Skip to content

Commit 1a96170

Browse files
committed
Implemented pretty-printing
1 parent 3b3cd64 commit 1a96170

File tree

14 files changed

+147
-0
lines changed

14 files changed

+147
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"separateMethodMetaAndSource" : false,
3+
"noMethodMetaData" : true,
4+
"useCypressPropertiesFile" : true
5+
}

DataFrame-PrettyPrint.package/DataPrettyPrinter.class/README.md

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
as yet unclassified
2+
eachColumnAlign: aDataFrame
3+
"Returns an array of characters specifying how each column should be aligned based on its type. Possible values are #LEFT, #RIGHT, and #CENTER"
4+
5+
^ aDataFrame columnTypes collect: [ :type |
6+
((type inheritsFrom: String) or:
7+
(type == String))
8+
ifTrue: [ #LEFT ]
9+
ifFalse: [ #RIGHT ] ].
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
as yet unclassified
2+
eachColumnMaxLength: aDataFrame
3+
"For every column converts all values to strind and determines the length of the longest string"
4+
5+
| maxLenArray findMaxLen |
6+
7+
maxLenArray := Array new: (aDataFrame numberOfColumns + 1).
8+
9+
findMaxLen := [ :initMaxLen :column |
10+
| len maxLen |
11+
maxLen := initMaxLen.
12+
13+
(1 to: column size) do: [ :i |
14+
len := (column at: i) asString size.
15+
16+
len > maxLen
17+
ifTrue: [ maxLen := len ] ].
18+
maxLen ].
19+
20+
maxLenArray at: 1 put:
21+
(findMaxLen value: 0 value: aDataFrame rowNames).
22+
23+
1 to: aDataFrame numberOfColumns do: [ :i |
24+
| column |
25+
column := aDataFrame columnAt: i.
26+
27+
maxLenArray at: (i + 1) put:
28+
(findMaxLen
29+
value: column name asString size
30+
value: column) ].
31+
32+
^ maxLenArray
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
as yet unclassified
2+
offsetWithSpaces: aNumber
3+
"Generates a string filled with a given number of spaces"
4+
5+
^ (1 to: aNumber) inject: '' into: [ :offset :i |
6+
offset, ' ' ].
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
printing
2+
print: aDataFrame on: aStream
3+
"Pretty-prints a data frame"
4+
5+
columnLengths := self eachColumnMaxLength: aDataFrame.
6+
columnAligns := self eachColumnAlign: aDataFrame.
7+
separator := ' '.
8+
9+
self printRow: (aDataFrame columnNames) andKey: '' on: aStream.
10+
aStream cr.
11+
12+
self printLineOn: aStream.
13+
aStream cr.
14+
15+
1 to: aDataFrame numberOfRows do: [ :i |
16+
self printRow: i of: aDataFrame on: aStream.
17+
aStream cr ].
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
printing
2+
printLineOn: aStream
3+
4+
| numberOfColumns sumColLen sumOffset keySideLen valSideLen |
5+
6+
numberOfColumns := columnLengths size.
7+
sumColLen := columnLengths sum - (columnLengths at: 1).
8+
sumOffset := (separator size) * numberOfColumns.
9+
10+
keySideLen := (columnLengths at: 1) + (separator size).
11+
valSideLen := sumColLen + sumOffset.
12+
13+
1 to: keySideLen do: [ :i |
14+
aStream nextPutAll: '-' ].
15+
16+
aStream nextPutAll: '+'.
17+
18+
1 to: valSideLen do: [ :i |
19+
aStream nextPutAll: '-' ].
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
printing
2+
printRow: aRow andKey: aKey on: aStream
3+
4+
| cellStr offset |
5+
6+
cellStr := aKey asString.
7+
8+
offset := self offsetWithSpaces:
9+
((columnLengths at: 1) - cellStr size).
10+
11+
(columnAligns at: 1) = #RIGHT
12+
ifTrue: [ cellStr := offset, cellStr ].
13+
(columnAligns at: 1) = #LEFT
14+
ifTrue: [ cellStr := cellStr, offset ].
15+
16+
aStream
17+
nextPutAll: cellStr;
18+
nextPutAll: separator;
19+
nextPutAll: '|';
20+
nextPutAll: separator.
21+
22+
1 to: aRow size do: [ :i |
23+
cellStr := (aRow at: i) asString.
24+
offset := self offsetWithSpaces:
25+
((columnLengths at: i + 1) - cellStr size).
26+
27+
(columnAligns at: i) = #RIGHT
28+
ifTrue: [ cellStr := offset, cellStr ].
29+
(columnAligns at: i) = #LEFT
30+
ifTrue: [ cellStr := cellStr, offset ].
31+
32+
aStream
33+
nextPutAll: cellStr;
34+
nextPutAll: separator. ].
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
printing
2+
printRow: aNumber of: aDataFrame on: aStream
3+
4+
| row key |
5+
row := aDataFrame rowAt: aNumber.
6+
key := (aDataFrame rowNames at: aNumber) asString.
7+
self printRow: row andKey: key on: aStream.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"commentStamp" : "",
3+
"super" : "Object",
4+
"category" : "DataFrame-PrettyPrint",
5+
"classinstvars" : [ ],
6+
"pools" : [ ],
7+
"classvars" : [ ],
8+
"instvars" : [
9+
"columnLengths",
10+
"columnAligns",
11+
"separator"
12+
],
13+
"name" : "DataPrettyPrinter",
14+
"type" : "normal"
15+
}

0 commit comments

Comments
 (0)