Best Python code snippet using autotest_python
serialmon.py
Source:serialmon.py
...43 self._collector.set_output(outpath)44 self._collector.reset()45 self.logger.debug("Acquiring serial port ({})".format(self.serial_port))46 if self._collector.collecting:47 self.stop_logging(context)48 self._collector.start()49 def stop_logging(self, context, identifier="job"):50 self.logger.debug("Releasing serial port ({})".format(self.serial_port))51 if self._collector.collecting:52 self._collector.stop()53 data = self._collector.get_data()54 for l in data: # noqa: E74155 context.add_artifact("{}_serial_log".format(identifier),56 l.path, kind="log")57 def on_run_start(self, context):58 self.start_logging(context, "preamble_serial.log")59 def before_job_queue_execution(self, context):60 self.stop_logging(context, "preamble")61 def after_job_queue_execution(self, context):62 self.start_logging(context, "postamble_serial.log")63 def on_run_end(self, context):64 self.stop_logging(context, "postamble")65 def on_job_start(self, context):66 self.start_logging(context)67 def on_job_end(self, context):68 self.stop_logging(context)69 @hostside70 def before_reboot(self, context):...
bus.py
Source:bus.py
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3import zmq4import threading5import logging6import logging.handlers7import util8class Logger(threading.Thread):9 """logger for all messages and events"""10 def __init__(self, stop_logging, filename='bus.log'):11 super(Logger, self).__init__()12 self.filename = filename13 self.stop_logging = stop_logging14 # receiving socket15 self.context = zmq.Context.instance()16 self.log_in = self.context.socket(zmq.PAIR)17 self.log_in.connect("inproc://logging")18 self.log_in.setsockopt(zmq.RCVTIMEO, 1000)19 # logger parameters for stdout and compressed file20 log_format = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s')21 file_log_handler = util.TimedCompressingRotatingFileHandler(self.filename, when='midnight', backupCount=7)22 file_log_handler.setFormatter(log_format)23 stream_log_handler = logging.StreamHandler()24 stream_log_handler.setFormatter(log_format)25 self.logger = logging.getLogger('logger')26 self.logger.setLevel(logging.INFO)27 self.logger.addHandler(file_log_handler)28 self.logger.addHandler(stream_log_handler)29 def run(self):30 while not self.stop_logging.is_set():31 try:32 # receive message33 message = self.log_in.recv_multipart()34 if len(message) > 1:35 # message with content36 [topic, contents] = message37 self.logger.info("[msg] {%s} %s", topic, contents)38 else:39 # subscribe/unsubscribe40 message = message[0]41 topic = message[1:]42 if message.startswith(b'\x00'):43 # unsubscribe44 self.logger.info("[unsub] {%s}", topic)45 elif message.startswith(b'\x01'):46 # subscribe47 self.logger.info("[sub] {%s}", topic)48 else:49 self.logger.warning("[unknown message] %s", message)50 except zmq.ZMQError as e:51 if e.errno == zmq.ETERM:52 self.logger.error("socket error, stopped logging")53 break54 elif e.errno == zmq.EAGAIN:55 pass56 else:57 print(e)58 self.logger.error("unknown error occurred during logging")59def main():60 context = zmq.Context.instance()61 # socket facing clients62 frontend = context.socket(zmq.XSUB)63 frontend.bind("tcp://*:5559")64 # socket facing services65 backend = context.socket(zmq.XPUB)66 backend.bind("tcp://*:5560")67 # log socket68 log_out = context.socket(zmq.PAIR)69 log_out.bind("inproc://logging")70 # start logging thread71 stop_logging = threading.Event()72 logger = Logger(stop_logging)73 logger.start()74 try:75 zmq.proxy(frontend, backend, log_out)76 except KeyboardInterrupt:77 print("shutting down")78 finally:79 frontend.close()80 backend.close()81 stop_logging.set()82 logger.join()83if __name__ == "__main__":...
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!!