问:
How to filter Google Drive API v3 mimeType?
I wrote a script that uses DriveApp to read Google Drive, SpreadsheetApp to log data in Sheets, and Google Drive API v3  + service account + OAuth to make changes on behalf of G Suite users.

It would be nice to search Google Drive from the target user perspective (calling Google Drive API v3) instead of the account running the script (calling DriveApp). I can't get the filter to work.

The query is built with parent folder keys, mimeType = or mimeType != for folders, and is passed into the function. The format is:

```<code>var query = "('GoogleDriveFolderKey01' in parents or 'GoogleDriveFolderKey02' in parents) and trashed = false and mimeType = 'application/vnd.google-apps.folder'"
</code>```

The DriveApp function uses:

```<code>files = Drive.Files.list({
  q: query,
  maxResults: 100,
  pageToken: pageToken
});
</code>```

The Google Drive API v3 function uses:

```<code>var url = 'https://www.googleapis.com/drive/v3/files/'
var options = {
  'contentType': 'application/json',
  'method'     : 'get',
  'headers'    : { Authorization: 'Bearer ' + service.getAccessToken() },
  'muteHttpExceptions': true,
  'corpora'    : 'domain',
  'q'          : query,
  'spaces'     : 'drive',
  'pageSize'   : 100,
  'pageToken'  : pageToken
};
var response = UrlFetchApp.fetch(url, options);

var resultParsed = JSON.parse(response.getContentText());
files = resultParsed.files
pageToken = resultParsed.pageToken
</code>```

The results with DriveApp are as expected, but Google Drive API v3 results in:

```<code>"files": [
{
  "kind": "drive#file",
  "id": "01abc123_etc",
  "name": "Something something (2021-04-15).pdf",
  "mimeType": "application/pdf"
},
{
  "kind": "drive#file",
  "id": "02ABC4321-qwertyuiop",
  "name": "Super Special Worksheet",
  "mimeType": "application/vnd.google-apps.spreadsheet"
},
{
  "kind": "drive#file",
  "id": "whatever",
  "name": "Copy of V1",
  "mimeType": "application/vnd.google-apps.folder"
},
...
</code>```

Any ideas?

Edit:

Thank you! The problem seems to be 'corpus' and 'space'. I have used to following in Google AppsScript:

```<code>var options = {
  'contentType': 'application/json',
  'method'     : 'get',
  'headers'    : { Authorization: 'Bearer ' + service.getAccessToken() },
  'muteHttpExceptions': true,
  'pageSize'   : 100
};
url += '?q=' + encodeURIComponent(query);
if ( pageToken.length > 0 ) url += '&pageToken=' + pageToken;
var response = UrlFetchApp.fetch(url, options);
</code>```

答:
How about this answer?

Modification points:

At the method of "Files: list" in Drive API v3, the values of corpora, q, space, pageSize, pageToken are used as the query parameters. I think that the reason of your issue is due to this.

At GET method, contentType is not required.

Modified script:

When your script is modified, it becomes as follows.

```<code>var query = "('GoogleDriveFolderKey01' in parents or 'GoogleDriveFolderKey02' in parents) and trashed = false and mimeType = 'application/vnd.google-apps.folder'"

var url = 'https://www.googleapis.com/drive/v3/files'  // Modified
var options = {  // Modified
  'method'     : 'get',
  'headers'    : { Authorization: 'Bearer ' + service.getAccessToken() },
  'muteHttpExceptions': true,
};
url += `?corpora=domain&q=${encodeURIComponent(query)}&spaces=drive&pageSize=100&pageToken=${pageToken}`;  // Added

var response = UrlFetchApp.fetch(url, options);
var resultParsed = JSON.parse(response.getContentText());
files = resultParsed.files
pageToken = resultParsed.pageToken
</code>```

Note:

This modified script supposes that service.getAccessToken() can be used for using the method of "Files: list" in Drive API v3.

If an error occurs please remove corpora=domain from the URL like below and test it again.

```<code>url += `?q=${encodeURIComponent(query)}&spaces=drive&pageSize=100&pageToken=${pageToken}`;
</code>```

Reference:

Files: list

Body:
I wrote a script that uses DriveApp to read Google Drive, SpreadsheetApp to log data in Sheets, and Google Drive API v3  + service account + OAuth to make changes on behalf of G Suite users.

It would be nice to search Google Drive from the target user perspective (calling Google Drive API v3) instead of the account running the script (calling DriveApp). I can't get the filter to work.

The query is built with parent folder keys, mimeType = or mimeType != for folders, and is passed into the function. The format is:

var query = "('GoogleDriveFolderKey01' in parents or 'GoogleDriveFolderKey02' in parents) and trashed = false and mimeType = 'application/vnd.google-apps.folder'"

The DriveApp function uses:

files = Drive.Files.list({
  q: query,
  maxResults: 100,
  pageToken: pageToken
});

The Google Drive API v3 function uses:

var url = 'https://www.googleapis.com/drive/v3/files/'
var options = {
  'contentType': 'application/json',
  'method'     : 'get',
  'headers'    : { Authorization: 'Bearer ' + service.getAccessToken() },
  'muteHttpExceptions': true,
  'corpora'    : 'domain',
  'q'          : query,
  'spaces'     : 'drive',
  'pageSize'   : 100,
  'pageToken'  : pageToken
};
var response = UrlFetchApp.fetch(url, options);

var resultParsed = JSON.parse(response.getContentText());
files = resultParsed.files
pageToken = resultParsed.pageToken

The results with DriveApp are as expected, but Google Drive API v3 results in:

"files": [
{
  "kind": "drive#file",
  "id": "01abc123_etc",
  "name": "Something something (2021-04-15).pdf",
  "mimeType": "application/pdf"
},
{
  "kind": "drive#file",
  "id": "02ABC4321-qwertyuiop",
  "name": "Super Special Worksheet",
  "mimeType": "application/vnd.google-apps.spreadsheet"
},
{
  "kind": "drive#file",
  "id": "whatever",
  "name": "Copy of V1",
  "mimeType": "application/vnd.google-apps.folder"
},
...

Any ideas?

Edit:

Thank you! The problem seems to be 'corpus' and 'space'. I have used to following in Google AppsScript:

var options = {
  'contentType': 'application/json',
  'method'     : 'get',
  'headers'    : { Authorization: 'Bearer ' + service.getAccessToken() },
  'muteHttpExceptions': true,
  'pageSize'   : 100
};
url += '?q=' + encodeURIComponent(query);
if ( pageToken.length > 0 ) url += '&pageToken=' + pageToken;
var response = UrlFetchApp.fetch(url, options);

Body:
How about this answer?

Modification points:

At the method of "Files: list" in Drive API v3, the values of corpora, q, space, pageSize, pageToken are used as the query parameters. I think that the reason of your issue is due to this.

At GET method, contentType is not required.

Modified script:

When your script is modified, it becomes as follows.

var query = "('GoogleDriveFolderKey01' in parents or 'GoogleDriveFolderKey02' in parents) and trashed = false and mimeType = 'application/vnd.google-apps.folder'"

var url = 'https://www.googleapis.com/drive/v3/files'  // Modified
var options = {  // Modified
  'method'     : 'get',
  'headers'    : { Authorization: 'Bearer ' + service.getAccessToken() },
  'muteHttpExceptions': true,
};
url += `?corpora=domain&q=${encodeURIComponent(query)}&spaces=drive&pageSize=100&pageToken=${pageToken}`;  // Added

var response = UrlFetchApp.fetch(url, options);
var resultParsed = JSON.parse(response.getContentText());
files = resultParsed.files
pageToken = resultParsed.pageToken

Note:

This modified script supposes that service.getAccessToken() can be used for using the method of "Files: list" in Drive API v3.

If an error occurs please remove corpora=domain from the URL like below and test it again.

url += `?q=${encodeURIComponent(query)}&spaces=drive&pageSize=100&pageToken=${pageToken}`;

Reference:

Files: list


来源: stackexchange

问:
R Loop through a group of columns to make several plots

I need to make several scatter plots from some of the columns on my data frame, so I decided to make a loop, but I don't Know how to set it to iterate just on my columns of interest.
  my code looks like this:

```<code>col_xVScol_y<-ggplot(data=df, aes(x=col_x, y=col_y)) +
geom_point(aes(color= col_z))
</code>```

col_y and col_z would always be the same, I just need to iterate over a group of columns that would take the place of col_x

I´ve seen that you can iterate over the columns with colnames(), like:

```<code>for(i in colnames(df)){
    col_xVScol_y<-ggplot(data=df, aes(x=col_x, y=col_y)) +
    geom_point(aes(color= col_z))]
}
</code>```

But this way is not suitable for my data frame, because it iterates over all columns and I´m interesed in just a group.

答:
As an alternative, you could reshape the data frame using pivot_longer from the tidyr package such that all your col_x columns (assuming they have unique column names) are stacked into a categorical column and the associated values column. With this you can use facet_wrap in your ggplot and create a facet plot that produces separate scatter plots for each combination.

```<code>library(dplyr)
library(tidyr)
library(ggplot2)

    data <- mtcars %>%
  select(mpg, cyl, disp, hp, wt) %>%
  pivot_longer(cols = c("mpg", "disp", "hp"),
               names_to = "Columns",
               values_to = "Values")

ggplot(data = data, aes(x = Values, y = wt, colour = as.factor(cyl))) +
         geom_point() +
         facet_wrap(. ~ Columns)
</code>```

Looks like this:

ContentLicense: CC BY-SA 4.0

Body:
I need to make several scatter plots from some of the columns on my data frame, so I decided to make a loop, but I don't Know how to set it to iterate just on my columns of interest.
  my code looks like this:

col_xVScol_y<-ggplot(data=df, aes(x=col_x, y=col_y)) +
geom_point(aes(color= col_z))

col_y and col_z would always be the same, I just need to iterate over a group of columns that would take the place of col_x

I´ve seen that you can iterate over the columns with colnames(), like:

for(i in colnames(df)){
    col_xVScol_y<-ggplot(data=df, aes(x=col_x, y=col_y)) +
    geom_point(aes(color= col_z))]
}

But this way is not suitable for my data frame, because it iterates over all columns and I´m interesed in just a group.

Body:
As an alternative, you could reshape the data frame using pivot_longer from the tidyr package such that all your col_x columns (assuming they have unique column names) are stacked into a categorical column and the associated values column. With this you can use facet_wrap in your ggplot and create a facet plot that produces separate scatter plots for each combination.

library(dplyr)
library(tidyr)
library(ggplot2)

    data <- mtcars %>%
  select(mpg, cyl, disp, hp, wt) %>%
  pivot_longer(cols = c("mpg", "disp", "hp"),
               names_to = "Columns",
               values_to = "Values")

ggplot(data = data, aes(x = Values, y = wt, colour = as.factor(cyl))) +
         geom_point() +
         facet_wrap(. ~ Columns)

Looks like this:


问:
Is there a way to connect to a GIT HTTPS site using a smart card on macOS?
I have a bitbucket site that supports smart cards only. It's operational and using Safari I can login using a smart card and PIN. I cannot use SSH for this instance.

Does anyone know of a client or way to use Git in a terminal command that would support a smart card?

Any git clone attempts 403 fail due to not passing a valid certificate to the site.

答:
Git is generally designed to operate on certificates in files.  However, if you're using HTTPS and your Git is built against Apple's Secure Transport library, then you can set http.sslCert to a handle to a certificate in the keychain, as documented in the libcurl documentation.  If you're using a version built against OpenSSL or GnuTLS, then no, this isn't supported.

If that doesn't work, you'll need to contact your server administrator about providing a functional approach, such as using SSH, or providing suitable documentation about how to appropriately use the server.


问:
Can JSONB GIN indexes be specified in CodeFirst EntityFramework with NPGSQL?
I've got some JSONB columns on my EF code first objects in an EFCore project.  I would like to set up some GIN indexes to properties inside those strongly typed objects being stored as JSONB property/column.

Is this possible? (currently using the postgres:latest image)

Thanks.

答:
Since .ForNpgsqlHasMethod("gin") is obsolete, with EF Core 3.1 you can use this:

```<code>metaBuilder
    .HasIndex(x => x.DocumentNumber)
    .HasMethod("gin")
    .HasOperators("gin_trgm_ops");
</code>```

and your migration will look like this:

```<code>migrationBuilder.CreateIndex(
    name: "IX_DocumentContentMeta_DocumentNumber",
    table: "DocumentContentMeta",
    column: "DocumentNumber")
    .Annotation("Npgsql:IndexMethod", "gin")
    .Annotation("Npgsql:IndexOperators", new[] { "gin_trgm_ops" });
</code>```

Body:
Since .ForNpgsqlHasMethod("gin") is obsolete, with EF Core 3.1 you can use this:

metaBuilder
    .HasIndex(x => x.DocumentNumber)
    .HasMethod("gin")
    .HasOperators("gin_trgm_ops");

and your migration will look like this:

migrationBuilder.CreateIndex(
    name: "IX_DocumentContentMeta_DocumentNumber",
    table: "DocumentContentMeta",
    column: "DocumentNumber")
    .Annotation("Npgsql:IndexMethod", "gin")
    .Annotation("Npgsql:IndexOperators", new[] { "gin_trgm_ops" });


问:
How to add dummy values to a table in SQL
Probably not the most descriptive title but sometimes it's hard to put stupid SQL problems(the kind that make you want to put booze in your coffee to get through the day) into english. I basically have the following two tables:

TABLE1

```<code>studentid   firstname   lastname    counter cmid    score   lessond
328000            Jacob           GG             5  189 90  103950
328111            Jacob           GG            6   189 80  103951
3283333           Tyler           GR            5   189 70  103950
3244444            William    FL               5    189 70  103950
3280000            Sean           DA           5    189 80  103950
3283333            Sean           DA             6  189 90  103951
</code>```

TABLE 2

```<code>Lesson2ClassroomAssignmentId    Counter
103956                      1
103947                           2
103948                          3
103949                          4
103950                          5
103951                          6
</code>```

The first table is basically a list of students and their scores for certain lessons(numbered 1-6 and denoted by counter). The second table is a list of all possible lessons(numbered 1-6 and denoted by counter).

What I need to do is get a table that has a row for each student and each lesson, regardless of whether or not the student has a row in table 1 for that lesson or not.

Ive tried doing something like selecting * from table 1 and UNIONING it with the results of the 2nd table LEFT OUTER JOINED with the first table where counter <> counter but havent been able to get the results I desired.

If anyone has any suggestions(dont need the full answer, just pointed in the right direction) I'd owe ya one!! Alternatively, feel free to make fun of me if this post is unintelligable as it barely makes sense to me and I'm the one who has been staring at it for the past 6 hours ;)

Ciao Ciao,

Tim

DESIRED OUTPUT:

The Desired output would be:

TABLE FINAL

```<code> studentid     firstname   lastname    counter cmid    score   lessond
    3280000         Jacob       GG      1 NULL, NULL, NULL
    3280000         Jacob       GG 2 NULL, NULL, NULL
    3280000         Jacob       GG 3 NULL, NULL, NULL
    3280000         Jacob       GG 4 NULL, NULL, NULL
    3280000         Jacob       GG 5 189, 90, 103950
    3280000         Jacob       GG 6 189, 80, 103951
    3283333         Tyler       GR 1, NULL, NULL, NULL
    3283333         Tyler       GR 2, NULL, NULL, NULL
    3283333         Tyler       GR 3, NULL, NULL, NULL
    3283333         Tyler       GR 4, NULL, NULL, NULL
    3283333         Tyler       GR 5, 189, 70, 103950
    3283333         Tyler       GR 6, NULL, NULL, NULL
</code>```

So basically taking any row with actual lesson data for a student from table one and supplementing it with dummy rows with no score data for each lesson(from table 2) that the student didnt have a score for.

答:
looks like you would benefit from a Student table and a Lesson table.
then you could do a unjoined select from them both to get the cross product of all rows.</p>

ContentLicense: CC BY-SA 3.0

Body:
Probably not the most descriptive title but sometimes it's hard to put stupid SQL problems(the kind that make you want to put booze in your coffee to get through the day) into english. I basically have the following two tables:

TABLE1

studentid   firstname   lastname    counter cmid    score   lessond
328000            Jacob           GG             5  189 90  103950
328111            Jacob           GG            6   189 80  103951
3283333           Tyler           GR            5   189 70  103950
3244444            William    FL               5    189 70  103950
3280000            Sean           DA           5    189 80  103950
3283333            Sean           DA             6  189 90  103951

TABLE 2

Lesson2ClassroomAssignmentId    Counter
103956                      1
103947                           2
103948                          3
103949                          4
103950                          5
103951                          6

The first table is basically a list of students and their scores for certain lessons(numbered 1-6 and denoted by counter). The second table is a list of all possible lessons(numbered 1-6 and denoted by counter).

What I need to do is get a table that has a row for each student and each lesson, regardless of whether or not the student has a row in table 1 for that lesson or not.

Ive tried doing something like selecting * from table 1 and UNIONING it with the results of the 2nd table LEFT OUTER JOINED with the first table where counter <> counter but havent been able to get the results I desired.

If anyone has any suggestions(dont need the full answer, just pointed in the right direction) I'd owe ya one!! Alternatively, feel free to make fun of me if this post is unintelligable as it barely makes sense to me and I'm the one who has been staring at it for the past 6 hours ;)

Ciao Ciao,

Tim

DESIRED OUTPUT:

The Desired output would be:

TABLE FINAL

 studentid     firstname   lastname    counter cmid    score   lessond
    3280000         Jacob       GG      1 NULL, NULL, NULL
    3280000         Jacob       GG 2 NULL, NULL, NULL
    3280000         Jacob       GG 3 NULL, NULL, NULL
    3280000         Jacob       GG 4 NULL, NULL, NULL
    3280000         Jacob       GG 5 189, 90, 103950
    3280000         Jacob       GG 6 189, 80, 103951
    3283333         Tyler       GR 1, NULL, NULL, NULL
    3283333         Tyler       GR 2, NULL, NULL, NULL
    3283333         Tyler       GR 3, NULL, NULL, NULL
    3283333         Tyler       GR 4, NULL, NULL, NULL
    3283333         Tyler       GR 5, 189, 70, 103950
    3283333         Tyler       GR 6, NULL, NULL, NULL

So basically taking any row with actual lesson data for a student from table one and supplementing it with dummy rows with no score data for each lesson(from table 2) that the student didnt have a score for.


问:
How to close the channel after all producer coroutines are done?
Consider the following code:

```<code>import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.*

fun main() = runBlocking<Unit> {
    val channel = Channel<String>()
    launch {
        channel.send("A1")
        channel.send("A2")
        log("A done")
    }
    launch {
        channel.send("B1")
        log("B done")
    }
    launch {
        for (x in channel) {
            log(x)
        }
    }
}

fun log(message: Any?) {
    println("[${Thread.currentThread().name}] $message")
}
</code>```

The original version has the receiver coroutine like that:

```<code>launch {
        repeat(3) {
            val x = channel.receive()
            log(x)
        }
    }
</code>```

It expects only 3 messages in the channel. If I change it to the first version then I need to close the channel after all producer coroutines are done. How can I do that?

答:
A possible solution is to create a job that will wait for all channel.send() to finish, and call channel.close() in the invokeOnCompletion of this job:

```<code>import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.*

fun main() = runBlocking<Unit> {
    val channel = Channel<String>()
    launch {
      launch {
          channel.send("A1")
          channel.send("A2")
          log("A done")
      }
      launch {
          channel.send("B1")
          log("B done")
      }
    }.invokeOnCompletion {
        channel.close()
    }
    launch {
        for (x in channel) {
            log(x)
        }
    }
}

fun log(message: Any?) {
    println("[${Thread.currentThread().name}] $message")
}
</code>```

Body:
Consider the following code:

import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.*

fun main() = runBlocking<Unit> {
    val channel = Channel<String>()
    launch {
        channel.send("A1")
        channel.send("A2")
        log("A done")
    }
    launch {
        channel.send("B1")
        log("B done")
    }
    launch {
        for (x in channel) {
            log(x)
        }
    }
}

fun log(message: Any?) {
    println("[${Thread.currentThread().name}] $message")
}

The original version has the receiver coroutine like that:

launch {
        repeat(3) {
            val x = channel.receive()
            log(x)
        }
    }

It expects only 3 messages in the channel. If I change it to the first version then I need to close the channel after all producer coroutines are done. How can I do that?

Body:
A possible solution is to create a job that will wait for all channel.send() to finish, and call channel.close() in the invokeOnCompletion of this job:

import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.*

fun main() = runBlocking<Unit> {
    val channel = Channel<String>()
    launch {
      launch {
          channel.send("A1")
          channel.send("A2")
          log("A done")
      }
      launch {
          channel.send("B1")
          log("B done")
      }
    }.invokeOnCompletion {
        channel.close()
    }
    launch {
        for (x in channel) {
            log(x)
        }
    }
}

fun log(message: Any?) {
    println("[${Thread.currentThread().name}] $message")
}


问:
Mock an Autowired ExecutorService
Abstract:

I have a Spring @Component that uses an autowired ExecutorService as a work pool. I'm using JUnit and Mockito to test the functionality of the component and I need to mock that Executor Service. This has been trivial for other autowired members - a generic helper, and a DAO layer for instance are easily mocked, but I need a real Executor Service.

Code:

```<code>@RunWith(MockitoJUnitRunner.class)
public class MadeUpClassNameTest{

  @Mock
  private ExecutorService executor;

  @Before
  public void initExecutor() throws Exception{
      executor = Executors.newFixedThreadPool(2);
  }

  @InjectMocks
  private ASDF componentBeingAutowired;
...
</code>```

This alone doesn't work, the results of invokeAll() is always an empty list.

Attempting to more explicitly mock the executor method also doesn't work...

```<code>@Test
public void myTestMethod(){
    when(executor.invokeAll(anyCollection()))
        .thenCallRealMethod();
    ...
}
</code>```

I get the cryptically worded exception:

You cannot use argument matchers outside of verification or stubbing.

(I thought this was a stubbing ?)

I could provide a thenReturn(Answer<>) method, but I'd like to make sure that the code actually works with an executor, a fair bit of the code is devoted to mapping the results of Futures.

Problem
How do I provide a real (or functionally usable mock) Executor Service ? Alternatively, is my difficulty in testing this component a sign that this is a poor design in need of refactoring, or possibly a bad test scenario ?

Notes
I want to stress that my problem is NOT getting Mockito or Junit set up. Other mocks and tests work correctly. My problem is specific only to the particular mock above.

Using: Junit 4.12, Mockito 1.10.19, Hamcrest 1.3

答:
You can use the @Spy annotation.</p>

