FW: Writing to a file
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
Hope someone can help!! Thanks in advance!!
this, for security reasons, will never work inside DTML or from simple python scripts. you'll have to use an External Method, which means you must use python. jens On Friday, November 30, 2001, at 01:09 , 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
Hope someone can help!! Thanks in advance!!
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
If you want to write files to the file system, you probably want to use an external python method. It wouldn't be hard, but of course it takes a bit of python programming. But there are some issues to take care of: 1) With your proposed algorithm, you could be opening a lot of files, all of which may stay open until near the end of the outer loops. Not only does this make you vulnerable in case of problems (many open files waiting to be affected), but you could run out of available file handles on some systems. So you should see if you can change your algorithm design to avoid these problems. 2) You are presumably planning to write to files named for the various banks in some way. If a second user started a similar report before the work was finished for a previous user, some of the files could be overwritten, and there is no guarantee that the results would be as desired for the first user. Alternatively, one of the users could get an locked file exception and wouldn't end up getting their results. You need to consider how to handle such concurrent uses. 3) You should find a way to avoid including the absolute paths to the files in your code. My preferred approach is to locate them relative to the code module that processes them. You can ask the module to tell you where it is in the file system, and construct the final paths using that information. This lets you move the code around to different places on different machines and have it still work. You could also use an environmental variable to point to the file locations, but then you have to arrange a way to set it and not forget to do so when you move the system to a different computer. 4) Use the path functions in the python os.path library module to construct paths rather than hard-coding path separators like "\". This will allow you to move between Windows and Unix machines without having to change any code. Cheers, Tom P [Beaudette, Sheree]
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
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
participants (4)
-
Beaudette, Sheree -
Jens Vagelpohl -
Oliver Bleutgen -
Thomas B. Passin