poliastro / czml3

Python 3 library to write CZML
https://pypi.org/project/czml3/
MIT License
38 stars 30 forks source link

Removing white space from document output #82

Closed ckoval7 closed 3 years ago

ckoval7 commented 3 years ago

First, thank you for this awesome library. My project wouldn't be anywhere near what it is without it.

Is there a good way to remove white space from the document? I found a way to do it, but I feel like it's inefficient/has extra steps. output = json.dumps(json.loads(str(Document([top] + packets))), separators=(',', ':'))

Removing white space turns a 24MB CZML file into a 9MB CZML file.

astrojuanlu commented 3 years ago

Thanks for your kind words @ckoval7 ! I'm so happy you're finding czml3 useful.

It makes sense to have an option to procure compact (instead of pretty) output. Will try to take a look within the next two weeks.

abhaykatheria commented 3 years ago

Hi @astrojuanlu I would love to help out with this issue :). I think removing the indent here -

class BaseCZMLObject:
    def __str__(self):
        return self.dumps(indent=4)

will do the job and I think this string dunder method should not have an indent field because if we look at when python dictionaries are converted to string they seem to ignore the indents. We can move this current functionality in some new method.

astrojuanlu commented 3 years ago

To clarify, this is the current behavior:

>>> from czml3 import Packet
>>> packet0 = Packet(id="Facility/AGI", name="AGI")
>>> packet0
Packet(id='Facility/AGI', delete=None, name='AGI', parent=None, description=None, availability=None, properties=None, position=None, orientation=None, viewFrom=None, billboard=None, box=None, corridor=None, cylinder=None, ellipse=None, ellipsoid=None, label=None, model=None, path=None, point=None, polygon=None, polyline=None, rectangle=None, tileset=None, wall=None)
>>> print(packet0)
{
    "id": "Facility/AGI",
    "name": "AGI"
}

@ckoval7 @abhaykatheria This works with the current version of czml3. Is it enough for your needs?

>>> print(packet0.dumps(separators=(',', ':')))
{"id":"Facility/AGI","name":"AGI"}
ckoval7 commented 3 years ago

Works on Document too, which is what I needed. Thanks!

astrojuanlu commented 3 years ago

Thanks a lot @ckoval7 !