Name | jacktrade JSON |
Version |
0.9.1
JSON |
| download |
home_page | |
Summary | A collection of commonly used Python utilities. |
upload_time | 2023-10-25 06:47:34 |
maintainer | |
docs_url | None |
author | |
requires_python | >=3.10 |
license | MIT License |
keywords |
utils
utilities
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
|
# jacktrade
[![test](https://github.com/mzaja/jacktrade/actions/workflows/test.yml/badge.svg)](https://github.com/mzaja/jacktrade/actions/workflows/test.yml) [![Coverage Status](https://coveralls.io/repos/github/mzaja/jacktrade/badge.svg?branch=main)](https://coveralls.io/github/mzaja/jacktrade?branch=main) ![PyPI version](https://img.shields.io/pypi/v/jacktrade) ![Python version](https://img.shields.io/pypi/pyversions/jacktrade) ![License](https://img.shields.io/github/license/mzaja/jacktrade)
**Jack of all trades, master of none** - a collection of commonly used Python utilities. Install using:
```
pip install jacktrade
```
The package consists of the following submodules:
- [Benchmark](#benchmark)
- [Buffers](#buffers)
- [Collections](#collections)
- [Files](#files)
- [Multicore](#multicore)
- [Pickler](#pickler)
- [Sysenv](#sysenv)
## Benchmark
Contains a `CodeTimer` class which is used to elegantly and precisely time a piece of code:
```py
from jacktrade import CodeTimer
from time import sleep
with CodeTimer() as ct:
# Enter code to time here
sleep(0.1) # Simulates a piece of code
# Prints: "Code execution took 100 ms."
(ct.ns, ct.us, ct.ms, ct.s) # Access code duration in nano/micro/milli/seconds.
```
A `CodeTimer` instance can also be used as a decorator and store results of repeated function calls inside the list provided as `results` argument:
```py
from jacktrade import CodeTimer
from time import sleep
results = [] # A list for storing timing results
@CodeTimer(no_print=True, results=results)
def sleep_ms(milliseconds: int):
sleep(milliseconds / 1000)
sleep_ms(100) # Call wrapped function
sleep_ms(200) # Call again
print(int(results[0].ms)) # Prints: "100"
print(int(results[1].ms)) # Prints: "200"
```
## Buffers
Contains a `StringBuffers` class, whose purpose is to reduce the number of I/O operations
when writing to files. By speficying `buffer_size` parameter, the contents of the buffer
are automatically flushed to disk when the buffer fills up. The class handles any number
of simultaneously "open" files.
```py
from jacktrade import StringBuffers
output_file = "out.txt"
buffers = StringBuffers(output_dir="text", buffer_size=3)
buffers.add(output_file, "Hello") # Nothing is written out
buffers.add(output_file, " world") # Nothing is written out
buffers.add(output_file, "!") # "Hello world!" is written to ./text/out.txt
```
## Collections
Contains utility functions for working with collections, namely dictionaries and iterables. Usage examples include:
```py
from jacktrade import *
# Dict utilities
dict_data = {"a": 1, "b": {"c": 2}}
flatten_dict(dict_data) # Returns: [1, 2]
get_first_dict_item(dict_data) # Returns: ("a", 1)
get_first_dict_key(dict_data) # Returns: "a"
get_first_dict_value(dict_data) # Returns: 1
# Iterable utilities
list_data = [1, 2, [3, 4], 5, 6]
flatten_list(list_data) # Returns: [1, 2, 3, 4, 5, 6]
chunkify(list_data, chunk_size=2) # Yields: [1, 2], [[3, 4], 5], [6]
limit_iterator(list_data, limit=3) # Yields: 1, 2, [3, 4]
```
`MasterDict` is a wrapper class holding multiple dictionaries. It provides methods for simultaneously deleting keys from all underlying dictionaries, as well as clearing them. It is intended to hold caches and reliably empty them with a single method call.
```py
from jacktrade import MasterDict
dicts = MasterDict(a={1: "1", 2: "2"}, b={1: "1", 3: "3"}, c={4: "4"})
dicts.delete_keys(1, 3) # Delete keys 1 and 3 from all dictionaries
dicts.as_dict() # Returns: {'a': {2: '2'}, 'b': {}, 'c': {4: '4'}}
dicts.clear_all() # Clear all dictionaries
dicts.as_dict() # Returns: {'a': {}, 'b': {}, 'c': {}}
```
`BaseMapping` is a generic base class used to create `dict` subclasses which automatically map keys to values from a collection of objects of the same type. It is used like so:
```py
from jacktrade import BaseMapping
class NameAgeLookup(BaseMapping):
"""
Maps a person's name to its age if the person is over 18 years old.
"""
def __init__(self, persons):
super().__init__(
items=persons,
key_getter=lambda p: p["name"],
value_getter=lambda p: p["age"],
condition=lambda p: p["age"] > 18,
)
mapping = NameAgeLookup(
[
{"name": "Jack", "age": 15},
{"name": "Mike", "age": 27},
{"name": "Pete", "age": 39},
]
)
# Assertions pass
assert mapping == {"Mike": 27, "Pete": 39}
assert mapping.invert() == {27: "Mike", 39: "Pete"}
```
`Permutations` class is used for parametrisation, returning all possible combinations of input parameters:
```py
from jacktrade import Permutations
p = Permutations(a=[1, 2, 3], b=["A", "B"])
p.args # Returns: [(1, "A"), (1, "B"), (2, "A"), (2, "B"), ...]
p.kwargs # Returns: [{"a": 1, "b": "A"}, {"a": 1, "b": "B"}, ...]
for kwargs in p:
# yields:
# {"a": 1, "b": "A"}
# {"a": 1, "b": "B"}
# ...
```
## Files
Provides utilities for working with files. Currently it contains only a single function for merging CSV files.
```py
from jacktrade import merge_csv_files
# Merges A.csv and B.csv into AB.csv without duplicating headers
merge_csv_files(["A.csv", "B.csv"], "AB.csv")
# Merges A.csv and B.csv into AB.csv verbatim, treating headers as data
merge_csv_files(["A.csv", "B.csv"], "AB.csv", has_headers=False)
```
## Multicore
Provides an elegant and memory-efficient way to process data using multiple cores. The main advantage of using `do_multicore_work` function over manually using `concurrent.futures` or `multiprocessing` modules is that new jobs are only submitted for execution when a CPU core is available. This optimises CPU and RAM usage. Using the aforementioned modules directly, it is all too easy to inadvarently cause memory leaks and crash the interpreter (if not the whole system).
Usage example (does not work in the interactive interpreter):
```py
from jacktrade import do_multicore_work
def worker(first, second) -> tuple:
"""Receives two arguments and returns them as a tuple."""
return (first, second)
def worker_done_callback(future):
"""Called whenever a worker process terminates and returns a result."""
print(future.result())
if __name__ == "__main__":
do_multicore_work(
worker, args=[(1, 2), (3, 4), (5, 6)], worker_done_callback=worker_done_callback
) # Prints: (1, 2)\n(3, 4)\n(5, 6)\n
```
## Pickler
This tiny module contains two convenience functions for pickling and unpickling Python objects, making it possible to do so with a single function call (a feature missing from `pickle` module):
```py
from jacktrade import pickle_object, unpickle_object
pickle_object(obj := [1, 2, 3], filename := "obj.pickle") # Pickles obj to obj.pickle file
assert unpickle_object(filename) == obj # Unpickle obj.pickle and test equality with obj
```
## Sysenv
Contains utilities for interacting with the operating system and the environment.
```py
from jacktrade import in_virtual_environment
# Power management
suspend() # Put the machine into standby
hibernate() # Hibernate the machine
restart() # Restart the machine
shutdown() # Shut down the machine
# Miscellaneous
in_virtual_environment() # True if called inside venv, else False
```
Raw data
{
"_id": null,
"home_page": "",
"name": "jacktrade",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "",
"keywords": "utils,utilities",
"author": "",
"author_email": "Mario Zaja <mzaja0@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/ad/39/5ec1996478589f1f61ec257cdaf4c88970b96c3c45e32e4174ed07d2208a/jacktrade-0.9.1.tar.gz",
"platform": null,
"description": "# jacktrade\r\n[![test](https://github.com/mzaja/jacktrade/actions/workflows/test.yml/badge.svg)](https://github.com/mzaja/jacktrade/actions/workflows/test.yml) [![Coverage Status](https://coveralls.io/repos/github/mzaja/jacktrade/badge.svg?branch=main)](https://coveralls.io/github/mzaja/jacktrade?branch=main) ![PyPI version](https://img.shields.io/pypi/v/jacktrade) ![Python version](https://img.shields.io/pypi/pyversions/jacktrade) ![License](https://img.shields.io/github/license/mzaja/jacktrade)\r\n\r\n**Jack of all trades, master of none** - a collection of commonly used Python utilities. Install using:\r\n```\r\npip install jacktrade\r\n```\r\n\r\nThe package consists of the following submodules:\r\n\r\n- [Benchmark](#benchmark)\r\n- [Buffers](#buffers)\r\n- [Collections](#collections)\r\n- [Files](#files)\r\n- [Multicore](#multicore)\r\n- [Pickler](#pickler)\r\n- [Sysenv](#sysenv)\r\n\r\n## Benchmark\r\nContains a `CodeTimer` class which is used to elegantly and precisely time a piece of code:\r\n```py\r\nfrom jacktrade import CodeTimer\r\nfrom time import sleep\r\n\r\nwith CodeTimer() as ct:\r\n # Enter code to time here\r\n sleep(0.1) # Simulates a piece of code\r\n\r\n# Prints: \"Code execution took 100 ms.\"\r\n(ct.ns, ct.us, ct.ms, ct.s) # Access code duration in nano/micro/milli/seconds.\r\n```\r\n\r\nA `CodeTimer` instance can also be used as a decorator and store results of repeated function calls inside the list provided as `results` argument:\r\n```py\r\nfrom jacktrade import CodeTimer\r\nfrom time import sleep\r\n\r\nresults = [] # A list for storing timing results\r\n\r\n@CodeTimer(no_print=True, results=results)\r\ndef sleep_ms(milliseconds: int):\r\n sleep(milliseconds / 1000)\r\n\r\nsleep_ms(100) # Call wrapped function\r\nsleep_ms(200) # Call again\r\nprint(int(results[0].ms)) # Prints: \"100\"\r\nprint(int(results[1].ms)) # Prints: \"200\"\r\n```\r\n\r\n## Buffers\r\nContains a `StringBuffers` class, whose purpose is to reduce the number of I/O operations\r\nwhen writing to files. By speficying `buffer_size` parameter, the contents of the buffer\r\nare automatically flushed to disk when the buffer fills up. The class handles any number\r\nof simultaneously \"open\" files.\r\n\r\n```py\r\nfrom jacktrade import StringBuffers\r\n\r\noutput_file = \"out.txt\"\r\nbuffers = StringBuffers(output_dir=\"text\", buffer_size=3)\r\nbuffers.add(output_file, \"Hello\") # Nothing is written out\r\nbuffers.add(output_file, \" world\") # Nothing is written out\r\nbuffers.add(output_file, \"!\") # \"Hello world!\" is written to ./text/out.txt\r\n```\r\n\r\n## Collections\r\nContains utility functions for working with collections, namely dictionaries and iterables. Usage examples include:\r\n```py\r\nfrom jacktrade import *\r\n\r\n# Dict utilities\r\ndict_data = {\"a\": 1, \"b\": {\"c\": 2}}\r\nflatten_dict(dict_data) # Returns: [1, 2]\r\nget_first_dict_item(dict_data) # Returns: (\"a\", 1)\r\nget_first_dict_key(dict_data) # Returns: \"a\"\r\nget_first_dict_value(dict_data) # Returns: 1\r\n\r\n# Iterable utilities\r\nlist_data = [1, 2, [3, 4], 5, 6]\r\nflatten_list(list_data) # Returns: [1, 2, 3, 4, 5, 6]\r\nchunkify(list_data, chunk_size=2) # Yields: [1, 2], [[3, 4], 5], [6]\r\nlimit_iterator(list_data, limit=3) # Yields: 1, 2, [3, 4]\r\n```\r\n\r\n`MasterDict` is a wrapper class holding multiple dictionaries. It provides methods for simultaneously deleting keys from all underlying dictionaries, as well as clearing them. It is intended to hold caches and reliably empty them with a single method call.\r\n```py\r\nfrom jacktrade import MasterDict\r\n\r\ndicts = MasterDict(a={1: \"1\", 2: \"2\"}, b={1: \"1\", 3: \"3\"}, c={4: \"4\"})\r\ndicts.delete_keys(1, 3) # Delete keys 1 and 3 from all dictionaries\r\ndicts.as_dict() # Returns: {'a': {2: '2'}, 'b': {}, 'c': {4: '4'}}\r\ndicts.clear_all() # Clear all dictionaries\r\ndicts.as_dict() # Returns: {'a': {}, 'b': {}, 'c': {}}\r\n```\r\n\r\n`BaseMapping` is a generic base class used to create `dict` subclasses which automatically map keys to values from a collection of objects of the same type. It is used like so:\r\n```py\r\nfrom jacktrade import BaseMapping\r\n\r\nclass NameAgeLookup(BaseMapping):\r\n \"\"\"\r\n Maps a person's name to its age if the person is over 18 years old.\r\n \"\"\"\r\n\r\n def __init__(self, persons):\r\n super().__init__(\r\n items=persons,\r\n key_getter=lambda p: p[\"name\"],\r\n value_getter=lambda p: p[\"age\"],\r\n condition=lambda p: p[\"age\"] > 18,\r\n )\r\n\r\nmapping = NameAgeLookup(\r\n [\r\n {\"name\": \"Jack\", \"age\": 15},\r\n {\"name\": \"Mike\", \"age\": 27},\r\n {\"name\": \"Pete\", \"age\": 39},\r\n ]\r\n)\r\n\r\n# Assertions pass\r\nassert mapping == {\"Mike\": 27, \"Pete\": 39}\r\nassert mapping.invert() == {27: \"Mike\", 39: \"Pete\"}\r\n```\r\n\r\n`Permutations` class is used for parametrisation, returning all possible combinations of input parameters:\r\n```py\r\nfrom jacktrade import Permutations\r\n\r\np = Permutations(a=[1, 2, 3], b=[\"A\", \"B\"])\r\np.args # Returns: [(1, \"A\"), (1, \"B\"), (2, \"A\"), (2, \"B\"), ...]\r\np.kwargs # Returns: [{\"a\": 1, \"b\": \"A\"}, {\"a\": 1, \"b\": \"B\"}, ...]\r\nfor kwargs in p:\r\n # yields:\r\n # {\"a\": 1, \"b\": \"A\"}\r\n # {\"a\": 1, \"b\": \"B\"}\r\n # ...\r\n```\r\n\r\n## Files\r\nProvides utilities for working with files. Currently it contains only a single function for merging CSV files.\r\n```py\r\nfrom jacktrade import merge_csv_files\r\n\r\n# Merges A.csv and B.csv into AB.csv without duplicating headers\r\nmerge_csv_files([\"A.csv\", \"B.csv\"], \"AB.csv\")\r\n\r\n# Merges A.csv and B.csv into AB.csv verbatim, treating headers as data\r\nmerge_csv_files([\"A.csv\", \"B.csv\"], \"AB.csv\", has_headers=False)\r\n```\r\n\r\n## Multicore\r\nProvides an elegant and memory-efficient way to process data using multiple cores. The main advantage of using `do_multicore_work` function over manually using `concurrent.futures` or `multiprocessing` modules is that new jobs are only submitted for execution when a CPU core is available. This optimises CPU and RAM usage. Using the aforementioned modules directly, it is all too easy to inadvarently cause memory leaks and crash the interpreter (if not the whole system).\r\n\r\nUsage example (does not work in the interactive interpreter):\r\n```py\r\nfrom jacktrade import do_multicore_work\r\n\r\ndef worker(first, second) -> tuple:\r\n \"\"\"Receives two arguments and returns them as a tuple.\"\"\"\r\n return (first, second)\r\n\r\ndef worker_done_callback(future):\r\n \"\"\"Called whenever a worker process terminates and returns a result.\"\"\"\r\n print(future.result())\r\n\r\nif __name__ == \"__main__\":\r\n do_multicore_work(\r\n worker, args=[(1, 2), (3, 4), (5, 6)], worker_done_callback=worker_done_callback\r\n ) # Prints: (1, 2)\\n(3, 4)\\n(5, 6)\\n\r\n```\r\n\r\n## Pickler\r\nThis tiny module contains two convenience functions for pickling and unpickling Python objects, making it possible to do so with a single function call (a feature missing from `pickle` module):\r\n```py\r\nfrom jacktrade import pickle_object, unpickle_object\r\n\r\npickle_object(obj := [1, 2, 3], filename := \"obj.pickle\") # Pickles obj to obj.pickle file\r\nassert unpickle_object(filename) == obj # Unpickle obj.pickle and test equality with obj\r\n```\r\n\r\n## Sysenv\r\nContains utilities for interacting with the operating system and the environment.\r\n```py\r\nfrom jacktrade import in_virtual_environment\r\n\r\n# Power management\r\nsuspend() # Put the machine into standby\r\nhibernate() # Hibernate the machine\r\nrestart() # Restart the machine\r\nshutdown() # Shut down the machine\r\n\r\n# Miscellaneous\r\nin_virtual_environment() # True if called inside venv, else False\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "A collection of commonly used Python utilities.",
"version": "0.9.1",
"project_urls": {
"Bug Tracker": "https://github.com/mzaja/jacktrade/issues",
"Homepage": "https://github.com/mzaja/jacktrade"
},
"split_keywords": [
"utils",
"utilities"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "383e258df7ca9fe9431ca8042be0f0924cff50a084dfb4518f1581c53fa0191d",
"md5": "2617a880f927c3d738af075ce2a4537e",
"sha256": "f0305c880eb1b1639d5c26dbed50d2e4ad6d8ef02aee5c569a1671b4146c443c"
},
"downloads": -1,
"filename": "jacktrade-0.9.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2617a880f927c3d738af075ce2a4537e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 14937,
"upload_time": "2023-10-25T06:47:32",
"upload_time_iso_8601": "2023-10-25T06:47:32.504578Z",
"url": "https://files.pythonhosted.org/packages/38/3e/258df7ca9fe9431ca8042be0f0924cff50a084dfb4518f1581c53fa0191d/jacktrade-0.9.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ad395ec1996478589f1f61ec257cdaf4c88970b96c3c45e32e4174ed07d2208a",
"md5": "cf96c9ff4efe775ae47d85ce0d25983c",
"sha256": "05af77ef0ff0293b48ab53c13ce28508861286072a914a3aea90e131df0990f5"
},
"downloads": -1,
"filename": "jacktrade-0.9.1.tar.gz",
"has_sig": false,
"md5_digest": "cf96c9ff4efe775ae47d85ce0d25983c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 22605,
"upload_time": "2023-10-25T06:47:34",
"upload_time_iso_8601": "2023-10-25T06:47:34.490064Z",
"url": "https://files.pythonhosted.org/packages/ad/39/5ec1996478589f1f61ec257cdaf4c88970b96c3c45e32e4174ed07d2208a/jacktrade-0.9.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-25 06:47:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mzaja",
"github_project": "jacktrade",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"lcname": "jacktrade"
}