Esempio n. 1
0
 def build_sampling_rate(min_freq, max_freq):
     min_sampling_rate = isodate.duration_isoformat(timedelta(seconds=min_freq))
     max_sampling_rate = isodate.duration_isoformat(timedelta(seconds=max_freq))
     sampling_rate = oadr_20b.oadrSamplingRateType(oadrMinPeriod=min_sampling_rate,
                                                   oadrMaxPeriod=max_sampling_rate,
                                                   oadrOnChange=False)
     return sampling_rate
Esempio n. 2
0
 def test_format(self):
     """
     Take duration/timedelta object and create ISO string from it.
     This is the reverse test to test_parse.
     """
     if altstr:
         self.assertEqual(duration_isoformat(expectation, format), altstr)
     else:
         # if durationstring == '-P2W':
         #     import pdb; pdb.set_trace()
         self.assertEqual(duration_isoformat(expectation, format), durationstring)
Esempio n. 3
0
 def to_raw(self, value, context=None):
     if isinstance(value, datetime.timedelta):
         return isodate.duration_isoformat(value)
     if isinstance(value, (int, float)):
         return isodate.duration_isoformat(
             datetime.timedelta(seconds=value)
         )
     if isinstance(value, six.string_types):
         self.to_python(value)
         return value
     raise exceptions.ValidationError(
         "Invalid duration value '%s' and type %s" % (value, type(value)),
         value=value,
         invalid='type',
         against='timedelta'
     )
    def _format_value(self, value):

        if isinstance(value, relativedelta):
            duration = utils.convert_relativedelta_to_duration(value)
            value = duration_isoformat(duration)

        return value
Esempio n. 5
0
    def default(self, o):
        if type(o) == decimal.Decimal:
            return float(o)
        if type(o) == datetime.date or type(o) == datetime.datetime:
            return o.isoformat()
        if type(o) == datetime.timedelta:
            return isodate.duration_isoformat(o)
        if hasattr(o, "to_dict") and callable(getattr(o, "to_dict")):
            return o.to_dict()
        else:
            array = None
            try:
                # If it is an iterator, then try to construct array and limit number of objects
                iterator = iter(o)
                count = self.iterator_limit
                array = []
                for i, obj in enumerate(iterator):
                    array.append(obj)
                    if i >= count:
                        break
            except TypeError as e:
                # not iterable
                pass

            if array is not None:
                return array
            else:
                return json.JSONEncoder.default(self, o)
Esempio n. 6
0
 def build_metadata_oadr_report(self, report):
     descriptions = []
     for tel_vals in json.loads(report.telemetry_parameters).values():
         # Rule 305: For TELEMETRY_USAGE reports, units in reportDescription.itemBase should be powerReal.
         if tel_vals['units'] == 'powerReal':
             item_base = oadr_20b.PowerRealType(itemDescription='RealPower',
                                                itemUnits='W',
                                                siScaleCode=None,
                                                powerAttributes=None)
         else:
             item_base = None
         min_freq, max_freq = tel_vals['min_frequency'], tel_vals['max_frequency']
         desc = oadr_20b.oadrReportDescriptionType(rID=tel_vals['r_id'],
                                                   reportType=tel_vals['report_type'],
                                                   readingType=tel_vals['reading_type'],
                                                   itemBase=item_base,
                                                   oadrSamplingRate=self.build_sampling_rate(min_freq, max_freq))
         descriptions.append(desc)
     rpt_interval_duration = isodate.duration_isoformat(timedelta(seconds=report.interval_secs))
     return oadr_20b.oadrReportType(duration=oadr_20b.DurationPropType(rpt_interval_duration),
                                    oadrReportDescription=descriptions,
                                    reportRequestID=None,
                                    reportSpecifierID=report.report_specifier_id,
                                    reportName=report.name,
                                    createdDateTime=utils.get_aware_utc_now())
Esempio n. 7
0
def await_resources(ec2, resource_function, resource_type, state_field, timeout, delay):
    log = ec2_log
    duration = None
    end = None
    states = {}

    if timeout:
        duration = isodate.parse_duration(timeout)
        end = datetime.now() + duration

    while True:
        states = {}
        resources = resource_function()
        for resource in resources:
            states.setdefault(getattr(resource, state_field), []).append(resource)

        # TODO: this should allow arbitrary resources via respective target state(s).
        if not "pending" in states:
            log.info("... {0} transitioned ({1})".format(resource_type, format_states(states)))
            break

        if end and datetime.now() > end:
            message = "FAILED to transition all {0} after {2} ({1})!".format(resource_type, format_states(states),
                                                                             isodate.duration_isoformat(duration))
            log.info("... " + message)
            raise bc.BotocrossAwaitTimeoutError(message)

        log.info("... {0} still transitioning ({1}) ...".format(resource_type, format_states(states)))
        time.sleep(delay)

    return states
Esempio n. 8
0
 def set_duration(self, duration=None, **kwargs):
     from datetime import timedelta
     from isodate import duration_isoformat
     duration = duration or timedelta(**kwargs)
     
     self._stmnt['result'] = self._stmnt.get('result', {})
     self._stmnt['result']["duration"] = duration_isoformat(duration)
Esempio n. 9
0
    def send_control_message(self, controller_queue, time_consumed=None, process_name=None, destination_endpoint=None,
                             destination_uuid=None, sender=None):
        """
        Control messages are notifications that a new message have been created, so the controller can keep track of
        this particular message and let the ramp know once the entire tree of messages has been completed.

        This is called implicitly on yield Message(_id, 'message')

        :param process_name: UUID of the process processing this message (as string)
        """
        content = {
            'process_name': process_name,
            'msg_type': 'new_msg',
        }
        if not self.producer_uuid:
            raise Exception("Cannot send control message without producer UUID")
        if time_consumed:
            # Ramps provide time consumed, since we don't know the "start time" like in a intersection
            # where it's clear when the message is received and later 'acked' as the last action
            content['duration'] = duration_isoformat(time_consumed)
            content['sender'] = sender
        controller_queue.send_json({
            'ramp_unique_id': self.ramp_unique_id,
            'ack_value': self.ack_value,
            'content': content,
            'producer_uuid': self.producer_uuid,
            'destination_endpoint': destination_endpoint,
            'destination_uuid': destination_uuid
        })
Esempio n. 10
0
    def serialize_duration(attr, **kwargs):
        """Serialize TimeDelta object into ISO-8601 formatted string.

        :param TimeDelta attr: Object to be serialized.
        :rtype: str
        """
        return isodate.duration_isoformat(attr)
Esempio n. 11
0
def dump_datetime(value):
    """Deserialize datetime object into string form for JSON processing."""
    if isinstance(value, datetime) or isinstance(value, date):
        return value.isoformat()
    elif isinstance(value, timedelta):
        return isodate.duration_isoformat(value)

    raise TypeError("type not serializable")
Esempio n. 12
0
 def test_format(self):
     """
     Test various other strftime combinations.
     """
     self.assertEqual(duration_isoformat(Duration(0)), "P0D")
     self.assertEqual(duration_isoformat(-Duration(0)), "P0D")
     self.assertEqual(duration_isoformat(Duration(seconds=10)), "PT10S")
     self.assertEqual(duration_isoformat(Duration(years=-1, months=-1)), "-P1Y1M")
     self.assertEqual(duration_isoformat(-Duration(years=1, months=1)), "-P1Y1M")
     self.assertEqual(duration_isoformat(-Duration(years=-1, months=-1)), "P1Y1M")
     self.assertEqual(duration_isoformat(-Duration(years=-1, months=-1)), "P1Y1M")
     dur = Duration(years=3, months=7, days=23, hours=5, minutes=25, milliseconds=330)
     self.assertEqual(duration_isoformat(dur), "P3Y7M23DT5H25M0.33S")
     self.assertEqual(duration_isoformat(-dur), "-P3Y7M23DT5H25M0.33S")
Esempio n. 13
0
 def __init__(self, brain):
     self.brain = brain
     self.microdata_vocabulary = 'http://schema.org/Event'
     # must the event implements the event_url as "url"?
     self.url = brain.getRemoteUrl or brain.getURL()
     self.startDate = brain.start.ISO8601()
     self.endDate = brain.end.ISO8601()
     self.duration = isodate.duration_isoformat(brain.end.asdatetime() - brain.start.asdatetime()) 
     self.location = brain.location
Esempio n. 14
0
 def build_report_interval(self, telemetry_values):
     """Build an Interval for a report timeframe that includes telemetry values gathered during that time."""
     interval_start = telemetry_values.start_time
     # duration_isoformat can yield fractional seconds, e.g. PT59.9S, resulting in a warning during XML creation.
     interval_duration = isodate.duration_isoformat(telemetry_values.get_duration())
     interval = oadr_20b.IntervalType(dtstart=oadr_20b.dtstart(date_time=interval_start),
                                      duration=oadr_20b.DurationPropType(duration=interval_duration))
     interval.set_streamPayloadBase(self.build_report_payload_list(telemetry_values))
     return interval
Esempio n. 15
0
 def __init__(self, content):
     super(EventMicrodataProvider, self).__init__(content)
     self.microdata_vocabulary = 'http://schema.org/Event'
     # must the event implements the event_url as "url"?
     self.url = content.event_url() or content.absolute_url()
     self.attendees = content.getAttendees()
     self.startDate = content.start().ISO8601()
     self.endDate = content.end().ISO8601()
     self.duration = isodate.duration_isoformat(content.end().asdatetime() - content.start().asdatetime()) 
     self.location = content.location
    def get_prep_value(self, value):
        # Value in DB should be null.
        if value is None:
            return None

        if not isinstance(value, isodate.duration.Duration):
            raise ValidationError(
                'Cannot convert objects that are not Durations.')

        return isodate.duration_isoformat(value)
Esempio n. 17
0
    def _get_properties(self):
        data = {}
        for k, v in self.__dict__.items():
            if k in self.writable_attrs:
                if k in self.timedelta_attrs and \
                        isinstance(v, datetime.timedelta):
                    v = duration_isoformat(v)
                data[self.to_camel_case(k)] = self._sanitize_property(v)

        return data
Esempio n. 18
0
def format_iso8601_duration(dur):
    """
    Format a timedelta instance as an iso8601 string.
    @type dur: datetime.timedelta instance
    @param dur: duration instance to format
    @rtype: str
    @return: iso8601 representation of the passed in timedelta instance
    """
    # adding a timedelta to a zero length Duration instance will effectively
    # convert the timedelta into a Duration, which is required by the api
    return isodate.duration_isoformat(dur)
Esempio n. 19
0
 def valid_value(self, value):
     "Check to see if the provided value is a valid choice"
     text_value = force_text(isodate.duration_isoformat(value))
     for k, v in self.choices:
         if isinstance(v, (list, tuple)):
             # This is an optgroup, so look inside the group for options
             for k2, v2 in v:
                 if value == k2 or text_value == force_text(k2):
                     return True
         else:
             if value == k or text_value == force_text(k):
                 return True
     return False
Esempio n. 20
0
 def valid_value(self, value):
     "Check to see if the provided value is a valid choice"
     text_value = force_text(isodate.duration_isoformat(value))
     for k, v in self.choices:
         if isinstance(v, (list, tuple)):
             # This is an optgroup, so look inside the group for options
             for k2, v2 in v:
                 if value == k2 or text_value == force_text(k2):
                     return True
         else:
             if value == k or text_value == force_text(k):
                 return True
     return False
Esempio n. 21
0
def from_timedelta(value):
    if isinstance(value, six.string_types):
        value = isodate.parse_duration(value)
    elif isinstance(value, datetime.timedelta):
        value = isodate.duration.Duration(days=value.days,
                                          seconds=value.seconds,
                                          microseconds=value.microseconds)

    if isinstance(value, isodate.duration.Duration):
        return isodate.duration_isoformat(value)
    else:
        raise ValueError('To create a proper string representation for a duration,'
                         ' either a isodate.duration.Duration or str has to be supplied!')
Esempio n. 22
0
 def test_format(self):
     '''
     Test various other strftime combinations.
     '''
     self.assertEqual(duration_isoformat(Duration(0)), 'P0D')
     self.assertEqual(duration_isoformat(-Duration(0)), 'P0D')
     self.assertEqual(duration_isoformat(Duration(seconds=10)), 'PT10S')
     self.assertEqual(duration_isoformat(Duration(years=-1, months=-1)),
                      '-P1Y1M')
     self.assertEqual(duration_isoformat(-Duration(years=1, months=1)),
                      '-P1Y1M')
     self.assertEqual(duration_isoformat(-Duration(years=-1, months=-1)),
                      'P1Y1M')
     self.assertEqual(duration_isoformat(-Duration(years=-1, months=-1)),
                      'P1Y1M')
     dur = Duration(years=3, months=7, days=23, hours=5, minutes=25,
                    milliseconds=330)
     self.assertEqual(duration_isoformat(dur), 'P3Y7M23DT5H25M0.33S')
     self.assertEqual(duration_isoformat(-dur), '-P3Y7M23DT5H25M0.33S')
Esempio n. 23
0
 def to_json(self) -> Dict:
     json_data = dict()
     json_data["server_name"] = self.server.name
     if isinstance(self.destination, Channel):
         json_data["channel_address"] = self.destination.address
     if isinstance(self.destination, User):
         json_data["user_address"] = self.destination.address
     json_data["period"] = isodate.duration_isoformat(self.period)
     if self.last_check is not None:
         json_data["last_check"] = self.last_check.isoformat()
     if self.last_update is not None:
         json_data["last_update"] = self.last_update.isoformat()
     json_data["source"] = self.source.to_json()
     return json_data
Esempio n. 24
0
def query_special_locations_timeseries(startdate, enddate, interval, parameters, username, password, model='mix',
                                       postal_codes=None, temporal_interpolation=None, spatial_interpolation=None,
                                       on_invalid=None,
                                       api_base_url=DEFAULT_API_BASE_URL, request_type='GET'):
    """Retrieve a time series from the Meteomatics Weather API.
    Requested locations can be soecified by Postal Codes; Input as dictionary, e.g.: postal_codes={'DE': [71679,70173], ...}.
    Start and End dates have to be in UTC.
    Returns a Pandas `DataFrame` with a `DateTimeIndex`.
    request_type is one of 'GET/POST'
    """

    # set time zone info to UTC if necessary
    startdate = sanitize_datetime(startdate)
    enddate = sanitize_datetime(enddate)

    # build URL
    coordinates = ""
    urlParams = {}
    urlParams['connector'] = VERSION
    if postal_codes is not None:
        for country, pcs in postal_codes.items():
            coordinates += "+".join(['postal_' + country.upper() + s for s in pcs])

    if model is not None:
        urlParams['model'] = model

    if on_invalid is not None:
        urlParams['on_invalid'] = on_invalid

    if temporal_interpolation is not None:
        urlParams['temporal_interpolation'] = temporal_interpolation

    if spatial_interpolation is not None:
        urlParams['spatial_interpolation'] = spatial_interpolation

    url = TIME_SERIES_TEMPLATE.format(
        api_base_url=api_base_url,
        coordinates=coordinates,
        startdate=startdate.isoformat(),
        enddate=enddate.isoformat(),
        interval=isodate.duration_isoformat(interval),
        parameters=",".join(parameters),
        urlParams="&".join(["{}={}".format(k, v) for k, v in urlParams.items()])
    )

    headers = {'Accept': 'text/csv'}
    response = query_api(url, username, password, request_type=request_type, headers=headers)

    coordinates_list = coordinates.split("+")
    return convert_time_series_binary_response_to_df(response.content, coordinates_list, parameters, station=True)
Esempio n. 25
0
def serialize_value_type(value):
    if isinstance(value, float):
        return "double", str(value)
    if isinstance(value, bool):  # Attention: bool is subclass of int. So put bool ahead of int
        return "boolean", str(value).lower()
    if isinstance(value, six.string_types):
        return "string", value
    if isinstance(value, six.integer_types):
        return "int" if value <= constants.INT32_MAX_VALUE else "long", str(value)
    if isinstance(value, datetime):
        return "dateTime", isodate.datetime_isoformat(value)
    if isinstance(value, timedelta):
        return "duration", isodate.duration_isoformat(value)
    raise ValueError("value {} of type {} is not supported for the key value".format(value, type(value)))
Esempio n. 26
0
    async def push_measurement_service(self, param=None):

        total_power = float(param["total_power"])
        period = parse_duration(param["period"])
        period_end = parse_datetime(param["period_end"])

        data = {
            "measurement": {
                "period_end": datetime_isoformat(period_end),
                "period": duration_isoformat(period),
                "total_power": total_power
            }
        }
        return await self._post_single_measurement(json.dumps(data))
Esempio n. 27
0
class JSONFormat(FileFormat):

    SERIALIZERS = {
        'datetime': lambda d: d.strftime(DATETIME_F_FORMAT),
        'date': lambda d: d.strftime(DATE_F_FORMAT),
        'time': lambda d: d.strftime(TIME_F_FORMAT),
        'number': float,
        'duration': lambda d: isodate.duration_isoformat(d),
        'geopoint': lambda d: list(map(float, d)),
        'yearmonth': lambda d: '{:04d}-{:02d}'.format(*d),
    }
    DEFAULT_SERIALIZER = identity
    NULL_VALUE = None

    PYTHON_DIALECT = {
        'date': {
            'format': DATE_P_FORMAT
        },
        'time': {
            'format': TIME_P_FORMAT
        },
        'datetime': {
            'format': DATETIME_P_FORMAT
        },
    }

    def prepare_resource(self, resource):
        resource['encoding'] = 'utf-8'
        basename, _ = os.path.splitext(get_path(resource))
        resource['path'] = basename + '.json'
        resource['format'] = 'json'
        super(JSONFormat, self).prepare_resource(resource)

    def initialize_file(self, file, headers):
        writer = file
        writer.write('[')
        writer.__first = True
        return writer

    def write_transformed_row(self, writer, transformed_row, fields):
        if not writer.__first:
            writer.write(',')
        else:
            writer.__first = False
        writer.write(
            json.dumps(transformed_row, sort_keys=True, ensure_ascii=True))

    def finalize_file(self, writer):
        writer.write(']')
