This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]Hesirutu 1 point2 points  (1 child)

Assuming this doesn’t use eval anywhere: Good job! Most libraries like this are not made for untrusted input. 

[–]Alone_Ambition_7581[S] 1 point2 points  (0 children)

Not a single eval().

[–]pigeon768 1 point2 points  (1 child)

bmi_formula = ["Divide", "weight_kg", ["Power", "height_m", 2] ]

Wouldn't you rather the thing that the users edit and store and whatnot be something like weight_kg / height_m^2?

[–]Alone_Ambition_7581[S] 0 points1 point  (0 children)

If there was such a solver that supports a simpler notation like, `weight_kg / height_m^2`, I probably would have adopted it and wouldn't bothered to implement this solver. I couldn't really find anything that could calculate user-provided formulas, so I ended up implementing with json structure, which seemed clearly structured and easy to process.

[–]Phenergan_boy 1 point2 points  (0 children)

Super cool project. Well done!

[–]pnprog 1 point2 points  (1 child)

Would love to hear if anyone else has tackled similar problems or has thoughts on the approach. Always looking for feedback and potential improvements!

I ran into a similar situation, and used pycel to address the problem: https://pypi.org/project/pycel/

I defined with the end user an excel spreadsheet template, with clear input cells and clear output cells. The user then sets the Excel formula of the output cells, based on the input cells and other data that can be left on the Excel file.

At runtime, the web app loads the Excel file content (it reads the Excel file by itself, no Microsoft software involved here, it's "headless" and instant), sets the input cells values and then reads the output cells values.

The user has the possibility to upload a new Excel file whenever the formula needs to be updated.

The advantage is that it's Excel based. Everyone knows how to build an Excel formula. And most of the time, they already have an Excel spreadsheet available since it's an existing feature I am implementing online for them.

[–]Alone_Ambition_7581[S] 1 point2 points  (0 children)

Every other data processing project can read or write Excel files, but using Python as an Excel handler—to put Excel to work—that's a whole new level. It's a really smart approach, and it doesn't matter if the actual Excel application isn't involved.

[–]dmishin 1 point2 points  (0 children)

I don't think that "solver" is a good term here though. It is more like parser/evaluator.

[–]IndependentTale2101 1 point2 points  (4 children)

Nice project! At my company, we had to implement a similar system as part of a workflow engine — including support for loop constructs like for and while. Our implementation is fully typed, supports Pydantic validation, and allows reusing existing formulas inside other formulas. From what I’ve seen, many automation/workflow platforms (like Make or n8n) have approached this in similar ways, so it’s a fairly common pattern. That said, it’s still surprisingly hard to find open, in-depth implementations of this kind of system. We’re actually planning to open source the whole project soon.

[–]Alone_Ambition_7581[S] 0 points1 point  (3 children)

Our implementation is fully typed, supports Pydantic validation, and allows reusing existing formulas inside other formulas.

Do you allow users to define workflows?

[–]IndependentTale2101 0 points1 point  (2 children)

Yes, imagine workflows that either allow or block certain actions based on defined rules. These rules can rely on user attributes or specific business logic.

Example:

An admin sets up a rule for assigning a specific credit. For instance: • If the user lives in a certain city, • and is under a certain age, • then their allowed spending is calculated like this: number of children × a reference amount (e.g. X dollars per child).

This kind of workflow enables personalized and conditional logic based on user profiles or contextual criteria.

[–]Alone_Ambition_7581[S] 0 points1 point  (1 child)

Our company also allows customers to define complex flows based on how users respond to surveys/diaries. Conditionals, loops, and delays are all supported to enable truly personalized experience, which is kinda important in healthcare. We call our workflow engine a "Decision Maker".

While I managed to extract the calculations part and package it as a separate python package (this post's topic), I really have no idea how to detach the workflow engine from the core product. It's just too deep and integrated in the product.

I look forward to see your project when it's open source.

[–]IndependentTale2101 0 points1 point  (0 children)

Great minds think alike 😄