# Persistencia (pickle, json)
import datetime as dt
import pickle
import json
class DefaultEncoder( json.JSONEncoder ) :
def default( self , o) :
return o.__dict__
class Nota:
def __init__ ( self , anyo= 0 , mes= 0 , dia= 0 , hora= 0 , contenido= "" ) :
self ._anyo = anyo
self ._mes = mes
self ._dia = dia
self ._hora = hora
self ._contenido = contenido
@ property
def contenido( self ) :
return self ._contenido
@ property
def fecha( self ) :
return dt.datetime ( self ._anyo,
self ._mes,
self ._dia,
self ._hora,
0 ,
self .contenido )
def __str__ ( self ) :
return ( str ( self ._anyo)
+ "-" + str ( self ._mes)
+ "-" + str ( self ._dia)
+ " " + str ( self ._hora) + ":00"
+ " -> " + self .contenido )
@ staticmethod
def guarda_notas_json( fn, lnotas) :
with open ( fn, "wt" ) as f:
json.dump ( lnotas, f, cls= DefaultEncoder)
@ staticmethod
def recupera_notas_json( fn) :
toret = [ ]
with open ( fn, "rU" ) as f:
lnotas = json.load ( f)
for d in lnotas:
n = Nota( )
n.__dict__ = d
toret.append ( n)
return toret
@ staticmethod
def guarda_notas( fn, lnotas) :
with open ( fn, "wb" ) as f:
pickle .dump ( lnotas, f)
@ staticmethod
def recupera_notas( fn) :
toret = [ ]
with open ( fn, "rb" ) as f:
toret = pickle .load ( f)
return toret
def guarda( ) :
nota = Nota( 2018 , 3 , 23 , 9 , "Venir a clases" )
Nota.guarda_notas ( "notas.dat" , [ nota] )
def recupera( ) :
return Nota.recupera_notas ( "notas.dat" )
def guarda_json( ) :
nota = Nota( 2018 , 3 , 23 , 9 , "Venir a clases" )
Nota.guarda_notas_json ( "notas_json.txt" , [ nota] )
def recupera_json( ) :
return Nota.recupera_notas_json ( "notas_json.txt" )
if __name__ == "__main__" :
guarda_json( )
lnotas = recupera_json( )
for nota in lnotas:
print ( nota)
IyBQZXJzaXN0ZW5jaWEgKHBpY2tsZSwganNvbikKCmltcG9ydCBkYXRldGltZSBhcyBkdAppbXBvcnQgcGlja2xlCmltcG9ydCBqc29uCgoKY2xhc3MgRGVmYXVsdEVuY29kZXIoanNvbi5KU09ORW5jb2Rlcik6CiAgICBkZWYgZGVmYXVsdChzZWxmLCBvKToKICAgICAgICByZXR1cm4gby5fX2RpY3RfXwoKCmNsYXNzIE5vdGE6CiAgICBkZWYgX19pbml0X18oc2VsZiwgYW55bz0wLCBtZXM9MCwgZGlhPTAsIGhvcmE9MCwgY29udGVuaWRvPSIiKToKICAgICAgICBzZWxmLl9hbnlvID0gYW55bwogICAgICAgIHNlbGYuX21lcyA9IG1lcwogICAgICAgIHNlbGYuX2RpYSA9IGRpYQogICAgICAgIHNlbGYuX2hvcmEgPSBob3JhCiAgICAgICAgc2VsZi5fY29udGVuaWRvID0gY29udGVuaWRvCgogICAgQHByb3BlcnR5CiAgICBkZWYgY29udGVuaWRvKHNlbGYpOgogICAgICAgIHJldHVybiBzZWxmLl9jb250ZW5pZG8KICAgICAgICAKICAgIEBwcm9wZXJ0eQogICAgZGVmIGZlY2hhKHNlbGYpOgogICAgICAgIHJldHVybiBkdC5kYXRldGltZShzZWxmLl9hbnlvLAogICAgICAgICAgICAgICAgICAgICAgICAgICBzZWxmLl9tZXMsCiAgICAgICAgICAgICAgICAgICAgICAgICAgIHNlbGYuX2RpYSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgc2VsZi5faG9yYSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgMCwKICAgICAgICAgICAgICAgICAgICAgICAgICAgc2VsZi5jb250ZW5pZG8pCgogICAgZGVmIF9fc3RyX18oc2VsZik6CiAgICAgICAgcmV0dXJuIChzdHIoc2VsZi5fYW55bykKICAgICAgICAgICAgICAgICsgIi0iICsgc3RyKHNlbGYuX21lcykKICAgICAgICAgICAgICAgICsgIi0iICsgc3RyKHNlbGYuX2RpYSkKICAgICAgICAgICAgICAgICsgIiAiICsgc3RyKHNlbGYuX2hvcmEpICsgIjowMCIKICAgICAgICAgICAgICAgICsgIiAtPiAiICsgc2VsZi5jb250ZW5pZG8pCgogICAgQHN0YXRpY21ldGhvZAogICAgZGVmIGd1YXJkYV9ub3Rhc19qc29uKGZuLCBsbm90YXMpOgogICAgICAgIHdpdGggb3BlbihmbiwgInd0IikgYXMgZjoKICAgICAgICAgICAganNvbi5kdW1wKGxub3RhcywgZiwgY2xzPURlZmF1bHRFbmNvZGVyKQoKICAgIEBzdGF0aWNtZXRob2QKICAgIGRlZiByZWN1cGVyYV9ub3Rhc19qc29uKGZuKToKICAgICAgICB0b3JldCA9IFtdCgogICAgICAgIHdpdGggb3BlbihmbiwgInJVIikgYXMgZjoKICAgICAgICAgICAgbG5vdGFzID0ganNvbi5sb2FkKGYpCgogICAgICAgIGZvciBkIGluIGxub3RhczoKICAgICAgICAgICAgbiA9IE5vdGEoKQogICAgICAgICAgICBuLl9fZGljdF9fID0gZAogICAgICAgICAgICB0b3JldC5hcHBlbmQobikKCiAgICAgICAgcmV0dXJuIHRvcmV0CgogICAgQHN0YXRpY21ldGhvZAogICAgZGVmIGd1YXJkYV9ub3RhcyhmbiwgbG5vdGFzKToKICAgICAgICB3aXRoIG9wZW4oZm4sICJ3YiIpIGFzIGY6CiAgICAgICAgICAgIHBpY2tsZS5kdW1wKGxub3RhcywgZikKCiAgICBAc3RhdGljbWV0aG9kCiAgICBkZWYgcmVjdXBlcmFfbm90YXMoZm4pOgogICAgICAgIHRvcmV0ID0gW10KICAgICAgICB3aXRoIG9wZW4oZm4sICJyYiIpIGFzIGY6CiAgICAgICAgICAgIHRvcmV0ID0gcGlja2xlLmxvYWQoZikKCiAgICAgICAgcmV0dXJuIHRvcmV0CgoKZGVmIGd1YXJkYSgpOgogICAgbm90YSA9IE5vdGEoMjAxOCwgMywgMjMsIDksICJWZW5pciBhIGNsYXNlcyIpCiAgICBOb3RhLmd1YXJkYV9ub3Rhcygibm90YXMuZGF0IiwgW25vdGFdKQoKCmRlZiByZWN1cGVyYSgpOgogICAgcmV0dXJuIE5vdGEucmVjdXBlcmFfbm90YXMoIm5vdGFzLmRhdCIpCgoKZGVmIGd1YXJkYV9qc29uKCk6CiAgICBub3RhID0gTm90YSgyMDE4LCAzLCAyMywgOSwgIlZlbmlyIGEgY2xhc2VzIikKICAgIE5vdGEuZ3VhcmRhX25vdGFzX2pzb24oIm5vdGFzX2pzb24udHh0IiwgW25vdGFdKQoKCmRlZiByZWN1cGVyYV9qc29uKCk6CiAgICByZXR1cm4gTm90YS5yZWN1cGVyYV9ub3Rhc19qc29uKCJub3Rhc19qc29uLnR4dCIpCgppZiBfX25hbWVfXyA9PSAiX19tYWluX18iOgogICAgZ3VhcmRhX2pzb24oKQogICAgbG5vdGFzID0gcmVjdXBlcmFfanNvbigpCiAgICBmb3Igbm90YSBpbiBsbm90YXM6CiAgICAgICAgcHJpbnQobm90YSkK