3/18/2014

Python Study, put_nowait(), get_nowait(), example source code

#put_nowait, get_nowait method


import queue
q = queue.Queue(2) #buffer size is 2
q.put("apple")
q.put("banana")
#q.put("orange")  # wait infinity until there is available space


#q.put_nowait("orange") #error occurs
# Line 187, in put_nowait return self.put(item, block=False)
# line 133, in put raise Full


q.get_nowait()
q.get_nowait()
#q.get_nowait() #error occurs
#line 195, in get_nowait return self.get(block=False)
#line 164, in get raise Empty


q.put("AA", True, 5);
q.put("AA", True, 5);
#q.put("AA", True, 5);  # wait 5 second until there is available space, if no space after 5 second, error occur



Python Study, queue, stack, priority queue, example source code

import queue

def GetItemList(q):
    ret = []
    n = q.qsize()
    while n>0:
        ret.append(q.get())
        n -= 1
    return ret

l = 'apple,banana,orange'


#queue test
q = queue.Queue()
for x in l.split(','):
    print(x)
    q.put(x)

print( GetItemList(q) )


#stack case
#LIFO last in first out
q = queue.LifoQueue()
for x in l.split(','):
    q.put(x)
print( GetItemList(q) )


#priorityQueue
q = queue.PriorityQueue()
q.put((5,"apple"))
q.put((10,"banana"))
q.put((1,"orange"))
print( GetItemList(q) )

"""
out put is...
['apple', 'banana', 'orange']
['orange', 'banana', 'apple']
[(1, 'orange'), (5, 'apple'), (10, 'banana')]
"""

Pyton study, Queue example source code

import queue

q = queue.Queue()
q.put("apple")
q.put("banana")
q.put(10)
print( q.qsize() )
print( q.get() )
print( q.get() )
print( q.qsize() )

"""
output is...

3
apple
banana
1

"""

Python Study, multiprocessing example source code

__author__ = 'mare'


from multiprocessing import Process, Lock, Value
import time


def run(name, l, c):
    print( name, 'process is created.')
    fixed = 0
    while 1:

        l.acquire()

        if c.value>0:
            c.value -= 1
            l.release()
            fixed += 1
            time.sleep(0.1)
        else:
            l.release()
            print(name, 'fixed', fixed, 'defects')
            break



if __name__=='__main__':
    lock = Lock()
    count = Value('i', 10)



dev_list = []
for name in ['Mare1', 'Mare2', 'Mare3']:
    dev = Process( target=run, args=(name, lock, count))
    dev_list.append(dev)
    dev.start()

for dev in dev_list:
    dev.join()
    print('All processes are finished.')





#out put is..
"""
Mare1 process is created.
Mare2 process is created.
Mare3 process is created.
Mare2 fixed 3 defects
Mare3 fixed 3 defects
Mare1 fixed 4 defects
All processes are finished.
All processes are finished.
All processes are finished.
"""

Python Study, Threading example source code

#Thread.start() //thread start
#Thread.run()   //thread main function run
#Thread.join([timeout]) //wait until thread end


from threading import Thread, Lock
import time

count = 10
lock = Lock()



class developer(Thread):

    def __init__(self, name):   #initialize
        Thread.__init__(self)
        self.name = name
        self.fixed = 0

    def run(self):              #thread main function


        global count
        while 1:
            #lock.acquire()      #lock  -> Untie comment, error occurs, I don't know why error occurs...
            if count > 0:
                count -= 1
                #lock.rlease()   #unlock -> Untie comment, error occurs, I don't know why error occurs...
                self.fixed += 1
                time.sleep(0.1)
            else:
                #lock.release()  #unlock -> Untie comment, error occurs, I don't know why error occurs...
                break




dev_list = []
for name in ['Mare1', 'Mare2', 'Mare3']:
    dev = developer(name)       #create thread
    dev_list.append(dev)
    dev.start()                 #thread start


for dev in dev_list:
    dev.join()                  #wait
    print(dev.name, 'fixed', dev.fixed)


#Mare1 fixed 3
#Mare2 fixed 4
#Mare3 fixed 3

3/17/2014

OpenCV Study, cvResize function

float percent = 50.0;
IplImage *source = cvLoadImage("bigImage.jpg");
IplImage *destination = cvCreateImage( cvSize((int)((source->width*percent)/100) , (int)((source->height*percent)/100) ),source->depth, source->nChannels );

//use cvResize to resize source to a destination image
cvResize(source, destination);

// save image with a name supplied with a second argument
cvSaveImage( "resizeImage.jpg", destination );



3/16/2014

Python study, sys modules




import sys

print(sys.prefix)
#/Library/Frameworks/Python.framework/Versions/3.3

print(sys.base_exec_prefix)
#/Library/Frameworks/Python.framework/Versions/3.3

#sys.exit([arg]) #process exit

t="test refcount"
print(sys.getrefcount(t))
#4
t1 = t
print(sys.getrefcount(t))
#5


#print(sys.getwindowsversion())
#sys.moudles

print(sys.path)
#['/Users/mare/PycharmProjects/sys_functions', '/Users/mare/PycharmProjects/sys_functions', '/Library/Frameworks/Python.framework/Versions/3.3/lib/python33.zip', '/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3', '/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages']

print( sys.copyright )
"""
Copyright (c) 2001-2013 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
"""

print("====")
print( sys.version )
#3.3.3 (v3.3.3:c3896275c0f6, Nov 16 2013, 23:39:35)
#[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]


print(sys.getdefaultencoding())
#utf-8

print( sys.stdout.write("hhh") )
print( sys.stderr.write("eee") )

#sys.argv
#sys.exc_info()