Simplify the common code a bit

This commit is contained in:
Sylvain Bellemare 2016-11-04 16:04:53 +01:00 committed by Sylvain Bellemare
parent 381ffca4e5
commit 3b9f6801a8

View File

@ -38,17 +38,15 @@ class Fulfillment(object):
TransactionLink`, optional): A link representing the input TransactionLink`, optional): A link representing the input
of a `TRANSFER` Transaction. of a `TRANSFER` Transaction.
""" """
self.fulfillment = fulfillment
if tx_input is not None and not isinstance(tx_input, TransactionLink): if tx_input is not None and not isinstance(tx_input, TransactionLink):
raise TypeError('`tx_input` must be a TransactionLink instance') raise TypeError('`tx_input` must be a TransactionLink instance')
else:
self.tx_input = tx_input
if not isinstance(owners_before, list): if not isinstance(owners_before, list):
raise TypeError('`owners_after` must be a list instance') raise TypeError('`owners_after` must be a list instance')
else:
self.owners_before = owners_before self.fulfillment = fulfillment
self.tx_input = tx_input
self.owners_before = owners_before
def __eq__(self, other): def __eq__(self, other):
# TODO: If `other !== Fulfillment` return `False` # TODO: If `other !== Fulfillment` return `False`
@ -216,14 +214,13 @@ class Condition(object):
Raises: Raises:
TypeError: if `owners_after` is not instance of `list`. TypeError: if `owners_after` is not instance of `list`.
""" """
if not isinstance(owners_after, list) and owners_after is not None:
raise TypeError('`owners_after` must be a list instance or None')
self.fulfillment = fulfillment self.fulfillment = fulfillment
# TODO: Not sure if we should validate for value here # TODO: Not sure if we should validate for value here
self.amount = amount self.amount = amount
self.owners_after = owners_after
if not isinstance(owners_after, list) and owners_after is not None:
raise TypeError('`owners_after` must be a list instance or None')
else:
self.owners_after = owners_after
def __eq__(self, other): def __eq__(self, other):
# TODO: If `other !== Condition` return `False` # TODO: If `other !== Condition` return `False`
@ -493,16 +490,12 @@ class Metadata(object):
data_id (str): A hash corresponding to the contents of data_id (str): A hash corresponding to the contents of
`data`. `data`.
""" """
# TODO: Rename `payload_id` to `id`
if data_id is not None:
self.data_id = data_id
else:
self.data_id = self.to_hash()
if data is not None and not isinstance(data, dict): if data is not None and not isinstance(data, dict):
raise TypeError('`data` must be a dict instance or None') raise TypeError('`data` must be a dict instance or None')
else:
self.data = data # TODO: Rename `payload_id` to `id`
self.data_id = data_id if data_id is not None else self.to_hash()
self.data = data
def __eq__(self, other): def __eq__(self, other):
# TODO: If `other !== Data` return `False` # TODO: If `other !== Data` return `False`
@ -592,51 +585,32 @@ class Transaction(object):
version (int): Defines the version number of a Transaction. version (int): Defines the version number of a Transaction.
""" """
if version is not None:
self.version = version
else:
self.version = self.__class__.VERSION
if timestamp is not None:
self.timestamp = timestamp
else:
self.timestamp = gen_timestamp()
if operation not in Transaction.ALLOWED_OPERATIONS: if operation not in Transaction.ALLOWED_OPERATIONS:
allowed_ops = ', '.join(self.__class__.ALLOWED_OPERATIONS) allowed_ops = ', '.join(self.__class__.ALLOWED_OPERATIONS)
raise ValueError('`operation` must be one of {}' raise ValueError('`operation` must be one of {}'
.format(allowed_ops)) .format(allowed_ops))
else:
self.operation = operation
# If an asset is not defined in a `CREATE` transaction, create a # Only assets for 'CREATE' operations can be un-defined.
# default one. if (asset and not isinstance(asset, Asset) or
if asset is None and operation == Transaction.CREATE: not asset and operation != Transaction.CREATE):
asset = Asset()
if not isinstance(asset, Asset):
raise TypeError('`asset` must be an Asset instance') raise TypeError('`asset` must be an Asset instance')
else:
self.asset = asset
if conditions is not None and not isinstance(conditions, list): if conditions and not isinstance(conditions, list):
raise TypeError('`conditions` must be a list instance or None') raise TypeError('`conditions` must be a list instance or None')
elif conditions is None:
self.conditions = []
else:
self.conditions = conditions
if fulfillments is not None and not isinstance(fulfillments, list): if fulfillments and not isinstance(fulfillments, list):
raise TypeError('`fulfillments` must be a list instance or None') raise TypeError('`fulfillments` must be a list instance or None')
elif fulfillments is None:
self.fulfillments = []
else:
self.fulfillments = fulfillments
if metadata is not None and not isinstance(metadata, Metadata): if metadata is not None and not isinstance(metadata, Metadata):
raise TypeError('`metadata` must be a Metadata instance or None') raise TypeError('`metadata` must be a Metadata instance or None')
else:
self.metadata = metadata self.version = version if version is not None else self.VERSION
self.timestamp = timestamp if timestamp else gen_timestamp()
self.operation = operation
self.asset = asset if asset else Asset()
self.conditions = conditions if conditions else []
self.fulfillments = fulfillments if fulfillments else []
self.metadata = metadata
@classmethod @classmethod
def create(cls, owners_before, owners_after, metadata=None, asset=None, def create(cls, owners_before, owners_after, metadata=None, asset=None,