Best Python code snippet using tox_python
PowertrainClass2.py
Source:PowertrainClass2.py
1#!/usr/bin/python32###############################3### IMPORT LIBRARIES ###4###############################5from AxleClass import AxleClass6from BatteryClass import BatteryClass7from BatteryManagementClass import BatteryManagementClass8from ControllerClass import ControllerClass9from EscClass import ESC10from MotorClass import MotorClass11from WheelClass import WheelClass12from statistics import mean13class PowertrainControllerClass(object):14 '''Electric vehicle powertrain class'''15 _axle = None16 _bms = None17 _esc = None18 _motor = None19 _wheel = [ None, None, None, None ]20 _ctrl_speed = None21 _ctrl_motor = None22 _ctrl_brake = None23 _speed_set = None24 _speed_now = None25 _mode = None26 _car_mass = None27 ###############################28 ### INITIALISATION ###29 ###############################30 def __init__(self, kwargs):31 kwargs.update(dict(i_max_charge=(kwargs.get('batt_p_max')*0.5/kwargs.get('batt_v_min')),32 i_max_discharge=kwargs.get('batt_i_max'),33 p_max=kwargs.get('batt_p_max'),34 batt_kwh=kwargs.get('batt_kwh'),35 v_min=kwargs.get('batt_v_min'),36 v_max=kwargs.get('batt_v_max'),37 ))38 self._axle = AxleClass()39 self._battery = BatteryClass(kwargs)40 self._bms = BatteryManagementClass(kwargs)41 self._esc = ESC(kwargs)42 self._motor = MotorClass(kwargs)43 self._wheel = [ WheelClass(kwargs), # FL44 WheelClass(kwargs), # FR45 WheelClass(kwargs), # RL46 WheelClass(kwargs) ] # RR47 self._ctrl_speed = ControllerClass(50, 5, -0.8, "signed")48 self._ctrl_motor = ControllerClass(2, 0, -0.075, "unsigned")49 self._ctrl_brake = ControllerClass(1, 0, -0.075, "unsigned")50 self._speed_target = 0.051 self._speed = 0.052 self._car_mass = kwargs.get('car_mass')53 self._bms.set_battery_data(self._battery.battery_data)54 self._esc.set_input_power(self._bms.availability)55 ###############################56 ### UPDATE LOOP ###57 ###############################58 def update(self, dt):59 # Get wheel speeds and torques, pass to axle then motor60 #self._axle.wheel_data = [ self._wheel[0].rotational_data, self._wheel[1].rotational_data ]61 #self._motor.rotational_data = self._axle.shaft_data.rotational_data62 # Get battery availability, pass to BMS & ESC63 self._bms.set_battery_data(self._battery.battery_data)64 65 # Parse control signal from speed controller66 self._parse_control_signal(dt)67 68 # Send motor control signal to ESC69 self._esc.control_signal = self._ctrl_motor.value70 self._esc.set_input_power(self._bms.availability)71 # Send brake control signal to wheels72 for ptr in self._wheel:73 ptr.brake_control_sig = self._ctrl_brake.value74 # Give (or receive) electricity to (or from) motor75 self._motor.set_electricity(self._esc.voltage, self._esc.current)76 # Calculate voltage boost in ESC to charge battery if in regen77 self._bms.current = self._esc.current78 self._battery.current = self._bms.current79 # Update axle torque and speed, pass to drive wheels80 self._motor.update(dt)81 self._axle.shaft_data = self._motor.rotational_data82 self._axle.update(dt)83 self._wheel[0].axle_data = self._axle.wheel_data[0].rotational_data84 self._wheel[1].axle_data = self._axle.wheel_data[1].rotational_data85 for ptr in self._wheel:86 ptr.update(dt)87 # Update time dependancies (energy calcs)88 self._battery.update(dt)89 self._bms.update(dt)90 self._esc.update(dt)91 ###############################92 ### CONTROL DECODER ###93 ############################### 94 def _parse_control_signal(self, dt):95 dv = self.target_speed - self.vehicle_speed96 self._ctrl_speed.update(dt, dv)97 mode = 0.098 if self._ctrl_speed.value <= 0.0:99 # Trying to decelerate100 self._ctrl_motor.update(dt, self._ctrl_speed.value)101 if self._ctrl_motor.at_minimum:102 # Motor fully off or in full regen, apply brakes103 self._ctrl_brake.update(dt, -1.0*self._ctrl_speed.value)104 mode = 1.0105 else:106 # In regen or coasting, no brakes applied107 self._ctrl_brake.reset()108 self._ctrl_brake.update(dt)109 mode = 2.0110 else:111 # Trying to accelerate112 self._ctrl_brake.update(dt, -1.0*self._ctrl_speed.value)113 if self._ctrl_brake.at_minimum:114 # Brake fully off, power motor115 self._ctrl_motor.update(dt, self._ctrl_speed.value)116 mode = 3.0117 else:118 # Brake on but trying to speed up119 self._ctrl_motor.update(dt)120 mode = 4.0121 self._mode = mode122 if self.target_speed <=0.01 and self.vehicle_speed <=0.01:123 self._ctrl_speed.anti_wind_up()124 self._ctrl_motor.anti_wind_up()125 self._ctrl_brake.anti_wind_up()126 ###############################127 ### GETTERS ###128 ###############################129 # Actual vehicle speed130 @property131 def vehicle_speed(self):132 return self._speed133 # Target speed134 @property135 def target_speed(self):136 return self._speed_target137 # Road Friction Drag138 @property139 def road_drag(self):140 return sum(ptr.road_drag for ptr in self._wheel)141 # Powertrain Force142 @property143 def force(self):144 return sum(ptr.force for ptr in self._wheel)145 # Speed Controller PID146 @property147 def speed_ctrl_error(self):148 return self._ctrl_speed.value149 @property150 def speed_ctrl_pid(self):151 return self._ctrl_speed.pid_data152 # Motor Controller PID153 @property154 def motor_ctrl_error(self):155 return self._ctrl_motor.value156 @property157 def motor_ctrl_pid(self):158 return self._ctrl_motor.pid_data159 # Brake Controller PID160 @property161 def brake_ctrl_error(self):162 return self._ctrl_brake.value163 @property164 def brake_ctrl_pid(self):165 return self._ctrl_brake.pid_data166 # Motor Rotational167 @property168 def motor_rotation(self):169 return self._motor.rotational_data170 # Axle Rotation171 @property172 def axle_rotation(self):173 return self._axle.shaft_data.rotational_data174 # Wheel Rotational175 @property176 def wheel_rotation(self):177 return [ self._wheel[0].rotational_data,178 self._wheel[1].rotational_data,179 self._wheel[2].rotational_data,180 self._wheel[3].rotational_data ]181 # Odometer182 @property183 def odometer(self):184 return mean([ self._wheel[0].odometer,185 self._wheel[1].odometer,186 self._wheel[2].odometer,187 self._wheel[3].odometer ])188 # Brake Rotational189 @property190 def brake_rotation(self):191 return [ self._wheel[0].brake_rotational,192 self._wheel[1].brake_rotational,193 self._wheel[2].brake_rotational,194 self._wheel[3].brake_rotational ]195 # Battery Electricity196 @property197 def battery_electricity(self):198 return self._battery.battery_data199 # ESC Electricity200 @property201 def esc_electricity(self):202 return self._esc.electricity_data203 # Motor Electricity204 @property205 def motor_electricity(self):206 return self._motor.electricity_data207 # Controller Mode208 @property209 def controller_mode(self):210 return self._mode211 ###############################212 ### SETTERS ###213 ###############################214 # Actual vehicle speed215 @vehicle_speed.setter216 def vehicle_speed(self, speed):217 self._speed = speed218 for ptr in self._wheel:219 ptr.vehicle_speed_feedback(speed)220 self._axle.wheel_speed_feedback(self._wheel[0].speed, self._wheel[1].speed) 221 self._motor.shaft_speed_feedback(self._axle.shaft_data.speed)222 # Target speed223 @target_speed.setter224 def target_speed(self, speed):225 self._speed_target = speed226 227###############################228###############################229###### END ######230###############################...
wheelcypher.py
Source:wheelcypher.py
1# Written by Sam Aikin Aug 13, 2021.2#3# Copyright (c) 2021 Sam Aikin4#5# Permission is hereby granted, free of charge, to any person obtaining a copy6# of this software and associated documentation files (the "Software"), to deal7# in the Software without restriction, including without limitation the rights8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9# copies of the Software, and to permit persons to whom the Software is10# furnished to do so, subject to the following conditions:11#12# The above copyright notice and this permission notice shall be included in all13# copies or substantial portions of the Software.14#15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21# SOFTWARE.22#23# All rights reserved.24#25# A simple proof of concept implementation of a Wheel Cypher or a Jefferson Disk.26#27# https://en.wikipedia.org/wiki/Jefferson_disk28#29# A WHEEL contains a SEQUENCE of letters (this is the base encoding mechanisms)30# a DRUM contains a SEQUENCE of WHEELs (this is how a message is encoded / decoded)31class Wheel:32 elements = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '*']33 def __init__(self, sequence: list):34 if len(sequence) != 27 or sum(sequence) != 378:35 raise ValueError('Bad Wheel Sequence Passed')36 self._wheel = []37 for i in range(0, len(sequence)):38 self._wheel += self.elements[sequence[i] - 1]39 def encode(self, coded_letter: str):40 # first item is letter being encoded, so encoding value can be checked.41 offset = self._wheel.index(coded_letter)42 result = []43 for i in range(0, len(self._wheel)):44 index = (offset + i) % len(self._wheel)45 result += self._wheel[index]46 return result47class Drum:48 seq1 = [1, 27, 12, 15, 22, 20, 26, 19, 13, 8, 14, 10, 18,49 21, 9, 5, 23, 3, 4, 17, 16, 11, 2, 25, 7, 24, 6]50 seq2 = [1, 24, 26, 20, 11, 25, 13, 8, 16, 5, 19, 4, 21,51 18, 6, 12, 23, 9, 22, 15, 7, 14, 2, 17, 27, 10, 3]52 seq3 = [1, 10, 4, 23, 17, 9, 25, 3, 6, 24, 13, 21, 19, 7, 12,53 2, 26, 20, 8, 27, 18, 22, 5, 14, 11, 16, 15]54 seq4 = [1, 5, 22, 4, 17, 21, 12, 9, 2, 19, 10, 27, 8, 14, 16,55 6, 23, 13, 25, 11, 7, 24, 3, 26, 20, 18, 15]56 seq5 = [1, 8, 21, 14, 6, 18, 9, 12, 20, 7, 27, 13, 4, 15, 17,57 16, 5, 24, 19, 25, 10 ,11, 22, 23, 2, 3, 26]58 seq6 = [1, 5, 21, 13, 4, 7, 26, 18, 10, 2, 17, 20, 24, 3, 27,59 19, 12, 25, 6, 16, 14, 9, 22, 15, 8, 11, 23]60 # for now, fixed set of wheels, matching drum bought at Valley Forge.61 # Could be expanded to include ability to select an arbitrary order of wheels.62 def __init__(self):63 self._wheel = []64 while len(self._wheel) < 12:65 self._wheel += [Wheel(self.seq1)]66 self._wheel += [Wheel(self.seq2)]67 self._wheel += [Wheel(self.seq3)]68 self._wheel += [Wheel(self.seq4)]69 self._wheel += [Wheel(self.seq5)]70 self._wheel += [Wheel(self.seq6)]71 def encode(self, coded_str: str):72 letters = [str(letter) for letter in coded_str]73 # ensure that coded messages are always multiples of 1274 while len(letters) % 12 != 0:75 letters += 'Z'76 encodings = []77 for i in range(0, len(letters)):78 encodings += [self._wheel[i % 12].encode(letters[i])]79 result = []80 for i in range(0, len(encodings[0])):81 encoded_string = []82 for j in range(0, len(letters)):83 encoded_string += encodings[j][i]84 result += [''.join(encoded_string)]85 return result86if __name__ == "__main__":87 encoder = Drum()88 to_encode = input('Enter string to encode: ').upper()89 message = encoder.encode(to_encode)90 for i in range(0, len(message)):...
mouse.py
Source:mouse.py
1class Mouse:2 def __init__(self):3 self._position = (0, 0)4 self._previous_state = False5 self._state = False6 self._wheel = 07 def kivy_frame(self):8 self._wheel = 09 self._previous_state = self._state10 self._state = False11 def kivy_update_position(self, etype, motionevent):12 if etype:13 self._position = motionevent14 def kivy_update_state(self, instance, touch):15 try:16 if touch.button == 'left':17 self._state = True18 except AttributeError:19 pass20 if touch.is_mouse_scrolling:21 self._wheel = 1 if touch.button == 'scrolldown' else -122 def pygame_update(self, wheel=0):23 '''24 self._previous_state = self._state25 #self._state = pygame.mouse.get_pressed(3)[0]26 #self._position = pygame.mouse.get_pos()27 self._wheel = wheel28 '''29 def position(self):30 return self._position31 def pressed(self):32 return self._state and not self._previous_state33 def released(self):34 return self._previous_state and not self._state35 def wheel(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!!