Skip to content

Commit

Permalink
BibTeX in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Gärtner committed Jul 5, 2017
1 parent 3c16e6c commit 0e6f0fd
Show file tree
Hide file tree
Showing 15 changed files with 367 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Tutorial/01 SQLite in Python/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sqlite3 import connect

class DB:
def __init__(self,path="example/people.db"):
def __init__(self,path="people.db"):
self.conn = connect(path)
self.c = self.conn.cursor()

Expand Down
Binary file added Tutorial/01 SQLite in Python/people.db
Binary file not shown.
3 changes: 1 addition & 2 deletions Tutorial/01 SQLite in Python/sql_fill.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import sqlite3

conn = sqlite3.connect('example/people.db')
conn = sqlite3.connect('people.db')
c = conn.cursor()

c.execute("DROP TABLE people")
c.execute("CREATE TABLE people (orcid CHARACTER(16), start DATE, end DATE)")
c.execute("INSERT INTO people VALUES ('0000000219094153','1900-01-01','2016-12-31')")
c.execute("INSERT INTO people VALUES ('000000020183570X','1900-01-01','2016-12-31')")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sqlite3 import connect

class DB:
def __init__(self,path="example/people.db"):
def __init__(self,path="people.db"):
self.conn = connect(path)
self.c = self.conn.cursor()

Expand Down
Binary file not shown.
2 changes: 1 addition & 1 deletion Tutorial/03 Rest API/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sqlite3 import connect

class DB:
def __init__(self,path="example/people.db"):
def __init__(self,path="people.db"):
self.conn = connect(path)
self.c = self.conn.cursor()

Expand Down
Binary file added Tutorial/03 Rest API/people.db
Binary file not shown.
2 changes: 1 addition & 1 deletion Tutorial/04 Sorting and Filter 2/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sqlite3 import connect

class DB:
def __init__(self,path="example/people.db"):
def __init__(self,path="people.db"):
self.conn = connect(path)
self.c = self.conn.cursor()

Expand Down
Binary file added Tutorial/04 Sorting and Filter 2/people.db
Binary file not shown.
209 changes: 209 additions & 0 deletions Tutorial/05 BibTeX in Python/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
from sqlite3 import connect

class DB:
def __init__(self,path="people.db"):
self.conn = connect(path)
self.c = self.conn.cursor()

def getList(self):
self.c.execute('SELECT * FROM people')
return self.c.fetchall()

def close(self):
self.conn.close()

class Date:
def __init__(self, y, m, d):
self.y = int(y)
self.m = int(m) if m else None
self.d = int(d) if d else None
def check (self, other, attr):
if getattr(self,attr) == None or getattr(other,attr) == None:
return 1
if getattr(self,attr) < getattr(other,attr):
return 1
if getattr(self,attr) > getattr(other,attr):
return -1
return 0
__le__ = lambda self, other: True if 1 == (self.check(other,"y") or self.check(other,"m") or self.check(other,"d") or 1) else False
__str__ = lambda self: str(self.y) + "-" + str(self.m) + "-" + str(self.d)

class OrcID:
def __init__(self, id, start, stop):
self.id = id
self.start = Date(*start.split("-"))
self.stop = Date(*stop.split("-"))
getID = lambda self: "-".join([self.id[4 * i : 4 * (i + 1)] for i in range(4)])
__str__ = lambda self: self.getID() + ": " + str(self.start) + " - " + str(self.stop)

class WorkSummary:
def __init__(self, path, title, date):
self.path = path
self.title = title
self.date = date
__lt__ = lambda self, other: self.date.y < other.date.y or (self.date.y == other.date.y and self.title < other.title)
__eq__ = lambda self, other: self.date.y == other.date.y and self.title == other.title
__str__ = lambda self: self.title + ": " + str(self.date)

from requests import Session
class API:
auth = "https://sandbox.orcid.org/oauth/token"
ORC_client_id = "APP-DZ4II2NELOUB89VC"
ORC_client_secret = "c0a5796e-4ed3-494b-987e-827755174718"
def __init__(self):
self.s = Session()
self.s.headers = {'Accept': 'application/json'}
data = {"grant_type":"client_credentials", "scope":"/read-public","client_id":self.ORC_client_id, "client_secret":self.ORC_client_secret}
r = self.s.request(method ="post",url= self.auth, data=data)
self.s.headers = {'Accept': 'application/json', "Access token":r.json()["access_token"]}
baseurl = "https://pub.sandbox.orcid.org/v2.0"
getDate = lambda self,d: Date(d["year"]["value"],d["month"]["value"] if d["month"] else None, d["day"]["value"] if d["day"] else None )
def getWorks(self,id):
r = self.s.request(method= "get",url = "{0}/{1}/works".format( self.baseurl, id.getID()))
for work in (w["work-summary"][0] for w in r.json()["group"]):
yield WorkSummary(work["path"],work["title"]["title"]["value"],self.getDate(work["publication-date"]))
def getWork(self, summary):
r = self.s.request(method= "get",url= self.baseurl + summary.path)
return r.json()['citation']['citation-value']

import itertools

from pybtex.database import parse_string, BibliographyData
def joinBibliography(bib1, bib2):
for key in bib2.entries:
bib1.entries[key] = bib2.entries[key]

#if __name__ == "__main__":
# db = DB()
# orcs = [OrcID(*t) for t in db.getList()]
# db.close()
# alldocs = []
# api = API()
# for orc in orcs:
# alldocs += [d for d in api.getWorks(orc) if orc.start <= d.date <= orc.stop]
# alldocs.sort()
# uniqdocs = [doc for doc,_ in itertools.groupby(alldocs)]
# bib = BibliographyData()
# for d in uniqdocs:
# joinBibliography (bib,parse_string(api.getWork(d),"bibtex"))
# bib.to_file(open("out.bib","w"))

from pybtex.style.formatting.unsrt import Style
from pybtex.backends.html import Backend
#if __name__ == "__main__":
# db = DB()
# orcs = [OrcID(*t) for t in db.getList()]
# db.close()
# alldocs = []
# api = API()
# for orc in orcs:
# alldocs += [d for d in api.getWorks(orc) if orc.start <= d.date <= orc.stop]
# alldocs.sort()
# uniqdocs = [doc for doc,_ in itertools.groupby(alldocs)]
# bib = BibliographyData()
# for d in uniqdocs:
# joinBibliography (bib,parse_string(api.getWork(d),"bibtex"))
# style = Style()
# formatbib = style.format_bibliography(bib)
# back = Backend()
# back.write_to_file(formatbib,"out.html")


from pybtex.richtext import Tag,Text,Symbol,HRef

class HtmlTag(Tag):
def __init__(self, name, opt, *args):
super(HtmlTag,self).__init__(name, *args)
self.options = opt
def render(self, backend):
text = super(Tag, self).render(backend)
try:
return backend.format_tag(self.name, text, self.options)
except TypeError:
return backend.format_tag(self.name, text)

class HtmlStyle(Style):
def format_article(self, context):
ret = Text()
ret += HtmlTag("h4","style=\"margin-bottom: 2px;\"", context.rich_fields['title'])
ret += Tag("i",context.rich_fields['author']) + Symbol('newblock')
ret += context.rich_fields['journal']
if 'volume' in context.fields:
ret += Symbol("nbsp") + context.rich_fields['volume']
if 'number' in context.fields:
ret += Symbol("nbsp") + "(" + context.rich_fields['number'] + ")"
if 'pages' in context.fields:
ret = ret + ":" + context.rich_fields['pages']
if 'doi' in context.fields:
ret += Symbol('newblock') + HRef('https://doi.org/' + context.fields['doi'],"[ Publishers's page ]")
return HtmlTag("div","class=\"" + context.fields['year'] + " mix \"",ret)

#if __name__ == "__main__":
# db = DB()
# orcs = [OrcID(*t) for t in db.getList()]
# db.close()
# alldocs = []
# api = API()
# for orc in orcs:
# alldocs += [d for d in api.getWorks(orc) if orc.start <= d.date <= orc.stop]
# alldocs.sort()
# uniqdocs = [doc for doc,_ in itertools.groupby(alldocs)]
# bib = BibliographyData()
# for d in uniqdocs:
# joinBibliography (bib,parse_string(api.getWork(d),"bibtex"))
# style = HtmlStyle()
# style.sort = lambda x: sorted(x, key = lambda e:-int(e.fields['year']))
# formatbib = style.format_bibliography(bib)
# back = Backend()
# back.write_to_file(formatbib,"out.html")

