我的选择表单存在问题.当我选择任何选项时,传递的值是字符串类型,而不是数字类型.这是我用于ng-repeat的对象:
$scope.apples ={
0:'Apple0',
1:'Apple1',
2:'Apple2',
3:'Apple3'
};
Run Code Online (Sandbox Code Playgroud)
这是我的选择,它传递字符串,而不是数字:
<select ng-model="test" ng-options="key as value for (key,value) in apples"></select>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我想在 C++ 中创建一个std::vector(特别是 a std::vector<string>),然后将它(或者更确切地说,指向它的指针)传递给 Javascript,以便能够从直接编写的 Javascript 访问其数据/函数。
到目前为止我可以获得整数指针:
vector<string> myVector;
myVector.push_back("First item");
myVector.push_back("Second item");
EM_ASM_ARGS({
// Prints out an integer value of the pointer,
// but I would like to access the object members/data
// of myVector
console.log($0);
}, &myVector);
Run Code Online (Sandbox Code Playgroud)
我在http://kripken.github.io/emscripten-site/docs/api_reference/bind.h.html#register_vector__cCP和http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript找到了信息/embind.html#built-in-type-conversions关于该register_vector函数
#include <emscripten/bind.h>
EMSCRIPTEN_BINDINGS(Wrappers) {
register_vector<std::string>("VectorString");
};
Run Code Online (Sandbox Code Playgroud)
它可以用来从 Javascript 世界创建一个新的向量:
var myVector = new Module.VectorString();
Run Code Online (Sandbox Code Playgroud)
但我不知道如何使用它来访问 C++ 世界中已经存在的向量。
Module.Runtime.dynCall('v', $0, []);如果我尝试在指针上使用,如/sf/answers/2052360831/中传递函数指针时,则会出现错误:
Invalid function pointer '380' called …
如何过滤列表,保留与模式匹配的项目,删除不匹配的项目?
例如,如果我有一个列表列表,我想要一个函数......
singleItemLists :: [[Int]] -> [[Int]]
Run Code Online (Sandbox Code Playgroud)
...删除所有与模式不匹配的项目[x]。
理想情况下,将答案转换为其他模式应该相当简单。
下面的代码片段使用 Cryptodome 进行 AES 解密,效果符合我的预期:
from Crypto.Cipher import AES
from Crypto.Util import Counter
key = b'\x12' * 32
decryptor = AES.new(key, AES.MODE_CTR,counter=Counter.new(nbits=128, little_endian=True))
print(decryptor.decrypt(b'Something encrypted'))
Run Code Online (Sandbox Code Playgroud)
下面使用Python Cryptography,并给出不同的结果:
from cryptography.hazmat.primitives.ciphers import Cipher, modes, algorithms
key = b'\x12' * 32
decryptor = Cipher(algorithms.AES(key), modes.CTR(b'\0' * 16)).decryptor()
print(decryptor.update(b'Something encrypted'))
Run Code Online (Sandbox Code Playgroud)
为什么?如何更改 Python Cryptography 版本以输出与 Cryptodome 相同的结果?
(上下文正在编写用于解压缩文件的 AES 解密代码,并且正在考虑使用 Python 加密)
将列表monad绑定函数应用于简单列表和标识函数时:
[[1,2],[3,4]] >>= \x -> x
Run Code Online (Sandbox Code Playgroud)
我明白了
[1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
但是,Monad类型的定义:
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
Run Code Online (Sandbox Code Playgroud)
似乎建议函数,在我的情况下lambda函数\x -> x,应该返回一个不同的类型传入.我希望在这种情况下编译器错误,但我没有.我在ghci中运行它.
为什么编译器在这种情况下不会抛出错误?
我正在尝试重复运行状态转换,直到状态不变.我搜索Hoogle了
Eq s => State s () -> State s ()
Run Code Online (Sandbox Code Playgroud)
但没有发现看起来合适的东西.
如何反复运行状态转换,直到它没有改变?
我正在编写一个用于分页的查询,其中典型的页面将由如下查询返回:
SELECT col_a, col_b
WHERE (col_a = 'value_a' AND col_b > 'value_b') OR (col_a > 'value_a')
ORDER BY col_a, col_b
LIMIT 50
Run Code Online (Sandbox Code Playgroud)
让我有点惊讶的是,没有一种更简洁的方法来减少WHERE重复的条款。有没有?
是否可以通过一定量的流来 gzip 数据,即无需立即将所有压缩数据加载到内存中?
例如,我可以在具有 2GB 内存的计算机上对一个 10GB 大小的文件进行 gzip 压缩吗?
在https://docs.python.org/3/library/gzip.html#gzip.compress,该gzip.compress函数返回 gzip 的字节,因此必须全部加载到内存中。但是......尚不清楚gzip.open内部是如何工作的:压缩的字节是否会同时全部存储在内存中。gzip 格式本身是否使得实现流式 gzip 变得特别困难?
[这个问题用Python标记,但也欢迎非Python答案]
我正在编写一个应该使用 inotify 响应文件更改的程序。下面的骨架程序按我的预期工作......
# test.py
import asyncio
import ctypes
import os
IN_CLOSE_WRITE = 0x00000008
async def main(loop):
libc = ctypes.cdll.LoadLibrary('libc.so.6')
fd = libc.inotify_init()
os.mkdir('directory-to-watch')
wd = libc.inotify_add_watch(fd, 'directory-to-watch'.encode('utf-8'), IN_CLOSE_WRITE)
loop.add_reader(fd, handle, fd)
with open(f'directory-to-watch/file', 'wb') as file:
pass
def handle(fd):
event_bytes = os.read(fd, 32)
print(event_bytes)
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
Run Code Online (Sandbox Code Playgroud)
...因为它输出...
b'\x01\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00file\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其更改为尝试读取 31 个字节...
b'\x01\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00file\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Run Code Online (Sandbox Code Playgroud)
......然后它引发了一个异常......
Traceback (most recent call last):
File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/t.py", line 19, in handle
event_bytes = os.read(fd, 31)
OSError: …Run Code Online (Sandbox Code Playgroud) haskell ×3
monads ×2
python ×2
aes ×1
angularjs ×1
c++ ×1
compression ×1
cryptography ×1
ctypes ×1
emscripten ×1
ghci ×1
gzip ×1
inotify ×1
javascript ×1
linux ×1
ng-options ×1
pagination ×1
postgresql ×1
pycryptodome ×1
python-3.x ×1
select ×1
sorting ×1
sql ×1
tuples ×1