Updating file objects automatically

  • Thread starter Jorge Luiz Godoy Filho
  • Start date
J

Jorge Luiz Godoy Filho

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

Craig Ringer

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

Jorge Luiz Godoy Filho

Craig said:
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 :)
 
J

Jorge Luiz Godoy Filho

Jorge said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top