dynamic type returning NameError:
Hi,
I hope that this hasn't been asked for the millionth time, so my apologies if
it has.
I have a base class (BaseClass - we'll call it for this example) with an http
call that i would like to inherit into a dynamic class at runtime. We'll call
that method in BaseClass; 'request'.
I have a dictionary(json) of key (class name): value(method) that I would like
to create inheriting this 'request' method from the BaseClass. So the derived
class would look something like this
definition in json:
{"Whatever": [{"method1": "Some Default", "async": True},{"method2": "Some
Other Default", "async": True}]}
Ideally I'd like the class def to look something like this if i were to type it
out by hand
[excuse the indents]
class Whatever(BaseClass):
def method1(self):
stupid_data = super(Whatever, self).request("method1")
return stupid_data
def method2(self):
stupid_data = super(Whatever, self).request("method1")
return stupid_data
Now, I've been trying to do this using the python cli, with out success.
So, attempting this at runtime I get a plethora of wonderful errors that I
suspect has broken my brain.
Here is what i've tried:
# trying with just an empty object of type BaseClass
obj = type("Object", (BaseClass,), {})
whatever = type("WhatEver", (obj,), {"method1": super(WhatEver,
self).request("method1")})
but when i try this I get 'NameError: name 'self' is not defined'
defining these classes manually works...
I hope that this was clear enough, apologies if it wasn't. It's late(ish), I'm
tired and borderline frustrated :) But enough about me...
Thanks in advance.
--
http://mail.python.org/mailman/listinfo/python-list
Re: dynamic type returning NameError:
On Sunday, July 28, 2013 10:51:57 PM UTC-4, Terry Reedy wrote:
> On 7/28/2013 9:38 PM, Tim O'Callaghan wrote:
>
> > Hi,
>
> >
>
> > I hope that this hasn't been asked for the millionth time, so my apologies
> > if it has.
>
> >
>
> > I have a base class (BaseClass - we'll call it for this example) with an
> > http call that i would like to inherit into a dynamic class at runtime.
> > We'll call that method in BaseClass; 'request'.
>
> >
>
> > I have a dictionary(json) of key (class name): value(method) that I would
> > like to create inheriting this 'request' method from the BaseClass. So the
> > derived class would look something like this
>
> >
>
> > definition in json:
>
> > {"Whatever": [{"method1": "Some Default", "async": True},{"method2": "Some
> > Other Default", "async": True}]}
>
> >
>
> > Ideally I'd like the class def to look something like this if i were to
> > type it out by hand
>
> >
>
> > [excuse the indents]
>
> >
>
> > class Whatever(BaseClass):
>
> > def method1(self):
>
> > stupid_data = super(Whatever, self).request("method1")
>
> > return stupid_data
>
> >
>
> > def method2(self):
>
> > stupid_data = super(Whatever, self).request("method1")
>
> > return stupid_data
>
> >
>
> > Now, I've been trying to do this using the python cli, with out success.
>
> >
>
> > So, attempting this at runtime I get a plethora of wonderful errors that I
> > suspect has broken my brain.
>
> >
>
> > Here is what i've tried:
>
> >
>
> > # trying with just an empty object of type BaseClass
>
> > obj = type("Object", (BaseClass,), {})
>
> >
>
> > whatever = type("WhatEver", (obj,), {"method1": super(WhatEver,
> > self).request("method1")})
>
>
>
> 'method1' has to be mapped to a function object.
But isn't that what calling super is doing? Calling the function object of the
parent class BaseClass?
>
> > but when i try this I get 'NameError: name 'self' is not defined'
>
> >
>
> > defining these classes manually works...
>
> >
>
> > I hope that this was clear enough, apologies if it wasn't. It's late(ish),
> > I'm tired and borderline frustrated :) But enough about me...
>
> >
>
> > Thanks in advance.
>
> >
>
>
>
>
>
> --
>
> Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list
Re: dynamic type returning NameError:
On Monday, July 29, 2013 1:43:39 AM UTC-4, Steven D'Aprano wrote:
> On Sun, 28 Jul 2013 18:38:10 -0700, Tim O'Callaghan wrote:
>
>
>
> > Hi,
>
> >
>
> > I hope that this hasn't been asked for the millionth time, so my
>
> > apologies if it has.
>
> [...]
>
> > I hope that this was clear enough, apologies if it wasn't.
>
>
>
> Clear as mud.
>
Alright, let me see if I can clear this up. And by the way, thanks for chiming
in on this. It's appreciated.
I have a 3rd party api definition that I'm using to generate python classes
from so that I can access this api using python. The api definition currently
changes, so what I've done is saved a local copy of the html (the api
definition from the vendor) and screen scraped the categories, and methods for
this api. So when the api changes, I can just get a fresh definition from the
vendors site, parse the html, and generate the classes again. This screen scape
is saved to a json object in the format I originally mentioned:
returned from json from screen scrape:
{"Whatever": [{"method1": "Some Default", "async": "True"},{"method2": "Some
Other Default", "async": "True"}]}
**note:
"method1": "Some Default"
"method2": "Some Other Default"
are just dummy values.
**
>
>
> > It's late(ish), I'm tired and borderline frustrated :)
>
>
>
> I see your smiley, but perhaps you would get better results by waiting
>
> until you can make a better post.
>
>
>
> It *really* helps if you post actual "working" (even if "working" means
>
> "fails in the way I said"), *short*, *simple* code. Often you'll find
>
> that trying to simplify the problem gives you the insight to solve the
>
> problem yourself.
>
>
>
> http://www.sscce.org/
>
I would normally post 'working' code, but I'm really not there yet. All I've
been doing up until this point is basically proof of concept.
>
>
> I'm going to try to guess what you're attempting, but I may get it
>
> completely wrong. Sorry if I do, but hopefully you'll get some insight
>
> even from my misunderstandings.
>
>
>
>
>
> > I have a base class (BaseClass - we'll call it for this example) with an
>
> > http call that i would like to inherit into a dynamic class at runtime.
>
> > We'll call that method in BaseClass; 'request'.
>
>
>
> If I read this literally, you want to do this:
>
>
>
> class BaseClass(DynamicParent):
>
> def request(self):
>
> ...
>
>
>
> except that DynamicParent isn't known until runtime. Am I close?
The parent is the stable/static part. That has the http request method to
communicate with the vendor api. The request method in BaseClass creates the
request(signs and authorizes the call).
Right now the BaseClass.request("api_call_to_vendor") will work and return
data, but again I would like to separate each api category into classes with
the appropriate methods.
>
> Obviously the above syntax won't work, but you can use a factory:
>
>
>
> def make_baseclass(parent):
>
> class BaseClass(parent):
>
> def request(self):
>
> ...
>
> return BaseClass
>
>
>
> class Spam: ...
>
>
>
> BaseClass = make_baseclass(Spam)
>
>
>
>
>
> Or you can use the type() constructor directly:
>
>
>
> BaseClass = type('BaseClass', (Spam,), dict_of_methods_and_stuff)
>
This, on the surface is what I'm after. Except the 'dict_of_methods_and_stuff'
call would be something like this:
Vendor_API_Cateogry = type("Vendor_API_Category", (BaseClass,),
{"api_call_from_vendors_category":
"call_supers_request_method_passing_in_vendor_call"})
resulting in a call something like this:
vendor_category = Vendor_API_Category()
vendor_category.api_call_from_vendors_category()
>
> which is probably far less convenient. But all this assumes I read you
>
> literally, and reading on, I don't think that's what you are after.
>
>
>
>
>
> > I have a dictionary(json) of key (class name): value(method) that I
>
> > would like to create inheriting this 'request' method from the
>
> > BaseClass. So the derived class would look something like this
>
> >
>
> > definition in json:
>
> > {"Whatever": [{"
