Did I find the right examples for you? yes no

All Samples(69)  |  Call(68)  |  Derive(0)  |  Import(1)
Return the entire source file and starting line number for an object.

The argument may be a module, class, method, function, traceback, frame,
or code object.  The source code is returned as a list of all the lines
in the file and the line number indexes a line in that list.  An IOError
is raised if the source code cannot be retrieved.

        def findsource(object):
    """Return the entire source file and starting line number for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of all the lines
    in the file and the line number indexes a line in that list.  An IOError
    is raised if the source code cannot be retrieved."""

    file = getfile(object)
    sourcefile = getsourcefile(object)
    if not sourcefile and file[:1] + file[-1:] != '<>':
        raise IOError('source code not available')
    file = sourcefile if sourcefile else file

    module = getmodule(object, file)
    if module:
        lines = linecache.getlines(file, module.__dict__)
    else:
        lines = linecache.getlines(file)
    if not lines:
        raise IOError('could not get source code')

    if ismodule(object):
        return lines, 0

    if isclass(object):
        name = object.__name__
        pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
        # make some effort to find the best matching class definition:
        # use the one with the least indentation, which is the one
        # that's most probably not inside a function definition.
        candidates = []
        for i in range(len(lines)):
            match = pat.match(lines[i])
            if match:
                # if it's at toplevel, it's already the best one
                if lines[i][0] == 'c':
                    return lines, i
                # else add whitespace to candidate list
                candidates.append((match.group(1), i))
        if candidates:
            # this will sort by whitespace, and by line number,
            # less whitespace first
            candidates.sort()
            return lines, candidates[0][1]
        else:
            raise IOError('could not find class definition')

    if ismethod(object):
        object = object.im_func
    if isfunction(object):
        object = object.func_code
    if istraceback(object):
        object = object.tb_frame
    if isframe(object):
        object = object.f_code
    if iscode(object):
        if not hasattr(object, 'co_firstlineno'):
            raise IOError('could not find function definition')
        lnum = object.co_firstlineno - 1
        pat = re.compile(r'^(\s*def\s)|(.*(? 0:
            if pat.match(lines[lnum]): break
            lnum = lnum - 1
        return lines, lnum
    raise IOError('could not find code object')
        


src/a/n/antisocial-HEAD/ve/lib/python2.7/site-packages/debug_toolbar/utils.py   antisocial(Download)
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except Exception:   # findsource raises platform-dependant exceptions
            first_lines = lines = index = None

src/r/o/rolf-HEAD/ve/lib/python2.7/site-packages/debug_toolbar/utils.py   rolf(Download)
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except Exception:   # findsource raises platform-dependant exceptions
            first_lines = lines = index = None

src/p/o/popcorn_maker-HEAD/vendor-local/lib/python/debug_toolbar/utils/__init__.py   popcorn_maker(Download)
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except (IOError, IndexError):
            lines = index = None

src/k/u/kuma-lib-HEAD/lib/python/debug_toolbar/utils.py   kuma-lib(Download)
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except Exception:   # findsource raises platform-dependant exceptions
            first_lines = lines = index = None

src/k/a/ka-lite-HEAD/python-packages/debug_toolbar/utils/__init__.py   ka-lite(Download)
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except (IOError, IndexError):
            lines = index = None

src/g/e/getopinionated-HEAD/libs/debug_toolbar/utils/__init__.py   getopinionated(Download)
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except (IOError, IndexError):
            lines = index = None

src/d/j/django-sql-stacktrace-0.2.2/sqlstacktrace/stacktrace.py   django-sql-stacktrace(Download)
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except (IOError, IndexError):
            lines = index = None

src/d/j/django-sql-stacktrace-HEAD/sqlstacktrace/stacktrace.py   django-sql-stacktrace(Download)
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except (IOError, IndexError):
            lines = index = None

src/d/j/django-debug-toolbar-1.2/debug_toolbar/utils.py   django-debug-toolbar(Download)
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except Exception:   # findsource raises platform-dependant exceptions
            first_lines = lines = index = None

src/d/j/django-debug-toolbar-HEAD/debug_toolbar/utils.py   django-debug-toolbar(Download)
        start = lineno - 1 - context // 2
        try:
            lines, lnum = inspect.findsource(frame)
        except Exception:   # findsource raises platform-dependant exceptions
            first_lines = lines = index = None

  1 | 2 | 3 | 4 | 5 | 6 | 7  Next