Ejemplo n.º 1
0
def test_parse_isodate_error_text():
    with pytest.raises(ValueError) as excinfo:
        isoparser().parse_isodate('2014-0423')

    # ensure the error message does not contain b' prefixes
    if six.PY2:
        expected_error = "String contains unknown ISO components: u'2014-0423'"
    else:
        expected_error = "String contains unknown ISO components: '2014-0423'"
    assert expected_error == str(excinfo.value)
Ejemplo n.º 2
0
def search_yt_title_artist(track_name: str, artist_name: str) -> dict:
    search_query = track_name + " " + artist_name

    api_key = getenv("GOOGLE_APPLICATION_CREDENTIALS")
    api_client = googleapiclient.discovery.build("youtube",
                                                 "v3",
                                                 developerKey=api_key,
                                                 cache_discovery=False)

    request = api_client.search().list(part=["id"], q=search_query)
    response = request.execute()
    # first 5 items
    items = response['items']
    for item in items:
        kind_id = item['id']['kind']
        if kind_id == 'youtube#video':
            video_id = item['id']['videoId']
            video_request = api_client.videos().list(
                part=["snippet", "statistics", "status"], id=video_id)
            video_response = video_request.execute()
            video = video_response['items'][0]
            return {
                "youtube": {
                    "title": video["snippet"]["title"],
                    "views": video["statistics"]["viewCount"],
                    "posted_date": isoparser(video["snippet"]["publishedAt"]),
                    "converted_link":
                    f"https://www.youtube.com/watch?v={video_id}",
                    "embeddable": video["status"]["embeddable"]
                }
            }
Ejemplo n.º 3
0
def test_isoparser_byte_sep():  #pragma: no cover
    dt = datetime(2017, 12, 6, 12, 30, 45)  #pragma: no cover
    dt_str = dt.isoformat(sep=str('T'))  #pragma: no cover

    dt_rt = isoparser(sep=b'T').isoparse(dt_str)  #pragma: no cover

    assert dt == dt_rt  #pragma: no cover
Ejemplo n.º 4
0
def test_isoparser_byte_sep():
    dt = datetime(2017, 12, 6, 12, 30, 45)
    dt_str = dt.isoformat(sep=str('T'))

    dt_rt = isoparser(sep=b'T').isoparse(dt_str)

    assert dt == dt_rt
Ejemplo n.º 5
0
def test_isoparser_byte_sep():
    dt = datetime(2017, 12, 6, 12, 30, 45)
    dt_str = dt.isoformat(sep=str('T'))

    dt_rt = isoparser(sep=b'T').isoparse(dt_str)

    assert dt == dt_rt
Ejemplo n.º 6
0
 def get_extra_attrs(self) -> dict:
     return {
         "youtube": {
             "title": self.api_data["snippet"]["title"],
             "views": self.api_data["statistics"]["viewCount"],
             "posted_date": isoparser(self.api_data["snippet"]["publishedAt"]),
         }
     }
Ejemplo n.º 7
0
def test_parse_isodate(d, dt_fmt, as_bytes):
    d_str = d.strftime(dt_fmt)
    if isinstance(d_str, six.text_type) and as_bytes:
        d_str = d_str.encode('ascii')
    elif isinstance(d_str, six.binary_type) and not as_bytes:
        d_str = d_str.decode('ascii')

    iparser = isoparser()
    assert iparser.parse_isodate(d_str) == d
Ejemplo n.º 8
0
def test_parse_isodate(d, dt_fmt, as_bytes):
    d_str = d.strftime(dt_fmt)
    if isinstance(d_str, six.text_type) and as_bytes:
        d_str = d_str.encode('ascii')
    elif isinstance(d_str, bytes) and not as_bytes:
        d_str = d_str.decode('ascii')

    iparser = isoparser()
    assert iparser.parse_isodate(d_str) == d
Ejemplo n.º 9
0
def test_isotime(time_val, time_fmt, as_bytes):
    tstr = time_val.strftime(time_fmt)
    if isinstance(tstr, six.text_type) and as_bytes:
        tstr = tstr.encode('ascii')
    elif isinstance(tstr, bytes) and not as_bytes:
        tstr = tstr.decode('ascii')

    iparser = isoparser()

    assert iparser.parse_isotime(tstr) == time_val
Ejemplo n.º 10
0
def test_isotime(time_val, time_fmt, as_bytes):
    tstr = time_val.strftime(time_fmt)
    if isinstance(time_val, six.text_type) and as_bytes:
        tstr = tstr.encode('ascii')
    elif isinstance(time_val, six.binary_type) and not as_bytes:
        tstr = tstr.decode('ascii')

    iparser = isoparser()

    assert iparser.parse_isotime(tstr) == time_val
Ejemplo n.º 11
0
 def validate(self, val):
     super().validate(val)
     if isinstance(val, six.string_types):
         try:
             val = parser.isoparser().parse_isotime(val)
         except Exception:
             val = parser.parse(val)
     elif isinstance(val, self.type):
         pass
     else:
         raise ValidationError(u'{}: {} is not string type or {} type'.format(self.name or 'value', val, self.type))
     return val
Ejemplo n.º 12
0
def dateobs2night(dateobs):
    """
    Convert DATE-OBS ISO8601 UTC string to YEARMMDD int night of KPNO sunset
    """
    # use astropy to flexibily handle multiple valid ISO8601 variants
    from astropy.time import Time
    try:
        mjd = Time(dateobs).mjd
    except ValueError:
        #- only use optional dependency dateutil if needed;
        #- it can handle some ISO8601 timezone variants that astropy can't
        from dateutil.parser import isoparser
        mjd = Time(isoparser().isoparse(dateobs))

    return mjd2night(mjd)
Ejemplo n.º 13
0
def test_parse_isodate(d, dt_fmt, as_bytes):
    if six.PY2:
        #strftime returns str in python2
        d_str_s = unicode(d.strftime(dt_fmt))
    else:
        d_str_s = d.strftime(dt_fmt)

    d_str_b = d.strftime(dt_fmt).encode('ascii')  #encode to bytes to test

    for d_str in d_str_s, d_str_b:
        if isinstance(d_str, six.text_type) and as_bytes:
            d_str = d_str.encode('ascii')
        elif isinstance(d_str, bytes) and not as_bytes:
            d_str = d_str.decode('ascii')

    iparser = isoparser()
    assert iparser.parse_isodate(d_str) == d
Ejemplo n.º 14
0
def test_isotime(time_val, time_fmt, as_bytes):
    if six.PY2:
        tstr_s = unicode(time_val.strftime(time_fmt))
    else:
        tstr_s = time_val.strftime(time_fmt)

    tstr_b = time_val.strftime(time_fmt).encode('ascii')

    for tstr in tstr_s, tstr_b:
        if isinstance(tstr, six.text_type) and as_bytes:
            tstr = tstr.encode('ascii')
        elif isinstance(tstr, bytes) and not as_bytes:
            tstr = tstr.decode('ascii')

    iparser = isoparser()

    assert iparser.parse_isotime(tstr) == time_val
Ejemplo n.º 15
0
    def decode_datetime(self, value: str):
        """Returns an appropriate Python datetime time, date, or datetime
        object by using the 3rd party dateutil library (if present)
        to parse an ISO 8601 datetime string in *value*.  If it cannot,
        or the dateutil library is not present, it will raise a
        ValueError.
        """

        try:
            return super().decode_datetime(value)
        except ValueError:
            try:
                from dateutil.parser import isoparser

                isop = isoparser()

                if len(value) > 3 and value[-2] == "+" and value[-1].isdigit():
                    # This technically means that we accept slightly more
                    # formats than ISO 8601 strings, since under that
                    # specification, two digits after the '+' are required
                    # for an hour offset, but ODL doesn't have this
                    # requirement.  If we find only one digit, we'll
                    # just assume it means an hour and insert a zero so
                    # that it can be parsed.
                    tokens = value.rpartition("+")
                    value = tokens[0] + "+0" + tokens[-1]

                try:
                    return isop.parse_isodate(value)
                except ValueError:
                    try:
                        return isop.parse_isotime(value)
                    except ValueError:
                        return isop.isoparse(value)

            except ImportError:
                warn(
                    "The dateutil library is not present, so more "
                    "exotic date and time formats beyond the PVL/ODL "
                    "set cannot be parsed.",
                    ImportWarning,
                )

            raise ValueError
Ejemplo n.º 16
0
def timezone_info_for_timezone(tzstr: str) -> datetime.tzinfo:
    """Returns a datetime.tzinfo subclass for a timezone string."""
    offset_err = None
    tzname_err = None

    try:
        isoparser = parser.isoparser()
        return isoparser.parse_tzstr(tzstr)
    except ValueError as e:
        offset_err = e  # Fall through

    try:
        return cast(datetime.tzinfo, zoneinfo.ZoneInfo(tzstr))
    except ValueError as e:
        tzname_err = e  # Fall through

    raise ValueError(
        f'Cannot serialize timezone: {tzstr!r} ({offset_err!r}, {tzname_err!r}).'
    )
Ejemplo n.º 17
0
    def coordVars(self):
        """
        Identify coordinate variables and type.  Avoid dependence on udunits for now.
        """
        coords = dict(lon=None,lat=None,time=None,lev=None, tunits=None, toff = None)
        Vars = self.variables.keys()
        for d in self.dimensions.keys(): 
            if d in Vars:          # potential coordinate variable
               try:
                   units = self.variables[d].units.lower()
                   if   'degrees_north' in units: coords['lat'] = d
                   elif 'degrees_south' in units: coords['lat'] = d
                   elif 'degrees_east'  in units: coords['lon'] = d
                   elif 'degrees_west'  in units: coords['lon'] = d
                   elif 'pa'            in units: coords['lev'] = d
                   elif 'mb'            in units: coords['lev'] = d
                   elif 'meter'         in units: coords['lev'] = d
                   elif 'level'         in units: coords['lev'] = d
                   elif 'sigma_level'   in units: coords['lev'] = d
                   elif 'seconds'       in units: coords['time'] = d
                   elif 'minutes'       in units: coords['time'] = d
                   elif 'hours'         in units: coords['time'] = d
                   elif 'days'          in units: coords['time'] = d
               except:
                   pass

        if coords['time'] != None:
            try:
                isoT = self.variables['time'].units.split('since ')[1]
                tunt = self.variables['time'].units.split(' since')[0].lower()
                if ':' not in isoT: isoT = isoT+':0:0'
                coords['toff'] = isoparser(isoT)
                if tunt == 'seconds': tunits = 1.
                elif tunt == 'minutes': tunits = 60.
                elif tunt == 'hours': tunits = 60.**2
                elif tunt == 'days': tunits = 24 * 60.**2
                coords['tunits'] = tunits
            except:
                self.variables['time'] = None
               
        return coords
Ejemplo n.º 18
0
class TimeType(BaseType):
    _TYPE = datetime.time
    _DEFAULT = parser.isoparser().parse_isotime('08:00:00')
    _FORMAT = '%H:%M:%S'

    def __init__(self, *args, **kwargs):
        super(TimeType, self).__init__(*args, **kwargs)
        self.format = kwargs.get('format', self._FORMAT)

    @field_exception
    def validate(self, val):
        super().validate(val)
        if isinstance(val, six.string_types):
            try:
                val = parser.isoparser().parse_isotime(val)
            except Exception:
                val = parser.parse(val)
        elif isinstance(val, self.type):
            pass
        else:
            raise ValidationError(u'{}: {} is not string type or {} type'.format(self.name or 'value', val, self.type))
        return val
Ejemplo n.º 19
0
    def __init__(self, filename, Extra=None):
        """
        Read select attributes from an ODS file.
        """

        nc = Dataset(filename)

        Vars = VARS
        if Extra is not None:
            Vars += Extra

        self._getVars(nc, Vars)

        self.nobs = len(self.lon)

        # Create python time
        # ------------------
        tunits = nc.variables['time'].units
        onemin = timedelta(minutes=1)
        toff = isoparser(tunits.split(' since ')[1].replace(' ', 'T'))
        t = array([toff + self.time[i] * onemin for i in range(self.nobs)])

        self.time = t  # array of python datetime objects
Ejemplo n.º 20
0
def test_iso_with_sep_raises(sep_act, valid_sep, exception):
    parser = isoparser(sep=valid_sep)
    isostr = '2012-04-25' + sep_act + '01:25:00'
    with pytest.raises(exception):
        parser.isoparse(isostr)
