pycharmers.utils.argparse_utils module

pycharmers.utils.argparse_utils.ListParamProcessorCreate(type=<class 'str'>)[source]

Create a ListParamProcessor

Parameters

type (type) – type of each element in list.

Returns

Processor which receives list arguments.

Return type

ListParamProcessor (argparse.Action)

Examples

>>> import argparse
>>> from pycharmers.utils import ListParamProcessorCreate
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("--list_params", action=ListParamProcessorCreate())
>>> args = parser.parse_args(args=["--list_params", "[あ, い, う]"])
>>> args.list_params
['あ', 'い', 'う']
class pycharmers.utils.argparse_utils.DictParamProcessor(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)[source]

Bases: argparse.Action

Receive an argument as a dictionary.

Raises

ValueError – You must give one argument for each one keyword.

Examples

>>> import argparse
>>> from pycharmers.utils import DictParamProcessor
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("--dict_params", action=DictParamProcessor)
>>> args = parser.parse_args(args=["--dict_params", "foo = [a, b, c]", "--dict_params", "bar=d"])
>>> args.dict_params
{'foo': ['a', 'b', 'c'], 'bar': 'd'}
>>> args = parser.parse_args(args=["--dict_params", "foo=a, bar=b"])
ValueError: too many values to unpack (expected 2)

Note

If you run from the command line, execute as follows:

$ python app.py --dict_params "foo = [a, b, c]" --dict_params bar=c
class pycharmers.utils.argparse_utils.KwargsParamProcessor(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)[source]

Bases: argparse.Action

Set a new argument.

Examples

>>> import argparse
>>> from pycharmers.utils import KwargsParamProcessor
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("--kwargs", action=KwargsParamProcessor)
>>> args = parser.parse_args(args=["--kwargs", "foo=a", "--kwargs", "bar=b"])
>>> (args.kwargs, args.foo, args.bar)
(None, 'a', 'b')

Note

If you run from the command line, execute as follows:

$ python app.py --kwargs foo=a --kwargs bar=b
pycharmers.utils.argparse_utils.cv2ArgumentParser(parser=None, prog='', description=None, add_help=True, **kwargs)[source]

ArgumentParser for OpenCV project.

Parameters
  • --winname (str) – Window name.

  • --path (str) – Path to video or image.

  • --cam (int) – The ID of the web camera.

  • --ext (str) – The extension for saved image.

  • --gui-width (int) – The width of the GUI tools.

  • --gui-margin (int) – The margin of GUI control tools.

  • --gui-color (list) – The background color of GUI tool.

  • --monitor-size (list) – Monitor size. ( width , height )

  • --autofit (bool) – Whether to fit display size to window size.

  • --twitter (bool) – Whether you want to run for tweet. ( display_size will be () )

  • --capture (bool) – Whether you want to save as video.

pycharmers.utils.argparse_utils.define_neg_sides(args: argparse.Namespace, prefix: str = 'un_')[source]

Define the negative side in argparse.Namespace

I know that action="store_false" can do the similar, but I defined this function because of the readability and ease of use of the code.

Parameters
  • args (argparse.Namespace) – Simple object for storing attributes.

  • prefix (str, optional) – Prefix indicating "Negative" . Defaults to "un_" .

Examples

>>> import argparse
>>> from pycharmers.utils import define_neg_sides
>>> parser = argparse.ArgumentParser(prog="Python-Charmers Examples")
>>> parser.add_argument("un_debug", action="store_true")
>>> parser.add_argument("-ur", "--un-reload", action="store_true")
>>> args = parser.parse_args([])
>>> define_neg_sides(args)
>>> print(f"debug={args.debug}, reload={args.reload}, un_debug={args.un_debug}, un_reload={args.un_reload}")
    debug=False, reload=True, un_debug=True, un_reload=False
>>> args = parser.parse_args(["-ur"])
>>> define_neg_sides(args)
>>> print(f"debug={args.debug}, reload={args.reload}, un_debug={args.un_debug}, un_reload={args.un_reload}")
    debug=False, reload=False, un_debug=True, un_reload=True