Skip to main content

Smarter

codecov CI

Some objects could be smarter

pip install smarter

SmartList

regular list operations

from smarter import SmartList

colors = SmartList(['red', 'green', 'blue'])

colors.append('yellow')
colors.extend(['orange', 'purple'])
colors.insert(1, 'black')
colors.remove('black')

smart list operations

colors.first() # 'red'
colors.last() # 'purple'
colors.first_or(13) # 'red'
colors.last_or("default") # 'purple'

items = SmartList([])  # empty list
items.first_or(13) # 13
items.last_or("default") # 'default'

items = SmartList([None, "", [], 13])
items.first_not_null() # ""

items = SmartList([None, "", [], 13])
items.first_not_nullable() # 13

Smart Result Wrapper

Instead of calling error prone functions, wrap it in a Result object

def this_fails(x):
    return x / 0

w = Result(this_fails, 5)  # instead of w = this_fails(5)

w.is_error() # True
w.is_ok() # False
w.exc # ZeroDivisionError
w.unwrap_or(5) # 5
w.unwrap_or_else(lambda: 5) # 5
w.and_then(lambda x: x + 1, 5) # Raises ZerodivisionError
w.unwrap() # Raises ZeroDivisionError
def this_succeeds(x):
    return 1 + x

w = Result(this_succeeds, 5)  # Instead of w = this_succeeds(5)
w.is_error() # False
w.is_ok() # True
w.exc # None
w.unwrap_or(5) # 6
w.unwrap_or_else(lambda: 'default') # 6
w.and_then(lambda value, x: value * x, 5).unwrap() # 30
w.unwrap() # 6

def double_integer(x):
    return x * 2

result = (
  w.and_then(double_integer)  # 12
   .and_then(double_integer)  # 24
   .and_then(double_integer)  # 48
   .and_then(double_integer)  # 96
   .unwrap()
) # 96

w.ok() # 6

By default all exceptions are wrapped in a Result object but it is possible to specify the exception type.

person = {'name': 'John', 'age': '25'}
w = Result(person.__getitem__, 'city', suppress=KeyError)
w.is_error() # True
w.exc # KeyError
w.unwrap_or('Gotham') # 'Gotham'

# When exception type is specified, other exceptions are not wrapped
# raising the original exception eagerly/immediately.
w = Result(person.get, 'city', 'other', 'another', suppress=KeyError)
Traceback (most recent call last):
...
TypeError: get expected at most 2 arguments, got 3

However the above example is better covered by the smart >> get described below.

Smart get operations

Given the following objects

person = {'name': 'John', 'age': 30}
colors = ['red', 'green', 'blue']
position = (1, 2, 6, 9)

class Person:
    name = "John"

Ubiquitous get operations using >> operator

# On a `dict` gets by `key`
person >> get('name') # 'John'
person >> get('age') # 30
person >> get('city', default='Gotham') # Gotham

# On a `list` gets by `index`
colors >> get(0) # 'red'
colors >> get(1) # 'green'
colors >> get(2) # 'blue'
colors >> get(3) # None
colors >> get(4, "default") # 'default'

# On a `tuple` gets by `index`
position >> get(0) # 1
position >> get(1) # 2

# On a `class` gets by `attribute`
p = Person()
p >> get('name') # 'John'
p >> get('age', default=45) # 45

Release files for smarter 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smarter 0.1.4
File Size Uploaded
smarter-0.1.4.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for smarter 0.1.4
File Interpreter ABI Platform
smarter-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 13.3 kB

Release files / smarter-0.1.4.tar.gz

Download URL smarter-0.1.4.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4a7c8172282414221eb4dcb601ebf8755a926c9988e3f87e76e8db8d2c0a39fb
BLAKE2b-256 checksum
How to use checksums
90fe4052d70a89a9310a58365e5c87020b234f61f891a587faa5801b4cf97c79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

Release files / smarter-0.1.4-py3-none-any.whl

Download URL smarter-0.1.4-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ab2e2d474b0a0b77adb771922dfeb8be6a01d4c8bb073c1a18e83aeb6e61b898
BLAKE2b-256 checksum
How to use checksums
e6a2d36359db6b907564c29fdfc1e00cc7f6c8ae87f7d36676be4bdbb5f7181a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page