473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Extending Python by Adding Keywords & Data types

HI all,

I am using python v2.5 and I am an amateur working on python. I am
extending python for my research work and would like some help and
guidance w.r.t this matter from you experienced python developers.

II want to add some more KEYWORDS and DATATYPES into the python script
apart from the existing ones.

It would be really great if anybody could guide me as which files and
modules I need to look to make these additions and enhancements to the
existing python version. Thanks a lot in advance and your help is
really appreciated.

Vishak

Jul 31 '07 #1
7 3365
On Tue, 31 Jul 2007 20:28:59 +0000, Maximus Decimus wrote:
II want to add some more KEYWORDS and DATATYPES into the python script
apart from the existing ones.
New data types are easy: Just implement the classes.

Why do you need new keywords? Can't the problem at hand be expressed in
functions and classes/methods?

Ciao,
Marc 'BlackJack' Rintsch
Jul 31 '07 #2
Since, I am an amateur in using python, could you please be more
specific. For new data types, you had asked to implement the classes.
I intend to use C for implementing these data types. So where do i
need to implement these classes ie under which file or module in the
python package.

Thanks in advance.
On Jul 31, 5:16 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Tue, 31 Jul 2007 20:28:59 +0000, Maximus Decimus wrote:
II want to add some more KEYWORDS and DATATYPES into the python script
apart from the existing ones.

New data types are easy: Just implement the classes.

Why do you need new keywords? Can't the problem at hand be expressed in
functions and classes/methods?

Ciao,
Marc 'BlackJack' Rintsch

Aug 1 '07 #3
On Wed, 01 Aug 2007 01:42:38 +0000, Maximus Decimus wrote:
Since, I am an amateur in using python, could you please be more
specific. For new data types, you had asked to implement the classes.
I intend to use C for implementing these data types. So where do i
need to implement these classes ie under which file or module in the
python package.
Like Ben Finney said, first do something about the amateur status and work
through the tutorial.

Everything in Python is an object. And classes are a way to define
templates for new objects. After you know classes better, ask yourself
how you would implement the already existing data types with classes
yourself and then you'll get an idea how to create new data types.

If you want to implement new types in C there's a tutorial in the docs
called “Extending and Embedding” and a reference of the Python/C API.

But maybe you should start with implementing them in Python and only
convert those parts to C code that are *really* to slow in the prototype.
Really means measure and don't guess.

A nice step between Python and C is Pyrex, a Python-like language with some
restrictions but also C data types. Pyrex is then translated to C and can
be compiled as Python extension module.

Ciao,
Marc 'BlackJack' Rintsch
Aug 1 '07 #4
Maximus Decimus wrote:
Since, I am an amateur in using python,
<http://en.wikipedia.org/wiki/Amateur>
| Most commonly an amateur is understood to be someone who does
| something without pay or formal training. Conversely, a
| professional is someone who has received training in a particular
| area and who also makes a living from it.

So, you're not being paid coding in Python, and/or have
no "official" training. Many readers of this list share this
situation, me too.
could you please be more specific.
I return the question. Your statements are most vague. Please give
an example of what you try to realise.
For new data types, you had asked to implement the classes. I
intend to use C for implementing these data types. So where do i
need to implement these classes ie under which file or module in
the python package.
Please do yourself a favor and read. Feel free to ask again for
specific problems.

http://docs.python.org/tut/node3.html
http://docs.python.org/tut/node11.html

Regards,
Bjrn
--
BOFH excuse #135:

You put the disk in upside down.

Aug 1 '07 #5
On Jul 31, 3:28 pm, Maximus Decimus <vishak.1...@gmail.comwrote:
>
I am using python v2.5 and I am an amateur working on python. I am
extending python for my research work and would like some help and
guidance w.r.t this matter from you experienced python developers.

II want to add some more KEYWORDS and DATATYPES into the python script
apart from the existing ones.
Vishak -

Let me echo the sentiments of the other posters who replied to your
message. It is *very* unusual for a new Python user to begin their
Pythonic journey by modifying the Python interpreter to add new
keywords and datatypes. Python is designed to be a very flexible and
extensible language just as it is.

