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 7039207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:46:09+00:00 2026-05-28T01:46:09+00:00

I’m still quite new to python with less than a year of experience, and

  • 0

I’m still quite new to python with less than a year of experience, and I’ve been learning it through building a rather large project on google app engine. It’s grown to be a behemoth of 10k+ lines of code and html templates, so I’m in the process of refactoring a rather large portion of the code to follow a much more rigorous MVC architecture.

My question is one concerning python directly. I don’t know the words to say exactly what I want to build, so I would just like to use an example.

This is my current “basic” code for displaying a view:

class MainHandler(webapp.RequestHandler):
    def get(self):
        tUser = users.get_current_user()
        tContext = {
            'user':      tUser,
            'login':     users.create_login_url(self.request.uri),
            'logout':    users.create_logout_url(self.request.uri),
        }

        #User is logged in
        if (tUser):
            #code for loading view information and adding to the context dict
            #CUSTOMIZATION GOES HERE
        else: 
            self.redirect("/")

        tmpl = os.path.join(os.path.dirname(__file__), 'logged-in.html')
        self.response.out.write(render(tmpl, tContext))

I would like to take this boilerplate code and abstract it somehow, maybe with a way of prepending/appending the “customizable” code for each class method?

I think I might be able to use a decorator to do this somehow, but I have no python mentors outside of stackoverflow to point me in the right direction. I would prefer the most pythonic method possible, or at least what’s generally considered “best practices” in this situation.

The python version is 2.7.2.

edit

Note, if I can do this with decorators, then what is necessary for me to be able to call the decorator from an entirely different class and python file? I would like to be able put my decorators in one file and reference it from elsewhere so my code is as normalized as is reasonable. =)

edit 2

This is the testing code that I worked out in the console, and I have to leave for the evening or I would refine it more. However, it appears that this successfully accesses and modifies the class’s properties, which is pretty much what I think you need to pull this off in GAE.

class Decorators():
@staticmethod
def BeginInit(pInFunction):
    def OutFunction1(self):
        print str(self.innerv)
        pInFunction(self)
    return OutFunction1

@staticmethod
def EndInit(pInFunction):
    def OutFunction2(self):
        self.innerv = 5
        pInFunction(self)
        print "this is test 3"
    return OutFunction2

class Test2Handler():
    innerv = 10
    @Decorators.BeginInit
    @Decorators.EndInit
    def TestPrint(self):
        print self.innerv
        print "this is test 2"

Prints

10
5
this is test 2
this is test 3
  • 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-05-28T01:46:10+00:00Added an answer on May 28, 2026 at 1:46 am

    Instead of using decorators, you could use a base class for your request handlers, like so

    class HandlerBase(webapp.RequestHandler):
    
        def get_context(self):
            return {}
    
        def get(self):
            tUser = users.get_current_user()
            tContext = {
                'user':      tUser,
                'login':     users.create_login_url(self.request.uri),
                'logout':    users.create_logout_url(self.request.uri),
            }
            # tContext holds the defaults for your context
    
            #User is logged in
            if (tUser):
                # if the dict returned by self.get_context() contains a key
                # that's already in tContext, tContext[key] will be overridden
                # with self.get_context()[key]
                tContext.update(self.get_context())
            else: 
                self.redirect("/")
    
            tmpl = os.path.join(os.path.dirname(__file__), 'logged-in.html')
            self.response.out.write(render(tmpl, tContext))
    
    
    class MainHandler(HandlerBase):
    
        def get_context(self):
            # the contents of the following dict will be added to
            # the context in HandlerBase
            return {'greeting': 'Hi!'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.