Best Python code snippet using lisa_python
vm.py
Source:vm.py
...20 self.set_status(2)21 def reboot(self):22 self.stop()23 self.start()24 def get_processes(self):25 sql = "select * from process where vmachine_id={} order by pid".\26 format(self.id)27 return self.db.run(sql)28 def run(self, pid, ram, cpu, hdd):29 sql = "insert into process (pid, ram, cpu, hdd, vmachine_id) \30 values({}, {}, {}, {}, {})".format(31 pid,32 ram,33 cpu,34 hdd,35 self.id36 )37 self.db.run(sql)38 def ram_usage(self):39 ram = 040 for p in self.get_processes():41 ram += p["ram"]42 return ram * 100 / self.ram43 def cpu_usage(self):44 cpu = 045 for p in self.get_processes():46 cpu += p["cpu"]47 return cpu * 100 / self.cpu48 def hdd_usage(self):49 hdd = 050 for p in self.get_processes():51 hdd += p["hdd"]52 return hdd * 100 / self.hdd53 def set_status(self, new_status):54 sql = "update vmachine set status={} where id={}".format(55 new_status,56 self.id57 )58 self.db.run(sql)59 def get_status(self):60 sql = "select status from vmachine where id={}".format(self.id)61 r = self.db.run(sql)62 status = r[0]["status"]63 if status == 0:64 return "Stopped"...
report.py
Source:report.py
1import subprocess2from collections import namedtuple3from collections import Counter4def get_processes():5 output = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE, text=True).stdout.readlines()6 headers = [h for h in output[0].replace("%", "").strip().split() if h]7 Proc = namedtuple("Proc", headers)8 raw_data = map(lambda s: Proc(*s.strip().split(None, len(headers) - 1)), output[1:])9 return list(raw_data)10def get_user():11 return ", ".join({i.USER for i in get_processes()})12def get_process():13 return len(get_processes())14def get_user_process():15 up = Counter(i.USER for i in get_processes())16 return "\n\t".join([f"{key}: {value}" for key, value in up.items()])17def get_mem():18 lis1 = sum([int(i.RSS) for i in get_processes()])19 return round(lis1 / 10 ** 6, 2)20def get_cpu():21 return round(sum([float(i.CPU) for i in get_processes()]), 2)22def get_max_mem():23 m = max(get_processes(), key=lambda one_process: one_process.MEM)24 return f"{m.COMMAND[:20]} {m.MEM}%"25def get_max_cpu():26 c = max(get_processes(), key=lambda process: process.CPU)27 return f"{c.COMMAND[:20]} {c.MEM}%"28if __name__ == "__main__":29 print(f"ÐолÑзоваÑели ÑиÑÑемÑ: {get_user()}")30 print(f"ÐÑоÑеÑÑов запÑÑено: {get_process()}")31 print(f"ÐолÑзоваÑелÑÑкиÑ
пÑоÑеÑÑов:\n\t{get_user_process()}")32 print(f"ÐÑего памÑÑи иÑполÑзÑеÑÑÑ: {get_mem()} mb")33 print(f"ÐÑего CPU иÑполÑзÑеÑÑÑ: {get_cpu()} %")34 print(f"ÐолÑÑе вÑего памÑÑи иÑполÑзÑеÑ: {get_max_mem()} ")...
linux.py
Source:linux.py
1import psutil2from common.utils.general import get_processes3def run():4 print("I am Linux")5 get_processes()6# Not tested7# TODO: TEST this!!!8def get_processes():9 for p in psutil.pids():...
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!!