Skip to content

Implicit data conversions and changes to an object - should we change the API? #613

@tobixen

Description

@tobixen

Today an object can be edited like this:

my_event = calendar.search(...)[0]
my_event.icalendar_component['summary'] = "This is the edited summary"
my_event.save()

This was my invention, so of course I like it - but there are troubles here. First of all, the side effect. Consider those code-lines:

my_event.data
my_event.icalendar_instance
my_event.vobject_instance
my_event.data

It looks like a noop, but this little sequence of commands will efficiently convert the data from a string to an icalendar_instance then to a vobject_instance (via a string), and finally back to a string again. This may cause some problems:

  • While the semantics of the object shouldn't be changed by the conversions, the actual data representation may change.
  • Ii've grown up in an age where it was important to optimize things not to use too many CPU-cycles and memory. The conversions above makes me cringe a bit - I would guess quite much CPU is spent on wasteful work here.
  • In quite some of the internal methods I was earlier always using my_event.data to avoid conversions - but at some point I gave up, after all we do have an icalendar library for doing icalendar operations for us. Meaning that just internally an object may be tossed from one format to another during ordinary usage of the library.
  • The data conversion may also cause a data loss in some corner situations. The code below will NOT edit the summary:
my_event = calendar.search(...)[0]
icalendar_component = my_event.icalendar_component['summary']
my_event.data ## now icalendar_component is no longer connected to my_event!
icalendar_component['summary']  = "This is the edited summary"
my_event.save()

Should the API be changed now that before we're releasing 3.0?

I do remember @niccokunzmann reaching out to me earlier on this topic having some thoughts about it, but I can't find back to the conversation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions