bsutils module¶
Contains various utilities programs:
printargs
: a decorator that reports the arguments of the function it decoratesbs_name_func
: returns the name of the current function or its callersbs_error_abort
: reports on error and aborts executionfinal_s
: whether a word should have a final 's'bs_switch
: an improvedswitch
statementfind_first
: returns the index of and the first item in an iterable that satisfies a conditionprint_stars
: prints a title within lines of starsfile_print_stars
: does the same, to a filefstring_integer_with_significant_digits
: rounds an integer and returns an f-stringmkdir_if_needed
: creates a directory if it does not existbscomb
: a combination \({n \choose k}\) operatorbslog, bsxlogx, bsexp
: \(C^2\) extensions of \(\log(x), x\log x, \exp(x)\), and their first two derivativesbs_projection_point
: projects a point on a line in the plane.
Note
if the math looks strange in the documentation, just reload the page.
bs_error_abort(msg='error, aborting')
¶
report error and abort
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str
|
a message |
'error, aborting'
|
Returns:
Type | Description |
---|---|
None
|
prints the message and exits with code 1 |
Source code in bs_python_utils/bsutils.py
67 68 69 70 71 72 73 74 75 76 77 78 |
|
bs_name_func(back=2)
¶
get the name of the current function, or further back in stack
Parameters:
Name | Type | Description | Default |
---|---|---|---|
back |
int
|
2 is current function, 3 the function that called it etc |
2
|
Returns:
Type | Description |
---|---|
str
|
the name of the function |
Source code in bs_python_utils/bsutils.py
52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
bs_projection_point(x, y, a, b, c)
¶
projection of point (x,y) on line ax+by+c=0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
float
|
y: coordinates |
required |
a |
float
|
b: c: line parameters (as in ax+by+c=0) |
required |
Returns:
Name | Type | Description |
---|---|---|
x_proj |
float
|
y_proj: coordinates of projection point |
dist |
float
|
distance of point from line |
Source code in bs_python_utils/bsutils.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
|
bs_switch(match, dico, strict=True, default='no match')
¶
a switch statement that allows for partial matches if strict is False
Parameters:
Name | Type | Description | Default |
---|---|---|---|
match |
str
|
what we look for in the keys |
required |
dico |
dict
|
a dictionary with string keys |
required |
strict |
bool
|
if |
True
|
default |
Any
|
what we return if no match is found |
'no match'
|
Returns:
Type | Description |
---|---|
Any
|
the value for the match, or |
Examples:
>>> calc_dict = {
"plus": lambda x, y: x + y,
"minus": lambda x, y: x - y
}
>>> plus = bs_switch('plus', calc_dict, default="unintended function")
>>> minus = bs_switch('min', calc_dict, strict=False, default="unintended function")
>>> plus(6, 4)
10
>>> minus(6, 4)
2
>>> bs_switch('plu', calc_dict)
"no match"
Source code in bs_python_utils/bsutils.py
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 164 165 |
|
bscomb(n, k)
¶
number of combinations of k among n {n \choose k}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
|
required |
k |
int
|
should be smaller than n |
required |
Returns:
Type | Description |
---|---|
int
|
|
Source code in bs_python_utils/bsutils.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
bsexp(x, bigx=50.0, lowx=-50.0, deriv=0)
¶
C^2
-extends the exponential above bigx
and below lowx
perhaps with derivatives
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
float
|
argument |
required |
bigx |
float
|
upper bound |
50.0
|
lowx |
float
|
lower bound |
-50.0
|
deriv |
int
|
if 1, also return first derivative; if 2, the first two derivatives |
0
|
Returns:
Type | Description |
---|---|
float | TwoFloats | ThreeFloats
|
the exponential |
float | TwoFloats | ThreeFloats
|
perhaps with derivatives |
Source code in bs_python_utils/bsutils.py
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 |
|
bslog(x, eps=1e-30, deriv=0)
¶
extends the logarithm below eps
by taking a second-order approximation
perhaps with derivatives
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
float
|
argument |
required |
eps |
float
|
lower bound |
1e-30
|
deriv |
int
|
if 1, also return first derivative; if 2, the first two derivatives |
0
|
Returns:
Type | Description |
---|---|
float | TwoFloats | ThreeFloats
|
|
Source code in bs_python_utils/bsutils.py
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 |
|
bsxlogx(x, eps=1e-30, deriv=0)
¶
extends x \ln(x)
below eps
by making it go to zero in a C^2
extension
perhaps with derivatives
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
float
|
argument |
required |
eps |
float
|
lower bound |
1e-30
|
deriv |
int
|
if 1, also return first derivative; if 2, the first two derivatives |
0
|
Returns:
Type | Description |
---|---|
float | TwoFloats | ThreeFloats
|
|
Source code in bs_python_utils/bsutils.py
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 |
|
file_print_stars(file_handle, title=None, n=70)
¶
prints to a file a title within stars
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_handle |
TextIOBase
|
file handle |
required |
title |
str | None
|
title |
None
|
n |
int
|
length of line |
70
|
Returns:
Type | Description |
---|---|
None
|
prints a starred line to the file, or two around the title |
Source code in bs_python_utils/bsutils.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
final_s(n, word)
¶
pluralizes word if n > 1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
how many times |
required |
word |
str
|
to be pluralized, maybe |
required |
Returns:
Type | Description |
---|---|
str
|
|
Source code in bs_python_utils/bsutils.py
81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
find_first(iterable, condition=lambda x: True)
¶
Returns the index of and the first item in the iterable
that
satisfies the condition
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterable |
Iterable
|
where to look |
required |
condition |
Callable
|
must return a boolean |
lambda x: True
|
Returns:
Type | Description |
---|---|
Any
|
If the condition is not given, returns 0 and the first item of |
Any
|
the iterable. |
Any
|
Raises |
Examples:
>>> find_first( (1,2,3), condition=lambda x: x % 2 == 0)
(1, 2)
>>> find_first(range(3, 100))
(0, 3)
>>> find_first( () )
Traceback (most recent call last):
...
StopIteration
Source code in bs_python_utils/bsutils.py
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 |
|
fstring_integer_with_significant_digits(number, m)
¶
returns an f-string with number
rounded to m
significant digits
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number |
int
|
the integer we want to round |
required |
m |
int
|
how many digits we keep; the rest is filled with zeroes |
required |
Return
a string with the rounded integer
Examples:
fstring_integer_with_significant_digits(12345, 0) ' 0' fstring_integer_with_significant_digits(12345, 3) '12,300' fstring_integer_with_significant_digits(12345, 7) '12345'
Source code in bs_python_utils/bsutils.py
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 |
|
mkdir_if_needed(p)
¶
create the directory if it does not exist
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p |
Path | str
|
a path |
required |
Returns:
Type | Description |
---|---|
Path
|
the directory Path |
Source code in bs_python_utils/bsutils.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
print_stars(title=None, n=70)
¶
prints a title within stars
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str | None
|
title |
None
|
n |
int
|
number of stars on line |
70
|
Returns:
Type | Description |
---|---|
None
|
prints a starred line, or two around the title |
Source code in bs_python_utils/bsutils.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
printargs(func)
¶
Decorator that reports the arguments of the function
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable
|
the decorated function |
required |
Source code in bs_python_utils/bsutils.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|