HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Minimal spreadsheet program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
minimalspreadsheetprogram

Problem

I wrote a very minimal spreadsheet program in Python with tkinter.

Here I present the help text:


You can use this program to calculate neatly in table format.


SPACES ARE MANDATORY:

5 + B1 =             # Is correct
5+B1=                # Is wrong




In order to perform the calculation please end the formula with an
equal sign = and press the ENTER or RETURN key.


What you write it will be executed as Python code, so you

* Can use all built-in functions and those in the math module cell names must be surrounded by spaces, like min(3, B5 )

* Must be careful not to hand to this program to ill-intentioned people.


If a cell is modified, cells depending on it are NOT automatically
updated. You must go back to them, delete what's after the = sign
and press enter again.


Load and save work with .csv format.

Any and all criticism is welcome, I most prefer simplifications and ways to reduce repetition.

Here is a screenshot, (the above window is what pops-up when you press the HELP button):

``
try:
import tkinter as tk
import filedialog as filedialog
except ImportError:
import Tkinter as tk
import tkFileDialog as filedialog

from string import ascii_uppercase
import math

HELP_TEXT ="""
Welcome to the minimal spreadsheet.

You can use this program to calculate neatly in table format.

SPACES ARE MANDATORY:

5 + B1 = # Is correct
5+B1= # Is wrong

In order to perform the calculation please end the formula with
an equal sign
= and press the ENTER or RETURN key.

What you write it will be executed as Python code, so you
* Can use all built-in functions and those in the math module**
** cell names must be surrounded by spaces, like
min(3, B5 )
* Must be careful not to hand to this program to ill-intentioned people.

If a cell is modified, cells depending on it are NOT automatically updated.
You must go back to them, delete what's after the
=` sign an

Solution

is_reference

The possibility of an IndexError can be mostly eliminated by reordering the conditions:

return (len(t) == 2 or t[-1] in "1234567890") and t[0] in ascii_uppercase and t[1] in "1234567890"


The only case this doesn't handle is the empty t.
For that, you can add t and at the start,
that way the code will be perfectly safe,
never raising an IndexError.

Another issue in this function is checking if a symbol is a letter or a digit. Using in for this is not great, as it may iterate over all the letters in the alphabet and digits. It would be better to rewrite using ranges:

return t and (len(t) == 2 or '0' <= t[-1] <= '9') and 'A' <= t[0] <= 'Z' and '0' <= t[1] <= '9'


Use the print() function

It's good to use the print() function instead of the print statement.
It should be available in any modern 2.x version,
it will keep the code closer to being 3.x-ready.

Code Snippets

return (len(t) == 2 or t[-1] in "1234567890") and t[0] in ascii_uppercase and t[1] in "1234567890"
return t and (len(t) == 2 or '0' <= t[-1] <= '9') and 'A' <= t[0] <= 'Z' and '0' <= t[1] <= '9'

Context

StackExchange Code Review Q#152506, answer score: 4

Revisions (0)

No revisions yet.