我会这样做:
def walk(samples):
for d in range(samples):
yield d
def walk200():
for d in walk(200):
yield d
Run Code Online (Sandbox Code Playgroud)
但实际上我想要的是这个,使代码更短:
def walk200():
reyield walk(200)
Run Code Online (Sandbox Code Playgroud)
我该怎么办reyield?
我试图理解某个OAuth2/web2py集成,但是python类模型中的一些怪癖让我很难理解.具体来说,我有这个web2py控制器:
def google():
if 'state' in request.vars and request.vars.state == 'google':
session.state = request.vars.state
person = Person("google")
print person.render()
return person.render()
Run Code Online (Sandbox Code Playgroud)
所以我们在Person这里使用这个课程.实现是这样的:
class Person(Base):
Run Code Online (Sandbox Code Playgroud)
没有__init__出现在Person类.该Base班有一个__init__功能:
class Base(object):
def __init__(
self,
hooks=[],
theme="%(name)s/",
view="app/generic",
meta=None,
context=None
):
from gluon.storage import Storage
self.meta = meta or Storage()
self.context = context or Storage()
self.context.alerts = []
self.context.content_types = []
self.context.categories = []
self.context.menus = []
self.context.internalpages = []
self.theme = theme
self.view = …Run Code Online (Sandbox Code Playgroud) 出于调试目的,我想将一个大字符串(一个难以可视化的session_id)映射到一个,比方说,6个字符"hash".这个哈希不需要以任何方式保证安全,只需要便宜的计算,以及固定和缩短的长度(md5太长).输入字符串可以具有任何长度.
你如何在python中实现这个"cheap_hash",这样计算起来并不昂贵?它应该生成这样的东西:
def compute_cheap_hash(txt, length=6):
# do some computation
return cheap_hash
print compute_cheap_hash("SDFSGSADSADFSasdfgsadfSDASAFSAGAsaDSFSA2345435adfdasgsaed")
aBxr5u
Run Code Online (Sandbox Code Playgroud) 我已经实现了这个算法:
def get_hot_pages(self, radius = 2):
if self.page == None or self.max_pages == None: return []
hot_pages = []
for page in xrange(self.page - radius, self.page + radius + 1):
if page < 0 or page >= self.max_pages : continue
hot_pages.append(page)
return hot_pages
Run Code Online (Sandbox Code Playgroud)
但有些事情告诉我,这可以更好地实施.是否有更多的pythonic方式来做到这一点?
(这里是ruby的新手)
我正在跑步CrunchBang Linux statler.
我已经卸载并重新安装compass了几次,只是为了确保.
这就是我做的:
» gem query
*** LOCAL GEMS ***
chunky_png (1.2.7)
fssm (0.2.10)
sass (3.2.5)
Run Code Online (Sandbox Code Playgroud)
然后:
» sudo gem install compass
Successfully installed compass-0.12.2
1 gem installed
Installing ri documentation for compass-0.12.2...
Installing RDoc documentation for compass-0.12.2...
Run Code Online (Sandbox Code Playgroud)
让我们检查:
» gem query
*** LOCAL GEMS ***
chunky_png (1.2.7)
compass (0.12.2)
fssm (0.2.10)
sass (3.2.5)
Run Code Online (Sandbox Code Playgroud)
和:
» gem which compass
/var/lib/gems/1.8/gems/compass-0.12.2/lib/compass.rb
Run Code Online (Sandbox Code Playgroud)
但:
» compass
-bash: compass: command not found
Run Code Online (Sandbox Code Playgroud)
那么,我做错了什么?
我有以下测试程序:
from rauth.service import OAuth1Service, OAuth2Service
SUPPORTED_SERVICES = {
'twitter' : ( 'OAuth1', 'twitter', 'https://api.twitter.com/oauth', 'request_token', 'access_token', 'authorize', 'https://api.twitter.com/1/', None),
'facebook' : ( 'OAuth2', 'facebook', 'https://graph.facebook.com/oauth', None, 'access_token', 'authorize', 'https://graph.facebook.com/', 'https://www.facebook.com/connect/login_success.html'),
'google' : ( 'OAuth2', 'google', 'https://accounts.google.com/o/oauth2', None, 'token', 'auth', None, 'http://localhost'),
}
CLIENT_DATA = {
'twitter' : ('dummy_client_id', 'dummy_client_secret'),
'facebook' : ('dummy_client_id', 'dummy_client_secret'),
'google' : ('dummy_client_id', 'dummy_client_secret'),
}
USER_TOKENS = {
'user1' : {
'twitter' : ('dummy_access_token', 'dummy_access_token_secret'),
'facebook' : ('dummy_access_token', None),
'google' : ('dummy_access_token', None),
}
}
def …Run Code Online (Sandbox Code Playgroud) 我有以下内容:
class A(object):
def x(self): print "Hello"
def y(self): self.x()
class Abis(A):
def x(self): print "Bye"
a = Abis()
a.x()
a.y()
Run Code Online (Sandbox Code Playgroud)
哪个印刷品:
Bye
Bye
Run Code Online (Sandbox Code Playgroud)
但我其实想要:
Bye
Hello
Run Code Online (Sandbox Code Playgroud)
因为我想A.y称之为"原创" A.x.我怎么可以参考原A.x中A,当派生类重载了吗?
在我的后端,我有一个可用的电话号码列表:/api/phonenumbers.我有一个模型:App.Phonenumber.我可以用我的所有电话号码App.Phonenumber.find();
现在我想过滤一下phonenumbers列表,只获取给定国家/城市的那些.后端能够通过接收/api/phonenumbers?country=DE&city=Berlin查询字符串来执行此过滤.
Phonenumber模型以便能够传递这些查询字符串参数?find使用这些查询参数执行?我期待 2 位小数精度,但是:
>>> from decimal import Decimal, getcontext
>>> getcontext().prec = 2
>>> Decimal(98791.4913)
Decimal('98791.491299999994225800037384033203125')
>>> getcontext()
Context(prec=2, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
Run Code Online (Sandbox Code Playgroud)
为什么不Decimal尊重Context精度?
这真的很奇怪:
$('form').on('submit', function(event) {
console.log('submit > this=%o', this);
});
Run Code Online (Sandbox Code Playgroud)
提交表单时不会显示任何控制台消息,但是:
$('form').on('submit', function(event) {
console.log('submit > this=%o', this);
event.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
会显示出来.这是为什么?代码必须在任何情况下执行,以便preventDefault有任何影响!
我只在Chrome中测试过,我会说这是Chrome漏洞.或者我错过了吗?
我正在尝试执行以下操作:
obscure ( ) {
local txt="$1"
echo "$txt" | tr '[:alnum:]' '*'
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做:
obscure 'mysecretstring'
Run Code Online (Sandbox Code Playgroud)
我明白了:
**************
Run Code Online (Sandbox Code Playgroud)
我可以使用什么匹配器tr,而不是[:alnum:]意味着"任何角色"?
有更好的实施方式obscure吗?想到的另一个选择是sed.
我想隐藏 URL 中的密码以用于日志记录。我希望通过解析、用虚拟密码替换密码并解析来使用urlparse,但这给了我:
>>> from urllib.parse import urlparse
>>> parts = urlparse('https://user:pass@66.66.66.66/aaa/bbb')
>>> parts.password = 'xxx'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
Run Code Online (Sandbox Code Playgroud)
所以替代方案似乎是这样,这似乎太过分了。
使用标准库是否有更简单的方法来替换密码?
这是我的实施:
def walk_two(a, b):
for x in a:
for y in b:
yield x, y
a = xrange(2)
b = xrange(3)
for x, y in walk_two(a, b):
print x, y
Run Code Online (Sandbox Code Playgroud)
有了这个输出:
0 0
0 1
0 2
1 0
1 1
1 2
Run Code Online (Sandbox Code Playgroud)
有更好的(更加pythonic)方式吗?一个内置的?更通用的walkN?
python ×9
bash ×1
class ×1
decimal ×1
ember-data ×1
ember.js ×1
forms ×1
gem ×1
generator ×1
javascript ×1
jquery ×1
logging ×1
oauth ×1
obfuscation ×1
overloading ×1
rauth ×1
ruby ×1
sed ×1
tr ×1
urlparse ×1