问:
How do I reduce the "computer time" that my Google Apps Script is using?
I followed Hugo Fierro's tutorial on adding a Google Apps Script to send emails from Google Sheets. The tut is at https://developers.google.com/apps-script/articles/sending_emails.

I customised the script:

1) I replaced the "MailApp.sendEmail" API with "GmailApp.sendEmail" because I was getting authentication errors and the emails were not sending. The Gmail API has worked fine.

2) I added an option to send a PDF attachment with each mail using "DriveApp.getFileById".

3) I added a second condition to the IF statement to check that the PDF document is available before sending (by referencing a column in the sheet).

The problem is that if the script references only 5 rows, then it processes in under 30 seconds. When I try to process 10 rows or more, the processing time goes up significantly.

I replaced "sheet.getRange" with "sheet.getLastRow()" in an attempt to reduce how many rows the script is referencing.

```<code>var READY = 'READY';
var SENT = 'SENT';

function sendEmails() {

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Email'); // Get the active spreadsheet, then get the "Email" sheet
  var startRow = 2; // Select data row to start at
  var endRow = sheet.getLastRow(); // Get the last row in the sheet
  var data = sheet.getRange(startRow, 1, endRow, 6).getValues(); // Get the range of cells, then get the values

  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var email = row[0]; // Column 1
    var subject = row[1]; // Column 2
    var message = row[2]; // Column 3
    var attachment = DriveApp.getFileById(row[3]); // Returns the attachment file ID
    var emailReady = row[4]; // Column 5
    var emailSent = row[5]; // Column 6
    var name = 'VFISA'; // Set "from" name in email
    var bcc = 'myaddress@gmail.com'; // Blind carbon copy this email address
    if (emailReady==READY && emailSent!==SENT) { // Prevents sending duplicates, waits for attachment cell to confirm available
      GmailApp.sendEmail(email, subject, message,{
        name: name,
        bcc: bcc,
        htmlBody: message,
        attachments: attachment
      });
      sheet.getRange(startRow + i, 6).setValue(SENT); // Set the cell in column F to "SENT"
      SpreadsheetApp.flush(); // Make sure the cell is updated right away in case the script is interrupted
    }
  }
}
</code>```

I expected the script to run much faster. The error I get is "Service using too much computer time for one day". When I reference 20 rows it takes up to 4 minutes to run.

答:
DriveApp.getFileById was causing each row to take about 5 seconds to execute. I removed this and now the entire script executes in under 1 second.

Instead of adding the PDF as an attachment, I used an inline link based on the file's ID (based on Alan Wells' suggestion).

Body:
I followed Hugo Fierro's tutorial on adding a Google Apps Script to send emails from Google Sheets. The tut is at https://developers.google.com/apps-script/articles/sending_emails.

I customised the script:

1) I replaced the "MailApp.sendEmail" API with "GmailApp.sendEmail" because I was getting authentication errors and the emails were not sending. The Gmail API has worked fine.

2) I added an option to send a PDF attachment with each mail using "DriveApp.getFileById".

3) I added a second condition to the IF statement to check that the PDF document is available before sending (by referencing a column in the sheet).

The problem is that if the script references only 5 rows, then it processes in under 30 seconds. When I try to process 10 rows or more, the processing time goes up significantly.

I replaced "sheet.getRange" with "sheet.getLastRow()" in an attempt to reduce how many rows the script is referencing.

var READY = 'READY';
var SENT = 'SENT';

function sendEmails() {

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Email'); // Get the active spreadsheet, then get the "Email" sheet
  var startRow = 2; // Select data row to start at
  var endRow = sheet.getLastRow(); // Get the last row in the sheet
  var data = sheet.getRange(startRow, 1, endRow, 6).getValues(); // Get the range of cells, then get the values

  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var email = row[0]; // Column 1
    var subject = row[1]; // Column 2
    var message = row[2]; // Column 3
    var attachment = DriveApp.getFileById(row[3]); // Returns the attachment file ID
    var emailReady = row[4]; // Column 5
    var emailSent = row[5]; // Column 6
    var name = 'VFISA'; // Set "from" name in email
    var bcc = 'myaddress@gmail.com'; // Blind carbon copy this email address
    if (emailReady==READY && emailSent!==SENT) { // Prevents sending duplicates, waits for attachment cell to confirm available
      GmailApp.sendEmail(email, subject, message,{
        name: name,
        bcc: bcc,
        htmlBody: message,
        attachments: attachment
      });
      sheet.getRange(startRow + i, 6).setValue(SENT); // Set the cell in column F to "SENT"
      SpreadsheetApp.flush(); // Make sure the cell is updated right away in case the script is interrupted
    }
  }
}

I expected the script to run much faster. The error I get is "Service using too much computer time for one day". When I reference 20 rows it takes up to 4 minutes to run.


来源: stackexchange

问:
Join many to many and merge the many refs into single row
I've consulted similar questions but the question is different since I've a join with not just many to many but one to many as well.

user

```<code>+----+-------+---------------+
| id | first | department_id |
+----+-------+---------------+
|  1 | John  | 1             |
|  2 | Jane  | 2             |
+----+-------+---------------+
</code>```

department

```<code>+----+------------+
| id |   name     |
+----+------------+
|  1 | purchasing |
|  2 | HR         |
|  3 | marketing  |
+----+------------+
</code>```

role

```<code>+----+----------+
| id |   name   |
+----+----------+
|  1 | admin    |
|  2 | accounts |
|  3 | sales    |
+----+----------+
</code>```

user_role

```<code>+--------+----------+
| user_id | role_id |
+--------+----------+
|      1  |       1 |
|      1  |       2 |
|      1  |       3 |
|      2  |       2 |
|      2  |       3 |
+--------+----------+
</code>```

sql

```<code>SELECT user.first, GROUP_CONCAT(role.name), department.id AS 'department.id', department.name AS 'department.name'
FROM user
INNER JOIN department ON user.department_id
INNER JOIN user_role ON user.id = user_role.user_id
INNER JOIN role ON user_role.role_id = role.id
GROUP BY user.name;
</code>```

Expectation

```<code>John, "admin,accounts,sales", 1, purchasing
Jane, "accounts,sales", 2, HR
</code>```

Essentially it's a 4 table join, with many to many and 1 to many with group concat function.

the query gives an error.

```<code>Error Code: 1055. Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'department.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
</code>```

答:
Since the department is unique for each user you can use max() to get the department details to overcome the limitations posed by group by.

```<code>SELECT
  user.first,
  GROUP_CONCAT(role.name) AS roles,
  max(department.id) AS department_id,
  max(department.name) AS department_name
FROM user
INNER JOIN department ON user.department_id = department.id
INNER JOIN user_role ON user.id = user_role.user_id
INNER JOIN role ON user_role.role_id = role.id
GROUP BY user.first;
</code>```

Also if the user's name is not unique, you should:

```<code>GROUP BY user.id, user.first
</code>```

Also the aliases of columns must not be enclosed in single quotes. Use backticks instead.

ContentLicense: CC BY-SA 4.0

Body:
I've consulted similar questions but the question is different since I've a join with not just many to many but one to many as well.

user

+----+-------+---------------+
| id | first | department_id |
+----+-------+---------------+
|  1 | John  | 1             |
|  2 | Jane  | 2             |
+----+-------+---------------+

department

+----+------------+
| id |   name     |
+----+------------+
|  1 | purchasing |
|  2 | HR         |
|  3 | marketing  |
+----+------------+

role

+----+----------+
| id |   name   |
+----+----------+
|  1 | admin    |
|  2 | accounts |
|  3 | sales    |
+----+----------+

user_role

+--------+----------+
| user_id | role_id |
+--------+----------+
|      1  |       1 |
|      1  |       2 |
|      1  |       3 |
|      2  |       2 |
|      2  |       3 |
+--------+----------+

sql

SELECT user.first, GROUP_CONCAT(role.name), department.id AS 'department.id', department.name AS 'department.name'
FROM user
INNER JOIN department ON user.department_id
INNER JOIN user_role ON user.id = user_role.user_id
INNER JOIN role ON user_role.role_id = role.id
GROUP BY user.name;

Expectation

John, "admin,accounts,sales", 1, purchasing
Jane, "accounts,sales", 2, HR

Essentially it's a 4 table join, with many to many and 1 to many with group concat function.

the query gives an error.

Error Code: 1055. Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'department.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Body:
Since the department is unique for each user you can use max() to get the department details to overcome the limitations posed by group by.

SELECT
  user.first,
  GROUP_CONCAT(role.name) AS roles,
  max(department.id) AS department_id,
  max(department.name) AS department_name
FROM user
INNER JOIN department ON user.department_id = department.id
INNER JOIN user_role ON user.id = user_role.user_id
INNER JOIN role ON user_role.role_id = role.id
GROUP BY user.first;

Also if the user's name is not unique, you should:

GROUP BY user.id, user.first

Also the aliases of columns must not be enclosed in single quotes. Use backticks instead.


问:
Putting RUN commands in one line make build faster?
In my docker file, does it really matter if I put RUN commands in one line or not? Does putting them in one line make build time faster?

```<code>RUN apt-get update
RUN apt-get -y install --no-install-recommends python3
RUN apt-get -y install --no-install-recommends open-vm-tools
</code>```

vs.

```<code>RUN apt-get update && apt-get -y install --no-install-recommends python3
RUN apt-get -y install --no-install-recommends open-vm-tools
</code>```

答:
Practically speaking, the build- and run-time cost of having one RUN command vs. several will be imperceptible, and I wouldn't try to optimize here solely in the name of performance.

In the specific example you show, a couple of things are true:

The Debian APT tool on its own has a non-trivial startup time; and separate from one RUN command vs. several, one apt-get install vs. two will be faster.

RUN apt-get install -y --no-install-recommends python3 open-vm-tools
```</li>
<li><p>Debian and Ubuntu update their repositories fairly frequently, and when they do, package links that were in last week's <code>apt-get update</code> stop working.  Meanwhile, Docker layer caching will try to avoid re-running a step that it's already run.  If you did build your image a week ago, Docker will say "I already did this <code>RUN apt-get update</code> and so I don't need to run it again"; but that means it's cached a stale package index.  <strong>It's important to run <code>apt-get update</code> and <code>apt-get install</code> in the same RUN step.</strong></p>

<pre class="lang-sh prettyprint-override"><code>RUN apt-get update \
 && apt-get install -y --no-install-recommends python3 open-vm-tools
</code>```

And in general:

If you have some cleanup steps you want to run, it's important to run them in the same RUN step.  This sequence creates a layer after the build step, and so the RUN rm step doesn't actually make the final layer smaller.

# All of this example should be combined into a single RUN step
RUN tar xzf package-1.2.3.tar.gz
RUN cd package-1.2.3 && ./configure && make && make install
# There is a layer here including the build tree
RUN rm -rf package-1.2.3
```</li>
<li><p>If you're iterating on a Dockerfile, it can be easier to split things out into many small RUN commands while you're debugging, and combine them later.  That's perfectly fine and you should get an identical tree at the end.</p>

<pre class="lang-sh prettyprint-override"><code># I'm trying to figure out the configure options so I might
RUN tar xzf package-1.2.3.tar.gz
RUN cd package-1.2.3 && ./configure --some-option
RUN cd package-1.2.3 && make
</code>```

Body:
In my docker file, does it really matter if I put RUN commands in one line or not? Does putting them in one line make build time faster?

RUN apt-get update
RUN apt-get -y install --no-install-recommends python3
RUN apt-get -y install --no-install-recommends open-vm-tools

vs.

RUN apt-get update && apt-get -y install --no-install-recommends python3
RUN apt-get -y install --no-install-recommends open-vm-tools

Body:
Practically speaking, the build- and run-time cost of having one RUN command vs. several will be imperceptible, and I wouldn't try to optimize here solely in the name of performance.

In the specific example you show, a couple of things are true:

The Debian APT tool on its own has a non-trivial startup time; and separate from one RUN command vs. several, one apt-get install vs. two will be faster.

RUN apt-get install -y --no-install-recommends python3 open-vm-tools

Debian and Ubuntu update their repositories fairly frequently, and when they do, package links that were in last week's apt-get update stop working.  Meanwhile, Docker layer caching will try to avoid re-running a step that it's already run.  If you did build your image a week ago, Docker will say "I already did this RUN apt-get update and so I don't need to run it again"; but that means it's cached a stale package index.  It's important to run apt-get update and apt-get install in the same RUN step.

RUN apt-get update \
 && apt-get install -y --no-install-recommends python3 open-vm-tools

And in general:

If you have some cleanup steps you want to run, it's important to run them in the same RUN step.  This sequence creates a layer after the build step, and so the RUN rm step doesn't actually make the final layer smaller.

# All of this example should be combined into a single RUN step
RUN tar xzf package-1.2.3.tar.gz
RUN cd package-1.2.3 && ./configure && make && make install
# There is a layer here including the build tree
RUN rm -rf package-1.2.3

If you're iterating on a Dockerfile, it can be easier to split things out into many small RUN commands while you're debugging, and combine them later.  That's perfectly fine and you should get an identical tree at the end.

# I'm trying to figure out the configure options so I might
RUN tar xzf package-1.2.3.tar.gz
RUN cd package-1.2.3 && ./configure --some-option
RUN cd package-1.2.3 && make


问:
Markov Chain from String
I am currently sitting on a problem considering Markov chains were an input is given in the form of a list of strings. This input has to be transformed into a Markov chain. I have been sitting on this problem already a couple of hours.

My idea: As you can see below I have tried to use the counter from collections to count all transitions, which has worked. Now I am trying to count all the tuples where A and B are the first elements. This gives me all possible transitions for A.

Then I'll count the transitions like (A, B).
Then I want to use these to create a matrix with all probabilities.

def markov(seq):

    states = Counter(seq).keys()
    liste = []
    print(states)
    a = zip(seq[:-1], seq[1:])
    print(list(a))

print(markov(["A","A","B","B","A","B","A","A","A"]))
```

So far I can't get the counting of the tuples to work.
Any help or new ideas on how to solve this is appreciated

答:
I didn't get exactly what you wanted but here is what I think it is:</p>

```<code>from collections import Counter

def count_occurence(seq):

    counted_states = []
    transition_dict = {}
    for tup in seq:
        if tup not in counted_states:
            transition_dict[tup] = seq.count(tup)
        counted_states.append(tup)
    print(transition_dict)
    #{('A', 'A'): 3, ('A', 'B'): 2, ('B', 'B'): 1, ('B', 'A'): 2}

def markov(seq):

    states = Counter(seq).keys()
    print(states)
    #dict_keys(['A', 'B'])
    a = list(zip(seq[:-1], seq[1:]))
    print(a)
    #[('A', 'A'), ('A', 'B'), ('B', 'B'), ('B', 'A'), ('A', 'B'), ('B',
    #'A'), ('A', 'A'), ('A', 'A')]
    return a

seq = markov(["A","A","B","B","A","B","A","A","A"])
count_occurence(seq)
</code>```

Title: Markov Chain from String

Body:
I am currently sitting on a problem considering Markov chains were an input is given in the form of a list of strings. This input has to be transformed into a Markov chain. I have been sitting on this problem already a couple of hours.

My idea: As you can see below I have tried to use the counter from collections to count all transitions, which has worked. Now I am trying to count all the tuples where A and B are the first elements. This gives me all possible transitions for A.

Then I'll count the transitions like (A, B).
Then I want to use these to create a matrix with all probabilities.

def markov(seq):

    states = Counter(seq).keys()
    liste = []
    print(states)
    a = zip(seq[:-1], seq[1:])
    print(list(a))

print(markov(["A","A","B","B","A","B","A","A","A"]))

So far I can't get the counting of the tuples to work.
Any help or new ideas on how to solve this is appreciated

Body:
I didn't get exactly what you wanted but here is what I think it is:

from collections import Counter

def count_occurence(seq):

    counted_states = []
    transition_dict = {}
    for tup in seq:
        if tup not in counted_states:
            transition_dict[tup] = seq.count(tup)
        counted_states.append(tup)
    print(transition_dict)
    #{('A', 'A'): 3, ('A', 'B'): 2, ('B', 'B'): 1, ('B', 'A'): 2}

def markov(seq):

    states = Counter(seq).keys()
    print(states)
    #dict_keys(['A', 'B'])
    a = list(zip(seq[:-1], seq[1:]))
    print(a)
    #[('A', 'A'), ('A', 'B'), ('B', 'B'), ('B', 'A'), ('A', 'B'), ('B',
    #'A'), ('A', 'A'), ('A', 'A')]
    return a

seq = markov(["A","A","B","B","A","B","A","A","A"])
count_occurence(seq)


问:
How to use anchor link or HTML in ternary operator in thymeleaf

I encountered a problem that need to write some HTML codes in ternary operator in Thymeleaf. In where I need to chose an anchor link by using ternary operator. For better understand, I putted those problematic code in below:

```<code><span th:text="${error_code == '404'} ? '<a href="http://localhost:8080/home">Home</a>' : '<a href="http://localhost:8080/login">Login</a>'"></span>
</code>```

So, how can I wirte those code correclty in Themeleaf

答:
This should do it :

```<code><span th:utext="((${error_code} == '404') ? '<a href="http://localhost:8080/home">Home</a>' : '<a href="http://localhost:8080/login">Login</a>')"></span>
</code>```

" is for  escaping double quote in html.

utext is to tell thymeleaf not to print plain text as "< a href.. "

Body:
I encountered a problem that need to write some HTML codes in ternary operator in Thymeleaf. In where I need to chose an anchor link by using ternary operator. For better understand, I putted those problematic code in below:

<span th:text="${error_code == '404'} ? 'Home' : 'Login'">

So, how can I wirte those code correclty in Themeleaf

Body:
This should do it :

<span th:utext="((${error_code} == '404') ? '<a href=&quot;http://localhost:8080/home&quot;>Home</a>' : '<a href=&quot;http://localhost:8080/login&quot;>Login</a>')"></span>

&quot; is for  escaping double quote in html.

utext is to tell thymeleaf not to print plain text as "< a href.. "


问:
fetching data and adding title to Json object
I would like to add title to my JSON object, the structure I wish to achieve is:

```<code>{
"posts": [
    {
        "title": "put title here",
        "upvotes": 1234,
        "score": 1000,
        "num_comments": 100,
        "created": "16.05.2019 12:12",
    },
]
}
</code>```

I was able to fetch data and put it into array of 26 elements, everything is fine but I wish to somehow add this "posts:" to be above whole rest, here is my code:

```<code>fetch("https://www.reddit.com/r/funny.json")
.then(resp => resp.json()
.then(async res => {
   let posts = await res.data.children.map(el => {
    let title = el.data.title;
    let upvote = el.data.ups;
    let score = el.data.score;
    let comments = el.data.num_comments;
    let created = el.data.created;
    const allPosts = {title, upvote, score, comments, created}
    postList.push(allPosts)
    return postList
})
console.log(posts);

    return posts
})
</code>```

答:
You might need to create the object like below

{propertyName:value}

```<code>    const allPosts = {title:title,upvote: upvote,score: score,comments: comments, created:created}
postList.push(allPosts)
</code>```

Body:
I would like to add title to my JSON object, the structure I wish to achieve is:

{
"posts": [
    {
        "title": "put title here",
        "upvotes": 1234,
        "score": 1000,
        "num_comments": 100,
        "created": "16.05.2019 12:12",
    },
]
}

I was able to fetch data and put it into array of 26 elements, everything is fine but I wish to somehow add this "posts:" to be above whole rest, here is my code:

fetch("https://www.reddit.com/r/funny.json")
.then(resp => resp.json()
.then(async res => {
   let posts = await res.data.children.map(el => {
    let title = el.data.title;
    let upvote = el.data.ups;
    let score = el.data.score;
    let comments = el.data.num_comments;
    let created = el.data.created;
    const allPosts = {title, upvote, score, comments, created}
    postList.push(allPosts)
    return postList
})
console.log(posts);

    return posts
})

Body:
You might need to create the object like below

{propertyName:value}

    const allPosts = {title:title,upvote: upvote,score: score,comments: comments, created:created}
postList.push(allPosts)


问:
I am trying to return the complete list of nested document after $unwind
Please forgive my brevity, I am new to MongoDB aggregate framework. I have the following collection with "history" as a nested document and I am trying to project the matching collection together with the full list of history but my query is only returning the matched element in the nested history.

```<code>{
    "_id" : ObjectId("5cc75fbcd1de1e96e0925a7e"),
    "case_id" : "5002J00001E2JheQAF",
    "__v" : 0,
    "account_id" : "0015000000xxYAWAA2",
    "account_name" : "HelloWorld",
    "caseNumber" : "C1000013634",
    "caseOrigin" : "Email",
    "case_type" : "A-Partner",
    "create_date" : ISODate("2019-04-29T20:15:48.000Z"),
    "current_mrr" : null,
    "escalated_team" : null,
    "history" : [
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a85"),
            "history_id" : "0172J0000tXXzkuQAD",
            "field" : "created",
            "new_value" : null,
            "old_value" : null,
            "created_date" : ISODate("2019-04-29T20:15:52.000Z"),
            "created_by_id" : "005500000071WBzAAM"
        },
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a84"),
            "history_id" : "0172J0000tXXzkyQAD",
            "field" : "Provider_Group__c",
            "new_value" : "HelloWorld Provisioning",
            "old_value" : "SGS - Web and Chat",
            "created_date" : ISODate("2019-04-29T20:15:52.000Z"),
            "created_by_id" : "005500000071WBzAAM"
        },
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a7d"),
            "history_id" : "0172J0000tYzZgoQAF",
            "field" : "Status",
            "new_value" : "In Progress",
            "old_value" : "Pending Customer Response",
            "created_date" : ISODate("2019-04-30T13:38:38.000Z"),
            "created_by_id" : "005500000071WBzAAM"
        },
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a7b"),
            "history_id" : "0172J0000tYzbgJQAR",
            "field" : "Status",
            "new_value" : "Resolved",
            "old_value" : "In Progress",
            "created_date" : ISODate("2019-04-30T13:54:44.000Z"),
            "created_by_id" : "0052J000007nM11QAE"
        }
    ],
  "feeds" : [
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a78"),
            "feed_id" : "0D52J00006qBd5PSAS",
            "event" : "TextPost",
            "created_by_id" : "0052J000007nM11QAE",
            "visibility" : "InternalUsers",
            "created_date" : ISODate("2019-04-30T13:54:42.000Z")
        },
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a77"),
            "feed_id" : "0D52J00006qBd4kSAC",
            "event" : "EmailMessageEvent",
            "created_by_id" : "0052J000007nM11QAE",
            "visibility" : "InternalUsers",
            "created_date" : ISODate("2019-04-30T13:54:32.000Z")
        },
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a76"),
            "feed_id" : "0D52J00006qBc4GSAS",
            "event" : "EmailMessageEvent",
            "created_by_id" : "005500000071WBzAAM",
            "visibility" : "InternalUsers",
            "created_date" : ISODate("2019-04-30T13:38:34.000Z")
        }
    ],
    "issue_level" : "HelloWorld",
    "last_month_service_mrr" : 572.866,
    "last_update" : ISODate("2019-05-14T14:00:39.000Z"),
    "owner" : "Ajay AJAY",
    "owner_id" : "0052J000007nM11QAE",
    "product" : "Virtual World",
    "provider_group" : "HelloWorld Provisioning",
    "provider_group_manager" : null,
    "provider_group_manager_email" : null,
    "resolve_date" : ISODate("2019-04-30T13:54:42.000Z"),
    "rt" : null,
    "service_mrr" : 583,
    "severity" : null,
    "status" : "Closed",
    "subject" : "HelloWorld - The Complex - 7516664 - Unsuspend",
    "support_country" : null,
    "support_group" : "Support - ETS"
}
</code>```

Here is my query

```<code>db.getCollection('cases').aggregate([
{
     $match: {
          "history.created_date": {
               '$gte': new Date("2019-04-01T00:00:00"),
               '$lte': new Date("2019-04-30T23:59:59")
          }
     }
},
{
    "$unwind": "$history"
}
,{
    "$match": {
        "history.created_date":{
               '$gte': new Date("2019-04-01T00:00:00"),
               '$lte': new Date("2019-04-30T23:59:59")
        },
        "$or" :[{"history.new_value":{
            "$in":['Tier 2','Global Application Support Team','HelloWorld Provisioning'],
        }},{
            "history.field" : "Resolved"
        }],

        "history.old_value":{
            "$nin":['HelloWorld Provisioning','Global Application Support Team'],
        },
     }
}
,{
     $group: {
          "_id": {
              year: {
                  $year: "$history.created_date"
              },
              month: {
                  $month: "$history.created_date"
              },
              week: {
                  $week: "$history.created_date"
              }
          },
          cases: {
                        $push: {
                            caseNumber: "$caseNumber",
                            account_name: "$account_name",
                            status: "$status",
                            owner: "$owner",
                            owner_id: "$owner_id",
                            create_date: "$create_date",
                            last_update: "$last_update",
                            resolve_date: "$resolve_date",
                            provider_group: "$provider_group",
                            issue_level: "$issue_level",
                            case_type: "$case_type",
                            account_id: "$account_id",
                            history: "$history",
                            feeds: "$feeds",
                            product: "$product",
                            severity: "$severity",
                            support_group: "$support_group"
                        }
                    }
     }
},{
                $project: {
                    "dates": "$_id",
                    _id: 0,
                    "cases": "$cases"
                }
            },
            {$sort: {_id: 1}}
])

