Skip to content

Commit

Permalink
Py2 compatibility
Browse files Browse the repository at this point in the history
Many small changes to restore Py2 compatibility.
  • Loading branch information
markgw committed Oct 2, 2020
1 parent ab4b6f7 commit 74f74b2
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 37 deletions.
5 changes: 2 additions & 3 deletions src/python/pimlico/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
pass
reload(site)

from pimlico.core.dependencies.base import check_and_install
from pimlico.core.dependencies.core import CORE_PIMLICO_DEPENDENCIES, coloredlogs_dependency

PIMLICO_ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", ".."))

# Fetch current version number from PIMLICO_ROOT/admin/release.txt
Expand Down Expand Up @@ -62,6 +59,8 @@


def install_core_dependencies():
from pimlico.core.dependencies.base import check_and_install
from pimlico.core.dependencies.core import CORE_PIMLICO_DEPENDENCIES, coloredlogs_dependency
# Always check that core dependencies are satisfied before running anything
# Core dependencies are not allowed to depend on the local config, as we can't get to it at this point
# We just pass in an empty dictionary
Expand Down
26 changes: 19 additions & 7 deletions src/python/pimlico/datatypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from builtins import next
from builtins import zip
from builtins import object
from future.utils import with_metaclass
from future.utils import with_metaclass, PY3

import json
import os
Expand Down Expand Up @@ -101,13 +101,17 @@ def _get_reader_cls(cls):
if "__weakref__" in my_dict:
del my_dict["__weakref__"]
# Set the reader's __qualname__ so it's properly treated as a nested class of the datatype
my_dict["__qualname__"] = "{}.Reader".format(cls.__qualname__)
if PY3:
my_dict["__qualname__"] = "{}.Reader".format(cls.__qualname__)
my_dict["__module__"] = cls.__module__

# No new documentation is provided, then we don't want to inherit the
# superclass' docstring, but instead let the reader follow the link to see that
if my_dict["__doc__"] is None:
my_dict["__doc__"] = "Reader class for {}".format(cls.__qualname__)
if PY3:
my_dict["__doc__"] = "Reader class for {}".format(cls.__qualname__)
else:
my_dict["__doc__"] = "Reader class for {}".format(cls.__name__)

reader_cls = PimlicoDatatypeReaderMeta("Reader", (parent_reader,), my_dict)
setattr(cls, _cache_name, reader_cls)
Expand Down Expand Up @@ -166,13 +170,17 @@ def _get_some_writer_cls(cls):
if "__weakref__" in new_cls_dict:
del new_cls_dict["__weakref__"]
# Set the writer's __qualname__ so it's properly treated as a nested class of the datatype
new_cls_dict["__qualname__"] = "{}.Writer".format(cls.__qualname__)
if PY3:
new_cls_dict["__qualname__"] = "{}.Writer".format(cls.__qualname__)
new_cls_dict["__module__"] = cls.__module__

# No new documentation is provided, then we don't want to inherit the
# superclass' docstring, but instead let the reader follow the link to see that
if new_cls_dict["__doc__"] is None:
new_cls_dict["__doc__"] = "Writer class for {}".format(cls.__qualname__)
if PY3:
new_cls_dict["__doc__"] = "Writer class for {}".format(cls.__qualname__)
else:
new_cls_dict["__doc__"] = "Writer class for {}".format(cls.__name__)

# Perform subclassing so that a new Writer is created that is a subclass of the parent's writer
writer_cls = type("Writer", (parent_writer,), new_cls_dict)
Expand Down Expand Up @@ -233,14 +241,18 @@ def _get_setup_cls(cls):
if "__weakref__" in my_dict:
del my_dict["__weakref__"]
# Set the reader setup's __qualname__ so it's properly treated as a nested class of the datatype's reader
my_dict["__qualname__"] = "{}.Setup".format(cls.__qualname__)
if PY3:
my_dict["__qualname__"] = "{}.Setup".format(cls.__qualname__)
my_dict["__module__"] = cls.__module__

if my_setup is parent_setup or my_dict["__doc__"] is None:
# If setup was not overridden: don't use the base class' doc
# If no new documentation is provided, then we don't want to inherit the
# superclass' docstring, but instead let the reader follow the link to see that
my_dict["__doc__"] = "Setup class for {}".format(cls.__qualname__)
if PY3:
my_dict["__doc__"] = "Setup class for {}".format(cls.__qualname__)
else:
my_dict["__doc__"] = "Setup class for {}".format(cls.__name__)

setup_cls = type("Setup", (parent_setup,), my_dict)
setattr(cls, _cache_name, setup_cls)
Expand Down
5 changes: 3 additions & 2 deletions src/python/pimlico/datatypes/corpora/data_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from traceback import format_exc

from builtins import object
from future.utils import with_metaclass
from future.utils import with_metaclass, PY3

__all__ = ["DataPointType", "RawDocumentType", "TextDocumentType", "RawTextDocumentType", "DataPointError",
"InvalidDocument"]
Expand Down Expand Up @@ -57,7 +57,8 @@ def _get_document_cls(cls):
if "__weakref__" in new_dict:
del new_dict["__weakref__"]
# Set the reader setup's __qualname__ so it's properly treated as a nested class of the datatype's reader
new_dict["__qualname__"] = "{}.Document".format(cls.__qualname__)
if PY3:
new_dict["__qualname__"] = "{}.Document".format(cls.__qualname__)
new_dict["__module__"] = cls.__module__

# If no new documentation is provided, then we don't want to inherit the
Expand Down
5 changes: 2 additions & 3 deletions src/python/pimlico/datatypes/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
import io
import os

from sklearn.metrics.pairwise import cosine_distances

from backports import csv

from pimlico.core.dependencies.python import numpy_dependency, PythonPackageOnPip
from pimlico.datatypes import PimlicoDatatype, NamedFileCollection
from pimlico.datatypes import PimlicoDatatype
from pimlico.datatypes.files import NamedFileCollection
from pimlico.utils.core import cached_property

Expand Down Expand Up @@ -240,6 +238,7 @@ def run_browser(self, reader, opts):
"""
import numpy as np
from sklearn.metrics import euclidean_distances
from sklearn.metrics.pairwise import cosine_distances

print("Reading embeddings...")
num_vectors = len(reader)
Expand Down
2 changes: 1 addition & 1 deletion src/python/pimlico/datatypes/gensim.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def write_model(self, model):

class GensimLdaSeqModel(PimlicoDatatype):
"""
A trained LDA-seq model i.e. Dynamic Topic Model (DTM).
A trained LDA-seq model - i.e. Dynamic Topic Model (DTM).
As well as the Gensim model, it also stores the list of slice labels, so that
we can easily look up the appropriate time slice for a document paired with
Expand Down
8 changes: 4 additions & 4 deletions src/python/pimlico/test/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
several) of how to use them -- how pipeline config should look and what sort of input data to use.
"""
if __name__ == "__main__":
from pimlico import install_core_dependencies
install_core_dependencies()

from builtins import object
import traceback

from past.utils import PY2

from pimlico.utils.core import remove_duplicates

if __name__ == "__main__":
from pimlico import install_core_dependencies
install_core_dependencies()

import argparse
import os
import shutil
Expand Down
9 changes: 5 additions & 4 deletions src/python/pimlico/test/suite.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

if __name__ == "__main__":
from pimlico import install_core_dependencies
install_core_dependencies()

import argparse
import sys

from pimlico.test.pipeline import run_test_suite
from pimlico.utils.logging import get_console_logger

if __name__ == "__main__":
from pimlico import install_core_dependencies
install_core_dependencies()


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Pipeline test suite runner")
Expand Down
12 changes: 12 additions & 0 deletions src/python/pimlico/utils/_core_py3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

def raise_from(exc, cause):
"""
Like future's raise_from function. However, on Py3, just calls raise X from Y.
On Py2, defers to future's replacement.
This means that we get the full functionality of raise from on PY3, which
is our main priority. If you run on PY2, you get less debugging information
in some cases, but that's no reason to ruin the debugging information on PY3 too!
"""
raise exc from cause
27 changes: 14 additions & 13 deletions src/python/pimlico/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from builtins import object
from contextlib import contextmanager

from future.utils import raise_from as future_raise_from, PY3
from future.utils import PY3

import sys
import ast
Expand Down Expand Up @@ -224,17 +224,18 @@ def __get__(self, obj, cls):
return value


def raise_from(exc, cause):
"""
Like future's raise_from function. However, on Py3, just calls raise X from Y.
On Py2, defers to future's replacement.
"""
Like future's raise_from function. However, on Py3, just calls raise X from Y.
On Py2, defers to future's replacement.
This means that we get the full functionality of raise from on PY3, which
is our main priority. If you run on PY2, you get less debugging information
in some cases, but that's no reason to ruin the debugging information on PY3 too!
This means that we get the full functionality of raise from on PY3, which
is our main priority. If you run on PY2, you get less debugging information
in some cases, but that's no reason to ruin the debugging information on PY3 too!
"""
if PY3:
raise exc from cause
else:
future_raise_from(exc, cause)
"""
if PY3:
# noinspection PyUnresolvedReferences
from ._core_py3 import raise_from
else:
# noinspection PyUnresolvedReferences
from future.utils import raise_from

0 comments on commit 74f74b2

Please sign in to comment.