Skip to content

Instantly share code, notes, and snippets.

@whilp
Created August 11, 2012 22:10
Show Gist options
  • Save whilp/3327528 to your computer and use it in GitHub Desktop.
Save whilp/3327528 to your computer and use it in GitHub Desktop.
Data formatting
#!/usr/bin/env python
# unicode_literals + unittest2 will give you handy diffs when the test methods fail.
from __future__ import unicode_literals
try:
import unittest2 as unittest
except ImportError:
import unittest
def formatter(data):
return "%9d%5d%5d%5d%5d%5d %.11E %.11E %.11E %.11E %.11E %.0F %3.0F\n" % tuple(data)
class Test(unittest.TestCase):
cases = [
[
# An array of the actual data.
[ -1, -1, 0, 0, 0, 501, 0.0, 0.0, -52.872156547, 52.872156547, 0.0, 0.0,1.0],
# The desired string representation.
""" -1 -1 0 0 0 501 0.00000000000E+00 0.00000000000E+00 -0.52872156547E+02 0.52872156547E+02 0.00000000000E+00 0. 1.\n"""
]
]
# Add a test method for each (data, result) pair in Test.cases.
for i, case in enumerate(Test.cases):
(data, result) = case
def test_formatter(self):
self.assertEqual(result, formatter(data))
setattr(Test, "test_data_%03d" % i, test_formatter)
if __name__ == "__main__":
# Run the test.
unittest.main()
python formatter.py
F
======================================================================
FAIL: test_data_000 (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/devin.py", line 28, in test_formatter
self.assertEqual(result, formatter(data))
AssertionError: u' -1 -1 0 0 0 501 0.00000000000E+00 0.00000000000E+00 -0.52 [truncated]... != u' -1 -1 0 0 0 501 0.00000000000E+00 0.00000000000E+00 -5.28 [truncated]...
- -1 -1 0 0 0 501 0.00000000000E+00 0.00000000000E+00 -0.52872156547E+02 0.52872156547E+02 0.00000000000E+00 0. 1.
? ^ - ^^^^ - ^ ^ -
+ -1 -1 0 0 0 501 0.00000000000E+00 0.00000000000E+00 -5.28721565470E+01 5.28721565470E+01 0.00000000000E+00 0 1
? ^ + ^^^^ + ^ ^
----------------------------------------------------------------------
Ran 1 test in 0.004s
FAILED (failures=1)
@whilp
Copy link
Author

whilp commented Aug 11, 2012

So, results.txt suggests I'm close. I can't get the stuff towards the end to work, but you should be able to use the above to test alternatives and different data. Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment