Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I don't know where outtext comes from, but this seems overly complicated. There's too much nesting, and there's not enough separation of concerns.

Let's back up. We want to (a) chunk our list into sizes of cutoff and (b) write each such chunk into a new file. For each chunk, we create a new file, and write all of the rows. Each of those problems is bite-size and manageable.

First, from How do you split a list into evenly sized chunks? How do you split a list into evenly sized chunks?:

def chunks(l, n):
 """Yield successive n-sized chunks from l."""
 for i in xrange(0, len(l), n):
 yield l[i:i+n]

That gives us the first half of the problem:

for chunk in chunks(outtext, cutoff):
 # write chunk

Now for the second half, we just need an index. But rather than keeping our own counter, let's use enumerate(). Additionally, note that csvwriter has a writerows() method - so we don't need a loop for that.

Putting it all together:

for index, chunk in enumerate(chunks(outtext, cutoff)):
 output_file = '{}{}.{}'.format(output_file_tokens[0], index, output_file_tokens[1])
 with open(output_file, 'wb') as f:
 writer = csv.writer(f)
 writer.writerows(chunk)
 print ">>> " + output_file + " successfully saved"

You don't need to del anything. That's going to be pretty slow too - everytime we delete a chunk we have to shift everything in memory. This way, we don't have to do that.

I don't know where outtext comes from, but this seems overly complicated. There's too much nesting, and there's not enough separation of concerns.

Let's back up. We want to (a) chunk our list into sizes of cutoff and (b) write each such chunk into a new file. For each chunk, we create a new file, and write all of the rows. Each of those problems is bite-size and manageable.

First, from How do you split a list into evenly sized chunks?:

def chunks(l, n):
 """Yield successive n-sized chunks from l."""
 for i in xrange(0, len(l), n):
 yield l[i:i+n]

That gives us the first half of the problem:

for chunk in chunks(outtext, cutoff):
 # write chunk

Now for the second half, we just need an index. But rather than keeping our own counter, let's use enumerate(). Additionally, note that csvwriter has a writerows() method - so we don't need a loop for that.

Putting it all together:

for index, chunk in enumerate(chunks(outtext, cutoff)):
 output_file = '{}{}.{}'.format(output_file_tokens[0], index, output_file_tokens[1])
 with open(output_file, 'wb') as f:
 writer = csv.writer(f)
 writer.writerows(chunk)
 print ">>> " + output_file + " successfully saved"

You don't need to del anything. That's going to be pretty slow too - everytime we delete a chunk we have to shift everything in memory. This way, we don't have to do that.

I don't know where outtext comes from, but this seems overly complicated. There's too much nesting, and there's not enough separation of concerns.

Let's back up. We want to (a) chunk our list into sizes of cutoff and (b) write each such chunk into a new file. For each chunk, we create a new file, and write all of the rows. Each of those problems is bite-size and manageable.

First, from How do you split a list into evenly sized chunks?:

def chunks(l, n):
 """Yield successive n-sized chunks from l."""
 for i in xrange(0, len(l), n):
 yield l[i:i+n]

That gives us the first half of the problem:

for chunk in chunks(outtext, cutoff):
 # write chunk

Now for the second half, we just need an index. But rather than keeping our own counter, let's use enumerate(). Additionally, note that csvwriter has a writerows() method - so we don't need a loop for that.

Putting it all together:

for index, chunk in enumerate(chunks(outtext, cutoff)):
 output_file = '{}{}.{}'.format(output_file_tokens[0], index, output_file_tokens[1])
 with open(output_file, 'wb') as f:
 writer = csv.writer(f)
 writer.writerows(chunk)
 print ">>> " + output_file + " successfully saved"

You don't need to del anything. That's going to be pretty slow too - everytime we delete a chunk we have to shift everything in memory. This way, we don't have to do that.

Source Link
Barry
  • 18.5k
  • 1
  • 40
  • 92

I don't know where outtext comes from, but this seems overly complicated. There's too much nesting, and there's not enough separation of concerns.

Let's back up. We want to (a) chunk our list into sizes of cutoff and (b) write each such chunk into a new file. For each chunk, we create a new file, and write all of the rows. Each of those problems is bite-size and manageable.

First, from How do you split a list into evenly sized chunks?:

def chunks(l, n):
 """Yield successive n-sized chunks from l."""
 for i in xrange(0, len(l), n):
 yield l[i:i+n]

That gives us the first half of the problem:

for chunk in chunks(outtext, cutoff):
 # write chunk

Now for the second half, we just need an index. But rather than keeping our own counter, let's use enumerate(). Additionally, note that csvwriter has a writerows() method - so we don't need a loop for that.

Putting it all together:

for index, chunk in enumerate(chunks(outtext, cutoff)):
 output_file = '{}{}.{}'.format(output_file_tokens[0], index, output_file_tokens[1])
 with open(output_file, 'wb') as f:
 writer = csv.writer(f)
 writer.writerows(chunk)
 print ">>> " + output_file + " successfully saved"

You don't need to del anything. That's going to be pretty slow too - everytime we delete a chunk we have to shift everything in memory. This way, we don't have to do that.

lang-py

AltStyle によって変換されたページ (->オリジナル) /