当前位置: 首页>>代码示例>>Python>>正文


Python mmap函数代码示例

本文整理汇总了Python中mmap函数的典型用法代码示例。如果您正苦于以下问题:Python mmap函数的具体用法?Python mmap怎么用?Python mmap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了mmap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_offset_more

    def test_offset_more(self):
        from mmap import mmap, ALLOCATIONGRANULARITY

        with open(self.tmpname, "w+b") as f:
            halfsize = ALLOCATIONGRANULARITY
            f.write("\0" * halfsize)
            f.write("foo")
            f.write("\0" * (halfsize - 3))
            m = mmap(f.fileno(), 0)
            m.close()

        with open(self.tmpname, "r+b") as f:
            m = mmap(f.fileno(), halfsize, offset=halfsize)
            assert m[0:3] == "foo"

        try:
            m.resize(512)
        except SystemError:
            pass
        else:
            assert len(m) == 512
            raises(ValueError, m.seek, 513, 0)
            assert m[0:3] == "foo"
            with open(self.tmpname) as f:
                f.seek(0, 2)
                assert f.tell() == halfsize + 512
            assert m.size() == halfsize + 512
        m.close()
开发者ID:Sherlockhlt,项目名称:pypy,代码行数:28,代码来源:test_mmap.py

示例2: __init__

 def __init__ (self,host,path,parts,pbegin=0,pend=0,m=None):
     asyncore.dispatcher.__init__(self)
     # Initialize class member variables.
     self.keepalive = False
     self.done = 0
     self.h = [self]
     self.recvhead = 1
     self.bytes = 0
     self.ack = 0
     self.begin = time()
     self.path = path
     self.parts = parts
     self.host = host
     self.buffer = ""
     self.pbegin = pbegin
     self.pend = pend
     self.length = 8192
     self.f = None
     # Grab the filename from the end of the URL.
     self.filename = split(path,"/")[-1]
     # Check if file exists and if so ask if overwrite necessary.
     if os.access(self.filename,os.O_RDWR) and self.parts > 0:
         u = raw_input("File already exists, overwrite? [y/N] ")
         if u == 'y' or u == 'Y':
             print "Overwriting..."
         else:
             print "Aborting..."
             return None
     # Connect to the host with it on port 80.
     print "Connecting..."
     self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
     self.connect((host, 80))
     # Parts are greater than 0 so we are the parent, open file, get header.
     if self.parts > 0:
         # Open and memory map the file.
         self.f = open(self.filename,'wb+')
         self.f.write("\0")
         self.f.flush() # We have to flush to make the file buffer ready for mmap.
         # Windows uses 0 for second parameter to auto-size to current file size.
         # Whereas, on Linux and other platforms, mmap requires a size.
         if platformName == "Windows":
             self.m = mmap(self.f.fileno(), 0)
         else:
             self.m = mmap(self.f.fileno(), os.fstat(self.f.fileno()).st_size)
         # Download the header.
         self.buffer = "HEAD %s HTTP/1.1\r\nHost: %s\r\n\r\n" % (self.path,self.host)
         print "Downloading http://%s%s" % (self.host,self.path)
     # Otherwise, we are a child, skip the header and download our segment.
     elif self.parts == 0:
         # Set our own mmap to the one given to us by the parent.
         self.m = m
         # Prepare ourselves to download the segment.
         self.bytes = self.pbegin
         self.length = self.pend
         self.recvhead = 2
         self.buffer = "GET %s HTTP/1.1\r\nHost: %s\r\nRange: bytes=%lu-%lu\r\n\r\n" % (self.path,self.host,self.pbegin,self.pend)
         print self.buffer
开发者ID:bhramoss,项目名称:code,代码行数:57,代码来源:recipe-114217.py

示例3: pci_set_value

 def pci_set_value(self, resource, val, offset):
     fd = open(resource, O_RDWR)
     mm = mmap(fd, 0)
     val = self.pci_mem_write(mm, offset, val)
     mm.close()
     close(fd)
     return val
开发者ID:Azure,项目名称:sonic-buildimage,代码行数:7,代码来源:sfputil.py

示例4: pci_get_value

 def pci_get_value(self, resource, offset):
     fd = open(resource, O_RDWR)
     mm = mmap(fd, 0)
     val = self.pci_mem_read(mm, offset)
     mm.close()
     close(fd)
     return val
开发者ID:Azure,项目名称:sonic-buildimage,代码行数:7,代码来源:sfputil.py

示例5: test_find

    def test_find(self):
        from mmap import mmap
        f = open(self.tmpname + "g", "w+")

        f.write("foobar\0")
        f.flush()
        m = mmap(f.fileno(), 7)
        raises(TypeError, m.find, 123)
        raises(TypeError, m.find, "foo", "baz")
        assert m.find("b") == 3
        assert m.find("z") == -1
        assert m.find("o", 5) == -1
        assert m.find("ob") == 2
        assert m.find("\0") == 6
        assert m.find("ob", 1) == 2
        assert m.find("ob", 2) == 2
        assert m.find("ob", 3) == -1
        assert m.find("ob", -4) == -1
        assert m.find("ob", -5) == 2
        assert m.find("ob", -999999999) == 2
        assert m.find("ob", 1, 3) == -1
        assert m.find("ob", 1, 4) == 2
        assert m.find("ob", 1, 999999999) == 2
        assert m.find("ob", 1, 0) == -1
        assert m.find("ob", 1, -1) == 2
        assert m.find("ob", 1, -3) == 2
        assert m.find("ob", 1, -4) == -1
        #
        data = m.read(2)
        assert data == "fo"
        assert m.find("o") == 2
        assert m.find("oo") == -1
        assert m.find("o", 0) == 1
        m.close()
        f.close()
开发者ID:Sherlockhlt,项目名称:pypy,代码行数:35,代码来源:test_mmap.py

示例6: __init__

    def __init__(self, code, ret_type, *arg_types, raw=False):
        if not raw:
            with tempfile.NamedTemporaryFile(suffix='.asm') as tmp:
                tmp.write(str.encode(code))
                tmp.flush()
                output_path = "{}.bin".format(tmp.name)
                subprocess.check_call(["nasm", tmp.name, "-o", output_path])

                output = open(output_path, 'rb')
                code = output.read()
                output.close()
                os.remove(output_path)

        if not isinstance(code, list) and isinstance(code, str):
            code = [ord(c) for c in code]

        self.length = len(code)

        mmap.restype = POINTER(ARRAY(c_ubyte, self.length))
        mem = mmap(0, self.length, PROT_READ | PROT_WRITE | PROT_EXEC,
                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)[0]
        getaddr = CFUNCTYPE(c_void_p, c_void_p)(lambda p: p)
        func = CFUNCTYPE(ret_type, *arg_types)(getaddr(mem))

        mem[:] = code
        self.func = func
        self.mem = mem
开发者ID:faineance,项目名称:inlineasm,代码行数:27,代码来源:__init__.py

示例7: main

def main():

    global uartfile
    uartfile=os.open(UART_DEVICE_NAME,os.O_RDWR)

    mm=mmap(fileno=uartfile, length=sizeof(UART),flags=MAP_SHARED,prot=PROT_READ | PROT_WRITE, offset=0)

    global devcon
    devcon=lms2012.DEVCON()
    global uart
    uart=UART.from_buffer(mm)


    resetAll()
    print "reset all"
    time.sleep(2)
    global port

    port=3
    
    setUartMode(2)
    timeout=datetime.datetime.now()+datetime.timedelta(seconds=10)
    while (datetime.datetime.now()<timeout):
        index = uart.Actual[port]
        print index
        print [str(b) for b in uart.Raw[port][index]]
开发者ID:jensadne,项目名称:python-ev3,代码行数:26,代码来源:UARTSensor.py

示例8: test_rfind

    def test_rfind(self):
        from mmap import mmap
        f = open(self.tmpname + "g", "w+")

        f.write("foobarfoobar\0")
        f.flush()
        m = mmap(f.fileno(), 13)
        raises(TypeError, m.rfind, 123)
        raises(TypeError, m.rfind, "foo", "baz")
        assert m.rfind("b") == 9
        assert m.rfind("z") == -1
        assert m.rfind("o", 11) == -1
        assert m.rfind("ob") == 8
        assert m.rfind("\0") == 12
        assert m.rfind("ob", 7) == 8
        assert m.rfind("ob", 8) == 8
        assert m.rfind("ob", 9) == -1
        assert m.rfind("ob", -4) == -1
        assert m.rfind("ob", -5) == 8
        assert m.rfind("ob", -999999999) == 8
        assert m.rfind("ob", 1, 3) == -1
        assert m.rfind("ob", 1, 4) == 2
        assert m.rfind("ob", 1, 999999999) == 8
        assert m.rfind("ob", 1, 0) == -1
        assert m.rfind("ob", 1, -1) == 8
        assert m.rfind("ob", 1, -3) == 8
        assert m.rfind("ob", 1, -4) == 2
        #
        data = m.read(8)
        assert data == "foobarfo"
        assert m.rfind("o") == 8
        assert m.rfind("oo") == -1
        assert m.rfind("o", 0) == 8
        m.close()
        f.close()
开发者ID:Sherlockhlt,项目名称:pypy,代码行数:35,代码来源:test_mmap.py

示例9: init

def init():
    global isInitialized    
    if not isInitialized:
        analogfile=os.open(lms2012.ANALOG_DEVICE_NAME,os.O_RDWR)
        global analogmm
        analogmm=mmap(fileno=analogfile, length=sizeof(lms2012.ANALOG),flags=MAP_SHARED,prot=PROT_READ | PROT_WRITE, offset=0)
        global analog
        analog=lms2012.ANALOG.from_buffer(analogmm)
        isInitialized=True
开发者ID:jensadne,项目名称:python-ev3,代码行数:9,代码来源:AnalogDevice.py

示例10: docalculation

def docalculation(popsout, popsin, operation, count):
   gimp.progress_init("Sending data...")
   file = os.open("/home/cybertron/source/.dummymmap", os.O_RDWR)
   shmem = mmap(file, 20000000)
   for x in range(0, width):
      gimp.progress_update(float(x) / float(width))
      gimp.displays_flush()
      for y in range(0, height):
         shmem.write(popsin[x, y][0:count])

   shmem.close()
   os.close(file)
   
   gimp.progress_init("Starting threads...")
   childplist = []
   for i in range(0, numthreads):
      childplist.append(Popen("/home/cybertron/bin/bumphelper.py", stdin=PIPE, stdout=PIPE))
      childplist[i].stdin.write(operation + "\n")
      childplist[i].stdin.write(str(width) + "\n")
      childplist[i].stdin.write(str(height) + "\n")
      childplist[i].stdin.write(str((width / numthreads + 1) * i) + "\n")
      childplist[i].stdin.write(str(width / numthreads + 1) + "\n")
      childplist[i].stdin.write(str(bias) + "\n")
      childplist[i].stdin.write(str(invertx) + "\n")
      childplist[i].stdin.write(str(inverty) + "\n")
      childplist[i].stdin.write(str(wrap) + "\n")
   
   gimp.progress_init("Waiting for threads...")
   gimp.displays_flush()
   # Keep threads from blocking
   threadouts = ["" for i in range(0, numthreads)]
   alldone = False
   while not alldone:
      alldone = True
      for i in range(0, numthreads):
         if threadouts[i].find("EOD") == -1:
            threadouts[i] += childplist[i].stdout.read(50000)
            alldone = False

   gimp.progress_init("Retrieving results...")
   for i in range(0, numthreads):
      gimp.progress_update(float(i) / float(numthreads))
      gimp.displays_flush()
      output = threadouts[i]
      start = (width / numthreads + 1) * i
      calc = width / numthreads + 1
      if (start + calc > width):
         calc = width - start
      currpos = 0
      for x in range(start, start + calc):
         for y in range(0, height):
            pixelval = output[currpos]
            pixelval += output[currpos + 1]
            pixelval += output[currpos + 2]
            pixelval += chr(255)
            popsout[x, y] = pixelval
            currpos += 3
开发者ID:cybertron,项目名称:gimp-plugins,代码行数:57,代码来源:createbump.py

示例11: test_sequence_type

 def test_sequence_type(self):
     from mmap import mmap
     f = open(self.tmpname + "x", "w+")
     f.write("foobar")
     f.flush()
     m = mmap(f.fileno(), 6)
     import operator
     assert operator.isSequenceType(m)
     assert not operator.isMappingType(m)
开发者ID:antoine1fr,项目名称:pygirl,代码行数:9,代码来源:test_mmap.py

示例12: test_close

 def test_close(self):
     from mmap import mmap
     f = open(self.tmpname + "c", "w+")
     
     f.write("c")
     f.flush()
     m = mmap(f.fileno(), 1)
     m.close()
     raises(ValueError, m.read, 1)
开发者ID:antoine1fr,项目名称:pygirl,代码行数:9,代码来源:test_mmap.py

示例13: test_tell

 def test_tell(self):
     from mmap import mmap
     f = open(self.tmpname + "m", "w+")
     
     f.write("c")
     f.flush()
     m = mmap(f.fileno(), 1)
     assert m.tell() >= 0
     m.close()
     f.close()
开发者ID:antoine1fr,项目名称:pygirl,代码行数:10,代码来源:test_mmap.py

示例14: test_size

 def test_size(self):
     from mmap import mmap
     f = open(self.tmpname + "l", "w+")
     
     f.write("foobar")
     f.flush()
     m = mmap(f.fileno(), 5)
     assert m.size() == 6 # size of the underline file, not the mmap
     m.close()
     f.close()
开发者ID:antoine1fr,项目名称:pygirl,代码行数:10,代码来源:test_mmap.py

示例15: test_buffer

 def test_buffer(self):
     from mmap import mmap
     f = open(self.tmpname + "y", "w+")
     f.write("foobar")
     f.flush()
     m = mmap(f.fileno(), 6)
     b = buffer(m)
     assert len(b) == 6
     assert b[3] == "b"
     assert b[:] == "foobar"
开发者ID:Sherlockhlt,项目名称:pypy,代码行数:10,代码来源:test_mmap.py


注:本文中的mmap函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。