What does this code do? Also, testing decorator via terminal?

57 views
Skip to first unread message

Micky Hulse

unread,
Sep 1, 2012, 12:17:10 AM9/1/12
to django...@googlegroups.com
Hello,

Silly question, but...

What does this code do:

if isinstance(objects, HttpResponse):
return objects

... full code found here:

<https://github.com/julian-amaya/django-decorators/blob/master/django_decorators/decorators.py#L37-38>

I'd like to add:

if objects.status_code != 200:
return objects

... which is found via this code:

<https://gist.github.com/871954>

In other words, the final code would look like so (untested):

<https://gist.github.com/3563671>

I just don't fully understand what the `isinstance()` bit is doing. :(

Also, how can I test decorators via the python shell?

Sorry if noob questions.

Thanks!
M

Micky Hulse

unread,
Sep 1, 2012, 1:19:08 AM9/1/12
to django...@googlegroups.com
On Fri, Aug 31, 2012 at 9:17 PM, Micky Hulse <rgm...@gmail.com> wrote:
> I'd like to add:
> if objects.status_code != 200:
> return objects
> ... which is found via this code:
> <https://gist.github.com/871954>

Doh! After a bit of trial and error, I given up on trying to merge
those two code snippets.

I'd still like to know what `if isinstance(objects, HttpResponse)` is doing?

I've RTM'd:

<http://docs.python.org/library/functions.html#isinstance>

and

<https://code.djangoproject.com/wiki/HttpResponse>

... but what doe these bits of code accomplish when working together?

Thanks for listening. :)

M

Kurtis Mullins

unread,
Sep 1, 2012, 2:38:31 AM9/1/12
to django...@googlegroups.com
On Fri, Aug 31, 2012 at 9:17 PM, Micky Hulse <rgm...@gmail.com> wrote:
> I'd like to add:
> if objects.status_code != 200:
>         return objects
> ... which is found via this code:
> <https://gist.github.com/871954>

Doh! After a bit of trial and error, I given up on trying to merge
those two code snippets.

I'd still like to know what `if isinstance(objects, HttpResponse)` is doing?

All that is doing is checking to see if the object named "objects" is an HttpResponse object.

According to this doc (https://docs.djangoproject.com/en/dev/ref/request-response/#id4) it appears that the objects.status_code *should* work, although I'm not sure if it returns a number or a string. I'm not sure why this isn't working for you, though.
 

Micky Hulse

unread,
Sep 1, 2012, 3:17:32 AM9/1/12
to django...@googlegroups.com
Hi Kurtis! Many thanks for your reply, I really appreciate the help! :)

On Fri, Aug 31, 2012 at 11:38 PM, Kurtis Mullins
<kurtis....@gmail.com> wrote:
> All that is doing is checking to see if the object named "objects" is an
> HttpResponse object.

Ah, ok! I think you've kicked me in the right direction here.

> that the objects.status_code *should* work, although I'm not sure if it
> returns a number or a string. I'm not sure why this isn't working for you,
> though.

I think it's not working for me because, in my view, I'm not returning:

return HTTPResponse(json.dumps(d), content_type='application/json')

... instead, I'm returning:

return {'this will be': 'JSON'}

Based on your reply, I think I now understand why the original author is using:

if isinstance(objects, HttpResponse):

... meaning, "don't do anything with `objects` if the view is already
passing an HTTPResponse". Essentially, that logic is just trying to
make sure that the view passes the right type of object to the
decorator... Correct? If so, then that's lesson learned for me: Never
try to combine two decorators before fully understanding how the code
works! :D

I thought I was being cool by trying to combine the two... But now I'd
rather spend my time learning how to convert this decorator so that it
works on CBVs.

Thanks again for the help Kurtis, I really appreciate it!!!!

Cheers,
Micky

Melvyn Sopacua

unread,
Sep 2, 2012, 1:47:20 AM9/2/12
to django...@googlegroups.com
On 1-9-2012 8:38, Kurtis Mullins wrote:
>>
>> On Fri, Aug 31, 2012 at 9:17 PM, Micky Hulse <rgm...@gmail.com> wrote:
>>> I'd like to add:
>>> if objects.status_code != 200:
>>> return objects
>>> ... which is found via this code:
>>> <https://gist.github.com/871954>
>>
>> Doh! After a bit of trial and error, I given up on trying to merge
>> those two code snippets.
>>
>> I'd still like to know what `if isinstance(objects, HttpResponse)` is
>> doing?
>
>
> All that is doing is checking to see if the object named "objects" is an
> HttpResponse object.

More precise, if it's an object instance that has HttpResponse as a
class in it's inheritance chain.
The goal of this check is to be sure you can perform certain operations
on it. You can of course just perform these operations and if the
operation isn't supported then just let it blow up, but doing an
isinstance check allows you to catch the error earlier and give a better
diagnostic to the end user.
There's also a negative to the check: If I have good reasons not to
subclass HttpResponse but can still make it behave like HttpResponse,
then this check is working against me.

--
Melvyn Sopacua

Micky Hulse

unread,
Sep 4, 2012, 10:41:23 PM9/4/12
to django...@googlegroups.com
On Sat, Sep 1, 2012 at 10:47 PM, Melvyn Sopacua <m.r.s...@gmail.com> wrote:
> More precise, if it's an object instance that has HttpResponse as a
> class in it's inheritance chain.
> <snip>

Thanks Melvyn! I really appreciate the help and description/clarification. :)
Reply all
Reply to author
Forward
0 new messages