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 datasets.
13
13
14
14
## Features
15
15
@@ -155,9 +155,9 @@ class Constructor(BaseModel):
155
155
156
156
` swagger.json ` from any online API (I tested file generated by drf-yasg and another one for Spotify API)
157
157
158
- It requires a lit bit of tweaking:
158
+ It requires a bit of tweaking:
159
159
* Some fields store routes/models specs as dicts
160
- * There is a lot of optinal fields so we reduce merging threshold
160
+ * There are a lot of optinal fields so we reduce merging threshold
161
161
* Disable string literals
162
162
163
163
```
@@ -405,9 +405,45 @@ class Run(BaseModel):
405
405
406
406
</p ></details >
407
407
408
+ ### Example with preamble
409
+
410
+ <details ><summary >----- Show -----</summary >
411
+ <p >
412
+ A simple example to demonstrate adding extra code before the class list.
413
+
414
+ ``` sh
415
+ json2models -f pydantic --preamble " # set up defaults
416
+ USERNAME = 'user'
417
+ SERVER_IP = '127.0.0.1'
418
+ " -m Swagger testing_tools/swagger.json
419
+ ```
420
+
421
+ ``` py
422
+ r """
423
+ generated by json2python-models v0.2.5 at Tue Aug 23 08:55:09 2022
424
+ command: json2models -f pydantic --preamble # set up defaults
425
+ USERNAME = 'user'
426
+ SERVER_IP = '127.0.0.1'
427
+ -m Swagger testing_tools/swagger.json -o output.py
428
+ """
429
+ from pydantic import BaseModel, Field
430
+ from typing import Any, List, Literal, Optional, Union
431
+
432
+
433
+ # set up defaults
434
+ USERNAME = ' user'
435
+ SERVER_IP = ' 127.0.0.1'
436
+
437
+
438
+
439
+ class Swagger (BaseModel ):
440
+ # etc.
441
+ ```
442
+ </p ></details >
443
+
408
444
## Installation
409
445
410
- | ** Be ware ** : this project supports only ` python3.7 ` and higher. |
446
+ | ** Beware ** : this project supports only ` python3.7 ` and higher. |
411
447
| --- |
412
448
413
449
To install it, use ` pip ` :
@@ -426,7 +462,7 @@ python setup.py install
426
462
427
463
### CLI
428
464
429
- For regular usage CLI tool is the best option. After you install this package you could use it as ` json2models <arguments> `
465
+ For regular usage CLI tool is the best option. After you install this package you can use it as ` json2models <arguments> `
430
466
or ` python -m json_to_models <arguments> ` . I.e.:
431
467
```
432
468
json2models -m Car car_*.json -f attrs > car.py
@@ -464,61 +500,71 @@ Arguments:
464
500
* ** Format** : ` -f {base, pydantic, attrs, dataclasses, custom} `
465
501
* ** Example** : ` -f pydantic `
466
502
* ** Default** : ` -f base `
467
-
503
+
468
504
* ` -s ` , ` --structure ` - Models composition style.
469
- * ** Format** : ` -s {flat, nested} `
505
+ * ** Format** : ` -s {flat, nested} `
470
506
* ** Example** : ` -s nested `
471
507
* ** Default** : ` -s flat `
472
-
508
+
509
+ * ` --preamble ` - Additional material to be
510
+ * ** Format** : ` --preamble "<formatted python code string to be added after module imports>" `
511
+ * ** Example** :
512
+ ``` sh
513
+ --preamble " # set up defaults
514
+ USERNAME = 'user'
515
+ SERVER = '127.0.0.1'"
516
+ ```
517
+ * ** Optional**
518
+
473
519
* ` --datetime ` - Enable datetime/date/time strings parsing.
474
520
* ** Default** : disabled
475
521
* ** Warning** : This can lead to 6-7 times slowdown on large datasets. Be sure that you really need this option.
476
-
522
+
477
523
* ` --disable-unicode-conversion ` , ` --no-unidecode ` - Disable unicode conversion in field labels and class names
478
524
* ** Default** : enabled
479
-
525
+
480
526
* ` --strings-converters ` - Enable generation of string types converters (i.e. ` IsoDatetimeString ` or ` BooleanString ` ).
481
527
* ** Default** : disabled
482
-
528
+
483
529
* ` --max-strings-literals ` - Generate ` Literal['foo', 'bar'] ` when field have less than NUMBER string constants as values.
484
- * ** Format** : ` --max-strings-literals <NUMBER> `
530
+ * ** Format** : ` --max-strings-literals <NUMBER> `
485
531
* ** Default** : 10 (generator classes could override it)
486
532
* ** Example** : ` --max-strings-literals 5 ` - only 5 literals will be saved and used to code generation
487
533
* ** Note** : There could not be more than ** 15** literals per field (for performance reasons)
488
534
* ** Note** : ` attrs ` code generator do not use Literals and just generate ` str ` fields instead
489
535
490
- * ` --merge ` - Merge policy settings. Possible values are:
536
+ * ` --merge ` - Merge policy settings. Possible values are:
491
537
* ** Format** : ` --merge MERGE_POLICY [MERGE_POLICY ...] `
492
538
* ** 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.
539
+ * ` percent[_<percent>] ` - two models had a certain percentage of matched field names.
540
+ Custom value could be i.e. ` percent_95 ` .
541
+ * ` number[_<number>] ` - two models had a certain number of matched field names.
496
542
* ` exact ` - two models should have exact same field names to merge.
497
543
* ** Example** : ` --merge percent_95 number_20 ` - merge if 95% of fields are matched or 20 of fields are matched
498
544
* ** Default** : ` --merge percent_70 number_10 `
499
-
545
+
500
546
* ` --dict-keys-regex ` , ` --dkr ` - List of regular expressions (Python syntax).
501
- If all keys of some dict are match one of the pattern then
547
+ If all keys of some dict are match one of the pattern then
502
548
this dict will be marked as dict field but not nested model.
503
549
* ** Format** : ` --dkr RegEx [RegEx ...] `
504
550
* ** Example** : ` --dkr node_\d+ \d+_\d+_\d+ `
505
- * ** Note** : ` ^ ` and ` $ ` (string borders) tokens will be added automatically but you
551
+ * ** Note** : ` ^ ` and ` $ ` (string borders) tokens will be added automatically but you
506
552
have to escape other special characters manually.
507
553
* ** Optional**
508
-
554
+
509
555
* ` --dict-keys-fields ` , ` --dkf ` - List of model fields names that will be marked as dict fields
510
556
* ** Format** : ` --dkf FIELD_NAME [FIELD_NAME ...] `
511
557
* ** Example** : ` --dkf "dict_data" "mapping" `
512
558
* ** Optional**
513
-
559
+
514
560
* ` --code-generator ` - Absolute import path to ` GenericModelCodeGenerator ` subclass.
515
561
* ** Format** : ` --code-generator CODE_GENERATOR `
516
562
* ** Example** : ` -f mypackage.mymodule.DjangoModelsGenerator `
517
563
* ** 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" ` .
564
+
565
+ * ` --code-generator-kwargs ` - List of GenericModelCodeGenerator subclass arguments (for ` __init__ ` method,
566
+ see docs of specific subclass).
567
+ Each argument should be in following format: ` argument_name=value ` or ` "argument_name=value with space" ` .
522
568
Boolean values should be passed in JS style: ` true ` or ` false `
523
569
* ** Format** : ` --code-generator-kwargs [NAME=VALUE [NAME=VALUE ...]] `
524
570
* ** Example** : ` --code-generator-kwargs kwarg1=true kwarg2=10 "kwarg3=It is string with spaces" `
0 commit comments