def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
    'Get a code string from history, file, url, or a string or macro.\n\n        This is mainly used by magic functions.\n\n        Parameters\n        ----------\n        target : str\n            A string specifying code to retrieve. This will be tried respectively\n            as: ranges of input history (see %history for syntax), url,\n            corresponding .py file, filename, or an expression evaluating to a\n            string or Macro in the user namespace.\n\n            If empty string is given, returns complete history of current\n            session, without the last line.\n\n        raw : bool\n            If true (default), retrieve raw history. Has no effect on the other\n            retrieval mechanisms.\n\n        py_only : bool (default False)\n            Only try to fetch python code, do not try alternative methods to decode file\n            if unicode fails.\n\n        Returns\n        -------\n        A string of code.\n        ValueError is raised if nothing is found, and TypeError if it evaluates\n        to an object of another type. In each case, .args[0] is a printable\n        message.\n        '
    code = self.extract_input_lines(target, raw=raw)
    if code:
        return code
    try:
        if target.startswith(('http://', 'https://')):
            return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
    except UnicodeDecodeError as e:
        if (not py_only):
            from urllib.request import urlopen
            response = urlopen(target)
            return response.read().decode('latin1')
        raise ValueError(("'%s' seem to be unreadable." % target)) from e
    potential_target = [target]
    try:
        potential_target.insert(0, get_py_filename(target))
    except IOError:
        pass
    for tgt in potential_target:
        if os.path.isfile(tgt):
            try:
                return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
            except UnicodeDecodeError as e:
                if (not py_only):
                    with io_open(tgt, 'r', encoding='latin1') as f:
                        return f.read()
                raise ValueError(("'%s' seem to be unreadable." % target)) from e
        elif os.path.isdir(os.path.expanduser(tgt)):
            raise ValueError(("'%s' is a directory, not a regular file." % target))
    if search_ns:
        object_info = self.object_inspect(target, detail_level=1)
        if (object_info['found'] and object_info['source']):
            return object_info['source']
    try:
        codeobj = eval(target, self.user_ns)
    except Exception as e:
        raise ValueError(("'%s' was not found in history, as a file, url, nor in the user namespace." % target)) from e
    if isinstance(codeobj, str):
        return codeobj
    elif isinstance(codeobj, Macro):
        return codeobj.value
    raise TypeError(('%s is neither a string nor a macro.' % target), codeobj)
Get a code string from history, file, url, or a string or macro.

This is mainly used by magic functions.

Parameters
----------
target : str
    A string specifying code to retrieve. This will be tried respectively
    as: ranges of input history (see %history for syntax), url,
    corresponding .py file, filename, or an expression evaluating to a
    string or Macro in the user namespace.

    If empty string is given, returns complete history of current
    session, without the last line.

raw : bool
    If true (default), retrieve raw history. Has no effect on the other
    retrieval mechanisms.

py_only : bool (default False)
    Only try to fetch python code, do not try alternative methods to decode file
    if unicode fails.

Returns
-------
A string of code.
ValueError is raised if nothing is found, and TypeError if it evaluates
to an object of another type. In each case, .args[0] is a printable
message.
def find_user_code(self, target, raw=True, py_only=False, skip_encoding_cookie=True, search_ns=False):
    'Get a code string from history, file, url, or a string or macro.\n\n        This is mainly used by magic functions.\n\n        Parameters\n        ----------\n        target : str\n            A string specifying code to retrieve. This will be tried respectively\n            as: ranges of input history (see %history for syntax), url,\n            corresponding .py file, filename, or an expression evaluating to a\n            string or Macro in the user namespace.\n\n            If empty string is given, returns complete history of current\n            session, without the last line.\n\n        raw : bool\n            If true (default), retrieve raw history. Has no effect on the other\n            retrieval mechanisms.\n\n        py_only : bool (default False)\n            Only try to fetch python code, do not try alternative methods to decode file\n            if unicode fails.\n\n        Returns\n        -------\n        A string of code.\n        ValueError is raised if nothing is found, and TypeError if it evaluates\n        to an object of another type. In each case, .args[0] is a printable\n        message.\n        '
    code = self.extract_input_lines(target, raw=raw)
    if code:
        return code
    try:
        if target.startswith(('http://', 'https://')):
            return openpy.read_py_url(target, skip_encoding_cookie=skip_encoding_cookie)
    except UnicodeDecodeError as e:
        if (not py_only):
            from urllib.request import urlopen
            response = urlopen(target)
            return response.read().decode('latin1')
        raise ValueError(("'%s' seem to be unreadable." % target)) from e
    potential_target = [target]
    try:
        potential_target.insert(0, get_py_filename(target))
    except IOError:
        pass
    for tgt in potential_target:
        if os.path.isfile(tgt):
            try:
                return openpy.read_py_file(tgt, skip_encoding_cookie=skip_encoding_cookie)
            except UnicodeDecodeError as e:
                if (not py_only):
                    with io_open(tgt, 'r', encoding='latin1') as f:
                        return f.read()
                raise ValueError(("'%s' seem to be unreadable." % target)) from e
        elif os.path.isdir(os.path.expanduser(tgt)):
            raise ValueError(("'%s' is a directory, not a regular file." % target))
    if search_ns:
        object_info = self.object_inspect(target, detail_level=1)
        if (object_info['found'] and object_info['source']):
            return object_info['source']
    try:
        codeobj = eval(target, self.user_ns)
    except Exception as e:
        raise ValueError(("'%s' was not found in history, as a file, url, nor in the user namespace." % target)) from e
    if isinstance(codeobj, str):
        return codeobj
    elif isinstance(codeobj, Macro):
        return codeobj.value
    raise TypeError(('%s is neither a string nor a macro.' % target), codeobj)