SmartIndexingAction#
- class SmartIndexingAction(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None, deprecated=False)[source]#
Bases:
Action
Custom argparse action to handle smart indexing of frame numbers.
Parses a comma-separated list of frame numbers with optional ranges (e.g., ‘1-20, 34, 25, 50-100’) and generates a list of individual frame numbers. Modifies the namespace by setting the attribute specified by the ‘dest’ parameter to the list of individual frame numbers.
- Parameters:
- parser: argparse.ArgumentParser
The argparse parser object.
- namespace: argparse.Namespace
The argparse namespace.
- values: str
The input string containing frame numbers and ranges.
- option_string: str, default=None
The option string.
- Attributes:
- deststr
The attribute name in the namespace where the parsed list will be stored.
Methods
__call__(parser, namespace, values, option_string=None)
Parses the provided string of values into a sorted list of integers and sets it as an attribute in the namespace.
Examples
>>> parser = argparse.ArgumentParser() >>> parser.add_argument("-fl", "--frame_list", metavar="FRAME_LIST", help="Smart-indexed list of 1-indexed Frame Numbers within trajectory to analyze", required=False, action=SmartIndexingAction) >>> args = parser.parse_args(["-fl", "1-20,34,25,50-100"]) >>> print(args.frame_list) [1, 2, ..., 20, 34, 25, 50, 51, ..., 100]
Methods Summary
parse_smart_index
(value)Checks that an inputted variable is a list that can be smart indexed and indexes it if necessary.
Methods Documentation
- format_usage()#
- static parse_smart_index(value)[source]#
Checks that an inputted variable is a list that can be smart indexed and indexes it if necessary.
- Parameters:
- value{str, set, list}
The input string containing ranges (e.g., “1-5,10,15-20”) or a set of integers.
- Returns:
- set
A set of integers parsed from the input.
- Raises:
- ValueError
If the input is not a string or set.
Examples
>>> import stacker as st >>> st.SmartIndexingAction.parse_smart_index('1-5,8,12-17') {1, 2, 3, 4, 5, 8, 12, 13, 14, 15, 16, 17}
- static parse_smart_index(value)[source]#
Checks that an inputted variable is a list that can be smart indexed and indexes it if necessary.
- Parameters:
- value{str, set, list}
The input string containing ranges (e.g., “1-5,10,15-20”) or a set of integers.
- Returns:
- set
A set of integers parsed from the input.
- Raises:
- ValueError
If the input is not a string or set.
Examples
>>> import stacker as st >>> st.SmartIndexingAction.parse_smart_index('1-5,8,12-17') {1, 2, 3, 4, 5, 8, 12, 13, 14, 15, 16, 17}
- __init__(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None, deprecated=False)#
- _get_args()#
- _get_kwargs()#
- format_usage()#