某些对象可能更智能
项目描述
Smarter
某些对象可能更智能
pip install smarter
SmartList
常规 list
操作
from smarter import SmartList
colors = SmartList(['red', 'green', 'blue'])
colors.append('yellow')
colors.extend(['orange', 'purple'])
colors.insert(1, 'black')
colors.remove('black')
智能 list
操作
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
包装器
代替调用易出错的函数,将其包装在 Result
对象中
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
默认情况下,所有异常都将在 Result
对象中包装,但也可以指定异常类型。
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
然而,上述示例更适合使用下面描述的智能 >> get
。
智能 get
操作
给定以下对象
person = {'name': 'John', 'age': 30}
colors = ['red', 'green', 'blue']
position = (1, 2, 6, 9)
class Person:
name = "John"
使用 >>
操作符的普遍 get
操作
# 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
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源分发
smarter-0.1.4.tar.gz (6.7 kB 查看哈希值)
构建分发
smarter-0.1.4-py3-none-any.whl (6.6 kB 查看哈希值)
关闭
smarter-0.1.4.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4a7c8172282414221eb4dcb601ebf8755a926c9988e3f87e76e8db8d2c0a39fb |
|
MD5 | b0d7f41da26fde72461687363a2bfeda |
|
BLAKE2b-256 | 90fe4052d70a89a9310a58365e5c87020b234f61f891a587faa5801b4cf97c79 |