Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make unit model hashable #186

Open
tstorek opened this issue Feb 23, 2023 · 2 comments · May be fixed by #187
Open

Make unit model hashable #186

tstorek opened this issue Feb 23, 2023 · 2 comments · May be fixed by #187
Labels
feature request Request a potential feature

Comments

@tstorek
Copy link
Collaborator

tstorek commented Feb 23, 2023

Is your feature request related to a problem? Please describe.
Currently, the units model is not hashable although the content should be immutable and, hence, frozen.
But due to the missing Config in the model the __hash__ function is not generated by pydantic.

Describe the solution you'd like
I would like to add the following content to the model:

class Unit(BaseModel):
    ...

    class Config:
        extra = 'ignore'
        allow_population_by_field_name = True
        allow_mutation = False
        frozen = True
           

Describe alternatives you've considered
Inherit, the Unit model and add a specific Config object. However, this may lead to inconssitency

Additional context
Use case:
I have a list ob sensors that also contains information about the unit.
For mapping measurements to the sepcific sensors I regularly must loop throuth this list. Therefore, caching such lookup can be wuite useful. Caching though requires hashing of the Objects in the list that I qant to operate on.

@tstorek tstorek added the feature request Request a potential feature label Feb 23, 2023
@djs0109
Copy link
Contributor

djs0109 commented Mar 2, 2023

Hello @tstorek , thanks for the detailed report, it makes sense to me. Can you describe a bit more what do you mean by "caching", and what kind of error you have received while trying to cach? It could be useful to other users, who later come into the same problem. But anyway, it is nice to hear from you! :)

@tstorek
Copy link
Collaborator Author

tstorek commented Mar 2, 2023

@djs0109 Python supports builtin caching of functions as long as all args are hashable. Using this minimized access time if the function call is identical with a former function call because the function reponse was already caluculated earlier. This is quite useful for filtering etc.

@djs0109 djs0109 linked a pull request Mar 3, 2023 that will close this issue
@djs0109 djs0109 linked a pull request Mar 3, 2023 that will close this issue
tstorek added a commit that referenced this issue May 31, 2023
tstorek added a commit that referenced this issue May 31, 2023
djs0109 pushed a commit that referenced this issue Jun 1, 2023
djs0109 pushed a commit that referenced this issue Jun 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request a potential feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants