我需要为用户组实现用户权限(非常类似于facebook组).例如,每个组可以拥有具有以下权限的成员:can_post,can_delete,can_ban等.当然,一个用户可以是许多组的成员,并且组可以拥有许多具有不同权限的不同用户.我需要什么型号才能使用此功能?
最近,我发现''.format
函数非常有用,因为与%
格式化相比,它可以提高可读性.试图实现简单的字符串格式化:
data = {'year':2012, 'month':'april', 'location': 'q2dm1'}
year = 2012
month = 'april'
location = 'q2dm1'
a = "year: {year}, month: {month}, location: {location}"
print a.format(data)
print a.format(year=year, month=month, location=location)
print a.format(year, month, location)
Run Code Online (Sandbox Code Playgroud)
虽然两个第一次打印的格式符合我的预期(是的,something=something
看起来很难看,但这只是一个例子),最后一个会提高KeyError: 'year'
.在python中有没有创建字典的技巧,所以它会自动填充键和值,例如somefunc(year, month, location)
输出{'year':year, 'month': month, 'location': location}
?
我是python的新手,无法找到关于这个主题的任何信息,但是像这样的技巧会大大改善和缩小我当前的代码.
提前谢谢并原谅我的英语.
我有一个项目列表,我想从中删除所有类似的值,但第一个和最后一个.例如:
listIn = [1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1]
Run Code Online (Sandbox Code Playgroud)
导致:
listOut = [1, 1, 0, 0, 1, 0, 0, 1]
Run Code Online (Sandbox Code Playgroud)
在c ++中这样做的方式非常明显,但它看起来与python编码风格有很大不同.或者这是唯一的方法?
基本上,只需删除图中"y"值未更改的过多点:
我为我的应用程序类使用extern变量,所以我可以将类函数转发到glutDisplayFunction(funcPtr).
main.cpp中:
#include "main.hpp"
int main(int argc, char** argv)
{
gApp = new GameApp();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
main.hpp:
#ifndef MAIN_HPP
#define MAIN_HPP
#include "GameApp.hpp"
#endif
Run Code Online (Sandbox Code Playgroud)
GameApp.hpp:
#include <GL/gl.h>
#include <GL/freeglut.h>
class GameApp
{
public:
int running;
GameApp();
virtual ~GameApp();
void resize(int width, int height);
void init(int argc, char** argv, int width, int height);
void draw();
void update();
void key_input(unsigned char key, int x, int y);
};
extern GameApp *gApp;
void display_clb()
{
if (!gApp)
{
return;
}
gApp->draw();
}
Run Code Online (Sandbox Code Playgroud)
这是输出: …
我想知道,是否有可能实现类似的位操作:
if a > maximum: a = maximum
Run Code Online (Sandbox Code Playgroud)
"最大值"可以是随机数吗?
在我当前的代码中有许多类似的行.当然可以使用:
def foo(a, max=512): return a if a<max else max
Run Code Online (Sandbox Code Playgroud)
只是好奇,如果有一个更优雅和有效的方式.
python ×4
bit ×1
c++ ×1
dictionary ×1
django ×1
extern ×1
formatting ×1
operations ×1
string ×1
usergroups ×1