HOME BLOG

How to perform actions when a Python program finishes whether successfully or not

Posted on: April 25th, 2020 by Olu No Comments

Hi folks,

In this post I talk about a good way to perform actions when your Python program completes. For example you may want to clean up temporary files created by program or close database connections or other resources resources when your program terminates. You may want to do this regardless of whether the program finishes successfully or crashes due to an error. An excellent way to perform such clean-up tasks automatically is to use the atexit module. Here is an example below of how to use it in a class below.

 

import atexit


class Foo:

    def __init__(self):
        atexit.register(self.goodbye)
        x = 5

    def goodbye(self):
        print('You are leaving the program')



f = Foo()

 

If you run the following code, you will see the message ‘You are leaving the program’ just before the program exits.

Here is another example where we intentionally cause the program to crash.

import atexit


class Bar:

    def __init__(self):
        atexit.register(self.goodbye)
        x = 5 / 0 # error!

    def goodbye(self):
        print('You are leaving the program')



f = Bar()

 

If you run this program, you will see the error, but nevertheless still see the message ‘You are leaving the program’ printed before the program exits.

You can read more about atexit in the Python documentation.

That’s it for now. Happy software development.

How to run system commands in Python and get the output

Posted on: April 20th, 2020 by Olu No Comments

Hi folks,

Here is an easy way to run system commands in Python and get the output. It is by using subprocess.getoutput command. E.g.

import subprocess
output = subprocess.getoutput("ls -l ~")
print(output)

 

That’s all for now. Till next time, happy software development.

How to fix ‘Connection Refused’ error when connecting to Apache virtual host from other client ip addresses

Posted on: April 17th, 2020 by Olu No Comments

Hi folks,

In this post I talk about how to fix ‘Connection Refused’ error you may experience when trying to connect to your web application which has been set up as a virtual host on Apache server. The issue I discuss here is where you are able to access the virtual host from your local machine where the app is hosted, but not from any other machine.

It turns out that the solution to this problem lies in the server name used for the virtual host.

If you use a server name that ends in ‘.localhost’, other clients will not be able to connect to your application. So, if you have such a server name, rename it to something that does not end in ‘.localhost’.

That’s all for now. Till next time, happy software development.

How to fix ‘image not found’ error related to ‘Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib’ when you import psycopg2 on MacOS

Posted on: April 3rd, 2020 by Olu No Comments

Hi folks,

In this post we discuss how to fix an error you may come across when you run import

psycopg2. You may come across this error if you have recently updated openssl to a version greater than 1.0.0 on Mac OS.

The error looks like

 

  File "/Users/someuser/.virtualenvs/top_games/lib/python3.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 737, in dbapi
    import psycopg2
  File "/Users/someuser/.virtualenvs/top_games/lib/python3.7/site-packages/psycopg2/__init__.py", line 50, in 
    from psycopg2._psycopg import (                     # noqa
ImportError: dlopen(/Users/someuser/.virtualenvs/top_games/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
  Referenced from: /Users/someuser/.virtualenvs/top_games/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so
  Reason: image not found

 

Here is how to fix it:

Activate your project’s virtual environment.

Update psycopg2 using the command:

 

pip install psycopg2 --upgrade

 

If you come across an error like

 

ld: library not found for -lssl

 

then run the following two commands in your terminal

 

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

 

Then, try the aforementioned pip command once more.

That’s it. Once psycopg2 is upgraded, the import issue should be fixed.

How to exit ipdb

Posted on: February 15th, 2020 by Olu No Comments

Hi folks,

If you have done a lot of debugging with ipdb on Python projects in a windows environment using tools like Git bash, you may notice that it’s not very easy to exit ipdb. Pressing familiar keys like Ctrl + c doesn’t work. Here’s how to exit ipdb in Windows:

Run the following command

 

import os; os._exit(0)

 

That’s all for now. Till next time.

How to pip install Python projects from source

Posted on: February 15th, 2020 by Olu No Comments

Hi folks,

In this post I talk about how to pip install Python projects from source.

Let’s say you have a project that you can normally install via pip using a command like pip install someproject, but you want to make some customization to the project.

Here’s how to go about it.

  • First, clone the project from Github.
  • Make any modifications to the project source code as desired.
  • Then change to the project’s root directory.
  • Run the following command to install the project
pip install -e . 
  • If you have the project’s library already installed and you upgrade that version to the latest from your source code using command
pip install -e . -U

 

That’s all for now. Happy software development.

New Release: Incredible Brick Breaker 3.0.0 for iOS

Posted on: April 16th, 2019 by Olu No Comments

Hi folks,

We’re pleased to announce a new release for Incredible Brick Breaker. This is Incredible Brick Breaker 3.0.0 for iOS.

Just like the Android version, Incredible Brick Breaker 3.0.0 brings a number of new features to the game, including fireball goodie, self-shooting gun, inventory, Gold coins and a Gold shop. See our Android announcement at http://blog.yinkos.com/2019/04/new-release-for-incredible-brick-breaker-android-3-0-0/.

As always, we like to know what you think about the game. Feel free to contact us. And if you haven’t yet installed the game, get it from App Store or Google Play.

New Release for Incredible Brick Breaker – Android 3.0.0

Posted on: April 11th, 2019 by Olu No Comments

Hi folks,

We’re pleased to announce a new release for Incredible Brick Breaker. This is Incredible Brick Breaker 3.0.0 for Android.

Incredible Brick Breaker 3.0.0 brings a number of new features to the game.

There is now a new fireball goodie which changes your ball into a fireball for a few seconds, destroying bricks without deflection.

The ammo goodie has been redesigned as well.

The shooting mechanics has been updated to make the game more fast-paced. Now, once a player collects an ammo goodie, a gun appears automatically over the paddle and shoots for a few seconds. This means the shoot button is no longer required and has therefore been removed.

There are also now Gold coins in the game. Gold coins are the game currency and can be picked up by breaking bricks. You can use gold coins to buy goodies like fireball and ammo.

In the main menu, there is a button to display number of gold coins the user has and allow access to the Gold shop.

There is also now a user inventory feature which you can use to store some goodies you purchase.

To access the inventory, take your hand off the screen during game play. The game slows down and the inventory overlay appears over the screen.

The inventory comprises your goodies (fireball and ammo) and the quantity of each you have.

The Gold Shop has been added, which allows you to buy Gold coins in various quantites.

Music within game has been removed as there are now more sound effects.

As always, we like to know what you think about the game. Feel free to contact us. And if you haven’t yet installed the game, get it from App Store or Google Play.

Generic Programming

Posted on: March 16th, 2019 by Olu No Comments

Hi folks,

Today I will like to talk a little bit about a programming pattern I really like. It’s called generic programming.

According to Wikipedia, generic programming is a style of computer programming in which algorithms are written in terms of types to-be-specified later that are then instantiated when needed for specific type provided as parameters.

It is useful if you have certain patterns you want to enforce in a class and you want the class to be reusable for arbitrary types of items.

E.g. a grid class that takes a kind of item and can add new item, remove items, etc.

The items it works with all have to be of a certain type.

You can make the class like

class Grid<T> {
    public addItem(item: T) {
        // ...
    }

    public removeItem(item: T) {
        // ...
    }

    public getItems(): T[] {
        // ...
    }
    
}

Note that once you define the generic variable T in the class declaration, you can then later define method input parameters and return types in terms of the generic parameter.

You can then reuse this class for all sorts of items, e.g. books, actors, etc. as follows

const productsGrid = new Grid();
const actorsGrid = new Grid();

This way, when you want to perform actions on your object, the type of input parameters and output can automatically be determined for error checking by your IDE, etc.

So, if you want to write nice reusable classes, a way to consider is generics.

That’s it for now.

How to fix error java.lang.OutOfMemoryError: Java heap space in Android Studio

Posted on: October 23rd, 2018 by Olu No Comments

Hi folks,

Here’s a quick tip on how to fix error java.lang.OutOfMemoryError: Java heap space when working with Android Studio.

Open gradle.properties,
find org.gradle.jvmargs
set -Xmx property to something bigger, e.g.
if you had -Xmx1500m, you can change it to -Xmx3000m

This way JVM uses max heap size of 3000MB
That’s all for now.
Happy coding.