#๐Ÿ”’ learning to debug

315 messages ยท Page 1 of 1 (latest)

spring latch
#

i want to know what the point that i put in the gutter does?. i think it tells at what time it will run. but i want to know what will the highlighted point do when randoom.random() < 0.2 become true, with the help of pycharm debugger

hot thicketBOT
#

@spring latch

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

cinder summit
spring latch
#

point no 16

cinder summit
#

you mean a breakpoint

spring latch
#

marked circle

#

yes

cinder summit
#

the red circle means you've set a breakpoint at line 16

spring latch
#

ok

cinder summit
#

Do you understand for loop?

spring latch
#

yes i do

cinder summit
#

OK so do you understand what columns[i] mean if i is 1 for example?

spring latch
#

i previously put breakpoint on different lines and that time it didnt run to a random i

cinder summit
spring latch
#

do i need to explain try, except too?

cinder summit
spring latch
#

if loop is running and firstly its i becomes 1 and the if random.random is less than .2 then column[1] will be equal to a random number between 4 and 14 (both inclusive)

cinder summit
#

ok good it seems you understood so far.

#

Next, lets look at that pycharm debugger

spring latch
#

ok

cinder summit
#

So in your debugger, you have, from what I can see, 3 expressions that you are watching out for and see what values they are returning during the program run

#

WIDTH is 70

#

columns is a list of 70 numbers

#

and i is a number 62

#

So by following the for loop logic, i is now on the 62 iteration out of the 70 range from WIDTH

#

Then on this 62 iteration, the program stopped because the if condition random.random() < 0.02 is True and is now hitting the line 16 for setting columns[62] to a new random.randint number between 4 and 14

#

You can probably verify the if condition by adding random.random() and random.random() < 0.02 in the expression to watch for in your debugger

#

Enter here for those expressions. If you wanna watch for more expressions and values, you enter them here

spring latch
#

oh ok

#

let me try

#

how do i enter

cinder summit
#

click in that box and type it in

cinder summit
#

just do random.random() and hit enter

#

And then do another expression random.random() < 0.02 and hit enter

spring latch
#

how do i differentiate

#

by a comma

#

?

cinder summit
spring latch
#

sorry, i am not clear how this expression writing works

cinder summit
# spring latch

OK so you see in this screenshot that you shared, you typed random.random() = .12, right?

cinder summit
#

Then show me a screenshot of your current debugger

spring latch
cinder summit
# spring latch

Pressing enter doesn't save that expression into the debugger?

#

Click here, and then press enter...

#

Or press this button

spring latch
cinder summit
# spring latch

I think you've deleted the random.random() or something. But if you add the random.random() again, you can see what value it shows to tell you that the current iteration of random.random() < 0.02 is False

spring latch
#

can i manually change this

cinder summit
#

Like this one. We can see what value is behind random.random() because its being watched in your Pycharm debugger. In this screenshot, its 0.0083627...

spring latch
#

no.

cinder summit
#

you can have as many expressions and variables that you wanna watch in the debugger

spring latch
cinder summit
#

This is like the better alternative than riddling your script with print() statements

cinder summit
cinder summit
# spring latch

So you can see now, at iteration i at 55, the random.random() has a value of 0.33 which is bigger than 0.02 so the random.random() < 0.02 is False

spring latch
#

what if i changed the value of the if random.random() < 0.2

cinder summit
spring latch
#

oh

cinder summit
#

The values follows whatever is going on inside the running program in debug

#

like if the program is doing a = 1 + 1. And you are watching the variable a during debug. You would see that value shows as a = 2. You can't forcibly change that to 3 or "Bob" or whatever you want during the debug.

#

Does this make sense to you @spring latch ?

cinder summit
spring latch
#

watching a means the breakpoint is on a

cinder summit
#

oh it doesn't make sense to you? Sorry. What part is still confusing you?

cinder summit
#

"watching" a variable or expression is to watch what values are being stored throughout the program run

#

The "breakpoint" is used to help stop the program when it reaches that line

#

You can "watch" a variable or expression without setting up breakpoints

spring latch
#

ok

#

how much the learning is in debugging

cinder summit
#

Everything I explained to you, that is it. There's nothing more to learn in debugging

spring latch
#

okay

cinder summit
#

Its not a big topic

spring latch
#

i think i need to do practical on it..i got your points like 40 to 50%

cinder summit
#

Maybe try a smaller script example?

spring latch
#

yes yes

#

that is what i was going to ask for'

#

i meant similar

#

not smaller

cinder summit
#

sorry whats the question?

spring latch
cinder summit
spring latch
#

but one question still remained unanswered

#

what does the random.randit(4,15) do

cinder summit
#

!d random.randint

hot thicketBOT
#

