Skip to content

Commit 6ebc0ac

Browse files
committed
-
1 parent cfc2ce7 commit 6ebc0ac

File tree

8 files changed

+35
-43
lines changed

8 files changed

+35
-43
lines changed

source_py2/python_toolbox/cute_profile/profile_handling.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
class BaseProfileHandler(object):
2020
'''Profile handler which saves the profiling result in some way.'''
21+
2122
__metaclass__ = abc.ABCMeta
2223

2324
def __call__(self, profile):
@@ -41,7 +42,7 @@ class AuxiliaryThreadProfileHandler(BaseProfileHandler):
4142
def handle(self):
4243
self.thread = threading.Thread(target=self.thread_job)
4344
self.thread.start()
44-
45+
4546
@abc.abstractmethod
4647
def thread_job(self):
4748
pass
@@ -104,7 +105,7 @@ def handle(self):
104105
def get_profile_handler(profile_handler_string):
105106
'''Parse `profile_handler_string` into a `ProfileHandler` class.'''
106107
if not profile_handler_string or profile_handler_string in \
107-
map(str, range(-1, 5)):
108+
['0', '1', '2', '3', '4']:
108109
try:
109110
sort_order = int(profile_handler_string)
110111
except (ValueError, TypeError):

source_py2/python_toolbox/emitting/emitter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# redundant calls to shared callable outputs.
1515

1616
import itertools
17+
import collections
18+
1719
from python_toolbox import cute_iter_tools
1820
from python_toolbox import misc_tools
1921
from python_toolbox import address_tools
@@ -211,15 +213,15 @@ def add_output(self, thing):
211213
If adding an emitter, every time this emitter will emit the output
212214
emitter will emit as well.
213215
'''
214-
assert isinstance(thing, Emitter) or callable(thing)
216+
assert isinstance(thing, (Emitter, collections.Callable))
215217
self._outputs.add(thing)
216218
if isinstance(thing, Emitter):
217219
thing._inputs.add(self)
218220
self._recalculate_total_callable_outputs_recursively()
219221

220222
def remove_output(self, thing):
221223
'''Remove an output from this emitter.'''
222-
assert isinstance(thing, Emitter) or callable(thing)
224+
assert isinstance(thing, (Emitter, collections.Callable))
223225
self._outputs.remove(thing)
224226
if isinstance(thing, Emitter):
225227
thing._inputs.remove(self)
@@ -234,9 +236,7 @@ def disconnect_from_all(self): # todo: use the freeze here
234236

235237
def _get_callable_outputs(self):
236238
'''Get the direct callable outputs of this emitter.'''
237-
return set((
238-
output for output in self._outputs if callable(output)
239-
))
239+
return set(filter(callable, self._outputs))
240240

241241
def _get_emitter_outputs(self):
242242
'''Get the direct emitter outputs of this emitter.'''

source_py2/python_toolbox/emitting/emitter_system/emitter_system.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,6 @@
1717
from .emitter import Emitter
1818

1919

20-
21-
#'''
22-
#Context manager for freezing the cache rebuilding in an emitter system.
23-
24-
#When you do actions using this context manager, the emitters will not
25-
#rebuild their cache when changing their inputs/outputs. When the outermost
26-
#context manager has exited, all the caches for these emitters will get
27-
#rebuilt.
28-
#'''
29-
30-
3120
class EmitterSystem(object):
3221
'''
3322
A system of emitters, representing a set of possible events in a program.
@@ -63,6 +52,14 @@ def __init__(self):
6352

6453

6554
cache_rebuilding_freezer = freezing.FreezerProperty()
55+
'''
56+
Context manager for freezing the cache rebuilding in an emitter system.
57+
58+
When you do actions using this context manager, the emitters will not
59+
rebuild their cache when changing their inputs/outputs. When the outermost
60+
context manager has exited, all the caches for these emitters will get
61+
rebuilt.
62+
'''
6663

6764

6865
@cache_rebuilding_freezer.on_thaw

source_py3/python_toolbox/context_management/context_manager.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
See its documentation for more information.
88
'''
99

10-
1110
import sys
1211
import types
1312
import abc

source_py3/python_toolbox/context_management/functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ def nested(*managers):
3737
# Don't rely on sys.exc_info() still containing
3838
# the right information. Another exception may
3939
# have been raised and caught by an exit method
40-
raise exc[0](exc[1]).with_traceback(exc[2])
40+
raise exc[1].with_traceback(exc[2])
4141

source_py3/python_toolbox/cute_profile/profile_handling.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class AuxiliaryThreadProfileHandler(BaseProfileHandler):
4040
def handle(self):
4141
self.thread = threading.Thread(target=self.thread_job)
4242
self.thread.start()
43-
43+
4444
@abc.abstractmethod
4545
def thread_job(self):
4646
pass
@@ -103,7 +103,7 @@ def handle(self):
103103
def get_profile_handler(profile_handler_string):
104104
'''Parse `profile_handler_string` into a `ProfileHandler` class.'''
105105
if not profile_handler_string or profile_handler_string in \
106-
list(map(str, list(range(-1, 5)))):
106+
['0', '1', '2', '3', '4']:
107107
try:
108108
sort_order = int(profile_handler_string)
109109
except (ValueError, TypeError):

source_py3/python_toolbox/emitting/emitter.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
# redundant calls to shared callable outputs.
1515

1616
import itertools
17+
import collections
18+
import functools
19+
1720
from python_toolbox import cute_iter_tools
1821
from python_toolbox import misc_tools
1922
from python_toolbox import address_tools
20-
import collections
21-
from functools import reduce
2223

2324

2425
class Emitter(object):
@@ -119,7 +120,7 @@ def _get_input_layers(self):
119120
current_layer = self._inputs
120121
while current_layer:
121122

122-
next_layer = reduce(
123+
next_layer = functools.reduce(
123124
set.union,
124125
(input._inputs for input in current_layer),
125126
set()
@@ -174,7 +175,7 @@ def _recalculate_total_callable_outputs(self):
174175
175176
This will to do the recalculation for this emitter and all its inputs.
176177
'''
177-
children_callable_outputs = reduce(
178+
children_callable_outputs = functools.reduce(
178179
set.union,
179180
(emitter.get_total_callable_outputs() for emitter
180181
in self._get_emitter_outputs() if emitter is not self),
@@ -213,15 +214,15 @@ def add_output(self, thing):
213214
If adding an emitter, every time this emitter will emit the output
214215
emitter will emit as well.
215216
'''
216-
assert isinstance(thing, Emitter) or isinstance(thing, collections.Callable)
217+
assert isinstance(thing, (Emitter, collections.Callable))
217218
self._outputs.add(thing)
218219
if isinstance(thing, Emitter):
219220
thing._inputs.add(self)
220221
self._recalculate_total_callable_outputs_recursively()
221222

222223
def remove_output(self, thing):
223224
'''Remove an output from this emitter.'''
224-
assert isinstance(thing, Emitter) or isinstance(thing, collections.Callable)
225+
assert isinstance(thing, (Emitter, collections.Callable))
225226
self._outputs.remove(thing)
226227
if isinstance(thing, Emitter):
227228
thing._inputs.remove(self)
@@ -236,9 +237,7 @@ def disconnect_from_all(self): # todo: use the freeze here
236237

237238
def _get_callable_outputs(self):
238239
'''Get the direct callable outputs of this emitter.'''
239-
return set((
240-
output for output in self._outputs if isinstance(output, collections.Callable)
241-
))
240+
return set(filter(callable, self._outputs))
242241

243242
def _get_emitter_outputs(self):
244243
'''Get the direct emitter outputs of this emitter.'''

source_py3/python_toolbox/emitting/emitter_system/emitter_system.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
'''
99

1010

11-
1211
import itertools
1312

1413
from python_toolbox import freezing
@@ -18,17 +17,6 @@
1817
from .emitter import Emitter
1918

2019

21-
22-
#'''
23-
#Context manager for freezing the cache rebuilding in an emitter system.
24-
25-
#When you do actions using this context manager, the emitters will not
26-
#rebuild their cache when changing their inputs/outputs. When the outermost
27-
#context manager has exited, all the caches for these emitters will get
28-
#rebuilt.
29-
#'''
30-
31-
3220
class EmitterSystem(object):
3321
'''
3422
A system of emitters, representing a set of possible events in a program.
@@ -64,6 +52,14 @@ def __init__(self):
6452

6553

6654
cache_rebuilding_freezer = freezing.FreezerProperty()
55+
'''
56+
Context manager for freezing the cache rebuilding in an emitter system.
57+
58+
When you do actions using this context manager, the emitters will not
59+
rebuild their cache when changing their inputs/outputs. When the outermost
60+
context manager has exited, all the caches for these emitters will get
61+
rebuilt.
62+
'''
6763

6864

6965
@cache_rebuilding_freezer.on_thaw

0 commit comments

Comments
 (0)