1
- [ ![ json2python-models] ( /etc/logo.png )] ( https://github.com/bogdandm/json2python-models )
1
+ dr [ ![ json2python-models] ( /etc/logo.png )] ( https://github.com/bogdandm/json2python-models )
2
2
3
3
[ ![ PyPI version] ( https://img.shields.io/pypi/v/json2python-models.svg?color=green )] ( https://badge.fury.io/py/json2python-models )
4
4
[ ![ Build] ( https://github.com/bogdandm/json2python-models/actions/workflows/test_and_release.yml/badge.svg )] ( https://github.com/bogdandm/json2python-models/actions/workflows/test_and_release.yml )
7
7
8
8
![ Example] ( /etc/convert.png )
9
9
10
- json2python-models is a [ Python] ( https://www.python.org/ ) tool that can generate Python models classes
11
- ([ pydantic] ( https://github.com/samuelcolvin/pydantic ) , dataclasses, [ attrs] ( https://github.com/python-attrs/attrs ) )
12
- from JSON dataset.
10
+ json2python-models is a [ Python] ( https://www.python.org/ ) tool that can generate Python models classes
11
+ ([ pydantic] ( https://github.com/samuelcolvin/pydantic ) , dataclasses, [ attrs] ( https://github.com/python-attrs/attrs ) )
12
+ from JSON dataset.
13
13
14
14
## Features
15
15
@@ -26,18 +26,23 @@ from JSON dataset.
26
26
27
27
## Table of Contents
28
28
29
- * [ Features] ( #features )
30
- * [ Table of Contents] ( #table-of-contents )
31
- * [ Example] ( #example )
32
- * [ Installation] ( #installation )
33
- * [ Usage] ( #usage )
34
- * [ CLI] ( #cli )
35
- * [ Low level API] ( )
36
- * [ Tests] ( #tests )
37
- * [ Test examples] ( #test-examples )
38
- * [ Built With] ( #built-with )
39
- * [ Contributing] ( #contributing )
40
- * [ License] ( #license )
29
+ - [ Features] ( #features )
30
+ - [ Table of Contents] ( #table-of-contents )
31
+ - [ Examples] ( #examples )
32
+ - [ Part of Path of Exile public items API] ( #part-of-path-of-exile-public-items-api )
33
+ - [ F1 Season Results] ( #f1-season-results )
34
+ - [ Swagger] ( #swagger )
35
+ - [ Github-actions config files] ( #github-actions-config-files )
36
+ - [ Example with preamble] ( #example-with-preamble )
37
+ - [ Installation] ( #installation )
38
+ - [ Usage] ( #usage )
39
+ - [ CLI] ( #cli )
40
+ - [ Low level API] ( #low-level-api )
41
+ - [ Tests] ( #tests )
42
+ - [ Test examples] ( #test-examples )
43
+ - [ Built With] ( #built-with )
44
+ - [ Contributing] ( #contributing )
45
+ - [ License] ( #license )
41
46
42
47
## Examples
43
48
@@ -155,9 +160,9 @@ class Constructor(BaseModel):
155
160
156
161
` swagger.json ` from any online API (I tested file generated by drf-yasg and another one for Spotify API)
157
162
158
- It requires a lit bit of tweaking:
163
+ It requires a bit of tweaking:
159
164
* Some fields store routes/models specs as dicts
160
- * There is a lot of optinal fields so we reduce merging threshold
165
+ * There are a lot of optinal fields so we reduce merging threshold
161
166
* Disable string literals
162
167
163
168
```
@@ -405,9 +410,45 @@ class Run(BaseModel):
405
410
406
411
</p ></details >
407
412
413
+ ### Example with preamble
414
+
415
+ <details ><summary >----- Show -----</summary >
416
+ <p >
417
+ A simple example to demonstrate adding extra code before the class list.
418
+
419
+ ``` sh
420
+ json2models -f pydantic --preamble " # set up defaults
421
+ USERNAME = 'user'
422
+ SERVER_IP = '127.0.0.1'
423
+ " -m Swagger testing_tools/swagger.json
424
+ ```
425
+
426
+ ``` py
427
+ r """
428
+ generated by json2python-models v0.2.5 at Tue Aug 23 08:55:09 2022
429
+ command: json2models -f pydantic --preamble # set up defaults
430
+ USERNAME = 'user'
431
+ SERVER_IP = '127.0.0.1'
432
+ -m Swagger testing_tools/swagger.json -o output.py
433
+ """
434
+ from pydantic import BaseModel, Field
435
+ from typing import Any, List, Literal, Optional, Union
436
+
437
+
438
+ # set up defaults
439
+ USERNAME = ' user'
440
+ SERVER_IP = ' 127.0.0.1'
441
+
442
+
443
+
444
+ class Swagger (BaseModel ):
445
+ # etc.
446
+ ```
447
+ </p ></details >
448
+
408
449
## Installation
409
450
410
- | ** Be ware ** : this project supports only ` python3.7 ` and higher. |
451
+ | ** Beware ** : this project supports only ` python3.7 ` and higher. |
411
452
| --- |
412
453
413
454
To install it, use ` pip ` :
@@ -426,7 +467,7 @@ python setup.py install
426
467
427
468
### CLI
428
469
429
- For regular usage CLI tool is the best option. After you install this package you could use it as ` json2models <arguments> `
470
+ For regular usage CLI tool is the best option. After you install this package you can use it as ` json2models <arguments> `
430
471
or ` python -m json_to_models <arguments> ` . I.e.:
431
472
```
432
473
json2models -m Car car_*.json -f attrs > car.py
@@ -464,61 +505,71 @@ Arguments:
464
505
* ** Format** : ` -f {base, pydantic, attrs, dataclasses, custom} `
465
506
* ** Example** : ` -f pydantic `
466
507
* ** Default** : ` -f base `
467
-
508
+
468
509
* ` -s ` , ` --structure ` - Models composition style.
469
- * ** Format** : ` -s {flat, nested} `
510
+ * ** Format** : ` -s {flat, nested} `
470
511
* ** Example** : ` -s nested `
471
512
* ** Default** : ` -s flat `
472
-
513
+
514
+ * ` --preamble ` - Additional material to be
515
+ * ** Format** : ` --preamble "<formatted python code string to be added after module imports>" `
516
+ * ** Example** :
517
+ ``` sh
518
+ --preamble " # set up defaults
519
+ USERNAME = 'user'
520
+ SERVER = '127.0.0.1'"
521
+ ```
522
+ * ** Optional**
523
+
473
524
* ` --datetime ` - Enable datetime/date/time strings parsing.
474
525
* ** Default** : disabled
475
526
* ** Warning** : This can lead to 6-7 times slowdown on large datasets. Be sure that you really need this option.
476
-
527
+
477
528
* ` --disable-unicode-conversion ` , ` --no-unidecode ` - Disable unicode conversion in field labels and class names
478
529
* ** Default** : enabled
479
-
530
+
480
531
* ` --strings-converters ` - Enable generation of string types converters (i.e. ` IsoDatetimeString ` or ` BooleanString ` ).
481
532
* ** Default** : disabled
482
-
533
+
483
534
* ` --max-strings-literals ` - Generate ` Literal['foo', 'bar'] ` when field have less than NUMBER string constants as values.
484
- * ** Format** : ` --max-strings-literals <NUMBER> `
535
+ * ** Format** : ` --max-strings-literals <NUMBER> `
485
536
* ** Default** : 10 (generator classes could override it)
486
537
* ** Example** : ` --max-strings-literals 5 ` - only 5 literals will be saved and used to code generation
487
538
* ** Note** : There could not be more than ** 15** literals per field (for performance reasons)
488
539
* ** Note** : ` attrs ` code generator do not use Literals and just generate ` str ` fields instead
489
540
490
- * ` --merge ` - Merge policy settings. Possible values are:
541
+ * ` --merge ` - Merge policy settings. Possible values are:
491
542
* ** Format** : ` --merge MERGE_POLICY [MERGE_POLICY ...] `
492
543
* ** Possible values** (MERGE_POLICY):
493
- * ` percent[_<percent>] ` - two models had a certain percentage of matched field names.
494
- Custom value could be i.e. ` percent_95 ` .
495
- * ` number[_<number>] ` - two models had a certain number of matched field names.
544
+ * ` percent[_<percent>] ` - two models had a certain percentage of matched field names.
545
+ Custom value could be i.e. ` percent_95 ` .
546
+ * ` number[_<number>] ` - two models had a certain number of matched field names.
496
547
* ` exact ` - two models should have exact same field names to merge.
497
548
* ** Example** : ` --merge percent_95 number_20 ` - merge if 95% of fields are matched or 20 of fields are matched
498
549
* ** Default** : ` --merge percent_70 number_10 `
499
-
550
+
500
551
* ` --dict-keys-regex ` , ` --dkr ` - List of regular expressions (Python syntax).
501
- If all keys of some dict are match one of the pattern then
552
+ If all keys of some dict are match one of the pattern then
502
553
this dict will be marked as dict field but not nested model.
503
554
* ** Format** : ` --dkr RegEx [RegEx ...] `
504
555
* ** Example** : ` --dkr node_\d+ \d+_\d+_\d+ `
505
- * ** Note** : ` ^ ` and ` $ ` (string borders) tokens will be added automatically but you
556
+ * ** Note** : ` ^ ` and ` $ ` (string borders) tokens will be added automatically but you
506
557
have to escape other special characters manually.
507
558
* ** Optional**
508
-
559
+
509
560
* ` --dict-keys-fields ` , ` --dkf ` - List of model fields names that will be marked as dict fields
510
561
* ** Format** : ` --dkf FIELD_NAME [FIELD_NAME ...] `
511
562
* ** Example** : ` --dkf "dict_data" "mapping" `
512
563
* ** Optional**
513
-
564
+
514
565
* ` --code-generator ` - Absolute import path to ` GenericModelCodeGenerator ` subclass.
515
566
* ** Format** : ` --code-generator CODE_GENERATOR `
516
567
* ** Example** : ` -f mypackage.mymodule.DjangoModelsGenerator `
517
568
* ** Note** : Is ignored without ` -f custom ` but is required with it.
518
-
519
- * ` --code-generator-kwargs ` - List of GenericModelCodeGenerator subclass arguments (for ` __init__ ` method,
520
- see docs of specific subclass).
521
- Each argument should be in following format: ` argument_name=value ` or ` "argument_name=value with space" ` .
569
+
570
+ * ` --code-generator-kwargs ` - List of GenericModelCodeGenerator subclass arguments (for ` __init__ ` method,
571
+ see docs of specific subclass).
572
+ Each argument should be in following format: ` argument_name=value ` or ` "argument_name=value with space" ` .
522
573
Boolean values should be passed in JS style: ` true ` or ` false `
523
574
* ** Format** : ` --code-generator-kwargs [NAME=VALUE [NAME=VALUE ...]] `
524
575
* ** Example** : ` --code-generator-kwargs kwarg1=true kwarg2=10 "kwarg3=It is string with spaces" `
0 commit comments