Skip to content

Instantly share code, notes, and snippets.

@kshirsagarsiddharth
Created August 6, 2020 06:36
Show Gist options
  • Select an option

  • Save kshirsagarsiddharth/450c9b1bc8831f66725ae4706879d9dd to your computer and use it in GitHub Desktop.

Select an option

Save kshirsagarsiddharth/450c9b1bc8831f66725ae4706879d9dd to your computer and use it in GitHub Desktop.
spooled memory
# the _rolled attribute of SpooledTemporaryFile object is a boolean, it returns True
# if the file is rolled over(written from the memory buffer to the disk) else False
import tempfile
temp = tempfile.SpooledTemporaryFile(max_size=10,mode="w+b")
# the dize of input is 11(greater than max_size)
temp.write(b"aaaaaaaaaaa")
temp._rolled
Output: True
# for binary data
import tempfile
with tempfile.SpooledTemporaryFile(max_size=100,mode="w+b") as temp:
print(temp)
while not temp._rolled:
temp.write(b"Hello World Hello Worlddddd")
print(temp._rolled,temp._file)
"""
Output:
<tempfile.SpooledTemporaryFile object at 0x0000019D426DE5C8>
False <_io.BytesIO object at 0x0000019D41F7A888>
False <_io.BytesIO object at 0x0000019D41F7A888>
False <_io.BytesIO object at 0x0000019D41F7A888>
True <tempfile._TemporaryFileWrapper object at 0x0000019D42A26C88>
"""
# for textual data
import tempfile
with tempfile.SpooledTemporaryFile(max_size=100,mode="w+t",encoding="utf-8") as temp:
print(temp)
while not temp._rolled:
temp.write("Hello World Hello Worlddddd")
print(temp._rolled,temp._file)
"""
Output:
<tempfile.SpooledTemporaryFile object at 0x0000019D42ADF388>
False <_io.TextIOWrapper encoding='utf-8'>
False <_io.TextIOWrapper encoding='utf-8'>
False <_io.TextIOWrapper encoding='utf-8'>
True <tempfile._TemporaryFileWrapper object at 0x0000019D425EC0C8>
"""
# to explictly cause the buffer to be written on the disk
# using fileno()
import tempfile
with tempfile.SpooledTemporaryFile(max_size = 1000,mode = "w+b") as temp:
print(temp)
for i in range(5):
temp.write(b"byteman")
print(temp._rolled,temp._file)
print("Let's rollover this function")
temp.fileno()
print(temp._rolled,temp._file)
"""
<tempfile.SpooledTemporaryFile object at 0x0000019D433EB788>
False <_io.BytesIO object at 0x0000019D41F7AE28>
False <_io.BytesIO object at 0x0000019D41F7AE28>
False <_io.BytesIO object at 0x0000019D41F7AE28>
False <_io.BytesIO object at 0x0000019D41F7AE28>
False <_io.BytesIO object at 0x0000019D41F7AE28>
Let's rollover this function
True <tempfile._TemporaryFileWrapper object at 0x0000019D433EBD08>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment