t-object


Namet-object JSON
Version 0.1.10 PyPI version JSON
download
home_pagehttps://www.thoughtful.ai/
Summaryt_object
upload_time2025-01-14 13:29:15
maintainerNone
docs_urlNone
authorThoughtful
requires_python>=3.9
licenseNone
keywords t_object
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            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

.. code-block:: python

    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

.. code-block:: python

    class Patient(ThoughtfulObject):
        name: str
        age: int
        birth_date: datetime

- creating instanse of model

.. code-block:: python

    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

.. code-block:: python

    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.

.. code-block:: python

    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,


- For custom configuration, use the `build_custom_t_object` function. You can find all configuration flags at https://docs.pydantic.dev/2.6/api/config/. Here is how to use it

.. code-block:: python

    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.

.. code-block:: python

    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,

            

Raw data

            {
    "_id": null,
    "home_page": "https://www.thoughtful.ai/",
    "name": "t-object",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "t_object",
    "author": "Thoughtful",
    "author_email": "support@thoughtful.ai",
    "download_url": "https://files.pythonhosted.org/packages/ed/ff/b4f75c33c20e2434a489c21eb6717da9625640dfdbe1b7a15a83d09b4146/t_object-0.1.10.tar.gz",
    "platform": null,
    "description": "Overview\n=============\n\n\nThis 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.\n\nInstallation\n=============\n\n\nYou can add it to your project using the following command:\n\n`pip install t-object`\n\nAfter installing, you can import it into your project using the command\n\n`from t_object import ThoughtfulObject`.\n\nMake sure to check the documentation for detailed usage instructions and examples.\n\n\nKey features you need to know\n=============================\n\n\n1. All features of the original Pydantic library are available.\n---------------------------------------------------------------\n\n- Creating models\n\n.. code-block:: python\n\n    class Driver(ThoughtfulObject):\n        name: str\n        age: int\n        driving_experience: timedelta\n        last_driving_date: datetime\n\n    class Car(ThoughtfulObject):\n        model: str\n        car_release_date: date\n        price: float\n        driver: list[Driver]\n\n\nand\n\n.. code-block:: python\n\n    class Patient(ThoughtfulObject):\n        name: str\n        age: int\n        birth_date: datetime\n\n- creating instanse of model\n\n.. code-block:: python\n\n    car = Car(\n        model=\"Tesla Model S\",\n        car_release_date=date(2012, 6, 22),\n        price=79999.99,\n        driver=[\n                Driver(\n                    name=\"Elon Musk\",\n                    age=49,\n                    driving_experience=timedelta(days=365 * 30),\n                    last_driving_date=datetime(2021, 1, 1)\n        ),\n            Driver(\n                  name=\"Jeff Bezos\",\n                  age=57,\n                  driving_experience=timedelta(days=365 * 20),\n                  last_driving_date=datetime(2021, 1, 1)\n         )]\n    )\n\nand\n\n.. code-block:: python\n\n    patient = Patient(\n        name=\"John Doe\",\n        age=42,\n        birth_date=datetime(1979, 1, 1)\n    )\n\n2. Configuration\n-----------------\n\n- Default configuration of the T-Object. Simply import it using `from t_object import ThoughtfulObject`. Default configuration listed below.\n\n.. code-block:: python\n\n    validate_assignment=True,\n    extra=Extra.forbid,\n    frozen=False,\n    populate_by_name=False,\n    arbitrary_types_allowed=True,\n    allow_inf_nan=True,\n    strict=True,\n    revalidate_instances=RevalidateInstances.always,\n    validate_default=False,\n    coerce_numbers_to_str=True,\n    validation_error_cause=True,\n    str_strip_whitespace=True,\n\n\n- For custom configuration, use the `build_custom_t_object` function. You can find all configuration flags at https://docs.pydantic.dev/2.6/api/config/. Here is how to use it\n\n.. code-block:: python\n\n    ResponseObject = build_custom_t_object(\n          extra=Extra.allow,\n          frozen=True,\n          allow_inf_nan=True,\n          strict=False,\n    )\n    class UserResponse(ResponseObject):\n        name: str\n        age: int\n        dob: datetime\n\n3. Exporting the model to JSON format\n--------------------------------------\n\n\n- 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.\n\n\n4. Importing JSON into the Model\n---------------------------------\n\n\n- 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.\n\n.. code-block:: python\n\n    patient = Patient.load_from_json_file(\"patient.json\")\n\n5. Pretty String\n-----------------\n\n\nIt 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,\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "t_object",
    "version": "0.1.10",
    "project_urls": {
        "Homepage": "https://www.thoughtful.ai/"
    },
    "split_keywords": [
        "t_object"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "edffb4f75c33c20e2434a489c21eb6717da9625640dfdbe1b7a15a83d09b4146",
                "md5": "b5fa46a2b9a02c9514016379ae065930",
                "sha256": "2d3918359d97cca1ef6be2bfc7b43dc2be2ce39f554c497a1a698f1648156999"
            },
            "downloads": -1,
            "filename": "t_object-0.1.10.tar.gz",
            "has_sig": false,
            "md5_digest": "b5fa46a2b9a02c9514016379ae065930",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 9705,
            "upload_time": "2025-01-14T13:29:15",
            "upload_time_iso_8601": "2025-01-14T13:29:15.218170Z",
            "url": "https://files.pythonhosted.org/packages/ed/ff/b4f75c33c20e2434a489c21eb6717da9625640dfdbe1b7a15a83d09b4146/t_object-0.1.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-14 13:29:15",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "t-object"
}
        
Elapsed time: 1.28338s