Skip to content

module llutil.argparser

parse arguments for functions and command line.

This module provides helper functions for commonly used argument processing for functions, and a FlexibleArgParser for command line argument parsing. The default singleton of this argument parser is accessable via ice.args.

Global Variables

  • REQUIRED

function isa

isa(obj, types)

an alias for python built-in isinstance.


function parse_scalar

parse_scalar(obj)

function as_list

as_list(maybe_element)

helps to regularize input into list of element.

No matter what is input, will output a list for your iteration.

Basic Examples:

assert as_list("string") == ["string"]
assert as_list(["string", "string"]) == ["string", "string"]
assert as_list(("string", "string")) == ["string", "string"]
assert as_list([["string", "string"]]) == ["string", "string"]

An Application Example:

def func(*args):
    return as_list(args)

assert func("a", "b") == ["a", "b"]
assert func(["a", "b"]) == ["a", "b"]

function as_dict

as_dict(maybe_element, key)

helps to regularize input into a dict.

if maybe_element is not a dict, will return a dict with single key as {key:maybe_element}, else will return maybe_element.

Args:

  • maybe_element: a dict or any object.

  • key: the sole key.

Returns:

  • dict: ensures to be a dict.

Example:

assert as_dict({"k": "v"}, "k") == {"k": "v"}
assert as_dict("v", "k") == {"k": "v"}

function is_seq_of

is_seq_of(seq, expected_type, seq_type=None)

Check whether it is a sequence of some type.

Args:

  • seq (Sequence): The sequence to be checked.

  • expected_type (type): Expected type of sequence items.

  • seq_type (type, optional): Expected sequence type.

Returns:

  • bool: Whether the sequence is valid.

function is_list_of

is_list_of(seq, expected_type)

Check whether it is a list of some type.

A partial method of :func:is_seq_of.


function is_tuple_of

is_tuple_of(seq, expected_type)

Check whether it is a tuple of some type.

A partial method of :func:is_seq_of.


function get_hostname

get_hostname()

class ArgumentMissingError

Raised when a required argument is missing from command line.


class ArgumentTypeError

Raised when converting an argument failed.


class FlexibleArgParser

A flexible and lightweight argument parser that saves loads of code.

This module works differently compared to python built-in argparse module.

  • It accepts two types of command line arguments, i.e. positional and keyword based (options).

  • The keyword based arguments (options) should be specified as key=value or key="value".

  • The positional arguments is indexed directly using an integer, but this feature is not recommended.

Example:

import ice

# same as `python <script>.py 2 k1=4` in shell.
ice.args.parse_args(["2", "k1=4"])

# setdefault() generally is optional.
ice.args.setdefault("k1", 8, int)
ice.args.setdefault("k2", 8)

assert len(ice.args) == 3
assert 2 == int(ice.args[0])  # default type is str.
assert 4 == ice.args["k1"]  # as setdefault specified a type, here a conversion is not needed.
assert 4 == ice.args.k1  # attribute also works.
assert 8 == ice.args.k2  # use default value.

ice.args["k1"] = 1
ice.args.k3 = 1
ice.args.update(k2=0)
ice.args.update({0: -1})
assert -1 == ice.args[0]
assert  1 == ice.args["k3"]
assert  0 == ice.args.k2

Note:

If you manually call parse_args(), call it before setdefault().

method __init__

__init__()  None

method get

get(key)

method hparam_dict

hparam_dict()  Dict

method parse_args

parse_args(argv)

Manually parse args.

Args:

  • argv (List[str]): simillar to sys.argv[1:].

method set

set(key, value, hparam=False)

method setdefault

setdefault(key, default, _type=<class 'str'>, hparam=False, help='')

Set argument value under key as value, only if original entry does not exists.

Args:

  • key (int|str): the keyword.

  • value: default_value to be set when orginal entry does not exists.

Returns:

original or updated value.


method update

update(_FlexibleArgParser__dict={}, **kwds)

simillar to dict.update().