source: branches/casa-release-4_3/python/utils.py@ 3150

Last change on this file since 3150 was 2456, checked in by Malte Marquarding, 13 years ago

added helper function to toggle rcParamsverbose

File size: 2.4 KB
Line 
1import os
2
3def mask_and(a, b):
4 assert(len(a)==len(b))
5 return [ a[i] & b[i] for i in xrange(len(a)) ]
6
7def mask_or(a, b):
8 assert(len(a)==len(b))
9 return [ a[i] | b[i] for i in xrange(len(a)) ]
10
11def mask_not(a):
12 return [ not i for i in a ]
13
14def _n_bools(n, val):
15 return [ val for i in xrange(n) ]
16
17def _is_sequence_or_number(param, ptype=int):
18 if isinstance(param,tuple) or isinstance(param,list):
19 if len(param) == 0: return True # empty list
20 out = True
21 for p in param:
22 out &= isinstance(p,ptype)
23 return out
24 elif isinstance(param, ptype):
25 return True
26 return False
27
28def _to_list(param, ptype=int):
29 if isinstance(param, ptype):
30 if ptype is str: return param.split()
31 else: return [param]
32 if _is_sequence_or_number(param, ptype):
33 return param
34 return None
35
36def unique(x):
37 """
38 Return the unique values in a list
39 Parameters:
40 x: the list to reduce
41 Examples:
42 x = [1,2,3,3,4]
43 print unique(x)
44 [1,2,3,4]
45 """
46 return dict([ (val, 1) for val in x]).keys()
47
48def list_files(path=".",suffix="rpf"):
49 """
50 Return a list files readable by asap, such as rpf, sdfits, mbf, asap
51 Parameters:
52 path: The directory to list (default '.')
53 suffix: The file extension (default rpf)
54 Example:
55 files = list_files("data/","sdfits")
56 print files
57 ['data/2001-09-01_0332_P363.sdfits',
58 'data/2003-04-04_131152_t0002.sdfits',
59 'data/Sgr_86p262_best_SPC.sdfits']
60 """
61 if not os.path.isdir(path):
62 return None
63 valid = "ms rpf rpf.1 rpf.2 sdf sdfits mbf asap".split()
64 if not suffix in valid:
65 return None
66 files = [os.path.expanduser(os.path.expandvars(path+"/"+f)) for f in os.listdir(path)]
67 return filter(lambda x: x.endswith(suffix),files)
68
69def page(message):
70 """Run the input message through a pager. This is only done if
71 ``rcParams["verbose"]`` is set.
72 """
73 verbose = False
74 try:
75 from asap.parameters import rcParams
76 verbose = rcParams['verbose']
77 except:
78 pass
79 if verbose:
80 try:
81 from IPython.genutils import page as pager
82 except ImportError:
83 from pydoc import pager
84 pager(message)
85 return None
86 else:
87 return message
88
89def toggle_verbose():
90 from asap import rcParams
91 rcParams['verbose'] = not bool(rcParams['verbose'])
Note: See TracBrowser for help on using the repository browser.