pip笔记

57 阅读1分钟

pip指南

pip笔记

pip导出依赖

用pip list --format=freeze
pip list --format=freeze > requirements.txt

项目1依赖

python 版本: 3.8.18

apache-beam==2.43.0
apache-flink==1.17.0
apache-flink-libraries==1.17.0
avro-python3==1.9.2.1
business-rules==1.1.1
certifi==2023.11.17
charset-normalizer==3.3.2
cloudpickle==2.2.0
crcmod==1.7
dill==0.3.1.1
docopt==0.6.2
fastavro==1.4.7
fasteners==0.19
grpcio==1.60.0
hdfs==2.7.3
httplib2==0.20.4
idna==3.6
kafka-python==2.0.2
mysql-connector==2.2.9
numpy==1.21.6
objsize==0.5.2
orjson==3.9.10
paho-mqtt==1.6.1
pandas==1.3.5
pip==23.3.1
proto-plus==1.23.0
protobuf==3.20.3
py4j==0.10.9.7
pyarrow==8.0.0
pydot==1.4.2
pymongo==3.13.0
pyparsing==3.1.1
python-dateutil==2.8.2
python-dotenv==1.0.0
pytz==2023.3.post1
PyYAML==6.0.1
regex==2023.12.25
requests==2.31.0
setuptools==68.2.2
six==1.16.0
typing_extensions==4.9.0
urllib3==2.1.0
wheel==0.41.2
zstandard==0.22.0

business_rules案例

business_rules案例1

import datetime

from business_rules import export_rule_data, run_all
from business_rules.variables import BaseVariables, string_rule_variable, numeric_rule_variable
from business_rules.actions import BaseActions, rule_action
from business_rules.fields import FIELD_NUMERIC
class Order:
    def __init__(self, product_id: int, quantity: int, expiration_date: datetime.datetime = datetime.datetime.now()):
        self.product_id = product_id
        self.quantity = quantity
        self.expiration_date = expiration_date

    def __str__(self):
        return f'[product_id:{self.product_id}, quantity: {self.quantity}, expiration_date: {self.expiration_date}]'


class Product:
    def __init__(self, product_id, current_inventory, orders, stock_state, price):
        self.product_id = product_id
        self.current_inventory = current_inventory
        self.orders = orders
        self.stock_state = stock_state
        self.price = price

    def __str__(self):
        return f'[orders: {self.orders.__str__()}, product_id:{self.product_id}, ' \
               f'current_inventory:{self.current_inventory}, stock_state: {self.stock_state}, price: {self.price}]'


class ProductVariables(BaseVariables):

    def __init__(self, product):
        self.product = product

    @numeric_rule_variable
    def current_inventory(self):
        return self.product.current_inventory

    @numeric_rule_variable(label='Days until expiration')
    def expiration_days(self):
        last_order = self.product.orders[-1]
        return (last_order.expiration_date - datetime.datetime.now()).days

    @string_rule_variable()
    def current_month(self):
        return datetime.datetime.now().strftime("%B")


class ProductActions(BaseActions):

    def __init__(self, product):
        self.product = product

    @rule_action(params={"sale_percentage": FIELD_NUMERIC})
    def put_on_sale(self, sale_percentage):
        self.product.price = (1.0 - sale_percentage) * self.product.price

    @rule_action(params={"number_to_order": FIELD_NUMERIC})
    def order_more(self, number_to_order):
        o1 = Order(product_id=self.product.product_id, quantity=number_to_order)
        self.product.orders.append(o1)


rules = [
    # expiration_days < 5 AND current_inventory > 20
    {"conditions": {"all": [
        {"name": "expiration_days",
         "operator": "less_than",
         "value": 5,
         },
        {"name": "current_inventory",
         "operator": "greater_than",
         "value": 20,
         },
    ]},
        "actions": [
            {"name": "put_on_sale",
             "params": {"sale_percentage": 0.25},
             },
        ],
    },

    # current_inventory < 5 OR (current_month = "December" AND current_inventory < 20)
    {"conditions": {"any": [
        {"name": "current_inventory",
         "operator": "less_than",
         "value": 5,
         },
        {"all": [
            {"name": "current_month",
             "operator": "equal_to",
             "value": "December",
             },
            {"name": "current_inventory",
             "operator": "less_than",
             "value": 20,
             }
        ]
        }
    ]},
        "actions": [
            {"name": "order_more",
             "params": {"number_to_order": 40},
             },
        ],
    }]


if __name__ == "__main__":
    export_rule_data(ProductVariables, ProductActions)

    p1 = Product(product_id=1,
                 current_inventory=3,
                 orders=[Order(product_id=1, quantity=10), Order(product_id=1, quantity=100)],
                 stock_state=10,
                 price=10)
    print(p1)
    b = run_all(rule_list=rules, defined_variables=ProductVariables(p1), defined_actions=ProductActions(p1),
                stop_on_first_trigger=True)
    print(b)
    print(p1)