Writing complicated systems is a fantastic feeling. You know, ideas sovastthat you have to sit down with pen and paper just to think for a bit. You might even talk with a coworker instead of just imagining your solution is perfect right off the bat and implementing
Then there are other days. When you have lost the ability to function in any meaningful way and you end up putting a disproportionate amount of mental energy into crafting a Python version of.. I don't know..
如果你也taring at a blank wall in confusion on this fine Sunday, perhaps you can write your own Python implementation of
tailwhich meets these requirements:
- supports following newly appended lines to a file (withoutshowing the existing lines of the file),
- supports iterating through all the existing lines and then following new lines as they appear.
Judging criteria are:
- correctness (working is better than not working)
- simplicity (simple is better than complex)
- conciseness (short is better than long)
- performance (fast is better than slow)
- style (mine is better than yours)
Well, get coding!
For your consideration, here is my solution.
defreadlines_then_tail(鳍):"Iterate through lines and then tail for further lines."whileTrue:line=鳍.readline()ifline:yieldlineelse:tail(鳍)
deftail(鳍):"Listen for new lines added to file."whileTrue:where=鳍.tell()line=鳍.readline()ifnotline:time.sleep(SLEEP_INTERVAL)鳍.seek(where)else:yieldline
defmain():p=OptionParser("usage: tail.py file")(options,args)=p.parse_args()iflen(args)<1:p.error("must specify a file to watch")withopen(args,'r')as鳍:forlineinreadlines_then_tail(鳍):printline.strip()
Now, where's yours?