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'

If LULLY_ENABLE_REPL is set to 1, it will do this automatically when importing lully.

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.5.0.tar.gz (77.4 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.5.0-py3-none-any.whl (73.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for lully-3.5.0.tar.gz
Algorithm Hash digest
SHA256 85b17ea4981ba2e8d0371953ca2c8c4199e5e2734410f664e2ebf3b7f42eb665
MD5 366e0621899ae2399bc57a37a8007f59
BLAKE2b-256 3e026490517b290849e6cc29466a9b1be2b6fbf627df99b2ba2aa2ea7b2706c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for lully-3.5.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.5.0-py3-none-any.whl.

File metadata

  • Download URL: lully-3.5.0-py3-none-any.whl
  • Upload date:
  • Size: 73.9 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.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c2c140702a1386454e23b70a756bbd1239a47c66ea0ef9b810841f062fb544e
MD5 9b7cc5c1d3ba755ed5f49cba1d9f45d5
BLAKE2b-256 19081fa110743984d73a4b42cb26acceda9d206e3ed216e612814861cbf37355

See more details on using hashes here.

Provenance

The following attestation bundles were made for lully-3.5.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