Best Python code snippet using lisa_python
runserver.py
Source:runserver.py  
...12        self.proc = proc13        self.net_last_time = 014        self.net_bytes_recv = 015        self.net_bytes_sent = 016        self._get_bandwidth()17    def close(self):18        self.fp.close()19    def step(self):20        now = datetime.datetime.now()21        midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)22        ts = (now-midnight).total_seconds()23        try:24            cpu = self.proc.cpu_percent()25            mem = self.proc.memory_info()26        except psutil.ZombieProcess:27            return28        except psutil.NoSuchProcess:29            return30        allcpu = psutil.cpu_percent(percpu=True)31        maxcpu = max(allcpu)32        rss = mem.rss33        vms = mem.vms34        net = psutil.net_io_counters()35        recv, sent = self._get_bandwidth()36        print(f'stats: component=ResourceConsumption, ts={ts:.3f}, cpu={cpu}, cpu_max={maxcpu}, mem={vms}, recv_bandwidth={recv:.0f}, sent_bandwidth={sent:.0f}', file=self.fp)37    def _get_bandwidth(self):38        net = psutil.net_io_counters()39        now = time.time()40        interval = now - self.net_last_time41        recv_bandwidth = (net.bytes_recv - self.net_bytes_recv) / interval42        sent_bandwidth = (net.bytes_sent - self.net_bytes_sent) / interval43        self.net_last_time = now44        self.net_bytes_recv = net.bytes_recv45        self.net_bytes_sent = net.bytes_sent46        return recv_bandwidth, sent_bandwidth47@app.route("/about")48def about():49    return "<p>Hello world!</p>"50@app.route("/runold", methods=["POST"])51def run():...ipfs.chart.py
Source:ipfs.chart.py  
...37        except (KeyError, TypeError):38            self.baseurl = "http://localhost:5001"39        self.order = ORDER40        self.definitions = CHARTS41    def _get_bandwidth(self):42        """43        Format data received from http request44        :return: int, int45        """46        self.url = self.baseurl + "/api/v0/stats/bw"47        try:48            raw = self._get_raw_data()49        except AttributeError:50            return None51        try:52            parsed = json.loads(raw)53            bw_in = int(parsed['RateIn'])54            bw_out = int(parsed['RateOut'])55        except:56            return None57        return bw_in, bw_out58    def _get_peers(self):59        """60        Format data received from http request61        :return: int62        """63        self.url = self.baseurl + "/api/v0/swarm/peers"64        try:65            raw = self._get_raw_data()66        except AttributeError:67            return None68        try:69            parsed = json.loads(raw)70            peers = len(parsed['Strings'])71        except:72            return None73        return peers74    def _get_data(self):75        """76        Get data from API77        :return: dict78        """79        try:80            peers = self._get_peers()81            bandwidth_in, bandwidth_out = self._get_bandwidth()82        except:83            return None84        data = {}85        if peers is not None:86            data['peers'] = peers87        if bandwidth_in is not None and bandwidth_out is not None:88            data['in'] = bandwidth_in89            data['out'] = bandwidth_out90        if len(data) == 0:91            return None...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!!
