decorator

How can I make class properties immutable?

 ̄綄美尐妖づ 提交于 2021-02-19 06:52:06
问题 @property is a nice way to define getters. When the property is mutable, the reference returned can be used to modify the property in ways not controlled by the class definition. I'll use a banana stand as a motivating analogy, but this issue applies to any class that wraps a container. class BananaStand: def __init__(self): self._money = 0 self._bananas = ['b1', 'b2'] @property def bananas(self): return self._bananas def buy_bananas(self, money): change = money basket = [] while change >= 1

decorator function syntax python

只愿长相守 提交于 2021-02-19 04:31:10
问题 I am learning decorator functions in python and I am wrapping my head around the @ syntax. Here is a simple example of a decorator function which calls the relevant function twice. def duplicator(func): @functools.wraps(func) def wrapper(*args, **kwargs): func(*args, **kwargs) func(*args, **kwargs) return wrapper If I understand correctly, it appears that: @duplicator def print_hi(): print('We will do this twice') Is equivalent to: print_hi = duplicator(print_hi) print_hi() However, let's

How can I resolve a decorator using a particular instance of a component or another decorator object in .net core

▼魔方 西西 提交于 2021-02-10 07:01:36
问题 After learning about the Decorator Pattern with typical Coffee example where Decorator Pattern saves us from the class explosion problem, I wrote some code to use and see it myself. Lets take a look at the UML first... and here is the code: Component ICofee defination: public interface ICoffee { string Name { get; } decimal Cost { get; } } LatteCoffee definition: public class LatteCoffee : ICoffee { public string Name { get; } = "Latte"; public decimal Cost => 2.00m; } Decorators

Make decorator for singelton class — typescript

折月煮酒 提交于 2021-02-08 17:00:03
问题 I am building a project using VueJS with Typescript. I feel comfortable using services instead of any state management library like Vuex. But when writing services I have to always copy paste some code in each service class, to make it singelton as: class MyService { private static Instance: MyService; public static getInstance() { if (!MyService.Instance) { MyService.Instance = new MyService(); } return MyService.Instance; } private constructor() {} } I was thinking about the decorators, so

Make decorator for singelton class — typescript

喜欢而已 提交于 2021-02-08 16:58:10
问题 I am building a project using VueJS with Typescript. I feel comfortable using services instead of any state management library like Vuex. But when writing services I have to always copy paste some code in each service class, to make it singelton as: class MyService { private static Instance: MyService; public static getInstance() { if (!MyService.Instance) { MyService.Instance = new MyService(); } return MyService.Instance; } private constructor() {} } I was thinking about the decorators, so

Python: Counting executing time of a recursion function with decorator

♀尐吖头ヾ 提交于 2021-02-08 07:47:39
问题 I want to write a function which behaves exactly similar to the given function, except that it prints the time consumed in executing it. Just like this: >>> fib = profile(fib) >>> fib(20) time taken: 0.1 sec 10946 This is my code,and it will print message in each function call. import time def profile(f): def g(x): start_time = time.clock() value = f(x) end_time = time.clock() print('time taken: {time}'.format(time=end_time-start_time)) return value return g @profile def fib(n): if n is 0 or

How can I decorate all inherited methods in a subclass

别说谁变了你拦得住时间么 提交于 2021-02-08 06:10:49
问题 class Reader: def __init__(self): pass def fetch_page(self): with open('/dev/blockingdevice/mypage.txt') as f: return f.read() def fetch_another_page(self): with open('/dev/blockingdevice/another_mypage.txt') as f: return f.read() class Wrapper(Reader): def __init__(self): super().__init__() def sanity_check(func): def wrapper(): txt = func() if 'banned_word' in txt: raise Exception('Device has banned word on it!') return wrapper @sanity_check <how to automatically put this decorator on each

Decorators on abstract methods

元气小坏坏 提交于 2021-02-07 04:44:05
问题 In python, is there a way to make a decorator on an abstract method carry through to the derived implementation(s)? For example, in import abc class Foo(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod @some_decorator def my_method(self, x): pass class SubFoo(Foo): def my_method(self, x): print x SubFoo 's my_method won't get decorated with some_decorator as far as I can tell. Is there some way I can make this happen without having to individually decorate each derived class of Foo ?

Doctest and Decorators in Python

青春壹個敷衍的年華 提交于 2021-02-07 03:54:47
问题 I was trying to use Python decorator to catch exceptions and log the exceptions. import os.path import shutil class log(object): def __init__(self, f): print "Inside __init__()" self.f = f def __call__(self, *args): print "Inside __call__()" try: self.f(*args) except Exception: print "Sorry" @log def testit(a, b, c): print a,b,c raise RuntimeError() if __name__ == "__main__": testit(1,2,3) It works fine Desktop> python deco.py Inside __init__() Inside __call__() 1 2 3 Sorry The issue is that

Doctest and Decorators in Python

不羁的心 提交于 2021-02-07 03:53:16
问题 I was trying to use Python decorator to catch exceptions and log the exceptions. import os.path import shutil class log(object): def __init__(self, f): print "Inside __init__()" self.f = f def __call__(self, *args): print "Inside __call__()" try: self.f(*args) except Exception: print "Sorry" @log def testit(a, b, c): print a,b,c raise RuntimeError() if __name__ == "__main__": testit(1,2,3) It works fine Desktop> python deco.py Inside __init__() Inside __call__() 1 2 3 Sorry The issue is that