Permalink
Browse files

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

  • Loading branch information...
2 parents 17c749b + 005736a commit 186caff95f1af5692ab7454ff2a403b762d7412f @robbles robbles committed Mar 1, 2012
Showing with 52 additions and 38 deletions.
  1. +2 −2 sps/config.py
  2. +44 −28 sps/quotes/client.py
  3. +3 −5 sps/transactions/commands.py
  4. +3 −3 tests/test_commands.py
View
@@ -1,4 +1,4 @@
-import sps.quotes.client
+from sps.quotes.client import RandomQuoteClient
class ConfigObject():
"""
@@ -18,7 +18,7 @@ class ConfigObject():
'echo': True,
}
- QUOTE_CLIENT = sps.quotes.client.RandomQuoteClient
+ QUOTE_CLIENT = RandomQuoteClient()
# The global configuration object
View
@@ -1,56 +1,72 @@
-import socket
-import sys
from random import randrange
from sps.database.models import Money
+from eventlet.green import socket
+_QUOTE_CLIENT = None
-class QuoteClient(object):
+def get_quote_client():
"""
- A client for requesting quotes from a stock quote server.
+ Returns the global quote client, creating it first if necessary.
"""
- _QUOTE_CLIENT = None
+ global _QUOTE_CLIENT
- def get_quote(self, symbol):
- raise NotImplementedError('QuoteClient can not be used directly')
+ if not _QUOTE_CLIENT:
+ from sps.config import config
+ _QUOTE_CLIENT = config.QUOTE_CLIENT
+ return _QUOTE_CLIENT
- @classmethod
- def get_quote_client(cls):
- """
- Returns a reference to the singleton quote client, creating it if
- necessary.
- """
- if not cls._QUOTE_CLIENT:
- cls._QUOTE_CLIENT = _DEFAULT_QUOTE_CLIENT()
- return cls._QUOTE_CLIENT
- @classmethod
- def set_quote_client(cls, client):
- cls._QUOTE_CLIENT = client
+class RandomQuoteClient(object):
+ def __init__(self, quote_min=0, quote_max=100):
+ self.qmin = quote_min
+ self.qmax = quote_max
-
-class RandomQuoteClient(QuoteClient):
- stock_quote_max = 100
-
- def get_quote(self, symbol):
- dollars, cents = randrange(0, self.stock_quote_max), randrange(0, 100)
+ def get_quote(self, symbol, username):
+ dollars, cents = randrange(self.qmin, self.qmax), randrange(0, 100)
return Money(dollars, cents)
-class DummyQuoteClient(QuoteClient):
+class DummyQuoteClient(object):
def __init__(self, quote_map, default=Money(0, 0)):
self.quote_map = quote_map
self.default = default
- def get_quote(self, symbol):
+ def get_quote(self, symbol, username):
if symbol in self.quote_map:
return self.quote_map[symbol]
return self.default
-_DEFAULT_QUOTE_CLIENT = RandomQuoteClient
+class SENGQuoteClient(object):
+ def __init__(self, address):
+ self.address = address
+
+ def get_quote(self, symbol, username):
+ message = ','.join(symbol, username)
+
+ # Create the socket
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ # Connect the socket
+ s.connect(self.address)
+ # Send the user's query
+ s.sendall(message)
+ # Read and print up to 1k of data.
+ data = s.recv(1024)
+
+ # message format: “Quote, Stock Symbol, USER NAME, CryptoKey”
+ # '58.17,APP,robodwye,1330546050315,ZcwKUqtHPq/PaprbZRKrFSw+zuIQiYA5XlEfLUkxkUIsWaN0xSiiWw==\n'
+ quotes, symbol2, username2, cryptokey = data.split(',')
+
+ quote = Money.from_string(quotes)
+ return quote, cryptokey
+
+
if __name__ == '__main__':
+ import socket
+ import sys
+
# Print info for the user
print("\nEnter: StockSYM, userid")
print(" Invalid entry will return 'NA' for userid.")
@@ -1,11 +1,9 @@
"""
-This file does stuff.
-
-
+This file contains the implementations for all transaction server commands.
"""
from sps.database.session import get_session
from sps.database.models import User, Money, Transaction, StockPurchase
-from sps.quotes.client import QuoteClient
+from sps.quotes.client import get_quote_client
from datetime import datetime
class CommandError(Exception):
@@ -115,7 +113,7 @@ def run(self, userid, stock_symbol):
if len(stock_symbol) > 4:
raise InvalidInputError('stock symbol too long: %d' % \
len(stock_symbol))
- quote_client = QuoteClient.get_quote_client()
+ quote_client = get_quote_client()
quote = quote_client.get_quote(stock_symbol)
return str(quote)
View
@@ -2,7 +2,7 @@
from tests.utils import DatabaseTest
from sps.transactions import commands
from sps.database.models import User, Money, Transaction, StockPurchase
-from sps.quotes.client import QuoteClient, DummyQuoteClient
+from sps.quotes import client
class TestADDCommand(DatabaseTest):
def setUp(self):
@@ -42,10 +42,10 @@ def setUp(self):
self.command = commands.QUOTECommand()
# Set the quote client to a dummy that returns predictable results
- QuoteClient.set_quote_client(DummyQuoteClient({
+ client._QUOTE_CLIENT = client.DummyQuoteClient({
'AAAA': Money(23, 45),
'BBBB': Money(85, 39),
- }))
+ })
def test_return_value(self):
""" Should return a decimal value for the stock price """

0 comments on commit 186caff

Please sign in to comment.