analitics

Pages

Monday, June 17, 2019

Python Qt5 : the most simple QTreeWidget - part 002.

This tutorial uses PyQt5 and Python version 3.7.3.
Let's install the PyQt5 python module with the pip tool:
C:\Python373\Scripts>pip install PyQt5
Collecting PyQt5
...
Successfully installed PyQt5-5.12.2 PyQt5-sip-4.19.17
Let's see one simple example with comments about how to use QTreeWidget.
import sys
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QApplication, QWidget

if __name__ == '__main__':
    # create a empty my_app application
    my_app = ''
    # test this my_app to create instance
    if QApplication.instance() != None:
        my_app = QApplication.instance()
    else:
        my_app = QApplication(sys.argv)
    # create a QTreeWidgetItem with tree columns
    my_tree= QTreeWidgetItem(["Column A", "Column B", "Column C"])
    # add date using a for loop 
    for i in range(6):
        list_item_row = QTreeWidgetItem(["Child A-" + str(i), "Child B-" + str(i), "Child C-" + str(i)])
        my_tree.addChild(list_item_row)
    # create my_widget widget
    my_widget = QWidget()
    my_widget.resize(640, 180)
    # create a QTreeWidget named my_tree_widget 
    my_tree_widget = QTreeWidget(my_widget)
    # set the size
    my_tree_widget.resize(640, 180)
    # set the number of columns 
    my_tree_widget.setColumnCount(3)
    # add labels for each column 
    my_tree_widget.setHeaderLabels(["Column A label", "Column B label", "Column C label"])
    # add my_tree using addTopLevelItem
    my_tree_widget.addTopLevelItem(my_tree)
    # show the widget
    my_widget.show()
    # the exit of my_app
    sys.exit(my_app.exec_())
This is another simple example written in a simple way to show how versatile are Python and PyQt5.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTreeWidget, QTreeWidgetItem
 
my_app = QApplication(sys.argv)
my_window = QWidget()
my_layout = QVBoxLayout(my_window)
 
my_tree = QTreeWidget()
my_tree.setHeaderLabels(['Name', 'Cost ($)'])
my_item_root = QTreeWidgetItem(my_tree, ['Romania', '238,397 kmp'])
my_item_raw = QTreeWidgetItem(my_item_root, ['Black Sea', '436,402 kmp'])
 
my_layout.addWidget(my_tree)
my_window.show()
sys.exit(my_app.exec_())
If you like my simple tutorials then you subscribe or you can search my other web sites too.

Sunday, June 16, 2019

Python 3.7.3 : Using the pycryptodome python module.

This python module can be used with python 3.
More information can be found here.
PyCryptodome is a self-contained Python package of low-level cryptographic primitives.
It supports Python 2.6 and 2.7, Python 3.4 and newer, and PyPy.

The install of this python module is easy with pip tool:
C:\Python373\Scripts>pip install pycryptodome
Collecting pycryptodome
...
Installing collected packages: pycryptodome
Successfully installed pycryptodome-3.8.2
All packages from this python module are:
  • Cipher;
  • Signature;
  • Hash;
  • Publickey;
  • Protocol;
  • IO;
  • Random and Util;
Let start with a few examples:
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Inte
l)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from Crypto.Random import get_random_bytes
>>> key16 = get_random_bytes(16) # 16 bytes * 8 = 128 bits (1 byte = 8 bits)
The python module comes with features for encryption and dencryption, like RSA, AES, DES and many options.
Another good feature is KDF with PBKDF2 function:
>>> from Crypto.Protocol.KDF import PBKDF2
>>> center = b'go'
>>> password = 'go'
>>> output = PBKDF2(password, center, dkLen=64)
>>> print(output)
b'E\x0b\x91\x87\xde\xb2E\xe5Gv\x03\x86fVe8\x1e%\xb5l\xa0\xdb\xbfI\x01\xb5\xdf\x8
1\xad\x82@\x00\xacr\xc7\xa26\xc6\xe92\x1e\xf8\xe9\x0b\x9e\x93\x1dj\x1c\xff\x1c4\
xc2\x0e6\xc2\x8eYc2N\x995\x87'
>>>
If you are a fan of encryption and decryption math, then this module will enable you to use it.
By studying this module you can give a more in-depth view of what exists in this field and what does not exist.

Saturday, June 8, 2019

Python 3.7.3 : Testing the PyX python module.

This python module has a good documenation with many examples, see the eofficial wepage.
The development team come with this intro:
Summary
PyX is a Python package for the creation of PostScript, PDF, and SVG files. It combines an abstraction of the PostScript drawing model with a TeX/LaTeX interface. Complex tasks like 2d and 3d plots in publication-ready quality are built out of these primitives.
Features
  • PostScript, PDF, and SVG output for device independent, freely scalable figures;
  • seamless TeX/LaTeX integration;
  • full access to PostScript features like paths, linestyles, fill patterns, transformations, clipping, bitmap inclusion, etc.;
  • advanced geometric operations on paths like intersections, transformations, splitting, smoothing, etc.;
  • sophisticated graph generation: modular design, pluggable axes, axes partitioning based on rational number arithmetics, flexible graph styles, etc.
Let's install this python module with pip tool:
C:\Python373\Scripts>pip install PyX
...
Installing collected packages: PyX
Successfully installed PyX-0.14.1
I try few examples from official webpage and working well.
>>> from pyx import *
>>>
>>> c = canvas.canvas()
>>> c.stroke(path.line(0, 0, 3, 0))
>>> c.stroke(path.rect(0, 1, 1, 1))
>>> c.fill(path.circle(2.5, 1.5, 0.5))
>>> c.writeEPSfile("path")
>>> c.writePDFfile("path")
>>> c.writeSVGfile("path")
>>> from pyx import *
>>>
>>> c = canvas.canvas()
>>> c.stroke(path.curve(0, 0, 0, 4, 2, 4, 3, 3),
...          [style.linewidth.THICK, style.linestyle.dashed, color.rgb.blue,
...           deco.earrow([deco.stroked([color.rgb.red, style.linejoin.round]),
...                        deco.filled([color.rgb.green])], size=1)])
>>> c.writeEPSfile("arrow")
>>> c.writePDFfile("arrow")
>>> c.writeSVGfile("arrow")
I got some error from writePDFfile with this example:
>>> c.writeEPSfile("textalongpath")
>>> c.writePDFfile("textalongpath")
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python373\lib\site-packages\pyx\canvas.py", line 50, in wrappedindocu
ment
    return method(d, file, **write_kwargs)
  File "C:\Python373\lib\site-packages\pyx\document.py", line 193, in writePDFfi
le
    pdfwriter.PDFwriter(self, f, **kwargs)
  File "C:\Python373\lib\site-packages\pyx\pdfwriter.py", line 321, in __init__
    registry.write(file, self, catalog)
  File "C:\Python373\lib\site-packages\pyx\pdfwriter.py", line 78, in write
    object.write(file, writer, self)
  File "C:\Python373\lib\site-packages\pyx\pdfwriter.py", line 248, in write
    file.write("/MediaBox [%f %f %f %f]\n" % self.PDFcontent.bbox.highrestuple_p
t())
  File "C:\Python373\lib\site-packages\pyx\bbox.py", line 112, in highrestuple_p
t
    raise ValueError("Cannot return high-res tuple for empty bbox")
ValueError: Cannot return high-res tuple for empty bbox
>>> c.writeSVGfile("textalongpath")
This python module has problem with some complex example from official webpage.

Monday, June 3, 2019

Python 3.7.3 : Working with wikipedia python module.

Wikipedia is a Python library that makes it easy to access and parse data from Wikipedia.
Let's install it:
C:\Python373\Scripts>pip install wikipedia
First, let's test the default example:
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Inte
l)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wikipedia
>>> print(wikipedia.summary("Wikipedia"))
...
.Wikipedia has been criticized for exhibiting systemic bias, for presenting a m
ixture of "truths, half truths, and some falsehoods", and for being subject to m
anipulation and spin in controversial topics. But by 2017, Facebook announced th
at it would help readers detect fake news by suggesting links to related Wikiped
ia articles. YouTube announced a similar plan in 2018.
>>> wikipedia.search("Falticeni")
['Falticeni', 'Foresta Falticeni', 'Charles, Prince of Wales', 'Sofia Ionescu',
'Buciumeni River (?omuzul Mare)', '?omuzul Mare River', 'Constantin Schumacher',
'Ionu? Atodiresei', '1967 Cupa României Final', 'J. J. Benjamin']
>>> wikipedia.page("Falticeni")
...
>>> city=wikipedia.page("Falticeni")
>>> city.title
...
>>> city.content
...
>>> wikipedia.set_lang("fr")
>>> page=wikipedia.page("Null")
>>> page.title
'Null' 
You can extract links:
>>> page = wikipedia.page("List_of_works_by_Leonardo_da_Vinci")
>>> print(page.links)
You can test all of these:
>>> dir(wikipedia)
['API_URL', 'BeautifulSoup', 'Decimal', 'DisambiguationError', 'HTTPTimeoutError
', 'ODD_ERROR_MESSAGE', 'PageError', 'RATE_LIMIT', 'RATE_LIMIT_LAST_CALL', 'RATE
_LIMIT_MIN_WAIT', 'RedirectError', 'USER_AGENT', 'WikipediaException', 'Wikipedi
aPage', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__na
me__', '__package__', '__path__', '__spec__', '__version__', 'cache', 'datetime'
, 'debug', 'donate', 'exceptions', 'geosearch', 'languages', 'page', 'random', '
re', 'requests', 'search', 'set_lang', 'set_rate_limiting', 'set_user_agent', 's
tdout_encode', 'suggest', 'summary', 'sys', 'time', 'timedelta', 'unicode_litera
ls', 'util', 'wikipedia']
Read more at pypi website.

Thursday, May 30, 2019

Python 3.7.3 : Using the win32com - part 007.

Today I show you a new script I tested a few days ago with win32com and Win32_LogonSession class, see thedocumentation.
The Win32_LogonSession WMI class describes the logon session or sessions associated with a user logged on to a computer system running Windows.
Let's see the script:
import win32com.client
from win32com.client import gencache
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_Session")
for objItem in colItems:
    print("Caption" , objItem.Caption)
    print("Description" , objItem.Description)
    print("InstallDate" , objItem.InstallDate)
    print("Name" , objItem.Name)
    print("Status" , objItem.Status)
    print("StartTime" , objItem.StartTime)
    print("AuthenticationPackage" , objItem.AuthenticationPackage)
    print("LogonId" , objItem.LogonId)
    print("LogonType" , objItem.LogonType)
The result of this python script:
Caption None
Description None
InstallDate None
Name None
Status None
StartTime 20190530105325.022532+180
AuthenticationPackage LiveSSP
LogonId 8291403
LogonType 2
Caption None
Description None
InstallDate None
Name None
Status None
StartTime 20190530105325.022532+180
AuthenticationPackage LiveSSP
LogonId 8291220
LogonType 2
You can read info with this python module.
For example, the output tells me aboutLogonType.
You’ll see type 2 logons when a user attempts to log on at the local keyboard and screen whether with a domain account or a local account from the computer.

Python 3.7.3 : How fast are Data Classes.

This simple tutorial follows the PEP 0557 subject of Data Classes.
About the python classes then you can define into three ways:
  • standard_class;
  • slot_class ( standard_class with __slots__);
  • the new dataclass
Today I will show you how this works and how fast are these classes.
First, let's import some python modules: dataclasses and timeit.
The dataclasses python module lets us use the new dataclass.
This dataclass is a class decorator as defined in PEP 526.
For testing, I define a function to show us the output:
def  _str(n): return f'({n.x})'
This will get value and return it.
I define three classes for each type of these with one value named x and set to 1.0.
Each of these classes can have values and be used:
sc = standard_class(1.0)
sl = slot_class(1.0)
dc = new_dataclass(1.0)
This will set for each class the value 1.0 to x parameter.
The next step will start with the measure execution time of the time object creation for each of these classes.
Let's see the source code:
from dataclasses import dataclass
from timeit import timeit

def  _str(n): return f'({n.x})'

class standard_class:
 def __init__(self, x=0.0):
  self.x = x
 def __str__(self): return _str(self)

class slot_class:
 __slots__ = 'x'
 def __init__(self, x=0):
  self.x =x
 def __str__(self): return _str(self)

@dataclass
class new_dataclass:
 x: float = 0.0
 def __str__(self): return _str(self)

sc = standard_class(1.0)
sl = slot_class(1.0)
dc = new_dataclass(1.0)
print(sc,sl,dc)

time_sc=timeit('standard_class()', setup = 'from __main__ import standard_class')
print(f'standard class: {time_sc:.5f}')
time_sc=timeit('slot_class()', setup = 'from __main__ import slot_class')
print(f'standard class: {time_sc:.5f}')
time_sc=timeit('new_dataclass()', setup = 'from __main__ import new_dataclass')
print(f'standard class: {time_sc:.5f}')
The output is:
C:\Python373>python.exe dataclasses_001.py
(1.0) (1.0) (1.0)
standard class: 0.48912
standard class: 0.41349
standard class: 0.48514
As you can see, the new types of the class definition are not very fast but allow for other advantages and disadvantages.
You can read more about this at PEP 0557.


Monday, May 27, 2019

Python 3.7.3 : Using the win32com - part 006.

Today I will show you how to see the system environment setting:
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_Environment")
for objItem in colItems:
    print("Caption:", objItem.Caption)
    print("Description:", objItem.Description)
    print("Install Date:", objItem.InstallDate)
    print("Name:", objItem.Name)
    print("Status:", objItem.Status)
    print("System Variable:", objItem.SystemVariable)
    print("User Name:", objItem.UserName)
    print("Variable Value:", objItem.VariableValue)
The result is :
...
Install Date: None
Name: PATH
Status: OK
System Variable: False...

Sunday, May 26, 2019

Python 3.7.3 : Using the win32com - part 005.

Another example with win32com python module:
This script show info about the audio or video codec, see this link:
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_CodecFile")
for objItem in colItems:
    print("Access Mask: " , objItem.AccessMask)
    print("Archive: " , objItem.Archive)
    print("Caption: " , objItem.Caption)
    print("Drive: " , objItem.Drive)
    print("Extension: " , objItem.Extension)
    print("File Name: " , objItem.FileName)
    print("File Size: " , objItem.FileSize)
    print("File Type: " , objItem.FileType)
    print("File System Name: " , objItem.FSName)
    print("Group: " , objItem.Group)
    print("Hidden: " , objItem.Hidden)
    print("Manufacturer: " , objItem.Manufacturer)
    print("Name: " , objItem.Name)
    print("Path: " , objItem.Path)
    print("Version: " , objItem.Version)
The output is this:
File Type:  Application Extension
File System Name:  NTFS
Group:  Video
Hidden:  False
Manufacturer:  Microsoft Corporation
Name:  C:\Windows\system32\MSVIDC32.DLL
Path:  \windows\system32\
Version:  6.3.9600.17415
Access Mask:  1179817
Archive:  True
Caption:  c:\windows\system32\l3codeca.acm
Drive:  c:
Extension:  acm
File Name:  l3codeca
File Size:  82432
File Type:  acm File
File System Name:  NTFS
Group:  Audio
Hidden:  False
Manufacturer:  Fraunhofer Institut Integrierte Schaltungen IIS
Name:  C:\Windows\system32\L3CODECA.ACM
Path:  \windows\system32\
Version:  1.9.0.401

Saturday, May 25, 2019

Python 3.7.3 : Using the win32com - part 004.

This is a source code I created a few days ago.
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_ProgIDSpecification")
for objItem in colItems:
    print ("Caption: ", objItem.Caption)
    print ("Check ID: ", objItem.CheckID)
    print ("Check Mode: ", objItem.CheckMode)
    print ("Description: ", objItem.Description)
    print ("Name: ", objItem.Name)
    print ("Parent: ", objItem.Parent)
    print ("ProgID: ", objItem.ProgID)
    print ("Software Element ID: ", objItem.SoftwareElementID)
    print ("Software Element State: ", objItem.SoftwareElementState)
    print ("Target Operating System: ", objItem.TargetOperatingSystem)
    print ("Version: ", objItem.Version)
This script uses the win32com python module to show us info about Win32_ProgIDSpecification.
The result is strange and shows like this:
...
Version:  None
Caption:  Addin Class
...
See the full documentation at this link.

Friday, May 24, 2019

Python 3.7.3 : Using the win32com - part 003.

This is another python script with win32com python module and python version 3.7.3 .
The script shows us info about the drive:
import win32com
from win32com import client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
out_items = objSWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive")
for object_item in out_items:
    if object_item.Availability != None:
        print("Availability:" +  str(object_item.Availability))
    if object_item.BytesPerSector != None:
        print("BytesPerSector:" +  str(object_item.BytesPerSector))
    print("Capabilities:")
    strList = " "
    try :
        for obj_element in object_item.Capabilities :
            strList = strList + 'obj_element' + "," + obj_element
    except:
        strList = strList + 'null'
    print(strList)
    print("CapabilityDescriptions:")
    strList = " "
    try :
        for obj_element in object_item.CapabilityDescriptions :
            strList = strList + 'obj_element' + "," + obj_element
    except:
        strList = strList + 'null'
    print(strList)
    if object_item.Caption != None:
        print("Caption:" +  str(object_item.Caption))
    if object_item.CompressionMethod != None:
        print("CompressionMethod:" + str(object_item.CompressionMethod))
    if object_item.ConfigManagerErrorCode != None:
        print("ConfigManagerErrorCode:" + str(object_item.ConfigManagerErrorCode))
    if object_item.ConfigManagerUserConfig != None:
        print("ConfigManagerUserConfig:" + str(object_item.ConfigManagerUserConfig))
    if object_item.CreationClassName != None:
        print("CreationClassName:" + str(object_item.CreationClassName))
    if object_item.DefaultBlockSize != None:
        print("DefaultBlockSize:" + str(object_item.DefaultBlockSize))
    if object_item.Description != None:
        print("Description:" + str(object_item.Description))
    if object_item.DeviceID != None:
        print("DeviceID:" + str(object_item.DeviceID))
    if object_item.ErrorCleared != None:
        print("ErrorCleared:" + str(object_item.ErrorCleared))
    if object_item.ErrorDescription != None:
        print("ErrorDescription:" + str(object_item.ErrorDescription))
    if object_item.ErrorMethodology != None:
        print("ErrorMethodology:" + str(object_item.ErrorMethodology))
    if object_item.Index != None:
        print("Index:" + str(object_item.Index))
    #object_item.InstallDate - this is not show
    if object_item.InterfaceType != None:
        print("InterfaceType:" + str(object_item.InterfaceType))
    if object_item.LastErrorCode != None:
        print("LastErrorCode:" + str(object_item.LastErrorCode))
    if object_item.Manufacturer != None:
        print("Manufacturer:" + str(object_item.Manufacturer))
    if object_item.MaxBlockSize != None:
        print("MaxBlockSize:" + str(object_item.MaxBlockSize))
    if object_item.MaxMediaSize != None:
        print("MaxMediaSize:" + str(object_item.MaxMediaSize))
    if object_item.MediaLoaded != None:
        print("MediaLoaded:" + str(object_item.MediaLoaded))
    if object_item.MediaType != None:
        print("MediaType:" + str(object_item.MediaType))
    if object_item.MinBlockSize != None:
        print("MinBlockSize:" + str(object_item.MinBlockSize))
    if object_item.Model != None:
        print("Model:" + str(object_item.Model))
    if object_item.Name != None:
        print("Name:" + str(object_item.Name))
    if object_item.NeedsCleaning != None:
        print("NeedsCleaning:" + str(object_item.NeedsCleaning))
    if object_item.NumberOfMediaSupported != None:
        print("NumberOfMediaSupported:" + str(object_item.NumberOfMediaSupported))
    if object_item.Partitions != None:
        print("Partitions:" + str(object_item.Partitions))
    if object_item.PNPDeviceID != None:
        print("PNPDeviceID:" + str(object_item.PNPDeviceID))
    print("PowerManagementCapabilities:")
    strList = " "
    try :
        for obj_element in object_item.PowerManagementCapabilities :
            strList = strList + 'obj_element' + "," + obj_element
    except:
        strList = strList + 'null'
    print(strList)
    if object_item.PowerManagementSupported != None:
        print("PowerManagementSupported:" + str(object_item.PowerManagementSupported))
    if object_item.SCSIBus != None:
        print("SCSIBus:" + str(object_item.SCSIBus))
    if object_item.SCSILogicalUnit != None:
        print("SCSILogicalUnit:" + str(object_item.SCSILogicalUnit))
    if object_item.SCSIPort != None:
        print("SCSIPort:" + str(object_item.SCSIPort))
    if object_item.SCSITargetId != None:
        print("SCSITargetId:" + str(object_item.SCSITargetId))
    if object_item.SectorsPerTrack != None:
        print("SectorsPerTrack:" + str(object_item.SectorsPerTrack))
    if object_item.Signature != None:
        print("Signature:" + str(object_item.Signature))
    if object_item.Size != None:
        print("Size:" + str(object_item.Size))
    if object_item.Status != None:
        print("Status:" + str(object_item.Status))
    if object_item.StatusInfo != None:
        print("StatusInfo:" + str(object_item.StatusInfo))
    if object_item.SystemCreationClassName != None:
        print("SystemCreationClassName:" + str(object_item.SystemCreationClassName))
    if object_item.SystemName != None:
        print("SystemName:" + str(object_item.SystemName))
    if object_item.TotalCylinders != None:
        print("TotalCylinders:" + str(object_item.TotalCylinders))
    if object_item.TotalHeads != None:
        print("TotalHeads:" + str(object_item.TotalHeads))
    if object_item.TotalSectors != None:
        print("TotalSectors:" + str(object_item.TotalSectors))
    if object_item.TotalTracks != None:
        print("TotalTracks:" + str(object_item.TotalTracks))
    if object_item.TracksPerCylinder != None:
        print("TracksPerCylinder:" + str(object_item.TracksPerCylinder))
The result looks something like this:
MediaLoaded:True
...
MediaType:Fixed hard disk media
Model:TOSHIBA MQ01ABF050
Name:\\.\PHYSICALDRIVE0
Partitions:2
PNPDeviceID:SCSI\DISK&VEN_TOSHIBA&PROD_MQ01ABF050\4&AD866E5&0&000000
PowerManagementCapabilities:
 null
SCSIBus:0
SCSILogicalUnit:0
SCSIPort:0
SCSITargetId:0
SectorsPerTrack:63
Signature:-1846003871
Size:500105249280
Status:OK
SystemCreationClassName:Win32_ComputerSystem
SystemName:CATAFEST
TotalCylinders:60801
TotalHeads:255
TotalSectors:976768065
TotalTracks:15504255
TracksPerCylinder:255

Thursday, May 23, 2019

Python 3.7.3 : Using the win32com - part 002.

Today I tested the win32com python module with python 3.7.3.
I'll show you two scripts I tested with this python module.
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_ClassicCOMClass")
for objItem in colItems:
    print("Component ID: " , objItem.ComponentId)
    print("Description: " , objItem.Description)
    print("Name: " , objItem.Name)
The result is something like this:
Name:  Shell Execute Hardware Event Handler
Component ID:  {FFC9F9AE-E87A-3252-8E25-B22423A40 }
Description:  System.ThreadStaticAttribute
Name:  System.ThreadStaticAttribute
Component ID:  {FFCDB781-D71C-4D10-BD5F-0492EAFFD }
Description:  PSFactoryBuffer
Name:  PSFactoryBuffer
Component ID:  {ffd90217-f7c2-4434-9ee1-6f1b530db }
Description:  XML Feed Moniker
Name:  XML Feed Moniker
Component ID:  {ffe1df5f-9f06-46d3-af27-f1fc10d63 }
Description:  HomeGroup CPL Advanced Settings Writer
Name:  HomeGroup CPL Advanced Settings Writer
Component ID:  {FFE2A43C-56B9-4bf5-9A79-CC6D42856 }
Description:  Windows Photo Viewer Image Verbs
Name:  Windows Photo Viewer Image Verbs
The following example contains this source code:
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_ComponentCategory")
for objItem in colItems:
    print("Category ID: " , objItem.CategoryId)
    print("Name: " , objItem.Name)
The result is something like this:
Name:  3D DirectTransform
Category ID:  {F0B7A1A1-9847-11CF-8F20-00805F2CD }
Name:  Active Scripting Engine
Category ID:  {F0B7A1A2-9847-11CF-8F20-00805F2CD }
Name:  Active Scripting Engine with Parsing
Category ID:  {F0B7A1A3-9847-11CF-8F20-00805F2CD }
Name:  Active Scripting Engine with Encoding

Python 3.7.3 : Using the pelican python module and GitHub.

This tutorial follows a similar tutorial from the web.
I tested that tutorial to see if it works.
This tutorial is focused on GitHub but can also be used independently on another python platform.
You need a GitHub Pro, your personal account:
With GitHub Pro, your personal account gets unlimited public and private repositories with unlimited collaborators.

In addition to the features available with GitHub Free, private repositories on GitHub Pro include advanced tools and insights:

Unlimited collaborators
GitHub Pages
Wikis
Protected branches
Code owners
Repository insights graphs: Pulse, contributors, traffic, commits, code frequency, network, and forks

Let's start with pip install tool:
C:\Python373>cd Scripts

C:\Python373\Scripts>pip install pelican ghp-import
...
Installing collected packages: unidecode, blinker, feedgenerator, pelican, ghp-import
Successfully installed blinker-1.4 feedgenerator-1.9 ghp-import-0.5.5 pelican-4.0.1
 unidecode-1.0.23
Now you can create a new repository with your name into the GitHub website.
I create a repository named catafest:
C:\Python373\Scripts>git clone https://github.com/catafest/catafest.git blog
Cloning into 'blog'...
warning: You appear to have cloned an empty repository.

C:\Python373\Scripts>cd blog

C:\Python373\Scripts\blog>git checkout -b content
Switched to a new branch 'content'

C:\Python373\Scripts\blog>pelican-quickstart
Welcome to pelican-quickstart v4.0.1.

This script will help you create a new Pelican-based website.

Please answer the following questions so this script can generate the files
needed by Pelican.


> Where do you want to create your new web site? [.] .
> What will be the title of this web site? catafest
> Who will be the author of this web site? Catalin George Festila
> What will be the default language of this web site? [English]
> Do you want to specify a URL prefix? e.g., https://example.com   (Y/n) n
> Do you want to enable article pagination? (Y/n)
> How many articles per page do you want? [10]
> What is your time zone? [Europe/Paris] Europe/Bucharest
> Do you want to generate a tasks.py/Makefile to automate generation and publish
ing? (Y/n) y
> Do you want to upload your website using FTP? (y/N) n
> Do you want to upload your website using SSH? (y/N) n
> Do you want to upload your website using Dropbox? (y/N) n
> Do you want to upload your website using S3? (y/N) n
> Do you want to upload your website using Rackspace Cloud Files? (y/N) n
> Do you want to upload your website using GitHub Pages? (y/N) y
> Is this your personal page (username.github.io)? (y/N) y
Done. Your new project is available at C:\Python373\Scripts\blog
The content of the blog file:
[.]              [..]             [content]        Makefile
[output]         pelicanconf.py   publishconf.py   tasks.py
               4 File(s)          6,496 bytes
               4 Dir(s)  333,558,878,208 bytes free
The quickstart created five files and one new directory:

  • Makefile: make command convenience tasks for common operations such as running a development server, building a site and cleaning extraneous build files;
  • fabfile.py: A Fabric file that has some of the same types of commands as the Makefile. Fabric is a wonderful code library but for now I recommend skipping the Fabric file because unfortunately Fabric does not yet support Python 3;
  • develop_server.sh: shell script for running the development server;
  • pelicanconf.py: settings file for your Pelican project. If you are used to earlier versions of Pelican this file was instead named settings.py;
  • publishconf.py: another (optional) settings file that can be considered as a "production" settings file when you move past the development phase and want to deploy your site;
  • content: location for your markup files, which should be stored under pages and posts directories

Now we can use the local Git repo, commit the changes, and push the local changes to the remote repo hosted on GitHub:
C:\Python373\Scripts\blog>git add .
warning: LF will be replaced by CRLF in Makefile.
This can be fixed with:
git config core.autocrlf true
Check the first commit:
C:\Python373\Scripts\blog>git commit -m "first commit"

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'catal_000@catafest.(none)')
Add the mail and name to your git settings:
C:\Python373\Scripts\blog>git config --global user.email "catafest@yahoo.com"
C:\Python373\Scripts\blog>git config --global user.name "catafest"
Use the git for the first commit
C:\Python373\Scripts\blog>git commit -m "first commit"
[content (root-commit) 85814f7] first commit
 6 files changed, 230 insertions(+)
 create mode 100644 Makefile
 create mode 100644 README.md
 create mode 100644 git
 create mode 100644 pelicanconf.py
 create mode 100644 publishconf.py
 create mode 100644 tasks.py

C:\Python373\Scripts\blog>git push origin content
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 2 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 2.91 KiB | 994.00 KiB/s, done.
Total 8 (delta 0), reused 0 (delta 0)
To https://github.com/catafest/catafest.git
 * [new branch]      content -> content
Create folders pages and images and create the first post with new file first-post.md.
C:\Python373\Scripts\blog>cd content
C:\Python373\Scripts\blog\content>mkdir pages images
C:\Python373\Scripts\blog\content>notepad first-post.md
Add content to the first post:
title: First Post on My Sweet New Blog
date: 
author: Your Name Here

#I am On My Way To Internet Fame and Fortune!

This is my first post on my new blog. While not super informative it
should convey my sense of excitement and eagerness to engage with you,
the reader!
Copy an image file into folder images.
I used the catafest.jpg image.
Create a new file into pages and named this file about.md.
C:\Python373\Scripts\blog\content>cd pages

C:\Python373\Scripts\blog\content\pages>notepad about.md
The content of about.md file is:
title: About
date: 

![So Schmexy][my_sweet_photo]

Hi, I am  and I wrote this epic collection of Interweb
wisdom. In days of yore, much of this would have been deemed sorcery
and I would probably have been burned at the stake.

😆

[my_sweet_photo]: {filename}/images/catafest.jpg
C:\Python373\Scripts\blog\content>cd pages

C:\Python373\Scripts\blog\content\pages>notepad about.md
Run Pelican to generate the static HTML files in the output:
C:\Python373\Scripts\blog\content\pages>notepad about.md

C:\Python373\Scripts\blog\content\pages>cd ..

C:\Python373\Scripts\blog\content>cd ..

C:\Python373\Scripts\blog>pelican content -o output -s publishconf.py
WARNING: Feeds generated without SITEURL set properly may not be valid
WARNING: Docutils has no localization for 'english'. Using 'en' instead.
WARNING: No valid files found in content for the active readers:
  | BaseReader (static)
  | HTMLReader (htm, html)
  | RstReader (rst)
Done: Processed 0 articles, 0 drafts, 0 pages, 0 hidden pages and 0 draft pages
in 0.23 seconds.

C:\Python373\Scripts\blog>ghp-import -m "Generate Pelican site" --no-jekyll -b m
aster output

C:\Python373\Scripts\blog>git push origin master
Enumerating objects: 49, done.
Counting objects: 100% (49/49), done.
Delta compression using up to 2 threads
Compressing objects: 100% (45/45), done.
Writing objects: 100% (49/49), 149.15 KiB | 2.98 MiB/s, done.
Total 49 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), done.
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/catafest/catafest/pull/new/master
remote:
To https://github.com/catafest/catafest.git
 * [new branch]      master -> master

C:\Python373\Scripts\blog>git add content

C:\Python373\Scripts\blog>git commit -m "added a first post, a photo and an abou
t page"
[content 4b036fd] added a first post, a photo and an about page
3 files changed, 21 insertions(+)
create mode 100644 content/first-post.md
create mode 100644 content/images/catafest.jpg
create mode 100644 content/pages/about.md

C:\Python373\Scripts\blog>git push origin content
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 2 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 20.28 KiB | 5.07 MiB/s, done.
Total 8 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/catafest/catafest.git
85814f7..4b036fd  content -> content
Open your browser and enter:
https://username.github.io
If you want to test it without Github, then use this:
C:\Python373\Scripts\blog>pelican -s pelicanconf.py -o output content
WARNING: Docutils has no localization for 'english'. Using 'en' instead.
WARNING: No valid files found in content for the active readers:
  | BaseReader (static)
  | HTMLReader (htm, html)
  | RstReader (rst)
Done: Processed 0 articles, 0 drafts, 0 pages, 0 hidden pages and 0 draft pages
in 0.19 seconds.

C:\Python373\Scripts\blog>cd output

C:\Python373\Scripts\blog\output>python -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
Open your browser with the http://localhost:8000 URL.
You need to understand the difference between these two commands and how this make changes into output:
C:\Python373\Scripts\blog>pelican -s pelicanconf.py -o output content
WARNING: Docutils has no localization for 'english'. Using 'en' instead.
WARNING: No valid files found in content for the active readers:
  | BaseReader (static)
  | HTMLReader (htm, html)
  | RstReader (rst)
Done: Processed 0 articles, 0 drafts, 0 pages, 0 hidden pages and 0 draft pages
in 0.19 seconds.

C:\Python373\Scripts\blog>pelican content -o output -s publishconf.py
WARNING: Feeds generated without SITEURL set properly may not be valid
WARNING: Docutils has no localization for 'english'. Using 'en' instead.
WARNING: No valid files found in content for the active readers:
  | BaseReader (static)
  | HTMLReader (htm, html)
  | RstReader (rst)
Done: Processed 0 articles, 0 drafts, 0 pages, 0 hidden pages and 0 draft pages
in 0.23 seconds.
You can create themes and used into similar way like django with this command:
pelican -s pelicanconf.py -o output -t theme content
I do not want to extend this tutorial, but I can tell you that this python module is very versatile.

Wednesday, May 22, 2019

Python 3.7.3 : Using the win32com - part 001.

The tutorial is about win32com python module using the python 3.7.3.
First exameple is simple:
import sys
import win32com.client
from win32com.client import constants

speaker = win32com.client.Dispatch("SAPI.SpVoice")
print ("Type word or phrase, then enter.")
while 1:
      text_to_say = input("> ")
      speaker.Speak(text_to_say)
Let's run it.
The Microsoft Speech SDK to speak what you type in from the keyboard.
C:\Python373>python.exe speak_001.py
Type word or phrase, then enter.
> this is a test
The next example is more complex and uses speech recognition to create outputs.
You can change this example to execute tasks.
# This is a sample code for speech recognition using the MS Speech API

from win32com.client import constants
import win32com.client
import pythoncom
 
""" 
You will see a voice say: Started successfully
The you need to start the speech recognition tool.
After running this, then saying "One", "Two", "Three" or "Four" should
display "You said One" etc on the console.
"""
 
class SpeechRecognition:
    """ Initialize the speech recognition with the passed in list of words """
    def __init__(self, wordsToAdd):

        # For text-to-speech
        self.speaker = win32com.client.Dispatch("SAPI.SpVoice")

        # For speech recognition - first create a listener
        self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")

        # Then a recognition context
        self.context = self.listener.CreateRecoContext()

        # which has an associated grammar
        self.grammar = self.context.CreateGrammar()

        # Do not allow free word recognition - only command and control
        # recognizing the words in the grammar only
        self.grammar.DictationSetState(0)

        # Create a new rule for the grammar, that is top level (so it begins
        # a recognition) and dynamic (ie we can change it at runtime)
        self.wordsRule = self.grammar.Rules.Add("wordsRule",
                        constants.SRATopLevel + constants.SRADynamic, 0)

        # Clear the rule (not necessary first time, but if we're changing it
        # dynamically then it's useful)
        self.wordsRule.Clear()

        # And go through the list of words, adding each to the rule
        [ self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd ]

        # Set the wordsRule to be active
        self.grammar.Rules.Commit()
        self.grammar.CmdSetRuleState("wordsRule", 1)

        # Commit the changes to the grammar
        self.grammar.Rules.Commit()

        # And add an event handler that's called back when recognition occurs
        self.eventHandler = ContextEvents(self.context)

        # Announce we've started
        self.say("Started successfully")
    """Speak a word or phrase"""
    def say(self, phrase):
        self.speaker.Speak(phrase)
 
"""
The callback class that handles the events raised by the speech object.
"""

class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):

    """Called when a word/phrase is successfully recognized and is
        found in a currently open grammar """

    def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
        newResult = win32com.client.Dispatch(Result)
        print ("You said: ",newResult.PhraseInfo.GetText())
    
if __name__=='__main__':
    wordsToAdd = [ "One", "Two", "Three", "Four" ]
    speechReco = SpeechRecognition(wordsToAdd)
    while 1:
        pythoncom.PumpWaitingMessages()
The result can be see on my youtube channel:

Monday, May 20, 2019

Python 3.7.3 : Use the tweepy to deal with twitter api - part 002.

The tutorial for today is about with python 3.7.3
The development team comes with this intro:
An easy-to-use Python library for accessing the Twitter API.
You need to have an application on twitter with tokens and key, see here.
The first step is to install this python module:
C:\Python373\Scripts>pip install tweepy
...
Successfully built PySocks
Installing collected packages: PySocks, tweepy
Successfully installed PySocks-1.6.8 tweepy-3.7.0
This is the source code I used.
Is very simple to understand:
import tweepy

# set the keys strings
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''

# get  the keys 
input("consumer_key :")
input("consumer_secret :")
input("access_token :")
input("access_token_secret :")
print("--------")

# authentification and get data 
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
user = api.me()
print (user.name)
user = api.me()
print(user.name)
print(user.location)
search = input("search :")
numberOfTweets = int(input("number of tweets :"))
phrase = input("response phrase :")
for follower in tweepy.Cursor(api.followers).items():
    try:
        follower.follow()
    except tweepy.error.TweepError:
        pass

print("Followed everyone that is following " + user.name)
for tweet in tweepy.Cursor(api.search, search).items(numberOfTweets):
    try:
    #Reply
        print('\nTweet by: @' + tweet.user.screen_name)
        print('ID: @' + str(tweet.user.id))
        tweetId = tweet.user.id
        username = tweet.user.screen_name
        api.update_status("@" + username + " " + phrase, in_reply_to_status_id = tweetId)
        print ("Replied with " + phrase)
    except tweepy.TweepError as e:
        print(e.reason)
    except StopIteration:
        break

for tweet in tweepy.Cursor(api.search, search).items(numberOfTweets):
    try:
        #Reply
        tweet.favorite()
        print('Favorited the tweet') 
        #Retweet
        tweet.retweet()
        print('Retweeted the tweet')   
        #Follow
        tweet.user.follow()
        print('Followed the user')        
    except tweepy.TweepError as e:
        print(e.reason)
    except StopIteration:
        break
The result is this:
C:\Python373>python.exe tweepy_001.py
catafest
catafest
Romania, Suceava, Falticeni
search :Catalin
number of tweets :1
response phrase :Festila
Followed everyone that is following catafest

Tweet by: @rumeys1a
ID: @1103384469703196673
Replied with Festila
Favorited the tweet
Retweeted the tweet
Followed the user
You can see what are the technical follow limits for twitter here.
The tweepy Documentation can be read at this link.

Python 3.7.3 : The google-cloud-vision python module - part 002.

I used Windows 8.1 and python 3.7.3 version.
The first step is to install the python module.
C:\Python373\Scripts>pip install --upgrade google-cloud-vision
You can see another tutorial about this python module here.
Let's test the python module.
C:\Python373>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD6
4)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from google.cloud import vision
>>> from google.cloud.vision import types
>>> from PIL import Image, ImageDraw
You need to have billing enabled for your Google Cloud Platform project.
Evaluated prices can vary and Google gives us this online calculator, or let us test with a FREE account with 300$.
To change the billing account go to the Google Cloud Platform Console.
Open the console left side menu and select Billing.
If you have more than one billing account then you need to solve this issue, see this webpage.
The Cloud Vision API integrates vision features, like: including image labeling, face, logo, and landmark detection, optical character recognition (OCR), and detection of explicit content, into applications.
You need to set the authentification for your google account and your project, see this page.
The next step is to instantiate a client:
client = vision.ImageAnnotatorClient()
Google provides this tutorial with the same steps as into this tutorial.
You can test and read more about Google Vision on the official page.

Sunday, May 5, 2019

Python 3.7.3 : Using the cognitive face detection from Azure Microsoft .

The Microsoft Azure comes with this free feature named Computer Vision:
This API key is currently active
7 days remaining
Distill actionable information from images
5,000 transactions, 20 per minute.

You can test with the cognitive_face python module from GitHub.
Another good example of extract printed text can be found at docs.microsoft.com.
Let's install it:
C:\Python373\Scripts>pip install cognitive_face
...
Installing collected packages: cognitive-face
Successfully installed cognitive-face-1.4.2
Now a simple intro using the dir:
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD6
4)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cognitive_face as CF
>>> dir(CF)
['BaseUrl', 'CognitiveFaceException', 'Key', '__builtins__', '__cached__', '__do
c__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__
', 'face', 'face_list', 'large_face_list', 'large_face_list_face', 'large_person
_group', 'large_person_group_person', 'large_person_group_person_face', 'person'
, 'person_group', 'util']
For detections you need to use the face with detect:
import cognitive_face as CF
# Replace with your valid Subscription Key here.
KEY = '111111111111'  
CF.Key.set(KEY)
# Replace with your regional Base URL based by 
# https://azure.microsoft.com/en-us/try/cognitive-services/my-apis/?apiSlug=computer-vision

BASE_URL = 'https://westcentralus.api.cognitive.microsoft.com/vision/v2.0'  
CF.BaseUrl.set(BASE_URL)

img_url = 'https://previews.123rf.com/images/boarding1now/boarding1now150
8/boarding1now150800143/44403889-background-collage-large-group-portrait
-of-multiracial-young-smile-smiling-people-social-media.jpg'
result = CF.face.detect(img_url)
print (result)

img_url = 'https://raw.githubusercontent.com/Microsoft/Cognitive-Face-
Windows/master/Data/detection1.jpg'
result2 = CF.face.detect(img_url)
print (result2)
The result of this running code is:
C:\Python373>python.exe face_cognitive_001.py
{'objects': [{'rectangle': {'x': 145, 'y': 14, 'w': 121, 'h': 160}, 'object': 'p
erson', 'confidence': 0.535}, {'rectangle': {'x': 533, 'y': 25, 'w': 121, 'h': 1
84}, 'object': 'person', 'confidence': 0.535}], 'requestId': '9a3d41e9-b8cc-49ac
-a99b-f3da8d5fac2a', 'metadata': {'width': 1300, 'height': 866, 'format': 'Jpeg'
}}
{'objects': [{'rectangle': {'x': 236, 'y': 0, 'w': 620, 'h': 657}, 'object': 'pe
rson', 'confidence': 0.912}], 'requestId': 'fd415dd3-0f11-4cac-b307-f58888fc875a
', 'metadata': {'width': 1000, 'height': 664, 'format': 'Jpeg'}}
If you take a look at the images and results will see how this works.

Friday, May 3, 2019

Python 3.7.3 : Try Ethereum with web3.

Today I tested the web3 python module.
This is the Ethereum python module.
More about this python module can be found here.
Ethereum programming has a large area of development.
For transaction you can use:
from web3 import Web3, HTTPProvider, IPCProvider, WebsocketProvider
I tested with an account from infura.io.
>>> from web3 import Web3, HTTPProvider, IPCProvider, WebsocketProvider
>>> w3 = Web3(HTTPProvider('ropsten.infura.io/v3/1f2fb5d1e1be4c11acdbbb07a2e06a1
c'))
>>> dir(w3)
['EthereumTesterProvider', 'HTTPProvider', 'IPCProvider', 'Iban', 'RequestManage
r', 'TestRPCProvider', 'WebsocketProvider', '__class__', '__delattr__', '__dict_
_', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__mo
dule__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__seta
ttr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_ens', 'adm
in', 'ens', 'eth', 'fromWei', 'isAddress', 'isChecksumAddress', 'isConnected', '
manager', 'middleware_stack', 'miner', 'net', 'parity', 'personal', 'providers',
 'sha3', 'soliditySha3', 'testing', 'toBytes', 'toChecksumAddress', 'toHex', 'to
Int', 'toText', 'toWei', 'txpool', 'version']
>>> from eth_account import Account
>>> my_account = Account.create('KEYSMASH FESTILA_GEORGE_CATALIN 1530')
>>> my_account.address
'0x2b8e7E92064F718B0AC91c381968baC8D1561d7d'
>>> # check the adresss and add it to infura.io
...
>>> address=Web3.toChecksumAddress(my_account.address)
>>> print (address)
0x2b8e7E92064F718B0AC91c381968baC8D1561d7d
Let's start with the default installation and some testing with a message:
C:\Python373\Scripts>pip install virtualenv
Requirement already satisfied: virtualenv in c:\python373\lib\site-packages (16.
5.0)
C:\Python373\Scripts>cd ..
C:\Python373>virtualenv env
Using base prefix 'c:\\python373'
New python executable in C:\Python373\env\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
(env) C:\Python373\Scripts>pip install web3
(env) C:\Python373\Scripts>cd ..
(env) C:\Python373>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD6
4)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from web3.auto import w3
>>> dir(w3)
['EthereumTesterProvider', 'HTTPProvider', 'IPCProvider', 'Iban', 'RequestManage
r', 'TestRPCProvider', 'WebsocketProvider', '__class__', '__delattr__', '__dict_
_', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__mo
dule__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__seta
ttr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_ens', 'adm
in', 'ens', 'eth', 'fromWei', 'isAddress', 'isChecksumAddress', 'isConnected', '
manager', 'middleware_stack', 'miner', 'net', 'parity', 'personal', 'providers',
 'sha3', 'soliditySha3', 'testing', 'toBytes', 'toChecksumAddress', 'toHex', 'to
Int', 'toText', 'toWei', 'txpool', 'version']
One example with message hashing mechanism :
>>> from web3.auto import w3
>>> from eth_account.messages import defunct_hash_message
>>> msg = "Hello world!"
>>> private_key = b"46474346474346474330313246474346"
>>> b"46474346474346474330313246474346".decode("utf-8", errors="ignore")
'46474346474346474330313246474346'
>>> signed_message = w3.eth.account.signHash(message_hash, private_key=private_
ey)
>>> signed_message
AttrDict({'messageHash': HexBytes('0xaa05af77f274774b8bdc7b61d98bc40da523dc2821f
dea555f4d6aa413199bcc'), 'r': 77081816383569854304871908993806785907809199379052
64818114708355336522791320, 's': 50065392922095131225302404096180206949656260456
560091946111418092912822128476, 'v': 27, 'signature': HexBytes('0x110aad1b6fa3f8
9ef0ceff3579de2a166c6cfefee73bd8b13364d71745f979986eb00219a1d6630a09fb0e7db3b713
51bce570d5842fa39fbae390fdfa7b0f5c1b')}) 

Thursday, May 2, 2019

Python 3.7.3 and Django CMS.

From official www.django-cms.org:
django CMS was originally conceived by web developers frustrated with the technical and security limitations of other systems. Its lightweight core makes it easy to integrate with other software and put to use immediately, while its ease of use makes it the go-to choice for content managers, content editors and website admins.
The django-crm.readthedocs.io/en/latest tells us:
Django-CRM provides a dashboard where you can manage customers at sales of the organization. It Provides to manage leads information and its activity, track issues from leads, contacts to send emails.
...
Modules used:

Python >= 2.6 (or Python 3.4)
Django = 1.9.6
JQuery >= 1.7

Let's start with these commands:
C:\Python373\Scripts>pip install --upgrade virtualenv
Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/4f/ba/6f9315180501d5ac3e70
7f19fcb1764c26cc6a9a31af05778f7c2383eadb/virtualenv-16.5.0-py2.py3-none-any.whl
(2.0MB)
     |████████████████████████████████| 2.0MB 939kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.5.0

C:\Python373\Scripts>cd ..

C:\Python373>virtualenv env
Using base prefix 'c:\\python373'
New python executable in C:\Python373\env\Scripts\python.exe
Installing setuptools, pip, wheel...
done.

C:\Python373>env\Scripts\activate

(env) C:\Python373>pip install djangocms-installer

(env) C:\Python373>djangocms mysite
...
  Applying djangocms_video.0005_migrate_to_filer... OK
  Applying djangocms_video.0006_field_adaptions... OK
  Applying djangocms_video.0007_create_nested_plugin... OK
  Applying djangocms_video.0008_reset_null_values... OK
  Applying djangocms_video.0009_removed_null_values... OK
  Applying djangocms_video.0010_videoplayer_parameters... OK
  Applying easy_thumbnails.0001_initial... OK
  Applying easy_thumbnails.0002_thumbnaildimensions... OK
  Applying filer.0008_auto_20171117_1313... OK
  Applying filer.0009_auto_20171220_1635... OK
  Applying filer.0010_auto_20180414_2058... OK
  Applying filer.0011_auto_20190418_0137... OK
  Applying menus.0001_initial... OK
  Applying sessions.0001_initial... OK
  Applying sites.0002_alter_domain_unique... OK
Creating admin user
All done!
Get into "C:\Python373\mysite" directory and type "python manage.py runserver" 
to start your project

(env) C:\Python373>cd mysite

(env) C:\Python373\mysite>python manage.py runserver
The result is this:

Tuesday, April 30, 2019

Python 3.7.3 : Fix kivy python module installation.

Kivy is a multi-platform GUI development library for Python, running on Windows, Mac, Linux, Android, and iOS.
Today I tested kivy python module with python version 3.7.3 and I got some errors but I fixed.
I started with the default installation using the pip tool.
C:\Python373>cd Scripts
C:\Python373\Scripts>pip install kivy
...
Installing collected packages: docutils, pygments, Kivy-Garden, kivy
...
Successfully installed kivy-1.10.1
I used a default script to test the kivy python module.
When I tested I got these errors:
[CRITICAL] [Window      ] Unable to find any valuable Window provider.
sdl2 - ImportError: DLL load failed: The specified module could not be found.
  File "C:\Python373\lib\site-packages\kivy\core\__init__.py", line 59, in core_
select_lib
    fromlist=[modulename], level=0)
  File "C:\Python373\lib\site-packages\kivy\core\window\window_sdl2.py", line 26
, in 
    from kivy.core.window._window_sdl2 import _WindowSDL2Storage
...
[CRITICAL] [App         ] Unable to get a Window, abort.
Iinstall the kivy.deps.sdl2:
C:\Python373>cd Scripts
C:\Python373\Scripts>pip install kivy.deps.sdl2
Collecting kivy.deps.sdl2
  Downloading https://files.pythonhosted.org/packages/93/84/a0dc274d993db6f9ebdf
41eb4d55b032de005dbf47e4d54602cf83708b08/kivy.deps.sdl2-0.1.18-cp37-cp37m-win_am
d64.whl (2.5MB)
     |████████████████████████████████| 2.5MB 726kB/s
Installing collected packages: kivy.deps.sdl2
Successfully installed kivy.deps.sdl2-0.1.18
When I tested again, I got another error:
[CRITICAL] [App         ] Unable to get a Window, abort.
I try to see if the install of these python modules are right:
C:\Python373>cd Scripts
C:\Python373\Scripts>pip install pypiwin32
Requirement already satisfied: pypiwin32 in c:\python373\lib\site-packages (223)
Requirement already satisfied: pywin32>=223 in c:\python373\lib\site-packages 
(from pypiwin32) (224)
C:\Python373\Scripts>pip install kivy.deps.glew
Collecting kivy.deps.glew
...
Now the kivy works well:
[INFO   ] [Kivy        ] v1.10.1
[INFO   ] [Python      ] v3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC
v.1916 64 bit (AMD64)]
[INFO   ] [Factory     ] 194 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif
 (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used 
[INFO   ] [GL          ] OpenGL version 
[INFO   ] [GL          ] OpenGL vendor 
[INFO   ] [GL          ] OpenGL renderer 
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version 
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Leaving application in progress...
If you search on web you will se this is a common error and some users use this solution:
python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2
python -m pip install kivy.deps.glew
python -m pip install kivy.deps.gstreamer
Hope this help you with python programming and kivy interface.

Monday, April 29, 2019

Python 3.7.3 : Get location of International Space Station.

Today I tested the urllib python module with python 3.7.3 and json python module.
The issue was to get the location of International Space Station - Open Notify.
The International Space Station is moving at close to 28,000 km/h so its location changes really fast! Where is it right now?
This is an open source project to provide a simple programming interface for some of NASA’s awesome data.
I do some of the work to take raw data and turn them into APIs related to space and spacecraft.
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD6
4)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib.request
>>> with urllib.request.urlopen('http://api.open-notify.org/iss-now.json') as f:
        print(f.read(300))
...
b'{"iss_position": {"longitude": "-86.9247", "latitude": "-38.3744"}, "message":
 "success", "timestamp": 1556575039}'
>>> with urllib.request.urlopen('http://api.open-notify.org/iss-now.json') as f:
...     source = f.read()
...     data = json.loads(source)
...
>>> print(data)
{'iss_position': {'longitude': '151.1941', 'latitude': '49.4702'}, 'message': 's
uccess', 'timestamp': 1556578621}
>>> print(data['iss_position']['longitude'])
151.1941
>>> print(data['iss_position']['latitude'])
49.4702
>>> print(data['message'])
success

Sunday, April 28, 2019

Python 3.7.3 and memory_profiler python module.

Today I will come up with a simpler and more effective tutorial in python programming.
First, I need to install the psutil python module for the example of this tutorial.
C:\Python373>cd Scripts
C:\Python373\Scripts>pip install psutil
Python memory monitor is very important for debugging application performance and fix bugs.
You can solve this issue is the python module named memory_profiler, see more here.
Let's install this python module with the pip python tool:
C:\Python373\Scripts>pip install memory_profiler
Let's start with a simple python script example:
import psutil

def test_psutil():
 # gives a single float value
 print(psutil.cpu_percent())
 # gives an object with many fields
 print(psutil.virtual_memory())
 # you can convert that object to a dictionary 
 print(dict(psutil.virtual_memory()._asdict()))
if __name__ == '__main__':
 test_psutil()

C:\Python373>python psutil_001.py
0.0
svmem(total=4171108352, available=1153679360, percent=72.3, used=3017428992, fre
e=1153679360)
{'total': 4171108352, 'available': 1153671168, 'percent': 72.3, 'used': 30174371
84, 'free': 1153671168}
The same result can see if you use memory_profiler
C:\Python373>python -m memory_profiler psutil_001.py
100.0
svmem(total=4171108352, available=1149018112, percent=72.5, used=3022090240, fre
e=1149018112)
{'total': 4171108352, 'available': 1149087744, 'percent': 72.5, 'used': 30220206
08, 'free': 1149087744}
Let's decorate python source code with @profile annotation to have a good output.
import psutil
@profile
def test_psutil():
 # gives a single float value
 print(psutil.cpu_percent())
 # gives an object with many fields
 print(psutil.virtual_memory())
 # you can convert that object to a dictionary 
 print(dict(psutil.virtual_memory()._asdict()))
if __name__ == '__main__':
 test_psutil()

Sure, this error tells us the decorate not working in the default way.
C:\Python373>python psutil_001.py
Traceback (most recent call last):
  File "psutil_001.py", line 2, in 
    @profile
NameError: name 'profile' is not defined
In this case, the decorate profile works great with the python module and give us all the information we need:
C:\Python373>python -m memory_profiler psutil_001.py
100.0
svmem(total=4171108352, available=1022672896, percent=75.5, used=3148435456, fre
e=1022672896)
{'total': 4171108352, 'available': 1022783488, 'percent': 75.5, 'used': 31483248
64, 'free': 1022783488}
Filename: psutil_001.py

Line #    Mem usage    Increment   Line Contents
================================================
     2   15.219 MiB   15.219 MiB   @profile
     3                             def test_psutil():
     4                                  # gives a single float value
     5   15.230 MiB    0.012 MiB        print(psutil.cpu_percent())
     6                                  # gives an object with many fields
     7   15.230 MiB    0.000 MiB        print(psutil.virtual_memory())
     8                                  # you can convert that object to a dicti
onary
     9   15.234 MiB    0.004 MiB        print(dict(psutil.virtual_memory()._asdi
ct()))
Let's test with another python script named 001.py :
from memory_profiler import profile

@profile(precision=4)
def test():
    a = 0
    a = a + 1

if __name__ == "__main__":
    test()
The result with precision=4 is this:
C:\Python373>python -m memory_profiler 001.py
Filename: 001.py

Line #    Mem usage    Increment   Line Contents
================================================
     3  15.3945 MiB  15.3945 MiB   @profile(precision=4)
     4                             def test():
     5  15.3945 MiB   0.0000 MiB       a = 0
     6  15.3945 MiB   0.0000 MiB       a = a + 1
If we change the precision=1 then this is the result:
C:\Python373>python -m memory_profiler 002.py
Filename: 002.py

Line #    Mem usage    Increment   Line Contents
================================================
     3     15.4 MiB     15.4 MiB   @profile(precision=1)
     4                             def test():
     5     15.4 MiB      0.0 MiB       a = 0
     6     15.4 MiB      0.0 MiB       a = a + 1
A good source of information for this python module can be found at GitHub.

Saturday, April 27, 2019

Django REST framework - part 001.

Today I will introduce you a tutorial to fix some of the necessary elements presented in the old tutorial.
The manage tool shell can also give us some info:
C:\Python373\Scripts\example>python manage.py shell
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD6
4)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from test001.models import test
>>> from django.db.models import Count, Min, Max, Avg
>>> out = test.objects.all
>>> out
...
One note about error method, do not use like this:
>>> out = test.objects.all
>>> out[0]
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'method' object is not subscriptable
The correct way method is:
>>> out = test.objects.all()
>>> out
]>
>>> out[0]

>>> vars(out[0])
{'_state': , 'id'
: 1, 'first_name': 'Cătălin George', 'last_name': 'Feștilă'}
We can use the annotate.
This issue can solve it into this way:
>>> minimal = test.objects.annotate(Min('last_name'))
>>> minimal
]>
>>> minimal[0]

>>> vars(minimal[0])
{'_state': , 'id'
: 1, 'first_name': 'Cătălin George', 'last_name': 'Feștilă', 'last_name__min': '
Feștilă'}
See the result of the annotate add the last_name__min value.
I created another class named city with just one field named city_name and I fill with one value:
All changes are posted at the end of this tutorial.
Let's test with the shell some of these new changes:
C:\Python373\Scripts\example>python manage.py shell
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit 
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from test001.models import test,city
>>> from django.db.models import Count, Min, Max, Avg
>>> out_test = test.objects.all()
>>> out_city = city.objects.all()
>>> out_test
]>
>>> out_city
]>
>>> vars(out_city[0])
{'_state': , 'id'
: 1, 'city_name': 'Fălticeni'}
We can easy test the new example:
>>> minimal = test.objects.annotate(Min('last_name'))
>>> minimal
]>
>>> vars(minimal[0])
{'_state': , 'id'
: 1, 'first_name': 'Cătălin George', 'last_name': 'Feștilă', 'last_name__min': '
Feștilă'}
>>> minimal = city.objects.annotate(Min('city_name'))
>>> minimal
]>
>>> vars(minimal[0])
{'_state': , 'id'
: 1, 'city_name': 'Fălticeni', 'city_name__min': 'Fălticeni'}
>>> filter_test = test.objects.filter(id = 1)
>>> vars(filter_test)
{'model': , '_db': None, '_hints': {}, 'query': 
, '_result_cache': 
None, '_sticky_filter': False, '_for_write': False, '_prefetch_related_lookups':
(), '_prefetch_done': False, '_known_related_objects': {}, '_iterable_class': , '_fields': None}
>>> vars(filter_test[0])
{'_state': , 'id'
: 1, 'first_name': 'Cătălin George', 'last_name': 'Feștilă'}
Feel free to test with my old and the new example:
# models.py
from django.db import models

class test(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    
class city(models.Model):
    city_name = models.CharField(max_length=30)
#admin.py
from django.contrib import admin
from .models import test, city
admin.site.register(test)
admin.site.register(city)
#serializers.py
from rest_framework import serializers
from .models import test, city

class test_serializer(serializers.ModelSerializer):
    class Meta:
        model = test
        fields = ('id', 'first_name', 'last_name')

class city_serializer(serializers.ModelSerializer):        
    class Meta:
        model = city
        fields = ('id', 'city_name')
#views.py
from django.shortcuts import render
from rest_framework import viewsets
from .models import test, city
from .serializers import test_serializer, city_serializer

class test_view(viewsets.ModelViewSet):
    #query to get all information from database
    queryset = test.objects.all()
    serializer_class = test_serializer

class city_view(viewsets.ModelViewSet):
    #query to get all information from database
    queryset = city.objects.all()
    serializer_class = city_serializer
#urls.py
from django.urls import path, include
from . import views
from rest_framework import routers

router = routers.DefaultRouter()
router.register('test001', views.test_view)
router.register('city', views.city_view)
#add to path 
urlpatterns = [
    path('', include(router.urls))
]

Friday, April 26, 2019

Python 3.7.3 and Django REST framework.

Today I tested something simpler for beginners: Django REST framework.
Once you understand how it works then it's simple to use.
This tutorial does not address the security issues generated by the REST, Django framework.
The official webpage comes with many information and technical specifications for this API:
Django REST framework is a powerful and flexible toolkit for building Web APIs.
The example I've submitted is built into the Scripts folder because I did not use the virtual environment.
Let's start installing the python module.
C:\Python373\> cd Scripts
C:\Python373\Scripts\>pip install djangorestframework
C:\Python373\Scripts\>django-admin startproject example
C:\Python373\Scripts\>cd example 
C:\Python373\Scripts\>python manage.py migrate 
C:\Python373\Scripts\>python manage.py createsuperuser
C:\Python373\Scripts\>python manage.py startapp test001
In the example folder we will make changes.
First is the settings.py file:
INSTALLED_APPS = [
...
    'rest_framework',
    'test001']
The next file is the urls.py:
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('test001.urls'))
]
Also make changes in the test001 folder
You must create a python script and call it urls.py:
from django.urls import path, include

# this urlpatterns will fill later 
urlpatterns = []
You make changes to the file models.py and create a named class test.
This class will have two fields that we will update.
from django.db import models

class test(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
44/5000
The following commands will synchronize the database:
C:\Python373\Scripts\example>python manage.py makemigrations
...
  test001\migrations\0001_initial.py
    - Create model test
C:\Python373\Scripts\example>python manage.py migrate
...
Running migrations:
...
Another step is to add the test into admin.py script:
from django.contrib import admin
from .models import test
admin.site.register(test)
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON , XML or other content types.
Let's create an serializers.py python script into test001 folder and use this code:
from rest_framework import serializers
from .models import test

class test_serializer(serializers.ModelSerializer):
    class Meta:
        model = test
        fields = ('id', 'first_name', 'last_name')
Also is need to update the views.py python script:
from django.shortcuts import render
from rest_framework import viewsets
from .models import test
from .serializers import test_serializer

class test_view(viewsets.ModelViewSet):
    #query to get all information from database
    queryset = test.objects.all()
    serializer_class = test_serializer
Now because is all create the last step is to fix the urls.py from test001 folder with the routers.
The REST framework adds support for automatic URL routing to Django.
from django.urls import path, include
from . import views
from rest_framework import routers

router = routers.DefaultRouter()
router.register('test001', views.test_view)
#add to path 
urlpatterns = [
    path('', include(router.urls))
]
Now you can test it with:
C:\Python373\Scripts\example>python manage.py runserver
If you want to make changes into models.py then you will need to use the commands to synchronize the database after these use"commands
makemigrations and migrate to fix errors.
If you encounter such run-time errors
"GET /static/assets/js/docs.min.js HTTP/1.1" 404 1667..."
These errors can be the result of settings like DEBUG, STATICFILES_DIRS or STATIC_ROOT from file settings.py.
Then you need to execute python manage.py collectstatic and Django goes through all directories where static files can be found and places them in your static root.
The result of this tutorial can be see on my youtube channel:

Wednesday, April 24, 2019

Google's Python Class - another step.

Here's something I like and I hope it should be known in the Python community.
Some people from Google want to attract the python community into a learning process.
Although most of the API documentation examples do not exist in the Python programming language, they have not disappeared.
Let's hope this little step will increase the chances of programming Google with the Python programming language.
This material was created by Nick Parlante working in the engEDU group at Google.
Welcome to Google's Python Class -- this is a free class for people with a little bit of programming experience who want to learn Python. The class includes written materials, lecture videos, and lots of code exercises to practice Python coding. These materials are used within Google to introduce Python to people who have just a little programming experience. The first exercises work on basic Python concepts like strings and lists, building up to the later exercises which are full programs dealing with text files, processes, and http connections. The class is geared for people who have a little bit of programming experience in some language, enough to know what a "variable" or "if statement" is. Beyond that, you do not need to be an expert programmer to use this material.
Read more about this course here.

Tuesday, April 23, 2019

Python 3.7.3 : Testing firebase with Python 3.7.3 .

The tutorial for today consists of using the Firebase service with python version 3.7.3 .
As you know Firebase offers multiple free and paid services.
In order to use the Python programming language, we need to use the pip utility to enter the required modules.
If your installation requires other python modules then you will need to install them in the same way.
C:\Python373>pip install firebase-admin
C:\Python373\Scripts>pip install google-cloud-firestore
The next step is to log in with a firebase account and create or use a project with a database.
You must use this link to see your project data and create your JSON configuration file for access.
Here's a screenshot of an old project in another programming language that I used for this tutorial.

At point 1, you will find Project Settings - Service accounts and you will need to use the Generate new private key button.
This will generate a JSON file that we will use in the python script.
Be careful to define the path to the python script file.
At point 2, you will find the database or you will need to create it to use it.
The script I'm going to present has commented on the lines for a better understanding of how to use it.
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

# the go-test.json from my project settings 
cred = credentials.Certificate("go-test.json")

# start using firebase python modules 
firebase_admin.initialize_app(cred)

# access the database
database = firestore.client()

# access the collection with limit 
my_ref = database.collection("test").limit(2)

# show the values from database
try:
    docs = my_ref.stream()
    for doc in docs:
         print(u'Doc Data:{}'.format(doc.to_dict()))
except:
    print(u'Missing data')

# result running
#Doc Data:{'nrcrt': 1, 'string_value': 'this is a text'}
#Doc Data:{'name': 'test', 'added': 'just now'}

# add data to database
my_ref_add = database.collection("test")
my_ref_add.add({u'nickname': u'catafest', u'friend': u'Last Ocelot'})

# show the new database values
try:
    docs = my_ref_add.stream()
    for doc in docs:
         print(u'Doc Data:{}'.format(doc.to_dict()))
except:
    print(u'Missing data')

# result running
#Doc Data:{'nrcrt': 1, 'string_value': 'this is a text'}
#Doc Data:{'friend': 'Last Ocelot', 'nickname': 'catafest'}
#Doc Data:{'name': 'test', 'added': 'just now'}

# get just one 
doc = database.collection('test').document('doc_id').get()

# show create time 
print(doc.create_time)

# result running
#seconds: 1556014478
#nanos: 460439000

Thursday, April 18, 2019

About psychopy tool.

A good definition for this tool can be found at the Wikipedia website:
2002: PsychoPy was originally written by Peirce as a proof of concept - that a high-level scripting language could generate experimental stimuli in real time (existing solutions, such as Psychtoolbox, had to pre-generate movies or use CLUT animation techniques).
The install of this python module is very simple:
C:\Python373\Scripts>pip install psychopy
Using this command to start this tool:
C:\Python373>psychopy
The tool starts with two graphical interfaces:
  • one for Coder area;
  • one for the project with an untitled.psyexp;
Let's see one screenshot with this tool:

This open source tool, written in the Python programming language, help you to run a wide range of neuroscience, psychology and psychophysics experiments.
I searched the internet for the capabilities of this tool and the obvious conclusion is a utility for building experiments with a wide range of applicability.
This tool lets you use the pavlovia website, see more:
Pavlovia is a place for the wide community of researchers in the behavioural sciences to run, share, and explore experiments online.
This tool used commonly-used components for linguistic experiments:
  1. Text Component (display text on the screen);
  2. Sound Component (play sounds);
  3. Keyboard Component (receive input from the keyboard);
  4. RatingScale Component (collect a numeric rating or a choice from a few alternatives, via the mouse, the keyboard or both);
  5. Code Component (insert short pieces of python code into your experiments (e.g. time stamp for the production task);
The easy way is to use the Builder tool to generate a wide range of experiments easily from the Builder using its intuitive, graphical user interface (GUI).
Today I will use the Coder with programming a simple example.
To access demos, you need to create a python script in the Coder area (use File menu - New) then use the Demos from the application menu.
Everything in a PsychoPy experiment needs a unique name.
The name must contain only letters, numbers and underscores and not contain spaces, punctuation or mathematical symbols.
This tool saves several data files: a Microsoft Excel (spreadsheet) file, a psydat file, and a log file.
The example I used today for this tutorial is how to import from a CSV file named colors_001.csv and used with psychopy python module to create stimuli:
3.3;"red"
1.1;"green"
4.4;"red"
2.2;"green"
This python script named colors.py I used to import the CSV file and use it:
from psychopy import core, visual, event
import csv
  
## Setup section, read experiment variables from file
win = visual.Window([400,300], monitor="testMonitor", units="cm", fullscr=False)
stimuli = []
datafile = open("colors_001.csv", "r",encoding="utf8")
reader = csv.reader(datafile, delimiter=";")
for row in reader:
    if len(row)==2:         # ignore empty and incomplete lines
        size = float(row[0])  # the first element in the row converted to a floating point number
        color = row[1]        # the second element in the row
        stimulus = visual.Rect(win, width=size, height=size)
        stimulus.fillColor = color
        stimuli.append(stimulus)
datafile.close()
  
## Experiment Section, use experiment variables here
for stimulus in stimuli:
    stimulus.draw()
    win.flip()
    core.wait(1.000)

## Closing Section
win.close()
core.quit()
The result of this script will show you four squares colored by the CSV file.
If you don't like to use the Builder or Coder areas, then you can simply use the python:
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from psychopy import visual, core
pygame 1.9.5
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> win = visual.Window()
>>> msg = visual.TextStim(win, text=u"Hello python users!")
>>> msg.draw()
>>> win.flip()
47.541564770566765
The output will be a window with a text message.

Wednesday, April 17, 2019

Update python modules of 3.73 version.

Today we tested an older tool with the new version of python 3.7.3.
This is a tool that will help you update your python modules.
Here's how to install:
C:\Python373\Scripts>pip install pip-review
Collecting pip-review
...
Requirement already satisfied: pyparsing>=2.0.2 in c:\python373\lib\site-package
s (from packaging->pip-review) (2.4.0)
Installing collected packages: packaging, pip-review
Successfully installed packaging-19.0 pip-review-1.0
Here is the complete overview of the options.
C:\Python373\Scripts>pip-review -h
usage: pip-review [-h] [--verbose] [--raw] [--interactive] [--auto]

Keeps your Python packages fresh.

optional arguments:
  -h, --help         show this help message and exit
  --verbose, -v      Show more output
  --raw, -r          Print raw lines (suitable for passing to pip install)
  --interactive, -i  Ask interactively to install updates
  --auto, -a         Automatically install every update found

Unrecognised arguments will be forwarded to pip list --outdated, so you can
pass things such as --user, --pre and --timeout and they will do exactly what
you expect. See pip list -h for a full overview of the options.
To update all python modules you can use:
C:\Python373\Scripts>pip-review --auto
To run interactively, you can ask to upgrade for each package:
C:\Python373\Scripts>pip-review --interactive


Monday, April 15, 2019

Using the ORB feature from OpenCV python module.

Today I will show you a simple script using the ORB (oriented BRIEF), see C++ documentation / OpenCV.
The algorithm uses FAST in pyramids to detect stable keypoints, selects the strongest features using FAST or Harris response, finds their orientation using first-order moments and computes the descriptors using BRIEF (where the coordinates of random point pairs (or k-tuples) are rotated according to the measured orientation).
One good feature of ORB is the is rotation invariant and resistant to noise.
The ORB descriptor use the Center of the mass of the patch of the Moment (sum of x,y), Centroid (the result of the matrix of all moment) and Orientation ( the atan2 of moment one and two).
One good article about ORB can be found here.
Let's see the script code of this python example:
import cv2
import numpy as np 

image_1 = cv2.imread("1.png", cv2.IMREAD_GRAYSCALE)
image_2 = cv2.imread("2.png", cv2.IMREAD_GRAYSCALE)

orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(image_1,None)
kp2, des2 = orb.detectAndCompute(image_2,None)

bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key = lambda x:x.distance)

matching_result = cv2.drawMatches(image_1, kp1, image_2, kp2, matches[:150], None, flags=2)

cv2.imshow("Image 1", image_1)
cv2.imshow("Image 2", image_2)
cv2.imshow("Matching result", matching_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
The script use two file images 1.png and 2.png.
The result is an image composed of the two on which the areas of similitude are traced as detected by the mathematical algorithm.

Sunday, April 14, 2019

Using the python module music21.

What is music21?
Music21 is a set of tools for helping scholars and other active listeners answer questions about music quickly and simply. If you’ve ever asked yourself a question like, “I wonder how often Bach does that” or “I wish I knew which band was the first to use these chords in this order,” or “I’ll bet we’d know more about Renaissance counterpoint (or Indian ragas or post-tonal pitch structures or the form of minuets) if I could write a program to automatically write more of them,” then music21 can help you with your work.
This toolkit for Computer-Aided Musical Analysis was developed at MIT by cuthbertLab. Michael Scott Cuthbert, Principal Investigator.
The development of music21 is supported by the generosity of the Seaver Institute and the NEH.
The tutorial today is about the python music21 module.
Let's start with the default pip tool to install this python module:
C:\Python364\Scripts>pip install --upgrade music21
Collecting music21
...
  Running setup.py install for music21 ... done
Successfully installed music21-5.5.0
The next step tells us to install some additional python modules:
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from music21 import * 
music21: Certain music21 functions might need these optional packages: matplotlib,
 scipy;
if you run into errors, install them by following the instructions at
http://mit.edu/music21/doc/installing/installAdditional.html
Let's install these python modules:
C:\Python373\Scripts>pip install matplotlib
C:\Python373\Scripts>pip install scipy
Let's test and play a simple example (Bach’s BWV 66.6):
>>> from music21 import *
>>> s = corpus.parse('bwv66.6')
>>> sChords = s.chordify()
>>> sChords
...
>>> sChords.show('midi')
If you want to save it, use:
>>> sChords.write("midi", "bach6.mid")
'bach6.mid'
Another example is to play a note on guitar (C#):
from music21 import stream, instrument
from music21.note import Note
cd = Note("C#", type='quarter')
test = stream.Part()
test.insert(0, instrument.AcousticGuitar())
test_measure = stream.Measure()
test_measure.append(cd)
test.append(test_measure)
test.show('midi')
You can see all the instruments here.
This python module is very complex and can be used with additional software, see here.
More details can be found in the documentation.

Friday, April 12, 2019

Using pytineye to automate searching for images.

The TinEye API is ideally suited for image and profile verification, UGC moderation, copyright compliance and fraud detection.
Read more about the TinEye API here.
You need to use authentication for this API, read here.
To use the TinEye API you must purchase a search bundle.
The documentation for Python can be found here.
Let's start with the installation.
You need to download the zip file from GitHub and install using the pip tool:
C:\Python373\Scripts>pip3.7.exe install pytineye-master.zip
...
Successfully installed pytineye-1.3

>>> import pytineye
>>> from pytineye import TinEyeAPIRequest
>>> myapi = TinEyeAPIRequest('http://api.tineye.com/rest/', 'your_public_key', 'your_private_key')
>>> myapi.remaining_searches()
{'bundles': [], 'total_remaining_searches': 0}
Because I don't have search the python code return an error:
>>> myapi.search_url(url='http://en.es-static.us/upl/2019/03/rogue-planet-CFBD-S
IR-J214947.2-040308.9-2012-800x450.jpg')
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python373\lib\site-packages\pytineye\api.py", line 304, in search_url

    obj = self._request('search', params, **kwargs)
  File "C:\Python373\lib\site-packages\pytineye\api.py", line 276, in _request
    raise TinEyeAPIError(obj['code'], obj.get('messages'))
pytineye.exceptions.TinEyeAPIError: APIError:
                   code    = 401
                   message = ['AUTHORIZATION_ERROR', 'You have no more searches
available. Please purchase another bundle if you wish to make more searches.']
If you want to buy TinEye API searches then the prices start from:
5,000 searches $200.00 USD($0.04 per search)
10,000 searches $300.00 USD($0.03 per search)
50,000 searches $1,000.00 USD($0.02 per search)
1,000,000 searches $10,000.00 USD($0.01 per search)

Friday, April 5, 2019

First test with 3.7.3 and opencv-python module version 4.0.0 .

The Python 3.7.3 is the third maintenance release of Python 3.7 and is released at March 25, 2019.
More about this new released version can be found at official website.
C:\Python373\Scripts>pip install opencv-python
Collecting opencv-python
...
Installing collected packages: numpy, opencv-python
Successfully installed numpy-1.16.2 opencv-python-4.0.0.21
Let's test it:
C:\Python373>python.exe
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.0.0'
Let test a simple example that computes a dense optical flow using the Gunnar Farneback’s algorithm.
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
ret, frame1 = cap.read()
prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[...,1] = 255
while(1):
    ret, frame2 = cap.read()
    next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
    flow = cv2.calcOpticalFlowFarneback(prvs,next, None, 0.1, 1, 2, 3, 5, 11.2, 0)
    mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
    hsv[...,0] = ang*180/np.pi/2
    hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
    bgr = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
    cv2.imshow('frame2',bgr)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
    prvs = next
cap.release()
cv2.destroyAllWindows()