add support for consistent unique id generation base on index tuple
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 0
- Forks
- 6
- Avg merge
- 12h 25m
- Merged PRs (30d)
- 5
Description
Consider below model:
entity A:
number n
end
index A(n)
entity AContainer:
end
AContainer.a [0:] -- A
and assume you want to generate a unique id for each A in an AContainer instance (for example the rule number from the bics bootcamp day 3). It would be nice to support generation of such an id.
A proof of concept that still needs some work:
`main.cf:
entity TestEntity:
string unique
end
index TestEntity(unique)
implement TestEntity using std::none
collector = IdCollector(instance_index_field = "unique")
Identifiable(collector = collector, instance = TestEntity(unique = "0"))
Identifiable(collector = collector, instance = TestEntity(unique = "1"))
Identifiable(collector = collector, instance = TestEntity(unique = "2"))
Identifiable(collector = collector, instance = TestEntity(unique = "3"))
Identifiable(collector = collector, instance = TestEntity(unique = "4"))
libs/auto_id/model/_init.cf
# entity responsible for id assignment to it's instances based on the instances' field with name instance_index_field. In practice, this should be a list of strings to support multi-field indices.
entity IdCollector:
string instance_index_field
end
implement IdCollector using collect_ids
# will receive an id from its IdCollector
entity Identifiable:
number id
end
index Identifiable(instance, collector)
IdCollector.instances [0:] -- Identifiable.collector [1]
# TODO: use inheritance over composition? Instances could inherit from Identifiable instead of binding them together.
Identifiable.instance [1] -- std::Entity
implement Identifiable using std::none
implementation collect_ids for IdCollector:
# plugin returns a dict from index value to assigned id. TODO: this method only works for single-field string indices
id_map = map_unique_ids(self.instances, self.instance_index_field, 11)
for i in self.instances:
instance_index_value = i.instance.unique
i.id = id_map[instance_index_value]
std::print("unique: {{i.instance.unique}}, id: {{i.id}}")
end
end
libs/auto_id/plugins/__init__.py
from operator import attrgetter
from inmanta.plugins import plugin
@plugin
def map_unique_ids(collection: "list", index_field: "string", offset: "number") -> "dict":
"""
"""
sorted_collection = sorted(
collection,
key=lambda i: attrgetter(index_field)(i.instance)
)
return {attrgetter(index_field)(instance.instance): offset + i for i, instance in enumerate(sorted_collection)}
# vim: set tabstop=4 shiftwidth=4 expandtab:
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the proof of concept in libs/auto_id/model/_init.cf and libs/auto_id/plugins/init.py, focusing on IdCollector, Identifiable, and map_unique_ids. The issue is done when ID generation consistently supports index tuples rather than only a single string field, with the model and plugin behavior aligned.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100