我需要一种非常便宜的方法来读取Python中没有终止字符串(流)的缓冲区.这就是我所拥有的,但它耗费了大量的CPU时间和精力.因为它经常"尝试和捕捉".我真的需要一种新的方法.
这是我的代码的简化工作版本:
#! /usr/bin/env/ python
import fcntl, os, sys
if __name__ == "__main__":
f = open("/dev/urandom", "r")
fd = f.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
ready = False
line = ""
while True:
try:
char = f.read()
if char == '\r':
continue
elif char = '\n':
ready = True
else:
line += char
except:
continue
if ready:
print line
Run Code Online (Sandbox Code Playgroud)
不要在终端中运行它.这只是为了说明."urandom"将破坏你的终端,因为它会吐出很多随机字符,终端模拟器无论解释什么(可以改变你当前的shell设置,标题等).我正在读取通过usb连接的gps.
问题:这可以使用100%的CPU使用率.我试过这个:
#! /usr/bin/env/ python
import fcntl, os, sys
if __name__ == …Run Code Online (Sandbox Code Playgroud) 哪个 Ansi 转义序列是最便携和/或最好的,为什么?
1. "\u001B[32;1mThis is bright green\u001B[0m"
2. "\x1B[33;1mThis is bright yellow\x1B[0m"
3. "\e[35;4;1mThis is bright purple underlined\e[0m"
Run Code Online (Sandbox Code Playgroud)
我出于习惯一直在使用printf "\x1B[32;1mgreen\x1B[0m"(例如,unix bash 脚本中的一个示例),但我想知道是否有任何理由使用其中一个而不是另一个。其中一款比其他的更便携吗?这是我的假设。
另外,如果您知道任何其他 Ansi Escape 序列,请随时在评论中或在答案末尾分享。
如果您不知道 Ansi Escape 序列是什么或想更熟悉它,请访问: http: //en.wikipedia.org/wiki/ANSI_escape_code
笔记:
上面的所有转义序列都适用于我使用过的所有 Unix 系统,但是仍然必须依赖系统本身来解释转义码。例如,Windows 不允许使用除四种转义码(BEL、LF 或换行符、CR 或回车符,当然还有 BS 或退格键)之外的任何类型的转义码,因此 Ansi 转义序列将不起作用。
我在Stack Overflow中看到了一个与Android类似的问题,但我想知道是否应该使用反引号(`)或双引号(") - 使用Python - 来选择表名或rowid或者你有什么.
我试过单引号 - 像这样select 'rowid', * from 'tbl' order by 'rowid'.单引号在某些情况下有效,但并非全部.我学会了使用双引号或反引号,我在看,SQLite Database Browser我注意到它使用了反引号.
我真的很喜欢在Python中使用双引号,因为我来自Java,所以这很自然cursor.execute("select 'rowid',* from 'table';"),而且反引号也很容易(双引号需要反斜杠并使查询看起来很简单)有点混乱).但是,我只是想确保反引号是可移植的(所有版本的Windows,Linux,Os x等).
如果添加键绑定在Java中带着面具-让我们只说与KeyEvent.VK_A的ActionEvent.ALT_MASK -然后你执行键(ALT + A),但你松开Alt键只在"A"键之前,你经常会遇到一个问题,即一个类(实现ActionListener)中的actionPerformed()将继续被激活.这可能(98%肯定)意味着密钥绑定从未注册密钥被释放.如果你在alt键之前释放'A'键,你很好,但是 - 就像我说的 - 如果你在另一个键之前释放alt键大约1/10秒,它会不断重复.
注意:这只发生 - 显然 - 在我的程序中(这里)
如果你不相信我,请亲自尝试一下.这是我的代码片段:
public ConwayPanel() {
super();
setBackground(new Color(245, 255, 245, 255)); // BG slightly green - all ready
paused = true; // nothing to play... in FUTURE put cool organism in
startX = 0; // starting position of the left of the grid
startY = 0; // starting position of the top of the grid
zoom = 15; // the width of each cell …Run Code Online (Sandbox Code Playgroud) 如果我有两个设备通过USB(在Linux中)连接,并希望同时读取它们.从本质上讲,它们永远不会终止,但我想在读取一行时读取它们(每行结束\r\n).
这是Python中的样子:
from threading import Thread
usb0 = open("/dev/ttyUSB0", "r")
usb1 = open("/dev/ttyUSB1", "r")
def read0():
while True: print usb0.readline().strip()
def read1():
while True: print usb1.readline().strip()
if __name__ == '__main__':
Thread(target = read0).start()
Thread(target = read1).start()
Run Code Online (Sandbox Code Playgroud)
有没有办法在bash中这样做.我知道你可以这样做:
while read -r -u 4 line1 && read -r -u 5 line2; do
echo $line1
echo $line2
done 4</dev/ttyUSB0 5</dev/ttyUSB1
Run Code Online (Sandbox Code Playgroud)
然而,这实际上每隔几次切断我的部分线路.我真的更好奇,如果这是可能的并且不需要它,因为使用Java或Python等现代编程语言进行线程化非常容易.
有什么办法可以做到以下几点吗?
def getTables(self, db = self._dbName):
//do stuff with db
return func(db)
Run Code Online (Sandbox Code Playgroud)
我得到了"self" is not defined.我能做到这一点......
def getTables(self, db = None):
if db is None:
db = self._db
//do stuff with db
return func(db)
Run Code Online (Sandbox Code Playgroud)
......但那只是烦人的.
我非常努力地为地标命名,但没有将其显示在地图上的地标旁边。当您转到 Google 地图的左侧时,列表中的所有地标都显示[no name]。那么,有没有办法摆脱名称(如果我想命名地标并将该文件夹显示在左侧)?
我的 KML 结构如下所示:
<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<kml xmlns=\"http://earth.google.com/kml/2.2\">
<Document>
<name>foo</name>
<Folder>
<name>bar</name>
<description>foo bar</description>
<Placemark>
<Style>
<LineStyle>
<color>ffff0000</color>
<width>2</width>
</LineStyle>
</Style>
<name></name>
<LineString>
<coordinates>{},{},{},{},{},{}</coordinates>
</LineString>
</Placemark>
<Placemark>
<Style>
<IconStyle>
<Icon><href>file://{11}arrow.png </href></Icon>
<heading>{4}</heading>
<color>{10}</color>
<scale>.6</scale>
</IconStyle>
</Style>
<name></name>
<scale>0</scale>
<color>{10}</color>
<description>{9}<br/><b>duration:</b> {8}<br/><b>time:</b> {7}<br/><b>CNR:</b> {5}<br/><b>BER:</b> {6}</description>
<Point><coordinates>{0},{1},{2}</coordinates></Point>
</Placemark>
<Placemark>
<Style>
<LineStyle>
<color>ffff0000</color>
<width>2</width>
</LineStyle>
</Style>
<name></name>
<LineString>
<coordinates>{},{},{},{},{},{}</coordinates>
</LineString>
</Placemark>
<Placemark>
<Style>
<IconStyle>
<Icon><href>file://{11}arrow.png </href></Icon>
<heading>{4}</heading>
<color>{10}</color>
<scale>.6</scale>
</IconStyle>
</Style>
<name></name>
<scale>0</scale>
<color>{10}</color>
<description>{9}<br/><b>duration:</b> {8}<br/><b>time:</b> …Run Code Online (Sandbox Code Playgroud) 我对编程有点陌生(不是真的,但我仍在学习 - 我们不是吗?)。虽然我了解 Java 和 Python,并且了解 C、C++、JS、C#、HTML、CSS 等(并且我可以在终端中很好地导航),但我不熟悉终端中的 $PATH 是什么。
我使用 Linux 终端和 Mac 终端的频率比以前要高得多(如果我两年前使用过的话),而且我知道对于 python,它希望你像PATH=\path\to\python\bin:${PATH}\ export PATH. 然而,我什至不知道它是做什么的。我试图找出答案,但我所能找到的只是人们说“导出这条路径并导出那条路径”。
那么,它是什么以及为什么使用它?我明白(如果你为Python这样做),它基本上使“python”(或“python2”或“python3”)成为一个变量,但我只是不明白它是什么的概念。
我在Mac OS X上使用C(版本10.6.8); 这是一个小程序,用于测试在声明int但未初始化时发生的情况:
#include <stdio.h>
int main() {
int a;
printf("\n%d\n\n", a);
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时gcc -std=c99 test.c; ./a.out,我得到一个可预测的结果:unsigned int(32767)的最大值.但是,当我使用-ogcc 时会发生一些奇怪的事情.当我这样做时,gcc -std=c99 test.c -o test.out; ./test.out我得到了别的东西:0.
我发现,无论我将文件命名为什么或如何编译它都没关系.如果名字不是 a.out,那我得到0.当名称是a.out我得到最大的uint类型或一些其他更大的数字.
与下面的一些想法相反,只有当编译文件a.out非常奇怪时,这个数字才是半随机的(大多数是最大的uint,但有时候数字更大).它可能只是我的电脑; 或者,也许,我幸运地 - 或者不幸的是 - 连续30次收到相同的结果.
我在 mdn(我知道这不是官方参考)或其他任何地方都找不到这个;所以,我想我会问这个简单的问题:
例如,我有以下代码片段:
var can = document.getElementById("can");
var ctx = can.getContext("2d");
var w = can.width; var h = can.height;
var ys = [1, 3, 2, 4, 5, 3, 2, 4, 5, 6, 7, 3];
ctx.beginPath();
for (var i = 0, iMax = ys.length; i < iMax; i++) {
ctx.lineTo(i, ys[i]);
}
ctx.stroke();
Run Code Online (Sandbox Code Playgroud)
它适用于 chrome、firefox、ie11,但我想知道代码的有效性和跨浏览器支持。我找不到任何提及它的内容,但我认为一定有提及它。
因此,我的问题是,应该ctx.moveTo在使用之前使用,还是仅先使用(之后)ctx.lineTo完全可以,为什么?(我找不到这个问题的答案,但很抱歉,如果它是重复的。)ctx.lineToctx.beginPath
python ×3
ansi-escape ×1
backticks ×1
bash ×1
c ×1
canvas ×1
filestream ×1
gcc ×1
java ×1
javascript ×1
key-bindings ×1
kml ×1
path ×1
self ×1
sqlite ×1
swing ×1
terminal ×1
xml ×1