Skip to content

Commit 2b5e1f2

Browse files
Merge pull request #28 from RudolfCardinal/improve_package_handling
Tidy up, and improve docs for, external libraries
2 parents 5dd68a1 + 2d45bd4 commit 2b5e1f2

File tree

9 files changed

+58
-141
lines changed

9 files changed

+58
-141
lines changed

cardinal_pythonlib/datetimefunc.py

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,19 @@
4646
from string import Formatter
4747
from typing import Any, Optional, Union
4848

49-
try:
50-
# noinspection PyPackageRequirements
51-
from arrow import Arrow
52-
except ImportError:
53-
Arrow = None
54-
55-
try:
56-
import dateutil.parser
57-
except ImportError:
58-
dateutil = None
59-
49+
from arrow import Arrow
50+
import dateutil.parser
6051
from isodate.isoduration import parse_duration, Duration as IsodateDuration
6152
import pendulum
6253
from pendulum import Date, DateTime, Duration, Time
6354
from pendulum.tz import local_timezone
6455
from pendulum.tz.timezone import Timezone
6556

66-
if Arrow is not None:
67-
PotentialDatetimeType = Union[
68-
None, datetime.datetime, datetime.date, DateTime, str, Arrow
69-
]
70-
DateTimeLikeType = Union[datetime.datetime, DateTime, Arrow]
71-
DateLikeType = Union[datetime.date, DateTime, Arrow]
72-
else:
73-
PotentialDatetimeType = Union[
74-
None, datetime.datetime, datetime.date, DateTime, str
75-
]
76-
DateTimeLikeType = Union[datetime.datetime, DateTime]
77-
DateLikeType = Union[datetime.date, DateTime]
57+
PotentialDatetimeType = Union[
58+
None, datetime.datetime, datetime.date, DateTime, str, Arrow
59+
]
60+
DateTimeLikeType = Union[datetime.datetime, DateTime, Arrow]
61+
DateLikeType = Union[datetime.date, DateTime, Arrow]
7862

7963
log = logging.getLogger(__name__)
8064

cardinal_pythonlib/extract_text.py

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -96,46 +96,21 @@
9696
Optional,
9797
)
9898
from xml.etree import ElementTree as ElementTree
99-
100-
# ... cElementTree used to be the fast implementation; now ElementTree is fast
101-
# and cElementTree is deprecated; see
102-
# https://docs.python.org/3.4/library/xml.etree.elementtree.html
10399
import zipfile
104100

105101
import bs4
102+
import chardet
103+
from chardet.universaldetector import UniversalDetector
104+
import pdfminer # pip install pdfminer.six
105+
import pdfminer.pdfinterp
106+
import pdfminer.converter
107+
import pdfminer.layout
108+
import pdfminer.pdfpage
106109
import prettytable
107110
from semantic_version import Version
108111

109112
from cardinal_pythonlib.logs import get_brace_style_log_with_null_handler
110113

111-
try:
112-
import chardet
113-
from chardet.universaldetector import UniversalDetector
114-
except ImportError:
115-
chardet = None
116-
UniversalDetector = None
117-
118-
try:
119-
# noinspection PyPackageRequirements
120-
import pdfminer # pip install pdfminer
121-
122-
assert (
123-
int(pdfminer.__version__) > 20191010
124-
), "pdfminer installed but too old" # version string is e.g. '20191125'
125-
126-
# noinspection PyPackageRequirements
127-
import pdfminer.pdfinterp
128-
129-
# noinspection PyPackageRequirements
130-
import pdfminer.converter
131-
132-
# noinspection PyPackageRequirements
133-
import pdfminer.layout
134-
135-
# noinspection PyPackageRequirements
136-
import pdfminer.pdfpage
137-
except ImportError:
138-
pdfminer = None
139114

140115
log = get_brace_style_log_with_null_handler(__name__)
141116

cardinal_pythonlib/getch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
import sys
3535

3636
try:
37-
import msvcrt # Windows only
37+
import msvcrt # Windows only, but part of core Python under Windows
3838

3939
termios = None
4040
tty = None
4141
except ImportError:
4242
msvcrt = None
43-
import termios # Unix only
43+
import termios # Unix only; tty/termios are part of core Python under Unix
4444
import tty # requires termios, so Unix only # noqa: F401
4545

4646

cardinal_pythonlib/hash.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@
6464
except ImportError:
6565
mmh3 = None
6666

67-
# try:
68-
# import xxhash
69-
# pyhashxx = None
70-
# except ImportError:
71-
# xxhash = None
72-
# import pyhashxx
73-
74-
7567
# https://docs.python.org/3/library/platform.html#platform.architecture
7668
IS_64_BIT = sys.maxsize > 2**32
7769
TIMING_HASH = "hash"

cardinal_pythonlib/module_version.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,13 @@
3535
# Imports
3636
# =============================================================================
3737

38-
import sys
38+
from importlib.metadata import version # structure for Python 3.8 or higher
3939

4040
from semantic_version import Version
4141

4242
# noinspection PyUnresolvedReferences
4343
import cardinal_pythonlib.ensure_test_executed_correctly # noqa: F401
4444

45-
if sys.version_info > (3, 8):
46-
# Python 3.8 or higher
47-
# noinspection PyCompatibility
48-
from importlib.metadata import version
49-
else:
50-
try:
51-
from importlib_metadata import version
52-
except ImportError:
53-
raise
54-
5545

5646
# =============================================================================
5747
# Report Python module versions

cardinal_pythonlib/spreadsheets.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,17 @@
3838
import logging
3939
from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple, Union
4040

41+
import xlrd
42+
from xlrd import Book
43+
from xlrd.sheet import Cell, Sheet
44+
4145
from cardinal_pythonlib.datetimefunc import (
4246
coerce_to_pendulum,
4347
pendulum_to_datetime_stripping_tz,
4448
)
4549
from cardinal_pythonlib.progress import ActivityCounter
4650
from cardinal_pythonlib.reprfunc import simple_repr
4751

48-
try:
49-
# noinspection PyPackageRequirements
50-
import xlrd
51-
52-
# noinspection PyPackageRequirements
53-
from xlrd import Book
54-
55-
# noinspection PyPackageRequirements
56-
from xlrd.sheet import Cell, Sheet
57-
except ImportError:
58-
raise ImportError("You must install the 'xlrd' package.")
59-
6052
log = logging.getLogger(__name__)
6153

6254

cardinal_pythonlib/sqlalchemy/arrow_types.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import datetime
3131
from typing import Any, Iterable, Optional
3232

33-
# noinspection PyUnresolvedReferences
3433
import arrow
3534
import sqlalchemy.dialects.mssql
3635
import sqlalchemy.dialects.mysql

docs/source/external_dependencies.rst

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,21 @@
1818
External libraries
1919
------------------
2020

21-
22-
This package also installs (and uses or extends):
21+
This package also installs (and uses or extends) the following packages, which
22+
are generally "pure Python", meaning that they can easily be installed e.g. on
23+
a Windows computer with no C compiler system installed.
2324

2425
- ``alembic``: http://alembic.zzzcomputing.com/
2526
- ``appdirs``: https://pypi.org/project/appdirs/
27+
- ``arrow``: https://arrow.readthedocs.io/
2628
- ``beautifulsoup4``: https://www.crummy.com/software/BeautifulSoup/bs4/doc/
29+
- ``chardet``: https://chardet.readthedocs.io/en/latest/
2730
- ``colorlog``: https://pypi.org/project/colorlog/
2831
- ``isodate``: https://pypi.org/project/isodate/
2932
- ``numpy``: http://www.numpy.org/
3033
- ``openpyxl``: https://openpyxl.readthedocs.io/
3134
- ``pandas``: https://pandas.pydata.org/
35+
- ``pdfminer.six``: https://pdfminersix.readthedocs.io/en/latest/
3236
- ``pendulum``: https://pendulum.eustace.io/
3337
- ``prettytable``: https://pypi.org/project/PrettyTable/
3438
- ``psutil``: https://pypi.org/project/psutil/
@@ -40,26 +44,40 @@ This package also installs (and uses or extends):
4044
- ``semantic_version``: https://pypi.org/project/semantic_version/
4145
- ``SQLAlchemy``: https://www.sqlalchemy.org/
4246
- ``sqlparse``: https://sqlparse.readthedocs.io/
47+
- ``xlrd``: https://pypi.org/project/xlrd/
4348

44-
The following will be used, if present (and an exception raised if you use
45-
library code that requires one of these packages without it being installed):
49+
The following packages will be used, if present, and an exception raised if you
50+
use library code that requires one of these packages without it being
51+
installed. They include large packages (e.g. Django), some other "less core"
52+
aspects, and packages that require a C compiler and so may be harder to install
53+
in some contexts.
4654

47-
- ``arrow``: https://arrow.readthedocs.io/
48-
- ``bcrypt``: https://pypi.org/project/bcrypt/
55+
- ``bcrypt``: https://pypi.org/project/bcrypt/ (C-based)
4956
- ``colander``: https://docs.pylonsproject.org/projects/colander/
57+
- ``cryptography``: https://cryptography.io/
5058
- ``deform``: https://docs.pylonsproject.org/projects/deform/
51-
- ``Django``: https://www.djangoproject.com/
59+
- ``Django`` >= 4.2: https://www.djangoproject.com/
5260
- ``dogpile.cache``: https://dogpilecache.readthedocs.io/
61+
- ``libChEBIpy``: https://pypi.org/project/libChEBIpy/ (Python 2 only?)
5362
- ``pyramid``: https://trypyramid.com/
54-
- ``webob``: https://webob.org/ (used by Pyramid)
63+
- ``webob``: https://webob.org/ (used and installed by Pyramid)
5564

