问:
input_shape with image_generator in Tensorflow
I'm trying to use this approach in Tensorflow 2.X to load large dataset that does not fit in memory.

I have a folder with X sub-folders that contains images. Each sub-folder is a class.

```<code>\dataset
    -\class1
        -img1_1.jpg
        -img1_2.jpg
        -...
    -\classe2
        -img2_1.jpg
        -img2_2.jpg
        -...
</code>```

I create my data generator from my folder like this:

```<code>train_data_gen = image_generator.flow_from_directory(directory="path\\to\\dataset",
                                                     batch_size=100,
                                                     shuffle=True,
                                                     target_size=(100, 100), # Image H x W
                                                     classes=list(CLASS_NAMES)) # list of folder/class names ["class1", "class2", ...., "classX"]
</code>```

Found 629 images belonging to 2 classes.

I've did a smaller dataset to test the pipeline. Only 629 images in 2 classes.
Now I can create a dummy model like this:

```<code>model = tf.keras.Sequential()
model.add(Dense(1, activation=activation, input_shape=(100, 100, 3))) # only 1 layer of 1 neuron
model.add(Dense(2)) # 2classes
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=['categorical_accuracy'])
</code>```

Once compile I try to fit this dummy model:

```<code>STEPS_PER_EPOCH = np.ceil(image_count / batch_size) # 629 / 100
model.fit_generator(generator=train_data_gen , steps_per_epoch=STEPS_PER_EPOCH, epochs=2, verbose=1)

1/7 [===>..........................] - ETA: 2s - loss: 1.1921e-07 - categorical_accuracy: 0.9948
2/7 [=======>......................] - ETA: 1s - loss: 1.1921e-07 - categorical_accuracy: 0.5124
3/7 [===========>..................] - ETA: 0s - loss: 1.1921e-07 - categorical_accuracy: 0.3449
4/7 [================>.............] - ETA: 0s - loss: 1.1921e-07 - categorical_accuracy: 0.2662
5/7 [====================>.........] - ETA: 0s - loss: 1.1921e-07 - categorical_accuracy: 0.2130
6/7 [========================>.....] - ETA: 0s - loss: 1.1921e-07 - categorical_accuracy: 0.1808
</code>```

2020-04-14 20:39:48.629203: W tensorflow/core/framework/op_kernel.cc:1610] Invalid argument: ValueError: generator yielded an element of shape (29, 100, 100, 3) where an element of shape (100, 100, 100, 3) was expected.

From what i understand, the last batch doesn't has the same shape has the previous batches. So it crashes. I've tried to specify a batch_input_shape.

```<code>model.add(Dense(1, activation=activation, batch_input_shape=(None, 100, 100, 3)))
</code>```

I've found here that I should put None to not specify the number of elements in the batch so it can be dynamic. But no success.

Edit: From the comment I had 2 mistakes:

The output shape was bad. I missed the flatten layer in the model.

The previous link does work with the correction of the flatten layer

Missing some code, I actually feed the fit_generator with a tf.data.Dataset.from_generator but I gave here a image_generator.flow_from_directory.

Here is the final code:

```<code>train_data_gen = image_generator.flow_from_directory(directory="path\\to\\dataset",
    batch_size=1000,
    shuffle=True,
    target_size=(100, 100),
    classes=list(CLASS_NAMES))

train_dataset = tf.data.Dataset.from_generator(
    lambda: train_data_gen,
    output_types=(tf.float32, tf.float32),
    output_shapes=([None, x, y, 3],
    [None, len(CLASS_NAMES)]))

model = tf.keras.Sequential()
model.add(Flatten(batch_input_shape=(None, 100, 100, 3)))
model.add(Dense(1, activation=activation))
model.add(Dense(2))
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=['categorical_accuracy'])

STEPS_PER_EPOCH = np.ceil(image_count / batch_size) # 629 / 100
model.fit_generator(generator=train_data_gen , steps_per_epoch=STEPS_PER_EPOCH, epochs=2, verbose=1)
</code>```

答:
For the benefit of community here i am explaining, how to use image_generator in Tensorflow with input_shape (100, 100, 3) using dogs vs cats dataset

If we haven't choose right batch size there is a chance of model struck right after first epoch, hence i am starting my explanation with how to choose batch_size ?

We generally observe that batch size to be the power of 2, this is because of the effective work of optimized matrix operation libraries. This is further elaborated in this research paper.

Check out this blog which describes how to choose the right batch size while comparing the effects of different batch sizes on the accuracy of CIFAR-10 dataset.

Here is the end to end working code with outputs

```<code>import os
import numpy as np
from keras import layers
import pandas as pd
from tensorflow.keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from tensorflow.keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from tensorflow.keras.models import Sequential
from tensorflow.keras import regularizers, optimizers
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator

import keras.backend as K

K.set_image_data_format('channels_last')

train_dir  = '/content/drive/My Drive/Dogs_Vs_Cats/train'
test_dir = '/content/drive/My Drive/Dogs_Vs_Cats/test'

img_width, img_height = 100, 100
input_shape = img_width, img_height, 3

train_samples = 2000
test_samples = 1000
epochs = 30
batch_size = 32

train_datagen = ImageDataGenerator(
    rescale = 1. /255,
    shear_range = 0.2,
    zoom_range = 0.2,
    horizontal_flip = True)

test_datagen = ImageDataGenerator(
    rescale = 1. /255)

train_data = train_datagen.flow_from_directory(
    train_dir,
    target_size = (img_width, img_height),
    batch_size = batch_size,
    class_mode = 'binary')

test_data = test_datagen.flow_from_directory(
    test_dir,
    target_size = (img_width, img_height),
    batch_size = batch_size,
    class_mode = 'binary')

model = Sequential()

model.add(Conv2D(32, (7, 7), strides = (1, 1), input_shape = input_shape))
model.add(BatchNormalization(axis = 3))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2)))

model.add(Conv2D(64, (7, 7), strides = (1, 1)))
model.add(BatchNormalization(axis = 3))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2)))

model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.summary()

model.compile(loss = 'binary_crossentropy',
            optimizer = 'rmsprop',
            metrics = ['accuracy'])

model.fit_generator(
        train_data,
        steps_per_epoch = train_samples//batch_size,
        epochs = epochs,
        validation_data = test_data,
        verbose = 1,
        validation_steps = test_samples//batch_size)
</code>```

Output:

```<code>Found 2000 images belonging to 2 classes.
Found 1000 images belonging to 2 classes.
Model: "sequential_5"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv2d_8 (Conv2D)            (None, 94, 94, 32)        4736
_________________________________________________________________
batch_normalization_8 (Batch (None, 94, 94, 32)        128
_________________________________________________________________
activation_8 (Activation)    (None, 94, 94, 32)        0
_________________________________________________________________
max_pooling2d_8 (MaxPooling2 (None, 47, 47, 32)        0
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 41, 41, 64)        100416
_________________________________________________________________
batch_normalization_9 (Batch (None, 41, 41, 64)        256
_________________________________________________________________
activation_9 (Activation)    (None, 41, 41, 64)        0
_________________________________________________________________
max_pooling2d_9 (MaxPooling2 (None, 20, 20, 64)        0
_________________________________________________________________
flatten_4 (Flatten)          (None, 25600)             0
_________________________________________________________________
dense_11 (Dense)             (None, 64)                1638464
_________________________________________________________________
dropout_4 (Dropout)          (None, 64)                0
_________________________________________________________________
dense_12 (Dense)             (None, 1)                 65
=================================================================
Total params: 1,744,065
Trainable params: 1,743,873
Non-trainable params: 192
_________________________________________________________________
Epoch 1/30
62/62 [==============================] - 14s 225ms/step - loss: 1.8307 - accuracy: 0.4853 - val_loss: 0.6931 - val_accuracy: 0.5000
Epoch 2/30
62/62 [==============================] - 14s 226ms/step - loss: 0.7085 - accuracy: 0.4832 - val_loss: 0.6931 - val_accuracy: 0.5010
Epoch 3/30
62/62 [==============================] - 14s 218ms/step - loss: 0.6955 - accuracy: 0.5300 - val_loss: 0.6894 - val_accuracy: 0.5292
Epoch 4/30
62/62 [==============================] - 14s 221ms/step - loss: 0.6938 - accuracy: 0.5407 - val_loss: 0.7309 - val_accuracy: 0.5262
Epoch 5/30
62/62 [==============================] - 14s 218ms/step - loss: 0.6860 - accuracy: 0.5498 - val_loss: 0.6776 - val_accuracy: 0.5665
Epoch 6/30
62/62 [==============================] - 13s 216ms/step - loss: 0.7027 - accuracy: 0.5407 - val_loss: 0.6895 - val_accuracy: 0.5101
Epoch 7/30
62/62 [==============================] - 13s 216ms/step - loss: 0.6852 - accuracy: 0.5528 - val_loss: 0.6567 - val_accuracy: 0.5887
Epoch 8/30
62/62 [==============================] - 13s 217ms/step - loss: 0.6772 - accuracy: 0.5427 - val_loss: 0.6643 - val_accuracy: 0.5847
Epoch 9/30
62/62 [==============================] - 13s 217ms/step - loss: 0.6709 - accuracy: 0.5534 - val_loss: 0.6623 - val_accuracy: 0.5887
Epoch 10/30
62/62 [==============================] - 14s 219ms/step - loss: 0.6579 - accuracy: 0.5711 - val_loss: 0.6614 - val_accuracy: 0.6058
Epoch 11/30
62/62 [==============================] - 13s 218ms/step - loss: 0.6591 - accuracy: 0.5625 - val_loss: 0.6594 - val_accuracy: 0.5454
Epoch 12/30
62/62 [==============================] - 13s 216ms/step - loss: 0.6419 - accuracy: 0.5767 - val_loss: 1.1041 - val_accuracy: 0.5161
Epoch 13/30
62/62 [==============================] - 13s 215ms/step - loss: 0.6479 - accuracy: 0.5783 - val_loss: 0.6441 - val_accuracy: 0.5837
Epoch 14/30
62/62 [==============================] - 13s 216ms/step - loss: 0.6373 - accuracy: 0.5899 - val_loss: 0.6427 - val_accuracy: 0.6310
Epoch 15/30
62/62 [==============================] - 13s 215ms/step - loss: 0.6203 - accuracy: 0.6133 - val_loss: 0.7390 - val_accuracy: 0.6220
Epoch 16/30
62/62 [==============================] - 13s 217ms/step - loss: 0.6277 - accuracy: 0.6362 - val_loss: 0.6649 - val_accuracy: 0.5786
Epoch 17/30
62/62 [==============================] - 13s 215ms/step - loss: 0.6155 - accuracy: 0.6316 - val_loss: 0.9823 - val_accuracy: 0.5484
Epoch 18/30
62/62 [==============================] - 14s 222ms/step - loss: 0.6056 - accuracy: 0.6408 - val_loss: 0.6333 - val_accuracy: 0.6048
Epoch 19/30
62/62 [==============================] - 14s 218ms/step - loss: 0.6025 - accuracy: 0.6529 - val_loss: 0.6514 - val_accuracy: 0.6442
Epoch 20/30
62/62 [==============================] - 13s 215ms/step - loss: 0.6149 - accuracy: 0.6423 - val_loss: 0.6373 - val_accuracy: 0.6048
Epoch 21/30
62/62 [==============================] - 13s 215ms/step - loss: 0.6030 - accuracy: 0.6519 - val_loss: 0.6086 - val_accuracy: 0.6573
Epoch 22/30
62/62 [==============================] - 13s 217ms/step - loss: 0.5936 - accuracy: 0.6865 - val_loss: 1.0677 - val_accuracy: 0.5605
Epoch 23/30
62/62 [==============================] - 13s 214ms/step - loss: 0.5964 - accuracy: 0.6728 - val_loss: 0.7927 - val_accuracy: 0.5877
Epoch 24/30
62/62 [==============================] - 13s 215ms/step - loss: 0.5866 - accuracy: 0.6707 - val_loss: 0.6116 - val_accuracy: 0.6421
Epoch 25/30
62/62 [==============================] - 13s 214ms/step - loss: 0.5933 - accuracy: 0.6662 - val_loss: 0.8282 - val_accuracy: 0.6048
Epoch 26/30
62/62 [==============================] - 13s 214ms/step - loss: 0.5705 - accuracy: 0.6885 - val_loss: 0.5806 - val_accuracy: 0.6966
Epoch 27/30
62/62 [==============================] - 14s 218ms/step - loss: 0.5709 - accuracy: 0.7017 - val_loss: 1.2404 - val_accuracy: 0.5333
Epoch 28/30
62/62 [==============================] - 13s 216ms/step - loss: 0.5691 - accuracy: 0.7104 - val_loss: 0.6136 - val_accuracy: 0.6442
Epoch 29/30
62/62 [==============================] - 13s 215ms/step - loss: 0.5627 - accuracy: 0.7048 - val_loss: 0.6936 - val_accuracy: 0.6613
Epoch 30/30
62/62 [==============================] - 13s 214ms/step - loss: 0.5714 - accuracy: 0.6941 - val_loss: 0.5872 - val_accuracy: 0.6825
</code>```

Body:
I'm trying to use this approach in Tensorflow 2.X to load large dataset that does not fit in memory.

I have a folder with X sub-folders that contains images. Each sub-folder is a class.

\dataset
    -\class1
        -img1_1.jpg
        -img1_2.jpg
        -...
    -\classe2
        -img2_1.jpg
        -img2_2.jpg
        -...

I create my data generator from my folder like this:

train_data_gen = image_generator.flow_from_directory(directory="path\\to\\dataset",
                                                     batch_size=100,
                                                     shuffle=True,
                                                     target_size=(100, 100), # Image H x W
                                                     classes=list(CLASS_NAMES)) # list of folder/class names ["class1", "class2", ...., "classX"]

Found 629 images belonging to 2 classes.

I've did a smaller dataset to test the pipeline. Only 629 images in 2 classes.
Now I can create a dummy model like this:

model = tf.keras.Sequential()
model.add(Dense(1, activation=activation, input_shape=(100, 100, 3))) # only 1 layer of 1 neuron
model.add(Dense(2)) # 2classes
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=['categorical_accuracy'])

Once compile I try to fit this dummy model:

STEPS_PER_EPOCH = np.ceil(image_count / batch_size) # 629 / 100
model.fit_generator(generator=train_data_gen , steps_per_epoch=STEPS_PER_EPOCH, epochs=2, verbose=1)

1/7 [===>..........................] - ETA: 2s - loss: 1.1921e-07 - categorical_accuracy: 0.9948
2/7 [=======>......................] - ETA: 1s - loss: 1.1921e-07 - categorical_accuracy: 0.5124
3/7 [===========>..................] - ETA: 0s - loss: 1.1921e-07 - categorical_accuracy: 0.3449
4/7 [================>.............] - ETA: 0s - loss: 1.1921e-07 - categorical_accuracy: 0.2662
5/7 [====================>.........] - ETA: 0s - loss: 1.1921e-07 - categorical_accuracy: 0.2130
6/7 [========================>.....] - ETA: 0s - loss: 1.1921e-07 - categorical_accuracy: 0.1808

2020-04-14 20:39:48.629203: W tensorflow/core/framework/op_kernel.cc:1610] Invalid argument: ValueError: generator yielded an element of shape (29, 100, 100, 3) where an element of shape (100, 100, 100, 3) was expected.

From what i understand, the last batch doesn't has the same shape has the previous batches. So it crashes. I've tried to specify a batch_input_shape.

model.add(Dense(1, activation=activation, batch_input_shape=(None, 100, 100, 3)))

I've found here that I should put None to not specify the number of elements in the batch so it can be dynamic. But no success.

Edit: From the comment I had 2 mistakes:

The output shape was bad. I missed the flatten layer in the model.

The previous link does work with the correction of the flatten layer

Missing some code, I actually feed the fit_generator with a tf.data.Dataset.from_generator but I gave here a image_generator.flow_from_directory.

Here is the final code:

train_data_gen = image_generator.flow_from_directory(directory="path\\to\\dataset",
    batch_size=1000,
    shuffle=True,
    target_size=(100, 100),
    classes=list(CLASS_NAMES))

train_dataset = tf.data.Dataset.from_generator(
    lambda: train_data_gen,
    output_types=(tf.float32, tf.float32),
    output_shapes=([None, x, y, 3],
    [None, len(CLASS_NAMES)]))

model = tf.keras.Sequential()
model.add(Flatten(batch_input_shape=(None, 100, 100, 3)))
model.add(Dense(1, activation=activation))
model.add(Dense(2))
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=['categorical_accuracy'])

STEPS_PER_EPOCH = np.ceil(image_count / batch_size) # 629 / 100
model.fit_generator(generator=train_data_gen , steps_per_epoch=STEPS_PER_EPOCH, epochs=2, verbose=1)

Body:
For the benefit of community here i am explaining, how to use image_generator in Tensorflow with input_shape (100, 100, 3) using dogs vs cats dataset

If we haven't choose right batch size there is a chance of model struck right after first epoch, hence i am starting my explanation with how to choose batch_size ?

We generally observe that batch size to be the power of 2, this is because of the effective work of optimized matrix operation libraries. This is further elaborated in this research paper.

Check out this blog which describes how to choose the right batch size while comparing the effects of different batch sizes on the accuracy of CIFAR-10 dataset.

Here is the end to end working code with outputs

import os
import numpy as np
from keras import layers
import pandas as pd
from tensorflow.keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D
from tensorflow.keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D, GlobalAveragePooling2D
from tensorflow.keras.models import Sequential
from tensorflow.keras import regularizers, optimizers
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator

import keras.backend as K

K.set_image_data_format('channels_last')

train_dir  = '/content/drive/My Drive/Dogs_Vs_Cats/train'
test_dir = '/content/drive/My Drive/Dogs_Vs_Cats/test'

img_width, img_height = 100, 100
input_shape = img_width, img_height, 3

train_samples = 2000
test_samples = 1000
epochs = 30
batch_size = 32

train_datagen = ImageDataGenerator(
    rescale = 1. /255,
    shear_range = 0.2,
    zoom_range = 0.2,
    horizontal_flip = True)

test_datagen = ImageDataGenerator(
    rescale = 1. /255)

train_data = train_datagen.flow_from_directory(
    train_dir,
    target_size = (img_width, img_height),
    batch_size = batch_size,
    class_mode = 'binary')

test_data = test_datagen.flow_from_directory(
    test_dir,
    target_size = (img_width, img_height),
    batch_size = batch_size,
    class_mode = 'binary')

model = Sequential()

model.add(Conv2D(32, (7, 7), strides = (1, 1), input_shape = input_shape))
model.add(BatchNormalization(axis = 3))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2)))

model.add(Conv2D(64, (7, 7), strides = (1, 1)))
model.add(BatchNormalization(axis = 3))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2)))

model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.summary()

model.compile(loss = 'binary_crossentropy',
            optimizer = 'rmsprop',
            metrics = ['accuracy'])

model.fit_generator(
        train_data,
        steps_per_epoch = train_samples//batch_size,
        epochs = epochs,
        validation_data = test_data,
        verbose = 1,
        validation_steps = test_samples//batch_size)

Output:

Found 2000 images belonging to 2 classes.
Found 1000 images belonging to 2 classes.
Model: "sequential_5"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv2d_8 (Conv2D)            (None, 94, 94, 32)        4736
_________________________________________________________________
batch_normalization_8 (Batch (None, 94, 94, 32)        128
_________________________________________________________________
activation_8 (Activation)    (None, 94, 94, 32)        0
_________________________________________________________________
max_pooling2d_8 (MaxPooling2 (None, 47, 47, 32)        0
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 41, 41, 64)        100416
_________________________________________________________________
batch_normalization_9 (Batch (None, 41, 41, 64)        256
_________________________________________________________________
activation_9 (Activation)    (None, 41, 41, 64)        0
_________________________________________________________________
max_pooling2d_9 (MaxPooling2 (None, 20, 20, 64)        0
_________________________________________________________________
flatten_4 (Flatten)          (None, 25600)             0
_________________________________________________________________
dense_11 (Dense)             (None, 64)                1638464
_________________________________________________________________
dropout_4 (Dropout)          (None, 64)                0
_________________________________________________________________
dense_12 (Dense)             (None, 1)                 65
=================================================================
Total params: 1,744,065
Trainable params: 1,743,873
Non-trainable params: 192
_________________________________________________________________
Epoch 1/30
62/62 [==============================] - 14s 225ms/step - loss: 1.8307 - accuracy: 0.4853 - val_loss: 0.6931 - val_accuracy: 0.5000
Epoch 2/30
62/62 [==============================] - 14s 226ms/step - loss: 0.7085 - accuracy: 0.4832 - val_loss: 0.6931 - val_accuracy: 0.5010
Epoch 3/30
62/62 [==============================] - 14s 218ms/step - loss: 0.6955 - accuracy: 0.5300 - val_loss: 0.6894 - val_accuracy: 0.5292
Epoch 4/30
62/62 [==============================] - 14s 221ms/step - loss: 0.6938 - accuracy: 0.5407 - val_loss: 0.7309 - val_accuracy: 0.5262
Epoch 5/30
62/62 [==============================] - 14s 218ms/step - loss: 0.6860 - accuracy: 0.5498 - val_loss: 0.6776 - val_accuracy: 0.5665
Epoch 6/30
62/62 [==============================] - 13s 216ms/step - loss: 0.7027 - accuracy: 0.5407 - val_loss: 0.6895 - val_accuracy: 0.5101
Epoch 7/30
62/62 [==============================] - 13s 216ms/step - loss: 0.6852 - accuracy: 0.5528 - val_loss: 0.6567 - val_accuracy: 0.5887
Epoch 8/30
62/62 [==============================] - 13s 217ms/step - loss: 0.6772 - accuracy: 0.5427 - val_loss: 0.6643 - val_accuracy: 0.5847
Epoch 9/30
62/62 [==============================] - 13s 217ms/step - loss: 0.6709 - accuracy: 0.5534 - val_loss: 0.6623 - val_accuracy: 0.5887
Epoch 10/30
62/62 [==============================] - 14s 219ms/step - loss: 0.6579 - accuracy: 0.5711 - val_loss: 0.6614 - val_accuracy: 0.6058
Epoch 11/30
62/62 [==============================] - 13s 218ms/step - loss: 0.6591 - accuracy: 0.5625 - val_loss: 0.6594 - val_accuracy: 0.5454
Epoch 12/30
62/62 [==============================] - 13s 216ms/step - loss: 0.6419 - accuracy: 0.5767 - val_loss: 1.1041 - val_accuracy: 0.5161
Epoch 13/30
62/62 [==============================] - 13s 215ms/step - loss: 0.6479 - accuracy: 0.5783 - val_loss: 0.6441 - val_accuracy: 0.5837
Epoch 14/30
62/62 [==============================] - 13s 216ms/step - loss: 0.6373 - accuracy: 0.5899 - val_loss: 0.6427 - val_accuracy: 0.6310
Epoch 15/30
62/62 [==============================] - 13s 215ms/step - loss: 0.6203 - accuracy: 0.6133 - val_loss: 0.7390 - val_accuracy: 0.6220
Epoch 16/30
62/62 [==============================] - 13s 217ms/step - loss: 0.6277 - accuracy: 0.6362 - val_loss: 0.6649 - val_accuracy: 0.5786
Epoch 17/30
62/62 [==============================] - 13s 215ms/step - loss: 0.6155 - accuracy: 0.6316 - val_loss: 0.9823 - val_accuracy: 0.5484
Epoch 18/30
62/62 [==============================] - 14s 222ms/step - loss: 0.6056 - accuracy: 0.6408 - val_loss: 0.6333 - val_accuracy: 0.6048
Epoch 19/30
62/62 [==============================] - 14s 218ms/step - loss: 0.6025 - accuracy: 0.6529 - val_loss: 0.6514 - val_accuracy: 0.6442
Epoch 20/30
62/62 [==============================] - 13s 215ms/step - loss: 0.6149 - accuracy: 0.6423 - val_loss: 0.6373 - val_accuracy: 0.6048
Epoch 21/30
62/62 [==============================] - 13s 215ms/step - loss: 0.6030 - accuracy: 0.6519 - val_loss: 0.6086 - val_accuracy: 0.6573
Epoch 22/30
62/62 [==============================] - 13s 217ms/step - loss: 0.5936 - accuracy: 0.6865 - val_loss: 1.0677 - val_accuracy: 0.5605
Epoch 23/30
62/62 [==============================] - 13s 214ms/step - loss: 0.5964 - accuracy: 0.6728 - val_loss: 0.7927 - val_accuracy: 0.5877
Epoch 24/30
62/62 [==============================] - 13s 215ms/step - loss: 0.5866 - accuracy: 0.6707 - val_loss: 0.6116 - val_accuracy: 0.6421
Epoch 25/30
62/62 [==============================] - 13s 214ms/step - loss: 0.5933 - accuracy: 0.6662 - val_loss: 0.8282 - val_accuracy: 0.6048
Epoch 26/30
62/62 [==============================] - 13s 214ms/step - loss: 0.5705 - accuracy: 0.6885 - val_loss: 0.5806 - val_accuracy: 0.6966
Epoch 27/30
62/62 [==============================] - 14s 218ms/step - loss: 0.5709 - accuracy: 0.7017 - val_loss: 1.2404 - val_accuracy: 0.5333
Epoch 28/30
62/62 [==============================] - 13s 216ms/step - loss: 0.5691 - accuracy: 0.7104 - val_loss: 0.6136 - val_accuracy: 0.6442
Epoch 29/30
62/62 [==============================] - 13s 215ms/step - loss: 0.5627 - accuracy: 0.7048 - val_loss: 0.6936 - val_accuracy: 0.6613
Epoch 30/30
62/62 [==============================] - 13s 214ms/step - loss: 0.5714 - accuracy: 0.6941 - val_loss: 0.5872 - val_accuracy: 0.6825


来源: stackexchange

问:
gnuplot: Increase gap between ylabel and ytics
In gnuplotting, when I increase the font size of ylabel and ytics, they start to overlap. I tried to create margin between them, but couldn't find a solution. Would really appreciate the help in this regard.

```<code>      set   autoscale
      unset log
      unset label
      set xtic auto
      set ytic auto
      set tics font "Helvetica,18"
      set key font "Helvetica,18"
      set xlabel font "Helvetica,18"
      set ylabel font "Helvetica,18"

      set xlabel "Iteration Day"
      set ylabel "Number of Persons"

        set key inside
        set key right
        set key top

      plot  "SIR_I11_L2_T036.txt" using 1:2 title 'CurInf' with lines lt "5" lw "3", \
"SIR_I11_L2_T036.txt" using 1:3 title 'totInf' with lines lt "10" lw "3", \
"SIR_I11_L2_T036.txt" using 1:4 title 'Susceptible' with lines lt "15" lw "3", \
"SIR_I11_L2_T036.txt" using 1:5 title 'Exposed' with lines lt "20" lw "3", \
"SIR_I11_L2_T036.txt" using 1:7 title 'Recovered' with lines lt "25" lw "3"
</code>```

答:
The argument is called offset. See help xtics and help xlabels

```<code>set xtics off 0,-2
set ytics off -4,0

set xlabel "Iteration Day" off 0,-4
set ylabel "Number of Persons" off 5,0
set bmargin 10

pl erf(0.3*x) t "recovered"
</code>```

ContentLicense: CC BY-SA 4.0

Body:
In gnuplotting, when I increase the font size of ylabel and ytics, they start to overlap. I tried to create margin between them, but couldn't find a solution. Would really appreciate the help in this regard.

      set   autoscale
      unset log
      unset label
      set xtic auto
      set ytic auto
      set tics font "Helvetica,18"
      set key font "Helvetica,18"
      set xlabel font "Helvetica,18"
      set ylabel font "Helvetica,18"

      set xlabel "Iteration Day"
      set ylabel "Number of Persons"

        set key inside
        set key right
        set key top

      plot  "SIR_I11_L2_T036.txt" using 1:2 title 'CurInf' with lines lt "5" lw "3", \
"SIR_I11_L2_T036.txt" using 1:3 title 'totInf' with lines lt "10" lw "3", \
"SIR_I11_L2_T036.txt" using 1:4 title 'Susceptible' with lines lt "15" lw "3", \
"SIR_I11_L2_T036.txt" using 1:5 title 'Exposed' with lines lt "20" lw "3", \
"SIR_I11_L2_T036.txt" using 1:7 title 'Recovered' with lines lt "25" lw "3"

Body:
The argument is called offset. See help xtics and help xlabels

set xtics off 0,-2
set ytics off -4,0

set xlabel "Iteration Day" off 0,-4
set ylabel "Number of Persons" off 5,0
set bmargin 10

pl erf(0.3*x) t "recovered"


问:
Redirect with PHP and skip specific parameter
Since I've started using friendly URLS in my website, I'm redirecting every page to the new version, only if the registered user has a "username" in his profile.

So, I'm redirecting from:

https://tribbr.me/post.php?id=850

with:

header("Location:/".$if_username."/post/".$post_id."?".$_SERVER['QUERY_STRING']); exit();

To keep all GET parameters.... but the problem is that this header request, obviously with $_SERVER['QUERY_STRING'] is adding the post id too to the URL, and when redirected, this is the final URL:

https://tribbr.me/TribeMasters/post/850?id=850

Is it possible to just skip the id=850 parameter to the URL redirection? Since it is a duplicated parameter: post/850 and id=850 are the same.

Thanks for helping me :)

答:
@DE_'s answer is best. But If you are not familiar with Regex, This is an alternative way.

```<code>function removeGetParam($param){

 $params = $_GET;

 // removing the key
 unset($params[$param]);

 // joining and returning the rest
 return implode(',', array_map(function ($value, $key) {
            return $key.'='.$value;
          },$params, array_keys($params))
        );
}

$filtered_params = removeGetParam('id');
header("Location:/".$if_username."/post/".$post_id."?".$filtered_params);
</code>```

Body:
@DE_'s answer is best. But If you are not familiar with Regex, This is an alternative way.

function removeGetParam($param){

 $params = $_GET;

 // removing the key
 unset($params[$param]);

 // joining and returning the rest
 return implode(',', array_map(function ($value, $key) {
            return $key.'='.$value;
          },$params, array_keys($params))
        );
}

$filtered_params = removeGetParam('id');
header("Location:/".$if_username."/post/".$post_id."?".$filtered_params);


问:
add blue tiles to matplotlib plot (background)
So when I want to plot this

```<code>plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('number of components')
plt.ylabel('cumulative explained variance');
</code>```

The result looks like:

But the above plot is not beautiful. What I want it to look like is similar to:

I did some search before asking here to no avail. Would appreciate if I given a hint

答: Quick, dirty fix is import seaborn as sns; sns.set() before your plot command. And then call seaborn.reset_orig() when you want the original pyplot style.

Body:
So when I want to plot this

plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('number of components')
plt.ylabel('cumulative explained variance');

The result looks like:

But the above plot is not beautiful. What I want it to look like is similar to:

I did some search before asking here to no avail. Would appreciate if I given a hint


问:
Merge if properties are equal and remove
Below is my code (can be copy paste in https://www.compilejava.net/ with -ea as command line option).

I have an Object called Main. I have Main inside a List. If 2 properties (a and b) are equal to another Main object in the list, property strings should be concatenated. Furthermore, duplicates (when the 2 properties are equal) should than be removed (so the list can not contain 2 or more Mains in which both a and b are the same).

I tried it with a HashMap, hashCode, but I can not figure it out well. Note: I use OpenJDK-12 and can not use newer versions.

```<code>import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Main {

    final int a;
    final int b;
    final List<String> strings;

    Main(int a, int b, List<String> strings) {
        this.a = a;
        this.b = b;
        this.strings = strings;
    }

    private static Main generateMain0() {
        return new Main(0, 1, createListWithOneElement("merge me with main1"));
    }

    public static void main(String[] args) {
        Main main0 = generateMain0();
        Main main1 = new Main(0, 1, createListWithOneElement("merge me with main2"));
        Main main2 = new Main(0, 2, createListWithOneElement("leave me alone"));
        Main main3 = new Main(0, 2, createListWithOneElement("leave me alone also"));

        List<Main> mains = new ArrayList<>();

        mains.add(main0);
        mains.add(main1);
        mains.add(main2);
        mains.add(main3);

        // Do magic here to remove duplicate and concat property strings

        // main1 should be removed, since property a and b were equal to main0 property a and b
        assert mains.size() == 3;

        Main main0Copy = generateMain0();

        main0Copy.strings.add("merge me with main2");

        // The first element should be main0. It should also contain
        // the strings of main1 since property a and b were equal
        assert mains.get(0).equals(main0Copy);
        assert mains.get(1).equals(main2);
        assert mains.get(2).equals(main3);
    }

    private static List<String> createListWithOneElement(String value) {
        List<String> l = new ArrayList<>();

        l.add(value);

        return l;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Main main = (Main) o;
        return a == main.a &&
                b == main.b &&
                strings.equals(main.strings);
    }

    @Override
    public int hashCode() {
        return Objects.hash(a, b, strings);
    }
}
</code>```

答:
If, as you said in the comments, you can use a fully custom List, you can try the code below.

Internally, it uses a combination of a List and a Map to find out if a combination of a and b was already added to the "List". If yes, it adds all strings of the given Main to the existing Main. If not, it adds the given Main the the list.

package example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainList {

    private final List mains;
    private final Map<Key, Main> lookup;

    public MainList() {
        this.mains = new ArrayList<>();
        this.lookup = new HashMap<>();
    }

    public Main get(int index) {
        return this.mains.get(index);
    }

    public void add(Main main) {
        final Key key = new Key(main.a, main.b);
        Main existingMain = this.lookup.get(key);

        if (existingMain == null) {
            this.mains.add(main);
            this.lookup.put(key, main);
        } else {
            existingMain.strings.addAll(main.strings);
        }
    }

    public void remove(Main main) {
        final Key key = new Key(main.a, main.b);
        Main existingMain = this.lookup.get(key);

        if (existingMain != null) {
            if (existingMain.equals(main)) {
                this.mains.remove(existingMain);
                this.lookup.remove(key);
            } else {
                existingMain.strings.removeAll(main.strings);
            }
        }
    }

    public void remove(int index) {
        final Main removedMain = this.mains.remove(index);
        final Key key = new Key(removedMain.a, removedMain.b);

        this.lookup.remove(key);
    }

    public int size() {
        return this.mains.size();
    }

    private static class Key {

        private final int a;
        private final int b;

        private Key(int a, int b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public boolean equals(Object object) {
            if (this == object) {
                return true;
            } else if (object == null || getClass() != object.getClass()) {
                return false;
            }
            Key key = (Key) object;
            return this.a == key.a && this.b == key.b;
        }

        @Override
        public int hashCode() {
            return 31 * this.a + 31 * this.b;
        }
    }
}

```

Body:
Below is my code (can be copy paste in https://www.compilejava.net/ with -ea as command line option).

I have an Object called Main. I have Main inside a List. If 2 properties (a and b) are equal to another Main object in the list, property strings should be concatenated. Furthermore, duplicates (when the 2 properties are equal) should than be removed (so the list can not contain 2 or more Mains in which both a and b are the same).

I tried it with a HashMap, hashCode, but I can not figure it out well. Note: I use OpenJDK-12 and can not use newer versions.

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Main {

    final int a;
    final int b;
    final List<String> strings;

    Main(int a, int b, List<String> strings) {
        this.a = a;
        this.b = b;
        this.strings = strings;
    }

    private static Main generateMain0() {
        return new Main(0, 1, createListWithOneElement("merge me with main1"));
    }

    public static void main(String[] args) {
        Main main0 = generateMain0();
        Main main1 = new Main(0, 1, createListWithOneElement("merge me with main2"));
        Main main2 = new Main(0, 2, createListWithOneElement("leave me alone"));
        Main main3 = new Main(0, 2, createListWithOneElement("leave me alone also"));

        List mains = new ArrayList<>();

        mains.add(main0);
        mains.add(main1);
        mains.add(main2);
        mains.add(main3);

        // Do magic here to remove duplicate and concat property strings

        // main1 should be removed, since property a and b were equal to main0 property a and b
        assert mains.size() == 3;

        Main main0Copy = generateMain0();

        main0Copy.strings.add("merge me with main2");

        // The first element should be main0. It should also contain
        // the strings of main1 since property a and b were equal
        assert mains.get(0).equals(main0Copy);
        assert mains.get(1).equals(main2);
        assert mains.get(2).equals(main3);
    }

    private static List<String> createListWithOneElement(String value) {
        List<String> l = new ArrayList<>();

        l.add(value);

        return l;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Main main = (Main) o;
        return a == main.a &&
                b == main.b &&
                strings.equals(main.strings);
    }

    @Override
    public int hashCode() {
        return Objects.hash(a, b, strings);
    }
}

Body:
If, as you said in the comments, you can use a fully custom List, you can try the code below.

Internally, it uses a combination of a List and a Map to find out if a combination of a and b was already added to the "List". If yes, it adds all strings of the given Main to the existing Main. If not, it adds the given Main the the list.

package example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainList {

    private final List<Main> mains;
    private final Map<Key, Main> lookup;

    public MainList() {
        this.mains = new ArrayList<>();
        this.lookup = new HashMap<>();
    }

    public Main get(int index) {
        return this.mains.get(index);
    }

    public void add(Main main) {
        final Key key = new Key(main.a, main.b);
        Main existingMain = this.lookup.get(key);

        if (existingMain == null) {
            this.mains.add(main);
            this.lookup.put(key, main);
        } else {
            existingMain.strings.addAll(main.strings);
        }
    }

    public void remove(Main main) {
        final Key key = new Key(main.a, main.b);
        Main existingMain = this.lookup.get(key);

        if (existingMain != null) {
            if (existingMain.equals(main)) {
                this.mains.remove(existingMain);
                this.lookup.remove(key);
            } else {
                existingMain.strings.removeAll(main.strings);
            }
        }
    }

    public void remove(int index) {
        final Main removedMain = this.mains.remove(index);
        final Key key = new Key(removedMain.a, removedMain.b);

        this.lookup.remove(key);
    }

    public int size() {
        return this.mains.size();
    }

    private static class Key {

        private final int a;
        private final int b;

        private Key(int a, int b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public boolean equals(Object object) {
            if (this == object) {
                return true;
            } else if (object == null || getClass() != object.getClass()) {
                return false;
            }
            Key key = (Key) object;
            return this.a == key.a && this.b == key.b;
        }

        @Override
        public int hashCode() {
            return 31 * this.a + 31 * this.b;
        }
    }
}


问:
How to Set Back Ground Color of text occupied area alone in QLabel?
How to set a background colour of text occupied area Only, in QLabel ?

```<code>import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class myList(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Frame Example")
        self.myui()

    def myui(self):

        self.textbox = QLineEdit()
        self.label_head = QLabel("Company Name")
        self.textbox.setFixedSize(400,30)
        self.label_head.setFont(QFont("caliber",12,QFont.Bold))
        self.label_head.setAlignment(Qt.AlignCenter)
        self.label_head.setStyleSheet("background-color:red;border-radius:5px;padding:2px;")
        self.label_head.adjustSize()
        self.label_head.setAutoFillBackground(True)

        vbox = QVBoxLayout()
        vbox.addWidget(self.label_head)
        vbox.addWidget(self.textbox)
        self.setLayout(vbox)

def main():
    myapp = QApplication(sys.argv)
    mywin = myList()
    mywin.show()
    sys.exit(myapp.exec_())

if __name__ == '__main__':
    main()
</code>```

In this code, I need a background color to text area only ( "Company name").

答:
Try it

```<code>self.label_head = QLabel("<span style='color: #fff; background-color: #00f'>Company Name</span>")
</code>```

```<code>import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class myList(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Frame Example")
        self.myui()

    def myui(self):

        self.textbox = QLineEdit()

        self.label_head = QLabel("<span style='color: #fff; background-color: #00f'>Company Name</span>") # <---

        self.textbox.setFixedSize(400,30)
        self.label_head.setFont(QFont("caliber",12,QFont.Bold))
        self.label_head.setAlignment(Qt.AlignCenter)
        self.label_head.setStyleSheet("background-color:red;border-radius:5px;padding:2px;")
        self.label_head.adjustSize()
        self.label_head.setAutoFillBackground(True)

        vbox = QVBoxLayout()
        vbox.addWidget(self.label_head)
        vbox.addWidget(self.textbox)
        self.setLayout(vbox)

def main():
    myapp = QApplication(sys.argv)
    mywin = myList()
    mywin.show()
    sys.exit(myapp.exec_())

if __name__ == '__main__':
    main()
</code>```

Body:
How to set a background colour of text occupied area Only, in QLabel ?

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class myList(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Frame Example")
        self.myui()

    def myui(self):

        self.textbox = QLineEdit()
        self.label_head = QLabel("Company Name")
        self.textbox.setFixedSize(400,30)
        self.label_head.setFont(QFont("caliber",12,QFont.Bold))
        self.label_head.setAlignment(Qt.AlignCenter)
        self.label_head.setStyleSheet("background-color:red;border-radius:5px;padding:2px;")
        self.label_head.adjustSize()
        self.label_head.setAutoFillBackground(True)

        vbox = QVBoxLayout()
        vbox.addWidget(self.label_head)
        vbox.addWidget(self.textbox)
        self.setLayout(vbox)

def main():
    myapp = QApplication(sys.argv)
    mywin = myList()
    mywin.show()
    sys.exit(myapp.exec_())

if __name__ == '__main__':
    main()

In this code, I need a background color to text area only ( "Company name").

Body:
Try it

self.label_head = QLabel("<span style='color: #fff; background-color: #00f'>Company Name</span>")

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class myList(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Frame Example")
        self.myui()

    def myui(self):

        self.textbox = QLineEdit()

        self.label_head = QLabel("<span style='color: #fff; background-color: #00f'>Company Name</span>") # <---

        self.textbox.setFixedSize(400,30)
        self.label_head.setFont(QFont("caliber",12,QFont.Bold))
        self.label_head.setAlignment(Qt.AlignCenter)
        self.label_head.setStyleSheet("background-color:red;border-radius:5px;padding:2px;")
        self.label_head.adjustSize()
        self.label_head.setAutoFillBackground(True)

        vbox = QVBoxLayout()
        vbox.addWidget(self.label_head)
        vbox.addWidget(self.textbox)
        self.setLayout(vbox)

def main():
    myapp = QApplication(sys.argv)
    mywin = myList()
    mywin.show()
    sys.exit(myapp.exec_())

if __name__ == '__main__':
    main()


问:
Angular 8: Problems with QueryParamsHandling
I'm trying to make a table with PrimeNG and after made the import in the module file

```<code>import {TableModule} from 'primeng/table';
</code>```

I have the next error:

```<code>node_modules/primeng/api/megamenuitem.d.ts(30,27): error TS2304: Cannot find name 'QueryParamsHandling'.
node_modules/primeng/api/menuitem.d.ts(1,10): error TS2305: Module '"../../@angular/router/router"' has no exported member 'QueryParamsHandling'.
</code>```

I have searched in google and in the community and I haven't found any solutions

Please. Could anybody help me?
Many thanks in advance
Regards

答:
There is a mismatch between the Angular version and the PrimeNG version. You are using Angular 8, and PrimeNG version 7. Run the following commands

```<code>npm uninstall primeng
npm install primeng@8.0.1
</code>```

Should fix it.

Body:
I'm trying to make a table with PrimeNG and after made the import in the module file

import {TableModule} from 'primeng/table';

I have the next error:

node_modules/primeng/api/megamenuitem.d.ts(30,27): error TS2304: Cannot find name 'QueryParamsHandling'.
node_modules/primeng/api/menuitem.d.ts(1,10): error TS2305: Module '"../../@angular/router/router"' has no exported member 'QueryParamsHandling'.

I have searched in google and in the community and I haven't found any solutions

Please. Could anybody help me?
Many thanks in advance
Regards

Body:
There is a mismatch between the Angular version and the PrimeNG version. You are using Angular 8, and PrimeNG version 7. Run the following commands

npm uninstall primeng
npm install primeng@8.0.1

Should fix it.


问:
Delete Last Row in Group
I have a workbook with information sorted by an UKey; I need to delete the last row in each group.

In this example, I need to remove row 524; ranges between UKeys are not fixed.

I've tried calculating a new column to reference the previous row with no luck, re-sorting the data and removing the first row, playing with index...

Help!

答:
This code should work:

```<code>let
    group = Table.Group(YourLastStep, "UKey", {"temp", Table.RemoveLastN}),
    combine = Table.Combine(group[temp])
in
    combine
</code>```

Note, if there is single row in the group - it will be deleted. If you need keep this single row, then use this code:

```<code>let
    group = Table.Group(YourLastStep, "UKey", {"temp", each if Table.RowCount(_) = 1
                                                       then _ else Table.RemoveLastN(_)}),
    combine = Table.Combine(group[temp])
in
    combine
</code>```

Title: Delete Last Row in Group

Body:
This code should work:

let
    group = Table.Group(YourLastStep, "UKey", {"temp", Table.RemoveLastN}),
    combine = Table.Combine(group[temp])
in
    combine

Note, if there is single row in the group - it will be deleted. If you need keep this single row, then use this code:

let
    group = Table.Group(YourLastStep, "UKey", {"temp", each if Table.RowCount(_) = 1
                                                       then _ else Table.RemoveLastN(_)}),
    combine = Table.Combine(group[temp])
in
    combine


问:
Convert certain columns that are recurring to rows in postgres
I have a table in Postgres that has ~50 million rows. I need to convert certain columns to rows.

I need to unpivot certain columns for individuals that repeat as an individual column and repeat the non-individual variables against the respective ID -

The following is the output I need -

Id appreciate any help on this.

答:
50 million rows is not a big deal in Greenplum but returning that many rows to a client is kind of pointless. I'm guessing you want to create a new table for this new output. You are also going to be creating a table that is 2x larger because you are taking a single row and turning it into 2.    </p>

```<code>create table new_table as
select id, mid_1 as mid, name_1 as name, age_1 as age, location
from your_table
union all
select id, mid_2 as mid, name_2 as name, age_2 as age, location
from your_table
distributed by (id);
</code>```

Body:
50 million rows is not a big deal in Greenplum but returning that many rows to a client is kind of pointless. I'm guessing you want to create a new table for this new output. You are also going to be creating a table that is 2x larger because you are taking a single row and turning it into 2.

create table new_table as
select id, mid_1 as mid, name_1 as name, age_1 as age, location
from your_table
union all
select id, mid_2 as mid, name_2 as name, age_2 as age, location
from your_table
distributed by (id);


问:
Python subprocess.Popen for multiple python scripts
I am trying to understand the Popen method. I currently have three python files in the same directory: test.py, hello.py and bye.py. test.py is the file containing the subprocess.Popen method while hello and bye are simple hello world and goodbye world files i.e. they only contain a single print.

if I do:

```<code>import subprocess
from subprocess import PIPE

tst = subprocess.Popen(["python", "hello.py"], stdout=PIPE, stderr=PIPE)
(out,err) = tst.communicate()
</code>```

Everything seems to work fine, obtaining in the shell the proper "Hello World" print for the hello.py and doing the same for bye.py the shell prints "GoodBye World" as it should.

The issue starts when I want to run both files,

```<code>import subprocess
from subprocess import PIPE

tst = subprocess.Popen(["python", "hello.py", "python", "bye.py"], stdout=PIPE, stderr=PIPE)
(out,err) = tst.communicate()
</code>```

This will only return the print for the first .py file, and then return

```<code>[WinError 2 ] The system cannot find the file specified
</code>```

This will happen if I also remove the second "python". Why is this happening?

答:
This will happen if I also remove the second "python". Why is this happening?

Running

```<code>subprocess.Popen(["python", "hello.py", "python", "bye.py"]
</code>```

is akin to running

```<code>$ python hello.py python bye.py
</code>```

which doesn't really make a whole lot of sense, since that's interpreted as passing the arguments hello.py python bye.py to python.

So that's part 1, to your question "I am trying to understand the Popen method."

Without knowing what you actually want to do with this proof of concept, you have a few options; call multiple Popen() sequentially, or use a semicolon with shell=True, but make sure to consider the security implications of that:

```<code># This will also break on Windows
>>> import subprocess as sp
>>> sp.check_output("python -V ; python -V", shell=True)
b'Python 3.8.2\nPython 3.8.2\n'
</code>```

Body:
I am trying to understand the Popen method. I currently have three python files in the same directory: test.py, hello.py and bye.py. test.py is the file containing the subprocess.Popen method while hello and bye are simple hello world and goodbye world files i.e. they only contain a single print.

if I do:

import subprocess
from subprocess import PIPE

tst = subprocess.Popen(["python", "hello.py"], stdout=PIPE, stderr=PIPE)
(out,err) = tst.communicate()

Everything seems to work fine, obtaining in the shell the proper "Hello World" print for the hello.py and doing the same for bye.py the shell prints "GoodBye World" as it should.

The issue starts when I want to run both files,

import subprocess
from subprocess import PIPE

tst = subprocess.Popen(["python", "hello.py", "python", "bye.py"], stdout=PIPE, stderr=PIPE)
(out,err) = tst.communicate()

This will only return the print for the first .py file, and then return

[WinError 2 ] The system cannot find the file specified

This will happen if I also remove the second "python". Why is this happening?

Body:
This will happen if I also remove the second "python". Why is this happening?

Running

subprocess.Popen(["python", "hello.py", "python", "bye.py"]

is akin to running

$ python hello.py python bye.py

which doesn't really make a whole lot of sense, since that's interpreted as passing the arguments hello.py python bye.py to python.

So that's part 1, to your question "I am trying to understand the Popen method."

Without knowing what you actually want to do with this proof of concept, you have a few options; call multiple Popen() sequentially, or use a semicolon with shell=True, but make sure to consider the security implications of that:

# This will also break on Windows
>>> import subprocess as sp
>>> sp.check_output("python -V ; python -V", shell=True)
b'Python 3.8.2\nPython 3.8.2\n'


问:
ComponentDidUpdate SetState ReactJS Infinite loop
Even though there are many questions with the same subject line, I could not get an answer for my problem.

Problem

      I have a select dropdown. On click of which, I call an Api which fetches some key values. I consider this set of key value input fields as a component. So each and every time onChange of my select drop-down, I have used lifecycle methods to handle API Calls. Also, I record these input key values and send back their state to parent component.

According to ReactJS lifecycle methods:

I use

componentDidMount
To call the API for the first time after initial render. This works.

componentDidUpdate
To call the API for subsequent API calls on select drop-down change. But here is the problem. When I try to update the state of input fields the program falls into an infinite loop and hence there are infinite API calls. I am pretty sure after debugging that the problem is with setState(), But I couldnt find the best way to handle states in componentDidUpdate method.

This link exactly replicates my problem but i want a standardized solution

Hope this is clear.
Thanks for the help in advance!

答:
You can use setState() within componentDidUpdate(). But you have to use the condition for that. Otherwise, it get infinite loop.

As the example,

```<code> componentDidUpdate(){
    if(this.props.id !== this.state.id) {
        this.setState({
            id: this.props.id
        });
    }
 }
</code>```

ContentLicense: CC BY-SA 3.0

Body:
You can use setState() within componentDidUpdate(). But you have to use the condition for that. Otherwise, it get infinite loop.

As the example,

 componentDidUpdate(){
    if(this.props.id !== this.state.id) {
        this.setState({
            id: this.props.id
        });
    }
 }


问:
How do I clone a single branch in Git?
I have a local Git repository in ~/local_repo. It has a few branches:

```<code>$ git branch
* master
  rails
  c
  c++
</code>```

To clone the local repository, I do:

```<code>$ git clone ~/local_repo new_repo
Initialized empty Git repository in /home/username/new_repo/.git/
</code>```

The new_repo master branch points to the local_repo master branch, and I can push / pull.

But I am unable to clone another branch. I want to only pull the branch I want (e.g. rails), so that the new repository has a master branch that pushes to and pulls from local_repo's rails branch, by default. How do I accomplish this, or perhaps something similar with local_repo tracking the master local_repo?

答:
```<code>git clone --branch {branch-name} {repo-URI}
</code>```

Example:

```<code>git clone --branch dev https://github.com/ann/cleaningmachine.git
</code>```

dev: This is the {branch-name}

https://github.com/ann/cleaningmachine.git: This is the {repo-URI}

Title: How do I clone a single branch in Git?

Body:
I have a local Git repository in ~/local_repo. It has a few branches:

$ git branch
* master
  rails
  c
  c++

To clone the local repository, I do:

$ git clone ~/local_repo new_repo
Initialized empty Git repository in /home/username/new_repo/.git/

The new_repo master branch points to the local_repo master branch, and I can push / pull.

But I am unable to clone another branch. I want to only pull the branch I want (e.g. rails), so that the new repository has a master branch that pushes to and pulls from local_repo's rails branch, by default. How do I accomplish this, or perhaps something similar with local_repo tracking the master local_repo?

Body:
git clone --branch {branch-name} {repo-URI}

Example:

git clone --branch dev https://github.com/ann/cleaningmachine.git

dev: This is the {branch-name}

https://github.com/ann/cleaningmachine.git: This is the {repo-URI}


问:
Dynamics CRM 2011 - Processes updating non-form fields?
I'm evaulating Dynamics CRM 2011 currently so I'm still learning how things work.

If a field marked as read-only on a form, is it possible to update it via a process step "update record"? I was trying to have some sort view-only fields that get dynamically updated via a process but in the interface those fields are not accessible to be set?

What if I wrote my own plug-in - would these fields still not be updateable via the assembly?

答:
In order to update a readonly field with a workflow, you have to set it writable until you have the workflow configured. The field will then be accessible and you can add your update action. After that you can set the field to readonly again.

Plugins are not affected by this setting because it is only used for the forms.


问:
Sending HTTP 1.1 GET requests with socket.send, python
I am trying to send a Get request to the google server by,
GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n
via Python3 sockets.send function. The code for sending is given below :

```<code>import socket
buffer = input()     #filled the with 'GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n'
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
target = "www.google.com"
port = 80
client.connect((target,port))
client.send(buffer.encode())
while True:
    recv_len = 1
    response = ""
    while recv_len:
        data        = client.recv(4096).decode(errors="ignore")
        recv_len    = len(data)
        response    += data
        if recv_len < 4096:
            break
    print(response)
</code>```

But I am receiving no data back from the server.
Can somebody help me with this? I tried similar questions, but most are concentrated on the use of \n in the request.

答:
The problem is with the line

```<code>buffer = input()
</code>```

As Serge Ballesta mentioned, input() function reads \r as two characters, "\" and "r". Using

```<code>buffer = sys.stdin.read()
</code>```

and  piping the GET request to the program file will solve the issue.

```<code>echo -ne "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n" |python3 temp.py
</code>```

Body:
I am trying to send a Get request to the google server by,
GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n
via Python3 sockets.send function. The code for sending is given below :

import socket
buffer = input()     #filled the with 'GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n'
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
target = "www.google.com"
port = 80
client.connect((target,port))
client.send(buffer.encode())
while True:
    recv_len = 1
    response = ""
    while recv_len:
        data        = client.recv(4096).decode(errors="ignore")
        recv_len    = len(data)
        response    += data
        if recv_len < 4096:
            break
    print(response)

But I am receiving no data back from the server.
Can somebody help me with this? I tried similar questions, but most are concentrated on the use of \n in the request.

Body:
The problem is with the line

buffer = input()

As Serge Ballesta mentioned, input() function reads \r as two characters, "\" and "r". Using

buffer = sys.stdin.read()

and  piping the GET request to the program file will solve the issue.

echo -ne "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n" |python3 temp.py


问:
Python3 how to combine 2 text files line by line conditionally
I have two ascii tables in text files containing information about stars, one of which with the headers

| ID | TIME | MAGNITUDE | ERROR |

and the other has the headers

| ID | CLASS |

and I want to add the CLASS column to the first text file. The main problem here is that the first text file has got many rows for each star (I.E. Star 3_6588 has got 20 entries in table a for different times) whereas the second text file has only one entry for each ID (as Star 3_6588 is always a Class I).

What I need to do is to add the |CLASS| column to the first table where every instance of a specific ID has the required class. The text file has over 14  million rows in it which is why I can't just do this manually.

答:
@Terry Spotts has the right idea. However the leading and trailing | characters in the header line will make this a slightly tricky CSV, as the delimiter is a pipe char, but sometimes with a leading space, trailing space, or both. But here's an example to generate your ID: Class dictionary:

```<code>> cat bigfile.txt
| ID | TIME | MAGNITUDE | ERROR |
| Star 3_6588 | 10 | 2 | 1.02 |
| Star 3_6588 | 15 | 4 | 1.2 |
| Star 2_999 | 20 | 6 | 1.4 |
| Star 2_999 | 25 | 8 | 1.6 |

> cat smallfile.txt
| ID | CLASS |
| Star 3_6588 | CLASS I |
</code>```

Code:

```<code>id2class = {}
with open('/tmp/smallfile.txt', 'r') as classfile:
    line = classfile.readline()        # Skip Header Line
    for line in classfile:
        line = line.rstrip('\n')[2:-2] # strip newline and the Pipe-Space / Space-Pipe and the start + end
        fields = line.split(' | ')     # Split on ' | '
        id = fields[0]
        starclass = fields[1]
        id2class[id] = starclass
</code>```

Now you have a dict id2class that looks like:

```<code>{
    'Star 3_6588': 'CLASS I',
    'Star 2_999': 'CLASS II'
}
</code>```

You can then parse the first file in a similar way, use the ID of each line to look up the Class in the dict, and write out the full data for the line to a new file. I'll leave that part to you :)

Happy Coding!

Body:
@Terry Spotts has the right idea. However the leading and trailing | characters in the header line will make this a slightly tricky CSV, as the delimiter is a pipe char, but sometimes with a leading space, trailing space, or both. But here's an example to generate your ID: Class dictionary:

> cat bigfile.txt
| ID | TIME | MAGNITUDE | ERROR |
| Star 3_6588 | 10 | 2 | 1.02 |
| Star 3_6588 | 15 | 4 | 1.2 |
| Star 2_999 | 20 | 6 | 1.4 |
| Star 2_999 | 25 | 8 | 1.6 |

> cat smallfile.txt
| ID | CLASS |
| Star 3_6588 | CLASS I |

Code:

id2class = {}
with open('/tmp/smallfile.txt', 'r') as classfile:
    line = classfile.readline()        # Skip Header Line
    for line in classfile:
        line = line.rstrip('\n')[2:-2] # strip newline and the Pipe-Space / Space-Pipe and the start + end
        fields = line.split(' | ')     # Split on ' | '
        id = fields[0]
        starclass = fields[1]
        id2class[id] = starclass

Now you have a dict id2class that looks like:

{
    'Star 3_6588': 'CLASS I',
    'Star 2_999': 'CLASS II'
}

You can then parse the first file in a similar way, use the ID of each line to look up the Class in the dict, and write out the full data for the line to a new file. I'll leave that part to you :)

Happy Coding!


问:
SpringMVC can not bind json correctly
There a json object to be sent to the server, which contains a field：

```<code>{"sName":"something"}
</code>```

In my request model,I declare a var with the same name:

```<code>String sName;
</code>```

But I got null when I receive in the Controller.

I change the field name to lower-case(sname) or add JsonProperty(value="sName") annotation,it work. So where is the problem？

Controller

```<code>public ResponseEntity<JSONObject> getComprehensiveInquiryCp(@Validated @RequestBody ComprehensiveInquiryRequestModel body) {
        Map<String, Object> content;
        JSONObject result = new JSONObject();
        String sLicense = body.getSLicense();
...
}
</code>```

ComprehensiveInquiryRequestModel

```<code>@Data
public class ComprehensiveInquiryRequestModel {
...
    //@JsonProperty(value = "sLicense")
    private String sLicense;
...
}
</code>```

答:
It is important that your setters (and getters) are present and actually conform to the Java naming conventions. A json property named "myFirstName" usually requires a public setter "setMyFirstName(...)" for example. So "sName" needs "setSName()", I guess.
Sure sounds like the naming convnetion might be at fault here.</p>

Title: SpringMVC can not bind json correctly

Body:
There a json object to be sent to the server, which contains a field：

{"sName":"something"}

In my request model,I declare a var with the same name:

String sName;

But I got null when I receive in the Controller.

I change the field name to lower-case(sname) or add JsonProperty(value="sName") annotation,it work. So where is the problem？

Controller

public ResponseEntity<JSONObject> getComprehensiveInquiryCp(@Validated @RequestBody ComprehensiveInquiryRequestModel body) {
        Map<String, Object> content;
        JSONObject result = new JSONObject();
        String sLicense = body.getSLicense();
...
}

ComprehensiveInquiryRequestModel

@Data
public class ComprehensiveInquiryRequestModel {
...
    //@JsonProperty(value = "sLicense")
    private String sLicense;
...
}


问:
Lua arrays question
I'm a PHP guy so I don't know how to solve this problem. I know how I'd do this in PHP but I have no clue what the constraints are for Lua regarding this problem.

```<code>T = {
  clocktable = {},
  beancabinet = {},
  --...etc
}

T.clocktable[674] = 1
T.clocktable[660] = 1
--...etc
</code>```

Q: How would I loop through "T" to quickly know that the clocktable key includes the extended keys "674" and "660", only knowing "clocktable"?

Note: please be careful of overhead as the "T" table will be very loaded with data and this is in a performance environment.

答:
Is there a reason not to just check if it is not nil?

```<code>T.clocktable[674] ~= nil
</code>```

Title: Lua arrays question

Body:
I'm a PHP guy so I don't know how to solve this problem. I know how I'd do this in PHP but I have no clue what the constraints are for Lua regarding this problem.

T = {
  clocktable = {},
  beancabinet = {},
  --...etc
}

T.clocktable[674] = 1
T.clocktable[660] = 1
--...etc

Q: How would I loop through "T" to quickly know that the clocktable key includes the extended keys "674" and "660", only knowing "clocktable"?

Note: please be careful of overhead as the "T" table will be very loaded with data and this is in a performance environment.

Body:
Is there a reason not to just check if it is not nil?

T.clocktable[674] ~= nil


问:
Azure AD Authentication 401 error "the audience is invalid" AddAzureADBearer .Net Core Web Api
I'm trying to create a simple example of Azure AD authentication using this sample except for my client is JQuery. I am not sure why I get the 401 error about the audience is invalid when the token shows the audience is https://myportal.onmicrosoft.com/test_core_web_api_spa. This matches the API definition in Azure. The only missing piece is the custom scope of user_impersonation but when I make the call using MSAL clientApplication.acquireTokenSilent(tokenRequest2) to acquire the token it my scopes matches the full URL of the API with scope:

```<code>const tokenRequest2 = {
    scopes: ["https://myportal.onmicrosoft.com/test_core_web_api_spa/user_impersonation"]
};
</code>```

In the API to establish authentication I am using this code (I noticed not many examples use this method)

```<code>services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
   .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
</code>```

And the configuration for the API is

```<code>  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "myportal.onmicrosoft.com",
    "TenantId": "my-tenant-guid",
    "ClientId": "my-api-client-guid"
  },
</code>```

I've noticed many examples showing a different format for the API (I assume these are older version) but the exposed API scope is listed in Azure as https://myportal.onmicrosoft.com/test_core_web_api_spa/user_impersonation. I have also added the guid of the client using the Azure dashboard to access this exposed API scope.

Any ideas where I have gone wrong? Alternately, any simple examples using MSAL, JQuery for the client, and a simple .Net Core Web Api? Seems like all the examples I find are out of date or use a different client or a different authentication method.

Update to show expose api settings in Azure for web api app.
I've added an image from Azure showing the settings for the "expose an api" screen. I've added the custom scope user_impersonation then added the client and granted it access to that scope. As you can see my Azure subscription does not have the api://guid format that is seen by others. When I try to use that api://guid format I get the error The resource principal named api://guid was not found in the tenant.

I also added image of the token. The aud tag matches my web api app name in Azure. And the scp lists the scope that I attached to my scopes request. I just cannot see what else to try.

答:
Don't just look at the below code, PLEASE READ!

Building on what pretzelb said, don't worry so much about how your client is configured (as long as your client is working).  Assuming the error you are getting is related to invalid issuer, or invalid audience then try the following.

Debug your api and set a debug point somewhere after your client has tried to connect and look at  HttpContext - Request - Headers - Values, in there you will see your token so drop that in jwt io website and you should find your issuer and your audience which may be completely different than you expected.  At this point change your TokenValidationParameters to agree with what you found in the token, then it should work.

Using .Net Core 3.1 and Micorosft.Identity.Web (currently in preview), I'll show you what worked for me:

Here is my config with fake guids.  Do not try to use the same instance, you need to look at the issuer in the token your client is sending.

```<code>"AzureAD": {
    "Instance": "https://sts.windows.net/",
    "ClientId": "28e36e14-5191-4987-bcdf-982d958de2b3",
    "Domain": "funco.com",
    "TenantId": "744ce43f-a10f-499f-29f3-7je6ef439787"
  }
</code>```

create a model for the AzureAd config section somewhere:

```<code>public class AzureModel
    {
        public String Instance { get; set; }
        public String ClientId { get; set; }
        public String Domain { get; set; }
        public String TenantId { get; set; }
    }
}
</code>```

then...

```<code>services.AddProtectedWebApi(Configuration, AzureADDefaults.AuthenticationScheme,  AzureADDefaults.JwtBearerAuthenticationScheme);
services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options =>
{
    var azureSettings = Configuration.GetSection(AzureADDefaults.AuthenticationScheme).Get<AzureModel>();
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuerSigningKey = true,
        ValidateIssuer = true,
        ValidIssuer = $"{azureSettings.Instance}{azureSettings.TenantId}/",
        ValidateAudience = true,
        ValidAudience = $"https://{azureSettings.Domain}/{azureSettings.ClientId}",
        //ValidateLifetime = true,
        //ClockSkew = TimeSpan.Zero
    };

    options.Events = new JwtBearerEvents();

    options.Events.OnTokenValidated = async context =>
    {
            await Task.FromResult(0);
    };
    options.Events.OnAuthenticationFailed = async context =>
    {
        await Task.CompletedTask;
    };
    options.Events.OnMessageReceived = async context =>
    {
        await Task.CompletedTask;
    };
});
</code>```

Body:
I'm trying to create a simple example of Azure AD authentication using this sample except for my client is JQuery. I am not sure why I get the 401 error about the audience is invalid when the token shows the audience is https://myportal.onmicrosoft.com/test_core_web_api_spa. This matches the API definition in Azure. The only missing piece is the custom scope of user_impersonation but when I make the call using MSAL clientApplication.acquireTokenSilent(tokenRequest2) to acquire the token it my scopes matches the full URL of the API with scope:

const tokenRequest2 = {
    scopes: ["https://myportal.onmicrosoft.com/test_core_web_api_spa/user_impersonation"]
};

In the API to establish authentication I am using this code (I noticed not many examples use this method)

services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
   .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));

And the configuration for the API is

  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "myportal.onmicrosoft.com",
    "TenantId": "my-tenant-guid",
    "ClientId": "my-api-client-guid"
  },

I've noticed many examples showing a different format for the API (I assume these are older version) but the exposed API scope is listed in Azure as https://myportal.onmicrosoft.com/test_core_web_api_spa/user_impersonation. I have also added the guid of the client using the Azure dashboard to access this exposed API scope.

Any ideas where I have gone wrong? Alternately, any simple examples using MSAL, JQuery for the client, and a simple .Net Core Web Api? Seems like all the examples I find are out of date or use a different client or a different authentication method.

Update to show expose api settings in Azure for web api app.
I've added an image from Azure showing the settings for the "expose an api" screen. I've added the custom scope user_impersonation then added the client and granted it access to that scope. As you can see my Azure subscription does not have the api://guid format that is seen by others. When I try to use that api://guid format I get the error The resource principal named api://guid was not found in the tenant.

I also added image of the token. The aud tag matches my web api app name in Azure. And the scp lists the scope that I attached to my scopes request. I just cannot see what else to try.

Body:
Don't just look at the below code, PLEASE READ!

Building on what pretzelb said, don't worry so much about how your client is configured (as long as your client is working).  Assuming the error you are getting is related to invalid issuer, or invalid audience then try the following.

Debug your api and set a debug point somewhere after your client has tried to connect and look at  HttpContext - Request - Headers - Values, in there you will see your token so drop that in jwt io website and you should find your issuer and your audience which may be completely different than you expected.  At this point change your TokenValidationParameters to agree with what you found in the token, then it should work.

Using .Net Core 3.1 and Micorosft.Identity.Web (currently in preview), I'll show you what worked for me:

Here is my config with fake guids.  Do not try to use the same instance, you need to look at the issuer in the token your client is sending.

"AzureAD": {
    "Instance": "https://sts.windows.net/",
    "ClientId": "28e36e14-5191-4987-bcdf-982d958de2b3",
    "Domain": "funco.com",
    "TenantId": "744ce43f-a10f-499f-29f3-7je6ef439787"
  }

create a model for the AzureAd config section somewhere:

public class AzureModel
    {
        public String Instance { get; set; }
        public String ClientId { get; set; }
        public String Domain { get; set; }
        public String TenantId { get; set; }
    }
}

then...

services.AddProtectedWebApi(Configuration, AzureADDefaults.AuthenticationScheme,  AzureADDefaults.JwtBearerAuthenticationScheme);
services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options =>
{
    var azureSettings = Configuration.GetSection(AzureADDefaults.AuthenticationScheme).Get<AzureModel>();
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuerSigningKey = true,
        ValidateIssuer = true,
        ValidIssuer = $"{azureSettings.Instance}{azureSettings.TenantId}/",
        ValidateAudience = true,
        ValidAudience = $"https://{azureSettings.Domain}/{azureSettings.ClientId}",
        //ValidateLifetime = true,
        //ClockSkew = TimeSpan.Zero
    };

    options.Events = new JwtBearerEvents();

    options.Events.OnTokenValidated = async context =>
    {
            await Task.FromResult(0);
    };
    options.Events.OnAuthenticationFailed = async context =>
    {
        await Task.CompletedTask;
    };
    options.Events.OnMessageReceived = async context =>
    {
        await Task.CompletedTask;
    };
});


问:
How do I stretch a background image to cover the entire HTML element?
I'm trying to get a background image of a HTML element (body, div, etc.) to stretch its entire width and height.

Not having much luck. Is it even possible or do I have to do it some other way besides it being a background image?

My current css is:

```<code>body {
    background-position: left top;
    background-image: url(_images/home.jpg);
    background-repeat: no-repeat;
}
</code>```

Edit: I'm not keen on maintaining the CSS in Gabriel's suggestion so I'm changing the layout of the page instead. But that seems like the best answer so I'm marking it as such.

答:
It works for me</p>

```<code>.page-bg {
  background: url("res://background");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
</code>```

OwnerDisplayName: fung

Body:
I'm trying to get a background image of a HTML element (body, div, etc.) to stretch its entire width and height.

Not having much luck. Is it even possible or do I have to do it some other way besides it being a background image?

My current css is:

body {
    background-position: left top;
    background-image: url(_images/home.jpg);
    background-repeat: no-repeat;
}

Edit: I'm not keen on maintaining the CSS in Gabriel's suggestion so I'm changing the layout of the page instead. But that seems like the best answer so I'm marking it as such.

LastEditorDisplayName: fung

Body:
It works for me

.page-bg {
  background: url("res://background");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
}


问:
Jquery special character handling for IE
This is a bit embarrassing scenario but I need solution.

I am using some names as an ID for li HTML element. These names are having special characters in it. using JQuery to grab an Id.

lets say my id usage is

```<code>var abc = li[id="someCompany=\"Some Term\""]
</code>```

if I am calling this as

```<code>alert($(abc).parent()[0]); or
alert($(abc).html());
</code>```

I am getting data and everything is working fine in firefox but not in IE.

My actual id display in application is

```<code><li id="someCompany="Some Term""> xyz </li>
</code>```

I am replacing it with .replace(/"/gi, "\"")

Please let me know, How to get it work in IE, Thanks in advance

Sorry guys, I need to give more information in my question...

Firstly, I get ID as

```<code>var aaa = "someCompany="some Data"";
</code>```

I am replacing it as

```<code>aaa = aaa.replace(/"/gi, "\"").replace(/'/gi, "\'").replace(/&/gi, "\&");
</code>```

Then,

```<code>var abc = aaa.replace(/"/gi, "\\\"").replace(/\:/gi, "\:").replace(/'/gi,"\\\'");
</code>```

and then my question follows....
When I tried changing the above line to

```<code>var abc = aaa.replace(/"/gi, "\"").replace(/\:/gi, "\:").replace(/'/gi,"\'");
</code>```

everything is fine but I have some names like

```<code>var aaa = "someCompany="some's Data""; //In this case I need to handle more special chanracters
</code>```

Here, .replace(/"/gi, "\\\"") is working fine in Firefox but not in IE. How can I make it work in IE as well, please suggest

答: try using browser validation, like single quotes and slash in IE and double in FF.</p>

Body:
This is a bit embarrassing scenario but I need solution.

I am using some names as an ID for li HTML element. These names are having special characters in it. using JQuery to grab an Id.

lets say my id usage is

var abc = li[id="someCompany=\"Some Term\""]

if I am calling this as

alert($(abc).parent()[0]); or
alert($(abc).html());

I am getting data and everything is working fine in firefox but not in IE.

My actual id display in application is

 xyz

I am replacing it with .replace(/"/gi, "\"")

Please let me know, How to get it work in IE, Thanks in advance

Sorry guys, I need to give more information in my question...

Firstly, I get ID as

var aaa = "someCompany="some Data"";

I am replacing it as

aaa = aaa.replace(/"/gi, "\"").replace(/'/gi, "\'").replace(/&/gi, "\&");

Then,

var abc = aaa.replace(/"/gi, "\\\"").replace(/\:/gi, "\:").replace(/'/gi,"\\\'");

and then my question follows....
When I tried changing the above line to

var abc = aaa.replace(/"/gi, "\"").replace(/\:/gi, "\:").replace(/'/gi,"\'");

everything is fine but I have some names like

var aaa = "someCompany="some's Data""; //In this case I need to handle more special chanracters

Here, .replace(/"/gi, "\\\"") is working fine in Firefox but not in IE. How can I make it work in IE as well, please suggest


问:
How to combine two composed applicative functors?
I have two composed applicative functors Maybe [Integer] and want to combine them with <$>/<*> but I am stuck with applying the applicative operation. The following does not typecheck:

```<code>(<*>) (<*>) ((<$>) ((+) <$>) $ Just [1,2,3]) $ Just [4,5,6]
</code>```

Expected result:

```<code>Just [5,6,7,6,7,8,7,8,9]
</code>```

The functor part works, i.e. the intermediate value passed to <*> as the first argument is Just [Integer -> Integer]. I am used to S-expressions so I have a hard time with the Haskell syntax. I know of Compose but I am interested in the mere composition wihtout abstraction.

答:
One other way could be to use the ListT transformer. While it works just fine in this case, for some reason it's a depreciated transformer, marked in red with "Deprecated: This transformer is invalid on most monads".

```<code>import Control.Monad.Trans.List

doit :: (Int-> Int -> Int) -> Maybe [Int] -> Maybe [Int] -> Maybe [Int]
doit f mt1 mt2 = runListT $ f <$> (ListT mt1) <*> (ListT mt2)

λ> doit (+) (Just [1,2,3]) (Just [4,5,6])
Just [5,6,7,6,7,8,7,8,9]
</code>```

Body:
I have two composed applicative functors Maybe [Integer] and want to combine them with <$>/<*> but I am stuck with applying the applicative operation. The following does not typecheck:

(<*>) (<*>) ((<$>) ((+) <$>) $ Just [1,2,3]) $ Just [4,5,6]

Expected result:

Just [5,6,7,6,7,8,7,8,9]

The functor part works, i.e. the intermediate value passed to <*> as the first argument is Just [Integer -> Integer]. I am used to S-expressions so I have a hard time with the Haskell syntax. I know of Compose but I am interested in the mere composition wihtout abstraction.

Body:
One other way could be to use the ListT transformer. While it works just fine in this case, for some reason it's a depreciated transformer, marked in red with "Deprecated: This transformer is invalid on most monads".

import Control.Monad.Trans.List

doit :: (Int-> Int -> Int) -> Maybe [Int] -> Maybe [Int] -> Maybe [Int]
doit f mt1 mt2 = runListT $ f <$> (ListT mt1) <*> (ListT mt2)

λ> doit (+) (Just [1,2,3]) (Just [4,5,6])
Just [5,6,7,6,7,8,7,8,9]


问:
How do I stop selenium automation if 20% or 1st 20 test cases test methods are failed?
I have 100 test cases, if 20% or first 20 test cases all fail how can I stop the execution ?
Already have testng ITestResult where should I break the build? </p>

```<code>@Test(retryAnalyzer = ReTryFail.class, dataProvider = "SanityTCTest", dataProviderClass = utility.Xlsdataprovider.class, groups = "Dashboard", alwaysRun = true)
public void Sanity_TC001(LinkedHashMap<String, String> data) throws InterruptedException, SQLException {

 Some code
}

@Test(retryAnalyzer = ReTryFail.class, dataProvider = "SanityTCTest", dataProviderClass = utility.Xlsdataprovider.class, groups = "Dashboard", alwaysRun = true)
public void Sanity_TC002(LinkedHashMap<String, String> data) throws InterruptedException, SQLException {

 Some code
}

@Test(retryAnalyzer = ReTryFail.class, dataProvider = "SanityTCTest", dataProviderClass = utility.Xlsdataprovider.class, groups = "Dashboard", alwaysRun = true)
public void Sanity_TC003(LinkedHashMap<String, String> data) throws InterruptedException, SQLException {

 Some code
}
///////////////////////////////
</code>```

<p>Where can I break this suite if Result, "FAIL is over 20 ?
DO i require to create new class or can I add in same below? </p>

```<code>@AfterMethod(alwaysRun = true)
public void reporterDataResults(ITestResult Result) throws InterruptedException {
    boolean flag = false;
    Testfail = TestResultStatus.Testfail;

    /*System.out.println("test fail flag in AfterMethod: " + Testfail); */
    if (Result.getStatus() == ITestResult.SKIP) {
        Resmark.put(Result.getName(), "");
        captureScreenShot(Result, "SKIP", getDriver());
        Reporter.log(Result.getName() + " is SKIPPED");
        Add_Log.info(Result.getName() + " is SKIPPED");
        TestResultTL.put(Result.getName(), "SKIP");
        if (!(getDriver() == null)) {
            closeWebBrowser();
        }
    } else if (Result.getStatus() == ITestResult.FAILURE) {

        Collection<String> values = TestResultStatus.mpaskeysnew.get(Result.getName());
        String resultout = String.join(" | ", values);
        System.out.println(resultout);
        Resmark.put(Result.getName(), resultout);

        captureScreenShot(Result, "FAIL", getDriver());
        Reporter.log(Result.getName() + " is FAIL");
        Add_Log.info(Result.getName() + " is FAIL");
        if (!(getDriver() == null)) {
            closeWebBrowser();
        }
        TestResultTL.put(Result.getName(), "FAIL");

    } else {
        captureScreenShot(Result, "PASS", getDriver());
        Resmark.put(Result.getName(), "");
        Reporter.log(Result.getName() + " is PASS");
        Add_Log.info(Result.getName() + " is PASS");
        if (!(getDriver() == null)) {
            closeWebBrowser();
        }
        TestResultTL.put(Result.getName(), "PASS");

    }
    Testskip = false;
    TestResultStatus.Testfail = false;

}
</code>```

答:
You can implement ISuiteListener and in onFinish method you'll have access to  ISuite and  ISuiteResult

Then you can do

```<code>public void onFinish(ISuite suite) {

        final Map<java.lang.String,ISuiteResult>  res = suite.getResults();

                for (ISuiteResult r : res.values()) {
                  context =  r.getTestContext()  ;
                  failedTestCases =context.getFailedTests().size();
                 }
         }
</code>```

size() will give you number of failed test for that suite. Once you know that number you can implement to stop execution using strategies in this

If your test cases are in different suites then in each call to onFinish method you can count number of failed test cases per suite and based on that stop execution.

One other alternative is to implement ITestListener . In onTestFailure method you have access to ITestResult

You can count how many times onTestFailure method is called and based on that stop execution. I think implementing ITestListener is more suitable and easy in your case.

Here , I edited to explain how you'd implement listener

```<code>import org.testng.ISuiteListener;

public class listener implements Itestlistener {
            public int i = 0;

             public void onTestFailure(ITestResult result) {

                     result.getName();
                     i++;
                     //your break logic goes here
                       if (i ==20){
                              // do something or call some function to stop execution
                             }
         }

     }
</code>```

You can read more about testng listeners here .

For your EDIT above (if you want to go that way). Though  I still think you should implement listener, which is more cleaner. It will be called only when the test fails.

But   do the same thing as I did in  onTestFailure method , add a counter and increase it in else if .

```<code>   public int i = 0;  //do this in your class
</code>```

then in your method

```<code> else if (Result.getStatus() == ITestResult.FAILURE) {
    i++;  //increase counter here
    Collection<String> values = TestResultStatus.mpaskeysnew.get(Result.getName());
    String resultout = String.join(" | ", values);
    System.out.println(resultout);
    Resmark.put(Result.getName(), resultout);

    captureScreenShot(Result, "FAIL", getDriver());
    Reporter.log(Result.getName() + " is FAIL");
    Add_Log.info(Result.getName() + " is FAIL");
    if (!(getDriver() == null)) {
        closeWebBrowser();
    }
       TestResultTL.put(Result.getName(), "FAIL");
      if (i==20){
         // stop execution here
      }

  }
</code>```

Body:
I have 100 test cases, if 20% or first 20 test cases all fail how can I stop the execution ?
Already have testng ITestResult where should I break the build?

@Test(retryAnalyzer = ReTryFail.class, dataProvider = "SanityTCTest", dataProviderClass = utility.Xlsdataprovider.class, groups = "Dashboard", alwaysRun = true)
public void Sanity_TC001(LinkedHashMap<String, String> data) throws InterruptedException, SQLException {

 Some code
}

@Test(retryAnalyzer = ReTryFail.class, dataProvider = "SanityTCTest", dataProviderClass = utility.Xlsdataprovider.class, groups = "Dashboard", alwaysRun = true)
public void Sanity_TC002(LinkedHashMap<String, String> data) throws InterruptedException, SQLException {

 Some code
}

@Test(retryAnalyzer = ReTryFail.class, dataProvider = "SanityTCTest", dataProviderClass = utility.Xlsdataprovider.class, groups = "Dashboard", alwaysRun = true)
public void Sanity_TC003(LinkedHashMap<String, String> data) throws InterruptedException, SQLException {

 Some code
}
///////////////////////////////

Where can I break this suite if Result, "FAIL is over 20 ?
DO i require to create new class or can I add in same below?

@AfterMethod(alwaysRun = true)
public void reporterDataResults(ITestResult Result) throws InterruptedException {
    boolean flag = false;
    Testfail = TestResultStatus.Testfail;

    /*System.out.println("test fail flag in AfterMethod: " + Testfail); */
    if (Result.getStatus() == ITestResult.SKIP) {
        Resmark.put(Result.getName(), "");
        captureScreenShot(Result, "SKIP", getDriver());
        Reporter.log(Result.getName() + " is SKIPPED");
        Add_Log.info(Result.getName() + " is SKIPPED");
        TestResultTL.put(Result.getName(), "SKIP");
        if (!(getDriver() == null)) {
            closeWebBrowser();
        }
    } else if (Result.getStatus() == ITestResult.FAILURE) {

        Collection<String> values = TestResultStatus.mpaskeysnew.get(Result.getName());
        String resultout = String.join(" | ", values);
        System.out.println(resultout);
        Resmark.put(Result.getName(), resultout);

        captureScreenShot(Result, "FAIL", getDriver());
        Reporter.log(Result.getName() + " is FAIL");
        Add_Log.info(Result.getName() + " is FAIL");
        if (!(getDriver() == null)) {
            closeWebBrowser();
        }
        TestResultTL.put(Result.getName(), "FAIL");

    } else {
        captureScreenShot(Result, "PASS", getDriver());
        Resmark.put(Result.getName(), "");
        Reporter.log(Result.getName() + " is PASS");
        Add_Log.info(Result.getName() + " is PASS");
        if (!(getDriver() == null)) {
            closeWebBrowser();
        }
        TestResultTL.put(Result.getName(), "PASS");

    }
    Testskip = false;
    TestResultStatus.Testfail = false;

}

Body:
You can implement ISuiteListener and in onFinish method you'll have access to  ISuite and  ISuiteResult

Then you can do

public void onFinish(ISuite suite) {

        final Map<java.lang.String,ISuiteResult>  res = suite.getResults();

                for (ISuiteResult r : res.values()) {
                  context =  r.getTestContext()  ;
                  failedTestCases =context.getFailedTests().size();
                 }
         }

size() will give you number of failed test for that suite. Once you know that number you can implement to stop execution using strategies in this

If your test cases are in different suites then in each call to onFinish method you can count number of failed test cases per suite and based on that stop execution.

One other alternative is to implement ITestListener . In onTestFailure method you have access to ITestResult

You can count how many times onTestFailure method is called and based on that stop execution. I think implementing ITestListener is more suitable and easy in your case.

Here , I edited to explain how you'd implement listener

import org.testng.ISuiteListener;

public class listener implements Itestlistener {
            public int i = 0;

             public void onTestFailure(ITestResult result) {

                     result.getName();
                     i++;
                     //your break logic goes here
                       if (i ==20){
                              // do something or call some function to stop execution
                             }
         }

     }

You can read more about testng listeners here .

For your EDIT above (if you want to go that way). Though  I still think you should implement listener, which is more cleaner. It will be called only when the test fails.

But   do the same thing as I did in  onTestFailure method , add a counter and increase it in else if .

   public int i = 0;  //do this in your class

then in your method

 else if (Result.getStatus() == ITestResult.FAILURE) {
    i++;  //increase counter here
    Collection<String> values = TestResultStatus.mpaskeysnew.get(Result.getName());
    String resultout = String.join(" | ", values);
    System.out.println(resultout);
    Resmark.put(Result.getName(), resultout);

    captureScreenShot(Result, "FAIL", getDriver());
    Reporter.log(Result.getName() + " is FAIL");
    Add_Log.info(Result.getName() + " is FAIL");
    if (!(getDriver() == null)) {
        closeWebBrowser();
    }
       TestResultTL.put(Result.getName(), "FAIL");
      if (i==20){
         // stop execution here
      }

  }


问:
CSV File with minus sign after number. "ValueError: could not convert string to float:"
I have several CSV Files, where the a negative number is displayed with the minus sign after the number (30.50- instead of -30.50). If I try to convert the column to an integer, Python returns an ValueError (ValueError: could not convert string to float: '30.50-')

Does anybody have an idea how to handle that?

Thanks in Advance!

Best,
Moritz

Body:
Like this:

In [141]: df = pd.DataFrame({'A':['30.50-', '20', '-10.01','22.10-']})
In [142]: df
Out[142]:
        A
0  30.50-
1      20
2  -10.01
3  22.10-

In [143]: df['A'] = df['A'].apply(lambda x: '-'+ (x.rstrip('-')) if x.endswith('-') else x).astype(float)

In [145]: df
Out[145]:
       A
0 -30.50
1  20.00
2 -10.01
3 -22.10

In [144]: df.dtypes
Out[144]:
A    float64
dtype: object


问:
Bottom Navigation showing in other fragment
I will be glad to finally get a help for this. I have a single activity application. One of the fragments is also a nav_host(say nav_host2) to other fragments for its bottom navigation.

I want to exit from the from fragment in nav_host two to nav_host1 using deep-linking but notice that the bottom navigation on nav_host2 show on nav_host1 which I do not want as seen in the diagram below.

How can I exit the nav_host2 without having the bottom navigation show even-though it is null

答:
use addOnDestinationChangedListener and hide and show bottomNavigation with respect to destinationId

```<code>private val AUTH_NAV_GRAPH = arrayOf(
    R.id.destination_splash,
    R.id.destination_login,
    R.id.destination_register,
    R.id.destination_forgetpassword
)

val navController = findNavController(this,R.id.nav_host_fragment)// this maybe change
navController.addOnDestinationChangedListener { controller, destination, arguments ->
   if(destination.id in AUTH_NAV_GRAPH) {
       bottomNavigation?.visibility=View.GONE

   } else {
       bottomNavigation?.visibility=View.VISIBLE
   }
}
</code>```

where AUTH_NAV_GRAPH are like fragments with no bottomNavigation

Body:
use addOnDestinationChangedListener and hide and show bottomNavigation with respect to destinationId

private val AUTH_NAV_GRAPH = arrayOf(
    R.id.destination_splash,
    R.id.destination_login,
    R.id.destination_register,
    R.id.destination_forgetpassword
)

val navController = findNavController(this,R.id.nav_host_fragment)// this maybe change
navController.addOnDestinationChangedListener { controller, destination, arguments ->
   if(destination.id in AUTH_NAV_GRAPH) {
       bottomNavigation?.visibility=View.GONE

   } else {
       bottomNavigation?.visibility=View.VISIBLE
   }
}

where AUTH_NAV_GRAPH are like fragments with no bottomNavigation