python 知识体系的原型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 知识体系的原型相关的知识,希望对你有一定的参考价值。

import random
import json

# Testing for obj-action-obj node model of knowledge 
# Contains multi-node tree system

OBJECT_TYPE = 'object'
ACTION_TYPE = 'action'

class KnowledgeNode(object):

	def __init__(self, type_info, value, *children):
		self.type_info = type_info
		self.value = value
		self.children = list(children)

	def __repr__(self):
		return json.dumps(self.to_dict(), indent=4)

	def to_dict(self):
		return {
		   'type_info':self.type_info,
		   'value':self.value,
		   'children':[child.to_dict() for child in self.children]
		}

	def create_child(self, type_info, value, *children):
		# Constructs and appends a new knowledge node.
		new_child = KnowledgeNode(type_info, value, *children)
		self.children.append(new_child)

	def add_child(self, child):
		self.children.append(child)

	def __getitem__(self, key):
		return self.children[key]

	def __contains__(self, key):
		for child in self.children:
			if child.value == key:
				return True
		return False


# Special child class which creates and manipulates an object node.
class ObjectNode(KnowledgeNode):
	"""
	>>> h = ObjectNode('Apples')
	>>> h
	{"value": "Apples", "type_info": "object", "children": []}
	"""

	def __init__(self, value, *children):
		super(ObjectNode, self).__init__(OBJECT_TYPE, value, *children)


class ActionNode(KnowledgeNode):
	# Convenience class to create action node specifically

	def __init__(self, value, *children):
		super(ObjectNode, self).__init__(ACTION_TYPE, value, *children)

以上是关于python 知识体系的原型的主要内容,如果未能解决你的问题,请参考以下文章

知识体系第二遍回顾(补充)--③原型链--借用构造函数继承

我的编程体系

前端开发知识体系技能点根据自我学习顺序

python知识体系

Python 编程核心知识体系-函数

汇总Python 编程核心知识体系