4
\$\begingroup\$

I wrote a calculator that can solve equations with variables using matplotlib and sympy. I am looking for tips on performance and style. How can I improve on this calculator? Is it a good idea to use sympy for this purpose? Repl.it

import sympy
import matplotlib.pyplot as plt
class Iterator:
 name, equation, val = None, None, None
 def __repr__(self):
 return self.__str__()
 def __init__(self, nameIn, equationIn, valIn):
 self.name, self.equation, self.val = nameIn, equationIn, valIn
 def __str__(self):
 return self.get_name() + "," + str(self.get_value())
 def iterate(self):
 replaced = self.equation.replace(self.name, str(self.val))
 self.val = sympy.sympify(replaced)
 def get_name(self):
 return self.name
 def get_value(self):
 return self.val
def graph():
 numTimes = input("How many points ")
 x = input("What to calculate ")
 points = []
 i = 0
 numInputs = int(input("How many variables "))
 j = 0
 replaced = []
 iterators = []
 while numInputs > j:
 name = input("What's the variabe name ")
 iter = input("What's the iteration equation ")
 val = int(input("What's the starting value "))
 iterators.append(Iterator(name, iter, val))
 j += 1
 while i < int(numTimes):
 replaced = x.replace("n", str(i))
 for j in iterators:
 replaced = replaced.replace(j.get_name(), str(j.get_value()))
 j.iterate()
 final = sympy.sympify(replaced)
 right_type = int(final)
 points.append(right_type)
 i += 1
 plt.plot(points)
 plt.ylabel(input("What's the y label "))
 plt.show()
while(True):
 command = input(">> ")
 if command == "graph":
 graph()
 elif command == "help":
 print("""
 msh.
 Used for graphing.
 Commands:
 graph: an interactive graphing calculator
 help: bring up this message
 quit: exit msh
 """)
 elif command == "quit":
 break
asked Sep 29, 2020 at 17:34
\$\endgroup\$
1
  • 4
    \$\begingroup\$ In Python, indentation is important. Please fix your indentation. Why did you create a class if it's basically an initialisation and one big function after that? \$\endgroup\$ Commented Sep 29, 2020 at 18:24

1 Answer 1

3
\$\begingroup\$

Indentation

You should use four spaces instead of two.

Spacing

Your class methods should be separated by an empty line, so everything doesn't look so crowded.

Getters

Since all class variables are public (except _var and __var variables), you don't need to define getters to access these properties. Simple do Iterator.name, etc.

Iterator.__str__

You should utilize f"" strings to directly include variables in strings.

def __str__(self):
 return f"{self.name},{self.val}"
answered Dec 18, 2020 at 9:50
\$\endgroup\$
2
  • \$\begingroup\$ But doesn't it make sense to make all variables "private" (__var or at least _var) and provide setters and getters with @property? \$\endgroup\$ Commented Dec 18, 2020 at 10:10
  • 1
    \$\begingroup\$ @TomGebel Not really. There's no structural advantage to making something private (which isn't enforced in Python anyway) and then offering for anyone to set it to anything; public accomplishes the same thing and is more Python-idiomatic. \$\endgroup\$ Commented Dec 18, 2020 at 15:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.