def add(self, user_id, name): '''Create new stream for given telegram user Args: user_id(str): Telegram user ID (chat ID) name(str): The name of the stream Returns: str: Stream key (secret) ''' # Generate a unique stream key alphabet = "0123456789ABCDEFGHIJKLMNPQRSTUVWXYZ" converter = baseconv.BaseConverter(alphabet) codeLength = 32 secret = converter.encode(uuid.uuid4().int)[0:codeLength] # Check the uniqueness if secret in self.__streams: log_main.error("Uniqueness error when adding a stream: %s", secret) return None # Add stream stream_status = int(StreamStatus.active) def addStreamSQL(): '''Get all existing streams from database''' with Database().getConnection().cursor() as cursor: sql = "INSERT INTO " + STREAMS_TABLE + " (user_id, secret, name, status) VALUES (%s, %s, %s, %s)" cursor.execute(sql, (user_id, secret, name, stream_status)) Database().getConnection().commit() return Database().getConnection().insert_id() res = Database().execute(addStreamSQL) if res['status'] is False: log_main.error("Error when adding a new stream") return None # Add to cache stream_id = res['result'] if stream_id is not None: stream = Stream(stream_id, user_id, secret, name, stream_status) self.__streams[secret] = stream log_main.info('Added new stream "%s" for user %s with the key: %s', name, user_id, secret) return secret # If it was not added raise Exception("Couldn't add new stream")
def return_time_list(ts, base=10): """Returns array of [yy, m, w, d, h, mn, sc] in base (default 10) :param int ts: unix timestamp. :param int base: number base for output. default 10. valid 2-64. """ tl = [] bconv = baseconv.BaseConverter(baseconv.BASE64_ALPHABET[:base]) t = bconv.encode(ts) e = None if len(t) < 12: t = "0" * (10 - len(t)) + t for i in [-2, -4, -5, -6, -7, -8]: tl.append(t[i:e]) e = i rest = t[:-8] tl.append(rest) tl.reverse() return tl
def base62uuid(): ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' converter = baseconv.BaseConverter(ALPHABET) uuid4_as_hex = str(uuid.uuid4()).replace('-', '') uuid4_as_int = int(uuid4_as_hex, 16) return converter.encode(uuid4_as_int)
def base62uuid(): converter = baseconv.BaseConverter(ALPHABET) uuid4_as_hex = str(uuid.uuid4()).replace('-', '') uuid4_as_int = int(uuid4_as_hex, 16) return converter.encode(uuid4_as_int)
from collections import Counter, defaultdict from functools import lru_cache import itertools as it import baseconv from scipy.misc import comb BASES = 'ACGT' DNA_BASE4 = baseconv.BaseConverter(BASES) COMPLEMENTS = str.maketrans(BASES, 'TGCA') @lru_cache() def hamming_distance(s1, s2): return sum(l != r for l, r in zip(s1, s2)) def neighbors(pattern, dist_max, *, dist_method=hamming_distance): if dist_max == 0: return set([pattern]) if len(pattern) == 1: return set(BASES) neighborhood = set() head, tail = pattern[0], pattern[1:] neighbors_tail = neighbors(tail, dist_max, dist_method=dist_method) for neighbor in neighbors_tail: if dist_method(tail, neighbor) < dist_max: neighborhood.update({base + neighbor for base in BASES}) else: