Exemplo n.º 1
0
def extract_moneycontrol_data(data_str):
    """Takes moneycontrol data and converts it in slightly more usable form.
    
    Converts '<date>,<value>,<value>,<value>,<value>' string returned by
    the website into dict with date as key and value is value."""
    # TODO: add check here if moneycontrol returns no data or bad data
    lines = data_str.split('\n')
    date_value_dict = {}
    for line in lines:
        l = line.split(',')
        date_value_dict[utils.int_date(l[0])] = float(l[1])
    return date_value_dict, utils.int_date(l[0])
Exemplo n.º 2
0
def txn_to_obj_list(txn_string, amc=None, user_id=None):
    # TODO: in future, if amc is none, detect it from txn_string
    
    txn_matrix = parse_txn(txn_string)
    txn_obj_list = []
    if amc.lower() == 'uti':
        positions = [0, 1, 2, 3, 4]
    elif amc.lower() == 'icici':
        positions = [0, 1, 5, 3, 2]
    else:
        raise

    for txn in txn_matrix:
        obj = dict(fund_name=txn[positions[0]],
                  txn_type=txn[positions[1]],
                  amount=utils.get_float(txn[positions[2]]),
                  units=utils.get_float(txn[positions[3]]),
                  date=utils.int_date(txn[positions[4]]))
        txn_obj_list.append(obj)
    
    for txn in txn_obj_list:        
        if txn['txn_type'].lower() in ['purchase', 'new purchase',
                                'additional purchase']:
            txn['txn_type'] = PURCHASE
        elif txn['txn_type'].lower() in ['redemption']:
            txn['txn_type'] = REDEMPTION
        else:
            raise BaseException


    
    # Additional details contained in transactions
    if amc.lower() == 'uti':
        for obj, txn in zip(txn_obj_list, txn_matrix):
            obj['remarks'] = txn[6].strip() if len(txn) >= 7 else ''

# DO NOT STORE ICICI NAV. IT IS SoMETIMES AN OLD VALUE
#     elif amc.lower() == 'icici':
#         for obj, txn in zip(txn_obj_list, txn_matrix):
#             obj['nav'] = float(txn[4])

    for obj in txn_obj_list:
        obj['amc'] = amc.lower() if amc.lower() in ['icici', 'uti'] else ''
    
    for obj in txn_obj_list:
        obj['user_id'] = user_id
        
    # TODO Do not make this much api calls to db!!
    for obj in txn_obj_list:
        obj['fund_id'] = db.get_fund_id(obj['fund_name'])

    return txn_obj_list
Exemplo n.º 3
0
 def test_int_date(self):
     valid_inp = ['22 feb 2014',
                  '22 sep 2000',
                  '01-oct-2012',
                  '22/22/1222',
                  '11-11-1111',
                  '11.11.1111',
                  date(2001, 12, 31)]
     valid_out = [20140222, 20000922, 20121001, 12222222, 11111111,
                 11111111, 20011231]
     for inp, out in zip(valid_inp, valid_out):
         self.assertEqual(utils.int_date(inp), out)
     
     self.assertRaises(BaseException, utils.int_date, 99999999)
     self.assertRaises(BaseException, utils.int_date, '1/1/2001')
Exemplo n.º 4
0
 def __init__(self, fund_name=None, txn_type=None, amount=None, units=None,
              date=None, status=None, remarks=None):
     self.fund_name = fund_name
     if txn_type.lower() in ['purchase', 'new purchase',
                             'additional purchase']:
         self.txn_type = PURCHASE
     elif txn_type.lower() in ['redemption']:
         self.txn_type = REDEMPTION
     else:
         raise BaseException
     
     self.amount = utils.get_float(amount)
     self.units = utils.get_float(units)
     self.date = utils.int_date(date)
     self.status = status
     self.remarks = remarks
     self.fund_id = db.get_fund_id(fund_name)
     self.nav = None     # NAV on the day the transaction is performed
     self.amc = None