Extracting the literal name of a variable, not the contents?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 4
Reputation: qadsia is an unknown quantity at this point 
Solved Threads: 0
qadsia qadsia is offline Offline
Newbie Poster

Extracting the literal name of a variable, not the contents?

 
0
  #1
Oct 13th, 2009
Hi, I have a problem. I basically have this python code, that generates a file.

I'm trying to get it to output a html file with a chosen name (chosencategory) but the problem is the chosencategory variables are already defined so it attempts to name the html file the contents of the variable.
What i'm basically trying to do is extract the literal name of the variable, and not the contents. Is there any way to do this?

Is there anyway I can do this? btw I'm a programming noob.
  1. output_file = file(filename, 'w')
  2. def build_html(chosenCategory):
  3. #my attempt, which returned an error
  4. filename = '%s.html' %(chosenCategory)
  5. print chosenCategory

example of the variable's output (chosencategory output)

  1. >>> print sports
  2. (('AFL', 6L), ('American Football', 2L), ('Backgammon', 1L), ('Badminton', 2L), ('Baseball', 2L), ('Basketball', 10L), ('Bowling', 1L), ('Chess', 1L), ('Climbing', 1L), ('Cricket', 11L), ('Cross Country', 1L), ('Cycling', 2L), ('Dancing', 1L), ('Darts', 1L), ('Dodgeball', 1L), ('Dog Racing', 1L), ('Dog Sledding', 1L), ('Drifting', 1L), ('Extreme Ironing', 1L), ('Fencing', 1L), ('Fishing', 1L), ('Frisbee', 1L), ('Golf', 5L), ('Hacky-Sack', 1L), ('Hockey', 1L), ('Horse Riding', 1L), ('Ice Hockey', 1L), ('Jogging', 1L), ('Karate', 3L), ('MMA', 1L), ('Modern Pentahlon', 1L), ('Moto GP', 1L), ('Motocross', 2L), ('Mountain Climbing', 1L), ('Netball', 1L), ('Pool', 1L), ('Racing', 1L), ('Rugby League', 15L), ('Rugby Union', 6L), ('Running', 2L), ('Skateboarding', 2L), ('Skiing', 2L), ('Snooker', 1L), ('Snowboarding', 2L), ('Soccer', 27L), ('Softball', 1L), ('Surfing', 1L), ('Swimming', 4L), ('Table Tennis', 3L), ('Tennis', 13L), ('Touch Football', 1L), ('Track and Field', 1L), ('Trekking', 1L), ('Volleyball', 5L), ('Water Polo', 1L), ('White Water Rafting', 1L), ('Wii Sports', 1L), ('World Superbikes', 1L))

the error i get:
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
build_html(sports)
File "C:/Documents and Settings/Administrator/Desktop/New Folder/popularity3.py", line 26, in build_html
filename = '%s.html' %(chosenCategory)
TypeError: not all arguments converted during string formatting

I understand this error, it's because it's trying to use all the contents of the variable (above) to create the html file. I just want the file to be called sports.html Thanks for the help guys
Last edited by qadsia; Oct 13th, 2009 at 9:20 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,042
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 933
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #2
Oct 13th, 2009
Call the function this way:
build_html('sports')
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: qadsia is an unknown quantity at this point 
Solved Threads: 0
qadsia qadsia is offline Offline
Newbie Poster
 
0
  #3
Oct 13th, 2009
Originally Posted by vegaseat View Post
Call the function this way:
build_html('sports')
Thank you so much for the suggestion, but the problem is it needs to be called as a variable because it's variable properties are used in another part of the program.
Is there a way to extract it as a string in the
define function area?
or maybe something involving a dictionary?
The annoying bit is i actually completed the program, I just can't get it to output the files with the correct name.

ok to be more clear, I want the variable sports to still print the data in the sports variable, but I need to also use the name 'sports' as a string to name the html file I create. The problem is I can't just hardcode it in, because there are 6 different categories.
Last edited by qadsia; Oct 13th, 2009 at 10:35 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,042
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 933
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
1
  #4
Oct 13th, 2009
You can not do it in the function you are calling, because the variable is now chosenCategory and the function does not have the information that this variable used to be called sports or whatever.

So my suggestion is to make the category string the first item in the tuple and then use a slice of the tuple for normal use ...
  1. def build_html(chosenCategory):
  2. filename = '%s.html' % (chosenCategory[0])
  3. return filename
  4.  
  5. # test
  6. # make the category string the first item in the tuple
  7. sports = ('sports', ('AFL', 6L), ('American Football', 2L))
  8.  
  9. fname = build_html(sports)
  10.  
  11. print fname # sports.html
  12.  
  13. # to use the tuple for its values slice out the category name
  14. print sports[1:] # (('AFL', 6L), ('American Football', 2L))
Last edited by vegaseat; Oct 13th, 2009 at 12:31 pm. Reason: slice
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,054
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 265
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
2
  #5
Oct 13th, 2009
This is a very hacked solution but:
  1. >>> sports = ## Defined as above - snipped for length
  2. >>> def build_html(chosenCategory):
  3. ... up_locals = sys._getframe(1).f_locals
  4. ... for each_var in up_locals:
  5. ... if id(up_locals[each_var]) == id(chosenCategory):
  6. ... var_name = each_var
  7. ... filename = '%s.html' % (var_name)
  8. ... print filename
  9. ...
  10. >>> build_html(sports)
  11. sports.html
  12. >>>
You could even be brazen and shorten that for loop to a single list comprehension (albeit a very long one):
  1. def build_html(chosenCategory):
  2. var_name = [each_var for each_var in sys._getframe(1).f_locals if id(sys._getframe(1).f_locals[each_var]) == id(chosenCategory)][0]
  3. filename = '%s.html' % (var_name)
  4. print filename
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: qadsia is an unknown quantity at this point 
Solved Threads: 0
qadsia qadsia is offline Offline
Newbie Poster
 
0
  #6
Oct 14th, 2009
Thank you , both suggestions worked perfectly The program was running perfectly!
My problem now is I realised I actually needed a string input. e.g. ('sports') So now I need the complete opposite.
Is there a way to use a string to call a previously named variable? Thank you so much
i've been researching and something like exec or .append or vars() might be what i'm looking for.. what do you think??
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,054
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 265
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
2
  #7
Oct 14th, 2009
Originally Posted by qadsia View Post
Is there a way to use a string to call a previously named variable?
Eval is what you're looking for:
  1. >>> sports = [1,23,4]
  2. >>> eval( 'sports' )
  3. [1, 23, 4]
  4. >>>
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: qadsia is an unknown quantity at this point 
Solved Threads: 0
qadsia qadsia is offline Offline
Newbie Poster
 
0
  #8
Oct 14th, 2009
Thanks, the eval function actually worked. But I actually solved the problem like an hour ago, by rewriting my program using a dictionary.
To help anyone else that might be stuck on this in the future,
what I did was use a dictionary with all the variables, which is more powerful than variable apparently? So it allowed me to take a string from the user input and output the data.
But yeah, eval also works, which I had seen it sooner lol. Will use it next time. But yeah, thank you Vegaseat and Jlm699, I appreciate the help so much.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC