Permalink
Browse files

Merge branch 'master' of github.com:TeamGamma/directedstudies

  • Loading branch information...
2 parents e1f4eea + 1385b69 commit 7f420d5cd2b645be8f6b339179e8a095779da05b @alimjiwa alimjiwa committed Feb 16, 2012
Showing with 162 additions and 24 deletions.
  1. +4 −2 fabfile.py
  2. +39 −0 sps/quotes/client.py
  3. +119 −0 sps/transactions/models.py
  4. +0 −22 sps/transactions/stocks.py
View
@@ -19,8 +19,10 @@
sys.path.insert(0, lib_path)
-def transaction_server(port=6000):
+def tserver(port=6000, autoreload=True):
""" Run transaction server in development mode """
from sps.transactions.server import run_server
- run_server(int(port))
+ run_server(int(port), autoreload)
+ exit(0)
+
View
@@ -1,4 +1,43 @@
import socket, sys
+from random import randrange
+from sps.transactions.models import Money
+
+class QuoteClient(object):
+ """
+ A client for requesting quotes from a stock quote server.
+ """
+ @staticmethod
+ def str_to_money(money_str):
+ """
+ Converts the quote servers representation of money (e.g. "1.43") into a Money
+ >>> money_str = "1.43"
+ >>> money = QuoteClient.str_to_money(money_str)
+ >>> money.dollars, money.cents
+ (1, 43)
+ """
+ dollars, cents = map(int, money_str.split('.'))
+ return Money(dollars, cents)
+
+ @staticmethod
+ def money_to_str(money):
+ """
+ Converts a Money object into the string representation used by the server
+ >>> money = Money(dollars=45, cents=67)
+ >>> QuoteClient.money_to_str(money)
+ '45.67'
+ """
+ return '.'.join(map(str, money))
+
+
+
+class RandomQuoteClient(QuoteClient):
+ stock_quote_max = 100
+
+ def get_quote(self, symbol):
+ dollars, cents = randrange(0, self.stock_quote_max), randrange(0, 100)
+ return Money(dollars, cents)
+
+
if __name__ == '__main__':
# Print info for the user
View
@@ -0,0 +1,119 @@
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy import Column, Integer, String
+from sqlalchemy.orm import composite
+from collections import namedtuple
+
+Base = declarative_base()
+
+class Money(namedtuple('Money', 'dollars cents')):
+ """
+ Used to represent money in the system without floating point errors.
+ Note: Money objects are immutable.
+
+ >>> m1 = Money(dollars=10, cents=20)
+ >>> m1.dollars, m1.cents
+ (10, 20)
+
+ >>> m2 = Money(10, 20)
+ >>> m1 == m2
+ True
+ """
+ __slots__ = ()
+
+ def __composite_values__(self):
+ return (self[0], self[1])
+
+
+class Client(Base):
+ __tablename__ = 'client'
+
+ id = Column(Integer, primary_key=True, autoincrement=True)
+ username = Column(String(50))
+ password = Column(String(50))
+ _account_balance_dollars = Column(Integer)
+ _account_balance_cents = Column(Integer)
+ _reserve_balance_dollars = Column(Integer)
+ _reserve_balance_cents = Column(Integer)
+
+ # Automatically convert between Money and dollars/cents columns
+ account_balance = composite(Money, _account_balance_dollars, _account_balance_cents)
+ reserve_balance = composite(Money, _reserve_balance_dollars, _reserve_balance_cents)
+
+ def __init__(self, username, password, account_balance, reserve_balance):
+ self.username = username
+ self.password = password
+ self._account_balance_dollars = account_balance.dollars
+ self._account_balance_cents = account_balance.cents
+ self._reserve_balance_dollars = reserve_balance.dollars
+ self._reserve_balance_cents = reserve_balance.cents
+
+ def __repr__(self):
+ return "<Client('%s', balance=%s)>" % (self.username, self.account_balance)
+
+
+class Query(Base):
+ __tablename__ = 'queries'
+
+ id = Column(Integer, primary_key=True, autoincrement=True)
+ user_id = Column(Integer)
+ stock_symbol = Column(String(7))
+ _stock_value_dollars = Column(Integer)
+ _stock_value_cents = Column(Integer)
+ stock_value = composite(Money, _stock_value_dollars, _stock_value_cents)
+ crypto_key = Column(String(50))
+ _query_fee_dollars = Column(Integer)
+ _query_fee_cents = Column(Integer)
+ query_fee = composite(Money, _query_fee_dollars, _query_fee_cents)
+ timestamp = Column(String(50))
+
+ def __init__(self, user_id, stock_symbol, stock_value, crypto_key, query_fee, timestamp):
+ self.user_id = user_id
+ self.stock_symbol = stock_symbol
+ self.stock_value = stock_value
+ self.crypto_key = crypto_key
+ self.query_fee = query_fee
+ self.timestamp = timestamp
+
+
+class Transaction(Base):
+ __tablename__ = 'transactions'
+
+ id = Column(Integer, primary_key=True, autoincrement=True)
+ user_id = Column(Integer)
+ stock_symbol = Column(String(7))
+ _stock_value_dollars = Column(Integer)
+ _stock_value_cents = Column(Integer)
+ operation = Column(String(3))
+ quantity = Column(Integer)
+ _broker_fee_dollars = Column(Integer)
+ _broker_fee_cents = Column(Integer)
+ broker_fee = composite(Money, _broker_fee_dollars, _broker_fee_cents)
+ processing_time = Column(String(50))
+ timestamp = Column(String(50))
+
+ def __init__(self):
+ pass
+
+
+
+class SetTransaction(Base):
+ __tablename__ = 'set_transactions'
+
+ id = Column(Integer, primary_key=True, autoincrement=True)
+ user_id = Column(Integer)
+ stock_symbol = Column(String(7))
+ _stock_value_dollars = Column(Integer)
+ _stock_value_cents = Column(Integer)
+ stock_value = composite(Money, _stock_value_dollars, _stock_value_cents)
+ operation = Column(String(3))
+ quantity = Column(Integer)
+ _broker_fee_dollars = Column(Integer)
+ _broker_fee_cents = Column(Integer)
+ broker_fee = composite(Money, _broker_fee_dollars, _broker_fee_cents)
+ processing_time = Column(String(50))
+ query_times = Column(Integer)
+ timestamp = Column(String(50))
+
+ def __init__(self):
+ pass
+
View
@@ -1,22 +0,0 @@
-from sqlalchemy.ext.declarative import declarative_base
-from sqlalchemy import Column, Integer, String
-
-Base = declarative_base()
-
-class User(Base):
- __tablename__ = 'users'
-
- id = Column(Integer, primary_key=True, autoincrement=True)
- username = Column(String(50))
- password = Column(String(50))
- funds = Column(Integer)
-
- def __init__(self, username, password, funds=0):
- self.username = username
- self.password = password
- self.funds = funds
-
- def __repr__(self):
- return "<User('%s','%s', '%s')>" % (self.username, self.password, self.funds)
-
-

0 comments on commit 7f420d5

Please sign in to comment.