问:
tf.TensorArray as a FIFO?
Here I was pointed to use tf.TensorArray instead of tf.Variable or tf.queue.FIFOQueue for making FIFO contained in custom layer. Is it an effective way? Exist any alternative here?

If it's the most effective method how can I replace self.queue.assign(tf.concat([self.queue[timesteps:, :], inputs], axis=0)) with methods of tf.TensorArray?

Code

class FIFOLayer(Layer):
    def __init__(self, window_size, **kwargs):
        super(FIFOLayer, self).__init__(**kwargs)

        self.window_size = window_size
        self.count = 0

    def build(self, input_shape):
        super(FIFOLayer, self).build(input_shape)

        self.queue = self.add_weight(
            name="queue",
            shape=(self.window_size, input_shape[-1]),
            initializer=tf.initializers.Constant(value=np.nan),
            trainable=False,
        )

    def call(self, inputs, training):
        timesteps = tf.shape(inputs)[0]

        # check if batch_size is more than queue capacity
        if timesteps > self.window_size:
            raise ValueError()

        # 1. append new state to queue
        self.queue.assign(tf.concat([self.queue[timesteps:, :], inputs], axis=0))
        self.count += timesteps

        # 2. feed-forward
        if self.count < self.window_size:
            # generate mask
            attention_mask = tf.cast(
                tf.math.reduce_all(
                    tf.math.logical_not(tf.math.is_nan(self.queue)), axis=-1
                ),
                dtype=tf.float32,
            )
            attention_mask = tf.matmul(
                attention_mask[..., tf.newaxis],
                attention_mask[..., tf.newaxis],
                transpose_b=True,
            )
            return self.queue[tf.newaxis, ...], attention_mask
        # !!! check overflow
        elif self.count > self.window_size:
            self.count = self.window_size

        return self.queue[tf.newaxis, ...], None

    @property
    def is_full(self):
        return self.count == self.window_size

    def clear(self):
        self.count = 0
        self.queue.assign(tf.fill(self.queue.shape, np.nan))

l = FIFOLayer(window_size=10)
for i in range(6):
    x = tf.random.normal((2, 12))
    y = l(x)
    print(y)

print(l.is_full, "\n\n")

l.clear()

print(l(x))
print(l.is_full, "\n\n")
```

答:
Using tf.TensorArray, you can try something like this:

import tensorflow as tf
import numpy as np
tf.random.set_seed(111)

class FIFOLayer(tf.keras.layers.Layer):
    def __init__(self, window_size, **kwargs):
        super(FIFOLayer, self).__init__(**kwargs)

        self.window_size = window_size
        self.count = 0

    def build(self, input_shape):
        super(FIFOLayer, self).build(input_shape)

        self.queue_array = tf.TensorArray(dtype=tf.float32, size=self.window_size)
        self.queue_array = self.queue_array.scatter(tf.range(self.window_size), tf.constant(np.nan)*tf.ones((self.window_size, input_shape[-1])))

    def call(self, inputs, training):
        timesteps = tf.shape(inputs)[0]

        # check if batch_size is more than queue capacity
        if timesteps > self.window_size:
            raise ValueError()

        self.queue_array = self.queue_array.scatter(tf.range(self.window_size), tf.concat([self.queue_array.gather(tf.range(timesteps, self.window_size)), inputs], axis=0))
        queue_tensor = self.queue_array.stack()
        self.count += timesteps
        # 2. feed-forward
        if self.count < self.window_size:
            # generate mask
            attention_mask = tf.cast(
                tf.math.reduce_all(
                    tf.math.logical_not(tf.math.is_nan(queue_tensor)), axis=-1
                ),
                dtype=tf.float32,
            )
            attention_mask = tf.matmul(
                attention_mask[..., tf.newaxis],
                attention_mask[..., tf.newaxis],
                transpose_b=True,
            )
            return queue_tensor[tf.newaxis, ...], attention_mask
        # !!! check overflow
        elif self.count > self.window_size:
            self.count = self.window_size

        return queue_tensor[tf.newaxis, ...], None

    @property
    def is_full(self):
        return self.count == self.window_size

    def clear(self):
        self.count = 0
        shape = tf.shape(self.queue_array.stack())[-1]
        self.queue_array = tf.TensorArray(dtype=tf.float32, size=self.window_size)
        self.queue_array = self.queue_array.scatter(tf.range(self.window_size), tf.constant(np.nan)*tf.ones((self.window_size, shape)))

l = FIFOLayer(window_size=10)
for i in range(6):
    x = tf.random.normal((2, 12))
    y = l(x)
    print(y)

print(l.is_full, "\n\n")

l.clear()

print(l(x))
print(l.is_full, "\n\n")
```
```(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [ 0.7558127 ,  1.5447265 ,  1.6315602 , -0.19868968,
          0.08828261,  0.01711658, -1.8133892 ,  0.12930395,
          0.47128937,  0.08567389, -1.7158676 , -0.5843805 ],
        [-0.7664911 , -0.7145203 , -1.089696  ,  0.14649415,
          0.03585422,  0.9916008 ,  0.9384322 ,  0.34755042,
         -0.09592161,  0.76490027, -1.2517685 , -1.5740465 ]]],
      dtype=float32)>, <tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 1., 1.],
       [0., 0., 0., 0., 0., 0., 0., 0., 1., 1.]], dtype=float32)>)
(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [ 0.7558127 ,  1.5447265 ,  1.6315602 , -0.19868968,
          0.08828261,  0.01711658, -1.8133892 ,  0.12930395,
          0.47128937,  0.08567389, -1.7158676 , -0.5843805 ],
        [-0.7664911 , -0.7145203 , -1.089696  ,  0.14649415,
          0.03585422,  0.9916008 ,  0.9384322 ,  0.34755042,
         -0.09592161,  0.76490027, -1.2517685 , -1.5740465 ],
        [-0.31995258, -0.43669155, -0.28932425, -0.06870204,
         -0.01291991,  1.171546  ,  0.75079876, -0.7693662 ,
          0.05902815,  0.60606545, -1.1038904 , -0.99837613],
        [-0.6687948 ,  0.22192897, -0.02249479, -0.08962449,
          1.2408841 ,  0.119805  , -0.53699344,  1.020805  ,
          0.9610218 ,  0.6133564 , -0.4358486 ,  2.733222  ]]],
      dtype=float32)>, <tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 1., 1., 1., 1.],
       [0., 0., 0., 0., 0., 0., 1., 1., 1., 1.],
       [0., 0., 0., 0., 0., 0., 1., 1., 1., 1.],
       [0., 0., 0., 0., 0., 0., 1., 1., 1., 1.]], dtype=float32)>)
(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [ 0.7558127 ,  1.5447265 ,  1.6315602 , -0.19868968,
          0.08828261,  0.01711658, -1.8133892 ,  0.12930395,
          0.47128937,  0.08567389, -1.7158676 , -0.5843805 ],
        [-0.7664911 , -0.7145203 , -1.089696  ,  0.14649415,
          0.03585422,  0.9916008 ,  0.9384322 ,  0.34755042,
         -0.09592161,  0.76490027, -1.2517685 , -1.5740465 ],
        [-0.31995258, -0.43669155, -0.28932425, -0.06870204,
         -0.01291991,  1.171546  ,  0.75079876, -0.7693662 ,
          0.05902815,  0.60606545, -1.1038904 , -0.99837613],
        [-0.6687948 ,  0.22192897, -0.02249479, -0.08962449,
          1.2408841 ,  0.119805  , -0.53699344,  1.020805  ,
          0.9610218 ,  0.6133564 , -0.4358486 ,  2.733222  ],
        [-0.33772066,  0.80799913, -0.00896128,  1.606288  ,
          1.1561627 ,  0.17252289,  0.2451608 ,  1.4633939 ,
         -0.9294784 ,  0.42795137, -0.3016553 , -1.1823792 ],
        [ 0.30927372,  0.3482721 ,  1.0262096 , -0.97228396,
         -0.55333287, -0.7914886 ,  1.0115404 , -0.5656188 ,
          0.30958036, -0.8476673 ,  2.4919312 ,  0.9093976 ]]],
      dtype=float32)>, <tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.]], dtype=float32)>)
(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [ 0.7558127 ,  1.5447265 ,  1.6315602 , -0.19868968,
          0.08828261,  0.01711658, -1.8133892 ,  0.12930395,
          0.47128937,  0.08567389, -1.7158676 , -0.5843805 ],
        [-0.7664911 , -0.7145203 , -1.089696  ,  0.14649415,
          0.03585422,  0.9916008 ,  0.9384322 ,  0.34755042,
         -0.09592161,  0.76490027, -1.2517685 , -1.5740465 ],
        [-0.31995258, -0.43669155, -0.28932425, -0.06870204,
         -0.01291991,  1.171546  ,  0.75079876, -0.7693662 ,
          0.05902815,  0.60606545, -1.1038904 , -0.99837613],
        [-0.6687948 ,  0.22192897, -0.02249479, -0.08962449,
          1.2408841 ,  0.119805  , -0.53699344,  1.020805  ,
          0.9610218 ,  0.6133564 , -0.4358486 ,  2.733222  ],
        [-0.33772066,  0.80799913, -0.00896128,  1.606288  ,
          1.1561627 ,  0.17252289,  0.2451608 ,  1.4633939 ,
         -0.9294784 ,  0.42795137, -0.3016553 , -1.1823792 ],
        [ 0.30927372,  0.3482721 ,  1.0262096 , -0.97228396,
         -0.55333287, -0.7914886 ,  1.0115404 , -0.5656188 ,
          0.30958036, -0.8476673 ,  2.4919312 ,  0.9093976 ],
        [-0.44241378, -0.6971805 , -0.37439492,  1.0154608 ,
         -0.34494257,  0.1988212 , -0.9541314 , -0.44339198,
          0.162457  , -0.31033182, -0.34568167,  1.0341203 ],
        [-0.89020306, -0.8646532 ,  0.13348487, -0.6604107 ,
          0.07642484,  1.3407826 ,  0.79119945, -0.7598532 ,
          0.85146165, -0.2791065 , -0.4600736 ,  0.809218  ]]],
      dtype=float32)>, <tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.]], dtype=float32)>)
(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[ 7.5581270e-01,  1.5447265e+00,  1.6315602e+00, -1.9868968e-01,
          8.8282607e-02,  1.7116580e-02, -1.8133892e+00,  1.2930395e-01,
          4.7128937e-01,  8.5673891e-02, -1.7158676e+00, -5.8438051e-01],
        [-7.6649112e-01, -7.1452028e-01, -1.0896960e+00,  1.4649415e-01,
          3.5854220e-02,  9.9160081e-01,  9.3843222e-01,  3.4755042e-01,
         -9.5921606e-02,  7.6490027e-01, -1.2517685e+00, -1.5740465e+00],
        [-3.1995258e-01, -4.3669155e-01, -2.8932425e-01, -6.8702042e-02,
         -1.2919909e-02,  1.1715460e+00,  7.5079876e-01, -7.6936620e-01,
          5.9028149e-02,  6.0606545e-01, -1.1038904e+00, -9.9837613e-01],
        [-6.6879481e-01,  2.2192897e-01, -2.2494787e-02, -8.9624494e-02,
          1.2408841e+00,  1.1980500e-01, -5.3699344e-01,  1.0208050e+00,
          9.6102178e-01,  6.1335641e-01, -4.3584859e-01,  2.7332220e+00],
        [-3.3772066e-01,  8.0799913e-01, -8.9612845e-03,  1.6062880e+00,
          1.1561627e+00,  1.7252289e-01,  2.4516080e-01,  1.4633939e+00,
         -9.2947841e-01,  4.2795137e-01, -3.0165529e-01, -1.1823792e+00],
        [ 3.0927372e-01,  3.4827209e-01,  1.0262096e+00, -9.7228396e-01,
         -5.5333287e-01, -7.9148859e-01,  1.0115404e+00, -5.6561881e-01,
          3.0958036e-01, -8.4766728e-01,  2.4919312e+00,  9.0939760e-01],
        [-4.4241378e-01, -6.9718051e-01, -3.7439492e-01,  1.0154608e+00,
         -3.4494257e-01,  1.9882120e-01, -9.5413142e-01, -4.4339198e-01,
          1.6245700e-01, -3.1033182e-01, -3.4568167e-01,  1.0341203e+00],
        [-8.9020306e-01, -8.6465323e-01,  1.3348487e-01, -6.6041070e-01,
          7.6424837e-02,  1.3407826e+00,  7.9119945e-01, -7.5985318e-01,
          8.5146165e-01, -2.7910650e-01, -4.6007359e-01,  8.0921799e-01],
        [-6.7833281e-01,  4.7877081e-02, -2.0416839e+00, -1.5634586e+00,
         -5.1782840e-01,  5.2898288e-01, -1.4573561e+00,  4.6455118e-01,
         -3.2871577e-01, -1.5697428e+00,  1.4454672e-01,  8.2387424e-01],
        [ 2.5552011e-03,  1.2834518e+00,  4.1382611e-01,  1.6535892e+00,
          7.8654990e-02, -1.2952465e-01,  3.6811054e-01,  1.1675907e+00,
          9.6434945e-01, -4.2399967e-01, -1.3700709e-01, -5.2056974e-01]]],
      dtype=float32)>, None)
(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[-3.1995258e-01, -4.3669155e-01, -2.8932425e-01, -6.8702042e-02,
         -1.2919909e-02,  1.1715460e+00,  7.5079876e-01, -7.6936620e-01,
          5.9028149e-02,  6.0606545e-01, -1.1038904e+00, -9.9837613e-01],
        [-6.6879481e-01,  2.2192897e-01, -2.2494787e-02, -8.9624494e-02,
          1.2408841e+00,  1.1980500e-01, -5.3699344e-01,  1.0208050e+00,
          9.6102178e-01,  6.1335641e-01, -4.3584859e-01,  2.7332220e+00],
        [-3.3772066e-01,  8.0799913e-01, -8.9612845e-03,  1.6062880e+00,
          1.1561627e+00,  1.7252289e-01,  2.4516080e-01,  1.4633939e+00,
         -9.2947841e-01,  4.2795137e-01, -3.0165529e-01, -1.1823792e+00],
        [ 3.0927372e-01,  3.4827209e-01,  1.0262096e+00, -9.7228396e-01,
         -5.5333287e-01, -7.9148859e-01,  1.0115404e+00, -5.6561881e-01,
          3.0958036e-01, -8.4766728e-01,  2.4919312e+00,  9.0939760e-01],
        [-4.4241378e-01, -6.9718051e-01, -3.7439492e-01,  1.0154608e+00,
         -3.4494257e-01,  1.9882120e-01, -9.5413142e-01, -4.4339198e-01,
          1.6245700e-01, -3.1033182e-01, -3.4568167e-01,  1.0341203e+00],
        [-8.9020306e-01, -8.6465323e-01,  1.3348487e-01, -6.6041070e-01,
          7.6424837e-02,  1.3407826e+00,  7.9119945e-01, -7.5985318e-01,
          8.5146165e-01, -2.7910650e-01, -4.6007359e-01,  8.0921799e-01],
        [-6.7833281e-01,  4.7877081e-02, -2.0416839e+00, -1.5634586e+00,
         -5.1782840e-01,  5.2898288e-01, -1.4573561e+00,  4.6455118e-01,
         -3.2871577e-01, -1.5697428e+00,  1.4454672e-01,  8.2387424e-01],
        [ 2.5552011e-03,  1.2834518e+00,  4.1382611e-01,  1.6535892e+00,
          7.8654990e-02, -1.2952465e-01,  3.6811054e-01,  1.1675907e+00,
          9.6434945e-01, -4.2399967e-01, -1.3700709e-01, -5.2056974e-01],
        [ 1.3070145e+00, -6.7240512e-01,  1.9308577e+00,  1.7688200e-03,
          3.0533668e-01,  6.5813893e-01,  5.2471739e-01,  2.1659613e+00,
         -8.7725663e-01,  3.5695407e-01, -1.2751107e+00, -7.7276069e-01],
        [-4.3180370e-01, -1.1814500e+00,  2.4167557e-01,  5.7490116e-01,
          5.6998456e-01, -7.4528801e-01, -9.1826969e-01, -7.3694932e-01,
         -1.2400552e+00,  1.6947891e+00, -2.6127639e-01,  7.8419834e-01]]],
      dtype=float32)>, None)
True

(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [ 1.3070145e+00, -6.7240512e-01,  1.9308577e+00,  1.7688200e-03,
          3.0533668e-01,  6.5813893e-01,  5.2471739e-01,  2.1659613e+00,
         -8.7725663e-01,  3.5695407e-01, -1.2751107e+00, -7.7276069e-01],
        [-4.3180370e-01, -1.1814500e+00,  2.4167557e-01,  5.7490116e-01,
          5.6998456e-01, -7.4528801e-01, -9.1826969e-01, -7.3694932e-01,
         -1.2400552e+00,  1.6947891e+00, -2.6127639e-01,  7.8419834e-01]]],
      dtype=float32)>, <tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 1., 1.],
       [0., 0., 0., 0., 0., 0., 0., 0., 1., 1.]], dtype=float32)>)
tf.Tensor(False, shape=(), dtype=bool)
```

On a side note, using tf.queue.FIFOQueue is really slow.

Title: tf.TensorArray as a FIFO?

Body:
Using tf.TensorArray, you can try something like this:

import tensorflow as tf
import numpy as np
tf.random.set_seed(111)

class FIFOLayer(tf.keras.layers.Layer):
    def __init__(self, window_size, **kwargs):
        super(FIFOLayer, self).__init__(**kwargs)

        self.window_size = window_size
        self.count = 0

    def build(self, input_shape):
        super(FIFOLayer, self).build(input_shape)

        self.queue_array = tf.TensorArray(dtype=tf.float32, size=self.window_size)
        self.queue_array = self.queue_array.scatter(tf.range(self.window_size), tf.constant(np.nan)*tf.ones((self.window_size, input_shape[-1])))

    def call(self, inputs, training):
        timesteps = tf.shape(inputs)[0]

        # check if batch_size is more than queue capacity
        if timesteps > self.window_size:
            raise ValueError()

        self.queue_array = self.queue_array.scatter(tf.range(self.window_size), tf.concat([self.queue_array.gather(tf.range(timesteps, self.window_size)), inputs], axis=0))
        queue_tensor = self.queue_array.stack()
        self.count += timesteps
        # 2. feed-forward
        if self.count < self.window_size:
            # generate mask
            attention_mask = tf.cast(
                tf.math.reduce_all(
                    tf.math.logical_not(tf.math.is_nan(queue_tensor)), axis=-1
                ),
                dtype=tf.float32,
            )
            attention_mask = tf.matmul(
                attention_mask[..., tf.newaxis],
                attention_mask[..., tf.newaxis],
                transpose_b=True,
            )
            return queue_tensor[tf.newaxis, ...], attention_mask
        # !!! check overflow
        elif self.count > self.window_size:
            self.count = self.window_size

        return queue_tensor[tf.newaxis, ...], None

    @property
    def is_full(self):
        return self.count == self.window_size

    def clear(self):
        self.count = 0
        shape = tf.shape(self.queue_array.stack())[-1]
        self.queue_array = tf.TensorArray(dtype=tf.float32, size=self.window_size)
        self.queue_array = self.queue_array.scatter(tf.range(self.window_size), tf.constant(np.nan)*tf.ones((self.window_size, shape)))

l = FIFOLayer(window_size=10)
for i in range(6):
    x = tf.random.normal((2, 12))
    y = l(x)
    print(y)

print(l.is_full, "\n\n")

l.clear()

print(l(x))
print(l.is_full, "\n\n")

(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [ 0.7558127 ,  1.5447265 ,  1.6315602 , -0.19868968,
          0.08828261,  0.01711658, -1.8133892 ,  0.12930395,
          0.47128937,  0.08567389, -1.7158676 , -0.5843805 ],
        [-0.7664911 , -0.7145203 , -1.089696  ,  0.14649415,
          0.03585422,  0.9916008 ,  0.9384322 ,  0.34755042,
         -0.09592161,  0.76490027, -1.2517685 , -1.5740465 ]]],
      dtype=float32)>, <tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 1., 1.],
       [0., 0., 0., 0., 0., 0., 0., 0., 1., 1.]], dtype=float32)>)