Ejemplo n.º 21
0
def test_parse_tzstr_fails(tzstr, exception):
    with pytest.raises(exception):
        isoparser().parse_tzstr(tzstr)
Ejemplo n.º 22
0
def test_parse_tzstr_zero_as_utc(tzstr, zero_as_utc):
    tzi = isoparser().parse_tzstr(tzstr, zero_as_utc=zero_as_utc)
    assert tzi == UTC
    assert (type(tzi) == tz.tzutc) == zero_as_utc
Ejemplo n.º 23
0
def test_isodate_raises(isostr, exception):
    with pytest.raises(exception):  #pragma: no cover
        isoparser().parse_isodate(isostr)
Ejemplo n.º 24
0
def test_isoparser_invalid_sep(sep):
    with pytest.raises(ValueError):
        isoparser(sep=sep)
Ejemplo n.º 25
0
def test_iso_with_sep_raises(sep_act, valid_sep, exception):
    parser = isoparser(sep=valid_sep)
    isostr = '2012-04-25' + sep_act + '01:25:00'
    with pytest.raises(exception):
        parser.isoparse(isostr)
Ejemplo n.º 26
0
    return typ[value]


def pass_faux_enum(value, typ):
    typ[value]
    return value


if hasattr(datetime, "fromisoformat"):
    convert_date = date.fromisoformat
    convert_datetime = datetime.fromisoformat
    convert_time = time.fromisoformat
else:
    from dateutil.parser import isoparser

    instance = isoparser(sep="T")
    convert_date = instance.parse_isodate
    convert_datetime = instance.isoparse
    convert_time = instance.parse_isotime
    del instance


def convert_timedelta_str(dur):
    "Barebones support for storing a timedelta as an ISO8601 duration."
    micro = ".{:06d}".format(dur.microseconds) if dur.microseconds else ""
    return "P{:d}DT{:d}{}S".format(dur.days, dur.seconds, micro)


_iso8601_duration = re.compile(
    r"^P(?!$)([-+]?\d+(?:[.,]\d+)?Y)?"
    r"([-+]?\d+(?:[.,]\d+)?M)?"
Ejemplo n.º 27
0
def test_isoparser_invalid_sep(sep):
    with pytest.raises(ValueError):
        isoparser(sep=sep)
Ejemplo n.º 28
0
def test_isotime_raises_xfail(isostr, exception):
    iparser = isoparser()
    with pytest.raises(exception):
        iparser.parse_isotime(isostr)
Ejemplo n.º 29
0
def test_isotime_midnight(isostr):
    iparser = isoparser()
    assert iparser.parse_isotime(isostr) == time(0, 0, 0, 0)
Ejemplo n.º 30
0
def test_isodate_raises(isostr, exception):
    with pytest.raises(exception):
        isoparser().parse_isodate(isostr)
Ejemplo n.º 31
0
def test_parse_tzstr_fails(tzstr, exception):
    with pytest.raises(exception):
        isoparser().parse_tzstr(tzstr)
Ejemplo n.º 32
0
def test_isotime_raises_xfail(isostr, exception):
    iparser = isoparser()  #pragma: no cover
    with pytest.raises(exception):  #pragma: no cover
        iparser.parse_isotime(isostr)  #pragma: no cover
Ejemplo n.º 33
0
def test_isodate_raises(isostr, exception):
    with pytest.raises(exception):
        isoparser().parse_isodate(isostr)
Ejemplo n.º 34
0
def test_isotime_midnight(isostr):
    iparser = isoparser()
    assert iparser.parse_isotime(isostr) == time(0, 0, 0, 0)
Ejemplo n.º 35
0
def test_isotime_raises_xfail(isostr, exception):
    iparser = isoparser()
    with pytest.raises(exception):
        iparser.parse_isotime(isostr)
Ejemplo n.º 36
0
def test_parse_tzstr_zero_as_utc(tzstr, zero_as_utc):
    tzi = isoparser().parse_tzstr(tzstr, zero_as_utc=zero_as_utc)
    assert tzi == tz.tzutc()
    assert (type(tzi) == tz.tzutc) == zero_as_utc