analitics

Pages

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()

Wednesday, April 3, 2019

About Ninja IDE for python programming.

This I.D.E. is a very good tool for python programming and development.
The version of this tool is 2.3.
I tested with my Django project on Windows OS and works great.
The development team comes with this info:
NINJA-IDE (from the recursive acronym: "Ninja-IDE Is Not Just Another IDE"), is a cross-platform integrated development environment (IDE). NINJA-IDE runs on Linux/X11, Mac OS X and Windows desktop operating systems, and allows developers to create applications for several purposes using all the tools and utilities of NINJA-IDE, making the task of writing software easier and more enjoyable.
The official webpage can be found here.
If you want to use it with Django the easy way is to install the plugin for this area.
You can see all about the development team about page.

Saturday, March 30, 2019

Testing the python IMDbPY module with simple commands.

Today we tested a more innovative but useful method with python aaa mode.
The main reason I used this method is the lack of documentation.
Using this method, we have reached elements related to the use of reported methods and errors.
The test was done on a Fedora 29 Linux system with a classic install with the pip utility:
[mythcat@desk ~]$ pip install imdbpy --user
Collecting imdbpy
...
Successfully installed SQLAlchemy-1.3.1 imdbpy-6.6 
I used an example of a person's search in the IMDB database to test this method.
>>> from imdb import IMDb, IMDbError
>>> try:
...     im=IMDb()
...     people = im.search_person('Mel Gibson')
... except IMDbError as exc:
...     print(exc) 
Using the dir and print function will show the resulting output configuration and will have the following form:
>>> print(people)
[, , , , , , , , , , , , , , , , , , , ] 
I have used the dir function for a relative view of the options we have:
>>> print(dir(people))
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', 
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', 
'__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', 
'__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', 
'__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 
'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> print(dir(people[0]))
['_Container__role', '__bool__', '__class__', '__contains__', '__deepcopy__', '__delattr__', 
'__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__getitem__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__module__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', 
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_additional_keys', '_clear', 
'_get_currentRole', '_get_roleID', '_getitem', '_image_key', '_init', '_reset', '_roleClass', 
'_roleIsPerson', '_set_currentRole', '_set_roleID', 'accessSystem', 'add_to_current_info', 
'append_item', 'asXML', 'billingPos', 'charactersRefs', 'clear', 'cmpFunct', 'copy', 'currentRole', 
'current_info', 'data', 'default_info', 'get', 'getAsXML', 'getID', 'get_charactersRefs', 
'get_current_info', 'get_fullsizeURL', 'get_namesRefs', 'get_titlesRefs', 'has_current_info', 
'has_key', 'infoset2keys', 'isSame', 'isSameName', 'isSamePerson', 'items', 'iteritems', 
'iterkeys', 'itervalues', 'key2infoset', 'keys', 'keys_alias', 'keys_tomodify', 'keys_tomodify_list', 
'modFunct', 'myID', 'myName', 'namesRefs', 'notes', 'personID', 'pop', 'popitem', 'reset', 'roleID', 
'set_current_info', 'set_data', 'set_item', 'set_mod_funct', 'set_name', 'setdefault', 'summary', 
'titlesRefs', 'update', 'update_charactersRefs', 'update_infoset_map', 'update_namesRefs', 
'update_titlesRefs', 'values']
Here are some simple examples of displaying using the print function to view content in output:

>>> print(people[0].values())
[u'Catalin', u'II', u'Catalin', u'Catalin (II)', u'Catalin (II)']
>>> print(people[0].data)
{u'name': u'Catalin', u'imdbIndex': u'II'}
>>> print(people[1].data.viewitems())
dict_items([(u'name', u'Moreno, Catalina Sandino')])
>>> print(people[1].data.values())
[u'Moreno, Catalina Sandino']
>>> print(people[0].getID())
2165704
>>> print(people[0].itervalues())
The built-in function iter takes an iterable object and returns an iterator.
>>> print(people[0].itervalues().next())
Catalin
>>> print(people[0].asXML()) 
The last line of code will return XML content.
This simple example simply illustrates how to access structured information through simple Python commands.

Saturday, March 23, 2019

Fix errors with the python errors encyclopedia.

Today I will present a website that I find very useful in learning and developing with the Python programming language.
This very short tutorial it is very useful for newcomers to get rid of all sorts of common questions about certain errors.
I encounter these types of errors when using stackoverflow account and can be a time consuming for most people who use it.
Try to read all about these errors on the python errors encyclopedia.

Wednesday, March 20, 2019

Using multiprocessing - a simple introduction.

The multiprocessing module was added to Python in version 2.6 and can be used with new python versions.
It was originally defined in PEP 371 by Jesse Noller and Richard Oudkerk.
The multiprocessing package also includes some APIs that are not in the threading module at all.
Python introduced the multiprocessing module to let us write parallel code.
You can use three basic classes: Process, Queue, and Lock, which will help you build a parallel program.
For more details, you can check the documentation of this python module.
Today, I will present in this tutorial two examples that highlight how to work with this python module.
The most basic approach is probably to use the Process class.
The Process class is very similar to the threading module’s Thread class.
It is also necessary to use the Manager because is responsible for coordinating shared information.
The source code is simple to understand and is commented on to see the intermediate steps.
Let's start with the first example that uses a function of adding two numbers in two processes.
import multiprocessing

def sum(a,b):
 t=a+b
 print ("sum is:",t)

if __name__ == '__main__':
 # create process processing_001 with target function and args of the target function
 processing_001 = multiprocessing.Process(target=sum, args=(7,6))
 processing_002 = multiprocessing.Process(target=sum, args=(1,1))
 # starting processing_001
 processing_001.start()
 # starting processing_002
 processing_002.start()
 #processes are started
 # stop processing_001 until is complete with join
 processing_001.join()
 # stop processing_002 until is complete with join
 processing_001.join()
 # result is C:\Python364>python.exe multiprocessing_001.py
 #sum is: 13
 #sum is: 2
The second example solves the next problem of the engineer who checks the speed of accomplishment of the tasks performed by two robots.
Three processes are created for each: robots and engineer.
All these processes are completed in the same dictionary resource using the Manager.
For each task performed by the robot (start/join processes), the robot finds a random execution rate (function random.random) that is then added to the result (result) with the robot name and execution speed.
Let's see the source code and the result:
import multiprocessing 
from multiprocessing import Process, Manager
import os
import random

def robot1(result):
 print("robot1 has ID process: {}".format(os.getpid()))
 speed_robot1=random.random()
 result["robot1"] = speed_robot1
 print ("worker speed tasks:",speed_robot1)
 #print (result)
 
def robot2(result):
 print("robot2 has ID process: {}".format(os.getpid()))
 speed_robot2=random.random()
 result["robot2"] = speed_robot2
 print ("worker speed tasks:",speed_robot2)
 #print (result)
 
def engineer(result):
 print("engineer has ID process: {}".format(os.getpid()))
 for key, value in result.items():
  print("Result: {}".format((key, value)))
 # you can add code to sort the values and print 
 # for key, value in sorted(result.items()):
 # print("Result: {}".format((key, value)))
 
if __name__ == '__main__':
 # show the main process
 print("the main ID process: {}". format(os.getpid()))
 # create a share dictionary resouce 
 manager = multiprocessing.Manager()
 result = manager.dict()
 # the process starts for  robot1
 ro1 = multiprocessing.Process(target=robot1, args=(result,))
 ro1.start()
 # the process starts for  robot2
 ro2 = multiprocessing.Process(target=robot2, args=(result,))
 ro2.start()
 # stops the robot1 process
 ro1.join()
 # stops the robot2 process
 ro2.join()
 # create the engineer process
 en = multiprocessing.Process(target=engineer, args=(result,))
 # start the process for the engineer
 en.start()
 # stops the engineer's process
 en.join()
The result is:
C:\Python364>python.exe multiprocessing_002.py
the main ID process: 7816
robot1 has ID process: 6476
worker speed tasks: 0.48279764083908094
robot2 has ID process: 7560
worker speed tasks: 0.44408591959008503
engineer has ID process: 7000
Result: ('robot1', 0.48279764083908094)
Result: ('robot2', 0.44408591959008503)

Sunday, March 17, 2019

Get bookmarks from your Firefox browser database.

This simple example tutorial is about reading the bookmarks from firefox database.
The database is an SQLite database.
You need to create a python file named: firefox_bookmarks.py.
Change your windows account on the bookmarks_path.
The script is simple to understand and comes with two functions: execute_query and get_bookmarks.
Follow the commented source code to understand how it's working the python script:
import os
import sqlite3

# execute a query on sqlite cursor
def execute_query(cursor, query):
    try:
        cursor.execute(query)
    except Exception as error:
        print(str(error) + "\n " + query)
# get bookmarks from firefox sqlite database file and print all
def get_bookmarks(cursor):
    bookmarks_query = """select url, moz_places.title, rev_host, frecency,
    last_visit_date from moz_places  join  \
    moz_bookmarks on moz_bookmarks.fk=moz_places.id where visit_count>0
    and moz_places.url  like 'http%'
    order by dateAdded desc;"""
    execute_query(cursor, bookmarks_query)
    for row in cursor:
        link = row[0]
        title = row[1]
        print(link,title)
# set the path of firefox folder with databases
bookmarks_path = "C:/Users/YOUR_WINDOWS_ACCOUNT/AppData/Roaming/Mozilla/Firefox/Profiles/"
# get firefox profile
profiles = [i for i in os.listdir(bookmarks_path) if i.endswith('.default')]
# get sqlite database of firefox bookmarks
sqlite_path = bookmarks_path+ profiles[0]+'/places.sqlite'
#
if os.path.exists(sqlite_path):
    firefox_connection = sqlite3.connect(sqlite_path)
cursor = firefox_connection.cursor()
get_bookmarks(cursor)
cursor.close()
The result of running the script comes with my bookmarks of Firefox:
C:\Python364>python.exe firefox_bookmarks.py
https://twitter.com/ Twitter. It's what's happening. 

Friday, March 15, 2019

Using Tornado - first steps...

About Tornado you can read at GitHub.
The basic info about this framework is this intro :
Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed. By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user.
C:\Python364>git clone https://github.com/facebook/tornado.git
Cloning into 'tornado'...
remote: Enumerating objects: 51, done.
remote: Counting objects: 100% (51/51), done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 22803 (delta 17), reused 51 (delta 17), pack-reused 22752
Receiving objects: 100% (22803/22803), 8.41 MiB | 2.18 MiB/s, done.
Resolving deltas: 100% (16735/16735), done.
Checking out files: 100% (302/302), done.

C:\Python364>cd tornado

C:\Python364\tornado>C:\Python364\python.exe setup.py install
running install
...
Processing dependencies for tornado==6.1.dev1
Finished processing dependencies for tornado==6.1.dev1
Use this demo chat to test it:
C:\Python364\tornado\demos\chat>C:\Python364\python.exe chatdemo.py
[I 190315 20:26:25 web:2162] 200 GET / (::1) 47.22ms
You can see the result into your browsers using http://localhost:8888
You can change port and address on this source code row with your IP address:
app.listen(options.port, '92.76.67.102')
The result is a chat example with Tornado framework.
The tornado comes with many demos for you, see all of this:
  • blog
  • chat
  • facebook
  • file_upload
  • helloworld
  • s3server
  • tcpecho
  • twitter
  • websocket
  • webspider

Friday, February 15, 2019

Install , test and fix error of the jupyter-book into python 3.

Jupyter Books lets you build an online book using a collection of Jupyter Notebooks and Markdown files. Its output is similar to the excellent Bookdown tool, and adds extra functionality for people running a Jupyter stack.
Read more about this on the official webpage.
Today I start to test this python module named jupyter-book.
I find some errors and I fixed to running well a demo jupyter-book instance.

C:\Python364\Scripts>pip install jupyter-book
Collecting jupyter-book
...
Installing collected packages: ruamel.yaml, jupyter-book
Successfully installed jupyter-book-0.4.1 ruamel.yaml-0.15.88
First I try to create a jupyter-book named catafest but I got this error:
C:\Python364>jupyter-book create catafest --demo
Traceback (most recent call last):
...
from nbclean import NotebookCleaner
ModuleNotFoundError: No module named 'nbclean'
The next step was to fix the error by install the nbclean python module and see all dependencies of python modules:
C:\Python364\Scripts>pip3.6.exe install nbclean
Collecting nbclean
...
Collecting nbgrader (from nbclean)
...
Collecting sqlalchemy (from nbgrader->nbclean)
...
Collecting alembic (from nbgrader->nbclean)
...
Collecting ipython<=6.2.1 (from nbgrader->nbclean)
...
Collecting jupyter-console<=5.2.0 (from nbgrader->nbclean)
...
Collecting Mako (from alembic->nbgrader->nbclean)
...
Collecting prompt-toolkit<2 .0.0="">=1.0.4 (from ipython<=6.2.1->nbgrader->nbclean)
...
Collecting prompt-toolkit<2 .0.0="">=1.0.4 (from ipython<=6.2.1->nbgrader->nbclean)
...
Building wheels for collected packages: nbgrader, sqlalchemy, alembic, Mako
...
Successfully built nbgrader sqlalchemy alembic Mako
Installing collected packages: sqlalchemy, Mako, python-editor, alembic, prompt-toolkit, ipython, 
jupyter-console, nbgrader, nbclean
...
Successfully installed Mako-1.0.7 alembic-1.0.7 ipython-6.2.1 jupyter-console-5.2.0 nbclean-0.3.2 
nbgrader-0.5.5 prompt-toolkit-1.0.15 python-editor-1.0.4 sqlalchemy-1.2.17 
Using again the jupyter-book to create catafest I got another error:
C:\Python364>jupyter-book create catafest--demo
Copying new book to: .\catafest
Copying over demo repository content
This is an error when I start to buid first time:
C:\Python364>jupyter-book build catafest
Convert and copy notebook/md files...
  0%|                                                                       | 0/35 [00:00
  File "c:\python364\lib\site-packages\jupyter_book\main.py", line 31, in main
    commands[args.command]()
  File "c:\python364\lib\site-packages\jupyter_book\build.py", line 266, in build_book
    lines = ff.readlines()
  File "c:\python364\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 183: character maps to  
The reason is the python 3.x and can be fixed by change the encoding into this file:
c:\python364\lib\site-packages\jupyter_book\build.py with this:
with open(path_new_file, 'r',encoding='utf-8') as ff:
The next step is to build the catafest jupyter-book:
C:\Python364>jupyter-book build catafest
Convert and copy notebook/md files...
... 
My GitHub account is catafest and I used this link to create a new repo named catafest_jupyter-book:
https://github.com/new
You need to use root folder use this commands:
C:\Python364\catafest>cd ..

C:\Python364>git clone https://github.com/catafest/catafest_jupyter-book
Cloning into 'catafest_jupyter-book'...
warning: You appear to have cloned an empty repository.
Copy all files and folders from catafest folder to catafest_jupyter-book folder and use GITHUB commands to upload to the web:
C:\Python364>cd catafest_jupyter-book

C:\Python364\catafest_jupyter-book>git add ./*

C:\Python364\catafest_jupyter-book>git commit -m "adding my first jupyter book!"

C:\Python364\catafest_jupyter-book>git push
Username for 'https://github.com': catafest
Password for 'https://catafest@github.com':
Enumerating objects: 347, done.
Counting objects: 100% (347/347), done.
Delta compression using up to 2 threads.
Compressing objects: 100% (304/304), done.
Writing objects: 100% (347/347), 1.40 MiB | 541.00 KiB/s, done.
Total 347 (delta 74), reused 0 (delta 0)
remote: Resolving deltas: 100% (74/74), done.
To https://github.com/catafest/catafest_jupyter-book
 * [new branch]      master -> master
You can make GITHUB settings with a new gh-pages branch to see into your browser.

Thursday, February 14, 2019

Using python with documents files.

Today I tested with python version 3.6.4 two python modules: python-docx and openpyxl.
This python modules let us to deal with document files like: docx, xlsx, xlsm, xltx, xltm.
First python module named python-docx is a Python library for creating and updating Microsoft Word (.docx) files.
The documentation of this python module can be found here.
Let's start with a simple example.
C:\Python364>cd Scripts
C:\Python364\Scripts>pip3.6.exe install python-docx
Collecting python-docx
...
Successfully installed python-docx-0.8.10
Let's start with the import step:
C:\Python364>python.exe
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import docx
>>> dir(docx)
['Document', 'ImagePart', 'RT', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '
__name__', '__package__', '__path__', '__spec__', '__version__', 'api', 'blkcntnr', 'compat', 'dml',
 'document', 'enum', 'exceptions', 'image', 'opc', 'oxml', 'package', 'parts', 'section', 'settings'
, 'shape', 'shared', 'styles', 'text']
Let's create a document with this python module and add some text and an image:
C:\Python364>python.exe
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import docx
>>> mydoc = docx.Document()
>>> mydoc.add_paragraph('This is a text')
>>> mydoc.add_picture('icon.png',width=docx.shared.Inches(1),height=docx.shared.Inches(1))
>>> mydoc.save('test.docx')
Another python module is openpyxl.
This python module let you to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.
C:\Python364\Scripts>pip3.6.exe install openpyxl
Collecting openpyxl
...
Successfully installed et-xmlfile-1.0.1 jdcal-1.4 openpyxl-2.6.0
Let's test it:
C:\Python364>python.exe
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from openpyxl import load_workbook
>>> w = load_workbook(filename='test.xlsx',read_only=True)
>>> print(w.sheetnames)
['Sheet1', 'TestSheet2']
>>> s=w['Sheet1']
>>> for row in s.rows:
...     for c in row:
...             print(c.value)
...
A1
None
ABC
None
None
NOP

Sunday, February 10, 2019

Using the asciimatics and pyfiglet python modules

This is a simple example how to use the asciimatics and pyfiglet python modules with python version 3.6.4.
First you need to install with the pip tool.
The source code is simple and start with the import it.
The Fire, Print and Screen is used to show the fire effect and print text with Figlet and FigletText.
Because the fire and text use the console application I used the default Screen Buffer Size set to 80.
The Screen.wrapper(my_work_web) show all effects from the my_work_web.
In this area is created variables for font type: banner_font and web_font.
The main reason I named the web_font was to show my web page but the size of the over the screensize.
I tested most of the fonts from pyfiglet python module but I cannot find one to show a web page link.
This is the source code I tested:
# -*- coding: utf-8 -*-
"""
@author: catafest
"""

from asciimatics.renderers import FigletText, Fire
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.effects import Print
from asciimatics.exceptions import ResizeScreenError
from pyfiglet import Figlet
import sys

def my_work_web(screen):
    banner_font = "banner3"
    web_font = "block"
    scenes = []
    effects = [
        Print(screen,
              Fire(screen.height, 80, "*" * 70, 0.8, 60, screen.colours,
                   bg=screen.colours >= 256),
              0,
              speed=1,
              transparent=False),
        Print(screen,
              FigletText("Follow ", banner_font),
              (screen.height - 4) // 2,
              colour=Screen.COLOUR_BLUE,
              speed=1,
              stop_frame=30),
        Print(screen,
              FigletText("me", banner_font),
              (screen.height - 4) // 2,
              colour=Screen.COLOUR_BLUE,
              speed=1,
              start_frame=30,
              stop_frame=50),
        Print(screen,
              FigletText("on web", banner_font),
              (screen.height - 4) // 2,
              colour=Screen.COLOUR_BLUE,
              speed=1,
              start_frame=50,
              stop_frame=70),
        Print(screen,
              FigletText("catafest", banner_font),
              (screen.height - 4) // 2,
              colour=Screen.COLOUR_BLUE,
              speed=1,
              start_frame=70),
    ]
    scenes.append(Scene(effects, 100))

    text = Figlet(font=web_font, width=300).renderText("bye!")
    width = max([len(x) for x in text.split("\n")])

    effects = [
        Print(screen,
              Fire(screen.height, 80, "*" * 70, 0.8, 60, screen.colours),
              0,
              speed=1,
              transparent=False),

        Print(screen,
              FigletText("bye!", web_font),
              (screen.height - 2)  // 2,
              colour=Screen.COLOUR_WHITE,
              bg=Screen.COLOUR_BLUE,
              speed=1)
    ]
    scenes.append(Scene(effects, -1))
    screen.play(scenes, stop_on_resize=True)


if __name__ == "__main__":
    while True:
        try:
            Screen.wrapper(my_work_web)
            sys.exit(0)
        except ResizeScreenError:
            pass
The result of this source code is this: