例如qa_sharutils-2009-04-22-15-20-39
,想要砍掉最后20个字节,然后得到' qa_sharutils
'.
我知道如何在sed中做到这一点,但为什么$A=${A/.\{20\}$/}
不起作用?
谢谢!
将pypy实现转换为c文件并在现代笔记本上构建pypy-c需要几个小时,具有2G内存和Intel Core2 2GHz CPU.
我知道这是一项cpu密集型任务,但是它必须这么慢吗?有没有机会或空间来减少计算,重新安排计算顺序并将时间缩短到几十分钟?
def foo(a, b, c = 0):
return a+b
Run Code Online (Sandbox Code Playgroud)
我有很多像'foo'这样的函数,它们都有不同的参数编号和名称.有没有一种常见的方法可以获得这些函数的返回值,并只对它们执行一个额外的操作,如pformat?
是的我可以生成如下的新功能:
func = ... # func can be got using getattr by name
def wrapper(*arg, **kw):
data = func(*arg, **kw)
return pprint.pformat(data)
return wrapper
Run Code Online (Sandbox Code Playgroud)
但是新函数'wrapper'与旧的'func'不同,例如,在参数编号中,'wrapper'只有2个args - 'arg'和'kw',但'func'可能有很多args ,像'a','b','c'.
我只想玩返回值,其他一切应该保持不变,是否可能?
谢谢!
更新 最后使用装饰模块和以下补丁解决了这个问题:
--- /home/jaime/cache/decorator-3.2.0/src/decorator.py 2010-05-22 23:53:46.000000000 +0800
+++ decorator.py 2010-10-28 14:55:11.511140589 +0800
@@ -66,9 +66,12 @@
self.name = '_lambda_'
self.doc = func.__doc__
self.module = func.__module__
- if inspect.isfunction(func):
+ if inspect.isfunction(func) or inspect.ismethod(func):
argspec = inspect.getargspec(func)
self.args, self.varargs, self.keywords, …
Run Code Online (Sandbox Code Playgroud) 我想在程序名称和参数中转义 '"' 和所有其他通配符,所以我尝试双引号它们。我可以在 cmd.exe 中执行此操作
C:\bay\test\go>"test.py" "a" "b" "c"
hello
['C:\\bay\\test\\go\\test.py', 'a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)
但是以下使用 os.sytem 的代码有什么问题?
cmd = '"test.py" "a" "b" "c"'
print cmd
os.system(cmd)
Run Code Online (Sandbox Code Playgroud)
它的输出:
C:\bay\test\go>test2.py
"test.py" "a" "b" "c"
'test.py" "a" "b" "c' is not recognized as an internal or external command,
operable program or batch file.
Run Code Online (Sandbox Code Playgroud)
为什么整个字符串 '"test.py" "a" "b" "c"' 被识别为单个命令?但下面的例子不是:
cmd = 'test.py a b c'
print cmd
os.system(cmd)
C:\bay\test\go>test2.py
test.py a b c
hello
['C:\\bay\\test\\go\\test.py', 'a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)
谢谢!
例如,我在gnome中打开pdf文件或网页,使用鼠标双击一些文本,因此选择了一个单词,如何在后台运行使用python-dbus编写的守护进程中获取该单词?
一些简单但有效的脚本非常受欢迎.
谢谢!