transaction_id.py 572 Bytes
from time import time
from random import randrange


# Untuk Nomor Transaksi Pemda

class TransactionID:
    def __init__(self, timeout=3):
        self.timeout = timeout

    def create(self, prefix=''):
        awal = time()
        while True:
            acak = randrange(0, 99999999)
            tid = prefix + str(acak).zfill(8)
            if not self.is_found(tid):
                return tid
            if time() - awal > 3:
                raise Exception('Timeout saat membuat Transaction ID')

    # Override, please
    def is_found(self, tid):
        pass