项目作者: Olllom

项目描述 :
Python working directories
高级语言: Python
项目地址: git://github.com/Olllom/pyworkdir.git
创建时间: 2019-07-25T22:21:21Z
项目社区:https://github.com/Olllom/pyworkdir

开源协议:MIT License

下载


pyworkdir

Travis Build Status
AppVeyor Build status
Documentation Status
codecov
License: MIT
Maintainability
python
Conda Recipe
Anaconda-Server Badge

Python working directories

Quickstart

Anaconda-Server Badge

  1. conda install -c conda-forge pyworkdir

Basic Usage

  1. from pyworkdir import WorkDir
  2. with WorkDir("some_directory"):
  3. # everything in this context is run
  4. # in the specified directory
  5. pass

Directories are Customizable Classes

WorkDir classes can be be customized by adding a file workdir.py to the directory.
All variables, functions, or classes defined in this file will be added as attributes of
the WorkDir instances.

For instance, consider the following workdir.py file:

  1. # -- workdir.py --
  2. def data_file(workdir, filename="data.csv"):
  3. return workdir/filename

The function can now be accessed from other code as follows:

  1. from pyworkdir import WorkDir
  2. with WorkDir() as wd:
  3. print(wd.data_file())

Note that the parameter workdir behaves like the self argument of the method. If workdir is not
an argument of the function, the function behaves like a static method.

By default, the WorkDir instance also recursively inherits attributes defined
in its parent directory’s workdir.py files.
Therefore, subdirectories behave like subclasses.

Directories have a Command Line Interface

Custom functions of the WorkDir are directly accessible from a terminal via the command workdir.
Before being called from the command line, all function parameters (except the reserved keywords workdir and here)
have to be declared as Click options.

  1. # -- workdir.py --
  2. import click
  3. num_apples = 2
  4. @click.option("-c", type=int, default=12, help="A number (default:12)")
  5. @click.option("-s","--somebody", type=str, help="A name")
  6. def hello(count, somebody, workdir):
  7. """This function says hello."""
  8. workdir.num_apples += 1
  9. print(
  10. f"{count} times Hello! to {somebody}: "
  11. f"we have {workdir.num_apples} apples."
  12. )

Calling the function from the command line looks like this:

  1. foo@bar:~$ workdir hello --help
  2. Usage: workdir hello [OPTIONS]
  3. This function says hello.
  4. Options:
  5. -c, --count INTEGER A number (default:12)
  6. -s, --somebody TEXT A name
  7. --help Show this message and exit.
  8. foo@bar:~$ workdir hello -s "you"
  9. 12 times Hello! to you: we have 3 apples.

Writing workdir.py files like this makes it easy to define local functions that can be called both from inside python
and from a terminal. For the latter, the workdir.py behaves similar to a Makefile.

To suppress generation of the command line interface for a function, pyworkdir provides a no_cli decorator.

  1. # -- workdir.py --
  2. from pyworkdir import no_cli
  3. @no_cli
  4. def a_function_without_command_line_interface():
  5. pass

Changing Environment Variables

  1. from pyworkdir import WorkDir
  2. with WorkDir(environment={"MY_ENVIRONMENT_VARIABLE":"1"}):
  3. # in this context the environment variable is set
  4. pass
  5. # outside the context, it is not set any longer

Yaml Files

Environment variables and simple attributes can also be set through yml files.
The templates {{ workdir }} and {{ here }} are available and will be replaced by the working directory
instance and the directory that contains the yml file, respectively.

  1. # -- workdir.yml --
  2. environment:
  3. VAR_ONE: "a"
  4. attributes:
  5. my_number: 1
  6. my_list:
  7. - 1
  8. - 2
  9. - 3
  10. my_tmpdir: {{ here/"tmpdir" }}
  11. my_local_tmpfile: {{ workdir/"file.tmp" }}
  12. commands:
  13. echo: echo Hello // print Hello to the command line

The commands are shortcuts for terminal commands that can be called from python and from the command line.
Everything after // is used as a documentation string for the command line interface.
The attributes and environment variables get added to the WorkDir.

  1. from pyworkdir import WorkDir
  2. with WorkDir() as wd:
  3. print(wd.my_number + 5, wd.my_tmpdir , wd.my_local_tmpfile)
  4. for el in wd.my_list:
  5. print(el)
  6. print(os.environ["VAR_ONE"])

Note that environment variables passed to the constructor have preference over those in a yml file.

Logging

  1. from pyworkdir import WorkDir
  2. import logging
  3. wd = WorkDir()
  4. wd.log("a INFO-level message")
  5. wd.log("a DEBUG-level message", logging.DEBUG)

By default, INFO-level and higher is printed to the console.
DEBUG-level output is only printed to a file workdir.log.

Documentation

Documentation Status

Copyright (c) 2019, Andreas Krämer

Acknowledgements

External Packages:

Project based on the
Computational Molecular Science Python Cookiecutter version 1.0.