Skip to content

Instantly share code, notes, and snippets.

@gjask
Last active February 25, 2016 09:21
Show Gist options
  • Save gjask/e9a377b17b89ed3b95ff to your computer and use it in GitHub Desktop.
Save gjask/e9a377b17b89ed3b95ff to your computer and use it in GitHub Desktop.
Reads go sgf file from stdin and returns 180 degrees rotated on stdout
import re
import string
import sys
class Flipper(object):
flip_re = re.compile('\[([a-z]{2})\]')
def __init__(self, board_size=19):
self.board_size = board_size
self.scale = string.ascii_lowercase[:board_size]
def flip_coordinates(self, match):
coordinates = match.group(1)
s = self.scale
b = self.board_size
return '[' + ''.join([s[b-s.index(c)-1] for c in coordinates]) + ']'
def flip_sgf(self, iterable):
return [self.flip_re.sub(self.flip_coordinates, l) for l in iterable]
if __name__ == '__main__':
flipper = Flipper()
for line in flipper.flip_sgf(sys.stdin):
sys.stdout.write(line)
@gjask
Copy link
Author

gjask commented Feb 24, 2016

However after I've realized that sgf does not avoid i/j ambiguity, all this snippet almost lost it's purpose. For crude rotation you can simply use linux tr abcdefghijklmnopqr rqponmlkjihgfedcba, though it will mess with comments and possibly with other stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment