"""Utility function for handling options without doubled or trailing spaces."""
-class OptionString(object):
+class OptionString:
"""Class serving as a builder for option strings.
- Motivation: Both manual contatenation and .join() methods
+ Motivation: Both manual concatenation and .join() methods
are prone to leaving superfluous spaces if some parts of options
are optional (missing, empty).
the best fitting one, without much logic near the call site.
"""
- def __init__(self, parts=tuple(), prefix=""):
+ def __init__(self, parts=tuple(), prefix=u""):
"""Create instance with listed strings as parts to use.
Prefix will be converted to string and stripped.
TODO: Support users calling with parts being a string?
- :param parts: List of of stringifiable objects to become parts.
- :param prefix: Subtring to prepend to every parameter (not value).
+ :param parts: List of stringifiable objects to become parts.
+ :param prefix: Substring to prepend to every parameter (not value).
:type parts: Iterable of object
:type prefix: object
"""
:returns: Executable constructor call as string.
:rtype: str
"""
- return "OptionString(parts={parts!r},prefix={prefix!r})".format(
- parts=self.parts, prefix=self.prefix)
+ return f"OptionString(parts={self.parts!r},prefix={self.prefix!r})"
# TODO: Would we ever need a copy() method?
# Currently, superstring "master" is mutable but unique,
:returns: The converted part without prefix, empty means not added.
:rtype: str
"""
- part = "" if part is None else str(part).strip()
+ part = u"" if part is None else str(part).strip()
if part:
prefixed_part = self.prefix + part if prefixed else part
self.parts.append(prefixed_part)
Parameter is prefixed before adding.
:param parameter: Parameter object, usually a word starting with dash.
- :type variable: object
+ :type parameter: object
:returns: Self, to enable method chaining.
:rtype: OptionString
"""
:param parameter: Parameter object, usually a word starting with dash.
:param condition: Do not add if truth value of this is false.
- :type variable: object
+ :type parameter: object
:type condition: object
:returns: Self, to enable method chaining.
:rtype: OptionString
:param parameter: Parameter object, usually a word starting with dash.
:param value: Value object. Prefix is never added.
- :type variable: object
+ :type parameter: object
:type value: object
:returns: Self, to enable method chaining.
:rtype: OptionString
:param parameter: Parameter object, usually a word starting with dash.
:param value: Value object. Prefix is never added.
- :type variable: object
+ :type parameter: object
:type value: object
:returns: Self, to enable method chaining.
:rtype: OptionString
# pylint: disable=protected-access
if temp._check_and_add(parameter, prefixed=True):
if temp._check_and_add(value, prefixed=False):
- self.parts.append("=".join(temp.parts))
+ self.parts.append(u"=".join(temp.parts))
return self
def add_with_value_if(self, parameter, value, condition):
:param parameter: Parameter object, usually a word starting with dash.
:param value: Value object. Prefix is never added.
:param condition: Do not add if truth value of this is false.
- :type variable: object
+ :type parameter: object
:type value: object
:type condition: object
:returns: Self, to enable method chaining.
:param parameter: Parameter object, usually a word starting with dash.
:param value: Value object. Prefix is never added.
:param condition: Do not add if truth value of this is false.
- :type variable: object
+ :type parameter: object
:type value: object
:type condition: object
:returns: Self, to enable method chaining.
self.add_equals(parameter, value)
return self
- def add_with_value_from_dict(self, parameter, key, mapping, default=""):
+ def add_with_value_from_dict(self, parameter, key, mapping, default=u""):
"""Add parameter with value from dict under key, or default.
If key is missing, default is used as value.
value = mapping.get(key, default)
return self.add_with_value(parameter, value)
- def add_equals_from_dict(self, parameter, key, mapping, default=""):
+ def add_equals_from_dict(self, parameter, key, mapping, default=u""):
"""Add parameter=value to options where value is from dict.
If key is missing, default is used as value.
value = mapping.get(key, default)
return self.add_equals(parameter, value)
- def add_if_from_dict(self, parameter, key, mapping, default="False"):
+ def add_if_from_dict(self, parameter, key, mapping, default=u"False"):
"""Add parameter based on if the condition in dict is true.
If key is missing, default is used as condition.
return self.add_if(parameter, condition)
def add_with_value_if_from_dict(
- self, parameter, value, key, mapping, default="False"):
+ self, parameter, value, key, mapping, default=u"False"):
"""Add parameter and value based on condition in dict.
If key is missing, default is used as condition.
return self.add_with_value_if(parameter, value, condition)
def add_equals_if_from_dict(
- self, parameter, value, key, mapping, default="False"):
+ self, parameter, value, key, mapping, default=u"False"):
"""Add parameter=value based on condition in dict.
If key is missing, default is used as condition.
:returns: Space separated string of options.
:rtype: str
"""
- return " ".join(self.parts)
+ return u" ".join(self.parts)