```<code>@RunWith(MockitoJUnitRunner.class)
public class MadeUpClassNameTest{
@Spy
private final ExecutorService executor = Executors.newFixedThreadPool(2);

@Test
...
}
</code>```

Title: Mock an Autowired ExecutorService

Body:
Abstract:

I have a Spring @Component that uses an autowired ExecutorService as a work pool. I'm using JUnit and Mockito to test the functionality of the component and I need to mock that Executor Service. This has been trivial for other autowired members - a generic helper, and a DAO layer for instance are easily mocked, but I need a real Executor Service.

Code:

@RunWith(MockitoJUnitRunner.class)
public class MadeUpClassNameTest{

  @Mock
  private ExecutorService executor;

  @Before
  public void initExecutor() throws Exception{
      executor = Executors.newFixedThreadPool(2);
  }

  @InjectMocks
  private ASDF componentBeingAutowired;
...

This alone doesn't work, the results of invokeAll() is always an empty list.

Attempting to more explicitly mock the executor method also doesn't work...

@Test
public void myTestMethod(){
    when(executor.invokeAll(anyCollection()))
        .thenCallRealMethod();
    ...
}

I get the cryptically worded exception:

You cannot use argument matchers outside of verification or stubbing.

(I thought this was a stubbing ?)

I could provide a thenReturn(Answer<>) method, but I'd like to make sure that the code actually works with an executor, a fair bit of the code is devoted to mapping the results of Futures.

Problem
How do I provide a real (or functionally usable mock) Executor Service ? Alternatively, is my difficulty in testing this component a sign that this is a poor design in need of refactoring, or possibly a bad test scenario ?

Notes
I want to stress that my problem is NOT getting Mockito or Junit set up. Other mocks and tests work correctly. My problem is specific only to the particular mock above.

Using: Junit 4.12, Mockito 1.10.19, Hamcrest 1.3

Body:
You can use the @Spy annotation.

@RunWith(MockitoJUnitRunner.class)
public class MadeUpClassNameTest{
@Spy
private final ExecutorService executor = Executors.newFixedThreadPool(2);

@Test
...
}


问:
ASP.NET Core 2.0 combining Cookies and Bearer Authorization for the same endpoint
I've created a new ASP.NET Core Web Application project in VS17 using the "Web Application (Model-View-Controller)" template and ".Net Framework" + "ASP.NET Core 2" as the configuration. The authentication config is set to "Individual User Accounts".

I have the following sample endpoint:

```<code>[Produces("application/json")]
[Route("api/price")]
[Authorize(Roles = "PriceViwer", AuthenticationSchemes = "Cookies,Bearer")]
public class PriceController : Controller
{

    public IActionResult Get()
    {
        return Ok(new Dictionary<string, string> { {"Galleon/Pound",
                                                   "999.999" } );
    }
}
</code>```

"Cookies,Bearer" is derived by  concatenating CookieAuthenticationDefaults.AuthenticationScheme and JwtBearerDefaults.AuthenticationScheme.

The objective is to be able to configure the authorization for the end point so that it's possible access it using both the token and cookie authentication methods.

Here is the setup I have for Authentication in my Startup.cs:

```<code>    services.AddAuthentication()
        .AddCookie(cfg => { cfg.SlidingExpiration = true;})
        .AddJwtBearer(cfg => {
            cfg.RequireHttpsMetadata = false;
            cfg.SaveToken = true;
            cfg.TokenValidationParameters = new TokenValidationParameters() {
                                                    ValidIssuer = Configuration["Tokens:Issuer"],
                                                    ValidAudience = Configuration["Tokens:Issuer"],
                                                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Tokens:Key"]))
                                                };
        });
</code>```

So, when I try to access the endpoint using a browser, I get the 401 response with a blank html page.

Then I login and when I try to access the endpoint again, I get the same response.

Then, I try to access the endpoint by specifying the bearer token. And that returns the desired result with the 200 response.

So then, if I remove [Authorize(AuthenticationSchemes = "Cookies,Bearer")], the situation becomes the opposite - cookie authentication works and returns 200, however the same bearer token method as used above doesn't give any results and just redirect to the default AspIdentity login page.

I can see two possible problems here:

1) ASP.NET Core doesn't allow 'combined' authentication.
2) 'Cookies' is not a valid schema name. But then what is the right one to use?

Please advise. Thank you.

答:
After many hours of research and head-scratching, this is what worked for me in ASP.NET Core 2.2 -> ASP.NET 5.0:

Use .AddCookie() and .AddJwtBearer() to configure the schemes

Use a custom policy scheme to forward to the correct Authentication Scheme.

You do not need to specify the scheme on each controller action and will work for both. [Authorize] is enough.

```<code>services.AddAuthentication( config =>
{
    config.DefaultScheme = "smart";
} )
.AddPolicyScheme( "smart", "Bearer or Jwt", options =>
{
    options.ForwardDefaultSelector = context =>
    {
        var bearerAuth = context.Request.Headers["Authorization"].FirstOrDefault()?.StartsWith( "Bearer " ) ?? false;
        // You could also check for the actual path here if that's your requirement:
        // eg: if (context.HttpContext.Request.Path.StartsWithSegments("/api", StringComparison.InvariantCulture))
        if ( bearerAuth )
            return JwtBearerDefaults.AuthenticationScheme;
        else
            return CookieAuthenticationDefaults.AuthenticationScheme;
    };
} )
.AddCookie( CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
    options.LoginPath = new PathString( "/Account/Login" );
    options.AccessDeniedPath = new PathString( "/Account/Login" );
    options.LogoutPath = new PathString( "/Account/Logout" );
    options.Cookie.Name = "CustomerPortal.Identity";
    options.SlidingExpiration = true;
    options.ExpireTimeSpan = TimeSpan.FromDays( 1 ); //Account.Login overrides this default value
} )
.AddJwtBearer( JwtBearerDefaults.AuthenticationScheme, options =>
{
    options.RequireHttpsMetadata = false;
    options.SaveToken = true;
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuerSigningKey = true,
        IssuerSigningKey = new SymmetricSecurityKey( key ),
        ValidateIssuer = false,
        ValidateAudience = false
    };
} );

services.AddAuthorization( options =>
{
    options.DefaultPolicy = new AuthorizationPolicyBuilder( CookieAuthenticationDefaults.AuthenticationScheme, JwtBearerDefaults.AuthenticationScheme )
        .RequireAuthenticatedUser()
        .Build();
} );
</code>```

Body:
I've created a new ASP.NET Core Web Application project in VS17 using the "Web Application (Model-View-Controller)" template and ".Net Framework" + "ASP.NET Core 2" as the configuration. The authentication config is set to "Individual User Accounts".

I have the following sample endpoint:

[Produces("application/json")]
[Route("api/price")]
[Authorize(Roles = "PriceViwer", AuthenticationSchemes = "Cookies,Bearer")]
public class PriceController : Controller
{

    public IActionResult Get()
    {
        return Ok(new Dictionary<string, string> { {"Galleon/Pound",
                                                   "999.999" } );
    }
}

"Cookies,Bearer" is derived by  concatenating CookieAuthenticationDefaults.AuthenticationScheme and JwtBearerDefaults.AuthenticationScheme.

The objective is to be able to configure the authorization for the end point so that it's possible access it using both the token and cookie authentication methods.

Here is the setup I have for Authentication in my Startup.cs:

    services.AddAuthentication()
        .AddCookie(cfg => { cfg.SlidingExpiration = true;})
        .AddJwtBearer(cfg => {
            cfg.RequireHttpsMetadata = false;
            cfg.SaveToken = true;
            cfg.TokenValidationParameters = new TokenValidationParameters() {
                                                    ValidIssuer = Configuration["Tokens:Issuer"],
                                                    ValidAudience = Configuration["Tokens:Issuer"],
                                                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Tokens:Key"]))
                                                };
        });

So, when I try to access the endpoint using a browser, I get the 401 response with a blank html page.

Then I login and when I try to access the endpoint again, I get the same response.

Then, I try to access the endpoint by specifying the bearer token. And that returns the desired result with the 200 response.

So then, if I remove [Authorize(AuthenticationSchemes = "Cookies,Bearer")], the situation becomes the opposite - cookie authentication works and returns 200, however the same bearer token method as used above doesn't give any results and just redirect to the default AspIdentity login page.

I can see two possible problems here:

1) ASP.NET Core doesn't allow 'combined' authentication.
2) 'Cookies' is not a valid schema name. But then what is the right one to use?

Please advise. Thank you.

Body:
After many hours of research and head-scratching, this is what worked for me in ASP.NET Core 2.2 -> ASP.NET 5.0:

Use .AddCookie() and .AddJwtBearer() to configure the schemes

Use a custom policy scheme to forward to the correct Authentication Scheme.

You do not need to specify the scheme on each controller action and will work for both. [Authorize] is enough.

services.AddAuthentication( config =>
{
    config.DefaultScheme = "smart";
} )
.AddPolicyScheme( "smart", "Bearer or Jwt", options =>
{
    options.ForwardDefaultSelector = context =>
    {
        var bearerAuth = context.Request.Headers["Authorization"].FirstOrDefault()?.StartsWith( "Bearer " ) ?? false;
        // You could also check for the actual path here if that's your requirement:
        // eg: if (context.HttpContext.Request.Path.StartsWithSegments("/api", StringComparison.InvariantCulture))
        if ( bearerAuth )
            return JwtBearerDefaults.AuthenticationScheme;
        else
            return CookieAuthenticationDefaults.AuthenticationScheme;
    };
} )
.AddCookie( CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
    options.LoginPath = new PathString( "/Account/Login" );
    options.AccessDeniedPath = new PathString( "/Account/Login" );
    options.LogoutPath = new PathString( "/Account/Logout" );
    options.Cookie.Name = "CustomerPortal.Identity";
    options.SlidingExpiration = true;
    options.ExpireTimeSpan = TimeSpan.FromDays( 1 ); //Account.Login overrides this default value
} )
.AddJwtBearer( JwtBearerDefaults.AuthenticationScheme, options =>
{
    options.RequireHttpsMetadata = false;
    options.SaveToken = true;
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuerSigningKey = true,
        IssuerSigningKey = new SymmetricSecurityKey( key ),
        ValidateIssuer = false,
        ValidateAudience = false
    };
} );

services.AddAuthorization( options =>
{
    options.DefaultPolicy = new AuthorizationPolicyBuilder( CookieAuthenticationDefaults.AuthenticationScheme, JwtBearerDefaults.AuthenticationScheme )
        .RequireAuthenticatedUser()
        .Build();
} );


问:
Is it performance matters to separate select fields to separate table?
I have patients table
It's contain smoking field it's values will be (smoker or non-smoker or ex-smoker)

My question is should I save values directly as string in patient table or make another table for smoking and make one-to-one relation between these tables?

Is it affects performance of query

答:
When I have three options like this, I typically use a nullable bit/bool. A bit/bool column will be more performant than a string column.

So, in your situation, my table would have a "smoking" column where null would be non-smoker, 0/false would be ex-smoker, and 1/true would be smoker.

If you think you might add options later, like maybe "recently quit" or something, a nullable bool might not be ideal as you'd have to alter the column, then write a script to convert all of them into the appropriate string.

If you don't want to use the nullable bool, then you'd want a one to many table where you listed each option: "smoker, non-smoker, ex-smoker" then add a foreign key onto the "smoking" table in your patients table. That would also increase performance over throwing strings in the column.


问:
Cannot display my rails 4 app in iframe even if 'X-Frame-Options' is 'ALLOWALL'
I am trying to test a responsive design. I am using Rails 4.
I know it sets 'X-Frame-Options' to SAME ORIGIN. So I overrided it in development.rb using

```<code>config.action_dispatch.default_headers = {
    'X-Frame-Options' => 'ALLOWALL'
  }
</code>```

and it worked. I checked out the network request in the Chrome console and it is as follows:

But still websites like responsive.is and responsinator.com give me below error:

```<code>Refused to display 'http://localhost:3000/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'. about:blank:1
</code>```

Whats going on??

答:
I just wanted to give an updated answer here on dealing with embedding a Rails app in an iframe.

Its not a great idea to simply delete X-Frame-Options headers without having some other kind of security enforced to prevent against Clickjacking (which is the vulnerability X-Frame-Options is largely trying to protect you from).

The problem is that the X-Frame-Options 'ALLOW-FROM' option is not accepted on most major browsers anymore.

As of writing this, May 28th 2020, the best solution for preventing Clickjacking and hosting your app in an iframe is to implement a Content-Security-Policy and set a 'frame_ancestors' policy. The 'frame_ancestors' key designates what domains can embed your app as an iframe. Its currently supported by major browsers and overrides your X-Frame-Options.

You can set up a Content-Security-Policy with Rails 5.2 in an initializer (example below), and for Rails < 5.2 you can use a gem like the Secure Headers gem: https://github.com/github/secure_headers

You can also override the policy specifications on a controller/action basis if you'd like.

Content-Security-Policies are great for advanced security protections. Check out all the things you can configure in the Rails docs: https://edgeguides.rubyonrails.org/security.html

A Rails 5.2 example for a Content-Security-Policy:

```<code># config/initializers/content_security_policy.rb
Rails.application.config.content_security_policy do |policy|
  policy.frame_ancestors :self, 'some_website_that_embeds_your_app.com'
end
</code>```

An example of a controller specific change to a policy:

```<code># Override policy inline
class PostsController < ApplicationController
  content_security_policy do |p|
    p.frame_ancestors :self, 'some_other_website_that_can_embed_posts.com'
  end
end
</code>```

Body:
I am trying to test a responsive design. I am using Rails 4.
I know it sets 'X-Frame-Options' to SAME ORIGIN. So I overrided it in development.rb using

config.action_dispatch.default_headers = {
    'X-Frame-Options' => 'ALLOWALL'
  }

and it worked. I checked out the network request in the Chrome console and it is as follows:

But still websites like responsive.is and responsinator.com give me below error:

Refused to display 'http://localhost:3000/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'. about:blank:1

Whats going on??

Body:
I just wanted to give an updated answer here on dealing with embedding a Rails app in an iframe.

Its not a great idea to simply delete X-Frame-Options headers without having some other kind of security enforced to prevent against Clickjacking (which is the vulnerability X-Frame-Options is largely trying to protect you from).

The problem is that the X-Frame-Options 'ALLOW-FROM' option is not accepted on most major browsers anymore.

As of writing this, May 28th 2020, the best solution for preventing Clickjacking and hosting your app in an iframe is to implement a Content-Security-Policy and set a 'frame_ancestors' policy. The 'frame_ancestors' key designates what domains can embed your app as an iframe. Its currently supported by major browsers and overrides your X-Frame-Options.

You can set up a Content-Security-Policy with Rails 5.2 in an initializer (example below), and for Rails < 5.2 you can use a gem like the Secure Headers gem: https://github.com/github/secure_headers

You can also override the policy specifications on a controller/action basis if you'd like.

Content-Security-Policies are great for advanced security protections. Check out all the things you can configure in the Rails docs: https://edgeguides.rubyonrails.org/security.html

A Rails 5.2 example for a Content-Security-Policy:

# config/initializers/content_security_policy.rb
Rails.application.config.content_security_policy do |policy|
  policy.frame_ancestors :self, 'some_website_that_embeds_your_app.com'
end

An example of a controller specific change to a policy:

# Override policy inline
class PostsController < ApplicationController
  content_security_policy do |p|
    p.frame_ancestors :self, 'some_other_website_that_can_embed_posts.com'
  end
end


问:
First audio CMSampleBuffer lost when reading mp4 file using AVAssetReader
I'm using AVAssetWriter to write audio CMSampleBuffer to an mp4 file, but when I later read that file using AVAssetReader, it seems to be missing the initial chunk of data.

Here's the debug description of the first CMSampleBuffer passed to writer input append method (notice the priming duration attachement of 1024/44_100):

```<code>CMSampleBuffer 0x102ea5b60 retainCount: 7 allocator: 0x1c061f840
            invalid = NO
            dataReady = YES
            makeDataReadyCallback = 0x0
            makeDataReadyRefcon = 0x0
            buffer-level attachments:
                TrimDurationAtStart    = {
            epoch = 0;
            flags = 1;
            timescale = 44100;
            value = 1024;
        }
            formatDescription = <CMAudioFormatDescription 0x281fd9720 [0x1c061f840]> {
            mediaType:'soun'
            mediaSubType:'aac '
            mediaSpecific: {
                ASBD: {
                    mSampleRate: 44100.000000
                    mFormatID: 'aac '
                    mFormatFlags: 0x2
                    mBytesPerPacket: 0
                    mFramesPerPacket: 1024
                    mBytesPerFrame: 0
                    mChannelsPerFrame: 2
                    mBitsPerChannel: 0  }
                cookie: {<CFData 0x2805f50a0 [0x1c061f840]>{length = 39, capacity = 39, bytes = 0x03808080220000000480808014401400 ... 1210068080800102}}
                ACL: {(null)}
                FormatList Array: {
                    Index: 0
                    ChannelLayoutTag: 0x650002
                    ASBD: {
                    mSampleRate: 44100.000000
                    mFormatID: 'aac '
                    mFormatFlags: 0x0
                    mBytesPerPacket: 0
                    mFramesPerPacket: 1024
                    mBytesPerFrame: 0
                    mChannelsPerFrame: 2
                    mBitsPerChannel: 0  }}
            }
            extensions: {(null)}
        }
            sbufToTrackReadiness = 0x0
            numSamples = 1
            outputPTS = {6683542167/44100 = 151554.244, rounded}(based on cachedOutputPresentationTimeStamp)
            sampleTimingArray[1] = {
                {PTS = {6683541143/44100 = 151554.221, rounded}, DTS = {6683541143/44100 = 151554.221, rounded}, duration = {1024/44100 = 0.023}},
            }
            sampleSizeArray[1] = {
                sampleSize = 163,
            }
            dataBuffer = 0x281cc7a80
</code>```

Here's the debug description of the second CMSampleBuffer (notice the priming duration attachement of 1088/44_100, which combined with the previous trim duration yields the standard value of 2112):

```<code>CMSampleBuffer 0x102e584f0 retainCount: 7 allocator: 0x1c061f840
    invalid = NO
    dataReady = YES
    makeDataReadyCallback = 0x0
    makeDataReadyRefcon = 0x0
    buffer-level attachments:
        TrimDurationAtStart    = {
    epoch = 0;
    flags = 1;
    timescale = 44100;
    value = 1088;
}
    formatDescription = <CMAudioFormatDescription 0x281fd9720 [0x1c061f840]> {
    mediaType:'soun'
    mediaSubType:'aac '
    mediaSpecific: {
        ASBD: {
            mSampleRate: 44100.000000
            mFormatID: 'aac '
            mFormatFlags: 0x2
            mBytesPerPacket: 0
            mFramesPerPacket: 1024
            mBytesPerFrame: 0
            mChannelsPerFrame: 2
            mBitsPerChannel: 0  }
        cookie: {<CFData 0x2805f50a0 [0x1c061f840]>{length = 39, capacity = 39, bytes = 0x03808080220000000480808014401400 ... 1210068080800102}}
        ACL: {(null)}
        FormatList Array: {
            Index: 0
            ChannelLayoutTag: 0x650002
            ASBD: {
            mSampleRate: 44100.000000
            mFormatID: 'aac '
            mFormatFlags: 0x0
            mBytesPerPacket: 0
            mFramesPerPacket: 1024
            mBytesPerFrame: 0
            mChannelsPerFrame: 2
            mBitsPerChannel: 0  }}
    }
    extensions: {(null)}
}
    sbufToTrackReadiness = 0x0
    numSamples = 1
    outputPTS = {6683543255/44100 = 151554.269, rounded}(based on cachedOutputPresentationTimeStamp)
    sampleTimingArray[1] = {
        {PTS = {6683542167/44100 = 151554.244, rounded}, DTS = {6683542167/44100 = 151554.244, rounded}, duration = {1024/44100 = 0.023}},
    }
    sampleSizeArray[1] = {
        sampleSize = 179,
    }
    dataBuffer = 0x281cc4750
</code>```

Now, when I read the audio track using AVAssetReader, the first CMSampleBuffer I get is:

```<code>CMSampleBuffer 0x102ed7b20 retainCount: 7 allocator: 0x1c061f840
    invalid = NO
    dataReady = YES
    makeDataReadyCallback = 0x0
    makeDataReadyRefcon = 0x0
    buffer-level attachments:
        EmptyMedia(P) = true
    formatDescription = (null)
    sbufToTrackReadiness = 0x0
    numSamples = 0
    outputPTS = {0/1 = 0.000}(based on outputPresentationTimeStamp)
    sampleTimingArray[1] = {
        {PTS = {0/1 = 0.000}, DTS = {INVALID}, duration = {0/1 = 0.000}},
    }
    dataBuffer = 0x0
</code>```

and the next one is contains priming info of 1088/44_100:

```<code>CMSampleBuffer 0x10318bc00 retainCount: 7 allocator: 0x1c061f840
    invalid = NO
    dataReady = YES
    makeDataReadyCallback = 0x0
    makeDataReadyRefcon = 0x0
    buffer-level attachments:
        FillDiscontinuitiesWithSilence(P) = true
        GradualDecoderRefresh(P) = 1
        TrimDurationAtStart(P) = {
    epoch = 0;
    flags = 1;
    timescale = 44100;
    value = 1088;
}
        IsGradualDecoderRefreshAuthoritative(P) = false
    formatDescription = <CMAudioFormatDescription 0x281fdcaa0 [0x1c061f840]> {
    mediaType:'soun'
    mediaSubType:'aac '
    mediaSpecific: {
        ASBD: {
            mSampleRate: 44100.000000
            mFormatID: 'aac '
            mFormatFlags: 0x0
            mBytesPerPacket: 0
            mFramesPerPacket: 1024
            mBytesPerFrame: 0
            mChannelsPerFrame: 2
            mBitsPerChannel: 0  }
        cookie: {<CFData 0x2805f3800 [0x1c061f840]>{length = 39, capacity = 39, bytes = 0x03808080220000000480808014401400 ... 1210068080800102}}
        ACL: {Stereo (L R)}
        FormatList Array: {
            Index: 0
            ChannelLayoutTag: 0x650002
            ASBD: {
            mSampleRate: 44100.000000
            mFormatID: 'aac '
            mFormatFlags: 0x0
            mBytesPerPacket: 0
            mFramesPerPacket: 1024
            mBytesPerFrame: 0
            mChannelsPerFrame: 2
            mBitsPerChannel: 0  }}
    }
    extensions: {{
    VerbatimISOSampleEntry = {length = 87, bytes = 0x00000057 6d703461 00000000 00000001 ... 12100680 80800102 };
}}
}
    sbufToTrackReadiness = 0x0
    numSamples = 43
    outputPTS = {83/600 = 0.138}(based on outputPresentationTimeStamp)
    sampleTimingArray[1] = {
        {PTS = {1024/44100 = 0.023}, DTS = {1024/44100 = 0.023}, duration = {1024/44100 = 0.023}},
    }
    sampleSizeArray[43] = {
        sampleSize = 179,
        sampleSize = 173,
        sampleSize = 178,
        sampleSize = 172,
        sampleSize = 172,
        sampleSize = 159,
        sampleSize = 180,
        sampleSize = 200,
        sampleSize = 187,
        sampleSize = 189,
        sampleSize = 206,
        sampleSize = 192,
        sampleSize = 195,
        sampleSize = 186,
        sampleSize = 183,
        sampleSize = 189,
        sampleSize = 211,
        sampleSize = 198,
        sampleSize = 204,
        sampleSize = 211,
        sampleSize = 204,
        sampleSize = 202,
        sampleSize = 218,
        sampleSize = 210,
        sampleSize = 206,
        sampleSize = 207,
        sampleSize = 221,
        sampleSize = 219,
        sampleSize = 236,
        sampleSize = 219,
        sampleSize = 227,
        sampleSize = 225,
        sampleSize = 225,
        sampleSize = 229,
        sampleSize = 225,
        sampleSize = 236,
        sampleSize = 233,
        sampleSize = 231,
        sampleSize = 249,
        sampleSize = 234,
        sampleSize = 250,
        sampleSize = 249,
        sampleSize = 259,
    }
    dataBuffer = 0x281cde370
</code>```

The input append method keeps returning true which in principle means that all sample buffers got appended, but the reader for some reason skips the first chunk of data. Is there anything I'm doing wrong here?

I'm using the following code to read the file:

```<code>let asset = AVAsset(url: fileURL)
guard let assetReader = try? AVAssetReader(asset: asset) else {
    return
}

asset.loadValuesAsynchronously(forKeys: ["tracks"]) { in
    guard let audioTrack = asset.tracks(withMediaType: .audio).first else { return }
    let audioOutput = AVAssetReaderTrackOutput(track: audioTrack, outputSettings: nil)
    assetReader.startReading()

    while assetReader.status == .reading {
        if let sampleBuffer = audioOutput.copyNextSampleBuffer() {
            // do something
        }
    }
}
</code>```

答:
First some pedantry: you haven't lost your first sample buffer, but rather the first packet within your first sample buffer.

The behaviour of AVAssetReader with nil outputSettings when reading AAC packet data has changed on iOS 13 and macOS 10.15 (Catalina).

Previously you would get the first AAC packet, that packet's presentation timestamp (zero) and a trim attachment instructing you to discard the usual first 2112 frames of decoded audio.

Now [iOS 13, macOS 10.15] AVAssetReader seems to discard the first packet, leaving you the second packet, whose presentation timestamp is 1024, and you need only discard 2112 - 1024 = 1088 of the decoded frames.

Something that might not be immediately obvious in the above situations is that AVAssetReader is talking about TWO timelines, not one. The packet timestamps are referred to one, the untrimmed timeline, and the trim instruction implies the existence of another: the untrimmed timeline.

The transformation from untrimmed to trimmed timestamps is very simple, it's usually trimmed = untrimmed - 2112.

So is the new behaviour a bug? The fact that if you decode to LPCM and correctly follow the trim instructions, then you should still get the same audio,  leads me believe the change was intentional (NB: I haven't yet personally confirmed the LPCM samples are the same).

However, the documentation says:

A value of nil for outputSettings configures the output to vend samples in their original format as stored by the specified track.

I don't think you can both discard packets [even the first one, which is basically a constant] and claim to be vending samples in their "original format", so from this point of view I think the change has a bug-like quality.

I also think it's an unfortunate change as I used to consider nil outputSettings AVAssetReader to be a sort of "raw" mode, but now it assumes your only use case is decoding to LPCM.

There's only one thing that could downgrade "unfortunate" to "serious bug", and that's if this new "let's pretend the first AAC packet doesn't exist" approach extends to files created with AVAssetWriter because that would break interoperability with non-AVAssetReader code, where the number of frames to trim has congealed to a constant 2112 frames. I also haven't personally confirmed this. Do you have a file created with the above sample buffers that you can share?

p.s. I don't think your input sample buffers are relevant here, I think you'd lose the first packet reading from any AAC file. However your input sample buffers seem slightly unusual in that they have hosttime [capture session?] style timestamps, yet are AAC, and only have one packet per sample buffer, which isn't very many and seems like a lot of overhead for 23ms of audio. Are you creating them them yourself in an AVCaptureSession -> AVAudioConverter chain?

Body:
I'm using AVAssetWriter to write audio CMSampleBuffer to an mp4 file, but when I later read that file using AVAssetReader, it seems to be missing the initial chunk of data.

Here's the debug description of the first CMSampleBuffer passed to writer input append method (notice the priming duration attachement of 1024/44_100):

CMSampleBuffer 0x102ea5b60 retainCount: 7 allocator: 0x1c061f840
            invalid = NO
            dataReady = YES
            makeDataReadyCallback = 0x0
            makeDataReadyRefcon = 0x0
            buffer-level attachments:
                TrimDurationAtStart    = {
            epoch = 0;
            flags = 1;
            timescale = 44100;
            value = 1024;
        }
            formatDescription = <CMAudioFormatDescription 0x281fd9720 [0x1c061f840]> {
            mediaType:'soun'
            mediaSubType:'aac '
            mediaSpecific: {
                ASBD: {
                    mSampleRate: 44100.000000
                    mFormatID: 'aac '
                    mFormatFlags: 0x2
                    mBytesPerPacket: 0
                    mFramesPerPacket: 1024
                    mBytesPerFrame: 0
                    mChannelsPerFrame: 2
                    mBitsPerChannel: 0  }
                cookie: {<CFData 0x2805f50a0 [0x1c061f840]>{length = 39, capacity = 39, bytes = 0x03808080220000000480808014401400 ... 1210068080800102}}
                ACL: {(null)}
                FormatList Array: {
                    Index: 0
                    ChannelLayoutTag: 0x650002
                    ASBD: {
                    mSampleRate: 44100.000000
                    mFormatID: 'aac '
                    mFormatFlags: 0x0
                    mBytesPerPacket: 0
                    mFramesPerPacket: 1024
                    mBytesPerFrame: 0
                    mChannelsPerFrame: 2
                    mBitsPerChannel: 0  }}
            }
            extensions: {(null)}
        }
            sbufToTrackReadiness = 0x0
            numSamples = 1
            outputPTS = {6683542167/44100 = 151554.244, rounded}(based on cachedOutputPresentationTimeStamp)
            sampleTimingArray[1] = {
                {PTS = {6683541143/44100 = 151554.221, rounded}, DTS = {6683541143/44100 = 151554.221, rounded}, duration = {1024/44100 = 0.023}},
            }
            sampleSizeArray[1] = {
                sampleSize = 163,
            }
            dataBuffer = 0x281cc7a80

Here's the debug description of the second CMSampleBuffer (notice the priming duration attachement of 1088/44_100, which combined with the previous trim duration yields the standard value of 2112):

CMSampleBuffer 0x102e584f0 retainCount: 7 allocator: 0x1c061f840
    invalid = NO
    dataReady = YES
    makeDataReadyCallback = 0x0
    makeDataReadyRefcon = 0x0
    buffer-level attachments:
        TrimDurationAtStart    = {
    epoch = 0;
    flags = 1;
    timescale = 44100;
    value = 1088;
}
    formatDescription = <CMAudioFormatDescription 0x281fd9720 [0x1c061f840]> {
    mediaType:'soun'
    mediaSubType:'aac '
    mediaSpecific: {
        ASBD: {
            mSampleRate: 44100.000000
            mFormatID: 'aac '
            mFormatFlags: 0x2
            mBytesPerPacket: 0
            mFramesPerPacket: 1024
            mBytesPerFrame: 0
            mChannelsPerFrame: 2
            mBitsPerChannel: 0  }
        cookie: {<CFData 0x2805f50a0 [0x1c061f840]>{length = 39, capacity = 39, bytes = 0x03808080220000000480808014401400 ... 1210068080800102}}
        ACL: {(null)}
        FormatList Array: {
            Index: 0
            ChannelLayoutTag: 0x650002
            ASBD: {
            mSampleRate: 44100.000000
            mFormatID: 'aac '
            mFormatFlags: 0x0
            mBytesPerPacket: 0
            mFramesPerPacket: 1024
            mBytesPerFrame: 0
            mChannelsPerFrame: 2
            mBitsPerChannel: 0  }}
    }
    extensions: {(null)}
}
    sbufToTrackReadiness = 0x0
    numSamples = 1
    outputPTS = {6683543255/44100 = 151554.269, rounded}(based on cachedOutputPresentationTimeStamp)
    sampleTimingArray[1] = {
        {PTS = {6683542167/44100 = 151554.244, rounded}, DTS = {6683542167/44100 = 151554.244, rounded}, duration = {1024/44100 = 0.023}},
    }
    sampleSizeArray[1] = {
        sampleSize = 179,
    }
    dataBuffer = 0x281cc4750

Now, when I read the audio track using AVAssetReader, the first CMSampleBuffer I get is:

CMSampleBuffer 0x102ed7b20 retainCount: 7 allocator: 0x1c061f840
    invalid = NO
    dataReady = YES
    makeDataReadyCallback = 0x0
    makeDataReadyRefcon = 0x0
    buffer-level attachments:
        EmptyMedia(P) = true
    formatDescription = (null)
    sbufToTrackReadiness = 0x0
    numSamples = 0
    outputPTS = {0/1 = 0.000}(based on outputPresentationTimeStamp)
    sampleTimingArray[1] = {
        {PTS = {0/1 = 0.000}, DTS = {INVALID}, duration = {0/1 = 0.000}},
    }
    dataBuffer = 0x0

and the next one is contains priming info of 1088/44_100:

CMSampleBuffer 0x10318bc00 retainCount: 7 allocator: 0x1c061f840
    invalid = NO
    dataReady = YES
    makeDataReadyCallback = 0x0
    makeDataReadyRefcon = 0x0
    buffer-level attachments:
        FillDiscontinuitiesWithSilence(P) = true
        GradualDecoderRefresh(P) = 1
        TrimDurationAtStart(P) = {
    epoch = 0;
    flags = 1;
    timescale = 44100;
    value = 1088;
}
        IsGradualDecoderRefreshAuthoritative(P) = false
    formatDescription = <CMAudioFormatDescription 0x281fdcaa0 [0x1c061f840]> {
    mediaType:'soun'
    mediaSubType:'aac '
    mediaSpecific: {
        ASBD: {
            mSampleRate: 44100.000000
            mFormatID: 'aac '
            mFormatFlags: 0x0
            mBytesPerPacket: 0
            mFramesPerPacket: 1024
            mBytesPerFrame: 0
            mChannelsPerFrame: 2
            mBitsPerChannel: 0  }
        cookie: {<CFData 0x2805f3800 [0x1c061f840]>{length = 39, capacity = 39, bytes = 0x03808080220000000480808014401400 ... 1210068080800102}}
        ACL: {Stereo (L R)}
        FormatList Array: {
            Index: 0
            ChannelLayoutTag: 0x650002
            ASBD: {
            mSampleRate: 44100.000000
            mFormatID: 'aac '
            mFormatFlags: 0x0
            mBytesPerPacket: 0
            mFramesPerPacket: 1024
            mBytesPerFrame: 0
            mChannelsPerFrame: 2
            mBitsPerChannel: 0  }}
    }
    extensions: {{
    VerbatimISOSampleEntry = {length = 87, bytes = 0x00000057 6d703461 00000000 00000001 ... 12100680 80800102 };
}}
}
    sbufToTrackReadiness = 0x0
    numSamples = 43
    outputPTS = {83/600 = 0.138}(based on outputPresentationTimeStamp)
    sampleTimingArray[1] = {
        {PTS = {1024/44100 = 0.023}, DTS = {1024/44100 = 0.023}, duration = {1024/44100 = 0.023}},
    }
    sampleSizeArray[43] = {
        sampleSize = 179,
        sampleSize = 173,
        sampleSize = 178,
        sampleSize = 172,
        sampleSize = 172,
        sampleSize = 159,
        sampleSize = 180,
        sampleSize = 200,
        sampleSize = 187,
        sampleSize = 189,
        sampleSize = 206,
        sampleSize = 192,
        sampleSize = 195,
        sampleSize = 186,
        sampleSize = 183,
        sampleSize = 189,
        sampleSize = 211,
        sampleSize = 198,
        sampleSize = 204,
        sampleSize = 211,
        sampleSize = 204,
        sampleSize = 202,
        sampleSize = 218,
        sampleSize = 210,
        sampleSize = 206,
        sampleSize = 207,
        sampleSize = 221,
        sampleSize = 219,
        sampleSize = 236,
        sampleSize = 219,
        sampleSize = 227,
        sampleSize = 225,
        sampleSize = 225,
        sampleSize = 229,
        sampleSize = 225,
        sampleSize = 236,
        sampleSize = 233,
        sampleSize = 231,
        sampleSize = 249,
        sampleSize = 234,
        sampleSize = 250,
        sampleSize = 249,
        sampleSize = 259,
    }
    dataBuffer = 0x281cde370

The input append method keeps returning true which in principle means that all sample buffers got appended, but the reader for some reason skips the first chunk of data. Is there anything I'm doing wrong here?

I'm using the following code to read the file:

let asset = AVAsset(url: fileURL)
guard let assetReader = try? AVAssetReader(asset: asset) else {
    return
}

asset.loadValuesAsynchronously(forKeys: ["tracks"]) { in
    guard let audioTrack = asset.tracks(withMediaType: .audio).first else { return }
    let audioOutput = AVAssetReaderTrackOutput(track: audioTrack, outputSettings: nil)
    assetReader.startReading()

    while assetReader.status == .reading {
        if let sampleBuffer = audioOutput.copyNextSampleBuffer() {
            // do something
        }
    }
}


问:
VBA SQL Query Table Error
I am trying to pull data from an ACD call data system, Nortel Contact Center 6.0 to be exact, and if you use that particular system what I am trying to capture is the daily call by call data.  However when I use this code

(sCW is a common word string that equals eCallByCallStat and
sDate is

dDate = Format(Month(deffDate) & "/" & iStartDay & "/" & Year(deffDate), "mm/dd/yyyy")

sDate = Format(dDate, "yyyymmdd")
)

```<code>sSql = ""
        sConn = "ODBC;DSN=Aus1S002;UID=somevaliduser;PWD=avalidpassword;SRVR=Thecorrectserver;DB=blue"
        sSql = "SELECT " & sCW & sDate & ".Timestamp, "
        sSql = sSql & sCW & sDate & ".CallEvent, "
        sSql = sSql & sCW & sDate & ".CallEventName, "
        sSql = sSql & sCW & sDate & ".CallID, "
        sSql = sSql & sCW & sDate & ".TelsetLoginID, "
        sSql = sSql & sCW & sDate & ".AssociatedData, "
        sSql = sSql & sCW & sDate & ".Destination, "
        sSql = sSql & sCW & sDate & ".EventData, "
        sSql = sSql & sCW & sDate & ".Source, "
        sSql = sSql & sCW & sDate & ".Time " & vbCrLf
        sSql = sSql & "FROM blue.dbo.eCallByCallStat" & sDate & " " & sCW & sDate & vbCrLf
        sSql = sSql & " ORDER BY " & sCW & sDate & ".Timestamp"

        Set oQT = ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("A1"), Sql:=sSql)

        oQT.Refresh BackgroundQuery:=False

        Do While oQT.Refreshing = True
        Loop"
</code>```

When I run this I get an odd error message at oQT.Refresh BackgroundQuery:=False

Oddly enough it worked for a month or so then just died

@ loopo
I actually added the "" to the connection string and actually have the user name and password hard coded into the query with out quotes, I have since removed them for clarity in the posting

The error I recieve is

Run-time error '-2147417848(80010108)':
  Method 'Refresh" of  Object "_QueryTable' Failed

Thanks for your input Kevin.  The Database is never in a state where no one is accessing it, it is a Call Handling system that is on 24 x 7 and always connected to is clients.  At least that is my understanding.  If I do this manually through Excel I never get an error, or have any issues only when I am doing this via a macro does it give me issues which lead me to think that it was my code causing the issue.

I am connecting to the database via ODBC as recommended by the manuafacturer, but I wonder if they ever envisioned this sort of thing.

I will see if I can leverage this into a .NET project and see if that helps.

答:
I was having this same issue when trying to refresh a Query.

For some reason that I don't know. When refering to a QueryTable object the refresh only works the first time you run the vba code. If you run it again the runtime error will prompt Run-time error '-2147217842(80040e4e): Method 'Refresh' of object '_QueryTable' failed  occurs

This is an example of a Query refresh that fails.
Ws.ListObjects("TableName").QueryTable.Refresh BackgroundQuery:=False

Here is the solution found.
ThisWorkbook.Connections("ConnectionName").Refresh

If someone knows the reason why the refresh method of the QueryTable object fails. please let us know.

OwnerDisplayName: CABecker

Title: VBA SQL Query Table Error

Body:
I am trying to pull data from an ACD call data system, Nortel Contact Center 6.0 to be exact, and if you use that particular system what I am trying to capture is the daily call by call data.  However when I use this code

(sCW is a common word string that equals eCallByCallStat and
sDate is

dDate = Format(Month(deffDate) & "/" & iStartDay & "/" & Year(deffDate), "mm/dd/yyyy")

sDate = Format(dDate, "yyyymmdd")
)

sSql = ""
        sConn = "ODBC;DSN=Aus1S002;UID=somevaliduser;PWD=avalidpassword;SRVR=Thecorrectserver;DB=blue"
        sSql = "SELECT " & sCW & sDate & ".Timestamp, "
        sSql = sSql & sCW & sDate & ".CallEvent, "
        sSql = sSql & sCW & sDate & ".CallEventName, "
        sSql = sSql & sCW & sDate & ".CallID, "
        sSql = sSql & sCW & sDate & ".TelsetLoginID, "
        sSql = sSql & sCW & sDate & ".AssociatedData, "
        sSql = sSql & sCW & sDate & ".Destination, "
        sSql = sSql & sCW & sDate & ".EventData, "
        sSql = sSql & sCW & sDate & ".Source, "
        sSql = sSql & sCW & sDate & ".Time " & vbCrLf
        sSql = sSql & "FROM blue.dbo.eCallByCallStat" & sDate & " " & sCW & sDate & vbCrLf
        sSql = sSql & " ORDER BY " & sCW & sDate & ".Timestamp"

        Set oQT = ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("A1"), Sql:=sSql)

        oQT.Refresh BackgroundQuery:=False

        Do While oQT.Refreshing = True
        Loop"

When I run this I get an odd error message at oQT.Refresh BackgroundQuery:=False

Oddly enough it worked for a month or so then just died

@ loopo
I actually added the "" to the connection string and actually have the user name and password hard coded into the query with out quotes, I have since removed them for clarity in the posting

The error I recieve is

Run-time error '-2147417848(80010108)':
  Method 'Refresh" of  Object "_QueryTable' Failed

Thanks for your input Kevin.  The Database is never in a state where no one is accessing it, it is a Call Handling system that is on 24 x 7 and always connected to is clients.  At least that is my understanding.  If I do this manually through Excel I never get an error, or have any issues only when I am doing this via a macro does it give me issues which lead me to think that it was my code causing the issue.

I am connecting to the database via ODBC as recommended by the manuafacturer, but I wonder if they ever envisioned this sort of thing.

I will see if I can leverage this into a .NET project and see if that helps.

LastEditorDisplayName: Remou


问:
HttpContext.Current is null after updated to VS 16.5.0 in .net framework
This line of code:

```<code> public static object Get(SessionStateKeys key)
        {
            string keyString = Enum.GetName(typeof(SessionStateKeys), key);
            return HttpContext.Current.Session[keyString];
        }
</code>```

it's not working anymore after installation latest verison of VS comunity 16.5.0

HttpContext.Current is null

This happens in several projects using .net framework (not .net core)

is it a bug in vs?

Any help?

答:
We had a similar problem.

In our case, it was an option that started to be selected after the VS 16.5 update.

Go to the Exception Settings screen and uncheck the System.NullReferenceException option.

If compared to previous versions, this option is unchecked by default.

Exception Settings screen

Body:
This line of code:

 public static object Get(SessionStateKeys key)
        {
            string keyString = Enum.GetName(typeof(SessionStateKeys), key);
            return HttpContext.Current.Session[keyString];
        }

it's not working anymore after installation latest verison of VS comunity 16.5.0

HttpContext.Current is null

This happens in several projects using .net framework (not .net core)

is it a bug in vs?

Any help?


问:
Jquery UI nested Selectable issue
I have requirement of creating/splitting my div elements horizontally and vertically on click of button at run time. I am able to create a div container in which splits the canvas according to the button clicked.

For example: When I click horizontalSplit button it should create two new div's within the selected container. I am running into issue with nested child div element. When I am trying to select the innermost child code is selecting parent div also which is creating issue. I am not able to figure out why my nonSelectable class is automatically getting "ui-selected"

This is what I have done so far:

