2019-10-24 14:41:10 +08:00
|
|
|
"""General catchall for functions that don't make sense as methods."""
|
2019-08-03 13:36:19 +08:00
|
|
|
import asyncio
|
2018-10-14 22:32:27 +08:00
|
|
|
import hashlib
|
|
|
|
import operator
|
|
|
|
|
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
async def gather_dict(dic):
|
|
|
|
cors = list(dic.values())
|
2018-10-14 22:32:27 +08:00
|
|
|
results = await asyncio.gather(*cors)
|
2019-01-16 01:41:41 +08:00
|
|
|
return dict(zip(dic.keys(), results))
|
2018-10-14 22:32:27 +08:00
|
|
|
|
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
def digest(string):
|
|
|
|
if not isinstance(string, bytes):
|
2019-08-01 06:00:12 +08:00
|
|
|
string = str(string).encode("utf8")
|
2019-01-16 01:41:41 +08:00
|
|
|
return hashlib.sha1(string).digest()
|
2018-10-14 22:32:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
class OrderedSet(list):
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
Acts like a list in all ways, except in the behavior of the.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2018-10-14 22:32:27 +08:00
|
|
|
:meth:`push` method.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def push(self, thing):
|
|
|
|
"""
|
|
|
|
1. If the item exists in the list, it's removed
|
|
|
|
2. The item is pushed to the end of the list
|
|
|
|
"""
|
|
|
|
if thing in self:
|
|
|
|
self.remove(thing)
|
|
|
|
self.append(thing)
|
|
|
|
|
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
def shared_prefix(args):
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
Find the shared prefix between the strings.
|
2018-10-14 22:32:27 +08:00
|
|
|
|
|
|
|
For instance:
|
|
|
|
|
|
|
|
sharedPrefix(['blahblah', 'blahwhat'])
|
|
|
|
|
|
|
|
returns 'blah'.
|
|
|
|
"""
|
|
|
|
i = 0
|
|
|
|
while i < min(map(len, args)):
|
|
|
|
if len(set(map(operator.itemgetter(i), args))) != 1:
|
|
|
|
break
|
|
|
|
i += 1
|
|
|
|
return args[0][:i]
|
|
|
|
|
|
|
|
|
2019-01-16 01:41:41 +08:00
|
|
|
def bytes_to_bit_string(bites):
|
2019-08-01 06:00:12 +08:00
|
|
|
bits = [bin(bite)[2:].rjust(8, "0") for bite in bites]
|
2018-10-14 22:32:27 +08:00
|
|
|
return "".join(bits)
|