-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringUtils.py
More file actions
209 lines (165 loc) · 4.94 KB
/
stringUtils.py
File metadata and controls
209 lines (165 loc) · 4.94 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import sys, re, uuid, time, urllib, subprocess
def splitInput(query):
linesList = query.splitlines()
commaList = query.split(",")
spaceList = query.split()
if len(linesList) > 1:
theList = linesList
elif len(commaList) > 1:
theList = commaList
elif len(spaceList) > 1:
theList = spaceList
return theList
def mapTrim(str):
return str.strip()
def mapIndent(str):
return(re.sub("^", "\t", str))
def mapUnindent(str):
return(re.sub("^\t", "", str))
def mapPyComment(str):
return(re.sub("^", "# ", str))
def mapUnPyComment(str):
return(re.sub("^# ", "", str))
def numberList(inList):
for num, word in enumerate(inList):
print("{}. {}".format(num+1, word.strip()))
def removeNumberList(inList):
for num, word in enumerate(inList):
print(re.sub("^\d+.(\.)? ", "", word))
def sumList(inList):
outNumber = 0
for num in inList:
if num != "":
outNumber += int(num)
print(outNumber)
def avgList(inList):
outNumber = 0
count = len(inList)
for num in inList:
if num != "":
outNumber += int(num)
print(round(outNumber/count, 2))
def sortList(inList):
inList.sort()
for line in inList:
print("{}".format(line))
def reverseSortList(inList):
inList.sort(reverse = True)
for line in inList:
print("{}".format(line))
def sortUnique(inList):
trimList = map(mapTrim, inList)
uniqueList = list(set(trimList))
uniqueList.sort()
for line in uniqueList:
print("{}".format(line))
def commaSeparate(inList):
output = filter(bool, map(mapTrim, inList))
print(", ".join(output))
def inSqlQuoted(inList):
readyList = filter(bool, map(mapTrim, inList))
connected = "\', \'".join(readyList)
myFormat = "(\'{}\')"
print(myFormat.format(connected))
def inSql(inList):
readyList = filter(bool, map(mapTrim, inList))
connected = ", ".join(readyList)
myFormat = "({})"
print(myFormat.format(connected))
def grepOr(inList):
readyList = filter(bool, map(mapTrim, inList))
connected = "\'\\|\'".join(readyList)
myFormat = "\'{}\'"
print(myFormat.format(connected))
def toArray(inList):
readyList = filter(bool, map(mapTrim, inList))
connected = "\", \"".join(readyList)
myFormat = "[\"{}\"]"
print(myFormat.format(connected))
def toLower(theQuery):
print(theQuery.lower())
def toCapital(theQuery):
print(theQuery.upper())
def toTitle(theQuery):
print(theQuery.title())
def reverseSlashes(theQuery):
out = re.sub("\\\\", "/", theQuery)
print(out)
def characterCount(theQuery):
print(len(theQuery))
def guid():
print(uuid.uuid1())
def ms():
millis = int(round(time.time() * 1000))
print(millis)
def indent(inList):
output = map(mapIndent, inList)
for line in output:
print("{}".format(line))
def unindent(inList):
output = map(mapUnindent, inList)
for line in output:
print("{}".format(line))
def pyComment(inList):
output = map(mapPyComment, inList)
for line in output:
print("{}".format(line))
def unPyComment(inList):
output = map(mapUnPyComment, inList)
for line in output:
print("{}".format(line))
def urlencode(str):
print(urllib.quote(str))
def urldecode(str):
print(urllib.unquote(str))
#query = sys.argv[1]
def run(query):
clipboard = subprocess.check_output("pbpaste", universal_newlines=True)
if query == "nl":
numberList(splitInput(clipboard))
elif query == "rn":
removeNumberList(splitInput(clipboard))
elif query == "sort":
sortList(splitInput(clipboard))
elif query == "rsort":
reverseSortList(splitInput(clipboard))
elif query == "usort":
sortUnique(splitInput(clipboard))
elif query == "sum":
sumList(splitInput(clipboard))
elif query == "avg":
avgList(splitInput(clipboard))
elif query == "array":
toArray(splitInput(clipboard))
elif query == "sqlIn":
inSql(splitInput(clipboard))
elif query == "sqlInQuoted":
inSqlQuoted(splitInput(clipboard))
elif query == "grepOr":
grepOr(splitInput(clipboard))
elif query == "comma":
commaSeparate(splitInput(clipboard))
elif query == "indent":
indent(splitInput(clipboard))
elif query == "unindent":
unindent(splitInput(clipboard))
elif query == "pycomment":
pyComment(splitInput(clipboard))
elif query == "unpycomment":
unPyComment(splitInput(clipboard))
elif query == "slashes":
reverseSlashes(clipboard)
elif query == "urlEncode":
urlencode(clipboard)
elif query == "urlUnencode":
urldecode(clipboard)
elif query == "toLower":
toLower(clipboard)
elif query == "toCaps":
toCapital((clipboard))
elif query == "cc":
characterCount((clipboard))
elif query == "guid":
guid()
elif query == "ms":
ms()