Bottom Page

Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 why is my dictionary not recognized as such
#1
I am still a newbie but learning fast. However, Python is still surprising me in strange ways. Here is my code
supported_drivers = {"SQLite" : 1,
                    "Sybase" : 3,
                    "SQLserver" : 7,
                    "Oracle" : 8 ,
                    "MongoDB" : 10,
                    "DynamoDB" : 11
                    }
print (vars(supported_drivers))
and here is the result:
Traceback (most recent call last):
File "Z:\projects\xxx\yyy.py", line 58, in <module>
print (vars(supported_drivers))
TypeError: vars() argument must have __dict__ attribute.

OK, so why is my, seemingly a dictionary, not a dictionary?

Thank you all
ZA
Quote
#2
remove vars:
supported_drivers = {"SQLite" : 1,
                    "Sybase" : 3,
                    "SQLserver" : 7,
                    "Oracle" : 8 ,
                    "MongoDB" : 10,
                    "DynamoDB" : 11
                    }
print (supported_drivers)
output:
Output:
{'SQLite': 1, 'Sybase': 3, 'SQLserver': 7, 'Oracle': 8, 'MongoDB': 10, 'DynamoDB': 11}
or as elements:
for key, value in supported_drivers.items():
    print('{}, {}'.format(key, value))
output:
Output:
SQLite, 1 Sybase, 3 SQLserver, 7 Oracle, 8 MongoDB, 10 DynamoDB, 11
Quote
#3
Because vars isn't looking for a dictionary, it's looking for something with a __dict__ attribute, which dictionaries don't have. The vars function is for things like classes and instances.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#4
Thank you all
Quote
#5
I must admit that Python comparisons and matches begin to drive me nuts:
       self.driver_id = supported_drivers[self.driver] if self.driver in supported_drivers else None
        print (self.driver, "\n", supported_drivers)

        print (self.driver, "  ", self.dbengine, "  ", self.driver_id, "\n\n")
        if 'Sybase' == self.driver:
            print ('Sybase', ' is equal ', self.driver)
            if self.dbengine == 'ASE':
                self.driver_id = 4
            elif self.dbengine == 'IQ':
                self.driver_id = 5
            elif self.dbengine != '':
                print(self.driver, "\n\n")
                sys.exit(self.dbengine)
        else:
            print ('Sybase', ' is not equal ', self.driver)
results:

'Sybase'
{'SQLite': 1, 'Sybase': 3, 'SybaseASE': 4, 'SybaseIQ': 5, 'SQLserver': 7, 'Oracle': 8, 'MongoDB': 10, 'DynamoDB': 11}
'Sybase' 'BLA' None


Sybase is not equal 'Sybase'

-------
as you see, self.driver is 'Sybase' and it is IN supported_drivers, yet Python says it is NOT.
And then it is NOT EQUAL to 'Sybase'...
What the heck is going here?

Now I do compare strings unless you tell me that in Python a string is also not a string under certain circumstances

Thank you
ZA
Quote
#6
Print the type of "Sybase" and self.driver. You may be comparing one type to another which is never equal. https://www.geeksforgeeks.org/byte-objec...ng-python/
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 188 Apr-27-2019, 03:13 AM
Last Post: perfringo
  pyserial-master installed bbut not recognized elwolv1 0 211 Jan-04-2019, 08:37 PM
Last Post: elwolv1
  How do I calculate the smallest value that is recognized as a difference when compari AFoeee 1 552 Oct-28-2018, 10:48 PM
Last Post: Gribouillis
  'videodigest' is not recognized as an internal or external command MM2018 2 451 Oct-12-2018, 02:43 PM
Last Post: MM2018
  Empty attribute class dictionary after saving it in a class object dictionary 3dimensions 6 1,076 May-20-2018, 01:57 PM
Last Post: 3dimensions
  Changing values in a dictionary within a dictionary swannhouse 2 642 Apr-25-2018, 11:59 PM
Last Post: swannhouse
  Class attribute not recognized\working J125 1 1,434 Dec-19-2016, 01:05 AM
Last Post: Yoriz
  Dictionary value not recognized marciokoko 1 1,260 Nov-16-2016, 04:01 PM
Last Post: snippsat
  Need to refer to Value of a key of a dictionary nested within a dictionary xepicxmonkeyx 14 3,996 Oct-06-2016, 07:35 PM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)