问:
Compare two values and add them to list if they match - code improvement
I am working with medical history data and need to figure out to find the series of diagnoses a patient has had.
I have a large database which includes data on unique ID, diagnosis, time of contact with healthcare, and so on.

I made some dummy data here to illustrate:

```<code>import pandas as pd
import numpy as np

columns = ["ID","DIAG","TYPE","IN","OUT","GENDER","DOB"]
diags = pd.DataFrame(np.random.randint(0,100,size=(2000,7)),columns=columns)
diags_counter = diags.groupby("ID")["DIAG"].count().to_frame()
diags_counter.reset_index(level=0,inplace=True)
</code>```

I reset the index, since the IDs in the database are more complex, and using .loc wouldn't work.

My idea was to make a list or dictionary of a list of dataframes for each patient, i.e. a patient might have only one diagnosis, while another might have 2 or more.
Following code is working, but extremely slow, and since I have over half a million patients, this is not going to work:

```<code>diags_seq = []
for i in range(0,len(diags_counter)):
  X= []
  for j in range(0,len(diags)):
    if diags_counter.ID.iloc[i] == diags.ID.iloc[j]:
      X.append(diags.iloc[j])
  diags_seq.append(X)
  print(f"\r{i+1} of {len(diags_counter)} found", end="")
</code>```

Any help to how to approach this otherwise would be greatly appreciated :)

答:
So after some tweaking, I ended up with using the following:

```<code>dict_of_specifics_id = {}
for id in unique_id:
   dict_of_specifics_id[id] = {}
   dict_of_specifics_id[id]['diag_list'] = []
for index, row in diags.iterrows():
   dict_of_specifics_id[row.ID]['diag_list'].append(row)

seq_diags_list = list(dict_of_specifics_id.values())

seq_diags = []
for i in range(0,len(seq_diags_list):
   X = list(seq_diags_list[i].values())
   df = pd.concat(X[0],axis=1).T
   seq_diags.append(df)
</code>```

This ends up giving a list of dataframes, where the length of each dataframe is represented in the number of diagnoses associated with each patient/id.

PS: It takes around 10 seconds to run for around 600,000 data points, so much better than before.

Body:
I am working with medical history data and need to figure out to find the series of diagnoses a patient has had.
I have a large database which includes data on unique ID, diagnosis, time of contact with healthcare, and so on.

I made some dummy data here to illustrate:

import pandas as pd
import numpy as np

columns = ["ID","DIAG","TYPE","IN","OUT","GENDER","DOB"]
diags = pd.DataFrame(np.random.randint(0,100,size=(2000,7)),columns=columns)
diags_counter = diags.groupby("ID")["DIAG"].count().to_frame()
diags_counter.reset_index(level=0,inplace=True)

I reset the index, since the IDs in the database are more complex, and using .loc wouldn't work.

My idea was to make a list or dictionary of a list of dataframes for each patient, i.e. a patient might have only one diagnosis, while another might have 2 or more.
Following code is working, but extremely slow, and since I have over half a million patients, this is not going to work:

diags_seq = []
for i in range(0,len(diags_counter)):
  X= []
  for j in range(0,len(diags)):
    if diags_counter.ID.iloc[i] == diags.ID.iloc[j]:
      X.append(diags.iloc[j])
  diags_seq.append(X)
  print(f"\r{i+1} of {len(diags_counter)} found", end="")

Any help to how to approach this otherwise would be greatly appreciated :)

Body:
So after some tweaking, I ended up with using the following:

dict_of_specifics_id = {}
for id in unique_id:
   dict_of_specifics_id[id] = {}
   dict_of_specifics_id[id]['diag_list'] = []
for index, row in diags.iterrows():
   dict_of_specifics_id[row.ID]['diag_list'].append(row)

seq_diags_list = list(dict_of_specifics_id.values())

seq_diags = []
for i in range(0,len(seq_diags_list):
   X = list(seq_diags_list[i].values())
   df = pd.concat(X[0],axis=1).T
   seq_diags.append(df)

This ends up giving a list of dataframes, where the length of each dataframe is represented in the number of diagnoses associated with each patient/id.

PS: It takes around 10 seconds to run for around 600,000 data points, so much better than before.


来源: stackexchange

问:
How to query rows grouped by matching string column but only count the most recent row for a specific set of keywords?
Have a single table of email events where each row is keyed with a specific outgoing email record fk and a specific recipient user fk. At any given time and in no specific order and even simultaneously from different threads I can have new records dropping into this table. Here are the relevant columns...

```<code>id (pk), email_id (fk), user_id (fk), event (string/name), created_at
</code>```

I am calculating the overall event counts for a given email, like how many emails were delivered, how many bounced, etc. However I need to ignore specific combinations of email events for a specific user because they become outdated when a newer event comes in. For example if a row says the email was 'deferred' for a specific user but then later a new event row is inserted that says 'delivered' or 'bounced' then I only want the most recently added row of any of those related keywords to be counted once as the current state.

What's a good way to do this at read time? I am having trouble because of the multiple layers of grouping that I need to do and reaching the limits of my SQL chops, here is the query I am trying to enhance as described:

```<code>select `event`, COUNT(1) as count, COUNT(DISTINCT user_id) as unique_count
from `email_activity`
where `email_id` = 7518
group by `event`
</code>```

For most of the events I want them all counted without any replacement so grouping by only event is fine in those cases, eg if something was a 'click' or 'open' event just total them up.

However, if there are any number of 'deferred', 'bounced', or 'delivered' events for the same email_id/user_id I only want to count the one with the most recent created_at date and ignore all the older ones.

Example row set (email_id, event, user_id, created_at):

```<code>7518, "click", 25, 1-20-2021
7518, "click", 73, 1-5-2021
7518, "bounced", 45, 1-19-2021
7518, "deferred", 45, 1-17-2021
7518, "delivered", 19, 1-1-2021
7518, "delivered", 25, 1-1-2021
7518, "delivered", 73, 1-1-2021
</code>```

So the queried count for email 7518 would be:

2 "click", 3 "delivered", and 1 "bounced" as the "deferred" row would be ignored for user 45 since it is older (only "bounced", "deferred", and "delivered" events are part of this "only count the latest" rule, all other event names are always counted).

答:
Considering you are passing email_id as a parameter, following query should be able to give you the intended result-

```<code>select sum(case when f.user_id is not null then 1 else 0 end) sum_event, f.event
from (select * from email_event e1
       where exists (select id from email_event e2 where e2.email_id = e1.email_id and e2.event = 'deferred')
        and e1.event <> 'deferred') f
 where f.email_id = 7518 group by f.event;
</code>```

Output when tested for email_id = 7518:

```<code>+-----------+-----------+
| sum_event |   event   |
+-----------+-----------+
|     2     | click     |
+-----------+-----------+
|     1     | bounced   |
+-----------+-----------+
|     3     | delivered |
+-----------+-----------+
</code>```

ContentLicense: CC BY-SA 4.0

Body:
Have a single table of email events where each row is keyed with a specific outgoing email record fk and a specific recipient user fk. At any given time and in no specific order and even simultaneously from different threads I can have new records dropping into this table. Here are the relevant columns...

id (pk), email_id (fk), user_id (fk), event (string/name), created_at

I am calculating the overall event counts for a given email, like how many emails were delivered, how many bounced, etc. However I need to ignore specific combinations of email events for a specific user because they become outdated when a newer event comes in. For example if a row says the email was 'deferred' for a specific user but then later a new event row is inserted that says 'delivered' or 'bounced' then I only want the most recently added row of any of those related keywords to be counted once as the current state.

What's a good way to do this at read time? I am having trouble because of the multiple layers of grouping that I need to do and reaching the limits of my SQL chops, here is the query I am trying to enhance as described:

select `event`, COUNT(1) as count, COUNT(DISTINCT user_id) as unique_count
from `email_activity`
where `email_id` = 7518
group by `event`

For most of the events I want them all counted without any replacement so grouping by only event is fine in those cases, eg if something was a 'click' or 'open' event just total them up.

However, if there are any number of 'deferred', 'bounced', or 'delivered' events for the same email_id/user_id I only want to count the one with the most recent created_at date and ignore all the older ones.

Example row set (email_id, event, user_id, created_at):

7518, "click", 25, 1-20-2021
7518, "click", 73, 1-5-2021
7518, "bounced", 45, 1-19-2021
7518, "deferred", 45, 1-17-2021
7518, "delivered", 19, 1-1-2021
7518, "delivered", 25, 1-1-2021
7518, "delivered", 73, 1-1-2021

So the queried count for email 7518 would be:

2 "click", 3 "delivered", and 1 "bounced" as the "deferred" row would be ignored for user 45 since it is older (only "bounced", "deferred", and "delivered" events are part of this "only count the latest" rule, all other event names are always counted).

Body:
Considering you are passing email_id as a parameter, following query should be able to give you the intended result-

select sum(case when f.user_id is not null then 1 else 0 end) sum_event, f.event
from (select * from email_event e1
       where exists (select id from email_event e2 where e2.email_id = e1.email_id and e2.event = 'deferred')
        and e1.event <> 'deferred') f
 where f.email_id = 7518 group by f.event;

Output when tested for email_id = 7518:

+-----------+-----------+
| sum_event |   event   |
+-----------+-----------+
|     2     | click     |
+-----------+-----------+
|     1     | bounced   |
+-----------+-----------+
|     3     | delivered |
+-----------+-----------+


问:
Why are my filenames getting struck out when I switch to a new branch?
Filename getting struck out when I switch to a new branch:

I have also tried switch different branches, but still no use.

答: The reason they are "struck out" is that the branch you have moved to doesn't have those files. Even though they don't exist you still have them open on the IDE so it is telling you that they don't actually exist on this branch. If you close them you won't be able to open them again on this branch.</p>


问:
Save Custom Timestamp with TypeORM
I want to save my timestamp in following format with TypeORM on NestJS

Date formats should be YYYY-MM-DDTHH:MM:SS+01:00 or YYYY-MM-DDTHH:MM:SS.sss+01:00

答: If you use new Date() you will get the same format you mention above. You can also use the package moment,js.

Title: Save Custom Timestamp with TypeORM


问:
Sorting heterogeneous elements in arraylist
How to sort heterogeneous elements in a array-list? For example,

```<code>ArrayList l1 = new ArrayList();
l1.add(1);
l1.add("hi");
l1.add(1.0f);
</code>```

Now how to sort this array-list?``

答:
Define computable sorting rules. Then implement those rules in a class that implements Comparator and use

```<code>Collections.sort(l1, myComparator);  // myComparator is an instance of the class
                                     // you just implemented
</code>```

ContentLicense: CC BY-SA 3.0

Body:
How to sort heterogeneous elements in a array-list? For example,

ArrayList l1 = new ArrayList();
l1.add(1);
l1.add("hi");
l1.add(1.0f);

Now how to sort this array-list?``

Body:
Define computable sorting rules. Then implement those rules in a class that implements Comparator and use

Collections.sort(l1, myComparator);  // myComparator is an instance of the class
                                     // you just implemented


问:
How to throttle events in bloc?
How to throttle events in bloc? Let's say, I want to trigger file save on user input, but perform file system access not more often than once per 100ms?

Example bloc event handler:

on<StoreFile>(_handleStoreEvent);
```

答:
Each handler have an optional transformer: field which can do throttling (and much more).

Using rxdart you can implement throttling yourself:

on<StoreFile>(
  _handleStoreEvent,
  transformer: (events, mapper) => events.throttleTime(Duration(milliseconds: 100)).switchMap(mapper),
);
```
<p>I wrote the bloc_event_transformers package to do popular transforms like throttle and debounce to reduce the boilerplate in my apps. It can be used like that:</p>
<pre class="lang-dart prettyprint-override"><code>on<StoreFile>(
  _handleStoreEvent,
  transformer: throttle(Duration(milliseconds: 100)),
);
</code>```

Title: How to throttle events in bloc?

Body:
Each handler have an optional transformer: field which can do throttling (and much more).

Using rxdart you can implement throttling yourself:

on<StoreFile>(
  _handleStoreEvent,
  transformer: (events, mapper) => events.throttleTime(Duration(milliseconds: 100)).switchMap(mapper),
);

I wrote the bloc_event_transformers package to do popular transforms like throttle and debounce to reduce the boilerplate in my apps. It can be used like that:

on<StoreFile>(
  _handleStoreEvent,
  transformer: throttle(Duration(milliseconds: 100)),
);


问:
CSS Animation Wave effect with 3 layers
I'm trying to create a wave animation with 3 layers. The animation as you can see in the snippet, doesn't seem smooth, and it seems like the waves aren't endless. Besides I can't really get to see the waves structure like in the picture.

I would to see the waves without interruption, when it comes to the end of the animation, it is clearly noticeable that the animation has ended.
[![

.w1 {
  stroke: none;
  fill: #b3696c;
  opacity: 55%;
  animation: move1 4.5s linear infinite;
}
.w2 {
  stroke: none;
  fill: #b3696c;
  opacity: 35%;
  transform: translate3d(0, 0, 0);
  animation: move2 4.5s linear infinite;
}
.w3 {
  stroke: none;
  fill: #cf2127;
  opacity: 30%;
  transform: translate3d(0, 0, 0);
  animation: move3 2s linear infinite;
}
@keyframes move1 {
  0% {
    transform: translateX(-500px) scaleX(2.5);
  }
  50% {
    transform: translateX(-100) scaleX(2.5);
  }
  100% {
    transform: translateX(0) scaleX(2.5);
  }
}
@keyframes move2 {
  0% {
    transform: translateX(-600px) scaleX(3);
  }
  50% {
    transform: translateX(-100) scaleX(2.5);
  }
  100% {
    transform: translateX(0) scaleX(3);
  }
}
@keyframes move3 {
  0% {
    transform: translateX(-800px) scaleX(4);
  }
  50% {
    transform: translateX(-100) scaleX(2.5);
  }
  100% {
    transform: translateX(0) scaleX(4);
  }
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="waveAnimation">
      <svg viewBox="0 0 500 150" preserveAspectRatio="none">
        <path
          class="w1 waveTop"
          d="M-8.74,71.55 C289.78,255.11 349.60,4.47 505.36,34.05 L500.00,150.00 L0.00,150.00 Z"
        />
        <path
          class="w2 waveMiddle"
          d="M-23.42,125.83 C187.63,45.89 299.38,57.73 526.80,123.86 L500.00,150.00 L0.00,150.00 Z"
        />
        <path
          class="w3 waveBottom"
          d="M-23.42,125.83 C172.96,-152.44 217.55,183.06 504.22,55.77 L500.00,150.00 L0.00,150.00 Z"
        />
      </svg>
    </div></code>```

答:
The wave animations should move smoothly, so instead of using the linear attribute, we use ease-in-out.

.w1 {
    stroke: none;
    fill: #b3696c;
    opacity: 55%;
    animation: move1 5s ease-in-out infinite;
  }
  .w2 {
    stroke: none;
    fill: #b3696c;
    opacity: 35%;
    transform: translate3d(0, 0, 0);
    animation: move2 4s ease-in-out infinite;
  }
  .w3 {
    stroke: none;
    fill: #cf2127;
    opacity: 30%;
    transform: translate3d(0, 0, 0);
    animation: move3 6s ease-in-out infinite;
  }
  @keyframes move1 {
    0% {
      transform: translateX(-500px) scaleX(2.5);
    }
    25% {
      transform: translateX(-100) scaleX(2.5);
    }
    50% {
      transform: translateX(0) scaleX(2.5);
    }
    75% {
      transform: translateX(-100) scaleX(2.5);
    }
    100% {
      transform: translateX(-500px) scaleX(2.5);
    }
  }
  @keyframes move2 {
    0% {
      transform: translateX(-600px) scaleX(3);
    }
    25% {
      transform: translateX(-100) scaleX(2.5);
    }
    50% {
      transform: translateX(0) scaleX(3);
    }
    75% {
      transform: translateX(-100) scaleX(2.5);
    }
    100% {
      transform: translateX(-600px) scaleX(3);
    }
  }
  @keyframes move3 {
    0% {
      transform: translateX(-800px) scaleX(3);
    }
    25% {
      transform: translateX(-100) scaleX(2.5);
    }
    50% {
      transform: translateX(0) scaleX(3);
    }
    75% {
      transform: translateX(-100) scaleX(2.5);
    }
    100% {
      transform: translateX(-800px) scaleX(3);
    }
  }```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="waveAnimation">
  <svg viewBox="0 0 500 150" preserveAspectRatio="none">
    <path
    class="w1 waveTop"
    d="M-8.74,71.55 C289.78,255.11 349.60,4.47 505.36,34.05 L500.00,150.00 L0.00,150.00 Z"
    />
    <path
    class="w2 waveMiddle"
    d="M-23.42,125.83 C187.63,45.89 299.38,57.73 526.80,123.86 L500.00,150.00 L0.00,150.00 Z"
    />
    <path
    class="w3 waveBottom"
    d="M-23.42,125.83 C172.96,-152.44 217.55,183.06 504.22,55.77 L500.00,150.00 L0.00,150.00 Z"
    />
  </svg>
</div></code>```

Title: CSS Animation Wave effect with 3 layers

Body:
I'm trying to create a wave animation with 3 layers. The animation as you can see in the snippet, doesn't seem smooth, and it seems like the waves aren't endless. Besides I can't really get to see the waves structure like in the picture.

I would to see the waves without interruption, when it comes to the end of the animation, it is clearly noticeable that the animation has ended.
[![

.w1 {
  stroke: none;
  fill: #b3696c;
  opacity: 55%;
  animation: move1 4.5s linear infinite;
}
.w2 {
  stroke: none;
  fill: #b3696c;
  opacity: 35%;
  transform: translate3d(0, 0, 0);
  animation: move2 4.5s linear infinite;
}
.w3 {
  stroke: none;
  fill: #cf2127;
  opacity: 30%;
  transform: translate3d(0, 0, 0);
  animation: move3 2s linear infinite;
}
@keyframes move1 {
  0% {
    transform: translateX(-500px) scaleX(2.5);
  }
  50% {
    transform: translateX(-100) scaleX(2.5);
  }
  100% {
    transform: translateX(0) scaleX(2.5);
  }
}
@keyframes move2 {
  0% {
    transform: translateX(-600px) scaleX(3);
  }
  50% {
    transform: translateX(-100) scaleX(2.5);
  }
  100% {
    transform: translateX(0) scaleX(3);
  }
}
@keyframes move3 {
  0% {
    transform: translateX(-800px) scaleX(4);
  }
  50% {
    transform: translateX(-100) scaleX(2.5);
  }
  100% {
    transform: translateX(0) scaleX(4);
  }
}

      <svg viewBox="0 0 500 150" preserveAspectRatio="none">
        <path
          class="w1 waveTop"
          d="M-8.74,71.55 C289.78,255.11 349.60,4.47 505.36,34.05 L500.00,150.00 L0.00,150.00 Z"
        />
        <path
          class="w2 waveMiddle"
          d="M-23.42,125.83 C187.63,45.89 299.38,57.73 526.80,123.86 L500.00,150.00 L0.00,150.00 Z"
        />
        <path
          class="w3 waveBottom"
          d="M-23.42,125.83 C172.96,-152.44 217.55,183.06 504.22,55.77 L500.00,150.00 L0.00,150.00 Z"
        />
      </svg>

Body:
The wave animations should move smoothly, so instead of using the linear attribute, we use ease-in-out.

.w1 {
    stroke: none;
    fill: #b3696c;
    opacity: 55%;
    animation: move1 5s ease-in-out infinite;
  }
  .w2 {
    stroke: none;
    fill: #b3696c;
    opacity: 35%;
    transform: translate3d(0, 0, 0);
    animation: move2 4s ease-in-out infinite;
  }
  .w3 {
    stroke: none;
    fill: #cf2127;
    opacity: 30%;
    transform: translate3d(0, 0, 0);
    animation: move3 6s ease-in-out infinite;
  }
  @keyframes move1 {
    0% {
      transform: translateX(-500px) scaleX(2.5);
    }
    25% {
      transform: translateX(-100) scaleX(2.5);
    }
    50% {
      transform: translateX(0) scaleX(2.5);
    }
    75% {
      transform: translateX(-100) scaleX(2.5);
    }
    100% {
      transform: translateX(-500px) scaleX(2.5);
    }
  }
  @keyframes move2 {
    0% {
      transform: translateX(-600px) scaleX(3);
    }
    25% {
      transform: translateX(-100) scaleX(2.5);
    }
    50% {
      transform: translateX(0) scaleX(3);
    }
    75% {
      transform: translateX(-100) scaleX(2.5);
    }
    100% {
      transform: translateX(-600px) scaleX(3);
    }
  }
  @keyframes move3 {
    0% {
      transform: translateX(-800px) scaleX(3);
    }
    25% {
      transform: translateX(-100) scaleX(2.5);
    }
    50% {
      transform: translateX(0) scaleX(3);
    }
    75% {
      transform: translateX(-100) scaleX(2.5);
    }
    100% {
      transform: translateX(-800px) scaleX(3);
    }
  }

<div class="waveAnimation">
  <svg viewBox="0 0 500 150" preserveAspectRatio="none">
    <path
    class="w1 waveTop"
    d="M-8.74,71.55 C289.78,255.11 349.60,4.47 505.36,34.05 L500.00,150.00 L0.00,150.00 Z"
    />
    <path
    class="w2 waveMiddle"
    d="M-23.42,125.83 C187.63,45.89 299.38,57.73 526.80,123.86 L500.00,150.00 L0.00,150.00 Z"
    />
    <path
    class="w3 waveBottom"
    d="M-23.42,125.83 C172.96,-152.44 217.55,183.06 504.22,55.77 L500.00,150.00 L0.00,150.00 Z"
    />
  </svg>
</div>


问:
How to get a command-line property to overwrite a maven property
I have a pom file in which version numbers of some dependencies rely on the project version property specified in the settings of the pom file. Can I overwrite this via the command-line? If so, how?

Here's the long story:

We are currently transitioning our projects to maven, but we're not all the way there yet. There are multiple modules that are still not built with maven and are therefore dependencies in our project (they are built into jars through ant). Upon release, we want all of these jars to be built and contain the same version number as the parent project. For a release, two steps are performed (until we can get everything using maven)

The jars are built in ant with the correct release version (12.12.4.0).

The maven release plugin is used to deploy the project to our artifact repository.

In the second step command-line arguments are used to specify the release:

```<code>mvn release:prepare -DreleaseVersion=12.12.4.0 -DdevelopmentVersion=12.12.4.1-SNAPSHOT -Dtag=iv-12.12.4.0
</code>```

I would like the pom file to be updated with the version specified. However, when this command is run, the version within the pom file (12.12.4.0-SNAPSHOT) is still being used. This fails the "checking dependencies and plugins for snapshots" step and I am required to resolve my jars that still have the 12.12.4.0-SNAPSHOT version used from the maven version property.

This led me to the original question of how I can override this so that the version resolves to that specified on the command line.
Additional questions that could get me past this is:
How to allow the maven release plugin to update the pom file before this check?
How to skip the snapshot check (not desirable)

I could create a property within the pom file that I can overwrite, but then I'd have to maintain the version number in two places within the pom file.

Thoughts?

答:
Here is one more option:

```<code><Logger name="com.mypackagename" level="${env:LOG_LEVEL:-error}" additivity="false">
  <AppenderRef ref="Console-Appender"/>
</Logger>
</code>```

${env:LOG_LEVEL:-error} means env = environment variable, LOG_LEVEL = env name, -error = default level error.

Body:
I have a pom file in which version numbers of some dependencies rely on the project version property specified in the settings of the pom file. Can I overwrite this via the command-line? If so, how?

Here's the long story:

We are currently transitioning our projects to maven, but we're not all the way there yet. There are multiple modules that are still not built with maven and are therefore dependencies in our project (they are built into jars through ant). Upon release, we want all of these jars to be built and contain the same version number as the parent project. For a release, two steps are performed (until we can get everything using maven)

The jars are built in ant with the correct release version (12.12.4.0).

The maven release plugin is used to deploy the project to our artifact repository.

In the second step command-line arguments are used to specify the release:

mvn release:prepare -DreleaseVersion=12.12.4.0 -DdevelopmentVersion=12.12.4.1-SNAPSHOT -Dtag=iv-12.12.4.0

I would like the pom file to be updated with the version specified. However, when this command is run, the version within the pom file (12.12.4.0-SNAPSHOT) is still being used. This fails the "checking dependencies and plugins for snapshots" step and I am required to resolve my jars that still have the 12.12.4.0-SNAPSHOT version used from the maven version property.

This led me to the original question of how I can override this so that the version resolves to that specified on the command line.
Additional questions that could get me past this is:
How to allow the maven release plugin to update the pom file before this check?
How to skip the snapshot check (not desirable)

I could create a property within the pom file that I can overwrite, but then I'd have to maintain the version number in two places within the pom file.

Thoughts?

Body:
Here is one more option:

<Logger name="com.mypackagename" level="${env:LOG_LEVEL:-error}" additivity="false">
  <AppenderRef ref="Console-Appender"/>
</Logger>

${env:LOG_LEVEL:-error} means env = environment variable, LOG_LEVEL = env name, -error = default level error.


问:
SuiteScript 2.x entry point scripts must implement one script type function
I added a field in the Sales Order named Membership that will source from the customer record. Then when that field is set to the membership level (ex. elite member) it automatically set the discount item field to a specific discount item.... I encountered a notif saying SuiteScript 2.x entry point scripts must implement one script type function how do I fix this?</p>
```<code>/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(["N/currentRecord", "N/runtime"], function(currentRecord,runtime) {

    function pageInit(context) {

     const record = currentRecord.get()  // Get value of Membership Field
     const user = runtime.getCurrentUser().name
    }

    var membership = currentRecord.getField({
        fieldId : "custentity1",
    })

    if(membership == "Elite"){
        //Setting up discount
        record.setValue({
            fieldId: "discountitem", //fieldId of the Discount Item
            value: 137 // Internal ID of the Discount Item
        })}

        else {
            record.setValue({
                fieldId: "discount item",
                value: 0
            });
        }

        return {
            pageInit : pageInit
        }
});
</code>```

答:
Because your using 2.x your global settings are going to determine if this is run as 2.0 or 2.1. I'm guessing you have it set at 2.1 because you're context is having an issue.

If you try to run 2.1 code on the client then you might run into issues. One of those issues is that the context will not be populated.  Perhaps this is why you are using currentRecord.get() instead of context.currentRecord ?

Note: Any code that is outside a function (like pageInit) will be run when the module's callback is run.

   if(membership == "Elite"){
    //Setting up discount
    record.setValue({
        fieldId: "discountitem", //fieldId of the Discount Item
        value: 137 // Internal ID of the Discount Item
    })}

    else {
        record.setValue({
            fieldId: "discount item",
            value: 0
            });
    }
```
<p>so this code is running before pageInit, so the suggestion by Sayeesh is a good one.</p>
<p>You are mixing some EcmaScipt versions.  I would get a good linter like eslint and make sure you are writing code for the target you want.</p>
<p>you got a field called <code>discount item</code> and one called <code>discountitem</code>? I'll assume that is a typo.</p>
<p>To play devil's advocate i'm going to say that you can use 2.1 if you really want to here.  In 2.1 you're code could be as simple as : (not checked)</p>
<pre class="lang-js prettyprint-override"><code>/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 */

define(['N/record','N/currentRecord'],(record,currentRecord)=>{
  return {
    pageInit({ currentRecord : { id, type } }){

        const
          so = record.load({ id, type }),
          membership = so.getValue({ fieldId : 'custentity1' }),
          fieldId = 'discountitem',
          value = membership === 'Elite' ? 137 : 0;

        so.setValue({ fieldId, value });
    }
  }
});
</code>```

Body:
I added a field in the Sales Order named Membership that will source from the customer record. Then when that field is set to the membership level (ex. elite member) it automatically set the discount item field to a specific discount item.... I encountered a notif saying SuiteScript 2.x entry point scripts must implement one script type function how do I fix this?

/**
 *@NApiVersion 2.x
 *@NScriptType ClientScript
 */
define(["N/currentRecord", "N/runtime"], function(currentRecord,runtime) {

    function pageInit(context) {

     const record = currentRecord.get()  // Get value of Membership Field
     const user = runtime.getCurrentUser().name
    }

    var membership = currentRecord.getField({
        fieldId : "custentity1",
    })

    if(membership == "Elite"){
        //Setting up discount
        record.setValue({
            fieldId: "discountitem", //fieldId of the Discount Item
            value: 137 // Internal ID of the Discount Item
        })}

        else {
            record.setValue({
                fieldId: "discount item",
                value: 0
            });
        }

        return {
            pageInit : pageInit
        }
});

Body:
Because your using 2.x your global settings are going to determine if this is run as 2.0 or 2.1. I'm guessing you have it set at 2.1 because you're context is having an issue.

If you try to run 2.1 code on the client then you might run into issues. One of those issues is that the context will not be populated.  Perhaps this is why you are using currentRecord.get() instead of context.currentRecord ?

Note: Any code that is outside a function (like pageInit) will be run when the module's callback is run.

   if(membership == "Elite"){
    //Setting up discount
    record.setValue({
        fieldId: "discountitem", //fieldId of the Discount Item
        value: 137 // Internal ID of the Discount Item
    })}

    else {
        record.setValue({
            fieldId: "discount item",
            value: 0
            });
    }

so this code is running before pageInit, so the suggestion by Sayeesh is a good one.

You are mixing some EcmaScipt versions.  I would get a good linter like eslint and make sure you are writing code for the target you want.

you got a field called discount item and one called discountitem? I'll assume that is a typo.

To play devil's advocate i'm going to say that you can use 2.1 if you really want to here.  In 2.1 you're code could be as simple as : (not checked)

/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 */

define(['N/record','N/currentRecord'],(record,currentRecord)=>{
  return {
    pageInit({ currentRecord : { id, type } }){

        const
          so = record.load({ id, type }),
          membership = so.getValue({ fieldId : 'custentity1' }),
          fieldId = 'discountitem',
          value = membership === 'Elite' ? 137 : 0;

        so.setValue({ fieldId, value });
    }
  }
});


问:
How to compress the image file size when it is in the folder
I want to compress the image file size to a max of 100kb, but the user can upload an image with a max size of 4MB, when entering the folder the image size becomes <100kb or max 100kb.

Below is my config code:

$config['upload_path']      = './assets/uploads/STNK/';
$config['allowed_types']    = 'jpg|png|jpeg|JPG|PNG|JPEG';
$config['max_size']         = '4000'; /* users can upload 4MB files */
$config['encrypt_name']     = TRUE;

$this->load->library('upload', $config);
if ($this->upload->do_upload('foto_stnk')) {
    $old_image = $this->data['row']->foto_stnk;
    unlink(FCPATH . './assets/uploads/STNK/' . $old_image);

    $new_image = $this->upload->data('file_name');
    $this->db->set('foto_stnk', $new_image);
} else {
    echo $this->upload->display_errors();
}
```

Please help, thank you!

答:
You can user imagejpeg() function of php to reduce image quality.
This might help you : imgejpegfunction</p>

Body:
I want to compress the image file size to a max of 100kb, but the user can upload an image with a max size of 4MB, when entering the folder the image size becomes <100kb or max 100kb.

Below is my config code:

$config['upload_path']      = './assets/uploads/STNK/';
$config['allowed_types']    = 'jpg|png|jpeg|JPG|PNG|JPEG';
$config['max_size']         = '4000'; /* users can upload 4MB files */
$config['encrypt_name']     = TRUE;

$this->load->library('upload', $config);
if ($this->upload->do_upload('foto_stnk')) {
    $old_image = $this->data['row']->foto_stnk;
    unlink(FCPATH . './assets/uploads/STNK/' . $old_image);

    $new_image = $this->upload->data('file_name');
    $this->db->set('foto_stnk', $new_image);
} else {
    echo $this->upload->display_errors();
}

Please help, thank you!


问:
How to carry over arguments to error handling in discord.py
I have a say command for my discord bot. The command works by asking for a channel and message. If the channel is left unfilled it will raise a ChannelNotFound error. I already handled that error but I want it to send the message in the channel the command was typed in if a ChannelNotFound is raised.

Here is the command and error handler.

@client.command()
async def say(ctx, channel: discord.TextChannel = None, *, message=None):
    if message is None:
        embed = discord.Embed(title="Say command",
                              description="Say something through It's Ez in a certain channel",
                              color=discord.Color.blue())
        embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar)
        embed.set_footer(text="Why was this message prompted? It's because you left message unfilled.")
        embed.add_field(name="Format", value="%say <#channel or id> <message>")
        await ctx.send(embed=embed)
    else:
        await channel.send(message)

    await ctx.message.delete()
```
<pre class="lang-py prettyprint-override"><code>@say.error
async def say_error(ctx, error):
    if isinstance(error, ChannelNotFound):
        embed = discord.Embed(title="Say command",
                              description="Say something through It's Ez in a certain channel",
                              color=discord.Color.blue())
        embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar)
        embed.set_footer(text="Why was this message prompted? It's because you left channel unfilled.")
        embed.add_field(name="Format", value="%say <#channel or id> <message>")
        await ctx.send(embed=embed)
    await ctx.message.delete()
</code>```

答:
I am not aware of any way that you can carry the argument over to the error handler, but you can use a try: and except: statment.

Here's your code with a try: and except: statment:

```<code>@client.command()
async def say(ctx, channel: discord.TextChannel = None, *, message=None):
    if message is None:
        embed = discord.Embed(title="Say command",
                              description="Say something through It's Ez in a certain channel",
                              color=discord.Color.blue())
        embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar)
        embed.set_footer(text="Why was this message prompted? It's because you left message unfilled.")
        embed.add_field(name="Format", value="%say <#channel or id> <message>")
        await ctx.send(embed=embed)
    else:
        try:
            await channel.send(message)
        except ChannelNotFound:
            await ctx.send("Channel not found!")
    await ctx.message.delete()
</code>```

Body:
I have a say command for my discord bot. The command works by asking for a channel and message. If the channel is left unfilled it will raise a ChannelNotFound error. I already handled that error but I want it to send the message in the channel the command was typed in if a ChannelNotFound is raised.

Here is the command and error handler.

@client.command()
async def say(ctx, channel: discord.TextChannel = None, *, message=None):
    if message is None:
        embed = discord.Embed(title="Say command",
                              description="Say something through It's Ez in a certain channel",
                              color=discord.Color.blue())
        embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar)
        embed.set_footer(text="Why was this message prompted? It's because you left message unfilled.")
        embed.add_field(name="Format", value="%say <#channel or id> <message>")
        await ctx.send(embed=embed)
    else:
        await channel.send(message)

    await ctx.message.delete()

@say.error
async def say_error(ctx, error):
    if isinstance(error, ChannelNotFound):
        embed = discord.Embed(title="Say command",
                              description="Say something through It's Ez in a certain channel",
                              color=discord.Color.blue())
        embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar)
        embed.set_footer(text="Why was this message prompted? It's because you left channel unfilled.")
        embed.add_field(name="Format", value="%say <#channel or id> <message>")
        await ctx.send(embed=embed)
    await ctx.message.delete()

Body:
I am not aware of any way that you can carry the argument over to the error handler, but you can use a try: and except: statment.

Here's your code with a try: and except: statment:

@client.command()
async def say(ctx, channel: discord.TextChannel = None, *, message=None):
    if message is None:
        embed = discord.Embed(title="Say command",
                              description="Say something through It's Ez in a certain channel",
                              color=discord.Color.blue())
        embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar)
        embed.set_footer(text="Why was this message prompted? It's because you left message unfilled.")
        embed.add_field(name="Format", value="%say <#channel or id> <message>")
        await ctx.send(embed=embed)
    else:
        try:
            await channel.send(message)
        except ChannelNotFound:
            await ctx.send("Channel not found!")
    await ctx.message.delete()


问:
Stable and simple amqp server for ubuntu
I am using rabbitmq-server as a amqp server with python client on Ubuntu 10.04 64 bit OS.

Rabbitmq-server now fails to start. I uninstalled and installed the latest rabbitmq-server 1.8.0 but still I could not make rabbitmq to work.

I want to other alternative with less learning curve to use the amqp on ubuntu system. Can you recommend one?

答:
Why waste your time by installing and managing a local installation of a MQ? This is something that can be outsourced at a low price very easily to StormMQ, a cloud based AMQP provider. More here:
http://www.stormmq.com</p>


问:
PHP Deployer failing on line without errors
I have a PHP Deployer task to recursively change file owner of everything in the deploy path:

```<code>task('set_owner', function () {
        run("chown -R someuser:www-data {{deploy_path}}");
    });

after('deploy:failed', 'deploy:unlock'); // Unlock after failed deploy
</code>```

Deployer failed on this task since there are some Operation not permitted errors given by this commond. Fair enough, since those files don't need changing anyway. So I add 2>&1 | grep -v 'Operation not permitted' to prevent the errors from showing:

```<code>task('set_owner', function () {
        run("chown -R deployer:www-data {{deploy_path}} 2>&1 | grep -v 'Operation not permitted'");
    });

after('deploy:failed', 'deploy:unlock'); // Unlock after failed deploy
</code>```

However when I run it Deployer still fails on this task:

```<code>➤ Executing task set_owner
[myapp.com] > chown -R someuser:www-data /var/www/myapp.com 2>&1 | grep -v 'Operation not permitted'
➤ Executing task deploy:failed
• done on [myapp.com]
✔ Ok [1ms]
➤ Executing task deploy:unlock
[myapp.com] > rm -f /var/www/myapp.com/.dep/deploy.lock
• done on [myapp.com]
✔ Ok [1s 364ms]
In Client.php line 103:

  [Deployer\Exception\RuntimeException (1)]
  The command "chown -R someuser:www-data /var/www/myapp.com 2>&
  1 | grep -v 'Operation not permitted'" failed.

  Exit Code: 1 (General error)
</code>```

Is there a way to prevent Deployer from failing in this case?

答:
I ended up putting a try catch around the call so Deployer will just ignore and continue.

```<code>task('set_owner', function () {
        try {
            run("chown -R someuser:www-data {{deploy_path}} 2>&1 | grep -v 'Operation not permitted'");
        }
        catch(\Deployer\Exception\RuntimeException $e) {
            // For some reason a RuntimeException is triggered afeter executing the command even with 2>&1 | grep -v 'Operation not permitted'
        }
    });
</code>```

Although I still don't get why Deployer throws this error even though the command line doesn't output anything.

Body:
I have a PHP Deployer task to recursively change file owner of everything in the deploy path:

task('set_owner', function () {
        run("chown -R someuser:www-data {{deploy_path}}");
    });

after('deploy:failed', 'deploy:unlock'); // Unlock after failed deploy

Deployer failed on this task since there are some Operation not permitted errors given by this commond. Fair enough, since those files don't need changing anyway. So I add 2>&1 | grep -v 'Operation not permitted' to prevent the errors from showing:

task('set_owner', function () {
        run("chown -R deployer:www-data {{deploy_path}} 2>&1 | grep -v 'Operation not permitted'");
    });

after('deploy:failed', 'deploy:unlock'); // Unlock after failed deploy

However when I run it Deployer still fails on this task:

➤ Executing task set_owner
[myapp.com] > chown -R someuser:www-data /var/www/myapp.com 2>&1 | grep -v 'Operation not permitted'
➤ Executing task deploy:failed
• done on [myapp.com]
✔ Ok [1ms]
➤ Executing task deploy:unlock
[myapp.com] > rm -f /var/www/myapp.com/.dep/deploy.lock
• done on [myapp.com]
✔ Ok [1s 364ms]
In Client.php line 103:

  [Deployer\Exception\RuntimeException (1)]
  The command "chown -R someuser:www-data /var/www/myapp.com 2>&
  1 | grep -v 'Operation not permitted'" failed.

  Exit Code: 1 (General error)

Is there a way to prevent Deployer from failing in this case?

Body:
I ended up putting a try catch around the call so Deployer will just ignore and continue.

task('set_owner', function () {
        try {
            run("chown -R someuser:www-data {{deploy_path}} 2>&1 | grep -v 'Operation not permitted'");
        }
        catch(\Deployer\Exception\RuntimeException $e) {
            // For some reason a RuntimeException is triggered afeter executing the command even with 2>&1 | grep -v 'Operation not permitted'
        }
    });

Although I still don't get why Deployer throws this error even though the command line doesn't output anything.


问:
How to Programmatically Scroll a ScrollView to Bottom
I've a problem I can't solve: inside a ScrollView I only have a
LinearLayout. By a user action I'm programmatically adding 2 TextView
on this LinearLayout, but by the default the scroll keeps on the top.
Since I controll the user action, I should be easy to scroll to the
bottom with something like:

```<code>ScrollView scroll = (ScrollView) this.findViewById(R.id.scroll);
scroll.scrollTo(0, scroll.getBottom());
</code>```

But actually not. Because immediately after adding this two new
elements getBottom() still returns the previous two. I tried to
refresh the state invoking refreshDrawableState(), but I doesn't work.

Do you have any idea how could I get the actual bottom of a ScrollView
after adding some elements?

答:
I Hope it Work

Try this:

nsscroll.post(new Runnable() {
@Override

```<code>                    public void run() {

                        nsscroll.scrollTo(0, nsscroll.getBottom());

                    }
                });
