Coding/Python 삽질기2013. 12. 15. 04:02

Cython 설치 ( Mac )

  1. python download ( http://www.cython.org )
  2. sudo python setup.py install
Cython 실습

factorial_100000.pyx ]

def Factorial(int N):
    cdef int checkSize = 5000
    cdef int fi
    cdef int endN
    endN = N / checkSize
    facResult = 1
    for fi in range(1, endN + 1):
        facResult = facResult * reduce(lambda x, y : x * y, range((fi-1)*checkSize+1,fi * checkSize+1))
   
    if ( (endN * checkSize + 1) != (N+1) ):   
        facResult = facResult * reduce(lambda x, y : x * y, range(endN * checkSize + 1, N+1))  
    return facResult


[ setup.py ]

from distutils.core import setup

from Cython.Build import cythonize

setup(
  name = 'Factorial app',
  ext_modules = cythonize("factorial_100000.pyx"),


[ Build ]
python setup.py build_ext —inplace

# 이러면 SO 파일이 생김

[ Sandbox.py ]

import time

from factorial_100000 import Factorial

startTime = time.time()
factorialN = Factorial(10000)

print "runtime is %s"%(time.time() - startTime) 


[ 실행결과 ]



'Coding > Python 삽질기' 카테고리의 다른 글

[Notepad++] Python 실행하기  (0) 2016.08.28
Cython 설치  (3) 2013.12.15
100000! ( 100000 팩토리얼 ) & 1000000! ( 1000000 팩토리얼 )  (0) 2013.06.22
fibonacci 수열  (0) 2013.06.20
Posted by chobocho

댓글을 달아 주세요

  1. boo

    Cython정보 감사합니다.
    cython을 설치를 하고 싶은데요. 기본 pyhon은 설치가 안되는 상태구요. maya라는 프로그램에 python이 내장되어있는 상태입니다.
    여기서 cython을 설치하고 싶은데요.
    처음설치해 보는 것이라서 어디서 부터 해야하는지 모르겠는데.
    시작을 어떻게 해야할까요?

    2016.04.23 09:14 [ ADDR : EDIT/ DEL : REPLY ]
    • 안녕하세요. 저도 Maya에서 Cython 설치를 해본적이 없어서, 도움을 드리기가 어렵습니다.

      2016.04.28 03:51 신고 [ ADDR : EDIT/ DEL ]
  2. boo

    답글 해주셔서 감사합니다.

    2016.04.30 12:10 [ ADDR : EDIT/ DEL : REPLY ]

Coding/Python 삽질기2011. 6. 16. 01:17
#-*- coding: cp949 -*-
#
# undo/redo 구현 방법
# 1) undo stack를 만든다.
# 2) redo stack를 만든다.
# 3) 새로운 액션을 undo에 넣는다.
# 4) 사용자가 undo를 선택하면 redo.append ( undo.pop() ) 를 수행한다.
# 5) 사용자가 redo를 선택하면 undo.append ( redo.pop() ) 를 수행한다.

undo = []
redo = []

undo.append (1)
undo.append (2)
undo.append (3)
redo.append ( undo.pop() )


print undo
print redo
Posted by chobocho
TAG Python, Stack

댓글을 달아 주세요

Coding/Python 삽질기2011. 1. 12. 00:24
#-*- coding: cp949 -*-
# Name        : py_patten.py
# Author      : chobocho.com
# Version     :
# Copyright   :
# Description : simple number change 
#

from Tkinter import *
import tkMessageBox
 
save_file_name = "test.txt"
names = [ 'number1', 'number2' ]
table  = { names[0] : '100', names[1] : '200' } 

text_data = """Trees are  %(number1)s 
won,  %(number2)s won!""" 
 
class App:
    def __init__ (self, master):
    
        self.target_file_name = save_file_name
        
        frame = Frame(master)
        frame.pack()
        
        f3 = Frame(frame, width = 100, height = 40)
        f3.grid(row = 0, column = 0)
        
        self.label_1 = Label(f3, text= names[0])
        self.label_1.grid(row = 0, column = 0, sticky = W) 
        self.input_1 = Entry(f3)
        self.input_1.grid(row = 0, column = 1)

        self.label_2 = Label(f3, text= names[1])
        self.label_2.grid(row = 1, column = 0, sticky = W)
        self.input_2 = Entry(f3)
        self.input_2.grid(row = 1, column = 1)

        # 버튼을 그려주는 부분
        f4 = Frame(frame, width = 100, height = 40)
        f4.grid(row = 1, column = 0)
        

        self.info_button = Button(f4, text="Make", command = self.MakeFile)
        self.info_button.grid(row = 0, column = 0)
        self.info_button = Button(f4, text="Info", command = self.ShowInfo)
        self.info_button.grid(row = 0, column = 1)

    def ShowInfo(self):
        tkMessageBox.showinfo("Information","http://chobocho.com\nVersion 0.2")

    def MakeFile(self):
        self.value1 = self.input_1.get()
        self.value2 = self.input_2.get() 
        
        table[ names[0] ] = self.value1
        table[ names[1] ] = self.value2
        
        self.data = text_data % table

        target = file(self.target_file_name, 'w')
        target.write(self.data)
        target.close()

               
#----------------------------------------------------------
# main
if __name__ == "__main__":      
    root = Tk()
    app = App(root)
    root.mainloop()

'Coding > Python 삽질기' 카테고리의 다른 글

[Python] Undo / Redo 구현하기  (0) 2011.06.16
간단한 반복 작업 매크로  (0) 2011.01.12
http://www.pythonchallenge.com...Level6  (0) 2010.12.13
http://www.pythonchallenge.com...Level5  (0) 2010.12.13
Posted by chobocho
TAG macro, Python

댓글을 달아 주세요

Coding/Python 삽질기2010. 12. 13. 01:29


 

import re
import zipfile


def main():

    target = zipfile.ZipFile("channel.zip", 'r')
       
    number = "90052"
    filename = number + ".txt"
    comment = ""

    while 1:   
        print filename
        data = target.read(filename)
        data_ = re.findall("nothing is ([0-9]+)", data)
        if data_:
            comment += target.getinfo(filename).comment
            filename = "".join(data_) + ".txt"
        else:
            break
       
        print comment
         
if __name__ == "__main__":
    main()


Posted by chobocho
TAG Python

댓글을 달아 주세요

Coding/Python 삽질기2010. 12. 13. 00:37


 

import urllib
import re


def main():

    url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
   
    url_to = url + "12345"
    #url_to = url + "46059" # After get new messages.
   
    while 1:
        source = urllib.urlopen(url_to)
        data = source.read()
        number = re.findall("nothing is ([0-9]+)", data)
        if number:
           url_to = url + "".join(number)
           print url_to
        else:
           break
       
    print data
   
   
if __name__ == "__main__":
    main()

Posted by chobocho
TAG Python

댓글을 달아 주세요

Coding/Python 삽질기2010. 12. 12. 23:11


 

import re

data_ = """_abaAAAbCCC"""

def main():
    p = re.compile ('[a-z]{1}[A-Z]{3}[a-z]{1}[A-Z]{3}[a-z]{1}')
    m = p.findall(data_)
   
    if m:
        print m
        for idx in m:
            print idx[4],
   
   
if __name__ == "__main__":
    main()

Posted by chobocho
TAG Python

댓글을 달아 주세요

Coding/Python 삽질기2010. 12. 6. 23:34
data='''
find at source code...
'''

def main():
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    result = filter(lambda x : x in alphabet, data)
   
    print result
   
if __name__ == "__main__":
    main()

'Coding > Python 삽질기' 카테고리의 다른 글

http://www.pythonchallenge.com...Level3  (0) 2010.12.12
http://www.pythonchallenge.com...Level2  (0) 2010.12.06
[Python] 파일 zip으로 압축하기  (0) 2010.10.25
1000!  (0) 2010.07.14
Posted by chobocho
TAG Python

댓글을 달아 주세요

Coding/Python 삽질기2010. 10. 25. 00:25

#-*- coding: cp949 -*-
# 파일 명을 받아서 압축해 주는 스크립트

import os
import sys
import glob
import zipfile


def main(files):
    file_number = len(files)
    target_file = files[1];
        
    target = zipfile.ZipFile(target_file, 'w')
 
    for idx in range (2, file_number):
        all_flist = glob.glob (files[idx])
        for f in all_flist:
            if os.path.isdir(f):
                pass
            target.write(f)
   
    target.close()
  

#-------------------------------------------------------------------------------
# main
if __name__ == "__main__": 
    if len(sys.argv) < 3:
        print "Usage : pyzip source1 [source2]*"
    else:
        main(sys.argv)


'Coding > Python 삽질기' 카테고리의 다른 글

http://www.pythonchallenge.com...Level2  (0) 2010.12.06
[Python] 파일 zip으로 압축하기  (0) 2010.10.25
1000!  (0) 2010.07.14
Self number ... 2  (0) 2010.07.13
Posted by chobocho

댓글을 달아 주세요

Coding/Python 삽질기2010. 7. 14. 23:15
1000! 의 값을 보여주는 코드

print reduce(lambda x, y : x * y, range(1, 1001))

402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


'Coding > Python 삽질기' 카테고리의 다른 글

[Python] 파일 zip으로 압축하기  (0) 2010.10.25
1000!  (0) 2010.07.14
Self number ... 2  (0) 2010.07.13
Self number  (1) 2010.07.10
Posted by chobocho

댓글을 달아 주세요

Coding/Python 삽질기2010. 7. 13. 23:10
1 부터 5000 사이의 Self number 개수와 그 합을 구하는 코드를 3줄로 줄여봤다.
self_number = set(range(1, 5001)) - set([sum( map(int, str(idx))) + idx for idx in range(1, 5001)])

print len(self_number)
print sum(self_number)
494
1227365

'Coding > Python 삽질기' 카테고리의 다른 글

1000!  (0) 2010.07.14
Self number ... 2  (0) 2010.07.13
Self number  (1) 2010.07.10
몬테카를로 법을 이용한 원주율 계산  (0) 2010.07.09
Posted by chobocho

댓글을 달아 주세요