参数值的变化 - web2py应用程序

时间:2012-05-01 02:18:20

标签: python web2py

我是web2py的新手并试用了一个应用程序。请参阅下面的相关controller函数和相应的相关views。我首先将浏览器指向http://127.0.0.1:8000/test/default/index,之后我会关注该应用程序。我体验过,对我来说很奇怪:有时候(并非总是,大约一次5-6次)http://127.0.0.1:8000/test/default/inandout页面上显示的MC和maxWTP的值在第一次价格后更改进入。我想要做的是当一个人进入http://127.0.0.1:8000/test/default/index时,应该随机选择MC和maxWTP,这些用于创建市场对象;但不是那之后。页面重定向到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)

查看:inandout.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>

市场,消费者和业务模块

#!/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,)

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

business.py

#!/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)

0 个答案:

没有答案