1From Python to Kotlin @Horgix / #TalkingKT
5 March 2020
From Python to Kotlin
Feedback from some scripts re-writing
Alexis “Horgix” Chotard
2From Python to Kotlin @Horgix / #TalkingKT
Alexis “Horgix” Chotard
SRE & Coach DevOps
▼ 30% Development
▼ 40% Operations
▼ 30% Training, coaching, …
▼ Usually prototypes in Python
▼ First time Kotlin user in Oct. 2019
▼ Loves Rust
@Horgix
Horgix
3From Python to Kotlin @Horgix / #TalkingKT
Warning!
This is a personal feedback from a personal experience
Lot of bias from my own knowledge :)
4From Python to Kotlin @Horgix / #TalkingKT
Context & Usage
Talks/Speakers handling from GitHub files
5From Python to Kotlin @Horgix / #TalkingKT
Sample - Talks list
6From Python to Kotlin @Horgix / #TalkingKT
Sample - Talk file template
7From Python to Kotlin @Horgix / #TalkingKT
What do we want to do?
▼ Files “parsing” - YAML frontmatter + Markdown body
▼ “Error” detection : missing infos, non-coherent infos, etc.
▼ Some stats would be nice
▼ Will be run mainly manually or from a CI pipeline
8From Python to Kotlin @Horgix / #TalkingKT
First Python implementation
“Hey, let’s code something quick to make our life easier”
9From Python to Kotlin @Horgix / #TalkingKT
At first, a simple set of Python scripts
10From Python to Kotlin @Horgix / #TalkingKT
Then some “tests”
11From Python to Kotlin @Horgix / #TalkingKT
“Let’s wrap everything in a Makefile!”
12From Python to Kotlin @Horgix / #TalkingKT
“Well, that look hacky as f***…”
And indeed. It is was
13From Python to Kotlin @Horgix / #TalkingKT
Rewrite everything in Kotlin!
(for science)
14From Python to Kotlin @Horgix / #TalkingKT
Why Kotlin?
▼ Lot of momentum
▼ Looked cool and I wanted to play with it :)
▼ A lot of data collections computing that can all be done in memory
▽ Functional approach made easy by Kotlin
▽ Kotlin standard library looked like it could make things easier
15From Python to Kotlin @Horgix / #TalkingKT
Let’s compare some use cases
Mainly about language features, libraries and stuff
16From Python to Kotlin @Horgix / #TalkingKT
1. Talks format handling
and base language constructs
17From Python to Kotlin @Horgix / #TalkingKT
Talk formats - Storage & field format
18From Python to Kotlin @Horgix / #TalkingKT
Talk formats - Storage & field format
19From Python to Kotlin @Horgix / #TalkingKT
Talk formats - Storage & field format (raw)
20From Python to Kotlin @Horgix / #TalkingKT
Some wise words
21From Python to Kotlin @Horgix / #TalkingKT
Better than enums : Enum classes <3
22From Python to Kotlin @Horgix / #TalkingKT
Yes, there are (more limited) enums in Python too...
23From Python to Kotlin @Horgix / #TalkingKT
1bis. Talks format handling
What about getting our enum value from what we parse?
24From Python to Kotlin @Horgix / #TalkingKT
How to get a Enum value from our initial String
Python:
25From Python to Kotlin @Horgix / #TalkingKT
Pattern matching <3
Python:
Kotlin:
26From Python to Kotlin @Horgix / #TalkingKT
Function extensions <3
Python:
Kotlin:
27From Python to Kotlin @Horgix / #TalkingKT
2. File parsing
YAML frontmatter + Markdown body
28From Python to Kotlin @Horgix / #TalkingKT
File parsing in Python :) https://github.com/eyeseast/python-frontmatter
29From Python to Kotlin @Horgix / #TalkingKT
File parsing in Python :) https://github.com/eyeseast/python-frontmatter
30From Python to Kotlin @Horgix / #TalkingKT
File parsing in Kotlin :( https://github.com/atlassian/commonmark-java/
+ commonmark-ext-yaml-front-matterc
31From Python to Kotlin @Horgix / #TalkingKT
File parsing in Kotlin :( https://github.com/atlassian/commonmark-java/
+ commonmark-ext-yaml-front-matterc
32From Python to Kotlin @Horgix / #TalkingKT
3. Directories browsing
YAML frontmatter + Markdown body
33From Python to Kotlin @Horgix / #TalkingKT
Directories browsing in Python :(
34From Python to Kotlin @Horgix / #TalkingKT
Directories browsing in Kotlin :)
35From Python to Kotlin @Horgix / #TalkingKT
Bootstrapping & Running
Getting started on a minimal software/script
and running it manually or in a CI pipeline
36From Python to Kotlin @Horgix / #TalkingKT
Bootstrap - From installed tooling to a working “project”
▼ Python :
▽ Virtualenv
▽ pip install + pip freeze > requirements.txt
▽ Profit
▼ Kotlin :
▽ Maven Gradle configuration
▽ Directory/files structure way more strict/predefined
▽ IntelliJ IDEA “New Project” to the rescue
37From Python to Kotlin @Horgix / #TalkingKT
Running in a CI Pipeline
▼ Python: cache dependencies & just run
▼ Kotlin: need to rebuild just in case
▼ Docker to the rescue
38From Python to Kotlin @Horgix / #TalkingKT
Kotlin script
.kts
39From Python to Kotlin @Horgix / #TalkingKT
Kotlin script
▼ .kts files
▼ Top level instructions
▼ kotlinc -script <file>.kts
▼ Can be used for your Gradle files :)
▼ Does not support multiple files :(
https://kotlinlang.org/docs/tutorials/command-line.html#using-the-command-line-to-run-scripts
40From Python to Kotlin @Horgix / #TalkingKT
kscript
https://github.com/holgerbrandl/kscript
41From Python to Kotlin @Horgix / #TalkingKT
kscript - Imports, libraries and entrypoint definition
42From Python to Kotlin @Horgix / #TalkingKT
kscript surprise n°1 - Imports clash
43From Python to Kotlin @Horgix / #TalkingKT
kscript surprise n°2 - Bad handling of typealias
44From Python to Kotlin @Horgix / #TalkingKT
kscript works fine in the end :)
Demo
45From Python to Kotlin @Horgix / #TalkingKT
Take away
What did I take away from this switch from Python to Kotlin
46From Python to Kotlin @Horgix / #TalkingKT
Take away
▼ Easier / More lightweight to start with Python than Kotlin
▼ More complex to run Kotlin scripts than Python, but kscript saves the day
▼ Thanks Kotlin interoperability with Java
▼ Community libraries are tailored to the language usual use cases
▼ List comprehension vs functional chaining
▼ ❤ Enum Classes
▼ ❤ Pattern matching
▼ ❤ Extension functions
47From Python to Kotlin @Horgix / #TalkingKT
Take away - Self-Q&A
▼ Did I get to re-implement all the things in Kotlin?
▽ Nope, didn’t implement README generation because parsing files was
already too much of a pain, so generating them…
▼ What are the key differences between both implementations in the end?
▽ Kotlin version looks way more clean and maintainable to me
▼ If I were to do it again, would I do it in Python or Kotlin?
▽ Same, first Python then Kotlin :)
48From Python to Kotlin @Horgix / #TalkingKT
5 March 2020
From Python to Kotlin
Feedback from some scripts re-writing
Alexis “Horgix” Chotard
Q&A

