FAQ
Given a program 'foo' that takes a command line argument '-I
includefile', I want to be able to look for 'includefile' in a path
specified in an environment variable, 'FOOPATH'.

I'd like a semantic that says:

"If 'includefile' contains one or more path separator characters,
ignore 'FOOPATH'. If it contains no path separators, look for it in
the paths specified by 'FOOPATH', beginning with the leftmost path
first."

Is there a standard Pythonic idiom for doing this or do I need to cook
up my own.


TIA,
--
----------------------------------------------------------------------------
Tim Daneliuk tundra at tundraware.com
PGP Key: http://www.tundraware.com/PGP/

Search Discussions

  • Chris Rebert at Jun 24, 2010 at 1:46 am

    On Wed, Jun 23, 2010 at 3:27 PM, Tim Daneliuk wrote:
    Given a program 'foo' that takes a command line argument '-I
    includefile', I want to be able to look for 'includefile' in a path
    specified in an environment variable, 'FOOPATH'.

    I'd like a semantic that says:

    ?"If 'includefile' contains one or more path separator characters,
    ? ignore 'FOOPATH'. If it contains no path separators, look for it in
    ? the paths specified by 'FOOPATH', beginning with the leftmost path
    ? first."

    Is there a standard Pythonic idiom for doing this or do I need to cook
    up my own.
    Cook your own. Also, I certainly wouldn't call something this complex
    a mere idiom.
    You will find optparse, os.sep, and os.walk helpful in writing your code.

    Cheers,
    Chris
  • Nobody at Jun 24, 2010 at 7:34 am

    On Wed, 23 Jun 2010 17:27:16 -0500, Tim Daneliuk wrote:

    Given a program 'foo' that takes a command line argument '-I
    includefile', I want to be able to look for 'includefile' in a path
    specified in an environment variable, 'FOOPATH'.

    I'd like a semantic that says:

    "If 'includefile' contains one or more path separator characters,
    ignore 'FOOPATH'. If it contains no path separators, look for it in
    the paths specified by 'FOOPATH', beginning with the leftmost path
    first."

    Is there a standard Pythonic idiom for doing this or do I need to cook
    up my own.
    There isn't an idiom.

    There are a surprising number of choices for such a simple task, e.g.
    whether the search path is used for relative paths containing a separator,
    whether you stop at the first file which exists or the first file which
    meets other criteria (e.g. suitable permissions), whether default
    locations come first or last, what happens if a default location is
    included in the search path, etc.
  • Jorgen Grahn at Jun 25, 2010 at 12:25 pm

    On Thu, 2010-06-24, Nobody wrote:
    On Wed, 23 Jun 2010 17:27:16 -0500, Tim Daneliuk wrote:

    Given a program 'foo' that takes a command line argument '-I
    includefile', I want to be able to look for 'includefile' in a path
    specified in an environment variable, 'FOOPATH'.

    I'd like a semantic that says:

    "If 'includefile' contains one or more path separator characters,
    ignore 'FOOPATH'. If it contains no path separators, look for it in
    the paths specified by 'FOOPATH', beginning with the leftmost path
    first."

    Is there a standard Pythonic idiom for doing this or do I need to cook
    up my own.
    There isn't an idiom.

    There are a surprising number of choices for such a simple task, e.g.
    whether the search path is used for relative paths containing a separator,
    whether you stop at the first file which exists or the first file which
    meets other criteria (e.g. suitable permissions), whether default
    locations come first or last, what happens if a default location is
    included in the search path, etc.
    Another favorite is whether relative paths are relative to your
    current directory, or to the location of whatever file this is to be
    included /into/.

    For an example where it mattered (to C compilers), google for the
    paper "recursive make considered harmful". It took compiler writers
    decades to realize what the best choice was there.

    (By the way, -I commonly means "search this directory for include
    files" rather than "include this file". You may want to avoid
    confusing people by choosing another letter, like -i.)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
  • Tim Daneliuk at Jun 25, 2010 at 12:52 pm

    On 6/25/2010 7:25 AM, Jorgen Grahn wrote:
    On Thu, 2010-06-24, Nobody wrote:
    On Wed, 23 Jun 2010 17:27:16 -0500, Tim Daneliuk wrote:

    Given a program 'foo' that takes a command line argument '-I
    includefile', I want to be able to look for 'includefile' in a path
    specified in an environment variable, 'FOOPATH'.

    I'd like a semantic that says:

    "If 'includefile' contains one or more path separator characters,
    ignore 'FOOPATH'. If it contains no path separators, look for it in
    the paths specified by 'FOOPATH', beginning with the leftmost path
    first."

    Is there a standard Pythonic idiom for doing this or do I need to cook
    up my own.
    There isn't an idiom.

    There are a surprising number of choices for such a simple task, e.g.
    whether the search path is used for relative paths containing a separator,
    whether you stop at the first file which exists or the first file which
    meets other criteria (e.g. suitable permissions), whether default
    locations come first or last, what happens if a default location is
    included in the search path, etc.
    Another favorite is whether relative paths are relative to your
    current directory, or to the location of whatever file this is to be
    included /into/.

    For an example where it mattered (to C compilers), google for the
    paper "recursive make considered harmful". It took compiler writers
    decades to realize what the best choice was there.

    (By the way, -I commonly means "search this directory for include
    files" rather than "include this file". You may want to avoid
    confusing people by choosing another letter, like -i.)

    /Jorgen
    I just went ahead and implemented my own. It will eventually find it's way
    to:

    http://www.tundraware.com/Software/tsearchpath/

    I'll post on c.l.p.a when the thing is released into the wild.


    Thanks to all that took the time to respond...

    --
    ----------------------------------------------------------------------------
    Tim Daneliuk tundra at tundraware.com
    PGP Key: http://www.tundraware.com/PGP/
  • Gregory Ewing at Jun 24, 2010 at 7:57 am

    Tim Daneliuk wrote:
    "If 'includefile' contains one or more path separator characters,
    ignore 'FOOPATH'.
    Are you sure that's exactly what you want? Usually with
    such things the distinction is absolute vs. relative,
    not whether there is more than one pathname component.
    E.g. in a C file,

    #include "GL/gl.h"

    will search the include path for a directory called
    "GL" containing a file called "gl.h".

    --
    Greg
  • Tim Daneliuk at Jun 24, 2010 at 12:38 pm

    On 6/24/2010 2:57 AM, Gregory Ewing wrote:
    Tim Daneliuk wrote:
    "If 'includefile' contains one or more path separator characters,
    ignore 'FOOPATH'.
    Are you sure that's exactly what you want? Usually with
    such things the distinction is absolute vs. relative,
    not whether there is more than one pathname component.
    E.g. in a C file,

    #include "GL/gl.h"

    will search the include path for a directory called
    "GL" containing a file called "gl.h".
    Point well taken, thanks.

    --
    ----------------------------------------------------------------------------
    Tim Daneliuk tundra at tundraware.com
    PGP Key: http://www.tundraware.com/PGP/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 23, '10 at 10:27p
activeJun 25, '10 at 12:52p
posts7
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase