-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
134 lines (112 loc) · 3.47 KB
/
__init__.py
File metadata and controls
134 lines (112 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
stringutils
~~~~~~~~~~~
A functional string utility library for Python.
:copyright: (c) 2018 Alex Hunt.
:license: MIT, see LICENSE.txt for more details.
"""
__version__ = '0.3.0'
import re
def camel_case(string):
"""
Convert a string identifier to :code:`camelCase`.
"""
return lcfirst(pascal_case(string))
def concat(strings):
"""
Concatenate a list of strings into a single string.
"""
return ''.join(strings)
def contains(string, matches):
"""
Determine if a string contains any of the given values. *matches* may be a
single string, or a list of strings.
"""
return any([m in string for m in ([matches] if isinstance(matches, str) else matches)])
def contains_all(string, matches):
"""
Determine if a string contains all of the given values.
"""
return all([m in string for m in matches])
def dashed_case(string):
"""
Convert a string identifier to :code:`dashed-case`. If the string is in
:code:`snake_case`, capitalization of words will be preserved.
"""
return join(split_identifier(string), '-')
def is_whitespace(string):
"""
Determine if a string contains only whitespace characters or is empty.
"""
return string.strip() == ''
def join(strings, sep=', ', insertend=False):
"""
Concatenate a list of strings into a single string by a separating
delimiter. If *insertend* is given and true, the delimiter is also included
at the end of the string.
"""
return sep.join(strings) + (sep if insertend else '')
def lines(string, keepends=False):
"""
Split a string into a list of strings at newline characters. Unless
*keepends* is given and true, the resulting strings do not have newlines
included.
"""
return string.splitlines(keepends)
def lcfirst(string):
"""
Convert the first character of a string to lowercase.
"""
return string[:1].lower() + string[1:]
def pascal_case(string):
"""
Convert a string identifier to :code:`PascalCase`.
"""
return concat(map(ucfirst, split_identifier(string)))
def reverse(string):
"""
Reverse the order of the characters in a string.
"""
return string[::-1]
def snake_case(string):
"""
Convert a string identifier to :code:`snake_case`. If the string is in
:code:`dashed-case`, capitalization of words will be preserved.
"""
return join(split_identifier(string), '_')
def split_identifier(string):
"""
Split a string identifier into a list of its subparts.
"""
return (
re.split('[ \-_]', string)
if re.findall('[ \-_]', string)
else words(re.sub(r'([a-z])([A-Z0-9])', r'\1 \2', string))
)
def title_case(string):
"""
Convert a string identifier to :code:`Title Case`.
"""
return join(map(ucfirst, split_identifier(string)), ' ')
def words(string):
"""
Split a string into a list of words, which were delimited by one or more
whitespace characters.
"""
return re.split('\s+', string)
def ucfirst(string):
"""
Convert the first character of a string to uppercase.
"""
return string[:1].upper() + string[1:]
def unlines(lines, newline='\n'):
"""
Join a list of lines into a single string after appending a terminating
newline character to each.
"""
return join(lines, newline, True)
def unwords(words):
"""
Join a list of words into a single string with separating spaces.
"""
return join(words, ' ')