cs-py-modules


Namecs-py-modules JSON
Version 20250724 PyPI version JSON
download
home_pageNone
SummaryConvenience functions related to modules, packages and importing.
upload_time2025-07-23 22:59:02
maintainerNone
docs_urlNone
authorNone
requires_pythonNone
licenseNone
keywords python2 python3
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Convenience functions related to modules, packages and importing.

*Latest release 20250724*:
* Improve resolution of relative imports. (Fix seems to be an overly strong word here, unfortunately.)
* Drop pretence at Python 2 support.

Short summary:
* `direct_imports`: Crudely parse `src_filename` for `import` statements. Return the set of directly imported module names.
* `import_extra`: Try to import the package named `extra_package_name` using `importlib.import_module`.
* `import_module_from_file`: Import a specific file as a module instance, return the module instance.
* `import_module_name`: Import `module_name` and return the value of `name` within it.
* `module_attributes`: Generator yielding the names and values of attributes from a module which were defined in the module.
* `module_files`: Generator yielding `.py` pathnames involved in a module.
* `module_names`: Return a list of the names of attributes from a module which were defined in the module.

Module contents:
- <a name="direct_imports"></a>`direct_imports(src_filename, module_name)`: Crudely parse `src_filename` for `import` statements.
  Return the set of directly imported module names.

  Resolve relative imports against `module_name`.

  This is a very simple minded source parse.
- <a name="import_extra"></a>`import_extra(extra_package_name, distinfo)`: Try to import the package named `extra_package_name`
  using `importlib.import_module`. Return the imported package.

  If an `ImportError` is raised,
  riffle through the extras mapping in `distinfo['extras_requires']`
  for the package name, and emit an informative warning
  about the extras which require this package
  and whose use a `pip install` time would bring the package in.
  The original `ImportError` is then reraised.

  If no extra is found this is presumed to be an error by the caller
  and a `RuntimeError` is raised.
  This function is for internal use as:

      pkg = import_extra('some_package', DISTINFO)

  which passes in the source module's `DISTINFO` mapping,
  which I use as the basis for my package distributions.

  A fuller example from my `cs.timeseries` module's
  `plot` command line mode:

      def cmd_plot(self, argv):
        """ Usage: {cmd} datadir days fields...
        """
        try:
          import_extra('plotly', DISTINFO)
        except ImportError as e:
          raise GetoptError(
              "the plotly package is not installed: %s" % (e,)
          ) from e

  which produces this output:

      timeseries.py: plot: import_extra('plotly'): package not available; the following extras pull it in: ['plotting']
      timeseries.py: the plotly package is not installed: timeseries.py: plot: import_extra('plotly'): No module named 'plotly'
- <a name="import_module_from_file"></a>`import_module_from_file(module_name, source_file, sys_path=None)`: Import a specific file as a module instance,
  return the module instance.

  Parameters:
  * `module_name`: the name to assign to the module
  * `source_file`: the source file to load
  * `sys_path`: optional list of paths to set as `sys.path`
    for the duration of this import;
    the default is the current value of `sys.path`

  Note that this is a "bare" import;
  the module instance is not inserted into `sys.modules`.

  *Warning*: `sys.path` is modified for the duration of this function,
  which may affect concurrent applications.
- <a name="import_module_name"></a>`import_module_name(module_name, name=None, sys_path=None, lock=None)`: Import `module_name` and return the value of `name` within it.

  Parameters:
  * `module_name`: the module name to import.
  * `name`: optional name within the module whose value is returned;
    if `name` is `None`, return the module itself.
  * `sys_path`optional list of paths to use as `sys.path` during the import.
  * `lock`: an optional lock to hold during the import (recommended).

  *Warning*: `sys.path` is modified for the duration of this function,
  which may affect concurrent applications.
- <a name="module_attributes"></a>`module_attributes(M)`: Generator yielding the names and values of attributes from a module
  which were defined in the module.
- <a name="module_files"></a>`module_files(M)`: Generator yielding `.py` pathnames involved in a module.
- <a name="module_names"></a>`module_names(M)`: Return a list of the names of attributes from a module which were
  defined in the module.

# Release Log



*Release 20250724*:
* Improve resolution of relative imports. (Fix seems to be an overly strong word here, unfortunately.)
* Drop pretence at Python 2 support.

*Release 20241122*:
import_module_name: rename the `path` parameter to `sys_path` to match import_module_from_file, default sys_path to sys.path.

*Release 20240630*:
direct_imports: fix off-by-one resolving leading dot relative import names.

*Release 20220606*:
New import_extra(extra_package_name,distinfo) function to politely try to import a package which is associated with an extra.

*Release 20210123*:
module_attributes: skip values from other modules _if we know the module_ (computed values like tuples have no module and still need to be returned).

*Release 20200521*:
* New import_module_from_file function to import a Python file as a module instance.
* New direct_imports(src_filename,module_name=None) returning the set of directly imports module names.

*Release 20190101*:
New functions: module_names, module_attributes.

*Release 20160918*:
* New generator function module_files yielding pathnames.
* import_module_name: accept name=None, just return the module.
* Add empty "install_requires" for DISTINFO completeness.

*Release 20150116*:
Initial PyPI release.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "cs-py-modules",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "python2, python3",
    "author": null,
    "author_email": "Cameron Simpson <cs@cskk.id.au>",
    "download_url": "https://files.pythonhosted.org/packages/63/b8/80d9a1ae3fc7f0d797bf028af249b938be866fa8d40a4a518747fa243887/cs_py_modules-20250724.tar.gz",
    "platform": null,
    "description": "Convenience functions related to modules, packages and importing.\n\n*Latest release 20250724*:\n* Improve resolution of relative imports. (Fix seems to be an overly strong word here, unfortunately.)\n* Drop pretence at Python 2 support.\n\nShort summary:\n* `direct_imports`: Crudely parse `src_filename` for `import` statements. Return the set of directly imported module names.\n* `import_extra`: Try to import the package named `extra_package_name` using `importlib.import_module`.\n* `import_module_from_file`: Import a specific file as a module instance, return the module instance.\n* `import_module_name`: Import `module_name` and return the value of `name` within it.\n* `module_attributes`: Generator yielding the names and values of attributes from a module which were defined in the module.\n* `module_files`: Generator yielding `.py` pathnames involved in a module.\n* `module_names`: Return a list of the names of attributes from a module which were defined in the module.\n\nModule contents:\n- <a name=\"direct_imports\"></a>`direct_imports(src_filename, module_name)`: Crudely parse `src_filename` for `import` statements.\n  Return the set of directly imported module names.\n\n  Resolve relative imports against `module_name`.\n\n  This is a very simple minded source parse.\n- <a name=\"import_extra\"></a>`import_extra(extra_package_name, distinfo)`: Try to import the package named `extra_package_name`\n  using `importlib.import_module`. Return the imported package.\n\n  If an `ImportError` is raised,\n  riffle through the extras mapping in `distinfo['extras_requires']`\n  for the package name, and emit an informative warning\n  about the extras which require this package\n  and whose use a `pip install` time would bring the package in.\n  The original `ImportError` is then reraised.\n\n  If no extra is found this is presumed to be an error by the caller\n  and a `RuntimeError` is raised.\n  This function is for internal use as:\n\n      pkg = import_extra('some_package', DISTINFO)\n\n  which passes in the source module's `DISTINFO` mapping,\n  which I use as the basis for my package distributions.\n\n  A fuller example from my `cs.timeseries` module's\n  `plot` command line mode:\n\n      def cmd_plot(self, argv):\n        \"\"\" Usage: {cmd} datadir days fields...\n        \"\"\"\n        try:\n          import_extra('plotly', DISTINFO)\n        except ImportError as e:\n          raise GetoptError(\n              \"the plotly package is not installed: %s\" % (e,)\n          ) from e\n\n  which produces this output:\n\n      timeseries.py: plot: import_extra('plotly'): package not available; the following extras pull it in: ['plotting']\n      timeseries.py: the plotly package is not installed: timeseries.py: plot: import_extra('plotly'): No module named 'plotly'\n- <a name=\"import_module_from_file\"></a>`import_module_from_file(module_name, source_file, sys_path=None)`: Import a specific file as a module instance,\n  return the module instance.\n\n  Parameters:\n  * `module_name`: the name to assign to the module\n  * `source_file`: the source file to load\n  * `sys_path`: optional list of paths to set as `sys.path`\n    for the duration of this import;\n    the default is the current value of `sys.path`\n\n  Note that this is a \"bare\" import;\n  the module instance is not inserted into `sys.modules`.\n\n  *Warning*: `sys.path` is modified for the duration of this function,\n  which may affect concurrent applications.\n- <a name=\"import_module_name\"></a>`import_module_name(module_name, name=None, sys_path=None, lock=None)`: Import `module_name` and return the value of `name` within it.\n\n  Parameters:\n  * `module_name`: the module name to import.\n  * `name`: optional name within the module whose value is returned;\n    if `name` is `None`, return the module itself.\n  * `sys_path`optional list of paths to use as `sys.path` during the import.\n  * `lock`: an optional lock to hold during the import (recommended).\n\n  *Warning*: `sys.path` is modified for the duration of this function,\n  which may affect concurrent applications.\n- <a name=\"module_attributes\"></a>`module_attributes(M)`: Generator yielding the names and values of attributes from a module\n  which were defined in the module.\n- <a name=\"module_files\"></a>`module_files(M)`: Generator yielding `.py` pathnames involved in a module.\n- <a name=\"module_names\"></a>`module_names(M)`: Return a list of the names of attributes from a module which were\n  defined in the module.\n\n# Release Log\n\n\n\n*Release 20250724*:\n* Improve resolution of relative imports. (Fix seems to be an overly strong word here, unfortunately.)\n* Drop pretence at Python 2 support.\n\n*Release 20241122*:\nimport_module_name: rename the `path` parameter to `sys_path` to match import_module_from_file, default sys_path to sys.path.\n\n*Release 20240630*:\ndirect_imports: fix off-by-one resolving leading dot relative import names.\n\n*Release 20220606*:\nNew import_extra(extra_package_name,distinfo) function to politely try to import a package which is associated with an extra.\n\n*Release 20210123*:\nmodule_attributes: skip values from other modules _if we know the module_ (computed values like tuples have no module and still need to be returned).\n\n*Release 20200521*:\n* New import_module_from_file function to import a Python file as a module instance.\n* New direct_imports(src_filename,module_name=None) returning the set of directly imports module names.\n\n*Release 20190101*:\nNew functions: module_names, module_attributes.\n\n*Release 20160918*:\n* New generator function module_files yielding pathnames.\n* import_module_name: accept name=None, just return the module.\n* Add empty \"install_requires\" for DISTINFO completeness.\n\n*Release 20150116*:\nInitial PyPI release.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Convenience functions related to modules, packages and importing.",
    "version": "20250724",
    "project_urls": {
        "MonoRepo Commits": "https://bitbucket.org/cameron_simpson/css/commits/branch/main",
        "Monorepo Git Mirror": "https://github.com/cameron-simpson/css",
        "Monorepo Hg/Mercurial Mirror": "https://hg.sr.ht/~cameron-simpson/css",
        "Source": "https://github.com/cameron-simpson/css/blob/main/lib/python/cs/py/modules.py"
    },
    "split_keywords": [
        "python2",
        " python3"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "8c3e37f81c692c69c6fd9e4994e393311313132b75bf1052005d6f22b05bbc08",
                "md5": "a5747ea84afe8cff4e22736bf9a53279",
                "sha256": "019d7c2b66b1158ed8c9e7b400754660c578117d538a206a779982fa40996b55"
            },
            "downloads": -1,
            "filename": "cs_py_modules-20250724-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a5747ea84afe8cff4e22736bf9a53279",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 5946,
            "upload_time": "2025-07-23T22:59:01",
            "upload_time_iso_8601": "2025-07-23T22:59:01.092039Z",
            "url": "https://files.pythonhosted.org/packages/8c/3e/37f81c692c69c6fd9e4994e393311313132b75bf1052005d6f22b05bbc08/cs_py_modules-20250724-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "63b880d9a1ae3fc7f0d797bf028af249b938be866fa8d40a4a518747fa243887",
                "md5": "f53a9214b9adabf55284fc91929a1f62",
                "sha256": "d39bdde0b874fb4f0cd5af2e0374cb47073df68826ad9c8b33c87ebc7ea05904"
            },
            "downloads": -1,
            "filename": "cs_py_modules-20250724.tar.gz",
            "has_sig": false,
            "md5_digest": "f53a9214b9adabf55284fc91929a1f62",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4833,
            "upload_time": "2025-07-23T22:59:02",
            "upload_time_iso_8601": "2025-07-23T22:59:02.567695Z",
            "url": "https://files.pythonhosted.org/packages/63/b8/80d9a1ae3fc7f0d797bf028af249b938be866fa8d40a4a518747fa243887/cs_py_modules-20250724.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-23 22:59:02",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "cameron-simpson",
    "github_project": "css",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "cs-py-modules"
}
        
Elapsed time: 0.87573s