Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting events to Python dictionaries #210

Closed
Tracked by #443
Phyks opened this issue Jan 22, 2017 · 2 comments
Closed
Tracked by #443

Converting events to Python dictionaries #210

Phyks opened this issue Jan 22, 2017 · 2 comments

Comments

@Phyks
Copy link

Phyks commented Jan 22, 2017

Hi,

I am looking for a way to convert icalendar events to Python dict of the values (back and forth between Python dict and icalendar.Event).

For now, my solution is to:

def to_dict(event):
        return dict(
            (x[0], x[1].to_ical()) for x in event.items()
        )

def from_dict(event):
        ev = icalendar.Event()
        for key in event:
            ev.add(key, event[key], encode=False)
        return event

However, this is far from perfect, especially since the resulting dict only has strings as values, and do not take benefit from the parsing abilities from icalendar. Typically, fields representing dates and times are converted to string instead of staying as datetime objects, which would be much more porwerful.

Looking at the ical.prop files and the API documentation, I could not find a way to get the "decoded" (Python typed) value from a given prop, to use in the to_dict function. I came accross #85 which is kind of similar, but strings are perfectly fine in this particular case.

Is there a way to achieve something like this?

Thanks!

EDIT: Actually, due to #127, the above solution is not even really working.
What I came with which is more closely working is

def to_dict(event):
        dict_event = {}
        for x in event.items():
            if not isinstance(x[1], icalendar.prop.vText):
                dict_event[x[0]] = x[1].to_ical().decode("utf-8")
            else:
                dict_event[x[0]] = "%s" % x[1]
        return dict_event
@untitaker
Copy link
Contributor

Decoding a value should be possible with icalendar.cal.types_factory.from_ical, but that method isn't part of the public API.

At some point I guess it'd be nice to support jCalendar, and maybe also pass jCalendar from the raw parser to cal (instead of the contentline abstraction).

@pgarmyn
Copy link

pgarmyn commented Sep 19, 2022

Using a dictionary is not appropriate seen that CALDAV events can contain multiple components (multiple "VEVENT" in recurrent events) and subcomponents ( "ATTENDEE') in one event. See page 9 in )
Dictionary's cannot have two items with the same key. Converting "BEGIN:VEVENT" to a "key:value" dictionary element wont work while the second occurrence of a "VEVEN" component will crush the first.

@collective collective locked and limited conversation to collaborators Aug 30, 2023
@niccokunzmann niccokunzmann converted this issue into discussion #543 Aug 30, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants