Skip to content

Commit

Permalink
Fix handling of unused alignment (#132)
Browse files Browse the repository at this point in the history
* This reverts a change introduced in 55467d2 (1.17) in order to resolve #112, but which broke more verbose use cases.
The proper solution to #112 should have been to instructing the user to use the '{:^S},{^:S}' pattern instead, because in some cases format() is not really reversible so you ought to be more verbose in your format.

This resolves #118.

* Added a specific test case for the example in #118

Co-authored-by: Tomer Harpaz <[email protected]>
  • Loading branch information
tomerha and tomerha authored Jun 4, 2021
1 parent 9a4a555 commit 421c199
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,11 +1264,11 @@ def _handle_field(self, field):

# align "=" has been handled
if align == '<':
s = '%s%s+' % (s, fill)
s = '%s%s*' % (s, fill)
elif align == '>':
s = '%s*%s' % (fill, s)
elif align == '^':
s = '%s*%s%s+' % (fill, s, fill)
s = '%s*%s%s*' % (fill, s, fill)

return s

Expand Down
27 changes: 16 additions & 11 deletions test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,17 +876,22 @@ def test_pickling_bug_110(self):
# prior to the fix, this would raise an AttributeError
pickle.dumps(p)

def test_search_centered_bug_112(self):
r = parse.parse("{:^},{:^}", " 12 , 34 ")
self.assertEqual(r[1], "34")
r = parse.search("{:^},{:^}", " 12 , 34 ")
self.assertEqual(r[1], "34")

def test_search_left_align_bug_112(self):
r = parse.parse("{:<},{:<}", "12 ,34 ")
self.assertEqual(r[1], "34")
r = parse.search("{:<},{:<}", "12 ,34 ")
self.assertEqual(r[1], "34")
def test_unused_centered_alignment_bug(self):
r = parse.parse("{:^2S}", "foo")
self.assertEqual(r[0], "foo")
r = parse.search("{:^2S}", "foo")
self.assertEqual(r[0], "foo")

# specifically test for the case in issue #118 as well
r = parse.parse("Column {:d}:{:^}", "Column 1: Timestep")
self.assertEqual(r[0], 1)
self.assertEqual(r[1], "Timestep")

def test_unused_left_alignment_bug(self):
r = parse.parse("{:<2S}", "foo")
self.assertEqual(r[0], "foo")
r = parse.search("{:<2S}", "foo")
self.assertEqual(r[0], "foo")

def test_match_trailing_newline(self):
r = parse.parse('{}', 'test\n')
Expand Down

0 comments on commit 421c199

Please sign in to comment.