Skip to main content

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

Project description

OwlMixin

travis coverage complexity license pypi versions

(゚∀゚) v5.0 have breaking changes

  • TIterator#group_by
    • Return TDict[TList[T]] instead of TDict[TIterator[T]]

(゚∀゚) v4.0 have breaking changes

  • OwlMixin
    • Must use keyword arguments in from_XXX and to_XXX except for some ones
    • from_csvf -> from_csvf_to_list
  • TList
    • head -> take
    • partial -> partition (switch left and right)
  • transformers.XXX
    • Must use keyword arguments in
      • to_dict
      • to_dicts
      • to_json
      • to_jsonf
      • to_yaml
      • to_yamlf
      • to_csv
      • to_csvf

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

API Reference

https://tadashi-aikawa.github.io/owlmixin/

Example

from owlmixin import OwlMixin, OwlEnum, TOption, 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"}
    ]
})

Then...

>>> 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.

Below sample code uses kachayev/fn.py, great package!!

from owlmixin import OwlMixin, TOption, TIterator

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: TIterator[Repository]

Then...

>>> print(
...     GithubRepository
...         .from_json_url("https://api.github.com/search/repositories?q=git")
...         .items
...         .filter(_.stargazers_count > 100)
...         .order_by(_.stargazers_count, True)
...         .take(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,84643,gitignore,A collection of useful .gitignore templates
2,30456,gogs,Gogs is a painless self-hosted Git service.
3,29908,git-flight-rules,Flight rules for git
4,27704,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.
5,15541,tips,Most commonly used git tips and tricks.

Don't you think smart?

For developer

Requires pipenv and make.

Commands

# Create env
$ make init

# Build documentation and run server locally
$ make serve-docs

Version up

Windows is not supported!!!

Before release, you need to

  1. Confirm that your branch name equals release version

Then

$ make release

Project details


Release history Release notifications

This version

5.1.0

Download files

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

Files for owlmixin, version 5.1.0
Filename, size File type Python version Upload date Hashes
Filename, size owlmixin-5.1.0-py3-none-any.whl (20.4 kB) File type Wheel Python version py3 Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page