Skip to content

Commit

Permalink
refactor!: rename installable serializable -> py_serializable
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <[email protected]>
  • Loading branch information
jkowalleck committed Jan 5, 2025
1 parent afe70c8 commit 39bfc1a
Show file tree
Hide file tree
Showing 24 changed files with 127 additions and 127 deletions.
4 changes: 2 additions & 2 deletions .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[settings]
## read the docs: https://pycqa.github.io/isort/docs/configuration/options.html
## keep in sync with flake8 config - in `tox.ini` file
known_first_party = serializable
known_first_party = py_serializable
skip_gitignore = true
skip_glob =
build/*,dist/*,__pycache__,.eggs,*.egg-info*,
Expand All @@ -33,4 +33,4 @@ default_section = THIRDPARTY
ensure_newline_before_comments = true
include_trailing_comma = true
line_length = 120
multi_line_output = 3
multi_line_output = 3
2 changes: 1 addition & 1 deletion .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

[mypy]

files = serializable/, tests/model.py
files = py_serializable/, tests/model.py

show_error_codes = True
pretty = True
Expand Down
2 changes: 1 addition & 1 deletion .pytype.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exclude = [

# Space-separated list of files or directories to process.
inputs = [
'serializable',
'py_serializable',
# 'tests/model.py'
]

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Get it all applied via:

```shell
poetry run isort .
poetry run autopep8 -ir serializable/ tests/
poetry run autopep8 -ir py_serializable/ tests/
```

This project prefers `f'strings'` over `'string'.format()`.
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

# Document Python Code
autoapi_type = 'python'
autoapi_dirs = ['../serializable']
autoapi_dirs = ['../py_serializable']
# see https://sphinx-autoapi.readthedocs.io/en/latest/reference/config.html#confval-autoapi_options
autoapi_options = ['show-module-summary', 'members', 'undoc-members', 'inherited-members', 'show-inheritance']

Expand Down
54 changes: 27 additions & 27 deletions docs/customising-structure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,39 @@ Property Name Mappings
----------------------------------------------------

You can directly control mapping of property names for properties in a Class by adding the decorators
:func:`serializable.json_name()` or :func:`serializable.xml_name()`.
:func:`py_serializable.json_name()` or :func:`py_serializable.xml_name()`.

For example, you might have a property called **isbn** in your class, but when serialized to JSON it should be called
**isbn_number**.

To implement this mapping, you would alter your class as follows adding the :func:`serializable.json_name()`
To implement this mapping, you would alter your class as follows adding the :func:`py_serializable.json_name()`
decorator to the **isbn** property:

.. code-block:: python
@serializable.serializable_class
@py_serializable.serializable_class
class Book:
def __init__(self, title: str, isbn: str, publish_date: date, authors: Iterable[str],
...
@property
@serializable.json_name('isbn_number')
@py_serializable.json_name('isbn_number')
def isbn(self) -> str:
return self._isbn
Excluding Property from Serialization
----------------------------------------------------

Properties can be ignored during deserialization by including them in the :func:`serializable.serializable_class()`
Properties can be ignored during deserialization by including them in the :func:`py_serializable.serializable_class()`
annotation as per the following example.

A typical use case for this might be where a JSON schema is referenced, but this is not part of the constructor for the
class you are deserializing to.

.. code-block:: python
@serializable.serializable_class(ignore_during_deserialization=['$schema'])
@py_serializable.serializable_class(ignore_during_deserialization=['$schema'])
class Book:
...
Expand All @@ -74,7 +74,7 @@ You can force a Property to be serialized even when the value is ``None`` by ann

.. code-block:: python
@serializable.include_none
@py_serializable.include_none
def email(self) -> Optional[str]:
return self._email
Expand All @@ -88,37 +88,37 @@ strings.
Depending on your use case, the string format could vary, and thus this library makes no assumptions. We have provided
an some example helpers for (de-)serializing dates and datetimes.

To define a custom serializer for a property, add the :func:`serializable.type_mapping()` decorator to the property.
For example, to have a property named *created* be use the :class:`serializable.helpers.Iso8601Date` helper you
To define a custom serializer for a property, add the :func:`py_serializable.type_mapping()` decorator to the property.
For example, to have a property named *created* be use the :class:`py_serializable.helpers.Iso8601Date` helper you
would add the following method to your class:

.. code-block:: python
@serializable.serializable_class
@py_serializable.serializable_class
class Book:
def __init__(self, title: str, isbn: str, publish_date: date, authors: Iterable[str],
...
@property
@serializable.type_mapping(Iso8601Date)
@py_serializable.type_mapping(Iso8601Date)
def publish_date(self) -> date:
return self._publish_date
Writing Custom Property Serializers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can write your own custom property serializer. The only requirements are that it must extend
:class:`serializable.helpers.BaseHelper` and therefore implement the ``serialize()`` and ``deserialize()`` class methods.
:class:`py_serializable.helpers.BaseHelper` and therefore implement the ``serialize()`` and ``deserialize()`` class methods.

For examples, see :mod:`serializable.helpers`.
For examples, see :mod:`py_serializable.helpers`.


Serializing Lists & Sets
----------------------------------------------------

Particularly in XML, there are many ways that properties which return Lists or Sets could be represented. We can handle
this by adding the decorator :func:`serializable.xml_array()` to the appropriate property in your class.
this by adding the decorator :func:`py_serializable.xml_array()` to the appropriate property in your class.

For example, given a Property that returns ``Set[Chapter]``, this could be serialized in one of a number of ways:

Expand Down Expand Up @@ -161,7 +161,7 @@ For *Example 2*, you would add the following to your class:
.. code-block:: python
@property
@serializable.xml_array(XmlArraySerializationType.NESTED, 'chapter')
@py_serializable.xml_array(XmlArraySerializationType.NESTED, 'chapter')
def chapters(self) -> List[Chapter]:
return self._chapters
Expand All @@ -170,7 +170,7 @@ For *Example 3*, you would add the following to your class:
.. code-block:: python
@property
@serializable.xml_array(XmlArraySerializationType.FLAT, 'chapter')
@py_serializable.xml_array(XmlArraySerializationType.FLAT, 'chapter')
def chapters(self) -> List[Chapter]:
return self._chapters
Expand All @@ -180,21 +180,21 @@ Serializing special XML string types
----------------------------------------------------

In XML, are special string types, ech with defined set of allowed characters and whitespace handling.
We can handle this by adding the decorator :obj:`serializable.xml_string()` to the appropriate property in your class.
We can handle this by adding the decorator :obj:`py_serializable.xml_string()` to the appropriate property in your class.

.. code-block:: python
@property
@serializable.xml_string(serializable.XmlStringSerializationType.TOKEN)
@py_serializable.xml_string(py_serializable.XmlStringSerializationType.TOKEN)
def author(self) -> str:
return self._author
Further examples are available in our :ref:`unit tests <unit-tests>`.

.. note::

The actual transformation is done by :func:`serializable.xml.xs_normalizedString()`
and :func:`serializable.xml.xs_token()`
The actual transformation is done by :func:`py_serializable.xml.xs_normalizedString()`
and :func:`py_serializable.xml.xs_token()`

Serialization Views
----------------------------------------------------
Expand All @@ -207,14 +207,14 @@ By default all Properties will be included in the serialization process, but thi
Defining Views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A View is a class that extends :class:`serializable.ViewType` and you should create classes as required in your
A View is a class that extends :class:`py_serializable.ViewType` and you should create classes as required in your
implementation.

For example:

.. code-block:: python
from serializable import ViewType
from py_serializable import ViewType
class SchemaVersion1(ViewType):
pass
Expand All @@ -230,7 +230,7 @@ For example:
.. code-block:: python
@property
@serializable.view(SchemaVersion1)
@py_serializable.view(SchemaVersion1)
def address(self) -> Optional[str]:
return self._address
Expand All @@ -243,8 +243,8 @@ Further to the above, you can vary the ``None`` value per View as follows:
.. code-block:: python
@property
@serializable.include_none(SchemaVersion2)
@serializable.include_none(SchemaVersion3, "RUBBISH")
@py_serializable.include_none(SchemaVersion2)
@py_serializable.include_none(SchemaVersion3, "RUBBISH")
def email(self) -> Optional[str]:
return self._email
Expand Down Expand Up @@ -274,15 +274,15 @@ XML Element Ordering
Some XML schemas utilise `sequence`_ which requires elements to be in a prescribed order.

You can control the order properties are serialized to elements in XML by utilising the
:func:`serializable.xml_sequence()` decorator. The default sort order applied to properties is 100 (where lower is
:func:`py_serializable.xml_sequence()` decorator. The default sort order applied to properties is 100 (where lower is
earlier in the sequence).

In the example below, the ``isbn`` property will be output first.

.. code-block:: python
@property
@serializable.xml_sequence(1)
@py_serializable.xml_sequence(1)
def isbn(self) -> str:
return self._isbn
Expand Down
10 changes: 5 additions & 5 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ This library utilizes an own instance of `Logger`_, which you may access and add
import sys
import logging
import serializable
import py_serializable
my_log_handler = logging.StreamHandler(sys.stderr)
my_log_handler.setLevel(logging.DEBUG)
my_log_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
serializable.logger.addHandler(my_log_handler)
serializable.logger.setLevel(my_log_handler.level)
serializable.logger.propagate = False
py_serializable.logger.addHandler(my_log_handler)
py_serializable.logger.setLevel(my_log_handler.level)
py_serializable.logger.propagate = False
@serializable.serializable_class
@py_serializable.serializable_class
class Chapter:
def __init__(self, *, number: int, title: str) -> None:
Expand Down
12 changes: 6 additions & 6 deletions docs/formatters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Property Name Formatting
====================================================

By default, ``py-serializable`` uses it's :class:`serializable.formatters.CamelCasePropertyNameFormatter` formatter for
By default, ``py-serializable`` uses it's :class:`py_serializable.formatters.CamelCasePropertyNameFormatter` formatter for
translating actual Python property names to element names in either JSON or XML.

``py-serializable`` includes a number of name formatters out of the box, but you can also create your own if required.
Expand All @@ -28,9 +28,9 @@ Included Formatters

``py-serializable`` includes three common formatters out of the box.

1. Camel Case Formatter: :class:`serializable.formatters.CamelCasePropertyNameFormatter` (the default)
2. Kebab Case Formatter: :class:`serializable.formatters.KebabCasePropertyNameFormatter`
3. Snake Case Formatter: :class:`serializable.formatters.SnakeCasePropertyNameFormatter`
1. Camel Case Formatter: :class:`py_serializable.formatters.CamelCasePropertyNameFormatter` (the default)
2. Kebab Case Formatter: :class:`py_serializable.formatters.KebabCasePropertyNameFormatter`
3. Snake Case Formatter: :class:`py_serializable.formatters.SnakeCasePropertyNameFormatter`

A summary of how these differ is included in the below table.

Expand All @@ -51,7 +51,7 @@ You can change the formatter being used by easily. The example below changes the

.. code-block:: python
from serializable.formatters import CurrentFormatter, SnakeCasePropertyNameFormatter
from py_serializable.formatters import CurrentFormatter, SnakeCasePropertyNameFormatter
CurrentFormatter.formatter = SnakeCasePropertyNameFormatter
Expand All @@ -60,4 +60,4 @@ Custom Formatters

If none of the included formatters work for you, why not write your own?

The only requirement is that it extends :class:`serializable.formatters.BaseNameFormatter`!
The only requirement is that it extends :class:`py_serializable.formatters.BaseNameFormatter`!
8 changes: 4 additions & 4 deletions docs/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ as follows:
self._chapters = list(chapters)
To make a class serializable to/from JSON or XML, the class must be annotated with the decorator
:func:`serializable.serializable_class`.
:func:`py_serializable.serializable_class`.

By simply modifying the classes above, we make them (de-)serializable with this library (albeit with some default
behaviour implied!).
Expand All @@ -88,9 +88,9 @@ This makes our classes:

.. code-block:: python
import serializable
import py_serializable
@serializable.serializable_class
@py_serializable.serializable_class
class Chapter:
def __init__(self, *, number: int, title: str) -> None:
Expand All @@ -105,7 +105,7 @@ This makes our classes:
def title(self) -> str:
return self._title
@serializable.serializable_class
@py_serializable.serializable_class
class Book:
def __init__(self, *, title: str, isbn: str, edition: int, publish_date: date, authors: Iterable[str],
Expand Down
Loading

0 comments on commit 39bfc1a

Please sign in to comment.