PyCon APAC 2023

Reformating your code without AI - let's see how a formatter work
2023-10-28 , track 2

We don’t need AI for writing better code! Formators like Black and PyBetter have been automatically refactoring messy code way before AI agents claim to help you write codes. In this talk, we are going to explore how formatters analyse and manipulate the CST of your code to make it nicer.


Recently year there are AI agents that pops up in the market saying that they can help you to write code. However, there are tools out there that have been used over decades that can help us write better codes - the auto formats like Black and PyBetter that can refactor your code into nicer-looking ones. In this talk, we are going to explore how auto-formators analyse and manipulate the CST of your code to make it nicer.

First, we will introduce what is an Abstract Syntax Tree (AST) and a Concrete Syntax Tree (CST). Next, we will talk about libcst and how it analyses the code and express the CST of your code as Python objects. Then, we explore how we can use the match method provided and modify our code if the logic matches. By the end of it, the auto-formators would not be a mystery for the audience anymore as we now understand the inner working of it.

Outline

  • Introduction: What is AST and what is CST, and what are the differences (5 mins)
  • Introducing libcst: example codes (5 mins)
  • Matching CST patterns: example of doing it with libcst 5 mins)
  • How auto-formatter works (10 mins)
  • Conclusions (5 mins)

Before working in Developer Relations, Cheuk has been a Data Scientist in various companies which demands high numerical and programmatical skills, especially in Python. To follow her passion for the tech community, Cheuk is now the Developer Advocate at Anaconda. Cheuk also contributes to multiple Open Source libraries like Hypothesis and Pandas.

Besides her work, Cheuk enjoys talking about Python on personal streaming platforms and podcasts. Cheuk has also been a speaker at Universities and various conferences. Besides speaking at conferences, Cheuk also organises events for developers. Conferences that Cheuk has organized include EuroPython (which she is a board member), PyData Global and Pyjamas Conf. Believing in Tech Diversity and Inclusion, Cheuk constantly organizes workshops and mentored sprints for minority groups. In 2021, Cheuk has become a Python Software Foundation fellow.