sob.utilities
get_property_name
get_property_name(name: str) -> str
Converts a "camelCased" attribute/property name, a name which conflicts with a python keyword, or an otherwise non-compatible string to a PEP-8 compliant property name.
Examples:
>>> print(get_property_name("theBirdsAndTheBees"))
the_birds_and_the_bees
>>> print(get_property_name("theBirdsAndTheBEEs"))
the_birds_and_the_bees
>>> print(get_property_name("theBirdsAndTheBEEsEs"))
the_birds_and_the_be_es_es
>>> print(get_property_name("FYIThisIsAnAcronym"))
fyi_this_is_an_acronym
>>> print(get_property_name("in"))
in_
>>> print(get_property_name("id"))
id_
>>> print(get_property_name("one2one")) # No change needed
one2one
>>> print(get_property_name("One2One"))
one_2_one
>>> print(get_property_name("@One2One"))
one_2_one
>>> print(get_property_name("One2One-ALL"))
one_2_one_all
>>> print(get_property_name("one2one-ALL"))
one2one_all
>>> print(get_property_name("type"))
type_
>>> print(get_property_name("@type"))
type_
Source code in src/sob/utilities.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
get_class_name
get_class_name(name: str) -> str
This function accepts a string and returns a variation of that string which is a PEP-8 compatible python class name.
Examples:
>>> print(get_class_name("the birds and the bees"))
TheBirdsAndTheBees
>>> print(get_class_name("the-birds-and-the-bees"))
TheBirdsAndTheBees
>>> print(get_class_name("**the - birds - and - the - bees**"))
TheBirdsAndTheBees
>>> print(get_class_name("FYI is an acronym"))
FYIIsAnAcronym
>>> print(get_class_name("in-you-go"))
InYouGo
>>> print(get_class_name("False"))
False_
>>> print(get_class_name("True"))
True_
>>> print(get_class_name("ABC Acronym"))
ABCAcronym
>>> print(get_class_name("AB CD Efg"))
ABCdEfg
Source code in src/sob/utilities.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
camel
camel(string: str, *, capitalize: bool = False) -> str
This function returns a camelCased representation of the input string.
Parameters:
-
string
(str
) –The string to be camelCased.
-
capitalize
(bool
, default:False
) –If this is
true
, the first letter will be capitalized.
Examples:
>>> print(camel("the birds and the bees"))
theBirdsAndTheBees
>>> print(camel("the birds and the bees", capitalize=True))
TheBirdsAndTheBees
>>> print(camel("the-birds-and-the-bees"))
theBirdsAndTheBees
>>> print(camel("**the - birds - and - the - bees**"))
theBirdsAndTheBees
>>> print(camel("FYI is an acronym"))
FYIIsAnAcronym
>>> print(camel("in-you-go"))
inYouGo
>>> print(camel("False"))
false
>>> print(camel("True"))
true
>>> print(camel("in"))
in
>>> print(camel("AB CD Efg", capitalize=True))
ABCdEfg
>>> print(camel("ABC DEF GHI", capitalize=True))
AbcDefGhi
>>> print(camel("ABC_DEF_GHI", capitalize=True))
AbcDefGhi
>>> print(camel("ABC DEF GHI"))
abcDefGhi
>>> print(camel("ABC_DEF_GHI"))
abcDefGhi
>>> print(camel("AB_CDEfg"))
ABCdEfg
Source code in src/sob/utilities.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 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 |
|
camel_split
camel_split(string: str) -> tuple[str, ...]
Split a string of camelCased words into a tuple.
Examples:
>>> camel_split("theBirdsAndTheBees")
('the', 'Birds', 'And', 'The', 'Bees')
>>> camel_split("theBirdsAndTheBees123")
('the', 'Birds', 'And', 'The', 'Bees', '123')
>>> camel_split("theBirdsAndTheBeesABC123")
('the', 'Birds', 'And', 'The', 'Bees', 'ABC', '123')
>>> camel_split("the-Birds-&-The-Bs-ABC--123")
('the', '-', 'Birds', '-&-', 'The', '-', 'Bs', '-', 'ABC', '--', '123')
>>> camel_split("THEBirdsAndTheBees")
('THE', 'Birds', 'And', 'The', 'Bees')
Source code in src/sob/utilities.py
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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
|
indent
indent(
string: str,
number_of_spaces: int = 4,
start: int = 1,
stop: int | None = None,
) -> str
Indent text by number_of_spaces
starting at line index start
and
stopping at line index stop
.
Source code in src/sob/utilities.py
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 |
|
get_url_relative_to
get_url_relative_to(
absolute_url: str, base_url: str
) -> str
Returns a relative URL given an absolute URL and a base URL
Source code in src/sob/utilities.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
|
split_long_docstring_lines
split_long_docstring_lines(
docstring: str,
max_line_length: int = sob.utilities.MAX_LINE_LENGTH,
) -> str
Split long docstring lines.
Example:
>>> print(
... split_long_docstring_lines(
... " Lorem ipsum dolor sit amet, consectetur adipiscing "
... "elit. Nullam faucibu odio a urna elementum, eu tempor "
... "nisl efficitur."
... )
... )
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam faucibu
odio a urna elementum, eu tempor nisl efficitur.
Source code in src/sob/utilities.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
|
suffix_long_lines
suffix_long_lines(
text: str,
max_line_length: int = sob.utilities.MAX_LINE_LENGTH,
suffix: str = " # noqa: E501",
) -> str
This function adds a suffix to the end of any line of code longer than
max_line_length
.
Parameters:
-
text
(str
) –Text representing python code
-
max_line_length
(int
, default:sob.utilities.MAX_LINE_LENGTH
) –The length at which a line should have the
suffix
appended. If this is a negative integer (or zero), the sum of this integer +MAX_LINE_LENGTH
is used -
suffix
(str
, default:' # noqa: E501'
) –The default suffix indicates to linters that a long line should be permitted
Example:
>>> print(
... suffix_long_lines(
... "A short line...\n"
... "Lorem ipsum dolor sit amet, consectetur adipiscing "
... "elit. Nullam faucibu odio a urna elementum, eu tempor "
... "nisl efficitur.\n"
... "...another short line"
... )
... )
A short line...
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam faucibu odio a urna elementum, eu tempor nisl efficitur.
...another short line
Source code in src/sob/utilities.py
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 |
|
iter_properties_values
iter_properties_values(
object_: object, *, include_private: bool = False
) -> collections.abc.Iterable[tuple[str, typing.Any]]
This function iterates over an object's public (non-callable) properties, yielding a tuple comprised of each attribute/property name and value.
Parameters:
-
object_
(object
) – -
include_private
(bool
, default:False
) –If this is
True
, private properties (those starting with an underscore) will be included in the iteration.
Source code in src/sob/utilities.py
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
|
get_qualified_name
get_qualified_name(
type_or_module: (
type | typing.Callable | types.ModuleType
),
) -> str
This function return the fully qualified name for a type or module.
Examples:
>>> print(get_qualified_name(get_qualified_name))
sob.utilities.get_qualified_name
>>> from sob import model
>>> print(get_qualified_name(model.marshal))
sob.model.marshal
Source code in src/sob/utilities.py
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
|
get_calling_module_name
get_calling_module_name(depth: int = 1) -> str
This function returns the name of the module from which the function which invokes this function was called.
Parameters:
-
depth
(int
, default:1
) –This defaults to
1
, indicating we want to return the name of the module whereinget_calling_module_name
is being called. If set to2
, it would instead indicate the module.
Examples:
>>> print(get_calling_module_name())
sob.utilities
>>> print(get_calling_module_name(2))
doctest
Source code in src/sob/utilities.py
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 |
|
get_calling_function_qualified_name
get_calling_function_qualified_name(
depth: int = 1,
) -> str | None
Return the fully qualified name of the function from within which this is being called
Examples:
>>> def my_function() -> str:
... return get_calling_function_qualified_name()
>>> print(my_function())
sob.utilities.my_function
>>> class MyClass:
... def __call__(self) -> None:
... return self.my_method()
...
... def my_method(self) -> str:
... return get_calling_function_qualified_name()
>>> print(MyClass()())
sob.utilities.MyClass.my_method
Source code in src/sob/utilities.py
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
|
get_source
get_source(
object_: type | typing.Callable | types.ModuleType,
) -> str
Get the source code which defined an object.
Source code in src/sob/utilities.py
766 767 768 769 770 771 772 773 |
|
represent
represent(value: typing.Any) -> str
Returns a string representation of a value, formatted to minimize character width, and utilizing fully qualified class/function names (including module) where applicable.
Parameters:
-
value
(typing.Any
) –
Source code in src/sob/utilities.py
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 |
|
get_method
get_method(
object_instance: object,
method_name: str,
default: (
typing.Callable | sob._types.Undefined | None
) = sob._types.UNDEFINED,
) -> typing.Callable[..., typing.Any] | None
This function attempts to retrieve an object's method, by name, if the
method exists. If the object does not have a method with the given name,
this function returns the defualt
function (if provided), otherwise
None
.
Parameters:
-
object_instance
(object
) – -
method_name
(str
) – -
default
(typing.Callable | sob._types.Undefined | None
, default:sob._types.UNDEFINED
) –
Source code in src/sob/utilities.py
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 |
|