pyutils
Helper functions for python.
Examples:
Attributes
Classes
DefaultOrderedDict(default_factory=None, *a, **kw)
Bases: OrderedDict
Ordered dictionary with default type.
This is similar to defaultdict and maintains the order of items added to it so in that regards it functions similar to OrderedDict.
Examples:
from pyrevit.coreutils import pyutils
od = pyutils.DefaultOrderedDict(list)
od['A'] = [1, 2, 3]
od['B'] = [4, 5, 6]
od['C'].extend([7, 8, 9])
for k, v in od.items():
print(k, v)
Source code in pyrevitlib/pyrevit/coreutils/pyutils.py
Functions
pairwise(iterable, step=2)
Iterate through items in pairs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterable |
iterable
|
any iterable object |
required |
step |
int
|
number of steps to move when making pairs |
2
|
Returns:
Type | Description |
---|---|
Iterable[Any]
|
list of pairs |
Examples:
[(1, 2), (3, 4)] # 5 can not be paired [(1, 2), (3, 4), (5, 6)] [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]Source code in pyrevitlib/pyrevit/coreutils/pyutils.py
safe_cast(val, to_type, default=None)
Convert value to type gracefully.
This method basically calls to_type(value) and returns the default if exception occurs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
any
|
value to be converted |
required |
to_type |
type
|
target type |
required |
default |
any
|
value to rerun on conversion exception |
None
|
Examples:
0Source code in pyrevitlib/pyrevit/coreutils/pyutils.py
isnumber(token)
Verify if given string token is int or float.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token |
str
|
string value |
required |
Returns:
Type | Description |
---|---|
bool
|
True of token is int or float |
Examples:
TrueSource code in pyrevitlib/pyrevit/coreutils/pyutils.py
compare_lists(x, y)
Compare two lists.
See: https://stackoverflow.com/a/10872313/2350244
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
list
|
first list |
required |
y |
list
|
second list |
required |
merge(d1, d2)
Merge d2 into d1.
d2 dict values are recursively merged into d1 dict values other d2 values are added to d1 dict values with the same key new d2 values are added to d1 d2 values override other d1 values
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d1 |
dict
|
dict to be updated |
required |
d2 |
dict
|
dict to be merge into d1 |
required |
Returns:
Type | Description |
---|---|
dict[Any, Any]
|
updated d1 |
Examples:
d1 = {1: 1, 2: "B" , 3: {1:"A", 2:"B"}, 4: "b" , 5: ["a", "b"]}
d2 = {1: 1, 2: {1:"A"}, 3: {1:"S", 3:"C"}, 4: ["a"], 5: ["c"]}
merge(d1, d2)
Source code in pyrevitlib/pyrevit/coreutils/pyutils.py
almost_equal(a, b, rnd=5)
Check if two numerical values almost equal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a |
float
|
value a |
required |
b |
float
|
value b |
required |
rnd |
int
|
n digits after comma. Defaults to 5. |
5
|
Returns:
Type | Description |
---|---|
bool
|
True if almost equal |