给定一个typeglob,我怎样才能找到实际定义的类型?
在我的应用程序中,我们将PERL用作简单的配置格式.我想要()用户配置文件,然后能够看到定义了哪些变量,以及它们是什么类型.
代码:(质量问题咨询)
#!/usr/bin/env perl
use strict;
use warnings;
my %before = %main::;
require "/path/to/my.config";
my %after = %main::;
foreach my $key (sort keys %after) {
next if exists $before{$symbol};
local *myglob = $after{$symbol};
#the SCALAR glob is always defined, so we check the value instead
if ( defined ${ *myglob{SCALAR} } ) {
my $val = ${ *myglob{SCALAR} };
print "\$$symbol = '".$val."'\n" ;
}
if ( defined *myglob{ARRAY} ) {
my @val = @{ *myglob{ARRAY} }; …Run Code Online (Sandbox Code Playgroud) 我发现在bash脚本中创建长流水线非常强大,但我看到的主要缺点是似乎没有办法插入注释.
例如,是否有一种向此脚本添加注释的好方法?
#find all my VNC sessions
ls -t $HOME/.vnc/*.pid \
| xargs -n1 \
| sed 's|\.pid$||; s|^.*\.vnc/||g' \
| xargs -P50 --replace vncconfig -display {} -get desktop \
| grep "($USER)" \
| awk '{print $1}' \
| xargs -n1 xdpyinfo -display \
| egrep "^name|dimensions|depths"
Run Code Online (Sandbox Code Playgroud) 我想捕获一个异常,但前提是它来自下一级逻辑.
目的是处理由错误数量的参数调用函数的行为引起的错误,而不掩盖函数实现生成的错误.
我该如何实现以下wrong_arguments功能?
例:
try:
return myfunc(*args)
except TypeError, error:
#possibly wrong number of arguments
#we know how to proceed if the error occurred when calling myfunc(),
#but we shouldn't interfere with errors in the implementation of myfunc
if wrong_arguments(error, myfunc):
return fixit()
else:
raise
Run Code Online (Sandbox Code Playgroud)
附录:
在简单的情况下,有几种解决方案可以很好地工作,但是当前的答案都不适用于装饰函数的真实情况.
考虑一下这些是myfunc上面可能的值:
def decorator(func):
"The most trivial (and common) decorator"
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
def myfunc1(a, b, c='ok'):
return (a, b, c)
myfunc2 = decorator(myfunc1)
myfunc3 = decorator(myfunc2) …Run Code Online (Sandbox Code Playgroud) 我想自动创建其中一个数据透视表:
https://drive.googleblog.com/2011/05/summarize-your-data-with-pivot-tables.html
给定一些数据和参数,该脚本将以非交互方式创建一个新的Google电子表格,其中包含数据透视表和图表.
这可能吗?
我有一些代码使用ctypes来尝试确定指向的文件sys.stdout是否实际 stdout.我知道在任何符合POSIX标准的系统上,甚至在Windows上,如果这样做应该是安全的sys.stdout.fileno() == 1,所以我的问题不是如何做到这一点.
在我的代码中(已经将ctypes用于与我的问题无关的东西)我不小心有类似的东西:
libc = ctypes.CDLL(ctypes.util.find_library('c'))
real_stdout = libc.fileno(ctypes.c_void_p.in_dll(libc, 'stdout'))
if sys.stdout.fileno() == real_stdout:
...
Run Code Online (Sandbox Code Playgroud)
这在Linux上完全正常,所以我并没有真正考虑它.它比硬编码1作为文件描述符看起来更好,更易读.但几天后我发现我的代码不能用于OSX.
它结束了OSX的libc不会导出任何名为'stdout'的符号.相反,它的stdio.h将stdout定义为:
#define stdout __stdoutp
Run Code Online (Sandbox Code Playgroud)
如果我将代码更改为c_void_p.in_dll(libc, '__stdoutp')我的代码按预期工作,但当然这只是OSX.事实证明,Windows有一个类似的问题(至少如果使用MSVC).
我可能只是改变我要使用的代码1,但我的问题仍然存在,出于好奇,如果有一种跨平台的方式来获取stdio指针(以及同样stdin和stderr)而不假设它使用符合POSIX的描述符?
当通过d3.js传递数据时,库将数据划分为输入/更新/退出组件,但我发现我们在更新部分中浪费了大量的计算,以便通过重新计算和重新计算来保持不变的值.将属性设置为已经是当前的相同值.
有没有什么好方法可以将"更新"的集合进一步划分为更改/未更改的集合?
我不明白以下行为.
locals()产生新的参考?locals()任何地方分配结果.X
import gc
from sys import getrefcount
def trivial(x): return x
def demo(x):
print getrefcount(x)
x = trivial(x)
print getrefcount(x)
locals()
print getrefcount(x)
gc.collect()
print getrefcount(x)
demo(object())
Run Code Online (Sandbox Code Playgroud)
输出是:
$ python demo.py
3
3
4
4
Run Code Online (Sandbox Code Playgroud) 以下代码的结果令我难以置信:
class MyClass(type):
@property
def a(self):
return 1
class MyObject(object):
__metaclass__ = MyClass
a = 2
print MyObject.a
print object.__getattribute__(MyObject, 'a')
print type.__getattribute__(MyObject, 'a')
print MyObject.__dict__['a']
print MyObject().a
Run Code Online (Sandbox Code Playgroud)
我真的希望这只是2重复打印,但它会打印出来1 1 1 2 2.有没有一种方法可以直观感觉?
澄清一下:我理解这种行为有很好的文档记录(这里是"数据描述符"),但我想了解为什么这有意义,以及核心开发人员为什么以这种方式实现描述符.
目前,当我们的项目的requirements.txt更新时,我们从头开始重新构建virtualenv以确保一致的结果.问题是我们的requirements.txt非常冗长,对它的更新通常只触及一个包.
是否有任何系统将virtualenv与requirements.txt进行比较并进行最少量的更改以使它们匹配?
我可以自己写这个,如果我能得到pip告诉我它会安装哪个版本给出一个requirements.txt,但我没有看到这样的选项.
我想将一个tarball上传到s3而不会产生临时文件的成本(我们在磁盘上很低).使用其他实用程序,我将tar命令的输出传递给upload命令,但它不适用于awscli:
$ echo ok | aws s3 cp /dev/stdin s3://my-bucket/test
upload failed: /dev/stdin to s3://my-bucket/test [Errno 29] Illegal seek
Run Code Online (Sandbox Code Playgroud)
有什么办法可以做我想要的吗?
python ×5
amazon-s3 ×1
aws-cli ×1
bash ×1
comments ×1
ctypes ×1
d3.js ×1
darwin ×1
decorator ×1
exception ×1
glob ×1
google-api ×1
google-docs ×1
javascript ×1
macos ×1
metaclass ×1
performance ×1
perl ×1
pip ×1
pipeline ×1
pivot-table ×1
posix ×1
refcounting ×1
sh ×1
try-catch ×1
typeglob ×1
virtualenv ×1