How frequently should Python decorators be used?
I recently started experimenting with Python decorators (and higher-order functions) because it looked like they might make my Django unit tests more concise. e.g., instead of writing:
I could instead do
However, after some experimenting, I have found that decorators are not as simple as I had hoped. First, I was confused by the different decorator syntax I found in different examples, until I learned that decorators behave very differently when they take arguments. Then I tried decorating a method, and eventually learned that it wasn't working because I first have to turn my decorator into a descriptor by adding a
__get__ method. During this whole process I've ended up confused more than a few times and still find that debugging this "decorated" code is more complicated than it normally is for Python. I'm now re-evaluating whether I really need decorators in my code, since my initial motivation was to save a bit of typing, not because there was anything that really required higher-order functions.
So my question is: should decorators be used liberally or sparingly? Is it ever more Pythonic to avoid using them?
How can I design an algorithm which can return the 10 most frequently used words in a document in O(n) time? If additional space can be used. I can parse and place the words in a hash map with count .
I am using Ruby on Rails 3.2.2 and Cucumber with the cucumber-rails gem. I would like to know what Cucumber tags are commonly used throughout an application or at least on what criteria I should think
I'm spending today learning Ruby from a Python perspective. One thing I have completely failed to grapple with is an equivalent of decorators. To pare things down I'm trying to replicate a trivial Pyt
I've been playing around with a timing decorator for my pylons app to provide on the fly timing info for specific functions. I've done this by creating a decorator & simply attaching it to any fun
This article, linked to a number of times from various stackoverflow questions, describes how decorators with arguments are syntactically different from those without arguments. Decorators without ar
One of my classes has a logical numpy array as parameter in many methods repeated (idx_vector=None). How can I use a decorator to: automatically specify idx_vector automatically insert the descriptio
I'm using Hadoop for data processing with python, what file format should be used? I have project with a substantial amount of text pages. Each text file has some header information that I need to pre
Sorry for the bad example. I'm just trying to understand the decorators in python. code as follows #!/usr/bin/env python3 def my_dec(func): def wrap(w): t=func(w) return t*4 return wrap @my_dec def
What are the best practices on updating the apk files in the Android Market ? Is it ok to publish a new version as soon as i fix a minor glitch or should i consolidate a few bugs (if those or minor) a
I have tried to check the best practices for using asp.net mvc and quite a few say that we should never use ViewData. I have read this post and it seems like it from that. One reason that I can think
should the functions be defined before it is used? but why the following code works: def main(): dog() def dog(): print(This is a dog.) if __name__ == '__main__': main() I mean the dog() is defined
Just learning about exception handling. I'm wondering if exception handling should be used for all programming situations? any help will be greatly appreciated
I've seen lots of descriptions how anonymous types work, but I'm not sure how they're really useful. What are some scenarios that anonymous types can be used to address in a well-designed program?
I am writing a program that executes a set of tasks sequentially. Each task is a function that outputs a new file, but any given task should not execute if the filename already exists. I find myself w
I am wondering how big object are too big for stack? If we are talking about normal PC (either Windows or some Linux). I've tried to find answer but all I found was allocate on heap if objects are too
As the title implies, I have a question concerning decorators in the Zend Framework. Please consider the following code example: $this->addElement('select', 'DisplayUntil', array( 'label' => 'Di
I'd like to see the output of some of my Draper Decorators in Rails console (https://github.com/drapergem/draper and http://railscasts.com/episodes/286-draper). To do so, I was looking for a way to in
Can you detail how to setup the form decorators to end up with the following html? I'm not very good with the form decorators. twitterbootstrapform
So I have been doing some research into how I should be doing try-catch-finally blocks and there is some conflicting information in every post I read. Can someone clarify? One common idea is to not ca
What's the best way to toggle decorators on and off, without actually going to each decoration and commenting it out? Say you have a benchmarking decorator: # deco.py def benchmark(func): def decorato
These are the error messages I am getting please advise. The value of the field LocationListActivity.adapter is not used The value of the field LocationListActivity.lv1 is not used The value of the f
How should (or is a clean way) of organising methods in Python? I always put the __init__ method first, followed by any other __foo__ (What do you call them?) methods. But then it leads into a jumble.
I'm trying to install python-ldap on Mac OS X 10.6, but I get this error because it's using 10.4u.sdk instead of 10.6.sdk. How can I specify the SDK version to be used? So far, I tried specifying the
(I follow this post to got the logic how to use Zend_Form without using decorators.) within the action I create Zend_Form object and assigned the form to the view before rendering it: public function
I'm having a hard time understanding what happens when I try to nest descriptors/decorators. I'm using python 2.7. For example, let's take the following simplified versions of property and classmethod
I am currently using Behave (BDD for Python) and have been digging in the source code to understand how the @given, @when and @then decorators are being declared. The farthest I've gone is to look at
This question already has an answer here: Python - making decorators with optional arguments 6 answers I would like to make a decorator which could be used with or without a parameter : Somethi
I was assigned to delete tables and indexes in a database which are used less frequently. Is there any tool or something that would say how many times a table has been queried ? And also is it possibl
I was trying to write a script to frequently switch my IP address. Does anyone know where I can start? Should I use VPN service? Is there any recommended US VPN service? Thanks!
Some decorators should only be used in the outermost layer. A decorator that augments the original function and add a configure parameter is one example. from functools import wraps def special_case(f
There are are 150 000 records in my comments table and about 1000 new comments are added per day... I wonder if I should use indexes on a table like this? Will it have positive or negative impact on p
Methods setUp() and tearDown() are invoked before and after each test. But really, is there any real word example about why should I need this? Inspecting other people tests, I always see something li
I'm new to python and I'm exploring my way through the language while building real applications. Right now I'm trying to build an event-based app, and was thinking about using decorators as a way to
Is there are a way to pass a variable between two python decorators applied to the same function? The goal is for one of the decorators to know that the other was also applied. I need something like d
I need this to be used as a delimiter, has anyone known about this statistics?
In my other (unrelated) question, @Meleak had SnapsToDevicePixels scattered throughout the code (in his answer), I found out it is used to make things look sharper. So the question, why is it not used
how to use this code in python interactive shell (interpreter): @makebold @makeitalic def hello(): print (Hello, world!) in shell I get this error: >>> @makebold ... hello() File <stdi
I'm developing a HTML5 game and I need to know if updating localStorage properties frequently can slow down the page. I'm actually storing my hero's position in four localStorage properties (two for t
I thought I understood decorators but not anymore. Do decorators only work when the function is created? I wanted to create a series of functions that all have a required argument called 'ticket_param
Just as the title asks, when should a trigger in your head go off signifying Aha! I should use the factory pattern here!? I find these moments will occur with many other design patterns, but never d
I am trying to make use of some of the nice features in simple injector. I am currently having problems with the decorators, they are not getting hit when I expect them too. I am registering them like
This question already has an answer here: Given a file, find the ten most frequently occuring words as efficiently as possible 14 answers I am aware that this has been asked on the forum a coup
Usually, an OCaml program can be written with or without objects. When is it most beneficial to use objects, and when should they be avoided?
I'm looking for a way to make programs appear (frequently) used, so that they would appear in the Start menu's Recently Used Programs (after a zero touch install). I'm trying to figure out how Windo
Say you have the following code: def addTags(functionHere): def wrapper(): return NumberTag + functionHere() + NumberTagOver return wrapper @addTags def numbers(firstValue, secondValue): return fi
I was just reading up on a robotics-oriented implementation for Python called Pyro. It stopped production in 2005, but I wonder if anyone knows if its usable in 2.6, and how it compares to other langu
I'm sort of a novice developer trying to expand my toolbox and learn some more tricks. I recently came across a pattern in Python called decoration and I was wondering if/how I could implement this
I was watching a Google Tech Talks video, and they frequently referred to polymorphism. What is polymorphism, what is it for, and how is it used?
Having (more) issues with zend form decorators. I've got this so far: Reset overall form decorator: $this->clearDecorators(); $this->setDecorators(array('FormElements', 'Form')); I'm adding al
Given a para as input, find the most frequently occurring character. Note that the case of the characterdoes not matter. If more than one characterhas the same maximum occurring frequency, return all