Skip to content

Streamline use of toListOf by adding method to print data class definition of table schema #344

@holgerbrandl

Description

@holgerbrandl

To streamline the transition from table rows to objects using df.toListOf<Something>(), it would be wonderful if kdf could print a schema (as data class definition) to standard out.

Currently, this is only possible by converting a data-frame into krangl-df (using https://github.com/holgerbrandl/kdfutils) and and using printDataClassSchema to print the schema:

import com.github.holgerbrandl.kdfutils.toKranglDF
import krangl.printDataClassSchema
import org.jetbrains.kotlinx.dataframe.api.toListOf
import org.jetbrains.kotlinx.dataframe.datasets.flightsData

fun main() {

    flightsData.toKranglDF().printDataClassSchema("FlightRecord")

    // note: the data class definition is created and printed by printDataClassSchema to stdout  
    data class FlightRecord(
        val year: Int,
        val month: Int,
        val day: Int,
        val dep_time: String?,
        val dep_delay: String?,
        val arr_time: String?,
        val arr_delay: String?,
        val carrier: String,
        val tailnum: String,
        val flight: Int,
        val origin: String,
        val dest: String,
        val air_time: String?,
        val distance: Int,
        val hour: String?,
        val minute: String?
    )

    val records = flightsData.toListOf<FlightRecord>()
    println(records.first())
}

which prints just

data class FlightRecord(val year: Int, val month: Int, val day: Int, val dep_time: Any?, val dep_delay: Any?, val arr_time: Any?, val arr_delay: Any?, val carrier: String, val tailnum: String, val flight: Int, val origin: String, val dest: String, val air_time: Any?, val distance: Int, val hour: Any?, val minute: Any?)

FlightRecord(year=2013, month=1, day=1, dep_time=517, dep_delay=2, arr_time=830, arr_delay=11, carrier=UA, tailnum=N14228, flight=1545, origin=EWR, dest=IAH, air_time=227, distance=1400, hour=5, minute=17)

Technically the approach suggested in #340 (comment) points into the same direction. However, having the ability to print a data-class definition of a data-frame would work more generally also in a simple kotlin jvm application without any compiler plugin or jupyter.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions