Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread Antoon Pardon




Op 27/01/21 om 05:17 schreef Dan Stromberg:

On Tue, Jan 26, 2021 at 8:13 PM Dan Stromberg  wrote:


On Tue, Jan 26, 2021 at 4:01 PM C W  wrote:


Hello everyone,

I'm a long time Matlab and R user working on data science. How do you
troubleshooting/debugging in Python?


I frequently read tracebacks and think about what's up in the code.

I also often add print functions or logging - empiricism often beats
theorizing when the problems are weird.

And once in a while I will use pudb - it's probably a pretty good fit for
a vim user like me, both being curses-based.  pdb is sad.  There are other
debuggers for Python:
https://wiki.python.org/moin/PythonDebuggingTools


BTW, there's a new tool I haven't tried yet, that sounds pretty
interesting, especially for newcomers: It's called "friendly traceback" and
can be found at https://pypi.org/project/friendly-traceback/

Oh, and of course google search is a terrific tool for deciphering error
messages.

I had a look at that and I think I still would prefer this recipe:
https://code.activestate.com/recipes/52215-get-more-information-from-tracebacks/

--
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread Grant Edwards
On 2021-01-27, C W  wrote:

> My main takeaway from the discussion so far is that: you can't troubleshoot
> Python without some kind of breakpoint or debugger.

How odd. I do it all the time.

--
Grant

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread Alan Gauld via Python-list
On 27/01/2021 23:04, 2qdxy4rzwzuui...@potatochowder.com wrote:

> systems are more painful than others, but yes, some debugging
> environments are more painful than others, too.

Very true! but a good debugger is a godsend. Howevder...

> A well placed call to print (they're not "print statements" anymore!)
> can be much more enlightening and much faster than single stepping
> through code in a debugger,

If you are single stepping more than 2 or 3 lines then you
aren't using the debugger properly!

Use breakpoints(selected by brain power!) and watches. The watches
are like print statements but don;t modify the source code
(nothing to remove later!)

Anytime I find myself hitting the step button more that a few
times I know I'm not using the debugger effectively and rethink.

>  and seeing the output from the same print
> statement inside a loop can be much better than manually examining
> variables iteration after iteration and trying to remember what the
> value was before.

So don't do that, use the debugger. Most of them have conditional
breakpoints that will stop only when you need to.

> The best debugging tool, however, remains your brain.  

Amen to that, regardless of any software tool used.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread Alan Gauld via Python-list
On 27/01/2021 18:42, C W wrote:

> I'd like to know how the experts on here are approaching and debugging
> this.
> 
> Bonus if no debugger or breakpoint. Just the good ol' run the function and
> evaluate/print output for problems.

One option you may like and nobody seems to have mentioned
yet is to use the interactive prompt.

Simply run the program with the -i flag and the program will stay in the
interpreter at the >>> prompt. Similar to what you seem to be used to
doing in R...

>From there you can print the current value of variables, inspect objects
etc. I find it useful sometimes when I'm not on my desktop PC, although
usually I jump into winpdb or similar if I need that kind of support.

dubuggers and breakpoints combined with watchpoints are very powerful
tools and easy to learn. (Although I may be pre-disposed since my first
job after uni' was to white-box test 500K lines of C using a VAX command
line debugger in batch mode. I wrote over 100K lines of debugger commands!)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread Michael Torrie
On 1/27/21 11:42 AM, C W wrote:
> For learning purposes, here's the files:
> https://www.dropbox.com/sh/a3iy40rcvib4uvj/AAADmlM2i6NquWC1SV0nZfnDa?dl=0
> 
> Yes, you are correct about "employee" and "person" discrepancies. For now,
> the list comprehension is where I get stuck.
> 
> I'd like to know how the experts on here are approaching and debugging
> this.
> 
> Bonus if no debugger or breakpoint. Just the good ol' run the function and
> evaluate/print output for problems.

Like I said, the key is in the traceback.  It told you exactly what the
problem was. And seeing your full code I can now tell you why.  There is
no "created_at" field in the person dict (I noticed it's now called neo
in the code you just posted).  The dict is created directly from JSON
and there is no "created_at field anywhere in the JSON, which is why
you're getting that error.

I don't really understand much of the code you posted. There doesn't
seem to be any link between the MySQL database and the NEODatabase class
and instances.  Something is missing and it looks vaguely java-esque,
which may not be your best way to work in Python. I'm not sure what
you're trying to do so I can't really say much about it.

If you want to play with a database abstraction, there are several
libraries available, including SQLchemy.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread J. Pic
Also

- https://github.com/cool-RR/pysnooper
- https://github.com/andy-landy/traceback_with_variables
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread duncan smith
On 27/01/2021 22:41, C W wrote:
> Great tutorial Irv, very simple with if-else example, gets the point
> across.
> 
> My main takeaway from the discussion so far is that: you can't troubleshoot
> Python without some kind of breakpoint or debugger.
> 

[snip]

Really?

Duncan
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread 2QdxY4RzWzUUiLuE
On 2021-01-27 at 17:41:52 -0500,
C W  wrote:

> Great tutorial Irv, very simple with if-else example, gets the point
> across.

Disclaimer:  I did not watch the video.

> My main takeaway from the discussion so far is that: you can't
> troubleshoot Python without some kind of breakpoint or debugger.

I disagree.  :-)

Having spent a long time developing and debugging embedded systems (some
soft- or hard- real time), I believe that you can troubleshoot programs
(regardless of source language) without debuggers.  If a system can
generate output, then I (the programmer) can change that output, observe
the results, and figure out what's working and what's not.  Yes, some
systems are more painful than others, but yes, some debugging
environments are more painful than others, too.

A well placed call to print (they're not "print statements" anymore!)
can be much more enlightening and much faster than single stepping
through code in a debugger, and seeing the output from the same print
statement inside a loop can be much better than manually examining
variables iteration after iteration and trying to remember what the
value was before.

The best debugging tool, however, remains your brain.  Way before I add
a call to print or you set up your debugger, thinking about what went
wrong and where to look can solve the problems without resorting to
external tools.  :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread C W
Great tutorial Irv, very simple with if-else example, gets the point
across.

My main takeaway from the discussion so far is that: you can't troubleshoot
Python without some kind of breakpoint or debugger.

I suppose I can't take the functional programming debugger style like C,
Matlab, or R, and apply it to a OOP language like Python.

On Wed, Jan 27, 2021 at 5:26 PM Irv Kalb  wrote:

> On Jan 26, 2021, at 5:28 PM, William Ray Wing via Python-list <
> python-list@python.org> wrote:
> >
> >
> >
> >> On Jan 26, 2021, at 2:00 PM, C W  wrote:
> >>
> >> Hello everyone,
> >>
> >> I'm a long time Matlab and R user working on data science. How do you
> >> troubleshooting/debugging in Python?
> >>
> >
> > Another approach is to run the code in an IDE.  I happen to use Wing,
> but that is a coincidence.  But almost ANY IDE will let you set a break
> point, then single-step through your code starting at the break point and
> examine the values of your variables at each step.  Sometimes this is an
> awfully big hammer for what is a head-slapping mistake.  But it has never
> failed me.
> >
> >
>
> I'm happy with the PyCharm IDE.  I created a video showing how to use the
> debugger in that environment.  It's available on YouTube here:
>
> https://www.youtube.com/watch?v=cxAOSQQwDJ4  >
>
> Irv
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread Irv Kalb
On Jan 26, 2021, at 5:28 PM, William Ray Wing via Python-list 
 wrote:
> 
> 
> 
>> On Jan 26, 2021, at 2:00 PM, C W  wrote:
>> 
>> Hello everyone,
>> 
>> I'm a long time Matlab and R user working on data science. How do you
>> troubleshooting/debugging in Python?
>> 
> 
> Another approach is to run the code in an IDE.  I happen to use Wing, but 
> that is a coincidence.  But almost ANY IDE will let you set a break point, 
> then single-step through your code starting at the break point and examine 
> the values of your variables at each step.  Sometimes this is an awfully big 
> hammer for what is a head-slapping mistake.  But it has never failed me.
> 
> 

I'm happy with the PyCharm IDE.  I created a video showing how to use the 
debugger in that environment.  It's available on YouTube here:

https://www.youtube.com/watch?v=cxAOSQQwDJ4 

Irv

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread Dietmar Schwertberger

On 27.01.2021 01:52, Skip Montanaro wrote:

Agree with Grant on these points. I certainly use gdb to debug C code
(like the interpreter), but for Python code, tracebacks and print
statements pretty much take care of things.

I thought so for the first 12 year of using Python.
For the last 12 years I have been using an IDE/debugger with built-in 
console.


It boosted productivity by at least a factor of two.


Sometimes "python -i ..." and "import pdb" is good enough, being able to 
inspect variables after an exception.

I have the impression that pdb as post mortem debugger is not well known...

Regards,

Dietmar


--
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread C W
For learning purposes, here's the files:
https://www.dropbox.com/sh/a3iy40rcvib4uvj/AAADmlM2i6NquWC1SV0nZfnDa?dl=0

Yes, you are correct about "employee" and "person" discrepancies. For now,
the list comprehension is where I get stuck.

I'd like to know how the experts on here are approaching and debugging
this.

Bonus if no debugger or breakpoint. Just the good ol' run the function and
evaluate/print output for problems.

Thanks so much,

Mike

On Wed, Jan 27, 2021 at 10:53 AM Michael Torrie  wrote:

> On 1/26/21 10:19 PM, C W wrote:
> > Traceback (most recent call last):
> >File "/Users/Mike/Documents/Mike/main.py", line 95, in 
> >   main()
> >File "/Users/Mike/Documents/Mike/main.py", line 86, in main
> >   args = get_feed()
> >File "/Users/Mike/DocumentsMike/main.py", line 32, in get_feed
> >   result = [PERSONatabase.get_person(raw_person) for raw_neo in
> > raw_objects]
> >File "/Users/Mike/Documents/Mike/main.py", line 32, in 
> >   result = [NEODatabase.get_person(raw_person) for raw_neo in
> > raw_objects]
> >File "/Users/Mike/Documents/Mike/database.py", line 24, in get_person
> >   return PERSONDatabase(person['created_at'],
> > KeyError: 'created_at'
>
> The actual error is the last part, which is a KeyError on line 24.  A
> key error usually is from a dictionary-like object and it means the
> requested key is not found in that object.  In other words, this person
> object has no "created_at" key.  Hope that makes sense.
>
> I do not know why the code you posted refers to "employee" but the
> traceback refers to "person."
>
> In any case the trace back shows you what called what until the error
> occurred. You can trace the execution of the code simply by following
> it.  main() called get_feed() which set up a list comprehension, which
> calls get_person() which is where the error is occurring.  I'm not
> following the list comprehension stuff; I don't know why python is first
> referring to PERSONatabase and then refers to NEODatabase.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread C W
Hi Cameron,
Yes, you are correct in all above. There's a mistake in my copy paste.
Thanks for pointing that out!

On Wed, Jan 27, 2021 at 12:58 AM Cameron Simpson  wrote:

> On 27Jan2021 00:19, C W  wrote:
> >Here's the code again, class should be called PERSONDatabase,
> >misspelled
> >earlier:
> >class PERSONDatabase:
> >   def __init__(self, id, created_at, name, attend_date, distance):
> >  self._id = id
> >  self.created_at = created_at
> >  self.name= name
> >  self.attend_date = attend_date
> >  self.distance = distance
>
> Here's you're setting attributes (which is a very normal thing to do).
>
> >   @classmethod
> >   def get_person(self, employee):
> >  return PERSONDatabase(employee['created_at'],
> >employee['id'],
> >employee['name'],
> >employee['attend_date'],
> >employee['distance'])
>
> I think this "employee" is called "person" in the code the traceback
> came from. It is better when these two things match.
>
> >The PERSONDatabase class is called from main. This is the trace back I got
> >from the VS code:
> >
> >Traceback (most recent call last):
> >   File "/Users/Mike/Documents/Mike/main.py", line 95, in 
> >  main()
> >   File "/Users/Mike/Documents/Mike/main.py", line 86, in main
> >  args = get_feed()
> >   File "/Users/Mike/DocumentsMike/main.py", line 32, in get_feed
> >  result = [PERSONatabase.get_person(raw_person) for raw_neo in
> >raw_objects]
> >   File "/Users/Mike/Documents/Mike/main.py", line 32, in 
> >  result = [NEODatabase.get_person(raw_person) for raw_neo in
> >raw_objects]
> >   File "/Users/Mike/Documents/Mike/database.py", line 24, in get_person
> >  return PERSONDatabase(person['created_at'],
> >KeyError: 'created_at'
>
> Here's you're trying to index another object using a string, which seems
> to resemble the .created_at attribute in your PERSONDatabase object.
>
> I would presume from this that the "person" object at the bottom of the
> traceback is the "raw_person" called above it. But I do not see
> raw_person defined anywhere. Are you sure you didn't mean to pass
> "raw_neo" instead of "raw_person"? That would be more normal, since
> you're iterating over "raw_objects".
>
> Cheers,
> Cameron Simpson 
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread Michael Torrie
On 1/26/21 10:19 PM, C W wrote:
> Traceback (most recent call last):
>File "/Users/Mike/Documents/Mike/main.py", line 95, in 
>   main()
>File "/Users/Mike/Documents/Mike/main.py", line 86, in main
>   args = get_feed()
>File "/Users/Mike/DocumentsMike/main.py", line 32, in get_feed
>   result = [PERSONatabase.get_person(raw_person) for raw_neo in
> raw_objects]
>File "/Users/Mike/Documents/Mike/main.py", line 32, in 
>   result = [NEODatabase.get_person(raw_person) for raw_neo in
> raw_objects]
>File "/Users/Mike/Documents/Mike/database.py", line 24, in get_person
>   return PERSONDatabase(person['created_at'],
> KeyError: 'created_at'

The actual error is the last part, which is a KeyError on line 24.  A
key error usually is from a dictionary-like object and it means the
requested key is not found in that object.  In other words, this person
object has no "created_at" key.  Hope that makes sense.

I do not know why the code you posted refers to "employee" but the
traceback refers to "person."

In any case the trace back shows you what called what until the error
occurred. You can trace the execution of the code simply by following
it.  main() called get_feed() which set up a list comprehension, which
calls get_person() which is where the error is occurring.  I'm not
following the list comprehension stuff; I don't know why python is first
referring to PERSONatabase and then refers to NEODatabase.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread David Raymond
In regards to the various comments about adding in print() calls what I've 
found myself doing is to basically always use the logging module, and use 
logging.debug() for those.

Somewhere at the top of the script I'll have a line like...

DEBUG = False

...and when initializing the handler to stdout I'll do something like this...

toScreen = logging.StreamHandler(sys.stdout)
toScreen.setLevel(logging.DEBUG if DEBUG else logging.INFO)


That way I can sprinkle in

logging.debug("Some message here")

in various spots. If things are going wrong I can change DEBUG to True to see 
them on screen, and when I've fixed it I can just set DEBUG back to False. That 
way I don't have to try finding all those print() statements that were only 
there for debugging and comment them out or remove them.

As a bonus, if you also set a file logger for example with its level set to 
logging.DEBUG, then you can have those go into the log file without them 
cluttering the screen output.

As a side effect to using logging I found I also like having the timestamp 
automatically prepended to my output by the logger. I'd find myself checking in 
on something I left running in the background and thinking it's been on that 
step for "a while", but I have no idea how long "a while" really is. So the 
timestamps help quickly show "been stuck there for 3 hours" vs "just got to 
that step 3 seconds before I switched to its window"

I don't claim these are the best practices :) But figured I'd offer another 
take on it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread Chris Angelico
On Wed, Jan 27, 2021 at 8:21 PM Chris Green  wrote:
>
> Skip Montanaro  wrote:
> > CW> How do you troubleshooting/debugging in Python?
> >
> > GE> Mostly I read exception trace and read the code and think about it.
> > GE> If that doesn't work, I add some print() or syslog() calls.
> >
> > CW> I know hardcore computer scientists would tell me about Python debugger.
> >
> > GE> I've been writing Python for 20+ years. I've never tried a debugger.
> >
> > Agree with Grant on these points. I certainly use gdb to debug C code
> > (like the interpreter), but for Python code, tracebacks and print
> > statements pretty much take care of things. I know pdb has been around
> > for a long while and many people like IDEs, but I've not generally
> > found them all that useful. I'm stuck in the 90s I guess.
> >
> Me too! :-)
>

Me three :)

I'm of the opinion that everyone should get some experience with the
most fundamental form of debugging: trigger the problem with logging
active. Sometimes that means reproducing a known problem, other times
it means predicting an unknown problem and just always having some
useful logging. (That's why your boot sequence inevitably creates a
bunch of logs of various forms.)

IIDPIO: If In Doubt, Print It Out... just keep that console noisy
until you're certain the information won't help you.

Some problems are easy to reproduce. You figure out how to make it
happen, you adjust your code, you tinker. Sometimes the tinkering will
involve a debug harness (like pdb), other times it just requires a few
more print() calls, and either way, great! But other problems aren't.
Try debugging a weird issue with your web site that shows up only once
every two weeks (or worse), and only ever occurs once. You can't run
your web server in a debug harness because the problem simply doesn't
happen on your local computer. Your only option is to try to log more
stuff and wait for the problem to happen again...

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread Chris Green
Skip Montanaro  wrote:
> CW> How do you troubleshooting/debugging in Python?
> 
> GE> Mostly I read exception trace and read the code and think about it.
> GE> If that doesn't work, I add some print() or syslog() calls.
> 
> CW> I know hardcore computer scientists would tell me about Python debugger.
> 
> GE> I've been writing Python for 20+ years. I've never tried a debugger.
> 
> Agree with Grant on these points. I certainly use gdb to debug C code
> (like the interpreter), but for Python code, tracebacks and print
> statements pretty much take care of things. I know pdb has been around
> for a long while and many people like IDEs, but I've not generally
> found them all that useful. I'm stuck in the 90s I guess.
> 
Me too! :-)

I tend to use multiple terminal windows so I have the code being
edited in one window, run the code in another window and sometimes a
third window for looking at configuration etc.

Add print statement etc. for debugging.

-- 
Chris Green
ยท
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread Cameron Simpson
On 27Jan2021 00:19, C W  wrote:
>Here's the code again, class should be called PERSONDatabase, 
>misspelled
>earlier:
>class PERSONDatabase:
>   def __init__(self, id, created_at, name, attend_date, distance):
>  self._id = id
>  self.created_at = created_at
>  self.name= name
>  self.attend_date = attend_date
>  self.distance = distance

Here's you're setting attributes (which is a very normal thing to do).

>   @classmethod
>   def get_person(self, employee):
>  return PERSONDatabase(employee['created_at'],
>employee['id'],
>employee['name'],
>employee['attend_date'],
>employee['distance'])

I think this "employee" is called "person" in the code the traceback 
came from. It is better when these two things match.

>The PERSONDatabase class is called from main. This is the trace back I got
>from the VS code:
>
>Traceback (most recent call last):
>   File "/Users/Mike/Documents/Mike/main.py", line 95, in 
>  main()
>   File "/Users/Mike/Documents/Mike/main.py", line 86, in main
>  args = get_feed()
>   File "/Users/Mike/DocumentsMike/main.py", line 32, in get_feed
>  result = [PERSONatabase.get_person(raw_person) for raw_neo in
>raw_objects]
>   File "/Users/Mike/Documents/Mike/main.py", line 32, in 
>  result = [NEODatabase.get_person(raw_person) for raw_neo in
>raw_objects]
>   File "/Users/Mike/Documents/Mike/database.py", line 24, in get_person
>  return PERSONDatabase(person['created_at'],
>KeyError: 'created_at'

Here's you're trying to index another object using a string, which seems 
to resemble the .created_at attribute in your PERSONDatabase object.

I would presume from this that the "person" object at the bottom of the 
traceback is the "raw_person" called above it. But I do not see 
raw_person defined anywhere. Are you sure you didn't mean to pass 
"raw_neo" instead of "raw_person"? That would be more normal, since 
you're iterating over "raw_objects".

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread joseph pareti
To debug python code I use spyder from the anaconda distribution

Am Mittwoch, 27. Januar 2021 schrieb C W :

> Hi Michael,
> Here's the code again, class should be called PERSONDatabase, misspelled
> earlier:
> class PERSONDatabase:
>def __init__(self, id, created_at, name, attend_date, distance):
>   self._id = id
>   self.created_at = created_at
>   self.name= name
>   self.attend_date = attend_date
>   self.distance = distance
>
>@classmethod
>def get_person(self, employee):
>   return PERSONDatabase(employee['created_at'],
> employee['id'],
> employee['name'],
> employee['attend_date'],
> employee['distance'])
>
> The PERSONDatabase class is called from main. This is the trace back I got
> from the VS code:
>
> Traceback (most recent call last):
>File "/Users/Mike/Documents/Mike/main.py", line 95, in 
>   main()
>File "/Users/Mike/Documents/Mike/main.py", line 86, in main
>   args = get_feed()
>File "/Users/Mike/DocumentsMike/main.py", line 32, in get_feed
>   result = [PERSONatabase.get_person(raw_person) for raw_neo in
> raw_objects]
>File "/Users/Mike/Documents/Mike/main.py", line 32, in 
>   result = [NEODatabase.get_person(raw_person) for raw_neo in
> raw_objects]
>File "/Users/Mike/Documents/Mike/database.py", line 24, in get_person
>   return PERSONDatabase(person['created_at'],
> KeyError: 'created_at'
>
> Thank you very much!
>
> On Wed, Jan 27, 2021 at 12:10 AM Michael Torrie  wrote:
>
> > On 1/26/21 8:37 PM, C W wrote:
> > > I have a naive question. How do I use traceback or trace the stack? In
> > > particular, I'm using VS Code with Python interactive console.
> >
> > Show us the traceback here and we can help you interpret it.  Copy and
> > paste it from the VS Code console.
> >
> > > Say, I want to print the value of employee['name']. Can I do it?
> >
> > Yes I would think so.
> >
> > > My understanding is that these classes are just "skeletons". I must
> > > create an instance, assign values, then test?
> >
> > Can't you just do something like this?
> >
> > class NEODatabase:
> > def __init__(self, id, created_at, name, attend_date, distance):
> > self._id = id
> > self.created_at = created_at
> > self.name = name
> > self.attend_date = attend_date
> > self.distance = distance
> >
> > @classmethod
> > def get_person(self, employee):
> > print (employee['name'])
> >
> > return PERSONDatabase(employee['created_at'],
> >   employee['id'],
> >   employee['name'],
> >   employee['attend_date'],
> >   employee['distance'])
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 
Regards,
Joseph Pareti - Artificial Intelligence consultant
Joseph Pareti's AI Consulting Services
https://www.joepareti54-ai.com/
cell +49 1520 1600 209
cell +39 339 797 0644
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread C W
Hi Michael,
Here's the code again, class should be called PERSONDatabase, misspelled
earlier:
class PERSONDatabase:
   def __init__(self, id, created_at, name, attend_date, distance):
  self._id = id
  self.created_at = created_at
  self.name= name
  self.attend_date = attend_date
  self.distance = distance

   @classmethod
   def get_person(self, employee):
  return PERSONDatabase(employee['created_at'],
employee['id'],
employee['name'],
employee['attend_date'],
employee['distance'])

The PERSONDatabase class is called from main. This is the trace back I got
from the VS code:

Traceback (most recent call last):
   File "/Users/Mike/Documents/Mike/main.py", line 95, in 
  main()
   File "/Users/Mike/Documents/Mike/main.py", line 86, in main
  args = get_feed()
   File "/Users/Mike/DocumentsMike/main.py", line 32, in get_feed
  result = [PERSONatabase.get_person(raw_person) for raw_neo in
raw_objects]
   File "/Users/Mike/Documents/Mike/main.py", line 32, in 
  result = [NEODatabase.get_person(raw_person) for raw_neo in
raw_objects]
   File "/Users/Mike/Documents/Mike/database.py", line 24, in get_person
  return PERSONDatabase(person['created_at'],
KeyError: 'created_at'

Thank you very much!

On Wed, Jan 27, 2021 at 12:10 AM Michael Torrie  wrote:

> On 1/26/21 8:37 PM, C W wrote:
> > I have a naive question. How do I use traceback or trace the stack? In
> > particular, I'm using VS Code with Python interactive console.
>
> Show us the traceback here and we can help you interpret it.  Copy and
> paste it from the VS Code console.
>
> > Say, I want to print the value of employee['name']. Can I do it?
>
> Yes I would think so.
>
> > My understanding is that these classes are just "skeletons". I must
> > create an instance, assign values, then test?
>
> Can't you just do something like this?
>
> class NEODatabase:
> def __init__(self, id, created_at, name, attend_date, distance):
> self._id = id
> self.created_at = created_at
> self.name = name
> self.attend_date = attend_date
> self.distance = distance
>
> @classmethod
> def get_person(self, employee):
> print (employee['name'])
>
> return PERSONDatabase(employee['created_at'],
>   employee['id'],
>   employee['name'],
>   employee['attend_date'],
>   employee['distance'])
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread Michael Torrie
On 1/26/21 8:30 PM, Grant Edwards wrote:
> Me too (MS in CSci), but I can't remember the last time I used a
> debugger. 

I use a debugger frequency in C++, and sometimes C.  Even running a
debugger on an attached device like an Arduino is sometimes very useful.
Good debuggers let you do things like conditional breakpoints and watch
expressions.  Helpful for tracking down hard-to-find and less frequent
conditions.  Sure you could print stuff out, which I do, but sometimes
it's more convenient to do it with the debugger.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread Michael Torrie
On 1/26/21 8:37 PM, C W wrote:
> I have a naive question. How do I use traceback or trace the stack? In
> particular, I'm using VS Code with Python interactive console.

Show us the traceback here and we can help you interpret it.  Copy and
paste it from the VS Code console.

> Say, I want to print the value of employee['name']. Can I do it? 

Yes I would think so.

> My understanding is that these classes are just "skeletons". I must
> create an instance, assign values, then test?

Can't you just do something like this?

class NEODatabase:
def __init__(self, id, created_at, name, attend_date, distance):
self._id = id
self.created_at = created_at
self.name = name
self.attend_date = attend_date
self.distance = distance

@classmethod
def get_person(self, employee):
print (employee['name'])

return PERSONDatabase(employee['created_at'],
  employee['id'],
  employee['name'],
  employee['attend_date'],
  employee['distance'])

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread Dan Stromberg
On Tue, Jan 26, 2021 at 8:13 PM Dan Stromberg  wrote:

>
> On Tue, Jan 26, 2021 at 4:01 PM C W  wrote:
>
>> Hello everyone,
>>
>> I'm a long time Matlab and R user working on data science. How do you
>> troubleshooting/debugging in Python?
>>
>
> I frequently read tracebacks and think about what's up in the code.
>
> I also often add print functions or logging - empiricism often beats
> theorizing when the problems are weird.
>
> And once in a while I will use pudb - it's probably a pretty good fit for
> a vim user like me, both being curses-based.  pdb is sad.  There are other
> debuggers for Python:
> https://wiki.python.org/moin/PythonDebuggingTools
>

BTW, there's a new tool I haven't tried yet, that sounds pretty
interesting, especially for newcomers: It's called "friendly traceback" and
can be found at https://pypi.org/project/friendly-traceback/

Oh, and of course google search is a terrific tool for deciphering error
messages.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread Dan Stromberg
On Tue, Jan 26, 2021 at 4:01 PM C W  wrote:

> Hello everyone,
>
> I'm a long time Matlab and R user working on data science. How do you
> troubleshooting/debugging in Python?
>

I frequently read tracebacks and think about what's up in the code.

I also often add print functions or logging - empiricism often beats
theorizing when the problems are weird.

And once in a while I will use pudb - it's probably a pretty good fit for a
vim user like me, both being curses-based.  pdb is sad.  There are other
debuggers for Python:
https://wiki.python.org/moin/PythonDebuggingTools
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread Grant Edwards
On 2021-01-27, Cameron Simpson  wrote:

> Me either. Like grant, i fall into the "look at the stack trace and 
> think a bit, then put in print() calls or similar to show me whether 
> things are what I expect them to be when the code runs".
>
> I actually have a Computer Science degree, but I think there are 
> definition those who like debuggers and those who tend not to.

Me too (MS in CSci), but I can't remember the last time I used a
debugger. It may help that I "grow" my apps. I don't sit down, write a
3000 line program, and then try to debug the program. That just
doesn't work. It doesn't matter how much UML you can point to if a
couple of your basic assumptions turned out to be wrong.

Write small funtions, and add small chunks of code, and test as you go
to make sure things work the way you thought they were going to. If it
used to work, you've only added 5 lines of code, and now it doesn't
work, it's usually not hard to figure out what's wrong.

And don't be afraid to refactor stuff when your original approach
starts to smell -- if you just keep going it will only get worse.

Source control can also be a lifesaver if you get really confused.

--
Grant



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread C W
Thanks for your replies. My apologies for the poor indent. I'm rewriting
the code below.

class NEODatabase:
def __init__(self, id, created_at, name, attend_date, distance):
self._id = id
self.created_at = created_at
self.name = name
self.attend_date = attend_date
self.distance = distance

@classmethod
def get_person(self, employee):
return PERSONDatabase(employee['created_at'],
employee['id'],
employee['name'],
employee['attend_date'],
employee['distance'])

I have a naive question. How do I use traceback or trace the stack? In
particular, I'm using VS Code with Python interactive console.

Say, I want to print the value of employee['name']. Can I do it? My
understanding is that these classes are just "skeletons". I must create an
instance, assign values, then test?

Thanks so much,

Mike

On Tue, Jan 26, 2021 at 9:55 PM Ed Leafe  wrote:

> On Jan 26, 2021, at 18:16, Grant Edwards 
> wrote:
> >
> >> How do you troubleshooting/debugging in Python?
> >
> > Mostly I read exception trace and read the code and think about it.
> >
> > If that doesn't work, I add some print() or syslog() calls.
> >
> > If I really get stuck, I try to write as small a program as possible
> > that demonstrates the problem.
>
> I do the first two, but if I get really stuck, I use the pudb debugger (
> https://pypi.org/project/pudb/).
>
> Using that, I can see all the locals, jump to any point in the stack and
> see the locals there, or shell into ipython if I need to run some quick
> code. For me, this is much faster than trying to write an additional
> program that is close enough to the problem code to be useful.
>
> -- Ed Leafe
>
>
>
>
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread Ed Leafe
On Jan 26, 2021, at 18:16, Grant Edwards  wrote:
> 
>> How do you troubleshooting/debugging in Python?
> 
> Mostly I read exception trace and read the code and think about it.
> 
> If that doesn't work, I add some print() or syslog() calls.
> 
> If I really get stuck, I try to write as small a program as possible
> that demonstrates the problem.

I do the first two, but if I get really stuck, I use the pudb debugger 
(https://pypi.org/project/pudb/).

Using that, I can see all the locals, jump to any point in the stack and see 
the locals there, or shell into ipython if I need to run some quick code. For 
me, this is much faster than trying to write an additional program that is 
close enough to the problem code to be useful.

-- Ed Leafe






-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread William Ray Wing via Python-list



> On Jan 26, 2021, at 2:00 PM, C W  wrote:
> 
> Hello everyone,
> 
> I'm a long time Matlab and R user working on data science. How do you
> troubleshooting/debugging in Python?
> 

Another approach is to run the code in an IDE.  I happen to use Wing, but that 
is a coincidence.  But almost ANY IDE will let you set a break point, then 
single-step through your code starting at the break point and examine the 
values of your variables at each step.  Sometimes this is an awfully big hammer 
for what is a head-slapping mistake.  But it has never failed me.

Good luck,
Bill

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread Cameron Simpson
On 26Jan2021 14:00, C W  wrote:
>I'm a long time Matlab and R user working on data science. How do you
>troubleshooting/debugging in Python?
>
>I ran into this impossible situation to debug:
>class person:
>def __init__(self, id, created_at, name, attend_date, distance):
>"""Create a new `person`.
>"""

Your mailer has removed all the indenting. See if it has a mode for 
code.  Often just making sure all the code is itself indented will do, 
eg:

some indented
code here

>I got an error message saying id was 'str', but expecting 'int'.

There should have been a complete traceback with that error, showing 
which line triggered the exception and how it was called.

Here, we don't know from your description what happened or where.

>If this error was in R, I would have tried:
>> self._id = 123

Well, provided "self" already existed, that would work in Python but 
tell you almost nothing, because any variable may reference a value of 
any type. (Python variables are not typed, but the values _are_.)

>But, I can't do that here! What do I do? Do I HAVE TO instantiate an 
>object
>first?

Well, "self" is the instantiaited object. The __init__ function does 
more setup (completing what you might call "normal" instantiation).

>It's not  convenient if I have 10 of these objects around. I need to
>instantiate 10 objects.

Your environment isn't clear to me.

>I know hardcore computer scientists would tell me about Python debugger. R
>also has one, but nobody ever uses it. I don't find them user-friendly!

Me either. Like grant, i fall into the "look at the stack trace and 
think a bit, then put in print() calls or similar to show me whether 
things are what I expect them to be when the code runs".

I actually have a Computer Science degree, but I think there are 
definition those who like debuggers and those who tend not to. I'm in 
the latter camp. Preferences like that affact how you _choose_ to debug.

Usually I have the code in a file in one window and a command line in 
another, and go:

python my_code_file.py ...

to test when debugging.

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread Skip Montanaro
CW> How do you troubleshooting/debugging in Python?

GE> Mostly I read exception trace and read the code and think about it.
GE> If that doesn't work, I add some print() or syslog() calls.

CW> I know hardcore computer scientists would tell me about Python debugger.

GE> I've been writing Python for 20+ years. I've never tried a debugger.

Agree with Grant on these points. I certainly use gdb to debug C code
(like the interpreter), but for Python code, tracebacks and print
statements pretty much take care of things. I know pdb has been around
for a long while and many people like IDEs, but I've not generally
found them all that useful. I'm stuck in the 90s I guess.

Skip
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread Grant Edwards
On 2021-01-26, C W  wrote:

> How do you troubleshooting/debugging in Python?

Mostly I read exception trace and read the code and think about it.

If that doesn't work, I add some print() or syslog() calls.

If I really get stuck, I try to write as small a program as possible
that demonstrates the problem.

> I know hardcore computer scientists would tell me about Python debugger.

I've been writing Python for 20+ years. I've never tried a debugger.

--
Grant

-- 
https://mail.python.org/mailman/listinfo/python-list


How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-26 Thread C W
Hello everyone,

I'm a long time Matlab and R user working on data science. How do you
troubleshooting/debugging in Python?

I ran into this impossible situation to debug:
class person:
def __init__(self, id, created_at, name, attend_date, distance):
"""Create a new `person`.
"""
self._id = id
self.created_at = created_at
self.name = name
self.attend_date = attend_date
self.distance = distance

@classmethod
def get_person(self, employee):
"""Find and return a person by.
"""
return person(employee['created_at'],
employee['id'],
employee['name'],
employee['attend_date'],
employee['distance']
)

I got an error message saying id was 'str', but expecting 'int'.

In R, I use the interactive IDE with console. Wherever the error interrupts
the code, I just evaluate that line in the console. Very convenient!

If this error was in R, I would have tried:
> self._id = 123

But, I can't do that here! What do I do? Do I HAVE TO instantiate an object
first? It's not  convenient if I have 10 of these objects around. I need to
instantiate 10 objects.

I know hardcore computer scientists would tell me about Python debugger. R
also has one, but nobody ever uses it. I don't find them user-friendly!

Thanks a lot,

Mike
-- 
https://mail.python.org/mailman/listinfo/python-list