Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
storing python objects
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
trscookie
Tux's lil' helper
Tux's lil' helper


Joined: 18 Jan 2006
Posts: 145
Location: London

PostPosted: Mon Aug 15, 2011 9:13 am    Post subject: storing python objects Reply with quote

Hello All,

I am new to python so I am sure that this question has cropped up before however I have not been able to find any real answers. I am looking for pythons answers to c-structs, I have followed the documentation on its arrays an pickle but I am not sure that this can help me.

Basically I would like to create an object/struct with a unique name or number and add values to this object:

i.e:
Code:

bob = student()

bob->name = bob
bob->age = "10/07/1990"


save(bob)


open(bob)

print bob->name


Also if there is a method of encrypting this data that would be really helpful.

many thanks in advance,
trscookie
Back to top
View user's profile Send private message
ppurka
Advocate
Advocate


Joined: 26 Dec 2004
Posts: 3256

PostPosted: Mon Aug 15, 2011 10:47 am    Post subject: Reply with quote

If you have only one value to assign then maybe you can look at dict?
Code:
student = {}
student['bob'] = "01/02/1922"
student['bib'] = "02/03/1955"
print student
{'bob': '01/02/1922', 'bib': '02/03/1955'}
print student['bob']
01/02/1922

_________________
emerge --quiet redefined | E17 vids: I, II | Now using kde5 | e is unstable :-/
Back to top
View user's profile Send private message
trscookie
Tux's lil' helper
Tux's lil' helper


Joined: 18 Jan 2006
Posts: 145
Location: London

PostPosted: Mon Aug 15, 2011 10:57 am    Post subject: Reply with quote

:S I have hundreds of values to assign unfortunately which makes this whole procedure harder im afraid!

I dont suppose you know if you can have an array of objects also, i.e:

Code:

student[0] = Student()
student[1] = Student()

student[0]t->name = "bob"
student[0]->dob = "12/12/2012"
student[0]->hometown = "london"


student[1]->name = "biff"
student[1]->dob = "11/11/1911"
student[1]->hometown = "New York"
Back to top
View user's profile Send private message
Hypnos
Advocate
Advocate


Joined: 18 Jul 2002
Posts: 2889
Location: Omnipresent

PostPosted: Mon Aug 15, 2011 12:55 pm    Post subject: Reply with quote

I would think that pickle applied to dictionaries would do the job for you, but you can look at the whole list of Python data persistence modules here. If you need higher performance and portability then a database-driven solution might be useful.

There is also the struct module for translating between C structs and Python objects.
_________________
Personal overlay | Simple backup scheme
Back to top
View user's profile Send private message
dol-sen
Retired Dev
Retired Dev


Joined: 30 Jun 2002
Posts: 2805
Location: Richmond, BC, Canada

PostPosted: Mon Aug 15, 2011 1:30 pm    Post subject: Reply with quote

Sounds like you want something like this:
Code:
class Student(object):
    def __init__(self, name, dob, hometown):
        self.name = name
        self.dob = dob
        self.hometown = hometown


students = []
student.append(Student('bob', "12/12/2012", "london")
...


There are different ways it can be saved to disk, it can be converted to xml...
It also depends how you want to access or index the data. Also what operations you want to perform on it.

Another option is a dictionary of dictionaries

Code:
data = {}
data['bob'] = {'dob':"12/12/2012", "hometown": "london"}
...
names = sorted(data)
bob = data['bob']
print bob['dob']


We need more info what you want to do with it to advise further.
_________________
Brian
Porthole, the Portage GUI frontend irc@freenode: #gentoo-guis, #porthole, Blog
layman, gentoolkit, CoreBuilder, esearch...
Back to top
View user's profile Send private message
trscookie
Tux's lil' helper
Tux's lil' helper


Joined: 18 Jan 2006
Posts: 145
Location: London

PostPosted: Mon Aug 15, 2011 2:08 pm    Post subject: Reply with quote

Ah cheers for the response, I like the idea of the first example that you give:

Code:

class Student(object):
    def __init__(self, name, dob, hometown):
        self.name = name
        self.dob = dob
        self.hometown = hometown


students = []
student.append(Student('bob', "12/12/2012", "london")


Ill check out methods of storing this; XML storage sounds useful.

I have tried to compile this example though and it fails on line 10 even though I only have 9 lines!?:

Code:

  1 class Student(object):
  2     def __init__(self, name, dob, hometown):
  3         self.name = name
  4         self.dob = dob
  5         self.hometown = hometown
  6
  7
  8 students = []
  9 students.append(Student('bob', "12/12/2012", "london")

python class_struct.py
  File "class_struct.py", line 10
   
                                                           ^
SyntaxError: invalid syntax



Many thanks again,
trscookie
Back to top
View user's profile Send private message
dol-sen
Retired Dev
Retired Dev


Joined: 30 Jun 2002
Posts: 2805
Location: Richmond, BC, Canada

PostPosted: Mon Aug 15, 2011 2:35 pm    Post subject: Reply with quote

Your missing a closing brace ;)

also you can add encription and decription functions to it for storing and unpacking the encrypted data from xml or just a file with end of line separation.
Code:
class Student(object):
    def __init__(self, name=none, dob=None, hometown=None, encrypted=None):
        self.name = name
        self.dob = dob
        self.hometown = hometown
        self.encrypted = encrypted
        if encrypted:
            self.unpack()

   def encrypt(self):
        # some code...
        pass
       
    def unpack(self):
        # some decrypting code
        pass

students = []
# some fake encrypted data for the example
enc = '4328558wd fdwkhj3rjhdfjdh30340-5jfjwdlwlr'
student.append(Student(enccrypt=enc))
...
s = students[x]
print s.name, s.dob, s.hometown
...
save = []
for s in students:
    save.append(s.encrypt())
...
# some code to write it to disk
...
write(... '\n'.join(save))

_________________
Brian
Porthole, the Portage GUI frontend irc@freenode: #gentoo-guis, #porthole, Blog
layman, gentoolkit, CoreBuilder, esearch...
Back to top
View user's profile Send private message
trscookie
Tux's lil' helper
Tux's lil' helper


Joined: 18 Jan 2006
Posts: 145
Location: London

PostPosted: Mon Aug 15, 2011 3:22 pm    Post subject: Reply with quote

ah cheers, one facepalm to me. thanks for your help, much appreciated :D
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum