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
-
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\$Mast– Mast ♦2020年09月29日 18:24:24 +00:00Commented Sep 29, 2020 at 18:24
1 Answer 1
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}"
-
\$\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\$Tom Gebel– Tom Gebel2020年12月18日 10:10:44 +00:00Commented 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\$Reinderien– Reinderien2020年12月18日 15:08:13 +00:00Commented Dec 18, 2020 at 15:08
Explore related questions
See similar questions with these tags.