mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2005-01-21, 10:03   #1
leifbk
 
leifbk's Avatar
 
May 2004
Oslo, Norway

23·3·5 Posts
Default Date conversion with Python

I'm dabbling with a small MySQL database that stores my finished exponents. A Python script gets the cleared exponent from the Individual report, and then does some munging to get the data into a MySQL insert command. I won't post the entire code here at the moment, but if anyone is interested, I just might do it in a while

Now, to convert the date from GIMPS format ("21-Jan-05 10:43") to MySQL format ("2005-01-21 10:43:00") I found that Python has excellent built-in functionality to do so. The documentation is found online at http://docs.python.org/lib/module-time.html:

Code:
from time import strptime, strftime

def convertDate(mystring): 
    mytimeobj = strptime(mystring, "%d-%b-%y %H:%M")
    return strftime("%Y-%m-%d %H:%M:00", mytimeobj)
I hope this might be of use to someone

regards, Leif.
leifbk is offline   Reply With Quote
Old 2005-01-26, 14:22   #2
ET_
Banned
 
ET_'s Avatar
 
"Luigi"
Aug 2002
Team Italia

475510 Posts
Default

Quote:
Originally Posted by leifbk
I hope this might be of use to someone

regards, Leif.
Could you zip and PM that code to me pls?

Luigi
ET_ is online now   Reply With Quote
Old 2005-01-26, 23:00   #3
leifbk
 
leifbk's Avatar
 
May 2004
Oslo, Norway

23·3·5 Posts
Default

Quote:
Originally Posted by ET_
Could you zip and PM that code to me pls?

Luigi
What do you need, -- the entire data definition, or just the input script? The latter isn't bigger than I just post it right here:

Code:
#! /usr/bin/env python

# ll_return, leifbk 2005
# purpose: Insert finished exponent from GIMPS into MySQL database.
# this program fetches parameters directly from server with wget, and
# turns them into MySQL-readable data.

# All you have to enter is the exponent number as a parameter. This should of
# course exist in the Cleared Exponents sections of the Individual Report, or
# the effect of this script will be totally unpredictable. Compare with eg.
# assign.py for an assessment of the improvement in automation level.

# this first version is very crude and has no error handling at all. Use at own risk.


import os, sys, MySQLdb
from time import strptime, strftime
from p90 import calc_p90

def wgetline(exp):  # see Python Cookbook p. 228
    str(exp)        # just to ensure that exp _is_ a string
    print "Getting exponent from server ..."
    command = 'wget -q -O - \
        http://mersenne.org/cgi-bin/primenet_report.pl\?UserID=xxx&userPW=yyy \
        | grep ' + exp
    child = os.popen(command)
    data = child.read()
    return data

def convertDate(mystring): # converts from GIMPS date format into MySQL date format.
    # RTFM at http://docs.python.org/lib/module-time.html
    mytimeobj = strptime(mystring, "%d-%b-%y %H:%M")
    return strftime("%Y-%m-%d %H:%M:00", mytimeobj)

def getcid(computername):
    myCursor = myConnection.cursor()
    myCursor.execute("select computer_id from computers \
                        where computer_name like %s", computername)
    row = myCursor.fetchone()
    return str(row[0])

if __name__ == '__main__':
    # open db
    myConnection = MySQLdb.connect(user="xxx", passwd="yyy", db="gimps")

    myline = wgetline(sys.argv[1])
    print myline
#   22788427  68     0x630FB7626E0591__                03-Jan-05 06:09  brutalis
    exp = myline[0:8]
    bts = myline[10:12]
    typ = myline[14]
    if typ == ' ': typ = '1'            # LL test
    elif typ == 'D': typ = '2'          # DC test
    res = myline[17:35]
    dtr = convertDate(myline[51:66])
    cid = getcid(myline[68:76])
    p90 = str(calc_p90(int(exp)))

    print "Exponent = " + exp
    print "Bits     = " + bts
    print "Type     = " + typ
    print "Residual = " + res
    print "Returned = " + dtr
    print "Computer = " + cid
    print "P-90 yrs = " + p90

    inp = raw_input("Is this information correct (y/n) ? ")
    if inp in ("n", "N"):
        print "Entry aborted."
        sys.exit(1)
    else:
        print "Adding exponent to database ..."
        myCursor = myConnection.cursor()
        myCursor.execute("insert into ll_test (exponent, bits, test_type, \
                            residual, finished, computer_id, on_line, p90_years) \
                            values (%s, %s, %s, %s, %s, %s, %s, %s)", \
                            (exp, bts, typ, res, dtr, cid, 1, p90))
    myConnection.close()
The part in "main" that extracts the fields should really be put in its own module, as it's quite useful and probably needed elsewhere.

The column "on_line" in the ll_test table is a "logical" field containing either 0 or 1 depending on the exponent's PrimeNet status. For the first months I used manual assignment and return for one of my computers, that's why I created this field. Nowadays, it' always 1 for new exponents.

HTH, Leif.
leifbk is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion GHz to GFLOPS? preda PrimeNet 11 2017-12-02 21:51
v5 Server Conversion compusion Software 3 2008-11-14 19:22
PS3 programmers(program conversion for pay?) jasong Programming 5 2007-12-16 00:10
Units Conversion Puzzle JHagerson Lounge 19 2005-11-24 05:38
conversion to GF(2) bigbud Math 9 2005-04-16 01:13

All times are UTC. The time now is 10:37.

Tue Jul 7 10:37:51 UTC 2020 up 104 days, 8:10, 1 user, load averages: 2.35, 2.36, 2.37

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.