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?

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?

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

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 =

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:

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:

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

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 (),

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

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

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

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

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

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

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,

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 =

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

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

Re: autoadd class properties

2006-12-08 Thread manstey
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

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

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

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

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

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

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

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') #

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

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

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

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

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

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')

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

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

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? --

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

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

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

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

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

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

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', '', '', '',

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

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