小编Pha*_*ani的帖子

如何制作一组列表

我有一个这样的列表列表:

i = [[1, 2, 3], [2, 4, 5], [1, 2, 3], [2, 4, 5]]
Run Code Online (Sandbox Code Playgroud)

我想得到一个包含"唯一"列表的列表(基于它们的元素),如:

o = [[1, 2, 3], [2, 4, 5]]
Run Code Online (Sandbox Code Playgroud)

我无法使用,set()因为列表中有不可清除的元素.相反,我这样做:

o = []
for e in i:
  if e not in o:
    o.append(e)
Run Code Online (Sandbox Code Playgroud)

有更简单的方法吗?

python

32
推荐指数
3
解决办法
3万
查看次数

如何查找psycopg2版本号

我使用apt-get在我的Ubuntu Natty机器上安装了psycopg2.现在,我想知道它的版本号.有人能说出找到这种python包的版本号的方法是什么.

python psycopg2

17
推荐指数
1
解决办法
2万
查看次数

Postgres连接

我正在尝试在PostgreSQL中进行简单的连接,并且它不断抛出错误消息.我不明白我在做错了什么.

select concat('abcde', 'fgh');
No function matches the given name and argument types. You might need to add explicit type casts.

select concat(cast('abcde' as text), cast('fgh' as text));
No function matches the given name and argument types. You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)

我正在使用Postgres版本8.4.11.请让我知道发生了什么.

postgresql

12
推荐指数
2
解决办法
2万
查看次数

top报告的Python线程的ID

我在Python脚本中启动了一堆不同的线程.我想跟踪每个线程的内存和CPU使用情况.我用它topps -eLf为此.

但事实证明,返回的标识符与其他类似程序thread.start_new_thread()显示的线程PID不同top.有没有办法从Python脚本中获取此PID?这样,我可以确定哪个PID属于哪个线程.

python linux multithreading ctypes

12
推荐指数
2
解决办法
4871
查看次数

在多个pcaps上使用pynids

我试图使用pynids库解析多个pcap文件,但可以只解析第一个文件.我看到nids_unregister_tcplibnids 中有一个函数,会有帮助吗?我在pynids中找不到这个功能.

import nids


def handle_tcp_stream(tcp):
    print "In handle_tcp_stream"


def extract(pcap_file):
    nids.param("tcp_workarounds", 1)
    nids.param("pcap_filter", "tcp")         # bpf restrict to TCP only, note
    nids.param("scan_num_hosts", 0)          # disable portscan detection
    nids.chksum_ctl([('0.0.0.0/0', False)])  # disable checksumming

    nids.param("filename", pcap_file)
    nids.init()
    nids.register_tcp(handle_tcp_stream)

    try:
        nids.run()
    except Exception, e:
        print "Exception ", pcap_file + " ", e


def main():
    extract("a.pcap")
    print "Done"
    extract("a.pcap")


if __name__ == "__main__":
    main()
Run Code Online (Sandbox Code Playgroud)

这是输出:

In handle_tcp_stream
In handle_tcp_stream
In handle_tcp_stream
In handle_tcp_stream
Done
Run Code Online (Sandbox Code Playgroud)

python tcp libnids

7
推荐指数
1
解决办法
958
查看次数

跨ipdb会话的历史记录

之前已经过这个问题,但我找不到一个好的答案.所以,我想再问一次.

我希望我的ipdb能够记住跨会话的命令.现在,它可以提取在Ipython会话中执行的命令,但不能从旧的ipdb会话中提取.如果我可以拥有此功能,那将节省大量时间.

有没有人有这个问题的解决方案?

python ipython ipdb

6
推荐指数
1
解决办法
571
查看次数

编写 GNU 屏幕脚本

我一直在尝试接受 GNU Screen 中的脚本编写。阅读手册页和其他一些示例后,我了解到我们可以使用命令at-X参数将命令发送到屏幕会话。

让我解释一下我的情况。我需要编写一个在现有屏幕会话中运行的脚本。该脚本应创建新窗口,设置其标题,浏览到特定目录并在每个窗口中运行程序。

该命令的问题at是我一次只能发送一个命令。当我使用命令创建新窗口时at,我将无法获取该新创建窗口的窗口号。因此,我将无法向该新窗口发送更多命令。如何检索这个新窗口的窗口号?

gnu-screen

5
推荐指数
1
解决办法
2792
查看次数

linux内核中UDP包的路径

我想在Linux内核中找到UDP数据包的路径.对于这一点,我想在一些文档(我读了这个迄今为止,这是TCP),然后在相应的内核函数的一些printk的声明,以确认.我将通过重新编译内核代码来完成此操作.

这是怎么回事?你有什么建议/参考吗?

networking udp linux-kernel

4
推荐指数
2
解决办法
8558
查看次数

Python没有释放Linux中的内存

我已经看过两年前发布的一些相关问题,但我想知道最近是否有任何解决方案.

我有一本庞大的字典词典.我的记忆中大约有4个词典(每个500 MB大小).当我继续运行程序时,我需要删除这4个字典中的一个并将内存释放到操作系统.因此,我不可能像以前的一些帖子中提到的那样开始一个新的内存分配子进程.

这里有一些代码来说明问题:

import cPickle
import resource
import gc
import time

mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print "memory usage:", mem
test_dict = {}
for i in range(100000):
    test_dict[i] = "AAAAAAAA"
    if i%10000 == 0:
        mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
        print "memory usage:", mem

mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print "memory usage: (dict created): ", mem
del test_dict
mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print "mem usage: (dict deleted)", mem
gc.collect()
mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print "mem usage (garbage collection)", mem
print "sleeping for a few seconds"
time.sleep(30)
gc.collect()
mem …
Run Code Online (Sandbox Code Playgroud)

python memory memory-management

2
推荐指数
1
解决办法
2141
查看次数

导入的代码无法打开其目录中的文件

我有以下文件结构:

test/
    test1.py
test2.py
text.txt
Run Code Online (Sandbox Code Playgroud)

以下是文件的内容

test1.py:

import sys
sys.path.append('../')
import test2

test2.read()
Run Code Online (Sandbox Code Playgroud)

test2.py:

def read():
    with open('text.txt', 'rb') as f:
        print f.read()

if __name__ == "__main__":
    read()
Run Code Online (Sandbox Code Playgroud)

text.txt包含一行文字.当我运行时test1.py,我收到"找不到文件"错误:

Traceback (most recent call last):
  File "test1.py", line 5, in <module>
    test2.read()
  File "../test2.py", line 2, in read
    with open('text.txt', 'rb') as f:
IOError: [Errno 2] No such file or directory: 'text.txt'
Run Code Online (Sandbox Code Playgroud)

我有点理解为什么会出现这个错误.但是我该如何处理这些错误呢.我希望代码test2.py就像我可以在任何地方使用的库代码.

python import

0
推荐指数
1
解决办法
438
查看次数