小物

Python一行プログラミング・素数編

Pythonで一行で素数のリストを出力します。一行で。
map(lambda A:filter(lambda x:x, [i not in [j*k for j in A for k in A] and i for i in A] ), [range(2,100)])[0]
100って所をお好きな値に変えて下さい。もっときれいなご回答もお待ちしてます。
改良。
map(lambda A:filter(lambda x: x not in [j*k for j in A for k in A] and x, A), [range(2,100)])[0]
高速版。
map(lambda x: map(lambda B:filter(lambda x:x, [i not in B and i for i in range(2, x)]), [[j*k for j in range(2,int(x**0.5)+1) for k in range(2,x/j+1)]]), [1000])[0] 
てかよく考えたらセミコロン使えるし…醒めた。
X=10000;A=range(2,X);_=[j*k in A and A.remove(j*k) for j in range(2,int(X**0.5)+1) for k in range(j,X/j+1)];A
てか素直にはこうか。
X=10;A=range(2,X);_=[i%j==0 and i in A and A.remove(i) for i in A for j in range(2,min(i-1,int(X**0.5)))];A

ちょっとだけ解説。基本はこれ。
A=range(2,100)
for i in A:
  if i not in [j*k for j in A for k in A]:
    print i
[j*k for j in A for k in A]ってとこに合成数のリストが入ってます。それに入ってなかったら素数。
A=range(2,100)
[i not in [j*k for j in A for k in A] and i for i in A]
これで、合成数にはFalseが、素数にはその値が入った配列になる。([2, 3, False, 5, False, 7]みたいな)
A=range(2,100)
filter(lambda x:x, [i not in [j*k for j in A for k in A] and i for i in A] )
これで、合成数のFalseが消えて、素数だけの配列になる。([2, 3, 5, 7]みたいな)
map(lambda A:filter(lambda x:x, [i not in [j*k for j in A for k in A] and i for i in A] ), [range(2,100)])[0]
これで変数Aの割り当ても一行に入れてしまった。

ファイル転送スクリプト

transfer.pyという名前で保存して、 として使えます。
#!/usr/bin/env python

import errno,os,random,re,select,socket,string,sys,time,struct,stat,threading

dbg=2

def Ws(s):
    sys.stdout.write(s)
    sys.stdout.flush()

def Es(s):
    sys.stderr.write(s)
    sys.stderr.flush()


class Socket:
    """
       A smart socket with sendInt and sendString
       
    """
    
    def __init__(self, sock=None, peer=None):
        """ Create a socket """
        """  peer : (hostname, port) """
        if sock is None:
            self.sock = socket.socket(\
              socket.AF_INET, socket.SOCK_STREAM)
        else:
            self.sock = sock
        self.peer = peer

    
    def connect(self, peer):
        """ Establish a connection """
        assert peer is not None
        self.peer = peer
        self.sock.connect(self.peer)
        if self.sock is None:
            raise "Could not establish a connection to %s:%d"%(self.peer[0], self.peer[1])
        if dbg >= 2:
            Es("New connection to %s:%d\n"%(self.peer[0], self.peer[1]))

        
    def close(self):
        """ Close the socket """
        self.sock.close()

        
    def send(self, msg, msglen = -1):
        """ Safe send() """
        if msglen == -1: 
            msglen = len(msg)
        totalsent = 0
        while totalsent < msglen:
            #t0 = time.time()
            sent = self.sock.send(msg[totalsent:])
            #t1 = time.time()
            
            if sent == 0:
                raise RuntimeError, \
                      "socket connection broken"
            totalsent = totalsent + sent


    def recv(self, msglen):
        """ Safe recv() """
        msg = ''
        while len(msg) < msglen:
            chunk = self.sock.recv(msglen-len(msg))
            if chunk == '':
                raise RuntimeError, \
                    "socket connection broken"
            msg = msg + chunk
        return msg


    def rawrecv(self, msglen):
        try:
            return self.sock.recv(msglen)
        except:
            print msglen
            sys.exit(1)

    def sendInt(self, value):
        self.send(struct.pack('i', value))

    def recvInt(self):
        msg = self.recv(struct.calcsize('i'))
        return struct.unpack('i', msg)[0]
    




