0

I am new to python trying to write a very simple game with simple login system to save progression later on.

I am doing User objects for login and storing in list, but list is empty on program exit, this is what I've written

class Users(object):


    users = []

    def __init__(self, username=None, password=None):

        global users #recommended from comment on this website other topic

        self.username = username
        self.password = password

#print Users.users[0].username    #was using to check if object saved to list
#print Users.users[0].password

print "L to login, R to register, Q to quit"
answer = raw_input("> ")
if answer == "r":
    username_r = raw_input("Create name: ")
    for user in Users.users:
        if username_r in user.username:
    #if any (self.username == username_r for Users in Users.users):
            print "Name already exists"
    password_r = raw_input("Create password: ")


    u = Users(username_r, password_r)
    Users.users.insert(len(Users.users), u)

    print "\n------------------\n"
    print Users.users[0].username #appends correctly
    print Users.users[0].password

I have tried and read other code from website but it does not store in list permanently to access later on.

This code is only introduction for game, user opens and he is asked to login or register to start.

Using notepad++ and powershell, any help is greatly appreciated, thank you.

gorgon
  • 13
  • 1
  • 1
    The `pickle` module provides data persistence, or the `shelve` module which uses pickle under the hood but a dictionary-like syntax that's easy to use. Otherwise, consider storing information in a database or in some other intermediary format (json, xml, etc). – g.d.d.c Sep 30 '14 at 21:56
  • where did you see the use of `global`in a class? – Padraic Cunningham Sep 30 '14 at 22:37
  • possible duplicate of [Common use-cases for pickle in Python](http://stackoverflow.com/questions/3438675/common-use-cases-for-pickle-in-python) – simonzack Sep 30 '14 at 22:46
  • I don't believe is a duplicate with Common use-cases for pickle in Python since this case might use another storage mechanism. – RobertoAllende Sep 30 '14 at 22:51

1 Answers1

1

I would use Pickle:

The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” or “flattening”, however, to avoid confusion, the terms used here are “pickling” and “unpickling”.

Below is an example for your class Users:

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
>>> class Users(object):
...     users = []
...     def __init__(self, username=None, password=None):
...         global users
...         self.username = username
...         self.password = password
... 
>>> import pickle
>>> luke = Users('Luke', 'Skywalker')
>>> messi = Users('Lionel', 'Messi')
>>> 
>>> myusers = [luke, messi]
>>> 
>>> pickle.dump( myusers, open("myusers.p", "wb"))
>>>

So, above i defined the class Users, i made two instances and finally i stored those objects inside a file called myusers.p. The last step is called to serialize.

To recover them you've to do:

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
>>> class Users(object):
...     users = []
...     def __init__(self, username=None, password=None):
...         global users
...         self.username = username
...         self.password = password
... 
>>> import pickle
>>> myusers = pickle.load( open( "myusers.p", "rb" ) )
>>> myusers
[<__main__.Users object at 0x7fe4cecfc350>, 
 <__main__.Users object at 0x7fe4cecfc390>]
>>> myusers[0].username
'Luke'
>>> myusers[1].username
'Lionel'
>>> 

Note that before restoring the objects, the class Users must be defined in current scope.

If you need go further in this approach you might consider using ZODB, a nosql database or a sql database.

A good thing of using a pure object storage mechanism like pickle or zodb, is that you deal just with one paradigm, objects. But in an relational database, you might need to use a object relational mapper (ORM) which adds a little of complexity but if you need to storage big amounts of data and do a lot of queries is one of the best ways to go. There are plenty of ORMs in Python.

RobertoAllende
  • 8,744
  • 4
  • 30
  • 49
  • cool! now I know about pickle, I used and it worked but only for one Users Object, if another user register later on it replace the first one, i.e it doesn't go beyond index 0 – gorgon Oct 01 '14 at 00:57
  • The example just shows how to use pickle. Based on your comment i suggest you take a look on how list works in python, in particular append method. Also it might be useful using dictionaries instead of lists. – RobertoAllende Oct 01 '14 at 14:17