Tutorials

Photo by Ali Yahya on Unsplash

Master python closure with 3 real-world examples

Introduction

Python closure is a technique for binding function with an environment where the function gets access to all the variables defined in the enclosing scope. Closure typically appears in the programming language with first class function, which means functions are allowed to be passed as arguments, return value or assigned to a variable.

This definition sounds confusing to the python beginners, and sometimes the examples found from online also not intuitive enough in the way that most of the examples are trying to illustrate with some printing statement, so the readers may not get the whole idea of why and how the closure should be used. In this article, I will be using some real-world example to explain how to use closure in your code.

Nested function in Python

To understand closure, we must first know that Python has nested function where one function can be defined inside another. For instance, the below inner_func is the nested function and the outer_func returns it’s nested function as return value.

def outer_func():    
    print("starting outer func")
    def inner_func():
        pi = 3.1415926
        print(f"pi is : {pi}")
    return inner_func

When you invoke the outer_func, it returns the reference to the inner_func, and subsequently you can call the inner_func. Below is the output when you run in Jupyter Notebook:

python closure nested function example

After you have got some feeling about the nested function, let’s continue to explore how nested function is related to closure. If we modify our previous function and move the pi variable into outer function, surprisedly it generates the same result as previously.

def outer_func():    
    print("starting outer func")
    #move pi variable definition to outer function
    pi = 3.1415926
    def inner_func():
        print(f"pi is : {pi}")
    return inner_func

You may wonder the pi variable is defined in outer function which is a local variable to outer_func, why inner_func is able access it since it’s not a global scope? This is exactly where closure happens, the inner_func has the full access to the environment (variables) in it’s enclosing scope. The inner_func refers to pi variable as nonlocal variable since there is no other local variable called pi.

If you want to modify the value of the pi inside the inner_func, you will have to explicitly specify “nonlocal pi” before you modify it since it’s immutable data type.

With the above understanding, now let’s walk through some real-world examples to see how we can use closure in our code.

Hide data with Python closure

Let’s say we want to implement a counter to record how many time the word has been repeated. The first thing you may want to do is to define a dictionary in global scope, and then create a function to add in the words as key into this dictionary and also update the number of times it repeated. Below is the sample code:

counter = {}

def count_word(word):    
    global counter
    counter[word] = counter.get(word, 0) + 1
    return counter[word]

To make sure the count_word function updates the correct “counter”, we need to put the global keyword to explicitly tell Python interpreter to use the “counter” defined in global scope, not any variable we accidentally defined with the same name in the local scope (within this function).

Sample output:

python closure word counter sample output

The above code works as expected, but there are two potential issues: Firstly, the global variable is accessible to any of the other functions and you cannot guarantee your data won’t be modified by others. Secondly, the global variable exists in the memory as long as the program is still running, so you may not want to create so many global variables if not necessary.

To address these two issues, let’s re-implement it with closure:

def word_counter():
    counter = {}
    def count(word):
        counter[word] = counter.get(word, 0) + 1
        return counter[word]
    return count

If we run it from Jupyter Notebook, you will see the below output:

python closure word counter example output

With this implementation, the counter dictionary is hidden from the public access and the functionality remains the same. (you may notice it works even after the word_counter function is deleted)

Convert small class to function with Python closure

Occasionally in your project, you may want to implement a small utility class to do some simple task. Let’s take a look at the below example:

import requests

class RequestMaker:
    def __init__(self, base_url):
        self.url = base_url
    def request(self, **kwargs):
        return requests.get(self.url.format_map(kwargs))

You can see the below output when you call the make_request from an instance of RequestMaker:

python closure small class example

Since you’ve already seen in the word counter example, the closure can also hold the data for your later use, the above class can be converted into a function with closure:

import requests

def request_maker(url):
    def make_request(**kwargs):
        return requests.get(url.format_map(kwargs))
    return make_request

The code becomes more concise and achieves the same result. Take note that in the above code, we are able to pass in the arguments into the nested function with **kwargs (or *args).

python closure convert small class to closure

Replace text with case matching

When you use regular express to find and replace some text, you may realize if you are trying to match text in case insensitive mode, you will not able to replace the text with proper case. For instance:

import re

paragraph = 'To start Python programming, you need to install python and configure PYTHON env.'
re.sub("python", "java", paragraph, flags=re.I)

Output from above:

python closure replace with case

It indeed replaced all the occurrence of the “python”, but the case does not match with the original text. To solve this problem, let’s implement the replace function with closure:

