Best Python code snippet using pandera_python
var.py
Source:var.py
1import random2from collections import OrderedDict3from datetime import datetime4CFG_STAT_VARS = [5 {6 "name": "pid",7 "type": "uint32",8 "value": {9 "value_min": 2000,10 "value_max": 4000,11 "value_random": True12 }13 },14 {15 "name": "uptime",16 "type": "uptime",17 },18 {19 "name": "time",20 "type": "time",21 },22 {23 "name": "version",24 "type": "string",25 "value": "1.4.25"26 },27 {28 "name": "libevent",29 "type": "string",30 "value": "2.0.22-stable"31 },32 {33 "name": "pointer_size",34 "type": "uint32",35 "value": 6436 },37 {38 "name": "rusage_user",39 "type": "float",40 "value": 0.5541 },42 {43 "name": "rusage_system",44 "type": "float",45 "value": 0.25346 },47 {48 "name": "curr_items",49 "type": "uint32",50 "value": {51 "value_min": 200,52 "value_max": 400,53 "value_random": True54 }55 },56 {57 "name": "total_items",58 "type": "uint32",59 "value": {60 "value_min": 350,61 "value_max": 500,62 "value_random": True63 }64 },65 {66 "name": "bytes",67 "type": "uint64",68 "value": {69 "value_min": 3000,70 "value_max": 50000,71 "value_random": True72 }73 },74 {75 "name": "curr_connections",76 "type": "uint32",77 "value": {78 "value_min": 350,79 "value_max": 500,80 "value_random": True81 }82 },83 {84 "name": "total_connections",85 "type": "uint32",86 "value": {87 "value_min": 350,88 "value_max": 500,89 "value_random": True90 }91 },92 # {93 # "name": "rejected_connections",94 # "type": "uint64",95 # "value": {96 # "value_min": 30,97 # "value_max": 50,98 # "value_random": True99 # }100 # },101 {102 "name": "connection_structures",103 "type": "uint32",104 "value": {105 "value_min": 350,106 "value_max": 500,107 "value_random": True108 }109 },110 {111 "name": "reserved_fds",112 "type": "uint32",113 "value": {114 "value_min": 350,115 "value_max": 500,116 "value_random": True117 }118 },119 {120 "name": "cmd_get",121 "type": "uint64",122 "value": {123 "value_min": 350,124 "value_max": 500,125 "value_random": True126 }127 },128 {129 "name": "cmd_set",130 "type": "uint64",131 "value": {132 "value_min": 350,133 "value_max": 500,134 "value_random": True135 }136 },137 {138 "name": "cmd_flush",139 "type": "uint64",140 "value": {141 "value_min": 350,142 "value_max": 500,143 "value_random": True144 }145 },146 {147 "name": "cmd_touch",148 "type": "uint64",149 "value": {150 "value_min": 350,151 "value_max": 500,152 "value_random": True153 }154 },155 {156 "name": "get_hits",157 "type": "uint64",158 "value": {159 "value_min": 350,160 "value_max": 500,161 "value_random": True162 }163 },164 {165 "name": "get_misses",166 "type": "uint64",167 "value": {168 "value_min": 350,169 "value_max": 500,170 "value_random": True171 }172 },173 {174 "name": "delete_misses",175 "type": "uint64",176 "value": {177 "value_min": 350,178 "value_max": 500,179 "value_random": True180 }181 },182 {183 "name": "delete_hits",184 "type": "uint64",185 "value": {186 "value_min": 350,187 "value_max": 500,188 "value_random": True189 }190 },191 {192 "name": "incr_misses",193 "type": "uint64",194 "value": {195 "value_min": 350,196 "value_max": 500,197 "value_random": True198 }199 },200 {201 "name": "incr_hits",202 "type": "uint64",203 "value": {204 "value_min": 350,205 "value_max": 500,206 "value_random": True207 }208 },209 {210 "name": "decr_misses",211 "type": "uint64",212 "value": {213 "value_min": 350,214 "value_max": 500,215 "value_random": True216 }217 },218 {219 "name": "decr_hits",220 "type": "uint64",221 "value": {222 "value_min": 350,223 "value_max": 500,224 "value_random": True225 }226 },227 {228 "name": "cas_misses",229 "type": "uint64",230 "value": {231 "value_min": 350,232 "value_max": 500,233 "value_random": True234 }235 },236 {237 "name": "cas_hits",238 "type": "uint64",239 "value": {240 "value_min": 350,241 "value_max": 500,242 "value_random": True243 }244 },245 {246 "name": "cas_badval",247 "type": "uint64",248 "value": {249 "value_min": 350,250 "value_max": 500,251 "value_random": True252 }253 },254 {255 "name": "touch_hits",256 "type": "uint64",257 "value": {258 "value_min": 350,259 "value_max": 500,260 "value_random": True261 }262 },263 {264 "name": "touch_misses",265 "type": "uint64",266 "value": {267 "value_min": 350,268 "value_max": 500,269 "value_random": True270 }271 },272 {273 "name": "auth_cmds",274 "type": "uint64",275 "value": {276 "value_min": 350,277 "value_max": 500,278 "value_random": True279 }280 },281 {282 "name": "auth_errors",283 "type": "uint64",284 "value": {285 "value_min": 350,286 "value_max": 500,287 "value_random": True288 }289 },290 {291 "name": "evictions",292 "type": "uint64",293 "value": {294 "value_min": 350,295 "value_max": 500,296 "value_random": True297 }298 },299 {300 "name": "reclaimed",301 "type": "uint64",302 "value": {303 "value_min": 350,304 "value_max": 500,305 "value_random": True306 }307 },308 {309 "name": "bytes_read",310 "type": "uint64",311 "value": {312 "value_min": 350,313 "value_max": 500,314 "value_random": True315 }316 },317 {318 "name": "bytes_written",319 "type": "uint64",320 "value": {321 "value_min": 350,322 "value_max": 500,323 "value_random": True324 }325 },326 {327 "name": "limit_maxbytes",328 "type": "uint32",329 "value": {330 "value_min": 350,331 "value_max": 500,332 "value_random": True333 }334 },335 {336 "name": "accepting_conns",337 "type": "bool",338 "value": True339 },340 {341 "name": "listen_disabled_num",342 "type": "uint64",343 "value": {344 "value_min": 350,345 "value_max": 500,346 "value_random": True347 }348 },349 {350 "name": "time_in_listen_disabled_us",351 "type": "uint64",352 "value": {353 "value_min": 350,354 "value_max": 500,355 "value_random": True356 }357 },358 {359 "name": "threads",360 "type": "uint32",361 "value": {362 "value_min": 350,363 "value_max": 500,364 "value_random": True365 }366 },367 {368 "name": "conn_yields",369 "type": "uint64",370 "value": {371 "value_min": 350,372 "value_max": 500,373 "value_random": True374 }375 },376 {377 "name": "hash_power_level",378 "type": "uint32",379 "value": {380 "value_min": 350,381 "value_max": 500,382 "value_random": True383 }384 },385 {386 "name": "hash_bytes",387 "type": "uint64",388 "value": {389 "value_min": 350,390 "value_max": 500,391 "value_random": True392 }393 },394 {395 "name": "hash_is_expanding",396 "type": "bool",397 "value": True398 },399 {400 "name": "malloc_fails",401 "type": "uint64",402 "value": {403 "value_min": 0,404 "value_max": 10,405 "value_random": True406 }407 },408 {409 "name": "expired_unfetched",410 "type": "uint64",411 "value": {412 "value_min": 350,413 "value_max": 500,414 "value_random": True415 }416 },417 {418 "name": "evicted_unfetched",419 "type": "uint64",420 "value": {421 "value_min": 350,422 "value_max": 500,423 "value_random": True424 }425 },426 # {427 # "name": "slab_reassign_running",428 # "type": "bool",429 # "value": True430 # },431 # {432 # "name": "slabs_moved",433 # "type": "uint64",434 # "value": {435 # "value_min": 350,436 # "value_max": 500,437 # "value_random": True438 # }439 # },440 {441 "name": "crawler_reclaimed",442 "type": "uint64",443 "value": {444 "value_min": 350,445 "value_max": 500,446 "value_random": True447 }448 },449 {450 "name": "crawler_items_checked",451 "type": "uint64",452 "value": {453 "value_min": 350,454 "value_max": 500,455 "value_random": True456 }457 },458 {459 "name": "lrutail_reflocked",460 "type": "uint64",461 "value": {462 "value_min": 350,463 "value_max": 500,464 "value_random": True465 }466 },467 # Values documented in protocol readme but not present in server response468 # {469 # "name": "moves_to_cold",470 # "type": "uint64",471 # "value": {472 # "value_min": 350,473 # "value_max": 500,474 # "value_random": True475 # }476 # },477 # {478 # "name": "moves_to_warm",479 # "type": "uint64",480 # "value": {481 # "value_min": 350,482 # "value_max": 500,483 # "value_random": True484 # }485 # },486 # {487 # "name": "moves_within_lru",488 # "type": "uint64",489 # "value": {490 # "value_min": 350,491 # "value_max": 500,492 # "value_random": True493 # }494 # },495 # {496 # "name": "direct_reclaims",497 # "type": "uint64",498 # "value": {499 # "value_min": 350,500 # "value_max": 500,501 # "value_random": True502 # }503 # },504 # {505 # "name": "lru_crawler_starts",506 # "type": "uint64",507 # "value": {508 # "value_min": 350,509 # "value_max": 500,510 # "value_random": True511 # }512 # },513 # {514 # "name": "lru_maintainer_juggles",515 # "type": "uint64",516 # "value": {517 # "value_min": 350,518 # "value_max": 500,519 # "value_random": True520 # }521 # },522 # {523 # "name": "slab_global_page_pool",524 # "type": "uint32",525 # "value": {526 # "value_min": 350,527 # "value_max": 500,528 # "value_random": True529 # }530 # },531 # {532 # "name": "slab_reassign_rescues",533 # "type": "uint64",534 # "value": {535 # "value_min": 350,536 # "value_max": 500,537 # "value_random": True538 # }539 # },540 # {541 # "name": "slab_reassign_evictions_nomem",542 # "type": "uint64",543 # "value": {544 # "value_min": 350,545 # "value_max": 500,546 # "value_random": True547 # }548 # },549 # {550 # "name": "slab_reassign_inline_reclaim",551 # "type": "uint64",552 # "value": {553 # "value_min": 350,554 # "value_max": 500,555 # "value_random": True556 # }557 # },558 # {559 # "name": "slab_reassign_busy_items",560 # "type": "uint64",561 # "value": {562 # "value_min": 350,563 # "value_max": 500,564 # "value_random": True565 # }566 # }567]568class VarHandler(object):569 def __init__(self):570 self.values = OrderedDict()571 def _get_var_class(self, name):572 if name == "bool":573 return Bool574 if name == "float":575 return Float576 if name == "string":577 return String578 if name == "time":579 return Time580 if name == "uint32":581 return UInt32582 if name == "uint64":583 return UInt64584 if name == "uptime":585 return Uptime586 raise ValueError("Unable to find class to handle %s type" % name)587 def load(self, vars):588 for var in vars:589 var_cls = self._get_var_class(var.get("type"))590 value = var.get("value", None)591 var_params = {}592 if isinstance(value, dict):593 var_params = value594 elif value is not None:595 var_params = {"value": value}596 self.values[var.get("name")] = var_cls(**var_params)597class BaseVar(object):598 pass599class Bool(BaseVar):600 def __init__(self, value=False):601 if not isinstance(value, bool):602 raise ValueError("Value must be boolean")603 self.value = value604 def __str__(self):605 if self.value:606 return "1"607 return "0"608class Float(BaseVar):609 def __init__(self, value=0.0):610 self._value = 0.0611 self.value = value612 def __str__(self):613 return "%.6f" % self.value614 def _get_value(self):615 return self._value616 def _set_value(self, value):617 self._value = value618 value = property(_get_value, _set_value)619class String(BaseVar):620 def __init__(self, value=""):621 if not isinstance(value, str):622 raise ValueError("Value must be string")623 self.value = value624 def __str__(self):625 return self.value626class Time(BaseVar):627 @property628 def value(self):629 return int(datetime.now().timestamp())630 def __str__(self):631 return str(self.value)632class UIntBase(BaseVar):633 default_value_max = 0634 def __init__(self, value=0, value_min=0, value_max=None, value_random=False):635 if not isinstance(value, int):636 raise ValueError("Value must be integer")637 self._value = 0638 self.value_min = value_min639 self.value_max = self.default_value_max640 if value_max is not None:641 self.value_max = value_max642 if value_random:643 self.value = random.randint(self.value_min, self.value_max)644 else:645 self.value = value646 def __str__(self):647 return str(self.value)648 def _value_get(self):649 return self._value650 def _value_set(self, value):651 if value < 0 or value > 2**32-1:652 raise ValueError("Value is '%d' but allowed range ist >0 and < 2°32-1", value)653 self._value = value654 value = property(_value_get, _value_set)655class UInt32(UIntBase):656 default_value_max = 2**32-1657class UInt64(UIntBase):658 default_value_max = 2**64-1659class Uptime(BaseVar):660 def __init__(self):661 self.start_time = datetime.now()662 def __str__(self):663 return str(self.value)664 @property665 def value(self):666 delta = datetime.now() - self.start_time...
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!!