Skip to main content

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

Project description

pypi Documentation Status 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 Reference sections.

Installation

pip install owlmixin

Example

from owlmixin import OwlMixin
from owlmixin.owlenum import OwlEnum
from owlmixin.owloption import TOption
from owlmixin.owlcollections import TList

class Color(OwlEnum):
    RED = "red"
    GREEN = "green"
    BLUE = "blue"

class Food(OwlMixin):
    id: int
    name: str
    color: TOption[Color]

class Human(OwlMixin):
    id: int
    name: str
    favorite: TList[Food]

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

>>> jiro.id
10
>>> jiro.name
'jiro'

>>> print(jiro.to_dict())
{'id': 10, 'name': 'jiro', 'favorite': [{'id': 1, 'name': 'apple'}, {'id': 2, 'name': 'orange', 'color': 'green'}]}

>>> print(jiro.favorite[0].to_pretty_json())
{
    "id": 1,
    "name": "apple"
}

>>> print(jiro.to_yaml())
favorite:
  - id: 1
    name: apple
  - color: green
    id: 2
    name: orange
id: 10
name: jiro

>>> print(jiro.favorite.to_csv(['id', 'name', 'color'], with_header=True))
id,name,color
1,apple,
2,orange,green

You can also use methods chains as following.

>>> from owlmixin.owloption import TOption
>>> from owlmixin.owlcollections import TList
>>>
>>> from fn import _
>>>
>>>
>>> class Repository(OwlMixin):
...     id: int
...     name: str
...     description: TOption[str]
...     stargazers_count: int
...
>>>
>>> class GithubRepository(OwlMixin):
...     total_count: int
...     incomplete_results: bool
...     items: TList[Repository]
...
>>>
>>> print(
...     GithubRepository \
...         .from_json_url("https://api.github.com/search/repositories?q=git") \
...         .items \
...         .filter(_.stargazers_count > 100) \
...         .order_by(_.stargazers_count, True) \
...         .head(5) \
...         .emap(lambda v, i: {
...             'RANK': i+1,
...             'STAR': v.stargazers_count,
...             'NAME': v.name,
...             'DESCRIPTION': v.description
...         }) \
...         .to_csv(fieldnames=["RANK", "STAR", "NAME", "DESCRIPTION"], with_header=True)
... )

RANK,STAR,NAME,DESCRIPTION
1,50787,gitignore,A collection of useful .gitignore templates
2,19308,gogs,Gogs is a painless self-hosted Git service.
3,17750,git,Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
4,10744,hub,hub helps you win at git.
5,10338,tips,Most commonly used git tips and tricks.

Don’t you think smart?

API Reference

  • Documentation Status for master

  • Documentation Status for current stable version

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-2.0.0a1-py2.py3-none-any.whl (16.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file owlmixin-2.0.0a1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for owlmixin-2.0.0a1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b74e5a1a8c7808a1b56693ff70e47a914c00fefa56bec08701c21668a355bfc5
MD5 55dbd3ecc6b875052fe4ea37ecb6e247
BLAKE2b-256 9937d81d9d433da1ab8a287dbc881377699b3cdbbfad014ccf58146cbdf4c77d

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