我有 3 個課程,如下所述 -
AWS客戶端
此類負責為應用程式中使用的 AWS 資源創建客戶端。
import boto3
class AWSClient:
def __init__(self):
self.region_name = "ap-south-1"
def get_dynamo_client(self):
try:
self.dynamo_client = boto3.resource("dynamodb", region_name=self.region_name)
except BaseException as e:
raise Exception("Some error!")
return self.dynamo_client
def get_ses_client(self):
return True
發電機表
class DynamoTable(AWSClient):
"""
This method will use attributes of AWSClients to create dynamo table instance.
"""
def __init__(self):
# Now we can access the attributes of AWSClients' __init__() method.
AWSClients.__init__(self)
def get_admin_table(self):
"""
This method will create the client for table Admins.
"""
try:
table = super(
DynamoTable, self).get_dynamo_client().Table("Admins")
except ClientError as e:
raise e
基礎模型
class BaseModel(DynamoTable):
"""
This class will act as a base for all developers while creating models.
"""
def __init__(self):
# Now we can access the attributes of DynamoTable's __init__() method.
# Additionally, attributes of AWSClient can also be accessed as DynamoTable
# is inheriting AWSClient.
DynamoTable.__init__(self)
上述類之間的關系是 -
AWSClient是一個超類的DynamoTable和DynamoTable是一個超類的BaseModel。
現在繼承BaseModel其他開發人員可能會創建他們自己的模型類。但是,在某些情況下,開發人員的模型類可能希望獲得其他 AWS 服務的客戶端,例如get_ses_client(),可以通過呼叫 class 的特定方法來完成AWSClient()。
這是我迄今為止嘗試過的-
class InviteAdminsModel(BaseModel, AWSClient):
"""
This is a sample model for Admins table.
This must be in the helper module of each API.
All the API specific interation with dynamo should be written in the methods of this class.
"""
def __init__(self):
"""
This method will have all the clients of dynamo table initialized.
"""
BaseModel.__init__(self)
self.admin_table = super(BaseModel, self).get_admin_table()
self.ses_client = super(BaseModel, self).get_ses_client()
在上面的實作中,我能夠獲得 的客戶端,admin_table但我無法呼叫get_ses_client()屬于AWSClient(). 它給我一個錯誤super(type, obj): obj must be an instance or subtype of type。
這是正確的實作InviteAdminsModel()嗎?
如何呼叫 的方法AWSClient()?
應該BaseModel()是抽象類嗎?
uj5u.com熱心網友回復:
讓我指出你犯的幾個錯誤。
class DynamoTable(AWSClients):
我看到有一個錯字,一定AWSClient不是AWSClients
AWSClient是的子類DynamoTable和DynamoTable是的子類BaseModel。
不,它是超類而不是子類
AWSClient是的超類DynamoTable和DynamoTable是一個超類BaseModel。
而在你的InvoteAdminsModel, 你不需要使用多重繼承謊言
class InviteAdminsModel(BaseModel, AWSClient):
As,AWSClient已經是 的父級MaseModel。只需使用
class InviteAdminsModel(BaseModel):
通過這些修改,它應該可以正常作業。我希望你理解這個問題,以及它是如何解決的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/330990.html
下一篇:“處理程式‘lambda_handler’在模塊‘lambda_function’上丟失”,“errorType”:“Runtime.HandlerNotFound”[ERROR]NameError:
