Read file in one hit then close. Make lines of for a class member
Add timing code
This commit is contained in:
parent
ed7122ca31
commit
5e1362abf9
|
@ -37,6 +37,7 @@ import datetime
|
||||||
import fpdb_simple
|
import fpdb_simple
|
||||||
import fpdb_parse_logic
|
import fpdb_parse_logic
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
from time import time
|
||||||
|
|
||||||
class Importer:
|
class Importer:
|
||||||
|
|
||||||
|
@ -47,6 +48,7 @@ class Importer:
|
||||||
self.cursor = None
|
self.cursor = None
|
||||||
self.options = None
|
self.options = None
|
||||||
self.callHud = False
|
self.callHud = False
|
||||||
|
self.lines = None
|
||||||
|
|
||||||
def dbConnect(self, options, settings):
|
def dbConnect(self, options, settings):
|
||||||
#connect to DB
|
#connect to DB
|
||||||
|
@ -70,6 +72,7 @@ class Importer:
|
||||||
self.callHud = value
|
self.callHud = value
|
||||||
|
|
||||||
def import_file_dict(self, options, settings):
|
def import_file_dict(self, options, settings):
|
||||||
|
starttime = time()
|
||||||
last_read_hand=0
|
last_read_hand=0
|
||||||
if (options.inputFile=="stdin"):
|
if (options.inputFile=="stdin"):
|
||||||
inputFile=sys.stdin
|
inputFile=sys.stdin
|
||||||
|
@ -78,19 +81,20 @@ class Importer:
|
||||||
|
|
||||||
self.dbConnect(options,settings)
|
self.dbConnect(options,settings)
|
||||||
|
|
||||||
line=inputFile.readline()
|
# Read input file into class and close file
|
||||||
|
self.lines=fpdb_simple.removeTrailingEOL(inputFile.readlines())
|
||||||
|
inputFile.close()
|
||||||
|
|
||||||
if line.find("Tournament Summary")!=-1:
|
firstline = self.lines[0]
|
||||||
|
|
||||||
|
if firstline.find("Tournament Summary")!=-1:
|
||||||
print "TODO: implement importing tournament summaries"
|
print "TODO: implement importing tournament summaries"
|
||||||
inputFile.close()
|
|
||||||
self.cursor.close()
|
self.cursor.close()
|
||||||
self.db.close()
|
self.db.close()
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
site=fpdb_simple.recogniseSite(line)
|
site=fpdb_simple.recogniseSite(firstline)
|
||||||
category=fpdb_simple.recogniseCategory(line)
|
category=fpdb_simple.recogniseCategory(firstline)
|
||||||
inputFile.seek(0)
|
|
||||||
lines=fpdb_simple.removeTrailingEOL(inputFile.readlines())
|
|
||||||
|
|
||||||
startpos=0
|
startpos=0
|
||||||
stored=0 #counter
|
stored=0 #counter
|
||||||
|
@ -98,10 +102,10 @@ class Importer:
|
||||||
partial=0 #counter
|
partial=0 #counter
|
||||||
errors=0 #counter
|
errors=0 #counter
|
||||||
|
|
||||||
for i in range (len(lines)): #main loop, iterates through the lines of a file and calls the appropriate parser method
|
for i in range (len(self.lines)): #main loop, iterates through the lines of a file and calls the appropriate parser method
|
||||||
if (len(lines[i])<2):
|
if (len(self.lines[i])<2):
|
||||||
endpos=i
|
endpos=i
|
||||||
hand=lines[startpos:endpos]
|
hand=self.lines[startpos:endpos]
|
||||||
|
|
||||||
if (len(hand[0])<2):
|
if (len(hand[0])<2):
|
||||||
hand=hand[1:]
|
hand=hand[1:]
|
||||||
|
@ -120,7 +124,7 @@ class Importer:
|
||||||
|
|
||||||
if (len(hand)<3):
|
if (len(hand)<3):
|
||||||
pass
|
pass
|
||||||
#todo: the above 2 lines are kind of a dirty hack, the mentioned circumstances should be handled elsewhere but that doesnt work with DOS/Win EOL. actually this doesnt work.
|
#todo: the above 2 self.lines are kind of a dirty hack, the mentioned circumstances should be handled elsewhere but that doesnt work with DOS/Win EOL. actually this doesnt work.
|
||||||
elif (hand[0].endswith(" (partial)")): #partial hand - do nothing
|
elif (hand[0].endswith(" (partial)")): #partial hand - do nothing
|
||||||
partial+=1
|
partial+=1
|
||||||
elif (hand[1].find("Seat")==-1 and hand[2].find("Seat")==-1 and hand[3].find("Seat")==-1):#todo: should this be or instead of and?
|
elif (hand[1].find("Seat")==-1 and hand[2].find("Seat")==-1 and hand[3].find("Seat")==-1):#todo: should this be or instead of and?
|
||||||
|
@ -156,7 +160,6 @@ class Importer:
|
||||||
|
|
||||||
if (options.failOnError):
|
if (options.failOnError):
|
||||||
self.db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends.
|
self.db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends.
|
||||||
inputFile.close()
|
|
||||||
self.cursor.close()
|
self.cursor.close()
|
||||||
self.db.close()
|
self.db.close()
|
||||||
raise
|
raise
|
||||||
|
@ -171,7 +174,6 @@ class Importer:
|
||||||
|
|
||||||
if (options.failOnError):
|
if (options.failOnError):
|
||||||
self.db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends.
|
self.db.commit() #dont remove this, in case hand processing was cancelled this ties up any open ends.
|
||||||
inputFile.close()
|
|
||||||
self.cursor.close()
|
self.cursor.close()
|
||||||
self.db.close()
|
self.db.close()
|
||||||
raise
|
raise
|
||||||
|
@ -183,23 +185,22 @@ class Importer:
|
||||||
if ((stored+duplicates+partial+errors)>=options.handCount):
|
if ((stored+duplicates+partial+errors)>=options.handCount):
|
||||||
if (not options.quiet):
|
if (not options.quiet):
|
||||||
print "quitting due to reaching the amount of hands to be imported"
|
print "quitting due to reaching the amount of hands to be imported"
|
||||||
print "Total stored:", stored, "duplicates:", duplicates, "partial/damaged:", partial, "errors:", errors
|
print "Total stored:", stored, "duplicates:", duplicates, "partial/damaged:", partial, "errors:", errors, " time:", (time() - starttime)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
startpos=endpos
|
startpos=endpos
|
||||||
print "Total stored:", stored, "duplicates:", duplicates, "partial:", partial, "errors:", errors
|
print "Total stored:", stored, "duplicates:", duplicates, "partial:", partial, "errors:", errors, " time:", (time() - starttime)
|
||||||
|
|
||||||
if stored==0:
|
if stored==0:
|
||||||
if duplicates>0:
|
if duplicates>0:
|
||||||
for line_no in range(len(lines)):
|
for line_no in range(len(self.lines)):
|
||||||
if lines[line_no].find("Game #")!=-1:
|
if self.lines[line_no].find("Game #")!=-1:
|
||||||
final_game_line=lines[line_no]
|
final_game_line=self.lines[line_no]
|
||||||
handsId=fpdb_simple.parseSiteHandNo(final_game_line)
|
handsId=fpdb_simple.parseSiteHandNo(final_game_line)
|
||||||
else:
|
else:
|
||||||
print "failed to read a single hand from file:", inputFile
|
print "failed to read a single hand from file:", inputFile
|
||||||
handsId=0
|
handsId=0
|
||||||
#todo: this will cause return of an unstored hand number if the last hand was error or partial
|
#todo: this will cause return of an unstored hand number if the last hand was error or partial
|
||||||
self.db.commit()
|
self.db.commit()
|
||||||
inputFile.close()
|
|
||||||
self.cursor.close()
|
self.cursor.close()
|
||||||
self.db.close()
|
self.db.close()
|
||||||
return handsId
|
return handsId
|
||||||
|
|
Loading…
Reference in New Issue
Block a user