Storing records from a parsed file

Discussion in 'Python' started by Ben, Sep 30, 2006.

  1. Ben

    Ben Guest

    Apologies if this is te wrong place to post - I realise the question
    is pretty basic...

    I have a simple python script that parses a text file and extracts data
    from it. Currently
    this data is stored in a list by a function, each element of which is
    an instance of a class with
    member variables to take the data. So for example I have:

    camera_list.append(camera(alpha,beta,gamma....))

    where

    class camera:
    def __init__(self,name,site,address,text):
    self.name=name
    self.site=site
    self.address=address
    self.text=text

    Every time I append an item to this list I pass in the constructor
    parameters so end up with all my data in the list which can then be
    accessed by doing myList[x].name (for example)

    This seemed like a nice solution until I tried to put the entire
    parsing program into its own class. I did this so that I could parse
    different types of file:

    thistype.getdata()
    thattype.getdata()
    ....
    thisfile and thatfile would have the same function definitions, but
    different implementations as needed.

    But now my list generating funtion needs to create inner "camera"
    classes when it is itself a member funcition of a class. This seems to
    be causing problems - I coudl possibly use nested dictionaries, but
    this sounds messy. Ideally I would use structs defined inside the
    outer class, but pythn doesn't seem to support these.

    I hope I haven't rambled too much here - I'm new to python so have
    probably done some silly things :)

    Cheers,

    Ben
     
    Ben, Sep 30, 2006
    #1
    1. Advertising

  2. Ben

    Ben Guest

    Ah I see. So istead of creating the classes diectly I use a facroty
    class as a buffer - I tell it what I want and it makes the appropriate
    instances. I am not enitely sure that applies here though (I may be
    wrong)

    Currently I have:

    camera_list[]

    class camera:
    def __init__(self,alpha,beta,gamma...):
    self.alpha=alpha
    self.beta=beta
    self.gamma=gamma
    ...

    for (some conditions)
    camera_list.append(camera(alpha,beta,gamma...)

    the append command creates an instance of the camera class and shoves
    it at the end of a list for each itteration of the loop.

    However, I want to recover various types of information from the text
    file I am parsing - not just (as in the example above) camera data.
    However I am trying to keep the interface the same.

    so I can have collectSomeData.getData() as well as
    collectSomeOtherData.getData()

    In each case the getData impementation will be different to suit the
    required task.

    So something like:

    class camera:
    def __init__(self,alpha,beta,gamma...):
    self.alpha=alpha
    self.beta=beta
    self.gamma=gamma
    ...

    class list_type1
    def createList() :
    for (some conditions)
    camera_list.append(camera(alpha,beta,gamma...)

    class list_type2
    def createList() :
    for (some other conditions)
    camera_list.append(camera(alpha,beta,gamma...)



    data1=list_type1()
    data2=list_type2()


    data1.createList()
    data2.createList()

    The only change above is that I have taken the list appending loop and
    put it into a class of its own.

    However, wheras when the method list_type_2 was not in a class tings
    worked fine, when put into a class as above and the method attempts to
    create an instance of the camera class to append to its list it
    complains.

    I'm pretty sure there is a solution, and I think I will kick myself
    when I work it out (or have it pointed out)!

    Cheers,

    Ben
     
    Ben, Sep 30, 2006
    #2
    1. Advertising

  3. Ben

    Ben Guest

    Ah - I think I've sorted it out.

    I can now have data1.function()

    or

    data2.function()


    etc


    However, I can still call function() directly:

    function()

    which seems very odd Indeed. The only instances of function are within
    classes data1 and data2, and these definitions are different, so I
    don't see why I can get away with calling it without reference to a
    base class - for a start how does it know whivh one to call... will
    investigate :p
     
    Ben, Sep 30, 2006
    #3
  4. Ben

    Steve Holden Guest

    Ben wrote:
    > Apologies if this is te wrong place to post - I realise the question
    > is pretty basic...
    >
    > I have a simple python script that parses a text file and extracts data
    > from it. Currently
    > this data is stored in a list by a function, each element of which is
    > an instance of a class with
    > member variables to take the data. So for example I have:
    >
    > camera_list.append(camera(alpha,beta,gamma....))
    >
    > where
    >
    > class camera:
    > def __init__(self,name,site,address,text):
    > self.name=name
    > self.site=site
    > self.address=address
    > self.text=text
    >
    > Every time I append an item to this list I pass in the constructor
    > parameters so end up with all my data in the list which can then be
    > accessed by doing myList[x].name (for example)
    >
    > This seemed like a nice solution until I tried to put the entire
    > parsing program into its own class. I did this so that I could parse
    > different types of file:
    >
    > thistype.getdata()
    > thattype.getdata()
    > ....
    > thisfile and thatfile would have the same function definitions, but
    > different implementations as needed.
    >
    > But now my list generating funtion needs to create inner "camera"
    > classes when it is itself a member funcition of a class. This seems to
    > be causing problems - I coudl possibly use nested dictionaries, but
    > this sounds messy. Ideally I would use structs defined inside the
    > outer class, but pythn doesn't seem to support these.
    >
    > I hope I haven't rambled too much here - I'm new to python so have
    > probably done some silly things :)
    >

    Sounds like a fairly simple problem, but just the kind to tax a beginner ...

    I think you are mistaken in your belief that the camera classes have to
    be declared inside the file-handler classes: it's quite possible to
    declare them independently and use them anyway. Here's a class whose
    method creates an instance of another class and returns it (though it
    could of course just as easily return a list of such objects):

    In [9]: class Camera1:
    ...: def __init__(self, p1, p2):
    ...: self.p1 = p1
    ...: self.p2 = p2
    ...:

    In [10]: class Camera2:
    ....: def __init__(self, p1, p2):
    ....: self.p1 = p1
    ....: self.p2 = p2
    ....:

    In [11]: class factory:
    ....: def CreateCamera(self, x):
    ....: if x == 1:
    ....: return Camera1("a", "b")
    ....: else:
    ....: return Camera2("x", "y")
    ....:

    In [12]: f = factory()

    In [13]: c1 = f.CreateCamera(1)

    In [14]: c2 = f.CreateCamera("something else")

    In [15]: c1
    Out[15]: <gs.model.Camera1 instance at 0x017E99E0>

    In [16]: c2
    Out[16]: <gs.model.Camera2 instance at 0x0180D940>

    Does this help?

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
     
    Steve Holden, Sep 30, 2006
    #4
  5. Ben

    Ben Guest

    Finally got it all sorted :)

    I got slightly confused because it seems that if you refer to class
    variables from methods within that class you need to explicitly state
    that they are self, otherwise, since class variables are all public in
    python, things could get quite confusing.

    Ben


    Ben wrote:
    > Ah - I think I've sorted it out.
    >
    > I can now have data1.function()
    >
    > or
    >
    > data2.function()
    >
    >
    > etc
    >
    >
    > However, I can still call function() directly:
    >
    > function()
    >
    > which seems very odd Indeed. The only instances of function are within
    > classes data1 and data2, and these definitions are different, so I
    > don't see why I can get away with calling it without reference to a
    > base class - for a start how does it know whivh one to call... will
    > investigate :p
     
    Ben, Sep 30, 2006
    #5
    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. Luke Airig
    Replies:
    0
    Views:
    796
    Luke Airig
    Dec 31, 2003
  2. toton
    Replies:
    11
    Views:
    714
    toton
    Oct 13, 2006
  3. lector

    storing 1,000,000 records

    lector, Apr 6, 2008, in forum: C Programming
    Replies:
    19
    Views:
    478
    Barry Schwarz
    Apr 8, 2008
  4. Dan

    Delete records or update records

    Dan, May 10, 2004, in forum: ASP General
    Replies:
    1
    Views:
    466
    Ray at
    May 10, 2004
  5. Replies:
    3
    Views:
    666
    Anthony Jones
    Nov 2, 2006
Loading...

Share This Page