Esempio n. 28
0
def convert_seconds_to_iso(seconds):
    """
    Convert seconds from integer to ISO format.

    Arguments:
        seconds (int): number of seconds

    Returns:
        str
    """
    if seconds is None:
        return None
    return duration_isoformat(timedelta(
        seconds=seconds
    ))
Esempio n. 29
0
 def ack(self, time_consumed=None):
     """
     Send a message to the controller that this message was properly processed
     """
     self.controller_queue.send_json({
         'ramp_unique_id': self.ramp_unique_id,
         'ack_value': self.ack_value,
         'content': {
             'process_name': self.process_name,
             'msg_type': 'ack',
             'duration': duration_isoformat(time_consumed or (datetime.datetime.now() - self.init_time))
         },
         'producer_uuid': self.producer_uuid,
         'destination_uuid': self.producer_uuid
     })
Esempio n. 30
0
    def default(self, obj):

        if isinstance(obj, decimal.Decimal):
            return {'type{decimal}': str(obj)}
        elif isinstance(obj, datetime.time):
            return {'type{time}': obj.strftime(TIME_FORMAT)}
        elif isinstance(obj, datetime.datetime):
            return {'type{datetime}': obj.strftime(DATETIME_FORMAT)}
        elif isinstance(obj, datetime.date):
            return {'type{date}': obj.strftime(DATE_FORMAT)}
        elif isinstance(obj, (isodate.Duration, datetime.timedelta)):
            return {'type{duration}': isodate.duration_isoformat(obj)}
        elif isinstance(obj, set):
            return {'type{set}': list(obj)}
        return super().default(obj)
Esempio n. 31
0
    async def clean_data(self, key: str,
                         val: typing.Any) -> typing.Tuple[str, str]:
        value_text = val
        clean_value = val

        # when setting a color
        if key in self.colors:
            hex_ = ALL_COLORS.get(val)

            if hex_ is None:
                if not isinstance(val, str):
                    raise InvalidConfigError('Invalid color name or hex.')
                if val.startswith('#'):
                    val = val[1:]
                if len(val) != 6:
                    raise InvalidConfigError('Invalid color name or hex.')
                for letter in val:
                    if letter not in {
                            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                            'a', 'b', 'c', 'd', 'e', 'f'
                    }:
                        raise InvalidConfigError('Invalid color name or hex.')
                clean_value = '#' + val
                value_text = clean_value
            else:
                clean_value = hex_
                value_text = f'{val} ({clean_value})'

        elif key in self.time_deltas:
            try:
                isodate.parse_duration(val)
            except isodate.ISO8601Error:
                try:
                    converter = UserFriendlyTime()
                    time = await converter.convert(None, val)
                    if time.arg:
                        raise ValueError
                except BadArgument as exc:
                    raise InvalidConfigError(*exc.args)
                except Exception:
                    raise InvalidConfigError(
                        'Unrecognized time, please use ISO-8601 duration format '
                        'string or a simpler "human readable" time.')
                clean_value = isodate.duration_isoformat(time.dt -
                                                         converter.now)
                value_text = f'{val} ({clean_value})'

        return clean_value, value_text
Esempio n. 32
0
 def fail(self, error_message="", capture_exception=True):
     """
     Send a message to the controller that this message failed to process
     """
     self.controller_queue.send_json({
         'ramp_unique_id': self.ramp_unique_id,
         'ack_value': -1,
         'content': {
             'process_name': self.process_name,
             'msg_type': 'fail',
             'duration': duration_isoformat(datetime.datetime.now() - self.init_time)
         },
         'producer_uuid': self.producer_uuid,
         'destination_uuid': self.producer_uuid,
         'error_message': error_message if not capture_exception else traceback.format_exc()
     })
Esempio n. 33
0
    def _new_queue(self, queue: str, **kwargs) -> SendReceive:
        """Ensure a queue exists in ServiceBus."""
        queue = self.entity_name(self.queue_name_prefix + queue)

        try:
            return self._queue_cache[queue]
        except KeyError:
            # Converts seconds into ISO8601 duration format ie 66seconds = P1M6S
            lock_duration = isodate.duration_isoformat(
                isodate.Duration(seconds=self.peek_lock_seconds))
            try:
                self.queue_mgmt_service.create_queue(
                    queue_name=queue, lock_duration=lock_duration)
            except azure.core.exceptions.ResourceExistsError:
                pass
            return self._add_queue_to_cache(queue)
Esempio n. 34
0
def make_json_serializable(o: dict) -> dict:
    """
    Turns `o` into a JSON-serializable dict, doing the following conversions:

    - Decimal -> float
    """
    json_serializable = {}
    for key, value in o.items():
        if isinstance(value, Decimal):
            value = float(value)
        elif isinstance(value, timedelta):
            value = duration_isoformat(value)
        elif isinstance(value, dict):
            value = make_json_serializable(value)
        json_serializable[key] = value
    return json_serializable
    def vtn_request_variable_event(self, event_id, start_time, duration_secs):
        """
            Push an oadrDistributeEvent VTN request in which the event's start_time and duration
            are adjustable parameters.

        @param event_id: (String) The event's ID.
        @param start_time: (DateTime) The event's start_time.
        @param duration_secs: (Integer seconds) The event's duration.
        """
        self.vtn_request('EiEvent',
                         'test_vtn_distribute_event_variable',
                         event_id=event_id,
                         event_start_time=isodate.datetime_isoformat(start_time),
                         event_duration=isodate.duration_isoformat(timedelta(seconds=duration_secs)))
        # Sleep for an extra cycle to give the event time to change status to active, completed, etc.
        time.sleep(POLL_INTERVAL_SECS + 1)
