Last active
August 29, 2015 14:10
-
-
Save kowey/9aeeabf2201baa3b62fc to your computer and use it in GitHub Desktop.
dubious cleanup (longer and solving a different problem)
This file contains hidden or 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 | |
class State: | |
""" | |
Holds the resources and the buildups | |
""" | |
def __init__(self, state_chunk): #state_chunk is actually line.split("|")[4] | |
item_list = state_chunk.replace('|',',').split(",") | |
res_list = [] | |
build_list = [] | |
for i in range(0,len(item_list)): | |
if re.match(r"[0-9]",item_list[i]): | |
# the coincidental pairing logic here (foo=[x,y,z] | |
# is split into 'foo=[x'; 'y'; 'z]') and then | |
# reassembled) felt so utterly wrong that I couldn't | |
# resist replacing it with something more like a parser | |
# (see 1_after.py)... but then it meant I had to | |
# introduce generation, and all sorts of knowledge about | |
# the output format, which was similar by design to the input | |
# :-/ | |
# so which is the greater sin? the bogosity before or the | |
# boat-rocking after? | |
build_list.append(item_list[i-1]+","+item_list[i]) | |
else: | |
if "[" not in item_list[i]: | |
res_list.append(item_list[i]) | |
self.resources = res_list[:] | |
self.buildups = build_list[:] | |
def resources_string(self): | |
return '; '.join(self.resources) | |
def buildups_string(self): | |
return '; '.join(self.buildups) |
This file contains hidden or 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
from collections import namedtuple, OrderedDict | |
import re | |
class State(namedtuple('State', | |
'resources buildups')): | |
"resources and buildups in an entry" | |
# pylint: disable=no-self-use | |
def _render(self, odict): | |
"convert a dictionary to string" | |
return "; ".join(u'{}={}'.format(k, v) | |
for k, v in odict.items()) | |
# pylint: enable=no-self-use | |
def resources_string(self): | |
"string representation of resources" | |
return self._render(self.resources) | |
def buildups_string(self): | |
"string representation of buildups" | |
formatted = OrderedDict() | |
for key, nums in self.buildups.items(): | |
if len(nums) == 1: | |
items = None | |
# NB: Eric thinks it should be something like this, or | |
# alternatively that this pairing mechanism should be limited | |
# to roads, but for now backwards compatibility is the main | |
# focus | |
# items = nums[0] | |
else: | |
npairs = zip(nums, nums[1:]) | |
items = '; '.join('{},{}'.format(x, y) | |
for x, y in npairs) | |
if items: | |
formatted[key] = '[{}]'.format(items) | |
return self._render(formatted) | |
def parse_state(snippet): | |
""" | |
From a substring soclog entry to some slightly higher level | |
representation of the resources and buildups it represents | |
""" | |
resources = OrderedDict() | |
buildups = OrderedDict() | |
for item in snippet.split('|'): | |
key, value = item.split('=', 1) | |
if value.isdigit(): | |
resources[key] = value | |
continue | |
num_match = re.match(r'^\[(.*)\]$', value) | |
if num_match: | |
nums = num_match.group(1).split(',') | |
buildups[key] = nums | |
continue | |
oops = 'Unknown key value pair ({}: {}) in state' | |
raise Exception(oops.format(key, value, snippet)) | |
return State(resources, buildups) | |
This file contains hidden or 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
from __future__ import print_function | |
# indentical in both cases | |
EXAMPLE = ("clay=0|ore=1|sheep=0|wheat=0|wood=1|unknown=0|knights=1|" | |
"roads=[69,86,70,71,72,73,90]|settlements=[69,103,107]|" | |
"cities=[]|dev-cards=1") | |
EXAMPLE_OUTPUT=""" | |
RESOURCES: clay=0; ore=1; sheep=0; wheat=0; wood=1; unknown=0; knights=1; dev-cards=1 | |
BUILDUPS: roads=[69,86; 86,70; 70,71; 71,72; 72,73; 73,90]; settlements=[69,103; 103,107]; cities=[] | |
""" | |
def print_reformatted(snippet): | |
""" | |
Demo the revised rewriter | |
""" | |
state = State(snippet) | |
print('RESOURCES: ', state.resources_string()) | |
print('BUILDUPS: ', state.buildups_string()) | |
if __name__ == '__main__': | |
print_reformatted(EXAMPLE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment