Best Python code snippet using pyatom_python
standingstest.py
Source:standingstest.py
...12 result = requests.get('https://api.deinsportplatz.de/api/v1.1/639703/matches/231/135',13 auth=('pytctest', 'tctest18'))14 if result.status_code == 200:15 for m in result.json().get('matches'):16 t1.add_match(_Match(m.get('teamHome'), m.get('fkTeam_home'),17 m.get('teamAway'), m.get('fkTeam_away'),18 m.get('goalsHome'), m.get('goalsAway')))19 tb = t1.get_table()20 self.assertEqual(tb[0].team_id, 2588)21 self.assertEqual(tb[0].points, 93)22 self.assertEqual(tb[0].goalDifference, 65)23 def testHandball1(self):24 m1 = _Match('Team 11', 11, 'Team 2', 2, 22, 24)25 m2 = _Match('Team 11', 11, 'Team 3', 3, 32, 29)26 m3 = _Match('Team 2', 2, 'Team 3', 3, 19, 21)27 m4 = _Match('Team 4', 4, 'Team 5', 5, 13, 13)28 m5 = _Match('Team 4', 4, 'Team 9', 9, 13, 12)29 t1 = Standings(TABLE_ALL, SPORTSTYPES.HANDBALL, CALCULATION_MODE_DIRECT_COMPARE)30 t1.add_match(m2)31 t1.add_match(m3)32 t1.add_match(m1)33 t1.add_match(m4)34 t1.add_match(m5)35 tb = t1.get_table()36 self.assertEqual(tb[0].team_id,4)37 self.assertEqual(tb[1].team_id,11)38 self.assertEqual(tb[2].team_id,2)39 self.assertEqual(tb[3].team_id,3)40 def testHandball2(self):41 # Handball 3. Liga Herren, Saison 2015/16, 17. Spieltag42 t1 = Standings(TABLE_ALL, SPORTSTYPES.HANDBALL, CALCULATION_MODE_DIRECT_COMPARE)43 t1.add_match(_Match('DHK Flensborg', 35551, 'SC Magdeburg II', 35561, 41, 23))44 t1.add_match(_Match('SV Meck.-Schwerin', 35651, 'TS GroÃburgwedel', 35661, 26, 17))45 t1.add_match(_Match('SV Anhalt Bernburg', 35631, 'Stralsunder HV', 35641, 20, 20))46 t1.add_match(_Match('HSV Insel Usedom', 35611, 'TSV Altenholz', 35621, 18, 25))47 t1.add_match(_Match('Füchse Berlin II', 35571, 'SG Fle-Ha II', 35581, 30, 20))48 t1.add_match(_Match('Eintracht Hildesheim', 35531, 'Dessau-Rosslauer HV', 35541, 25, 25))49 t1.add_match(_Match('TSV Burgdorf II', 35511, 'Oranienburger HC', 35521, 25, 22))50 t1.add_match(_Match('HSV Hannover', 35591, 'VfL Potsdam', 35601, 18, 26))51 t1.add_match(_Match('SG Fle-Ha II', 35581, 'HSV Hannover', 35591, 22, 24))52 t1.add_match(_Match('Dessau-Rosslauer HV', 35541, 'DHK Flensborg', 35551, 29, 23))53 t1.add_match(_Match('Oranienburger HC', 35521, 'Eintracht Hildesheim', 35531, 29, 26))54 t1.add_match(_Match('SC Magdeburg II', 35561, 'Füchse Berlin II', 35571, 41, 24))55 t1.add_match(_Match('TSV Altenholz', 35621, 'SV Anhalt Bernburg', 35631, 30, 22))56 t1.add_match(_Match('TS GroÃburgwedel', 35661, 'TSV Burgdorf II', 35511, 27, 25))57 t1.add_match(_Match('Stralsunder HV', 35641, 'SV Meck.-Schwerin', 35651, 21, 24))58 t1.add_match(_Match('VfL Potsdam', 35601, 'HSV Insel Usedom', 35611, 26, 25))59 t1.add_match(_Match('DHK Flensborg', 35551, 'Oranienburger HC', 35521, 24, 25))60 t1.add_match(_Match('SV Meck.-Schwerin', 35651, 'TSV Altenholz', 35621, 24, 23))61 t1.add_match(_Match('HSV Hannover', 35591, 'SC Magdeburg II', 35561, 29, 31))62 t1.add_match(_Match('Füchse Berlin II', 35571, 'Dessau-Rosslauer HV', 35541, 29, 29))63 t1.add_match(_Match('SV Anhalt Bernburg', 35631, 'VfL Potsdam', 35601, 30, 23))64 t1.add_match(_Match('HSV Insel Usedom', 35611, 'SG Fle-Ha II', 35581, 22, 28))65 t1.add_match(_Match('TSV Burgdorf II', 35511, 'Stralsunder HV', 35641, 33, 23))66 t1.add_match(_Match('Eintracht Hildesheim', 35531, 'TS GroÃburgwedel', 35661, 29, 29))67 t1.add_match(_Match('Oranienburger HC', 35521, 'Füchse Berlin II', 35571, 32, 25))68 t1.add_match(_Match('TSV Altenholz', 35621, 'TSV Burgdorf II', 35511, 25, 27))69 t1.add_match(_Match('VfL Potsdam', 35601, 'SV Meck.-Schwerin', 35651, 30, 17))70 t1.add_match(_Match('SG Fle-Ha II', 35581, 'SV Anhalt Bernburg', 35631, 18, 19))71 t1.add_match(_Match('SC Magdeburg II', 35561, 'HSV Insel Usedom', 35611, 33, 23))72 t1.add_match(_Match('Dessau-Rosslauer HV', 35541, 'HSV Hannover', 35591, 35, 29))73 t1.add_match(_Match('Stralsunder HV', 35641, 'Eintracht Hildesheim', 35531, 27, 29))74 t1.add_match(_Match('TS GroÃburgwedel', 35661, 'DHK Flensborg', 35551, 27, 28))75 t1.add_match(_Match('SC Magdeburg II', 35561, 'SG Fle-Ha II', 35581, 33, 27))76 t1.add_match(_Match('TSV Burgdorf II', 35511, 'Eintracht Hildesheim', 35531, 32, 20))77 t1.add_match(_Match('TS GroÃburgwedel', 35661, 'Stralsunder HV', 35641, 27, 19))78 t1.add_match(_Match('HSV Hannover', 35591, 'SV Meck.-Schwerin', 35651, 37, 25))79 t1.add_match(_Match('HSV Insel Usedom', 35611, 'SV Anhalt Bernburg', 35631, 29, 27))80 t1.add_match(_Match('TSV Altenholz', 35621, 'VfL Potsdam', 35601, 30, 35))81 t1.add_match(_Match('Füchse Berlin II', 35571, 'DHK Flensborg', 35551, 34, 33))82 t1.add_match(_Match('Dessau-Rosslauer HV', 35541, 'Oranienburger HC', 35521, 29, 25))83 t1.add_match(_Match('SV Anhalt Bernburg', 35631, 'SC Magdeburg II', 35561, 23, 23))84 t1.add_match(_Match('Füchse Berlin II', 35571, 'TS GroÃburgwedel', 35661, 25, 28))85 t1.add_match(_Match('DHK Flensborg', 35551, 'Stralsunder HV', 35641, 23, 19))86 t1.add_match(_Match('Eintracht Hildesheim', 35531, 'TSV Altenholz', 35621, 30, 27))87 t1.add_match(_Match('SV Meck.-Schwerin', 35651, 'SG Fle-Ha II', 35581, 24, 25))88 t1.add_match(_Match('HSV Insel Usedom', 35611, 'Dessau-Rosslauer HV', 35541, 20, 24))89 t1.add_match(_Match('HSV Hannover', 35591, 'Oranienburger HC', 35521, 27, 22))90 t1.add_match(_Match('TSV Burgdorf II', 35511, 'VfL Potsdam', 35601, 28, 26))91 t1.add_match(_Match('VfL Potsdam', 35601, 'Eintracht Hildesheim', 35531, 30, 25))92 t1.add_match(_Match('Stralsunder HV', 35641, 'Füchse Berlin II', 35571, 30, 24))93 t1.add_match(_Match('TS GroÃburgwedel', 35661, 'HSV Hannover', 35591, 18, 19))94 t1.add_match(_Match('TSV Altenholz', 35621, 'DHK Flensborg', 35551, 27, 28))95 t1.add_match(_Match('SC Magdeburg II', 35561, 'SV Meck.-Schwerin', 35651, 28, 21))96 t1.add_match(_Match('Dessau-Rosslauer HV', 35541, 'SV Anhalt Bernburg', 35631, 25, 23))97 t1.add_match(_Match('Oranienburger HC', 35521, 'HSV Insel Usedom', 35611, 35, 26))98 t1.add_match(_Match('SG Fle-Ha II', 35581, 'TSV Burgdorf II', 35511, 34, 28))99 t1.add_match(_Match('HSV Hannover', 35591, 'Stralsunder HV', 35641, 25, 28))100 t1.add_match(_Match('SV Anhalt Bernburg', 35631, 'Oranienburger HC', 35521, 32, 31))101 t1.add_match(_Match('HSV Insel Usedom', 35611, 'TS GroÃburgwedel', 35661, 27, 30))102 t1.add_match(_Match('Füchse Berlin II', 35571, 'TSV Altenholz', 35621, 23, 23))103 t1.add_match(_Match('DHK Flensborg', 35551, 'VfL Potsdam', 35601, 29, 33))104 t1.add_match(_Match('Eintracht Hildesheim', 35531, 'SG Fle-Ha II', 35581, 35, 31))105 t1.add_match(_Match('TSV Burgdorf II', 35511, 'SC Magdeburg II', 35561, 22, 27))106 t1.add_match(_Match('SV Meck.-Schwerin', 35651, 'Dessau-Rosslauer HV', 35541, 21, 22))107 t1.add_match(_Match('Dessau-Rosslauer HV', 35541, 'TSV Burgdorf II', 35511, 32, 28))108 t1.add_match(_Match('SC Magdeburg II', 35561, 'Eintracht Hildesheim', 35531, 32, 29))109 t1.add_match(_Match('DHK Flensborg', 35551, 'SG Fle-Ha II', 35581, 25, 22))110 t1.add_match(_Match('VfL Potsdam', 35601, 'Füchse Berlin II', 35571, 36, 21))111 t1.add_match(_Match('TSV Altenholz', 35621, 'HSV Hannover', 35591, 32, 29))112 t1.add_match(_Match('Stralsunder HV', 35641, 'HSV Insel Usedom', 35611, 34, 27))113 t1.add_match(_Match('TS GroÃburgwedel', 35661, 'SV Anhalt Bernburg', 35631, 26, 24))114 t1.add_match(_Match('Oranienburger HC', 35521, 'SV Meck.-Schwerin', 35651, 21, 19))115 t1.add_match(_Match('Eintracht Hildesheim', 35531, 'HSV Insel Usedom', 35611, 39, 28))116 t1.add_match(_Match('SG Fle-Ha II', 35581, 'TS GroÃburgwedel', 35661, 28, 35))117 t1.add_match(_Match('DHK Flensborg', 35551, 'SV Anhalt Bernburg', 35631, 25, 29))118 t1.add_match(_Match('SC Magdeburg II', 35561, 'Stralsunder HV', 35641, 24, 23))119 t1.add_match(_Match('Oranienburger HC', 35521, 'VfL Potsdam', 35601, 27, 26))120 t1.add_match(_Match('Dessau-Rosslauer HV', 35541, 'TSV Altenholz', 35621, 34, 25))121 t1.add_match(_Match('Füchse Berlin II', 35571, 'SV Meck.-Schwerin', 35651, 17, 22))122 t1.add_match(_Match('TSV Burgdorf II', 35511, 'HSV Hannover', 35591, 24, 22))123 t1.add_match(_Match('HSV Hannover', 35591, 'Füchse Berlin II', 35571, 32, 27))124 t1.add_match(_Match('TS GroÃburgwedel', 35661, 'SC Magdeburg II', 35561, 27, 22))125 t1.add_match(_Match('SV Meck.-Schwerin', 35651, 'DHK Flensborg', 35551, 23, 34))126 t1.add_match(_Match('SV Anhalt Bernburg', 35631, 'Eintracht Hildesheim', 35531, 22, 22))127 t1.add_match(_Match('VfL Potsdam', 35601, 'SG Fle-Ha II', 35581, 30, 39))128 t1.add_match(_Match('TSV Altenholz', 35621, 'Oranienburger HC', 35521, 28, 20))129 t1.add_match(_Match('Stralsunder HV', 35641, 'Dessau-Rosslauer HV', 35541, 27, 25))130 t1.add_match(_Match('HSV Insel Usedom', 35611, 'TSV Burgdorf II', 35511, 29, 29))131 t1.add_match(_Match('SG Fle-Ha II', 35581, 'TSV Altenholz', 35621, 31, 27))132 t1.add_match(_Match('Dessau-Rosslauer HV', 35541, 'TS GroÃburgwedel', 35661, 30, 23))133 t1.add_match(_Match('DHK Flensborg', 35551, 'HSV Hannover', 35591, 37, 27))134 t1.add_match(_Match('Oranienburger HC', 35521, 'Stralsunder HV', 35641, 27, 26))135 t1.add_match(_Match('TSV Burgdorf II', 35511, 'SV Anhalt Bernburg', 35631, 25, 23))136 t1.add_match(_Match('SC Magdeburg II', 35561, 'VfL Potsdam', 35601, 36, 31))137 t1.add_match(_Match('Füchse Berlin II', 35571, 'HSV Insel Usedom', 35611, 28, 27))138 t1.add_match(_Match('Eintracht Hildesheim', 35531, 'SV Meck.-Schwerin', 35651, 30, 20))139 t1.add_match(_Match('HSV Insel Usedom', 35611, 'DHK Flensborg', 35551, 28, 32))140 t1.add_match(_Match('VfL Potsdam', 35601, 'Dessau-Rosslauer HV', 35541, 21, 20))141 t1.add_match(_Match('SV Anhalt Bernburg', 35631, 'Füchse Berlin II', 35571, 22, 29))142 t1.add_match(_Match('TS GroÃburgwedel', 35661, 'Oranienburger HC', 35521, 29, 24))143 t1.add_match(_Match('SV Meck.-Schwerin', 35651, 'TSV Burgdorf II', 35511, 25, 22))144 t1.add_match(_Match('TSV Altenholz', 35621, 'SC Magdeburg II', 35561, 28, 29))145 t1.add_match(_Match('Stralsunder HV', 35641, 'SG Fle-Ha II', 35581, 24, 34))146 t1.add_match(_Match('HSV Hannover', 35591, 'Eintracht Hildesheim', 35531, 24, 22))147 t1.add_match(_Match('HSV Insel Usedom', 35611, 'HSV Hannover', 35591, 21, 25))148 t1.add_match(_Match('SV Anhalt Bernburg', 35631, 'SV Meck.-Schwerin', 35651, 25, 21))149 t1.add_match(_Match('Füchse Berlin II', 35571, 'Eintracht Hildesheim', 35531, 26, 32))150 t1.add_match(_Match('DHK Flensborg', 35551, 'TSV Burgdorf II', 35511, 32, 30))151 t1.add_match(_Match('Dessau-Rosslauer HV', 35541, 'SG Fle-Ha II', 35581, 31, 29))152 t1.add_match(_Match('Oranienburger HC', 35521, 'SC Magdeburg II', 35561, 24, 22))153 t1.add_match(_Match('VfL Potsdam', 35601, 'Stralsunder HV', 35641, 35, 25))154 t1.add_match(_Match('TSV Altenholz', 35621, 'TS GroÃburgwedel', 35661, 35, 26))155 t1.add_match(_Match('TS GroÃburgwedel', 35661, 'VfL Potsdam', 35601, 26, 29))156 t1.add_match(_Match('Stralsunder HV', 35641, 'TSV Altenholz', 35621, 28, 31))157 t1.add_match(_Match('SG Fle-Ha II', 35581, 'Oranienburger HC', 35521, 28, 19))158 t1.add_match(_Match('SC Magdeburg II', 35561, 'Dessau-Rosslauer HV', 35541, 31, 32))159 t1.add_match(_Match('Eintracht Hildesheim', 35531, 'DHK Flensborg', 35551, 26, 26))160 t1.add_match(_Match('TSV Burgdorf II', 35511, 'Füchse Berlin II', 35571, 24, 20))161 t1.add_match(_Match('SV Meck.-Schwerin', 35651, 'HSV Insel Usedom', 35611, 28, 27))162 t1.add_match(_Match('HSV Hannover', 35591, 'SV Anhalt Bernburg', 35631, 27, 29))163 t1.add_match(_Match('SC Magdeburg II', 35561, 'DHK Flensborg', 35551, 30, 30))164 t1.add_match(_Match('TS GroÃburgwedel', 35661, 'SV Meck.-Schwerin', 35651, 22, 21))165 t1.add_match(_Match('Stralsunder HV', 35641, 'SV Anhalt Bernburg', 35631, 27, 28))166 t1.add_match(_Match('TSV Altenholz', 35621, 'HSV Insel Usedom', 35611, 34, 29))167 t1.add_match(_Match('SG Fle-Ha II', 35581, 'Füchse Berlin II', 35571, 37, 30))168 t1.add_match(_Match('Dessau-Rosslauer HV', 35541, 'Eintracht Hildesheim', 35531, 22, 22))169 t1.add_match(_Match('Oranienburger HC', 35521, 'TSV Burgdorf II', 35511, 27, 29))170 t1.add_match(_Match('VfL Potsdam', 35601, 'HSV Hannover', 35591, 31, 29))171 t1.add_match(_Match('SV Meck.-Schwerin', 35651, 'Stralsunder HV', 35641, 22, 19))172 #pprint(t1.get_table())173 tb = t1.get_table()174 self.assertEqual(tb[1].team_id,35561)175 self.assertEqual(tb[2].team_id,35601)176 self.assertEqual(tb[4].team_id,35661)177def main():178 unittest.main()179if __name__ == 'main':...
markdown.py
Source:markdown.py
1# -*- coding: utf-8 -*-2# author: @RShirohara3import json4import re5from novelconverter import parser, processor, renderer6_BOLD = re.compile(r"\*\*(?P<text>.*?)\*\*")7_CODE_INLINE = re.compile(r"`(?P<text>.*?)`")8_IMAGE = re.compile(r"!\[(?P<text>.*?){,1}\]\((?P<link>.*?)\)")9_LINK = re.compile(r"\[(?P<text>.*?){,1}\]\((?P<link>.*?)\)")10_HEADER = re.compile(r"^(?P<level>#{1,6}) (?P<title>.*?) {,1}#{0,}$")11_CODE_BLOCK = re.compile(12 r"^```(?P<lang>.*?){,1}\n(?P<text>.*?)\n```$", re.DOTALL13)14_ITEM_LIST = re.compile(15 r"^(?P<level>( ){,})([-+*]|[0-9]+[.]) (?P<text>.*?)$"16)17_INDENT = re.compile(r"^(?P<level>( ){1,})(?P<text>.*?)$")18_QUOTE = re.compile(r"^(?P<level>(> {,1}){1,})(?P<text>.*?)$")19def build_inlineparser():20 parser = InlineParser()21 parser.reg.add(parser.code_inline, "code_inline", 40)22 parser.reg.add(parser.bold, "bold", 30)23 parser.reg.add(parser.image, "image", 20)24 parser.reg.add(parser.link, "link", 10)25 return parser26def build_blockparser():27 parser = BlockParser()28 parser.reg.add(parser.code_block, "code_block", 50)29 parser.reg.add(parser.header, "header", 40)30 parser.reg.add(parser.item_list, "item_list", 30)31 parser.reg.add(parser.quote, "quote", 20)32 parser.reg.add(parser.para, "para", 10)33 return parser34def build_renderer():35 renderer = Renderer()36 renderer.reg.add(renderer.bold, "bold", 90)37 renderer.reg.add(renderer.code_inline, "code_inline", 80)38 renderer.reg.add(renderer.image, "image", 70)39 renderer.reg.add(renderer.link, "link", 60)40 renderer.reg.add(renderer.header, "header", 50)41 renderer.reg.add(renderer.code_block, "code_block", 40)42 renderer.reg.add(renderer.item_list, "item_list", 30)43 renderer.reg.add(renderer.quote, "quote", 20)44 renderer.reg.add(renderer.para, "para", 10)45 return renderer46def build_preprocessor():47 processor = PreProcessor()48 processor.reg.add(processor.del_space, "del_space", 10)49 return processor50def build_postprocessor():51 processor = PostProcessor()52 processor.reg.add(processor.add_space, "add_space", 10)53 return processor54class InlineParser(parser.InlineParser):55 def bold(self, source):56 _pos = 057 while True:58 _match = _BOLD.search(source, pos=_pos)59 if not _match:60 break61 _pos = _match.end(0)62 _dict = _match.groupdict()63 _old = _match.group(0)64 _new = '{"type": "bold", ' + \65 f'"content": ["{_dict["text"]}\"]' + \66 "}"67 source = source.replace(_old, _new)68 return source69 def code_inline(self, source):70 _pos = 071 while True:72 _match = _CODE_INLINE.search(source, pos=_pos)73 if not _match:74 break75 _pos = _match.end(0)76 _dict = _match.groupdict()77 _old = _match.group(0)78 _new = '{"type": "code_inline", ' + \79 f'"content": ["{_dict["text"]}"]' + \80 "}"81 source = source.replace(_old, _new)82 return source83 def image(self, source):84 _pos = 085 while True:86 _match = _IMAGE.search(source, pos=_pos)87 if not _match:88 break89 _pos = _match.end(0)90 _dict = _match.groupdict()91 _old = _match.group(0)92 _new = '{"type": "image", "content": [' + \93 f'"{_dict["text"] if _dict["text"] else _dict["link"]}"' + \94 f', "{_dict["link"]}"]' + \95 "}"96 source = source.replace(_old, _new)97 return source98 def link(self, source):99 _pos = 0100 while True:101 _match = _LINK.search(source, pos=_pos)102 if not _match:103 break104 _pos = _match.end(0)105 _dict = _match.groupdict()106 _old = _match.group(0)107 _new = '{"type": "link", "content": [' + \108 f'"{_dict["text"] if _dict["text"] else _dict["link"]}"' + \109 f', "{_dict["link"]}"]' + \110 "}"111 source = source.replace(_old, _new)112 return source113class BlockParser(parser.BlockParser):114 def header(self, source):115 _match = _HEADER.match(source)116 if not _match:117 return118 _dict = _match.groupdict()119 result = {120 "type": "header",121 "content": [_dict["title"], len(_dict["level"])]122 }123 return result124 def code_block(self, source):125 _match = _CODE_BLOCK.match(source)126 if not _match:127 return128 _dict = _match.groupdict()129 result = {130 "type": "code_block",131 "content": [132 _dict["text"].splitlines(),133 _dict["lang"]134 ]135 }136 return result137 def item_list(self, source):138 text = []139 level = []140 for s in source.splitlines():141 _index = len(text) - 1142 _match = _ITEM_LIST.match(s)143 if not _match:144 _match_indent = _INDENT.match(s)145 if not _match_indent:146 return147 if level[_index] + 2 == len(_match_indent.group(1)):148 text[_index] += f"\n{_match_indent.group(3)}"149 continue150 _dic = _match.groupdict()151 text.append(_dic["text"])152 level.append(len(_dic["level"]))153 result = '{"type": "item_list", ' +\154 f'"content": [{text}, {level}]'.replace("'", '"') + \155 "}"156 return json.loads(result)157 def quote(self, source):158 text = []159 level = []160 for s in source.splitlines():161 _match = _QUOTE.match(s)162 if not _match:163 return164 _dict = _match.groupdict()165 text.append(_dict["text"])166 level.append(len(_dict["level"].replace(" ", "")))167 result = '{"type": "quote", ' + \168 f'"content": [{text}, {level}]'.replace("'", '"') + \169 "}"170 return json.loads(result)171class Renderer(renderer.Renderer):172 def header(self, source):173 level = "#" * source["content"][1]174 return f"{level} {source['content'][0]}"175 def code_block(self, source):176 result = self._join_nest(source["content"][0], "\n", "")177 lang = source["content"][1]178 return f"```{lang}\n{result}\n```"179 def item_list(self, source):180 result = [181 " "*l+"- "+s for s, l in zip(source["content"][0],182 source["content"][1])183 ]184 return "\n".join(result)185 def quote(self, source):186 result = [187 "> "*l+s for s, l in zip(source["content"][0],188 source["content"][1])189 ]190 return "\n".join(result)191 def bold(self, source):192 return f'**{source["content"][0]}**'193 def code_inline(self, source):194 return f'`{source["content"][0]}`'195 def image(self, source):196 text = source["content"][0]197 link = source["content"][1]198 if text == link:199 text = ""200 return f"![{text}]({link})"201 def link(self, source):202 text = source["content"][0]203 link = source["content"][1]204 if text == link:205 text = ""206 return f"[{text}]({link})"207class PreProcessor(processor.PreProcessor):208 def del_space(self, source):209 """Delete a space at the end of line."""210 return source.replace(" \n", "\n")211class PostProcessor(processor.PostProcessor):212 def add_space(self, source):213 """Add a space at the end of line."""...
machine.py
Source:machine.py
...4import struct5class Hangman(Actions):6 with open('support/words.txt') as word_fh:7 words = [x.strip() for x in word_fh.readlines()]8 def _match(self, data):9 self.read(length=len(data), expect=data)10 def start(self):11 #self.delay(100)12 self._match('Password: ')13 def good_password(self):14 self.write("HANGEMHIGH!\n")15 self._match("Welcome to Hangman Blondie!\n")16 def bad_password(self):17 sequence = ''.join([chr(x) for x in range(256)]).replace('\x00','').replace('\n','')18 data = self.random_buffer(random.randint(1, 254), sequence) + "\n"19 self.write(data)20 return -121 def _high_score(self):22 self._match("Congratulations blondie, add yourself to the high score list\n")23 self._match("What is your name? ")24 25 # send a random name26 banned = '\x25\x00\x0a\x5c'27 sequence = ''.join([chr(x) for x in range(256)])28 for i in banned:29 sequence = sequence.replace(i, '')30 name = self.random_buffer(random.randint(1, 30), sequence) + "\n"31 self.write(name)32 self._match("New member of the HOF: %s" % name)33 def _bad_choice(self):34 # any character, and then anything but 'newline' causes a failure35 sequence = ''.join([chr(x) for x in range(256)])36 response = random.choice(sequence)37 sequence = sequence.replace('\n', '')38 response += random.choice(sequence)39 40 self.write(response)41 self._match("Invalid guess, game over.\n")42 def _repeat_choice(self, used):43 # pick a character from an existing stream44 self._send_from(used)45 def _get_word(self):46 value = random.randint(0, 0xFFFFFFFF)47 48 offset = value % 409349 magic_value = struct.unpack('<L', self.magic_page[offset:offset+4])[0]50 word = Hangman.words[magic_value % len(Hangman.words)]51 return word, struct.pack('<L', value)52 def _send_from(self, sequence):53 char = random.choice(sequence)54 self.write('%s\n' % char)55 return char56 def _replace_offset(self, sequence, offset, value):57 result = sequence[:offset] + value + sequence[offset+1:]58 return result59 def play_game(self):60 self._match("Choose your word seed. Please send 4 bytes\n")61 word, location = self._get_word()62 self.write(location)63 self.comment('word should be: %s' % word)64 used = ''65 current = '_' * len(word)66 available = string.lowercase67 correct = [x for x in string.lowercase if x in word]68 incorrect = [x for x in string.lowercase if x not in word]69 # bleck70 gallows = " ---\n | |\n |\n |\n |\n |\n -----\n | |\n"71 indexes = [17, 24, 23, 25, 31, 37, 39]72 parts = ['O', '|', '/', '\\', '|', '/', '\\']73 bad_guesses = 074 while True:75 self._match("\n" + gallows + "\n")76 self._match('used: "%s"\n' % used)77 self._match('available: "%s"\n' % available)78 self._match('\ncurrent: %s\n' % current)79 self._match("\nYour guess: ")80 # Very rarely, send invalid input which causes a failure81 if self.chance(.00001):82 self._bad_choice()83 return84 # Very rarely, repeat a choice.85 if len(used) and self.chance(.00001):86 self._send_from(used)87 self._match("Invalid guess, game over.\n")88 return89 # Do we choose correctly?90 if self.chance(.50):91 # print "GOT", repr(correct)92 sent = self._send_from(correct)93 correct.remove(sent)94 available = available.replace(sent, '')95 locations = [i for i, x in enumerate(word) if x == sent]96 for i in locations:97 current = self._replace_offset(current, i, sent)98 used += sent99 if '_' in current:100 self._match("\nMiraculously, you have manage to prolong Tuco's life.\n")101 else:102 self._match("\nNice shot Blondie, you freed Tuco.\n")103 self._match("The correct word is: %s.\n" % word)104 self._high_score()105 return106 else:107 # choose & send an incorrect letter108 sent = self._send_from(incorrect)109 incorrect.remove(sent)110 available = available.replace(sent, '')111 used += sent112 gallows = self._replace_offset(gallows, indexes[bad_guesses], parts[bad_guesses])113 # gallows = gallows[:indexes[bad_guesses]] + parts[bad_guesses] + gallows[indexes[bad_guesses]+1:]114 bad_guesses += 1 115 # handle adding a part116 if bad_guesses == len(parts):117 self._match("\n" + gallows + "\n")118 self._match("\nBlondie, you missed, Tuco has been hanged.\n")119 self._match("The correct word is: %s.\n" % word)120 return121 else:122 self._match("\nHaha, Tuco is feeling that rope a little more!\n")123 def play_again(self):124 self._match("Play again (y/n)? ")125 def play_again_yes(self):126 self.write('%s\n' % random.choice('yY'))127 128 def play_again_no(self):129 self.write('%s\n' % random.choice('nN'))130 def play_again_bad_answer(self):131 self.comment('play again bad answer')132 sequence = ''.join([chr(x) for x in range(256)])133 for x in 'yYnN':134 sequence = sequence.replace(x, '')135 data = random.choice(sequence)136 sequence = sequence.replace('\n', '')137 data += random.choice(sequence)138 self.write(data)
test_regex_parser.py
Source:test_regex_parser.py
...6 # It's a bit of a PITA to test for RegularExpression objects equality, so we check7 # matched strings8 def _parse(self, rx):9 return buildRegex(rx)10 def _match(self, rx, s):11 return rx.match(s)12 def test_newline(self):13 rx = self._parse(r'\n')14 self.assertTrue(self._match(rx, '\n'))15 def test_concat(self):16 rx = self._parse('ab')17 self.assertFalse(self._match(rx, 'a'))18 self.assertTrue(self._match(rx, 'ab'))19 self.assertFalse(self._match(rx, 'abc'))20 def test_union(self):21 rx = self._parse('a|b')22 self.assertTrue(self._match(rx, 'a'))23 self.assertTrue(self._match(rx, 'b'))24 self.assertFalse(self._match(rx, 'ab'))25 self.assertFalse(self._match(rx, 'c'))26 def test_kleene(self):27 rx = self._parse('a*')28 self.assertTrue(self._match(rx, ''))29 self.assertTrue(self._match(rx, 'a'))30 self.assertTrue(self._match(rx, 'aa'))31 self.assertFalse(self._match(rx, 'b'))32 def test_closure(self):33 rx = self._parse('a+')34 self.assertFalse(self._match(rx, ''))35 self.assertTrue(self._match(rx, 'a'))36 self.assertTrue(self._match(rx, 'aa'))37 self.assertFalse(self._match(rx, 'b'))38 def test_exp_single(self):39 rx = self._parse('a{2}')40 self.assertFalse(self._match(rx, ''))41 self.assertFalse(self._match(rx, 'a'))42 self.assertTrue(self._match(rx, 'aa'))43 self.assertFalse(self._match(rx, 'aaa'))44 def test_exp_both(self):45 rx = self._parse('a{2-3}')46 self.assertFalse(self._match(rx, ''))47 self.assertFalse(self._match(rx, 'a'))48 self.assertTrue(self._match(rx, 'aa'))49 self.assertTrue(self._match(rx, 'aaa'))50 self.assertFalse(self._match(rx, 'aaaa'))51 def test_class(self):52 rx = self._parse('[a-c]')53 self.assertTrue(self._match(rx, 'a'))54 self.assertTrue(self._match(rx, 'b'))55 self.assertTrue(self._match(rx, 'c'))56 self.assertFalse(self._match(rx, 'd'))57 def test_any(self):58 rx = self._parse('.')59 self.assertTrue(self._match(rx, 'U'))60 self.assertFalse(self._match(rx, '\n'))61 def test_prio_1(self):62 rx = self._parse('a|b*')63 self.assertTrue(self._match(rx, 'a'))64 self.assertTrue(self._match(rx, 'b'))65 self.assertTrue(self._match(rx, 'bb'))66 self.assertFalse(self._match(rx, 'ab'))67 def test_prio_2(self):68 rx = self._parse('ab*')69 self.assertTrue(self._match(rx, 'a'))70 self.assertTrue(self._match(rx, 'ab'))71 self.assertTrue(self._match(rx, 'abb'))72 self.assertFalse(self._match(rx, 'abab'))73 def test_prio_3(self):74 rx = self._parse('a|bc')75 self.assertTrue(self._match(rx, 'a'))76 self.assertTrue(self._match(rx, 'bc'))77 self.assertFalse(self._match(rx, 'ac'))78 def test_paren(self):79 rx = self._parse('(ab)*')80 self.assertTrue(self._match(rx, 'ab'))81 self.assertTrue(self._match(rx, 'abab'))82 self.assertFalse(self._match(rx, 'abb'))83 def test_crlf(self):84 rx = self._parse(r'\r\n')85 self.assertTrue(self._match(rx, '\r\n'))86 def test_extra_tokens(self):87 try:88 rx = self._parse('ab(')89 except RegexParseError:90 pass91 else:92 self.fail()93 def test_missing_paren(self):94 try:95 rx = self._parse('(a')96 except RegexParseError:97 pass98 else:99 self.fail()100class RegexParserUnicodeTestCase(RegexParserTestCaseMixin, unittest.TestCase):101 pass102class RegexParserBytesTestCase(RegexParserTestCaseMixin, unittest.TestCase):103 def _parse(self, rx):104 if isinstance(rx, str):105 rx = rx.encode('UTF-8')106 return super()._parse(rx)107 def _match(self, rx, s):108 if isinstance(s, str):109 s = s.encode('UTF-8')110 return super()._match(rx, s)111if __name__ == '__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!!