The result i get is the matched element of the flattened array.

{
    "dates" : {
        "year" : 2019,
        "month" : 4,
        "week" : 14
    },
    "totalCases" : 86.0,
    "cases" : [
        {
            "caseNumber" : "C1781724",
            "account_name" : "American Fed of Govt Emp",
            "status" : "Closed",
            "owner" : "Hy Van",
            "owner_id" : "005500000071TgPAAU",
            "create_date" : ISODate("2019-04-10T22:32:40.000Z"),
            "last_update" : ISODate("2019-04-29T18:45:42.000Z"),
            "resolve_date" : ISODate("2019-04-15T18:34:50.000Z"),
            "provider_group" : "Tier 2",
            "issue_level" : "Phone Service",
            "case_type" : "Issue-Fault",
            "account_id" : "001500000168SrjAAE",
            "history" : {
                "_id" : ObjectId("5cc75fcd55e11149c3366285"),
                "history_id" : "0172J0000t73NlXQAU",
                "field" : "Provider_Group__c",
                "new_value" : "Tier 2",
                "old_value" : "SGS - Web and Chat",
                "created_date" : ISODate("2019-04-11T19:10:22.000Z"),
                "created_by_id" : "005500000071TgXAAU"
            },
            "feeds" : [
                {
                    "_id" : ObjectId("5cc75fcd55e11149c336627a"),
                    "feed_id" : "0D52J00006omQnySAE",
                    "event" : "EmailMessageEvent",
                    "created_by_id" : "005500000071TgPAAU",
                    "visibility" : "InternalUsers",
                    "created_date" : ISODate("2019-04-15T18:34:52.000Z")
                },
                {
                    "_id" : ObjectId("5cc75fcd55e11149c3366279"),
                    "feed_id" : "0D52J00006omQnxSAE",
                    "event" : "TextPost",
                    "created_by_id" : "005500000071TgPAAU",
                    "visibility" : "InternalUsers",
                    "created_date" : ISODate("2019-04-15T18:34:50.000Z")
                },
                {
                    "_id" : ObjectId("5cc75fcd55e11149c3366278"),
                    "feed_id" : "0D52J00006oGTMwSAO",
                    "event" : "EmailMessageEvent",
                    "created_by_id" : "005500000071WBzAAM",
                    "visibility" : "AllUsers",
                    "created_date" : ISODate("2019-04-12T16:15:47.000Z")
                },
                {
                    "_id" : ObjectId("5cc75fcd55e11149c3366277"),
                    "feed_id" : "0D52J00006oGQhvSAG",
                    "event" : "TextPost",
                    "created_by_id" : "00550000007gdQEAAY",
                    "visibility" : "InternalUsers",
                    "created_date" : ISODate("2019-04-12T15:47:26.000Z")
                },
                {
                    "_id" : ObjectId("5cc75fcd55e11149c3366276"),
                    "feed_id" : "0D52J00006oGQXTSA4",
                    "event" : "TextPost",
                    "created_by_id" : "00550000007gdQEAAY",
                    "visibility" : "InternalUsers",
                    "created_date" : ISODate("2019-04-12T15:45:30.000Z")
                },
                {
                    "_id" : ObjectId("5cc75fcd55e11149c3366275"),
                    "feed_id" : "0D52J00006oER2gSAG",
                    "event" : "EmailMessageEvent",
                    "created_by_id" : "0052J0000082whRQAQ",
                    "visibility" : "AllUsers",
                    "created_date" : ISODate("2019-04-10T22:32:42.000Z")
                }
            ],
            "product" : "Virtual Office",
            "severity" : null,
            "support_group" : "Support - SMB"
        },
        .
        .
        .
}
</code>```

What can I do to return the full list of history?

答:
My query was okay for what I wanted to achieve but it didn't return the full list of histories for the matched document. The only way was to make a second query to retrieve the full document using the cases returned. </p>

```<code>db.getCollection('cases').aggregate([
{
     $match: {
          "history.created_date": {
               '$gte': new Date("2019-04-01T00:00:00"),
               '$lte': new Date("2019-04-30T23:59:59")
          }
     }
},
{
    "$unwind": "$history"
}
,{
    "$match": {
        "history.created_date":{
               '$gte': new Date("2019-04-01T00:00:00"),
               '$lte': new Date("2019-04-30T23:59:59")
        },
        "$or" :[{"history.new_value":{
            "$in":['Tier 2','Global Application Support Team','HelloWorld Provisioning'],
        }},{
            "history.field" : "Resolved"
        }],

        "history.old_value":{
            "$nin":['HelloWorld Provisioning','Global Application Support Team'],
        },
     }
}
,{
     $group: {
          "_id": {
              year: {
                  $year: "$history.created_date"
              },
              month: {
                  $month: "$history.created_date"
              },
              week: {
                  $week: "$history.created_date"
              }
          },
          cases:  {$addToSet: "$caseNumber"}
     }
},{
                $project: {
                    "dates": "$_id",
                    _id: 0,
                    "cases": "$cases"
                }
            },
            {$sort: {_id: 1}}
])
</code>```

Body:
Please forgive my brevity, I am new to MongoDB aggregate framework. I have the following collection with "history" as a nested document and I am trying to project the matching collection together with the full list of history but my query is only returning the matched element in the nested history.

{
    "_id" : ObjectId("5cc75fbcd1de1e96e0925a7e"),
    "case_id" : "5002J00001E2JheQAF",
    "__v" : 0,
    "account_id" : "0015000000xxYAWAA2",
    "account_name" : "HelloWorld",
    "caseNumber" : "C1000013634",
    "caseOrigin" : "Email",
    "case_type" : "A-Partner",
    "create_date" : ISODate("2019-04-29T20:15:48.000Z"),
    "current_mrr" : null,
    "escalated_team" : null,
    "history" : [
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a85"),
            "history_id" : "0172J0000tXXzkuQAD",
            "field" : "created",
            "new_value" : null,
            "old_value" : null,
            "created_date" : ISODate("2019-04-29T20:15:52.000Z"),
            "created_by_id" : "005500000071WBzAAM"
        },
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a84"),
            "history_id" : "0172J0000tXXzkyQAD",
            "field" : "Provider_Group__c",
            "new_value" : "HelloWorld Provisioning",
            "old_value" : "SGS - Web and Chat",
            "created_date" : ISODate("2019-04-29T20:15:52.000Z"),
            "created_by_id" : "005500000071WBzAAM"
        },
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a7d"),
            "history_id" : "0172J0000tYzZgoQAF",
            "field" : "Status",
            "new_value" : "In Progress",
            "old_value" : "Pending Customer Response",
            "created_date" : ISODate("2019-04-30T13:38:38.000Z"),
            "created_by_id" : "005500000071WBzAAM"
        },
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a7b"),
            "history_id" : "0172J0000tYzbgJQAR",
            "field" : "Status",
            "new_value" : "Resolved",
            "old_value" : "In Progress",
            "created_date" : ISODate("2019-04-30T13:54:44.000Z"),
            "created_by_id" : "0052J000007nM11QAE"
        }
    ],
  "feeds" : [
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a78"),
            "feed_id" : "0D52J00006qBd5PSAS",
            "event" : "TextPost",
            "created_by_id" : "0052J000007nM11QAE",
            "visibility" : "InternalUsers",
            "created_date" : ISODate("2019-04-30T13:54:42.000Z")
        },
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a77"),
            "feed_id" : "0D52J00006qBd4kSAC",
            "event" : "EmailMessageEvent",
            "created_by_id" : "0052J000007nM11QAE",
            "visibility" : "InternalUsers",
            "created_date" : ISODate("2019-04-30T13:54:32.000Z")
        },
        {
            "_id" : ObjectId("5cdad34e4520d570fc0f6a76"),
            "feed_id" : "0D52J00006qBc4GSAS",
            "event" : "EmailMessageEvent",
            "created_by_id" : "005500000071WBzAAM",
            "visibility" : "InternalUsers",
            "created_date" : ISODate("2019-04-30T13:38:34.000Z")
        }
    ],
    "issue_level" : "HelloWorld",
    "last_month_service_mrr" : 572.866,
    "last_update" : ISODate("2019-05-14T14:00:39.000Z"),
    "owner" : "Ajay AJAY",
    "owner_id" : "0052J000007nM11QAE",
    "product" : "Virtual World",
    "provider_group" : "HelloWorld Provisioning",
    "provider_group_manager" : null,
    "provider_group_manager_email" : null,
    "resolve_date" : ISODate("2019-04-30T13:54:42.000Z"),
    "rt" : null,
    "service_mrr" : 583,
    "severity" : null,
    "status" : "Closed",
    "subject" : "HelloWorld - The Complex - 7516664 - Unsuspend",
    "support_country" : null,
    "support_group" : "Support - ETS"
}

Here is my query

db.getCollection('cases').aggregate([
{
     $match: {
          "history.created_date": {
               '$gte': new Date("2019-04-01T00:00:00"),
               '$lte': new Date("2019-04-30T23:59:59")
          }
     }
},
{
    "$unwind": "$history"
}
,{
    "$match": {
        "history.created_date":{
               '$gte': new Date("2019-04-01T00:00:00"),
               '$lte': new Date("2019-04-30T23:59:59")
        },
        "$or" :[{"history.new_value":{
            "$in":['Tier 2','Global Application Support Team','HelloWorld Provisioning'],
        }},{
            "history.field" : "Resolved"
        }],

        "history.old_value":{
            "$nin":['HelloWorld Provisioning','Global Application Support Team'],
        },
     }
}
,{
     $group: {
          "_id": {
              year: {
                  $year: "$history.created_date"
              },
              month: {
                  $month: "$history.created_date"
              },
              week: {
                  $week: "$history.created_date"
              }
          },
          cases: {
                        $push: {
                            caseNumber: "$caseNumber",
                            account_name: "$account_name",
                            status: "$status",
                            owner: "$owner",
                            owner_id: "$owner_id",
                            create_date: "$create_date",
                            last_update: "$last_update",
                            resolve_date: "$resolve_date",
                            provider_group: "$provider_group",
                            issue_level: "$issue_level",
                            case_type: "$case_type",
                            account_id: "$account_id",
                            history: "$history",
                            feeds: "$feeds",
                            product: "$product",
                            severity: "$severity",
                            support_group: "$support_group"
                        }
                    }
     }
},{
                $project: {
                    "dates": "$_id",
                    _id: 0,
                    "cases": "$cases"
                }
            },
            {$sort: {_id: 1}}
])

The result i get is the matched element of the flattened array.

{
    "dates" : {
        "year" : 2019,
        "month" : 4,
        "week" : 14
    },
    "totalCases" : 86.0,
    "cases" : [
        {
            "caseNumber" : "C1781724",
            "account_name" : "American Fed of Govt Emp",
            "status" : "Closed",
            "owner" : "Hy Van",
            "owner_id" : "005500000071TgPAAU",
            "create_date" : ISODate("2019-04-10T22:32:40.000Z"),
            "last_update" : ISODate("2019-04-29T18:45:42.000Z"),
            "resolve_date" : ISODate("2019-04-15T18:34:50.000Z"),
            "provider_group" : "Tier 2",
            "issue_level" : "Phone Service",
            "case_type" : "Issue-Fault",
            "account_id" : "001500000168SrjAAE",
            "history" : {
                "_id" : ObjectId("5cc75fcd55e11149c3366285"),
                "history_id" : "0172J0000t73NlXQAU",
                "field" : "Provider_Group__c",
                "new_value" : "Tier 2",
                "old_value" : "SGS - Web and Chat",
                "created_date" : ISODate("2019-04-11T19:10:22.000Z"),
                "created_by_id" : "005500000071TgXAAU"
            },
            "feeds" : [
                {
                    "_id" : ObjectId("5cc75fcd55e11149c336627a"),
                    "feed_id" : "0D52J00006omQnySAE",
                    "event" : "EmailMessageEvent",
                    "created_by_id" : "005500000071TgPAAU",
                    "visibility" : "InternalUsers",
                    "created_date" : ISODate("2019-04-15T18:34:52.000Z")
                },
                {
                    "_id" : ObjectId("5cc75fcd55e11149c3366279"),
                    "feed_id" : "0D52J00006omQnxSAE",
                    "event" : "TextPost",
                    "created_by_id" : "005500000071TgPAAU",
                    "visibility" : "InternalUsers",
                    "created_date" : ISODate("2019-04-15T18:34:50.000Z")
                },
                {
                    "_id" : ObjectId("5cc75fcd55e11149c3366278"),
                    "feed_id" : "0D52J00006oGTMwSAO",
                    "event" : "EmailMessageEvent",
                    "created_by_id" : "005500000071WBzAAM",
                    "visibility" : "AllUsers",
                    "created_date" : ISODate("2019-04-12T16:15:47.000Z")
                },
                {
                    "_id" : ObjectId("5cc75fcd55e11149c3366277"),
                    "feed_id" : "0D52J00006oGQhvSAG",
                    "event" : "TextPost",
                    "created_by_id" : "00550000007gdQEAAY",
                    "visibility" : "InternalUsers",
                    "created_date" : ISODate("2019-04-12T15:47:26.000Z")
                },
                {
                    "_id" : ObjectId("5cc75fcd55e11149c3366276"),
                    "feed_id" : "0D52J00006oGQXTSA4",
                    "event" : "TextPost",
                    "created_by_id" : "00550000007gdQEAAY",
                    "visibility" : "InternalUsers",
                    "created_date" : ISODate("2019-04-12T15:45:30.000Z")
                },
                {
                    "_id" : ObjectId("5cc75fcd55e11149c3366275"),
                    "feed_id" : "0D52J00006oER2gSAG",
                    "event" : "EmailMessageEvent",
                    "created_by_id" : "0052J0000082whRQAQ",
                    "visibility" : "AllUsers",
                    "created_date" : ISODate("2019-04-10T22:32:42.000Z")
                }
            ],
            "product" : "Virtual Office",
            "severity" : null,
            "support_group" : "Support - SMB"
        },
        .
        .
        .
}

What can I do to return the full list of history?

Body:
My query was okay for what I wanted to achieve but it didn't return the full list of histories for the matched document. The only way was to make a second query to retrieve the full document using the cases returned.

db.getCollection('cases').aggregate([
{
     $match: {
          "history.created_date": {
               '$gte': new Date("2019-04-01T00:00:00"),
               '$lte': new Date("2019-04-30T23:59:59")
          }
     }
},
{
    "$unwind": "$history"
}
,{
    "$match": {
        "history.created_date":{
               '$gte': new Date("2019-04-01T00:00:00"),
               '$lte': new Date("2019-04-30T23:59:59")
        },
        "$or" :[{"history.new_value":{
            "$in":['Tier 2','Global Application Support Team','HelloWorld Provisioning'],
        }},{
            "history.field" : "Resolved"
        }],

        "history.old_value":{
            "$nin":['HelloWorld Provisioning','Global Application Support Team'],
        },
     }
}
,{
     $group: {
          "_id": {
              year: {
                  $year: "$history.created_date"
              },
              month: {
                  $month: "$history.created_date"
              },
              week: {
                  $week: "$history.created_date"
              }
          },
          cases:  {$addToSet: "$caseNumber"}
     }
},{
                $project: {
                    "dates": "$_id",
                    _id: 0,
                    "cases": "$cases"
                }
            },
            {$sort: {_id: 1}}
])


问:
Upgrade from Wheezy to Jessie: heartbeat + pacemaker, Using legacy config location: /var/lib/heartbeat/crm but doesn't seem to work
Trying to upgrade from Wheezy to Jessie (I know, late).

I already found out that despite heartbeat,pacemaker and corosync version numbers barely changing, there is big change in how it's supposed to work. I'm using this article https://wiki.debian.org/Debian-HA/ClustersFromScratch to install it.

However, I'm unable to start the cluster with original configuration. It reports
ERROR: status: crm_mon (rc=107): Connection to cluster failed: Transport endpoint is not connected

I noticed in log

```<code>May 25 01:07:59 [4989] domainname.com        cib:   notice: main:      Using legacy config location: /var/lib/heartbeat/crm
May 25 01:07:59 [4989] domainname.com        cib:     info: get_cluster_type:  Verifying cluster type: 'corosync'
May 25 01:07:59 [4989] domainname.com        cib:     info: get_cluster_type:  Assuming an active 'corosync' cluster
May 25 01:07:59 [4989] domainname.com        cib:     info: retrieveCib:       Reading cluster configuration file /var/lib/heartbeat/crm/cib.xml (digest: /var/lib/heartbeat/crm
May 25 01:07:59 [4992] domainname.com      attrd:     info: corosync_node_name:        Unable to get node name for nodeid 2130...
May 25 01:07:59 [4992] domainname.com      attrd:   notice: get_node_name:     Defaulting to uname -n for the local corosync node name
May 25 01:07:59 [4992] domainname.com      attrd:     info: crm_get_peer:      Node 2130... is now known as domainname.com
May 25 01:07:59 [4990] domainname.com stonith-ng:     info: corosync_node_name:        Unable to get node name for nodeid 2130...
May 25 01:07:59 [4990] domainname.com stonith-ng:   notice: get_node_name:     Defaulting to uname -n for the local corosync node name
May 25 01:07:59 [4990] domainname.com stonith-ng:     info: crm_get_peer:      Node 2130... is now known as domainname.com
May 25 01:07:59 [4992] domainname.com      attrd:     info: main:      Cluster connection active
May 25 01:07:59 [4992] domainname.com      attrd:     info: qb_ipcs_us_publish:        server name: attrd
May 25 01:07:59 [4992] domainname.com      attrd:     info: main:      Accepting attribute updates
May 25 01:07:59 [4989] domainname.com        cib:     info: validate_with_relaxng:     Creating RNG parser context
May 25 01:07:59 [4987] domainname.com pacemakerd:    error: pcmk_child_exit:   The cib process (4989) exited: Key has expired (127)
</code>```

and when I remove /var/lib/heartbeat/crm it at least starts so I can do crm status.

Now: the question: is the old configuration supposed to work and I should search elsewhere (the log is HUGE), or will I have simpler to remove the directory and just define the four resources I have again?

For reference, version numbers:
Wheezy:

```<code>pacemaker/wheezy uptodate 1.1.7-1
libcorosync4/wheezy uptodate 1.4.2-3
heartbeat/wheezy uptodate 1:3.0.5-3
libheartbeat2/wheezy uptodate 1:3.0.5-3
</code>```

Jessie:

```<code>pacemaker:amd64/jessie-backports 1.1.16-1~bpo8+1 uptodate
corosync:amd64/jessie-backports 2.4.2-3+deb9u1~bpo8+1 uptodate
libcorosync-common4:amd64/jessie-backports 2.4.2-3+deb9u1~bpo8+1 uptodate
libcorosync4:all/jessie 1.4.6-1.1 uptodate
heartbeat:amd64/jessie 1:3.0.5+hg12629-1.2 uptodate
libheartbeat2:amd64/jessie 1:3.0.5+hg12629-1.2 uptodate
</code>```

答:
Reading http://www.linux-ha.org/doc/users-guide/_upgrading_from_crm_enabled_heartbeat_2_1_clusters.html#_backing_up_the_cib it seems that the correct course of action will be to remove everything in /var/lib/heartbeat/crm EXCEPT /var/lib/heartbeat/crm/cib.xml

... not sure why the heartbeat/wheezy 3.0.5-3 would be like 2.1 but it does make sense ...

... hmmm no doesn't work.

Body:
Trying to upgrade from Wheezy to Jessie (I know, late).

I already found out that despite heartbeat,pacemaker and corosync version numbers barely changing, there is big change in how it's supposed to work. I'm using this article https://wiki.debian.org/Debian-HA/ClustersFromScratch to install it.

However, I'm unable to start the cluster with original configuration. It reports
ERROR: status: crm_mon (rc=107): Connection to cluster failed: Transport endpoint is not connected

I noticed in log

May 25 01:07:59 [4989] domainname.com        cib:   notice: main:      Using legacy config location: /var/lib/heartbeat/crm
May 25 01:07:59 [4989] domainname.com        cib:     info: get_cluster_type:  Verifying cluster type: 'corosync'
May 25 01:07:59 [4989] domainname.com        cib:     info: get_cluster_type:  Assuming an active 'corosync' cluster
May 25 01:07:59 [4989] domainname.com        cib:     info: retrieveCib:       Reading cluster configuration file /var/lib/heartbeat/crm/cib.xml (digest: /var/lib/heartbeat/crm
May 25 01:07:59 [4992] domainname.com      attrd:     info: corosync_node_name:        Unable to get node name for nodeid 2130...
May 25 01:07:59 [4992] domainname.com      attrd:   notice: get_node_name:     Defaulting to uname -n for the local corosync node name
May 25 01:07:59 [4992] domainname.com      attrd:     info: crm_get_peer:      Node 2130... is now known as domainname.com
May 25 01:07:59 [4990] domainname.com stonith-ng:     info: corosync_node_name:        Unable to get node name for nodeid 2130...
May 25 01:07:59 [4990] domainname.com stonith-ng:   notice: get_node_name:     Defaulting to uname -n for the local corosync node name
May 25 01:07:59 [4990] domainname.com stonith-ng:     info: crm_get_peer:      Node 2130... is now known as domainname.com
May 25 01:07:59 [4992] domainname.com      attrd:     info: main:      Cluster connection active
May 25 01:07:59 [4992] domainname.com      attrd:     info: qb_ipcs_us_publish:        server name: attrd
May 25 01:07:59 [4992] domainname.com      attrd:     info: main:      Accepting attribute updates
May 25 01:07:59 [4989] domainname.com        cib:     info: validate_with_relaxng:     Creating RNG parser context
May 25 01:07:59 [4987] domainname.com pacemakerd:    error: pcmk_child_exit:   The cib process (4989) exited: Key has expired (127)

and when I remove /var/lib/heartbeat/crm it at least starts so I can do crm status.

Now: the question: is the old configuration supposed to work and I should search elsewhere (the log is HUGE), or will I have simpler to remove the directory and just define the four resources I have again?

For reference, version numbers:
Wheezy:

pacemaker/wheezy uptodate 1.1.7-1
libcorosync4/wheezy uptodate 1.4.2-3
heartbeat/wheezy uptodate 1:3.0.5-3
libheartbeat2/wheezy uptodate 1:3.0.5-3

Jessie:

pacemaker:amd64/jessie-backports 1.1.16-1~bpo8+1 uptodate
corosync:amd64/jessie-backports 2.4.2-3+deb9u1~bpo8+1 uptodate
libcorosync-common4:amd64/jessie-backports 2.4.2-3+deb9u1~bpo8+1 uptodate
libcorosync4:all/jessie 1.4.6-1.1 uptodate
heartbeat:amd64/jessie 1:3.0.5+hg12629-1.2 uptodate
libheartbeat2:amd64/jessie 1:3.0.5+hg12629-1.2 uptodate


问:
Is there a way to score a list that have its members in another list?
I have two lists. The phrase list contain both words and phrases while the check list contains only words. I want to confirm if any member of the check list is a part or in the phrase list. I will give each string in the phrase list a score depending on its membership in the check list.

In the example below, information retrieval yields 0.5 because only one word ("information") out of two is in the check list. On the other hand, wave transformation yields 1 because both "wave" and "transformation" appear in check.

I have looked for how to do it but none has worked

```<code>score = []
phrase = ['information retrieval', 'wave transformation', 'information', 'services', 'gold coast village']
check = ['information', 'wave', 'transformation', 'village', 'services']
</code>```

I expect the score list to contain the score for each member of the phrase list.

```<code>phrase = ['information retrieval', 'wave transformation', 'information', 'services', 'gold coast village']
score = [0.5, 1, 1, 1, 0.33]
</code>```

答:
You could use the statistics module to get the scores given that each word of a phrase will count for either 1 or 0 depending if it's present or absent from the check list:

```<code>import statistics as stats
score = [stats.mean(w in check for w in p.split()) for p in phrase]
</code>```

To make this go faster you should define check as a set instead of a list.

Body:
I have two lists. The phrase list contain both words and phrases while the check list contains only words. I want to confirm if any member of the check list is a part or in the phrase list. I will give each string in the phrase list a score depending on its membership in the check list.

In the example below, information retrieval yields 0.5 because only one word ("information") out of two is in the check list. On the other hand, wave transformation yields 1 because both "wave" and "transformation" appear in check.

I have looked for how to do it but none has worked

score = []
phrase = ['information retrieval', 'wave transformation', 'information', 'services', 'gold coast village']
check = ['information', 'wave', 'transformation', 'village', 'services']

I expect the score list to contain the score for each member of the phrase list.

phrase = ['information retrieval', 'wave transformation', 'information', 'services', 'gold coast village']
score = [0.5, 1, 1, 1, 0.33]

Body:
You could use the statistics module to get the scores given that each word of a phrase will count for either 1 or 0 depending if it's present or absent from the check list:

import statistics as stats
score = [stats.mean(w in check for w in p.split()) for p in phrase]

To make this go faster you should define check as a set instead of a list.


问:
can scala compiler utilize multi-core CPU?
Is there any benefit from compiling Scala code on multi-CPU machine?

(Asking about scala compilation, not about scala code for multi-core processing)

答:
When I build a large Scala project my CPU is maxed on all virtual cores (quad core with hyperthreading). There is nothing special like Hydra at play, just a bunch of modules to build, which seem to go in parallel. Don’t forget that GC needs its own CPU time also.

One good way to experiment would be to spin up a cloud instance with a ton of cores, clone & build your project, and time it. When it next comes time to upgrade my PC I will certainly be doing this to see how many cores are worth having.


问:
If loop to choose which value to populate in textbox
I have a textbox Job in Form2, and I have multiple textboxes in Form1.

Lets say in form1 I have Textbox1 which is RollFromInventory, Textbox2 is RollFromMachine1, Texbox3 is RollFromMachine2 and so on, assume there are 4 other machines, so four other Textboxes.

When I want to populate the textbox Job in Form2, I want to write an If loop which should look for the textbox which has value populated in it, in form1 (there will be only one textbox which will have a value among all the available textboxes in form1), i.e. either RollFromInventory will have a value or RollFromMachine1 will have a value or RollFromMachine2..

I am unsure of the looping logic, so I cant really figure out how to go about it.

Currently the code I have written is mainly for populating concatenated values (I am not providing that code, because it will make the objective seem complicated).

答:
The simplest approach might be to populate the Control Source of your Job textbox in Form2 with a Switch statement similar to the following:

```<code>=Switch(
    [Forms]![Form1]![RollFromInventory] is not null, [Forms]![Form1]![RollFromInventory],
    [Forms]![Form1]![RollFromMachine1]  is not null, [Forms]![Form1]![RollFromMachine1],
    [Forms]![Form1]![RollFromMachine2]  is not null, [Forms]![Form1]![RollFromMachine2],
    [Forms]![Form1]![RollFromMachine3]  is not null, [Forms]![Form1]![RollFromMachine3]
)
</code>```

Though, this isn't particularly pretty and reeks of bad interface design - it sounds like a set of radio buttons and a single textbox would be more appropriate for this scenario.

If you want to implement this in VBA, you could use a set of nested If statements or Nz expressions, e.g.:

Forms![PrinterWaxLabel].JOB = Nz(Nz(InvPW, FHPW), WxPW)
```

Body:
The simplest approach might be to populate the Control Source of your Job textbox in Form2 with a Switch statement similar to the following:

=Switch(
    [Forms]![Form1]![RollFromInventory] is not null, [Forms]![Form1]![RollFromInventory],
    [Forms]![Form1]![RollFromMachine1]  is not null, [Forms]![Form1]![RollFromMachine1],
    [Forms]![Form1]![RollFromMachine2]  is not null, [Forms]![Form1]![RollFromMachine2],
    [Forms]![Form1]![RollFromMachine3]  is not null, [Forms]![Form1]![RollFromMachine3]
)

Though, this isn't particularly pretty and reeks of bad interface design - it sounds like a set of radio buttons and a single textbox would be more appropriate for this scenario.

If you want to implement this in VBA, you could use a set of nested If statements or Nz expressions, e.g.:

Forms![PrinterWaxLabel].JOB = Nz(Nz(InvPW, FHPW), WxPW)


问:
using puppeteer-recorder to record video of browser
I'm trying to record puppeteer to see what happens when i run it on server, as I understand this package does what i want.

https://www.npmjs.com/package/puppeteer-recorder

so here is simplified version of my code

```<code>const puppeteer = require('puppeteer');
const { record } = require('puppeteer-recorder');
var path = 'C:\\wamp64\\www\\home_robot\\';

init_puppeteer();

const global_browser  ;
async function init_puppeteer() {

        global_browser = await puppeteer.launch({headless: false , args: ['--no-sandbox', '--disable-setuid-sandbox']});

    check_login()
};

async function check_login()
{
    try {
        const page = await global_browser.newPage();
        await page.setViewport({width: 1000, height: 1100});

        await record({
            browser: global_browser, // Optional: a puppeteer Browser instance,
            page: page, // Optional: a puppeteer Page instance,
            output: path + 'output.webm',
            fps: 60,
            frames: 60 * 5, // 5 seconds at 60 fps
        prepare: function () {}, // <-- add this line
        render: function () {} // <-- add this line

        });

        await page.goto('https://www.example.cob', {timeout: 60000})
            .catch(function (error) {
                throw new Error('TimeoutBrows');
            });

        await page.close();
    }
    catch (e) {
        console.log(' LOGIN ERROR ---------------------');
        console.log(e);
    }
}
</code>```

But I get this error

```<code>    $ node home.js
(node:7376) UnhandledPromiseRejectionWarning: Error: spawn ffmpeg ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:7376) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejection
 id: 1)
(node:7376) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre
cated. In the future, promise rejections that are not handled will terminate the
 Node.js process with a non-zero exit code.
 LOGIN ERROR ---------------------
Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at doWrite (_stream_writable.js:406:19)
    at writeOrBuffer (_stream_writable.js:394:5)
    at Socket.Writable.write (_stream_writable.js:294:11)
    at Promise (C:\wamp64\www\home_robot\node_modules\puppeteer-recorder\index.j
s:72:12)
    at new Promise (<anonymous>)
    at write (C:\wamp64\www\home_robot\node_modules\puppeteer-recorder\index.js:
71:3)
    at module.exports.record (C:\wamp64\www\home_robot\node_modules\puppeteer-re
corder\index.js:44:11)
    at process._tickCallback (internal/process/next_tick.js:68:7)
</code>```

i've even ran npm i reinstall ffmpeg --with-libvpx
 as  it was suggested here

https://github.com/clipisode/puppeteer-recorder/issues/6

but still didnt work  .... wha telse do i need to do ?

答:
Add two empty functions called prepare and render in the options.

```<code>await record({
  browser: global_browser, // Optional: a puppeteer Browser instance,
  page, // Optional: a puppeteer Page instance,
  output: path + 'output.webm',
  fps: 60,
  frames: 60 * 5, // 5 seconds at 60 fps,
  prepare: function () {}, // <-- add this line
  render: function () {} // <-- add this line
});
</code>```

Basically it's missing some default functions and the error is not handled properly.

Body:
I'm trying to record puppeteer to see what happens when i run it on server, as I understand this package does what i want.

https://www.npmjs.com/package/puppeteer-recorder

so here is simplified version of my code

const puppeteer = require('puppeteer');
const { record } = require('puppeteer-recorder');
var path = 'C:\\wamp64\\www\\home_robot\\';

init_puppeteer();

const global_browser  ;
async function init_puppeteer() {

        global_browser = await puppeteer.launch({headless: false , args: ['--no-sandbox', '--disable-setuid-sandbox']});

    check_login()
};

async function check_login()
{
    try {
        const page = await global_browser.newPage();
        await page.setViewport({width: 1000, height: 1100});

        await record({
            browser: global_browser, // Optional: a puppeteer Browser instance,
            page: page, // Optional: a puppeteer Page instance,
            output: path + 'output.webm',
            fps: 60,
            frames: 60 * 5, // 5 seconds at 60 fps
        prepare: function () {}, // <-- add this line
        render: function () {} // <-- add this line

        });

        await page.goto('https://www.example.cob', {timeout: 60000})
            .catch(function (error) {
                throw new Error('TimeoutBrows');
            });

        await page.close();
    }
    catch (e) {
        console.log(' LOGIN ERROR ---------------------');
        console.log(e);
    }
}

But I get this error

    $ node home.js
(node:7376) UnhandledPromiseRejectionWarning: Error: spawn ffmpeg ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:7376) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejection
 id: 1)
(node:7376) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre
cated. In the future, promise rejections that are not handled will terminate the
 Node.js process with a non-zero exit code.
 LOGIN ERROR ---------------------
Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at doWrite (_stream_writable.js:406:19)
    at writeOrBuffer (_stream_writable.js:394:5)
    at Socket.Writable.write (_stream_writable.js:294:11)
    at Promise (C:\wamp64\www\home_robot\node_modules\puppeteer-recorder\index.j
s:72:12)
    at new Promise (<anonymous>)
    at write (C:\wamp64\www\home_robot\node_modules\puppeteer-recorder\index.js:
71:3)
    at module.exports.record (C:\wamp64\www\home_robot\node_modules\puppeteer-re
corder\index.js:44:11)
    at process._tickCallback (internal/process/next_tick.js:68:7)

i've even ran npm i reinstall ffmpeg --with-libvpx
 as  it was suggested here

https://github.com/clipisode/puppeteer-recorder/issues/6

but still didnt work  .... wha telse do i need to do ?

Body:
Add two empty functions called prepare and render in the options.

await record({
  browser: global_browser, // Optional: a puppeteer Browser instance,
  page, // Optional: a puppeteer Page instance,
  output: path + 'output.webm',
  fps: 60,
  frames: 60 * 5, // 5 seconds at 60 fps,
  prepare: function () {}, // <-- add this line
  render: function () {} // <-- add this line
});

Basically it's missing some default functions and the error is not handled properly.


问:
Hardware security module
Does anyone know aobut HSMs supporting C#? I googled and got 3 vendors. But there's no information given on there sites about whether they support C# and I would like to know the prices also.

The three HSM vendors:

AEP Keyper

Safenet Luna

Thales nShield

Any suggestions appreciated!

Regards,

Supriya

答: An additional vendor that is missing on your list above is Utimaco, make sure to visit http://hsm.utimaco.com - they also offer a HSM-simulator to test the integration which can be downloaded from their website after registering.</p>

ContentLicense: CC BY-SA 3.0

Title: Hardware security module


问:
How to explain the reentrant RuntimeError caused by printing in signal handlers?
Code:

```<code># callee.py
import signal
import sys
import time

def int_handler(*args):
    for i in range(10):
        print('INTERRUPT', args)
    sys.exit()

if __name__ == '__main__':

    signal.signal(signal.SIGINT, int_handler)
    signal.signal(signal.SIGTERM, int_handler)
    while 1:
        time.sleep(1)
</code>```

```<code># caller.py
import subprocess
import sys

def wait_and_communicate(p):
    out, err = p.communicate(timeout=1)
    print('========out==========')
    print(out.decode() if out else '')
    print('========err==========')
    print(err.decode() if err else '')
    print('=====================')

if __name__ == '__main__':

    p = subprocess.Popen(
        ['/usr/local/bin/python3', 'callee.py'],
        stdout=sys.stdout,
        stderr=subprocess.PIPE,
    )
    while 1:
        try:
            wait_and_communicate(p)
        except KeyboardInterrupt:
            p.terminate()
            wait_and_communicate(p)
            break
        except subprocess.TimeoutExpired:
            continue
</code>```

Simply execute caller.py and then press Ctrl+C, the program will raise RuntimeError: reentrant call inside <_io.BufferedWriter name='<stdout>'> randomly. From the documentation I learn that signal handlers are called asynchronously, and in this case two signals SIGINT(Ctrl+C action) and SIGTERM(p.terminate()) are sent nearly at the same time, causing a race condition.

However, from this post I learn that signal module doesn't execute signal handler inside low-level (C) handler. Instead, it sets a flag, and the interpreter checks the flag between bytecode instructions and then invokes the python signal handler. In other words, while signal handlers may mess up the control flow in the main thread, a bytecode instruction is always atomic.

This seems to contradict with the result of my example program. As far as I am concerned, print and the implicit _io.BufferedWriter are both implemented in pure C, and thus calling print function should consume only one bytecode instruction (CALL_FUNCTION). I am confused: within one uninterrupted instruction on one thread, how can a function be reentrant?

I'm using Python 3.6.2.

答:
You might prefer to inhibit delivery of SIGINT to the child, so there's no race,
perhaps by putting it in a different process group,
or by having it ignore the signal.
Then only SIGTERM from the parent would matter.

To reveal where it was interrupted, use this:

```<code>    sig_num, frame = args
    print(dis.dis(frame.f_code.co_code))
    print(frame.f_lasti)
</code>```

The bytecode offsets in the left margin correspond to
that last instruction executed offset.

Other items of interest include
frame.f_lineno,
frame.f_code.co_filename, and
frame.f_code.co_names.

This issue becomes moot in python 3.7.3, which no longer exhibits the symptom.

Body:
Code:

# callee.py
import signal
import sys
import time

def int_handler(*args):
    for i in range(10):
        print('INTERRUPT', args)
    sys.exit()

if __name__ == '__main__':

    signal.signal(signal.SIGINT, int_handler)
    signal.signal(signal.SIGTERM, int_handler)
    while 1:
        time.sleep(1)

# caller.py
import subprocess
import sys

def wait_and_communicate(p):
    out, err = p.communicate(timeout=1)
    print('========out==========')
    print(out.decode() if out else '')
    print('========err==========')
    print(err.decode() if err else '')
    print('=====================')

if __name__ == '__main__':

    p = subprocess.Popen(
        ['/usr/local/bin/python3', 'callee.py'],
        stdout=sys.stdout,
        stderr=subprocess.PIPE,
    )
    while 1:
        try:
            wait_and_communicate(p)
        except KeyboardInterrupt:
            p.terminate()
            wait_and_communicate(p)
            break
        except subprocess.TimeoutExpired:
            continue

Simply execute caller.py and then press Ctrl+C, the program will raise RuntimeError: reentrant call inside <_io.BufferedWriter name='<stdout>'> randomly. From the documentation I learn that signal handlers are called asynchronously, and in this case two signals SIGINT(Ctrl+C action) and SIGTERM(p.terminate()) are sent nearly at the same time, causing a race condition.

However, from this post I learn that signal module doesn't execute signal handler inside low-level (C) handler. Instead, it sets a flag, and the interpreter checks the flag between bytecode instructions and then invokes the python signal handler. In other words, while signal handlers may mess up the control flow in the main thread, a bytecode instruction is always atomic.

This seems to contradict with the result of my example program. As far as I am concerned, print and the implicit _io.BufferedWriter are both implemented in pure C, and thus calling print function should consume only one bytecode instruction (CALL_FUNCTION). I am confused: within one uninterrupted instruction on one thread, how can a function be reentrant?

I'm using Python 3.6.2.

Body:
You might prefer to inhibit delivery of SIGINT to the child, so there's no race,
perhaps by putting it in a different process group,
or by having it ignore the signal.
Then only SIGTERM from the parent would matter.

To reveal where it was interrupted, use this:

    sig_num, frame = args
    print(dis.dis(frame.f_code.co_code))
    print(frame.f_lasti)

The bytecode offsets in the left margin correspond to
that last instruction executed offset.

Other items of interest include
frame.f_lineno,
frame.f_code.co_filename, and
frame.f_code.co_names.

This issue becomes moot in python 3.7.3, which no longer exhibits the symptom.


问:
How do I get the current time as a TimeStamp in Kotlin?
This is the timestamp format I need: 2018-03-22 19:02:12.337909 </p>

答:
I'm using this

val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())

and here you have all Date and Time patterns
https://developer.android.com/reference/java/text/SimpleDateFormat


问:
Validating Folder Selection
In the portion of the code below I have a Function called FolderPick which returns the file path of a selected folder via a dialogue box.  I then strip the folder path down to find the name of the selected selected folder.  That all works as intended.

What I would like to do is verify that the name of the selected folder is one of the approved names.  I was initially thinking of using a series of IF statements, then I thought about using the case statement.  So I am pretty sure the syntax for my CASEs are wrong and that you can only have one value per case.

Is my only choice here an IF statement with a lot of ORs?

Also is there a format for a catch all CASE?  i.e. none of the previous cases were a match so do this?

```<code>Pickafolder:

FolderPath = FolderPick

Foldername = Left(FolderPath, Len(FolderPath) - 1)
Foldername = RIght(Foldername, Len(Foldername) - InStrRev(Foldername, "\"))

Select Case Foldername
    Case Foldername = "OPSS" Or Foldername = "SSP" Or Foldername = "OPSD" Or Foldername = "MTOD" Or Foldername = "SSD"

        'do something

    Case Foldername = "West" Or Foldername = "Eastern" Or Foldername = "Northeastern" Or Foldername = "Northwestern" Or Foldername = "Head Office"

        'inser "NSP\ before foldername

    Case Foldername = "NSP"

        'msgbox "Pick the  NSP sub folder"
        GoTo Pickafolder

    Case Foldername <> any other case

        'msgbox "Inappropriate directory to upload from. Please select one of the CPS download directories"
        GoTo Pickafolder

 End Select
</code>```

答:
Try this, you can use "," instead of OR</p>

```<code>Select Case FolderName
    Case "OPSS", "SSP", "Something", "MTOD", "SSD"

        'do something

    Case "West", "Eastern", "Northeastern", "Northwestern", "Head Office"

        'inser "NSP\ before foldername

    Case "NSP"

        'msgbox "Pick the  NSP sub folder"
        'GoTo Pickafolder

    Case Else

        'msgbox "Inappropriate directory to upload from. Please select one of the CPS download directories"
        'GoTo Pickafolder

 End Select
</code>```

