analitics

Pages

Showing posts with label 3.7. Show all posts
Showing posts with label 3.7. Show all posts

Thursday, May 30, 2019

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.


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