Python recursive string concatenation

1.7k views Asked by At

I'm using a python class to generate a HTML table used in a calendar daily view. To generate the table I recursively call my function until it reaches the desired end. I looks like so:

# By default time=[0][0] and end_time=[0][24]
---------------------------------------------
def makeRows(self, time, end_time): # time/end_time = [min][hour] 
        row = ['<tr><td>']
        if time[1] == end_time[1]:  # Base case
            # String format
            if time[0] == 0:
                row.append('%s:00' % time[1])
            else:
                row.append('%s:%s' % (time[1], time[0]))

            row.append('</td></tr>')

            return format_html(''.join(row))
        else:   # Recursive case

            if time[0] == 0 and time[1] == 0: # First row
                row.append('0:00')
                row.append('</td><td rowspan="97"><div class="day_event_container"></div></td></tr>')
            else:
                # String format
                if time[0] == 0:
                    row.append('%s:00' % time[1])
                else:
                    row.append('%s:%s' % (time[1], time[0]))

                row.append('</td></tr>')

            return format_html(''.join(row)+self.makeRows(self.increaseTime(time), end_time))

def increaseTime(self, time):
        hour = time[1]
        minute = time[0]

        if minute == 0:
            minute+= 30
        else:
            hour += 1
            minute = 0

        return [minute, hour]

I recently changed all my string concatenations from += to ''.join() but the only ''naive'' concatenation is in my recursive call.

The function is usually called 48 times to generate rows from 0:00 -> 24:00

Is the ''naive'' concatenation in my recursive call really that costly for 48 function calls?

How would I replace my ''naive'' concatenation if it actually is very costly?

I tried doing

return format_html(''.join(row).join(self.makeRows(self.increaseTime(time), end_time)))

but that throws Exception Type: MemoryError and I'm guessing that it just overflows the heap or stack or wherever its stored.

Lastly, I'm quite new to python and I'm 99% sure that this is very far from the pythonic way of doing things. So is there a more pythonic way to generate a daily calendar view from 0:00 to 24:00?

1

There are 1 answers

2
Anand S Kumar On

Seems to me like you are trying to replace -

format_html(''.join(row)+self.makeRows(self.increaseTime(time), end_time))

With

format_html(''.join(row).join(self.makeRows(self.increaseTime(time), end_time)))

But that is not correct, both return different things. Lets take a very simple example to explain -

>>> ''.join('Bye').join('Hello')
'HByeeByelByelByeo'
>>> ''.join('Bye') + 'Hello'
'ByeHello'

I think you should keep your current usage of + concatenation operator instead of trying to make it perform better , unless you are working with a very large list of strings to be concatenated, which does not seem to be that case.