Two things:
1. I'm a moron.
2. You ppl are AWESOME!
That very simple function took care of everything.
Thanks!
On Thu, Apr 3, 2008 at 1:23 PM, Alan Kennedy <jython-users@...> wrote:
> [Michael]
>
> > I'm using a Java API for SourceForge enterprise.. if you want to
> > 'grab' a file from it you make a call that grabs the file
> > incrementally:
> >
> > partialFile=docfile.read(sessionid, documentid, offset, 16384)
> >
> > .. partialFile is suppose to be a Java byte array that contains up to
> > 16384 bytes of that file I'm downloading..
> >
> > How am I supposed to convert that to string so I can write it to disk?
>
> This code should be informative
>
> # -------------------------------
> def the_python_way(byte_array, encoding="utf-8"):
> char_array = [chr(byte) for byte in byte_array]
> result = "".join(char_array)
> if encoding:
> return result.decode(encoding)
> else:
> return result
>
> def the_java_way(binary_data, encoding='utf-8'):
> from java.lang import String
> if encoding:
> return String(binary_data)
> else:
> return String(binary_data, encoding)
>
> def get_binary_data():
> # download from sourceforge
> # binary_array = docfile.read(sessionid, documentid, offset, 16384)
> import jarray
> return jarray.array([ord(b) for b in "My binary data\n"], 'b')
>
> def timeit(binary_data, method, iterations):
> import time
> start = time.time()
> for ix in xrange(iterations):
> data_string = method(binary_data)
> total_msec = time.time() - start
> print "Method %s, total %1.3lf msec, per iter %1.6lf msec" % \
> (str(method), total_msec, total_msec/iterations)
>
> def save_data(filename, data_string, encoding="utf-8"):
> if encoding:
> import codecs
> outfile = codecs.open(filename, "wb", encoding)
> else:
> outfile = open(filename, "wb")
> outfile.write(data_string)
> outfile.close()
>
> if __name__ == "__main__":
> # timeit(get_binary_data(), the_python_way, 100000)
> # timeit(get_binary_data(), the_java_way, 100000)
> save_data("bytes.dat", the_java_way(get_binary_data()))
>
> # -------------------------------
>
> As I expected, the java way (using java.lang.String, which is natively
> implemented) is over three times faster than the standard python way.
>
> I think the python way is easier to grok though.
|