Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb075e51

Browse files
committed
Only initialise the method dispatcher once
This has a _significant_ effect on parser creation time
1 parent9d1a0fd commitb075e51

File tree

2 files changed

+396
-340
lines changed

2 files changed

+396
-340
lines changed

‎html5lib/_utils.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
fromtypesimportModuleType
44

5+
try:
6+
fromcollections.abcimportMapping
7+
exceptImportError:
8+
fromcollectionsimportMapping
9+
510
fromsiximporttext_type
611

712
try:
@@ -47,9 +52,6 @@ class MethodDispatcher(dict):
4752
"""
4853

4954
def__init__(self,items=()):
50-
# Using _dictEntries instead of directly assigning to self is about
51-
# twice as fast. Please do careful performance testing before changing
52-
# anything here.
5355
_dictEntries= []
5456
forname,valueinitems:
5557
ifisinstance(name, (list,tuple,frozenset,set)):
@@ -64,6 +66,36 @@ def __init__(self, items=()):
6466
def__getitem__(self,key):
6567
returndict.get(self,key,self.default)
6668

69+
def__get__(self,instance,owner=None):
70+
returnBoundMethodDispatcher(instance,self)
71+
72+
73+
classBoundMethodDispatcher(Mapping):
74+
"""Wraps a MethodDispatcher, binding its return values to `instance`"""
75+
def__init__(self,instance,dispatcher):
76+
self.instance=instance
77+
self.dispatcher=dispatcher
78+
79+
def__getitem__(self,key):
80+
# see https://docs.python.org/3/reference/datamodel.html#object.__get__
81+
# on a function, __get__ is used to bind a function to an instance as a bound method
82+
returnself.dispatcher[key].__get__(self.instance)
83+
84+
defget(self,key,default):
85+
ifkeyinself.dispatcher:
86+
returnself[key]
87+
else:
88+
returndefault
89+
90+
def__iter__(self):
91+
returniter(self.dispatcher)
92+
93+
def__len__(self):
94+
returnlen(self.dispatcher)
95+
96+
def__contains__(self,key):
97+
returnkeyinself.dispatcher
98+
6799

68100
# Some utility functions to deal with weirdness around UCS2 vs UCS4
69101
# python builds

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp