Setting Virtual Environment For Atari Games and Running Airstriker Genesis using gym-retro

In this blog, I will set up a virtual environment using pip, It is always better to make a virtual environment in order to perform some machine learning or reinforcement learning or any other task which depends upon different library version. You can also create a virtual environment using Anaconda but in this blog, I will go with the virtual environment created using pip. The rest of the steps will be the same.

The first thing you have to do is to install the package that will be used to create the virtual environment

pip install virtualenv

Next is to create a virtual environment using pip with the following command:

virtualenv striker
source ./striker/bin/activate

Now the virtual environment is activated. Next, install important libraries to run the retro.

pip install tensorflow
pip install retro

Next run the Airstriker-Genesis game with the sample actions.

import retro

def main():
    env = retro.make(game='Airstriker-Genesis')
    obs = env.reset()
    while True:
        obs, rew, done, info = env.step(env.action_space.sample())
        env.render()
        if done:
            obs = env.reset()
    env.close()


if __name__ == "__main__":
    main()

When you run this code you will get this error.

Read More »

Python Virtual Environment on Linux

In this blog, you can learn how to install python and pip globally on your linux system and also install the python virtual environment.

Python Installation:

On linux python is installed as default python2.7.
First you have to install python3.6 or the latest version by using ppa repository. This is  done by typing this command on terminal.

sudo add-apt-repository ppa:jonathonf/python-3.6

Then update and install the python3.6.

sudo apt update
sudo apt install python3.6

Now python3.6 is installed in your system but you have to set python3.6 as your default. You can do this by running following command.

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2

Finally switch between the two python versions for python via command:

sudo update-alternatives --config python3

Following output come on your screen:

There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.6   2         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.6   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

You can check your version via command:

Read More »

Setting up Anaconda Environment On Linux

In this blog I am going to show you the Installation process and creation of your own conda environment.So let’s start.

Installation:

First thing you have to do is click here to open a link and select a version of anaconda that you want to install on your system.You can simply click on it to download or you can download it via command:

wget https://repo.continuum.io/archive/Anaconda3-5.2.0-Linux-x86_64.sh

After downloading bash script of anaconda you can run this script via command:

bash Anaconda3-5.2.0-Linux-x86_64.sh

Then installation of Anaconda is started:

Welcome to Anaconda3 5.2.0

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>>

You’ll receive the following output:

Anaconda End User License Agreement
===================================

Copyright 2015, Anaconda, Inc.

All rights reserved under the 3-clause BSD License:

Redistribution and use in source and binary forms, with or without modification,
 are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice, this 
list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright notice, th
is list of conditions and the following disclaimer in the documentation and/or o
ther materials provided with the distribution.
  * Neither the name of Anaconda, Inc. ("Anaconda, Inc.") nor the names of its c
ontributors may be used to endorse or promote products derived from this softwar
e without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WA
RRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
--More--

Read More »

Guide to Establish Connection with S3 using Python Scripts

We will take a look at using Python Scripts to interact with infrastructure provided by Amazon Web Services(AWS).
In this tutorial, We are considering only Python scripting for S3.

First thing, You have to check python is installed in your machine by typing simple command:

which python

That will show you the path of python executable. If python is not installed in your machine first download it from this site: https://www.python.org/downloads/

After this check the version of python by typing:

python -V

We are using python 2.7.12 and you can use python 2.6 or higher.

Next thing you have to check pip is intall in your machine by typing command:

 which pip

That will show you the path of pip executable.If pip is not installed in your machine first download it from this site: https://pip.pypa.io/en/stable/installing/

Your version of pip should be 9.0.1 or newer.Check your version of pip by typing :

pip -V

Install boto3  and AWS CLI

Now basic installation is completed. Now install the library of python which is used to interact with AWS on by using this command

pip install awscli boto3 -U --ignore-installed six

Now login to your AWS account and select IAM.

Screenshot from 2018-07-16 13-08-51

This window will come on your screen.Read More »

Establish EC2 using Python Scripts

We will take a look at using Python Scripts to interact with infrastructure provided by Amazon Web Services(AWS).
In this tutorial, We are considering only Python scripting for EC2.

First thing, You have to check python is installed in your machine by typing simple command:

which python

That will show you the path of python executable. If python is not installed in your machine first download it from this site: https://www.python.org/downloads/

After this check the version of python by typing:

python -V

We are using python 2.7.12 and you can use python 2.6 or higher.

Next thing you have to check pip is intall in your machine by typing command:

 which pip

That will show you the path of pip executable.If pip is not installed in your machine first download it from this site: https://pip.pypa.io/en/stable/installing/

Your version of pip should be 9.0.1 or newer.Check your version of pip by typing :

pip -V

Install boto3  and AWS CLI

Now basic installation is completed. Now install the library of python which is used to interact with AWS on by using this command

pip install awscli boto3 -U --ignore-installed six

