127.0.0.1:6379> SET key1 "1" OK 127.0.0.1:6379> SET key2 "value" OK 127.0.0.1:6379> SET key3 "Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp." OK 127.0.0.1:6379> TYPE key1 string 127.0.0.1:6379> TYPE key2 string 127.0.0.1:6379> TYPE key3 string 127.0.0.1:6379> OBJECT ENCODING key1 "int" 127.0.0.1:6379> OBJECT ENCODING key2 "embstr" 127.0.0.1:6379> OBJECT ENCODING key3 "raw"
而hash类型也有两种底层实现
1 2 3 4 5 6 7 8
127.0.0.1:6379> HSET myhash field1 "Hello" (integer) 1 127.0.0.1:6379> HSET myhash2 field1 "Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp." (integer) 1 127.0.0.1:6379> OBJECT ENCODING myhash "ziplist" 127.0.0.1:6379> OBJECT ENCODING myhash2 "hashtable"
call() is used in order to call a given command in the context of a given client.
The global variable redisCommandTable defines all the Redis commands, specifying the name of the command, the function implementing the command, the number of arguments required, and other properties of each command.
/* Note that we can't flag set as fast, since it may perform an * implicit DEL of a large key. */ {"set",setCommand,-3, "write use-memory @string", 0,NULL,1,1,1,0,0,0},
➜ /tmp rmdir show_gif ➜ /tmp mkdir show_gif ➜ /tmp cd show_gif ➜ show_gif python3 -m venv ./venv ➜ show_gif . ./venv/bin/activate (venv) ➜ show_gif pip install Pillow Collecting Pillow Using cached Pillow-8.1.0-cp39-cp39-macosx_10_10_x86_64.whl (2.2 MB) Installing collected packages: Pillow Successfully installed Pillow-8.1.0 WARNING: You are using pip version 20.2.3; however, version 21.0.1 is available. You should consider upgrading via the '/private/tmp/show_gif/venv/bin/python3 -m pip install --upgrade pip' command.
接着便可以让它读入并解析一张GIF图片
1 2 3 4 5 6 7 8 9
import sys
from PIL import Image, ImageSequence
if __name__ == '__main__': path = sys.argv[1] im = Image.open(path) for frame in ImageSequence.Iterator(im): pass
然后将每一帧都转换为RGB模式再遍历其每一个像素
1 2 3 4 5 6 7 8 9 10 11 12 13
import sys
from PIL import Image, ImageSequence
if __name__ == '__main__': path = sys.argv[1] im = Image.open(path) for frame in ImageSequence.Iterator(im): rgb_frame = frame.convert('RGB') pixels = rgb_frame.load() for y inrange(0, rgb_frame.height): for x inrange(0, rgb_frame.width): pass
if __name__ == '__main__': path = sys.argv[1] im = Image.open(path) for frame in ImageSequence.Iterator(im): rgb_frame = frame.convert('RGB') pixels = rgb_frame.load() for y inrange(0, rgb_frame.height): for x inrange(0, rgb_frame.width): colors = pixels[x, y] print('\x1b[48;2;{};{};{}m \x1b[0m'.format(*colors), end='') print('')
在每次二重循环遍历了所有像素后,还必须清除输出的内容,并将光标重置到左上角才能再次打印,这可以用ASCII转义序列来实现。查阅VT100 User Guide可以知道,用ED命令可以擦除显示的字符,对应的转义序列为\x1b[2J;用CUP命令可以移动光标的位置到左上角,对应的转义序列为\x1b[0;0H。在每次开始打印一帧图像前输出这两个转义序列即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import sys
from PIL import Image, ImageSequence
if __name__ == '__main__': path = sys.argv[1] im = Image.open(path) for frame in ImageSequence.Iterator(im): rgb_frame = frame.convert('RGB') pixels = rgb_frame.load() print('\x1b[2J\x1b[0;0H', end='') for y inrange(0, rgb_frame.height): for x inrange(0, rgb_frame.width): colors = pixels[x, y] print('\x1b[48;2;{};{};{}m \x1b[0m'.format(*colors), end='') print('')
if __name__ == '__main__': path = sys.argv[1] im = Image.open(path) for frame in ImageSequence.Iterator(im): rgb_frame = frame.convert('RGB') pixels = rgb_frame.load() print('\x1b[2J\x1b[0;0H', end='') for y inrange(0, rgb_frame.height): last_colors = None line = '' for x inrange(0, rgb_frame.width): colors = pixels[x, y] if colors != last_colors: line += '\x1b[0m\x1b[48;2;{};{};{}m '.format(*colors) else: line += ' ' last_colors = colors print('{}\x1b[0m'.format(line)) time.sleep(rgb_frame.info['duration'] / 1000)
defis_square(num: int) -> bool: return math.isqrt(num) ** 2 == num
deffind_x(D: int) -> int: """ 求出给定D时,满足题目所给的丢番图方程的最小的x。 """ assertnot is_square(D) y = 1 whileTrue: candidate = D * y * y + 1 if is_square(candidate): return math.isqrt(candidate) y += 1
defsolve_66(limit): """ 找出不大于limi的D中,使find_x的返回值最大的那一个数字。 """ max_D = None max_x = None D = 2 while D <= limit: if is_square(D): D += 1 continue x = find_x(D) if max_x isNoneor x > max_x: max_D = D max_x = x D += 1 return max_D, max_x
if __name__ == '__main__': D, x = solve_66(7) print('D is {} and x is {}'.format(D, x))
(formatt"~(hello world~)~%") ; 打印hello world (formatt"~:(hello world~)~%") ; 打印Hello World (formatt"~@(hello world~)~%") ; 打印Hello world (formatt"~:@(hello world~)~%") ; 打印HELLO WORLD
(multiple-value-bind (sec min hour date mon year) (decode-universal-time (get-universal-time)) (declare (ignorable sec)) (formatt"~4D-~2,'0D-~2,'0D ~2,'0D:~2,'0D~%" year mon date hour min))