Skip to content

Commit 0e9619a

Browse files
authored
Merge pull request #102 from RamanjaneyuluIdavalapati/master
status codes and README for sub commands #101, #82
2 parents 891a3a7 + f8372d5 commit 0e9619a

4 files changed

Lines changed: 46 additions & 7 deletions

File tree

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ deploy:
1919
- basescript/utils.py
2020
- examples/adder.py
2121
- examples/helloworld.py
22-
name: basescript-0.3.4
23-
tag_name: 0.3.4
22+
name: basescript-0.3.5
23+
tag_name: 0.3.5
2424
true:
2525
repo: deep-compute/basescript
2626
- provider: pypi

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,46 @@ https://docs.python.org/2/library/logging.html#logging-levels.
148148
`log` is a log object created using python's standard `logging` module. You can
149149
read more about it at https://docs.python.org/2/library/logging.html.
150150
151+
#### Sub commands
152+
When we have multiple functionalities then there is a way to call or execute each functionality with different sub command.
153+
154+
For example if we have add and subtract in the same script then we can call each functionality with different sub command.
155+
156+
calc.py
157+
```python
158+
from basescript import BaseScript
159+
160+
class Calc(BaseScript):
161+
A = 10
162+
163+
def add(self):
164+
print(self.A + self.args.b)
165+
166+
def sub(self):
167+
print(self.A - self.args.b)
168+
169+
def define_subcommands(self, subcommands):
170+
super(Calc, self).define_subcommands(subcommands)
171+
172+
add_cmd = subcommands.add_parser("add", help="Adds number")
173+
add_cmd.set_defaults(func=self.add)
174+
add_cmd.add_argument('--b', type=int, help="Number to add")
175+
176+
sub_cmd = subcommands.add_parser("sub", help="Subtracts number")
177+
sub_cmd.set_defaults(func=self.sub)
178+
sub_cmd.add_argument('--b', type=int, help="Number to subtract")
179+
180+
if __name__ == '__main__':
181+
Calc().start()
182+
```
183+
Run
184+
```bash
185+
$ python3 calc.py add --b 4
186+
14
187+
$ python3 calc.py sub --b 4
188+
6
189+
```
190+
151191
### Metric-Grouping
152192
When writing a `Metric` using `self.log`, you can specify `type=metric`. If this is done, a background thread will automatically group multiple metrics into one by averaging values (to prevent writing too many log lines).
153193
test.py

basescript/basescript.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ def start(self):
7777
raise
7878
except KeyboardInterrupt:
7979
self.log.warning("exited via keyboard interrupt")
80-
except:
81-
self.log.exception("exited start function")
82-
# set exit code so we know it did not end successfully
83-
# TODO different exit codes based on signals ?
80+
except Exception as e:
81+
self.log.error("exited start function")
82+
raise e
8483
finally:
8584
self._flush_metrics_q.put(None, block=True)
8685
self._flush_metrics_q.put(None, block=True, timeout=1)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_long_description():
2525

2626
long_description = get_long_description()
2727

28-
version = "0.3.4"
28+
version = "0.3.5"
2929
setup(
3030
name="basescript",
3131
version=version,

0 commit comments

Comments
 (0)