'''
(2.0 ÐÂÔö) mmap Ä£¿éÌá¹©ÁË²Ù×÷ÏµÍ³ÄÚ´æÓ³Éäº¯ÊýµÄ½Ó¿Ú, Èç ÏÂÀý ËùÊ¾.
Ó³ÉäÇøÓòµÄÐÐÎªºÍ×Ö·û´®¶ÔÏóÀàËÆ, µ«Êý¾ÝÊÇÖ±½Ó´ÓÎÄ¼þ¶ÁÈ¡µÄ.
'''

import mmap
import os

filename = "samples/sample.txt"

file = open(filename, "r+")

size = os.path.getsize(filename)
data = mmap.mmap(file.fileno(), size)

# basics
print data
print len(data), size

# use slicing to read from the file
# Ê¹ÓÃÇÐÆ¬²Ù×÷¶ÁÈ¡ÎÄ¼þ

print repr(data[:10]), repr(data[:10])

# or use the standard file interface
# »òÊ¹ÓÃ±ê×¼µÄÎÄ¼þ½Ó¿Ú, read·½·¨»áÒÆ¶¯ÎÄ¼þ¶ÁÐ´ÓÎ±ê
print repr(data.read(10)), repr(data.read(10))

'''
ÔÚ Windows ÏÂ, Õâ¸öÎÄ¼þ±ØÐëÒÔ¼È¿É¶ÁÓÖ¿ÉÐ´µÄÄ£Ê½´ò¿ª( `r+` , `w+` , »ò `a+` ), ·ñÔò mmap µ÷ÓÃ»áÊ§°Ü.
[!Feather ×¢: ¾­±¾ÈË²âÊÔ, a+ Ä£Ê½ÊÇÍêÈ«¿ÉÒÔµÄ, Ô­ÎÄÖ»ÓÐ r+ ºÍ w+]
'''