473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how can I create/set a 'file' reference in a attribute of a class

ken
Hi,
i have a class:

class LogHandler(ContentHandler):
# a reference to a file open by some other function/class
outputFile;
def endElement(self, name):
doSomething(self, "GroupResultList", self.text, outputFile)
First, I get an error saying 'NameError: global name 'outputFile' is
not defined' , how can I declare outputFile as a 'file reference'?

Second , how can I set this file reference after I create the object
'LogHandler'?

How can I do that?
f = open('dummyFile.txt');
curHandler = LogHandler()
curHandler.file = f

Feb 26 '07 #1
2 1082
ken wrote:
Hi,
i have a class:

class LogHandler(ContentHandler):
# a reference to a file open by some other function/class
outputFile;
def endElement(self, name):
doSomething(self, "GroupResultList", self.text, outputFile)
First, I get an error saying 'NameError: global name 'outputFile' is
not defined' , how can I declare outputFile as a 'file reference'?

Second , how can I set this file reference after I create the object
'LogHandler'?

How can I do that?
f = open('dummyFile.txt');
curHandler = LogHandler()
curHandler.file = f
Normally this is done when you instantiate the class:

fp = open('dummyFile.txt')
curHandler=LogHander(fp)

class LogHandler(ContentHandler):
def __init__(self, fp=None):
self.fp=fp

def endElement(self, name:
doSomething(self, "GroupResultList", self.text, self.fp)
but you could set it later if you like:

fp = open('dummyFile.txt')
curHandler=LogHander()
curHandler.fp=fp
-Larry Bates
Feb 26 '07 #2
ken wrote:
Hi,
i have a class:

class LogHandler(ContentHandler):
# a reference to a file open by some other function/class
outputFile;
What do you intend to achieve with this last line, and what's
the ';' for?
First, I get an error saying 'NameError: global name 'outputFile'
is not defined' , how can I declare outputFile as a 'file
reference'?
If I'm not mistaken it would be best to read the Python tutorial.
Second , how can I set this file reference after I create the
object 'LogHandler'?
You shouldn't create an object that has the same name as its class.

Anyway, you can open a file using

var = file("/path/to/file")
How can I do that?
f = open('dummyFile.txt');
curHandler = LogHandler()
curHandler.file = f
Is it this you want to do?

class LogHandler(ContentHandler):
pass

f = file("dummyfile.txt")
curHandler = LogHandler()
curHandler.file = f

Seems quite pointless to me.

I'm still confused about your intentions.

Regards,
Björn

--
BOFH excuse #111:

The salesman drove over the CPU board.

Feb 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Elliott | last post by:
I can create this: ?xml version="1.0" standalone="yes" ?> <ConfigOpt> <record> <Field_1>Text # 1</Field_1> <Field_2>Text # 2</Field_2> </record> </ConfigOpt>
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
2
by: s.hong | last post by:
Hello? On the ASP.NET, I can use the class by import declation on the top of page. <%@ page language="vb" explicit="true" aspcompat="false" debug="true"%> <%@ import namespace="System.Data" %>...
6
by: Dennis | last post by:
I am trying to use ZLIB.Dll in a VB.Net project but keep getting an error message that says it can't load the DLL. I tried to add a reference using "Project-Add Reference" but get the error message...
6
by: ken.carlino | last post by:
>From http://www.parashift.com/c++-faq-lite/references.html#faq-8.6, it said "Use references when you can, and pointers when you have to." And I need to convert this java class to c++: public...
9
by: Alan Silver | last post by:
Hello, I have a user control which I am trying to load dynamically, but am running into problems. I think the problem is because I have two .ascx files that refer to the same .ascx.cs file. A...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
1
by: Jamie J. Begin | last post by:
I'm very new to the world of Python and am trying to wrap my head around it's OOP model. Much of my OOP experience comes from VB.Net, which is very different. Let's say I wanted to create an...
4
by: BorisBoshond | last post by:
Hi all, Hope someone is able and willing to help me with following problem. I received a xsd file from another company, our company i supposed to return xml based on that xsd. Problem is that I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.