问:
Nginx Reverse proxy in front of WordPress and MySQL slowness
I have the following setup:

1 server with 4 cores / 8 HT (Intel Xeon E3-1231 v3) with 32GB RAM and 2TB HDD
I've installed Proxmox on it. Then I've created a few KVMs:

Nginx Reverse proxy - Public IP 192.168.0.10, Internal IP 10.10.10.10

The IPs are set for example purposes only, the real IPs are different

Nginx which servers only static content website - Only Internal IP 10.10.10.20

Nginx + PHP-FPM which servers WordPress - Only internal IP 10.10.10.30

MySQL - Only internal IP 10.10.10.40

All servers communicates trough the internal network. The "lag" is 0.200ms - 0.350ms

I also have 2 websites:

example.com - pointed via A record to Nginx Reverse Proxy's Public IP 192.168.0.10

wp.example.com - pointed via A record to Nginx Reverse Proxy's Public IP 192.168.0.10

Nginx Reverse Proxy is configured to proxy_pass the requests based on the website entered in the browser.

When you try to load example.com NGINX reverse proxy pass the connection to internal IP 10.10.10.20 where another nginx serves the html website - example.com

So far so good. Everything works good.

However, the issue occur when someone try to enter wp.example.com, NGINX reverse proxy pass the connection to IP 10.10.10.30 where another nginx + php-fpm serves the php files.

So far so good, however I've installed WordPress and use MySQL server which is setup on another KVM with IP 10.10.10.40. Ok. Everything works, however the connection to and from MySQL server seems to be slow, as the entire wordpress website is loading too slow. If only php files are served (without any mysql queries) the website is loading super fast, however when WordPress is installed it makes connections to the databases which is on another server and the things start to work slow...

None of the machines are loading high at all. They have a lot of free CPU, disk space and RAM. However for some reason MySQL (MariaDB 10.2.6) is the bottleneck here... but I do not know why...

Here is the conf file from my NGINX reverse proxy:

```<code>server  {
    listen  80;
    server_name example.com www.example.com;
    location / {
        proxy_pass http://10.10.10.20:80/;
    }
}
server {
    listen 80;
    server_name wp.example.com www.wp.example.com;
    location / {
        proxy_pass http://10.10.10.30:80/;
    }
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
}
</code>```

Here is the nginx conf file from my KVM server who serves PHP website - WordPress:

```<code>server {
    listen       10.10.10.30:80;
    server_name  wp.example.com www.wp.example.com;
    root   /var/www;
    index index.php index.html index.htm;
    location / {
         try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
         root /usr/share/nginx/html;
    }
    location ~ \.php$ {
         try_files $uri =404;
         fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
}
</code>```

How I can troubleshot why WordPress is loading too slow? There aren't packet drops between servers, also the connections are good I think... only 0.200 - 0.350 ms between servers...

I think the slowness occur because WordPress tries to make connections to external addresses but as the server where wordpress "lives" doesn't have public internet connection, this slows the things... I am not sure if this is the reason... but it could be?

答:
A few things pop into my mind:

How are you connecting from wordpress to the mysql server (the
wp-config.php file)?

Are you using the IP address or a DNS?

Do you connect directly to the mysql server or do you have a proxysql in
the middle?

Are you using ssl to connect to mysql?

Have you done a benchmark on the mysql server, and determine how many queries it can take from the remote wp server?

Is the mysql server using an SSD? HDD will be slow...

Have you tweaked the my.cnf file, or are you using the default settings?

Are the mysql and wp servers on the same zone, within the same region?

For mysql, I would suggest removing mariadb and installing Percona.
You can generate a new my.cnf from there too: https://tools.percona.com/

I also recently installed MariaDB 10.2 and was wondering why it was much slower than 10.1... and percona solved it for me.

Body:
I have the following setup:

1 server with 4 cores / 8 HT (Intel Xeon E3-1231 v3) with 32GB RAM and 2TB HDD
I've installed Proxmox on it. Then I've created a few KVMs:

Nginx Reverse proxy - Public IP 192.168.0.10, Internal IP 10.10.10.10

The IPs are set for example purposes only, the real IPs are different

Nginx which servers only static content website - Only Internal IP 10.10.10.20

Nginx + PHP-FPM which servers WordPress - Only internal IP 10.10.10.30

MySQL - Only internal IP 10.10.10.40

All servers communicates trough the internal network. The "lag" is 0.200ms - 0.350ms

I also have 2 websites:

example.com - pointed via A record to Nginx Reverse Proxy's Public IP 192.168.0.10

wp.example.com - pointed via A record to Nginx Reverse Proxy's Public IP 192.168.0.10

Nginx Reverse Proxy is configured to proxy_pass the requests based on the website entered in the browser.

When you try to load example.com NGINX reverse proxy pass the connection to internal IP 10.10.10.20 where another nginx serves the html website - example.com

So far so good. Everything works good.

However, the issue occur when someone try to enter wp.example.com, NGINX reverse proxy pass the connection to IP 10.10.10.30 where another nginx + php-fpm serves the php files.

So far so good, however I've installed WordPress and use MySQL server which is setup on another KVM with IP 10.10.10.40. Ok. Everything works, however the connection to and from MySQL server seems to be slow, as the entire wordpress website is loading too slow. If only php files are served (without any mysql queries) the website is loading super fast, however when WordPress is installed it makes connections to the databases which is on another server and the things start to work slow...

None of the machines are loading high at all. They have a lot of free CPU, disk space and RAM. However for some reason MySQL (MariaDB 10.2.6) is the bottleneck here... but I do not know why...

Here is the conf file from my NGINX reverse proxy:

server  {
    listen  80;
    server_name example.com www.example.com;
    location / {
        proxy_pass http://10.10.10.20:80/;
    }
}
server {
    listen 80;
    server_name wp.example.com www.wp.example.com;
    location / {
        proxy_pass http://10.10.10.30:80/;
    }
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
}

Here is the nginx conf file from my KVM server who serves PHP website - WordPress:

