I'm attempting to use the library as a module, but having a difficult time figuring out the correct inputs for generate from the documentation. Command line usage is a no brainer:
# Note: test_data.csv is currently just the data from simple.csv in the tests
# % datamodel-codegen --input test_data.csv --input-file-type csv --output test_model.py
# generated by datamodel-codegen:
# filename: test_data.csv
# timestamp: 2021-04-30T20:18:11+00:00
from __future__ import annotations
from pydantic import BaseModel, Field
class Model(BaseModel):
id: str
name: str
tel: str
zip_code: str = Field(..., alias='zip code')
... but when trying to include this in my own code (based on this example: https://koxudaxi.github.io/datamodel-code-generator/using_as_module/) ... I'm trying to figure out the most straight forward way. I don't need a temp directory or even to write it out to disk ATM.
This is what I'm currently doing:
from pathlib import Path
from tempfile import TemporaryDirectory
from datamodel_code_generator import InputFileType, generate
with TemporaryDirectory() as temporary_directory_name:
temporary_directory = Path(temporary_directory_name)
output = Path(temporary_directory / 'model.py')
generate(
Path("test_data.csv"),
input_file_type=InputFileType.CSV,
input_filename="test_data.csv",
output=output,
)
model: str = output.read_text()
print(model)
... it works, but seems super wonky. Amidoingitrite?
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too