Skip to content

Commit 5cd4800

Browse files
committed
master: add readme
1 parent c4b5a33 commit 5cd4800

File tree

4 files changed

+99
-15
lines changed

4 files changed

+99
-15
lines changed

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,87 @@
11
# Importify
22
Import and export your configuration like a boss !!
33

4+
## Installation
5+
Install using pypi distribution using following command.
6+
7+
```
8+
pip install importify
9+
```
10+
11+
## Quick Start
12+
Following is a recommended usage.
13+
14+
1. Define configuration class.
15+
16+
- Must inherit Serializable
17+
- Must call super constructor
18+
- Serializable can consist json serializable types and nested Serializable object.
19+
20+
example:
21+
22+
```python
23+
from importify import Serializable
24+
25+
26+
class Config(Serializable):
27+
def __init__(self):
28+
super(Config, self).__init__()
29+
self.use_gpu = True
30+
self.batch_size = 32
31+
self.model_config = ModelConfig()
32+
33+
class ModelConfig(Serializable):
34+
def __init__(self):
35+
super(ModelConfig, self).__init__()
36+
self.hidden_dim = 256
37+
self.output_dim = 128
38+
```
39+
40+
2. Parse arguments
41+
42+
- Arguments are named the same as object variables.
43+
- Nested variables are divided with dot symbol.
44+
45+
(CODE)
46+
47+
```python
48+
# Initialize Serializable inherited configuration class
49+
config = Config()
50+
51+
# Parse
52+
config.parse()
53+
```
54+
55+
2-1. Parse command line arguments.
56+
57+
(BASH)
58+
59+
```
60+
python -m your_module --use_gpu False
61+
```
62+
63+
2-2. Import from pre-exported configuration json file. Use ```--load_json``` option.
64+
65+
(BASH)
66+
67+
```
68+
python -m your_module --load_json PATH_TO_JSON_FILE
69+
```
70+
71+
3. Export current settings to json.
72+
73+
example:
74+
75+
```python
76+
config = Config()
77+
78+
saved_status = config.export_json(path=PATH_TO_JSON_FILE)
79+
```
80+
481
## Usage
82+
Please refer to [https://github.com/litcoderr/importify/tree/master/examples](examples) code.
83+
84+
## Contributions
85+
Please commit any issues or pull requests. Every bit of contributions are welcomed.
86+
If this ever goes viral, I'll make a legitimate template for issues and PRs. Thanks.
87+

examples/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# temporary for test use
2-
import sys
3-
sys.path.insert(0, "/Users/litcoderr/project/importify")
4-
#####
51
"""
62
This demonstrates how you can construct a serializable configuration object
73
"""

examples/exported.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"is_awesome": true, "nested": {"is_legit": true, "double_nested": {"is_intuitive": true}}}
1+
{"is_awesome": true, "nested": {"is_legit": false, "double_nested": {"is_intuitive": true}}}

importify/interface.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ def strip(self, prefix: str = "") -> Dict[str, Any]:
105105
""" Strip arguments.
106106
107107
Args:
108-
instance: Parent Serializable instance
109108
prefix: Prefix string.
110109
111110
Returns:
@@ -172,6 +171,7 @@ def parse(self):
172171
# Parse
173172
boolean_keys = []
174173
parser = argparse.ArgumentParser()
174+
parser.add_argument('--load_json', type=str, default=None)
175175
for key, value in dicts.items():
176176
if type(value) == bool:
177177
boolean_keys.append(key)
@@ -185,13 +185,18 @@ def parse(self):
185185
parser.add_argument('--{key}'.format(key=key), type=type(value), default=value)
186186
args = vars(parser.parse_args())
187187

188-
# Deal with stringed boolean attributes
189-
for key in boolean_keys:
190-
if args[key] == "True":
191-
args[key] = True
192-
else:
193-
args[key] = False
188+
if args['load_json'] is None:
189+
# Deal with stringed boolean attributes
190+
for key in boolean_keys:
191+
if args[key] == "True":
192+
args[key] = True
193+
else:
194+
args[key] = False
194195

195-
# Unstrip and Update
196-
for stripped_key, res in self.instance.unstrip(args).items():
197-
setattr(res["reference"], res["key"], res["value"])
196+
# Unstrip and Update
197+
for stripped_key, res in self.instance.unstrip(args).items():
198+
setattr(res["reference"], res["key"], res["value"])
199+
else:
200+
with open(args['load_json'], 'r') as file:
201+
data = json.load(file)
202+
self.instance.import_dict(data)

0 commit comments

Comments
 (0)