How do I call a user defined variable from another module... :P

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jory R Ferrell
    New Member
    • Jul 2011
    • 62

    How do I call a user defined variable from another module... :P

    I would like to assign a variable to x, in this case a list.

    A = ['a', 'b', 'c']
    B = ['d', 'e', 'f']## list A and B would both be in a separate

    x = A

    print(importedM odule.x)

    When I run it this way, the program just throws an error because x is technically still undefined in the main module, so this is psuedocode...an y ideas?
    I would also like to know if the same solution to this can be used to call random/user-defined FUNCTIONS from an imported module instead of simple variables.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    "A" and "x" both point to the same object so there is no difference, i.e. use "A".
    Code:
    A = ['a', 'b', 'c']
    
    x = A
    print id(A)
    print id(x)
    x[1] = "1"
    print A
    When I run it this way, the program just throws an error because x is technically still undefined
    Works fine for me, so post your code.
    I would also like to know if the same solution to this can be used to call random/user-defined FUNCTIONS from an imported module instead of simple variables.
    Try it for yourself and see.

    Comment

    • Jory R Ferrell
      New Member
      • Jul 2011
      • 62

      #3
      I should have written the code out in a different manner....

      Originally posted by dwblas
      "A" and "x" both point to the same object so there is no difference, i.e. use "A".
      Code:
      A = ['a', 'b', 'c']
      
      x = A
      print id(A)
      print id(x)
      x[1] = "1"
      print A
      Works fine for me, so post your code. Try it for yourself and see.
      -------------------------------------------------
      import listModule

      x = input('Type A, B, or C.') ## either A, B, or C which are stored lists in imported module

      print(listModul e.x)

      when run this way, importing the lists and their list names, I get an error saying that x is not defined.

      >>>
      Type A, B, or C.A
      Traceback (most recent call last):
      File "C:/Users/Normal Account/Desktop/bbpowre3vrjh", line 5, in <module>
      print(listModul e.x)
      AttributeError: 'module' object has no attribute 'x'
      >>>

      I did write the first example improperly, sorry.

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        "A", as input by the user, is a string and is in an entirely different block of memory from the variable, "A" in listModule.
        Code:
        ##---------- listModule  ----------
        A = ["a", "c", "b"]
        B = "ABC"
        C = 123
        
        
        ##---------- calling program  ----------
        import listModule
        
        x = input('Type A, B, or C--> ')
        x = x.upper()
        
        if "A" == x:     ## note the comparison...string==x
           print(listModule.A)  ## not a string (in quotes), but a variable
        elif "B" == x:
           print(listModule.B)
        elif "C" == x:
           print(listModule.C)
        
        # if you have a lot of comparisons you can use a list or tuple or dictionary
        print("----- second way")
        for check_it in (("A", listModule.A), ("B", listModule.B), ("C", listModule.C)):
            if x = check_it[0]:
                print(check_it[1])

        Comment

        • Jory R Ferrell
          New Member
          • Jul 2011
          • 62

          #5
          Originally posted by dwblas
          "A", as input by the user, is a string and is in an entirely different block of memory from the variable, "A" in listModule.
          Code:
          ##---------- listModule  ----------
          A = ["a", "c", "b"]
          B = "ABC"
          C = 123
          
          
          ##---------- calling program  ----------
          import listModule
          
          x = input('Type A, B, or C--> ')
          x = x.upper()
          
          if "A" == x:     ## note the comparison...string==x
             print(listModule.A)  ## not a string (in quotes), but a variable
          elif "B" == x:
             print(listModule.B)
          elif "C" == x:
             print(listModule.C)
          
          # if you have a lot of comparisons you can use a list or tuple or dictionary
          for check_it in (("A", listModule.A), ("B", listModule.B), ("C", listModule.C)):
              if x = check_it[0]:
                  print check_it[1]
          ---------------------------------------------
          I know how to call a set number of functions or variables from an imported module....what I don't know how to do is
          compactly allow a user to access over 10,000 (yes....ten thousand...)lis ts from an imported module(it'll actually be several modules :P). I'd need to write 10k elif blocks.

          I can't simply write out every single elif block.
          Well...I can but I really don't effing feel like it. lol :D
          Besides, it would just be an impractically large file.
          I want this to have the lowest footprint possible.
          Is there no way to get the two into the same block of memory?

          Comment

          • dwblas
            Recognized Expert Contributor
            • May 2008
            • 626

            #6
            Is there no way to get the two into the same block of memory?
            No. There is no way to automatically map keyboard input to a variable in memory. You have to program it yourself, as this is programming the computer. You can use a dictionary to simplify things. An example follows.
            Code:
            ##---------- listModule  ----------
            external_dict = {"A":["a", "b", "c"],
                             "B":["B", "B", "b"],
                             "C":[1, 2, 3] }
             
             
             ##---------- calling program  ----------
             import listModule
             
             x = input('Type A, B, or C--> ')
             x = x.upper()
            
            if x in listModule.external_dict:
                print(listModule.external_dict[x])
            
            # or
            choices_dict = {"A":listModule.A,
            "                B":listModule.B }  ## etc but that would require thousands of hand built entries
            It is next to impossible to respond with good options shooting in the dark. Are the lists named in any convenient way that could be automatically placed into a dictionary? Can you program a replacement to listModule to generate lists are already in a dictionary? Can you iterate over the program's statements themselves and extract the lists into a dictionary?

            Comment

            • Jory R Ferrell
              New Member
              • Jul 2011
              • 62

              #7
              Originally posted by dwblas
              No. There is no way to automatically map keyboard input to a variable in memory. You have to program it yourself, as this is programming the computer. You can use a dictionary to simplify things. An example follows.
              Code:
              ##---------- listModule  ----------
              external_dict = {"A":["a", "b", "c"],
                               "B":["B", "B", "b"],
                               "C":[1, 2, 3] }
               
               
               ##---------- calling program  ----------
               import listModule
               
               x = input('Type A, B, or C--> ')
               x = x.upper()
              
              if x in listModule.external_dict:
                  print(listModule.external_dict[x])
              ----------------------------------------------------

              Wow....ok.....t his problem has held my project for over 2-3 months now....I was getting seriously discouraged and thinking I MIGHT be retarded. lol
              That explains why compact test engines with with 10k questions in "addressed" , sectioned modules aren't common. :P Thanks for your help.

              Comment

              • Jory R Ferrell
                New Member
                • Jul 2011
                • 62

                #8
                Originally posted by dwblas
                No. There is no way to automatically map keyboard input to a variable in memory. You have to program it yourself, as this is programming the computer. You can use a dictionary to simplify things. An example follows.
                Code:
                ##---------- listModule  ----------
                external_dict = {"A":["a", "b", "c"],
                                 "B":["B", "B", "b"],
                                 "C":[1, 2, 3] }
                 
                 
                 ##---------- calling program  ----------
                 import listModule
                 
                 x = input('Type A, B, or C--> ')
                 x = x.upper()
                
                if x in listModule.external_dict:
                    print(listModule.external_dict[x])
                
                # or
                choices_dict = {"A":listModule.A,
                "                B":listModule.B }  ## etc but that would require thousands of hand built entries
                It is next to impossible to respond with good options shooting in the dark. Are the lists named in any convenient way that could be automatically placed into a dictionary? Can you program a replacement to listModule to generate lists are already in a dictionary? Can you iterate over the program's statements themselves and extract the lists into a dictionary?
                -----------------------------------------------

                Copying the list to a dictionary should work. Again...ty.

                Comment

                • papin
                  New Member
                  • Nov 2011
                  • 7

                  #9
                  python is not PHP: there is no variable variables.

                  Normally, the data should be a dictionary saved using pickle. You should not do import hundreds/thousands of lists/tuples.

                  Here's a solution. I wrote your data as they are (very badly formatted) in an external file.

                  data.txt:
                  A = ['a', 'b', 'c']
                  B = ['d', 'e', 'f']
                  ...
                  Code:
                  import re
                  f = open('data.txt', 'r')
                  data = {}
                  for line in f:
                      key, value = re.findall('([^=]+)=(.*)', line)[0]
                      data[key.strip()] = eval(value.strip())
                  f.close()
                  ## from here, you can save the dictionary with pickle,
                  ## ready to be loaded and quickly accessed
                  x = raw_input('Type A, B, or C--> ')
                  print data[x.upper()][1]  # returns 'e' if x == 'b'
                  del data
                  Avoid using eval().

                  Comment

                  • Jory R Ferrell
                    New Member
                    • Jul 2011
                    • 62

                    #10
                    Hey...I already wrote the program...I had the answer a while ago....thanks though.

                    Comment

                    Working...