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"""
    print("Hello!")
    return 0

if __name__=="__main__":
    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"""
    configparser=ConfigParser.RawConfigParser()
    configparser.readfp(open('./config.cfg'))
    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"""
    userid,authkey=readconfigfile()
    print("Hello!")
    
    print('userid:{0}, authkey:{1}'.format(userid,authkey))
    return 0
also create a simple file config.cfg with simple data:

[user]
userid=12344
authkey=34fewfsad

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.

Comments

Popular posts from this blog

Update grub using dracut

Fixing grub using dracut Last kernel update was not successful to me. Centos can not boot with next messages:  [ 180.098802] dracut-initqueue[376]: Warning: dracut-initqueue timeout - starting timeout scripts [ 180.610167] dracut-initqueue[376]: Warning: dracut-initqueue timeout - starting timeout scripts [ 181.121619] dracut-initqueue[376]: Warning: dracut-initqueue timeout - starting timeout scripts [ 181.633093] dracut-initqueue[376]: Warning: dracut-initqueue timeout - starting timeout scripts [ 182.144831] dracut-initqueue[376]: Warning: dracut-initqueue timeout - starting timeout scripts [ 182.656146] dracut-initqueue[376]: Warning: dracut-initqueue timeout - starting timeout scripts [ 183.167306] dracut-initqueue[376]: Warning: dracut-initqueue timeout - starting timeout scripts [ 183.678755] dracut-initqueue[376]: Warning: dracut-initqueue timeout - starting timeout scripts  Of course simples way  is creating  linux  usb stick  and fix it. But dracut
  debian,  amavis,  virus inside archive   One my client asked informed me, that amavis skips some files types. mail server configuration is really simple: Postfix as SMTP server and  amavis working as context filter. Also amavis runs spamassasin and clamd antivirus. Amavis gets files from attachment and unpack it. lha file is not detected. short investigation First I deceided to run amavis  in debug mode and verify how virus passed postix+amavis.  root@newserver:/var/lib/amavis# /etc/init.d/amavis stop [ ok ] Stopping amavis (via systemctl): amavis.service. root@newserver:/var/lib/amavis# /etc/init.d/amavis debug Trying to run amavisd-new in debug mode. Debug mode inform about loaded plugins: ' Nov 13 22:07:23.335 newserver. /usr/sbin/amavisd-new[40334]: Found decoder for .cpio at /bin/pax Nov 13 22:07:23.336 newserver. /usr/sbin/amavisd-new[40334]: Found decoder for .tar at /bin/pax Nov 13 22:07:23.336 newserver. /usr/sbin/amavisd-new[40334]

Postfix can not start via systemd (simple fix)

Solving problem related to systemd process I like postfix.   This is really smart and secure mail server. I'm helping above  dozen clients around the world and  tunning  postfix is really fun task. This morning I was downgrading postfix  to the stable version for one of the my friends and come across interesting issue.  root@newserver:/etc/init.d# systemctl status postfix ● postfix.service Loaded: masked (/dev/null; bad) Active: inactive (dead) since вт 2017-06-13 14:35:41 EEST; 1h 48min ago Main PID: 25145 (code=exited, status=0/SUCCESS) чер 13 14:47:09 newserver systemd[1]: Stopped postfix.service. чер 13 14:47:29 newserver systemd[1]: Stopped postfix.service. чер 13 14:58:22 newserver systemd[1]: Stopped postfix.service. чер 13 14:58:23 newserver systemd[1]: Stopped postfix.service. чер 13 15:05:20 newserver systemd[1]: Stopped postfix.service. чер 13 15:29:06 newserver systemd[1]: Stopped postfix.service. чер 13 15:29:06 newserver systemd[1]: Stopp