On Fri, Aug 28, 2009 at 4:37 PM, Joshua Russo <josh.r.ru...@gmail.com>wrote:
> On Fri, Aug 28, 2009 at 4:11 PM, Joshua Russo <josh.r.ru...@gmail.com>wrote: > >> On Thu, Aug 27, 2009 at 10:39 PM, Forest Bond <for...@alittletooquiet.net >> > wrote: >> >>> Hi, >>> >>> On Thu, Aug 27, 2009 at 07:42:24PM -0100, Joshua Russo wrote: >>> > On Thu, Aug 27, 2009 at 6:22 PM, Forest Bond < >>> for...@alittletooquiet.net> >>> > wrote: >>> > >>> > Hi, >>> > >>> > On Thu, Aug 27, 2009 at 03:28:03PM -0100, Joshua Russo wrote: >>> > > On Thu, Aug 27, 2009 at 11:54 AM, Joshua Russo < >>> josh.r.ru...@gmail.com> >>> > wrote: >>> > > Ok, so I found that the way I was 'casting' the response object >>> didn't >>> > work. Is >>> > > there no way to cast an instance of a base class to a child class >>> in >>> > Python? >>> > > >>> > > What I did was to create a class method on my child class that >>> takes the >>> > > current response instance and creates a copy of it using my new >>> response >>> > > object. >>> > > >>> > > http://dpaste.com/hold/86193/ >>> > > >>> > > Does this look appropriate, or is there a better way to do this? >>> > >>> > This should work: >>> > >>> > response.__class__ = TestHttpResponse >>> > >>> > >>> > Nope, it resets the status_code. That's what I tried at first. >>> >>> Okay, you'd have to copy the class attributes, too. Or, you could just >>> define a >>> sub-class on-the-fly: >>> >>> class _TestHttpResponse(TestHttpResponse, response.__class__): >>> pass >>> >>> response.__class__ = _TestHttpResponse >>> >>> I haven't tested this. >> >> >> That might just work. I would need to to change the base class of >> TestHttpResponse to Object, but then my class wouldn't (shouldn't) override >> the status_code in the response class. Good idea. Though I may have to wait >> until Monday to test it out. >> >> Thanks >> > > Yup that works! Thanks! > Building off your suggestion Forest I've actually built a function that is similar to a factory function. def castHttpResponse(curClass): class TestHttpResponse(curClass): def form_data(self, search_name): """ Returns the entire set of form values associated with form of the given field or form name """ if getattr(self, "_form_data", None) is None: self._form_data = self._get_form_data() if self._form_data.has_key(search_name): return self._form_data[search_name] for form_name, data in self._form_data.items(): if data.has_key(search_name): return data raise FieldNotFound("A field or form with the name %s was not found." % search_name) def _get_form_data(self): curParser = FormHTMLParser() curParser.feed(self.content) curParser.close() return curParser.forms return TestHttpResponse And then I can just call the function like so: response.__class__ = castHttpResponse(response.__class__) I think this is cleaner as it avoids the multiple inheritance. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---