Created
July 28, 2012 21:10
-
-
Save Odomontois/3194793 to your computer and use it in GitHub Desktop.
PSOMAG
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
from xml.dom.minidom import parse, Node, Text as TextNode, Element as Element | |
from collections import namedtuple | |
import re | |
class SpecialQueryElement: pass | |
text = SpecialQueryElement() | |
def childQuery(node,query): | |
if isinstance(query,str): | |
tag = query | |
return NodeList(child for child in node.childNodes | |
if isinstance(child,Element) | |
and child.localName == tag ) | |
elif isinstance(query,tuple) and len(query) == 3: | |
tag,attr,value = query | |
return NodeList(child for child in node.childNodes | |
if isinstance(child,Element) | |
and child.localName == tag | |
and child.hasAttribute(attr) | |
and child.attributes[attr].value == value) | |
elif query == text: | |
return NodeList(child.data for child in node.childNodes | |
if isinstance(child,TextNode)) | |
class NodeList(list): | |
def __truediv__(self,query): | |
return NodeList( child for node in self for child in node / query ) | |
Node.__truediv__ = childQuery | |
FeedChart = namedtuple('Feed',('Sync','IQ','Def','Pow','Dex','Mind')) | |
def feedChartAdd(chart,other): | |
return FeedChart(*map(sum,zip(chart,other))) | |
def feedChartMul(chart,q): | |
return FeedChart(*map((lambda x: x*q),chart)) | |
FeedChart.__add__ = feedChartAdd | |
FeedChart.__mul__ = FeedChart.__rmul__ = feedChartMul | |
doc = parse("psomag.htm") | |
feed = {} | |
for info in doc / "html" / "body" / "div" / "table" / "tbody" : | |
names = ''.join( info / "tr" / "td" / "b" / text ) | |
names = re.compile('[a-zA-z]+').findall(names) | |
values = [] | |
items = [] | |
for itemcol in (info / "tr")[2] / "td": | |
cells = itemcol / text | |
itemnames = [q[0] for q in re.compile('(([a-zA-Z]+[ ])*[a-zA-Z]+)').findall(''.join(cells))] | |
if itemnames: items = itemnames | |
itemvalues = re.compile('[0-9\+-]+').findall(''.join(cells)) | |
if itemvalues : values.append([int(x) for x in itemvalues]) | |
values = [FeedChart(*vallist) for vallist in zip(*values)] | |
chart = dict(zip(items,values)) | |
for name in names: feed[name] = chart | |
mag = feed['Marutah'] | |
sol = mag['Sol Atomizer'] | |
moon = mag['Moon Atomizer'] | |
dimate = mag['Dimate'] | |
monomate = mag['Monomate'] | |
dote = mag['Antidote'] | |
par = mag['Antiparalysis'] | |
difluid = mag['Difluid'] | |
print( 2*dote + 3*par + sol ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment