schrodinger.application.matsci.parserutils module¶
Utilities for working with argument parsers
Copyright Schrodinger, LLC. All rights reserved.
-
schrodinger.application.matsci.parserutils.type_ranged_num(arg, top=None, bottom=0.0, top_allowed=False, bottom_allowed=False, typer=<class 'float'>)[source]¶ Validate that the argument is a number over a given range. By default, it is (0, +inf)
- Note
This function can be used to directly create argparse typing for
- numbers within a specified range using a lambda function such as:
- eqopts.add_argument(
FLAG_TEMP, action=’store’, type=lambda x:parserutils.type_ranged_num(x, top=423, bottom=273), metavar=’KELVIN’, help=’Simulation temperature for MD equilibration step’)
- Parameters
arg (str) – The argument to validate
top (float or int) – The upper limit of the allowed range
bottom (float or int) – The lower limit of the allowed range
top_allowed (bool) – Whether arg may take the top value or not (whether the upper limit is inclusive or exclusive)
bottom_allowed (bool) – Whether arg may take the bottom value or not (whether the bottom limit is inclusive or exclusive)
typer (callable) – Should be one of the built-in float or int functions and defines the type of the value returned from this function.
- Return type
float or int
- Returns
The argument converted to a float or int, depending on the value of the typer keyword argument
- Raises
argparse.ArgumentTypeError – If the argument cannot be converted to a ranged floating point number in the given range
-
schrodinger.application.matsci.parserutils.type_ranged_int(*args, **kwargs)[source]¶ Validate that the argument is an int over a given range
see type_ranged_num for documentation of arguments, return values and exceptions raised
-
schrodinger.application.matsci.parserutils.get_ranged_int_typer(bottom_allowed=True, top_allowed=True, **kwargs)[source]¶ Get a validator that will validate an integer over a custom range
- Usage::
- parser.add_argument(
A_FLAG, type=get_ranged_int_typer(bottom=4, top=12, bottom_allowed=False))
See type_ranged_num for documentation of arguments, return values and exceptions raised
-
schrodinger.application.matsci.parserutils.get_ranged_float_typer(bottom_allowed=True, top_allowed=True, **kwargs)[source]¶ Get a validator that will validate a float over a custom range
- Usage::
- parser.add_argument(
A_FLAG, type=get_ranged_float_typer(bottom=-5.0, top=5.0, top_allowed=False))
See type_ranged_num for documentation of arguments, return values and exceptions raised
-
schrodinger.application.matsci.parserutils.type_positive_float(arg)[source]¶ Validate that the argument is a positive float
see type_ranged_num for documentation of arguments, return values and exceptions raised
-
schrodinger.application.matsci.parserutils.type_negative_float(arg)[source]¶ Validate that the argument is a negative float
see type_ranged_num for documentation of arguments, return values and exceptions raised
-
schrodinger.application.matsci.parserutils.type_positive_int(arg)[source]¶ Validate that the argument is a positive int
see type_ranged_num for documentation of arguments, return values and exceptions raised
-
schrodinger.application.matsci.parserutils.type_nonnegative_int(arg)[source]¶ Validate that the argument is a nonnegative int
see type_ranged_num for documentation of arguments, return values and exceptions raised
-
schrodinger.application.matsci.parserutils.type_nonpositive_float(arg)[source]¶ Validate that the argument is a nonpositive float
see type_ranged_num for documentation of arguments, return values and exceptions raised
-
schrodinger.application.matsci.parserutils.type_nonzero_percent(arg)[source]¶ Validate that the argument is a percent but not zero
see type_ranged_num for documentation of arguments, return values and exceptions raised
-
schrodinger.application.matsci.parserutils.type_random_seed(arg, seed_min=0, seed_max=2147483647)[source]¶ Validate that the argument is a valid random seed value. If a random value is requested, that value is generated and returned.
- Parameters
arg (str) – The argument to validate
seed_min (int) – The minimum allowed random number seed
seed_max (int) – The maximum allowed random number seed
- Return type
int
- Returns
The random seed
- Raises
argparse.ArgumentTypeError – If the argument is not RANDOM_SEED_RANDOM or an integer
-
schrodinger.application.matsci.parserutils.type_file(arg)[source]¶ Validate that the argument is an existing filename
- Parameters
arg (str or unicode) – The argument to validate
- Return type
str
- Returns
The str-ed argument
- Raises
argparse.ArgumentTypeError – If the given filename does not exist
-
schrodinger.application.matsci.parserutils.type_nonnegative_float(arg)[source]¶ Validate that the argument is a nonnegative float
see type_ranged_num for documentation of arguments, return values and exceptions raised
-
schrodinger.application.matsci.parserutils.get_trj_dir_name(st)[source]¶ Get the trajectory directory name from the given structure.
- Parameters
st (schrodinger.structure.Structure) – the structure
- Return type
str or None
- Returns
the trajectory directory name or None if there isn’t one
-
schrodinger.application.matsci.parserutils.type_cms_file(arg, ensure_trj=False)[source]¶ Validate that the argument is a cms file.
- Parameters
arg (str or unicode) – the argument to validate
ensure_trj (bool) – ensure that the cms file has trajectory information, for example is a Desmond output file
- Raises
argparse.ArgumentTypeError – if the given argument is not a cms file
- Return type
str
- Returns
the str-ed argument
-
schrodinger.application.matsci.parserutils.type_xyz_file(arg)[source]¶ Validate that the argument is an existing filename with xyz extension and can be converted into sdf format.
- Parameters
arg (The argument to validate) – str
- Returns
str
- Return type
The validated argument
- Raises
argparse.ArgumentTypeError – If the given filename does not exist
argparse.ArgumentTypeError – If the given filename does not have xyz as file extension.
argparse.ArgumentTypeError – If the given filename cannot be converted
-
schrodinger.application.matsci.parserutils.type_cms_with_trj(arg)[source]¶ Validate that the argument is a cms file with trajectory information.
- Parameters
arg (str or unicode) – the argument to validate
- Return type
str
- Returns
the str-ed argument
-
schrodinger.application.matsci.parserutils.type_json_file(arg)[source]¶ Validate that the argument is a json file.
- Parameters
arg (str or unicode) – the argument to validate
- Raises
argparse.ArgumentTypeError – if the given argument is not a json file
- Return type
str
- Returns
the str-ed argument
-
schrodinger.application.matsci.parserutils.type_yaml_file(arg)[source]¶ Validate that the argument is a yaml file.
- Parameters
arg (str or unicode) – the argument to validate
- Raises
argparse.ArgumentTypeError – if the given argument is not a yaml file
- Return type
str
- Returns
the str-ed argument
-
schrodinger.application.matsci.parserutils.type_yaml_str(arg)[source]¶ Load the arg as a yaml string.
- Parameters
arg (str or unicode) – the argument to validate
- Raises
argparse.ArgumentTypeError – if the given argument is not in yaml format
- Return type
dict
- Returns
loaded yaml string
-
schrodinger.application.matsci.parserutils.type_structure_file(arg)[source]¶ Validate that the argument is a structure file that can be read by StructureReader.
- Parameters
arg (str or unicode) – the argument to validate
- Raises
argparse.ArgumentTypeError – if the given argument is not a readable structure file
- Return type
str
- Returns
the str-ed argument
-
schrodinger.application.matsci.parserutils.type_periodic_structure_file(arg)[source]¶ Validate that the argument is a periodic structure file (priority is chorus followed by pdb).
- Parameters
arg (str or unicode) – the argument to validate
- Raises
argparse.ArgumentTypeError – if the given argument is not a periodic structure file
- Return type
str
- Returns
the str-ed argument
-
schrodinger.application.matsci.parserutils.type_element(value)[source]¶ Validate that the argument is a valid atomic atymbol
- Parameters
value (str) – The argument to validate
- Return type
str
- Returns
The validated argument
- Raises
argparse.ArgumentTypeError – If the argument is not a valid atomic symbol
-
schrodinger.application.matsci.parserutils.type_forcefield(value)[source]¶ Validate that the argument is a valid force field name
This accepts both OPLS3 and OPLS3e for force field #16
- Parameters
value (str) – The argument to validate
- Return type
str
- Returns
The canonical force field name
- Raises
argparse.ArgumentTypeError – If the argument is not a valid atomic symbol
-
schrodinger.application.matsci.parserutils.type_forcefield_and_get_number(value)[source]¶ Validate that the argument is a valid force field name and returns both the canonical force field name and number.
This accepts both OPLS3 and OPLS3e for force field #16
- Parameters
value (str) – The argument to validate
- Return type
str, int
- Returns
The canonical force field name and the corresponding force field int
- Raises
argparse.ArgumentTypeError – If the argument is not a valid atomic symbol
-
schrodinger.application.matsci.parserutils.type_desmond_ensemble(value)[source]¶ Validate that the argument is a valid desmond ensemble
- Parameters
value (str) – The argument to validate
- Return type
str
- Returns
The validated argument
- Raises
argparse.ArgumentTypeError – If the argument is not a valid desmond ensemble
-
schrodinger.application.matsci.parserutils.valid_forcefield_info()[source]¶ Get an informative sentence that can be included in help messages that indicate the valid force field names.
Note that these are the default names and type_forcefield will accept many variants of them, including OPLS3 for OPLS3E.
- Return type
str
- Returns
A full sentence describing the valid force field names
-
class
schrodinger.application.matsci.parserutils.NoArgCustomAction(*args, **kwargs)[source]¶ Bases:
argparse.ActionAn action that allows giving an argparse flag a custom action so that the flag does not require an argument
One possible use is the implementation of a flag like ‘-doc’ which immediately performs an action when it is found on the command line - such as printing out detailed documentation and then exiting. This avoids the user having to specify any other flags even if they are marked as required.
-
class
schrodinger.application.matsci.parserutils.KeyPairDict[source]¶ Bases:
dictThis is a subclass of dict so that the {a:b} can be displayed as “a=b” (e.g, as the help message”
-
__contains__(key, /)¶ True if the dictionary has the specified key, else False.
-
__init__(*args, **kwargs)¶ Initialize self. See help(type(self)) for accurate signature.
-
__len__()¶ Return len(self).
-
clear() → None. Remove all items from D.¶
-
copy() → a shallow copy of D¶
-
fromkeys(value=None, /)¶ Create a new dictionary with keys from iterable and values set to value.
-
get(key, default=None, /)¶ Return the value for key if key is in the dictionary, else default.
-
items() → a set-like object providing a view on D’s items¶
-
keys() → a set-like object providing a view on D’s keys¶
-
pop(k[, d]) → v, remove specified key and return the corresponding value.¶ If key is not found, d is returned if given, otherwise KeyError is raised
-
popitem()¶ Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.
-
setdefault(key, default=None, /)¶ Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
-
update([E, ]**F) → None. Update D from dict/iterable E and F.¶ If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
-
values() → an object providing a view on D’s values¶
-
-
class
schrodinger.application.matsci.parserutils.StoreDictKeyPair(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)[source]¶ Bases:
argparse._StoreActionThis is the action used with nargs=”+” and values=’KEYWORD=VALUE KEYWORD=VALUE’
By default nargs=”+” and action=’store’ save the multiple values “k1=v1 k2=v2” as list. With this StoreDictKeyPair as the action, “k1=v1 k2=v2” will be stored as {k1:v1, k2:v2} in the options.
-
__init__(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)¶ Initialize self. See help(type(self)) for accurate signature.
-
-
schrodinger.application.matsci.parserutils.type_keywords_to_dict(value)[source]¶ Convert a string of ‘keyword=value keyword=value …’ to a dictionary keyed by keyword. Values are the keyword values.
- Note
When calling parser.add_argument for the flag that uses this function, do not use “nargs=+”. That will result in the return value from this function being placed as an item in a list. Instead, do not use nargs at all and simply use “action=’store’”. All of the keyword=value pairs will then be passed into this function as a single string. Example:
- parser.add_argument(
jobutils.FLAG_KEYWORDS, action=’store’, metavar=’KEYWORD=VALUE KEYWORD=VALUE …’, type=parserutils.type_keywords_to_dict, help=’Jaguar keywords given as a space-separated keyword=value ‘
‘ pairs’
)
will result in options.keyword = the dictionary returned by this function
- Parameters
value (str) – The argument to validate
- Return type
dict
- Returns
A dictionary with keywords as key keyword values as values
- Raises
argparse.ArgumentTypeError – If the argument is not a valid keyword string
-
schrodinger.application.matsci.parserutils.type_keywords_to_string(value)[source]¶ Validate the format of a string of ‘keyword=value keyword=value …’
- Note
When calling parser.add_argument for the flag that uses this function, do not use “nargs=+”. That will result in the return value from this function being placed as an item in a list. Instead, do not use nargs at all and simply use “action=’store’”. All of the keyword=value pairs will then be passed into this function as a single string. Example:
- parser.add_argument(
jobutils.FLAG_KEYWORDS, action=’store’, metavar=’KEYWORD=VALUE KEYWORD=VALUE …’, type=parserutils.type_keywords_to_string, help=’Jaguar keywords given as a space-separated keyword=value ‘
‘ pairs’
)
will result in options.keyword = the string returned by this function
- Parameters
value (str) – The argument to validate
- Return type
string
- Returns
The original command line string
- Raises
argparse.ArgumentTypeError – If the argument is not a valid keyword string
-
schrodinger.application.matsci.parserutils.type_num_list(value, delimiter=', ', typer=<class 'float'>, bottom=None, bottom_allowed=False)[source]¶ Validate that the argument is a list of numbers separated by the delimiter
- Parameters
value (str) – string containing numbers separated by delimiter
delimiter (str) – the char to split the input value
typer (callable) – Should be one of the built-in float or int functions and defines the type of the value returned from this function.
bottom (float or int) – The lower limit of the allowed range
bottom_allowed (bool) – Whether arg may take the bottom value or not (whether the bottom limit is inclusive or exclusive)
- Return type
list
- Returns
The validated argument converted to a list of numbers
- Raises
argparse.ArgumentTypeError – If the argument is not a valid list of numbers
-
schrodinger.application.matsci.parserutils.type_num_comma_list(value, typer=<class 'float'>, bottom=None, bottom_allowed=False)[source]¶ Validate that the argument is a list of numbers separated by comma
- Parameters
value (str) – string containing numbers separated by commas
typer (callable) – Should be one of the built-in float or int functions and defines the type of the value returned from this function.
bottom (float or int) – The lower limit of the allowed range
bottom_allowed (bool) – Whether arg may take the bottom value or not (whether the bottom limit is inclusive or exclusive)
- Return type
list
- Returns
The validated argument positive integers coverted to list
- Raises
argparse.ArgumentTypeError – If the argument is not a valid list of floats
-
schrodinger.application.matsci.parserutils.type_num_colon_list(value, typer=<class 'float'>, bottom=None, bottom_allowed=False)[source]¶ Validate that the argument is a list of numbers separated by colon
- Parameters
value (str) – string containing numbers separated by colon
typer (callable) – Should be one of the built-in float or int functions and defines the type of the value returned from this function.
bottom (float or int) – The lower limit of the allowed range
bottom_allowed (bool) – Whether arg may take the bottom value or not (whether the bottom limit is inclusive or exclusive)
- Return type
list
- Returns
The validated argument positive integers coverted to list
- Raises
argparse.ArgumentTypeError – If the argument is not a valid list of floats
-
schrodinger.application.matsci.parserutils.type_float_comma_list(value)[source]¶ Validate that the argument is a float list separated by comma
- Parameters
value (str) – string containing numbers separated by commas
- Return type
list
- Returns
The validated argument positive integers converted to list
- Raises
argparse.ArgumentTypeError – If the argument is not a valid list of floats
-
schrodinger.application.matsci.parserutils.type_nonnegative_float_comma_list(value)[source]¶ Validate that the argument is a nonnegative float list separated by comma
- Parameters
value (str) – string containing numbers separated by commas
- Return type
list
- Returns
The validated argument positive integers converted to list
- Raises
argparse.ArgumentTypeError – If the argument is not a valid list of floats
-
schrodinger.application.matsci.parserutils.type_positive_integer_comma_list(value)[source]¶ Validate that the argument is a integer list separated by comma
- Parameters
value (str) – string containing numbers separated by commas
- Return type
list
- Returns
The validated argument positive integers coverted to list
- Raises
argparse.ArgumentTypeError – If the argument is not a valid list of positive integers
-
schrodinger.application.matsci.parserutils.type_element_comma_list(value)[source]¶ Validate that the argument is a element separated by comma
- Return type
list
- Returns
The validated argument list of element symbols
- Raises
argparse.ArgumentTypeError – If the argument is not a valid list of strings
-
schrodinger.application.matsci.parserutils.type_percentage(value)[source]¶ Validate that the argument is a percentage between 0 and 100
see type_ranged_num for documentation of arguments, return values and exceptions raised
-
schrodinger.application.matsci.parserutils.type_asl(asl)[source]¶ Validate ASL.
- Return type
str
- Returns
ASL
- Raises
argparse.ArgumentTypeError – If ASL is not valid
-
schrodinger.application.matsci.parserutils.type_desmond_cfg(arg)[source]¶ Validate that the argument is a desmond CFG file.
- Parameters
arg (str or unicode) – the argument to validate
- Raises
argparse.ArgumentTypeError – if the given argument is not a readable CFG file
- Return type
str
- Returns
the str-ed argument
-
class
schrodinger.application.matsci.parserutils.DriverParser(*args, **kwargs)[source]¶ Bases:
argparse.ArgumentParserSubclass that shows driver usage relative to python/scripts or python/common directories and adds a help argument by default
-
add_argument(dest, ..., name=value, ...)¶ add_argument(option_string, option_string, …, name=value, …)
-
add_argument_group(*args, **kwargs)¶
-
add_mutually_exclusive_group(**kwargs)¶
-
add_subparsers(**kwargs)¶
-
convert_arg_line_to_args(arg_line)¶
-
error(message: string)¶ Prints a usage message incorporating the message to stderr and exits.
If you override this in a subclass, it should not return – it should either exit or raise an exception.
-
exit(status=0, message=None)¶
-
format_help()¶
-
format_usage()¶
-
get_default(dest)¶
-
parse_args(args=None, namespace=None)¶
-
parse_intermixed_args(args=None, namespace=None)¶
-
parse_known_args(args=None, namespace=None)¶
-
parse_known_intermixed_args(args=None, namespace=None)¶
-
print_help(file=None)¶
-
print_usage(file=None)¶
-
register(registry_name, value, object)¶
-
set_defaults(**kwargs)¶
-