3

Python Helper

 3 years ago
source link: https://pyhelper.in/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Assistance in python when u need it most

The second and last part containing the two sections of connecting to embedded device will be seen here. I am going to get the support of yaml to store the commands.

Before going into how we are going to do it, lets see a bit about what yaml is.

yaml or YAML Ain’t Markup Language 🙂 is a human friendly data serialization standard. It has APIs in all programming languages and we are going to use the python version of yaml called the PyYaml.

In my words using pyyaml we are going to store the set of commands and their expected output in a human readable format. We will see how.

PyYaml has a specific format for storage of the information. We are going to make use of the nesting facility in yaml syntax to nest both a command and its subsequent result together. To learn more about pyyaml and its syntax please visit http://pyyaml.org/wiki/PyYAMLDocumentation.

The file is stored as command.yaml and contains information like below.

– command: command_to_be_exec
result: result_of_cmd
– command: second_cmd
result: res_of_sec_cmd

The above format actually corresponds to Python dictionary format where “command” is key and “command_to_be_exec” is the value. This recognition will be important as it will be useful when you look at the parsed output. so using pyyaml we are going to parse and get the commands in pythonic format for easy execution.

import yaml
conts = yaml.load(open("text.yaml"))

We parse the contents given above using the load method in pyyaml. The method loads and generates a list of dictionaries where the command to be executed will be value and the “command” word will be key. Exactly as given in the yaml file.

print conts
[{'command': 'command_to_be_exec', 'result': 'result_of_cmd'}, {'command': 'second_cmd', 'result': 'res_of_sec_cmd'}]

Now it will be easy to create a small python module to combine both.

import devconnect
def parse_yaml(yaml_file):
"""
Method to parse the yaml file provided and give the contents in pythonic format.
:param yaml_file: yaml file to be parsed
:return: contents of the file
"""
conts = yaml.load(open(yaml_file))
return conts
def executeCommands(hostname, username, password):
"""
Method to execute the commands in yaml by integrating with the devconnect
file and creating a object for DevConnect class.
"""
cmdfile = "commands.yaml"
dcon = devconnect.DevConnect(hostname, username, password)
data = parse_yaml(cmdfile)
for test_no, cmd in enumerate(data):
print "Executing test number: %d and command: %s" % (test_no+1, cmd["command"])
rc, out, err = dcon.runCommand(cmd["command"])
print "out"

Now the above code just prints the output executed in the device. But you guys can use these modules in more efficient way and create an entire test suite for the device.

That’s all folks. Please dont forget to like my posts if its good and informative. And dont forget to look into the documentation of paramiko and pyyaml. I have provided them in the references.

References:

http://docs.paramiko.org
http://pyyaml.org/wiki/PyYAMLDocumentation

Command execution is one of the important part to be automated in any devices like Switches, Routers or any other embedded device. Any specific task to be done in such embedded devices may result in running a set of commands. This may be a basic configuration or a specific feature configuration done in the device. It is best to automate this as it can reduce the effort of the tester.

Lets see a common way of doing this. I am going to take the help of paramiko and pyyaml to do the same.

So we are going to see this in three sections. They are,

  1. Section which talks about connecting to the embedded device with paramiko
  2. Section which talks about the parsing the pyyaml
  3. Section which talks in integrating both to get results

Lets see the first part here.

paramiko enables us to connect to the device through a secure SSH channel. The module enables us to create a SSH Channel, SSH client, Transport etc. To learn more about the module please visit http://docs.paramiko.org/

They have explained the API well in the above link.

Here we are going to see just a way to connect to device using this package.

So first of all we are deciding to create a class for the same. This is because we dont want to make it functional and call the device each and every time for each command to be executed. So how are we going to use paramiko to connect to a device?

First start with the import of the module

import paramiko

paramiko has a class with which we can connect to a particular host and port with a username and password provided for authentication. This class is SSHClient. We need to create an object and use connect() method to connect to the device.

