Skip to content

Instantly share code, notes, and snippets.

@Colk-tech
Last active April 2, 2023 13:45
Show Gist options
  • Save Colk-tech/1e9eab04c3fddcf0e091de8bc28cd8a4 to your computer and use it in GitHub Desktop.
Save Colk-tech/1e9eab04c3fddcf0e091de8bc28cd8a4 to your computer and use it in GitHub Desktop.
UUID -> docsid converter written in Python
from sqlalchemy import Column, String
from models.types.uuid import UUIDColumn
from models.mixin.timestamp import TimeStampMixin
from db import Base
class IDPair(Base, TimeStampMixin):
__tablename__: str = "documents"
uid = UUIDColumn(
column_name="uid",
)
docsid = Column(
String(4),
)
from typing import Optional
import asyncio
import uuid
import hashlib
from models import id_pair as idp_model
from cruds import id_pair as idp_cruds
from db import Session
async def main():
RED = '\033[35m'
END = '\033[0m'
number_of_records: int = 0
while number_of_records <= 65536:
uid: uuid.UUID = uuid.uuid4()
docsid: str = hashlib.sha256(uid.bytes).hexdigest()[:4].upper()
# number_of_id_pair: int = await idp_cruds.get_number_of_id_pair(Session)
# if number_of_id_pair >= 65536:
# break
already_existing_id_pair: Optional[idp_model.IDPair] = await idp_cruds.get_user_by_docs_id(
Session, docsid
)
if already_existing_id_pair is not None:
print(f"{RED}===== FOUND DUPLICATE =====\n"
f"{docsid}{END}\n\n")
continue
idp: idp_model.IDPair = await idp_cruds.create_id_pair(
Session, uid, docsid
)
number_of_records += 1
print(f"{number_of_records}", idp.uid, idp.docsid, "\n\n")
if __name__ == "__main__":
asyncio.run(main())
import uuid
import hashlib
def uuid_to_docsid(uid: uuid.UUID) -> str:
docsid: str = hashlib.sha256(uid.bytes).hexdigest()[:4].upper()
return docsid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment