Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions docs/tutorials/customize.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@

You can crate a schema containing the specified table data with a new token using `new(...)` methods.

```python title="generate_attribute.py"
from t4_devkit.schema import Attribute
from t4_devkit.common.serialize import serialize_dataclass

# schema data except of the unique identifier token
data = {
"name": "foo",
"description": "this is re-generated attribute."
}

attr1 = Attribute.new(data)

# Also, it allows us to create a copy of the existing table data with a new token
serialized = serialize_dataclass(attr1)
attr2 = Attribute.new(serialized)

assert attr1.token != attr2.token
```python
>>> from t4_devkit.schema import Attribute
>>> from t4_devkit.common.serialize import serialize_dataclass
>>>
>>> # schema data except of the unique identifier token
>>> data = {
... "name": "foo",
... "description": "this is re-generated attribute."
... }
>>>
>>> attr1 = Attribute.new(data)
>>>
>>> # Also, it allows us to create a copy of the existing table data with a new token
>>> serialized = serialize_dataclass(attr1)
>>> attr2 = Attribute.new(serialized)
>>>
>>> attr1.token != attr2.token
True
>>> attr1
Attribute(token='b08701e5095fbd12a45e7f51b85ffc08', name='foo', description='this is re-generated attribute.')
>>> attr2
Attribute(token='f40e605870aa29b1473ca6e65255c45e', name='foo', description='this is re-generated attribute.')
```

## Customize Schema Class
Expand All @@ -40,14 +45,14 @@ Now you have the following workspace structure:
my_package
├── src
│ ├── __init__.py
│ ├── custom_attribute.py
│ ├── custom_schema.py
│ └── main.py
└── pyproject.toml
```

By editing `custom_attribute.py`, you can customize `Attribute` as follows:
By editing `custom_schema.py`, you can overwrite `Attribute` as follows:

```python title="custom_attribute.py"
```python title="custom_schema.py"
from __future__ import annotations

from attrs import define, field
Expand All @@ -58,7 +63,7 @@ from t4_devkit.common.io import load_json
__all__ = ["CustomAttribute"]


@define
@define(slots=False)
@SCHEMAS.register(SchemaName.ATTRIBUTE, force=True)
class CustomAttribute(SchemaBase):
"""Custom Attribute class ignoring if there is no `description` field.
Expand All @@ -72,7 +77,7 @@ class CustomAttribute(SchemaBase):
description: str | None = field(default=None)
```

Note that `CustomAttribute` should be imported before to instantiate `Tier4` class.
Note that `CustomAttribute` should be imported before instantiating `Tier4` class.
Then modify `__init__.py` in order to import it automatically:

```python title="__init__.py"
Expand Down
Loading