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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:15:42+00:00 2026-05-18T02:15:42+00:00

I’m not sure if this is effective or not. It works, but sometimes i

  • 0

I’m not sure if this is effective or not. It works, but sometimes i feel…weird about it. Can you please tell me if this is a good way or not?

I threw the code on pastebin, because i think it’s a bit too much to put here: http://pastebin.com/662TiQLq

EDIT
I edited the title to make it more objective.

  • 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-18T02:15:43+00:00Added an answer on May 18, 2026 at 2:15 am

    I’m just guessing that the questioner is asking about creating a dictionary of functions in the __ init __ function of the handlers, and then using this dict in the “get” function to look up specific functions. If this is the question, then IMHO a clearer approach would be to set up separate handlers for each different function. For example

    class QuotesView(webapp.RequestHandler):
        """Super class for quotes that can accommodate common functionality"""
        pass
    
    class QuotesViewSingle(QuotesView):
        def get(self):
            ...
    
    class QuotesViewRandom(QuotesView):
        def get(self):
            ...
    
    class QuotesViewAll(QuotesView):
        def get(self):
            ...
    
    def main():
        application = webapp.WSGIApplication([('/quote/new',NewQuote),
                                              (r'/quotes/single',QuotesViewSingle),
                                              (r'/quotes/all',QuotesViewAll),
                                              (r'/quotes/random',QuotesViewRandom),
                                              ...
                                              ('/', MainHandler)],
                                             debug=True)
    

    BTW. A lot of people use the regex in the WSGIApplication calls to parse out arguments for the get functions. There’s nothing particularly wrong with it. I’m not a big fan of that feature, and prefer to parse the arguments in the get functions. But that’s just me.

    For completeness here’s the original code:

    class Quote(db.Model):
        author = db.StringProperty()
        string = db.StringProperty()
    
    
    class MainHandler(webapp.RequestHandler):
        def get(self):
            user = users.get_current_user()
    
    
            quotes = Quote.all()
            path = os.path.join(os.path.dirname(__file__),'quotery.html')
            template_values = {'quotes':quotes,'user':user,'login_url':users.create_login_url('/')}
            self.response.out.write(template.render(path, template_values))
    
    
    class QuoteHandler(webapp.RequestHandler):
    
        def __init__(self):
            self.actions = {'fetch':self.fetch, 'random':self.fetch_random}
    
            #Memcache the number of quotes in the datastore, to minimize datastore calls
            self.quote_count = memcache.get('quote_count')
            if not self.quote_count:
                self.quote_count = self.cache_quote_count()
    
        def cache_quote_count(self):
            count = Quote.all().count()
            memcache.add(key='quote_count', value=count, time=3600)
            return count
    
    
        def get(self, key):
            if key in self.actions:
                action = self.actions[key]
                action()
    
    
    
    
        def fetch(self):
            for quote in Quote.all():
                print 'Quote!'
                print 'Author: ',quote.author
                print 'String: ',quote.string
                print
    
    
        def fetch_random(self):
            max_offset = self.quote_count-1
            random_offset = random.randint(0,max_offset)
            '''self.response.out.write(max_offset)
            self.response.out.write('\n<br/>')
            self.response.out.write(random_offset)'''
            try:
                query = db.GqlQuery("SELECT * FROM Quote")
                quotes = query.fetch(1,random_offset)
                return quotes
                '''for quote in quotes:
                    self.response.out.write(quote.author)
                    self.response.out.write('\n')
                    self.response.out.write(quote.string)'''
            except BaseException:
                raise
    
    
    class NewQuote(webapp.RequestHandler):
    
        def post(self):
            author = self.request.get('quote_author')
            string = self.request.get('quote_string')
    
            if not author or not string:
                return False        
            quote = Quote()
            quote.author = author
            quote.string = string
            quote.put()
            QuoteHandler().cache_quote_count()
            self.redirect("/")
            #return True
    
    
    class QuotesView(webapp.RequestHandler):
    
        def __init__(self):
            self.actions = {'all':self.view_all,'random':self.view_random,'get':self.view_single}
    
        def get(self, key):
            if not key or key not in self.actions:
                self.view_all()
            if key in self.actions:
                action = self.actions[key]
                action()
    
        def view_all(self):
            print 'view all'
    
        def view_random(self):
            quotes = QuoteHandler().fetch_random()
            template_data = {}
    
            for quote in quotes:
                template_data['quote'] = quote
    
            template_path = os.path.join(os.path.dirname(__file__),'base_view.html')
            self.response.out.write(template.render(template_path, template_data))
    
    
        def view_single(self):
            print 'view single'
    
    
    def main():
        application = webapp.WSGIApplication([('/quote/new',NewQuote),(r'/quotes/(.*)',QuotesView),(r'/quote/(.*)',QuoteHandler),('/', MainHandler)],
                                             debug=True)
        util.run_wsgi_app(application)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
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
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.