Python working directories
conda install -c conda-forge pyworkdirfrompyworkdirimportWorkDirwithWorkDir("some_directory"):
# everything in this context is run # in the specified directorypassWorkDir 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:
# -- workdir.py --defdata_file(workdir, filename="data.csv"):
returnworkdir/filenameThe function can now be accessed from other code as follows:
frompyworkdirimportWorkDirwithWorkDir() aswd:
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.
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.
# -- workdir.py --importclicknum_apples=2@click.option("-c", type=int, default=12, help="A number (default:12)")@click.option("-s","--somebody", type=str, help="A name")defhello(count, somebody, workdir):
"""This function says hello."""workdir.num_apples+=1print(
f"{count} times Hello! to {somebody}: "f"we have {workdir.num_apples} apples."
)Calling the function from the command line looks like this:
foo@bar:~$ workdir hello --helpUsage: workdir hello [OPTIONS] This function says hello.Options: -c, --count INTEGER A number (default:12) -s, --somebody TEXT A name --help Show this message and exit.foo@bar:~$ workdir hello -s "you"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.
# -- workdir.py --frompyworkdirimportno_cli@no_clidefa_function_without_command_line_interface():
passfrompyworkdirimportWorkDirwithWorkDir(environment={"MY_ENVIRONMENT_VARIABLE":"1"}):
# in this context the environment variable is setpass# outside the context, it is not set any longerEnvironment 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.
# -- workdir.yml --
environment:
VAR_ONE: "a"
attributes:
my_number: 1
my_list:
- 1
- 2
- 3
my_tmpdir: {{ here/"tmpdir" }}
my_local_tmpfile: {{ workdir/"file.tmp" }}
commands:
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.
frompyworkdirimportWorkDirwithWorkDir() aswd:
print(wd.my_number+5, wd.my_tmpdir , wd.my_local_tmpfile)
forelinwd.my_list:
print(el)
print(os.environ["VAR_ONE"])Note that environment variables passed to the constructor have preference over those in a yml file.
frompyworkdirimportWorkDirimportloggingwd=WorkDir()
wd.log("a INFO-level message")
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.
Copyright (c) 2019, Andreas Krämer
External Packages:
Project based on the Computational Molecular Science Python Cookiecutter version 1.0.