How to recover python dateutil.rrule object from dictionary string?

I want to store dateutil.rrule objects in a database and recreate them after reading from the database.

Given the following problem, I think I need to use a workaround. Python dateutils print repeat rule according to iCalendar format (see RFC 5545)

I am going to save myrrule output. dict to the database as a string, and then, if necessary, recreate the rrule object.

This is what 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 that a good idea? Any other suggestions?

How to restore python object from dictionary? Is python setattr () a better option or something simpler?

Should I use something like this instead? stack overflow

+4
source share
1 answer

You need a python pickle module. The pickle module is a simple serialization module that can convert any python object to 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) 

See the documentation for the brine module for details.

+2
source

Source: https://habr.com/ru/post/1439511/


All Articles