Best Python code snippet using autotest_python
cimxml_parse.py
Source:cimxml_parse.py
...43 (event, node) = parser.next()44 if event != END_ELEMENT or node.tagName != tagName:45 raise ParseError(46 'Expecting %s end tag, got %s %s' % (tagName, event, node.tagName))47def is_start(event, node, tagName):48 """Return true if (event, node) is a start event for tagname."""49 return event == START_ELEMENT and node.tagName == tagName50def is_end(event, node, tagName):51 """Return true if (event, node) is an end event for tagname."""52 return event == END_ELEMENT and node.tagName == tagName53# <!-- ************************************************** -->54# <!-- Root element -->55# <!-- ************************************************** -->56# <!ELEMENT CIM (MESSAGE | DECLARATION)>57# <!ATTLIST CIM58# CIMVERSION CDATA #REQUIRED59# DTDVERSION CDATA #REQUIRED60# >61# <!-- ************************************************** -->62# <!-- Object declaration elements -->63# <!-- ************************************************** -->64# <!ELEMENT DECLARATION (DECLGROUP | DECLGROUP.WITHNAME | DECLGROUP.WITHPATH)+>65# <!ELEMENT DECLGROUP ((LOCALNAMESPACEPATH | NAMESPACEPATH)?, QUALIFIER.DECLARATION*, VALUE.OBJECT*)>66# <!ELEMENT DECLGROUP.WITHNAME ((LOCALNAMESPACEPATH | NAMESPACEPATH)?, QUALIFIER.DECLARATION*, VALUE.NAMEDOBJECT*)>67# <!ELEMENT DECLGROUP.WITHPATH (VALUE.OBJECTWITHPATH | VALUE.OBJECTWITHLOCALPATH)*>68# <!ELEMENT QUALIFIER.DECLARATION (SCOPE?, (VALUE | VALUE.ARRAY)?)>69# <!ATTLIST QUALIFIER.DECLARATION 70# %CIMName; 71# %CIMType; #REQUIRED72# ISARRAY (true|false) #IMPLIED73# %ArraySize;74# %QualifierFlavor;>75# <!ELEMENT SCOPE EMPTY>76# <!ATTLIST SCOPE77# CLASS (true | false) "false"78# ASSOCIATION (true | false) "false"79# REFERENCE (true | false) "false"80# PROPERTY (true | false) "false"81# METHOD (true | false) "false"82# PARAMETER (true | false) "false"83# INDICATION (true | false) "false"84# >85# <!-- ************************************************** -->86# <!-- Object Value elements -->87# <!-- ************************************************** -->88# <!ELEMENT VALUE (#PCDATA)>89def parse_value(parser, event, node):90 value = ''91 (next_event, next_node) = parser.next()92 if next_event == CHARACTERS: 93 value = next_node.nodeValue94 (next_event, next_node) = parser.next()95 96 if not is_end(next_event, next_node, 'VALUE'):97 raise ParseError('Expecting end VALUE')98 99 return value100# <!ELEMENT VALUE.ARRAY (VALUE*)>101def parse_value_array(parser, event, node):102 value_array = []103 (next_event, next_node) = parser.next()104 if is_start(next_event, next_node, 'VALUE'):105 value_array.append(parse_value(parser, next_event, next_node))106 while 1:107 (next_event, next_node) = parser.next()108 if is_end(next_event, next_node, 'VALUE.ARRAY'):109 break110 if is_start(next_event, next_node, 'VALUE'):111 value_array.append(parse_value(parser, next_event, next_node))112 else:113 raise ParseError('Expecting VALUE element')114 return value_array115# <!ELEMENT VALUE.REFERENCE (CLASSPATH | LOCALCLASSPATH | CLASSNAME |116# INSTANCEPATH | LOCALINSTANCEPATH | INSTANCENAME)>117def parse_value_reference(parser, event, node):118 (next_event, next_node) = parser.next()119 if is_start(next_event, next_node, 'CLASSPATH'):120 result = parse_classpath(parser, next_event, next_node)121 elif is_start(next_event, next_node, 'LOCALCLASSPATH'):122 result = parse_localclasspath(parser, next_event, next_node)123 elif is_start(next_event, next_node, 'CLASSNAME'):124 result = parse_classname(parser, next_event, next_node)125 elif is_start(next_event, next_node, 'INSTANCEPATH'):126 result = parse_instancepath(parser, next_event, next_node)127 elif is_start(next_event, next_node, 'LOCALINSTANCEPATH'):128 result = parse_localinstancepath(parser, next_event, next_node)129 elif is_start(next_event, next_node, 'INSTANCENAME'):130 result = parse_instancename(parser, next_event, next_node)131 132 else:133 raise ParseError('Expecting (CLASSPATH | LOCALCLASSPATH | CLASSNAME '134 '| INSTANCEPATH | LOCALINSTANCEPATH | INSTANCENAME)')135 136 get_end_event(parser, 'VALUE.REFERENCE')137 return result138# <!ELEMENT VALUE.REFARRAY (VALUE.REFERENCE*)>139# <!ELEMENT VALUE.OBJECT (CLASS | INSTANCE)>140# <!ELEMENT VALUE.NAMEDINSTANCE (INSTANCENAME, INSTANCE)>141# <!ELEMENT VALUE.NAMEDOBJECT (CLASS | (INSTANCENAME, INSTANCE))>142# <!ELEMENT VALUE.OBJECTWITHLOCALPATH ((LOCALCLASSPATH, CLASS) | (LOCALINSTANCEPATH, INSTANCE))>143# <!ELEMENT VALUE.OBJECTWITHPATH ((CLASSPATH, CLASS) | (INSTANCEPATH, INSTANCE))>144# <!ELEMENT VALUE.NULL EMPTY>145 146# <!-- ************************************************** -->147# <!-- Object naming and locating elements -->148# <!-- ************************************************** -->149# <!ELEMENT NAMESPACEPATH (HOST, LOCALNAMESPACEPATH)>150def parse_namespacepath(parser, event, node):151 152 (next_event, next_node) = parser.next()153 if not is_start(next_event, next_node, 'HOST'):154 raise ParseError('Expecting HOST')155 156 host = parse_host(parser, next_event, next_node)157 (next_event, next_node) = parser.next()158 if not is_start(next_event, next_node, 'LOCALNAMESPACEPATH'):159 raise ParseError('Expecting LOCALNAMESPACEPATH')160 161 namespacepath = parse_localnamespacepath(parser, next_event, next_node)162 (next_event, next_node) = parser.next()163 if not is_end(next_event, next_node, 'NAMESPACEPATH'):164 raise ParseError('Expecting end NAMESPACEPATH')165 return (host, namespacepath)166# <!ELEMENT LOCALNAMESPACEPATH (NAMESPACE+)>167def parse_localnamespacepath(parser, event, node):168 (next_event, next_node) = parser.next()169 namespaces = []170 if not is_start(next_event, next_node, 'NAMESPACE'):171 print next_event, next_node172 raise ParseError('Expecting NAMESPACE')173 namespaces.append(parse_namespace(parser, next_event, next_node))174 while 1:175 (next_event, next_node) = parser.next()176 if is_end(next_event, next_node, 'LOCALNAMESPACEPATH'):177 break178 if is_start(next_event, next_node, 'NAMESPACE'):179 namespaces.append(parse_namespace(parser, next_event, next_node))180 else:181 raise ParseError('Expecting NAMESPACE')182 return string.join(namespaces, '/')183# <!ELEMENT HOST (#PCDATA)>184def parse_host(parser, event, node):185 host = ''186 (next_event, next_node) = parser.next()187 if next_event == CHARACTERS: 188 host = next_node.nodeValue189 (next_event, next_node) = parser.next()190 191 if not is_end(next_event, next_node, 'HOST'):192 raise ParseError('Expecting end HOST')193 194 return host195# <!ELEMENT NAMESPACE EMPTY>196# <!ATTLIST NAMESPACE197# %CIMName; 198# >199def parse_namespace(parser, event, node):200 name = get_required_attribute(node, 'NAME')201 (next_event, next_node) = parser.next()202 if not is_end(next_event, next_node, 'NAMESPACE'):203 raise ParseError('Expecting end NAMESPACE')204 return name205# <!ELEMENT CLASSPATH (NAMESPACEPATH, CLASSNAME)>206# <!ELEMENT LOCALCLASSPATH (LOCALNAMESPACEPATH, CLASSNAME)>207# <!ELEMENT CLASSNAME EMPTY>208# <!ATTLIST CLASSNAME209# %CIMName; 210# >211# <!ELEMENT INSTANCEPATH (NAMESPACEPATH, INSTANCENAME)>212def parse_instancepath(parser, event, node):213 (next_event, next_node) = parser.next()214 if not is_start(next_event, next_node, 'NAMESPACEPATH'):215 raise ParseError('Expecting NAMESPACEPATH')216 217 host, namespacepath = parse_namespacepath(parser, next_event, next_node)218 (next_event, next_node) = parser.next()219 if not is_start(next_event, next_node, 'INSTANCENAME'):220 print next_event, next_node221 raise ParseError('Expecting INSTANCENAME')222 instancename = parse_instancename(parser, next_event, next_node)223 instancename.host = host224 instancename.namespace = namespacepath225 return instancename226# <!ELEMENT LOCALINSTANCEPATH (LOCALNAMESPACEPATH, INSTANCENAME)>227def parse_localinstancepath(parser, event, node):228 (next_event, next_node) = parser.next()229 if not is_start(next_event, next_node, 'LOCALNAMESPACEPATH'):230 raise ParseError('Expecting LOCALNAMESPACEPATH')231 232 namespacepath = parse_localnamespacepath(parser, next_event, next_node)233 (next_event, next_node) = parser.next()234 if not is_start(next_event, next_node, 'INSTANCENAME'):235 raise ParseError('Expecting INSTANCENAME')236 instancename = parse_instancename(parser, next_event, next_node)237 instancename.namespace = namespacepath238 return instancename239# <!ELEMENT INSTANCENAME (KEYBINDING* | KEYVALUE? | VALUE.REFERENCE?)>240# <!ATTLIST INSTANCENAME241# %ClassName; 242# >243def parse_instancename(parser, event, node):244 245 classname = get_required_attribute(node, 'CLASSNAME')246 keybindings = []247 248 (next_event, next_node) = parser.next()249 if is_start(next_event, next_node, 'KEYBINDING'):250 keybindings.append(parse_keybinding(parser, next_event, next_node))251 while 1:252 (next_event, next_node) = parser.next()253 if is_end(next_event, next_node, 'INSTANCENAME'):254 break255 if is_start(next_event, next_node, 'KEYBINDING'):256 keybindings.append(257 parse_keybinding(parser, next_event, next_node))258 else:259 raise ParseError('Expecting KEYBINDING element')260 261 if is_end(next_event, next_node, 'INSTANCENAME'):262 pass263 264 elif is_start(next_event, next_node, 'KEYVALUE'):265 keybindings.append(('', parse_keyvalue(parser, next_event, next_node)))266 267 elif is_start(next_event, next_node, 'VALUE.REFERENCE'): 268 keybindings.append(269 parse_value_reference(parser, next_event, next_node))270 271 else:272 raise ParseError(273 'Expecting KEYBINDING* | KEYVALUE? | VALUE.REFERENCE')274 return CIMInstanceName(classname, keybindings)275# <!ELEMENT OBJECTPATH (INSTANCEPATH | CLASSPATH)>276# <!ELEMENT KEYBINDING (KEYVALUE | VALUE.REFERENCE)>277# <!ATTLIST KEYBINDING278# %CIMName; 279# >280def parse_keybinding(parser, event, node):281 name = get_required_attribute(node, 'NAME')282 (next_event, next_node) = parser.next()283 if is_start(next_event, next_node, 'KEYVALUE'):284 keyvalue = parse_keyvalue(parser, next_event, next_node)285 result = (name, keyvalue)286 elif is_start(next_event, next_node, 'VALUE.REFERENCE'):287 value_reference = parse_value_reference(parser, next_event, next_node)288 result = (name, value_reference)289 else:290 raise ParseError('Expecting KEYVALUE or VALUE.REFERENCE element')291 get_end_event(parser, 'KEYBINDING')292 return result293# <!ELEMENT KEYVALUE (#PCDATA)>294# <!ATTLIST KEYVALUE295# VALUETYPE (string | boolean | numeric) "string"296# %CIMType; #IMPLIED>297def parse_keyvalue(parser, event, node):298 valuetype = get_required_attribute(node, 'VALUETYPE')299 type = get_attribute(node, 'TYPE')300 (next_event, next_node) = parser.next()301 if next_event != CHARACTERS:302 raise ParseError('Expecting character data')303 304 value = next_node.nodeValue305 if valuetype == 'string':306 pass307 elif valuetype == 'boolean':308 # CIM-XML says "These values MUST be treated as309 # case-insensitive" (even though the XML definition310 # requires them to be lowercase.)311 p = value.strip().lower()312 if p == 'true':313 value = True314 elif p == 'false':315 value = False316 else:317 raise ParseError('invalid boolean value "%s"' % `p`)318 elif valuetype == 'numeric':319 try: 320 # XXX: Use TYPE attribute to create named CIM type.321 # if attrs(tt).has_key('TYPE'):322 # return cim_obj.tocimobj(attrs(tt)['TYPE'], p.strip())323 # XXX: Would like to use long() here, but that tends to cause324 # trouble when it's written back out as '2L'325 value = int(value.strip(), 0)326 except ValueError:327 raise ParseError(328 'invalid numeric value "%s"' % value)329 else:330 raise ParseError('Invalid VALUETYPE')331 get_end_event(parser, 'KEYVALUE')332 return value333# <!-- ************************************************** -->334# <!-- Object definition elements -->335# <!-- ************************************************** -->336# <!ELEMENT CLASS (QUALIFIER*, (PROPERTY | PROPERTY.ARRAY | PROPERTY.REFERENCE)*, METHOD*)>337# <!ATTLIST CLASS338# %CIMName; 339# %SuperClass; 340# >341# <!ELEMENT INSTANCE (QUALIFIER*, (PROPERTY | PROPERTY.ARRAY |342# PROPERTY.REFERENCE)*)>343# <!ATTLIST INSTANCE344# %ClassName;345# xml:lang NMTOKEN #IMPLIED 346# >347def parse_instance(parser, event, node):348 classname = get_required_attribute(node, 'CLASSNAME')349 properties = []350 qualifiers = []351 (next_event, next_node) = parser.next()352 if is_start(next_event, next_node, 'QUALIFIER'):353 qualifiers.append(parse_qualifier(parser, next_event, next_node))354 while 1:355 (next_event, next_node) = parser.next()356 if is_start(next_event, next_node, 'PROPERTY') or \357 is_start(next_event, next_node, 'PROPERTY.ARRAY') or \358 is_start(next_event, next_node, 'PROPERTY.REFERENCE') or \359 is_end(next_event, next_node, 'INSTANCE'):360 break361 362 if is_start(next_event, next_node, 'QUALIFIER'):363 qualifiers.append(364 parse_qualifier(parser, next_event, next_node))365 else:366 raise ParseError('Expecting QUALIFIER')367 while 1:368 if is_end(next_event, next_node, 'INSTANCE'):369 break370 if is_start(next_event, next_node, 'PROPERTY'):371 properties.append(parse_property(parser, next_event, next_node))372 elif is_start(next_event, next_node, 'PROPERTY.ARRAY'):373 properties.append(374 parse_property_array(parser, next_event, next_node))375 elif is_start(next_event, next_node, 'PROPERTY.REFERENCE'):376 properties.append(377 parse_property_reference(parser, next_event, next_node))378 else:379 raise ParseError(380 'Expecting (PROPERTY | PROPERTY.ARRAY | PROPERTY.REFERENCE)')381 (next_event, next_node) = parser.next()382 if not is_end(next_event, next_node, 'INSTANCE'):383 raise ParseError('Expecting end INSTANCE')384 return CIMInstance(classname,385 properties = dict([(x.name, x) for x in properties]),386 qualifiers = dict([(x.name, x) for x in qualifiers]))387 388 389# <!ELEMENT QUALIFIER ((VALUE | VALUE.ARRAY)?)>390# <!ATTLIST QUALIFIER 391# %CIMName;392# %CIMType; #REQUIRED393# %Propagated;394# %QualifierFlavor;395# xml:lang NMTOKEN #IMPLIED 396# >397def parse_qualifier(parser, event, node):398 name = get_required_attribute(node, 'NAME')399 type = get_required_attribute(node, 'TYPE')400 propagated = get_attribute(node, 'PROPAGATED')401 (next_event, next_node) = parser.next()402 if is_end(next_event, next_node, 'QUALIFIER'):403 return CIMQualifier(name, None, type = type)404 if is_start(next_event, next_node, 'VALUE'):405 value = parse_value(parser, next_event, next_node)406 elif is_start(next_event, next_node, 'VALUE.ARRAY'):407 value = parse_value_array(parser, next_event, next_node)408 else:409 raise ParseError('Expecting (VALUE | VALUE.ARRAY)')410 result = CIMQualifier(name, tocimobj(type, value))411 get_end_event(parser, 'QUALIFIER')412 return result413# <!ELEMENT PROPERTY (QUALIFIER*, VALUE?)>414# <!ATTLIST PROPERTY 415# %CIMName;416# %ClassOrigin;417# %Propagated;418# %CIMType; #REQUIRED419# xml:lang NMTOKEN #IMPLIED 420# >421def parse_property(parser, event, node):422 423 name = get_required_attribute(node, 'NAME')424 type = get_required_attribute(node, 'TYPE')425 class_origin = get_attribute(node, 'CLASSORIGIN')426 propagated = get_attribute(node, 'PROPAGATED')427 qualifiers = []428 value = None429 (next_event, next_node) = parser.next()430 if is_start(next_event, next_node, 'QUALIFIER'):431 qualifiers.append(parse_qualifier(parser, next_event, next_node))432 while 1:433 (next_event, next_node) = parser.next()434 if is_start(next_event, next_node, 'VALUE'):435 break436 if is_start(next_event, next_node, 'QUALIFIER'):437 qualifiers.append(438 parse_qualifier(parser, next_event, next_node))439 else:440 raise ParseError('Expecting QUALIFIER')441 if is_start(next_event, next_node, 'VALUE'):442 value = parse_value(parser, next_event, next_node)443 (next_event, next_node) = parser.next()444 if not is_end(next_event, next_node, 'PROPERTY'):445 raise ParseError('Expecting end PROPERTY')446 return CIMProperty(name,447 tocimobj(type, value),448 type = type,449 class_origin = class_origin,450 propagated = propagated,451 qualifiers = dict([(x.name, x) for x in qualifiers]))452# <!ELEMENT PROPERTY.ARRAY (QUALIFIER*, VALUE.ARRAY?)>453# <!ATTLIST PROPERTY.ARRAY 454# %CIMName;455# %CIMType; #REQUIRED456# %ArraySize;457# %ClassOrigin;458# %Propagated;459# xml:lang NMTOKEN #IMPLIED 460# >461def parse_property_array(parser, event, node):462 463 name = get_required_attribute(node, 'NAME')464 type = get_required_attribute(node, 'TYPE')465 array_size = get_attribute(node, 'ARRAYSIZE')466 class_origin = get_attribute(node, 'CLASSORIGIN')467 propagated = get_attribute(node, 'PROPAGATED')468 qualifiers = []469 value = None470 (next_event, next_node) = parser.next()471 if is_start(next_event, next_node, 'QUALIFIER'):472 qualifiers.append(parse_qualifier(parser, next_event, next_node))473 while 1:474 (next_event, next_node) = parser.next()475 if is_start(next_event, next_node, 'VALUE.ARRAY'):476 break477 if is_start(next_event, next_node, 'QUALIFIER'):478 qualifiers.append(479 parse_qualifier(parser, next_event, next_node))480 else:481 raise ParseError('Expecting QUALIFIER')482 if is_start(next_event, next_node, 'VALUE.ARRAY'):483 value = parse_value_array(parser, next_event, next_node)484 (next_event, next_node) = parser.next()485 if not is_end(next_event, next_node, 'PROPERTY.ARRAY'):486 raise ParseError('Expecting end PROPERTY.ARRAY')487 return CIMProperty(name,488 tocimobj(type, value),489 type = type,490 class_origin = class_origin,491 propagated = propagated,492 is_array = True,493 qualifiers = dict([(x.name, x) for x in qualifiers]))494 495# <!ELEMENT PROPERTY.REFERENCE (QUALIFIER*, (VALUE.REFERENCE)?)>496# <!ATTLIST PROPERTY.REFERENCE497# %CIMName; 498# %ReferenceClass; 499# %ClassOrigin; 500# %Propagated; 501# >502def parse_property_reference(parser, event, node):503 name = get_required_attribute(node, 'NAME')504 class_origin = get_attribute(node, 'CLASSORIGIN')505 propagated = get_attribute(node, 'PROPAGATED')506 qualifiers = []507 value = None508 (next_event, next_node) = parser.next()509 if is_start(next_event, next_node, 'QUALIFIER'):510 qualifiers.append(parse_qualifier(parser, next_event, next_node))511 while 1:512 (next_event, next_node) = parser.next()513 if is_start(next_event, next_node, 'VALUE.REFERENCE'):514 break515 if is_start(next_event, next_node, 'QUALIFIER'):516 qualifiers.append(517 parse_qualifier(parser, next_event, next_node))518 else:519 raise ParseError('Expecting QUALIFIER')520 if is_start(next_event, next_node, 'VALUE.REFERENCE'):521 value = parse_value_reference(parser, next_event, next_node)522 (next_event, next_node) = parser.next()523 if not is_end(next_event, next_node, 'PROPERTY.REFERENCE'):524 raise ParseError('Expecting end PROPERTY.REFERENCE')525 return CIMProperty(name,526 value,527 class_origin = class_origin,528 propagated = propagated,529 type = 'reference',530 qualifiers = dict([(x.name, x) for x in qualifiers]))531# <!ELEMENT METHOD (QUALIFIER*, (PARAMETER | PARAMETER.REFERENCE | PARAMETER.ARRAY | PARAMETER.REFARRAY)*)>532# <!ATTLIST METHOD 533# %CIMName;534# %CIMType; #IMPLIED...
thinkbat.py
Source:thinkbat.py
1#!/usr/bin/env python32import re3import click4class ThinkBat:5 def __init__(self, bat_num=0):6 self._path = "/sys/class/power_supply/BAT" + str(bat_num)7 self._stats = {}8 self.read_stats()9 def _thresh_type_str(self, is_start):10 return "start" if is_start else "end"11 def _thresh_key(self, is_start):12 return self._thresh_type_str(is_start) + "_threshold"13 def _get_thresh_path(self, is_start):14 return self._path + "/charge_control_" + self._thresh_type_str(is_start) + "_threshold"15 def _format_line(self, line):16 return re.sub("\ |\n", "", line).lower()17 def read_stats(self):18 with open(self._path + "/uevent", "r") as f:19 for line in f.readlines():20 line = self._format_line(line)21 key, val = tuple(line.split("="))22 if val.isnumeric():23 val = int(val)24 key = key.replace("power_supply_", "")25 self._stats[key] = val26 self._stats["capacity"] = 100 * self._stats["energy_full"] / self._stats["energy_full_design"]27 self._stats["percentage"] = 100 * self._stats["energy_now"] / self._stats["energy_full"]28 with open(self._path + "/voltage_now", "r") as f:29 self._stats["voltage"] = int(self._format_line(f.readline())) / 100000030 def read_thresholds(is_start):31 with open(self._get_thresh_path(is_start), "r") as f:32 self._stats[self._thresh_key(is_start)] = int(self._format_line(f.readline()))33 read_thresholds(is_start=True)34 read_thresholds(is_start=False)35 def set_charge_thresholds(self, start, end):36 def in_range(thresh):37 return thresh > 1 and thresh <= 10038 assert start < end and in_range(start) and in_range(end), "Invalid threshold!"39 40 def write_threshold(value, is_start):41 try:42 with open(self._get_thresh_path(is_start), "w") as f:43 f.write(str(value))44 self._stats[self._thresh_key(is_start)] = value45 return True46 except PermissionError:47 print("Changing thresholds requires sudo!")48 except Exception as e:49 print("Failed to change thresholds: " + str(e))50 return False51 if write_threshold(start, True) and write_threshold(end, False):52 print("Successfully changed charging thresholds.")53 def man_info(self):54 return "BAT Type: \n" + \55 "\tManufacturer: " + self._stats["manufacturer"] + "\n" + \56 "\tModel: " + self._stats["model_name"] + "\n" + \57 "\tSerial: " + str(self._stats["serial_number"]) + "\n" + \58 "\tTechnology: " + self._stats["technology"]59 def status_info(self):60 return "BAT Status:\n" + \61 "\tPercentage: " + str(round(self._stats["percentage"], 3)) + "%\n" \62 "\tState: -" + self._stats["status"] + "-\n" + \63 "\tCapacity: " + str(round(self._stats["capacity"], 3)) + "%\n" + \64 "\tCycle Count: #" + str(self._stats["cycle_count"]) + "\n" + \65 "\tVoltage: " + str(self._stats["voltage"]) + " V\n" + \66 "\tStart Thresh: " + str(self._stats["start_threshold"]) + "%\n" + \67 "\tStop Thresh: " + str(self._stats["end_threshold"]) + "%"68@click.command()69@click.option("-t", "--thresholds", nargs=2, type=(click.INT, click.INT), help="Set start and stop thresholds")70def prompt(thresholds):71 bat = ThinkBat()72 if thresholds:73 bat.set_charge_thresholds(thresholds[0], thresholds[1])74 else:75 print(bat.man_info())76 print(bat.status_info())77if __name__ == "__main__":...
render_calendar.py
Source:render_calendar.py
1# 13.6 RENDER A CALENDAR2# Write a program that takes a set of events, and determines the maximum number3# of events that take place concurrently.4from typing import List5import collections6Event = collections.namedtuple('Event' , ('start' , 'finish'))7class Solution:8 # time complexity sorting give n(Log(N)) time and iterating through9 # E give O(N) reduces to N(log(N))10 # Space complexity we use an addiitonal DS the size of n so O(N)11 def find_concurrent_events(self,A:List[Event])-> int:12 # Endpoint is a tuple (start_time, 0) or (end_time, 1) so that if times13 # are equal start_time comes first14 Endpoint = collections.namedtuple('Endpoint',('time','is_start'))15 E =[]16 # build array of all endpoints17 for event in A:18 E.append(Endpoint(event.start,True))19 E.append(Endpoint(event.finish,False))20 # first sort on time then if its start/end21 E.sort(key=lambda e: (e.time, not e.is_start))22 # E sorted by time first then start> finish second23 # [Endpoint(time=1, is_start=True), Endpoint(time=2, is_start=True),24 # Endpoint(time=4, is_start=True), Endpoint(time=5, is_start=False),25 # Endpoint(time=5, is_start=False), Endpoint(time=6, is_start=True),26 # Endpoint(time=7, is_start=False), Endpoint(time=8, is_start=True),27 # Endpoint(time=9, is_start=True), Endpoint(time=9, is_start=False),28 # Endpoint(time=10, is_start=False), Endpoint(time=11, is_start=True),29 # Endpoint(time=12, is_start=True), Endpoint(time=13, is_start=False),30 # Endpoint(time=14, is_start=True), Endpoint(time=15, is_start=False),31 # Endpoint(time=15, is_start=False), Endpoint(time=17, is_start=False)]32 # track the number of simultaneous events33 max_events, num_events = 0,034 # now the data is structured with time then starts first add 1 to35 # max_events if its a start and - if its a finish36 for e in E:37 if e.is_start:38 num_events += 139 max_events = max(max_events,num_events)40 else:41 num_events -= 142 return max_events43if __name__ =="__main__":44 mySolution = Solution()45 event1 = Event(1,5)46 event2 = Event(6,10)47 event3 = Event(11,13)48 event4 = Event(14,15)49 event5 = Event(2,7)50 event6 = Event(8,9)51 event7 = Event(12,15)52 event8 = Event(4,5)53 event9 = Event(9,17)54 A = [event1,event2,event3,event4,event5,event6,event7,event8,event9]...
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!!