Encapsulating conditional execution based on list membership - how do you do it?

M

metaperl

I have a series of scripts which retrieve files. None of these scripts
should continue if the file to be retrieved already exists in the
archive. Here is the code:

if f in path(self.storage.archive).files('*'):
print f, "exists in archive. Not continuing"
sys.exit()


self.storage is a class which gives me object-oriented access to the
filesystem directories that will be manipulated by the script. E.g.
self.storage.archive is a string which refers to a certain directory.
Initializing this object initializes all the members (input, archive,
output, workfiles) and makes all the correct directories.

ok, so I'm thinking of adding a method to the storage class so I can do
this:

self.storage.archive_has(f, print_msg=True) and sys.exit()

but really there is no need to hardcode which storage directory. So I'm
thinking:

self.storage.memberof('archive', f, print_msg=Tree) and sys.exit()


Is that the best solution you can think of?
 
G

Gabriel Genellina

ok, so I'm thinking of adding a method to the storage class so I can do
this:

self.storage.archive_has(f, print_msg=True) and sys.exit()

but really there is no need to hardcode which storage directory. So I'm
thinking:

self.storage.memberof('archive', f, print_msg=Tree) and sys.exit()


Is that the best solution you can think of?

This is just _my_ opinion, of course.
I don't like the construct "blablabla() and sys.exit()" or
"blablabla() or sys.exit()", I prefer an explicit check "if not
blablabla(): exit()"
I'm not sure what you mean by "archive" but instead of a string, make
it an object so you can write self.storage.archive.has_file(f). The
check should do just *that*, return a boolean without side effects;
if you want to print a message, make the caller print it.
If you alias .has_file as .__contains__, you could also write: if f
in self.storage.archive, which looks even better.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top