944,111 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1168
  • Python RSS
Oct 13th, 2009
0

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

Expand Post »
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.
Python Syntax (Toggle Plain Text)
  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)

Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
qadsia is offline Offline
5 posts
since Oct 2009
Oct 13th, 2009
0
Re: Extracting the literal name of a variable, not the contents?
Call the function this way:
build_html('sports')
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 13th, 2009
0
Re: Extracting the literal name of a variable, not the contents?
Click to Expand / Collapse  Quote originally posted by vegaseat ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
qadsia is offline Offline
5 posts
since Oct 2009
Oct 13th, 2009
1
Re: Extracting the literal name of a variable, not the contents?
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 ...
python Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 13th, 2009
2
Re: Extracting the literal name of a variable, not the contents?
This is a very hacked solution but:
python Syntax (Toggle Plain Text)
  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):
python Syntax (Toggle Plain Text)
  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
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 14th, 2009
0
Re: Extracting the literal name of a variable, not the contents?
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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
qadsia is offline Offline
5 posts
since Oct 2009
Oct 14th, 2009
2
Re: Extracting the literal name of a variable, not the contents?
Click to Expand / Collapse  Quote originally posted by qadsia ...
Is there a way to use a string to call a previously named variable?
Eval is what you're looking for:
python Syntax (Toggle Plain Text)
  1. >>> sports = [1,23,4]
  2. >>> eval( 'sports' )
  3. [1, 23, 4]
  4. >>>
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 14th, 2009
0
Re: Extracting the literal name of a variable, not the contents?
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
qadsia is offline Offline
5 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Why does this return an error: Lists
Next Thread in Python Forum Timeline: Sieve Program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC