Skip to content

Commit a879ce2

Browse files
committedDec 16, 2018
release/0.7.0: Refactor ArgumentParser args and groups
1 parent f37cd39 commit a879ce2

File tree

4 files changed

+67
-73
lines changed

4 files changed

+67
-73
lines changed
 

‎readme.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,19 @@ You can run and test all notebooks by starting a Jupyter notebook server locally
221221

222222
```bash
223223
$ make open.examples
224-
$ make stop.examples
224+
$ make stop.examples
225225
```
226226

227227
## Command-line interface
228228

229229
In general you can use the porter on the command line:
230230

231231
```
232-
$ porter --input <PICKLE_FILE> [--output <DEST_DIR>]
233-
[--class_name <CLASS_NAME>] [--method_name <METHOD_NAME>]
232+
$ porter <PICKLE_FILE> [--to <directory>]
233+
[--class_name <class_name>] [--method_name <method_name>]
234234
[--export] [--checksum] [--data] [--pipe]
235235
[--c] [--java] [--js] [--go] [--php] [--ruby]
236-
[--help] [--version]
236+
[--version] [--help]
237237
```
238238

239239
The following example shows how you can save a trained estimator to the [pickle format](http://scikit-learn.org/stable/modules/model_persistence.html#persistence-example):
@@ -248,29 +248,29 @@ joblib.dump(clf, 'estimator.pkl', compress=0)
248248
After that the estimator can be transpiled to JavaScript by using the following command:
249249

250250
```bash
251-
$ porter -i estimator.pkl --js
251+
$ porter estimator.pkl --js
252252
```
253253

254254
The target programming language is changeable on the fly:
255255

256256
```bash
257-
$ porter -i estimator.pkl --c
258-
$ porter -i estimator.pkl --java
259-
$ porter -i estimator.pkl --php
260-
$ porter -i estimator.pkl --java
261-
$ porter -i estimator.pkl --ruby
257+
$ porter estimator.pkl --c
258+
$ porter estimator.pkl --java
259+
$ porter estimator.pkl --php
260+
$ porter estimator.pkl --java
261+
$ porter estimator.pkl --ruby
262262
```
263263

264264
For further processing the argument `--pipe` can be used to pass the result:
265265

266266
```bash
267-
$ porter -i estimator.pkl --js --pipe > estimator.js
267+
$ porter estimator.pkl --js --pipe > estimator.js
268268
```
269269

270270
For instance the result can be minified by using [UglifyJS](https://github.com/mishoo/UglifyJS2):
271271

272272
```bash
273-
$ porter -i estimator.pkl --js --pipe | uglifyjs --compress -o estimator.min.js
273+
$ porter estimator.pkl --js --pipe | uglifyjs --compress -o estimator.min.js
274274
```
275275

276276

@@ -336,7 +336,7 @@ Independently, the following compilers and intepreters are required to cover all
336336
The tests cover module functions as well as matching predictions of transpiled estimators. Start all tests with:
337337

338338
```bash
339-
$ make test
339+
$ make test
340340
```
341341

342342
The test files have a specific pattern: `'[Algorithm][Language]Test.py'`:

‎sklearn_porter/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def _load_meta(path):
3333
readme_path = join(src_dir, readme_path)
3434
readme = open(readme_path, 'r').read().strip()
3535
meta['long_description'] = readme
36+
3637
return meta
3738

3839

‎sklearn_porter/cli/__main__.py

+52-59
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# -*- coding: utf-8 -*-
22

33
import sys
4-
import argparse
4+
5+
from argparse import ArgumentParser
6+
from argparse import RawTextHelpFormatter
7+
from argparse import SUPPRESS
8+
from argparse import _HelpAction
59

610
from os import sep
711
from os.path import isdir
@@ -26,81 +30,70 @@ def parse_args(args):
2630
summary = dict(
2731
usage=header,
2832
description=meta.get('description'),
29-
epilog='More details on ' + meta.get('url')
33+
epilog='More details on ' + meta.get('url'),
34+
formatter_class=RawTextHelpFormatter,
35+
add_help=False
3036
)
31-
32-
parser = argparse.ArgumentParser(**summary)
37+
p = ArgumentParser(**summary)
3338

3439
# Remove the default arguments group:
35-
parser._action_groups.pop()
36-
37-
# Required arguments:
38-
required = parser.add_argument_group('Required arguments')
39-
required.add_argument('--input', '-i',
40-
required=True,
41-
help=('Path to an exported estimator in pickle '
42-
'(.pkl) format.'))
40+
p._action_groups.pop()
41+
42+
# File arguments:
43+
files = p.add_argument_group('File arguments')
44+
help = 'Path to an exported estimator in pickle (.pkl) format.'
45+
files.add_argument('input', help=help)
46+
help = 'Path to the output directory where ' \
47+
'the transpiled estimator will be stored.'
48+
files.add_argument('--to', required=False, help=help)
49+
50+
# Template arguments:
51+
templates = p.add_argument_group('Template arguments')
52+
templates.add_argument('--class_name', default=None, required=False,
53+
help='Define a custom class name.')
54+
templates.add_argument('--method_name', default='predict', required=False,
55+
help='Define a custom method name.')
4356

4457
# Optional arguments:
45-
optional = parser.add_argument_group('Optional arguments')
46-
optional.add_argument('--output', '-o',
47-
required=False,
48-
help=('Path to the destination directory where the '
49-
'transpiled estimator will be stored.'))
50-
optional.add_argument('--class_name',
51-
default=None,
52-
required=False,
53-
help='Define the class name in the final output.')
54-
optional.add_argument('--method_name',
55-
default='predict',
56-
required=False,
57-
help='Define the method name in the final output.')
58-
optional.add_argument('--export', '-e',
59-
required=False,
60-
default=False,
61-
action='store_true',
62-
help='Whether to export the model data or not.')
63-
optional.add_argument('--checksum', '-s',
64-
required=False,
65-
default=False,
66-
action='store_true',
67-
help='Whether to append the checksum to the '
68-
'filename or not.')
69-
optional.add_argument('--data', '-d',
70-
required=False,
71-
default=False,
72-
action='store_true',
73-
help='Whether to export just the model data or all.')
74-
optional.add_argument('--pipe', '-p',
75-
required=False,
76-
default=False,
77-
action='store_true',
78-
help='Print the transpiled estimator to the console.')
79-
80-
# Languages:
81-
langs = parser.add_argument_group('Programming languages')
58+
optional = p.add_argument_group('Optional arguments')
59+
optional.add_argument('--export', '-e', required=False, default=False,
60+
action='store_true', help='Whether to export '
61+
'the model data or not.')
62+
optional.add_argument('--checksum', '-s', required=False, default=False,
63+
action='store_true', help='Whether to append the '
64+
'checksum to the filename '
65+
'or not.')
66+
optional.add_argument('--data', '-d', required=False, default=False,
67+
action='store_true', help='Whether to export just '
68+
'the model data or all.')
69+
optional.add_argument('--pipe', '-p', required=False, default=False,
70+
action='store_true', help='Print the transpiled '
71+
'estimator to the console.')
72+
73+
# Programming languages:
74+
langs = p.add_argument_group('Programming languages')
8275
languages = {key: clazz.LABEL for key, clazz in list(LANGUAGES.items())}
83-
langs.add_argument('--language', '-l',
84-
choices=languages.keys(),
85-
default='java',
86-
required=False,
87-
help=argparse.SUPPRESS)
76+
langs.add_argument('--language', '-l', choices=languages.keys(),
77+
default='java', required=False, help=SUPPRESS)
8878
for key, label in list(languages.items()):
8979
help = 'Set \'{}\' as the target programming language.'.format(label)
9080
langs.add_argument('--{}'.format(key), action='store_true', help=help)
9181

9282
# Extra arguments:
93-
extras = parser.add_argument_group('Extra arguments')
83+
extras = p.add_argument_group('Extra arguments')
9484
extras.add_argument('--version', '-v', action='version',
95-
version='sklearn-porter v{}'.format(version))
85+
version='sklearn-porter v{}'.format(version),
86+
help='Show the version number and exit.')
87+
extras.add_argument('--help', '-h', action=_HelpAction,
88+
help="Show this help message and exit.")
9689

9790
# Show help by default:
9891
if len(sys.argv) == 1:
99-
parser.print_help(sys.stderr)
92+
p.print_help(sys.stderr)
10093
sys.exit(1)
10194

10295
# Return dictionary:
103-
args = vars(parser.parse_args(args))
96+
args = vars(p.parse_args(args))
10497
return args
10598

10699

@@ -126,7 +119,7 @@ def main():
126119
break
127120

128121
# Define destination path:
129-
dest_dir = str(args.get('output'))
122+
dest_dir = str(args.get('to'))
130123
if dest_dir == '' or not isdir(dest_dir):
131124
dest_dir = pkl_file_path.split(sep)
132125
del dest_dir[-1]

‎tests/PorterTest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_python_command_execution(self):
6767
joblib.dump(self.estimator, pkl_path)
6868

6969
# Port estimator:
70-
cmd = 'python -m sklearn_porter.cli.__main__ -i {}' \
70+
cmd = 'python -m sklearn_porter.cli.__main__ {}' \
7171
' --class_name Brain'.format(pkl_path)
7272
Shell.call(cmd)
7373
# Compare file contents:

0 commit comments

Comments
 (0)
Please sign in to comment.