Skip to main content

Starting python programming

Programming is easy. Really easy.

Many years ago programming requires good skills in hardware and software. "How computers works" was really important question 30 years ago. Cracking Enigma computer or first computer at a nuclear submarine were more simplest the modern cell phone.  This is a reason why building mathematical solution requires perfect engineering skill.
Computer is more powerful now and  tarting programming does not requires long term eduction. For example: driving car does not requires deep knowing in  engines and physics, you are driver not mechanics.

Downloading software. 

First  download  python.python 
Also text editor  or powerful IDE is needed. I like Emacs but a lot of peoples love pycharm. You can download it  from PyCharm. Python is really popular and a lot of system have it included. I'm planning  to use some additional libraries  for our application. It is good idea do not touch your original software. Correct  way is virtual environment usage. 
 Linux people can take a look at : Virtual environment for Linux and IMac
Windows  people  should take a look at : Virtual environment for Windows

First our python  program

Using your favorite editor  type next code: 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Small  script for plarium game.
This application has education purposes only.

def main():
    """main function for this one"""
    return 0

if __name__=="__main__":

First string  informs system that python will be used. 
Second one  is setting code page for our script.  My system  uses utf-8.  Next one  is python doc string. This can help other guys to understand your code.  Of course,   you can move out  this string. 
Starting  from def  to the end it is default python code  structure.   Also you can delete  all code except string print("Hello!")  and result is a same.   I've added some additional string for making good programmer's writing skill.

Working with files

Of course, writing 'hello'  is really simple and everyone can do this.  More interested is working with files. Writing simple  files  is simple but interesting from performance point of view as I wrote early List in python performance.  I think it is not a bad idea to start from writing and reading configurations files. One of a simplest modules for this task is ConfigParser in python 3.0  it has been renamed to configparser. 
add please to our script after doc string next code: 
import os, ConfigParser

def readconfigfile():
    """ Readinf importamt values from config files"""
    userid=configparser.get('user', 'userid')
    authkey=configparser.get('user', 'authkey')
    return userid, authkey
 and update main function to  the next one:

def main():
    """main function for this one"""
    print('userid:{0}, authkey:{1}'.format(userid,authkey))
    return 0
also create a simple file config.cfg with simple data:


Of course, you can set those values using our investigation from firefox development tools: getting UserID and authkey Run it and our script print values from our config file. Also we can use those data for future programming.


Popular posts from this blog

Upgrade postgres 9.4 to 9.6 at Debian linux

I'm using virtual server based on Debian Linux for some my free projects.  One of them is based on Postgres with PostGIS extension.   This windy rainy day is a good time for executing some upgrade. Also I would like to add partman extension for partituoning.
Checking new version. Update package list :
aptitude update Get: 1 jessie-updates InRelease [145 kB] Get: 2 stretch/updates InRelease [62.9 kB] Get: 3 jessie/updates InRelease [63.1 kB]
 Check for new version:

dpkg-query -l postgresql* Desired=Unknown/Install/Remove/Purge/Hold| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)||/ Name Version Ar…

Small script for one of the plarium game

few notes about google games.
Goggle informed that access to the all games be closed after June, 30. I played "Pirates: Tides of fortune" and decided to limit my game time using small script. Detail is below.
Some history
First internet games were updated network games only. 15 years ago internet connection requires a lot of money and nobody had a problem with lagging of the other players. Usually games uses personal communication protocol. Warbirds, Aces High and Eve online use this way.
Next part use a browser and trivial HTTP protocol. One of the popular game in this area is travian . Travian player uses browser and every browser (PC, cell phone, tablet) can be used for playing. Of course, popularity of the game is related to graphics. Trivial HTTP does not have good power in this and other technology is used for this side. One of them is Flash . Unfortunately flash requires a lot of CPU : "Pirates" was near to froze at my old celeron with 512M…

Small python script for monitoring MySQL performance

I have few services which use MySQL as database server. I would like to have information about load in PNG  image  or  in Cacti app.
MySQL   has  performance information at 'SHOW STATUS' command.

Values  which  are monitored : 
 threads_running, threads_connected, thread_cached, slow_queries
 Of course,  it is really easy to add more variables.

Connection to MySQL is accomplished by MySQLdb  module. Typical example of usage is below :
import MySQLdb mydb = MySQLdb.connect(host = 'hostname', user = 'username', password = 'secret', database = 'mysatabase' ) mycursor = mydb.cursor() mycursor.execute('SQL command') sqlresult = cur.fetchall()
Storing data in rrd file is aviable via rrdtools package. This one is present in debian and Centos OS. example of creating file is below: import rrdtool rrdtool.create("myfile.rrd" , "DS:value1:datatype:hea…