Bases: argparse.Action
An argparse action for adding the common argument of "number of cpus"
to use.
Example usage
parser = argparse.ArgumentParser()
parser.add_argument("--ncpus", action=NCpus)
This will expose an optional argument --ncpus, which can have values
of 1-, as well as "some", "half", "most", and
"all", which corresponds to 25%, 50%, 75% and 100% of the cpus,
respectively. The parsed argument is always an int in range 1-.
Source code in /home/anders/projects/CorpusTools/corpustools/common_arg_ncpus.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 | class NCpus(argparse.Action):
"""An argparse action for adding the common argument of "number of cpus"
to use.
Example usage:
parser = argparse.ArgumentParser()
parser.add_argument("--ncpus", action=NCpus)
This will expose an optional argument --ncpus, which can have values
of 1-<number of cpus on system>, as well as "some", "half", "most", and
"all", which corresponds to 25%, 50%, 75% and 100% of the cpus,
respectively. The parsed argument is always an int in range 1-<n cpus>.
"""
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs is not None:
raise ValueError("nargs not allowed")
some, half = CPU_CHOICES["some"], CPU_CHOICES["half"]
most, all = CPU_CHOICES["most"], CPU_CHOICES["all"]
self.CHOOSE_BETWEEN_STR = (
f"Choose between 1-{N_AVAILABLE_CPUS}, "
f"some ({some}), half ({half}), most ({most}) or all ({all})."
)
if "help" not in kwargs:
kwargs["help"] = (
"The number of cpus to use. If unspecified, defaults to using "
f"as many cpus as it can. {self.CHOOSE_BETWEEN_STR}"
)
super().__init__(option_strings, dest, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
try:
value = CPU_CHOICES[values]
except KeyError:
parser.error(
f"argument '{option_string}': invalid choice "
f"'{values}'. {self.CHOOSE_BETWEEN_STR}"
)
setattr(namespace, self.dest, value)
|