Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102900 > unrolled thread

How do i instantiate a class_name passed via cmd line

Started by"Veek. M" <vek.m1234@gmail.com>
First post2016-02-14 10:10 +0530
Last post2016-02-14 16:20 +1100
Articles 8 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  How do i instantiate a class_name passed via cmd line "Veek. M" <vek.m1234@gmail.com> - 2016-02-14 10:10 +0530
    Re: How do i instantiate a class_name passed via cmd line Rick Johnson <rantingrickjohnson@gmail.com> - 2016-02-13 21:31 -0800
      Re: How do i instantiate a class_name passed via cmd line "Veek. M" <vek.m1234@gmail.com> - 2016-02-14 11:09 +0530
        Re: How do i instantiate a class_name passed via cmd line Rick Johnson <rantingrickjohnson@gmail.com> - 2016-02-13 21:55 -0800
    Re: How do i instantiate a class_name passed via cmd line Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-02-14 18:41 +1300
      Re: How do i instantiate a class_name passed via cmd line "Veek. M" <vek.m1234@gmail.com> - 2016-02-14 11:21 +0530
      Re: How do i instantiate a class_name passed via cmd line Peter Otten <__peter__@web.de> - 2016-02-14 14:48 +0100
    Re: How do i instantiate a class_name passed via cmd line Cameron Simpson <cs@zip.com.au> - 2016-02-14 16:20 +1100

#102900 — How do i instantiate a class_name passed via cmd line

From"Veek. M" <vek.m1234@gmail.com>
Date2016-02-14 10:10 +0530
SubjectHow do i instantiate a class_name passed via cmd line
Message-ID<n9p0ba$s7b$1@dont-email.me>
I'm writing a price parser. I need to do the equivalent of perl's
$$var to instantiate a class where $car is the class_name.

I'm passing 'Ebay' or 'Newegg' or 'Amazon' via cmd-line. I have a module 
named ebay.py and a class called Ebay (price parser). I do something 
like:

\> main.py ebay motherboard

and this does: 
 module = __import__(module_name)
but now i need to instantiate the class - right now I do:
 instance = module.Ebay(module_name, product)

how do i replace the 'Ebay' bit with a variable so that I can load any 
class via cmd line.

class Load(object):
    def __init__(self, module_name, product):
        try:
            module = __import__(module_name)
            instance = module.Ebay(module_name, product)
        except ImportError:
            print("Can't find module %s" % module_name)

[toc] | [next] | [standalone]


#102902

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2016-02-13 21:31 -0800
Message-ID<7142528d-6465-4825-af4f-85d6c7640b16@googlegroups.com>
In reply to#102900
On Saturday, February 13, 2016 at 10:41:20 PM UTC-6, Veek. M wrote:
> how do i replace the 'Ebay' bit with a variable so that I
> can load any class via cmd line.

Is this what you're trying to do? 

(Python2.x code)
>>> import Tkinter as tk
>>> classNames = ["Button", "Label"]
>>> root = tk.Tk()
>>> for className in classNames:
	classN = getattr(tk, className)
	instanceN = classN(root, text=className)
	instanceN.pack()
    
Note: You won't need to call "mainloop" when testing this
code on the command line, only in a script or in the IDLE
shell.

>>> root.mainloop() 

[toc] | [prev] | [next] | [standalone]


#102903

From"Veek. M" <vek.m1234@gmail.com>
Date2016-02-14 11:09 +0530
Message-ID<n9p3pb$41b$1@dont-email.me>
In reply to#102902
Rick Johnson wrote:

> On Saturday, February 13, 2016 at 10:41:20 PM UTC-6, Veek. M wrote:
>> how do i replace the 'Ebay' bit with a variable so that I
>> can load any class via cmd line.
> 
> Is this what you're trying to do?
> 
> (Python2.x code)
>>>> import Tkinter as tk
>>>> classNames = ["Button", "Label"]
>>>> root = tk.Tk()
>>>> for className in classNames:
> classN = getattr(tk, className)
> instanceN = classN(root, text=className)
> instanceN.pack()
>     
> Note: You won't need to call "mainloop" when testing this
> code on the command line, only in a script or in the IDLE
> shell.
> 
>>>> root.mainloop()
Nope - this is what i'm doing:

class Foo():
 pass

x = 'Foo'

How do i use 'x' to create an instance of class Foo?

[toc] | [prev] | [next] | [standalone]


#102906

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2016-02-13 21:55 -0800
Message-ID<e4dede26-2cb1-402f-8014-cb08575e6c74@googlegroups.com>
In reply to#102903
On Saturday, February 13, 2016 at 11:39:56 PM UTC-6, Veek. M wrote:
> Nope - this is what i'm doing:
> 
> class Foo():
>  pass
> 
> x = 'Foo'
> 
> How do i use 'x' to create an instance of class Foo?

Use the builtin function `getattr` on the module that contains
the class named "Foo". 

For example:

    module.Foo

is equivelant to:

    getattr(module, "Foo")

Here is an interactive session:

    py> import Tkinter
    py> Tkinter.Label
    <class Tkinter.Label at 0x023BE5A8>
    py> getattr(Tkinter, "Label")
    <class Tkinter.Label at 0x023BE5A8>

[toc] | [prev] | [next] | [standalone]


#102904

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2016-02-14 18:41 +1300
Message-ID<diaid4F6assU1@mid.individual.net>
In reply to#102900
Veek. M wrote:
> I'm writing a price parser. I need to do the equivalent of perl's
> $$var to instantiate a class where $car is the class_name.
> 
> I'm passing 'Ebay' or 'Newegg' or 'Amazon' via cmd-line. I have a module 
> named ebay.py and a class called Ebay (price parser). I do something 
> like:
> 
> \> main.py ebay motherboard
> 
> and this does: 
>  module = __import__(module_name)
> but now i need to instantiate the class - right now I do:
>  instance = module.Ebay(module_name, product)
> 
> how do i replace the 'Ebay' bit with a variable so that I can load any 
> class via cmd line.
> 
> class Load(object):
>     def __init__(self, module_name, product):
>         try:
>             module = __import__(module_name)
>             instance = module.Ebay(module_name, product)
>         except ImportError:
>             print("Can't find module %s" % module_name)
> 

Something like this should do it:

   instance = getattr(module, class_name)(module_name, product)

If the class name is always the same as the module name with the
first letter capitalized, you could use

   instance = getattr(module, module_name.capitalize())(module_name, product)

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#102905

From"Veek. M" <vek.m1234@gmail.com>
Date2016-02-14 11:21 +0530
Message-ID<n9p4ep$518$1@dont-email.me>
In reply to#102904
Gregory Ewing wrote:

> Veek. M wrote:
>> I'm writing a price parser. I need to do the equivalent of perl's
>> $$var to instantiate a class where $car is the class_name.
>> 
>> I'm passing 'Ebay' or 'Newegg' or 'Amazon' via cmd-line. I have a
>> module named ebay.py and a class called Ebay (price parser). I do
>> something like:
>> 
>> \> main.py ebay motherboard
>> 
>> and this does:
>>  module = __import__(module_name)
>> but now i need to instantiate the class - right now I do:
>>  instance = module.Ebay(module_name, product)
>> 
>> how do i replace the 'Ebay' bit with a variable so that I can load
>> any class via cmd line.
>> 
>> class Load(object):
>>     def __init__(self, module_name, product):
>>         try:
>>             module = __import__(module_name)
>>             instance = module.Ebay(module_name, product)
>>         except ImportError:
>>             print("Can't find module %s" % module_name)
>> 
> 
> Something like this should do it:
> 
>    instance = getattr(module, class_name)(module_name, product)
> 
> If the class name is always the same as the module name with the
> first letter capitalized, you could use
> 
>    instance = getattr(module, module_name.capitalize())(module_name,
>    product)
> 
Ah! i see - clever!

'getattr' returns the class object with class_name=whatever
and we can instantiate now that we have a class object - nice - thanks 
guys - the bell should have rung from Rick's example.

[toc] | [prev] | [next] | [standalone]


#102916

FromPeter Otten <__peter__@web.de>
Date2016-02-14 14:48 +0100
Message-ID<mailman.108.1455457712.22075.python-list@python.org>
In reply to#102904
Gregory Ewing wrote:

> Something like this should do it:
> 
>    instance = getattr(module, class_name)(module_name, product)
> 
> If the class name is always the same as the module name with the
> first letter capitalized, you could use
> 
>    instance = getattr(module, module_name.capitalize())(module_name,
>    product)

If the convention is that strict you could also expose the class under an 
alias:

# in ebay.py
Site = Ebay

# in the client module
module = importlib.import_module(module_name)
instance = module.Site(product) 

[toc] | [prev] | [next] | [standalone]


#102907

FromCameron Simpson <cs@zip.com.au>
Date2016-02-14 16:20 +1100
Message-ID<mailman.103.1455429389.22075.python-list@python.org>
In reply to#102900
On 14Feb2016 10:10, Veek. M <vek.m1234@gmail.com> wrote:
>I'm writing a price parser. I need to do the equivalent of perl's
>$$var to instantiate a class where $car is the class_name.
>
>I'm passing 'Ebay' or 'Newegg' or 'Amazon' via cmd-line. I have a module
>named ebay.py and a class called Ebay (price parser). I do something
>like:
>
>\> main.py ebay motherboard
>
>and this does:
> module = __import__(module_name)
>but now i need to instantiate the class - right now I do:
> instance = module.Ebay(module_name, product)
>
>how do i replace the 'Ebay' bit with a variable so that I can load any
>class via cmd line.
>
>class Load(object):
>    def __init__(self, module_name, product):
>        try:
>            module = __import__(module_name)
>            instance = module.Ebay(module_name, product)
>        except ImportError:
>            print("Can't find module %s" % module_name)

Ebay = geattr(module, 'Ebay')

or

klass = getattr(module, module_name.capitalize())
instance = klass(module_name, product)

Cheers,
Cameron Simpson <cs@zip.com.au>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web