Skip to content

Commit

Permalink
Add method to serialize models
Browse files Browse the repository at this point in the history
  • Loading branch information
emreozcan committed Mar 21, 2021
1 parent 84da605 commit 67f3f92
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
24 changes: 17 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ Python API for the Turkish Language Foundation
tdk-py is a Python package that allows for simple access to `Turkish dictionaries`_ made available by the TDK_, the Turkish Language Society.
tdk-py aims to be easy to use and internally queries the TDK and parses its response into easy to use Python class objects.

.. _Turkish dictionaries: https://sozluk.gov.tr
.. _TDK: https://www.tdk.gov.tr
.. _Turkish dictionaries:
https://sozluk.gov.tr
.. _TDK:
https://www.tdk.gov.tr

Installation
============
Expand All @@ -19,7 +21,8 @@ tdk-py is supported on Python 3.6+. The recommended way to install is via *pip*

If your machine doesn't have Python and pip installed you can download it from `The Python Software Foundation's website`_.

.. _The Python Software Foundation's website: https://www.python.org/downloads/
.. _The Python Software Foundation's website:
https://www.python.org/downloads/

Sample usage
============
Expand Down Expand Up @@ -56,9 +59,15 @@ Let's see the distribution of entries by the number of maximum consecutive conso

>>> from tdk.tools import max_streak
>>> from tdk.alphabet import CONSONANTS
>>> annotated_list = [max_streak(word=x, targets=CONSONANTS) for x in tdk.gts.index()]
>>> for i in set(annotated_list):
... print(i, annotated_list.count(i))
>>> annotated_dict = {}
>>> for entry in tdk.gts.index():
... streaks = max_streak(entry)
... if streaks not in annotated_dict:
... annotated_dict[streaks] = [entry]
... else:
... annotated_dict[streaks].append(entry)
>>> for i in set(annotated_dict):
... print(i, len(annotated_dict[i]))
...
0 19
1 15199
Expand All @@ -73,4 +82,5 @@ tdk-py's source code is provided under the `MIT License`_.

Copyright © 2021 Emre Özcan

.. _MIT License: LICENSE
.. _MIT License:
https://github.com/EmreOzcan/tdk-py/blob/master/LICENSE
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="tdk-py",
version="1.0.0.post1",
version="1.1.0",
author="Emre Özcan",
author_email="[email protected]",
description="Python API for the Turkish Language Foundation",
Expand Down
16 changes: 16 additions & 0 deletions src/tdk/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from typing import List

from .classifications import OriginLanguage
Expand All @@ -8,6 +9,21 @@ class TdkModel:
def __eq__(self, other: object) -> bool:
return self.__dict__ == other.__dict__

def as_dict(self):
def serialize(obj):
if isinstance(obj, list):
if len(obj) == 0:
return []
return list(map(serialize, obj))
elif isinstance(obj, TdkModel):
return serialize(obj.as_dict())
elif isinstance(obj, MeaningProperty):
return serialize(obj.value.id)
elif isinstance(obj, Enum):
return serialize(obj.value)
return obj
return {k: serialize(v) for k, v in self.__dict__.items()}


class Writer(TdkModel):
tdk_id: int
Expand Down

0 comments on commit 67f3f92

Please sign in to comment.