IdGenerator
IdGenerator(scope, _counters=dict(), _seen=set())Generates unique IDs within a given scope.
Supports both: 1. Wrapping external IDs with the scope 2. Generating new IDs when none provided
Tracks seen IDs to detect collisions.
Attributes: scope: The scope to apply to all generated/wrapped IDs
Examples: >>> gen = IdGenerator(“midi”)
>>> gen.get_or_create("n42") # Wrap external ID
'midi:n42'
>>> gen.get_or_create(None, type_hint="note") # Generate
'midi:note_1'
>>> gen.get_or_create(None, type_hint="note") # Generate next
'midi:note_2'
Attributes
| Name | Description |
|---|---|
| count | Total number of IDs generated/wrapped. |
Methods
| Name | Description |
|---|---|
| create | Generate a new unique ID (no external ID). |
| get_or_create | Get a scoped ID string. |
| has_seen | Check if an ID has been generated/wrapped by this generator. |
| reset | Reset all counters and seen IDs. |
| reset_counters | Reset counters but keep seen IDs (for continuation). |
| wrap | Wrap an external ID with the scope. |
create
IdGenerator.create(type_hint='event')Generate a new unique ID (no external ID).
get_or_create
IdGenerator.get_or_create(external_id=None, type_hint='event')Get a scoped ID string.
Args: external_id: The ID from the source, if any. Will be wrapped with the scope but otherwise preserved. type_hint: A prefix for generated IDs if external_id is None.
Returns: A string in “scope:local_id” format (or just “local_id” if scope is empty).
has_seen
IdGenerator.has_seen(id_str)Check if an ID has been generated/wrapped by this generator.
reset
IdGenerator.reset()Reset all counters and seen IDs.
reset_counters
IdGenerator.reset_counters()Reset counters but keep seen IDs (for continuation).
wrap
IdGenerator.wrap(external_id)Wrap an external ID with the scope.