Пример #1
0
 def __init__(self, defaults: Dict = None):
     self.defaults = {
         datetime: lambda x: TimeUtils.to_timestamp(x),
         date: lambda x: TimeUtils.to_timestamp(x),
         UUID: lambda x: x.hex,
         set: lambda x: list(x),
         DictObject: lambda x: x.to_dict(),
     }
     self.defaults.update(defaults or {})
Пример #2
0
 def write_command(cls, command, path: Text = None):
     path = path if path else '/tmp'
     timestamp = TimeUtils.utc_timestamp()
     cls.sequence += 1
     tmp_file = f'appyratus-sys-exec-{timestamp}-{cls.sequence}.sh'
     file_path = PathUtils.join(path, tmp_file)
     File.write(file_path, f"#!/usr/bin/env bash\n{command}")
     PathUtils.make_executable(file_path, user=True)
     return file_path
Пример #3
0
 def process(self, value):
     if isinstance(value, (int, float)):
         return (value, None)
     elif isinstance(value, datetime):
         return (TimeUtils.to_timestamp(value), None)
     elif isinstance(value, date):
         return (time.mktime(value.timetuple()), None)
     else:
         return (None, UNRECOGNIZED_VALUE)
Пример #4
0
class NumericIndex(RangeIndex):
    custom_serializers = {
        UUID: lambda x: int(x.hex, 16),
        datetime: lambda x: TimeUtils.to_timestamp(x),
        bool: lambda x: int(x),
    }

    def upsert(self, _id, value, pipe=None):
        redis = pipe if pipe is not None else self.redis
        ser = self.custom_serializers.get(value.__class__)
        value = ser(value) if ser else value

        redis.zrem(self.name, _id)
        redis.zadd(self.name, {_id: value})

    def delete(self, _id, pipe=None):
        redis = pipe if pipe is not None else self.redis
        redis.zrem(self.name, _id)

    def delete_many(self, _ids, pipe=None):
        if isinstance(_ids, GeneratorType):
            _ids = tuple(_ids)
        if _ids:
            redis = pipe if pipe is not None else self.redis
            redis.zrem(self.name, *_ids)

    def search(self,
               lower=None,
               upper=None,
               include_lower=True,
               include_upper=False,
               offset=None,
               limit=None,
               pipe=None):
        if lower is not None:
            lower = '({}'.format(lower) if not include_lower else lower
        else:
            lower = '-inf'

        if upper is not None:
            upper = '({}'.format(upper) if not include_upper else upper
        else:
            upper = '+inf'

        if limit is not None and offset is None:
            offset = 0

        redis = pipe if pipe is not None else self.redis

        return [
            v.split(self.DELIM_BYTES)[-1] for v in redis.zrangebyscore(
                self.name, lower, upper, start=offset, num=limit)
        ]
Пример #5
0
 def __init__(self, exc, middleware=None):
     self.middleware = middleware
     self.timestamp = TimeUtils.utc_now()
     self.exc = exc
     if isinstance(exc, RavelError) and exc.wrapped_traceback:
         self.trace = self.exc.wrapped_traceback.strip().split('\n')[1:]
         self.exc_message = self.trace[-1].split(': ', 1)[1]
     else:
         self.trace = traceback.format_exc().strip().split('\n')[1:]
         final_line = self.trace[-1]
         if ':' in final_line:
             self.exc_message = final_line.split(':', 1)[1]
         else:
             self.exc_message = None
Пример #6
0
 def read_context(cls, path: Text = None):
     _, context, _, _ = DotFileManager.get_context(path)
     embryo_commands = cls.parse_context(context)
     embryo_commands.sort(key=lambda tup: tup[0])
     used_commands = []
     for timestamp, command in embryo_commands:
         if command in used_commands:
             continue
         if timestamp is not None:
             ts = TimeUtils.from_timestamp(timestamp)
         else:
             ts = None
         used_commands.append((ts, command))
     return used_commands
Пример #7
0
    def build_response_message(self, message: Message,
                               source: Dict) -> Message:
        """
        Recursively bind each value in a source dict to the corresponding
        attribute in a grpc Message object, resulting in the prepared response
        message, ready to be sent back to the client.
        """
        if source is None:
            return None

        for k, v in source.items():
            if v is None:
                # protobufs don't understand null values
                continue

            # TODO: move conversion logic into adapters
            if isinstance(v, (date, datetime)):
                ts = TimeUtils.to_timestamp(v)
                setattr(message, k, ts)
            elif isinstance(getattr(message, k), Message):
                sub_message = getattr(message, k)
                assert isinstance(v, dict)
                self.build_response_message(sub_message, v)
            elif isinstance(v, dict):
                json_str = self.json.encode(v)
                setattr(message, k, json_str)
            elif isinstance(v, (list, tuple, set)):
                list_field = getattr(message, k)
                sub_message_type_name = ('{}Schema'.format(k.title().replace(
                    '_', '')))
                sub_message_type = getattr(message, sub_message_type_name,
                                           None)
                if sub_message_type:
                    list_field.extend(
                        self.build_response_message(sub_message_type(), v_i)
                        for v_i in v)
                else:
                    v_prime = [
                        x if not isinstance(x, dict) else self.json.encode(x)
                        for x in v
                    ]
                    list_field.extend(v_prime)
            else:
                setattr(message, k, v)

        return message
Пример #8
0
    def __init__(
        self,
        method: Text,
        args: Tuple = None,
        kwargs: Dict = None,
        result=None,
        exc: Exception = None,
    ):
        self.method = method
        self.args = list(args) if args else []
        self.kwargs = kwargs or {}
        self.result = result
        self.timestamp = TimeUtils.utc_now()
        self.exc = exc

        # fix up create and create_many args and kwargs.
        # see the doc string for _backfill_id_fields.
        if (exc is None) and (method in {'create', 'create_many'}):
            self._backfill_id_fields(result)
Пример #9
0
    def process_message(self, level: Text, message: Text, data: Dict) -> Text:
        when = TimeUtils.utc_now().strftime('%m/%d/%Y %H:%M:%S')
        level = level.upper()[0]

        if data:
            data = json.decode(json.encode(data))
            if self._style == 'json':
                dumped_data = self._to_json(data)
            elif self._style == 'yaml':
                dumped_data = self._to_yaml(data)
            else:
                raise ValueError(f'unrecognized log style: {self.style}')
        else:
            dumped_data = None

        display_string = f'{when} ({level}) {self._name} - {message}'
        if dumped_data:
            display_string += f'\n\n{dumped_data}\n'
        return display_string
Пример #10
0
    def __init__(
        self,
        embryo_name: str,
        destination: str,
        context: Dict = None,
        embryo: 'Embryo' = None,
    ):
        """
        Generate an embryo, along with any embryos nested therein. Returns a
        list of Renderer objects. The first instance is the embryo being
        generated, and the rest are the nested ones.

        # Args
        - `embryo_name`: The name of the embryo.
        - `destination`: Directory to hatch embryo into
        - `context`: Context data to merge into other sources.
        """
        self._embryo_class = None
        self._embryo_path = None
        self._embryo = None

        if embryo_name is None:
            # this should mean we're coming from the
            # from_embryo factory method
            return

        # ------
        # Add Embryo metadata to context
        context.update({
            'embryo': {
                'name': embryo_name,
                'destination':
                os.path.abspath(os.path.expanduser(destination)),
                'timestamp': TimeUtils.utc_now(),
                'action': 'hatch',
            }
        })

        embryo_name, embryo_path, embryo_class = get_embryo_resource(
            embryo_name)
        self._embryo = embryo_class(embryo_path, context)
Пример #11
0
    def process(self, value):
        if isinstance(value, str):
            try:
                dt = dateutil.parser.parse(value)
            except:
                return (None, INVALID_VALUE)
        elif isinstance(value, (int, float)):
            dt = TimeUtils.from_timestamp(value)
        elif isinstance(value, datetime):
            dt = value.replace(tzinfo=self.timezone)
        elif isinstance(value, date):
            dt = datetime.combine(value, datetime.min.time())
        else:
            return (None, UNRECOGNIZED_VALUE)

        if self.format_spec:
            dt_str = datetime.strftime(dt, self.format_spec)
        else:
            dt_str = dt.isoformat()

        return (dt_str, None)
Пример #12
0
 def process(self, value):
     if isinstance(value, datetime):
         return (value.replace(tzinfo=self.tz), None)
     elif isinstance(value, (int, float)):
         try:
             return (TimeUtils.from_timestamp(value).replace(
                 tzinfo=self.tz), None)
         except ValueError:
             return (None, INVALID_VALUE)
     elif isinstance(value, date):
         new_value = datetime.combine(value, datetime.min.time())
         new_value = new_value.replace(tzinfo=self.tz)
         return (new_value, None)
     elif isinstance(value, str):
         try:
             return (dateutil.parser.parse(value).replace(tzinfo=self.tz),
                     None)
         except:
             return (None, INVALID_VALUE)
     else:
         return (None, UNRECOGNIZED_VALUE)
Пример #13
0
    return fitness


if __name__ == '__main__':
    # optional constraints on the argument search space
    options = {
        'age': {
            'min': 30,
            'max': 50
        },
        'temperature': {
            'min': 91.0,
            'max': 104
        },
        'name': {
            'enum': ['Jeff', 'John', 'Jimbob', 'Joffrey']
        },
    }

    # NOTE: statistics False in production by default
    tuner = FunctionTuner(epochs=256, statistics=True)

    # returns top individual from latest generation
    from appyratus.utils.time_utils import TimeUtils

    args, time = TimeUtils.timed(lambda: tuner.tune(objective, options))

    print(f'Runtime: {time.total_seconds():.2f}s')
    print(f'Best Args:', args)

    tuner.plot()
Пример #14
0
 def on_generate(self, **kwargs):
     return TimeUtils.to_timestamp(
         self.faker.date_time_this_year(tzinfo=pytz.utc))
Пример #15
0
 def logout(self):
     if self.is_active:
         self.update(logged_out_at=TimeUtils.utc_now(), is_active=False)