问:
Finding matched pairs in two character strings
I have a dataset that looks like the example I've created below

```<code>ex_data <- data.frame(
  House = c("House 1", "House 2", "House 3"),
  features = c("Roof, Walls, Windows, Oven", "Oven, Roof, Walls, TV", "Size, Oven, Bedrooms"),
  attributes = c("Large, White, 5, Whirlpool", "Samsung, Large, White, Sony", "4000 sq ft, KitchenAid, 5")
)
</code>```

Essentially my dataset has the main grouper (House) and has characteristics about that House. The characteristics are comma-separated values.

I am trying to figure out how to find the Oven and the brand that is attributed to the Oven in each house. I know that each house has an oven in the features string but I don't know how to find the corresponding brand.

My thinking is that I would use the function separate to create a new column for each feature and attribute however my real dataset has up to 100 comma-separated features/attributes.

I would like the end result to look like this

```<code>ex_data_result <- data.frame(
  House = c("House 1", "House 2", "House 3"),
  features = c("Oven", "Oven", "Oven"),
  attributes = c("Whirlpool", "Samsung", "KitchenAid")
)
</code>```

Thank you in advance.

答:
You can split the string on "," for features and attributes and select the corresponding attributes where features = "Oven".

In base R, you can do this with strsplit and mapply :

```<code>ex_data$Brand <- mapply(function(x, y) y[x == 'Oven'],
             strsplit(ex_data$features,", "), strsplit(ex_data$attributes,", "))

ex_data
#    House                   features                  attributes      Brand
#1 House 1 Roof, Walls, Windows, Oven  Large, White, 5, Whirlpool  Whirlpool
#2 House 2      Oven, Roof, Walls, TV Samsung, Large, White, Sony    Samsung
#3 House 3       Size, Oven, Bedrooms   4000 sq ft, KitchenAid, 5 KitchenAid
</code>```

Body:
I have a dataset that looks like the example I've created below

ex_data <- data.frame(
  House = c("House 1", "House 2", "House 3"),
  features = c("Roof, Walls, Windows, Oven", "Oven, Roof, Walls, TV", "Size, Oven, Bedrooms"),
  attributes = c("Large, White, 5, Whirlpool", "Samsung, Large, White, Sony", "4000 sq ft, KitchenAid, 5")
)

Essentially my dataset has the main grouper (House) and has characteristics about that House. The characteristics are comma-separated values.

I am trying to figure out how to find the Oven and the brand that is attributed to the Oven in each house. I know that each house has an oven in the features string but I don't know how to find the corresponding brand.

My thinking is that I would use the function separate to create a new column for each feature and attribute however my real dataset has up to 100 comma-separated features/attributes.

I would like the end result to look like this

ex_data_result <- data.frame(
  House = c("House 1", "House 2", "House 3"),
  features = c("Oven", "Oven", "Oven"),
  attributes = c("Whirlpool", "Samsung", "KitchenAid")
)

Thank you in advance.

Body:
You can split the string on "," for features and attributes and select the corresponding attributes where features = "Oven".

In base R, you can do this with strsplit and mapply :

ex_data$Brand <- mapply(function(x, y) y[x == 'Oven'],
             strsplit(ex_data$features,", "), strsplit(ex_data$attributes,", "))

ex_data
#    House                   features                  attributes      Brand
#1 House 1 Roof, Walls, Windows, Oven  Large, White, 5, Whirlpool  Whirlpool
#2 House 2      Oven, Roof, Walls, TV Samsung, Large, White, Sony    Samsung
#3 House 3       Size, Oven, Bedrooms   4000 sq ft, KitchenAid, 5 KitchenAid


来源: stackexchange

问:
Php writes XML file with no values
I have a script that reads values from a mysql database. With the values received i want to make a XML file.

The file is written but with no values.
I can't figure it out. Here is the complete source code:
http://www.segasiauto.ro/tmp/writexml.txt

Can someone help?

Thanks,
Sebastian

EDIT

Solved. There was a problem with the array. I added the data directly to createNodeText

答:
The problem is at the end of your file.
You use:

```<code>$doc->saveXML('document.xml');
</code>```

That does not output anything, the function returns a xml string. In order to send the xml string to your browser you should echo it:

```<code>echo $doc->saveXML('document.xml');
</code>```

See also DOMDocument::saveXML()

ContentLicense: CC BY-SA 3.0

Title: Php writes XML file with no values

Body:
The problem is at the end of your file.
You use:

$doc->saveXML('document.xml');

That does not output anything, the function returns a xml string. In order to send the xml string to your browser you should echo it:

echo $doc->saveXML('document.xml');

See also DOMDocument::saveXML()


问:
Is it okay to use GitHub Secrets with a public repo?
I have a private repo with a GitHub Action that pushes the code to an AWS S3 bucket when there's a new push to the master branch. I need a pair of access keys to be able to push the contents and I'm storing them as GitHub Secrets and referencing them as environment variables in the build script. Now I would like to make this repo public in the near future, and I was wondering if it's a safe thing to do. The workflow (.github/workflows/main.yml) itself for the action is indeed publicly visible and what it does, but it only has the single command aws s3 cp myfile s3://my-bucket and absolutely no access keys in the code itself.

Is it safe to use GitHub Secrets for the Actions in a public repo? I am the sole owner and only contributor, this will not change in the future. I might switch to CodePipeline with a webhook later but wanted to try GitHub Actions first. Thanks.

答:
Yes, secrets are safe to use in public repositories but there are some things you should be careful about.

All secrets are automatically masked in build logs and show as ***. However, if during your workflow you create a sensitive credential from a secret (e.g. base64 an API key) then you should mask the new value so it doesn't leak in the build log.

```<code>echo "::add-mask::My sensitive value"
</code>```

If you are very concerned about the security of your secrets, I would also suggest not using third party GitHub actions directly by following the action's tags or branches. Fork the action and use your fork in workflows. This will prevent the possibility of someone modifying an action you are using to capture secrets being used by the action, and send them to some external server under their control.

Alternatively, use the action directly and reference the commit hash for the version you want to target.

```<code>- uses: thirdparty/foo-action@172ec762f2ac8e050062398456fccd30444f8f30
</code>```

Use two-factor authentication (2FA) on your account. If your account is compromised, it's trivial for an attacker to create a workflow and export your secrets.

Repository collaborators or any organization users with write access are able to create a workflow to export secrets. So manage access to your repository carefully.

Points related to pull requests:

Public repository pull_request events triggered by forks do not have access to secrets, except for the default GITHUB_TOKEN. Additionally, The GITHUB_TOKEN has read-only access when an event is triggered by a forked repository. These are intentional restrictions enforced by GitHub Actions to prevent an attacker creating a pull request containing a workflow that captures secrets, or uses secrets to perform operations.

The pull_request_target event does not have secret restrictions for events triggered by forks. By default it checks out the last commit on the base branch, but it is possible to checkout the pull request HEAD. Choosing to do this requires extreme caution. Passing secrets to any code that could be modified in a pull request could allow an attacker to write code to export secrets.

ContentLicense: CC BY-SA 4.0

Body:
Yes, secrets are safe to use in public repositories but there are some things you should be careful about.

All secrets are automatically masked in build logs and show as ***. However, if during your workflow you create a sensitive credential from a secret (e.g. base64 an API key) then you should mask the new value so it doesn't leak in the build log.

echo "::add-mask::My sensitive value"

If you are very concerned about the security of your secrets, I would also suggest not using third party GitHub actions directly by following the action's tags or branches. Fork the action and use your fork in workflows. This will prevent the possibility of someone modifying an action you are using to capture secrets being used by the action, and send them to some external server under their control.

Alternatively, use the action directly and reference the commit hash for the version you want to target.

- uses: thirdparty/foo-action@172ec762f2ac8e050062398456fccd30444f8f30

Use two-factor authentication (2FA) on your account. If your account is compromised, it's trivial for an attacker to create a workflow and export your secrets.

Repository collaborators or any organization users with write access are able to create a workflow to export secrets. So manage access to your repository carefully.

Points related to pull requests:

Public repository pull_request events triggered by forks do not have access to secrets, except for the default GITHUB_TOKEN. Additionally, The GITHUB_TOKEN has read-only access when an event is triggered by a forked repository. These are intentional restrictions enforced by GitHub Actions to prevent an attacker creating a pull request containing a workflow that captures secrets, or uses secrets to perform operations.

The pull_request_target event does not have secret restrictions for events triggered by forks. By default it checks out the last commit on the base branch, but it is possible to checkout the pull request HEAD. Choosing to do this requires extreme caution. Passing secrets to any code that could be modified in a pull request could allow an attacker to write code to export secrets.


问:
is there any similar function with clamp_ in tensorflow > 2.0
I'm converting torch code to tensorflow 2.0

```<code>prior_boxes = torch.FloatTensor(prior_boxes).to(device)  # (8732, 4)
prior_boxes.clamp_(0, 1)  # (8732, 4)
</code>```

is there any replacement of clamp_(0,1) in tensorflow > 2.0?

答:
Try tf.clip_by_value, though unlike clamp_, it is not in-place:

```<code>t = tf.constant([[-10., -1., 0.], [0., 2., 10.]])
t2 = tf.clip_by_value(t, clip_value_min=-1, clip_value_max=1)
t2.numpy()
# gives [[-1., -1., 0.], [0., 1., 1.]]
</code>```

Body:
I'm converting torch code to tensorflow 2.0

prior_boxes = torch.FloatTensor(prior_boxes).to(device)  # (8732, 4)
prior_boxes.clamp_(0, 1)  # (8732, 4)

is there any replacement of clamp_(0,1) in tensorflow > 2.0?

Body:
Try tf.clip_by_value, though unlike clamp_, it is not in-place:

t = tf.constant([[-10., -1., 0.], [0., 2., 10.]])
t2 = tf.clip_by_value(t, clip_value_min=-1, clip_value_max=1)
t2.numpy()
# gives [[-1., -1., 0.], [0., 1., 1.]]


问:
Get the generated source of a single page application using curl syntax?
I use curl all the time at the command line, to request an url and parse it's markup.

I do this easily for authenticated pages by going in Chrome, loading the url, and then opening the inspector, finding the url at the top of the Network history, right clicking it, and choose Copy | Copy as Curl

I'd like to do the same with a single page application, that of course runs tons of other things to render itself, like javascript, or whatever.

Are there any tools out there that will let me easily change the "curl" to something else, and it will download the generated source of the page?

e.g. Normally I'd run this to get the source of the authenticated page if it wasn't a single page application (copied from Chrome)

```<code>curl 'https://mywebsite.com/singlePageApplication' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'Accept-Language: en,en-US;q=0.9' \
  -H 'Cookie: session=XXX"
</code>```

I'd like to be able to just switch that to something else, and it take in all the headers and preferably, exactly the same syntax as curl, and give me the generated source.

```<code>downloadGeneratedSource 'https://mywebsite.com/singlePageApplication' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'Accept-Language: en,en-US;q=0.9' \
  -H 'Cookie: session=XXX"
</code>```

Does this exist anywhere?

答:
I googled around and found a similar implementation in phantomjs, and tried to modify it to fit this use case, though it didnt seem to work. I can't seem to find the gist I based this off of ;-( but am throwing this up here as at least a crack at the solution ;-)

