Random Instance generation

Discussion in 'Python' started by Balaji, Sep 14, 2004.

  1. Balaji

    Balaji Guest

    Hello eveybody....

    Suppose there is a class A:

    I want to generate say 100 random instance of it...

    How can I do that....

    I want the instance name to be like a1, a2, a3...

    Any ideas.....

    Cheers

    Balaji
     
    Balaji, Sep 14, 2004
    #1
    1. Advertising

  2. Balaji

    Paul McNett Guest

    Balaji writes:

    > Suppose there is a class A:
    > I want to generate say 100 random instance of it...


    Define "random instance".


    > How can I do that....
    > I want the instance name to be like a1, a2, a3...


    If you just want to make 100 instances of a class, try something
    like:

    class A(object): pass

    for count in range(100):
    exec "a%s = A()" % count

    That is hardly random, though.

    --
    Paul McNett
    Independent Software Consultant
    http://www.paulmcnett.com
     
    Paul McNett, Sep 14, 2004
    #2
    1. Advertising

  3. from basicproperty import common, propertied

    class MyObject( propertied.Propertied ):
    name = common.StringProperty(
    "name", """The "name" of the object by some measure""",
    )

    instances = [ MyObject( 'a%d'%(i,)) for i in range(100) ]

    Doing it without basicproperty defining a "name" would, of course,
    require you to determine what you mean by having a "name of" a1 (in
    Python, names point to objects from namespaces, they aren't necessarily
    attributes of the objects themselves, so two names can point to one
    object, or an object can be entirely without a name).

    For instance:

    instances = dict( [ ('a%d'%(i,),object()) for i in range( 100 ) ] )

    would give you a dictionary with 100 named objects in which you could
    then "eval" or "exec" code snippets that rely on those names.

    >>> instances = dict( [ ('a%d'%(i,),object()) for i in range( 100 ) ] )
    >>> instances['a1']

    <object object at 0x00C7D3C0>
    >>> eval( '(a2, a8,a4)', instances )

    (<object object at 0x00C7D3C8>, <object object at 0x00C7D3F8>, <object
    object at 0x00C7D3D8>)

    which is the kind of thing you would do if you were trying to write an
    interpreter.

    Good luck,
    Mike

    Balaji wrote:

    >Hello eveybody....
    >
    >Suppose there is a class A:
    >
    >I want to generate say 100 random instance of it...
    >
    >How can I do that....
    >
    >I want the instance name to be like a1, a2, a3...
    >
    >Any ideas.....
    >
    >Cheers
    >
    >Balaji
    >
    >

    ________________________________________________
    Mike C. Fletcher
    Designer, VR Plumber, Coder
    http://www.vrplumber.com
    http://blog.vrplumber.com
     
    Mike C. Fletcher, Sep 14, 2004
    #3
  4. Balaji

    Larry Bates Guest

    I don't know what you mean by "random" but one
    solution is:

    a=[]
    a=[A() for n in range(0,100)]

    They are a[0], a[1], a[2} and don't resemble
    anything like "randomness", but there are 100
    of them stored in the list a.

    Larry Bates

    "Balaji" <> wrote in message
    news:...
    > Hello eveybody....
    >
    > Suppose there is a class A:
    >
    > I want to generate say 100 random instance of it...
    >
    > How can I do that....
    >
    > I want the instance name to be like a1, a2, a3...
    >
    > Any ideas.....
    >
    > Cheers
    >
    > Balaji
     
    Larry Bates, Sep 15, 2004
    #4
  5. Balaji wrote:

    > Hello eveybody....
    >
    > Suppose there is a class A:
    >
    > I want to generate say 100 random instance of it...
    >
    > How can I do that....
    >
    > I want the instance name to be like a1, a2, a3...


    Usually when you feel that you need to generate instances with
    certain names you are on the wrong track in translating the
    problem you need to solve into a program.

    You can always store 100 instances in a list then iterate
    over them.

    If you need to identify individual instances by name, create
    a data structure (the simplest is a dictionary)
    that maps a name to an instance.

    Istvan.
     
    Istvan Albert, Sep 15, 2004
    #5
  6. Balaji

    Peter Abel Guest

    (Balaji) wrote in message news:<>...
    > Hello eveybody....
    >
    > Suppose there is a class A:
    >
    > I want to generate say 100 random instance of it...
    >
    > How can I do that....
    >
    > I want the instance name to be like a1, a2, a3...
    >
    > Any ideas.....
    >
    > Cheers
    >
    > Balaji


    >>> class A:

    .... pass
    ....
    >>> filter(lambda i:globals().__setitem__('a%d'%i,A()),range(5))

    []
    >>> a0

    <__main__.A instance at 0x00DD8418>
    >>> a1

    <__main__.A instance at 0x00DDB748>
    >>> a2

    <__main__.A instance at 0x00C0DD20>
    >>> a3

    <__main__.A instance at 0x00C0DD50>
    >>> a4

    <__main__.A instance at 0x00DDB1E0>
    >>>


    Instead of globals() you can take any dictionary you want.
    Instead of filter you can take list comprehension.
    Instead of dictionary you can take a list as container.
    Instead of what I posted above you can do what ohters posted.
    Pyhton lets you do it in any way of your gusto.

    Regards Peter
     
    Peter Abel, Sep 15, 2004
    #6
  7. Balaji

    Paul Rubin Guest

    (Balaji) writes:
    > I want to generate say 100 random instance of it...
    >
    > How can I do that....
    >
    > I want the instance name to be like a1, a2, a3...


    You almost certainly want to use an array instead of all those a1,a2,... .
     
    Paul Rubin, Sep 15, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ALuPin

    Random generation in function

    ALuPin, Sep 1, 2004, in forum: VHDL
    Replies:
    1
    Views:
    651
    Ralf Hildebrandt
    Sep 1, 2004
  2. dpi

    Random Number Generation

    dpi, Mar 13, 2006, in forum: VHDL
    Replies:
    4
    Views:
    10,098
    vipinlal
    Mar 26, 2010
  3. globalrev
    Replies:
    4
    Views:
    778
    Gabriel Genellina
    Apr 20, 2008
  4. John W. Long

    HTML Generation (Next Generation CGI)

    John W. Long, Nov 22, 2003, in forum: Ruby
    Replies:
    4
    Views:
    357
    John W. Long
    Nov 24, 2003
  5. VK
    Replies:
    15
    Views:
    1,186
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page