Example #1
0
 def _bucket_scalar(cls, val, step, head):
   if pdutils.is_empty(val):
     return None
   if (cls.TAIL is not None) and (val == cls.TAIL):
     val = cls.TAIL - (0.5 * step)
   index = math.floor((val - head) / step)
   return head + (index * step)
Example #2
0
 def _coax_scalar(cls, obj, fmt='%m'):
   if pdutils.is_empty(obj):
     return None
   if isinstance(obj, (datetime.datetime, pd.Timestamp)):
     return obj.month
   if isinstance(obj, str) and (fmt is not None):
     return pd.to_datetime(obj, format=fmt, errors=COERCE).month
   return int(obj)
Example #3
0
def weighted_tqdm(items, weights=None, default_weight=0, total=None, **kwargs):
    weights = pdutils.coalesce(weights, {})
    total = total if not pdutils.is_empty(total) else (
        sum(weights.values()) if weights else None)
    if not weights and not default_weight:
        yield from items
    else:
        with tqdm.tqdm(items, total=total, **kwargs) as tq:
            for item in items:
                yield item
                weight = weights.get(item, default_weight)
                tq.update(weight)
Example #4
0
 def label_column(self, name=None, gtype=None, label_kwargs=None):
     # Create a set of candidate columns that match the search criteria.
     candidates = list(self.label_columns)
     if name is not None:
         candidates = [x for x in candidates if x.name == name]
     if gtype is not None:
         candidates = [x for x in candidates if x.gtype == gtype]
     if label_kwargs is not None:
         candidates = [
             x for x in candidates if x.labeler().kwargs == label_kwargs
         ]
     # Assert that we found exactly 1 candidate, and return it.
     if len(candidates) == 0:
         target = {
             k: v
             for k, v in [('name',
                           name), ('gtype',
                                   gtype), ('label_kwargs', label_kwargs)]
             if not pdutils.is_empty(v)
         }
         raise lib_errors.NoLabelColumnsError(
             '0 columns found on %r for %r' % (
                 str(self.endpointer),
                 target,
             ))
     if len(candidates) > 1:
         target = {
             k: v
             for k, v in [('name',
                           name), ('gtype',
                                   gtype), ('label_kwargs', label_kwargs)]
             if not pdutils.is_empty(v)
         }
         raise lib_errors.MultipleLabelColumnsError(
             '%d columns found on %r for %r: %r' %
             (str(self.endpointer), target, len(candidates), candidates))
     return more_itertools.one(candidates)
Example #5
0
 def _cell(self, position, col_index, val, width):
     align = pd_align(self._df.iloc[:, col_index].dtype)
     is_hilighted = (self.focus == position)
     if pdutils.is_empty(val):
         widget = urwid.AttrMap(
             SelectableText(str(val), wrap='clip', align=align),
             'row-item-nil-hilighted' if is_hilighted else 'row-item-nil',
             'row-item-nil-selected')
     else:
         widget = urwid.AttrMap(
             SelectableText(str(val).replace('\n',
                                             '\\n').replace('\r', '\\r'),
                            wrap='clip',
                            align=align), None, 'row-item-selected')
     return (width, widget)
Example #6
0
 def how_name(self, how):
   """Returns the value used by How.name for any How instance that points to this target."""
   kw = ((k, v) for k, v in how.coax_kwargs.items() if not pdutils.is_empty(v))
   kw_str = ','.join('%s=%r' % (k, v) for k, v in kw)
   kw_str = '(%s)' % (kw_str,) if kw_str else ''
   return '%s%s|%s' % (how.via_column, kw_str, self.name)
Example #7
0
 def _coax_scalar(cls, obj):
   if pdutils.is_empty(obj):
     return None
   if not isinstance(obj, float):
     obj = float(obj)
   return obj if cls.is_valid(obj) else None
Example #8
0
 def _label_scalar_nocontext(cls, bucket):
   if pdutils.is_empty(bucket):
     return None
   return cls.LABEL_FMT.format(decimal.Decimal(bucket))
Example #9
0
 def name(self):
   if self._name is None:
     kw = ((k, v) for k, v in self._kwargs.items() if not pdutils.is_empty(v))
     kw_str = ', '.join('%s=%r' % (k, v) for k, v in kw)
     self._name = '%s(%s)' % (self._gtype.qualname, kw_str)
   return self._name
Example #10
0
 def __repr__(self):
   kw = ((k, v) for k, v in self._kwargs.items() if not pdutils.is_empty(v))
   kw_str = ', '.join('%s=%r' % (k, v) for k, v in kw)
   return ('<%s %s(%s)>' % (self.__class__.__name__, self._gtype.qualname, kw_str))