This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Here is my variation on theme 'Create a set of named values with a minimum of fuss' | |
| # Based on idea seen in https://gist.github.com/href/1319371 | |
| # Rudi Farkas 6 Jan 2016 | |
| from collections import namedtuple | |
| def MakeNamedTuple(name='NamedTuple', **kwargs): | |
| """ | |
| Returns a namedtuple instance. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # formatter for arrays of floating point numbers | |
| # Rudi Farkas 8 Aug 2014 | |
| def format_rounded(float_arr, decimals=2, general=False, sep=', '): | |
| """ | |
| Formats elements of float_arr with the stated number of decimal digits, | |
| in the fixed '%f' or general '%g' format. | |
| Returns the element strings joined with the separator. | |
| """ | |
| return sep.join(['{:.{precision}{type}}'.format(x, precision=decimals, type='g' if general else 'f') for x in float_arr]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # test_floating_point_number_re_pattern.py | |
| # Rudi Farkas 19 Nov 2013 | |
| import re | |
| # define a regex pattern that matches valid floating point number strings | |
| floating_point_number_re_pattern = fpn_rpat = "[-+]?\s*(?:\d+(?:\.(?:\d+)?)?|\.\d+)(?:[eE][-+]\d+)?" | |
| if __name__ == '__main__': |
NewerOlder