teklia-toolbox


Nameteklia-toolbox JSON
Version 0.1.8 PyPI version JSON
download
home_pageNone
SummaryNone
upload_time2025-02-11 10:01:29
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseNone
keywords python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Python Toolbox

Python toolbox that provides a timer and a configuration parser.

## Installation

To install this module run:
```
pip install teklia-toolbox
```

## Timer

Wrapper that calculates the execution time of instructions. This information is stored in the `delta` attribute which is of type [datetime.timedelta](https://docs.python.org/3/library/datetime.html#available-types).

```python
from teklia_toolbox.time import Timer

with Timer() as t:
    # Some code
    pass
print(f'These instructions took {t.delta}')
```

## Configuration parser

### ConfigParser

The `ConfigParser` class allows to instantiate a parser. It takes as argument:
- a boolean `allow_extra_keys` to specify if the parser should ignore extra unspecified keys instead of causing errors (default to `True`)

#### Add option

The `add_option` function allows to add parameter to the parser. It takes as argument:
- a parameter `name`
- a parameter `type` (which must be [callable](https://docs.python.org/3/library/functions.html#callable)) (default to `str`)
- a `many` boolean to specify if the parameter can have a list of values (default to `False`)
- a `default` value (default to `object()`)

#### Add subparser

The `add_subparser` function adds a parser as a new option to the initial parser, to allow finer control over nested configuration options. It takes the same arguments as the `ConfigParser` class and the `add_option` function.

#### Parse data

The `parse_data` function parses configuration data from a dict. It will raise `ConfigurationError` if any error is detected. Otherwise it returns a dictionary. It takes as argument:
- `data` of type [Mapping](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping)

#### Parse

The `parse` function parses configuration data from a yaml file. It will raise `ConfigurationError` if any error is detected. Otherwise it returns a dictionary. It takes as argument:
- a `path` to the yaml file
- a boolean `exist_ok` to specify if the parser should ignore a non-existing file (default to `False`)

```python
from teklia_toolbox.config import ConfigParser

parser = ConfigParser()
parser.add_option('names', type=str, many=True, default=[])
parser.add_option('pseudo', type=str) # Required
parser.add_option('age', type=int, default=21)

parents_parser = parser.add_subparser('parents', default={})

mother_parser = parents_parser.add_subparser('mother', default={})
mother_parser.add_option('name', type=str, default=None)
mother_parser.add_option('age', type=int, default=None)

father_parser = parents_parser.add_subparser('father', default={})
father_parser.add_option('name', type=str, default=None)
father_parser.add_option('age', type=int, default=None)

# This will return
# {
#     'names': ['Pierre', 'Dupont'],
#     'pseudo': 'BoumBoum',
#     'age': 21,
#     'parents': {
#         'mother': {
#             'name': 'Marie',
#             'age': None
#         },
#         'father': {
#             'name': None,
#             'age': None
#         }
#     }
# }
parser.parse_data({
    'names': ['Pierre', 'Dupont'],
    'pseudo': 'BoumBoum',
    'parents': {
        'mother': {
            'name' : 'Marie'
        }
    }
})
```

### ConfigurationError

The `ConfigurationError` class inherits from the [ValueError](https://docs.python.org/3/library/exceptions.html#ValueError) class. This type of error is raised if the parser finds errors during parsing.

```python
from teklia_toolbox.config import ConfigurationError

raise ConfigurationError("Oops..")
```

### dir_path and file_path

The `dir_path` and `file_path` functions allow you to easily add path or file parameters to the parser.

```python
from teklia_toolbox.config import ConfigParser
from teklia_toolbox.config import dir_path, file_path

parser = ConfigParser()
parser.add_option('root_path', type=dir_path, default=None)
parser.add_option('csv_file', type=file_path, default=None)

# This will return
# {
#   'root_path': PosixPath('/sweet/home'),
#   'csv_file': PosixPath('/coucou.csv')
# }
parser.parse_data({
    'root_path': '/sweet/home/',
    'csv_file': './coucou.csv'
})
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "teklia-toolbox",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "Teklia <contact@teklia.com>",
    "keywords": "python",
    "author": null,
    "author_email": "Teklia <contact@teklia.com>",
    "download_url": "https://files.pythonhosted.org/packages/92/06/5c8c0a134baf9b2820b8fdda219d00416b06b6bebffd1f603fcfad847deb/teklia_toolbox-0.1.8.tar.gz",
    "platform": null,
    "description": "# Python Toolbox\n\nPython toolbox that provides a timer and a configuration parser.\n\n## Installation\n\nTo install this module run:\n```\npip install teklia-toolbox\n```\n\n## Timer\n\nWrapper that calculates the execution time of instructions. This information is stored in the `delta` attribute which is of type [datetime.timedelta](https://docs.python.org/3/library/datetime.html#available-types).\n\n```python\nfrom teklia_toolbox.time import Timer\n\nwith Timer() as t:\n    # Some code\n    pass\nprint(f'These instructions took {t.delta}')\n```\n\n## Configuration parser\n\n### ConfigParser\n\nThe `ConfigParser` class allows to instantiate a parser. It takes as argument:\n- a boolean `allow_extra_keys` to specify if the parser should ignore extra unspecified keys instead of causing errors (default to `True`)\n\n#### Add option\n\nThe `add_option` function allows to add parameter to the parser. It takes as argument:\n- a parameter `name`\n- a parameter `type` (which must be [callable](https://docs.python.org/3/library/functions.html#callable)) (default to `str`)\n- a `many` boolean to specify if the parameter can have a list of values (default to `False`)\n- a `default` value (default to `object()`)\n\n#### Add subparser\n\nThe `add_subparser` function adds a parser as a new option to the initial parser, to allow finer control over nested configuration options. It takes the same arguments as the `ConfigParser` class and the `add_option` function.\n\n#### Parse data\n\nThe `parse_data` function parses configuration data from a dict. It will raise `ConfigurationError` if any error is detected. Otherwise it returns a dictionary. It takes as argument:\n- `data` of type [Mapping](https://docs.python.org/3/library/collections.abc.html#collections.abc.Mapping)\n\n#### Parse\n\nThe `parse` function parses configuration data from a yaml file. It will raise `ConfigurationError` if any error is detected. Otherwise it returns a dictionary. It takes as argument:\n- a `path` to the yaml file\n- a boolean `exist_ok` to specify if the parser should ignore a non-existing file (default to `False`)\n\n```python\nfrom teklia_toolbox.config import ConfigParser\n\nparser = ConfigParser()\nparser.add_option('names', type=str, many=True, default=[])\nparser.add_option('pseudo', type=str) # Required\nparser.add_option('age', type=int, default=21)\n\nparents_parser = parser.add_subparser('parents', default={})\n\nmother_parser = parents_parser.add_subparser('mother', default={})\nmother_parser.add_option('name', type=str, default=None)\nmother_parser.add_option('age', type=int, default=None)\n\nfather_parser = parents_parser.add_subparser('father', default={})\nfather_parser.add_option('name', type=str, default=None)\nfather_parser.add_option('age', type=int, default=None)\n\n# This will return\n# {\n#     'names': ['Pierre', 'Dupont'],\n#     'pseudo': 'BoumBoum',\n#     'age': 21,\n#     'parents': {\n#         'mother': {\n#             'name': 'Marie',\n#             'age': None\n#         },\n#         'father': {\n#             'name': None,\n#             'age': None\n#         }\n#     }\n# }\nparser.parse_data({\n    'names': ['Pierre', 'Dupont'],\n    'pseudo': 'BoumBoum',\n    'parents': {\n        'mother': {\n            'name' : 'Marie'\n        }\n    }\n})\n```\n\n### ConfigurationError\n\nThe `ConfigurationError` class inherits from the [ValueError](https://docs.python.org/3/library/exceptions.html#ValueError) class. This type of error is raised if the parser finds errors during parsing.\n\n```python\nfrom teklia_toolbox.config import ConfigurationError\n\nraise ConfigurationError(\"Oops..\")\n```\n\n### dir_path and file_path\n\nThe `dir_path` and `file_path` functions allow you to easily add path or file parameters to the parser.\n\n```python\nfrom teklia_toolbox.config import ConfigParser\nfrom teklia_toolbox.config import dir_path, file_path\n\nparser = ConfigParser()\nparser.add_option('root_path', type=dir_path, default=None)\nparser.add_option('csv_file', type=file_path, default=None)\n\n# This will return\n# {\n#   'root_path': PosixPath('/sweet/home'),\n#   'csv_file': PosixPath('/coucou.csv')\n# }\nparser.parse_data({\n    'root_path': '/sweet/home/',\n    'csv_file': './coucou.csv'\n})\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": null,
    "version": "0.1.8",
    "project_urls": {
        "Authors": "https://teklia.com",
        "Bug Tracker": "https://gitlab.teklia.com/tools/python-toolbox/issues",
        "Repository": "https://gitlab.teklia.com/tools/python-toolbox"
    },
    "split_keywords": [
        "python"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "05010bebec02b517aac26379ff5a015ca9baed57e60958388a5e4a97e4294fc1",
                "md5": "9802436052d602d18f4c20a8c6bf256b",
                "sha256": "671cc16205af3887f5503886af0f6a14928a77e045f31863ea33918769c67bb4"
            },
            "downloads": -1,
            "filename": "teklia_toolbox-0.1.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9802436052d602d18f4c20a8c6bf256b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 12323,
            "upload_time": "2025-02-11T10:01:25",
            "upload_time_iso_8601": "2025-02-11T10:01:25.638352Z",
            "url": "https://files.pythonhosted.org/packages/05/01/0bebec02b517aac26379ff5a015ca9baed57e60958388a5e4a97e4294fc1/teklia_toolbox-0.1.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "92065c8c0a134baf9b2820b8fdda219d00416b06b6bebffd1f603fcfad847deb",
                "md5": "589ec3eadb2caa7220c0967aed5aea11",
                "sha256": "13b491ee4724bd9ec23b7784af4f6c8c701a08a44b2963ec76fbb2632d0456f8"
            },
            "downloads": -1,
            "filename": "teklia_toolbox-0.1.8.tar.gz",
            "has_sig": false,
            "md5_digest": "589ec3eadb2caa7220c0967aed5aea11",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 12219,
            "upload_time": "2025-02-11T10:01:29",
            "upload_time_iso_8601": "2025-02-11T10:01:29.059309Z",
            "url": "https://files.pythonhosted.org/packages/92/06/5c8c0a134baf9b2820b8fdda219d00416b06b6bebffd1f603fcfad847deb/teklia_toolbox-0.1.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-11 10:01:29",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "teklia-toolbox"
}
        
Elapsed time: 0.55668s