Skip to main content

tools for common operations in a module

Project description

olutils

travis codecov PyPI Latest Release

Introduction

About

The module olutils provide common tools to simplify daily coding. It includes:

  • object storing (.csv, .json, .pickle, .txt)
  • convenient collection (deep defaultdict, lazy list, identity/prod functions)
  • conversion functions (for datetime, dictionaries, errors)
  • sequencing helpers (iteration with progress display, wait until predicate)
  • parameter management
  • and more...

Installation

One can install olutils using pip install command: pip install olutils

Usage

Object Storing

  • Storage: save and load
import olutils

my_dict = {'key_1': "value_1", 'key_2': 2}
my_rows = [{'col_1': 11, 'col_2': 21}, {'col_1': 21, 'col_2': 22}]

# Saving objects in output directory (automatically created)
olutils.save(my_dict, "output/my_dict.json")
olutils.save(my_rows, "output/my_rows.csv")
olutils.save(my_rows, "output/my_rows.unknown", mthd="json")

# Loading objects from save outputs
my_loaded_dict = olutils.load("output/my_dict.json")
my_loaded_rows = olutils.load("output/my_rows.csv")
my_loaded_rows_ = olutils.load("output/my_rows.unknown", mthd="json")

Collections

  • deepdefaultdict
import olutils
from olutils.conversion import str2dt

# Building a deep default dict with datetimes as values
flights = olutils.deepdefaultdict(lambda x: None, depth=2)

# Filling it
flights['Paris-NY']['departure'] = str2dt("2019-01-15 08:00+01:00")
flights['Paris-NY']['arrival'] = str2dt("2019-01-15 10:30-05:00")
flights['NY-Paris']['departure'] = str2dt("2019-01-17 23:00-05:00")
flights['NY-Paris']['arrival'] = str2dt("2019-01-15 11:00+01:00")

flights.pprint()
  • LazyList
import olutils

print(olutils.LazyList(range(10000), 10))
  • functions: prod and identity
import olutils

# Operations
assert olutils.prod([2, 7]) == 14
assert olutils.identity(1) == 1
  • singleton:
import olutils

class MyClass(metaclass=olutils.Singleton):
    pass

instance = MyClass()
assert instance is MyClass()

More

  • Explicit iteration
import olutils


print("Iterating on very long iterable:")
for elem in olutils.countiter(range(int(10e6)), vbatch=100, prefix=". "):
    # One-Line display of progress every 100 iteration
    pass
  • Compare
import olutils

# Comparison
l1 = [1, 2, "hi", "bye"]
l2 = [3, "bye", "bye bye", 2]
assert olutils.content_diff(l1, l2) == {
    'common': {2, "bye"},
    'minus': {1, "hi"},
    'plus': {3, "bye bye"},
}
  • Pretty displays
import olutils

assert olutils.err2str(ValueError("Message")) == "ValueError - Message"
l = [1, 2, 3, 4, 5]
imp_l = olutils.lazy_content(l, 4)
assert str(imp_l) == '[1, 2, Ellipsis, 5]'
dic = {
    'values': l,
    'info': {
        'name': "Some example",
        'also': "This is awesome (kinda)",
    }
}
print(f"Dictionary used: {dic}")
print(f"Dictionary to pretty string:")
def leafconv(x):
    return (
        str(olutils.lazy_content(x, 5))
        if isinstance(x, list)
        else str(x)
    )
print(olutils.dict2str(dic, leafconv=leafconv))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

olutils-2.1.0.tar.gz (28.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

olutils-2.1.0-py3-none-any.whl (37.2 kB view details)

Uploaded Python 3

File details

Details for the file olutils-2.1.0.tar.gz.

File metadata

  • Download URL: olutils-2.1.0.tar.gz
  • Upload date:
  • Size: 28.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for olutils-2.1.0.tar.gz
Algorithm Hash digest
SHA256 0fb863664fcabcb7b09b7d98762444c9708daeebb435ecb7c80904f3c9548268
MD5 08debc45eae7e1edddfa56ebe4a5524e
BLAKE2b-256 42075306f32b288bae5eb86c5bae445369a5e024a6b89f8de1ae28753f98ff11

See more details on using hashes here.

File details

Details for the file olutils-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: olutils-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 37.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.9.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for olutils-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b6e171282b0a1782e2ccd4484e11fcbbc856feb6c2b5c5bcbeb96319fe44723
MD5 f672b9c4ae8a85c652d85b21c037a033
BLAKE2b-256 3020dc1383c37ebf1989d9425383b1179da24e9cf86fcf866c4b67afe0503083

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page