Skip to main content

Mixin which converts ``data class instance`` and others each other more simple.

Project description

pypi Build Status Test Coverage Code Climate versions license

Mixin which converts data class instance and others each other more simple.

Motivation

Have you ever wanted to write robust code in Python? This library will make such your wishes come true.

Define your data class which is extend OwlMixin, you can use some useful methods which help your codes robust. See following Example and API sections.

Example

You don’t need to use typing necessarily, but I recommend to use this to make your codes more robust. See PEP 484 – Type Hints.

from typing import Text, Optional
from owlmixin import OwlMixin, TList

class Food(OwlMixin):
    def __init__(self, id, name, color=None):
        self.id = id  # type: Text
        self.name = name  # type: Text
        self.color = color  # type: Optional[Text]

class Human(OwlMixin):
    def __init__(self, id, name, favorite):
        self.id = id  # type: Text
        self.name = name  # type: Text
        self.favorite = Food.from_dicts(favorite)  # type: TList[Food]

jiro = Human.from_dict({
    "id": 10,
    "name": "jiro",
    "favorite": [
        {"id": 1, "name": "apple"},
        {"id": 2, "name": "orange", "color": "white"}
    ]
})

>>> jiro.id
10
>>> jiro.name
'jiro'
>>> print(jiro.favorite[0].to_pretty_json())
{
    "id": 1,
    "name": "apple"
}
>>> print(jiro.to_dict(ignore_none=False))
{'favorite': [{'id': 1, 'name': 'apple'}, {'color': 'white', 'id': 2, 'name': 'orange'}], 'id': 10, 'name': 'jiro'}
>>> print(jiro.to_dict())
{'favorite': [{'color': None, 'id': 1, 'name': 'apple'}, {'color': 'white', 'id': 2, 'name': 'orange'}], 'id': 10, 'name': 'jiro'}

You can also use methods chains as following.

from typing import Optional
from owlmixin import OwlMixin, TList

# `**extra` is necessary to allow extra elements.
# Note that you must define all properties in github response json if you don't use `**extra`
class Repository(OwlMixin):
    def __init__(self, id, name, description, stargazers_count, **extra):
        self.id = id  # type: int
        self.name = name  # type: Text
        self.description = description  # type: Optional[Text]
        self.star_count = stargazers_count  # type: int

class GithubRepository(OwlMixin):
    def __init__(self, total_count, incomplete_results, items):
        self.total_count = total_count  # type: int
        self.incomplete_results = incomplete_results  # type: bool
        self.repositories = Repository.from_dicts(items)  # type: TList[Repository]

>>> r = GithubRepository \
...         .from_json_url("https://api.github.com/search/repositories?q=git") \
...         .repositories \
...         .filter(lambda x: x.star_count > 100) \
...         .order_by(lambda x: x.star_count, True) \
...         .map(lambda x: {
...             "id": x.id,
...             "message": '★{0.star_count}   {0.name}'.format(x)
...         }) \
...         .to_csv(fieldnames=["id", "message"], with_header=True)
>>> print(r)
id,message
1062897,45252   gitignore
36502,15888   git
36560369,2931   my-git
18484639,212   git

API

From instance to another

  • to_dict
    • instance => dict

    • TDict[instance] => dict

  • to_dicts
    • TList[instance] => List[dict]

  • to_json
    • instance => json string

  • to_pretty_json
    • instance => json string (has indent and line break)

  • to_yaml
    • instance => yaml string

  • to_csv
    • TList[instance] => csv string

From something to instance

  • from_dict [1] [2]
    • dict => instance

  • from_optional_dict [1] [2]
    • Optional[dict] => Optional[instance]

  • from_dicts [1] [2]
    • List[dict] => TList[instance]

  • from_optional_dicts [1] [2]
    • Optional[List[dict]] => Optional[TList[instance]]

  • from_dicts_by_key [1] [2]
    • dict => TDict[instance]

  • from_optional_dicts_by_key [1] [2]
    • Optional[dict] => Optional[TDict[instance]]

  • from_json [1] [2]
    • json string => instance

  • from_jsonf [1] [2] [3]
    • json file path => instance

  • from_json_to_list [1] [2]
    • json string => TList[instance]

  • from_jsonf_to_list [1] [2] [3]
    • json file path => TList[instance]

  • from_json_url [1] [2]
    • url (which returns json) => instance

  • from_yaml [1] [2]
    • yaml string => instance

  • from_yamlf [1] [2] [3]
    • yaml file path => instance

  • from_yaml_to_list [1] [2]
    • yaml string => TList[instance]

  • from_yamlf_to_list [1] [2] [3]
    • yaml file path => TList[instance]

  • from_csvf [1] [2] [3]
    • csv file => TList[instance]

Installation

pip install owlmixin

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

owlmixin-1.0.0rc6-py2.py3-none-any.whl (9.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file owlmixin-1.0.0rc6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for owlmixin-1.0.0rc6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 86dce692b86d89a3e078efe3865a2926d4de258c40451d62c504393383eeb840
MD5 2e674fc1d02f2bd2ee42a836c9085a01
BLAKE2b-256 9c5d5dc5d56252216613884d7975515d2399311ca76df50d7cfaf3b42b8436f6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page