Example #1
0
def parse_xml(tree):
    """
    try to parse XML from tree
    :param tree: XML
    :return: user
    """
    # get root
    root = tree.getroot()
    # creates new user
    ret_user = User()
    # set his money
    ret_user.set_money(decimal.Decimal(root.attrib['money']))

    for account in root[0]:
        # create new account
        new_account = Accounting()
        # set its description
        new_account.set_description(account.attrib['description'])
        # set new sum
        new_account.set_sum(decimal.Decimal(account.attrib['price']))
        # get datetime
        dtime = account[0]
        # set datetime
        new_account.set_datetime(datetime.datetime(int(dtime.attrib['year']),
                                                   int(dtime.attrib['month']),
                                                   int(dtime.attrib['day'])))
        # set this payment to user
        ret_user.add_payment(new_account)
        # add money
        ret_user.add_money(-new_account.get_sum())

    return ret_user
Example #2
0
def encode_dict(dict):
    """
    decodes user from dictionary
    :param dict: decoded user
    :return: new user
    """
    # create new user
    ret_user = User()
    # set his money
    ret_user.set_money(decimal.Decimal(dict['money']))
    for account in dict['payment list']:
        # set new accounting
        tmp_account = Accounting()
        date = account['datetime']
        # set datetime
        tmp_account.set_datetime(datetime.datetime(date['year'],
                                                   date['month'],
                                                   date['day']))
        # set sum
        tmp_account.set_sum(decimal.Decimal(account['sum']))
        # set description
        tmp_account.set_description(account['description'])
        # add account
        ret_user.add_payment(tmp_account)
        ret_user.add_money(-tmp_account.get_sum())
    return ret_user
Example #3
0
def encode_dict(dict):
    """
    decodes user from dictionary
    :param dict: decoded user
    :return: new user
    """
    # create new user
    ret_user = User()
    # set his money
    ret_user.set_money(decimal.Decimal(dict['money']))
    for account in dict['payment list']:
        # set new accounting
        tmp_account = Accounting()
        date = account['datetime']
        # set datetime
        tmp_account.set_datetime(datetime.datetime(date['year'],
                                                   date['month'],
                                                   date['day']))
        # set sum
        tmp_account.set_sum(decimal.Decimal(account['sum']))
        # set description
        tmp_account.set_description(account['description'])
        # add account
        ret_user.add_payment(tmp_account)
        ret_user.add_money(-tmp_account.get_sum())
    return ret_user