我有这个html片段需要在jinja2模板中的很多地方使用:
<div class="usedalot">{{ somevalue }}</div>
Run Code Online (Sandbox Code Playgroud)
例如,在template1.html,template2.html中.template3.html,这段代码重复了几个地方
<!-- template1.html, template2.html. template3.html -->
<div class="usedalot">{{ somevalue }}</div>
......
<div class="usedalot">{{ somevalue }}</div>
....
<div class="usedalot">{{ somevalue }}</div>
......
Run Code Online (Sandbox Code Playgroud)
而不是复制和粘贴,有没有什么可以使用此代码片段作为块?jinja2模板继承(带块)似乎没有解决这个问题.
我尝试使用Python中的日志记录来编写一些日志,但奇怪的是,只会error记录下来,info无论我设置哪个级别,都会被忽略.
码:
import logging
import logging.handlers
if __name__ == "__main__":
logger = logging.getLogger()
fh = logging.handlers.RotatingFileHandler('./logtest.log', maxBytes=10240, backupCount=5)
fh.setLevel(logging.DEBUG)#no matter what level I set here
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info('INFO')
logger.error('ERROR')
Run Code Online (Sandbox Code Playgroud)
结果是:
2014-01-14 11:47:38,990 - root - ERROR - ERROR
Run Code Online (Sandbox Code Playgroud)
根据http://docs.python.org/2/library/logging.html#logging-levels
该INFO也应该被记录下来.
如果您看一下简单DLL注入的以下工作代码:
//Open the target process with read , write and execute priviledges
Process = OpenProcess(PROCESS_CREATE_THREAD|PROCESS_QUERY_INFORMATION|PROCESS_VM_READ|PROCESS_VM_WRITE|PROCESS_VM_OPERATION, FALSE, ID);
//Get the address of LoadLibraryA
LoadLibrary = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
// Allocate space in the process for our DLL
Memory = (LPVOID)VirtualAllocEx(Process, NULL, strlen(dll)+1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
// Write the string name of our DLL in the memory allocated
WriteProcessMemory(Process, (LPVOID)Memory, dll, strlen(dll)+1, NULL);
// Load our DLL
CreateRemoteThread(Process, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibrary, (LPVOID)Memory, NULL, NULL);
//Let the program regain control of itself
CloseHandle(Process); …Run Code Online (Sandbox Code Playgroud) 我需要为Flask路由函数添加一个python装饰器,(基本上我从这里编辑了代码)
def requires_admin(f):
def wrapper(f):
@wraps(f)
def wrapped(*args, **kwargs):
#if not admin:
#return render_template('error.html')
return f(*args, **kwargs)
return wrapped
return wrapper
Run Code Online (Sandbox Code Playgroud)
并使用它就像这样就可以了:
@app.route('/admin/action')
@requires_admin
def AdminAction():
#NO error if NO parameter
Run Code Online (Sandbox Code Playgroud)
但是使用它会有这样的错误:
@app.route('/admin/action/<int:id>')
@requires_admin
def AdminAction(id):
Run Code Online (Sandbox Code Playgroud)
在Flask 0.10中,我得到这样的错误(我刚从Flask 0.9更新到0.10,而在Flask 0.9中没有像这样的语法错误):
@requires_admin
File "/usr/local/lib/python2.6/dist-packages/Flask-0.10.1-py2.6.egg/flask/app.
py", line 1013, in decorator
self.add_url_rule(rule, endpoint, f, **options)
File "/usr/local/lib/python2.6/dist-packages/Flask-0.10.1-py2.6.egg/flask/app.
py", line 62, in wrapper_func
return f(self, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/Flask-0.10.1-py2.6.egg/flask/app.
py", line 984, in add_url_rule
'existing endpoint function: %s' % endpoint)
AssertionError: View …Run Code Online (Sandbox Code Playgroud) 最近我在CSS初学者的书中看到了这一点(由Simon Collison开始的CSS Web开发,第19页)
h1#title{color:red;}
Run Code Online (Sandbox Code Playgroud)
这是一种element#id结构.
但这有必要吗?由于ID在一个html网页中是唯一的,因此element不是必需的.
我的理解是对的吗?
我有两个列表,第一个列表是键顺序,第二个列表是元组列表.
colorOrder = ['red', 'blue', 'yellow', 'green']
tupleList = [(111,'red'),(222,'pink'),(333,'green')]
Run Code Online (Sandbox Code Playgroud)
请注意这两个列表不是一对一的关系.有些颜色不在colorOrder,有些颜色colorOrder从未出现过tupleList.所以它与其他类似的重复问题不同.
我需要根据colorOrder对tupleList进行排序.
我可以使用两个嵌套for循环来解决这个问题,但需要一个更有效的解决方案.
#First sort according to the color order
for aColor in colorOrder:
for aTuple in tupleList:
if aTuple[1] == aColor:
ResultList.append(aTuple)
#Second add the tuples to the ResultList, whose color is not in the colorOrder
for aTuple in tupleList:
if aTuple[1] not in colorOrder:
ResultList.append(aTuple)
Run Code Online (Sandbox Code Playgroud) using (T resource = expression)
embedded-statement
Run Code Online (Sandbox Code Playgroud)
被编译器翻译为以下代码:
{
T resource = expression;//Shouldn't this statement be moved inside the try block?
try
{
embedded-statement
}
finally
{
if (resource != null)
((IDisposable)resource).Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
{}我的问题是:为什么try 块周围有多余的东西?第一个语句不应该移到 try 块内吗?
MSDN解释道:
前面的代码示例在编译时扩展为以下代码 (请注意额外的花括号来创建对象的有限范围):
但根据另一个MSDN页面,
通过使用finally块,您可以清理try块中分配的所有资源
更新:如果变量可见性是原因,那么我们首先声明变量并将其分配为 null,然后在 try 块内初始化它怎么样?这比原来的代码好吗?
{
T resource = null;//Now it is visible in the try block
try
{
resource =expression;// in case an exception is thrown here …Run Code Online (Sandbox Code Playgroud) 我正在使用 WinPcap 学习网络编程。这是片段:
int ip_hlen = (ih->ver_ihl & 0xf) * 4; /* get ip header length */
tcp_header *th = (tcp_header *) ((u_char*)ih + ip_len);
int tcp_hlen = (ntohs(th->th_len_resv_code) & 0xf000) >> 12)*4; /* get tcp header length */
Run Code Online (Sandbox Code Playgroud)
问题是为什么ntohs只有在获取 tcp_hlennot时才使用ip_hlen。
事实上,ntohs只接受u_short作为参数解释一点。http://msdn.microsoft.com/en-us/library/windows/desktop/ms740075%28v=vs.85%29.aspx
我仍然对何时使用 ntohs 何时不使用感到困惑。
以下是 IP 和 TCP 数据包定义的结构:
/* ipv4 header */
typedef struct ip_header {
u_char ver_ihl; /* version and ip header length */
u_char tos; /* …Run Code Online (Sandbox Code Playgroud) 为什么Windows中的某些功能需要pWnd参数而其他功能需要HWND参数?例:
// Get a dc for a CWnd object pointer.
CPaintDC dc(pWnd);
// Send my private massage.
::SendMessage(pWnd->m_hWnd, WM_MYMESSAGE, (LPARAM) &dc.m_ps, 0);
Run Code Online (Sandbox Code Playgroud)
其实,为什么Windows系统有两种类型的pWnd和HWND?
由于HWND可以通过pWnd->m_hWnd,所以函数总是可以pWnd作为参数.它背后的设计理念是什么?