Jump to content

modern languages - working with files


Recommended Posts

Check out:

How to Think Like a Computer Scientist - Learning with Python

Online

http://www.ibiblio.org/obp/thinkCSpy/

Download HTML

http://www.ibiblio.org/obp/thinkCSpy/dist/thinkCSpy.html.tgz

PDF

http://www.ibiblio.org/obp/thinkCSpy/dist/thinkCSpy.pdf

 

It is a good book.

Edited by johnnyv
Link to comment
Share on other sites

thanks for that link.

 

I'm gonna read up on python - on the web, and probably will buy one introductory book. I'm an old guy - still like to read books.

 

Anyway, I'm still unsure how files are updated these days, say in python.

 

For example, lets say I alread have a Record Album Collection file with hundreds of entries in it. I run a python program that accesses the file and displays a record from it (I'm saying a record, but I guess it is called a line now). So on my GUI screen I now see the following info for one album:

 

Artist: Pink Fairies

Title: What a Bunch of Sweeties

Label: Polydor

Number: 555-5555

Released: 1971

Bought: 1979

Price: 8.99

Where: Sound Warehouse

Condition: NM

Comments: autographed

 

Lets say I change one or more of the fields... how does python know where to write the changed info, or would it re-write the entire record (line)? I mean, does it work the same way that I am used to in procedural languages: You read in a record and then each field would have whatever value. If you change one of the fields, and then write the record, it just puts the new info over the old in the same place. Now, I haven't written any program (cobol) since around 1990 so I am rusty in this stuff.

Link to comment
Share on other sites

Well, It's up to you to write code to parse the file.

 

Take a look at my notes program, i implement a lot of file access.

http://www.mandrakeusers.org/index.php?showtopic=15687

 

 

It makes the configuration and database in ~/.gnunotes/ so feel free to trawl through it manually.

 

I wrote a function to check whether the files exist, and if they are read only. It is capable of making them if they don't exist. This function i believe is not the easiest way to do this, but it works well and you are free to use it in whichever program you want.

 

The 'database' It isnt complex, it's ~/.gnunotes/db-main and just follows this pattern,

 

name

this is the note

crazy

iphitus is a crazy bugger

whoa

whoa!!! this is note 3!!!

 

 

Read through the code, i load the database into a python list with readlines(), every line is a entry in the list. Then i use loops to split it's contents into the two lists that the program uses, note_data and note_name

 

If you have any questions with the code, let me know.

 

iphitus

Link to comment
Share on other sites

It sounds like you would be better off putting that info into a mysql/postgres/firebird/sqlite(all free) database and using the relevant python db module to work with the data.

Link to comment
Share on other sites

I just checked out a tutorial on mysql. Looks like what I want. I've been wanting to learn it anyway.

 

Last year in my Intro to Db course, we spent a few weeks on Oracle 9i / SQL (it came with our textbooks - 5 CDs). So I am slightly familiar with SQL. One of our assignments to do at home was to create tables & such for a Video Collection, and then put some data into it. Wasn't too bad if I remember. If I learn mysql, would it be very similar to regular sql (like oracle's) ? Or is it alot different, with different commands and such? Meaning, I don't like learning stuff unless it'll benefit me career-wise.

Link to comment
Share on other sites

Should be similar, mysql doesn't have all the functions of oracle but it should be good enough.

 

for example if you create a db with a table called "list" which has the fields "name", "age" and "size".

Sql statements are:

To insert
insert into list values("bob", 30, "large");
To update
update list set age = 31 where name = "bob";
To select
select * from list where name = "bob";
To delete
delete from list where name = "bob";

Edited by johnnyv
Link to comment
Share on other sites

looks about the same to me.

 

I just got through doing a "yum install mysql", and it looks like it installed correctly. And I checked if I had python installed from my original FC2 install, and looks like it is also there. I got java installed last week, so I'm getting a real development machine going here... :D

 

Now to learn it all... :unsure:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...