From Python to Kotlin - TalkingKT 2019

  • 1.
    1From Python toKotlin @Horgix / #TalkingKT 5 March 2020 From Python to Kotlin Feedback from some scripts re-writing Alexis “Horgix” Chotard
  • 2.
    2From Python toKotlin @Horgix / #TalkingKT Alexis “Horgix” Chotard SRE & Coach DevOps ▼ 30% Development ▼ 40% Operations ▼ 30% Training, coaching, … ▼ Usually prototypes in Python ▼ First time Kotlin user in Oct. 2019 ▼ Loves Rust @Horgix Horgix
  • 3.
    3From Python toKotlin @Horgix / #TalkingKT Warning! This is a personal feedback from a personal experience Lot of bias from my own knowledge :)
  • 4.
    4From Python toKotlin @Horgix / #TalkingKT Context & Usage Talks/Speakers handling from GitHub files
  • 5.
    5From Python toKotlin @Horgix / #TalkingKT Sample - Talks list
  • 6.
    6From Python toKotlin @Horgix / #TalkingKT Sample - Talk file template
  • 7.
    7From Python toKotlin @Horgix / #TalkingKT What do we want to do? ▼ Files “parsing” - YAML frontmatter + Markdown body ▼ “Error” detection : missing infos, non-coherent infos, etc. ▼ Some stats would be nice ▼ Will be run mainly manually or from a CI pipeline
  • 8.
    8From Python toKotlin @Horgix / #TalkingKT First Python implementation “Hey, let’s code something quick to make our life easier”
  • 9.
    9From Python toKotlin @Horgix / #TalkingKT At first, a simple set of Python scripts
  • 10.
    10From Python toKotlin @Horgix / #TalkingKT Then some “tests”
  • 11.
    11From Python toKotlin @Horgix / #TalkingKT “Let’s wrap everything in a Makefile!”
  • 12.
    12From Python toKotlin @Horgix / #TalkingKT “Well, that look hacky as f***…” And indeed. It is was
  • 13.
    13From Python toKotlin @Horgix / #TalkingKT Rewrite everything in Kotlin! (for science)
  • 14.
    14From Python toKotlin @Horgix / #TalkingKT Why Kotlin? ▼ Lot of momentum ▼ Looked cool and I wanted to play with it :) ▼ A lot of data collections computing that can all be done in memory ▽ Functional approach made easy by Kotlin ▽ Kotlin standard library looked like it could make things easier
  • 15.
    15From Python toKotlin @Horgix / #TalkingKT Let’s compare some use cases Mainly about language features, libraries and stuff
  • 16.
    16From Python toKotlin @Horgix / #TalkingKT 1. Talks format handling and base language constructs
  • 17.
    17From Python toKotlin @Horgix / #TalkingKT Talk formats - Storage & field format
  • 18.
    18From Python toKotlin @Horgix / #TalkingKT Talk formats - Storage & field format
  • 19.
    19From Python toKotlin @Horgix / #TalkingKT Talk formats - Storage & field format (raw)
  • 20.
    20From Python toKotlin @Horgix / #TalkingKT Some wise words
  • 21.
    21From Python toKotlin @Horgix / #TalkingKT Better than enums : Enum classes <3
  • 22.
    22From Python toKotlin @Horgix / #TalkingKT Yes, there are (more limited) enums in Python too...
  • 23.
    23From Python toKotlin @Horgix / #TalkingKT 1bis. Talks format handling What about getting our enum value from what we parse?
  • 24.
    24From Python toKotlin @Horgix / #TalkingKT How to get a Enum value from our initial String Python:
  • 25.
    25From Python toKotlin @Horgix / #TalkingKT Pattern matching <3 Python: Kotlin:
  • 26.
    26From Python toKotlin @Horgix / #TalkingKT Function extensions <3 Python: Kotlin:
  • 27.
    27From Python toKotlin @Horgix / #TalkingKT 2. File parsing YAML frontmatter + Markdown body
  • 28.
    28From Python toKotlin @Horgix / #TalkingKT File parsing in Python :) https://github.com/eyeseast/python-frontmatter
  • 29.
    29From Python toKotlin @Horgix / #TalkingKT File parsing in Python :) https://github.com/eyeseast/python-frontmatter
  • 30.
    30From Python toKotlin @Horgix / #TalkingKT File parsing in Kotlin :( https://github.com/atlassian/commonmark-java/ + commonmark-ext-yaml-front-matterc
  • 31.
    31From Python toKotlin @Horgix / #TalkingKT File parsing in Kotlin :( https://github.com/atlassian/commonmark-java/ + commonmark-ext-yaml-front-matterc
  • 32.
    32From Python toKotlin @Horgix / #TalkingKT 3. Directories browsing YAML frontmatter + Markdown body
  • 33.
    33From Python toKotlin @Horgix / #TalkingKT Directories browsing in Python :(
  • 34.
    34From Python toKotlin @Horgix / #TalkingKT Directories browsing in Kotlin :)
  • 35.
    35From Python toKotlin @Horgix / #TalkingKT Bootstrapping & Running Getting started on a minimal software/script and running it manually or in a CI pipeline
  • 36.
    36From Python toKotlin @Horgix / #TalkingKT Bootstrap - From installed tooling to a working “project” ▼ Python : ▽ Virtualenv ▽ pip install + pip freeze > requirements.txt ▽ Profit ▼ Kotlin : ▽ Maven Gradle configuration ▽ Directory/files structure way more strict/predefined ▽ IntelliJ IDEA “New Project” to the rescue
  • 37.
    37From Python toKotlin @Horgix / #TalkingKT Running in a CI Pipeline ▼ Python: cache dependencies & just run ▼ Kotlin: need to rebuild just in case ▼ Docker to the rescue
  • 38.
    38From Python toKotlin @Horgix / #TalkingKT Kotlin script .kts
  • 39.
    39From Python toKotlin @Horgix / #TalkingKT Kotlin script ▼ .kts files ▼ Top level instructions ▼ kotlinc -script <file>.kts ▼ Can be used for your Gradle files :) ▼ Does not support multiple files :( https://kotlinlang.org/docs/tutorials/command-line.html#using-the-command-line-to-run-scripts
  • 40.
    40From Python toKotlin @Horgix / #TalkingKT kscript https://github.com/holgerbrandl/kscript
  • 41.
    41From Python toKotlin @Horgix / #TalkingKT kscript - Imports, libraries and entrypoint definition
  • 42.
    42From Python toKotlin @Horgix / #TalkingKT kscript surprise n°1 - Imports clash
  • 43.
    43From Python toKotlin @Horgix / #TalkingKT kscript surprise n°2 - Bad handling of typealias
  • 44.
    44From Python toKotlin @Horgix / #TalkingKT kscript works fine in the end :) Demo
  • 45.
    45From Python toKotlin @Horgix / #TalkingKT Take away What did I take away from this switch from Python to Kotlin
  • 46.
    46From Python toKotlin @Horgix / #TalkingKT Take away ▼ Easier / More lightweight to start with Python than Kotlin ▼ More complex to run Kotlin scripts than Python, but kscript saves the day ▼ Thanks Kotlin interoperability with Java ▼ Community libraries are tailored to the language usual use cases ▼ List comprehension vs functional chaining ▼ ❤ Enum Classes ▼ ❤ Pattern matching ▼ ❤ Extension functions
  • 47.
    47From Python toKotlin @Horgix / #TalkingKT Take away - Self-Q&A ▼ Did I get to re-implement all the things in Kotlin? ▽ Nope, didn’t implement README generation because parsing files was already too much of a pain, so generating them… ▼ What are the key differences between both implementations in the end? ▽ Kotlin version looks way more clean and maintainable to me ▼ If I were to do it again, would I do it in Python or Kotlin? ▽ Same, first Python then Kotlin :)
  • 48.
    48From Python toKotlin @Horgix / #TalkingKT 5 March 2020 From Python to Kotlin Feedback from some scripts re-writing Alexis “Horgix” Chotard Q&A