Mercurial > repos > vimalkumarvelayudhan > riboseqr_wrapper
comparison riboseqr/utils.py @ 0:c34c364ce75d
First commit
| author | Vimalkumar Velayudhan <vimal@biotechcoder.com> |
|---|---|
| date | Tue, 21 Jul 2015 14:48:39 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:c34c364ce75d |
|---|---|
| 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 if args: | |
| 20 all_args = [item.strip() for item in args.split(',')] | |
| 21 else: | |
| 22 all_args = [] | |
| 23 num_options = len(all_args) | |
| 24 if num_options == 1 and len(all_args[0]): | |
| 25 if ret_type == 'int': | |
| 26 option = int(all_args[0]) | |
| 27 if ret_mode == 'charvector': | |
| 28 # if option: | |
| 29 return 'c({})'.format(option) | |
| 30 # else: | |
| 31 # return 'c()' | |
| 32 elif ret_mode == 'listvector': | |
| 33 # if option: | |
| 34 return 'list({})'.format(option) | |
| 35 # else: | |
| 36 # return 'list()' | |
| 37 elif ret_mode == 'list': | |
| 38 # if option: | |
| 39 return [option] | |
| 40 # else: | |
| 41 # return [] | |
| 42 else: | |
| 43 return option | |
| 44 else: | |
| 45 # str, bool | |
| 46 option = all_args[0] | |
| 47 if ret_mode == 'charvector': | |
| 48 # if len(option): | |
| 49 return 'c("{}")'.format(option) | |
| 50 # else: | |
| 51 # return 'c("")' | |
| 52 elif ret_mode == 'listvector': | |
| 53 # if option: | |
| 54 return 'list("{}")'.format(option) | |
| 55 # else: | |
| 56 # return 'list("")' | |
| 57 elif ret_mode == 'list': | |
| 58 # if option: | |
| 59 return [option] | |
| 60 # else: | |
| 61 # return [] | |
| 62 else: | |
| 63 return option | |
| 64 elif num_options > 1: | |
| 65 if ret_type == 'int': | |
| 66 options = tuple([int(item) for item in all_args]) | |
| 67 if ret_mode == 'charvector': | |
| 68 # if len(options): | |
| 69 return 'c{}'.format(options) | |
| 70 # else: | |
| 71 # return 'c()' | |
| 72 elif ret_mode == 'listvector': | |
| 73 # if len(options): | |
| 74 return 'list{}'.format(options) | |
| 75 # else: | |
| 76 # return 'list()' | |
| 77 elif ret_mode == 'list': | |
| 78 return list(options) | |
| 79 else: | |
| 80 options = tuple(all_args) | |
| 81 if ret_mode == 'charvector': | |
| 82 # if len(options): | |
| 83 return 'c{}'.format(options) | |
| 84 elif ret_mode == 'listvector': | |
| 85 return 'list{}'.format(options) | |
| 86 elif ret_mode == 'list': | |
| 87 # if len(all_args): | |
| 88 return all_args | |
| 89 # else: | |
| 90 # return [] | |
| 91 else: | |
| 92 # as original with spaces stripped | |
| 93 return ','.join(all_args) |
