问:
Correctly structuring text data for text generation with Tensorflow model
I am trying to train my model to generate sentences no longer that 210 characters. From what I have read I have only seen training on 'continuous' text. Like a book. However I am trying to train my model on single sentences.

I'm pretty new to tensorflow and ML so right now I am able to train my model but it generates garbage, seemingly random text. I have 10,000 sentences so I think I have sufficient data.

Overview of my data

Structure [['SENTENCE'], ['SENTENCE2']...]

Data Prep

tokenizer = keras.preprocessing.text.Tokenizer(num_words=209, lower=False, char_level=True, filters='#$%&()*+-<=>@[\\]^_`{|}~\t\n')
tokenizer.fit_on_texts(df['title'].values)
df['encoded_with_keras'] = tokenizer.texts_to_sequences(df['title'].values)

dataset = df['encoded_with_keras'].values
dataset = tf.keras.preprocessing.sequence.pad_sequences(dataset, padding='post')

dataset = dataset.flatten()

dataset = tf.data.Dataset.from_tensor_slices(dataset)

sequences = dataset.batch(seq_len+1, drop_remainder=True)

def create_seq_targets(seq):
    input_txt = seq[:-1]
    target_txt = seq[1:]
    return input_txt, target_txt

dataset = sequences.map(create_seq_targets)

dataset = dataset.shuffle(buffer_size).batch(batch_size, drop_remainder=True)
```

<p><strong>Model</strong></p>

<pre class="lang-py prettyprint-override"><code>def create_model(vocab_size, embed_dim, rnn_neurons, batch_size):
    model = Sequential()
    model.add(Embedding(vocab_size, embed_dim, batch_input_shape=[batch_size, None],input_length=209, mask_zero=True))
    model.add(LSTM(rnn_neurons, return_sequences=True, stateful=True,))
    model.add(Dropout(0.2))
    model.add(Dense(258, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(vocab_size, activation='softmax'))
    model.compile(optimizer='adam', loss="sparse_categorical_crossentropy")
    return model
</code>```

When I give the model a sequence to start from I get back absolute nonsense and eventually the model predicts a 0 which is not in the char_index mapping.

Edit

Text Generation

epochs = 2

# Directory where the checkpoints will be saved
checkpoint_dir = './training_checkpoints'
# Name of the checkpoint files
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt_{epoch}")

checkpoint_callback=tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_prefix,
    save_weights_only=True)

model = create_model(vocab_size = vocab_size,
  embed_dim=embed_dim,
  rnn_neurons=rnn_neurons,
  batch_size=1)

model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))

model.build(tf.TensorShape([1, None]))

def generate_text(model, start_string):
  num_generate = 200

  input_eval = [char_2_index[s] for s in start_string]
  input_eval = tf.expand_dims(input_eval, 0)

  text_generated = []

  temperature = 1

  # model.reset_states()
  for i in range(num_generate):
      print(text_generated)
      predictions = model(input_eval)

      predictions = tf.squeeze(predictions, 0)

      predictions = predictions / temperature

      predicted_id = tf.random.categorical(predictions, num_samples=1)[-1,0].numpy()
      print(predicted_id)

      input_eval = tf.expand_dims([predicted_id], 0)

      text_generated.append(index_2_char[predicted_id])

  return (start_string + ''.join(text_generated))
```

答:
There are a few things that must be changed on the first sight.

Tokenizer must have num_words = vocab_size

At first (didn't analyse it deeply), I can't imagine why you're flattening your dataset and getting slices if it's probably correctly structured

You cannot use stateful=True if you don't want that "batch 2 is a sequel of batch 1", you have individual sentences, so stateful=False. (Unless you are training correctly with manual training loops and resetting states for each batch, which is unnecessary trouble in the training phase)

What you need to check visually:

Input data must have format like:

```<code>[
    [1,2,3,6,10,4,10, ...up to sentence length - 1...],
    [5,6,3,6,7,3,11,... up to sentence length - 1...],
    .... up to number of sentences ...
]
</code>```

Output data must then be:

```<code>[
    [2,3,6,10,4,10,15 ...], #equal to input data, shifted by 1
    [6,3,6,7,3,11,13, ...],
    ...
]
</code>```

Print a few rows of them to check if they're correctly preprocessed as intended.

Training will then be easy:

```<code>model.fit(input_data, output_data, epochs=....)
</code>```

Yes, your model will predict zeros, as you have zeros in your data, that's not weird: you did a pad_sequences.

You can interpret a zero as a "sentence end" in this case, since you did a 'post' pading. When your model gives you a zero, it decided that the sentence it's generating should end at that point - if it was well trained, it will probably continue outputting zeros for that sentence from this point on.

Generating new senteces

This part is more complex and you need to rewrite the model, now being stative=True, and transfer the weights from the trained model to this new model.

Before anything, call model.reset_states().

You will need to manually feed a batch with shape (number_of_sentences=batch_size, 1). This will be the "first character" of each of the sentences it will generate. The output will be the "second character" of each sentence.

Get this output and feed the model with it. It will generate the "third character" of each sentence. And so on.

When all outputs are zero, all sentences are fully generated and you can stop the loop.

Call model.reset_states() again before trying to generate a new batch of sentences.

You can find examples of this kind of predicting here: https://stackoverflow.com/a/50235563/2097240

Body:
I am trying to train my model to generate sentences no longer that 210 characters. From what I have read I have only seen training on 'continuous' text. Like a book. However I am trying to train my model on single sentences.

I'm pretty new to tensorflow and ML so right now I am able to train my model but it generates garbage, seemingly random text. I have 10,000 sentences so I think I have sufficient data.

Overview of my data

Structure [['SENTENCE'], ['SENTENCE2']...]

Data Prep

tokenizer = keras.preprocessing.text.Tokenizer(num_words=209, lower=False, char_level=True, filters='#$%&()*+-<=>@[\\]^_`{|}~\t\n')
tokenizer.fit_on_texts(df['title'].values)
df['encoded_with_keras'] = tokenizer.texts_to_sequences(df['title'].values)

dataset = df['encoded_with_keras'].values
dataset = tf.keras.preprocessing.sequence.pad_sequences(dataset, padding='post')

dataset = dataset.flatten()

dataset = tf.data.Dataset.from_tensor_slices(dataset)

sequences = dataset.batch(seq_len+1, drop_remainder=True)

def create_seq_targets(seq):
    input_txt = seq[:-1]
    target_txt = seq[1:]
    return input_txt, target_txt

dataset = sequences.map(create_seq_targets)

dataset = dataset.shuffle(buffer_size).batch(batch_size, drop_remainder=True)

Model

def create_model(vocab_size, embed_dim, rnn_neurons, batch_size):
    model = Sequential()
    model.add(Embedding(vocab_size, embed_dim, batch_input_shape=[batch_size, None],input_length=209, mask_zero=True))
    model.add(LSTM(rnn_neurons, return_sequences=True, stateful=True,))
    model.add(Dropout(0.2))
    model.add(Dense(258, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(vocab_size, activation='softmax'))
    model.compile(optimizer='adam', loss="sparse_categorical_crossentropy")
    return model

When I give the model a sequence to start from I get back absolute nonsense and eventually the model predicts a 0 which is not in the char_index mapping.

Edit

Text Generation

epochs = 2

# Directory where the checkpoints will be saved
checkpoint_dir = './training_checkpoints'
# Name of the checkpoint files
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt_{epoch}")

checkpoint_callback=tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_prefix,
    save_weights_only=True)

model = create_model(vocab_size = vocab_size,
  embed_dim=embed_dim,
  rnn_neurons=rnn_neurons,
  batch_size=1)

model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))

model.build(tf.TensorShape([1, None]))

def generate_text(model, start_string):
  num_generate = 200

  input_eval = [char_2_index[s] for s in start_string]
  input_eval = tf.expand_dims(input_eval, 0)

  text_generated = []

  temperature = 1

  # model.reset_states()
  for i in range(num_generate):
      print(text_generated)
      predictions = model(input_eval)

      predictions = tf.squeeze(predictions, 0)

      predictions = predictions / temperature

      predicted_id = tf.random.categorical(predictions, num_samples=1)[-1,0].numpy()
      print(predicted_id)

      input_eval = tf.expand_dims([predicted_id], 0)

      text_generated.append(index_2_char[predicted_id])

  return (start_string + ''.join(text_generated))

Body:
There are a few things that must be changed on the first sight.

Tokenizer must have num_words = vocab_size

At first (didn't analyse it deeply), I can't imagine why you're flattening your dataset and getting slices if it's probably correctly structured

You cannot use stateful=True if you don't want that "batch 2 is a sequel of batch 1", you have individual sentences, so stateful=False. (Unless you are training correctly with manual training loops and resetting states for each batch, which is unnecessary trouble in the training phase)

What you need to check visually:

Input data must have format like:

[
    [1,2,3,6,10,4,10, ...up to sentence length - 1...],
    [5,6,3,6,7,3,11,... up to sentence length - 1...],
    .... up to number of sentences ...
]

Output data must then be:

[
    [2,3,6,10,4,10,15 ...], #equal to input data, shifted by 1
    [6,3,6,7,3,11,13, ...],
    ...
]

Print a few rows of them to check if they're correctly preprocessed as intended.

Training will then be easy:

model.fit(input_data, output_data, epochs=....)

Yes, your model will predict zeros, as you have zeros in your data, that's not weird: you did a pad_sequences.

You can interpret a zero as a "sentence end" in this case, since you did a 'post' pading. When your model gives you a zero, it decided that the sentence it's generating should end at that point - if it was well trained, it will probably continue outputting zeros for that sentence from this point on.

Generating new senteces

This part is more complex and you need to rewrite the model, now being stative=True, and transfer the weights from the trained model to this new model.

Before anything, call model.reset_states().

You will need to manually feed a batch with shape (number_of_sentences=batch_size, 1). This will be the "first character" of each of the sentences it will generate. The output will be the "second character" of each sentence.

Get this output and feed the model with it. It will generate the "third character" of each sentence. And so on.

When all outputs are zero, all sentences are fully generated and you can stop the loop.

Call model.reset_states() again before trying to generate a new batch of sentences.

You can find examples of this kind of predicting here: https://stackoverflow.com/a/50235563/2097240


来源: stackexchange

问:
Why segment merge in Elasticsearch requires stopping the writes to index
I am looking to run the optimize(ES 1.X) which is now known as forcemerge API in ES latest version. After reading some articles like this and this. it seems we should run it only on read-only indices, quoting the official ES docs:

Force merge should only be called against read-only indices. Running
  force merge against a read-write index can cause very large segments
  to be produced (>5Gb per segment)

But I don't understand the

Reason behind putting index on read-only mode before running forcemerge or optimize API.

As explained in above ES doc, it could cause very large segments which shouldn't be the case as what I understand is that, new updates are first written in memory which are written to segments when refresh happens, so why having write during forcemerge can produce the very large segments?

Also is there is any workaround if we don't want to put the index on read-only mode and still run force merge to expunge delete.

Let me know if I need to provide any additional information.

答:
forcemerge can significantly improve the performance of your queries as it allows you to merge the existing number of segments into a smaller number of segments which is more efficient for querying, as segments get searched sequentially. While merging, also all documents marked for deletion get cleaned up.

Merging happens regularly and automatically in the background as part of Elasticsearch‘s housekeeping based on a merge policy.

The tricky thing: only segments up to 5 GB are considered by the merge policy. Using the forcemerge API with the parameter that allows you to specify the number of resulting segments, you risk that the resulting segment(s) get bigger than 5GB, meaning that they will no longer be considered by future merge requests. As long as you don‘t delete or update documents there is nothing wrong about that. However, if you keep on deleting or updating documents, Lucene will mark the old version of your documents in the existing segments as deleted and write the new version of your documents into new segments. If your deleted documents reside in segments larger than 5GB, no more housekeeping is done on them, i.e. the documents marked for deletion will never get cleaned up.

By setting an index to readonly prior to doing a force-merge, you ensure that you will not end up with huge segments, containing a lot of legacy documents, which consume precious resources in memory and on disk and slow down your queries.

A refresh is doing something different: it‘s correct that documents you want to get indexed are first processed in memory, before getting written to disk. But the data structure that allows you to actually find a document (the „segment“) does not get created for every single document right away, as this would be highly inefficient. Segments are only created when the internal buffer gets full, or when a refresh occurs. By triggering a refresh you make a document immediately available for finding. Still the segment at first only lives in memory, as - again - it would be extremely inefficient to immediately sync every segment to disk right after it got created. Segments in memory get periodically synced to disk. Even if you pull the plug before a sync to disk happened you don‘t lose any information, as Elasticsearch maintains a translog that will allow Elasticsearch to „replay“ all indexing request that did not make it yet into a segment on disk.

ContentLicense: CC BY-SA 4.0


问:
Excel VBA lookup issue
I'm new to excel VBA though I tried putting efforts but of no luck.

Problem Statement:

I have a sheet with Source (color white) row and Destination (color Yellow) row, for each source there is a corresponding destination row in next line. I have to look for an application name which user input at the beginning and will search throughout the sheet (more than 10000 rows)  in column 6 and it must extract the source row also if found in destination and destination row also if found in source row in sheet2.

And also one cell may have many application name within it so it should trim all other app names from that cell and leave only the searched app name.

Here is the partial code i tried :

```<code>Sub GetInterfaceCounts()
    Dim RANGEBOTTOM As String
    Dim cell
    Dim strAction As String
    Dim intAdd As Integer
    Dim strName As String

    intAdd = 0
    RANGEBOTTOM = "G700"
    strName = InputBox(Prompt:="Please enter the application name.", _
    Title:="Application Name", Default:="Application")

    For Each cell In Range("G2:" & RANGEBOTTOM)
        strAction = cell.Value

        If InStr(1, strAction, strName) <> 0 Then
            intAdd = intAdd + 1
        End If
    Next

    MsgBox "Total number of " & strName & " counts are :" & CStr(intAdd)
    GetMS4AppInventory (strName)
End Sub

Sub GetMS4AppInventory(strName As String)

    Dim strAction
    Dim intAdd As Integer
    Dim RowIndex As Integer
    RowIndex = 0

    Sheets("Sheet1").Select

    'For Each cell In Range("G2:G700")
    With Worksheets("Sheet1").Range("G2:G700")
        Set strAction = .Find(strName, LookIn:=xlValues)

        'strAction = cell.Value
        If Not strAction Is Nothing Then
            Do
                If InStr(1, strAction, strName) <> 0 Then
                    Rows(strAction.Row).Select
                    Selection.Copy

                    Sheets("MS4Inventory").Select
                    Rows(RowIndex + 1).Select
                    Selection.Insert Shift:=xlDown
                    Rows(RowIndex + 2).Select
                    Application.CutCopyMode = False
                    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                    Cells(RowIndex + 3, 1).Select
                End If

                Set strAction = .FindNext(strAction)  //gets hanged here go to infinite loop
            Loop While Not strAction Is Nothing
        End If
    End With
End Sub
</code>```

If anybody could help me it will be great else manually doing seggregation of inventory will suck me.

Regards,

Vijay

答:
When you use FindNext, you have to store the first found cell's address and compare it.  strAction, in your example, will never be Nothing because FindNext will keep finding the first cell that had it.

I'm not sure how your white and yellow rows factor into this, but here's a basic structure for finding cells and copy their rows.  Maybe you can modify it to your needs or clarify what your existing data looks like.

```<code>Sub GetInterfaceCounts()

    Dim sName As String
    Dim rFound As Range
    Dim lCount As Long
    Dim sFirstAdd As String

    'Get the application name from the user
    sName = InputBox(Prompt:="Please enter the application name.", _
        Title:="Application Name", Default:="Application")

    'if the user doesn't press cancel
    If Len(sName) > 0 Then
        'Find the first instance of the application
        Set rFound = Sheet1.Columns(7).Find(sName, , xlValues, xlPart, , , False)

        'if something was found
        If Not rFound Is Nothing Then
            'Remember the first address where it was found
            sFirstAdd = rFound.Address

            Do
                lCount = lCount + 1
                'Copy the entirerow to the other sheet
                rFound.EntireRow.Copy _
                    rFound.Parent.Parent.Sheets("MS4Inventory").Cells(lCount, 1).EntireRow
                'Find the next instance
                Set rFound = Sheet1.Columns(7).FindNext(rFound)

            'if we've looped around to the first found, then get out
            Loop Until rFound.Address = sFirstAdd
        End If

        MsgBox "Total number of " & sName & " counts are :" & lCount
    End If

End Sub
</code>```

ContentLicense: CC BY-SA 3.0

Title: Excel VBA lookup issue

Body:
I'm new to excel VBA though I tried putting efforts but of no luck.

Problem Statement:

I have a sheet with Source (color white) row and Destination (color Yellow) row, for each source there is a corresponding destination row in next line. I have to look for an application name which user input at the beginning and will search throughout the sheet (more than 10000 rows)  in column 6 and it must extract the source row also if found in destination and destination row also if found in source row in sheet2.

And also one cell may have many application name within it so it should trim all other app names from that cell and leave only the searched app name.

Here is the partial code i tried :

Sub GetInterfaceCounts()
    Dim RANGEBOTTOM As String
    Dim cell
    Dim strAction As String
    Dim intAdd As Integer
    Dim strName As String

    intAdd = 0
    RANGEBOTTOM = "G700"
    strName = InputBox(Prompt:="Please enter the application name.", _
    Title:="Application Name", Default:="Application")

    For Each cell In Range("G2:" & RANGEBOTTOM)
        strAction = cell.Value

        If InStr(1, strAction, strName) <> 0 Then
            intAdd = intAdd + 1
        End If
    Next

    MsgBox "Total number of " & strName & " counts are :" & CStr(intAdd)
    GetMS4AppInventory (strName)
End Sub

Sub GetMS4AppInventory(strName As String)

    Dim strAction
    Dim intAdd As Integer
    Dim RowIndex As Integer
    RowIndex = 0

    Sheets("Sheet1").Select

    'For Each cell In Range("G2:G700")
    With Worksheets("Sheet1").Range("G2:G700")
        Set strAction = .Find(strName, LookIn:=xlValues)

        'strAction = cell.Value
        If Not strAction Is Nothing Then
            Do
                If InStr(1, strAction, strName) <> 0 Then
                    Rows(strAction.Row).Select
                    Selection.Copy

                    Sheets("MS4Inventory").Select
                    Rows(RowIndex + 1).Select
                    Selection.Insert Shift:=xlDown
                    Rows(RowIndex + 2).Select
                    Application.CutCopyMode = False
                    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                    Cells(RowIndex + 3, 1).Select
                End If

                Set strAction = .FindNext(strAction)  //gets hanged here go to infinite loop
            Loop While Not strAction Is Nothing
        End If
    End With
End Sub

If anybody could help me it will be great else manually doing seggregation of inventory will suck me.

Regards,

Vijay

Body:
When you use FindNext, you have to store the first found cell's address and compare it.  strAction, in your example, will never be Nothing because FindNext will keep finding the first cell that had it.

I'm not sure how your white and yellow rows factor into this, but here's a basic structure for finding cells and copy their rows.  Maybe you can modify it to your needs or clarify what your existing data looks like.

Sub GetInterfaceCounts()

    Dim sName As String
    Dim rFound As Range
    Dim lCount As Long
    Dim sFirstAdd As String

    'Get the application name from the user
    sName = InputBox(Prompt:="Please enter the application name.", _
        Title:="Application Name", Default:="Application")

    'if the user doesn't press cancel
    If Len(sName) > 0 Then
        'Find the first instance of the application
        Set rFound = Sheet1.Columns(7).Find(sName, , xlValues, xlPart, , , False)

        'if something was found
        If Not rFound Is Nothing Then
            'Remember the first address where it was found
            sFirstAdd = rFound.Address

            Do
                lCount = lCount + 1
                'Copy the entirerow to the other sheet
                rFound.EntireRow.Copy _
                    rFound.Parent.Parent.Sheets("MS4Inventory").Cells(lCount, 1).EntireRow
                'Find the next instance
                Set rFound = Sheet1.Columns(7).FindNext(rFound)

            'if we've looped around to the first found, then get out
            Loop Until rFound.Address = sFirstAdd
        End If

        MsgBox "Total number of " & sName & " counts are :" & lCount
    End If

End Sub


问:
Find count of each consecutive characters
Need to find the count of each consecutive characters in a row.
Ex: aaaabbccaa
output: 4a2b2c2a

Character may repeat but need to count only consecutive ones. I also need to maintain original sequence.

I tried following but it groups all characters so was not useful.

```<code>str.GroupBy(c => c).Select(g => new { g.Key, Count = g.Count() }).ToList().ForEach(x => str+= x.Count + "" + x.Key)
</code>```

答:
Non-LINQ solution (dotnetfiddle): </p>

```<code>using System;
using System.Text;

public class Program
{
    public static void Main()
    {
        // produces 4a2b2c2a
        Console.WriteLine(GetConsecutiveGroups("aaaabbccaa"));
    }

    private static string GetConsecutiveGroups(string input)
    {
        var result = new StringBuilder();
        var sb = new StringBuilder();

        foreach (var c in input)
        {
            if (sb.Length == 0 || sb[sb.Length - 1] == c)
            {
                sb.Append(c);
            }
            else
            {
                result.Append($"{sb.Length}{sb[0]}");
                sb.Clear();
                sb.Append(c);
            }
        }

        if (sb.Length > 0)
        {
            result.Append($"{sb.Length}{sb[0]}");
        }

        return result.ToString();
    }
}
</code>```

Body:
Need to find the count of each consecutive characters in a row.
Ex: aaaabbccaa
output: 4a2b2c2a

Character may repeat but need to count only consecutive ones. I also need to maintain original sequence.

I tried following but it groups all characters so was not useful.

str.GroupBy(c => c).Select(g => new { g.Key, Count = g.Count() }).ToList().ForEach(x => str+= x.Count + "" + x.Key)

Body:
Non-LINQ solution (dotnetfiddle):

using System;
using System.Text;

public class Program
{
    public static void Main()
    {
        // produces 4a2b2c2a
        Console.WriteLine(GetConsecutiveGroups("aaaabbccaa"));
    }

    private static string GetConsecutiveGroups(string input)
    {
        var result = new StringBuilder();
        var sb = new StringBuilder();

        foreach (var c in input)
        {
            if (sb.Length == 0 || sb[sb.Length - 1] == c)
            {
                sb.Append(c);
            }
            else
            {
                result.Append($"{sb.Length}{sb[0]}");
                sb.Clear();
                sb.Append(c);
            }
        }

        if (sb.Length > 0)
        {
            result.Append($"{sb.Length}{sb[0]}");
        }

        return result.ToString();
    }
}


问:
How to parse Cloud SQL CSV export nulls ("N) with Pandas read_csv
Using Airflow, we're exporting data from Google Cloud SQL to a CSV, and eventually loading that CSV into a different SQL warehouse. However, Cloud SQL exports null values as the string "N (This is a known Google issue: https://issuetracker.google.com/issues/64579566). As an interim step, we need to open up the file and delete the "N.  There are also actual strings in the csv, using " normally.

Ideally, we'd be able to do this with pandas - we're setup to use a DataFrame for the next step.  However, I can't get read_csv to interpret the "N as nulls.  Here's the basic command I've tried:

df = pd.read_csv(filepath, na_values='"N')

I've also tried na_values="\"N", but that gave me the same results.

It appears that read_csv checks for strings first, and for nulls second, so I'm getting output that looks like this:

```<code>Id                                                                           100
IncidentDate                                                 2018-08-29 07:00:00
StudentInvolved                                                      Psueudonym
IncidentLocation                                                       Classroom
IncidentCategory                                             Academic dishonesty
IncidentDescriptionDetails     [TESTING] How does this insert into table when...
FollowUp                                                                       0
ConsequenceGiven                                             Afterschool Academy
ConsequenceStartDate                                                         N,N
ConsequenceEndDate                                                           N,N
PrimaryViolation                                                 <email address>
Weapon                                                             <school_name>
CreatedBy                                                                    N,N
SiteName                                                                    1000
DisciplinaryActionAuthority                                                N,0,N
DocumentationUrl                                                             N,N
SIS_ID                                                                       N,N
SubmittedBy                                                                  N,N
Deleted                                                                      N,N
FollowUpNotes                                                                N,N
StudentLists_fk                                                              N,N
</code>```

Any ideas on if read_csv is capable of parsing this?

答:
Ultimately had to brute force it by replacing the text.

```<code>    with open(filepath, 'r', encoding="utf-8") as inputFile:
        raw_text = inputFile.read()

    edited_text = raw_text.replace('"N,', ',')
    edited_text = edited_text.replace(',"N\n', ',\n')

    with open(edited_filepath, 'w', newline='\n', encoding="utf-8") as outputFile:
        outputFile.write(edited_text)

    df = pd.read_csv(edited_filepath, names=incident_columns)
</code>```

The one downside is that this will mess up any string that legitimately begins with the N, or N\n, but hopefully our users aren't in the habit of inputting stuff like that.

Body:
Using Airflow, we're exporting data from Google Cloud SQL to a CSV, and eventually loading that CSV into a different SQL warehouse. However, Cloud SQL exports null values as the string "N (This is a known Google issue: https://issuetracker.google.com/issues/64579566). As an interim step, we need to open up the file and delete the "N.  There are also actual strings in the csv, using " normally.

Ideally, we'd be able to do this with pandas - we're setup to use a DataFrame for the next step.  However, I can't get read_csv to interpret the "N as nulls.  Here's the basic command I've tried:

df = pd.read_csv(filepath, na_values='"N')

I've also tried na_values="\"N", but that gave me the same results.

It appears that read_csv checks for strings first, and for nulls second, so I'm getting output that looks like this:

Id                                                                           100
IncidentDate                                                 2018-08-29 07:00:00
StudentInvolved                                                      Psueudonym
IncidentLocation                                                       Classroom
IncidentCategory                                             Academic dishonesty
IncidentDescriptionDetails     [TESTING] How does this insert into table when...
FollowUp                                                                       0
ConsequenceGiven                                             Afterschool Academy
ConsequenceStartDate                                                         N,N
ConsequenceEndDate                                                           N,N
PrimaryViolation                                                 <email address>
Weapon                                                             <school_name>
CreatedBy                                                                    N,N
SiteName                                                                    1000
DisciplinaryActionAuthority                                                N,0,N
DocumentationUrl                                                             N,N
SIS_ID                                                                       N,N
SubmittedBy                                                                  N,N
Deleted                                                                      N,N
FollowUpNotes                                                                N,N
StudentLists_fk                                                              N,N

Any ideas on if read_csv is capable of parsing this?

Body:
Ultimately had to brute force it by replacing the text.

    with open(filepath, 'r', encoding="utf-8") as inputFile:
        raw_text = inputFile.read()

    edited_text = raw_text.replace('"N,', ',')
    edited_text = edited_text.replace(',"N\n', ',\n')

    with open(edited_filepath, 'w', newline='\n', encoding="utf-8") as outputFile:
        outputFile.write(edited_text)

    df = pd.read_csv(edited_filepath, names=incident_columns)

The one downside is that this will mess up any string that legitimately begins with the N, or N\n, but hopefully our users aren't in the habit of inputting stuff like that.


问:
Linear gradient is not working in Outlook
I know this is a very basic question but I have tried many StackOverflow solutions but nothing worked for me.

This is the HTML content which I am using.

<div style="background:rgba(0,0,0,0.5);background:-moz-linear-gradient(top,rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%);background:-o-linear-gradient(top,rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%);background:-ms-linear-gradient(top,rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%);background: rgba(0,0,0,0.5);background: -moz-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* FF3.6+ */background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.5)), color-stop(100%,rgba(0,0,0,0.7))); /* Chrome,Safari4+ *//* background: -webkit-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); */ /* Chrome10+,Safari5.1+ */background: -o-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* Opera 11.10+ */background: -ms-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* IE10+ */background: linear-gradient(to bottom, rgba(0,0,0,0.5) 0%,rgba(216, 30, 30, 0.7) 100%);  /* W3C */">

This is heading

```

It is working on Gmail but not in Outlook.

Screenshot of the Gmail

Screenshot of the Outlook

Please correct me what I am doing wrong.

答:
Unfortunately, Outlook does not support linear-gradients.
Outlook, especially for Windows, primarily supports basic CSS.

See Campaign monitor:
https://www.campaignmonitor.com/css/color-background/css-gradients/

Body:
I know this is a very basic question but I have tried many StackOverflow solutions but nothing worked for me.

This is the HTML content which I am using.

<div style="background:rgba(0,0,0,0.5);background:-moz-linear-gradient(top,rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%);background:-o-linear-gradient(top,rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%);background:-ms-linear-gradient(top,rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%);background: rgba(0,0,0,0.5);background: -moz-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* FF3.6+ */background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.5)), color-stop(100%,rgba(0,0,0,0.7))); /* Chrome,Safari4+ *//* background: -webkit-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); */ /* Chrome10+,Safari5.1+ */background: -o-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* Opera 11.10+ */background: -ms-linear-gradient(top,  rgba(0,0,0,0.5) 0%,rgba(0,0,0,0.7) 100%); /* IE10+ */background: linear-gradient(to bottom, rgba(0,0,0,0.5) 0%,rgba(216, 30, 30, 0.7) 100%);  /* W3C */">

This is heading

It is working on Gmail but not in Outlook.

Screenshot of the Gmail

Screenshot of the Outlook

Please correct me what I am doing wrong.


问:
I am having a problem with a Syntax error with a Filter command in VBA
```<code>Private Sub CboQuickSearch_AfterUpdate()
    Me.Filter = "Item Card Number = " & Me.CboQuickSearch.Value"
    Me.FilterOn = True
End Sub
</code>```

答:
Wrap a text value in single quotes:</p>

```<code>Me.Filter = "Item Card Number = '" & Me.CboQuickSearch.Value & "'"
</code>```

Body:
Wrap a text value in single quotes:

Me.Filter = "Item Card Number = '" & Me.CboQuickSearch.Value & "'"


问:
How to responsive owl carousel in react js
I want to show only one item in mobile view. but it shows 4 item which i show in desktop view,
please help me out.

Here's my code:-

```<code>    <div className="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
      <OwlCarousel className="owl-theme hotel-slider tab-slide" id="tab-slider" loop margin={10} items={4} nav>
        {Data.shimladetail.map((shimlatour, i) => (

        <div className="item no-brdr">
          <img src={shimlatour.alltourimg} className="tour-img" />
          <div className="tour-cntnt">
            <h5 className="tours-heading">{shimlatour.alltourtitle}</h5>
            <p className="tour-pera">{shimlatour.alltourdiscription}</p>
          </div>
        </div>

        ))}
      </OwlCarousel>
    </div>
</code>```

答:
try to use plugin events please like onResize && onResized
 or options like mergeFit && autoWidth , you find this options in this link in below :

https://www.npmjs.com/package/react-owl-carousel

Body:
I want to show only one item in mobile view. but it shows 4 item which i show in desktop view,
please help me out.

Here's my code:-

      <OwlCarousel className="owl-theme hotel-slider tab-slide" id="tab-slider" loop margin={10} items={4} nav>
        {Data.shimladetail.map((shimlatour, i) => (

{shimlatour.alltourtitle}

{shimlatour.alltourdiscription}

        ))}
      </OwlCarousel>


问:
Model A is not associated to Model B sequelize error
I have the following problem,
I have 2 Models:
Client

```<code>module.exports = (sequelize, DataTypes) => {
const Client = sequelize.define('Client', {
  first_name: DataTypes.STRING,
  last_name: DataTypes.STRING,
  phone: DataTypes.STRING,
  mobile_phone: DataTypes.STRING,
  email: DataTypes.STRING,
});

//creating association Client with Gender
Client.associate = function (models) {
  Client.hasOne(models.Gender, {
       //
  });
};

return Client; }
</code>```

and Gender:

```<code>module.exports = (sequelize, DataTypes) => {
const Gender = sequelize.define('Gender', {
  name: DataTypes.STRING,
});

//creating association Gender with Employee
Gender.associate = function (models) {
  models.Gender.hasMany(models.Employee, {
    //
  });
};

//creating association Gender with Client
Gender.associate = function (models) {
  models.Gender.belongsTo(models.Client, {
    foreignKey: 'gender_id'
  });
};

return Gender; }
</code>```

My ClientController:

```<code>const { Client, Gender } = require('../models');

class ClientController {

  // Apresenta todos os resgistros do determinado Model
  async index (req, res) {
      const clients = await Client.findAll({});

      return res.status(200).json({
          status: 'ok',
          clients
      });
  }

  // Apresenta os atributos setados de um resgistro específico
  async show (req, res) {
      const client = await Client.findAll({
          attributes: ['id', 'first_name', 'last_name', 'phone', 'mobile_phone', 'email'],
          where: {
              id: req.params.id
          },
          include: [{
              model: Gender,
              attributes: [],

          }],
      });

      return res.status(200).json({
          status: 'ok',
          client
      });
  }

  // Cria e salva um resgistro do Model especificado
  async store (req, res) {
      const client = await Client.create(req.body);

      return res.status(201).json({
          status: 'created',
          client
      });
  }

  // Edita e salva um resgistro do Model especificado
  async update (req, res) {
      const { first_name, last_name, phone, mobile_phone, email } = req.body;

      const client = await Client.update({
          first_name: first_name,
          last_name: last_name,
          phone: phone,
          mobile_phone: mobile_phone,
          email: email

      }, {
          where: {
              id: req.params.id
          }
      });

      return res.status(200).json({
          status: 'updated',
          client
      });
  }

  // Exclui um resgistro do Model especificado
  async destroy (req, res) {
      await Client.destroy({
          where: {
              id: req.params.id
          }
      });

      return res.status(204).json({
          status: 'removed'
      });
  }
}

module.exports = new ClientController()

</code>```

But the following returns to me:

node ./bin/www

api:server Listening on port 3000 +0ms
(node:16328) UnhandledPromiseRejectionWarning: SequelizeEagerLoadingError: Gender is not associated to Client!
    at Function._getIncludedAssociation (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:715:13)
    at Function._validateIncludedElement (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:619:53)
    at options.include.options.include.map.include (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:516:37)
    at Array.map ()
    at Function._validateIncludedElements (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:511:39)
    at Promise.try.then.then (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:1726:14)
    at tryCatcher (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\promise.js:547:31)
    at Promise._settlePromise (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\promise.js:604:18)
    at Promise._settlePromise0 (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\promise.js:649:10)
    at Promise._settlePromises (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\promise.js:729:18)
    at _drainQueueStep (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\async.js:93:12)
    at _drainQueue (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\async.js:86:9)
    at Async._drainQueues (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\async.js:15:14)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
(node:16328) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:16328) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Obs: In n: m relationships with pivot Table, everything is normal, could someone help me?

答:
You're overwriting your first association with the second one:

```<code>//creating association Gender with Employee
Gender.associate = function (models) {
  models.Gender.hasMany(models.Employee, {
    //
  });
};

//creating association Gender with Client
Gender.associate = function (models) {
  models.Gender.belongsTo(models.Client, {
    foreignKey: 'gender_id'
  });
};
</code>```

just combine them into a single one:

```<code>//creating Gender associations
Gender.associate = function (models) {
  models.Gender.hasMany(models.Employee, {
  models.Gender.belongsTo(models.Client, {
    foreignKey: 'gender_id'
  });
};
</code>```

Body:
I have the following problem,
I have 2 Models:
Client

module.exports = (sequelize, DataTypes) => {
const Client = sequelize.define('Client', {
  first_name: DataTypes.STRING,
  last_name: DataTypes.STRING,
  phone: DataTypes.STRING,
  mobile_phone: DataTypes.STRING,
  email: DataTypes.STRING,
});

//creating association Client with Gender
Client.associate = function (models) {
  Client.hasOne(models.Gender, {
       //
  });
};

return Client; }

and Gender:

module.exports = (sequelize, DataTypes) => {
const Gender = sequelize.define('Gender', {
  name: DataTypes.STRING,
});

//creating association Gender with Employee
Gender.associate = function (models) {
  models.Gender.hasMany(models.Employee, {
    //
  });
};

//creating association Gender with Client
Gender.associate = function (models) {
  models.Gender.belongsTo(models.Client, {
    foreignKey: 'gender_id'
  });
};

return Gender; }

My ClientController:

const { Client, Gender } = require('../models');

class ClientController {

  // Apresenta todos os resgistros do determinado Model
  async index (req, res) {
      const clients = await Client.findAll({});

      return res.status(200).json({
          status: 'ok',
          clients
      });
  }

  // Apresenta os atributos setados de um resgistro específico
  async show (req, res) {
      const client = await Client.findAll({
          attributes: ['id', 'first_name', 'last_name', 'phone', 'mobile_phone', 'email'],
          where: {
              id: req.params.id
          },
          include: [{
              model: Gender,
              attributes: [],

          }],
      });

      return res.status(200).json({
          status: 'ok',
          client
      });
  }

  // Cria e salva um resgistro do Model especificado
  async store (req, res) {
      const client = await Client.create(req.body);

      return res.status(201).json({
          status: 'created',
          client
      });
  }

  // Edita e salva um resgistro do Model especificado
  async update (req, res) {
      const { first_name, last_name, phone, mobile_phone, email } = req.body;

      const client = await Client.update({
          first_name: first_name,
          last_name: last_name,
          phone: phone,
          mobile_phone: mobile_phone,
          email: email

      }, {
          where: {
              id: req.params.id
          }
      });

      return res.status(200).json({
          status: 'updated',
          client
      });
  }

  // Exclui um resgistro do Model especificado
  async destroy (req, res) {
      await Client.destroy({
          where: {
              id: req.params.id
          }
      });

      return res.status(204).json({
          status: 'removed'
      });
  }
}

module.exports = new ClientController()

But the following returns to me:

node ./bin/www

api:server Listening on port 3000 +0ms
(node:16328) UnhandledPromiseRejectionWarning: SequelizeEagerLoadingError: Gender is not associated to Client!
    at Function._getIncludedAssociation (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:715:13)
    at Function._validateIncludedElement (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:619:53)
    at options.include.options.include.map.include (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:516:37)
    at Array.map ()
    at Function._validateIncludedElements (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:511:39)
    at Promise.try.then.then (C:\SISTEMA-REACT-EXPRESS\api\node_modules\sequelize\lib\model.js:1726:14)
    at tryCatcher (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\promise.js:547:31)
    at Promise._settlePromise (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\promise.js:604:18)
    at Promise._settlePromise0 (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\promise.js:649:10)
    at Promise._settlePromises (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\promise.js:729:18)
    at _drainQueueStep (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\async.js:93:12)
    at _drainQueue (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\async.js:86:9)
    at Async._drainQueues (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\async.js:102:5)
    at Immediate.Async.drainQueues [as _onImmediate] (C:\SISTEMA-REACT-EXPRESS\api\node_modules\bluebird\js\release\async.js:15:14)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
(node:16328) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:16328) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Obs: In n: m relationships with pivot Table, everything is normal, could someone help me?

Body:
You're overwriting your first association with the second one:

//creating association Gender with Employee
Gender.associate = function (models) {
  models.Gender.hasMany(models.Employee, {
    //
  });
};

//creating association Gender with Client
Gender.associate = function (models) {
  models.Gender.belongsTo(models.Client, {
    foreignKey: 'gender_id'
  });
};

just combine them into a single one:

//creating Gender associations
Gender.associate = function (models) {
  models.Gender.hasMany(models.Employee, {
  models.Gender.belongsTo(models.Client, {
    foreignKey: 'gender_id'
  });
};


问:
Async function that returns a Promise or String - Typescript
Is it possible to return not just a Promise from a function?

```<code>const someFunc = async <T>(url: string): Promise<T> => {
  const res = await fetch(url);

  if (!res.ok) return { error: 'error text' };
  return await res.json();
}
</code>```

Depending on the condition, function above can return a promise or an object with { error: 'string' }. I'm getting an error here: TS2322: Type '{ error: any; }' is not assignable to type 'T'.   '{ error: any; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.

I've tried to use:

```<code>const someFunc = async <T>(url: string): Promise<T> | Promise<string> => {}
</code>```

This throws an error: TS1055: Type 'Promise<T> | Promise<string>' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.

Also i've tried this variant:

```<code>const someFunc = async <T>(url: string): Promise<T | { error: string }> => {}
</code>```

But when i consume this function:

```<code>interface IResponse {
  access_token: string;
  refresh_token: string;
  userRoles: string[];
  error: any;
}

const result = await someFunc<IResponse>(url);
console.log(result.access_token); // throws an error
// TS2339: Property 'access_token' does not exist on type '{ error: string; } | IResponse'.   Property 'access_token' does not exist on type '{ error: string; }'.
</code>```

UPDATE with type guard

```<code>If ('access_token' in result) {
  console.log(result.access_token);
}
</code>```

this works fine, uses access_token from IResponse interface.

答:
The return type for an async should be a Promise, but the result type of the Promise can be a union:

```<code>const someFunc = async <T>(url: string): Promise<T | { error: string}> => {
  const res = await fetch(url);

  if (!res.ok) return { error: 'error text' };
  return await res.json();
}

</code>```

Playground Link

To use the function result you will need to narrow the type of the result to see if it is an error or the actual result:

```<code>
interface IResponse {
  access_token: string;
  refresh_token: string;
  userRoles: string[];
}

async function test() {
  const result = await someFunc<IResponse>("url");
  if (!('error' in result)) {
    console.log(result.access_token);
  }
}
</code>```

Playground Link

See the section on type guards from the handbook

Body:
Is it possible to return not just a Promise from a function?

const someFunc = async <T>(url: string): Promise<T> => {
  const res = await fetch(url);

  if (!res.ok) return { error: 'error text' };
  return await res.json();
}

Depending on the condition, function above can return a promise or an object with { error: 'string' }. I'm getting an error here: TS2322: Type '{ error: any; }' is not assignable to type 'T'.   '{ error: any; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.

I've tried to use:

const someFunc = async <T>(url: string): Promise<T> | Promise<string> => {}

This throws an error: TS1055: Type 'Promise<T> | Promise<string>' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.

Also i've tried this variant:

const someFunc = async <T>(url: string): Promise<T | { error: string }> => {}

But when i consume this function:

interface IResponse {
  access_token: string;
  refresh_token: string;
  userRoles: string[];
  error: any;
}

const result = await someFunc<IResponse>(url);
console.log(result.access_token); // throws an error
// TS2339: Property 'access_token' does not exist on type '{ error: string; } | IResponse'.   Property 'access_token' does not exist on type '{ error: string; }'.

UPDATE with type guard

If ('access_token' in result) {
  console.log(result.access_token);
}

this works fine, uses access_token from IResponse interface.

Body:
The return type for an async should be a Promise, but the result type of the Promise can be a union:

const someFunc = async <T>(url: string): Promise<T | { error: string}> => {
  const res = await fetch(url);

  if (!res.ok) return { error: 'error text' };
  return await res.json();
}

Playground Link

To use the function result you will need to narrow the type of the result to see if it is an error or the actual result:

interface IResponse {
  access_token: string;
  refresh_token: string;
  userRoles: string[];
}

async function test() {
  const result = await someFunc<IResponse>("url");
  if (!('error' in result)) {
    console.log(result.access_token);
  }
}

<a href="https://www.typescriptlang.org/play/?ssl=19&ssc=2&pln=1&pc=1#code/MYewdgzgLgBBIFsCmAxArmYMC8MCGEAnpjADwAqAfABRoBOANgFxxR0CWYA5gJQsAKdROwhIKMAD4wA3jCR0hdFtA7cAvpRybpAKBgxQkWHSQQc+AO552sAGZIowABa1GPANw69MdrZjUAQhMIADoQAGseGBMoejAZOQUQJRgAcnlFGCgkAA8oVJg1T30YuMtrY1MQgCt4MGoPHTUvTmy6WzxgJBgASQAlUwAHcFEZb06uiAgAfSgIpDBlNk4uYuikW2CnWfnF1lVV7zRROj6QBlMlg4BtAF1PZp0CYixbDGAodnAs0ygGsf0hmg6wgaAYsFweCsNjgiFQ71I-SGIyQNAARPQGGjGvpfP4AtR0kk6AVOCCwVAeFFdPpASNzkgQgwQFxqMEKSEJqYZnNwgscYUmkA" rel="nofollow noreferrer">Playground Link

See the section on type guards from the handbook


问:
HttpContext.Current is null in my web service
I have a web service (.svc), and I am trying to capture the SOAP request using a piece of code found elsewhere on StackOverflow.

The problem is that HttpContext.Current is null, so I can't access Request.InputString.

Why is this null, and how can it be solved?

```<code>XmlDocument xmlSoapRequest = new XmlDocument();

Stream receiveStream = HttpContext.Current.Request.InputStream;
receiveStream.Position = 0;

using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
    xmlSoapRequest.Load(readStream);
}
</code>```

答: HttpContext.Current is null when accessed from a web service.  Use HttpRuntime.Cache instead.</p>

Body:
I have a web service (.svc), and I am trying to capture the SOAP request using a piece of code found elsewhere on StackOverflow.

The problem is that HttpContext.Current is null, so I can't access Request.InputString.

Why is this null, and how can it be solved?

XmlDocument xmlSoapRequest = new XmlDocument();

Stream receiveStream = HttpContext.Current.Request.InputStream;
receiveStream.Position = 0;

using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
    xmlSoapRequest.Load(readStream);
}


问:
Delete all elements in a list except two o three
Assuming I have this list

```<code> [['GA', 11], ['GO', 25], ['GE', 23], ['DA', 22], ['DE', 33], ['DU', 54], ['DD', 55], ['DF', 27]
</code>```

Given this example of a short list compared to a list of 500 items, how could I remove all items from this list except for GA, GO?

答:
l = [['GA', 11], ['GO', 25], ['GE', 23], ['DA', 22], ['DE', 33], ['DU', 54], ['DD', 55], ['DF', 27]]
l = [item for item in l if item[0] in ['GA', 'GO']]
```

This is a list comprehension with condition. There are plenty of tutorials, it's very useful.

Body:
Assuming I have this list

 [['GA', 11], ['GO', 25], ['GE', 23], ['DA', 22], ['DE', 33], ['DU', 54], ['DD', 55], ['DF', 27]

Given this example of a short list compared to a list of 500 items, how could I remove all items from this list except for GA, GO?

Body:
l = [['GA', 11], ['GO', 25], ['GE', 23], ['DA', 22], ['DE', 33], ['DU', 54], ['DD', 55], ['DF', 27]]
l = [item for item in l if item[0] in ['GA', 'GO']]

This is a list comprehension with condition. There are plenty of tutorials, it's very useful.


问:
Prism 4 RegionManager loses first region when second one is added in WPF
We're using Castle Windsor and Prism 4 (Feb 2010).  We're using the Windsor bootstrapper that makes Castle play nice with Prism that was released in the CompositeWPFContrib package.

I'm trying to define regions on my main Shell's XAML.  If I define one region, like so:

```<code><ContentControl prism:RegionManager.RegionName="{x:Static core:RegionNames.ToolBarRegion}"
                    DockPanel.Dock="Top"/>
</code>```

And then do the following in one of my Modules Initialize method:

```<code>_regionManager.Regions[RegionNames.ToolBarRegion].Add(typeof(SomeView));
</code>```

...life is good.

However, as soon as I add another region in the Shell's XAML:

```<code><ContentControl prism:RegionManager.RegionName="{x:Static core:RegionNames.WorkspaceRegion}"
                    DockPanel.Dock="Bottom"/>
</code>```

And then do:

```<code>_regionManager.Regions[RegionNames.WorkspaceRegion].Add(typeof(SomeOtherView));
</code>```

...I get the error: "The region manager does not contain the ToolBarRegion region."

What gives?  As soon as I comment out the second region it finds the first, when I add the second region back in it blows up, as if the RegionManager refuses to hold a collection of regions.  It should be said that this is my first foray into both Castle Windsor and Prism, so it's not out of the realm of possibility that I'm missing something painfully obvious here.  Any light that could be shed on this would be most helpful.

答:
Are you sure it's DockPanel that you are adding your controls to? Maybe your container is a content control itself (kind of control that accepts only one child)?

Also, you could try register your region manager in the bootstrapper:

```<code>RegionManager.SetRegionManager(shell, this.Container.Resolve<IRegionManager>());
</code>```

See the following questions:

Cannot find Region in RegionManager (using PRISM)

WPF, Prism v2, Region in a modal dialog, add region in code behind

EDIT

I looked at the sample solution (link in comments), and found out that your view injection code gets executed before main view is created. Your module initializers get called in StartRuntime->CreatePrismBootStrapper, and DisplayRootView (which creates your shell) is called later. Of course it can't find the region when the shell hasn't been created yet.

If all you want to register your subcontrols in module initialize code, view discovery is more suitable - it doesn't require your shell to be already created. View injection is better when you need to switch views based on user input (in this case making sure that containing control had been registered is up to you).

You have several options:

Use view discovery - as you did in the sample solution.

Create and register your Shell instance before loading your modules. base.DisplayRootView() should be able to find it in the container so It wouldn't create another. One way to do, but I'm not sure if best:

PrismBootstrapper.cs:

```<code>protected override DependencyObject CreateShell()
{
    Thor.Application.Views.ShellView view = new Thor.Application.Views.ShellView();
    _container.Register(Castle.MicroKernel.Registration.Component.For<Thor.Application.Views.ShellView>().Instance(view));
    // _container.Resolve<Thor.Application.Views.ShellView>();

    return view;
}
</code>```

.3. CreatePrismBootstrapper() after base.DisplayRootView ? It doesn't work (NullPointerException on ServiceLocator and I'm not sure if it would make sense since I'm not really familiar with libraries used by you except Prism...

Hope this helps...

Body:
We're using Castle Windsor and Prism 4 (Feb 2010).  We're using the Windsor bootstrapper that makes Castle play nice with Prism that was released in the CompositeWPFContrib package.

I'm trying to define regions on my main Shell's XAML.  If I define one region, like so:

<ContentControl prism:RegionManager.RegionName="{x:Static core:RegionNames.ToolBarRegion}"
                    DockPanel.Dock="Top"/>

And then do the following in one of my Modules Initialize method:

_regionManager.Regions[RegionNames.ToolBarRegion].Add(typeof(SomeView));

...life is good.

However, as soon as I add another region in the Shell's XAML:

<ContentControl prism:RegionManager.RegionName="{x:Static core:RegionNames.WorkspaceRegion}"
                    DockPanel.Dock="Bottom"/>

And then do:

_regionManager.Regions[RegionNames.WorkspaceRegion].Add(typeof(SomeOtherView));

...I get the error: "The region manager does not contain the ToolBarRegion region."

What gives?  As soon as I comment out the second region it finds the first, when I add the second region back in it blows up, as if the RegionManager refuses to hold a collection of regions.  It should be said that this is my first foray into both Castle Windsor and Prism, so it's not out of the realm of possibility that I'm missing something painfully obvious here.  Any light that could be shed on this would be most helpful.

Body:
Are you sure it's DockPanel that you are adding your controls to? Maybe your container is a content control itself (kind of control that accepts only one child)?

Also, you could try register your region manager in the bootstrapper:

RegionManager.SetRegionManager(shell, this.Container.Resolve<IRegionManager>());

See the following questions:

Cannot find Region in RegionManager (using PRISM)

WPF, Prism v2, Region in a modal dialog, add region in code behind

EDIT

I looked at the sample solution (link in comments), and found out that your view injection code gets executed before main view is created. Your module initializers get called in StartRuntime->CreatePrismBootStrapper, and DisplayRootView (which creates your shell) is called later. Of course it can't find the region when the shell hasn't been created yet.

If all you want to register your subcontrols in module initialize code, view discovery is more suitable - it doesn't require your shell to be already created. View injection is better when you need to switch views based on user input (in this case making sure that containing control had been registered is up to you).

You have several options:

Use view discovery - as you did in the sample solution.

Create and register your Shell instance before loading your modules. base.DisplayRootView() should be able to find it in the container so It wouldn't create another. One way to do, but I'm not sure if best:

PrismBootstrapper.cs:

protected override DependencyObject CreateShell()
{
    Thor.Application.Views.ShellView view = new Thor.Application.Views.ShellView();
    _container.Register(Castle.MicroKernel.Registration.Component.For<Thor.Application.Views.ShellView>().Instance(view));
    // _container.Resolve<Thor.Application.Views.ShellView>();

    return view;
}

.3. CreatePrismBootstrapper() after base.DisplayRootView ? It doesn't work (NullPointerException on ServiceLocator and I'm not sure if it would make sense since I'm not really familiar with libraries used by you except Prism...

Hope this helps...


问:
How to get number of used/opened connections in a MQ Connection factory configured in WebSphere Application Server
I have a java application running on WebSphere 8.5.5.12 server. I connect to other applications via MQ. I have faced a performance issue with the application and found that whenever the MQ reply is getting timeout, the Queue connection was not closed properly. I have fixed the issue. I am planning to increase the Maximum connection for the particular Queue Connection factory and i want to get the number of connection used/opened in the Queue Connection Factory via code, so that i can increase the maximum connections accordingly based on the traffic/volume. Any leads would be much helpful. </p>

答:
For your second part of your question, how do you change the maximum connections based on load.

I have a some sample code using a datasource that may help to answer you question.  Where I using name=built-in-derby-datasource, you can change the name to your Queue Connection factory name.  If you need the lookup, change this jndi name jdbc/built-in-derby-datasource to your Queue Connection factory jndi name.

The code will get the admin client giving you access to queryMBeans.  After you have the mbean, you can change maximum connections dynamically while the server is running.

```<code>@SuppressWarnings("unchecked")
public void AdminClientExample()
{
    Object adminClient = null;

    // Need to set the properties, type, host and port, defaults likely will work for most
    Properties acProps = new Properties();
    acProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
    acProps.setProperty(AdminClient.CONNECTOR_HOST, "localhost");
    acProps.setProperty(AdminClient.CONNECTOR_PORT, "8880");

    // Set if security is enabled
    //acProps.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, "true");
    //acProps.setProperty(AdminClient.USERNAME, "userid");
    //acProps.setProperty(AdminClient.PASSWORD, "userid password");

    try
    {
        adminClient = AdminClientFactory.createAdminClient(acProps);

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    ObjectInstance obi = null;
    ObjectName obn = null;
    Set<ObjectInstance> s = null;
    try {
        // The two types to use are J2CConnectionFactory and DataSource if searching through a list of mbeans of that type.
        // type=J2CConnectionFactory
        // type=DataSource
        // obn = new ObjectName("WebSphere:type=DataSource,*");
        // s1 =((AdminClient)adminClient).queryMBeans(obn, null);   // search through s1

        // You can provide the name like this,
        obn = new ObjectName("WebSphere:name=built-in-derby-datasource,*");
        s =((AdminClient)adminClient).queryMBeans(obn, null);
        // s should contain WebSphere:name=built-in-derby-datasource,process=server1,platform=dynamicproxy,node=DefaultNode01,JDBCProvider=Derby JDBC Provider (XA),diagnosticProvider=true,j2eeType=JDBCDataSource,J2EEServer=server1,Server=server1,version=9.0.0.11,type=DataSource,mbeanIdentifier=cells/DefaultCell01/resources.xml#DataSource_9007001,JDBCResource=Derby JDBC Provider (XA),cell=DefaultCell01,spec=1.0
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (s == null) {
        System.out.println("Did not find MBeans querying for object name " + obn.toString());
        return;
    } else {
        obi = s.iterator().next();
    }

    // Normally the application using the connection pool will have
    // already done the lookup which creates the objects
    // required to change maxConnections.  This lookup is only for
    // this example.
    InitialContext ctx;
    try {
        ctx = new InitialContext();
        ctx.lookup("jdbc/built-in-derby-datasource");
    } catch (Exception e) {
        e.printStackTrace();
    }

    // show the connection pool contents
    Object [] parms =  null;
    String [] parmsTypes = null;
    try {
        String ss = (String) ((AdminClient)adminClient).invoke(obi.getObjectName(), "showPoolContents", parms, parmsTypes);
        System.out.println(ss);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // get maxConnections
    try {
        Object maxConnections = ((AdminClient)adminClient).getAttribute(obi.getObjectName(), "maxConnections");
        System.out.println(maxConnections);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // change the maxConnections to 11,
    try {
        Integer it = new Integer(11);
        Attribute at = new Attribute("maxConnections", it);
        ((AdminClient)adminClient).setAttribute(obi.getObjectName(), at );
    } catch (Exception e) {
        e.printStackTrace();
    }

    // show the connection pool contents, maxConnection now should be 11.
    // or you can use the get maxConnection to check
    // the changed value.
    try {
        String ss = (String) ((AdminClient)adminClient).invoke(obi.getObjectName(), "showPoolContents", parms, parmsTypes);
        System.out.println(ss);
        // or
        Object maxConnections = ((AdminClient)adminClient).getAttribute(obi.getObjectName(), "maxConnections");
        System.out.println(maxConnections);
    } catch (Exception e) {
        e.printStackTrace();
    }

}
</code>```

Body:
For your second part of your question, how do you change the maximum connections based on load.

I have a some sample code using a datasource that may help to answer you question.  Where I using name=built-in-derby-datasource, you can change the name to your Queue Connection factory name.  If you need the lookup, change this jndi name jdbc/built-in-derby-datasource to your Queue Connection factory jndi name.

The code will get the admin client giving you access to queryMBeans.  After you have the mbean, you can change maximum connections dynamically while the server is running.

@SuppressWarnings("unchecked")
public void AdminClientExample()
{
    Object adminClient = null;

    // Need to set the properties, type, host and port, defaults likely will work for most
    Properties acProps = new Properties();
    acProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
    acProps.setProperty(AdminClient.CONNECTOR_HOST, "localhost");
    acProps.setProperty(AdminClient.CONNECTOR_PORT, "8880");

    // Set if security is enabled
    //acProps.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, "true");
    //acProps.setProperty(AdminClient.USERNAME, "userid");
    //acProps.setProperty(AdminClient.PASSWORD, "userid password");

    try
    {
        adminClient = AdminClientFactory.createAdminClient(acProps);

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    ObjectInstance obi = null;
    ObjectName obn = null;
    Set<ObjectInstance> s = null;
    try {
        // The two types to use are J2CConnectionFactory and DataSource if searching through a list of mbeans of that type.
        // type=J2CConnectionFactory
        // type=DataSource
        // obn = new ObjectName("WebSphere:type=DataSource,*");
        // s1 =((AdminClient)adminClient).queryMBeans(obn, null);   // search through s1

        // You can provide the name like this,
        obn = new ObjectName("WebSphere:name=built-in-derby-datasource,*");
        s =((AdminClient)adminClient).queryMBeans(obn, null);
        // s should contain WebSphere:name=built-in-derby-datasource,process=server1,platform=dynamicproxy,node=DefaultNode01,JDBCProvider=Derby JDBC Provider (XA),diagnosticProvider=true,j2eeType=JDBCDataSource,J2EEServer=server1,Server=server1,version=9.0.0.11,type=DataSource,mbeanIdentifier=cells/DefaultCell01/resources.xml#DataSource_9007001,JDBCResource=Derby JDBC Provider (XA),cell=DefaultCell01,spec=1.0
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (s == null) {
        System.out.println("Did not find MBeans querying for object name " + obn.toString());
        return;
    } else {
        obi = s.iterator().next();
    }

    // Normally the application using the connection pool will have
    // already done the lookup which creates the objects
    // required to change maxConnections.  This lookup is only for
    // this example.
    InitialContext ctx;
    try {
        ctx = new InitialContext();
        ctx.lookup("jdbc/built-in-derby-datasource");
    } catch (Exception e) {
        e.printStackTrace();
    }

    // show the connection pool contents
    Object [] parms =  null;
    String [] parmsTypes = null;
    try {
        String ss = (String) ((AdminClient)adminClient).invoke(obi.getObjectName(), "showPoolContents", parms, parmsTypes);
        System.out.println(ss);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // get maxConnections
    try {
        Object maxConnections = ((AdminClient)adminClient).getAttribute(obi.getObjectName(), "maxConnections");
        System.out.println(maxConnections);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // change the maxConnections to 11,
    try {
        Integer it = new Integer(11);
        Attribute at = new Attribute("maxConnections", it);
        ((AdminClient)adminClient).setAttribute(obi.getObjectName(), at );
    } catch (Exception e) {
        e.printStackTrace();
    }

    // show the connection pool contents, maxConnection now should be 11.
    // or you can use the get maxConnection to check
    // the changed value.
    try {
        String ss = (String) ((AdminClient)adminClient).invoke(obi.getObjectName(), "showPoolContents", parms, parmsTypes);
        System.out.println(ss);
        // or
        Object maxConnections = ((AdminClient)adminClient).getAttribute(obi.getObjectName(), "maxConnections");
        System.out.println(maxConnections);
    } catch (Exception e) {
        e.printStackTrace();
    }

}


问:
Error in method to visualize missing values
I am trying to visualize missing values (NA) in a data.frame containing the Hepatitis dataset and using the package VIM. I am trying to do that by using the function spineMiss:

```<code>spineMiss(hepatitis[, c("PRONOSTICO", "PROTIME")])
</code>```

But I get the following error:

Error in createPlot(main, sub, xlab, ylab, labels): (list) object
  cannot be coerced to type 'double'

This the str of the dataframe:

If I use the function like this:

```<code>a <- hepatitis$PRONOSTICO
b <- hepatitis$PROTIME
spineMiss(c(a,b))
</code>```

I do not get any error, but the result does not make much sense. What I am doing wrong?

答:
The data in the documentation is working correctly

```<code>library(VIM)
data(tao, package = "VIM")
## for missing values
spineMiss(tao[, c("Air.Temp", "Humidity")])
</code>```

and both the columns are numeric

```<code>str(tao[, c("Air.Temp", "Humidity")])
#'data.frame':  736 obs. of  2 variables:
# $ Air.Temp: num  27.1 27 27 26.9 26.8 ...
# $ Humidity: num  79.6 75.8 76.5 76.2 76.4 76.7 76.5 78.3 78.6 76.9 ...
</code>```

As one of the column is factor, it could be used as the first variable.  It is not giving any errors

```<code>set.seed(24)
hepatitis <- data.frame(PROTIME = sample(c(NA, 80:95), 100,
  replace = TRUE), PRONOSTICO = sample(c("FALLECE", "VIVE"),
 100, replace = TRUE))

spineMiss(hepatitis[c("PRONOSTICO", "PROTIME")])
</code>```

Body:
I am trying to visualize missing values (NA) in a data.frame containing the Hepatitis dataset and using the package VIM. I am trying to do that by using the function spineMiss:

spineMiss(hepatitis[, c("PRONOSTICO", "PROTIME")])

But I get the following error:

Error in createPlot(main, sub, xlab, ylab, labels): (list) object
  cannot be coerced to type 'double'

This the str of the dataframe:

If I use the function like this:

a <- hepatitis$PRONOSTICO
b <- hepatitis$PROTIME
spineMiss(c(a,b))

I do not get any error, but the result does not make much sense. What I am doing wrong?

Body:
The data in the documentation is working correctly

library(VIM)
data(tao, package = "VIM")
## for missing values
spineMiss(tao[, c("Air.Temp", "Humidity")])

and both the columns are numeric

str(tao[, c("Air.Temp", "Humidity")])
#'data.frame':  736 obs. of  2 variables:
# $ Air.Temp: num  27.1 27 27 26.9 26.8 ...
# $ Humidity: num  79.6 75.8 76.5 76.2 76.4 76.7 76.5 78.3 78.6 76.9 ...

As one of the column is factor, it could be used as the first variable.  It is not giving any errors

set.seed(24)
hepatitis <- data.frame(PROTIME = sample(c(NA, 80:95), 100,
  replace = TRUE), PRONOSTICO = sample(c("FALLECE", "VIVE"),
 100, replace = TRUE))

spineMiss(hepatitis[c("PRONOSTICO", "PROTIME")])


问:
Upload JSON file to WebAPI
I'm trying to upload a JSON file to a .Net Core 3 WebApp using Powershell Core, but can't get it to work. The 'file' is always null.

Below the code from the WebApp:

```<code>[HttpPost]
public async Task<ActionResult> UploadFile(IFormFile file)
{
    if (!ModelState.IsValid)
        return BadRequest();

        var path = Path.GetTempFileName();
        var stream = new FileStream(path, FileMode.Create);
        await file.CopyToAsync(stream);
        string FileName = file.FileName;

        // .. parsing the file

        return Ok();
}
</code>```

And the actual request from Powershell:

```<code>$file = '/fileToUpload.json'
Invoke-RestMethod -Uri 'https://localhost:5001/uploadfile' -Method Post -InFile $file -ContentType 'application/json'
</code>```

But this request always returns the following error:

```<code>Invoke-RestMethod : System.NullReferenceException: Object reference not set to an instance of an object.
</code>```

And when setting a breakpoint in the method, I can see that the 'file' is always null.

Initially I started with a method that accepts JSON data in the body, but there seems to be some sort of limitation in the body of a Post request when using Invoke-RestMethod, so I had to come up with another solution to work around this 'limit'.
I am not running into a limit issue when using Postman though, so I guess it's related to a Powershell. In case anyone knows how to solve this 'body limit' issue, that would be even greater as I prefer posting the JSON data in the body instead of posting a file.

Hope someone can help me out.
Thanks in advance.

答:
You shouldn't use -ContentType 'application/json for uploading files.
See Example 4: Simplified Multipart/Form-Data Submission

Title: Upload JSON file to WebAPI

Body:
I'm trying to upload a JSON file to a .Net Core 3 WebApp using Powershell Core, but can't get it to work. The 'file' is always null.

Below the code from the WebApp:

[HttpPost]
public async Task<ActionResult> UploadFile(IFormFile file)
{
    if (!ModelState.IsValid)
        return BadRequest();

        var path = Path.GetTempFileName();
        var stream = new FileStream(path, FileMode.Create);
        await file.CopyToAsync(stream);
        string FileName = file.FileName;

        // .. parsing the file

        return Ok();
}

And the actual request from Powershell:

$file = '/fileToUpload.json'
Invoke-RestMethod -Uri 'https://localhost:5001/uploadfile' -Method Post -InFile $file -ContentType 'application/json'

But this request always returns the following error:

Invoke-RestMethod : System.NullReferenceException: Object reference not set to an instance of an object.

And when setting a breakpoint in the method, I can see that the 'file' is always null.

Initially I started with a method that accepts JSON data in the body, but there seems to be some sort of limitation in the body of a Post request when using Invoke-RestMethod, so I had to come up with another solution to work around this 'limit'.
I am not running into a limit issue when using Postman though, so I guess it's related to a Powershell. In case anyone knows how to solve this 'body limit' issue, that would be even greater as I prefer posting the JSON data in the body instead of posting a file.

Hope someone can help me out.
Thanks in advance.


问:
How to hide SQL scripts from Liquibase in the console?
In my Spring boot project, I am using Liquibase as version control for my database. Each time when I'm running my project I see the body of all my scripts in the console (Intellij IDEA or Terminal).

Is there any possibility to remove these scripts from console logs? It could be done via properties or bean.

I tried to set

```<code>spring.jpa.show-sql=false
</code>```

but I didn't work obviously. This property is not related to Liquibase. I also checked the documentation https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#data-migration-properties but I didn't find anything helpful.

答:
Since Spring-Boot 1.2.4 you can disable logging with properties (Github issue) :

```<code>logging.level.liquibase=off
</code>```

If you just want to remove the SQL part and still see something like Table MY_TABLE created., then just remove the executor :

```<code>logging.level.liquibase.executor=off
</code>```

Body:
In my Spring boot project, I am using Liquibase as version control for my database. Each time when I'm running my project I see the body of all my scripts in the console (Intellij IDEA or Terminal).

Is there any possibility to remove these scripts from console logs? It could be done via properties or bean.

I tried to set

spring.jpa.show-sql=false

but I didn't work obviously. This property is not related to Liquibase. I also checked the documentation https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#data-migration-properties but I didn't find anything helpful.

Body:
Since Spring-Boot 1.2.4 you can disable logging with properties (Github issue) :

logging.level.liquibase=off

If you just want to remove the SQL part and still see something like Table MY_TABLE created., then just remove the executor :

logging.level.liquibase.executor=off


问:
project can't be opened, visual studio crashing
Last thing I did was add a timer in a user control which updates form color. Now every time I open the project, it loads it up and then says 'Visual Studio Stopped Working'. </p>

<p>I noticed that Visual Studio 'runs' the timer while in designer mode too, so I thought that might be causing the problem so I removed the timer from designer.cs and then renamed all the files Visual Studio was opening, but still it crashes. All the other projects work fine. This is the 4th time that it has happened to me. I'm using Visual Studio 2012 Ultimate. Any suggestions please? </p>

答: I installed the Newtonsoft.Json Fixer extension, which worked for me. I had attached another VS to see that the missing file for me was with Newtonsoft.Json, which wasn't referenced by my project, and when I found this extension I thought I'd give it a go.

Body:
Last thing I did was add a timer in a user control which updates form color. Now every time I open the project, it loads it up and then says 'Visual Studio Stopped Working'.

I noticed that Visual Studio 'runs' the timer while in designer mode too, so I thought that might be causing the problem so I removed the timer from designer.cs and then renamed all the files Visual Studio was opening, but still it crashes. All the other projects work fine. This is the 4th time that it has happened to me. I'm using Visual Studio 2012 Ultimate. Any suggestions please?


问:
Exporting calculation of all calculated columns
I inherited a couple of reports with several calculated columns. For understanding and documentation I'd need to export all the calculations behind those columns.

Searching the web I couldn't find anything useful.

So far the process is to open properties of each column and manually copy the formula.

Is there something more efficient?

答: Another option is via data table properties in the menu bar. Each data table has a "source information" tab that has a step by step recap of the ETL for that table, including joins, transformations, & calculated columns.</p>


Title: Delete all elements in a list except two o three

Body:
data = [['GA', 11], ['GO', 25], ['GE', 23], ['DA', 22], ['DE', 33], ['DU', 54], ['DD', 55], ['DF', 27]]

result = [x for x in data if x[0] == 'GA' or x[0] == 'GO']

Output:

[['GA', 11], ['GO', 25]]


问:
CORS error when serving angular with express
I have 2 server.js files. One of them is inside the backend folder where when I run nodemon server.js  on localhost:3000. And I'll start angular with ng serve inside the angular folder and login to my app no problem. Everything works. No CORS issue.

BUT

If I try to run my server.js file in the root directory where express serves my angular build folder. It starts up on localhost:8080, but when I try to login to my app, I get

inside my app.js file I have the following, which should be relevant.  Another thing worth noting
 is I made a docker version and that version gives me the CORS error too. So it seems to only work when running locally with ng serve and nodemon server.js

```<code>app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  );
  res.header(
    "Access-Control-Allow-Methods",
    "GET, POST, PATCH, PUT, DELETE, OPTIONS"
  );

  next();
});

</code>```

server.js file in backend folder (localhost:3000 No CORS error)

```<code>const app = require("./app");
const debug = require("debug")("node-angular");
const http = require("http");
const mongoose = require("mongoose");
var redis = require("redis");

var env = require("dotenv").config();

const normalizePort = val => {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    e;
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
};

const onError = error => {
  if (error.syscall !== "listen") {
    throw error;
  }
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  switch (error.code) {
    case "EACCES":
      console.error(bind + " requires elevated privileges");
      process.exit(1);
      break;
    case "EADDRINUSE":
      console.error(bind + " is already in use");
      process.exit(1);
      break;
    default:
      throw error;
  }
};
console.log("process.env.COSMODDB_USER");
console.log(env.COSMODDB_USER);
mongoose
  .connect(
    "mongodb://" +
      process.env.COSMOSDB_HOST +
      ":" +
      process.env.COSMOSDB_PORT +
      "/" +
      process.env.COSMOSDB_DBNAME +
      "?ssl=true&replicaSet=globaldb",
    {
      auth: {
        user: process.env.COSMODDB_USER,
        password: process.env.COSMOSDB_PASSWORD
      }
    }
  )
  .then(() => console.log("Connection to CosmosDB successful"))
  .catch(err => console.error(err));

const onListening = () => {
  const addr = server.address();
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  debug("Listening on " + bind);
};

const port = normalizePort(process.env.PORT || "3000");
app.set("port", process.env.PORT || port);

var server = app.listen(app.get("port"), function() {
  debug("Express server listening on port " + server.address().port);
});
</code>```

server.js in root directory (localhost:8080 CORS error)

```<code>var express    = require('express');
var app        = express();
var bodyParser = require('body-parser');
var morgan     = require('morgan');
const userRoutes = require("./app/routes/user");
const appRoutes = require("./app/app");

const cors = require("cors");
app.use(morgan('dev'));

app.use(cors({ origin: 'http://localhost:8080' , credentials :  true}));var mongoose   = require('mongoose');
var path       = require('path');
var config = 'mongodb://azure45:azure45@ds133321.mlab.com:33321/azure_chat'
//const app = require("./app");
const debug = require("debug")("node-angular");
var env = require("dotenv").config();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
    next();
});

mongoose
  .connect(
    "mongodb://" +
      process.env.COSMOSDB_HOST +
      ":" +
      process.env.COSMOSDB_PORT +
      "/" +
      process.env.COSMOSDB_DBNAME +
      "?ssl=true&replicaSet=globaldb",
    {
      auth: {
        user: process.env.COSMODDB_USER,
        password: process.env.COSMOSDB_PASSWORD
      }
    }
  )
  .then(() => console.log("Connection to CosmosDB successful"))
  .catch(err => console.error(err));

app.use(express.static(__dirname + '/public/dist/glass/'));

//var apiRoutes = require('./app/app')(app, express);
app.use("/api/", appRoutes);
app.use("/api/user", userRoutes);

app.get('*', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/dist/glass/index.html'));
});

app.listen(8080);
console.log('Magic happens on port ' + 8080);

const normalizePort = val => {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    e;
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
};

const onError = error => {
  if (error.syscall !== "listen") {
    throw error;
  }
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  switch (error.code) {
    case "EACCES":
      console.error(bind + " requires elevated privileges");
      process.exit(1);
      break;
    case "EADDRINUSE":
      console.error(bind + " is already in use");
      process.exit(1);
      break;
    default:
      throw error;
  }
};
console.log("process.env.COSMODDB_USER");
console.log(env.COSMODDB_USER);

const onListening = () => {
  const addr = server.address();
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  debug("Listening on " + bind);
};

</code>```

Here's the project file structure as a reference. app.js is where all my APIs are.

答:
Try to use Node.js CORS library for CORS.

Something like below.

```<code>const express = require("express");

const cors = require("cors");

const app = express();

app.use(cors());
</code>```

Body:
I have 2 server.js files. One of them is inside the backend folder where when I run nodemon server.js  on localhost:3000. And I'll start angular with ng serve inside the angular folder and login to my app no problem. Everything works. No CORS issue.

BUT

If I try to run my server.js file in the root directory where express serves my angular build folder. It starts up on localhost:8080, but when I try to login to my app, I get

inside my app.js file I have the following, which should be relevant.  Another thing worth noting
 is I made a docker version and that version gives me the CORS error too. So it seems to only work when running locally with ng serve and nodemon server.js

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  );
  res.header(
    "Access-Control-Allow-Methods",
    "GET, POST, PATCH, PUT, DELETE, OPTIONS"
  );

  next();
});

server.js file in backend folder (localhost:3000 No CORS error)

const app = require("./app");
const debug = require("debug")("node-angular");
const http = require("http");
const mongoose = require("mongoose");
var redis = require("redis");

var env = require("dotenv").config();

const normalizePort = val => {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    e;
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
};

const onError = error => {
  if (error.syscall !== "listen") {
    throw error;
  }
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  switch (error.code) {
    case "EACCES":
      console.error(bind + " requires elevated privileges");
      process.exit(1);
      break;
    case "EADDRINUSE":
      console.error(bind + " is already in use");
      process.exit(1);
      break;
    default:
      throw error;
  }
};
console.log("process.env.COSMODDB_USER");
console.log(env.COSMODDB_USER);
mongoose
  .connect(
    "mongodb://" +
      process.env.COSMOSDB_HOST +
      ":" +
      process.env.COSMOSDB_PORT +
      "/" +
      process.env.COSMOSDB_DBNAME +
      "?ssl=true&replicaSet=globaldb",
    {
      auth: {
        user: process.env.COSMODDB_USER,
        password: process.env.COSMOSDB_PASSWORD
      }
    }
  )
  .then(() => console.log("Connection to CosmosDB successful"))
  .catch(err => console.error(err));

const onListening = () => {
  const addr = server.address();
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  debug("Listening on " + bind);
};

const port = normalizePort(process.env.PORT || "3000");
app.set("port", process.env.PORT || port);

var server = app.listen(app.get("port"), function() {
  debug("Express server listening on port " + server.address().port);
});

server.js in root directory (localhost:8080 CORS error)

var express    = require('express');
var app        = express();
var bodyParser = require('body-parser');
var morgan     = require('morgan');
const userRoutes = require("./app/routes/user");
const appRoutes = require("./app/app");

const cors = require("cors");
app.use(morgan('dev'));

app.use(cors({ origin: 'http://localhost:8080' , credentials :  true}));var mongoose   = require('mongoose');
var path       = require('path');
var config = 'mongodb://azure45:azure45@ds133321.mlab.com:33321/azure_chat'
//const app = require("./app");
const debug = require("debug")("node-angular");
var env = require("dotenv").config();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
    next();
});

mongoose
  .connect(
    "mongodb://" +
      process.env.COSMOSDB_HOST +
      ":" +
      process.env.COSMOSDB_PORT +
      "/" +
      process.env.COSMOSDB_DBNAME +
      "?ssl=true&replicaSet=globaldb",
    {
      auth: {
        user: process.env.COSMODDB_USER,
        password: process.env.COSMOSDB_PASSWORD
      }
    }
  )
  .then(() => console.log("Connection to CosmosDB successful"))
  .catch(err => console.error(err));

app.use(express.static(__dirname + '/public/dist/glass/'));

//var apiRoutes = require('./app/app')(app, express);
app.use("/api/", appRoutes);
app.use("/api/user", userRoutes);

app.get('*', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/dist/glass/index.html'));
});

app.listen(8080);
console.log('Magic happens on port ' + 8080);

const normalizePort = val => {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    e;
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
};

const onError = error => {
  if (error.syscall !== "listen") {
    throw error;
  }
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  switch (error.code) {
    case "EACCES":
      console.error(bind + " requires elevated privileges");
      process.exit(1);
      break;
    case "EADDRINUSE":
      console.error(bind + " is already in use");
      process.exit(1);
      break;
    default:
      throw error;
  }
};
console.log("process.env.COSMODDB_USER");
console.log(env.COSMODDB_USER);

const onListening = () => {
  const addr = server.address();
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  debug("Listening on " + bind);
};

Here's the project file structure as a reference. app.js is where all my APIs are.

Body:
Try to use Node.js CORS library for CORS.

Something like below.

const express = require("express");

const cors = require("cors");

const app = express();

app.use(cors());


问:
Automapper with base class and different configuration options for implementations
I have two classes (MVC view model) which inherits from one abstract base class.

```<code>abstract class BaseModel { }

class Car : BaseModel
{
    public string Speed { get; set; }
}

class Camper : BaseModel
{
    public int Beds { get; set; }
}
</code>```

and want to configure AutoMapper with base class, something like:

```<code>Mapper.CreateMap<BaseModel, DataDestination>();

var someObj = new DataDastination();
Mapper.Map(instanceOfBaseModel, someObj);
</code>```

Here I get error, because Automapper doesn't have configuration of Car or Camper. Tried configuring Automapper with something like this:

```<code>Mapper.CreateMap<BaseModel, DataDestination>()
    .ForMember(dest => dest.SomeProp, mapper => mapper.MapFrom( .... ));
</code>```

In MapFrom, I only see properties from base class! How to configure Automapper to use BaseClass, and specific ForMember expression for Car and Camper? For example, if it's a Car, map this property from this, and if it's a Camper, map this property from somewhere else.

答:
Use .IncludeAllDerived()</p>

```<code>Mapper.CreateMap<BaseModel, DataDestination>().IncludeAllDerived()
Mapper.CreateMap<Car, DataDestination>();
Mapper.CreateMap<Camper, DataDestination>();
</code>```

Body:
I have two classes (MVC view model) which inherits from one abstract base class.

abstract class BaseModel { }

class Car : BaseModel
{
    public string Speed { get; set; }
}

class Camper : BaseModel
{
    public int Beds { get; set; }
}

and want to configure AutoMapper with base class, something like:

Mapper.CreateMap<BaseModel, DataDestination>();

var someObj = new DataDastination();
Mapper.Map(instanceOfBaseModel, someObj);

Here I get error, because Automapper doesn't have configuration of Car or Camper. Tried configuring Automapper with something like this:

Mapper.CreateMap<BaseModel, DataDestination>()
    .ForMember(dest => dest.SomeProp, mapper => mapper.MapFrom( .... ));

In MapFrom, I only see properties from base class! How to configure Automapper to use BaseClass, and specific ForMember expression for Car and Camper? For example, if it's a Car, map this property from this, and if it's a Camper, map this property from somewhere else.

Body:
Use .IncludeAllDerived()

Mapper.CreateMap<BaseModel, DataDestination>().IncludeAllDerived()
Mapper.CreateMap<Car, DataDestination>();
Mapper.CreateMap<Camper, DataDestination>();