Paul Hankin
1/9/2008 9:21:00 AM
On Jan 9, 2:41 am, Tim Chase <python.l...@tim.thechases.com> wrote:
> > I decided that I was just trying to be "too smooth by 1/2" so I fell back to
>
> > messages = open(os.path.join(host_path,'messages.txt'), 'wb')
> > deliveries = open(os.path.join(host_path,'deliveries.txt'), 'wb')
> > actions = open(os.path.join(host_path,'actions.txt'), 'wb')
> > parts = open(os.path.join(host_path,'parts.txt'), 'wb')
> > recipients = open(os.path.join(host_path,'recipients.txt'), 'wb')
> > viruses = open(os.path.join(host_path,'viruses.txt'), 'wb')
> > esp_scores = open(os.path.join(host_path,'esp_scores.txt'), 'wb')
>
> Another way to write this which reduces some of the code would be
>
> filenames = ['messages', 'deliveries', 'actions', 'parts',
> 'recipients', 'viruses', 'esp_scores']
>
> (messages, deliveries, actions, parts,
> recipients, viruses, esp_scores) = [
> open(os.path.join(host_path, '%s.txt' % fn), 'wb')
> for fn in filenames
> ]
>
> It becomes even more clear if you make an intermediate "opener"
> function such as
>
> binwriter = lambda fname: open(
> os.path.join(host_path, '%s.txt' % fname), 'wb')
>
> (messages, deliveries, actions, parts,
> recipients, viruses, esp_scores) = [
> binwriter(fn) for fn in filenames]
This can be more cleanly written using locals()
for fn in filenames:
locals()[fn] = open(os.path.join(host_path, fname + '.txt', 'wb')
--
Paul Hankin