```<code><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
                <link rel="stylesheet" href="css/themes/base/jquery.ui.all.css">
                <script type="text/javascript" src="scripts/jquery-1.6.js"></script>
                <script type="text/javascript" src="scripts/jquery-ui-1.8.13.custom.min.js" ></script>
                    <style>

                        #canvasWrapper {
                            border: 1px solid #DDDDDD;
                            height: 500px;
                            vertical-align:top;
                            margin-left: auto;
                            margin-right: auto;
                            width:  90%;
                        }
                        .Frame {
                            border: 1px solid #DDDDDD;
                            height: 500px;
                            margin-left: auto;
                            margin-right: auto;
                            width:  100%;
                        }
                        .hFrame {
                            border: 1px solid #DDDDDD;
                            height: 50%;
                            width:  100%;
                            position:relative;
                        }

                        .nonSelectable {
                            border: 1px solid #DDDDDD;
                            height: 50%;
                            width:  100%;
                            position:relative;
                        }

                        .vFrame {
                            border: 1px solid #DDDDDD;
                            height: 100%;
                            width: 50%;
                            position:relative;
                        }

                        .buttonBar {
                            position: relative;
                            margin-left: auto;
                            margin-right: auto;
                            width:90%;
                        }
                        .Frame .ui-selecting,.vFrame .ui-selecting ,.hFrame .ui-selecting  { background: blue; }
                        .Frame .ui-selected,.vFrame .ui-selected ,.hFrame .ui-selected  { background: grey; }
                        .hFrame ui-selectable { background: yellow; }
                        .hFrame ui-selected { background: green; }
                        .hFrame ui-selectable ui-selected { background: pink; }

                    </style>

</head>
<body>
    <div id="canvasWrapper">
        <div id="canvasFrame" class="Frame">
        </div>
    </div>
    <div class="buttonBar">
        <input id="B1" class="button" type="submit" value="Horizontal Split">
        <input id="B2" class="button" type="submit" value="Vertical Split">
    </div>

    <script>
        $(document).ready(function()
        {
            $("#canvasFrame").addClass("ui-selected");
            $(function() {
                $( ".Frame" ).selectable({
                     // cancel: '.nonSelectable'
                });
                $( ".hFrame" ).selectable({
                    // cancel: '.nonSelectable'

                });
                $( ".vFrame" ).selectable();
                // $( ".nonSelectable" ).selectable("disable");
            });

            addHFrame();

            addVFrame();

        });

    function addHFrame(){
        $("#B1").unbind('click.addit').bind('click.addit',function()
        {

        var numSplits=2;
        var select="";

        $(".ui-selected ").each(function () {
                for (var i=0; i<numSplits ; i++){
                        $(this).removeClass('ui-selected ui-selectable');
                        $(this).parent().removeClass('ui-selected ui-selectable');
                        var $newElement = '<div></div>';

                        $(this).append($newElement);
                        $(this).children().addClass("hFrame");

                        //$(this).unbind('ui-selected ui-selectable');
                        // $(this).parent().selectable("disable")   ;
                        // $(this).remove("hFrame").addClass("nonSelectable")   ;
                        addHFrame();
                    }
                $(this).hasClass("hFrame") ? $(this).removeClass("hFrame").addClass("nonSelectable") : $(this);
            });
        });

    }

    function addVFrame(){

                $("#B2").click(function()
                {

                    $("div.ui-selected").each(function () {
                        // $(this).append('<div class="vFrame"> </div>');
                        alert("Button Vertical Split Clicked");
                    });
                });
    }

    </script>
</body>

</html>
</code>```

答:
Sounds like you need to prevent event propagation:
http://api.jquery.com/event.stopPropagation/

Similar question: jQuery UI Sortable -- How can I cancel the click event on an item that's dragged/sorted?

When the element is clicked, the event bubbles up the DOM until it hits the highest level HTML node. This is referred to as event propagation. So the event is fired on all of the element's ancestors unless event propagation is halted. This can be done like so:

HTML-

```<code><body>
    <div id="container">
        <a href="javascript:void(0);" id="click-element"></a>
    </div>
</body>
</code>```

Script-

```<code>$('#click-element').click(function(event){

    //prevent container and body from triggering click event
    event.stopPropagation();

    //do stuff

});
</code>```

Title: Jquery UI nested Selectable issue

Body:
I have requirement of creating/splitting my div elements horizontally and vertically on click of button at run time. I am able to create a div container in which splits the canvas according to the button clicked.

For example: When I click horizontalSplit button it should create two new div's within the selected container. I am running into issue with nested child div element. When I am trying to select the innermost child code is selecting parent div also which is creating issue. I am not able to figure out why my nonSelectable class is automatically getting "ui-selected"

This is what I have done so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

                <link rel="stylesheet" href="css/themes/base/jquery.ui.all.css">

        <input id="B1" class="button" type="submit" value="Horizontal Split">
        <input id="B2" class="button" type="submit" value="Vertical Split">

Body:
Sounds like you need to prevent event propagation:
http://api.jquery.com/event.stopPropagation/

Similar question: jQuery UI Sortable -- How can I cancel the click event on an item that's dragged/sorted?

When the element is clicked, the event bubbles up the DOM until it hits the highest level HTML node. This is referred to as event propagation. So the event is fired on all of the element's ancestors unless event propagation is halted. This can be done like so:

HTML-

<body>
    <div id="container">
        <a href="javascript:void(0);" id="click-element"></a>
    </div>
</body>

Script-

$('#click-element').click(function(event){

    //prevent container and body from triggering click event
    event.stopPropagation();

    //do stuff

});


问:
Is it possible to run a gmt script (csh file) using python?
I have this gmt script named map.csh, this file will create a map. I want to run/execute it(map.csh) using python. I tried using subprocess.call but I got an error, "Exec format error"</p>

```<code>import subprocess

subprocess.call(['./map.csh'])
</code>```

答:
The first line of your script does not appear to be a shebang, since you know it is csh you can use env to invoke it for you. Like,

```<code>subprocess.call(['/usr/bin/env','csh','./map.csh'])
</code>```

This will search for csh (it could be /bin/csh, /usr/bin/csh, etc).

I still recommend specifying a proper shebang as the first line of map.csh

```<code>#!/usr/bin/env csh
</code>```

as that would follow the principle of least surprise.

Body:
I have this gmt script named map.csh, this file will create a map. I want to run/execute it(map.csh) using python. I tried using subprocess.call but I got an error, "Exec format error"

import subprocess

subprocess.call(['./map.csh'])

Body:
The first line of your script does not appear to be a shebang, since you know it is csh you can use env to invoke it for you. Like,

subprocess.call(['/usr/bin/env','csh','./map.csh'])

This will search for csh (it could be /bin/csh, /usr/bin/csh, etc).

I still recommend specifying a proper shebang as the first line of map.csh

#!/usr/bin/env csh

as that would follow the principle of least surprise.


问:
Get first object from array
I am trying to create an array which will hold just the Name elements from this array:

```<code>var array = [{Name: "steve"}, {Age: 18}, {Location: "Uk"}];
</code>```

I am new to JavaScript and I am not sure how this would be done.

答:
A new feature that javascript has is array destructuring!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

It removes all the complexity of using a custom function, or using the hard coded 0 index (seems like a code smell).

Just destructure the value into a variable. If the array is empty the value will be undefined.

```<code>const arr = ["one", "two", "three"];
const [first] = arr;
</code>```

Title: Get first object from array

Body:
I am trying to create an array which will hold just the Name elements from this array:

var array = [{Name: "steve"}, {Age: 18}, {Location: "Uk"}];

I am new to JavaScript and I am not sure how this would be done.

Body:
A new feature that javascript has is array destructuring!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

It removes all the complexity of using a custom function, or using the hard coded 0 index (seems like a code smell).

Just destructure the value into a variable. If the array is empty the value will be undefined.

const arr = ["one", "two", "three"];
const [first] = arr;


问:
How to filter typescript array based on a variable
I'm trying to filter an array in TypeScript but using a variable instead of a hardcoded number. Most of the results on Stack Overflow talk about filtering arrays with arrays but none talk about using a single variable.

I have an array of objects of this type:

```<code>class pair {
    name: string,
    value: number
}
</code>```

and I want to create a function that filters some of these out based on a value.

```<code>myFunction(removeMe: string) {
    ...
    var resultingArray = arrayOfPairObjects.filter(x => x.value === Number(removeMe)); // the cast is necessary, sadly
    return resultingArray;
}
</code>```

But if I do this, removeMe is set properly however its value becomes undefined when the filter runs. I have no idea why, but if I change Number(removeMe) to 2 it works perfectly. I just need to know how to make the variable work.

Thank you!

EDIT: By becomes undefined, I mean that I can log the removeMe value on the console on the line immediately before the .filter() and its value is correct (is ‘1’ or ‘2’, whatever is passed in), but I get an error in the console saying that the value within the .filter() is undefined. I think it’s maybe losing scope but I’m a bit new to Typescript so maybe there’s a subtlety I’m not aware of.

答:
```<code>arrayOfPairObjects: Pair[] = [
  { name: 'hello', value: 2 },
  { name: 'hello', value: 5 },
  { name: 'hello', value: 7 },
  { name: 'hello', value: 8 },
  { name: 'hello', value: 9 },
  { name: 'hello', value: 10 }
]

// removeMe is a little bit confusing, but okay..
getFilteredArray(removeMe: string) {
   return this.arrayOfPairObjects.filter(x => x.value === +removeMe);
}

console.log(this.getFilteredArray('7'));

// Output:
// { name: 'hello', value: 7 }
</code>```

Body:
I'm trying to filter an array in TypeScript but using a variable instead of a hardcoded number. Most of the results on Stack Overflow talk about filtering arrays with arrays but none talk about using a single variable.

I have an array of objects of this type:

class pair {
    name: string,
    value: number
}

and I want to create a function that filters some of these out based on a value.

myFunction(removeMe: string) {
    ...
    var resultingArray = arrayOfPairObjects.filter(x => x.value === Number(removeMe)); // the cast is necessary, sadly
    return resultingArray;
}

But if I do this, removeMe is set properly however its value becomes undefined when the filter runs. I have no idea why, but if I change Number(removeMe) to 2 it works perfectly. I just need to know how to make the variable work.

Thank you!

EDIT: By becomes undefined, I mean that I can log the removeMe value on the console on the line immediately before the .filter() and its value is correct (is ‘1’ or ‘2’, whatever is passed in), but I get an error in the console saying that the value within the .filter() is undefined. I think it’s maybe losing scope but I’m a bit new to Typescript so maybe there’s a subtlety I’m not aware of.


问:
static_cast seems to work when it can't possibly work
In the following code I use static_cast on a void*, which points to an A object.

A and B are not related in any way. I understand the compiler cannot raise an error against this. But what I don't understand is, how come this actually seems to work when run...? I would expect a segfault or an error of some kind.

```<code>#include <iostream>

using namespace std;

class A {
    public:
    void f() const {
        cout << "f" << endl;
    }
};

class B {
    public:
    void q() {
        cout << "q" << endl;
    }
};

int main(int argc, char** argv) {
    A a;
    void* p = &a;

    static_cast<B*>(p)->q(); // Prints "q"!

    return 0;
}
</code>```

What is the mechanism behind this?

答:
how come this actually seems to work when run...?

Because the behaviour of the program is undefined.

I would expect a segfault or an error of some kind.

When behaviour is undefined, there is no guarantee that there would be a segfault or an error of some kind. It would generally be unreasonable to expect such. When behaviour is undefined, there are no guarantees whatsoever.

Body:
In the following code I use static_cast on a void*, which points to an A object.

A and B are not related in any way. I understand the compiler cannot raise an error against this. But what I don't understand is, how come this actually seems to work when run...? I would expect a segfault or an error of some kind.

#include <iostream>

using namespace std;

class A {
    public:
    void f() const {
        cout << "f" << endl;
    }
};

class B {
    public:
    void q() {
        cout << "q" << endl;
    }
};

int main(int argc, char** argv) {
    A a;
    void* p = &a;

    static_cast(p)->q(); // Prints "q"!

    return 0;
}

What is the mechanism behind this?


问:
Number Guessing Game with discord.py rewrite
I tried this code, bot says guess but doesn't respond to my guess.</p>

```<code>@commands.command()
async def game(self, ctx):
    number = random.randint(0, 100)
    for guess in range(0, 5):
        await ctx.send('guess')
        Message = await bot.wait_for('message')
        Message = int(Message)
        if Message.cleant_content > number:
            await ctx.send('bigger')
        elif Message.cleant_content < number:
            await ctx.send('smaller')
        else:
            await ctx.send('true')
</code>```

答:
Because you're using cogs, I'd imagine you've got your bot initialised as self.bot instead of just bot.

Also, you're converting a message object to an integer, and trying to access an attribute of an integer named cleant_content.

This is how your code should look:

@commands.command()
async def game(self, ctx):
    number = random.randint(0, 100)
    for i in range(0, 5):
        await ctx.send('guess')
        response = await self.bot.wait_for('message')
        guess = int(response.content)
        if guess > number:
            await ctx.send('bigger')
        elif guess < number:
            await ctx.send('smaller')
        else:
            await ctx.send('true')
```

I also changed up a few of the variable names for readability purposes. If you want to, you can add some checks for whether their guess was actually a number or not.

Additionally, I changed what was meant to be clean_content to content instead, as clean_content is completely void of its purpose because you can't convert <, @, #, !, > and such to an integer, meaning it would error either way. I'm hoping that made some sense.

References:

Client.wait_for()

Message.content

Message.clean_content - Read the difference between content and clean_content to better understand what I was getting at

Body:
I tried this code, bot says guess but doesn't respond to my guess.

@commands.command()
async def game(self, ctx):
    number = random.randint(0, 100)
    for guess in range(0, 5):
        await ctx.send('guess')
        Message = await bot.wait_for('message')
        Message = int(Message)
        if Message.cleant_content > number:
            await ctx.send('bigger')
        elif Message.cleant_content < number:
            await ctx.send('smaller')
        else:
            await ctx.send('true')

Body:
Because you're using cogs, I'd imagine you've got your bot initialised as self.bot instead of just bot.

Also, you're converting a message object to an integer, and trying to access an attribute of an integer named cleant_content.

This is how your code should look:

@commands.command()
async def game(self, ctx):
    number = random.randint(0, 100)
    for i in range(0, 5):
        await ctx.send('guess')
        response = await self.bot.wait_for('message')
        guess = int(response.content)
        if guess > number:
            await ctx.send('bigger')
        elif guess < number:
            await ctx.send('smaller')
        else:
            await ctx.send('true')

I also changed up a few of the variable names for readability purposes. If you want to, you can add some checks for whether their guess was actually a number or not.

Additionally, I changed what was meant to be clean_content to content instead, as clean_content is completely void of its purpose because you can't convert <, @, #, !, > and such to an integer, meaning it would error either way. I'm hoping that made some sense.

References:

Client.wait_for()

Message.content

Message.clean_content - Read the difference between content and clean_content to better understand what I was getting at


问:
Removing duplicate dictionaries in list of dicts based on value uniqueness for a given key
I have a dictionary:

```<code>dicts = [
{'id': 'item1', 'type': 'foo', 'metaId': 'metaId1'},
{'id': 'item2', 'type': 'foo', 'metaId': 'metaId2'},
{'id': 'item3', 'type': 'foo3', 'metaId': 'metaId3'},
{'id': 'item4', 'type': 'foo2', 'metaId': 'metaId2'},
{'id': 'item5', 'type': 'foo3', 'metaId': 'metaId3'},
{'id': 'item6', 'type': 'foo2', 'metaId': 'metaId2'},
{'id': 'item7', 'type': 'foo3', 'metaId': 'metaId3'},
{'id': 'item8', 'type': 'foo2', 'metaId': 'metaId2'},
{'id': 'item9', 'type': 'foo3', 'metaId': 'metaId3'}]
</code>```

I want to loop through the list and create a new list, that contains dictionaries with unique values for key 'type'. I don't care which dictionaries stay, first instance with that key: value  stays, the rest is omitted. So in the end I'd like to see:

```<code>expected = [
{'id': 'item1', 'type': 'foo', 'metaId': 'metaId1'},
{'id': 'item3', 'type': 'foo3', 'metaId': 'metaId3'},
{'id': 'item4', 'type': 'foo2', 'metaId': 'metaId2'}
]
</code>```

Here is what I tried, definitely not what I need as it returns an empty list. I think I struggle with checking for a value in new sublist of dictionaries to make it excluded

```<code>keys_to_keep = set()
expected = []
for d in dicts:
    for key, value in d.items():
        if value not in expected:
            keys_to_keep.add(key)
remove_keys = set(d) - keys_to_keep

for d in dicts:
    for k in remove_keys:
        del d[k]
dicts = expected
print(dicts)
</code>```

答:
Keep seen types and add missing to a result slice and also mark it as seen. A function that does this:</p>

```<code>def transform(dicts):
  seen, result = set(), []
  for d in dicts:
    my_key = d['type']
    if my_key not in seen:
      result.append(d)
      seen.add(my_key)
  return result
</code>```

Body:
I have a dictionary:

dicts = [
{'id': 'item1', 'type': 'foo', 'metaId': 'metaId1'},
{'id': 'item2', 'type': 'foo', 'metaId': 'metaId2'},
{'id': 'item3', 'type': 'foo3', 'metaId': 'metaId3'},
{'id': 'item4', 'type': 'foo2', 'metaId': 'metaId2'},
{'id': 'item5', 'type': 'foo3', 'metaId': 'metaId3'},
{'id': 'item6', 'type': 'foo2', 'metaId': 'metaId2'},
{'id': 'item7', 'type': 'foo3', 'metaId': 'metaId3'},
{'id': 'item8', 'type': 'foo2', 'metaId': 'metaId2'},
{'id': 'item9', 'type': 'foo3', 'metaId': 'metaId3'}]

I want to loop through the list and create a new list, that contains dictionaries with unique values for key 'type'. I don't care which dictionaries stay, first instance with that key: value  stays, the rest is omitted. So in the end I'd like to see:

expected = [
{'id': 'item1', 'type': 'foo', 'metaId': 'metaId1'},
{'id': 'item3', 'type': 'foo3', 'metaId': 'metaId3'},
{'id': 'item4', 'type': 'foo2', 'metaId': 'metaId2'}
]

Here is what I tried, definitely not what I need as it returns an empty list. I think I struggle with checking for a value in new sublist of dictionaries to make it excluded

keys_to_keep = set()
expected = []
for d in dicts:
    for key, value in d.items():
        if value not in expected:
            keys_to_keep.add(key)
remove_keys = set(d) - keys_to_keep

for d in dicts:
    for k in remove_keys:
        del d[k]
dicts = expected
print(dicts)

Body:
Keep seen types and add missing to a result slice and also mark it as seen. A function that does this:

def transform(dicts):
  seen, result = set(), []
  for d in dicts:
    my_key = d['type']
    if my_key not in seen:
      result.append(d)
      seen.add(my_key)
  return result


问:
Error when running (open-mmlab) C:\mmdetection>python setup.py develop - raise RuntimeError(message)
I get the following error in the last stage of the mmdetection install from
https://github.com/open-mmlab/mmdetection/blob/master/docs/install.md

when running

```<code>C:\...\mmdetection\python setup.py develop
running develop
running egg_info
writing mmdet.egg-info\PKG-INFO
writing dependency_links to mmdet.egg-info\dependency_links.txt
writing requirements to mmdet.egg-info\requires.txt
writing top-level names to mmdet.egg-info\top_level.txt
reading manifest file 'mmdet.egg-info\SOURCES.txt'
writing manifest file 'mmdet.egg-info\SOURCES.txt'
running build_ext
C:\...\torch\utils\cpp_extension.py:237: UserWarning: Error checking compiler version for cl: [WinError 2] The system cannot find the file specified

warnings.warn('Error checking compiler version for {}: {}'.format(compiler, error))

building 'mmdet.ops.utils.compiling_info' extension
Emitting ninja build file C:\...\mmdetection\build\temp.win-amd64-3.7\Release\build.ninja...
Compiling objects...
Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N)

FAILED: C:/.../mmdetection/build/temp.win-amd64-3.7/Release/mmdet/ops/utils/src/compiling_info.obj
cl /showIncludes /nologo /Ox /W3 /GL /DNDEBUG /MD /MD /wd4819 /EHsc -DWITH_CUDA -IC:\torch\include -

CreateProcess failed: The system cannot find the file specified.
ninja: build stopped: subcommand failed.

Traceback (most recent call last):
  File "C:...\torch\utils\cpp_extension.py", line 1400, in _run_ninja_build
    check=True)
  File "C:\anaconda3\envs\open-mmlab\lib\subprocess.py", line 512, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['ninja', '-v']' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "setup.py", line 300, in <module>
    zip_safe=False)
  File "C:...\setuptools\__init__.py", line 144, in setup
    return distutils.core.setup(**attrs)
  File "C:\...\anaconda3\envs\open-mmlab\lib\distutils\core.py", line 148, in
  File "C:\...\torch\utils\cpp_extension.py", line 1140, in _write_ninja_file_and_compile_objects
    ..............
    error_prefix='Error compiling objects for extension')
  File "C:\...\torch\utils\cpp_extension.py", line 1413, in _run_ninja_build

raise RuntimeError(message)
RuntimeError: Error compiling objects for extension
</code>```

I was wondering if anyone knows what could be the cause of this error message ?

I am using windows 10

答: Use cudatoolkit==10.0 not 10.2 so that pytorch 1.4 is used instead of pytorch 1.5. This fixes the issue. However if using mmdetection v2.0.0 it seems that pytorch 1.5 with cudatoolkit==10.2 works fine. </p>

Body:
I get the following error in the last stage of the mmdetection install from
https://github.com/open-mmlab/mmdetection/blob/master/docs/install.md

when running

C:\...\mmdetection\python setup.py develop
running develop
running egg_info
writing mmdet.egg-info\PKG-INFO
writing dependency_links to mmdet.egg-info\dependency_links.txt
writing requirements to mmdet.egg-info\requires.txt
writing top-level names to mmdet.egg-info\top_level.txt
reading manifest file 'mmdet.egg-info\SOURCES.txt'
writing manifest file 'mmdet.egg-info\SOURCES.txt'
running build_ext
C:\...\torch\utils\cpp_extension.py:237: UserWarning: Error checking compiler version for cl: [WinError 2] The system cannot find the file specified

warnings.warn('Error checking compiler version for {}: {}'.format(compiler, error))

building 'mmdet.ops.utils.compiling_info' extension
Emitting ninja build file C:\...\mmdetection\build\temp.win-amd64-3.7\Release\build.ninja...
Compiling objects...
Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N)

FAILED: C:/.../mmdetection/build/temp.win-amd64-3.7/Release/mmdet/ops/utils/src/compiling_info.obj
cl /showIncludes /nologo /Ox /W3 /GL /DNDEBUG /MD /MD /wd4819 /EHsc -DWITH_CUDA -IC:\torch\include -

CreateProcess failed: The system cannot find the file specified.
ninja: build stopped: subcommand failed.

Traceback (most recent call last):
  File "C:...\torch\utils\cpp_extension.py", line 1400, in _run_ninja_build
    check=True)
  File "C:\anaconda3\envs\open-mmlab\lib\subprocess.py", line 512, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['ninja', '-v']' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "setup.py", line 300, in <module>
    zip_safe=False)
  File "C:...\setuptools\__init__.py", line 144, in setup
    return distutils.core.setup(**attrs)
  File "C:\...\anaconda3\envs\open-mmlab\lib\distutils\core.py", line 148, in
  File "C:\...\torch\utils\cpp_extension.py", line 1140, in _write_ninja_file_and_compile_objects
    ..............
    error_prefix='Error compiling objects for extension')
  File "C:\...\torch\utils\cpp_extension.py", line 1413, in _run_ninja_build

raise RuntimeError(message)
RuntimeError: Error compiling objects for extension

I was wondering if anyone knows what could be the cause of this error message ?

I am using windows 10