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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • metaperl

    #1

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

    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.stora ge.archive).fil es('*'):
    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.ar chive 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.ar chive_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.me mberof('archive ', f, print_msg=Tree) and sys.exit()


    Is that the best solution you can think of?

  • Gabriel Genellina

    #2
    Re: Encapsulating conditional execution based on listmembership - how do you do it?

    At Monday 11/12/2006 12:23, metaperl wrote:
    >ok, so I'm thinking of adding a method to the storage class so I can do
    >this:
    >
    >self.storage.a rchive_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.m emberof('archiv e', 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.ar chive.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.ar chive, 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

    Comment

    Working...