server {
    listen       10.10.10.30:80;
    server_name  wp.example.com www.wp.example.com;
    root   /var/www;
    index index.php index.html index.htm;
    location / {
         try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
         root /usr/share/nginx/html;
    }
    location ~ \.php$ {
         try_files $uri =404;
         fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
}

How I can troubleshot why WordPress is loading too slow? There aren't packet drops between servers, also the connections are good I think... only 0.200 - 0.350 ms between servers...

I think the slowness occur because WordPress tries to make connections to external addresses but as the server where wordpress "lives" doesn't have public internet connection, this slows the things... I am not sure if this is the reason... but it could be?


来源: stackexchange

问:
Regular Expression matches in online testing but not in javascript execution?
My regular expression should find and remove all words beginning with foo-. The regular expression works in an online regex http://regexr.com/3gafi but fails in my javascript.

Whats going wrong?

$('[class*="foo-"]').each(function(index, ele) {

 ele.className = ele.className.replace(/(foo-.*( |\0))/g, '');
  console.log(ele.className);
});```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="foo-abc img-responsive"></div>

<div class="foo-abc"></div>

<div class=" foo-abc"></div>

<div class=" foo-abc "></div></code>```

答: The regular expression in your example does not work for the replacement. Try using the following - /(foo-[^ "]+ ?)/g

ContentLicense: CC BY-SA 3.0


问:
What is the difference between threading.active_count() and threading.activeCount() in Python?
Working with the Python library import threading I came across the threading.active_count() function which returns the number of active threads. But the threading.activeCount() function, which returns the exact same thing.

Even their help string are exactly the same.

So my question is, what is the difference?

If they are exactly the same, doesn't this go against PEP 20, The Zen of Python, where it says:

```<code>There should be one-- and preferably only one --obvious way to do it.
</code>```

Could this be a backward compatibility issue?

答:
Python basically cloned the Java API for the threading interface, and so it keeps two versions of names: those using Python's preferred underscores, and those in camelCase for backwards compatibility. The comments in the threading module say:

Note regarding PEP 8 compliant names. This threading model was originally inspired by Java, and inherited the convention of camelCase function and method names from that language. Those original names are not in any imminent danger of being deprecated (even for Py3k),so this module provides them as an alias for the PEP 8 compliant names. Note that using the new PEP 8 compliant names facilitates substitution with the multiprocessing module, which doesn't provide the old Java inspired names.

You can see they are just aliases for each other:

```<code>def active_count():
    """Return the number of Thread objects currently alive.
    The returned count is equal to the length of the list returned by
    enumerate().
    """
    with _active_limbo_lock:
        return len(_active) + len(_limbo)

activeCount = active_count
</code>```

At one commit, the CPython implementation actually removed the old functions, however, they reintroduced and temporarily deprecated them to avoid backwards compatibility issues. You should not use the camelCase functions for new code. Although they are not visibly deprecated right now, the camelCase functions are not explicitly documented and you should prefer the PEP8 versions.

Body:
Working with the Python library import threading I came across the threading.active_count() function which returns the number of active threads. But the threading.activeCount() function, which returns the exact same thing.

Even their help string are exactly the same.

So my question is, what is the difference?

If they are exactly the same, doesn't this go against PEP 20, The Zen of Python, where it says:

There should be one-- and preferably only one --obvious way to do it.

Could this be a backward compatibility issue?

Body:
Python basically cloned the Java API for the threading interface, and so it keeps two versions of names: those using Python's preferred underscores, and those in camelCase for backwards compatibility. The comments in the threading module say:

Note regarding PEP 8 compliant names. This threading model was originally inspired by Java, and inherited the convention of camelCase function and method names from that language. Those original names are not in any imminent danger of being deprecated (even for Py3k),so this module provides them as an alias for the PEP 8 compliant names. Note that using the new PEP 8 compliant names facilitates substitution with the multiprocessing module, which doesn't provide the old Java inspired names.

You can see they are just aliases for each other:

def active_count():
    """Return the number of Thread objects currently alive.
    The returned count is equal to the length of the list returned by
    enumerate().
    """
    with _active_limbo_lock:
        return len(_active) + len(_limbo)

activeCount = active_count

At one commit, the CPython implementation actually removed the old functions, however, they reintroduced and temporarily deprecated them to avoid backwards compatibility issues. You should not use the camelCase functions for new code. Although they are not visibly deprecated right now, the camelCase functions are not explicitly documented and you should prefer the PEP8 versions.


问:
How can I read a file packaged with an android app through a C++ library?
I have written a Unity application in C# that uses a C++ library built using Android NDK. I want to read some files that are packaged with the android application (in the assets folder) from this C++ library.

NDK does provide an API that allows files to be accessed. However, this API seems to require the Java AssetManager, and I'm not sure how I can access that from C#.

Is there some way I can do so? Alternatively, is there any other way I can go about reading these assets?

答: I believe that APK files are just ZIP files so you can open them like you would any zip file. C# has built in utilities for reading zip files.</p>


答:
(\bfoo-.*?(?= |$) works. You'll want to make sure it removes only words beginning with foo and nothing else. The lookahead helps with that.

$('[class*="foo-"]').each(function(index, ele) {

 ele.className = ele.className.replace(/(\bfoo-.*?(?= |$))/g, '');
  console.log(ele.className);
});```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="foo-abc img-responsive"></div>

<div class="foo-abc"></div>

<div class=" foo-abc"></div>

<div class=" foo-abc "></div></code>```

I assumed you want to retain img-responsive. If not, remove the lookahead, and convert the non-greedy match to a greedy one (with *).

Title: Regular Expression matches in online testing but not in javascript execution?

Body:
My regular expression should find and remove all words beginning with foo-. The regular expression works in an online regex http://regexr.com/3gafi but fails in my javascript.

Whats going wrong?

$('[class*="foo-"]').each(function(index, ele) {

 ele.className = ele.className.replace(/(foo-.*( |\0))/g, '');
  console.log(ele.className);
});

Body:
(\bfoo-.*?(?= |$) works. You'll want to make sure it removes only words beginning with foo and nothing else. The lookahead helps with that.

$('[class*="foo-"]').each(function(index, ele) {

 ele.className = ele.className.replace(/(\bfoo-.*?(?= |$))/g, '');
  console.log(ele.className);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="foo-abc img-responsive"></div>

<div class="foo-abc"></div>

<div class=" foo-abc"></div>

<div class=" foo-abc "></div>

I assumed you want to retain img-responsive. If not, remove the lookahead, and convert the non-greedy match to a greedy one (with *).


问:
Autocompletetextview with multiple variables
I have a JSON :

```<code>{"results":[
    {"id":"21","name":"Jonny"},
    {"id":"22","name":"Samuel"},
    {"id":"23","name":"Martha"}
    ]}
</code>```

I need to put "name" into an autocompletetextview.

I need to post "id" to other activity, based on selected name in autocompletetextview.

Please help, thanks in advance

答:
First convert json to hashmap:

```<code>HashMap<String, Integer> result = readJson("Your json string here");

public HashMap<String, Integer> readJson(String json) {
    try {
        JSONObject jsonObj = new JSONObject(json);

        // Getting JSON Array node
        JSONArray results = jsonObj.getJSONArray("results");
        final int j = results.length();
        HashMap<String, Integer> resultMap = new HashMap<>(j);
        for (int i = 0; i < j; i++) {
            JSONObject item = results.getJSONObject(i);
            resultMap.put(item.getString("name"), item.getInt("id"));
        }
        return resultMap;
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
}
</code>```

Now set autocomplete adapter like:

```<code>autoComplete.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_dropdown_item_1line, new ArrayList<>(result.keySet())));

public void startNextActivity(int id) {
    Intent intent = new Intent(getContext(), NextActivity.class);
    intent.putExtra("id", id);
    getContext().startActivity(intent);
}
</code>```

call the start activity like:

```<code>        startNextActivity(result.get(autoComplete.getText().toString()));
</code>```

Body:
I have a JSON :

{"results":[
    {"id":"21","name":"Jonny"},
    {"id":"22","name":"Samuel"},
    {"id":"23","name":"Martha"}
    ]}

I need to put "name" into an autocompletetextview.

I need to post "id" to other activity, based on selected name in autocompletetextview.

Please help, thanks in advance

Body:
First convert json to hashmap:

HashMap<String, Integer> result = readJson("Your json string here");

public HashMap<String, Integer> readJson(String json) {
    try {
        JSONObject jsonObj = new JSONObject(json);

        // Getting JSON Array node
        JSONArray results = jsonObj.getJSONArray("results");
        final int j = results.length();
        HashMap<String, Integer> resultMap = new HashMap<>(j);
        for (int i = 0; i < j; i++) {
            JSONObject item = results.getJSONObject(i);
            resultMap.put(item.getString("name"), item.getInt("id"));
        }
        return resultMap;
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
}

Now set autocomplete adapter like:

autoComplete.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_dropdown_item_1line, new ArrayList<>(result.keySet())));

public void startNextActivity(int id) {
    Intent intent = new Intent(getContext(), NextActivity.class);
    intent.putExtra("id", id);
    getContext().startActivity(intent);
}

call the start activity like:

        startNextActivity(result.get(autoComplete.getText().toString()));


问:
C#, variable value as a part of an object name?
For example i have a player object with a list of suits of cards it has.

```<code>player.card[0] = "diamonds";
player.card[1] = "clubs";
player.card[2] = "spades";
etc...
</code>```

also i have 4 hidden pictureboxes with an image of suites ( pb_spades , pb_hearts m etc. )

and another 4 pictureboxs ( pb_playerCard1 , pb_playerCard2 , etc. ) to which I have to assign an image from a hidden pb corresponding to the suit of card the player object has.

Therefor

```<code>if ( player.card[0] == "diamonds" ) { pb_playerCard1.Image = pb_diamonds.Image; }
</code>```

of course, doing it all with IFs would take quite a long time... Can I somehow use variable value as a part of an objects name?

kinda

```<code>for (int i = 1; i != 5; i++)
{
pb_playerCard+'i'.Image = pb_+'player.card[i+1]'.Image;
}
</code>```

答:
No, not how you're doing it, and although similar things can be achieved with reflection, what you want here is probably closer to just using FindControl for the "pb_playerCard" part and a simple dictionary of type Dictionary<CardSuitEnum, WhateverTheTypeOfImageIs> which you can populate with the pb_diamonds.Image etc.

Apols, I don't know what type .Image holds and can't be bothered to look it up :)

ContentLicense: CC BY-SA 2.5

Body:
For example i have a player object with a list of suits of cards it has.

player.card[0] = "diamonds";
player.card[1] = "clubs";
player.card[2] = "spades";
etc...

also i have 4 hidden pictureboxes with an image of suites ( pb_spades , pb_hearts m etc. )

and another 4 pictureboxs ( pb_playerCard1 , pb_playerCard2 , etc. ) to which I have to assign an image from a hidden pb corresponding to the suit of card the player object has.

Therefor

if ( player.card[0] == "diamonds" ) { pb_playerCard1.Image = pb_diamonds.Image; }

of course, doing it all with IFs would take quite a long time... Can I somehow use variable value as a part of an objects name?

kinda

for (int i = 1; i != 5; i++)
{
pb_playerCard+'i'.Image = pb_+'player.card[i+1]'.Image;
}


问:
A default document is not configured for the requested URL Web Api 2 / IIS express
I created a empty web api 2 project and created a default controller. When I try to go to my controller I get

```<code>HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.

Most likely causes:
A default document is not configured for the requested URL, and directory browsing is not enabled on the server.

Things you can try:
If you do not want to enable directory browsing, ensure that a default document is configured and that the file exists.
Enable directory browsing.
Go to the IIS Express install directory.
Run appcmd set config /section:system.webServer/directoryBrowse /enabled:true to enable directory browsing at the server level.
Run appcmd set config ["SITE_NAME"] /section:system.webServer/directoryBrowse /enabled:true to enable directory browsing at the site level.
Verify that the configuration/system.webServer/directoryBrowse@enabled attribute is set to true in the site or application configuration file.

Detailed Error Information:
Module     DirectoryListingModule
Notification       ExecuteRequestHandler
Handler    StaticFile
Error Code     0x00000000
Requested URL      http://localhost:49886/api/default/
Physical Path      C:\Users\Source\Repos\ospapi\OSP.Api\OSP.Api\
Logon Method       Anonymous
Logon User     Anonymous
Request Tracing Directory      C:\Users\Documents\IISExpress\TraceLogFiles\OSP.API

More Information:
This error occurs when a document is not specified in the URL, no default document is specified for the Web site or application, and directory listing is not enabled for the Web site or application. This setting may be disabled on purpose to secure the contents of the server.
View more information »
</code>```

My url: http://localhost:49886/api/default/

答: Make sure  that you have a controller with name default. Check the routes in WebApiConfig file</p>

Body:
I created a empty web api 2 project and created a default controller. When I try to go to my controller I get

HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.

Most likely causes:
A default document is not configured for the requested URL, and directory browsing is not enabled on the server.

Things you can try:
If you do not want to enable directory browsing, ensure that a default document is configured and that the file exists.
Enable directory browsing.
Go to the IIS Express install directory.
Run appcmd set config /section:system.webServer/directoryBrowse /enabled:true to enable directory browsing at the server level.
Run appcmd set config ["SITE_NAME"] /section:system.webServer/directoryBrowse /enabled:true to enable directory browsing at the site level.
Verify that the configuration/system.webServer/directoryBrowse@enabled attribute is set to true in the site or application configuration file.

Detailed Error Information:
Module     DirectoryListingModule
Notification       ExecuteRequestHandler
Handler    StaticFile
Error Code     0x00000000
Requested URL      http://localhost:49886/api/default/
Physical Path      C:\Users\Source\Repos\ospapi\OSP.Api\OSP.Api\
Logon Method       Anonymous
Logon User     Anonymous
Request Tracing Directory      C:\Users\Documents\IISExpress\TraceLogFiles\OSP.API

More Information:
This error occurs when a document is not specified in the URL, no default document is specified for the Web site or application, and directory listing is not enabled for the Web site or application. This setting may be disabled on purpose to secure the contents of the server.
View more information »

My url: http://localhost:49886/api/default/


问:
create-react-app + docker = QA and PROD Deploy
I'm using create-react-app for my projects using docker as my dev env.

Now I would like to know how is the best practice to deploy my project into AWS (I'll deploy the docker).

Maybe my question is a dummy but I'm really stuck on it.

My docker file has a command yarn start... for dev it is enough I don't need to build anything, my bundle will run in memory, but for QA or PROD I would like to build using npm run build but as I know it will create a new folder with the files that should be used on prod env.

That said, my question is: what is the best practice for this kind of situation?

Thanks.

答:
You would have to run npm run build and then copy the resulting files into your container. You could use a separate Dockerfile.build to build the files, extract them and add them to your final container. Your final container should be able to serve the files. You can base it on nginx or another server. You can also use it as a data volume container in your existing server container.

Recent versions of Docker make this process easier by allowing you to combine the two Dockerfiles. You can have a build container and then the final container both be defined in the same file.

Here's a simple example for your use case:

```<code>FROM node:onbuild AS builder
RUN npm run build

FROM nginx:latest
COPY --from=builder /usr/src/app/build /usr/share/nginx/html
</code>```

You'd probably want to include your own nginx configuration file.

More on multistage builds here:

https://docs.docker.com/engine/userguide/eng-image/multistage-build/

Body:
You would have to run npm run build and then copy the resulting files into your container. You could use a separate Dockerfile.build to build the files, extract them and add them to your final container. Your final container should be able to serve the files. You can base it on nginx or another server. You can also use it as a data volume container in your existing server container.

Recent versions of Docker make this process easier by allowing you to combine the two Dockerfiles. You can have a build container and then the final container both be defined in the same file.

Here's a simple example for your use case:

FROM node:onbuild AS builder
RUN npm run build

FROM nginx:latest
COPY --from=builder /usr/src/app/build /usr/share/nginx/html

You'd probably want to include your own nginx configuration file.

More on multistage builds here:

https://docs.docker.com/engine/userguide/eng-image/multistage-build/


问:
Creating dynamic array name in python numpy
There are already several similar questions on SO, for example How do I create a variable number of variables?. But I think my question is bit different one.

I want to create dynamic array names. MATLAB (and Octave) has built in data structure cell array that can do the job. For example code creates A{1} to A{10} variables dynamically. var1 is 'dynamic input' to program.

```<code>var1=10;
for j=1:1:var1
  A{j} = {}; %creating empty cell
end
%resizing empty cells using a loop, arrays can be of different size
</code>```

As answered in the first question, python has dictionary to do this task. However, I am working with huge amount of data stored in form of arrays, and need to use numpy array as they are faster than python built in lists. Any suggestions?

Update:
I want to create variable size arrays afterwards.

Body:
There are already several similar questions on SO, for example How do I create a variable number of variables?. But I think my question is bit different one.

I want to create dynamic array names. MATLAB (and Octave) has built in data structure cell array that can do the job. For example code creates A{1} to A{10} variables dynamically. var1 is 'dynamic input' to program.

var1=10;
for j=1:1:var1
  A{j} = {}; %creating empty cell
end
%resizing empty cells using a loop, arrays can be of different size

As answered in the first question, python has dictionary to do this task. However, I am working with huge amount of data stored in form of arrays, and need to use numpy array as they are faster than python built in lists. Any suggestions?

Update:
I want to create variable size arrays afterwards.

Body:
So your Octave code produces:

A =
{
  [1,1] =
  {
    [1,1] = [](0x0)
    [1,2] = [](0x0)
    [1,3] = [](0x0)
    [1,4] = [](0x0)
    [1,5] = [](0x0)
    [1,6] = [](0x0)
    [1,7] = [](0x0)
    [1,8] = [](0x0)
    [1,9] = [](0x0)
    [1,10] = [](0x0)
  }
  [1,2] =
  {
    [1,1] = [](0x0)
    [1,2] = [](0x0)
    [1,3] = [](0x0)
    [1,4] = [](0x0)
    [1,5] = [](0x0)
    [1,6] = [](0x0)
    [1,7] = [](0x0)
    [1,8] = [](0x0)
    [1,9] = [](0x0)
    [1,10] = [](0x0)
  }
  ...
  [1,10] =
  {
    [1,1] = [](0x0)
    [1,2] = [](0x0)
    ...
    [1,9] = [](0x0)
    [1,10] = [](0x0)
  }
}

So A is a cell that contains 10 cells, each with 10 0x0 matrices.

Cells are 2d (or higher) and contain any kind of object. python lists are 1d and contain any kind of object.  numpy arrays are more like matrices, except they can be 0 or 1d.  Another difference, MATLAB/Octave lets you 'grow' arrays and cells by just assigning to a higher index.  You grow lists with .append, and 'grow' arrays be concatenating several together to make a new one (no grow-in-place).

In [559]: A = []
In [560]: for i in range(10):
     ...:     A.append([[] for _ in range(10)])
     ...:
In [561]: A
Out[561]:
[[[], [], [], [], [], [], [], [], [], []],
 [[], [], [], [], [], [], [], [], [], []],
 ...
 [[], [], [], [], [], [], [], [], [], []],
 [[], [], [], [], [], [], [], [], [], []],
 [[], [], [], [], [], [], [], [], [], []]]

Or if you want a list of lists of (0,0) arrays:

In [562]: A =[]
In [563]: for i in range(10):
     ...:     A.append([np.zeros((0,0)) for _ in range(10)])

Or you could initial a multidimensional array:

In [565]: np.zeros((10,10,0,0),int)
Out[565]: array([], shape=(10, 10, 0, 0), dtype=int32)
In [566]: np.zeros((1,10,1,10,0,0),int)
Out[566]: array([], shape=(1, 10, 1, 10, 0, 0), dtype=int32)

Octave equivalent:

>> zeros(1,10,1,10,0,0)
ans = [](1x10x1x10x0x0)

numpyalso have object dtype lists which can contain 'anything'.

If I save the Octave A to a mat file, and load it in numpy I get:

In [569]: data = loadmat('test.mat')
In [570]: data
Out[570]:
{'A': array([[ array([[array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64)]], dtype=object),
         array([[array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         ...
         array([[array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64),
         array([], shape=(0, 0), dtype=float64)]], dtype=object)]], dtype=object),
 '__globals__': [],
 '__header__': b'MATLAB 5.0 MAT-file, written by Octave 4.0.0, 2017-07-08 02:13:25 UTC',
 '__version__': '1.0'}

A is an object dtype array containing object dtype arrays containing (0,0) float arrays:

In [572]: data['A'].shape
Out[572]: (1, 10)
In [573]: data['A'][0,0].shape
Out[573]: (1, 10)
In [574]: data['A'][0,0][0,0].shape
Out[574]: (0, 0)

Without the automatic growth behavior of MATLAB, it doesn't make much sense to initial an numpy array to shape (0,0).

Simpler example

A simple example of creating arrays of differing size - in a list:

In [590]: A = [np.arange(i+3) for i in range(5)]
In [591]: A
Out[591]:
[array([0, 1, 2]),
 array([0, 1, 2, 3]),
 array([0, 1, 2, 3, 4]),
 array([0, 1, 2, 3, 4, 5]),
 array([0, 1, 2, 3, 4, 5, 6])]

I can save and reload it:

In [600]: savemat('test.mat', {'A':A})
In [601]: loadmat('test.mat')
Out[601]:
{'A': array([[array([[0, 1, 2]]), array([[0, 1, 2, 3]]),
         array([[0, 1, 2, 3, 4]]), array([[0, 1, 2, 3, 4, 5]]),
         array([[0, 1, 2, 3, 4, 5, 6]])]], dtype=object),
 '__globals__': [],
 '__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Sun Jul  9 09:44:45 2017',
 '__version__': '1.0'}

Note that this has converted the list of arrays into an object array of arrays.

Octave loads this as:

>> A
A =
{
  [1,1] =
    0  1  2
  [1,2] =
    0  1  2  3
  [1,3] =
    0  1  2  3  4
  [1,4] =
    0  1  2  3  4  5
  [1,5] =
    0  1  2  3  4  5  6
}


问:
For jmeter post request, how can I generate input json from csv file?
I am trying to make a post rest call to my service. My sample input json file is,

{
  "$id": "1",
  "description": "sfdasd"
}

I have one csv file which contain a bunch of id and description, So is there a option where I can convert csv file to json objects and pass them to post call?

答:
Json is just text. Send as is with the variable id taken from csv:</p>

```<code> { "${id}": "1", "description": "sfdasd" }
</code>```

Body:
Json is just text. Send as is with the variable id taken from csv:

 { "${id}": "1", "description": "sfdasd" }


问:
How can I show file sizes with commas when getting a directory listing with 'ls -l'?
You can do 'ls -l' to get a detailed directory listing like this:

```<code>-rw-rw-rw-  1 alice themonkeys 1159995999 2008-08-20 07:01 foo.log
-rw-rw-rw-  1 bob   bob         244251992 2008-08-20 05:30 bar.txt
</code>```

But notice how you have to slide your finger along the screen to figure out the order of magnitude of those file sizes.

What's a good way to add commas to the file sizes in the directory listing, like this:

```<code>-rw-rw-rw-  1 alice themonkeys 1,159,995,999 2008-08-20 07:01 foo.log
-rw-rw-rw-  1 bob   bob          244,251,992 2008-08-20 05:30 bar.txt
</code>```

答:
Here's a perl script that will filter the output of  'ls -l'  to add the commas.
If you call the script commafy.pl then you can alias 'ls' to 'ls -l | commafy.pl'.

```<code>#!/usr/bin/perl -p
# pipe the output of ls -l through this to add commas to numbers.

s/(.{5} )(\d{4,}) /truncatePre($1,$2).commafy($2).' '/e;

# adds commas to an integer as appropriate
sub commafy
{
  my($num) = @_;
  my $len = length($num);
  if ($len <= 3) { return $num; }
  return commafy(substr($num, 0, $len - 3)) . ',' . substr($num, -3);
}

# removes as many chars from the end of str as there are commas to be added
#   to num
sub truncatePre
{
  my($str, $num) = @_;

  $numCommas = int((length($num)-1) / 3);

  return substr($str, 0, length($str) - $numCommas);
}
</code>```

OwnerDisplayName: dreeves

Body:
You can do 'ls -l' to get a detailed directory listing like this:

-rw-rw-rw-  1 alice themonkeys 1159995999 2008-08-20 07:01 foo.log
-rw-rw-rw-  1 bob   bob         244251992 2008-08-20 05:30 bar.txt

But notice how you have to slide your finger along the screen to figure out the order of magnitude of those file sizes.

What's a good way to add commas to the file sizes in the directory listing, like this:

-rw-rw-rw-  1 alice themonkeys 1,159,995,999 2008-08-20 07:01 foo.log
-rw-rw-rw-  1 bob   bob          244,251,992 2008-08-20 05:30 bar.txt

Body:
Here's a perl script that will filter the output of  'ls -l'  to add the commas.
If you call the script commafy.pl then you can alias 'ls' to 'ls -l | commafy.pl'.

#!/usr/bin/perl -p
# pipe the output of ls -l through this to add commas to numbers.

s/(.{5} )(\d{4,}) /truncatePre($1,$2).commafy($2).' '/e;

# adds commas to an integer as appropriate
sub commafy
{
  my($num) = @_;
  my $len = length($num);
  if ($len <= 3) { return $num; }
  return commafy(substr($num, 0, $len - 3)) . ',' . substr($num, -3);
}

# removes as many chars from the end of str as there are commas to be added
#   to num
sub truncatePre
{
  my($str, $num) = @_;

  $numCommas = int((length($num)-1) / 3);

  return substr($str, 0, length($str) - $numCommas);
}


问:
How can I run a custom task (different from test or build) from a keychord shortcut in VSCode?
I know I can assign a keychord to 2 standard tasks: build and test in VSCode. I need to do the same with a custom task. How can I do it?

答:
Use runTask with args.

```<code>{
    "key": "shift+cmd+t",
    "command": "workbench.action.tasks.runTask",
    "args": "The name of your task"
}
</code>```

ContentLicense: CC BY-SA 4.0

Body:
Use runTask with args.

{
    "key": "shift+cmd+t",
    "command": "workbench.action.tasks.runTask",
    "args": "The name of your task"
}


问:
Set id in dynamic edittext and textview
I have some troubles trying to put an id to a dynamic edittext and texview in Android Studio, i want to use this id to get the id's in another functions.
Note: Im not setting any id in the onCreate function.

This is my code:

```<code>            for (Map.Entry<String,String> entry : getMap(newContact).entrySet()) {
            total++;
            TextView ProgrammaticallyTextView = new TextView(this.getActivity());
            EditText ProgrammaticallyEditText = new EditText(this.getActivity());
            ProgrammaticallyTextView.setId(total);
            ProgrammaticallyEditText.setId(total+1);
            ProgrammaticallyTextView.setText(entry.getKey());
            ProgrammaticallyEditText.setText(entry.getValue());

            linearLayout.addView(ProgrammaticallyTextView);
            linearLayout.addView(ProgrammaticallyEditText);

            total++;
        }
</code>```

This is the function that i use the edittext and textview id's

```<code>    public void onClick(View v) {
    switch (v.getId()) {

        case R.id.btn_create:

            String test = "";

            for(int i=0; i < (this.total); i+=2){
                TextView tv = (TextView) v.findViewById(i++);
                EditText et = (EditText) v.findViewById(i+2);
                if ((i+2) >= this.total){
                    test += tv.getText()+"="+et.getText();
                }else
                {
                    test += tv.getText()+"="+et.getText()+",";
                }

            }

            mContact = getMap(test);
            newContactRequest();
            break;

    }
}
</code>```

I appreciate any help!

答:
You need to explicitly change the data type to string. Just writing i++ tries to assign that value as an integer.
I think if you change your lines to these it should work.</p>

```<code>TextView tv = (TextView) v.findViewById(String.valueOf(i++));
EditText et = (EditText) v.findViewById(String.valueOf(i+2));
</code>```

Title: Set id in dynamic edittext and textview

Body:
I have some troubles trying to put an id to a dynamic edittext and texview in Android Studio, i want to use this id to get the id's in another functions.
Note: Im not setting any id in the onCreate function.

This is my code:

            for (Map.Entry<String,String> entry : getMap(newContact).entrySet()) {
            total++;
            TextView ProgrammaticallyTextView = new TextView(this.getActivity());
            EditText ProgrammaticallyEditText = new EditText(this.getActivity());
            ProgrammaticallyTextView.setId(total);
            ProgrammaticallyEditText.setId(total+1);
            ProgrammaticallyTextView.setText(entry.getKey());
            ProgrammaticallyEditText.setText(entry.getValue());

            linearLayout.addView(ProgrammaticallyTextView);
            linearLayout.addView(ProgrammaticallyEditText);

            total++;
        }

This is the function that i use the edittext and textview id's

    public void onClick(View v) {
    switch (v.getId()) {

        case R.id.btn_create:

            String test = "";

            for(int i=0; i < (this.total); i+=2){
                TextView tv = (TextView) v.findViewById(i++);
                EditText et = (EditText) v.findViewById(i+2);
                if ((i+2) >= this.total){
                    test += tv.getText()+"="+et.getText();
                }else
                {
                    test += tv.getText()+"="+et.getText()+",";
                }

            }

            mContact = getMap(test);
            newContactRequest();
            break;

    }
}

I appreciate any help!

Body:
You need to explicitly change the data type to string. Just writing i++ tries to assign that value as an integer.
I think if you change your lines to these it should work.

TextView tv = (TextView) v.findViewById(String.valueOf(i++));
EditText et = (EditText) v.findViewById(String.valueOf(i+2));


问:
Jenkins Changelog
I am trying to get the change-log of GitHub changes through a mail, when build happens through Jenkins.

I have the below code, which is giving me the commit message but not the author information. How would I get the author information for the commits? Am I missing anything in "getChangeString Function" ?

```<code>@NonCPS
def getChangeString() {
  MAX_MSG_LEN = 100
  def changeString = ""
  echo "Gathering SCM changes"
  def changeLogSets = currentBuild.changeSets
  for (int i = 0; i < changeLogSets.size(); i++) {
    def entries = changeLogSets[i].items
    for (int j = 0; j < entries.length; j++) {
      def entry = entries[j]
      truncated_msg = entry.msg.take(MAX_MSG_LEN)
      echo "******${entry.author}**********"
      changeString += " <tr><td> ${truncated_msg} </td><td> [${entry.author}]
      </td></tr>\n"
    }
  }

  if (!changeString) {
    changeString = " <tr><td> No changes </td><td> No changes </td></tr>"
  }
  return changeString
 }

def sendEmail() {
  String Changelog=getChangeString()
  body 1, body 2, body 3 = some of my personal text
  def body=body1+getChangeString()+body2+build_url+body3
  mail(to:"xxxx@gmail.com",
  subject:"${env.JOB_NAME}(${env.BUILD_NUMBER}) completed",
  mimeType:'text/html',
  body:"${body}")
}

Stage('Notification'){
  echo "***** Send Email Notification *****"
  sendEmail()
}
</code>```

Please let me know, thanks!!!

答:
this complete Jenkinsfile prints the author full name for me. there weren't any author-related changes, i don't think. i just had to tweak a couple things to get it to run.

```<code>@NonCPS
def getChangeString() {
  MAX_MSG_LEN = 100
  def changeString = ""
  echo "Gathering SCM changes"
  def changeLogSets = currentBuild.changeSets
  for (int i = 0; i < changeLogSets.size(); i++) {
    def entries = changeLogSets[i].items
    for (int j = 0; j < entries.length; j++) {
      def entry = entries[j]
      truncated_msg = entry.msg.take(MAX_MSG_LEN)
      echo "******${entry.author}**********"
      changeString += " <tr><td> ${truncated_msg} </td><td> [${entry.author}]</td></tr>\n"
    }
  }

  if (!changeString) {
    changeString = " <tr><td> No changes </td><td> No changes </td></tr>"
  }
  return changeString
 }

def sendEmail() {
  def body=getChangeString()
  mail(to:"hey@example.com",
  subject:"${env.JOB_NAME}(${env.BUILD_NUMBER}) completed",
  mimeType:'text/html',
  body:"${body}")
}

pipeline {
  agent { label 'docker' }
  stages {
    stage('notification') {
      steps {
        echo "***** Send Email Notification *****"
        sendEmail()
      }
    }
  }
}
</code>```

so...

```<code><joke>if it still doesn't print the author for you, i think the most likely
explanation is that someone hacked your git hosting tool and replaced it with a
double with perfect fidelity except that it doesn't capture the author</joke>
</code>```

Title: Jenkins Changelog

Body:
I am trying to get the change-log of GitHub changes through a mail, when build happens through Jenkins.

I have the below code, which is giving me the commit message but not the author information. How would I get the author information for the commits? Am I missing anything in "getChangeString Function" ?

@NonCPS
def getChangeString() {
  MAX_MSG_LEN = 100
  def changeString = ""
  echo "Gathering SCM changes"
  def changeLogSets = currentBuild.changeSets
  for (int i = 0; i < changeLogSets.size(); i++) {
    def entries = changeLogSets[i].items
    for (int j = 0; j < entries.length; j++) {
      def entry = entries[j]
      truncated_msg = entry.msg.take(MAX_MSG_LEN)
      echo "******${entry.author}**********"
      changeString += "
 ${truncated_msg} 	 [${entry.author}]

\n"
    }
  }

  if (!changeString) {
    changeString = "
 No changes 	 No changes
"
  }
  return changeString
 }

def sendEmail() {
  String Changelog=getChangeString()
  body 1, body 2, body 3 = some of my personal text
  def body=body1+getChangeString()+body2+build_url+body3
  mail(to:"xxxx@gmail.com",
  subject:"${env.JOB_NAME}(${env.BUILD_NUMBER}) completed",
  mimeType:'text/html',
  body:"${body}")
}

Stage('Notification'){
  echo "***** Send Email Notification *****"
  sendEmail()
}

Please let me know, thanks!!!

Body:
this complete Jenkinsfile prints the author full name for me. there weren't any author-related changes, i don't think. i just had to tweak a couple things to get it to run.

@NonCPS
def getChangeString() {
  MAX_MSG_LEN = 100
  def changeString = ""
  echo "Gathering SCM changes"
  def changeLogSets = currentBuild.changeSets
  for (int i = 0; i < changeLogSets.size(); i++) {
    def entries = changeLogSets[i].items
    for (int j = 0; j < entries.length; j++) {
      def entry = entries[j]
      truncated_msg = entry.msg.take(MAX_MSG_LEN)
      echo "******${entry.author}**********"
      changeString += " <tr><td> ${truncated_msg} </td><td> [${entry.author}]</td></tr>\n"
    }
  }

  if (!changeString) {
    changeString = " <tr><td> No changes </td><td> No changes </td></tr>"
  }
  return changeString
 }

def sendEmail() {
  def body=getChangeString()
  mail(to:"hey@example.com",
  subject:"${env.JOB_NAME}(${env.BUILD_NUMBER}) completed",
  mimeType:'text/html',
  body:"${body}")
}

pipeline {
  agent { label 'docker' }
  stages {
    stage('notification') {
      steps {
        echo "***** Send Email Notification *****"
        sendEmail()
      }
    }
  }
}

so...

<joke>if it still doesn't print the author for you, i think the most likely
explanation is that someone hacked your git hosting tool and replaced it with a
double with perfect fidelity except that it doesn't capture the author</joke>


问:
c++ socket - sendto - no data recieved
Visual Studio 2017 Community

c++ CLR project

It should be sending a UDP datagram to a multicast address.

I am sending without errors.

```<code>--------------
MULTICAST
-------------------
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\mswsock.dll'. Symbols loaded.

MULTICAST ----
IP:224.0.0.2
PORT:7125
-------------

Sent: 13

Sent: 13

Sent: 13
</code>```

The receiver gets this input

```<code> {"socketId":0,"data":{},"remoteAddress":"192.168.0.80","remotePort":2535}
</code>```

Here is the sending part of the code

```<code>const char *msg = "1 2 3 4 5 6 7";

while (sending) {

    //swprintf(str, L"\nMSG: \n%c ", channels);
    //OutputDebugString(str);

    // Send a message to the multicasting address.
    int ret = sendto(Sock, msg, strlen(msg), 0, (struct sockaddr FAR *) &dest_sin, sizeof(dest_sin));

    swprintf(str, L"\nSent: %d \n\n", ret);
    OutputDebugString(str);

    if (ret == SOCKET_ERROR)
    {
        swprintf(str, L"\nsendto failed! Error: %d ", WSAGetLastError());
        OutputDebugString(str);

        closesocket(Sock);
        sending = false;
    }
    Sleep(66);
}
</code>```

Full socket code if it helps

```<code>// Sent message string
TCHAR szError[100];               // Error message string
SOCKET Sock = INVALID_SOCKET;     // Datagram window socket

SOCKADDR_IN source_sin,           // Source socket address
    dest_sin;             // Destination socket address

WSADATA WSAData;                  // Contains details of the
                                  // Winsock implementation

OutputDebugString(L"\n--------------\nMULTICAST\n-------------------\n");

// Initialize Winsock.
if (WSAStartup(MAKEWORD(1, 1), &WSAData) != 0)
{
    swprintf(str, TEXT("WSAStartup failed! Error: %d"), WSAGetLastError());
    OutputDebugString(str);
    return FALSE;
}

// Create a datagram window socket, Sock.
if ((Sock = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
    swprintf(str, TEXT("Allocating socket failed! Error: %d"), WSAGetLastError());
    OutputDebugString(str);
    return FALSE;
}

// Fill out source socket's address information.
source_sin.sin_family = AF_INET;
source_sin.sin_port = htons(SOURCE_PORT);
source_sin.sin_addr.s_addr = htonl(INADDR_ANY);

// Associate the source socket's address with the socket, Sock.
if (bind(Sock,
    (struct sockaddr FAR *) &source_sin,
    sizeof(source_sin)) == SOCKET_ERROR)
{
    swprintf(str, TEXT("Binding socket failed! Error: %d"), WSAGetLastError());
    OutputDebugString(str);
    closesocket(Sock);
    return FALSE;
}

// Set the Time-to-Live of the multicast.
int set_sock = setsockopt(Sock, SOL_SOCKET, SO_BROADCAST, (char FAR *)&iOptVal, sizeof(int));
if (set_sock == SOCKET_ERROR)
{
    swprintf(str, TEXT("\n`setsockopt` failed! Error: %d\n\n"), WSAGetLastError());
    OutputDebugString(str);
    closesocket(Sock);
    return FALSE;
}

// Fill out the desination socket's address information.
dest_sin.sin_family = AF_INET;
dest_sin.sin_port = ntohs(mcast_port);
dest_sin.sin_addr.s_addr = inet_addr(mcast_ip);

swprintf(str, L"\nMULTICAST ----\nIP:%s\nPORT:%d\n-------------\n", mcast_ip, mcast_port);
OutputDebugString(str);

const char *msg = "1 2 3 4 5 6 7";

while (sending) {

    //swprintf(str, L"\nMSG: \n%c ", channels);
    //OutputDebugString(str);

    // Send a message to the multicasting address.
    int ret = sendto(Sock, msg, strlen(msg), 0, (struct sockaddr FAR *) &dest_sin, sizeof(dest_sin));

    swprintf(str, L"\nSent: %d \n\n", ret);
    OutputDebugString(str);

    if (ret == SOCKET_ERROR)
    {
        swprintf(str, L"\nsendto failed! Error: %d ", WSAGetLastError());
        OutputDebugString(str);

        closesocket(Sock);
        sending = false;
    }
    Sleep(66);
}

OutputDebugString(L"\n---------------------\nMULTICAST DONE\n---------------------\n");

if (!sending) {
    // Disable sending on Sock before closing it.
    shutdown(Sock, 0x01);

    // Close Sock.
    closesocket(Sock);

    WSACleanup();
}
</code>```

From what I have read.

const char * msg

should be correct.?

Any advise is appreciated.

Edit:

As per Remy Lebeau

int set_sock = setsockopt(Sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR *)&iOptVal, sizeof(int));

Error: 10042 -- An unknown, invalid or unsupported option or level was specified in a getsockopt or setsockopt call.

IP_ADD_MEMBERSHIP = 5

答:
Subnet Broadcasting is NOT the same thing as Multicasting!

You have implemented code for broadcasting, but you are using a multicast IP address as the broadcast target, which will not work.

Don't use setsockopt(SOL_SOCKET, SO_BROADCAST) at all in this situation.  Instead, you have to join the multicast group using setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP) before you can then send/receive datagrams to/from the group.

Read MSDN for more details:

Multicast Programming

Title: c++ socket - sendto - no data recieved

Body:
Visual Studio 2017 Community

c++ CLR project

It should be sending a UDP datagram to a multicast address.

I am sending without errors.

--------------
MULTICAST
-------------------
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\mswsock.dll'. Symbols loaded.

MULTICAST ----
IP:224.0.0.2
PORT:7125
-------------

Sent: 13

Sent: 13

Sent: 13

The receiver gets this input

 {"socketId":0,"data":{},"remoteAddress":"192.168.0.80","remotePort":2535}

Here is the sending part of the code

const char *msg = "1 2 3 4 5 6 7";

while (sending) {

    //swprintf(str, L"\nMSG: \n%c ", channels);
    //OutputDebugString(str);

    // Send a message to the multicasting address.
    int ret = sendto(Sock, msg, strlen(msg), 0, (struct sockaddr FAR *) &dest_sin, sizeof(dest_sin));

    swprintf(str, L"\nSent: %d \n\n", ret);
    OutputDebugString(str);

    if (ret == SOCKET_ERROR)
    {
        swprintf(str, L"\nsendto failed! Error: %d ", WSAGetLastError());
        OutputDebugString(str);

        closesocket(Sock);
        sending = false;
    }
    Sleep(66);
}

Full socket code if it helps

// Sent message string
TCHAR szError[100];               // Error message string
SOCKET Sock = INVALID_SOCKET;     // Datagram window socket

SOCKADDR_IN source_sin,           // Source socket address
    dest_sin;             // Destination socket address

WSADATA WSAData;                  // Contains details of the
                                  // Winsock implementation

OutputDebugString(L"\n--------------\nMULTICAST\n-------------------\n");

// Initialize Winsock.
if (WSAStartup(MAKEWORD(1, 1), &WSAData) != 0)
{
    swprintf(str, TEXT("WSAStartup failed! Error: %d"), WSAGetLastError());
    OutputDebugString(str);
    return FALSE;
}

// Create a datagram window socket, Sock.
if ((Sock = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
    swprintf(str, TEXT("Allocating socket failed! Error: %d"), WSAGetLastError());
    OutputDebugString(str);
    return FALSE;
}

// Fill out source socket's address information.
source_sin.sin_family = AF_INET;
source_sin.sin_port = htons(SOURCE_PORT);
source_sin.sin_addr.s_addr = htonl(INADDR_ANY);

// Associate the source socket's address with the socket, Sock.
if (bind(Sock,
    (struct sockaddr FAR *) &source_sin,
    sizeof(source_sin)) == SOCKET_ERROR)
{
    swprintf(str, TEXT("Binding socket failed! Error: %d"), WSAGetLastError());
    OutputDebugString(str);
    closesocket(Sock);
    return FALSE;
}

// Set the Time-to-Live of the multicast.
int set_sock = setsockopt(Sock, SOL_SOCKET, SO_BROADCAST, (char FAR *)&iOptVal, sizeof(int));
if (set_sock == SOCKET_ERROR)
{
    swprintf(str, TEXT("\n`setsockopt` failed! Error: %d\n\n"), WSAGetLastError());
    OutputDebugString(str);
    closesocket(Sock);
    return FALSE;
}

// Fill out the desination socket's address information.
dest_sin.sin_family = AF_INET;
dest_sin.sin_port = ntohs(mcast_port);
dest_sin.sin_addr.s_addr = inet_addr(mcast_ip);

swprintf(str, L"\nMULTICAST ----\nIP:%s\nPORT:%d\n-------------\n", mcast_ip, mcast_port);
OutputDebugString(str);

const char *msg = "1 2 3 4 5 6 7";

while (sending) {

    //swprintf(str, L"\nMSG: \n%c ", channels);
    //OutputDebugString(str);

    // Send a message to the multicasting address.
    int ret = sendto(Sock, msg, strlen(msg), 0, (struct sockaddr FAR *) &dest_sin, sizeof(dest_sin));

    swprintf(str, L"\nSent: %d \n\n", ret);
    OutputDebugString(str);

    if (ret == SOCKET_ERROR)
    {
        swprintf(str, L"\nsendto failed! Error: %d ", WSAGetLastError());
        OutputDebugString(str);

        closesocket(Sock);
        sending = false;
    }
    Sleep(66);
}

OutputDebugString(L"\n---------------------\nMULTICAST DONE\n---------------------\n");

if (!sending) {
    // Disable sending on Sock before closing it.
    shutdown(Sock, 0x01);

    // Close Sock.
    closesocket(Sock);

    WSACleanup();
}

From what I have read.

const char * msg

should be correct.?

Any advise is appreciated.

Edit:

As per Remy Lebeau

int set_sock = setsockopt(Sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR *)&iOptVal, sizeof(int));

Error: 10042 -- An unknown, invalid or unsupported option or level was specified in a getsockopt or setsockopt call.

IP_ADD_MEMBERSHIP = 5


问:
Facing issues when writing multiple rows to a csv file in python 3.6
My requirement is to read all the files in the directory and get the length of the file, and word count of each file and store it as a row in a CSV file.

Following is my Python code.
      #Iterating through the list directory

```<code> for filename in os.listdir(passedArgument1):
  #Opening each file in the directory
   with open(passedArgument1+filename,"r",encoding="ISO-8859-1") as f:
    actualWordCount = 0
  # Getting the file Size
    fileSize=os.path.getsize(passedArgument1+filename)
    for line in f:
  # Getting the word count
      actualWordCount+=writeToExcelFile(line)
  # Writing to a file
      with open('output.csv', 'w') as csvfile:
       spamwriter = csv.writer(csvfile, delimiter=' ', quotechar='|',   quoting=csv.QUOTE_MINIMAL)
       spamwriter.writerow([actualWordCount,fileSize])
</code>```

When I am writing to the CSV file, it writes only one record that is the last file. I have checked writerows method but not sure how it works.Any help is appreciated on achieving my requirement in Python 3.x.

答: You are re-opening the output file for every single line of output.  Just open it once.</p>

Body:
My requirement is to read all the files in the directory and get the length of the file, and word count of each file and store it as a row in a CSV file.

Following is my Python code.
      #Iterating through the list directory

 for filename in os.listdir(passedArgument1):
  #Opening each file in the directory
   with open(passedArgument1+filename,"r",encoding="ISO-8859-1") as f:
    actualWordCount = 0
  # Getting the file Size
    fileSize=os.path.getsize(passedArgument1+filename)
    for line in f:
  # Getting the word count
      actualWordCount+=writeToExcelFile(line)
  # Writing to a file
      with open('output.csv', 'w') as csvfile:
       spamwriter = csv.writer(csvfile, delimiter=' ', quotechar='|',   quoting=csv.QUOTE_MINIMAL)
       spamwriter.writerow([actualWordCount,fileSize])

When I am writing to the CSV file, it writes only one record that is the last file. I have checked writerows method but not sure how it works.Any help is appreciated on achieving my requirement in Python 3.x.


问:
return stops a function and returns a value. But why use it over a variable?
return stops a function and returns a value.  For example this simple function returns 2 words.

```<code>function returnValue(name) {
    return "Hello " + name;
}

document.getElementById("box-1").innerHTML = returnValue("Freddy");
</code>```

However I can accomplish the same thing with a variable.  For example this function creates the same output without return.

```<code>var testVar;

function returnValue(name) {
    testVar = "Hello " + name;
}

returnValue("Freddy");
document.getElementById("box-1").innerHTML = testVar;
</code>```

As a noob, at least on the surface I don't see a significant benefit to return compared to a variable.  Am I missing something that is uniquely good about return?  Or alternately do most people simply use variables?

答:
Return statements tell the processor to return to the code that called the function. It also has the capability to report values. Using a reporter variable does not cause the function to cease.

Look below:

```<code>function returnValue(name) {
    if (name == "Tim") return "Bye " + name;
    return "Hello " + name;
}
</code>```

This function stops execution if the name is Tim. With reporter variables"

```<code>function returnValue(name) {
        if (name == "Tim") testVar = "Bye " + name;
        testVar = "Hello " + name;
}
</code>```

Even if the name is Tim, execution continues and the value is incorrectly reported as Tim. The variable also takes up unneccesary memory.

Body:
return stops a function and returns a value.  For example this simple function returns 2 words.

function returnValue(name) {
    return "Hello " + name;
}

document.getElementById("box-1").innerHTML = returnValue("Freddy");

However I can accomplish the same thing with a variable.  For example this function creates the same output without return.

var testVar;

function returnValue(name) {
    testVar = "Hello " + name;
}

returnValue("Freddy");
document.getElementById("box-1").innerHTML = testVar;

As a noob, at least on the surface I don't see a significant benefit to return compared to a variable.  Am I missing something that is uniquely good about return?  Or alternately do most people simply use variables?

Body:
Return statements tell the processor to return to the code that called the function. It also has the capability to report values. Using a reporter variable does not cause the function to cease.

Look below:

function returnValue(name) {
    if (name == "Tim") return "Bye " + name;
    return "Hello " + name;
}

This function stops execution if the name is Tim. With reporter variables"

function returnValue(name) {
        if (name == "Tim") testVar = "Bye " + name;
        testVar = "Hello " + name;
}

Even if the name is Tim, execution continues and the value is incorrectly reported as Tim. The variable also takes up unneccesary memory.


问:
In Java should I copy a volatile reference locally before I foreach it
If I have the following

```<code>private volatile Collection<Integer> ints;

private void myMethod()
{
   for ( Integer i : ints )
   {
      ...
   }
}
</code>```

The ints collection is never changed but the entire collection maybe replaced by another thread (so it's an immutable collection).

Should I be copying the ints variable locally before I iterate it?  I'm not sure if it will be accessed multiple times.  ie Iterating the collection, another thread replaces the collection, the code continues iterating but with the new collection.

EDIT : This question is relevant for additional info on how foreach works internally.

答: You don't have to. Implicitly, the code will do an ints.iterator() anyway, and from that point on only use that iterator, on the old collection.

Body:
If I have the following

private volatile Collection<Integer> ints;

private void myMethod()
{
   for ( Integer i : ints )
   {
      ...
   }
}

The ints collection is never changed but the entire collection maybe replaced by another thread (so it's an immutable collection).

Should I be copying the ints variable locally before I iterate it?  I'm not sure if it will be accessed multiple times.  ie Iterating the collection, another thread replaces the collection, the code continues iterating but with the new collection.

EDIT : This question is relevant for additional info on how foreach works internally.


问:
Unpaired but not paired ttest loop in R working
I have a loop that goes through a dataframe, runs ttests and stores the resulting p-value of each ttest in another dataframe.

Here is the loop where 'mydata' is the dataframe that the ttests are being run on. 'mydata' is a dataframe with 4 columns:

```<code>df <- mydata
mydf <- data.frame(c(1:4))

# this is the new dataframe being initialized to store my p-values

row.names(mydf) <- names(df)
for(i in names(df)){
  if(sd(df[[i]]) == 0) {

  # this prevents the loop from terminating and returning an error when ttests
  # are run on columns with binary values

  } else {
    ttest <- t.test(df[df$Pre==1,][[i]], df[df$Pre==2,][[i]], paired=FALSE)

    # 'Pre' is the column that groups my data into
    # distinct cohorts. I am comparing the Pre cohort versus the Post cohort
    # in these ttests.

    mydf[i,1] <- ttest$p.value
  }
}
mydf
</code>```

Here is my output of mydf for an unpaired (paired=FALSE) ttest:

```<code>          c.1.4.
density 0.3569670
clust   0.9715987
Pre     3.0000000
HC      4.0000000
</code>```

However, when I change paired=FALSE to paired=TRUE (to run a paired ttest), here is mydf:

```<code>           c.1.4.
density      1
clust        2
Pre          3
HC           4
</code>```

I checked this line of my loop in isolation using the first column of my dataframe, '1' in double brackets,(for paired=TRUE) and it does appear to be outputting a p-value:

```<code>ttest <- t.test(df[df$Pre==1,][[1]], df[df$Pre==2,][[1]], paired=TRUE)
ttest$p.value
[1] 0.356967
</code>```

Below is a sample dataset that you can use to reproduce the error:

```<code>            density      clust      Pre HC
RDHC008A_13 0.47991     0.676825    1   1
RDHC009A_13 0.49955     0.696441    1   1
RDHC010A_16 0.491454    0.706507    1   1
RDHC013A_13 0.442879    0.689118    1   1
RDHC014A_13 0.453823    0.691603    1   1
RDHC016A_16 0.481259    0.706978    1   1
RDHC019A_06 0.515442    0.699514    1   1
RDHC021A_15 0.449925    0.685202    1   1
RDHC022A_12 0.461319    0.705446    1   1
RDHC023A_11 0.468816    0.667698    1   1
RDHC024A_12 0.515142    0.719474    1   1
RDHC025A_13 0.496702    0.710877    1   1
RDHC026A_12 0.477061    0.695061    1   1
RDHC027A_12 0.515442    0.722269    1   1
RDHC029A_12 0.406747    0.669998    1   1
RDHC030A_12 0.476162    0.69219     1   1
RDHC032B_13 0.50075     0.685474    1   1
RDHC034B_07 0.525487    0.725558    1   1
RDHC036B_07 0.468816    0.698904    1   1
RDHC038B_07 0.470015    0.706668    1   1
RDHC039B_07 0.511544    0.712818    1   1
RDHC041A_14 0.551574    0.732983    1   1
RDHC004C_12 0.486207    0.695121    2   1
RDHC005C_12 0.505997    0.695598    2   1
RDHC006C_13 0.487406    0.697044    2   1
RDHC013C_12 0.41979     0.685518    2   1
RDHC015C_13 0.297751    0.69632     2   1
RDHC016C_16 0.463718    0.700011    2   1
RDHC019C_14 0.508096    0.690071    2   1
RDHC021C_12 0.448426    0.688265    2   1
RDHC022C_12 0.468816    0.700968    2   1
RDHC024C_12 0.515292    0.70664     2   1
RDHC025C_13 0.473163    0.704231    2   1
RDHC027C_12 0.518741    0.732939    2   1
RDHC030C_11 0.489205    0.708174    2   1
</code>```

You can import it by doing the following:

copy the data and paste it within the quotation marks of the code below into R:

```<code>zz <- ""
</code>```

now, assign the data to a data.frame:

```<code>mydata <- read.table(text=zz, header=TRUE)
</code>```

I have no idea why changing the 'paired' parameter to TRUE would cause this to happen. Any help/advice would be much appreciated. Thanks - Paul

答:
You initialize the mydf data.frame with the values 1:4 here

```<code>mydf <- data.frame(c(1:4))
</code>```

basically the loop does nothing because t.test is throwing an error when you do PAIRED=TRUE because your two sets of values aren't the same length (and they need to be when doing a paired t-test. You have 22 values where Pre==1 and 13 values where Pre==2. You can't do a paired test with an imbalance like that.

Body:
I have a loop that goes through a dataframe, runs ttests and stores the resulting p-value of each ttest in another dataframe.

Here is the loop where 'mydata' is the dataframe that the ttests are being run on. 'mydata' is a dataframe with 4 columns:

df <- mydata
mydf <- data.frame(c(1:4))

# this is the new dataframe being initialized to store my p-values

row.names(mydf) <- names(df)
for(i in names(df)){
  if(sd(df[[i]]) == 0) {

  # this prevents the loop from terminating and returning an error when ttests
  # are run on columns with binary values

  } else {
    ttest <- t.test(df[df$Pre==1,][[i]], df[df$Pre==2,][[i]], paired=FALSE)

    # 'Pre' is the column that groups my data into
    # distinct cohorts. I am comparing the Pre cohort versus the Post cohort
    # in these ttests.

    mydf[i,1] <- ttest$p.value
  }
}
mydf

Here is my output of mydf for an unpaired (paired=FALSE) ttest:

          c.1.4.
density 0.3569670
clust   0.9715987
Pre     3.0000000
HC      4.0000000

However, when I change paired=FALSE to paired=TRUE (to run a paired ttest), here is mydf:

           c.1.4.
density      1
clust        2
Pre          3
HC           4

I checked this line of my loop in isolation using the first column of my dataframe, '1' in double brackets,(for paired=TRUE) and it does appear to be outputting a p-value:

ttest <- t.test(df[df$Pre==1,][[1]], df[df$Pre==2,][[1]], paired=TRUE)
ttest$p.value
[1] 0.356967

Below is a sample dataset that you can use to reproduce the error:

            density      clust      Pre HC
RDHC008A_13 0.47991     0.676825    1   1
RDHC009A_13 0.49955     0.696441    1   1
RDHC010A_16 0.491454    0.706507    1   1
RDHC013A_13 0.442879    0.689118    1   1
RDHC014A_13 0.453823    0.691603    1   1
RDHC016A_16 0.481259    0.706978    1   1
RDHC019A_06 0.515442    0.699514    1   1
RDHC021A_15 0.449925    0.685202    1   1
RDHC022A_12 0.461319    0.705446    1   1
RDHC023A_11 0.468816    0.667698    1   1
RDHC024A_12 0.515142    0.719474    1   1
RDHC025A_13 0.496702    0.710877    1   1
RDHC026A_12 0.477061    0.695061    1   1
RDHC027A_12 0.515442    0.722269    1   1
RDHC029A_12 0.406747    0.669998    1   1
RDHC030A_12 0.476162    0.69219     1   1
RDHC032B_13 0.50075     0.685474    1   1
RDHC034B_07 0.525487    0.725558    1   1
RDHC036B_07 0.468816    0.698904    1   1
RDHC038B_07 0.470015    0.706668    1   1
RDHC039B_07 0.511544    0.712818    1   1
RDHC041A_14 0.551574    0.732983    1   1
RDHC004C_12 0.486207    0.695121    2   1
RDHC005C_12 0.505997    0.695598    2   1
RDHC006C_13 0.487406    0.697044    2   1
RDHC013C_12 0.41979     0.685518    2   1
RDHC015C_13 0.297751    0.69632     2   1
RDHC016C_16 0.463718    0.700011    2   1
RDHC019C_14 0.508096    0.690071    2   1
RDHC021C_12 0.448426    0.688265    2   1
RDHC022C_12 0.468816    0.700968    2   1
RDHC024C_12 0.515292    0.70664     2   1
RDHC025C_13 0.473163    0.704231    2   1
RDHC027C_12 0.518741    0.732939    2   1
RDHC030C_11 0.489205    0.708174    2   1

You can import it by doing the following:

copy the data and paste it within the quotation marks of the code below into R:

zz <- ""

now, assign the data to a data.frame:

mydata <- read.table(text=zz, header=TRUE)

I have no idea why changing the 'paired' parameter to TRUE would cause this to happen. Any help/advice would be much appreciated. Thanks - Paul

Body:
You initialize the mydf data.frame with the values 1:4 here

mydf <- data.frame(c(1:4))

basically the loop does nothing because t.test is throwing an error when you do PAIRED=TRUE because your two sets of values aren't the same length (and they need to be when doing a paired t-test. You have 22 values where Pre==1 and 13 values where Pre==2. You can't do a paired test with an imbalance like that.


问:
Elasticsearch Reindexing while updating documents?
What if I've changed mapping for my index and wants to reindex?

I'm currenly using the Java API which does not yet have the reindex functionality, so using bulk would solve my problems. So the solution would look something like this

ref How to reindex in ElasticSearch via Java API

Long time ago

create index MY_INDEX_1

create mapping for MY_INDEX_1

create alias MY_INDEX_1 -> MY_INDEX

create documents in MY_INDEX

Time to reindex!

List item

create index MY_INDEX_2

create mapping for MY_INDEX_2

scroll search + bulk all documents from MY_INDEX_1 to MY_INDEX_2

Renaming and deletion of old index

create alias MY_INDEX_2 -> MY_INDEX

delete alias MY_INDEX_1 -> MY_INDEX

delete index MY_INDEX_1

But what happens, while reindexing all documents, a document that was reindexed in the beginning is updated from a user.
Or that between reindexing and rename aliases the above happpens?

Possible Solutions ?

One way would be using external version, such as it does not overwrite an document with an higher version

Or could it be solved in another way?

Or between renaming aliases and deleting my_index_1, reindexing all documents that has been indexed since the reindexing? But then still it would be the case that a document has been updated between renaming aliases and second reindexing

Or should we lock while reindexing? Seems like a bad solution..

答:
I think this is your real question:

But what happens, while reindexing all documents, a document that was reindexed in the beginning is updated from a user. Or that between reindexing and rename aliases the above happpens?

I just asked a question that is very close, but still has questions that need to be resolved separately. However, my research allows me to answer this question.  See the question for details and references.

To answer your question, you create a second alias just before reindexing.  I call this a duplicate_write_alias and you have your application, if it sees this second alias, write to first the old and then the new index via the two aliases.  (the order is important to cancel a potential race). When the indexing is done, your indexing process deletes this duplicate_write_alias and moves your MY_INDEX alias to the new MY_INDEX_2 as noted above.  Do the alias switch in one atomic command.

As I noted in my question, you still have to deal with potential 'index does not exist' errors because of a remaining race between your application's checking for existence of the alias and the alias being deleted.  I'm hoping there's a better answer than 'always write twice and ignore errors' or 'check and hope for the best'...


问:
Merging two datasets using the last value of a string
I found the following code to pull off the last value in a string:

```<code>tail(strsplit("The quick brown fox",split = " ")[[1]],1)
</code>```

which correctly returns the value of "fox". I'm trying to apply this same principle to a text variable using the following code:

```<code>policy.data$LastName <- tail(strsplit(policy.data$Name,split = " ")[[1]],1)
</code>```

but this code gives me the following error:

Error in strsplit(policy.data$Name, split = " ") : non-character argument*

I do not understand why the code works when the string is specified but not with a text variable. I have a data set that has a full first and last name ("Jane Doe") and a data set that has the first initial and last name ("J Doe").  What I am really trying to do is merge the two datasets using last name ("Doe") so if there is a way to do this without creating a separate variable called "LastName" in each dataset, I would be very interested in that solution as well.

答:
The strsplit() function requires the first argument to be a vector (of characters). The policy.data$Name is most likely a factor. You can check using the str() function.

Try

```<code>policy.data$LastName <- tail(strsplit(as.vector(policy.data$Name),split = " ")[[1]],1)
</code>```

Body:
I found the following code to pull off the last value in a string:

tail(strsplit("The quick brown fox",split = " ")[[1]],1)

which correctly returns the value of "fox". I'm trying to apply this same principle to a text variable using the following code:

policy.data$LastName <- tail(strsplit(policy.data$Name,split = " ")[[1]],1)

but this code gives me the following error:

Error in strsplit(policy.data$Name, split = " ") : non-character argument*

I do not understand why the code works when the string is specified but not with a text variable. I have a data set that has a full first and last name ("Jane Doe") and a data set that has the first initial and last name ("J Doe").  What I am really trying to do is merge the two datasets using last name ("Doe") so if there is a way to do this without creating a separate variable called "LastName" in each dataset, I would be very interested in that solution as well.

Body:
The strsplit() function requires the first argument to be a vector (of characters). The policy.data$Name is most likely a factor. You can check using the str() function.

Try

policy.data$LastName <- tail(strsplit(as.vector(policy.data$Name),split = " ")[[1]],1)


问:
How to execute something after subscribe in angular
I want to return a boolean value ,but the variable in the "if" condition is undefined. </p>

```<code>function() {
    this.menuDataService.getMenu()
      .subscribe(res => {
       this.mainMenus = res.MainMenus;
       console.log(this.mainMenus);
    });

    console.log(this.mainMenus);

    if(this.mainMenus == 1){
       return true;
    }
    else {
      return false;
    }
}
</code>```

答:
Once you start using observables you have to work within the chained set of methods off of the observable.  In your case you could do something like this:

function InnerFunc(){
  return this.menuDataService.getMenu()
  .map(res => {
    if (res.mainMenus == 1) {
      return true;
    } else {
      return false;
    }
  )
}

function OuterFunc() {
  InnerFunc()
  .subscribe(result => {
    // will be either true or false
    console.log(result);
  });
}
```

Body:
I want to return a boolean value ,but the variable in the "if" condition is undefined.

function() {
    this.menuDataService.getMenu()
      .subscribe(res => {
       this.mainMenus = res.MainMenus;
       console.log(this.mainMenus);
    });

    console.log(this.mainMenus);

    if(this.mainMenus == 1){
       return true;
    }
    else {
      return false;
    }
}


问:
Why cannot define array variable in state of react class?
I've created a React class with state variable as array type.

```<code>export default class Demo extends React.Component {
  constructor(props){
    super(props)
    this.state ={
      test: []
    }
  }
</code>```

Now, when I use console.log(this.state.test) in render function then it returns object type. I thought it is for the unique-key purpose when mapping.

But, what if I just want to define an array for push method?

Is there any other way to define the same? Please guide.

答:
Remember that array are objects in javascipt.

```<code>typeof([]) //outputs object
</code>```

To print the array in readable form, you can use JSON.stringify

console.log(JSON.stringify(this.state.test, null, 2))

To set array in the state, you can use the setState method:

```<code>this.setState({test: [/* new array */]})
</code>```

In react I really like to use the pre tag to show state while I'm developing.

Add this to render:

```<code>```{JSON.stringify(this.state, null, 2)}```
</code>```

Body:
I've created a React class with state variable as array type.

export default class Demo extends React.Component {
  constructor(props){
    super(props)
    this.state ={
      test: []
    }
  }

Now, when I use console.log(this.state.test) in render function then it returns object type. I thought it is for the unique-key purpose when mapping.

But, what if I just want to define an array for push method?

Is there any other way to define the same? Please guide.

Body:
Remember that array are objects in javascipt.

typeof([]) //outputs object

To print the array in readable form, you can use JSON.stringify

console.log(JSON.stringify(this.state.test, null, 2))

To set array in the state, you can use the setState method:

this.setState({test: [/* new array */]})

In react I really like to use the pre tag to show state while I'm developing.

Add this to render:

<pre>{JSON.stringify(this.state, null, 2)}</pre>


问:
Apply size of video element to bootstrap modal while scaling
I'm currently trying to use Bootstrap to get a resizable modal which i can move around. So far i got that both working but i'm having a scaling issue. If i make the modal smaller by clicking and dragging the scaling icon and scale it only on one axis, the video gets out of the modal. So how do i make sure that the modal receives the current height of the video element and applies it to it's own height? Is there an easy way with jquery or CSS?

https://jsfiddle.net/pdh4cmuf/23/

```<code>$('.modal-content').resizable({
    //alsoResize: ".modal-dialog",
    minHeight: 150,
    minWidth: 200
});
$('.modal-dialog').draggable();

$('#myModal').on('show.bs.modal', function () {

    $(this).find('.modal-body').css({
        'max-height':'100%'
    });

});
 $('.modal-backdrop').removeClass("modal-backdrop");

$(window).load(function () {
            $('#myModal').modal({ backdrop: 'static', keyboard: false});
            $('#myModal').modal('show');
        });

function myFunction() {
            $('#myModal').modal('toggle');
            window.alert('Hello');
        }

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>

      </div>
      <div class="modal-body">
       <object class="embed-responsive-item">
<video controls>
    <source src="https://www.quirksmode.org/html5/videos/big_buck_bunny.mp4" type="video/mp4">
    <p>Your browser does not support H.264/MP4.</p>
</video>
</object>
    </div>
  </div>
</div>

.modal {
  pointer-events: none;
  }
.modal-backdrop {
  display: none;
  }

.vertical-alignment-helper {
            display:table;
            height: 100%;
            width: 100%;
            pointer-events:none; /* This makes sure that we can still click outside of the modal to close it */
        }
        .vertical-align-center {
            /* To center vertically */
            display: table-cell;
            vertical-align: middle;
            pointer-events:none;
        }
        .modal-content {

            margin: 0 auto;
            pointer-events: all;
            float: left;

            background-color: #ffffff;
        }
        .textarea-nonresizable {
            height: 10em;
            border-radius: 1em;
            resize: none; /* prevents the user-resizing, adjust to taste */
        }
video {
  width: 100%;
  height: auto!important;
}
</code>```

答:
Here you go with the updated fiddle https://jsfiddle.net/pdh4cmuf/26/

```<code>**Updated JS**
$('.modal-content').resizable({
    alsoResize: "#video",
    minHeight: 150,
    minWidth: 200
}).bind({
    resizestop: function(event, ui){

        $('video').css({
            'height':ui.size.height - 60,
            'width': ui.size.width - 30
        });
    }
});
</code>```

-60 & -30 are the padding to be removed from the calculated height & width respectively.

Body:
I'm currently trying to use Bootstrap to get a resizable modal which i can move around. So far i got that both working but i'm having a scaling issue. If i make the modal smaller by clicking and dragging the scaling icon and scale it only on one axis, the video gets out of the modal. So how do i make sure that the modal receives the current height of the video element and applies it to it's own height? Is there an easy way with jquery or CSS?

https://jsfiddle.net/pdh4cmuf/23/

$('.modal-content').resizable({
    //alsoResize: ".modal-dialog",
    minHeight: 150,
    minWidth: 200
});
$('.modal-dialog').draggable();

$('#myModal').on('show.bs.modal', function () {

    $(this).find('.modal-body').css({
        'max-height':'100%'
    });

});
 $('.modal-backdrop').removeClass("modal-backdrop");

$(window).load(function () {
            $('#myModal').modal({ backdrop: 'static', keyboard: false});
            $('#myModal').modal('show');
        });

function myFunction() {
            $('#myModal').modal('toggle');
            window.alert('Hello');
        }

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

        <button type="button" class="close" data-dismiss="modal" aria-label="Close">×</button>

       <object class="embed-responsive-item">
<video controls>
    <source src="https://www.quirksmode.org/html5/videos/big_buck_bunny.mp4" type="video/mp4">

Your browser does not support H.264/MP4.

</video>
</object>

.modal {
  pointer-events: none;
  }
.modal-backdrop {
  display: none;
  }

.vertical-alignment-helper {
            display:table;
            height: 100%;
            width: 100%;
            pointer-events:none; /* This makes sure that we can still click outside of the modal to close it */
        }
        .vertical-align-center {
            /* To center vertically */
            display: table-cell;
            vertical-align: middle;
            pointer-events:none;
        }
        .modal-content {

            margin: 0 auto;
            pointer-events: all;
            float: left;

            background-color: #ffffff;
        }
        .textarea-nonresizable {
            height: 10em;
            border-radius: 1em;
            resize: none; /* prevents the user-resizing, adjust to taste */
        }
video {
  width: 100%;
  height: auto!important;
}

Body:
Here you go with the updated fiddle https://jsfiddle.net/pdh4cmuf/26/

**Updated JS**
$('.modal-content').resizable({
    alsoResize: "#video",
    minHeight: 150,
    minWidth: 200
}).bind({
    resizestop: function(event, ui){

        $('video').css({
            'height':ui.size.height - 60,
            'width': ui.size.width - 30
        });
    }
});

-60 & -30 are the padding to be removed from the calculated height & width respectively.


问:
k-NN on non linear data + Dimensionality reduction
I'm trying to use k-NN on a tricky simulated dataset. the numpy array is (1000, 100), hence lot of dimensions. Before I run the k-NN for training/classification I need to pre-process/transform the dataset. PCA doesn't work, as the variance of all the features are almost same. The data as csv is available here as gist: https://gist.github.com/modqhx/0ab61da16eae8f371a1d6a787f018a64

On plotting the data, it looks like a 3d spherical structure(here's a screenshot using 'hypertools'):
 enter image

Any thoughts on how to proceed?

EDIT and in response to the comment: Yes, I understand if there are no "apparent" clustering, why use k-nn. I should have phrased that the right way. The raw data does not, however, some form of dimensionality reduction may reveal clusters. There are 100 dimensions, and PCA does not help as the variance of all 100 features are same. Question becomes, how can we do dimensionality reduction when the variance of all features are almost the same? .. Again, this is an exercise and the point is to make "knn" work! (if that makes any sense). I've been told that upto first and second moments you won't find any clusters, but after that(third moment and after) you may.

答:
I mostly concur with the comment of @javadba: if your data set has no obvious clustering property if you look at it, applying k-NN or any other clustering algorithm will only give you artifacts and dubious signals. The reason I'm writing this answer is because I did find some structure in your data.

What I did was first load your data. As I understand it, your (1000,101)-shaped array corresponds to 1000 points in 100-d space (plus a trailing column of zeros/ones that probably doesn't matter now). Note that this sounds like a very sparse object if you think about it. Consider a line with 2 points, a square with 4 points, a cube with 8 points...a 100-dimensional regular grid with the same sparsity (2 points along each dimension) would contain 2^100 points. That's...a lot more than 1000. Unfortunately, I have difficulty visualizing sparse point clouds in 100-d space.

So what I did was choose three axes randomly, and plot the corresponding 3d scatter plot to see if there are any patterns. And I did this multiple times at a time. Code:

```<code>import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

dat = np.loadtxt('foodat.csv',skiprows=1,delimiter=',')
nums = np.arange(dat.shape[1]-1)
for _ in range(5):
    fig = plt.figure()
    ax = fig.add_subplot(111,projection='3d')
    np.random.shuffle(nums)
    inds = nums[:3]
    plotdat = dat[:,inds]
    ax.scatter(*plotdat.T)
    ax.set_title(str(inds))
</code>```

Here's how a typical plot looks like from every angle:

As you see, it's a mess! Or more scientifically speaking: it's hard to visually distinguish these scatter plots from instances of uniform distributions in a cube. If all of the plots look like this, then there's possible that there's absolutely no clustering to begin with, so you should stop before you even begin. Whatever labels you could assign to your data would be meaningless.

But there's good news: in the interactive window I noticed that the above plot looks much more interesting from a certain angle:

The data clearly show a separation along dimension 42 (of all numbers!). Now this is promising. Note that a data set could even be truly clustered yet this might not be obvious from axis-aligned parallel projections. Imagine the following example scenario in 2d:

While the data is clearly clustered, this is far from obvious if we're only looking at axis-aligned projections.

My point is thus that finding evidence for the existence of clusters in your 100-dimensional dataset is really difficult. It's already hard to find evidence of clustering in lower-dimensional subspaces, but even if you can't find evidence of this, that would not mean that your data is not clustered in a diametrical configuration in 100d space.

I would start along this path of looking at lower-dimensional cuts. The fact that your points very nicely separate along dimension 42 suggests that this is not impossible. You could try systematically checking every dimension to see if any other produces such a separation...but you have to keep in mind that even for a dimension such as number 42 such a separation might only occur for certain dimension combinations.

In case dimension 42 is special in that it fully separates most of your points, you could try clustering your data along this axis, and working with the two halved datasets in a reduced, 99-dimensional space.

Body:
I mostly concur with the comment of @javadba: if your data set has no obvious clustering property if you look at it, applying k-NN or any other clustering algorithm will only give you artifacts and dubious signals. The reason I'm writing this answer is because I did find some structure in your data.

What I did was first load your data. As I understand it, your (1000,101)-shaped array corresponds to 1000 points in 100-d space (plus a trailing column of zeros/ones that probably doesn't matter now). Note that this sounds like a very sparse object if you think about it. Consider a line with 2 points, a square with 4 points, a cube with 8 points...a 100-dimensional regular grid with the same sparsity (2 points along each dimension) would contain 2^100 points. That's...a lot more than 1000. Unfortunately, I have difficulty visualizing sparse point clouds in 100-d space.

So what I did was choose three axes randomly, and plot the corresponding 3d scatter plot to see if there are any patterns. And I did this multiple times at a time. Code:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

dat = np.loadtxt('foodat.csv',skiprows=1,delimiter=',')
nums = np.arange(dat.shape[1]-1)
for _ in range(5):
    fig = plt.figure()
    ax = fig.add_subplot(111,projection='3d')
    np.random.shuffle(nums)
    inds = nums[:3]
    plotdat = dat[:,inds]
    ax.scatter(*plotdat.T)
    ax.set_title(str(inds))

Here's how a typical plot looks like from every angle:

As you see, it's a mess! Or more scientifically speaking: it's hard to visually distinguish these scatter plots from instances of uniform distributions in a cube. If all of the plots look like this, then there's possible that there's absolutely no clustering to begin with, so you should stop before you even begin. Whatever labels you could assign to your data would be meaningless.

But there's good news: in the interactive window I noticed that the above plot looks much more interesting from a certain angle:

The data clearly show a separation along dimension 42 (of all numbers!). Now this is promising. Note that a data set could even be truly clustered yet this might not be obvious from axis-aligned parallel projections. Imagine the following example scenario in 2d:

While the data is clearly clustered, this is far from obvious if we're only looking at axis-aligned projections.

My point is thus that finding evidence for the existence of clusters in your 100-dimensional dataset is really difficult. It's already hard to find evidence of clustering in lower-dimensional subspaces, but even if you can't find evidence of this, that would not mean that your data is not clustered in a diametrical configuration in 100d space.

I would start along this path of looking at lower-dimensional cuts. The fact that your points very nicely separate along dimension 42 suggests that this is not impossible. You could try systematically checking every dimension to see if any other produces such a separation...but you have to keep in mind that even for a dimension such as number 42 such a separation might only occur for certain dimension combinations.

In case dimension 42 is special in that it fully separates most of your points, you could try clustering your data along this axis, and working with the two halved datasets in a reduced, 99-dimensional space.