From 64151c8e5074d956134d0b118ce1384af511413b Mon Sep 17 00:00:00 2001 From: Jonathan de Bruin Date: Mon, 23 Oct 2023 17:24:12 +0200 Subject: [PATCH] Reorder stuff --- pyalex/api.py | 123 +++++++++++++++++++++++++------------------------- 1 file changed, 62 insertions(+), 61 deletions(-) diff --git a/pyalex/api.py b/pyalex/api.py index d319fc1..053e110 100644 --- a/pyalex/api.py +++ b/pyalex/api.py @@ -106,67 +106,6 @@ class OpenAlexEntity(dict): pass -class Work(OpenAlexEntity): - """OpenAlex work object.""" - - def __getitem__(self, key): - if key == "abstract": - return invert_abstract(self["abstract_inverted_index"]) - - return super().__getitem__(key) - - def ngrams(self, return_meta=False): - openalex_id = self["id"].split("/")[-1] - - res = _get_requests_session().get( - f"{config.openalex_url}/works/{openalex_id}/ngrams", - headers={"User-Agent": "pyalex/" + __version__, "email": config.email}, - ) - res.raise_for_status() - results = res.json() - - # return result and metadata - if return_meta: - return results["ngrams"], results["meta"] - else: - return results["ngrams"] - - -class Author(OpenAlexEntity): - pass - - -class Source(OpenAlexEntity): - pass - - -class Institution(OpenAlexEntity): - pass - - -class Concept(OpenAlexEntity): - pass - - -class Publisher(OpenAlexEntity): - pass - - -class Funder(OpenAlexEntity): - pass - - -def Venue(*args, **kwargs): # deprecated - # warn about deprecation - warnings.warn( - "Venue is deprecated. Use Sources instead.", - DeprecationWarning, - stacklevel=2, - ) - - return Source(*args, **kwargs) - - class Paginator: VALUE_CURSOR_START = "*" VALUE_NUMBER_START = 1 @@ -383,34 +322,96 @@ def select(self, s): return self +# The API + + +class Work(OpenAlexEntity): + def __getitem__(self, key): + if key == "abstract": + return invert_abstract(self["abstract_inverted_index"]) + + return super().__getitem__(key) + + def ngrams(self, return_meta=False): + openalex_id = self["id"].split("/")[-1] + + res = _get_requests_session().get( + f"{config.openalex_url}/works/{openalex_id}/ngrams", + headers={"User-Agent": "pyalex/" + __version__, "email": config.email}, + ) + res.raise_for_status() + results = res.json() + + # return result and metadata + if return_meta: + return results["ngrams"], results["meta"] + else: + return results["ngrams"] + + class Works(BaseOpenAlex): resource_class = Work +class Author(OpenAlexEntity): + pass + + class Authors(BaseOpenAlex): resource_class = Author +class Source(OpenAlexEntity): + pass + + class Sources(BaseOpenAlex): resource_class = Source +class Institution(OpenAlexEntity): + pass + + class Institutions(BaseOpenAlex): resource_class = Institution +class Concept(OpenAlexEntity): + pass + + class Concepts(BaseOpenAlex): resource_class = Concept +class Publisher(OpenAlexEntity): + pass + + class Publishers(BaseOpenAlex): resource_class = Publisher +class Funder(OpenAlexEntity): + pass + + class Funders(BaseOpenAlex): resource_class = Funder +def Venue(*args, **kwargs): # deprecated + # warn about deprecation + warnings.warn( + "Venue is deprecated. Use Sources instead.", + DeprecationWarning, + stacklevel=2, + ) + + return Source(*args, **kwargs) + + def Venues(*args, **kwargs): # deprecated # warn about deprecation warnings.warn(