<commit_before>from IPython.core.debugger import Pdb
import os
import ipdb
import sys
from pprint import pprint

class Lvdb(Pdb, object):
    """extends IPython Pdb by outputting 2 files on the interaction event

    .debug_location: filename:line_num
    """
    f_loc = "/tmp/lvdb.txt"

    def __init__(self, *args, **kwargs):
        self.fname = None
        self.line = None
        super(Lvdb, self).__init__(*args, **kwargs)

    def interaction(self, frame, traceback):

        if frame and frame.f_code:

            fname = os.path.abspath(frame.f_code.co_filename)
            line = frame.f_lineno

            if fname != self.fname or line != self.line:
                with open(self.f_loc, "w") as f:
                    f.write(fname + ":" + str(line))

                self.fname = fname
                self.line = line

        # call ipdb as normal
        super(Lvdb, self).interaction(frame, traceback)

def set_trace(frame=None):
    ''' recreates ipdb.set_trace() but uses Lvdb class rather than Pdb

        calls most of the ipdb.set_trace functionality and then does the rest
        manually. this is required because the internal functions update_stdout
        and wrap_sys_excepthook are not made available in the ipdb.__init__
        script

    '''
    try:
        ipdb.set_trace("a string as the frame argument causes an AttributeError")
    except AttributeError:
        pass

    if frame is None:
        frame = sys._getframe().f_back

    # 'Linux' is the color definition. For a cross-platform implementation, see
    # from IPython import get_ipython
    # def_colors = get_ipython().colors
    # on a linux system, def_colors will be 'Linux'
    # if a need arises this will be made cross-platform
    Lvdb('Linux').set_trace(frame)
<commit_msg>follow frames when going up/down in python<commit_after>from IPython.core.debugger import Pdb, decorate_fn_with_doc
import os
import sys

import ipdb


class Lvdb(Pdb, object):
    """extends IPython Pdb by outputting 2 files on the interaction event.

    .debug_location: filename:line_num
    """

    f_loc = "/tmp/lvdb.txt"

    def __init__(self, *args, **kwargs):
        self.fname = None
        self.line = None
        super(Lvdb, self).__init__(*args, **kwargs)

    def _write_frame(self, frame):
        if frame and frame.f_code:

            fname = os.path.abspath(frame.f_code.co_filename)
            line = frame.f_lineno

            if fname != self.fname or line != self.line:
                with open(self.f_loc, "w") as f:
                    f.write(fname + ":" + str(line))

                self.fname = fname
                self.line = line

    def interaction(self, frame, traceback):
        self._write_frame(frame)
        super(Lvdb, self).interaction(frame, traceback)

    def new_do_up(self, arg):
        super(Lvdb, self).new_do_up(arg)
        self._write_frame(self.curframe) 

    do_u = do_up = decorate_fn_with_doc(new_do_up, Pdb.do_up)

    def new_do_down(self, arg):
        super(Lvdb, self).new_do_down(arg)
        self._write_frame(self.curframe)

    do_d = do_down = decorate_fn_with_doc(new_do_down, Pdb.do_down)



def set_trace(frame=None):
    """Recreates ipdb's set_trace but uses Lvdb class rather than Pdb.

    calls most of the ipdb.set_trace functionality and then does the rest
    manually. this is required because the internal functions update_stdout
    and wrap_sys_excepthook are not made available in the ipdb.__init__
    script
    """
    try:
        ipdb.set_trace("a string as the frame argument causes an AttributeError")
    except AttributeError:
        pass

    if frame is None:
        frame = sys._getframe().f_back

    # 'Linux' is the color definition. For a cross-platform implementation, see
    # from IPython import get_ipython
    # def_colors = get_ipython().colors
    # on a linux system, def_colors will be 'Linux'
    # if a need arises this will be made cross-platform
    Lvdb('Linux').set_trace(frame)

{'fraction_non_alphanumeric': 0.088, 'fraction_numerical': 0.0005, 'mean_word_length': 2.5659536541889483, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3471023', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1242, 'n_words': 370}