ServerPortfolio  2.0
Python parsers and server
 All Classes Namespaces Files Functions Variables Properties Pages
Public Member Functions | Private Member Functions | Static Private Attributes | List of all members
serverportfolio.StockTemplates.StockTemplates Class Reference

Define template dictionaries to describe the data structure. More...

Inheritance diagram for serverportfolio.StockTemplates.StockTemplates:
Inheritance graph
[legend]
Collaboration diagram for serverportfolio.StockTemplates.StockTemplates:
Collaboration graph
[legend]

Public Member Functions

def get_template_stock
 Return a copy of an action template for stocks, include 'template' and 'config', delete 'attrib' entry. More...
 
def get_template_parser
 Return the template for parser to be included by all parsers. More...
 
def get_template_xml
 Specific for xml dictionary, replace each value by a list. More...
 
def get_template_one_value_xml
 Return a template for each xml value, include 'value' and 'atrib' for each one Or/And, with action, name can set default append/date. More...
 
def get_template_valid_xml_value
 Retrun a template for the validation template, include 'new_value','xml_value','repl_add','date'. More...
 
def set_tmpl_static
 Set the internal template for the static data, also for invalid stock. More...
 
def get_tmpl_static_invalid
 Return the invalid static template. More...
 
def to_save_xml
 Query to know if this template must be saved in XML file. More...
 
def saved_as
 Query the format in which the data must be saved. More...
 
def create_default_template_xml
 Create a default xml dictionary, called when creating a new XML file. More...
 
def make_header
 

Private Member Functions

def _reset
 Reset the state of templates to the original. More...
 

Static Private Attributes

tuple _logger = logging.getLogger("SP.StockTemplate")
 
dictionary _templ_fundamental
 Fundamental data, used by YQL and YahooCSV. More...
 
dictionary _templ_info
 Normally these entries should not change. More...
 
dictionary _templ_hp
 Historical Prices, the complete list (CSV) is written to file but not saved in dictionary format of date in template, string/timestamp/datetime.datetime ? C++ always timestamp, but datetime.datetime more convenient in python. More...
 
dictionary _templ_inst_value
 For instantaneous value. More...
 
dictionary _templ_div
 Dividend/Split same as HP, need dates certainly maybe easier to split div and split ? More...
 
dictionary _templ_intro
 DatesIntro, first CSV date available (in CSV), integer format (extends to string? last date?) More...
 
dictionary _templ_one_value_xml
 Template for one XML value, stored in a list (of _templ_spec_xml) Option specify the default value in case of a new file date : str(), force each entry to provide a value. More...
 
dictionary _templ_static = {}
 Static data, keys are initialised only when reading the configuration file in DictionaryStocks::init. More...
 
dictionary _templ_static_invalid = {}
 Static data for InvalidStock, all fields are set to 'invalid'. More...
 
dictionary _templ_valid_xml_value
 
dictionary _templ_action
 Skeleton for the dictionaries of each action. More...
 
dictionary _templ_parser
 Define a generic template to be included by all parsers, for each stock. More...
 

Detailed Description

Define template dictionaries to describe the data structure.

Templates are named by the "action" to be performed by the parsers.
XML output extends the action template.
It is used by a global variable which must be accessed by StockTemplates.StTmpl
Group templates, by use of dictionaries, to be included by Stock when they run a specific request (EAction, e.g. InstValue, HistPrice..)
'Static' data are a particular template which is read from the configuration file (dictstocks.txt by default) by DictionaryStocks.

Definition at line 20 of file StockTemplates.py.

Member Function Documentation

def serverportfolio.StockTemplates.StockTemplates._reset (   cls)
private

Reset the state of templates to the original.

Warning
Should not be used in the application, only for unit_test at this point, but may need later for reload. to add dynamically in unit-test ?

Definition at line 280 of file StockTemplates.py.

def serverportfolio.StockTemplates.StockTemplates.create_default_template_xml (   cls)

Create a default xml dictionary, called when creating a new XML file.

Assign same templates as stock, with value replaced by a list (one_value_xml)
Apply only to action with a save option 'xml' in 'config'

Postcondition
All one_values_xml template have the default attributes of the action

Definition at line 459 of file StockTemplates.py.

def serverportfolio.StockTemplates.StockTemplates.get_template_one_value_xml (   cls)

Return a template for each xml value, include 'value' and 'atrib' for each one Or/And, with action, name can set default append/date.

Definition at line 375 of file StockTemplates.py.

def serverportfolio.StockTemplates.StockTemplates.get_template_parser (   cls,
  action = None 
)

Return the template for parser to be included by all parsers.

Include the symbol of the stock (used for multiple stocks query),
and the template specific an action.
In multiple stock query, these template are stored in a list (AbstractParser.list_return_data)

Note
Must be included before any other templates (usually in the parse() function of the parser)

Definition at line 327 of file StockTemplates.py.

def serverportfolio.StockTemplates.StockTemplates.get_template_stock (   cls,
  action,
  for_xml = False 
)

