PythonCard http://pythoncard.sourceforge.net/ AFAIK still has the most complete turtle geometry routines for Python, though Guido's turtle.py included with Python is fine if you're using tkinter. I think Guido experimented with the PythonCard turtle, so maybe he can chime in on what he liked and disliked and what improvements he would like to see. There are over twenty scripts included with the turtle sample, the docs directory even includes documentation on Logo commands and the most important issues to be aware of while converting old Logo programs to Python. You can have as many turtles as you want, each is independent, so you should be able to do artificial life experiments similar to what is possible with Starlogo. You can drive the turtle from the shell which has command-completion as well as toolTips for all of the turtle commands. There are screen shots at: http://pythoncard.sourceforge.net/samples2.html I've brought up the the turtle library before on this list, but didn't get much feedback, so I assumed there wasn't any interest. Turtles are fun, so I'm happy to work on this part of PythonCard. I would love to get feedback from people interested in using the turtle library with PythonCard. Note that there is an abstract turtle class, which is suitable for making a tkinter, wxPython, PostScript or serial-port based turtle, say for driving a Legos robot. The abstract turtle api will be improved along with the PythonCard turtle api depending on user demand. In fact, on a related note, in preparation for making SciPy and PythonCard more integrated (SciPy already works with PythonCard and the PyCrust shell) I'm integrating the turtle directly into the PythonCard framework. I posted a message to the list about this last night: http://aspn.activestate.com/ASPN/Mail/Message/PythonCard/802606 The latest release version of PythonCard is always at: http://sourceforge.net/project/showfiles.php?group_id=19015 PythonCard requires Python 2.1 or higher and wxPython 2.3.x, and yes I bug Robin regularly about the Mac port. ka --- Kevin Altis altis@semi-retired.com
At 08:59 AM 10/13/2001 -0700, Kevin Altis wrote:
There are screen shots at: http://pythoncard.sourceforge.net/samples2.html
Impressive. Kirby
Here's a simple boolean algebra module, for which "useless Python" I'll no doubt find many more capable versions (haven't checked yet). Still, I like this one because it includes a broad range of Python concepts in not-too-difficult form, namely: * a simple class: the Bool class does little more than override the behavior of __invert__ for integers, so instead of getting two's complement, I get the logical negation, i.e. I say '~a' instead of 'not a'. * a regular expression: really basic, but used to find all occurances of a single letter variable, and replace it with ~variable. This version only allows up to four variables a-d. * plenty of string manipulation, which is the nuts and bolts of a lot of programming * an example of eval() with a local namespace Here's how it looks, used in shell mode:
reload(boolean) <module 'boolean' from 'D:\PROGRAM FILES\PYTHON22\work\boolean.py'>
boolean.truth(1,'~a')
a | ~a ==|=== 0 | 1 1 | 0
expr = "(a & ~b) | c" boolean.truth(3,expr)
a b c | (a & ~b) | c ======|============= 0 0 0 | 0 0 0 1 | 1 0 1 0 | 0 0 1 1 | 1 1 0 0 | 1 1 0 1 | 1 1 1 0 | 0 1 1 1 | 1
newexpr = boolean.demorgan(expr) newexpr '~(((~a) | ~(~b)) & (~c))' boolean.truth(3,newexpr)
a b c | ~(((~a) | ~(~b)) & (~c)) ======|========================= 0 0 0 | 0 0 0 1 | 1 0 1 0 | 0 0 1 1 | 1 1 0 0 | 1 1 0 1 | 1 1 1 0 | 0 1 1 1 | 1 As an intro to computer science module, it's got some other key stuff too, in addition to exercising the above features of Python (which features are characteristic of many other languages). It's got decimal to binary conversion...
from boolean import bin bin(10) '1010' bin(100) '1100100' bin(3242034) '1100010111100000110010'
And it's got this DeMorgan's equivalence relation, whereby you can express the same logical expression in two different ways. CS students know what I'm talking about. That being said, I'm sure the code could be enchanced/improved, features added and so on (I'm working on an improvement or two right now). So, have at it... Kirby ===== """ Simple boolean algebra module by Kirby Urner, Oct 13, 2001 """ from string import replace, center from re import sub class Bool: """ Intialize w/ 1 or 0: a,b = Bool(1), Bool(0) Example expressions: a&b, a|b, ~a (note: ~ for negation) returns a Bool """ def __init__(self,val): if not (val==0 or val==1): raise ValueError,"Must be 1 or 0" self.value = val def __invert__(self): return Bool(not self.value) def __and__(self,other): return Bool(self.value & other.value) def __or__(self,other): return Bool(self.value | other.value) def __repr__(self): return repr(self.value) def demorgan(expr): """ Swap & with |, negate args, negate expression -- should have same truth table as original """ def negate(matchobj): t = matchobj.group(0) return '(~%s)' % t expr = replace(expr,'&','@') expr = replace(expr,'|','&') expr = replace(expr,'@','|') expr = sub('[a-d]',negate,expr) return "~("+expr+")" def bin(n): result = '' while 1: result = str(n%2)+result n = n//2 if not n: break return result def truth(n,expr): """ Generates truth table for n=1-4 variables a-d, for the passed boolean expression expr """ truth_table = [] locals = {} padding = n*'0' for i in range(2**n): digits = (padding + bin(i))[-n:] truth_table.append(tuple(digits)) print print " "+("%s "*n) % tuple('abcd')[:n] + "| " + expr print " "+"=="*n + "|=" + "="*len(expr) for row in truth_table: for i in range(n): locals['abcd'[i]]=Bool(int(row[i])) print " "+("%s "*n) % row + "| " + \ center(str(eval(expr,locals)),len(expr)) print =====
Hi Kirby, I've written a small "propositional logic" parser class that you might find useful: http://hkn.eecs.berkeley.edu/~dyoo/python/propositions/ I wrote it as an experiment with the Spark parser-generator a month ago. I haven't been working on it for a while now, and you're welcome to use the parser if it's useful for you. Here's a small sample of what it does: ### [dyoo@tesuque propositions-0.2]$ python parser.py This is a small test of the propositional parser. Enter propositional phrases at the prompt. To quit, type "quit" at the prompt. Examples of some phrases: jack and jill rock and rolling implies not moss [ParserTest] >>> a and not b or c or[and[a, not[b]], c] ### The parser tries to be nice by allowing both English and "C"-ish syntax, and sticks with precedence rules to reduce the number of parentheses needed to disambiguate the expressions. I actually have an updated version 0.3 on my laptop that should work better than what's on the web site, and I'll put it up tonight when I get the chance. (Version 0.2 is actually a little broken: it has a logic bug that I should investigate.) Hope this helps!
participants (3)
-
Danny Yoo -
Kevin Altis -
Kirby Urner