使用python成型LP

时间:2018-11-08 23:42:41

标签: python

热狗除其他外还制造热狗。其 给出了全年热狗需求预测。一条新鲜的热狗售价为3.29美元。一天大的热狗售价为99分。老了 必须扔掉。他们每天可以制作50条热狗。

我知道这个问题可能在这里允许,但是我需要使用python解决。

1 个答案:

答案 0 :(得分:1)

由于最初的问题中缺少一些细节,所以我在设计笨拙的管式肉类自动售货机脚本时获得了一些自由。

我对这里的信号处理特别不满意。我显然错过了有关signal.signal(...)signal.sigwait(...)交互方式的一些知识,因此我可能需要将其移交给代码审查以弄清楚。

SIGALRM导致进货,SIGIO被解释为客户要求的一份新鲜加油站美食神秘物品。请注意,使用信号处理可能会阻止它在Windows环境中正常工作或无法按预期工作。它是为Python 3.6编写的。

import os
import sys
import signal
import logging
from random import choice
from decimal import Decimal
from datetime import date, datetime


FLAVOR_QUESTIONABLE = "pungent"
FLAVOR_REPUGNANT = "standard"
FLAVOR_ACCEPTABLE = "unicorn"
FLAVOR_PROFILES = (
    FLAVOR_QUESTIONABLE,
    FLAVOR_REPUGNANT,
    FLAVOR_ACCEPTABLE,
)

PRICE_STANDARD = Decimal("3.29")
PRICE_AGED = Decimal("0.99")

TORPEDO_DAILY_PRODUCTION_RATE = 50


logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.handlers = (
    logging.FileHandler("vend.out", "w"),
    logging.StreamHandler(sys.stdout),
)


class SmeatTorpedo():
    start_timestamp = None
    flavor_profile = None

    def __init__(self):
        self.start_timestamp = datetime.now()
        self.flavor_profile = choice(FLAVOR_PROFILES)

    @property
    def price(self):
        if (datetime.now() - self.start_timestamp).days >= 1:
            return PRICE_AGED
        return PRICE_STANDARD

    def is_rancid(self):
        return (datetime.now() - self.start_timestamp).days >= 2

    def __str__(self):
        return "{f}: ${p} ({a})".format(
            f=self.flavor_profile,
            p=self.price,
            a=datetime.now() - self.start_timestamp,
        )


class SmeatTorpedoVendor():
    revenue = Decimal("0")
    torpedoes = None
    current_date = None
    last_production_date = None

    def __init__(self):
        self.torpedoes = []
        self.current_date = date.today()
        self.restock()

    def restock(self):
        self.torpedoes.extend(
            SmeatTorpedo() for _ in range(TORPEDO_DAILY_PRODUCTION_RATE)
        )
        self.last_production_date = date.today()
        logger.info("Current inventory: {c}".format(c=len(self.torpedoes)))

    def sell(self):
        while len(self.torpedoes):
            torpedo = self.torpedoes.pop(0)
            if not torpedo.is_rancid():
                logger.info("Sold!")
                self.revenue += torpedo.price
                return

    def retire(self):
        logger.info("Total revenue: ${r}".format(r=self.revenue))

    def vend(self):
        logger.info("Selling questionable sustenance at PID {p}".format(p=os.getpid()))

        received = None

        signal.signal(signal.SIGIO, lambda: None)
        signal.signal(signal.SIGALRM, lambda: None)
        signal.signal(signal.SIGINT, lambda: None)

        while received != signal.SIGINT:
            if received == signal.SIGIO:
                self.sell()
            elif received == signal.SIGALRM:
                self.restock()

            received = signal.sigwait((
                signal.SIGIO,
                signal.SIGALRM,
                signal.SIGINT,
            ))

        self.retire()


SmeatTorpedoVendor().vend()

这是找到正在运行的脚本的PID后在单独的终端中运行的命令的列表。请注意,我包括了不让一天过去的补货,这也许不应该被允许,但是如果您发现此代码有用,我相信强制执行将是一个值得的练习。

# kill -SIGIO 16650
# kill -SIGIO 16650
# kill -SIGIO 16657
# kill -SIGIO 16657
# kill -SIGIO 16657
# kill -SIGALRM 16657
# kill -SIGIO 16657
# kill -SIGIO 16657
# kill -SIGIO 16657
# kill -SIGIO 16657
# kill -SIGIO 16657
# kill -SIGIO 16657
# kill -SIGINT 16657

这是程序的完整日志记录输出。

# Current inventory: 50
# Selling questionable sustenance at PID 16657
# Sold!
# Sold!
# Sold!
# Current inventory: 97
# Sold!
# Sold!
# Sold!
# Sold!
# Sold!
# Sold!
# Total revenue: $29.61

如您所见,这不是一个完整的实现,因为它没有预测明年的热狗需求。我必须道歉,因为这超出了我的技能水平。

祝你好运!