Compare commits

..
10 Commits
3 changed files with 6 additions and 36 deletions
+1
View File
@@ -26,6 +26,7 @@ if __name__ == "__main__":
minter.store(f"{args.name}.noid") minter.store(f"{args.name}.noid")
case "mint": case "mint":
minter = Minter.load(f"{args.name}.noid") minter = Minter.load(f"{args.name}.noid")
print(minter)
print(f"New noid: {minter.genid()}") print(f"New noid: {minter.genid()}")
minter.store(f"{args.name}.noid") minter.store(f"{args.name}.noid")
case _: case _:
+4 -35
View File
@@ -17,7 +17,6 @@ class Counter():
@dataclass @dataclass
class Minter(Persistent): class Minter(Persistent):
legalstring: ClassVar[str] = "0123456789bcdfghjkmnpqrstvwxz" legalstring: ClassVar[str] = "0123456789bcdfghjkmnpqrstvwxz"
xdig: tuple[str] = tuple(legalstring)
alphacount: ClassVar[int] = len(legalstring) alphacount: ClassVar[int] = len(legalstring)
digitcount: ClassVar[int] = 10 digitcount: ClassVar[int] = 10
@@ -139,6 +138,7 @@ class Minter(Persistent):
# 8. return s # 8. return s
s = "" s = ""
varwidth = 0 varwidth = 0
xdig = list(self.legalstring)
rmask = list(self.template) rmask = list(self.template)
rmask.reverse() rmask.reverse()
while num != 0 or not varwidth: while num != 0 or not varwidth:
@@ -164,44 +164,13 @@ class Minter(Persistent):
continue continue
remainder = num % div remainder = num % div
num = int(num / div) num = int(num / div)
s = self.xdig[remainder] + s s = xdig[remainder] + s
if self.template[-1] == "k": if self.template[-1] == "k":
s += "+" s += "+"
return s return s
def _checkchar(self, id): def _checkbar(self, id):
# 1. Return undef if id is not set pass
# 2. init lastchar to last char of the id, and remove that char from id
# 3. init pos to 1
# 4. init sum to 0
# 5. declare c
# 6. for each character of the id
# - get the ordinal value of the character
# - increment sum with the pos * the index value of the char in legalstring
# - increment pos
# 7. init checkchar to the value of xdig[sum % alphacount]
# 8. return the concat of id and checkchar is lastchar equal to '+' or checkchar
# 9. return None
if id is None:
return None
lastchar = id[-1]
sum = 0
for idx, char in enumerate(id[:-1], 1):
sum += idx * (self.legalstring.index(char) if self.legalstring.index(char) else 0)
checkchar = self.xdig[sum % self.alphacount]
if lastchar == "+" or lastchar == checkchar:
return id[:-1] + checkchar
else:
return None
def mint(self, n=1):
for _ in range(n):
_id = self.genid()
if self.template[-1] == "k":
_id = self._checkchar(_id)
if _id is not None:
return self.prefix + _id
return self.prefix + _id
if __name__ == "__main__": if __name__ == "__main__":
+1 -1
View File
@@ -17,7 +17,7 @@ if __name__ == "__main__":
for i in range(noid.oatop + 2): for i in range(noid.oatop + 2):
print(f"--> {i=}") print(f"--> {i=}")
try: try:
n = noid.mint() n = noid.genid()
print("noid:", n) print("noid:", n)
print(noid.active_counters) print(noid.active_counters)
print(noid.inactive_counters) print(noid.inactive_counters)