[Zope3-checkins] SVN: Zope3/trunk/src/zope/tal/ The lists has been
changed to dicts for faster membership testing
Dmitry Vasiliev
dima at hlabs.spb.ru
Thu Apr 21 12:10:08 EDT 2005
Log message for revision 30078:
The lists has been changed to dicts for faster membership testing
Changed:
U Zope3/trunk/src/zope/tal/htmltalparser.py
U Zope3/trunk/src/zope/tal/taldefs.py
U Zope3/trunk/src/zope/tal/talinterpreter.py
-=-
Modified: Zope3/trunk/src/zope/tal/htmltalparser.py
===================================================================
--- Zope3/trunk/src/zope/tal/htmltalparser.py 2005-04-21 14:54:25 UTC (rev 30077)
+++ Zope3/trunk/src/zope/tal/htmltalparser.py 2005-04-21 16:10:07 UTC (rev 30078)
@@ -23,28 +23,29 @@
from zope.tal.talgenerator import TALGenerator
-BOOLEAN_HTML_ATTRS = [
+# TODO: In Python 2.4 we can use frozenset() instead of dict.fromkeys()
+BOOLEAN_HTML_ATTRS = dict.fromkeys([
# List of Boolean attributes in HTML that may be given in
# minimized form (e.g. <img ismap> rather than <img ismap="">)
# From http://www.w3.org/TR/xhtml1/#guidelines (C.10)
"compact", "nowrap", "ismap", "declare", "noshade", "checked",
"disabled", "readonly", "multiple", "selected", "noresize",
"defer"
- ]
+ ])
-EMPTY_HTML_TAGS = [
+EMPTY_HTML_TAGS = dict.fromkeys([
# List of HTML tags with an empty content model; these are
# rendered in minimized form, e.g. <img />.
# From http://www.w3.org/TR/xhtml1/#dtds
"base", "meta", "link", "hr", "br", "param", "img", "area",
"input", "col", "basefont", "isindex", "frame",
- ]
+ ])
-PARA_LEVEL_HTML_TAGS = [
+PARA_LEVEL_HTML_TAGS = dict.fromkeys([
# List of HTML elements that close open paragraph-level elements
# and are themselves paragraph-level.
"h1", "h2", "h3", "h4", "h5", "h6", "p",
- ]
+ ])
BLOCK_CLOSING_TAG_MAP = {
"tr": ("tr", "td", "th"),
@@ -55,16 +56,19 @@
"dt": ("dd", "dt"),
}
-BLOCK_LEVEL_HTML_TAGS = [
+BLOCK_LEVEL_HTML_TAGS = dict.fromkeys([
# List of HTML tags that denote larger sections than paragraphs.
"blockquote", "table", "tr", "th", "td", "thead", "tfoot", "tbody",
"noframe", "ul", "ol", "li", "dl", "dt", "dd", "div",
- ]
+ ])
-TIGHTEN_IMPLICIT_CLOSE_TAGS = (PARA_LEVEL_HTML_TAGS
- + BLOCK_CLOSING_TAG_MAP.keys())
+SECTION_LEVEL_HTML_TAGS = PARA_LEVEL_HTML_TAGS.copy()
+SECTION_LEVEL_HTML_TAGS.update(BLOCK_LEVEL_HTML_TAGS)
+TIGHTEN_IMPLICIT_CLOSE_TAGS = PARA_LEVEL_HTML_TAGS.copy()
+TIGHTEN_IMPLICIT_CLOSE_TAGS.update(BLOCK_CLOSING_TAG_MAP)
+
class NestingError(HTMLParseError):
"""Exception raised when elements aren't properly nested."""
@@ -199,7 +203,7 @@
close_to = i
elif t in BLOCK_LEVEL_HTML_TAGS:
close_to = -1
- elif tag in PARA_LEVEL_HTML_TAGS + BLOCK_LEVEL_HTML_TAGS:
+ elif tag in SECTION_LEVEL_HTML_TAGS:
i = len(self.tagstack) - 1
while i >= 0:
closetag = self.tagstack[i]
Modified: Zope3/trunk/src/zope/tal/taldefs.py
===================================================================
--- Zope3/trunk/src/zope/tal/taldefs.py 2005-04-21 14:54:25 UTC (rev 30077)
+++ Zope3/trunk/src/zope/tal/taldefs.py 2005-04-21 16:10:07 UTC (rev 30078)
@@ -19,6 +19,7 @@
from zope.tal.interfaces import ITALExpressionErrorInfo
from zope.interface import implements
+
TAL_VERSION = "1.5.1"
XML_NS = "http://www.w3.org/XML/1998/namespace" # URI for XML namespace
@@ -32,14 +33,15 @@
# zope.i18n.simpletranslationservice module:
NAME_RE = "[a-zA-Z_][-a-zA-Z0-9_]*"
-KNOWN_METAL_ATTRIBUTES = [
+# TODO: In Python 2.4 we can use frozenset() instead of dict.fromkeys()
+KNOWN_METAL_ATTRIBUTES = dict.fromkeys([
"define-macro",
"use-macro",
"define-slot",
"fill-slot",
- ]
+ ])
-KNOWN_TAL_ATTRIBUTES = [
+KNOWN_TAL_ATTRIBUTES = dict.fromkeys([
"define",
"condition",
"content",
@@ -51,9 +53,9 @@
"script",
"tal tag", # a pseudo attribute that holds the namespace of elements
# like <tal:x>, <metal:y>, <i18n:z>
- ]
+ ])
-KNOWN_I18N_ATTRIBUTES = [
+KNOWN_I18N_ATTRIBUTES = dict.fromkeys([
"translate",
"domain",
"target",
@@ -61,7 +63,7 @@
"attributes",
"data",
"name",
- ]
+ ])
class TALError(Exception):
@@ -117,20 +119,20 @@
for part in splitParts(arg):
m = _attr_re.match(part)
if not m:
- raise TALError("Bad syntax in attributes: " + `part`)
- name, expr = m.group(1, 2)
+ raise TALError("Bad syntax in attributes: %r" % part)
+ name, expr = m.groups()
if not xml:
name = name.lower()
- if dict.has_key(name):
- raise TALError("Duplicate attribute name in attributes: " + `part`)
+ if name in dict:
+ raise TALError("Duplicate attribute name in attributes: %r" % part)
dict[name] = expr
return dict
def parseSubstitution(arg, position=(None, None)):
m = _subst_re.match(arg)
if not m:
- raise TALError("Bad syntax in substitution text: " + `arg`, position)
- key, expr = m.group(1, 2)
+ raise TALError("Bad syntax in substitution text: %r" % arg, position)
+ key, expr = m.groups()
if not key:
key = "text"
return key, expr
@@ -155,8 +157,8 @@
return type in ob.__class__.__mro__
except AttributeError:
return False
-
+
def getProgramMode(program):
version = getProgramVersion(program)
if (version == TAL_VERSION and isinstance_(program[1], tuple) and
Modified: Zope3/trunk/src/zope/tal/talinterpreter.py
===================================================================
--- Zope3/trunk/src/zope/tal/talinterpreter.py 2005-04-21 14:54:25 UTC (rev 30077)
+++ Zope3/trunk/src/zope/tal/talinterpreter.py 2005-04-21 16:10:07 UTC (rev 30078)
@@ -28,7 +28,9 @@
from zope.tal.talgenerator import TALGenerator
from zope.tal.translationcontext import TranslationContext
-BOOLEAN_HTML_ATTRS = [
+
+# TODO: In Python 2.4 we can use frozenset() instead of dict.fromkeys()
+BOOLEAN_HTML_ATTRS = dict.fromkeys([
# List of Boolean attributes in HTML that should be rendered in
# minimized form (e.g. <img ismap> rather than <img ismap="">)
# From http://www.w3.org/TR/xhtml1/#guidelines (C.10)
@@ -37,16 +39,8 @@
"compact", "nowrap", "ismap", "declare", "noshade", "checked",
"disabled", "readonly", "multiple", "selected", "noresize",
"defer"
-]
+])
-def _init():
- d = {}
- for s in BOOLEAN_HTML_ATTRS:
- d[s] = 1
- return d
-
-BOOLEAN_HTML_ATTRS = _init()
-
_nulljoin = ''.join
_spacejoin = ' '.join
More information about the Zope3-Checkins
mailing list