Skip to content

Commit c35003a

Browse files
committed
-
1 parent 30ec7ca commit c35003a

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

source_py3/python_toolbox/cute_iter_tools.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,11 @@ def _fill(iterable, fill_value, fill_value_maker, length):
332332

333333

334334
def call_until_exception(function, exception, lazy_tuple=False):
335-
'''Iterate on values returned from `function` until getting `exception`.'''
335+
'''
336+
Iterate on values returned from `function` until getting `exception`.
337+
338+
If `lazy_tuple=True`, returns a `LazyTuple` rather than an iterator.
339+
'''
336340
iterator = _call_until_exception(function, exception)
337341
if lazy_tuple:
338342
from python_toolbox import nifty_collections # Avoiding circular import.

source_py3/python_toolbox/misc_tools/misc_tools.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,18 @@ def repeat_getattr(thing, query):
298298

299299

300300
def set_attributes(**kwargs):
301+
'''
302+
Decorator to set attributes on a function.
303+
304+
Example:
305+
306+
@set_attributes(meow='frrr')
307+
def f():
308+
return 'whatever'
309+
310+
assert f.meow == 'frrr'
311+
312+
'''
301313
def decorator(function):
302314
for key, value in kwargs.items():
303315
setattr(function, key, value)
@@ -309,6 +321,17 @@ def decorator(function):
309321
pocket_container.value = None
310322
@set_attributes(container=pocket_container)
311323
def pocket(*args):
324+
'''
325+
Put something in the pocket, or get the value in the pocket.
326+
327+
Useful for things like this:
328+
329+
if pocket(expensive_computation):
330+
result = pocket()
331+
# Do something with result...
332+
333+
The contents of the pocket are thread-local.
334+
'''
312335
if args:
313336
(pocket.container.value,) = args
314337
else:

source_py3/python_toolbox/sequence_tools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ def get_recurrences(sequence):
282282
'''
283283
return {item: n_recurrences for item, n_recurrences in
284284
collections.Counter(sequence).most_common() if n_recurrences >= 2}
285+
285286

286287
### Not using now, might want in future:
287288

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2009-2014 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
5+
from python_toolbox.misc_tools import pocket
6+
7+
8+
def test():
9+
assert pocket() == None
10+
assert pocket() == None
11+
assert pocket(7) == None
12+
assert pocket() == 7
13+
assert pocket() == 7
14+
assert pocket(8) == None
15+
assert pocket() == 8
16+
assert pocket() == 8
17+
if pocket(1+1):
18+
pass
19+
assert pocket() == 2

0 commit comments

Comments
 (0)