StaSh for Pythonista 2 and 3
@ihf The fix is now merged into the
masterbranch along with some other improvements. You can get the update by running
disorientedp last edited by
@ywangd On the latest version of StaSh, I get an error message that makes it unusable:
Traceback (most recent call last): File "/private/var/mobile/Containers/Shared/AppGroup/9BC2C58D-632D-4A13-9539-7CC3C7AA159A/Documents/launch_stash.py", line 36, in <module> reload(stash) File "/private/var/mobile/Containers/Shared/AppGroup/9BC2C58D-632D-4A13-9539-7CC3C7AA159A/Documents/site-packages/stash/stash.py", line 34, in <module> from system.shcommon import IN_PYTHONISTA, ON_IPAD ImportError: cannot import name IN_PYTHONISTA
disorientedp last edited by
@ywangd Never mind, I stopped getting the error without changing anything
Olaf last edited by
@ywangd, I sent you a pull request for an improved version of
edit. Please let me know if you don't receive or if I've erred otherwise in the process; I'm an amateur on GitHub ;-(
So the porting came along quicker than I expected. The py3 branch can at least start in Pythonista 3.
However many commands still not working, esepcially
wgetwhich then affects
git. The issue is that I could not figure out how to download a zip file from GitHub using
urllib3. I tried following code, but the
readmethod always returns an empty string !!??
import urllib3 http = urllib3.PoolManager() r = http.request('GET', 'https://github.com/ywangd/stash/archive/master.zip') # So far so good, but then nothing can be read from the response object print(r.read()) # this gives an empty string print(r.read(999)) # empty string as well ...
requestsas well with
requests.get(ZIP_FILE_URL).raw.read(). It returns empty string as well. I guess this is not a surprise as
requestsis a wrapper of
Any help is appreciated.
@Webmaster4o Thanks for the help. Unfortunately it didn't solve the issue. Still getting empty strings.
The problem that I am experiencing is with
2. It actually worked fine with
urllib2is removed from Python 3,
urllib3seems to be the logical alternative. The
urllibfamily just seem to have so many incompatibilities between versions.
Webmaster4o last edited by Webmaster4o
@ywangd What was your code using
requests? That's usually easier to handle ;)
@Webmaster4o I used the requests module as follows and try to read a chunk of data at a time.
import requests r2 = requests.get('https://github.com/ywangd/stash/archive/master.zip') print(repr(r2.raw.read())) print(repr(r2.raw.read(999)))
The read calls return empty strings as well. I noticed the
rawproperty is in fact a
urllib3response object. So above code should behave the same as using
urllib3. I do get content if I call
r2.text. But this downloads the entire file at once which is not what I am after.
omz last edited by
I usually use
requestslike this to download a file without having to load it in memory completely. Works in Python 2 and 3.
import requests url = 'https://github.com/ywangd/stash/archive/master.zip' r = requests.get(url, stream=True) with open('dest.zip', 'wb') as f: for chunk in r.iter_content(1024): f.write(chunk)
I'm not sure what
rawdoes, but it's empty on my macbook as well. Try
text. Here's a comparison:
>>> import requests >>> r=requests.get('https://github.com/ywangd/stash/archive/master.zip') >>> r.text[:100] 'PK\x03\x04\n\x00\x00\x00\x00\x00ۀNH\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\r\x00\t\x00stash-master/UT\x05\x00\x01�\x16�VPK\x03\x04\n\x00\x00\x00\x08\x00ۀNHf��?�\x01\x00\x00\x1e\x03\x00\x00\x17\x00\t\x00stash-master/.gitign' >>> r.raw.read()[:100] b''
@omz Beat me to it 😄
@omz It worked! Thanks for helping out on this noob issue. :)
Now I am reading the source code of
iter_contentand it calls
self.raw.stream(....). But if I try this with
urllib3directly, it still won't work ... anyway, it is not important as only
requestsis designed for humans ...
omz last edited by omz
@Webmaster4o Aside: When you're dealing with binary data (zip files, images...), you should use
dgelessus last edited by
ccc last edited by
Why not just switch to requests for all of this? It works in both Python 2 and 3 and is far easier and more reliable to use than the alternatives. At the last Pycon, the consensus was to move requests into the Standard Library but the maintainer declined because he wants to keep the innovations and bug fixes coming at a faster pace than the Standard Library process would allow.
If you moved to requests, what functionality would still be missing?
@dgelessus Thanks for the information. Didn't know that.
BTW, to test the Python 3 port, run following one-liner to get the
import requests as r; exec(r.get('http://j.mp/gsp35').text)
@ccc I have now switched to requests following omz's example code. It's indeed much friendly than urllib.