oasis-open / cti-python-stix2

OASIS TC Open Repository: Python APIs for STIX 2
https://stix2.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
356 stars 113 forks source link

fp write for STIX Objects #500

Closed emmanvg closed 3 years ago

emmanvg commented 3 years ago

Adding this piece of code would help write more flexible code while also providing the same options as serialize()

emmanvg commented 3 years ago

I started with having both under a same function, but I was doubtful about having two different behaviors (one that returned a string and one that returned None). Based on the same idea that json.dumps() and json.dump() are essentially the same except for the required fp argument and what each return, I thought it would be better to also keep them separate on stix2 too.

codecov-io commented 3 years ago

Codecov Report

Merging #500 (c2d360d) into master (f155e3e) will decrease coverage by 0.00%. The diff coverage is 90.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #500      +/-   ##
==========================================
- Coverage   89.47%   89.47%   -0.01%     
==========================================
  Files         147      147              
  Lines       16554    16599      +45     
==========================================
+ Hits        14812    14852      +40     
- Misses       1742     1747       +5     
Impacted Files Coverage Δ
stix2/base.py 74.70% <50.00%> (-0.20%) :arrow_down:
stix2/datastore/filesystem.py 75.90% <66.66%> (-0.10%) :arrow_down:
stix2/serialization.py 70.96% <71.42%> (-0.97%) :arrow_down:
stix2/test/v20/test_bundle.py 100.00% <100.00%> (ø)
stix2/test/v21/test_bundle.py 100.00% <100.00%> (ø)
stix2/datastore/taxii.py 69.60% <0.00%> (-0.15%) :arrow_down:
stix2/test/v20/test_datastore_taxii.py 100.00% <0.00%> (ø)
stix2/test/v21/test_datastore_taxii.py 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update f155e3e...c2d360d. Read the comment docs.

chisholm commented 3 years ago

I didn't mean to combine them into one function. It was more like change the serialize() function to be something like:

with io.StringIO() as fp:
    fp_serialize(obj, fp)
    return fp.getvalue()

So the "main" code for serializing lives in one place, the two functions can't get out of sync, the serialize() implementation is shorter and simpler, etc. Just a thought.

emmanvg commented 3 years ago

I misinterpreted your prior message. It is a good suggestion...