Skip to content

Commit 4624d17

Browse files
committed
-
1 parent b37350c commit 4624d17

File tree

9 files changed

+58
-26
lines changed

9 files changed

+58
-26
lines changed

source_py3/python_toolbox/caching/decorators.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import functools
1212
import datetime as datetime_module
1313

14+
from python_toolbox import misc_tools
1415
from python_toolbox import binary_search
1516
from python_toolbox import decorator_tools
1617
from python_toolbox.sleek_reffing import SleekCallArgs
@@ -93,7 +94,6 @@ def decorator(function):
9394

9495
if time_to_keep:
9596

96-
cache_dict = OrderedDict()
9797
sorting_key_function = lambda sleek_call_args: \
9898
cached._cache[sleek_call_args][1]
9999

@@ -111,11 +111,11 @@ def remove_expired_entries():
111111
for key in list(cached._cache.keys())[:cutting_point]:
112112
del cached._cache[key]
113113

114-
114+
@misc_tools.set_attributes(_cache=OrderedDict())
115115
def cached(function, *args, **kwargs):
116116
remove_expired_entries()
117117
sleek_call_args = \
118-
SleekCallArgs(cache_dict, function, *args, **kwargs)
118+
SleekCallArgs(cached._cache, function, *args, **kwargs)
119119
try:
120120
return cached._cache[sleek_call_args][0]
121121
except KeyError:
@@ -129,11 +129,10 @@ def cached(function, *args, **kwargs):
129129

130130
else: # not time_to_keep
131131

132-
cache_dict = {}
133-
132+
@misc_tools.set_attributes(_cache={})
134133
def cached(function, *args, **kwargs):
135134
sleek_call_args = \
136-
SleekCallArgs(cache_dict, function, *args, **kwargs)
135+
SleekCallArgs(cached._cache, function, *args, **kwargs)
137136
try:
138137
return cached._cache[sleek_call_args]
139138
except KeyError:
@@ -143,11 +142,10 @@ def cached(function, *args, **kwargs):
143142

144143
else: # max_size < infinity
145144

146-
cache_dict = OrderedDict()
147-
145+
@misc_tools.set_attributes(_cache=OrderedDict())
148146
def cached(function, *args, **kwargs):
149147
sleek_call_args = \
150-
SleekCallArgs(cache_dict, function, *args, **kwargs)
148+
SleekCallArgs(cached._cache, function, *args, **kwargs)
151149
try:
152150
result = cached._cache[sleek_call_args]
153151
cached._cache.move_to_end(sleek_call_args)
@@ -159,7 +157,6 @@ def cached(function, *args, **kwargs):
159157
cached._cache.popitem(last=False)
160158
return value
161159

162-
cached._cache = cache_dict
163160

164161
result = decorator_tools.decorator(cached, function)
165162

source_py3/python_toolbox/misc_tools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
is_magic_variable_name, get_actual_type, is_number, identity_function,
1010
do_nothing, OwnNameDiscoveringDescriptor, find_clear_place_on_circle,
1111
general_sum, general_product, is_legal_email_address, is_type, NonInstatiable,
12-
repeat_getattr, add_extension_if_plain
12+
repeat_getattr, add_extension_if_plain, set_attributes, pocket
1313
)
1414
from . import name_mangling
1515
from .proxy_property import ProxyProperty

source_py3/python_toolbox/misc_tools/misc_tools.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import math
1010
import types
1111
import functools
12+
import threading
1213

1314
from python_toolbox import cute_iter_tools
1415

@@ -293,4 +294,23 @@ def repeat_getattr(thing, query):
293294
current = thing
294295
for attribute_name in attribute_names:
295296
current = getattr(current, attribute_name)
296-
return current
297+
return current
298+
299+
300+
def set_attributes(**kwargs):
301+
def decorator(function):
302+
for key, value in kwargs.items():
303+
setattr(function, key, value)
304+
return function
305+
return decorator
306+
307+
308+
pocket_container = threading.local()
309+
pocket_container.value = None
310+
@set_attributes(container=pocket_container)
311+
def pocket(*args):
312+
if args:
313+
(pocket.container.value,) = args
314+
else:
315+
return pocket.container.value
316+

source_py3/python_toolbox/string_tools/string_tools.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import sys
77
import re
88

9+
from python_toolbox import cute_iter_tools
10+
911

1012
def docstring_trim(docstring):
1113
'''Trim a docstring, removing redundant tabs.'''
@@ -69,4 +71,10 @@ def rreplace(s, old, new, count=None):
6971
'''
7072
return new.join(s.rsplit(old, count) if count is not None
7173
else s.rsplit(old))
72-
74+
75+
76+
def split(string, separator, max_split=-1, blank_fill=False):
77+
return tuple(
78+
cute_iter_tools.fill(string.split(separator, max_split),
79+
fill_value='')
80+
)

source_py3/test_python_toolbox/test_caching/test_cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212

1313
from python_toolbox import caching
1414
from python_toolbox.caching import cache
15+
from python_toolbox import misc_tools
1516
from python_toolbox import temp_value_setting
1617
from python_toolbox import cute_testing
1718
from python_toolbox import gc_tools
1819

1920

21+
@misc_tools.set_attributes(i=0)
2022
def counting_func(a=1, b=2, *args, **kwargs):
2123
'''Function that returns a bigger number every time.'''
22-
if not hasattr(counting_func, 'i'):
23-
counting_func.i = 0
2424
try:
2525
return counting_func.i
2626
finally:
27-
counting_func.i = (counting_func.i + 1)
27+
counting_func.i += 1
2828

2929

3030
def test_basic():

source_py3/test_python_toolbox/test_caching/test_cached_property.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
import nose
77

88
from python_toolbox import context_management
9+
from python_toolbox import misc_tools
910

1011
from python_toolbox.caching import cache, CachedType, CachedProperty
1112

1213

14+
@misc_tools.set_attributes(i=0)
1315
def counting_func(self):
1416
'''Return a bigger number every time.'''
15-
if not hasattr(counting_func, 'i'):
16-
counting_func.i = 0
1717
try:
1818
return counting_func.i
1919
finally:
20-
counting_func.i = (counting_func.i + 1)
20+
counting_func.i += 1
2121

2222

2323
def test():

source_py3/test_python_toolbox/test_emitting/test_emitter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from python_toolbox import misc_tools
2+
13
from python_toolbox import emitting
24

35

@@ -7,9 +9,9 @@ def test():
79
emitter_2 = emitting.Emitter(inputs=(emitter_1,))
810
emitter_0 = emitting.Emitter(outputs=(emitter_1,))
911

12+
@misc_tools.set_attributes(call_counter=0)
1013
def my_function():
1114
my_function.call_counter += 1
12-
my_function.call_counter = 0
1315

1416
emitter_1.add_output(my_function)
1517

source_py3/test_python_toolbox/test_sleek_reffing/shared.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import weakref
77

8+
from python_toolbox import misc_tools
9+
10+
811
def _is_weakreffable(thing):
912
'''Return whether a weakref can be created to `thing`.'''
1013
try:
@@ -22,10 +25,10 @@ def s():
2225
pass
2326

2427

28+
@misc_tools.set_attributes(count=0)
2529
def counter(*args, **kwargs):
2630
'''Function that returns a higher number every time it's called.'''
27-
if not hasattr(counter, 'count'):
28-
counter.count = 0
29-
result = counter.count
30-
counter.count += 1
31-
return result
31+
try:
32+
return counter.count
33+
finally:
34+
counter.count += 1

source_py3/test_python_toolbox/test_temp_value_setting/test_temp_value_setter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
'''Testing module for `python_toolbox.temp_value_setting.TempValueSetter`.'''
55

6+
from python_toolbox import misc_tools
67
from python_toolbox import cute_testing
78

89
from python_toolbox.temp_value_setting import TempValueSetter
@@ -72,8 +73,9 @@ def test_dict_key():
7273

7374
def test_as_decorator():
7475
'''Test `TempValueSetter` used as a decorator.'''
76+
77+
@misc_tools.set_attributes(x=1)
7578
def a(): pass
76-
a.x = 1
7779

7880
@TempValueSetter((a, 'x'), 2)
7981
def f():

0 commit comments

Comments
 (0)