Skip to content
This repository was archived by the owner on Apr 12, 2018. It is now read-only.

Commit 185b3f2

Browse files
committed
Add function swf.utils.underscore_to_camel
1 parent 05eb42c commit 185b3f2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

swf/utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from datetime import datetime, timedelta
99
from time import mktime
10+
from itertools import chain, izip, islice
1011

1112
from functools import wraps
1213

@@ -181,3 +182,32 @@ def camel_to_underscore(string):
181182
res.extend([char.lower()])
182183

183184
return ''.join(res)
185+
186+
187+
def underscore_to_camel(string):
188+
"""
189+
190+
>>> underscore_to_camel('')
191+
''
192+
>>> underscore_to_camel('a')
193+
'A'
194+
>>> underscore_to_camel('A')
195+
'A'
196+
>>> underscore_to_camel('ab')
197+
'Ab'
198+
>>> underscore_to_camel('a_b_c')
199+
'ABC'
200+
>>> underscore_to_camel('ab_cd_ef')
201+
'AbCdEf'
202+
>>> underscore_to_camel('request_cancel_workflow')
203+
'RequestCancelWorkflow'
204+
205+
"""
206+
if string == '':
207+
return ''
208+
209+
return ''.join(chain([string[0].upper()],
210+
((c.upper() if p == '_' else c) if
211+
c != '_' else '' for p, c in
212+
izip(islice(string, 0, None),
213+
islice(string, 1, None)))))

0 commit comments

Comments
 (0)