Best Python code snippet using autotest_python
profiler_main.py
Source: profiler_main.py
...3from amitools.vamos.cfgcore import ConfigDict4def profiler_main_disabled_test(caplog):5 mp = MainProfiler()6 assert mp.parse_config(None)7 assert not mp.add_profiler(Profiler())8 mp.setup()9 mp.shutdown()10 assert caplog.record_tuples == []11def profiler_main_config_test(caplog, tmpdir):12 path = str(tmpdir.join("prof.json"))13 mp = MainProfiler()14 cfg = ConfigDict(15 {"enabled": True, "output": {"dump": True, "file": path, "append": True}}16 )17 assert mp.parse_config(cfg)18 assert mp.enabled19 assert mp.file == path20 assert mp.append21 mp.setup()22 mp.shutdown()23 assert caplog.record_tuples == []24def profiler_main_def_profiler_test(caplog):25 caplog.set_level(logging.INFO)26 p = Profiler()27 mp = MainProfiler(enabled=True)28 cfg = ConfigDict(29 {"enabled": True, "output": {"dump": True, "file": None, "append": True}}30 )31 assert mp.parse_config(cfg)32 assert mp.add_profiler(p)33 mp.setup()34 mp.shutdown()35 assert caplog.record_tuples == [36 ("prof", logging.INFO, "---------- Profiling Results ----------"),37 ("prof", logging.INFO, "----- profiler 'foo' -----"),38 ]39def profiler_main_file_test(caplog, tmpdir):40 caplog.set_level(logging.DEBUG)41 path = str(tmpdir.join("prof.json"))42 p = Profiler()43 mp = MainProfiler(enabled=True)44 cfg = ConfigDict(45 {"enabled": True, "output": {"dump": False, "file": path, "append": True}}46 )47 assert mp.parse_config(cfg)48 assert mp.add_profiler(p)49 mp.setup()50 mp.shutdown()51 assert caplog.record_tuples == [52 ("prof", logging.DEBUG, "added profiler 'foo'"),53 ("prof", logging.DEBUG, "saving profile data to '%s'" % path),54 ("prof", logging.DEBUG, "done saving."),55 ]56 caplog.clear()57 # now repeat setup to test appending58 p = Profiler()59 mp = MainProfiler(enabled=True)60 assert mp.parse_config(cfg)61 assert mp.add_profiler(p)62 mp.setup()63 mp.shutdown()64 assert caplog.record_tuples == [65 ("prof", logging.DEBUG, "added profiler 'foo'"),66 ("prof", logging.DEBUG, "loading profile data from '%s'" % path),67 ("prof", logging.DEBUG, "done loading."),68 ("prof", logging.DEBUG, "saving profile data to '%s'" % path),69 ("prof", logging.DEBUG, "done saving."),70 ]71class MyProfiler(Profiler):72 def __init__(self):73 self.foo = 074 self.bar = "baz"75 self.got_setup = False76 self.got_shutdown = False77 def get_name(self):78 return "test"79 def parse_config(self, cfg):80 self.foo = cfg.foo81 self.bar = cfg.bar82 return True83 def set_data(self, data_dict):84 self.foo = data_dict.foo85 self.bar = data_dict.bar86 def get_data(self):87 return {"foo": self.foo, "bar": self.bar}88 def setup(self):89 self.got_setup = True90 def shutdown(self):91 self.got_shutdown = True92 def dump(self, write):93 write("foo=%d, bar='%s'", self.foo, self.bar)94def profiler_main_test_prof_cfg_test():95 p = MyProfiler()96 mp = MainProfiler(enabled=True)97 cfg = ConfigDict(98 {99 "enabled": True,100 "output": {"dump": True, "file": None, "append": True},101 "test": {"foo": 42, "bar": "hello"},102 }103 )104 assert mp.parse_config(cfg)105 assert mp.add_profiler(p)106 assert p.foo == 42107 assert p.bar == "hello"108def profiler_main_test_prof_load_test(tmpdir):109 path = str(tmpdir.join("prof.json"))110 cfg = ConfigDict(111 {"enabled": True, "output": {"dump": True, "file": path, "append": True}}112 )113 p = MyProfiler()114 mp = MainProfiler(enabled=True)115 assert mp.parse_config(cfg)116 assert mp.add_profiler(p)117 mp.setup()118 assert p.foo == 0119 assert p.bar == "baz"120 p.foo = 42121 p.bar = "hello"122 mp.shutdown()123 # load again124 p = MyProfiler()125 mp = MainProfiler(enabled=True)126 assert mp.parse_config(cfg)127 assert mp.add_profiler(p)128 mp.setup()129 assert p.foo == 42130 assert p.bar == "hello"131 mp.shutdown()132def profiler_main_test_prof_dump_test(caplog):133 caplog.set_level(logging.INFO)134 cfg = ConfigDict(135 {"enabled": True, "output": {"dump": True, "file": None, "append": True}}136 )137 p = MyProfiler()138 mp = MainProfiler(enabled=True)139 assert mp.parse_config(cfg)140 assert mp.add_profiler(p)141 mp.setup()142 assert p.foo == 0143 assert p.bar == "baz"144 p.foo = 42145 p.bar = "hello"146 mp.shutdown()147 assert caplog.record_tuples == [148 ("prof", logging.INFO, "---------- Profiling Results ----------"),149 ("prof", logging.INFO, "----- profiler 'test' -----"),150 ("prof", logging.INFO, "foo=42, bar='hello'"),...
unused_code_checker.py
Source: unused_code_checker.py
...32. Line-by-line memory usage4"""5import os67def add_profiler(file_path):8 new_path = file_path.rsplit('.', 1)[0]+"_with_descriptors.py"9 fin = open(file_path, "r")10 fout = open(new_path, "w")1112 for line in fin:13 fout.write(line.replace('def ', '@profile \ndef '))1415 fin.close()16 fout.close()17 return new_path1819if __name__ == "__main__":20 file_path = 'demo.py'2122 # 1. Check unused imports, variables and functions (Used 'vulture' package)23 os.system('vulture ' + file_path)2425 # 2. Line-by-line memory usage26 new_path = add_profiler(file_path)27 os.system('python -m memory_profiler ' + new_path)2829 # 3. Time-based memory usage graph30 os.system('mprof run ' + new_path)
...
Check out the latest blogs from LambdaTest on this topic:
Before we discuss Scala testing, let us understand the fundamentals of Scala and how this programming language is a preferred choice for your development requirements.The popularity and usage of Scala are rapidly rising, evident by the ever-increasing open positions for Scala developers.
So, now that the first installment of this two fold article has been published (hence you might have an idea of what Agile Testing is not in my opinion), I’ve started feeling the pressure to explain what Agile Testing actually means to me.
Did you know that according to Statista, the number of smartphone users will reach 18.22 billion by 2025? Let’s face it, digital transformation is skyrocketing and will continue to do so. This swamps the mobile app development market with various options and gives rise to the need for the best mobile app testing tools
As a developer, checking the cross browser compatibility of your CSS properties is of utmost importance when building your website. I have often found myself excited to use a CSS feature only to discover that it’s still not supported on all browsers. Even if it is supported, the feature might be experimental and not work consistently across all browsers. Ask any front-end developer about using a CSS feature whose support is still in the experimental phase in most prominent web browsers. ????
The count of mobile users is on a steep rise. According to the research, by 2025, it is expected to reach 7.49 billion users worldwide. 70% of all US digital media time comes from mobile apps, and to your surprise, the average smartphone owner uses ten apps per day and 30 apps each month.
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!!