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 improvedswitchstatementfind_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
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
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:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Source code in bs_python_utils/bsutils.py
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 | |
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
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
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
355 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 381 382 383 384 385 386 387 | |
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
268 269 270 271 272 273 274 275 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 | |
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
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 | |
file_print_stars(file_handle, title=None, n=70)
¶
Write the print_stars output to a file-like object.
Source code in bs_python_utils/bsutils.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
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 | |
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:
1 2 3 4 5 6 7 8 | |
Source code in bs_python_utils/bsutils.py
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 | |
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
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 | |
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
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
print_stars(title=None, n=70)
¶
Print a horizontal bar of * characters, optionally framing a title.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str | None
|
Optional text centred between two star lines. |
None
|
n
|
int
|
Length of the star line. |
70
|
Source code in bs_python_utils/bsutils.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
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 | |