Best Python code snippet using fMBT_python
lsts.py
Source:lsts.py
...70props_by_states(r)[42]71returns list of numbers of propositions true in state 42. To convert72the number of a state proposition to a name, index alphabetically73ordered list of state proposition keys with it:74stateproplist=r.get_stateprops().keys()75stateproplist.sort()76name_of_prop=stateproplist[num_of_a_stateprop]77"""78version="0.522 svn"79# 0.490 -> 0.522 support for dos lines (carriage returns are removed)80# 0.110 -> 0.490 support for multirow action names81# 0.55 -> 0.110 lsts file objects can be read and written already in82# the constructor.83# 0.53 -> 0.55 stateprops numbered in the order of occurence in the lsts file84# (alphabetical order). Documentation changed accordingly.85# 0.52 -> 0.53 bugfix: state props numbered off-by-one86# 0.52 -> 0.53 bugfix: reading multi-line transitions and state props87# 0.52 -> 0.53 props_by_states-helper function88# 0.52 -> 0.53 psyco is used if possible89# 0.50 -> 0.52 added support for state prop ranges "x..y"90from sys import stderr91class fakefile:92 """93 fakefile(string) is a file-like object that contains the94 string. It implements readline and write methods and nothing else.95 This class can be used as input and output file of the reader and96 the writer classes. The contents of the file are in attribute s.97 """98 def __init__(self,lstscontents):99 self.s=lstscontents100 def readline(self):101 l=self.s[:self.s.find("\n")+1]102 self.s=self.s[self.s.find("\n")+1:]103 return l104 def write(self,s):105 self.s+=s106def props_by_states(lsts_object):107 """props_by_states(lsts_object) -> state_table108 If propositions x and y (and no other propositions) are true in109 state s, then state_table[s]==[x,y]. s, x and y are natural110 numbers. To obtain the name of the proposition x, use111 stateproplist=r.get_stateprops().keys()112 stateproplist.sort()113 name_of_prop=stateproplist[num_of_a_stateprop]114 """115 statetbl=[ [] for x in xrange(lsts_object.get_header().state_cnt)]116 propdict=lsts_object.get_stateprops()117 propkeys = propdict.keys()118 propkeys.sort()119 for propindex,prop in enumerate(propkeys):120 for state in propdict[prop]:121 statetbl[state].append(propindex)122 return statetbl123class _header:124 pass125class lsts:126 def __init__(self):127 self._history=[]128 self._header=_header()129 self._header.state_cnt=0130 self._header.action_cnt=0131 self._header.transition_cnt=0132 self._header.initial_states=0133 self._actionnames=[]134 self._transitions=[]135 self._stateprops={} # state prop name -> list of states136 self._layout=[]137 def set_actionnames(self,actionnames):138 """139 Parameters:140 - actionnames is a list of strings141 Notes:142 This method modifies Action_cnt field in the header.143 """144 if len(actionnames) > 0 and actionnames[0]=="tau":145 self._actionnames=actionnames146 else:147 stderr.write('LSTS.PY: warning: set_actionnames did not receive "tau".\n')148 self._actionnames=["tau"]+actionnames149 self._header.action_cnt=len(self._actionnames)-1150 def set_transitions(self,transitions):151 """152 Parameters:153 - transitions should be a list of list of pairs, where154 transitions[i] is a list of pairs (dest_state, action_index)155 that describe leaving transitions from state i. If there are156 no transitions from state i, then transitions[i] is empty157 list.158 dest_state and action_index are indexes to transitions and159 actionnames lists. That is, they may have values from 0 to160 len(list)-1.161 Notes:162 This method modifies State_cnt and Transition_cnt fields in163 the header.164 """165 self._transitions=transitions166 self._header.state_cnt=len(transitions)167 self._header.transition_cnt=0168 for s in transitions:169 self._header.transition_cnt+=len(s)170 def set_stateprops(self,stateprops):171 """172 Parameters:173 - stateprops should be a dictionary whose keys are state174 proposition names and values are lists of state numbers.175 Notes:176 This method modifies State_prop_cnt field in the header.177 """178 self._stateprops=stateprops179 self._header.state_prop_cnt=len(self._stateprops)180 def set_layout(self,layout):181 """182 Parameters:183 - layout should be a list of pairs (xcoord, ycoord), indexed184 with state numbers.185 """186 self._layout=layout187 def get_actionnames(self):188 return self._actionnames189 def get_transitions(self):190 return self._transitions191 def get_stateprops(self):192 return self._stateprops193 def get_layout(self):194 return self._layout195 def get_history(self):196 return self._history197 def get_header(self):198 return self._header199class writer(lsts):200 """201 LSTS writer class202 """203 def __init__(self,file=None,lsts_object=None):204 """205 Parameters:206 - Optional parameter 'file' should provide method207 'write'. Output will be written to this object. Valid objects208 are, for example, files opened for writing and sys.stdout.209 - Optional parameter lsts_object should be an instance of lsts210 (or reader or writer) class. New header will be automatically211 generated based on action names and transitions of the lsts212 object.213 - If both optional arguments are provided, the lsts object is214 immediately written to the file. In this case, for backward215 compatibility, the first call of write method will not do216 anything the writing target is the same file object.217 """218 self._written_in_constructor=None # this file has been written219 lsts.__init__(self)220 self.__file=file221 if isinstance(lsts_object,lsts):222 self.set_actionnames( lsts_object.get_actionnames() )223 self.set_transitions( lsts_object.get_transitions() )224 self.set_stateprops( lsts_object.get_stateprops() )225 self.set_layout( lsts_object.get_layout() )226 self._header.initial_states=lsts_object.get_header().initial_states227 if file!=None and lsts_object!=None: # write immediately228 self.write()229 self._written_in_constructor=file230 def write(self,file=None,stateprop_order=None):231 """232 Parameters:233 - optional parameter file is the same as in __init__.234 Notes:235 Writes all lsts information to the given file object.236 """237 if not file:238 file=self.__file...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!