Return a copy of an action template for stocks, include 'template' and 'config', delete 'attrib' entry.

Parameters
actionEAction or EAction.name

Definition at line 300 of file StockTemplates.py.

def serverportfolio.StockTemplates.StockTemplates.get_template_valid_xml_value (   cls)

Retrun a template for the validation template, include 'new_value','xml_value','repl_add','date'.

Definition at line 380 of file StockTemplates.py.

def serverportfolio.StockTemplates.StockTemplates.get_template_xml (   cls,
  action 
)

Specific for xml dictionary, replace each value by a list.

Returns
dictionary template action for xml data

Definition at line 349 of file StockTemplates.py.

def serverportfolio.StockTemplates.StockTemplates.get_tmpl_static_invalid (   cls)

Return the invalid static template.

Called at the creation of InvalidStock

Definition at line 418 of file StockTemplates.py.

def serverportfolio.StockTemplates.StockTemplates.make_header (   self,
  action_name 
)

Definition at line 497 of file StockTemplates.py.

def serverportfolio.StockTemplates.StockTemplates.saved_as (   cls,
  action,
  specific = None 
)

Query the format in which the data must be saved.

Parameters
action
specific

Definition at line 436 of file StockTemplates.py.

Referenced by serverportfolio.ValidStockUpdate.ValidStockUpdate.check_new_xml_data().

Here is the caller graph for this function:

def serverportfolio.StockTemplates.StockTemplates.set_tmpl_static (   cls,
  list_key 
)

Set the internal template for the static data, also for invalid stock.

Set cls._templ_static with keys read from dictstocks.txt
It should be executed only once when reading the configuration file.

Postcondition
cls._tmpl_dict['Static'] should be link to the same object (to allow to use get_template('Static') safely)
Parameters
list_keykeys to insert in the template for static data. Set default as empty string.

Definition at line 392 of file StockTemplates.py.

def serverportfolio.StockTemplates.StockTemplates.to_save_xml (   cls,
  action 
)

Query to know if this template must be saved in XML file.

Returns
bool

Definition at line 427 of file StockTemplates.py.

Member Data Documentation

tuple serverportfolio.StockTemplates.StockTemplates._logger = logging.getLogger("SP.StockTemplate")
staticprivate

Definition at line 23 of file StockTemplates.py.

dictionary serverportfolio.StockTemplates.StockTemplates._templ_action
staticprivate

Skeleton for the dictionaries of each action.

Store: -a specific "template" for the parser

  • a config entry for the management of the sauvegarde (save : 'xml'/'csv')
  • optional xml attributes for the update of data in case of 'save: : 'xml'

Set default values, save_xml, append, date, ... could add:

  • date of the last modification (avoid to rerun web queries)

Definition at line 184 of file StockTemplates.py.

dictionary serverportfolio.StockTemplates.StockTemplates._templ_div
staticprivate
Initial value:
1 = {
2  'first_date' : datetime.datetime.fromtimestamp(0),
3  'last_date' : datetime.datetime.fromtimestamp(0),
4  # maybe to keep all in one list ? easier to recompute adjusted close ? [ ['dividend',date, value],['dividend',date,value],[ SPLIT, date, value(1:4)] ]
5  'list_div' : None,
6  'list_split' : None
7  #'error' : None
8  }

Dividend/Split same as HP, need dates certainly maybe easier to split div and split ?

Definition at line 107 of file StockTemplates.py.

dictionary serverportfolio.StockTemplates.StockTemplates._templ_fundamental
staticprivate
Initial value:
1 = {
2  # fixed fields
3  'MarketCapitalization' : float(0), #append=false, not here
4  'SharesOwned' : int(0),
5  'DividendYield' : float(0),
6  'BVPS' : float(0),
7  'PriceBook' : float(0),
8  'PER' : float(0),
9  'PEG' : float(0),
10  # variable field, only YQL may report other entries, YahooCSV query could be modified as well for test
11  'other' : {},
12  #'error' : None,
13  # add source for each action to be saved in XML. Common, to include by default by an other template ?(one_value_xml)
14  # new added, strange, can delete ?, used by parser to save the source. created on the fly
15  # 'source' : str()
16  }

Fundamental data, used by YQL and YahooCSV.

Note
to copy with copy.deepcopy( templ_dict[ key ] ) Template for fundamental data, used by YahooYQL(default) and YahooCSV

Definition at line 34 of file StockTemplates.py.

dictionary serverportfolio.StockTemplates.StockTemplates._templ_hp
staticprivate
Initial value:
1 = {
2  'first_date' : datetime.datetime.fromtimestamp(0),
3  'last_date' : datetime.datetime.fromtimestamp(0),
4  # yahoo, correct format epoch for list_csv
5  'list_csv' : None
6  #'error' : None
7  }

Historical Prices, the complete list (CSV) is written to file but not saved in dictionary format of date in template, string/timestamp/datetime.datetime ? C++ always timestamp, but datetime.datetime more convenient in python.

If C++ read timestamp. internal C++ datetime.datetime easier, datetime if extended later to inst ?

