'Python'에 해당되는 글 105건

  1. 2015.04.02 randomGenerator.py
  2. 2015.03.08 wirteObject.py
  3. 2015.03.06 mmapFork.py
  4. 2015.03.06 mmapTest.py
  5. 2015.03.06 xmlDom.py
  6. 2015.03.06 xmlSax.py
  7. 2015.03.06 threadingQueue.py
  8. 2015.03.06 threadingLock.py
  9. 2015.03.05 threadingEx.py
  10. 2015.03.05 threadEx.py
2015. 4. 2. 22:07
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import random

class CMWC(random.Random):
    'Long period random number generator: Complementary Multiply with Carry'
    # http://en.wikipedia.org/wiki/Multiply-with-carry

    a = 3636507990
    logb = 32
    b = 2 ** logb
    r = 1359

    def _gen_word(self):
        i = self.i
        xc, self.c = divmod(self.a * self.Q[i] + self.c, self.b)
        x = self.Q[i] = self.b - 1 - xc
        self.i = 0 if i + 1 == self.r else i + 1
        return x

    def getrandbits(self, k):
        while self.bits < k:
            self.f = (self.f << self.logb) | self._gen_word()
            self.bits += self.logb
        x = self.f & ((1 << k) - 1)
        self.f >>= k;  self.bits -= k
        return x

    def random(self, RECIP_BPF=random.RECIP_BPF, BPF=random.BPF):
        return self.getrandbits(BPF) * RECIP_BPF

    def seed(self, seed=None):
        seeder = random.Random(seed)
        Q = [seeder.randrange(0x100000000) for i in range(self.r)]
        c = seeder.randrange(0x100000000)
        self.setstate((0, 0, 0, c, Q))

    def getstate(self):
        return self.f, self.bits, self.i, self.c, tuple(self.Q)

    def setstate(self, (f, bits, i, c, Q)):
        self.f, self.bits, self.i, self.c, self.Q = f, bits, i, c, list(Q)


if __name__ == '__main__':
    prng = CMWC(134123413541344)
    for i in range(20):
        print prng.random()
    print
    for i in range(20):
        print random.normalvariate(mu=5.0, sigma=2.2)

'Python' 카테고리의 다른 글

dll injection in python2  (0) 2015.09.24
fileUpload.py  (0) 2015.04.13
threadingQueue.py  (0) 2015.03.06
threadEx.py  (0) 2015.03.05
cookie.py  (0) 2015.03.05
Posted by af334
2015. 3. 8. 04:04
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


file1=open('/python/email.py',"r")


while 1:
    s=file1.read()
  
    if not s :break
    print(s)
    repr(s)

    open('/python/newnew.py','w').write(s)
   
   

'Python > Python 3.x' 카테고리의 다른 글

threading  (0) 2015.05.08
unixCommand.py  (0) 2015.04.03
mmapFork.py  (0) 2015.03.06
mmapTest.py  (0) 2015.03.06
xmlDom.py  (0) 2015.03.06
Posted by af334
2015. 3. 6. 04:45
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import mmap
import os

mm=mmap.mmap(-1, 13)
mm.write(b"Hello world!")

pid=os.fork()

if pid==0: #In a child process
    mm.seek(0)
    print(mm.readline())

    mm.close()


'Python > Python 3.x' 카테고리의 다른 글

unixCommand.py  (0) 2015.04.03
wirteObject.py  (0) 2015.03.08
mmapTest.py  (0) 2015.03.06
xmlDom.py  (0) 2015.03.06
xmlSax.py  (0) 2015.03.06
Posted by af334
2015. 3. 6. 04:34
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import mmap

with open("/python/trials/mmapTest.txt","wb") as f:
    f.write(b"Hello Python!\n")
    print (f.fileno())

with open("/python/trials/mmapTest.txt","r+b") as f:
    #memory-map the file, size 0 means whole file
    mm=mmap.mmap(f.fileno(), 0)

    #read content via standard file methods
    print(mm.readline()) #prints b"Hello Python!\n"

    #read content via slice notation
    print(mm[:5]) #prints b"Hello"


    #update content using slice notation
    #note that new content must have same size
    mm[6:]=b" world!\n"


    #... and read again using standard file methods
    mm.seek(0)
    print(mm.readline()) #prints b"Hello world!\n"


    #close the map
    mm.close()


'Python > Python 3.x' 카테고리의 다른 글

wirteObject.py  (0) 2015.03.08
mmapFork.py  (0) 2015.03.06
xmlDom.py  (0) 2015.03.06
xmlSax.py  (0) 2015.03.06
threadingLock.py  (0) 2015.03.06
Posted by af334
2015. 3. 6. 02:00
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

from xml.dom.minidom import parse
import xml.dom.minidom

#open XML document using minidom parser
DOMTree=xml.dom.minidom.parse("movie.xml")
collection=DOMTree.documentElement
if collection.hasAttribute("shelf"):
    print("Root element : %s"%collection.getAttribute("shelf"))

#get all the movies in the collection
movies=collection.getElementsByTagName("movie")

#print detail of each movie
for movie in movies:
    print ("*****Movie*****")
    if movie.hasAttribute("title"):
        print ("Title: %s"%movie.getAttribute("title"))

    type=movie.getElementsByTagName('type')[0]
    print ("Type: %s" %type.childNodes[0].data)
   
    format=movie.getElementsByTagName('format')[0]
    print ("Format: %s"% format.childNodes[0].data)
   
    rating=movie.getElementsByTagName('rating')[0]
    print("Rating: %s"%rating.childNodes[0].data)

    description=movie.getElementsByTagName('description')[0]
    print("Description: %s"%description.childNodes[0].data)


'Python > Python 3.x' 카테고리의 다른 글

mmapFork.py  (0) 2015.03.06
mmapTest.py  (0) 2015.03.06
xmlSax.py  (0) 2015.03.06
threadingLock.py  (0) 2015.03.06
threadingEx.py  (0) 2015.03.05
Posted by af334
2015. 3. 6. 01:27
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import xml.sax

class MovieHandler(xml.sax.ContentHandler):
    def __init__(self):
        self.CurrentData=""
        self.type=""
        self.format=""
        self.year=""
        self.rating=""
        self.stars=""
        self.description=""

    #call when an element starts
    def startElement(self, tag, attributes):
        self.CurrentData=tag
        if tag=="movie":
            print ("*****Movie*****")
            title=attributes["title"]
            print ("Title:",title)

    #call when an elements ends
    def endElement(self,tag):
        if self.CurrentData=="type":
            print ("Type:", self.type)
        elif self.CurrentData=="format":
            print ("Format:", self.format)
        elif self.CurrentData=="year":
            print("Year:", self.year)
        elif self.CurrentData=="rating":
            print("Rating:", self.rating)
        elif self.CurrentData=="stars":
            print("Stars:",self.stars)
        elif self.CurrentData=="description":
            print("Description:", self.description)
        self.CurrentData=""

    #call when a character is read
    def characters(self,content):
        if self.CurrentData=="type":
            self.type=content
        elif self.CurrentData=="format":
            self.format=content
        elif self.CurrentData=="year":
            self.year=content
        elif self.CurrentData=="rating":
            self.rating=content
        elif self.CurrentData=="stars":
            self.stars=content
        elif self.CurrentData=="description":
            self.description=content

if __name__=="__main__":
    #create an XMLReader
    parser=xml.sax.make_parser()
    #turn off namespaces
    parser.setFeature(xml.sax.handler.feature_namespaces,0)

    #override the default ContextHandler
    Handler=MovieHandler()
    parser.setContentHandler(Handler)

    parser.parse("movie.xml")



======================


movie.xml


<collection shelf="New Arrivals">
<movie title="Enemy Behind">
   <type>War, Thriller</type>
   <format>DVD</format>
   <year>2003</year>
   <rating>PG</rating>
   <stars>10</stars>
   <description>Talk about a US-Japan war</description>
</movie>
<movie title="Transformers">
   <type>Anime, Science Fiction</type>
   <format>DVD</format>
   <year>1989</year>
   <rating>R</rating>
   <stars>8</stars>
   <description>A schientific fiction</description>
</movie>
   <movie title="Trigun">
   <type>Anime, Action</type>
   <format>DVD</format>
   <episodes>4</episodes>
   <rating>PG</rating>
   <stars>10</stars>
   <description>Vash the Stampede!</description>
</movie>
<movie title="Ishtar">
   <type>Comedy</type>
   <format>VHS</format>
   <rating>PG</rating>
   <stars>2</stars>
   <description>Viewable boredom</description>
</movie>
</collection>



'Python > Python 3.x' 카테고리의 다른 글

mmapTest.py  (0) 2015.03.06
xmlDom.py  (0) 2015.03.06
threadingLock.py  (0) 2015.03.06
threadingEx.py  (0) 2015.03.05
forInReadline.py  (0) 2015.03.05
Posted by af334
2015. 3. 6. 00:56
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import Queue
import threading
import time
exitFlag=0

class myThread(threading.Thread):
    def __init__(self, threadID, name,q):
        threading.Thread.__init__(self)
        self.threadID=threadID
        self.name=name
        self.q=q

    def run(self):
        print ("Starting "+self.name)
        process_data(self.name, self.q)
        print ("Exiting "+self.name)

def process_data(threadName, q):
    while not exitFlag:
        queueLock.acquire()
        if not workQueue.empty():
            data=q.get()
            queueLock.release()
            print ("%s processing %s"%(threadName, data))
        else:
            queueLock.release()
        time.sleep(1)

threadList=["Thread-1", "Thread-2", "Thread-3"]
nameList=["One","Two","Three","Four","Five"]
queueLock=threading.Lock()
workQueue=Queue.Queue()
threads=[]
threadID=1

#create new threads
for tName in threadList:
    thread=myThread(threadID, tName, workQueue)
    thread.start()
    threads.append(thread)
    threadID+=1

#Fill the queue
queueLock.acquire()
for word in nameList:
    workQueue.put(word)
queueLock.release()

#wait for queue to empty
while not workQueue.empty():
    pass

#notify threads it's time to exit
exitFlag=1

#wait for all threads to complete
for t in threads:
    t.join()
print("Exiting Main Thread")


'Python' 카테고리의 다른 글

fileUpload.py  (0) 2015.04.13
randomGenerator.py  (0) 2015.04.02
threadEx.py  (0) 2015.03.05
cookie.py  (0) 2015.03.05
fileReadline.py  (0) 2015.03.05
Posted by af334
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import threading
import time

exitFlag=0

class myThread(threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID=threadID
        self.name=name
        self.counter=counter

    def run(self):
        print ("Starting"+self.name)
        print_time(self.name, self.counter,5)
        print("Exiting "+self.name)

def print_time(threadName, delay, counter):
    while counter:
        if exitFlag:
            thread.exit()
        time.sleep(delay)
        print ("%s: %s"% (threadName, time.ctime(time.time())))
        counter-=1

#create new threads
thread1=myThread(1, "Thread-1",1)
thread2=myThread(2,"Thread-2",2)


#start new threads
thread1.start()
thread2.start()

print ("Exiting Main Thread")

'Python > Python 3.x' 카테고리의 다른 글

xmlDom.py  (0) 2015.03.06
xmlSax.py  (0) 2015.03.06
threadingEx.py  (0) 2015.03.05
forInReadline.py  (0) 2015.03.05
putSome.py  (0) 2015.03.05
Posted by af334
2015. 3. 5. 23:55
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import threading
import time

exitFlag=0

class myThread(threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID=threadID
        self.name=name
        self.counter=counter

    def run(self):
        print ("Starting"+self.name)
        print_time(self.name, self.counter,5)
        print("Exiting "+self.name)

def print_time(threadName, delay, counter):
    while counter:
        if exitFlag:
            thread.exit()
        time.sleep(delay)
        print ("%s: %s"% (threadName, time.ctime(time.time())))
        counter-=1

#create new threads
thread1=myThread(1, "Thread-1",1)
thread2=myThread(2,"Thread-2",2)


#start new threads
thread1.start()
thread2.start()

print ("Exiting Main Thread")

'Python > Python 3.x' 카테고리의 다른 글

xmlSax.py  (0) 2015.03.06
threadingLock.py  (0) 2015.03.06
forInReadline.py  (0) 2015.03.05
putSome.py  (0) 2015.03.05
withAsReadline.py  (0) 2015.03.05
Posted by af334
2015. 3. 5. 23:30
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import thread
import time

#Define a function for the thread
def print_time(threadName, delay):
    count=0
    while count <5:
        time.sleep(delay)
        count+=1
        print "%s: %s"%(threadName, time.ctime(time.time()))

#create two threads as follows
try:
    thread.start_new_thread(print_time, ("Thread-1",2,))
    thread.start_new_thread(print_time, ("Thread-2",4,))
except:
    print "Error: unable to start thread"

while 1:
    pass

   

'Python' 카테고리의 다른 글

randomGenerator.py  (0) 2015.04.02
threadingQueue.py  (0) 2015.03.06
cookie.py  (0) 2015.03.05
fileReadline.py  (0) 2015.03.05
SimpleServer.py  (0) 2015.03.04
Posted by af334