Repository navigation
bpo-43780: Sync with importlib_metadata 3.10 #25297
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import collections | ||
|
|
||
|
|
||
| # from jaraco.collections 3.3 | ||
| class FreezableDefaultDict(collections.defaultdict): | ||
| """ | ||
| Often it is desirable to prevent the mutation of | ||
| a default dict after its initial construction, such | ||
| as to prevent mutation during iteration. | ||
| >>> dd = FreezableDefaultDict(list) | ||
| >>> dd[0].append('1') | ||
| >>> dd.freeze() | ||
| >>> dd[1] | ||
| [] | ||
| >>> len(dd) | ||
| 1 | ||
| """ | ||
|
|
||
| def __missing__(self, key): | ||
| return getattr(self, '_frozen', super().__missing__)(key) | ||
|
|
||
| def freeze(self): | ||
| self._frozen = lambda key: self.default_factory() | ||
|
|
||
|
|
||
| class Pair(collections.namedtuple('Pair', 'name value')): | ||
| @classmethod | ||
| def parse(cls, text): | ||
| return cls(*map(str.strip, text.split("=", 1))) |
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,85 @@ | ||
| import types | ||
| import functools | ||
|
|
||
|
|
||
| # from jaraco.functools 3.3 | ||
| def method_cache(method, cache_wrapper=None): | ||
| """ | ||
| Wrap lru_cache to support storing the cache data in the object instances. | ||
|
|
||
| Abstracts the common paradigm where the method explicitly saves an | ||
| underscore-prefixed protected property on first call and returns that | ||
| subsequently. | ||
|
|
||
| >>> class MyClass: | ||
| ... calls = 0 | ||
| ... | ||
| ... @method_cache | ||
| ... def method(self, value): | ||
| ... self.calls += 1 | ||
| ... return value | ||
|
|
||
| >>> a = MyClass() | ||
| >>> a.method(3) | ||
| 3 | ||
| >>> for x in range(75): | ||
| ... res = a.method(x) | ||
| >>> a.calls | ||
| 75 | ||
|
|
||
| Note that the apparent behavior will be exactly like that of lru_cache | ||
| except that the cache is stored on each instance, so values in one | ||
| instance will not flush values from another, and when an instance is | ||
| deleted, so are the cached values for that instance. | ||
|
|
||
| >>> b = MyClass() | ||
| >>> for x in range(35): | ||
| ... res = b.method(x) | ||
| >>> b.calls | ||
| 35 | ||
| >>> a.method(0) | ||
| 0 | ||
| >>> a.calls | ||
| 75 | ||
|
|
||
| Note that if method had been decorated with ``functools.lru_cache()``, | ||
| a.calls would have been 76 (due to the cached value of 0 having been | ||
| flushed by the 'b' instance). | ||
|
|
||
| Clear the cache with ``.cache_clear()`` | ||
|
|
||
| >>> a.method.cache_clear() | ||
|
|
||
| Same for a method that hasn't yet been called. | ||
|
|
||
| >>> c = MyClass() | ||
| >>> c.method.cache_clear() | ||
|
|
||
| Another cache wrapper may be supplied: | ||
|
|
||
| >>> cache = functools.lru_cache(maxsize=2) | ||
| >>> MyClass.method2 = method_cache(lambda self: 3, cache_wrapper=cache) | ||
| >>> a = MyClass() | ||
| >>> a.method2() | ||
| 3 | ||
|
|
||
| Caution - do not subsequently wrap the method with another decorator, such | ||
| as ``@property``, which changes the semantics of the function. | ||
|
|
||
| See also | ||
| http://code.activestate.com/recipes/577452-a-memoize-decorator-for-instance-methods/ | ||
| for another implementation and additional justification. | ||
| """ | ||
| cache_wrapper = cache_wrapper or functools.lru_cache() | ||
|
|
||
| def wrapper(self, *args, **kwargs): | ||
| # it's the first call, replace the method with a cached, bound method | ||
| bound_method = types.MethodType(method, self) | ||
| cached_method = cache_wrapper(bound_method) | ||
| setattr(self, method.__name__, cached_method) | ||
| return cached_method(*args, **kwargs) | ||
|
|
||
| # Support cache clear even before cache has been created. | ||
| wrapper.cache_clear = lambda: None | ||
|
|
||
| return wrapper |
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.