Trying to Learn Packages

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

    Trying to Learn Packages

    Hi,

    I'm making a project into my first package, mainly for organization, but
    also to learn how to do it. I have a number of data files, both
    experimental results and PNG files. My project is organized as a root
    directory, with two subdirectories, src and data, and directory trees
    below them. I put the root directory in my pythonpath, and I've no
    trouble accessing the modules, but for the data, I'm giving relative
    paths, that depend on the current working directory.

    This has obvious drawbacks, and hard-coding a directory path is worse.
    Where the data files are numbers, I can simply incorporate them in
    python scripts that initialize data structures , but what can I do with
    the image files?

    What is the usual way of dealing with this?

    Thanks,
    Saul
  • David

    #2
    Re: Trying to Learn Packages

    On Sun, Jun 22, 2008 at 4:07 PM, Saul Spatz <sspatz@kcnet.c omwrote:
    Hi,
    >
    I'm making a project into my first package, mainly for organization, but
    also to learn how to do it. I have a number of data files, both
    experimental results and PNG files. My project is organized as a root
    directory, with two subdirectories, src and data, and directory trees below
    them. I put the root directory in my pythonpath, and I've no trouble
    accessing the modules, but for the data, I'm giving relative paths, that
    depend on the current working directory.
    >
    This has obvious drawbacks, and hard-coding a directory path is worse. Where
    the data files are numbers, I can simply incorporate them in python scripts
    that initialize data structures , but what can I do with the image files?
    >
    What is the usual way of dealing with this?
    >
    The usual method (afaik) is to use relative paths, and to not change
    into the script dirs before running them.

    eg, instead of:

    cd src/dir1/dir2/dir3
    ../script.py

    You run it like this:
    ../src/dir1/dir2/dir3/script.py

    And then all the scripts under src can load data files using a path
    like this: './data/datafile'

    Another method is to use a file-finder func which looks in various
    places (using PATH, PYHONPATH, etc), and returns the first-found path
    to the file.

    Comment

    • =?iso-8859-1?q?C=E9dric_Lucantis?=

      #3
      Re: Trying to Learn Packages

      Le Sunday 22 June 2008 16:07:37 Saul Spatz, vous avez écrit :
      Hi,
      >
      I'm making a project into my first package, mainly for organization, but
      also to learn how to do it. I have a number of data files, both
      experimental results and PNG files. My project is organized as a root
      directory, with two subdirectories, src and data, and directory trees
      below them. I put the root directory in my pythonpath, and I've no
      trouble accessing the modules, but for the data, I'm giving relative
      paths, that depend on the current working directory.
      >
      This has obvious drawbacks, and hard-coding a directory path is worse.
      Where the data files are numbers, I can simply incorporate them in
      python scripts that initialize data structures , but what can I do with
      the image files?
      For a small project you can do the same with images or any kind of data, for
      instance by serializing your objects with pickle in ascii mode and putting
      the result in a string constant (well, I never did it and can't guarantee it
      will work, this is only an example of what people do in many situations)
      >
      What is the usual way of dealing with this?
      >
      A more conventional way is to provide a configure script to run before
      compiling/installing. That script should let the user choose where to install
      datafiles with a command line option such as --datadir=/path or provide a
      reasonable default value. Then it will auto-generate some code defining this
      value as a global variable, to make it accessible to the rest of your own
      code. Ideally, your app would also provide a command line option or an
      environment variable to override this hard-coded setting at runtime.

      But maybe the distutils tools have some features for this, I don't know enough
      of them to tell that.

      --
      Cédric Lucantis

      Comment

      • Saul Spatz

        #4
        Re: Trying to Learn Packages

        Cédric Lucantis wrote:
        Le Sunday 22 June 2008 16:07:37 Saul Spatz, vous avez écrit :
        >Hi,
        >>
        >I'm making a project into my first package, mainly for organization, but
        >also to learn how to do it. I have a number of data files, both
        >experimental results and PNG files. My project is organized as a root
        >directory, with two subdirectories, src and data, and directory trees
        >below them. I put the root directory in my pythonpath, and I've no
        >trouble accessing the modules, but for the data, I'm giving relative
        >paths, that depend on the current working directory.
        >>
        >This has obvious drawbacks, and hard-coding a directory path is worse.
        >Where the data files are numbers, I can simply incorporate them in
        >python scripts that initialize data structures , but what can I do with
        >the image files?
        >
        For a small project you can do the same with images or any kind of data, for
        instance by serializing your objects with pickle in ascii mode and putting
        the result in a string constant (well, I never did it and can't guarantee it
        will work, this is only an example of what people do in many situations)
        >
        >What is the usual way of dealing with this?
        >>
        >
        A more conventional way is to provide a configure script to run before
        compiling/installing. That script should let the user choose where to install
        datafiles with a command line option such as --datadir=/path or provide a
        reasonable default value. Then it will auto-generate some code defining this
        value as a global variable, to make it accessible to the rest of your own
        code. Ideally, your app would also provide a command line option or an
        environment variable to override this hard-coded setting at runtime.
        >
        But maybe the distutils tools have some features for this, I don't know enough
        of them to tell that.
        >
        A couple of good ideas here. Thanks.

        Comment

        Working...