def test_avl_tree_insert(self): bt = avl_tree.AVLTree() bt.insert(10) bt.insert(11) bt.insert(9) assert bt.tree.left.value == 9 assert bt.tree.right.value == 11
def test_avl_tree_init_values(self): bt = avl_tree.AVLTree(keys=[1, 2, 3, 4, 0]) assert bt.tree.value == 2 assert bt.tree.right.value == 3 assert bt.tree.right.right.value == 4 assert bt.tree.left.value == 1 assert bt.tree.left.left.value == 0
def test_avl_tree_left_rotate(self): avl = avl_tree.AVLTree([1, 2, 3, 4, 5]) assert avl.tree.value == 2 assert avl.tree.left.value == 1 assert avl.tree.right.value == 4 assert avl.tree.right.left.value == 3 assert avl.tree.right.right.value == 5
def test_avl_tree_right_rotate(self): avl = avl_tree.AVLTree([5, 4, 3, 2, 1]) assert avl.tree.value == 4 assert avl.tree.left.value == 2 assert avl.tree.right.value == 5 assert avl.tree.left.left.value == 1 assert avl.tree.left.right.value == 3
def get_all_vendor(self, ): self.vendor_data = avl.AVLTree() data_buffer = [] data_buffer = FileIO.getalldata("vendor") for i in data_buffer: self.vendor_data.insert(db.tools.a1z26(i.RN), i) self.vendor_dict[i.name] = db.tools.a1z26(i.RN)
def test_avl_tree_search(self): bt = avl_tree.AVLTree(keys=[1, 2, 3, 4, 0], values=['one', 'two', 'three', 'four', 'zero']) assert bt.search(1) == 'one' assert bt.search(2) == 'two' assert bt.search(3) == 'three' assert bt.search(4) == 'four' assert bt.search(0) == 'zero'
def get_all_product(self, ): self.product_data = avl.AVLTree() data_buffer = [] data_buffer = FileIO.getalldata("product") for i in data_buffer: self.product_data.insert(db.tools.a1z26(i.SN), i) self.product_dict[i.name] = db.tools.a1z26(i.SN) self.product_data.last_num = FileIO.last_num
def test_avl_tree_search_no_find(self): bt = avl_tree.AVLTree(keys=[1, 2, 3, 4, 0], values=['one', 'two', 'three', 'four', 'zero']) assert bt.search(11) == -1 assert bt.search(100) == -1 bt.insert(11, value='eleven') bt.insert(100, value='hundred') assert bt.search(11) == 'eleven' assert bt.search(100) == 'hundred'
def test_avl_tree_init_key_value(self): bt = avl_tree.AVLTree(keys=[1, 2, 3, 4, 0], values=['one', 'two', 'three', 'four', 'zero']) assert bt.tree.key == 2 assert bt.tree.right.key == 3 assert bt.tree.right.right.key == 4 assert bt.tree.left.key == 1 assert bt.tree.left.left.key == 0 assert bt.tree.value == "two" assert bt.tree.right.value == "three" assert bt.tree.right.right.value == "four" assert bt.tree.left.value == "one" assert bt.tree.left.left.value == "zero"
def test_avl_tree_wrong_types(self): bt = avl_tree.AVLTree(keys=[1, 2, 3, 4, 0]) self.assertRaises(TypeError, bt.insert, 'string')
def test_avl_tree_init_no_in(self): bt = avl_tree.AVLTree() assert bt.tree is None
def test_avl_tree_test_left_right_rotation(self): avl = avl_tree.AVLTree([8, 5, 6]) assert avl.tree.value == 6 assert avl.tree.right.value == 8 assert avl.tree.left.value == 5
def test_avl_tree_mixed_key_value_no_value(self): bt = avl_tree.AVLTree(keys=[1, 2, 3, 4, 0], values=['one', 'two', 'three', 'four', 'zero']) bt.insert(23) assert bt.search(23) == 23 assert bt.search(1) == 'one'
class search(): vendor_data = avl.AVLTree() product_data = avl.AVLTree() vendor_dict = {} product_dict = {} def get_all_vendor(self, ): self.vendor_data = avl.AVLTree() data_buffer = [] data_buffer = FileIO.getalldata("vendor") for i in data_buffer: self.vendor_data.insert(db.tools.a1z26(i.RN), i) self.vendor_dict[i.name] = db.tools.a1z26(i.RN) def get_all_product(self, ): self.product_data = avl.AVLTree() data_buffer = [] data_buffer = FileIO.getalldata("product") for i in data_buffer: self.product_data.insert(db.tools.a1z26(i.SN), i) self.product_dict[i.name] = db.tools.a1z26(i.SN) self.product_data.last_num = FileIO.last_num def insert_product(self, data_dict): if 'name' in data_dict: product = data_dict['name'] vendor = "" if 'No' in data_dict: vendor = data_dict['No'] elif 'vendor' in data_dict: vendor = data_dict['vendor'] else: return -9 if vendor in self.vendor_dict: product_data_ = None getvendor = self.vendor_data.search(self.vendor_dict[vendor]) if product in getvendor.product: product_data_ = self.product_data.search( getvendor.product[product]) if product_data_.SN != data_dict['SN']: #print("資料名稱 跟 商品條碼不符合") return -4 FileIO.remove(str(product_data_.SN), 'product') else: self.product_data.last_num += 1 from copy import deepcopy data = deepcopy(self.product_data.last_num) product_data_ = db.product(product, data_dict['SN']) product_data_.number = deepcopy(data) product_data_.SN = data_dict['SN'] getvendor.product[product] = product_data_.SN FileIO.remove(getvendor.name, 'vendor') FileIO.createYaml(getvendor, 'vendor') self.product_dict[product] = product_data_.SN self.product_data.insert(product_data_.SN, product_data_) product_data_.quantity += data_dict['quantity'] #print("fileIO = ",FileIO.createYaml(product_data_,'product')) FileIO.createYaml(product_data_, 'product') return 1 return -1 def take_product(self, data_dict): if 'name' in data_dict: product = data_dict['name'] vendor = "" if 'No' in data_dict: vendor = data_dict['No'] elif 'vendor' in data_dict: vendor = data_dict['vendor'] else: return -9 if vendor in self.vendor_dict: product_data_ = None getvendor = self.vendor_data.search(self.vendor_dict[vendor]) if product in getvendor.product: product_data_ = self.product_data.search( getvendor.product[product]) if product_data_.quantity >= data_dict['quantity']: FileIO.remove(str(product_data_.SN), 'product') product_data_.quantity -= data_dict['quantity'] FileIO.createYaml(product_data_, 'product') else: #print("提取數量大於庫存數量") return -2 else: #print("查無此商品") return -3 def createData(self, data_dict, typeSelect): if typeSelect == 'vendor': item = FileIO.formatDatabase(data_dict, typeSelect) debug = FileIO.createYaml(item, typeSelect) if debug == 1: self.vendor_data.insert(db.tools.a1z26(item.RN), item) self.vendor_dict[item.name] = db.tools.a1z26(item.RN) else: return debug elif typeSelect == 'product': item = FileIO.formatDatabase(data_dict, typeSelect) debug = FileIO.createYaml(item, typeSelect) if debug == 1: self.product_data.insert(db.tools.a1z26(item.SN), item) self.product_dict[item.name] = db.tools.a1z26(item.SN) else: return debug def removeData(self, data_name, typeSelect): if typeSelect == 'vendor': if data_name in self.vendor_dict.keys(): if FileIO.remove(data_name, typeSelect) == 0: self.vendor_data.delete(data_name) self.vendor_dict.pop(data_name) else: return FileIO.remove(data_name, typeSelect) elif typeSelect == 'product': if data_name in self.product_dict.keys(): if FileIO.remove(data_name, typeSelect) == 0: self.product_data.delete(data_name) self.product_dict.pop(data_name) else: return FileIO.remove(data_name, typeSelect) def getName(self, number, typeSelect): try: listOfKeys = list() listOfItems = eval('self.' + typeSelect + '_dict.items()') for item in listOfItems: if item[1] == db.tools.a1z26(number): listOfKeys.append(item[0]) return listOfKeys except: #print('dict找不到number的key') return -1 def require(self, data): return_list = [] attribute = [] keys = [] all_vatt = ['name', 'RN', 'principle', 'address', 'product'] all_patt = [ 'name', 'SN', 'number', 'warranty', 'volume', 'weight', 'category', 'quantity' ] attribute = data['select'] keys = data['where'].keys() if data['from'] == 'vendor': for key in keys: vdata = self.vendor_data.search(key, data['where'][key]) for v in vdata: inside_return_list = [] if '*' in attribute: attribute = all_vatt for att in attribute: inside_return_list.append(eval('v.' + att)) if len(inside_return_list) > 0: return_list.append(inside_return_list) return return_list elif data['from'] == 'product': for key in keys: pdata = self.product_data.search(key, data['where'][key]) for p in pdata: inside_return_list = [] if '*' in attribute: attribute = all_patt for att in attribute: inside_return_list.append(eval('p.' + att)) if len(inside_return_list) > 0: return_list.append(inside_return_list) return return_list def __init__(self, ): print("正在將外部檔案載入...") self.get_all_vendor() self.get_all_product() print("讀取完成")
def __init__(self): self.size = 0 self.tree = avl_tree.AVLTree()