问:
PostgreSQL: creating several pgplsql procedures in a batch
I have a PHP-script with several similar SQL tables (only 1 column varies):

```<code>define('SQL_TABLES', '
create table hide_id (
        ID       varchar(20) references quincytrack,
        USERNAME varchar(20) not null,
        UPDATED  timestamp default current_timestamp
);
create table hide_name (
        NAME     varchar(20) not null,
        USERNAME varchar(20) not null,
        UPDATED  timestamp default current_timestamp
);
create table hide_category (
        CATEGORY varchar(120) not null,
        USERNAME varchar(20) not null,
        UPDATED  timestamp default current_timestamp
);
create table hide_appsversion (
        APPSVERSION varchar(30) not null,
        USERNAME varchar(20) not null,
        UPDATED  timestamp default current_timestamp
);
create table hide_osversion (
        OSVERSION varchar(30) not null,
        USERNAME varchar(20) not null,
        UPDATED  timestamp default current_timestamp
);
</code>```

And I'd like to create an upsert procedure for each of them:

```<code>define('SQL_PROCS', '
create or replace function hide_id_upsert(
        _ID       varchar,
        _USERNAME varchar
        ) returns void as $BODY$
        begin
                update hide_id set
                        ID       = _ID,
                        USERNAME = _USERNAME,
                        UPDATED  = current_timestamp
                where ID = _ID;

                if not found then
                        insert into hide_id (
                                ID,
                                USERNAME
                        ) values (
                                _ID,
                                _USERNAME
                        );
                end if;
        end;
$BODY$ language plpgsql;
');
</code>```

I wonder, if I could do it in a batch by PgPlSQL or PHP means. Something like:

```<code>$columns = array('ID', 'NAME', 'CATEGORY', 'APPSVERSION', 'OSVERSION');
foreach ($columns as $key) {
   $sth = $pg->prepare(sprintf(SQL_PROCS_TEMPLATE, $key, $key, ...);
   $sth->execute();
}
</code>```

but without the above ugliness of having to specify $key numerous times.

Using PostgreSQL 8.4.5 and PHP 5.1.6 under CentOS 5.5 Linux

Thank you! Alex

答:
Well, I don't see you using %s anywhere in the string for your sprintf, but if you want to use the same variable multiple times in the same sprintf string, you can use %1$s for the first argument, like this:

```<code>sprintf('%1$s %1$s', 'hubba') // hubba hubba
</code>```

Edit: here's the link: http://php.net/manual/en/function.sprintf.php. Of course, this is not very PostgreSpecific, but I hope it is what you're looking for.

EDIT: So, in your case, you would do:

```<code>define('SQL_PROCS', '
create or replace function %1$s_upsert(
        _ID       varchar,
        _USERNAME varchar
        ) returns void as $BODY$
        begin
                update %1$s set
                        ID       = _ID,
                        %2$s = _%2$s,
                        UPDATED  = current_timestamp
                where ID = _ID;

                if not found then
                        insert into %1$s (
                                ID,
                                %2$s
                        ) values (
                                _ID,
                                _%2$s
                        );
                end if;
        end;
$BODY$ language plpgsql;
');

$columns = array('ID', 'NAME', 'CATEGORY', 'APPSVERSION', 'OSVERSION');
foreach ($columns as $key) {
   $sth = $pg->prepare(sprintf(SQL_PROCS, 'hide_' . strtolower($key), $key));
   $sth->execute();
}
</code>```

Body:
I have a PHP-script with several similar SQL tables (only 1 column varies):

define('SQL_TABLES', '
create table hide_id (
        ID       varchar(20) references quincytrack,
        USERNAME varchar(20) not null,
        UPDATED  timestamp default current_timestamp
);
create table hide_name (
        NAME     varchar(20) not null,
        USERNAME varchar(20) not null,
        UPDATED  timestamp default current_timestamp
);
create table hide_category (
        CATEGORY varchar(120) not null,
        USERNAME varchar(20) not null,
        UPDATED  timestamp default current_timestamp
);
create table hide_appsversion (
        APPSVERSION varchar(30) not null,
        USERNAME varchar(20) not null,
        UPDATED  timestamp default current_timestamp
);
create table hide_osversion (
        OSVERSION varchar(30) not null,
        USERNAME varchar(20) not null,
        UPDATED  timestamp default current_timestamp
);

And I'd like to create an upsert procedure for each of them:

define('SQL_PROCS', '
create or replace function hide_id_upsert(
        _ID       varchar,
        _USERNAME varchar
        ) returns void as $BODY$
        begin
                update hide_id set
                        ID       = _ID,
                        USERNAME = _USERNAME,
                        UPDATED  = current_timestamp
                where ID = _ID;

                if not found then
                        insert into hide_id (
                                ID,
                                USERNAME
                        ) values (
                                _ID,
                                _USERNAME
                        );
                end if;
        end;
$BODY$ language plpgsql;
');

I wonder, if I could do it in a batch by PgPlSQL or PHP means. Something like:

$columns = array('ID', 'NAME', 'CATEGORY', 'APPSVERSION', 'OSVERSION');
foreach ($columns as $key) {
   $sth = $pg->prepare(sprintf(SQL_PROCS_TEMPLATE, $key, $key, ...);
   $sth->execute();
}

but without the above ugliness of having to specify $key numerous times.

Using PostgreSQL 8.4.5 and PHP 5.1.6 under CentOS 5.5 Linux

Thank you! Alex

Body:
Well, I don't see you using %s anywhere in the string for your sprintf, but if you want to use the same variable multiple times in the same sprintf string, you can use %1$s for the first argument, like this:

sprintf('%1$s %1$s', 'hubba') // hubba hubba

Edit: here's the link: http://php.net/manual/en/function.sprintf.php. Of course, this is not very PostgreSpecific, but I hope it is what you're looking for.

EDIT: So, in your case, you would do:

define('SQL_PROCS', '
create or replace function %1$s_upsert(
        _ID       varchar,
        _USERNAME varchar
        ) returns void as $BODY$
        begin
                update %1$s set
                        ID       = _ID,
                        %2$s = _%2$s,
                        UPDATED  = current_timestamp
                where ID = _ID;

                if not found then
                        insert into %1$s (
                                ID,
                                %2$s
                        ) values (
                                _ID,
                                _%2$s
                        );
                end if;
        end;
$BODY$ language plpgsql;
');

$columns = array('ID', 'NAME', 'CATEGORY', 'APPSVERSION', 'OSVERSION');
foreach ($columns as $key) {
   $sth = $pg->prepare(sprintf(SQL_PROCS, 'hide_' . strtolower($key), $key));
   $sth->execute();
}


来源: stackexchange

问:
How can I parse a line of potentially quoted search terms, and how can I use these terms to match input lines that each contain all of them?
I'm using Bash on Mac OS X El Capitan.

My main goal:

Allow a user to input any number of search terms, including ones containing quotes.

Iterate through a text file and show the user every line that contains matches for all search terms input.

I have a text file that is essentially an index of files on various servers. E.g.,

```<code>/Volumes/Server1/Resources/Images/this.jpg
/Volumes/Server2/Inventory/docs/that.doc
/Volumes/Server6/Projects/Project 32/the other.pdf
/Volumes/Server6/Projects/Project 32/audio video training.doc
</code>```

I'm using

```<code>read -r sSearchTerms
</code>```

to let a user input search criteria. I basically just want to parse whatever she enters so I can allow her to search based on multiple terms. I also want to allow the use of quotes to search for terms containing spaces.

For example, a user might enter "Project 32" "audio video" doc

In this case, I want to compare 3 terms to each line of my index text file: Project 32, audio video, and doc, building a results.txt file that I can then easily show the user.

The two main things I need to figure out:

How to parse the input line properly into separate strings to use for comparison purposes (making sure that anything inside quotes is treated as a single search term (and removing the quotes before comparing)). I'm imagining using an array?

stringCompare[0]="Project 32"

stringCompare[1]="audio video"

stringCompare[2]="doc"

How to properly test each line of the text file to see if it contains matches for all search terms input.

Below is my working code for comparing the whole input line as a single search term.

```<code>#!/bin/bash

SEARCH_FILE="/Users/User/Desktop/SEARCH_TEST_2.txt"
RESULTS_FILE="results.txt"
# -i ignores case
GREP_OPTS="-i"

echo "PLEASE ENTER YOUR SEARCH:"

# -r treats backslash as a backslash, instead of an escape character.
read -r sSearchTerms

echo " Searching..."

grep $GREP_OPTS "$sSearchTerms" "$SEARCH_FILE">>"$RESULTS_FILE"

echo " All Done! "

# -t = open with default text editor
open -t "$RESULTS_FILE"

osascript -e 'tell application "Terminal" to quit' &
exit
</code>```

I'm imagining replacing everything after the

```<code>read -r sSearchTerms
</code>```

with this:

```<code>strQuotes='"'
numberOfQuotes=$(grep -o "$strQuotes" <<< "$sSearchTerms" | wc -l)

if [ "$(($numberOfQuotes%2))" != "0" ]
then
    echo "ODD number of quotes"
    # Can't properly parse an odd number of quotes, so Abort!
else
    echo "EVEN number of quotes"
    # We're good to go on quotes, so go ahead and process

    # Create or overwrite the results file
    echo "">"$RESULTS_FILE"

    # CODE HERE to parse input

    # CODE HERE to compare terms to index and build results file
fi

echo " All Done! "
open -t "$RESULTS_FILE"

osascript -e 'tell application "Terminal" to quit' &
exit
</code>```

For safety and ease of coding, I already test to make sure that the user inputs an even number of quotes (0, 2, 4, ...). If not, I'll just throw a message and make the user try again.

答:
Splitting a list of potentially quoted terms into an array of unquoted terms:

Assuming that you don't need to supported embedded " instances inside "..."-quoted search terms, you can use xargs to split your list of search terms into individual, unquoted terms, because xargs recognizes double-and single-quoted tokens:

```<code>#!/bin/bash

# Prompt the user for a list of potentially quoted search terms.
read -r -p 'PLEASE ENTER YOUR SEARCH: ' termList

# Split the list of terms into an array of unquoted terms.
IFS=$'\n' read -d '' -ra terms < <(xargs printf '%s\n' <<<"$termList")
</code>```

With the sample input ("Project 32" "audio video" doc), if you run declare -p terms after the above, you get:

```<code>declare -a terms='([0]="Project 32" [1]="audio video" [2]="doc")'
</code>```

This demonstrates that the list was split into unquoted search terms correctly (the " around the element values are not part of the values themselves, they're just an artifact of printing the array's contents with declare -p).

Searching for lines that EACH contain ALL of multiple search terms:

Passing multiple search terms to grep only supports disjunctive logic: any line that matches any of those terms matches.

Therefore, you must roll your own conjunctive logic, i.e., to match only lines that contain all terms.

While you could call grep in a loop, that would be inefficient, so awk is the better choice:

```<code># Search each line of the input file for ALL terms entered and print only
# matching lines.
awk '
  NR==FNR { terms[++i] = $0; next }
  { for(i in terms) { if (index(tolower($0), terms[i]) == 0) next } print }
' <(printf '%s\n' "${terms[@]}" | tr '[:upper:]' '[:lower:]') file
</code>```

Note:

The above performs literal substring matching, as I presume you don't want to support users entering regular expressions as search terms.

If you do, use the !~ operator instead of the index() call, but be aware that the regex dialect supported by BSD awk has fewer features than BSD grep's.

BSD awk, as found on macOS, has the following limitations:

Case-insensitive matching is not supported, hence the need to convert both the terms (tr '[:upper:]' '[:lower:]') and each input line (tolower($0)) to lowercase before matching.

Even then, matching will only work with ASCII-range characters, because BSD awk is not Unicode-aware.

If we put it all together:

```<code>#!/bin/bash

# Determine filenames.
# Note: Better not to use all-uppercase variable names in Bash, because
#       they can conflict with special environment and shell variables.
searchFile="$HOME/Desktop/SEARCH_TEST_2.txt"
resultsFile='results.txt'

# Prompt the user for a list of potentially quoted search terms.
read -r -p 'PLEASE ENTER YOUR SEARCH: ' termList

# Split the list of terms into an array of unquoted terms.
IFS=$'\n' read -d '' -ra terms < <(xargs printf '%s\n' <<<"$termList")

echo 'Searching...'

# Search each line of the input file for ALL terms entered and print only
# matching lines.
awk '
  NR==FNR { terms[++i] = $0; next }
  { for(i in terms) { if (index(tolower($0), terms[i]) == 0) next } print }
' <(printf '%s\n' "${terms[@]}" | tr '[:upper:]' '[:lower:]') "$searchFile" >"$resultsFile"

echo "All Done!"

open -t "$resultsFile"

osascript -e 'tell application "Terminal" to quit' &
exit
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I'm using Bash on Mac OS X El Capitan.

My main goal:

Allow a user to input any number of search terms, including ones containing quotes.

Iterate through a text file and show the user every line that contains matches for all search terms input.

I have a text file that is essentially an index of files on various servers. E.g.,

/Volumes/Server1/Resources/Images/this.jpg
/Volumes/Server2/Inventory/docs/that.doc
/Volumes/Server6/Projects/Project 32/the other.pdf
/Volumes/Server6/Projects/Project 32/audio video training.doc

I'm using

read -r sSearchTerms

to let a user input search criteria. I basically just want to parse whatever she enters so I can allow her to search based on multiple terms. I also want to allow the use of quotes to search for terms containing spaces.

For example, a user might enter "Project 32" "audio video" doc

In this case, I want to compare 3 terms to each line of my index text file: Project 32, audio video, and doc, building a results.txt file that I can then easily show the user.

The two main things I need to figure out:

How to parse the input line properly into separate strings to use for comparison purposes (making sure that anything inside quotes is treated as a single search term (and removing the quotes before comparing)). I'm imagining using an array?

stringCompare[0]="Project 32"

stringCompare[1]="audio video"

stringCompare[2]="doc"

How to properly test each line of the text file to see if it contains matches for all search terms input.

Below is my working code for comparing the whole input line as a single search term.

#!/bin/bash

SEARCH_FILE="/Users/User/Desktop/SEARCH_TEST_2.txt"
RESULTS_FILE="results.txt"
# -i ignores case
GREP_OPTS="-i"

echo "PLEASE ENTER YOUR SEARCH:"

# -r treats backslash as a backslash, instead of an escape character.
read -r sSearchTerms

echo " Searching..."

grep $GREP_OPTS "$sSearchTerms" "$SEARCH_FILE">>"$RESULTS_FILE"

echo " All Done! "

# -t = open with default text editor
open -t "$RESULTS_FILE"

osascript -e 'tell application "Terminal" to quit' &
exit

I'm imagining replacing everything after the

read -r sSearchTerms

with this:

strQuotes='"'
numberOfQuotes=$(grep -o "$strQuotes" <<< "$sSearchTerms" | wc -l)

if [ "$(($numberOfQuotes%2))" != "0" ]
then
    echo "ODD number of quotes"
    # Can't properly parse an odd number of quotes, so Abort!
else
    echo "EVEN number of quotes"
    # We're good to go on quotes, so go ahead and process

    # Create or overwrite the results file
    echo "">"$RESULTS_FILE"

    # CODE HERE to parse input

    # CODE HERE to compare terms to index and build results file
fi

echo " All Done! "
open -t "$RESULTS_FILE"

osascript -e 'tell application "Terminal" to quit' &
exit

For safety and ease of coding, I already test to make sure that the user inputs an even number of quotes (0, 2, 4, ...). If not, I'll just throw a message and make the user try again.

Body:
Splitting a list of potentially quoted terms into an array of unquoted terms:

Assuming that you don't need to supported embedded " instances inside "..."-quoted search terms, you can use xargs to split your list of search terms into individual, unquoted terms, because xargs recognizes double-and single-quoted tokens:

#!/bin/bash

# Prompt the user for a list of potentially quoted search terms.
read -r -p 'PLEASE ENTER YOUR SEARCH: ' termList

# Split the list of terms into an array of unquoted terms.
IFS=$'\n' read -d '' -ra terms < <(xargs printf '%s\n' <<<"$termList")

With the sample input ("Project 32" "audio video" doc), if you run declare -p terms after the above, you get:

declare -a terms='([0]="Project 32" [1]="audio video" [2]="doc")'

This demonstrates that the list was split into unquoted search terms correctly (the " around the element values are not part of the values themselves, they're just an artifact of printing the array's contents with declare -p).

Searching for lines that EACH contain ALL of multiple search terms:

Passing multiple search terms to grep only supports disjunctive logic: any line that matches any of those terms matches.

Therefore, you must roll your own conjunctive logic, i.e., to match only lines that contain all terms.

While you could call grep in a loop, that would be inefficient, so awk is the better choice:

# Search each line of the input file for ALL terms entered and print only
# matching lines.
awk '
  NR==FNR { terms[++i] = $0; next }
  { for(i in terms) { if (index(tolower($0), terms[i]) == 0) next } print }
' <(printf '%s\n' "${terms[@]}" | tr '[:upper:]' '[:lower:]') file

Note:

The above performs literal substring matching, as I presume you don't want to support users entering regular expressions as search terms.

If you do, use the !~ operator instead of the index() call, but be aware that the regex dialect supported by BSD awk has fewer features than BSD grep's.

BSD awk, as found on macOS, has the following limitations:

Case-insensitive matching is not supported, hence the need to convert both the terms (tr '[:upper:]' '[:lower:]') and each input line (tolower($0)) to lowercase before matching.

Even then, matching will only work with ASCII-range characters, because BSD awk is not Unicode-aware.

If we put it all together:

#!/bin/bash

# Determine filenames.
# Note: Better not to use all-uppercase variable names in Bash, because
#       they can conflict with special environment and shell variables.
searchFile="$HOME/Desktop/SEARCH_TEST_2.txt"
resultsFile='results.txt'

# Prompt the user for a list of potentially quoted search terms.
read -r -p 'PLEASE ENTER YOUR SEARCH: ' termList

# Split the list of terms into an array of unquoted terms.
IFS=$'\n' read -d '' -ra terms < <(xargs printf '%s\n' <<<"$termList")

echo 'Searching...'

# Search each line of the input file for ALL terms entered and print only
# matching lines.
awk '
  NR==FNR { terms[++i] = $0; next }
  { for(i in terms) { if (index(tolower($0), terms[i]) == 0) next } print }
' <(printf '%s\n' "${terms[@]}" | tr '[:upper:]' '[:lower:]') "$searchFile" >"$resultsFile"

echo "All Done!"

open -t "$resultsFile"

osascript -e 'tell application "Terminal" to quit' &
exit


问:
How to fix getimagedata the operation is insecure
Using fairly basic code.

HTML

```<code><img id="img" style="max-width: 100%; max-height: 650px; " />
</code>```

Javascript

```<code>var image = document.getElementById("img");

image.crossOrigin = "anonymous";
image.addEventListener('load', function (event) {
    alert("here");

    orgImg.width = this.naturalWidth;
    orgImg.height = this.naturalHeight;
    orgImg.getContext("2d").drawImage(this, 0, 0);

    //etc
    orgImg.getContext("2d").getImageData(0, 0, orgImg.width, orgImg.height) //security error
}, {once: true});
image.src = '<?= $this->imgSrc ?>';
</code>```

imgSrc is a URL that is not on the same domain.  The site is on localhost, the image is on another site, and is https.

If I remove the image.crossOrigin = "anonymous";, the image loads, but I get the security error.  If I put back the cross origin, the image doesn't load at all, the load handler is never fired.

Moving the image to the server serving the page is not a realistic option as the image would be stuck there until another service or process cleaned it out.

What did I miss?

Thanks

答:
I was able to fix this using somewhat spine curling code.

In my php, I did this.

```<code>        $imgSrc= $this->_request->getParam('imgSrc', ''); //URL of image
        if (!empty($imgSrc)) {
            $imgSrc = file_get_contents($imgSrc); //naughty variable reuse
            $imgSrc = 'data:image/x-icon;base64,' . base64_encode($imgSrc);
        }
</code>```

This way the original image isn't used, and instead an epic string is sent instead, which doesn't cause the security issue.
Then to alleviate memory, I added image.src=""; to the end of the load event.

```<code>var image = document.getElementById("img");

image.addEventListener('load', function (event) {
    alert("here");

    orgImg.width = this.naturalWidth;
    orgImg.height = this.naturalHeight;
    orgImg.getContext("2d").drawImage(this, 0, 0);
    image.src=""; //clear the massive string from memory

    //etc
    orgImg.getContext("2d").getImageData(0, 0, orgImg.width, orgImg.height) //security error
}, {once: true});
image.src = '<?= $this->imgSrc ?>';
</code>```

This page will never be accessed by mobile so it's ok to use crazy RAM for a moment.  But a better solution would be good if anyone has one.

Thanks

Body:
Using fairly basic code.

HTML

Javascript

var image = document.getElementById("img");

image.crossOrigin = "anonymous";
image.addEventListener('load', function (event) {
    alert("here");

    orgImg.width = this.naturalWidth;
    orgImg.height = this.naturalHeight;
    orgImg.getContext("2d").drawImage(this, 0, 0);

    //etc
    orgImg.getContext("2d").getImageData(0, 0, orgImg.width, orgImg.height) //security error
}, {once: true});
image.src = '<?= $this->imgSrc ?>';

imgSrc is a URL that is not on the same domain.  The site is on localhost, the image is on another site, and is https.

If I remove the image.crossOrigin = "anonymous";, the image loads, but I get the security error.  If I put back the cross origin, the image doesn't load at all, the load handler is never fired.

Moving the image to the server serving the page is not a realistic option as the image would be stuck there until another service or process cleaned it out.

What did I miss?

Thanks

Body:
I was able to fix this using somewhat spine curling code.

In my php, I did this.

        $imgSrc= $this->_request->getParam('imgSrc', ''); //URL of image
        if (!empty($imgSrc)) {
            $imgSrc = file_get_contents($imgSrc); //naughty variable reuse
            $imgSrc = 'data:image/x-icon;base64,' . base64_encode($imgSrc);
        }

This way the original image isn't used, and instead an epic string is sent instead, which doesn't cause the security issue.
Then to alleviate memory, I added image.src=""; to the end of the load event.

var image = document.getElementById("img");

image.addEventListener('load', function (event) {
    alert("here");

    orgImg.width = this.naturalWidth;
    orgImg.height = this.naturalHeight;
    orgImg.getContext("2d").drawImage(this, 0, 0);
    image.src=""; //clear the massive string from memory

    //etc
    orgImg.getContext("2d").getImageData(0, 0, orgImg.width, orgImg.height) //security error
}, {once: true});
image.src = '<?= $this->imgSrc ?>';

This page will never be accessed by mobile so it's ok to use crazy RAM for a moment.  But a better solution would be good if anyone has one.

Thanks


问:
Set max_input_var on 1and1 Server for a Prstashop 1.6
I have a Prestashop 1,6 with 1and1 as hosting Service and cant use the Translation Service in the Backend because of the max_input_vars settings.

Warning! Your PHP configuration limits the maximum number of fields allowed in a form: 5000 for max_input_vars.
Please ask your hosting provider to increase this limit to 5271 at least, or you will have to edit the translation files.

I tryed to set the max_input_vars settings to 6000 and didnt get a error on the server but it wont work has someone a solution to fix it or knows how to change it?

P.S. here is the PHP file i used

```<code><?php
error_reporting(E_ALL);
ini_set('max_input_vars', 6000);
?>
</code>```

答:
if you have shared hosting then instead of php.ini you have create a file name .user.ini and
max_input_vars=6000;
and upload on root. it will take time to reflect changes based on server settings.

Body:
I have a Prestashop 1,6 with 1and1 as hosting Service and cant use the Translation Service in the Backend because of the max_input_vars settings.

Warning! Your PHP configuration limits the maximum number of fields allowed in a form: 5000 for max_input_vars.
Please ask your hosting provider to increase this limit to 5271 at least, or you will have to edit the translation files.

I tryed to set the max_input_vars settings to 6000 and didnt get a error on the server but it wont work has someone a solution to fix it or knows how to change it?

P.S. here is the PHP file i used

<?php
error_reporting(E_ALL);
ini_set('max_input_vars', 6000);
?>


问:
Python decorator get or set dictionary value in class
I'm working on a class representing on object with numerous associated data. I'm storing these data in a dictionary class attribute called metadata. A representation could be:

```<code>{'key1':slowToComputeValue, 'key2':evenSlowerToComputeValue}
</code>```

The calculating of the values is in some cases very slow, so what I want to do is, using "getter" functions, first try and get the value from the metadata  dict. Only on a KeyError (i.e. when the getter tries to get a value for a key which doesn't exist yet) should the value be calculated (and added to the dictionary for fast access next time the getter is called).

I began with a simple:

```<code>try:
    return self.metadata[requested_key]
except KeyError:
    #Implementation of function
</code>```

As there are many getters in the class, I started thought that these first 3 lines of code could be handled by a decorator. However I'm having problems making this work. The problem is that I need to pass the metadata dictionary from the class instance to the decorator. I've found several tutorials and posts like this one which show that it is possible to send a parameter to an enclosing function but the difficulty I'm having is sending a class instantiation attribute metadata to it (if I send a string value it works).

Some example code from my attempt is here:

```<code>def get_existing_value_from_metadata_if_exists(metadata):
    def decorator(function):
        @wraps(function)
        def decorated(*args, **kwargs):
            function_name = function.__name__
            if function_name in metadata.keys():
                return metadata[function_name]
            else:
                function(*args, **kwargs)
        return decorated
    return decorator

class my_class():
    @get_existing_value_from_metadata_if_exists(metadata)
    def get_key1(self):
        #Costly value calculation and add to metadata

    @get_existing_value_from_metadata_if_exists(metadata)
    def get_key2(self):
        #Costly value calculation and add to metadata

    def __init__(self):
        self.metadata = {}
</code>```

The errors I'm getting are generally self not defined but I've tried various combinations of parameter placement, decorator placement etc. without success.

So my questions are:

How can I make this work?

Are decorators a suitable way to achieve what I'm trying to do?

答:
Yes, a decorator is a good use case for this. Django for example has something similar already included with it, it's called cached_property.

Basically all it does is that when the property is accessed first time it will store the data in instance's dict(__dict__) by the same name as the function. When we fetch the same property later on it simple fetches the value from the instance dictionary.

A cached_property is a non-data descriptor. Hence once the key is set in instance's dictionary, the access to property would always get the value from there.

```<code>class cached_property(object):
    """
    Decorator that converts a method with a single self argument into a
    property cached on the instance.

    Optional ``name`` argument allows you to make cached properties of other
    methods. (e.g.  url = cached_property(get_absolute_url, name='url') )
    """
    def __init__(self, func, name=None):
        self.func = func
        self.__doc__ = getattr(func, '__doc__')
        self.name = name or func.__name__

    def __get__(self, instance, cls=None):
        if instance is None:
            return self
        res = instance.__dict__[self.name] = self.func(instance)
        return res
</code>```

In your case:

```<code>class MyClass:
    @cached_property
    def key1(self):
        #Costly value calculation and add to metadata

    @cached_property
    def key2(self):
        #Costly value calculation and add to metadata

    def __init__(self):
        # self.metadata not required
</code>```

Use the name argument to convert an existing method to cached property.

```<code>class MyClass:
    def __init__(self, data):
        self.data = data

    def get_total(self):
        print('Processing...')
        return sum(self.data)

    total = cached_property(get_total, 'total')
</code>```

Demo:

```<code>>>> m = MyClass(list(range(10**5)))

>>> m.get_total()
Processing...
4999950000

>>> m.total
Processing...
4999950000

>>> m.total
4999950000

>>> m.data.append(1000)

>>> m.total  # This is now invalid
4999950000

>>> m.get_total()  # This still works
Processing...
4999951000

>>> m.total
4999950000
</code>```

Based on the example above we can see that we can use total as long as we know the internal data hasn't been updated yet, hence saving processing time. But it doesn't make get_total() redundant, as it can get the correct total based on the data.

Another example could be that our public facing client was using something(say get_full_name()) as method so far but we realised that it would be more appropriate to use it as a property(just full_name), in that case it makes sense to keep the method intact but mark it as deprecated and start suggesting the users to use the new property from now on.

Body:
I'm working on a class representing on object with numerous associated data. I'm storing these data in a dictionary class attribute called metadata. A representation could be:

{'key1':slowToComputeValue, 'key2':evenSlowerToComputeValue}

The calculating of the values is in some cases very slow, so what I want to do is, using "getter" functions, first try and get the value from the metadata  dict. Only on a KeyError (i.e. when the getter tries to get a value for a key which doesn't exist yet) should the value be calculated (and added to the dictionary for fast access next time the getter is called).

I began with a simple:

try:
    return self.metadata[requested_key]
except KeyError:
    #Implementation of function

As there are many getters in the class, I started thought that these first 3 lines of code could be handled by a decorator. However I'm having problems making this work. The problem is that I need to pass the metadata dictionary from the class instance to the decorator. I've found several tutorials and posts like this one which show that it is possible to send a parameter to an enclosing function but the difficulty I'm having is sending a class instantiation attribute metadata to it (if I send a string value it works).

Some example code from my attempt is here:

def get_existing_value_from_metadata_if_exists(metadata):
    def decorator(function):
        @wraps(function)
        def decorated(*args, **kwargs):
            function_name = function.__name__
            if function_name in metadata.keys():
                return metadata[function_name]
            else:
                function(*args, **kwargs)
        return decorated
    return decorator

class my_class():
    @get_existing_value_from_metadata_if_exists(metadata)
    def get_key1(self):
        #Costly value calculation and add to metadata

    @get_existing_value_from_metadata_if_exists(metadata)
    def get_key2(self):
        #Costly value calculation and add to metadata

    def __init__(self):
        self.metadata = {}

The errors I'm getting are generally self not defined but I've tried various combinations of parameter placement, decorator placement etc. without success.

So my questions are:

How can I make this work?

Are decorators a suitable way to achieve what I'm trying to do?

Body:
Yes, a decorator is a good use case for this. Django for example has something similar already included with it, it's called cached_property.

Basically all it does is that when the property is accessed first time it will store the data in instance's dict(__dict__) by the same name as the function. When we fetch the same property later on it simple fetches the value from the instance dictionary.

A cached_property is a non-data descriptor. Hence once the key is set in instance's dictionary, the access to property would always get the value from there.

class cached_property(object):
    """
    Decorator that converts a method with a single self argument into a
    property cached on the instance.

    Optional ``name`` argument allows you to make cached properties of other
    methods. (e.g.  url = cached_property(get_absolute_url, name='url') )
    """
    def __init__(self, func, name=None):
        self.func = func
        self.__doc__ = getattr(func, '__doc__')
        self.name = name or func.__name__

    def __get__(self, instance, cls=None):
        if instance is None:
            return self
        res = instance.__dict__[self.name] = self.func(instance)
        return res

In your case:

class MyClass:
    @cached_property
    def key1(self):
        #Costly value calculation and add to metadata

    @cached_property
    def key2(self):
        #Costly value calculation and add to metadata

    def __init__(self):
        # self.metadata not required

Use the name argument to convert an existing method to cached property.

class MyClass:
    def __init__(self, data):
        self.data = data

    def get_total(self):
        print('Processing...')
        return sum(self.data)

    total = cached_property(get_total, 'total')

Demo:

>>> m = MyClass(list(range(10**5)))

>>> m.get_total()
Processing...
4999950000

>>> m.total
Processing...
4999950000

>>> m.total
4999950000

>>> m.data.append(1000)

>>> m.total  # This is now invalid
4999950000

>>> m.get_total()  # This still works
Processing...
4999951000

>>> m.total
4999950000

Based on the example above we can see that we can use total as long as we know the internal data hasn't been updated yet, hence saving processing time. But it doesn't make get_total() redundant, as it can get the correct total based on the data.

Another example could be that our public facing client was using something(say get_full_name()) as method so far but we realised that it would be more appropriate to use it as a property(just full_name), in that case it makes sense to keep the method intact but mark it as deprecated and start suggesting the users to use the new property from now on.


问:
Maths related task having trouble solving using python
I'm having trouble solving this question, using python.

When the middle digit of a three-digit number N is removed, the two-digit number thus obtained is N divided by 16. Find all such numbers n.

Can anyone answer the question and explain?

EDIT: The number N when divided by 16 has to equal that 2 digit number, but most of your answers don't work.

答:
From what I understand, if we call S the number given by the first and last digit of N. You are looking for all N where S = N/16.
I solve it with the following solution :

```<code>i = 7 # first i where i*16 > 100

while i*16 < 1000:
    N_num = i * 16
    N = str(N_num)
    S = N[0] + N[-1]
    S_num = int(S)
    if S_num == N_num/16:
        print(N_num)
    i += 1
</code>```

Answer:

160 (S = 10 and 10*16 = 160 = N)

192 (S = 12 and 12*16 = 160 = N)

Body:
From what I understand, if we call S the number given by the first and last digit of N. You are looking for all N where S = N/16.
I solve it with the following solution :

i = 7 # first i where i*16 > 100

while i*16 < 1000:
    N_num = i * 16
    N = str(N_num)
    S = N[0] + N[-1]
    S_num = int(S)
    if S_num == N_num/16:
        print(N_num)
    i += 1

Answer:

160 (S = 10 and 10*16 = 160 = N)

192 (S = 12 and 12*16 = 160 = N)


问:
PyCharm 2016 and lline waprs on copy/pasting
can someone tell me if theres some way to disable the autoformating, when copy/pasting?

Every time i paste some line, thats longer than PEP-8-Max line, PyCharm automaticly inserts line warps. Thats realy anoying.

I'm using the professional Version.

Many thanks
rene

答:
(On a mac)

To get my editor to stop auto wrapping long lines of code I did this.

PyCharm --> Preferences --> Editor --> Code Style --> Default Options --> Right margin (columns): 999

The editor wouldn't let me set a value larger than 999.

It's not perfect but it reduced the annoyance factor quite a bit for me.

Hope it helps.


问:
Find the opposite of a direction angle (360 degrees) in JavaScript
The following question: https://math.stackexchange.com/questions/1319615/how-to-calculate-opposite-direction-angle shows the formula for doing this. How would I write this: (α+180) mod 360 in javascript. (a is a variable)

答:
You should use the % operator:

var angle = 210;
var oppositeAngle = (angle + 180) % 360;
console.log(oppositeAngle);```


问:
Using Firebase Cloud Functions to Update Hosted File
I am building a site that is using Angular 4, Firebase, and Firebase Cloud Functions. What I am looking to do is when certain data is added to the database I want to add, remove, or update something in an RSS feed, sitemap, and JSON feed which are files hosted with Firebase Hosting.

Any suggestions on how to accomplish this or an alternative if it is not possible without manual updates and redeploys of the project?

答:
There is currently no way to publish a new (or update a) file to Firebase Hosting based on a database (or other dynamic) trigger.

What is possible is to map a Cloud Function to a path on your site (e.g. /sitemap or /feed), generate the HTML in that Cloud Function, and then set caching headers on the result.


问:
Resharper C++ Lambda Braces Indentation
I am using Resharper C++.

I have code that often uses lambdas as function parameters, and I want them aligned as such:

```<code>return Method(
    [&]() -> bool
    {
        return this.variable >= 0;
    },
    "a normal parameter");
</code>```

i.e. I want all parameters aligned on "one clear vertical line".

However, Resharper will align it as such:

```<code>return Method(
    [&]() -> bool
{
    return this.variable >= 0;
},
    "a normal parameter");
</code>```

Which is obviously far less readably (especially when working with functions that use many lambdas and many "normal" parameters.

Neither

RESHARPER -> Options -> C++ -> Formatting Style -> Braces Layout -> Other statements and blocks

nor

RESHARPER -> Options -> C++ -> Formatting Style -> Braces Layout -> Other braces

seemed to work (or work without undesired side effects).

I am looking for a solution as described here: Resharper C# lambda indentation but I can't seem to find one for Resharper C++.

Now, my questions are:

Is the way I want to indent the braces a common approach or should I look for another?

How can I accomplish that particular formatting (if it is possible at all)?

Thank in advance you for your help

答: This is a bug in ReSharper's formatting logic. I filed RSCPP-19853, it should be fixed in the next release. Please vote for the issue to receive updates.</p>

Title: Resharper C++ Lambda Braces Indentation

Body:
I am using Resharper C++.

I have code that often uses lambdas as function parameters, and I want them aligned as such:

return Method(
    [&]() -> bool
    {
        return this.variable >= 0;
    },
    "a normal parameter");

i.e. I want all parameters aligned on "one clear vertical line".

However, Resharper will align it as such:

return Method(
    [&]() -> bool
{
    return this.variable >= 0;
},
    "a normal parameter");

Which is obviously far less readably (especially when working with functions that use many lambdas and many "normal" parameters.

Neither

RESHARPER -> Options -> C++ -> Formatting Style -> Braces Layout -> Other statements and blocks

nor

RESHARPER -> Options -> C++ -> Formatting Style -> Braces Layout -> Other braces

seemed to work (or work without undesired side effects).

I am looking for a solution as described here: Resharper C# lambda indentation but I can't seem to find one for Resharper C++.

Now, my questions are:

Is the way I want to indent the braces a common approach or should I look for another?

How can I accomplish that particular formatting (if it is possible at all)?

Thank in advance you for your help


问:
Error in last keras layer of neural network
Model :

```<code>model = Sequential()
act = 'relu'
model.add(Dense(430, input_shape=(3,)))
model.add(Activation(act))

model.add(Dense(256))
model.add(Activation(act))
model.add(Dropout(0.4))

model.add(Dense(148))
model.add(Activation(act))
model.add(Dropout(0.3))

model.add(Dense(1))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#model.summary()
</code>```

Error :
Error when checking target: expected activation_4 to have shape (None, 1) but got array with shape (1715, 2)

The error is in the last layer of the neural network. The neural network is trying to classify whether 2 drugs are synergic or not.
COMPLETE SOURCE CODE :
https://github.com/tanmay-edgelord/Drug-Synergy-Models/blob/master/Drug%20Synergy%20NN%20Classifier.ipynb

Data:
https://github.com/tanmay-edgelord/Drug-Synergy-Models/blob/master/train.csv

答:
In your code you have the following lines:

```<code>y_binary = to_categorical(Y[:])
y_train = y_binary[:split]
y_test = y_binary[split:]
</code>```

to_categorical transforms the vector into a 1-hot vector. So since you have two classes, it transforms every number to a vector of length 2 (0 is transformed to [1,0] and 1 is transformed to [0,1]). So your last layer needs to be defined as follows:

```<code>model.add(Dense(2))
model.add(Activation('softmax'))
</code>```

(note that I replaced the 1 with 2).

Body:
Model :

model = Sequential()
act = 'relu'
model.add(Dense(430, input_shape=(3,)))
model.add(Activation(act))

model.add(Dense(256))
model.add(Activation(act))
model.add(Dropout(0.4))

model.add(Dense(148))
model.add(Activation(act))
model.add(Dropout(0.3))

model.add(Dense(1))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#model.summary()

Error :
Error when checking target: expected activation_4 to have shape (None, 1) but got array with shape (1715, 2)

The error is in the last layer of the neural network. The neural network is trying to classify whether 2 drugs are synergic or not.
COMPLETE SOURCE CODE :
https://github.com/tanmay-edgelord/Drug-Synergy-Models/blob/master/Drug%20Synergy%20NN%20Classifier.ipynb

Data:
https://github.com/tanmay-edgelord/Drug-Synergy-Models/blob/master/train.csv

Body:
In your code you have the following lines:

y_binary = to_categorical(Y[:])
y_train = y_binary[:split]
y_test = y_binary[split:]

to_categorical transforms the vector into a 1-hot vector. So since you have two classes, it transforms every number to a vector of length 2 (0 is transformed to [1,0] and 1 is transformed to [0,1]). So your last layer needs to be defined as follows:

model.add(Dense(2))
model.add(Activation('softmax'))

(note that I replaced the 1 with 2).


问:
How do I create new columns with pandas by combining rows with a particular value?
I have a dataframe with two "categories" of information. One category is repeated across multiple rows, and the other is specific to each row.

It looks something like this:

```<code>City State Industry Pay Hours
15   10    1        20  40
15   10    2        30  25
20   10    1        25  30
20   10    2        50  80
</code>```

I want it to look like:

```<code>City State Industry1Pay Industry1Hours Industry2Pay Industry2Hours
15   10    20           40             30           25
20   10    25           30             50           80
</code>```

This is a simplified version because the full table is much too long to fit up there. There are 8 columns in place of city and state, and 2 additional columns to pay and hours. In addition, each row should contain 4 industries for now (it will be 5 once that data comes in).

I am really struggling with how to do this. The dataset is from a project conducted in Stata, so the columns are mostly floats and need to stay that way for when I send it in.

The closest I think I've gotten is

```<code>wage = wage.pivot_table(index='cityid', columns='Industry').rename_axis(None)
wage.columns = wage.columns.map('_'.join)
</code>```

but I get an error because you can't join a float to a string, and I suspect that this will not work the way I'm hoping it will regardless.

So far I've looked at quite a few stackoverflow questions, as well as:
https://hackernoon.com/reshaping-data-in-python-fa27dda2ff77
http://nikgrozev.com/2015/07/01/reshaping-in-pandas-pivot-pivot-table-stack-and-unstack-explained-with-pictures/
and two others I am unable to link to because I haven't used stackoverflow very much

I'm really struggling with this, and would appreciate any help, even a link to a good tutorial to wrap my head around this. It seems like a really simple task but for the life of me I can't figure out how to do it without just manually moving stuff around in Excel.

I apologize in advance if this is a duplicate - I looked around a lot but I might be missing something obvious because I'm not sure what this is called beyond reshaping.

答:
Let's use set_index and unstack:

```<code>df['Industry'] = 'Industry'+df.Industry.astype(str)
df_out = df.set_index(['City','State','Industry']).unstack()
</code>```

And flatten the multiindex columns with swaplevel, map, join:

```<code>df_out.columns = df_out.columns.swaplevel(1,0)
df_out.columns = df_out.columns.map(''.join)
</code>```

Output:

```<code>            Industry1Pay  Industry2Pay  Industry1Hours  Industry2Hours
City State
15   10               20            30              40              25
20   10               25            50              30              80
</code>```

In case of multiple values of City, State, and Industry then use pivot_table

```<code>df['Industry'] = 'Industry'+df.Industry.astype(str)

df_out = df.pivot_table(index=['City','State'],columns='Industry',values=['Pay','Hours'], aggfunc='sum')

df_out.columns = df_out.columns.swaplevel(1,0)
df_out.columns = df_out.columns.map(''.join)

df_out
</code>```

OR use groupby

```<code>df_out = df.groupby(['City','State','Industry'])['Pay','Hours'].sum().unstack()

df_out.columns = df_out.columns.swaplevel(1,0)
df_out.columns = df_out.columns.map(''.join)
df_out
</code>```

Output:

```<code>            Industry1Pay  Industry2Pay  Industry1Hours  Industry2Hours
City State
15   10               20            30              40              25
20   10               25            50              30              80
</code>```

Body:
I have a dataframe with two "categories" of information. One category is repeated across multiple rows, and the other is specific to each row.

It looks something like this:

City State Industry Pay Hours
15   10    1        20  40
15   10    2        30  25
20   10    1        25  30
20   10    2        50  80

I want it to look like:

City State Industry1Pay Industry1Hours Industry2Pay Industry2Hours
15   10    20           40             30           25
20   10    25           30             50           80

This is a simplified version because the full table is much too long to fit up there. There are 8 columns in place of city and state, and 2 additional columns to pay and hours. In addition, each row should contain 4 industries for now (it will be 5 once that data comes in).

I am really struggling with how to do this. The dataset is from a project conducted in Stata, so the columns are mostly floats and need to stay that way for when I send it in.

The closest I think I've gotten is

wage = wage.pivot_table(index='cityid', columns='Industry').rename_axis(None)
wage.columns = wage.columns.map('_'.join)

but I get an error because you can't join a float to a string, and I suspect that this will not work the way I'm hoping it will regardless.

So far I've looked at quite a few stackoverflow questions, as well as:
https://hackernoon.com/reshaping-data-in-python-fa27dda2ff77
http://nikgrozev.com/2015/07/01/reshaping-in-pandas-pivot-pivot-table-stack-and-unstack-explained-with-pictures/
and two others I am unable to link to because I haven't used stackoverflow very much

I'm really struggling with this, and would appreciate any help, even a link to a good tutorial to wrap my head around this. It seems like a really simple task but for the life of me I can't figure out how to do it without just manually moving stuff around in Excel.

I apologize in advance if this is a duplicate - I looked around a lot but I might be missing something obvious because I'm not sure what this is called beyond reshaping.

Body:
Let's use set_index and unstack:

df['Industry'] = 'Industry'+df.Industry.astype(str)
df_out = df.set_index(['City','State','Industry']).unstack()

And flatten the multiindex columns with swaplevel, map, join:

df_out.columns = df_out.columns.swaplevel(1,0)
df_out.columns = df_out.columns.map(''.join)

Output:

            Industry1Pay  Industry2Pay  Industry1Hours  Industry2Hours
City State
15   10               20            30              40              25
20   10               25            50              30              80

In case of multiple values of City, State, and Industry then use pivot_table

df['Industry'] = 'Industry'+df.Industry.astype(str)

df_out = df.pivot_table(index=['City','State'],columns='Industry',values=['Pay','Hours'], aggfunc='sum')

df_out.columns = df_out.columns.swaplevel(1,0)
df_out.columns = df_out.columns.map(''.join)

df_out

OR use groupby

df_out = df.groupby(['City','State','Industry'])['Pay','Hours'].sum().unstack()

df_out.columns = df_out.columns.swaplevel(1,0)
df_out.columns = df_out.columns.map(''.join)
df_out

Output:

            Industry1Pay  Industry2Pay  Industry1Hours  Industry2Hours
City State
15   10               20            30              40              25
20   10               25            50              30              80


问:
find and select non-english character in text
I have some paragraph text in page that made with english and non-english words, I need to find english words with Javascript or jquery and insert  after and before these.</p>

答:
If you mean non-English character (not word), you can use regular expressions:

```<code>if (str.match(/^[a-zA-Z1-9.?!:;\- ]+$/g) === null)
{
    //str contains non-English character(s)
    // (You can add more characters to the block if you want to allow more. I may have missed something.)
}
</code>```

If you mean non-English words, it's a bit more difficult, because then you are required to use some sort of resource (dictionary-like) to match each word. This is much more complicated.

Body:
If you mean non-English character (not word), you can use regular expressions:

if (str.match(/^[a-zA-Z1-9.?!:;\- ]+$/g) === null)
{
    //str contains non-English character(s)
    // (You can add more characters to the block if you want to allow more. I may have missed something.)
}

If you mean non-English words, it's a bit more difficult, because then you are required to use some sort of resource (dictionary-like) to match each word. This is much more complicated.


问:
Cakephp 3: How to do an aggregate select statement within an inner join using Cakephp's Query Builder?
Well I just can't seem to figure this out. I'm on day two of messing around with this and it's time to enlist some help.

Here's the SQL that works:

```<code>SELECT rt1.request_id, rt1.status
FROM request_transactions rt1
INNER JOIN (
    SELECT max(id) AS max_id
    FROM request_transactions
    GROUP BY request_id
) AS rt2 ON rt1.id = rt2.max_id;
</code>```

How in the world do I write this using the Cakephp 3 query builder? I'm completely stuck in regards to the inner join with an aggregate function (max).

Many thanks in advance.

EDIT: Here's the table columns and data, if that helps:

```<code>SELECT id, user_id, created, modified FROM requests;

 id | user_id |       created       |      modified
----+---------+---------------------+---------------------
 94 |     101 | 2017-07-04 18:20:47 | 2017-07-04 18:21:26
 95 |     101 | 2017-07-04 18:48:14 | 2017-07-04 18:48:14
 96 |     101 | 2017-07-04 18:48:40 | 2017-07-04 18:48:40
(3 rows)

SELECT id, status, created, request_id FROM request_transactions;

 id  |  status  |       created       | request_id
-----+----------+---------------------+------------
 127 | created  | 2017-07-04 18:20:47 |         94
 128 | read     | 2017-07-04 18:21:03 |         94
 129 | rejected | 2017-07-04 18:21:26 |         94
 130 | created  | 2017-07-04 18:48:14 |         95
 131 | created  | 2017-07-04 18:48:40 |         96
(5 rows)

SELECT rt1.request_id, rt1.status
FROM request_transactions rt1
INNER JOIN (
    SELECT max(id) AS max_id
    FROM request_transactions
    GROUP BY request_id
) AS rt2 ON rt1.id = rt2.max_id;

 request_id |  status
------------+----------
         96 | created
         94 | rejected
         95 | created
(3 rows)
</code>```

答:
Alright! After some muddling about, I think I have it:

```<code>$requestTransactionsModel = $this->loadModel('RequestTransactions');

$query = $requestTransactionsModel->find('all');
$query->select([
    'max_id' => $query->func()->max('id'),
])
->group('request_id');

$query2 = $requestTransactionsModel->find('all');
$query2->select([
    'request_id',
    'status',
])
->join([
    'table' => $query,
    'alias' => 'rt2',
    'type' => 'INNER',
    'conditions' => ['rt2.max_id = RequestTransactions.id'],
]);
</code>```

This provides the same data as the SQL that was posted in the question. There were two parts I was having a hard time finding answers to:

I didn't know you could use a query as a table name in join(). That was trial and error.

I expected the conditions to be in the format

```<code>conditions => ['rt2.max_id' => 'RequestTransactions.id']
</code>```

That wasn't working. It was putting quotes around RequestTransactions.id. It needed to be written as

```<code>conditions => ['rt2.max_id = RequestTransactions.id']
</code>```

Body:
Well I just can't seem to figure this out. I'm on day two of messing around with this and it's time to enlist some help.

Here's the SQL that works:

SELECT rt1.request_id, rt1.status
FROM request_transactions rt1
INNER JOIN (
    SELECT max(id) AS max_id
    FROM request_transactions
    GROUP BY request_id
) AS rt2 ON rt1.id = rt2.max_id;

How in the world do I write this using the Cakephp 3 query builder? I'm completely stuck in regards to the inner join with an aggregate function (max).

Many thanks in advance.

EDIT: Here's the table columns and data, if that helps:

SELECT id, user_id, created, modified FROM requests;

 id | user_id |       created       |      modified
----+---------+---------------------+---------------------
 94 |     101 | 2017-07-04 18:20:47 | 2017-07-04 18:21:26
 95 |     101 | 2017-07-04 18:48:14 | 2017-07-04 18:48:14
 96 |     101 | 2017-07-04 18:48:40 | 2017-07-04 18:48:40
(3 rows)

SELECT id, status, created, request_id FROM request_transactions;

 id  |  status  |       created       | request_id
-----+----------+---------------------+------------
 127 | created  | 2017-07-04 18:20:47 |         94
 128 | read     | 2017-07-04 18:21:03 |         94
 129 | rejected | 2017-07-04 18:21:26 |         94
 130 | created  | 2017-07-04 18:48:14 |         95
 131 | created  | 2017-07-04 18:48:40 |         96
(5 rows)

SELECT rt1.request_id, rt1.status
FROM request_transactions rt1
INNER JOIN (
    SELECT max(id) AS max_id
    FROM request_transactions
    GROUP BY request_id
) AS rt2 ON rt1.id = rt2.max_id;

 request_id |  status
------------+----------
         96 | created
         94 | rejected
         95 | created
(3 rows)

Body:
Alright! After some muddling about, I think I have it:

$requestTransactionsModel = $this->loadModel('RequestTransactions');

$query = $requestTransactionsModel->find('all');
$query->select([
    'max_id' => $query->func()->max('id'),
])
->group('request_id');

$query2 = $requestTransactionsModel->find('all');
$query2->select([
    'request_id',
    'status',
])
->join([
    'table' => $query,
    'alias' => 'rt2',
    'type' => 'INNER',
    'conditions' => ['rt2.max_id = RequestTransactions.id'],
]);

This provides the same data as the SQL that was posted in the question. There were two parts I was having a hard time finding answers to:

I didn't know you could use a query as a table name in join(). That was trial and error.

I expected the conditions to be in the format

conditions => ['rt2.max_id' => 'RequestTransactions.id']

That wasn't working. It was putting quotes around RequestTransactions.id. It needed to be written as

conditions => ['rt2.max_id = RequestTransactions.id']


问:
Python FTP-SSL / FTP-TLS: Verifying Public Certificate?
I'm using Python 2.7.5 (not 3.X) and I need to verify a FTPS (FTP-TLS) public certificate. That is, I want to verify it against the standard certificate authority, not a custom key. (Similar to HTTPS.)

I see some options but I cannot get them to work:
The FTP_TLS() class doesn't seem to offer the ability to verify certificates, unless I'm mistaken:
class ftplib.FTP_TLS([host[, user[, passwd[, acct[, keyfile[, certfile[, timeout]]]]]]])

I've read into the certifi and also M2Crypto, but while I can connect and transfer using FTP/TLS, I can't seem to find a way to verify the certificate.

Also, I don't think I will be able to use the CURL libraries in this case :( Just a note.

答:
The resource referenced by mcepl is no longer available over http, but only using https.
https://heikkitoivonen.net/blog/2008/10/14/ssl-in-python-26

So much for 301 redirects.


问:
Cannot modify header information - headers already sent
I use googleAuth for 2-Factor authentication and it getting below error

"Cannot modify header information - headers already sent by (output started at ../GoogleAuth/index.php:2) in ../GoogleAuth/index.php on line 30."

HERE is index.php

<?php
include("config.php");
if(!empty($_SESSION['uid']))
{
    header("Location: device_confirmations.php");
}

include('class/userClass.php');
$userClass = new userClass();

require_once 'googleLib/GoogleAuthenticator.php';
$ga = new GoogleAuthenticator();
$secret = $ga->createSecret();

$errorMsgReg='';
$errorMsgLogin='';
if (!empty($_POST['loginSubmit']))
{
$usernameEmail=$_POST['usernameEmail'];
$password=$_POST['password'];
 if(strlen(trim($usernameEmail))>1 && strlen(trim($password))>1 )
   {
    $uid=$userClass->userLogin($usernameEmail,$password,$secret);
    if($uid)
    {
        $url=BASE_URL.'device_confirmations.php';
        header("Location: $url");
    }
    else
    {
        $errorMsgLogin="Please check login details.";
    }
   }
}

if (!empty($_POST['signupSubmit']))
{

 $username=$_POST['usernameReg'];
 $email=$_POST['emailReg'];
 $password=$_POST['passwordReg'];
    $name=$_POST['nameReg'];
 $username_check = preg_match('~^[A-Za-z0-9_]{3,20}$~i', $username);
 $email_check = preg_match('~^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$~i', $email);
 $password_check = preg_match('~^[A-Za-z0-9!@#$%^&*()_]{6,20}$~i', $password);

 if($username_check && $email_check && $password_check && strlen(trim($name))>0)
 {

    $uid=$userClass->userRegistration($username,$password,$email,$name,$secret);
    if($uid)
    {
     $url=BASE_URL.'device_confirmations.php';
     header("Location: $url");
    }
    else
    {
      $errorMsgReg="Username or Email already exits.";
    }

 }
    else
    {
      $errorMsgReg="Enter valid details.";
    }

}

?>
<!DOCTYPE html>

    <title>2-Step Verification using Google Authenticator</title>
    <link rel="stylesheet" type="text/css" href="style.css" charset="utf-8" />

2-Step Verification using Google Authenticator

Login

<form method="post" action="" name="login">
Username or Email
<input type="text" name="usernameEmail" autocomplete="off" />
Password
<input type="password" name="password" autocomplete="off"/>

<?php echo $errorMsgLogin; ?>

<input type="submit" class="button" name="loginSubmit" value="Login">
</form>

Registration

<form method="post" action="" name="signup">
Name
<input type="text" name="nameReg" autocomplete="off" />
Email
<input type="text" name="emailReg" autocomplete="off" />
Username
<input type="text" name="usernameReg" autocomplete="off" />

Password
<input type="password" name="passwordReg" autocomplete="off"/>

<?php echo $errorMsgReg; ?>

<input type="submit" class="button" name="signupSubmit" value="Signup">
</form>

```
</div>
</div>
</p>

<p>device_confirmation.php</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code> <?php
include('config.php');

if(empty($_SESSION['uid']))
{
 header("Location: index.php");
}

include('class/userClass.php');
$userClass = new userClass();
$userDetails=$userClass->userDetails($_SESSION['uid']);
$secret=$userDetails->google_auth_code;
$email=$userDetails->email;

require_once 'googleLib/GoogleAuthenticator.php';

$ga = new GoogleAuthenticator();

$qrCodeUrl = $ga->getQRCodeGoogleUrl($email, $secret,'9lessons Demos');

?>
<!DOCTYPE html>
<html>
<head>
    <title>2-Step Verification using Google Authenticator</title>
    <link rel="stylesheet" type="text/css" href="style.css" charset="utf-8" />
</head>
<body>
 <div id="container">
  <h1>2-Step Verification using Google Authenticator</h1>
  <div id='device'>

<p>Enter the verification code generated by Google Authenticator app on your phone.</p>
<div id="img">
<img src='<?php echo $qrCodeUrl; ?>' />
</div>

<form method="post" action="home.php">
<label>Enter Google Authenticator Code</label>
<input type="text" name="code" />
<input type="submit" class="button"/>
</form>
</div>
<div style="text-align:center">
 <h3>Get Google Authenticator on your phone</h3>
<img class='app' src="images/iphone.png" />

<img class="app" src="images/android.png" />
</div>
</div>
</body>
</html></code>```

config.php

<?php
session_start();
/* DATABASE CONFIGURATION */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'demos');
define("BASE_URL", "http://localhost/GoogleAuth/"); // Eg. http://yourwebsite.com

function getDB()
{
 $dbhost=DB_SERVER;
 $dbuser=DB_USERNAME;
 $dbpass=DB_PASSWORD;
 $dbname=DB_DATABASE;
 try {
 $dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
 $dbConnection->exec("set names utf8");
 $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 return $dbConnection;
    }
    catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
 }

}
?>```
</div>
</div>
</p>

<p>and home.php</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code> <?php
include('config.php');
include('class/userClass.php');
$userClass = new userClass();
$userDetails=$userClass->userDetails($_SESSION['uid']);

if($_POST['code'])
{
$code=$_POST['code'];
$secret=$userDetails->google_auth_code;
require_once 'googleLib/GoogleAuthenticator.php';
$ga = new GoogleAuthenticator();
$checkResult = $ga->verifyCode($secret, $code, 2);    // 2 = 2*30sec clock tolerance

if ($checkResult)
{
$_SESSION['googleCode']=$code;

}
else
{
echo 'FAILED';
}

}

include('session.php');
$userDetails=$userClass->userDetails($session_uid);

?>
<!DOCTYPE html>
<html>
<head>
    <title>2-Step Verification using Google Authenticator</title>
    <link rel="stylesheet" type="text/css" href="style.css" charset="utf-8" />
</head>
<body>
 <div id="container">
<h1>Welcome <?php echo $userDetails->name; ?></h1>

```
<?php print_r($userDetails); ?>
```
<h4>logout.php">Logout</h4>
</div>
</body>
</html></code>```

Please tell me where I was wrong

答:
This happens when you use functions like header and setcookie after you start sending output to the browser. The HTTP headers can't be altered after you start sending the body of the request, so you need to make sure no content is sent before any call to those functions is made.

This is one reason why it's a good idea to separate PHP code from output generation (HTML). Most major PHP frameworks do this by using separate Template engines, like Twig or Smarty.

If you have a large poorly constructed project, where it's an excessive effort to make sure output is generated after headers need to be set, you can use PHP's Output Control functions as a quick workaround.

Body:
I use googleAuth for 2-Factor authentication and it getting below error

"Cannot modify header information - headers already sent by (output started at ../GoogleAuth/index.php:2) in ../GoogleAuth/index.php on line 30."

HERE is index.php

<?php
include("config.php");
if(!empty($_SESSION['uid']))
{
    header("Location: device_confirmations.php");
}

include('class/userClass.php');
$userClass = new userClass();

require_once 'googleLib/GoogleAuthenticator.php';
$ga = new GoogleAuthenticator();
$secret = $ga->createSecret();

$errorMsgReg='';
$errorMsgLogin='';
if (!empty($_POST['loginSubmit']))
{
$usernameEmail=$_POST['usernameEmail'];
$password=$_POST['password'];
 if(strlen(trim($usernameEmail))>1 && strlen(trim($password))>1 )
   {
    $uid=$userClass->userLogin($usernameEmail,$password,$secret);
    if($uid)
    {
        $url=BASE_URL.'device_confirmations.php';
        header("Location: $url");
    }
    else
    {
        $errorMsgLogin="Please check login details.";
    }
   }
}

if (!empty($_POST['signupSubmit']))
{

 $username=$_POST['usernameReg'];
 $email=$_POST['emailReg'];
 $password=$_POST['passwordReg'];
    $name=$_POST['nameReg'];
 $username_check = preg_match('~^[A-Za-z0-9_]{3,20}$~i', $username);
 $email_check = preg_match('~^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$~i', $email);
 $password_check = preg_match('~^[A-Za-z0-9!@#$%^&*()_]{6,20}$~i', $password);

 if($username_check && $email_check && $password_check && strlen(trim($name))>0)
 {

    $uid=$userClass->userRegistration($username,$password,$email,$name,$secret);
    if($uid)
    {
     $url=BASE_URL.'device_confirmations.php';
     header("Location: $url");
    }
    else
    {
      $errorMsgReg="Username or Email already exits.";
    }

 }
    else
    {
      $errorMsgReg="Enter valid details.";
    }

}

?>
<!DOCTYPE html>

    <title>2-Step Verification using Google Authenticator</title>
    <link rel="stylesheet" type="text/css" href="style.css" charset="utf-8" />

2-Step Verification using Google Authenticator

Login

<form method="post" action="" name="login">
Username or Email
<input type="text" name="usernameEmail" autocomplete="off" />
Password
<input type="password" name="password" autocomplete="off"/>

<?php echo $errorMsgLogin; ?>

<input type="submit" class="button" name="loginSubmit" value="Login">
</form>

Registration

<form method="post" action="" name="signup">
Name
<input type="text" name="nameReg" autocomplete="off" />
Email
<input type="text" name="emailReg" autocomplete="off" />
Username
<input type="text" name="usernameReg" autocomplete="off" />

Password
<input type="password" name="passwordReg" autocomplete="off"/>

<?php echo $errorMsgReg; ?>

<input type="submit" class="button" name="signupSubmit" value="Signup">
</form>

device_confirmation.php

 <?php
include('config.php');

if(empty($_SESSION['uid']))
{
 header("Location: index.php");
}

include('class/userClass.php');
$userClass = new userClass();
$userDetails=$userClass->userDetails($_SESSION['uid']);
$secret=$userDetails->google_auth_code;
$email=$userDetails->email;

require_once 'googleLib/GoogleAuthenticator.php';

$ga = new GoogleAuthenticator();

$qrCodeUrl = $ga->getQRCodeGoogleUrl($email, $secret,'9lessons Demos');

?>
<!DOCTYPE html>

    <title>2-Step Verification using Google Authenticator</title>
    <link rel="stylesheet" type="text/css" href="style.css" charset="utf-8" />

2-Step Verification using Google Authenticator

Enter the verification code generated by Google Authenticator app on your phone.

<img src='<?php echo $qrCodeUrl; ?>' />

<form method="post" action="home.php">
Enter Google Authenticator Code
<input type="text" name="code" />
<input type="submit" class="button"/>
</form>

Get Google Authenticator on your phone

config.php

<?php
session_start();
/* DATABASE CONFIGURATION */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'demos');
define("BASE_URL", "http://localhost/GoogleAuth/"); // Eg. http://yourwebsite.com

function getDB()
{
 $dbhost=DB_SERVER;
 $dbuser=DB_USERNAME;
 $dbpass=DB_PASSWORD;
 $dbname=DB_DATABASE;
 try {
 $dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
 $dbConnection->exec("set names utf8");
 $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 return $dbConnection;
    }
    catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
 }

}
?>

and home.php

 <?php
include('config.php');
include('class/userClass.php');
$userClass = new userClass();
$userDetails=$userClass->userDetails($_SESSION['uid']);

if($_POST['code'])
{
$code=$_POST['code'];
$secret=$userDetails->google_auth_code;
require_once 'googleLib/GoogleAuthenticator.php';
$ga = new GoogleAuthenticator();
$checkResult = $ga->verifyCode($secret, $code, 2);    // 2 = 2*30sec clock tolerance

if ($checkResult)
{
$_SESSION['googleCode']=$code;

}
else
{
echo 'FAILED';
}

}

include('session.php');
$userDetails=$userClass->userDetails($session_uid);

?>
<!DOCTYPE html>

    <title>2-Step Verification using Google Authenticator</title>
    <link rel="stylesheet" type="text/css" href="style.css" charset="utf-8" />

Welcome <?php echo $userDetails->name; ?>

<?php print_r($userDetails); ?>

<a href="<?php echo BASE_URL; ?>logout.php">Logout

Please tell me where I was wrong


问:
Push a subroutine with argument into a stack in Perl
I would like to push a subroutine with arguments into a stack, but I can't figure out the syntax. Consider a working example with no arguments:

#!/usr/bin/perl -w
use strict;
use warnings;

sub hi    { print "hi\n";    }
sub hello { print "hello\n"; }
sub world { print "world\n"; }

my @stack;
push (@stack, \&hi   );
push (@stack, \&hello);
push (@stack, \&world);

while (@stack) {
    my $proc = pop @stack;
    $proc->();
}
```

<p>when I run the code:</p>

<pre class="lang-sh prettyprint-override"><code>% ./pop-proc.pl
world
hello
hi
</code>```

Now my question is, what if the subroutine looked like this:

sub mysub
{
    chomp( my( $arg ) = @_ );
    print "$arg\n";
}
```

<p>and I'd like to push subroutines with arguments, such as:</p>

<pre class="lang-perl prettyprint-override"><code>mysub("hello");
mysub("world");
</code>```

your input is highly appreciated.

答:
Use an anonymous sub (which may even be a closure).

```<code>push @stack, sub { mysub("hello") };
push @stack, sub { mysub("world") };
</code>```

For example,

```<code>sub hi { say "@_" }
my $arg = "Hello";
my $sub = sub { hi($arg, @_) };
$sub->("World");   # Hello World
</code>```

Body:
I would like to push a subroutine with arguments into a stack, but I can't figure out the syntax. Consider a working example with no arguments:

#!/usr/bin/perl -w
use strict;
use warnings;

sub hi    { print "hi\n";    }
sub hello { print "hello\n"; }
sub world { print "world\n"; }

my @stack;
push (@stack, \&hi   );
push (@stack, \&hello);
push (@stack, \&world);

while (@stack) {
    my $proc = pop @stack;
    $proc->();
}

when I run the code:

% ./pop-proc.pl
world
hello
hi

Now my question is, what if the subroutine looked like this:

sub mysub
{
    chomp( my( $arg ) = @_ );
    print "$arg\n";
}

and I'd like to push subroutines with arguments, such as:

mysub("hello");
mysub("world");

your input is highly appreciated.

Body:
Use an anonymous sub (which may even be a closure).

push @stack, sub { mysub("hello") };
push @stack, sub { mysub("world") };

For example,

sub hi { say "@_" }
my $arg = "Hello";
my $sub = sub { hi($arg, @_) };
$sub->("World");   # Hello World


问:
Running parallel R on multiple hosts
Can you please provide a script to run parallel cluster on 2 hosts ( amazon ec2) from scratch on Ubuntu Linux Machine ?

Specifications

The hosts are connected from local machine via identity key named amazon_key.pem

hostnames are supposed to be connected via internal IP address provided by Amazon cloud

keep the hostnames as rserver1 and rserver2 while setting up the cloud

答:
All these commands are run from local system. This has been written in such a way so that one can automate this code based on their need.

```<code>   HOST1=ip_of_server1
   HOST2=ip_of_server2
</code>```

People who do not have pem file can avoid this. Else put your exact location of the pem(key) file present in your local system.

```<code>pem_file_loc="~"
</code>```

for finding out the private IP , my region is eu-west-1 , please replace with your region. people who already knows private IP can ignore this and simply fill PIP1 and PIP2

```<code>PIP1=$(aws ec2 describe-instances --region eu-west-1 --filter "Name=ip-address,Values=${HOST1}" --query 'Reservations[].Instances[].[PrivateIpAddress]' --output text) #for finding out the private IP
PIP2=$(aws ec2 describe-instances --region eu-west-1 --filter "Name=ip-address,Values=${HOST2}" --query 'Reservations[].Instances[].[PrivateIpAddress]' --output text)
</code>```

for ease of usage only. some people may not need this based on how you spin up your ec2 instance.

```<code>SSH_ARGS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ${pem_file_loc}/amazon_key.pem"
</code>```

copy the pem file to the amazon machine , if pem file is not present then ignore this

```<code>rsync -e "ssh ${SSH_ARGS}" ${pem_file_loc}/amazon_key.pem ubuntu@${HOST1}:~/
rsync -e "ssh ${SSH_ARGS}" ${pem_file_loc}/amazon_key.pem ubuntu@${HOST2}:~/
</code>```

Run this command to set up the ssh authentication on HOST1 , people without pem file , can directly login and run the steps between the two EOF . Also note that i am setting up rserver1 and rserver2 for the ease of coding. People who already have many host machines can avoid this and instead use the private IP for all below commands.

```<code>ssh -T $SSH_ARGS ubuntu@${HOST1} <<EOF
sudo sh -c 'echo ${PIP1} rserver1 >> /etc/hosts'
sudo sh -c 'echo ${PIP2} rserver2 >> /etc/hosts'
rm -rf ~/.ssh/id_rsa.pub ~/.ssh/id_rsa
ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub | ssh -i amazon_key.pem -o StrictHostKeyChecking=no ubuntu@rserver2 'cat >> ~/.ssh/authorized_keys'
cat ~/.ssh/id_rsa.pub | ssh -i amazon_key.pem  -o StrictHostKeyChecking=no ubuntu@rserver1 'cat >> ~/.ssh/authorized_keys' #required for clustering
EOF
</code>```

Run the same for HOST2

```<code>ssh -T $SSH_ARGS ubuntu@${HOST2} <<EOF
sudo sh -c 'echo ${PIP1} rserver1 >> /etc/hosts'
sudo sh -c 'echo ${PIP2} rserver2 >> /etc/hosts'
rm -rf ~/.ssh/id_rsa.pub ~/.ssh/id_rsa
ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub | ssh -i amazon_key.pem  -o StrictHostKeyChecking=no ubuntu@rserver1 'cat >> ~/.ssh/authorized_keys'
EOF
</code>```

These are the commands i took from the blog http://www.win-vector.com/blog/2016/01/running-r-jobs-quickly-on-many-machines/ .

Run these commands on R server of host1. Cross verify the number of cores you want to keep. My case i used as 11 . empirically it is good to keep as detectCores() - 1

```<code>machineAddresses <- list(
  list(host='rserver1',user='ubuntu',
       ncore=11),
  list(host='rserver2',user='ubuntu',
       ncore=11)
)

spec <- lapply(machineAddresses,
               function(machine) {
                 rep(list(list(host=machine$host,
                               user=machine$user)),
                     machine$ncore)
               })

spec <- unlist(spec,recursive=FALSE)

library("doParallel")
cl <- makeCluster(type='PSOCK',master=primary,spec=spec)
registerDoParallel(cl)
#this is purely based on your need , there are many articles on how to run parallel loops , the focus is mainly on multiple hosts
clusterExport(cl, varlist=ls(.GlobalEnv))

print(cl)
##run your commands
stopCluster(cl)
</code>```

Just in case the command hangs , confirm the setup by running
system("ssh ubuntu@rserver1") and system("ssh ubuntu@rserver2") . These commands should work if ssh is properly setup.

Title: Running parallel R on multiple hosts

Body:
All these commands are run from local system. This has been written in such a way so that one can automate this code based on their need.

   HOST1=ip_of_server1
   HOST2=ip_of_server2

People who do not have pem file can avoid this. Else put your exact location of the pem(key) file present in your local system.

pem_file_loc="~"

for finding out the private IP , my region is eu-west-1 , please replace with your region. people who already knows private IP can ignore this and simply fill PIP1 and PIP2

PIP1=$(aws ec2 describe-instances --region eu-west-1 --filter "Name=ip-address,Values=${HOST1}" --query 'Reservations[].Instances[].[PrivateIpAddress]' --output text) #for finding out the private IP
PIP2=$(aws ec2 describe-instances --region eu-west-1 --filter "Name=ip-address,Values=${HOST2}" --query 'Reservations[].Instances[].[PrivateIpAddress]' --output text)

for ease of usage only. some people may not need this based on how you spin up your ec2 instance.

SSH_ARGS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ${pem_file_loc}/amazon_key.pem"

copy the pem file to the amazon machine , if pem file is not present then ignore this

rsync -e "ssh ${SSH_ARGS}" ${pem_file_loc}/amazon_key.pem ubuntu@${HOST1}:~/
rsync -e "ssh ${SSH_ARGS}" ${pem_file_loc}/amazon_key.pem ubuntu@${HOST2}:~/

Run this command to set up the ssh authentication on HOST1 , people without pem file , can directly login and run the steps between the two EOF . Also note that i am setting up rserver1 and rserver2 for the ease of coding. People who already have many host machines can avoid this and instead use the private IP for all below commands.

ssh -T $SSH_ARGS ubuntu@${HOST1} <<EOF
sudo sh -c 'echo ${PIP1} rserver1 >> /etc/hosts'
sudo sh -c 'echo ${PIP2} rserver2 >> /etc/hosts'
rm -rf ~/.ssh/id_rsa.pub ~/.ssh/id_rsa
ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub | ssh -i amazon_key.pem -o StrictHostKeyChecking=no ubuntu@rserver2 'cat >> ~/.ssh/authorized_keys'
cat ~/.ssh/id_rsa.pub | ssh -i amazon_key.pem  -o StrictHostKeyChecking=no ubuntu@rserver1 'cat >> ~/.ssh/authorized_keys' #required for clustering
EOF

Run the same for HOST2

ssh -T $SSH_ARGS ubuntu@${HOST2} <<EOF
sudo sh -c 'echo ${PIP1} rserver1 >> /etc/hosts'
sudo sh -c 'echo ${PIP2} rserver2 >> /etc/hosts'
rm -rf ~/.ssh/id_rsa.pub ~/.ssh/id_rsa
ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub | ssh -i amazon_key.pem  -o StrictHostKeyChecking=no ubuntu@rserver1 'cat >> ~/.ssh/authorized_keys'
EOF

These are the commands i took from the blog http://www.win-vector.com/blog/2016/01/running-r-jobs-quickly-on-many-machines/ .

Run these commands on R server of host1. Cross verify the number of cores you want to keep. My case i used as 11 . empirically it is good to keep as detectCores() - 1

machineAddresses <- list(
  list(host='rserver1',user='ubuntu',
       ncore=11),
  list(host='rserver2',user='ubuntu',
       ncore=11)
)

spec <- lapply(machineAddresses,
               function(machine) {
                 rep(list(list(host=machine$host,
                               user=machine$user)),
                     machine$ncore)
               })

spec <- unlist(spec,recursive=FALSE)

library("doParallel")
cl <- makeCluster(type='PSOCK',master=primary,spec=spec)
registerDoParallel(cl)
#this is purely based on your need , there are many articles on how to run parallel loops , the focus is mainly on multiple hosts
clusterExport(cl, varlist=ls(.GlobalEnv))

print(cl)
##run your commands
stopCluster(cl)

Just in case the command hangs , confirm the setup by running
system("ssh ubuntu@rserver1") and system("ssh ubuntu@rserver2") . These commands should work if ssh is properly setup.


问:
How can I best parse this comma delimited text file?
I am trying to figure out the best way to parse this comma delimited text file. Here is an excerpt:

```<code>bldgA, fred, lunch
bldgA, sally, supper
bldgB, bob, parking lot
bldgB, frank, rooftop
...
</code>```

What I am trying to do is read "bldgA" and then I want the person (2nd column), "fred" for example. But I don't want to parse the file looking for "fred" because fred may not be there next time whereas bldgA always will be. I want to read the text file, see that I am on bldgA, and read the next item in my list which is fred. After that I want to test if it is fred, sally, etc.and print out the third column. I know this might be easier with a database but seems to be a bit of overhead for a small text file just so I can name columns. Before I resorted to Access or something small, I thought I'd try Stack Overflow. Here is what I have:

```<code>string BuildingFile = Server.MapPath("buildings.txt");
StreamReader FileStreamReader;

FileStreamReader = File.OpenText(BuildingFile);

while (FileStreamReader.Peek() != -1)
{
    string[] words;
    words = FileStreamReader.ReadLine().Split(',');

    foreach (string word in words)
    {
        if (word == "bldgA")
        {
            //but since word is only on "bldgA"
            //how can I get the next item in the list which
            //is on the same line?

            //print out the name of the person and then the duty
        }
        if (word == "bldgB")
        {
            //same as A
        }
    }

}
FileStreamReader.Close();
</code>```

My final output would be

"You are in bldgA and your name is fred and your duty is lunch"

答:
I think that your excerpt is intended as so:

bldgA,fred,lunch

  bldgA,sally,supper

  bldgB,bob,parking lot

  bldgB,frank,rooftop

The steps to get your desired output:

read one line

split the line along commata

format your desired output with the parts returned from the split function

A split function is often part of a regex library.

In Common Lisp, this could be written like this:

```<code>(defun show-people-status (filename)
  (with-open-file (input-stream filename)
    (do ((line (read-line input-stream nil nil)
               (read-line input-stream nil nil)))
        ((null line) t)
      (apply #'format t "You are in ~a, your name is ~a, and your duty is ~a.~%"
             (cl-ppcre:split "," line)))))
</code>```

In Perl, you could use something like this:

```<code>#!/usr/bin/perl -w
use strict;

use Tie::File;

tie (@data, 'Tie::File', $ARGV[0]);

foreach (@data) {
    (my $Building, my $Name, my $Duty) = split (/,/);
    print "You are in $Building, your name is $Name, and your duty is $Duty."; };
</code>```

Note that the Perl version is intended as a standalone script, while the CL version shows a function to be used from the runtime.  There is no input checking in either.

OwnerDisplayName: johnny

Body:
I am trying to figure out the best way to parse this comma delimited text file. Here is an excerpt:

bldgA, fred, lunch
bldgA, sally, supper
bldgB, bob, parking lot
bldgB, frank, rooftop
...

What I am trying to do is read "bldgA" and then I want the person (2nd column), "fred" for example. But I don't want to parse the file looking for "fred" because fred may not be there next time whereas bldgA always will be. I want to read the text file, see that I am on bldgA, and read the next item in my list which is fred. After that I want to test if it is fred, sally, etc.and print out the third column. I know this might be easier with a database but seems to be a bit of overhead for a small text file just so I can name columns. Before I resorted to Access or something small, I thought I'd try Stack Overflow. Here is what I have:

string BuildingFile = Server.MapPath("buildings.txt");
StreamReader FileStreamReader;

FileStreamReader = File.OpenText(BuildingFile);

while (FileStreamReader.Peek() != -1)
{
    string[] words;
    words = FileStreamReader.ReadLine().Split(',');

    foreach (string word in words)
    {
        if (word == "bldgA")
        {
            //but since word is only on "bldgA"
            //how can I get the next item in the list which
            //is on the same line?

            //print out the name of the person and then the duty
        }
        if (word == "bldgB")
        {
            //same as A
        }
    }

}
FileStreamReader.Close();

My final output would be

"You are in bldgA and your name is fred and your duty is lunch"

Body:
I think that your excerpt is intended as so:

bldgA,fred,lunch

  bldgA,sally,supper

  bldgB,bob,parking lot

  bldgB,frank,rooftop

The steps to get your desired output:

read one line

split the line along commata

format your desired output with the parts returned from the split function

A split function is often part of a regex library.

In Common Lisp, this could be written like this:

(defun show-people-status (filename)
  (with-open-file (input-stream filename)
    (do ((line (read-line input-stream nil nil)
               (read-line input-stream nil nil)))
        ((null line) t)
      (apply #'format t "You are in ~a, your name is ~a, and your duty is ~a.~%"
             (cl-ppcre:split "," line)))))

In Perl, you could use something like this:

#!/usr/bin/perl -w
use strict;

use Tie::File;

tie (@data, 'Tie::File', $ARGV[0]);

foreach (@data) {
    (my $Building, my $Name, my $Duty) = split (/,/);
    print "You are in $Building, your name is $Name, and your duty is $Duty."; };

Note that the Perl version is intended as a standalone script, while the CL version shows a function to be used from the runtime.  There is no input checking in either.

OwnerDisplayName: Harleqin

ContentLicense: CC BY-SA 2.5

LastEditorDisplayName: Jeff Atwood


问:
Isn't a password a form of security through obscurity?
I know that security through obscurity is frowned upon and considered not really secure, but isn't a password security through obscurity?  It's only secure so long as no one finds it.

Is it just a matter of the level of obscurity? (i.e. a good password well salted and hashed is impractical to break)

Note I'm not asking about the process of saving passwords (Assume they are properly hashed and salted).  I'm asking about the whole idea using a password, which is a piece of information, which if known could compromise a person's account.

Or am I misunderstanding what security through obscurity means?  I guess that's what I assume it to mean, that is there exists some information which if known would compromise a system (in this case, the system being defined as whatever the password is meant to protect)

答:
Maybe it's easier to understand what Security-by-Obscurity is about, by looking at something that is in some sense the opposite: Auguste Kerckhoffs's Second Principle (now simply known usually as Kerckhoffs's Principle), formulated in 1883 in two articles on La Cryptographie Militaire:

[The cipher] must not be required to be secret, and it must be able to fall into the hands of the enemy without inconvenience.

Claude Shannon reformulated it as:

The enemy knows the system.

And Eric Raymond as:

Any security software design that doesn't assume the enemy possesses the source code is already untrustworthy.

An alternative formulation of that principle is that

The security of the system must depend only of the secrecy of the key, not the secrecy of the system.

So, we can simply define Security-by-Obscurity to be any system that does not follow that principle, and thus we cleverly out-defined the password :-)

There are two basic reasons why this Principle makes sense:

Keys tend to be much smaller than systems, therefore they are easier to protect.

Compromising the secrecy of a key only compromises the secrecy of all communications protected by that key, compromising the secrecy of the system compromises all communications.

Note that it doesn't say anywhere that you can't keep your system secret. It just says you shouldn't depend on it. You may use Security-by-Obscurity as an additional line of defense, you just shouldn't assume that it actually works.

In general, however, cryptography is hard, and cryptographic systems are complex, therefore you pretty much need to publish it, to get as many eyeballs on it as possible. There are only very few organizations on this planet that actually have the necessary smart people to design cryptographic systems in secrecy: in the past, when mathematicians were patriots and governments were rich, those were the NSA and the KGB, right now it's IBM and a couple of years from now it's gonna be the Chinese Secret Service and international crime syndicates.


问:
JQuery code won't exceute
I was working on an Redirecting page and when i was testing it i seen that 100% of the JQuery code isn't working... so please can anyone help me fixing it ???

var redirect_count = 5;

setTimeout(UpdateCounter, 1000);

function UpdateCounter() {
  if (redirect_count >= 1) {
    redirect_count = redirect_count - 1; // Another Method: redirect_count--;
    document.getElementById("counter").html("Redirecting after " + redirect_count + " seconds");
    document.title = "Redirecting after " + redirect_count + "sec - please wait...";
  } else if (redirect_count < 1) {
    window.location.assign("http://example.com");
  }
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>[INFO]: This page has been moved to http://example.com.</p>
<p id="counter">Redirecting after <strong>5 seconds</strong></p></code>```

答:
You need to change the whole code to actually use jQuery.

```<code>$("#counter").html(); // This is the right syntax!
</code>```

Including the jQuery library alone will not make your page awesome, but you need to make use of jQuery library to make it work!

Full Snippet

var redirect_count = 5;

setInterval(UpdateCounter, 1000);

function UpdateCounter() {
  if (redirect_count >= 1) {
    redirect_count = redirect_count - 1; // Another Method: redirect_count--;
    $("#counter").html("Redirecting after " + redirect_count + " seconds");
    document.title = "Redirecting after " + redirect_count + "sec - please wait...";
  } else if (redirect_count < 1) {
    window.location.assign("http://example.com");
  }
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>[INFO]: This page has been moved to http://example.com.</p>
<p id="counter">Redirecting after <strong>5 seconds</strong></p></code>```

Also, you need setInterval and not setTimeout.

Title: JQuery code won't exceute

Body:
I was working on an Redirecting page and when i was testing it i seen that 100% of the JQuery code isn't working... so please can anyone help me fixing it ???

var redirect_count = 5;

setTimeout(UpdateCounter, 1000);

function UpdateCounter() {
  if (redirect_count >= 1) {
    redirect_count = redirect_count - 1; // Another Method: redirect_count--;
    document.getElementById("counter").html("Redirecting after " + redirect_count + " seconds");
    document.title = "Redirecting after " + redirect_count + "sec - please wait...";
  } else if (redirect_count < 1) {
    window.location.assign("http://example.com");
  }
}

[INFO]: This page has been moved to http://example.com.

Redirecting after 5 seconds

Body:
You need to change the whole code to actually use jQuery.

$("#counter").html(); // This is the right syntax!

Including the jQuery library alone will not make your page awesome, but you need to make use of jQuery library to make it work!

Full Snippet

var redirect_count = 5;

setInterval(UpdateCounter, 1000);

function UpdateCounter() {
  if (redirect_count >= 1) {
    redirect_count = redirect_count - 1; // Another Method: redirect_count--;
    $("#counter").html("Redirecting after <strong>" + redirect_count + " seconds</strong>");
    document.title = "Redirecting after " + redirect_count + "sec - please wait...";
  } else if (redirect_count < 1) {
    window.location.assign("http://example.com");
  }
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>[INFO]: This page has been moved to <a href="http://example.com">http://example.com</a>.</p>
<p id="counter">Redirecting after <strong>5 seconds</strong></p>

Also, you need setInterval and not setTimeout.