Analysis Note #2

我可不是按顺序发布的啊

Screen Shot 2017-10-08 at 22.10.30

Screen Shot 2017-11-18 at 13.50.10

Advertisements

Rust: Access the last value of a slice?

记录一下今后可能会踩的坑:

fn last<T: Clone>(slice: &[T]) -> T {
    slice[slice.len()-1].clone()
}

不加 .clone() 是不行的,因为 slice[slice.len()-1] 的值是属于 slice 的,不可移出。所以只能将其 copy 一份返回出来。我有预感以后一定会有忘记 .clone() 的时候……

 

Ref:

https://users.rust-lang.org/t/error-e0507-cannot-move-out-of-indexed-content/12815/2

Python: Handle binary data

165089struct module: pack, unpack

struct

struct.pack(fmt, v1, v2, )

Return a bytes object containing the values v1, v2, … packed according to the format string fmt. The arguments must match the values required by the format exactly.

 

struct.unpack(fmt, buffer)

Unpack from the buffer buffer (presumably packed by pack(fmt, ...)) according to the format string fmt. The result is a tuple even if it contains exactly one item. The buffer’s size in bytes must match the size required by the format

 

struct.calcsize(fmt)

Return the size of the struct (and hence of the bytes object produced bypack(fmt, ...)) corresponding to the format string fmt.

 

fmt table:

Screen Shot 2017-09-21 at 16.54.52

Example:

>>> import struct
>>> struct.pack('B', 65)
b'A'
>>> struct.pack('BB', 65, 66)
b'AB'

>>> struct.unpack('h', b'A')     # refer to the table above
Traceback (most recent call last):
 File "<pyshell#8>", line 1, in <module>
 struct.unpack('h', b'A')
struct.error: unpack requires a bytes object of length 2

>>> struct.unpack('h', b'AB')    # correct
(16961,)

>>> struct.pack('hhl', 11, 22, 33)
b'\x0b\x00\x16\x00\x00\x00\x00\x00!\x00\x00\x00\x00\x00\x00\x00'
>>> struct.unpack('hhl', b'\x0b\x00\x16\x00\x00\x00\x00\x00!\x00\x00\x00\x00\x00\x00\x00')
(11, 22, 33)

>>> struct.calcsize('hhhhhlllB')
41