我正在尝试创建一个语法友好的库,该库不需要用户不断传递凭据或键入冗余或过多的语句。我的策略是专注于我打算如何看待库语法,然后设计库以这种方式运行。我知道我可以实现以下目标:
api = Api(user="admin", pswd="blahblah")
new_order = api.Order()
new_order.status = "pending"
api.create(new_order)
api.order_assign_user(new_order, user)
existing_order = api.get_order(orderId=12345)
existing_order.status = "shipped"
api.update(existing_order)
使用如下所示的内容:
class Api(object):
def __init__(self, user=None, pswd=None):
self.user = user
self.pswd = pswd
class Order(api):
def __init__ (self, status=None):
self.status = status
def create(self, x):
auth = Auth(self.user, self.pswd)
# use authorization credentials to create data remotely
return x
def update(self, x):
auth = Auth(self.user, self.pswd)
# use authorization credentials to update data from remote
return x
def get_order(self, orderId=None):
auth = Auth(self.user, self.pswd)
# use authorization credentials to update data from remote
return order
但是我希望能够使用以下语句:
new_order.create() # instead of api.create(new_order)
new_order.assign_user(user) # instead of api.order_assign_user(new_order, user)
existing_order = api.Order.get(orderId=12345) # returns retrieved Order Instance
如何获取Order()
实例以访问创建它的Api()
实例的属性?如果无法访问这些属性,则无法访问“ user”和“ pswd”之类的任何属性(requests.get()调用需要它们)
我已经尝试过使用各种函数和类来实现此目的,但永远无法解决此问题。这是我所能达到的最接近的结果:
class Api(object):
def __init__(self, user=None, pswd=None):
self.user = user
self.pswd = pswd
class Order(api):
def __init__ (self, status=None):
self.status = status
@classmethod
def get(cls, value):
return cls(status=value)
def create(self):
auth = Auth(self.user, self.pswd)
# these credentials need to come from the Api() instance?, not self
return x
def update(self):
auth = Auth(self.user, self.pswd)
# these credentials need to come from the Api() instance?, not self
return x
这可能吗?还是我走错路了?我已经考虑过将其放入模块中,但这似乎也不是一个有效的选择。
答案 0 :(得分:1)
Python中的内部类只是单独的类,并且它们的实例不会以任何方式自动与外部类的实例相关联。
我建议一个解决方案,其中Order
的构造函数将一个Api
实例作为参数并使用该实例:
class Api(object):
def __init__(self, user=None, pswd=None):
self.user = user
self.pswd = pswd
class Order(object):
def __init__(self, api, status=None):
self.api = api
self.status = status
@classmethod
def get(cls, api, value):
return cls(api, status=value)
def create(self):
auth = Auth(self.api.user, self.api.pswd)
return x
def update(self):
auth = Auth(self.api.user, self.api.pswd)
return x
答案 1 :(得分:1)
这证明了我想做的事情的基本实现,我发现它比我在评论您的问题时想到的__get__
,__set__
黑客技术要好
import requests
class Order:
def __init__(self, user:str, passwd: str):
self.session = requests.Session()
self.session.auth = (user, passwd)
self.status = None
# This is only to make the class callable, since the class is
# already instantiated in Api init calling it again would raise an exception
def __call__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
return self
def get(self):
response = self.session.get('https://httpbin.org/basic-auth/my_user/my_pass')
print(response.text)
def post(self):
response = self.session.post('https://httpbin.org/post', data={'status': self.status})
print(response.text)
class Api:
def __init__(self, user: str, passwd: str):
self.Order = Order(user, passwd)