ServerPortfolio  2.0
Python parsers and server
 All Classes Namespaces Files Functions Variables Properties Pages
main_dict.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 ## @package serverportfolio.main_dict
4 # @brief Executable to test the module DictionaryStocks.
5 #
6 # Because it is defined as a singleton, the main cannot be included in the original module.
7 #
8 # Last Changed $Id: main_dict.py 9 2015-04-02 23:27:25Z michael $
9 
10 
11 
12 #import sys, re, types, logging
13 import sys, logging, traceback
14 #import traceback
15 # bug parser try to sleep
16 #import time
17 # try for bug related to split
18 #import string
19 #import os
20 #import os.path
21 #import time, datetime
22 
23 # import first the singleton
24 from serverportfolio.DictionaryStocks import DictionaryStocks
25 
26 #from serverportfolio.GlobalDicts import *
27 #from serverportfolio.ModClientTCP import TCPClient
28 from serverportfolio import Utils
29 from serverportfolio.PortfolioException import PortfolioError, ParserError
30 
31 
32 
33 # ############# Main only for standalone test ########
34 
35 # problem because DictStock variable in __name__, cannot access from import module !
36 if __name__ == "__main__":
37 
38  import sys
39  print "DictionaryStocks main"
40 
41  # To use logger need to set in all main executables
42  logging.basicConfig(level=logging.DEBUG)
43  # write output to a file
44  # logging.basicConfig(filename='myapp.log', level=logging.INFO) # filemode='w' to not append to file
45  # all modules declare child loggers of SP
46  logger = logging.getLogger('SP')
47  # logger.warning("warning1")
48  # specific to the main part
49  main_logger = logging.getLogger("SP.main")
50 
51  # try to initialise the dictionary with the file given as first argument, searched in PATH_DATA
52  try:
53  dict_filename = str(sys.argv[1])
54  main.logger.debug("dictionary file as argument: %s" % dict_filename)
55  dict_stocks1 = DictionaryStocks( dict_filename )
56 
57  # if argument not given, use the default dictstocks.txt
58  except:
59  dict_stocks1 = DictionaryStocks()
60 
61  print "DictionaryStocks initialized"
62  #sys.exit()
63 
64  print "\n== dict_stocks 2, test Singleton"
65  # OK, init is not called this time. Singleton working
66  dict_stocks2 = DictionaryStocks( "dictstocks.txt" )
67  # test id, should be the same
68  print dict_stocks1
69  print id(dict_stocks1)
70  print dict_stocks2
71  print id(dict_stocks2)
72  #sys.exit()
73 
74  # load a new stock
75  print "\n== get 1 Stock, CAC40"
76  DictionaryStocks().get_stocks('CAC40')
77  #sys.exit(1)
78 
79  # test function, print data
80  print "First Print "
81  dict_stocks1.print_dict_stocks( opt_print = True )
82  #sys.exit(1)
83 
84  print "Second Print "
85  lines = dict_stocks1.print_dict_stocks( "CAC40", opt_print = False )
86  print "line CAC40"
87  print lines
88 
89  # ask for a non existing Stock, got the exception
90  # no more exception but a InvalidStock
91  #try:
92  dict_stocks1.get_stocks("toto")
93  #except PortfolioError as ex:
94  # print "Got PortfolioError ", ex
95  #sys.exit(1)
96 
97  # print a non-existing Stock
98  print "\n== Print a existing and non-loaded stock"
99  lines = dict_stocks1.print_dict_stocks( ["CAC40","SP500"], opt_print = False )
100  print "line CAC40 SP500"
101  print lines
102 
103  print "\n== Print 2 existing and 1 non-existing stock"
104  lines = dict_stocks1.print_dict_stocks( ["CAC40","SP500", "toto"], opt_print = False )
105  print "line CAC40 SP500 toto"
106  print lines
107  #sys.exit()
108 
109 
110  print "\n=== Update Inst Value"
111  try :
112  #dict_stocks1.update_inst_value()
113  # more general
114  dict_stocks1.update_data( action='InstValue' )
115 
116  except PortfolioError as ex:
117  print "Main catch a PortfolioError "
118  print "message ", ex # print only exc_value
119  # test is instance
120  #if ex.url is not None:
121  # print "url from ex ", ex.url
122  # from documentation traceback, ok
123  exc_type, exc_value, exc_traceback = sys.exc_info()
124  # traceback lost again...
125  print "*** print_tb:"
126  traceback.print_tb(exc_traceback, limit=2, file=sys.stdout)
127  print "full message: \n", ex.get_format_string()
128 
129  print "exit properly "
130  sys.exit(1)
131 
132  # global exception, should not happen
133  except Exception as ex:
134  #self.logger.error("got exception in Update() %s " % ex )
135  print "got general Exception in Update(), Should not happen %s " % ex
136  exc_type, exc_value, exc_traceback = sys.exc_info()
137  # traceback lost again...
138  print "*** print_tb:"
139  traceback.print_tb(exc_traceback, limit=4, file=sys.stdout)
140  #print traceback
141  sys.exit(1)
142 
143 # post-process
144  print '\n== After update inst value, summarize'
145  dict_stocks1.print_dict_stocks( opt_print = True )
146  #sys.exit(1)
147 
148  print "\n===== all stocks ===== "
149  print dict_stocks1
150  dict_stocks1.print_dict_stocks( opt_print = True )
151  #sys.exit(1)
152  # make different test
153  # useful, independent of the structure, dict_stocks.get_liststocks()
154 
155  print "\n=== check_open "
156  #for stock in dict_stocks1.get_stocks( ["CAC40","SP500", "toto"] ):
157  for stock in ["CAC40","SP500", "toto"]:
158  #print "stock : ", stock
159  print " CheckOpen ",dict_stocks1.check_open(stock)
160  #dict_stocks1.check_open( ["CAC40","SP500", "toto"] )
161  # time to open, gives strange results
162  #time_to_open = dict_stocks1.time_to_open(stock)
163  #print " time_to_open ",time_to_open
164 
165  ## test to add, TestML_done
166  dict_stocks1.print_dict_stocks( opt_print = True )
167  #sys.exit(1)
168 
169  # has been moved to Stock, logic!
170  print "\n=== time_to_open "
171  for stock in dict_stocks1.get_stocks( ["CAC40","SP500", "toto"] ):
172  time_to_open = stock.time_to_open()
173  print " time_to_open ",time_to_open
174  dict_stocks1.print_dict_stocks( opt_print = True )
175  #sys.exit(1)
176 
177  ## Test save to file
178  print "\n SaveInstValue "
179  #for stock in dict_stocks1.GetListStocks():
180  for stock in dict_stocks1.get_stocks( ["CAC40","SP500", "toto"] ):
181  print "stock : ", stock
182 
183  # from AutoParser
184  # here get last values from the web
185  #dict_data=self.dict_stocks.GetInstValue(namestock)
186  #self.dict_stocks.SetLastModification ( namestock, dict_data['time'] )
187  #self.dict_stocks.AddVolatileData( namestock, dict_data )
188 
189  #dict_data = dict_stocks1.GetInstValue( stock )
190  #dict_data = dict_stock1.GetStock( stock )
191  #dict_stocks1.SetLastModification( stock, dict_data['Static']['time'] )
192  #dict_stocks1.AddVolatileData( stock, dict_data )
193 
194  #dict_stocks1.SaveInstValue( stock, dict_data )
195  #dict_stocks1.SaveInstValue2( stock, dict_stocks1[stock]['InstValue'] )
196  stock.save_inst_value()
Define custom and specific exceptions for the complete package.
Container of all Stocks objects, it also reads the static stocks configuration file "dictstocks...
Define singleton class DictionaryStocks, act as the main container of Stocks objects.