Search Results

Search found 107 results on 5 pages for 'decorators'.

Page 1/5 | 1 2 3 4 5  | Next Page >

  • Testing Python Decorators?

    - by Jama22
    I'm writing some unit tests for a Django project, and I was wondering if its possible (or necessary?) to test some of the decorators that I wrote for it. Here is an example of a decorator that I wrote: class login_required(object): def __init__(self, f): self.f = f def __call__(self, *args): request = args[0] if request.user and request.user.is_authenticated(): return self.f(*args) return redirect('/login')

    Read the article

  • Class Decorators, Inheritence, super(), and maximum recursion

    - by jamstooks
    I'm trying to figure out how to use decorators on subclasses that use super(). Since my class decorator creates another subclass a decorated class seems to prevent the use of super() when it changes the className passed to super(className, self). Below is an example: def class_decorator(cls): class _DecoratedClass(cls): def __init__(self): return super(_DecoratedClass, self).__init__() return _DecoratedClass class BaseClass(object): def __init__(self): print "class: %s" % self.__class__.__name__ def print_class(self): print "class: %s" % self.__class__.__name__ bc = BaseClass().print_class() class SubClass(BaseClass): def print_class(self): super(SubClass, self).print_class() sc = SubClass().print_class() @class_decorator class SubClassAgain(BaseClass): def print_class(self): super(SubClassAgain, self).print_class() sca = SubClassAgain() # sca.print_class() # Uncomment for maximum recursion The output should be: class: BaseClass class: BaseClass class: SubClass class: SubClass class: _DecoratedClass Traceback (most recent call last): File "class_decorator_super.py", line 34, in <module> sca.print_class() File "class_decorator_super.py", line 31, in print_class super(SubClassAgain, self).print_class() ... ... RuntimeError: maximum recursion depth exceeded while calling a Python object Does anyone know of a way to not break a subclass that uses super() when using a decorator? Ideally I'd like to reuse a class from time to time and simply decorate it w/out breaking it.

    Read the article

  • Zend Form problem:Setting decorators for textarea and textinput length values

    - by davykiash
    In my Zend form code I have the following $address = new Zend_Form_Element_Textarea('accounts_address'); $address->setLabel('Address') ->setAttrib('rows','4') ->setAttrib('cols','4') ->addFilter('StripTags') ->addFilter('StringTrim') ->addValidator('NotEmpty'); $address->setDecorators(array( 'ViewHelper', 'Description', 'Errors', array(array('data'=>'HtmlTag'), array('tag' => 'td')), array('Label', array('tag' => 'td')), array(array('row'=>'HtmlTag'),array('tag'=>'tr')) )); However in my output I do get the attributes set but the text area seems to remain the same size Output <tr><td id="accounts_address-label"><label for="accounts_address" class="optional">Address</label></td> <td> <textarea name="accounts_address" id="accounts_address" rows="4" cols="4"></textarea></td></tr> What am I missing?

    Read the article

  • Zend Form, table decorators

    - by levacjeep
    Hello, I am having an incredibly difficult time to decorate a Zend form the way I need to. This is the HTML structure I am in need of: <table> <thead><tr><th>one</th><th>two</th><th>three</th><th>four</th></thead> <tbody> <tr> <td><input type='checkbox' id='something'/></td> <td><img src='src'/></td> <td><input type='text' id='something'/></td> <td><input type='radio' group='justonegroup'/></td> </tr> <tr> <td><input type='checkbox' id='something'/></td> <td><img src='src'/></td> <td><input type='text' id='something'/></td> <td><input type='radio' group='justonegroup'/></td> </tr> </tbody> </table> The number of rows in the body is determined by my looping structure inside my form class. All ids will be unique of course. All radio buttons in the form belongs to one group. My issue really is that I am unsure how to create and then style the object Zend_Form_Element_MultiCheckbox and Zend_Form_Element_Radio inside my table. Where/how would I apply the appropriate decoraters to the checkboxes and radio buttons to have a form structure like above? My Form class so far: class Form_ManageAlbums extends Zend_Form { public function __construct($album_id) { $photos = Model_DbTable_Photos::getAlbumPhotos($album_id); $selector = new Zend_Form_Element_MultiCheckbox('selector'); $radio = new Zend_Form_Element_Radio('group'); $options = array(); while($photo = $photos->fetchObject()) { $options[$photo->id] = ''; $image = new Zend_Form_Element_Image('image'.$photo->id); $image->setImageValue('/dog/upload/'.$photo->uid.'/photo/'.$photo->src); $caption = new Zend_Form_Element_Text('caption'.$photo->id); $caption->setValue($photo->caption); $this->addElements(array($image, $caption)); } $selector->addMultiOptions($options); $radio->addMultiOptions($options); $this->addElement($selector); $this->setDecorators(array( 'FormElements', array('HtmlTag', array('tag' => 'table')), 'Form' )); } } I have tried a few combination of decoraters for the td and tr, but no success to date. Thank you for any help, very appreciated. JP Levac

    Read the article

  • Use python decorators on class methods and subclass methods

    - by AlexH
    Goal: Make it possible to decorate class methods. When a class method gets decorated, it gets stored in a dictionary so that other class methods can reference it by a string name. Motivation: I want to implement the equivalent of ASP.Net's WebMethods. I am building this on top of google app engine, but that does not affect the point of difficulty that I am having. How it Would look if it worked: class UsefulClass(WebmethodBaseClass): def someMethod(self, blah): print(blah) @webmethod def webby(self, blah): print(blah) # the implementation of this class could be completely different, it does not matter # the only important thing is having access to the web methods defined in sub classes class WebmethodBaseClass(): def post(self, methodName): webmethods[methodName]("kapow") ... a = UsefulClass() a.post("someMethod") # should error a.post("webby") # prints "kapow" There could be other ways to go about this. I am very open to suggestions

    Read the article

  • How can I check if Zend_Form_Elements has no decorators set

    - by jiewmeng
    I find that even if I just declare an element like $this->addElement('textarea', 'txt1'); I find that it already has decorators set Zend_Debug::dump($this->getElement('txt1')->getDecorators()); http://pastebin.com/7Y24g62w I want to test that I didn't set decorators using setDecorators() or using something like $this->addElement('textarea', 'txt1', array( 'decorators' => array(...) )); If I didn't set any decorators then apply default decorators, how can I do that. I want to apply default decorators per element basis, not using Zend_Form#setDisableLoadDefaultDecoraotrs()

    Read the article

  • Is there a better way of designing zend_forms rather than using decorators?

    - by Hanseh
    Hi, I am currently using zend_decorators to add styles to my form. I was wondering if there is an alternative way of doing it? It is a bit difficult to write decorators. I would love the casual one using divs and css style : <input type="submit" class="colorfulButton" > It is much simpler rather than set a decorator for a certain control and add it. Since it requires creating a decorator for each style implementation and adding it up with the control. Will view helpers to the trick?

    Read the article

  • Named keywords in decorators?

    - by wheaties
    I've been playing around in depth with attempting to write my own version of a memoizing decorator before I go looking at other people's code. It's more of an exercise in fun, honestly. However, in the course of playing around I've found I can't do something I want with decorators. def addValue( func, val ): def add( x ): return func( x ) + val return add @addValue( val=4 ) def computeSomething( x ): #function gets defined If I want to do that I have to do this: def addTwo( func ): return addValue( func, 2 ) @addTwo def computeSomething( x ): #function gets defined Why can't I use keyword arguments with decorators in this manner? What am I doing wrong and can you show me how I should be doing it?

    Read the article

  • Zend Framework AjaxContext filters the results and Decorators not removable

    - by Janis Peisenieks
    Ok, since this problem has 2 parts, it will be easier to explain them together. So here goes: I am trying to remove the default decorators from these elements, since I am using a little different way of styling them. But no matter what i do, the DtDDWrapper still shows up. If I try to remove all of the decorators, all of the fields below disappear. public function newfieldAction() { $ajaxContext = $this->_helper->getHelper('AjaxContext'); $ajaxContext->addActionContext('newfield', 'html')->initContext(); $id = $this->_getParam('id', null); $id1=$id+1; $id2=$id+2; $element = new Zend_Form_Element_Text("newTitle$id1"); $element->setOptions(array('escape'=>false)); $element->setRequired(true)->setLabel('Vertiba')->removeDecorator('label'); $tinyelement=new Zend_Form_Element_Text("newName$id"); $tinyelement->setRequired(true)->setOptions(array('escape'=>false))->setLabel('Vertiba')->removeDecorator('label'); $textarea_element = new Zend_Form_Element_Textarea("newText$id2"); $textarea_element->setRequired(true)->setOptions(array('escape'=>false))->setLabel('Vertiba')->removeDecorator('label'); $this->view->descriptionField = "<td>".$textarea_element->__toString()."</td>"; $this->view->titleField = $element->__toString(); $this->view->field = $tinyelement->__toString(); $this->view->id=$id; } The context view script seams to trim my code in one way or another. When I try to put a <td> or a <table> tag in the view script, it just skips the tags. Is there a way to stop this escaping from happening? My view script: id; ?" asdfasdfasdfasd field ? titleField ? descriptionField ? id ?"remove P.S. the code formatting system is barfing at me, could someone please help me with the formatting of the code?

    Read the article

  • using zend form decorators

    - by pradeep
    <div class="field50Pct"> <div class="fieldItemLabel"> <label for='First Name'>First Name:</label> </div> <div class="fieldItemValue"> <input type="text" id="firstname" name="firstname" value="" /> </div> </div> <div class="clear"></div> I want the code to appear like this in source code . how do i write the same thing in zend using decorators ? The element is like $firstname = new Zend_Form_Element_Text('FirstName'); $firstname->setLabel('FirstName') ->setRequired(true) ->addFilter('StripTags') ->addFilter('StringTrim') ->addErrorMessage('Error in First Name') ->addValidator('NotEmpty');

    Read the article

  • how to pass in dynamic data to decorators

    - by steve
    Hi, I am trying to write a base crud controller class that does the following: class BaseCrudController: model = "" field_validation = {} template_dir = "" @expose(self.template_dir) def new(self, *args, **kwargs) .... @validate(self.field_validation, error_handler=new) @expose() def post(self, *args, **kwargs): ... My intent is to have my controllers extend this base class, set the model, field_validation, and template locations, and am ready to go. Unfortunately, decorators (to my understanding), are interpreted when the function is defined. Hence it won't have access to instance's value. Is there a way to pass in dynamic data or values from the sub class? If not, I guess I could use override_template as a workaround to expose and set the template within the controller action. How would I go about validating the form within the controller action? Thanks, Steve

    Read the article

  • Table Decorators on Zend Framework Form

    - by ulduz114
    hello i created a form that it decorates as table form its my code for decorates $this->setElementDecorators(array( 'ViewHelper', 'Errors' array(array('data'=>'HtmlTag'), array('tag'=>'td','class'=>'element')), array('Label',array('tag'=>'td')), array(array('row'=>'HtmlTag'),array('tag'=>'tr')), )); $this->setDecorators(array( 'FormElements', array('HtmlTag',array('tag'=>'table')), 'Form' )); it works correctly, now i wana errors message decorates too what do i change my code?

    Read the article

  • confused about python decorators

    - by nbv4
    I have a class that has an output() method which returns a matplotlib Figure instance. I have a decorator I wrote that takes that fig instance and turns it into a Django response object. My decorator looks like this: class plot_svg(object): def __init__(self, view): self.view = view def __call__(self, *args, **kwargs): print args, kwargs fig = self.view(*args, **kwargs) canvas=FigureCanvas(fig) response=HttpResponse(content_type='image/svg+xml') canvas.print_svg(response) return response and this is how it was being used: def as_avg(self): return plot_svg(self.output)() The only reason I has it that way instead of using the "@" syntax is because when I do it with the "@": @plot_svg def as_svg(self): return self.output() I get this error: as_svg() takes exactly 1 argument (0 given) I'm trying to 'fix' this by putting it in the "@" syntax but I can't figure out how to get it working. I'm thinking it has something to do with self not getting passed where it's supposed to...

    Read the article

  • Zend Framework Zend_Form Decorators: <span> Inside Button Element?

    - by leek
    I have a button element that I've created like so: $submit = new Zend_Form_Element_Button('submit'); $submit->setLabel('My Button'); $submit->setDecorators(array( 'ViewHelper', array('HtmlTag', array('tag' => 'li')) )); $submit->setAttrib('type', 'submit'); This generates the following HTML: <li> <label for="submit" class="optional">My Button</label> <button name="submit" id="submit" type="submit">My Button</button> </li> I would like to wrap the inside of the button with a <span, like this: <button...><span>My Button</span></button> What is the best way to do this using Zend_Form?

    Read the article

  • Python Decorators and inheritance

    - by wheaties
    Help a guy out. Can't seem to get a decorator to work with inheritance. Broke it down to the simplest little example in my scratch workspace. Still can't seem to get it working. class bar(object): def __init__(self): self.val = 4 def setVal(self,x): self.val = x def decor(self, func): def increment(self, x): return func( self, x ) + self.val return increment class foo(bar): def __init__(self): bar.__init__(self) @decor def add(self, x): return x Oops, name "decor" is not defined. Okay, how about @bar.decor? TypeError: unbound method "decor" must be called with a bar instance as first argument (got function instance instead) Ok, how about @self.decor? Name "self" is not defined. Ok, how about @foo.decor?! Name "foo" is not defined. AaaaAAaAaaaarrrrgggg... What am I doing wrong?

    Read the article

  • Multi-argument decorators in 2.6

    - by wheaties
    Generally don't do OO-programming in Python. This project requires it and am running into a bit of trouble. Here's my scratch code for attempting to figure out where it went wrong: class trial(object): def output( func, x ): def ya( self, y ): return func( self, x ) + y return ya def f1( func ): return output( func, 1 ) @f1 def sum1( self, x ): return x which doesn't compile. I've attempted to add the @staticmethod tag to the "output" and "f1" functions but to no avail. Normally I'd do this def output( func, x ): def ya( y ): return func( x ) + y return ya def f1( func ): return output( func, 1 ) @f1 def sum1( x ): return x which does work. So how do I get this going in a class?

    Read the article

  • Setting the inner html text of a < span > element using Zend_Form_Decorators

    - by Mallika Iyer
    I'm trying to set the inner html of the < span tag here , so it looks like: Group this is what i have so far: $form->addDisplayGroup( array( ................ ), 'maingroup1', array( 'legend'=>'', 'disableDefaultDecorators'=> true, 'decorators'=> array('FormElements', array('FieldSet',array('class'=>'dashed-outline2')), array(array('SpanTag' => 'HtmlTag'), array('tag'=>'span','class' => 'group',)), array('HtmlTag',array('tag'=>'div','id'=>'group1','class'=>'group','openOnly'=> true)) ) ) ); Is there a setter / property that I can use to set the inner text of the < span element using Zend_form_decorators? Thanks.

    Read the article

  • What's the simplest way to create a Zend_Form tabular display with each row having a radio button?

    - by RenderIn
    I've seen simple examples of rendering a Zend_Form using decorators, but I'm not sure they are able to handle the issue I'm facing very well. I query the database and get an array of user objects. I want to display these users as a form, with a radio button next to each of them and a submit button at the bottom of the page. Here's roughly what the form will look like: [user id] [email] [full name] ( ) 1 [email protected] Test user 1 (*) 2 [email protected] Test user 2 [SUBMIT] Is this something achievable in a reasonably straightforward way or do I need to use the ViewScript partial?

    Read the article

  • Any way to set or overwrite the __line__ and __file__ metadata?

    - by charles.merriam
    I'm writing some code that needs to change function signatures. Right now, I'm using Simionato's FunctionMaker class, which uses the (hacky) inspect module, and does a compile. Unfortunately, this still loses the line and file metadata. Does anyone know: If it is possible to overwrite these values in some odd way? If hacking up a class with a complex getattribute() to intercept the values and also try to make the class looks like a function is any more possible than a moose with a flying nun hat? Is there an alternative to the (hacky) inspect module? PEP 362 is dead dead dead? I know decorators and cPickle users fight with this. What other situations is the read only metadata in people's way? I appreciate any insights. Thank you.

    Read the article

  • Adding a decorator that converts strings to lowercase in Python

    - by user2905382
    So I am new to learning decorators and I have gone through countless tutorials and while I understand and can mostly follow all of the examples, I think the best way to learn, would be to implement a decorator myself. So I am going to use this example below. I realize a decorator is not at all necessary to do this, but for the sake of learning, I would like to add a decorator that filters the strings like dog name and breed and turns them into lowercase. Any ideas or pointers in the right direction would be appreciated. class Dogs: totalDogs = 0 dogList=[] def __init__(self, breed, color, age): self.breed=breed self.color=color self.age=age Dogs.dogList.append(self.breed) Dogs.totalDogs += 1 def displayDogs(self): print "breed: ", self.breed print "color: ",self.color print "age: ",self.age print "list of breeds:", Dogs.dogList print "total dogs: ", Dogs.totalDogs def somedecorator(*args): #now what terrier=Dogs("TeRrIer", "white", 5) terrier.displayDogs() retriever=Dogs("goldenRETRIEVER", "brown", 10) retriever.displayDogs()

    Read the article

  • Decorators vs. classes in python web development.

    - by Tristan
    I've noticed three main ways Python web frameworks deal request handing: decorators, controller classes with methods for individual requests, and request classes with methods for GET/POST. I'm curious about the virtues of these three approaches. Are there major advantages or disadvantages to any of these approaches? To fix ideas, here are three examples. Bottle uses decorators: @route('/') def index(): return 'Hello World!' Pylons uses controller classes: class HelloController(BaseController): def index(self): return 'Hello World' Tornado uses request handler classes with methods for types: class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") Which style is the best practice?

    Read the article

1 2 3 4 5  | Next Page >