Skip to content

Commit c3df847

Browse files
author
patrick
committed
add more files
1 parent fcbe5f6 commit c3df847

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,121 @@ copier copy --trust https://github.com/fluent-qa/fluentqa-pytpl.git my-project
1414
mkdir my-project && cd my-project
1515
pdm init https://github.com/fluent-qa/fluentqa-pytpl.git
1616
```
17+
18+
19+
## How to Use
20+
21+
data and format:
22+
23+
```json
24+
{
25+
"key": "v1",
26+
"kList": [
27+
"test",
28+
"t2"
29+
],
30+
"kObj": {
31+
"k1": "v1",
32+
"k2": "v2"
33+
},
34+
"kBool": true,
35+
"kNum": 4.3
36+
}
37+
38+
```
39+
40+
```python
41+
42+
class DemoModel(GenericDataModel):
43+
key: str
44+
k_list: []
45+
k_obj: Any
46+
k_bool: bool
47+
k_num: float
48+
49+
50+
class DemoModelAlias(GenericDataModel):
51+
key: str
52+
k_list: []
53+
k_obj: Any
54+
k_bool: bool
55+
# k_index: float = Field(alias='k_num')
56+
k_index: Annotated[float, Field(alias='kNum')]
57+
58+
59+
class DemoUnit(GenericDataModel):
60+
unit_group_name: str | None = ""
61+
unit_name: str | None = ""
62+
unit_symbol: str | None = ""
63+
unit_latex: str | None = ""
64+
base_unit: str | None = ""
65+
factor: str | None = ""
66+
67+
68+
class UnitInfoRawModel(BaseModel):
69+
unit_name: str = Field("", alias='单位名')
70+
unit_symbol: str = Field("", alias='单位符号')
71+
unit_symbol_latex: str = Field("", alias='单位符号LaTex')
72+
unit_group_name: str = Field("", alias='单位组名称')
73+
base_unit: str = Field("", alias='基准单位')
74+
conversion_factor: None|str |float = Field("", alias='换算系数')
75+
76+
model_config = ConfigDict(
77+
arbitrary_types_allowed=True,
78+
populate_by_name=True,
79+
use_enum_values=True,
80+
)
81+
82+
class UnitInfo(GenericDataModel):
83+
unit_name: str = Field("", alias='单位名')
84+
unit_symbol: str = Field("", alias='单位符号')
85+
unit_symbol_latex: str = Field("", alias='单位符号LaTex')
86+
unit_group_name: str = Field("", alias='单位组名称')
87+
base_unit: str = Field("", alias='基准单位')
88+
conversion_factor: None|str |float = Field("", alias='换算系数')
89+
```
90+
91+
92+
```
93+
def test_modelize_camel_naming():
94+
result = DemoModel.parse_file("./structured-data/camel.json")
95+
print(result)
96+
97+
98+
def test_modelize_alias_naming():
99+
result = DemoModelAlias.parse_file("./structured-data/camel-alias.json")
100+
print(result.k_index)
101+
print(result.model_dump_json(by_alias=True))
102+
print(result.to_json())
103+
104+
105+
def test_parse_as():
106+
result = DemoModelAlias.parse_file("./structured-data/camel-alias.json")
107+
json_str = result.model_dump_json(by_alias=True)
108+
obj_model = sr.parse_as(json_str, DemoModelAlias)
109+
dict_obj = result.to_dict(by_alias=True)
110+
print(obj_model)
111+
dict_model = sr.parse_as(dict_obj, DemoModelAlias)
112+
assert dict_model.key == "v1"
113+
114+
115+
def test_parse_file_as():
116+
result = operations.parse_file_as("./structured-data/camel-alias.json", DemoModelAlias)
117+
assert result.k_index == 4.3
118+
119+
120+
def test_load_csv_files():
121+
result = operations.parse_file_as("./structured-data/unit.csv", DemoUnit, data_format=DataFormatType.CSV)
122+
print(result)
123+
124+
125+
def test_load_excel_file():
126+
result = operations.parse_file_as("./structured-data/unit_demo.xlsx", UnitInfo, data_format=DataFormatType.EXCEL)
127+
print(result)
128+
129+
130+
def test_load_excel_file_raw():
131+
result = operations.parse_file_as("./structured-data/unit_demo.xlsx", UnitInfoRawModel,
132+
data_format=DataFormatType.EXCEL)
133+
print(result)
134+
```

0 commit comments

Comments
 (0)