A General Use Python Tookit.
Project description
PyToolkit
Python General tools.
Table of Contents:
Installation
Source: https://pypi.org/project/pytoolkit928/
>>> python -m pip install pytoolkit928
Utilities
NOTE: There are 2 utils files that will be fixed in later releases. Currently pyoolkit.utils holds basic utilities and pytoolkit.utilities holds primarily dataclass and dict reformat utilities.
Manipulating Strings
String to List
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']
Camel and Snake Cases
Camel_to_Snake Case:
Converts camelCase into snake_case values. This funcition uses regular expressions to also handle special characters found in some non-standard formats.
>>> from pytoolkit.utils import camel_to_snake
>>> camel_to_snake(name='someValue.1')
'some_value1'
# To use a list of values and return them use `output='list'`
>>> camel_to_snake(name=['someValue.1', 'someValue', 'nextValue', 'lastTimeModifiedValue'], output='list')
['some_value1', 'some_value', 'next_value', 'last_time_modified_value']
Snake_to_Camel Case:
Converts snake_case values into camelCase.
>>> from pytoolkit.utils import snake_to_camel
>>> snake_to_camel(name='last_time_modified_value')
'lastTimeModifiedValue
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.
Sanatize dictionary data
from pytoolkit.utils import sanatize_data
test_dict = {
"value1": "one",
"value2": "two",
"subvalue01": {
"password": "welcome123",
"username": "testuser",
}
}
sanatized_dict = sanatize_data(data=test_dict)
print(sanatize_dict)
# {"value1": "one", "value2": "two", "subvalue01": { "password": "[MASKED]", "username": "testuser"}}
Search Utilities
extract_matches function allows you to extract matches and non matches from a list of strings using a conditional function.
Sample Code:
from datetime import datetime, timezone, timedelta
from pytoolkit.utilities import extract_matches
# This function searches for data that is extracted from a file into a list and does a compare of the datetime to match values in a dataframe that are older than a certain time.
m = extract_matches(iterable=m.matches, condition=lambda x: [bool(datetime.strptime(str(x.stem).split('_', maxsplit=1)[-1],'%Y%m%dT%H%M') < datetime.now(timezone.utc) - timedelta(days=days))])
The aove code passes a lambda function that allows you to create a single or list of conditions that each line is used to search and return a match and non-match dataclass which can be exported using:
m.match
m.no_match
Files
Reads different file types:
- yaml
- json
- csv
- text
- ini
Read in files and manipulate them using standard functions and pandas library.
from pytoolkit.files import readfile
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytoolkit928-1.0.1.tar.gz.
File metadata
- Download URL: pytoolkit928-1.0.1.tar.gz
- Upload date:
- Size: 63.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1806c8ad438be202dbe1a862dac58f8a0723dc94c3c03afd84b79776091b83ee
|
|
| MD5 |
3f573163b2158e2e64aac4f9a33570dc
|
|
| BLAKE2b-256 |
4a93ad2e4f0838f35ff2f0040b45cb5478cc1a0a9d8bca58bf63633c14cce483
|
File details
Details for the file pytoolkit928-1.0.1-py3-none-any.whl.
File metadata
- Download URL: pytoolkit928-1.0.1-py3-none-any.whl
- Upload date:
- Size: 53.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15df4aa8252a89810562164ac871382df93b5ff665513c38fae1ee4c7ffdc09c
|
|
| MD5 |
15e63614cf2368a70531d55694e4d997
|
|
| BLAKE2b-256 |
c85de58d142e6496dd77c197d60af97a875e53e6de5aabf91b3d45993a3accb1
|