LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-24-2017, 12:08 AM   #1
dy20082250
Member
 
Registered: Oct 2013
Location: China
Distribution: Fefora 9
Posts: 77

Rep: Reputation: Disabled
Is there python(2.7) master who can give me some directions python decorator?


Recently i learned python decorator and want to use it in my interface testcase
as below:


import requests
from nose.tools import istest, nottest
import xml.dom.minidom
from TestBase import TestBase
from decorators import methodrunning


Code:
class TestSpecial(TestBase):
    """This is a class to test all function of get method."""

    @istest
   def test_from_post(self):
        """To test form post."""
        self.endpoint = "forms/post"
        self.url = ''.join([self.host, self.endpoint])
        print self.url
        r = requests.get(self.url)
        print r.text
and I want to write a decorator like this:

Code:
def methodrunning(func):
    """Be used to decorate method."""
    def wrapper(*args, **kwargs):
        print "%s is running" % func.__name__
        u = func(*args, **kwargs)
        return u
    return wrapper
the decorator's purpose is to tell me that a method is beginning to run

but when i use it ,

@istest
@methodrunning
def test_from_post(self):
"""To test form post."""
self.endpoint = "forms/post"
self.url = ''.join([self.host, self.endpoint])
print self.url
r = requests.get(self.url)
print r.text[/CODE]

get the result below:

Code:
ERROR: Failure: AttributeError (TestSpecial instance has no attribute 'wrapper')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/wdy/interfacetest/testvir/lib/python2.7/site-packages/nose/loader.py", line 523, in makeTest
    return self._makeTest(obj, parent)
  File "/Users/wdy/interfacetest/testvir/lib/python2.7/site-packages/nose/loader.py", line 582, in _makeTest
    return MethodTestCase(obj)
  File "/Users/wdy/interfacetest/testvir/lib/python2.7/site-packages/nose/case.py", line 348, in __init__
    self.test = getattr(self.inst, method_name)
AttributeError: TestSpecial instance has no attribute 'wrapper'
I have to make it clear that @istest is nose's decorator
and @methodrunning is myself's decorator

Is there any master of python that can give me some advice?
 
Old 10-01-2017, 06:01 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,873

Rep: Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905Reputation: 2905
first of all you need to put all these things into one single piece of [code] instead of splitting it into different parts (not to speak about the missing indentation).
 
  


Reply

Tags
python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 04:03 AM
Writing a Python Class Give C# Equivalent Nexusfactor Programming 9 07-29-2015 01:04 PM
LFS with systemd, what features will Python give me? McZ Linux From Scratch 1 03-06-2013 02:21 AM
How to write a project to grab the UI into a image bitmap??? 1073710317 Programming 2 03-29-2011 01:52 PM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 09:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration