Skip to content

Commit 7a56a14

Browse files
committed
README and update examples
1 parent f08d378 commit 7a56a14

File tree

6 files changed

+153
-28
lines changed

6 files changed

+153
-28
lines changed

README.md

Lines changed: 127 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,112 @@ json2python-models is a [Python](https://www.python.org/) tool that can generate
2222
* Specifying when dictionaries should be processed as is
2323
* CLI tool
2424

25+
## Examples
26+
[*skip*](#installation)
27+
28+
```json
29+
[
30+
{
31+
"season": "2019",
32+
"round": "3",
33+
"DriverStandings": [
34+
{
35+
"position": "1",
36+
"positionText": "1",
37+
"points": "68",
38+
"wins": "2",
39+
"Driver": {
40+
"driverId": "hamilton",
41+
"permanentNumber": "44",
42+
"code": "HAM",
43+
"url": "http://en.wikipedia.org/wiki/Lewis_Hamilton",
44+
"givenName": "Lewis",
45+
"familyName": "Hamilton",
46+
"dateOfBirth": "1985-01-07",
47+
"nationality": "British"
48+
},
49+
"Constructors": [
50+
{
51+
"constructorId": "mercedes",
52+
"url": "http://en.wikipedia.org/wiki/Mercedes-Benz_in_Formula_One",
53+
"name": "Mercedes",
54+
"nationality": "German"
55+
}
56+
]
57+
},
58+
{
59+
"position": "2",
60+
"positionText": "2",
61+
"points": "62",
62+
"wins": "1",
63+
"Driver": {
64+
"driverId": "bottas",
65+
"permanentNumber": "77",
66+
"code": "BOT",
67+
"url": "http://en.wikipedia.org/wiki/Valtteri_Bottas",
68+
"givenName": "Valtteri",
69+
"familyName": "Bottas",
70+
"dateOfBirth": "1989-08-28",
71+
"nationality": "Finnish"
72+
},
73+
"Constructors": [
74+
{
75+
"constructorId": "mercedes",
76+
"url": "http://en.wikipedia.org/wiki/Mercedes-Benz_in_Formula_One",
77+
"name": "Mercedes",
78+
"nationality": "German"
79+
}
80+
]
81+
}
82+
]
83+
}
84+
]
85+
```
86+
87+
```python
88+
import attr
89+
from json_to_models.dynamic_typing import IntString, IsoDateString
90+
from typing import List
91+
92+
93+
@attr.s
94+
class DriverStandings:
95+
@attr.s
96+
class DriverStanding:
97+
@attr.s
98+
class Driver:
99+
driver_id: str = attr.ib()
100+
permanent_number: IntString = attr.ib(converter=IntString)
101+
code: str = attr.ib()
102+
url: str = attr.ib()
103+
given_name: str = attr.ib()
104+
family_name: str = attr.ib()
105+
date_of_birth: IsoDateString = attr.ib(converter=IsoDateString)
106+
nationality: str = attr.ib()
107+
108+
@attr.s
109+
class Constructor:
110+
constructor_id: str = attr.ib()
111+
url: str = attr.ib()
112+
name: str = attr.ib()
113+
nationality: str = attr.ib()
114+
115+
position: IntString = attr.ib(converter=IntString)
116+
position_text: IntString = attr.ib(converter=IntString)
117+
points: IntString = attr.ib(converter=IntString)
118+
wins: IntString = attr.ib(converter=IntString)
119+
driver: 'Driver' = attr.ib()
120+
constructors: List['Constructor'] = attr.ib()
121+
122+
season: IntString = attr.ib(converter=IntString)
123+
round: IntString = attr.ib(converter=IntString)
124+
driver_standings: List['DriverStanding'] = attr.ib()
125+
```
126+
25127
## Installation
26128

27-
| **Be ware**: it supports only `python3.7` and higher. |
28-
| --- |
129+
| **Be ware**: this project supports only `python3.7` and higher. |
130+
| --- |
29131

30132
To install Requests, use `pip`:
31133

@@ -39,6 +141,15 @@ cd json2python-models
39141
python setup.py install
40142
```
41143

144+
## Usage
145+
146+
### CLI
147+
148+
> Coming soon
149+
150+
### Low level
151+
152+
> Coming soon (Wiki)
42153
43154
## Tests
44155

@@ -47,19 +158,23 @@ To run tests you should clone project and install `pytest` and `requests` (to do
47158
```
48159
git clone https://github.com/bogdandm/json2python-models.git
49160
cd json2python-models
50-
pip install pytest>=4.4.0
51161
52-
python setup.py test -a '<pytest arguments>'
53-
or
54-
pytest tests
162+
python setup.py test -a '<pytest additional arguments>'
55163
```
56164

57-
Also I would recommend you to install `pytest-xdist` for parallel execution
58-
and `pytest-sugar` for pretty printing test results
165+
Also I would recommend you to install `pytest-sugar` for pretty printing test results
59166

60167
### Test examples
61168

62-
>
169+
You can find out some examples of usage of this project at [testing_tools/real_apis/...](/testing_tools/real_apis)
170+
171+
Each file contains functions to download data from some online API (references included at the top of file) and
172+
`main` function that generate and print code. Some examples may print debug data before actual code.
173+
Downloaded data will be saved at `testing_tools/real_apis/<name of example>/<dataset>.json`
174+
175+
## API docs
176+
177+
> Coming soon (Wiki)
63178
64179
## Built With
65180

@@ -72,11 +187,12 @@ and `pytest-sugar` for pretty printing test results
72187
## Contributing
73188

74189
Feel free to open pull requests with new features or bug fixes. Just follow few rules:
190+
75191
1. Always use some code formatter ([black](https://github.com/ambv/black) or PyCharm built-in)
76192
2. Keep code coverage above 95-98%
77-
3. All existing tests should be passed (including test examples)
193+
3. All existing tests should be passed (including test examples from `testing_tools/real_apis`)
78194
4. Use `typing` module
79-
5. Fix [codacy](https://app.codacy.com/project/bogdandm/json2python-models/dashboard) issues
195+
5. Fix [codacy](https://app.codacy.com/project/bogdandm/json2python-models/dashboard) issues from your PR
80196

81197
## License
82198

etc/logo.png

-1.24 KB
Loading

testing_tools/real_apis/f1.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from json_to_models.models.attr import AttrsModelCodeGenerator
1212
from json_to_models.models.base import generate_code
1313
from json_to_models.registry import ModelRegistry
14-
from json_to_models.utils import json_format
1514
from testing_tools.pprint_meta_data import pretty_format_meta
1615
from testing_tools.real_apis import dump_response
1716

@@ -58,8 +57,8 @@ def main():
5857
print("=" * 20, end='')
5958

6059
structure = compose_models(reg.models_map)
61-
print('\n', json_format([structure[0], {str(a): str(b) for a, b in structure[1].items()}]))
62-
print("=" * 20)
60+
# print('\n', json_format([structure[0], {str(a): str(b) for a, b in structure[1].items()}]))
61+
# print("=" * 20)
6362

6463
print(generate_code(structure, AttrsModelCodeGenerator))
6564

testing_tools/real_apis/openlibrary.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import requests
55

66
from json_to_models.generator import MetadataGenerator
7+
from json_to_models.models import compose_models
8+
from json_to_models.models.attr import AttrsModelCodeGenerator
9+
from json_to_models.models.base import generate_code
710
from json_to_models.registry import ModelRegistry
8-
from testing_tools.pprint_meta_data import pretty_format_meta
911
from testing_tools.real_apis import dump_response
1012

1113
session = requests.Session()
@@ -44,16 +46,16 @@ def main():
4446

4547
gen = MetadataGenerator()
4648
reg = ModelRegistry()
47-
for data in (search_result, books):
48-
reg.process_meta_data(gen.generate(*data))
49+
reg.process_meta_data(gen.generate(*search_result), model_name="Search")
50+
reg.process_meta_data(gen.generate(*books), model_name="Book")
4951
reg.merge_models(generator=gen)
5052

51-
print("\n" + "=" * 20, end='')
53+
print("\n" + "=" * 20)
5254
for model in reg.models:
5355
model.generate_name()
54-
for model in reg.models:
55-
print(pretty_format_meta(model))
56-
print("\n" + "=" * 20, end='')
56+
57+
structure = compose_models(reg.models_map)
58+
print(generate_code(structure, AttrsModelCodeGenerator))
5759

5860

5961
if __name__ == '__main__':

testing_tools/real_apis/pathofexile.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Path of Exile API http://www.pathofexile.com/developer/docs/api-resource-public-stash-tabs
2+
Path of Exile Stash API http://www.pathofexile.com/developer/docs/api-resource-public-stash-tabs
33
"""
44
from datetime import datetime
55

@@ -10,7 +10,6 @@
1010
from json_to_models.models.attr import AttrsModelCodeGenerator
1111
from json_to_models.models.base import generate_code
1212
from json_to_models.registry import ModelRegistry
13-
from json_to_models.utils import json_format
1413
from testing_tools.pprint_meta_data import pretty_format_meta
1514
from testing_tools.real_apis import dump_response
1615

@@ -34,12 +33,13 @@ def main():
3433
reg.merge_models(generator=gen)
3534
reg.generate_names()
3635

36+
print("Meta tree:")
3737
print(pretty_format_meta(next(iter(reg.models))))
3838
print("\n" + "=" * 20, end='')
3939

4040
structure = compose_models_flat(reg.models_map)
41-
print('\n', json_format([structure[0], {str(a): str(b) for a, b in structure[1].items()}]))
42-
print("=" * 20)
41+
# print('\n', json_format([structure[0], {str(a): str(b) for a, b in structure[1].items()}]))
42+
# print("=" * 20)
4343

4444
print(generate_code(structure, AttrsModelCodeGenerator))
4545
print(f"{(datetime.now() - start_t).total_seconds():.4f} seconds")

testing_tools/real_apis/randomapis.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@ def main():
3939

4040
gen = MetadataGenerator()
4141
reg = ModelRegistry()
42-
for data in ([chroniclingamerica_data], [launchlibrary_data], university_domains_data):
43-
fields = gen.generate(*data)
44-
reg.process_meta_data(fields)
42+
43+
fields = gen.generate(chroniclingamerica_data)
44+
reg.process_meta_data(fields, model_name="CHRONICLING")
45+
46+
fields = gen.generate(launchlibrary_data)
47+
reg.process_meta_data(fields, model_name="LaunchLibrary")
48+
49+
fields = gen.generate(*university_domains_data)
50+
reg.process_meta_data(fields, model_name="Universities")
51+
4552
reg.merge_models(generator=gen)
4653
reg.generate_names()
54+
4755
structure = compose_models(reg.models_map)
4856
print(generate_code(structure, AttrsModelCodeGenerator))
4957

0 commit comments

Comments
 (0)