stdin

Reading in integer from stdin in Python

痞子三分冷 提交于 2019-11-30 09:15:12
问题 I have the following piece of code where I take in an integer n from stdin, convert it to binary, reverse the binary string, then convert back to integer and output it. import sys def reversebinary(): n = str(raw_input()) bin_n = bin(n)[2:] revbin = "".join(list(reversed(bin_n))) return int(str(revbin),2) reversebinary() However, I'm getting this error: Traceback (most recent call last): File "reversebinary.py", line 18, in <module> reversebinary() File "reversebinary.py", line 14, in

Read a line of input faster than fgets?

喜欢而已 提交于 2019-11-30 09:05:20
I'm writing a program where performance is quite important, but not critical. Currently I am reading in text from a FILE* line by line and I use fgets to obtain each line. After using some performance tools, I've found that 20% to 30% of the time my application is running, it is inside fgets . Are there faster ways to get a line of text? My application is single-threaded with no intentions to use multiple threads. Input could be from stdin or from a file. Thanks in advance. You don't say which platform you are on, but if it is UNIX-like, then you may want to try the read() system call, which

wget or curl from stdin

陌路散爱 提交于 2019-11-30 08:26:27
I'd like to download a web pages while supplying URLs from stdin. Essentially one process continuously produces URLs to stdout/file and I want to pipe them to wget or curl. (Think about it as simple web crawler if you want). This seems to work fine: tail 1.log | wget -i - -O - -q But when I use 'tail -f' and it doesn't work anymore (buffering or wget is waiting for EOF?): tail -f 1.log | wget -i - -O - -q Could anybody provide a solution using wget, curl or any other standard Unix tool? Ideally I don't won't want to restart wget in the loop, just keep it running downloading URLs as they come.

node js interact with shell application

梦想与她 提交于 2019-11-30 07:29:26
There are plenty of node js examples on online about how to spawn a child process and then catch the result as a string for your own processing. But... I want to 'interact' with a child process. For example, how would I write a node js application than starts by calling ' python ' and then types a statement ' 1+1 ', lets me catch the result ' 2 ', before proceeding to type another arbitrary statement ' 4+4 '? (And by 'type' I'm assuming it will require streaming data to the stdin that the process uses). var child = require('child_process'); var ps = child.spawn('python', ['-i']); ps.stdout

Is there a way to send data to another process's standard input under Mac OS?

北慕城南 提交于 2019-11-30 05:08:58
问题 I was wondering if there was an API, however obscure it would be, that would allow someone to send data to another process's stdin stream under Mac OS X. Under Linux, if I remember correctly, you can use the filesystem in /proc to access these streams (with the correct permissions, of course). I dunno. Mach ports, anyone? 回答1: Unfortunately, I don't believe you can do that -- MacPorts are all userland, and the operation you require needs (either a lot of trickery, see below, or) kernel

Write to spawned process stdin nodejs?

会有一股神秘感。 提交于 2019-11-30 04:58:35
I have a script that I want to run from another one. The problem is that the child script (process) needs an user input before it continues. var child = spawn('script'); child.stdin.setEncoding('utf8'); child.stdout.on('data', function (data) { console.log(data.toString().trim()); // tells me to input my data child.stdin.write('my data\n'); }); After I input my data the child script should continue but instead it hang in there. Solution Actually the above code work for me. I'm using commander.js in the child script to prompt the user for action. Here is how I respond to a child's script prompt

How to pipe binary data into numpy arrays without tmp storage?

故事扮演 提交于 2019-11-30 04:37:42
问题 There are several similar questions but none of them answers this simple question directly: How can i catch a commands output and stream that content into numpy arrays without creating a temporary string object to read from? So, what I would like to do is this: import subprocess import numpy import StringIO def parse_header(fileobject): # this function moves the filepointer and returns a dictionary d = do_some_parsing(fileobject) return d sio = StringIO.StringIO(subprocess.check_output(cmd))

using QTextStream to read stdin in a non-blocking fashion

落爺英雄遲暮 提交于 2019-11-30 03:26:22
问题 Using Qt, I'm attempting to read the contents of the stdin stream in a non-blocking fashion. I'm using the QSocketNotifier to alert me when the socket has recieved some new data. The setup for the notifier looks like this: QSocketNotifier *pNot = new QSocketNotifier(STDIN_FILENO, QSocketNotifier::Read, this); connect(pNot, SIGNAL(activated(int)), this, SLOT(onData())); pNot->setEnabled(true); The onData() slot looks like this: void CIPCListener::onData() { qDebug() << Q_FUNC_INFO; QTextStream

Cannot get lldb to read file input

落花浮王杯 提交于 2019-11-30 02:35:39
I'm using lldb as a standalone debugger in OSX. I'm trying to debug a C executable, using a text file as input. The lldb documentation specifies the following command for changing stdin to a given file: process launch -i <file> Using this command, lldb seems to ignore the specified file, instead waiting for keyboard input. Is this intended behaviour? If so; what do I need to do to actually get the process to operate on my wanted input file? tl;dr: How do I get lldb to imitate a standard terminal execution like: ./executable < <file> I got it to work as follows: lldb <executable> (lldb)

How to force ffmpeg into non-interactive mode?

偶尔善良 提交于 2019-11-30 00:45:08
问题 Sometimes you want ffmpeg to ask you whether it should overwrite a file. Sometimes it's just a script that you prefer would fail if something is amiss. I.e. don't rely on stdin to answer if you have a question. 回答1: See https://ffmpeg.org/ffmpeg.html#Main-options -stdin - Enable interaction on standard input. On by default unless a pipe is detected. -nostdin - To explicitly disable console interactions. Without -y this will cause ffmpeg to error out if the target file exists. -y - To