How to use func_name method in Slash

Best Python code snippet using slash

item_flags.py

Source:item_flags.py Github

copy

Full Screen

1# PythonWars copyright © 2020, 2021 by Paul Penner. All rights reserved.2# In order to use this codebase you must comply with all licenses.3#4# Original Diku Mud copyright © 1990, 1991 by Sebastian Hammer,5# Michael Seifert, Hans Henrik Stærfeldt, Tom Madsen, and Katja Nyboe.6#7# Merc Diku Mud improvements copyright © 1992, 1993 by Michael8# Chastain, Michael Quan, and Mitchell Tse.9#10# ROM 2.4 is copyright 1993-1998 Russ Taylor. ROM has been brought to11# you by the ROM consortium: Russ Taylor (rtaylor@hypercube.org),12# Gabrielle Taylor (gtaylor@hypercube.org), and Brian Moore (zump@rom.org).13#14# Ported to Python by Davion of MudBytes.net using Miniboa15# (https://code.google.com/p/miniboa/).16#17# In order to use any part of this Merc Diku Mud, you must comply with18# both the original Diku license in 'license.doc' as well the Merc19# license in 'license.txt'. In particular, you may not remove either of20# these copyright notices.21#22# Much time and thought has gone into this software, and you are23# benefiting. We hope that you share your changes too. What goes24# around, comes around.25import json26import sys27class ItemFlags:28 def __init__(self, et_data: set = None, iaf_data: set = None, ir_data: set = None, wa_data: set = None):29 self._equips_to = set({})30 if et_data:31 self._equips_to |= set(et_data)32 self._item_attributes = set({})33 if iaf_data:34 self._item_attributes |= set(iaf_data)35 self._item_restrictions = set({})36 if ir_data:37 self._item_restrictions |= set(ir_data)38 self._weapon_attributes = set({})39 if wa_data:40 self._weapon_attributes |= set(wa_data)41 @property42 def head(self):43 func_name = sys._getframe().f_code.co_name44 return func_name if func_name in self._equips_to else False45 @head.setter46 def head(self, is_equippable):47 func_name = sys._getframe().f_code.co_name48 if is_equippable:49 self._equips_to |= {func_name}50 else:51 self._equips_to -= {func_name}52 @property53 def legs(self):54 func_name = sys._getframe().f_code.co_name55 return func_name if func_name in self._equips_to else False56 @legs.setter57 def legs(self, is_equippable):58 func_name = sys._getframe().f_code.co_name59 if is_equippable:60 self._equips_to |= {func_name}61 else:62 self._equips_to -= {func_name}63 @property64 def feet(self):65 func_name = sys._getframe().f_code.co_name66 return func_name if func_name in self._equips_to else False67 @feet.setter68 def feet(self, is_equippable):69 func_name = sys._getframe().f_code.co_name70 if is_equippable:71 self._equips_to |= {func_name}72 else:73 self._equips_to -= {func_name}74 @property75 def hands(self):76 func_name = sys._getframe().f_code.co_name77 return func_name if func_name in self._equips_to else False78 @hands.setter79 def hands(self, is_equippable):80 func_name = sys._getframe().f_code.co_name81 if is_equippable:82 self._equips_to |= {func_name}83 else:84 self._equips_to -= {func_name}85 @property86 def face(self):87 func_name = sys._getframe().f_code.co_name88 return func_name if func_name in self._equips_to else False89 @face.setter90 def face(self, is_equippable):91 func_name = sys._getframe().f_code.co_name92 if is_equippable:93 self._equips_to |= {func_name}94 else:95 self._equips_to -= {func_name}96 @property97 def left_finger(self):98 func_name = sys._getframe().f_code.co_name99 return func_name if func_name in self._equips_to else False100 @left_finger.setter101 def left_finger(self, is_equippable):102 func_name = sys._getframe().f_code.co_name103 if is_equippable:104 self._equips_to |= {func_name}105 else:106 self._equips_to -= {func_name}107 @property108 def right_finger(self):109 func_name = sys._getframe().f_code.co_name110 return func_name if func_name in self._equips_to else False111 @right_finger.setter112 def right_finger(self, is_equippable):113 func_name = sys._getframe().f_code.co_name114 if is_equippable:115 self._equips_to |= {func_name}116 else:117 self._equips_to -= {func_name}118 @property119 def right_wrist(self):120 func_name = sys._getframe().f_code.co_name121 return func_name if func_name in self._equips_to else False122 @right_wrist.setter123 def right_wrist(self, is_equippable):124 func_name = sys._getframe().f_code.co_name125 if is_equippable:126 self._equips_to |= {func_name}127 else:128 self._equips_to -= {func_name}129 @property130 def left_wrist(self):131 func_name = sys._getframe().f_code.co_name132 return func_name if func_name in self._equips_to else False133 @left_wrist.setter134 def left_wrist(self, is_equippable):135 func_name = sys._getframe().f_code.co_name136 if is_equippable:137 self._equips_to |= {func_name}138 else:139 self._equips_to -= {func_name}140 @property141 def waist(self):142 func_name = sys._getframe().f_code.co_name143 return func_name if func_name in self._equips_to else False144 @waist.setter145 def waist(self, is_equippable):146 func_name = sys._getframe().f_code.co_name147 if is_equippable:148 self._equips_to |= {func_name}149 else:150 self._equips_to -= {func_name}151 @property152 def about_body(self):153 func_name = sys._getframe().f_code.co_name154 return func_name if func_name in self._equips_to else False155 @about_body.setter156 def about_body(self, is_equippable):157 func_name = sys._getframe().f_code.co_name158 if is_equippable:159 self._equips_to |= {func_name}160 else:161 self._equips_to -= {func_name}162 @property163 def light(self):164 func_name = sys._getframe().f_code.co_name165 return func_name if func_name in self._equips_to else False166 @light.setter167 def light(self, is_equippable):168 func_name = sys._getframe().f_code.co_name169 if is_equippable:170 self._equips_to |= {func_name}171 else:172 self._equips_to -= {func_name}173 @property174 def body(self):175 func_name = sys._getframe().f_code.co_name176 return func_name if func_name in self._equips_to else False177 @body.setter178 def body(self, is_equippable):179 func_name = sys._getframe().f_code.co_name180 if is_equippable:181 self._equips_to |= {func_name}182 else:183 self._equips_to -= {func_name}184 @property185 def neck_one(self):186 func_name = sys._getframe().f_code.co_name187 return func_name if func_name in self._equips_to else False188 @neck_one.setter189 def neck_one(self, is_equippable):190 func_name = sys._getframe().f_code.co_name191 if is_equippable:192 self._equips_to |= {func_name}193 else:194 self._equips_to -= {func_name}195 @property196 def neck_two(self):197 func_name = sys._getframe().f_code.co_name198 return func_name if func_name in self._equips_to else False199 @neck_two.setter200 def neck_two(self, is_equippable):201 func_name = sys._getframe().f_code.co_name202 if is_equippable:203 self._equips_to |= {func_name}204 else:205 self._equips_to -= {func_name}206 @property207 def arms(self):208 func_name = sys._getframe().f_code.co_name209 return func_name if func_name in self._equips_to else False210 @arms.setter211 def arms(self, is_equippable):212 func_name = sys._getframe().f_code.co_name213 if is_equippable:214 self._equips_to |= {func_name}215 else:216 self._equips_to -= {func_name}217 @property218 def off_hand(self):219 func_name = sys._getframe().f_code.co_name220 return func_name if func_name in self._equips_to else False221 @off_hand.setter222 def off_hand(self, is_equippable):223 func_name = sys._getframe().f_code.co_name224 if is_equippable:225 self._equips_to |= {func_name}226 else:227 self._equips_to -= {func_name}228 @property229 def right_hand(self):230 func_name = sys._getframe().f_code.co_name231 return func_name if func_name in self._equips_to else False232 @right_hand.setter233 def right_hand(self, is_equippable):234 func_name = sys._getframe().f_code.co_name235 if is_equippable:236 self._equips_to |= {func_name}237 else:238 self._equips_to -= {func_name}239 @property240 def left_hand(self):241 func_name = sys._getframe().f_code.co_name242 return func_name if func_name in self._equips_to else False243 @left_hand.setter244 def left_hand(self, is_equippable):245 func_name = sys._getframe().f_code.co_name246 if is_equippable:247 self._equips_to |= {func_name}248 else:249 self._equips_to -= {func_name}250 # Item Attribute Flags251 @property252 def magic(self):253 func_name = sys._getframe().f_code.co_name254 return func_name if func_name in self._item_attributes else False255 @magic.setter256 def magic(self, has_attr):257 func_name = sys._getframe().f_code.co_name258 if has_attr:259 self._item_attributes |= {func_name}260 else:261 self._item_attributes -= {func_name}262 @property263 def glow(self):264 func_name = sys._getframe().f_code.co_name265 return func_name if func_name in self._item_attributes else False266 @glow.setter267 def glow(self, has_attr):268 func_name = sys._getframe().f_code.co_name269 if has_attr:270 self._item_attributes |= {func_name}271 else:272 self._item_attributes -= {func_name}273 @property274 def hum(self):275 func_name = sys._getframe().f_code.co_name276 return func_name if func_name in self._item_attributes else False277 @hum.setter278 def hum(self, has_attr):279 func_name = sys._getframe().f_code.co_name280 if has_attr:281 self._item_attributes |= {func_name}282 else:283 self._item_attributes -= {func_name}284 @property285 def dark(self):286 func_name = sys._getframe().f_code.co_name287 return func_name if func_name in self._item_attributes else False288 @dark.setter289 def dark(self, has_attr):290 func_name = sys._getframe().f_code.co_name291 if has_attr:292 self._item_attributes |= {func_name}293 else:294 self._item_attributes -= {func_name}295 @property296 def silver(self):297 func_name = sys._getframe().f_code.co_name298 return func_name if func_name in self._item_attributes else False299 @silver.setter300 def silver(self, has_attr):301 func_name = sys._getframe().f_code.co_name302 if has_attr:303 self._item_attributes |= {func_name}304 else:305 self._item_attributes -= {func_name}306 @property307 def wolfweapon(self):308 func_name = sys._getframe().f_code.co_name309 return func_name if func_name in self._item_attributes else False310 @wolfweapon.setter311 def wolfweapon(self, has_attr):312 func_name = sys._getframe().f_code.co_name313 if has_attr:314 self._item_attributes |= {func_name}315 else:316 self._item_attributes -= {func_name}317 @property318 def demonic(self):319 func_name = sys._getframe().f_code.co_name320 return func_name if func_name in self._item_attributes else False321 @demonic.setter322 def demonic(self, has_attr):323 func_name = sys._getframe().f_code.co_name324 if has_attr:325 self._item_attributes |= {func_name}326 else:327 self._item_attributes -= {func_name}328 @property329 def highlander(self):330 func_name = sys._getframe().f_code.co_name331 return func_name if func_name in self._item_attributes else False332 @highlander.setter333 def highlander(self, has_attr):334 func_name = sys._getframe().f_code.co_name335 if has_attr:336 self._item_attributes |= {func_name}337 else:338 self._item_attributes -= {func_name}339 @property340 def relic(self):341 func_name = sys._getframe().f_code.co_name342 return func_name if func_name in self._item_attributes else False343 @relic.setter344 def relic(self, has_attr):345 func_name = sys._getframe().f_code.co_name346 if has_attr:347 self._item_attributes |= {func_name}348 else:349 self._item_attributes -= {func_name}350 @property351 def enchanted(self):352 func_name = sys._getframe().f_code.co_name353 return func_name if func_name in self._item_attributes else False354 @enchanted.setter355 def enchanted(self, has_attr):356 func_name = sys._getframe().f_code.co_name357 if has_attr:358 self._item_attributes |= {func_name}359 else:360 self._item_attributes -= {func_name}361 @property362 def artifact(self):363 func_name = sys._getframe().f_code.co_name364 return func_name if func_name in self._item_attributes else False365 @artifact.setter366 def artifact(self, has_attr):367 func_name = sys._getframe().f_code.co_name368 if has_attr:369 self._item_attributes |= {func_name}370 else:371 self._item_attributes -= {func_name}372 @property373 def vanish(self):374 func_name = sys._getframe().f_code.co_name375 return func_name if func_name in self._item_attributes else False376 @vanish.setter377 def vanish(self, has_attr):378 func_name = sys._getframe().f_code.co_name379 if has_attr:380 self._item_attributes |= {func_name}381 else:382 self._item_attributes -= {func_name}383 @property384 def loyal(self):385 func_name = sys._getframe().f_code.co_name386 return func_name if func_name in self._item_attributes else False387 @loyal.setter388 def loyal(self, has_attr):389 func_name = sys._getframe().f_code.co_name390 if has_attr:391 self._item_attributes |= {func_name}392 else:393 self._item_attributes -= {func_name}394 @property395 def shadowplane(self):396 func_name = sys._getframe().f_code.co_name397 return func_name if func_name in self._item_attributes else False398 @shadowplane.setter399 def shadowplane(self, has_attr):400 func_name = sys._getframe().f_code.co_name401 if has_attr:402 self._item_attributes |= {func_name}403 else:404 self._item_attributes -= {func_name}405 @property406 def spellproof(self):407 func_name = sys._getframe().f_code.co_name408 return func_name if func_name in self._item_attributes else False409 @spellproof.setter410 def spellproof(self, has_attr):411 func_name = sys._getframe().f_code.co_name412 if has_attr:413 self._item_attributes |= {func_name}414 else:415 self._item_attributes -= {func_name}416 @property417 def lock(self):418 func_name = sys._getframe().f_code.co_name419 return func_name if func_name in self._item_attributes else False420 @lock.setter421 def lock(self, has_attr):422 func_name = sys._getframe().f_code.co_name423 if has_attr:424 self._item_attributes |= {func_name}425 else:426 self._item_attributes -= {func_name}427 @property428 def evil(self):429 func_name = sys._getframe().f_code.co_name430 return func_name if func_name in self._item_attributes else False431 @evil.setter432 def evil(self, has_attr):433 func_name = sys._getframe().f_code.co_name434 if has_attr:435 self._item_attributes |= {func_name}436 else:437 self._item_attributes -= {func_name}438 @property439 def invis(self):440 func_name = sys._getframe().f_code.co_name441 return func_name if func_name in self._item_attributes else False442 @invis.setter443 def invis(self, has_attr):444 func_name = sys._getframe().f_code.co_name445 if has_attr:446 self._item_attributes |= {func_name}447 else:448 self._item_attributes -= {func_name}449 @property450 def bless(self):451 func_name = sys._getframe().f_code.co_name452 return func_name if func_name in self._item_attributes else False453 @bless.setter454 def bless(self, has_attr):455 func_name = sys._getframe().f_code.co_name456 if has_attr:457 self._item_attributes |= {func_name}458 else:459 self._item_attributes -= {func_name}460 @property461 def had_timer(self):462 func_name = sys._getframe().f_code.co_name463 return func_name if func_name in self._item_attributes else False464 @had_timer.setter465 def had_timer(self, has_attr):466 func_name = sys._getframe().f_code.co_name467 if has_attr:468 self._item_attributes |= {func_name}469 else:470 self._item_attributes -= {func_name}471 @property472 def take(self):473 func_name = sys._getframe().f_code.co_name474 return func_name if func_name in self._item_attributes else False475 @take.setter476 def take(self, has_attr):477 func_name = sys._getframe().f_code.co_name478 if has_attr:479 self._item_attributes |= {func_name}480 else:481 self._item_attributes -= {func_name}482 @property483 def shop_inventory(self):484 func_name = sys._getframe().f_code.co_name485 return func_name if func_name in self._item_attributes else False486 @shop_inventory.setter487 def shop_inventory(self, has_restr):488 func_name = sys._getframe().f_code.co_name489 if has_restr:490 self._item_attributes |= {func_name}491 else:492 self._item_attributes -= {func_name}493 # Item Restriction Flags494 @property495 def no_drop(self):496 func_name = sys._getframe().f_code.co_name497 return func_name if func_name in self._item_restrictions else False498 @no_drop.setter499 def no_drop(self, has_restr):500 func_name = sys._getframe().f_code.co_name501 if has_restr:502 self._item_restrictions |= {func_name}503 else:504 self._item_restrictions -= {func_name}505 @property506 def no_sac(self):507 func_name = sys._getframe().f_code.co_name508 return func_name if func_name in self._item_restrictions else False509 @no_sac.setter510 def no_sac(self, has_restr):511 func_name = sys._getframe().f_code.co_name512 if has_restr:513 self._item_restrictions |= {func_name}514 else:515 self._item_restrictions -= {func_name}516 @property517 def no_remove(self):518 func_name = sys._getframe().f_code.co_name519 return func_name if func_name in self._item_restrictions else False520 @no_remove.setter521 def no_remove(self, has_restr):522 func_name = sys._getframe().f_code.co_name523 if has_restr:524 self._item_restrictions |= {func_name}525 else:526 self._item_restrictions -= {func_name}527 @property528 def no_uncurse(self):529 func_name = sys._getframe().f_code.co_name530 return func_name if func_name in self._item_restrictions else False531 @no_uncurse.setter532 def no_uncurse(self, has_restr):533 func_name = sys._getframe().f_code.co_name534 if has_restr:535 self._item_restrictions |= {func_name}536 else:537 self._item_restrictions -= {func_name}538 @property539 def no_purge(self):540 func_name = sys._getframe().f_code.co_name541 return func_name if func_name in self._item_restrictions else False542 @no_purge.setter543 def no_purge(self, has_restr):544 func_name = sys._getframe().f_code.co_name545 if has_restr:546 self._item_restrictions |= {func_name}547 else:548 self._item_restrictions -= {func_name}549 @property550 def anti_good(self):551 func_name = sys._getframe().f_code.co_name552 return func_name if func_name in self._item_restrictions else False553 @anti_good.setter554 def anti_good(self, has_restr):555 func_name = sys._getframe().f_code.co_name556 if has_restr:557 self._item_restrictions |= {func_name}558 else:559 self._item_restrictions -= {func_name}560 @property561 def anti_evil(self):562 func_name = sys._getframe().f_code.co_name563 return func_name if func_name in self._item_restrictions else False564 @anti_evil.setter565 def anti_evil(self, has_restr):566 func_name = sys._getframe().f_code.co_name567 if has_restr:568 self._item_restrictions |= {func_name}569 else:570 self._item_restrictions -= {func_name}571 @property572 def anti_neutral(self):573 func_name = sys._getframe().f_code.co_name574 return func_name if func_name in self._item_restrictions else False575 @anti_neutral.setter576 def anti_neutral(self, has_restr):577 func_name = sys._getframe().f_code.co_name578 if has_restr:579 self._item_restrictions |= {func_name}580 else:581 self._item_restrictions -= {func_name}582 @property583 def no_locate(self):584 func_name = sys._getframe().f_code.co_name585 return func_name if func_name in self._item_restrictions else False586 @no_locate.setter587 def no_locate(self, has_restr):588 func_name = sys._getframe().f_code.co_name589 if has_restr:590 self._item_restrictions |= {func_name}591 else:592 self._item_restrictions -= {func_name}593 # Weapon Attributes594 @property595 def flaming(self):596 func_name = sys._getframe().f_code.co_name597 return func_name if func_name in self._weapon_attributes else False598 @flaming.setter599 def flaming(self, weap_attr):600 func_name = sys._getframe().f_code.co_name601 if weap_attr:602 self._weapon_attributes |= {func_name}603 else:604 self._weapon_attributes -= {func_name}605 @property606 def sharp(self):607 func_name = sys._getframe().f_code.co_name608 return func_name if func_name in self._weapon_attributes else False609 @sharp.setter610 def sharp(self, weap_attr):611 func_name = sys._getframe().f_code.co_name612 if weap_attr:613 self._weapon_attributes |= {func_name}614 else:615 self._weapon_attributes -= {func_name}616 @property617 def frost(self):618 func_name = sys._getframe().f_code.co_name619 return func_name if func_name in self._weapon_attributes else False620 @frost.setter621 def frost(self, weap_attr):622 func_name = sys._getframe().f_code.co_name623 if weap_attr:624 self._weapon_attributes |= {func_name}625 else:626 self._weapon_attributes -= {func_name}627 @property628 def vampiric(self):629 func_name = sys._getframe().f_code.co_name630 return func_name if func_name in self._weapon_attributes else False631 @vampiric.setter632 def vampiric(self, weap_attr):633 func_name = sys._getframe().f_code.co_name634 if weap_attr:635 self._weapon_attributes |= {func_name}636 else:637 self._weapon_attributes -= {func_name}638 @property639 def vorpal(self):640 func_name = sys._getframe().f_code.co_name641 return func_name if func_name in self._weapon_attributes else False642 @vorpal.setter643 def vorpal(self, weap_attr):644 func_name = sys._getframe().f_code.co_name645 if weap_attr:646 self._weapon_attributes |= {func_name}647 else:648 self._weapon_attributes -= {func_name}649 @property650 def shocking(self):651 func_name = sys._getframe().f_code.co_name652 return func_name if func_name in self._weapon_attributes else False653 @shocking.setter654 def shocking(self, weap_attr):655 func_name = sys._getframe().f_code.co_name656 if weap_attr:657 self._weapon_attributes |= {func_name}658 else:659 self._weapon_attributes -= {func_name}660 @property661 def poison(self):662 func_name = sys._getframe().f_code.co_name663 return func_name if func_name in self._weapon_attributes else False664 @poison.setter665 def poison(self, weap_attr):666 func_name = sys._getframe().f_code.co_name667 if weap_attr:668 self._weapon_attributes |= {func_name}669 else:670 self._weapon_attributes -= {func_name}671 # Serialization672 def to_json(self, outer_encoder=None):673 if outer_encoder is None:674 outer_encoder = json.JSONEncoder.default675 cls_name = "__class__/" + __name__ + "." + self.__class__.__name__676 return {677 cls_name: {678 "equips_to": outer_encoder(self._equips_to),679 "item_attributes": outer_encoder(self._item_attributes),680 "item_restrictions": outer_encoder(self._item_restrictions),681 "weapon_attributes": outer_encoder(self._weapon_attributes),682 }683 }684 @classmethod685 def from_json(cls, data, outer_decoder=None):686 if outer_decoder is None:687 outer_decoder = json.JSONDecoder.decode688 cls_name = "__class__/" + __name__ + "." + cls.__name__689 if cls_name in data:690 return cls(et_data=outer_decoder(data[cls_name]["equips_to"]),691 iaf_data=outer_decoder(data[cls_name]["item_attributes"]),692 ir_data=outer_decoder(data[cls_name]["item_restrictions"]),693 wa_data=outer_decoder(data[cls_name]["weapon_attributes"]))...

