Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Peter Otten
Juan Christian wrote: > On Fri, Sep 19, 2014 at 4:30 PM, Peter Otten <__pete...@web.de> wrote: >> >> Well, you import closing from the stdlib with >> >> from contextlib import closing >> >> and then proceed to write your own closing >> >> @contextmanager >> def closing(thing): >> try: >>

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Juan Christian
On Fri, Sep 19, 2014 at 4:30 PM, Peter Otten <__pete...@web.de> wrote: > > Well, you import closing from the stdlib with > > from contextlib import closing > > and then proceed to write your own closing > > @contextmanager > def closing(thing): > try: > yield thing > finally: >

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Peter Otten
Juan Christian wrote: > This is the code using the contextlib (more than 105 lines): > http://pastebin.com/jVhz7Ta6 > > But I'm getting another (super confusing) error now: > > [#] [Tf2 inventory [W]CS GO Skins > Author: Pnoy Paragon << UNTIL HERE, OK! > > Traceback (most recent call la

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Juan Christian
On Fri, Sep 19, 2014 at 3:53 PM, Peter Otten <__pete...@web.de> wrote: > > If I understand you correctly you can test for the presence of a value. > Example: > > api = response.json()['response']['players'][steamID64] > status["steamrep_scammer"] = "steamrep_scammer" in api Thanks, this way I can

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Juan Christian
This is the code using the contextlib (more than 105 lines): http://pastebin.com/jVhz7Ta6 But I'm getting another (super confusing) error now: [#] [Tf2 inventory [W]CS GO Skins Author: Pnoy Paragon << UNTIL HERE, OK! Traceback (most recent call last): File "C:\Users\Juan Christian\Desk

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Peter Otten
Juan Christian wrote: > On Fri, Sep 19, 2014 at 3:15 PM, Peter Otten <__pete...@web.de> wrote: >> >> Let's take a step back: if you were to write >> >> > with requests.get(''.join([BACKPACKTF, steamID64])) as response: >> > status = {'profile': ''.join([BACKPACKTF, steamID64]),'backpack_value': >>

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Danny Yoo
>> class AutoClosable(object): [cut] > There is also > > Ah, much better! Thanks Peter. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: h

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Juan Christian
On Fri, Sep 19, 2014 at 3:15 PM, Peter Otten <__pete...@web.de> wrote: > > Let's take a step back: if you were to write > > > with requests.get(''.join([BACKPACKTF, steamID64])) as response: > > status = {'profile': ''.join([BACKPACKTF, steamID64]),'backpack_value': > > 'Private or invalid', 'steam

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Peter Otten
Danny Yoo wrote: > I should have added that you can write your own context manager for > requests; it shouldn't be too bad. I'd expect the helper code to be > something like: > class AutoClosable(object): > def __init__(self, closable): > self.closable = closable > > def __enter

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Danny Yoo
I should have added that you can write your own context manager for requests; it shouldn't be too bad. I'd expect the helper code to be something like: # class AutoClosable(object): def __init__(self, closable): self.closable = closable def

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Peter Otten
Juan Christian wrote: > This part of my code isn't working: > > def check_backpacktf(steamID64): > with requests.get(''.join([BACKPACKTF, steamID64])) as response: > status = {'profile': ''.join([BACKPACKTF, steamID64]),'backpack_value': > 'Private or invalid', 'steamrep_scammer': False} > > wit

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Joel Goldstick
On Fri, Sep 19, 2014 at 2:00 PM, Juan Christian wrote: > On Fri, Sep 19, 2014 at 2:50 PM, Danny Yoo wrote: >> >> So Python's "with" appears to require a bit more out the thing being >> managed than the similar try-with-resources in Java. >> >> (http://docs.oracle.com/javase/tutorial/essential/exc

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Juan Christian
On Fri, Sep 19, 2014 at 2:50 PM, Danny Yoo wrote: > > So Python's "with" appears to require a bit more out the thing being > managed than the similar try-with-resources in Java. > ( > http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html > ) > Is there anything simila

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Danny Yoo
> Traceback (most recent call last): > File "C:\[...]\shark.py", line 98, in > main(sys.argv[1]) > File "C:\[...]\shark.py", line 93, in main > fetch_forum() > File "C:\[...]\shark.py", line 79, in fetch_forum > fetch_user(user_url) > File "C:\[...]\shark.py", line 42, in fetch

Re: [Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Joel Goldstick
On Fri, Sep 19, 2014 at 1:30 PM, Juan Christian wrote: > This part of my code isn't working: > > def check_backpacktf(steamID64): > with requests.get(''.join([BACKPACKTF, steamID64])) as response: I'm not sure why my reply mangled your indents, but can you check to see that your "".join() is gett

[Tutor] Using 'requests' + 'with statement' in Python 3.4.1

2014-09-19 Thread Juan Christian
This part of my code isn't working: def check_backpacktf(steamID64): with requests.get(''.join([BACKPACKTF, steamID64])) as response: status = {'profile': ''.join([BACKPACKTF, steamID64]),'backpack_value': 'Private or invalid', 'steamrep_scammer': False} with response.json()['response']['players'