Quite often, as a programmer, I learn something new. Some are utilitarian; some are philosophical; some are opinions in programming. I want to document these learnings for later use and also to remember. So I’m starting a new site, til.kracekumar.com, to demonstrate this learning. So far, there are six posts.
The inspiration comes from Simon Willson’s TIL website.
Why the new site? Two reasons
I’m planning to write often; sometimes, the post will fit in just two tweets.
[Read More]
Python Typing Koans
Python 3 introduced type annotation syntax. PEP 484 introduced a provisional module to provide these standard definitions and tools, along with some conventions for situations where annotations are not available.
Python is a dynamic language and follows gradual typing. When a static type checker runs a python code, the type checker considers code without type hints as Any.
def print_name(name): print(name) planet: str = "earth" In the above example, the name argument type hint will be Any since the type hint is missing while the type hint for the planet variable is a string.
[Read More]
Model Field - Django ORM Working - Part 2
The last post covered the structure of Django Model. This post covers how the model field works, what are the some important methods and functionality and properties of the field.
Object-Relational Mapper is a technique of declaring, querying the database tables using Object relationship in the programming language. Here is a sample model declaration in Django.
class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') Each class inherits from models.Model becomes a table inside the SQL database unless explicitly marked as abstract.
[Read More]
Structure - Django ORM Working - Part 1
Django ORM hides a lot of complexity while developing the web application. The data model declaration and querying pattern are simplified, whereas it’s structured differently behind the scenes. The series of blog posts will explain Django ORM working(not just converting Python code to SQL), model declaration, querying (manager, queryset), supporting multiple drivers, writing custom queries, migrations etc…
Consider a model definition from the Django tutorial.
from django.db import models class Question(models.
[Read More]
jut - render jupyter notebook in the terminal
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more. The definition copied from the official website.
It’s becoming common to use Jupyter notebook to write books, do data analysis, reproducible experiments, etc… The file produced out of notebook follows JSON Schema.
[Read More]
Five reasons to use Py.test
Pytest library provides a better way to write tests, run the tests, and report the test results. This post is a comparison between the Python unit test standard library and pytest features and leaves out other libraries like nose2.
TL;DR Single assert statement(assert a == b) over 40 different assert methods(self.assertEqual, self.assertIsInstance, self.assertDictEqual) Better and detailed error messages on failure. Useful command line options for reporting, discovering, and reporting tests like --last-failed, --collect-only.
[Read More]
Build Plugins with Pluggy
Introduction The blog post is a write up of my two talks from PyGotham and PyCon India titled, Build Plugins with Pluggy. The write-up covers a trivial use-case, discusses why a plugin-based architecture is a good fit, what is plugin-based architecture, how to develop plugin-based architecture using pluggy, and how pluggy works.
Link to PyCon India 2020 Talk
Trivial Use Case For the scope of the blog post, consider a command-line application queries gutenberg service, processes the data, and displays the relevant information.
[Read More]
Render local images in datasette using datasette-render-local-images
Datasette python library lets publishing a dataset as a SQLite database and inspects them using a web browser. The database can store text, numbers, arrays, images, binary data, and all possible formats. datasette-render-images lets you save the image in the database and render the image using data-uris.
Sometimes, while publishing the dataset, you may want to store the images in a separate directory for various reasons and include the relative path to the database’s images in the table.
[Read More]
Tamil 1k Tweets For Binary Sentiment Analysis
To find a labeled data for Tamil NLP task is a difficult task. Some papers talk about Tamil Neural Translation, but the article doesn’t release code. If you’re working part-time or possess an interest in Tamil NLP, you have a tough time finding data.
When I was looking for labeled data for simple sentiment analysis, I couldn’t find any. It’s understandable because there is no one working on it. So I decided to build my dataset.
[Read More]
Parameterize Python Tests
Introduction A single test case follows a pattern. Setup the data, invoke the function or method with the arguments and assert the return data or the state changes. A function will have a minimum of one or more test cases for various success and failure cases.
Here is an example implementation of wc command for a single file that returns number of words, lines, and characters for an ASCII text file.
[Read More]