Refactor UUID cache - #184
Conversation
|
I think this looks very good and will make the scripts cleaner 👍 Only two thoughts so far:
|
|
I already thought about making a I could:
|
Hmm but then we have to make an extra call for that: uuid_cache = UuidCache('uuid_cache_axial_tht.csv')
uuid_cache_connectors = UuidCache('uuid_cache_connectors.csv')
uuid_cache_connectors.open() # either here
def main():
uuid_cache_connectors.open() # or here
with uuid_cache:
# ...
I think a flag for the read-only mode makes more sense because it can/should have more features:
IMHO we could even distinguish between those two modes (read-only vs. read-modify-write) by the usage of the
Though this distinguishing is neither explicit nor obvious, so I understand if you consider this a bad idea 😅 My alternative suggestion would be just to pass uuid_cache = UuidCache('uuid_cache_axial_tht.csv')
uuid_cache_connectors = UuidCache('uuid_cache_connectors.csv', read_only=True)
# read_only=True: open cache immediately, no stale check, no saving, exception when accessing nonexistent key
def __main__():
with uuid_cache:
# ...I think I'd still prefer the implicit mode, since then we cannot forget to pass uuid_cache = UuidCache('uuid_cache_axial_tht.csv') # opened read-only
uuid_cache_connectors = UuidCache('uuid_cache_connectors.csv') # opened read-only
def __main__():
with uuid_cache: # automatically switches to writable mode
# ...Anyway, I'm open for other ideas which cover those two use-cases in a simple-to-use way. |
|
Just a side note: As long as we set up that global UuidCache object which gets "initialized" in |
ubruhin
left a comment
There was a problem hiding this comment.
Thanks for the update. From looking at the code, I'd say this is good 👍
So the remaining generators can be updated - hopefully the new concept works as intended ;-)
|
|
||
| def __exit__(self, exception: Any, value: Any, traceback: Any) -> None: | ||
| if not exception and self.entered: | ||
| assert self.data is not None, 'Exiting non-entered generator' |
There was a problem hiding this comment.
No longer needed
| assert self.data is not None, 'Exiting non-entered generator' |
| @@ -93,10 +90,7 @@ def uuid(category: str, full_name: str, identifier: str) -> str: | |||
| identifier: | |||
| For example 'pad-1' or 'pin-13'. | |||
| """ | |||
| key = '{}-{}-{}'.format(category, full_name, identifier).lower().replace(' ', '~') | |||
| if key not in uuid_cache: | |||
| uuid_cache[key] = str(uuid4()) | |||
| return uuid_cache[key] | |||
| return uuid_cache.get(category, full_name, identifier) | |||
There was a problem hiding this comment.
Hmm this function is now just a plain wrapper without any additional functionality. IMO it should be removed then - up to you, no strong opinion.
@ubruhin : Would this kind of refactoring be okay? If yes, I will update the other generators accordingly.