Esempio n. 36
0
def message_for_post_meter_data(
    no_connection: bool = False,
    single_connection: bool = False,
    single_connection_group: bool = False,
    production: bool = False,
    different_target_resolutions: bool = False,
    tile_n=1,
) -> dict:
    sign = 1 if production is False else -1
    message = {
        "type":
        "PostMeterDataRequest",
        "groups": [
            {
                "connections": ["CS 1", "CS 2"],
                "values":
                (tile([306.66, 306.66, 0, 0, 306.66, 306.66], tile_n) *
                 sign).tolist(),
            },
            {
                "connection":
                ["CS 4" if different_target_resolutions else "CS 3"],
                "values": (tile([306.66, 0, 0, 0, 306.66, 306.66], tile_n) *
                           sign).tolist(),
            },
        ],
        "start":
        "2015-01-01T00:00:00Z",
        "duration":
        duration_isoformat(timedelta(hours=1.5 * tile_n)),
        "horizon":
        "PT0H",
        "unit":
        "MW",
    }
    if no_connection:
        message.pop("groups", None)
    elif single_connection:
        message["connection"] = message["groups"][0]["connections"][0]
        message["values"] = message["groups"][1]["values"]
        message.pop("groups", None)
    elif single_connection_group:
        message["connections"] = message["groups"][0]["connections"]
        message["values"] = message["groups"][0]["values"]
        message.pop("groups", None)

    return message
Esempio n. 37
0
    def __init__(self,
                 constellationType=None,
                 numberSatellites=1,
                 numberPlanes=None,
                 relativeSpacing=None,
                 satelliteInterval=None,
                 orbit=None,
                 satellites=None,
                 _id=None):
        """Initialize a constellation object.
        """
        self.constellationType = ConstellationType.get(constellationType)
        self.numberSatellites = numberSatellites
        # assign default value of 1 plane for delta constellations
        if numberPlanes is None and (
                self.constellationType is ConstellationType.DELTA_HOMOGENOUS
                or self.constellationType is
                ConstellationType.DELTA_HETEROGENEOUS):
            self.numberPlanes = 1
        else:
            self.numberPlanes = numberPlanes

        # assign default relative spacing value for delta constellations
        if relativeSpacing is None and isinstance(
                self.numberPlanes,
                Number) and (self.constellationType is
                             ConstellationType.DELTA_HOMOGENOUS
                             or self.constellationType is
                             ConstellationType.DELTA_HETEROGENEOUS):
            # assign relative spacing of 1 for multi-plane sun-synchronous constellations
            if isinstance(orbit, Orbit) and self.numberPlanes > 1 and (
                    orbit.orbitType is OrbitType.SUN_SYNCHRONOUS
                    or orbit.inclination is "SSO"):
                self.relativeSpacing = 1
            # otherwise assign relative spacing of 0
            else:
                self.relativeSpacing = 0
        else:
            self.relativeSpacing = relativeSpacing
        if isinstance(satelliteInterval, Number):
            self.satelliteInterval = isodate.duration_isoformat(
                datetime.timedelta(minutes=satelliteInterval))
        else:
            self.satelliteInterval = satelliteInterval
        self.orbit = orbit
        self.satellites = satellites
        super(Constellation, self).__init__(_id, "Constellation")
Esempio n. 38
0
def query_init_date(startdate,
                    enddate,
                    interval,
                    parameter,
                    username,
                    password,
                    model,
                    api_base_url=DEFAULT_API_BASE_URL):
    # set time zone info to UTC if necessary
    startdate = sanitize_datetime(startdate)
    enddate = sanitize_datetime(enddate)

    interval_string = "{}--{}:{}".format(startdate.isoformat(),
                                         enddate.isoformat(),
                                         isodate.duration_isoformat(interval))

    url = INIT_DATE_TEMPLATE.format(api_base_url=api_base_url,
                                    model=model,
                                    interval_string=interval_string,
                                    parameter=parameter)

    headers = {'Accept': 'text/csv'}
    response = query_api(url,
                         username,
                         password,
                         request_type='GET',
                         headers=headers)

    try:
        df = pd.read_csv(StringIO(response.text),
                         sep=";",
                         header=0,
                         encoding="utf-8",
                         index_col=0,
                         na_values=["0000-00-00T00:00:00Z"],
                         parse_dates=[0, 1])
    except:
        raise WeatherApiException(response.text)

    try:
        # mark index as UTC timezone
        df.index = df.index.tz_localize("UTC")
    except TypeError:
        pass

    return df
Esempio n. 39
0
    def vtn_request_variable_event(self, event_id, start_time, duration_secs):
        """
            Push an oadrDistributeEvent VTN request in which the event's start_time and duration
            are adjustable parameters.

        @param event_id: (String) The event's ID.
        @param start_time: (DateTime) The event's start_time.
        @param duration_secs: (Integer seconds) The event's duration.
        """
        import isodate                      # Import the library only if the test is not skipped
        self.vtn_request('EiEvent',
                         'test_vtn_distribute_event_variable',
                         event_id=event_id,
                         event_start_time=isodate.datetime_isoformat(start_time),
                         event_duration=isodate.duration_isoformat(timedelta(seconds=duration_secs)))
        # Sleep for an extra cycle to give the event time to change status to active, completed, etc.
        time.sleep(POLL_INTERVAL_SECS + 1)
Esempio n. 40
0
    def default(self, obj):
        """Tests the input object, obj, to encode as JSON."""
        if hasattr(obj, '__json__'):
            return getattr(obj, '__json__')()

        import datetime
        import isodate
        if isinstance(obj, datetime.datetime):
            return isodate.datetime_isoformat(obj)
        elif isinstance(obj, datetime.date):
            return isodate.date_isoformat(obj)
        elif isinstance(obj, datetime.time):
            return isodate.time_isoformat(obj)
        elif isinstance(obj, datetime.timedelta):
            return isodate.duration_isoformat(obj)

        return simplejson.JSONEncoder.default(self, obj)
Esempio n. 41
0
 def test_format(self):
     """
     Test various other strftime combinations.
     """
     self.assertEqual(duration_isoformat(Duration(0)), "P0D")
     self.assertEqual(duration_isoformat(-Duration(0)), "P0D")
     self.assertEqual(duration_isoformat(Duration(seconds=10)), "PT10S")
     self.assertEqual(duration_isoformat(Duration(years=-1, months=-1)),
                      "-P1Y1M")
     self.assertEqual(duration_isoformat(-Duration(years=1, months=1)),
                      "-P1Y1M")
     self.assertEqual(duration_isoformat(-Duration(years=-1, months=-1)),
                      "P1Y1M")
     self.assertEqual(duration_isoformat(-Duration(years=-1, months=-1)),
                      "P1Y1M")
     dur = Duration(years=3,
                    months=7,
                    days=23,
                    hours=5,
                    minutes=25,
                    milliseconds=330)
     self.assertEqual(duration_isoformat(dur), "P3Y7M23DT5H25M0.33S")
     self.assertEqual(duration_isoformat(-dur), "-P3Y7M23DT5H25M0.33S")