class HtmlBackend(Backend):
symbols = {'ndash': u'&ndash;', 'newblock': u'<br/>\n', 'nbsp': u'&nbsp;'}
format_tag = lambda self, tag, text, options =None: u'<{0} {2} >{1}</{0}>'.format(tag, text, options if options else "") if text else u''
label = None
def write_entry(self, key, label, text):
if label != self.label:
self.output(u'<h3 class=\"{0} year\">{0}</h3>\n'.format(label))
self.label = label
self.output(u'%s\n' % text)
write_epilogue = lambda self: self.output(u'</div></body></html>\n')
prologue = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head><meta name="generator" content="Pybtex">
<meta http-equiv="Content-Type" content="text/html; charset=%s">
<title>Bibliography</title>
{HEAD}
</head>
<body>
{BODY}
<div id="content">
"""
def prepout (self, head, body):
self.prologue = self.prologue.format(HEAD = head, BODY = body)
def write_prologue(self):
try:
self.prepout("","")
except ValueError:
pass
self.output(self.prologue % (self.encoding or pybtex.io.get_default_encoding()))


if __name__ == "__main__":
db = DB()
orcs = [OrcID(*t) for t in db.getList()]
db.close()
alldocs = []
api = API()
for orc in orcs:
alldocs += [d for d in api.getWorks(orc) if orc.start <= d.date <= orc.stop]
alldocs.sort()
uniqdocs = [doc for doc,_ in itertools.groupby(alldocs)]
bib = BibliographyData()
for d in uniqdocs:
joinBibliography (bib,parse_string(api.getWork(d),"bibtex"))
style = HtmlStyle()
style.sort = lambda x: sorted(x, key = lambda e:-int(e.fields['year']))
style.format_labels = lambda x: [int(e.fields['year']) for e in x]
formatbib = style.format_bibliography(bib)
back = HtmlBackend()
back.write_to_file(formatbib,"out.html")
62 changes: 62 additions & 0 deletions Tutorial/05 BibTeX in Python/out.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
@article{micceri1989unicorn,
author = "Micceri, Theodore",
title = "The unicorn, the normal curve, and other improbable creatures.",
journal = "Psychological bulletin",
volume = "105",
number = "1",
pages = "156",
year = "1989",
publisher = "American Psychological Association"
}

@article{barthes199411,
author = "Barthes, Roland",
title = "11 The Death of the Author",
journal = "Media Texts, Authors and Readers: A Reader",
pages = "166",
year = "1994",
publisher = "Multilingual Matters"
}

@article{collins1998s,
author = "Collins, James J and Chow, Carson C",
title = "It's a small world",
journal = "Nature",
volume = "393",
number = "6684",
pages = "409--410",
year = "1998",
publisher = "Nature Publishing Group"
}

@article{curtiss2013unicorn,
author = "Curtiss, Michael and Becker, Iain and Bosman, Tudor and Doroshenko, Sergey and Grijincu, Lucian and Jackson, Tom and Kunnatur, Sandhya and Lassen, Soren and Pronin, Philip and Sankar, Sriram and others",
title = "Unicorn: A system for searching the social graph",
journal = "Proceedings of the VLDB Endowment",
volume = "6",
number = "11",
pages = "1150--1161",
year = "2013",
publisher = "VLDB Endowment",
doi = "10.14778/2536222.2536239"
}

@article{aad2015combined,
author = "Aad, Georges and Abbott, B and Abdallah, J and Abdinov, O and Aben, R and Abolins, M and AbouZeid, OS and Abramowicz, H and Abreu, H and Abreu, R and others",
title = "Combined Measurement of the Higgs Boson Mass in p p Collisions at s= 7 and 8 TeV with the ATLAS and CMS Experiments",
journal = "Physical review letters",
volume = "114",
number = "19",
pages = "191803",
year = "2015",
publisher = "APS"
}

@article{cheng2015generalized,
author = "Cheng, Xinyue and Zou, Yangyang",
title = "The generalized unicorn problem in Finsler geometry",
journal = "Differential Geometry-Dynamical Systems",
volume = "17",
pages = "38--48",
year = "2015"
}
34 changes: 34 additions & 0 deletions Tutorial/05 BibTeX in Python/out_1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head><meta name="generator" content="Pybtex">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bibliography</title>
</head>
<body>
<dl>
<dt>1</dt>
<dd>Theodore Micceri.
The unicorn, the normal curve, and other improbable creatures.
<em>Psychological bulletin</em>, 105(1):156, 1989.</dd>
<dt>2</dt>
<dd>Roland Barthes.
11 the death of the author.
<em>Media Texts, Authors and Readers: A Reader</em>, pages 166, 1994.</dd>
<dt>3</dt>
<dd>James&nbsp;J Collins and Carson&nbsp;C Chow.
It's a small world.
<em>Nature</em>, 393(6684):409–410, 1998.</dd>
<dt>4</dt>
<dd>Michael Curtiss, Iain Becker, Tudor Bosman, Sergey Doroshenko, Lucian Grijincu, Tom Jackson, Sandhya Kunnatur, Soren Lassen, Philip Pronin, Sriram Sankar, and others.
Unicorn: a system for searching the social graph.
<em>Proceedings of the VLDB Endowment</em>, 6(11):1150–1161, 2013.
<a href="https://doi.org/10.14778/2536222.2536239">doi:10.14778/2536222.2536239</a>.</dd>
<dt>5</dt>
<dd>Georges Aad, B&nbsp;Abbott, J&nbsp;Abdallah, O&nbsp;Abdinov, R&nbsp;Aben, M&nbsp;Abolins, OS&nbsp;AbouZeid, H&nbsp;Abramowicz, H&nbsp;Abreu, R&nbsp;Abreu, and others.
Combined measurement of the higgs boson mass in p p collisions at s= 7 and 8 tev with the atlas and cms experiments.
<em>Physical review letters</em>, 114(19):191803, 2015.</dd>
<dt>6</dt>
<dd>Xinyue Cheng and Yangyang Zou.
The generalized unicorn problem in finsler geometry.
<em>Differential Geometry-Dynamical Systems</em>, 17:38–48, 2015.</dd>
</dl></body></html>
28 changes: 28 additions & 0 deletions Tutorial/05 BibTeX in Python/out_2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head><meta name="generator" content="Pybtex">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bibliography</title>
</head>
<body>
<dl>
<dt>1</dt>
<dd><div><h4>Combined Measurement of the Higgs Boson Mass in p p Collisions at s= 7 and 8 TeV with the ATLAS and CMS Experiments</h4><i>Aad, Georges and Abbott, B and Abdallah, J and Abdinov, O and Aben, R and Abolins, M and AbouZeid, OS and Abramowicz, H and Abreu, H and Abreu, R and others</i>
Physical review letters&nbsp;114&nbsp;(19):191803</div></dd>
<dt>2</dt>
<dd><div><h4>The generalized unicorn problem in Finsler geometry</h4><i>Cheng, Xinyue and Zou, Yangyang</i>
Differential Geometry-Dynamical Systems&nbsp;17:38–48</div></dd>
<dt>3</dt>
<dd><div><h4>Unicorn: A system for searching the social graph</h4><i>Curtiss, Michael and Becker, Iain and Bosman, Tudor and Doroshenko, Sergey and Grijincu, Lucian and Jackson, Tom and Kunnatur, Sandhya and Lassen, Soren and Pronin, Philip and Sankar, Sriram and others</i>
Proceedings of the VLDB Endowment&nbsp;6&nbsp;(11):1150–1161
<a href="https://doi.org/10.14778/2536222.2536239">[ Publishers's page ]</a></div></dd>
<dt>4</dt>
<dd><div><h4>It's a small world</h4><i>Collins, James J and Chow, Carson C</i>
Nature&nbsp;393&nbsp;(6684):409–410</div></dd>
<dt>5</dt>
<dd><div><h4>11 The Death of the Author</h4><i>Barthes, Roland</i>
Media Texts, Authors and Readers: A Reader:166</div></dd>
<dt>6</dt>
<dd><div><h4>The unicorn, the normal curve, and other improbable creatures.</h4><i>Micceri, Theodore</i>
Psychological bulletin&nbsp;105&nbsp;(1):156</div></dd>
</dl></body></html>
Loading

0 comments on commit 0e6f0fd

Please sign in to comment.