问:
Keras: could not broadcast input array from shape (14,1) into shape (14)
I'm using Keras with Tensorflow on Windows 10. I'm trying to create a maschine learning model for the Adult dataset (https://archive.ics.uci.edu/ml/datasets/Adult).

First I am doing One Hot Encoding and then I'm trying to train my model but i get:

"ValueError: could not broadcast input array from shape (14,1) into shape (14)"

Even if I change the input_dim to something else than 14, I still get the same error with the same shapes.

What am I doing wrong?

```<code>df = pd.read_csv("adult_data.csv",header=None)
X = df.iloc[:,0:14]
Y = df.iloc[:,14]

encoder = LabelEncoder()
#X
for i in [1,3,5,6,7,8,9,13]:
   column = X[i]
   encoder.fit(column)
   encoded_C = encoder.transform(column)
   X[i] = np_utils.to_categorical(encoded_C)

print(X.shape)
#Y
encoder.fit(Y)
en_Y = encoder.transform(Y)
Y = np_utils.to_categorical(en_Y)

#model
model = Sequential()
model.add(Dense(21, input_dim=14, activation="relu"))
model.add(Dense(2, activation="softmax"))
#compile
model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=
["accuracy"])

#train
model.fit(X,Y, epochs=50, batch_size=100)
score = model.evaluate(X,Y)
print("Accuracy: {}%".format(score[0]))
</code>```

FULL ERROR:

```<code>Traceback (most recent call last):
  File "main.py", line 36, in <module>
    model.fit(X,Y, epochs=50, batch_size=100)
  File "C:\Users\K\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\models.py", line 960, in fit
    validation_steps=validation_steps)
  File "C:\Users\K\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1574, in fit
    batch_size=batch_size)
  File "C:\Users\K\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1407, in _standardize_user_data
    exception_prefix='input')
  File "C:\Users\K\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 128, in _standardize_input_data
    arrays[i] = array
 ValueError: could not broadcast input array from shape (14,1) into shape (14)
</code>```

答: Fixed the error by updating to Keras version 2.1.2.</p>

Body:
I'm using Keras with Tensorflow on Windows 10. I'm trying to create a maschine learning model for the Adult dataset (https://archive.ics.uci.edu/ml/datasets/Adult).

First I am doing One Hot Encoding and then I'm trying to train my model but i get:

"ValueError: could not broadcast input array from shape (14,1) into shape (14)"

Even if I change the input_dim to something else than 14, I still get the same error with the same shapes.

What am I doing wrong?

df = pd.read_csv("adult_data.csv",header=None)
X = df.iloc[:,0:14]
Y = df.iloc[:,14]

encoder = LabelEncoder()
#X
for i in [1,3,5,6,7,8,9,13]:
   column = X[i]
   encoder.fit(column)
   encoded_C = encoder.transform(column)
   X[i] = np_utils.to_categorical(encoded_C)

print(X.shape)
#Y
encoder.fit(Y)
en_Y = encoder.transform(Y)
Y = np_utils.to_categorical(en_Y)

#model
model = Sequential()
model.add(Dense(21, input_dim=14, activation="relu"))
model.add(Dense(2, activation="softmax"))
#compile
model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=
["accuracy"])

#train
model.fit(X,Y, epochs=50, batch_size=100)
score = model.evaluate(X,Y)
print("Accuracy: {}%".format(score[0]))

FULL ERROR:

Traceback (most recent call last):
  File "main.py", line 36, in <module>
    model.fit(X,Y, epochs=50, batch_size=100)
  File "C:\Users\K\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\models.py", line 960, in fit
    validation_steps=validation_steps)
  File "C:\Users\K\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1574, in fit
    batch_size=batch_size)
  File "C:\Users\K\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1407, in _standardize_user_data
    exception_prefix='input')
  File "C:\Users\K\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 128, in _standardize_input_data
    arrays[i] = array
 ValueError: could not broadcast input array from shape (14,1) into shape (14)


来源: stackexchange

问:
Socket option IPV6_PKTINFO
I would like to use IPV6_PKTINFO option to specify an outgoing IPv6 address in sendmsg(). The socket is not bound neither to an address nor to an interface. Should I always specify ifindex together with source address

(according to rfc3542 :

The kernel must verify that the requested source address is indeed a
   unicast address assigned to the node.  When the address is a scoped
   one, there may be ambiguity about its scope zone.  This is
   particularly the case for link-local addresses.  In such a case, the
   kernel must first determine the appropriate scope zone based on the
   zone of the destination address or the outgoing interface (if known),
   then qualify the address.  This also means that it is not feasible to
   specify the source address for a non-binding socket by the
   IPV6_PKTINFO sticky option, unless the outgoing interface is also
   specified.  The application should simply use bind() for such
   purposes.
)

If I don't specify ifindex does it mean that this option should be ignored?
Thanks, Renata

答: If you know the interface index, you could specify it.  It is not required unless you are attempting to set a an IPv6 Link Local address.</p>

ContentLicense: CC BY-SA 3.0

Title: Socket option IPV6_PKTINFO


问:
summing dict value of list to single integer
I am trying to do something pretty simple but cant seem to get it. I have a dictionary where the value is a list. I am trying to just sum the list and assign the value back to the same key as an int. Using the code below the first line doesn't do anything, the second as it says puts the value back but in a list. All other things ive tried has given me an error can only assign iterable. As far as i know iterables are anything that can be iterated on such as list and not int. Why can I only use iterable and how can i fix this issue ? The dict im using is here (https://gist.github.com/ishikawa-rei/53c100449605e370ef66f1c06f15b62e)</p>

```<code>for i in dict.values():
    i = sum(i)
    #i[:] = [sum(i) / 3600] # puts answer into dict but as a list
</code>```

答:
```<code>for i in dikt.keys():
    dickt[i] = sum(dict[i]))
</code>```

btw, dict is a type.  best not to use it as a variable name

Body:
I am trying to do something pretty simple but cant seem to get it. I have a dictionary where the value is a list. I am trying to just sum the list and assign the value back to the same key as an int. Using the code below the first line doesn't do anything, the second as it says puts the value back but in a list. All other things ive tried has given me an error can only assign iterable. As far as i know iterables are anything that can be iterated on such as list and not int. Why can I only use iterable and how can i fix this issue ? The dict im using is here (https://gist.github.com/ishikawa-rei/53c100449605e370ef66f1c06f15b62e)

for i in dict.values():
    i = sum(i)
    #i[:] = [sum(i) / 3600] # puts answer into dict but as a list

Body:
for i in dikt.keys():
    dickt[i] = sum(dict[i]))

btw, dict is a type.  best not to use it as a variable name


问:
Trouble getting SSL to work with django + nginx + wsgi
I've followed a couple of examples for Django + nginx + wsgi + ssl, but I can't get them to work.  I simply get an error in my browser than I can't connect.

I'm running two websites off the host.  The config files are identical except for the ip addresses, server names, and directories.

When neither use SSL, they work fine.  When I try to listen on 443 with one of them, I can't connect to either.

My config files are below, and any suggestions would be appreciated.

```<code>server{
listen xxx.xxx.xxx.xxx:80;
server_name sub.domain.com;

access_log /home/django/logs/nginx_customerdb_http_access.log;
error_log /home/django/logs/nginx_customerdb_http_error.log;

location / {
    proxy_pass  http://127.0.0.1:8080;
    proxy_redirect          off;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size    10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout   90;
    proxy_send_timeout      90;
    proxy_read_timeout      90;
    proxy_buffers           32 4k;
}

location /site_media/ {
    alias /home/django/customerdb_site_media/;
}

location /admin-media/ {
    alias /home/django/django_admin_media/;
}
}

server{
listen xxx.xxx.xxx.xxx:443;
server_name sub.domain.com;

access_log /home/django/logs/nginx_customerdb_http_access.log;
error_log /home/django/logs/nginx_customerdb_http_error.log;

ssl on;
ssl_certificate sub.domain.com.crt;
ssl_certificate_key sub.domain.com.key;
ssl_prefer_server_ciphers   on;

location / {
    proxy_pass              http://127.0.0.1:8080;
    proxy_redirect          off;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Protocol    https;
    client_max_body_size    10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout   90;
    proxy_send_timeout      90;
    proxy_read_timeout      90;
    proxy_buffers           32 4k;
}

location /site_media/ {
    alias /home/django/customerdb_site_media/;
}

location /admin-media/ {
    alias /home/django/django_admin_media/;
}
}

<VirtualHost *:8080>
ServerName xxx.xxx.xxx.xxx
ServerAlias xxx.xxx.xxx.xxx

LogLevel warn
ErrorLog /home/django/logs/apache_customerdb_error.log
CustomLog /home/django/logs/apache_customerdb_access.log combined

WSGIScriptAlias / /home/django/customerdb/apache/django.wsgi
WSGIDaemonProcess customerdb_wsgi processes=4 threads=5
WSGIProcessGroup customerdb_wsgi

SetEnvIf X-Forwarded-Protocol "^https$" HTTPS=on

</VirtualHost>
</code>```

UDPATE: the existence of two sites (on separate IPs) on the host is the issue.  if i delete the other site, the setting above mostly work.  doing so also brings up another issue: chrome doesn't accept the site as secure saying that some content is not encrypted.

答: I changed the server that listens on 80 to rewrite to https removed all the other directives.</p>

ContentLicense: CC BY-SA 2.5

Body:
I've followed a couple of examples for Django + nginx + wsgi + ssl, but I can't get them to work.  I simply get an error in my browser than I can't connect.

I'm running two websites off the host.  The config files are identical except for the ip addresses, server names, and directories.

When neither use SSL, they work fine.  When I try to listen on 443 with one of them, I can't connect to either.

My config files are below, and any suggestions would be appreciated.

server{
listen xxx.xxx.xxx.xxx:80;
server_name sub.domain.com;

access_log /home/django/logs/nginx_customerdb_http_access.log;
error_log /home/django/logs/nginx_customerdb_http_error.log;

location / {
    proxy_pass  http://127.0.0.1:8080;
    proxy_redirect          off;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size    10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout   90;
    proxy_send_timeout      90;
    proxy_read_timeout      90;
    proxy_buffers           32 4k;
}

location /site_media/ {
    alias /home/django/customerdb_site_media/;
}

location /admin-media/ {
    alias /home/django/django_admin_media/;
}
}

server{
listen xxx.xxx.xxx.xxx:443;
server_name sub.domain.com;

access_log /home/django/logs/nginx_customerdb_http_access.log;
error_log /home/django/logs/nginx_customerdb_http_error.log;

ssl on;
ssl_certificate sub.domain.com.crt;
ssl_certificate_key sub.domain.com.key;
ssl_prefer_server_ciphers   on;

location / {
    proxy_pass              http://127.0.0.1:8080;
    proxy_redirect          off;
    proxy_set_header        Host            $host;
    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Protocol    https;
    client_max_body_size    10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout   90;
    proxy_send_timeout      90;
    proxy_read_timeout      90;
    proxy_buffers           32 4k;
}

location /site_media/ {
    alias /home/django/customerdb_site_media/;
}

location /admin-media/ {
    alias /home/django/django_admin_media/;
}
}

<VirtualHost *:8080>
ServerName xxx.xxx.xxx.xxx
ServerAlias xxx.xxx.xxx.xxx

LogLevel warn
ErrorLog /home/django/logs/apache_customerdb_error.log
CustomLog /home/django/logs/apache_customerdb_access.log combined

WSGIScriptAlias / /home/django/customerdb/apache/django.wsgi
WSGIDaemonProcess customerdb_wsgi processes=4 threads=5
WSGIProcessGroup customerdb_wsgi

SetEnvIf X-Forwarded-Protocol "^https$" HTTPS=on

</VirtualHost>

UDPATE: the existence of two sites (on separate IPs) on the host is the issue.  if i delete the other site, the setting above mostly work.  doing so also brings up another issue: chrome doesn't accept the site as secure saying that some content is not encrypted.


问:
Convert hex in text representation to decimal number
I am trying to convert hex to decimal using PostgreSQL 9.1

with this query:

```<code>SELECT to_number('DEADBEEF', 'FMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
</code>```

I get the following error:

```<code>ERROR:  invalid input syntax for type numeric: " "
</code>```

What am I doing wrong?

答:
pg-bignum

Internally, pg-bignum uses the SSL library for big numbers. This method has none of the drawbacks mentioned in the other answers with numeric. Nor is it slowed down by plpgsql. It's fast and it works with a number of any size. Test case taken from Erwin's answer for comparison,

```<code>CREATE EXTENSION bignum;

SELECT hex, bn_in_hex(hex::cstring)
FROM   (
   VALUES ('ff'::text)
        , ('7fffffff')
        , ('80000000')
        , ('deadbeef')
        , ('7fffffffffffffff')
        , ('8000000000000000')
        , ('ffffffffffffffff')
        , ('ffffffffffffffff123')
   ) t(hex);

         hex         |        bn_in_hex
---------------------+-------------------------
 ff                  | 255
 7fffffff            | 2147483647
 80000000            | 2147483648
 deadbeef            | 3735928559
 7fffffffffffffff    | 9223372036854775807
 8000000000000000    | 9223372036854775808
 ffffffffffffffff    | 18446744073709551615
 ffffffffffffffff123 | 75557863725914323415331
(8 rows)
</code>```

You can get the type to numeric using bn_in_hex('deadbeef')::text::numeric.

Body:
I am trying to convert hex to decimal using PostgreSQL 9.1

with this query:

SELECT to_number('DEADBEEF', 'FMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');

I get the following error:

ERROR:  invalid input syntax for type numeric: " "

What am I doing wrong?

Body:
pg-bignum

Internally, pg-bignum uses the SSL library for big numbers. This method has none of the drawbacks mentioned in the other answers with numeric. Nor is it slowed down by plpgsql. It's fast and it works with a number of any size. Test case taken from Erwin's answer for comparison,

CREATE EXTENSION bignum;

SELECT hex, bn_in_hex(hex::cstring)
FROM   (
   VALUES ('ff'::text)
        , ('7fffffff')
        , ('80000000')
        , ('deadbeef')
        , ('7fffffffffffffff')
        , ('8000000000000000')
        , ('ffffffffffffffff')
        , ('ffffffffffffffff123')
   ) t(hex);

         hex         |        bn_in_hex
---------------------+-------------------------
 ff                  | 255
 7fffffff            | 2147483647
 80000000            | 2147483648
 deadbeef            | 3735928559
 7fffffffffffffff    | 9223372036854775807
 8000000000000000    | 9223372036854775808
 ffffffffffffffff    | 18446744073709551615
 ffffffffffffffff123 | 75557863725914323415331
(8 rows)

You can get the type to numeric using bn_in_hex('deadbeef')::text::numeric.


问:
Use sed or awk to remove lines not adjacent to pattern in bash

I need to delete any line that is not surrounding a ">" symbol.

Here is some sample data:

```<code>sample1.fasta
>R00003
ATCATACTACTACG
sample2.fasta
sample3.fasta
sample4.fasta
>R00003
ATACTACGTA
sample7.fasta
>R00003
ATGCATCAT
sample8.fasta
>R00003
AATCATCGACCT
sample9.fasta
sample10.fasta
>R00003
AGCATCTCAGTC
</code>```

I tried using awk to help reveal the issue:

```<code>awk '{/fasta/?f++:f=0} f==2' R3.fasta
</code>```

This returns:

```<code>sample3.fasta
sample10.fasta
</code>```

This is diagnostic in that it shows where duplicates are present. However, I want to remove the lines that are not flanking the ">" symbol on either side. This does not remove them and only displays the second.

The result I expect is:

```<code>sample1.fasta
>R00003
ATCATACTACTACG
sample4.fasta
>R00003
ATACTACGTA
sample7.fasta
>R00003
ATGCATCAT
sample8.fasta
>R00003
AATCATCGACCT
sample10.fasta
>R0003
AGCATCTCAGTC
</code>```

Where the lines not flaking the ">" symbol have been removed

答:
From the example, it appears that you want all non-fasta lines and, for the fasta lines, you want only the last fasta file before the next >.  In that case, try:

```<code>$ awk 'f && !/fasta/{print f; f=""} /fasta/{f=$0; next} END{if(f)print f} 1' R3.fasta
sample1.fasta
>R00003
ATCATACTACTACG
sample4.fasta
>R00003
ATACTACGTA
sample7.fasta
>R00003
ATGCATCAT
sample8.fasta
>R00003
AATCATCGACCT
sample10.fasta
>R0003
AGCATCTCAGTC
</code>```

How it works

f && !/fasta/{print f; f=""}

If the variable f is set and the current line does not contain fasta, then print f and erase its current value.

/fasta/{f=$0; next}

If the current line contains fasta, then save the current line to variable f, slip the rest of the commands, and jump to the next line.

END{if(f)print f}

If f is still set to something after we have reached the end of the file, then print it.

1

For all other lines, print them.

Body:
I need to delete any line that is not surrounding a ">" symbol.

Here is some sample data:

sample1.fasta
>R00003
ATCATACTACTACG
sample2.fasta
sample3.fasta
sample4.fasta
>R00003
ATACTACGTA
sample7.fasta
>R00003
ATGCATCAT
sample8.fasta
>R00003
AATCATCGACCT
sample9.fasta
sample10.fasta
>R00003
AGCATCTCAGTC

I tried using awk to help reveal the issue:

awk '{/fasta/?f++:f=0} f==2' R3.fasta

This returns:

sample3.fasta
sample10.fasta

This is diagnostic in that it shows where duplicates are present. However, I want to remove the lines that are not flanking the ">" symbol on either side. This does not remove them and only displays the second.

The result I expect is:

sample1.fasta
>R00003
ATCATACTACTACG
sample4.fasta
>R00003
ATACTACGTA
sample7.fasta
>R00003
ATGCATCAT
sample8.fasta
>R00003
AATCATCGACCT
sample10.fasta
>R0003
AGCATCTCAGTC

Where the lines not flaking the ">" symbol have been removed

Body:
From the example, it appears that you want all non-fasta lines and, for the fasta lines, you want only the last fasta file before the next >.  In that case, try:

$ awk 'f && !/fasta/{print f; f=""} /fasta/{f=$0; next} END{if(f)print f} 1' R3.fasta
sample1.fasta
>R00003
ATCATACTACTACG
sample4.fasta
>R00003
ATACTACGTA
sample7.fasta
>R00003
ATGCATCAT
sample8.fasta
>R00003
AATCATCGACCT
sample10.fasta
>R0003
AGCATCTCAGTC

How it works

f && !/fasta/{print f; f=""}

If the variable f is set and the current line does not contain fasta, then print f and erase its current value.

/fasta/{f=$0; next}

If the current line contains fasta, then save the current line to variable f, slip the rest of the commands, and jump to the next line.

END{if(f)print f}

If f is still set to something after we have reached the end of the file, then print it.

1

For all other lines, print them.


问:
XAMPP Apache Webserver localhost not working on MAC OS
I install XAMPP server on MAC OS 10.6 it was working fine.
After a lot of days I checked it, but not working this time, localhost not opening this time.

after some R&D I reinstall XAMPP server after uninstall

When I start the apache after reinstall it giving port 80 running a another webserver Then I restart system, then apache start ok, but same local host not working

Then I check Web Sharing in my System Preference then it was already Tuned Off ...

Please anybody tell me where I am wrong?

答:
To be able to do this, you will have to stop apache from your terminal.

```<code>sudo apachectl stop
</code>```

After you've done this, your apache server will be be up and running again!

Hope this helps

Body:
To be able to do this, you will have to stop apache from your terminal.

sudo apachectl stop

After you've done this, your apache server will be be up and running again!

Hope this helps


问:
Angularfire2 - retrieve added object
I'm trying to find a complete example that uses angularfire2 but I can't find one. Some examples uses anugalrfire2 with native firebase or an old version of angularfire.

I have some questions about how to use angularfire2/database to create two documents with dependencies.

Example

Database:

```<code>locations {
 country: string
 lat: number
 long: number
 .....
}

stores {
 name: string
 locationID: string
 description: string
  .....
}
</code>```

So tried to find a way to create one location and one store in a atomic creation if possible, without luck. Then, after I could update newStore.locationID = newLocation.id

```<code>import { AngularFireDatabase } from 'angularfire2/database';
....
constructor(private db: AngularFireDatabase) { }

createStore(ownerID: number, storeData: Store, locationData: Location) {

    let promises = {
        newStore: this.db.object('/stores').add(storeData),
        newLocation: this.db.object('/locations').add(locationData),
    }

    Observable.forkJoin(promises).subscribe((r) => {
        newStore.userID = ownerID;
        newStore.workingRegionID = newLocation.key;
    });
}
</code>```

I also tried to create an empty Store (using AngularFire2), but I didn't find a way to do this. (received all types of error)

```<code>import { AngularFireDatabase } from 'angularfire2/database';
....
constructor(private db: AngularFireDatabase) { }

createStore(ownerID: number, storeData: Store, locationData: Location) {

    let newStoreKey = this.db.object('/stores').push().key;
    let newLocationKey = this.db.object('/locations').push().key;

    storeData.userID = ownerID;
    storeData.workingRegionID = newLocationKey;

    let sets = {};
    sets['/stores/' + newStoreKey] = Store;
    sets['/locations/' + newLocationKey] = Location;

    return Observable.fromPromise(this.db.set(sets));
}
</code>```

Finally, I have some questions about atomicy and rules.

Do I need to use Firebase's Transactions to make it atomic?

AngularFire2 retrive rules from Firebase? If not, can I set/update info offline and when I came online Angular could fire an write exception? How can I should handle this in my code?

答:
I was close from a valid answer. That what I got in my storeService

```<code>import { AngularFireDatabase } from 'angularfire2/database';
....
constructor(private db: AngularFireDatabase) { }

createStore(ownerID: string, storeData: Store, locationData: Location) {
    let newStore = this.db.list('/stores').push(undefined);
    let newStoreKey = newStore.key;
    let newLocation = this.db.list('/stores').push(undefined);
    let newLocationKey = newLocation.key;
    storeData.ownerID = ownerID;
    storeData.workingRegionID = newLocationKey;
    storeData.name = storeData.name.toLowerCase();

    let tasks = {};
    tasks["/stores/" + newStoreKey] = storeData;
    tasks["/locations/" + newLocationKey] = locationData;

    return this.db.object('/').update(tasks);
}
</code>```

Answering my own questions.

Q: Do I need to use Firebase's Transactions to make it atomic?

A: No, you can use multi-location update using AngularFire2. Good answer here

Title: Angularfire2 - retrieve added object

Body:
I'm trying to find a complete example that uses angularfire2 but I can't find one. Some examples uses anugalrfire2 with native firebase or an old version of angularfire.

I have some questions about how to use angularfire2/database to create two documents with dependencies.

Example

Database:

locations {
 country: string
 lat: number
 long: number
 .....
}

stores {
 name: string
 locationID: string
 description: string
  .....
}

So tried to find a way to create one location and one store in a atomic creation if possible, without luck. Then, after I could update newStore.locationID = newLocation.id

import { AngularFireDatabase } from 'angularfire2/database';
....
constructor(private db: AngularFireDatabase) { }

createStore(ownerID: number, storeData: Store, locationData: Location) {

    let promises = {
        newStore: this.db.object('/stores').add(storeData),
        newLocation: this.db.object('/locations').add(locationData),
    }

    Observable.forkJoin(promises).subscribe((r) => {
        newStore.userID = ownerID;
        newStore.workingRegionID = newLocation.key;
    });
}

I also tried to create an empty Store (using AngularFire2), but I didn't find a way to do this. (received all types of error)

import { AngularFireDatabase } from 'angularfire2/database';
....
constructor(private db: AngularFireDatabase) { }

createStore(ownerID: number, storeData: Store, locationData: Location) {

    let newStoreKey = this.db.object('/stores').push().key;
    let newLocationKey = this.db.object('/locations').push().key;

    storeData.userID = ownerID;
    storeData.workingRegionID = newLocationKey;

    let sets = {};
    sets['/stores/' + newStoreKey] = Store;
    sets['/locations/' + newLocationKey] = Location;

    return Observable.fromPromise(this.db.set(sets));
}

Finally, I have some questions about atomicy and rules.

Do I need to use Firebase's Transactions to make it atomic?

AngularFire2 retrive rules from Firebase? If not, can I set/update info offline and when I came online Angular could fire an write exception? How can I should handle this in my code?

Body:
I was close from a valid answer. That what I got in my storeService

import { AngularFireDatabase } from 'angularfire2/database';
....
constructor(private db: AngularFireDatabase) { }

createStore(ownerID: string, storeData: Store, locationData: Location) {
    let newStore = this.db.list('/stores').push(undefined);
    let newStoreKey = newStore.key;
    let newLocation = this.db.list('/stores').push(undefined);
    let newLocationKey = newLocation.key;
    storeData.ownerID = ownerID;
    storeData.workingRegionID = newLocationKey;
    storeData.name = storeData.name.toLowerCase();

    let tasks = {};
    tasks["/stores/" + newStoreKey] = storeData;
    tasks["/locations/" + newLocationKey] = locationData;

    return this.db.object('/').update(tasks);
}

Answering my own questions.

Q: Do I need to use Firebase's Transactions to make it atomic?

A: No, you can use multi-location update using AngularFire2. Good answer here


问:
Trying to get a snapshot of a variable from firebase gives me an error

Problem

In a social media app I am making with react native and firebase, I am trying to grab the number of comments a post has using the snapshot function of a variable I have saved on my servers, then I am going to add one to this variable when a user adds a new comment. My code to do so is right here:

```<code>firebase.database().ref('posts').child(this.state.passKey).update({
   comments: firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1
})
</code>```

When I actually run this code, I get an error saying:

```<code>Reference.child failed: First argument was an invalid path = "undefined".
Paths must be non-empty strings and can't contain ".","#","$","[", or "["
</code>```

At first I thought this might be that the "this.state.passKey" wasn't actually passing the key, but putting in a key I copied from the server didn't fix the problem.

My Server
-

答:
It looks like you're expecting this bit of code to query the database:

```<code>firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1
</code>```

Unfortunately, it doesn't work that way.  There's no snapshot property on a database Reference object returned by child() or ref().

Instead, you'll need to query the database at that reference, then when you're called back with its value, you can apply it elsewhere.

```<code>var ref = firebase.database().ref('posts/'+this.state.passKey+'/comments')
ref.once('value', function(snapshot) {
    // use the snapshot here
})
</code>```

Body:
Problem

In a social media app I am making with react native and firebase, I am trying to grab the number of comments a post has using the snapshot function of a variable I have saved on my servers, then I am going to add one to this variable when a user adds a new comment. My code to do so is right here:

firebase.database().ref('posts').child(this.state.passKey).update({
   comments: firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1
})

When I actually run this code, I get an error saying:

Reference.child failed: First argument was an invalid path = "undefined".
Paths must be non-empty strings and can't contain ".","#","$","[", or "["

At first I thought this might be that the "this.state.passKey" wasn't actually passing the key, but putting in a key I copied from the server didn't fix the problem.

My Server
-

Body:
It looks like you're expecting this bit of code to query the database:

firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1

Unfortunately, it doesn't work that way.  There's no snapshot property on a database Reference object returned by child() or ref().

Instead, you'll need to query the database at that reference, then when you're called back with its value, you can apply it elsewhere.

var ref = firebase.database().ref('posts/'+this.state.passKey+'/comments')
ref.once('value', function(snapshot) {
    // use the snapshot here
})


问:
javascript syntax structure
I am trying to understand this snippet of code and was hoping I could get some assistance.

the purpose it to use an onclick event to open a bootstrap modal and I have it working but I want to pass in the modal I want to active when I call the script

```<code>    function showThisModal() {
       $(#usage).modal('show');
    }
</code>```

the function on first glance is quite simple it displays the modal I have setup with the id of usage. the part that is throwing me is the '#' in front of the modal name

```<code>    function showThisModal(modID) {
       $(modID).modal('show');
    }
</code>```

doesn't seem to work if I pass in the modal name ---usage---  or if I pass in the modal name with a # in front of it ---#usage---

What is the significance of the '#' and how do I set this function up to pass in the modal name

like so

```<code>onclick='showThisModal($modal)'
</code>```

where $modal is any modal name I have available

答:
The # refers to an id set in the html. so (it looks like you are using jQuery) $('#usage') selects the element with the id of usage. In your function passing in modID you need it to be a string with the hash. In the onclick, where you have $modal you would really want the id, so this.id should do that I think.

edit: so your html would be like <button id="modalWanted" onclick="showThisModal(this.id)">

Title: javascript syntax structure

Body:
I am trying to understand this snippet of code and was hoping I could get some assistance.

the purpose it to use an onclick event to open a bootstrap modal and I have it working but I want to pass in the modal I want to active when I call the script

    function showThisModal() {
       $(#usage).modal('show');
    }

the function on first glance is quite simple it displays the modal I have setup with the id of usage. the part that is throwing me is the '#' in front of the modal name

    function showThisModal(modID) {
       $(modID).modal('show');
    }

doesn't seem to work if I pass in the modal name ---usage---  or if I pass in the modal name with a # in front of it ---#usage---

What is the significance of the '#' and how do I set this function up to pass in the modal name

like so

onclick='showThisModal($modal)'

where $modal is any modal name I have available


问:
Change date format in a Java string
I've a String representing a date.

```<code>String date_s = "2011-01-18 00:00:00.0";
</code>```

I'd like to convert it to a Date and output it in YYYY-MM-DD format.

2011-01-18

How can I achieve this?

Okay, based on the answers I retrieved below, here's something I've tried:

```<code>String date_s = " 2011-01-18 00:00:00.0";
SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss");
Date date = dt.parse(date_s);
SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd");
System.out.println(dt1.format(date));
</code>```

But it outputs 02011-00-1 instead of the desired 2011-01-18. What am I doing wrong?

答:
[edited to include BalusC's corrections]
The SimpleDateFormat class should do the trick:</p>

```<code>String pattern = "yyyy-MM-dd HH:mm:ss.S";
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
  Date date = format.parse("2011-01-18 00:00:00.0");
  System.out.println(date);
} catch (ParseException e) {
  e.printStackTrace();
}
</code>```

Title: Change date format in a Java string

Body:
I've a String representing a date.

String date_s = "2011-01-18 00:00:00.0";

I'd like to convert it to a Date and output it in YYYY-MM-DD format.

2011-01-18

How can I achieve this?

Okay, based on the answers I retrieved below, here's something I've tried:

String date_s = " 2011-01-18 00:00:00.0";
SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss");
Date date = dt.parse(date_s);
SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd");
System.out.println(dt1.format(date));

But it outputs 02011-00-1 instead of the desired 2011-01-18. What am I doing wrong?

Body:
[edited to include BalusC's corrections]
The SimpleDateFormat class should do the trick:

String pattern = "yyyy-MM-dd HH:mm:ss.S";
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
  Date date = format.parse("2011-01-18 00:00:00.0");
  System.out.println(date);
} catch (ParseException e) {
  e.printStackTrace();
}


问:
OktaAuth Javascript + Flask not sending code & state value to Flask
I am new to okta, javascript. I am trying to add OktaAuth to existing Jquery/javascript + Flask app.

I have configured the following in my Javascript. The redirect calls to server-side callback works. But don't pass code, state values for it to proceed. Can you please let me know what is wrong here? Any help is appreciated.

```<code>var authClient = new OktaAuth({
    url: 'https://{okta-url}.com',
    clientId: 'xxxxxx',
    clientSecret: 'yyyyyyyyyy',
    issuer: 'https://{okta-url}.com',
    redirectUri: 'http://{redirect-url}/login'
    //scopes: ['openid', 'email', 'profile']
});

var idToken = authClient.tokenManager.get('idToken');
console.log(JSON.stringify(idToken))
if (idToken) {
    console.log('hi ${idToken.claims.email}!');
}
else if (location.hash) {
    authClient.token.parseFromUrl()
        .then(function (idToken) {
            console.log('hi ${idToken.claims.email}!');
            authClient.tokenManager.add('idToken', idToken);
            console.log(idToken);
        });
    console.log(JSON.stringify(authClient.token))
}
else {
    authClient.token.getWithRedirect({
        responseType: ['id_token', 'code', 'token']
    });
    console.log(authClient.token)
}
</code>```

答:
Warning: It's dangerous to have your client secret in JavaScript code! Exposing a client secret is like revealing your password. You should remove it and consider generating a new client ID/secret just to be safe.

There's two main ways you can use OpenID Connect, which is what OktaAuth uses: with a server-side callback (the code flow), or entirely on the client-side (the implicit flow). You're trying to do both here, which is probably why it's acting weird.

Instead, do this:

```<code>var authClient = new OktaAuth({
    url: 'https://{okta-url}.com',
    clientId: 'xxxxxx',
    issuer: 'default', // Use the default Authorization Server
});

var idToken = authClient.tokenManager.get('idToken');

if (idToken) {
    console.log('hi ${idToken.claims.email}!');
}
else if (location.hash) {
    authClient.token.parseFromUrl()
        .then(function (idToken) {
            authClient.tokenManager.add('idToken', idToken);
            console.log(idToken);
        });
}
else {
    authClient.token.getWithRedirect({
        responseType: 'id_token'
        // Use ['id_token', 'token'] if you also need an access token
    });
}
</code>```

This will get you an ID token that you can use on the client side. Watch the console and network panels for any errors that occur.

Body:
I am new to okta, javascript. I am trying to add OktaAuth to existing Jquery/javascript + Flask app.

I have configured the following in my Javascript. The redirect calls to server-side callback works. But don't pass code, state values for it to proceed. Can you please let me know what is wrong here? Any help is appreciated.

var authClient = new OktaAuth({
    url: 'https://{okta-url}.com',
    clientId: 'xxxxxx',
    clientSecret: 'yyyyyyyyyy',
    issuer: 'https://{okta-url}.com',
    redirectUri: 'http://{redirect-url}/login'
    //scopes: ['openid', 'email', 'profile']
});

var idToken = authClient.tokenManager.get('idToken');
console.log(JSON.stringify(idToken))
if (idToken) {
    console.log('hi ${idToken.claims.email}!');
}
else if (location.hash) {
    authClient.token.parseFromUrl()
        .then(function (idToken) {
            console.log('hi ${idToken.claims.email}!');
            authClient.tokenManager.add('idToken', idToken);
            console.log(idToken);
        });
    console.log(JSON.stringify(authClient.token))
}
else {
    authClient.token.getWithRedirect({
        responseType: ['id_token', 'code', 'token']
    });
    console.log(authClient.token)
}

Body:
Warning: It's dangerous to have your client secret in JavaScript code! Exposing a client secret is like revealing your password. You should remove it and consider generating a new client ID/secret just to be safe.

There's two main ways you can use OpenID Connect, which is what OktaAuth uses: with a server-side callback (the code flow), or entirely on the client-side (the implicit flow). You're trying to do both here, which is probably why it's acting weird.

Instead, do this:

var authClient = new OktaAuth({
    url: 'https://{okta-url}.com',
    clientId: 'xxxxxx',
    issuer: 'default', // Use the default Authorization Server
});

var idToken = authClient.tokenManager.get('idToken');

if (idToken) {
    console.log('hi ${idToken.claims.email}!');
}
else if (location.hash) {
    authClient.token.parseFromUrl()
        .then(function (idToken) {
            authClient.tokenManager.add('idToken', idToken);
            console.log(idToken);
        });
}
else {
    authClient.token.getWithRedirect({
        responseType: 'id_token'
        // Use ['id_token', 'token'] if you also need an access token
    });
}

This will get you an ID token that you can use on the client side. Watch the console and network panels for any errors that occur.


问:
Python building a dictionary with multiprocessing
I am quite beginner with the multiprocessing module. In my code I am trying to build a dictionary with images from a given path. I wrote the following code:

```<code>from multiprocessing import Pool
from PIL import Image, ImageTk
import glob

def process(path):
    print path
    im=ImageTk.PhotoImage(Image.open(path).resize((600, 600), Image.ANTIALIAS))
    name = (path.split('/')[1]).split('.')[0]
    return (name, im)

p = Pool(4)
input = glob.glob('./*.jpg')
image_list = dict(p.map(process, input))
</code>```

If the code works correctly I would expect something like the following:

{'-22': PIL.ImageTk.PhotoImage object at 0x7f6b66507150,

'-23': PIL.ImageTk.PhotoImage object at 0x7f6b66507190, ...
  and so on}

... but instead I get the following error:

```<code>`multiprocessing.pool.MaybeEncodingError: Error sending result:`
`'[('-51', PIL.ImageTk.PhotoImage object at 0x7f6b664f6990),`
`('-47', PIL.ImageTk.PhotoImage object at 0x7f6b664f6fd0),`
`('-54', PIL.ImageTk.PhotoImage object at 0x7f6b66507050),`
`('-13', PIL.ImageTk.PhotoImage object at 0x7f6b665070d0),`
`('-45', PIL.ImageTk.PhotoImage object at 0x7f6b66507110),`
`('-49', PIL.ImageTk.PhotoImage object at 0x7f6b66507150),`
`('-48', PIL.ImageTk.PhotoImage object at 0x7f6b66507190),`
`('-26', PIL.ImageTk.PhotoImage object at 0x7f6b665071d0),`
`('-10', PIL.ImageTk.PhotoImage object at 0x7f6b66507210)]'.`
`Reason: 'UnpickleableError(tkapp object at 0x7f6b67c88e30,)'`
</code>```

How can I solve this?

答:
Multiprocessing is NOT threading. It is a completely separate process with its own interpreter. This has some advantages- you can't accidentally created shared mutable state, which is great! It has some disadvantages though- this is one of them.

All data structures that are passed to or from a multiprocessing process have to be serialized/deserialized. So the return value of that function, behind the scenes, has to be pickled- which as you can see, it cannot be.

With your current design use Threading instead of Multiprocessing.

Body:
I am quite beginner with the multiprocessing module. In my code I am trying to build a dictionary with images from a given path. I wrote the following code:

from multiprocessing import Pool
from PIL import Image, ImageTk
import glob

def process(path):
    print path
    im=ImageTk.PhotoImage(Image.open(path).resize((600, 600), Image.ANTIALIAS))
    name = (path.split('/')[1]).split('.')[0]
    return (name, im)

p = Pool(4)
input = glob.glob('./*.jpg')
image_list = dict(p.map(process, input))

If the code works correctly I would expect something like the following:

{'-22': PIL.ImageTk.PhotoImage object at 0x7f6b66507150,

'-23': PIL.ImageTk.PhotoImage object at 0x7f6b66507190, ...
  and so on}

... but instead I get the following error:

`multiprocessing.pool.MaybeEncodingError: Error sending result:`
`'[('-51', PIL.ImageTk.PhotoImage object at 0x7f6b664f6990),`
`('-47', PIL.ImageTk.PhotoImage object at 0x7f6b664f6fd0),`
`('-54', PIL.ImageTk.PhotoImage object at 0x7f6b66507050),`
`('-13', PIL.ImageTk.PhotoImage object at 0x7f6b665070d0),`
`('-45', PIL.ImageTk.PhotoImage object at 0x7f6b66507110),`
`('-49', PIL.ImageTk.PhotoImage object at 0x7f6b66507150),`
`('-48', PIL.ImageTk.PhotoImage object at 0x7f6b66507190),`
`('-26', PIL.ImageTk.PhotoImage object at 0x7f6b665071d0),`
`('-10', PIL.ImageTk.PhotoImage object at 0x7f6b66507210)]'.`
`Reason: 'UnpickleableError(tkapp object at 0x7f6b67c88e30,)'`

How can I solve this?


问:
Failed to execute 'postMessage' on 'DOMWindow': https://www.youtube.com !== http://localhost:9000
This is the error message that I get:

```<code>Failed to execute 'postMessage' on 'DOMWindow': The target origin provided
('https://www.youtube.com') does not match the recipient window's origin
('http://localhost:9000').
</code>```

I've seen other similar problems where the target origin is http://www.youtube.com and the recipient origin is https://www.youtube.com, but none like mine where the target is https://www.youtube.com and the origin is http://localhost:9000.

I don't get the problem. What is the problem?

How can I fix it?

答:
Setting this seems to fix it:</p>

```<code>  this$1.player = new YouTube.Player(this$1.elementId, {
    videoId: videoId,
    host: 'https://www.youtube.com',
</code>```

Body:
This is the error message that I get:

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided
('https://www.youtube.com') does not match the recipient window's origin
('http://localhost:9000').

I've seen other similar problems where the target origin is http://www.youtube.com and the recipient origin is https://www.youtube.com, but none like mine where the target is https://www.youtube.com and the origin is http://localhost:9000.

I don't get the problem. What is the problem?

How can I fix it?

Body:
Setting this seems to fix it:

  this$1.player = new YouTube.Player(this$1.elementId, {
    videoId: videoId,
    host: 'https://www.youtube.com',


问:
All possible combinations for 3 digits of list never the same
I have a list that looks like:

```<code>A
B
C
D
E
F
G
</code>```

How do I solve this to find all combinations for 3 digits.  The same letter cannot be used in same row.

```<code>ABC
ABD
ABE
ABF
ABG
AGB
</code>```

E.g something like...:

```<code>x = ['a','b','c','d','e']
n = 3
import itertools
aa = [list(comb) for i in range(1, n+2) for comb in itertools.combinations(x, i)]
print(aa)
</code>```

This does not give desired input:

```<code>[['a'], ['b'], ['c'], ['d'], ['e'], ['a', 'b'], ['a', 'c'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['b', 'e'], ['c'
</code>```

答:
The Python Standard Library itertools already has the functionality you are trying to implement. Also you are using it in your code (funnily).

itertools.combinations(a,3) returns all 3-combinations of the a. To convert that to "list of list" you should use .extend() as follows;

```<code>x = ['a','b','c','d','e']
n = 3
import itertools
permutations = []
combinations = []
combinations.extend(itertools.combinations(x,n))
permutations.extend(itertools.permutations(x,n))

print("Permutations;", permutations)
print("\n")
print("Combinations;", combinations)
</code>```

Additionally, I suggest you to search on "Combination, Permutation Difference". As I understood from your question; permutation is what you want. (If you run the code I shared, you will understand the difference easliy.)

Body:
I have a list that looks like:

A
B
C
D
E
F
G

How do I solve this to find all combinations for 3 digits.  The same letter cannot be used in same row.

ABC
ABD
ABE
ABF
ABG
AGB

E.g something like...:

x = ['a','b','c','d','e']
n = 3
import itertools
aa = [list(comb) for i in range(1, n+2) for comb in itertools.combinations(x, i)]
print(aa)

This does not give desired input:

[['a'], ['b'], ['c'], ['d'], ['e'], ['a', 'b'], ['a', 'c'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['b', 'e'], ['c'

Body:
The Python Standard Library itertools already has the functionality you are trying to implement. Also you are using it in your code (funnily).

itertools.combinations(a,3) returns all 3-combinations of the a. To convert that to "list of list" you should use .extend() as follows;

x = ['a','b','c','d','e']
n = 3
import itertools
permutations = []
combinations = []
combinations.extend(itertools.combinations(x,n))
permutations.extend(itertools.permutations(x,n))

print("Permutations;", permutations)
print("\n")
print("Combinations;", combinations)

Additionally, I suggest you to search on "Combination, Permutation Difference". As I understood from your question; permutation is what you want. (If you run the code I shared, you will understand the difference easliy.)


问:
Why the cost function and the last activation function are bound in MXNet?
When we define a deep learning model, we do the following steps:

Specify how the output should be calculated based on the input and the model's parameters.

Specify a cost (loss) function.

Search for the model's parameters by minimizing the cost function.

It looks to me that in MXNet the first two steps are bound. For example, in the following way I define a linear transformation:

```<code># declare a symbolic variable for the model's input
inp = mx.sym.Variable(name = 'inp')
# define how output should be determined by the input
out = mx.sym.FullyConnected(inp, name = 'out', num_hidden = 2)

# specify input and model's parameters
x = mx.nd.array(np.ones(shape = (5,3)))
w = mx.nd.array(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]))
b = mx.nd.array(np.array([7.0, 8.0]))

# calculate output based on the input and parameters
p = out.bind(ctx = mx.cpu(), args = {'inp':x, 'out_weight':w, 'out_bias':b})
print(p.forward()[0].asnumpy())
</code>```

Now, if I want to add a SoftMax transformation on top of it, I need to do the following:

```<code># define the cost function
target = mx.sym.Variable(name = 'target')
cost = mx.symbol.SoftmaxOutput(out, target, name='softmax')

y = mx.nd.array(np.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 0.0], [0.0, 1.0]]))
c = cost.bind(ctx = mx.cpu(), args = {'inp':x, 'out_weight':w, 'out_bias':b, 'target':y})
print(c.forward()[0].asnumpy())
</code>```

What I do not understand, is why do we need to create the symbolic variable target. We would need it only if we want to calculate costs, but so far, we just calculate output based on the input (by doing a linear transformation and SoftMax).

Moreover, we need to provide a numerical value for the target to get the output calculated. So, it looks like it is required but it is not used (the provided value of the target does not change the value of the output).

Finally, we can use the cost object to define a model which we can fit as soon as we have data. But what about the cost function? It has to be specified, but it is not. Basically, it looks like I am forced to use a specific cost bunction just because I use SoftMax. But why?

ADDED

For more statistical / mathematical point of view check here. Although the current question is more pragmatic / programmatic in nature. It is basically: How to decouple the output nonlinearity and the cost function in MXNEt. For example I might want to do a linear transformation and then find the model parameters by minimizing absolute deviation instead of squared one.

答: You can use mx.sym.softmax() if you only want softmax. mx.sym.SoftmaxOutput() contains efficient code for calculating gradient of cross-entropy (negative log loss), which is the most common loss used with softmax. If you want to use your own loss, just use softmax and add a loss on top during training. I should note that you could also replace the SoftmaxOutput layer with a simple softmax during inference if you really want to.

Body:
When we define a deep learning model, we do the following steps:

Specify how the output should be calculated based on the input and the model's parameters.

Specify a cost (loss) function.

Search for the model's parameters by minimizing the cost function.

It looks to me that in MXNet the first two steps are bound. For example, in the following way I define a linear transformation:

# declare a symbolic variable for the model's input
inp = mx.sym.Variable(name = 'inp')
# define how output should be determined by the input
out = mx.sym.FullyConnected(inp, name = 'out', num_hidden = 2)

# specify input and model's parameters
x = mx.nd.array(np.ones(shape = (5,3)))
w = mx.nd.array(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]))
b = mx.nd.array(np.array([7.0, 8.0]))

# calculate output based on the input and parameters
p = out.bind(ctx = mx.cpu(), args = {'inp':x, 'out_weight':w, 'out_bias':b})
print(p.forward()[0].asnumpy())

Now, if I want to add a SoftMax transformation on top of it, I need to do the following:

# define the cost function
target = mx.sym.Variable(name = 'target')
cost = mx.symbol.SoftmaxOutput(out, target, name='softmax')

y = mx.nd.array(np.array([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 0.0], [0.0, 1.0]]))
c = cost.bind(ctx = mx.cpu(), args = {'inp':x, 'out_weight':w, 'out_bias':b, 'target':y})
print(c.forward()[0].asnumpy())

What I do not understand, is why do we need to create the symbolic variable target. We would need it only if we want to calculate costs, but so far, we just calculate output based on the input (by doing a linear transformation and SoftMax).

Moreover, we need to provide a numerical value for the target to get the output calculated. So, it looks like it is required but it is not used (the provided value of the target does not change the value of the output).

Finally, we can use the cost object to define a model which we can fit as soon as we have data. But what about the cost function? It has to be specified, but it is not. Basically, it looks like I am forced to use a specific cost bunction just because I use SoftMax. But why?

ADDED

For more statistical / mathematical point of view check here. Although the current question is more pragmatic / programmatic in nature. It is basically: How to decouple the output nonlinearity and the cost function in MXNEt. For example I might want to do a linear transformation and then find the model parameters by minimizing absolute deviation instead of squared one.


问:
Editing ObservableDictionary via DataGrid
I binded my ObservableDictionary to a DataGrid.ItemSource .
My problem is when i try to edit a value via the datagrid.
I get the exception "Edititem is not allowed for this view".
I've tried different solutions - such as binding the ObservableDictionary.Values.ToList to the datagrid - which makes it possible to edit items but the original value collection is no longer binded.

So, how do I bind ObservableDictionary to a DataGrid and i still keep it editable.
Thank you very much!

答:
You may find the following helpful:

Working with editing commands
  Default commands have been added to the DataGrid to support editing.  These commands and their default input bindings are:

·         BeginEditCommand (F2) ·

CancelEditCommand (Esc) ·

CommitEditCommand (Enter) ·

DeleteCommand (Delete)

The whole blog post is here:
http://blogs.msdn.com/b/vinsibal/archive/2008/10/01/overview-of-the-editing-features-in-the-wpf-datagrid.aspx


问:
Longer plot labels move to the right when arranging plots with plot_grid from cowplot
I was asked this question on Twitter and thought it might be good to have it here.

When making labeled, side-by-side plots with plot_grid(), things work as expected for single-letter labels:

```<code>library(cowplot)

p1 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density(alpha = 0.7) +
  ggtitle("") + theme_minimal()

p2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density(alpha = 0.7) +
  ggtitle("") +
  scale_fill_grey() + theme_minimal()

plot_grid(p1, p2, labels = c("A", "B"))
</code>```

However, if we're using longer strings as labels, the labels move to the right, and they move the more the longer the strings are:

```<code>plot_grid(p1, p2, labels = c("Density plot in color", "In gray"))
</code>```

How can this be fixed?

Disclaimer: I'm the author of the package. Posting this here with answer in the hope it will be useful.

答:
The default settings for the parameters hjust and label_x in plot_grid() are optimized for single-letter labels, and they don't work for longer labels. Overriding the settings fixes the problem:

```<code>plot_grid(p1, p2, labels = c("Density plot in color", "In gray"),
          hjust = 0, label_x = 0.01)
</code>```

In particular, the default hjust setting is hjust = -0.5. This moves the label to the right by an amount equivalent to half its width. This makes sense for single letter labels, because then we can have the letters appear half a letter width away from the left border by setting label_x = 0, and this will work irrespective of label font size or any other plot features the user may have chosen.

However, moving a label by half its width doesn't make any sense at all for longer labels, and in particular labels of differing lengths.

Body:
I was asked this question on Twitter and thought it might be good to have it here.

When making labeled, side-by-side plots with plot_grid(), things work as expected for single-letter labels:

library(cowplot)

p1 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density(alpha = 0.7) +
  ggtitle("") + theme_minimal()

p2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density(alpha = 0.7) +
  ggtitle("") +
  scale_fill_grey() + theme_minimal()

plot_grid(p1, p2, labels = c("A", "B"))

However, if we're using longer strings as labels, the labels move to the right, and they move the more the longer the strings are:

plot_grid(p1, p2, labels = c("Density plot in color", "In gray"))

How can this be fixed?

Disclaimer: I'm the author of the package. Posting this here with answer in the hope it will be useful.

Body:
The default settings for the parameters hjust and label_x in plot_grid() are optimized for single-letter labels, and they don't work for longer labels. Overriding the settings fixes the problem:

plot_grid(p1, p2, labels = c("Density plot in color", "In gray"),
          hjust = 0, label_x = 0.01)

In particular, the default hjust setting is hjust = -0.5. This moves the label to the right by an amount equivalent to half its width. This makes sense for single letter labels, because then we can have the letters appear half a letter width away from the left border by setting label_x = 0, and this will work irrespective of label font size or any other plot features the user may have chosen.

However, moving a label by half its width doesn't make any sense at all for longer labels, and in particular labels of differing lengths.


答:
To understand how the solution process works, try the following:</p>

```<code># get all combinations of n items from given list
def getCombinations(items, n):
    if len(items) < n: return [] # need more items than are remaining
    if n == 0: return [''] # need no more items, return the combination of no items

    [fst, *rst] = items

    # all combinations including the first item in the list
    including = [fst + comb for comb in getCombinations(rst, n-1)]

    # all combinations excluding the first item in the list
    excluding = getCombinations(rst, n)

    both = including + excluding
    return both

x = ['a','b','c','d','e']
n = 3
print(getCombinations(x, n))
# ['abc', 'abd', 'abe', 'acd', 'ace', 'ade', 'bcd', 'bce', 'bde', 'cde']
</code>```

Title: All possible combinations for 3 digits of list never the same

Body:
To understand how the solution process works, try the following:

# get all combinations of n items from given list
def getCombinations(items, n):
    if len(items) < n: return [] # need more items than are remaining
    if n == 0: return [''] # need no more items, return the combination of no items

    [fst, *rst] = items

    # all combinations including the first item in the list
    including = [fst + comb for comb in getCombinations(rst, n-1)]

    # all combinations excluding the first item in the list
    excluding = getCombinations(rst, n)

    both = including + excluding
    return both

x = ['a','b','c','d','e']
n = 3
print(getCombinations(x, n))
# ['abc', 'abd', 'abe', 'acd', 'ace', 'ade', 'bcd', 'bce', 'bde', 'cde']


问:
Phantomjs - saving data to database
I've got an app based on phantomjs that works so:
1. I'm running a php script that gets data from my databse (postgres) as an array,
2. Then via shell_exec I'm running phantomjs script and as argument I'm passing array with data (1),
3. In phantom I'm processing the data - checking domains WHOIS - and collecting for each domain expiration date. As result I'm getting an array that I'm storing in a file,
4. In the end phantom runs php script that gets the data from stored file and saves it in my database.

I'm wondering if there is a better option? Maybe doing everything in the phantomjs script? Maybe there is a js client for postgres?

答:
I'd change workflow starting from step 3 and start saving data right away (PhantomJS is no stranger to crashing so it may not always get to step 4).

You could send data via an AJAX  or POST  request to an endpoint of your own. It could be another PHP script available via HTTP, even if on localhost. So you'd do another page.open to there and send data.

An even more reliable approach: after processing data execute a local PHP script feeding it data via CLI (or save data to a file like before and feed the script path to it).

Title: Phantomjs - saving data to database


问:
Update progressbar from worker thread
I'm building a java program that downloads N elements from a server. I'd like to have a working progress bar, that shows "already downloaded elements"/N percentage. Right now, I update progressbar value by passing to the thread a reference to the GUI object, but it makes the code "ugly" ( GUI class creates the thread, that contains a reference to the GUI itself, creating a sort of loop).
Is there a better way to do it?</p>

答:
One design pattern that may be of use here is the listener pattern.  Register a progress listener with the worker thread.  The listener is then responsible for posting to the GUI (or whatever else may be interested in progress).

Because it in this sort of situation it is highly unlikely that you'd have multiple listeners, it's often better to just have a single listener (decreases complexity and overhead).

In my apps, long running operations are invoked by passing in a ProgressTracker (which has two event types:  percentage and status text).  This information is used to updated the JProgressBar, JLabel, whatever...  I also use the ProgressTracker to check for whether the operation should be canceled.

SwingWorker does have some code that helps with merging multiple updates - but it is built around the assumption that long running tasks should inherit from SwingWorker, which doesn't help separation of concerns.  It's really up to you which direction to head - I personally prefer the operations on my data to have nothing to do with GUI.

Remember to post your updates to any Swing components on the EDT using SwingUtilities.invokeLater()

Title: Update progressbar from worker thread


问:
Value of type 'StorageReference' has no member 'data'?
I am trying to implement a photo picker into my app. For some reason I receive the following errors in two functions, Value of type 'StorageReference' has no member 'data' and Value of type 'StorageReference' has no member 'put'. Here is my code. I am honestly not sure what the problem seems to be. Any input would be greatly appreciated. Thanks.</p>

```<code>import UIKit

import FirebaseStorage
import Photos
class AlbumViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    var id: String = ""
let picker = UIImagePickerController()

    @IBOutlet weak var image1: UIImageView!
    let storage = Storage.storage()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = id
        picker.delegate = self as! UIImagePickerControllerDelegate & UINavigationControllerDelegate
        showPhoto()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //confirm
    func confirm() {
        let myAlert = UIAlertController(title: "Firebase Storage", message: "Image Uploaded", preferredStyle: UIAlertControllerStyle.alert)
        myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
        self.present(myAlert, animated: true, completion: nil)
    }

  //retrive pictures
    @IBAction func accessAlbum(_ sender: UIBarButtonItem) {

        picker.allowsEditing = false
        picker.sourceType = .photoLibrary
        picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
        picker.modalPresentationStyle = .popover
        present(picker, animated: true, completion: nil)
        picker.popoverPresentationController?.barButtonItem = sender

    }
    //for image size
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        print(info)

        var selectedImageFromPicker: UIImage?

        if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage
        {
            selectedImageFromPicker = editedImage
        }else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage{
            selectedImageFromPicker = originalImage
        }

        if let selectedImage = selectedImageFromPicker
        {
            image1.image = selectedImage.resizedImageWithinRect(rectSize: CGSize(width: 300, height: 300))
            image1.contentMode = .scaleAspectFit //3

        }
        dismiss(animated: true, completion: nil)
    }

    @IBAction func savePic(_ sender: Any) {
        var data = Data()
        data = UIImagePNGRepresentation(image1.image!)! as Data
        // set upload path
        let imageName = "Photos/" + id + ".png"

        // ----  Upload the image to firebase storage
        let storageRef = storage.reference(withPath: imageName)
        let metaData = StorageMetadata()
        metaData.contentType = "image/png"
        storageRef.put(data, metadata: metaData){(metaData,error) in //ERROR HERE
            if let error = error {
                print(error.localizedDescription)
                return
            }else{
                self.confirm()
            }
        }
    }

    func showPhoto() {
        let imageName = "Photos/" + id + ".png"

        // Create a storage reference  path with image name

        let storageRef = storage.reference(withPath: imageName)

        storageRef.data(withMaxSize: 3 * 1024 * 1024) { (data, error) // ERROR HERE-> Void in
            // Create a UIImage, add it to the array
            if let imgData = data {
                let image = UIImage(data: imgData)

                DispatchQueue.main.async {
                    self.myImageView.image = image
                }
                //

            } else {
                print ("no image data")
            }

        }
    }

}
extension UIImage {

    /// Returns a image that fills in newSize
    func resizedImage(newSize: CGSize) -> UIImage {
        // Guard newSize is different
        guard self.size != newSize else { return self }

        UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0);
        // self.drawInRect(CGRect(0, 0, newSize.width, newSize.height))
        self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return newImage
    }

    /// Returns a resized image that fits in rectSize, keeping it's aspect ratio
    /// Note that the new image size is not rectSize, but within it.
    func resizedImageWithinRect(rectSize: CGSize) -> UIImage {
        let widthFactor = size.width / rectSize.width
        let heightFactor = size.height / rectSize.height

        var resizeFactor = widthFactor
        if size.height > size.width {
            resizeFactor = heightFactor
        }

        let newSize = CGSize(width: size.width/resizeFactor, height: size.height/resizeFactor)

        // CGSize(size.width/resizeFactor, size.height/resizeFactor)
        let resized = resizedImage(newSize: newSize)
        return resized
    }

}
</code>```

答:
StorageReference has no methods called put or data.

Perhaps you were trying to use putData and getData?

Body:
I am trying to implement a photo picker into my app. For some reason I receive the following errors in two functions, Value of type 'StorageReference' has no member 'data' and Value of type 'StorageReference' has no member 'put'. Here is my code. I am honestly not sure what the problem seems to be. Any input would be greatly appreciated. Thanks.

import UIKit

import FirebaseStorage
import Photos
class AlbumViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    var id: String = ""
let picker = UIImagePickerController()

    @IBOutlet weak var image1: UIImageView!
    let storage = Storage.storage()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = id
        picker.delegate = self as! UIImagePickerControllerDelegate & UINavigationControllerDelegate
        showPhoto()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //confirm
    func confirm() {
        let myAlert = UIAlertController(title: "Firebase Storage", message: "Image Uploaded", preferredStyle: UIAlertControllerStyle.alert)
        myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
        self.present(myAlert, animated: true, completion: nil)
    }

  //retrive pictures
    @IBAction func accessAlbum(_ sender: UIBarButtonItem) {

        picker.allowsEditing = false
        picker.sourceType = .photoLibrary
        picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
        picker.modalPresentationStyle = .popover
        present(picker, animated: true, completion: nil)
        picker.popoverPresentationController?.barButtonItem = sender

    }
    //for image size
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        print(info)

        var selectedImageFromPicker: UIImage?

        if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage
        {
            selectedImageFromPicker = editedImage
        }else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage{
            selectedImageFromPicker = originalImage
        }

        if let selectedImage = selectedImageFromPicker
        {
            image1.image = selectedImage.resizedImageWithinRect(rectSize: CGSize(width: 300, height: 300))
            image1.contentMode = .scaleAspectFit //3

        }
        dismiss(animated: true, completion: nil)
    }

    @IBAction func savePic(_ sender: Any) {
        var data = Data()
        data = UIImagePNGRepresentation(image1.image!)! as Data
        // set upload path
        let imageName = "Photos/" + id + ".png"

        // ----  Upload the image to firebase storage
        let storageRef = storage.reference(withPath: imageName)
        let metaData = StorageMetadata()
        metaData.contentType = "image/png"
        storageRef.put(data, metadata: metaData){(metaData,error) in //ERROR HERE
            if let error = error {
                print(error.localizedDescription)
                return
            }else{
                self.confirm()
            }
        }
    }

    func showPhoto() {
        let imageName = "Photos/" + id + ".png"

        // Create a storage reference  path with image name

        let storageRef = storage.reference(withPath: imageName)

        storageRef.data(withMaxSize: 3 * 1024 * 1024) { (data, error) // ERROR HERE-> Void in
            // Create a UIImage, add it to the array
            if let imgData = data {
                let image = UIImage(data: imgData)

                DispatchQueue.main.async {
                    self.myImageView.image = image
                }
                //

            } else {
                print ("no image data")
            }

        }
    }

}
extension UIImage {

    /// Returns a image that fills in newSize
    func resizedImage(newSize: CGSize) -> UIImage {
        // Guard newSize is different
        guard self.size != newSize else { return self }

        UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0);
        // self.drawInRect(CGRect(0, 0, newSize.width, newSize.height))
        self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return newImage
    }

    /// Returns a resized image that fits in rectSize, keeping it's aspect ratio
    /// Note that the new image size is not rectSize, but within it.
    func resizedImageWithinRect(rectSize: CGSize) -> UIImage {
        let widthFactor = size.width / rectSize.width
        let heightFactor = size.height / rectSize.height

        var resizeFactor = widthFactor
        if size.height > size.width {
            resizeFactor = heightFactor
        }

        let newSize = CGSize(width: size.width/resizeFactor, height: size.height/resizeFactor)

        // CGSize(size.width/resizeFactor, size.height/resizeFactor)
        let resized = resizedImage(newSize: newSize)
        return resized
    }

}


问:
How do items in an object from one function transfer into another function?
I am learning react redux, I am using firebase for storing data.

I installed thunk middleware. Everything works, I just don't understand why.

As I understand it, the const expense is an object which is in another function's scope. How can addExpense gets access to it?

```<code>export const addExpense = (expense) => ({
    type: 'ADD_EXPENSE',
    expense
});

export const startAddExpense = (expenseData = {}) => {
    return (dispatch) => {
        const {
            description = '',
            note = '',
            amount = 0,
            createdAt = 0
        } = expenseData;
        const expense = { description, note, amount, createdAt };

        database.ref('expenses').push(expense).then((ref) => {
            dispatch(addExpense({
                id: ref.key,
                ...expense
            }));
        });
    };
};
</code>```

答:
startAddExpense is passing the const expense object to your addExpense function, along with an id field. It just so happens that the argument to addExpense is also called expense, which is where you might be getting confused.

Hope that clears it up.

Body:
I am learning react redux, I am using firebase for storing data.

I installed thunk middleware. Everything works, I just don't understand why.

As I understand it, the const expense is an object which is in another function's scope. How can addExpense gets access to it?

export const addExpense = (expense) => ({
    type: 'ADD_EXPENSE',
    expense
});

export const startAddExpense = (expenseData = {}) => {
    return (dispatch) => {
        const {
            description = '',
            note = '',
            amount = 0,
            createdAt = 0
        } = expenseData;
        const expense = { description, note, amount, createdAt };

        database.ref('expenses').push(expense).then((ref) => {
            dispatch(addExpense({
                id: ref.key,
                ...expense
            }));
        });
    };
};


答:
combinations works on strings not lists, so you should first turn it into a string using: ''.join(x)

```<code>from itertools import combinations
x = ['a', 'b', 'c', 'd', 'e']
n = 3
aa = combinations(''.join(x), n)
for comb in aa:
    print(''.join(comb))
</code>```

OUTPUT

```<code>abc
abd
abe
acd
ace
ade
bcd
bce
bde
cde
</code>```

Or as a one-liner:

```<code>[''.join(comb) for comb in combinations(''.join(x), n)]
</code>```

Body:
combinations works on strings not lists, so you should first turn it into a string using: ''.join(x)

from itertools import combinations
x = ['a', 'b', 'c', 'd', 'e']
n = 3
aa = combinations(''.join(x), n)
for comb in aa:
    print(''.join(comb))

OUTPUT

abc
abd
abe
acd
ace
ade
bcd
bce
bde
cde

Or as a one-liner:

[''.join(comb) for comb in combinations(''.join(x), n)]


问:
push logs in S3 to dynamoDB continuously
we have our application logs pumped to S3 via Kinesis Firehose. we want this data to also flow to DynamoDB so that we can efficiently query the data to be presented in web UI (Ember app). need for this is so that users are able to filter and sort the data and so on. basically to support querying abilities via web UI.

i looked into AWS Data pipeline. this is reliable but more tuned to one time imports or scheduled imports. we want the flow of data from s3 to dynamoDB to be continuous.

what other choices are out there to achieve this? moving data from S3 to dynamoDB isn't a very unique requirement. so how have you solved this problem?

Is an S3 event triggered lambda an option? if yes, then how to make this lambda fault tolerant?

答:
For Full Text Querying

You can design your solution as follows for better querying using AWS Elasticsearch as the destination for rich querying.

Setup Kinesis Firehouse Destination to Amazon Elastic Search. This will allow you to do full text querying from your Web UI.

You can choose to either back up failed records only or all records. If you choose all records, Kinesis Firehose backs up all incoming source data to your S3 bucket concurrently with data delivery to Amazon Elasticsearch.

For Basic Querying

If you plan to use DynamoDB to store the metadata of logs its better to configure S3 Trigger to Lambda which will retrieve the file and update the metadata to DynamoDB.


问:
how to cap kotlin coroutines maximum concurrency
I've got a Sequence (from File.walkTopDown) and I need to run a long-running operation on each of them.  I'd like to use Kotlin best practices / coroutines, but I either get no parallelism, or way too much parallelism and hit a "too many open files" IO error.

```<code>File("/Users/me/Pictures/").walkTopDown()
    .onFail { file, ex -> println("ERROR: $file caused $ex") }
    .filter { ... only big images... }
    .map { file ->
        async { // I *think* I want async and not "launch"...
            ImageProcessor.fromFile(file)
        }
    }
</code>```

This doesn't seem to run it in parallel, and my multi-core CPU never goes above 1 CPU's worth.  Is there a way with coroutines to run "NumberOfCores parallel operations" worth of Deferred jobs?

I looked at Multithreading using Kotlin Coroutines which first creates ALL the jobs then joins them, but that means completing the Sequence/file tree walk completly bfore the heavy processing join step, and that seems... iffy!  Splitting it into a collect and a process step means the collection could run way ahead of the processing.

```<code>val jobs = ... the Sequence above...
    .toSet()
println("Found ${jobs.size}")
jobs.forEach { it.await() }
</code>```

答:
I got it working with a Channel.  But maybe I'm being redundant with your way?</p>

```<code>val pipe = ArrayChannel<Deferred<ImageFile>>(20)
launch {
    while (!(pipe.isEmpty && pipe.isClosedForSend)) {
        imageFiles.add(pipe.receive().await())
    }
    println("pipe closed")
}
File("/Users/me/").walkTopDown()
        .onFail { file, ex -> println("ERROR: $file caused $ex") }
        .forEach { pipe.send(async { ImageFile.fromFile(it) }) }
pipe.close()
</code>```

Body:
I've got a Sequence (from File.walkTopDown) and I need to run a long-running operation on each of them.  I'd like to use Kotlin best practices / coroutines, but I either get no parallelism, or way too much parallelism and hit a "too many open files" IO error.

File("/Users/me/Pictures/").walkTopDown()
    .onFail { file, ex -> println("ERROR: $file caused $ex") }
    .filter { ... only big images... }
    .map { file ->
        async { // I *think* I want async and not "launch"...
            ImageProcessor.fromFile(file)
        }
    }

This doesn't seem to run it in parallel, and my multi-core CPU never goes above 1 CPU's worth.  Is there a way with coroutines to run "NumberOfCores parallel operations" worth of Deferred jobs?

I looked at Multithreading using Kotlin Coroutines which first creates ALL the jobs then joins them, but that means completing the Sequence/file tree walk completly bfore the heavy processing join step, and that seems... iffy!  Splitting it into a collect and a process step means the collection could run way ahead of the processing.

val jobs = ... the Sequence above...
    .toSet()
println("Found ${jobs.size}")
jobs.forEach { it.await() }

Body:
I got it working with a Channel.  But maybe I'm being redundant with your way?

val pipe = ArrayChannel<Deferred<ImageFile>>(20)
launch {
    while (!(pipe.isEmpty && pipe.isClosedForSend)) {
        imageFiles.add(pipe.receive().await())
    }
    println("pipe closed")
}
File("/Users/me/").walkTopDown()
        .onFail { file, ex -> println("ERROR: $file caused $ex") }
        .forEach { pipe.send(async { ImageFile.fromFile(it) }) }
pipe.close()


问:
Reading key-value json data into google sheet cells
I am looking to bring in the "bid" values from each "ticker" from this API call https://api.etherdelta.com/returnTicker into Google Sheet cells.

An example cell value will have something like: =crypt("PPT).

Here is the code I have so far, but I am having a hard time figuring out how I can get the data for each ticker (I know I haven't declared "ticker" anywhere in the code).

```<code>function crypt(ticker) {

  var url = "https://api.etherdelta.com/returnTicker";

  var response = UrlFetchApp.fetch(url);
  var text = response.getContentText();

  var json = JSON.parse(text);
  var price = json[bid];

  return parseFloat(bid);

}
</code>```

答:
How about the following modifications?

Modification points :

Each ticker name has a header of ETH_.

ETH_ + ticker is a key of the object.

When =crypt("PPT") is used, the key is ETH_PPT and "bid" you want is in the value of ETH_PPT.

The modified script which was reflected above is as follows.

Modified script :

```<code>function crypt(ticker) {

  var url = "https://api.etherdelta.com/returnTicker";

  var response = UrlFetchApp.fetch(url);
  var text = response.getContentText();

  var json = JSON.parse(text);
  var price = json["ETH_" + ticker].bid; // Modified

  return parseFloat(price); // Modified

}
</code>```

This modified script retrieves the value of bid for each ticker by putting =crypt("PPT") to a cell in the spreadsheet.

Note :

It seems that an error response is sometimes returned from the URL.

If I misunderstand your question, I'm sorry.

Body:
I am looking to bring in the "bid" values from each "ticker" from this API call https://api.etherdelta.com/returnTicker into Google Sheet cells.

An example cell value will have something like: =crypt("PPT).

Here is the code I have so far, but I am having a hard time figuring out how I can get the data for each ticker (I know I haven't declared "ticker" anywhere in the code).

function crypt(ticker) {

  var url = "https://api.etherdelta.com/returnTicker";

  var response = UrlFetchApp.fetch(url);
  var text = response.getContentText();

  var json = JSON.parse(text);
  var price = json[bid];

  return parseFloat(bid);

}

Body:
How about the following modifications?

Modification points :

Each ticker name has a header of ETH_.

ETH_ + ticker is a key of the object.

When =crypt("PPT") is used, the key is ETH_PPT and "bid" you want is in the value of ETH_PPT.

The modified script which was reflected above is as follows.

Modified script :

function crypt(ticker) {

  var url = "https://api.etherdelta.com/returnTicker";

  var response = UrlFetchApp.fetch(url);
  var text = response.getContentText();

  var json = JSON.parse(text);
  var price = json["ETH_" + ticker].bid; // Modified

  return parseFloat(price); // Modified

}

This modified script retrieves the value of bid for each ticker by putting =crypt("PPT") to a cell in the spreadsheet.

Note :

It seems that an error response is sometimes returned from the URL.

If I misunderstand your question, I'm sorry.


问:
How to type a new line character in SQL Server Management Studio
In the "datagrid" view of an open table of data, how can I type a new line character into an nvarchar field directly in SSMS?

Is there an alt code?

答:
This is possible if you have an existing Newline character in the row or another row.

Select the square-box that represents the existing Newline character, copy it (control-C), and then paste it (control-V) where you want it to be.

This is slightly cheesy, but I actually did get it to work in SSMS 2008 and I was not able to get any of the other suggestions (control-enter, alt-13, or any alt-##) to work.


问:
Why can you still modify a List<> when it has the "in" parameter modifier in C# 7.2
Earlier this week, C# 7.2 was released with the new feature of "in" parameter modifier see release notes here

The details provided in the release notes are:"The in modifier on parameters, to specify that an argument is passed by reference but not modified by the called method."

There is not a lot of documentation out on this new feature yet, so I have been trying it out. Seems to work as expected for primitive types, and prevents access to object properties. However, for lists you can still call the methods to modify a List (i.e. Add, Remove, Reverse) and you can actually modify an element directly.

```<code>    static void Main(string[] args)
    {
        var test = new List<int>();
        test.Add(5);
        Console.WriteLine(test[0]);
        TestMethod(test);
        Console.WriteLine(test[0]);
    }

    public static void TestMethod(in List<int> myList)
    {
        myList[0] = 10;
        myList.Add(7);
        myList.Remove(2);
    }
</code>```

My question is, why can still modify collections when using the "in" parameter modifier?

答:
These points from the documentation really clarifies the intent of the in modifier:

There are several ways in which the compiler ensures that the read-only nature of an in argument is enforced. First of all, the called method can't directly assign to an in parameter.

Ok so that means we cannot do this:

```<code>public void Something(in Point3D point1)
{
    point1 = new Point3D(); // not allowed
}
</code>```

It can't directly assign to any field of an in parameter.

Ok so that means we cannot do this as well:

```<code>public void Something(in Point3D point1)
{
    point1.X = 10; // not allowed
}
</code>```

In addition, you cannot pass an in parameter to any method demanding the ref or out modifier.

Ok so that means we cannot do below as well for obvious reasons because the method we are calling can set the reference which will break the above first rule:

```<code>public void Something(in Point3D point1)
{
    SomethingElse(ref point1); // not allowed
}

public void SomethingElse(ref Point3D pointAnother)
{}
</code>```

The compiler enforces that the in argument is a readonly variable. You can call any instance method that uses pass-by-value semantics. In those instances, a copy of the in parameter is created. Because the compiler can create a temporary variable for any in parameter, you can also specify default values for any in parameter.

So what I gather from the above is that you can pass a List<T> and the called method will be able to add items to it but it will not be able to assign another list to it or changes it's fields.

In Conclusion

Why can you still modify a List<> when it has the “in” parameter modifier in C# 7.2

You are allowed to do that because you are not breaking any of the rules.

Body:
Earlier this week, C# 7.2 was released with the new feature of "in" parameter modifier see release notes here

The details provided in the release notes are:"The in modifier on parameters, to specify that an argument is passed by reference but not modified by the called method."

There is not a lot of documentation out on this new feature yet, so I have been trying it out. Seems to work as expected for primitive types, and prevents access to object properties. However, for lists you can still call the methods to modify a List (i.e. Add, Remove, Reverse) and you can actually modify an element directly.

    static void Main(string[] args)
    {
        var test = new List<int>();
        test.Add(5);
        Console.WriteLine(test[0]);
        TestMethod(test);
        Console.WriteLine(test[0]);
    }

    public static void TestMethod(in List<int> myList)
    {
        myList[0] = 10;
        myList.Add(7);
        myList.Remove(2);
    }

My question is, why can still modify collections when using the "in" parameter modifier?

Body:
These points from the documentation really clarifies the intent of the in modifier:

There are several ways in which the compiler ensures that the read-only nature of an in argument is enforced. First of all, the called method can't directly assign to an in parameter.

Ok so that means we cannot do this:

public void Something(in Point3D point1)
{
    point1 = new Point3D(); // not allowed
}

It can't directly assign to any field of an in parameter.

Ok so that means we cannot do this as well:

public void Something(in Point3D point1)
{
    point1.X = 10; // not allowed
}

In addition, you cannot pass an in parameter to any method demanding the ref or out modifier.

Ok so that means we cannot do below as well for obvious reasons because the method we are calling can set the reference which will break the above first rule:

public void Something(in Point3D point1)
{
    SomethingElse(ref point1); // not allowed
}

public void SomethingElse(ref Point3D pointAnother)
{}

The compiler enforces that the in argument is a readonly variable. You can call any instance method that uses pass-by-value semantics. In those instances, a copy of the in parameter is created. Because the compiler can create a temporary variable for any in parameter, you can also specify default values for any in parameter.

So what I gather from the above is that you can pass a List<T> and the called method will be able to add items to it but it will not be able to assign another list to it or changes it's fields.

In Conclusion

Why can you still modify a List<> when it has the “in” parameter modifier in C# 7.2

You are allowed to do that because you are not breaking any of the rules.


问:
Java finding first & last letter. Last not working
My char lastLetter line is not working. Any help on this will be useful.

```<code>for (int k=0; k<userInput.length; k++)
{
    char firstLetter = userInput[k].charAt(0);
    char lastLetter = userInput[k].charAt(userInput[k].lenght-1);

    System.out.print(userInput[k] +" First Letter "+ firstLetter);
    System.out.print(userInput[k] +" Last letter " + lastLetter);

    System.out.print("\n");
}
</code>```

答:
Judging by use of .charAt(int), userInput is most likely an array of Strings

In this case, Strings do not have a length field, but they do have a length method

```<code>char lastLetter = userInput[k].charAt(userInput[k].length() - 1);
</code>```

Body:
My char lastLetter line is not working. Any help on this will be useful.

for (int k=0; k<userInput.length; k++)
{
    char firstLetter = userInput[k].charAt(0);
    char lastLetter = userInput[k].charAt(userInput[k].lenght-1);

    System.out.print(userInput[k] +" First Letter "+ firstLetter);
    System.out.print(userInput[k] +" Last letter " + lastLetter);

    System.out.print("\n");
}

Body:
Judging by use of .charAt(int), userInput is most likely an array of Strings

In this case, Strings do not have a length field, but they do have a length method

char lastLetter = userInput[k].charAt(userInput[k].length() - 1);


问:
It telling me that I have make a mistake when I am using  on php
I am trying to make a chart on php which when the number is 2/4/6/8/10 ... etc. The number will be on white color and it's box on green color, and when the number is 1/3/5/7/9 ... etc, the number will be on black color and it's box on red color.

Everything is ok but when I am adding style="color:white(/black);" and style="background-color:red(/green);" on the  and I am saving it, my window is telling me there is a mistake background-color/color ... etc. on this line. Can somebody help me?

Here is my code:

```<code><?php
    echo '<table>'
    for ($i=1; $i<=10; $i++) {
        echo "<tr>  </tr>";
        for ($j=1; $j<=10; $j++) {
            $t= $i * $j;
            if ($j%2==1) {
                echo "<td style="background-color:red;color:black"> $t </td>";
                } else  {
                echo "<td background-color:green;color:white>  $t </td>";
                }
        }
    }
    echo '</table>';
?>
</code>```

I have included the html/head/body etc.

答:
Change one of the sets of quotes to single quotes in the echo statement
 You are exiting the statement otherwise which will give you an error.

```<code>echo "<td style='background-color:red;color:black'> $t </td>";
</code>```

And second echo doesn't contain style attribute at all. This also needs to be fixed.

Title: It telling me that I have make a mistake when I am using <td style="color:...;"> on php

Body:
I am trying to make a chart on php which when the number is 2/4/6/8/10 ... etc. The number will be on white color and it's box on green color, and when the number is 1/3/5/7/9 ... etc, the number will be on black color and it's box on red color.

Everything is ok but when I am adding style="color:white(/black);" and style="background-color:red(/green);" on the  and I am saving it, my window is telling me there is a mistake background-color/color ... etc. on this line. Can somebody help me?

Here is my code:

<?php
    echo '
'
    for ($i=1; $i<=10; $i++) {
        echo "

";
        for ($j=1; $j<=10; $j++) {
            $t= $i * $j;
            if ($j%2==1) {
                echo " $t 	";
                } else  {
                echo "  $t 	";
                }
        }
    }
    echo '
';
?>

I have included the html/head/body etc.

Body:
Change one of the sets of quotes to single quotes in the echo statement
 You are exiting the statement otherwise which will give you an error.

echo "<td style='background-color:red;color:black'> $t </td>";

And second echo doesn't contain style attribute at all. This also needs to be fixed.


问:
What is the fastest way to insert text into a
 element with javascript?
I am writing a long series of numbers to my document, one at a time. document.write() handles this fine, but using innerHTML with a
 element is sluggish, and rather quickly crashes my tab.  Is there some way to insert text into a
 element that is as fast as document.write?

答: Is displaying the results one at a time critical? You could write them into a string one at a time, and then flush it into the div with innerHTML.</p>

Title: What is the fastest way to insert text into a <div> element with javascript?