A General Use Python Tookit.
Project description
PyToolkit
Python General tools
Utilities
string_or_list
function allows you to interpret a string and return a list. Provides you the option of adding a delimeter using an OR function to return a possible string that you may be expecting possible commond delimeters. Such as: ",|:|\|, "
.
Example:
>>> from pytoolkit.utils import string_or_list
>>> test = 'string1,string2 string3|string4'
>>> string_or_list(test)
['string1,string2 string3|string4']
>>> string_or_list(test,delimeters=',| ')
['string1', 'string2', 'string3|string4']
>>> string_or_list(test,delimeters=',| |\|')
['string1', 'string2', 'string3', 'string4']
Dataclass Base
Used for basic extended functionality for dataclass declerations. Includes the ability to create a dataclass from a dictionary
or from **kwargs
. Also, includes a conversion from Dataclass
to a Python dictionary
.
Usage:
from typing import Optional
from dataclasses import dataclass
from pytoolkit.utilities import BaseMonitor, NONETYPE
@dataclass
class Sample(BaseMonitor):
key1: str
key2: str
key3: int
key5: Optional[str] = NONETYPE
# create a sample module
_dict = {"key1": "value1", "key2": "value2", "key3": 3}
sample1 = Sample.create_from_dict(_dict)
sample2 = Sample.create_from_kwargs(**_dict)
print(sample1)
print(sample2)
print(sample1.to_dict())
OUTPUT:
>>> print(sample1)
Sample(key1='value1', key2='value2', key3=3, key5=<object object at 0x10c8e8b70>)
>>> print(sample2)
Sample(key1='value1', key2='value2', key3=3, key5=<object object at 0x10c8e8b70>)
>>> print(sample1.to_dict())
{'key1': 'value1', 'key2': 'value2', 'key3': 3}
Maniuplating Dictionaries
Flatten a Dictionary:
import json
from pytoolkit import utilities
sample_dict = {"key1":"value","key2": "value2", "metadata": {"key1": "meta_value1","key2":"meta_value2"}}
# Convert dictionary into a flat dictionary
flat_dict = utilities.flatten_dict(sample_dict)
# Convert dictionary back into a nested ditionary
nest_dict = utilities.nested_dict(flat_dict)
print(f"This is a Flattened Dictionary:\n{json.dumps(flat_dict,indent=1)}")
print(f"This is a Nested Dictionary:\n{json.dumps(nest_dict,indent=1)}")
OUTPUT:
This is a Flattened Dictionary:
{
"key1": "value",
"key2": "value2",
"metadata.key1": "meta_value1",
"metadata.key2": "meta_value2"
}
This is a Nested Dictionary:
{
"key1": "value",
"key2": "value2",
"metadata": {
"key1": "meta_value1",
"key2": "meta_value2"
}
}
The above is using the default '.'
seperator value. There is a mix of commands that can be passed to adjust how the dictionary is expressed. This is useful for expressing data in otherformats that do not allow for nested dictionaries, but need a way to recreate the original formated datastructure.
Nested Dictionary:
TOOD: Create a way to extract a CSV or XCEL file and turn it into a proper dictionary based on the type. Integrate with Splunk
TODO: Add splunk HEC fromatter with proper chunck
TODO: KVSTORE configuration tool.
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 Distribution
Built Distribution
File details
Details for the file pytoolkit928-0.0.9.tar.gz
.
File metadata
- Download URL: pytoolkit928-0.0.9.tar.gz
- Upload date:
- Size: 51.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0ecc674a7d8e3e00ecd9c3824520cd616338ad7f96999838a46160f34996935 |
|
MD5 | 9c6cb6ef80df7f325ce32edcab5fce85 |
|
BLAKE2b-256 | d42b71f9143ec87bac30b72d3f23d44e8ecc4d7844b59a5de2b042833a3e643e |
File details
Details for the file pytoolkit928-0.0.9-py3-none-any.whl
.
File metadata
- Download URL: pytoolkit928-0.0.9-py3-none-any.whl
- Upload date:
- Size: 41.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e98c40e22c165c7dceb91789604a9697511ed1f785971de280c74a584c90c844 |
|
MD5 | 1d159cd0bf5ee2fc5b84a8b72d72bb07 |
|
BLAKE2b-256 | 8192daf197ab16e35f691c2c3f6a8575dd00cb07e12f81fa3d4ea9d91d78dfbf |