text:
layout: post
title: "Digest of <i>Information Theory, Inference, and Learning Algorithms</i>"
description: ""
category: Math
tags: [Book]
---
{% include JB/setup %}

## 1. Introduction to Information Theory

### 1.1 How can we achieve perfect communication over an imperfect, noisy communication channel?

In some cases, if we transmit data, e.g., a string of bits, over the channel, there is some probability $f$ that the received message will not be identical to the transmitted message. We would prefer to have a communication channel for which this $f$ was 0 – or so close to 0 that for practical purposes it is indistinguishable from 0.

E.g.

- modem $\rightarrow$ phone line $\rightarrow$ modem
- Galileo $\rightarrow$ radio waves $\rightarrow$ Earth
- parent cell $\rightarrow$ $\begin{cases} \text{daughter cell A} \newline \text{daughter cell B} \end{cases}$
- RAM $\rightarrow$ Hard Drive $\rightarrow$ RAM

N.B. 当然也有主动引入 noise 的情况,比如你把一个 720p 转成 480p

考虑 1-bit data 的情况,send 时的 data 我们用 $x$ 表示,receive 到的 data 我们用 $y$ 表示:

- √:$P(y=0 \mid x=0) = 1-f$
- √:$P(y=1 \mid x=1) = 1-f$
- ×:$P(y=1 \mid x=0) = f$
- ×:$P(y=0 \mid x=1) = f$

N.B. This is a Binary Symmetric Channel, A.k.a BSC. In BSC model, a transmitter wishes to send a bit (a zero or a one), and the receiver receives a bit.

A useful disk drive would flip no bits at all in its entire lifetime. If we expect to read and write a gigabyte per day for ten years, we require a bit error probability of the order of 10 −15 , or smaller. There are two approaches to this goal.

- The physical solution: to improve the physical characteristics of the communication channel to reduce its error probability.
    - 简单说就是提高 channel 的产品工艺
    - 缺点是会提高 channel 的物理成本
- The 'system' solution: we accept the given noisy channel as it is and add communication ***systems*** to it so that we can detect and correct the errors introduced by the channel.
    - We add an ***encoder*** before the channel and a ***decoder*** after it.
        - The encoder encodes the source message $s$ into a transmitted message $t$, adding redundancy to $s$ in some way.
        - The channel adds noise to $t$, yielding a received message $r$.
        - The decoder uses the known redundancy introduced by the encoder to infer both the original signal $s$ and the added noise.
        - $s \xrightarrow[\text{add redundancy}]{\text{encoder}} t \xrightarrow[\text{may add noise}]{\text{channel}} r \xrightarrow[\text{infer}]{\text{decoder}} \hat{s}$
    - 新添加了 system 的 computational cost

***Information theory*** is concerned with the theoretical limitations and po- tentials of such systems. 'What is the best error-correcting performance we could achieve?'

***Coding theory*** is concerned with the creation of practical encoding and decoding systems.

### 1.2 Error-correcting codes for BSC

#### Repetition codes

A straightforward idea is to repeat every bit of the message a prearranged number of times – for example, three times:

| $s$ | $t$ |
|:---:|:---:|
|  0  | 000 |
|  1  | 111 |

We call this repetition code $R_3$.

问题来了:如何 decode?$101$ 是 0 还是 1?$010$ 呢?

我们假设 $t=t_1t_2t_3, r=r_1r_2r_3$。 By Bayes' theorem, the ***posterior probability*** of $s$ is $P(s \mid r) = \frac{ P(r \mid s) P(s)}{P(r)}$

We can spell out the posterior probability of the two alternatives thus:

$$
\begin{align}
    P(s=1 \mid r) = \frac{ P(r \mid s = 1) P(s=1)}{P(r)} \newline
    P(s=0 \mid r) = \frac{ P(r \mid s = 0) P(s=0)}{P(r)}
\end{align}
$$

- $P(s)$ is the ***prior probability*** of $s$
- $P(r \mid s)$ is the ***likihood*** of $s$
- We assume that the prior probabilities are equal: $P(s = 0) = P(s = 1) = 0.5$; then maximizing the posterior probability $P(s \mid r)$ is equivalent to maximizing the likelihood $P(r \mid s)$.
- And we assume that this BSC has noise level $f \le 0.5$,

So the likelihood is

$$
P(r \mid s) = P(r \mid t) = P(r_1r_2r_3 \mid t_1t_2t_3) = \prod_{i=1}^{3} P(r_i \mid t_i)
$$

where

$$
P(r_i \mid t_i) = \begin{cases} 1 - f & \text{ if } r_i = t_i \newline f & \text{ if } r_i \neq t_i \end{cases}
$$

Thus the likelihood ratio for the two hypotheses is

$$
\frac{ P(r \mid s = 1) }{ P(r \mid s = 0) } = \prod_{i=1}^{3} \frac{ P(r_i \mid t_i = 1) }{ P(r_i \mid s_i = 0) }
$$

where each factor $\frac{ P(r_i \mid t_i = 1) }{ P(r_i \mid s_i = 0) }$ equals $\frac{1-f}{f}$ if $r_i = 1$, and $\frac{f}{1-f}$ if $r_i=0$.

We guess $\hat{s} = 1$ if $\frac{ P(r_i \mid t_i = 1) }{ P(r_i \mid s_i = 0) } > 1$ and $\hat{s} = 0$ otherwise.

Let's define $\gamma \equiv \frac{1-f}{f}$. $\gamma \ge 1$ since $f \le 0.5$. 假设 $r_i = 1$ 的个数为 $k$:

$$
\frac{ P(r \mid s = 1) }{ P(r \mid s = 0) } = \gamma^k \frac{1}{\gamma^{3-k}} = \gamma^{2k-3}
$$

所以 decode 的策略就是:数 $r_i = 1$ 的个数,$k \ge 2$ 时判定 $\hat{s} = 1$,否则判定 $\hat{s} = 0$。这个策略可以形象地理解为 majority vote: 每一个 $r_i$ 算一票,多数为 0 就判定为 0,多数为 1 就判定为 1。

而且这个 decode 还可以判断 noise 位置:若 $r_i \neg \hat{s}$,则可以判断第 $i$ 位是 noise。

使用 $R_3$ 依旧有可能传输出错,比如 $s=1 \rightarrow t=111 \rightarrow r=010 \rightarrow \hat{s}=0$

-----

*Exercise 1.2.* Show that the error probability is reduced by the use of $R_3$ by computing the error probability of this code for a BSC with noise level $f$.

Assume the probability of bit error is $p_b$ (注意 $p_b$ 是整个信息传递过程的出错概率,$f$ 仅仅是 channel 的出错概率)

Previous noise level: $p_b = f$

$R_3$ noise level: $p_b = f^3 + 3 f^2 (1-f)$

证明 $f^3 + 3 f^2 (1-f) < f$ 即可

*Exercise 1.3.*

The probability of error for $R_N$ is

$$
p_b = \sum_{n=\lceil \frac{N}{2} \rceil}^{N} { N \choose n } f^n (1-f)^{N-n}
$$

(待补充)

-----

The repetition code $R_3$ has therefore reduced the probability of error, as desired. Yet we have lost something: our ***rate*** of information transfer (姑且理解为 $\frac{len(s)}{len(r)}$) has fallen to $\frac{1}{3}$.

#### Block codes – the $(7, 4)$ Hamming code

We would like to communicate with tiny probability of error and at a substantial rate. Can we improve on repetition codes? What if we add redundancy to ***blocks of data*** instead of encoding ***one bit*** at a time? We now study a simple ***block code***.

$\vert s \vert = K, \vert t \vert = N$. $N > k$ because we are adding redundancy.

In a ***linear block code***, the extra $N − K$ bits are linear functions of the original $K$ bits; these extra $N − K$ bits are called ***parity-check bits***. An example of a linear block code is the $(7, 4)$ Hamming code, which transmits $N = 7$ bits for every $K = 4$ source bits.

- parity: (of a number) the fact of being even or odd.

Let's define

$$
\operatorname{parity}(x_1,\dots,x_n) = \left ( \sum_{i=1}^{n}x_i \right ) \bmod 2
$$

N.B. 这个 $\operatorname{parity}()$ 是个 linear function!linear 出来了!

- $t_1 = s_1$
- $t_2 = s_2$
- $t_3 = s_3$
- $t_4 = s_4$
- $t_5 = \operatorname{parity}(s_1, s_2, s_3)$
- $t_6 = \operatorname{parity}(s_2, s_3, s_4)$
- $t_7 = \operatorname{parity}(s_1, s_3, s_4)$

也可以从另一个角度理解 $t_5 t_6 t_7$: 比如对 $t_5$ 而言,$\operatorname{parity}(s_1, s_2, s_3, t_5) = 0$ 一定成立;$t_6$、$t_7$ 类似

Because the Hamming code is a linear code, it can be written compactly in terms of matrices as follows. The transmitted codeword $t$ is obtained from the source sequence $s$ by a linear operation,

$$
t = \textbf{G}^\intercal s,
$$

where $\textbf{G}$ is the ***generator matrix*** of the code,

$$
\textbf{G}^\intercal = \begin{bmatrix}
    1 & 0 & 0 & 0 \newline
    0 & 1 & 0 & 0 \newline
    0 & 0 & 1 & 0 \newline
    0 & 0 & 0 & 1 \newline
    1 & 1 & 1 & 0 \newline
    0 & 1 & 1 & 1 \newline
    1 & 0 & 1 & 1
\end{bmatrix}
$$

and the encoding operation uses modulo-2 arithmetic ($1 + 1 = 0$, $0 + 1 = 1$, etc.).

N.B. $t = \textbf{G}^\intercal s$ 的 dimension 是 $(7,1) = (7,4) \cdot (4,1)$

我觉得可以把 $\textbf{G}^\intercal$ 理解为 7 个 functions 的合体:

$$
\textbf{G}^\intercal(x) = \begin{bmatrix}
    \text{lambda } x: x[0] \newline
    \text{lambda } x: x[1] \newline
    \text{lambda } x: x[2] \newline
    \text{lambda } x: x[3] \newline
    \text{lambda } x: x[0] \oplus x[1] \oplus x[2] \newline
    \text{lambda } x: x[1] \oplus x[2] \oplus x[3] \newline
    \text{lambda } x: x[0] \oplus x[2] \oplus x[3]
\end{bmatrix}
$$

注意有的教材会写成 $t = s \textbf{G}$,明显没有 $t = \textbf{G}^\intercal s$ 好理解(因为 $\textbf{G}$ 写出来是个 $(4,7)$);这也说明,如果一个 matrix function 你看不懂的话,试试转置说不定会有用。

#### Syndrome decoding for the Hamming code

(待补充)

#### General view of decoding for linear codes: syndrome decoding

(待补充)

#### Summary of the $(7, 4)$ Hamming code's properties

(待补充)

#### Summary of codes' performances

(待补充)

### 1.3 What performance can the best codes achieve?

There seems to be a trade-off between the decoded bit-error probability $p_b$ (which we would like to reduce) and the rate $R$ (which we would like to keep large). How can this trade-off be characterized? What points in the $(R, p_b)$ plane are achievable?

***Noisy-channel coding theorem***: For any channel, there exist codes that make it possible to communicate with ***arbitrarily small*** probability of error $p_b$ at non-zero rates.

The maximum rate at which communication is possible with arbitrarily small $p_b$ is called the *capacity* of the channel. The formula for the capacity of a BSC with noise level $f$ is

$$
C(f) = 1 - H_2(f) = 1 - \left [ f \log_2 \frac{1}{f} + (1-f) \log_2 \frac{1}{1-f} \right ]
$$

A channel with noise level $f = 0.1$ has capacity $C \approx 0.53$.

> What performance are you trying to achieve? $10^{−15}$? You don't need sixty disk drives – you can get that performance with just two disk drives (since $1/2$ is less than $0.53$). And if you want $p_b = 10^{−18}$ or $10^{−24}$ or anything, you can get there with two disk drives too!

### 1.4 Summary

(待补充)

## 2. Probability, Entropy, and Inference

This chapter, and its sibling, Chapter 8, devote some time to notation. Just as the _White Knight_ distinguished between

- the song,
- the name of the song, and
- what the name of the song was called (Carroll, 1998),

we will sometimes need to be careful to distinguish between

- a random variable,
- the value of the random variable, and
- the proposition (命题) that asserts that the random variable has a particular value.

### 2.1 Probabilities and ensembles

An ***ensemble*** $X$ is a triple $(x, \mathcal{A}\_X, \mathcal{P}\_X)$, where the ***outcome*** $x$ is the value of a random variable, which takes on one of a set of possible values, $\mathcal{A}\_X = \lbrace a_1, a_2, \dots, a_I \rbrace$, having probabilities $\mathcal{P}\_X = \lbrace p_1, p_2, \dots, p_I \rbrace$, with $P(x = a_i ) = p_i$, $pi \geq 0$ and $\sum_{a_i \in \mathcal{A}\_X} P(x = a_i) = 1$.

- $\mathcal{A}$ means 'alphabet'.

$P(x = a_i)$ may be written as $P(a_i)$ or $P(x)$.

A ***joint ensemble*** $XY$ is an ensemble in which each outcome is an ordered pair $x, y$ (read '$x$ and $y$') with $x \in \mathcal{A}\_X = \lbrace a_1, a_2, \dots, a_I \rbrace$ and $y \in \mathcal{A}\_Y = \lbrace b_1, b_2, \dots, b_J \rbrace$.

We call $P(x,y)$ the joint probability of $x$ and $y$. Commas are optional when writing ordered pairs, so $xy \iff x, y$.

N.B. In a joint ensemble $XY$ the two variables are not necessarily independent.

We can obtain the ***marginal probability*** $P(x)$ from the joint probability $P(x,y)$ by summation:

$$
P(x = a_i) \equiv \sum_{y \in \mathcal{A}\_Y} P(x = a_i, y)
$$

Similarly, using briefer notation, the marginal probability of $y$ is:

$$
P(y) \equiv \sum_{x \in \mathcal{A}\_X} P(x, y)
$$

N.B. 说到 marginal 的时候一定要记得是先有的 joint 才有的 marginal

Consider the set of bigrams $x,y$ in a corpus:

- The probability $P(y \mid x = \text{'q'})$ is the probability distribution of the second letter $y$ given that the first letter $x$ is a $\text{'q'}$.
- The probability $P(x \mid y = \text{'u'})$ is the probability distribution of the first letter $x$ given that the second letter $y$ is a $\text{'u'}$.

N.B. 注意这个描述方式!

***Product rule*** or ***chain rule***:

$$
P(x,y \mid \mathcal{H}) = P(x \mid y, \mathcal{H}) P(y \mid \mathcal{H}) = P(y \mid x, \mathcal{H}) P(x \mid \mathcal{H})
$$

N.B. $P(x \mid y, \mathcal{H})$ 应该理解为 $P(x \mid \left ( y, \mathcal{H} \right ))$ 而不是 $P(\left ( x \mid y \right ), \mathcal{H})$

### 2.2 The meaning of probability

Probabilities can be used in two ways.

- Probabilities can describe ***frequencies of outcomes in random experiments***
    - but giving noncircular definitions of the terms 'frequency' and 'random' is a challenge
- Probabilities can also be used, more generally, to describe ***degrees of belief*** in propositions that do not involve random variables, for example
    - 'the probability that Mr. S. was the murderer of Mrs. S., given the evidence'
    - 'the probability that Shakespeare's plays were written by Francis Bacon'

Thus probabilities can be used to describe assumptions, and to describe inferences given those assumptions. This more general use of probability to quantify beliefs is known as the ***Bayesian*** viewpoint. It is also known as the ***subjective*** (主观的;客观是 objective) interpretation of probability, since the probabilities depend on assumptions. Advocates of a Bayesian approach to data modelling and pattern recognition do not view this subjectivity as a defect, since in their view,

> you cannot do inference without making assumptions.

N.B. Readers should be warned that this is not yet a globally held view.

In non-Bayesian methods probabilities are allowed to describe ***only*** random variables, while Bayesians also use probabilities to describe ***inferences***.

### 2.3 Forward probabilities and inverse probabilities

我们先研究下 Generative Model 和 Discriminative Model。

#### Digress: Generative Models

[wikipedia: Generative model](https://en.wikipedia.org/wiki/Generative_model):

> In probability and statistics, a generative model is a model for randomly generating observable data values, typically given some hidden parameters.

有点难懂,啥叫 "randomly generating"?我们再看 [Quora: What is a generative model? - Jack Rae](https://www.quora.com/What-is-a-generative-model/answer/Jack-Rae?srid=mncz):

> A generative model describes how data is generated, in terms of a probabilistic model.

另外在 ML context 下:

> In the scenario of supervised learning, a generative model estimates the joint probability distribution of data $P(X, Y)$ between the observed data $X$ and corresponding labels $Y$.

这个描述就好懂多了。换言之,generative model 描述生成 examples 的规律,比如 $P(X=x_1) = 0.6$ 就表示 "生成 $x_1$ 的概率是 60%";又比如 $X \sim \mathcal {N}(\mu ,\sigma ^{2})$ 就表示 "$X$ 的生成遵循高斯分布"。

也正如 [Quora: What is a generative model? - Eren Golge](https://www.quora.com/What-is-a-generative-model/answer/Eren-Golge?srid=mncz) 所说:

> Generative model assumes data is created by a particular distribution which is defined by a couple of parameters (Gaussian Distribution) or non-parametric variants...

常见的 generative models 有:

- Gaussians
- Mixtures of multinomials
- Naive Bayes
- Hidden Markov Models
- Latent Dirichlet Allocation

另外 [Generative vs. Discriminative Models](http://luizcoletta.com/blog/2015/09/07/generative-vs-discriminative-models/) 谈到:

- Pros:
    - We have the knowledge about the data distribution.
- Cons:
    - Very expensive to get (a lot of parameters);
    - Need lots of data.

这里谈到 parameter 又涉及到另外一个问题,如 [Cross Validated: Generative vs. discriminative - raegtin](https://stats.stackexchange.com/a/12456) 所言:

> Another way of thinking about this is that generative algorithms make some kind of structure assumptions on your model, but discriminative algorithms make fewer assumptions.

比如我们说 $X \sim \mathcal {N}(\mu ,\sigma ^{2})$ 是假设了它满足高斯分布的条件;必须先有这些假设,我们才能接着谈 parameter 的问题。

#### Digress: Discriminative Model

Discriminative 向来比 Generative 好理解,还是用 [Generative vs. Discriminative Models](http://luizcoletta.com/blog/2015/09/07/generative-vs-discriminative-models/) 的说法吧:

> Algorithms aim at learning $P(y|x)$ by using probabilistic approaches (e.g., logistic regression), or non-probabilistically by mapping classes from a set of points (e.g., perceptrons and SVMs).

- Pros:
    - Easy to model.
- Cons:
    - To classify, but not to generate the data.

回到正题。Probability calculations often fall into one of two categories: ***forward probability*** and ***inverse probability***.

- Forward probability problems
    - Involve a generative model of a process (这里这个 process 的意思就是指生成 examples 的 process)
    - The task is to compute the probability distribution.
        - 提出假设并求 parameter、计算 expectation、variance 等都算 forward problems
- Inverse probability problems
    - Also involve a generative model of a process
    - Instead, we compute the conditional probability of one or more of the unobserved variables in the process, given the observed variables.
        - 比如 HMM
    - This invariably requires the use of Bayes' theorem.

#### Digress: Marginal Probability

大家都知道 $P(a,b)$ 是 joint probability ($P(A,B)$ 是 joint distribution),$P(a \vert b)$ 是 conditional probability ($P(A \vert B)$ 是 conditional distribution)。那 marginal probability 是啥?其实很简单,marginal 一定是来自 joint 的 (by sum rule),比如 $P(a) = \sum_{b \in B} P(a, b)$ 就是一个 marginal probability。有时候文章里会简单粗暴地说 "$P(a)$ 是一个 marginal probability;$P(A)$ 是一个 marginal distribution",你要知道它一定是来自某个 joint,然后是 marginalized over 某个 $B$。

复杂一点的例子:$P(a \vert N) = \sum_{b \in B} P(a, b \vert N)$ 也是一个 marginal probability (of $a$ conditional on $N$).

#### Terminology of inverse probability

If $\theta$ denotes the unknown parameters, $D$ denotes the data, and $\mathcal{H}$ denotes the overall hypothesis space, the general equation:

$$
P(\theta \vert D, \mathcal{H}) = \frac{P(D \vert \theta, \mathcal{H}) P(\theta \vert \mathcal{H})}{P(D \vert \mathcal{H})}
$$

can be written:

$$
\text{posterior} = \frac{\text{likelihood} \times \text{prior}}{\text{evidence}}
$$

For a fixed $\theta_1 \in \theta$ and a fixed $D_1 \in D$:

- The conditional probability $P(\theta_1 \vert D_1, \mathcal{H})$ is called the ***posterior probability*** of $\theta_1$ given $D_1$
- The conditional probability $P(D_1 \vert \theta_1, \mathcal{H})$ is called the ***likelihood*** of parameter $\theta_1$
    - also probability of $D_1$ given $\theta_1$
- The marginal probability $P(\theta_1 \vert \mathcal{H})$ is called the ***prior probability*** of parameter $\theta_1$
- $P(D \vert \mathcal{H})$ is known as the ***evidence*** or ***marginal likelihood***

#### The likelihood principle

**The likelihood principle:** given a generative model for data $d$ given parameters $\theta$, $P(d \vert \theta)$, and having observed a particular outcome $d_1$, all inferences and predictions should depend only on the function $P (d_1 \vert \theta)$.

In spite of the simplicity of this principle, many classical statistical methods violate it.

### 2.4 Definition of entropy and related functions


text:
#include "mac.h"
#include "device.h"
#include "power.h"
#include "wcmd.h"
#include "rxtx.h"
#include "card.h"
#include "usbpipe.h"

/*
 *
 * Routine Description:
 * Enable hw power saving functions
 *
 * Return Value:
 *    None.
 *
 */

void vnt_enable_power_saving(struct vnt_private *priv, u16 listen_interval)
{
	u16 aid = priv->current_aid | BIT(14) | BIT(15);

	/* set period of power up before TBTT */
	vnt_mac_write_word(priv, MAC_REG_PWBT, C_PWBT);

	if (priv->op_mode != NL80211_IFTYPE_ADHOC)
		/* set AID */
		vnt_mac_write_word(priv, MAC_REG_AIDATIM, aid);

	/* Warren:06-18-2004,the sequence must follow
	 * PSEN->AUTOSLEEP->GO2DOZE
	 */
	/* enable power saving hw function */
	vnt_mac_reg_bits_on(priv, MAC_REG_PSCTL, PSCTL_PSEN);

	/* Set AutoSleep */
	vnt_mac_reg_bits_on(priv, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);

	/* Warren:MUST turn on this once before turn on AUTOSLEEP ,or the
	 * AUTOSLEEP doesn't work
	 */
	vnt_mac_reg_bits_on(priv, MAC_REG_PSCTL, PSCTL_GO2DOZE);

	if (listen_interval >= 2) {

		/* clear always listen beacon */
		vnt_mac_reg_bits_off(priv, MAC_REG_PSCTL, PSCTL_ALBCN);

		/* first time set listen next beacon */
		vnt_mac_reg_bits_on(priv, MAC_REG_PSCTL, PSCTL_LNBCN);
	} else

		/* always listen beacon */
		vnt_mac_reg_bits_on(priv, MAC_REG_PSCTL, PSCTL_ALBCN);

	dev_dbg(&priv->usb->dev,  "PS:Power Saving Mode Enable...\n");
}

/*
 *
 * Routine Description:
 * Disable hw power saving functions
 *
 * Return Value:
 *    None.
 *
 */

void vnt_disable_power_saving(struct vnt_private *priv)
{

	/* disable power saving hw function */
	vnt_control_out(priv, MESSAGE_TYPE_DISABLE_PS, 0,
	                0, 0, NULL);

	/* clear AutoSleep */
	vnt_mac_reg_bits_off(priv, MAC_REG_PSCFG, PSCFG_AUTOSLEEP);

	/* set always listen beacon */
	vnt_mac_reg_bits_on(priv, MAC_REG_PSCTL, PSCTL_ALBCN);
}

/*
 *
 * Routine Description:
 * Check if Next TBTT must wake up
 *
 * Return Value:
 *    None.
 *
 */

int vnt_next_tbtt_wakeup(struct vnt_private *priv)
{
	struct ieee80211_hw *hw = priv->hw;
	struct ieee80211_conf *conf = &hw->conf;
	int wake_up = false;

	if (conf->listen_interval > 1) {
		/* Turn on wake up to listen next beacon */
		vnt_mac_reg_bits_on(priv, MAC_REG_PSCTL, PSCTL_LNBCN);
		wake_up = true;
	}

	return wake_up;
}

redpajama_set_name: RedPajamaGithub


text:
use proc_macro2::*;
use proc_quote::*;
use syn::*;

pub fn derive(input: &DeriveInput) -> TokenStream {
    let DeriveInput {
        ident,
        data,
        generics,
        ..
    } = input;

    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    let fields = match crate::object_view::get_fields(data) {
        Ok(fields) => fields,
        Err(err) => return err.to_compile_error(),
    };

    quote! {
        impl #impl_generics ::liquid::ValueView for #ident #ty_generics #where_clause {
            fn as_debug(&self) -> &dyn ::std::fmt::Debug {
                self
            }

            fn render(&self) -> ::liquid::model::DisplayCow<'_> {
                ::liquid::model::DisplayCow::Owned(Box::new(::liquid::model::ObjectRender::new(self)))
            }
            fn source(&self) -> ::liquid::model::DisplayCow<'_> {
                ::liquid::model::DisplayCow::Owned(Box::new(::liquid::model::ObjectSource::new(self)))
            }
            fn type_name(&self) -> &'static str {
                "object"
            }
            fn query_state(&self, state: ::liquid::model::State) -> bool {
                match state {
                    ::liquid::model::State::Truthy => true,
                    ::liquid::model::State::DefaultValue |
                    ::liquid::model::State::Empty |
                    ::liquid::model::State::Blank => self.size() == 0,
                }
            }

            fn to_kstr(&self) -> ::liquid::model::KStringCow<'_> {
                let s = ::liquid::model::ObjectRender::new(self).to_string();
                ::liquid::model::KStringCow::from_string(s)
            }
            fn to_value(&self) -> ::liquid::model::Value {
                let mut object = ::liquid::model::Object::new();
                #(
                    object.insert(stringify!(#fields).into(), ::liquid::model::ValueView::to_value(&self.#fields));
                )*
                ::liquid::model::Value::Object(object)
            }

            fn as_object(&self) -> Option<&dyn ::liquid::ObjectView> {
                Some(self)
            }
        }
    }
}

pub fn core_derive(input: &DeriveInput) -> TokenStream {
    let DeriveInput {
        ident,
        data,
        generics,
        ..
    } = input;

    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

    let fields = match crate::object_view::get_fields(data) {
        Ok(fields) => fields,
        Err(err) => return err.to_compile_error(),
    };

    quote! {
        impl #impl_generics ::liquid_core::ValueView for #ident #ty_generics #where_clause {
            fn as_debug(&self) -> &dyn ::std::fmt::Debug {
                self
            }

            fn render(&self) -> ::liquid_core::model::DisplayCow<'_> {
                ::liquid_core::model::DisplayCow::Owned(Box::new(::liquid_core::model::ObjectRender::new(self)))
            }
            fn source(&self) -> ::liquid_core::model::DisplayCow<'_> {
                ::liquid_core::model::DisplayCow::Owned(Box::new(::liquid_core::model::ObjectSource::new(self)))
            }
            fn type_name(&self) -> &'static str {
                "object"
            }
            fn query_state(&self, state: ::liquid_core::model::State) -> bool {
                match state {
                    ::liquid_core::model::State::Truthy => true,
                    ::liquid_core::model::State::DefaultValue |
                    ::liquid_core::model::State::Empty |
                    ::liquid_core::model::State::Blank => self.size() == 0,
                }
            }

            fn to_kstr(&self) -> ::liquid_core::model::KStringCow<'_> {
                let s = ::liquid_core::model::ObjectRender::new(self).to_string();
                ::liquid_core::model::KStringCow::from_string(s)
            }
            fn to_value(&self) -> ::liquid_core::model::Value {
                let mut object = ::liquid_core::model::Object::new();
                #(
                    object.insert(stringify!(#fields).into(), ::liquid_core::model::ValueView::to_value(&self.#fields));
                )*
                ::liquid_core::model::Value::Object(object)
            }

            fn as_object(&self) -> Option<&dyn ::liquid_core::ObjectView> {
                Some(self)
            }
        }
    }
}


text:
package httl.util;

/**
 * Token. (Tool, Prototype, NotThreadsafe)
 *
 * @author Liang Fei (liangfei0201 AT gmail DOT com)
 */
public final class Token {

    public static final int UNKNOWN_TYPE = -1;

    private final String message;

    private final int offset;

    private final int type;

    /**
     * Create Token.
     *
     * @param message message
     * @param offset  offset
     */
    public Token(String message, int offset) {
        this(message, offset, UNKNOWN_TYPE);
    }

    /**
     * Create Token with type.
     *
     * @param message message
     * @param offset  offset
     * @param type    type
     */
    public Token(String message, int offset, int type) {
        if (StringUtils.isEmpty(message))
            throw new IllegalArgumentException("message == null");
        this.message = message;
        this.offset = offset;
        this.type = type;
    }

    /**
     * Get message.
     *
     * @return message.
     */
    public String getMessage() {
        return message;
    }

    /**
     * Get offset.
     *
     * @return offset
     */
    public int getOffset() {
        return offset;
    }

    /**
     * Get type.
     *
     * @return type.
     */
    public int getType() {
        return type;
    }

    @Override
    public String toString() {
        return message;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((message == null) ? 0 : message.hashCode());
        result = prime * result + offset;
        result = prime * result + type;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        Token other = (Token) obj;
        if (message == null) {
            if (other.message != null) return false;
        } else if (!message.equals(other.message)) return false;
        if (offset != other.offset) return false;
        if (type != other.type) return false;
        return true;
    }

}


text:
package org.apache.beam.sdk.transforms;

import static com.google.common.base.Preconditions.checkArgument;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import org.apache.beam.sdk.coders.BigEndianIntegerCoder;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.coders.CoderRegistry;
import org.apache.beam.sdk.coders.IterableCoder;
import org.apache.beam.sdk.coders.KvCoder;
import org.apache.beam.sdk.transforms.Combine.CombineFn;
import org.apache.beam.sdk.transforms.display.DisplayData;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.values.PCollection;

/**
 * {@code PTransform}s for taking samples of the elements in a
 * {@code PCollection}, or samples of the values associated with each
 * key in a {@code PCollection} of {@code KV}s.
 *
 * {@link #fixedSizeGlobally(int)} and {@link #fixedSizePerKey(int)} compute uniformly random
 * samples. {@link #any(long)} is faster, but provides no uniformity guarantees.
 *
 *
{@link #combineFn} can also be used manually, in combination with state and with the
 * {@link Combine} transform.
 */
public class Sample {

  /** Returns a {@link CombineFn} that computes a fixed-sized uniform sample of its inputs. */
  public static <T> CombineFn<T, ?, Iterable<T>> combineFn(int sampleSize) {
    return new FixedSizedSampleFn<>(sampleSize);
  }

  /**
   * Returns a {@link CombineFn} that computes a fixed-sized potentially non-uniform sample of its
   * inputs.
   */
  public static <T> CombineFn<T, ?, Iterable<T>> anyCombineFn(int sampleSize) {
    return new SampleAnyCombineFn<>(sampleSize);
  }

  /**
   * {@code Sample#any(long)} takes a {@code PCollection<T>} and a limit, and
   * produces a new {@code PCollection<T>} containing up to limit
   * elements of the input {@code PCollection}.
   *
   *
If limit is greater than or equal to the size of the input
   * {@code PCollection}, then all the input's elements will be selected.
   *
   *
Example of use:
   *
 {@code
   * PCollection<String> input = ...;
   * PCollection<String> output = input.apply(Sample.<String>any(100));
   * }

   *
   * @param <T> the type of the elements of the input and output
   * {@code PCollection}s
   * @param limit the number of elements to take from the input
   */
  public static <T> PTransform<PCollection<T>, PCollection<T>> any(long limit) {
    return new Any<>(limit);
  }

  /**
   * Returns a {@code PTransform} that takes a {@code PCollection<T>}, selects {@code sampleSize}
   * elements, uniformly at random, and returns a {@code PCollection<Iterable<T>>} containing the
   * selected elements. If the input {@code PCollection} has fewer than {@code sampleSize} elements,
   * then the output {@code Iterable<T>} will be all the input's elements.
   *
   *
All of the elements of the output {@code PCollection} should fit into
   * main memory of a single worker machine.  This operation does not
   * run in parallel.
   *
   *
Example of use:
   *
   *
{@code
   * PCollection<String> pc = ...;
   * PCollection<Iterable<String>> sampleOfSize10 =
   *     pc.apply(Sample.fixedSizeGlobally(10));
   * }
   *

   *
   * @param sampleSize the number of elements to select; must be {@code >= 0}
   * @param <T> the type of the elements
   */
  public static <T> PTransform<PCollection<T>, PCollection<Iterable<T>>> fixedSizeGlobally(
      int sampleSize) {
    return new FixedSizeGlobally<>(sampleSize);
  }

  /**
   * Returns a {@code PTransform} that takes an input {@code PCollection<KV<K, V>>} and returns a
   * {@code PCollection<KV<K, Iterable<V>>>} that contains an output element mapping each distinct
   * key in the input {@code PCollection} to a sample of {@code sampleSize} values associated with
   * that key in the input {@code PCollection}, taken uniformly at random. If a key in the input
   * {@code PCollection} has fewer than {@code sampleSize} values associated with it, then the
   * output {@code Iterable<V>} associated with that key will be all the values associated with that
   * key in the input {@code PCollection}.
   *
   *
Example of use:
   *
   *
{@code
   * PCollection<KV<String, Integer>> pc = ...;
   * PCollection<KV<String, Iterable<Integer>>> sampleOfSize10PerKey =
   *     pc.apply(Sample.<String, Integer>fixedSizePerKey());
   * }
   *

   *
   * @param sampleSize the number of values to select for each distinct key; must be {@code >= 0}
   * @param <K> the type of the keys
   * @param <V> the type of the values
   */
  public static <K, V>
      PTransform<PCollection<KV<K, V>>, PCollection<KV<K, Iterable<V>>>> fixedSizePerKey(
          int sampleSize) {
    return new FixedSizePerKey<>(sampleSize);
  }

  /////////////////////////////////////////////////////////////////////////////

  /** Implementation of {@link #any(long)}. */
  private static class Any<T> extends PTransform<PCollection<T>, PCollection<T>> {
    private final long limit;

    /**
     * Constructs a {@code SampleAny<T>} PTransform that, when applied,
     * produces a new PCollection containing up to {@code limit}
     * elements of its input {@code PCollection}.
     */
    private Any(long limit) {
      checkArgument(limit >= 0, "Expected non-negative limit, received %s.", limit);
      this.limit = limit;
    }

    @Override
    public PCollection<T> expand(PCollection<T> in) {
      return in
          .apply(Combine.globally(new SampleAnyCombineFn<T>(limit)).withoutDefaults())
          .apply(Flatten.<T>iterables());
    }

    @Override
    public void populateDisplayData(DisplayData.Builder builder) {
      super.populateDisplayData(builder);
      builder.add(DisplayData.item("sampleSize", limit)
        .withLabel("Sample Size"));
    }
  }

  /** Implementation of {@link #fixedSizeGlobally(int)}. */
  private static class FixedSizeGlobally<T>
      extends PTransform<PCollection<T>, PCollection<Iterable<T>>> {
    private final int sampleSize;

    private FixedSizeGlobally(int sampleSize) {
      this.sampleSize = sampleSize;
    }

    @Override
    public PCollection<Iterable<T>> expand(PCollection<T> input) {
      return input.apply(Combine.globally(new FixedSizedSampleFn<T>(sampleSize)));
    }

    @Override
    public void populateDisplayData(DisplayData.Builder builder) {
      super.populateDisplayData(builder);
      builder.add(DisplayData.item("sampleSize", sampleSize)
          .withLabel("Sample Size"));
    }
  }

  /** Implementation of {@link #fixedSizeGlobally(int)}. */
  private static class FixedSizePerKey<K, V>
      extends PTransform<PCollection<KV<K, V>>, PCollection<KV<K, Iterable<V>>>> {
    private final int sampleSize;

    private FixedSizePerKey(int sampleSize) {
      this.sampleSize = sampleSize;
    }

    @Override
    public PCollection<KV<K, Iterable<V>>> expand(PCollection<KV<K, V>> input) {
      return input.apply(Combine.<K, V, Iterable<V>>perKey(new FixedSizedSampleFn<V>(sampleSize)));
    }

    @Override
    public void populateDisplayData(DisplayData.Builder builder) {
      super.populateDisplayData(builder);
      builder.add(DisplayData.item("sampleSize", sampleSize)
          .withLabel("Sample Size"));
    }
  }

  /**
   * A {@link CombineFn} that combines into a {@link List} of up to limit elements.
   */
  private static class SampleAnyCombineFn<T> extends CombineFn<T, List<T>, Iterable<T>> {
    private final long limit;

    private SampleAnyCombineFn(long limit) {
      this.limit = limit;
    }

    @Override
    public List<T> createAccumulator() {
      return new ArrayList<T>((int) limit);
    }

    @Override
    public List<T> addInput(List<T> accumulator, T input) {
      if (accumulator.size() < limit) {
        accumulator.add(input);
      }
      return accumulator;
    }

    @Override
    public List<T> mergeAccumulators(Iterable<List<T>> accumulators) {
      Iterator<List<T>> iter = accumulators.iterator();
      if (!iter.hasNext()) {
        return createAccumulator();
      }
      List<T> res = iter.next();
      while (iter.hasNext()) {
        for (T t : iter.next()) {
          if (res.size() >= limit) {
            return res;
          }
          res.add(t);
        }
      }
      return res;
    }

    @Override
    public Iterable<T> extractOutput(List<T> accumulator) {
      return accumulator;
    }
  }

  /**
   * {@code CombineFn} that computes a fixed-size sample of a
   * collection of values.
   *
   * @param <T> the type of the elements
   */
  public static class FixedSizedSampleFn<T>
      extends CombineFn<T,
          Top.BoundedHeap<KV<Integer, T>, SerializableComparator<KV<Integer, T>>>,
          Iterable<T>> {
    private final int sampleSize;
    private final Top.TopCombineFn<KV<Integer, T>, SerializableComparator<KV<Integer, T>>>
        topCombineFn;
    private final Random rand = new Random();

    private FixedSizedSampleFn(int sampleSize) {
      if (sampleSize < 0) {
        throw new IllegalArgumentException("sample size must be >= 0");
      }

      this.sampleSize = sampleSize;
      topCombineFn = new Top.TopCombineFn<KV<Integer, T>, SerializableComparator<KV<Integer, T>>>(
          sampleSize, new KV.OrderByKey<Integer, T>());
    }

    @Override
    public Top.BoundedHeap<KV<Integer, T>, SerializableComparator<KV<Integer, T>>>
        createAccumulator() {
      return topCombineFn.createAccumulator();
    }

    @Override
    public Top.BoundedHeap<KV<Integer, T>, SerializableComparator<KV<Integer, T>>> addInput(
        Top.BoundedHeap<KV<Integer, T>, SerializableComparator<KV<Integer, T>>> accumulator,
        T input) {
      accumulator.addInput(KV.of(rand.nextInt(), input));
      return accumulator;
    }

    @Override
    public Top.BoundedHeap<KV<Integer, T>, SerializableComparator<KV<Integer, T>>>
        mergeAccumulators(
            Iterable<Top.BoundedHeap<KV<Integer, T>, SerializableComparator<KV<Integer, T>>>>
            accumulators) {
      return topCombineFn.mergeAccumulators(accumulators);
    }

    @Override
    public Iterable<T> extractOutput(
        Top.BoundedHeap<KV<Integer, T>, SerializableComparator<KV<Integer, T>>> accumulator) {
      List<T> out = new ArrayList<>();
      for (KV<Integer, T> element : accumulator.extractOutput()) {
        out.add(element.getValue());
      }
      return out;
    }

    @Override
    public Coder<Top.BoundedHeap<KV<Integer, T>, SerializableComparator<KV<Integer, T>>>>
        getAccumulatorCoder(CoderRegistry registry, Coder<T> inputCoder) {
      return topCombineFn.getAccumulatorCoder(
          registry, KvCoder.of(BigEndianIntegerCoder.of(), inputCoder));
    }

    @Override
    public Coder<Iterable<T>> getDefaultOutputCoder(
        CoderRegistry registry, Coder<T> inputCoder) {
      return IterableCoder.of(inputCoder);
    }

    @Override
    public void populateDisplayData(DisplayData.Builder builder) {
      super.populateDisplayData(builder);
      builder.add(DisplayData.item("sampleSize", sampleSize)
        .withLabel("Sample Size"));
    }
  }
}


text:
// ******************************************************************

// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.

// ******************************************************************

namespace Microsoft.Knowzy.WPF.Views
{
    public partial class AboutView
    {
        public AboutView()
        {
            InitializeComponent();
        }
    }
}


text:
<?php

namespace Passengers\Controller;

use Passengers\Controller\AppController;
use Cake\Event\Event;
use Cake\Core\Configure;
use Hackzilla\PasswordGenerator\Generator\ComputerPasswordGenerator;
use Cake\Auth\DefaultPasswordHasher;

/**
 * Users Controller
 *
 * @property Passengers\Model\Table\UsersTable $Users
 */
class UsersController extends AppController {

	public function initialize()
	{
		parent::initialize();
		$this->loadComponent('Cookie');
		$this->loadComponent('Platform.Email');
	}

	public function beforeFilter(Event $event)
	{
	    parent::beforeFilter($event);
		//Allow users to signin and signup
		$this->Auth->allow(['signin', 'signup', 'reset']);
	}

/**
 * View method
 *
 * @param string $id
 * @return void
 * @throws NotFoundException
 */
	public function view($id = null) {
		$user = $this->Users->get($id, [
			'contain' => []
		]);
		$this->set('user', $user);
	}

	/**
	 * Edit method
	 *
	 * @param string $id
	 * @return void
	 * @throws NotFoundException
	 */
	public function edit($id = null) {
		$user = $this->Users->get($id, [
			'contain' => []
		]);

		if ($this->request->is(['patch', 'post', 'put'])) {
			$user = $this->Users->patchEntity($user, $this->request->data);
			if ($this->Users->save($user)) {
				$this->Flash->success('The user has been saved.');
				return $this->redirect(['action' => 'index']);
			} else {
				$this->Flash->error('The user could not be saved. Please, try again.');
			}
		}
		$this->set(compact('user'));
	}

/**
 * SignIn method
 *
 * @return void
 * @throws NotFoundException
 */
	public function signin() {
		if ($this->request->is('post')) {
			$event = $this->dispatchEvent('Controller.Users.beforeSignIn');
			if ($event->isStopped()) {
				$this->Flash->error($event->result);
				$this->redirect(['action' => 'signin']);
			}
			if ($user = $this->Auth->identify()) {
				$this->Auth->setUser($user);
				$this->_setCookie();
				$event = $this->dispatchEvent('Controller.Users.afterSignIn');
				return $this->redirect($this->Auth->redirectUrl());
			}
			$this->Flash->error(__d('passengers', 'Invalid username or password, try again'));
		}
		$this->set('signupAllowed', Configure::read('App.allow_user_registration'));
	}

/**
 * SignOut method
 *
 * @return void
 */
	public function signout() {
		$this->Cookie->delete('RememberMe');
		return $this->redirect($this->Auth->logout());
	}

/**
 * SignOut method
 *
 * @return void
 */
	public function signup() {
		$signupAllowed = Configure::read('App.allow_user_registration');
		if (!$signupAllowed) {
			$this->Flash->error(__d('passengers', 'Registration is disabled. Please contact with resourse administration'));
			return $this->redirect(['action' => 'signin']);
		}
		$this->Users->addBehavior('Passengers.Passwordable', [
			'formField' => 'password_new',
			'formFieldRepeat' => 'password_confirm',
			'formFieldCurrent' => 'password_current',
		]);
        $user = $this->Users->newEntity();
		if ($this->request->is('post')&&$signupAllowed) {
            $user = $this->Users->patchEntity($user, $this->request->data);
            //TODO: make it configurable
            $user->role_id = 2;
            $user->active = 1;
            foreach($user->errors() as $field=>$error){
                $this->Flash->error(__d('passengers', implode("\n", $error)));
                return;
            }
			if(Configure::read('App.use_users_activation')){
				$user->active = 1;
				$user->activation_code = md5(time().$_data['password_new']);
			}
            $event = $this->dispatchEvent('Controller.Users.beforeSignUp', [$user]);
            if ($event->isStopped()) {
                $this->Flash->error($event->result);
                return;
            }
			$rememberPass = $user->password_new;
			if ($this->Users->save($user)) {
				$user->password = $rememberPass;
                //Deprecated! Email sending moved to event afterSignUp
                //$this->Email->send($user->email, ['user' => $user], [
				//	'subject' => __('Registration confirmation'),
				//	'template' => 'Passengers.signup'
				//]);
                $this->dispatchEvent('Controller.Users.afterSignUp', [$user]);
				$this->Flash->success(__d('passengers', 'Your account has been created.'));
				return $this->redirect(['action' => 'signin']);
			} else {
                foreach($user->errors() as $field=>$error){
                    $this->Flash->error(__d('passengers', implode(" ", $error)));
                }
			}
			$user->unsetProperty('password_new');
			$user->unsetProperty('password_confirm');
		}

		$this->set(compact('user'));
	}

    public function activate($code = null){
        if(!$code){
            throw new BadRequestException('There is no any code to activate');
        }
        $user = $this->Users->findByActivationCode($code)->first();
        if(!$user){
            throw new NotFoundException('There is no any user to activate');
        }
        $user = $this->Users->patchEntity($user, [
            'activation_code' => '',
            'active' => true,
        ]);
        if($this->Users->save($user)){
			$this->Auth->setUser($user->toArray());
            if($user->update_required){
                $this->Flash->warning(__d('passengers', 'We are not sure that your profile data is correct. Maybe you need to update your data.'));
                $this->redirect(['action' => 'edit']);
            }
        }
        $this->set('user', $user);
    }

    public function reset()
    {
        if ($this->request->is('post')) {
            $user = $this->Users->find('all', [
                'conditions' => [
                    'Users.active' => true,
                    'Users.email' => $this->request->data('email'),
                ]
            ])->first();

            if ($user) {
                $password = (new ComputerPasswordGenerator())
                    ->setOptionValue(ComputerPasswordGenerator::OPTION_UPPER_CASE, true)
                    ->setOptionValue(ComputerPasswordGenerator::OPTION_LOWER_CASE, true)
                    ->setOptionValue(ComputerPasswordGenerator::OPTION_NUMBERS, true)
                    ->setOptionValue(ComputerPasswordGenerator::OPTION_SYMBOLS, false)
                    ->generatePassword();

                $this->Users->addBehavior('Passengers.Passwordable', [
                    'confirm' => false,
                ]);
                $user = $this->Users->patchEntity($user, ['pwd' => $password]);
                if ($this->Users->save($user)) {
                    $user->password = $password;
                    //Deprecated. Old way to send reset email
                    //$this->Email->send($user->email, ['user' => $user], [
                    //    'subject' => __('Reset password'),
                    //    'template' => 'Passengers.reset'
                    //]);
                    $this->getMailer('Passengers.User')->send('resetPassword', [$user]);
                    $this->Flash->success('Please check email for new password.');
                    return $this->redirect(['action' => 'signin']);
                } else {
                    $this->Flash->error('The password could not be reseted. Please, try again.');
                }
            }

            $this->Flash->warning(__d('passengers', 'If the email you specified exists in our system, we\'ve sent a new password to it.'));
            return $this->redirect(['action' => 'signin']);
        }
    }

    public function password()
    {
        if ($this->request->is('post')) {
            $user = $this->Users->findById(
                $this->request->session()->read('Auth.User.id')
            )->first();

            if ((new DefaultPasswordHasher)->check($this->request->data('password_current'), $user->password)) {
                $this->Users->addBehavior('Passengers.Passwordable', [
                    'current' => true,
                    'formFieldCurrent' => 'password_current',
                    'formField' => 'password_new',
                    'formFieldRepeat' => 'password_confirm',
                ]);

                $user = $this->Users->patchEntity($user, $this->request->data);
                if ($this->Users->save($user)) {
                    $this->Flash->success('The password has been changed.');
                    return $this->redirect(['action' => 'password']);
                } else {
                    $this->Flash->error('The password could not be changed. Please, try again.');
                    return $this->redirect(['action' => 'password']);
                }
            }

            $this->Flash->error('Error. The password could not be changed. Please, try again.');
            return $this->redirect(['action' => 'password']);
        }
    }

	protected function _setCookie() {
		if($this->request->data('remember_me')) {
		    $this->Cookie->configKey(Configure::read('Passengers.rememberMe.cookieName'), [
		        'expires' => '+1 month',
		        'httpOnly' => true
		    ]);
		    $this->Cookie->write(Configure::read('Passengers.rememberMe.cookieName'), [
		        'username' => $this->request->data('username'),
		        'password' => $this->request->data('password')
		    ]);
		}
	}
}


text:
package rapture.exchange.rabbitmq;

import java.io.IOException;

import org.apache.log4j.Logger;

import rapture.exchange.TopicMessageHandler;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;

public class SubscriptionThread extends Thread {
	private static Logger logger = Logger
			.getLogger(SubscriptionThread.class);
	private QueueingConsumer consumer;
	private String exchange;
	private String topic;
	private Channel channel;
	private TopicMessageHandler handler;
	private boolean closeMe = false;

	public SubscriptionThread(String exchange, String topic, Channel channel,
			TopicMessageHandler handler) {
		this.channel = channel;
		this.exchange = exchange;
		this.topic = topic;
		this.handler = handler;
	}

	public boolean closeSubscription() {
		closeMe = true;
		return true;
	}

	@Override
	public void run() {
		logger.info("Running subscription thread for " + exchange + "," + topic);
		String queueName;
		try {
			queueName = channel.queueDeclare().getQueue();
			logger.info("Queue is " + queueName);
			channel.queueBind(queueName, exchange, topic);

			QueueingConsumer consumer = new QueueingConsumer(channel);
			channel.basicConsume(queueName, true, consumer);

			while (true) {
				QueueingConsumer.Delivery delivery = consumer.nextDelivery(500);
				if (delivery != null) {
					String message = new String(delivery.getBody());
					String sentTopic = delivery.getEnvelope().getRoutingKey();

					try {
					handler.deliverMessage(exchange, topic, sentTopic, message);
					} catch(Exception e) {
						logger.info("Socket closed");
						closeMe = true;
					}
				}
				if (closeMe) {
					logger.info("Stopping subscription thread");
					break;
				}
			}

			channel.queueDelete(queueName);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ShutdownSignalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ConsumerCancelledException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


text:
SYNONYM

#### According to
The Catalogue of Life, 3rd January 2011

#### Published in
Flore mycologique de la France et des pays limitrophes 418 (1888)

#### Original name
Boletus impolitus Fr., 1838

### Remarks
null


text:
include_recipe 'openstack-clients::_common'

python_pip 'python-glanceclient'


text:
from . import layers
from . import keras_layers
from . import keras_tensorflow_layers
from . import tensorflow_layers