Skip to content

Commit 8faa86f

Browse files
authored
Merge pull request #23 from smarie/fix_issue_21
New method `in_stdlib` (Fixes issue #21)
2 parents f343504 + 9995d69 commit 8faa86f

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

docs/usage.rst

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
Usage (Or: How To Get The List of Libraries)
2-
============================================
1+
Usage
2+
=====
33

4-
The primary function that you'll care about in this package is ``stdlib_list.stdlib_list``.
4+
Getting The List of Libraries
5+
-----------------------------
6+
7+
``stdlib_list.stdlib_list`` returns the list of libraries in stdlib for any given version (by default, current python version).
58

69
In particular:
710

@@ -15,5 +18,30 @@ In particular:
1518
Out[3]: ['__future__', '__main__', '_dummy_thread', '_thread', 'abc', 'aifc']
1619

1720

21+
Checking if a Module is part of stdlib
22+
--------------------------------------
23+
24+
``stdlib_list.in_stdlib`` provides an efficient way to check if a module name is part of stdlib.
25+
It relies on ``@lru_cache`` to cache the stdlib list and query results for similar calls. Therefore it is much more efficient than ``module_name in stdlib_list()`` especially if you wish to perform multiple checks.
26+
27+
In particular:
28+
29+
::
30+
31+
>>> from stdlib_list import in_stdlib
32+
>>> in_stdlib('zipimport') # built in
33+
True
34+
>>> in_stdlib('math') # C-API stdlib module, but linked as extension (on my machine)
35+
True
36+
>>> in_stdlib('numpy') # C-API extension, not stdlib
37+
False
38+
>>> in_stdlib('sys') # built-in (and special)
39+
True
40+
>>> in_stdlib('os') # Python code in stdlib
41+
True
42+
>>> in_stdlib('requests') # Python code, not stdlib
43+
False
44+
45+
1846
.. automodule:: stdlib_list
19-
:members: stdlib_list
47+
:members: stdlib_list, in_stdlib

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
author_email='[email protected]',
1818
url='https://github.com/jackmaney/python-stdlib-list',
1919
version=versioneer.get_version(),
20+
install_requires=['functools32;python_version<"3.2"'],
2021
extras_require={"develop": ["sphinx"]},
2122
description='A list of Python Standard Libraries (2.6-7, 3.2-6).',
2223
long_description=long_description,

stdlib_list/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
del get_versions
44

55
# Import all the things that used to be in here for backwards-compatibility reasons
6-
from .base import stdlib_list, get_canonical_version, short_versions, long_versions, base_dir, list_dir
6+
from .base import stdlib_list, in_stdlib, get_canonical_version, short_versions, long_versions, base_dir, list_dir

stdlib_list/base.py

+38
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import os
44
import sys
55

6+
try:
7+
from functools import lru_cache
8+
except ImportError:
9+
from functools32 import lru_cache
10+
611
base_dir = os.path.dirname(os.path.realpath(__file__))
712

813
list_dir = os.path.join(base_dir, "lists")
@@ -45,3 +50,36 @@ def stdlib_list(version=None):
4550
result = [y for y in [x.strip() for x in f.readlines()] if y]
4651

4752
return result
53+
54+
55+
@lru_cache(maxsize=16)
56+
def _stdlib_list_with_cache(version=None):
57+
"""Internal cached version of `stdlib_list`"""
58+
return stdlib_list(version=version)
59+
60+
61+
@lru_cache(maxsize=256)
62+
def in_stdlib(module_name, version=None):
63+
"""
64+
Return a ``bool`` indicating if module ``module_name`` is in the list of stdlib
65+
symbols for python version ``version``. If ``version`` is ``None`` (default), the
66+
version of current python interpreter is used.
67+
68+
Note that ``True`` will be returned for built-in modules too, since this project
69+
considers they are part of stdlib. See :issue:21.
70+
71+
It relies on ``@lru_cache`` to cache the stdlib list and query results for similar
72+
calls. Therefore it is much more efficient than ``module_name in stdlib_list()``
73+
especially if you wish to perform multiple checks.
74+
75+
:param str|None module_name: The module name (as a string) to query for.
76+
:param str|None version: The version (as a string) whose list of libraries you want
77+
(one of ``"2.6"``, ``"2.7"``, ``"3.2"``, ``"3.3"``, ``"3.4"``, or ``"3.5"``).
78+
If not specified, the current version of Python will be used.
79+
80+
:return: A bool indicating if the given module name is part of standard libraries
81+
for the specified version of Python.
82+
:rtype: list
83+
"""
84+
ref_list = _stdlib_list_with_cache(version=version)
85+
return module_name in ref_list

0 commit comments

Comments
 (0)