Importing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HMS Surprise

    #1

    Importing


    Greetings,

    First I will admit I am new to Python but have experience with C++ and
    some Tcl/Tk. I am starting to use a tool called MaxQ that uses
    jython. I have been studying Rossum's tutorial but still am unclear on
    importing, the use of self, and calling functions with their own name
    in quotes. In the code below the lines between #Start and #End are
    generated by browsing web pages. In a large test this section can
    become quite large. The maxQ documentation suggests breaking it up
    into functions. If so could these functions be placed in a separate
    file and imported? Is it acceptable to import into a class? If
    imported as a function will self as used here need to be changed?
    Would it be better to duplicate class and import whole classes?

    Any suggestions will be appreciated, especially other Python
    tutorials.

    Best Regards,

    jh

    ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~

    # Generated by MaxQ
    [com.bitmechanic .maxq.generator .JythonCodeGene rator]
    from PyHttpTestCase import PyHttpTestCase
    from com.bitmechanic .maxq import Config
    global validatorPkg
    if __name__ == 'main':
    validatorPkg = Config.getValid atorPkgName()
    # Determine the validator for this testcase.
    exec 'from '+validatorPkg+ ' import Validator'


    # definition of test class
    class MaxQTest(PyHttp TestCase):
    def runTest(self):
    self.msg('Test started')

    #Start

    self.msg("Testi ng URL: %s" % self.replaceURL ('''http://
    www.dogpile.com/'''))
    url = "http://www.dogpile.com/"
    params = None
    Validator.valid ateRequest(self , self.getMethod( ), "get", url,
    params)
    self.get(url, params)
    self.msg("Testi ng URL: %s" % self.replaceURL ('''http://
    www.dogpile.com/favicon.ico''') )
    url = "http://www.dogpile.com/favicon.ico"
    params = None
    Validator.valid ateRequest(self , self.getMethod( ), "get", url,
    params)
    self.get(url, params)

    #End

    # ^^^ Insert new recordings here. (Do not remove this line.)


    # Code to load and run the test
    if __name__ == 'main':
    test = MaxQTest("MaxQT est")


    # test.Run() #Either of these two lines work equally well.
    test.runTest() #I suspect that Run() is a method of
    PPyHttpTestCase or a builtIn

  • Bruno Desthuilliers

    #2
    Re: Importing

    HMS Surprise a écrit :
    Greetings,
    >
    First I will admit I am new to Python but have experience with C++ and
    some Tcl/Tk. I am starting to use a tool called MaxQ that uses
    jython. I have been studying Rossum's tutorial but still am unclear on
    importing,
    What's your problem here ?
    the use of self,
    In Python, a method is a wrapper around a function. This wrappers as a
    reference to the instance on which the method is called, and calls the
    wrapped function passing the instance as first argument. By convention,
    it's named 'self', but technically you could use any other name. So, to
    make a long story short:
    - you need to declare 'self' as the first argument of a method
    - you need to use self to access the current instance within the
    method body
    - you *don't* have to pass the instance when calling the method
    and calling functions with their own name
    You mean the
    test = MaxQTest("MaxQT est")
    line ?

    First point: it's not a function call, it's an object instanciation.
    There's no 'new' keyword in Python, classes are callable objects acting
    as factory for their instances.

    Second point: passing the name of the class here is specific to MaxQ,
    and I'd say it's only used for internal and display stuff.
    in quotes. In the code below the lines between #Start and #End are
    generated by browsing web pages. In a large test this section can
    become quite large. The maxQ documentation suggests breaking it up
    into functions. If so could these functions be placed in a separate
    file and imported?
    Yes, but you'd have to bind them to the class manually (cf below).
    Is it acceptable to import into a class?
    Yes, but this wont solve your problem. Import creates a module object in
    the current namespace, that's all.
    If
    imported as a function will self as used here need to be changed?
    Nope.

    def quux(self, foo):
    self.bar += foo

    class Boo(object):
    def __init__(self, bar):
    self.bar = bar

    Boo.quux = quux
    b = Boo(40)
    b.quux(2)
    print b.bar

    Comment

    • HMS Surprise

      #3
      Re: Importing

      Thanks for posting. I continued to read and realized the instantiation
      was not a function call soon after I posted. Oh well...

      Still unsure about the best way to break up the large #start - #end
      section. I appreciate your answers and try to digest them more fully.
      Then maybe I will have a better idea and can post better qualified/
      quantified questions.

      Thanks again,

      jh

      Comment

      Working...