'''This module implements specialized container datatypes providing
alternatives to Python's general purpose built-in containers, dict,
list, set, and tuple.
* namedtuple factory function for creating tuple subclasses with named fields
* deque list-like container with fast appends and pops on either end
* ChainMap dict-like class for creating a single view of multiple mappings
* Counter dict subclass for counting hashable objects
* OrderedDict dict subclass that remembers the order entries were added
* defaultdict dict subclass that calls a factory function to supply missing values
* UserDict wrapper around dictionary objects for easier dict subclassing
* UserList wrapper around list objects for easier list subclassing
* UserString wrapper around string objects for easier string subclassing
'''
__all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList',
'UserString', 'Counter', 'OrderedDict', 'ChainMap']
class deque(object):
"""
deque([iterable[, maxlen]]) --> deque object
A list-like sequence optimized for data accesses near its endpoints.
"""
def append(self, *args, **kwargs): # real signature unknown
""" Add an element to the right side of the deque. """
pass
def appendleft(self, *args, **kwargs): # real signature unknown
""" Add an element to the left side of the deque. """
pass
def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from the deque. """
pass
def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a deque. """
pass
def count(self, value): # real signature unknown; restored from __doc__
""" D.count(value) -> integer -- return number of occurrences of value """
return 0
def extend(self, *args, **kwargs): # real signature unknown
""" Extend the right side of the deque with elements from the iterable """
pass
def extendleft(self, *args, **kwargs): # real signature unknown
""" Extend the left side of the deque with elements from the iterable """
pass
def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
D.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0
def insert(self, index, p_object): # real signature unknown; restored from __doc__
""" D.insert(index, object) -- insert object before index """
pass
def pop(self, *args, **kwargs): # real signature unknown
""" Remove and return the rightmost element. """
pass
def popleft(self, *args, **kwargs): # real signature unknown
""" Remove and return the leftmost element. """
pass
def remove(self, value): # real signature unknown; restored from __doc__
""" D.remove(value) -- remove first occurrence of value. """
pass
def reverse(self): # real signature unknown; restored from __doc__
""" D.reverse() -- reverse *IN PLACE* """
pass
def rotate(self, *args, **kwargs): # real signature unknown
""" Rotate the deque n steps to the right (default n=1). If n is negative, rotates left. """
pass
age = 20
# ...
# 这里进行了一大串的其他指令
# 然后你忘记了 age 应该是 int
# 错误地将其赋值为字符串
age = '20'
print('The age is: ', age + 1)
# Output: TypeError: can only concatenate str (not "int") to str
# Python 3.8 之前的版本
from typing import Sequence as Seq1
def foo(seq: Seq1[str]):
for item in seq:
print(item)
# Python 3.9+ 也可以这么写
from collections.abc import Sequence as Seq2
def bar(seq: Seq2[str]):
for item in seq:
print(item)
按下 PREFIX-[ 进入文本复制模式。可以使用方向键在屏幕中移动光标。默认情况下,方向键是启用的。在配置文件中启用 Vim 键盘布局来切换窗口、调整窗格大小。Tmux 也支持 Vi 模式。要是想启用 Vi 模式,只需要把下面这一行添加到 .tmux.conf 中:
setw -g mode-keys vi
启用这条配置后,就可以使用 h、j、k、l 来移动光标了。
想要退出文本复制模式的话,按下回车键就可以了。然后按下 PREFIX-] 粘贴刚才复制的文本。
一次移动一格效率低下,在 Vi 模式启用的情况下,可以辅助一些别的快捷键高效工作。
例如,可以使用 w 键逐词移动,使用 b 键逐词回退。使用 f 键加上任意字符跳转到当前行第一次出现该字符的位置,使用 F 键达到相反的效果。
vi emacs 功能
^ M-m 反缩进
Escape C-g 清除选定内容
Enter M-w 复制选定内容
j Down 光标下移
h Left 光标左移
l Right 光标右移
L 光标移到尾行
M M-r 光标移到中间行
H M-R 光标移到首行
k Up 光标上移
d C-u 删除整行
D C-k 删除到行末
$ C-e 移到行尾
: g 前往指定行
C-d M-Down 向下滚动半屏
C-u M-Up 向上滚动半屏
C-f Page down 下一页
w M-f 下一个词
p C-y 粘贴
C-b Page up 上一页
b M-b 上一个词
q Escape 退出
C-Down or J C-Down 向下翻
C-Up or K C-Up 向下翻
n n 继续搜索
? C-r 向前搜索
/ C-s 向后搜索
0 C-a 移到行首
Space C-Space 开始选中
C-t 字符调序
杂项:
d 退出 tmux(tmux 仍在后台运行)
t 窗口中央显示一个数字时钟
? 列出所有快捷键
: 命令提示符
配置选项:
# 鼠标支持 - 设置为 on 来启用鼠标(与 2.1 之前的版本有区别,请自行查阅 man page)
* set -g mouse on
# 设置默认终端模式为 256color
set -g default-terminal "screen-256color"
# 启用活动警告
setw -g monitor-activity on
set -g visual-activity on
# 居中窗口列表
set -g status-justify centre
# 最大化/恢复窗格
unbind Up bind Up new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmp
unbind Down
bind Down last-window \; swap-pane -s tmp.1 \; kill-window -t tmp
参考配置文件(~/.tmux.conf):
下面这份配置是我使用 Tmux 几年来逐渐精简后的配置,请自取。
# -----------------------------------------------------------------------------
# Tmux 基本配置 - 要求 Tmux >= 2.3
# 如果不想使用插件,只需要将此节的内容写入 ~/.tmux.conf 即可
# -----------------------------------------------------------------------------
# C-b 和 VIM 冲突,修改 Prefix 组合键为 Control-Z,按键距离近
set -g prefix C-z
set -g base-index 1 # 窗口编号从 1 开始计数
set -g display-panes-time 10000 # PREFIX-Q 显示编号的驻留时长,单位 ms
set -g mouse on # 开启鼠标
set -g pane-base-index 1 # 窗格编号从 1 开始计数
set -g renumber-windows on # 关掉某个窗口后,编号重排
setw -g allow-rename off # 禁止活动进程修改窗口名
setw -g automatic-rename off # 禁止自动命名新窗口
setw -g mode-keys vi # 进入复制模式的时候使用 vi 键位(默认是 EMACS)
# -----------------------------------------------------------------------------
# 使用插件 - via tpm
# 1. 执行 git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# 2. 执行 bash ~/.tmux/plugins/tpm/bin/install_plugins
# -----------------------------------------------------------------------------
setenv -g TMUX_PLUGIN_MANAGER_PATH '~/.tmux/plugins'
# 推荐的插件(请去每个插件的仓库下读一读使用教程)
set -g @plugin 'seebi/tmux-colors-solarized'
set -g @plugin 'tmux-plugins/tmux-pain-control'
set -g @plugin 'tmux-plugins/tmux-prefix-highlight'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'tmux-plugins/tpm'
# tmux-resurrect
set -g @resurrect-dir '~/.tmux/resurrect'
# tmux-prefix-highlight
set -g status-right '#{prefix_highlight} #H | %a %Y-%m-%d %H:%M'
set -g @prefix_highlight_show_copy_mode 'on'
set -g @prefix_highlight_copy_mode_attr 'fg=white,bg=blue'
# 初始化 TPM 插件管理器 (放在配置文件的最后)
run '~/.tmux/plugins/tpm/tpm'
# -----------------------------------------------------------------------------
# 结束
# ---------------
下图描述了使用编码器—解码器将上述英语句子翻译成法语句子的一种方法。在训练数据集中,我们可以在每个句子后附上特殊符号“<eos>”(end of sequence)以表示序列的终止。编码器每个时间步的输入依次为英语句子中的单词、标点和特殊符号“<eos>”。下图中使用了编码器在最终时间步的隐藏状态作为输入句子的表征或编码信息。解码器在各个时间步中使用输入句子的编码信息和上个时间步的输出以及隐藏状态作为输入。我们希望解码器在各个时间步能正确依次输出翻译后的法语单词、标点和特殊符号”<eos>”。需要注意的是,解码器在最初时间步的输入用到了一个表示序列开始的特殊符号”<bos>”(beginning of sequence)。
CLASS torchtext.vocab.GloVe(name='840B', dim=300, **kwargs)
CLASS torchtext.vocab.FastText(language='en', **kwargs)
CLASS torchtext.vocab.ChaarNGram(**kwargs)
#实例
def get_vocab_imdb(data):
tokenized_data = get_tokenized_imdb(data)
counter = collections.Counter([tk for st in tokenized_data for tk in st])
return Vocab.vocab(counter, min_freq=5)
vocab = get_vocab_imdb(train_data)
'# words in vocab:', len(vocab)
输出:('# words in vocab:', 46151)
SubwordVocab: 构建子词汇表
CLASS torchtext.vocab.SubwordVocab(counter, max_size=None, specials='<pad>'
vectors=None, unk_init=...)
__init__(counter, ...)
从 collections.Counter 构建子词词汇表,
specials - padding or eos 等
torchtext.vocab.build_vocab_from_iterator(iterator: Iterable, min_freq: int = 1, specials: Optional[List[str]] = None, special_first: bool = True) → torchtext.vocab.vocab.Vocab[SOURCE]
Build a Vocab from an iterator.
Parameters
iterator – Iterator used to build Vocab. Must yield list or iterator of tokens.
min_freq – The minimum frequency needed to include a token in the vocabulary.
specials – Special symbols to add. The order of supplied tokens will be preserved.
special_first – Indicates whether to insert symbols at the beginning or at the end.