decorator

Can IntelliJ automatically create a decorator class?

若如初见. 提交于 2019-12-20 11:29:06
问题 Sometimes, I create a decorator class like this: class MyInterfaceDecorator implements MyInterface { private final MyInterface delegate; ... constructor taking a MyInterface instance ... @Override public Object someInterfaceMethod(Some argument) { return delegate.someInterfaceMethod(argument); } ... etc, more methods here... } Can IntelliJ automatically create this class for me? 回答1: Update// I noticed that IntelliJ has a "Generate" option for generating delegate methods. Create a new class:

How can I use a class instance variable as an argument for a method decorator in Python?

感情迁移 提交于 2019-12-20 10:55:38
问题 How can I use a class instance variable as an argument for a method decorator in Python? The following is a minimal example shows what I'm trying to do. It obviously fails as the decorator function does not have access to the reference to the instance and I have no idea how to get access to the reference from the decorator. def decorator1(arg1): def wrapper(function): print "decorator argument: %s" % arg1 return function return wrapper class Foo(object): def __init__(self, arg1): self.var1 =

Why do I need to decorate login_required decorator with @method_decorator

半城伤御伤魂 提交于 2019-12-20 10:42:05
问题 I am trying to understand the code for the mixins posted at this blog post. These mixins call the login_required decorator from django.contrib.auth.decorators within the mixins , but they do so decorated by the method_decorator from django.utils.decorators . In the sample code below I dont understand why I need to decorate the login_required decorator . from django.utils.decorators import method_decorator from django.contrib.auth.decorators import login_required class LoginRequiredMixin

Best way to override lineno in Python logger

早过忘川 提交于 2019-12-20 10:00:55
问题 I have written a decorator which logs the arguments used to call a particular function or method. As shown below, it works well except that the line number reported in the logRecord is the line number of the decorator rather than the line number of func that is being wrapped: from functools import wraps import inspect import logging arg_log_fmt = "{name}({arg_str})" def log_args(logger, level=logging.DEBUG): """Decorator to log arguments passed to func.""" def inner_func(func): line_no =

Decorators on functions

纵饮孤独 提交于 2019-12-20 09:46:23
问题 I see that babel.js decorators (available in "stage 1") implement the spec at https://github.com/wycats/javascript-decorators. It appears that decorators are limited to (1) classes, (2) accessors, and (3) methods. In my case, I want to use decorators on plain old functions , as in @chainable function foo() { } where (just an example) function chainable(fn) { return function() { fn.apply(this, arguments); return this; }; } I don't see any logical reason why decorators should not be able to

What are Python metaclasses useful for?

旧时模样 提交于 2019-12-20 08:35:17
问题 What can be done with metaclasses that can't be in any other way? Alex Martelli told that there are tasks that can't be achieved without metaclasses here Python metaclasses vs class decorators I'd like to know which are? 回答1: Metaclasses are indispensable if you want to have class objects (as opposed to instances of class objects) equipped with "special customized behavior", since an object's behavior depends on special methods on the type of the object, and a class object's type is, exactly

Strategy Pattern V/S Decorator Pattern

假装没事ソ 提交于 2019-12-20 07:59:12
问题 I just came across two patterns. Strategy Pattern Decorator Strategy Pattern :- Strategy pattern gives several algorithms that can be used to perform particular operation or task. Decorator Pattern :- Decorator pattern adds some functionality to component. In fact I have found that Strategy Pattern and Decorator Pattern can also be used interchangeably. Here is the link :- When and How Strategy pattern can be applied instead of decorator pattern? What is the difference between Strategy

c++ decorator pattern, static polymorphism with templates and registering callback methods

陌路散爱 提交于 2019-12-20 07:28:08
问题 I am attempting to use static polymorphism to create a decorator pattern. As to why I do not use dynamic polymorphism, please see this QA. Basically, I could not dynamic_cast to each decorator so as to access some specific functionality present only in the decorators (and not in the base class A). With static polymorphism this problem has been overcome, but now I cannot register all the et() methods from the decorators back to the base class A (as callbacks or otherwise), thus when A::et()

How do I write angular2 without decorator syntax?

社会主义新天地 提交于 2019-12-20 05:25:28
问题 I'm roughly following JavaScript/TypeScript quickstart for Angular2 to write my app in ES6 but can't get the decoractor to works entry.js import * as stylesheet from '../assets/styles/app.scss'; import jQuery from '../node_modules/jquery/dist/jquery'; import $ from '../node_modules/jquery/dist/jquery'; import * as semanticUi from '../node_modules/semantic-ui/dist/semantic'; import '../node_modules/angular2/bundles/angular2-polyfills' import '../node_modules/rxjs/bundles/Rx.umd' import '..

What happens when I wrap I/O streams twice?

筅森魡賤 提交于 2019-12-20 04:21:03
问题 I know that java I/O uses decorator pattern. But I feel that I understand its wrong. Please clarify difference between two code snippets: snippet 1: PipedInputStream pipedInputStream = new PipedInputStream(); PipedOutputStream pipedOutputStream = new PipedOutputStream(); pipedOutputStream.connect(pipedInputStream); ObjectOutputStream objectOutputStream = new ObjectOutputStream(pipedOutputStream); objectOutputStream.writeObject("this is my string"); ObjectInputStream objectInputStream = new