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

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

Mac, emacs and new python3.10

Mac, emacs and new python3.10   I've deceided to upgrade my python to the new Python 3.10 and upgrade my IDE based on Emacs too.  Upgrading python to version 3.10  A installing python 3.10  is really simple task using a homebrew:  MacBook-Pro-Hohlov:call-me skhohlov$ brew search python ==> Formulae app-engine-python gst-python python-launcher python-tk@3.9 python@3.8 pythran boost-python ipython python-markdown python-yq python@3.9 ✔ jython boost-python3 micropython python-tabulate python@3.10 reorder-python-imports cython bpython ptpython python-tk@3.10 python@3.7 wxpython ==> Casks awips-python awips-python
  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]