Python features to help you become a better programmer

Python has lot of niche features in which can help improve efficiency or readability of code. Today we will discuss some of those features that you can immediately start using in your day to day scripting.

named tuple/SimpleNamespace

namedTuple

Tuples eg: a =(1, 2) are useful in a sense they occupy the least space, and the closest thing to constant that you you can expect from python. But they are one of the least used datatype. Reading from tuple is based on position like a[0] and a[1]. Enter named tuple, that takes advantage of normal tuple and adds the basic subscription of dict…


PyQt is powerful cross platform framework that can be used for GUI development. It wraps the popular QT C++ framework and allows connection via python. In this article we will be creating desktop application using python.

note: I am not going to use QtDesigner. I use vim for coding. Installing and learning an IDE is an effort I refuse to make.

Installation

Lets create and activate the virtual environment

$ virtualenv venv
$ source venv/bin/activate

Installing dependencies:

$ pip install pyqt5
$ pip install pyinstaller

We will use pyqt5 for creating GUI. We will use pyinstaller for creating compiled app.

Design

This…


How to Create a Language Interpreter with Python

How to create a programming language with Python

Computers only understand machine code. At the end of the day programming languages are just words that make it’s easier for humans to write what they want computers to do. The real magic is done by compilers and interpreters to bridge the gap. Interpreter reads the code line by line and convert it to machine code. In this article, we will design interpreter that can perform arithmetic operation. We are not going to reinvent the wheel. I will be using the popular PLY ( Python Lex-Yacc) by David M. Beazley in this tutorial. Download it using:

$ pip install ply


Composition vs. Inheritance. Why prefer composition over inheritance?

Composition over inheritance is oops concept that states classes should achieve polymorphic behavior and reuse classes using composition. You might have heard the saying Favor object composition over class inheritance Some programming languages like GO and rust are designed to facilitate composition over inheritance. Lets try to understand what is inheritances and composition, what are their strength and weaknesses and try to determine if the saying holds any merit.

Inheritance

Inheritance is one of the four major concepts associated with Object oriented programming (OOPS) which includes — abstraction, encapsulation, inheritance, and polymorphism.Inheritance allows…


Python regex functions. A quick summary of all regex functions in Python.

list of regex functions

re.match(pattern, string, flags=0)

>>> pattern = r”\w”
>>> string = “a,b,c,d\ne;f;g;h”
>>> re.match(pattern, string)

re.match returns returns first occurrence pattern in the first line. If no occurrence is found None is returned. If we have kept parenthesizes ( or ) then we can extract sub pattern .

>>> pattern = r"(\w)(,)"
>>> re.match(pattern, string)
<re.Match object; span=(0, 2), match='a,'>
>>> re.match(pattern, string).group(0)
'a,'
>>> re.match(pattern, string).group(1)
'a'
>>> re.match(pattern, string).group(2)
','
>>> re.match(pattern, string).groups()
('a', ',')

re.search(pattern, string, flags=0)

>>> pattern = r”\w”
>>> string = “a,b,c,d\ne;f;g;h”
>>> re.search(pattern, string)
<re.Match object; span=(0, 1), match=’a’>

It scans the string till…


How to create a REST API from Django from scratch

Rest API

Rest API, or RESTful API stands for Representational State Transfer. It is an application programming interface that conforms to the constraints of REST architecture style. It is not a protocol or a standard. It is as stated an architecture constraint. In order for API to be considered RESTful, it has to confront to following criteria.

  • Use of uniform unterface(UI): Interface is build around uniquely identifiable resource identified by single URL, which can be manipulated using network protocols like GET, POST, DELETE, UPDATE.
  • Client-Server based: Server parses the requests given by client and sends a response, and client determines how the…


How can computers make such a silly mistake?

Modern digital computers have come a long way. Computers have become a foundation on which recent years of scientific and technological advancements have been made. So, it's a little funny to think that computers yield strange results for some floating-point operations that look intuitive to even toddlers. In this article, we will try to understand why is it so?

0.1 + 0.2 != 0.3

I am showing it in python interpreter but you can see the issue in every programming language.

>>> 0.1 + 0.2 == 0.3
False
>>> 1.0 +2.0
3.0

When we try to look…


Using TCP socket to implement HTTP server and client with Python

HTTP stands for Hyper Text Transfer protocol. It is an application layer protocol for communicating data between client and server using browser. We can use sockets to implement HTTP. We have to decode/encode messages according to the HTTP specification (RFC2616) which is just a Text. In this article, we will implement the basics of HTTP protocol.

HTTP server

We will create a TCP socket. The default port is 80 for HTTP. But we will be using 8080 for running our test.

You can use request module to receive response from…


Introduction to web assembly using Rust

Introduction

JavaScript is a dynamic language with with JIT compiler. The problem with javascript is it was not designed for performance and it runs relatively slowly. WebAssembly is the new programming language for the web that allows the code to run with high performance. It is low-level, portable, binary format for the web. Web assembly being binary format is faster to load and run for browser, but that also means it is hard to read and write by humans. Luckily, we don’t write the web assembly directly, we use high-level languages like C/C++/Rust, enabling deployment…


Introduction to Brython for web development.

Saying JavaScript is popular is an understatement. It dominates the software industry. It is the only language that can give Python a run for its money in popularity. It is mainly due to its association with the web.

Brython stands for browser Python. It’s is a transpiler that allows us to use Python3 instead of JavaScript to communicate with web APIs on the server-side or client-side. It will read the Python program which is stored in the element <script type = “text/python”> during load time and then compile the Python program for the equivalent…

Umangshrestha

I like computer and programming. I am currently trying to learn how to write articles for sharing what I know.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store