Data Type Tools
Includes useful helper methods for Python's immutable data types using the forbiddenfruit library.
Install
python3 -m pip install datatype-tools
Usage
- Import single datatype
from datatype_tools.lib import Float
- Import all datatypes
from datatype_tools.lib import *
Dict Tools
- sort_by_val
- Sorts dictionary by values, expects dictionary with a depth of 1
>>> obj = {'a': 3, 'b': 1, 'c': 2} >>> obj.sort_by_val(sort_type='int') {'b': 1, 'c': 2, 'a': 3}
- Arguments
- self: dictionary
- asc: boolean (True)
- Ascending order when True, Descending when False
- sort_type: string ('string')
- Data Type for sort, includes 'string', 'int', 'float', and 'date' types
- Date Type is a string representing a date
- Data does not need to match type i.e. a string with an int value '1'
- Returns original data type
- date_input: string ('mdy')
- Date input format
- i.e. 'ymd', 'mdy', etc.
- Returns sorted dict
- Sorts dictionary by values, expects dictionary with a depth of 1
- sort_by_key
- Sorts dictionary by keys, expects dictionary with a depth of 1
>>> obj = {'c': 1, 'a': 3, 'b': 2} >>> obj.sort_by_key(sort_type='string') {'a': 3, 'b': 2, 'c': 1}
- Arguments
- self: dictionary
- asc: boolean (True)
- Ascending order when True, Descending when False
- sort_type: string ('string')
- Data Type for sort, includes 'string', 'int', 'float', and 'date' types
- Date Type is a string representing a date
- Data does not need to match type i.e. a string with an int value '1'
- Returns original data type
- date_input: string ('mdy')
- Date input format
- i.e. 'ymd', 'mdy', etc.
- Returns sorted dict
- Sorts dictionary by keys, expects dictionary with a depth of 1
- nested_sort
- Sorts dictionary by nested key value
>>> obj = {'a': {'b': {'c': 2}}, 'b': {'b': {'c': 1}}} >>> obj.nested_sort(nested_args=[{'keys': ['b', 'c'], 'sort_type': 'int'}]) {'b': {'b': {'c': 1}}, 'a': {'b': {'c': 2}}}
- Arguments
- self: dictionary
- asc: boolean (True)
- Ascending order when True, Descending when False
- nested_args: list
- List of sort argument dicts, can include multiple to sort by multiple nested keys respectively
- Args
- keys: list
- list of nested keys respectively i.e. ['b', 'c', 'd'] will sort by key ['b']['c']['d']
- sort_type: string ('string')
- Data Type for sort, includes 'string', 'int', 'float', and 'date' types
- Date Type is a string representing a date
- Data does not need to match type i.e. a string with an int value '1'
- Returns original data type
- date_input: string ('mdy')
- Date input format
- i.e. 'ymd', 'mdy', etc.
- keys: list
- Returns sorted dict
- Sorts dictionary by nested key value
List Tools
- sort_by_val
- Sorts list of dictionaries by values, expects dictionaries with a depth of 1
>>> li = [{'a': 3}, {'b': 1}, {'c': 2}] >>> li.sort_by_val(sort_type='int') [{'b': 1}, {'c': 2}, {'a': 3}]
- Arguments
- self: list
- asc: boolean (True)
- Ascending order when True, Descending when False
- sort_type: string ('string')
- Data Type for sort, includes 'string', 'int', 'float', and 'date' types
- Date Type is a string representing a date
- Data does not need to match type i.e. a string with an int value '1'
- Returns original data type
- date_input: string ('mdy')
- Date input format
- i.e. 'ymd', 'mdy', etc.
- Returns sorted list
- Sorts list of dictionaries by values, expects dictionaries with a depth of 1
- sort_by_key
- Sorts list of dictionaries by keys, expects dictionaries with a depth of 1
>>> li = [{'c': 1}, {'a': 3}, {'b': 2}] >>> li.sort_by_key(sort_type='string') [{'a': 3}, {'b': 2}, {'c': 1}]
- Arguments
- self: list
- asc: boolean (True)
- Ascending order when True, Descending when False
- sort_type: string ('string')
- Data Type for sort, includes 'string', 'int', 'float', and 'date' types
- Date Type is a string representing a date
- Data does not need to match type i.e. a string with an int value '1'
- Returns original data type
- date_input: string ('mdy')
- Date input format
- i.e. 'ymd', 'mdy', etc.
- Returns sorted list
- Sorts list of dictionaries by keys, expects dictionaries with a depth of 1
- sort_by_key_val
- Sorts list of dictionaries by specific key value, expects dictionaries with a depth of 1
>>> li = [{'a': 1, 'b': 2}, {'a': 2, 'b': 1}] >>> li.sort_by_key_val(key='b', sort_type='string') [{'a': 2, 'b': 1}, {'a': 1, 'b': 2}]
- Arguments
- self: list
- asc: boolean (True)
- Ascending order when True, Descending when False
- sort_type: string ('string')
- Data Type for sort, includes 'string', 'int', 'float', and 'date' types
- Date Type is a string representing a date
- Data does not need to match type i.e. a string with an int value '1'
- Returns original data type
- date_input: string ('mdy')
- Date input format
- i.e. 'ymd', 'mdy', etc.
- Returns sorted list
- Sorts list of dictionaries by specific key value, expects dictionaries with a depth of 1
- nested_sort
- Sorts list of dictionaries by nested key value
>>> li = [{'a': {'b': {'c': 2}}}, {'b': {'b': {'c': 1}}}] >>> li.nested_sort(nested_args=[{'keys': ['b', 'c'], 'sort_type': 'int'}]) [{'b': {'b': {'c': 1}}}, {'a': {'b': {'c': 2}}}]
- Arguments
- self: list
- asc: boolean (True)
- Ascending order when True, Descending when False
- nested_args: list
- List of sort argument dicts, can include multiple to sort by multiple nested keys respectively
- Args
- keys: list
- list of nested keys respectively i.e. ['b', 'c', 'd'] will sort by key ['b']['c']['d']
- sort_type: string ('string')
- Data Type for sort, includes 'string', 'int', 'float', and 'date' types
- Date Type is a string representing a date
- Data does not need to match type i.e. a string with an int value '1'
- Returns original data type
- date_input: string ('mdy')
- Date input format
- i.e. 'ymd', 'mdy', etc.
- keys: list
- Returns sorted list
- Sorts list of dictionaries by nested key value
String Tools
- replace_multiple
- Replace multiple substrings or characters in a string with their respective replacements
>>> 'The Apple is Red'.replace_multiple({'Apple': 'Grass', 'Red': 'Green'}) 'The Grass is Green'
- Arguments
- self: string
- dictionary: dict
- dict keys are the values to replace, and key values will be their respective replacements in the string
- Returns string with replacements
- Replace multiple substrings or characters in a string with their respective replacements
- format_date
- Formats string representing date in multiple formats
>>> '1/01/75'.format_date(date_input='mdy', date_format='mmddyyyy', delimiter='-') '01-01-1975'
- Arguments
- self: string
- date_input: string ('mdy')
- date input order, i.e. 'mdy' for month, day, year, and 'ymd' for year, month, day
- date_format string ('mmddyy')
- date output format, the number of each letter correspond to date digits, i.e. a date with 'mmddyyyy' will have 4 digits for year while a date with 'mmddyy' will only have 2 digits for year
- delimiter: string ('')
- delimiter for date output, i.e. '/' yields 01/01/2019
- Returns formatted date string
- Formats string representing date in multiple formats
- find_nth
- Find nth occurence of substring in string
>>> 'apple picking'.find_nth('p', 3) 11
- Arguments
- self: string
- string: string
- substring to search in string
- n: int
- 1-based occurence number of substring to find in string.
- Returns int index
- Find nth occurence of substring in string
- b64_enc
- Base64 encode string
>>> 'plain_text'.b64_enc() 'cGxhaW5fdGV4dA=='
- Arguments
- self: string
- Returns string
- b64_dec
- Base64 decode string
>>> 'cGxhaW5fdGV4dA=='.b64_dec() 'plain_text'
- Arguments
- self: string
- Returns string
- get_iso_date
- Get ISO Date from string
>>> '2019-05-27T19:09:04.285211Z'.get_iso_date() {'date': '05/27/2019', 'time': '19:09:04', 'micros': '285211', 'date_and_time': '05/27/2019-19:09:04', 'full_date': '05/27/2019-19:09:04.285211'}
- Arguments
- self: string
- iso_format: string, ISO datetime format for strftime
- date_format: string, date output format
- delimiter: string, date output delimiter
- Returns dict
Float Tools
- round
- Round value to n decimal places (scientific rounding, fixes python rounding errors)
>>> 4.055.round(2) 4.06 >>> 4.054(2) 4.05
- Arguments
- self: float
- places: int (2)
- Number of places to round to
- Returns rounded float
- Round value to n decimal places (scientific rounding, fixes python rounding errors)
Metadata
Release files for datatype-tools 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| datatype-tools-0.1.3.tar.gz | 7.3 kB | Without provenance | Details for datatype-tools-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| datatype_tools-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for datatype_tools-0.1.3-py3-none-any.whl |
Total release size: 18.5 kB
Release files / datatype-tools-0.1.3.tar.gz
| Download URL | datatype-tools-0.1.3.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c8430897697a1aa142702b21c3203a0eb7eb9dec3137f671eed9bbaf871c1009
|
|
BLAKE2b-256 checksum How to use checksums |
4fdc8003090cd47d7289c50ded37fd59da4598edf3d056dae4120318ded819f7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8
|
Release files / datatype_tools-0.1.3-py3-none-any.whl
| Download URL | datatype_tools-0.1.3-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f16d4fa6d89964a0b4765506cbed04d40c444eebb3e0548b5ef854bcce9f5c4e
|
|
BLAKE2b-256 checksum How to use checksums |
7d0146e1b7729076e7dd822fc313becd16abcdfa5cb5fc08876fb1f96b8ae73e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8
|
Security for datatype-tools 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.