56-
The following will be used, but the library code won't complain if not:
65+
The following packages will be used sometimes, but the library code won't
66+
complain much if they are absent. They include some other C-based packages, one
67+
that is specific to Windows and won't install on other platforms, and a
68+
selection of PDF-handling libraries where there is not a clear best choice.
5769

58-
- ``mmh3``: https://pypi.org/project/mmh3/
70+
- ``brotlipy``: https://pypi.org/project/brotlipy/ (C-based)
71+
- ``mmh3``: https://pypi.org/project/mmh3/ (C-based)
72+
- ``matplotlib``: https://matplotlib.org/
5973
- ``pdfkit``: https://pypi.org/project/pdfkit/
60-
- ``pdfminer``: https://pypi.org/project/pdfminer/
61-
- ``pypiwin32``: https://pypi.org/project/pypiwin32/
62-
- ``pyth``: https://pyth.readthedocs.io/
63-
- ``python-docx`` (``import docx``): https://python-docx.readthedocs.io/
74+
- ``pypiwin32``: https://pypi.org/project/pypiwin32/ (Windows only)
6475
- ``weasyprint``: https://weasyprint.org/
6576
- ``xhtml2pdf``: https://xhtml2pdf.readthedocs.io/
77+
78+
To *build* the library distribution (most people won't need this!), a few other
79+
development libraries are required:
80+
81+
- ``sphinx``: https://www.sphinx-doc.org/
82+
- ``sphinx_rtd_theme``: https://github.com/readthedocs/sphinx_rtd_theme
83+
- ``twine``: https://pypi.org/project/twine/

setup.py

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,17 @@
4949
# - SEE ALSO external_dependencies.rst
5050
"alembic",
5151
"appdirs>=1.4.0",
52+
"arrow>=0.15",
5253
"beautifulsoup4", # "import bs4" or "from bs4 import ..."
54+
"chardet>=5.0.0",
5355
"colorlog",
5456
"isodate>=0.5.4",
5557
"numba", # just-in-time compilation
5658
"numpy>=1.20.0,<2.0", # 1.20.0 required for numpy.typing
5759
"openpyxl",
5860
"pandas",
5961
"pendulum>=2.1.1",
62+
"pdfminer.six>=20191010", # "import pdfminer"
6063
"prettytable",
6164
"psutil",
6265
"pygments",
@@ -68,6 +71,7 @@
6871
"semantic-version",
6972
"SQLAlchemy>=1.4,<3.0",
7073
"sqlparse",
74+
"xlrd>=2.0.0",
7175
]
7276

7377
NOTES_RE_OTHER_REQUIREMENTS = """
@@ -79,45 +83,8 @@
7983
8084
8185
# -----------------------------------------------------------------------------
82-
# The following are NOT HANDLED GRACEFULLY; their absence will cause a runtime
83-
# ImportError, but we don't make them requirements as they need a compiler to
84-
# install (and one might want to use the rest of the library without them).
86+
# FOR OTHER REQUIREMENTS, AND OPTIONAL EXTRAS: SEE external_dependencies.rst
8587
# -----------------------------------------------------------------------------
86-
# - SEE ALSO external_dependencies.rst
87-
88-
# arrow
89-
# bcrypt
90-
# colander
91-
# deform
92-
# Django>=4.2
93-
# dogpile.cache
94-
# pyramid
95-
# webob # installed by pyramid
96-
97-
98-
# -----------------------------------------------------------------------------
99-
# The following are OPTIONAL; their absence will be handled gracefully, so
100-
# they are not requirements, but we note them here:
101-
# -----------------------------------------------------------------------------
102-
# - SEE ALSO external_dependencies.rst
103-
104-
# mmh3
105-
# pdfkit
106-
# pdfminer
107-
# pypiwin32
108-
# pyth
109-
# python-docx # "import docx"
110-
# weasyprint
111-
# xhtml2pdf
112-
113-
114-
# -----------------------------------------------------------------------------
115-
# FOR LIBRARY DEVELOPMENT
116-
# -----------------------------------------------------------------------------
117-
118-
# sphinx
119-
# sphinx_rtd_theme
120-
# twine
12188
12289
"""
12390

0 commit comments

Comments
 (0)