Skip to content
Utilities

Utilities

Small helpers: parsing bibcodes, normalising document arguments, flattening.

This module duck-types documents and libraries instead of importing the models, so any module (including the models themselves) can import it without a cycle.

scixplorer.utils.flatten

Written utils.flatten after from scixplorer import utils.

flatten(obj)

Flatten nested containers into one list, dropping None.

Lists, tuples, sets, dicts (their values) and iterators such as generators are expanded recursively. Everything else is kept as a single item, including strings and other iterables such as libraries or queries (so nothing is fetched from ADS). None is dropped at every level, so that Document.affiliation (one entry per author: an affiliation, a tuple of them, or None) can be scanned directly:

any(aff.country == "Australia" for aff in flatten(doc.affiliation))

scixplorer.utils.is_valid_orcid

Written utils.is_valid_orcid after from scixplorer import utils.

is_valid_orcid(orcid, *, strict=False)

Whether orcid is an ORCID iD with a correct check digit.

An ORCID iD is 16 characters, written in four hyphenated groups: 0000-0002-1825-0097. The last character is a check digit (0 to 9 or X) computed from the other fifteen (ISO 7064 MOD 11-2), so one mistyped digit, or two neighbouring digits swapped, makes the iD invalid. This checks the form and the check digit only: a valid iD need not be registered to anyone. The bare iD is expected, without https://orcid.org/ in front.

Parameters

  • strict: require the hyphens, each in its place. By default they may be missing (0000000218250097), which SciX accepts in a search too.

scixplorer.utils.parse_bibcode

Written utils.parse_bibcode after from scixplorer import utils.

parse_bibcode(bibcode)

Split a bibcode into its parts, with the padding dots removed.

Returns a dict with keys year, journal_abbreviation, volume, qualifier, page_number and first_letter_of_last_name. A digit in the qualifier position is the leading digit of a five-digit page, so it is moved into page_number and qualifier is "":

>>> parse_bibcode("1996PhRvB..5411169K")["page_number"]
'11169'

Raises ValueError if bibcode is not a 19-character bibcode.

scixplorer.utils.to_bibcodes

Written utils.to_bibcodes after from scixplorer import utils.

to_bibcodes(*items)

Collect the bibcodes of everything given, in order, without duplicates.

Each item may be a bibcode string (surrounding whitespace is removed), an object with a bibcode attribute (a document), an object with a bibcodes attribute holding strings (a library: its list of bibcodes is used, without fetching each document), or any other iterable of these (lists, sets, generators, queries), nested to any depth. Bibcodes are not checked against a pattern, because ADS also accepts other identifiers; the server decides what is valid.

Raises ValueError for an empty string or a document whose bibcode is None, and TypeError for anything else.