0

I have two files..

data.py

a1 = {"city":"Boston","code":"BOS"}
a2 = {"city":"Atlanta","code":"ATL"}
a3 = {"city":"Chicago","code":"ORD"}
a4 = ...
a5 = ...
a6 = ...

run.py

from data import *

Class Port_code():
def _init_()
   ...

def get_code():
  x = globals().items()
  for i in range(len(x)):
        if x[i][1]['city'] == user_input_city:
           print x[i][1]['code']

when i run this i get the below Error :

typeerror: 'module' object has no attribute '__getitem__'

is it because i'm using it inside the Class ??

i have a sample code where it gives me what i want it's not inside a class. sample.py

from data import *

user_input=raw_input("enter a City: ")
x = globals().items()
for i in range(len(x)):
   if x[i][1]['city'] == user_input:
      print x[i][1]['city'], 
      print " <== "+str(i)+" ==> ",
      print x[i][1]['code']

**The Sample.py works Fine ! **

So can i get a clear idea of using inside Class ?

  • your class contains many typos, `class` not `Class`, `__init__` not `_init_`, please check if this is your real code – PRMoureu Nov 15 '17 at 06:34
  • @PRMoureu ignore the Typos ! that's not the issue here – Dante Devilhunter Nov 15 '17 at 06:36
  • 1
    Please use a list. Do not iterate through globals, that could backfire very easily. Using a JSON file to store the content would make even more sense. – Neil Nov 15 '17 at 06:37
  • i'm using a list. @nfn neil – Dante Devilhunter Nov 15 '17 at 06:38
  • a1 = {"city":"Boston","code":"BOS"} is a dictionary... Anyway. What I meant was a list of dictionaries: [{"city":"Boston","code":"BOS"}, {"city":"Atlanta","code":"ATL"}, {"city":"Chicago","code":"ORD"}] – Neil Nov 15 '17 at 06:39
  • @nfn neil : yes ! if you look carefully... i converted the dict to lists x = globals().items() – Dante Devilhunter Nov 15 '17 at 06:40
  • Wait, this part of a school assignment? Because I am asking you not to do this because grabbing all the globals and iterating over them isn't a good idea. But if it's part of your assignment, than you're teacher is insane. You could have other globals in your code which would break your code. – Neil Nov 15 '17 at 06:44
  • No, it's not school assignment ! the data file contains 4K lines of data. i need to use it inside a CLASS bro... i already made a sample code with iterates through the entire data file and gets me my airport code. – Dante Devilhunter Nov 15 '17 at 06:50
  • @DanteDevilhunter the problem is that you are iterating through globals, but globals contains at least one module object. Globals will contain *everything* at the module-level. Don't do this. Just use `json` as a serialization format, or if you want to use a python module, put all your `a` variables in a list, then `from data import a_list` and iterate over that list. – juanpa.arrivillaga Nov 15 '17 at 06:59
  • @juanpa.arrivillaga Yess !!! you're Right !! that is the problem. it's getting all the variables. Thank you ! it helped me understand – Dante Devilhunter Nov 15 '17 at 08:30

0 Answers0