subclassing list question

2007-05-20 Thread manstey
Hi,

I have a simple class that subclasses list:

class CaListOfObj(list):
 subclass of list 
def __init__(self, *args, **kwargs):
list.__init__(self, *args, **kwargs)

a = CaListOfObj([1,2,3])

I want an instance method to be run EVERY time a is modified. Is this
possible?

Thanks,
Matthew

-- 
http://mail.python.org/mailman/listinfo/python-list


subclassing list question

2007-05-20 Thread manstey
Hi,

I have a simple class that subclasses a list:

class CaListOfObj(list):
 subclass of list 
def __init__(self, *args, **kwargs):
list.__init__(self, *args, **kwargs)

a= CaListOfObj([1,2,3])

Is it possible to have a method in the class that is called EVERY time
a is modified?

Thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


subclassing list question

2007-05-20 Thread manstey
test

-- 
http://mail.python.org/mailman/listinfo/python-list


list modification subclassing

2007-05-20 Thread manstey
Hi,

I have a simple subclass of a list:

class CaListOfObj(list):
 subclass of list 
def __init__(self, *args, **kwargs):
list.__init__(self, *args, **kwargs)

a= CaListOfObj([1,2,3])

How do I write a method that does something EVERY time a is modified?

Thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


replacing one instance with another

2007-03-25 Thread manstey
Hi,
I'm not sure why this doesn't work:

dic_myinstances={}

class MyClass(object):
def __init__(self, id):
 global dic_myinstances
 if dic_myinstances.has_key(id):
  self = dic_myinstances[id]
 else:
  dic_myinstances[id] = self

ins1 = MyClass('xx')
ins2 = MyClass('yy')
ins3 = MyClass('xx')
ins3 is ins1
False

Why isn't ins3 the same as ins1? How can I get this to work?

thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: replacing one instance with another

2007-03-25 Thread manstey
Hi,

I solved it myself! I realised that __new__ creates self prior to
init, so this works:

dic_myinstances={}

class MyClass(object):
def __new__(self,id):
global dic_myinstances
if dic_myinstances.has_key(id):
return dic_myinstances[id]
else:
dic_myinstances[id] = self
return self


ins1 = MyClass('xx')
ins2 = MyClass('yy')
ins3 = MyClass('xx')
print ins3 is ins1
True

Is this the best way to do this?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: replacing one instance with another

2007-03-25 Thread manstey
Hi, yet again to myself!

I've realised after further testing and reading that I actually need
to do this:

dic_myinstances={}
class MyClass(object):
def __new__(cls,id):
global dic_myinstances
if dic_myinstances.has_key(id):
return dic_myinstances[id]
else:
dic_myinstances[id] = super(MyClass, cls).__new__(cls, id)
return dic_myinstances[id]
def __init__(self,id):
print id

ins1 = MyClass('xx')
'xx'
ins2 = MyClass('yy')
'yy'
ins3 = MyClass('xx')
'xx'
ins3 is ins1
True

-- 
http://mail.python.org/mailman/listinfo/python-list


how to detect change of list of instances

2007-03-13 Thread manstey
how do I detect a change in a list of class instances?

from copy import deepcopy

class CaListOfObj(list):
 subclass of list 
def __init__(self, *args, **kwargs):
list.__init__(self, *args, **kwargs)

class CaClass(object):
pass

class CaData(object):
pass

myclass=CaClass()
a=CaData()
b=CaData()
c=CaData()

listInstances = CaListOfObj([a,b,c])
setattr(myclass,'initlist',listInstances)
setattr(myclass,'newlist',deepcopy(listInstances))

print myclass.initlist == myclass.newlist
myclass.newlist.append(c)
print myclass.initlist == myclass.newlist

gives
False
False

because deep copies of instances are different instances. what I want
to do is detect a change between .initlist and .newlist.

thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to detect change of list of instances

2007-03-13 Thread manstey
Thanks.

All I want to know is whether the newlist, as a list of instances, is
modified. I thought equality was the way to go, but is there a simpler
way? How can I monitor the state of newlist and set a flag if it is
changed in anyway?


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: is it possible to give an instance a value?

2007-03-07 Thread manstey
Thanks everyone for your replies. The language is Cache Object Script,
a language used in  Intersystems Relational Dbase.

I think egbert's answer provides the simplest answer. It allows our
python interface to Cache to be almost identical to Cache Object
Script, with simeply the addition of (), which we prefer to .val
or .data for various reasons.

Using the example .type was a mistake, as I meant simply any metadata
about a value. If we make the instance callable, then we can have:

instance()
instance.calculated
instance.type
instance.whatever
instance.save()
etc

thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


is it possible to give an instance a value?

2007-03-06 Thread manstey
Hi,

My question probably reflects my misunderstanding of python objects,
but I would still like to know the answer.

The question is, is it possible for an instnace to have a value (say a
string, or integer) that can interact with other datatypes and be
passed as an argument?

The following code of course gives an error:

class Test(object):
 def __init__(self, val):
   self.val = val

 a = Test('hello')
 a.val  + ' happy'
'hello happy'
 a + 'happy'
TypeError: unsupported operand type(s) for +: 'Test' and 'str'

Is there a way to make a have the value a.val when it is used as
above, or as an argument (eg function(a, 10, 'sdf') etc)?

The only fudge I discovered for simple addition was to add to the
class

   def __add__(self, obj):
   return a.val + obj

but this doesn't solve the problem in general. I have tried
subclassing the string type, but as it is immutable, this is not
flexible the way a.val is (i.e. it can't e reassigned and remain a
subclass).

Any pointers, or is my question wrong-headed?

btw, my motivation is wanting to mimic another oo language which
allows this, so it allows:

Person.Address
'Sydney'
Person.Address.type
'%String'
Person.Address = 'Canberra'
print Person.Address. Person.Address.type
Canberra %String

etc.

We have had to implement Person.Address as Person.Address.val, making
Address an instance with .val, .type, etc.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parent-child object design question

2007-02-02 Thread manstey
Hi,

There was a mistake above, and then I'll explain what we're doing:
 insCacheClass = CacheClass(oref)
 insCacheProperty = CacheProperty(insOref,'Chapter')

should have been
 insCacheClass = CacheClass(oref)
 insCacheProperty = CacheProperty(insCacheClass ,'Chapter')

Now, to answer some questions.
1. Cache refers to Intersystems Cache database, an powerful oo dbase
that holds our data.
2. It comes with a pythonbinding, but unfortunatley the API, written
in C as a python extension, only provides old-style classes, which is
why we wrap the oref (an in-memory instance of a Cache class/table)
with CacheClass. This allows us to add attributes and methods to the
CacheClass, the most important being CacheProperty, which is a class
corresponding to the Cache property/field classes of the dbase.
3. The pythonbind also has a strange behaviour, to our view. It gets
and sets values of its properties (which are classes), via the
'parent' oref, i.e. oref.set('Name','Peter'). But we want a pythonic
way to interact with Cache, such as, insCacheClass.Name='Peter'. and
insOref.Name.Set(). The code above (in post 7) does this, but as I
asked, by storing the parent instance (insCacheClass) inside each of
its attributes (insCacheProperty1,2, etc).
4. Another reason we want a new-style class wrapped around the oref,
is that each oref has many methods on the server-side Cache database,
but they are all called the same way, namely,
oref.run_obj_method('%METHODNAME',[lisArgs]). We want to call these in
python in the much better insCacheClass.METHODNAME(Args). E.g. we
prefer insCacheClass.Save() to oref.run_obj_method('%Save',[None]).
More importantly, the in-memory version of Cache lists and arrays are
Cache lists and arrays, but these are not Python lists and
dictionaries, but they can be easily converted into them. So having a
class for each dbase property allows us to have Cache on disk
(server), get the Cache list/array to python in memory, but still in
Cache dataformat (e.g. %ListOfDataTypes) and convert it to a python
list. Tweaking the set and get methods then lets us use python lists
and dics without ever worrying about cache dataformat.

I hope this clarifies what we are doing. We are not experienced
programmers, but we want to do it this way.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parent-child object design question

2007-01-31 Thread manstey
Thanks for your input. Here is my next version, which works very well,
but for one problem I explain below:

class CacheProperty(object):
def __init__(self, insCacheClass, name):
self.Name = name
self._bind_to_parent(insCacheClass)
self.__parent = insCacheClass
self.__pyValue = self.__parent.get(name)

def _bind_to_parent(self, parent):
setattr(parent, self.Name, self)

def get(self):
return self.__pyValue

def set(self, val):
self.__parent.set(self.Name, val)
self.__pyValue = val # Set in wrapper's copy

Value = property(get, set)

def __repr__(self):
return str(self.Value)
def __str__(self):
return str(self.Value)


class CacheClass(object):
def __init__(self, obj):
self.__data = obj

def getOref(self):
return self.__data
def __repr__(self):
return 'self.__data'
def __str__(self):
return str(self.__data)
def __getattr__(self, attr):
return getattr(self.__data, attr)

Our code works fine as follows (oref is in-memory version of Cache oo-
dbase class, and is an old-style class that comes with set and get and
run_obj_method methods):
 insCacheClass = CacheClass(oref)
 insCacheProperty = CacheProperty(insOref,'Chapter')
 print insOref.Chapter.Value
5
 print insOref.Chapter.Name
'Chapter'
 insOref.Chapter.Value=67
 print insOref.Chapter
67

However, the problem is now that I can also write:
 insOref.Chapter=67
but we want to disallow this, as insOref.Chapter must remain =
insProperty

We add various other instances of CacheProperty to the insOref as
well, btw.

So any idea how to prohibit this, and can the class code above be
improved? Does it matter that each instance of CacheProperty contains
self.__parent? Does it actually contain the parent (I realise this
is not 'parent' in usual python lingo - what would be a better term?),
or is self.__parent simply point to it somehow? I don't understand
this part of python at all!

Thanks, you are being a great help in our development.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parent-child object design question

2007-01-30 Thread manstey
Hi Ben,

Could I also do something like the following? What does it mean to
store the parent class as a private variable in the child class?

class CacheProperty(object):
def __init__(self, obj, parent, properties=None):
self.__data = obj
self._parent = parent
if properties is None:
properties = {}
self._accumulate_properties(properties)


def _accumulate_properties(self, properties):
self.properties = []
for key, val in properties.iteritems():
setattr(self, key, val)
self.properties.append(key)


def __getattr__(self, name):
return getattr(self.__data, name)

def set(self, property):
return self._parent.set(property)

the set function allows us to call the parent set function within the
child, which is what we need to do.



-- 
http://mail.python.org/mailman/listinfo/python-list


parent-child object design question

2007-01-29 Thread manstey
Hi,

I am having trouble designing my classes.

I have two classes. The first one wraps around an old-style class 
called oref


Class CacheClass(object):
def __init__(self, obj):
self.__data = obj
def __getattr__(self, attr):
return getattr(self.__data, attr)

The second class is much the same, also wrapping, but has some 
attributes.

class CacheProperty(object):
def __init__(self, obj, dicProperties={}):
self.__data = obj
lisProperties=[]
for key, val in dicProperties.iteritems():
setattr(self, key, val)
lisProperties.append(key)
self.Properties = lisProperties
def __getattr__(self, attr):
return getattr(self.__data, attr)

Here is my code:

 MyClass = CacheClass(oref)
 MyProperty = CacheProperty(oref2,{'Name':'Surname', 'Value':'Peter'})
 setattr(MyClass,MyProperty.Name,MyProperty)

Now, the problem is that I want a method MyClass.MyProperty.Save() to 
save the value of MyClass.MyProperty to the database backend on disk, 
but the code for this is part of the wrapped oref code, and thus is 
invoked only by MyClass.set(MyProperty.Name,MyProperty.Value).

How can I access this method inside the MyProperty class?

I hope this is clear!

regard,s
matthew

-- 
http://mail.python.org/mailman/listinfo/python-list


sending a class as an argument

2007-01-28 Thread manstey
Hi,

Our class has its attributes set as classes, as in

MyClass.Phone.Value='34562346'
MyClass.Phone.Private=True

Inside the MyClass definition we have a function like this:

def MyFunc(self,clsProperty):
   if clsProperty.Private:
print 'Private property'
   else:
print ClsProperty.Value

In our code, we then call
 MyClass.MyFunc(MyClass.Phone)

We want to be able in our code instead simply to call:
 MyClass.MyFunc(Phone) or MyClass.MyFunc('Phone')

But we can't get it to work. If we rewrite the function as follows:

def MyFunc(self,attr):
   if self.attr.Private:
  print 'Private'
   else:
  print self.attr.Value

we get an error.

Is there a better way to do this? Thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


wrapping problem with old-style class

2006-12-19 Thread manstey
I have a problem I would like some advice on.

We have a Python binding to the Intersystems Cache OO database. It
provides an old style class in Python as an in memory instance of a
Cache class, and this is a intersystems.pythonbind.object type (= ipo).

The ipo class has three basic methods, namely, get, set, and
run_obj_method (which takes the Cache class method, as its first
argument, and the list of its arguments as the second argument)

e.g.
 CacheClass=ipo()
 CacheClass.set('Name','John')
 valName = CacheClass.get('Name')
 print valName
'John'

I want to add functionality to ipo, so I have wrapped a new style class
(MyWrapper class, very basic) around it.

PythonCacheClass=MyWrapper(CacheClass)

Now, I want to make PythonCacheClass more pythonic, so I have a
dictionary of all the properties and values of each ipo. Eg

dicCacheProperties = {'Name':'John', 'Address':'The Strand'} etc
for key, val in dicCacheProperties.iteritems():
   setattr(PythonCacheClass, key, val)

So now I have:
print PythonCacheClass.Name
'John'

My problem is this: how do I link the set and get methods of the ipo
class with the set and get methods in the wrapper class
PythonCacheClass?

So, I would like the following code:
 PythonCacheClass.Name='Alexander'
to execute automatically
CacheClass.set('Name','Alexander')etc

My thinking is that inside the PythonCacheClass, I can reference the
ipo class by self.get() - is this right? If so, how then do I set the
set attribute of the PythonCacheClass to automatically run

self.get().set('Name','Alexander')

I hope this is sufficiently clear. Am I tackling the problem the right
way?

-- 
http://mail.python.org/mailman/listinfo/python-list


Class property with value and class

2006-12-18 Thread manstey
Hi,

Is is possible to have two classes, ClassA and ClassB, and
setattr(ClassA, 'xx',ClassB), AND to then have ClassA.xx store an
integer value as well, which is not part of ClassB?

e.g. If ClassB has two properties, name and address:

ClassA.xx=10
ClassA.xx.name = 'John'
ClassA.xx.address = 'Sydney'.

etc?  I've no idea if this is possible or desirable.

thanks,
matthew

-- 
http://mail.python.org/mailman/listinfo/python-list


autoadd class properties

2006-12-08 Thread manstey
I have a ClassWrapper that wraps around a third party database object.
Each database object has a set of properties, like columns in a
relational database.

I want my wrapper to generate a property for each database object and
load its value into it.

Thus, in my database (which is an oodbms) say I have a MyDbaseClass
with MyProperty1, MyProperty2, etc, after it is loaded, I have:

dbase_object = LoadMyDbaseClass
PythonDbaseClass = PythonWrapper(dbase_object)

I can then load in a list of ['MyProperty1','MyProperty2', etc].

But how can I turn these list elements into PythonDbaseClass
properties, so that I could then simply have:

print PythonDbaseClass.MyProperty1
PythonDbaseClass.MyProperty2=4 

Is this clear?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: autoadd class properties

2006-12-08 Thread manstey
We've looked at them a little. Cache is a native OO dbase, so there is
no ORM required. Cache does that for you behind the scenes if you need
it to. What I want is to translate Cache classes and properties into
Python classes and properties. We can only use class wrappers, because
cache uses old style python objects, but this still works.

Because I am not an experienced programmer, the problem I face is how
to load ANY Cache class, whose properties the wrapper doesn't know in
advance, and turn the class properties into python properties.

So in Cache, I might have Name = String, Age = Integer, Colours = List,
in the class Person. The python binding provided by Cache creates a
Person class in Python, but it provides none of its properties, so I
want to write a wrapping class that adds the properties. Can you advise
me on how to do this?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: autoadd class properties

2006-12-08 Thread manstey
Hi,

Cache is a pure OO dbase, so no ORM is required. Cache provides a
python equivalent for the Cache class, but not for the Cache
properties. Instead, the programmer has to know in advance the
properties and their type to get and set them. I want to wrap the
Python-cache class and add the Cache properties into the class, but I
don't know how to do this. Any ideas?



George Sakkis wrote:
 manstey wrote:

  I have a ClassWrapper that wraps around a third party database object.
  Each database object has a set of properties, like columns in a
  relational database.
 
  I want my wrapper to generate a property for each database object and
  load its value into it.
 
  Thus, in my database (which is an oodbms) say I have a MyDbaseClass
  with MyProperty1, MyProperty2, etc, after it is loaded, I have:
 
  dbase_object = LoadMyDbaseClass
  PythonDbaseClass = PythonWrapper(dbase_object)
 
  I can then load in a list of ['MyProperty1','MyProperty2', etc].
 
  But how can I turn these list elements into PythonDbaseClass
  properties, so that I could then simply have:
 
  print PythonDbaseClass.MyProperty1
  PythonDbaseClass.MyProperty2=4
 
  Is this clear?

 Sounds as if you're reinventing a part of an ORM. Have you checked out
 SQLAlchemy or Django's ORM, in case they provide what you want out of
 the box ?
 
 George

-- 
http://mail.python.org/mailman/listinfo/python-list


deriving classes from object extensions

2006-12-07 Thread manstey
Hi,

I am using Python with Cache dbase, which provides pythonbind module,
and intersys.pythonbind.object types. But I can't create a class based
on this type:

import intersys.pythonbind
class MyClass(intersys.pythonbind.object):
   pass

gives me the error: TypeError: Error when calling the metaclass bases
type 'intersys.pythonbind.object' is not an acceptable base type

Can anyone expain if it is possible for me to derive my own class from
the intersys object so as to add my own functionality?

thanks,
matthew

-- 
http://mail.python.org/mailman/listinfo/python-list


sending string or list to a function

2006-12-04 Thread manstey
Hi,

Is there a neat way to write a function that can receive either a
string or a list of strings, and then if it receives a string it
manipulates that, otherwise it manipulates each string in the list?

That is, rather than having to send a list of one member
MyFunction(['var1']), I can send

MyFunction('var1')  or MyFunction(['var1','var2',var3'])

Or is this bad programming style?

What do you think?

-- 
http://mail.python.org/mailman/listinfo/python-list


naming objects from string

2006-09-20 Thread manstey
Hi,

If I have a string, how can I give that string name to a python object,
such as a tuple.

e.g.

a = 'hello'
b=(1234)

and then a function
name(b) = a

which would mean:
hello=(1234)

is this possible?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: naming objects from string

2006-09-20 Thread manstey
Hi,

But this doesn't work if I do:

a=object()
x='bob'
 locals()[x] = a

How can I do this?

James Stroud wrote:
 manstey wrote:
  Hi,
 
  If I have a string, how can I give that string name to a python object,
  such as a tuple.
 
  e.g.
 
  a = 'hello'
  b=(1234)
 
  and then a function
  name(b) = a
 
  which would mean:
  hello=(1234)
 
  is this possible?
 

 Depends on your namespace, but for the local namespace, you can use this:

 py a = object()
 py a
 object object at 0x40077478
 py locals()['bob'] = a
 py bob
 object object at 0x40077478

 A similar approach can be used for the global namespace.

 James

 --
 James Stroud
 UCLA-DOE Institute for Genomics and Proteomics
 Box 951570
 Los Angeles, CA 90095
 
 http://www.jamesstroud.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: naming objects from string

2006-09-20 Thread manstey
Hi,

thanks for the suggestions. this is my problem:

I have a metadata file that another user defines, and I don't know
their structure in advance. They might have 300+ structures. the
metadata defines the name and the tuple-like structure when read by
python.

my program reads in the metadata file and then generates python tuples
corresponding to their structures.

so they might provide a list of names, like 'bob','john','pete', with 3
structures per name, such as 'apple','orange','red' and I need 9 tuples
in my code to store their data:

bob_apple=()
bob_orange=()
..
pete_red=()

I then populate the 9 tuples with data they provide in a separate file,
and the filled tuples are then validated against the metadata to make
sure they are isomorphic.

Is this clear?

thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: naming objects from string

2006-09-20 Thread manstey
Hi,

thanks for the suggestions. this is my problem:

I have a metadata file that another user defines, and I don't know
their structure in advance. They might have 300+ structures. the
metadata defines the name and the tuple-like structure when read by
python.

my program reads in the metadata file and then generates python tuples
corresponding to their structures.

so they might provide a list of names, like 'bob','john','pete', with 3
structures per name, such as 'apple','orange','red' and I need 9 tuples
in my code to store their data:

bob_apple=()
bob_orange=()
..
pete_red=()

I then populate the 9 tuples with data they provide in a separate file,
and the filled tuples are then validated against the metadata to make
sure they are isomorphic.

Is this clear?

thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: looping question 4 NEWB

2006-07-09 Thread manstey
Thanks Marc, that was very helpful.

Marc 'BlackJack' Rintsch wrote:
 In [EMAIL PROTECTED], manstey wrote:

  I often have code like this:
 
  data='asdfbasdf'
  find = (('a','f')('s','g'),('x','y'))
  for i in find:
 if i[0] in data:
 data = data.replace(i[0],i[1])
 
  is there a faster way of implementing this? Also, does the if clause
  increase the speed?

 It decreases it.  You search through `data` in the ``if`` clause.  If it's
 `False` then you have searched the whole data and skip the replace.  If
 it's `True` you searched into data until there's a match and the the
 `replace()` starts again from the start and searches/replaces through the
 whole data.

 You can get rid of the indexes and make the code a bit clearer by the way:

 for old, new in find:
 data = data.replace(old, new)
 
 Ciao,
   Marc 'BlackJack' Rintsch

-- 
http://mail.python.org/mailman/listinfo/python-list


eval to dict problems NEWB going crazy !

2006-07-06 Thread manstey
Hi,

I have a text file called a.txt:

# comments
[('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]
[('recId', 5), ('parse', {'pos': u'np', 'gen': u'm'})]
[('recId', 7 ), ('parse', {'pos': u'np', 'gen': u'm'})]

I read it using this:

filAnsMorph = codecs.open('a.txt', 'r', 'utf-8') # Initialise input
file
dicAnsMorph = {}
for line in filAnsMorph:
if line[0] != '#': # Get rid of comment lines
x = eval(line)
dicAnsMorph[x[0][1]] = x[1][1] # recid is key, parse dict is
value

But it crashes every time on x = eval(line). Why is this? If I change
a.txt to:

# comments
[('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]

it works fine. Why doesn't it work with multiple lines? it's driving me
crazy!

Thanks,
Matthew

-- 
http://mail.python.org/mailman/listinfo/python-list


looping question 4 NEWB

2006-07-06 Thread manstey
Hi,

I often have code like this:

data='asdfbasdf'
find = (('a','f')('s','g'),('x','y'))
for i in find:
   if i[0] in data:
   data = data.replace(i[0],i[1])

is there a faster way of implementing this? Also, does the if clause
increase the speed?

Thanks,
Matthew

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: looping question 4 NEWB

2006-07-06 Thread manstey
But what about substitutions like:
'ab'  'cd', 'ced'  'de', etc

what is the fastest way then?


Roel Schroeven wrote:
 manstey schreef:
  Hi,
 
  I often have code like this:
 
  data='asdfbasdf'
  find = (('a','f')('s','g'),('x','y'))
  for i in find:
 if i[0] in data:
 data = data.replace(i[0],i[1])
 
  is there a faster way of implementing this? Also, does the if clause
  increase the speed?

 I think this is best done with translate() and string.maketrans() (see
 http://docs.python.org/lib/node110.html#l2h-835 and
 http://docs.python.org/lib/string-methods.html#l2h-208). An example:

 import string

 data = 'asdfbasdf'
 translatetable = string.maketrans('asx', 'fgy')
 data = data.translate(translatetable)
 print data

 This results in:

 fgdfbfgdf

 --
 If I have been able to see further, it was only because I stood
 on the shoulders of giants.  -- Isaac Newton
 
 Roel Schroeven

-- 
http://mail.python.org/mailman/listinfo/python-list


setting variables from a tuple NEWB

2006-07-06 Thread manstey
Hi,

If I have a tuple like this:

tupGlob = (('VOWELS','aeiou'),('CONS','bcdfgh'))

is it possible to write code using tupGlob that is equivalent to:
VOWELS = 'aeiou'
CONS = ''bcdfgh'

Thanks,
Matthew

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: eval to dict problems NEWB going crazy !

2006-07-06 Thread manstey
That doesn't work. I just get an error:

x = eval(line.strip('\n'))
  File string, line 1
 [('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]

 SyntaxError: unexpected EOF while parsing


any other ideas?

Bruno Desthuilliers wrote:
 manstey wrote:
  Hi,
 
  I have a text file called a.txt:
 
  # comments
  [('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]
  [('recId', 5), ('parse', {'pos': u'np', 'gen': u'm'})]
  [('recId', 7 ), ('parse', {'pos': u'np', 'gen': u'm'})]
 
  I read it using this:
 
  filAnsMorph = codecs.open('a.txt', 'r', 'utf-8') # Initialise input
  file
  dicAnsMorph = {}
  for line in filAnsMorph:
  if line[0] != '#': # Get rid of comment lines
  x = eval(line)
  dicAnsMorph[x[0][1]] = x[1][1] # recid is key, parse dict is
  value
 
  But it crashes every time on x = eval(line). Why is this? If I change
  a.txt to:
 
  # comments
  [('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})]
 
  it works fine. Why doesn't it work with multiple lines? it's driving me
  crazy!

 try with:
   x = eval(line.strip('\n'))


 --
 bruno desthuilliers
 python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
 p in '[EMAIL PROTECTED]'.split('@')])

-- 
http://mail.python.org/mailman/listinfo/python-list


searching for strings (in a tuple) in a string

2006-07-06 Thread manstey
Hi,

I often use:

a='yy'
tup=('x','yy','asd')
if a in tup:
   ...

but I can't find an equivalent code for:

a='xfsdfyysd asd x'
tup=('x','yy','asd')
if tup in a:
...

I can only do:

if 'x' in a or 'yy' in a or 'asd' in a:
   ...

but then I can't make the if clause dependent on changing value of tup.

Is there a way around this?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: searching for strings (in a tuple) in a string

2006-07-06 Thread manstey
I know I can do it this way. I wanted to know if there was another way.

Fredrik Lundh wrote:
 manstey [EMAIL PROTECTED] wrote:

  but I can't find an equivalent code for:
 
  a='xfsdfyysd asd x'
  tup=('x','yy','asd')
  if tup in a:
 ...
 
  I can only do:
 
  if 'x' in a or 'yy' in a or 'asd' in a:
...
 
  but then I can't make the if clause dependent on changing value of tup.
 
  Is there a way around this?

 is the def statement broken in your Python version ?

 def findany(text, words):
 for w in words:
 if w in text:
 return True
 return False
 
 if findany(a, tup):
 ...
 
 /F

-- 
http://mail.python.org/mailman/listinfo/python-list


list problem 4 newbie

2006-06-26 Thread manstey
I can't figure out why my code is not working. I thought I had the list
copied correctly:

Here is my code:



a=[[u'HF', []], [u')F', [u'75']], [u'RE', []], [u'C', []]]
b=a[:]

for index in reversed(range(0,len(a)-1)):
   if '75' in b[index][1]:
  b[index][1].remove('75')
  b[index][1].append('99')

print a,'\n',b



but when it finishes, I get

[[u'HF', []], [u')F', ['99']], [u'RE', []], [u'C', []]]
[[u'HF', []], [u')F', ['99']], [u'RE', []], [u'C', []]]

instead of the desired:

[[u'HF', []], [u')F', ['75']], [u'RE', []], [u'C', []]]
[[u'HF', []], [u')F', ['99']], [u'RE', []], [u'C', []]]

why is this?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list problem 4 newbie

2006-06-26 Thread manstey
Thanks very much. Deepcopy works fine, as does reversed(b). I thought I
needed the index number but I didn't.


Duncan Booth wrote:

 manstey wrote:

  for index in reversed(range(0,len(a)-1)):
 if '75' in b[index][1]:
b[index][1].remove('75')
b[index][1].append('99')
 

 What on earth is all that messing around in the for loop intended to do? If
 you want a range from len(a)-2 to 0 inclusive then just do it in range
 directly (and did you really mean not to look at the last element?), if you
 actually just wanted to iterate through b in reverse, then just iterate
 through b in reverse:

 b = copy.deepcopy(a)
 for element in reversed(b):
if '75' in element[1]:
   element[1].remove('75')
   element[1].append('99')

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pickling multiple dictionaries

2006-05-24 Thread manstey
Thanks very much. How large is *really* large for making pytables
worthwhile. Our python script generates an xml file of about 450Mb. Is
pytables worth using then?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NEWB: how to convert a string to dict (dictionary)

2006-05-24 Thread manstey
Thanks.  I didn't know eval could do that. But why do many posts say
they want a solution that doesn't use eval?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NEWB: reverse traversal of xml file

2006-05-23 Thread manstey
But will this work if I don't know parts in advance. I only know parts
by reading through the file, which has 450,000 lines.

-- 
http://mail.python.org/mailman/listinfo/python-list


pickling multiple dictionaries

2006-05-23 Thread manstey
Hi,

I am running a script that produces about 450,000 dictionaries. I tried
putting them into a tuple and then pickling the tuple, but the tuple
gets too big. Can I pickle dictionaries one after another into the same
file and then read them out again?

Cheers,
Matthew

-- 
http://mail.python.org/mailman/listinfo/python-list


NEWB: how to convert a string to dict (dictionary)

2006-05-23 Thread manstey
Hi,

How do I convert a string like:
a={'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'}

into a dictionary:
b={'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'}

Thanks, Matthew

PS why in Python is it so often easy to convert one way but not the
other?

-- 
http://mail.python.org/mailman/listinfo/python-list


NEWB: reverse traversal of xml file

2006-05-22 Thread manstey
Hi,

I have an xml file of about 140Mb like this:

book
  record
...
 wordpartWTS1/wordpartWTS
  /record
  record
...
wordpartWTS2/wordpartWTS
  /record
  record
...
wordpartWTS1/wordpartWTS
  /record
/book

I want to traverse it from bottom to top and add another field to each
record totalWordPart1/totalWordPart
which would give the highest value of wordpartWTS for each record for
each word

so if wordparts for the first ten records were 1 2 1 1 1 2 3 4 1 2
I want totalWordPart to be 2 2 1 1 4 4 4 4 2 2

I figure the easiest way to do this is to go thru the file backwards.

Any ideas how to do this with an xml data file? 

Thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


newb: comapring two strings

2006-05-18 Thread manstey
Hi,

Is there a clever way to see if two strings of the same length vary by
only one character, and what the character is in both strings.

E.g. str1=yaqtil str2=yaqtel

they differ at str1[4] and the difference is ('i','e')

But if there was str1=yiqtol and str2=yaqtel, I am not interested.

can anyone suggest a simple way to do this?

My next problem is, I have a list of 300,000+ words and I want to find
every pair of such strings. I thought I would first sort on length of
string, but how do I iterate through the following:

str1
str2
str3
str4
str5

so that I compare str1  str2, str1  str3, str 1  str4, str1  str5,
str2  str3, str3  str4, str3  str5, str4  str5.

Thanks in advance,
Matthew

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A Unicode problem -HELP

2006-05-17 Thread manstey
Hi Martin,

Thanks very much. Your def comma_separated_utf8(items): approach raises
an exception in codecs.py, so I tried  = u, .join(word_info + parse +
gloss), which works perfectly. So I want to understand exactly why this
works. word_info and parse and gloss are all tuples. does str convert
the three into an ascii string? but the join method retains their
unicode status.

In the text file, the unicode characters appear perfectly, so I'm very
happy.

cheers
matthew

-- 
http://mail.python.org/mailman/listinfo/python-list


index in for loops

2006-05-17 Thread manstey
in for loops like the following:

word='abcade'

for letter in word:
   print letter


Is it possible to get the position of letter for any iteration through
the loop?

so for examlpe letter=='a', and I want to know if it is the first or
second 'a' in 'abcade'.

can i do this by looking at a property of letter?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A Unicode problem -HELP

2006-05-16 Thread manstey
Hi Martin,

HEre is how I write:

input_file = open(input_file_loc, 'r')
output_file = open(output_file_loc, 'w')
for line in input_file:
output_file.write(str(word_info + parse + gloss))  # = three
functions that return tuples

(u'F', u'\u0254')  are two of the many unicode tuple elements returned
by the three functions.

What am I doing wrong?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A Unicode problem -HELP

2006-05-16 Thread manstey
I'm a newbie at python, so I don't really understand how your answer
solves my unicode problem.

I have done more reading on unicode and then tried my code in IDLE
rather than WING IDE, and discovered that it works fine in IDLE, so I
think WING has a problem with unicode. For example, in WING this code
returns an error:

a={'a':u'\u0254'}
print a['a']


UnicodeEncodeError: 'ascii' codec can't encode character u'\u0254' in
position 0: ordinal not in range(128)

but in IDLE it correctly prints open o

So, assuming I now work in IDLE, all I want help with is how to read in
an ascii string and convert its letters to various unicode values and
save the resulting 'string' to a utf-8 text file. Is this clear?

so in pseudo code
1.  F is converted to \u0254, $ is converted to \u0283, C is converted
to \u02A6\02C1, etc.
(i want to do this using a dictionary TRANSLATE={'F':u'\u0254', etc)
2. I read in a file with lines like:
F$
FCF$
$$C$ etc
3. I convert this to
\u0254\u0283
\u0254\u02A6\02C1\u0254 etc
4. i save the results in a new file

when i read the new file in a unicode editor (EmEditor), i don't see
\u0254\u02A6\02C1\u0254, but I see the actual characters (open o, esh,
ts digraph, modified letter reversed glottal stop, etc.

I'm sure this is straightforward but I can't get it to work.

All help appreciated!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A Unicode problem -HELP

2006-05-16 Thread manstey
OK, I apologise for not being clearer.

1. Here is my input data file, line 2:
gn1:1,1.2 R)$I73YT R)[EMAIL PROTECTED]

2. Here is my output data file, line 2:
u'gn', u'1', u'1', u'1', u'2', u'-', u'R)$I73YT', u'R)$IYT',
u'R)$IYT', u'@', u'ncfsa', u'nc', '', '', '', u'f', u's', u'a', '',
'', '', '', '', '', '', '', u'B.:R)$I^YT', u'b.:cv)cv^yc', '\xc9\x94'

3. Here is my main program:
# -*- coding: UTF-8 -*-
import codecs

import splitFunctions
import surfaceIPA

# Constants for file location

# Working directory constants
dir_root = 'E:\\'
dir_relative = '2 Core\\2b Data\\Data Working\\'

# Input file constants
input_file_name = 'in.grab.txt'
input_file_loc = dir_root + dir_relative + input_file_name
# Initialise input file
input_file = codecs.open(input_file_loc, 'r', 'utf-8')

# Output file constants
output_file_name = 'out.grab.txt'
output_file_loc = dir_root + dir_relative + output_file_name
# Initialise output file
output_file = codecs.open(output_file_loc, 'w', 'utf-8') # unicode

i = 0
for line in input_file:
if line[0] != '': # Ignore headers
i += 1
if i != 1:
word_info = splitFunctions.splitGrab(line, i)
parse=splitFunctions.splitParse(word_info[10])

gloss=surfaceIPA.surfaceIPA(word_info[6],word_info[8],word_info[9],parse)
a=str(word_info + parse + gloss).encode('utf-8')
a=a[1:len(a)-1]
output_file.write(a)
output_file.write('\n')

input_file.close()
output_file.close()

print 'done'


4. Here is my problem:
At the end of my output file, where my unicode character \u0254 (OPEN
O) appears, the file has '\xc9\x94'

What I want is an output file like:

'gn', '1', '1', '1', '2', '-', . 'ɔ'

where ɔ is an open O, and would display correctly in the appropriate
font.

Once I can get it to display properly, I will rewrite gloss so that it
returns a proper translation of 'R)$I73YT', which will be a string of
unicode characters.

Is this clearer? The other two functions are basic. splitGrab turns
'gn1:1,1.2 R)$I73YT R)[EMAIL PROTECTED]' into 'gn 1 1 1 2 R)$I73YT R)$IYT
@ ncfsa' and splitParse turns the final piece of this 'ncfsa' into 'n c
f s a'. They have to be done separately as splitParse involves some
translation and program logic. SurfaceIPA reads in 'R)$I73YT' and
other data to produce the unicode string. At the moment it just returns
two dummy strings and u'\u0254'.encode('utf-8').

All help is appreciated!

Thanks

-- 
http://mail.python.org/mailman/listinfo/python-list

A Unicode problem -HELP

2006-05-11 Thread manstey
I am writing a program to translate a list of ascii letters into a
different language that requires unicode encoding. This is what I have
done so far:

1. I have # -*- coding: UTF-8 -*- as my first line.
2. In Wing IDE I have set Default Encoding to UTF-8
3. I have imported codecs and opened and written my file, which doesn't
have a BOM, as encoding=UTF-8
4. I have written a dictionary for translation, with entries such as
{'F':u'\u0254'} and a function to do the translation

Everything works fine, except that my output file, when loaded in
unicode aware emeditor has
(u'F', u'\u0254')

But I want to display it as:
('F', 'ɔ') # where the ɔ is a back-to-front 'c'

So my questions are:
1. How do I do this?
2. Do I need to change any of my steps above?

-- 
http://mail.python.org/mailman/listinfo/python-list

Recommended data structure for newbie

2006-05-02 Thread manstey
Hi,

I have a text file with about 450,000 lines. Each line has 4-5 fields,
separated by various delimiters (spaces, @, etc).

I want to load in the text file and then run routines on it to produce
2-3 additional fields.

I am a complete newbie to Python but I have the docs and done some
experimenting. But I'm not sure what sort of data structure I should be
creating.

Example:
gee fre asd[234
ger dsf asd[243
gwer af as.:^25a

what i want is
'gee' 'fre' 'asd' '[' '234'
'ger' dsf' asf' '[' '243'
'gwer' 'af 'as.:' '^' '25a

etc for 450,000 lines.

Then on the basis of information in 1 or more lines, I want to append
to each line new data such as

'gee' 'fre' 'asd' '[' '234' 'geefre2' '2344f'
'ger' dsf' asf' '[' '243' 'gerdsd' '2431'
'gwer' 'af 'as.:' '^' '25a 'gweraf' 'sfd2'

What do you recommend? I think it is some sort of list inside a list,
but I'm not sure. I know how to generate the new data but not how to
store everything both in memory and on disk.

Regards,
Matthew

-- 
http://mail.python.org/mailman/listinfo/python-list