Initial port to python3
[htsworkflow.git] / htsworkflow / util / conversion.py
1 """
2 Miscellaneous, more refined type casting functions
3 """
4
5 def unicode_or_none(value):
6     """
7     Convert value to unicode if its not none.
8     """
9     if value is None:
10         return None
11     else:
12         return str(value)
13
14 def parse_flowcell_id(flowcell_id):
15     """
16     Return flowcell id and any status encoded in the id
17
18     We stored the status information in the flowcell id name.
19     this was dumb, but database schemas are hard to update.
20     """
21     fields = flowcell_id.split()
22     fcid = None
23     status = None
24     if len(fields) > 0:
25         fcid = fields[0]
26     if len(fields) > 1:
27         status = fields[1]
28     return fcid, status
29
30 def parse_slice(slice_text):
31     if slice_text is None or len(slice_text) == 0:
32         return slice(None)
33
34     slice_data = []
35     for element in slice_text.split(':'):
36         if len(element) == 0:
37             element = None
38         else:
39             element = int(element)
40         slice_data.append(element)
41
42     return slice(*slice_data)
43
44