Changeset 3a0b0ce in flexoentity
- Timestamp:
- 10/19/25 17:44:39 (3 months ago)
- Branches:
- master
- Children:
- 8aea9a0
- Parents:
- 811ce96
- File:
-
- 1 edited
-
flexoentity/id_factory.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
flexoentity/id_factory.py
r811ce96 r3a0b0ce 75 75 76 76 @staticmethod 77 def _ensure_unique(hash_part: str, version: int | None = None) -> str: 78 """Append disambiguator if hash was already seen this session.""" 79 candidate = f"{hash_part}-{version}" if version is not None else hash_part 80 if candidate not in FlexOID._seen_hashes: 81 FlexOID._seen_hashes.add(candidate) 82 return candidate 83 # fallback only if truly same hash+version (should never happen) 84 for suffix in itertools.chain(map(chr, range(65, 91)), range(1, 100)): 85 alt = f"{candidate}-{suffix}" 77 def _ensure_unique(hash_part: str) -> str: 78 """Append disambiguator only if the hash was already seen this session.""" 79 if hash_part not in FlexOID._seen_hashes: 80 FlexOID._seen_hashes.add(hash_part) 81 return hash_part 82 # fallback only if truly same hash (rare) 83 for suffix in range(1, 100): 84 alt = f"{hash_part}-{suffix}" 86 85 if alt not in FlexOID._seen_hashes: 87 86 FlexOID._seen_hashes.add(alt) … … 89 88 raise RuntimeError("Too many collisions; adjust hash length or logic.") 90 89 90 91 91 @staticmethod 92 92 def generate(domain: str, etype: str, estate: str, text: str, … … 109 109 base_hash = FlexOID._blake_hash(hash_seed) 110 110 unique_hash = ( 111 FlexOID._ensure_unique(base_hash , version) if enforce_unique else base_hash111 FlexOID._ensure_unique(base_hash) if enforce_unique else base_hash 112 112 ) 113 113 … … 120 120 121 121 return FlexOID(flexo_id_str, signature) # ────────────────────────────────────────────────────────────────────────── 122 123 124 @staticmethod125 def generate_old(domain: str, etype: str, estate: str, text: str,126 version: int = 1, enforce_unique = True):127 """128 Generate a new, versioned, and state-aware Flex-O ID.129 130 This is the primary constructor for new entities. It combines the domain,131 entity type, creation date, unique hash (derived from the canonicalized132 text and lifecycle state), version number, and state suffix into a133 deterministic but unique identifier string.134 135 Format:136 <domain>-<etype><date>-<hash>@<version><state>137 Example:138 AF-Q251019-9B3E2@001A139 140 Parameters141 ----------142 domain : str143 Two-to-six letter domain or organizational prefix (e.g. "AF").144 etype : str145 One-to-three letter entity type identifier (e.g. "RQ" for Question).146 estate : str147 Lifecycle state whose one-letter code is appended to the version.148 text : str149 Entity-specific text seed (used to derive a stable hash).150 version : int, optional151 Starting version number (default is 1).152 153 Returns154 -------155 FlexOID156 A new Flex-O ID object with unique hash and digital signature.157 158 Notes159 -----160 - Changing the text or state will produce a new hash and signature.161 - Versions start at 1 and increment through `next_version()`.162 """163 164 if not (1 <= version <= FlexOID.MAX_VERSION):165 raise ValueError(f"Version {version} exceeds limit; mark obsolete.")166 167 date_part = datetime.now(timezone.utc).strftime("%y%m%d")168 169 # include state in the seed so hash & signature depend on lifecycle state170 seed = canonical_seed(f"{text}:{estate}")171 172 base_hash = FlexOID._blake_hash(seed)173 unique_hash = FlexOID._ensure_unique(base_hash, version)if enforce_unique else base_hash174 175 ver_part = f"{version:03d}{estate}"176 flexo_id_str = f"{domain}-{etype}{date_part}-{unique_hash}@{ver_part}"177 178 # short signature derived from state-aware seed179 signature = hashlib.blake2s(seed.encode("utf-8"), digest_size=8).hexdigest().upper()180 181 return FlexOID(flexo_id_str, signature)182 122 183 123 @property
Note:
See TracChangeset
for help on using the changeset viewer.
