www.main.lv

Don't think just code it

Menu

  • Projects
    • Robatik
    • ArpSni
  • Samples
    • FreeBSD Asm
    • Linux Asm
    • PyGame Tutorial
    • UNIX programming
    • PSP programming
    • AVR
    • Math
  • Contact

Tags

algo (1)asm (19)attractor (2)avr (2)blender (3)bug (1)c (25)coalision (2)debug (3)editor (1)elf (1)fractals (2)freebsd (3)game (3)generator (1)gimp (1)int80h (22)map (1)math (5)mit (1)nano (1)net (2)opengl (1)plugin (1)post (2)povray (1)psp (3)pygame (19)python (28)robatik (2)sdl (3)skype (2)sql (1)towers (2)tutorial (7)voronoi (1)wudu (1)

Archive

  • 2010 august (1)
  • 2010 july (2)
  • 2010 june (1)
  • 2010 april (2)
  • 2010 march (2)
  • 2010 february (2)
  • 2010 january (2)
  • 2009 december (3)
  • 2009 november (8)
  • 2009 october (3)
  • 2009 september (5)
  • 2009 august (1)
  • 2009 july (1)
  • 2009 june (1)
  • 2009 may (1)
  • 2009 april (3)
  • 2009 march (1)
  • 2009 february (2)
  • 2009 january (1)
  • 2008 october (2)
  • 2008 september (4)

2008-10-26 Modifed Blender Towers

This is modifed verdion of this script. There was added only few new lines of code. Final result is better than simply generated boxes of random height. Also I have added 3 images of 2 random generated towns.

Script


2009-01-27 Gimp Plugin


Frend asked to write this plugin because he has tired to process his photos with same method many times. Plugin writen in Python. Copy plugin file in gimp plugin directory. Under linux you shold make it executable. Start Gimp and plugin works in Filters->Automated

Plugin Source


2009-02-12 Pygame Border Coalision

I wanted to know how many simple object can be drawn by Pygame. Red point is starting point for flying bullets. And only thing with bullets have coalision is screen border. You can shut new bullets with mouse.


Download Script


After some optimisations script works faster.

Download Script v2


Also I have implemented the same on C++.

Download Source


2008-10-05 Blender Towers



Blender Towers is script for generating random height blocks. Resulting image looks like city.
Download


2008-09-18 Blender Fractal

Python Script that generates in Blender3D simple fractal.

Download


2008-09-14 Star generator



Python scripth with generating picture of points from bmp image. Colors in bmp image is like koeficent map with allow generate more random points in any place of image you want. There shown source image from what where generated points on black image.
Download


2008-09-07 Simple Fractals

Here is some Fractal examples. That is my first fractals.


Start Script


Circle Script


2008-09-07 Robatik


I like programming and in past i played AutoWar. This is my first game in python. At this moment I dont develop it. Package contains Python script,tutorial and examples.

Robatik Game


2009-02-14 PyGame Bullets with Boxes

You can shut your bullets and they will contact with difrent boxes with diffrent result there is boxes that speed up, freeze, slow down bullets.


Download


2009-03-18 PyGame Post

Wanted to know how will looks when mousepointer moving and all arrows shows it direction


Download


2009-04-13 Map Editor

This is simple map editor for small games. Also I think it will be useful for other people who started writing game and made first test maps.

Download


2009-04-15 PyGame Rotating Flower

Flower where all list rotating around lists.

SDownload


2009-05-07 PyGame Attractors

I have found old article about attractors that is resoult what I have done with playing with attractor values. Attractor formula is very simple but result is interesting.


2009-06-02 Voronoi distance

I where interested to see how looks visualy distance to the closest point. When I saw result i remembered that it is Voronoi diagramm.


Download


2009-11-12 Python Skype Sync Status

This script synhronize your status with selected user status.

python SyncStat.py favorite_uzer



Script


2009-07-29 Python Skype Status Check

Script that checks and writes (in case of change) status of Skype users to the SQLite database. Communication with Skype is done via Skype4Py module. 


Use:
Create database file:
python SetDB.py
Run Skype Status check:
python CheckStatus.py
Generate Hourly statistics:
python GenerateStat.py


Download.


Dependencies:

Download Skype4Py


2009-08-23 Python PyGame Tutorial


Python Pygame tutorial. This tutorial goal show how to make small and simple pygame pythone script that include all that is need for bigger application. Every tutorial part add only few new lines of code.

Moving Boxes

Goal

Make featured applications with boxes that coaliding one with another.

1. [Set Display]
2. [Event Sytem]
3. [Image loading]
4. [Box object]
5. [Box move]
6. [Boxes move]
7. [Randomnes]
8. [Fullscreen]


2009-09-15 Python Pygame Tutorial Box Move

added constants that helps controlling screen size

SCREEN_X = 500
SCREEN_Y = 500
BOX_SIZE = 20
BOX_SPEED = 1
box have speed by axis
self.dx = BOX_SPEED
self.dy = BOX_SPEE
detecting if given rect is inside screen borders or not if not then change it direction
def move( self ):
        if self.rect.left+BOX_SIZE > SCREEN_X:
            self.dx = -BOX_SPEED
        if self.rect.left < 0:
            self.dx = BOX_SPEED
        if self.rect.top+BOX_SIZE > SCREEN_Y:
            self.dy = -BOX_SPEED
        if self.rect.top < 0:
            self.dy = BOX_SPEED
        self.rect.left += self.dx
        self.rect.top += self.dy
after few line of code where added box move inside given screen and coalide with screen borders

Tutorial Source


2009-09-23 Python Pygame Tutorial Boxes Move

New class boxes handles many boxes. Number of boxes depends on cpnstant MAX_BOXES.

MAX_BOXES = 100

and all class Boxes methods is the same only diference is that it handles many boxes.

Tutorial Source


2009-09-23 Python Pygame Tutorial Randomnes

All boxes moving with same speed in same directions and all boxes have same size and color
Make changes step by step to see result

self.dx = randint(1,BOX_SPEED)
self.dy = randint(1,BOX_SPEED)
and boxes now moving all seperatly at diferent directions.
self.boxes.append( Box( i*2 , i*2 , randint(BOX_MIN_SIZE,BOX_MAX_SIZE) ,
(i,0,0) )
now boxes have diferent sizes

Tutorial source


2009-09-23 PyGame Lorenz attractor

Here is Lorenz attractor whery popular attractor i have used basic coloring tehnique.

Script

PovRay source


2009-10-08 Basic HTTP server

Basic HTTP server. When you type url it shows listing of your local directory. If you tipe with path to file name noting hapens
Use:
http://*.*.*.*:/ -> disk start directory
http://*.*.*.*:/home/ -> home directory
Run:
./server port

Compile:
gcc server.c -o server

C Source


Here is also python source. It runs on port:8081 and prints in terminal HTTP request. You can see what browser sends to server.

Py Source


2009-11-12 Python PyGame Tutorial Fullscreen

added new variable tha tcheck in with mode now screen is
fullscreen = False

Used new event type

if event.type == pygame.KEYDOWN:

When pressing key m we making new screen surface in fullscreen mode. If screen is in fuulscreen mode make window.

if event.key == pygame.K_m:		
  if not fullscreen:
    screen = pygame.display.set_mode( (SCREEN_X,SCREEN_Y) , pygame.FULLSCREEN )
    fullscreen = True
  else:
    screen = pygame.display.set_mode( (SCREEN_X,SCREEN_Y) )
    fullscreen = False



Tutorial Source


2010-03-16 Python Manage Lynksys Router

Good fellow asked me to write some script that will help him to turn on/off passway to
global network. There was used linksys machine for controlling such stuff


Here is some code that login, change some rulles and logout. Also pygtk script that do
it in visual way

from linksys import *
 
ls = LinkSys( "http://192.168.1.1/" )
ls.login( "admin" , "admin" )
ls.setip( STATIC_IP , "gateway" , 10 , 66 , 66 , 66 )
ls.setip( STATIC_IP , "subnet" , 255 , 255 , 255 , 0  )
if ls.response():
	print "Succes"
else:
	print "O_O AIam BAd GUy -^-"
ls.logout()
Everything was writen in early 2009. I have tested at that days. Now I don't have linksys machine
to test it.

Source


2010-04-24 CVS 2010-1160 Exploiting nano

CVE-2010-1160 Nano Changed File Symlink Privilege Escalation

Usualy if I have to edit some file I am using nano editor.
It is almost on every distribution and easy and fast to use. Some time ago i hated vim
beacouse of Ctrl-D =] and that way used nano or pico. Now I know how to exit from vim :q!. After this bug
reported in  CVE i was exited to check it out in real life. It is first bug that i have fully tested.

This bug is fixed in newest versions. Testing all nano version this bug works
on < 2.1.7 versions now on my system is latest nano version and I have
compiled many < 2.1.7 versions to test this bug.

To get your nano version run:
$ nano -V

When user is editing file nano don't check if it is edited by some
one else. When saving file it simply save it and dont check if it was modified. If file was changed by some one else
then nano will overwrite it with his text. But it can be changed to symlink that points to other file. How to use it in real life:

1) Open file with nano
2) Change file or set symlink
3) Make changes in file and save file in nano
4) See result in symlinked file

Everytning looks like
$nano text.txt
Now some one do:
$ls -s empty.txt text.txt
Nano save
whach you save in text.txt

In  python it looks like:

os.remove( "text.txt" )
open( "empty.txt" , "w" ).close()
os.symlink( "empty.txt" , "text.txt"
Python step by step

If you are root and opening file with owner isnt you. Than owner while you
editing his file can set
symlink to some "/etc/important.conf" and you will overwrite it with some
other unrelated info. This can make some harm to your system.

How can it be exploited in real life by "small unpreviliged user". Make some interesting file
that root will interested in. Make some process that whachs nanos running in system. If nano opened file is our , symlink it.

1)Detect running nano in system
2)Check with file is opened
3)If file is yours make symlink

Nano catch

Script is only for user and dont work if you try to symlink root opened nano. It makes
all steps as described above. Change script variables for your tests:
debug = True
nano = "nano-2.0.9"
user = "user"
sym_path="/home/user/empty.txt"
Tested only with python 2.6.5


Simply be uptodated or if you using old nano dont open with privileged user unpriveleged user files.
It will save you from this bug.


Linkage:
[1] http://osvdb.org/show/osvdb/63872
[2] http://cve.mitre.org/cgi-bin/cvename.cgi?name=2010-1160
[3] http://drosenbe.blogspot.com/2010/03/nano-as-root.html
[4] http://svn.savannah.gnu.org/viewvc/trunk/nano/ChangeLog?revision=4503&root=nano&view=markup



2010-07-20 Python MIT Binary Trees

After reading chapter form MIT Introduction in algorithms about trees I have implemented same algorithm in python
I haven't tryed to make best perfomance only easy to understund and one to one like is in pseudo-code

Tree python class that are used to represent BinaryTree.

class Tree:
	p = None
	left = None
	right = None
	key = 0

pseudo-code
Inorder_Tree_Walk( x )
if x != NIL
    then Inorder_Tree_Walk( left[x] )
        print key[x]
        Inorder_Tree_Walk( right[x] )

python code
def inorder_tree_walk( t ):
    if t != None:
        inorder_tree_walk( t.left )
        print t.key,
        inorder_tree_walk( t.right )

pseudo code
Tree_Search( x , k )
if x = NIL or k = key[x]
    then return x
if k < key[x]
    then return Tree_Search( left[x] , k )
    else return Tree_Search( right[x] , k )

python code
def tree_search( t , k ):
    if (t == None) or (k == t.key):
        return t
    if k < t.key:
        return tree_search( t.left, k )
    return tree_search( t.right, k )

pseudo code
Tree_Minimum( x )
while left[x] != NIL
    do x <- left[x]
return x
python code
def tree_minimum( t ):
    while t.left != None:
        t = t.left
    return t
pseudo code
Tree_Maximum( x )
while right[x] != NIL
    do x <- right[x]
