Re: [Tutor] dictionaries are same but returning false
Ashfaq wrote: > Hi Peter, > The way you find the issue is really cool! Very cool! :) Thanks ;) Here's a bonus solution: >>> import unittest >>> class T(unittest.TestCase): ... def test_xy(self): ... self.maxDiff = None ... self.assertEqual(x, y) ... >>> unittest.main() F == FAIL: test_xy (__main__.T) -- Traceback (most recent call last): File "", line 4, in test_xy AssertionError: {'att[483 chars]in': '22', 'max': '22'}}, '_is_stateless': Fal[23 chars]/24'} != {'att[483 chars]in': 22, 'max': 22}}, '_is_stateless': False, [19 chars]/24'} {'_icmp_options': None, '_is_stateless': False, '_protocol': '6', '_source': '0.0.4.0/24', - '_tcp_options': {'destination_port_range': {'max': '22', 'min': '22'}, ? - - - - + '_tcp_options': {'destination_port_range': {'max': 22, 'min': 22}, 'source_port_range': None}, '_udp_options': None, 'attribute_map': {'icmp_options': 'icmpOptions', 'is_stateless': 'isStateless', 'protocol': 'protocol', 'source': 'source', 'tcp_options': 'tcpOptions', 'udp_options': 'udpOptions'}, 'swagger_types': {'icmp_options': 'IcmpOptions', 'is_stateless': 'bool', 'protocol': 'str', 'source': 'str', 'tcp_options': 'TcpOptions', 'udp_options': 'UdpOptions'}} -- Ran 1 test in 0.008s FAILED (failures=1) $ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries are same but returning false
On Jul 5, 2017 11:09 PM, "shubham goyal" wrote: > Yha that's very smart. Only experience can drive you this way. Otherwise I > was checking value by value. > > On Jul 5, 2017 10:21 PM, "Ashfaq" wrote: > > Hi Peter, > The way you find the issue is really cool! Very cool! :) > > On Wed, Jul 5, 2017 at 6:10 PM, shubham goyal > wrote: > >> Thank you Peter. >> Silly mistakes 😀 >> >> On Jul 5, 2017 5:10 PM, "Peter Otten" <__pete...@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@python.org >> > To unsubscribe or change subscription options: >> > https://mail.python.org/mailman/listinfo/tutor >> > >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries are same but returning false
The difflib library (https://docs.python.org/2/library/difflib.html) can also help with some exploratory discovery of the problem. Here's an example: >>> import difflib >>> for line in difflib.context_diff(repr(x).split(','), repr(y).split(',')): ... print line ... *** --- *** *** 1,7 {'_icmp_options': None '_tcp_options': {'source_port_range': None ! 'destination_port_range': {'max': '22' ! 'min': '22'}} '_protocol': '6' 'swagger_types': {'protocol': 'str' 'tcp_options': 'TcpOptions' --- 1,7 {'_icmp_options': None '_tcp_options': {'source_port_range': None ! 'destination_port_range': {'max': 22 ! 'min': 22}} '_protocol': '6' 'swagger_types': {'protocol': 'str' 'tcp_options': 'TcpOptions' To make the diff comprehensible, we're splitting by commas and comparing each comma-delimited chunk between the representations of x and y. We got lucky here because the dictionaries are printing their values in the same order here. Normally we should not depend on this kind of behavior, but we're just doing exploratory programming, so no foul. :P ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries are same but returning false
On 07/05/2017 11:09 AM, Marc Tompkins wrote: > On Wed, Jul 5, 2017 at 9:51 AM, Ashfaq 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. but not terribly useful for dictionaries, which don't have any sense of order (unless, of course you sort first, or use an ordered dict) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries are same but returning false
On Wed, Jul 5, 2017 at 9:51 AM, Ashfaq 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 > wrote: > > > Thank you Peter. > > Silly mistakes 😀 > > > > On Jul 5, 2017 5:10 PM, "Peter Otten" <__pete...@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@python.org > > > To unsubscribe or change subscription options: > > > https://mail.python.org/mailman/listinfo/tutor > > > > > ___ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries are same but returning false
Hi Peter, The way you find the issue is really cool! Very cool! :) On Wed, Jul 5, 2017 at 6:10 PM, shubham goyal wrote: > Thank you Peter. > Silly mistakes 😀 > > On Jul 5, 2017 5:10 PM, "Peter Otten" <__pete...@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@python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries are same but returning false
Thank you Peter. Silly mistakes 😀 On Jul 5, 2017 5:10 PM, "Peter Otten" <__pete...@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@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionaries are same but returning false
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@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] dictionaries are same but returning false
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? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor