Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8407063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:16:44+00:00 2026-06-09T23:16:44+00:00

I’ve been reading and learning about decorators and their use in python. I figured

  • 0

I’ve been reading and learning about decorators and their use in python. I figured I would try to create a decorator for a python script I have been working on. From what I have read, I know there should be numerous ways to do this with the particular bit of code I have.

So far:

#! /usr/bin/env python
import mechanize
from BeautifulSoup import BeautifulSoup
import sys
import sqlite3

## create the Decorator class ##

class Deco:
## initialize function and pass in the function as an argument ##
    def __init__(self,func):
        self.func = func
## use the built in __call__ method to run when the decorator is called ##
## I having issues knowing what the proper to pass the function in as an argument ##
    def __call__(self,func):
## right here I going to call the Vocab().dictionary() method ##
        self.func
## I can have the Vocab method run after I don't have a preference ##
## this decorator is going to find the number of tables and entries in the ##
## database with every call ##
        conn = sqlite3.connect('/home/User/vocab_database/vocab.db')
        with conn:
            cur = conn.cursor()
            cur.execute("SELECT name FROM sqlite_master WHERE type='table'")
            total = cur.fetchall()
            print "You have %d tables " % len(total)
            cur.execute("SELECT * FROM %s" % (total[0][0],))
            ent = cur.fetchall()
            print "You have %d entries" % len(ent)

class Vocab:
    def __init__(self):
        self.word = sys.argv[1] 
        self.def_count = 1
        self.query = {}

    @Deco        
    def dictionary(self,word):
        br = mechanize.Browser()
        response = br.open('http://www.dictionary.reference.com')
        br.select_form(nr=0)
        br.form['q'] = word
        br.submit()
        definition = BeautifulSoup(br.response().read())
        trans = definition.findAll('td',{'class':'td3n2'})
        fin = [i.text for i in trans]
        for i in fin: 
            self.query[fin.index(i)] = i
        self.create_database()
        self.word_database()
        return self.query

    def create_database(self):
        con = sqlite3.connect('/home/oberon/vocab_database/vocab.db')
        with con:
            cur = con.cursor()
            cur.execute("CREATE TABLE IF NOT EXISTS Words(vocab_id INTEGER PRIMARY KEY, vocab TEXT)")
            cur.execute("CREATE TABLE IF NOT EXISTS Definitions(def_id INTEGER, def  TEXT, def_word INTEGER, FOREIGN KEY(def_word) REFERENCES Words(vocab_id))")

    def word_database(self):
        con = sqlite3.connect('/home/User/vocab_database/vocab.db')
        with con:
            spot = con.cursor()
            spot.execute("SELECT * FROM Words")
            rows = spot.fetchall() 
            spot.execute("INSERT INTO Words VALUES(?,?)", (len(rows),self.word))
            spot = con.cursor()
            spot.execute("SELECT * FROM Definitions")
            rows_two = spot.fetchall()
            for row in rows_two:
                self.def_count += 1
            for q in self.query:
                spot.execute("INSERT INTO Definitions VALUES(?,?,?)", (self.def_count,self.query[q],len(rows)))
                self.def_count += 1

print Vocab().dictionary(sys.argv[1])

After I run this code, the Deco prints out,runs and does everything inside the decorator’s method but, the Vocab().dictionary() prints out None:

You have 2 tables 
You have 4 entries
None

I am sure theres more wrong here than just getting the Vocab().dictionary() method to run. If someone could help shed some light on what is preventing this from working properly that be great and anything else I should be looking into with decoraters in general even better!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:16:45+00:00Added an answer on June 9, 2026 at 11:16 pm

    self.func does not call the function, as you seem to expect (judging from the comment). To call it you need to do self.func(). Moreover, if you want to wrap the function so that the value is returned to the outside, you need to do return self.func(), or store the value and return it later (after your with block).

    However, your code seems a bit suspect in other ways. For instance, you have __call__ accepting func as an argument, but it’s not used, whereas the dictionary method that the decorator supposedly wraps accepts an argument called word which is apparently supposed to be a string. This makes me think you’ve misunderstood how the decorator works.

    When you use @Deco as a decorator, the class is passed the function to decorate as an argument. The Deco instance itself is the result, so after this Vocab.dictionary is an instance of Deco. When you then call Vocab().dictionary(), you are invoking the __call__ method of Deco. So if you’re trying to wrap dictionary, your Deco’s __call__ should accept the same arguments that dictionary accepts, and it should pass them on to dictionary. (This may be why you got an error with self.func() — you were calling dictionary with no arguments, but it requires an argument.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.