问:
An error occured in MPI_Recv while sending an array
```<code>#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
int main(int argc, char **argv)
{
    int N;
    scanf("%d", &N);
    double *a = (double *)malloc(N * sizeof(double));
    int i, rank, size, tag = 99, tag1 = 100;
    MPI_Status status;
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    if (rank == 0)
    {
        for(int j=0;j<N;++j)
        {
            a[j] = j+0.1;
        }
        for (i = 1; i < size; i++)
        {
            MPI_Send(&N, 1, MPI_INT, i, tag1, MPI_COMM_WORLD);
            MPI_Send(a, N, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
        }
    }
    else
    {
        MPI_Recv(&N, 1, MPI_INT, 0, tag1, MPI_COMM_WORLD, &status);
        MPI_Recv(a, N, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
        // for(int j=0;j<N*2;++j)
            // printf("%d %f\n", rank, a[j]);
    }
    MPI_Barrier(MPI_COMM_WORLD);
    printf("Message from process %d : %f\n", rank, a[rank]);
    MPI_Finalize();
    return 0;
}
</code>```

I'm creating the array 'a' in 0th process and sending it to remaining proccesses. But I'm getting the following error upon doing this.

```<code>[nikhil:8599] *** An error occurred in MPI_Recv
[nikhil:8599] *** reported by process [4228579329,1]
[nikhil:8599] *** on communicator MPI_COMM_WORLD
[nikhil:8599] *** MPI_ERR_BUFFER: invalid buffer pointer
[nikhil:8599] *** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
[nikhil:8599] ***    and potentially your MPI job)
[nikhil:08593] 2 more processes have sent help message help-mpi-errors.txt / mpi_errors_are_fatal
[nikhil:08593] Set MCA parameter "orte_base_help_aggregate" to 0 to see all help / error messages

</code>```

Can anybody explain why I'm getting this error?

As you can see in the code there's a for loop containing a print statement which is commented. The weird thing is... upon uncommenting that loop. It's working fine.

答:
Thoughts:

MPI_Init should be the first thing in your program.

Only one rank should scanf.

N is not communicated across ranks, so you are allocating memory of undefined size.

Define variables as close to their point of usage as possible. Putting int i at the top of your function is a disaster waiting to happen.

The barrier at the end is unnecessary.

All the ranks need to allocate their own memory.

That gets us to this code:

#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

