ServerPortfolio  2.0
Python parsers and server
 All Classes Namespaces Files Functions Variables Properties Pages
Functions | Variables
serverportfolio.Utils Namespace Reference

Utility functions. More...

Functions

def to_list
 Always return a list of strings. More...
 
def stringToEAction
 Always return a valid EAction from an Eaction or an EAction.name. More...
 
def timestamp_to_string
 Time function. More...
 
def string_to_timestamp
 opposite all seems to be in local time, like the rest of the application (1970-01-01 00:00:00 = -3600) http://stackoverflow.com/questions/9637838/convert-string-date-to-timestamp-in-python More...
 
def pretty_dict
 Nice string output of python dictionary or list of dictionaries. More...
 

Variables

dictionary dict1
 
dictionary dict2 = {'symbol': 'CAC40', 'InstValue ': {'volume': 898.0, 'plusbas': 4224.34, 'state': 'OPEN', 'ouverture': 4294.05, 'variation': -0.22, 'plushaut': 4311.0, 'time': '14:36:00', 'date': '2015-01-02', 'value': 4263.39}}
 
list dict3 = [{'symbol': '"GSZ.PA"', 'list_dict': ['Fundamental'], 'Fundamental': {'other': {}, 'MarketCapitalization': '45.753B', 'BVPS': 21.442, 'SharesOwned': '-', 'PriceBook': 0.9, 'PEG': 'N/A\r', 'DividendYield': 1.67, 'PER': 'N/A'}}]
 
list dict4 = [{'symbol': '"GSZ.PA"', 'list_dict': ['Fundamental'], 'Fundamental': {'other': {}, 'MarketCapitalization': '45.753B', 'BVPS': 21.442, 'SharesOwned': '-', 'PriceBook': 0.9, 'PEG': 'N/A\r', 'DividendYield': 1.67, 'PER': 'N/A'}},{'symbol': '"GSZ2.PA"', 'list_dict': ['Fundamental'], 'Fundamental': {'other': {'subother':'toto', 'subother2':'toto2'}, 'MarketCapitalization': '45.753B', 'BVPS': 21.442, 'SharesOwned': '-', 'PriceBook': 0.9, 'PEG': 'N/A\r', 'DividendYield': 1.67, 'PER': 'N/A'}}]
 
tuple str_pretty = pretty_dict( dict4 )
 

Detailed Description

Utility functions.

Last Changed

Id:
Utils.py 27 2015-04-25 16:16:47Z michael

Function Documentation

def serverportfolio.Utils.pretty_dict (   dict,
  out_str = str(),
  shift_nb = 0 
)

Nice string output of python dictionary or list of dictionaries.

The function is called recursively and deal with dictionary, list and simple values.

Deal with a list of dict_data_stock, ok to improve, option multi-column...

Parameters
dictinput can be a dictionary or a list of dictionaries
out_strused only for recursive calls
shift_nbonly used for recursive calls
Returns
a string containing the formatted output def pretty_dict_origin( dict, out_str = str(), shift_nb = 0 ):
#print "\nPretty Dict:"
#print "type(dict) ", type(dict)
#print "out_str:", out_str,"!!!"
#print dict

shift_str='     '
shift_str=shift_str * shift_nb
#print "shift_str %d:%s!!!" % (shift_nb,shift_str)

if (type(dict) == types.DictType): #| (type(dict) == types.ListType ):

    # if dictionary, for sure will call again 
    #if (type(dict) == types.DictType) :
    # print key, check value. If List for value in dict: (and same logic)
    for key,value in dict.iteritems():

        #print "key::"+key+"!!"
        #specific entry
        #if key == 'list_dict':
        #    out_str +='\n======= ' + value
        #    pass

        # do not want to print key for symbol or list_dict
        #out_str += shift_str + key +' : '

        # recursive call
        if ( type(value) == types.DictType ) | ( type(value) == types.ListType ):

            # specific value, do not want to print
            if key == 'list_dict':
                pass

            else:
                out_str += shift_str + key +' : '
                out_str += '\n'
                # do not want to increase for each entry, maybe each stock
                shift_nb += 1
                #shift_nb = 1 #ok with one stock
                # append the new dictionary or list
                out_str = pretty_dict( value, out_str, shift_nb )    
        # else simple value
        else :

            #specific entry
            if key == 'symbol':
                out_str +='\n======= ' + value + '\n'
                pass
            # not place to check
            #elif key == 'list_dict':
            #    pass
            #out_str += shift_str + str(value)
            else :
                out_str += shift_str + key +' : '
                out_str += str(value)
                out_str += '\n'

