2

I have defined this class:

class User():
 def __init__(self, ip, update, priority):
    self.ip = ip
    self.update = update
    self.priority = priority

I work with a Socket and every time one client is connect I create an User object. I would like to save it somewhere and then be able to process its data, meaning read the file where it's saved. I don't know how to do that. I tried using a txt file but with the fields of the class User is to complicated and I would also like to have something more dynamic, like

add(user), isPresent(user), overwrite(user)

What is the best solution for my case?

rebrid
  • 430
  • 8
  • 27

1 Answers1

1

If your User class is really that simple, you can use Pickle:

(both Python 2 and 3 links included).

 u = User(127.0.0.1, "foo", "bar")
 pickle.dumps(u)
criswell
  • 759
  • 6
  • 10
  • Thanks. What does it happen if I save twice the same object? There will be only one or two entities? – rebrid Nov 19 '15 at 14:56
  • @m.ridolfi - as far as I know, Pickle should be idempotent. So saving the same object multiple times will have the same result. Now, of course, you have to *pick* where to save the object (a text file, to a database, whatever), so if you're asking can you *physically* save the same object twice, that's entirely up to you. – criswell Nov 19 '15 at 15:14