dev_client = paramiko.SSHClient()
dev_client.connect("krishnan.device.com", "user1", "password1")

Now dev_client has connected to the device with hostname “krishnan.device.com” authenticated with credentials “user1” and “password1”. Now we can use the client to execute commands in the device and return the output. This can be done using the exec_command() method in SSHClient Class.

rc, output, error = dev_client.exec_command("command_to_execute")

This method returns the stdin, stdout and stderr as 3 tuple. We can grep the rc for command execution status. If pass we can check the output variable for output and if fail check the error variable for error.

We can even create an interactive shell session for executing string of commands using it. This is done by the method invoke_Shell(). It opens a new channel connected to a pseudo terminal.

Now lets see how to put things together in a class.

__author__ = 'Krishnan'
import paramiko
from paramiko.ssh_exception import SSHException
class DevConnectException(Exception):
pass
class DevConnect(object):
"""
This Class is uses Paramiko module to create a SSH Connection to
the Device under test named. The DUT can be contacted through
commands remotely from a server by opening a secure shell connection
through this class
"""
def __init__(self, host, uname, passwd):
"""
Object initialisation
:param host: Hostname of the DUT
:param uname: Username of the DUT
:param passwd: Password of the DUT
:return:
"""
self.host = host
self.uname = uname
self.passwd = passwd
self.devclient = paramiko.SSHClient()
self.devclient.connect(host, uname, passwd)
def createChannel(self):
"""
This method will be used to create a interactive connection
between the Device under test and server for sending commands
through SSH Connection.
:return: The new SSH Channel created
"""
try:
self.devchannel = self.devclient.invoke_shell()
except SSHException, err:
raise DevConnectException("Unable to invoke the SSH Command shell")
def runCommand(self, cmd):
"""
This Method will be used to run a command in the Device under test
and return the output produced. If the command fails then the
RtConnectException is raised.
:param cmd: The command to be executed
:return: tuple of command RC, command output and command error if any
"""
try:
stdin, stdout, stderr = self.devclient.exec_command(cmd)
return (stdin, stdout, stderr)
except SSHException, err:
raise DevConnectException("Execution of command '%s' failed" % cmd)
def closeConnection(self):
"""
This method will close the created SSH Connection
:return: None
"""
self.devclient.close()


References:

docs.paramiko.org

Hi Guys, welcome again to another fascinating part in Experimenting with pygame. In this part I am not going to disappoint you guys. You are actually going to build user interactive game today. Interested??

Come lets build a very simple game. Don’t go with the title :). This is just a simple game. You guys would know well about the famous game SNAKE in nokia phones. Well this is kinda adapted on that. Here we are going to interact with Mario to keep him well within the bounds of the window. If he hits the boundary then its GAME OVER. Really simple game to make. So the take away in making this game are as follows;

  • How to capture user interactions in the game and manipulate accordingly
  •  Write some complex code enough to make it a game. 🙂

So as usual, first the code.

__author__ = 'krishnan'
import sys, time
import pygame
# Initialising pygame
pygame.init()
# Setting the frame rate variable
# X, y coordinates and direction control variable
fps = 40
x, y = (0, 0)
direction = ('x', 1)
# Setting the window size and screen according to it
window_size = length, width = (800, 600)
screen = pygame.display.set_mode(window_size)
clock = pygame.time.Clock()
# Creating the font for displaying the Game over
gameover = pygame.font.SysFont("Arial", 48)
text_to_render = gameover.render("Game Over", 1, (255, 255, 255), (0, 0, 255))
# Loading the Mario character to the pygame environment
mario = pygame.image.load("Mario.png")
mario = pygame.transform.scale(mario, (100, 50))
# Getting the size of mario
marioSize = mario.get_size()
screen.blit(mario, (x, y))
def gameFinish():
"""
This is a simple function which exits the game because the user lost.
The function is called when character hits the window walls.
:return: No returns
"""
# Display the Game over text on the screen
screen.blit(text_to_render, (0, 0))
pygame.display.flip()
time.sleep(2)
# Exit from the game
sys.exit()
# Game Loop
while True:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Get all the four arrow events and
# move the character according to arrow key pressed
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
direction = ('x', 1)
if event.key == pygame.K_LEFT:
direction = ('x', -1)
if event.key == pygame.K_DOWN:
direction = ('y', 1)
if event.key == pygame.K_UP:
direction = ('y', -1)
# Condition for the Game loss and exiting. If character hits the walls
# of the window, user loses the game.
if (x + marioSize[0] > length or y + marioSize[1] > width) or (x < 0 or y < 0):
gameFinish()
# Logic to move the character
if direction[0] == 'x':
x += 5 * direction[1]
else:
y += 5 * direction[1]
# Render the display with the background and the current character position
screen.fill((0,0,0))
screen.blit(mario, (x, y))
pygame.display.flip()

So we can see that our amount of code increases as our game logic gets more complex. So most of the parts you guys know. Or else please visit my previous posts to know.

So regarding this post we are going to see how an interactive game is built. So the basic game logic is

  • The character keeps moving in a particular direction.
  • We need to keep him of the boundary.
  • To change the direction we use the arrow keys.

So as usual we,

  •  Load the character to pygame
  • Create the game loop
  • Initiate the Clock instance with fps as it is an animation game
  • Create the basic exit scenario (clicking the close button)

In the previous post the superman moves on his own wish. That is upward.So there was no interaction. But now as a game we are going to need user to control the character using arrow keys. We already know that we have a for loop which gets all events in each iteration. Using this we can solve this. We know that every interaction is an event. So a keyboard key press will also be an event. So we capture that and do the logic accordingly.

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
direction = ('x', 1)
if event.key == pygame.K_LEFT:
direction = ('x', -1)
if event.key == pygame.K_DOWN:
direction = ('y', 1)
if event.key == pygame.K_UP:
direction = ('y', -1)

In the above code we check if the event type is a KEYDOWN. If it is a KEYDOWN we check if it is any of the four arrow keys by checking the key attribute in event. Then according to the key pressed we set the direction control variable. The direction variable is a tuple with two elements depicting the axis to move and the direction to take.

So that was the user interaction using keyboard. Now a game should have a win or lose part. The winning part is to keep Mario within bounds. Losing part comes when Mario hits the window boundary.

So for this we get the size of image first. Because the coordinates of the image is based on the top left corner of the image and not the complete one. So the image can go beyond boundaries. So we decide this by taking the imagesize and adding it with the actual x and y coordinate to check the boundaries.

mario = pygame.image.load("Mario.png")
mario = pygame.transform.scale(mario, (100, 50))
# Getting the size of mario
marioSize = mario.get_size()
if (x + marioSize[0] > length or y + marioSize[1] > width) or (x < 0 or y < 0):
gameFinish()

Finally the based on the direction variable set using keyboard interaction we use the same superman logic to reposition the Mario in the window.

if direction[0] == 'x':
x += 5 * direction[1]
else:
y += 5 * direction[1]
# Render the display with the background and the current character position
screen.fill((0,0,0))
screen.blit(mario, (x, y))
pygame.display.flip()
mario_win
game_over

And thats it. We made our first User interactive game 🙂

Hi Guys. Welcome to the pygame experimentation. Continuing from the previous post, we saw how to print a Hello world like text in the pygame window. That’s nice but not cool. Because when doing game development, we like to see something game like. Yeah now we are talking Game. Let’s now see some game like animation and how to make it.

So everybody loves Superman including me. Normal superman flies on his own, but our pygame superman needs our help. So lets make him fly through the clouds.

From this pygame experiment we will see,

  • How to load a picture into pygame.
  • How to transform the picture according to the need.
  • How to right a simple code to animate the picture.

So here we are going to animate superman to fly straight up amidst the clouds. So we need to get two set of pictures, superman and clouds. So first the code. Then we will break it up.