Since you are familiar with C, your question strikes those of us on
this group as one who would write "I'm just learning C and I would
like to modify the core library to add some new functions for my
research." And all the readers on comp.lang.c scratch their heads,
thinking "Huh? Why doesn't this guy just write the functions in his
own program, the way the language was designed for him to do in the
first place?"

Now maybe your research is in the field of language design, and has
something to do with the relative ease/difficulty of modifying
computer languages. Then it would make sense for you to dive right in
to learning how to modify Python's compiler.

But if your research was in just about any other field (from finite
elements analysis to genetic programming to genetics to simulations to
process control to whatever), DON'T start by modifying Python - start
by LEARNING Python.

When a new datatype is required in a Python program, then the
programmer writes a class to implement this datatype. The class
itself is written in Python, but it can be used just like any built-in
datatype. For instance, here is a new datatype I just thought up - a
Box that can hold up to 'n' objects.

class Box(object):
def __init__(self,n):
self.capacity = n
self.contents = []

def add(self,other):
if len(self.contents) < self.capacity:
self.contents.append( other )
else:
raise ValueError("can't add any more to this Box")

def __iadd__(self,other):
self.add(other)
return self

box = Box(3)

# add stuff to the box until it overflows
while(True):
box += object()
Voila! I created a new datatype, Box, and even added support for it
to understand how to use the '+=' operator so that adding objects to
the Box looks like an addition statement. All without modifying
Python itself. (Here's an exercise - extend this example so that the
Box has a limited capacity in the *weight* of the added objects, as
well as in the number of objects.)

This is the basic way that one extends Python with their own new
datatypes and methods. New keywords are a little rarer, but really,
start by just adding methods, like the add method above. Python users
around the world develop a wide range of applications and programs
using just these techniques, and never touch the Python compiler
itself.

And here is a case you'd like to avoid. Let's say you start by
learning how to modify Python because you need a general-purpose
container for things. You spend two weeks learning how to do this,
getting your code mostly debugged, and then you post to
comp.lang.python your proud achievement. Immediately the replies come
back, "Congratulations, newbie, you just reinvented the built-in list
type." Without LEARNING Python, you wont know what is already
provided in the language.

So, in general this is a helpful group, and it is possible that you DO
need to learn how to add datatypes and keywords to Python as your
first objective. We're not trying to pry, but give us a bit more
detail. Someone might even rough out for you what one of your new
datatypes might look like. So pray, tell us what sort of specialized
datatypes and keywords do you think you need to add, and we'll try to
point you in one or more directions.

-- Paul

Aug 1 '07 #6
On Aug 1, 3:10 pm, Maximus Decimus <pingmaxi...@gmail.comwrote:
>
Your code snippet was quite simple and it explained me very well than
the tutorial. HAts off to u!!
Thanks!

<snip>
>
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:

class Resistor(object):
def __init__(self,r):
self.resistance = r

def __sub__(self,other):
"""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
resistors."""
return Resistor(self.resistance + other.resistance)

def __or__(self,other):
"R1 | R2 looks like two resistors in parallel, sort of"
return Resistor(float(self.resistance * other.resistance) /
float(self.resistance + other.resistance))

def colorCode(self):
"""insert nifty routine here to convert self.resistance to
sequence of color bands"""
pass

def __repr__(self):
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
another
print r1 - (r1 | r1)

Prints:

Resistor(300)
Resistor(66.6666666667)
Resistor(40.0)
Resistor(100.0)
Resistor(150.0)

This is just using vanilla Python with some tricky operator
interpreting.

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.

-- Paul

Aug 2 '07 #7
On Aug 1, 8:08 am, Paul McGuire <pt...@austin.rr.comwrote:
>
def add(self,other):
if len(self.contents) < self.capacity:
self.contents.append( other )
else:
raise ValueError("can't add any more to this Box")
I guess that really should raise an OverflowError...

-- Paul

Aug 2 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

75
by: David MacQuigg | last post by:
Seems like we need a simple way to extend Python syntax that doesn't break existing syntax or clash with any other syntax in Python, is easy to type, easy to read, and is clearly distinct from the...
0
by: todddeluca | last post by:
I am posting code for calling almost any python function from php, because it seems generally useful. Please feel free to suggest improvements or tell me this has already been done better...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.