Skip to content

Commit e6c3425

Browse files
authored
Merge pull request #26 from supriyopaul/master
Included serialize_dict_keys, flatten_dict and more comments
2 parents ba71c5a + db4401b commit e6c3425

6 files changed

Lines changed: 230 additions & 134 deletions

File tree

.travis.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
language: python
2-
python:
3-
- '3.5'
2+
#python:
3+
#- '3.7.0'
4+
matrix:
5+
include:
6+
- python: 3.7
7+
dist: xenial
8+
sudo: true
49
install:
510
- pip3 install .
611
script:
@@ -10,8 +15,8 @@ deploy:
1015
skip_cleanup: true
1116
api-key:
1217
secure: Q3wwYSZkwXAG1DwgKZrR/vZTGLZlDBfR9O5MoZ+dpmy6EmFozQLRB+qFh+eWh2Y8xYIdXz+6CaJLcM92JU5zJTslWLHyhO7kTOt31fxuZu+HGnR835Try6TlU11948nn2Ramk4nI3lT/G4jO+PdNq23sOPdhV4KDI0nv9Pc9Ywqoyg+4okpSnbJNWn7bdinthA88iMRNxqH88LJ4CM6J/eh0qJUm2xcAOTpw9gIkq188UTCbT71qGUWhWFicvbV1oJ6r+C87Ru/rf+nHJyZ7Dn2y8odBx+MHicUp7XomKP/niM2K9TkX/wOMqopE6XrmAnZ/6W/8cGOoqLWT0oqksktIqlOrUYQAq5UNXee3cHPq6k+Q/CGhbGb9feNEzb3PMPKkD6wict90arhHfpqk0yGP1lCRSwM0eIgegMWgSpFXi2Zc+K/6iucZ21ayVDZf20f7Pe70SEgjB/VJiTgI+BMmOG70a2MYsHUG+rK4fYiSDiO+9ADVNHHNy5r9dL+VLhRxkkcgaIkkZsx/xoE2KUO601EOEfjX55S0C8R/VRNDpxg1VXhu2i19E3G08Xcv+xuz8awst3gvVImVJY9j9GiimMtT0l/pLMjWTeAvMmlraxRaMa36Q96BntThdwRkNCAhsfCTF364egRI+PEWciRcrb0Tpj8/L8p2OUMMqgI=
13-
name: deeputil-0.2.5
14-
tag_name: 0.2.5
18+
name: deeputil-0.2.6
19+
tag_name: 0.2.6
1520
on:
1621
branch: master
1722
repo: deep-compute/deeputil

deeputil/keep_running.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
'''Keeps running a function running even on error
2+
'''
3+
14
import time
25
import inspect
36

4-
class KeepRunningTerminate(Exception): pass
7+
class KeepRunningTerminate(Exception):
8+
pass
59

