Best Python code snippet using PyHamcrest_python
q15.py
Source:q15.py
1#!/usr/bin/env python32# -*- coding=utf-8 -*-3'''4ç¨äºå®ç°ä¸ä¸ªç®åçpla5'''6import os7import re8import random9def getData(path):10 # è¿ä¸æ¥è¦ç¹å«æ³¨æä¸ç¹ï¼ææçåéç°å¨éè¦å¨åééé¢å ä¸x0ï¼é»è®¤ä¸º111 ret = []12 with open(path, "r") as f:13 for line in f.readlines():14 tem = [1]15 for x in re.split(r'[\s]', line.strip()):16 tem.append(float(x))17 ret.append(tem)18 return ret19def sign(n):20 if n <= 0:21 return -122 return 123def multiply_wx(w, x, n):24 sum = 025 for i in range(n):26 sum += w[i] * x[i]27 return sum28def multiply_xy(x, n):29 xy = []30 for i in range(n):31 xy.append(x[i] * x[n])32 return xy33def add(w, xy, n):34 w1 = []35 for i in range(n):36 w1.append(w[i] + xy[i])37 return w138def add17(w, xy, n):39 w1 = []40 for i in range(n):41 w1.append(w[i] + 0.5 * xy[i])42 return w143def pla():44 # è·åæ°æ®45 ret = getData("q15.txt")46 n = len(ret)47 # è¿è¡è®¡ç®48 # ä¿®æ£ç次æ°49 step = 050 # å½åæä½çç´¢å¼51 index = 052 # è¿ç»æ£ç¡®ç次æ°53 correctNum = 054 isFinished = False55 test = 056 w = [0, 0, 0, 0, 0] # é»è®¤wåé57 while not isFinished:58 x = ret[index]59 sum = multiply_wx(w, x, 5)60 if x[5] == sign(sum):61 correctNum += 162 else:63 xy = multiply_xy(x, 5)64 w = add(w, xy, 5)65 step += 166 correctNum = 067 if index == (n - 1):68 index = 069 else:70 index += 171 if correctNum == n:72 isFinished = True73 return step74def pla16():75 # è·åæ°æ®76 ret = getData("q15.txt")77 n = len(ret)78 random.shuffle(ret)79 # è¿è¡è®¡ç®80 # ä¿®æ£ç次æ°81 step = 082 # å½åæä½çç´¢å¼83 index = 084 # è¿ç»æ£ç¡®ç次æ°85 correctNum = 086 isFinished = False87 test = 088 w = [0, 0, 0, 0, 0] # é»è®¤wåé89 while not isFinished:90 x = ret[index]91 sum = multiply_wx(w, x, 5)92 if x[5] == sign(sum):93 correctNum += 194 else:95 xy = multiply_xy(x, 5)96 w = add(w, xy, 5)97 step += 198 correctNum = 099 if index == (n - 1):100 index = 0101 else:102 index += 1103 if correctNum == n:104 isFinished = True105 return step106def pla17():107 # è·åæ°æ®108 ret = getData("q15.txt")109 n = len(ret)110 random.shuffle(ret)111 # è¿è¡è®¡ç®112 # ä¿®æ£ç次æ°113 step = 0114 # å½åæä½çç´¢å¼115 index = 0116 # è¿ç»æ£ç¡®ç次æ°117 correctNum = 0118 isFinished = False119 test = 0120 w = [0, 0, 0, 0, 0] # é»è®¤wåé121 while not isFinished:122 x = ret[index]123 sum = multiply_wx(w, x, 5)124 if x[5] == sign(sum):125 correctNum += 1126 else:127 xy = multiply_xy(x, 5)128 w = add17(w, xy, 5)129 step += 1130 correctNum = 0131 if index == (n - 1):132 index = 0133 else:134 index += 1135 if correctNum == n:136 isFinished = True137 return step138if __name__ == "__main__":139 # ret = pla() # 45140 # print(ret)141 # 40142 # sum = 0143 # for i in range(2000):144 # sum += pla16()145 # print(sum / 2000)146 # 40147 sum = 0148 for i in range(2000):149 sum += pla17()...
commands.py
Source:commands.py
1__author__ = "seamonsters"2import wpilib.command3class CommandWrapper(wpilib.command.Command):4 """5 Wraps another command. This is meant to be extended to do useful things.6 """7 def __init__(self, command):8 super().__init__()9 self.command = command10 for requirement in self.command.getRequirements():11 self.requires(requirement)12 def initialize(self):13 self.command.initialize()14 def execute(self):15 self.command.execute()16 def isFinished(self):17 return self.command.isFinished()18 def interrupted(self):19 self.command.interrupted()20 def end(self):21 self.command.end()22class ForeverCommand(CommandWrapper):23 """24 Wraps a command. isFinished() always returns False.25 """26 def __init__(self, command):27 super().__init__(command)28 def isFinished(self):29 return False30class EnsureFinishedCommand(CommandWrapper):31 """32 Waits until isFinished() of the wrapped command returns True for a certain33 number of steps before finishing.34 """35 def __init__(self, command, requiredCount):36 super().__init__(command)37 self.requiredCount = requiredCount38 self.count = 039 def isFinished(self):40 if self.command.isFinished():41 self.count += 142 else:43 self.count = 044 return self.count > self.requiredCount45class WhileRunningCommand(CommandWrapper):46 """47 Wraps one command, but tracks a different command (``watchCommand``) for48 isFinished().49 """50 def __init__(self, command, watchCommand):51 super().__init__(command)52 self.watchCommand = watchCommand53 def isFinished(self):...
State.py
Source:State.py
1import select, socket, threading, time2from Client import Client3from ServerMessage import *4# Base state class5class BaseServerState:6 def __init__(self):7 None8 def Update(self, clients):9 None10 def IsFinished(self):11 return True12 def GetNextState(self):13 state = Lobby()14 return state15 def OnConnectionDetected(self, client, clients):16 client.close()17class Lobby:18 def __init__(self):19 None20 def Update(self, clients):21 for client in clients:22 try:23 print(client.PopPackage())24 except:25 None26 def IsFinished(self):27 return self.isFinished28 def GetNextState(self):29 state = Loading()30 return state31 def OnConnectionDetected(self, client, clients):32 clients.append(client)33 isFinished = False34class Loading:35 def __init__(self):36 None37 def Update(self, clients):38 None39 def IsFinished(self):40 return self.isFinished41 def GetNextState(self):42 state = GamePlay()43 return state44 def OnConnectionDetected(self, client, clients):45 client.close()46 isFinished = False47class GamePlay:48 def __init__(self):49 None50 def Update(self, clients):51 None52 def IsFinished(self):53 return True54 def GetNextState(self):55 state = Lobby()56 return state57 def OnConnectionDetected(self, client, clients):...
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!!