174790119166b86a87963777d15607039ecd6277
[calibcant.git] / calibcant / common.py
1 # calibcant - tools for thermally calibrating AFM cantilevers
2 #
3 # Copyright (C) 2008-2010 W. Trevor King <wking@drexel.edu>
4 #
5 # This file is part of CalibCant.
6 #
7 # CalibCant is free software: you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation, either
10 # version 3 of the License, or (at your option) any later version.
11 #
12 # CalibCant is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with CalibCant.  If not, see
19 # <http://www.gnu.org/licenses/>.
20
21 import .config
22
23
24 VERSION="0.4"
25
26 # handle extra verbose input modules, only imported if we need them
27 _flush_plot = None
28 _final_flush_plot = None
29 _pylab = None
30 def _dummy_fn(): pass
31
32 def _import_pylab() :  # TODO: auto detect no DISPLAY and abort
33     """Import pylab plotting functions for when we need to plot.  This
34     function can be called multiple times, and ensures that the pylab
35     setup is only imported once.  It defines the functions
36       _flush_plot() to be called after incremental changes,
37       _final_flush_plot(), to be called at the end of any graphical processing, and
38       _pylab, a reference to the pylab module."""
39     global _pylab
40     global _flush_plot
41     global _final_flush_plot
42     if _pylab == None :
43         import pylab as _pylab
44     if _flush_plot == None :
45         if config.PYLAB_INTERACTIVE :
46             _flush_plot = _pylab.draw
47         else :
48             _flush_plot = _pylab.show #_dummy_fn
49     if _final_flush_plot == None :
50         if config.PYLAB_INTERACTIVE :
51             _final_flush_plot = _pylab.draw
52         else :
53             _final_flush_plot = _dummy_fn # _pylab.show
54
55 def write_array(ofile, array, seperator):
56     """Erite an iterable array seperated by seperator.
57     Terminate the output with an endline."""
58     strings = [repr(x) for x in array]
59     ofile.write(seperator.join(strings))
60     ofile.write('\n')