Best Python code snippet using hypothesis
strategy.py
Source:strategy.py
1from abc import ABC2from enum import Enum, auto345class OutputFormat(Enum):6 MARKDOWN = auto()7 HTML = auto()8910# not required but a good idea11class ListStrategy(ABC):12 """We don't follow de Interface Segretaion principle13 because the MarkdownListStrategy doesn't implement14 all these methods. Despite that, the TextProcessor15 will use all of these methods, so it's ok.16 """1718 def start(self, buffer):19 pass2021 def end(self, buffer):22 pass2324 def add_list_item(self, buffer, item):25 pass262728class MarkdownListStrategy(ListStrategy):29 def add_list_item(self, buffer, item):30 buffer.append(f" * {item}\n")313233class HtmlListStrategy(ListStrategy):34 def start(self, buffer):35 buffer.append("<ul>\n")3637 def end(self, buffer):38 buffer.append("</ul>\n")3940 def add_list_item(self, buffer, item):41 buffer.append(f" <li>{item}</li>\n")424344class TextProcessor:45 def __init__(self, list_strategy=HtmlListStrategy()):46 self.buffer = []47 self.list_strategy = list_strategy4849 def append_list(self, items):50 self.list_strategy.start(self.buffer)51 for item in items:52 self.list_strategy.add_list_item(self.buffer, item)53 self.list_strategy.end(self.buffer)5455 def set_output_format(self, format):56 if format == OutputFormat.MARKDOWN:57 self.list_strategy = MarkdownListStrategy()58 elif format == OutputFormat.HTML:59 self.list_strategy = HtmlListStrategy()6061 def clear(self):62 self.buffer.clear()6364 def __str__(self):65 return "".join(self.buffer)666768if __name__ == "__main__":69 items = ["foo", "bar", "baz"]7071 tp = TextProcessor()72 tp.set_output_format(OutputFormat.MARKDOWN)73 tp.append_list(items)74 print(tp)7576 tp.set_output_format(OutputFormat.HTML)77 tp.clear()78 tp.append_list(items)
...
1_strategy.py
Source:1_strategy.py
1from abc import ABC2from enum import Enum, auto345class OutputFormat(Enum):6 MARKDOWN = auto()7 HTML = auto()8910# not required but a good idea11class ListStrategy(ABC):12 def start(self, buffer): pass1314 def end(self, buffer): pass1516 def add_list_item(self, buffer, item): pass171819class MarkdownListStrategy(ListStrategy):2021 def add_list_item(self, buffer, item):22 buffer.append(f' * {item}\n')232425class HtmlListStrategy(ListStrategy):2627 def start(self, buffer):28 buffer.append('<ul>\n')2930 def end(self, buffer):31 buffer.append('</ul>\n')3233 def add_list_item(self, buffer, item):34 buffer.append(f' <li>{item}</li>\n')353637class TextProcessor:38 def __init__(self, list_strategy=HtmlListStrategy()):39 self.buffer = []40 self.list_strategy = list_strategy4142 def append_list(self, items):43 self.list_strategy.start(self.buffer)44 for item in items:45 self.list_strategy.add_list_item(46 self.buffer, item47 )48 self.list_strategy.end(self.buffer)4950 def set_output_format(self, format):51 if format == OutputFormat.MARKDOWN:52 self.list_strategy = MarkdownListStrategy()53 elif format == OutputFormat.HTML:54 self.list_strategy = HtmlListStrategy()5556 def clear(self):57 self.buffer.clear()5859 def __str__(self):60 return ''.join(self.buffer)616263if __name__ == '__main__':64 items = ['foo', 'bar', 'baz']6566 tp = TextProcessor()67 tp.set_output_format(OutputFormat.MARKDOWN)68 tp.append_list(items)69 print(tp)7071 tp.set_output_format(OutputFormat.HTML)72 tp.clear()73 tp.append_list(items)
...
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!!