Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From Python to Kotlin - TalkingKT 2019

Horgix
March 05, 2020

From Python to Kotlin - TalkingKT 2019

Talk presenting a real life feedback from a rewriting of some scripts in Python into Kotlin - talking about languages features, running scripts, tooling, etc. This was presented at TalkingKT 2019 on 5th March 2019.

I'm sure you already know that Kotlin is cool, you wouldn't be there otherwise. But can we stop a second to ask ourselves: what does it concretely brings to the table? Let's avoid the usual comparison with Java - and instead, talk about what Kotlin offers for some almost-scripting tooling, coming from Python. This talk is a real field feedback about the re-coding of a Python tool into Kotlin from the ground up.

Horgix

March 05, 2020
Tweet

More Decks by Horgix

Other Decks in Programming

Transcript

  1. 1 From Python to Kotlin @Horgix / #TalkingKT 5 March

    2020 From Python to Kotlin Feedback from some scripts re-writing Alexis “Horgix” Chotard
  2. 2 From 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
  3. 3 From Python to Kotlin @Horgix / #TalkingKT Warning! This

    is a personal feedback from a personal experience Lot of bias from my own knowledge :)
  4. 4 From Python to Kotlin @Horgix / #TalkingKT Context &

    Usage Talks/Speakers handling from GitHub files
  5. 7 From 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
  6. 8 From Python to Kotlin @Horgix / #TalkingKT First Python

    implementation “Hey, let’s code something quick to make our life easier”
  7. 12 From Python to Kotlin @Horgix / #TalkingKT “Well, that

    look hacky as f***…” And indeed. It is was
  8. 14 From 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
  9. 15 From Python to Kotlin @Horgix / #TalkingKT Let’s compare

    some use cases Mainly about language features, libraries and stuff
  10. 16 From Python to Kotlin @Horgix / #TalkingKT 1. Talks

    format handling and base language constructs
  11. 22 From Python to Kotlin @Horgix / #TalkingKT Yes, there

    are (more limited) enums in Python too...
  12. 23 From Python to Kotlin @Horgix / #TalkingKT 1bis. Talks

    format handling What about getting our enum value from what we parse?
  13. 24 From Python to Kotlin @Horgix / #TalkingKT How to

    get a Enum value from our initial String Python:
  14. 27 From Python to Kotlin @Horgix / #TalkingKT 2. File

    parsing YAML frontmatter + Markdown body
  15. 28 From Python to Kotlin @Horgix / #TalkingKT File parsing

    in Python :) https://github.com/eyeseast/python-frontmatter
  16. 29 From Python to Kotlin @Horgix / #TalkingKT File parsing

    in Python :) https://github.com/eyeseast/python-frontmatter
  17. 30 From Python to Kotlin @Horgix / #TalkingKT File parsing

    in Kotlin :( https://github.com/atlassian/commonmark-java/ + commonmark-ext-yaml-front-matterc
  18. 31 From Python to Kotlin @Horgix / #TalkingKT File parsing

    in Kotlin :( https://github.com/atlassian/commonmark-java/ + commonmark-ext-yaml-front-matterc
  19. 32 From Python to Kotlin @Horgix / #TalkingKT 3. Directories

    browsing YAML frontmatter + Markdown body
  20. 35 From Python to Kotlin @Horgix / #TalkingKT Bootstrapping &

    Running Getting started on a minimal software/script and running it manually or in a CI pipeline
  21. 36 From 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
  22. 37 From 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
  23. 39 From 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
  24. 41 From Python to Kotlin @Horgix / #TalkingKT kscript -

    Imports, libraries and entrypoint definition
  25. 45 From Python to Kotlin @Horgix / #TalkingKT Take away

    What did I take away from this switch from Python to Kotlin
  26. 46 From 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
  27. 47 From 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 :)
  28. 48 From Python to Kotlin @Horgix / #TalkingKT 5 March

    2020 From Python to Kotlin Feedback from some scripts re-writing Alexis “Horgix” Chotard Q&A