</code>```

Body:
I've a problem I can't solve: inside a ScrollView I only have a
LinearLayout. By a user action I'm programmatically adding 2 TextView
on this LinearLayout, but by the default the scroll keeps on the top.
Since I controll the user action, I should be easy to scroll to the
bottom with something like:

ScrollView scroll = (ScrollView) this.findViewById(R.id.scroll);
scroll.scrollTo(0, scroll.getBottom());

But actually not. Because immediately after adding this two new
elements getBottom() still returns the previous two. I tried to
refresh the state invoking refreshDrawableState(), but I doesn't work.

Do you have any idea how could I get the actual bottom of a ScrollView
after adding some elements?

Body:
I Hope it Work

Try this:

nsscroll.post(new Runnable() {
@Override

                    public void run() {

                        nsscroll.scrollTo(0, nsscroll.getBottom());

                    }
                });


问:
pandas: comparing non-identical list of panda dataframes based on values from a certain column
I have a two lists of panda dataframes as follows,

```<code>import pandas as pd
import numpy as np
list_one = [pd.DataFrame({'sent_a.1': [0, 3, 2, 1], 'sent_a.2': [0, 1, 4, 0], 'sent_b.3': [0, 6, 0, 8],'sent_b.4': [1, 1, 8, 6],'ID':['id_1','id_1','id_1','id_1']}),
        pd.DataFrame({'sent_a.1': [0, 3], 'sent_a.2': [0, 2], 'sent_b.3': [0, 6],'sent_b.4': [1, 1],'ID':['id_2','id_2']})]

list_two = [pd.DataFrame({'sent_a.1': [0, 5], 'sent_a.2': [0, 1], 'sent_b.3': [0, 6],'sent_b.4': [1, 1],'ID':['id_2','id_2']}),
            pd.DataFrame({'sent_a.1': [0, 5, 3, 1], 'sent_a.2': [0, 2, 3, 1], 'sent_b.3': [0, 6, 6, 8],'sent_b.4': [1, 5, 8, 5],'ID':['id_1','id_1','id_1','id_1']})]
</code>```

I would like to compare the dataframes in these two lists and if the values are the same, I would like to replace the value with 'True' and if the values are different, I would like to set them to 'False' and save the result in a different list of panda dataframes. I have done the following,

```<code>for dfs in list_one:
    for dfs2 in list_two:
       g = np.where(dfs == dfs2, 'True', 'False')
       print (g)
</code>```

but I get the error,

```<code>ValueError: Can only compare identically-labeled DataFrame objects
</code>```

how can I sort values in these two lists, based on the values from column 'ID'?

Edit
I would like the dataframes that have the same value for column 'ID' to be compared. meaning that dataframes that have 'ID' == 'id_1' are to be compared with one another and dataframes that have 'ID' == 'id_2' to be compared with each other (not a cross comparison)

so the desired output is:

```<code>output = [   sent_a.1  sent_a.2  sent_b.3  sent_b.4    ID
        0    True         True      True       True   id_1

        1    False        False     True       False  id_1

        2    False        False     False      True   id_1

        3    False        False     True       True   id_1,
             sent_a.1  sent_a.2  sent_b.3  sent_b.4    ID
        0    True         True      True       True   id_2
        1    True         True      False      False  id_2]