random.randint(a, b)```
Return a random integer *N* such that `a <= N <= b`. Alias for `randrange(a, b+1)`.
cinder summit
#

It gives you a random number between 4 and 15

spring latch
#

columns[i] = random.randint(4,14)

#

and this?

cinder summit
#

!e

import random
print(random.randint(4,14))
print(random.randint(4,14))
print(random.randint(4,14))
print(random.randint(4,14))
hot thicketBOT
cinder summit
cinder summit
cinder summit
spring latch
#

does it mean column name will be changed to a no. btw 4 and 14 inclusive and after reading the remaining code, for e.g. it is 4 , then else will run and changed it into 0 or 1?

cinder summit
#

@spring latch what is i in this context?

spring latch
#

column no

cinder summit
spring latch
#

from length of width

cinder summit
#
    columns = [0] * WIDTH
    while True:
        #loop over each column:
        for i in range(WIDTH):
            if random.random() < 0.02:
                # Restart a stream counter on this column.
                # the stream length is between 4 and 14 characters long.
                columns[i] = random.randint(4,14)
spring latch
#

let me show you the full code

cinder summit
#

You have a columns which is [0] * WIDTH

#

The WIDTH seems to be a constant of 70

#

So columns is [0] * 70

spring latch
#
import random,sys,time

WIDTH = 70

try:
    # For each column, when the counter is 0, no stream is shown.
    # otherwise, it acts as a counter for how many times a 1 or 0
    # should be displayed in that column
    columns = [0] * WIDTH
    while True:
        #loop over each column:
        for i in range(WIDTH):
            if random.random() < 0.02:
                # Restart a stream counter on this column.
                # the stream length is between 4 and 14 characters long.
                columns[i] = random.randint(4,14)
            #print a character in this column
            if columns[i] == 0:
                # change this ' ' to '.' to see empty spaces:
                print(' ', end='')
            else:
                #print a 0 or 1:
                print(random.choice([0,1]), end='')
                columns[i] -= 1 #Decrement the counter for this column
        print() #print a newline at the end of the row of columns
        time.sleep(0.1)
except KeyboardInterrupt:
    sys.exit()```
cinder summit
#

70 zeroes inside columns

spring latch
#

ok

cinder summit
#

You can see the value for columns in your Pycharm debugger with all of that zeroes

spring latch
#

ok

#

then

cinder summit
#

Next line is a while True

#

Do I need to explain while True for you or is that something you already understood?

spring latch
#

while True run till the code doesnt break or goes flase

#

false

cinder summit
#

yea pretty much

#

OK next line

#

for i in range(WIDTH):

#

Do you understand what this line is doing?

spring latch
#

taking 70 and making it a range

cinder summit
#

alright and what is i doing in that line?

spring latch
#

loops from 0 to 69

cinder summit
spring latch
#

because range(width) is 70

#

i mean

cinder summit
#

correct โœ…

#

i goes through the range of that WIDTH value

#

so since you know that then columns[i] should be going through the loops from 0 to 69 like columns[0], columns[1], etc

#

But!

#

It doesn't reach that line unless it goes through that if condition as True

#

which is a random.random() < 0.02

#

If its not True, then it doesn't go through that line columns[i] = random.randint(4,14)

cinder summit
spring latch
#

let me see the code again

cinder summit
spring latch
#
                #print a 0 or 1:
                print(random.choice([0,1]), end='')
                columns[i] -= 1```
this part is what confusing me
#

no

#

copied from the book

#

ATBS]

cinder summit
#

Sorry I have to go

spring latch
#

does it mean..
if i == no. between 4 and 14... does it mean this else block will run?

spring latch
cinder summit
#

This line doesn't really do anything
print(random.choice([0,1]), end='')

#

It just prints a random 0 or 1

#

And that's it

spring latch
#

ok

cinder summit
#

No impact or effect to the rest of the code

#

This line makes no sense
columns[i] -= 1 #Decrement the counter for this column

#

Sorry but I really have to go

spring latch
#

ok..np...thanks for the help

cinder summit
#

If this help thread is still open, I'll check back later

spring latch
#

i aprreciate your time

lunar pawn
#

heard agent q had to leave, can you catch me up @spring latch ?

spring latch
#

hi,
please!
i am happy to be assisted

#

on this

lunar pawn
#

summarize what you wonder about now

spring latch
#

breakpoint - it is the point where the code stops and i can see the steps at my pace
2. expression or add to watch - helps in checking the variable in detail(i am little confused with this

#

wait

#

i am summarizing

lunar pawn
#

when it comes to debugging, there are a bunch off tools out there

spring latch
#

i didnt get the else:
part

lunar pawn
#

one of them is the pycharm debugger, and it is arguably the best debugger for python.

#

but python also has a debugger built in

#

and you can also debug your code without a debugger

#

so lets take this in these steps

#

lets start with how to debug without using any debugging tools

#

do you know how to do that ?

spring latch
#

logging? printing?

lunar pawn
#

yes

#

printing alone can do this, but there are things that are important to know about when you print out something for debugging purposes

#

have you given that any thoughts?

#

what things or information is important to see?

spring latch
#

i tried to run this

lunar pawn
#

let me simplify that a bit

#
import random

value = None
for i in range(10):
    value = random.randrange(100)
#

now we want to debug valuewhat is one way to do that using print?

spring latch
lunar pawn
#

alright.. lets see

#

!e

import random

value = None
for i in range(10):
    value = random.randrange(100)
    print(value)
hot thicketBOT
lunar pawn
#

what do we know about this value from this print?

spring latch
#

what does value = None represents

lunar pawn
#

nothing of importance

#

it represent a start value

spring latch
#

ok

spring latch
lunar pawn
spring latch
#

it has no upper commas

lunar pawn
lunar pawn
spring latch
#

i get it

#

but idk how do i explain

lunar pawn
#

so when you debug, you dont only want to see the human friendly value

#

you also want to see the name and the type

#

do you know how to get that using print?

spring latch
#

oh you put str data type

#

i didnt see that

spring latch
#

my bad

lunar pawn
#

how can you see the name, type and value using print?

spring latch
#

by writing type

#

in the print

lunar pawn
#

yes, that will for sure work

#

print(value, type(value))

#

there is another way

#

want to see?

spring latch
#

yes

lunar pawn
spring latch
#

oh!

lunar pawn
#

{=} suntax in an fstring

#

now you see the name, value and type

spring latch
#

ok

#

something new

lunar pawn
#

sometimes you want the program to pause at a certain point while debugging. and for that you need a debugger, print just shows you

#

let me change the code a bit

#
import random

value = None
for i in range(10):
    value = random.randrange(100)
    if value > 75:
        # i want a debugger breakpooint here
        print(value)
stable sundial
#

is this a pycharm help post?

lunar pawn
#

using only python, do you know how to set a breakpoint before the print?

spring latch
lunar pawn
#

yeah, and it will enter the python debugger

spring latch
#

or something like importing the pdb and then pdb.trace or something

lunar pawn
lunar pawn
spring latch
#

i have used acronyms instead of full names

#

like p or s

lunar pawn
#

using good names makes debuging simpler

#

the same tool you can activate in pycharm using the gutter and debug run

spring latch
#

ok

lunar pawn
#

so if you do that you will get the same info as python, but in an interface that shows you the stack much better.

#

let me start pycharm and show

spring latch
#

ok

lunar pawn
#

notice that i already see i and value and both are int

spring latch
#

there is no issue is i write value = 0 instead of none right?

lunar pawn
#

no issue no

spring latch
#

ok

#

without any breakpoint?

lunar pawn
#

you can see i have a breakpoint in the editor gutter

#

thats the red dot

spring latch
#

ok

#

and what if i put it somewhere else.
will the results differ?

lunar pawn
#

thats what the continue command does

#

continues to the next breakpoint

#

in pycharm all navigation commands are here

limpid steeple
#

oh thats an interesting topic

spring latch
lunar pawn
#

when you use the python debugger, you have to write commands, like continue jump and so on

#

in the pycharm debugger you use these buttons or their shortcuts

limpid steeple
#

u can also write breakpoint

#

breakpoint is a function that triggers a breakpoint

stable sundial
#

that has already been mentioned

spring latch
#

yes..but please open your thread..

#

to continue this topic

stable sundial
limpid steeple
#

u guys wrote here a lot i ain't gonna read all this...

#

i'll just remain silent then i guess

lunar pawn
#

is this things so far clear?

spring latch
#

yes

stable sundial
spring latch
#

to stop the program there

#

and inspect

stable sundial
spring latch
lunar pawn
#

now that we have gone over three main debugging steps

#

what more do you wonder about?

spring latch
#

wdym..i am interpreting something else

lunar pawn
#

do you have any other questions around debugging?

stable sundial
lunar pawn
#

yes, the docs tell you about all variants

stable sundial
#

memorizing different buttons is harder imo..

lunar pawn
#

!docs pdb

hot thicketBOT
#
pdb

Source code: Lib/pdb.py

The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control.

The debugger is extensible โ€“ it is actually defined as the class Pdb. This is currently undocumented but easily understood by reading the source. The extension interface uses the modules bdb and cmd.

spring latch
#

what does this do..it is unclear to me

lunar pawn
#

c cont and continue

spring latch
#

this is going to be the last topic..

lunar pawn
spring latch
#

1 + 2

lunar pawn
#

yes. and in this context, you can do this

spring latch
#

or any single number

#

can you show me the practical side of it

lunar pawn
stable sundial
#

what editor is that

lunar pawn
spring latch
#

ok, i wont take your more time..
i should go and just write

lunar pawn
#

you can inject expressions into the debugger without having to write it in code and rerunn it

spring latch
#

oh ok

lunar pawn
#

notice i change i from 0 to 9

stable sundial
stable sundial
#

damn:(

#

im pretty good at remembering things tho

#

its only like 3.5k lines.. i can go through over it in like a week

spring latch
#

ok i am closing this

#

!close

hot thicketBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.