def _infer_compression(filepath_or_buffer, compression):
<EOL>if compression is None:<EOL><INDENT>return None<EOL><DEDENT>if compression == '<STR_LIT>':<EOL><INDENT>filepath_or_buffer = _stringify_path(filepath_or_buffer)<EOL>if not isinstance(filepath_or_buffer, str):<EOL><INDENT>return None<EOL><DEDENT>for compression, extension in _compression_to_extension.items():<EOL><INDENT>if filepath_or_buffer.endswith(extension):<EOL><INDENT>return compression<EOL><DEDENT><DEDENT>return None<EOL><DEDENT>if compression in _compression_to_extension:<EOL><INDENT>return compression<EOL><DEDENT>msg = '<STR_LIT>'.format(compression)<EOL>valid = ['<STR_LIT>', None] + sorted(_compression_to_extension)<EOL>msg += '<STR_LIT>'.format(valid)<EOL>raise ValueError(msg)<EOL>
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