</code>```

答:
Based on your current example

For your first question:

how can I sort values in these two lists, based on the values from column 'ID'?

```<code>list_one = sorted(list_one,key=lambda x: x['ID'].unique()[0][3:], reverse=False)
list_two =sorted(list_two,key=lambda x: x['ID'].unique()[0][3:], reverse=False)
</code>```

ValueError: Can only compare identically-labeled DataFrame objects

error due to different index values order in dataframes or dataframes are of different shapes

First way of comparison:

```<code>for dfs in list_one:
    for dfs2 in list_two:
        if dfs.shape == dfs2.shape:
            g = np.where(dfs == dfs2, 'True', 'False')
            print (g)
</code>```

Second way:

I would like the dataframes that have the same value for column 'ID' to be compared

```<code>for dfs in list_one:
    for dfs2 in list_two:
        if (dfs['ID'].unique() == dfs2['ID'].unique()) and (dfs.shape == dfs2.shape):
            g = np.where(dfs == dfs2, 'True', 'False')
            print (g)
</code>```

Body:
I have a two lists of panda dataframes as follows,

import pandas as pd
import numpy as np
list_one = [pd.DataFrame({'sent_a.1': [0, 3, 2, 1], 'sent_a.2': [0, 1, 4, 0], 'sent_b.3': [0, 6, 0, 8],'sent_b.4': [1, 1, 8, 6],'ID':['id_1','id_1','id_1','id_1']}),
        pd.DataFrame({'sent_a.1': [0, 3], 'sent_a.2': [0, 2], 'sent_b.3': [0, 6],'sent_b.4': [1, 1],'ID':['id_2','id_2']})]

list_two = [pd.DataFrame({'sent_a.1': [0, 5], 'sent_a.2': [0, 1], 'sent_b.3': [0, 6],'sent_b.4': [1, 1],'ID':['id_2','id_2']}),
            pd.DataFrame({'sent_a.1': [0, 5, 3, 1], 'sent_a.2': [0, 2, 3, 1], 'sent_b.3': [0, 6, 6, 8],'sent_b.4': [1, 5, 8, 5],'ID':['id_1','id_1','id_1','id_1']})]

I would like to compare the dataframes in these two lists and if the values are the same, I would like to replace the value with 'True' and if the values are different, I would like to set them to 'False' and save the result in a different list of panda dataframes. I have done the following,

for dfs in list_one:
    for dfs2 in list_two:
       g = np.where(dfs == dfs2, 'True', 'False')
       print (g)

but I get the error,

ValueError: Can only compare identically-labeled DataFrame objects

how can I sort values in these two lists, based on the values from column 'ID'?

Edit
I would like the dataframes that have the same value for column 'ID' to be compared. meaning that dataframes that have 'ID' == 'id_1' are to be compared with one another and dataframes that have 'ID' == 'id_2' to be compared with each other (not a cross comparison)

so the desired output is:

output = [   sent_a.1  sent_a.2  sent_b.3  sent_b.4    ID
        0    True         True      True       True   id_1

        1    False        False     True       False  id_1

        2    False        False     False      True   id_1

        3    False        False     True       True   id_1,
             sent_a.1  sent_a.2  sent_b.3  sent_b.4    ID
        0    True         True      True       True   id_2
        1    True         True      False      False  id_2]

Body:
Based on your current example

For your first question:

how can I sort values in these two lists, based on the values from column 'ID'?

list_one = sorted(list_one,key=lambda x: x['ID'].unique()[0][3:], reverse=False)
list_two =sorted(list_two,key=lambda x: x['ID'].unique()[0][3:], reverse=False)

ValueError: Can only compare identically-labeled DataFrame objects

error due to different index values order in dataframes or dataframes are of different shapes

First way of comparison:

for dfs in list_one:
    for dfs2 in list_two:
        if dfs.shape == dfs2.shape:
            g = np.where(dfs == dfs2, 'True', 'False')
            print (g)

Second way:

I would like the dataframes that have the same value for column 'ID' to be compared

for dfs in list_one:
    for dfs2 in list_two:
        if (dfs['ID'].unique() == dfs2['ID'].unique()) and (dfs.shape == dfs2.shape):
            g = np.where(dfs == dfs2, 'True', 'False')
            print (g)


问:
cpu vs gpu - when cpu is better
I know many examples when GPU is much faster than CPU. But exists algorithms (problems) which are very hard to parallelise. Could you give me some examples or tests when CPU can overcome GPU ?

Edit:

Thanks for suggestions! We can make a comparison between the most popular and the newest cpu's and gpu's, for example Core i5 2500k vs GeForce GTX 560 Ti.

I wonder how to compare SIMD model between them. For example: Cuda calls a SIMD model more precisely SIMT. But SIMT should be compared to the multhitreading on CPU's which is distributing threads (tasks) between MIMD cores (Core i5 2500k give as 4 MIMD cores). On the other hand each of these MIMD cores can implement SIMD model, but this is something else than SIMT and I don't know how to compare them. Finally a fermi architecture with concurrent kernel execution might be consider as MIMD cores with SIMT.

答:
Based on my experience, I will summarize the key differences in terms of performance between parallel programs in CPUs and GPUs. Trust me, a comparison can be changed from generation to generation. So I will just point out what is good and is bad for CPUs and GPUs. Of course, if you make a program at the extreme, i.e., having only bad or good sides, it will run definitely faster on one platform. But a mixture of those requires very complicated reasoning.

Host program level

One key difference is memory transfer cost. GPU devices requires some memory transfers. This cost is non-trivial in some cases, for example when you have to frequently transfer some big arrays. In my experience, this cost can be minimized but pushing most of host code to device code. The only cases you can do so are when you have to interact with the host operating system in program, such as outputting to monitor.

Device program level

Now we come to see a complex picture that hasn't been fully revealed yet. What I mean is there are many mysterious scenes in GPUs that haven't been disclosed. But still, we have a lot of distinguish CPU and GPU (kernel code) in terms of performance.

There are few factors that I noticed those dramatically contribute to the difference.

Workload distribution

GPUs, which consist of many execution units, are designed to handle massively parallel programs. If you have little of work, say a few sequential tasks, and put these tasks on a GPU, only a few of those many execution units are busy, thus will be slower than CPU. Because CPUs are, in other hand, better to handle short and sequential tasks. The reason is simple, CPUs are much more complicated and able to exploit instruction level parallelism, whereas GPUs exploit thread level parallelism. Well, I heard NVIDIA GF104 can do Superscalar, but I had no chance to experience with it though.

It is worth noting that, in GPUs, workload are divided into small blocks (or workgroups in OpenCL), and blocks are arranged in chunks, each of which is executed in one Streaming processor (I am using terminologies from NVIDIA). But in CPUs, those blocks are executed sequentially - I can't think of anything else than a single loop.

Thus, for programs that have small number of blocks, it will be likely to run faster on CPUs.

Control flow instructions

Branches are bad things to GPUs, always. Please bear in mind that GPUs prefer equal things. Equal blocks, equal threads within a blocks, and equal threads within a warp. But what matters the most?

```<code>                            ***Branch divergences.***
</code>```

Cuda/OpenCL programmers hate branch divergences. Since all the threads somehow are divided into sets of 32 threads, called a warp, and all threads within a warp execute in lockstep, a branch divergence will cause some threads in the warp to be serialized. Thus, the execution time of the warp will be accordingly multiplied.

Unlike GPUs, each cores in CPUs can follow their own path. Furthermore, branches can be efficiently executed because CPUs have branch prediction.

Thus, programs that have more warp divergences are likely to run faster on CPUs.

Memory access instructions

This REALLY is complicated enough so let's make it brief.

Remember that global memory accesses have very high latency (400-800 cycles). So in old generations of GPUs, whether memory accesses are coalesced was a critical matter. Now your GTX560 (Fermi) has more 2 level of caches. So global memory access cost can be reduced in many cases. However, caches in CPUs and GPUs are different, so their effects are also different.

What I can say is that it really really depends on your memory access pattern, your kernel code pattern (how memory accesses are interleaved with computation, the types of operations, etc., ) to tell if one runs faster on GPUs or CPUs.

But somehow you can expect a huge number of cache misses (in GPUs) has a very bad effect on GPUs (how bad? - it depends on your code).

Additionally, shared memory is an important feature of GPUs. Accessing to shared memory is as fast as accessing to GPU L1 cache. So kernels that make use of shared memory will have pretty much benefit.

Some other factors I haven't really mentioned but those can have big impact on the performance in many cases such as bank conflicts, size of memory transaction, GPU occupancy...

Title: cpu vs gpu - when cpu is better

Body:
Based on my experience, I will summarize the key differences in terms of performance between parallel programs in CPUs and GPUs. Trust me, a comparison can be changed from generation to generation. So I will just point out what is good and is bad for CPUs and GPUs. Of course, if you make a program at the extreme, i.e., having only bad or good sides, it will run definitely faster on one platform. But a mixture of those requires very complicated reasoning.

Host program level

One key difference is memory transfer cost. GPU devices requires some memory transfers. This cost is non-trivial in some cases, for example when you have to frequently transfer some big arrays. In my experience, this cost can be minimized but pushing most of host code to device code. The only cases you can do so are when you have to interact with the host operating system in program, such as outputting to monitor.

Device program level

Now we come to see a complex picture that hasn't been fully revealed yet. What I mean is there are many mysterious scenes in GPUs that haven't been disclosed. But still, we have a lot of distinguish CPU and GPU (kernel code) in terms of performance.

There are few factors that I noticed those dramatically contribute to the difference.

Workload distribution

GPUs, which consist of many execution units, are designed to handle massively parallel programs. If you have little of work, say a few sequential tasks, and put these tasks on a GPU, only a few of those many execution units are busy, thus will be slower than CPU. Because CPUs are, in other hand, better to handle short and sequential tasks. The reason is simple, CPUs are much more complicated and able to exploit instruction level parallelism, whereas GPUs exploit thread level parallelism. Well, I heard NVIDIA GF104 can do Superscalar, but I had no chance to experience with it though.

It is worth noting that, in GPUs, workload are divided into small blocks (or workgroups in OpenCL), and blocks are arranged in chunks, each of which is executed in one Streaming processor (I am using terminologies from NVIDIA). But in CPUs, those blocks are executed sequentially - I can't think of anything else than a single loop.

Thus, for programs that have small number of blocks, it will be likely to run faster on CPUs.

Control flow instructions

Branches are bad things to GPUs, always. Please bear in mind that GPUs prefer equal things. Equal blocks, equal threads within a blocks, and equal threads within a warp. But what matters the most?

                            ***Branch divergences.***

Cuda/OpenCL programmers hate branch divergences. Since all the threads somehow are divided into sets of 32 threads, called a warp, and all threads within a warp execute in lockstep, a branch divergence will cause some threads in the warp to be serialized. Thus, the execution time of the warp will be accordingly multiplied.

Unlike GPUs, each cores in CPUs can follow their own path. Furthermore, branches can be efficiently executed because CPUs have branch prediction.

Thus, programs that have more warp divergences are likely to run faster on CPUs.

Memory access instructions

This REALLY is complicated enough so let's make it brief.

Remember that global memory accesses have very high latency (400-800 cycles). So in old generations of GPUs, whether memory accesses are coalesced was a critical matter. Now your GTX560 (Fermi) has more 2 level of caches. So global memory access cost can be reduced in many cases. However, caches in CPUs and GPUs are different, so their effects are also different.

What I can say is that it really really depends on your memory access pattern, your kernel code pattern (how memory accesses are interleaved with computation, the types of operations, etc., ) to tell if one runs faster on GPUs or CPUs.

But somehow you can expect a huge number of cache misses (in GPUs) has a very bad effect on GPUs (how bad? - it depends on your code).

Additionally, shared memory is an important feature of GPUs. Accessing to shared memory is as fast as accessing to GPU L1 cache. So kernels that make use of shared memory will have pretty much benefit.

Some other factors I haven't really mentioned but those can have big impact on the performance in many cases such as bank conflicts, size of memory transaction, GPU occupancy...


问:
Tesseract OCR confuses slashed 0 as 8
I have trained tesseract on the terminus font, but no matter what, I can't get it to recognize the 0s. I am using the jTessEditor to create the training tif and boxes. Even when validating, it reads all 0s as 8s. Is there anything I am missing?

Here is an example of the 0 and it reading it as 8:

I use the following parameters:

--psm 10 -c tessedit_char_whitelist=0123456789# --oem 3 -l terminus

答:
I cannot help you on finding a better way, how to use tesseract.

I used CIB deeper OCR engine, which uses an OCR recognition purely based on an AI on your image at top. There the zeros from your example image were recognized as zeros.

The OCR engine can be freely used on https://doxiview.cib.de/ (click on the right side on text recognition). More info on Deeper: https://deeper.cib.de/en/

Title: Tesseract OCR confuses slashed 0 as 8


问:
R Avoid rowwise() and looking for faster alternative
I want to merge two vectors into one dataset and integrate it with the function mutate as 5 new columns into the already existing dataset.
Here is my example code:

```<code>vector1<-list(c("Reply","Reshare","Like","Share","Search"),c("Reply","Reshare","Like","Share","Search"),c("Reply","Reshare","Like","Share","Search"))
vector2<-list(c(1,2,6,3,4),c(3,7,9,2,4),c(5,2,8,4,0))

tibble(vector1=vector1,
       vector2=vector2)%>%
  rowwise()%>%
  mutate(vector2|> set_names(vector1)|> as.list()|> data.frame())

# A tibble: 3 x 7
# Rowwise:
  vector1   vector2   Reply Reshare  Like Share Search
  <list>    <list>    <dbl>   <dbl> <dbl> <dbl>  <dbl>
1 <chr [5]> <dbl [5]>     1       2     6     3      4
2 <chr [5]> <dbl [5]>     3       7     9     2      4
3 <chr [5]> <dbl [5]>     5       2     8     4      0
</code>```

This works quite well so far. However, I have a very large dataset and the rowwise() solution is very time consuming.
If I omit the rowwise() function I get an error message.

I think the error is due to the fact that I transform the vectors as a list (as.list()). The mutate function for the data set does not seem to be able to handle this.

The rowwise() function should be omitted and only the code in the mutate function should be changed.

Can anyone help me and provide a faster solution?

答:
I suggest to use mapply

library(dplyr)
library(magrittr)

tibble(vector1=vector1,
       vector2=vector2) %>%
  mutate(mapply(set_names, vector2, vector1, SIMPLIFY = FALSE) %>%
         do.call(rbind, .) %>%
         data.frame())

# A tibble: 3 × 7
  vector1   vector2   Reply Reshare  Like Share Search
  <list>    <list>    <dbl>   <dbl> <dbl> <dbl>  <dbl>
1 <chr [5]> <dbl [5]>     1       2     6     3      4
2 <chr [5]> <dbl [5]>     3       7     9     2      4
3 <chr [5]> <dbl [5]>     5       2     8     4      0

```
<p>Here is a benchmark that compares <code>rowwise</code> against <code>mapply</code> with vectors of length 100 and shuffled labels</p>
<pre class="lang-r prettyprint-override"><code>vector1 <- replicate(sample(c("Reply","Reshare","Like","Share","Search"),
                            5,
                            replace = FALSE),
                     n = 100,
                     simplify = FALSE)

vector2 <- replicate(rnorm(5), n= 100, simplify = FALSE)

tb <- tibble(vector1 = vector1, vector2 = vector2)

microbenchmark(mapply = tb |>
                 mutate(mapply(set_names, vector2, vector1, SIMPLIFY = FALSE) %>%
                        do.call(rbind, .) |>
                        data.frame()),
               rowwise = tb %>%
                 rowwise()%>%
                 mutate(vector2|> set_names(vector1)|> as.list()|> data.frame()))

+ Unit: milliseconds
    expr       min        lq      mean    median        uq       max neval
  mapply  2.439877  2.487191  2.630114  2.512208  2.576073  5.990312   100
 rowwise 37.309123 37.775255 39.386047 38.193196 41.221624 44.088820   100
</code>```

Body:
I want to merge two vectors into one dataset and integrate it with the function mutate as 5 new columns into the already existing dataset.
Here is my example code:

vector1<-list(c("Reply","Reshare","Like","Share","Search"),c("Reply","Reshare","Like","Share","Search"),c("Reply","Reshare","Like","Share","Search"))
vector2<-list(c(1,2,6,3,4),c(3,7,9,2,4),c(5,2,8,4,0))

tibble(vector1=vector1,
       vector2=vector2)%>%
  rowwise()%>%
  mutate(vector2|> set_names(vector1)|> as.list()|> data.frame())

# A tibble: 3 x 7
# Rowwise:
  vector1   vector2   Reply Reshare  Like Share Search
  <list>    <list>    <dbl>   <dbl> <dbl> <dbl>  <dbl>
1 <chr [5]> <dbl [5]>     1       2     6     3      4
2 <chr [5]> <dbl [5]>     3       7     9     2      4
3 <chr [5]> <dbl [5]>     5       2     8     4      0

This works quite well so far. However, I have a very large dataset and the rowwise() solution is very time consuming.
If I omit the rowwise() function I get an error message.

I think the error is due to the fact that I transform the vectors as a list (as.list()). The mutate function for the data set does not seem to be able to handle this.

The rowwise() function should be omitted and only the code in the mutate function should be changed.

Can anyone help me and provide a faster solution?

Body:
I suggest to use mapply

library(dplyr)
library(magrittr)

tibble(vector1=vector1,
       vector2=vector2) %>%
  mutate(mapply(set_names, vector2, vector1, SIMPLIFY = FALSE) %>%
         do.call(rbind, .) %>%
         data.frame())

# A tibble: 3 × 7
  vector1   vector2   Reply Reshare  Like Share Search
  <list>    <list>    <dbl>   <dbl> <dbl> <dbl>  <dbl>
1 <chr [5]> <dbl [5]>     1       2     6     3      4
2 <chr [5]> <dbl [5]>     3       7     9     2      4
3 <chr [5]> <dbl [5]>     5       2     8     4      0

Here is a benchmark that compares rowwise against mapply with vectors of length 100 and shuffled labels

vector1 <- replicate(sample(c("Reply","Reshare","Like","Share","Search"),
                            5,
                            replace = FALSE),
                     n = 100,
                     simplify = FALSE)

vector2 <- replicate(rnorm(5), n= 100, simplify = FALSE)

tb <- tibble(vector1 = vector1, vector2 = vector2)

microbenchmark(mapply = tb |>
                 mutate(mapply(set_names, vector2, vector1, SIMPLIFY = FALSE) %>%
                        do.call(rbind, .) |>
                        data.frame()),
               rowwise = tb %>%
                 rowwise()%>%
                 mutate(vector2|> set_names(vector1)|> as.list()|> data.frame()))

+ Unit: milliseconds
    expr       min        lq      mean    median        uq       max neval
  mapply  2.439877  2.487191  2.630114  2.512208  2.576073  5.990312   100
 rowwise 37.309123 37.775255 39.386047 38.193196 41.221624 44.088820   100


问:
Opening a Excel sheet which is read only
I'm writing a program which transfers data from an acces database to an Excel Sheet.

The program is written in Excel and is stored in another excel file then where the data is tranferred to

This because the owner is very careful with his data and preferes the code in a seperate file.

He marked the file read Only.

I can open it and write data, but as soon as i close the workbook the changes are discarded.

Is there a way to open the workbook so i can write data to it?

答: After consulting with the IT department here, it seems that it is a setting in sharepoint. Wich is managed by the IT department. So there is no solution</p>


问:
Using the RUN instruction in a Dockerfile with 'source' does not work
I have a Dockerfile that I am putting together to install a vanilla python environment (into which I will be installing an app, but at a later date).

```<code>FROM ubuntu:12.04

# required to build certain python libraries
RUN apt-get install python-dev -y

# install pip - canonical installation instructions from pip-installer.org
# http://www.pip-installer.org/en/latest/installing.html
ADD https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py /tmp/ez_setup.py
ADD https://raw.github.com/pypa/pip/master/contrib/get-pip.py /tmp/get-pip.py
RUN python /tmp/ez_setup.py
RUN python /tmp/get-pip.py
RUN pip install --upgrade pip

# install and configure virtualenv
RUN pip install virtualenv
RUN pip install virtualenvwrapper
ENV WORKON_HOME ~/.virtualenvs
RUN mkdir -p $WORKON_HOME
RUN source /usr/local/bin/virtualenvwrapper.sh
</code>```

The build runs ok until the last line, where I get the following exception:

```<code>[previous steps 1-9 removed for clarity]
...
Successfully installed virtualenvwrapper virtualenv-clone stevedore
Cleaning up...
 ---> 1fc253a8f860
Step 10 : ENV WORKON_HOME ~/.virtualenvs
 ---> Running in 8b0145d2c80d
 ---> 0f91a5d96013
Step 11 : RUN mkdir -p $WORKON_HOME
 ---> Running in 9d2552712ddf
 ---> 3a87364c7b45
Step 12 : RUN source /usr/local/bin/virtualenvwrapper.sh
 ---> Running in c13a187261ec
