Skip to main content

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

Project description

# coalesce
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:

```python
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:

```python
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:
```python
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:
```python
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.

Source Distribution

Coalesce-0.1.tar.gz (2.1 kB view details)

Uploaded Source

Built Distribution

Coalesce-0.1-py2-none-any.whl (4.2 kB view details)

Uploaded Python 2

File details

Details for the file Coalesce-0.1.tar.gz.

File metadata

  • Download URL: Coalesce-0.1.tar.gz
  • Upload date:
  • Size: 2.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Coalesce-0.1.tar.gz
Algorithm Hash digest
SHA256 8fb70abd1094ef171d9893371c764fc7c91af17a5a56684b21a58440681baaf3
MD5 4d0f08b8b71ba2406c7477329d02db43
BLAKE2b-256 7c8ae170045370fd61c302eac33e9a06ab86040f33a349742d1fafcf3f9e341f

See more details on using hashes here.

File details

Details for the file Coalesce-0.1-py2-none-any.whl.

File metadata

File hashes

Hashes for Coalesce-0.1-py2-none-any.whl
Algorithm Hash digest
SHA256 7433b4b4fb43b3f6c2498e2aadd8847c661d7c0db6e5c49f3ca7bdb3d20529ff
MD5 72314ec6c6e27ca96e2daeb945b2dfaa
BLAKE2b-256 0449cf11fbad65ea7a0f777096b7b1c565a41bff716031c25edd7f3b15112328

See more details on using hashes here.

Supported by

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