cjm-psl-utils


Namecjm-psl-utils JSON
Version 0.0.4 PyPI version JSON
download
home_pagehttps://github.com/cj-mills/cjm-psl-utils
SummarySome utility functions using the Python Standard Library.
upload_time2023-07-04 21:27:58
maintainer
docs_urlNone
authorcj-mills
requires_python>=3.9
licenseApache Software License 2.0
keywords nbdev jupyter notebook python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # cjm-psl-utils

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

This file will become your README and also the index of your
documentation.

## Install

``` sh
pip install cjm_psl_utils
```

## How to use

### get_source_code

``` python
from cjm_psl_utils.core import get_source_code
```

``` python
get_source_code(get_source_code, markdown=True)
```

``` python
def get_source_code(obj:object, # The object whose source code you want to retrieve.
                    markdown=False, # Return the source code formatted as markdown
                    remove_documentation=False): # Remove docstrings and comments
    """
    Returns the source code of an object, with an optional markdown formatting.
    """
    # Get the source code of the object
    source = inspect.getsource(obj)
    
    # If the remove_documentation flag is set to True, remove docstrings and comments
    if remove_documentation:
        in_docstring = False
        lines = source.split('\n')
        source = ''
        # Remove docstrings
        for line in lines:
            if line.strip().startswith(('\'\'\'', '\"\"\"')):
                in_docstring = not in_docstring
            elif not in_docstring:
                source += line + '\n'
        # Remove comments
        source = '\n'.join([line for line in source.split('\n')
                            if not line.strip().startswith(('#'))])
        source = source.replace('\n\n', '\n')

    if markdown:
        # Format the source code as markdown code block
        source = f"```python\n{source}\n```"

        # Check if the code is running in Jupyter Notebook
        try:
            get_ipython
            from IPython.display import Markdown
            # Format the source code as an IPython Markdown object
            source = Markdown(source)
        except NameError:
            # If not in Jupyter Notebook, do nothing
            pass
    # Return the formatted source code
    return source
```

### file_extract

``` python
from cjm_psl_utils.core import file_extract
from pathlib import Path
```

``` python
fname = "../images/images.zip"
dest = Path("./images")
```

``` python
print(dest.exists())
file_extract(fname, dest)
print(dest.exists())
print(list(os.walk(dest))[0][2])
```

    False
    True
    ['cat.jpg']

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/cj-mills/cjm-psl-utils",
    "name": "cjm-psl-utils",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "nbdev jupyter notebook python",
    "author": "cj-mills",
    "author_email": "millscj.mills2@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/0a/69/a01b731b34adce7613dfbc6cac4ed031667fe0fa6d0dd7a0e06dfb7486f3/cjm-psl-utils-0.0.4.tar.gz",
    "platform": null,
    "description": "# cjm-psl-utils\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\nThis file will become your README and also the index of your\ndocumentation.\n\n## Install\n\n``` sh\npip install cjm_psl_utils\n```\n\n## How to use\n\n### get_source_code\n\n``` python\nfrom cjm_psl_utils.core import get_source_code\n```\n\n``` python\nget_source_code(get_source_code, markdown=True)\n```\n\n``` python\ndef get_source_code(obj:object, # The object whose source code you want to retrieve.\n                    markdown=False, # Return the source code formatted as markdown\n                    remove_documentation=False): # Remove docstrings and comments\n    \"\"\"\n    Returns the source code of an object, with an optional markdown formatting.\n    \"\"\"\n    # Get the source code of the object\n    source = inspect.getsource(obj)\n    \n    # If the remove_documentation flag is set to True, remove docstrings and comments\n    if remove_documentation:\n        in_docstring = False\n        lines = source.split('\\n')\n        source = ''\n        # Remove docstrings\n        for line in lines:\n            if line.strip().startswith(('\\'\\'\\'', '\\\"\\\"\\\"')):\n                in_docstring = not in_docstring\n            elif not in_docstring:\n                source += line + '\\n'\n        # Remove comments\n        source = '\\n'.join([line for line in source.split('\\n')\n                            if not line.strip().startswith(('#'))])\n        source = source.replace('\\n\\n', '\\n')\n\n    if markdown:\n        # Format the source code as markdown code block\n        source = f\"```python\\n{source}\\n```\"\n\n        # Check if the code is running in Jupyter Notebook\n        try:\n            get_ipython\n            from IPython.display import Markdown\n            # Format the source code as an IPython Markdown object\n            source = Markdown(source)\n        except NameError:\n            # If not in Jupyter Notebook, do nothing\n            pass\n    # Return the formatted source code\n    return source\n```\n\n### file_extract\n\n``` python\nfrom cjm_psl_utils.core import file_extract\nfrom pathlib import Path\n```\n\n``` python\nfname = \"../images/images.zip\"\ndest = Path(\"./images\")\n```\n\n``` python\nprint(dest.exists())\nfile_extract(fname, dest)\nprint(dest.exists())\nprint(list(os.walk(dest))[0][2])\n```\n\n    False\n    True\n    ['cat.jpg']\n",
    "bugtrack_url": null,
    "license": "Apache Software License 2.0",
    "summary": "Some utility functions using the Python Standard Library.",
    "version": "0.0.4",
    "project_urls": {
        "Homepage": "https://github.com/cj-mills/cjm-psl-utils"
    },
    "split_keywords": [
        "nbdev",
        "jupyter",
        "notebook",
        "python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cc90d2a82488979d96a0bf783c7e51d75a37ce5c06f1641b4b073cf6d9a164b9",
                "md5": "7870cf402ae51e19bd28bbdadde5f8f2",
                "sha256": "6b1d291d43fa5234b8664a069eb802e91d1aaa40424a814c2d6a2ce15288136d"
            },
            "downloads": -1,
            "filename": "cjm_psl_utils-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7870cf402ae51e19bd28bbdadde5f8f2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 5852,
            "upload_time": "2023-07-04T21:27:57",
            "upload_time_iso_8601": "2023-07-04T21:27:57.251480Z",
            "url": "https://files.pythonhosted.org/packages/cc/90/d2a82488979d96a0bf783c7e51d75a37ce5c06f1641b4b073cf6d9a164b9/cjm_psl_utils-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0a69a01b731b34adce7613dfbc6cac4ed031667fe0fa6d0dd7a0e06dfb7486f3",
                "md5": "59318b83ced1e9f725391b796fbc37e7",
                "sha256": "eec200f9f2ccfe4e2df01e0f22bd8483afde77377130bd8282e628c6762fe755"
            },
            "downloads": -1,
            "filename": "cjm-psl-utils-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "59318b83ced1e9f725391b796fbc37e7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 5907,
            "upload_time": "2023-07-04T21:27:58",
            "upload_time_iso_8601": "2023-07-04T21:27:58.692191Z",
            "url": "https://files.pythonhosted.org/packages/0a/69/a01b731b34adce7613dfbc6cac4ed031667fe0fa6d0dd7a0e06dfb7486f3/cjm-psl-utils-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-04 21:27:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "cj-mills",
    "github_project": "cjm-psl-utils",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "cjm-psl-utils"
}
        
Elapsed time: 0.11248s