Storing data in python

A place to discuss things that aren't specific to any one creator or game.
Forum rules
Ren'Py specific questions should be posted in the Ren'Py Questions and Annoucements forum, not here.
Post Reply
Message
Author
Mew151
Newbie
Posts: 10
Joined: Thu Jul 16, 2009 8:13 pm
Projects: Unbeatable Battles (Pre-production)
Contact:

Storing data in python

#1 Post by Mew151 »

What's an efficient way of storing data in python, like a database?

I need to store data for stats in a game, as well as enemy information.

chunderbunny
Regular
Posts: 52
Joined: Sat Feb 09, 2008 1:50 pm
Projects: SSS:Renaissance, Canadense (WIP)
Contact:

Re: Storing data in python

#2 Post by chunderbunny »

There are a few ways of doing it, depending on comfortable you are with programming.

The simplest would be to use a python dictionary, which stores data as key-data pairs. An empty dictionary is initialised with curly braces:

Code: Select all

D = {}
And then you assign data to the dictionary thus:

Code: Select all

D['Name'] = 'James'
D['Age'] = 35
D['Health'] = 100
A good way of tracking stats for a game would be to store the character's name as the key in a dictionary, and then store a dictionary of the various stats as the data. (Basically we are storing a bunch of dictionaries inside a master dictionary)

Code: Select all

Stats = {}

James = {}
James['Health'] = 78
James['Money'] = 12

Mark = {}
Mark['Health'] = 21
Mark['Money'] = 156

Stats['James'] = James 
Stats['Mark'] = Mark
Then when you want to get the data back again:

Code: Select all

Stats['Mark']['Health']
>>> 21
A more powerful way of doing this would be to use classes and object oriented programming techniques, but this is more complicated and probably not worth it if you just want to store some data in a sensible way.

Mew151
Newbie
Posts: 10
Joined: Thu Jul 16, 2009 8:13 pm
Projects: Unbeatable Battles (Pre-production)
Contact:

Re: Storing data in python

#3 Post by Mew151 »

Thank you!

I'll be using this method for data storage! :D

Mew151
Newbie
Posts: 10
Joined: Thu Jul 16, 2009 8:13 pm
Projects: Unbeatable Battles (Pre-production)
Contact:

Re: Storing data in python

#4 Post by Mew151 »

Actually, I'm familiar with OOP.

I'd actually like to know how to store data with Class objects.

User avatar
killdream
Veteran
Posts: 325
Joined: Wed Nov 05, 2008 1:05 pm
Projects: EVūL (WIP), insilo (WIP), Cute Demon Crashers!
Deviantart: robotlolita
Github: robotlolita
Location: World's End (aka Brazil)
Contact:

Re: Storing data in python

#5 Post by killdream »

Mew151 wrote:I'd actually like to know how to store data with Class objects.
Eto... just create a class? o.o

Code: Select all

class human:
    # the object constructor
    def __init__(self, age, sex, weight, height):
        self.age        = age
        self.sex        = sex
        self.weight     = weight
        self.height     = height
Then you instantiate this class whenever you need it. And access the fields defined in it.

Code: Select all

james = human(31,       #age
              "male",   #sex
              67,       #weight (in kg)
              170,      #height (in cm)
              )
Or you could inherit MaleHuman and FemaleHuman from the base human class.

Code: Select all

class MaleHuman(human):
    def __init__(self, age, weight, height):
        human.__init__(self, age, "male", weight, height) # inherits from base class
If you need persistent data, you can use CVS files, or write a file format of your own using xdrlib/another module or getting the pain of writing/reading it directly using binary data. Or you can even use some DBGS system like MySQL and the likes (which I don't recommend, anyways, unless you're making a MMO game).

chunderbunny
Regular
Posts: 52
Joined: Sat Feb 09, 2008 1:50 pm
Projects: SSS:Renaissance, Canadense (WIP)
Contact:

Re: Storing data in python

#6 Post by chunderbunny »

killdream wrote: If you need persistent data, you can use CVS files, or write a file format of your own using xdrlib/another module or getting the pain of writing/reading it directly using binary data. Or you can even use some DBGS system like MySQL and the likes (which I don't recommend, anyways, unless you're making a MMO game).
I think most people persist data in python using pickle, cpickle (basically just a faster pickle) or shelve. Renpy probably has its own recommended way of storing things though.

Post Reply

Who is online

Users browsing this forum: No registered users