1

我是 web2py 的新手,正在尝试一个应用程序。controller相关功能及对应相关请见下文views。我首先将我的浏览器指向http://127.0.0.1:8000/test/default/index,然后我关注应用程序。我经历过,对我来说似乎很奇怪的事情:有时(不总是,大约每 5-6 次)页面上显示的 MC 和 maxWTP 的值在第一次输入价格http://127.0.0.1:8000/test/default/inandout会发生变化。我想要做的是当一个人去的时候应该随机选择 MC 和 maxWTP ,这些被用来创建一个市场对象;但不是在那之后。一旦页面被重定向到http://127.0.0.1:8000/test/default/indexhttp://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,)

消费者.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

业务.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)
4

0 回答 0