Extending Python by Adding Keywords & Data types
ptmcg at austin.rr.com
Thu Aug 2 01:58:03 CEST 2007
On Aug 1, 3:10 pm, Maximus Decimus <pingmaxi... at gmail.com> wrote:
> Your code snippet was quite simple and it explained me very well than
> the tutorial. HAts off to u!!
> I am doing my research in Pervasive space environments filled with
> sensors and actuators. I am just trying to modify an existing language
> which suits that kind of environment. Pervasive space environment is
> filled with sensors and actuators and everything depends on their
> state and behavior ie on the sensor readings and actuator states. So
> why not express a language variable in terms of sensors and actuators?
> THis is my idea of working around and trying to introduce new data
> types for these sensor and actuator variables which store sensor
> readings and actuator states.
> I think this explains a bit clearly on what I am looking
> for..Suggestion and ideas are kindly appreciated.
> Thanks in advance and for your time.
You got me thinking about a little exercise I did a long time ago,
using overloaded operators to help model circuits of resistors in
series and parallel. Here is one way to model some physical objects
using notation that is particular to their own domain (and even
counter to the conventional uses of that notation, as noted in the
docstrings). See below:
self.resistance = r
"""R1-R2 looks like two resistors in series, nevermind
that we are using a subtraction operator to perform
what is essentially an addition operation. Think of
the horizontal line as a circuit connecting the two
return Resistor(self.resistance + other.resistance)
"R1 | R2 looks like two resistors in parallel, sort of"
return Resistor(float(self.resistance * other.resistance) /
float(self.resistance + other.resistance))
"""insert nifty routine here to convert self.resistance to
sequence of color bands"""
return "Resistor(%s)" % self.resistance
r1 = Resistor(100)
r2 = Resistor(200)
print r1-r2 # resistors in series
print r1 | r2 # resistors in parallel
print r1 | r1 | r2 # three resistors in parallel, not so easy math
print (r1 - r1) | r2 # two resistors in series in parallel with
print r1 - (r1 | r1)
This is just using vanilla Python with some tricky operator
You can also do some syntax manipulation using the import hooks (first
shown to me by Wilson Fowlie). Here is an example of modeling an in-
memory state machine, with some customized syntax. (http://
www.geocities.com/ptmcg/python/stateMachine.html) This is more
involved, and requires a custom parser, but let's not get distracted
down that path...
Just some more alternatives to mucking about in the Python compiler.
More information about the Python-list