How to use execute_safely method in autotest

Best Python code snippet using autotest_python

real-time-settings

Source:real-time-settings Github

copy

Full Screen

...53 def rt_settings_on(self):54 pass55 def rt_settings_off(self):56 pass57 def execute_safely(self, function, *args, **kwargs):58 """59 Method prints what would be done if simulating or60 does it otherwise.61 """62 def as_pretty_string():63 return "%s.%s(%s, %s)" % (64 function.__module__,65 function.__name__,66 ', '.join((repr(arg) for arg in args)),67 ', '.join(( "%s=%s" % (repr(k), repr(v))68 for k, v in kwargs.items())),69 )70 if cli_args.simulate:71 print("simulating - would execute: %s" % (72 as_pretty_string()73 ))74 return75 else:76 logging.debug("executing " + as_pretty_string())77 return function(*args, **kwargs)78 def service(self, name, action):79 if not hasattr(ActionBase, "_available_services_cache"):80 ActionBase._available_services_cache = []81 output = check_output(("systemctl", "list-unit-files"),82 universal_newlines=True)83 for line in output.split("\n")[1:-3]:84 service_name = path.splitext(line.split(" ")[0])[0]85 if service_name != "":86 ActionBase._available_services_cache.append(service_name)87 logging.debug("found services %r" %88 ActionBase._available_services_cache)89 if name not in ActionBase._available_services_cache:90 logging.info("service '%s' does not seem to exist" % name)91 return92 self.execute_safely(call, (which("systemctl"), action, name))93 def service_start(self, name):94 self.service(name, "start")95 def service_stop(self, name):96 self.service(name, "stop")97 def _load_all_from_temp(self):98 try:99 with open(self.TEMPFILE, "r") as handle:100 return json_load(handle)101 except (ValueError, FileNotFoundError):102 return {}103 def load_from_temp(self, key, default=None):104 self._load_all_from_temp().get(key, default)105 def store_to_temp(self, key, value):106 obj = self._load_all_from_temp()107 obj[key] = value108 with open(self.TEMPFILE, "w+") as handle:109 return json_dump(obj, handle)110 def store_and_replace_content_in_file(self, file_path, value):111 with open(file_path) as handle:112 content = handle.readline().strip()113 self.store_to_temp(file_path, content)114 with open(file_path, "w+") as handle:115 handle.write(str(value))116 def restore_or_set_default_content_in_file(self, file_path, default):117 loaded = self.load_from_temp(file_path)118 with open(file_path, "w+") as handle:119 handle.write(str(loaded or default))120class CheckForRealTimeKernel(ActionBase):121 def rt_settings_on(self):122 SYS_RT_FILE = "/sys/kernel/realtime"123 if path.isfile(SYS_RT_FILE):124 with open(SYS_RT_FILE) as sys_rt:125 if sys_rt.readline() == "1":126 logging.debug("found %s with '1' in it" % SYS_RT_FILE)127 return128 uname = get_uname()129 if uname.release.endswith("+rt"):130 logging.debug("found +rt in kernel release")131 return132 if " RT " in uname.version:133 logging.debug("found RT in kernel version")134 return135 if " PREEMPT " in uname.version:136 logging.debug("found PREEMT in kernel version")137 return138 logging.warn("You don't seem to be using a real-time kernel.")139settings_modules.append(CheckForRealTimeKernel)140class Cron(ActionBase):141 def rt_settings_on(self):142 self.service_stop("cron")143 self.service_stop("crond")144 self.service_stop("cronie")145 def rt_settings_off(self):146 self.service_start("cron")147 self.service_start("crond")148 self.service_start("cronie")149settings_modules.append(Cron)150class Anacron(ActionBase):151 def rt_settings_on(self):152 self.service_stop("anacron.service")153 self.service_stop("anacron.timer")154 def rt_settings_off(self):155 self.service_start("anacron.service")156 self.service_start("anacron.timer")157settings_modules.append(Anacron)158class Tlp(ActionBase):159 def rt_settings_on(self):160 self.service_stop("tlp")161 def rt_settings_off(self):162 self.service_start("tlp")163settings_modules.append(Tlp)164class TuneD(ActionBase):165 def rt_settings_on(self):166 self.service_stop("tuned")167 def rt_settings_off(self):168 self.service_start("tuned")169settings_modules.append(TuneD)170class FrequencyScaling(ActionBase):171 CPUFREQ_BASE_PATH = "/sys/devices/system/cpu/cpu%i/cpufreq"172 CPUFREQ_MIN = path.join(CPUFREQ_BASE_PATH, "cpuinfo_min_freq")173 CPUFREQ_MAX = path.join(CPUFREQ_BASE_PATH, "cpuinfo_max_freq")174 CPUFREQ_MIN_ALLOWED = path.join(CPUFREQ_BASE_PATH, "scaling_min_freq")175 def rt_settings_on(self):176 for cpu_num in range(cpu_count()):177 self.execute_safely(178 copyfile,179 self.CPUFREQ_MAX % cpu_num,180 self.CPUFREQ_MIN_ALLOWED % cpu_num181 )182 def rt_settings_off(self):183 for cpu_num in range(cpu_count()):184 self.execute_safely(185 copyfile,186 self.CPUFREQ_MIN % cpu_num,187 self.CPUFREQ_MIN_ALLOWED % cpu_num188 )189settings_modules.append(FrequencyScaling)190class CpuGovernor(ActionBase):191 GOVERNOR_FILE_PATH_TEMPLATE = "/sys/devices/system/cpu/cpu%i/cpufreq/scaling_governor"192 GOVERNOR_ON = "performance"193 GOVERNOR_OFF_DEFAULT = "powersave"194 def rt_settings_on(self):195 for cpu_num in range(cpu_count()):196 self.execute_safely(self.store_and_replace_content_in_file,197 self.GOVERNOR_FILE_PATH_TEMPLATE % cpu_num,198 self.GOVERNOR_ON)199 def rt_settings_off(self):200 for cpu_num in range(cpu_count()):201 self.execute_safely(self.store_and_replace_content_in_file,202 self.GOVERNOR_FILE_PATH_TEMPLATE % cpu_num,203 self.GOVERNOR_OFF_DEFAULT)204settings_modules.append(CpuGovernor)205class TurboBoost(ActionBase):206 NO_TURBO_FILE_NAME = "/sys/devices/system/cpu/intel_pstate/no_turbo"207 NO_TURBO_ON = 1208 NO_TURBO_OFF_DEFAULT = 0209 def rt_settings_on(self):210 self.execute_safely(self.store_and_replace_content_in_file,211 self.NO_TURBO_FILE_NAME,212 self.NO_TURBO_ON)213 def rt_settings_off(self):214 self.execute_safely(self.restore_or_set_default_content_in_file,215 self.NO_TURBO_FILE_NAME,216 self.NO_TURBO_OFF_DEFAULT)217settings_modules.append(TurboBoost)218#219# execution of settings modules220#221if cli_args.list:222 for action in settings_modules:223 print(action.__name__)224 exit(0)225# assort ignored settings module226unignored_settings_modules = list(settings_modules)227for settings_module in settings_modules:228 settings_module_name = settings_module.__name__...

Full Screen

Full Screen

TestEverything.py

Source:TestEverything.py Github

copy

Full Screen

...50 ["am ba", "ra ba", "ci ci", "co co"],51 element_wise_merge(["am", "ra", "ci", "co"], ["ba", "ba", "ci", "co"]))5253 # Task 754 def test_execute_safely(self):55 self.assertAlmostEqual(5, execute_safely(lambda x, y: x + y, 2, 3))56 self.assertAlmostEqual(6, execute_safely(lambda x, y: x * y, 2, 3))57 self.assertAlmostEqual(5, execute_safely(lambda x, y: x / y, 10, 2))58 self.assertAlmostEqual(-1, execute_safely(lambda x, y: x / y, 1, 0))59 self.assertAlmostEqual(-1, execute_safely(lambda x, y: math.pow(x, y), -1, 0.5))6061 # Task 862 def test_area(self):63 rectangle = Rectangle(3, 4) # 3 x 4 rectangle64 self.assertAlmostEqual(12, rectangle.area())65 circle = Circle(2) # circle with radius r=266 self.assertAlmostEqual(12.566370614359172953850573533118, circle.area())6768 # Task 969 def test_items(self):70 product_1 = Product("coffee", 1)71 product_2 = Product('car', 20000)72 product_3 = Product('book', 15)73 self.assertEqual("coffee costs 1 euro(s)", str(product_1)) ...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful