Can Python Module Locate Itself?

Discussion in 'Python' started by sj, Sep 17, 2003.

  1. sj

    sj Guest

    I have written several small shell utilities in Python and typically use
    comments at the start of the source file as documentation. A command line
    option allows the user to read this documentation. The problem is that I
    have to explicitly code the source files location within the source which
    is not very portable. Is there anyway for a python module to locate its
    own source ?

    Thanks
     
    sj, Sep 17, 2003
    #1
    1. Advertising

  2. sj

    Juha Autero Guest

    sj <> writes:

    > I have written several small shell utilities in Python and typically use
    > comments at the start of the source file as documentation. A command line
    > option allows the user to read this documentation. The problem is that I
    > have to explicitly code the source files location within the source which
    > is not very portable. Is there anyway for a python module to locate its
    > own source ?


    I don't know about locating source, but does the documentation have to
    be in comments? I think documentation strings exist for this purpose.

    example.py:
    """
    This is an example file. By using command line argument '-h' user can
    print this documentation.
    """
    import sys
    if sys.argv[1] == "-h":
    print __doc__




    --
    Juha Autero
    http://www.iki.fi/jautero/
    Eschew obscurity!
     
    Juha Autero, Sep 17, 2003
    #2
    1. Advertising

  3. sj

    Kerim Guest

    Try this:

    print 'usage:', __file__

    Kerim
     
    Kerim, Sep 17, 2003
    #3
  4. sj

    netytan Guest

    You can use sys.argv[0] to get the path of the program and use that to
    read in the program file.. you could then parse the source as usual.



    But personally i'd be enclined to go with Juha's idea, it seems more
    elegant and requires less work on the part of the program and the
    programmer..



    Have fun,

    Mark.


    --
    Posted via http://dbforums.com
     
    netytan, Sep 17, 2003
    #4
  5. sj

    Miki Tebeka Guest

    Hello sj,

    > I have written several small shell utilities in Python and typically use
    > comments at the start of the source file as documentation. A command line
    > option allows the user to read this documentation. The problem is that I
    > have to explicitly code the source files location within the source which
    > is not very portable. Is there anyway for a python module to locate its
    > own source ?

    sys.argv[0] is always the name of the script.

    Have you looked at the new optparse module?
    (http://www.python.org/doc/current/lib/module-optparse.html)

    HTH.
    Miki
     
    Miki Tebeka, Sep 17, 2003
    #5
  6. sj> I have written several small shell utilities in Python and typically
    sj> use comments at the start of the source file as documentation. A
    sj> command line option allows the user to read this documentation. The
    sj> problem is that I have to explicitly code the source files location
    sj> within the source which is not very portable. Is there anyway for a
    sj> python module to locate its own source ?

    This doesn't answer your question about locating the source file (yes, you
    can most of the time, check the __file__ global), but I start out with this
    template when creating a new script:

    #!/usr/bin/env python

    """
    Python Script Template

    usage %(prog)s [ -h ] ...

    -h - print this documentation and exit.
    """

    import sys
    import getopt

    prog = sys.argv[0]

    def usage(msg=None):
    if msg is not None:
    print >> sys.stderr, msg
    print >> sys.stderr, __doc__.strip() % globals()

    def main(args):
    try:
    opts, args = getopt.getopt(args, "h", ["--help"])
    except getopt.GetoptError, msg:
    usage(msg)
    return 1

    for opt, arg in opts:
    if opt in ("-h", "--help"):
    usage()
    return 0

    return 0

    if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))

    This guarantees I always have a help capability, no matter how trivial.
    (This is not original with me. I gleaned all the idioms used in this script
    from this group over the years.)

    Skip
     
    Skip Montanaro, Sep 17, 2003
    #6
  7. sj

    sj Guest

    Thanks all for the many useful suggestions.

    One of the reasons I placed the docs in comments is that I have a modified
    version of head which outputs the initial comment block of a file. Its
    smart enough to recognize c++, java, python and lisp commenting styles. On
    reflection I'm not sure this approach offers any advantage over the elegant
    and standard python doc string. Also it would be easy to to update the
    modified head program to look for any initial doc strings.
     
    sj, Sep 18, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. plb
    Replies:
    2
    Views:
    369
  2. prog4ever
    Replies:
    1
    Views:
    365
    Matt Garrish
    May 27, 2004
  3. Jasper
    Replies:
    4
    Views:
    297
    Sisyphus
    Jun 29, 2004
  4. Justin Wyllie
    Replies:
    5
    Views:
    264
    Peter Pan
    Jan 11, 2005
  5. MorningStar
    Replies:
    2
    Views:
    101
    MorningStar
    Aug 2, 2005
Loading...

Share This Page