argparse¶
Initializes argparse package of arandomness
- Copyright:
__init.py__ initializes argparse package of arandomness Copyright (C) 2017 Alex Hyer
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
Introduction¶
The argparse
subpackage of arandomness contains scripts and
actions to
expand the utility of Python’s
argparse
library.
CheckThreads¶
CheckThreads
is an argparse
action
, as such, it is called as the
value of the action
argument in argparse
. For example:
from arandomness.argparse import CheckThreads
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-t', '--threads',
action=CheckThreads,
type=int,
default=1,
help='number of threads to use')
args = parser.parse_args()
When -t
is parsed, the value is passed to CheckThreads
which then
checks that the value is between 1 and the maximum number of threads on the
computer as per
multiprocessing.cpu_count().
API Documentation¶
-
class
arandomness.argparse.
CheckThreads
(option_strings, dest, nargs=None, **kwargs)[source]¶ Argparse Action that ensures number of threads requested is valid
-
__call__
(parser, namespace, values, option_string=None)[source]¶ Called by Argparse when user specifies multiple threads
Simply asserts that the number of threads requested is greater than 0 but not greater than the maximum number of threads the computer can support.
Parameters: - parser (ArgumentParser) – parser used to generate values
- namespace (Namespace) – parse_args() generated namespace
- values (int) – actual value specified by user
- option_string (str) – argument flag used to call this function
Raises: TypeError
– if threads is not an integerValueError
– if threads is less than one or greater than number of threads available on computer
-
CopyRight¶
CopyRight
is an argparse
action
that simply takes in text,
strips it of leading and trailing whitespace, prints it, and exits the
program. Its functionality is analogous to
argparse
‘s version. The action can take in arbitrary text and is only named
CopyRight
for code readability.
from arandomness.argparse import CopyRight
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--copyright',
action=CopyRight,
copyright_text='This is my copyright',
help='print copyright and exit')
args = parser.parse_args()
API Documentation¶
-
class
arandomness.argparse.
CopyRight
(option_strings, dest, copyright_text=None, nargs=None, **kwargs)[source]¶ Argparse Action that prints a program copyright
-
option_strings
¶ list – list of str giving command line flags that call this action
-
dest
¶ str – Namespace reference to value
-
nargs
¶ bool – True if multiple arguments specified
-
**kwargs
various – optional arguments to pass to super call
It does not escape my notice that this action prints arbitrary text without any sort of “copyright-specific” attributes or mangling. This function is only called this for readability in code.
-
__call__
(parser, namespace, value, option_string=None)[source]¶ Prints the given text stripped of excess whitespace and exits
Parameters: - parser (ArgumentParser) – parser used to generate values
- namespace (Namespace) – namespace to set values for
- value (str) – actual value specified by user
- option_string (str) – argument flag used to call this function
Raises: TypeError
– if value is not a stringValueError
– if value cannot, for any reason, be parsed by commas
-
Open¶
Open
is an argparse
action
that seamlessly handles reading and
writing compressed files using the
gzip,
bz2, and
lzma libraries. To do this,
Open
actually exposes the arguments of each to libraries *File
function to the command line after automatically selecting the proper
library based on the arguments it receives. Essentially, this action
operates in a read mode and a write/append mode. In read mode, when mode is
equal to any read mode supported by the appropriate library such as r
or
rb
, Open
reads the first few bytes of the file to see what
compression format the file uses and then opens the file with the
corresponding in decompression algorithm. In write mode, basically when mode
is set to anything else, Open
just checks the file extension and maps it
to the corresponding compression algorithm. If Open
does not recognize
the first few bytes of a file or a file extension, it defaults to reading
and writing in plain text.
As aforementioned, Open
exposes the arguments of the underlying library.
It does this by collecting arbitrary arguments, filtering them by the
supported arguments of the *File
functions, and only passing those
arguments to the function. For example, GzipFile
and BZ2File
can
control the level on compression via the argument compresslevel
while
LZMAFile
uses preset
to control compression levels. In order to use
these arguments at the argparse
level, simply add them as options to
Open
as follows:
from arandomness.argparse import Open
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--gzip',
action=Open,
mode='r',
type=str,
compresslevel=9,
help='compressed file to read')
parser.add_argument('--bz2',
action=Open,
mode='w',
type=str,
compresslevel=9
help='compressed file to write')
parser.add_argument('--lzma',
action=Open,
mode='w',
type=str,
preset=9,
help='compressed file to write')
args = parser.parse_args(['-i', 'input.gz', '-o', 'output.xz'])
As stated, this works for any argument and arguments that aren’t supported
by the *File
are silently ignored.
Common use example:
from arandomness.argparse import Open
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-i', '--input',
action=Open,
mode='r',
type=str,
help='compressed file to read')
parser.add_argument('-o', '--output',
action=Open,
mode='w',
type=str,
help='compressed file to write')
args = parser.parse_args(['-i', 'input.gz', '-o', 'output.xz'])
API Documentation¶
-
class
arandomness.argparse.
Open
(option_strings, dest, mode='rb', nargs=None, **kwargs)[source]¶ Argparse Action that detects and opens compressed files for rw
-
option_strings
¶ list – list of str giving command line flags that call this action
-
dest
¶ str – Namespace reference to value
-
mode
¶ str – mode to pass to (de)compression algorithm
-
nargs
¶ bool – True if multiple arguments specified
-
**kwargs
various – optional arguments to pass to argparse and algo
-
__call__
(parser, namespace, value, option_string=None, **kwargs)[source]¶ Detects and opens compressed files
Parameters: - parser (ArgumentParser) – parser used to generate values
- namespace (Namespace) – namespace to set values for
- value (str) – actual value specified by user
- option_string (str) – argument flag used to call this function
- **kwargs (various) – optional arguments later passed to the compression algorithm
-
ParseCommas¶
By default, argparse
parses multiple arguments by spaces. While useful,
it can sometimes be more practical, or at least easier to read, arguments
parsed by commas when multiple arguments make use of
nargs.
ParseCommas
simply takes a string, splits it by commas, and sets the
resulting list as the value for the argument. For example:
from arandomness.argparse import ParseCommas
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-a', '--an_argument',
action=ParseCommas,
type=str,
help='nargs using a string')
args = parser.parse_args(['hello,world'])
print(args.an_argument)
So the argument hello,world
would be set as ['hello', 'world']
in args.
API Documentation¶
-
class
arandomness.argparse.
ParseCommas
(option_strings, dest, nargs=None, **kwargs)[source]¶ Argparse Action that parses arguments by commas
-
option_strings
¶ list – list of str giving command line flags that call this action
-
dest
¶ str – Namespace reference to value
-
nargs
¶ str – number of args as special char or int
-
**kwargs
various – optional arguments to pass to super call
-
__call__
(parser, namespace, value, option_string=None)[source]¶ Called by Argparse when user specifies a comma-separated list
Simply splits a list by commas and adds the values to namespace.
Parameters: - parser (ArgumentParser) – parser used to generate values
- namespace (Namespace) – namespace to set values for
- value (str) – actual value specified by user
- option_string (str) – argument flag used to call this function
Raises: TypeError
– if value is not a stringValueError
– if value cannot, for any reason, be parsed by commas
-