Giter Site home page Giter Site logo

danoan / python-project-model Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 2.81 MB

Functional python project with GitHub workflow

Home Page: https://danoan.github.io/python-project-model

License: MIT License

Shell 26.40% Python 36.44% Cython 37.17%

python-project-model's Introduction

โ— This repository is based on a copy of toml-dataclassv0.1. Its only purpose is to serve as a model of a functional python project with continuous integration set.

Getting started with toml_dataclass

Toml and Python Dataclass serialization.

Features

  • Preserve the in-memory representation of Python dataclasses

Examples

Creating a dataclass with support to toml serialization.

>>> from danoan.toml_dataclass import TomlDataClassIO
>>> from dataclasses import dataclass

>>> @dataclass
... class Plugin(TomlDataClassIO):
...    name: str
...    version: str
...    description: str


>>> jpg_plugin = Plugin("image-jpg", "1.0", "Conversion functions to jpg type.")
>>> with open("jpg-plugin.toml", "w") as fw:
...     jpg_plugin.write(fw)

Here it is what it looks like the written toml.

>>> with open("jpg-plugin.toml", "r") as fr:
...     print(fr.read())
name = "image-jpg"
version = "1.0"
description = "Conversion functions to jpg type."

Preserve the in-memory representation of Python dataclasses after reading.

>>> from pprint import pprint
>>> @dataclass
... class Configuration(TomlDataClassIO):
...     project_name: str
...     plugin: Plugin

>>> config = Configuration("image-reader", jpg_plugin)
>>> with open("config.toml", "w") as fw:
...     config.write(fw)

>>> with open("config.toml", "r") as fr:
...     config_from_file = Configuration.read(fr)
>>> pprint(config_from_file)
Configuration(project_name='image-reader',
              plugin=Plugin(name='image-jpg',
                            version='1.0',
                            description='Conversion functions to jpg type.'))

>>> pprint(f"Plugin name: {config_from_file.plugin.name}")
'Plugin name: image-jpg'

Notice how it differs from the ouptut of the toml library.

>>> import toml
>>> pprint(toml.load("config.toml"))
{'plugin': {'description': 'Conversion functions to jpg type.',
            'name': 'image-jpg',
            'version': '1.0'},
 'project_name': 'image-reader'}

Preserve in-memory representation with toml tables.

>>> from danoan.toml_dataclass import TomlTableDataClassIO
>>> from typing import List

>>> @dataclass
... class PluginTable(TomlTableDataClassIO):
...     list_of_plugins: List[Plugin]

>>> png_plugin = Plugin("image-png", "1.0", "Conversion functions to png type.")
>>> plugin_table = PluginTable([jpg_plugin, png_plugin])
>>> with open("plugin-table.toml", "w") as fw:
...     plugin_table.write(fw)

>>> with open("plugin-table.toml", "r") as fr:
...     plugin_table_from_file = PluginTable.read(fr)
>>> pprint(plugin_table_from_file)
PluginTable(list_of_plugins=[Plugin(name='image-jpg',
                                    version='1.0',
                                    description='Conversion functions to jpg '
                                                'type.'),
                             Plugin(name='image-png',
                                    version='1.0',
                                    description='Conversion functions to png '
                                                'type.')])

Notice how it differs from the ouptut of the toml library.

>>> pprint(toml.load("plugin-table.toml"))
{'list_of_plugins': [{'description': 'Conversion functions to jpg type.',
                      'name': 'image-jpg',
                      'version': '1.0'},
                     {'description': 'Conversion functions to png type.',
                      'name': 'image-png',
                      'version': '1.0'}]}

Here it is what looks like the written toml.

>>> with open("plugin-table.toml", "r") as f:
...     print(f.read())
[[list_of_plugins]]
name = "image-jpg"
version = "1.0"
description = "Conversion functions to jpg type."
<BLANKLINE>
[[list_of_plugins]]
name = "image-png"
version = "1.0"
description = "Conversion functions to png type."
<BLANKLINE>
<BLANKLINE>

python-project-model's People

Contributors

danoan avatar dependabot[bot] avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.