Kaviraj       Archive       Tags

Understanding Condition Variable in Go

If you had ever done some kind of multi-threaded or concurrent programming in any languages, then there is a high chance that you would have used or at least heard about Condition Variable.

This post is an attempt to explain my understanding of the Condition Variable, which I happened to use in one of my recent projects. All the code samples are written in Go, but the concepts remains same irrespective of the programming languages.

A condition variable is basically a container of threads that are waiting for a certain condition.

That definition from wikipedia is pretty straight forward. And don’t worry if that still confusing. I hope this blog may help you understand what it is and how it can be used.

Condition Variable is one of the synchronization mechanisms.

Before using Condition Variable, lets understand what problem it tries to solve.

Lets start simple.

How to make a goroutine wait till some event(condition) occur?.

Read More

#condition-variable #go #golang #sync #synchronization

Go-gorm: JOINS make life easier!

I have been using go-lang and gorm to build web apps for a while. Though gorm provids very good documentation, I think it lacks some real-time examples. Here is what I have learned how to use gorm for advanced queries. Most importantly to get data from multiple tables that are not directly related.

Let me try explaining the concepts with three use cases.

Consider following models

type Launguage struct {
	ID uint `gorm:"primary_key"`
	Name string
type Movie struct {
	ID uint `gorm:"primary_key"`
	Title string
	LaunguageID uint
	Launguage Launguage

type Artist struct {
	ID uint `gorm:"primary_key"`
	Name string
	Movies []Movie `gorm:"many2many:artist_movies"`

Read More

#ORM #go #golang #sql

Django: Internals series - Gotchas

Hi Everyone. Welcome to Django internal series. Goal of this entire series is to share my experience on understanding the internals of django.

In part 1 of this series we try to understand why its important to understand django internals.

I started using django about an year ago. It’s a part of our company’s technology stack. We love building apps with Django(of course who wouldn’t?). During early stage of using django, I really suck at debugging as I felt django is doing lot of magic under the hood. Few basic questions started bothering me.

  1. I am declaring all fields in model as class variables. How come I am able to access those as instance variables on any of those model instances?
  2. What is happening when I access any field of a model instance. For e.g take a look at simple_django_model.py snippet below. There we have single field called email of type models.EmailField(). Now if try to get user.email it should technically return models.EmailField instance right?, but django does some magic and return the exact email address as string [email protected]
  3. How does “class Meta” inside any model affects the model behaviour?
  4. What does really ‘app’ mean to django?(HINT: Every directory with __init__.py is not an app)
  5. How django creates actual model instance from db tables?

And much more basic questions..

Read More

#django #python

Python: Generating pdf containing emojis using reportlab

Understanding Emoji

Emojis 😃 🎉 👍 💕 have become so popular. Anyone who used Whatsapp or Instagram knows how cool emojis are 😎. And they are everywhere now. Emoji originated in Japan. The word “emoji” means “Picture(e) letter(moji)” in Japanese.

Technically, emojis are subset of Unicode character set. In short, Unicode is the collection of every writable symbol available on the planet. That being said, unicode includes symbols from every language that exist.

There are different types of encoding used to interpret different sets of unicode. Most popular is “UTF-8” encoding. To understand how unicode are interpreted, stored and retrieved here is a great article The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Every emoji is a single unicode character that can be upto 4 bytes long depending on type of encoding used. Yes, every character is just one byte is no longer true.

Read More

#emoji #python #reportlab