[pybliographer] format: Encapsulate module variables
- From: Germán Poo-Caamaño <gpoo src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pybliographer] format: Encapsulate module variables
- Date: Thu, 29 Mar 2018 03:39:55 +0000 (UTC)
commit 9fdbb11d8c874b6dc27c1848a95d1d12711d95d2
Author: Germán Poo-Caamaño <gpoo gnome org>
Date: Wed Mar 28 20:31:56 2018 -0300
format: Encapsulate module variables
* Initialize the global variables inside methods/functions,
to avoid loading twice the same data when the module is
loaded multiple times.
* Set paths according to location of files to make the test
working from any directory, e.g. when running nosetests.
Pyblio/Format/Medline.py | 16 ++++++---
Pyblio/Format/test_Isi.py | 44 +++++++-------------------
Pyblio/Format/test_Ovidlike.py | 68 ++++++++++++++-------------------------
Pyblio/Format/test_medline.py | 55 +++++++++++++++++++++-----------
4 files changed, 83 insertions(+), 100 deletions(-)
---
diff --git a/Pyblio/Format/Medline.py b/Pyblio/Format/Medline.py
index f88e5dd..7b6a2cf 100644
--- a/Pyblio/Format/Medline.py
+++ b/Pyblio/Format/Medline.py
@@ -29,17 +29,18 @@ from Pyblio import Base, Fields, Types, Autoload, Open, Iterator, Utils, Config
header = re.compile ('^(\w\w[\w ][\w ])- (.*)$')
contin = re.compile ('^ (.*)$')
-one_to_one = Config.get ('medline/mapping').data
+one_to_one = None
medurl = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi?cmd=prlinks&dbfrom=pubmed&retmode=ref&id='
-class MedlineIterator (Iterator.Iterator):
+class MedlineIterator(Iterator.Iterator):
+ def __init__(self, file):
+ global one_to_one
+ if one_to_one is None:
+ one_to_one = Config.get ('medline/mapping').data
- def __init__ (self, file):
self.file = file
- return
-
def first (self):
# rewind the file
@@ -215,7 +216,10 @@ class Medline (Base.DataBase):
return
-def writer (iter, output, **argh):
+def writer(iter, output, **argh):
+ global one_to_one
+ if one_to_one is None:
+ one_to_one = Config.get ('medline/mapping').data
entry = iter.first ()
while entry:
diff --git a/Pyblio/Format/test_Isi.py b/Pyblio/Format/test_Isi.py
index b8bde4a..4998ec5 100644
--- a/Pyblio/Format/test_Isi.py
+++ b/Pyblio/Format/test_Isi.py
@@ -28,8 +28,8 @@ import os
import sys
import unittest
-sys.path.append (os.path.abspath('../..'))
-print os.path.abspath('../..')
+basedir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
+sys.path.append(basedir)
from Pyblio.Format import isifile
from Pyblio import Base, Config, Fields
@@ -66,11 +66,10 @@ example_3 = """ED X, ABC
ER
"""
-class ReaderCase (unittest.TestCase):
- def setUp (self):
-
- Config.parse_directory (os.path.abspath('../ConfDir'))
- Config.load_user ()
+class ReaderCase(unittest.TestCase):
+ def setUp(self):
+ Config.parse_directory(os.path.join(basedir, 'ConfDir'))
+ Config.load_user()
print 'CONFIGURATION'
print 50*'-'
for i in Config.domains ():
@@ -79,7 +78,6 @@ class ReaderCase (unittest.TestCase):
self.db = Base.DataBase ('//localhost/Internal')
self.output = cStringIO.StringIO()
-
def test01(self):
"""Test that all fields are Instances, as
opposed to strings"""
@@ -105,7 +103,7 @@ class ReaderCase (unittest.TestCase):
comparison = {'X': 'A. B. C.',
'Y': 'D.',
'Z': 'E. F. G. H.'}
-
+
inpt = cStringIO.StringIO (example_2)
rdr = isifile.IsifileIterator (inpt)
e = rdr.first ()
@@ -119,11 +117,11 @@ class ReaderCase (unittest.TestCase):
def test03 (self):
"""Test that Editors are accepted."""
-
+
comparison = {'X': 'A. B. C.',
'Y': 'D.',
'Z': 'E. F. G. H.'}
-
+
inpt = cStringIO.StringIO (example_3)
rdr = isifile.IsifileIterator (inpt)
e = rdr.first ()
@@ -137,11 +135,11 @@ class ReaderCase (unittest.TestCase):
def test04 (self):
"""Test that Editors are accepted."""
-
+
comparison = {'X': 'A. B. C.',
'Y': 'D.',
'Z': 'E. F. G. H.'}
-
+
inpt = cStringIO.StringIO (example_3)
rdr = isifile.IsifileIterator (inpt)
e = rdr.first ()
@@ -164,7 +162,7 @@ def suite():
def main ():
unittest.main (defaultTest='suite' )
-
+
if __name__ == '__main__':
main()
@@ -174,21 +172,3 @@ if __name__ == '__main__':
### Mode: python
### encoding: utf-8
### End:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/Pyblio/Format/test_Ovidlike.py b/Pyblio/Format/test_Ovidlike.py
index 56ad8f3..113e1b9 100644
--- a/Pyblio/Format/test_Ovidlike.py
+++ b/Pyblio/Format/test_Ovidlike.py
@@ -26,7 +26,8 @@ import re
import sys
import unittest
-sys.path.append (os.path.abspath('../..'))
+basedir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
+sys.path.append(basedir)
from Pyblio.Base import DataBase, Entry
from Pyblio import Config
@@ -149,34 +150,27 @@ Abstract
All rights reserved. [References: 31] """]
-class WriterCase (unittest.TestCase):
-
- def setUp (self):
-
-
- Config.parse_directory (os.path.abspath('../ConfDir'))
- Config.load_user ()
- self.db = DataBase ('//localhost/Internal')
+class WriterCase(unittest.TestCase):
+ def setUp(self):
+ Config.parse_directory(os.path.join(basedir, 'ConfDir'))
+ Config.load_user()
+ self.db = DataBase('//localhost/Internal')
self.output = cStringIO.StringIO()
self.mapping = Config.get('ovid/mapping').data
-
def test01(self):
-
self.entry = Entry ( Key('TEST', 'KEY1'), get_entry('article'),
{'journal': 'CACM',
- 'number': 22,
+ 'number': 22,
'volume': 123,
'pages': '234-543'})
self.db.add(self.entry)
self.itera = self.db.iterator()
-
+
writer (self.itera, self.output, self.mapping)
print self.output.getvalue()
-
def test02source (self):
-
data = [ ## from cites.ovid
{'result':
'International Journal of Hematology. 69(2):81-88, 1999 Feb.',
@@ -184,7 +178,7 @@ class WriterCase (unittest.TestCase):
'volume': 69, 'number': 2, 'pages': '81-88',
'date': Date ((1999, 2, None))},
{'result':
- 'Journal of Trauma-Injury Infection & Critical Care. 44(6):1047-1054; discussion 1054-5, 1998
Jun.',
+ 'Journal of Trauma-Injury Infection & Critical Care. 44(6):1047-1054; discussion 1054-5, 1998
Jun.',
'journal': 'Journal of Trauma-Injury Infection & Critical Care',
'volume': 44, 'number': 6, 'pages': '1047-1054',
'date': Date ((1998, 6, None)),
@@ -195,7 +189,7 @@ class WriterCase (unittest.TestCase):
'journal': 'Chemotherapy',
'volume': 42, 'number': 3, 'pages': '215-219',
'date': Date ((1996, 5, None))},
- {'result': 'Circulatory Shock. 18(3):193-203, 1986.',
+ {'result': 'Circulatory Shock. 18(3):193-203, 1986.',
'journal': 'Circulatory Shock',
'volume': 18, 'number': 3, 'pages': '193-203',
'date': Date ('1986')},
@@ -205,10 +199,7 @@ class WriterCase (unittest.TestCase):
'volume': 162, 'number': 17, 'pages': '1961-1965',
'date': Date ((2002, 9, 23))},]
-
-
for i in data :
-
e = Entry ( Key('TEST', 'KEY1'), get_entry('article'),
i)
self.output.seek(0)
@@ -216,13 +207,9 @@ class WriterCase (unittest.TestCase):
write_source_field (self.output, e, self.mapping)
r = self.output.getvalue()
self.assertEqual (e['result'], r[9:-1])
-
-
-
class RexpCase (unittest.TestCase):
-
rx = re.compile (# Fall 1:
r"""(?P<journal>.*?)\.\ +
(?P<volume>\w+)?
@@ -286,9 +273,6 @@ class RexpCase (unittest.TestCase):
'Biochemistry', '38', '49', None, '16333-16339',
'1999', 'Dec 7', None),
]
-
-
-
def test01 (self):
for test in self.data:
@@ -299,18 +283,18 @@ class RexpCase (unittest.TestCase):
print m.group(
'journal', 'volume', 'number', 'inseries',
'pages', 'year', 'month', 'other')
- self.assertEqual (journal, m.group('journal'))
- self.assertEqual (volume, m.group('volume'))
- self.assertEqual (number, m.group('number'))
- self.assertEqual (inseries, m.group('inseries'))
- self.assertEqual (pages, m.group('pages'))
- self.assertEqual (year, m.group('year'))
- self.assertEqual (month, m.group('month'))
- self.assertEqual (other, m.group('other'))
+ self.assertEqual (journal, m.group('journal'))
+ self.assertEqual (volume, m.group('volume'))
+ self.assertEqual (number, m.group('number'))
+ self.assertEqual (inseries, m.group('inseries'))
+ self.assertEqual (pages, m.group('pages'))
+ self.assertEqual (year, m.group('year'))
+ self.assertEqual (month, m.group('month'))
+ self.assertEqual (other, m.group('other'))
else: print 'Fehler'
-class Rexp2Case (unittest.TestCase):
+class Rexp2Case (unittest.TestCase):
def test01 (self):
"""Regexp wie oben"""
rx = re.compile (# Fall 1:
@@ -325,7 +309,7 @@ class Rexp2Case (unittest.TestCase):
\.\Z
"""
, flags= re.VERBOSE)
-
+
data = ['Biophysical Journal. 71(6):3320-3329, 1996 Dec.',
'Biochemistry. 38(49):16333-16339, 1999 Dec 7.',
'VERY HIGH FREQUENCY (VHF) ESR/EPR. 22 PG. 431-464. 2004 [Figures].'
@@ -340,7 +324,6 @@ class Rexp2Case (unittest.TestCase):
else:
print '**** FEHLER', d
-
def test02 (self):
rx = re.compile (# Fall 1:
r"""(?P<journal>.*?)\.\ +
@@ -353,7 +336,7 @@ class Rexp2Case (unittest.TestCase):
(?P<month>.*)
\.\s*\Z"""
, flags= re.VERBOSE)
-
+
data = ['Biophysical Journal. 71(6):3320-3329, 1996 Dec.',
'Biochemistry. 38(49):16333-16339, 1999 Dec 7.',
'VERY HIGH FREQUENCY (VHF) ESR/EPR. 22 PG. 431-464. 2004 [Figures].'
@@ -368,8 +351,8 @@ class Rexp2Case (unittest.TestCase):
else:
print '**** FEHLER', d
-class AuthorCase (unittest.TestCase):
+class AuthorCase (unittest.TestCase):
def test01 (self):
aut = ["Marsh D.",
"de Planque MRR.", "Kruijtzer JAW."]
@@ -379,7 +362,6 @@ class AuthorCase (unittest.TestCase):
x = R.parse_author (i)
print `x`
-
def test02 (self):
aut = """Pali T. Whyteside G. Dixon N. Kee TP. Ball S. Harrison MA. Findlay JBC.
Finbow ME. Marsh D."""
@@ -399,7 +381,7 @@ def suite():
def main ():
unittest.main (defaultTest='suite' )
-
+
if __name__ == '__main__':
main()
@@ -408,4 +390,4 @@ if __name__ == '__main__':
### Local Variables:
### Mode: python
### encoding: utf-8
-### End:
\ No newline at end of file
+### End:
diff --git a/Pyblio/Format/test_medline.py b/Pyblio/Format/test_medline.py
index 7cc0388..d7255ea 100644
--- a/Pyblio/Format/test_medline.py
+++ b/Pyblio/Format/test_medline.py
@@ -1,26 +1,41 @@
-# -*- coding: iso8859-1 -*-
-
-
-import cStringIO, os, sys, unittest
+# -*- coding: iso-8859-1 -*-
+# This file is part of pybliographer
+#
+# Copyright (C) Germán Poo-Caamaño <gpoo gnome org>
+# Copyright (C) 1998-2004 Frederic GOBRY <gobry pybliographer org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+
+import cStringIO
+import os
+import sys
+import unittest
import locale
locale.setlocale (locale.LC_ALL, '')
import gettext
-gettext.install ('pybliographer', '/usr/local/share/locale', unicode = True)
+gettext.install('pybliographer', '/usr/local/share/locale', unicode=True)
-sys.path.append (os.path.abspath('../..'))
+basedir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
+sys.path.append (basedir)
from Pyblio import Base, Config, Fields
-
-Config.parse_directory (os.path.abspath('../ConfDir'))
-Config.load_user ()
-
from Pyblio.Format import Medline
-
-
-
example_1 = """PMID- 15985842
OWN - NLM
STAT- MEDLINE
@@ -157,13 +172,15 @@ comparison = {'Holmes': 'W. C.',
class ReaderCase (unittest.TestCase):
-
- def setUp (self):
-
- self.db = Base.DataBase ('//localhost/Internal')
+ def setUp(self):
+ Config.parse_directory(os.path.join(basedir, 'ConfDir'))
+ Config.load_user()
+ self.db = Base.DataBase('//localhost/Internal')
self.output = cStringIO.StringIO()
-
+ def teardown(self):
+ Config.forget_changes()
+
def test01(self):
"""Test that all fields are Instances, as
opposed to strings"""
@@ -232,5 +249,5 @@ if __name__ == '__main__':
### Local Variables:
### Mode: python
-### encoding: iso-8859-1
+### encoding: iso-8859-1
### End:
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]