Skip to main content

Overview

This library streamlines the process of creating data classes and offers a versatile configuration model. It also enables the dumping and restoration of data. This library is built on Pydantic.

Installation

You can add it to your project using the following command:

pip install t-object

After installing, you can import it into your project using the command

from t_object import ThoughtfulObject.

Make sure to check the documentation for detailed usage instructions and examples.

Key features you need to know

1. All features of the original Pydantic library are available.

  • Creating models

class Driver(ThoughtfulObject):
    name: str
    age: int
    driving_experience: timedelta
    last_driving_date: datetime

class Car(ThoughtfulObject):
    model: str
    car_release_date: date
    price: float
    driver: list[Driver]

and

class Patient(ThoughtfulObject):
    name: str
    age: int
    birth_date: datetime
  • creating instanse of model

car = Car(
    model="Tesla Model S",
    car_release_date=date(2012, 6, 22),
    price=79999.99,
    driver=[
            Driver(
                name="Elon Musk",
                age=49,
                driving_experience=timedelta(days=365 * 30),
                last_driving_date=datetime(2021, 1, 1)
    ),
        Driver(
              name="Jeff Bezos",
              age=57,
              driving_experience=timedelta(days=365 * 20),
              last_driving_date=datetime(2021, 1, 1)
     )]
)

and

patient = Patient(
    name="John Doe",
    age=42,
    birth_date=datetime(1979, 1, 1)
)

2. Configuration

  • Default configuration of the T-Object. Simply import it using from t_object import ThoughtfulObject. Default configuration listed below.

validate_assignment=True,
extra=Extra.forbid,
frozen=False,
populate_by_name=False,
arbitrary_types_allowed=True,
allow_inf_nan=True,
strict=True,
revalidate_instances=RevalidateInstances.always,
validate_default=False,
coerce_numbers_to_str=True,
validation_error_cause=True,
str_strip_whitespace=True,
ResponseObject = build_custom_t_object(
      extra=Extra.allow,
      frozen=True,
      allow_inf_nan=True,
      strict=False,
)
class UserResponse(ResponseObject):
    name: str
    age: int
    dob: datetime

3. Exporting the model to JSON format

  • To export data, use the save_to_json_file() method. You can either define the file path manually or leave it blank for automatic naming.

4. Importing JSON into the Model

  • To import data from a JSON file, use the load_from_json_file(file_path: str) class method. This method validates the data against your model automatically. The file_path attribute is required, which is the path to the JSON file.

patient = Patient.load_from_json_file("patient.json")

5. Pretty String

It is possible to print any instance in a more readable and attractive format. This formatting can be achieved by employing the pretty_string() method. This method allows for the effortless transformation of the raw data into a format that is easier on the eyes,

Metadata

Release files for t-object 0.1.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for t-object 0.1.13
File Size Uploaded
t_object-0.1.13.tar.gz 9.9 kB Details

Release files / t_object-0.1.13.tar.gz

Download URL t_object-0.1.13.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
306c034b445ebc62f63f7d9afccb027bf6e6aa17d5fdc00a5aaf4ca299ebf07c
BLAKE2b-256 checksum
How to use checksums
f156aba3f030797130fc9c3a4c9a4f8477312582409f9e70e353abddce6df759
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.0

Release history Release notifications | RSS feed

This release

0.1.13 This release

1 release file

0.1.12

1 release file

0.1.11

1 release file

0.1.10

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page