Esempio n. 42
0
    def test_iso_duration_type(self):
        type_duration = IsoDurationType()
        period_str = "P3Y6M4DT12H30M5S"
        duration_period = Duration(years=3,
                                   months=6,
                                   days=4,
                                   hours=12,
                                   minutes=30,
                                   seconds=5)
        res_to_native = type_duration.to_native(period_str)

        self.assertEqual(res_to_native.years, 3)
        self.assertEqual(res_to_native.months, 6)
        self.assertEqual(res_to_native.days, 4)
        self.assertEqual(res_to_native.seconds, 45005)
        self.assertEqual(res_to_native, duration_period)
        self.assertEqual(duration_isoformat(res_to_native), period_str)

        res_to_primitive = type_duration.to_primitive(duration_period)
        self.assertEqual(res_to_primitive, period_str)
        # Parse with errors
        result = type_duration.to_native(duration_period)
        self.assertEqual(result, duration_period)
        with self.assertRaises(Exception) as context:
            result = type_duration.to_native("Ptest")
        self.assertEqual(context.exception.messages, [
            "ISO 8601 time designator 'T' missing. Unable to parse datetime string 'test'"
        ])
        with self.assertRaises(Exception) as context:
            result = type_duration.to_native("P3Y6MW4DT12H30M5S")
        self.assertEqual(context.exception.messages,
                         ["Unrecognised ISO 8601 date format: '3Y6MW4D'"])
        with self.assertRaises(Exception) as context:
            result = type_duration.to_native(123123)
        self.assertEqual(
            context.exception.messages,
            ["Could not parse 123123. Should be ISO8601 Durations."])
        res_native1 = type_duration.to_native("P3Y6M4DT12H30M5S")
        res_native2 = type_duration.to_native("P2Y18M4DT12H30M5S")
        self.assertEqual(res_native1, res_native2)

        res_dur1 = type_duration.to_primitive(res_native1)
        res_dur2 = type_duration.to_primitive(res_native2)
        self.assertEqual("P3Y6M4DT12H30M5S", res_dur1)
        self.assertEqual("P2Y18M4DT12H30M5S", res_dur2)
Esempio n. 43
0
 def draw_editable_sequence_ui(self, sequence, process_type):
     task = Data.tasks[sequence[process_type]]
     row = self.layout.row(align=True)
     row.label(text=task["Identification"] or "XXX")
     row.label(text=task["Name"] or "Unnamed")
     row.label(text=sequence["SequenceType"] or "N/A")
     if sequence["TimeLag"]:
         row.operator("bim.unassign_lag_time", text="",
                      icon="X").sequence = sequence["id"]
         row.label(text=isodate.duration_isoformat(Data.lag_times[
             sequence["TimeLag"]]["LagValue"]))
     else:
         row.operator("bim.assign_lag_time", text="",
                      icon="ADD").sequence = sequence["id"]
         row.label(text="N/A")
     if self.props.active_sequence_id == sequence["id"]:
         if self.props.editing_sequence_type == "ATTRIBUTES":
             row.operator("bim.edit_sequence_attributes",
                          text="",
                          icon="CHECKMARK")
             row.operator("bim.disable_editing_sequence",
                          text="",
                          icon="CANCEL")
             self.draw_editable_sequence_attributes_ui()
         elif self.props.editing_sequence_type == "TIME_LAG":
             op = row.operator("bim.edit_sequence_time_lag",
                               text="",
                               icon="CHECKMARK")
             op.lag_time = sequence["TimeLag"]
             row.operator("bim.disable_editing_sequence",
                          text="",
                          icon="CANCEL")
             self.draw_editable_sequence_time_lag_ui()
     else:
         if sequence["TimeLag"]:
             op = row.operator("bim.enable_editing_sequence_time_lag",
                               text="",
                               icon="CON_LOCKTRACK")
             op.sequence = sequence["id"]
             op.lag_time = sequence["TimeLag"]
         op = row.operator("bim.enable_editing_sequence_attributes",
                           text="",
                           icon="GREASEPENCIL")
         op.sequence = sequence["id"]
Esempio n. 44
0
def update_heartbeat(app_name: str,
                     *,
                     status: Optional[str] = None,
                     expiry_period: Optional[timedelta] = None):
    try:
        if status is None and expiry_period is None:
            requests.get(app_url(app_name), timeout=5)
        else:
            post_data = {}
            if status is not None:
                post_data['status'] = status
            if expiry_period is not None:
                post_data['expiry'] = isodate.duration_isoformat(expiry_period)
            requests.post(app_url(app_name), json=post_data, timeout=5)
    except Exception as e:
        logger = logging.getLogger("heartbeat-status")
        logger.error(
            "Failed to update status on heartbeat server due to exception",
            exc_info=e)
Esempio n. 45
0
def get_isoduration(date_str):
    """convert the given date_str string into an iso 8601 duration"""
    iso_duration = None

    if not date_str:
        return None

    # first, is it already a valid isoduration?
    try:
        isodate.parse_duration(date_str)
        return date_str
    except isodate.ISO8601Error, e:
        # if not, try to parse it
        try:
            delta = timelib.strtodatetime(date_str) - timelib.strtodatetime("now")
            iso_duration = isodate.duration_isoformat(delta)
        except Exception, e:
            log.msg(e.message, level=log.WARNING)
            return None
