analitics

Pages

Friday, June 30, 2017

Blender 3D - bpy and scripting - part 001.

The tutorial for today is bpy python module used by Blender 3D software for scripting.
The last Application Programming Interface (A.P.I.) for Blender 3D can be found here.
You need to take a look at the link to see the base of this python module.
The example I will start today is a python script that helps me to deal with Blender 3D.
First I put the script named catafest.py into Blender 3D path: C:\Program Files\Blender Foundation\Blender\2.78\python\lib.
The reason is to be load by Blender 3D application.
Let's start with the script:
First, you need to import the Blender 3D python module named bpy.
This allows us to deal with Blender 3D using python and Application Programming Interface (A.P.I.).
The script is an example not a lib script for Blender 3D.
The goal of this script is to show how to deal with python scripting into Blender 3D.
If you want to make a lib for the Blender 3D then you need to read about Python’s standard library - here.
My script just creates materials. To do that I used functions to make materials - mat, set the material to the object - set_mat and the run function to see how is working.
Let's see the python script:
import bpy

def mat(name,df_col,df_sh,df_int,sp_col,sp_sh,sp_int,alp,amb):
    mat = bpy.data.materials.new(name)
    mat.diffuse_color = df_col
    mat.diffuse_shader = df_sh
    mat.diffuse_intensity = df_int
    mat.specular_color = sp_col
    mat.specular_shader = sp_sh
    mat.specular_intensity = sp_int
    mat.alpha = alp
    mat.ambient = amb
    return mat

def set_mat(ob, mat):
    me = ob.data
    me.materials.append(mat)

def run(origin):
    # create two materials
    red = mat('Red', (1,0,0),'LAMBERT',1.0,(1,1,1),'COOKTORR',0.5, 1,1)
    blue = mat('Green', (0,1,0),'LAMBERT',1.0,(0,0.5,0.5),'COOKTORR',0.5, 1, 0.5)

    # create red cube
    bpy.ops.mesh.primitive_cube_add(location=origin)
    set_mat(bpy.context.object, red)
    # create a green torus
    bpy.ops.mesh.primitive_torus_add(location=origin)
    bpy.ops.transform.translate(value=(1,0,0))
    set_mat(bpy.context.object, blue)

if __name__ == "__main__":
    run((0,0,0))
To run this script put the script into the lib path of Blender 3D software.
Open the Blender 3D and go to the scripting area:

Now about the source code of this script.
As you know the Blender 3D interface let you add and change objects and materials.
Each material comes with a name, colors with a diffuse and specular property, alpha color, ambient color, etc. see the documentation.
Some values come like strings and can get it from Blender 3D interface, see:
diffuse_shader 

and the
specular_shader
.
The set_mat function just load one object using: me = ob.data
Then put the material to object using append function.
The run function is used to show the result with how to make materials, create objects with this materials.
I run into Blender 3D - Console area:
YTHON INTERACTIVE CONSOLE 3.5.2 (default, Dec  1 2016, 20:58:16) [MSC v.1800 64 bit (AMD64)]

Command History:     Up/Down Arrow
Cursor:              Left/Right Home/End
Remove:              Backspace/Delete
Execute:             Enter
Autocomplete:        Ctrl-Space
Zoom:                Ctrl +/-, Ctrl-Wheel
Builtin Modules:     bpy, bpy.data, bpy.ops, bpy.props, bpy.types, bpy.context, bpy.utils, bgl, blf, mathutils
Convenience Imports: from mathutils import *; from math import *
Convenience Variables: C = bpy.context, D = bpy.data

>>> import catafest 
>>> dir(catafest)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__',
 'bpy', 'mat', 'run', 'set_mat']

>>> catafest.run((0,0,0))
This is the result of the script:





Wednesday, June 28, 2017

The Google API Client Library python module.

This python module named Google API Client Library for Python is a client library for accessing the Plus, Moderator, and many other Google APIs, according to the official link.
C:\Python27\Scripts>pip install --upgrade google-api-python-client
Collecting google-api-python-client
  Downloading google_api_python_client-1.6.2-py2.py3-none-any.whl (52kB)
    100% |################################| 61kB 426kB/s
...
Successfully installed google-api-python-client-1.6.2 ...
The example I used is this:
from oauth2client.client import flow_from_clientsecrets
import httplib2
import apiclient
from apiclient.discovery import build
from oauth2client.file import Storage
import webbrowser

def get_credentials():
    scope = 'https://www.googleapis.com/auth/blogger'
    flow = flow_from_clientsecrets(
        'client_id.json', scope,
        redirect_uri='urn:ietf:wg:oauth:2.0:oob')
    storage = Storage('credentials.dat')
    credentials = storage.get()

    if  not credentials or credentials.invalid:
        auth_uri = flow.step1_get_authorize_url()
        webbrowser.open(auth_uri)
        auth_code = raw_input('Enter the auth code: ')
        credentials = flow.step2_exchange(auth_code)
        storage.put(credentials)
    return credentials

def get_service():
    """Returns an authorised blogger api service."""
    credentials = get_credentials()
    http = httplib2.Http()
    http = credentials.authorize(http)
    service = apiclient.discovery.build('blogger', 'v3', http=http)
    return service

if __name__ == '__main__':
    served = get_service()
    print dir(served.blogs)
    users = served.users()

    # Retrieve this user's profile information
    thisuser = users.get(userId='self').execute()
    print('This user\'s display name is: %s' % thisuser['displayName'].encode('utf-8'))

    blogs = served.blogs()

    # Retrieve the list of Blogs this user has write privileges on
    thisusersblogs = blogs.listByUser(userId='self').execute()
    for blog in thisusersblogs['items']:
        print('The blog named \'%s\' is at: %s' % (blog['name'], blog['url']))
The result of this script is this:
C:\Python27>python.exe google_001.py
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__func__', 
'__get__', '__getattribute__', '__hash__', '__init__', '__is_resource__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', 
'__str__', '__subclasshook__', 'im_class', 'im_func', 'im_self']
This user's display name is: Cătălin George Feștilă
The blog named 'python-catalin' is at: http://python-catalin.blogspot.com/
The blog named 'graphics' is at: http://graphic-3d.blogspot.com/
The blog named 'About me and my life ...' is at: http://catalin-festila.blogspot.com/
The blog named 'pygame-catalin' is at: http://pygame-catalin.blogspot.com/
About google settings then you need to have a google account to use Google’s API.
The first step for accessing the Google Developer’s Console.
Then navigate to the Developer Console’s projects page and create a new project for our application by clicking the Create project button and then enable blogger API.
Enter your projects name and hit create.
Click the Go to Credentials button with these settings like in the next image:

Download this credential information in JSON format, in this case, is the client_id.json file.
When you run for the first time this script you will see an open HTML page with your auth code.
The script example named google_001.py will come with this message:
C:\Python27>python.exe google_001.py
C:\Python27\lib\site-packages\oauth2client\_helpers.py:255: UserWarning: Cannot access credentials.dat: No such file or directory
  warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
Enter the auth code:
Put this auth code and allow the script using the open page and your google account using Allow button.
Now you can run the example.


The pyquery python module.

This tutorial is about pyquery python module and python 2.7.13 version.
First I used pip command to install it.
C:\Python27>cd Scripts

C:\Python27\Scripts>pip install pyquery
Collecting pyquery
  Downloading pyquery-1.2.17-py2.py3-none-any.whl
Requirement already satisfied: lxml>=2.1 in c:\python27\lib\site-packages (from pyquery)
Requirement already satisfied: cssselect>0.7.9 in c:\python27\lib\site-packages (from pyquery)
Installing collected packages: pyquery
Successfully installed pyquery-1.2.17
I try to install with pip and python 3.4 version but I got errors.
The development team tells us about this python module:
pyquery allows you to make jquery queries on xml documents. The API is as much as possible the similar to jquery. pyquery uses lxml for fast xml and html manipulation.
Let's try a simple example of this python module.
The base of this example is found links by HTML tag.
from pyquery import PyQuery
 
seeds = [
    'https://twitter.com',
    'http://google.com'
]
 
crawl_frontiers = []
 
def start_crawler():
    crawl_frontiers = crawler_seeds()
 
    print(crawl_frontiers)
 
def crawler_seeds():
    frontiers = []
    for index, seed in enumerate(seeds):
        frontier = {index: read_links(seed)}
        frontiers.append(frontier)
 
    return frontiers
 
def read_links(seed):
    crawler = PyQuery(seed)
    return [crawler(tag_a).attr("href") for tag_a in crawler("a")]
 
start_crawler()
The read_links function takes links from seeds array.
To do that, I need to read the links and put in into another array crawl_frontiers.
The frontiers array is used just for crawler process.
Also, this simple example allows you to understand better the arrays.
You can read more about this python module here.

Saturday, June 17, 2017

Translate with goslate python module .

This python module comes with many features and this is the main reason I make this tutorial.
We can read about this python module here.
Google has updated its translation service recently with a ticket mechanism to prevent simple crawler program like goslate from accessing.
Though a more sophisticated crawler may still work technically, however it would have crossed the fine line between using the service and breaking the service. goslate will not be updated to break google’s ticket mechanism. Free lunch is over. Thanks for using.


Let's install this python module with python 2.7 version and pip:

C:\Python27>cd Scripts

C:\Python27\Scripts>pip install goslate
Collecting goslate
  Downloading goslate-1.5.1.tar.gz
Requirement already satisfied: futures in c:\python27\lib\site-packages (from goslate)
Installing collected packages: goslate
  Running setup.py install for goslate ... done
Successfully installed goslate-1.5.1
Let's test a simple example from English to Romanian:
C:\Python27>python.exe
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import goslate
>>> gs = goslate.Goslate()
>>> print(gs.translate('I\'m not here','ro'))
Eu nu sunt aici
Using detail dictionary explanation for a single word/phrase:
>>> gs.lookup_dictionary('internet', 'ro')
[[[u'Internet', u'internet', None, None, 2]], [[u'noun', [u'Internet'], [[u'Internet', 
[u'Internet'], None, 0.43686765]], u'Internet', 1]], u'en', None, None, None, 0.73151749,
 None, [[u'en'], None, [0.73151749], [u'en']]]
In my opinion, I have no idea what they might use, perhaps in chat applications, specific translations, and text detection.

Friday, June 16, 2017

Python Qt4 - part 002.

This tutorial covers only part of the practice of using G.U.I. (graphical user interface) elements in PyQt4.
First of all, I will start with the theory and then I will simply exemplify how these work.
There are three basic elements called: Event, Signal, and Slot.
Since all GUI applications are driven by events, we will have several elements interconnected with signals and slots.
What do we need to know?
Events are generated mainly by the user of an application into the event processing system.
The event processing system in PyQt4 is built with the signal and slot mechanism.
The event processing system is an event model with three participants:
  • event source 
  • event object 
  • event target 
Signals and slots are used for communication between objects.
A signal is emitted when something of potential interest happens.
If a signal is connected to a slot then the slot is called when the signal is emitted.
Rules of signals and slots:
  • A signal may be connected to many slots.
  • A signal may also be connected to another signal.
  • Signal arguments may be any Python type.
  • A slot may be connected to many signals.
  • Connections may be direct (ie. synchronous) or queued (ie. asynchronous).
  • Connections may be made across threads.
  • Signals may be disconnected.
A signal (specifically an unbound signal) is an attribute of a class that is a subclass of QObject.
Signals are connected to slots using the connect() method of a bound signal.
Signals are disconnected from slots using the disconnect() method of a bound signal.
Signals are emitted from using the emit() method of a bound signal.
Example of a signal used into the myclassapp PyQt4 application:
I create a new signal called closeApp.
closeApp = QtCore.pyqtSignal()
This signal is emitted during a mouse press event.
def mousePressEvent(self, event):
    self.myclassapp.closeApp.emit()
The signal is connected to the close() slot of the QtGui.QMainWindow.
self.myclassapp.closeApp.connect(self.close)
I did not show the entire example here because the reason was to show the direct connection between the signal, the event and the slot.
The events are functions or methods are executed in response to user’s actions like clicking on a button, selecting an item from a collection or a mouse click etc.
Another simple example with o application with two buttons:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

def window():
   app = QApplication(sys.argv)
   win = QDialog()
   mybutton1= QPushButton(win)
   mybutton1.setText("Button1")
   mybutton1.move(50,20)
   mybutton1.clicked.connect(mybutton1_clicked)

   mybutton2= QPushButton(win)
   mybutton2.setText("Button2")
   mybutton2.move(50,50)
   QObject.connect(mybutton2,SIGNAL("clicked()"),mybutton2_clicked)

   win.setGeometry(100,100,200,100)
   win.setWindowTitle("PyQt Event Signal Slot")
   win.show()
   sys.exit(app.exec_())

def mybutton1_clicked():
   print "Button 1 clicked"

def mybutton2_clicked():
   print "Button 2 clicked"

if __name__ == '__main__':
   window()
The result of clicking on these buttons is something like that:
Button 2 clicked
Button 2 clicked
Button 1 clicked
Button 1 clicked
Button 1 clicked
Button 2 clicked
Button 1 clicked
Button 2 clicked
All widgets used to build the G.U.I. (graphical user interface) act as the source of such events, see the mybutton1 source code part.
Now about this part of the source code, I just used to exemplify how the signals are connected to the slots:
QObject.connect(mybutton2,SIGNAL("clicked()"),mybutton2_clicked)
So each PyQt widget (which is derived from QObject class) is designed to emit a signal in response to one or more events.
The signal on its own does not perform any action. Instead, it is connected to a slot. The slot can be any callable Python function.
And this part of the source code is exemplified with mybutton2.
Signals are complex due to their use (how they are used).
More theory about the signals.
To send a signal across threads we have to use the Qt.QueuedConnection parameter.
There is also a special form of a PyQt4 signal known as a short-circuit signal.
The short-circuit signals implicitly declare each argument as being of type PyQt_PyObject.
Short-circuit signals do not have a list of arguments or the surrounding parentheses.
Short-circuit signals may only be connected to slots that have been implemented in Python.
They cannot be connected to Qt slots or the Python callables that wrap Qt slots.
The older style of connecting signals and slots will continue to be supported throughout the life of PyQt4.

Saturday, June 10, 2017

Python Qt4 - part 001.

Today I started with PyQt4 and python version :
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
To install PyQt4 I used this link to take the executable named: PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x32.exe.
The name of this executable shows us: can be used with python 2.7.x versions and come with Qt4.8.7 for our 32-bit python.
I start with a default Example class to make a calculator interface with PyQt4.
This is my example:
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui

"""
Qt.Gui calculator example
"""

class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):
 title = QtGui.QLabel('Title')
        titleEdit = QtGui.QLineEdit()
        grid = QtGui.QGridLayout()
 grid.setSpacing(10)

 grid.addWidget(title, 0, 0)

 grid.addWidget(titleEdit,0,1,1,4)

        self.setLayout(grid)
 
        names = ['Cls', 'Bck', 'OFF',
                 '/', '.', '7', '8',
                '9', '*', 'SQR', '3',
                 '4', '5', '-', '=',
                '0', '1', '2', '+']
        
        positions = [(i,j) for i in range(1,5) for j in range(0,5)]
        
        for position, name in zip(positions, names):
            
            if name == '':
                continue
            button = QtGui.QPushButton(name)
            grid.addWidget(button, *position)
            
        self.move(300, 250)
        self.setWindowTitle('Calculator')
        self.show()
        
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
The example is simple.
First, you need a QGridLayout - this makes a matrix.
I used labels, line edit and buttons all from QtGui: QLabel, QLineEdit and QPushButton.
First into this matrix - named grid is Title and edit area named titleEdit.
This two is added to the grid - matrix with addWidget.
The next step is to put all the buttons into one array.
This array will be added to the grid matrix with a for a loop.
To make this add from array to matrix I used the zip function.
The zip function makes an iterator that aggregates elements from each of the iterable.
Also, I set the title to Calculator with setWindowTitle.
I have not implemented the part of the events and the calculation.
The main function will start the interface by using the QApplication.
The goal of this tutorial was the realization of the graphical interface with PyQt4.
This is the result of my example:

Sunday, June 4, 2017

The SpeechRecognition python module - part 001.

First, you need to install the SpeechRecognition python module for Windows 10:
C:\Python27>cd Scripts
C:\Python27\Scripts>pip install --upgrade  --trusted-host  pypi.python.org  SpeechRecognition
Collecting SpeechRecognition
  Downloading SpeechRecognition-3.6.5-py2.py3-none-any.whl (31.8MB)
    100% |################################| 31.8MB 4.9MB/s
Installing collected packages: SpeechRecognition
  Found existing installation: SpeechRecognition 3.5.0
    Uninstalling SpeechRecognition-3.5.0:
      Successfully uninstalled SpeechRecognition-3.5.0
Successfully installed SpeechRecognition-3.6.5
The next step is the PyAudio python module:
C:\Python27\Scripts>pip install --upgrade  --trusted-host  pypi.python.org  PyAudio
Collecting PyAudio
  Downloading PyAudio-0.2.11-cp27-cp27m-win32.whl (49kB)
    100% |################################| 51kB 258kB/s
Installing collected packages: PyAudio
  Found existing installation: PyAudio 0.2.9
    Uninstalling PyAudio-0.2.9:
      Successfully uninstalled PyAudio-0.2.9
Successfully installed PyAudio-0.2.11
Also, this python module can be installed under python version 3.4.1:
C:\Python34\Scripts>pip install SpeechRecognition
Downloading/unpacking SpeechRecognition
Installing collected packages: SpeechRecognition
Successfully installed SpeechRecognition
Cleaning up...
The problem with Python 3.4.x version is PyAudio python module installation.
Anyway, I used the python 2.7.13 version to test this module with a simple python script:
import speech_recognition as sr
import os
print ("HELP: Set your microphone hardware on and try this script")
def active_listen():
    r = sr.Recognizer()
    with sr.Microphone() as src:
        audio = r.listen(src)
    msg = ''
    try:
        msg = r.recognize_google(audio)
        print (msg.lower())
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google STT; {0}".format(e))
    except:
        print("Unknown exception occurred!")
    finally:
        return msg.lower()
active_listen()
Just start your microphone hardware on and run the script.
Working well for me this test.

The development with python-instagram .

The python-instagram python module is a Python 2/3 client for the Instagram REST and Search APIs.
This python module requires httplib2, simplejson and six.
Instagram API uses the OAuth2 protocol for authentication, see docs.
C:\Python27\Scripts>pip install --upgrade  --trusted-host  pypi.python.org  
python-instagram
Collecting python-instagram
  Downloading python-instagram-1.3.2.tar.gz
Collecting simplejson (from python-instagram)
  Downloading simplejson-3.10.0-cp27-cp27m-win32.whl (66kB)
    100% |################################| 71kB 1.1MB/s
Requirement already up-to-date: httplib2 in c:\python27\lib\site-packages 
(from python-instagram)
Requirement already up-to-date: six in c:\python27\lib\site-packages 
(from python-instagram)
Building wheels for collected packages: python-instagram
  Running setup.py bdist_wheel for python-instagram ... done
 ...
Installing collected packages: simplejson, python-instagram
Successfully installed python-instagram-1.3.2 simplejson-3.10.0
Now about this python module:
C:\Python27>python.exe
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from instagram.client import InstagramAPI
>>> dir(InstagramAPI)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__',
 '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', 
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_make_relationship_shortcut', 
'_make_subscription_action', 'access_token_field', 'access_token_url', 'api_name', 
'approve_user_request', 'authorize_url', 'base_path', 'block_user', 'change_user_relationship',
 'create_media_comment', 'create_subscription', 'delete_comment', 'delete_subscriptions',
 'exchange_code_for_access_token', 'exchange_user_id_for_access_token', 
'exchange_xauth_login_for_access_token', 'follow_user', 'geography_recent_media', 
'get_authorize_login_url', 'get_authorize_url', 'host', 'ignore_user_request', 'like_media',
 'list_subscriptions', 'location', 'location_recent_media', 'location_search', 'media', 
'media_comments', 'media_likes', 'media_popular', 'media_search', 'media_shortcode', 'protocol',
 'redirect_uri', 'tag', 'tag_recent_media', 'tag_search', 'unblock_user', 'unfollow_user', 
'unlike_media', 'user', 'user_followed_by', 'user_follows', 'user_incoming_requests', 
'user_liked_media', 'user_media_feed', 'user_recent_media', 'user_relationship', 'user_search',
 'x_ratelimit', 'x_ratelimit_remaining']
If you have an Instagram account then just log in into instagram developer website.
Then fill the issue about your website the phone number and what do you want to build for your application check your agreement with Instagram.
Now you need to use Register Your Application and finally on Register a New Client.
About Register Your Application you need to fill them with data for your application ( basic info: Description, Company Name, Website URL, Contact email).
Select the tab Security and disable the Disable implicit OAuth.

About the token authorizations:

Is given to you with this words:

basic – to read a user’s profile info and media

or needs additional permission:

public_content – to read any public profile info and media on a user’s behalf
follower_list – to read the list of followers and followed-by users
comments – to post and delete comments on a user’s behalf
relationships – to follow and unfollow accounts on a user’s behalf
likes – to like and unlike media on a user’s behalf
The next step is to get access token then you need to add http://localhost link into Security tag from Manage Client.
Use this URL to get the access token by pasting it into your web browser.
https://instagram.com/oauth/authorize/?client_id=[CLIENT_ID_HERE]&redirect_uri=http://localhost&response_type=token&scope=public_content
Into the browser, you will see one page with one button for Authorizing access.
Press this button and into your browser address bar you will get the access token like:
http://localhost/#access_token=################
A simple python script to test it.
from time import sleep
from instagram.client import InstagramAPI

client_id="zzzzz"
client_secret="sssssssssssss"
redirect_uri= "http://xxxxx"
access_token="eeeee"

api = InstagramAPI(client_id=client_id, client_secret=client_secret)
print dir(api)
print api.api_name
To deal with python and Instagram is not very easy for me.
The main reason comes from errors and the Instagram API development way.
Some simple tasks are very hard to do.

Saturday, May 27, 2017

Using Python for .NET the clr python module - part 001 .

Python for .NET is available as a source release and as a Windows installer for various versions of Python and the common language runtime from the Python for the .NET website.
Let's install it under Windows 10.
C:\Python27\Scripts>pip install pythonnet
Collecting pythonnet
  Downloading pythonnet-2.3.0-cp27-cp27m-win32.whl (58kB)
    100% |################################| 61kB 740kB/s
Installing collected packages: pythonnet
Successfully installed pythonnet-2.3.0
Now I will show you how to use form and buttons.
First, you need to run the python code into python script files.
The first example is simple:
import clr

clr.AddReference("System.Windows.Forms")

from System.Windows.Forms import Application, Form

class IForm(Form):

    def __init__(self):
        self.Text = 'Simple'
        self.Width = 640
        self.Height = 480
        self.CenterToScreen()

Application.Run(IForm())
The next example comes with one button and tooltips for form and button:
import clr

clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Windows.Forms import Application, Form
from System.Windows.Forms import Button, ToolTip
from System.Drawing import Point, Size

class IForm(Form):

    def __init__(self):
        self.Text = 'Tooltips'
        self.CenterToScreen()
        self.Size = Size(640, 480)

        tooltip = ToolTip()
        tooltip.SetToolTip(self, "This is a Form")

        button = Button()
        button.Parent = self
        button.Text = "Button"
        button.Location = Point(50, 70)

        tooltip.SetToolTip(button, "This is a Button")


Application.Run(IForm())
This is the result of this python script.

Another example is how to see the interfaces that are part of a .NET assembly:
>>> import System.Collections
>>> interfaces = [entry for entry in dir(System.Collections)
... if entry.startswith('I')]
>>> for entry in interfaces:
...   print entry
...
ICollection
IComparer
IDictionary
IDictionaryEnumerator
IEnumerable
IEnumerator
IEqualityComparer
IHashCodeProvider
IList
IStructuralComparable
IStructuralEquatable

Friday, May 26, 2017

OpenGL and OpenCV with python 2.7 - part 005.

In this tutorial, I will show you how to mount OpenCV in the Windows 10 operating system with any python version.
You can use the same steps for other versions of python.
Get the wheel binary package opencv_python-3.2.0.7-cp27-cp27m-win32.whl from here.
C:\Python27>

C:\Python27>cd Scripts

C:\Python27\Scripts>pip install opencv_python-3.2.0.7-cp27-cp27m-win32.whl
Processing c:\python27\scripts\opencv_python-3.2.0.7-cp27-cp27m-win32.whl
Requirement already satisfied: numpy>=1.11.1 in c:\python27\lib\site-packages (from opencv-python==3.2.0.7)
Installing collected packages: opencv-python
Successfully installed opencv-python-3.2.0.7

C:\Python27\Scripts>python
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Let's test it with default source code:

>>> import cv2
>>> dir(cv2)
['', 'ACCESS_FAST', 'ACCESS_MASK', 'ACCESS_READ', 'ACCESS_RW', 'ACCESS_WRITE', 
'ADAPTIVE_THRESH_GAUSSIAN_C', 'ADAPTIVE_THRESH_MEAN_C', 'AGAST_FEATURE_DETECTOR_AGAST_5_8', 
'AGAST_FEATURE_DETECTOR_AGAST_7_12D', 'AGAST_FEATURE_DETECTOR_AGAST_7_12S',
 'AGAST_FEATURE_DETECTOR_NONMAX_SUPPRESSION', 'AGAST_FEATURE_DETECTOR_OAST_9_16',
...
Now we can test this python script example with PyQt4 python module and cv2.resize function very easy.
The example loads an image with PyQt4 python module.
from PyQt4.QtGui import QApplication, QWidget, QVBoxLayout, QImage, QPixmap, QLabel, QPushButton, QFileDialog
import cv2
import sys
app = QApplication([])
window = QWidget()
layout = QVBoxLayout(window)
window.setLayout(layout)
display = QLabel()
width = 600
height = 400
display.setMinimumSize(width, height)
layout.addWidget(display)
button = QPushButton('Load', window)
layout.addWidget(button)

def read_image():
    path = QFileDialog.getOpenFileName(window)
    if path:
        print str(path)
        picture = cv2.imread(str(path))
        if picture is not None:
            print width, height
            picture = cv2.resize(picture, (width, height))
            image = QImage(picture.tobytes(),  # The content of the image
                           picture.shape[1],  # The width (number of columns)
                           picture.shape[0],  # The height (number of rows)
                           QImage.Format_RGB888)  # The image is stored in 3*8-bit format
            display.setPixmap(QPixmap.fromImage(image.rgbSwapped()))
        else:
            display.setPixmap(QPixmap())

button.clicked.connect(read_image)
window.show()

app.exec_()
See the result for this python script:

Monday, May 22, 2017

Make one executable from a python script.

The official website of this tool tells us:
PyInstaller bundles a Python application and all its dependencies into a single package. The user can run the packaged app without installing a Python interpreter or any modules. PyInstaller supports Python 2.7 and Python 3.3+, and correctly bundles the major Python packages such as numpy, PyQt, Django, wxPython, and others.

PyInstaller is tested against Windows, Mac OS X, and Linux. However, it is not a cross-compiler: to make a Windows app you run PyInstaller in Windows; to make a Linux app you run it in Linux, etc. PyInstaller has been used successfully with AIX, Solaris, and FreeBSD, but is not tested against them.

The manual of this tool can see it here.
C:\Python27>cd Scripts

C:\Python27\Scripts>pip install pyinstaller
Collecting pyinstaller
  Downloading PyInstaller-3.2.1.tar.bz2 (2.4MB)
    100% |################################| 2.4MB 453kB/s
....
Collecting pypiwin32 (from pyinstaller)
  Downloading pypiwin32-219-cp27-none-win32.whl (6.7MB)
    100% |################################| 6.7MB 175kB/s
...
Successfully installed pyinstaller-3.2.1 pypiwin32-219
Also, this will install the PyWin32 python module.
Let's make one test python script and then to make it executable.
I used this python script to test it:
from tkinter import Tk, Label, Button

class MyFirstGUI:
    def __init__(self, master):
        self.master = master
        master.title("A simple GUI")

        self.label = Label(master, text="This is our first GUI!")
        self.label.pack()

        self.greet_button = Button(master, text="Greet", command=self.greet)
        self.greet_button.pack()

        self.close_button = Button(master, text="Close", command=master.quit)
        self.close_button.pack()

    def greet(self):
        print("Greetings!")

root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()
The output of the command of pyinstaller:
C:\Python27\Scripts>pyinstaller.exe   --onefile --windowed ..\tk_app.py
92 INFO: PyInstaller: 3.2.1
92 INFO: Python: 2.7.13
93 INFO: Platform: Windows-10-10.0.14393
93 INFO: wrote C:\Python27\Scripts\tk_app.spec
95 INFO: UPX is not available.
96 INFO: Extending PYTHONPATH with paths
['C:\\Python27', 'C:\\Python27\\Scripts']
96 INFO: checking Analysis
135 INFO: checking PYZ
151 INFO: checking PKG
151 INFO: Building because toc changed
151 INFO: Building PKG (CArchive) out00-PKG.pkg
213 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 9247)
2120 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
2251 INFO: Bootloader c:\python27\lib\site-packages\PyInstaller\bootloader\Windows-32bit\runw.exe
2251 INFO: checking EXE
2251 INFO: Rebuilding out00-EXE.toc because tk_app.exe missing
2251 INFO: Building EXE from out00-EXE.toc
2267 INFO: Appending archive to EXE C:\Python27\Scripts\dist\tk_app.exe
2267 INFO: Building EXE from out00-EXE.toc completed successfully.
Then I run the executable output:
C:\Python27\Scripts>C:\Python27\Scripts\dist\tk_app.exe

C:\Python27\Scripts>
...and working well.

The output file come with this icon:

Also, you can make changes by using your icons or set the type of this file, according to VS_FIXEDFILEINFO structure.
You need to have the icon file and/or version.txt file for VS_FIXEDFILEINFO structure.
Let's see the version.txt file:
# UTF-8
#
# For more details about fixed file info 'ffi' see:
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
VSVersionInfo(
  ffi=FixedFileInfo(
    # filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
    # Set not needed items to zero 0.
    filevers=(2017, 1, 1, 1),
    prodvers=(1, 1, 1, 1),
    # Contains a bitmask that specifies the valid bits 'flags'
    mask=0x3f,
    # Contains a bitmask that specifies the Boolean attributes of the file.
    flags=0x0,
    # The operating system for which this file was designed.
    # 0x4 - NT and there is no need to change it.
    OS=0x4,
    # The general type of file.
    # 0x1 - the file is an application.
    fileType=0x1,
    # The function of the file.
    # 0x0 - the function is not defined for this fileType
    subtype=0x0,
    # Creation date and time stamp.
    date=(0, 0)
    ),
  kids=[
    StringFileInfo(
      [
      StringTable(
        u'040904b0',
        [StringStruct(u'CompanyName', u'python-catalin'),
        StringStruct(u'ProductName', u'test'),
        StringStruct(u'ProductVersion', u'1, 1, 1, 1'),
        StringStruct(u'InternalName', u'tk_app'),
        StringStruct(u'OriginalFilename', u'tk_app.exe'),
        StringStruct(u'FileVersion', u'2017, 1, 1, 1'),
        StringStruct(u'FileDescription', u'test tk'),
        StringStruct(u'LegalCopyright', u'Copyright 2017 free-tutorials.org.'),
        StringStruct(u'LegalTrademarks', u'tk_app is a registered trademark of catafest.'),])
      ]),
    VarFileInfo([VarStruct(u'Translation', [0x409, 1200])])
  ]
)
Now you can use this command for tk_app.py and version.txt files from the C:\Python27 folder:
 pyinstaller.exe --onefile --windowed --version-file=..\version.txt ..\tk_app.py
Let's see this info into the executable file:

If you want to change the icon then you need to add the --icon=tk_app.ico, where tk_app.ico is the new icon of the executable.



Updating all Python with pip on Windows OS.

Just use this python module named pip-review.
C:\Python27\Scripts>pip install pip-review
C:\Python27\Scripts>pip-review.exe --auto --verbose
Checking for updates of ...
...
pip-review.exe --auto --verbose
Everything up-to-date

The pycrypto python module - part 001.

This python module name pycrypto is a collection of Python Cryptography Toolkit.
This python module has been created by Andrew Kuchling and now maintained by Dwayne C. Litzenberger.
Let's install under Windows 10 OS using Command Prompt (Admin) shell.
C:\WINDOWS\system32>cd ..

C:\Windows>cd ..

C:\>cd Python27\Scripts

C:\Python27\Scripts>pip install pycrypto
Requirement already satisfied: pycrypto in c:\python27\lib\site-packages
Some info and help under python shell can be seen using this:
C:\Python27>python.exe
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Crypto
>>> dir(Crypto)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 
'__revision__', '__version__', 'version_info']
>>> help(Crypto)
Help on package Crypto:

NAME
    Crypto - Python Cryptography Toolkit

FILE
    c:\python27\lib\site-packages\crypto\__init__.py

DESCRIPTION
    A collection of cryptographic modules implementing various algorithms
    and protocols.

    Subpackages:

    Crypto.Cipher
     Secret-key (AES, DES, ARC4) and public-key encryption (RSA PKCS#1) algorithms    Crypto.Hash
     Hashing algorithms (MD5, SHA, HMAC)
    Crypto.Protocol
     Cryptographic protocols (Chaffing, all-or-nothing transform, key derivation
     functions). This package does not contain any network protocols.
    Crypto.PublicKey
     Public-key encryption and signature algorithms (RSA, DSA)
    Crypto.Signature
     Public-key signature algorithms (RSA PKCS#1)
    Crypto.Util
     Various useful modules and functions (long-to-string conversion, random number
     generation, number theoretic functions)

PACKAGE CONTENTS
    Cipher (package)
    Hash (package)
    Protocol (package)
    PublicKey (package)
    Random (package)
    SelfTest (package)
    Signature (package)
    Util (package)
    pct_warnings

DATA
    __all__ = ['Cipher', 'Hash', 'Protocol', 'PublicKey', 'Util', 'Signatu...
    __revision__ = '$Id$'
    __version__ = '2.6.1'

VERSION
    2.6.1
Let's test some examples with this python module.
The first example comes with encrypting and decrypt message based one key.
The key also needs to be one encryption key and fix to key32.
The iv will not be specified by the user, it will be generated and then encrypted with RSA.
NEVER make the IV constant and unique, it must be unique for every message.
Let's see the example source code:
from Crypto.Cipher import AES
from Crypto import Random
def encrypt(key32,message):
    cipher=AES.new(key32,AES.MODE_CFB,iv)
    msg=cipher.encrypt(message)
    print(msg)
    return msg
def decrypt(key32,msg):
    dec=AES.new(key32,AES.MODE_CFB,iv)
    return dec.decrypt(msg).decode('ascii')
if __name__=='__main__':
    global iv
    iv=Random.new().read(AES.block_size)
    key='free-tutorials.org'
    key32 = "".join([ ' ' if i >= len(key) else key[i] for i in range(32) ])
    message='another website with free tutorials'
    enc =encrypt(key32, message)
    print enc
    print(decrypt(key32,enc))
The resulting output is this:
ᄚ Cᆪ゚2 ᄊÕ|ýXÍ ᄇNäÇ3ヨ゙Lマᆱuï: ù メNᄚm
ᄚ Cᆪ゚2 ᄊÕ|ýXÍ ᄇNäÇ3ヨ゙Lマᆱuï: ù メNᄚm
another website with free tutorials

Another more simplistic example:
from Crypto.Cipher import AES
from Crypto import Random
key = b'Sixteen byte key'
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
msg = iv + cipher.encrypt(b'Attack at dawn')
See the output of variables:
>>> print key
Sixteen byte key
>>> print iv
ÔÄ▀DÒ ÕØ} m║dÕ╚\
>>> print cipher.encrypt(b'Attack at dawn')
åÌ£┴\u\ÍÈSÕ╦╔.
Using MD5 example:
>>> from Crypto.Hash import MD5
>>> MD5.new('free text').hexdigest()
'be9420c1596a781119c53a9933a8234f'
Using RSA key example:
>>> from Crypto.PublicKey import RSA
>>> from Crypto import Random
>>> rng = Random.new().read
>>> RSAkey = RSA.generate(1024, rng)
>>> public_key = RSAkey.publickey()
>>> print public_key
<_rsaobj e="" n="" x3650b98="">
>>> enc_data = public_key.encrypt('test data', 32)[0]
>>> print enc_data
H +îÕÊ ÙH:?ª2S½Fã0á! f¬ = ·+,Í0r³┐o·¼ÉlWy¿6ôên(£jê¿ ╦çª|*°q Ò4ì┌çÏD¦¿╝û╠╠MY¶ïzµ>©a}hRô ]í;
_[v¸¤u:2¦y¾/ ²4R╩HvéÌ'÷Ç)KT:P _<! D
>>> dec_data = RSAkey.decrypt(enc_data)
>>> print dec_data
test data 
Encrypted and decrypted output texts may look different depending on how encoded the used text editor or python language.