(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [ 0.7558127 ,  1.5447265 ,  1.6315602 , -0.19868968,
          0.08828261,  0.01711658, -1.8133892 ,  0.12930395,
          0.47128937,  0.08567389, -1.7158676 , -0.5843805 ],
        [-0.7664911 , -0.7145203 , -1.089696  ,  0.14649415,
          0.03585422,  0.9916008 ,  0.9384322 ,  0.34755042,
         -0.09592161,  0.76490027, -1.2517685 , -1.5740465 ],
        [-0.31995258, -0.43669155, -0.28932425, -0.06870204,
         -0.01291991,  1.171546  ,  0.75079876, -0.7693662 ,
          0.05902815,  0.60606545, -1.1038904 , -0.99837613],
        [-0.6687948 ,  0.22192897, -0.02249479, -0.08962449,
          1.2408841 ,  0.119805  , -0.53699344,  1.020805  ,
          0.9610218 ,  0.6133564 , -0.4358486 ,  2.733222  ]]],
      dtype=float32)>, <tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 1., 1., 1., 1.],
       [0., 0., 0., 0., 0., 0., 1., 1., 1., 1.],
       [0., 0., 0., 0., 0., 0., 1., 1., 1., 1.],
       [0., 0., 0., 0., 0., 0., 1., 1., 1., 1.]], dtype=float32)>)
(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [ 0.7558127 ,  1.5447265 ,  1.6315602 , -0.19868968,
          0.08828261,  0.01711658, -1.8133892 ,  0.12930395,
          0.47128937,  0.08567389, -1.7158676 , -0.5843805 ],
        [-0.7664911 , -0.7145203 , -1.089696  ,  0.14649415,
          0.03585422,  0.9916008 ,  0.9384322 ,  0.34755042,
         -0.09592161,  0.76490027, -1.2517685 , -1.5740465 ],
        [-0.31995258, -0.43669155, -0.28932425, -0.06870204,
         -0.01291991,  1.171546  ,  0.75079876, -0.7693662 ,
          0.05902815,  0.60606545, -1.1038904 , -0.99837613],
        [-0.6687948 ,  0.22192897, -0.02249479, -0.08962449,
          1.2408841 ,  0.119805  , -0.53699344,  1.020805  ,
          0.9610218 ,  0.6133564 , -0.4358486 ,  2.733222  ],
        [-0.33772066,  0.80799913, -0.00896128,  1.606288  ,
          1.1561627 ,  0.17252289,  0.2451608 ,  1.4633939 ,
         -0.9294784 ,  0.42795137, -0.3016553 , -1.1823792 ],
        [ 0.30927372,  0.3482721 ,  1.0262096 , -0.97228396,
         -0.55333287, -0.7914886 ,  1.0115404 , -0.5656188 ,
          0.30958036, -0.8476673 ,  2.4919312 ,  0.9093976 ]]],
      dtype=float32)>, <tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.],
       [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.]], dtype=float32)>)
(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [        nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan,
                 nan,         nan,         nan,         nan],
        [ 0.7558127 ,  1.5447265 ,  1.6315602 , -0.19868968,
          0.08828261,  0.01711658, -1.8133892 ,  0.12930395,
          0.47128937,  0.08567389, -1.7158676 , -0.5843805 ],
        [-0.7664911 , -0.7145203 , -1.089696  ,  0.14649415,
          0.03585422,  0.9916008 ,  0.9384322 ,  0.34755042,
         -0.09592161,  0.76490027, -1.2517685 , -1.5740465 ],
        [-0.31995258, -0.43669155, -0.28932425, -0.06870204,
         -0.01291991,  1.171546  ,  0.75079876, -0.7693662 ,
          0.05902815,  0.60606545, -1.1038904 , -0.99837613],
        [-0.6687948 ,  0.22192897, -0.02249479, -0.08962449,
          1.2408841 ,  0.119805  , -0.53699344,  1.020805  ,
          0.9610218 ,  0.6133564 , -0.4358486 ,  2.733222  ],
        [-0.33772066,  0.80799913, -0.00896128,  1.606288  ,
          1.1561627 ,  0.17252289,  0.2451608 ,  1.4633939 ,
         -0.9294784 ,  0.42795137, -0.3016553 , -1.1823792 ],
        [ 0.30927372,  0.3482721 ,  1.0262096 , -0.97228396,
         -0.55333287, -0.7914886 ,  1.0115404 , -0.5656188 ,
          0.30958036, -0.8476673 ,  2.4919312 ,  0.9093976 ],
        [-0.44241378, -0.6971805 , -0.37439492,  1.0154608 ,
         -0.34494257,  0.1988212 , -0.9541314 , -0.44339198,
          0.162457  , -0.31033182, -0.34568167,  1.0341203 ],
        [-0.89020306, -0.8646532 ,  0.13348487, -0.6604107 ,
          0.07642484,  1.3407826 ,  0.79119945, -0.7598532 ,
          0.85146165, -0.2791065 , -0.4600736 ,  0.809218  ]]],
      dtype=float32)>, <tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],
       [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.]], dtype=float32)>)
(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[ 7.5581270e-01,  1.5447265e+00,  1.6315602e+00, -1.9868968e-01,
          8.8282607e-02,  1.7116580e-02, -1.8133892e+00,  1.2930395e-01,
          4.7128937e-01,  8.5673891e-02, -1.7158676e+00, -5.8438051e-01],
        [-7.6649112e-01, -7.1452028e-01, -1.0896960e+00,  1.4649415e-01,
          3.5854220e-02,  9.9160081e-01,  9.3843222e-01,  3.4755042e-01,
         -9.5921606e-02,  7.6490027e-01, -1.2517685e+00, -1.5740465e+00],
        [-3.1995258e-01, -4.3669155e-01, -2.8932425e-01, -6.8702042e-02,
         -1.2919909e-02,  1.1715460e+00,  7.5079876e-01, -7.6936620e-01,
          5.9028149e-02,  6.0606545e-01, -1.1038904e+00, -9.9837613e-01],
        [-6.6879481e-01,  2.2192897e-01, -2.2494787e-02, -8.9624494e-02,
          1.2408841e+00,  1.1980500e-01, -5.3699344e-01,  1.0208050e+00,
          9.6102178e-01,  6.1335641e-01, -4.3584859e-01,  2.7332220e+00],
        [-3.3772066e-01,  8.0799913e-01, -8.9612845e-03,  1.6062880e+00,
          1.1561627e+00,  1.7252289e-01,  2.4516080e-01,  1.4633939e+00,
         -9.2947841e-01,  4.2795137e-01, -3.0165529e-01, -1.1823792e+00],
        [ 3.0927372e-01,  3.4827209e-01,  1.0262096e+00, -9.7228396e-01,
         -5.5333287e-01, -7.9148859e-01,  1.0115404e+00, -5.6561881e-01,
          3.0958036e-01, -8.4766728e-01,  2.4919312e+00,  9.0939760e-01],
        [-4.4241378e-01, -6.9718051e-01, -3.7439492e-01,  1.0154608e+00,
         -3.4494257e-01,  1.9882120e-01, -9.5413142e-01, -4.4339198e-01,
          1.6245700e-01, -3.1033182e-01, -3.4568167e-01,  1.0341203e+00],
        [-8.9020306e-01, -8.6465323e-01,  1.3348487e-01, -6.6041070e-01,
          7.6424837e-02,  1.3407826e+00,  7.9119945e-01, -7.5985318e-01,
          8.5146165e-01, -2.7910650e-01, -4.6007359e-01,  8.0921799e-01],
        [-6.7833281e-01,  4.7877081e-02, -2.0416839e+00, -1.5634586e+00,
         -5.1782840e-01,  5.2898288e-01, -1.4573561e+00,  4.6455118e-01,
         -3.2871577e-01, -1.5697428e+00,  1.4454672e-01,  8.2387424e-01],
        [ 2.5552011e-03,  1.2834518e+00,  4.1382611e-01,  1.6535892e+00,
          7.8654990e-02, -1.2952465e-01,  3.6811054e-01,  1.1675907e+00,
          9.6434945e-01, -4.2399967e-01, -1.3700709e-01, -5.2056974e-01]]],
      dtype=float32)>, None)
(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[-3.1995258e-01, -4.3669155e-01, -2.8932425e-01, -6.8702042e-02,
         -1.2919909e-02,  1.1715460e+00,  7.5079876e-01, -7.6936620e-01,
          5.9028149e-02,  6.0606545e-01, -1.1038904e+00, -9.9837613e-01],
        [-6.6879481e-01,  2.2192897e-01, -2.2494787e-02, -8.9624494e-02,
          1.2408841e+00,  1.1980500e-01, -5.3699344e-01,  1.0208050e+00,
          9.6102178e-01,  6.1335641e-01, -4.3584859e-01,  2.7332220e+00],
        [-3.3772066e-01,  8.0799913e-01, -8.9612845e-03,  1.6062880e+00,
          1.1561627e+00,  1.7252289e-01,  2.4516080e-01,  1.4633939e+00,
         -9.2947841e-01,  4.2795137e-01, -3.0165529e-01, -1.1823792e+00],
        [ 3.0927372e-01,  3.4827209e-01,  1.0262096e+00, -9.7228396e-01,
         -5.5333287e-01, -7.9148859e-01,  1.0115404e+00, -5.6561881e-01,
          3.0958036e-01, -8.4766728e-01,  2.4919312e+00,  9.0939760e-01],
        [-4.4241378e-01, -6.9718051e-01, -3.7439492e-01,  1.0154608e+00,
         -3.4494257e-01,  1.9882120e-01, -9.5413142e-01, -4.4339198e-01,
          1.6245700e-01, -3.1033182e-01, -3.4568167e-01,  1.0341203e+00],
        [-8.9020306e-01, -8.6465323e-01,  1.3348487e-01, -6.6041070e-01,
          7.6424837e-02,  1.3407826e+00,  7.9119945e-01, -7.5985318e-01,
          8.5146165e-01, -2.7910650e-01, -4.6007359e-01,  8.0921799e-01],
        [-6.7833281e-01,  4.7877081e-02, -2.0416839e+00, -1.5634586e+00,
         -5.1782840e-01,  5.2898288e-01, -1.4573561e+00,  4.6455118e-01,
         -3.2871577e-01, -1.5697428e+00,  1.4454672e-01,  8.2387424e-01],
        [ 2.5552011e-03,  1.2834518e+00,  4.1382611e-01,  1.6535892e+00,
          7.8654990e-02, -1.2952465e-01,  3.6811054e-01,  1.1675907e+00,
          9.6434945e-01, -4.2399967e-01, -1.3700709e-01, -5.2056974e-01],
        [ 1.3070145e+00, -6.7240512e-01,  1.9308577e+00,  1.7688200e-03,
          3.0533668e-01,  6.5813893e-01,  5.2471739e-01,  2.1659613e+00,
         -8.7725663e-01,  3.5695407e-01, -1.2751107e+00, -7.7276069e-01],
        [-4.3180370e-01, -1.1814500e+00,  2.4167557e-01,  5.7490116e-01,
          5.6998456e-01, -7.4528801e-01, -9.1826969e-01, -7.3694932e-01,
         -1.2400552e+00,  1.6947891e+00, -2.6127639e-01,  7.8419834e-01]]],
      dtype=float32)>, None)
True

(<tf.Tensor: shape=(1, 10, 12), dtype=float32, numpy=
array([[[           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [           nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan,
                    nan,            nan,            nan,            nan],
        [ 1.3070145e+00, -6.7240512e-01,  1.9308577e+00,  1.7688200e-03,
          3.0533668e-01,  6.5813893e-01,  5.2471739e-01,  2.1659613e+00,
         -8.7725663e-01,  3.5695407e-01, -1.2751107e+00, -7.7276069e-01],
        [-4.3180370e-01, -1.1814500e+00,  2.4167557e-01,  5.7490116e-01,
          5.6998456e-01, -7.4528801e-01, -9.1826969e-01, -7.3694932e-01,
         -1.2400552e+00,  1.6947891e+00, -2.6127639e-01,  7.8419834e-01]]],
      dtype=float32)>, <tf.Tensor: shape=(10, 10), dtype=float32, numpy=
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 1., 1.],
       [0., 0., 0., 0., 0., 0., 0., 0., 1., 1.]], dtype=float32)>)
tf.Tensor(False, shape=(), dtype=bool)

On a side note, using tf.queue.FIFOQueue is really slow.


来源: stackexchange

问:
Cognos report studio last day of month
I'm using the following filter, but it's not including any transactions from the last day of the month.  I think this filter assumes 12:01am.  I need to include through 11:59:59PM.

[TRL_DATE]<=_last_of_month(?ReportDate?)

答:
```<code>cast([TRL_DATE], date) <= cast(_last_of_month(?ReportDate?), date)
</code>```

ContentLicense: CC BY-SA 4.0

Title: Cognos report studio last day of month


问:
How to add taint to AWS EKS node Group?
I am having one AWS EKS Cluster up & running. I need to add node group with taint. So that I can deploy the pod on particular node group in EKS. I can do it in azure AKS using the following command.

az aks nodepool add --resource-group rg-xx --cluster-name aks-xxx --name np1 --node-count 1 --node-vm-size xxx --node-taints key=value:NoSchedule --no-wait
```

How to achieve same in AWS EKS?

答:
You can Add like this in AWS:EKS:NodeGroup Cloudformation Template</p>
```<code>Taints:
  - Effect: "NO_SCHEDULE"
    Key: !Ref NodeGroupName
    Value: "dedicated"
</code>```

Title: How to add taint to AWS EKS node Group?

Body:
I am having one AWS EKS Cluster up & running. I need to add node group with taint. So that I can deploy the pod on particular node group in EKS. I can do it in azure AKS using the following command.

az aks nodepool add --resource-group rg-xx --cluster-name aks-xxx --name np1 --node-count 1 --node-vm-size xxx --node-taints key=value:NoSchedule --no-wait

How to achieve same in AWS EKS?

Body:
You can Add like this in AWS:EKS:NodeGroup Cloudformation Template

Taints:
  - Effect: "NO_SCHEDULE"
    Key: !Ref NodeGroupName
    Value: "dedicated"


问:
How to get only specific fields with Google Directory API users.list
The documentation (https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list#query-parameters) says you can use customFieldMask to get only specific fields. I cannot figure out the format/structure of this property.

For example, given this for a user:

```<code>{
  "kind": "admin#directory#user",
  "id": "...",
  "etag": "\"...\"",
  "primaryEmail": "...",
  "name": {
    "givenName": "...",
    "familyName": "...",
    "fullName": "......"
  },
  "emails": [
    {
      "address": "...",
      "primary": true
    }
  ]
}
</code>```

I only want to get primaryEmail but I can't figure out how.

答: Send "users(primaryEmail)" in the fields parameter, not in the customFieldMask parameter.

Body:
The documentation (https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list#query-parameters) says you can use customFieldMask to get only specific fields. I cannot figure out the format/structure of this property.

For example, given this for a user:

{
  "kind": "admin#directory#user",
  "id": "...",
  "etag": "\"...\"",
  "primaryEmail": "...",
  "name": {
    "givenName": "...",
    "familyName": "...",
    "fullName": "......"
  },
  "emails": [
    {
      "address": "...",
      "primary": true
    }
  ]
}

I only want to get primaryEmail but I can't figure out how.


问:
How to vary the color according to the elevation?
I would like to achieve something like this :

However, I fail to get the Y coordinates (before projection) of a given point inside the Fragment Shader.

Here is my Fragment Shader :

```<code>precision mediump float;

vec4 blue = vec4(0.0, 0.0, 1.0, 1.0);
vec4 pink = vec4(1.0, 0.0, 1.0, 1.0);

void main() {
    float y_coordinates = ???
    gl_FragColor = mix(blue , pink , y_coordinates);
}
</code>```

Should I send them from the Vertex Shader?

答:
Pass the vertex coordinate from the vertex shader to the fragment shader. e.g.:

Vertex shader

attribute vec3 pos;
varying vec3 vertex_pos;

void main() {
    vertex_pos = pos;
    gl_Position = ...;
}
```
<p>Fragment shader:</p>
<pre class="lang-c prettyprint-override"><code>precision mediump float;
varying vec3 vertex_pos;

vec4 blue = vec4(0.0, 0.0, 1.0, 1.0);
vec4 pink = vec4(1.0, 0.0, 1.0, 1.0);

void main() {
    gl_FragColor = mix(blue, pink, vertex_pos.y);
}
</code>```

Note, the y component of the vertex coordinate must be in range [0.0, 1.0]. If your coordinate is in another range, you must map it to the range [0.0, 1.0]. e.g. map from [a, b] to [0.0, 1.0]:

float w = (vertex_pos.y - a) / (b-a);
gl_FragColor = mix(blue, pink, w);
```

Body:
I would like to achieve something like this :

However, I fail to get the Y coordinates (before projection) of a given point inside the Fragment Shader.

Here is my Fragment Shader :

precision mediump float;

vec4 blue = vec4(0.0, 0.0, 1.0, 1.0);
vec4 pink = vec4(1.0, 0.0, 1.0, 1.0);

void main() {
    float y_coordinates = ???
    gl_FragColor = mix(blue , pink , y_coordinates);
}

Should I send them from the Vertex Shader?

Body:
Pass the vertex coordinate from the vertex shader to the fragment shader. e.g.:

Vertex shader

attribute vec3 pos;
varying vec3 vertex_pos;

void main() {
    vertex_pos = pos;
    gl_Position = ...;
}

Fragment shader:

precision mediump float;
varying vec3 vertex_pos;

vec4 blue = vec4(0.0, 0.0, 1.0, 1.0);
vec4 pink = vec4(1.0, 0.0, 1.0, 1.0);

void main() {
    gl_FragColor = mix(blue, pink, vertex_pos.y);
}

Note, the y component of the vertex coordinate must be in range [0.0, 1.0]. If your coordinate is in another range, you must map it to the range [0.0, 1.0]. e.g. map from [a, b] to [0.0, 1.0]:

float w = (vertex_pos.y - a) / (b-a);
gl_FragColor = mix(blue, pink, w);


问:
Condition to check that an array of variables is defined
Let's say I want to verify that a variable is neither null or undefined.

I would genuinely do:

```<code>let foo: string | undefined | null;

if (foo !== undefined && foo !== null) {
  // foo is a string
}
</code>```

Now, I would like to create a function that can do the same check on multiple variable at once.

I have tried:

```<code>const isSet = <T>(...values: T[]): values is Exclude<T, null | undefined>[] => {
  return values.every(value => value !== undefined && value !== null)
};
</code>```

However, I get a Typescript error "A type predicate cannot reference a rest parameter", which make sense.

What do you suggest?

答:
You cannot write a type guard against multiple parameters. I would change your strategy to something like this:

function isSet<T,>(...values: T[]) : null | NonNullable<T>[] {
    return values.every(value => value !== undefined && value !== null)
        ? values as NonNullable<T>[]
        : null;
}
```
<p>This is an instance where the compiler isn't smart enough to assert the type of <code>values</code>, so you have to give it a little help with <code>as NonNullable<T>[]</code>. Rather than return a boolean (or type guard), return the values you just
verified.</p>
<p>Usage looks like this:</p>
<pre class="lang-ts prettyprint-override"><code>let a = null, b = undefined, c = 0;

// guarded: null | number[];
let guarded = isSet(a, b, c);

if (guarded) {
  // guarded: number[]
  let [x, y, z] = guarded;
}
</code>```

Playground

Body:
Let's say I want to verify that a variable is neither null or undefined.

I would genuinely do:

let foo: string | undefined | null;

if (foo !== undefined && foo !== null) {
  // foo is a string
}

Now, I would like to create a function that can do the same check on multiple variable at once.

I have tried:

const isSet = <T>(...values: T[]): values is Exclude<T, null | undefined>[] => {
  return values.every(value => value !== undefined && value !== null)
};

However, I get a Typescript error "A type predicate cannot reference a rest parameter", which make sense.

What do you suggest?

Body:
You cannot write a type guard against multiple parameters. I would change your strategy to something like this:

