Beispiel #1
0
def test_examples_numbers():
    for num in range(1000):
        exp = iso4217.by_code_num(num)
        if exp is None:
            assert [] == iso4217.parse(num)
        else:
            assert [exp] == iso4217.parse(num)
Beispiel #2
0
    def _to_rur(self, obj):
        if obj['currency'] != 810:
            # Get currency:
            curr = iso4217parse.parse(int(obj['currency']))[0]

            if curr is not None:
                curr_alpha3 = curr[0]

            else:
                raise ValueError('Unknown currency code: {}'.format(
                    obj['currency']))

            curr_amount = obj['amount']
            # Convert:
            try:
                obj['amount'] = curr_amount / self.rates.loc[curr_alpha3][
                    'rate']
                # print('Converted {} {} to {} RUR'.format(curr_amount, curr_alpha3, obj['amount']))
            except KeyError as e:
                # Ex. rate not found, fake 1000 rub on tis transaction:
                self.log.warning(
                    'Rate missing for {} {}, substituted by 1000 RUR'.format(
                        obj['amount'], curr_alpha3))
                obj['amount'] = 1000

        obj['currency'] = 643
        return obj
Beispiel #3
0
def find_currency_data():
    currency_symbols = iso4217parse._symbols()
    symbols = {}
    exclusion_list = ['.', '/']
    for item in currency_symbols:
        code = iso4217parse.parse(item[0])
        # symbols.append({"'" + item[0][0] + "' : '" + code[0][0] + "'"})
        if not item[0][0].isalpha() and item[0][0] not in exclusion_list:
            symbols[item[0][0]] = code[0][0]
    return symbols
Beispiel #4
0
def test_examples_EUR():
    exp = iso4217.Currency(
        alpha3='EUR',
        code_num=978,
        name='Euro',
        symbols=['€', 'euro', 'euros'],
        minor=2,
        countries=[
            'AD', 'AT', 'AX', 'BE', 'BL', 'CY', 'DE', 'EE', 'ES', 'FI', 'FR',
            'GF', 'GP', 'GR', 'IE', 'IT', 'LT', 'LU', 'LV', 'MC', 'ME', 'MF',
            'MQ', 'MT', 'NL', 'PM', 'PT', 'RE', 'SI', 'SK', 'SM', 'TF', 'VA',
            'XK', 'YT'
        ],
    )

    assert [exp] == iso4217.parse('Price is 5 €')
    assert [exp] == iso4217.parse('Price is 5 EUR')
    assert [exp] == iso4217.parse('Price is 5 eur')
    assert [exp] == iso4217.parse('Price is 5 euro')
    assert [exp] == iso4217.parse('Price is 5 Euro')

    exp = iso4217.Currency(
        alpha3='CAD',
        code_num=124,
        name='Canadian dollar',
        symbols=[
            'CA$', 'CA$', '$', '$', 'dollar', 'dollars', 'Dollar', 'Dollars',
            'CA﹩', '﹩'
        ],
        minor=2,
        countries=['CA'],
    )

    assert [exp] == iso4217.parse('CA﹩15.76')
def ocr_core(filename):
    """
    This function will handle the core OCR processing of images.
    """
    img = _convert_image_to_bytes(filename)

    data = pytesseract.image_to_string(img)
    log.debug('Data: %s', data)
    receipt = Receipt()
    products = []
    previous_line = None
    iso4217 = None
    for _line in data.split('\n')[1:]:
        if len(_line) > 2 and (not _line.isspace()):
            _line = _line.replace(". ", ".").replace(" .", ".")
            if receipt.name is None and not _line.__contains__('CASH SALE'):
                receipt.name = _line
            elif re_total.match(_line) and receipt.total is None:
                receipt.total = float(re_non_decimal_number.sub("", _line))
            elif re_date.match(_line) and receipt.date is None:
                receipt.date = _line
                break
            elif re_decimal_number.match(_line) and receipt.total is None:
                product = Product()
                if iso4217 is None:
                    iso4217 = iso4217parse.parse(_line)
                currency_name = " "
                if iso4217:
                    currency_symbol = iso4217[0].symbols[0]
                    currency_name = iso4217[0].alpha3
                    product.currency = currency_symbol
                line_split = _line.split(currency_name)
                if len(line_split) == 2:
                    product.name = line_split[0].strip()
                    product.quantity = 1
                    product.total_price = float(
                        re_non_decimal_number.sub(
                            "", line_split[1].replace(" ", ".")))
                    product.quantity_price = product.total_price
                else:
                    _process_quantity_product(product, previous_line,
                                              currency_symbol, currency_name,
                                              _line)

                products.append(product)
                receipt.products = products
            previous_line = _line
    log.debug('Receipt: %s', receipt)
    return receipt
Beispiel #6
0
def test_invalid():
    for v in (None, [], {}, 3.14):
        with pytest.raises(ValueError):
            iso4217.parse(v)
Beispiel #7
0
def test_examples():
    exp = iso4217.Currency(
        alpha3='CHF',
        code_num=756,
        name='Swiss franc',
        symbols=[
            'SFr.', 'fr', 'Fr.', 'F', 'franc', 'francs', 'Franc', 'Francs'
        ],
        minor=2,
        countries=['CH', 'LI'],
    )

    assert [exp] == iso4217.parse('CHF')

    exp = iso4217.Currency(
        alpha3='CUP',
        code_num=192,
        name='Cuban peso',
        symbols=[
            '₱', '$', '﹩', '$', 'dollar', 'dollars', 'Dollar', 'Dollars',
            '$MN', '﹩MN', '$MN'
        ],
        minor=2,
        countries=['CU'],
    )

    assert [exp] == iso4217.parse(192)

    exp = iso4217.Currency(
        alpha3='EUR',
        code_num=978,
        name='Euro',
        symbols=['€', 'euro', 'euros'],
        minor=2,
        countries=[
            'AD', 'AT', 'AX', 'BE', 'BL', 'CY', 'DE', 'EE', 'ES', 'FI', 'FR',
            'GF', 'GP', 'GR', 'IE', 'IT', 'LT', 'LU', 'LV', 'MC', 'ME', 'MF',
            'MQ', 'MT', 'NL', 'PM', 'PT', 'RE', 'SI', 'SK', 'SM', 'TF', 'VA',
            'XK', 'YT'
        ],
    )

    assert [exp] == iso4217.parse('Price is 5 €')
    assert [exp] == iso4217.parse('Price is 5 EUR')
    assert [exp] == iso4217.parse('Price is 5 eur')
    assert [exp] == iso4217.parse('Price is 5 euro')
    assert [exp] == iso4217.parse('Price is 5 Euro')

    exp = iso4217.Currency(
        alpha3='CAD',
        code_num=124,
        name='Canadian dollar',
        symbols=[
            'CA$', 'CA$', '$', '$', 'dollar', 'dollars', 'Dollar', 'Dollars',
            'CA﹩', '﹩'
        ],
        minor=2,
        countries=['CA'],
    )

    assert [exp] == iso4217.parse('CA﹩15.76')
Beispiel #8
0
def test_examples_CZK():
    expect = iso4217.by_alpha3('CZK')

    assert [expect] == iso4217.parse('1499 CZK')
Beispiel #9
0
def test_examples_code():
    for code, exp in iso4217._data()['alpha3'].items():
        assert [exp] == iso4217.parse(code)