Full Screen

Full Screen

optimize.py

Source:optimize.py Github

copy

Full Screen

1import os2os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'3import matplotlib.pyplot as plt # noqa4import numpy as np # noqa5import tensorflow as tf # noqa6from bayes_opt import BayesianOptimization # noqa7from functools import partial # noqa8from hyperopt import hp, fmin, tpe, STATUS_OK, Trials # noqa9from mpl_toolkits.mplot3d import Axes3D # noqa10def cost_function(x, y, func_name='kaist', return_np=False):11 if func_name == 'kaist':12 z = -1 * tf.sin(x * x) * tf.cos(3 * y * y) * tf.exp(-(x * y) * (x * y)) - tf.exp(-(x + y) * (x + y))13 elif func_name == 'sphere':14 z = tf.square(x) + tf.square(y)15 elif func_name == 'rosenbrock':16 z = 100 * tf.square(y - tf.square(x)) + tf.square(1 - x)17 elif func_name == 'himmelblau':18 z = tf.square(tf.square(x) + y - 11) + tf.square(x + tf.square(y) - 7)19 else:20 raise ValueError()21 if return_np:22 return z.numpy()23 else:24 return z25def bayes_opt_objective(x, y, func_name='kaist', target=None):26 z = cost_function(x, y, func_name=func_name, return_np=True)27 if target is None:28 loss = z29 else:30 loss = np.abs(z - target)31 return -1 * loss32def hyperopt_objective(space):33 x = space['x']34 y = space['y']35 func_name = space['func_name']36 target = space['target']37 z = cost_function(x, y, func_name=func_name, return_np=True)38 if target is None:39 loss = z40 else:41 loss = np.abs(z - target)42 return {'loss': loss, 'status': STATUS_OK, 'history': [x, y, z, loss]}43def draw_cost_function(func_name='sphere'):44 plt.ion()45 fig = plt.figure(figsize=(3, 2), dpi=300)46 ax = fig.add_subplot(1, 1, 1, projection='3d')47 plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)48 params = {'legend.fontsize': 3,49 'legend.handlelength': 3}50 plt.rcParams.update(params)51 plt.axis('off')52 if func_name == 'himmelblau':53 x_val = np.arange(-5.0, 5.0, 0.005, dtype=np.float32)54 y_val = np.arange(-5.0, 5.0, 0.005, dtype=np.float32)55 else:56 x_val = np.arange(-1.5, 1.5, 0.005, dtype=np.float32)57 y_val = np.arange(-1.5, 1.5, 0.005, dtype=np.float32)58 x_val_mesh, y_val_mesh = np.meshgrid(x_val, y_val)59 x_val_mesh_flat = x_val_mesh.reshape([-1, 1])60 y_val_mesh_flat = y_val_mesh.reshape([-1, 1])61 z_val_mesh_flat = cost_function(x_val_mesh_flat, y_val_mesh_flat, func_name=func_name, return_np=True)62 z_val_mesh = z_val_mesh_flat.reshape(x_val_mesh.shape)63 ax.plot_surface(x_val_mesh, y_val_mesh, z_val_mesh, alpha=.4, cmap='jet')64 plt.draw()65 return ax66def draw_optimize_history(ax1, history, label):67 color_list = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red']68 iterations = len(history[0])69 x = list()70 y = list()71 z = list()72 for iteration in range(iterations - 1):73 scatter_list = list()74 text_list = list()75 for i in range(len(history)):76 x_pre = history[i][iteration][0]77 y_pre = history[i][iteration][1]78 z_pre = history[i][iteration][2]79 x_val = history[i][iteration + 1][0]80 y_val = history[i][iteration + 1][1]81 z_val = history[i][iteration + 1][2]82 x.append(x_val)83 y.append(y_val)84 z.append(z_val)85 loss = history[i][iteration + 1][3]86 # ax.plot([x_pre, x_val], [y_pre, y_val], [z_pre, z_val],87 # linewidth=0.5, color=color_list[i])88 scatter_list.append(ax1.scatter(x_val, y_val, z_val,89 s=1, depthshade=True, color=color_list[i]))90 text_list.append(ax1.text(x_val, y_val, z_val,91 ' x: {:.2f}, y: {:.2f}, z: {:.2f}, cost: {:.2f}'.format(92 x_val, y_val, z_val, loss93 ), fontsize=3))94 if iteration == 0:95 plt.legend(scatter_list, label)96 plt.pause(0.0001)97 if iteration != range(iterations - 1)[-1]:98 # for scatter in scatter_list:99 # scatter.remove()100 for text in text_list:101 text.remove()102 plt.pause(100)103def optimize_tf(x_0, y_0, optimizer, func_name='kaist', target=None, iterations=100):104 z_0 = cost_function(x_0, y_0, func_name=func_name, return_np=True)105 history = [[x_0, y_0, z_0, np.nan]]106 x = tf.Variable(x_0, dtype=tf.float32)107 y = tf.Variable(y_0, dtype=tf.float32)108 for iteration in range(iterations):109 with tf.GradientTape() as tape:110 z = cost_function(x, y, func_name=func_name, return_np=False)111 if target is None:112 loss = z113 else:114 loss = tf.abs(z - target)115 grad = tape.gradient(loss, [x, y])116 optimizer.apply_gradients(zip(grad, [x, y]))117 history.append([x.numpy(), y.numpy(), z.numpy(), loss.numpy()])118 return history119def opimize_hyperopt(x_0, y_0, func_name='kaist', target=None, iterations=100):120 space = dict()121 space['x'] = hp.uniform('x', -1.5, 1.5)122 space['y'] = hp.uniform('y', -1.5, 1.5)123 space['func_name'] = func_name124 space['target'] = target125 trials = Trials()126 fmin(hyperopt_objective, space=space, algo=tpe.suggest, max_evals=iterations, trials=trials)127 history = [[x_0, y_0, cost_function(x_0, y_0, func_name=func_name, return_np=True), np.nan]]128 for i in range(iterations):129 history.append(trials.results[i]['history'])130 return history131def optimize_bayes_opt(x_0, y_0, func_name='kaist', target=None, iterations=100):132 f = partial(bayes_opt_objective, func_name=func_name, target=target)133 pbounds = {'x': (-1.5, 1.5), 'y': (-1.5, 1.5)}134 optimizer = BayesianOptimization(135 f=f,136 pbounds=pbounds137 )138 optimizer.maximize(init_points=0, n_iter=iterations)139 history = [[x_0, y_0, cost_function(x_0, y_0, func_name=func_name, return_np=True), np.nan]]140 for i, res in enumerate(optimizer.res):141 params = res['params']142 cost = res['target']143 x = params['x']144 y = params['y']145 z = cost_function(x, y, func_name=func_name, return_np=True)146 history.append([x, y, z, cost])147 return history148def main():149 x_0 = 0.75150 y_0 = 1.0151 func_name = 'kaist'152 target = -1153 iterations = 100154 ax = draw_cost_function(func_name=func_name)155 sgd_history = optimize_tf(156 x_0, y_0, tf.keras.optimizers.SGD(lr=0.1), func_name=func_name, target=target, iterations=iterations157 )158 adam_history = optimize_tf(159 x_0, y_0, tf.keras.optimizers.Adam(lr=0.1), func_name=func_name, target=target, iterations=iterations160 )161 # bayes_opt_history = optimize_bayes_opt(x_0, y_0, func_name=func_name, target=target, iterations=iterations)162 hyperopt_history = opimize_hyperopt(x_0, y_0, func_name=func_name, target=target, iterations=iterations)163 draw_optimize_history(ax,164 [sgd_history,165 adam_history,166 # bayes_opt_history,167 hyperopt_history],168 ['SGD (lr=0.1)',169 'Adam (lr=0.1)',170 # 'Bayes-GP',171 'Bayes-TPE'])172if __name__ == '__main__':...

Full Screen

Full Screen

readme_generate.py

Source:readme_generate.py Github

copy

Full Screen

1print("### 1. read register_scheduler::function registerFunction")2func_names = {} # lua_func:cpp_func3src_file_name = "contract_register_function.cpp"4src_file = open(src_file_name, 'r')5for line in src_file.readlines():6 if line.find("registerFunction(") != -1:7 tokens = line.split("&")8 lua_func_name = tokens[0].split('"')[1]9 print(" " + lua_func_name)10 func_name = tokens[1]11 func_name = func_name[:-3]12 # print(" " + func_name)13 func_name = func_name.split("::")[1]14 func_names[lua_func_name] = func_name15 elif line.find("registerFunction<") != -1:16 pass17 else:18 continue19src_file.close()20print("\n### 2. write register_scheduler::register-function to README.md")21readme_file = open("README.md", "w+")22readme_file.write("## contract api\n")23api_count = 124flag = False25src_file = open(src_file_name, 'r')26for line in src_file.readlines():27 if flag or line.find("struct register_scheduler") != -1:28 flag = True29 else:30 continue31 for lua_func_name in func_names:32 cpp_func_name = func_names[lua_func_name]33 if line.find(cpp_func_name+"(") != -1 and line.find("registerFunction(") == -1:34 api_desc = "### {}. {}\n* 函数名:\n``` text\n\t[lua] {} --> [cpp] {}\n```\n* cpp函数原型:\n``` c++\n\t{}\n```\n\n".format(35 api_count, lua_func_name, lua_func_name, cpp_func_name, line.strip())36 # print(api_desc)37 readme_file.write(api_desc)38 api_count += 139print("total api function: {}".format(api_count))40src_file.close()41print("\n### 3. write register-lambda-function to README.md")42flag = True43startWith = "registerFunction<register_scheduler, "44src_file = open(src_file_name, 'r')45func_name = ""46return_type = ""47for line in src_file.readlines():48 line = line.strip()49 if line.startswith("/*"):50 flag = False51 elif line.endswith("*/"):52 flag = True53 if not flag:54 continue55 if line.find("registerFunction<") != -1:56 tokens = line.split(">")57 func_name = tokens[1][2:-2]58 print(" " + func_name)59 params = tokens[0][len(startWith):]60 params_tokens = params.split("(")61 return_type = params_tokens[0]62 # full_func_name = "{} {}({}".format(params_tokens[0], func_name, params_tokens[1])63 # print(full_func_name)64 # api_desc = "### {}. {}\n> 函数原型:\n``` c++\n{}\n```\n\n".format(api_count, func_name, full_func_name)65 # api_desc = "### {}. {}\n* 函数名:\n``` text\n\t[lua] {} --> [cpp] {}\n```\n* cpp函数原型:\n``` c++\n\t{}\n```\n\n".format(66 # api_count, func_name, func_name, func_name, full_func_name)67 # # print(api_desc)68 # readme_file.write(api_desc)69 # api_count += 170 if line.startswith("[](register_scheduler"):71 # print(line)72 tokens = line.split("fc_register,")73 params = tokens[1].strip()[:-1].strip()74 # print(params)75 full_func_name = "{} {}({}".format(return_type, func_name, params)76 api_desc = "### {}. {}\n* 函数名:\n``` text\n\t[lua] {} --> [cpp] {}\n```\n* cpp函数原型:\n``` c++\n\t{}\n```\n\n".format(77 api_count, func_name, func_name, func_name, full_func_name)78 # print(api_desc)79 readme_file.write(api_desc)80 api_count += 181 func_name = ""82src_file.close()...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful