473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Jory R Ferrell
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(importedModule.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...any 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.
Oct 28 '11 #1

✓ answered by dwblas

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.
Expand|Select|Wrap|Line Numbers
  1. ##---------- listModule  ----------
  2. external_dict = {"A":["a", "b", "c"],
  3.                  "B":["B", "B", "b"],
  4.                  "C":[1, 2, 3] }
  5.  
  6.  
  7.  ##---------- calling program  ----------
  8.  import listModule
  9.  
  10.  x = input('Type A, B, or C--> ')
  11.  x = x.upper()
  12.  
  13. if x in listModule.external_dict:
  14.     print(listModule.external_dict[x])
  15.  
  16. # or
  17. choices_dict = {"A":listModule.A,
  18. "                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?

9 2442
dwblas
626 Expert 512MB
"A" and "x" both point to the same object so there is no difference, i.e. use "A".
Expand|Select|Wrap|Line Numbers
  1. A = ['a', 'b', 'c']
  2.  
  3. x = A
  4. print id(A)
  5. print id(x)
  6. x[1] = "1"
  7. 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.
Oct 28 '11 #2
@dwblas
-------------------------------------------------
import listModule

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

print(listModule.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(listModule.x)
AttributeError: 'module' object has no attribute 'x'
>>>

I did write the first example improperly, sorry.
Oct 28 '11 #3
dwblas
626 Expert 512MB
"A", as input by the user, is a string and is in an entirely different block of memory from the variable, "A" in listModule.
Expand|Select|Wrap|Line Numbers
  1. ##---------- listModule  ----------
  2. A = ["a", "c", "b"]
  3. B = "ABC"
  4. C = 123
  5.  
  6.  
  7. ##---------- calling program  ----------
  8. import listModule
  9.  
  10. x = input('Type A, B, or C--> ')
  11. x = x.upper()
  12.  
  13. if "A" == x:     ## note the comparison...string==x
  14.    print(listModule.A)  ## not a string (in quotes), but a variable
  15. elif "B" == x:
  16.    print(listModule.B)
  17. elif "C" == x:
  18.    print(listModule.C)
  19.  
  20. # if you have a lot of comparisons you can use a list or tuple or dictionary
  21. print("----- second way")
  22. for check_it in (("A", listModule.A), ("B", listModule.B), ("C", listModule.C)):
  23.     if x = check_it[0]:
  24.         print(check_it[1]) 
Oct 28 '11 #4
@dwblas
---------------------------------------------
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...)lists 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?
Oct 28 '11 #5
dwblas
626 Expert 512MB
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.
Expand|Select|Wrap|Line Numbers
  1. ##---------- listModule  ----------
  2. external_dict = {"A":["a", "b", "c"],
  3.                  "B":["B", "B", "b"],
  4.                  "C":[1, 2, 3] }
  5.  
  6.  
  7.  ##---------- calling program  ----------
  8.  import listModule
  9.  
  10.  x = input('Type A, B, or C--> ')
  11.  x = x.upper()
  12.  
  13. if x in listModule.external_dict:
  14.     print(listModule.external_dict[x])
  15.  
  16. # or
  17. choices_dict = {"A":listModule.A,
  18. "                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?
Oct 28 '11 #6
@dwblas
----------------------------------------------------

Wow....ok.....this 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.
Oct 28 '11 #7
@dwblas
-----------------------------------------------

Copying the list to a dictionary should work. Again...ty.
Oct 28 '11 #8
papin
7
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']
...
Expand|Select|Wrap|Line Numbers
  1. import re
  2. f = open('data.txt', 'r')
  3. data = {}
  4. for line in f:
  5.     key, value = re.findall('([^=]+)=(.*)', line)[0]
  6.     data[key.strip()] = eval(value.strip())
  7. f.close()
  8. ## from here, you can save the dictionary with pickle,
  9. ## ready to be loaded and quickly accessed
  10. x = raw_input('Type A, B, or C--> ')
  11. print data[x.upper()][1]  # returns 'e' if x == 'b'
  12. del data
  13.  
Avoid using eval().
Nov 14 '11 #9
Hey...I already wrote the program...I had the answer a while ago....thanks though.
Nov 26 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Miso Hlavac | last post by:
Hello, I need to create user defined variable in every database session. In Sybase ASA is equivalent: CREATE VARIABLE name TYPE; I need use this in views. Is it possible??? thanx, miso
1
by: AP | last post by:
Hi, If I have created a word template that has a VBA function that I defined in it, is there anyway I can call this function from C#? Adam
2
by: Sydney | last post by:
Hi there I know this should be simple but I cant work out how to call a procedure from another module in .NET. I keep getting an error (Argument not specified for the parameter) Im using...
1
by: comp.lang.python | last post by:
Hi, I am using some TCL driver APIs and user define TCL function to control a traffic generator. The APIs are the traffic generator's specific and in built to the driver library. The issue is...
0
FishVal
by: FishVal | last post by:
Hereby I'm proposing a way of convinient work with properties containing SQL Select statements, particulary RowSource property of ComboBox and ListBox. The usual way is the following. Private...
5
by: jjeanj1 | last post by:
I am having issue using a user defined variable i an a mysql statement. here is the issue that i am facing: i have a variable called $date in php.This variable is assigned a valued from a dropdown...
3
by: anniebai | last post by:
In myTable, there're several columns as Avg1, Avg2 ....Avg92, they all are float type. in my SQL code: declare @counter int declare @parm nvarchar(20) set @counter=0 while @counter<10
3
by: govind161986 | last post by:
I am creating a stored procedure which has multiple joins. Now the problem is I want some joins to be executed for a given condition and some other join for other condition. For example select...
2
by: omar999 | last post by:
hi guys I have a user defined sql function in my sql server db called TenPercentDiscount but not sure how to call it via classic asp? iv tried both ...
4
by: omar999 | last post by:
hi guys I have a user defined sql function in my sql server db called TenPercentDiscount but not sure how to call it within sql statement? I'm ideally looking for my function to be applied to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.