spotfiresoftware / spotfire-python

Package for Building Python Extensions to Spotfire®
Other
18 stars 6 forks source link

sbdf_file type #63

Open jagrata opened 8 months ago

jagrata commented 8 months ago

In the sbdf class, on line 1888 there is the presumption that the sbdf_file provided is a string filename. One might like to use the function by passing in a io.BytesIO object to avoid having to create a file and then just read back the contents.

Can we update it to allow file-like objects be passed in in place of filenames?

For example, line 1888 in sbdf.pyx could reflect something like this:

    # Open the SBDF file if necessary
    if isinstance( sbdf_file, str ):
        output_file = _pathlike_to_fileptr(sbdf_file, "wb")
    else:
        output_file = sbdf_file
bbassett-tibco commented 8 months ago

Hi, @jagrata! The reason the SBDF module behaves like this (i.e., working with paths instead of any Python io objects) is that the implementation is using a native C library for SBDF operations that uses the C standard-IO functions (fopen/fread/fwrite/et al), which have no easy way to extend to working with io objects. Any support would be almost exactly like what you described: using tempfile.NamedTemporaryFile to create a temp file, export the data to it with sbdf.export_data(), rewind the temp file, and read it into the io.BytesIO object. Directly updating the native library to have a level of abstraction to allow what you is looking for might also have the effect of impacting read/write performance (losing perf that was the primary reason for using the native library).