473,668 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing records from a parsed file

Ben
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.app end(camera(alph a,beta,gamma... .))

where

class camera:
def __init__(self,n ame,site,addres s,text):
self.name=name
self.site=site
self.address=ad dress
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.getdat a()
thattype.getdat a()
....
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

Sep 30 '06 #1
4 1161
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.app end(camera(alph a,beta,gamma... .))

where

class camera:
def __init__(self,n ame,site,addres s,text):
self.name=name
self.site=site
self.address=ad dress
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.getdat a()
thattype.getdat a()
....
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(se lf, 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.Camer a1 instance at 0x017E99E0>

In [16]: c2
Out[16]: <gs.model.Camer a2 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

Sep 30 '06 #2
Ben
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,a lpha,beta,gamma ...):
self.alpha=alph a
self.beta=beta
self.gamma=gamm a
...

for (some conditions)
camera_list.app end(camera(alph a,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
collectSomeOthe rData.getData()

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

So something like:

class camera:
def __init__(self,a lpha,beta,gamma ...):
self.alpha=alph a
self.beta=beta
self.gamma=gamm a
...

class list_type1
def createList() :
for (some conditions)
camera_list.app end(camera(alph a,beta,gamma... )

class list_type2
def createList() :
for (some other conditions)
camera_list.app end(camera(alph a,beta,gamma... )

data1=list_type 1()
data2=list_type 2()
data1.createLis t()
data2.createLis t()

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

Sep 30 '06 #3
Ben
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

Sep 30 '06 #4
Ben
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
Sep 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4141
by: jeff brubaker | last post by:
Hello, Currently we have a database, and it is our desire for it to be able to store millions of records. The data in the table can be divided up by client, and it stores nothing but about 7 integers. | table | | id | clientId | int1 | int2 | int 3 | ... | Right now, our benchmarks indicate a drastic increase in performance if we divide the data into different tables. For example,...
8
1531
by: DeadCert | last post by:
Hi, How can I store all my scripts on a separate page and then call them from the html page that I want to use them on? the <head> section is getting too cluttered and I would like to store all in one place. Anyone know how? Cheers
3
4715
by: hamvil79 | last post by:
I'm implementig a java web application using MySQL as database. The main function of the application is basically to redistribuite documents. Those documents (PDF, DOC with an average size around 2Mb) are stored in BLOB column. The amount of documents for the first year should not exceed 5/6 Giga, but I cannot make prevision for the next years. Those documents are mainly just accessed (update and delete are not so
2
7230
by: Pecanfan | last post by:
Hi, I'm trying to come up with a way of generating 'warning' e-mails based on specific criteria within an Access (2003) database. I've decided to do this by exporting specific information to a text file which then gets parsed by a simple shell program and processed by a 3rd party SMTP engine (Blat). With my VERY limited knowledge of VB and Access coding, this is what I've come up with so far:-
14
5830
by: Luiz Antonio Gomes Pican?o | last post by:
How i can store a variable length data in file ? I want to do it using pure C, without existing databases. I'm thinking to use pages to store data. Anyone has idea for the file format ? I want to store data like a database: ---------------------------------- Custumer:
6
1726
by: Kieran Benton | last post by:
Hi, I have quite a lot of metadata in a WinForms app that I'm currently storing within a hashtable, which is fine as long as I know the unique ID of the track (Im storing info on media files). Up until now I've been sending the track info to a seperate server app using serialization/sockets which stores the metadata in a mysql db so that I can query on Artist/Album etc as well. However, I'm looking to remove the server and move into a more...
15
2899
by: Jaraba | last post by:
I am working in a project that I need to parse an arrayt an select records based upon the values parsed. I used the functions developed by Knut Stolze in his article 'Parsing Strings'. I am particulary having problems inserting records using a function. I am attaching the code for your review. Please, help. =================================--Function 1--====================================
3
1547
by: ArmsTom | last post by:
I was using structures to store information read from a file. That was working fine for me, but then I read that anything stored in a structure is added to the stack and not the heap. So, I made a class that stores the same information. The user selects any number of records from the file when the program loads & can then make changes. The records the user selects are added to an array and changes are made to the records in that array...
6
1296
by: Lint Radley | last post by:
Hi Everyone, I need an opinion here on storing data for a program I am working on the processes DICOM images. Essentially, my program stores 25-45 (it varies depending on the user) ranges of pixel values to search the image for. Currently, I am using a .MDF database that requires SQL Express to be installed. For only 25-45 records it seems like overkill to me. That being said, I love the ability to easily update records, delete, add,...
0
8381
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8797
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8583
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8656
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7401
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6209
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5681
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2791
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1786
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.