Log File Parsing In Python

In this tutorial you will learn how to create log file parser in python. Parsing a log file or any type of text file in order to extract specific information is not that hard if you know a bit of python and regex. Python itself is perfect for this kind of use and does not require any third party modules. Also, if you are new to python world, I wrote a quick python setup on windows tutorial in which you can look into if you want to quickly setup python environment.

In my day job, I was working on testing Skype for Business iOS application and it came to the point where I had to go through iOS application’s log files in order to see all the requests and received responses. I had to look for specific properties like:

<code><property name="saveMessagingHistory">Enabled</property></code>

Usually they were buried under bunch of other not so important text. While I was looking for specific things in those log files, I realized that it’s going to be really time consuming to go through log files manually. In order to save time I wrote the following python script. If you want to test this script with similar text file, you can download SfB iOS log file here.

Continue reading “Log File Parsing In Python”

Compile Standalone exe in Python

In this tutorial you will learn how to compile standalone exe in python with pyinstaller module. This is great for PyGame and PySide, but works for any python script. The best thing about pyinstaller is that creates one file and you don’t have to have options script file to compile something. Everything can be done through console with single command line.

There are couple of ways you can do this, but as far as I found, things like py2exe, cx_Freeze and others require specific options script file. It just took me too much time, all I wanted to do was simply compile python script to standalone executable without any dependencies and pyinstaller does exactly that.

Continue reading “Compile Standalone exe in Python”

How to setup python on Windows

Apparently a lot of people are struggling with simple setup of python environment on Windows. When I first started coding with python, the environment setup took me longer than it should. I’m going to give simple fast instructions with easiest ways to setup python on Windows.

Install canopy

If you quickly need to setup python and write some code, I would suggest to go with Canopy. It’s great for scientific computing, data analysis and engineering. It’s free and includes 200+ most popular python packages.

Simply download the installer click next, next.. and you are half way done. When installation and environment setup is completed, select Editor from Welcome to Canopy window. Create a new file, write some code and run it by clicking on green triangle at the top menu or use Ctrl+R

Install python packages in canopy

To install python package in canopy, simply start canopy and from welcome menu select Package Manager. Select Available tab from the left side and type selenium in search field at the top. That should display selenium package. Select selenium package and click on blue install button. Now you can import selenium package in your scripts if you are using canopy.

Continue reading “How to setup python on Windows”

Find and replace strings with python

python-big

The following code was designed for situation where you have a folder with about 50 subfolders and a script file in each subfolder. With a bit of changes code can be adjusted to similar situations and effectively used to save time. If you haven’t got your python environment setup on windows already, here’s a tutorial on how to quickly setup python.

import fileinput
import re
from tempfile import mkstemp
from shutil import move
from os import remove, close
import os

def main():
    folders = getFolderNames(r'C:\main_folder');

    for folder in xrange(0, len(folders)):
        files = getFileNames(r'C:\main_folder\%s' %folders[folder], '.py');

        for file in xrange(0, len(files)):
            findAndReplace(r'C:\main_folder\%s' %folders[folder], files[file]);

if __name__ == '__main__':
    main()

The code won’t work without getFolderNames, getFileNames, replace and findAndReplace functions which are explained below. In order for this script to work properly add necessary functions after def main() function or download find_and_replace.py from pythonicways GitHub page.

Continue reading “Find and replace strings with python”