Esempio n. 46
0
    def default(self, obj):

        if isinstance(obj, decimal.Decimal):
            return {'type{decimal}': str(obj)}
        elif isinstance(obj, datetime.time):
            return {'type{time}': obj.strftime(TIME_FORMAT)}
        elif isinstance(obj, datetime.datetime):
            return {
                'type{datetime}':
                (obj.strftime(DATETIME_FORMAT), obj.utcoffset().seconds
                 if obj.utcoffset() is not None else None, obj.tzname())
            }
        elif isinstance(obj, datetime.date):
            return {'type{date}': obj.strftime(DATE_FORMAT)}
        elif isinstance(obj, (isodate.Duration, datetime.timedelta)):
            return {'type{duration}': isodate.duration_isoformat(obj)}
        elif isinstance(obj, set):
            return {'type{set}': list(obj)}
        return super().default(obj)
Esempio n. 47
0
def process(
        obj,
        fallback_processor: Optional[
            Callable[..., Serializable]
        ]=_DEFAULT_FALLBACK_PROCESSOR,
) -> Serializable:
    if isinstance(obj, timedelta):
        return isodate.duration_isoformat(obj)

    if isinstance(obj, pendulum.Pendulum):
        return obj.isoformat()

    if fallback_processor is not None:
        return fallback_processor(obj)

    raise TypeError(
        f'Cannot process object of type {type(obj)}, no fallback_processor '
        f'provided'
    )
Esempio n. 48
0
    def __init__(self,
                 location,
                 pt_mode_filter=None,
                 line=None,
                 number_of_results=None,
                 time_window=None,
                 stop_event_type=None,
                 include_previous_calls=None,
                 include_onward_calls=None,
                 include_operating_days=None,
                 include_realtime_data=True):
        '''
        Request to get all stop events from a location. All parameters except location are optional. Chapter 10 in the documention.
        Params:

        location: LocationRef
        pt_mode_filter: PtMode (Enum)
        line: LineFilter
        number_of_results: int
        time_window: isodate.Duration|datetime.timedelta
        stop_event_type: StopEventType (Enum)
        include_previous_calls: boolean
        include_onward_calls: boolean
        include_operatiing_days: boolean
        include_realtime_data: boolean
        '''
        self.location = location
        self.pt_mode_filter = pt_mode_filter
        self.line = line
        self.number_of_results = number_of_results
        self.time_window = time_window
        self.stop_event_type = stop_event_type
        self.include_previous_calls = include_previous_calls
        self.include_onward_calls = include_onward_calls
        self.include_operatiing_days = include_operating_days
        self.include_realtime_data = include_realtime_data
        if self.pt_mode_filter != None:
            self.pt_mode_filter = self.pt_mode_filter.value
        if self.stop_event_type != None:
            self.stop_event_type = self.stop_event_type.value
        if self.time_window != None:
            self.time_window = isodate.duration_isoformat(self.time_window)
        super().__init__()
Esempio n. 49
0
 def to_json(self):
     recommended_period = self.recommended_period
     if recommended_period is not None:
         recommended_period = isodate.duration_isoformat(recommended_period)
     latest_done = self.latest_done
     if isinstance(latest_done, date):
         latest_done = isodate.date_isoformat(latest_done)
     next_date = self.get_next_date()
     if isinstance(next_date, date):
         next_date = isodate.date_isoformat(next_date)
     return {
         "id": self.id,
         "display_name": self.display_name,
         "category": self.category,
         "recommended_period": recommended_period,
         "latest_done": latest_done,
         "next_date": next_date,
         "is_overdue": self.is_overdue(),
     }
Esempio n. 50
0
    def _serialize_item(item, prefix=""):
        if type(item) == str:
            yield (prefix, item)
        elif isinstance(item, float) or isinstance(item, int):
            yield (prefix, str(item))
        elif isinstance(item, datetime.timedelta):
            yield (prefix, isodate.duration_isoformat(item))
        elif isinstance(item, datetime.datetime):
            yield (prefix, item.isoformat())
        else:
            sub_items = []
            if type(item) == dict:
                sub_items = item.items()
            elif (isinstance(item, xr.Dataset)
                  or isinstance(item, xr.DataArray)
                  or isinstance(item, ERA5DataSet)):
                sub_items = item.attrs.items()
            # can use isinstance because namedtuple is a kind of tuple and we
            # want to save the tuple keys in that case
            elif isinstance(item, list) or type(item) == tuple:
                sub_items = zip(range(len(item)), item)
            else:
                # collections.named_tuple has a `_asdict` method to turn it into a dictionary
                if hasattr(item, "_asdict"):
                    sub_items = item._asdict().items()
                else:
                    sub_items = filter(
                        lambda item: not item[0].startswith("_"),
                        vars(item).items())

            for (k, v) in sub_items:
                # skip None values
                if v is None:
                    continue

                if prefix != "":
                    label = f"{prefix}_{k}"
                else:
                    label = k

                for serialized_item in _serialize_item(v, prefix=label):
                    yield serialized_item
Esempio n. 51
0
def get_isoduration(date_str):
    """convert the given date_str string into an iso 8601 duration"""
    iso_duration = None

    if not date_str:
        return None

    #first, is it already a valid isoduration?
    try:
        isodate.parse_duration(date_str)
        return date_str
    except isodate.ISO8601Error, e:
        # if not, try to parse it
        try:
            delta = (timelib.strtodatetime(date_str) -
                     timelib.strtodatetime('now'))
            iso_duration = isodate.duration_isoformat(delta)
        except Exception, e:
            log.msg(e.message, level=log.WARNING)
            return None
Esempio n. 52
0
 def __init__(self, connection, project, work_package, activity, comment, spent_on, hours):
     super().__init__(connection)
     self.form = {
         "_links": {
             "project": {
                 "href": project.__dict__["_links"]["self"]["href"]
             },
             "activity": {
                 "href": f"/api/v3/time_entries/activities/{activity}"
             },
             "workPackage": {
                 "href": work_package.__dict__["_links"]["self"]["href"]
             }
         },
         "hours": isodate.duration_isoformat(hours),
         "comment": {
             "raw": comment
         },
         "spentOn": spent_on.strftime("%Y-%m-%d"),
     }
    def start_sandbox(self, blueprint_identifier, time_delta, sandbox_name):
        """
        :param str blueprint_identifier: name or id
        :param datetime.timedelta time_delta: the sandbox duration
        :param str sandbox_name:
        :return str: The sandbox id
        """
        url = 'http://{hostname}:{api_port}/api/v1/blueprints/{blueprint_identifier}/start' \
            .format(hostname=self.hostname, api_port=self.api_port, blueprint_identifier=blueprint_identifier)

        duration = isodate.duration_isoformat(time_delta)

        response = requests.post(url, json={
            "duration": "{duration}".format(duration=duration),
            "name": "{name}".format(name=sandbox_name)
        }, auth=self.token_auth)

        self._ensure_response_success(response)

        response_text = self.python_version_compatible_response_text(response)

        return jsonpickle.loads(response_text)['id']
