tail

How do I make a function involving futures tail recursive?

雨燕双飞 提交于 2019-11-27 05:15:39
In my Scala app, I have a function that calls a function which returns a result of type Future[T]. I need to pass the mapped result in my recursive function call. I want this to be tail recursive, but the map (or flatMap) is breaking the ability to do that. I get an error "Recursive call not in tail position." Below is a simple example of this scenario. How can this be modified so that the call will be tail recursive (without subverting the benefits of Futures with an Await.result())? import scala.annotation.tailrec import scala.concurrent.{Await, Future} import scala.concurrent.duration._

Do a tail -F until matching a pattern

馋奶兔 提交于 2019-11-27 03:58:39
I want to do a tail -F on a file until matching a pattern. I found a way using awk, but IMHO my command is not really clean. The problem is that I need to do it in only one line, because of some limitations. tail -n +0 -F /tmp/foo | \ awk -W interactive '{if ($1 == "EOF") exit; print} END {system("echo EOF >> /tmp/foo")}' The tail will block until EOF appears in the file. It works pretty well. The END block is mandatory because awk's "exit" does not exit right away. It makes awk to eval the END block before quitting. The END block hangs on a read call (because of tail), so the last thing I

Head and tail in one line

半城伤御伤魂 提交于 2019-11-27 03:26:44
Is there a pythonic way to unpack a list in the first element and the "tail" in a single command? For example: >> head, tail = **some_magic applied to** [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] >> head 1 >>> tail [1, 2, 3, 5, 8, 13, 21, 34, 55] Under Python 3.x, you can do this nicely: >>> head, *tail = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] >>> head 1 >>> tail [1, 2, 3, 5, 8, 13, 21, 34, 55] A new feature in 3.x is to use the * operator in unpacking, to mean any extra values. It is described in PEP 3132 - Extended Iterable Unpacking . This also has the advantage of working on any iterable, not just

Implement “tail -f” in C++

邮差的信 提交于 2019-11-27 03:15:11
问题 I want to create a small code in C++ with the same functionality as "tail-f": watch for new lines in a text file and show them in the standard output. The idea is to have a thread that monitors the file Is there an easy way to do it without opening and closing the file each time? 回答1: Just keep reading the file. If the read fails, do nothing. There's no need to repeatedly open and close it. However, you will find it much more efficient to use operating system specific features to monitor the

python head, tail and backward read by lines of a text file

穿精又带淫゛_ 提交于 2019-11-27 00:24:29
问题 How to implement somethig like the 'head' and 'tail' commands in python and backward read by lines of a text file? 回答1: This is my personal file class ;-) class File(file): """ An helper class for file reading """ def __init__(self, *args, **kwargs): super(File, self).__init__(*args, **kwargs) self.BLOCKSIZE = 4096 def head(self, lines_2find=1): self.seek(0) #Rewind file return [super(File, self).next() for x in xrange(lines_2find)] def tail(self, lines_2find=1): self.seek(0, 2) #Go to end of

How To watch a file write in PHP?

荒凉一梦 提交于 2019-11-26 20:27:54
问题 I want to make movement such as the tail command with PHP, but how may watch append to the file? 回答1: I don't believe that there's some magical way to do it. You just have to continuously poll the file size and output any new data. This is actually quite easy, and the only real thing to watch out for is that file sizes and other stat data is cached in php. The solution to this is to call clearstatcache() before outputting any data. Here's a quick sample, that doesn't include any error

How to implement a pythonic equivalent of tail -F?

删除回忆录丶 提交于 2019-11-26 15:14:42
What is the pythonic way of watching the tail end of a growing file for the occurrence of certain keywords? In shell I might say: tail -f "$file" | grep "$string" | while read hit; do #stuff done Jochen Ritzel Well, the simplest way would be to constantly read from the file, check what's new and test for hits. import time def watch(fn, words): fp = open(fn, 'r') while True: new = fp.readline() # Once all lines are read this just returns '' # until the file changes and a new line appears if new: for word in words: if word in new: yield (word, new) else: time.sleep(0.5) fn = 'test.py' words = [

How do I make a function involving futures tail recursive?

两盒软妹~` 提交于 2019-11-26 12:46:56
问题 In my Scala app, I have a function that calls a function which returns a result of type Future[T]. I need to pass the mapped result in my recursive function call. I want this to be tail recursive, but the map (or flatMap) is breaking the ability to do that. I get an error \"Recursive call not in tail position.\" Below is a simple example of this scenario. How can this be modified so that the call will be tail recursive (without subverting the benefits of Futures with an Await.result())?

Head and tail in one line

一世执手 提交于 2019-11-26 12:39:15
问题 Is there a pythonic way to unpack a list in the first element and the \"tail\" in a single command? For example: >> head, tail = **some_magic applied to** [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] >> head 1 >>> tail [1, 2, 3, 5, 8, 13, 21, 34, 55] 回答1: Under Python 3.x, you can do this nicely: >>> head, *tail = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] >>> head 1 >>> tail [1, 2, 3, 5, 8, 13, 21, 34, 55] A new feature in 3.x is to use the * operator in unpacking, to mean any extra values. It is described in

Do a tail -F until matching a pattern

≯℡__Kan透↙ 提交于 2019-11-26 10:58:42
问题 I want to do a tail -F on a file until matching a pattern. I found a way using awk, but IMHO my command is not really clean. The problem is that I need to do it in only one line, because of some limitations. tail -n +0 -F /tmp/foo | \\ awk -W interactive \'{if ($1 == \"EOF\") exit; print} END {system(\"echo EOF >> /tmp/foo\")}\' The tail will block until EOF appears in the file. It works pretty well. The END block is mandatory because awk\'s \"exit\" does not exit right away. It makes awk to