rust-code-analysis-python
  • rust-code-analysis-python
  • Quick-Start Guide
    • Remove comments from code
    • Get code metrics
  • Reference
rust-code-analysis-python
  • Quick-Start Guide

Quick-Start Guide¶

If you are using a supported Operating System - Architecture combination, you should be able to simply pip install rust-code-analysis-python, which will download and install the pre-compiled binary package.

Should this fail, you may need to install the Rust toolchain to build from source. We recommend using https://rustup.rs/ to do so. Once installed, pip install rust-code-analysis-python will work as expected.

In [ ]:
Copied!
%pip install rust-code-analysis-python
from rust_code_analysis_python import compute_metrics, remove_comments

filename = "example.js"
code = """
/* My example file */
const x = 42; // This is a constant
function add(a, b) {
    return a + b; // Adds two numbers
}
function fibonacci(n) {
    if (n <= 1) return n; // Base case
    return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case
}
"""
%pip install rust-code-analysis-python from rust_code_analysis_python import compute_metrics, remove_comments filename = "example.js" code = """ /* My example file */ const x = 42; // This is a constant function add(a, b) { return a + b; // Adds two numbers } function fibonacci(n) { if (n <= 1) return n; // Base case return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case } """

Remove comments from code¶

In [2]:
Copied!
code_without_comments = remove_comments(filename, code)
print(code_without_comments)
code_without_comments = remove_comments(filename, code) print(code_without_comments)

const x = 42; 
function add(a, b) {
    return a + b; 
}
function fibonacci(n) {
    if (n <= 1) return n; 
    return fibonacci(n - 1) + fibonacci(n - 2); 
}

Get code metrics¶

In [11]:
Copied!
metrics_result = compute_metrics(filename, code, unit=False)
print("File Metrics")
print("\tHalstead Difficulty =", metrics_result['metrics']['halstead']['difficulty'])
print("\tMaintainability Index =", metrics_result['metrics']['mi']['mi_visual_studio'])
for space in metrics_result['spaces']:
  print(f"Function {space['name']} metrics")
  print("\tHalstead Difficulty =", space['metrics']['halstead']['difficulty'])
  print("\tMaintainability Index =", space['metrics']['mi']['mi_visual_studio'])
print()
metrics_result = compute_metrics(filename, code, unit=False) print("File Metrics") print("\tHalstead Difficulty =", metrics_result['metrics']['halstead']['difficulty']) print("\tMaintainability Index =", metrics_result['metrics']['mi']['mi_visual_studio']) for space in metrics_result['spaces']: print(f"Function {space['name']} metrics") print("\tHalstead Difficulty =", space['metrics']['halstead']['difficulty']) print("\tMaintainability Index =", space['metrics']['mi']['mi_visual_studio']) print()
File Metrics
	Halstead Difficulty = 12.0
	Maintainability Index = 62.70845905142113
Function add metrics
	Halstead Difficulty = 5.833333333333333
	Maintainability Index = 78.25036867974562
Function fibonacci metrics
	Halstead Difficulty = 12.375
	Maintainability Index = 72.71109985054457

The following are all available metrics:

In [10]:
Copied!
def pprint_dict(d, depth = 0):
  for k, v in d.items():
    head = f"{'\t'*depth}- {k}:"
    if k == 'spaces':
      print(head, "A list of dictionaries with the same schema if unit=False, otherwise []")
    elif isinstance(v, dict):
      print(head)
      pprint_dict(v, depth + 1)
    else: 
      print(head, v)
pprint_dict(metrics_result)
def pprint_dict(d, depth = 0): for k, v in d.items(): head = f"{'\t'*depth}- {k}:" if k == 'spaces': print(head, "A list of dictionaries with the same schema if unit=False, otherwise []") elif isinstance(v, dict): print(head) pprint_dict(v, depth + 1) else: print(head, v) pprint_dict(metrics_result)
- name: example.js
- start_line: 2
- end_line: 10
- kind: unit
- spaces: A list of dictionaries with the same schema if unit=False, otherwise []
- metrics:
	- nargs:
		- total_functions: 3.0
		- total_closures: 0.0
		- average_functions: 1.5
		- average_closures: 0.0
		- total: 3.0
		- average: 1.5
		- functions_min: 0.0
		- functions_max: 2.0
		- closures_min: 0.0
		- closures_max: 0.0
	- nexits:
		- sum: 0.0
		- average: 1.5
		- min: 0.0
		- max: 2.0
	- cognitive:
		- sum: 1.0
		- average: 0.5
		- min: 0.0
		- max: 1.0
	- cyclomatic:
		- sum: 4.0
		- average: 1.3333333333333333
		- min: 1.0
		- max: 2.0
	- halstead:
		- n1: 12.0
		- N1: 25.0
		- n2: 9.0
		- N2: 18.0
		- length: 43.0
		- estimated_program_length: 71.54887502163469
		- purity_ratio: 1.6639273260845275
		- vocabulary: 21.0
		- volume: 188.86964917948671
		- difficulty: 12.0
		- level: 0.08333333333333333
		- effort: 2266.4357901538406
		- time: 125.91309945299115
		- bugs: 0.05751410399993093
	- loc:
		- sloc: 9.0
		- ploc: 8.0
		- lloc: 6.0
		- cloc: 5.0
		- blank: 0.0
		- sloc_average: 3.0
		- ploc_average: 2.6666666666666665
		- lloc_average: 2.0
		- cloc_average: 1.6666666666666667
		- blank_average: 0.0
		- sloc_min: 3.0
		- sloc_max: 4.0
		- cloc_min: 1.0
		- cloc_max: 2.0
		- ploc_min: 3.0
		- ploc_max: 4.0
		- lloc_min: 2.0
		- lloc_max: 4.0
		- blank_min: 0.0
		- blank_max: 0.0
	- nom:
		- functions: 2.0
		- closures: 0.0
		- functions_average: 0.6666666666666666
		- closures_average: 0.0
		- total: 2.0
		- average: 0.6666666666666666
		- functions_min: 0.0
		- functions_max: 1.0
		- closures_min: 0.0
		- closures_max: 0.0
	- mi:
		- mi_original: 107.23146497793014
		- mi_sei: 125.14242821222805
		- mi_visual_studio: 62.70845905142113
	- abc:
		- assignments: 0.0
		- branches: 0.0
		- conditions: 0.0
		- magnitude: 0.0
		- assignments_average: 0.0
		- branches_average: 0.0
		- conditions_average: 0.0
		- assignments_min: 0.0
		- assignments_max: 0.0
		- branches_min: 0.0
		- branches_max: 0.0
		- conditions_min: 0.0
		- conditions_max: 0.0
Previous Next

Built with MkDocs using a theme provided by Read the Docs.