Skip to main content

Function Extractor

Python application License: MIT codecov

Tool to extract the function definitions from the source code

It can be used to extract functions from,

  • C

  • C++

  • C#

  • Java

  • Python

  • TypeScript

  • JavaScript

Advantage of using such function extractions are,

  • Resolving technical debt

  • Identify function similarity

  • Identify pattern check (Supresswarnings, Assert, etc...)

Dependencies

  • python 3.8 : 64 bit

  • python packages (xlrd, xlsxwriter, pandas)

  • third party packages [Ctags, grep]

Installation

Requirements are added in requirement.txt file

Python 3.8

python: install python for the respective OS at "https://www.python.org/downloads/" Make sure to update the path variable to point to the python installation folder.

pip: (only if pip is not present by default) get get-pip.py from below link to your folder "https://bootstrap.pypa.io/get-pip.py" Open a command prompt and navigate to the folder containing get-pip.py.

Run the following command:

functiondefextractor:

pip install functiondefextractor

Other tools

Ctags: "https://en.wikipedia.org/wiki/Ctags"

  • Windows:

1.Download Ctags from "http://ctags.sourceforge.net/"

2.Select the right package(based on OS & architecture) and

extract the zip file to a folder

3.Update the system 'path' environment variable with the path to ctags executable

  • Linux:

apt-get install ctags

  • OS X:

brew install ctags

grep

1.Download grep "binaries and Dependencies"

from http://gnuwin32.sourceforge.net/packages/grep.htm

2.Extract the content to a folder

3.Copy and paste contents from \bin folder of Dependencies

to \bin folder of Binaries

4.Update the system 'path' environment variable with the path to "grep" executable

pip install functiondefextractor

Usage & Configuration

Code

  • General usage with out options.
from functiondefextractor import core_extractor
out_put = core_extractor.extractor (r"path_to_repo/code")
print(out_put)
  • To exclude specific files from repository.
from functiondefextractor import core_extractor
out_put = core_extractor.extractor (r"path_to_repo/code", exclude=r'*\test\*,*.java')
print(out_put)

Sample regex patterns: Note: Space given after comma(,) in regex pattern is also treated as part of the pattern. For example

(*.java, *.cpp) != (*.java,*.cpp)
1. '*.java' =>  to exclude all java files in a repository.

2. '*/test/*' => to exclude test folder and files in it.

3. '*/src/*/*.cpp' => to exclude all cpp files in src and it's sub directories
  • To extract functions based on annotation.
from functiondefextractor import core_extractor
out_put = core_extractor.extractor (r"path_to_repo/code", annot="@Test")
print(out_put)
  • To extract delta lines(+/-) from code based on annotation/key word. Note: If user is unaware of complete annotation use this(annot with delta) feature to extract functions else use the above feature. Suggested to use delta=0 to get only line with annotation.
from functiondefextractor import core_extractor
out_put = core_extractor.extractor(r"path_to_repo/code", annot="@SupressWarning", delta="5")
print(out_put)
  • To analyse various patterns in the code based on given condition. For example to search assert, suppress warnings patterns.
from functiondefextractor import condition_checker
out_put = core_extractor.check_condition("@SupressWarning", r"path_to_excelfile/dataframe", "(")
print(out_put[0], out_put[1])

Commandline

  • General usage with out options to extract functions from repo.
>>>python -m functiondefextractor --p "path/to/repo"
  • To extract functions from repo having specific annotation.
>>>python -m functiondefextractor --p "path/to/repo" --a "@SuppressWarnings(\"UnusedReturnValue\")"

Note: If annotation contains double quotes as part of annotation(like above example) use backslash() before double quote inside annotation.

  • To ignore files from repo using regex pattern.
>>>python -m functiondefextractor --p "path/to/repo" --i '*.java, *.cpp'
  • To analyse various patterns in the code based on given condition.
>>>python -m functiondefextractor --c "Assert" --e "path/to/excel" --s "("
  • Help option can be found at,
>>>python -m functiondefextractor --h

Sample use cases

  • To extract all functions from a repository
>>>python -m functiondefextractor --p "path/to/repo"
from functiondefextractor import core_extractor
out_put = core_extractor.extractor (r"path_to_repo/code")
print(out_put)
  • To extract all functions with "@Test" annotation excluding all ".cpp" files in the repository
>>>python -m functiondefextractor --p "path/to/repo" --a "@Test" --i '*.cpp'
from functiondefextractor import core_extractor
out_put = core_extractor.extractor(r"path_to_repo/code", annot="@Test", exclude=r'*.cpp')
print(out_put)

Note:

  1. functionstartwith argument can be used to specifically extract code from required functions whose names starts with "test_" or what ever name user is interested in.

  2. delta and annot arguments together can be used to extract required number of lines below and above the given annotation/keyword.

  • To analyze various patterns present in extracted code
>>>python -m functiondefextractor --c "Assert" --e "path/to/excel" --s "("
from functiondefextractor import condition_checker
out_put = core_extractor.check_condition("@SupressWarning", r"path_to_excelfile/dataframe", "(")
print(out_put[0], out_put[1])

Output

  • Executing functiondefextractor to extract functions from command line would generate an output excel file which contains FileName_FunctionName in Unique ID column and extracted functions in Code column

  • Using functiondefextractor to extract functions from code would return a dataframe with same content as excel file.

  • When functiondefextractor is executed from script to analyse patterns in code, a tuple with 2 data frames would be generated which contains the requested pattern statements with their count in various functions and a pivot table of the same respectively.

Contact

Brijesh Krishnan brijesh.krishnank@philips.com

Sannihith Reddy sannihith.reddyp@philips.com

License

The MIT License (MIT) Copyright © [2019] Koninklijke Philips N.V, "https://www.philips.com"

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Metadata

Release files for functiondefextractor 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for functiondefextractor 0.1.2
File Size Uploaded Provenance
functiondefextractor-0.1.2.tar.gz 16.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for functiondefextractor 0.1.2
File Interpreter ABI Platform
functiondefextractor-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 32.4 kB

Release files / functiondefextractor-0.1.2.tar.gz

Download URL functiondefextractor-0.1.2.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4dbb6d91a1c8124fe415bbccc6e7bbd7a6dbaead2d9d7edd864925251a345fcb
BLAKE2b-256 checksum
How to use checksums
2e05bded82b4bba27f5c6e85aac079555b607d160aa52dbfb27f7c9aff356e0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release files / functiondefextractor-0.1.2-py3-none-any.whl

Download URL functiondefextractor-0.1.2-py3-none-any.whl
Size 15.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
87fa607c9c4823b9ccbc30ae5638d685b11c9203365926267de00c3ce38c1cc1
BLAKE2b-256 checksum
How to use checksums
196a33e95f946ceb8f02d8cfe6b3f546c9f72c68a314b55ea116e4839c2bf45d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Security for functiondefextractor 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page