

How to optimize lots of calls to Font.measure
I need to implement text wrapping on a TKinter canvas, so I made a modified version of textwrap.TextWrapper from the python standard library, that uses the width of text instead of the character count.
This uses a lot of calls to Font.measure though, so according to cProfile most of my runtime is spent calling Font.measure. So now i want to optimize this, but the only idea i can come up with is to pre-measure all common characters and cache those values to then do the calculations on my own, but that seems like a janky solution.
Here's the (very long) function:
def _wrap_chunks(self, chunks, font: tkinter.font.Font = None, starting_width = None):
"""_wrap_chunks(chunks : [string]) -> [string]
Wrap a sequence of text chunks and return a list of lines of
length 'self.width' or less. (If 'break_long_words' is false,
some lines may be longer than this.) Chunks correspond roughly
to words and the whitespace between them: each chunk is
indivisible (modulo 'break_long_words'), but a line break can
come between any two chunks. Chunks should not have internal
whitespace; ie. a chunk is either all whitespace or a "word".
Whitespace chunks will be removed from the beginning and end of
lines, but apart from that whitespace is preserved.
"""
lines = []
if self.width <= 0:
raise ValueError("invalid width %r (must be > 0)" % self.width)
if self.max_lines is not None:
if self.max_lines > 1:
indent = self.subsequent_indent
else:
indent = self.initial_indent
if font.measure(indent) + font.measure(self.placeholder.lstrip()) > self.width:
raise ValueError("placeholder too large for max width")
# Arrange in reverse order so items can be efficiently popped
# from a stack of chucks.
chunks.reverse()
first_chunk = True
while chunks:
# Start the list of chunks that will make up the current line.
# cur_len is just the length of all the chunks in cur_line.
cur_line = []
cur_len = starting_width
starting_width = 0
# Figure out which static string will prefix this line.
if lines:
indent = self.subsequent_indent
else:
indent = self.initial_indent
# Maximum width for this line.
width = self.width - font.measure(indent)
# First chunk on line is whitespace -- drop it, unless this
# is the very beginning of the text (ie. no lines started yet).
if self.drop_whitespace and chunks[-1].strip() == '' and lines:
del chunks[-1]
while chunks:
l = font.measure(chunks[-1])
# Can at least squeeze this chunk onto the current line.
if cur_len + l <= width:
cur_line.append(chunks.pop())
cur_len += l
# Nope, this line is full.
else:
if first_chunk:
lines.append("")
break
first_chunk = False
# The current line is full, and the next chunk is too big to
# fit on *any* line (not just this one).
if chunks and font.measure(chunks[-1]) > width:
self._handle_long_word(chunks, cur_line, cur_len, width, font)
cur_len = sum(map(font.measure, cur_line))
# If the last chunk on this line is all whitespace, drop it.
if self.drop_whitespace and cur_line and cur_line[-1].strip() == '':
cur_len -= font.measure(cur_line[-1])
del cur_line[-1]
if cur_line:
if (self.max_lines is None or
len(lines) + 1 < self.max_lines or
(not chunks or
self.drop_whitespace and
len(chunks) == 1 and
not chunks[0].strip()) and cur_len <= width):
# Convert current line back to a string and store it in
# list of all lines (return value).
lines.append(indent + ''.join(cur_line))
else:
while cur_line:
if (cur_line[-1].strip() and
cur_len + font.measure(self.placeholder) <= width):
cur_line.append(self.placeholder)
lines.append(indent + ''.join(cur_line))
break
cur_len -= font.measure(cur_line[-1])
del cur_line[-1]
else:
if lines:
prev_line = lines[-1].rstrip()
if (font.measure(prev_line) + font.measure(self.placeholder) <=
self.width):
lines[-1] = prev_line + self.placeholder
break
lines.append(indent + self.placeholder.lstrip())
break
first_chunk = False
return lines
Am I the only one that think it's hilarious that they have Martin Freeman voice a character with literally 7 lines of dialogue?
Like this is everything he says:
"Sister, dear, if we turn all of the townspeople into frogs who will give us our gold and turnips?"
"Mhmmm, interesting, yeah, thinking outside the box, yes, but if we eat them all who will give us our milk?"
"Hohoho, what a delightful compromise."
"Now what do you say for sparing your goat-dad's life?"
"Magica, stop! The treasure!"
"Uhuhuhuh, you cannot give in to his goading. Focus your powers!"
"Magica!"
I was reading the art book and saw the little pieces of sheet music and decided to recreate them to see if i recognized them
I don't really recognize Webby's or Magica's, and only recognize the ending of GizmoDuck's, but that may be because there was no tempo specified and I defaulted to 100 BPM for all of them.
How would Della know which one of the triplets were supposed to be Jet, Turbo, and Rebel
When Della returns she says that Huey was supposed to be Jet, Dewey was Turbo, and Louie was Rebel, but how would she know which was supposed to be which?
Edit: My theory is that Della came up with the idea of color-coding them before she disappeared and Donald just kept that.
What are some lines that you think are underrated?
I really like "Make new foes, but keep the old. One is steel, the other is ... FLINTHEART GLOMGOLD! [maniacal laughter] Wait where am I?" from the finale.
I Fixed the user flairs!
Hi! I was just added as a moderator here and decided to fix the user flairs, because those broke at some point.
I couldn't see what they were originally, but i replaced all of them based on their names and added some new ones.
Maybe we could create some user flairs in this subreddit
I think this has been said before in this subreddit, but the subreddit has user flairs, they're just currently all blank for some reason, and I think it would be cool to add some real flairs.
You could make a bunch of characters, so people can show their favourite, or you could make it a bunch of catchphrases like "Woo-Hoo!", "Bless me bagpipes!", etc., or you could do the unofficial "teams" like team science, team magic, etc.
Create: coasters simulated coaster that can run with either 1 or 2 trains using a block zone system made with CC: Tweaked
My duck (comic) book collection
Most of them are in Dutch
edit: i just counted them and i have 102 normal donald duck pockets
In the theme song is it "Tales of derring-do, bad and good lucktales!" or " Tales of daring, do bad, and good lucktales!
I always thought it was the first but I have seen some people say it is the second, so now I'm confused.
P.S. English is not my first Language and I'm unsure about my comma placement
Data transmitted from the ACIA gets lost in the MAX232, which also gets very hot sporadically
im hooking up the ACIA and it recieves data just fine it also sends data to the MAX232 just fine, but the MAX232 outputs 4 volts and, according to my logic analyser, outputs logic high constantly.
The MAX232 also gets very hot occasionally but cools down quickly.
6502 jumps to wrong address
i recently accidentally caused a shortcircuit in my 6502 computer and it wasn't working anymore so i decided to rebuild it from scratch to figure out the problem, i'm at the point where you hardwire 0xEA to the databus, but after resetting the computer the cpu jumps to 0x7AEA instead of 0xEAEA, is my 65c02 broken or could it be something else?
EDIT: i just noticed that the read write pin indicates that the processor is trying to write to the reset vector?
EDIT2: sorry I'm stupid I had the wires that go to the arduino hooked up in the wrong order
output from the arduino:
0111101011110000 11101010 7af0 W ea
0111101011110000 11101010 7af0 r ea < press reset about here
0111101011110000 11101010 7af0 r ea
1111111111111111 11101010 ffff r ea
0111101011110000 11101010 7af0 W ea
0000000111100010 11101010 01e2 W ea
0000000111100001 11101010 01e1 W ea
0000000111100000 11101010 01e0 r ea
1111111111111100 11101010 fffc W ea < reads (writes?) reset vector
1111111111111101 11101010 fffd r ea < reads reset vector
0111101011101010 11101010 7aea r ea < jumps to 0x7AEA
0111101011101011 11101010 7aeb r ea
0111101011101011 11101010 7aeb r ea
0111101011101100 11101010 7aec r ea
0111101011101100 11101010 7aec r ea
0111101011101101 11101010 7aed r ea
0111101011101101 11101010 7aed r ea