Remove all namedicts.

This commit is contained in:
Stephen Diehl
2012-05-14 11:35:43 -04:00
parent f8401dc88e
commit e04415e63f
13 changed files with 58 additions and 60 deletions
+2 -2
View File
@@ -17,7 +17,7 @@ from zipline.finance.trading import TradingEnvironment
from zipline.simulator import AddressAllocator
from zipline.lines import SimulatedTrading
from zipline.finance.performance import PerformanceTracker
from zipline.utils.protocol_utils import namedict
from zipline.utils.protocol_utils import ndict
from zipline.finance.trading import TransactionSimulator, SIMULATION_STYLE
DEFAULT_TIMEOUT = 15 # seconds
@@ -434,7 +434,7 @@ class FinanceTestCase(TestCase):
order_date = start_date
for i in xrange(order_count):
order = namedict(
order = ndict(
{
'sid' : sid,
'amount' : order_amount * alternator**i,
+1 -1
View File
@@ -1,4 +1,4 @@
from zipline.utils.protocol_utils import ndict, namedict
from zipline.utils.protocol_utils import ndict
def test_ndict():
nd = ndict({})
+1 -1
View File
@@ -547,7 +547,7 @@ shares in position"
#create a transaction for all but
#first trade in each sid, to simulate None transaction
if(event.dt != self.trading_environment.period_start):
txn = zp.namedict({
txn = zp.ndict({
'sid' : event.sid,
'amount' : -25,
'dt' : event.dt,
+5 -5
View File
@@ -45,7 +45,7 @@ class ProtocolTestCase(TestCase):
for trade in trades:
#simulate data source sending frame
msg = zp.DATASOURCE_FRAME(zp.namedict(trade))
msg = zp.DATASOURCE_FRAME(zp.ndict(trade))
#feed unpacking frame
recovered_trade = zp.DATASOURCE_UNFRAME(msg)
#feed sending frame
@@ -74,13 +74,13 @@ class ProtocolTestCase(TestCase):
self.assertTrue(event.helloworld == 2345.6)
event.delete('helloworld')
self.assertEqual(zp.namedict(trade), event)
self.assertEqual(zp.ndict(trade), event)
@timed(DEFAULT_TIMEOUT)
def test_order_protocol(self):
#client places an order
now = datetime.utcnow().replace(tzinfo=pytz.utc)
order = zp.namedict({
order = zp.ndict({
'dt':now,
'sid':133,
'amount':100
@@ -94,7 +94,7 @@ class ProtocolTestCase(TestCase):
self.assertEqual(order.dt, now)
#order datasource datasource frames the order
order_event = zp.namedict({
order_event = zp.ndict({
"sid" : order.sid,
"amount" : order.amount,
"dt" : order.dt,
@@ -111,7 +111,7 @@ class ProtocolTestCase(TestCase):
self.assertEqual(now, recovered_order.dt)
#create a transaction from the order
txn = zp.namedict({
txn = zp.ndict({
'sid' : recovered_order.sid,
'amount' : recovered_order.amount,
'dt' : recovered_order.dt,
+1 -2
View File
@@ -9,13 +9,12 @@ import protocol # namespace
from core.monitor import Controller
from lines import SimulatedTrading
from core.host import ComponentHost
from utils.protocol_utils import namedict, ndict
from utils.protocol_utils import ndict
__all__ = [
SimulatedTrading,
Controller,
ComponentHost,
protocol,
namedict,
ndict
]
+1 -1
View File
@@ -50,7 +50,7 @@ class DataSource(Component):
"""
Emit data.
"""
assert isinstance(event, zp.namedict)
assert isinstance(event, zp.ndict)
event['source_id'] = self.get_id
event['type'] = self.get_type
+1 -1
View File
@@ -57,7 +57,7 @@ class Merge(Feed):
def append(self, event):
"""
:param event: a namedict with one entry. key is the name of the
:param event: a ndict with one entry. key is the name of the
transform, value is the transformed value.
Add an event to the buffer for the source specified by
source_id.
+12 -12
View File
@@ -40,8 +40,8 @@ Performance Tracking
| | through all the events delivered to this tracker. |
| | For details look at the comments for |
| | :py:meth:`zipline.finance.risk.RiskMetrics.to_dict`|
+-----------------+----------------------------------------------------+
| exceeded_max_ | True if the simulation was stopped because single |
+-----------------+----------------------------------------------------+
| exceeded_max_ | True if the simulation was stopped because single |
| loss | day losses exceeded the max_drawdown stipulated in |
| | trading_environment. |
+-----------------+----------------------------------------------------+
@@ -191,7 +191,7 @@ class PerformanceTracker():
)
def get_portfolio(self):
return self.cumulative_performance.to_namedict()
return self.cumulative_performance.to_ndict()
def publish_to(self, zmq_socket, context=None):
"""
@@ -231,7 +231,7 @@ class PerformanceTracker():
if self.exceeded_max_loss:
return
assert isinstance(event, zp.namedict)
assert isinstance(event, zp.ndict)
self.event_count += 1
if(event.dt >= self.market_close):
@@ -521,18 +521,18 @@ class PerformancePeriod():
return rval
def to_namedict(self):
def to_ndict(self):
"""
Creates a namedict representing the state of this perfomance period.
Creates a ndict representing the state of this perfomance period.
Properties are the same as the results of to_dict. See header comments
for a detailed description.
"""
positions = self.get_positions(namedicted=True)
positions = self.get_positions(ndicted=True)
positions = zp.namedict(positions)
positions = zp.ndict(positions)
return zp.namedict({
return zp.ndict({
'ending_value' : self.ending_value,
'capital_used' : self.period_capital_used,
'starting_value' : self.starting_value,
@@ -545,12 +545,12 @@ class PerformancePeriod():
'transactions' : self.processed_transactions
})
def get_positions(self, namedicted=False):
def get_positions(self, ndicted=False):
positions = {}
for sid, pos in self.positions.iteritems():
cur = pos.to_dict()
if namedicted:
positions[sid] = zp.namedict(cur)
if ndicted:
positions[sid] = zp.ndicted(cur)
else:
positions[sid] = cur
+4 -4
View File
@@ -6,7 +6,7 @@ import random
import pytz
from zipline.components import DataSource
from zipline.utils import ndict, namedict
from zipline.utils import ndict
import zipline.protocol as zp
@@ -24,7 +24,7 @@ class TradeDataSource(DataSource):
if event.sid in self.filter['SID']:
message = zp.DATASOURCE_FRAME(event)
else:
blank = namedict({
blank = ndict({
"type" : zp.DATASOURCE_TYPE.TRADE,
"source_id" : self.get_id
})
@@ -59,7 +59,7 @@ class RandomEquityTrades(TradeDataSource):
self.price = self.price + random.uniform(-0.05, 0.05)
volume = random.randrange(100,10000,100)
event = zp.namedict({
event = zp.ndict({
"type" : zp.DATASOURCE_TYPE.TRADE,
"sid" : self.sid,
"price" : self.price,
@@ -100,5 +100,5 @@ class SpecificEquityTrades(TradeDataSource):
return
event = self.event_list.pop(0)
self.send(zp.namedict(event))
self.send(zp.ndict(event))
self.count +=1
+3 -3
View File
@@ -13,7 +13,7 @@ from zipline.core import Component
import zipline.protocol as zp
import zipline.finance.performance as perf
from zipline.utils.protocol_utils import Enum, namedict
from zipline.utils.protocol_utils import Enum, ndict
# the simulation style enumerates the available transaction simulation
# strategies.
@@ -164,7 +164,7 @@ class TradeSimulationClient(Component):
return self.connect_push_socket(self.addresses['order_address'])
def order(self, sid, amount):
order = zp.namedict({
order = zp.ndict({
'dt':self.current_dt,
'sid':sid,
'amount':amount
@@ -357,7 +357,7 @@ for orders:
'commission' : self.commission * amount * direction,
'source_id' : zp.FINANCE_COMPONENT.TRANSACTION_SIM
}
return zp.namedict(txn)
return zp.ndict(txn)
class TradingEnvironment(object):
+24 -24
View File
@@ -65,7 +65,7 @@ Namedict
Namedicts are dict like objects that have fields accessible by attribute lookup
as well as being indexable and iterable::
HEARTBEAT_PROTOCOL = namedict({
HEARTBEAT_PROTOCOL = ndict({
'REQ' : b'\x01',
'REP' : b'\x02',
})
@@ -123,7 +123,7 @@ import time
import copy
from collections import namedtuple
from utils.protocol_utils import Enum, FrameExceptionFactory, namedict
from utils.protocol_utils import Enum, FrameExceptionFactory, ndict
from utils.date_utils import EPOCH, UN_EPOCH
# -----------------------
@@ -221,7 +221,7 @@ def DATASOURCE_FRAME(event):
Wraps any datasource payload with id and type, so that unpacking may choose
the write UNFRAME for the payload.
:param event: namedict with following properties
:param event: ndict with following properties
- *ds_id* an identifier that is unique to the datasource in the context of a component host (e.g. Simulator)
- *ds_type* a string denoting the datasource type. Must be on of:
@@ -283,9 +283,9 @@ datasource type passed along.
try:
ds_type, source_id, payload = msgpack.loads(msg)
assert isinstance(ds_type, int)
rval = namedict({'source_id':source_id})
rval = ndict({'source_id':source_id})
if payload == DATASOURCE_TYPE.EMPTY:
child_value = namedict({'dt':None})
child_value = ndict({'dt':None})
elif(ds_type == DATASOURCE_TYPE.TRADE):
child_value = TRADE_UNFRAME(payload)
elif(ds_type == DATASOURCE_TYPE.ORDER):
@@ -314,7 +314,7 @@ def FEED_FRAME(event):
- source_id
- type
"""
assert isinstance(event, namedict)
assert isinstance(event, ndict)
source_id = event.source_id
ds_type = event.type
PACK_DATE(event)
@@ -326,7 +326,7 @@ def FEED_UNFRAME(msg):
payload = msgpack.loads(msg)
#TODO: anything we can do to assert more about the content of the dict?
assert isinstance(payload, dict)
rval = namedict(payload)
rval = ndict(payload)
UNPACK_DATE(rval)
return rval
except TypeError:
@@ -350,13 +350,13 @@ def TRANSFORM_FRAME(name, value):
def TRANSFORM_UNFRAME(msg):
"""
:rtype: namedict with <transform_name>:<transform_value>
:rtype: ndict with <transform_name>:<transform_value>
"""
try:
name, value = msgpack.loads(msg)
if(value == TRANSFORM_TYPE.EMPTY):
return namedict({name : None})
return ndict({name : None})
#TODO: anything we can do to assert more about the content of the dict?
assert isinstance(name, basestring)
if(name == TRANSFORM_TYPE.PASSTHROUGH):
@@ -364,7 +364,7 @@ def TRANSFORM_UNFRAME(msg):
elif(name == TRANSFORM_TYPE.TRANSACTION):
value = TRANSACTION_UNFRAME(value)
return namedict({name : value})
return ndict({name : value})
except TypeError:
raise INVALID_TRANSFORM_FRAME(msg)
except ValueError:
@@ -382,7 +382,7 @@ def MERGE_FRAME(event):
- source_id
- type
"""
assert isinstance(event, namedict)
assert isinstance(event, ndict)
PACK_DATE(event)
if(event.has_attr(TRANSFORM_TYPE.TRANSACTION)):
if(event.TRANSACTION == None):
@@ -397,7 +397,7 @@ def MERGE_UNFRAME(msg):
payload = msgpack.loads(msg)
#TODO: anything we can do to assert more about the content of the dict?
assert isinstance(payload, dict)
payload = namedict(payload)
payload = ndict(payload)
if(payload.has_attr(TRANSFORM_TYPE.TRANSACTION)):
if(payload.TRANSACTION == TRANSFORM_TYPE.EMPTY):
payload.TRANSACTION = None
@@ -425,7 +425,7 @@ INVALID_TRADE_FRAME = FrameExceptionFactory('TRADE')
def TRADE_FRAME(event):
"""
:param event: should be a namedict with:
:param event: should be a ndict with:
- ds_id -- the datasource id sending this trade out
- sid -- the security id
@@ -434,7 +434,7 @@ def TRADE_FRAME(event):
- dt -- datetime for the trade
"""
assert isinstance(event, namedict)
assert isinstance(event, ndict)
assert event.type == DATASOURCE_TYPE.TRADE
assert isinstance(event.sid, int)
assert isinstance(event.price, numbers.Real)
@@ -456,7 +456,7 @@ def TRADE_UNFRAME(msg):
assert isinstance(sid, int)
assert isinstance(price, numbers.Real)
assert isinstance(volume, numbers.Integral)
rval = namedict({
rval = ndict({
'sid' : sid,
'price' : price,
'volume' : volume,
@@ -491,7 +491,7 @@ def ORDER_UNFRAME(msg):
sid, amount, dt = msgpack.loads(msg)
assert isinstance(sid, int)
assert isinstance(amount, int)
rval = namedict({
rval = ndict({
'sid':sid,
'amount':amount,
'dt':dt
@@ -513,7 +513,7 @@ def ORDER_UNFRAME(msg):
def TRANSACTION_FRAME(event):
assert isinstance(event, namedict)
assert isinstance(event, ndict)
assert isinstance(event.sid, int)
assert isinstance(event.price, numbers.Real)
assert isinstance(event.commission, numbers.Real)
@@ -535,7 +535,7 @@ def TRANSACTION_UNFRAME(msg):
assert isinstance(price, numbers.Real)
assert isinstance(commission, numbers.Real)
assert isinstance(amount, int)
rval = namedict({
rval = ndict({
'sid' : sid,
'price' : price,
'amount' : amount,
@@ -577,7 +577,7 @@ def ORDER_SOURCE_FRAME(event):
def ORDER_SOURCE_UNFRAME(msg):
try:
sid, amount, dt, source_id, source_type = msgpack.loads(msg)
event = namedict({
event = ndict({
"sid" : sid,
"amount" : amount,
"dt" : dt,
@@ -688,7 +688,7 @@ def PACK_DATE(event):
PACK_DATE and UNPACK_DATE are inverse operations.
:param event: event must a namedict with a property named 'dt' that is a datetime.
:param event: event must a ndict with a property named 'dt' that is a datetime.
:rtype: None
"""
assert isinstance(event.dt, datetime.datetime)
@@ -710,7 +710,7 @@ def UNPACK_DATE(event):
UNPACK_DATE and PACK_DATE are inverse operations.
:param tuple event: event must a namedict with:
:param tuple event: event must a ndict with:
- a property named 'dt_tuple' that is a tuple of integers \
representing the date and time in UTC.
@@ -742,15 +742,15 @@ ORDER_PROTOCOL = Enum(
)
#Transform type needs to be a namedict to facilitate merging.
TRANSFORM_TYPE = namedict({
#Transform type needs to be a ndict to facilitate merging.
TRANSFORM_TYPE = ndict({
'TRANSACTION' : 'TRANSACTION', #needed?
'PASSTHROUGH' : 'PASSTHROUGH',
'EMPTY' : ''
})
FINANCE_COMPONENT = namedict({
FINANCE_COMPONENT = ndict({
'TRADING_CLIENT' : 'TRADING_CLIENT',
'PORTFOLIO_CLIENT' : 'PORTFOLIO_CLIENT',
'ORDER_SOURCE' : 'ORDER_SOURCE',
+1 -2
View File
@@ -1,6 +1,5 @@
from protocol_utils import namedict, ndict
from protocol_utils import ndict
__all__ = [
namedict,
ndict,
]
+2 -2
View File
@@ -55,7 +55,7 @@ def create_trading_environment(year=2006):
return trading_environment
def create_trade(sid, price, amount, datetime):
row = zp.namedict({
row = zp.ndict({
'source_id' : "test_factory",
'type' : zp.DATASOURCE_TYPE.TRADE,
'sid' : sid,
@@ -88,7 +88,7 @@ def create_trade_history(sid, prices, amounts, interval, trading_calendar):
return trades
def create_txn(sid, price, amount, datetime, btrid=None):
txn = zp.namedict({
txn = zp.ndict({
'sid' : sid,
'amount' : amount,
'dt' : datetime,