Now login to your AWS account and select IAM.

Screenshot from 2018-07-16 13-08-51

This window will come on your screen.Read More »

Regular Expression

A regular expression (or regex, or regexp) is a way to describe complex search patterns using sequences of characters.

Regular expressions are patterns that can be matched against strings. Regular expressions are important tools for text processing. Many text editors and most programming languages have some built-in support for regular expressions.

Regular expressions are used for searching through data. They allow you to search for pieces of text that match a certain form, instead of searching for a piece of text identical to the one you supply. For example, the regular expression [0-9]+ allows you to search through a file for any integer number.

Certain characters have special purposes in regular expressions. These are called meta-characters or meta-symbols. Meta-characters are not part of the strings that are matched by a pattern. Instead, they are part of the syntax that is used for representing patterns. Typically, the following characters are meta-characters:

      .   *   |   ?   +   (   )   [   ]   {   }   ^   $   \

These characters have special meaning in regular expressions. For example, parentheses are used for grouping, just as they are in arithmetic. If you want to use a meta-character as a regular character instead of with its special meaning, you have to “escape” it by preceding it with a backslash, such as \*\(\$, or \\.

MATCH ANY NUMBER LINE

We’ll start with a very simple example – Match any line that only contains numbers.

^[0-9]+$

Let’s walk through this piece-by-piece.

  • ^ – Signifies the start of a line.
  • [0-9] – Matches any digit between 0 and 9
  • + – Matches one or more instance of the preceding expression.
  • $ – Signifies the end of the line

We could replace [0-9] with \d which will do the same thing.

The great thing about this expression (and regular expressions in general) is that it can be used, without much modification, in any programing language.

import re

with open('test.txt', 'r') as f:
  test_string = f.read()
  regex = re.compile(r'^([0-9]+)$', re.MULTILINE)
  result = regex.findall(test_string)
  print(result)

 

Design Pattern

Initially, you can think of a pattern as an especially clever and insightful way of solving a particular class of problems. That is, it looks like a lot of people have worked out all the angles of a problem and have come up with the most general, flexible solution for it. The problem could be one you have seen and solved before, but your solution probably didn’t have the kind of completeness you’ll see embodied in a pattern.

The basic concept of a pattern can also be seen as the basic concept of program design: adding a layer of abstraction. Whenever you abstract something you’re isolating particular details, and one of the most compelling motivations behind this is to separate things that change from things that stay the same. Another way to put this is that once you find some part of your program that’s likely to change for one reason or another, you’ll want to keep those changes from propagating other changes throughout your code. Not only does this make the code much cheaper to maintain, but it also turns out that it is usually simpler to understand.

So the goal of design patterns is to isolate changes in your code.For example, inheritance can be thought of as a design pattern. It allows you to express differences in behavior (that’s the thing that changes) in objects that all have the same interface (that’s what stays the same). Composition can also be considered a pattern, since it allows you to change-dynamically or statically-the objects that implement your class, and thus the way that class works.

Classifying Patterns

The Design Patterns book discusses 23 different patterns, classified under three purposes. The three purposes are:

  1. Creational: how an object can be created. This often involves isolating the details of object creation so your code isn’t dependent on what types of objects there are and thus doesn’t have to be changed when you add a new type of object. The aforementioned Singleton is classified as a creational pattern, and later in this book you’ll see examples of Factory Method and Prototype.
  2. Structural: designing objects to satisfy particular project constraints. These work with the way objects are connected with other objects to ensure that changes in the system don’t require changes to those connections.
  3. Behavioral: objects that handle particular types of actions within a program. These encapsulate processes that you want to perform, such as interpreting a language, fulfilling a request, moving through a sequence (as in an iterator), or implementing an algorithm. This book contains examples of the Observer and the Visitor patterns.

I am discussing here only one pattern that is factory pattern.



class Dog:
    def __init__(self , name):
        self.name = name
    def speak(self):
        return "barks"
class Cat:
    def __init__(self , name):
        self.name = name
    def speak(self):
        return "Meoow"
    
    
    
def get_pet(pet = "dog"):
    pets = dict (dog = Dog('harm'), cat = Cat("Peace"))
    return pets[pet]


d = get_pet()

d.speak()

c= get_pet("cat")

c.speak()

 

Importance of Python

Pyhton is a good choice in the begginning of learning process of  programming languages. It can be used as a stepping stone into other programming languages. If you’re an absolute beginner and this is your first time working with any type of coding language, that’s something you definitely want. Python make it easy to understand some difficult concepts of programming languages.

Before starting any language you should know  some of its main characteristics.Python have following characteristics:

  • High Level Language
  • General Purpose language
  • Interpreted
  • Dynamic type

pythonlearn_thumbnail_1x1

Python is a general-purpose language, which means it can be used to build just about anything, which will be made easy with the right tools/libraries.Read More »