int main(int argc, char **argv){
    MPI_Init(&argc, &argv);

    const int tag = 99;
    const int tag1 = 100;

    int rank, size;
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    double *a; //Pointer to the memory we will allocate
    int N;

    if (rank == 0){
        scanf("%d", &N);

        a = (double *)malloc(N * sizeof(double));

        for(int j=0;j<N;++j){
            a[j] = j+0.1;
        }
        for (int i = 1; i < size; i++){
            MPI_Send(&N, 1, MPI_INT, i, tag1, MPI_COMM_WORLD);
            MPI_Send(a, N, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
        }
    } else {
        MPI_Status status;
        MPI_Recv(&N, 1, MPI_INT, 0, tag1, MPI_COMM_WORLD, &status);
        //Have to allocate memory on all ranks
        a = (double *)malloc(N * sizeof(double));
        MPI_Recv(a, N, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
        // for(int j=0;j<N*2;++j)
            // printf("%d %f\n", rank, a[j]);
    }

    printf("Message from process %d : %f\n", rank, a[rank]);

    MPI_Finalize();
    return 0;
}
```

<h1>Doing it better</h1>

<p>The broadcast command is your friend here:</p>

<pre class="lang-c prettyprint-override"><code>#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

#define MPI_Error_Check(x) {const int err=x; if(x!=MPI_SUCCESS) { fprintf(stderr, "MPI ERROR %d at %d.", err, __LINE__);}}

int main(int argc, char **argv){
    MPI_Init(&argc, &argv);

    int rank, size;
    MPI_Error_Check(MPI_Comm_rank(MPI_COMM_WORLD, &rank));
    MPI_Error_Check(MPI_Comm_size(MPI_COMM_WORLD, &size));

    int N;
    if (rank==0){
        scanf("%d", &N);
    }

    MPI_Error_Check(MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD));

    double *a = (double *)malloc(N * sizeof(double));

    if(rank==0){
        for(int j=0;j<N;++j){
            a[j] = j+0.1;
        }
    }

    printf("Message from process %d : N=%d\n", rank, N);

    MPI_Error_Check(MPI_Bcast(a, N, MPI_DOUBLE, 0, MPI_COMM_WORLD));

    fprintf(stderr, "Message from process %d : %f\n", rank, a[rank]);

    free(a);

    MPI_Finalize();
    return 0;
}
</code>```

Doing It Even Better

The fastest form of communication is no communication at all. In your case, once the value N is known each rank can recreate the data on its own:

#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

#define MPI_Error_Check(x) {const int err=x; if(x!=MPI_SUCCESS) { fprintf(stderr, "MPI ERROR %d at %d.", err, __LINE__);}}

int main(int argc, char **argv){
    MPI_Init(&argc, &argv);

    int rank, size;
    MPI_Error_Check(MPI_Comm_rank(MPI_COMM_WORLD, &rank));
    MPI_Error_Check(MPI_Comm_size(MPI_COMM_WORLD, &size));

    int N;
    if (rank==0){
        scanf("%d", &N);
    }

    MPI_Error_Check(MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD));

    double *a = (double *)malloc(N * sizeof(double));

    for(int j=0;j<N;++j){
        a[j] = j+0.1;
    }

    printf("Message from process %d : N=%d\n", rank, N);

    fprintf(stderr, "Message from process %d : %f\n", rank, a[rank]);

    free(a);

    MPI_Finalize();
    return 0;
}
```

Body:
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
int main(int argc, char **argv)
{
    int N;
    scanf("%d", &N);
    double *a = (double *)malloc(N * sizeof(double));
    int i, rank, size, tag = 99, tag1 = 100;
    MPI_Status status;
    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    if (rank == 0)
    {
        for(int j=0;j<N;++j)
        {
            a[j] = j+0.1;
        }
        for (i = 1; i < size; i++)
        {
            MPI_Send(&N, 1, MPI_INT, i, tag1, MPI_COMM_WORLD);
            MPI_Send(a, N, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
        }
    }
    else
    {
        MPI_Recv(&N, 1, MPI_INT, 0, tag1, MPI_COMM_WORLD, &status);
        MPI_Recv(a, N, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
        // for(int j=0;j<N*2;++j)
            // printf("%d %f\n", rank, a[j]);
    }
    MPI_Barrier(MPI_COMM_WORLD);
    printf("Message from process %d : %f\n", rank, a[rank]);
    MPI_Finalize();
    return 0;
}

I'm creating the array 'a' in 0th process and sending it to remaining proccesses. But I'm getting the following error upon doing this.

[nikhil:8599] *** An error occurred in MPI_Recv
[nikhil:8599] *** reported by process [4228579329,1]
[nikhil:8599] *** on communicator MPI_COMM_WORLD
[nikhil:8599] *** MPI_ERR_BUFFER: invalid buffer pointer
[nikhil:8599] *** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
[nikhil:8599] ***    and potentially your MPI job)
[nikhil:08593] 2 more processes have sent help message help-mpi-errors.txt / mpi_errors_are_fatal
[nikhil:08593] Set MCA parameter "orte_base_help_aggregate" to 0 to see all help / error messages

Can anybody explain why I'm getting this error?

As you can see in the code there's a for loop containing a print statement which is commented. The weird thing is... upon uncommenting that loop. It's working fine.

Body:
Thoughts:

MPI_Init should be the first thing in your program.

Only one rank should scanf.

N is not communicated across ranks, so you are allocating memory of undefined size.

Define variables as close to their point of usage as possible. Putting int i at the top of your function is a disaster waiting to happen.

The barrier at the end is unnecessary.

All the ranks need to allocate their own memory.

That gets us to this code:

#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

int main(int argc, char **argv){
    MPI_Init(&argc, &argv);

    const int tag = 99;
    const int tag1 = 100;

    int rank, size;
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    double *a; //Pointer to the memory we will allocate
    int N;

    if (rank == 0){
        scanf("%d", &N);

        a = (double *)malloc(N * sizeof(double));

        for(int j=0;j<N;++j){
            a[j] = j+0.1;
        }
        for (int i = 1; i < size; i++){
            MPI_Send(&N, 1, MPI_INT, i, tag1, MPI_COMM_WORLD);
            MPI_Send(a, N, MPI_DOUBLE, i, tag, MPI_COMM_WORLD);
        }
    } else {
        MPI_Status status;
        MPI_Recv(&N, 1, MPI_INT, 0, tag1, MPI_COMM_WORLD, &status);
        //Have to allocate memory on all ranks
        a = (double *)malloc(N * sizeof(double));
        MPI_Recv(a, N, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &status);
        // for(int j=0;j<N*2;++j)
            // printf("%d %f\n", rank, a[j]);
    }

    printf("Message from process %d : %f\n", rank, a[rank]);

    MPI_Finalize();
    return 0;
}

Doing it better

The broadcast command is your friend here:

#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

#define MPI_Error_Check(x) {const int err=x; if(x!=MPI_SUCCESS) { fprintf(stderr, "MPI ERROR %d at %d.", err, __LINE__);}}

int main(int argc, char **argv){
    MPI_Init(&argc, &argv);

    int rank, size;
    MPI_Error_Check(MPI_Comm_rank(MPI_COMM_WORLD, &rank));
    MPI_Error_Check(MPI_Comm_size(MPI_COMM_WORLD, &size));

    int N;
    if (rank==0){
        scanf("%d", &N);
    }

    MPI_Error_Check(MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD));

    double *a = (double *)malloc(N * sizeof(double));

    if(rank==0){
        for(int j=0;j<N;++j){
            a[j] = j+0.1;
        }
    }

    printf("Message from process %d : N=%d\n", rank, N);

    MPI_Error_Check(MPI_Bcast(a, N, MPI_DOUBLE, 0, MPI_COMM_WORLD));

    fprintf(stderr, "Message from process %d : %f\n", rank, a[rank]);

    free(a);

    MPI_Finalize();
    return 0;
}

Doing It Even Better

The fastest form of communication is no communication at all. In your case, once the value N is known each rank can recreate the data on its own:

#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

#define MPI_Error_Check(x) {const int err=x; if(x!=MPI_SUCCESS) { fprintf(stderr, "MPI ERROR %d at %d.", err, __LINE__);}}

int main(int argc, char **argv){
    MPI_Init(&argc, &argv);

    int rank, size;
    MPI_Error_Check(MPI_Comm_rank(MPI_COMM_WORLD, &rank));
    MPI_Error_Check(MPI_Comm_size(MPI_COMM_WORLD, &size));

    int N;
    if (rank==0){
        scanf("%d", &N);
    }

    MPI_Error_Check(MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD));

    double *a = (double *)malloc(N * sizeof(double));

    for(int j=0;j<N;++j){
        a[j] = j+0.1;
    }

    printf("Message from process %d : N=%d\n", rank, N);

    fprintf(stderr, "Message from process %d : %f\n", rank, a[rank]);

    free(a);

    MPI_Finalize();
    return 0;
}


来源: stackexchange

问:
Define field type with add_fields processor in filebeat.yml?
Looking at this documentation on adding fields, I see that filebeat can add any custom field by name and value that will be appended to every documented pushed to Elasticsearch by Filebeat.

This is defined in filebeat.yml:

```<code>processors:
- add_fields:
    target: project
    fields:
      name: myproject
      id: '574734885120952459'
</code>```

Is there a way, strictly from filebeat.yml, to give fields added here a "type" as well? For example, can I assign "name" to type "keyword" ?

答:
The only way I've seen to accomplish does not involve filebeat.yml, but creating a custom fields.yml file in the filebeat directory (this all applies for any beat) with the field and type specified under the "beat" key.

Example, if field "id" above was declared in filebeat.yml, and we wanted it to be a custom field of type "keyword," we would do the following:

copy fields.yml to my_filebeat_fields.yml file in the filebeat directory.
In my_filebeat_fields, add in this section:

```<code>- key: beat
  anchor: beat-common
  title: Beat
  description: >
    Contains common beat fields available in all event types.
  fields:
    # your customization begins here:
    - name: id
    - type: keyword
</code>```

Then do the following to use this new custom fields file:

Modify this portion of filebeat.yml to include:

```<code>#==================== Elasticsearch template setting ==========================

setup.template.name: "filebeat-*"
setup.template.fields: "my_filebeat_fields.yml"
setup.template.overwrite: true
</code>```

then load the template into elastic in whichever method is appropriate for your environment, following this guide.

(Assuming version 7.x for everything)

EDIT:

Apparently setting "setup.template.append_fields" option in the filebeat.yml file could also work, but I have not explored that.

https://www.elastic.co/guide/en/beats/filebeat/current/configuration-template.html

ContentLicense: CC BY-SA 4.0

Body:
Looking at this documentation on adding fields, I see that filebeat can add any custom field by name and value that will be appended to every documented pushed to Elasticsearch by Filebeat.

This is defined in filebeat.yml:

processors:
- add_fields:
    target: project
    fields:
      name: myproject
      id: '574734885120952459'

Is there a way, strictly from filebeat.yml, to give fields added here a "type" as well? For example, can I assign "name" to type "keyword" ?

Body:
The only way I've seen to accomplish does not involve filebeat.yml, but creating a custom fields.yml file in the filebeat directory (this all applies for any beat) with the field and type specified under the "beat" key.

Example, if field "id" above was declared in filebeat.yml, and we wanted it to be a custom field of type "keyword," we would do the following:

copy fields.yml to my_filebeat_fields.yml file in the filebeat directory.
In my_filebeat_fields, add in this section:

- key: beat
  anchor: beat-common
  title: Beat
  description: >
    Contains common beat fields available in all event types.
  fields:
    # your customization begins here:
    - name: id
    - type: keyword

Then do the following to use this new custom fields file:

Modify this portion of filebeat.yml to include:

#==================== Elasticsearch template setting ==========================

setup.template.name: "filebeat-*"
setup.template.fields: "my_filebeat_fields.yml"
setup.template.overwrite: true

then load the template into elastic in whichever method is appropriate for your environment, following this guide.

(Assuming version 7.x for everything)

EDIT:

Apparently setting "setup.template.append_fields" option in the filebeat.yml file could also work, but I have not explored that.

https://www.elastic.co/guide/en/beats/filebeat/current/configuration-template.html


问:
How to check if string contains chars which are not in a list?
I have a question. How do I check if a python string contains chars which are not in a given list?

Here is the list (set):

set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")
```

答:
Comparing the runtime of the different solutions:

```<code>import timeit

search_strings = [
    '"#12"',                     # short string, early match
    '"#1234567"',                # longer string, early match
    '"1234567#"',                # longer string, late match
    '"123" * 100 + "#"',         # long string, late match
    '"123#" * 100',              # long string early match
]

algorithms = [
    ("r.search(s)", 's={};import re; r = re.compile(r"[^-.\w]")'),
    ("set(s) - SET", 's={};SET=frozenset("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")'),
    ("any(x not in SET for x in s)", 's={};SET=frozenset("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")'),
    ("SET.issuperset(s)", 's={};SET=frozenset("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")'),
]

for alg, setup in algorithms:
    print alg
    for sstr in search_strings:
        print "%35s %.3f" % (sstr[:35], timeit.timeit(alg, setup.format(sstr)))
</code>```

which gives the following output on my machine:

```<code>r.search(s)
                              "#12" 0.470
                         "#1234567" 0.514
                         "1234567#" 0.572
                  "123" * 100 + "#" 3.493
                       "123#" * 100 0.502
set(s) - SET
                              "#12" 0.566
                         "#1234567" 1.045
                         "1234567#" 1.075
                  "123" * 100 + "#" 7.658
                       "123#" * 100 10.170
any(x not in SET for x in s)
                              "#12" 0.786
                         "#1234567" 0.797
                         "1234567#" 1.475
                  "123" * 100 + "#" 27.266
                       "123#" * 100 1.087
SET.issuperset(s)
                              "#12" 0.466
                         "#1234567" 0.864
                         "1234567#" 0.896
                  "123" * 100 + "#" 7.512
                       "123#" * 100 10.199
</code>```

we see that the regex solution is the fastest.

Body:
Comparing the runtime of the different solutions:

import timeit

search_strings = [
    '"#12"',                     # short string, early match
    '"#1234567"',                # longer string, early match
    '"1234567#"',                # longer string, late match
    '"123" * 100 + "#"',         # long string, late match
    '"123#" * 100',              # long string early match
]

algorithms = [
    ("r.search(s)", 's={};import re; r = re.compile(r"[^-.\w]")'),
    ("set(s) - SET", 's={};SET=frozenset("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")'),
    ("any(x not in SET for x in s)", 's={};SET=frozenset("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")'),
    ("SET.issuperset(s)", 's={};SET=frozenset("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._")'),
]

for alg, setup in algorithms:
    print alg
    for sstr in search_strings:
        print "%35s %.3f" % (sstr[:35], timeit.timeit(alg, setup.format(sstr)))

which gives the following output on my machine:

r.search(s)
                              "#12" 0.470
                         "#1234567" 0.514
                         "1234567#" 0.572
                  "123" * 100 + "#" 3.493
                       "123#" * 100 0.502
set(s) - SET
                              "#12" 0.566
                         "#1234567" 1.045
                         "1234567#" 1.075
                  "123" * 100 + "#" 7.658
                       "123#" * 100 10.170
any(x not in SET for x in s)
                              "#12" 0.786
                         "#1234567" 0.797
                         "1234567#" 1.475
                  "123" * 100 + "#" 27.266
                       "123#" * 100 1.087
SET.issuperset(s)
                              "#12" 0.466
                         "#1234567" 0.864
                         "1234567#" 0.896
                  "123" * 100 + "#" 7.512
                       "123#" * 100 10.199

we see that the regex solution is the fastest.


问:
Need to search for names, but have no Freetext index
I have a need to search names in our table, but we don't have a FREETEXT index setup.  This isn't an option anymore due to the large amount of data in the table.

Is there any alternatives that I can do?  Essentially I'd be looking at doing:

```<code>SELECT *
  FROM MyTable
 WHERE FREETEXT(FirstName, @firstname)
</code>```

Any ideas or pointers would be greatly appreciated.

答:
Without full text indexes, then you'd maybe consider using

```<code>WHERE FirstName LIKE '%' + @firstname+ '%'
</code>```

However it doesn't scale well (linearly, scan of all rows).

And you have a "large amount of data" which means you really should have full text indexes

ContentLicense: CC BY-SA 3.0

Body:
I have a need to search names in our table, but we don't have a FREETEXT index setup.  This isn't an option anymore due to the large amount of data in the table.

Is there any alternatives that I can do?  Essentially I'd be looking at doing:

SELECT *
  FROM MyTable
 WHERE FREETEXT(FirstName, @firstname)

Any ideas or pointers would be greatly appreciated.

Body:
Without full text indexes, then you'd maybe consider using

WHERE FirstName LIKE '%' + @firstname+ '%'

However it doesn't scale well (linearly, scan of all rows).

And you have a "large amount of data" which means you really should have full text indexes


问:
DOMContentLoaded not firing after navigation but fires on page reload when using javascript_pack_tag
I'm working with a Rails 6 rc1 project with React and Webpacker.

I'm currently using javascript_pack_tag to render React components in my Rails app. I'm trying not to use a gem like react-rails just to keep things simple.

Today I found a weird behavior where my React component would not render after the page is navigated to from a link. However, on a page reload, my component is rendered.

Gemfile:

```<code>gem 'rails', '~> 6.0.0.rc1'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.11'
gem 'sass-rails', '~> 5'
gem 'webpacker', '~> 4.0'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'bootsnap', '>= 1.4.2', require: false
gem 'devise', '~>4.6'

group :development, :test do
  gem 'pry-byebug'
  gem 'rspec-rails', '~> 3.8'
end

group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end
</code>```

app/views/layouts/application.html.erb:

```<code><html>
  <head>
    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
....
</code>```

app/javascript/packs/application.js:

```<code>require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")

import "bootstrap"
import "../stylesheets/application"
</code>```

app/views/posts/new.html.erb:

```<code><%= content_tag :div,
  id: 'post_data',
  data: @post do %>
<% end %>
<div id="render_here"></div>
<%= javascript_pack_tag 'new_post' %>

</code>```

app/javascript/packs/new_post.jsx: (though the React code in this file isn't really relevant for the question)

```<code>import React from 'react';
import ReactDOM from 'react-dom';
import PostForm from '../components/PostForm';

document.addEventListener('DOMContentLoaded', () => {
  const node = document.getElementById('post_data');
  const data = JSON.parse(node.getAttribute('data'));
  const dom = document.querySelector('#render_here');

  ReactDOM.render(
    <PostForm post={data} />,
    dom);
});
</code>```

With this setup, the component is not rendered after clicking on a link that navigates to the page. On page reload, the component is rendered.

I tried a number of things.

In new_post.jsx, after commenting everything out and leaving only console.log(document.readyState), the console displays complete twice. On page load, it displays loading.

Then I tried to use jQuery's document.ready in new_post.jsx:

```<code>$(document).ready(function() {
  if (pageInitialized) {
    return;
  }
  pageInitialized = true;

  console.log('render');
...
</code>```

And again, render is displayed twice after the page is navigated to. On page reload, render is displayed once.

What is going on?

答:
I know this was resolved for you by a work around, but I came across this question as the first result in my search for the same issue, so for future reference for others having the same problem, I want to leave an explanation.

As described here Rails 5: how to use $(document).ready() with turbo-links a better way would be to listen for the turbolinks events.

document.addEventListener("turbolinks:load", ()=> {
   alert("loaded");
}
```

<p>or if you need jQuery</p>

<pre class="lang-js prettyprint-override"><code>$( document ).on("turbolinks:load", ()=> {
    alert("loaded");
}
</code>```

CoffeScript variant:

document.addEventListener "turbolinks:load",  ->
    alert "loaded"
```

Body:
I'm working with a Rails 6 rc1 project with React and Webpacker.

I'm currently using javascript_pack_tag to render React components in my Rails app. I'm trying not to use a gem like react-rails just to keep things simple.

Today I found a weird behavior where my React component would not render after the page is navigated to from a link. However, on a page reload, my component is rendered.

Gemfile:

gem 'rails', '~> 6.0.0.rc1'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.11'
gem 'sass-rails', '~> 5'
gem 'webpacker', '~> 4.0'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'bootsnap', '>= 1.4.2', require: false
gem 'devise', '~>4.6'

group :development, :test do
  gem 'pry-byebug'
  gem 'rspec-rails', '~> 3.8'
end

group :development do
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

app/views/layouts/application.html.erb:

    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
....

app/javascript/packs/application.js:

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")

import "bootstrap"
import "../stylesheets/application"

app/views/posts/new.html.erb:

<%= content_tag :div,
  id: 'post_data',
  data: @post do %>
<% end %>

<%= javascript_pack_tag 'new_post' %>

app/javascript/packs/new_post.jsx: (though the React code in this file isn't really relevant for the question)

import React from 'react';
import ReactDOM from 'react-dom';
import PostForm from '../components/PostForm';

document.addEventListener('DOMContentLoaded', () => {
  const node = document.getElementById('post_data');
  const data = JSON.parse(node.getAttribute('data'));
  const dom = document.querySelector('#render_here');

  ReactDOM.render(
    <PostForm post={data} />,
    dom);
});

With this setup, the component is not rendered after clicking on a link that navigates to the page. On page reload, the component is rendered.

I tried a number of things.

In new_post.jsx, after commenting everything out and leaving only console.log(document.readyState), the console displays complete twice. On page load, it displays loading.

Then I tried to use jQuery's document.ready in new_post.jsx:

$(document).ready(function() {
  if (pageInitialized) {
    return;
  }
  pageInitialized = true;

  console.log('render');
...

And again, render is displayed twice after the page is navigated to. On page reload, render is displayed once.

What is going on?

Body:
I know this was resolved for you by a work around, but I came across this question as the first result in my search for the same issue, so for future reference for others having the same problem, I want to leave an explanation.

As described here Rails 5: how to use $(document).ready() with turbo-links a better way would be to listen for the turbolinks events.

document.addEventListener("turbolinks:load", ()=> {
   alert("loaded");
}

or if you need jQuery

$( document ).on("turbolinks:load", ()=> {
    alert("loaded");
}

CoffeScript variant:

document.addEventListener "turbolinks:load",  ->
    alert "loaded"


问:
Get rows with certain ID from all CSV files in one directory and create a new CSV file containing records only with that ID
I'm new to python and looking for help.

I have multiple files in one directory from which I need to pull rows with certain ID and save those into a new CSV file. My directory looks like this:

```<code>2019-09-01.csv
2019-09-02.csv
2019-09-03.csv
2019-09-04.csv
2019-09-05.csv
2019-09-06.csv
...
</code>```

My file structure:

```<code>   date        id     itemName   customerCount  orderCount
2019-09-01    1234     Guitar         72            90
2019-09-01    5432     Drums          15            15
...
</code>```

Would someone be able to give me a hand with this?

Thanks in advance!

答:
To get all your csv files, we can use OS module like below:

```<code>import os

all_files = []

for dirpath, dirname, files in os.walk('<your path where csv files are located>'):
for file in files:
    if file.endswith('.csv'):
        all_files.append(file)
</code>```

This will list all of your CSV files.

Now to read the CSV file, filter based on the ID and write to another csv file, you can use something like the below shown:

```<code>import pandas as pd
df01 = pd.read_csv('2019-01-01.csv')
df02 = pd.read_csv('2019-01-02.csv')

id01 = [1234,5432]

df01_filt = df01[df01['id'].isin(id01)]

df02_filt = df02[df02['id'].isin(['4023'])]

df_final = pd.concat([df01_filt, df02_filt])
df_final.to_csv('output.csv')
</code>```

Body:
I'm new to python and looking for help.

I have multiple files in one directory from which I need to pull rows with certain ID and save those into a new CSV file. My directory looks like this:

2019-09-01.csv
2019-09-02.csv
2019-09-03.csv
2019-09-04.csv
2019-09-05.csv
2019-09-06.csv
...

My file structure:

   date        id     itemName   customerCount  orderCount
2019-09-01    1234     Guitar         72            90
2019-09-01    5432     Drums          15            15
...

Would someone be able to give me a hand with this?

Thanks in advance!

Body:
To get all your csv files, we can use OS module like below:

import os

all_files = []

for dirpath, dirname, files in os.walk('<your path where csv files are located>'):
for file in files:
    if file.endswith('.csv'):
        all_files.append(file)

This will list all of your CSV files.

Now to read the CSV file, filter based on the ID and write to another csv file, you can use something like the below shown:

import pandas as pd
df01 = pd.read_csv('2019-01-01.csv')
df02 = pd.read_csv('2019-01-02.csv')

id01 = [1234,5432]

df01_filt = df01[df01['id'].isin(id01)]

df02_filt = df02[df02['id'].isin(['4023'])]

df_final = pd.concat([df01_filt, df02_filt])
df_final.to_csv('output.csv')


问:
How to sum up the duplicated value and keep all the rows on R
I have a dataset like this

```<code>V0  V1  V2
1   A   21
2   A   21
3   B   22
4   C   23
5   C   23
</code>```

So there are duplicated value in V1; and I would like to create a dataset like this, which sum up V2 but keeping the duplicates in V1:

```<code>V0  V1  V2
1   A   42
2   A   42
3   B   22
4   C   46
5   C   46
</code>```

thanks

答:
We can also use data.table

```<code>library(data.table)
setDT(dt)[,V2:=sum(V2),by=V1]
</code>```

Body:
I have a dataset like this

V0  V1  V2
1   A   21
2   A   21
3   B   22
4   C   23
5   C   23

So there are duplicated value in V1; and I would like to create a dataset like this, which sum up V2 but keeping the duplicates in V1:

V0  V1  V2
1   A   42
2   A   42
3   B   22
4   C   46
5   C   46

thanks

Body:
We can also use data.table

library(data.table)
setDT(dt)[,V2:=sum(V2),by=V1]


问:
Pandas groupby range of values when range is unknown
I am currently trying to extract interval ranges in pandas when the interval is unknown.
Suppose I have a df like:

```<code>df = pd.DataFrame({'range': ['range1','range1','range1','range1','range1','range1','range1','range1','range1','range1','range1','range1','range1','range1',
                             'range2','range2','range2','range2','range2','range2','range2','range2',
                             'range3','range3','range3','range3','range3','range3','range3','range3','range3','range3','range3','range3','range3','range3','range3','range3'],
                   'pos1':[1,2,3,4,100,101,102,104,107,108,207,208,209,210,
                           10,11,12,50,51,52,54,55,
                           50,51,52,53,107,108,109,110,111,112,113,800,802,803,804,805]})
</code>```

You can see that within each range, the numbers always increase and sometimes there is a big jump in between numbers.
I just eventually writing the output to a file so I don't need this to be a dataframe. I want the final output to be like

```<code>range1    1    4
range1    100  108
range1    207  210
range2    10   12
range2    50   55
range3    50   53
range3    107  113
range3    800  805
</code>```

I tried to do it like this (it's ugly), but my output is missing all of range2 and the last range of range1 and range3.

```<code>ranges = []
tmp = []
for r1, r2, p1, p2 in zip(df['range'], df['range'][1:], df['pos1'], df['pos1'][1:]):
    if r1 == r2 and (p1+10 > p2):
        tmp.append(p1)
    elif r1 == r2 and (p1+10 < p2):
        tmp.append(p1)
        ranges.append((r1, tmp))
        tmp = []

f = open('ranges.txt', 'w')
for x in ranges:
    f.write(x[0]+'\t'+str(min(x[1]))+'\t'+str(max(x[1]))+'\n')
</code>```

output:

```<code>range1  1       4
range1  100     108
range3  50      53
range3  107     113
</code>```

Body:
I am currently trying to extract interval ranges in pandas when the interval is unknown.
Suppose I have a df like:

df = pd.DataFrame({'range': ['range1','range1','range1','range1','range1','range1','range1','range1','range1','range1','range1','range1','range1','range1',
                             'range2','range2','range2','range2','range2','range2','range2','range2',
                             'range3','range3','range3','range3','range3','range3','range3','range3','range3','range3','range3','range3','range3','range3','range3','range3'],
                   'pos1':[1,2,3,4,100,101,102,104,107,108,207,208,209,210,
                           10,11,12,50,51,52,54,55,
                           50,51,52,53,107,108,109,110,111,112,113,800,802,803,804,805]})

You can see that within each range, the numbers always increase and sometimes there is a big jump in between numbers.
I just eventually writing the output to a file so I don't need this to be a dataframe. I want the final output to be like

range1    1    4
range1    100  108
range1    207  210
range2    10   12
range2    50   55
range3    50   53
range3    107  113
range3    800  805

I tried to do it like this (it's ugly), but my output is missing all of range2 and the last range of range1 and range3.

ranges = []
tmp = []
for r1, r2, p1, p2 in zip(df['range'], df['range'][1:], df['pos1'], df['pos1'][1:]):
    if r1 == r2 and (p1+10 > p2):
        tmp.append(p1)
    elif r1 == r2 and (p1+10 < p2):
        tmp.append(p1)
        ranges.append((r1, tmp))
        tmp = []

f = open('ranges.txt', 'w')
for x in ranges:
    f.write(x[0]+'\t'+str(min(x[1]))+'\t'+str(max(x[1]))+'\n')

output:

range1  1       4
range1  100     108
range3  50      53
range3  107     113

Body:
Would some thing like this work (you should modify the print command to write to file):

thresh = 10
s = df.groupby('range')['pos1'].diff().gt(thresh).cumsum()

for (r,g), d in df.groupby(['range',s])['pos1']:
    print(r, list(d))

Output:

range1 [1, 2, 3, 4]
range1 [100, 101, 102, 104, 107, 108]
range1 [207, 208, 209, 210]
range2 [10, 11, 12]
range2 [50, 51, 52, 54, 55]
range3 [50, 51, 52, 53]
range3 [107, 108, 109, 110, 111, 112, 113]
range3 [800, 802, 803, 804, 805]


问:
How can i extract values from promise and send them to client via node.js server?
I have a promise that returns data and I want to pass values of the promise as a response to client(web browser). I know i should probably use asynchronous js, but I'm not sure how to do that. Could you please give me some advice?

Here is how it looks like:

```<code>if(req.url === "/api/posts"){
    res.writeHead(200, {"Content-Type": "application/json"})
    let db = new AppDAO('./db/db.sqlite3')
    const postsDb = new PostsRepository(db)
    let posts = postsDb.getAll()
    db.close()
    console.log(posts)
    res.end()
}
</code>```

答:
What you need is to build the response when the DB Promise resolves

```<code>postsDb.getAll().then(posts => {
    console.log(posts)
    res.send(posts)
}).finally(() => db.close())
</code>```

Or if you want to use the modern syntax, and can declare the surrounding function as async:

```<code>try {
    const posts = await postsDb.getAll()
    console.log(posts)
    res.send(posts)
} catch(e) {
    // Handle database error
} finally {
    db.close()
}
</code>```

Body:
I have a promise that returns data and I want to pass values of the promise as a response to client(web browser). I know i should probably use asynchronous js, but I'm not sure how to do that. Could you please give me some advice?

Here is how it looks like:

if(req.url === "/api/posts"){
    res.writeHead(200, {"Content-Type": "application/json"})
    let db = new AppDAO('./db/db.sqlite3')
    const postsDb = new PostsRepository(db)
    let posts = postsDb.getAll()
    db.close()
    console.log(posts)
    res.end()
}

Body:
What you need is to build the response when the DB Promise resolves

postsDb.getAll().then(posts => {
    console.log(posts)
    res.send(posts)
}).finally(() => db.close())

Or if you want to use the modern syntax, and can declare the surrounding function as async:

try {
    const posts = await postsDb.getAll()
    console.log(posts)
    res.send(posts)
} catch(e) {
    // Handle database error
} finally {
    db.close()
}


问:
Adding value from HTML select into MySQL query
I've been re-learning HTML & MySQL, and diving into PHP lately.  I'm having some trouble (likely due to the fact that I'm still learning PHP) getting a situation to work correctly.  I have an instance of MySQL running on my server, which is also an IIS7 web server, with PHP installed. I've created a form with HTML & PHP to add entries to the table, and another to display the table, and filter it (if needed).  Currently it looks messy to me, and would also say it's wet. Trying to follow  the principals of DRY, I'd like to change this latter piece (which currently uses a text box & button for each possible table column to filter on) to use a dropdown to select the column to filter, and a single text box to fill in a value to filter for.  This is my current code (with multiple text boxes & buttons):

```<code><?php

/**
 * Function to query information based on
 * a parameter: in the first case, track_eps.
 *
 */

if (isset($_POST['search_eps'])) {
    try  {

        require "../config.php";
        require "../common.php";

        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "SELECT *
        FROM track_list
    WHERE track_eps = :track_eps";

        $track_eps = $_POST['track_eps'];

        $statement = $connection->prepare($sql);
        $statement->bindParam(':track_eps', $track_eps, PDO::PARAM_STR);
        $statement->execute();

        $result = $statement->fetchAll();
    } catch(PDOException $error) {
        echo $sql . "<br>" . $error->getMessage();
    }
}
if (isset($_POST['search_artist'])) {
    try  {

        require "../config.php";
        require "../common.php";

        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "SELECT *
        FROM track_list
    WHERE track_artist = :track_artist";

        $track_artist = $_POST['track_artist'];

        $statement = $connection->prepare($sql);
        $statement->bindParam(':track_artist', $track_artist, PDO::PARAM_STR);
        $statement->execute();

        $result = $statement->fetchAll();
    } catch(PDOException $error) {
        echo $sql . "<br>" . $error->getMessage();
    }
}
if (isset($_POST['search_album'])) {
    try  {

        require "../config.php";
        require "../common.php";

        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "SELECT *
        FROM track_list
    WHERE track_album = :track_album";

        $track_album = $_POST['track_album'];

        $statement = $connection->prepare($sql);
        $statement->bindParam(':track_album', $track_album, PDO::PARAM_STR);
        $statement->execute();

        $result = $statement->fetchAll();
    } catch(PDOException $error) {
        echo $sql . "<br>" . $error->getMessage();
    }
}
if (isset($_POST['search_year'])) {
    try  {

        require "../config.php";
        require "../common.php";

        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "SELECT *
        FROM track_list
    WHERE track_year = :track_year";

        $track_year = $_POST['track_year'];

        $statement = $connection->prepare($sql);
        $statement->bindParam(':track_year', $track_year, PDO::PARAM_STR);
        $statement->execute();

        $result = $statement->fetchAll();
    } catch(PDOException $error) {
        echo $sql . "<br>" . $error->getMessage();
    }
}
if (isset($_POST['search_pick'])) {
    try  {

        require "../config.php";
        require "../common.php";

        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "SELECT *
        FROM track_list
    WHERE track_pick = :track_pick";

        $track_pick = $_POST['track_pick'];

        $statement = $connection->prepare($sql);
        $statement->bindParam(':track_pick', $track_pick, PDO::PARAM_STR);
        $statement->execute();

        $result = $statement->fetchAll();
    } catch(PDOException $error) {
        echo $sql . "<br>" . $error->getMessage();
    }
}
if (isset($_POST['view'])) {
    try  {

        require "../config.php";
        require "../common.php";

        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "SELECT *
        FROM track_list";

        $track_pick = $_POST['track_pick'];

        $statement = $connection->prepare($sql);
        $statement->bindParam(':track_pick', $track_pick, PDO::PARAM_STR);
        $statement->execute();

        $result = $statement->fetchAll();
    } catch(PDOException $error) {
        echo $sql . "<br>" . $error->getMessage();
    }
}
?>
<?php require "templates/header.php"; ?>

<?php
if (isset($_POST['search_eps'])) {
    if ($result && $statement->rowCount() > 0) { ?>
        <h2>Results</h2>

        <table>
            <thead>
                <tr>
            <th>Title</th>
            <th>Artist</th>
            <th>Album</th>
            <th>Year</th>
            <th>Episode</th>
            <th>Picked By</th>
        </tr>
            </thead>
            <tbody>
        <?php foreach ($result as $row) { ?>
            <tr>
                <td><?php echo escape($row["track_name"]); ?></td>
                <td><?php echo escape($row["track_artist"]); ?></td>
                <td><?php echo escape($row["track_album"]); ?></td>
                <td><?php echo escape($row["track_year"]); ?></td>
                <td><?php echo escape($row["track_eps"]); ?></td>
                <td><?php echo escape($row["track_pick"]); ?></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
    <?php } else { ?>
        <blockquote>No results found for <?php echo escape($_POST['track_eps']); ?>.</blockquote>
    <?php }
}
if (isset($_POST['search_artist'])) {
    if ($result && $statement->rowCount() > 0) { ?>
        <h2>Results</h2>

        <table>
            <thead>
                <tr>
            <th>Title</th>
            <th>Artist</th>
            <th>Album</th>
            <th>Year</th>
            <th>Episode</th>
            <th>Picked By</th>
        </tr>
            </thead>
            <tbody>
        <?php foreach ($result as $row) { ?>
            <tr>
                <td><?php echo escape($row["track_name"]); ?></td>
                <td><?php echo escape($row["track_artist"]); ?></td>
                <td><?php echo escape($row["track_album"]); ?></td>
                <td><?php echo escape($row["track_year"]); ?></td>
                <td><?php echo escape($row["track_eps"]); ?></td>
                <td><?php echo escape($row["track_pick"]); ?></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
    <?php } else { ?>
        <blockquote>No results found for <?php echo escape($_POST['track_artist']); ?>.</blockquote>
    <?php }
}
if (isset($_POST['search_album'])) {
    if ($result && $statement->rowCount() > 0) { ?>
        <h2>Results</h2>

        <table>
            <thead>
                <tr>
            <th>Title</th>
            <th>Artist</th>
            <th>Album</th>
            <th>Year</th>
            <th>Episode</th>
            <th>Picked By</th>
        </tr>
            </thead>
            <tbody>
        <?php foreach ($result as $row) { ?>
            <tr>
                <td><?php echo escape($row["track_name"]); ?></td>
                <td><?php echo escape($row["track_artist"]); ?></td>
                <td><?php echo escape($row["track_album"]); ?></td>
                <td><?php echo escape($row["track_year"]); ?></td>
                <td><?php echo escape($row["track_eps"]); ?></td>
                <td><?php echo escape($row["track_pick"]); ?></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
    <?php } else { ?>
        <blockquote>No results found for <?php echo escape($_POST['track_album']); ?>.</blockquote>
    <?php }
}
if (isset($_POST['search_year'])) {
    if ($result && $statement->rowCount() > 0) { ?>
        <h2>Results</h2>

        <table>
            <thead>
                <tr>
            <th>Title</th>
            <th>Artist</th>
            <th>Album</th>
            <th>Year</th>
            <th>Episode</th>
            <th>Picked By</th>
        </tr>
            </thead>
            <tbody>
        <?php foreach ($result as $row) { ?>
            <tr>
                <td><?php echo escape($row["track_name"]); ?></td>
                <td><?php echo escape($row["track_artist"]); ?></td>
                <td><?php echo escape($row["track_album"]); ?></td>
                <td><?php echo escape($row["track_year"]); ?></td>
                <td><?php echo escape($row["track_eps"]); ?></td>
                <td><?php echo escape($row["track_pick"]); ?></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
    <?php } else { ?>
        <blockquote>No results found for <?php echo escape($_POST['track_year']); ?>.</blockquote>
    <?php }
}
if (isset($_POST['search_pick'])) {
    if ($result && $statement->rowCount() > 0) { ?>
        <h2>Results</h2>

        <table>
            <thead>
                <tr>
            <th>Title</th>
            <th>Artist</th>
            <th>Album</th>
            <th>Year</th>
            <th>Episode</th>
            <th>Picked By</th>
        </tr>
            </thead>
            <tbody>
        <?php foreach ($result as $row) { ?>
            <tr>
                <td><?php echo escape($row["track_name"]); ?></td>
                <td><?php echo escape($row["track_artist"]); ?></td>
                <td><?php echo escape($row["track_album"]); ?></td>
                <td><?php echo escape($row["track_year"]); ?></td>
                <td><?php echo escape($row["track_eps"]); ?></td>
                <td><?php echo escape($row["track_pick"]); ?></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
    <?php } else { ?>
        <blockquote>No results found for <?php echo escape($_POST['track_pick']); ?>.</blockquote>
    <?php }
}
if (isset($_POST['view'])) {
    if ($result && $statement->rowCount() > 0) { ?>
        <h2>Results</h2>

        <table>
            <thead>
                <tr>
            <th>Title</th>
            <th>Artist</th>
            <th>Album</th>
            <th>Year</th>
            <th>Episode</th>
            <th>Picked By</th>
        </tr>
            </thead>
            <tbody>
        <?php foreach ($result as $row) { ?>
            <tr>
                <td><?php echo escape($row["track_name"]); ?></td>
                <td><?php echo escape($row["track_artist"]); ?></td>
                <td><?php echo escape($row["track_album"]); ?></td>
                <td><?php echo escape($row["track_year"]); ?></td>
                <td><?php echo escape($row["track_eps"]); ?></td>
                <td><?php echo escape($row["track_pick"]); ?></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
    <?php } else { ?>
        <blockquote>No results found for <?php echo escape($_POST['track_pick']); ?>.</blockquote>
    <?php }
} ?>

<h2>Find track based on Episode #</h2>

<form method="post">
    <input type="submit" name="view" value="View All">
    <label for="track_eps">Episode #</label>
    <input type="text" id="track_eps" name="track_eps">
    <input type="submit" name="search_eps" value="Search Episodes">
    <label for="track_artist">Artist</label>
    <input type="text" id="track_artist" name="track_artist">
    <input type="submit" name="search_artist" value="Search Artist">
    <label for="track_album">Album</label>
    <input type="text" id="track_album" name="track_album">
    <input type="submit" name="search_album" value="Search Album">
    <label for="track_year">Year</label>
    <input type="text" id="track_year" name="track_year">
    <input type="submit" name="search_year" value="Search Year">
    <label for="track_pick">Picked By</label>
    <input type="text" id="track_pick" name="track_pick">
    <input type="submit" name="search_pick" value="Search Pick">
</form>

<a href="index.php">Back to home</a>

<?php require "templates/footer.php"; ?>
</code>```

As you can see - very lengthy.  I have a basic understanding of how to get dropdowns - although it seems like there's more than 1 way, I was trying this:

<form method="post">
    <select name="colunm">
        <option value="track_eps">Episode #</option>
        <option value="track_artist">Artist</option>
        <option value="track_album">Album</option>
        <option value="track_year">Year</option>
        <option value="track_pick">Picked by</option>
    </select>
    <input type="text" id="filter" name="filter">
    <input type="submit" name="Search" value="Search">
</form>
```

But the thing I'm having trouble with is taking the if statements from the beginning and making it more streamlined so that I only need 1 which will insert the value for the option selection and the value of the text box into the SQL statement WHERE [option value] = [text value]";

Does anyone have any suggestions?  I'm likely overlooking something basic.

答:
In your first set of if statements, the only thing changing is the query. There is no need to repeat everything else. In your second set of if statements, there is literally a single word changed in the dozens of lines of code. This is wasteful.

So, as you suspected, this can be done much more efficiently.

```<code><?php
require_once "../config.php";
require_once "../common.php";

// if it doesn't already, $options should look like this:
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];

// these are safe columns to search
$columns = ["track_eps", "track_artist", "track_album", "track_year", "track_pick"];
try  {
    // fall back to a safe value if needed
    $column = in_array($_POST["search_column"], $columns) ? $_POST["search_column"] : $columns[0];
    // if it doesn't already, $dsn should include charset=utf8mb4
    $connection = new PDO($dsn, $username, $password, $options);
    $sql = "SELECT * FROM track_list WHERE $column = ?";
    $statement = $connection->prepare($sql);
    // no need for binding, just pass parameters to execute
    $statement->execute([$_POST["search_text"]]);
    $result = $statement->fetchAll();

} catch (\Exception $e) {
    // don't show errors to the user, just pretend you got no results
    $result = [];
    // if you have a global exception handler, let it take over
    throw $e;
}
?>

<?php if(count($result) === 0): ?>
    <div class="alert">No results found for <?= escape($_POST["search_text"]) ?>.</div>
<?php else: ?>
    <h2>Results</h2>

    <table>
        <thead>
            <tr>
                <th>Title</th>
                <th>Artist</th>
                <th>Album</th>
                <th>Year</th>
                <th>Episode</th>
                <th>Picked By</th>
            </tr>
        </thead>
        <tbody>
    <?php foreach ($result as $row): ?>
            <tr>
                <td><?= escape($row["track_name"]) ?></td>
                <td><?= escape($row["track_artist"]) ?></td>
                <td><?= escape($row["track_album"]) ?></td>
                <td><?= escape($row["track_year"]) ?></td>
                <td><?= escape($row["track_eps"]) ?></td>
                <td><?= escape($row["track_pick"]) ?></td>
            </tr>
    <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>

<form method="post">
    <select name="search_column">
        <option value="track_eps">Episode #</option>
        <option value="track_artist">Artist</option>
        <option value="track_album">Album</option>
        <option value="track_year">Year</option>
        <option value="track_pick">Picked by</option>
    </select>
    <input type="text" id="filter" name="search_text">
    <button type="submit">Search</button>
</form>

<a href="index.php">Back to home</a>

<?php require "templates/footer.php"; ?>
</code>```

A couple of notes:

When mixing HTML and PHP, if you aren't using a proper templating system, it's neater to use alternative syntax for control structures and short echo tags. This is somewhat a matter of opinion, but once you've tried figuring out something like <?php }}} ?> 100 lines down the page, you'll appreciate endif and endforeach a lot.

I assume your escape() function is doing nothing more than htmlspecialchars(), nothing more is needed. So if you want to save typing, why not call it e() instead?

It's a 20 year old argument, but you should not be using semantic elements like
 for presentational purposes. Make it a
, give it a class, and style it.

Body:
I've been re-learning HTML & MySQL, and diving into PHP lately.  I'm having some trouble (likely due to the fact that I'm still learning PHP) getting a situation to work correctly.  I have an instance of MySQL running on my server, which is also an IIS7 web server, with PHP installed. I've created a form with HTML & PHP to add entries to the table, and another to display the table, and filter it (if needed).  Currently it looks messy to me, and would also say it's wet. Trying to follow  the principals of DRY, I'd like to change this latter piece (which currently uses a text box & button for each possible table column to filter on) to use a dropdown to select the column to filter, and a single text box to fill in a value to filter for.  This is my current code (with multiple text boxes & buttons):

<?php

/**
 * Function to query information based on
 * a parameter: in the first case, track_eps.
 *
 */

if (isset($_POST['search_eps'])) {
    try  {

        require "../config.php";
        require "../common.php";

        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "SELECT *
        FROM track_list
    WHERE track_eps = :track_eps";

        $track_eps = $_POST['track_eps'];

        $statement = $connection->prepare($sql);
        $statement->bindParam(':track_eps', $track_eps, PDO::PARAM_STR);
        $statement->execute();

        $result = $statement->fetchAll();
    } catch(PDOException $error) {
        echo $sql . "
" . $error->getMessage();
    }
}
if (isset($_POST['search_artist'])) {
    try  {

        require "../config.php";
        require "../common.php";

        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "SELECT *
        FROM track_list
    WHERE track_artist = :track_artist";

        $track_artist = $_POST['track_artist'];

        $statement = $connection->prepare($sql);
        $statement->bindParam(':track_artist', $track_artist, PDO::PARAM_STR);
        $statement->execute();

        $result = $statement->fetchAll();
    } catch(PDOException $error) {
        echo $sql . "
" . $error->getMessage();
    }
}
if (isset($_POST['search_album'])) {
    try  {

        require "../config.php";
        require "../common.php";

        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "SELECT *
        FROM track_list
    WHERE track_album = :track_album";

        $track_album = $_POST['track_album'];

        $statement = $connection->prepare($sql);
        $statement->bindParam(':track_album', $track_album, PDO::PARAM_STR);
        $statement->execute();

        $result = $statement->fetchAll();
    } catch(PDOException $error) {
        echo $sql . "
" . $error->getMessage();
    }
}
if (isset($_POST['search_year'])) {
    try  {

        require "../config.php";
        require "../common.php";

        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "SELECT *
        FROM track_list
    WHERE track_year = :track_year";

        $track_year = $_POST['track_year'];

        $statement = $connection->prepare($sql);
        $statement->bindParam(':track_year', $track_year, PDO::PARAM_STR);
        $statement->execute();

        $result = $statement->fetchAll();
    } catch(PDOException $error) {
        echo $sql . "
" . $error->getMessage();
    }
}
if (isset($_POST['search_pick'])) {
    try  {

        require "../config.php";
        require "../common.php";

        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "SELECT *
        FROM track_list
    WHERE track_pick = :track_pick";

        $track_pick = $_POST['track_pick'];

        $statement = $connection->prepare($sql);
        $statement->bindParam(':track_pick', $track_pick, PDO::PARAM_STR);
        $statement->execute();

        $result = $statement->fetchAll();
    } catch(PDOException $error) {
        echo $sql . "
" . $error->getMessage();
    }
}
if (isset($_POST['view'])) {
    try  {

        require "../config.php";
        require "../common.php";

        $connection = new PDO($dsn, $username, $password, $options);

        $sql = "SELECT *
        FROM track_list";

        $track_pick = $_POST['track_pick'];

        $statement = $connection->prepare($sql);
        $statement->bindParam(':track_pick', $track_pick, PDO::PARAM_STR);
        $statement->execute();

        $result = $statement->fetchAll();
    } catch(PDOException $error) {
        echo $sql . "
" . $error->getMessage();
    }
}
?>
<?php require "templates/header.php"; ?>

<?php
if (isset($_POST['search_eps'])) {
    if ($result && $statement->rowCount() > 0) { ?>

Results

            Title
            Artist
            Album
            Year
            Episode
            Picked By

        <?php foreach ($result as $row) { ?>

                <?php echo escape($row["track_name"]); ?>
                <?php echo escape($row["track_artist"]); ?>
                <?php echo escape($row["track_album"]); ?>
                <?php echo escape($row["track_year"]); ?>
                <?php echo escape($row["track_eps"]); ?>
                <?php echo escape($row["track_pick"]); ?>

        <?php } ?>

    <?php } else { ?>

No results found for <?php echo escape($_POST['track_eps']); ?>.

    <?php }
}
if (isset($_POST['search_artist'])) {
    if ($result && $statement->rowCount() > 0) { ?>

Results

            Title
            Artist
            Album
            Year
            Episode
            Picked By

        <?php foreach ($result as $row) { ?>

                <?php echo escape($row["track_name"]); ?>
                <?php echo escape($row["track_artist"]); ?>
                <?php echo escape($row["track_album"]); ?>
                <?php echo escape($row["track_year"]); ?>
                <?php echo escape($row["track_eps"]); ?>
                <?php echo escape($row["track_pick"]); ?>

        <?php } ?>

    <?php } else { ?>

No results found for <?php echo escape($_POST['track_artist']); ?>.

    <?php }
}
if (isset($_POST['search_album'])) {
    if ($result && $statement->rowCount() > 0) { ?>

Results

            Title
            Artist
            Album
            Year
            Episode
            Picked By

        <?php foreach ($result as $row) { ?>

                <?php echo escape($row["track_name"]); ?>
                <?php echo escape($row["track_artist"]); ?>
                <?php echo escape($row["track_album"]); ?>
                <?php echo escape($row["track_year"]); ?>
                <?php echo escape($row["track_eps"]); ?>
                <?php echo escape($row["track_pick"]); ?>

        <?php } ?>

    <?php } else { ?>

No results found for <?php echo escape($_POST['track_album']); ?>.

    <?php }
}
if (isset($_POST['search_year'])) {
    if ($result && $statement->rowCount() > 0) { ?>

Results

            Title
            Artist
            Album
            Year
            Episode
            Picked By

        <?php foreach ($result as $row) { ?>

                <?php echo escape($row["track_name"]); ?>
                <?php echo escape($row["track_artist"]); ?>
                <?php echo escape($row["track_album"]); ?>
                <?php echo escape($row["track_year"]); ?>
                <?php echo escape($row["track_eps"]); ?>
                <?php echo escape($row["track_pick"]); ?>

        <?php } ?>

    <?php } else { ?>

No results found for <?php echo escape($_POST['track_year']); ?>.

    <?php }
}
if (isset($_POST['search_pick'])) {
    if ($result && $statement->rowCount() > 0) { ?>

Results

            Title
            Artist
            Album
            Year
            Episode
            Picked By

        <?php foreach ($result as $row) { ?>

                <?php echo escape($row["track_name"]); ?>
                <?php echo escape($row["track_artist"]); ?>
                <?php echo escape($row["track_album"]); ?>
                <?php echo escape($row["track_year"]); ?>
                <?php echo escape($row["track_eps"]); ?>
                <?php echo escape($row["track_pick"]); ?>

        <?php } ?>

    <?php } else { ?>

No results found for <?php echo escape($_POST['track_pick']); ?>.

    <?php }
}
if (isset($_POST['view'])) {
    if ($result && $statement->rowCount() > 0) { ?>

Results

            Title
            Artist
            Album
            Year
            Episode
            Picked By

        <?php foreach ($result as $row) { ?>

                <?php echo escape($row["track_name"]); ?>
                <?php echo escape($row["track_artist"]); ?>
                <?php echo escape($row["track_album"]); ?>
                <?php echo escape($row["track_year"]); ?>
                <?php echo escape($row["track_eps"]); ?>
                <?php echo escape($row["track_pick"]); ?>

        <?php } ?>

    <?php } else { ?>

No results found for <?php echo escape($_POST['track_pick']); ?>.

    <?php }
} ?>

Find track based on Episode #

<form method="post">
    <input type="submit" name="view" value="View All">
    Episode #
    <input type="text" id="track_eps" name="track_eps">
    <input type="submit" name="search_eps" value="Search Episodes">
    Artist
    <input type="text" id="track_artist" name="track_artist">
    <input type="submit" name="search_artist" value="Search Artist">
    Album
    <input type="text" id="track_album" name="track_album">
    <input type="submit" name="search_album" value="Search Album">
    Year
    <input type="text" id="track_year" name="track_year">
    <input type="submit" name="search_year" value="Search Year">
    Picked By
    <input type="text" id="track_pick" name="track_pick">
    <input type="submit" name="search_pick" value="Search Pick">
</form>

Back to home

<?php require "templates/footer.php"; ?>

As you can see - very lengthy.  I have a basic understanding of how to get dropdowns - although it seems like there's more than 1 way, I was trying this:

<form method="post">
    <select name="colunm">
        <option value="track_eps">Episode #</option>
        <option value="track_artist">Artist</option>
        <option value="track_album">Album</option>
        <option value="track_year">Year</option>
        <option value="track_pick">Picked by</option>
    </select>
    <input type="text" id="filter" name="filter">
    <input type="submit" name="Search" value="Search">
</form>

But the thing I'm having trouble with is taking the if statements from the beginning and making it more streamlined so that I only need 1 which will insert the value for the option selection and the value of the text box into the SQL statement WHERE [option value] = [text value]";

Does anyone have any suggestions?  I'm likely overlooking something basic.

Body:
In your first set of if statements, the only thing changing is the query. There is no need to repeat everything else. In your second set of if statements, there is literally a single word changed in the dozens of lines of code. This is wasteful.

So, as you suspected, this can be done much more efficiently.

<?php
require_once "../config.php";
require_once "../common.php";

// if it doesn't already, $options should look like this:
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];

// these are safe columns to search
$columns = ["track_eps", "track_artist", "track_album", "track_year", "track_pick"];
try  {
    // fall back to a safe value if needed
    $column = in_array($_POST["search_column"], $columns) ? $_POST["search_column"] : $columns[0];
    // if it doesn't already, $dsn should include charset=utf8mb4
    $connection = new PDO($dsn, $username, $password, $options);
    $sql = "SELECT * FROM track_list WHERE $column = ?";
    $statement = $connection->prepare($sql);
    // no need for binding, just pass parameters to execute
    $statement->execute([$_POST["search_text"]]);
    $result = $statement->fetchAll();

} catch (\Exception $e) {
    // don't show errors to the user, just pretend you got no results
    $result = [];
    // if you have a global exception handler, let it take over
    throw $e;
}
?>

<?php if(count($result) === 0): ?>
    <div class="alert">No results found for <?= escape($_POST["search_text"]) ?>.</div>
<?php else: ?>
    <h2>Results</h2>

    <table>
        <thead>
            <tr>
                <th>Title</th>
                <th>Artist</th>
                <th>Album</th>
                <th>Year</th>
                <th>Episode</th>
                <th>Picked By</th>
            </tr>
        </thead>
        <tbody>
    <?php foreach ($result as $row): ?>
            <tr>
                <td><?= escape($row["track_name"]) ?></td>
                <td><?= escape($row["track_artist"]) ?></td>
                <td><?= escape($row["track_album"]) ?></td>
                <td><?= escape($row["track_year"]) ?></td>
                <td><?= escape($row["track_eps"]) ?></td>
                <td><?= escape($row["track_pick"]) ?></td>
            </tr>
    <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>

<form method="post">
    <select name="search_column">
        <option value="track_eps">Episode #</option>
        <option value="track_artist">Artist</option>
        <option value="track_album">Album</option>
        <option value="track_year">Year</option>
        <option value="track_pick">Picked by</option>
    </select>
    <input type="text" id="filter" name="search_text">
    <button type="submit">Search</button>
</form>

<a href="index.php">Back to home</a>

<?php require "templates/footer.php"; ?>

A couple of notes:

When mixing HTML and PHP, if you aren't using a proper templating system, it's neater to use alternative syntax for control structures and short echo tags. This is somewhat a matter of opinion, but once you've tried figuring out something like <?php }}} ?> 100 lines down the page, you'll appreciate endif and endforeach a lot.

I assume your escape() function is doing nothing more than htmlspecialchars(), nothing more is needed. So if you want to save typing, why not call it e() instead?

It's a 20 year old argument, but you should not be using semantic elements like <blockquote> for presentational purposes. Make it a <div>, give it a class, and style it.


问:
posix thread in c: terminating 1 thread from another using by passing threads
So as i am practicing threads, i realize that we can pass values to threads. Now i am wondering, Can i pass thread to another when creating. Something like that;

```<code>int main(){
  pthread_t t1;
  pthread_t t2;

  pthread_create(&t1, NULL, counting, t2);
  pthread_create(&t2, NULL, waiting, &results);
//...
}
</code>```

and my function looks like that;

```<code>void* counting(void * arg) {
    pthread_t *t = arg;
    pthread_cancel(arg);
}
</code>```

I am doing it because from the counting thread i want to terminate the waiting thread when it is done.

答:
You have two issues:

1) The value of t2 is not set until your second call to pthread_create returns. You need to change the order of the two pthread_create calls.

2) Since the thread takes a void *, you need to pass it a void *. It's not safe to cast a pthread_t to a void *. A common pattern is to malloc new structure, fill it in, and pass a pointer to it to the newly-created thread. The newly-created thread can free the structure when it's done with it. (You could also cast &t2 to a void *, just be sure that t2 remains valid at all times because otherwise it's not safe to dereference the pointer you passed to the newly-created thread.)

With those two changes, it should work.

Body:
So as i am practicing threads, i realize that we can pass values to threads. Now i am wondering, Can i pass thread to another when creating. Something like that;

int main(){
  pthread_t t1;
  pthread_t t2;

  pthread_create(&t1, NULL, counting, t2);
  pthread_create(&t2, NULL, waiting, &results);
//...
}

and my function looks like that;

void* counting(void * arg) {
    pthread_t *t = arg;
    pthread_cancel(arg);
}

I am doing it because from the counting thread i want to terminate the waiting thread when it is done.


问:
Return a list from Python Flask Get endpoint
My python version is 2.7 and Flask version is 0.10.
Below is my script:

```<code>from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask import request, jsonify
from flask import Response
import json
from pprint import pprint

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:postgres@localhost/postgres'
app.debug = True
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)

    def __init__(self, username, email):
        self.username = username
        self.email = email

    def __repr__(self):
        return '<User %r' % self.username

 @app.route('/users', methods=['GET'])
 def get_users():
    myUsers = User.query.all()
    pprint(myUsers)
    return jsonify({'users': myUsers})

@app.route('/post_user', methods=['POST'])
def post_user():
    data = request.get_json()
    user = User(data['username'], data['email'])
    db.session.add(user)
    db.session.commit()
    return jsonify(**data)

if __name__ == '__main__':
    app.run()
</code>```

The post call works fine and I can see the data in the database. But when I try to get the users,I get the below error:

```<code>'TypeError: <User u'John' is not JSON serializable // Werkzeug Debugger'.
</code>```

The output of the 'print' is as below:

```<code>[<User u'John', <User u'Jane']
</code>```

It isn't printing the entire object in the list i.e. the id, username and email. I am not sure what is the problem here. I have gone through many stackoverflow post about returing a list with Flask but nothing works. Also, the versions that I need to use are quite old so that is also a limitation here.

答:
I expect that the problem is in your __repr__ function that it returns the string '<User %r' % self.username and doesn't return the whole object data.

I can suggest that you use a serialize function in your User class like that:

    @property
    def serialize(self):
        return {
            'id': self.id,
            'username': self.username
            'email': self.email
        }
```

<p>And then you will be able to return <em>JSON</em> output using:</p>

```users = session.query(User).all()
return jsonify(users=[i.serialize for i in users])
```

Body:
My python version is 2.7 and Flask version is 0.10.
Below is my script:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask import request, jsonify
from flask import Response
import json
from pprint import pprint

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:postgres@localhost/postgres'
app.debug = True
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)

    def __init__(self, username, email):
        self.username = username
        self.email = email

    def __repr__(self):
        return '<User %r' % self.username

 @app.route('/users', methods=['GET'])
 def get_users():
    myUsers = User.query.all()
    pprint(myUsers)
    return jsonify({'users': myUsers})

@app.route('/post_user', methods=['POST'])
def post_user():
    data = request.get_json()
    user = User(data['username'], data['email'])
    db.session.add(user)
    db.session.commit()
    return jsonify(**data)

if __name__ == '__main__':
    app.run()

The post call works fine and I can see the data in the database. But when I try to get the users,I get the below error:

'TypeError: <User u'John' is not JSON serializable // Werkzeug Debugger'.

The output of the 'print' is as below:

[<User u'John', <User u'Jane']

It isn't printing the entire object in the list i.e. the id, username and email. I am not sure what is the problem here. I have gone through many stackoverflow post about returing a list with Flask but nothing works. Also, the versions that I need to use are quite old so that is also a limitation here.

Body:
I expect that the problem is in your __repr__ function that it returns the string '<User %r' % self.username and doesn't return the whole object data.

I can suggest that you use a serialize function in your User class like that:

    @property
    def serialize(self):
        return {
            'id': self.id,
            'username': self.username
            'email': self.email
        }

And then you will be able to return JSON output using:

users = session.query(User).all()
return jsonify(users=[i.serialize for i in users])


问:
Using the g++ C++ compiler from cygwin
I am trying to execute my first "Hello World!" in C++. I am using Windows XP, and I have installed cygwin, in which the g++ C++ compiler is installed. I have written a small hello-world program, and saved it in hello.cpp. From the command prompt I write:

g++ hello.cpp

But I get:

'g++' is not recognized as an internal or external command, operable program or batch file.

I have installed cygwin in my D:\programs\cygwin. I have made another directory with my hello-world file in D:\cpp. Something with my installation or my paths seems to be not Ok, but I cannot figure what. I have run the exe file of cygwin, and in the installation directory I have got all necessary files, I think: bin folder, lib, Cygwin.bat, etc.

I have read that in case of such error message I should check whether the cygwin1.dll file is copied into the main folder of Windows. Is this the C:\WINDOWS directory? I have looked there, and I don't have such a file there. I have cygwin1.dll in the bin folder of cygwin: D:\programs\cygwin\bin.
Also, how do I check whether the bin folder (D:\programs\cygwin\bin) is considered in the search path of commands?

I think that the hello-world program shouldn't contain any syntax errors, I have just copied it. Also, when I write

g++ -v

I get the same error message: that the command is not recognized.
I would appreciate if someone give me a hint what should I look at. Thank you.

Here is the code of the hello-world program:

```<code>#include <iostream>
using namespace std;

int main() {
  cout <<"Hello World!"<<endl;
  return 0;
}
</code>```

I have installed g++, and now when I type g++ -v, I get: Reading specs from: \d\cygnus\cygwin-b20\H-i586-cygwin32\bin..\lib\gcc-lib\i586-cygwin32\egcs-2.91.57\specs gcc version egcs-2.91.57 19980901 (egcs-1.1 release)

In the bin directory I have both g++.exe, and gcc.exe. I don't understand why I get "gcc" above and not "g++". I don't know how important this is, but I receive an error message when I try to compile the program in the DOS command prompt:

g++ hello.cpp

I get:

```<code>hello.cpp:1: parse error before character 0357
hello.cpp: In function 'int main()':
hello.cpp:'cout' undeclared (first use this function)
hello.cpp: (Each undeclared identifier is reported only once
hello.cpp: for each function it appears in.)
hello.cpp: 'endl' undeclared (first use this function)
</code>```

And here is what I get when I try to compile the program in the cygwin shell:

```<code>hello.cpp:1: parse error before character 0357
hello.cpp: In function 'int main()':
hello.cpp:'cout' undeclared (first use this function)
hello.cpp: (Each undeclared identifier is reported only once
hello.cpp: for each function it appears in.)
hello.cpp: 'endl' undeclared (first use this function)
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
</code>```

答:
Ah, I have thought that g++ is installed when I install cygwin. So, I should actually first install g++ in that case, shouldn't I?

(and then reboot)

ContentLicense: CC BY-SA 2.5

Title: Using the g++ C++ compiler from cygwin

Body:
I am trying to execute my first "Hello World!" in C++. I am using Windows XP, and I have installed cygwin, in which the g++ C++ compiler is installed. I have written a small hello-world program, and saved it in hello.cpp. From the command prompt I write:

g++ hello.cpp

But I get:

'g++' is not recognized as an internal or external command, operable program or batch file.

I have installed cygwin in my D:\programs\cygwin. I have made another directory with my hello-world file in D:\cpp. Something with my installation or my paths seems to be not Ok, but I cannot figure what. I have run the exe file of cygwin, and in the installation directory I have got all necessary files, I think: bin folder, lib, Cygwin.bat, etc.

I have read that in case of such error message I should check whether the cygwin1.dll file is copied into the main folder of Windows. Is this the C:\WINDOWS directory? I have looked there, and I don't have such a file there. I have cygwin1.dll in the bin folder of cygwin: D:\programs\cygwin\bin.
Also, how do I check whether the bin folder (D:\programs\cygwin\bin) is considered in the search path of commands?

I think that the hello-world program shouldn't contain any syntax errors, I have just copied it. Also, when I write

g++ -v

I get the same error message: that the command is not recognized.
I would appreciate if someone give me a hint what should I look at. Thank you.

Here is the code of the hello-world program:

#include <iostream>
using namespace std;

int main() {
  cout <<"Hello World!"<<endl;
  return 0;
}

I have installed g++, and now when I type g++ -v, I get: Reading specs from: \d\cygnus\cygwin-b20\H-i586-cygwin32\bin..\lib\gcc-lib\i586-cygwin32\egcs-2.91.57\specs gcc version egcs-2.91.57 19980901 (egcs-1.1 release)

In the bin directory I have both g++.exe, and gcc.exe. I don't understand why I get "gcc" above and not "g++". I don't know how important this is, but I receive an error message when I try to compile the program in the DOS command prompt:

g++ hello.cpp

I get:

hello.cpp:1: parse error before character 0357
hello.cpp: In function 'int main()':
hello.cpp:'cout' undeclared (first use this function)
hello.cpp: (Each undeclared identifier is reported only once
hello.cpp: for each function it appears in.)
hello.cpp: 'endl' undeclared (first use this function)

And here is what I get when I try to compile the program in the cygwin shell:

hello.cpp:1: parse error before character 0357
hello.cpp: In function 'int main()':
hello.cpp:'cout' undeclared (first use this function)
hello.cpp: (Each undeclared identifier is reported only once
hello.cpp: for each function it appears in.)
hello.cpp: 'endl' undeclared (first use this function)
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files

LastEditorDisplayName: Rich B


问:
ALAssetPropertyDate returns "wrong" date
I'm currently working on a project in which i need to read some (Latitude, Longitude and date ) EXIF data. The location data seems correct, but the date i'm getting seems to be the "date last modified" date.

```<code>{
    CLLocation *loc = [asset valueForProperty:ALAssetPropertyLocation];
    NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
    //Returns Last modified date(Picture was taken ... let's say september
    //last year and it would return the date and time I 'modified' the image).
    NSString *latitude  = [NSString stringWithFormat:@"%g",loc.coordinate.latitude];//Returns correct Latitude
    NSString *longitude = [NSString stringWithFormat:@"%g",loc.coordinate.longitude];//Returns correct Longitude
}
</code>```

My question is: Am i doing something terribly wrong, or is this expected behavior.
I also tried to use the loc.timestamp instead of the [asset valueForProperty:ALAssetPropertyDate] but these returned the same date.
 Any help is greatly appreciated !

答:
Though it's not explicitly documented, I'm guessing that this is the expected behavior. The date refers to when the asset was created and when you're modifying the image, you're probably implicitly creating a new asset. Nothing in the ALAsset documentation suggests that its properties correspond to the image's EXIF data.

To access the EXIF data, you could use the Image I/O framework (available since iOS 4.0), specifically the CGImageSourceCopyProperties function.

Body:
I'm currently working on a project in which i need to read some (Latitude, Longitude and date ) EXIF data. The location data seems correct, but the date i'm getting seems to be the "date last modified" date.

{
    CLLocation *loc = [asset valueForProperty:ALAssetPropertyLocation];
    NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
    //Returns Last modified date(Picture was taken ... let's say september
    //last year and it would return the date and time I 'modified' the image).
    NSString *latitude  = [NSString stringWithFormat:@"%g",loc.coordinate.latitude];//Returns correct Latitude
    NSString *longitude = [NSString stringWithFormat:@"%g",loc.coordinate.longitude];//Returns correct Longitude
}

My question is: Am i doing something terribly wrong, or is this expected behavior.
I also tried to use the loc.timestamp instead of the [asset valueForProperty:ALAssetPropertyDate] but these returned the same date.
 Any help is greatly appreciated !


问:
Building a custom loss in Keras
I have been trying to build my own custom loss in Keras and encounter some error.

My need is the following: I want to build a classifier on a time series that is stable: it must decide wether the curve has an ascendant or descendant tendency, meaning that if the derivative is negative for say 2 or 3 time steps, it must keep the "ascendant" classification rather than switching to descendant for a small amount of time. The input data are contextual data that should help the NN detect wether the current change in the direction of the time series is likely to last or not.

To do this I imagined building a custom loss function that would add a penalty for each change of classification above or below the reference.

I first wanted to use np.where on the predicted and reference tensors but I understood browsing the forum this was not possible as the loss function uses placeholder tensors. I must remain within the functional space of keras backend.

For this reason, I used some code I found on this forum to build the Following loss function:

```<code>import keras.backend as kb

def roll_reg(x):
    length = kb.int_shape(x)[0]
    x_tile = kb.tile(x, [2, 1])
    x_roll = x_tile[length - 1:-1]
    return kb.sum(kb.abs(x - x_roll))

def custom_loss(y_actual,y_predicted):
    posact=kb.flatten(y_actual)
    pospred=kb.flatten(y_predicted)
    na=roll_reg(posact)
    np=roll_reg(pospred)
    loss_cust=kb.mean(((y_actual-y_predicted)**2)**0.5/(kb.max(y_actual)-kb.min(y_actual)))+abs(na-np)/kb.int_shape(posact)*10
    return loss_cust
</code>```

but I still get this error:

ValueError: Shape must be rank 1 but is rank 2 for
  'loss_9/dense_25_loss/Tile' (op: 'Tile') with input shapes: [?], [2].

which I interpret as my operations not being possible on placeholder tensors… while I was trying to use a dedicated function for these kind of tensors.

Does anyone have any idea on what I could do to improve this code in order to get my loss function running?

Some update on this question. I found on the internet this page : https://medium.com/@j.ali.hab/on-custom-loss-functions-in-keras-3af88cf59e48 where the same issue is solved using tf.math Library.

So I updated my code to :

```<code>def custom_loss(y_actual,y_predicted):
    na=kb.tf.math.cumsum(y_actual,axis=0)
    np=kb.tf.math.cumsum(y_predicted,axis=0)

    penalty=kb.abs(kb.sum(np-na))/118319#/kb.int_shape(y_actual)[0]
    loss_nico=penalty
    return loss_nico
</code>```

Now the code is compiling but I get a Nan at the very end of each epoch as a loss function. Of course, the neural net is not learning anything as a consequence.

Does anyone have a clue about what I am doing wrong ?

答:
OK, I finally got it !

the problem was in te kb.abs() : it is not needed as kb.sum() returns a scalar.

So the final code that works is :

```<code>def custom_loss(y_actual,y_predicted):

    na=kb.tf.math.cumsum(y_actual,axis=0)
    np=kb.tf.math.cumsum(y_predicted,axis=0)
    penalty=abs(kb.sum(na-np))
    loss_nico=kb.sum(((y_actual-y_predicted)))/1000+penalty/1000
    return loss_nico
</code>```

this compiles and enables the RN to actually learn Something. It is still not perfect as the "non penalty" part should be improved but it works still.

Title: Building a custom loss in Keras

Body:
I have been trying to build my own custom loss in Keras and encounter some error.

My need is the following: I want to build a classifier on a time series that is stable: it must decide wether the curve has an ascendant or descendant tendency, meaning that if the derivative is negative for say 2 or 3 time steps, it must keep the "ascendant" classification rather than switching to descendant for a small amount of time. The input data are contextual data that should help the NN detect wether the current change in the direction of the time series is likely to last or not.

To do this I imagined building a custom loss function that would add a penalty for each change of classification above or below the reference.

I first wanted to use np.where on the predicted and reference tensors but I understood browsing the forum this was not possible as the loss function uses placeholder tensors. I must remain within the functional space of keras backend.

For this reason, I used some code I found on this forum to build the Following loss function:

import keras.backend as kb

def roll_reg(x):
    length = kb.int_shape(x)[0]
    x_tile = kb.tile(x, [2, 1])
    x_roll = x_tile[length - 1:-1]
    return kb.sum(kb.abs(x - x_roll))

def custom_loss(y_actual,y_predicted):
    posact=kb.flatten(y_actual)
    pospred=kb.flatten(y_predicted)
    na=roll_reg(posact)
    np=roll_reg(pospred)
    loss_cust=kb.mean(((y_actual-y_predicted)**2)**0.5/(kb.max(y_actual)-kb.min(y_actual)))+abs(na-np)/kb.int_shape(posact)*10
    return loss_cust

but I still get this error:

ValueError: Shape must be rank 1 but is rank 2 for
  'loss_9/dense_25_loss/Tile' (op: 'Tile') with input shapes: [?], [2].

which I interpret as my operations not being possible on placeholder tensors… while I was trying to use a dedicated function for these kind of tensors.

Does anyone have any idea on what I could do to improve this code in order to get my loss function running?

Some update on this question. I found on the internet this page : https://medium.com/@j.ali.hab/on-custom-loss-functions-in-keras-3af88cf59e48 where the same issue is solved using tf.math Library.

So I updated my code to :

def custom_loss(y_actual,y_predicted):
    na=kb.tf.math.cumsum(y_actual,axis=0)
    np=kb.tf.math.cumsum(y_predicted,axis=0)

    penalty=kb.abs(kb.sum(np-na))/118319#/kb.int_shape(y_actual)[0]
    loss_nico=penalty
    return loss_nico

Now the code is compiling but I get a Nan at the very end of each epoch as a loss function. Of course, the neural net is not learning anything as a consequence.

Does anyone have a clue about what I am doing wrong ?

Body:
OK, I finally got it !

the problem was in te kb.abs() : it is not needed as kb.sum() returns a scalar.

So the final code that works is :

def custom_loss(y_actual,y_predicted):

    na=kb.tf.math.cumsum(y_actual,axis=0)
    np=kb.tf.math.cumsum(y_predicted,axis=0)
    penalty=abs(kb.sum(na-np))
    loss_nico=kb.sum(((y_actual-y_predicted)))/1000+penalty/1000
    return loss_nico

this compiles and enables the RN to actually learn Something. It is still not perfect as the "non penalty" part should be improved but it works still.


问:
Adding extra column when exporting Feature Importance
I am using .feature_importances_ to export the importance values from a regression tree model to a CSV. The tree is run multiple times with different numbers of variables. I am looking to add an extra column that lists the number of variables used when calculating the importance.

Currently I am able to export the importance values and variable names to a CSV with the code below

```<code>Importance = list(zip(model.feature_importances_,list(X)))
        Importance = pd.DataFrame(Importance,columns=["Importance","Feature_Name"])
        Importance.to_csv('File.csv', mode='a', header=False)
</code>```

and I get a CSV that looks like this

```<code>  Imp  VarName
0 1.00 0mm
0 0.58 0mm
1 0.42 0.63mm
0 0.54 0.63mm
1 0.36 0mm
2 0.10 0.125mm
</code>```

What I am looking for is to add a 4th column with the number of variables used in that tree so the output would be like this

```<code>  Imp  VarName NumVar
0 1.00 0mm     1
0 0.58 0mm     2
1 0.42 0.63mm  2
0 0.54 0.63mm  3
1 0.36 0mm     3
2 0.10 0.125mm 3
</code>```

答:
This should work, I add a array of the size of the len with the value of the number of the len of X.

import numpy as np

Importance = list(zip(model.feature_importances_, list(X), len(X) * np.ones(len(X))))
Importance = pd.DataFrame(Importance,columns=["Importance","Feature_Name", "NumVar"])
Importance.to_csv('File.csv')
```

When running for the first time, I think you can delete the append mode and the header to False.

Body:
I am using .feature_importances_ to export the importance values from a regression tree model to a CSV. The tree is run multiple times with different numbers of variables. I am looking to add an extra column that lists the number of variables used when calculating the importance.

Currently I am able to export the importance values and variable names to a CSV with the code below

Importance = list(zip(model.feature_importances_,list(X)))
        Importance = pd.DataFrame(Importance,columns=["Importance","Feature_Name"])
        Importance.to_csv('File.csv', mode='a', header=False)

and I get a CSV that looks like this

  Imp  VarName
0 1.00 0mm
0 0.58 0mm
1 0.42 0.63mm
0 0.54 0.63mm
1 0.36 0mm
2 0.10 0.125mm

What I am looking for is to add a 4th column with the number of variables used in that tree so the output would be like this

  Imp  VarName NumVar
0 1.00 0mm     1
0 0.58 0mm     2
1 0.42 0.63mm  2
0 0.54 0.63mm  3
1 0.36 0mm     3
2 0.10 0.125mm 3

Body:
This should work, I add a array of the size of the len with the value of the number of the len of X.

import numpy as np

Importance = list(zip(model.feature_importances_, list(X), len(X) * np.ones(len(X))))
Importance = pd.DataFrame(Importance,columns=["Importance","Feature_Name", "NumVar"])
Importance.to_csv('File.csv')

When running for the first time, I think you can delete the append mode and the header to False.


问:
lifting performance of query
I'm building a forum app in which users can post messages. These messages can be seen by others.

Table structure (simplified):

```<code>// table: users
user_id | username | gender
---------------------------
1       | john     | m
2       | jane     | f
...

// table: posts
post_id | user_id | title
-------------------------
1       | 1       | Hello
...

// table: views
view_id | post_id | user_id | timestamp
---------------------------------------
1       | 1       | 2       | 2020-01-01 12:00:00
...
</code>```

Now I want to create a query to return some statistics about this post. I want to get the unique views (every time a user views a post, this is logged, but I only want to count all users once) and I want to get the ratio male/female.

```<code>SELECT
    title,
    (SELECT COUNT(DISTINCT user_id) FROM views WHERE post_id = 1) AS unique_views,
    (SELECT COUNT(user_id) FROM users WHERE gender = 'm' AND user_id IN (SELECT user_id FROM views WHERE post_id = 1) AS male_views,
    (SELECT COUNT(user_id) FROM users WHERE gender = 'f' AND user_id IN (SELECT user_id FROM views WHERE post_id = 1) AS female_views
FROM
    posts
WHERE
    post_id = 1
</code>```

The query works, but it's a query with 5 subqueries. I don't have a lot of data to test on yet, but I'm afraid performance will drop when I have let's say +1m users, +1m posts and +10m views.

A different approach would be to completely split the query into multiple queries: one for the total unique views, on for the gender views (with a distinct), but then it would still be 6 queries in total.

I'm using postgresql and I have an index on users.user_id, users.gender, posts.post_id, views.view_id, views.post_id.

Question: is there another way (for example with JOINs) to execute this query, and would have better performance when the volume of the database increases?

答:
You can join and do conditional aggregation instead of nesting subqueries:</p>

```<code>select
    p.title,
    count(distinct u.user_id) unique_views,
    count(u.user_id) filter(where u.gender = 'm') male_views,
    count(u.user_id) filter(where u.gender = 'f') female_views
from views v
inner join users u on u.user_id = v.user_id
inner join posts p on p.post_id = v.post_id
where p.post_id = 1
group by p.post_id, p.title
</code>```

Title: lifting performance of query

Body:
I'm building a forum app in which users can post messages. These messages can be seen by others.

Table structure (simplified):

// table: users
user_id | username | gender
---------------------------
1       | john     | m
2       | jane     | f
...

// table: posts
post_id | user_id | title
-------------------------
1       | 1       | Hello
...

// table: views
view_id | post_id | user_id | timestamp
---------------------------------------
1       | 1       | 2       | 2020-01-01 12:00:00
...

Now I want to create a query to return some statistics about this post. I want to get the unique views (every time a user views a post, this is logged, but I only want to count all users once) and I want to get the ratio male/female.

SELECT
    title,
    (SELECT COUNT(DISTINCT user_id) FROM views WHERE post_id = 1) AS unique_views,
    (SELECT COUNT(user_id) FROM users WHERE gender = 'm' AND user_id IN (SELECT user_id FROM views WHERE post_id = 1) AS male_views,
    (SELECT COUNT(user_id) FROM users WHERE gender = 'f' AND user_id IN (SELECT user_id FROM views WHERE post_id = 1) AS female_views
FROM
    posts
WHERE
    post_id = 1

The query works, but it's a query with 5 subqueries. I don't have a lot of data to test on yet, but I'm afraid performance will drop when I have let's say +1m users, +1m posts and +10m views.

A different approach would be to completely split the query into multiple queries: one for the total unique views, on for the gender views (with a distinct), but then it would still be 6 queries in total.

I'm using postgresql and I have an index on users.user_id, users.gender, posts.post_id, views.view_id, views.post_id.

Question: is there another way (for example with JOINs) to execute this query, and would have better performance when the volume of the database increases?

Body:
You can join and do conditional aggregation instead of nesting subqueries:

select
    p.title,
    count(distinct u.user_id) unique_views,
    count(u.user_id) filter(where u.gender = 'm') male_views,
    count(u.user_id) filter(where u.gender = 'f') female_views
from views v
inner join users u on u.user_id = v.user_id
inner join posts p on p.post_id = v.post_id
where p.post_id = 1
group by p.post_id, p.title


问:
Retrieve and Edit Contact Marketing List Subscribed using Webservice Endpoint Optiomization Cannot be Performed
I am using my own custom webservice endpoint to access the Contacts.  I also want to get the list of marketing lists the contact is associated too and update the subscribed field.

But when I try expand the Marketing Lists details the API responds with this Optiomization cannot be performed error?  Message below

"message": "An error has occurred.",
    "exceptionMessage": "Optimization cannot be performed.The following fields cause the error:\r\nMarketingLists.Subscribed: View Subscriptions has BQL delegate\r\nMarketingLists.MarketingListID: View Subscriptions has BQL delegate\r\nMarketingLists.ContactID: View Subscriptions has BQL delegate\r\n",

I get the same error when using the Default endpoint as well.

Also when trying to go the other way and get the marketing lists and expand the list members I also get the same similar message?

Any help with this would be appreciated.

答: Looks like it only works by retrieving one contact at a time.  Not by getting all Contacts at once.</p>