madgui.util.misc module¶
Misc programming toolbox.
Functions
|
Decorator for cached method that remembers its result from the first execution and returns this in all subsequent calls rather than executing the function again. |
|
Invalidate cache for memoized function. |
|
Decorator for cached, writeable properties. |
|
Identify groups of consecutive numbers in a list. |
|
Strip a suffix from a string, if present. |
|
Try to make |
|
Expand ‘~’ and environment variables in a user-given path string. |
-
madgui.util.misc.
cachedproperty
(func)[source]¶ Decorator for cached, writeable properties.
>>> class Foo: ... @cachedproperty ... def bar(self): ... return ['bar']
>>> foo = Foo() >>> foo.bar ['bar']
>>> foo.bar is foo.bar True
>>> foo.bar = 'qux' >>> foo.bar 'qux'
>>> del foo.bar >>> foo.bar ['bar']
-
madgui.util.misc.
memoize
(func)[source]¶ Decorator for cached method that remembers its result from the first execution and returns this in all subsequent calls rather than executing the function again.
Example:
>>> class Foo: ... @memoize ... def bar(self): ... print("executing bar…") ... return 'bar'
>>> foo = Foo() >>> foo.bar() executing bar… 'bar' >>> foo.bar() 'bar'
The cached result can be cleared using
invalidate
:>>> invalidate(foo, 'bar') >>> foo.bar() executing bar… 'bar'
If arguments are passed, the result is always recomputed.
-
madgui.util.misc.
ranges
(nums)[source]¶ Identify groups of consecutive numbers in a list. Returns a list of intervals
[start, end)
as tuples.