test_tutorial.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from .lib import TestBase
  2. class TestTutorial(TestBase):
  3. def test_example(self):
  4. # Memory Managers
  5. ##################
  6. import smmap
  7. # This instance should be globally available in your application
  8. # It is configured to be well suitable for 32-bit or 64 bit applications.
  9. mman = smmap.SlidingWindowMapManager()
  10. # the manager provides much useful information about its current state
  11. # like the amount of open file handles or the amount of mapped memory
  12. assert mman.num_file_handles() == 0
  13. assert mman.mapped_memory_size() == 0
  14. # and many more ...
  15. # Cursors
  16. ##########
  17. import smmap.test.lib
  18. with smmap.test.lib.FileCreator(1024 * 1024 * 8, "test_file") as fc:
  19. # obtain a cursor to access some file.
  20. c = mman.make_cursor(fc.path)
  21. # the cursor is now associated with the file, but not yet usable
  22. assert c.is_associated()
  23. assert not c.is_valid()
  24. # before you can use the cursor, you have to specify a window you want to
  25. # access. The following just says you want as much data as possible starting
  26. # from offset 0.
  27. # To be sure your region could be mapped, query for validity
  28. assert c.use_region().is_valid() # use_region returns self
  29. # once a region was mapped, you must query its dimension regularly
  30. # to assure you don't try to access its buffer out of its bounds
  31. assert c.size()
  32. c.buffer()[0] # first byte
  33. c.buffer()[1:10] # first 9 bytes
  34. c.buffer()[c.size() - 1] # last byte
  35. # you can query absolute offsets, and check whether an offset is included
  36. # in the cursor's data.
  37. assert c.ofs_begin() < c.ofs_end()
  38. assert c.includes_ofs(100)
  39. # If you are over out of bounds with one of your region requests, the
  40. # cursor will be come invalid. It cannot be used in that state
  41. assert not c.use_region(fc.size, 100).is_valid()
  42. # map as much as possible after skipping the first 100 bytes
  43. assert c.use_region(100).is_valid()
  44. # You can explicitly free cursor resources by unusing the cursor's region
  45. c.unuse_region()
  46. assert not c.is_valid()
  47. # Buffers
  48. #########
  49. # Create a default buffer which can operate on the whole file
  50. buf = smmap.SlidingWindowMapBuffer(mman.make_cursor(fc.path))
  51. # you can use it right away
  52. assert buf.cursor().is_valid()
  53. buf[0] # access the first byte
  54. buf[-1] # access the last ten bytes on the file
  55. buf[-10:] # access the last ten bytes
  56. # If you want to keep the instance between different accesses, use the
  57. # dedicated methods
  58. buf.end_access()
  59. assert not buf.cursor().is_valid() # you cannot use the buffer anymore
  60. assert buf.begin_access(offset=10) # start using the buffer at an offset