gpt4 book ai didi

python - 这是在 App Engine/Python 中执行/处理 url 重写的正确方法吗?

转载 作者:行者123 更新时间:2023-12-01 06:14:18 25 4
gpt4 key购买 nike

我不确定这是否有效。它有效,但有时我觉得……很奇怪。您能告诉我这是一个好方法吗?

我把代码放在pastebin上,因为我认为放在这里有点太多了:http://pastebin.com/662TiQLq

编辑我编辑了标题以使其更加客观。

最佳答案

我只是猜测提问者正在询问如何在处理程序的 __ init __ 函数中创建函数字典,然后在“get”函数中使用此字典来查找特定函数。如果这是问题所在,那么恕我直言,更清晰的方法是为每个不同的函数设置单独的处理程序。例如

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)
顺便说一句。很多人在 WSGIApplication 调用中使用正则表达式来解析 get 函数的参数。没有什么特别不好的地方。我不太喜欢该功能,并且更喜欢解析 get 函数中的参数。但这只是我。

为了完整起见,这里是原始代码:

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)

关于python - 这是在 App Engine/Python 中执行/处理 url 重写的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4117051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com