return x
python code
def tree_maximum( t ):
    while t.right != None:
        t = t.right
    return t
python code
def tree_root( t ):
    while ( t.p != None):
        t = t.p
    return t
pseudo code
Tree_Successor( x )
if right[x] != NIL
    then return Tree_Minimum( right[x] )
y <- p[x]
while y != NIL and x = right[y]
    do  x <- y
        y <- p[y]
return y


python code
def tree_successor( t ):
    if t.right != None:
        return tree_minimum( t.right )
    y = t.p
    while (y != None) and (t == y.right):
        t = y
        y = y.p
    return y
pseudo code
Tree_Insert( T , z )
y <- NIL
x <- root[T]
while x != NIL
    do y <- x
        if key[z] < key[x]
            then x <- left[x]
            else x <- right[x]
p[x] <- y
if y = NIL
    then root[T] <- z
    else if key[z] < key[y]
        then left[y] <- z
        else right[y] <- z
python code
def tree_insert( t , z ):
    y = None
    x = tree_root( t )
    while x != None:
        y = x
        if z.key < x.key:
            x = x.left
        else:
            x = x.right
    z.p = y
    if y == None:
        r = tree_root( t )
        r = z
    else:
        if z.key < y.key:
            y.left = z
        else:
            y.right = z
           
def tree_insert_recrusive( t , z ):
    if t.left == None and t.right == None:
        if z.key < t.key:
            t.left = z
        else:
            t.right = z
        return
    if z.key < t.key:
        tree_insert_recrusive( t.left , z )
    else:
        tree_insert_recrusive( t.right , z )

pseudo code
Tree_Delete( T , z )
if left[z] = NIL or right[z] = NIL
    then y <- z
    else y <- Tree_Successor( z )
if left[y] != NIL
    then x <- left[y]
    else x <- right[y]
if x != NIL
    then p[x] <- p[y]
if p[y] = NIL
    then root[T] <- x
    else if y = left[p[y]]
        then left[p[y]] <- x
        else right[p[y]] <- x
if y != z
    then key[z] <- key[y]
return y

python code
def tree_delete( t , z ):
    if (z.left == None) or (z.right == None):
        y = z
    else:
        y = tree_successor( z )
    if y.left != None:
        x = y.left
    else:
        x = y.right
    if x != None:
        x.p = y.p
    if y.p == None:
        r = tree_root( t )
        r = x
        t = r
    else:
        if y == y.p.left:
            y.p.left = x
        else:
            y.p.right = x
    if y != z:
        z.key = y.key
    return y

Example of usage:
Now we can use out tree. There is some more functions like create_tree that creates binary tree from given array.
And print_tree that print all ree values.
keys = [10,6,1,0,3,8,7,9,21,15,11,17,25,23,46]
max_deep = log(len(keys),2)

def create_tree( n=0 , p=None):
    if (len(keys) == 0) or (n >= max_deep):
        return None
    t = Tree()
    t.p = p
    t.key = keys.pop(0)
    t.left = create_tree( n+1 , t )
    t.right = create_tree( n+1 , t)
    return t

   
   
def print_tree( t ):
    if (t != None) and (t.key != None):
        if t.left == t.right == None:
            print "Key:%d "%(t.key)
            return
        if t.left.key == None:
            print "Key:%d Right:%d"%(t.key,t.right.key)
            print_tree( t.right )
            return
        if t.right.key == None:
            print "Key:%d Left:%d"%(t.key,t.left.key)
            print_tree( t.left )
            return
        print "Key:%d Left:%d Right:%d"%(t.key,t.left.key,t.right.key)
        print_tree( t.left )
        print_tree( t.right )


t = create_tree()
r = tree_search( t, 10 )
n = Tree()
n.key = 150
tree_insert_recrusive( t , n )
inorder_tree_walk( t )
print ""
tree_delete( t , r )
inorder_tree_walk( t )
print ""
r = tree_root( t )
print r.key



Source


2010-07-26 Snake by wudu

Terminal snake. First project in python by wudu

Author: wudu


Source


© 2010