Skip to content

Commit 0254b0e

Browse files
author
RamanjaneyuluIdavalapati
committed
status codes and README for sub commands
1 parent 891a3a7 commit 0254b0e

4 files changed

Lines changed: 47 additions & 12 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: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,16 @@ def start(self):
7272
# invoke the appropriate sub-command as requested from command-line
7373
try:
7474
self.args.func()
75-
except SystemExit as e:
76-
if e.code != 0:
77-
raise
75+
self.log.debug("exited_successfully")
7876
except KeyboardInterrupt:
7977
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 ?
78+
except Exception as e:
79+
self.log.error("exited start function")
80+
raise e
8481
finally:
8582
self._flush_metrics_q.put(None, block=True)
8683
self._flush_metrics_q.put(None, block=True, timeout=1)
8784

88-
self.log.debug("exited_successfully")
89-
9085
@property
9186
def name(self):
9287
return ".".join([x for x in (sys.argv[0].split(".")[0], self.args.name) if x])

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)