Title: Validating Folder Selection

Body:
In the portion of the code below I have a Function called FolderPick which returns the file path of a selected folder via a dialogue box.  I then strip the folder path down to find the name of the selected selected folder.  That all works as intended.

What I would like to do is verify that the name of the selected folder is one of the approved names.  I was initially thinking of using a series of IF statements, then I thought about using the case statement.  So I am pretty sure the syntax for my CASEs are wrong and that you can only have one value per case.

Is my only choice here an IF statement with a lot of ORs?

Also is there a format for a catch all CASE?  i.e. none of the previous cases were a match so do this?

Pickafolder:

FolderPath = FolderPick

Foldername = Left(FolderPath, Len(FolderPath) - 1)
Foldername = RIght(Foldername, Len(Foldername) - InStrRev(Foldername, "\"))

Select Case Foldername
    Case Foldername = "OPSS" Or Foldername = "SSP" Or Foldername = "OPSD" Or Foldername = "MTOD" Or Foldername = "SSD"

        'do something

    Case Foldername = "West" Or Foldername = "Eastern" Or Foldername = "Northeastern" Or Foldername = "Northwestern" Or Foldername = "Head Office"

        'inser "NSP\ before foldername

    Case Foldername = "NSP"

        'msgbox "Pick the  NSP sub folder"
        GoTo Pickafolder

    Case Foldername <> any other case

        'msgbox "Inappropriate directory to upload from. Please select one of the CPS download directories"
        GoTo Pickafolder

 End Select

Body:
Try this, you can use "," instead of OR

Select Case FolderName
    Case "OPSS", "SSP", "Something", "MTOD", "SSD"

        'do something

    Case "West", "Eastern", "Northeastern", "Northwestern", "Head Office"

        'inser "NSP\ before foldername

    Case "NSP"

        'msgbox "Pick the  NSP sub folder"
        'GoTo Pickafolder

    Case Else

        'msgbox "Inappropriate directory to upload from. Please select one of the CPS download directories"
        'GoTo Pickafolder

 End Select


问:
Find the resulting Sum after adding to the previous value in a Python list
I have a random number as input, lets say 5. What I need is the sum of the numbers 5 + 55 + 555 + 5555 + 55555.

Kindly help

Body:
Input

 s=0
 for i in range(1,6):
     x="5"*i
     s+=eval(x)
 print(5 + 55 + 555 + 5555 + 55555)
 print(s)

Output

61725
61725


问:
why does having more than one thread(parallel processing) in some specific cases degrade performance?
i noticed that having more than a thread running for some code is much much slower than having one thread, and i have been really pulling my hair to know why,can anyone help?

code explanation :
i have ,sometimes, a very large array that i need to process parts of in a parallel way for optimization,each "part" of a row gets looped on and processed on in a specific thread, now i've noticed that if i only have one "part",i.e the whole array and a single worker thread that runs through it is noticeably faster than if i divide the array and process it as separate sub arrays with different threads.

```<code>    bool m_generate_row_worker(ull t_row_start,ull t_row_end)
    {
        for(;t_row_start<t_row_end;t_row_start++)
            {
                m_current_row[t_row_start]=m_singularity_checker(m_previous_row[t_row_start],m_shared_random_row[t_row_start]);

            }
        return true;
    }

    ...
    //code
    ...
    for(unsigned short thread_indx=0;thread_indx<noThreads-1;thread_indx++)
    {
        m_threads_array[thread_indx]=std::thread(
                m_generate_row_worker,this,
                thread_indx*(m_parts_per_thread),(thread_indx+1)*(m_parts_per_thread));
    }
    m_threads_array[noThreads-1]=std::thread(m_generate_row_worker,this,
            (noThreads-1)*(m_parts_per_thread),std::max((noThreads)*(m_parts_per_thread),m_blocks_per_row));
    //join
    for(unsigned short thread_indx=0;thread_indx<noThreads;thread_indx++)
    {
        m_threads_array[thread_indx].join();
    }
//EDIT
    inline ull m_singularity_checker(ull t_to_be_ckecked_with,ull
    t_to_be_ckecked)
    {
            return (t_to_be_ckecked & (t_to_be_ckecked_with<<1)
             & (t_to_be_ckecked_with>>1) ) | (t_to_be_ckecked_with &
    t_to_be_ckecked);
    }
</code>```

答:
Using threads do not always mean that you will get more work done. For example using 2 threads does not mean you will get a task done in half the time. There is an overhead to setting up the threads and depending on how many cores and OS etc... how much context switching is occurring between threads (saving the thread stack/regs and loading the next one - it all adds up). At some point adding more threads will start to slow your program down since there will be more time spent switching between threads/setting threads up/down then there is work being done. So you may be a victim of this.

If you have 100 very small items (like 1 instruction) of work to do, then 100 threads will be guaranteed to be slower since you now have ("many instructions" + 1) x 100 of work to do. Where the "many instructions" are the work of setting up the threads and clearing them up at the end - and switching between them.

So, you may want to start to profile this for yourself.. How much work is done processing each row and how many threads in total are you setting up?

One very crude, but quick/simple way to start to measure is to just take the time elapsed to processes one row in isolation (e.g. use std::chrono functions to measure the time at the start of processing one row and then take the time at the end to see total time spent. Then maybe do the same test over the entire table to get an idea how total time.

If you find that a individual row is taking very little time then you may not be getting so much benefit from the threads... You may be better of splitting the table into chunks of work that are equal to the number of cores your CPU has, then start changing the number of threads (+/-) to find the sweet spot.  Just making threads based on number of rows is a poor choice - you really want to design it to max out each core (for example).

So if you had 4 cores, maybe start by splitting the work into 4 threads to start with. Then test it with 8 if its better try 16, if its worse try 12....etc...

Also you might get different results on different PCs...

Body:
i noticed that having more than a thread running for some code is much much slower than having one thread, and i have been really pulling my hair to know why,can anyone help?

code explanation :
i have ,sometimes, a very large array that i need to process parts of in a parallel way for optimization,each "part" of a row gets looped on and processed on in a specific thread, now i've noticed that if i only have one "part",i.e the whole array and a single worker thread that runs through it is noticeably faster than if i divide the array and process it as separate sub arrays with different threads.

    bool m_generate_row_worker(ull t_row_start,ull t_row_end)
    {
        for(;t_row_start<t_row_end;t_row_start++)
            {
                m_current_row[t_row_start]=m_singularity_checker(m_previous_row[t_row_start],m_shared_random_row[t_row_start]);

            }
        return true;
    }

    ...
    //code
    ...
    for(unsigned short thread_indx=0;thread_indx<noThreads-1;thread_indx++)
    {
        m_threads_array[thread_indx]=std::thread(
                m_generate_row_worker,this,
                thread_indx*(m_parts_per_thread),(thread_indx+1)*(m_parts_per_thread));
    }
    m_threads_array[noThreads-1]=std::thread(m_generate_row_worker,this,
            (noThreads-1)*(m_parts_per_thread),std::max((noThreads)*(m_parts_per_thread),m_blocks_per_row));
    //join
    for(unsigned short thread_indx=0;thread_indx<noThreads;thread_indx++)
    {
        m_threads_array[thread_indx].join();
    }
//EDIT
    inline ull m_singularity_checker(ull t_to_be_ckecked_with,ull
    t_to_be_ckecked)
    {
            return (t_to_be_ckecked & (t_to_be_ckecked_with<<1)
             & (t_to_be_ckecked_with>>1) ) | (t_to_be_ckecked_with &
    t_to_be_ckecked);
    }


问:
Warning "Potential leak of an object allocated on line ..." when declaring a 2D array
Initially i declared a 2D array in this way:

```<code>subUrb = [[NSArray alloc] initWithObjects:
     [[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"East",@"South", @"West", @"North", nil] ,
     [[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"Kuala Lumpur SubUrb1", @"Kuala Lumpur SubUrb2", nil],
     [[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"Jakarta SubUrb1",nil],
     nil];
</code>```

But when i try to 'analyze' the warnings of the project, i got three same type of issues in this chunk of code - "potential leak of an object allocated on line xxx"

I noticed that to get rid of it, i have to write sth like this:

```<code>subUrb =
    [[NSArray alloc] initWithObjects:
        [[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"East",@"South", @"West", @"North", nil] autorelease],
        [[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"Kuala Lumpur SubUrb1", @"Kuala Lumpur SubUrb2", nil] autorelease],
        [[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"Jakarta SubUrb1",nil] autorelease],
        nil];
</code>```

Then i will not get any analyze warnings. But i dun like this...it is not logical. This 2D array should never be released in my controller actually, the entire 2D array should be retained for the entire life time of the controller for a PickerView.

How should I declare the 2D array more elegantly?

答:
1: NARC

If you send +alloc, you own that object and it is your responsibility to release it. Hence

```<code>[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"East",@"South", @"West", @"North", nil]
</code>```

creates an array that’s owned by you, hence you should release it.

2: Cocoa collections own their elements

When you add an object to a Cocoa collection such as NSArray, the collection owns that object. It will also release all elements in the collection when they’re not used any longer.

3: Conclusion

Your code without -autorelease gives you ownership of both the outermost array and each inner array that’s an element of the outermost array. This means that you’re responsible for releasing those four arrays (one outermost, three innermost). However, since the outermost array already owns the innermost arrays, you probably don’t need to own them as well, hence -autorelease.

Body:
Initially i declared a 2D array in this way:

subUrb = [[NSArray alloc] initWithObjects:
     [[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"East",@"South", @"West", @"North", nil] ,
     [[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"Kuala Lumpur SubUrb1", @"Kuala Lumpur SubUrb2", nil],
     [[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"Jakarta SubUrb1",nil],
     nil];

But when i try to 'analyze' the warnings of the project, i got three same type of issues in this chunk of code - "potential leak of an object allocated on line xxx"

I noticed that to get rid of it, i have to write sth like this:

subUrb =
    [[NSArray alloc] initWithObjects:
        [[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"East",@"South", @"West", @"North", nil] autorelease],
        [[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"Kuala Lumpur SubUrb1", @"Kuala Lumpur SubUrb2", nil] autorelease],
        [[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"Jakarta SubUrb1",nil] autorelease],
        nil];

Then i will not get any analyze warnings. But i dun like this...it is not logical. This 2D array should never be released in my controller actually, the entire 2D array should be retained for the entire life time of the controller for a PickerView.

How should I declare the 2D array more elegantly?

Body:
1: NARC

If you send +alloc, you own that object and it is your responsibility to release it. Hence

[[NSArray alloc] initWithObjects:ALL_SUBURBS_LABEL, @"East",@"South", @"West", @"North", nil]

creates an array that’s owned by you, hence you should release it.

2: Cocoa collections own their elements

When you add an object to a Cocoa collection such as NSArray, the collection owns that object. It will also release all elements in the collection when they’re not used any longer.

3: Conclusion

Your code without -autorelease gives you ownership of both the outermost array and each inner array that’s an element of the outermost array. This means that you’re responsible for releasing those four arrays (one outermost, three innermost). However, since the outermost array already owns the innermost arrays, you probably don’t need to own them as well, hence -autorelease.


答:
You can try out the following code.

fetch("https://www.reddit.com/r/funny.json")
.then(resp => resp.json())
.then(res => ({
  posts: res.data.children.map(el => ({
    title: el.data.title,
    upvote: el.data.upvote,
    score: el.data.score,
    comments: el.data.num_comments,
    created: el.data.created
  }))
}))
.then(posts => {
  console.log(posts);
});```

Title: fetching data and adding title to Json object

Body:
I would like to add title to my JSON object, the structure I wish to achieve is:

{
"posts": [
    {
        "title": "put title here",
        "upvotes": 1234,
        "score": 1000,
        "num_comments": 100,
        "created": "16.05.2019 12:12",
    },
]
}

I was able to fetch data and put it into array of 26 elements, everything is fine but I wish to somehow add this "posts:" to be above whole rest, here is my code:

fetch("https://www.reddit.com/r/funny.json")
.then(resp => resp.json()
.then(async res => {
   let posts = await res.data.children.map(el => {
    let title = el.data.title;
    let upvote = el.data.ups;
    let score = el.data.score;
    let comments = el.data.num_comments;
    let created = el.data.created;
    const allPosts = {title, upvote, score, comments, created}
    postList.push(allPosts)
    return postList
})
console.log(posts);

    return posts
})


问:
Is OpenGL function loader needed?
On desktop OSes, OpenGL function loaders like GLEW, GLAD, etc. are used to load functions at runtime. But what about on Android? How are functions loaded? I've looked at a few apps and all of them seem to depend on EGL and GLES. But AFAIK EGL isn't a loading library, but an interface. Well, an interface to an interface as GLES is actually an interface.

This leads to another question: How come Android uses EGL when it is generally not used on desktops?

答:
How come Android uses EGL when it is generally not used on desktops?

It was widely used on embedded electronics prior to Android, so I suspect it was path of least resistance for a new smartphone OS as all GPU vendors had an EGL implementation already.

Title: Is OpenGL function loader needed?


问:
convert a list of dictionaries to a dictionary with dict comprehension
Original:

```<code># abs is not a string, it's a function name
[{'name': 'abs'}, {'op': abs}]
</code>```

to

```<code>{'name': 'abs', 'op': abs}
</code>```

My code:

```<code>op = [{'name': 'abs'}, {'op': abs}] # key value is unique
new_dict = {}
for item in op:
    new_dict.update(item)
</code>```

Is there a way to do the same with dict comprehension?

答:
Using dict

Ex:

```<code>data = [{'name': 'abs'}, {'op': abs}]
print(dict(tuple(*i.items()) for i in data))
</code>```

Output:

```<code>{'name': 'abs', 'op': <built-in function abs>}
</code>```

Body:
Original:

# abs is not a string, it's a function name
[{'name': 'abs'}, {'op': abs}]

to

{'name': 'abs', 'op': abs}

My code:

op = [{'name': 'abs'}, {'op': abs}] # key value is unique
new_dict = {}
for item in op:
    new_dict.update(item)

Is there a way to do the same with dict comprehension?

Body:
Using dict

Ex:

data = [{'name': 'abs'}, {'op': abs}]
print(dict(tuple(*i.items()) for i in data))

Output:

{'name': 'abs', 'op': <built-in function abs>}


问:
Using class variable in @compnent animation Angular 7
I have an animation that is expanding the height on hover. I am taking the height of the label in ngOnInit because that label is auto height and doesn't animate properly with just :hover this is my ts @Component with the animation:

```<code>@Component({
  selector: 'app-create',
  templateUrl: './create.component.html',
  styleUrls: ['./create.component.css'],
  animations: [
    trigger('buttonState', [
      state('inactive', style({
        height: '100%'
       })),
      state('active', style({
        height: this.buttonHeight
       })),
      transition('inactive => active', animate('1.5s ease-in')),
      transition('active => inactive', animate('1.5s ease-out'))
    ])
  ]
})
</code>```

The problem is that the this.buttonHeight gives me Cannot read property 'buttonHeight' of undefined. I guess that 'this' is not the class in the @Compnent or is not defined yet. How can I make it work?

This is how i take the height:

```<code>  @ViewChild('button') button : ElementRef;
  buttonHeight : number;

  ngOnInit() {
    this.buttonHeight = this.button.nativeElement.offsetHeight
  }
</code>```

In html:

```<code><label [@buttonState]="state" (mouseover)="toggleState()" (mouseout)="toggleState()">
</code>```

答:
Wasn't able to get the animation to work but you can't use this.buttonHeight in animation.
You have to add a parameter:

HTML

```<code><label [@buttonState]="{value: state, params: {buttonHeight: buttonHeight}}" (mouseover)="toggleState()"
    (mouseout)="toggleState()">LABEL</label>
</code>```

TS

```<code>animations: [
    trigger('buttonState', [
      state('inactive', style({
        height: '100%'
      })),
      state('active', style({
        height: "{{buttonHeight}}px"
      }), { params: { buttonHeight: "4" } }),
      transition('inactive => active', animate('1.5s ease-in')),
      transition('active => inactive', animate('1.5s ease-out'))
    ])
  ]
</code>```

You have to add a default value for each param you define (else you get an error).

I hope I could lead you in the right direction (:

Body:
I have an animation that is expanding the height on hover. I am taking the height of the label in ngOnInit because that label is auto height and doesn't animate properly with just :hover this is my ts @Component with the animation:

@Component({
  selector: 'app-create',
  templateUrl: './create.component.html',
  styleUrls: ['./create.component.css'],
  animations: [
    trigger('buttonState', [
      state('inactive', style({
        height: '100%'
       })),
      state('active', style({
        height: this.buttonHeight
       })),
      transition('inactive => active', animate('1.5s ease-in')),
      transition('active => inactive', animate('1.5s ease-out'))
    ])
  ]
})

The problem is that the this.buttonHeight gives me Cannot read property 'buttonHeight' of undefined. I guess that 'this' is not the class in the @Compnent or is not defined yet. How can I make it work?

This is how i take the height:

  @ViewChild('button') button : ElementRef;
  buttonHeight : number;

  ngOnInit() {
    this.buttonHeight = this.button.nativeElement.offsetHeight
  }

In html:

Body:
Wasn't able to get the animation to work but you can't use this.buttonHeight in animation.
You have to add a parameter:

HTML

<label [@buttonState]="{value: state, params: {buttonHeight: buttonHeight}}" (mouseover)="toggleState()"
    (mouseout)="toggleState()">LABEL</label>

TS

animations: [
    trigger('buttonState', [
      state('inactive', style({
        height: '100%'
      })),
      state('active', style({
        height: "{{buttonHeight}}px"
      }), { params: { buttonHeight: "4" } }),
      transition('inactive => active', animate('1.5s ease-in')),
      transition('active => inactive', animate('1.5s ease-out'))
    ])
  ]

You have to add a default value for each param you define (else you get an error).

I hope I could lead you in the right direction (:


问:
How can I get files within the tests in Python?
I have the following package structure:

```<code>.
├── my_app
│   ├── app.py
│   ├── cli.py
│   ├── db.py
│   └── __init__.py
├── setup.py
├── tests
│   ├── data
│   │   └── foobar.gz
│   ├── test_app.py
│   └── test_database.py
└── tox.ini
</code>```

Within test_app.py, I do this:

```<code>import pkg_resources
path = '../tests/data/foobar.gz'
full_path = pkg_resources.resource_filename(__name__, path)
</code>```

Now I've seen the message:

```<code>/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py:1145:
  DeprecationWarning: Use of .. or absolute path in a resource path
  is not allowed and will raise exceptions in a future release.
self, resource_name
</code>```

What should I do instead?

(I could also change the structure of the package, but I would like to keep the test data within the tests - it is not necessary for running the application)

答:
To get the test resource when passing the test filename to pkg_resources you want to use the path from the file with the tests like:

```<code>import pkg_resources
path = 'data/foobar.gz'
full_path = pkg_resources.resource_filename(__name__, path)
</code>```

With pytest, I also use this pattern in conftest.py:

```<code>@pytest.fixture('session')
def test_data_dir():
    return os.path.join(os.path.dirname(__file__), 'data')
</code>```

I can the use os.path.join() to easily get to any test data.

Body:
I have the following package structure:

.
├── my_app
│   ├── app.py
│   ├── cli.py
│   ├── db.py
│   └── __init__.py
├── setup.py
├── tests
│   ├── data
│   │   └── foobar.gz
│   ├── test_app.py
│   └── test_database.py
└── tox.ini

Within test_app.py, I do this:

import pkg_resources
path = '../tests/data/foobar.gz'
full_path = pkg_resources.resource_filename(__name__, path)

Now I've seen the message:

/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py:1145:
  DeprecationWarning: Use of .. or absolute path in a resource path
  is not allowed and will raise exceptions in a future release.
self, resource_name

What should I do instead?

(I could also change the structure of the package, but I would like to keep the test data within the tests - it is not necessary for running the application)

Body:
To get the test resource when passing the test filename to pkg_resources you want to use the path from the file with the tests like:

import pkg_resources
path = 'data/foobar.gz'
full_path = pkg_resources.resource_filename(__name__, path)

With pytest, I also use this pattern in conftest.py:

@pytest.fixture('session')
def test_data_dir():
    return os.path.join(os.path.dirname(__file__), 'data')

I can the use os.path.join() to easily get to any test data.


问:
fetching data and adding title to Json object
I would like to add title to my JSON object, the structure I wish to achieve is:

```<code>{
"posts": [
    {
        "title": "put title here",
        "upvotes": 1234,
        "score": 1000,
        "num_comments": 100,
        "created": "16.05.2019 12:12",
    },
]
}
</code>```

I was able to fetch data and put it into array of 26 elements, everything is fine but I wish to somehow add this "posts:" to be above whole rest, here is my code:

```<code>fetch("https://www.reddit.com/r/funny.json")
.then(resp => resp.json()
.then(async res => {
   let posts = await res.data.children.map(el => {
    let title = el.data.title;
    let upvote = el.data.ups;
    let score = el.data.score;
    let comments = el.data.num_comments;
    let created = el.data.created;
    const allPosts = {title, upvote, score, comments, created}
    postList.push(allPosts)
    return postList
})
console.log(posts);

    return posts
})
</code>```

答:
You can do it in this way:

```<code>fetch("https://www.reddit.com/r/funny.json")
    .then(resp => resp.json()
        .then(async res => {
            let posts = await res.data.children.map(el => {
                return {
                    title: el.data.title,
                    upvote: el.data.ups,
                    score: el.data.score,
                    comments: el.data.num_comments,
                    created: el.data.created
                }
            })
            const postObject = { posts }
            console.log(postObject);
            return postObject
        })
</code>```

Map function return value, in this way you get an object with key (posts) and values (an object with details).

Body:
You can do it in this way:

fetch("https://www.reddit.com/r/funny.json")
    .then(resp => resp.json()
        .then(async res => {
            let posts = await res.data.children.map(el => {
                return {
                    title: el.data.title,
                    upvote: el.data.ups,
                    score: el.data.score,
                    comments: el.data.num_comments,
                    created: el.data.created
                }
            })
            const postObject = { posts }
            console.log(postObject);
            return postObject
        })

Map function return value, in this way you get an object with key (posts) and values (an object with details).