Package kenozooid :: Module util

Source Code for Module kenozooid.util

 1  # 
 2  # Kenozooid - dive planning and analysis toolbox. 
 3  # 
 4  # Copyright (C) 2009-2019 by Artur Wroblewski <wrobell@riseup.net> 
 5  # 
 6  # This program is free software: you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation, either version 3 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
18  # 
19   
20  """ 
21  Kenozooid utility funtions. 
22  """ 
23   
24  import math 
25  import operator 
26  import string 
27   
28  from cytoolz.curried import accumulate 
29   
30  # format for dive time presentation 
31  FMT_DIVETIME = '%Y-%m-%d %H:%M' 
32   
33 -def min2str(t):
34 """ 35 Convert decimal minutes (i.e. 38.84) into MM:SS string (i.e. 38:50). 36 37 >>> min2str(38.84) 38 '38:50' 39 40 >>> min2str(67.20) 41 '67:12' 42 """ 43 return '%02d:%02d' % (int(t), math.modf(t)[0] * 60)
44 45
46 -class _Formatter(string.Formatter):
47 """ 48 Formatter, which formats 'None' as empty string. 49 """
50 - def format_field(self, value, fs):
51 """ 52 Format null value as empty string. If value is not null, then 53 render it using default method of the formatter. 54 """ 55 if value is None: 56 return '' 57 else: 58 return super(_Formatter, self).format_field(value, fs)
59 60 61 nformat = _Formatter().format 62 63
64 -def pipe(data, *gens):
65 """ 66 Pipe data through list of geneators. 67 68 :Parameters: 69 data 70 Data to pipe through the generators. 71 gens 72 List of generators to process the data. 73 """ 74 for g in gens: 75 data = g(data) 76 return data
77 78 79 # Return empty iterator if ``it`` is None, otherwise return ``it`` 80 # itself. 81 nit = lambda it: () if it is None else it 82 83 cumsum = accumulate(operator.add) 84 85 # vim: sw=4:et:ai 86