Python next function operation tutorial

Source: Internet
Author: User

We need to learn a lot about the Python next function in actual use. Related technologies must be continuously learned to better master. The following describes how to use the Python next function.

The following provides an iterator implementation and a CharBufReader class that encapsulates the buf and provides an interface for reading a byte at a time (internal implementation reads data from the buf, while buf reads data from the fill buf ). This makes the code reusable.

Iterator can be used to access Python next functions. However, the efficiency is very slow, which is not optimized previously. It takes about 90 s to use file. read (1. It can be seen that the main reason is that the function call causes the slow speed of the original program. Instead, it takes a long time to buffer and read files without writing them.

 
 
  1. class CharBufReader(object):  
  2. def __init__(self, mfile, bufSize = 1000):  
  3. self.mfile = mfile  
  4. #self.bufSize = 64 * 1024 #64k buf size  
  5. self.capacity = bufSize 
  6. self.buf = '' #buf of char  
  7. self.cur = len(self.buf)  
  8. self.size = len(self.buf)  
  9. def __iter__(self):  
  10. return self  
  11. def next(self):  
  12. if self.cur == self.size:  
  13. #if self.cur == len(self.buf):  
  14. #if self.cur == self.buf.__len__():  
  15. selfself.buf = self.mfile.read(self.capacity)  
  16. self.size = len(self.buf)  
  17. if self.size == 0:  
  18. raise StopIteration  
  19. self.cur = 0 
  20. self.cur += 1  
  21. return self.buf[self.cur - 1]   
  22. class Compressor():  
  23. def caculateFrequence(self):  
  24. """The first time of reading the input file and caculate each  
  25. character frequence store in self.dict  
  26. """  
  27. self.infile.seek(0)  
  28. reader = compressor.CharBufReader(self.infile)  
  29. for c in reader:  
  30. if c in self.dict:  
  31. self.dict[c] += 1  
  32. else:  
  33. self.dict[c] = 0 

The above is a detailed introduction to the Python next function, and I hope you will gain some benefits.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.