-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringhandler.py
128 lines (103 loc) · 3.84 KB
/
stringhandler.py
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
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
""" A class to perform string operations """
# Python imports
import re # Regular expression operations
import sys # System-specific parameters and functions
# A class to perform string operations
class StringHandler(object):
# Initialization
def __init__(self):
# Declare variables
self.count = 0 # Replace operation counter
self.error = "" # String for errors
self.regex1 = r"(?<![-'])(?<=\b)"
self.regex2 = r"(?=\b)(?![-'])"
# Replace
def replace(self, s, mode, old, new, variations=False):
self.error = ""
# Check if string is empty
if s == "":
self.error = "String is empty."
return False, s
# Check mode range
if mode < 1 or mode > 3:
self.error = "Mode is out of range."
return False, s
# Check if old and new match
if old == new:
self.error = "Old and new strings match."
return False, s
try:
# RegEx
if mode == 1: # RegEx
s, c = re.subn(old, new, s)
self.count += c
return True, s
# Normal replace
if s.find(old) > -1:
if mode == 2: # Replace
self.count += s.count(old)
s = s.replace(old, new)
elif mode == 3: # Word
s, c = re.subn(self.regex1 + re.escape(old) + self.regex2,\
new, s)
self.count += c
# Should text case variations be performed?
if not variations:
return True, s
# Capitalize
oldc = old.capitalize()
if old != oldc and s.find(oldc) > -1:
if mode == 2: # Replace
self.count += s.count(oldc)
s = s.replace(oldc, new.capitalize())
elif mode == 3: # Word
s, c = re.subn(self.regex1 + re.escape(oldc) + self.regex2,\
new.capitalize(), s)
self.count += c
# Lowercase
oldl = old.lower()
if old != oldl and s.find(oldl) > -1:
if mode == 2: # Replace
self.count += s.count(oldl)
s = s.replace(oldl, new.lower())
elif mode == 3: # Word
s, c = re.subn(self.regex1 + re.escape(oldl) + self.regex2,\
new.lower(), s)
self.count += c
# Uppercase
oldu = old.upper()
if old != oldu and s.find(oldu) > -1:
if mode == 2: # Replace
self.count += s.count(oldu)
s = s.replace(oldu, new.upper())
elif mode == 3: # Word
s, c = re.subn(self.regex1 + re.escape(oldu) + self.regex2,\
new.upper(), s)
self.count += c
except re.error as e:
self.error = "Regular expression error: " + str(e)
return False, s
except:
self.error = "Unexpected error: " + str(sys.exc_info()[1])
return False, s
else:
return True, s
# Reset
def reset(self):
self.count = 0
self.error = ""
# Test
def test(self, old, new):
self.error = ""
try:
re.subn(old, new, "foo")
except re.error as e:
self.error = "Regular expression error: " + str(e)
return False
except:
self.error = "Unexpected error: " + str(sys.exc_info()[1])
return False
else:
return True