Out of a challenge for myself, I created this little program in which you can create "variables", test those "variable" values, test regular values, or clear all the "variables".
from operator import *
oper = {
"==": eq,
">": gt,
"<": lt,
"<=": le,
">=": ge,
"!=": one,}
var = []
def println(stringint):
print stringint
def clearvars():
del var[:]
def createvar(value):
var.append(value)
def returnbool(value1, testvalue, value2):
print testvalue(value1, value2)
Here's an example of an interpreter session. If you're curious, pcl
is the filename.
>>> import pcl
>>> pcl.createvar(1)
>>> pcl.createvar(3)
>>> pcl.println(pcl.var)
[1, 3]
>>> pcl.returnbool(pcl.var[0], pcl.oper["!="], pcl.var[1])
True
>>> pcl.clearvars()
>>> pcl.println(pcl.var)
[]
All I'm really looking for are places where I could improve on, and how I can make this compatible with Python2 & Python3.
1 Answer 1
The code is sufficiently trivial that it's hard to see where improvements could be made. The only element that isn't already compatible with Python 3 is println
(print
is a function in 3.x), which seems useless anyway - why would you do:
pcl.println(pcl.var)
rather than
print pcl.var
If you're determined to keep it,
def println(stringint):
print(stringint)
will work in 2.x and 3.x.
One step up would be to go for a class:
from operator import ne, ... # avoid 'from xyz import *'
class PCL(object):
OPERATORS = {"!=": ne, ...}
def __init__(self, vars=None): # provide initial variables (defaults to empty list)
if vars is None:
vars = []
self.vars = vars
def clear_vars(self): # create new list rather than deleting old one
self.vars = []
def create_var(self, value):
self.vars.append(value)
def return_bool(self, var1_index, op, var2_index):
return self.OPERATORS[op](self.vars[var1_index],
self.vars[var2_index])
This allows you to have multiple "sessions" alongside each other, with separate variable lists. It also removes the reliance on scoping for variable access, which will make testing easier as this grows in complexity.
I have also altered return_bool
to take the indices and operator as arguments directly, simplifying the syntax:
>>> from pcl import PCL
>>> pcl1 = PCL()
>>> pcl1.create_var(1)
>>> pcl1.create_var(3)
>>> pcl1.return_bool(0, "!=", 1)
True
This may or may not be desirable.
-
\$\begingroup\$ Just curious, why should I avoid
from whatever import *
? \$\endgroup\$Vladimir Putin– Vladimir Putin2014年07月04日 22:51:43 +00:00Commented Jul 4, 2014 at 22:51 -
1\$\begingroup\$ Per the style guide: "Wildcard imports (
from <module> import *
) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. ". See e.g. here for a real-world example. \$\endgroup\$jonrsharpe– jonrsharpe2014年07月04日 22:58:00 +00:00Commented Jul 4, 2014 at 22:58 -
\$\begingroup\$ There is one flaw with this, I want
returnbool()
to be able to return the values of regular numbers. Not just the elements in listvar
. \$\endgroup\$Vladimir Putin– Vladimir Putin2014年07月12日 17:48:01 +00:00Commented Jul 12, 2014 at 17:48 -
\$\begingroup\$ @VladimirPutin well I'll leave that minor modification to you! \$\endgroup\$jonrsharpe– jonrsharpe2014年07月12日 18:13:56 +00:00Commented Jul 12, 2014 at 18:13
-
\$\begingroup\$ I know, just wanted to let you know! \$\endgroup\$Vladimir Putin– Vladimir Putin2014年07月12日 18:48:12 +00:00Commented Jul 12, 2014 at 18:48