code: def _infer_compression(filepath_or_buffer, compression):
      """
      Get the compression method for filepath_or_buffer. If compression='infer',
      the inferred compression method is returned. Otherwise, the input
      compression method is returned unchanged, unless it's invalid, in which
      case an error is raised.
  
      Parameters
      ----------
      filepath_or_buffer :
          a path (str) or buffer
      compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}
          If 'infer' and `filepath_or_buffer` is path-like, then detect
          compression from the following extensions: '.gz', '.bz2', '.zip',
          or '.xz' (otherwise no compression).
  
      Returns
      -------
      string or None :
          compression method
  
      Raises
      ------
      ValueError on invalid compression specified
      """
  
      # No compression has been explicitly specified
      if compression is None:
          return None
  
      # Infer compression
      if compression == "infer":
          # Convert all path types (e.g. pathlib.Path) to strings
          filepath_or_buffer = _stringify_path(filepath_or_buffer)
          if not isinstance(filepath_or_buffer, compat.string_types):
              # Cannot infer compression of a buffer, assume no compression
              return None
  
          # Infer compression from the filename/URL extension
          for (
              compression,
              extension,
          ) in _compression_to_extension.items():
              if filepath_or_buffer.endswith(extension):
                  return compression
          return None
  
      # Compression has been specified. Check that it's valid
      if compression in _compression_to_extension:
          return compression
  
      msg = "Unrecognized compression type: {}".format(compression)
      valid = ["infer", None] + sorted(_compression_to_extension)
      msg += "\nValid compression types are {}".format(valid)
      raise ValueError(msg)
  
imports: import codecs
  from contextlib import closing, contextmanager
  import csv
  import mmap
  import os
  import zipfile
  import pandas.compat
  from pandas.compat import BytesIO, StringIO, string_types, text_type
  from pandas.errors import AbstractMethodError, DtypeWarning, EmptyDataError, ParserError, ParserWarning
  from pandas.core.dtypes.common import is_file_like, is_number
  from pandas.io.formats.printing import pprint_thing
label_desc: Use image node_id 9 for calling a global function with example usage: _infer_compression(filepath_or_buffer, compression) and returns: None, None, compression, None, compression