class ServerSocket:
    def __init__(self, initial_port=10000):
        """ Create a socket """
        """  peer : (hostname, port) """
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        #self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        for self.port in range(initial_port, 65535):
            try:
                self.sock.bind((socket.gethostbyname(socket.gethostname()), self.port))
            except socket.error, msg:
                Es("Err: on bind(): %s\n"%msg)
                continue
            break
        if self.port == 65534:
            raise Exception("Failed to bind any port")
        self.sock.listen(5) # arg : number of backlog

        
    def accept(self):
        (conn,peer) = self.sock.accept()
        if dbg >= 2 :
            Es("Accepted from %s:%d\n"%(peer[0], peer[1]))
        return Socket(sock=conn, peer=peer)

    def close(self):
        self.sock.close()


class SyncQueue:
    """
        Synchronized queue
    """
    
    def __init__(self):
        self.list = []
        self.cond = threading.Condition()

    def dequeue(self):
        self.cond.acquire()
        while len(self.list) == 0:
            self.cond.wait()
        item = self.list.pop(0)
        self.cond.release()
        return item


    def enqueue(self, item):
        self.cond.acquire()
        self.list.append(item)
        self.cond.notify()
        self.cond.release()


    def isEmpty(self):
        return len(self.list) == 0



#------------------------------------------------------------------#


def send(initial_port, sync_q, filesize):
    ss = ServerSocket(initial_port)
    print "PORT: %s"%(ss.port)
    sock = ss.accept()
    ss.close()

    t0 = time.time()
    sock.sendInt(filesize)
    # Ready to start transfer
    sent = 0
    while sent < filesize:
        buf = sync_q.dequeue()
        if len(buf) == 0:
            Es("Err: pkt size is zero\n")
            sys.exit(1)
        sock.send(buf)
        sent += len(buf)
    sock.close()
    assert sent == filesize
    assert sync_q.isEmpty()
    t1 = time.time()    
    if dbg >= 1:
        bps = float(filesize)/float(t1-t0) * 8
        Es("t = %5.3f sec, %5.3f Mbps\n"%((t1-t0), bps/1024/1024))


def recv(host, initial_port, sync_q, pkt_size=1024 * 1024):
    sock = Socket()
    sock.connect(peer=(host, initial_port))
    filesize = sock.recvInt()
    if dbg >= 1:
        Es("Filesize: %d\n"%(filesize))
    rcvd = 0
    while rcvd < filesize:
        s = filesize - rcvd
        if s > pkt_size: s = pkt_size
        buf = sock.rawrecv(s)
        if len(buf) == 0:
            Es("Connection reset by peer\n")
            sys.exit(1)
        rcvd += len(buf)
        sync_q.enqueue(buf)
    sync_q.enqueue("")
    if dbg >= 1:
        Es("Received every packet\n")

    assert rcvd == filesize


def readfile(filepath, sync_q, filesize, pkt_size=1024 * 1024):
    """ Read a file, enqueue data to the Q (Do not enqueue empty buffer)"""
    fp = open(filepath, "r")
    readsize = 0
    while readsize < filesize:
        buf = fp.read(pkt_size)
        readsize += len(buf)
        if len(buf) == 0: break
        sync_q.enqueue(buf)
    fp.close()
    assert readsize == filesize


def writefile(filepath, sync_q):
    """ Dequeue data from the Q, write it on a file (use empty buffer as the end marker)"""
    fp = open(filepath, "w")
    while True:
        buf = sync_q.dequeue()
        if len(buf) == 0: break
        buf = fp.write(buf)
    fp.close()


