Link to home
Start Free TrialLog in
Avatar of Evan Cutler
Evan CutlerFlag for United States of America

asked on

how to use Python lists to create dynamic calculations

Greetings,
I am trying to use python to create a lookup table.  The lookup table is in a csv right now.
example:
Category1, .15xPrice + 2.00
Category2, .18xPrice + 2.00

The lookup locates the Category, and the second column is the algorithm that is executed against a given value assigned to the the variable Price.

I do not know how to execute this.

For example:
PRice = 29.95
Category = Category1

THe output would be (29.95 * .15) + 2.00 or 6.4925
How do I accomplish this?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nas-Banov
Nas-Banov

@pepr, i did not see your solution when i wrote mine - there was no answer when i started writing it; i wouldn't have done double-take if i saw yours.

Now, i tried to balance simplicity and correctness for a novice level. But your note about "more dangerous" does not apply:
Even without with, the file will be closed automatically right after the for loop (you can check, files are fine w/o with).
Using eval() sure has dangers but you are using it too and formula.replace() substitution is not making it safer
replacing letter 'x' with '*' will cause havoc if there are formulas like '.23*fee + extra'
@Nas-Banov: No problem with your answer ;) I believe that Evan is novice only concerning Python, not a novice programmer.

The file is closed automatically because the program is terminated, not because it leaves the for loop. Or possibly you may be lucky and the file object is garbage collected. However, it is not a good practice. The with was designed for exactly such situations. How did you check that the file is closed by the for loop?

I did mention the problem with `x` (that it can be a part of an identifier). This is also reason why formulas should be specifically interpreted.

When the substitution of Price in the formula is done, one can add a check that the formula after the substitution does not contain any letters. In such case, the eval is quite safe.
@pepr - i like your idea about checking for remaining letters re securing eval! The file will be closed because its reference count becomes 0 after the loop and then it gets immediately disposed, destructor called etc. Python uses reference counting (plus mark&sweep for cyclic structures). Google it - but let's end discussion here.

As a practical matter, i advise @Evan Cutler to ignore our nit-picking here, just look at out first answers and make use of it.
Avatar of Evan Cutler

ASKER

Guys,
This was the best discussion ever!!

Thank you so much for your help.  
These solutions worked perfectly and I got my script started.
When I submitted my script to my client, he came at me and told me that the categories might fuzzy match the lookup categories.
This means I have titles that might be part of titles in both places.
But that question I'll give another 500 for.
Thanks guys.
Evan