Simple Python library with coalesce function and “magic” empty value and others features
Installation
pip install coalesce
Features
UniqueValue
This is a factory of some abstract unique values
Example:
from coalesce import UniqueValue
Yes = UniqueValue(truthful=True)
MayBe = UniqueValue()
IDontKnow = UniqueValue()
Unlikely = UniqueValue()
No = UniqueValue(truthful=False)
def answer_lottery():
import random
return random.choice((Yes, MayBe, IDontKnow, Unlikely, No))
print("- Are you hungry?")
hungry_answer = answer_lottery()
try:
if hungry_answer:
print("- YES, i would eat an elephant!")
else:
print("- I'm fed up...")
except TypeError:
print("- Well, I want only coffee")
print("- Will you marry me?")
marry_answer = answer_lottery()
if marry_answer == Yes:
print("- Well, finally, YES!")
elif marry_answer == MayBe:
print("- Well, only if you really are rich...")
elif marry_answer == IDontKnow:
print("- I need to think...")
elif marry_answer == Unlikely:
print("- When the cancer on the mountain whistles.")
elif marry_answer == No:
print("- Who are you, boy?")
empty
The empty is conrete falsely UniqueValue.
Using in situations, when we want differ None and real “empty” value. For example set a dynamically calculated default value:
from coalesce import empty
from random import randint
def f(value=empty):
if value == empty:
value = randint(1,10)
print('value={}'.format(value))
f() # value=<random(1,10)>
f(None) # value=None
coalesce
Function returns first not ignoring value from iterable object. By default ignoring empty value
Example:
from coalesce import coalesce, empty
from random import randint
def f(value=empty):
value = coalesce([value, randint(1,10)])
print('value={}'.format(value))
f() # value=<random(1,10)>
f(None) # value=None
print coalesce([None, 1, 2], ignore=None, default=-7) # 1
print coalesce([None, None], ignore=None, default=-7) # -7
first
Function returns first value from iterable for which the function(value) is truthful from iterable object. Else it returns default value
Example:
from coalesce import first
print first(lambda x: x > 1, [None, 1, 2], default=-7) # 2
print first(lambda x: x > 2, [None, 1, 2], default=-7) # -7
Release files for Coalesce 0.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 | |
|---|---|---|---|
| coalesce-0.3.tar.gz | 2.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| coalesce-0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.4 kB
Release files / coalesce-0.3.tar.gz
| Download URL | coalesce-0.3.tar.gz |
|---|---|
| Size | 2.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
77559bcef4b18475767469492acb657405704677f33431faccaf4a9471e08173
|
|
BLAKE2b-256 checksum How to use checksums |
0ccf7a83865668cdf32e0061fd74ad56b6f6ceb211fd5e0de08ee972e15082f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / coalesce-0.3-py3-none-any.whl
| Download URL | coalesce-0.3-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
54cb849db8297235c147dddf079bc03ce977bf158ae92822deb33f567915142c
|
|
BLAKE2b-256 checksum How to use checksums |
ed346dbd6748861d55e2ba596c4a4d2a8cae21fdf4416c18601b2b58f0beed20
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |