Mercurial > repos > biotechcoder > riboseqr_wrapper
comparison riboseqr/utils.py @ 0:e01de823e919 draft default tip
Uploaded
author | biotechcoder |
---|---|
date | Fri, 01 May 2015 05:41:51 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e01de823e919 |
---|---|
1 """Common functions""" | |
2 | |
3 | |
4 def process_args(args, ret_type='str', ret_mode=None): | |
5 """Split arguments (only strings) on comma, return in requested | |
6 | |
7 ret_type | |
8 (str, int or bool) | |
9 | |
10 as | |
11 | |
12 ret_mode | |
13 char vector - c(1,2,3) | |
14 list vector - list(1,2,3) | |
15 list - as list [1,2,3] | |
16 None - same as input | |
17 | |
18 """ | |
19 all_args = [item.strip() for item in args.split(',')] | |
20 num_options = len(all_args) | |
21 if num_options == 1 and len(all_args[0]): | |
22 if ret_type == 'int': | |
23 option = int(all_args[0]) | |
24 if ret_mode == 'charvector': | |
25 # if option: | |
26 return 'c({})'.format(option) | |
27 # else: | |
28 # return 'c()' | |
29 elif ret_mode == 'listvector': | |
30 # if option: | |
31 return 'list({})'.format(option) | |
32 # else: | |
33 # return 'list()' | |
34 elif ret_mode == 'list': | |
35 # if option: | |
36 return [option] | |
37 # else: | |
38 # return [] | |
39 else: | |
40 return option | |
41 else: | |
42 # str, bool | |
43 option = all_args[0] | |
44 if ret_mode == 'charvector': | |
45 # if len(option): | |
46 return 'c("{}")'.format(option) | |
47 # else: | |
48 # return 'c("")' | |
49 elif ret_mode == 'listvector': | |
50 # if option: | |
51 return 'list("{}")'.format(option) | |
52 # else: | |
53 # return 'list("")' | |
54 elif ret_mode == 'list': | |
55 # if option: | |
56 return [option] | |
57 # else: | |
58 # return [] | |
59 else: | |
60 return option | |
61 elif num_options > 1: | |
62 if ret_type == 'int': | |
63 options = tuple([int(item) for item in all_args]) | |
64 if ret_mode == 'charvector': | |
65 # if len(options): | |
66 return 'c{}'.format(options) | |
67 # else: | |
68 # return 'c()' | |
69 elif ret_mode == 'listvector': | |
70 # if len(options): | |
71 return 'list{}'.format(options) | |
72 # else: | |
73 # return 'list()' | |
74 elif ret_mode == 'list': | |
75 return list(options) | |
76 else: | |
77 options = tuple(all_args) | |
78 if ret_mode == 'charvector': | |
79 # if len(options): | |
80 return 'c{}'.format(options) | |
81 elif ret_mode == 'listvector': | |
82 return 'list{}'.format(options) | |
83 elif ret_mode == 'list': | |
84 # if len(all_args): | |
85 return all_args | |
86 # else: | |
87 # return [] | |
88 else: | |
89 # as original with spaces stripped | |
90 return ','.join(all_args) | |
91 | |
92 |