610
def keeprunning(wait_secs=0, exit_on_success=False,
7-
on_success=None, on_error=None, on_done=None):
11+
on_success=None, on_error=None, on_done=None):
812
'''
9-
Keeps running a function running even on error.
10-
11-
# Example 1: dosomething needs to run until completion condition
12-
# without needing to have a loop in its code. Also, when error
13-
# happens, we should NOT terminate execution
14-
13+
Example 1: dosomething needs to run until completion condition
14+
without needing to have a loop in its code. Also, when error
15+
happens, we should NOT terminate execution
16+
1517
>>> from deeputil import AttrDict
1618
>>> @keeprunning(wait_secs=1)
1719
... def dosomething(state):
@@ -23,7 +25,8 @@ def keeprunning(wait_secs=0, exit_on_success=False,
2325
... if state.i >= 7:
2426
... print ("Done")
2527
... raise keeprunning.terminate
26-
...
28+
...
29+
2730
>>> state = AttrDict(i=0)
2831
>>> dosomething(state)
2932
AttrDict({'i': 1})
@@ -37,15 +40,14 @@ def keeprunning(wait_secs=0, exit_on_success=False,
3740
Error happened
3841
AttrDict({'i': 7})
3942
Done
40-
41-
42-
# Example 2: In case you want to log exceptions while
43-
# dosomething keeps running, or perform any other action
44-
# when an exceptions arise
45-
43+
44+
Example 2: In case you want to log exceptions while
45+
dosomething keeps running, or perform any other action
46+
when an exceptions arise
47+
4648
>>> def some_error(__exc__):
4749
... print (__exc__)
48-
...
50+
...
4951
>>> @keeprunning(on_error=some_error)
5052
... def dosomething(state):
5153
... state.i += 1
@@ -56,8 +58,8 @@ def keeprunning(wait_secs=0, exit_on_success=False,
5658
... if state.i >= 7:
5759
... print ("Done")
5860
... raise keeprunning.terminate
59-
...
60-
61+
...
62+
6163
>>> state = AttrDict(i=0)
6264
>>> dosomething(state)
6365
AttrDict({'i': 1})
@@ -74,26 +76,26 @@ def keeprunning(wait_secs=0, exit_on_success=False,
7476
division by zero
7577
AttrDict({'i': 7})
7678
Done
77-
78-
# Example 3: Full set of arguments that can be passed in @keeprunning()
79-
# with class implementations
80-
79+
80+
Example 3: Full set of arguments that can be passed in @keeprunning()
81+
with class implementations
82+
8183
>>> # Class that has some class variables
8284
... class Demo(object):
8385
... SUCCESS_MSG = 'Yay!!'
8486
... DONE_MSG = 'STOPPED AT NOTHING!'
8587
... ERROR_MSG = 'Error'
86-
...
88+
...
8789
... # Functions to be called by @keeprunning
8890
... def success(self):
8991
... print((self.SUCCESS_MSG))
90-
...
92+
...
9193
... def failure(self, __exc__):
9294
... print((self.ERROR_MSG, __exc__))
93-
...
95+
...
9496
... def task_done(self):
9597
... print((self.DONE_MSG))
96-
...
98+
...
9799
... #Actual use of keeprunning with all arguments passed
98100
... @keeprunning(wait_secs=1, exit_on_success=False,
99101
... on_success=success, on_error=failure, on_done=task_done)
@@ -102,29 +104,31 @@ def keeprunning(wait_secs=0, exit_on_success=False,
102104
... print (state)
103105
... if state.i % 2 == 0:
104106
... print("Error happened")
105-
... 1 / 0 # create an error condition
107+
... # create an error condition
108+
... 1 / 0
106109
... if state.i >= 7:
107110
... print ("Done")
108111
... raise keeprunning.terminate
109-
...
112+
...
113+
110114
>>> demo = Demo()
111115
>>> state = AttrDict(i=0)
112116
>>> demo.dosomething(state)
113117
AttrDict({'i': 1})
114118
Yay!!
115119
AttrDict({'i': 2})
116120
Error happened
117-
('Error', ZeroDivisionError('division by zero',))
121+
('Error', ZeroDivisionError('division by zero'))
118122
AttrDict({'i': 3})
119123
Yay!!
120124
AttrDict({'i': 4})
121125
Error happened
122-
('Error', ZeroDivisionError('division by zero',))
126+
('Error', ZeroDivisionError('division by zero'))
123127
AttrDict({'i': 5})
124128
Yay!!
125129
AttrDict({'i': 6})
126130
Error happened
127-
('Error', ZeroDivisionError('division by zero',))
131+
('Error', ZeroDivisionError('division by zero'))
128132
AttrDict({'i': 7})
129133
Done
130134
STOPPED AT NOTHING!
@@ -159,7 +163,7 @@ def _fn(*args, **kwargs):
159163
continue
160164

161165
_call_callback(on_success, fargs)
162-
166+
163167
_call_callback(on_done, fargs)
164168

165169
return _fn

0 commit comments

Comments
 (0)