有谁知道将光频率转换为RGB值的任何公式?
虽然强烈建议(W3C源,通过维基百科)Web服务器支持分号作为URL查询项的分隔符(除了&符号),但似乎通常不会遵循.
例如,比较
http://www.google.com/search?q=nemo & OE = UTF-8
http://www.google.com/search?q=nemo ; OE = UTF-8
结果.(在后一种情况下,分号是,或者在撰写本文时,被视为普通的字符串字符,就像网址是:http://www.google.com/search?q = nemo %3B oe = utf-8)
虽然我尝试了第一个URL解析库,但表现良好:
>>> from urlparse import urlparse, query_qs
>>> url = 'http://www.google.com/search?q=nemo;oe=utf-8'
>>> parse_qs(urlparse(url).query)
{'q': ['nemo'], 'oe': ['utf-8']}
Run Code Online (Sandbox Code Playgroud)
接受分号作为分隔符的当前状态是什么,哪些是潜在的问题或一些有趣的注释?(从服务器和客户端的角度来看)
有很多方法可以编写计算直方图的Python程序.
通过直方图,我的意思是一个函数,它计算a中对象的出现次数iterable
并输出字典中的计数.例如:
>>> L = 'abracadabra'
>>> histogram(L)
{'a': 5, 'b': 2, 'c': 1, 'd': 1, 'r': 2}
Run Code Online (Sandbox Code Playgroud)
编写此函数的一种方法是:
def histogram(L):
d = {}
for x in L:
if x in d:
d[x] += 1
else:
d[x] = 1
return d
Run Code Online (Sandbox Code Playgroud)
是否有更简洁的方法来编写此功能?
如果我们在Python中有字典理解,我们可以写:
>>> { x: L.count(x) for x in set(L) }
Run Code Online (Sandbox Code Playgroud)
但由于Python 2.6没有它们,我们必须写:
>>> dict([(x, L.count(x)) for x in set(L)])
Run Code Online (Sandbox Code Playgroud)
虽然这种方法可以读取,但效率不高:L经过多次.此外,这对单寿命发电机不起作用; 该函数应该对迭代器生成器同样有效,例如:
def gen(L):
for x in L:
yield x
Run Code Online (Sandbox Code Playgroud)
我们可能会尝试使用该reduce
函数(RIP):
>>> reduce(lambda d,x: dict(d, …
Run Code Online (Sandbox Code Playgroud) 是否可以从函数范围内访问python函数对象属性?
比如我们有
def f():
return SOMETHING
f._x = "foo"
f() # -> "foo"
Run Code Online (Sandbox Code Playgroud)
现在,如果我们想让_x属性内容"foo"返回,那么SOMETHING必须是什么?如果它甚至可能(简单)
谢谢
更新:
我也想做以下工作:
g = f
del f
g() # -> "foo"
Run Code Online (Sandbox Code Playgroud)
更新2:
声明不可能(如果是这种情况),以及为什么,比提供如何伪造它的方式更令人满意,例如使用与函数不同的对象
它可能发生在你身上,以及-有时当你复制一些网页中的文字转换为富文本电子邮件草案中你最喜欢的网络邮件客户端,你不喜欢粘贴的事实部分有不同的字体/尺寸/重量.它以某种方式记住了风格(通常是图像,选中时).如果将它粘贴到像Vim这样的喜爱的文本编辑器中,那么没有HTML,只有纯文本?
剪贴板似乎以各种格式维护所选数据.如何以任何一种格式(以编程方式或使用某些实用程序)访问数据?X11剪贴板是如何工作的?
假设有人(邪恶)为我们设置了一个计时器setInterval
,但是我们不知道它的ID(我们没有对该对象的引用,setInterval正在返回,也没有它的值)
(function(){
setInterval(function(){console.log('pwned')},
10000)
})();
Run Code Online (Sandbox Code Playgroud)
有办法,如何清除它?是否有可能以其他方式访问计时器?或者至少特别是浏览器/ javascript引擎?
大卫·弗拉纳根(David Flanagan)接触了他的大型JSTDG.
setInterval() method, use in malicious code
索引中的键指向
...某些浏览器会检测重复的对话框和长时间运行的脚本,并为用户提供停止它们的选项.但是恶意代码可以使用setInterval()等方法来加载CPU,也可以通过分配大量内存来攻击你的系统.Web浏览器没有一般的方法可以阻止这种火腿攻击.在实践中,这不是Web上的常见问题,因为没有人返回到从事此类脚本滥用的网站!
花了一些时间研究pycurl和libcurl文档,我仍然找不到(简单)方法,如何在pycurl中获取HTTP状态消息(reason-phrase).
状态代码很简单:
import pycurl
import cStringIO
curl = pycurl.Curl()
buff = cStringIO.StringIO()
curl.setopt(pycurl.URL, 'http://example.org')
curl.setopt(pycurl.WRITEFUNCTION, buff.write)
curl.perform()
print "status code: %s" % curl.getinfo(pycurl.HTTP_CODE)
# -> 200
# print "status message: %s" % ???
# -> "OK"
Run Code Online (Sandbox Code Playgroud) 我想知道什么是正确的pythonic向后兼容和向前兼容方法如何检查对象是否是编译re
对象.
isinstance
方法不能轻易使用,而生成的对象声称是_sre.SRE_Pattern
对象:
>>> import re
>>> rex = re.compile('')
>>> rex
<_sre.SRE_Pattern object at 0x7f63db414390>
Run Code Online (Sandbox Code Playgroud)
但没有这样的:
>>> import _sre
>>> _sre.SRE_Pattern
AttributeError: 'module' object has no attribute 'SRE_Pattern'
>>> import sre
__main__:1: DeprecationWarning: The sre module is deprecated, please import re.
>>> sre.SRE_Pattern
AttributeError: 'module' object has no attribute 'SRE_Pattern'
>>> re.SRE_Pattern
AttributeError: 'module' object has no attribute 'SRE_Pattern'
Run Code Online (Sandbox Code Playgroud)
我不想使用duck typing(即检查某些特定方法的可用性),因为这可能会与其他一些类型冲突.
现在,我正在使用:
>>> RegexpType = type(re.compile(''))
>>> type(rex) == RegexpType
True
Run Code Online (Sandbox Code Playgroud)
但可能有更好的方法..
在Python中,我们可以使用.strip()
字符串的方法来删除所选字符的前导或尾随出现:
>>> print " (Removes (only) leading & trailing brackets & ws ) ".strip(" ()")
'Removes (only) leading & trailing brackets & ws'
Run Code Online (Sandbox Code Playgroud)
我们如何在Ruby中做到这一点?Ruby的strip
方法不带参数,只剥离空格.
声称以下函数评估一个数是否为4的整数次幂.我不太明白它是如何工作的?
bool fn(unsigned int x)
{
if ( x == 0 ) return false;
if ( x & (x - 1) ) return false;
return x & 0x55555555;
}
Run Code Online (Sandbox Code Playgroud) python ×4
algorithm ×2
attributes ×1
c++ ×1
clipboard ×1
closures ×1
counting ×1
formula ×1
function ×1
histogram ×1
http ×1
http-status ×1
javascript ×1
libcurl ×1
math ×1
parsing ×1
pycurl ×1
query-string ×1
reduce ×1
regex ×1
rgb ×1
ruby ×1
scope ×1
setinterval ×1
string ×1
strip ×1
text ×1
timer ×1
types ×1
url ×1
webserver ×1
x11 ×1
xorg ×1