[Tutor] dictionaries are same but returning false

Marc Tompkins marc.tompkins at gmail.com
Wed Jul 5 13:09:45 EDT 2017


On Wed, Jul 5, 2017 at 9:51 AM, Ashfaq <quazi.ashfaq at gmail.com> wrote:

> Hi Peter,
> The way you find the issue is really cool! Very cool! :)
>
>
I agree - that is very cool.  But I have also made this sort of mistake a
few times, and found it by using a very quick, low-tech method...
"Unfold" the lines of the two dictionary specifications and put them one
above the other - I usually just cut'n'paste into a text editor and delete
the newlines.  If the two unfolded lines don't line up (which these
wouldn't, because one has extra quotes in it) the error will be immediately
visible.  Subtler typos will be harder to spot, but still much easier than
trying to compare two folded paragraphs several lines apart.

Not nearly as sophisticated, but sometimes quick'n'dirty is best.


> On Wed, Jul 5, 2017 at 6:10 PM, shubham goyal <skgoyal721 at gmail.com>
> wrote:
>
> > Thank you Peter.
> > Silly mistakes 😀
> >
> > On Jul 5, 2017 5:10 PM, "Peter Otten" <__peter__ at web.de> wrote:
> >
> > > shubham goyal wrote:
> > >
> > > > null=None
> > > > x={'_udp_options': None, '_icmp_options': None, 'attribute_map':
> > > > {'icmp_options': 'icmpOptions', 'protocol': 'protocol', 'source':
> > > > {'source',
> > > > 'tcp_options': 'tcpOptions', 'is_stateless': 'isStateless',
> > > 'udp_options':
> > > > 'udpOptions'}, '_is_stateless': False, 'swagger_types':
> > {'icmp_options':
> > > > 'IcmpOptions', 'protocol': 'str', 'source': 'str', 'tcp_options':
> > > > 'TcpOptions', 'is_stateless': 'bool', 'udp_options': 'UdpOptions'},
> > > > '_protocol': '6', '_source': '0.0.4.0/24', '_tcp_options': {
> > > >   "destination_port_range": {
> > > >     "max": "22",
> > > >     "min": "22"
> > > >   },
> > > >   "source_port_range": null
> > > > }}
> > > >
> > > > y={'_udp_options': None, '_icmp_options': None, 'attribute_map':
> > > > {'icmp_options': 'icmpOptions', 'protocol': 'protocol', 'source':
> > > > {'source',
> > > > 'tcp_options': 'tcpOptions', 'is_stateless': 'isStateless',
> > > 'udp_options':
> > > > 'udpOptions'}, '_is_stateless': False, 'swagger_types':
> > {'icmp_options':
> > > > 'IcmpOptions', 'protocol': 'str', 'source': 'str', 'tcp_options':
> > > > 'TcpOptions', 'is_stateless': 'bool', 'udp_options': 'UdpOptions'},
> > > > '_protocol': '6', '_source': '0.0.4.0/24', '_tcp_options': {
> > > >   "destination_port_range": {
> > > >     "max": 22,
> > > >     "min": 22
> > > >   },
> > > >   "source_port_range": null
> > > > }}
> > > > if x==y:
> > > >     print "true"
> > > > else:
> > > >     print "false"
> > > >
> > > >
> > > > These dictionaries are same exactly. but its returning false. i don't
> > > > understand
> > > > what to do?
> > >
> > > Let's narrow down the problem, with the help of the interactive
> > > interpreter:
> > >
> > > >>> changed = [k for k in x if x[k] != y[k]]
> > > >>> changed
> > > ['_tcp_options']
> > > >>> k, = changed
> > >
> > > A closer look:
> > >
> > > >>> x[k]
> > > {'source_port_range': None, 'destination_port_range': {'max': '22',
> > 'min':
> > > '22'}}
> > > >>> y[k]
> > > {'source_port_range': None, 'destination_port_range': {'max': 22,
> 'min':
> > > 22}}
> > >
> > > So x uses strings for min/max while y uses integers, and those do not
> > > compare equal in Python:
> > >
> > > >>> 22 == "22"
> > > False
> > >
> > > Once you fix this
> > >
> > > >>> x[k]["destination_port_range"]["max"] = 22
> > > >>> x[k]["destination_port_range"]["min"] = 22
> > >
> > > you get the expected result:
> > >
> > > >>> x == y
> > > True
> > >
> > >
> > > _______________________________________________
> > > Tutor maillist  -  Tutor at python.org
> > > To unsubscribe or change subscription options:
> > > https://mail.python.org/mailman/listinfo/tutor
> > >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list