Best Python code snippet using hypothesis
server.py
Source:server.py
1from bottle import route, run, get, post, response, static_file, request2import os3import RPi.GPIO as GPIO4import threading5import time6GPIO.setmode (GPIO.BOARD)7@route ("/")8def do_root_index ():9 print ("do_root_index (\"/\") is invoked ==> ./index.html will be executed ...")10 return static_file ("index.html", root = ".")11@route ("/gpio", method = "POST")12def read_gpio ():13 gpio_msg = os.popen ("gpio readall")14 return gpio_msg.read ()15@route ("/gpio_write", method = "POST")16def write_pin ():17 pin = int (request.forms.get ("pin"))18 state = int (request.forms.get ("state").strip ())19 if (state == 1):20 GPIO.output (pin, GPIO.LOW)21 else:22 GPIO.output (pin, GPIO.HIGH)23@route ("/gpio_mode", method = "POST")24def set_mode_pin ():25 pin = int (request.forms.get ("pin"))26 mode = request.forms.get ("mode").strip ()27 if (mode == "IN"):28 GPIO.setup (pin, GPIO.OUT)29 else:30 GPIO.setup (pin, GPIO.IN)31@route ("/gpio_pwm_cdc", method = "POST")32def pwm_cdc ():33 pin = request.forms.get ("pin")34 if (pin == ""):35 return "<script style=\"text/javascript\">alert (\"Bad num of pin\");</script>"36 else:37 pin = int (pin)38 if (pwm_list[pin][4] == False):39 return "<script style=\"text/javascript\">alert (\"You need to set pwm data first\");</script>"40 pwm = int (request.forms.get ("pwm"))41 pwm_list[pin][1] = pwm42# pwm_pin, pwm_value, pwm_start, pwm_end43pwm_list = [[None, 0, 0, 0, True] for i in range (41)]44@route ("/gpio_pwm", method = "POST")45def pwm_pin ():46 pwm_pin = request.forms.get ("gpio_pin")47 if (pwm_pin == ""):48 return "<script style=\"text/javascript\">alert (\"Bad num of pin\"); history.back ();</script>"49 else:50 pwm_pin = int (pwm_pin)51 if (pwm_pin < 1 or pwm_pin > 40):52 return "<script style=\"text/javascript\">alert (\"Bad num of pin\"); history.back ();</script>"53 54 pwm_value = request.forms.get ("pwm_value")55 if (pwm_value == ""):56 return "<script style=\"text/javascript\">alert (\"Input num of pwm value\"); history.back ();</script>"57 else:58 pwm_value = int (pwm_value)59 pwm_start = request.forms.get ("pwm_range_start")60 if (pwm_start == ""):61 return "<script style=\"text/javascript\">alert (\"Input num of pwm start \"); history.back ();</script>"62 else:63 pwm_start = int (pwm_start)64 pwm_end = request.forms.get ("pwm_range_end")65 if (pwm_end == ""):66 return "<script style=\"text/javascript\">alert (\"Input num of pwm end \"); history.back ();</script>"67 else:68 pwm_end = int (pwm_end)69 if (pwm_end > 100):70 return "<script style=\"text/javascript\">alert (\"Bad num of pwm end (0 ~ 100)\"); history.back ();</script>"71 72 if (pwm_list[pwm_pin][0] != None):73 pwm_list[pwm_pin][4] = False74 pwm_list[pwm_pin][0] = threading.Thread (target = pwm_thread, daemon = True, args = [pwm_pin])75 pwm_list[pwm_pin][1] = pwm_value76 pwm_list[pwm_pin][2] = pwm_start77 pwm_list[pwm_pin][3] = pwm_end78 pwm_list[pwm_pin][4] = True79 pwm_list[pwm_pin][0].start ()80 return "<script style=\"text/javascript\">history.back ()</script>"81@route ("/gpio_pwm_reset", method = "POST")82def pwm_reset ():83 pwm_pin = request.forms.get ("gpio_pin")84 if (pwm_pin == ""):85 return "<script style=\"text/javascript\">alert (\"Bad num of pin\"); history.back ();</script>"86 else:87 pwm_pin = int (pwm_pin)88 if (pwm_pin < 1 or pwm_pin > 40):89 return "<script style=\"text/javascript\">alert (\"Bad num of pin\"); history.back ();</script>"90 pwm_list[pwm_pin][4] = False91 return "<script style=\"text/javascript\">history.back ()</script>"92def pwm_thread (pin):93 GPIO.setup (pin, GPIO.OUT)94 pwm = GPIO.PWM (pin, pwm_list[pin][3])95 pwm.start (pwm_list[pin][2])96 while pwm_list[pin][4]:97 pwm.ChangeDutyCycle (pwm_list[pin][1])98 time.sleep (0.1)99not_available = [1, 2, 4, 6, 9, 14, 17, 20, 25, 27, 28, 30, 34, 39]100def check_pin_available (pin):101 for i in not_available:102 if pin == i:103 return False104 return True105@route ("/gpio40")106def gpio40_index ():107 for i in range (1, 41):108 if (check_pin_available (i) == True):109 GPIO.setup (i, GPIO.OUT)110 GPIO.output (i, GPIO.LOW)111 print ("gpio40_index (\"/\") is invoked ==> ./gpio40x.html will be executed ...")112 return static_file ("gpio40.html", root = ".")113@route ("/gpio40_output", method = "POST")114def gpio40_output ():115 gpio_pin = int (request.forms.get ("pin"))116 gpio_mode = int (request.forms.get ("mode"))117 GPIO.setup (gpio_pin, GPIO.OUT)118 GPIO.output (gpio_pin, gpio_mode)119@route ("/gpio40_pwm_ready", method = "POST")120def gpio40_pwm_ready ():121 pwm_pin = int (request.forms.get ("pin"))122 if (pwm_list[pwm_pin][0] != None):123 pwm_list[pwm_pin][4] = False124 pwm_list[pwm_pin][0] = threading.Thread (target = pwm_thread, daemon = True, args = [pwm_pin])125 pwm_list[pwm_pin][1] = 0126 pwm_list[pwm_pin][2] = 0127 pwm_list[pwm_pin][3] = 100128 pwm_list[pwm_pin][4] = True129 pwm_list[pwm_pin][0].start ()130@route ("/gpio40_pwm_cdc", method = "POST")131def gpio40_pwm_cdc ():132 pwm_pin = int (request.forms.get ("pin"))133 pwm_value = int (request.forms.get ("pwm"))134 pwm_list[pwm_pin][1] = pwm_value135@route ("/gpio40_pwm_stop", method = "POST")136def gpio40_pwm_stop ():137 pwm_pin = int (request.forms.get ("pin"))138 pwm_list[pwm_pin][4] = False139run (host = "165.229.185.179", port = 80, server = "paste")...
claw_joystick.py
Source:claw_joystick.py
1import logging2import pigpio3from games.claw.config import (4 BOTTOM_PIN,5 JOYSTICK_STATE_OFF,6 JOYSTICK_STATE_ON,7 LEFT_PIN,8 MIN_AMOUNT,9 RIGHT_PIN,10 TOP_PIN,11)12from surrortg.inputs import Directions, Joystick13DIR_PINS = [TOP_PIN, LEFT_PIN, BOTTOM_PIN, RIGHT_PIN]14DIRECTION_CMD_MAP = {15 Directions.TOP: {16 "on": [TOP_PIN],17 "off": [BOTTOM_PIN, LEFT_PIN, RIGHT_PIN],18 },19 Directions.BOTTOM: {20 "on": [BOTTOM_PIN],21 "off": [TOP_PIN, LEFT_PIN, RIGHT_PIN],22 },23 Directions.LEFT: {24 "on": [LEFT_PIN],25 "off": [TOP_PIN, BOTTOM_PIN, RIGHT_PIN],26 },27 Directions.RIGHT: {28 "on": [RIGHT_PIN],29 "off": [TOP_PIN, BOTTOM_PIN, LEFT_PIN],30 },31 Directions.TOP_LEFT: {32 "on": [TOP_PIN, LEFT_PIN],33 "off": [BOTTOM_PIN, RIGHT_PIN],34 },35 Directions.TOP_RIGHT: {36 "on": [TOP_PIN, RIGHT_PIN],37 "off": [BOTTOM_PIN, LEFT_PIN],38 },39 Directions.BOTTOM_LEFT: {40 "on": [BOTTOM_PIN, LEFT_PIN],41 "off": [TOP_PIN, RIGHT_PIN],42 },43 Directions.BOTTOM_RIGHT: {44 "on": [BOTTOM_PIN, RIGHT_PIN],45 "off": [TOP_PIN, LEFT_PIN],46 },47 Directions.MIDDLE: {48 "on": [],49 "off": [TOP_PIN, BOTTOM_PIN, LEFT_PIN, RIGHT_PIN],50 },51}52class ClawJoystick(Joystick):53 def __init__(self, pi):54 self.set_min_amount(MIN_AMOUNT)55 self.pi = pi56 # Set output pins57 for dir_pin in DIR_PINS:58 self.pi.set_mode(dir_pin, pigpio.OUTPUT)59 # Stop claw movement60 self.move(Directions.MIDDLE)61 async def handle_coordinates(self, x, y, seat=0):62 direction = self.get_direction_8(x, y)63 self.move(direction)64 def move(self, direction):65 logging.debug(f"Moving {direction}")66 gpio_cmds = DIRECTION_CMD_MAP[direction]67 for off_pin in gpio_cmds["off"]:68 self.pi.write(off_pin, JOYSTICK_STATE_OFF)69 for on_pin in gpio_cmds["on"]:70 self.pi.write(on_pin, JOYSTICK_STATE_ON)71 async def reset(self, seat=0):...
Motor.py
Source:Motor.py
1import RPi.GPIO as GPIO2import time3import sys4class Motor(object):5 '''Motor control'''6 def __init__(self,ENA,IN1,IN2,IN3,IN4,ENB):7 '''Specify motor pins'''8 self.enab_pin=[ENA,ENB] #Enable pins9 self.inx_pin=[IN1,IN2,IN3,IN4] #Control pins10 11 self.RightAhead_pin=self.inx_pin[3]12 self.RightBack_pin=self.inx_pin[2]13 self.LeftAhead_pin=self.inx_pin[1]14 self.LeftBack_pin=self.inx_pin[0]15 self.setup()16 def setup(self):17 GPIO.setmode(GPIO.BCM)18 GPIO.setwarnings(False)19 for pin in self.inx_pin:20 GPIO.setup(pin,GPIO.OUT)21 GPIO.output(pin,GPIO.LOW)22 pin=None23 for pin in self.enab_pin:24 GPIO.setup(pin,GPIO.OUT)25 GPIO.output(pin,GPIO.HIGH)26 def ahead(self):27 for pin in self.inx_pin:28 GPIO.output(pin,GPIO.LOW)29 GPIO.output(self.RightAhead_pin,GPIO.HIGH)30 GPIO.output(self.LeftAhead_pin,GPIO.HIGH)31 def left(self):32 for pin in self.inx_pin:33 GPIO.output(pin,GPIO.LOW)34 GPIO.output(self.RightAhead_pin,GPIO.HIGH)35 GPIO.output(self.LeftBack_pin,GPIO.HIGH)36 def right(self):37 for pin in self.inx_pin:38 GPIO.output(pin,GPIO.LOW)39 GPIO.output(self.LeftAhead_pin,GPIO.HIGH)40 GPIO.output(self.RightBack_pin,GPIO.HIGH)41 def rear(self):42 for pin in self.inx_pin:43 GPIO.output(pin,GPIO.LOW)44 GPIO.output(self.RightBack_pin,GPIO.HIGH)45 GPIO.output(self.LeftBack_pin,GPIO.HIGH)46 def stop(self):47 for pin in self.inx_pin:48 GPIO.output(pin,GPIO.LOW)49 def clear(self):...
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!!