File: Broken/Core/__init__.py
¶
Broken.Core ¶
flatten ¶
flatten(
*items: Any,
cast: type = list,
block: Optional[Collection] = (None, ""),
unpack: Iterable[type] = (
list,
deque,
tuple,
map,
Generator,
)
) -> Collection[Any]
Flatten/unpack nested iterables (list, deque, tuple, map, Generator) to a plain 1D list - Removes common falsy values by default, modify with block={None, False, "", [], ...}
Example
1 2 3 4 5 |
|
Returns:
-
Collection[Any]
–cast
ed object with allunpack
editems
without any of theblock
ed values
Source code in Broken/Core/__init__.py
34 35 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 |
|
every ¶
every(
*items: Any,
cast: type = list,
block: Collection[Any] = (None, "")
) -> Optional[Collection]
Returns the flattened items if not any element is in the block list, else None. Useful when a Model class has a list of optional arguments that doesn't add falsy values to a command
Usage
1 2 3 |
|
Source code in Broken/Core/__init__.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
shell ¶
shell(
*args: Any,
output: bool = False,
Popen: bool = False,
env: dict[str, str] = None,
confirm: bool = False,
skip: bool = False,
echo: bool = True,
**kwargs
) -> Optional[
Union[
subprocess.CompletedProcess, subprocess.Popen, str
]
]
Enhanced subprocess runners with many additional features. Flattens the args, converts to str
Example
1 |
|
Source code in Broken/Core/__init__.py
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 116 117 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 |
|
apply ¶
apply(
callback: Callable,
iterable: Iterable,
*,
cast: Callable = list
) -> Collection
Applies a callback to all items of an iterable, returning a $cast of the results
Source code in Broken/Core/__init__.py
152 153 154 155 156 157 158 |
|
denum ¶
denum(item: Union[enum.Enum, Any]) -> Any
De-enumerates an item: if it's an Enum, returns the value, else the item itself
Source code in Broken/Core/__init__.py
161 162 163 |
|
pop_fill ¶
pop_fill(
data: Collection, fill: type, length: int
) -> Collection
Pop or fill until a data's length is met
Source code in Broken/Core/__init__.py
166 167 168 169 170 171 172 |
|
multi_context ¶
multi_context(
*contexts: contextlib.AbstractContextManager,
) -> Generator
Enter multiple contexts at once
Source code in Broken/Core/__init__.py
175 176 177 178 179 180 181 |
|
tempvars ¶
tempvars(**variables: str) -> Generator
Temporarily sets environment variables inside a context
Source code in Broken/Core/__init__.py
184 185 186 187 188 189 190 191 192 193 194 195 |
|
block_modules ¶
block_modules(*modules: str) -> Generator
Pretend a module isn't installed
Source code in Broken/Core/__init__.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
smartproxy ¶
smartproxy(object: Any) -> Any
Returns a weakref proxy if the object is not already proxied
Source code in Broken/Core/__init__.py
214 215 216 217 218 219 220 221 |
|
clamp ¶
clamp(
value: float, low: float = 0, high: float = 1
) -> float
Source code in Broken/Core/__init__.py
224 225 |
|
nearest ¶
nearest(
number: Number,
multiple: Number,
*,
cast=int,
operator: Callable = round
) -> Number
Finds the nearest multiple of a base number, by default ints but works for floats too
Source code in Broken/Core/__init__.py
228 229 230 |
|
list_get ¶
list_get(
data: list, index: int, default: Any = None
) -> Optional[Any]
Returns the item at 'index' or 'default' if out of range
Source code in Broken/Core/__init__.py
233 234 235 236 237 |
|
hyphen_range ¶
hyphen_range(
string: Optional[str], *, inclusive: bool = True
) -> Iterable[int]
Yields the numbers in a hyphenated CSV range, just like when selecting what pages to print - Accepts any of ("-", "..", "...", "_", "->") as a hyphenated range - Special values: - "all", returns infinite range from 0 - "even", returns even numbers - "odd", returns odd numbers
Example
1 2 3 |
|
Source code in Broken/Core/__init__.py
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 |
|
limited_ratio ¶
limited_ratio(
number: Optional[float], *, limit: float = None
) -> Optional[tuple[int, int]]
Same as Number.as_integer_ratio but with an optional upper limit and optional return
Source code in Broken/Core/__init__.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
overrides ¶
overrides(
old: Any,
new: Optional[Any],
default: Optional[Any] = None,
resets: Any = -1,
) -> Optional[Any]
Returns 'new' if is not None, else keeps 'old' value
Source code in Broken/Core/__init__.py
292 293 294 295 296 297 298 299 300 301 302 303 |
|
install ¶
install(
*,
packages: Union[str, Iterable[str]],
pypi: Optional[Union[str, Iterable[str]]] = None,
args: Optional[Union[str, Iterable[str]]] = None
) -> None
Source code in Broken/Core/__init__.py
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 |
|
combinations ¶
combinations(**options: Any) -> Iterable[DotMap]
Returns a dictionary of key='this' of itertools.product
Source code in Broken/Core/__init__.py
338 339 340 341 |
|
arguments ¶
arguments() -> bool
Returns True if any arguments are present on sys.argv
Source code in Broken/Core/__init__.py
344 345 346 |
|
easyloop ¶
easyloop(method: Callable = None, *, period: float = 0.0)
Wraps a method in an infinite loop called every 'period' seconds
Source code in Broken/Core/__init__.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
Nothing ¶
No-operation faster than Mock - A class that does nothing
Source code in Broken/Core/__init__.py
371 372 373 374 375 376 377 378 |
|
__nop__ ¶
__nop__(*args, **kwargs) -> Self
Source code in Broken/Core/__init__.py
373 374 |
|
__call__ ¶
__call__(*args, **kwargs) -> Self
Source code in Broken/Core/__init__.py
375 376 |
|
__getattr__ ¶
__getattr__(_) -> Callable
Source code in Broken/Core/__init__.py
377 378 |
|
StaticClass ¶
A class that can't be instantiated directl, only used for static methods (namespace)
Source code in Broken/Core/__init__.py
381 382 383 384 385 |
|
__new__ ¶
__new__(*args, **kwargs)
Source code in Broken/Core/__init__.py
384 385 |
|
BrokenSingleton ¶
Bases: ABC
Source code in Broken/Core/__init__.py
388 389 390 391 392 393 |
|
__new__ ¶
__new__(*args, **kwargs)
Source code in Broken/Core/__init__.py
389 390 391 392 393 |
|
BrokenFluent ¶
Fluent-like .copy(update) and .(update) setter for classes
Source code in Broken/Core/__init__.py
396 397 398 399 400 401 402 403 404 405 406 407 |
|
__call__ ¶
__call__(**update) -> Self
Updates the instance with the provided kwargs
Source code in Broken/Core/__init__.py
399 400 401 402 403 |
|
copy ¶
copy(**update) -> Self
Returns an updated copy of this instance
Source code in Broken/Core/__init__.py
405 406 407 |
|
BrokenAttrs ¶
Walk over an @attrs.defined class and call post on all classes in the MRO Warn: Must NOT define attrs_post_init in an inheriting class Fixme: Can improve by starting on BrokenAttrs itself
Source code in Broken/Core/__init__.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
|
__attrs_post_init__ ¶
__attrs_post_init__()
Source code in Broken/Core/__init__.py
416 417 418 419 |
|
__post__ ¶
__post__() -> None
Source code in Broken/Core/__init__.py
421 422 423 |
|
BrokenModel ¶
Bases: BaseModel
Pydantic model utilities
Source code in Broken/Core/__init__.py
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 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 |
|
model_post_init ¶
model_post_init(__context)
Source code in Broken/Core/__init__.py
432 433 434 435 |
|
__post__ ¶
__post__() -> None
Source code in Broken/Core/__init__.py
437 438 |
|
__hash__ ¶
__hash__() -> int
Deterministic hash heuristic, as hash() is random seeded
Source code in Broken/Core/__init__.py
440 441 442 |
|
json ¶
json(full: bool = True) -> str
Source code in Broken/Core/__init__.py
446 447 448 449 450 |
|
dict ¶
dict(full: bool = True) -> dict
Source code in Broken/Core/__init__.py
452 453 454 455 456 |
|
schema ¶
schema() -> dict
Source code in Broken/Core/__init__.py
458 459 |
|
load ¶
load(data: Union[dict, str]) -> Self
Source code in Broken/Core/__init__.py
463 464 465 466 467 468 469 470 471 |
|
update ¶
update(**data: Union[dict, str]) -> Self
Source code in Broken/Core/__init__.py
473 474 475 476 |
|
keys ¶
keys() -> Iterable[str]
Source code in Broken/Core/__init__.py
480 481 |
|
values ¶
values() -> Iterable[Any]
Source code in Broken/Core/__init__.py
483 484 |
|
items ¶
items() -> Iterable[tuple[str, Any]]
Source code in Broken/Core/__init__.py
486 487 |
|
reset ¶
reset() -> None
Source code in Broken/Core/__init__.py
491 492 493 |
|
FrozenHash ¶
Bases: BaseModel
Source code in Broken/Core/__init__.py
496 497 498 499 500 501 |
|
BrokenAttribute ¶
Bases: StaticClass
Recursive implementation for getattr and setattr from strings
Source code in Broken/Core/__init__.py
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 545 |
|
Parts ¶
decompose ¶
decompose(key: str) -> Parts
Source code in Broken/Core/__init__.py
513 514 515 516 517 518 519 520 521 |
|
get ¶
get(root: object, key: str) -> Optional[Any]
Source code in Broken/Core/__init__.py
523 524 525 526 527 528 529 530 531 532 533 |
|
set ¶
set(object: object, attribute: str, value: Any) -> None
Source code in Broken/Core/__init__.py
535 536 537 538 539 540 541 542 543 544 545 |
|
StringUtils ¶
Bases: StaticClass
Source code in Broken/Core/__init__.py
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
|
border ¶
border(string: str, border: str) -> bool
Returns True if 'border' is both a prefix and suffix of 'string'
Source code in Broken/Core/__init__.py
550 551 552 553 |
|
dunder ¶
dunder(name: str) -> bool
Checks if a string is a double underscore 'name'
Source code in Broken/Core/__init__.py
555 556 557 558 |
|
sunder ¶
sunder(name: str) -> bool
Checks if a string is a single underscore 'name'
Source code in Broken/Core/__init__.py
560 561 562 563 |
|
private ¶
private(name: str) -> bool
Checks if a string is a private name
Source code in Broken/Core/__init__.py
565 566 567 568 |
|
DictUtils ¶
Bases: StaticClass
Source code in Broken/Core/__init__.py
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
|
filter_dict ¶
filter_dict(
data: dict[str, Any],
*,
block: Optional[Collection] = None,
allow: Optional[Collection] = None
) -> dict[str, Any]
Filters a dictionary by removing 'block' or only allowing 'allow' keys
Source code in Broken/Core/__init__.py
573 574 575 576 577 578 579 580 581 582 583 584 |
|
ritems ¶
ritems(data: dict[str, Any]) -> Iterable[tuple[str, Any]]
Recursively yields all items from a dictionary
Source code in Broken/Core/__init__.py
586 587 588 589 590 591 592 593 |
|
rvalues ¶
rvalues(data: dict[str, Any]) -> Iterable[Any]
Recursively yields all values from a dictionary
Source code in Broken/Core/__init__.py
595 596 597 598 599 |
|
selfless ¶
selfless(data: dict) -> dict
Removes the 'self' key from a dictionary (useful for locals() or dict)
Source code in Broken/Core/__init__.py
601 602 603 604 605 |
|
BrokenCache ¶
Bases: StaticClass
Source code in Broken/Core/__init__.py
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 |
|
lru ¶
lru(*args, **kwargs) -> Callable
Smarter lru_cache consistent with multi-calls
Source code in Broken/Core/__init__.py
610 611 612 613 614 615 |
|
requests ¶
requests(*args, patch: bool = False, **kwargs)
Source code in Broken/Core/__init__.py
617 618 619 620 621 622 623 624 |
|
BrokenRelay ¶
A utility class for sharing one-to-many callbacks in a 'observer' pattern style. Multiple callabacks can be subscribed to receive the same args and kwargs when an instance of this class is called. Useful cases are to avoid inheritance when sharing callbacks.
Example
1 2 3 4 5 6 7 8 9 10 |
|
Source code in Broken/Core/__init__.py
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 |
|
subscribe ¶
subscribe(*methods: Iterable[Callable]) -> Self
Adds callbacks to be called with same arguments as self.call
Source code in Broken/Core/__init__.py
650 651 652 653 |
|
__call__ ¶
__call__(*args, **kwargs)
Source code in Broken/Core/__init__.py
655 656 657 |
|
Patch ¶
Source code in Broken/Core/__init__.py
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
|
__attrs_post_init__ ¶
__attrs_post_init__()
Source code in Broken/Core/__init__.py
666 667 |
|
apply ¶
apply()
Source code in Broken/Core/__init__.py
669 670 671 672 673 |
|
revert ¶
revert()
Source code in Broken/Core/__init__.py
675 676 |
|
__enter__ ¶
__enter__()
Source code in Broken/Core/__init__.py
678 679 |
|
__exit__ ¶
__exit__(*args)
Source code in Broken/Core/__init__.py
680 681 |
|
BrokenWatchdog ¶
Bases: ABC
Source code in Broken/Core/__init__.py
684 685 686 687 688 689 690 691 692 693 694 |
|
__changed__ ¶
__changed__(key, value) -> None
Called when a property changes
Source code in Broken/Core/__init__.py
686 687 688 689 |
|
__setattr__ ¶
__setattr__(key, value)
Calls changed when a property changes
Source code in Broken/Core/__init__.py
691 692 693 694 |
|
ThreadedStdin ¶
Source code in Broken/Core/__init__.py
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
|
__attrs_post_init__ ¶
__attrs_post_init__()
Source code in Broken/Core/__init__.py
703 704 705 |
|
write ¶
write(data)
Source code in Broken/Core/__init__.py
706 707 |
|
worker ¶
worker()
Source code in Broken/Core/__init__.py
708 709 710 711 |
|
close ¶
close()
Source code in Broken/Core/__init__.py
712 713 714 715 716 717 |
|
transcends ¶
transcends(method, base, generator: bool = False)
Are you tired of managing and calling super().
We have just the right solution for you!
Introducing transcends, the decorator that crosses your class's MRO and calls the method with the same name as the one you are decorating. It's an automatic super() everywhere!
Source code in Broken/Core/__init__.py
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
|
LazyImport ¶
Source code in Broken/Core/__init__.py
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
|
__init__ ¶
__init__(_name: str = None)
Source code in Broken/Core/__init__.py
756 757 |
|
__load__ ¶
__load__() -> Any
Source code in Broken/Core/__init__.py
759 760 761 762 763 764 765 766 767 |
|
__getattr__ ¶
__getattr__(name) -> Any
Source code in Broken/Core/__init__.py
769 770 |
|
__str__ ¶
__str__() -> str
Source code in Broken/Core/__init__.py
772 773 |
|
__enter__ ¶
__enter__()
Source code in Broken/Core/__init__.py
775 776 777 778 779 780 781 782 783 |
|
__exit__ ¶
__exit__(*args)
Source code in Broken/Core/__init__.py
785 786 |
|