-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHistoryDB.py
87 lines (67 loc) · 2.28 KB
/
HistoryDB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
#
# Keep track of and use information from out previous runs. The DB is just a tab delimited file.
import csv
import Title
import Papers
import Matchup
NEW = "new"
TITLE = "title"
AUTHORS = "authors"
DOI = "doi"
COMMENTS = "comments"
COLUMNS = [NEW, TITLE, AUTHORS, DOI, COMMENTS]
class HistoryDB(object):
def __init__(self, csvInFileName):
self.byTitleLower = {}
self.byDoi = {}
csvIn = open(csvInFileName, "r")
csvReader = csv.DictReader(csvIn, fieldnames=COLUMNS, dialect="excel-tab")
for row in csvReader:
self.byTitleLower[Title.strip(row[TITLE])] = row
self.byDoi[row[DOI]] = row
csvIn.close()
return None
def getByTitleLower(self, lowerTitle):
"""
Returns None if we don't know about this title.
"""
return(self.byTitleLower.get(lowerTitle))
def getByDoi(self, doi):
"""
Returns None if we don't know about this DOI.
"""
return(self.byDoi.get(doi))
def getEntryGivenMatchup(self, matchup):
"""
Use information in a matchup to locate that paper's comments in the
history DB. Can use DOI or lower title
"""
entry = None
doi = matchup.getDoiFromPapers()
if doi:
entry = self.getByDoi(doi)
else:
entry = self.getByTitleLower(matchup.lowerTitle)
return(entry)
def writeHistory(matchups, sortedTitles, csvOutFileName, priorHistoryDb):
csvOut = open(csvOutFileName, "w")
csvWriter = csv.DictWriter(csvOut, fieldnames=COLUMNS, dialect="excel-tab")
csvWriter.writeheader()
for title in sortedTitles:
matchup = matchups[title]
row = {}
newPaper = not matchup.culEntries
if newPaper:
row[NEW] = 1
else:
row[NEW] = 0
row[TITLE] = matchup.papers[0].title
row[AUTHORS] = matchup.papers[0].authors
row[DOI] = Papers.getDoiFromPaperList(matchup.papers)
priorHistoryEntry = priorHistoryDb.getEntryGivenMatchup(matchup)
if priorHistoryEntry:
row[COMMENTS] = priorHistoryEntry[COMMENTS]
csvWriter.writerow(row)
return None