/bin/sh: 1: source: not found
</code>```

If I ls into that directory (just to test that the previous steps were committed) I can see that the files exist as expected:

```<code>$ docker run 3a87 ls /usr/local/bin
easy_install
easy_install-2.7
pip
pip-2.7
virtualenv
virtualenv-2.7
virtualenv-clone
virtualenvwrapper.sh
virtualenvwrapper_lazy.sh
</code>```

If I try just running the source command I get the same 'not found' error as above. If I RUN an interactive shell session however, source does work:

```<code>$ docker run 3a87 bash
source
bash: line 1: source: filename argument required
source: usage: source filename [arguments]
</code>```

I can run the script from here, and then happily access workon, mkvirtualenv etc.

I've done some digging, and initially it looked as if the problem might lie in the difference between bash as the Ubuntu login shell, and dash as the Ubuntu system shell, dash not supporting the source command.

However, the answer to this appears to be to use '.' instead of source, but this just causes the Docker runtime to blow up with a go panic exception.

What is the best way to run a shell script from a Dockerfile RUN instruction to get around this (am running off the default base image for Ubuntu 12.04 LTS).

答:
This is my solution on "Ubuntu 20.04"</p>
```<code>RUN apt -y update
RUN apt -y install curl
SHELL ["/bin/bash", "-c"]
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
RUN source /root/.bashrc
RUN bash -c ". /root/.nvm/nvm.sh && nvm install v16 && nvm alias default v16 && nvm use default"
</code>```

Body:
I have a Dockerfile that I am putting together to install a vanilla python environment (into which I will be installing an app, but at a later date).

FROM ubuntu:12.04

# required to build certain python libraries
RUN apt-get install python-dev -y

# install pip - canonical installation instructions from pip-installer.org
# http://www.pip-installer.org/en/latest/installing.html
ADD https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py /tmp/ez_setup.py
ADD https://raw.github.com/pypa/pip/master/contrib/get-pip.py /tmp/get-pip.py
RUN python /tmp/ez_setup.py
RUN python /tmp/get-pip.py
RUN pip install --upgrade pip

# install and configure virtualenv
RUN pip install virtualenv
RUN pip install virtualenvwrapper
ENV WORKON_HOME ~/.virtualenvs
RUN mkdir -p $WORKON_HOME
RUN source /usr/local/bin/virtualenvwrapper.sh

The build runs ok until the last line, where I get the following exception:

[previous steps 1-9 removed for clarity]
...
Successfully installed virtualenvwrapper virtualenv-clone stevedore
Cleaning up...
 ---> 1fc253a8f860
Step 10 : ENV WORKON_HOME ~/.virtualenvs
 ---> Running in 8b0145d2c80d
 ---> 0f91a5d96013
Step 11 : RUN mkdir -p $WORKON_HOME
 ---> Running in 9d2552712ddf
 ---> 3a87364c7b45
Step 12 : RUN source /usr/local/bin/virtualenvwrapper.sh
 ---> Running in c13a187261ec
/bin/sh: 1: source: not found

If I ls into that directory (just to test that the previous steps were committed) I can see that the files exist as expected:

$ docker run 3a87 ls /usr/local/bin
easy_install
easy_install-2.7
pip
pip-2.7
virtualenv
virtualenv-2.7
virtualenv-clone
virtualenvwrapper.sh
virtualenvwrapper_lazy.sh

If I try just running the source command I get the same 'not found' error as above. If I RUN an interactive shell session however, source does work:

$ docker run 3a87 bash
source
bash: line 1: source: filename argument required
source: usage: source filename [arguments]

I can run the script from here, and then happily access workon, mkvirtualenv etc.

I've done some digging, and initially it looked as if the problem might lie in the difference between bash as the Ubuntu login shell, and dash as the Ubuntu system shell, dash not supporting the source command.

However, the answer to this appears to be to use '.' instead of source, but this just causes the Docker runtime to blow up with a go panic exception.

What is the best way to run a shell script from a Dockerfile RUN instruction to get around this (am running off the default base image for Ubuntu 12.04 LTS).

Body:
This is my solution on "Ubuntu 20.04"

RUN apt -y update
RUN apt -y install curl
SHELL ["/bin/bash", "-c"]
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
RUN source /root/.bashrc
RUN bash -c ". /root/.nvm/nvm.sh && nvm install v16 && nvm alias default v16 && nvm use default"


问:
Is qHash consistent across computers?
I have a database table with multiple text columns that collectively have to be unique, and I don't want to use a multicolumn key, so I was thinking to hash the strings together into an int and use that as the primary key. I was wondering if it would be a better idea to take advantage of uint qHash ( const QString & key ), or to write my own function, given that the database will need to be edited by different people in different places. (Also, if the whole approach is bad, please help.)</p>

答:
qHash is implemented as below :

```<code>static uint hash(const uchar *p, int n)
{
    uint h = 0;
    uint g;

    while (n--) {
        h = (h << 4) + *p++;
        if ((g = (h & 0xf0000000)) != 0)
            h ^= g >> 23;
        h &= ~g;
    }
    return h;
}

static uint hash(const QChar *p, int n)
{
    uint h = 0;
    uint g;

    while (n--) {
        h = (h << 4) + (*p++).unicode();
        if ((g = (h & 0xf0000000)) != 0)
            h ^= g >> 23;
        h &= ~g;
    }
    return h;
}
</code>```

There is nothing specific to platform in that code. However a hash algorithm does not guarantee uniqueness like a database. It does its best to avoid collisions but it is not guaranteed. That is why most hash containers use buckets and reallocation algorithms.

Title: Is qHash consistent across computers?

Body:
qHash is implemented as below :

static uint hash(const uchar *p, int n)
{
    uint h = 0;
    uint g;

    while (n--) {
        h = (h << 4) + *p++;
        if ((g = (h & 0xf0000000)) != 0)
            h ^= g >> 23;
        h &= ~g;
    }
    return h;
}

static uint hash(const QChar *p, int n)
{
    uint h = 0;
    uint g;

    while (n--) {
        h = (h << 4) + (*p++).unicode();
        if ((g = (h & 0xf0000000)) != 0)
            h ^= g >> 23;
        h &= ~g;
    }
    return h;
}

There is nothing specific to platform in that code. However a hash algorithm does not guarantee uniqueness like a database. It does its best to avoid collisions but it is not guaranteed. That is why most hash containers use buckets and reallocation algorithms.


问:
Flyway migration and callbacks (java)
colleagues! I'm using flyway migration and want to use callback after my own schema is created. Is it possible to do it and how?

答: see my answer, may be it help you stackoverflow</p>

Title: Flyway migration and callbacks (java)


问:
Mixing optional certificate authentication with Windows authentication in ASP.NET Core
I have a .NET 6 app where I need to have different endpoints reacting to different authentication schemes. I curently support two types of JWT authentication (local authentication, OIDC) along with Windows auth. Now I'm trying to add certificate authentification (the idea being a differnet authentification endpoint, which checks the supplied certificate, and if there's one, issues a bearer token so the rest of the app can keep using bearer tokens).

My authentication controller allows to fetch tokens for multiple authorization schemes. The controller has no defined authorization scheme - the schemes are then set on individual methods

```<code>[ApiController]
[Route("[controller]")]
public class AuthenticationController : ControllerBase
{
}
</code>```

Then we have the ability to fetch an access token using Windows authentication, local authentification, and certificate authentification:

```<code>[Authorize(AuthenticationSchemes = NegotiateDefaults.AuthenticationScheme)]
[HttpGet("Token")]
public IActionResult GenerateToken([FromQuery] string clientId = null)
{
  var userIdentity = User?.Identity?.Name;
  //
}
</code>```

And for local auth

```<code>[AllowAnonymous]
[HttpPost("authenticate")]
public IActionResult Authenticate([FromBody] AuthenticateModel model, [FromQuery] string clientId = null)
{
  // validate login, issue token
}
</code>```

Now I've added a third authentication method that looks at a certificate

```<code>[Authorize(AuthenticationSchemes = CertificateAuthenticationDefaults.AuthenticationScheme)]
[HttpGet("Certificate")]
public IActionResult GenerateCertificateToken([FromQuery] string clientId = null)
{
    var cert = HttpContext.Connection.ClientCertificate;
    // get identity from certificate, validate, issue token
}
</code>```

To support optional client certificates I'm using this code approach.

```<code>webBuilder.ConfigureKestrel(options =>
{
    options.ConfigureHttpsDefaults(options =>
    {
        options.ClientCertificateMode = Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode.AllowCertificate;
     });
});
</code>```

Most of my controllers then use the access token returned by the authentification controller's 3 methods to get a token. Sample controller:

```<code>[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
}
</code>```

So far, all is good. Now I also have a bunch of controllers that do Windows authentification only

```<code>[Authorize(AuthenticationSchemes = Microsoft.AspNetCore.Authentication.Negotiate.NegotiateDefaults.AuthenticationScheme)]
[Route("Maintenance")]
public class MaintenanceController: BaseMaintenanceController
{
}

[ApiExplorerSettings(IgnoreApi = true)]
public class BaseMaintenanceController: ControllerBase
{
}
</code>```

The MaintenanceController is in a different namespace than BaseMaintenanceController, UserController and AuthentificationController (not sure if this matters).

When I do not register certificate authentification, things work as expected (except of course I cannot use GenerateCertificateToken because the auth scheme certificate is not registered. As soon as I register for certificate authentification, my MaintenanceController only throws 403s.

Here's how I register the authentification schemes in Startup.cs 's

```<code>public void ConfigureServices(IServiceCollection services)
{
    ....
    var authBuilder = services.AddAuthentication(x =>
        {
            x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
        });

    var authPolicyBuilder = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme);

     authBuilder = authBuilder
           .AddNegotiate()
           .AddJwtBearer() // details omitted here
           .AddCertificate(u =>
            {
                u.AllowedCertificateTypes = CertificateTypes.All;
                u.ValidateCertificateUse = true;
                u.ValidateValidityPeriod = true;
                u.RevocationMode = X509RevocationMode.NoCheck; // for speed
            });

    services.AddAuthorization(options =>
        {
            options.DefaultPolicy = authPolicyBuilder.Build();
        });

}
</code>```

As soon as I have the authBuilder.AddCertificate call in ConfigureServices, my MaintenanceController keeps returning 403s. Remove the call to AddCertificate and MaintenanceController works fine again.Interestingly enough, the GenerateToken method in my AuthentificationController keeps working though.

Anybody has any idea why AddCertificate kills my controllers that use NegotiateDefaults.AuthenticationScheme for the entire controller, but leaves the AuthentificationController.GenerateCertificateToken running, even though it uses the same Authorize tag?

答: I figured, I'd see if me having a controller in a different Namespace would trip things up somehow, so added a new controller that performs negotiate, and from that point on, things started working, and kept working when I removed the controller I had added. I guess the sun needed top past the mid-day point or something.</p>

Body:
I have a .NET 6 app where I need to have different endpoints reacting to different authentication schemes. I curently support two types of JWT authentication (local authentication, OIDC) along with Windows auth. Now I'm trying to add certificate authentification (the idea being a differnet authentification endpoint, which checks the supplied certificate, and if there's one, issues a bearer token so the rest of the app can keep using bearer tokens).

My authentication controller allows to fetch tokens for multiple authorization schemes. The controller has no defined authorization scheme - the schemes are then set on individual methods

[ApiController]
[Route("[controller]")]
public class AuthenticationController : ControllerBase
{
}

Then we have the ability to fetch an access token using Windows authentication, local authentification, and certificate authentification:

[Authorize(AuthenticationSchemes = NegotiateDefaults.AuthenticationScheme)]
[HttpGet("Token")]
public IActionResult GenerateToken([FromQuery] string clientId = null)
{
  var userIdentity = User?.Identity?.Name;
  //
}

And for local auth

[AllowAnonymous]
[HttpPost("authenticate")]
public IActionResult Authenticate([FromBody] AuthenticateModel model, [FromQuery] string clientId = null)
{
  // validate login, issue token
}

Now I've added a third authentication method that looks at a certificate

[Authorize(AuthenticationSchemes = CertificateAuthenticationDefaults.AuthenticationScheme)]
[HttpGet("Certificate")]
public IActionResult GenerateCertificateToken([FromQuery] string clientId = null)
{
    var cert = HttpContext.Connection.ClientCertificate;
    // get identity from certificate, validate, issue token
}

To support optional client certificates I'm using this code approach.

webBuilder.ConfigureKestrel(options =>
{
    options.ConfigureHttpsDefaults(options =>
    {
        options.ClientCertificateMode = Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode.AllowCertificate;
     });
});

Most of my controllers then use the access token returned by the authentification controller's 3 methods to get a token. Sample controller:

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{
}

So far, all is good. Now I also have a bunch of controllers that do Windows authentification only

[Authorize(AuthenticationSchemes = Microsoft.AspNetCore.Authentication.Negotiate.NegotiateDefaults.AuthenticationScheme)]
[Route("Maintenance")]
public class MaintenanceController: BaseMaintenanceController
{
}

[ApiExplorerSettings(IgnoreApi = true)]
public class BaseMaintenanceController: ControllerBase
{
}

The MaintenanceController is in a different namespace than BaseMaintenanceController, UserController and AuthentificationController (not sure if this matters).

When I do not register certificate authentification, things work as expected (except of course I cannot use GenerateCertificateToken because the auth scheme certificate is not registered. As soon as I register for certificate authentification, my MaintenanceController only throws 403s.

Here's how I register the authentification schemes in Startup.cs 's

public void ConfigureServices(IServiceCollection services)
{
    ....
    var authBuilder = services.AddAuthentication(x =>
        {
            x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
        });

    var authPolicyBuilder = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme);

     authBuilder = authBuilder
           .AddNegotiate()
           .AddJwtBearer() // details omitted here
           .AddCertificate(u =>
            {
                u.AllowedCertificateTypes = CertificateTypes.All;
                u.ValidateCertificateUse = true;
                u.ValidateValidityPeriod = true;
                u.RevocationMode = X509RevocationMode.NoCheck; // for speed
            });

    services.AddAuthorization(options =>
        {
            options.DefaultPolicy = authPolicyBuilder.Build();
        });

}

As soon as I have the authBuilder.AddCertificate call in ConfigureServices, my MaintenanceController keeps returning 403s. Remove the call to AddCertificate and MaintenanceController works fine again.Interestingly enough, the GenerateToken method in my AuthentificationController keeps working though.

Anybody has any idea why AddCertificate kills my controllers that use NegotiateDefaults.AuthenticationScheme for the entire controller, but leaves the AuthentificationController.GenerateCertificateToken running, even though it uses the same Authorize tag?


问:
nextjs Image property null
I have a component with dynamic content like so :
<Image width="680" height="400" src={post.imgsrc} alt={post.title} />

I keep having the same error even if I have added the width/height :

Image with src "/imgs/paintings/2022/2.jpg" must use "width" and "height" properties or "layout='fill'" property.

I don't know why, and I wonder why I have to specify width and height since my pictures have different sizes...

答:
One thing to mention is that you can import the images if they are local, if you want that you do not need to use width, height, or fill.
They get provided automatically

Example from the docs:

```<code>import profilePic from '../public/me.png'

import Image from 'next/image'
import profilePic from '../public/me.png'

function Home() {
  return (
    <>
      <h1>My Homepage</h1>
      <Image
        src={profilePic}
        alt="Picture of the author"
        // width={500} automatically provided
        // height={500} automatically provided
        // blurDataURL="data:..." automatically provided
        // placeholder="blur" // Optional blur-up while loading
      />
      <p>Welcome to my homepage!</p>
    </>
  )
}
</code>```

This also explains why remote images do need these properties.

Title: nextjs Image property null

Body:
One thing to mention is that you can import the images if they are local, if you want that you do not need to use width, height, or fill.
They get provided automatically

Example from the docs:

import profilePic from '../public/me.png'

import Image from 'next/image'
import profilePic from '../public/me.png'

function Home() {
  return (
    <>
      <h1>My Homepage</h1>
      <Image
        src={profilePic}
        alt="Picture of the author"
        // width={500} automatically provided
        // height={500} automatically provided
        // blurDataURL="data:..." automatically provided
        // placeholder="blur" // Optional blur-up while loading
      />
      <p>Welcome to my homepage!</p>
    </>
  )
}

This also explains why remote images do need these properties.


问:
How to write a query conditionally in laravel?
I am new to the laravel, i am joining three tables based on conditions every thing is working fine but i need to write conditionally if the $field is array it should run with whereIn otherwise it should run where condition,can you please help me to acheive this thing

```<code>//$field sometimes it's an array sometimes it's a string.
public function find($field){

}
</code>```

答:
For conditional constraints, you can use the when() clause:

```<code>$query = DB::table(...);

$data = $query
    ->when(is_array($field), function ($query) use ($field) {
        $query->whereIn('my_field', $field);
    }, function ($query) use ($field) {
        $query->where('my_field', $field);
    })
    ->get();
</code>```

Now, as a tip, you could do this: Wrap the $fields variables to an array and then use always the whereIn clause. You can achieve this with the Arr::wrap() function:

```<code>use Illuminate\Support\Arr;

// ...

$query = DB::table(...);

$data = $query
    ->whereIn('my_field', Arr::wrap($field))
    ->get();
</code>```

PS: I have linked the relevant functions to the docs so you can know how they work and their params.

Body:
I am new to the laravel, i am joining three tables based on conditions every thing is working fine but i need to write conditionally if the $field is array it should run with whereIn otherwise it should run where condition,can you please help me to acheive this thing

//$field sometimes it's an array sometimes it's a string.
public function find($field){

}

Body:
For conditional constraints, you can use the when() clause:

$query = DB::table(...);

$data = $query
    ->when(is_array($field), function ($query) use ($field) {
        $query->whereIn('my_field', $field);
    }, function ($query) use ($field) {
        $query->where('my_field', $field);
    })
    ->get();

Now, as a tip, you could do this: Wrap the $fields variables to an array and then use always the whereIn clause. You can achieve this with the Arr::wrap() function:

use Illuminate\Support\Arr;

// ...

$query = DB::table(...);

$data = $query
    ->whereIn('my_field', Arr::wrap($field))
    ->get();

PS: I have linked the relevant functions to the docs so you can know how they work and their params.


问:
Webpack generates duplicate images
I have a problem, Webpack generates duplicate images, while one of the duplicates is broken.

I have an image image, and two duplicates are generated from it, a working one: image, and a non-working one: image. I 'm in CSS for a class .logo I'm hanging the background-image style: image. After compiling the code, it inserts the path to the non-working image: image, and I don't understand what's the matter :(

Please help me fix it, Thanks in advance!!!

My webpack.config.js:

```<code>const path = require('path')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')

module.exports = {
    context: path.resolve(__dirname, 'src'),
    mode: 'development',
    entry: {
        main: './index.js',
        analytics: './analytics.js'
    },
    output: {
        filename: '[name].[contenthash].js',
        path: path.resolve(__dirname, 'dist')
    },
    plugins: [
        new HTMLWebpackPlugin({
            title: 'Webpack Tenzo',
            template: './index.html'
        }),
        new CleanWebpackPlugin()
    ],
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.(png|jpg|svg|gif)$/,
                use: ['file-loader']
            }
        ]
    }
}
</code>```

答: I solved the problem. The point was that it was necessary to use "assets" instead of leaders such as "file-loader".</p>

Title: Webpack generates duplicate images

Body:
I have a problem, Webpack generates duplicate images, while one of the duplicates is broken.

I have an image image, and two duplicates are generated from it, a working one: image, and a non-working one: image. I 'm in CSS for a class .logo I'm hanging the background-image style: image. After compiling the code, it inserts the path to the non-working image: image, and I don't understand what's the matter :(

Please help me fix it, Thanks in advance!!!

My webpack.config.js:

const path = require('path')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')

module.exports = {
    context: path.resolve(__dirname, 'src'),
    mode: 'development',
    entry: {
        main: './index.js',
        analytics: './analytics.js'
    },
    output: {
        filename: '[name].[contenthash].js',
        path: path.resolve(__dirname, 'dist')
    },
    plugins: [
        new HTMLWebpackPlugin({
            title: 'Webpack Tenzo',
            template: './index.html'
        }),
        new CleanWebpackPlugin()
    ],
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.(png|jpg|svg|gif)$/,
                use: ['file-loader']
            }
        ]
    }
}


问:
How to find a minimum from every rows(if there are many minimums in a row keep the first one ) and replace else elements of matrix with 0?
The question is how to get matrix 'a' from 'sum' by using skylearn?

```<code>data_points = np.array([[1,2,0],[1,0,1],[0,1,1],[0,1,2],[1,1,0]])
centers = np.array([[0,0,0],[1,1,1],[1,1,0],[1,0,0]])
sum = np.sum((data_points[:, np.newaxis] - centers)**2,axis=2)
min = np.min(sum,axis=1)
a=np.array([[0,0,1,0],[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,0,1,0]])
print("The question is how to get matrix 'a' from 'sum' by using skylearn?\n",a)
</code>```

答:
You can use numpy.argmin to get the index of the first min value:

```<code>my_sum = np.sum((data_points[:, np.newaxis] - centers)**2,axis=2)
idx_min = np.argmin(my_sum,axis=1)

# set up output
a = np.zeros(my_sum.shape, 'int')
a[np.arange(a.shape[0]), idx_min] = 1
</code>```

NB. do not use sum and min as variable names, those are python builtins

output:

```<code>array([[0, 0, 1, 0],
       [0, 1, 0, 0],
       [0, 1, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0]])
</code>```

Body:
The question is how to get matrix 'a' from 'sum' by using skylearn?

data_points = np.array([[1,2,0],[1,0,1],[0,1,1],[0,1,2],[1,1,0]])
centers = np.array([[0,0,0],[1,1,1],[1,1,0],[1,0,0]])
sum = np.sum((data_points[:, np.newaxis] - centers)**2,axis=2)
min = np.min(sum,axis=1)
a=np.array([[0,0,1,0],[0,1,0,0],[0,1,0,0],[0,1,0,0],[0,0,1,0]])
print("The question is how to get matrix 'a' from 'sum' by using skylearn?\n",a)

Body:
You can use numpy.argmin to get the index of the first min value:

my_sum = np.sum((data_points[:, np.newaxis] - centers)**2,axis=2)
idx_min = np.argmin(my_sum,axis=1)

# set up output
a = np.zeros(my_sum.shape, 'int')
a[np.arange(a.shape[0]), idx_min] = 1

NB. do not use sum and min as variable names, those are python builtins

output:

array([[0, 0, 1, 0],
       [0, 1, 0, 0],
       [0, 1, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0]])


问:
Chartjs with Typescript: The types of 'interaction.mode' are incompatible between these types
I want to create a React Functional Component with Typescript that displays a chart using chartjs.

My parent passes options and data to the child component that renders the line chart.

When trying to pass the options object I get the following typescript error:

```<code>The types of 'interaction.mode' are incompatible between these types.
Type 'string' is not assignable to type '"index" | "dataset" | "point" | "nearest" | "x" | "y" | undefined'.
</code>```

Which is weird because my options look like this:

```<code>const options = {
    ...
    interaction: {
        ...
        mode: "index"
    },
    ...
}
</code>```

Why is "index" not assignable to "index" | ...?

I tried to recreate the error in stackblitz and although the error don't pop up in the preview it still seem to occur:

Link to Stackblitz

答:
When I change my options to

```<code>const options = {
    ...
    interaction: {
        ...
        mode: "index" as "index"
    },
    ...
}
</code>```

it does work

Body:
I want to create a React Functional Component with Typescript that displays a chart using chartjs.

My parent passes options and data to the child component that renders the line chart.

When trying to pass the options object I get the following typescript error:

The types of 'interaction.mode' are incompatible between these types.
Type 'string' is not assignable to type '"index" | "dataset" | "point" | "nearest" | "x" | "y" | undefined'.

Which is weird because my options look like this:

const options = {
    ...
    interaction: {
        ...
        mode: "index"
    },
    ...
}

Why is "index" not assignable to "index" | ...?

I tried to recreate the error in stackblitz and although the error don't pop up in the preview it still seem to occur:

Link to Stackblitz

Body:
When I change my options to

const options = {
    ...
    interaction: {
        ...
        mode: "index" as "index"
    },
    ...
}

it does work