Skip to content

Commit 6caf913

Browse files
authored
Merge pull request #286 from Joshua-Dias-Barreto/dataframeCsv
Added writeToCsv: withColumnNames: withRowNames:
2 parents fa00a1d + 9671eee commit 6caf913

File tree

4 files changed

+142
-10
lines changed

4 files changed

+142
-10
lines changed

src/DataFrame-IO-Tests/DataFrameCsvWriterTest.class.st

+27
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,30 @@ DataFrameCsvWriterTest >> testWriteToCsvWithSeparatorTab [
9393
expected := TestCsvStrings tabQuoteCsvString.
9494
self assertCollection: actual lines equals: expected lines
9595
]
96+
97+
{ #category : #tests }
98+
DataFrameCsvWriterTest >> testWriteToCsvWithoutColumnNames [
99+
| actual expected |
100+
dataFrame writeToCsv: commaQuoteCsvFile withColumnNames: false.
101+
actual := self readFile: commaQuoteCsvFile.
102+
expected := TestCsvStrings commaQuoteCsvStringWithoutColumnNames.
103+
self assert: actual lines equals: expected lines
104+
]
105+
106+
{ #category : #tests }
107+
DataFrameCsvWriterTest >> testWriteToCsvWithoutRowNames [
108+
| actual expected |
109+
dataFrame writeToCsv: commaQuoteCsvFile withRowNames: false.
110+
actual := self readFile: commaQuoteCsvFile.
111+
expected := TestCsvStrings commaQuoteCsvStringWithoutRowNames.
112+
self assert: actual lines equals: expected lines
113+
]
114+
115+
{ #category : #tests }
116+
DataFrameCsvWriterTest >> testWriteToCsvWithoutRowNamesWithoutColumnNames [
117+
| actual expected |
118+
dataFrame writeToCsv: commaQuoteCsvFile withColumnNames: false withRowNames: false.
119+
actual := self readFile: commaQuoteCsvFile.
120+
expected := TestCsvStrings commaQuoteCsvStringWithoutRowNamesWithoutColumnNames.
121+
self assert: actual lines equals: expected lines
122+
]

src/DataFrame-IO-Tests/TestCsvStrings.class.st

+34
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,40 @@ TestCsvStrings class >> commaQuoteCsvString [
3737
'
3838
]
3939

40+
{ #category : #running }
41+
TestCsvStrings class >> commaQuoteCsvStringWithoutColumnNames [
42+
43+
^ '"1:10 am","2.4","true","rain"
44+
"1:30 am","0.5","true","rain"
45+
"1:50 am","-1.2","true","snow"
46+
"2:10 am","-2.3","false","-"
47+
"2:30 am","3.2","true","rain"
48+
'
49+
]
50+
51+
{ #category : #running }
52+
TestCsvStrings class >> commaQuoteCsvStringWithoutRowNames [
53+
54+
^ '"temperature","precipitation","type"
55+
"2.4","true","rain"
56+
"0.5","true","rain"
57+
"-1.2","true","snow"
58+
"-2.3","false","-"
59+
"3.2","true","rain"
60+
'
61+
]
62+
63+
{ #category : #running }
64+
TestCsvStrings class >> commaQuoteCsvStringWithoutRowNamesWithoutColumnNames [
65+
66+
^ '"2.4","true","rain"
67+
"0.5","true","rain"
68+
"-1.2","true","snow"
69+
"-2.3","false","-"
70+
"3.2","true","rain"
71+
'
72+
]
73+
4074
{ #category : #running }
4175
TestCsvStrings class >> dollarSignCsvString [
4276

src/DataFrame-IO/DataFrame.extension.st

+33-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,39 @@ DataFrame >> writeTo: aLocation using: aDataFrameWriter [
110110

111111
{ #category : #'*DataFrame-IO' }
112112
DataFrame >> writeToCsv: aFileReference [
113-
| writer |
114-
writer := DataFrameCsvWriter new.
115-
self writeTo: aFileReference using: writer
113+
114+
self
115+
writeToCsv: aFileReference
116+
withColumnNames: true
117+
withRowNames: true
118+
]
119+
120+
{ #category : #'*DataFrame-IO' }
121+
DataFrame >> writeToCsv: aFileReference withColumnNames: enableColumnNames [
122+
123+
self
124+
writeToCsv: aFileReference
125+
withColumnNames: enableColumnNames
126+
withRowNames: true
127+
]
128+
129+
{ #category : #'*DataFrame-IO' }
130+
DataFrame >> writeToCsv: aFileReference withColumnNames: enableColumnNames withRowNames: enableRowNames [
131+
| writer |
132+
writer := DataFrameCsvWriter new.
133+
enableColumnNames ifFalse: [ writer disableColumnName ].
134+
enableRowNames ifFalse: [ writer disableRowName ].
135+
self writeTo: aFileReference using: writer.
136+
137+
]
138+
139+
{ #category : #'*DataFrame-IO' }
140+
DataFrame >> writeToCsv: aFileReference withRowNames: enableRowNames [
141+
142+
self
143+
writeToCsv: aFileReference
144+
withColumnNames: true
145+
withRowNames: enableRowNames
116146
]
117147

118148
{ #category : #'*DataFrame-IO' }

src/DataFrame-IO/DataFrameCsvWriter.class.st

+48-7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,30 @@ Class {
55
'separator',
66
'lineEndConvention',
77
'fieldWriter',
8-
'rowNameEnabled'
8+
'rowNameEnabled',
9+
'columnNameEnabled'
910
],
1011
#category : #'DataFrame-IO-Core'
1112
}
1213

14+
{ #category : #accessing }
15+
DataFrameCsvWriter >> columnNameEnabled [
16+
17+
^ columnNameEnabled
18+
]
19+
20+
{ #category : #accessing }
21+
DataFrameCsvWriter >> columnNameEnabled: aBoolean [
22+
23+
columnNameEnabled := aBoolean = true
24+
]
25+
26+
{ #category : #accessing }
27+
DataFrameCsvWriter >> defaultColumnIndexEnabled [
28+
29+
^ true
30+
]
31+
1332
{ #category : #'accessing - field writers' }
1433
DataFrameCsvWriter >> defaultFieldWriter [
1534

@@ -36,12 +55,24 @@ DataFrameCsvWriter >> defaultSeparator [
3655
^ $,
3756
]
3857

58+
{ #category : #'enable/disable' }
59+
DataFrameCsvWriter >> disableColumnName [
60+
61+
self columnNameEnabled: false
62+
]
63+
3964
{ #category : #'enable/disable' }
4065
DataFrameCsvWriter >> disableRowName [
4166

4267
self rowNameEnabled: false
4368
]
4469

70+
{ #category : #'enable/disable' }
71+
DataFrameCsvWriter >> enableColumnName [
72+
73+
self columnNameEnabled: true
74+
]
75+
4576
{ #category : #'enable/disable' }
4677
DataFrameCsvWriter >> enableRowName [
4778

@@ -67,7 +98,8 @@ DataFrameCsvWriter >> initialize [
6798
separator := self defaultSeparator.
6899
lineEndConvention := self defaultLineEndConvention.
69100
fieldWriter := self defaultFieldWriter.
70-
rowNameEnabled := self defaultRowIndexEnabled
101+
rowNameEnabled := self defaultRowIndexEnabled.
102+
columnNameEnabled := self defaultColumnIndexEnabled
71103
]
72104

73105
{ #category : #accessing }
@@ -156,16 +188,15 @@ DataFrameCsvWriter >> useRawFieldWriter [
156188

157189
{ #category : #writing }
158190
DataFrameCsvWriter >> write: aDataFrame to: aFileReference [
159-
160191
| stream writer |
161192
stream := aFileReference writeStream.
162-
163193
writer := NeoCSVWriter on: stream.
164194
fieldWriter ifNotNil: [ writer fieldWriter: fieldWriter ].
165195
writer separator: self separator.
166196
writer lineEndConvention: self lineEndConvention.
167197

168-
self rowNameEnabled ifTrue: [
198+
self columnNameEnabled ifTrue: [
199+
self rowNameEnabled ifTrue: [
169200
writer
170201
writeField: '';
171202
writeSeparator ].
@@ -178,7 +209,17 @@ DataFrameCsvWriter >> write: aDataFrame to: aFileReference [
178209
writeField: row name;
179210
writeSeparator;
180211
nextPut: row ] ]
181-
ifFalse: [ aDataFrame do: [ :row | writer nextPut: row ] ].
212+
ifFalse: [ aDataFrame do: [ :row | writer nextPut: row ] ]]
213+
214+
ifFalse: [
215+
self rowNameEnabled ifTrue: [
216+
aDataFrame do: [ :row |
217+
writer
218+
writeField: row name;
219+
writeSeparator;
220+
nextPut: row ] ]
221+
ifFalse: [ aDataFrame do: [ :row | writer nextPut: row ] ] ].
222+
223+
writer close.
182224

183-
writer close
184225
]

0 commit comments

Comments
 (0)