function isSet<T,>(...values: T[]) : null | NonNullable<T>[] {
    return values.every(value => value !== undefined && value !== null)
        ? values as NonNullable<T>[]
        : null;
}

This is an instance where the compiler isn't smart enough to assert the type of values, so you have to give it a little help with as NonNullable<T>[]. Rather than return a boolean (or type guard), return the values you just
verified.

Usage looks like this:

let a = null, b = undefined, c = 0;

// guarded: null | number[];
let guarded = isSet(a, b, c);

if (guarded) {
  // guarded: number[]
  let [x, y, z] = guarded;
}

Playground


问:
Getting a warning when using a pyodbc Connection object with pandas
I am trying to make sense of the following error that I started getting when I setup my python code to run on a VM server, which has 3.9.5 installed instead of 3.8.5 on my desktop.  Not sure that matters, but it could be part of the reason.

The error

```<code>C:\ProgramData\Miniconda3\lib\site-packages\pandas\io\sql.py:758: UserWarning: pandas only support SQLAlchemy connectable(engine/connection) or
database string URI or sqlite3 DBAPI2 connection
other DBAPI2 objects are not tested, please consider using SQLAlchemy
  warnings.warn(
</code>```

This is within a fairly simple .py file that imports pyodbc & sqlalchemy fwiw.  A fairly generic/simple version of sql calls that yields the warning is:

```<code>myserver_string = "xxxxxxxxx,nnnn"
db_string = "xxxxxx"

cnxn = "Driver={ODBC Driver 17 for SQL Server};Server=tcp:"+myserver_string+";Database="+db_string +";TrustServerCertificate=no;Connection Timeout=600;Authentication=ActiveDirectoryIntegrated;"

def readAnyTable(tablename, date):
    conn = pyodbc.connect(cnxn)

    query_result = pd.read_sql_query(
            '''
                 SELECT *
                 FROM [{0}].[dbo].[{1}]
                where Asof >= '{2}'
            '''.format(db_string,tablename,date,), conn)

    conn.close()

    return query_result
</code>```

All the examples I have seen using pyodbc in python look fairly similar.  Is pyodbc becoming deprecated?  Is there a better way to achieve similar results without warning?

答:
Is pyodbc becoming deprecated?

No. For at least the last couple of years pandas' documentation has clearly stated that it wants either

a SQLAlchemy Connectable (i.e., an Engine or Connection object),

a string containing a SQLAlchemy connection URL, or

a SQLite DBAPI connection.

(The switch-over to SQLAlchemy was almost universal, but they continued supporting SQLite connections for backwards compatibility.) People have been passing other DBAPI connections (like pyodbc Connection objects) for read operations and pandas hasn't complained … until now.

Is there a better way to achieve similar results without warning?

Yes. You can take your existing ODBC connection string and use it to create a SQLAlchemy Engine object as described in the SQLAlchemy 1.4 documentation:

from sqlalchemy.engine import URL
connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=dagger;DATABASE=test;UID=user;PWD=password"
connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})

from sqlalchemy import create_engine
engine = create_engine(connection_url)
```
<p>Then use the SQLAlchemy <code>engine</code> to work with the pandas methods you require. For example, with SQLAlchemy 2.0 and pandas 1.5.3:</p>
<pre class="lang-py prettyprint-override"><code>import pandas as pd
import sqlalchemy as sa

# …

with engine.begin() as conn:
    df = pd.read_sql_query(sa.text("SELECT 'thing' as txt"), conn)
</code>```

Body:
I am trying to make sense of the following error that I started getting when I setup my python code to run on a VM server, which has 3.9.5 installed instead of 3.8.5 on my desktop.  Not sure that matters, but it could be part of the reason.

The error

C:\ProgramData\Miniconda3\lib\site-packages\pandas\io\sql.py:758: UserWarning: pandas only support SQLAlchemy connectable(engine/connection) or
database string URI or sqlite3 DBAPI2 connection
other DBAPI2 objects are not tested, please consider using SQLAlchemy
  warnings.warn(

This is within a fairly simple .py file that imports pyodbc & sqlalchemy fwiw.  A fairly generic/simple version of sql calls that yields the warning is:

myserver_string = "xxxxxxxxx,nnnn"
db_string = "xxxxxx"

cnxn = "Driver={ODBC Driver 17 for SQL Server};Server=tcp:"+myserver_string+";Database="+db_string +";TrustServerCertificate=no;Connection Timeout=600;Authentication=ActiveDirectoryIntegrated;"

def readAnyTable(tablename, date):
    conn = pyodbc.connect(cnxn)

    query_result = pd.read_sql_query(
            '''
                 SELECT *
                 FROM [{0}].[dbo].[{1}]
                where Asof >= '{2}'
            '''.format(db_string,tablename,date,), conn)

    conn.close()

    return query_result

All the examples I have seen using pyodbc in python look fairly similar.  Is pyodbc becoming deprecated?  Is there a better way to achieve similar results without warning?

Body:
Is pyodbc becoming deprecated?

No. For at least the last couple of years pandas' documentation has clearly stated that it wants either

a SQLAlchemy Connectable (i.e., an Engine or Connection object),

a string containing a SQLAlchemy connection URL, or

a SQLite DBAPI connection.

(The switch-over to SQLAlchemy was almost universal, but they continued supporting SQLite connections for backwards compatibility.) People have been passing other DBAPI connections (like pyodbc Connection objects) for read operations and pandas hasn't complained … until now.

Is there a better way to achieve similar results without warning?

Yes. You can take your existing ODBC connection string and use it to create a SQLAlchemy Engine object as described in the SQLAlchemy 1.4 documentation:

from sqlalchemy.engine import URL
connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=dagger;DATABASE=test;UID=user;PWD=password"
connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})

from sqlalchemy import create_engine
engine = create_engine(connection_url)

Then use the SQLAlchemy engine to work with the pandas methods you require. For example, with SQLAlchemy 2.0 and pandas 1.5.3:

import pandas as pd
import sqlalchemy as sa

# …

with engine.begin() as conn:
    df = pd.read_sql_query(sa.text("SELECT 'thing' as txt"), conn)


问:
HTML and XSLT transformation
I have simple question,

Can I use XSLT to transform HTML to other HTML.

If it's possible, how i should do this?

答: If your input HTML is well-formed XML, then you can use XSLT to transform your input HTML in any text format.</p>

ContentLicense: CC BY-SA 3.0

Title: HTML and XSLT transformation


问:
M-x slime [No match] in emacs under mac osx
I am learning common lisp now. I want to use slime in emacs, but when I press M-x slime it says [No match]

Initially I download the CVS snapshot and put this in my .emacs file

```<code>(setq inferior-lisp-program "/opt/local/bin/sbcl") ; your Lisp system
(add-to-list ’load-path "~/.slime") ; your SLIME directory
(require ’slime)
(slime-setup)
</code>```

Then I set up again followed this tread Setting the SLIME in emacs

But the problem did not disappear.

Also, M-x run-lisp does not work. When I tried to do so, buffer displayed "Searching for program: No such file or directory, lisp"

答:
Configure this into your init.el using c-x c-f ~/.init.el.

```<code>(require 'package)

(add-to-list 'package-archives '("melpa"     . "http://melpa.milkbox.net/packages/"))
(add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/"))

(package-initialize)

(unless package-archive-contents
  (package-refresh-contents))
</code>```

Reinstall both sbcl and slime.

I used roswell for the whole setup. This site should help.

To use slime in emacs, you add it to using c-x c-f  ~/.emacs.

```<code>(load (expand-file-name "~/.roswell/helper.el"))
</code>```

This should all work.

Body:
I am learning common lisp now. I want to use slime in emacs, but when I press M-x slime it says [No match]

Initially I download the CVS snapshot and put this in my .emacs file

(setq inferior-lisp-program "/opt/local/bin/sbcl") ; your Lisp system
(add-to-list ’load-path "~/.slime") ; your SLIME directory
(require ’slime)
(slime-setup)

Then I set up again followed this tread Setting the SLIME in emacs

But the problem did not disappear.

Also, M-x run-lisp does not work. When I tried to do so, buffer displayed "Searching for program: No such file or directory, lisp"

Body:
Configure this into your init.el using c-x c-f ~/.init.el.

(require 'package)

(add-to-list 'package-archives '("melpa"     . "http://melpa.milkbox.net/packages/"))
(add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/"))

(package-initialize)

(unless package-archive-contents
  (package-refresh-contents))

Reinstall both sbcl and slime.

I used roswell for the whole setup. This site should help.

To use slime in emacs, you add it to using c-x c-f  ~/.emacs.

(load (expand-file-name "~/.roswell/helper.el"))

This should all work.


问:
How to make view that uses a web scraper load faster
I have a view that outputs destination city results on a page. As part of the view, a web scraper searches for flight prices from the user's area to the destination city. After adding the web scraper to the view it now takes about 8 seconds to load. I was wondering how I can make the view load quicker, maybe with threading or another alternative.

views.py

```<code>def results(request, result1, result2, result3, result4, result5, result6, broad_variable1, broad_variable2, broad_variable3, specific_variable_dictionary, user_city):
    result1 = City.objects.filter(city=result1).first()
    result2 = City.objects.filter(city=result2).first()
    result3 = City.objects.filter(city=result3).first()
    result4 = City.objects.filter(city=result4).first()
    result5 = City.objects.filter(city=result5).first()
    result6 = City.objects.filter(city=result6).first()

    # get the first user selected specific variable value for each result
    result1_value1 = City.objects.filter(city=result1.city).values(broad_variable1)[0][broad_variable1]
    result2_value1 = City.objects.filter(city=result2.city).values(broad_variable1)[0][broad_variable1]
    result3_value1 = City.objects.filter(city=result3.city).values(broad_variable1)[0][broad_variable1]
    result4_value1 = City.objects.filter(city=result4.city).values(broad_variable1)[0][broad_variable1]
    result5_value1 = City.objects.filter(city=result5.city).values(broad_variable1)[0][broad_variable1]
    result6_value1 = City.objects.filter(city=result6.city).values(broad_variable1)[0][broad_variable1]

    # assign variables before referencing them
    result1_value2 = None
    result2_value2 = None
    result3_value2 = None
    result4_value2 = None
    result5_value2 = None
    result6_value2 = None

    # check if the user chose a second variable
    # get the second user selected specific variable value for each result
    if broad_variable2 != "Nothing":
        result1_value2 = City.objects.filter(city=result1.city).values(broad_variable2)[0][broad_variable2]
        result2_value2 = City.objects.filter(city=result2.city).values(broad_variable2)[0][broad_variable2]
        result3_value2 = City.objects.filter(city=result3.city).values(broad_variable2)[0][broad_variable2]
        result4_value2 = City.objects.filter(city=result4.city).values(broad_variable2)[0][broad_variable2]
        result5_value2 = City.objects.filter(city=result5.city).values(broad_variable2)[0][broad_variable2]
        result6_value2 = City.objects.filter(city=result6.city).values(broad_variable2)[0][broad_variable2]

    # assign variables before referencing them
    result1_value3 = None
    result2_value3 = None
    result3_value3 = None
    result4_value3 = None
    result5_value3 = None
    result6_value3 = None

    # check if the user chose a third variable
    # get the third user selected specific variable value for each result
    if broad_variable3 != "Nothing":
        result1_value3 = City.objects.filter(city=result1.city).values(broad_variable3)[0][broad_variable3]
        result2_value3 = City.objects.filter(city=result2.city).values(broad_variable3)[0][broad_variable3]
        result3_value3 = City.objects.filter(city=result3.city).values(broad_variable3)[0][broad_variable3]
        result4_value3 = City.objects.filter(city=result4.city).values(broad_variable3)[0][broad_variable3]
        result5_value3 = City.objects.filter(city=result5.city).values(broad_variable3)[0][broad_variable3]
        result6_value3 = City.objects.filter(city=result6.city).values(broad_variable3)[0][broad_variable3]

    # create list of cities
    city_list = [result1.city, result2.city, result3.city, result4.city, result5.city, result6.city]

    # create price list
    prices_list = []

    # set origin for flight
    origin = user_city

    # set headers
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36"
    }

    for city in city_list:

        # set search query
        url = "https://google.com/search?q=" + origin + " to " + city + " Google Flights"

        response = requests.get(url, headers=headers)

        soup = BeautifulSoup(response.text, 'lxml')

        # get price element
        prices = soup.find("span", attrs={"class": "GARawf"})
        if prices != None:
            prices_list.append(prices.text.strip())
        else:
            prices_list.append("Not Available")

    # change string dictionary into actual dictionary
    specific_variable_dictionary = ast.literal_eval(specific_variable_dictionary)

    context = {
        "result1": result1, "result2": result2, "result3": result3, "result4": result4, "result5": result5,
        "result6": result6, "result1_value1": result1_value1, "result2_value1": result2_value1, "result3_value1": result3_value1,
        "result4_value1": result4_value1, "result5_value1": result5_value1, "result6_value1": result6_value1, "result1_value2": result1_value2,
        "result2_value2": result2_value2, "result3_value2": result3_value2, "result4_value2": result4_value2,
        "result5_value2": result5_value2, "result6_value2": result6_value2, "result1_value3": result1_value3, "result2_value3": result2_value3,
        "result3_value3": result3_value3, "result4_value3": result4_value3, "result5_value3": result5_value3, "result6_value3": result6_value3,
        "broad_variable1": broad_variable1, "broad_variable2": broad_variable2, "broad_variable3": broad_variable3,
        "specific_variable_dictionary": specific_variable_dictionary, "prices_list": prices_list
    }
    return render(request, 'Discovery_App/results.html', context)
</code>```

答:
You are doing a lot of query. You should try to minimize the number of query you do.
For example, you can do City.objects.filter(city__in=[v1, v2, etc]) instead of

City.objects.filter(city=v1)
City.objects.filter(city=v2)
etc.
```

For more complexe query you should check .annotate()

https://docs.djangoproject.com/en/4.0/topics/db/aggregation/

Accessing to the DB has a cost. It's like making a call each time you need something instead of making a list of what you need and ask it in one call.

Body:
I have a view that outputs destination city results on a page. As part of the view, a web scraper searches for flight prices from the user's area to the destination city. After adding the web scraper to the view it now takes about 8 seconds to load. I was wondering how I can make the view load quicker, maybe with threading or another alternative.

views.py

def results(request, result1, result2, result3, result4, result5, result6, broad_variable1, broad_variable2, broad_variable3, specific_variable_dictionary, user_city):
    result1 = City.objects.filter(city=result1).first()
    result2 = City.objects.filter(city=result2).first()
    result3 = City.objects.filter(city=result3).first()
    result4 = City.objects.filter(city=result4).first()
    result5 = City.objects.filter(city=result5).first()
    result6 = City.objects.filter(city=result6).first()

    # get the first user selected specific variable value for each result
    result1_value1 = City.objects.filter(city=result1.city).values(broad_variable1)[0][broad_variable1]
    result2_value1 = City.objects.filter(city=result2.city).values(broad_variable1)[0][broad_variable1]
    result3_value1 = City.objects.filter(city=result3.city).values(broad_variable1)[0][broad_variable1]
    result4_value1 = City.objects.filter(city=result4.city).values(broad_variable1)[0][broad_variable1]
    result5_value1 = City.objects.filter(city=result5.city).values(broad_variable1)[0][broad_variable1]
    result6_value1 = City.objects.filter(city=result6.city).values(broad_variable1)[0][broad_variable1]

    # assign variables before referencing them
    result1_value2 = None
    result2_value2 = None
    result3_value2 = None
    result4_value2 = None
    result5_value2 = None
    result6_value2 = None

    # check if the user chose a second variable
    # get the second user selected specific variable value for each result
    if broad_variable2 != "Nothing":
        result1_value2 = City.objects.filter(city=result1.city).values(broad_variable2)[0][broad_variable2]
        result2_value2 = City.objects.filter(city=result2.city).values(broad_variable2)[0][broad_variable2]
        result3_value2 = City.objects.filter(city=result3.city).values(broad_variable2)[0][broad_variable2]
        result4_value2 = City.objects.filter(city=result4.city).values(broad_variable2)[0][broad_variable2]
        result5_value2 = City.objects.filter(city=result5.city).values(broad_variable2)[0][broad_variable2]
        result6_value2 = City.objects.filter(city=result6.city).values(broad_variable2)[0][broad_variable2]

    # assign variables before referencing them
    result1_value3 = None
    result2_value3 = None
    result3_value3 = None
    result4_value3 = None
    result5_value3 = None
    result6_value3 = None

    # check if the user chose a third variable
    # get the third user selected specific variable value for each result
    if broad_variable3 != "Nothing":
        result1_value3 = City.objects.filter(city=result1.city).values(broad_variable3)[0][broad_variable3]
        result2_value3 = City.objects.filter(city=result2.city).values(broad_variable3)[0][broad_variable3]
        result3_value3 = City.objects.filter(city=result3.city).values(broad_variable3)[0][broad_variable3]
        result4_value3 = City.objects.filter(city=result4.city).values(broad_variable3)[0][broad_variable3]
        result5_value3 = City.objects.filter(city=result5.city).values(broad_variable3)[0][broad_variable3]
        result6_value3 = City.objects.filter(city=result6.city).values(broad_variable3)[0][broad_variable3]

    # create list of cities
    city_list = [result1.city, result2.city, result3.city, result4.city, result5.city, result6.city]

    # create price list
    prices_list = []

    # set origin for flight
    origin = user_city

    # set headers
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36"
    }

    for city in city_list:

        # set search query
        url = "https://google.com/search?q=" + origin + " to " + city + " Google Flights"

        response = requests.get(url, headers=headers)

        soup = BeautifulSoup(response.text, 'lxml')

        # get price element
        prices = soup.find("span", attrs={"class": "GARawf"})
        if prices != None:
            prices_list.append(prices.text.strip())
        else:
            prices_list.append("Not Available")

    # change string dictionary into actual dictionary
    specific_variable_dictionary = ast.literal_eval(specific_variable_dictionary)

    context = {
        "result1": result1, "result2": result2, "result3": result3, "result4": result4, "result5": result5,
        "result6": result6, "result1_value1": result1_value1, "result2_value1": result2_value1, "result3_value1": result3_value1,
        "result4_value1": result4_value1, "result5_value1": result5_value1, "result6_value1": result6_value1, "result1_value2": result1_value2,
        "result2_value2": result2_value2, "result3_value2": result3_value2, "result4_value2": result4_value2,
        "result5_value2": result5_value2, "result6_value2": result6_value2, "result1_value3": result1_value3, "result2_value3": result2_value3,
        "result3_value3": result3_value3, "result4_value3": result4_value3, "result5_value3": result5_value3, "result6_value3": result6_value3,
        "broad_variable1": broad_variable1, "broad_variable2": broad_variable2, "broad_variable3": broad_variable3,
        "specific_variable_dictionary": specific_variable_dictionary, "prices_list": prices_list
    }
    return render(request, 'Discovery_App/results.html', context)

Body:
You are doing a lot of query. You should try to minimize the number of query you do.
For example, you can do City.objects.filter(city__in=[v1, v2, etc]) instead of

City.objects.filter(city=v1)
City.objects.filter(city=v2)
etc.

For more complexe query you should check .annotate()

https://docs.djangoproject.com/en/4.0/topics/db/aggregation/

Accessing to the DB has a cost. It's like making a call each time you need something instead of making a list of what you need and ask it in one call.


问:
React 16: Error: Unable to find node on an unmounted component
I recently upgraded my project from React v15.2.1 to 16.4.1 and my Sidebar component is throwing the following error:

```<code>Error: Unable to find node on an unmounted component. bundle.js line 1326 > eval:42:15
invariant
webpack:///./node_modules/fbjs/lib/invariant.js?:42:15
findCurrentFiberUsingSlowPath
webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:3817:7
findCurrentHostFiber
webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:3886:23
findHostInstance
webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:16824:19
findDOMNode
webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:17310:12
handleClickOutside
webpack:///./src/components/simulator/sidebar/Sidebar.js?:99:31
handleClickOutside self-hosted:984:17
</code>```

Based on the error message, I believe the error is happening when calling ReactDOM.findDOMNode(this) in the handleClickOutside(event) method.

The component that I am using can be found here: https://ashiknesin.com/blog/build-custom-sidebar-component-react/,
I have changed it a little bit to this:

```<code>import React from 'react'
import ReactDOM from 'react-dom'
import classNames from 'classnames'
import SimulatorForm from './SimulatorForm'
import './Sidebar.scss'

const  openForm = require('../../../public/icons/si-glyph-arrow-left.svg');
const closeForm = require('../../../public/icons/si-glyph-arrow-right.svg');
const   pinForm = require('../../../public/icons/si-glyph-pin-location-love.svg');
const unpinForm = require('../../../public/icons/si-glyph-pin-location-delete.svg');

class Sidebar extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            showMenu: false,
            isMenuPinned: false,
            formIcon: openForm,
            pinIcon: pinForm,
            modelsDescription: props.modelsDescription
        }
        // Methods to pin/unpin the Form
        this.toggleMenu = this.toggleMenu.bind(this)
        this.pinMenu = this.pinMenu.bind(this)
        // Handlers
        this.handleSelectedModelChange = this.props.handleSelectedModelChange
        this.handleNewMasterGraphsData = this.props.handleNewMasterGraphsData
        this.handleNewResults = this.props.handleNewResults
    }
    componentWillReceiveProps(nextProps) {
        this.setState({ modelsDescription: nextProps.modelsDescription});
    }
    componentDidMount() {
        document.addEventListener('click', this.handleClickOutside.bind(this), true);
    }

    componentWillUnmount() {
        document.removeEventListener('click', this.handleClickOutside.bind(this), true);
    }
    pinMenu() {
        this.setState({
            isMenuPinned: !this.state.isMenuPinned,
            pinIcon: this.state.isMenuPinned ? pinForm : unpinForm
        });
    }
    toggleMenu() {
        this.setState({
            showMenu: !this.state.showMenu,
            formIcon: this.state.showMenu ? openForm : closeForm
        });
    }
    handleClickOutside(event) {
        if (!this.state.isMenuPinned) {
            const domNode = ReactDOM.findDOMNode(this);

            if ((!domNode || !domNode.contains(event.target))) {
                this.setState({
                    showMenu: false,
                    formIcon: openForm
                });
            }
        }
    }

    render() {
        const showMenu = this.state.showMenu;
        const sidebarClass = classNames({
            'sidebar': true,
            'sidebar-menu-expanded': showMenu,
            'sidebar-menu-collapsed': !showMenu
        });

        const elementsClass = classNames({
            'expanded-element': true,
            'is-hidden': !showMenu,
        });

        return (
            <nav className={sidebarClass}>
                <img className="menuIcon" src={this.state.formIcon} height="42" width="42" onClick={this.toggleMenu} />
                <ul>
                    <li>
                        {
                            this.state.showMenu ? <img className="pinIcon" src={this.state.pinIcon}  height="42" width="42" onClick={this.pinMenu} /> : null
                        }
                    </li>
                    <li>
                        {
                            this.state.showMenu ?  <SimulatorForm modelsDescription={this.state.modelsDescription} handleSelectedModelChange={this.handleSelectedModelChange}
                                                    handleNewMasterGraphsData={this.handleNewMasterGraphsData} handleNewResults={this.handleNewResults} /> : null
                        }
                    </li>
                </ul>
            </nav>
        )
    }
}

