By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

Updating file objects automatically

P: n/a

Hi,
I have the following situation where I only open the file on the Search
class (where it should be used more often) and I want to reutilize search
methods to find the exact location of where the changes should occur. In
code terms, I have something like:

================================================== =============================
class Search(object):
def __init__(self, fileToRead):
self.fileToRead = open(fileToRead)
(definition of several search methods)

class Writer(Search):
def __init__(self, fileToRead, fileToWrite=None):
super(Writer, self).__init__(fileToRead)
super(Search, self).__init__(fileToRead)
(definition of write methods)

def exampleMethod(self):
place = self.searchSomeParticularCondition() # method from Search
place.change.values('old', 'new')
self.fileToRead.write(all_things) # object from Search

search = Search('someFile')
print "Information:", search.something()
change = Writer('someFile')
change.exampleMethod()
# here if I do another search, with "search.something('id')" I get the old
# value ...
print search.something('id') # prints old data
# ... I want the new one, so I reassign the search instance:
search = Search('someFile')
print search.something('id') # prints new data
================================================== =============================

Is there something I can do to change all instances of the Search class?
Are there different alternatives for such a "thing"?
Thanks in advance,
--
Godoy. <go***@ieee.org>

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, 2004-12-30 at 11:15, Jorge Luiz Godoy Filho wrote:
Is there something I can do to change all instances of the Search class?
Are there different alternatives for such a "thing"?


I couldn't really catch your explanation, but mention of changing all
instances of a class suggests that you may be in a situation where you
need to modify the class, not its instances. There are two methods I use
when I have to change things across all instances:

def A(object):
"A class that provides a variable shared by all instances,
and a method of changing it using a normal method and, for
example's sake, a class method."""

class_level_variable = True

def __init__(self):
pass

def getvalue(self):
return self.class_level_variable

def setvalue(self, newval):
self.__class__.class_level_variable = newval

def setvaluecls(cls, newval):
cls.class_level_variable = newval
setvaluecls = classmethod(setvaluecls)

sevaluecls and setvalue look the same to callers calling them on an
instance of the class.

I have no idea if that's actually appropriate for your needs, it's just
a stab in the dark, but perhaps it might be.

--
Craig Ringer

Jul 18 '05 #2

P: n/a
Craig Ringer, Quinta 30 Dezembro 2004 06:27, wrote:
I couldn't really catch your explanation, but mention of changing all
instances of a class suggests that you may be in a situation where you
need to modify the class, not its instances. There are two methods I use
when I have to change things across all instances:
I'm sorry. It was late here and I've been coding this and other things for
near 14h in a row... I think that even I wouldn't understand that if I
weren't working with it :-)
I have no idea if that's actually appropriate for your needs, it's just
a stab in the dark, but perhaps it might be.


It helps, yes. Putting the object on the class instead of on an instance of
it might (I'm 99.9% sure) solve the problem. (I have already done that for
other object that is shared, but I didn't remember doing that for this)
Thank you. You helped a lot to make me "see" it :-)

--
Godoy. <go***@ieee.org>

Jul 18 '05 #3

P: n/a
Jorge Luiz Godoy Filho, Quinta 30 Dezembro 2004 09:20, wrote:
It helps, yes. Putting the object on the class instead of on an instance
of
it might (I'm 99.9% sure) solve the problem. (I have already done that
for other object that is shared, but I didn't remember doing that for
this)


And, as expected, it worked. Thanks for the help, Craig.

--
Godoy. <go***@ieee.org>

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.