예제 #1
0
def test_with_backtrace(writer):
    logger.add(writer, backtrace=True)

    def c():
        a = 2
        b = 0
        a / b

    decorated = logger.catch()(c)
    decorated()

    result_with = writer.read()

    logger.remove()
    writer.clear()

    logger.add(writer, backtrace=False)

    decorated = logger.catch()(c)
    decorated()

    result_without = writer.read()

    assert len(result_with) > len(result_without)
    assert result_with.endswith(zero_division_error)
    assert result_without.endswith(zero_division_error)
예제 #2
0
def test_with_and_without_backtrace(writer):
    logger.add(writer, backtrace=True)

    def c():
        a = 2
        b = 0
        a / b

    decorated = logger.catch()(c)
    decorated()

    result_with = writer.read()

    logger.remove()
    writer.clear()

    logger.add(writer, backtrace=False)

    decorated = logger.catch()(c)
    decorated()

    result_without = writer.read()

    assert len(result_with.splitlines()) > len(result_without.splitlines())
    assert result_with.endswith("ZeroDivisionError: division by zero\n")
    assert result_without.endswith("ZeroDivisionError: division by zero\n")
예제 #3
0
파일: main.py 프로젝트: Danmou/MerCur-Re
 def _catch(self) -> Generator[None, None, None]:
     with logger.catch(BaseException,
                       level='TRACE',
                       reraise=not self.catch_exceptions):
         if self.catch_exceptions:
             with logger.catch(reraise=self.debug):
                 yield
         else:
             yield
예제 #4
0
def test_exception(writer, exception, should_raise, keyword, wrap_mode):
    logger.add(writer)

    if keyword:
        if wrap_mode == "decorator":

            @logger.catch(exception=exception)
            def a():
                1 / 0

        elif wrap_mode == "function":

            def a():
                1 / 0

            a = logger.catch(exception=exception)(a)
        elif wrap_mode == "context_manager":

            def a():
                with logger.catch(exception=exception):
                    1 / 0

    else:
        if wrap_mode == "decorator":

            @logger.catch(exception)
            def a():
                1 / 0

        elif wrap_mode == "function":

            def a():
                1 / 0

            a = logger.catch(exception)(a)
        elif wrap_mode == "context_manager":

            def a():
                with logger.catch(exception):
                    1 / 0

    if should_raise:
        with pytest.raises(ZeroDivisionError):
            a()
        assert writer.read() == ""
    else:
        a()
        assert writer.read().endswith(zero_division_error)
예제 #5
0
    async def on_ready(self) -> None:
        """Initialize some stuff once the bot is ready."""
        if self.first_on_ready:
            self.first_on_ready = False

            try:
                self.pool = await asyncpg.create_pool(**config.DATABASE)
            except asyncpg.exceptions.PostgresError:
                print("Database connection error. Killing program.")
                return await self.close()

            # Load all extensions
            for extension in self.extension_list:
                with logger.catch(message=f"Cog {extension} failed to load"):
                    self.load_extension(extension)
                    logger.debug(f"Cog {extension} loaded.")

            logger.info("Bot is ready")
        else:
            logger.info("Bot connection reinitialized")

        query = "SELECT * FROM public.prefixes"
        async with self.pool.acquire(timeout=5) as database:
            for row in await database.fetch(query):
                self.prefix_dict[row["ctx_id"]] = row["prefix"]
예제 #6
0
async def exception_logger(request: Request, call_next: Callable[..., Any]) -> Any:
    """
    Just to log all exception with godlike logger from loguru.
    """
    target_func = logger.catch()(call_next)
    response = await target_func(request)
    return response
예제 #7
0
def test_level(writer, wrap_mode, level, expected):
    logger.add(writer, format="{level.name} | {level.no}")

    if wrap_mode == "decorator":

        @logger.catch(level=level)
        def a():
            1 / 0

    elif wrap_mode == "function":

        def a():
            1 / 0

        a = logger.catch(level=level)(a)
    elif wrap_mode == "context_manager":

        def a():
            with logger.catch(level=level):
                1 / 0

    a()

    lines = writer.read().strip().splitlines()
    assert lines[0] == expected
예제 #8
0
def test_reraise(writer, wrap_mode):
    logger.add(writer)

    if wrap_mode == "decorator":

        @logger.catch(reraise=True)
        def a():
            1 / 0

    elif wrap_mode == "function":

        def a():
            1 / 0

        a = logger.catch(reraise=True)(a)
    elif wrap_mode == "context_manager":

        def a():
            with logger.catch(reraise=True):
                1 / 0

    with pytest.raises(ZeroDivisionError):
        a()

    assert writer.read().endswith(zero_division_error)
예제 #9
0
def test_context_manager(writer):
    logger.add(writer)

    with logger.catch():
        1 / 0

    assert writer.read().endswith(zero_division_error)
예제 #10
0
def test_returnwriter(writer, wrap_mode):
    logger.add(writer, format="{message}")

    if wrap_mode == "decorator":

        @logger.catch
        def a(x):
            return 100 / x

        result = a(50)
    elif wrap_mode == "function":

        def a(x):
            return 100 / x

        a = logger.catch()(a)
        result = a(50)
    elif wrap_mode == "context_manager":

        def a(x):
            with logger.catch():
                return 100 / x

        result = a(50)

    assert writer.read() == ""
    assert result == 2
예제 #11
0
def test_context_manager(writer):
    logger.add(writer)

    with logger.catch():
        1 / 0

    assert writer.read().endswith("ZeroDivisionError: division by zero\n")
예제 #12
0
def write_example(logger_name):
    """Writes log entries to the given logger."""
    logger.add(StackDriverSink(logger_name))
    logger.info('Hello, world!')
    logger.debug('Goodbye, world!')
    with logger.catch(
            message='devide by zero'):  #, onerror=lambda _: sys.exit(1)):
        1 / 0
    print('Wrote logs to {}.'.format(logger))
예제 #13
0
def test_message(writer, wrap_mode):
    logger.start(writer, format='{message}')
    message = 'An error occured:'

    if wrap_mode == 'decorator':
        @logger.catch(message=message)
        def a():
            1 / 0
        a()
    elif wrap_mode == 'function':
        def a():
            1 / 0
        a = logger.catch(message=message)(a)
        a()
    elif wrap_mode == "context_manager":
        with logger.catch(message=message):
            1 / 0

    assert writer.read().startswith(message + '\n')
예제 #14
0
def test_function(writer):
    logger.add(writer)

    def a():
        1 / 0

    a = logger.catch()(a)
    a()

    assert writer.read().endswith("ZeroDivisionError: division by zero\n")
예제 #15
0
def test_not_raising(writer, wrap_mode):
    logger.start(writer, format='{message}')
    message = "It's ok"

    if wrap_mode == "decorator":
        @logger.catch
        def a():
            logger.debug(message)
        a()
    elif wrap_mode == "function":
        def a():
            logger.debug(message)
        a = logger.catch()(a)
        a()
    elif wrap_mode == "context_manager":
        with logger.catch():
            logger.debug(message)

    assert writer.read() == message + '\n'
예제 #16
0
def main():
    email = environ.get("CODEWARS_EMAIL")
    password = environ.get("CODEWARS_PASSWORD")

    if not email or not password:
        raise Exception(
            "Environment variables CODEWARS_EMAIL and CODEWARS_PASSWORD are required"
        )

    args = arguments()

    if args.json:
        if args.path:
            logger.warning(
                "JSON output will be ignored because --path was provided")
        else:
            logger.remove()

    logger.catch(scrape)(args, email, password)
예제 #17
0
def test_function(writer):
    logger.add(writer)

    def a():
        1 / 0

    a = logger.catch()(a)
    a()

    assert writer.read().endswith(zero_division_error)
예제 #18
0
def test_not_raising(writer):
    logger.add(writer, format="{message}")
    message = "It's ok"

    def a():
        logger.debug(message)

    a = logger.catch()(a)
    a()

    assert writer.read() == message + "\n"
예제 #19
0
def test_message(writer):
    logger.add(writer, format="{message}")
    message = "An error occurred:"

    def a():
        1 / 0

    a = logger.catch(message=message)(a)
    a()

    assert writer.read().startswith(message + "\n")
    assert writer.read().endswith("ZeroDivisionError: division by zero\n")
예제 #20
0
def test_onerror_with_reraise(writer):
    called = False
    logger.add(writer, format="{message}")

    def onerror(_):
        nonlocal called
        called = True

    with pytest.raises(ZeroDivisionError):
        with logger.catch(onerror=onerror, reraise=True):
            1 / 0

    assert called
예제 #21
0
 def make_post(self, r):
     """"Uses currently open Response object to test for existence of various CSS
     tags. Returns dictionary for insertion into MongoDB, or None if the
     ingredients field is blank."""
     post = dict()
     soup = BeautifulSoup(r)
     for key, val in zip(list(self.tags.keys())[3:],
                         list(self.tags.values())[3:]):
         try:
             post[key] = soup.find(val.split('.')).string
         except AttributeError:
             pass
     raw_ingredients = soup.find(self.tags['full_recipe'].split('.'))
     if len(raw_ingredients) == 0:
         return None
     else:
         post['ingredients'] = raw_ingredients
         try:
             post['keywords'] = post['keywords'].split(',')
         except Exception as e:
             logger.catch(e)
             pass
         return post
예제 #22
0
def test_message(writer, wrap_mode):
    logger.add(writer, format="{message}")
    message = "An error occurred:"

    if wrap_mode == "decorator":

        @logger.catch(message=message)
        def a():
            1 / 0

        a()
    elif wrap_mode == "function":

        def a():
            1 / 0

        a = logger.catch(message=message)(a)
        a()
    elif wrap_mode == "context_manager":
        with logger.catch(message=message):
            1 / 0

    assert writer.read().startswith(message + "\n")
예제 #23
0
파일: command.py 프로젝트: deknowny/vkquick
    def __post_init__(self):
        self.prefixes = list(self.prefixes)
        self.names = list(self.names)
        if not self.names:
            self.names = self.handler.__name__
        self.handler = logger.catch(self.handler)

        self._text_arguments: typing.List[CommandTextArgument] = []
        self._message_storage_argument_name = None
        self._message_storage_argument_name: str
        self._parse_handler_arguments()

        self._routing_regex: typing.Pattern
        self._build_routing_regex()
예제 #24
0
파일: search.py 프로젝트: kivo360/jamboree
    def normal_insert(self, allow_duplicates=False):
        if allow_duplicates == False:
            verbatim_docs = self.verbatim_docs()

            if len(verbatim_docs) > 0 and allow_duplicates == False:

                # Not adding docs because we're not allowing duplicates
                return verbatim_docs[0].id, False
        insert_variables = self.insert_builder.build()
        _doc_id = self.insert_builder.doc_id
        index_name = self.client.index_name
        fields = [i.redis_args()[0] for i in self.indexable]
        with logger.catch(message=f"{index_name} - {fields}", reraise=True):
            self.client.add_document(_doc_id,
                                     payload=_doc_id,
                                     **insert_variables)

        return _doc_id, True
예제 #25
0
def reorder_csv_rows(test_jsonl, src):
    with logger.catch(reraise=True):
        # src = pd.read_csv(sub_csv)
        val_mtx = src.T.to_dict()
        id2row = {}
        for row in val_mtx.values():
            id2row[int(row['id'])] = row

        test_row_idx = []
        with open(test_jsonl, mode='r') as f:
            for row in f:
                test_row_idx.append(json.loads(row)['id'])

        reorder = [{
            'id': int(id2row[i]['id']),
            'proba': id2row[i]['proba'],
            'label': int(id2row[i]['proba'] > 0.5),
        } for i in test_row_idx]
        dst = pd.DataFrame.from_dict(reorder)[['id', 'proba', 'label']]
        return dst
예제 #26
0
def catch(decorated_function):
    """Customizing loguru's @catch decorator in one place

    sends tg message if SUDOISBOT_SYSTEMD env var is set

    """
    def onerror(e):
        # squawk to telegram, runs after error has been logged
        if os.environ.get("SUDOISBOT_SYSTEMD"):
            name = sys.argv[0]
            msg = f"{name} | {type(e).__name__}: {e}"
            logger.debug("sending notification of my impending death")
            try:
                send_to_me(f"``` {msg} ```")
            except Exception as e:
                logger.error(f"failed to send message: {e}")

        logger.debug("Exiting with '1'")
        sys.exit(1)

    return logger.catch(onerror=onerror)(decorated_function)
예제 #27
0
def test_returnwriter(writer, wrap_mode):
    logger.start(writer, format='{message}')

    if wrap_mode == 'decorator':
        @logger.catch
        def a(x):
            return 100 / x
        result = a(50)
    elif wrap_mode == 'function':
        def a(x):
            return 100 / x
        a = logger.catch()(a)
        result = a(50)
    elif wrap_mode == "context_manager":
        def a(x):
            with logger.catch():
                return 100 / x
        result = a(50)

    assert writer.read() == ""
    assert result == 2
예제 #28
0
파일: search.py 프로젝트: kivo360/jamboree
    def client(self):
        """client

        Get the client for the user. If it doesn't exist yet, create a new one with the given stop words. 
        
        For subkeys it adds fields if we've created them recently. 

        .. code-block:: python

            >>> self.client.add_document(_id, payload, **records)


        Returns
        -------
        [type]
            A redis connected client. Gets connection from Jamboree processor.
        """
        # self.processor
        if self.current_client is None:
            # We would insert a connection here. Use the connection from the search processor to operate.
            with logger.catch(ResponseError):
                self.current_client = Client(self.index,
                                             conn=self.processor.rconn)
                if len(self.indexable) > 0:
                    self.current_client.create_index(
                        self.indexable,
                        stopwords=[
                            "but", "there", "these", "they", "this", "to"
                        ],
                    )

        if self.is_sub_key:
            if not self.finished_alter:
                for i in self.indexable:
                    with suppress(ResponseError):
                        self.current_client.alter_schema_add([i])
                self.finished_alter = True

        return self.current_client
예제 #29
0
파일: main.py 프로젝트: ollema/purl
def run(action, args):
    algorithms = {}
    debug("algorithms detected:")
    for algorithm in ReinforcementLearningAlgorithm.subclasses:
        debug(algorithm.__name__)
        algorithms.update({algorithm.__name__: algorithm})
    debug("")

    try:
        if args.algorithm not in algorithms:
            valid_algorithms = ", ".join(algorithms.keys())
            error(f"choose a valid algorithm: {valid_algorithms}")
            return 1
        try:
            env = gym.make(args.environment)
        except gym.error.Error:
            valid_environments = "\n".join([
                env.id for env in gym.envs.registry.all()
                if env.id.startswith("MiniGrid")
            ])
            error(f"choose a valid gym enviroment:\n{valid_environments}")
            return 1

        algo = algorithms[args.algorithm](env=env, args=args)
        with logger.catch(reraise=True):
            if action == "train":
                algo.train()
            if action == "visualize":
                algo.visualize()

    except AlgorithmError as e:
        error(e.msg)
        return 1

    except Exception as e:
        error(e)
        return 1
    return 0
예제 #30
0
def b(x):
    def nested(i):
        1 / i

    with logger.catch():
        nested(x)