Side note: I just found this python webdriver approach that could work when doing more googling

downloadGeneratedSource

```<code>var argIs,getArg,d;
var customHeaders = {};

// grab the "rendered" HTML of a JavaScript-requiring web page

// TBD:
// add '-' as magic filename to read from STDIN
// add more curl-like switches? --or just let curl do that and consume the output of curl?
// add a switch for page.render( URLdump); // "screenshot"

var system = require('system'); // var args = require('system').args;
var page = require('webpage').create();

if (system.args.length === 1) {
    console.log('Usage: curl-phantom.js <http://URL/path/file.ext>');
    console.log(system.args);
    // note: can also read "pages" from the local filesystem
    phantom.exit();
};

var URLarg=system.args[1];
var theStatusCode = null;
var theStatusPrev = null;
var thePrevURL    = ''  ;
var theCurrURL    = ''  ;
var timestamp     = Date.now();
var verbose       = false;
var debug         = true;
var full_page     = false;
var header_key    = 'X-Forwarded-For';
var header_val    = '3.1.20.13';
var requestTimeout= 5000;   // Default request timeout

argIs = function(i, name){
  if (system.args[i].indexOf(name) == 0 ) {
    return true;
  }
  return false;
}

getArg = function(i) {
  return system.args[i].trim();
}

v = function(a,b) {
  verbose && console.log(a,b)
}

d = function(a,b) {
  debug && console.log(a,b)
}

for (var i=1; i<system.args.length; i++) {
  if (argIs(i, '--debug')) {
    debug = true;
    d('DEBUG: ' + getArg(i));
  }
  else if (argIs(i, '--full_page')) {
    full_page = true;
    d('PAGE: ' + getArg(i));
  }
  else if (argIs(i, '-H', '--header')) {
    var arg = getArg(++i);
    var arr = arg.trim().split(/\s*:\s*/);
    var header = {};
    var key = arr[0];
    var value = (arr.length == 2) ? arr[1] : '';
    customHeaders[key] = value;

    d('HEADER:', [key, value]);
  }
  else if (argIs(i, '--verbose')) {
    verbose   = true;
    v('VERBOSE: ' + getArg(i));
  }
  else if (argIs(i, '--timeout')) {
    requestTimeout = getArg(++i);
    d('REQUEST_TIMEOUT', requestTimeout);
  }
  else {
    console.log('unknown param: '+getArg(i));
  }
}
console.log('################');
console.log('headers and values');
console.log(JSON.stringify(customHeaders));

page.settings.resourceTimeout = requestTimeout;

page.customHeaders = customHeaders;
//page.customHeaders = { header_key : header_val };
v('VERBOSE: ' + header_key +': '+ header_val);

page.onConsoleMessage = function (msg) { // call-back function intercepts console.log messages
    d('DEBUG: console.log message="' + msg + '"');
};

page.onLoadFinished = function(status) {
  if ( debug ) {
    // console.log('Status: ' + status +' after onLoadFinished(' + status +')');
    system.stderr.write('OnLoadFinished.Status: ' + (theStatusCode ? theStatusCode : status) +' after onLoadFinished(' + status +')\n');
  }
};

page.onResourceReceived = function(resource) {
  // if (resource.url == URLarg || (theStatusCode >= 300 && theStatusCode < 400)) {
    theStatusPrev = theStatusCode  ;
    theStatusCode = resource.status;
    thePrevURL    = theCurrURL  ;
    theCurrURL    = resource.url;
  // }
    if ( resource.status === 200 ) {
        v('VERBOSE status ' + resource.status + ' for ' + resource.url ); // don't usually log standard success
    } else {
        v('Status Code was: ' + theStatusPrev   + ' for ' + thePrevURL );
        v('Status Code is : ' + theStatusCode   + ' for ' + theCurrURL );
    }
};

page.onUrlChanged = function (URLnew) { // call-back function intercepts console.log messages
    if ( URLnew === URLarg ) {
      d('DEBUG: old/new URL: ' + URLnew + ' --onUrlChanged()');
    } else {
      v('DEBUG: old URL: ' + URLarg);
      v('DEBUG: new URL: ' + URLnew);
    }
};

phantom.onError = function(msg, trace) {
    var msgStack = ['PHANTOM ERROR: ' + msg];
    if (trace) {
        msgStack.push('TRACE:');
        trace.forEach(function(t) {
            msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function + ')' : ''));
        });
    }
    console.error(msgStack.join('\n'));
};

page.onResourceTimeout = function(request) {
    console.error('Request timed out due to ' + request.errorCode + ' - ' + request.errorString);
    phantom.exit(1);
}

page.open( URLarg, function () {
    // onLoadFinished executes here
    var page_content = page.content;
    var body_innerHTML= page.evaluate( function() {
      return document.body.innerHTML ? document.body.innerHTML : '(empty)' ;
    });
    var title = page.evaluate(function() {return document.title; });

    // page.render( URLdump); // "screenshot"
    v('VERBOSE: Loading time '+ ( Date.now() - timestamp ) +' msec');
    d('DEBUG: Page title: ' + ((title==='') ? '(none)':title) );
    d('DEBUG: body_innerHTML.length='+ body_innerHTML.length);
    d(' ');

    if ( full_page  || ( ! body_innerHTML ) || body_innerHTML.length < 9 ) {
      console.log( page_content   ); // return all if body is empty
    } else {
      console.log( body_innerHTML );
    }
    setTimeout(function() {
        v('VERBOSE: status ' + theStatusPrev   + ' for ' + thePrevURL + ' (b)');
        v('VERBOSE: status ' + theStatusCode   + ' for ' + theCurrURL + ' (c)');
      }, 1333 ) ; // delay in milliseconds
    phantom.exit( theStatusCode);
  }) ;

</code>```

Body:
I use curl all the time at the command line, to request an url and parse it's markup.

I do this easily for authenticated pages by going in Chrome, loading the url, and then opening the inspector, finding the url at the top of the Network history, right clicking it, and choose Copy | Copy as Curl

I'd like to do the same with a single page application, that of course runs tons of other things to render itself, like javascript, or whatever.

Are there any tools out there that will let me easily change the "curl" to something else, and it will download the generated source of the page?

e.g. Normally I'd run this to get the source of the authenticated page if it wasn't a single page application (copied from Chrome)

curl 'https://mywebsite.com/singlePageApplication' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'Accept-Language: en,en-US;q=0.9' \
  -H 'Cookie: session=XXX"

I'd like to be able to just switch that to something else, and it take in all the headers and preferably, exactly the same syntax as curl, and give me the generated source.

downloadGeneratedSource 'https://mywebsite.com/singlePageApplication' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'Accept-Language: en,en-US;q=0.9' \
  -H 'Cookie: session=XXX"

Does this exist anywhere?

Body:
I googled around and found a similar implementation in phantomjs, and tried to modify it to fit this use case, though it didnt seem to work. I can't seem to find the gist I based this off of ;-( but am throwing this up here as at least a crack at the solution ;-)

Side note: I just found this python webdriver approach that could work when doing more googling

downloadGeneratedSource

var argIs,getArg,d;
var customHeaders = {};

// grab the "rendered" HTML of a JavaScript-requiring web page

// TBD:
// add '-' as magic filename to read from STDIN
// add more curl-like switches? --or just let curl do that and consume the output of curl?
// add a switch for page.render( URLdump); // "screenshot"

var system = require('system'); // var args = require('system').args;
var page = require('webpage').create();

if (system.args.length === 1) {
    console.log('Usage: curl-phantom.js <http://URL/path/file.ext>');
    console.log(system.args);
    // note: can also read "pages" from the local filesystem
    phantom.exit();
};

var URLarg=system.args[1];
var theStatusCode = null;
var theStatusPrev = null;
var thePrevURL    = ''  ;
var theCurrURL    = ''  ;
var timestamp     = Date.now();
var verbose       = false;
var debug         = true;
var full_page     = false;
var header_key    = 'X-Forwarded-For';
var header_val    = '3.1.20.13';
var requestTimeout= 5000;   // Default request timeout

argIs = function(i, name){
  if (system.args[i].indexOf(name) == 0 ) {
    return true;
  }
  return false;
}

getArg = function(i) {
  return system.args[i].trim();
}

v = function(a,b) {
  verbose && console.log(a,b)
}

d = function(a,b) {
  debug && console.log(a,b)
}

for (var i=1; i<system.args.length; i++) {
  if (argIs(i, '--debug')) {
    debug = true;
    d('DEBUG: ' + getArg(i));
  }
  else if (argIs(i, '--full_page')) {
    full_page = true;
    d('PAGE: ' + getArg(i));
  }
  else if (argIs(i, '-H', '--header')) {
    var arg = getArg(++i);
    var arr = arg.trim().split(/\s*:\s*/);
    var header = {};
    var key = arr[0];
    var value = (arr.length == 2) ? arr[1] : '';
    customHeaders[key] = value;

    d('HEADER:', [key, value]);
  }
  else if (argIs(i, '--verbose')) {
    verbose   = true;
    v('VERBOSE: ' + getArg(i));
  }
  else if (argIs(i, '--timeout')) {
    requestTimeout = getArg(++i);
    d('REQUEST_TIMEOUT', requestTimeout);
  }
  else {
    console.log('unknown param: '+getArg(i));
  }
}
console.log('################');
console.log('headers and values');
console.log(JSON.stringify(customHeaders));

page.settings.resourceTimeout = requestTimeout;

page.customHeaders = customHeaders;
//page.customHeaders = { header_key : header_val };
v('VERBOSE: ' + header_key +': '+ header_val);

page.onConsoleMessage = function (msg) { // call-back function intercepts console.log messages
    d('DEBUG: console.log message="' + msg + '"');
};

page.onLoadFinished = function(status) {
  if ( debug ) {
    // console.log('Status: ' + status +' after onLoadFinished(' + status +')');
    system.stderr.write('OnLoadFinished.Status: ' + (theStatusCode ? theStatusCode : status) +' after onLoadFinished(' + status +')\n');
  }
};

page.onResourceReceived = function(resource) {
  // if (resource.url == URLarg || (theStatusCode >= 300 && theStatusCode < 400)) {
    theStatusPrev = theStatusCode  ;
    theStatusCode = resource.status;
    thePrevURL    = theCurrURL  ;
    theCurrURL    = resource.url;
  // }
    if ( resource.status === 200 ) {
        v('VERBOSE status ' + resource.status + ' for ' + resource.url ); // don't usually log standard success
    } else {
        v('Status Code was: ' + theStatusPrev   + ' for ' + thePrevURL );
        v('Status Code is : ' + theStatusCode   + ' for ' + theCurrURL );
    }
};

page.onUrlChanged = function (URLnew) { // call-back function intercepts console.log messages
    if ( URLnew === URLarg ) {
      d('DEBUG: old/new URL: ' + URLnew + ' --onUrlChanged()');
    } else {
      v('DEBUG: old URL: ' + URLarg);
      v('DEBUG: new URL: ' + URLnew);
    }
};