export default Sidebar
</code>```

Lastly, the error is only thrown when I reload the page. If I don't, it seems to be working perfectly fine. Do you have any suggestions or recommendations to make sure the error is not thrown again?

I have been reading about this on-line and I couldn't find a fix for it. Also, I dont't think this is listed as a breaking change but I could be very wrong.

答: I just use material ui Menu from another package and got same issue , it strange but installing latest (similar to the spa version) react-dom solve the issue.</p>

Body:
I recently upgraded my project from React v15.2.1 to 16.4.1 and my Sidebar component is throwing the following error:

Error: Unable to find node on an unmounted component. bundle.js line 1326 > eval:42:15
invariant
webpack:///./node_modules/fbjs/lib/invariant.js?:42:15
findCurrentFiberUsingSlowPath
webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:3817:7
findCurrentHostFiber
webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:3886:23
findHostInstance
webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:16824:19
findDOMNode
webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:17310:12
handleClickOutside
webpack:///./src/components/simulator/sidebar/Sidebar.js?:99:31
handleClickOutside self-hosted:984:17

Based on the error message, I believe the error is happening when calling ReactDOM.findDOMNode(this) in the handleClickOutside(event) method.

The component that I am using can be found here: https://ashiknesin.com/blog/build-custom-sidebar-component-react/,
I have changed it a little bit to this:

import React from 'react'
import ReactDOM from 'react-dom'
import classNames from 'classnames'
import SimulatorForm from './SimulatorForm'
import './Sidebar.scss'

const  openForm = require('../../../public/icons/si-glyph-arrow-left.svg');
const closeForm = require('../../../public/icons/si-glyph-arrow-right.svg');
const   pinForm = require('../../../public/icons/si-glyph-pin-location-love.svg');
const unpinForm = require('../../../public/icons/si-glyph-pin-location-delete.svg');

class Sidebar extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            showMenu: false,
            isMenuPinned: false,
            formIcon: openForm,
            pinIcon: pinForm,
            modelsDescription: props.modelsDescription
        }
        // Methods to pin/unpin the Form
        this.toggleMenu = this.toggleMenu.bind(this)
        this.pinMenu = this.pinMenu.bind(this)
        // Handlers
        this.handleSelectedModelChange = this.props.handleSelectedModelChange
        this.handleNewMasterGraphsData = this.props.handleNewMasterGraphsData
        this.handleNewResults = this.props.handleNewResults
    }
    componentWillReceiveProps(nextProps) {
        this.setState({ modelsDescription: nextProps.modelsDescription});
    }
    componentDidMount() {
        document.addEventListener('click', this.handleClickOutside.bind(this), true);
    }

    componentWillUnmount() {
        document.removeEventListener('click', this.handleClickOutside.bind(this), true);
    }
    pinMenu() {
        this.setState({
            isMenuPinned: !this.state.isMenuPinned,
            pinIcon: this.state.isMenuPinned ? pinForm : unpinForm
        });
    }
    toggleMenu() {
        this.setState({
            showMenu: !this.state.showMenu,
            formIcon: this.state.showMenu ? openForm : closeForm
        });
    }
    handleClickOutside(event) {
        if (!this.state.isMenuPinned) {
            const domNode = ReactDOM.findDOMNode(this);

            if ((!domNode || !domNode.contains(event.target))) {
                this.setState({
                    showMenu: false,
                    formIcon: openForm
                });
            }
        }
    }

    render() {
        const showMenu = this.state.showMenu;
        const sidebarClass = classNames({
            'sidebar': true,
            'sidebar-menu-expanded': showMenu,
            'sidebar-menu-collapsed': !showMenu
        });

        const elementsClass = classNames({
            'expanded-element': true,
            'is-hidden': !showMenu,
        });

        return (

                        {
                            this.state.showMenu ?  : null
                        }

                        {
                            this.state.showMenu ?  <SimulatorForm modelsDescription={this.state.modelsDescription} handleSelectedModelChange={this.handleSelectedModelChange}
                                                    handleNewMasterGraphsData={this.handleNewMasterGraphsData} handleNewResults={this.handleNewResults} /> : null
                        }

        )
    }
}

export default Sidebar

Lastly, the error is only thrown when I reload the page. If I don't, it seems to be working perfectly fine. Do you have any suggestions or recommendations to make sure the error is not thrown again?

I have been reading about this on-line and I couldn't find a fix for it. Also, I dont't think this is listed as a breaking change but I could be very wrong.


问:
Different results using OneVsRestClassifier(KNeighborsClassifier(n_neighbors=2)) compared to KNeighborsClassifier(n_neighbors=2)
I'm implementing a multi-class classifier and I'm getting different results when wrapping KNN in a multi-class classifier.

Unsure why as I understood KNN worked for multiclass already?

```<code>y = rock_df['Sample_type']
X = rock_df[col_list]

def model_eval(model, X,y):
    """ Function implements classifier model on X and y with a 0.33 test hold out, stratified by y and returns accuracy and standard deviation

    Inputs:

        model: The ML model to be tested

        X: the cleaned and preprocessed data (normalized, and NAN dealt with)

        y: Target labels for input data X

    """
    #Split train /test
    X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.33, random_state=42, stratify = y)

    n = X_test.size
    #Fit model
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)

    #Scoring
    confusion_matrix(y_test,y_pred)
    balanced_accuracy_score( y_test, y_pred)

    scores = cross_val_score(model, X, y, cv=3)
    mean= scores.mean()
    sd = scores.std()
    print("For {} : {:.1%} accuracy on cross validation, with a standard deviation of {:.1%}".format(model, mean, sd) )

    # binomial confidence interval - 95% -- confirm difference with SD
    #interval = 1.96 * sqrt( (mean * (1 - mean)) /n )
    #print('Confidence Interval: {:.3%}'.format(interval) )

    #return balanced_accuracy_score, confusion_matrix

model = OneVsRestClassifier(KNeighborsClassifier(n_neighbors=2))
model_eval(model, X,y)

model = KNeighborsClassifier(n_neighbors=2)
model_eval(model, X,y)
</code>```

First model I get:
For OneVsRestClassifier(estimator=KNeighborsClassifier(n_neighbors=2)) : 78.6% accuracy on cross validation, with a standard deviation of 5.8%

second:

For KNeighborsClassifier(n_neighbors=2) : 83.3% accuracy on cross validation, with a standard deviation of 8.9%

thanks

答: It is OK that you have different results. KNeighborsClassifier doesn't employ one-vs-rest strategy; majority vote works with 3 and more classes and there is no need to have OvR in the original implementation. But trying OneVsRestClassifier might be useful as well. I believe that generally decision boundaries will be different. Here I played with Iris dataset to get decision boundaries using KNeighborsClassifier(n_neighbors=5) and OneVsRestClassifier(KNeighborsClassifier(n_neighbors=5)):

Body:
I'm implementing a multi-class classifier and I'm getting different results when wrapping KNN in a multi-class classifier.

Unsure why as I understood KNN worked for multiclass already?

y = rock_df['Sample_type']
X = rock_df[col_list]

def model_eval(model, X,y):
    """ Function implements classifier model on X and y with a 0.33 test hold out, stratified by y and returns accuracy and standard deviation

    Inputs:

        model: The ML model to be tested

        X: the cleaned and preprocessed data (normalized, and NAN dealt with)

        y: Target labels for input data X

    """
    #Split train /test
    X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.33, random_state=42, stratify = y)

    n = X_test.size
    #Fit model
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)

    #Scoring
    confusion_matrix(y_test,y_pred)
    balanced_accuracy_score( y_test, y_pred)

    scores = cross_val_score(model, X, y, cv=3)
    mean= scores.mean()
    sd = scores.std()
    print("For {} : {:.1%} accuracy on cross validation, with a standard deviation of {:.1%}".format(model, mean, sd) )

    # binomial confidence interval - 95% -- confirm difference with SD
    #interval = 1.96 * sqrt( (mean * (1 - mean)) /n )
    #print('Confidence Interval: {:.3%}'.format(interval) )

    #return balanced_accuracy_score, confusion_matrix

model = OneVsRestClassifier(KNeighborsClassifier(n_neighbors=2))
model_eval(model, X,y)

model = KNeighborsClassifier(n_neighbors=2)
model_eval(model, X,y)

First model I get:
For OneVsRestClassifier(estimator=KNeighborsClassifier(n_neighbors=2)) : 78.6% accuracy on cross validation, with a standard deviation of 5.8%

second:

For KNeighborsClassifier(n_neighbors=2) : 83.3% accuracy on cross validation, with a standard deviation of 8.9%

thanks


问:
How to present PHPhotoPicker which contains only photos user gave permission to use in Swift?
Since iOS 14 there is a new API for photo selection called PHPhotoPicker. Alongside that, newly feature has been added and it allows users to give permission for only selected photos to be accessed by the iOS app.

Considering that, I want to be able to show only photos which user granted access to when opening instance of PHPhotoPicker inside my app.

Code I have so far:

```<code>        switch PHPhotoLibrary.authorizationStatus(for: .readWrite) {
        case .authorized:
            //present PHPhotoPicker with content of entire photo library
                var config = PHPickerConfiguration()
                config.filter = .images
                let vc = PHPickerViewController(configuration: config)
                vc.delegate = self
                viewController.present(vc, animated: true, completion: nil)
        case .limited:
            //present PHPhotoPicker with only photos user granted access to
        }

</code>```

As far as i've done my research it is possible to present PHPPicker which lets users choose photos to which they grant access (see code bellow), but not possible to actually show any kind of image picker which shows only photos with granted access.

```<code>PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
</code>```

Apple documentation isn't really lengthy which surprises me because it makes no sense to be able to present PHPhotoPicker which allows you to give access to certain photos, but then not have a way to present those photos in an image picker.

EDIT:

WhatsApp app example:

• In settings user can select photos which he is fine for the app to have access to:

•Then when he opens the Whatsapp app and tries to send a photo he gets Photo picker noted with number 2 on the picture below:

If he didn't choose "Selected photos" but instead chose "All photos" then he would have photo picker of an entire library shown on second picture and marked with "1. Access to all photos".

So my question is: How to present image picker with only selected photos(second picture number 2.) instead of image picker with entire photo library(second picture number 1.)

答:
этого нельзя сделать - https://developer.apple.com/forums/thread/658485

Even if your app is under Limited Photos Library mode, PHPicker will still show the entire photos library (all photos and videos can be selected by the user as well).

Body:
Since iOS 14 there is a new API for photo selection called PHPhotoPicker. Alongside that, newly feature has been added and it allows users to give permission for only selected photos to be accessed by the iOS app.

Considering that, I want to be able to show only photos which user granted access to when opening instance of PHPhotoPicker inside my app.

Code I have so far:

        switch PHPhotoLibrary.authorizationStatus(for: .readWrite) {
        case .authorized:
            //present PHPhotoPicker with content of entire photo library
                var config = PHPickerConfiguration()
                config.filter = .images
                let vc = PHPickerViewController(configuration: config)
                vc.delegate = self
                viewController.present(vc, animated: true, completion: nil)
        case .limited:
            //present PHPhotoPicker with only photos user granted access to
        }

As far as i've done my research it is possible to present PHPPicker which lets users choose photos to which they grant access (see code bellow), but not possible to actually show any kind of image picker which shows only photos with granted access.

PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)

Apple documentation isn't really lengthy which surprises me because it makes no sense to be able to present PHPhotoPicker which allows you to give access to certain photos, but then not have a way to present those photos in an image picker.

EDIT:

WhatsApp app example:

• In settings user can select photos which he is fine for the app to have access to:

•Then when he opens the Whatsapp app and tries to send a photo he gets Photo picker noted with number 2 on the picture below:

If he didn't choose "Selected photos" but instead chose "All photos" then he would have photo picker of an entire library shown on second picture and marked with "1. Access to all photos".

So my question is: How to present image picker with only selected photos(second picture number 2.) instead of image picker with entire photo library(second picture number 1.)


问:
How to show all Visual Studio's code snippets in Resharper's IntelliSense?
Resharper's IntelliSense only shows its own Live Templates but not Visual Studio's code snippets.

Is there a way to make Resharper IntelliSense to show all VS code snippets?

In Templates Explorer of Resharper I found that it has many Live Snippets marked as Imported Visual C# Snippets

But it didn't import all of them, only some of them. For example, Resharper didn't import propfull code snippet from Visual Studio(as you can see on the first screenshot propfull isn't on the list):

I can't find a feature in Resharper to import VS code snippets manually, one by one, or all of them together. I tried Import... button in Live Templates Manager in Resharper but apparently it only allows to import Live Templates(.DotSettings and .XML files, not .snippets files), but not Code Snippets files. Is it possible to import VS code snippets somehow?

All I want is to somehow make all VS code snippets work in Resharper IntelliSense. Maybe there's some third party tool that allows to convert snippets into Live Templates or something? Thanks.

答:
Just for completion that is how i did
This picture shows the code. As Alexander did.

This picture the configuration needed in Resharper Template editor
in Visual Studio

Another way to insert templates are the Mnemonic package extension of Resharper. That package has a lot of live templates for C#.
See extension


问:
JavaScript arrays with functions
Planned output:

```<code>The array has 10 elements.
The average of the values is 36.1
</code>```

I plan two functions: addNumber() and printInfo().

Function: addNumber() reads a value from a text input field on the HTML document (id="num") and adds it at the end of the array.

Another function: printInfo() outputs the amount of elements in the array to the console, then the average of their values.

HTML document has two buttons for calling the functions.

I wrote:

```<code>let arr = [];
arr.push (function addNumber() {
let element = document.getElementById("num");
})
function printInfo() {
console.log (arr.length );
}
</code>```

How to use buttons for calling the functions in HTML?

How to count average of values in the array?

答:
You have a couple of errors in your code.
First create the function outside of arr.push().
Second, you should call the function from somewhere for it to work. Also, next time provide the HTML too. You can see a working example here:

let arr = [];

function addNumber() {
    const element = document.getElementById("num");
  const value = Number.isNaN(Number(element.value)) ? 0 :Number(element.value);
    arr.push(value)
    printInfo()
}

function printInfo() {
    console.log(`The array has ${arr.length} elements.`);
    const full = arr.reduce((acc, item) => (acc += item), 0)
    const average = full/arr.length

    console.log(`The average of the values is ${average}`)
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><html>
<body>
<button id='main' onclick="addNumber()">
Click me
</button>
<input id="num" type="number"/>
</body>
</html></code>```

I am adding also a minimal working example in fiddle https://jsfiddle.net/5ybz6Lmk/

Title: JavaScript arrays with functions

Body:
Planned output:

The array has 10 elements.
The average of the values is 36.1

I plan two functions: addNumber() and printInfo().

Function: addNumber() reads a value from a text input field on the HTML document (id="num") and adds it at the end of the array.

Another function: printInfo() outputs the amount of elements in the array to the console, then the average of their values.

HTML document has two buttons for calling the functions.

I wrote:

let arr = [];
arr.push (function addNumber() {
let element = document.getElementById("num");
})
function printInfo() {
console.log (arr.length );
}

How to use buttons for calling the functions in HTML?

How to count average of values in the array?

Body:
You have a couple of errors in your code.
First create the function outside of arr.push().
Second, you should call the function from somewhere for it to work. Also, next time provide the HTML too. You can see a working example here:

let arr = [];

function addNumber() {
    const element = document.getElementById("num");
  const value = Number.isNaN(Number(element.value)) ? 0 :Number(element.value);
    arr.push(value)
    printInfo()
}

function printInfo() {
    console.log(`The array has ${arr.length} elements.`);
    const full = arr.reduce((acc, item) => (acc += item), 0)
    const average = full/arr.length

    console.log(`The average of the values is ${average}`)
}

<html>
<body>
<button id='main' onclick="addNumber()">
Click me
</button>
<input id="num" type="number"/>
</body>
</html>

I am adding also a minimal working example in fiddle https://jsfiddle.net/5ybz6Lmk/


问:
Existing implementation of Btree or B+tree in Java
I am doing a project in which I require btree or b+tree data structure. Does anyone know of an existing implementation of btree or b+tree (with insert, delete, search algorithms)? It should accept string as input and form btree or b+tree of these string.</p>

答: You could try Electric's BTree (author page here).</p>

ContentLicense: CC BY-SA 2.5


问:
Unable to find an exact match for CDP version 98, so returning the closest version found: 97
this is the screenshot of my first test case in selenium

pls help me with this what error its there as I am new to selenium

答:
Try using something like this:

```<code>WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));

// click on the compose button as soon as the "compose" button is visible
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
}
</code>```

Source: https://www.browserstack.com/guide/wait-commands-in-selenium-webdriver

Your error is caused by trying to Click on something that the webdriver has not found, giving it time to load the webpage will help.

Body:
Try using something like this:

WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));

// click on the compose button as soon as the "compose" button is visible
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
}

Source: https://www.browserstack.com/guide/wait-commands-in-selenium-webdriver

Your error is caused by trying to Click on something that the webdriver has not found, giving it time to load the webpage will help.