sob.utilities
get_property_name
get_property_name(string: 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
Source code in sob/utilities.py
36 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 |
|
get_class_name
get_class_name(string: 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 sob/utilities.py
118 119 120 121 122 123 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 |
|
camel
camel(string: str, *, capitalize: bool = False) -> str
This function returns a camelCased representation of the input string. When/if an input string corresponds to a python keyword,
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 sob/utilities.py
164 165 166 167 168 169 170 171 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 |
|
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 sob/utilities.py
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 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
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 sob/utilities.py
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 |
|
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 sob/utilities.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
|
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 sob/utilities.py
457 458 459 460 461 462 463 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 |
|
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. # noqa: E501
...another short line
Source code in sob/utilities.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
|
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 sob/utilities.py
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
|
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 sob/utilities.py
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
|
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 sob/utilities.py
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 |
|
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 sob/utilities.py
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 |
|
get_source
get_source(
object_: type | typing.Callable | types.ModuleType,
) -> str
Get the source code which defined an object.
Source code in sob/utilities.py
727 728 729 730 731 732 733 734 |
|
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 sob/utilities.py
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
|
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 sob/utilities.py
834 835 836 837 838 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 |
|