# works if values are a list,
# need more test if elements are dictionary/an other List 
elif type(dict) == types.ListType :

    # check type of the first element, only for list
    if len(dict) > 0:
        type_elem = type( dict[0] )
    else:
        print "Empty dictionary in Utils.pretty_dict"
        return out_str
    #print "type_elem ", type_elem

    # case of list of dictionary, to add list of list ??
    if type_elem == types.DictType :
        #out_str += '\n'
        #shift_nb += 1
        for value in dict:
            out_str = pretty_dict( value, out_str, shift_nb )
        #shift_nb -= 1
    # simple value to print
    else :
        # need to loop
        out_str += shift_str + ', '.join(str(x) for x in dict)
        out_str += '\n'
    #shift_nb -= 1

return out_str 

Definition at line 190 of file Utils.py.

def serverportfolio.Utils.string_to_timestamp (   str_date)

opposite all seems to be in local time, like the rest of the application (1970-01-01 00:00:00 = -3600) http://stackoverflow.com/questions/9637838/convert-string-date-to-timestamp-in-python

Definition at line 86 of file Utils.py.

def serverportfolio.Utils.stringToEAction (   action)

Always return a valid EAction from an Eaction or an EAction.name.

Parameters
actionEAction type or EAction.name
Returns
valid EAction
Exceptions
PortfolioError

Definition at line 40 of file Utils.py.

def serverportfolio.Utils.timestamp_to_string (   ts)

Time function.

1 timestamp_to_string(1424991600) \n
2 '2015-02-27 00:00:00'

Definition at line 80 of file Utils.py.

def serverportfolio.Utils.to_list (   stock)

Always return a list of strings.

Assure the list format expected by Parsers::create_url

Parameters
stocka string ("CAC40") or a list of string ( ["CAC40","GSZ"] )
Returns
list of strings

Definition at line 18 of file Utils.py.

Variable Documentation

dictionary serverportfolio.Utils.dict1
Initial value:
1 = {'key1' : 1,
2  'key2' : [2,"toto"], #"2" acepted with join, not 2
3  'key3' : 3 }

Definition at line 261 of file Utils.py.

dictionary serverportfolio.Utils.dict2 = {'symbol': 'CAC40', 'InstValue ': {'volume': 898.0, 'plusbas': 4224.34, 'state': 'OPEN', 'ouverture': 4294.05, 'variation': -0.22, 'plushaut': 4311.0, 'time': '14:36:00', 'date': '2015-01-02', 'value': 4263.39}}

Definition at line 265 of file Utils.py.

list serverportfolio.Utils.dict3 = [{'symbol': '"GSZ.PA"', 'list_dict': ['Fundamental'], 'Fundamental': {'other': {}, 'MarketCapitalization': '45.753B', 'BVPS': 21.442, 'SharesOwned': '-', 'PriceBook': 0.9, 'PEG': 'N/A\r', 'DividendYield': 1.67, 'PER': 'N/A'}}]

Definition at line 267 of file Utils.py.

list serverportfolio.Utils.dict4 = [{'symbol': '"GSZ.PA"', 'list_dict': ['Fundamental'], 'Fundamental': {'other': {}, 'MarketCapitalization': '45.753B', 'BVPS': 21.442, 'SharesOwned': '-', 'PriceBook': 0.9, 'PEG': 'N/A\r', 'DividendYield': 1.67, 'PER': 'N/A'}},{'symbol': '"GSZ2.PA"', 'list_dict': ['Fundamental'], 'Fundamental': {'other': {'subother':'toto', 'subother2':'toto2'}, 'MarketCapitalization': '45.753B', 'BVPS': 21.442, 'SharesOwned': '-', 'PriceBook': 0.9, 'PEG': 'N/A\r', 'DividendYield': 1.67, 'PER': 'N/A'}}]

Definition at line 270 of file Utils.py.

tuple serverportfolio.Utils.str_pretty = pretty_dict( dict4 )

Definition at line 279 of file Utils.py.