def cleanup(pin=None, assert_exists=False):
if pin is None:<EOL><INDENT>for pin in list(_open):<EOL><INDENT>cleanup(pin)<EOL><DEDENT>return<EOL><DEDENT>if not isinstance(pin, int):<EOL><INDENT>raise TypeError("<STR_LIT>".format(pin))<EOL><DEDENT>state = _open.get(pin)<EOL>if state is None:<EOL><INDENT>if assert_exists:<EOL><INDENT>raise ValueError("<STR_LIT>".format(pin))<EOL><DEDENT>return<EOL><DEDENT>state.value.close()<EOL>state.direction.close()<EOL>if os.path.exists(gpiopath(pin)):<EOL><INDENT>log.debug("<STR_LIT>".format(pin))<EOL>with _export_lock:<EOL><INDENT>with open(pjoin(gpio_root, '<STR_LIT>'), '<STR_LIT:w>') as f:<EOL><INDENT>_write(f, pin)<EOL><DEDENT><DEDENT><DEDENT>del _open[pin]<EOL>
Cleanup the pin by closing and unexporting it.

    Args:
        pin (int, optional): either the pin to clean up or None (default).
            If None, clean up all pins.
        assert_exists: if True, raise a ValueError if the pin was not
            setup. Otherwise, this function is a NOOP.