/astropy/io/ascii/tests/test_fixedwidth.py

https://github.com/jiffyclub/astropy
Python | 408 lines | 355 code | 18 blank | 35 comment | 6 complexity | d49639b4dafdf8879d8f7d97300e1faf MD5 | raw file
  1. import re
  2. import glob
  3. import numpy as np
  4. from ... import ascii as asciitable
  5. io = asciitable.core.io
  6. from .common import (raises,
  7. assert_equal, assert_almost_equal, assert_true,
  8. setup_function, teardown_function)
  9. def assert_equal_splitlines(arg1, arg2):
  10. assert_equal(arg1.splitlines(), arg2.splitlines())
  11. def test_read_normal():
  12. """Nice, typical fixed format table"""
  13. table = """
  14. # comment (with blank line above)
  15. | Col1 | Col2 |
  16. | 1.2 | "hello" |
  17. | 2.4 |'s worlds|
  18. """
  19. reader = asciitable.get_reader(Reader=asciitable.FixedWidth)
  20. dat = reader.read(table)
  21. assert_equal(reader.header.colnames, ('Col1', 'Col2'))
  22. assert_almost_equal(dat[1][0], 2.4)
  23. assert_equal(dat[0][1], '"hello"')
  24. assert_equal(dat[1][1], "'s worlds")
  25. def test_read_normal_names():
  26. """Nice, typical fixed format table with col names provided"""
  27. table = """
  28. # comment (with blank line above)
  29. | Col1 | Col2 |
  30. | 1.2 | "hello" |
  31. | 2.4 |'s worlds|
  32. """
  33. reader = asciitable.get_reader(Reader=asciitable.FixedWidth,
  34. names=('name1', 'name2'))
  35. dat = reader.read(table)
  36. assert_equal(reader.header.colnames, ('name1', 'name2'))
  37. assert_almost_equal(dat[1][0], 2.4)
  38. def test_read_normal_names_include():
  39. """Nice, typical fixed format table with col names provided"""
  40. table = """
  41. # comment (with blank line above)
  42. | Col1 | Col2 | Col3 |
  43. | 1.2 | "hello" | 3 |
  44. | 2.4 |'s worlds| 7 |
  45. """
  46. reader = asciitable.get_reader(Reader=asciitable.FixedWidth,
  47. names=('name1', 'name2', 'name3'),
  48. include_names=('name1', 'name3'))
  49. dat = reader.read(table)
  50. assert_equal(reader.header.colnames, ('name1', 'name3'))
  51. assert_almost_equal(dat[1][0], 2.4)
  52. assert_equal(dat[0][1], 3)
  53. def test_read_normal_exclude():
  54. """Nice, typical fixed format table with col name excluded"""
  55. table = """
  56. # comment (with blank line above)
  57. | Col1 | Col2 |
  58. | 1.2 | "hello" |
  59. | 2.4 |'s worlds|
  60. """
  61. reader = asciitable.get_reader(Reader=asciitable.FixedWidth,
  62. exclude_names=('Col1',))
  63. dat = reader.read(table)
  64. assert_equal(reader.header.colnames, ('Col2',))
  65. assert_equal(dat[1][0], "'s worlds")
  66. def test_read_weird():
  67. """Weird input table with data values chopped by col extent """
  68. table = """
  69. Col1 | Col2 |
  70. 1.2 "hello"
  71. 2.4 sdf's worlds
  72. """
  73. reader = asciitable.get_reader(Reader=asciitable.FixedWidth)
  74. dat = reader.read(table)
  75. assert_equal(reader.header.colnames, ('Col1', 'Col2'))
  76. assert_almost_equal(dat[1][0], 2.4)
  77. assert_equal(dat[0][1], '"hel')
  78. assert_equal(dat[1][1], "df's wo")
  79. def test_read_double():
  80. """Table with double delimiters"""
  81. table = """
  82. || Name || Phone || TCP||
  83. | John | 555-1234 |192.168.1.10X|
  84. | Mary | 555-2134 |192.168.1.12X|
  85. | Bob | 555-4527 | 192.168.1.9X|
  86. """
  87. dat = asciitable.read(table, Reader=asciitable.FixedWidth, guess=False)
  88. assert_equal(tuple(dat.dtype.names), ('Name', 'Phone', 'TCP'))
  89. assert_equal(dat[1][0], "Mary")
  90. assert_equal(dat[0][1], "555-1234")
  91. assert_equal(dat[2][2], "192.168.1.9")
  92. def test_read_space_delimiter():
  93. """Table with space delimiter"""
  94. table = """
  95. Name --Phone- ----TCP-----
  96. John 555-1234 192.168.1.10
  97. Mary 555-2134 192.168.1.12
  98. Bob 555-4527 192.168.1.9
  99. """
  100. dat = asciitable.read(table, Reader=asciitable.FixedWidth, guess=False,
  101. delimiter=' ')
  102. assert_equal(tuple(dat.dtype.names), ('Name', '--Phone-', '----TCP-----'))
  103. assert_equal(dat[1][0], "Mary")
  104. assert_equal(dat[0][1], "555-1234")
  105. assert_equal(dat[2][2], "192.168.1.9")
  106. def test_read_no_header_autocolumn():
  107. """Table with no header row and auto-column naming"""
  108. table = """
  109. | John | 555-1234 |192.168.1.10|
  110. | Mary | 555-2134 |192.168.1.12|
  111. | Bob | 555-4527 | 192.168.1.9|
  112. """
  113. dat = asciitable.read(table, Reader=asciitable.FixedWidth, guess=False,
  114. header_start=None, data_start=0)
  115. assert_equal(tuple(dat.dtype.names), ('col1', 'col2', 'col3'))
  116. assert_equal(dat[1][0], "Mary")
  117. assert_equal(dat[0][1], "555-1234")
  118. assert_equal(dat[2][2], "192.168.1.9")
  119. def test_read_no_header_names():
  120. """Table with no header row and with col names provided. Second
  121. and third rows also have hanging spaces after final |."""
  122. table = """
  123. | John | 555-1234 |192.168.1.10|
  124. | Mary | 555-2134 |192.168.1.12|
  125. | Bob | 555-4527 | 192.168.1.9|
  126. """
  127. dat = asciitable.read(table, Reader=asciitable.FixedWidth, guess=False,
  128. header_start=None, data_start=0,
  129. names=('Name', 'Phone', 'TCP'))
  130. assert_equal(tuple(dat.dtype.names), ('Name', 'Phone', 'TCP'))
  131. assert_equal(dat[1][0], "Mary")
  132. assert_equal(dat[0][1], "555-1234")
  133. assert_equal(dat[2][2], "192.168.1.9")
  134. def test_read_no_header_autocolumn_NoHeader():
  135. """Table with no header row and auto-column naming"""
  136. table = """
  137. | John | 555-1234 |192.168.1.10|
  138. | Mary | 555-2134 |192.168.1.12|
  139. | Bob | 555-4527 | 192.168.1.9|
  140. """
  141. dat = asciitable.read(table, Reader=asciitable.FixedWidthNoHeader)
  142. assert_equal(tuple(dat.dtype.names), ('col1', 'col2', 'col3'))
  143. assert_equal(dat[1][0], "Mary")
  144. assert_equal(dat[0][1], "555-1234")
  145. assert_equal(dat[2][2], "192.168.1.9")
  146. def test_read_no_header_names_NoHeader():
  147. """Table with no header row and with col names provided. Second
  148. and third rows also have hanging spaces after final |."""
  149. table = """
  150. | John | 555-1234 |192.168.1.10|
  151. | Mary | 555-2134 |192.168.1.12|
  152. | Bob | 555-4527 | 192.168.1.9|
  153. """
  154. dat = asciitable.read(table, Reader=asciitable.FixedWidthNoHeader,
  155. names=('Name', 'Phone', 'TCP'))
  156. assert_equal(tuple(dat.dtype.names), ('Name', 'Phone', 'TCP'))
  157. assert_equal(dat[1][0], "Mary")
  158. assert_equal(dat[0][1], "555-1234")
  159. assert_equal(dat[2][2], "192.168.1.9")
  160. def test_read_col_starts():
  161. """Table with no delimiter with column start and end values specified."""
  162. table = """
  163. # 5 9 17 18 28
  164. # | | || |
  165. John 555- 1234 192.168.1.10
  166. Mary 555- 2134 192.168.1.12
  167. Bob 555- 4527 192.168.1.9
  168. """
  169. dat = asciitable.read(table, Reader=asciitable.FixedWidthNoHeader,
  170. names=('Name', 'Phone', 'TCP'),
  171. col_starts=(0, 9, 18),
  172. col_ends=(5, 17, 28),
  173. )
  174. assert_equal(tuple(dat.dtype.names), ('Name', 'Phone', 'TCP'))
  175. assert_equal(dat[0][1], "555- 1234")
  176. assert_equal(dat[1][0], "Mary")
  177. assert_equal(dat[1][2], "192.168.1.")
  178. assert_equal(dat[2][2], "192.168.1") # col_end=28 cuts this column off
  179. table = """\
  180. | Col1 | Col2 | Col3 | Col4 |
  181. | 1.2 | "hello" | 1 | a |
  182. | 2.4 | 's worlds | 2 | 2 |
  183. """
  184. dat = asciitable.read(table, Reader=asciitable.FixedWidth)
  185. def test_write_normal():
  186. """Write a table as a normal fixed width table."""
  187. out = io.StringIO()
  188. asciitable.write(dat, out, Writer=asciitable.FixedWidth)
  189. assert_equal_splitlines(out.getvalue(), """\
  190. | Col1 | Col2 | Col3 | Col4 |
  191. | 1.2 | "hello" | 1 | a |
  192. | 2.4 | 's worlds | 2 | 2 |
  193. """)
  194. def test_write_no_pad():
  195. """Write a table as a fixed width table with no padding."""
  196. out = io.StringIO()
  197. asciitable.write(dat, out, Writer=asciitable.FixedWidth,
  198. delimiter_pad=None)
  199. assert_equal_splitlines(out.getvalue(), """\
  200. |Col1| Col2|Col3|Col4|
  201. | 1.2| "hello"| 1| a|
  202. | 2.4|'s worlds| 2| 2|
  203. """)
  204. def test_write_no_bookend():
  205. """Write a table as a fixed width table with no bookend."""
  206. out = io.StringIO()
  207. asciitable.write(dat, out, Writer=asciitable.FixedWidth, bookend=False)
  208. assert_equal_splitlines(out.getvalue(), """\
  209. Col1 | Col2 | Col3 | Col4
  210. 1.2 | "hello" | 1 | a
  211. 2.4 | 's worlds | 2 | 2
  212. """)
  213. def test_write_no_delimiter():
  214. """Write a table as a fixed width table with no delimiter."""
  215. out = io.StringIO()
  216. asciitable.write(dat, out, Writer=asciitable.FixedWidth, bookend=False,
  217. delimiter=None)
  218. assert_equal_splitlines(out.getvalue(), """\
  219. Col1 Col2 Col3 Col4
  220. 1.2 "hello" 1 a
  221. 2.4 's worlds 2 2
  222. """)
  223. def test_write_noheader_normal():
  224. """Write a table as a normal fixed width table."""
  225. out = io.StringIO()
  226. asciitable.write(dat, out, Writer=asciitable.FixedWidthNoHeader)
  227. assert_equal_splitlines(out.getvalue(), """\
  228. | 1.2 | "hello" | 1 | a |
  229. | 2.4 | 's worlds | 2 | 2 |
  230. """)
  231. def test_write_noheader_no_pad():
  232. """Write a table as a fixed width table with no padding."""
  233. out = io.StringIO()
  234. asciitable.write(dat, out, Writer=asciitable.FixedWidthNoHeader,
  235. delimiter_pad=None)
  236. assert_equal_splitlines(out.getvalue(), """\
  237. |1.2| "hello"|1|a|
  238. |2.4|'s worlds|2|2|
  239. """)
  240. def test_write_noheader_no_bookend():
  241. """Write a table as a fixed width table with no bookend."""
  242. out = io.StringIO()
  243. asciitable.write(dat, out, Writer=asciitable.FixedWidthNoHeader,
  244. bookend=False)
  245. assert_equal_splitlines(out.getvalue(), """\
  246. 1.2 | "hello" | 1 | a
  247. 2.4 | 's worlds | 2 | 2
  248. """)
  249. def test_write_noheader_no_delimiter():
  250. """Write a table as a fixed width table with no delimiter."""
  251. out = io.StringIO()
  252. asciitable.write(dat, out, Writer=asciitable.FixedWidthNoHeader, bookend=False,
  253. delimiter=None)
  254. assert_equal_splitlines(out.getvalue(), """\
  255. 1.2 "hello" 1 a
  256. 2.4 's worlds 2 2
  257. """)
  258. def test_write_formats():
  259. """Write a table as a fixed width table with no delimiter."""
  260. out = io.StringIO()
  261. asciitable.write(dat, out, Writer=asciitable.FixedWidth,
  262. formats={'Col1': '%-8.3f', 'Col2': '%-15s'})
  263. assert_equal_splitlines(out.getvalue(), """\
  264. | Col1 | Col2 | Col3 | Col4 |
  265. | 1.200 | "hello" | 1 | a |
  266. | 2.400 | 's worlds | 2 | 2 |
  267. """)
  268. def test_read_twoline_normal():
  269. """Typical fixed format table with two header lines (with some cruft
  270. thrown in to test column positioning"""
  271. table = """
  272. Col1 Col2
  273. ---- ---------
  274. 1.2xx"hello"
  275. 2.4 's worlds
  276. """
  277. dat = asciitable.read(table, Reader=asciitable.FixedWidthTwoLine)
  278. assert_equal(dat.dtype.names, ('Col1', 'Col2'))
  279. assert_almost_equal(dat[1][0], 2.4)
  280. assert_equal(dat[0][1], '"hello"')
  281. assert_equal(dat[1][1], "'s worlds")
  282. def test_read_twoline_ReST():
  283. """Read restructured text table"""
  284. table = """
  285. ======= ===========
  286. Col1 Col2
  287. ======= ===========
  288. 1.2 "hello"
  289. 2.4 's worlds
  290. ======= ===========
  291. """
  292. dat = asciitable.read(table, Reader=asciitable.FixedWidthTwoLine,
  293. header_start=1, position_line=2, data_end=-1)
  294. assert_equal(dat.dtype.names, ('Col1', 'Col2'))
  295. assert_almost_equal(dat[1][0], 2.4)
  296. assert_equal(dat[0][1], '"hello"')
  297. assert_equal(dat[1][1], "'s worlds")
  298. def test_read_twoline_human():
  299. """Read text table designed for humans and test having position line
  300. before the header line"""
  301. table = """
  302. +------+----------+
  303. | Col1 | Col2 |
  304. +------|----------+
  305. | 1.2 | "hello" |
  306. | 2.4 | 's worlds|
  307. +------+----------+
  308. """
  309. dat = asciitable.read(table, Reader=asciitable.FixedWidthTwoLine,
  310. delimiter='+',
  311. header_start=1, position_line=0,
  312. data_start=3, data_end=-1)
  313. assert_equal(dat.dtype.names, ('Col1', 'Col2'))
  314. assert_almost_equal(dat[1][0], 2.4)
  315. assert_equal(dat[0][1], '"hello"')
  316. assert_equal(dat[1][1], "'s worlds")
  317. def test_write_twoline_normal():
  318. """Write a table as a normal fixed width table."""
  319. out = io.StringIO()
  320. asciitable.write(dat, out, Writer=asciitable.FixedWidthTwoLine)
  321. assert_equal_splitlines(out.getvalue(), """\
  322. Col1 Col2 Col3 Col4
  323. ---- --------- ---- ----
  324. 1.2 "hello" 1 a
  325. 2.4 's worlds 2 2
  326. """)
  327. def test_write_twoline_no_pad():
  328. """Write a table as a fixed width table with no padding."""
  329. out = io.StringIO()
  330. asciitable.write(dat, out, Writer=asciitable.FixedWidthTwoLine,
  331. delimiter_pad=' ', position_char='=')
  332. assert_equal_splitlines(out.getvalue(), """\
  333. Col1 Col2 Col3 Col4
  334. ==== ========= ==== ====
  335. 1.2 "hello" 1 a
  336. 2.4 's worlds 2 2
  337. """)
  338. def test_write_twoline_no_bookend():
  339. """Write a table as a fixed width table with no bookend."""
  340. out = io.StringIO()
  341. asciitable.write(dat, out, Writer=asciitable.FixedWidthTwoLine,
  342. bookend=True, delimiter='|')
  343. assert_equal_splitlines(out.getvalue(), """\
  344. |Col1| Col2|Col3|Col4|
  345. |----|---------|----|----|
  346. | 1.2| "hello"| 1| a|
  347. | 2.4|'s worlds| 2| 2|
  348. """)