Best Python code snippet using gherkin-python
TIDclass.py
Source:TIDclass.py
...9 self.filename = filename10 # @staticmethod 11 def get_layout_count(self,layout):12 return len(layout)13 def get_line_text(self,layout,line_number):14 i=115 for lt_obj in layout:16 if isinstance(lt_obj, LTTextBox) :17 if i==line_number :18 # print('--in function--%s' % i)19 x=lt_obj.get_text()20 return x.strip()21 i=i+122 return ''23 def getInfo(self):24 fp = open(self.filename, 'rb')25 parser = PDFParser(fp)26 doc = PDFDocument()27 parser.set_document(doc)28 doc.set_parser(parser)29 doc.initialize('')30 rsrcmgr = PDFResourceManager()31 laparams = LAParams()32 device = PDFPageAggregator(rsrcmgr, laparams=laparams)33 interpreter = PDFPageInterpreter(rsrcmgr, device)34 # Process each page contained in the document.35 images_folder=""36 text_content = []37 # print ("Info")38 import re39 rex = re.compile("^[0-9]{2}[A-Z][0-9]{2}[A-Z][0-9]$")40 for page in doc.get_pages():41 interpreter.process_page(page)42 layout = device.get_result()43 layout_count = self.get_layout_count(layout)44 i=145 # print ('LayOut Count : %s' % layout_count)46 # if layout_count =9 , means Full1Box (IN) or MTY1Box(OUT)47 company=self.get_company(layout,layout_count)48 lpn = self.get_license_plate_number(layout,layout_count)49 50 contain1_no = self.get_container1(layout,layout_count)51 contain1_loc = self.get_location1(layout,layout_count)52 contain1_seal = self.get_seal_number1(layout,layout_count)53 contain2_no = self.get_container2(layout,layout_count)54 contain2_loc = self.get_location2(layout,layout_count)55 contain2_seal = self.get_seal_number2(layout,layout_count)56 call_card = self.get_call_card(layout,layout_count)57 time_stamp =self.get_timestamp(layout,layout_count)58 data = {59 'filename' : self.filename,60 'type' : layout_count,61 'company' : company ,62 'license_no': lpn,63 'call_card':call_card,64 'time_stamp' : time_stamp65 }66 if layout_count == 9 :67 emptyFixContainer=False68 # print("Full (IN)/Empty(Out) 1 Box TID")69 c2_text = self.get_line_text(layout,2)70 c3_text = self.get_line_text(layout,3)71 # if len(c2_text) > 1 :72 # direction='in'73 # # print ('Full (IN) 1 box')74 # else:75 # direction='out'76 # # print ('Full (OUT) 1 box')77 if c2_text=='-' and c3_text=='-' :78 direction='out'79 # print ('Full (IN) 1 box')80 else:81 y = c2_text.split(' ')82 if len(y) > 1 :83 direction='in'84 else :85 emptyFixContainer = True86 direction='out'87 # print ('Full (OUT) 1 box')88 if direction=='in':89 data[direction]={}90 data[direction]['item1']={'container_no' : contain1_no,91 'location' : contain1_loc,92 'seal' : contain1_seal}93 if direction=='out':94 # either 1 or 2 Full out95 c4_text = self.get_line_text(layout,4)96 y= c4_text.split(' ')97 data[direction]={}98 if emptyFixContainer :99 data[direction]['item1']={'container_no' : contain1_loc,100 'location' : '',101 'seal' : contain1_seal,102 'line3':''}103 else:104 if '-' in c4_text :105 data[direction]['item1']={'container_no' : contain1_no,106 'location' : contain1_loc,107 'seal' : contain1_seal,108 'line3':''}109 else:110 data[direction]['item1']={'container_no' : contain1_no,111 'location' : contain1_loc,112 'seal' : contain1_seal,113 'line3' : ''}114 data[direction]['item2']={'container_no' : contain2_no,115 'location' : contain2_loc,116 'seal' : contain2_seal,117 'line3' : ''}118 # data[direction]['item1']={'container_no' : contain1_no,119 # 'location' : contain1_loc,120 # 'seal' : contain1_seal}121 122 if layout_count == 7 :123 data['in']={}124 data['in']['item1']={'container_no' : contain1_no,125 'location' : contain1_loc,126 'seal' : contain1_seal}127 if self.get_line_text(layout,3) == '-' :128 data['in']['item2']={'container_no' : contain2_no,129 'location' : contain2_loc,130 'seal' : contain2_seal}131 else:132 data['out']={}133 data['out']['item1'] ={'container_no':contain2_no,134 'location' : contain2_loc,135 'seal':contain2_seal,136 'line3':''}137 # print('')138 if layout_count == 10 :139 data['in']={}140 data['in']['item1']={'container_no' : contain1_no,141 'location' : contain1_loc,142 'seal' : contain1_seal}143 tmp=self.get_line3text1(layout,layout_count)144 if rex.match(tmp):145 location=tmp146 else:147 location=''148 data['out']={}149 data['out']['item1']={150 'container_no' : self.get_service1(layout,layout_count),151 'seal' : self.get_container1_type(layout,layout_count),152 'line3' : self.get_line3text1(layout,layout_count),153 'location' : location}154 tmp=self.get_line3text2(layout,layout_count)155 if rex.match(tmp):156 location=tmp157 else:158 location=''159 data['out']['item2']={160 'container_no' : self.get_service2(layout,layout_count),161 'seal' : self.get_container2_type(layout,layout_count),162 'line3' : self.get_line3text2(layout,layout_count),163 'location' : location}164 if layout_count == 11 :165 data['out']={}166 tmp=self.get_line3text1(layout,layout_count)167 if rex.match(tmp):168 location=tmp169 else:170 location=''171 data['out']['item1']={172 'container_no' : self.get_service1(layout,layout_count),173 'seal' : self.get_container1_type(layout,layout_count),174 'line3' : self.get_line3text1(layout,layout_count),175 'location' : location176 }177 if self.get_line_text(layout,3)=='-' :178 tmp=self.get_line3text2(layout,layout_count)179 if rex.match(tmp):180 location=tmp181 else:182 location=''183 data['out']['item2']={184 'container_no' : self.get_service2(layout,layout_count),185 'seal' : self.get_container2_type(layout,layout_count),186 'line3' : self.get_line3text2(layout,layout_count),187 'location' : location}188 if layout_count == 12 :189 data['out']={}190 tmp=self.get_line3text1(layout,layout_count)191 if rex.match(tmp):192 location=tmp193 else:194 location=''195 data['out']['item1']={196 'container_no' : self.get_service1(layout,layout_count),197 'seal' : self.get_container1_type(layout,layout_count),198 'line3' : self.get_line3text1(layout,layout_count),199 'location' :location}200 tmp=self.get_line3text2(layout,layout_count)201 if rex.match(tmp):202 location=tmp203 else :204 location=''205 data['out']['item2']={206 'container_no' : self.get_service2(layout,layout_count),207 'seal' : self.get_container2_type(layout,layout_count),208 'line3' : self.get_line3text2(layout,layout_count),209 'location' :location}210 return data211 # For View file details212 # if argv[1] == 'y':213 # for lt_obj in layout:214 # if isinstance(lt_obj, LTTextBox) :215 # line_text=lt_obj.get_text()216 # print('%s) %s' % (i,line_text))217 # i=i+1218 # print('-------------End Page----------------------------------------------')219 220 def getRaw(self):221 fp = open(self.filename, 'rb')222 parser = PDFParser(fp)223 doc = PDFDocument()224 parser.set_document(doc)225 doc.set_parser(parser)226 doc.initialize('')227 rsrcmgr = PDFResourceManager()228 laparams = LAParams()229 device = PDFPageAggregator(rsrcmgr, laparams=laparams)230 interpreter = PDFPageInterpreter(rsrcmgr, device)231 # Process each page contained in the document.232 images_folder=""233 text_content = []234 # print ("Info")235 import re236 rex = re.compile("^[0-9]{2}[A-Z][0-9]{2}[A-Z][0-9]$")237 i=0238 for page in doc.get_pages():239 interpreter.process_page(page)240 layout = device.get_result()241 for lt_obj in layout:242 if isinstance(lt_obj, LTTextBox) :243 line_text=lt_obj.get_text()244 print('%s) %s' % (i,line_text))245 i=i+1246 # tid_type 247 # 9 : Full(in) 1 box OR Empty(out) 1 box248 # 7 : Full(in) 2 box249 #12 : Empty(out) 2 box250 def get_company(self,layout,tid_type):251 line_text = self.get_line_text(layout,1)252 x = line_text.split(' ')253 return x[0].strip()254 # if tid_type == 9 or tid_type == 7 :255 # return x[0].strip()256 # if tid_type == 12 :257 # return x[0].strip()258 def get_license_plate_number(self,layout,tid_type):259 if tid_type == 7 or tid_type == 10:260 line_text = self.get_line_text(layout,1)261 x = line_text.split(' ')262 return x[len(x)-1].strip().replace(' ','')263 if tid_type == 9 :264 if len(self.get_line_text(layout,1))>2:265 line_text = self.get_line_text(layout,2)266 x = line_text.split(' ')267 return x[0].strip().replace(' ','')268 else :269 line_text = self.get_line_text(layout,1)270 x = line_text.split(' ')271 return x[len(x)-1].strip().replace(' ','')272 if tid_type == 11 :273 line_text = self.get_line_text(layout,3) #Check 274 if line_text == '-':275 line_text = self.get_line_text(layout,1)276 x = line_text.split(' ')277 return x[len(x)-1].strip().replace(' ','')278 else:279 line_text = self.get_line_text(layout,2)280 return line_text.strip().replace(' ','')281 if tid_type == 12 :282 line_text = self.get_line_text(layout,2)283 x = line_text.split(' ')284 return x[0].strip().replace(' ','')285 def get_container1(self,layout,tid_type):286 if tid_type == 9 or tid_type == 7 :287 line_text = self.get_line_text(layout,2).strip()288 # print (line_text)289 if len(line_text) == 1:290 # return self.get_line_text(layout,4).strip(x = line_text.split(' ')).replace(' ','').replace('-','')291 line_text = self.get_line_text(layout,4).strip()292 x = line_text.split(' ')293 return x[0].strip().replace(' ','')294 else:295 x = line_text.split(' ')296 return x[0].strip().replace(' ','')297 if tid_type == 10 :298 line_text = self.get_line_text(layout,3).strip()299 x = line_text.split(' ')300 return x[0].strip().replace(' ','')301 def get_container2(self,layout,tid_type):302 if tid_type == 7 :303 # print ('Line #3 %s' % self.get_line_text(layout,3).strip())304 if self.get_line_text(layout,3).strip()=='-':305 line_text = self.get_line_text(layout,2).strip()306 x = line_text.split(' ')307 return x[1].strip().replace(' ','')308 else:309 # print ('get Container#2')310 line_text = self.get_line_text(layout,3).strip()311 x = line_text.split(' ')312 return x[0].strip().replace(' ','')313 if tid_type == 9 :314 line_text = self.get_line_text(layout,4).strip()315 x = line_text.split(' ')316 return x[len(x)-1].strip().replace(' ','')317 def get_location1(self,layout,tid_type):318 if tid_type == 7 : #Full(in)2 boxes319 line_text = self.get_line_text(layout,4).strip()320 x = line_text.split(' ')321 return x[0].strip().replace(' ','')322 if tid_type == 10 : 323 line_text = self.get_line_text(layout,7).strip()324 x = line_text.split(' ')325 return x[0].strip().replace(' ','')326 if tid_type == 9 :327 line_text = self.get_line_text(layout,5).strip()328 if len(line_text) > 1 :329 330 return line_text.strip().replace(' ','').replace('-','')331 else :332 line_text = self.get_line_text(layout,7).strip()333 x = line_text.split(' ')334 return x[0].strip().replace(' ','').replace('-','')335 # return self.get_line_text(layout,7).strip().replace(' ','').replace('-','')336 # print (line_text)337 # if len(line_text) == 1:338 # return get_line_text(layout,4).strip().replace(' ','').replace('-','')339 # else:340 341 # return x[0].strip().replace(' ','')342 def get_location2(self,layout,tid_type):343 if tid_type == 7 : #Full(in)2 boxes344 # print ('line #3 %s ' % self.get_line_text(layout,3).strip() )345 if self.get_line_text(layout,3).strip()=='-' :346 line_text = self.get_line_text(layout,4).strip()347 x = line_text.split(' ')348 return x[2].strip().replace(' ','')349 else :350 line_text = self.get_line_text(layout,5).strip()351 # print ('line #5 %s ' % line_text )352 x = line_text.split(' ')353 return x[0].strip().replace(' ','')354 if tid_type == 9 : #Full(out)2 boxes355 line_text = self.get_line_text(layout,7).strip()356 x = line_text.split(' ')357 return x[len(x)-1].strip().replace(' ','')358 def get_seal_number1(self,layout,tid_type):359 if tid_type == 9 or tid_type == 7 :360 line_text = self.get_line_text(layout,6).strip()361 # print (line_text)362 if len(line_text) == 1:363 #1 Full Box IN364 # return self.get_line_text(layout,8).strip().replace(' ','').replace('-','')365 line_text = self.get_line_text(layout,8).strip()366 x = line_text.split(' ')367 return x[0].strip().replace(' ','').replace('-','')368 else:369 #2 Full boxes IN370 x = line_text.split(' ')371 return x[0].strip().replace(' ','').replace('-','')372 if tid_type == 10 :373 line_text = self.get_line_text(layout,9).strip()374 x = line_text.split(' ')375 return x[0].strip().replace(' ','').replace('-','')376 def get_seal_number2(self,layout,tid_type):377 if tid_type == 7 :378 line_text = self.get_line_text(layout,6).strip()379 #2 Full boxes IN380 x = line_text.split(' ')381 return x[len(x)-1].strip().replace(' ','').replace('-','')382 if tid_type == 9 : #Full(out)2 boxes383 line_text = self.get_line_text(layout,8).strip()384 x = line_text.split(' ')385 return x[len(x)-1].strip().replace(' ','')386 387 def get_call_card(self,layout,tid_type):388 if tid_type == 9 or tid_type == 7 :389 line_text = self.get_line_text(layout,9).strip()390 # print (line_text)391 if len(line_text) == 0:392 #1 Full Box IN393 line_text=self.get_line_text(layout,7).strip()394 x = line_text.split(' ')395 return x[0].strip().replace(' ','').replace('-','')396 else:397 #2 Full boxes IN398 x = line_text.split(' ')399 return x[0].strip().replace(' ','').replace('-','')400 if tid_type == 10 :401 line_text = self.get_line_text(layout,10).strip()402 x = line_text.split(' ')403 return x[0].strip().replace(' ','').replace('-','')404 if tid_type == 11 :405 line_text = self.get_line_text(layout,11).strip()406 x = line_text.split(' ')407 return x[0].strip().replace(' ','').replace('-','')408 if tid_type == 12 :409 line_text = self.get_line_text(layout,12).strip()410 x = line_text.split(' ')411 return x[0].strip().replace(' ','').replace('-','')412 def get_timestamp(self,layout,tid_type):413 if tid_type == 9 or tid_type == 7 :414 line_text = self.get_line_text(layout,9).strip()415 # print (line_text)416 if len(line_text) == 0:417 #1 Full Box IN418 line_text=self.get_line_text(layout,7).strip()419 x = line_text.split(' ')420 # print (len(x))421 return x[len(x)-1].strip().replace('-','')422 else:423 #2 Full boxes IN424 x = line_text.split(' ')425 # print (len(x))426 return x[len(x)-1].strip().replace('-','')427 if tid_type == 10 :428 line_text = self.get_line_text(layout,10).strip()429 x = line_text.split(' ')430 return x[len(x)-1].strip().replace('-','')431 if tid_type == 11 :432 line_text = self.get_line_text(layout,11).strip()433 x = line_text.split(' ')434 return x[len(x)-1].strip().replace(' ','').replace('-','')435 if tid_type == 12 :436 line_text = self.get_line_text(layout,12).strip()437 x = line_text.split(' ')438 return x[len(x)-1].strip().replace(' ','').replace('-','')439 440 def get_service1(self,layout,tid_type):441 if tid_type == 10 :442 line_text = self.get_line_text(layout,2).strip()443 x = line_text.split(' ')444 return x[0].strip().replace(' ','')445 if tid_type == 11 :446 line_text = self.get_line_text(layout,3).strip()447 if line_text=='-':448 line_text = self.get_line_text(layout,2).strip()449 x = line_text.split(' ')450 return x[0].strip().replace(' ','')451 else:452 line_text = self.get_line_text(layout,3).strip()453 return line_text.strip().replace(' ','')454 if tid_type == 12 :455 line_text = self.get_line_text(layout,3).strip()456 x = line_text.split(' ')457 return x[0].strip().replace(' ','')458 def get_service2(self,layout,tid_type):459 if tid_type == 10 or tid_type == 11 :460 line_text = self.get_line_text(layout,2).strip()461 x = line_text.split(' ')462 return x[len(x)-1].strip().replace(' ','')463 if tid_type == 12 :464 line_text = self.get_line_text(layout,3).strip()465 x = line_text.split(' ')466 return x[len(x)-1].strip().replace(' ','')467 def get_container1_type(self,layout,tid_type):468 if tid_type == 10 :469 line_text1 = self.get_line_text(layout,4).strip()470 x = line_text1.split(' ')471 line_text2 = self.get_line_text(layout,5).strip()472 y = line_text2.split(' ')473 return '%s %s' % (x[0].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))474 if tid_type == 11 :475 line_text = self.get_line_text(layout,3).strip()476 if line_text=='-':477 line_text1 = self.get_line_text(layout,5).strip()478 x = line_text1.split(' ')479 line_text2 = self.get_line_text(layout,6).strip()480 y = line_text2.split(' ')481 return '%s %s' % (x[0].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))482 else:483 line_text1 = self.get_line_text(layout,6).strip()484 x = line_text1.split(' ')485 line_text2 = self.get_line_text(layout,7).strip()486 y = line_text2.split(' ')487 return '%s %s' % (x[0].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))488 if tid_type == 12 :489 line_text1 = self.get_line_text(layout,6).strip()490 x = line_text1.split(' ')491 line_text2 = self.get_line_text(layout,7).strip()492 y = line_text2.split(' ')493 return '%s %s' % (x[0].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))494 def get_container2_type(self,layout,tid_type):495 if tid_type == 10 :496 line_text1 = self.get_line_text(layout,5).strip()497 x = line_text1.split(' ')498 line_text2 = self.get_line_text(layout,6).strip()499 y = line_text2.split(' ')500 return '%s %s' % (x[len(x)-1].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))501 if tid_type == 11 :502 line_text1 = self.get_line_text(layout,6).strip()503 x = line_text1.split(' ')504 line_text2 = self.get_line_text(layout,7).strip()505 y = line_text2.split(' ')506 return '%s %s' % (x[len(x)-1].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))507 if tid_type == 12 :508 line_text1 = self.get_line_text(layout,7).strip()509 x = line_text1.split(' ')510 line_text2 = self.get_line_text(layout,8).strip()511 y = line_text2.split(' ')512 return '%s %s' % (x[len(x)-1].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))513 def get_line3text1(self,layout,tid_type):514 if tid_type == 10 :515 line_text1 = self.get_line_text(layout,8).strip()516 x = line_text1.split(' ')517 return '%s' % (x[0].strip().replace(' ',''))518 if tid_type == 11 :519 line_text1 = self.get_line_text(layout,10).strip()520 x = line_text1.split(' ')521 return '%s' % (x[0].strip().replace(' ',''))522 if tid_type == 12 :523 line_text1 = self.get_line_text(layout,11).strip()524 x = line_text1.split(' ')525 return '%s' % (x[0].strip().replace(' ',''))526 def get_line3text2(self,layout,tid_type):527 if tid_type == 10 :528 line_text1 = self.get_line_text(layout,8).strip()529 x = line_text1.split(' ')530 return '%s' % (x[len(x)-1].strip().replace(' ',''))531 if tid_type == 11 :532 line_text1 = self.get_line_text(layout,10).strip()533 x = line_text1.split(' ')534 return '%s' % (x[len(x)-1].strip().replace(' ',''))535 if tid_type == 12 :536 line_text1 = self.get_line_text(layout,11).strip()537 x = line_text1.split(' ')538 return '%s' % (x[len(x)-1].strip().replace(' ',''))539 def _parse_pages (doc, images_folder):540 """With an open PDFDocument object, get the pages, parse each one, and return the entire text541 [this is a higher-order function to be passed to with_pdf()]"""542 rsrcmgr = PDFResourceManager()543 laparams = LAParams()544 device = PDFPageAggregator(rsrcmgr, laparams=laparams)545 interpreter = PDFPageInterpreter(rsrcmgr, device)546 text_content = [] # a list of strings, each representing text collected from each page of the doc547 for i, page in enumerate(doc.get_pages()):548 interpreter.process_page(page)549 # receive the LTPage object for this page550 layout = device.get_result()...
history.py
Source:history.py
...40 def get_history_item(self, index):41 '''Return the current contents of history item at index (starts with index 1).'''42 item = self.history[index - 1]43 log("get_history_item: index:%d item:%r"%(index, item))44 return item.get_line_text()45 def set_history_length(self, value):46 log("set_history_length: old:%d new:%d"%(self._history_length, value))47 self._history_length = value48 def get_history_cursor(self):49 value = self._history_cursor50 log("get_history_cursor:%d"%value)51 return value52 def set_history_cursor(self, value):53 log("set_history_cursor: old:%d new:%d"%(self._history_cursor, value))54 self._history_cursor = value55 56 history_length = property(get_history_length, set_history_length)57 history_cursor = property(get_history_cursor, set_history_cursor)58 def clear_history(self):59 '''Clear readline history.'''60 self.history[:] = []61 self.history_cursor = 062 def read_history_file(self, filename=None): 63 '''Load a readline history file.'''64 if filename is None:65 filename = self.history_filename66 try:67 for line in open(filename, 'r'):68 self.add_history(lineobj.ReadLineTextBuffer(ensure_unicode(line.rstrip())))69 except IOError:70 self.history = []71 self.history_cursor = 072 def write_history_file(self, filename = None): 73 '''Save a readline history file.'''74 if filename is None:75 filename = self.history_filename76 fp = open(filename, 'wb')77 for line in self.history[-self.history_length:]:78 fp.write(ensure_str(line.get_line_text()))79 fp.write('\n'.encode('ascii'))80 fp.close()81 def add_history(self, line):82 '''Append a line to the history buffer, as if it was the last line typed.'''83 if not hasattr(line, "get_line_text"):84 line = lineobj.ReadLineTextBuffer(line)85 if not line.get_line_text():86 pass87 elif len(self.history) > 0 and self.history[-1].get_line_text() == line.get_line_text():88 pass89 else:90 self.history.append(line)91 self.history_cursor = len(self.history)92 def previous_history(self, current): # (C-p)93 '''Move back through the history list, fetching the previous command. '''94 if self.history_cursor == len(self.history):95 self.history.append(current.copy()) #do not use add_history since we do not want to increment cursor96 97 if self.history_cursor > 0:98 self.history_cursor -= 199 current.set_line(self.history[self.history_cursor].get_line_text())100 current.point = lineobj.EndOfLine101 def next_history(self, current): # (C-n)102 '''Move forward through the history list, fetching the next command. '''103 if self.history_cursor < len(self.history) - 1:104 self.history_cursor += 1105 current.set_line(self.history[self.history_cursor].get_line_text())106 def beginning_of_history(self): # (M-<)107 '''Move to the first line in the history.'''108 self.history_cursor = 0109 if len(self.history) > 0:110 self.l_buffer = self.history[0]111 def end_of_history(self, current): # (M->)112 '''Move to the end of the input history, i.e., the line currently113 being entered.'''114 self.history_cursor = len(self.history)115 current.set_line(self.history[-1].get_line_text())116 def reverse_search_history(self, searchfor, startpos=None):117 if startpos is None:118 startpos = self.history_cursor119 origpos = startpos120 result = lineobj.ReadLineTextBuffer("")121 for idx, line in list(enumerate(self.history))[startpos:0:-1]:122 if searchfor in line:123 startpos = idx124 break125 #If we get a new search without change in search term it means126 #someone pushed ctrl-r and we should find the next match127 if self.last_search_for == searchfor and startpos > 0:128 startpos -= 1129 for idx, line in list(enumerate(self.history))[startpos:0:-1]:130 if searchfor in line:131 startpos = idx132 break133 if self.history: 134 result = self.history[startpos].get_line_text()135 else:136 result = ""137 self.history_cursor = startpos138 self.last_search_for = searchfor139 log("reverse_search_history: old:%d new:%d result:%r"%(origpos, self.history_cursor, result))140 return result141 142 def forward_search_history(self, searchfor, startpos=None):143 if startpos is None:144 startpos = min(self.history_cursor, max(0, self.get_current_history_length()-1))145 origpos = startpos146 147 result = lineobj.ReadLineTextBuffer("")148 for idx, line in list(enumerate(self.history))[startpos:]:149 if searchfor in line:150 startpos = idx151 break152 #If we get a new search without change in search term it means153 #someone pushed ctrl-r and we should find the next match154 if self.last_search_for == searchfor and startpos < self.get_current_history_length()-1:155 startpos += 1156 for idx, line in list(enumerate(self.history))[startpos:]:157 if searchfor in line:158 startpos = idx159 break160 if self.history: 161 result = self.history[startpos].get_line_text()162 else:163 result = ""164 self.history_cursor = startpos165 self.last_search_for = searchfor166 return result167 def _search(self, direction, partial):168 try:169 if (self.lastcommand != self.history_search_forward and170 self.lastcommand != self.history_search_backward):171 self.query = ''.join(partial[0:partial.point].get_line_text())172 hcstart = max(self.history_cursor,0) 173 hc = self.history_cursor + direction174 while (direction < 0 and hc >= 0) or (direction > 0 and hc < len(self.history)):175 h = self.history[hc]176 if not self.query:177 self.history_cursor = hc178 result = lineobj.ReadLineTextBuffer(h, point=len(h.get_line_text()))179 return result180 elif (h.get_line_text().startswith(self.query) and (h != partial.get_line_text())):181 self.history_cursor = hc182 result = lineobj.ReadLineTextBuffer(h, point=partial.point)183 return result184 hc += direction185 else:186 if len(self.history) == 0:187 pass 188 elif hc >= len(self.history) and not self.query:189 self.history_cursor = len(self.history)190 return lineobj.ReadLineTextBuffer("", point=0)191 elif self.history[max(min(hcstart, len(self.history) - 1), 0)]\192 .get_line_text().startswith(self.query) and self.query:193 return lineobj.ReadLineTextBuffer(self.history\194 [max(min(hcstart, len(self.history) - 1),0)],195 point = partial.point)196 else: 197 return lineobj.ReadLineTextBuffer(partial, 198 point=partial.point)199 return lineobj.ReadLineTextBuffer(self.query, 200 point=min(len(self.query),201 partial.point))202 except IndexError:203 raise204 def history_search_forward(self, partial): # ()205 '''Search forward through the history for the string of characters206 between the start of the current line and the point. This is a...
test_history.py
Source:test_history.py
...24 hist = self.q25 assert hist.history_cursor == 626 l = RL("")27 hist.previous_history(l)28 assert l.get_line_text() == "ako"29 hist.previous_history(l)30 assert l.get_line_text() == "bbb"31 hist.previous_history(l)32 assert l.get_line_text() == "akca"33 hist.previous_history(l)34 assert l.get_line_text() == "aaca"35 hist.previous_history(l)36 assert l.get_line_text() == "aaba"37 hist.previous_history(l)38 assert l.get_line_text() == "aaaa"39 hist.previous_history(l)40 assert l.get_line_text() == "aaaa"41 def test_next_history (self):42 hist=self.q43 hist.beginning_of_history()44 assert hist.history_cursor==045 l=RL("")46 hist.next_history(l)47 assert l.get_line_text()=="aaba"48 hist.next_history(l)49 assert l.get_line_text()=="aaca"50 hist.next_history(l)51 assert l.get_line_text()=="akca"52 hist.next_history(l)53 assert l.get_line_text()=="bbb"54 hist.next_history(l)55 assert l.get_line_text()=="ako"56 hist.next_history(l)57 assert l.get_line_text()=="ako"58class Test_prev_next_history(unittest.TestCase):59 t = "test text"60 def setUp(self):61 self.q = q = LineHistory()62 for x in ["aaaa","aaba","aaca","akca","bbb","ako"]:63 q.add_history(RL(x))64 65 def test_history_search_backward (self):66 q = LineHistory()67 for x in ["aaaa","aaba","aaca"," aacax","akca","bbb","ako"]:68 q.add_history(RL(x))69 a=RL("aa",point=2)70 for x in ["aaca","aaba","aaaa","aaaa"]:71 res=q.history_search_backward(a)72 assert res.get_line_text()==x73 74 def test_history_search_forward (self):75 q = LineHistory()76 for x in ["aaaa","aaba","aaca"," aacax","akca","bbb","ako"]:77 q.add_history(RL(x))78 q.beginning_of_history()79 a=RL("aa",point=2)80 for x in ["aaba","aaca","aaca"]:81 res=q.history_search_forward(a)82 assert res.get_line_text()==x83class Test_history_search_incr_fwd_backwd(unittest.TestCase):84 def setUp(self):85 self.q = q = LineHistory()86 for x in ["aaaa","aaba","aaca","akca","bbb","ako"]:87 q.add_history(RL(x))88 def test_backward_1(self):89 q = self.q90 self.assertEqual(q.reverse_search_history("b"), "bbb") 91 self.assertEqual(q.reverse_search_history("b"), "aaba") 92 self.assertEqual(q.reverse_search_history("bb"), "aaba") 93 def test_backward_2(self):94 q = self.q95 self.assertEqual(q.reverse_search_history("a"), "ako") 96 self.assertEqual(q.reverse_search_history("aa"), "aaca") ...
history_test.py
Source:history_test.py
...25 hist=self.q26 assert hist.history_cursor==627 l=RL("")28 hist.previous_history(l)29 assert l.get_line_text()=="ako"30 hist.previous_history(l)31 assert l.get_line_text()=="bbb"32 hist.previous_history(l)33 assert l.get_line_text()=="akca"34 hist.previous_history(l)35 assert l.get_line_text()=="aaca"36 hist.previous_history(l)37 assert l.get_line_text()=="aaba"38 hist.previous_history(l)39 assert l.get_line_text()=="aaaa"40 hist.previous_history(l)41 assert l.get_line_text()=="aaaa"42 def test_next_history (self):43 self.init_test()44 hist=self.q45 hist.beginning_of_history()46 assert hist.history_cursor==047 l=RL("")48 hist.next_history(l)49 assert l.get_line_text()=="aaba"50 hist.next_history(l)51 assert l.get_line_text()=="aaca"52 hist.next_history(l)53 assert l.get_line_text()=="akca"54 hist.next_history(l)55 assert l.get_line_text()=="bbb"56 hist.next_history(l)57 assert l.get_line_text()=="ako"58 hist.next_history(l)59 assert l.get_line_text()=="ako"60 def init_test2(self):61 self.q=q=LineHistory()62 for x in ["aaaa","aaba","aaca","akca","bbb","ako"]:63 q.add_history(RL(x))64 65 def test_history_search_backward (self):66 history._ignore_leading_spaces=False67 q=LineHistory()68 for x in ["aaaa","aaba","aaca"," aacax","akca","bbb","ako"]:69 q.add_history(RL(x))70 a=RL("aa",point=2)71 for x in ["aaca","aaba","aaaa","aaaa"]:72 res=q.history_search_backward(a)73 assert res.get_line_text()==x74 75 def test_history_search_forward (self):76 history._ignore_leading_spaces=False77 q=LineHistory()78 for x in ["aaaa","aaba","aaca"," aacax","akca","bbb","ako"]:79 q.add_history(RL(x))80 q.beginning_of_history()81 a=RL("aa",point=2)82 for x in ["aaba","aaca","aaca"]:83 res=q.history_search_forward(a)84 assert res.get_line_text()==x85#----------------------------------------------------------------------86# utility functions87#----------------------------------------------------------------------88if __name__ == '__main__':89 unittest.main()...
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!!