Jump to content

radarstorm

Members
  • Posts

    4
  • Joined

  • Last visited

Posts posted by radarstorm

  1. I find shell scripts arcane and difficult to understand, especially the one liners! I would use a delightful python script, for example:

    import os,sys,glob,time
    
    if len(sys.argv) != 3:
        print 'Usage %s: wildcard time_to_wait'
        raise SystemExit
    
    stub = sys.argv[1]
    wait = sys.argv[2]
    
    try:
        wait = int(wait)
    except ValueError:
        print 'Invalid wait time, use an integer number of seconds'
        raise SystemExit
    
    done = False
    while not done:
        now = time.time()
        modify_times = [(now-os.stat(filename).st_mtime) for filename in glob.glob(stub)]
        modify_times.sort() #ascending sort, so the first one is the most recent
    
        if modify_times[0] > wait:
            done = True
        else:
            #Don't kill the cpu by sitting in this loop forever, the absolute quickest that we could
            #exit this loop is wait-modify_times[0], so sleep for that long
            time.sleep(wait-modify_times[0])
    
    

    It waits until all of the files matched by a wildcard (e.g file*) are older than a given number of seconds. You need to escape the wildcard so it doesn't get expanded by the os though, ./wait.py "file*" 300 for example

  2. Whoa! :) Your the man! Looks alot different in python, I will be sure to take notes and google alot. You are very talented, thanks for your help.

    What is this? "return '$%d.%02d' % (x/100,x%100)" and the other /% codes or \n and such? (I understand the return statement just not the %\stuff) So does the programming go backwards with python? or I guess instead of closing the statement at the end of each module or function you end it at the very end of the python file? for example would this be correct?..

    #commenting my python code

    def helloworld()

    #######################putting a bunch of these - can I? without an error?? ##################

    print "Hello world!"

    print "This is just a test..."

    The currencyFormat looks a bit odd because it wasn't implemented in the original. I took a guess that it was presenting an integer representing the number of cents (say 1234) and displaying that as a string like $12.34. The line '$%d.%02d' % (x/100,x%100) is an example of the old-style of python string formatting. It says create a string that consists of a $ followed by a number, followed by a dot, followed by a 2 digit number padded with zeros. It then fills the first number with x/100 (so the number of dollars) and the second with x mod 100 (the remainder when x is divided by 100), which is the number of cents.

    Regarding the main function being at the end rather than the beginning, it's not that python is backwards, but rather that it is interpreted line by line. In your pseudocode example you had to create a main function, with the understanding that when your code was run that function would be executed first. When python scripts are executed they are simply executed line by line from the top. The functions are created first with def commands, which sets them up but doesn't execute them. Once they are set up we call them just like the main function in your pseudocode.

    Your example illustrates the above quite nicely. If you were to run it (after adding a colon to the end of def helloworld and indenting the subsequent 2 prints) you would find that it doesn't do anything. That's because you've created your function, but not called it. You could either add a line helloworld() at the end, or just get rid of the function definition and have your script just be the print lines.

    It sounds like you'd benefit from playing with a python interpreter, there are several you can access through a browser online, for example http://try-python.mired.org/

  3. I think so.. I'm still trying to figure out what the heirarchy chart of a program written in python would look like..

    or maybe a flowchart. So the function / module ends at the end of the .py file? and you can import variables and such from another .py file that you import at the beginning of the main modules code?

    How would this puesdo code translate to python?

    ...

    Something like this:

    def currencyFormat(x):
        return '$%d.%02d' % (x/100,x%100)
    
    def salesHeader():
        print "Brewster's Used Cars, Inc. Monthly Sales report"
        print
        print "Sales persons ID                                Sale amount"
        print "========================================"
    
    def salesDetails():
        #accumulator variables
        salesTotal = 0
        total      = 0
        salesFile  = open("sales.dat")
    
        currentID = salesID = None
    
        for line in salesFile:
            salesID,salesAmount = line.strip().split()
            salesAmount = int(salesAmount)
            if salesID != currentID: 
                if currentID != None: #Don't print this on the first iteration of the loop
                    print "Total salesAmounts for sales agent: ",currencyFormat(salesTotal)
                    print
                currentID = salesID
                salesTotal = 0
    
            print salesID, '\t', currencyFormat(salesAmount)
            #update the accumulators
            salesTotal += salesAmount
            total      += salesAmount
    
    
        #print the total for the last agent
        print " Total sales for this salesperson is: ",currencyFormat(salesTotal)
        print " Total of all the sales: ",currencyFormat(total)
        salesFile.close()
    
    salesHeader()
    salesDetails()

×
×
  • Create New...