我正在写一个表示某些Web API的Python库。现在,我的库目录看起来与此相似:
__init__.pyAccount.pyOrder.pyCategory.pyrequests.py在中__init__.py,我有类似以下内容:
from .Account import Account
from .Order import Order
from .Category import Category
from . import requests
Run Code Online (Sandbox Code Playgroud)
这允许先使用import cool_site然后再cool_site.Account(…)进行诸如此类的操作,但是存在以下问题:当我在IDLE中处理代码时,该对象随后被称为cool_site.Account.Account,这感觉很不好。
接下来我感到不满意的是我的代码组织。现在,我的Account班级接受初始化凭证,创建一个requests.Session对象,然后处理与服务器的所有通信,即搜索订单等。Account然后,此类实例将自身传递给所有其他实例,例如传递给Order-,因此订单的实例将具有.account保存Account创建它的实例的属性。当另一个类实例本身必须执行某项操作时(例如,更改订单的注释)(通过调用o.comment = 'new comment',由类中的@comment.setter装饰器进行更改Order),它将其转发给一个Account对象,该对象在初始化时传递给该对象,然后使用example self.account.set_order_comment(new_comment)。然后,此方法将使用所有Web请求来实现该目标。
我想问的最后一件事是如何以及在哪里保留低级请求模板。现在我在cool_site.requests子模块中有它,针对不同的请求有不同的功能,例如SetOrderComment上述情况(这是一个功能,因此应该小写,但是在这种情况下,我认为它在某种程度上类似于类-是可以吗?)。该Account.set_order_comment会使用这样的:
r = cool_site.requests.SetOrderComment(order.id, new_comment)
response = self._session.request(**r)
Run Code Online (Sandbox Code Playgroud)
因为此函数会返回一个带有参数的字典,以Session.request从requests库中获取函数。身份验证标头已在类实例的_session …
这是我的源代码:
#include <iostream>
#include <cmath>
using namespace std;
double up = 19.0 + (61.0/125.0);
double down = -32.0 - (2.0/3.0);
double rectangle = (up - down) * 8.0;
double f(double x) {
return (pow(x, 4.0)/500.0) - (pow(x, 2.0)/200.0) - 0.012;
}
double g(double x) {
return -(pow(x, 3.0)/30.0) + (x/20.0) + (1.0/6.0);
}
double area_upper(double x, double step) {
return (((up - f(x)) + (up - f(x + step))) * step) / 2.0;
}
double area_lower(double x, double step) { …Run Code Online (Sandbox Code Playgroud)