Utility functions.
More...
|
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 ) |
|
Utility functions.
Last Changed
- Id:
- Utils.py 27 2015-04-25 16:16:47Z michael
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
-
dict | input can be a dictionary or a list of dictionaries |
out_str | used only for recursive calls |
shift_nb | only 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 | ) |
|
def serverportfolio.Utils.stringToEAction |
( |
|
action | ) |
|
Always return a valid EAction from an Eaction or an EAction.name.
- Parameters
-
action | EAction type or EAction.name |
- Returns
- valid EAction
- Exceptions
-
Definition at line 40 of file Utils.py.
def serverportfolio.Utils.timestamp_to_string |
( |
|
ts | ) |
|
Time function.
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
-
stock | a string ("CAC40") or a list of string ( ["CAC40","GSZ"] ) |
- Returns
- list of strings
Definition at line 18 of file Utils.py.
dictionary serverportfolio.Utils.dict1 |
Initial value:
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}} |
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'}}] |
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'}}] |