Skip to content

Commit 2f940cc

Browse files
__str__() for all remaining ScienceDirect classes
1 parent ff80f4b commit 2f940cc

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

pybliometrics/sciencedirect/nonserial_title.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,18 @@ def __init__(self,
115115
# Parse json
116116
self._json = self._json['nonserial-metadata-response']
117117
self._entry = self._json['entry'][0]
118+
119+
def __str__(self) -> str:
120+
"""Return a citation string for the Nonserial Title."""
121+
if self.authors:
122+
authors = f"{self.authors}."
123+
elif self.editors:
124+
authors = f"{self.editors} (ed.)."
125+
else:
126+
authors = ""
127+
edition = f", {self.edition}." if self.edition else "."
128+
publisher = f" {self.publisher_name}." if self.publisher_name else "."
129+
title = f" {self.title}"
130+
isbn = self.isbn
131+
132+
return f"{authors}{title}{edition}{publisher} ISBN: {isbn}"

pybliometrics/sciencedirect/object_retrieval.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def __init__(self,
4343
identifier = self._get_eid(identifier)
4444
file_identifier = f'{identifier}-{filename}'
4545

46+
self._identifier = identifier
47+
self._filename = filename
4648
self._view = ''
4749
self._refresh = refresh
4850

@@ -52,3 +54,9 @@ def _get_eid(self, identifier: str) -> str:
5254
"""Get the EID of a document."""
5355
am = ArticleRetrieval(identifier, field='eid')
5456
return am.eid
57+
58+
def __str__(self) -> str:
59+
"""Return a string with the filename, document and object size in KB."""
60+
size_kb = f"{len(self._object) / 1024:.1f}" if self._object else "0.0"
61+
return (f"Object {self._filename} from document with EID {self._identifier}"
62+
f" has size of {size_kb} KB.")

pybliometrics/sciencedirect/tests/test_NonserialTitle.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ def test_self_link():
7979
assert nst_2.self_link == "https://api.elsevier.com/content/nonserial/title/isbn/9780128203101"
8080
assert nst_3.self_link == "https://api.elsevier.com/content/nonserial/title/isbn/9780128217771"
8181

82+
def test_str():
83+
expected_1 = "Ian Newton. The Migration Ecology of Birds, Second Edition. Academic Press. ISBN: 9780128237519"
84+
expected_2 = "Elias Barriga and Ivana Pajic-Lijakovic (ed.). Viscoelasticity and Collective Cell Migration. Academic Press. ISBN: 9780128203101"
85+
expected_3 = "Fatos Xhafa, Mohamed A. Tawhid, Pardeep Kumar and Yugal Kumar (ed.). Machine Learning, Big Data, and IoT for Medical Informatics. Academic Press. ISBN: 9780128217771"
86+
assert str(nst_1) == expected_1
87+
assert str(nst_2) == expected_2
88+
assert str(nst_3) == expected_3
89+
8290

8391
def test_title():
8492
assert nst_1.title == "The Migration Ecology of Birds"

pybliometrics/sciencedirect/tests/test_ObjectRetrieval.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ def test_object():
3030
assert isinstance(or_2.object, BytesIO)
3131
assert or_2.object.getvalue()[150:200] == obj_2_150_200
3232
assert ET.parse(or_2.object).getroot().tag == '{http://www.w3.org/2000/svg}svg'
33+
34+
def test_str():
35+
"""Tests the string representation of the ObjectRetrieval object."""
36+
expected_1 = "Object gr10.jpg from document with EID 1-s2.0-S156984322300331X has size of 34.7 KB."
37+
assert str(or_1) == expected_1
38+
expected_2 = "Object si92.svg from document with EID 1-s2.0-S0736584520302969 has size of 10.0 KB."
39+
assert str(or_2) == expected_2

0 commit comments

Comments
 (0)