Skip to content

Commit 92b7e60

Browse files
committed
-
1 parent 2f215cb commit 92b7e60

File tree

6 files changed

+92
-30
lines changed

6 files changed

+92
-30
lines changed

source_py3/python_toolbox/string_tools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
'''Defines string-related tools.'''
55

66
from .string_tools import docstring_trim, get_n_identical_edge_characters
7-
from . import conversions
7+
from . import case_conversions
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2009-2014 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
'''Defines functions for converting between different string conventions.'''
5+
6+
import sys
7+
import re
8+
9+
10+
def camelcase_to_spacecase(s):
11+
'''
12+
Convert a string from camelcase to spacecase.
13+
14+
Example: camelcase_to_underscore('HelloWorld') == 'Hello world'
15+
'''
16+
if s == '': return s
17+
process_character = lambda c: (' ' + c.lower()) if c.isupper() else c
18+
return s[0] + ''.join(process_character(c) for c in s[1:])
19+
20+
21+
def camel_case_to_lower_case(s):
22+
'''
23+
Convert a string from camel-case to lower-case.
24+
25+
Example:
26+
27+
camel_case_to_lower_case('HelloWorld') == 'hello_world'
28+
29+
'''
30+
return re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', '_\\1', s). \
31+
lower().strip('_')
32+
33+
34+
def lower_case_to_camel_case(s):
35+
'''
36+
Convert a string from lower-case to camel-case.
37+
38+
Example:
39+
40+
camel_case_to_lower_case('hello_world') == 'HelloWorld'
41+
42+
'''
43+
s = s.capitalize()
44+
while '_' in s:
45+
head, tail = s.split('_', 1)
46+
s = head + tail.capitalize()
47+
return s
48+
49+
50+
def camel_case_to_upper_case(s):
51+
'''
52+
Convert a string from camel-case to upper-case.
53+
54+
Example:
55+
56+
camel_case_to_lower_case('HelloWorld') == 'HELLO_WORLD'
57+
58+
'''
59+
return camel_case_to_lower_case(s).upper()
60+
61+
62+
def upper_case_to_camel_case(s):
63+
'''
64+
Convert a string from upper-case to camel-case.
65+
66+
Example:
67+
68+
camel_case_to_lower_case('HELLO_WORLD') == 'HelloWorld'
69+
70+
'''
71+
return lower_case_to_camel_case(s.lower())

source_py3/python_toolbox/string_tools/conversions.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

source_py3/python_toolbox/string_tools/string_tools.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ def get_n_identical_edge_characters(string, character=None, head=True):
5858
if c != character:
5959
return i
6060
else:
61-
return len(string)
61+
return len(string)
62+
63+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2009-2014 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
from python_toolbox.string_tools import case_conversions
5+
6+
7+
def test():
8+
assert case_conversions.camelcase_to_spacecase('HelloWorld') == \
9+
'Hello world'
10+
assert case_conversions.camel_case_to_lower_case('HelloWorld') == \
11+
'hello_world'
12+
assert case_conversions.lower_case_to_camel_case('hello_world') == \
13+
'HelloWorld'
14+
assert case_conversions.camel_case_to_upper_case('HelloWorld') == \
15+
'HELLO_WORLD'
16+
assert case_conversions.upper_case_to_camel_case('HELLO_WORLD') == \
17+
'HelloWorld'

source_py3/test_python_toolbox/test_string_tools/test_get_n_identical_edge_characters.py renamed to source_py3/test_python_toolbox/test_string_tools/test_get_n_identical_edge_characters - Copy.py

File renamed without changes.

0 commit comments

Comments
 (0)