Suggestion for a Python Extension
Luis Cortes
lcortes at flash.net
Wed Mar 22 21:55:05 EST 2000
Hello,
I would like to suggest an extension to python which I think would make it a
little nicer. I know there might be other ways to do this, but I think this is
pretty intuative. I have included the code for those people that might like to try
it out. I basically got the idea from Prolog. In Prolog you might have something
like this:
fact( 1, 3 ).
fact( 2, 3 ).
fact( 3, 4 ).
you can query the relation with a simple statement like so:
? fact( _X, 3 )
which returns:
fact( 1, 3 ).
fact( 2, 3 ).
I thought it might be nice to do the same with Python. Except that facts are really
lists in Python. For example:
fact = [ (1,3), (2,3), (3,4)]
ans = fact( SomeAnonymousVariable, 3 )
so that ans has:
ans = [ (1,3), (2,3) ]
I have create a couple of classes in Python to allow me to simulate what I want, but
it would be nice if it where built-in instead, using my classes the last example
would be:
fact = plist( [ (1,3), (2,3), (3,4)] )
ans = fact.query( Anonymous(), 3 )
so that ans has:
ans = [ (1,3), (2,3) ]
other examples might be:
ans = fact.query( 1, 3 )
so that ans has:
ans = [ (1,3) ]
OR:
ans = fact.query( 1, Anonymous() )
so that ans has:
ans = [ (1,3), (2,3) ]
OR
ans = fact.query( Anonymous(), Anonymous() )
so that ans has:
ans = [ (1,3), (2,3), (3,4) ]
What do you'all think?
best regards,
Luis.
P.S. Please also reply to sender with your comments, suggests, and corrections.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20000323/07334e1f/attachment.html>
-------------- next part --------------
# -------------------------------------------------------
# copyleft: Author Luis Cortes, 2000
# -------------------------------------------------------
# -------------------------------------------------------
# Empty class to select on so that I know what is an anonymous
# variable and what is not. This might be replaced by None, but
# I am not sure of the semantics of None enough to know what
# will happen in all cases. This makes it obvious.
# -------------------------------------------------------
class Anonymous:
""" Empty class """
# ------------------------------------------------------
# plist - stands for Prolog List. We are allowed to use the Anonymous class
# so that we can Query the list. Currently I have a few assumptions:
#
# 1. you must have exactly the correct number of arguments to each tuple as
# the number of arguments for query. ( this might be relaxed )
# 2. It is a separate function, it might be nice if it was part of the list data-structure.
# i.e.
# a = [ (1,2), (2,3), (5,3) ]
# b = a( Anonymous(), 3 )
#
# therefore b = [ (2,3), (5,3) ]
#
# 3. It might be nice if we could inherit from the list data-structure.
# i.e.
# class plist( type( [])):
# def __init__( ....)
# ....
#
#
# 4. I don't know if python currently supports something like this already, I think
# mxtools might have several functions that do similar things, but one nice
# property about doing it this way is that if it is built into the language, then
# we have an easy way to query a list or tuple without having to remember
# the name of another function (or functions).
#
# ------------------------------------------------------
class plist:
_oMyList=[]
def __init__( self, oList ):
assert( type( oList ) == type ([]) )
self._oMyList = oList
def query( self, *parms ):
NewTuple = []
# for all tuples in our list do ...
for grp in self._oMyList:
bNoMatch = 0
# Check All Parameters to see if 1st assumption is true
for i in range(len(grp)):
if not ( isinstance( parms[i], Anonymous ) or parms[i] == grp[i]):
bNoMatch = 1
break
if (not bNoMatch):
NewTuple.append( grp )
return NewTuple
def main():
test = plist( [ (1,2), (0,4), (1,3), (4,4),(1,5)] )
print test.query( 4, 4 )
print test.query( Anonymous(), 4 )
print test.query( 1, Anonymous())
print test.query( Anonymous(), Anonymous())
print "done"
main()
More information about the Python-list
mailing list