Definition at line 78 of file StockTemplates.py.

dictionary serverportfolio.StockTemplates.StockTemplates._templ_info
staticprivate
Initial value:
1 = {
2  # Name of the company
3  'Name' : str(),
4  # sector/ industry could be an enum (maybe can extend on the fly??)
5  # YahooAPI CSV defines 9 sectors
6  'Sector' : str(),
7  # Each sector has many industries (not so clear)
8  'Industry' : str(),
9  # place of cotation (Paris, NasdaqNM)
10  'StockExchange' : str(),
11  # with YQL other entries are provided
12  'other' : {},
13  # test error
14  #'error' : None,
15  # new added, in each template, not too clear, necessary for parser
16  #'source' : str()
17  }

Normally these entries should not change.

Executed by YQL, quotes or quote table give the name

Definition at line 53 of file StockTemplates.py.

dictionary serverportfolio.StockTemplates.StockTemplates._templ_inst_value
staticprivate
Initial value:
1 = {
2  'value':0,
3  'variation':0,
4  # format Bourso store string: "2015-02-01 12:10:45"
5  # to change to one datetime like others
6  'time':0,
7  'date':0,
8  'volume':0,
9  'ouverture':0,
10  'plushaut':0,
11  'plusbas':0,
12  # if not parsed, must be available, DEFAULT/initiliazed, ERROR, CLOSED, OPEN
13  'state' : 'CLOSED',
14  # maybe error to add, nice for checking
15  # needed to add to avoid bug with ServerPortfolio, to check where exactly !
16  #'source' : None
17  }

For instantaneous value.

Only Boursorama parser implemented (plan to extend to Yahoo_HTML)

Definition at line 87 of file StockTemplates.py.

dictionary serverportfolio.StockTemplates.StockTemplates._templ_intro
staticprivate
Initial value:
1 = {
2  'intro' : int(0),
3  'intro_str' : str()
4  }

DatesIntro, first CSV date available (in CSV), integer format (extends to string? last date?)

Definition at line 118 of file StockTemplates.py.

dictionary serverportfolio.StockTemplates.StockTemplates._templ_one_value_xml
staticprivate
Initial value:
1 = {
2  'value' : None,
3  'attrib' : { 'append' : False, 'source' : None, 'date' : None } #date should be always present
4  }

Template for one XML value, stored in a list (of _templ_spec_xml) Option specify the default value in case of a new file date : str(), force each entry to provide a value.

None can use default. Could be assumed date is ALWAYS required (I think it is now)

Definition at line 126 of file StockTemplates.py.

dictionary serverportfolio.StockTemplates.StockTemplates._templ_parser
staticprivate
Initial value:
1 = {
2  ## @brief If parsing multiple stocks, associate the symbol
3  'symbol' : str(),
4  ## @brief One of action template: _templ_inst_value, _templ_hp.
5  'action_templ' : {}
6 
7  # error could be common as well or used to report all errors
8  # indicates the action, e.g. 'HistPrice' or a list
9  # here not the best place, used only in dict_return_data,
10  # possible check in UpdateStock before clean parser, in add_dict_data is best
11 
12  #'global_error' : None , not global, one templ_dict_data for each Stock symbol
13  # global_error can only be UpdateStock, local in Stock (stock_error) can check in add_dict_data
14 
15  # source ?? best place, this template only used by parser, need variable to store in dict_data also
16  # done actually, send with add_dict_parser
17  #'source' : str()
18  }

Define a generic template to be included by all parsers, for each stock.

symbol allows the parser to associate the results to each stock (case of multiple stocks query)
action_templ stores templates of the actions performed

Todo:
errors may be common or included by each, only for transient storage, destroyed after parsing is done see get_template_parser()

Definition at line 253 of file StockTemplates.py.

dictionary serverportfolio.StockTemplates.StockTemplates._templ_static = {}
staticprivate

Static data, keys are initialised only when reading the configuration file in DictionaryStocks::init.

Must care about maintaining the link, and to not create a different dictionary

Definition at line 138 of file StockTemplates.py.

dictionary serverportfolio.StockTemplates.StockTemplates._templ_static_invalid = {}
staticprivate

Static data for InvalidStock, all fields are set to 'invalid'.

Definition at line 142 of file StockTemplates.py.

dictionary serverportfolio.StockTemplates.StockTemplates._templ_valid_xml_value
staticprivate
Initial value:
1 = {
2  'new_value' : None,
3  'xml_value' : None,
4  'source' : None,
5  # important to save ! can store the old date ? previous date if interactive ?
6  'date' : datetime.datetime.fromtimestamp(0), #if timestamp all float/int
7  # to replace/add/append in dictionary, done partly automatic - partly by user input
8  # keep status after check. 2:accept, 1:accept or user validation, 0: reject or user validation, -1: reject/discard directly(None,empty...)
9  'repl_add' : int(0)
10  }

Definition at line 161 of file StockTemplates.py.


The documentation for this class was generated from the following file: