You are not logged in.

#1 2009-07-17 11:27:47

tcoffeep
Member
From: Timmins, Ontario
Registered: 2008-11-26
Posts: 99

[PYTHON] Array problems, I think? [SOLVED]

Hi. I'm trying to make a little script, as a pet project while I try not to sleep, and it doesn't seem to be working out the way I want it to.

Here's the code :

from time import localtime, strftime
import os.path, re

def getTime():
        time = strftime("%H%M%S", localtime())
        return time

def getDate():
        date = strftime("%a", localtime())
        return date

def loadFile( file ):
        if file == "weekend":
                filename = "weekend.lst"
        else:
                filename = "weekday.lst"
        check = (os.path.isfile(filename))
        if check == True:
                checkline = []
                openf = open(filename, 'r')
                linecheck = re.compile(r'^\@(\d+)\((.+)\)$')
                for line in openf:
                        checkline = linecheck.search(line).groups()
                        print checkline
        else:
                print "### ERROR ### : File '", filename, "' does not exist."


date = getDate()
a = 0

if date == "Fri" or date == "Sat" or date == "Sun":
        file = "weekend"
else:
        file = "weekday"
loadFile( file )

When it loads into the array ( the line "print checkline" ), i can only print :

checkline[0], checkline[1]

else I go out of range. How do I load the file into an array, then? I'm very confused sad

oh, and it loads from a file built like this :

@163000(Play with Jayde)
@170000(Feed Jayde)
@173000(Free time)
@183000(Go for a walk)
@190000(Read book)

Last edited by tcoffeep (2009-07-21 05:14:18)


=============== Read An Essay ===============
   Distro : Funtoo Linux || Kernel : ckernel-2.6.30-gentoo-r5
Processor : Athlon 64 X2 4400+ || RAM : 2GB || HD : 300GB
========================================

Offline

#2 2009-07-17 11:40:11

mikesd
Member
From: Australia
Registered: 2008-02-01
Posts: 788
Website

Re: [PYTHON] Array problems, I think? [SOLVED]

Replace:

 for line in openf:

with:

for line in openf.readlines():

should work.

Last edited by mikesd (2009-07-17 11:42:09)

Offline

#3 2009-07-17 11:44:47

foutrelis
Developer
From: Athens, Greece
Registered: 2008-07-28
Posts: 705
Website

Re: [PYTHON] Array problems, I think? [SOLVED]

You're assigning the two groups your regular expression is returning to checkline. What you probably want to do is append them to checkline, as you're declaring it as a list.

help([].append)

Also, don't program when you're sleepy. :>

Last edited by foutrelis (2009-07-17 11:46:32)

Offline

#4 2009-07-17 12:16:50

tcoffeep
Member
From: Timmins, Ontario
Registered: 2008-11-26
Posts: 99

Re: [PYTHON] Array problems, I think? [SOLVED]

But i have to do something, and thank you. I'll try out both of your suggestions right away!


=============== Read An Essay ===============
   Distro : Funtoo Linux || Kernel : ckernel-2.6.30-gentoo-r5
Processor : Athlon 64 X2 4400+ || RAM : 2GB || HD : 300GB
========================================

Offline

#5 2009-07-17 12:59:11

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [PYTHON] Array problems, I think? [SOLVED]

@mikesd:  in an iterator context, a file object returns each line in it successively, so the original code does the same as yours.

@tcoffeep:  Perhaps it would help if you told us what you want to achieve.  What should checkline look like when the for loop in loadFile finishes?

Offline

#6 2009-07-17 13:45:34

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,092

Re: [PYTHON] Array problems, I think? [SOLVED]

As a sidenote:

check = (os.path.isfile(filename))
if check == True:
    do stuff

|
v

if os.path.isfile(filename):
    do stuff

Or just try to open the file and catch the exception if it fails.


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#7 2009-07-17 21:29:24

tcoffeep
Member
From: Timmins, Ontario
Registered: 2008-11-26
Posts: 99

Re: [PYTHON] Array problems, I think? [SOLVED]

Trent wrote:

@tcoffeep:  Perhaps it would help if you told us what you want to achieve.  What should checkline look like when the for loop in loadFile finishes?

Basically, it parses through the file, and at certain times ( that would be the @070000, being 7am ), it displays what is listed.

So, if the line says :

@070000(Do stuff)

At 7am until the next displayed time, it would display : "Do stuff".

I'm trying to gain some skills with Python, and I thought this would be a fun project before I take on another sizable project which I have no idea how to even begin (it parses XML and translates it into another XML structure).


=============== Read An Essay ===============
   Distro : Funtoo Linux || Kernel : ckernel-2.6.30-gentoo-r5
Processor : Athlon 64 X2 4400+ || RAM : 2GB || HD : 300GB
========================================

Offline

#8 2009-07-17 23:12:20

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [PYTHON] Array problems, I think? [SOLVED]

Okay, I've taken your code and edited it somewhat.  I'm not sure where you were going with the original, so I made some significant changes.

The most significant change (from a design standpoint) was to make loadFile return a dictionary mapping strings such as '070000' to messages like 'Do stuff'.  Your main code can then iterate through the items in the dictionary.

I made a few other changes:
* pulled the file/filename logic out of loadFile because, well... it just doesn't belong in there.  (Please feel free to disagree.)
* compulsively changed the error message so the line would fit in 80 columns, and directed it to stderr instead of stdout
* exited the script after printing the error message
* used the new str.format method of string formatting, purely out of force of habit

If it were my project, I would also get rid of getTime and getDate (because their implementations are trivial).

Anyway, I hope this gets you started and helps you understand what you were having trouble with.

from time import strftime, localtime
import sys, os.path, re

def getTime():
        time = strftime("%H%M%S", localtime())
        return time

def getDate():
        date = strftime("%a", localtime())
        return date

def loadFile(filename):
    if not os.path.isfile(filename):
        sys.stderr.write("ERROR: File '{0}' does not exist.\n".format(filename))
        sys.exit(-1)
    alarms = {}
    openf = open(filename, 'r')
    linecheck = re.compile(r'^@(\d+)\((.+)\)$')
    for line in openf:
        time, message = linecheck.search(line).groups()
        alarms[time]= message
    return alarms

date = getDate()

if date == 'Fri' or date == 'Sat' or date == 'Sun':
    file = "weekend.lst"
else:
    file = "weekday.lst"
alarms = loadFile(file)

for time in sorted(alarms):
    # Wait for the indicated time
    # Print the message
    print '{0}: {1}'.format(time, alarms[time])

Edit:  In retrospect, my post seems a little eager to find fault.  Please don't take this the wrong way.  I simply took your code and thought "how would I rewrite this to meet its stated goal?" and this is what I came up with.  I wouldn't presume to tell you how you should write your program.

Last edited by Trent (2009-07-18 14:41:10)

Offline

Board footer

Powered by FluxBB