Helpers
This page documents the analyzer.helpers utilities used across PatchTrack.
The module provides small, well-scoped helpers for common tasks used by the analysis pipeline: API request handling, file and path utilities, comment removal for many languages, light-weight timing instrumentation, and simple file I/O helpers.
Notes
- The module uses a lazy
_get_extension_map()helper to avoid import-time circular dependencies withanalyzer.common. - The
remove_commentsalias exists for backward compatibility — call eitherremove_commentorremove_comments.
Examples
Remove comments from a Python source string:
from analyzer import helpers, common
code = """# comment\nprint('hello')\n"""
clean = helpers.remove_comment(code, common.FileExt.Python)
print(clean)
Get the file type for a filename and count lines in a file:
Use the timing decorator to measure function time:
API Reference
Helper utilities for PatchTrack analysis.
Provides functions for API requests, file handling, comment removal, and type detection across multiple programming languages.
analyzer.helpers.unique(items)
Get unique items from a list while preserving order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
List
|
Input list with potential duplicates. |
required |
Returns:
| Type | Description |
|---|---|
List
|
List with duplicate entries removed. |
Source code in analyzer/helpers.py
analyzer.helpers.api_request(url, token)
Make an authenticated API request to GitHub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL endpoint to request. |
required |
token
|
str
|
GitHub API token for authentication. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Parsed JSON response or response object on error. |
Source code in analyzer/helpers.py
analyzer.helpers.get_response(url, token_list, ct)
Retrieve JSON response from API endpoint using token rotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
API endpoint URL. |
required |
token_list
|
List[str]
|
List of available GitHub API tokens. |
required |
ct
|
int
|
Current token index counter. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
Tuple of (json_data, updated_token_counter). |
Source code in analyzer/helpers.py
analyzer.helpers.file_name(name)
Extract the file name from a file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
File path or name string. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Extracted file name. |
Source code in analyzer/helpers.py
analyzer.helpers.file_dir(name)
Extract the directory path from a file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
File path string. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Directory path (empty string if no directory). |
Source code in analyzer/helpers.py
analyzer.helpers.save_file(file, storage_dir, file_name)
Save binary file to specified directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
bytes
|
Binary file content. |
required |
storage_dir
|
str
|
Directory path for storage. |
required |
file_name
|
str
|
Name of file to save. |
required |
Source code in analyzer/helpers.py
analyzer.helpers.get_file_type(file_path)
Detect file type based on extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path or name of the file. |
required |
Returns:
| Type | Description |
|---|---|
int
|
FileExt enum value indicating the file type. |
Source code in analyzer/helpers.py
analyzer.helpers.remove_comment(source, file_ext)
Remove comments from source code based on file type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
Source code text. |
required |
file_ext
|
int
|
FileExt enum value indicating language type. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Source code with comments removed. |
Source code in analyzer/helpers.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
analyzer.helpers.remove_comments(source, file_ext)
analyzer.helpers.timing(func)
Decorator to measure and print function execution time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Function to decorate. |
required |
Returns:
| Type | Description |
|---|---|
|
Decorated function with timing output. |
Source code in analyzer/helpers.py
analyzer.helpers.count_loc(file_path)
Count total lines of code in a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the file. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Total number of lines in the file. |
See Also
- Patch Loader - Parses PR patches
- Source Loader - Parses ChatGPT code
- Classifier - Uses n-gram configuration
- Common - Configuration (n-gram size, etc.)