Author: Siddharth Ravikumar <firstname.lastname@example.org>
Date: Thu, 30 Oct 2014 11:36:49 -0400
updated combox/file.py: renamed split_file -> split_data and glue_file -> glue_data
split_data: reads a string or a stream of bytes and splits them into `n' parts.
glue_data: reads an array of strings / stream of bytes and glues them into one string / stream of bytes.
read_file: reads content from file and returns it.
"Python has been an important part of Google since the beginning, and
remains so as the system grows and evolves. Today dozens of Google
engineers use Python, and we're looking for more people with skills in
this language." said Peter Norvig, director of search quality at
Google has employed a Gingrich-era Republican as its head lobbyist in
DC. Now, Google is effectively helping Comcast kill the Internet as we
know it, and end "Net Neutrality" forever. -- SumOfUs.org
Sign a petition to ask Google to stand up for Net Neutrality:
2 files changed, 46 insertions(+), 35 deletions(-)
diff --git a/combox/file.py b/combox/file.py
@@ -20,52 +20,63 @@ from os import path
from sys import exit
-def split_file(filename, n):
- """Split the file into `n' parts and return them as an array.
+def split_data(data, n):
+ """Split data into `n' parts and return them as an array.
- filename: Absolute pathname of the file.
+ data: Stream of bytes or string
n: Number of parts the file has to be split.
- file_ = None
- file_ = open(filename, 'rb')
- except IOError:
- print "ERROR: opening %s" % (filename)
- f_parts = 
+ d_parts = 
# File size in bytes.
- file_size = path.getsize(filename)
- # No. of bytes for each file part.
- part_size = file_size / n
- # Take note of remaining bytes; this is non-zero when file_size is
+ data_size = len(data)
+ # No. of bytes for each data part.
+ part_size = data_size / n
+ # Take note of remaining bytes; this is non-zero when data_size is
# not divisible by `n'.
- rem_bytes = file_size % n
+ rem_bytes = data_size % n
- i = 0
- while i < n:
- i += 1
+ start = 0
+ end = part_size
+ while end <= data_size:
+ start = end
+ end = end + part_size
- # read the remaining bytes into the last file part.
- f_parts[n-1] += file_.read(rem_bytes)
+ # read the remaining bytes into the last data part.
+ end += start + rem_bytes
+ d_parts[n-1] += data[start:end]
- return f_parts
+ return d_parts
- """Glue different parts of the file to one.
+ """Glue different parts of the data to one.
- f_parts: Array containing different parts of the file. Each part
+ f_parts: Array containing different parts of the data. Each part
is a sequence of bytes.
- file_content = ''
- for part in f_parts:
- file_content += part
+ data = ''
+ for part in d_parts:
+ data += part
+ return data
- return file_content
+ """Read file and return it as a string.
+ filename: Absolute pathname of the file.
+ file_ = None
+ file_ = open(filename, 'rb')
+ except IOError:
+ print "ERROR: opening %s" % (filename)
+ return file_.read()
def write_file(filename, filecontent):
@@ -74,7 +85,6 @@ def write_file(filename, filecontent):
filename: Absolute pathname of the file.
filecontent: String/bytstream to write to filename.
file_ = None
file_ = open(filename, 'wb')
diff --git a/combox/tests/test_file.py b/combox/tests/test_file.py
@@ -18,12 +18,13 @@
from os import path
-from file import split_file, glue_file, write_file
+from file import split_data, glue_data, write_file, read_file
### Test to split, glue and create a copy of the image file from the
### glued image file.
f = path.abspath('tests/files/the-red-star.jpg')
+f_content = read_file(f)
+f_parts = split_data(f_content, 5)
+f_content = glue_data(f_parts)
f_copy = path.abspath('tests/files/the-red-star-copy.jpg')
-f_parts = split_file(f, 3)
-filecontent = glue_file(f_parts)