functionalthreading 0.2.7__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: functionalthreading
3
- Version: 0.2.7
3
+ Version: 0.4.0
4
4
  Summary: Functional programming with thread-based parallelism
5
5
  Author-email: Richard Tong <richytong@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/richytong/functionalthreading
@@ -1,6 +1,7 @@
1
1
  from functools import partial, Placeholder as _
2
2
  from functionalthreading.functions import chain, tap, tmap
3
+ from functionalthreading.classes import Thread
3
4
 
4
5
  __name__ = 'functionalthreading'
5
6
 
6
- __all__ = ['partial', '_' ,'chain', 'tap', 'tmap']
7
+ __all__ = ['Thread', 'partial', '_' ,'chain', 'tap', 'tmap']
@@ -0,0 +1,15 @@
1
+ from threading import Thread as _Thread
2
+
3
+ class Thread(_Thread):
4
+ def __init__(self, *args, **kwargs):
5
+ _Thread.__init__(self, *args, **kwargs)
6
+ self.value = None
7
+ self.target = kwargs['target']
8
+ self.args = kwargs['args']
9
+
10
+ def run(self):
11
+ self.value = self.target(*self.args)
12
+
13
+ __name__ = 'classes'
14
+
15
+ __all__ = ['Thread']
@@ -1,5 +1,5 @@
1
- from threading import Thread
2
- import itertools
1
+ from functools import partial, Placeholder as _
2
+ from functionalthreading.classes import Thread
3
3
 
4
4
  def _chain(argument, funcs):
5
5
  ret = argument
@@ -37,10 +37,7 @@ def chain(argument, *funcs):
37
37
 
38
38
  """
39
39
  if callable(argument):
40
- inner_funcs = (argument,) + funcs
41
- def inner_func(inner_argument):
42
- return _chain(inner_argument, inner_funcs)
43
- return inner_func
40
+ return partial(_chain, _, (argument,) + funcs)
44
41
  return _chain(argument, funcs)
45
42
 
46
43
  def tap(func):
@@ -64,32 +61,13 @@ def tap(func):
64
61
  return argument
65
62
  return inner_func
66
63
 
67
- class _Thread(Thread):
68
- def __init__(self, *args, **kwargs):
69
- Thread.__init__(self, *args, **kwargs)
70
- self.value = None
71
- self.target = kwargs['target']
72
- self.args = kwargs['args']
73
-
74
- def run(self):
75
- self.value = self.target(*self.args)
76
-
77
- def tmap(array_or_tuple, func):
78
- """Map a function concurrently across each element of an array or tuple.
79
-
80
- Each function invokation happens in a separate thread.
81
-
82
- ```python
83
- squared = tmap([1, 2, 3], lambda n: n ** 2)
84
- ```
85
-
86
- """
64
+ def _tmap(array_or_tuple, func):
87
65
  length = len(array_or_tuple)
88
66
  threads = []
89
67
  index = 0
90
68
  args = [array_or_tuple]
91
69
  while index < length:
92
- t = _Thread(target=func, args=[array_or_tuple[index]])
70
+ t = Thread(target=func, args=[array_or_tuple[index]])
93
71
  t.start()
94
72
  threads.append(t)
95
73
  index += 1
@@ -104,6 +82,27 @@ def tmap(array_or_tuple, func):
104
82
  return tuple(result)
105
83
  return result
106
84
 
85
+ def tmap(*args):
86
+ """Map a function concurrently across each element of an array or tuple.
87
+
88
+ Each function invokation happens in a separate thread.
89
+
90
+ ```python
91
+ squared = tmap([1, 2, 3], lambda n: n ** 2)
92
+ ```
93
+
94
+ If the array or tuple argument is omitted, returns a function of the mapping function that expects the non-function argument.
95
+
96
+ ```python
97
+ my_mapping_func = tmap(lambda n: n ** 2)
98
+ squared = my_mapping_func([1, 2, 3])
99
+ ```
100
+
101
+ """
102
+ if callable(args[0]):
103
+ return partial(_tmap, _, args[0])
104
+ return _tmap(*args)
105
+
107
106
  __name__ = 'functions'
108
107
 
109
108
  __all__ = ['chain', 'tap', 'tmap']
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: functionalthreading
3
- Version: 0.2.7
3
+ Version: 0.4.0
4
4
  Summary: Functional programming with thread-based parallelism
5
5
  Author-email: Richard Tong <richytong@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/richytong/functionalthreading
@@ -7,4 +7,5 @@ functionalthreading.egg-info/PKG-INFO
7
7
  functionalthreading.egg-info/SOURCES.txt
8
8
  functionalthreading.egg-info/dependency_links.txt
9
9
  functionalthreading.egg-info/top_level.txt
10
+ functionalthreading/classes/__init__.py
10
11
  functionalthreading/functions/__init__.py
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "functionalthreading"
3
- version = "0.2.7"
3
+ version = "0.4.0"
4
4
  authors = [
5
5
  { name="Richard Tong", email="richytong@gmail.com" },
6
6
  ]
@@ -8,5 +8,9 @@ with open('pyproject.toml', 'rb') as file:
8
8
  name='functionalthreading',
9
9
  version=pyproject['project']['version'],
10
10
  description='Functional programming with thread-based parallelism',
11
- packages=['functionalthreading', 'functionalthreading.functions'],
11
+ packages=[
12
+ 'functionalthreading',
13
+ 'functionalthreading.classes',
14
+ 'functionalthreading.functions',
15
+ ],
12
16
  )