Skip to content

Commit

Permalink
Merge 92ea888 into 545406c
Browse files Browse the repository at this point in the history
  • Loading branch information
hootnot committed Nov 16, 2016
2 parents 545406c + 92ea888 commit ef0c321
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
20 changes: 17 additions & 3 deletions oandapyV20/definitions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,27 @@ def make_definition_classes(mod):
setattr(dyncls, attrName, K) # set as class attributes
definitions.update({K: V}) # for mapping by __getitem__

def mkgi(definitions):
def mkgi():
def __getitem__(self, definitionID):
self._definitions = definitions
"""return description for definitionID."""
return self._definitions[definitionID]
return __getitem__

setattr(dyncls, "__getitem__", mkgi(definitions))
def mkinit(definitions):
def __init__(self):
self._definitions = definitions

return __init__

def mkPropDefinitions():
def definitions(self):
"""readonly property holding definition dict."""
return self._definitions
return property(definitions)

setattr(dyncls, "__getitem__", mkgi())
setattr(dyncls, "__init__", mkinit(definitions))
setattr(dyncls, "definitions", mkPropDefinitions())
setattr(sys.modules["{}.definitions.{}".format(rootpath, mod)],
cls, dyncls)

Expand Down
2 changes: 2 additions & 0 deletions oandapyV20/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

__all__ = [
'AccountID',
'OrderID',
'TradeID',
'AccountUnits',
Expand All @@ -13,6 +14,7 @@
]

from .types import (
AccountID,
OrderID,
TradeID,
AccountUnits,
Expand Down
29 changes: 29 additions & 0 deletions oandapyV20/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""types."""

import six
import re
from abc import ABCMeta


Expand All @@ -15,6 +16,34 @@ def value(self):
return self._v


class AccountID(OAType):
"""representation of an AccountID, string value of an Account Identifier.
Parameters
----------
accountID : string (required)
the accountID of a v20 account
Example
-------
>>> print AccountID("001-011-5838423-001").value
A ValueError exception is raised in case of an incorrect value.
"""

def __init__(self, accountID):
l = re.match(r"(?P<siteID>\d+)-(?P<divisionID>\d+)"
"-(?P<userID>\d+)-(?P<accountNumber>\d+)", accountID)
if not l:
msg = "AccountID {} not a valid V20 account".format(accountID)
raise ValueError(msg)

self._v = l.groupdict()


class OrderID(OAType):
"""representation of an orderID, string value of an integer.
Expand Down
6 changes: 6 additions & 0 deletions tests/test_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ def test__order_definitions(self):
self.assertTrue(isinstance(c, allDEF.orders.OrderType) and
c['MARKET'] == orderDefs['OrderType']['MARKET'])

def test__order_definitions_dictproperty(self):
"""test for the definitions property."""
c = allDEF.orders.OrderType()
self.assertTrue(isinstance(c.definitions, dict) and
c.definitions['MARKET'] == c['MARKET'])


if __name__ == "__main__":

Expand Down
8 changes: 8 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class TestTypes(unittest.TestCase):
"""Tests types."""

@parameterized.expand([
# AccountID
(tp.AccountID,
{"accountID": "001-011-5838423-001"},
{"siteID": "001",
"divisionID": "011",
"userID": "5838423",
"accountNumber": "001"}
),
# OrderID
(tp.OrderID,
{"orderID": "1234"},
Expand Down

0 comments on commit ef0c321

Please sign in to comment.