def replace_case(word):
    def replace(m):
        text = m.group()
        if text.islower():
            return word.lower()
        elif text.isupper():
            return word.upper()
        elif text[0].isupper():
            return word.capitalize()
        else:
            return word
    return replace

In the above code, the replace function has the access to the original text we intend to replace with, and when we detect the case of the matched text, we can convert the case of original text and return it back.

So in our original substitute function, let’s pass in a function replace_case(“java”) as the second argument. (You may refer to Python official doc in case you want to know what is the behavior when passing in function to re.sub)

re.sub("python", replace_case("java"), paragraph, flags=re.IGNORECASE)

If we run the above again, you should be able to see the case has been retained during the replacement as per below:

python closure replace with case

Conclusion

In this article, we have discussed about the general reasons why Python closure is used and also demonstrated how it can be used in your code with 3 real-world examples. In fact, Python decorator is also a use case of closure, I will be discussing this topic in the next article.

 

pyinstaller pack python program into exe

How to pack python program into exe file

After you have built your python program, you may want to distribute this program to your users to run by themselves. However, in most of the cases, your uses either may not have the access to install Python for executing the script nor have the knowledge to run script from command line. In this case, you will need to find a way to pack your program into some executable file, so that it can be run with a simply click like other apps. In this article, I will be sharing with you how to pack python program into exe file with PyInstaller library for Windows users.

Prerequisite

You will need to create a virtual environment for your python program and activate it with the below command. I will explain why this is needed later.

python -m venv test
test\Scripts\activate.bat

Then install PyInstaller library:

pip install pyinstaller

Let’s get started

Let me first explain why we need to set up a virtual environment for your program. If you are concurrently working on different projects, and each of them are using a different set of python libraries, sometimes these libraries may conflict with each other due the version difference or other dependencies. In this case, you will need to use venv module to create a isolated python environment for each of your projects, so that each virtual environment only has the necessary libraries for running that particular python project.

Same comes when packing your program with PyInstaller, the virtual environment will ensure only the necessary libraries will be packed generating the executable file.

Build your Python program

For this article, our main objective is to demonstrate how to pack python program into exe file, so let’s just include some random library and write some dummy code.

pip install requests

And create a hello.py with the below code:

import requests
import sys, time

result = requests.get("https://www.google.com")
print(f"Google responded {result.status_code}")

with open("test.config") as f:
    print(f.read())

for i in range(15, 0, -1):
    sys.stdout.write("\r")
    sys.stdout.write(f"Window will be closed in {i:2d} seconds")
    sys.stdout.flush()
    time.sleep(1)

Let’s also create a file at the current directory called “test.config” and write some random words, saying “some configurations”.

If you run it with python hello.py, you shall get something similar output to the below:

Google responded 200
some configuration
Window will be closed in  1 seconds

Everything is ready, let’s move to the next step to pack python program into exe file.

Pack python program into exe file with PyInstaller

The PyInstaller program is actually quite easy to use, everything comes with a default option. E.g., If you do not specify any parameter and just run the below:

pyinstaller hello.py

You will be able to get a folder (onedir mode) under dist\hello, where you can find a hello.exe. But if you click to run it, it probably will auto close after a few seconds before you can see any error message.

The problem here is that, inside our program, we have some code to read some external file “test.config”, and this file was not packed into the dist\hello folder. Of course you can manually copy this file to dist\hello every time after you built the Python program, but there is a option you can use to tell PyInstaller to include the additional files.

–add-data option

This –add-data option can be used to include the additional file or directory. e.g.:

–add-data “src file or folder;dest file or folder”

If you have multiple files to be added, you can use this option multiple times. (for binary file, you may consider to use –add-binary option)

So you can re-run the below command to include the additional file, and also use –clean to clean up the directory before generating the files again.

pyinstaller hello.py --add-data "test.config;." --clean
–noconfirm option

You may see the warning similar to below to ask your confirmation to delete the old files, you can just key in “y” to confirm. This question can be avoided if you put the option –noconfirm.

WARNING: The output directory “c:\test\dist\hello” and ALL ITS CONTENTS will be REMOVED! Continue? (y/n)

So once the new exe file generated, you shall be able to run and see the below result:

pack python program into exe file

So far so good, but still can be better. Let’s specify the name of the exe file, and make it one file rather than a directory.

–onefile vs –onedir

With the below extra options : –onefile and –name “SuperHero”, we shall expect to pack the Python program into a single SuperHero.exe file.

pyinstaller --onefile hello.py --name "SuperHero" --add-data "test.config;." --clean

When we try to execute this exe file, you would see some error like below. This is because when running the exe, PyInstaller unpack your data into a temp folder, and the temp folder path is set to sys._MEIPASS, which will be different from your original file path.

pack python program into exe file

In this case, let’s modify our code again to cater for this:

import os

def get_resource_path(relative_path):
    try:
        # PyInstaller creates a temp folder and set the path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

with open(get_resource_path("test.config")) as f:
    print(f.read())

When you rebuild the SuperHero.exe, this time you shall be able to execute it without any issue. And it also works perfectly if you rebuild your exe with –onedir mode.

–log-level

If you do not wish to see so many output messages when packing the program, you can turn it off by using the –log-level, the log level option can be one of TRACE, DEBUG, INFO, WARN, ERROR, CRITICAL. For instance, –log-level=”ERROR” will only show any output with error, and you do not even see a “Building completed successfully” message after build completion as it is logged as INFO.

–noconsole

If you are working with some automation program like auto sending emails or auto save some attachments, which does not necessarily interact with users, you can use –noconsole option, so when you click to run your exe file, it does not show up any console window.

PyInstaller specification file

You may noticed after you run the pyinstaller command, there is a .spec file generated. This file keeps all the options you have used for your last build. So if you just want to rebuild your executable files without changing any option, you may use the below command:

pyinstaller - D SuperHero.spec

Conclusion

With the options covered in above, it should meet your basic needs to pack python program into exe file. You may also refer to the official document for the other options PyInstaller offers.

python string data type

Python String Data Type

In the previous article, we have discussed about the Python variables including string variables. String is a Python built-in data type which holds a sequence of characters, you will need to use it whenever you need to do any text processing. In this article, I will be sharing with you the various operations you can perform with the Python string data type.

Python string data type

In python, you can define a string variable with single quote, double quotes or triple quotes. And use type() function to verify the data type of your variable. E.g.:

text1 = 'hello \n world!'
text2 = "bac;def,what$ is"
text3 = """this is also fine"""
print(type(text1), text1)
print(type(text2), text2)
print(type(text3), text3)

You should be able to see the below output, and the data type is showing as “str”.

<class 'str'> hello 
 world!
<class 'str'> bac;def,what$ is
<class 'str'> this is also fine
Slice Operation

As per the definition for Python string data type, it is a sequence of characters, which means you can access each of the character with the index. (index starts from 0 for the first element)

print(text1[0], text2[1], text3[2])
h a i

And you can use slice operation to get a sub set of your string variable:

#get a sub string starting from index 0 and ending at index 5 (exclusive)
print(text1[0:5])
#get a sub string starting from index 5 and ending at index 7 (exclusive)
print(text3[5:7])
#get a sub string starting from default index 0 and ending at index 4 (exclusive)
print(text3[:4])
#get a sub string starting from index 5 and ending at the end of the string
print(text3[5:])
hello
is
this
is also fine

You can also specify the negative index value to slice the string starting from right to left:

print(text1[-1])
print(text3[-3:-1])
!
in

There is actually a third option – slice step you can use, which you can specify a non-zero integer, e.g:

print(text4[0::2])
print(text4[1::2])
aceg
bdf
Immutable nature

Since we are able to get each individual character from a string, you may wonder if we can re-assign something else to a particular position in the string. e.g.:

text4[0] = 'T'
#TypeError: 'str' object does not support item assignment

The error shows up because string is immutable and you cannot change anything in it’s original content unless you create a new string:

new_text4 = "T" + text4[1:]
+ and *

And you may noticed different strings can be concatenated by using the “+” in the above example. There is also more operator * can be used in the string.

print(text4 + text3*2)

This will duplicate text3 twice and concatenate them into a single string:

abcdefgthis is also finethis is also fine
Formatting Python string data type

Below are some of the string formatting functions, it’s quite self-explanatory by the function name:

print("lower:", text4.lower())
#same as lower()
print("casefold:", text4.casefold())

print("upper:", text4.upper())

print("title:", text4.title())
#same as title
print("capitalize:", text4.capitalize())

print("swapcase:", text4.swapcase())
print("center:", text4.center(40, "*"))
print("ljust:", text4.ljust(40))
print("rjust:", text4.rjust(40, "*"))
print("zfill:", text4.zfill(40))
print("strip:", text4.strip("a"))
print("replace:", text4.replace("a", "A"))

Below is the output:

lower: abcdefg
casefold: abcdefg
upper: ABCDEFG
title: Abcdefg
capitalize: Abcdefg
swapcase: ABCDEFG
center: ****************abcdefg*****************
ljust: abcdefg                                 
rjust: *********************************abcdefg
zfill: 000000000000000000000000000000000abcdefg
strip: bcdefg
replace: Abcdefg

And also there are functions you can use for checking the string format:

print("isalnum:",text4.isalnum())	
print("isalpha:",text4.isalpha())
print("isdecimal:",text4.isdecimal())
print("isdigit:",text4.isdigit())
print("isnumeric:",text4.isnumeric())
print("isidentifier:",text4.isidentifier())
print("islower:",text4.islower())
print("istitle:",text4.istitle())
print("isupper:",text4.isupper())
print("isspace:",text4.isspace())
print("isprintable:",text4.isprintable())

Output will be something similar to below:

isalnum: True
isalpha: True
isdecimal: False
isdigit: False
isnumeric: False
isidentifier: True
islower: True
istitle: False
isupper: False
isspace: False
isprintable: True
Comparison operations

You can use relational operators such as ==, >, < to compare the two strings. Python will try to compare letter by letter, and all the uppercase letters come before lowercase, hence you will need to convert your texts into a standard format e.g. all upper or lower case, in order to get the comparison result in alphabetical order.

To check if the string starts/ends with any characters, you can use the startswith and endswith function:

if text3.startswith("this"):
    print("yes, it starts with 'this'")
if text3.endswith("fine"):
    print("yes, it ends with 'fine'")

There is no function called contains (sometime people get confused since Java string has this contains method), but you can use the below function – in, find, index or rindex to check if the string has any sub string:

if "this" in text3:
    print("'this' is in text3")
else:
    print("not found")

if text3.find("this") > -1:
    print("found 'this' from tex3")
else:
    print("not found")

if text3.find("this",1, 20) > -1:
    print("found 'this' from tex3")
else:
    print("'this' is not found from text3, starting from index 1 to 20 ")

if text3.index("this") >-1:
    print("found 'this' from tex3, index >=0")
else:
    print("not found")

#ValueError: substring not found
#idx = text3.index("this",1, 20)

Both find and index function return the index value of the sub string, the difference between of two function is that, index function will raise ValueError when the sub string is not found, while find will just return -1.

Split & Join texts

A lot times you may need to split the text by certain delimiter, e.g. newlines (\n), ; space etc. You can use the split function to the text into a list. If the delimiter is not found, the split function will return the original text as in a list.

print("split by default deliminator:", text3.split())
print("split by s", text3.split('s'))
print("split by ;", text3.split(';'))

The output will be:

split by default deliminator: ['this', 'is', 'also', 'fine']
split by s ['thi', ' i', ' al', 'o fine']
split by ; ['this is also fine']

On the other hand, if you have a list of string, you would like to join them into one string, you can do the following:

print("join the words with ';':", ';'.join(text3.split()))
print("join the words without space:", ''.join(text3.split()))

And below is the output:

join the words with ';': this;is;also;fine
join the words without space: thisisalsofine
Count occurrence

The count function can be used for calculating the occurrence of a sub string from the original string, for instance :

print(text3*5)
print("'is' occurence:',(text3*5).count("is"))

Result will be :

this is also finethis is also finethis is also finethis is also finethis is also fine
'is' occurence:10

Conclusion

With all the above examples provided, we have covered most of the commonly used functions for Python string data type. You may also check through the Python official document to see if there is any additional functions you are interested to know for the Python strings data type.

Python Variables and Keywords

Python Tutorial – Variables and Keywords

This article serves as a tutorial for Python beginners to gain the essential knowledge to start coding in Python. By complete this tutorial, you shall be able to know how to correctly use Python variables as well as the Python keywords.

Python Variable

Variable is a name that refers to some value. Like any other programming languages, Python allows to define variables and manipulate it in your code logic.

Name convention

Python allows to use letter, number, or underscore [_] in a variable name, but it has to start with a letter or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).

There is no limit on the length of your variable name, so you can choose anything meaningful to you in your code. but Python provided some guidelines to use lowercase as much as possible for the variables and function name.

Below are some examples of valid variable names:

a = "a"
#Python variable name is case sensitive
A = "a"
 
module_name = "Python Tutorial for Variables & Keywords"
speed_of_gravity = 299792458
pi = 3.14159265359
is_matched = True

And some invalid variable names as per below, if you use them in your code, Python throws “SyntaxError: invalid syntax” error.

1st_name = "John"
#invalid as variable cannot start with digits
first name = "John"
right/wrong = True
#invalid as variable cannot has special characters like /, whitespace, @, &, * etc., except _

Use of underscore

Take note of the _, although it is allowed to use in your variable name, it has some special meaning if you use it at the beginning. e.g. if you use _salary in your class, Python will protect it from accessing from outside of the class. This is out of scope for this topic, but do bear in mind on this.

Also if you use _ as your available name, there will be a conflict in the Python interactive mode, as in interactive mode, _ is interpreted as the result of the last executed expression, check more from this article.

You may also noticed that variables can hold different sorts of values, e.g. single character, multiple characters, numbers, and True or False etc. This is the different data type in Python, we will come to this topic in the later article.

Reserved Keywords

There are some other words we cannot directly use as variable, these words are so called Python reserved keywords, as Python uses these words to recognize the structure of the program.

Below are all the keywords reserved by Python3, and it is not allowed to use them directly as variable name.

False      await      else       import     pass
None       break      except     in         raise
True       class      finally    is         return
and        continue   for        lambda     try
as         def        from       nonlocal   while
assert     del        global     not        with
async      elif       if         or         yield

For Python beginners, if you use some IDE like PyCharm or Jupyter Notebook, these keywords will be automatically highlighted in different color, so you don’t worry about you mistakenly used them as variable name.

Python variables and keywords

Besides these reserved keywords, there are a few more words you shall try to avoid using them when defining your variable. For instance the below:

str
int
float
list
dict
set
tuple
bytes

These are the Python built-in data types which will be covered in the next tutorial. And there won’t be any error prompted immediately when you assign a value to them, but you will face some issues when you want to call the default behavior of the built-in data type later. Below is an example:

python built-in data type

The str() will throw error if you assigned “Test” to it, and it only works again if you delete the “str” as a variable. Hence the best practice is not to use these words as variable name in your code to prevent some unexpected errors and confusions.

 

Python tuple

Python built-in types – Tuples

Tuple is a python built-in data structure which holds a sequence of values, and the values can be in any data type. If you write a hundred lines of python code, it is almost impossible to avoid it in your code, as it comes in implicitly or explicitly from your variable assignment and iteration to return values of your method. In this article, I will be sharing with you where and how the tuples will be possibly used in your code.

Variable assignment with Tuple

You may have written the code in the below way to assign the values to variables in one line. The left side is the tuple of variables, and the right side is the tuple of values/expressions.

sort_by_name, sort_by_date = True, False
#output : sort_by_name True, sort_by_date False
key, val = "20200601" , "Mon"
#output : key '20200601', val 'Mon'

Sometimes if you want to swap the values of two variables, you do not need to create a temp variable for swapping. The below will do a perfect job to swap the value for key, val variables.

key, val = val, key
#output: key 'Mon', val '20200601'

Traverse the elements of a sequence

If you want to iterate through each of the elements in a sequence and meanwhile get the index of the element, you can do it by below code:

for idx, label in enumerate('ABCDEFG'):
    print(idx, label)

#output: 0, A
#1, B
#...

When iterating a dictionary, the iterms method returns a list of tuples, and each tuple is the key and value pair, e.g.:

company_info = {"name" : "Alibaba", "headquarter" : "Hangzhou, China", "founded" : "4 April 1999"}
for key, val in company_info.items():
    print(f"{key} : {val}")

If you have checked my another post – How to swap the key and value in a python dictionary, it is just an extension to the above.

Iterate multiple sequences at one time with zip

If you use the built-in zip function to iterate multiple sequences at one time, it actually returns an iterator of tuples. See the below example:

names = ["Alibaba", "Amazon", "Google"]
countries = ["China", "USA", "USA"]
years = ["1999", "1996", "1998"]
for rec in zip(names, countries, years):
    print(rec)

#output:
#('Alibaba', 'China', '1999')
#('Amazon', 'USA', '1996')
#('Google', 'USA', '1998')

Return multiple values from function

Normally a function can only returns 1 value, but with tuple, you can return multiple values even in different data types. (technically speaking, it is still 1 value but tuple type)

e.g. The python built-in method divmod:

quotient, remainder = divmod(10, 3)
print(quotient, remainder)
#output: 3 1

You can also define your own function to return multiple values like below:

def split_email(email):
    user_name, company_site = email.split("@")
    return user_name, company_site


split_email("contact@codeforests.com")
#output: ('contact', 'codeforests.com')

With this example, I am going to wrap up my article for this topic. If you have any questions or comments, please share in the below.