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