python上的微型抽象数据类型

miniadt的Python项目详细描述


如何使用

## create Type

>>> from miniadt import ADTTypeProvider
>>> TreeType = ADTTypeProvider("Tree")

>>> Node = TreeType("Node", "e children")
>>> Leaf = TreeType("Leaf", "e")


## printing value

>>> Leaf(e=10)
Leaf(e=10)
>>> Node(e=10, children=[Leaf(e=20)])
Node(e=10, children=[Leaf(e=20)])


## use pattern match

>>> @TreeType.match
... class depth(object):
...     def Node(e, children):
...         return max(depth(e)for e in children) + 1
...
...     def Leaf(e):
...         return 1

>>> depth(Leaf(e=10))
1

>>> depth(Node(e=10, children=[Leaf(e=20), Node(e=30, children=[Leaf(e=40)])]))
3

miniadt具有全面的检查功能。

## not comprehensive definition on pattern matching function error is occur

### 1. lack of dispatch andidates
>>> class invalid_dispatch(object):
...     def Node(e, children):
...         return "foo"

>>> TreeType.match(invalid_dispatch)
Traceback (most recent call last):
 ...
miniadt.NotComprehensive: Leaf is not found. expected=['Node', 'Leaf']


### 2. dispatch function's arguments are invalid.
>>> class invalid_dispatch2(object):
...     def Node(e):  ## correct argsspec is "e, children"
...         return "foo"
...     def Leaf(e):
...         return "foo"

>>> TreeType.match(invalid_dispatch2)
Traceback (most recent call last):
 ...
miniadt.NotComprehensive: on Tree.Node:  expected=['e', 'children'] != actual=['e']

类似功能

  • 匹配
  • 匹配实例
  • 分类
fromminiadtimportADTTypeProviderTree=ADTTypeProvider("Tree")Node=Tree("Node","e children")Leaf=Tree("Leaf","e")print(Leaf(e=10))# => Leaf(e=10)print(Node(e=10,children=[Leaf(e=20)]))# => Node(e=10, children=[Leaf(e=20)])@Tree.matchclassdepth(object):defLeaf(e):return1defNode(e,children):returnmax(depth(e)foreinchildren)+1print(depth(Leaf(e=10)))# => 10print(depth(Node(e=10,children=[Leaf(e=20)])))# 2@Tree.match_instanceclassApplicator(object):def__init__(self,name):self.name=namedefLeaf(self,e):returnself.namedefNode(self,e,children):return[self.name,[self(x)forxinchildren]]print(Applicator("foo")(Leaf(e=10)))# => fooprint(Applicator("foo")(Node(e=10,children=[Leaf(e=20)])))# => ['foo', ['foo']]@Tree.classifyclassToDict(object):defLeaf(self,leaf):returnleaf.edefNode(self,node):return{"e":node.e,"children":[self(e)foreinnode.children]}todict=ToDict()print(todict(Leaf(e=10)))# => 10print(todict(Node(e=10,children=[Leaf(e=20)])))# => {'e': 10, 'children': [20]}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java连接usb到uart设备到安卓设备>3.1   可以强制Php中的web应用程序与Java中的桌面应用程序一起工作吗?   java为什么自定义系统类加载器不工作?   数组在Java中解析具有多个分隔符的字符串   PMD Java 8德米特定律   JavaSpringMVC表单验证不适用于嵌套的复杂类型   让Eclipse Java组织导入以使用Google checkstyle   java Appium:无法创建新会话   java如何在数组中声明新字段   java如何解决“无法初始化类org.apache.cassandra.config.DatabaseDescriptor”?   java AsyncTask创建socket   java向@CreatedBy添加更多信息   如何在ubuntu中运行包含大量jars依赖项的java文件   java如何使用<s:select>标记并在中休眠来填充下拉列表?   java获取错误:找不到符号变量“level”和“next_level_button”   javaweb应用中基于UI的ajax显示代码流   Java长到MySql   java JvisualVM:奇怪的应用程序行为   ubuntu将Java程序的输出结果保存到一个文件中