Python: Help me find the stupid error

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
chennes
Veteran
Posts: 3877
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Python: Help me find the stupid error

Post by chennes »

I'm making some sort of dumb error in my Python this morning, and I just can't see it. More coffee is probably the solution, but I'm going to throw it out there to see if you find it before I do.

The code in question is:

Code: Select all

# self.replies is created with replies: Dict[int, QtNetwork.QNetworkReply] = {}
# In another function...
self.replies[item.index] = reply
print (type(reply))
for i,r in enumerate(self.replies):
    print (f"Reply {i}: {type(r)}")
The output (for two calls to this function) is:

Code: Select all

10:35:54  <class 'PySide2.QtNetwork.QNetworkReply'>
10:35:54  Reply 0: <class 'int'>
10:35:54  <class 'PySide2.QtNetwork.QNetworkReply'>
10:35:54  Reply 0: <class 'int'>
10:35:54  Reply 1: <class 'int'>
Why is type(r) an int, when type(reply) is a QNetworkReply?
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Python: Help me find the stupid error

Post by onekk »

I'm missing something or the dict creator is dict and not Dict[]

If the Dict[] costruct will create a dictionary, what is the output of:

Code: Select all

for k, v in self.replies.items():
print(k,v)
That is the usual way to retrieve dictionaries key, value pairs in Python 3.x?

If Dict[] is Something else, maybe a different class for iterators, I don't know

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Python: Help me find the stupid error

Post by openBrain »

I'm not totally clear about what "Dict[]" does, but to me it looks like 'r' is the first value of it, so an int.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Python: Help me find the stupid error

Post by onekk »

openBrain wrote: Sat Jan 15, 2022 5:18 pm I'm not totally clear about what "Dict[]" does, but to me it looks like 'r' is the first value of it, so an int.
A little test.

Code: Select all

dict = {"1":"as", "2":"bs", "3":"cs"}

for i,v in enumerate(dict):
    print(i,v)



it returns simply the keys:

Code: Select all

0 1
1 2
2 3
While:

Code: Select all

for k,v in dict.items():
    print(k,v)
will print:

Code: Select all

1 as
2 bs
3 cs
So if it is a dictionary the looping technique is not correct.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
chennes
Veteran
Posts: 3877
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Python: Help me find the stupid error

Post by chennes »

The Dict is a type decorator, it is not important here: think of the line as reading

Code: Select all

self.replies = {}
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
chennes
Veteran
Posts: 3877
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Python: Help me find the stupid error

Post by chennes »

Yes, that’s it, just incorrect iteration here. Thanks!
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Python: Help me find the stupid error

Post by onekk »

chennes wrote: Sat Jan 15, 2022 5:37 pm Yes, that’s it, just incorrect iteration here. Thanks!
definitevely it was lack of coffee. :D

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply