[Zope] FW: Writing to a file

Oliver Bleutgen myzope@gmx.net
Fri, 30 Nov 2001 19:54:03 +0100


Beaudette, Sheree wrote:

> I'm going to resubmit my question below since I haven't yet received any
> answer.  Is there anyone who can at least lead me in the right direction?
> I'm sure people have done this before.  Still new to Zope and especially
> Python.
> 
> Thanks!
> 
> 
>>-----Original Message-----
>>From:	Beaudette, Sheree 
>>Sent:	Thursday, November 29, 2001 12:12 PM
>>To:	'zope@zope.org'
>>Subject:	Writing to a file
>>
>>The logic here is simple but I'm not sure about the code.  Also not sure
>>how or if I need to use Python (which I'm just learning).
>>
>>I'm in a zope dtml method called cldmSearchForm
>>
>>what I need to do from this method is:
>>
>>start a loop that loops through the SQL method clsqBanks until there are
>>no more banks
>>open a file which will be named something differently for each bank (i.e.
>>bkbankidrom.txt)
>>write a header of some sort
>>start another loop which loops through the SQL method clsqOfficers until
>>there are no more officers
>>for each officer write the bankno, officer no, etc, then new line
>>end officer loop
>>close file
>>end bank loop



Hi Sheree,

it's not quite clear where exactly your problem is, i.e. where you are 
unsure about what to do - this might explain that you didn't get any 
answers.

I'll try an answer anyway, please combine with the answers you already 
got (esp. about the security, concurrency and portability aspects).

I would first gather all data, and then load off just the file writing 
to an external method which gets the data as a dictionary.

So you need 1 python script, two sql-methods (which you already seem to 
have) and an external method to write the files.


You mentioned the methods, now I would use a python script to gather the 
data. untested:


------------------------------------------------------
result={}
for row in container.clsqBanks():
# loops through the list of result objects which clsqBanks() returns.
# container is the container of the python method, therefore
# we assume all methods are in one folder.

     bank_officers = clsqOfficers(bank_id = i.bank_id).dictionaries()

# we assume the bank_id gives is the column which links your two tables
# dictionaries() gives us a list of dictionaries which each contain the 
# data of a row in the form {'column1':'value1', 'column2':'value2' ..}

     result[i.bank_id] = bank_officers

# result is a dictionary, holding the bank_id as a key and all data of
# the bank officers (which itself is a dictionary) as the values.


container.write_data(data = result)

# here we call the external method write_data with our result data
# structure.

-------------------------------------------------------------

Now your external method roughly looks like:


------------------------

def write_data(data = {}):
# loop through the banks
     for bank_id in data.keys():
# open a file for each bank
     fh=open('/your/directory/of/choice/bb'+bank_id + '.txt','w')
# write header
        fh.write('header of some sort\n')
# loop through the officers of that bank
        for officer in data[bank_id]:
# loop through the data of one officer and write it to the file
            for data_name in officer.keys():
                fh.write(data_name + ' : ' officer[data_name] + '\n')
# we are done with that bank, close file
     fh.close()
# completly done, maybe return feedback
return 1

-------------------------------

You see, it's quite some work to try to answer your question adequatly, 
because it was quite broad. Oh, and I guarantee the above won't work ;-)

cheers,
oliver