Skip to content

Instantly share code, notes, and snippets.

@kshirsagarsiddharth
Last active August 3, 2020 09:54
Show Gist options
  • Select an option

  • Save kshirsagarsiddharth/2a250a10fc299e2176a963703cdd174f to your computer and use it in GitHub Desktop.

Select an option

Save kshirsagarsiddharth/2a250a10fc299e2176a963703cdd174f to your computer and use it in GitHub Desktop.
# Reading
import mmap
with open("hello.txt",'r') as f:
with mmap.mmap(f.fileno(),0,access=mmap.ACCESS_READ) as m:
print("First 10 bytes via read -->",m.read(10))
print("First 10 bytes via slice -->",m[:10])
print("2nd 10 bytes via read -->",m.read(10))
"""
First 10 bytes via read --> b'Lorem ipsu'
First 10 bytes via slice --> b'Lorem ipsu'
2nd 10 bytes via read --> b'm dolor si'
"""
# writing
import mmap
import shutil
shutil.copyfile("hello.txt","hello_copy.txt") # copying the file
word = b"python"
rev_word = word[::-1]
with open("hello_copy.txt","r+") as f:
with mmap.mmap(f.fileno(),0,access = mmap.ACCESS_WRITE) as m:
print("Before reversing the string ---> {}".format(m.readline().strip()))
m.seek(0) # coming to initial position
# the mmap.mmap.find() function returns the lowest
# index in the object where the subsequence sub is found.
loc = m.find(word)
m[loc:loc + len(word)] = rev_word
# even though we just wrote into The mmap it may not occure in the mmap
# until the file is closed(mmap.close) hence we use flush which forces the buffer
# content to the memory map(mmap)
m.flush()
m.seek(0)
print("After reversing the string ---> {}".format(m.readline().strip()))
f.seek(0)
print("After reversing the string ---> {}".format(f.readline().strip()))
# IN This case we observe how ACCESS_WRITE effects the memory(RAM) as well as
# the file(Disk)
"""
Before reversing the string ---> b'Lorem ipsum dolor sit amet, consectetuer python adipiscing elit.'
After reversing the string ---> b'Lorem ipsum dolor sit amet, consectetuer nohtyp adipiscing elit.'
After reversing the string ---> Lorem ipsum dolor sit amet, consectetuer nohtyp adipiscing elit.
"""
# copying
import mmap
import shutil
shutil.copyfile("hello.txt","hello_copy.txt") # copying the file
word = b"python"
rev_word = word[::-1]
with open("hello_copy.txt","r+") as f:
with mmap.mmap(f.fileno(),0,access = mmap.ACCESS_COPY) as m:
print("Before reversing the string ---> {}".format(m.readline().strip()))
m.seek(0) # coming to initial position
# the mmap.mmap.find() function returns the lowest
# index in the object where the subsequence sub is found.
loc = m.find(word)
m[loc:loc + len(word)] = rev_word
# even though we just wrote into The mmap it may not occure in the mmap
# until the file is closed(mmap.close) hence we use flush which forces the buffer
# content to the memory map(mmap)
m.flush()
m.seek(0)
print("After reversing the string ---> {}".format(m.readline().strip()))
f.seek(0)
print("After reversing the string ---> {}".format(f.readline().strip()))
# in this case only the memory is affected not the file
"""
Before reversing the string ---> b'Lorem ipsum dolor sit amet, consectetuer python adipiscing elit.'
After reversing the string ---> b'Lorem ipsum dolor sit amet, consectetuer nohtyp adipiscing elit.'
After reversing the string ---> Lorem ipsum dolor sit amet, consectetuer python adipiscing elit.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment