zoukankan      html  css  js  c++  java
  • robot源码解读8【aliasing_scope】

    1. aliasing
    
    '''
    ##################
    '''
    #
    # class Animal:
    #     def __init__(self, favourite_food):
    #         self.favourite_food = favourite_food
    #
    #
    # class Zoo:
    #     def new_animal(self, favourite_food):
    #         return Animal(favourite_food)
    #
    #     def feed(self, food, animal):
    #         print("Im giving {} to {}.".format(food, animal))
    #
    #     def get_favourite_food(self, animal):
    #         return animal.favourite_food
    #
    #
    # z1 = Zoo()
    # animal = z1.new_animal('beef')
    # food = z1.get_favourite_food(animal)
    # z1.feed(food, animal)
    
    '''
    Previously, to use these keywords in Robot Framework, you would have to do
    this:
    
        ${animal}=    New animal    beef
        ${food}=      Get favourite food
        Feed          ${food}       ${animal}
    
    After the aforementioned modifications, these keywords can be used in the
    following way:
    
        New animal    beef                    Lion
        ${food}=      Get favourite food      Lion
        Feed          ${food}                 Lion
    '''
    
    
    
    '''
    ##################
    '''
    # from taf.internal.aliasing import aliasing_scope
    from aliasing import aliasing_scope
    import logging
    
    
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s:%(message)s')
    logger = logging.getLogger(__name__)
    
    
    class Animal:
        def __init__(self, favourite_food):
            self.favourite_food = favourite_food
    
    
    class Man:
        def __init__(self, favourite_food):
            self.favourite_food = favourite_food
    
    
    class Zoo:
        # lookup_handler, store = aliasing_scope()
        lookup_handler, store = aliasing_scope('animal', Animal, 'test_aliasing_scope')
        # lookup_handler, store = aliasing_scope('animal', Animal)
        # lookup_handler_man, store_man = aliasing_scope('man', Man, 'test2')
    
        print('lookup_handler: ', lookup_handler.__dict__)
        print('store         : ', store.__dict__)
    
        def new_animal(self, favourite_food, name=None):
            new_animal = Animal(favourite_food)
            self.store.add(new_animal, name)
            print('def new_animal: ',self.store.__dict__)
            return new_animal
    
        # def new_man(self, favourite_food, name=None):
        #     new_man = Man(favourite_food)
        #     self.store_man.add(new_man, name)
        #     print('def new_man: ',self.store.__dict__)
        #     return new_man
    
        @lookup_handler.implicit_lookup('animal')
        def feed(self, food, animal):
            print("Im giving {} to {}.".format(food, animal))
    
        # @lookup_handler_man.implicit_lookup
        # def eat(self, food, man):
        #     print("{} is eating {} .".format(man, food))
    
        # @lookup_handler.implicit_lookup('animal')  # 前面lookup_handler, store = aliasing_scope(),这里需要指定 animal
        # @lookup_handler.implicit_lookup('animal222') # 默认就这样用
        @lookup_handler.implicit_lookup('animal222')
        def get_favourite_food(self, animal222):
            return animal222.favourite_food
    
    
    
    
    z1 = Zoo()
    # animal
    # Lion = z1.new_animal('beef')  # 主要是替换这种用法,Lion不用对象,可以用字符串
    # z1.new_animal('beef', 'Lion')
    # food = z1.get_favourite_food('Lion')
    # z1.feed(food, 'Lion')
    
    
    # 原始用法
    # print('@@')
    # Lion2 = z1.new_animal('beef')
    # food2 = z1.get_favourite_food(Lion2)
    # z1.feed(food2,Lion2)
    # print('@@')
    
    
    # default
    # z1.new_animal('food')
    # food = z1.get_favourite_food('amize_default')
    # z1.feed(food,'amize_default')
    
    
    # man
    # z1.new_man('rice', 'Jhon')
    # z1.eat('rice', 'Jhon')
    
    
    
    
  • 相关阅读:
    linux 命令——48 watch (转)
    linux 命令——47 iostat (转)
    linux 命令——46 vmstat(转)
    linux 命令——45 free(转)
    linux 命令——44 top (转)
    linux 命令——43 killall(转)
    linux 命令——42 kill (转)
    linux 命令——41 ps(转)
    linux 命令——40 wc (转)
    Java for LeetCode 068 Text Justification
  • 原文地址:https://www.cnblogs.com/amize/p/15045148.html
Copyright © 2011-2022 走看看