我可以更改Python脚本的有效进程名称吗?当我获得系统进程列表时,我想显示一个不同的名称而不是进程的真实名称.在CI中可以设置
strcpy(argv[0],"othername");
Run Code Online (Sandbox Code Playgroud)
但在Python中
argv[0] = "othername"
Run Code Online (Sandbox Code Playgroud)
似乎不起作用.当我获得进程列表(ps ax
在我的linux框中)时,真实姓名不会改变.如果存在,我更喜欢便携式解决方案(或者一个用于posix的解决方案和另一个用于Windows环境的解决方案).
提前致谢
我正在运行这个简单的代码:
import threading, time
class reqthread(threading.Thread):
def run(self):
for i in range(0, 10):
time.sleep(1)
print('.')
try:
thread = reqthread()
thread.start()
except (KeyboardInterrupt, SystemExit):
print('\n! Received keyboard interrupt, quitting threads.\n')
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,它会打印出来
$ python prova.py
.
.
^C.
.
.
.
.
.
.
.
Exception KeyboardInterrupt in <module 'threading' from '/usr/lib/python2.6/threading.pyc'> ignored
Run Code Online (Sandbox Code Playgroud)
实际上python线程忽略我的Ctrl+ C键盘中断而不打印Received Keyboard Interrupt
.为什么?这段代码有什么问题?
我无法获取文本值Node.getNodeValue()
,Node.getFirstChild().getNodeValue()
或者用Node.getTextContent()
.
我的XML就像
<add job="351">
<tag>foobar</tag>
<tag>foobar2</tag>
</add>
Run Code Online (Sandbox Code Playgroud)
而我正在尝试获取标记值(非文本元素提取工作正常).我的Java代码听起来像
Document doc = db.parse(new File(args[0]));
Node n = doc.getFirstChild();
NodeList nl = n.getChildNodes();
Node an,an2;
for (int i=0; i < nl.getLength(); i++) {
an = nl.item(i);
if(an.getNodeType()==Node.ELEMENT_NODE) {
NodeList nl2 = an.getChildNodes();
for(int i2=0; i2<nl2.getLength(); i2++) {
an2 = nl2.item(i2);
// DEBUG PRINTS
System.out.println(an2.getNodeName() + ": type (" + an2.getNodeType() + "):");
if(an2.hasChildNodes())
System.out.println(an2.getFirstChild().getTextContent());
if(an2.hasChildNodes())
System.out.println(an2.getFirstChild().getNodeValue());
System.out.println(an2.getTextContent());
System.out.println(an2.getNodeValue());
}
}
}
Run Code Online (Sandbox Code Playgroud)
打印出来
tag type …
Run Code Online (Sandbox Code Playgroud) 有人使用libapt或libept来列出包并在类似debian的系统中获取有关包的信息吗?
Libapt没有详细记录,我发现很少有关于libept的例子和教程.有人可以解释我最好的方法
直接使用apt内部文件非常简单,但我想使用库来遵守apt规范.
我用a QTreeWidget
来显示一些简单的项目.我通过.setSortingEnabled(true)
调用将列表排序为可排序.这样,列表仅在用户按标题列时排序,而不是在插入新项目时自动排序.
有没有办法强制在指定列中自动排序而不调用.sortItems(column)
每个项目插入?
如果可能,我会突出显示整个排序列.
我必须清除并重绘一个raphael javascript主容器.我试过了
var paper = Raphael(10, 50, 320, 200); paper.remove(); // Doesn't work paper.node.removeNode(); //this neither paper.removeNode(); //this neither
任何的想法?
使用提供的位替换字节的最低有效位的最佳方法是什么?
我知道如何检查和比较最后一位(使用例如posix ffs()函数),但我想知道是否有更好性能的解决方案,而不检查替换位是0还是1.
该示例以python编写为伪代码,但我将在C中实现工作算法:
>>> bin(0b1) # bit is '0b1'
>>> bin(128) # byte is '0b10000000'
>>> bin(129) # byte is '0b10000001'
>>> bin(128 OPERATOR 0b1) # Replace LSB with 1
'0b10000001'
>>> bin(128 OPERATOR 0b0) # Keep LSB at 0
'0b10000000'
>>> bin(129 OPERATOR 0b1) # Keep LSB at 1
'0b10000001'
>>> bin(129 OPERATOR 0b0) # Replace LSB with 0
'0b10000000'
Run Code Online (Sandbox Code Playgroud)
显然,运算符可以是一组运算,但我正在寻找最优(最快)的方法.
我无法理解为什么这几行
Date submissionT;
SimpleDateFormat tempDate = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
public time_print(String time) {
try {
submissionT=tempDate.parse(time);
}
catch (Exception e) {
System.out.println(e.toString() + ", " + time);
}
}
Run Code Online (Sandbox Code Playgroud)
导致异常并打印出来
java.text.ParseException: Unparseable date: "Tue Mar 31 06:09:00 CEST 2009", Tue Mar 31 06:09:00 CEST 2009
Run Code Online (Sandbox Code Playgroud)
...虽然"不可解决的"时间符合我传递给SimpleDateFormat()的格式字符串..任何想法?
为什么这个使用os.setuid()/ gid()的简单程序会失败?是用python编写的,但我认为这不是语言相对问题(最后都是相同的posix系统调用):
import os, pwd
if os.getenv("SUDO_UID") and os.getenv("SUDO_GID"):
orig_uid=int(os.getenv("SUDO_UID"))
orig_gid=int(os.getenv("SUDO_GID"))
else:
pw = pwd.getpwnam("nobody")
orig_uid = pw.pw_uid
orig_gid = pw.pw_gid
print os.getuid(), os.getgid(), os.geteuid(), os.getegid(), orig_uid, orig_gid
os.setgid(orig_gid)
os.setuid(orig_uid)
Run Code Online (Sandbox Code Playgroud)
它返回此异常:
$ sudo python provgid.py
0 0 0 0 1000 1000
Traceback (most recent call last):
File "provgid.py", line 15, in <module>
os.setgid(orig_gid)
OSError: [Errno 1] Operation not permitted
Run Code Online (Sandbox Code Playgroud)
错误是什么?
我想用来register_tick_function()
挂钩以下调用并打印他们的堆栈跟踪debug_backtrace()
.
如果我运行以下代码.
<?php
function dump() {
// Replace this with var_dump(debug_backtrace()); to print the entire trace.
foreach (debug_backtrace() as $trace)
echo("Function ${trace['function']}() has been called" . PHP_EOL);
}
declare(ticks = 1);
register_tick_function('dump');
print("");
array_search('green', Array());
Run Code Online (Sandbox Code Playgroud)
它只打印dump()
功能.
Function dump() has been called
Function dump() has been called
Function dump() has been called
Run Code Online (Sandbox Code Playgroud)
为什么我没有看到print()
和array_search()
跟踪数据?就像堆栈在调用之前已经重置一样dump()
.我也很确定它在过去是否正常工作.