Skip to content

Commit

Permalink
Merge pull request #26 from supriyopaul/master
Browse files Browse the repository at this point in the history
Included serialize_dict_keys, flatten_dict and more comments
  • Loading branch information
supriyopaul authored Nov 2, 2018
2 parents ba71c5a + db4401b commit e6c3425
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 134 deletions.
13 changes: 9 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
language: python
python:
- '3.5'
#python:
#- '3.7.0'
matrix:
include:
- python: 3.7
dist: xenial
sudo: true
install:
- pip3 install .
script:
Expand All @@ -10,8 +15,8 @@ deploy:
skip_cleanup: true
api-key:
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=
name: deeputil-0.2.5
tag_name: 0.2.5
name: deeputil-0.2.6
tag_name: 0.2.6
on:
branch: master
repo: deep-compute/deeputil
Expand Down
68 changes: 36 additions & 32 deletions deeputil/keep_running.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
'''Keeps running a function running even on error
'''

import time
import inspect

class KeepRunningTerminate(Exception): pass
class KeepRunningTerminate(Exception):
pass

def keeprunning(wait_secs=0, exit_on_success=False,
on_success=None, on_error=None, on_done=None):
on_success=None, on_error=None, on_done=None):
'''
Keeps running a function running even on error.
# Example 1: dosomething needs to run until completion condition
# without needing to have a loop in its code. Also, when error
# happens, we should NOT terminate execution
Example 1: dosomething needs to run until completion condition
without needing to have a loop in its code. Also, when error
happens, we should NOT terminate execution
>>> from deeputil import AttrDict
>>> @keeprunning(wait_secs=1)
... def dosomething(state):
Expand All @@ -23,7 +25,8 @@ def keeprunning(wait_secs=0, exit_on_success=False,
... if state.i >= 7:
... print ("Done")
... raise keeprunning.terminate
...
...
>>> state = AttrDict(i=0)
>>> dosomething(state)
AttrDict({'i': 1})
Expand All @@ -37,15 +40,14 @@ def keeprunning(wait_secs=0, exit_on_success=False,
Error happened
AttrDict({'i': 7})
Done
# Example 2: In case you want to log exceptions while
# dosomething keeps running, or perform any other action
# when an exceptions arise
Example 2: In case you want to log exceptions while
dosomething keeps running, or perform any other action
when an exceptions arise
>>> def some_error(__exc__):
... print (__exc__)
...
...
>>> @keeprunning(on_error=some_error)
... def dosomething(state):
... state.i += 1
Expand All @@ -56,8 +58,8 @@ def keeprunning(wait_secs=0, exit_on_success=False,
... if state.i >= 7:
... print ("Done")
... raise keeprunning.terminate
...
...
>>> state = AttrDict(i=0)
>>> dosomething(state)
AttrDict({'i': 1})
Expand All @@ -74,26 +76,26 @@ def keeprunning(wait_secs=0, exit_on_success=False,
division by zero
AttrDict({'i': 7})
Done
# Example 3: Full set of arguments that can be passed in @keeprunning()
# with class implementations
Example 3: Full set of arguments that can be passed in @keeprunning()
with class implementations
>>> # Class that has some class variables
... class Demo(object):
... SUCCESS_MSG = 'Yay!!'
... DONE_MSG = 'STOPPED AT NOTHING!'
... ERROR_MSG = 'Error'
...
...
... # Functions to be called by @keeprunning
... def success(self):
... print((self.SUCCESS_MSG))
...
...
... def failure(self, __exc__):
... print((self.ERROR_MSG, __exc__))
...
...
... def task_done(self):
... print((self.DONE_MSG))
...
...
... #Actual use of keeprunning with all arguments passed
... @keeprunning(wait_secs=1, exit_on_success=False,
... on_success=success, on_error=failure, on_done=task_done)
Expand All @@ -102,29 +104,31 @@ def keeprunning(wait_secs=0, exit_on_success=False,
... print (state)
... if state.i % 2 == 0:
... print("Error happened")
... 1 / 0 # create an error condition
... # create an error condition
... 1 / 0
... if state.i >= 7:
... print ("Done")
... raise keeprunning.terminate
...
...
>>> demo = Demo()
>>> state = AttrDict(i=0)
>>> demo.dosomething(state)
AttrDict({'i': 1})
Yay!!
AttrDict({'i': 2})
Error happened
('Error', ZeroDivisionError('division by zero',))
('Error', ZeroDivisionError('division by zero'))
AttrDict({'i': 3})
Yay!!
AttrDict({'i': 4})
Error happened
('Error', ZeroDivisionError('division by zero',))
('Error', ZeroDivisionError('division by zero'))
AttrDict({'i': 5})
Yay!!
AttrDict({'i': 6})
Error happened
('Error', ZeroDivisionError('division by zero',))
('Error', ZeroDivisionError('division by zero'))
AttrDict({'i': 7})
Done
STOPPED AT NOTHING!
Expand Down Expand Up @@ -159,7 +163,7 @@ def _fn(*args, **kwargs):
continue

_call_callback(on_success, fargs)

_call_callback(on_done, fargs)

return _fn
Expand Down
Loading

0 comments on commit e6c3425

Please sign in to comment.