Repository: yt-dlp/yt-dlp
File: yt_dlp/utils.py (Python)

Code before:

        if not to_cancel:
            return

        for task in to_cancel:
            task.cancel()

        loop.run_until_complete(
            asyncio.tasks.gather(*to_cancel, loop=loop, return_exceptions=True))

        for task in to_cancel:
            if task.cancelled():
                continue
            if task.exception() is not None:
                loop.call_exception_handler({
                    'message': 'unhandled exception during asyncio.run() shutdown',
                    'exception': task.exception(),
                    'task': task,
                })


has_websockets = bool(compat_websockets)


def get_first(obj, keys, **kwargs):
    return traverse_obj(obj, (..., *variadic(keys)), **kwargs, get_all=False)


Review comment (refactor):
This was made a function in youtube.py because it was being used a lot (33 hits). Considering it is just thin wrapper around travsere_obj, idk if this is useful enough in general to be a util function. Excluding this extractor (6 hits), this pattern is only used 5 other times

```py
yt_dlp • extractor\facebook.py:
  408:             title = traverse_obj(media, (..., 'title', 'text'), get_all=False)
  409              description = traverse_obj(media, (
  410                  ..., 'creation_story', 'comet_sections', 'message', 'story', 'message', 'text'), get_all=False)
  411:             uploader_data = (traverse_obj(media, (..., 'owner'), get_all=False)
  412:                              or traverse_obj(post, (..., 'node', 'actors', ...), get_all=False) or {})

yt_dlp • extractor\tiktok.py:
  820:             traverse_obj(render_data, (..., 'aweme', 'detail'), get_all=False), url)
```

Code after:
    @staticmethod
    def hide_login_info(opts):
        PRIVATE_OPTS = set(['-p', '--password', '-u', '--username', '--video-password', '--ap-password', '--ap-username'])
        eqre = re.compile('^(?P<key>' + ('|'.join(re.escape(po) for po in PRIVATE_OPTS)) + ')=.+$')

        def _scrub_eq(o):
            m = eqre.match(o)
            if m:
                return m.group('key') + '=PRIVATE'
            else:
                return o

        opts = list(map(_scrub_eq, opts))
        for idx, opt in enumerate(opts):
            if opt in PRIVATE_OPTS and idx + 1 < len(opts):
                opts[idx + 1] = 'PRIVATE'
        return opts

    def append_config(self, *args, label=None):
        config = type(self)(self._parser, label)
        config._loaded_paths = self._loaded_paths
        if config.init(*args):
            self.configs.append(config)

    @property
    def all_args(self):
        for config in reversed(self.configs):
            yield from config.all_args
        yield from self.own_args or []

    def parse_args(self):
        return self._parser.parse_args(list(self.all_args))


class WebSocketsWrapper():
    """Wraps websockets module to use in non-async scopes"""

    def __init__(self, url, headers=None):
        self.loop = asyncio.events.new_event_loop()
        self.conn = compat_websockets.connect(
            url, extra_headers=headers, ping_interval=None,
            close_timeout=float('inf'), loop=self.loop, ping_timeout=float('inf'))
        atexit.register(self.__exit__, None, None, None)

    def __enter__(self):
        self.pool = self.run_with_loop(self.conn.__aenter__(), self.loop)
        return self

    def send(self, *args):
        self.run_with_loop(self.pool.send(*args), self.loop)