mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   Programming (https://www.mersenneforum.org/forumdisplay.php?f=29)
-   -   Date conversion with Python (https://www.mersenneforum.org/showthread.php?t=3608)

leifbk 2005-01-21 10:03

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 :w00t:

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 [url]http://docs.python.org/lib/module-time.html[/url]:

[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)
[/CODE]

I hope this might be of use to someone :innocent:

regards, Leif.

ET_ 2005-01-26 14:22

[QUOTE=leifbk]
I hope this might be of use to someone :innocent:

regards, Leif.[/QUOTE]

Could you zip and PM that code to me pls? :innocent:

Luigi

leifbk 2005-01-26 23:00

[QUOTE=ET_]Could you zip and PM that code to me pls? :innocent:

Luigi[/QUOTE]

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()

[/CODE]

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.


All times are UTC. The time now is 13:53.

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