Python 参数值的更改-web2py应用程序
我是web2py新手,正在尝试一个应用程序。有关相关的Python 参数值的更改-web2py应用程序,python,web2py,Python,Web2py,我是web2py新手,正在尝试一个应用程序。有关相关的控制器功能和相应的视图,请参见下文。我首先将浏览器指向http://127.0.0.1:8000/test/default/index之后,我将按照应用程序进行操作。我经历了一件对我来说很奇怪的事情:有时(不总是,大约5-6次中有一次)MC和maxWTP的值显示在http://127.0.0.1:8000/test/default/inandout第一次输入价格后,页面更改。我想做的是,当进入http://127.0.0.1:8000/tes
控制器
功能和相应的视图
,请参见下文。我首先将浏览器指向http://127.0.0.1:8000/test/default/index
之后,我将按照应用程序进行操作。我经历了一件对我来说很奇怪的事情:有时(不总是,大约5-6次中有一次)MC和maxWTP的值显示在http://127.0.0.1:8000/test/default/inandout
第一次输入价格后,页面更改。我想做的是,当进入http://127.0.0.1:8000/test/default/index
,这些用于创建marketobject;但在那之后就不行了。一旦页面被重定向到http://127.0.0.1:8000/test/default/inandout
我不明白为什么在输入价格后MC和maxWTP应该改变。而且,似乎只有在第一次输入价格后才会发生。有人能给我解释一下为什么会发生这种情况,以及如何纠正这种情况吗?
我使用的是Chrome浏览器以及web2py和python 2.7的1.99.7版(2012-03-04 22:12:08)
我认为原因不在于市场模块。但是如果你想看市场模块和其他模块,请让我知道
控制器功能
import random
import market
def index():
'''Creates a new market object and resets the price, quantity
and profit histories'''
session.marketobject = None
mc = random.choice([1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2])
maxWTP = random.choice([4,5,6,7])
session.marketobject = market.Market(mc,maxWTP)
session.priceHistory = []
session.profitHistory = []
session.quantityHistory = []
redirect(URL('inandout'), 303)
def second():
if request.vars.price:
price = float(request.vars.price)
# use the price to calculate profit and qsold
currentprice, qsold, profit = session.marketobject.calculate_qsold_profit(price)
# add the result to the history of prices, profits and qsold
session.priceHistory.append(currentprice)
session.profitHistory.append(profit)
session.quantityHistory.append(qsold)
redirect(URL('inandout'), 303)
def inandout():
capacity = session.marketobject.K
populationsize = session.marketobject.N
mc = session.marketobject.MC
maxWTP = session.marketobject.maxWTP
return dict(capacity=capacity, populationsize=populationsize,
mc=mc, maxWTP=maxWTP,
priceHistory=session.priceHistory,
profitHistory=session.profitHistory,
quantityHistory=session.quantityHistory)
#!/usr/bin/env python
# coding: utf8
import consumers
import business
class Market(object):
def __init__(self, MC, maxWTP, N=30000, minWTP=0,
FC=0, K=10000, numDecisions=3):
self.numDecisions = numDecisions
self.K = K
self.N = N
self.MC = MC
self.maxWTP = maxWTP
self.consumerClass = consumers.AllConsumers(N, minWTP, maxWTP)
self.seller = business.Seller(FC, MC, K)
def calculate_qsold_profit(self, price):
currentprice = price
qtydemanded = self.consumerClass.qtyDemanded(currentprice)
calculate_qsold_profit = \
self.seller.calculate_qsold_profit(currentprice,
qtydemanded)
qSold = calculate_qsold_profit[0]
profit = calculate_qsold_profit[1]
return (currentprice, qSold,profit,)
查看:inadout.html
<html>
<head>
<title>First page</title>
<link rel="stylesheet" href="{{=URL('static', 'css/testapp.css')}}">
</head>
<body>
<h2>Seller capacity: {{=capacity}}</h2>
<h2>Population size: {{=populationsize}}</h2>
<h2>MC: {{=mc}}</h2>
<h2>Maximum WTP: {{=maxWTP}}</h2>
<form action="second">
Enter price: <input type="text" name="price">
<input type="submit">
</form>
<table>
{{for count in range(len(priceHistory)):}}
<tr>
<td>{{=priceHistory[count]}}</td>
<td>{{=quantityHistory[count]}}</td>
<td>{{=profitHistory[count]}}</td>
</tr>
{{pass}}
</table>
</body>
</html>
consumers.py
#!/usr/bin/env python
# coding: utf8
import random
##=====================================================================
# Create the class that creates all consumers
##=====================================================================
class AllConsumers(object):
def __init__(self, N, minWTP, maxWTP):
self.N = N
self.minWTP = minWTP
self.maxWTP = maxWTP
self.listOfWTPs = sorted([random.uniform(minWTP,maxWTP) \
for x in range(N)], reverse=True)
def qtyDemanded(self, price):
count = 0
for wtp in self.listOfWTPs:
if wtp >= price:
count += 1
else:
break
return count
#!/usr/bin/env python
# coding: utf8
##=====================================================================
# Create a seller class
##=====================================================================
class Seller(object):
def __init__(self, FC, MC, K):
self.FC = FC
self.MC = MC
self.K = K
def calculate_qsold_profit(self, price, qtydemanded):
# choose minimum of quantity demanded and capacity
# create a vector of monthly capacity (same element repeated 12 times)
qsold = min(qtydemanded,self.K)
# Calculate the profit vector
profit = ((price - self.MC) * qsold) - self.FC
return (qsold, profit)
business.py
#!/usr/bin/env python
# coding: utf8
import random
##=====================================================================
# Create the class that creates all consumers
##=====================================================================
class AllConsumers(object):
def __init__(self, N, minWTP, maxWTP):
self.N = N
self.minWTP = minWTP
self.maxWTP = maxWTP
self.listOfWTPs = sorted([random.uniform(minWTP,maxWTP) \
for x in range(N)], reverse=True)
def qtyDemanded(self, price):
count = 0
for wtp in self.listOfWTPs:
if wtp >= price:
count += 1
else:
break
return count
#!/usr/bin/env python
# coding: utf8
##=====================================================================
# Create a seller class
##=====================================================================
class Seller(object):
def __init__(self, FC, MC, K):
self.FC = FC
self.MC = MC
self.K = K
def calculate_qsold_profit(self, price, qtydemanded):
# choose minimum of quantity demanded and capacity
# create a vector of monthly capacity (same element repeated 12 times)
qsold = min(qtydemanded,self.K)
# Calculate the profit vector
profit = ((price - self.MC) * qsold) - self.FC
return (qsold, profit)
我们能看看市场模块吗?您还可以打包整个应用程序(或者至少是复制问题的最小版本),并将其发布到上。@Anthony谢谢!我已经添加了所有模块。请让我知道,如果你仍然想让我包装的应用程序,并张贴在谷歌集团。我不确定,但问题似乎发生在Chrome上,而不是其他浏览器上(但我可能错了,因为它不是每次都发生)。要尝试在Chrome中复制它,当首次加载
inandout
页面时,快速输入价格并提交可能会有所帮助。再次感谢。我无法在Ubuntu或Windows7的Chrome浏览器中重现这个问题,也无法在Windows7的IE浏览器中重现。我能够重现这种行为的唯一方法是首先在一个选项卡中转到索引URL,然后在第二个选项卡中转到索引URL,然后在第一个选项卡中提交价格。在这种情况下,对索引的第二次访问会导致在会话中生成MC和maxWTP的新值,因此,当您在第一个选项卡中提交价格时,返回的页面具有新生成的MC和maxWTP值。注意,同一浏览器中的所有选项卡共享同一会话,因此,由一个选项卡启动的对会话变量的更改将传播到所有其他选项卡。谢谢,Anthony!我想这可能很难复制。好吧,我现在将使用Safari,因为我在Safari中没有这个问题。