__author__ = 'Krishnan'
import sys
import pygame
# Initializing pygame
pygame.init()
# Setting the window size
window_size = length, width = (800, 600)
# Setting the frames per second to 20 fps
fps = 20
# Initializing the pygame clock object
clock = pygame.time.Clock()
# Setting x and y coordinates
x, y = (300, 300)
# Creating the screen object
screen = pygame.display.set_mode(window_size)
# Loading the images into the pygame window
superman = pygame.image.load("superman.png")
clouds = pygame.image.load("clouds.png")
# Transforming the image according to the window scaling
superman = pygame.transform.scale(superman, (200, 200))
clouds = pygame.transform.scale(clouds, (800, 600))
# Loading the images on the screen
screen.blit(clouds, (0, 0))
screen.blit(superman, (x, y))
# The Game Loop
while True:
# Setting the clock tick to the fps defined above
clock.tick(fps)
# Looping through the events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Filling the background or redrawing background
# each and every time to avoid unclean rendering
screen.blit(clouds, (0, 0))
# Logic of the program
if y == 0:
y = 300
else:
y -= 10
screen.blit(superman, (x, y))
pygame.display.flip()

I will exclude the basics of pygame. If you want to know you can go to my first post on pygame. Let’s directly jump into the necessities for this part.

First of all you need to load the pictures into pygame. We use the load() method in pygame.image to do the same.

superman = pygame.image.load("superman.png")
clouds = pygame.image.load("clouds.png")

The pictures you get need not always be suiting to your window requirement. Hence we need to resize or transform the pictures to suit to our pygame resolution. This can be done by the scale method in pygame.transform.

superman = pygame.transform.scale(superman, (200, 200))
clouds = pygame.transform.scale(clouds, (800, 600))

Now we have loaded the image and transformed it to our need. Now lets draw them onto the screen. We use the same blit() method for drawing the images too.

screen.blit(clouds, (0, 0))
screen.blit(superman, (x, y))

Now put in your Game loop and the for loop to get the list of events. Dont forget the exit event. Now to the program logic. So we are going to make superman fly continously from bottom to up. Hence we have initially drawn superman at the bottom of the screen. So our logic is to bring superman up till the ‘y’ coordinate reaches zero. When it reaches zero we reset to original position at the bottom. By doing this we animate our superman to fly.

superman_flying

Now thats not all. There are couple of things we must keep in mind.

  • We must never forget to redraw the background. Here it is the clouds. Or else the animated images gets drawn in unclean background and animation will look some thing like this.
unclean_bg
  •  For animations use the pygame.time.Clock(). Because the default animation time will differ from machine to machine. If our machine is slow we can see the animation for a glimpse. In faster machine the animation happens in a flash. So to make it uniform we must use the clock object and give the frames per second.

Thats all for this part folks. Stay tuned for more on pygame. 🙂

Games fascinates all including me :). Playing games is something dear to me like python. This actually inspired me to learn Game Development with my favorite language. So is Game Development possible with Python?

Of Course. Even without the game libraries it is possible to make small games in python. I have made a small connect four game without using game libraries in python. But for now let’s leave that aside and focus on Game development using a game library in python called PYGAME.

Pygame is one of the famous game library in python with more than million downloads. It is designed to make cool games in python which is built on top of excellent SDL library. SDL or Simple DirectMediaLayer is a low level library on top of OpenGL and Direct3D to interface with audio, keyboard, mouse, joystick, and graphics hardware. Hence Pygame is highly portable and runs in any platform you name.

Now lets get into Pygame directly by creating some cool things.

Lets start in the conventional way by creating a Hello World program. For a difference let it be a “First Pygame experiment” Program.