Esempio n. 54
0
   def default(self, obj):

      if isinstance(obj, datetime.date) or \
         isinstance(obj, datetime.datetime) or \
         isinstance(obj, datetime.time):
         ## Note this issue with isodate module: "time formating does not allow
         ## to create fractional representations".
         ## Hence we use standard Python isoformat()
         ##
         s = obj.isoformat()
         if hasattr(obj, 'tzinfo') and obj.tzinfo is None and s[-1] != 'Z':
            ## assume UTC and append 'Z' for ISO format compliance!
            return s + 'Z'
         else:
            return s

      elif isinstance(obj, datetime.timedelta):
         #return (datetime.datetime.min + obj).time().isoformat()
         return isodate.duration_isoformat(obj)

      else:
         return super(CustomJsonEncoder, self).default(obj)
Esempio n. 55
0
 def build_intervals(self):
     """Build an intervals object, holding a list of Intervals to report in this update."""
     intervals = oadr_20b.intervals()
     if self.report.report_specifier_id == 'telemetry':
         # Build Intervals that report metrics.
         for telemetry_values in self.telemetry:
             intervals.add_interval(self.build_report_interval(telemetry_values))
     elif self.report.report_specifier_id == 'telemetry_status':
         # OADR rule 331, 510: Build an Interval, in a telemetry_status report, giving current VEN status.
         interval_start = isodate.parse_datetime(self.report.iso_last_report)
         interval_duration = isodate.duration_isoformat(timedelta(seconds=self.report.interval_secs))
         interval = oadr_20b.IntervalType(dtstart=oadr_20b.dtstart(date_time=interval_start),
                                          duration=oadr_20b.DurationPropType(duration=interval_duration))
         if self.online is not None or self.manual_override is not None:
             payload = oadr_20b.oadrReportPayloadType(rID='Status')
             payload_status = oadr_20b.oadrPayloadResourceStatusType(oadrOnline=self.online,
                                                                     oadrManualOverride=self.manual_override)
             payload_status.original_tagname_ = 'oadrPayloadResourceStatus'
             payload.set_payloadBase(payload_status)
             interval.set_streamPayloadBase([payload])
         else:
             interval.set_streamPayloadBase([])
         intervals.add_interval(interval)
     return intervals
Esempio n. 56
0
 def xmlvalue(self, value):
     return isodate.duration_isoformat(value)
Esempio n. 57
0
def get_site_data(site_code, service=None, parameter_code=None, start=None,
        end=None, period=None, modified_since=None, input_file=None):
    """Fetches site data.


    Parameters
    ----------
    site_code : str
        The site code of the site you want to query data for.
    service : {``None``, 'instantaneous', 'iv', 'daily', 'dv'}
        The service to use, either "instantaneous", "daily", or ``None``
        (default).  If set to ``None``, then both services are used.  The
        abbreviations "iv" and "dv" can be used for "instantaneous" and "daily",
        respectively.
    parameter_code : str
        Parameter code(s) that will be passed as the parameterCd parameter.
    start : ``None`` or datetime (see :ref:`dates-and-times`)
        Start of a date range for a query. This parameter is mutually exclusive
        with period (you cannot use both).
    end : ``None`` or datetime (see :ref:`dates-and-times`)
        End of a date range for a query. This parameter is mutually exclusive
        with period (you cannot use both).
    period : {``None``, str, datetime.timedelta}
        Period of time to use for requesting data. This will be passed along as
        the period parameter. This can either be 'all' to signal that you'd like
        the entire period of record, or string in ISO 8601 period format (e.g.
        'P1Y2M21D' for a period of one year, two months and 21 days) or it can
        be a datetime.timedelta object representing the period of time. This
        parameter is mutually exclusive with start/end dates.
    modified_since : ``None`` or datetime.timedelta
        Passed along as the modifiedSince parameter.
    input_file: ``None``, file path or file object
        If ``None`` (default), then the NWIS web services will be queried, but
        if a file is passed then this file will be used instead of requesting
        data from the NWIS web services.


    Returns
    -------
    data_dict : dict
        a python dict with parameter codes mapped to value dicts
    """
    url_params = {'format': 'waterml',
                  'site': site_code}
    if parameter_code:
        url_params['parameterCd'] = parameter_code
    if modified_since:
        url_params['modifiedSince'] = isodate.duration_isoformat(modified_since)

    if not (start is None or end is None) and period is not None:
        raise ValueError("must use either a date range with start/end OR a "
                "period, but not both")
    if period is not None:
        if isinstance(period, basestring):
            if period == 'all':
                if service in ('iv', 'instantaneous'):
                    start = datetime.datetime(2007, 10, 1)
                elif service in ('dv', 'daily'):
                    start = datetime.datetime(1851, 1, 1)
            else:
                url_params['period'] = period
        elif isinstance(period, datetime.timedelta):
            url_params['period'] = isodate.duration_isoformat(period)

    if service in ('dv', 'daily'):
        datetime_formatter = isodate.date_isoformat
    else:
        datetime_formatter = isodate.datetime_isoformat
    if start is not None:
        start_datetime = util.convert_datetime(start)
        url_params['startDT'] = datetime_formatter(start_datetime)
    if end is not None:
        end_datetime = util.convert_datetime(end)
        url_params['endDT'] = datetime_formatter(end_datetime)

    if service is not None:
        values = _get_site_values(service, url_params, input_file=input_file)
    else:
        kwargs = dict(parameter_code=parameter_code, start=start, end=end,
                period=period, modified_since=modified_since,
                input_file=input_file)
        values = get_site_data(site_code, service='daily', **kwargs)
        values.update(
            get_site_data(site_code, service='instantaneous', **kwargs))

    return values