Repository: quantopian/zipline
File: zipline/finance/transaction.py (Python)

Code before:
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division

from copy import copy

from zipline.assets import Asset
from zipline.protocol import DATASOURCE_TYPE


class Transaction(object):

    def __init__(self, sid, amount, dt, price, order_id, commission=None):
        assert isinstance(sid, Asset)

        self.sid = sid
        self.amount = amount
        self.dt = dt
        self.price = price
        self.order_id = order_id
        self.commission = commission
        self.type = DATASOURCE_TYPE.TRANSACTION

    def __getitem__(self, name):
        return self.__dict__[name]

    def __repr__(self):
        return """{typename}(sid={sid}, \
dt={dt}, amount={amount}, price={price})""".format(
            typename=type(self).__name__,
            sid=self.sid,
            dt=self.dt,
            amount=self.amount,
            price=self.price
        )

    def to_dict(self):
        py = copy(self.__dict__)
        del py['type']
        return py


def create_transaction(order, dt, price, amount):

    # floor the amount to protect against non-whole number orders
    # TODO: Investigate whether we can add a robust check in blotter
    # and/or tradesimulation, as well.
    amount_magnitude = int(abs(amount))

    if amount_magnitude < 1:
        raise Exception("Transaction magnitude must be at least 1.")

Review comment (bug):
Very minor style point: this might be slightly nicer to read broken out over multiple lines:

```python
    def __repr__(self):
        template = "{cls}(sid={sid}, dt={dt}, amount={amount}, price={price})"
        return template.format(
            cls=type(self).__name__,
            sid=self.sid,
            dt=self.dt,
            amount=self.amount,
            price=self.price
        )
```

Code after:
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division

from copy import copy

from zipline.assets import Asset
from zipline.protocol import DATASOURCE_TYPE
from zipline.utils.input_validation import expect_types


class Transaction(object):
    @expect_types(asset=Asset)
    def __init__(self, asset, amount, dt, price, order_id, commission=None):
        self.asset = asset
        self.amount = amount
        self.dt = dt
        self.price = price
        self.order_id = order_id
        self.commission = commission
        self.type = DATASOURCE_TYPE.TRANSACTION

    def __getitem__(self, name):
        return self.__dict__[name]

    def __repr__(self):
        template = (
            "{cls}(asset={asset}, dt={dt},"
            " amount={amount}, price={price})"
        )

        return template.format(
            cls=type(self).__name__,
            asset=self.asset,
            dt=self.dt,
            amount=self.amount,
            price=self.price
        )

    def to_dict(self):
        py = copy(self.__dict__)
        del py['type']
        del py['asset']

        # Adding 'sid' for backwards compatibility with downstrean consumers.
        py['sid'] = self.asset

        return py


def create_transaction(order, dt, price, amount):