0

I want to make a kind of simple spreadsheet with python.

I need to parse a formula from a string.

All the operations I need for now are: + - * / ^ ()

Formulas will be always starting with '='.

Examples of input:

  1. =4+8-6/2 this is simple: just usig eval()

2.=4b+12*(2+5) where '4b' is a link to another cell in spreadsheet (a variable).

** It's possible to make all the links like 'b4' instead of '4b'

The script would substitute the link (variable) with corresponding value.

What I'm unable to achive is to make parser 'understand' variables like '4b' or 'b4', the rest is pretty simple

What would you suggest?

P.S. I'm new to Python. Tried pyparsing but it's to complicated to use or to correct custom examples according to my needs. Hope to find more simple solution

3
  • 1
    Check out this Python spreadsheet recipe (and the comments). Commented Jul 8, 2013 at 8:12
  • @martineau why you wrote a comment instead of answer? I'd set it as an accepted answer :) Commented Jul 8, 2013 at 12:04
  • OK, that's good news. I didn't write it up as an answer because I only saw your question a few minutes before I was about to log-off for an extended amount of time. Commented Jul 8, 2013 at 13:20

2 Answers 2

1

ActiveState has an amazingly simple recipe for a Python-based spreadsheet that meets most of your requirements, I believe. Its class SpreadSheet is defined in terms of a couple of internal dictionaries along with a very small number of relatively short methods.

The related comments are also very interesting and show how it could be extended and made to minimize potential security issues. I highly recommend you take a look at it.

A major limitation I noted about it is that there's no dependency checking, so updating a cell doesn't automatically update any that might be depend upon it, and any that might be dependent on those, etc.

Sign up to request clarification or add additional context in comments.

Comments

0

Lambda expressions to create anonymous functions would be an efficient way to handle this. I would recommend checking out some of Peter Norvig's Udacity course - The design of computer programs. As part of lesson 2 he covers a scenario very similar to this looking at cryptarithmatic. The course is free, and self paced so you can dip in and skip around lessons / sub lessons as much as you need:

https://www.udacity.com/course/cs212

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.