stdin

Python Twisted integration with Cmd module

旧城冷巷雨未停 提交于 2019-12-07 01:58:58
问题 I like Python's Twisted and Cmd. I want to use them together. I got some things working, but so far I haven't figured out how to make tab-completion work, because I don't see how to receive tab keypres events right away (without pressing Enter) in Twisted's LineReceiver. Here's my code so far: #!/usr/bin/env python from cmd import Cmd from twisted.internet import reactor from twisted.internet.stdio import StandardIO from twisted.protocols.basic import LineReceiver class CommandProcessor(Cmd):

split STDIN to multiple files (and compress them if possible)

假如想象 提交于 2019-12-07 01:48:55
问题 I have program (gawk) that outputs stream of data to its STDOUT. The data processed is literally 10s of GBs. I don't want to persist it in a single file but rather split it into chunks and potentially apply some extra processing (like compression) to each before saving. my data is a sequence of records and I don't want splitting to cut record in half. Each record matches the following regexp: ^\{index.+?\}\}\n\{.+?\}$ or for simplicity can assume that two rows (first uneven then even when

How do I check if my program has data piped into it

不想你离开。 提交于 2019-12-07 00:09:18
问题 Im writing a program that should read input via stdin, so I have the following contruct. FILE *fp=stdin; But this just hangs if the user hasn't piped anything into the program, how can I check if the user is actually piping data into my program like gunzip -c file.gz |./a.out #should work ./a.out #should exit program with nice msg. thanks 回答1: Since you're using file pointers, you'll need both isatty() and fileno() to do this: #include <unistd.h> #include <stdio.h> int main(int argc, char*

using stdin in pycharm [duplicate]

三世轮回 提交于 2019-12-06 23:33:24
问题 This question already has answers here : Reading from a file with sys.stdin in Pycharm (5 answers) Closed 2 years ago . I have started to use pycharm and i can't seem to in the command line i use to run the program in this way: python parse_file.py < test.txt when parse_file.py can be simple as : import sys for line in sys.stdin: print line i cant find in the configuration where to do it i tried typeing something like <test.txt in the script parameters but no luck I have looked at https://www

How to read line by line from stdin in python

此生再无相见时 提交于 2019-12-06 17:04:13
问题 Everyone knows how to count the characters from STDIN in C. However, when I tried to do that in python3, I find it is a puzzle. (counter.py) import sys chrCounter = 0 for line in sys.stdin.readline(): chrCounter += len(line) print(chrCounter) Then I try to test the program by python3 counter.py < counter.py The answer is only the length of the first line "import sys". In fact, the program ONLY read the first line from the standard input, and discard the rest of them. It will be work if I take

Why does Bash always add a newline when expanding here strings?

筅森魡賤 提交于 2019-12-06 15:52:31
Having read the following description of this feature of Bash (excerpt from the man page): Here Strings A variant of here documents, the format is: <<<word The word is expanded and supplied to the command on its standard input. I expected that the interpretation of here strings is that Bash simply passes the contents of a variable directly on a command's standard input, unmodified. Following this logic, the lines [1] and [2] below would be effectively equivalent. [1]~$ printf foo | cat - <(echo end) fooend [2]~$ cat - <(echo end) <<<foo foo end However, Bash added a newline when “expanding” a

Peek stdin using pthreads

╄→尐↘猪︶ㄣ 提交于 2019-12-06 11:59:22
I'm trying to peek stdin to see if anything is there using pthreads. I (think I) need to do this because if there is nothing in std in, stream access functions will block for input. I feel the way to do this is to fire off a pthread that checks stdin, and sleep(1) and see if the thread found anything. Here's what I have so far. If nothing is piped into the program then it will sleep as expected, but if something is in stdin the thread never gets fired. #include <iostream> #include <pthread.h> #include <stdlib.h> using namespace std; void *checkStdIn(void *d){ char c = '\0'; c = cin.peek(); if

Prepend to stdin

人盡茶涼 提交于 2019-12-06 09:56:45
I've got an interactive command line program that we'll call program.exe. While it's running, I can type in a bunch of different commands that do different things. We'll call them: doA, doB, doC, and quit. Now, as it turns out, the first thing I want to do in program.exe is always doA. I'd like to save a few keystrokes and automatically have program.exe run the command doA upon startup. Unfortunately, I can't just pass "doA" on the command line because whoever created program.exe didn't implement that feature. The way I see it, I have a few options: Write another program that calls program.exe

Python: equivalent of input using sys.stdin

余生颓废 提交于 2019-12-06 09:24:06
I want to test some (python 3) code that directly uses the print and input functions. As I understand it, the easiest way to do this is by dependency injection: modifying the code so that it takes input and output streams as arguments, using sys.stdin and sys.stdout by default and passing in mock objects during testing. It's obvious what to do with print calls: print(text) #replaced with... print(text, file=output_stream) However, input doesn't have arguments for input and output streams. Does the following code correctly reproduce its behaviour? text = input(prompt) #replaced with... print

Erlang: How to pipe stdin input from a file to an erlang program and match eof?

社会主义新天地 提交于 2019-12-06 07:56:46
问题 How to pipe input from a file as stdin to an erlang program running in the shell as well as standalone? I have a file hr.erl and I compile it from the shell. There is a function in it which accepts input from stdin using io:fread() . I have written a case expression with guards where if it matches {ok, [0]} it should terminate. Instead of 0 , I actually need it to be eof . How to send eof when running in a shell? I have a file inp.txt with values 1 2 3 and 0 on each line. How can I pass it