strings.py¶
String manipulation utilities for NodeWeaver.
This module provides comprehensive string manipulation utilities focused on handling Houdini-specific string operations, path management, and object representation. It includes functions for sanitization, case conversion, tree visualization, and detailed object inspection.
Functions:
Name | Description |
---|---|
sanitize_string |
Clean strings with custom transformations |
fix_slash |
Replace backslashes with forward slashes |
fix_periods |
Replace periods with underscores |
clean_posix_path |
Normalize and validate POSIX paths |
is_camelcase |
Check if string follows camelCase format |
to_camelcase |
Convert string to camelCase format |
to_titlecase |
Convert string to Title Case format |
camelcase_path |
Convert path components to camelCase |
tree_from_string_list |
Create tree visualization from path strings |
to_digits |
Extract only digits from a string |
print_dict |
Print nested dictionary structures with formatting |
comprehensive_repr |
Generate detailed object representations |
camelcase_path(path)
¶
Convert a full path to camelCase while preserving slashes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path string to convert |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Path with each component converted to camelCase |
Source code in utils/strings.py
clean_posix_path(path)
¶
Clean and normalize a path to POSIX format.
Removes invalid characters, normalizes slashes, and resolves relative paths.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to clean and normalize |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Cleaned POSIX-style path |
Raises:
Type | Description |
---|---|
ValueError
|
If path contains invalid characters |
Source code in utils/strings.py
comprehensive_repr(obj, exclude=None, prioritize=None, include_private=False, include_callable=False, sort_keys=False, max_length=None, one_per_line=False, filter_func=None, _depth=0, _visited=None)
¶
Generate a comprehensive string representation of an object.
Creates a detailed string representation of an object, with extensive customization options for controlling output format and content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
The object to represent |
required |
exclude
|
Optional[List[str]]
|
Attributes to exclude from representation |
None
|
prioritize
|
Optional[List[str]]
|
Attributes to list first in representation |
None
|
include_private
|
bool
|
Include attributes starting with underscore |
False
|
include_callable
|
bool
|
Include methods and other callable attributes |
False
|
sort_keys
|
bool
|
Sort attributes alphabetically |
False
|
max_length
|
Optional[int]
|
Truncate result to this length |
None
|
one_per_line
|
bool
|
Put each attribute on a new line |
False
|
filter_func
|
Optional[Callable[[str, Any], bool]]
|
Custom function to filter attributes |
None
|
_depth
|
int
|
Internal parameter tracking recursion depth |
0
|
_visited
|
Optional[set]
|
Internal set tracking visited objects |
None
|
Returns:
Type | Description |
---|---|
str
|
Formatted string representation |
Examples:
>>> # Exclude specific attributes
>>> print(comprehensive_repr(Example(), exclude=['b']))
Example(a=1)
Notes
- Handles recursive structures safely
- Provides cycle detection
- Supports custom filtering and formatting
- Preserves type information
Since: 1.0.0
Source code in utils/strings.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
|
fix_periods(s)
¶
Replace periods with underscores in a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
Input string containing periods to fix |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
String with periods replaced by underscores |
fix_slash(s)
¶
Replace backslashes with forward slashes in a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
Input string containing slashes to fix |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
String with backslashes replaced by forward slashes |
is_camelcase(s)
¶
Check if a string is in camelCase format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
String to check |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if string is camelCase, False otherwise |
Source code in utils/strings.py
print_dict(d, indent=0, is_nested=False)
¶
Print dictionary or list with formatted indentation.
Recursively prints dictionaries and lists with proper indentation for nested structures. Handles None values and non-dict/list objects appropriately.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d
|
dict
|
Dictionary or list to print |
required |
indent
|
int
|
Number of spaces to indent. Defaults to 0 |
0
|
is_nested
|
bool
|
Whether this is a nested call. Defaults to False. Used internally for recursive calls. |
False
|
Example
data = {'a': 1, 'b': {'c': 2}} print_dict(data) { "a": 1, "b": { "c": 2, }, }
Source code in utils/strings.py
sanitize_string(s, transformations=None)
¶
Apply a series of transformations to sanitize a string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
The input string to sanitize |
required |
transformations
|
Optional[List[Callable[[str], str]]]
|
Optional list of functions to apply to the string. Default transformations handle special characters and spaces. |
None
|
Returns:
Type | Description |
---|---|
str
|
The sanitized string |
Examples:
>>> # Custom transformations
>>> transformations = [str.lower, lambda x: x.replace(' ', '-')]
>>> sanitize_string("Hello World!", transformations)
'hello-world!'
Note
Default transformations replace: *?:"<>| and spaces with underscores
Source code in utils/strings.py
to_camelcase(s, check=True)
¶
Convert string to camelCase format.
Handles multiple separator types and preserves existing camelCase if check=True.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
String to convert |
required |
check
|
bool
|
Whether to preserve existing camelCase. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
String in camelCase format |
Source code in utils/strings.py
to_digits(s)
¶
Convert string to digits by removing all non-digit characters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
Input string containing digits and other characters |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
String containing only the digit characters from input |
Example
to_digits("abc123def456") '123456'
Source code in utils/strings.py
to_titlecase(s)
¶
Convert string to Title Case format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
str
|
String to convert |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
String in Title Case format |
tree_from_string_list(paths)
¶
Create a tree visualization from a list of path strings.
Converts a list of path strings into an ASCII tree structure representation, showing hierarchical relationships.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
paths
|
List[str]
|
List of path strings to visualize |
required |
Returns:
Type | Description |
---|---|
str
|
String containing ASCII tree visualization |
Example
paths = ['/root/dir1/file1', '/root/dir1/file2', '/root/dir2'] print(tree_from_string_list(paths)) root ├── dir1 │ ├── file1 │ └── file2 └── dir2
Notes
- Uses Unicode box-drawing characters
- Properly handles mixed depth paths
- Sorts entries for consistent output
Since: 1.0.0