小编Luk*_*hne的帖子

Python列表问题

蟒蛇:

m=[[0]*3]*2
for i in range(3):
    m[0][i]=1

print m
Run Code Online (Sandbox Code Playgroud)

我希望这段代码可以打印出来

[[1, 1, 1], [0, 0, 0]]
Run Code Online (Sandbox Code Playgroud)

但它打印出来

[[1, 1, 1], [1, 1, 1]] 
Run Code Online (Sandbox Code Playgroud)

python list

10
推荐指数
2
解决办法
2700
查看次数

从运算符T&()中推导出const

问题是不同的编译器产生不同的输出(clang/gcc),因此我认为这种用法是未定义的行为.但是我的目标是const在分配参考时推断.

输出:
clang-3.6 - > not const
gcc-4.8.4 - >const

#include <iostream>
#include <type_traits>

struct AnyReference {

    template <typename RT> AnyReference(RT &a_var) : _ptr(&a_var) {}

    template <typename T> operator T &() const
    {
        if (std::is_const<T>::value) {
            std::cout << "const\n";
        }
        else {
            std::cout << "not const\n";
        }
        return *reinterpret_cast<T *>(_ptr);
    }
    void *_ptr;
};

int main()
{
    int i(5);
    AnyReference a(i);
    const int &c = a;
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

9
推荐指数
1
解决办法
169
查看次数

无法重载python socket.send

我们可以看到,send方法没有重载.

from socket import socket

class PolySocket(socket):

    def __init__(self,*p):
        print "PolySocket init"
        socket.__init__(self,*p)

    def sendall(self,*p):
        print "PolySocket sendall"
        return socket.sendall(self,*p)

    def send(self,*p):
        print "PolySocket send"
        return socket.send(self,*p)


    def connect(self,*p):
        print "connecting..."
        socket.connect(self,*p)
        print "connected"

HOST="stackoverflow.com"   
PORT=80
readbuffer=""

s=PolySocket()
s.connect((HOST, PORT))
s.send("a")
s.sendall("a")
Run Code Online (Sandbox Code Playgroud)

输出:

PolySocket init
connecting...
connected
PolySocket sendall
Run Code Online (Sandbox Code Playgroud)

python inheritance overloading

5
推荐指数
1
解决办法
1981
查看次数

Visual Studio调试执行路径

我有c代码.

FILE * fin = fopen("myfile", "rb");
if (fin == NULL)
{
    printf("file myfile does not exist, closing");
    return false;
}
Run Code Online (Sandbox Code Playgroud)

它在编译时有效,但在调试模式(步进低谷)中则返回false.检查argv [0]是好的,绝对路径是双向的.

c debugging visual-studio-2008 visual-studio

4
推荐指数
1
解决办法
7797
查看次数

CMAC 为什么选择 K1 和 K2

http://en.wikipedia.org/wiki/CMAC

http://www.rfc-archive.org/getrfc.php?rfc=4493

有两个键K1和K2。除了消息 1 与 10^127(1 和 127 个零)不同之外,还有其他原因吗

如果消息携带长度(并且长度也是 CMAC-ed 的消息),仅使用一个随机生成的 K 是否存在任何安全弱点?

cryptography cmac message-authentication-code

4
推荐指数
1
解决办法
3538
查看次数

从球坐标旋转体

是否可以旋转其顶点在球坐标中定义的主体.目前我正在用VHDL进行拼贴项目,关于旋转十二面体和呈现VGA.

我应用针孔相机模型方程,只需要两个正弦/余弦计算和每个顶点两次乘法.我只是考虑在两个角度上使用3个步骤围绕第3轴旋转,但我无法找出正确的方程式,即使这是可能的.

编辑

我想我明白了.

在计算它们时,在与摄像机方向相同的第3轴上旋转只是摄像机坐标的2D变换.这意味着比在3轴(ok两轴和一个倾斜)中旋转需要应用总共4个正弦/余弦计算和4次乘法.如果有人提出更好的东西,可以自由发布回答.

3d graphics geometry

4
推荐指数
2
解决办法
1万
查看次数

递归闭包(函数发生器)

我一直在学习函数式编程,我开始思考,组装数学运算符. counting -> addition -> multiplication -> power -> ... 自然地出来了简单和最天真的代码来表达这一点,它的工作原理!问题是我真的不知道为什么它能如此好地运行并且输出如此大的输出.

问题是:这个功能的复杂性是什么?

代码在python中:

def operator(d):
        if d<=1:
                return lambda x,y:x+y
        else:
                return lambda x,y:reduce(operator(d-1),(x for i in xrange(y)))


#test 
f1 = operator(1)       #f1 is adition
print("f1",f1(50,52))  #50+52

f2 = operator(2)      #f2 is multiplication
print("f2",f2(2,20))  #2*20

f3 = operator(3)      #f3 is power, just look how long output can be
print("f3",f3(4,100)) #4**100 

f4 = operator(4)      #f4 is superpower, this one does not work that well
print("f4",f4(2,6))   #((((2**2)**2)**2)**2)**2

f5 = operator(5)      #f5 do …
Run Code Online (Sandbox Code Playgroud)

python recursion complexity-theory closures functional-programming

4
推荐指数
1
解决办法
578
查看次数

理解python异步协议

我对 Python asyncio 和协议的工作方式缺乏了解。

似乎相关的文档: 状态机显示相关转换
class asyncio.Protocol
Protocol.eof_received()AbstractEventLoop.run_until_complete(future)

echo tcp 客户端的示例代码:

import asyncio

class EchoClientProtocol(asyncio.Protocol):
    def __init__(self, message, loop):
        self.message = message.encode()

    def connection_made(self, transport):
        self.transport = transport
        self.write_data()

    def data_received(self, data):
        print('Data received: {!r}',len(data))
        self.write_data()

    def eof_received(self):
        print("eof")
        return True

    def write_data(self):
        print("write")
        self.transport.write(self.message)

    def connection_lost(self, exc):
        print('The server closed the connection')
        print('Stop the event loop')


loop = asyncio.get_event_loop()
message = 'Hello World!'

coro = loop.create_connection(lambda: EchoClientProtocol(message, loop),
                              '127.0.0.1', 5676)
loop.run_until_complete(coro)
print("done")
Run Code Online (Sandbox Code Playgroud)

在回显服务器上连接时的输出:

write
Data received: {!r} …
Run Code Online (Sandbox Code Playgroud)

python python-asyncio

3
推荐指数
1
解决办法
4343
查看次数

向右转/我做错了什么?

不能正常工作,因为它没有设置MSB位正确.我正在使用metrowerks编译器.

//shifting right 5 characters
char * buffer;
buffer=global_buffer;
for(i=0;i<5;i++) //shift right for 1;
{
    buffer[17-i]=(buffer[17-i]>>1)|(buffer[17-i-1]<<7);
}
Run Code Online (Sandbox Code Playgroud)

EDIT输入缓冲区(就在for循环之前)0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x2F,0xA0,0xC6,0x9D

我得到了循环0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x17,0xD0,0xE3,0xCE

c bit-manipulation shift

1
推荐指数
1
解决办法
737
查看次数

这是双重铸造有效/正确的代码吗?

int compare_acd(const void *a, const void * b) 
{
     return ( (int)(*((ADC_VALUE_TYPE*)a)) - (int)(*((ADC_VALUE_TYPE*)b)) );
}
Run Code Online (Sandbox Code Playgroud)

在我的情况ADC_VALUE_TYPEuint16_t,但就是不能排除.

c

1
推荐指数
1
解决办法
93
查看次数