我正在做一个需要身份验证的应用程序.在应用程序的索引页面中,我指定了这样的访问规则
public function accessRules() {
return array(
array('deny',
'actions'=>array('index','register','login','password'),
'users'=>array('@'),
),
array('allow',
'users'=>array('*')
),
);
}
Run Code Online (Sandbox Code Playgroud)
在第一条规则中,操作'index','register','login'和'password'对于经过身份验证的用户是不可访问的.但是,我不想显示此消息
Unauthorized
You are not authorized to perform this action.
You do not have the proper credential to access this page.
If you think this is a server error, please contact the webmaster.
Run Code Online (Sandbox Code Playgroud)
...当经过身份验证的用户尝试访问这些操作时.相反,我想将它们重定向到另一个页面.如果我能在第一条规则上做这样的事情会很有用
array('redirect',
'actions'=>array('index','register','login','password'),
'users'=>array('@'),
'url'=>array('home/index'),
),
Run Code Online (Sandbox Code Playgroud) 我有两个数据帧,我想按列连接(轴= 1)与内连接.其中一个数据帧有一些重复的索引,但行不是重复的,我不想丢失那些数据:
df1 = pd.DataFrame([{'a':1,'b':2},{'a':1,'b':3},{'a':2,'b':4}],
columns = ['a','b']).set_index('a')
df2 = pd.DataFrame([{'a':1,'c':5},{'a':2,'c':6}],columns = ['a','c']).set_index('a')
>>> df1
b
a
1 2
1 3
2 4
8 9
>>> df2
c
a
1 5
2 6
Run Code Online (Sandbox Code Playgroud)
默认concat行为是使用NaN填充缺失值:
>>> pd.concat([df1,df2])
b c
a
1 2 NaN
1 3 NaN
2 4 NaN
1 NaN 5
2 NaN 6
Run Code Online (Sandbox Code Playgroud)
我想保留df1中的重复索引并用df2中的重复值填充它们,但是在pandas 0.13.1中,列上的内连接会产生错误.在更新版本的pandas concat做我想要的:
>>> pd.concat([df1, df2], axis=1, join='inner')
b c
a
1 2 5
1 3 5
2 4 6
Run Code Online (Sandbox Code Playgroud)
达到我想要的结果的最佳方法是什么?有没有groupby解决方案?或者也许我根本不应该使用concat?
有没有办法写一个python doctest字符串来测试一个脚本,该脚本打算从命令行(终端)启动,不会使用os.popen调用污染文档示例?
#!/usr/bin/env python
# filename: add
"""
Example:
>>> import os
>>> os.popen('add -n 1 2').read().strip()
'3'
"""
if __name__ == '__main__':
from argparse import ArgumentParser
p = ArgumentParser(description=__doc__.strip())
p.add_argument('-n',type = int, nargs = 2, default = 0,help = 'Numbers to add.')
p.add_argument('--test',action = 'store_true',help = 'Test script.')
a = p.parse_args()
if a.test:
import doctest
doctest.testmod()
if a.n and len(a.n)==2:
print a.n[0]+a.n[1]
Run Code Online (Sandbox Code Playgroud)
不使用popen运行doctest.testmod()只会导致测试失败,因为脚本是在python shell而不是bash(或DOS)shell中运行的.
LLNL的高级python课程建议将脚本放在与.py模块分开的文件中.但是doctest字符串只测试模块,没有arg解析.我的os.popen()方法污染了Examples文档.有没有更好的办法?
Python日志记录使用我在python中没有看到的语法格式化字符串,比如
'format': '%(name)s'
Run Code Online (Sandbox Code Playgroud)
有没有办法使用格式化程序截断错误消息,或者我是否需要覆盖LogRecord类?
这会截断部分消息(虽然我在正常位置找不到此功能的文档):
'format': '%(name).40s %(message).40s'
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我宁愿截断整个格式化的消息(例如,对于80列控制台).
我是Python的新手.实际上我用Java实现了一些东西,如下所示.
for(;;){
switch(expression){
case c1: statements
case c2: statements
default: statement
}
}
Run Code Online (Sandbox Code Playgroud)
我如何在Python中实现它?
我有一个这样的词典列表:
data = [{'x': 1, 'y': 10},
{'x': 3, 'y': 15},
{'x': 2, 'y': 1},
... ]
Run Code Online (Sandbox Code Playgroud)
我有一个函数(例如matplotlib.axis.plot)需要列表x和y值.所以我必须"转置"字典".
第一个问题:你怎么称呼这个操作?"转置"正确的术语吗?
我试过这个,但我正在寻找一种有效的方法(也许有一些特殊的numpy功能):
x = range(100)
y = reversed(range(100))
d = [dict((('x',xx), ('y', yy))) for (xx, yy) in zip(x,y)]
# d is [{'y': 99, 'x': 0}, {'y': 98, 'x': 1}, ... ]
timeit.Timer("[dd['x'] for dd in d]", "from __main__ import d").timeit()
# 6.803985118865967
from operator import itemgetter
timeit.Timer("map(itemgetter('x'), d)", "from __main__ import d, itemgetter").timeit()
# …Run Code Online (Sandbox Code Playgroud) 使用git bisect,我们可以放大提交之间可能引入的问题.
我想知道是否还有一种方法可以在一次提交中让git遍历(组合)文件,这样你就可以找出导致问题的文件/部分?
当前的项目要求我们尽可能使用Vega Visualization Grammar ,否则回退到 D3.js。3D 曲面图不是 Vega 示例库的一部分,但是Vega 文档让我乐观地认为我们可以创建自己的图形表示。有人可以告诉我如何使用 Vega 生成 3D 曲面图吗?你有什么建议的教程吗?或者,假设我有一个包含 100 个(x,y,z)元组的数据集,甚至可能是一个简短的秘诀?
编辑:目标是在点之间进行线性插值的准静态图形,在 R 中我们可能会像使用线框一样。
\n\n显然,我广泛使用了搜索引擎,但在\xce\xb1 Lyrae上看到了许多天文学研究的结果。
\n我说的是Doug Hellman 的 virtualenvwrapper。好吧,按照他的说法,一旦我们安装了 virtualenvwrapper,我们应该按照这里的解释编辑 .bashrc 文件
我们所做的是添加以下三行代码:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
Run Code Online (Sandbox Code Playgroud)
其中 .virtualenv 是放置 venv 的目录。Devel 是代码所在的目录。
观察 1:
当我执行时mkvirtualenv proj1,会在 .virtualenv 中创建一个名为 proj1 的目录,但不在 Devel 中。
观察 2:
当我这样做时mkproject proj1,会在 .virtualenv 和 Devel 中创建一个目录。
现在的问题:
请解释观察 1。
如果两个项目具有相同的要求并且我想使用单个 env 并且不想从创建另一个 virtualenv 开始并重新安装已安装的相同内容,该怎么办。我该怎么做呢?
Django汇总文档给出了以下示例,该示例计算Book与每个相关的s 的数量,Publisher并返回一个查询集,该查询集的前5名出版商均标有其图书计数(例如,在此字段中添加新的计数):
>>> pubs = Publisher.objects.annotate(num_books=Count('book')).order_by('-num_books')[:5]
>>> pubs[0].num_books
1323
Run Code Online (Sandbox Code Playgroud)
但是我只需要数一本特定类型的书。就像是。
>>> pubs = Publisher.objects.annotate(num_books=Count('book__type="nonfiction"')).order_by('-num_books')[:5]
Run Code Online (Sandbox Code Playgroud)
是否可以使用过滤器来完成此操作,还是需要使用原始SQL?
上面是与我的实际问题类似的docs示例,即当系统和医院都被建模为医疗机构时,通过其系统中医院的数量来识别和量化最大的医疗组Entity:
>>> biggest_systems = Entity.objects.filter(relationships__type_id=NAME_TO_TYPE_ID.get('hospital')).annotate(num_hospitals=Count('relationships')).order_by('-num_hospitals')[:5]
>>> biggest_systems[0].num_hospitals
25
Run Code Online (Sandbox Code Playgroud)
relationships 是具有穿透表的Entity M2M字段,type_id也是Entity中的字段:
class Entity(models.Model):
id = models.AutoField(verbose_name='ID',primary_key=True, db_column='ID')
type_id = models.IntegerField(verbose_name='detailed type',db_column='TypeID', blank=True, editable=True, choices=ENTITYTYPE_CHOICES, help_text="A category for this healthcare entity.")
relationships = models.ManyToManyField('self', verbose_name='affiliated with', through='EntityRelationship', symmetrical=False, related_name='related_to+')
Run Code Online (Sandbox Code Playgroud) python ×7
argparse ×1
concat ×1
d3.js ×1
dictionary ×1
django ×1
doctest ×1
formatter ×1
formatting ×1
git ×1
git-bisect ×1
git-rebase ×1
java ×1
logging ×1
optimization ×1
pandas ×1
php ×1
plot ×1
shell ×1
sql ×1
vega ×1
vega-lite ×1
virtualenv ×1
yii ×1