-
Notifications
You must be signed in to change notification settings - Fork 72
refactor: cleaning of using package/global defaults #193
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b1b9405
refactor: cleaning of using package/global defaults
Borda 4469759
Literal
Borda e24fde2
MissingMongetter
Borda af0c6bf
typos
Borda 76317eb
cleaning
Borda a7a5cc4
types
Borda 21acefc
note
Borda ad71d89
Merge branch 'master' into refactor/defaults
Borda cb1ffbb
isort
Borda d1408a6
move loweer
Borda 19bcb4f
resolve
Borda 28fcf7c
global
Borda fcee4e0
nonlocal
Borda 3a1ed04
import
Borda 3d34945
fall
Borda 8d258d7
core = _MongoCore(None, _test_mongetter, 0)
Borda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from typing import TYPE_CHECKING, Callable, Literal | ||
|
|
||
| if TYPE_CHECKING: | ||
| import pymongo.collection | ||
|
|
||
|
|
||
| HashFunc = Callable[..., str] | ||
| Mongetter = Callable[[], "pymongo.collection.Collection"] | ||
| Backend = Literal["pickle", "mongo", "memory"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import datetime | ||
| import hashlib | ||
| import os | ||
| import pickle | ||
| from typing import Optional, TypedDict, Union | ||
|
|
||
| from ._types import Backend, HashFunc, Mongetter | ||
|
|
||
|
|
||
| def _default_hash_func(args, kwds): | ||
| # Sort the kwargs to ensure consistent ordering | ||
| sorted_kwargs = sorted(kwds.items()) | ||
| # Serialize args and sorted_kwargs using pickle or similar | ||
| serialized = pickle.dumps((args, sorted_kwargs)) | ||
| # Create a hash of the serialized data | ||
| return hashlib.sha256(serialized).hexdigest() | ||
|
|
||
|
|
||
| class Params(TypedDict): | ||
| caching_enabled: bool | ||
| hash_func: HashFunc | ||
| backend: Backend | ||
| mongetter: Optional[Mongetter] | ||
| stale_after: datetime.timedelta | ||
| next_time: bool | ||
| cache_dir: Union[str, os.PathLike] | ||
| pickle_reload: bool | ||
| separate_files: bool | ||
| wait_for_calc_timeout: int | ||
| allow_none: bool | ||
|
|
||
|
|
||
| _default_params: Params = { | ||
| "caching_enabled": True, | ||
| "hash_func": _default_hash_func, | ||
| "backend": "pickle", | ||
| "mongetter": None, | ||
| "stale_after": datetime.timedelta.max, | ||
| "next_time": False, | ||
| "cache_dir": "~/.cachier/", | ||
| "pickle_reload": True, | ||
| "separate_files": False, | ||
| "wait_for_calc_timeout": 0, | ||
| "allow_none": False, | ||
| } | ||
|
|
||
|
|
||
| def _update_with_defaults(param, name: str): | ||
| import cachier | ||
|
|
||
| if param is None: | ||
| return cachier.config._default_params[name] | ||
| return param | ||
|
|
||
|
|
||
| def set_default_params(**params): | ||
| """Configure global parameters applicable to all memoized functions. | ||
|
|
||
| This function takes the same keyword parameters as the ones defined in the | ||
| decorator, which can be passed all at once or with multiple calls. | ||
| Parameters given directly to a decorator take precedence over any values | ||
| set by this function. | ||
|
|
||
| Only 'stale_after', 'next_time', and 'wait_for_calc_timeout' can be changed | ||
| after the memoization decorator has been applied. Other parameters will | ||
| only have an effect on decorators applied after this function is run. | ||
|
|
||
| """ | ||
| import cachier | ||
|
|
||
| valid_params = ( | ||
| p for p in params.items() if p[0] in cachier.config._default_params | ||
| ) | ||
| _default_params.update(valid_params) | ||
|
|
||
|
|
||
| def get_default_params(): | ||
| """Get current set of default parameters.""" | ||
| import cachier | ||
|
|
||
| return cachier.config._default_params | ||
|
|
||
|
|
||
| def enable_caching(): | ||
| """Enable caching globally.""" | ||
| import cachier | ||
|
|
||
| cachier.config._default_params["caching_enabled"] = True | ||
|
|
||
|
|
||
| def disable_caching(): | ||
| """Disable caching globally.""" | ||
| import cachier | ||
|
|
||
| cachier.config._default_params["caching_enabled"] = False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.