code: def hadoop_open(path: str, mode: str = "r", buffer_size: int = 8192):
      """Open a file through the Hadoop filesystem API. Supports distributed
      file systems like hdfs, gs, and s3.
  
      Warning
      -------
      Due to an implementation limitation, :func:`hadoop_open` may be quite
      slow for large data sets (anything larger than 50 MB).
  
      Examples
      --------
      Write a Pandas DataFrame as a CSV directly into Google Cloud Storage:
  
      >>> with hadoop_open('gs://my-bucket/df.csv', 'w') as f: # doctest: +SKIP
      ...     pandas_df.to_csv(f)
  
      Read and print the lines of a text file stored in Google Cloud Storage:
  
      >>> with hadoop_open('gs://my-bucket/notes.txt') as f: # doctest: +SKIP
      ...     for line in f:
      ...         print(line.strip())
  
      Write two lines directly to a file in Google Cloud Storage:
  
      >>> with hadoop_open('gs://my-bucket/notes.txt', 'w') as f: # doctest: +SKIP
      ...     f.write('result1: %s\\n' % result1)
      ...     f.write('result2: %s\\n' % result2)
  
      Unpack a packed Python struct directly from a file in Google Cloud Storage:
  
      >>> from struct import unpack
      >>> with hadoop_open('gs://my-bucket/notes.txt', 'rb') as f: # doctest: +SKIP
      ...     print(unpack('<f', bytearray(f.read())))
  
      Notes
      -----
      The supported modes are:
  
       - ``'r'`` -- Readable text file (:class:`io.TextIOWrapper`). Default behavior.
       - ``'w'`` -- Writable text file (:class:`io.TextIOWrapper`).
       - ``'x'`` -- Exclusive writable text file (:class:`io.TextIOWrapper`).
         Throws an error if a file already exists at the path.
       - ``'rb'`` -- Readable binary file (:class:`io.BufferedReader`).
       - ``'wb'`` -- Writable binary file (:class:`io.BufferedWriter`).
       - ``'xb'`` -- Exclusive writable binary file (:class:`io.BufferedWriter`).
         Throws an error if a file already exists at the path.
  
  
      The provided destination file path must be a URI (uniform resource identifier).
  
      .. caution::
  
          These file handles are slower than standard Python file handles. If you
          are writing a large file (larger than ~50M), it will be faster to write
          to a local file using standard Python I/O and use :func:`.hadoop_copy`
          to move your file to a distributed file system.
  
      Parameters
      ----------
      path : :class:`str`
          Path to file.
      mode : :class:`str`
          File access mode.
      buffer_size : :obj:`int`
          Buffer size, in bytes.
  
      Returns
      -------
          Readable or writable file handle.
      """
      # pile of hacks to preserve some legacy behavior, like auto gzip
      fs = Env.fs()
      if isinstance(fs, HadoopFS):
          return fs.legacy_open(path, mode, buffer_size)
      _, ext = os.path.splitext(path)
      if ext in (".gz", ".bgz"):
          binary_mode = "wb" if mode[0] == "w" else "rb"
          file = fs.open(path, binary_mode, buffer_size)
          file = gzip.GzipFile(fileobj=file, mode=mode)
          if "b" not in mode:
              file = io.TextIOWrapper(file, encoding="utf-8")
      else:
          file = fs.open(path, mode, buffer_size)
      return file
  
file_path: hail/hail/python/hail/utils/hadoop_utils.py
imports: import gzip
  import io
  import os.path
  import sys
  from typing import Any, Dict, List
  from hail.fs.hadoop_fs import HadoopFS
  from hail.utils import local_path_uri
  from hail.utils.java import Env, info
  from hail.typecheck import typecheck, enumeration
label_desc: Use image node_id 1 for calling a global function with example usage: hadoop_open(path, mode, buffer_size) and returns: file, fs