Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
StaSh for Pythonista 2 and 3
-
@ywangd Never mind, I stopped getting the error without changing anything
-
@disorientedp said:
@ywangd Never mind, I stopped getting the error without changing anything
It is likely that the old stash related modules were still cached after updating. Restart Pythonista will clear the cache and fix the problem (as indicated by the last message printed by
selfupdate
). -
@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
wget
which then affectspip
andgit
. The issue is that I could not figure out how to download a zip file from GitHub usingurllib3
. I tried following code, but theread
method 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 ...
I tried
requests
as well withrequests.get(ZIP_FILE_URL).raw.read()
. It returns empty string as well. I guess this is not a surprise asrequests
is a wrapper ofurllib3
.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
urllib3
not2
. It actually worked fine withurllib2
. Sinceurllib2
is removed from Python 3,urllib3
seems to be the logical alternative. Theurllib
family just seem to have so many incompatibilities between versions. -
@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
raw
property is in fact aurllib3
response object. So above code should behave the same as usingurllib3
. I do get content if I callr2.text
. But this downloads the entire file at once which is not what I am after. -
I usually use
requests
like 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
raw
does, but it's empty on my macbook as well. Trytext
. 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_content
and it callsself.raw.stream(....)
. But if I try this withurllib3
directly, it still won't work ... anyway, it is not important as onlyrequests
is designed for humans ... -
@Webmaster4o Aside: When you're dealing with binary data (zip files, images...), you should use
r.content
instead ofr.text
-
-
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
py3
branch.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.
-
@JonB Dulwich supports at least to Python 3.4 But Gittle has no Python 3 support. Any idea how the
git
tools could be ported to Pythonista 3?The python-future project has an experimental feature to automatically translate Python 2 modules to Python 3 upon loading. But I am not sure how reliable it is.
The main framework of StaSh is working now. I am in the process to make sure all the commands still work. I have ran
git
through some 2to3 tools so itself is now mostly Python 3 compatible. But the dependencies could be the tricky part. -
This post is deleted!