Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2192fb9

Browse files
author
Chris Glass
committed
Added some caching to size and box calculation. Should improve performance a
little.
1 parent afe0931 commit 2192fb9

File tree

1 file changed

+77
-27
lines changed

1 file changed

+77
-27
lines changed

sx/pisa3/pisa_util.py

Lines changed: 77 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
11
# -*- coding: utf-8 -*-
2+
from functools import wraps
3+
from reportlab.lib.colors import *
4+
from reportlab.lib.enums import *
5+
from reportlab.lib.pagesizes import *
6+
from reportlab.lib.styles import *
7+
from reportlab.lib.units import inch, cm
8+
from reportlab.pdfbase import pdfmetrics
9+
import base64
10+
import copy
11+
import httplib
12+
import logging
13+
import mimetypes
14+
import os
15+
import os.path
16+
import pprint
17+
import re
18+
import reportlab
19+
import shutil
20+
import string
21+
import sys
22+
import tempfile
23+
import types
24+
import urllib
25+
import urllib2
26+
import urlparse
227

328
# Copyright 2010 Dirk Holtwick, holtwick.it
429
#
@@ -19,35 +44,12 @@
1944
__author__ = "$Author: holtwick $"
2045
__date__ = "$Date: 2007-10-09 12:58:24 +0200 (Di, 09 Okt 2007) $"
2146

22-
from reportlab.lib.units import inch, cm
23-
from reportlab.lib.styles import *
24-
from reportlab.lib.enums import *
25-
from reportlab.lib.colors import *
26-
from reportlab.lib.pagesizes import *
27-
from reportlab.pdfbase import pdfmetrics
2847

2948
# from reportlab.platypus import *
3049
# from reportlab.platypus.flowables import Flowable
3150
# from reportlab.platypus.tableofcontents import TableOfContents
3251
# from reportlab.platypus.para import Para, PageNumberObject, UNDERLINE, HotLink
3352

34-
import reportlab
35-
import copy
36-
import types
37-
import os
38-
import os.path
39-
import pprint
40-
import sys
41-
import string
42-
import re
43-
import base64
44-
import urlparse
45-
import mimetypes
46-
import urllib2
47-
import urllib
48-
import httplib
49-
import tempfile
50-
import shutil
5153

5254
rgb_re = re.compile("^.*?rgb[(]([0-9]+).*?([0-9]+).*?([0-9]+)[)].*?[ ]*$")
5355

@@ -57,7 +59,6 @@
5759
REPORTLAB22 = (reportlab.Version[0] == "2" and reportlab.Version[2] >= "2")
5860
# print "***", reportlab.Version, REPORTLAB22, reportlab.__file__
5961

60-
import logging
6162
log = logging.getLogger("ho.pisa")
6263

6364
try:
@@ -221,17 +222,66 @@ def getBorderStyle(value, default=None):
221222

222223
MIN_FONT_SIZE = 1.0
223224

225+
#===============================================================================
226+
# Memoize decorators
227+
#===============================================================================
228+
class memoized(object):
229+
230+
def __init__(self, func):
231+
self.funct = func
232+
self.cache = {}
233+
234+
def __call__(self, *args):
235+
try:
236+
return self.cache[args]
237+
except KeyError:
238+
result = self.funct(*args)
239+
self.cache[args] = result
240+
return result
241+
except TypeError:
242+
return self.funct(*args)
243+
244+
class Cache(object):
245+
def __init__(self, func):
246+
self.cache = {}
247+
self.func = func
248+
249+
def __call__(self, *args):
250+
if args not in self.cache:
251+
res = self.func(*args)
252+
self.cache[args] = res
253+
else:
254+
print 'Cache hit!'
255+
return self.cache[args]
256+
257+
#===============================================================================
258+
# end memoize decorators
259+
#===============================================================================
260+
261+
SIZE_CACHE = {}
262+
224263
def getSize(value, relative=0, base=None, default=0.0):
225264
"""
226265
Converts strings to standard sizes
227266
"""
267+
# TODO: Figure out why it's not working with either of the memoize decorators!!!
268+
cached = SIZE_CACHE.get((value, relative, base, default), None)
269+
if cached:
270+
#print 'CACHE HIT'
271+
return cached
272+
res = _get_size(value, relative, base, default)
273+
SIZE_CACHE[(value, relative, base, default)] = res
274+
return res
275+
276+
def _get_size(value, relative=0, base=None, default=0.0):
277+
228278
try:
229279
original = value
230280
if value is None:
231281
return relative
232282
elif type(value) is types.FloatType:
233283
return value
234-
elif type(value) is types.IntType:
284+
elif isinstance(value, int):
235285
return float(value)
236286
elif type(value) in (types.TupleType, types.ListType):
237287
value = "".join(value)
@@ -313,7 +363,7 @@ def getBox(box, pagesize):
313363
box = str(box).split()
314364
if len(box) != 4:
315365
raise Exception, "box not defined right way"
316-
x, y, w, h = map(getSize, box)
366+
x, y, w, h = [getSize(pos) for pos in box]
317367
return getCoords(x, y, w, h, pagesize)
318368

319369
def getPos(position, pagesize):
@@ -323,7 +373,7 @@ def getPos(position, pagesize):
323373
position = str(position).split()
324374
if len(position) != 2:
325375
raise Exception, "position not defined right way"
326-
x, y = map(getSize, position)
376+
x, y = [getSize(pos) for pos in position]
327377
return getCoords(x, y, None, None, pagesize)
328378

329379
def getBool(s):

0 commit comments

Comments
 (0)