Last active
February 25, 2016 09:21
-
-
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
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 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.