(Python Study) about Module (example source code)

The reason to use module is like that
Concise code
namespace differently
fast execution of a function

The module in python is similar with #include in C

To make module, make .py file.


...
"""
import math
print( math.pow(2, 10) )
print( math.log(100) )
"""


#make user module
from functools import *
#bring functools module to use reduce function

def intersect(*ar):
 return reduce( __intersectSC, ar )

def __intersectSC( listX, listY):
 setList = []

 for x in listX:
  if x in listY:
   setList.append(x)
 return setList

def difference(*ar):
 setList = []
 intersectSet = intersect(*ar)
 unionSet = union(*ar)

 for x in unionSet:
  if not x in intersectSet:
   setList.append(x)
 return setList

def union(*ar):
 setList = []
 for item in ar:
  for x in item:
   if not x in setList:
    setList.append(x)
 return setList

#save *.py and copy to python32\lib directory
#and bring this source code using import command
---

and copy this file to lib folder.
In my case lib is located in "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3"
Additionally, my computer is mac.


And import this file. 

...
import simpleset

print( dir(simpleset) )

setA = [1, 3, 7, 10]
setB = [2, 3, 4, 9]

print( simpleset.union(setA, setB) )
print( simpleset.intersect(setA, setB, [1, 2, 3] ))
#[1, 3, 7, 10, 2, 4, 9]
#[3]

---



Comments

Popular posts from this blog

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

OpenCV Stitching example (Stitcher class, Panorama)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

Optical Flow sample source code using OpenCV

OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

SICK LMS511 sensor data acquisition interface (source code, C++/MFC)

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Image warping (using opencv findHomography, warpPerspective)