Skip to main content

simple Python library with coalesce function and "magic" empty value

Project description

Simple Python library with coalesce function and “magic” empty value and others features


pip install coalesce



This is a factory of some abstract unique values


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()
    if hungry_answer:
        print("- YES, i would eat an elephant!")
        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?")


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)

f()  # value=<random(1,10)>
f(None)  # value=None


Function returns first not ignoring value from iterable object. By default ignoring empty value


from coalesce import coalesce, empty
from random import randint

def f(value=empty):
    value = coalesce([value, randint(1,10)])

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


Function returns first value from iterable for which the function(value) is truthful from iterable object. Else it returns default value


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

Project details

Download files

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

Files for Coalesce, version 0.3
Filename, size File type Python version Upload date Hashes
Filename, size coalesce-0.3-py3-none-any.whl (4.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size coalesce-0.3.tar.gz (2.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page