How to write good Python objects?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Ladasky

    How to write good Python objects?

    Hi, folks,

    At the beginning of 2003, I was a frustrated computer user, and lapsed
    programmer, with problems to solve that screamed for programming.
    Thanks to the Python language and community, I am a programmer once
    again.

    My earlier solicitation to the computer world is here:



    Anyway, I've been busy with Python for several months now. I'm an old
    procedural guy. I had never written code in an object-oriented
    language before. I'm starting to get the hang of it, and to see its
    advantages, but I'm still struggling. I think that I have issues with
    both OOP in general, and with Python in particular.

    I have written one nice, self-contained object that contained a DNA
    sequence, and various functions to manipulate the data therein. My
    wxPython GUI objects, in contrast, are in a state of constant flux.
    Rather than importing and reusing a piece of code, I find myself
    copying the code into my new program and playing with it, just a bit.
    I'm starting to believe that writing a good GUI object, one that you
    can really reuse, is actually quite hard. Yes, I know that you can
    derive a new object that overrides properties of your old object.
    Should I find myself doing this for every object that I write?

    One other thing -- I would like to be able to include a statement in
    my program to the effect of: "from my_package import
    my_function_or_ class". Python seems to look for .pyc files in the
    /Lib folder. By placing a .py file in /Lib, I got it to compile, and
    to be recognized by an import statement. Is this the right way for
    users to package their own code? It seems kludgy to me.

    If it matters (it shouldn't), I'm running Python 2.2.2 on Win2000 Pro.

    Thanks for your advice!

    --
    John J. Ladasky Jr., Ph.D.
    Department of Biology
    Johns Hopkins University
    Baltimore MD 21218
    USA
    Earth
  • Marijan Tadin

    #2
    Re: How to write good Python objects?

    > One other thing -- I would like to be able to include a statement in[color=blue]
    > my program to the effect of: "from my_package import
    > my_function_or_ class". Python seems to look for .pyc files in the
    > /Lib folder. By placing a .py file in /Lib, I got it to compile, and
    > to be recognized by an import statement. Is this the right way for
    > users to package their own code? It seems kludgy to me.[/color]

    AFAIK if you want to import a python module (actually *.py file), you
    can place it in /Lib subdirectory of python distribution, but I think
    this subdirectory schould better stay reserved for modules from standard
    python distribution.
    You can put your module in any directory, and then add this subdirectory
    to the PYTHONPATH environment variable of your operating system. I've
    been doing this for a while, but then I ended with unmanageable cluster
    of files distributed somewhere on my computer.
    I think the best way is to use distutilities (Distributing Python
    Modules link in your python documentation). I've studied it a few days
    ago, and it is much easier to use (at least for simple things) than I
    had thought before.
    There is also a /lib/site-packages directory in your python
    distribution, if you put your *.py file in it, you can allways import it
    in your application, and it is also intended as directory for
    third-party python modules, and distutilities will install your modules
    into this subdirectory. I 'd recommend you to use packages (section 6.4
    Packages in the tutorial in your python documentation) if you are not
    allready doing so, because otherwise you'll probably soon get name
    clashes.
    If you use distutillities, you get two extras too:
    1. You can easily make a Windows installer for your library, and if you
    install your library this way, it will be registerd by OS, so if you
    want to uninstall your library, you can do it as for any other windows
    programm.
    2. If you want to compile C extension, the easiest way to do so is with
    distutilities. The first time I succeeded to do so, was with help from
    some web post by Alex Martelli (maybe it is in Python recepies, but I'm
    not sure). Most information how to do this is rather linux oriented.
    Actually it is not difficult to do so on Windows (also with Mingw
    compiler) but there is, AFAIK, no detailed (from begin to end at one
    place) instruction how to do it for dummies (I still feel like one, so
    the worst thing that can happen is a hint: "look into your compiler
    documentation") , it is rather distributed over the web.
    [color=blue]
    > I have written one nice, self-contained object that contained a DNA
    > sequence, and various functions to manipulate the data therein. My
    > wxPython GUI objects, in contrast, are in a state of constant flux.
    > Rather than importing and reusing a piece of code, I find myself
    > copying the code into my new program and playing with it, just a bit.
    > I'm starting to believe that writing a good GUI object, one that you
    > can really reuse, is actually quite hard. Yes, I know that you can
    > derive a new object that overrides properties of your old object.
    > Should I find myself doing this for every object that I write?[/color]

    I do not feel competent enough to give much advice here, but I believe
    that, if you start to copy and paste your code, you should reconsider
    your design. But, on the other hand, if you intend your library to be
    used by others, to many classes and inheritance can be cumbersome. I
    remember trying to use some Java library, which can do great things, but
    in order to do simplest things with it, I schould use dozen classes,
    where each of them was e.g. 5-th or 10-th in some inheritance hierarchy,
    and you can imagine the fun of finding documentation of the
    class-methods I wanted to use. I can not judge if it is OK for complex
    library which is intended to be used by professional programmers.
    [color=blue]
    > At the beginning of 2003, I was a frustrated computer user, and lapsed
    > programmer, with problems to solve that screamed for programming.
    > Thanks to the Python language and community, I am a programmer once
    > again.[/color]

    I am actualy weather forecaster, and I do some programming in my spare
    time. I can remember learning C++ for about 6-7 months, and then
    learning Java for about 3-4 months, and still not beeing able to open
    text file and do some simple processing (which I could have done easily
    in Fortran77 before) without looking in a book . The best advice I've
    found in the book "Thinking in Java", was to have a look at Python.

    Marijan Tadin


    Comment

    • Martin Franklin

      #3
      Re: How to write good Python objects?

      On Fri, 2003-10-24 at 02:15, John Ladasky wrote:[color=blue]
      > I have written one nice, self-contained object that contained a DNA
      > sequence, and various functions to manipulate the data therein. My
      > wxPython GUI objects, in contrast, are in a state of constant flux.
      > Rather than importing and reusing a piece of code, I find myself
      > copying the code into my new program and playing with it, just a bit.
      > I'm starting to believe that writing a good GUI object, one that you
      > can really reuse, is actually quite hard. Yes, I know that you can
      > derive a new object that overrides properties of your old object.
      > Should I find myself doing this for every object that I write?
      >[/color]

      John ,


      I see you've been helped with the /Lib folder question. I am more
      interested to hear about your GUI problem. I don't use wxPython I use
      Tkinter but hardly ever find myself copying code from one project to
      another...(thes e days) perhaps you could share some examples of the kind
      of GUI elements you find yourself copying...


      One example I used to find myself copying would be a tool bar (Tkinter
      does not have a 'toolbar' widget) I would find myself creating a Frame
      then adding lots of buttons to it now however I use a Toolbar class I
      created that has an add method that takes a couple of arguments.


      e.g (untested) I now use something like:

      class Toolbar(Frame):
      def __init__(self, parent):
      Frame.__init__( self, parent)


      def add(self, label="", icon=None, command=None):
      b = Button(self, text=label, command=command , image=icon)
      b.pack(side="le ft")

      .... now to use it in some other module...

      tb = Toolbar(root)
      tb.pack(fill="x ")

      tools = [("Open", None, self.openFile),
      ("Close", None, self.closeFile) ,
      ("Quit", None, self.quit)
      ]
      for label, image, command in tools:
      tb.add(label=la bel, icon=image, command=command )





      ## rather than...


      toolbar = Frame(parent)
      toolbar.pack()

      openButton = Button(toolbar, text="Open", command=self.op enFile)
      openButton.pack (side="left")

      closeButton = Button(toolbar, text="Close", command=self.cl oseFile)
      closeButton.pac k(side="left")

      .... you get the picture...


      Is this the sort of problem you are having..?


      Regards
      Martin




      --
      Martin Franklin <mfranklin1@gat wick.westerngec o.slb.com>


      Comment

      • Andy Todd

        #4
        Re: How to write good Python objects?

        John Ladasky wrote:
        [color=blue]
        > Hi, folks,
        >[/color]
        [snip][color=blue]
        >
        > One other thing -- I would like to be able to include a statement in
        > my program to the effect of: "from my_package import
        > my_function_or_ class". Python seems to look for .pyc files in the
        > /Lib folder. By placing a .py file in /Lib, I got it to compile, and
        > to be recognized by an import statement. Is this the right way for
        > users to package their own code? It seems kludgy to me.
        >
        > If it matters (it shouldn't), I'm running Python 2.2.2 on Win2000 Pro.
        >
        > Thanks for your advice!
        >
        > --
        > John J. Ladasky Jr., Ph.D.
        > Department of Biology
        > Johns Hopkins University
        > Baltimore MD 21218
        > USA
        > Earth[/color]

        You can put your modules anywhere you like. You just need to tell the
        Python interpreter where to look for them. You can do this one of two
        ways, either by setting a PYTHONPATH environment variable;



        Do this on Windows 2000 with Start->Settings->Control Panel->System
        Then select the 'Advanced' tab and click on the button labelled
        "Environmen t variables..."

        Or, you can use path configuration (.pth) files;



        On Windows 2000, and with Python installed to C:\Python2.2 you would
        place your file (called, say John.pth) in C:\Python2.2\Li b\site-packages

        Regards,
        Andy
        --
        --------------------------------------------------------------------------------
        From the desk of Andrew J Todd esq - http://www.halfcooked.com/



        Comment

        • John Roth

          #5
          Re: How to write good Python objects?


          "John Ladasky" <ladasky@my-deja.com> wrote in message
          news:c09b237b.0 310231715.2e1a6 bc@posting.goog le.com...[color=blue]
          > Hi, folks,
          >
          > At the beginning of 2003, I was a frustrated computer user, and lapsed
          > programmer, with problems to solve that screamed for programming.
          > Thanks to the Python language and community, I am a programmer once
          > again.
          >
          > My earlier solicitation to the computer world is here:
          >
          >[/color]
          http://groups.google.com/groups?selm...ing.google.com[color=blue]
          >
          > Anyway, I've been busy with Python for several months now. I'm an old
          > procedural guy. I had never written code in an object-oriented
          > language before. I'm starting to get the hang of it, and to see its
          > advantages, but I'm still struggling. I think that I have issues with
          > both OOP in general, and with Python in particular.
          >
          > I have written one nice, self-contained object that contained a DNA
          > sequence, and various functions to manipulate the data therein. My
          > wxPython GUI objects, in contrast, are in a state of constant flux.
          > Rather than importing and reusing a piece of code, I find myself
          > copying the code into my new program and playing with it, just a bit.
          > I'm starting to believe that writing a good GUI object, one that you
          > can really reuse, is actually quite hard. Yes, I know that you can
          > derive a new object that overrides properties of your old object.
          > Should I find myself doing this for every object that I write?[/color]

          I'd suggest you look at refactoring out duplication. What it sounds like
          is that you're generating a lot of minor variations on the same theme.
          If you go after duplication relentlessly, eventually the code itself will
          tell you what it wants to look like.

          [color=blue]
          > Thanks for your advice![/color]

          You're welcome.

          John Roth[color=blue]
          >
          > --
          > John J. Ladasky Jr., Ph.D.
          > Department of Biology
          > Johns Hopkins University
          > Baltimore MD 21218
          > USA
          > Earth[/color]


          Comment

          • Anna

            #6
            Re: How to write good Python objects?

            Not sure if ya'll have seen these references to distutils and windows. I'm
            forwarding to make sure we keep it in mind when we're working on our book.

            Anna


            On Fri, 24 Oct 2003 07:01:52 +0100, Marijan Tadin wrote:
            [color=blue][color=green]
            >> One other thing -- I would like to be able to include a statement in my
            >> program to the effect of: "from my_package import my_function_or_ class".
            >> Python seems to look for .pyc files in the /Lib folder. By placing a
            >> .py file in /Lib, I got it to compile, and to be recognized by an import
            >> statement. Is this the right way for users to package their own code?
            >> It seems kludgy to me.[/color]
            >
            > AFAIK if you want to import a python module (actually *.py file), you can
            > place it in /Lib subdirectory of python distribution, but I think this
            > subdirectory schould better stay reserved for modules from standard python
            > distribution.
            > You can put your module in any directory, and then add this subdirectory
            > to the PYTHONPATH environment variable of your operating system. I've been
            > doing this for a while, but then I ended with unmanageable cluster of
            > files distributed somewhere on my computer. I think the best way is to use
            > distutilities (Distributing Python Modules link in your python
            > documentation). I've studied it a few days ago, and it is much easier to
            > use (at least for simple things) than I had thought before.
            > There is also a /lib/site-packages directory in your python distribution,
            > if you put your *.py file in it, you can allways import it in your
            > application, and it is also intended as directory for third-party python
            > modules, and distutilities will install your modules into this
            > subdirectory. I 'd recommend you to use packages (section 6.4 Packages in
            > the tutorial in your python documentation) if you are not allready doing
            > so, because otherwise you'll probably soon get name clashes.
            > If you use distutillities, you get two extras too: 1. You can easily make
            > a Windows installer for your library, and if you install your library this
            > way, it will be registerd by OS, so if you want to uninstall your library,
            > you can do it as for any other windows programm.
            > 2. If you want to compile C extension, the easiest way to do so is with
            > distutilities. The first time I succeeded to do so, was with help from
            > some web post by Alex Martelli (maybe it is in Python recepies, but I'm
            > not sure). Most information how to do this is rather linux oriented.
            > Actually it is not difficult to do so on Windows (also with Mingw
            > compiler) but there is, AFAIK, no detailed (from begin to end at one
            > place) instruction how to do it for dummies (I still feel like one, so the
            > worst thing that can happen is a hint: "look into your compiler
            > documentation") , it is rather distributed over the web.
            >[color=green]
            >> I have written one nice, self-contained object that contained a DNA
            >> sequence, and various functions to manipulate the data therein. My
            >> wxPython GUI objects, in contrast, are in a state of constant flux.
            >> Rather than importing and reusing a piece of code, I find myself copying
            >> the code into my new program and playing with it, just a bit. I'm
            >> starting to believe that writing a good GUI object, one that you can
            >> really reuse, is actually quite hard. Yes, I know that you can derive a
            >> new object that overrides properties of your old object. Should I find
            >> myself doing this for every object that I write?[/color]
            >
            > I do not feel competent enough to give much advice here, but I believe
            > that, if you start to copy and paste your code, you should reconsider your
            > design. But, on the other hand, if you intend your library to be used by
            > others, to many classes and inheritance can be cumbersome. I remember
            > trying to use some Java library, which can do great things, but in order
            > to do simplest things with it, I schould use dozen classes, where each of
            > them was e.g. 5-th or 10-th in some inheritance hierarchy, and you can
            > imagine the fun of finding documentation of the class-methods I wanted to
            > use. I can not judge if it is OK for complex library which is intended to
            > be used by professional programmers.
            >[color=green]
            >> At the beginning of 2003, I was a frustrated computer user, and lapsed
            >> programmer, with problems to solve that screamed for programming. Thanks
            >> to the Python language and community, I am a programmer once again.[/color]
            >
            > I am actualy weather forecaster, and I do some programming in my spare
            > time. I can remember learning C++ for about 6-7 months, and then learning
            > Java for about 3-4 months, and still not beeing able to open text file and
            > do some simple processing (which I could have done easily in Fortran77
            > before) without looking in a book . The best advice I've found in the book
            > "Thinking in Java", was to have a look at Python.
            >
            > Marijan Tadin[/color]

            Comment

            Working...