ultra newbie question (don't laugh)

Discussion in 'Python' started by John Salerno, Sep 26, 2006.

  1. John Salerno

    John Salerno Guest

    Ok, I've decided to make a little project for myself which involves
    storing employee information in an XML file. I'm doing this partly to
    experiment with working with XML. The blocks in the file will look
    something like this:

    <researcher id="salerjo01">
    <first_name>John</first_name>
    <last_name>Salerno</last_name>
    <birth_country>United States</birth_country>
    <birth_state>Texas</birth_state>
    <birth_city>Houston</birth_city>
    #etc.......
    <researcher>

    I also plan to make a GUI frontend with wxPython for entering the
    records. This will be fairly easy, but not as fun as writing the logic.
    For now I've decided to focus just on writing the logic, and not worry
    about the GUI yet.

    So this is what I came up with so far, then I sat staring at the screen
    wondering how to proceed:

    class LabXMLWriter(object):

    def write_name(self, first, last, given=''):

    Suddenly I realized I just have no idea how to start thinking about what
    I need to do. My first instinct was to use a class, as above, but then I
    wondered if that was even necessary, since all I need to do is get
    information from a user and write it to a file. Do I really need that
    information stored in an object?

    Then I wondered if I needed an __init__ method, and what could go in it?
    Or should I just make separate methods for each bit of information to
    write to the file (i.e., name, birth location, address, phone number,
    etc.). I thought maybe I could create the ID in the __init__ method
    (salerjo01), but to do that I need the name first. I could do this:

    def __init__(self, first_name, last_name, given_name=''):
    # code to initialize name and create ID

    But then I wondered if this detracts from the work that the class
    methods would do later.

    So you see, what I'm asking for is very basic help, sort of along the
    lines of "what things do I need to consider before I even begin this?"
    Is OOP necessary here? Would utility functions work just as well for
    simply writing the information to a file?

    Perhaps I should just take some kind of programming intro class! I read
    all these Python books, but when it comes time to write something
    non-trivial, I get stuck almost immediately with all the possibilities.

    Thanks,
    John
    John Salerno, Sep 26, 2006
    #1
    1. Advertising

  2. John Salerno

    Gal Diskin Guest

    John Salerno wrote:
    > Ok, I've decided to make a little project for myself which involves
    > storing employee information in an XML file. I'm doing this partly to
    > experiment with working with XML. The blocks in the file will look
    > something like this:
    >
    > <researcher id="salerjo01">
    > <first_name>John</first_name>
    > <last_name>Salerno</last_name>
    > <birth_country>United States</birth_country>
    > <birth_state>Texas</birth_state>
    > <birth_city>Houston</birth_city>
    > #etc.......
    > <researcher>
    >
    > I also plan to make a GUI frontend with wxPython for entering the
    > records. This will be fairly easy, but not as fun as writing the logic.
    > For now I've decided to focus just on writing the logic, and not worry
    > about the GUI yet.
    >
    > So this is what I came up with so far, then I sat staring at the screen
    > wondering how to proceed:
    >
    > class LabXMLWriter(object):
    >
    > def write_name(self, first, last, given=''):
    >
    > Suddenly I realized I just have no idea how to start thinking about what
    > I need to do. My first instinct was to use a class, as above, but then I
    > wondered if that was even necessary, since all I need to do is get
    > information from a user and write it to a file. Do I really need that
    > information stored in an object?
    >
    > Then I wondered if I needed an __init__ method, and what could go in it?
    > Or should I just make separate methods for each bit of information to
    > write to the file (i.e., name, birth location, address, phone number,
    > etc.). I thought maybe I could create the ID in the __init__ method
    > (salerjo01), but to do that I need the name first. I could do this:
    >
    > def __init__(self, first_name, last_name, given_name=''):
    > # code to initialize name and create ID
    >
    > But then I wondered if this detracts from the work that the class
    > methods would do later.
    >
    > So you see, what I'm asking for is very basic help, sort of along the
    > lines of "what things do I need to consider before I even begin this?"
    > Is OOP necessary here? Would utility functions work just as well for
    > simply writing the information to a file?
    >
    > Perhaps I should just take some kind of programming intro class! I read
    > all these Python books, but when it comes time to write something
    > non-trivial, I get stuck almost immediately with all the possibilities.
    >
    > Thanks,
    > John


    I think this is marely a matter of programming style in your case. If
    you like to use OOP use it. If not, don't. It doesn't seem to me that
    OOP is necessary to anything you just mentioned. Don't let the
    possibilities slow you - they're there so you can use whatever you
    like, not to flood you with too many options.

    Good luck,
    Gal
    Gal Diskin, Sep 26, 2006
    #2
    1. Advertising

  3. John Salerno

    Larry Bates Guest

    John Salerno wrote:
    > Ok, I've decided to make a little project for myself which involves
    > storing employee information in an XML file. I'm doing this partly to
    > experiment with working with XML. The blocks in the file will look
    > something like this:
    >
    > <researcher id="salerjo01">
    > <first_name>John</first_name>
    > <last_name>Salerno</last_name>
    > <birth_country>United States</birth_country>
    > <birth_state>Texas</birth_state>
    > <birth_city>Houston</birth_city>
    > #etc.......
    > <researcher>
    >
    > I also plan to make a GUI frontend with wxPython for entering the
    > records. This will be fairly easy, but not as fun as writing the logic.
    > For now I've decided to focus just on writing the logic, and not worry
    > about the GUI yet.
    >
    > So this is what I came up with so far, then I sat staring at the screen
    > wondering how to proceed:
    >
    > class LabXMLWriter(object):
    >
    > def write_name(self, first, last, given=''):
    >
    > Suddenly I realized I just have no idea how to start thinking about what
    > I need to do. My first instinct was to use a class, as above, but then I
    > wondered if that was even necessary, since all I need to do is get
    > information from a user and write it to a file. Do I really need that
    > information stored in an object?
    >
    > Then I wondered if I needed an __init__ method, and what could go in it?
    > Or should I just make separate methods for each bit of information to
    > write to the file (i.e., name, birth location, address, phone number,
    > etc.). I thought maybe I could create the ID in the __init__ method
    > (salerjo01), but to do that I need the name first. I could do this:
    >
    > def __init__(self, first_name, last_name, given_name=''):
    > # code to initialize name and create ID
    >
    > But then I wondered if this detracts from the work that the class
    > methods would do later.
    >
    > So you see, what I'm asking for is very basic help, sort of along the
    > lines of "what things do I need to consider before I even begin this?"
    > Is OOP necessary here? Would utility functions work just as well for
    > simply writing the information to a file?
    >
    > Perhaps I should just take some kind of programming intro class! I read
    > all these Python books, but when it comes time to write something
    > non-trivial, I get stuck almost immediately with all the possibilities.
    >
    > Thanks,
    > John

    On reason to use OOP is to insulate your main code from things like storage
    backends or future changes you may decide to make. What if you change your mind
    and want to store the data into something other than XML? If you move all your
    storage code into an object and have it do the writing/reading then you can
    easily replace that object without disrupting your main program. You could even
    extend your program and allow someoneto store in CSV, database, or other storage
    by adding additional storage objects. I had a project where I was reading data
    from CSV file. Later in the implementation the client decided that the file
    that was to be processed would be a .ZIP file instead of a plain-text CSV.
    Because I had implemented a class to do the reading of all data from the CSV
    file that had implemented an iterator, I was able to insert code to open .ZIP
    file (using zipfile module), open a file inside the .ZIP and read using CSV
    module from the zipped file without ever actually unzipping the file. I didn't
    change any top level code and the change took a just a few minutes to implement.
    There was little chance of creating unintended problems and I was able to
    easily support both the old unzipped CSV filesAND the new zipped files. If I
    had not isolated that code into a class, the changes would have been a LOT more
    difficult.

    It can also be nice to provide for future expansion by supporting additional
    keyword arguments. Something like:

    def __init__(self, first_name, last_name, **kwargs):

    Then process the kwargs for additional fields. This way you can painlessly add
    a field to your code by just specifying an additional keyword argument. If the
    application is as simple as you describe it may not be worth the additional
    effort. Once you have done one of these it will become second nature to think
    of creating/using objects in places where you want to provide for maximum
    flexibility and code isolation.

    At a minimum you should take a look at the elementtree module for handling your
    XML. It is downloadable for Python < 2.5 and is part of the standard library
    starting with version 2.5.

    Hope information helps at least a little.

    -Larry Bates
    Larry Bates, Sep 26, 2006
    #3
  4. John Salerno wrote:

    > So you see, what I'm asking for is very basic help, sort of along the
    > lines of "what things do I need to consider before I even begin this?"
    > Is OOP necessary here? Would utility functions work just as well for
    > simply writing the information to a file?


    To start with your last question, yes, they probably would, *IF* all
    you need to do is take data from the user (through a GUI, command line,
    etc.) and store them in some persistent state (text file, pickle, db,
    etc.). In practice, chances are you'll need to do something more with
    the data; I mean, what's the point in just storing data without using
    them somehow ? Using the data is the core of an application, so the
    question you should be able to answer beforehand is "what do I want to
    do with all these data?". Compute statistics ? Print reports ? Create a
    social network type of app ? All of the above ? You get the point.

    After you have some good (or even rough) idea of what you want to do
    with the data, the answer to "Is OOP necessary here?" would be
    "strictly speaking, no, OOP is never necessary, but it's pretty often a
    good idea". I mean, you can code the whole thing in plain old C, with
    global functions and variables, and people have been doing this for
    ages, but why build a fire from wood when there are zippos? In short,
    OOP comes handy when:
    a. you want to couple data with behavior (methods), AND
    b. you have many instances of the same "kind(s)" around, interacting
    with each other (deliberately using "kind" here to avoid starting a
    "class" vs "type" theoretical discussion). If there's only one instance
    of some "kind", in python it is usually better to have a module instead
    of a singleton class (since modules are builtin singletons).

    > Perhaps I should just take some kind of programming intro class! I read
    > all these Python books, but when it comes time to write something
    > non-trivial, I get stuck almost immediately with all the possibilities.


    Or, if you're comfortable studying on your own, you could start with a
    book or two that focus on software design and architecture, rather than
    language details and small programming recipes. I can't think of any
    specific title to suggest off the top of my head but I'm sure you'll
    get some good suggestions from others if you ask here.

    George
    George Sakkis, Sep 26, 2006
    #4
  5. John Salerno wrote:

    > So you see, what I'm asking for is very basic help, sort of along the
    > lines of "what things do I need to consider before I even begin this?"
    > Is OOP necessary here? Would utility functions work just as well for
    > simply writing the information to a file?


    when you get programmers block, telling your brain to "shut up and let
    me write some code" is often a good idea. just start tinkering with
    your first idea, and see if you can make it work. if you get a little
    stuck, hack your way through it. if you get really stuck, take a break,
    work on something else for a while, and wait for your brain to come up
    with a better idea. repeat.

    here's how I would start:

    import xml.etree.ElementTree as ET

    class Researcher:

    # attributes (you can skip this part, if you want)
    first_name = None
    last_name = None
    birth_country = None
    birth_state = None
    birth_city = None

    # the identity is lazily evaluated by getid (see below)
    id = None

    def getid(self):
    if not self.id:
    self.id = (self.first_name + self.last_name).lower() # etc
    return self.id

    def maketree(self):
    elem = ET.Element("researcher", id=self.getid())
    ET.SubElement(elem, "first_name").text = self.first_name
    ET.SubElement(elem, "last_name").text = self.last_name
    ET.SubElement(elem, "birth_country").text = self.birth_country
    ET.SubElement(elem, "birth_state").text = self.birth_state
    ET.SubElement(elem, "birth_city").text = self.birth_city
    return elem

    x = Researcher()

    # simulate the UI's "OK" handler

    x.first_name = "John"
    x.last_name = "Salerno"
    x.birth_country = "United States"
    x.birth_state = "Texas"
    x.birth_city = "Houston"

    print ET.tostring(x.maketree())

    </F>
    Fredrik Lundh, Sep 26, 2006
    #5
  6. John Salerno

    John Salerno Guest

    George Sakkis wrote:

    > To start with your last question, yes, they probably would, *IF* all
    > you need to do is take data from the user (through a GUI, command line,
    > etc.) and store them in some persistent state (text file, pickle, db,
    > etc.). In practice, chances are you'll need to do something more with
    > the data; I mean, what's the point in just storing data without using
    > them somehow ? Using the data is the core of an application, so the
    > question you should be able to answer beforehand is "what do I want to
    > do with all these data?". Compute statistics ? Print reports ? Create a
    > social network type of app ? All of the above ? You get the point.


    Thanks for the advice guys! OOP seems like a good idea, and besides it
    can't hurt to experiment with it even if I don't necessarily need it.
    I'll give this a shot and maybe post the code later and let you all tear
    it apart. :)
    John Salerno, Sep 26, 2006
    #6
  7. John Salerno

    John Salerno Guest

    Fredrik Lundh wrote:

    > when you get programmers block, telling your brain to "shut up and let
    > me write some code" is often a good idea. just start tinkering with
    > your first idea, and see if you can make it work. if you get a little
    > stuck, hack your way through it. if you get really stuck, take a break,
    > work on something else for a while, and wait for your brain to come up
    > with a better idea. repeat.


    Thanks Fredrik! My initial feeling was the opposite, i.e. don't rush
    into anything, just stop and think about how I want to do this first.
    But then of course the possibilities piled up and I was stuck, so your
    suggestion is probably much better at this point. I just need to try
    some things! If nothing else, I'll learn what's efficient and what's not.


    > here's how I would start:
    > snip


    You know, I planned all along to use ElementTree to *read* my XML file,
    but like a bonehead I for some reason was trying to create all the
    *write* methods myself! Next stop: ElementTree documentation page! :)
    John Salerno, Sep 26, 2006
    #7
  8. John Salerno

    Peter Otten Guest

    [OT] Re: ultra newbie question (don't laugh)

    John Salerno wrote:

    > Thanks Fredrik! My initial feeling was the opposite, i.e. don't rush
    > into anything, just stop and think about how I want to do this first.
    > But then of course the possibilities piled up and I was stuck, so your
    > suggestion is probably much better at this point. I just need to try
    > some things! If nothing else, I'll learn what's efficient and what's not.


    Here's a lighthearted take on the matter:

    http://www.theonion.com/content/node/47469

    (link found in a post to python-dev by the notorious Fredrik Lundh)

    Peter
    Peter Otten, Sep 26, 2006
    #8
    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. gale

    Please don't laugh

    gale, Oct 14, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    371
    David Wier
    Oct 14, 2004
  2. WhiteGoldWielder
    Replies:
    4
    Views:
    348
    Harald Hein
    Dec 1, 2003
  3. Julia Briggs

    * Ultra Newbie HTML Question *

    Julia Briggs, Nov 29, 2003, in forum: HTML
    Replies:
    2
    Views:
    362
    Spartanicus
    Nov 29, 2003
  4. JKop
    Replies:
    36
    Views:
    949
    Ioannis Vranos
    Oct 30, 2004
  5. Tayna Taylor

    % Ultra Newbie Question %

    Tayna Taylor, Nov 29, 2003, in forum: Javascript
    Replies:
    3
    Views:
    110
    Tayna Taylor
    Nov 30, 2003
Loading...

Share This Page