简体   繁体   中英

Is there a cleaner way to do this loop?

Can any one help me come up with a solution that makes this loop simpler. The code runs fine but just seems too much for what it does.

    font_sizes = map(int, re.findall(r'\\fs(\d+)', rtf)) # Get all the font sizes
    # Set font sizes
    for font_size in font_sizes:
        font_size = (font_size / 2)
        if font_size <= 8:
                rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(1))
        else:
            if 9 <= font_size <= 11:
                rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(2))
            else:
                if 12 <= font_size <= 13:
                    rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(3))
                else:
                    if 14 <= font_size <= 17:
                        rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(4))
                    else:
                        if 18 <= font_size <= 31:
                            rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(5))
                        else:
                            if 32 <= font_size <= 34:
                                rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(6))
                            else:
                                if 35 <= font_size <= 59:
                                    rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(7))
                                else:
                                    if font_size > 60:
                                        rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(8))
                                    else:
                                        pass

First of all, you can use elif to avoid having to nest so deeply:

if font_size <= 8:
    rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(1))

elif 9 <= font_size <= 11:
    rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(2))

elif 12 <= font_size <= 13:
    rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(3))

# etc

You can drop the else: pass altogether, else is optional and you should just omit it when not used.

Next, factor out the common element; the str.replace() call; all that differs between them is the replacement fontsize, pick just that in each branch:

if font_size <= 8:
    size = 1
elif 9 <= font_size <= 11:
    size = 2
elif 12 <= font_size <= 13:
    size = 3
# etc.

rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(size))

Far more readable as you are not repeating yourself all the time.

You can remove the lower bound each time, as you already eliminated the lower bound with the preceding test:

if font_size <= 8:
    size = 1
elif font_size <= 11:
    size = 2
elif font_size <= 13:
    size = 3
# etc.

Now all you have is a bisection problem ; finding the size to go with a font-size is as simple as finding it's insertion point into a list of font_sizes; where you'd insert it is an index between 0 and 7, so you'd add 1:

import bisect

font_sizes = [8, 11, 13, 17, 31, 34, 59]
size = bisect.bisect_left(font_sizes, font_size) + 1
rtf = rtf.replace("\\fs{0}".format(font_size * 2), "<font size='{0}'>".format(size))

I'm not sure why you are dividing the font sizes by two; the above 3 lines work just fine with the font size not divided.

Since you are replacing elements, you should really use re.sub() here, using a function to produce the replacement:

def insert_fontsize(match):
    font_sizes = [16, 22, 26, 34, 62, 68, 118]
    size = bisect.bisect_left(font_sizes, int(match.group(1))) + 1
    return '<font size="{0}">'.format(size)

re.sub(r'\\fs(\d+)', insert_fontsize, rtf)

Now the re.sub() call will do the looping, rather than you loop and then do manual replacements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM