Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-1823

Add mechanism to encode any iterable without inflating to a list

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None

      In PYTHON-1528, salty-horse suggested adding support for encoding sets, ranges, frozenset, and various other built-in (and iterable) objects by default. We decided that doing so could mask bugs because BSON cannot preserve these objects when decoding.

      Applications that want to encode these types can use the TypeEncoder API added in PYTHON-1476 to convert these types to a list. This approach allows sets to be encoded as a bson array but it has the unfortunate side effect of temporarily inflating the entire set/iterable to a single python list.

      It would be more efficient to iterate over the set while encoding it. One way we could accomplish this is by adding a new bson module type bson.Iterable to wrap the object. Something like this:

      >>> from bson import BSON, Iterable
      >>>
      >>> bson_bytes = BSON.encode({'ints': Iterable(range(5))})
      >>> bson_bytes.decode()
      {'ints': [0, 1, 2, 3, 4]}
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: