Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 16th, 2007, 12:17 AM   #1
Primusville
Newbie
 
Join Date: Aug 2007
Posts: 11
Rep Power: 0 Primusville is on a distinguished road
Problem with file input

Well, I recently got back into Yu-Gi-Oh cards (the show sucks, but the game is actually fun) as just another way to pass time. Anyway, it gave me the idea to write a Yu-Gi-Oh TCG game in Python. Just command line to start with, maybe with graphics later. My problem is, I want to use text files as databases to hold card information so that I can easily add new cards, but I can't think of a good way to input the file in Python and then make it so that each card adheres to a certain class, and is treated a a separate instance (unless there is a better way). How would I go about doing this?

Many thanks in advance.
Primusville is offline   Reply With Quote
Old Aug 16th, 2007, 12:18 AM   #2
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Try using XML.
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Aug 16th, 2007, 12:20 AM   #3
Primusville
Newbie
 
Join Date: Aug 2007
Posts: 11
Rep Power: 0 Primusville is on a distinguished road
What could I do with XML that I couldn't do with a regular text file (well a lot, I'm sure, but I'm just speaking in the context of my game), and how would I implement the XML files into my program so that they adhered to a class like I want to? I don't have any previous experience with XML so I have no idea. Thanks for the suggestion.

Edit: Also, I need to make it so that the card addition process is completely automated. Would I use a for loop for this?
Primusville is offline   Reply With Quote
Old Aug 16th, 2007, 12:40 AM   #4
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Imagine if you have a class, Card. This class has the following attributes:
class Card
{
	int hitPoints;
	string name;
        string[] attacks;
        ....functions and other shit goes here...
}
Now with the example above, you can read from a XML file, example:
<card>
   <name>Blue Eyes White Dragon</name>
   <hitpoints>1202020</hitpoints>
   <attacks>
      <name>Fire Spin</name>
      <name>Tail Whip</name>
   </attacks>
</card>
I do know a bit of Python but not enough to read from a XML and parse it to extract all of that information. I'm sure someone here could help with that. But if you see my two examples above, that could get your design started.
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Aug 16th, 2007, 12:47 AM   #5
Primusville
Newbie
 
Join Date: Aug 2007
Posts: 11
Rep Power: 0 Primusville is on a distinguished road
Thanks a million for that help; it's definitely what I needed to get started. I found a good guide on processing XML in Python, so that in conjunction with your post will help a lot.
Primusville is offline   Reply With Quote
Old Aug 17th, 2007, 1:43 AM   #6
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
You don't really have to use XML. You just need a structured data format you could use Json with the Python
simplejson library.

Here is a usage example:
python Syntax (Toggle Plain Text)
  1. import simplejson
  2.  
  3. # file mycards.txt
  4. # {
  5. # "Blue Eyes White Dragon" : {
  6. # "hitpoints" : 1202020
  7. # "attacks" : ["Fire Spin", "Tail Whip"]
  8. # }
  9. # }
  10.  
  11. mycards = file("mycards.txt")
  12. myData = simplejson.load(mycards.read())
  13. hitpoints = myData["Blue Eyes White Dragon"]["hitpoints"]

XML might be a good data interchange format, but its pretty verbose, and a pain to work with by hand compared to Json and Yaml. The key is, there is no "parsing" with Json or Yaml. They are already in dict + list format. The data structure in the file exactly maps to python. No hoop jumping or awkward APIs.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Aug 17th, 2007, 3:31 AM   #7
Primusville
Newbie
 
Join Date: Aug 2007
Posts: 11
Rep Power: 0 Primusville is on a distinguished road
Quote:
Originally Posted by Game_Ender View Post
You don't really have to use XML. You just need a structured data format you could use Json with the Python
simplejson library.

Here is a usage example:
python Syntax (Toggle Plain Text)
  1. import simplejson
  2.  
  3. # file mycards.txt
  4. # {
  5. # "Blue Eyes White Dragon" : {
  6. # "hitpoints" : 1202020
  7. # "attacks" : ["Fire Spin", "Tail Whip"]
  8. # }
  9. # }
  10.  
  11. mycards = file("mycards.txt")
  12. myData = simplejson.load(mycards.read())
  13. hitpoints = myData["Blue Eyes White Dragon"]["hitpoints"]

XML might be a good data interchange format, but its pretty verbose, and a pain to work with by hand compared to Json and Yaml. The key is, there is no "parsing" with Json or Yaml. They are already in dict + list format. The data structure in the file exactly maps to python. No hoop jumping or awkward APIs.
Thank you! That's just what I was looking for. At first XML seemed like a great idea, but then I realized the unnecessary difficulty of parsing it in Python. Thanks again for the help; now I think that I can finally get to work on my game.

Edit: I installed simplejson, and it's completely PERFECT for what I need. It works just as well as I hoped it would. Thanks once again for your assistance.

Last edited by Primusville; Aug 17th, 2007 at 4:24 AM.
Primusville is offline   Reply With Quote
Old Aug 17th, 2007, 4:03 AM   #8
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 887
Rep Power: 4 lectricpharaoh will become famous soon enough
I would be very surprised if there weren't libraries to do this in Python. I've never actually used the language, but XML is so ubiquitous that it seems silly to be lacking support.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old Aug 17th, 2007, 4:22 AM   #9
Primusville
Newbie
 
Join Date: Aug 2007
Posts: 11
Rep Power: 0 Primusville is on a distinguished road
There are. In fact, newer versions of Python include them by default. They're just not practical for what I need to do. Too much work to get something so simple done.
Primusville is offline   Reply With Quote
Old Aug 17th, 2007, 2:36 PM   #10
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Python has 3 built in XML libraries but they are either SAX or DOM based. There is a recipe on the Python cookbook that enables a similar dict/list interface to xml data but the resulting xml, is again, very awkward. I think there might be few python modules that wrap these libraries to provide a dict type interface, but why use them? For many purposes JSON is good enough, its easier to type, and has good language support.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem processing file into a char array csrocker101 C++ 1 May 8th, 2007 11:50 PM
OnlineTextEditor.Com! Sane Show Off Your Open Source Projects 43 Jun 16th, 2006 8:55 AM
Problem read'ing from file and user input jmsilver Bash / Shell Scripting 3 May 20th, 2005 2:35 PM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 9:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:52 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC