4

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

Community
  • 1
  • 1
Rohit Banga
  • 18,458
  • 31
  • 113
  • 191

1 Answers1

2

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.

phininity
  • 1,673
  • 2
  • 14
  • 9
  • care should be taken about line folding http://www.apps.ietf.org/rfc/rfc2445.html#sec-4.1 – Auberon Vacher Oct 25 '12 at 13:55
  • @oberron I don't think your comment applies here because it's not being serialized into the iCalendar grammar. Instead it's the python object structure/content being serialized into a format that has nothing to do with iCalendar – User Oct 26 '13 at 23:20