class ParcedDataHandler(): """ Handle parced data in Server. Return slice result """ def __init__(self, cutted_list=[], parsed_line_sequence_list=[]): # Default arguement (cutted_list=[]) would only be evaluated once in python # So its memory id would be the same during the different object creation # One of classic python pitfall self.cutted_list = deepcopy(cutted_list) self.parsed_line_sequence_list = deepcopy(parsed_line_sequence_list) # old_stored_partial is used to save old value # when stored_partial is updated # after SliceMachine.previous_partial return new partial value self.old_stored_partial = '' # stored_partial is sent to SliceMachine.previous_partial # and would receive new partial value # from SliceMachine.slice_result self.stored_partial = '' self.singleton_factory = StateMachineSingletonFactory() def get_machine(self, mark): sm = self.singleton_factory.dispatch_machine(mark) return sm def update_cutted_list(self, sm): cutted = sm.slice_result[0] if '' != cutted: print "cutted in update_cutted_list:", cutted self.cutted_list.append(cutted) def get_machine_result(self, sm): self.update_cutted_list(sm) self.old_stored_partial = self.stored_partial self.stored_partial = sm.slice_result[1] #self.cutted = sm.cutted def slice_line(self, parcedTuple): #self.handle_package(parcedTuple) assert self.stored_partial is not None mark = parcedTuple[1] sm = self.get_machine(mark) sm.__init__(self.stored_partial, parcedTuple) sm.do_slice() self.get_machine_result(sm) #return sm def slice_lines(self): # a new line in the data file would be eventually parsed as ("",) # self.parsed_line_sequence_list should be already parced to a list of tuples for parcedLineTuple in self.parsed_line_sequence_list: if "" == parcedLineTuple[0]: self.cutted_list.append('\n') continue sm = self.slice_line(parcedLineTuple) def cutted_list_to_line(self): lines = "" for line in self.cutted_list: if "\n" != line: lines = lines.join([line, "\n"]) return lines
class Server(): def __init__(self,cutted_list=[]): print "... Impossible ... Could cutted_list be changed?:", cutted_list self.cutted_list = cutted_list print "Soooooo strange, am I [] or ['cuu']? :", self.cutted_list self.stored_previous_partial = '' self.new_partial = '' self.singleton_factory = StateMachineSingletonFactory() print "Finished singleton_factory, am I changed at cutted_list? :", self.cutted_list def file_handler(self,data_path): self.data_path = data_path f = open(data_path,'r') self.lines = f.readlines() f.close() def slice_lines(self): # new line would be parsed as a new-line-word. for line in self.lines: self.file_line = line if '\n' == line: self.cutted_list.append('\n') continue self.prepare_line(line) self.slice_line() self.handle_result() def prepare_line(self,line): line = line.rstrip('\n') self.prepared_line = line def slice_line(self): self.parse_line(self.prepared_line) self.handle_package() def parse_line(self,line): self.re_parse_line(line) def simple_parse_line(self,line): splitter = " " # 3 spaces self.parsed_sequence = line.split(splitter) def re_parse_line(self,line): delimiter = re.compile("\s{3}") self.parsed_sequence = re.split(delimiter, line) def handle_package(self): assert self.stored_previous_partial is not None self.take_machine() self.update_machine_data_before_slice() self.sm.do_slice() def take_machine(self): #SliceMachineSingletonFactory.get_state_machine self.mark = self.parsed_sequence[1] self.sm = self.singleton_factory.dispatch_machine(self.mark) def update_machine_data_before_slice(self): self.sm.previous_partial = self.stored_previous_partial self.sm.parsed_sequence = self.parsed_sequence self.sm.mark = self.mark def handle_result(self): assert len(self.sm.slice_result) > 0 self.slice_result = self.sm.slice_result self.update_server_data() def update_server_data(self): self.update_cutted_list() self.stored_previous_partial = self.slice_result[1] self.cutted = self.slice_result[0] def update_cutted_list(self): cutted = self.sm.slice_result[0] if '' != cutted: self.cutted_list.append(cutted) def sliced_list_to_line(self): lines = "" for line in self.cutted_list: if "\n" != line: lines = lines + line + "\n" return lines