Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, 19 March 2011

Simple Python IRC Bot

This article shows the code of a simple python irc bot.

To run it, you should create a file, e.g: pybot.py, copy following code in it, and exec:
$ python pybot.py

other way to run it:
$ chmod a+x pybot.py
$ ./pybot.py

BOT SOURCE CODE

Saturday, 31 January 2009

Calculating Prime Numbers using python

This blog article shows a python exercise which consists on calculating some prime numbers using trial division algorithm.

You can copy this code in a script file and then execute it!

#!/usr/bin/env python

#Prime numbers
import sys
import math

print "Prime numbers"

#Asks user how many numbers he wants to calculate.
top = int(raw_input("How many prime numbers do you want to calculate?: "))

if (top <1 ) :
print "Error: Invalid number. It must be greater than one"
sys.exit

#Initializing some variables
a = 3    # first number to test if it is prime.
result = [2]  # result list begins with prime number 2.
num = 1   # number of already calculated primes.
print 2,

while num < top : # main loop
cont = 0

# Performs the division test
while 1 :
divisor = result[cont]   # we only test with already calculated prime numbers.

(quotient, remainder) = divmod(a,divisor)  # calculates quotient and remainder at the same time.

if (remainder) :
if divisor <= quotient :
cont += 1 
else:
result.append(a)
print a,  # number a is a prime.
num += 1
break
else:
break   # number a is not a prime

a += 1   # calculate next number to test.
This other script avoids testing numbers that are multiple of 2,3,5 or 7. Also calculates the integer square root to obtain the higher divisor limit to test for each number.
#!/usr/bin/env python

#Prime numbers
import sys
import math

print "Prime numbers"

#Asks user how many numbers he wants to calculate.
top = int(raw_input("How many prime numbers do you want to calculate?: "))

if (top <1 ) :
print "Error: Invalid number. It must be greater than one"
sys.exit

#Initializing some variables
a = 11   # first number to test if it is prime
statea = 0
stateb = 0
result = [7]  # result list begins with prime number 7
num = 4   # number of already calculated primes.
print 2, 3, 5, 7,

while num < top : # main loop
cont = 0

#Calculates the integer square root of a
low = 1
upper = a
while (upper - low) > 1 :
med = (low + upper)/2
temp = med * med
if temp > a :
upper = med
else:
low = med
if temp == a :
break

# Performs the division test
while 1 :
divisor = result[cont]
if (a % divisor) :
if divisor <= low :
cont += 1
else:
result.append(a)
print a,  # number a is a prime
num += 1
break
else:
break   # number a is not a prime

# Calculates next number to test if it is a prime.
if (stateb == 4) or (stateb == 6) :
a += 6
stateb += 1
elif statea :
a += 4
statea = 0
if stateb == 7 :
stateb = 0
else :
stateb += 1
else :
a += 2
statea = 1
stateb +=1

Sunday, 6 July 2008

Hello World program examples

Hello World is the first program every user usually codes when he learns a new programming language.

Its goal is learning the basics to edit, compile and execute a program which sends a "hello world" string to standard output.


We have used an Ubuntu Hardy distro based system. Most commands will serve to other distros as well, like Debian.


BASH Shell Scripting Hello World


bash command line interpreter is usually already installed in your system.

if it is not installed we type: $sudo aptitude install bash


Now we create hello.sh script using whatever text editor we prefer.
e.g: $ gedit hello.sh


Copy this lines into your hello.sh script
#!/bin/bash
echo "hello world"


Bash shell scripting is an interpreted language so we have not to compile anything. We directly execute the shell script file:

We give the shell script execution permissions:
$ chmod a+x hello.sh

and launch the program
$ ./hello.sh


PYTHON Scripting Hello World


If you do not have python installed in your system, exec:

$ sudo aptitude install python

Wednesday, 26 March 2008

Reverse Engineering Python Code with PyNSource

PyNSource is a tool that allow us to obtain UML diagrams from python source code.

PyNSource Web Page


INSTALLING PyNSource ON AN UBUNTU GUTSY SYSTEM.

NOTE: You must have python already installed in your system. I assume 2.5 version.

$mkdir PyNSource
$cd PyNSource


Now we download the program and install it:
$wget http://www.atug.com/downloads/pynsource1.4c.zip
$unzip pynsource1.4c.zip
$sudo python setup.py install


We want to run the GUI so we need a graphical library:
$sudo aptitude install python2.5-wxgtk2.6

Current code does not work with version wxgtk2.6 so we need to update it:
$wget http://www.atug.com/andypatterns/code/pyNsourceGui1.4c-wx26.zip
$unzip pyNsourceGui1.4c-wx26.zip
$sudo cp pyNsourceGui-wx26.py /usr/lib/python2.5/site-packages/pynsource/


Finally we exec the GUI module
$python /usr/lib/python2.5/site-packages/pynsource/pyNsourceGui-wx26.py


This application presents some annoying bugs:

* There are problems when moving shapes on the screen. It does not refresh correctly.
* It is difficult to adjust page size when you try to print it.
* When you press right click button on the mouse to delete an object, usually it doesn't delete all the arrows that point to it, messing the schema.
* Saving and opening diagram feature is not implemented yet, so you cannot save temporary work.


Well, you know, this bugs are annoying, but it you dont like them, it is free software, so hack the code. :-)