#-----
def sendfile(filepath, initial_port):
    sync_q = SyncQueue()
    filesize = os.stat(filepath)[stat.ST_SIZE]
    if dbg >= 1:
        Es("Filepath: %s, size: %s\n"%(filepath, filesize))
    threads = [ threading.Thread(target=readfile, args=(filepath, sync_q, filesize)),
                threading.Thread(target=send, args=(initial_port, sync_q, filesize)) ]

    for th in threads:
        th.start()
        
    for th in threads:
        th.join()

    if dbg >= 2:
        Es("All threads has exited\n")


def recvfile(filepath, host, initial_port):
    sync_q = SyncQueue()
    
    threads = [ threading.Thread(target=writefile, args=(filepath, sync_q)),
                threading.Thread(target=recv, args=(host, initial_port, sync_q)) ]

    for th in threads:
        th.setDaemon(True)
        th.start()
        
    for th in threads:
        th.join()



def main():
    initial_port = 10000
    if len(sys.argv) == 3:
        hn,port = sys.argv[1].split(":")
        port = int(port)
        host = socket.gethostbyname(hn)
        filepath = sys.argv[2]
        recvfile(filepath, host, port)
    
    elif len(sys.argv) == 2:
        filepath = sys.argv[1]
        sendfile(filepath, initial_port)

main()

バンド幅測定スクリプト

#!/usr/bin/env python

import socket
import threading
import struct

import os, sys, time, stat

NOISY = True

## new socket
class Socket:
    """
       A smart socket with sendInt and sendString
       
    """
    
    def __init__(self, sock=None, me=None, peer=None):
        """ Create a socket """
        """  me   : (hostname, port) """
        """  peer : (hostname, port) """
        if sock is None:
            self.sock = socket.socket(\
              socket.AF_INET, socket.SOCK_STREAM)
        else:
            self.sock = sock
        self.peer = peer
        self.me = me

    
    def connect(self, peer = None):
        """ Establish a connection """
        if peer is not None : self.peer = peer
        assert self.peer is not None
        self.sock.connect(self.peer)
        #        if sock is None:
        #            raise "Could not establish a connection to %s:%d"%(self.peer[0], self.peer[1])
        #        if NOISY :
        print "New connection to %s:%d"%(self.peer[0], self.peer[1])

        
        
    def close(self):
        """ Close the socket """
        self.sock.close()

        
    def send(self, msg, msglen = -1):
        """ Safe send() """
        
        if msglen == -1: 
            msglen = len(msg)
        totalsent = 0
        while totalsent < msglen:
            sent = self.sock.send(msg[totalsent:])
            if sent == 0:
                raise RuntimeError, \
                      "socket connection broken"
            totalsent = totalsent + sent


    def recv(self, msglen):
        """ Safe recv() """
        msg = ''
        while len(msg) < msglen:
            chunk = self.sock.recv(msglen-len(msg))
            if chunk == '':
                raise RuntimeError, \
                    "socket connection broken"
            msg = msg + chunk
        return msg


    def sendInt(self, value):
        self.send(struct.pack('i', value))


    def recvInt(self):
        msg = self.recv(struct.calcsize('i'))
        return struct.unpack('i', msg)[0]
    

    def sendString(self, msg, length = -1):
        if length == -1: length = len(msg)
        self.sendInt(length)
        self.send(msg, length)

        
    def recvString(self):
        msglen = self.recvInt()
        return (self.recv(msglen))


    def createServerSocket(self):
        """ Create a server socket """
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind(self.me)
        self.sock.listen(5) # arg : number of backlog

        
    def accept(self):
        (conn,peer) = self.sock.accept()
        if NOISY :
            print "Accepted from %s:%d"%(peer[0], peer[1])
        return Socket(sock=conn, peer=peer)
    


def socket_test(src, port, fn):
    pkt_size = 1024
    """ Test function for commons.Soecket """
    def server_test(me, fn):
        sock = Socket(me=me)
        sock.createServerSocket()
        s = sock.accept()

        t0 = time.time()
        fp = open(fn, "rb")
        fs = os.stat(fn)[stat.ST_SIZE]

        s.sendInt(fs)
        read = 0
        while True:
            X = fp.read(pkt_size)
            s.send(X)
            read += len(X)
            if read == fs: break
        fp.close()
        t1 = time.time()
        #print "Transfer time: %d sec"%(t1-t0)
        sz_mb = (float(fs)/1024 / 1024)
        t = (t1-t0)
        bw = sz_mb/(t1-t0) * 8
        print "%8.1f MB / %4.1f sec = %8.1f Mb/sec sec"%(sz_mb, t, bw)
        
        
    def client_test(peer, fn):
        s = Socket(peer = peer)
        s.connect()
        t0 = time.time()
        fs = s.recvInt()

        fp = open(fn, "wb")
        rcvd = 0
        while True:
            X = s.sock.recv(pkt_size)
            assert len(X) != 0
            rcvd += len(X)
            fp.write(X)
            if rcvd == fs: break
        fp.close()
        t1 = time.time()
        s.close()
        assert rcvd == fs
        sz_mb = (float(fs)/1024 / 1024)
        t = (t1-t0)
        bw = sz_mb/(t1-t0)*8
        print "%4.1f MB /  %6.1f sec = %8.1f Mb/sec sec"%(sz_mb, t, bw)


    me = (socket.gethostbyname(socket.gethostname()), port)
    if me[0] == src:
        server_test(me, fn)
    else :
        client_test((src, port), fn)



if len(sys.argv) != 4:
    print "Usage: %s FROMHOST PORT FILENAME"%(sys.argv[0])
    sys.exit(1)

src = socket.gethostbyname(sys.argv[1])
port = int(sys.argv[2])
fn = sys.argv[3]
socket_test(src, port, fn)


色んなサイズのファイルを創る

#!/usr/bin/env python

import os, sys, time, stat

NOISY = True

fns = {100:"100M", 200:"200M", 1024:"1G"}


for key in fns:
    size = key * 1024 * 1024
    fn = fns[key]
    fp = open(fn, "w")
    flagment = "01234567" * 1024
    for i in range(0, size/len(flagment)):
        fp.write(flagment)
    fp.close()
  • class GxpPtn:
        def __init__(self, ptn_str):
            self.ptns = []
            self.values = []
            for s in ptn_str.split():
                ptn_s,value = string.split(s, ":", 1)
                ptn = mk_host_regexp(ptn_s)
                self.ptns.append(ptn)
                self.values.append(value)
    
    
        def get(self, s):
            for i,ptn in enumerate(self.ptns):
                if ptn.match(s):
                    return self.values[i]
    
    
    def mk_host_regexp(p):
        # gxp like istbs** --> istbs.*
        # quote '.'
        p1 = string.replace(p,   ".", "\.")
        # ** matches everytihng including '.'
        p2 = string.replace(p1, "**", r".+")
        # * matches everytihng except '.'
        p3 = string.replace(p2, "*", r"[^\.]+")
        # ? matches a character except '.'
        p4 = string.replace(p3, "?", r"[^\.]")
        #
        p5 = string.replace(p4, "+", r"*")
        # return re.compile(p5 + "$")
        return re.compile(p5)
    
    
    gp = GxpPtn(ptn)
    
  • 全部のファイルツリーに対してなんかする

    #!/usr/bin/env python3
    
    import os,sys
    
    
    def process_file(orig, dest):
        なんかする
    
    def process(orig, dest):
        if os.path.isdir(orig):
            for f in os.listdir(orig):
                process(os.path.join(orig, f), os.path.join(dest, f))
        elif orig.endswith(".csv"):
            os.makedirs(dest)
            destfile = os.path.join(dest, os.path.basename(orig))
            process_file(orig, destfile)
    
    if len(sys.argv) < 4:
        print("Usage: %s ORIG DESTDIR"%(sys.argv[0]))
        sys.exit(1)
    
    process(sys.argv[1], sys.argv[2])