Showing posts with label module. Show all posts
Showing posts with label module. Show all posts

2/03/2014

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

---