Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formula Parsing Error #160

Open
agoddijn opened this issue Feb 20, 2024 · 1 comment
Open

Formula Parsing Error #160

agoddijn opened this issue Feb 20, 2024 · 1 comment

Comments

@agoddijn
Copy link

What actually happened

[Please include the full traceback if there was an exception]

Traceback (most recent call last):
File "/opt/homebrew/lib/python3.11/site-packages/pycel/excelformula.py", line 923, in eval_func
excel_formula.compiled_lambda())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/pycel/excelformula.py", line 948, in
### Traceback will show this line if not loaded from a text file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/pycel/lib/stats.py", line 203, in countif
valid = find_corresponding_index(rng, criteria)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/pycel/excelutil.py", line 1104, in find_corresponding_index
return tuple(find_corresponding_index_generator(rng, criteria))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/pycel/excelutil.py", line 1113, in
for c, item in enumerate(row) if check(item))
^^^^^^^^^^^
File "/opt/homebrew/lib/python3.11/site-packages/pycel/excelutil.py", line 1031, in
return lambda x: x is not None and compiled.match(x.lower()) is not None
^^^^^^^
AttributeError: 'int' object has no attribute 'lower'

What was expected to happen

If I cast x to string before calling lower, I get the right output

Problem description

[If needed, this should explain why the current behavior is a problem and why the expected output is a better solution]

Evaluation is failing when it shouldn't be

Code Sample

# Your code here
workbook = openpyxl.load_workbook(tmp_xlsx)
workbook_compiler = ExcelCompiler(excel=workbook)
workbook_compiler.evaluate(get_cell_coordinate(cell, sheet_name))

[If possible, include a minimal, complete, and verifiable example to help us identify the issue. This also helps check that the issue is not with your own code]

Environment

[Pycel Version, Python Version and OS used. Also any other environment details that you think might be relevant]

Python 3.11
Pycel 1.0b30

@agoddijn
Copy link
Author

agoddijn commented Feb 20, 2024

I monkey patched build_wildcard_re in my code which solved my issues, but really didn't get too deep into the weeds here because I just need my thing to work

# ========= Start Monkey Patch =========
QUESTION_MARK_RE = re.compile(r'\?(?<!~)')
STAR_RE = re.compile(r'\*(?<!~)')

def build_wildcard_re_fixed(lookup_value):
    regex = QUESTION_MARK_RE.sub('.', STAR_RE.sub('.*', lookup_value))
    if regex != lookup_value:
        # this will be a regex match"""
        compiled = re.compile(f'^{regex.lower()}$')
        return lambda x: x is not None and compiled.match(str(x).lower()) is not None
    else:
        return None

excelutil.build_wildcard_re = build_wildcard_re_fixed
# ========= End Monkey Patch ========= 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant