I want to store dateutil.rrule objects to a database and recreate them after reading from the database.
Given the following issue, I think I need to use a workaround. Python dateutils print recurrence rule according to iCalendar format (see RFC 5545)
I am thinking of storing the output of myrrule.dict to the database as a string and then recreate the rrule object when required.
This is how the dict looks like:
{'_cache_complete': False, '_byhour': (0,), '_wkst': 0,
'_timeset': (datetime.time(0, 10),), '_bysecond': (0,),
'_bymonthday': (), '_byweekno': None, '_bysetpos': None,
'_cache': None, '_bymonth': None, '_bynweekday': ((4, 3),),
'_tzinfo': None, '_byyearday': None, '_byweekday': None,
'_byminute': (10,), '_len': 10, '_until': None,
'_bynmonthday': (), '_dtstart': datetime.datetime(2012, 10, 13, 0, 10),
'_count': 10, '_freq': 1, '_interval': 1, '_byeaster': None}
Is it a good idea? Any other suggestions?
How do I recover the python object from the dictionary? Is python setattr() on my best bet or is there something easier?
Should I consider using something like this instead? https://stackoverflow.com/a/1305663/161628
What you need is python's pickle
module. The pickle
module is a simple serialization module which can convert any python object into a string. The string can later be de-serialized back to the original object.
import pickle
serial_str = pickle.dumps(your_rrule_object)
db.store(serial_str)
...
serial_str = db.retrieve()
new_rrule_object = pickle.loads(serial_str)
Check the documentation for the pickle module for more details.