Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issues #24 and #25 #26

Merged
merged 3 commits into from
Nov 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions mm/document_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def write(self, filename):
self.composer = ComposerPrettyTable(self.data_model, self.grid, self)
log.info("Setting output format to TXT, based on file extension")

f = open(filename, "wb")
f.write(self.writestr())
f.close()
with open(filename, "wb") as f:
f.write(self.writestr())

log.info("wrote file: %s" % filename)

def write_gdata(self, name, username, password, auth_token=None):
Expand Down
6 changes: 3 additions & 3 deletions scripts/textwidths.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ def get_width(font, char_str):

# write in binary pickle mode
import cPickle as pickle
f = open("font_data.bin","wb")
pickle.dump(fonts,f,True)
f.close()
with open("font_data.bin","wb") as f:
pickle.dump(fonts,f,True)




51 changes: 24 additions & 27 deletions tests/basic_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ def test_minimal(self):
self.assertTrue(
len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_doc.xls", "wb")
f.write(str)
f.close()
self.check("test_doc.xls", my_data)
with open("tests/generated_files/test_doc.xls", "wb") as f:
f.write(str)
self.check("tests/generated_files/test_doc.xls", my_data)

def test_minimal_lists(self):

Expand All @@ -49,11 +48,11 @@ def test_minimal_lists(self):
self.assertTrue(
len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_list_doc.xls", "wb")
f.write(str)
f.close()
with open("tests/generated_files/test_list_doc.xls", "wb") as f:
f.write(str)

as_dict = [dict(zip(my_headers, row)) for row in my_data]
self.check("test_list_doc.xls", as_dict)
self.check("tests/generated_files/test_list_doc.xls", as_dict)

def check(self, filename, my_data):
xls = XLSReader(filename)
Expand Down Expand Up @@ -99,11 +98,10 @@ def test_mid_complex(self):
self.assertTrue(
len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_doc2.xls", "wb")
f.write(str)
f.close()
with open("tests/generated_files/test_doc2.xls", "wb") as f:
f.write(str)

#TODO self.check("test_doc2.xls", my_data)
#TODO self.check("tests/generated_files/test_doc2.xls", my_data)

def test_image(self):

Expand All @@ -117,8 +115,8 @@ def test_image(self):
self.assertTrue(
len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_doc_image.xls", "wb")
f.write(str)
with open("tests/generated_files/test_doc_image.xls", "wb") as f:
f.write(str)

def test_col_type(self):

Expand All @@ -133,8 +131,8 @@ def test_col_type(self):
self.assertTrue(
len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_col_types.xls", "wb")
f.write(str)
with open("tests/generated_files/test_col_types.xls", "wb") as f:
f.write(str)

def test_missing_1(self):
my_data = [
Expand All @@ -154,10 +152,9 @@ def test_missing_1(self):
self.assertTrue(
len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_doc.xls", "wb")
f.write(str)
f.close()
self.check("test_doc.xls", my_data)
with open("tests/generated_files/test_doc.xls", "wb") as f:
f.write(str)
self.check("tests/generated_files/test_doc.xls", my_data)

def test_missing_2(self):
my_data = [
Expand All @@ -181,10 +178,9 @@ def test_missing_2(self):
self.assertTrue(
len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_doc.xls", "wb")
f.write(str)
f.close()
self.check("test_doc.xls", my_data)
with open("test_doc.xls", "wb") as f:
f.write(str)
self.check("tests/generated_files/test_doc.xls", my_data)

def test_write_and_writestr_binary_output(self):
"""
Expand All @@ -205,14 +201,15 @@ def test_write_and_writestr_binary_output(self):

# Write using write()
mm_doc = mm.Document(my_data)
mm_doc.write('test_file1.xls')
mm_doc.write("tests/generated_files/test_file1.xls")

# Write using writestr() in binary mode
str = mm_doc.writestr()
with open('test_file2.xls', 'wb') as f:
with open("tests/generated_files/test_file2.xls", "wb") as f:
f.write(str)

self.assertTrue(filecmp.cmp('test_file1.xls', 'test_file2.xls'))
self.assertTrue(filecmp.cmp("tests/generated_files/test_file1.xls",
"tests/generated_files/test_file2.xls"))

if __name__ == "__main__":
unittest.main()
18 changes: 9 additions & 9 deletions tests/customize_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ def test_no_header_row(self):
str = mm_doc.writestr()
self.assertTrue(len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_custom_no_header.xls", "wb")
f.write(str)
f.close()
with open("tests/generated_files/test_custom_no_header.xls", "wb") as f:
f.write(str)



def test_no_style(self):
Expand All @@ -49,9 +49,9 @@ def test_no_style(self):
str = mm_doc.writestr()
self.assertTrue(len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_custom_no_styles.xls", "wb")
f.write(str)
f.close()
with open("tests/generated_files/test_custom_no_styles.xls", "wb") as f:
f.write(str)



def test_row_style(self):
Expand All @@ -64,9 +64,9 @@ def test_row_style(self):
str = mm_doc.writestr()
self.assertTrue(len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_custom_row_styles.xls", "wb")
f.write(str)
f.close()
with open("tests/generated_files/test_custom_row_styles.xls", "wb") as f:
f.write(str)



if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions tests/django_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def test_custom_django_serializer(self):
str = mm_doc.writestr()
self.assertTrue(len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_django_serializer.xls", "wb")
f.write(str)
f.close()
with open("tests/generated_files/test_django_serializer.xls", "wb") as f:
f.write(str)




Expand Down
8 changes: 4 additions & 4 deletions tests/dup_type_fix_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def test_minimal(self):
str = mm_doc.writestr()
self.assertTrue(len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_dup.xls", "wb")
f.write(str)
f.close()
self.check("test_dup.xls", my_data)
with open("tests/generated_files/test_dup.xls", "wb") as f:
f.write(str)

self.check("tests/generated_files/test_dup.xls", my_data)

def check(self, filename, my_data):
xls = XLSReader(filename)
Expand Down
4 changes: 4 additions & 0 deletions tests/generated_files/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
8 changes: 4 additions & 4 deletions tests/more_data_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def test_minimal(self):
self.assertTrue(
len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_multi_data.xls", "wb")
f.write(str)
f.close()
self.check("test_multi_data.xls", my_data)
with open("tests/generated_files/test_multi_data.xls", "wb") as f:
f.write(str)

self.check("tests/generated_files/test_multi_data.xls", my_data)

def check(self, filename, my_data):
xls = XLSReader(filename)
Expand Down
8 changes: 4 additions & 4 deletions tests/multi_sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ def test_minimal(self):
self.assertTrue(
len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_multi.xls", "wb")
f.write(str)
f.close()
self.check("test_multi.xls", my_data)
with open("tests/generated_files/test_multi.xls", "wb") as f:
f.write(str)

self.check("tests/generated_files/test_multi.xls", my_data)

def check(self, filename, my_data):
xls = XLSReader(filename)
Expand Down
8 changes: 4 additions & 4 deletions tests/psycopg2_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def test_psycopg(self):
self.assertTrue(
len(str) > 10,
msg="String should be longer than %s" % len(str))
f = open("test_psycopg.xls", "wb")
f.write(str)
f.close()
self.check("test_psycopg.xls", my_data)
with open("tests/generated_files/test_psycopg.xls", "wb") as f:
f.write(str)

self.check("tests/generated_files/test_psycopg.xls", my_data)

def check(self, filename, my_data):
xls = XLSReader(filename)
Expand Down