phantom.onError = function(msg, trace) {
    var msgStack = ['PHANTOM ERROR: ' + msg];
    if (trace) {
        msgStack.push('TRACE:');
        trace.forEach(function(t) {
            msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function + ')' : ''));
        });
    }
    console.error(msgStack.join('\n'));
};

page.onResourceTimeout = function(request) {
    console.error('Request timed out due to ' + request.errorCode + ' - ' + request.errorString);
    phantom.exit(1);
}

page.open( URLarg, function () {
    // onLoadFinished executes here
    var page_content = page.content;
    var body_innerHTML= page.evaluate( function() {
      return document.body.innerHTML ? document.body.innerHTML : '(empty)' ;
    });
    var title = page.evaluate(function() {return document.title; });

    // page.render( URLdump); // "screenshot"
    v('VERBOSE: Loading time '+ ( Date.now() - timestamp ) +' msec');
    d('DEBUG: Page title: ' + ((title==='') ? '(none)':title) );
    d('DEBUG: body_innerHTML.length='+ body_innerHTML.length);
    d(' ');

    if ( full_page  || ( ! body_innerHTML ) || body_innerHTML.length < 9 ) {
      console.log( page_content   ); // return all if body is empty
    } else {
      console.log( body_innerHTML );
    }
    setTimeout(function() {
        v('VERBOSE: status ' + theStatusPrev   + ' for ' + thePrevURL + ' (b)');
        v('VERBOSE: status ' + theStatusCode   + ' for ' + theCurrURL + ' (c)');
      }, 1333 ) ; // delay in milliseconds
    phantom.exit( theStatusCode);
  }) ;


问:
inet_ntop() was not decleared in this scope
I am building a TCP server using C: I included the library where the inet_ntop() function is (WS2tcpip.h), yet my compiler returns with the following error:

'inet_ntop' was not declared in this scope

I have no idea why; even my IDE doesn't notice anything wrong (I use vscode as my IDE because it can detect errors in real-time without compiling every time) yet when I compile it gcc returns the error I stated previously.

What I have included and defined:

```<code>#include <stdio.h>
#include <WS2tcpip.h>

#define WIN32_LEAN_AND_MEAN

#pragma comment (lib, "ws2_32.lib")
</code>```

The faulty line of code:

```<code>if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXHOST, 0) == 0) {
        printf("\n[CONNECTION]: %c connesso sulla porta %c", host, service);
    } else {
        inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
        printf("\n[CONNECTION]: %c connesso sulla porta %c", host, ntohs(client.sin_port));
    }
</code>```

Any helpful tip is appreciated, thanks!

答:
Are you using VSCode? or manual compilers?

I have pretty same error with my code.
Im using C++ in my code but I get error with these:
'getnameinfo' was not declared in this scope
'inet_ntop' was not declared in this scope

I use VS Code. What I realize is, when I copy paste same code to VS, it works! I think its something with compiler problem.

Still can't compile my code in VSCode but in VS it works.

Body:
I am building a TCP server using C: I included the library where the inet_ntop() function is (WS2tcpip.h), yet my compiler returns with the following error:

'inet_ntop' was not declared in this scope

I have no idea why; even my IDE doesn't notice anything wrong (I use vscode as my IDE because it can detect errors in real-time without compiling every time) yet when I compile it gcc returns the error I stated previously.

What I have included and defined:

#include <stdio.h>
#include <WS2tcpip.h>

#define WIN32_LEAN_AND_MEAN

#pragma comment (lib, "ws2_32.lib")

The faulty line of code:

if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXHOST, 0) == 0) {
        printf("\n[CONNECTION]: %c connesso sulla porta %c", host, service);
    } else {
        inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
        printf("\n[CONNECTION]: %c connesso sulla porta %c", host, ntohs(client.sin_port));
    }

Any helpful tip is appreciated, thanks!


问:
jtextarea is showing character for a moment when character is masked which shouldn't
I have a requirement where I am entering input in JTextarea at run time and input should be masked. I am able to achieve this through below code snippet

```<code>if(encryptKeystroke == true) {
            jTextArea.addKeyListener(new KeyListener() {
                public void keyTyped(KeyEvent e) {
                }

                public void keyPressed(KeyEvent e) {
                    if (e.getExtendedKeyCode() == KeyEvent.VK_BACK_SPACE) {
                        if(text.length() > 0)
                         text = text.substring(0, text.length() - 1);
                    }
                    else {
                        text += String.valueOf(e.getKeyChar());
                    }
                    jTextArea.setText(text.replaceAll(".", "*"));

                }
                public void keyReleased(KeyEvent e) {
                    jTextArea.setText(text.replaceAll(".", "*"));
                }
            });
        }
</code>```

Issue is when I am running this, entered character is visible for a small moment and then getting masked(like it happened in Android).
I am using JTextarea because unable to achieve the scrollable & wrapstyle in Ttextfield.
Any suggestion how this can be achieved ?

答:
Don't use a KeyListener for something like this. What if the user:

pastes text into the text area. The code won't handle multiple characters

moves the caret to the beginning of the text area. The code assumes text is always added at the end.

uses the Delete key

highlights a block of text and then enters a character

A KeyListener can't handle all these special situations. Swing has better and newer API's to use.

Instead you can use a DocumentFilter. The DocumentFilter allows you to filter the text BEFORE it is added to the Document of the JTextArea.

Basic example:

```<code>import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class AsteriskFilter extends DocumentFilter
{
    private StringBuilder realText = new StringBuilder();

    @Override
    public void insertString(FilterBypass fb, int offset, String text, AttributeSet attributes)
        throws BadLocationException
    {
        replace(fb, offset, 0, text, attributes);
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attributes)
        throws BadLocationException
    {
        //  Update the StringBuilder to contain the real text

        Document doc = fb.getDocument();
        realText.replace(offset, offset + length, text);

        //  Update the Document with asterisks

        text = text.replaceAll(".", "*");
        super.replace(fb, offset, length, text, attributes);
    }

    @Override
    public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
        throws BadLocationException
    {
        realText.delete(offset, offset + length);

        super.remove(fb, offset, length);
    }

    public String getRealText()
    {
        return realText.toString();
    }

    private static void createAndShowGUI()
    {
        JTextArea textArea = new JTextArea(3, 20);
        AbstractDocument doc = (AbstractDocument) textArea.getDocument();
        AsteriskFilter filter = new AsteriskFilter();
        doc.setDocumentFilter( filter );

        JButton button = new JButton("Display Text");
        button.addActionListener(e -> JOptionPane.showMessageDialog(textArea, filter.getRealText()));

        JFrame frame = new JFrame("Asterisk Filter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(textArea, BorderLayout.CENTER);
        frame.add(button, BorderLayout.PAGE_END);
        frame.setSize(220, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }

}
</code>```

Body:
I have a requirement where I am entering input in JTextarea at run time and input should be masked. I am able to achieve this through below code snippet

if(encryptKeystroke == true) {
            jTextArea.addKeyListener(new KeyListener() {
                public void keyTyped(KeyEvent e) {
                }

                public void keyPressed(KeyEvent e) {
                    if (e.getExtendedKeyCode() == KeyEvent.VK_BACK_SPACE) {
                        if(text.length() > 0)
                         text = text.substring(0, text.length() - 1);
                    }
                    else {
                        text += String.valueOf(e.getKeyChar());
                    }
                    jTextArea.setText(text.replaceAll(".", "*"));

                }
                public void keyReleased(KeyEvent e) {
                    jTextArea.setText(text.replaceAll(".", "*"));
                }
            });
        }

Issue is when I am running this, entered character is visible for a small moment and then getting masked(like it happened in Android).
I am using JTextarea because unable to achieve the scrollable & wrapstyle in Ttextfield.
Any suggestion how this can be achieved ?

Body:
Don't use a KeyListener for something like this. What if the user:

pastes text into the text area. The code won't handle multiple characters

moves the caret to the beginning of the text area. The code assumes text is always added at the end.

uses the Delete key

highlights a block of text and then enters a character

A KeyListener can't handle all these special situations. Swing has better and newer API's to use.

Instead you can use a DocumentFilter. The DocumentFilter allows you to filter the text BEFORE it is added to the Document of the JTextArea.

Basic example:

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class AsteriskFilter extends DocumentFilter
{
    private StringBuilder realText = new StringBuilder();

    @Override
    public void insertString(FilterBypass fb, int offset, String text, AttributeSet attributes)
        throws BadLocationException
    {
        replace(fb, offset, 0, text, attributes);
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attributes)
        throws BadLocationException
    {
        //  Update the StringBuilder to contain the real text

        Document doc = fb.getDocument();
        realText.replace(offset, offset + length, text);

        //  Update the Document with asterisks

        text = text.replaceAll(".", "*");
        super.replace(fb, offset, length, text, attributes);
    }

    @Override
    public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
        throws BadLocationException
    {
        realText.delete(offset, offset + length);

        super.remove(fb, offset, length);
    }

    public String getRealText()
    {
        return realText.toString();
    }

    private static void createAndShowGUI()
    {
        JTextArea textArea = new JTextArea(3, 20);
        AbstractDocument doc = (AbstractDocument) textArea.getDocument();
        AsteriskFilter filter = new AsteriskFilter();
        doc.setDocumentFilter( filter );

        JButton button = new JButton("Display Text");
        button.addActionListener(e -> JOptionPane.showMessageDialog(textArea, filter.getRealText()));

        JFrame frame = new JFrame("Asterisk Filter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(textArea, BorderLayout.CENTER);
        frame.add(button, BorderLayout.PAGE_END);
        frame.setSize(220, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }

}


问:
How to create IProject without Eclipse
I'm trying to mavenize a third part Eclipse's plugin,
but I need to create an instance of IProject to pass at JavaCore.create(instance) to obtain an instance of IJavaProject.

This application will not have workspace and will be better that depends from eclipse's libraries as less as I can.

So I can't use:

```<code>ResourcesPlugin.getWorkspace().getRoot().getProject(...);
</code>```

When I try to start my program I have this error report:

```<code>Exception in thread "main" java.lang.IllegalStateException: Workspace is closed.
    at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:367)
    at gr.uom.java.jdeodorant.helpers.JavaProjectHelper.createJavaProject(JavaProjectHelper.java:55)
    at gr.uom.java.jdeodorant.refactoring.actions.Index.initializeJavaProject(Index.java:28)
    at gr.uom.java.jdeodorant.refactoring.actions.Index.main(Index.java:53)
</code>```

So is there any way to create this instance outside Eclipse, just passing the path of the project that I need?
Some ideas?

答:
The short answer: no.

The workspace requires the OSGi framework to start, and set the osgi.instance.area property.  It also needs some of the other org.eclipse.core.runtime services to be started.

JavaCore.create(*) is in JDT ... parts of org.eclipse.jdt.core might run without having the UI workbench up, but that really depend on which parts.

It might be possible to run a HEADLESS RCP application that includes the org.eclipse.core.runtime and org.eclipse.core.resources (workspace) without including any of the UI components, but you won't be able to use the workspace without starting up OSGi and the core runtime.

Title: How to create IProject without Eclipse

Body:
I'm trying to mavenize a third part Eclipse's plugin,
but I need to create an instance of IProject to pass at JavaCore.create(instance) to obtain an instance of IJavaProject.

This application will not have workspace and will be better that depends from eclipse's libraries as less as I can.

So I can't use:

ResourcesPlugin.getWorkspace().getRoot().getProject(...);

When I try to start my program I have this error report:

Exception in thread "main" java.lang.IllegalStateException: Workspace is closed.
    at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:367)
    at gr.uom.java.jdeodorant.helpers.JavaProjectHelper.createJavaProject(JavaProjectHelper.java:55)
    at gr.uom.java.jdeodorant.refactoring.actions.Index.initializeJavaProject(Index.java:28)
    at gr.uom.java.jdeodorant.refactoring.actions.Index.main(Index.java:53)

So is there any way to create this instance outside Eclipse, just passing the path of the project that I need?
Some ideas?


问:
how parallel operator changes the flow on subsequent stream elements?
Executed below code

```<code>List.of(1, 2, 3, 4).stream()
    .map(
        integer -> {
          System.out.println(
              "Before parallel operator : " + Thread.currentThread().getName() + " : " + integer);
          return integer * 2;
        })
    .parallel()
    .map(
        integer -> {
          System.out.println(
              " After parallel operator : " + Thread.currentThread().getName() + " : " + integer);
          return integer * 2;
        })
    .forEach(
            integer -> {
          System.out.println(" For Each : " + Thread.currentThread().getName() + " : " + integer);
        });
</code>```

Output:

```<code>    Before parallel operator : main : 3
Before parallel operator : ForkJoinPool.commonPool-worker-19 : 2
Before parallel operator : ForkJoinPool.commonPool-worker-23 : 1
Before parallel operator : ForkJoinPool.commonPool-worker-5 : 4
 After parallel operator : main : 6
 After parallel operator : ForkJoinPool.commonPool-worker-23 : 2
 After parallel operator : ForkJoinPool.commonPool-worker-19 : 4
 After parallel operator : ForkJoinPool.commonPool-worker-5 : 8
 For Each : ForkJoinPool.commonPool-worker-19 : 8
 For Each : main : 12
 For Each : ForkJoinPool.commonPool-worker-23 : 4
 For Each : ForkJoinPool.commonPool-worker-5 : 16
</code>```

Apart from element 3 all other or ran in parallel?
Would like to understand behavior parallel operator on subsequent calls?

Where does the parallel operator kick in and how does the parallelism continue?

答:
The stream won't be processed until a terminal operation is called (such as forEach or collect), more to this in a bit. So, answering your question, "Where does the parallel operator kick in and how does the parallelism continue?".

What the docs say?

The documentation is clear about this matter:

the stream's mode can be modified with the BaseStream.sequential() and BaseStream.parallel() operations. The most recent sequential or parallel mode setting applies to the execution of the entire stream pipeline

A Little Demo

Now consider the following piece of code (Pardon my System.out, it's for demo purposes). If we change between parallel and sequential, the entire pipeline changes, not only the subsequent operators.

System.out.println("=== Creating stream s1 as 1,2,3,4");
var s1 = List.of(1, 2, 3, 4).stream();
System.out.println("s1 is parallel? " + s1.isParallel());

System.out.println("=== s2 results of applying map to s1");
var s2 = s1.map(integer -> integer * 2);
System.out.println("s1 is parallel? " + s1.isParallel());
System.out.println("s2 is parallel? " + s2.isParallel());

System.out.println("=== s3 results of applying parallel to s2");
var s3 = s2.parallel();
System.out.println("s1 is parallel? " + s1.isParallel());
System.out.println("s2 is parallel? " + s2.isParallel());
System.out.println("s3 is parallel? " + s3.isParallel());

System.out.println("=== s4 results of applying map to s3");
var s4 = s3.map(integer -> integer * 2);
System.out.println("s1 is parallel? " + s1.isParallel());
System.out.println("s2 is parallel? " + s2.isParallel());
System.out.println("s3 is parallel? " + s3.isParallel());
System.out.println("s4 is parallel? " + s4.isParallel());

System.out.println("=== s5 results of applying sequential to s4");
var s5 = s4.sequential();
System.out.println("s1 is parallel? " + s1.isParallel());
System.out.println("s2 is parallel? " + s2.isParallel());
System.out.println("s3 is parallel? " + s3.isParallel());
System.out.println("s4 is parallel? " + s4.isParallel());
System.out.println("s5 is parallel? " + s5.isParallel());
```

<p>This will output the following:</p>

<pre class="lang-none prettyprint-override"><code>=== Creating stream s1 as 1,2,3,4
s1 is parallel? false
=== s2 results of applying map to s1
s1 is parallel? false
s2 is parallel? false
=== s3 results of applying parallel to s2
s1 is parallel? true
s2 is parallel? true
s3 is parallel? true
=== s4 results of applying map to s3
s1 is parallel? true
s2 is parallel? true
s3 is parallel? true
s4 is parallel? true
=== s5 results of applying sequential to s4
s1 is parallel? false
s2 is parallel? false
s3 is parallel? false
s4 is parallel? false
s5 is parallel? false
</code>```

Now when you call a terminal operator like forEach or collect, it will consider only sequential streams during processing, even though parallel was called in the middle. As the documentation states, the most recent applied mode is used for the entire pipeline.

How is this useful?

You may ask. It's possible to change the behaviour in the middle of the pipeline by "breaking" the pipeline with a terminal operator. For instance, taking your example, if we apply collect right after the first map, the first map will be executed sequentially, and then parallel will apply only to the subsequent operators but, in practice, this is now a different pipeline because everything was collected in the middle to a list.

List.of(1, 2, 3, 4).stream()
    .map(integer -> {
        System.out.println("Before stream : " + Thread.currentThread().getName() + " : " + integer);
        return integer * 2;
    })
    .collect(Collectors.toList())
    .stream()
    .parallel()
    .map(integer -> {
        System.out.println("After parallel stream : " + Thread.currentThread().getName() + " : " + integer);
        return integer * 2;
    })
    .forEach(integer -> System.out.println("For Each : " + Thread.currentThread().getName() + " : " + integer));
```

<p>This will now output something like:</p>

<pre class="lang-java prettyprint-override"><code>Before stream : main : 1
Before stream : main : 2
Before stream : main : 3
Before stream : main : 4
After parallel stream : main : 6
After parallel stream : ForkJoinPool.commonPool-worker-23 : 2
After parallel stream : ForkJoinPool.commonPool-worker-5 : 4
After parallel stream : ForkJoinPool.commonPool-worker-19 : 8
For Each : ForkJoinPool.commonPool-worker-19 : 16
For Each : ForkJoinPool.commonPool-worker-5 : 8
For Each : ForkJoinPool.commonPool-worker-23 : 4
For Each : main : 12
</code>```

Notice how the first map is executed sequentially while the remaining operators are executed in parallel.

Observable streams implementations such as RxJava got a different hang of this with the observeOn operator, but they are a totally different way of doing things as well.

Body:
Executed below code

List.of(1, 2, 3, 4).stream()
    .map(
        integer -> {
          System.out.println(
              "Before parallel operator : " + Thread.currentThread().getName() + " : " + integer);
          return integer * 2;
        })
    .parallel()
    .map(
        integer -> {
          System.out.println(
              " After parallel operator : " + Thread.currentThread().getName() + " : " + integer);
          return integer * 2;
        })
    .forEach(
            integer -> {
          System.out.println(" For Each : " + Thread.currentThread().getName() + " : " + integer);
        });

Output:

    Before parallel operator : main : 3
Before parallel operator : ForkJoinPool.commonPool-worker-19 : 2
Before parallel operator : ForkJoinPool.commonPool-worker-23 : 1
Before parallel operator : ForkJoinPool.commonPool-worker-5 : 4
 After parallel operator : main : 6
 After parallel operator : ForkJoinPool.commonPool-worker-23 : 2
 After parallel operator : ForkJoinPool.commonPool-worker-19 : 4
 After parallel operator : ForkJoinPool.commonPool-worker-5 : 8
 For Each : ForkJoinPool.commonPool-worker-19 : 8
 For Each : main : 12
 For Each : ForkJoinPool.commonPool-worker-23 : 4
 For Each : ForkJoinPool.commonPool-worker-5 : 16

Apart from element 3 all other or ran in parallel?
Would like to understand behavior parallel operator on subsequent calls?

Where does the parallel operator kick in and how does the parallelism continue?

Body:
The stream won't be processed until a terminal operation is called (such as forEach or collect), more to this in a bit. So, answering your question, "Where does the parallel operator kick in and how does the parallelism continue?".

What the docs say?

The documentation is clear about this matter:

the stream's mode can be modified with the BaseStream.sequential() and BaseStream.parallel() operations. The most recent sequential or parallel mode setting applies to the execution of the entire stream pipeline

A Little Demo

Now consider the following piece of code (Pardon my System.out, it's for demo purposes). If we change between parallel and sequential, the entire pipeline changes, not only the subsequent operators.

System.out.println("=== Creating stream s1 as 1,2,3,4");
var s1 = List.of(1, 2, 3, 4).stream();
System.out.println("s1 is parallel? " + s1.isParallel());

System.out.println("=== s2 results of applying map to s1");
var s2 = s1.map(integer -> integer * 2);
System.out.println("s1 is parallel? " + s1.isParallel());
System.out.println("s2 is parallel? " + s2.isParallel());

System.out.println("=== s3 results of applying parallel to s2");
var s3 = s2.parallel();
System.out.println("s1 is parallel? " + s1.isParallel());
System.out.println("s2 is parallel? " + s2.isParallel());
System.out.println("s3 is parallel? " + s3.isParallel());

System.out.println("=== s4 results of applying map to s3");
var s4 = s3.map(integer -> integer * 2);
System.out.println("s1 is parallel? " + s1.isParallel());
System.out.println("s2 is parallel? " + s2.isParallel());
System.out.println("s3 is parallel? " + s3.isParallel());
System.out.println("s4 is parallel? " + s4.isParallel());

System.out.println("=== s5 results of applying sequential to s4");
var s5 = s4.sequential();
System.out.println("s1 is parallel? " + s1.isParallel());
System.out.println("s2 is parallel? " + s2.isParallel());
System.out.println("s3 is parallel? " + s3.isParallel());
System.out.println("s4 is parallel? " + s4.isParallel());
System.out.println("s5 is parallel? " + s5.isParallel());

This will output the following:

=== Creating stream s1 as 1,2,3,4
s1 is parallel? false
=== s2 results of applying map to s1
s1 is parallel? false
s2 is parallel? false
=== s3 results of applying parallel to s2
s1 is parallel? true
s2 is parallel? true
s3 is parallel? true
=== s4 results of applying map to s3
s1 is parallel? true
s2 is parallel? true
s3 is parallel? true
s4 is parallel? true
=== s5 results of applying sequential to s4
s1 is parallel? false
s2 is parallel? false
s3 is parallel? false
s4 is parallel? false
s5 is parallel? false

Now when you call a terminal operator like forEach or collect, it will consider only sequential streams during processing, even though parallel was called in the middle. As the documentation states, the most recent applied mode is used for the entire pipeline.

How is this useful?

You may ask. It's possible to change the behaviour in the middle of the pipeline by "breaking" the pipeline with a terminal operator. For instance, taking your example, if we apply collect right after the first map, the first map will be executed sequentially, and then parallel will apply only to the subsequent operators but, in practice, this is now a different pipeline because everything was collected in the middle to a list.

List.of(1, 2, 3, 4).stream()
    .map(integer -> {
        System.out.println("Before stream : " + Thread.currentThread().getName() + " : " + integer);
        return integer * 2;
    })
    .collect(Collectors.toList())
    .stream()
    .parallel()
    .map(integer -> {
        System.out.println("After parallel stream : " + Thread.currentThread().getName() + " : " + integer);
        return integer * 2;
    })
    .forEach(integer -> System.out.println("For Each : " + Thread.currentThread().getName() + " : " + integer));

This will now output something like:

Before stream : main : 1
Before stream : main : 2
Before stream : main : 3
Before stream : main : 4
After parallel stream : main : 6
After parallel stream : ForkJoinPool.commonPool-worker-23 : 2
After parallel stream : ForkJoinPool.commonPool-worker-5 : 4
After parallel stream : ForkJoinPool.commonPool-worker-19 : 8
For Each : ForkJoinPool.commonPool-worker-19 : 16
For Each : ForkJoinPool.commonPool-worker-5 : 8
For Each : ForkJoinPool.commonPool-worker-23 : 4
For Each : main : 12

Notice how the first map is executed sequentially while the remaining operators are executed in parallel.

Observable streams implementations such as RxJava got a different hang of this with the observeOn operator, but they are a totally different way of doing things as well.


问:
Excel VBA - Update unique record on multiple Rows after inserted to table
I need to update data in Table1 after it's been added. The way I add the data currently is in the code below. I do this through a user form that when runs calculates the difference between two dates and then uses that value to create an equal number of rows through a loop.

Adding Data Code:

```<code>Private Sub CommandButton1_Click()

Call sflicker

Dim ArriveDate As Date
Dim DepartDate As Date
Dim DateRangeLength As Long
Dim LoopCounter As Long
Dim RowAfterLast As Long

Set Dbm = ThisWorkbook.Sheets("DBM")

ArriveDate = CDate(Me.Arrive_Date.Value)
DepartDate = CDate(Me.Depart_Date.Value)

DateRangeLength = (DepartDate - ArriveDate)

With DBM

        For LoopCounter = 1 To DateRangeLength
        RowAfterLast = Cells(Rows.Count, "A").End(xlUp).Row + 1
        If LoopCounter = 1 Then

            .Range("A" & RowAfterLast).Value = ID           'ok
            .Range("B" & RowAfterLast).Value = ArriveDate   'ok
            .Range("C" & RowAfterLast).Value = LoopCounter  'ok
            .Range("D" & RowAfterLast).Value = 1            'ok
            .Range("E" & RowAfterLast).Value = ArriveDate   'ok
            .Range("F" & RowAfterLast).Value = DepartDate   'ok

        Else
            .Range("A" & RowAfterLast).Value = ID           'ok
            .Range("B" & RowAfterLast).Value = ArriveDate + (LoopCounter - 1) ' Accrul Date
            .Range("C" & RowAfterLast).Value = LoopCounter  'ok
            .Range("D" & RowAfterLast).Value = 2            'ok
            .Range("E" & RowAfterLast).Value = ArriveDate   'ok
            .Range("F" & RowAfterLast).Value = DepartDate   'ok
        End If
    Next LoopCounter
End With

Add_Renter.Hide

End Sub
</code>```

After I run this. I'm looking for a way to update the data from cells in the workbook. e.g. ArriveDate = A1 and Depart Date = B1. If I change A1 and B1 then run command (macro), I would like the table to update the existing records.

I've thought that if I delete the records based on the unique ID and run the code again - it might do what I need.  Looking for some guidance. Appreciate the help.

I've tried merging this code with no luck.

```<code>row_number = 0
Do
DoEvents
row_number = row_number + 1
item_in_review = Sheets("DBM").Range("A" & row_number)
    If item_in_review = Sheets("Bookings").Range("G31").Text Then

                    Sheets("Bookings").Range("G37").Value = Sheets("DBM").Range("B" & row_number)
                    Sheets("Bookings").Range("N37").Value = Sheets("DBM").Range("C" & row_number)

     End If

Loop Until item_in_review = ""
</code>```

答:
After a few tries, I figured how to delete the initial entries by working backwards up the table. Here is a sample of the code used.

Sub Update_Record_Click()

```<code>Dim DateRangeLength As Long
Dim LoopCounter As Long
Dim RowAfterLast As Long
Dim ArriveDate As Date
Dim DepartDate As Date
Dim BookID As Long
Dim Book As Worksheet
Dim DBA As Worksheet
Dim FirstRow As Long
Dim LastRow As Long
Dim Lrow As Long

Set Book = ThisWorkbook.Sheets("Bookings")
Set CRT = ThisWorkbook.Sheets("CRT")
Set DBA = ThisWorkbook.Sheets("DBA")

BookID = CLng(Book.Range("G35").Value)
ArriveDate = CDate(Book.Range("G41").Value)
DepartDate = CDate(Book.Range("N41").Value)

'Delete first records that were inputed

With DBA

FirstRow = .UsedRange.Cells(1).Row
LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

For Lrow = LastRow To FirstRow Step -1

With .Cells(Lrow, "A")

    If Not IsError(.Value) Then

        If .Value = Book.Range("G35").Value Then .EntireRow.Delete

        End If

    End With

    Next Lrow

End With

'Start Re Add after record delete
DateRangeLength = (DepartDate - ArriveDate)

With DBA
    For LoopCounter = 1 To DateRangeLength
        RowAfterLast = .Cells(Rows.Count, "A").End(xlUp).Row + 1
        If LoopCounter = 1 Then

            .Range("A" & RowAfterLast).Value = BookID
            .Range("B" & RowAfterLast).Value = ArriveDate
            .Range("C" & RowAfterLast).Value = LoopCounter
            .Range("D" & RowAfterLast).Value = 1
            .Range("F" & RowAfterLast).Value = ArriveDate
            .Range("G" & RowAfterLast).Value = DepartDate

        Else
            .Range("A" & RowAfterLast).Value = BookID
            .Range("B" & RowAfterLast).Value = ArriveDate + (LoopCounter - 1)
            .Range("C" & RowAfterLast).Value = LoopCounter
            .Range("D" & RowAfterLast).Value = 2
            .Range("F" & RowAfterLast).Value = ArriveDate
            .Range("G" & RowAfterLast).Value = DepartDate

        End If
    Next LoopCounter
End With

Calculate

End Sub
</code>```

Hope this helps anyone looking to update multiple records in a table in excel with VBA.

Body:
I need to update data in Table1 after it's been added. The way I add the data currently is in the code below. I do this through a user form that when runs calculates the difference between two dates and then uses that value to create an equal number of rows through a loop.

Adding Data Code:

Private Sub CommandButton1_Click()

Call sflicker

Dim ArriveDate As Date
Dim DepartDate As Date
Dim DateRangeLength As Long
Dim LoopCounter As Long
Dim RowAfterLast As Long

Set Dbm = ThisWorkbook.Sheets("DBM")

ArriveDate = CDate(Me.Arrive_Date.Value)
DepartDate = CDate(Me.Depart_Date.Value)

DateRangeLength = (DepartDate - ArriveDate)

With DBM

        For LoopCounter = 1 To DateRangeLength
        RowAfterLast = Cells(Rows.Count, "A").End(xlUp).Row + 1
        If LoopCounter = 1 Then

            .Range("A" & RowAfterLast).Value = ID           'ok
            .Range("B" & RowAfterLast).Value = ArriveDate   'ok
            .Range("C" & RowAfterLast).Value = LoopCounter  'ok
            .Range("D" & RowAfterLast).Value = 1            'ok
            .Range("E" & RowAfterLast).Value = ArriveDate   'ok
            .Range("F" & RowAfterLast).Value = DepartDate   'ok

        Else
            .Range("A" & RowAfterLast).Value = ID           'ok
            .Range("B" & RowAfterLast).Value = ArriveDate + (LoopCounter - 1) ' Accrul Date
            .Range("C" & RowAfterLast).Value = LoopCounter  'ok
            .Range("D" & RowAfterLast).Value = 2            'ok
            .Range("E" & RowAfterLast).Value = ArriveDate   'ok
            .Range("F" & RowAfterLast).Value = DepartDate   'ok
        End If
    Next LoopCounter
End With

Add_Renter.Hide

End Sub

After I run this. I'm looking for a way to update the data from cells in the workbook. e.g. ArriveDate = A1 and Depart Date = B1. If I change A1 and B1 then run command (macro), I would like the table to update the existing records.

I've thought that if I delete the records based on the unique ID and run the code again - it might do what I need.  Looking for some guidance. Appreciate the help.

I've tried merging this code with no luck.

row_number = 0
Do
DoEvents
row_number = row_number + 1
item_in_review = Sheets("DBM").Range("A" & row_number)
    If item_in_review = Sheets("Bookings").Range("G31").Text Then

                    Sheets("Bookings").Range("G37").Value = Sheets("DBM").Range("B" & row_number)
                    Sheets("Bookings").Range("N37").Value = Sheets("DBM").Range("C" & row_number)

     End If

Loop Until item_in_review = ""

Body:
After a few tries, I figured how to delete the initial entries by working backwards up the table. Here is a sample of the code used.

Sub Update_Record_Click()

Dim DateRangeLength As Long
Dim LoopCounter As Long
Dim RowAfterLast As Long
Dim ArriveDate As Date
Dim DepartDate As Date
Dim BookID As Long
Dim Book As Worksheet
Dim DBA As Worksheet
Dim FirstRow As Long
Dim LastRow As Long
Dim Lrow As Long

Set Book = ThisWorkbook.Sheets("Bookings")
Set CRT = ThisWorkbook.Sheets("CRT")
Set DBA = ThisWorkbook.Sheets("DBA")

BookID = CLng(Book.Range("G35").Value)
ArriveDate = CDate(Book.Range("G41").Value)
DepartDate = CDate(Book.Range("N41").Value)

'Delete first records that were inputed

With DBA

FirstRow = .UsedRange.Cells(1).Row
LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

For Lrow = LastRow To FirstRow Step -1

With .Cells(Lrow, "A")

    If Not IsError(.Value) Then

        If .Value = Book.Range("G35").Value Then .EntireRow.Delete

        End If

    End With

    Next Lrow

End With

'Start Re Add after record delete
DateRangeLength = (DepartDate - ArriveDate)

With DBA
    For LoopCounter = 1 To DateRangeLength
        RowAfterLast = .Cells(Rows.Count, "A").End(xlUp).Row + 1
        If LoopCounter = 1 Then

            .Range("A" & RowAfterLast).Value = BookID
            .Range("B" & RowAfterLast).Value = ArriveDate
            .Range("C" & RowAfterLast).Value = LoopCounter
            .Range("D" & RowAfterLast).Value = 1
            .Range("F" & RowAfterLast).Value = ArriveDate
            .Range("G" & RowAfterLast).Value = DepartDate

        Else
            .Range("A" & RowAfterLast).Value = BookID
            .Range("B" & RowAfterLast).Value = ArriveDate + (LoopCounter - 1)
            .Range("C" & RowAfterLast).Value = LoopCounter
            .Range("D" & RowAfterLast).Value = 2
            .Range("F" & RowAfterLast).Value = ArriveDate
            .Range("G" & RowAfterLast).Value = DepartDate

        End If
    Next LoopCounter
End With

Calculate

End Sub

Hope this helps anyone looking to update multiple records in a table in excel with VBA.


问:
scanf strings to structure char pointer
```<code>typedef struct Std{
    char* name;
    char* surname;
    int yearOfBirth;
} Std;
[...]
Std* obj = (Std*)calloc(1, sizeof(Std));
scanf("%s", obj->name);
scanf("%s", obj->surname);
scanf("%d", obj->yearOfBirth);
</code>```

Dont work. How make scanf save the strings to the structure's fields?

答:
With calloc () you allocated for obj the memory space needed by Std structure. But with this action you are not allocating the two char * pointers it contains: name and surname.

Accessing them with scanf means writing to undefined location, and this probably leads to a segmentation fault.

So, just malloc () to them enough space to contain name and surname:

```<code>typedef struct Std{
    char* name;
    char* surname;
    int yearOfBirth;
} Std;

Std* obj = calloc(1, sizeof(Std));
obj->name = malloc(50*sizeof char);
obj->surname = malloc(50*sizeof char);

scanf("%49s", obj->name);
scanf("%49s", obj->surname);
</code>```

Some notes:

In C, casting the result of malloc is not required. It is instead required in C++, so you can omit it unless you are using a C++ compiler

sizeof char is 1. I added it to hilight the type ypu are allocating. It can be omitted or otherwise sizeof(obj->name)` can be used

With %49s I make sure that at most 49 characters + the string terminator are written in my char arrays, avoiding undesirable out of bounds access

As an alternative, you could change the Std definition in order to statically define the size of name and surname char arrays:

```<code>typedef struct Std{
    char name[50];
    char surname[50];
    int yearOfBirth;
} Std;

scanf("%49s", obj->name);
scanf("%49s", obj->surname);
</code>```

Title: scanf strings to structure char pointer

Body:
typedef struct Std{
    char* name;
    char* surname;
    int yearOfBirth;
} Std;
[...]
Std* obj = (Std*)calloc(1, sizeof(Std));
scanf("%s", obj->name);
scanf("%s", obj->surname);
scanf("%d", obj->yearOfBirth);

Dont work. How make scanf save the strings to the structure's fields?

Body:
With calloc () you allocated for obj the memory space needed by Std structure. But with this action you are not allocating the two char * pointers it contains: name and surname.

Accessing them with scanf means writing to undefined location, and this probably leads to a segmentation fault.

So, just malloc () to them enough space to contain name and surname:

typedef struct Std{
    char* name;
    char* surname;
    int yearOfBirth;
} Std;

Std* obj = calloc(1, sizeof(Std));
obj->name = malloc(50*sizeof char);
obj->surname = malloc(50*sizeof char);

scanf("%49s", obj->name);
scanf("%49s", obj->surname);

Some notes:

In C, casting the result of malloc is not required. It is instead required in C++, so you can omit it unless you are using a C++ compiler

sizeof char is 1. I added it to hilight the type ypu are allocating. It can be omitted or otherwise sizeof(obj->name)` can be used

With %49s I make sure that at most 49 characters + the string terminator are written in my char arrays, avoiding undesirable out of bounds access

As an alternative, you could change the Std definition in order to statically define the size of name and surname char arrays:

typedef struct Std{
    char name[50];
    char surname[50];
    int yearOfBirth;
} Std;

scanf("%49s", obj->name);
scanf("%49s", obj->surname);


问:
Sum cols where row value is equal to header - Pandas
I am trying to sum columns in a pandas df where the column is equal to a specific value. Using the df below, where ['X'] == 'GrV', I want to sum cols ('GrV A', 'GrV B'). But I'm returning nan values.

```<code>import pandas as pd

df = pd.DataFrame({
    'X' : ['GrV', 'GrX', 'GrY', 'GrZ', 'GrV', 'GrX', 'GrY', 'GrZ'],
    'GrV A' : [4, 2, 6, 5, 1, 2, 5, 6],
    'GrX A' : [3, 4, 5, 1, 2, 5, 6, 2],
    'GrY A' : [5, 2, 2, 6, 5, 1, 5, 4],
    'GrZ A' : [1, 2, 5, 5, 2, 1, 5, 4],
    'GrV B' : [4, 2, 6, 5, 1, 2, 5, 6],
    'GrX B' : [3, 4, 5, 1, 2, 5, 6, 2],
    'GrY B' : [5, 2, 2, 6, 5, 1, 5, 4],
    'GrZ B' : [1, 2, 5, 5, 2, 1, 5, 4],
        })

df['Total'] = df.loc[(df['X'] == 'GrV'), ('GrV A', 'GrV B')].sum()
df['Total'] = df.loc[(df['X'] == 'GrX'), ('GrX A', 'GrX B')].sum()
df['Total'] = df.loc[(df['X'] == 'GrY'), ('GrY A', 'GrY B')].sum()
df['Total'] = df.loc[(df['X'] == 'GrZ'), ('GrZ A', 'GrZ B')].sum()
</code>```

Intended Output:

```<code>     X  GrV A  GrX A  GrY A  GrZ A  GrV B  GrX B  GrY B  GrZ B  Total
0  GrV      4      3      5      1      4      3      5      1      8
1  GrX      2      4      2      2      2      4      2      2      8
2  GrY      6      5      2      5      6      5      2      5      4
3  GrZ      5      1      6      5      5      1      6      5     10
4  GrV      1      2      5      2      1      2      5      2      2
5  GrX      2      5      1      1      2      5      1      1     10
6  GrY      5      6      5      5      5      6      5      5     10
7  GrZ      6      2      4      4      6      2      4      4      8
</code>```

答:
This is one way about it : append column X to the index, get a pairing of index and columns where values in the new index can be found in the columns, use the resulting values to index the dataframe(with loc), stack, unstack and get the totals

```<code>#append column X to the index
df = df.set_index("X",append=True)

from itertools import product

#get pairing of index and column, where index is in column
index = [(first,second, last)
         for (first,second), last
         in product(df.index,df.columns)
         if second in last]

#stack, index with the index variable,
#unstack, get the sum
#and assign to Total
df['Total'] = df.stack().loc[index].unstack().sum(axis=1)

df

           GrV A    GrX A   GrY A   GrZ A   GrV B   GrX B   GrY B   GrZ B   Total
X
0   GrV      4       3       5       1       4       3       5      1       8.0
1   GrX      2       4       2       2       2       4       2      2       8.0
2   GrY      6       5       2       5       6       5       2      5       4.0
3   GrZ      5       1       6       5       5       1       6      5      10.0
4   GrV      1       2       5       2       1       2       5      2       2.0
5   GrX      2       5       1       1       2       5       1      1      10.0
6   GrY      5       6       5       5       5       6       5      5      10.0
7   GrZ      6       2       4       4       6       2       4      4       8.0
</code>```

Body:
I am trying to sum columns in a pandas df where the column is equal to a specific value. Using the df below, where ['X'] == 'GrV', I want to sum cols ('GrV A', 'GrV B'). But I'm returning nan values.

import pandas as pd

df = pd.DataFrame({
    'X' : ['GrV', 'GrX', 'GrY', 'GrZ', 'GrV', 'GrX', 'GrY', 'GrZ'],
    'GrV A' : [4, 2, 6, 5, 1, 2, 5, 6],
    'GrX A' : [3, 4, 5, 1, 2, 5, 6, 2],
    'GrY A' : [5, 2, 2, 6, 5, 1, 5, 4],
    'GrZ A' : [1, 2, 5, 5, 2, 1, 5, 4],
    'GrV B' : [4, 2, 6, 5, 1, 2, 5, 6],
    'GrX B' : [3, 4, 5, 1, 2, 5, 6, 2],
    'GrY B' : [5, 2, 2, 6, 5, 1, 5, 4],
    'GrZ B' : [1, 2, 5, 5, 2, 1, 5, 4],
        })

df['Total'] = df.loc[(df['X'] == 'GrV'), ('GrV A', 'GrV B')].sum()
df['Total'] = df.loc[(df['X'] == 'GrX'), ('GrX A', 'GrX B')].sum()
df['Total'] = df.loc[(df['X'] == 'GrY'), ('GrY A', 'GrY B')].sum()
df['Total'] = df.loc[(df['X'] == 'GrZ'), ('GrZ A', 'GrZ B')].sum()

Intended Output:

     X  GrV A  GrX A  GrY A  GrZ A  GrV B  GrX B  GrY B  GrZ B  Total
0  GrV      4      3      5      1      4      3      5      1      8
1  GrX      2      4      2      2      2      4      2      2      8
2  GrY      6      5      2      5      6      5      2      5      4
3  GrZ      5      1      6      5      5      1      6      5     10
4  GrV      1      2      5      2      1      2      5      2      2
5  GrX      2      5      1      1      2      5      1      1     10
6  GrY      5      6      5      5      5      6      5      5     10
7  GrZ      6      2      4      4      6      2      4      4      8

Body:
This is one way about it : append column X to the index, get a pairing of index and columns where values in the new index can be found in the columns, use the resulting values to index the dataframe(with loc), stack, unstack and get the totals

#append column X to the index
df = df.set_index("X",append=True)

from itertools import product

#get pairing of index and column, where index is in column
index = [(first,second, last)
         for (first,second), last
         in product(df.index,df.columns)
         if second in last]

#stack, index with the index variable,
#unstack, get the sum
#and assign to Total
df['Total'] = df.stack().loc[index].unstack().sum(axis=1)

df

           GrV A    GrX A   GrY A   GrZ A   GrV B   GrX B   GrY B   GrZ B   Total
X
0   GrV      4       3       5       1       4       3       5      1       8.0
1   GrX      2       4       2       2       2       4       2      2       8.0
2   GrY      6       5       2       5       6       5       2      5       4.0
3   GrZ      5       1       6       5       5       1       6      5      10.0
4   GrV      1       2       5       2       1       2       5      2       2.0
5   GrX      2       5       1       1       2       5       1      1      10.0
6   GrY      5       6       5       5       5       6       5      5      10.0
7   GrZ      6       2       4       4       6       2       4      4       8.0


问:
Firebase not adding realtime data
I have around 3000 entities (key-value) pairs added in firebase db for realtime.
Now I am trying to add more values from my flutter app, but its not adding(not showing). It was working fine with less number of values in db. Why is this happening with bulk data? What has to be solution?

答:
firebaser here

When there are more than a certain number of child nodes, or a certain amount of data, under a location, the Firebase console will stop showing updates in realtime. There is no way to change this behavior, aside from reducing the amount of data or number of child nodes, until the console switches to realtime mode again.

Note: this does not affect the clients/servers that are connected to the same location through the API. They will still be getting all updates in realtime.

Also see:

Firebase : Read-only & non-realtime mode activated to improve browser performance

Title: Firebase not adding realtime data


问:
How to specify a buildspec file contained in a secondary source in Codebuild?
My plan is to create a build project with two sources:

The primary source is the repository of the application I'm building.

A secondary source is a repository containing a generic buildspec.yml and other files like eslint configs.

The reason for that is I want to separate app code from build definition, so I can reuse the same build definition for several apps. The same buildspec.yml and accompanying eslint files are capable of building any of my backend applications.

According to documentation the buildspec path for a build project is a path relative to the root path of primary source.

But what is the correct way of pointing to a buildspec.yml residing in secondary source?

(As far as I know the application code must be the primary source so Codebuild can detect code changes like PRs opened and code push operations.)

(I know that Codebuild allows a S3 path as buildspec path but I don't see how it can help me since my secondary source is a repository.)

Thanks!

答:
My understanding is that you want ONE buildspec that you can reuse for multiple projects with a similar build.  If this is the case, I think you can do this but you need to reverse your primary and secondary sources.

When you create a build project you have to define the buildspec that is going to be used.  Your build project is going to use the buildspec from your PRIMARY source. So the SOURCE that has your primary buildspec will need to be your PRIMARY and the project you are going to build will be your SECONDARY.

Then, in your buildspec you can reference the commands pointing to your SECONDARY source using environment variables.  In your buildspec, you would reference CODEBUILD_SRC_DIR_sourceIdentier.

I have done this with codepipeline having multiple sources.  If you define your output for your secondary source to be called SECONDARY_SOURCE_OUTPUT

Then you would refer to it in your buildspec as $CODEBUILD_SRC_DIR_SECONDARY_SOURCE_OUTPUT/.

Your buildspec exists in your Primary source but you would set your commands would execute from the directory above in your buildspec.  Now you can have several similar projects that have the same build pattern use the same buildspec.

In the case below, I use the same buildspec from the project on the left in the source stage, with different projects that can be pulled in as secondary sources.

This link has some information on multiple sources.


问:
Interacting with slurm through python (to automatically chain jobs)
I need to run 10,000 jobs on slurm (each taking 30 minutes let's say). Currently, the plan was to do it with a job array, using 250 cores in parallel, like so:

```<code>sbatch --array=0-10000%250 array_script.sh args
</code>```

Unfortunately, the sys admin hasn't changed slurm's MaxArraySize (from the default 1001). To "circumvent" this, I was planning on slicing the overall job into 10 pieces, and somehow scheduling each piece so that it runs after the previous piece has finished. For example, I would start with:

```<code>sbatch --array=0-999%250 array_script.sh args
</code>```

then when that is done, I would do:

```<code>sbatch --array=1000-1999%250 array_script.sh args
</code>```

Now I need to somehow schedule this. I'm not that experienced with bash, and I had a python wrapper around everything so I thought I would do it with python (plus I'm using a python wrapper around the job array to do a lot of other stuff anyway). So how would I do this normally?

Currently I have:

```<code>        for i in range(num_slices):
            command = 'sbatch --array={lower_end}-{upper_end}%250 array_script.sh args'.format(lower_end=i*1000, upper_end=min((i+1)*1000-1, num_targets-1), args=args)
            subprocess.run(command, shell=True)
            << need to have a step that waits till the job is done >>
</code>```

First of all, in the above, I run sbatch with subprocess.run, which means I currently don't know the JOB_ID. Is there a way to catch the output from subprocess.run or something that would allow me to find the JOB_ID. And how do I do the equivalent of squeue to check if the job is still running and decide whether to continue the loop?

答:
For other people stumbling onto this question: Your administrators have set a limit on the max number of jobs and number of array jobs for a good reason.

Slurm (and other job schedulers) tends to use resources on the management system proportional to the number of jobs in the queue. Too many jobs, and the scheduler itself will get bogged down, job scheduling will get delayed, which will increase the number of jobs in the queue even further. This can possibly lead to the system grinding to a complete halt.

If you need to submit a lot more jobs that the admins allow you to, the right way to do this is to contact the administrators. Explain what you want to do, and what you are trying to achieve. You may get permission to submit your jobs, or they may know of  a better way to achieve your goals that doesn't involve running quite so many jobs.

HPC system administrators are generally very happy to discuss how to achieve your goals beforehand. They are a lot less happy putting out the fires caused by a user trying to circumvent a limit that was there for a good technical reason.

Body:
I need to run 10,000 jobs on slurm (each taking 30 minutes let's say). Currently, the plan was to do it with a job array, using 250 cores in parallel, like so:

sbatch --array=0-10000%250 array_script.sh args

Unfortunately, the sys admin hasn't changed slurm's MaxArraySize (from the default 1001). To "circumvent" this, I was planning on slicing the overall job into 10 pieces, and somehow scheduling each piece so that it runs after the previous piece has finished. For example, I would start with:

sbatch --array=0-999%250 array_script.sh args

then when that is done, I would do:

sbatch --array=1000-1999%250 array_script.sh args

Now I need to somehow schedule this. I'm not that experienced with bash, and I had a python wrapper around everything so I thought I would do it with python (plus I'm using a python wrapper around the job array to do a lot of other stuff anyway). So how would I do this normally?

Currently I have:

        for i in range(num_slices):
            command = 'sbatch --array={lower_end}-{upper_end}%250 array_script.sh args'.format(lower_end=i*1000, upper_end=min((i+1)*1000-1, num_targets-1), args=args)
            subprocess.run(command, shell=True)
            << need to have a step that waits till the job is done >>

First of all, in the above, I run sbatch with subprocess.run, which means I currently don't know the JOB_ID. Is there a way to catch the output from subprocess.run or something that would allow me to find the JOB_ID. And how do I do the equivalent of squeue to check if the job is still running and decide whether to continue the loop?


问:
Converting a Buffer into a ReadableStream in Node.js
I have a library that takes as input a ReadableStream, but my input is just a base64 format image. I could convert the data I have in a Buffer like so:

```<code>var img = new Buffer(img_string, 'base64');
</code>```

But I have no idea how to convert it to a ReadableStream or convert the Buffer I obtained to a ReadableStream.

Is there a way to do this?

答:
For nodejs 10.17.0 and up:</p>
```<code>const { Readable } = require('stream');

const stream = Readable.from(myBuffer);
</code>```

Body:
I have a library that takes as input a ReadableStream, but my input is just a base64 format image. I could convert the data I have in a Buffer like so:

var img = new Buffer(img_string, 'base64');

But I have no idea how to convert it to a ReadableStream or convert the Buffer I obtained to a ReadableStream.

Is there a way to do this?

Body:
For nodejs 10.17.0 and up:

const { Readable } = require('stream');

const stream = Readable.from(myBuffer);


答:
Here's another way of doing it:</p>

```<code>for col in df['X'].tolist():
    df['Total_'+col] = np.where(df['X']==col, df[col+' A'] + df[col+' B'], np.NaN)

cols = [col for col in df.columns if col.startswith('Total_')]
df['Total'] = df[cols].sum(axis=1)
df.drop(columns=cols, inplace=True)
print(df)

     X  GrV A  GrX A  GrY A  GrZ A  GrV B  GrX B  GrY B  GrZ B  Total
0  GrV      4      3      5      1      4      3      5      1    8.0
1  GrX      2      4      2      2      2      4      2      2    8.0
2  GrY      6      5      2      5      6      5      2      5    4.0
3  GrZ      5      1      6      5      5      1      6      5   10.0
4  GrV      1      2      5      2      1      2      5      2    2.0
5  GrX      2      5      1      1      2      5      1      1   10.0
6  GrY      5      6      5      5      5      6      5      5   10.0
7  GrZ      6      2      4      4      6      2      4      4    8.0
</code>```

Title: Sum cols where row value is equal to header - Pandas

Body:
Here's another way of doing it:

for col in df['X'].tolist():
    df['Total_'+col] = np.where(df['X']==col, df[col+' A'] + df[col+' B'], np.NaN)

cols = [col for col in df.columns if col.startswith('Total_')]
df['Total'] = df[cols].sum(axis=1)
df.drop(columns=cols, inplace=True)
print(df)

     X  GrV A  GrX A  GrY A  GrZ A  GrV B  GrX B  GrY B  GrZ B  Total
0  GrV      4      3      5      1      4      3      5      1    8.0
1  GrX      2      4      2      2      2      4      2      2    8.0
2  GrY      6      5      2      5      6      5      2      5    4.0
3  GrZ      5      1      6      5      5      1      6      5   10.0
4  GrV      1      2      5      2      1      2      5      2    2.0
5  GrX      2      5      1      1      2      5      1      1   10.0
6  GrY      5      6      5      5      5      6      5      5   10.0
7  GrZ      6      2      4      4      6      2      4      4    8.0


问:
For MariaDB, is it necessary to create and close a connection on every function I use?
im have some doubts about good practicing about MariaDB connections uses, i have created a library with some functions, so i create connection/Create cursor/.../close cursor/close connection each Function as it follow:

```<code>def Func1():
    cnx=mariadb.connect(host,user,pass,db)
    cursor=cnx.cursor()
    do...Query and something else
    cnx.commit()
    cursor.close()
    cnx.close()

def Func 2():
    cnx=mariadb.connect(host,user,pass,db)
    cursor=cnx.cursor()
    do...Query and something else
    cnx.commit()
    cursor.close()
    cnx.close()
...
def FuncN():
    cnx=mariadb
    ....
    cnx.close()

Main:
    Func1()
    Func2()
... FuncN()
</code>```

and i would like to know if i can save some lines  doing something like this:

```<code>cnx=mariadb.connect(host,user,pass,db)
cursor=cnx.cursor()

def Func1():
    do...Query and something else
def Func2():
    do...Query and something else
....
Main:
    Func1()
    Func2()
    ....FuncN()
    cnx.commit()
    cursor.close()
    cnx.close()
</code>```

i just want to figure out how i can do my code shorter, im using python 2 by now btw...Thanks

答:
In any programming language, you should connect once and (optionally) disconnect once.

You need some way of using a global variable, singleton object, or otherwise avoid doing two connections.

There are cases where two connections is warranted, but they are very rare.

Usually the platform (eg, web server) will do the disconnect if you fail to do so.

Body:
im have some doubts about good practicing about MariaDB connections uses, i have created a library with some functions, so i create connection/Create cursor/.../close cursor/close connection each Function as it follow:

def Func1():
    cnx=mariadb.connect(host,user,pass,db)
    cursor=cnx.cursor()
    do...Query and something else
    cnx.commit()
    cursor.close()
    cnx.close()

def Func 2():
    cnx=mariadb.connect(host,user,pass,db)
    cursor=cnx.cursor()
    do...Query and something else
    cnx.commit()
    cursor.close()
    cnx.close()
...
def FuncN():
    cnx=mariadb
    ....
    cnx.close()

Main:
    Func1()
    Func2()
... FuncN()

and i would like to know if i can save some lines  doing something like this:

cnx=mariadb.connect(host,user,pass,db)
cursor=cnx.cursor()

def Func1():
    do...Query and something else
def Func2():
    do...Query and something else
....
Main:
    Func1()
    Func2()
    ....FuncN()
    cnx.commit()
    cursor.close()
    cnx.close()

i just want to figure out how i can do my code shorter, im using python 2 by now btw...Thanks


问:
Hough circles on edges
I have the following image in which I've detected the borders, representing 7 circles. In my opinion, it is fairly easy to identify the circles on it, but I am having trouble detecting all circles with the opencv Hough transform. Here it is what I've tried:

```<code>img = cv2.imread('sample.png',0)
edges = cv2.Canny(img,20,120)
circles = cv2.HoughCircles(edges,cv2.HOUGH_GRADIENT,1,100,
                                param2=40,minRadius=0,maxRadius=250)
</code>```

I either get the central circle, the outer one or a lot of circles, depending on the parameters I input on the function. Do you guys have a set of parameters that would output all the circles?

Thanks in advance

答:
Solved with this example from scikit-image adjusting canny thresholds to match the posted image and also the radii range.
Thanks to @barny</p>

Title: Hough circles on edges

Body:
I have the following image in which I've detected the borders, representing 7 circles. In my opinion, it is fairly easy to identify the circles on it, but I am having trouble detecting all circles with the opencv Hough transform. Here it is what I've tried:

img = cv2.imread('sample.png',0)
edges = cv2.Canny(img,20,120)
circles = cv2.HoughCircles(edges,cv2.HOUGH_GRADIENT,1,100,
                                param2=40,minRadius=0,maxRadius=250)

I either get the central circle, the outer one or a lot of circles, depending on the parameters I input on the function. Do you guys have a set of parameters that would output all the circles?

Thanks in advance


问:
How to parse a string with multiple underscores and dashes
I need some guidance and looking for a better way to parse strings.

Example 1:

String: Automation_LOY_Loyalty_PC_CampaignName3-Abandoners-Email1_NoPromo_USA

CSV: Automation, LOY, Loyalty, PC, CampaignName3, Abandoners, Email1, NoPromo, USA

Example 2:

String: 20200601_LOY_Functional_PC_CampaignName1_NoPromo_CAN

CSV: 20200601, LOY, Functional, PC, CampaignName1,,, No Promo, CAN

As you can see, some strings don’t have all the fields, so some csv fields need to be left blank blank.

Right now I’m using this code below and it’s very messy. Is there a better way to handle it other than using CHARINDEX like this?

SELECT [EmailName]
      -- Deployment Type
      ,CASE LEFT([EmailName], (CHARINDEX('_', [EmailName])) - 1)
        WHEN 'Automation' THEN 'Automation'
        ELSE 'AdHoc' END AS [Deployment]
      -- Type
      ,SUBSTRING([EmailName], (CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName])) + 1)) + 1, CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName])) + 1) + 1) - CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName])) + 1) - 1) AS [Type]
      -- Customer_Type
      ,SUBSTRING([EmailName], CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName])) + 1) + 1) + 1, CHARINDEX('_', [EmailName],CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName])) + 1)) + 1) + 1)) - CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName])) + 1)) + 1) - 1) AS [Customer_Type]
      -- Campaign_Name
      ,CASE WHEN (CHARINDEX('-', [EmailName])) = 0 THEN SUBSTRING([EmailName], (CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName])) + 1)) + 1) + 1))) + 1, (CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName])) + 1)) + 1) + 1)) + 1))) - (CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName])) + 1)) + 1) + 1))) - 1)
            WHEN (CHARINDEX('-', [EmailName])) > 0 THEN SUBSTRING([EmailName], (CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName])) + 1)) + 1) + 1))) + 1, ((CHARINDEX('-', [EmailName])) - (CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName])) + 1)) + 1) + 1)))) - 1)
            ELSE NULL END AS [Campaign_Name]
      ,CASE WHEN (CHARINDEX('-', [EmailName])) = 0 THEN 1
            WHEN (CHARINDEX('-', [EmailName])) > 0 THEN REPLACE(SUBSTRING([EmailName], ((CHARINDEX('-', [EmailName])) + 1), (CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName])) + 1)) + 1) + 1)) + 1))) - (CHARINDEX('-', [EmailName])) - 1),'Email','')
            ELSE NULL END AS [Email_Num]
FROM TableName
```

答:
I found STRING_SPLIT and PARSENAME but they weren't helpful. After some thought I precalculated the indexes in subquery.

SELECT
    x.[EmailName]
    ,CASE
        WHEN LEFT(x.[EmailName], Underscore_One - 1) = 'Automation' THEN 'Automation'
        WHEN LEFT(x.[EmailName], Underscore_One - 1) LIKE '[1-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9]' THEN 'Adhoc'
        ELSE NULL END AS [Deployment]
    ,SUBSTRING(x.[EmailName],Underscore_One + 1, Underscore_Two - Underscore_One - 1) AS [BU]
    ,SUBSTRING(x.[EmailName],Underscore_Two + 1, Underscore_Three - Underscore_Two - 1) AS [Type]
    ,SUBSTRING(x.[EmailName],Underscore_Three + 1, Underscore_Four - Underscore_Three - 1)  AS [Customer_Type]
    ,CASE WHEN x.Dash_Nums = 0 THEN SUBSTRING(x.[EmailName],Underscore_Four + 1, Underscore_Five - Underscore_Four - 1)
        WHEN x.Dash_Nums > 0 THEN SUBSTRING(x.[EmailName], Underscore_Four + 1, Dash_One - Underscore_Four - 1)
        ELSE NULL END AS [Campaign_Name]
    ,CASE WHEN x.Dash_Nums = 1 AND SUBSTRING(x.[EmailName], Dash_One + 1, Underscore_Five - Dash_One - 1) LIKE 'Email[0-9]%' THEN REPLACE(SUBSTRING(x.[EmailName], Dash_One + 1, Underscore_Five - Dash_One - 1),'Email','')
        WHEN x.Dash_Nums = 2 THEN REPLACE(SUBSTRING(x.[EmailName], Dash_Two + 1, Underscore_Five - Dash_Two - 1),'Email','')
        ELSE NULL END AS [Email_Num]
    ,CASE WHEN x.Dash_Nums = 2 THEN REPLACE(SUBSTRING(x.[EmailName], Dash_One + 1, Dash_Two - Dash_One - 1),'Email','')
        ELSE NULL END AS [Segment]
    ,CASE SUBSTRING(x.[EmailName],Underscore_Five + 1, Underscore_Six - Underscore_Five - 1)
        WHEN 'Promo' THEN 'Y'
        WHEN 'NoPromo' THEN 'N'
        ELSE NULL END AS [Promo_Flag]

FROM (SELECT [EmailName]
    ,LEN([EmailName]) - LEN(REPLACE([EmailName],'_','')) AS Underscore_Nums
      ,CHARINDEX('_', [EmailName]) AS Underscore_One
      ,CHARINDEX('_', [EmailName], ((CHARINDEX('_', [EmailName])) + 1)) AS Underscore_Two
      ,CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], ((CHARINDEX('_', [EmailName])) + 1)) + 1)) AS Underscore_Three
      ,CHARINDEX('_', [EmailName], ((CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], ((CHARINDEX('_', [EmailName])) + 1)) + 1))) + 1)) AS Underscore_Four
      ,CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], ((CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], ((CHARINDEX('_', [EmailName])) + 1)) + 1))) + 1)) + 1)) AS Underscore_Five
      ,CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], ((CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], ((CHARINDEX('_', [EmailName])) + 1)) + 1))) + 1)) + 1)) + 1)) AS Underscore_Six
      ,CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], ((CHARINDEX('_', [EmailName], (CHARINDEX('_', [EmailName], ((CHARINDEX('_', [EmailName])) + 1)) + 1))) + 1)) + 1)) + 1)) + 1)) AS Underscore_Seven
      ,LEN([EmailName]) - LEN(REPLACE([EmailName],'-','')) AS Dash_Nums
      ,CHARINDEX('-', [EmailName]) AS Dash_One
      ,CHARINDEX('-', [EmailName], (CHARINDEX('-', [EmailName])  + 1)) AS Dash_Two
      ,CHARINDEX('-', [EmailName], (CHARINDEX('-', [EmailName], (CHARINDEX('-', [EmailName])  + 1)) + 1)) AS Dash_Three
      -- Substring(Col, Start Index + 1, End Index - 1)
  FROM [minyoung].[dbo].[Consumer_Marketing_EmailName20200601]
    WHERE Campaign_Name = '') AS x
```


问:
Twilio Receive Fax, Body is Empty
I'm trying to get started with Twilio's Programmable Fax API and I have completed their getting started guide. However, when I receive the fax, I log the request body to the console. However, the body is just an empty object.
I'm not sure what is going wrong.

```<code>const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();

// Parse any incoming POST parameters
app.use(bodyParser.json({ extended: false }));

// Define a handler for when the fax is initially sent
app.post('/fax/sent', (req, res) => {
  // Let's manually build some TwiML. We can choose to receive the
  // fax with <Receive>, or reject with <Reject>.
  console.log(req.body);

  const twiml = `
  <Response>
    <Receive action="/fax/received" mediaType="application/pdf" storeMedia="true"/>
  </Response>
  `;

  // Send Fax twiml response
  res.type('text/xml');
  res.send(twiml);
});

// Define a handler for when the fax is finished sending to us - if successful,
// We will have a URL to the contents of the fax at this point
app.post('/fax/received', (req, res) => {
  // log the URL of the PDF received in the fax
  console.log(req.body);
  // Respond with empty 200/OK to Twilio
  res.status(200);
  res.send(req.body);
});

// Start the web server
http.createServer(app).listen(3000, () => {
  console.log('Express server listening on port 3000');
});
</code>```

And here is what I get back in the console. You can see the empty object that is logged...

```<code>Express server listening on port 3000
{}
</code>```

UPDATE:
I changed the body parser middleware to use urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
And I get the object but I don't see a media url...

答:
BodyParser has updated since their documentation was written. You need to do</p>

```<code>app.use(bodyParser.urlencoded({ extended: false }));
</code>```

Title: Twilio Receive Fax, Body is Empty

Body:
I'm trying to get started with Twilio's Programmable Fax API and I have completed their getting started guide. However, when I receive the fax, I log the request body to the console. However, the body is just an empty object.
I'm not sure what is going wrong.

const http = require('http');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();

// Parse any incoming POST parameters
app.use(bodyParser.json({ extended: false }));

// Define a handler for when the fax is initially sent
app.post('/fax/sent', (req, res) => {
  // Let's manually build some TwiML. We can choose to receive the
  // fax with <Receive>, or reject with <Reject>.
  console.log(req.body);

  const twiml = `
  <Response>
    <Receive action="/fax/received" mediaType="application/pdf" storeMedia="true"/>
  </Response>
  `;

  // Send Fax twiml response
  res.type('text/xml');
  res.send(twiml);
});

// Define a handler for when the fax is finished sending to us - if successful,
// We will have a URL to the contents of the fax at this point
app.post('/fax/received', (req, res) => {
  // log the URL of the PDF received in the fax
  console.log(req.body);
  // Respond with empty 200/OK to Twilio
  res.status(200);
  res.send(req.body);
});

// Start the web server
http.createServer(app).listen(3000, () => {
  console.log('Express server listening on port 3000');
});

And here is what I get back in the console. You can see the empty object that is logged...

Express server listening on port 3000
{}

UPDATE:
I changed the body parser middleware to use urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
And I get the object but I don't see a media url...

Body:
BodyParser has updated since their documentation was written. You need to do

app.use(bodyParser.urlencoded({ extended: false }));