Skip to main content

Helpers and utils

Project description

Lully

A small library providing some helpers for python devs.

See the tests for usage examples and ideas.

Popglobal

Populate global with cabalistic aliases and some of lully's objects.

import lully as ll
ll.popglob()  # use show=True to get a list of added values


ı('a', 'b', 'c')
i(['a', 'b', 'c'])
ï()

assert first('ab') == 'a'
assert last('ab') == 'b'
assert last('ab') == 'b'

Will do this automatically in REPL mode if you import lully, and if LULLY_DISABLE_REPL is not set to 1.

Funcmore

More tools for functions, notably lully.ẍ, that create a function out of two (or more):

import lully as ll
ll.ẍ(print, input)('$ ')  # --> print(input('$ '))
ll.ẍ(''.join, lambda v: v.split())('h e l l o')  # --> 'hello'
ll.ẍ(print, list)('hello')  # --> print(list('hello')) --> ['h', 'e', 'l', 'l', 'o']

And lully.ÿ that do the same thing, but splat the output in next function calls :

ll.ÿ(print, list)('hello')  # --> print(*list('hello')) --> h e l l o

Collections

Otom

The One-To-One-Mapping is a special kind of dict, where all values are also keys, associated to their key.

from lully import Otom

O = Otom({
    'one': 1,
    'two': 2,
    'tee': 3,
})
assert O[1] == 'one'
assert O['one'] == 1

An Otom acts like a dict. See tests for more.

Transformations

Date utils

Many functions, like time_since, time_from_now, pretty_seconds, parse_date, now_plus, add_time, parse_isoformat_with_microseconds may help dealing with (un)aware datetimes, relative times, and various date format.

Human-readable hashing

You may get a string like Pieuvre Minérale out of any python object with lully.human_code.

Confiseur

A Confiseur is here to help with configurations.

The principle is to subclass, for each configuration kind you need, the Confiseur base class and populate it with Bonbon instances, describing options.

from lully import Confiseur, Bonbon

class MyConfig(Confiseur):

    def bonbons(self) -> [Bonbon]:
        return (
            Bonbon('server options', 'max instances', default=3),
        )

    def validate(self, cfg):
        if cfg['server options']['max instances'] > 10:
            self.add_error(f"Can't handle more than ten instances. Provided: {cfg['server options']['max instances']}.")

myconfig = MyConfig('{"server options": { "max instances": 11 }}')
assert myconfig['server options']['max instances'] == 11
assert myconfig.has_error
assert len(myconfig.errors)

See tests for more.

Itermore

More itertools functions !

from lully.itermore import window, grouper, flatten, dotproduct, ncycles

See the source file for the full set of functions.

Kotlin-inspired functions

When coding in other languages, you get ideas. Here are the ones i got after a 1h course of Kotlin.

from lully import first, last, zip_with_next

assert first([1, 2]) == 1
assert last([1, 2]) == 2
assert first([2, 3], lambda x: x % 2) == 3
assert tuple(zip_with_next('abc')) == (('a', 'b'), ('c', None))

See the source file for the full set of functions.

Fief

This name should recall both its goal, which is FIltering of EFfective paramaters, and the fact it keeps functions to work within their fief.

You have this function:

def func(a, b):
    ...  # some implementation

and you have its parameter stored in a dict, with other keys that are not for that specific function:

config = {'a': 1, 'b': 'doe', 'loglevel': 'WARNING'}

Thus, you can't just do that:

func(**config)

Because of the expected:

TypeError: func() got an unexpected keyword argument 'loglevel'

One solution can be to filter that dict, but that's cumbersome and needs maintainance. And that's worse if you have a lot of functions to call that way.

Fief is a decorator that will make that for you, using inspect module.

from lully import fief

@fief
def func(a, b):
    return a + b

config = {'a': 2, 'b': 3, 'loglevel': 'WARNING'}

# and suddenly, you can provide anything in keywords argument:
assert func(**config) == 5   # no TypeError, that's magic !

random

lsample

This is a function answering to the n choose k problem using the Vitter's algorithm.

The problem is to choose randomly n element in a set of k. That's usually done with the random.sample(n, [1, 2, ...]) function. Hence the sampling part of lsample name. However, that stdlib function will load everything in memory, forcing you to provide a list, not a generator.

Vitter's solution is to collect the n elements during a single pass over the list, making possible to work on generators, as long as you have an idea of their size, hence not loading all data in memory.

from lully import lsample
print(lsample(3, [x for x in range(10)], it_size=10))

This enables you to pick 100 random tweets among the entire tweeter database without having to load it in memory. Provided you have enough time for the full browsing to be performed.

See that repo for more information, sources and benchmarks.

Logging

The generic lully.logger_from(o) will help you getting a logger, whatever o is. By default, it will be a logger using print statements written in stderr, but you can also provide a configured logger.

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

lully-3.3.0.tar.gz (72.7 kB view details)

Uploaded Source

Built Distribution

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

lully-3.3.0-py3-none-any.whl (69.3 kB view details)

Uploaded Python 3

File details

Details for the file lully-3.3.0.tar.gz.

File metadata

  • Download URL: lully-3.3.0.tar.gz
  • Upload date:
  • Size: 72.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lully-3.3.0.tar.gz
Algorithm Hash digest
SHA256 b0a729a778f42993df0ea0430b7896bed9bc593c600adda576a9f8766deeda8b
MD5 0771dda76e3c4ccf00049959eb21f844
BLAKE2b-256 e03938ca6f1fa70a37fd8a7cd093ce555d2c1601b3218e8d94ae76abe2b5a925

See more details on using hashes here.

Provenance

The following attestation bundles were made for lully-3.3.0.tar.gz:

Publisher: python-publish.yml on Aluriak/lully

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lully-3.3.0-py3-none-any.whl.

File metadata

  • Download URL: lully-3.3.0-py3-none-any.whl
  • Upload date:
  • Size: 69.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lully-3.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 784a918983e06e0aa00c1b732c28fd22ada943d31c451a08c10e73cf5fe977ea
MD5 959cd097fb2e34b06fe6a4f93e7369d0
BLAKE2b-256 5a5805059d5eb6edef7833cbc5e29838332f4f012648fdce3e27de674bc27635

See more details on using hashes here.

Provenance

The following attestation bundles were made for lully-3.3.0-py3-none-any.whl:

Publisher: python-publish.yml on Aluriak/lully

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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