python类suppress()的实例源码

consul.py 文件源码 项目:raptiformica 作者: vdloo 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def ensure_latest_consul_release(host=None, port=22):
    """
    Make sure the latest consul release is downloaded on the remote machine
    :param str host: hostname or ip of the remote machine, or None for the local machine
    :param int port: port to use to connect to the remote machine over ssh
    :return None:
    """
    log.info("Ensuring consul release {} is on disk "
             "on the remote machine".format(CONSUL_RELEASE.split('/')[-1]))
    with suppress(FileNotFoundError):
        # remove any previously existing zip in case we tried
        # before but the zip was corrupted
        remove(CONSUL_RELEASE.split('/')[-1])
    wget(
        CONSUL_RELEASE, host=host, port=port,
        failure_message="Failed to retrieve {}".format(
            CONSUL_RELEASE.split('/')[-1]
        )
    )
main.py 文件源码 项目:aiohttp-devtools 作者: aio-libs 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def run_app(app, port, loop):
    handler = app.make_handler(access_log=None, loop=loop)

    loop.run_until_complete(app.startup())
    server = loop.run_until_complete(loop.create_server(handler, HOST, port))

    try:
        loop.run_forever()
    except KeyboardInterrupt:  # pragma: no branch
        pass
    finally:
        logger.info('shutting down server...')
        server.close()
        loop.run_until_complete(server.wait_closed())
        loop.run_until_complete(app.shutdown())
        with contextlib.suppress(asyncio.TimeoutError, KeyboardInterrupt):
            loop.run_until_complete(handler.shutdown(0.1))
        with contextlib.suppress(asyncio.TimeoutError, KeyboardInterrupt):
            loop.run_until_complete(app.cleanup())
        with contextlib.suppress(KeyboardInterrupt):
            loop.close()
conftest.py 文件源码 项目:asynccmd 作者: valentinmk 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def loop():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(None)
    executor = concurrent.futures.ThreadPoolExecutor()
    loop.set_default_executor(executor)
    yield loop

    if loop.is_closed:
        pending = asyncio.Task.all_tasks(loop=loop)
        for task in pending:
            task.cancel()
            # Now we should await task to execute it's cancellation.
            # Cancelled task raises asyncio.CancelledError that we can suppress
            with suppress(asyncio.CancelledError):
                loop.run_until_complete(task)
        executor.shutdown()
        loop.close()

    gc.collect()
    asyncio.set_event_loop(None)
preferences_dialog.py 文件源码 项目:transmission-remote-gnome 作者: TingPing 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def apply(self):
        """Creates or deletes the autostart file based upon current state"""
        if not self.props.sensitive: # We never got the current state
            return

        if self.props.active and not self._was_enabled:
            logging.info('Creating autostart file')
            source = Gio.File.new_for_uri('resource:///se/tingping/Trg/se.tingping.Trg.service.desktop')
            if hasattr(source, 'copy_async'):
                # TODO: Fix upstream in GLib
                source.copy_async(self.autostart_file, Gio.FileCopyFlags.NONE, GLib.PRIORITY_DEFAULT)
            else:
                with suppress(GLib.Error):
                    source.copy(self.autostart_file, Gio.FileCopyFlags.NONE)
        elif not self.props.active and self._was_enabled:
            logging.info('Deleting autostart file')
            self.autostart_file.delete_async(GLib.PRIORITY_DEFAULT)
utils.py 文件源码 项目:difftrack 作者: qntln 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def data_mapper(mapper: Callable[[types.DataType], types.DataType]) -> Callable[[types.ListenerType], types.ListenerType]:
    '''
    Apply a mapper to the listener's `data` argument.
    '''

    def decorator(listener: types.ListenerType) -> types.ListenerType:

        @functools.wraps(listener)
        def wrapped(dtype: types.DiffType, index: int, data: types.DataType) -> None:
            listener(dtype, index, mapper(data))

        with contextlib.suppress(AttributeError):
            wrapped.finalize_batch = listener.finalize_batch

        return wrapped
    return decorator
get_reviews_by_neighborhood.py 文件源码 项目:AirbnbReviewAnalyzer 作者: mrsata 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def insert_reviews(neighborhood, db):
    listings_cursor = db['listings_' +
                         neighborhood].find({"reviews_count": {"$gt": 0}})
    listings = [listing for listing in listings_cursor]
    collection = db['reviews_' + neighborhood]
    for listing in listings:
        reviews_cursor = db.reviews_en.find({'listing_id':listing['_id']})
        reviews = [review for review in reviews_cursor]
        if reviews:
            with suppress(Exception):
                collection.insert_many(reviews, ordered=True)
            # try:
            #     collection.insert_many(reviews, ordered=True)
            # except:
            #     print(reviews)
            #     raise
grapher.py 文件源码 项目:speccer 作者: bensimner 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def remove(self, node):
        self._nodes.remove(node)

        for n in iter(self._nodes):
            with contextlib.suppress(KeyError):
                n.edges.remove(node)
clauses.py 文件源码 项目:speccer 作者: bensimner 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def _get_name_from_func(func, other):
    if not isinstance(func, types.LambdaType):
        with contextlib.suppress(AttributeError):
            return func.__qualname__

        with contextlib.suppress(AttributeError):
            return func.__name__

    return other
create_securitygroup.py 文件源码 项目:foremast 作者: gogoair 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def resolve_self_references(self, rules):
        """Resolves `$self` references to actual application name in security group rules."""
        with suppress(KeyError):
            rule = rules.pop('$self')
            rules[self.app_name] = rule
        return rules
utils.py 文件源码 项目:xarray-simlab 作者: benbovy 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __getattr__(self, name):
        if name != '__setstate__':
            # this avoids an infinite loop when pickle looks for the
            # __setstate__ attribute before the object is initialized
            with suppress(KeyError):
                return self._mapping[name]
        raise AttributeError("%r object has no attribute %r" %
                             (type(self).__name__, name))
scitools_client.py 文件源码 项目:saapy 作者: ashapochka 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def remove_project(self):
        self.close_project()
        with contextlib.suppress(FileNotFoundError):
            self.project_path.unlink()
povray.py 文件源码 项目:saapy 作者: ashapochka 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def cleanup(ctx):
    pv = Povray(ctx.config.povray.parent_dir)
    with contextlib.suppress(FileNotFoundError):
        pv.shelve_db_path.unlink()
        pv.scitools_udb_path.unlink()
context_processors.py 文件源码 项目:byro 作者: byro 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def byro_information(request):
    ctx = {'config': Configuration.get_solo()}

    try:
        ctx['url_name'] = resolve(request.path_info).url_name
    except Http404:
        ctx['url_name'] = ''

    if settings.DEBUG:
        ctx['development_warning'] = True
        with suppress(Exception):
            import subprocess
            ctx['byro_version'] = subprocess.check_output(['git', 'describe', '--always'])

    return ctx
caves.py 文件源码 项目:caves 作者: mikaelho 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def delete(self, sender):
    if self.control.filename != '':
      (name, _) = os.path.splitext(os.path.basename(self.control.filename))
      try:
        console.alert('Delete', name, button1='Ok')
      except KeyboardInterrupt:
        return
      os.remove(self.control.filename)
      with contextlib.suppress(FileNotFoundError):
        os.remove(self.control.filename[:-4]+'.json')
      self.control.set_default_map()
    else:
      self.control.quit_map(sender)
utils.py 文件源码 项目:plotnine 作者: has2k1 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def match(v1, v2, nomatch=-1, incomparables=None, start=0):
    """
    Return a vector of the positions of (first)
    matches of its first argument in its second.

    Parameters
    ----------
    v1: array-like
        the values to be matched

    v2: array-like
        the values to be matched against

    nomatch: int
        the value to be returned in the case when
        no match is found.

    incomparables: array-like
        a list of values that cannot be matched.
        Any value in v1 matching a value in this list
        is assigned the nomatch value.
    start: int
        type of indexing to use. Most likely 0 or 1
    """
    lookup = {}
    for i, x in enumerate(v2):
        if x not in lookup:
            lookup[x] = i

    lst = [nomatch] * len(v1)
    skip = set(incomparables) if incomparables else set()
    for i, x in enumerate(v1):
        if x in skip:
            continue

        with suppress(KeyError):
            lst[i] = lookup[x] + start

    return lst
utils.py 文件源码 项目:plotnine 作者: has2k1 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def suppress(*exceptions):
        """
        Return a context manager that suppresses any of the
        specified exceptions if they occur in the body of a
        with statement and then resumes execution with the
        first statement following the end of the with statement.
        """
        try:
            yield
        except exceptions:
            pass
utils.py 文件源码 项目:plotnine 作者: has2k1 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def get_valid_kwargs(func, potential_kwargs):
    """
    Return valid kwargs to function func
    """
    kwargs = {}
    for name in get_kwarg_names(func):
        with suppress(KeyError):
            kwargs[name] = potential_kwargs[name]
    return kwargs
database.py 文件源码 项目:saffron 作者: Lamden 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def init_dbs(sqls):
    # graceful initialization tries to create new tables as a test to see if this is a new DB or not
    for s in sqls:
        with suppress(sqlite3.OperationalError):
            cursor.execute(s)
vmf.py 文件源码 项目:srctools 作者: TeamSpen210 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __setitem__(self, key, val):
        """Allow using [] syntax to save a keyvalue.

        - It is case-insensitive, so it will overwrite a key which only
          differs by case.
        """
        if isinstance(val, bool):
            val = '1' if val else '0'
        key_fold = key.casefold()
        for k in self.keys:
            if k.casefold() == key_fold:
                # Check case-insensitively for this key first
                orig_val = self.keys.get(k)
                self.keys[k] = str(val)
                break
        else:
            orig_val = self.keys.get(key)
            self.keys[key] = str(val)

        # Update the by_class/target dicts with our new value
        if key_fold == 'classname':
            with suppress(KeyError):
                self.map.by_class[orig_val].remove(self)
            self.map.by_class[val].add(self)
        elif key_fold == 'targetname':
            with suppress(KeyError):
                self.map.by_target[orig_val].remove(self)
            self.map.by_target[val].add(self)
_5_idioms.py 文件源码 项目:Clean-code-in-Python 作者: rmariano 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def ignore_exceptions_2():
    data = None
    import contextlib
    with contextlib.suppress(FileNotFoundError, PermissionError):
        with open(DATA_FILE) as f:
            data = f.read()
    return data


问题


面经


文章

微信
公众号

扫码关注公众号