0

I'm doing the nucleai courses. Exercises are based on python scripts and I'm using Visual Studio 2015. At some point, we have to use the library nltk. I was trying to debug some code I'm calling (I have the source) and something really weird happens: breakpoints work, but I can't use F10 to jump line to line. It just skips all the script. I can debug without any problem any of my scripts but not those within the library. So my question is: is there any option to "unlock" the script so I can debug line by line? I'm new with python and I can't find anything similar on google. I'm posting the code in case something is relevant. The function I want to debug is 'Respond'

from __future__ import print_function

import re
import random
from nltk import compat

reflections = {
"i am"       : "you are",
"i was"      : "you were",
"i"          : "you",
"i'm"        : "you are",
"i'd"        : "you would",
"i've"       : "you have",
"i'll"       : "you will",
"my"         : "your",
"you are"    : "I am",
"you were"   : "I was",
"you've"     : "I have",
"you'll"     : "I will",
"your"       : "my",
"yours"      : "mine",
"you"        : "me",
"me"         : "you"
}

class Chat(object):
def __init__(self, pairs, reflections={}):
    self._pairs = [(re.compile(x, re.IGNORECASE),y) for (x,y) in pairs]
    self._reflections = reflections
    self._regex = self._compile_reflections()


def _compile_reflections(self):
    sorted_refl = sorted(self._reflections.keys(), key=len,
            reverse=True)
    return  re.compile(r"\b({0})\b".format("|".join(map(re.escape,
        sorted_refl))), re.IGNORECASE)

def _substitute(self, str):
   return self._regex.sub(lambda mo:
            self._reflections[mo.string[mo.start():mo.end()]],
                str.lower())

def _wildcards(self, response, match):
    pos = response.find('%')
    while pos >= 0:
        num = int(response[pos+1:pos+2])
        response = response[:pos] + \
            self._substitute(match.group(num)) + \
            response[pos+2:]
        pos = response.find('%')
    return response

def respond(self, str):
    # check each pattern
    for (pattern, response) in self._pairs:
        match = pattern.match(str)

        # did the pattern match?
        if match:
            resp = random.choice(response)    # pick a random response
            resp = self._wildcards(resp, match) # process wildcards

            # fix munged punctuation at the end
            if resp[-2:] == '?.': resp = resp[:-2] + '.'
            if resp[-2:] == '??': resp = resp[:-2] + '?'
            return resp

# Hold a conversation with a chatbot
def converse(self, quit="quit"):
    input = ""
    while input != quit:
        input = quit
        try: input = compat.raw_input(">")
        except EOFError:
            print(input)
        if input:
            while input[-1] in "!.": input = input[:-1]
            print(self.respond(input))

Any help will be very much appreciated. Thanks.

EDIT: I solved my problem but I haven't found a solution for the question. I'm using PyCharm (as suggested in the first comment) and it works like a charm. I can debug everything without any problem now. No file modification at all. I'm inclined to think that this is a bug in Python tools for Visual Studio.

MBRebaque
  • 359
  • 2
  • 5
  • 14
  • 1
    Do you custom the keyboard shortcuts? It seems that F10 is "step Over", do you mean that you want to use "Step Into(F11)" under Debug menu? If you add a breakpoint, how about the result if you debug it? Other members use the PyCharm instead of PTVS as a workaround: http://stackoverflow.com/questions/37240431/python-tools-visual-studio-step-into-not-working – Jack Zhai Oct 18 '16 at 03:22
  • Hi Jack. I haven't customize my keyboard, I'm keeping all the default settings. I can't either Step Over or Step into within that script, I can do it however in any of the scripts I write. If I add a breakpoint, it stops and I can see the values of variables etc but, if I hit F10 to go to the next line, it just jumps everithing in that function. The only thing that worked is if I set a breakpoint in every line within the function and I hit F5 to jump one by one. It just doesn't make any sense. I will try PyCharm and post the results. Thanks – MBRebaque Oct 18 '16 at 09:04
  • you're welcome. You could share me a simple sample, I will debug it using the same VS Environment, so we could know that whether it is related to the VS debugger tool or specific project type. Another suggestion, like my previous comment, using other way like PyCharm instead of it as a workaround. If you get any information, feel free to let me know:) – Jack Zhai Oct 18 '16 at 09:35
  • Well Jack, you gave me the solution. I think this is a bug in Python VS tools. I opened the project in PyCharm and I can debug everything without any problem. It works great. So I guess this is my new Python IDE. Thanks a lot. Cheers – MBRebaque Oct 18 '16 at 18:53
  • Hi MBRebaque, since it was helpful for you, I post it as the answer, if possible, please mark it as the answer. Any VS debugger issue, welcome back:) – Jack Zhai Oct 19 '16 at 00:43

1 Answers1

1

Other members also got the similar issue, my suggestion is that you can use the PyCharm instead of PTVS as a workaround. Of course, you could also start a discussion(Q AND A) from this site for PTVS tool:

https://visualstudiogallery.msdn.microsoft.com/9ea113de-a009-46cd-99f5-65ef0595f937

Jack Zhai
  • 6,230
  • 1
  • 12
  • 20