Python抽象数据结构(ADT)扩展
adt-extension的Python项目详细描述
adt分机
Python抽象数据结构(ADT)扩展。在
Install:
pip install adt-extension
Import:
^{pr2}$扩展
目前该软件包具有ADT扩展:
Class | Extension of | Description |
---|---|---|
^{ | ^{ | Set with element type and validation rule. |
^{ | ^{ | Dictionary with the possibility of behavior of a ^{ |
设置
使用元素类型和将插入到集中的元素的验证规则设置。在
示例:
fromadt_extensionimportSet# A set with only even integersset_int_even=Set(element_type=int,rule=lambdax:(x%2==0))# Elements that satisfies the element type and validation ruleset_int_even.update({4,6})# Elements that satisfies the element type, but not validation ruleset_int_even.update({5})# Elements that not satisfies the element typeset_int_even.update({"qwe",True})print(set_int_even)# Output:# Set({4, 6})# Remove element typeset_int_even.element_type=None# Remove validation ruleset_int_even.rule=None
SwitchDict
当试图访问Dictionary类中不存在的索引时,可以执行函数或返回值的字典。在
示例:
fromadt_extensionimportSwitchDict# Same behavior of a normal dictionaryswitch_dict=SwitchDict({'Apartament':125,'House':250,'Condominium':300,})# Add default caseswitch_dict.default_case=999# List exampleproperties_list=['Apartament','House','Condominium','Treehouse','Hotel',]# Get valuesproperties_values=[switch_dict[x]forxinproperties_list]print(properties_values)# Output:# [ 125, 250, 300, 999, 999 ]# Remove default case, becomes a normal dictionaryswitch_dict.default_case=None
- 项目
标签: