Changeset - a70552534acd
[Not reviewed]
0 1 0
Lance Edgar (lance) - 7 years ago 2018-02-12 17:43:39
lance@edbob.org
Tweak `util.pretty_hours()` logic per python 3
1 file changed with 1 insertions and 1 deletions:
0 comments (0 inline, 0 general)
rattail/util.py
Show inline comments
 
@@ -94,146 +94,146 @@ def import_module_path(module_path):
 
    return last_module(module, module_path)
 

	
 

	
 
def get_object_spec(obj):
 
    """
 
    Returns the "object spec" string for the given object.
 
    """
 
    # TODO: this only handles a class *instance* currently
 
    return '{}:{}'.format(obj.__module__, obj.__class__.__name__)
 

	
 

	
 
def load_object(specifier):
 
    """
 
    Load an arbitrary object from a module, according to a specifier.
 

	
 
    The specifier string should contain a dotted path to an importable module,
 
    followed by a colon (``':'``), followed by the name of the object to be
 
    loaded.  For example:
 

	
 
    .. code-block:: none
 

	
 
       rattail.files:overwriting_move
 

	
 
    You'll notice from this example that "object" in this context refers to any
 
    valid Python object, i.e. not necessarily a class instance.  The name may
 
    refer to a class, function, variable etc.  Once the module is imported, the
 
    ``getattr()`` function is used to obtain a reference to the named object;
 
    therefore anything supported by that method should work.
 

	
 
    :param specifier: Specifier string.
 

	
 
    :returns: The specified object.
 
    """
 
    module_path, name = specifier.split(':')
 
    module = import_module_path(module_path)
 
    return getattr(module, name)
 

	
 

	
 
def load_entry_points(group):
 
    """
 
    Load a set of ``setuptools``-style entry points.
 

	
 
    This is a convenience wrapper around ``pkg_resources.iter_entry_points()``.
 

	
 
    :param group: The group of entry points to be loaded.
 

	
 
    :returns: A dictionary whose keys are the entry point names, and values are
 
       the loaded entry points.
 
    """
 
    entry_points = {}
 
    for entry_point in iter_entry_points(group):
 
        entry_points[entry_point.name] = entry_point.load()
 
    return entry_points
 

	
 

	
 
def prettify(text):
 
    """
 
    Return a "prettified" version of text.
 
    """
 
    text = text.replace('_', ' ')
 
    text = text.replace('-', ' ')
 
    words = text.split()
 
    return ' '.join([x.capitalize() for x in words])
 

	
 

	
 
def pretty_boolean(value):
 
    """
 
    Returns ``'Yes'`` or ``'No'`` or empty string if value is ``None``
 
    """
 
    if value is None:
 
        return ""
 
    return "Yes" if value else "No"
 

	
 

	
 
def hours_as_decimal(hours):
 
    """
 
    Convert the given ``datetime.timedelta`` object into a Decimal whose
 
    value is in terms of hours.
 
    """
 
    if hours is None:
 
        return
 
    minutes = (hours.days * 1440) + (hours.seconds / 60)
 
    return decimal.Decimal('{:0.2f}'.format(minutes / 60.0))
 

	
 

	
 
def pretty_hours(hours=None, seconds=None):
 
    """
 
    Format the given ``hours`` value (which is assumed to be a
 
    ``datetime.timedelta`` object) as HH:MM.  Note that instead of providing
 
    that, you can provide ``seconds`` instead and a delta will be generated.
 
    """
 
    if hours is None and seconds is None:
 
        return ''
 
    if hours is None:
 
        hours = datetime.timedelta(seconds=seconds)
 
    minutes = (hours.days * 1440) + (hours.seconds / 60)
 
    return '{}:{:02d}'.format(minutes // 60, minutes % 60)
 
    return "{}:{:02d}".format(int(minutes // 60), int(minutes % 60))
 

	
 

	
 
def pretty_quantity(value, empty_zero=False):
 
    """
 
    Return a "pretty" version of the given value, as string.  This is meant primarily
 
    for use with things like order quantities, so that e.g. 1.0000 => 1
 
    """
 
    if value is None:
 
        return ''
 
    if int(value) == value:
 
        value = int(value)
 
        if empty_zero and value == 0:
 
            return ''
 
        return six.text_type(value)
 
    return six.text_type(value).rstrip('0')
 

	
 

	
 
def progress_loop(func, items, factory, *args, **kwargs):
 
    """
 
    This will iterate over ``items`` and call ``func`` for each.  If a progress
 
    ``factory`` kwarg is provided, then a progress instance will be created and
 
    updated along the way.
 
    """
 
    message = kwargs.pop('message', None)
 
    count = kwargs.pop('count', None)
 
    allow_cancel = kwargs.pop('allow_cancel', False)
 
    if count is None:
 
        try:
 
            count = len(items)
 
        except TypeError:
 
            count = items.count()
 
    if not count:
 
        return True
 

	
 
    prog = None
 
    if factory:
 
        prog = factory(message, count)
 

	
 
    canceled = False
 
    for i, item in enumerate(items, 1):
 
        func(item, i, *args, **kwargs)
 
        if prog and not prog.update(i):
 
            canceled = True
 
            break
 
    if prog:
 
        prog.destroy()
 
    if canceled and not allow_cancel:
 
        raise RuntimeError("Operation was canceled")
 
    return not canceled
0 comments (0 inline, 0 general)