__author__ = 'Krishnan'
import sys
import pygame
# This will initialize all the features needed in pygame
pygame.init()
# We are specifying the size of the window needed to display
window_size = (600, 400)
# We are initialising the screen with the resolution specified
screen = pygame.display.set_mode(window_size)
# Creating a SysFont font object for rendering text
font = pygame.font.SysFont("Arial", 32)
# Setting the text to italics before rendering
font.set_italic(1)
# Rendering the text needed with proper text color and background color
text_to_render = font.render("My First Pygame Experiment", 1, (255, 255, 255),
(0, 0, 255))
# Game loop
# Games always don't close on their own. They run an infinite loop until
# the user himself closes it.
while True:
# Looping over multiple events happening in the game environment
for event in pygame.event.get():
# Checking if the event is to QUIT from the game.
# ex: pressing the close button
if event.type == pygame.QUIT:
sys.exit()
screen.blit(text_to_render, (0, 0))
# Method to regularly update the display according to the user
pygame.display.flip()

Now lets discuss how the above was built. As always to access all pygame resources we need to import the module.

import pygame

After importing all the pygame modules we must initialize them. Each and every modules have their own initialization, but it is always good to do a master intialization immediately after import.

pygame.init()

Now the game runs on a window. Hence we create window with resolution of our choice.

window_size = (600, 400)
screen = pygame.display.set_mode(window_size)

Now we have to show the screen to user. For that we are using the following code

pygame.display.flip()

Every game does not end on itself. The User interaction only ends the game. Hence it is always wise to have a infinite loop running with a user interaction mapped to exiting the game window. The while loops infinitely till the event happening is a pygame.QUIT event. That will be user closing the window. Again inside the loop we regularly update the window.

# Game loop
# Games always don't close on their own. They run an infinite loop until
# the user himself closes it.
while True:
# Looping over multiple events happening in the game environment
for event in pygame.event.get():
# Checking if the event is to QUIT from the game.
# ex: pressing the close button
if event.type == pygame.QUIT:
sys.exit()
# Method to regularly update the display according to the user
pygame.display.flip()

Now for our business logic in the game. We are going to show the text “First Experiment in Pygame” in the window. For that we are using below code.

screen.blit(text_to_render, (0, 0))

If you see clearly the text_to_render variable is not a text. We cannot directly give a text to the pygame display. It throws the below error.


Traceback (most recent call last):
File "/home/krishnan/Krish-Python-Venture/Game Development/pygame_startup/hello_world.py", line 28, in <module>
screen.blit("Hello World", (0, 0))
TypeError: argument 1 must be pygame.Surface, not str

This means that pygame display always needs a Surface class object and not plain string. So lets create one.

font = pygame.font.SysFont("Arial", 32)
text_to_render = font.render("First Pygame Experiment", 1, (255, 255, 255),
(0, 0, 255))

Here we are creating a SysFont object which loads a System font to the display. We choose Arial font with 32 size and create a SysFont object. Then using the render method we create a font “First Pygame Experiment” on the display. render method takes in the text to display, antialias arguement which decides the smoothness of characters, font color and font background color. Here we create a font with size 32, white foreground color and blue background color.

Now we have our first ever experiment in pygame. A window with our text on it.

pygame_hello_world

Watch out for cool stuffs in pygame following this. 🙂


References

http://www.pygame.org/docs

Hi to All Python Lovers out there !!!

DSC_0234

I am Krishnan making my entry into blogging about python.

That’s me above with my favorite programming language’s logo and wearing Pycon TShirt:).

I have been using python for the past 5 years for Test Automation. I have spent the time writing frameworks, scripts for testing the Device Under Test using Python. Now I am eager to explore the vast applications of Python. I have decided to touch each and every application in python and check out which suites me better. My exploration is going to start from Core python programming to Web development using Django, Flask. Game programming using pygame to Network programming. I am also going to blog about my experiences on the same. So guys out there please welcome my blog and guide me during my first steps into Python programming and blogging.

More to come in my blog. Keep tuned to it 🙂

– Krishnan

Welcome to WordPress.com! This is your very first post. Click the Edit link to modify or delete it, or start a new post. If you like, use this post to tell readers why you started this blog and what you plan to do with it.

Happy blogging!

Post navigation


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK