Last active
February 22, 2016 21:00
-
-
Save SmithWebster/739d5f3341f73bf67248 to your computer and use it in GitHub Desktop.
remove unclosed parentheses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sys | |
# 'up' is unclosed-parentheses | |
def cut_up_regexp(text): | |
pattern = re.compile(r'\([^\)]+$') | |
return pattern.sub('', text) | |
def cut_up(text): | |
i = 0 | |
pos = -1 | |
for c in list(text): | |
if c is '(': | |
pos = i | |
elif c is ')': | |
pos = -1 | |
i += 1 | |
res = text[0:(pos if pos is not -1 else len(text))] | |
return res | |
def main(): | |
text = sys.argv[1] | |
print cut_up_regexp(text) | |
print cut_up(text) | |
if __name__ == '__main__': | |
main() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
import os | |
from main import cut_up_regexp, cut_up | |
class Test(unittest.TestCase): | |
def setUp(self): | |
self.textIn = 'esdfd((esdf)(esdf' | |
self.textOut = 'esdfd((esdf)' | |
def test_cut_up_regexp(self): | |
res = cut_up_regexp(self.textIn) | |
self.assertEqual(res, self.textOut) | |
def test_cut_up(self): | |
res = cut_up(self.textIn) | |
self.assertEqual(res, self.textOut) | |
if __name__ == "__main__": | |
unittest.main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment