projects
/
python-bulletml.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Intern type strings for faster comparison during action running.
[python-bulletml.git]
/
bulletml
/
parser.py
diff --git
a/bulletml/parser.py
b/bulletml/parser.py
index
e9b0488
..
9d515a2
100644
(file)
--- a/
bulletml/parser.py
+++ b/
bulletml/parser.py
@@
-13,6
+13,12
@@
import math
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import ElementTree
+# Python 3 moved this for no really good reason.
+try:
+ from sys import intern
+except ImportError:
+ pass
+
try:
from io import StringIO
except ImportError:
try:
from io import StringIO
except ImportError:
@@
-25,7
+31,7
@@
from bulletml.errors import Error
from bulletml.expr import NumberDef, INumberDef
from bulletml.expr import NumberDef, INumberDef
-__all_ = ["ParseError", "BulletML"]
+__all_
_
= ["ParseError", "BulletML"]
class ParseError(Error):
"""Raised when an error occurs parsing the XML structure."""
class ParseError(Error):
"""Raised when an error occurs parsing the XML structure."""
@@
-64,7
+70,7
@@
class Direction(object):
def __init__(self, type, value):
if type not in self.VALID_TYPES:
raise ValueError("invalid type %r" % type)
def __init__(self, type, value):
if type not in self.VALID_TYPES:
raise ValueError("invalid type %r" % type)
- self.type =
type
+ self.type =
intern(type)
self.value = value
def __getstate__(self):
self.value = value
def __getstate__(self):
@@
-132,7
+138,7
@@
class Speed(object):
def __init__(self, type, value):
if type not in self.VALID_TYPES:
raise ValueError("invalid type %r" % type)
def __init__(self, type, value):
if type not in self.VALID_TYPES:
raise ValueError("invalid type %r" % type)
- self.type =
type
+ self.type =
intern(type)
self.value = value
def __getstate__(self):
self.value = value
def __getstate__(self):
@@
-358,10
+364,11
@@
class BulletDef(object):
direction = None
speed = None
direction = None
speed = None
- def __init__(self, actions=
[], direction=None, speed=None
):
+ def __init__(self, actions=
(), direction=None, speed=None, tags=()
):
self.direction = direction
self.speed = speed
self.actions = list(actions)
self.direction = direction
self.speed = speed
self.actions = list(actions)
+ self.tags = set(tags)
def __getstate__(self):
state = []
def __getstate__(self):
state = []
@@
-371,6
+378,8
@@
class BulletDef(object):
state.append(("speed", self.speed))
if self.actions:
state.append(("actions", self.actions))
state.append(("speed", self.speed))
if self.actions:
state.append(("actions", self.actions))
+ if self.tags:
+ state.append(("tags", list(self.tags)))
return state
def __setstate__(self, state):
return state
def __setstate__(self, state):
@@
-383,6
+392,7
@@
class BulletDef(object):
actions = []
speed = None
direction = None
actions = []
speed = None
direction = None
+ tags = set()
for subelem in element.getchildren():
tag = realtag(subelem)
if tag == "direction":
for subelem in element.getchildren():
tag = realtag(subelem)
if tag == "direction":
@@
-393,7
+403,9
@@
class BulletDef(object):
actions.append(ActionDef.FromXML(doc, subelem))
elif tag == "actionRef":
actions.append(ActionRef.FromXML(doc, subelem))
actions.append(ActionDef.FromXML(doc, subelem))
elif tag == "actionRef":
actions.append(ActionRef.FromXML(doc, subelem))
- dfn = cls(actions, direction, speed)
+ elif tag == "tag":
+ tags.add(subelem.text)
+ dfn = cls(actions, direction, speed, tags)
doc._bullets[element.get("label")] = dfn
return dfn
doc._bullets[element.get("label")] = dfn
return dfn
@@
-402,6
+414,7
@@
class BulletDef(object):
return (
self.direction and self.direction(params, rank),
self.speed and self.speed(params, rank),
return (
self.direction and self.direction(params, rank),
self.speed and self.speed(params, rank),
+ self.tags,
actions)
def __repr__(self):
actions)
def __repr__(self):
@@
-530,7
+543,7
@@
class Offset(object):
def __init__(self, type, x, y):
if type not in self.VALID_TYPES:
raise ValueError("invalid type %r" % type)
def __init__(self, type, x, y):
if type not in self.VALID_TYPES:
raise ValueError("invalid type %r" % type)
- self.type =
type
+ self.type =
intern(type)
self.x = x
self.y = y
self.x = x
self.y = y
@@
-567,11
+580,13
@@
class Offset(object):
class FireDef(object):
"""Fire definition (creates a bullet)."""
class FireDef(object):
"""Fire definition (creates a bullet)."""
- def __init__(self, bullet, direction=None, speed=None, offset=None):
+ def __init__(
+ self, bullet, direction=None, speed=None, offset=None, tags=()):
self.bullet = bullet
self.direction = direction
self.speed = speed
self.offset = offset
self.bullet = bullet
self.direction = direction
self.speed = speed
self.offset = offset
+ self.tags = set(tags)
def __getstate__(self):
state = []
def __getstate__(self):
state = []
@@
-581,6
+596,8
@@
class FireDef(object):
state.append(("speed", self.speed))
if self.offset:
state.append(("offset", self.offset))
state.append(("speed", self.speed))
if self.offset:
state.append(("offset", self.offset))
+ if self.tags:
+ state.append(("tags", list(self.tags)))
try:
params = self.bullet.params
except AttributeError:
try:
params = self.bullet.params
except AttributeError:
@@
-603,6
+620,7
@@
class FireDef(object):
direction = None
speed = None
offset = None
direction = None
speed = None
offset = None
+ tags = set()
for subelem in element.getchildren():
tag = realtag(subelem)
for subelem in element.getchildren():
tag = realtag(subelem)
@@
-616,8
+634,10
@@
class FireDef(object):
bullet = BulletRef.FromXML(doc, subelem)
elif tag == "offset":
offset = Offset.FromXML(doc, subelem)
bullet = BulletRef.FromXML(doc, subelem)
elif tag == "offset":
offset = Offset.FromXML(doc, subelem)
+ elif tag == "tag":
+ tags.add(subelem.text)
try:
try:
- fire = cls(bullet, direction, speed, offset)
+ fire = cls(bullet, direction, speed, offset
, tags
)
except UnboundLocalError as exc:
raise ParseError(str(exc))
else:
except UnboundLocalError as exc:
raise ParseError(str(exc))
else:
@@
-625,12
+645,13
@@
class FireDef(object):
return fire
def __call__(self, params, rank):
return fire
def __call__(self, params, rank):
- direction, speed, actions = self.bullet(params, rank)
+ direction, speed,
tags,
actions = self.bullet(params, rank)
if self.direction:
direction = self.direction(params, rank)
if self.speed:
speed = self.speed(params, rank)
if self.direction:
direction = self.direction(params, rank)
if self.speed:
speed = self.speed(params, rank)
- return direction, speed, actions, self.offset
+ tags = tags.union(self.tags)
+ return direction, speed, self.offset, tags, actions
def __repr__(self):
return "%s(direction=%r, speed=%r, bullet=%r)" % (
def __repr__(self):
return "%s(direction=%r, speed=%r, bullet=%r)" % (
@@
-691,7
+712,7
@@
class BulletML(object):
)
def __init__(self, type="none", actions=None):
)
def __init__(self, type="none", actions=None):
- self.type =
type
+ self.type =
intern(type)
self.actions = [] if actions is None else actions
def __getstate__(self):
self.actions = [] if actions is None else actions
def __getstate__(self):
@@
-759,7
+780,7
@@
class BulletML(object):
else:
try:
return yaml.load(source)
else:
try:
return yaml.load(source)
- except Exception
,
exc:
+ except Exception
as
exc:
raise ParseError(str(exc))
@classmethod
raise ParseError(str(exc))
@classmethod