#!/usr/bin/python from pprint import pprint from copy import deepcopy import os class TSG: def __init__(self): self.conditions = {} def open_file(self, path): data_file = path with open(data_file, 'r') as f: for line in f: if line.strip(): (key, value) = line.split("|") key=key.strip() value=value.strip() if(key=="" or key==">" or key=="-" or value=="" or value==">" or value=="-"): continue if key in self.conditions: if value not in self.conditions[key]: self.conditions[key].append(value) elif not key in self.conditions: self.conditions[key] = [value] else: self.conditions[key].append(value) def print_conditions(self): for condition in self.conditions: for item in self.conditions[condition]: pprint ("Jei " + condition + ", tai " + item ) pprint (self.conditions) pprint ("--------------------------") #_parse_entry #_parse_removal # return false def get_conditions(self): return self.conditions def add_conditions(self, a , b): a=a.strip() b=b.strip() if(a=="" or a==">" or a=="-" or b=="" or b==">" or b=="-"): return False if a in self.conditions: if b not in self.conditions[a]: self.conditions[a].append(b) elif not a in self.conditions: self.conditions[a] = [b] else: self.conditions[a].append(b) def rem_conditions(self, a, b): a=a.strip() b=b.strip() if(a=="" or a==">" or a=="-" or b=="" or b==">" or b=="-"): return False self.conditions[a].remove(b) def save_file(self, data_file): with open(data_file, 'w') as f: for condition in self.conditions: for item in self.conditions[condition]: f.write(condition + "|" + item + "\n") def search_for_tsgs(self, starting_point): #parse starting point starting_point=starting_point.strip() listerino = [] listerino.append([starting_point]) FIX_ME=True while FIX_ME: list_hello = deepcopy(listerino) for inner_listerino in listerino: for condition in self.conditions: if condition == inner_listerino[-1]: i=0 for item in self.conditions[condition]: if item not in inner_listerino: if i>=1: listerino.append(inner_listerino[:-1]) listerino[-1].append(item) i=+1 else: inner_listerino.append(item) i=+1 if list_hello== listerino: FIX_ME=False return listerino def search_for_asgs(self, starting_point): starting_point=starting_point.strip() listerino = [] listerino.append([starting_point]) FIX_ME=True while FIX_ME: list_hello = deepcopy(listerino) for inner_listerino in listerino: i=0 tmp=inner_listerino[-1] for condition in self.conditions: if tmp in self.conditions[condition]: if condition not in inner_listerino: if i>=1: listerino.append(inner_listerino[:-1]) listerino[-1].append(condition) i=+1 else: inner_listerino.append(condition) i=+1 if list_hello == listerino: FIX_ME=False return listerino a = TSG() a.conditions['a']='b' a.conditions['a']='c' a.conditions['c']='d' a.conditions['c']='f' a.conditions['f']='c' print(a.search_for_tsgs('a'))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question