creating a variable from a function

Discussion in 'Python' started by steve, Oct 9, 2003.

  1. steve

    steve Guest

    Code:
    from net.grinder.script import Test
    from net.grinder.plugin.http import HTTPRequest
    from HTTPClient import NVPair
    
    log = grinder.logger.output
    
    # Random Number Generator
    def randomnumber():
    	import random
    	number = random.randrange(1, 2000, 1)
    	return number
    
    # We declare a default URL for the HTTPRequest.
    request = HTTPRequest(url = "http://host")
    
    # Details of HTTP request
    def page1():
    	request.GET('/app')
    	request.GET('/app/j_security_check?j_password=jr&j_username=operator@DIC')
    	request.GET('/app/adduser.do?cn=jbloggs_%(seed)s&userPassword=jbloggs'%
    {'seed': randomnumber()} )
    
    page1Test = Test(1, "First page").wrap(page1)
    
    class TestRunner:
        def __call__(self):
            page1Test()
    
    
    I can't get the output from randomnumber() to input as the variable
    %(seed)s in page1(). randomnumber() works ok on its own, but when it
    is called it seems to be inserting the actual function into the
    variable, rather than the output.

    I get this error when running it:
    ImportError: no module named random

    (note: the 3rd and 4th line of page(1) are actually one line)

    Any ideas anyone? thanks.
    steve, Oct 9, 2003
    #1
    1. Advertising

  2. On 9 Oct 2003 07:38:53 -0700, (steve) wrote:

    >
    Code:
    >from net.grinder.script import Test
    >from net.grinder.plugin.http import HTTPRequest
    >from HTTPClient import NVPair
    >
    >log = grinder.logger.output
    >
    ># Random Number Generator
    >def randomnumber():
    >	import random
    >	number = random.randrange(1, 2000, 1)
    >	return number
    >
    ># We declare a default URL for the HTTPRequest.
    >request = HTTPRequest(url = "http://host")
    >
    ># Details of HTTP request
    >def page1():
    >	request.GET('/app')
    >	request.GET('/app/j_security_check?j_password=jr&j_username=operator@DIC')
    >	request.GET('/app/adduser.do?cn=jbloggs_%(seed)s&userPassword=jbloggs'%
    >{'seed': randomnumber()} )
    >
    >page1Test = Test(1, "First page").wrap(page1)
    >
    >class TestRunner:
    >    def __call__(self):
    >        page1Test()
    >
    >
    >
    >I can't get the output from randomnumber() to input as the variable
    >%(seed)s in page1(). randomnumber() works ok on its own, but when it

    Are you running on your PC when it works, and having it run on a server
    when it doesn't? Perhaps the environment on the server is not set up
    the same, so that random is not found?

    What happens if you run page1() manually and set request to something that will print
    the GET args passed to it, e.g., if you do this (untested)

    class Foo(object):
    def GET(self, *args): print 'GET args = %r' % args
    request = Foo()
    page1()

    to override request, interactively after the above, what do you get?

    >is called it seems to be inserting the actual function into the
    >variable, rather than the output.
    >
    >I get this error when running it:
    >ImportError: no module named random

    I guess it might mean it, for the context it's running in.

    >
    >(note: the 3rd and 4th line of page(1) are actually one line)
    >
    >Any ideas anyone? thanks.


    Try putting debug prints or log writes in your code to tell you more.

    Regards,
    Bengt Richter
    Bengt Richter, Oct 12, 2003
    #2
    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. Replies:
    13
    Views:
    522
    patelvijayp
    Apr 18, 2007
  2. mfglinux
    Replies:
    11
    Views:
    685
    Roberto Bonvallet
    Sep 12, 2007
  3. Replies:
    6
    Views:
    151
    Bob Barrows [MVP]
    Oct 13, 2007
  4. AikidoGuy
    Replies:
    11
    Views:
    549
    Seebs
    Nov 21, 2011
  5. David Filmer
    Replies:
    19
    Views:
    227
    Kevin Collins
    May 21, 2004
Loading...

Share This Page