Pages

Monday, March 14, 2016

Custom Implementation of Python Basic Concepts

Iterator

class custom_iterator():
    
    def __init__(self, start, end, interval=None):
        self.start = start
        self.end = end
        self.interval = interval
    
    def __iter__(self):
        return self
    
    def next(self):
        if self.start < self.end:
            if self.interval:
                self.start += self.interval
                return self.start - self.interval
            else:
                self.start += 1
                return self.start - 1
        else:
            raise StopIteration
            
for i in custom_iterator(1, 10):
    print i

"Iterator means to interate value one by one from the sequence. In custom iterator we need to create class which shoud be iterated by __iter__ function so that to implement iter functionality. Here, we can pass two argument with default interval, if we want custom interval also then we will pass three arguments in custom iterator like custom_iterator(1, 10, 2)"

Generator

def custom_generator(start, end, interval=None):
    while start < end:
        yield start
        if interval:
            start += interval
        else:
            start += 1
    else:
        raise StopIteration
        
for j in custom_generator(1, 10, 2):
    print j
    
"Don't forget yield statment whenever we will talk about generator, it used to store currrent status of the function and continue with previous state, this type of function also called co-routing function".
    

Decorator

def first_decorator(func):
    
    def wrapper_function(*args, **kwargs):
       # here can code for extra functionality
        return func(*args, **kwargs)
    
    return wrapper_function

@first_decorator
def add_val(x, y):
    return x + y

print "val", add_val(10, 20)

"Decorator means pass the function as arguments in other function, why we need to do this type of implementation because object oriented concept is write once read many times, to proof this concept without any changes in previos function we will add new funtionality in newly created function".

Exception

class CustomError(Exception):
    def __init__(self, arg):
        # Set some exception infomation
        self.msg = arg

try:
    # Raise an exception with argument
    raise CustomError('This is a CustomError')
except CustomError, arg:
    # Catch the custom exception
    print 'Error: ', arg.msg 

"Custom Eception, where user will create own exception class and based on the condition will raise error message".

Enjoy ... :)

Monday, February 9, 2015

Python Package into stand-alone application by Pyinstaller and Inno Setup

Pyinstaller

Pyinstaller is a program that converts (packages) Python programs into stand-alone executables, under Windows, Linux, Mac OS X, and Solaris and so on.

Download: The latest stable version is https://github.com/pyinstaller/pyinstaller/wiki and run script with

>> python setup.py install

We can install using pip command also:

>> pip pyinstaller install

More details about pyinstaller visit http://pythonhosted.org/PyInstaller/#installing-pyinstaller

Let the script is monitor.py

Desktop\SmartTimeMonitor\
                                   monitor.py
                                   monitor.config
Then,

>> pyinstaller monitor.py

Some folders dist and build will create in SmartTimeMonitor:



Here we will look monitor.exe in the dist\monitor folder. When we will go to create setup file using inno setup, then normal setup will create using exe by pyinstaller.



If we have to need some changes, like at the time of run setup, don’t want to show console window then need to use options with pyinstaller command:

>> pyinstaller monitor.py –w

Where w is used to create exe which will not show the pop up window but script will run in background.
...........................................................................................................................................

Inno Setup

                                       Inno setup is free installer for windows programs.

Download: The stable version http://www.jrsoftware.org/isdl.php and install it.
Create new file in Inno setup compiler and use these statements.

; Script generated by the Inno Setup Script Wizard.
 ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
 ; then did a few changes to make maintenance, move to other folder easier
  ; define the path to your work folder         C:\Users\anupk\Desktop\Monitor
 #define BaseFolder "C:\Users\anupk\Desktop\Monitor\dist\monitor"
 ; get version information from the exe
 #define ExeName BaseFolder+"\monitor.exe"
 #define AppVersionNo GetFileVersion(ExeName)
 #define AppMajorVersionIdx Pos(".", AppVersionNo)
 #define AppMinorVersionTemp Copy(AppVersionNo, AppMajorVersionIdx +1)
 #define AppMajorVersionNo Copy(AppVersionNo, 1, AppMajorVersionIdx -1)
 #define AppMinorVersionNo Copy(AppMinorVersionTemp, 1, Pos(".", AppMinorVersionTemp)-1)
  
 ; define some more stuff, mainly to just keep it all at the beginning
 #define MyAppName "SmartTime"
 #define MyAppPublisher "Smartek Consultancy Services"
 #define MyAppURL "http://www.smartek21.com/"
 #define MyAppSupportURL "http://www.smartek21.com/support"
 #define MyAppUpdatesURL "http://www.smartek21.com/downloads"
  #define MyAppExeName "monitor.exe"
  #define OutputFileName "MonitorSetup"
      
 [Setup]
 ; NOTE: The value of AppId uniquely identifies this application.
 ; Do not use the same AppId value in installers for other applications.
 ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
 AppId={{451B698D-E97F-444E-9FA9-B7180E450563}
 AppName={#MyAppName}
 AppVersion={#AppVersionNo}
 AppVerName={#MyAppName} version {#AppMajorVersionNo}.{#AppMinorVersionNo}
 AppPublisher={#MyAppPublisher}
 AppPublisherURL={#MyAppURL}
 AppSupportURL={#MyAppSupportURL}
 AppUpdatesURL={#MyAppUpdatesURL}
 ; Following should probably be something like "{pf}\yourappname" for a real application
; set DefaultDirName to store files after setup done
 DefaultDirName=C:\Program Files\MonitorTime
 DefaultGroupName=Smart Time
 AllowNoIcons=yes
 PrivilegesRequired=admin
 DisableStartupPrompt=yes
 DisableWelcomePage=yes
 ; AlwaysRestart=yes
  
 OutputBaseFilename={#OutputFileName}_{#AppVersionNo}
  
 ; bzip/9 is better by about 400KB over zip/9 and lzma is even better
 Compression=lzma/ultra
 ; Following would reduce size a bit more
 ; SolidCompression=yes
  
 [Languages]
 Name: english; MessagesFile: compiler:Default.isl
  
 [Tasks]
; To create Desktop Icon
 Name: desktopicon; Description: {cm:CreateDesktopIcon}; GroupDescription: {cm:AdditionalIcons}; Flags: unchecked
; To create Quicklaunchicon
 Name: quicklaunchicon; Description: {cm:CreateQuickLaunchIcon}; GroupDescription: {cm:AdditionalIcons}; Flags: unchecked
  
 [Files]
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\monitor.exe; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\include\*; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\monitor.config; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\base_library.zip; DestDir: {app}; Flags: ignoreversion recursesubdirs createallsubdirs
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\_bz2.pyd; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\_ctypes.pyd; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\_hashlib.pyd; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\_lzma.pyd; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\_socket.pyd; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\_ssl.pyd; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\_win32sysloader.pyd; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\mfc100u.dll; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\MSVCR100.dll; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\pyexpat.pyd; DestDir: {app}; Flags: ignoreversion
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\MSVCR100.dll; DestDir: {app};
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\python34.dll; DestDir: {app};
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\pythoncom34.dll; DestDir: {app};
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\pywintypes34.dll; DestDir: {app};
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\select.pyd; DestDir: {app};
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\unicodedata.pyd; DestDir: {app};
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\win32api.pyd; DestDir: {app};
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\win32gui.pyd; DestDir: {app};
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\win32process.pyd; DestDir: {app};
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\win32service.pyd; DestDir: {app};
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\win32trace.pyd; DestDir: {app};
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\win32ui.pyd; DestDir: {app};
 Source: C:\Users\anupk\Desktop\Monitor\dist\monitor\win32wnet.pyd; DestDir: {app};

 ; Source: C:\Users\anupk\Desktop\Py_script\dist\monitor\pythoncom34.dll; DestDir: {app}; Flags: ignoreversion
 ; Source: C:\Users\anupk\Desktop\Py_script\dist\monitor\pywintypes34.dll; DestDir: {app}; Flags: ignoreversion
 ; Source: ..\dist\w9xpopen.exe; DestDir: {app}; Flags: ignoreversion
 ; Source: C:\Users\anupk\Desktop\Py_script\dist\monitor\base_library\*; DestDir: {app}\lib; Flags: ignoreversion recursesubdirs createallsubdirs
 ; Source: C:\Users\anupk\Desktop\Py_script\dist\monitor\include\*; DestDir: {app}\include; Flags: ignoreversion recursesubdirs createallsubdirs
 ; NOTE: Don't use "Flags: ignoreversion" on any shared system files
  
 [Icons]
 Name: {group}\{#MyAppName}; Filename: {app}\{#MyAppExeName}
 Name: {commondesktop}\{#MyAppName}; Filename: {app}\{#MyAppExeName}; Tasks: desktopicon;
 ;create uninstall shortcut in start menu
 ;Name: {group} \ {cm: UninstallProgram, {#MyAppName}}; Filename: {uninstallexe}     
 Name: {userappdata}\Microsoft\Internet Explorer\Quick Launch\{#MyAppName}; Filename: {app}\{#MyAppExeName}; Tasks: quicklaunchicon
 Name: {commonstartup}\{#MyAppName}; Filename: {app}\{#MyAppExeName};

 ;[Registry]
 ;Root: HKLM; Subkey: "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; ValueType: string; ValueName: {#MyAppName}; ValueData: {app}\{#MyAppExeName}; Flags: uninsdeletevalue
 ;Root: HKLM; Subkey: "Software\My Company\My Program"; Flags: uninsdeletekey
  
 [Run]
; Here runhidden flag is used to hide pop up window after setup done
 Filename: {app}\{#MyAppExeName}; Description: {cm:LaunchProgram,{#MyAppName}}; Flags: nowait postinstall skipifsilent runhidden
 Filename: {sys}\net.exe; Parameters: start {#MyAppName}; Flags: runhidden;


For more details about inno setup visit http://www.jrsoftware.org/ishelp/index.php?topic=wizardpages
Setup file will store in desktop: output/MonitorSetup
And installed file: C:\Program Files\MonitorTime\


Thanks :)

Sunday, August 11, 2013

OTRS Installation in Ubuntu 12.04

Installation:


Ubuntu already has a package for OTRS in the repositories. Before start installation OTRS we have to install MySql, Apache.

1)  Install MySQL 5

sudo apt-get install mysql-server mysql-client


Password for MySQL root user:

New password for the MySQL "root" user:  <-- yourrootsqlpassword
Repeat password for the MySQL "root" user:  <-- yourrootsqlpassword


* To check whether the MySQL server is running:
sudo netstat -tap | grep mysql


* When you run this command, you should see the following line or something similar:

tcp        0      0 localhost:mysql         *:*                LISTEN      2556/mysqld


* If the server is not running correctly, you can type the following command to start it:
sudo service mysql restart


2) Install Apache2
* Apache2 is available as an Ubuntu package, therefore we can install it like this:
sudo apt-get install apache2

Now direct your browser to http://192.168.0.100, and you should see the Apache2 placeholder page (It works!):


After completion I and II installation, we will move to OTRS Installation:

3) Installing OTRS:

* Basic ubuntu 12.04 LTS, fully updated:



sudo aptitude update 

sudo aptitude dist-upgrade

@ Source


* Get a copy of a recent version of OTRS from the OTRS site (http://www.otrs.com/en/open-source/get-otrs/software-download/)

wget http://ftp.otrs.org/pub/otrs/otrs-3.2.9.tar.gz


* Extract the archive


tar -xzf otrs-3.2.5.tar.gz

* Move the extracted directory to its permanent location


sudo mv otrs-3.2.5 /opt/otrs


@ Users and Permissions


* OTRS needs to run as its own user. So we need to create some folders and files:
sudo useradd -r -d /opt/otrs/ -c 'OTRS user' otrs
sudo usermod -g www-data otrs
cd /opt/otrs/Kernel
cp Config.pm.dist Config.pm
cp Config/GenericAgent.pm.dist Config/GenericAgent.pm

* Now we need to set permission

cd /opt/otrs/bin
sudo ./otrs.SetPermissions.pl --otrs-user=otrs --otrs-group=otrs --web-user=www-data --web-group=www-data /opt/otrs


@ Perl Modules

cd /opt/otrs/bin
sudo ./otrs.CheckModules.pl

* The following command should fix the failed Perl modules.


sudo aptitude install liblwp-useragent-determined-perl libapache2-reload-perl libnet-smtp-ssl-perl libnet-smtp-tls-butmaintained-perl


* And Some extra modules that should prove to be useful (i.e. PDF functionality, LDAP connectivity, etc.).


sudo aptitude install libgd-gd2-perl libgd-graph-perl libgd-text-perl libjson-xs-perl libnet-dns-perl libyaml-libyaml-perl libpdf-api2-simple-perl libtext-csv-xs-perl libxml-parser-perl libmail-imapclient-perl libnet-ldap-perl


@ Configuration

* Apache2


cp /opt/otrs/scripts/apache2-httpd.include.conf /etc/apache2/conf.d/otrs.conf 
service apache2 restart


sudo cp -va /opt/otrs/scripts/apache2-httpd.include.conf /etc/apache2/sites-available/otrs
cd /etc/apache2/sites-available/

sudo chown root:root otrs
sudo a2ensite otrs
sudo service apache2 reload


@ Finalization


* Go to http://127.0.0.1/otrs/installer.pl with your web browser.


All the best...!