source: trunk/python/utils.py@ 1840

Last change on this file since 1840 was 1824, checked in by Malte Marquarding, 14 years ago

Refactoring of init.py. Moved functionality into separate modules. Some minor fixes to make unit test work under 'standard asap'.

File size: 1.8 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 = "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)
Note: See TracBrowser for help on using the repository browser.