def generate_short_id(): """ Short ID generator - v2: Shuffled XOR UUID1 """ num = uuid1().int mask = (1 << 64) - 1 result = (num ^ (num >> 1)) & 0x55555555555555555555555555555555 result = ((mask & (result >> 64)) << 1) | (mask & result) return base62().encode(result)
def test_bases(): b36 = base36() b52 = base52() b56 = base56() b58 = base58() b62 = base62() b94 = base94()
def post(self): user = self.get_current_user() trip = dict() trip_fields = { 'start_date': 'start_date', 'end_date': 'end_date', 'description': 'description', 'place_id': 'place_id', 'address': 'formatted_address', 'locality': 'locality', 'region': 'administrative_area_level_1', 'county': 'administrative_area_level_2', 'longitude': 'lng', 'latitude': 'lat' } for key in trip_fields: trip[key] = self.get_argument(trip_fields[key], None) trip_uuid = simpleflake() trip['trip_id'] = base62().hash(trip_uuid, 12) trip['created_at'] = r.now() trip['updated_at'] = r.now() trip['creator_user_id'] = user['id'] trip['geo'] = r.point(float(trip['longitude']), float(trip['latitude'])) r.table("trip").insert(trip).run() self.redirect("/")
def _get_short_string(string): sha = hashlib.sha384(string.encode()) digest = sha.digest() for i in range(StringShortner.FOLDS): digest = StringShortner._fold_hex_series(digest) number = StringShortner._num_from_hex_series(digest) base62 = basehash.base62(length=StringShortner.STRING_LEN) return base62.hash(number)
def convert_short_id(n): """ Short ID converter - v1: Half Unshuffled UUID1 """ x = (n ^ (n >> 1)) & 0x55555555555555555555555555555555 x = (x | (x >> 1)) & 0x33333333333333333333333333333333 x = (x | (x >> 2)) & 0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F x = (x | (x >> 4)) & 0x00FF00FF00FF00FF00FF00FF00FF00FF x = (x | (x >> 8)) & 0x0000FFFF0000FFFF0000FFFF0000FFFF x = (x | (x >> 16)) & 0x00000000FFFFFFFF00000000FFFFFFFF x = (x | (x >> 32)) & 0x0000000000000000FFFFFFFFFFFFFFFF return base62().encode(x)
def generate_short_id(): """ Short ID generator - v1: Half Unshuffled UUID1 """ n = uuid1().int x = (n ^ (n >> 1)) & 0x55555555555555555555555555555555 x = (x | (x >> 1)) & 0x33333333333333333333333333333333 x = (x | (x >> 2)) & 0x0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F x = (x | (x >> 4)) & 0x00FF00FF00FF00FF00FF00FF00FF00FF x = (x | (x >> 8)) & 0x0000FFFF0000FFFF0000FFFF0000FFFF x = (x | (x >> 16)) & 0x00000000FFFFFFFF00000000FFFFFFFF x = (x | (x >> 32)) & 0x0000000000000000FFFFFFFFFFFFFFFF return base62().encode(x)
def generate_urlcode(redirect_url): new_id = yield tornado.gen.Task(c.incr, settings.URL_CODE_ID_REDIS_NAME) code = basehash.base62().encode(new_id) # set redis cache yield tornado.gen.Task(c.sadd, settings.URL_CODE_SET_NAME, code) yield tornado.gen.Task(c.set, settings.URL_CODE_REDIS_BASE_NAME.format(code=code), redirect_url) raise tornado.gen.Return(code)
def post(self): user = self.get_current_user() event = dict() event_fields = [ 'title', 'start_date', 'end_date', 'start_time', 'end_time', 'description', 'website' ] venue = dict() venue_fields = [ 'foursquare_id', 'name', 'address', 'locality', 'region', 'postal_code', 'longitude', 'latitude' ] for key in event_fields: event[key] = self.get_argument(key, None) for key in venue_fields: venue[key] = self.get_argument(key, None) event_uuid = simpleflake() event['event_id'] = base62().hash(event_uuid, 12) venue_uuid = simpleflake() venue['venue_id'] = base62().hash(venue_uuid, 12) event['venue_id'] = venue['venue_id'] event['created_at'] = r.now() event['updated_at'] = r.now() event['creator_user_id'] = user['id'] venue['geo'] = r.point(float(venue['longitude']), float(venue['latitude'])) venue['created_at'] = r.now() venue['updated_at'] = r.now() venue['creator_user_id'] = user['id'] r.table("event").insert(event).run() r.table("venue").insert(venue).run() self.redirect("/")
def asset_by_id(module, id): base = base62() encoded_id = base.encode(id) assets = '/myallegan/myallegan/static/assets/{}'.format(module) files = [f.name for f in scandir(assets) if f.is_file()] asset_ext = None for file_name in files: if encoded_id in file_name: asset_ext = file_name.split('.')[-1] if asset_ext: r = '/static/assets/{module}/{id}.{ext}' return r.format(module=module, id=encoded_id, ext=asset_ext) else: return None
def generate_short_id(): """ Short ID generator - v0: Base62-Encoded UUID1 """ return base62().encode(uuid1().int)
def convert_short_id(num): """ Short ID converter - v0: Base62-Encoded UUID1 """ return base62().encode(num)
def b62encode(text: str) -> str: return base62().encode(int.from_bytes(text.encode(), byteorder='big'))
def url_safe_base62_uuid() -> str: return base62().encode(uuid.uuid4().int)
from sqlalchemy.dialects.postgresql.base import UUID from sqlalchemy_utils import ScalarListType from extensions import db from basehash import base62 from ._base import ModelMixin base62 = base62(6) class ComparisonResponse(db.Model, ModelMixin): __tablename__ = "comparison_responses" response_hash = db.Column(db.String) data = db.Column(db.JSON) @classmethod def get_by_hash(cls, response_hash): response = ComparisonResponse.query.filter(cls.response_hash == response_hash).first() return response
import basehash from flask import current_app as app from path import Path from sqlalchemy.types import Boolean, DateTime, Integer from sqlalchemy.schema import Column from datetime import datetime from passgen.extensions import db, ma _base62 = basehash.base62() class Order(db.Model): """DB representation of a single pass order. Basically needed as a big counter.""" __tablename__ = 'orders' id = Column(Integer, primary_key=True) range_from = Column(Integer, nullable=True) range_size = Column(Integer, nullable=True) ordered = Column(DateTime(timezone=False), nullable=False) finished = Column(DateTime(timezone=False), nullable=True) progress = Column(Integer, default=0, nullable=False) single_page = Column(Boolean, default=False, nullable=False) @classmethod def create(cls, size=1, single_page=False): """Create a new order, put it into the database and return the id"""
from nose.tools import raises from basehash import base62 bh62 = base62() bh6 = base62(6) bh10 = base62(10) def test_base62_encode_with_1234567890_1LY7VK(): assert bh62.encode(1234567890) == '1LY7VK' def test_base62_decode_with_1LY7VK_1234567890(): assert bh62.decode('1LY7VK') == 1234567890 def test_base62_hash_with_1234567890_10_RERZ0NM8Na(): assert bh10.hash(1234567890) == 'RERZ0NM8Na' def test_base62_unhash_with_RERZ0NM8Na_1234567890(): assert bh10.unhash('RERZ0NM8Na') == 1234567890 def test_base62_maximum_value_with_6_56800235583(): assert bh6.maximum == 56800235583 @raises(ValueError)
from basehash import HASH_LENGTH, base36, base52, base56, base58, base62, base94 base36 = base36() base52 = base52() base56 = base56() base58 = base58() base62 = base62() base94 = base94() # Base encode an integer/long print(base36.encode(200)) print(base52.encode(200)) print(base56.encode(200)) print(base58.encode(200)) print(base62.encode(200)) print(base94.encode(200)) # Base decode encoded string. print(base36.decode('5K')) print(base52.decode('3r')) print(base56.decode('5a')) print(base58.decode('4T')) print(base62.decode('3E')) print(base94.decode('#-')) # Base hash an integer/long # Takes an option param, length, which is defaulted to # basehash.base.HASH_LENGTH print(base36.hash(200)) print(base52.hash(200)) print(base56.hash(200))
from nose.tools import raises from basehash import base62 bh62 = base62() bh6 = base62(6) bh10 = base62(10) def test_base62_encode_with_1234567890_1LY7VK(): assert bh62.encode(1234567890) == '1LY7VK' def test_base62_decode_with_1LY7VK_1234567890(): assert bh62.decode('1LY7VK') == 1234567890 def test_base62_hash_with_1234567890_10_RERZ0NM8Na(): assert bh10.hash(1234567890) == 'RERZ0NM8Na' def test_base62_unhash_with_RERZ0NM8Na_1234567890(): assert bh10.unhash('RERZ0NM8Na') == 1234567890 def test_base62_maximum_value_with_6_56800235583(): assert bh6.maximum == 56800235583 @raises(ValueError) def test_base62_hash_fail_with_56800235584_6():
def convert_short_id(num): """ Short ID converter - v2: Shuffled XOR UUID1 """ mask = (1 << 64) - 1 result = (num ^ (num >> 1)) & 0x55555555555555555555555555555555 result = ((mask & (result >> 64)) << 1) | (mask & result) return base62().encode(result)
def generate_short_id(): """ Short ID generator - v4: Urandom """ return base62().encode(unpack("<Q", urandom(8))[0])
from nose.tools import raises from basehash import base62 base62 = base62() def test_base62_encode_with_1234567890_1LY7VK(): assert base62.encode(1234567890) == '1LY7VK' def test_base62_decode_with_1LY7VK_1234567890(): assert base62.decode('1LY7VK') == 1234567890 def test_base62_hash_with_1234567890_10_RERZ0NM8Na(): assert base62.hash(1234567890, 10) == 'RERZ0NM8Na' def test_base62_unhash_with_RERZ0NM8Na_1234567890(): assert base62.unhash('RERZ0NM8Na') == 1234567890 def test_base62_maximum_value_with_6_56800235583(): assert base62.maximum_value(6) == 56800235583 @raises(ValueError) def test_base62_hash_fail_with_56800235584_6(): assert base62.hash(56800235584, 6)