Exemplo n.º 1
0
    def _parse_time_slice_str(slc_str):
        r"""
        Args:
            slc_str (str): must be a single string containing a single
                time slice

        Returns:
            one of {int, string, or slice (can contain ints,
            floats, or strings)}

        Note:
            Individual elements of the slice can look like an int,
            float with trailing 'f', or they can have the form
            [A-Z]+[\d:]+\.\d*. This last one is a datetime-like
            representation with some preceding letters. The preceding
            letters are
        """
        # regex parse the sting into a list of datetime-like strings,
        # integers, floats, and bare colons that mark the slices
        # Note: for datetime-like strings, the letters preceeding a datetime
        # are necessary, otherwise 02:20:30.01 would have more than one meaning
        rstr = (r"\s*(?:(?!:)[A-Z]+[-\d:T]+\.\d*|:|[-+]?[0-9]*\.?[0-9]+f?)\s*|"
                r"[-+]?[0-9]+")
        r = re.compile(rstr, re.I)

        all_times = r.findall(slc_str)
        if len(all_times) == 1 and all_times[0] != ":":
            return vutil.str_to_value(all_times[0])

        # fill in implied slice colons, then replace them with something
        # unique... like !!
        all_times += [':'] * (2 - all_times.count(':'))
        all_times = [s if s != ":" else "!!" for s in all_times]
        # this is kinda silly, but turn all times back into a string,
        # then split it again, this is the easiest way to parse something
        # like '1::2'
        ret = "".join(all_times).split("!!")
        # convert empty -> None, ints -> ints and floats->floats
        for i, val in enumerate(ret):
            ret[i] = vutil.str_to_value(val)
        if len(ret) > 3:
            raise ValueError("Could not decipher slice: '{0}'. Perhaps you're "
                             "missing some letters in front of a time "
                             "string?".format(slc_str))
        # trim trailing dots
        ret = [r.rstrip('.') if hasattr(r, 'rstrip') else r for r in ret]
        return slice(*ret)
Exemplo n.º 2
0
    def _parse_time_slice_str(slc_str):
        r"""
        Args:
            slc_str (str): must be a single string containing a single
                time slice

        Returns:
            one of {int, string, or slice (can contain ints,
            floats, or strings)}

        Note:
            Individual elements of the slice can look like an int,
            float with trailing 'f', or they can have the form
            [A-Z]+[\d:]+\.\d*. This last one is a datetime-like
            representation with some preceding letters. The preceding
            letters are
        """
        # regex parse the sting into a list of datetime-like strings,
        # integers, floats, and bare colons that mark the slices
        # Note: for datetime-like strings, the letters preceeding a datetime
        # are necessary, otherwise 02:20:30.01 would have more than one meaning
        rstr = (r"\s*(?:(?!:)[A-Z]+[-\d:T]+\.\d*|:|[-+]?[0-9]*\.?[0-9]+f?)\s*|"
                r"[-+]?[0-9]+")
        r = re.compile(rstr, re.I)

        all_times = r.findall(slc_str)
        if len(all_times) == 1 and all_times[0] != ":":
            return vutil.str_to_value(all_times[0])

        # fill in implied slice colons, then replace them with something
        # unique... like !!
        all_times += [':'] * (2 - all_times.count(':'))
        all_times = [s if s != ":" else "!!" for s in all_times]
        # this is kinda silly, but turn all times back into a string,
        # then split it again, this is the easiest way to parse something
        # like '1::2'
        ret = "".join(all_times).split("!!")
        # convert empty -> None, ints -> ints and floats->floats
        for i, val in enumerate(ret):
            ret[i] = vutil.str_to_value(val)
        if len(ret) > 3:
            raise ValueError("Could not decipher slice: '{0}'. Perhaps you're "
                             "missing some letters in front of a time "
                             "string?".format(slc_str))
        # trim trailing dots
        ret = [r.rstrip('.') if hasattr(r, 'rstrip') else r for r in ret]
        return slice(*ret)
Exemplo n.º 3
0
    def as_floating_t(self, t, none_passthrough=False):
        t_as_s = None
        try:
            t = vutil.str_to_value(t)

            if viscid.is_timedelta_like(t, conservative=True):
                t_as_s = viscid.as_timedelta(t).total_seconds()
            elif viscid.is_datetime_like(t, conservative=True):
                delta_t = viscid.as_datetime64(t) - self.basetime
                t_as_s = viscid.as_timedelta(delta_t).total_seconds()
            elif not isinstance(t, (int, np.integer, type(None))):
                t_as_s = float(t)
        except AttributeError:
            if t is None:
                if none_passthrough:
                    pass
                else:
                    t = 0.0
            else:
                t_as_s = float(t)

        return t_as_s
Exemplo n.º 4
0
    def as_floating_t(self, t, none_passthrough=False):
        t_as_s = None
        try:
            t = vutil.str_to_value(t)

            if viscid.is_timedelta_like(t, conservative=True):
                t_as_s = viscid.as_timedelta(t).total_seconds()
            elif viscid.is_datetime_like(t, conservative=True):
                delta_t = viscid.as_datetime64(t) - self.basetime
                t_as_s = viscid.as_timedelta(delta_t).total_seconds()
            elif not isinstance(t, (int, np.integer, type(None))):
                t_as_s = float(t)
        except AttributeError:
            if t is None:
                if none_passthrough:
                    pass
                else:
                    t = 0.0
            else:
                t_as_s = float(t)

        return t_as_s