问:
Why does not this callback function cause race condition?
I don't understand why the following code does not cause potential race condition. It looks like count and totalBytes are variables that can be accessed by multiple callback functions and I felt count-- and totalBytes += stats.size might cause racing because you can't guarantee the orders of the callback functions passed to fs.stat() (as it's an IO operation).

```<code>var fs = require('fs');

var count = 0,
    totalBytes = 0;

function calculateByteSize() {
    fs.readdir(".", function (err, filenames) {
        var i;
        count = filenames.length;

        for (i = 0; i < filenames.length; i++) {
            fs.stat("./" + filenames[i], function (err, stats) {
                totalBytes += stats.size;
                count--;
                if (count === 0) {
                    console.log(totalBytes);
                }
            });
        }
    });
}

calculateByteSize();
</code>```

答: If you had reached the count over async function, yes there had been race condition here. But your function is sync, it means that your function is atomic. Due to this no race condition here. If you had used async function here, despite single thread, due to context switching in the event loop, race condition had taken place.</p>

Body:
I don't understand why the following code does not cause potential race condition. It looks like count and totalBytes are variables that can be accessed by multiple callback functions and I felt count-- and totalBytes += stats.size might cause racing because you can't guarantee the orders of the callback functions passed to fs.stat() (as it's an IO operation).

var fs = require('fs');

var count = 0,
    totalBytes = 0;

function calculateByteSize() {
    fs.readdir(".", function (err, filenames) {
        var i;
        count = filenames.length;

        for (i = 0; i < filenames.length; i++) {
            fs.stat("./" + filenames[i], function (err, stats) {
                totalBytes += stats.size;
                count--;
                if (count === 0) {
                    console.log(totalBytes);
                }
            });
        }
    });
}

calculateByteSize();


来源: stackexchange

问:
coding a server on using restlet 2.0
I am coding a server for a project of mine using restlet 2.0. I have a java class which starts the server (starting it on a port and all those stuff). I am stuck at a point where i need to map the uri's of different services i intend to offer. If i were to include uri mapping part in a servlet how do i go about it. what are the changes i need to make in the web.xml. i have found very little documentation regarding this.
Any help appreciated</p>

答:
In fact, the routing configuration must be done in your Restlet application class. You need to override the createInboundRoot method to attach your resources to paths, as described below:

```<code>public class MyRestletApplication extends Application {
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/path/{id}", MyServerResource.class);
        (...)
        return router;
    }
}
</code>```

The configured resources can be then reached through the configured Restlet server. For example, with the address http://localhost:8082/path/12 if you implement your Restlet server as following:

```<code>public static void main(String[] args) {
    try {
        Server server = new Server(Protocol.HTTP, 8182);
        server.setNext(new MyRestletApplication());
        server.start();
        (...)
    } catch(Exception ex) {}
}
</code>```

Restlet also provides a servlet adapter with its org.restlet.ext.servlet extension. The latter allows using the ServerServlet servlet in order to access the configured resources. When configuring this servlet you need to specify the application class to use (the application contains the paths for your resources) through the org.restlet.application context parameter. The servlet can be configured as every servlet and be mapped on the /* pattern, as described below:

```<code><web-app>
    <context-param>
        <param-name>org.restlet.application</param-name>
        <param-value>org.restlet.example.MyApplication</param-value>
    </context-param>

    <servlet>
        <servlet-name>ServerServlet</servlet-name>
        <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ServerServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>
</code>```

In this case, your RESTful application implemented with Restlet will be accessed through a servlet container. In this case, your application needs to be packaged as a Java EE web application and will be reached with address: http://localhost:8080/mywebapp/path/12.

Hope it'll help you.
Thierry

ContentLicense: CC BY-SA 2.5

Title: coding a server on using restlet 2.0

Body:
In fact, the routing configuration must be done in your Restlet application class. You need to override the createInboundRoot method to attach your resources to paths, as described below:

public class MyRestletApplication extends Application {
    public Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attach("/path/{id}", MyServerResource.class);
        (...)
        return router;
    }
}

The configured resources can be then reached through the configured Restlet server. For example, with the address http://localhost:8082/path/12 if you implement your Restlet server as following:

public static void main(String[] args) {
    try {
        Server server = new Server(Protocol.HTTP, 8182);
        server.setNext(new MyRestletApplication());
        server.start();
        (...)
    } catch(Exception ex) {}
}

Restlet also provides a servlet adapter with its org.restlet.ext.servlet extension. The latter allows using the ServerServlet servlet in order to access the configured resources. When configuring this servlet you need to specify the application class to use (the application contains the paths for your resources) through the org.restlet.application context parameter. The servlet can be configured as every servlet and be mapped on the /* pattern, as described below:

<web-app>
    <context-param>
        <param-name>org.restlet.application</param-name>
        <param-value>org.restlet.example.MyApplication</param-value>
    </context-param>

    <servlet>
        <servlet-name>ServerServlet</servlet-name>
        <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ServerServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

In this case, your RESTful application implemented with Restlet will be accessed through a servlet container. In this case, your application needs to be packaged as a Java EE web application and will be reached with address: http://localhost:8080/mywebapp/path/12.

Hope it'll help you.
Thierry

ContentLicense: CC BY-SA 3.0


问:
Change start angle/ first slice angle of PIE chart in python-pptx
I have a pie chart with multiple segments in it. I want certain pie segment to start from an angle I provide.

答:
Pie chart "rotation" features in PowerPoint are limited to positioning the "start" angle of the first segment, with segments growing in the clockwise direction only.

So you can specify that the first element appears at 70-degrees (clockwise from the 12-o'clock position).

Currently there is no API support for this, but the value is in the XML at the location mentioned by @Saleh above: /c:chartSpace/c:chart/c:plotArea/c:pieChart/c:firstSliceAng

You can access the c:doughnutChart element on:

```<code>chart.plots[0]._element
</code>```

And print it with:

```<code>print(chart.plots[0]._element.xml)
</code>```

If it happens to already have a c:firstSliceAng element on it, you can just change the setting, perhaps something like this:

```<code>pieChart = chart.plots[0]._element
firstSliceAng = pieChart.xpath("./c:firstSliceAng")
firstSliceAng["val"] = "70"
</code>```

If there is no firstSliceAng element there you need to use lxml calls to add it first.

ContentLicense: CC BY-SA 4.0

Body:
Pie chart "rotation" features in PowerPoint are limited to positioning the "start" angle of the first segment, with segments growing in the clockwise direction only.

So you can specify that the first element appears at 70-degrees (clockwise from the 12-o'clock position).

Currently there is no API support for this, but the value is in the XML at the location mentioned by @Saleh above: /c:chartSpace/c:chart/c:plotArea/c:pieChart/c:firstSliceAng

You can access the c:doughnutChart element on:

chart.plots[0]._element

And print it with:

print(chart.plots[0]._element.xml)

If it happens to already have a c:firstSliceAng element on it, you can just change the setting, perhaps something like this:

pieChart = chart.plots[0]._element
firstSliceAng = pieChart.xpath("./c:firstSliceAng")
firstSliceAng["val"] = "70"

If there is no firstSliceAng element there you need to use lxml calls to add it first.


问:
console.log() displays the correct value, but when assigned it to a variable, it is undefined - Angular, firestore
I'm working on an Angular application and want to get some data from Firestore. But my function always returns undefined. When I, however, console.log() the value before returning it, it displays the correct value.

```<code>getToken(user_id) {
    this.afs.collection("tokens").ref.doc(user_id).get().then((doc) => {
      if (doc.exists) {
        console.log(doc.data().user_token); //displays the correct user_token value
        return doc.data().user_token; //returns undefined!
      }
    });
}
</code>```

Shouldn't both values be the same?

答:
It appears that you are not returning the Promise from your function - there is no return statement in the getToken function at all, so the function itself just returns undefined. The internal return statement you have will resolve the promise, which is good, but you have to handle that resolution.

If you return the promise like so:

```<code>getToken(user_id) {
    return this.afs.collection("tokens").ref.doc(user_id).get().then((doc) => {
      if (doc.exists) {
        console.log(doc.data().user_token); //displays the correct user_token value
        return doc.data().user_token;
      }
    });
}
</code>```

You should be able to access the user_token asynchronously when the promise resolves by doing the following:

```<code>getToken(user_id).then(user_token => {
  handle_user_token_here(user_token);
});
</code>```

Note: The function as modified will return a promise. Therefore, you cannot simply the following:

```<code>let user_token = getToken(user_id);
// user_token here is a Promise object, not the token!
handle_user_token_here(user_token); // will not work.
</code>```

You can do this though:

```<code>let user_token = getToken(user_id);
// user_token here is a Promise object, not the token!
user_token.then(user_token => handle_user_token_here(user_token)); // will work
</code>```

Body:
I'm working on an Angular application and want to get some data from Firestore. But my function always returns undefined. When I, however, console.log() the value before returning it, it displays the correct value.

getToken(user_id) {
    this.afs.collection("tokens").ref.doc(user_id).get().then((doc) => {
      if (doc.exists) {
        console.log(doc.data().user_token); //displays the correct user_token value
        return doc.data().user_token; //returns undefined!
      }
    });
}

Shouldn't both values be the same?

Body:
It appears that you are not returning the Promise from your function - there is no return statement in the getToken function at all, so the function itself just returns undefined. The internal return statement you have will resolve the promise, which is good, but you have to handle that resolution.

If you return the promise like so:

getToken(user_id) {
    return this.afs.collection("tokens").ref.doc(user_id).get().then((doc) => {
      if (doc.exists) {
        console.log(doc.data().user_token); //displays the correct user_token value
        return doc.data().user_token;
      }
    });
}

You should be able to access the user_token asynchronously when the promise resolves by doing the following:

getToken(user_id).then(user_token => {
  handle_user_token_here(user_token);
});

Note: The function as modified will return a promise. Therefore, you cannot simply the following:

let user_token = getToken(user_id);
// user_token here is a Promise object, not the token!
handle_user_token_here(user_token); // will not work.

You can do this though:

let user_token = getToken(user_id);
// user_token here is a Promise object, not the token!
user_token.then(user_token => handle_user_token_here(user_token)); // will work


问:
Pandas wide_to_long - keep rows with values in i or just one row with blank
I am trying to transform a dataframe into long format, and want to keep the records  following this rule:

(1) if the id has data in the stub - keep all the non-empty "long" records, drop the empty ones
(2) if the id does not have data in any of the stubs, keep only 1 "long" record of it

In other words, for each id I need to keep at least one record:
-exactly one record if the id does not have data in the stub,
-or as many rows as the id has filled in the stub

I am using wide_to_long because it allows the use of stubnames, which melt does not (or at least I could not figure out how to use). This is the sample code (inspired by the official pandas doc on wide_to_long):

```<code>import numpy as np
import pandas as pd

np.random.seed(123)
df = pd.DataFrame({"A2001" : {0 : "a", 1 : "b", 2 : "c",3:""},
                    "A2002" : {0 : "d", 1 : "e", 2 : "",3:""},
                    "A2003" : {0 : "g", 1 : "h", 2 : "i",3:""},
                    "A2004" : {0 : "j", 1 : "k", 2 : "l",3:""},
                   })

df["id"] = df.index
pd.wide_to_long(df, ["A"], i="id", j="year")
</code>```

The output is:

```<code>         A
id year
0  2001  a
1  2001  b
2  2001  c
3  2001
0  2002  d
1  2002  e
2  2002
3  2002
0  2003  g
1  2003  h
2  2003  i
3  2003
0  2004  j
1  2004  k
2  2004  l
3  2004
</code>```

What I need to achieve:
for ids 0, 1 and 2 I need the rows gone where ["A"] is blank (or NaN);
for id 3 - I need to keep 1 row (any one row, does not matter which one):

```<code>         A
id year
0  2001  a
1  2001  b
2  2001  c
3  2001
0  2002  d
1  2002  e
0  2003  g
1  2003  h
2  2003  i
0  2004  j
1  2004  k
2  2004  l
</code>```

I tried this:

```<code>df.dropna(axis=0,how="all",subset=["A"])
</code>```

but it deletes all rows for id 3

Edit:

I am also looking for a general solution for this as well, with a list of stubnames:

```<code>df = pd.DataFrame({"A2001" : {0 : "a", 1 : "b", 2 : "",3:""},
                    "A2002" : {0 : "d", 1 : "e", 2 : "test",3:""},
                    "A2003" : {0 : "g", 1 : "h", 2 : "i",3:""},
                    "A2004" : {0 : "j", 1 : "k", 2 : "l",3:""},
                    "B2001" : {0 : "a", 1 : "b", 2 : "",3:""},
                    "B2002" : {0 : "d", 1 : "e", 2 : "",3:""},
                    "B2003" : {0 : "g", 1 : "h", 2 : "i",3:""},
                    "B2004" : {0 : "j", 1 : "k", 2 : "l",3:""},
                   })

pd.wide_to_long(df, ["A","B"], i="id", j="year")
</code>```

I need to delete rows where both A and B columns are empty, but still keep at least 1 record for each id (and exactly one record if A and B are both blanks)

答:
Similar to WeNYoBen

```<code>s = pd.wide_to_long(df, ["A"], i="id", j="year")

def f(d):
    m = d.A.eq('')
    return d.head(1) if m.all() else d[~m]

t = pd.concat([f(d) for _, d in s.groupby('id')])

t.loc[[*filter(t.index.__contains__, s.index)]]

         A
id year
0  2001  a
1  2001  b
2  2001  c
3  2001
0  2002  d
1  2002  e
0  2003  g
1  2003  h
2  2003  i
0  2004  j
1  2004  k
2  2004  l
</code>```

Body:
I am trying to transform a dataframe into long format, and want to keep the records  following this rule:

(1) if the id has data in the stub - keep all the non-empty "long" records, drop the empty ones
(2) if the id does not have data in any of the stubs, keep only 1 "long" record of it

In other words, for each id I need to keep at least one record:
-exactly one record if the id does not have data in the stub,
-or as many rows as the id has filled in the stub

I am using wide_to_long because it allows the use of stubnames, which melt does not (or at least I could not figure out how to use). This is the sample code (inspired by the official pandas doc on wide_to_long):

import numpy as np
import pandas as pd

np.random.seed(123)
df = pd.DataFrame({"A2001" : {0 : "a", 1 : "b", 2 : "c",3:""},
                    "A2002" : {0 : "d", 1 : "e", 2 : "",3:""},
                    "A2003" : {0 : "g", 1 : "h", 2 : "i",3:""},
                    "A2004" : {0 : "j", 1 : "k", 2 : "l",3:""},
                   })

df["id"] = df.index
pd.wide_to_long(df, ["A"], i="id", j="year")

The output is:

         A
id year
0  2001  a
1  2001  b
2  2001  c
3  2001
0  2002  d
1  2002  e
2  2002
3  2002
0  2003  g
1  2003  h
2  2003  i
3  2003
0  2004  j
1  2004  k
2  2004  l
3  2004

What I need to achieve:
for ids 0, 1 and 2 I need the rows gone where ["A"] is blank (or NaN);
for id 3 - I need to keep 1 row (any one row, does not matter which one):

         A
id year
0  2001  a
1  2001  b
2  2001  c
3  2001
0  2002  d
1  2002  e
0  2003  g
1  2003  h
2  2003  i
0  2004  j
1  2004  k
2  2004  l

I tried this:

df.dropna(axis=0,how="all",subset=["A"])

but it deletes all rows for id 3

Edit:

I am also looking for a general solution for this as well, with a list of stubnames:

df = pd.DataFrame({"A2001" : {0 : "a", 1 : "b", 2 : "",3:""},
                    "A2002" : {0 : "d", 1 : "e", 2 : "test",3:""},
                    "A2003" : {0 : "g", 1 : "h", 2 : "i",3:""},
                    "A2004" : {0 : "j", 1 : "k", 2 : "l",3:""},
                    "B2001" : {0 : "a", 1 : "b", 2 : "",3:""},
                    "B2002" : {0 : "d", 1 : "e", 2 : "",3:""},
                    "B2003" : {0 : "g", 1 : "h", 2 : "i",3:""},
                    "B2004" : {0 : "j", 1 : "k", 2 : "l",3:""},
                   })

pd.wide_to_long(df, ["A","B"], i="id", j="year")

I need to delete rows where both A and B columns are empty, but still keep at least 1 record for each id (and exactly one record if A and B are both blanks)

Body:
Similar to WeNYoBen

s = pd.wide_to_long(df, ["A"], i="id", j="year")

def f(d):
    m = d.A.eq('')
    return d.head(1) if m.all() else d[~m]

t = pd.concat([f(d) for _, d in s.groupby('id')])

t.loc[[*filter(t.index.__contains__, s.index)]]

         A
id year
0  2001  a
1  2001  b
2  2001  c
3  2001
0  2002  d
1  2002  e
0  2003  g
1  2003  h
2  2003  i
0  2004  j
1  2004  k
2  2004  l


问:
Toggle visibility for React Native elements?
In React Native, is there a way to toggle visibility of an element?

For example:

```<code><Text visibility={this.state.isVisible}>Visibility depends on state</Text>
</code>```

答:
There are various ways to do this. See the Conditional Rendering guide at the React.js website, which explains different options.

One simple way is to use the && operator inline:

{this.state.isVisible &&
  <Text>Something</Text>
}
```
<hr />
<p>Note that with the conditional rendering approach, if you have (for example) some views <em>centered vertically</em>, this views will <em>suddenly</em> move up or down when the text appears (to give room for the new view). Another approach that avoids this jump is to either change the text color or set the text to empty string:</p>
<pre class="lang-js prettyprint-override"><code>// Change the text color
<Text style={this.state.isVisible ? {color: 'black'} : {color: 'white'}}>Something</Text>

// Set text to empty string
<Text>{this.state.isVisible ? "Something" : ""}</Text>
</code>```

Changing the color of the text is the best way to make sure that the other views don't suddenly move when showing the text, given that the text is already occupying the layout space it needs.

Body:
In React Native, is there a way to toggle visibility of an element?

For example:

<Text visibility={this.state.isVisible}>Visibility depends on state</Text>

Body:
There are various ways to do this. See the Conditional Rendering guide at the React.js website, which explains different options.

One simple way is to use the && operator inline:

{this.state.isVisible &&
  <Text>Something</Text>
}

Note that with the conditional rendering approach, if you have (for example) some views centered vertically, this views will suddenly move up or down when the text appears (to give room for the new view). Another approach that avoids this jump is to either change the text color or set the text to empty string:

// Change the text color
<Text style={this.state.isVisible ? {color: 'black'} : {color: 'white'}}>Something</Text>

// Set text to empty string
<Text>{this.state.isVisible ? "Something" : ""}</Text>

Changing the color of the text is the best way to make sure that the other views don't suddenly move when showing the text, given that the text is already occupying the layout space it needs.


问:
adb reverse failing with Operation Not Permitted error
I am trying to run a reverse tcp link to a newly installed android emulator on linux via adb (trying to debug a failing call from a react_native build.):

```<code>adb -s emulator-5556 reverse --no-rebind tcp:0 tcp:8081
</code>```

with adb version:

```<code>Android Debug Bridge version 1.0.41
Version 28.0.3-5475833
</code>```

Curiously the same command as a forward link works fine.

I am getting the result:

```<code>adb: error: cannot bind listener: Operation not permitted
</code>```

Now it looks like the emulator is what's throwing the error (adb seems to just receive this error message as a read from a process that it's communicating with, after running it via strace, and via ADB_TRACE=all).

It seems to fail after issuing a write to the adbd process with:

```<code>write(3, "0027reverse:forward:norebind:tcp:0;tcp:8081", 43) = 43
</code>```

Now this is obviously a command protocol that's being sent to the emulator. (I couldn't find command number 27 with a light poking around the adb source. Any pointers?)

To try and figure out exactly what's going wrong I tried running the emulator via strace. (is it trying to write to a file that causes the permissions error? is it caused when trying to open the socket?)

Here are the ADBTRACE=all logs (ADB_TRACE=all adb reverse --no-rebind tcp:0 tcp:8081):

```<code>adb D 05-29 00:16:29 16167 16167 adb_trace.cpp:192] Android Debug Bridge version 1.0.41
adb D 05-29 00:16:29 16167 16167 adb_trace.cpp:192] Version 28.0.3-5475833
adb D 05-29 00:16:29 16167 16167 adb_trace.cpp:192] Installed as /home/something/Android/Sdk/platform-tools/adb
adb D 05-29 00:16:29 16167 16167 adb_trace.cpp:192]
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:327] adb_connect: service: reverse:forward:norebind:tcp:0;tcp:8081
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:153] _adb_connect: host:version
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:107] writex: fd=3 len=16 30303063686f73743a76657273696f6e 000chost:version
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 4f4b4159 OKAY
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:187] _adb_connect: return fd 3
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 30303034 0004
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 30303239 0029
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:153] _adb_connect: reverse:forward:norebind:tcp:0;tcp:8081
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:107] writex: fd=3 len=18 30303065686f73743a74706f72743a61 000ehost:tport:a [truncated]
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:113] Switch transport in progress: host:tport:any
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 4f4b4159 OKAY
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=8
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=8 got=8 0d00000000000000 ........
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:127] Switch transport success
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:107] writex: fd=3 len=43 30303237726576657273653a666f7277 0027reverse:forw [truncated]
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 4f4b4159 OKAY
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:187] _adb_connect: return fd 3
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:345] adb_connect: return fd 3
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 4641494c FAIL
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 30303264 002d
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=45
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=45 got=45 63616e6e6f742062696e64206c697374 cannot bind list [truncated]
adb: error: cannot bind listener: Operation not permitted
</code>```

When running the emulator with:

```<code>strace -s 10000 -f emulator -avd Pixel_2_API_Q 2>&1 | grep -C100 reverse
</code>```

I get:

```<code>[pid 26748] recvfrom(79, "reverse:forward:norebind:tcp:0;tcp:8081\0", 40, 0, NULL, NULL) = 40
[pid 26748] ioctl(54, KVM_RUN, 0)       = 0
[pid 26748] recvfrom(79, 0x7f7b49edec50, 24, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
[pid 26748] ioctl(54, KVM_RUN, 0)       = 0
[pid 26748] write(31, "\1\0\0\0\0\0\0\0", 8) = 8
[pid 26729] <... ppoll resumed>)        = 1 ([{fd=31, revents=POLLIN}], left {tv_sec=0, tv_nsec=9712500})
[pid 26748] write(28, "\1\0\0\0\0\0\0\0", 8 <unfinished ...>
[pid 26729] futex(0x2f80ce8, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
[pid 26748] <... write resumed>)        = 8
[pid 26748] futex(0x2f80ce8, FUTEX_WAKE_PRIVATE, 1) = 1
[pid 26729] <... futex resumed>)        = 0
[pid 26748] ioctl(54, KVM_RUN <unfinished ...>
[pid 26729] read(31, "\1\0\0\0\0\0\0\0", 16) = 8
[pid 26729] futex(0x2f80ce8, FUTEX_WAKE_PRIVATE, 1) = 0
[pid 26748] <... ioctl resumed>, 0)     = 0
[pid 26729] ppoll([{fd=31, events=POLLIN}, {fd=30, events=POLLIN}, {fd=26, events=POLLIN}, {fd=28, events=POLLIN}, {fd=52, events=POLLIN}, {fd=64, events=POLLIN}, {fd=65, events=POLLIN}, {fd=66, events=POLLIN}, {fd=67, events=POLLIN}, {fd=69, events=POLLIN}, {fd=71, events=POLLIN}, {fd=73, events=POLLIN}, {fd=75, events=POLLIN}, {fd=77, events=POLLIN}, {fd=50, events=POLLIN}, {fd=51, events=POLLIN}, {fd=34, events=POLLIN}, {fd=79, events=POLLIN}], 18, {tv_sec=0, tv_nsec=9404000}, NULL, 8 <unfinished ...>
[pid 26748] ioctl(54, KVM_RUN <unfinished ...>
[pid 26729] <... ppoll resumed>)        = 1 ([{fd=28, revents=POLLIN}], left {tv_sec=0, tv_nsec=9399241})
[pid 26748] <... ioctl resumed>, 0)     = 0
[pid 26729] read(31,  <unfinished ...>
[pid 26748] futex(0x2f80ce8, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
[pid 26729] <... read resumed>0x7f7bb75925a0, 16) = -1 EAGAIN (Resource temporarily unavailable)
[pid 26729] read(28, "\1\0\0\0\0\0\0\0", 512) = 8
[pid 26729] futex(0x2f80ce8, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
[pid 26748] <... futex resumed>)        = 0
[pid 26729] <... futex resumed>)        = 1
[pid 26729] ppoll([{fd=31, events=POLLIN}, {fd=30, events=POLLIN}, {fd=26, events=POLLIN}, {fd=28, events=POLLIN}, {fd=52, events=POLLIN}, {fd=64, events=POLLIN}, {fd=65, events=POLLIN}, {fd=66, events=POLLIN}, {fd=67, events=POLLIN}, {fd=69, events=POLLIN}, {fd=71, events=POLLIN}, {fd=73, events=POLLIN}, {fd=75, events=POLLIN}, {fd=77, events=POLLIN}, {fd=50, events=POLLIN}, {fd=51, events=POLLIN}, {fd=34, events=POLLIN}, {fd=79, events=POLLIN}], 18, {tv_sec=0, tv_nsec=9246000}, NULL, 8 <unfinished ...>
[pid 26748] futex(0x2f80ce8, FUTEX_WAKE_PRIVATE, 1) = 0
[pid 26748] ioctl(54, KVM_RUN, 0)       = 0
[pid 26748] ioctl(54, KVM_RUN <unfinished ...>
[pid 26747] <... ioctl resumed>, 0)     = 0
[pid 26747] ioctl(53, KVM_RUN <unfinished ...>
[pid 26748] <... ioctl resumed>, 0)     = 0
[pid 26748] ioctl(54, KVM_RUN, 0)       = 0
[pid 26747] <... ioctl resumed>, 0)     = 0
[pid 26748] ioctl(54, KVM_RUN <unfinished ...>
[pid 26747] sendto(79, "OKAY\256\2\0\0X\1\0\0\0\0\0\0\0\0\0\0\260\264\276\246", 24, MSG_NOSIGNAL, NULL, 0 <unfinished ...>
[pid 26748] <... ioctl resumed>, 0)     = 0
[pid 26747] <... sendto resumed>)       = 24
[pid 26747] ioctl(53, KVM_RUN <unfinished ...>
[pid 26748] ioctl(54, KVM_RUN <unfinished ...>
[pid 26747] <... ioctl resumed>, 0)     = 0
[pid 26748] <... ioctl resumed>, 0)     = 0
[pid 26747] sendto(79, "WRTE\256\2\0\0X\1\0\0005\0\0\0\0\0\0\0\250\255\253\272", 24, MSG_NOSIGNAL, NULL, 0 <unfinished ...>
[pid 26748] futex(0x2f80ce8, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
[pid 26747] <... sendto resumed>)       = 24
[pid 26747] futex(0x2f80ce8, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
[pid 26748] <... futex resumed>)        = 0
[pid 26747] <... futex resumed>)        = 1
[pid 26748] futex(0x2f80ce8, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
[pid 26747] ioctl(53, KVM_RUN <unfinished ...>
[pid 26748] <... futex resumed>)        = 0
[pid 26748] ioctl(54, KVM_RUN <unfinished ...>
[pid 26747] <... ioctl resumed>, 0)     = 0
[pid 26748] <... ioctl resumed>, 0)     = 0
[pid 26747] sendto(79, "FAIL002dcannot bind listener: Operation not permitted", 53, MSG_NOSIGNAL, NULL, 0 <unfinished ...>
</code>```

Now this doesn't make much sense unless it is an error inside the kvm_run stuff (I'm not super familiar with kvm.)

So I'm assuming this is some simple permissions problem, but my messing around doesn't allow me to figure out where the permissions problem is.

Is anyone able to help shed light on how to deal with this error for adb? are there any strace/kvm wizards who can point out how I can find out where this permissions error is?

答:
Try to restart the adbd daemon with root permissions: </p>

```<code>adb kill-server; adb root
</code>```

Body:
I am trying to run a reverse tcp link to a newly installed android emulator on linux via adb (trying to debug a failing call from a react_native build.):

adb -s emulator-5556 reverse --no-rebind tcp:0 tcp:8081

with adb version:

Android Debug Bridge version 1.0.41
Version 28.0.3-5475833

Curiously the same command as a forward link works fine.

I am getting the result:

adb: error: cannot bind listener: Operation not permitted

Now it looks like the emulator is what's throwing the error (adb seems to just receive this error message as a read from a process that it's communicating with, after running it via strace, and via ADB_TRACE=all).

It seems to fail after issuing a write to the adbd process with:

write(3, "0027reverse:forward:norebind:tcp:0;tcp:8081", 43) = 43

Now this is obviously a command protocol that's being sent to the emulator. (I couldn't find command number 27 with a light poking around the adb source. Any pointers?)

To try and figure out exactly what's going wrong I tried running the emulator via strace. (is it trying to write to a file that causes the permissions error? is it caused when trying to open the socket?)

Here are the ADBTRACE=all logs (ADB_TRACE=all adb reverse --no-rebind tcp:0 tcp:8081):

adb D 05-29 00:16:29 16167 16167 adb_trace.cpp:192] Android Debug Bridge version 1.0.41
adb D 05-29 00:16:29 16167 16167 adb_trace.cpp:192] Version 28.0.3-5475833
adb D 05-29 00:16:29 16167 16167 adb_trace.cpp:192] Installed as /home/something/Android/Sdk/platform-tools/adb
adb D 05-29 00:16:29 16167 16167 adb_trace.cpp:192]
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:327] adb_connect: service: reverse:forward:norebind:tcp:0;tcp:8081
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:153] _adb_connect: host:version
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:107] writex: fd=3 len=16 30303063686f73743a76657273696f6e 000chost:version
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 4f4b4159 OKAY
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:187] _adb_connect: return fd 3
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 30303034 0004
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 30303239 0029
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:153] _adb_connect: reverse:forward:norebind:tcp:0;tcp:8081
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:107] writex: fd=3 len=18 30303065686f73743a74706f72743a61 000ehost:tport:a [truncated]
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:113] Switch transport in progress: host:tport:any
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 4f4b4159 OKAY
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=8
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=8 got=8 0d00000000000000 ........
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:127] Switch transport success
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:107] writex: fd=3 len=43 30303237726576657273653a666f7277 0027reverse:forw [truncated]
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 4f4b4159 OKAY
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:187] _adb_connect: return fd 3
adb D 05-29 00:16:29 16167 16167 adb_client.cpp:345] adb_connect: return fd 3
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 4641494c FAIL
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=4
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=4 got=4 30303264 002d
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:81] readx: fd=3 wanted=45
adb D 05-29 00:16:29 16167 16167 adb_io.cpp:97] readx: fd=3 wanted=45 got=45 63616e6e6f742062696e64206c697374 cannot bind list [truncated]
adb: error: cannot bind listener: Operation not permitted

When running the emulator with:

strace -s 10000 -f emulator -avd Pixel_2_API_Q 2>&1 | grep -C100 reverse

I get:

[pid 26748] recvfrom(79, "reverse:forward:norebind:tcp:0;tcp:8081\0", 40, 0, NULL, NULL) = 40
[pid 26748] ioctl(54, KVM_RUN, 0)       = 0
[pid 26748] recvfrom(79, 0x7f7b49edec50, 24, 0, NULL, NULL) = -1 EAGAIN (Resource temporarily unavailable)
[pid 26748] ioctl(54, KVM_RUN, 0)       = 0
[pid 26748] write(31, "\1\0\0\0\0\0\0\0", 8) = 8
[pid 26729] <... ppoll resumed>)        = 1 ([{fd=31, revents=POLLIN}], left {tv_sec=0, tv_nsec=9712500})
[pid 26748] write(28, "\1\0\0\0\0\0\0\0", 8 <unfinished ...>
[pid 26729] futex(0x2f80ce8, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
[pid 26748] <... write resumed>)        = 8
[pid 26748] futex(0x2f80ce8, FUTEX_WAKE_PRIVATE, 1) = 1
[pid 26729] <... futex resumed>)        = 0
[pid 26748] ioctl(54, KVM_RUN <unfinished ...>
[pid 26729] read(31, "\1\0\0\0\0\0\0\0", 16) = 8
[pid 26729] futex(0x2f80ce8, FUTEX_WAKE_PRIVATE, 1) = 0
[pid 26748] <... ioctl resumed>, 0)     = 0
[pid 26729] ppoll([{fd=31, events=POLLIN}, {fd=30, events=POLLIN}, {fd=26, events=POLLIN}, {fd=28, events=POLLIN}, {fd=52, events=POLLIN}, {fd=64, events=POLLIN}, {fd=65, events=POLLIN}, {fd=66, events=POLLIN}, {fd=67, events=POLLIN}, {fd=69, events=POLLIN}, {fd=71, events=POLLIN}, {fd=73, events=POLLIN}, {fd=75, events=POLLIN}, {fd=77, events=POLLIN}, {fd=50, events=POLLIN}, {fd=51, events=POLLIN}, {fd=34, events=POLLIN}, {fd=79, events=POLLIN}], 18, {tv_sec=0, tv_nsec=9404000}, NULL, 8 <unfinished ...>
[pid 26748] ioctl(54, KVM_RUN <unfinished ...>
[pid 26729] <... ppoll resumed>)        = 1 ([{fd=28, revents=POLLIN}], left {tv_sec=0, tv_nsec=9399241})
[pid 26748] <... ioctl resumed>, 0)     = 0
[pid 26729] read(31,  <unfinished ...>
[pid 26748] futex(0x2f80ce8, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
[pid 26729] <... read resumed>0x7f7bb75925a0, 16) = -1 EAGAIN (Resource temporarily unavailable)
[pid 26729] read(28, "\1\0\0\0\0\0\0\0", 512) = 8
[pid 26729] futex(0x2f80ce8, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
[pid 26748] <... futex resumed>)        = 0
[pid 26729] <... futex resumed>)        = 1
[pid 26729] ppoll([{fd=31, events=POLLIN}, {fd=30, events=POLLIN}, {fd=26, events=POLLIN}, {fd=28, events=POLLIN}, {fd=52, events=POLLIN}, {fd=64, events=POLLIN}, {fd=65, events=POLLIN}, {fd=66, events=POLLIN}, {fd=67, events=POLLIN}, {fd=69, events=POLLIN}, {fd=71, events=POLLIN}, {fd=73, events=POLLIN}, {fd=75, events=POLLIN}, {fd=77, events=POLLIN}, {fd=50, events=POLLIN}, {fd=51, events=POLLIN}, {fd=34, events=POLLIN}, {fd=79, events=POLLIN}], 18, {tv_sec=0, tv_nsec=9246000}, NULL, 8 <unfinished ...>
[pid 26748] futex(0x2f80ce8, FUTEX_WAKE_PRIVATE, 1) = 0
[pid 26748] ioctl(54, KVM_RUN, 0)       = 0
[pid 26748] ioctl(54, KVM_RUN <unfinished ...>
[pid 26747] <... ioctl resumed>, 0)     = 0
[pid 26747] ioctl(53, KVM_RUN <unfinished ...>
[pid 26748] <... ioctl resumed>, 0)     = 0
[pid 26748] ioctl(54, KVM_RUN, 0)       = 0
[pid 26747] <... ioctl resumed>, 0)     = 0
[pid 26748] ioctl(54, KVM_RUN <unfinished ...>
[pid 26747] sendto(79, "OKAY\256\2\0\0X\1\0\0\0\0\0\0\0\0\0\0\260\264\276\246", 24, MSG_NOSIGNAL, NULL, 0 <unfinished ...>
[pid 26748] <... ioctl resumed>, 0)     = 0
[pid 26747] <... sendto resumed>)       = 24
[pid 26747] ioctl(53, KVM_RUN <unfinished ...>
[pid 26748] ioctl(54, KVM_RUN <unfinished ...>
[pid 26747] <... ioctl resumed>, 0)     = 0
[pid 26748] <... ioctl resumed>, 0)     = 0
[pid 26747] sendto(79, "WRTE\256\2\0\0X\1\0\0005\0\0\0\0\0\0\0\250\255\253\272", 24, MSG_NOSIGNAL, NULL, 0 <unfinished ...>
[pid 26748] futex(0x2f80ce8, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
[pid 26747] <... sendto resumed>)       = 24
[pid 26747] futex(0x2f80ce8, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
[pid 26748] <... futex resumed>)        = 0
[pid 26747] <... futex resumed>)        = 1
[pid 26748] futex(0x2f80ce8, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
[pid 26747] ioctl(53, KVM_RUN <unfinished ...>
[pid 26748] <... futex resumed>)        = 0
[pid 26748] ioctl(54, KVM_RUN <unfinished ...>
[pid 26747] <... ioctl resumed>, 0)     = 0
[pid 26748] <... ioctl resumed>, 0)     = 0
[pid 26747] sendto(79, "FAIL002dcannot bind listener: Operation not permitted", 53, MSG_NOSIGNAL, NULL, 0 <unfinished ...>

Now this doesn't make much sense unless it is an error inside the kvm_run stuff (I'm not super familiar with kvm.)

So I'm assuming this is some simple permissions problem, but my messing around doesn't allow me to figure out where the permissions problem is.

Is anyone able to help shed light on how to deal with this error for adb? are there any strace/kvm wizards who can point out how I can find out where this permissions error is?

Body:
Try to restart the adbd daemon with root permissions:

adb kill-server; adb root


问:
How to get a resultset from a dynamic select-based sql query in Pentaho-Kettle?
I want to execute a set of select-based sql queries, derived from  xml-node elements within a XML file, and write the values of the corresponding resultsets in a CSV file. I'd like to clarify that no field of the sql query is parameterized, but the full sql query itself is.

The part of getting the full sql query is done as expected, but I don't know how to proceed with the part of launching the sql query so that I can get the corresponding resultset to manage it later.

What I've tried until now with no success is the following:

Execute SQL script step: according to what it's said here, this sort of step doesn't get any resultset.

Dynamic SQL Row step: I get the corresponding sql as long as I hardcode the field 'TemplateSQL', but I can't do that because the sql queries derived from the previous step are very different among them and don't provide a fixed meta-data scheme.

Table Input step: this step only allows single values of a sql query to be parameterized, not the full query.

I'd really appreciate if some pentaho-kettle expert could tell me the right way of accomplishing the task I want to do.

Update (following @Cristian Curti instructions):
The problem arise in KTR-1. When you say "you have to pass this query as a variable to another KTR", I understand that what I have to do first is setting the xml-node value in a variable and then pass it to the KTR-2. To do so, in my KTR-1 I'm using these steps:

Get Data from XML: I'm getting 'SQLQuery' field.

Set Variables: I'm setting the field 'SQLQuery' in a variable named 'QUERY'.
(Moreover, KTR-1 is set as you said, that is, in the Parameters tab I set a parameter named 'QUERY').

When I preview the second step of KTR-1, the Kettle console logs the following error:

```<code>Get data from XML.0 - Finished processing (I=2, O=0, R=0, W=2, U=0, E=0)
Set variables.0 - Setting environment variables...
Set variables.0 - Set variable QUERY to value [
  SELECT "Table1".*, "table2"."field1" FROM "Table1" INNER JOIN "Table2" ON Table1"."PATIENT" = "Table2"."PATIENT" WHERE ("Table1"."field1" > Table1"."field2") AND NOT("field1" BETWEEN 'date1' AND 'date2')
]
Set variables.0 - ERROR (version 8.2.0.0-342, build 8.2.0.0-342 from 2018-1-14 10.30.55 by buildguy) : Unexpected error
Set variables.0 - ERROR (version 8.2.0.0-342, build 8.2.0.0-342 from 2018-1-14 10.30.55 by buildguy) : org.pentaho.di.core.exception.KettleStepException:
Set variables.0 - Only 1 input row was expected to set the variables and at least 2 were received.
</code>```

It seems Set Variables step only expects to receive one single row.
I don't know if I'm doing something wrong or missing something. In case you need further details, just tell me. Thanks.

答:
From the link you shared, follow Rishu's Answer, Part 2.

In the part where YOU are getting the Query from the XML nodes, you have to pass this query as a variable to another KTR, and in the Input Table step of this second KTR, use ${variable} in the SQL Editor of the step.

Should look something like this.

Update Answer

No, you used the wrong approach. You use the Set Variables step when you know only 1 row will reach the step, when you have multiple rows reaching your "Result set", you need to use the Copy rows > Execute for every row approach. This requires 1 Job and 2 KTR's to be executed.

Your Job will simply look like this:

The Last step in your first KTR will be 'Copy Rows to Result'.

In the Job, you need to configure your second KTR entry as such:

The Stream column name must match the same column name in the first KTR that contains the queries to be executed, also, the named parameter in the second KTR (First answer picture) MUST match the parameter in the JOB configuration as the pictures show.

After that all you have to do is generate a dynamic filename for each query on the second KTR, usually all output steps that generate Files have a 'Include date in filename' and a 'Include time in filename', using both should be enough to generate different filenames with zero effort, and you get a timestamp of when the file was created.

Body:
I want to execute a set of select-based sql queries, derived from  xml-node elements within a XML file, and write the values of the corresponding resultsets in a CSV file. I'd like to clarify that no field of the sql query is parameterized, but the full sql query itself is.

The part of getting the full sql query is done as expected, but I don't know how to proceed with the part of launching the sql query so that I can get the corresponding resultset to manage it later.

What I've tried until now with no success is the following:

Execute SQL script step: according to what it's said here, this sort of step doesn't get any resultset.

Dynamic SQL Row step: I get the corresponding sql as long as I hardcode the field 'TemplateSQL', but I can't do that because the sql queries derived from the previous step are very different among them and don't provide a fixed meta-data scheme.

Table Input step: this step only allows single values of a sql query to be parameterized, not the full query.

I'd really appreciate if some pentaho-kettle expert could tell me the right way of accomplishing the task I want to do.

Update (following @Cristian Curti instructions):
The problem arise in KTR-1. When you say "you have to pass this query as a variable to another KTR", I understand that what I have to do first is setting the xml-node value in a variable and then pass it to the KTR-2. To do so, in my KTR-1 I'm using these steps:

Get Data from XML: I'm getting 'SQLQuery' field.

Set Variables: I'm setting the field 'SQLQuery' in a variable named 'QUERY'.
(Moreover, KTR-1 is set as you said, that is, in the Parameters tab I set a parameter named 'QUERY').

When I preview the second step of KTR-1, the Kettle console logs the following error:

Get data from XML.0 - Finished processing (I=2, O=0, R=0, W=2, U=0, E=0)
Set variables.0 - Setting environment variables...
Set variables.0 - Set variable QUERY to value [
  SELECT "Table1".*, "table2"."field1" FROM "Table1" INNER JOIN "Table2" ON Table1"."PATIENT" = "Table2"."PATIENT" WHERE ("Table1"."field1" > Table1"."field2") AND NOT("field1" BETWEEN 'date1' AND 'date2')
]
Set variables.0 - ERROR (version 8.2.0.0-342, build 8.2.0.0-342 from 2018-1-14 10.30.55 by buildguy) : Unexpected error
Set variables.0 - ERROR (version 8.2.0.0-342, build 8.2.0.0-342 from 2018-1-14 10.30.55 by buildguy) : org.pentaho.di.core.exception.KettleStepException:
Set variables.0 - Only 1 input row was expected to set the variables and at least 2 were received.

It seems Set Variables step only expects to receive one single row.
I don't know if I'm doing something wrong or missing something. In case you need further details, just tell me. Thanks.


问:
Django project hierarchy/organization
I am new to Django and starting a project, and I would like to do it the right way.

I would like to know what you think is best practice for organizing a project.

Here are some questions I have:

How do I separate the static resources from the Python code so that I don't waste time processing the static content through Django?

As apps are reusable modules, they are not really tight to a project, so should they be located in the project directory, or in another directory that would contain all my "homemade" apps?

Are templates considered to be static or dynamic content?

Here is my current file hierarchy:

```<code>webapps/
    myproject/
        apache/
        bin/
        lib/
        templates/
            app1/
            app2/
        src/
            app1/
            app2/
            __init.py
            settings.py
            urls.py
            manage.py
        myproject.wsgi
    admin/
    static/
        css/
        img/
</code>```

What do you think?
What would be better?

Thanks!

答:
My designer doesn't want to go hunting everywhere (all over the python path) for template files. My template layout follows yours in that there is one template folder and all the apps go below that.  Each app has its own base.html that extends basebase.html.

Here lately I've started following the pinax model of an apps folder, and all apps go in there.  The reason for this is purely aesthetic since Wing shows me a tree, all my apps are clustered together in that part of the tree.  What I didn't like was an app that sorted out alphabetically after templates or media or site_media.  Scrolling up and down the tree slowed me down.  By putting all the apps into one place in the tree, git commit -m "feature notes" apps checkins in the code changes too which is another plus.

```<code>webapps/
  myvirtenv/
    bin/
    lib/
    myproject/        <- Source control starts here
      site_media/
        collected_static/
          js/
          css/
          img/
        uploaded_media/
      deploy/
          myproject.wsgi
          procmail scripts
          apache site files  # linked from /etc/apache2/sites-endabled
      apps/
        app1/
          templates/       <- This should be here, but in practice I just leave in templates below
            app1/
        app2/
      templates/ # lib template overrides and site wide templates
        basebase.html      <- I changed the name to help my designer
        app1/
        app2/
      settings.py
      gethostname()_local_settings.py # keep machine specific settings here
      urls.py
      manage.py
      requirements
        base.txt
        project.txt
</code>```

There were too many base.html files and talking about it was difficult, so basebase.html was born and we've been happy with that ever since.

I haven't had any static files that didn't work with the staticfiles app.  I was using that app with 1.2.  I haven't gone back and done the 1.3 static folders yet but I probably will in the next few months as things get worked on.

I got the requirements folder trick from pinax.

Create VirtualENV

git clone url

pip install -r requirements\base.txt -r requirements\project.txt

manage syncdb  (if using sqlite, otherwise you have to do db creation tasks first)

symlink from /etc/apache2

reload apache config

Profit!

Title: Django project hierarchy/organization

Body:
I am new to Django and starting a project, and I would like to do it the right way.

I would like to know what you think is best practice for organizing a project.

Here are some questions I have:

How do I separate the static resources from the Python code so that I don't waste time processing the static content through Django?

As apps are reusable modules, they are not really tight to a project, so should they be located in the project directory, or in another directory that would contain all my "homemade" apps?

Are templates considered to be static or dynamic content?

Here is my current file hierarchy:

webapps/
    myproject/
        apache/
        bin/
        lib/
        templates/
            app1/
            app2/
        src/
            app1/
            app2/
            __init.py
            settings.py
            urls.py
            manage.py
        myproject.wsgi
    admin/
    static/
        css/
        img/

What do you think?
What would be better?

Thanks!

Body:
My designer doesn't want to go hunting everywhere (all over the python path) for template files. My template layout follows yours in that there is one template folder and all the apps go below that.  Each app has its own base.html that extends basebase.html.

Here lately I've started following the pinax model of an apps folder, and all apps go in there.  The reason for this is purely aesthetic since Wing shows me a tree, all my apps are clustered together in that part of the tree.  What I didn't like was an app that sorted out alphabetically after templates or media or site_media.  Scrolling up and down the tree slowed me down.  By putting all the apps into one place in the tree, git commit -m "feature notes" apps checkins in the code changes too which is another plus.

webapps/
  myvirtenv/
    bin/
    lib/
    myproject/        <- Source control starts here
      site_media/
        collected_static/
          js/
          css/
          img/
        uploaded_media/
      deploy/
          myproject.wsgi
          procmail scripts
          apache site files  # linked from /etc/apache2/sites-endabled
      apps/
        app1/
          templates/       <- This should be here, but in practice I just leave in templates below
            app1/
        app2/
      templates/ # lib template overrides and site wide templates
        basebase.html      <- I changed the name to help my designer
        app1/
        app2/
      settings.py
      gethostname()_local_settings.py # keep machine specific settings here
      urls.py
      manage.py
      requirements
        base.txt
        project.txt

There were too many base.html files and talking about it was difficult, so basebase.html was born and we've been happy with that ever since.

I haven't had any static files that didn't work with the staticfiles app.  I was using that app with 1.2.  I haven't gone back and done the 1.3 static folders yet but I probably will in the next few months as things get worked on.

I got the requirements folder trick from pinax.

Create VirtualENV

git clone url

pip install -r requirements\base.txt -r requirements\project.txt

manage syncdb  (if using sqlite, otherwise you have to do db creation tasks first)

symlink from /etc/apache2

reload apache config

Profit!


问:
ImportError: cannot import name 'Pregel' from 'graphframes.lib'
I am using pyspark and graphframes from jupyter.  I am able to successfully import pyspark and graphframes, but when I try:

```<code>from graphframes.lib import Pregel
</code>```

I get the following error:

```<code>ImportError: cannot import name 'Pregel' from 'graphframes.lib'
</code>```

This post is how I was able to get graphframes to work, but without graphframes.lib:

https://github.com/graphframes/graphframes/issues/104

```<code>wget https://github.com/graphframes/graphframes/archive/release-0.2.0.zip
unzip release-0.2.0.zip
cd graphframes-release-0.2.0
build/sbt assembly
cd ..

# Copy necessary files to root level so we can start pyspark.
cp graphframes-release-0.2.0/target/scala-2.11/graphframes-release-0-2-0-assembly-0.2.0-spark2.0.jar .
cp -r graphframes-release-0.2.0/python/graphframes .

# Set environment to use Jupyter
export PYSPARK_DRIVER_PYTHON=jupyter
export PYSPARK_DRIVER_PYTHON_OPTS=notebook

# Launch the jupyter server.
pyspark --jars graphframes-release-0-2-0-assembly-0.2.0-spark2.0.jar
</code>```

I tried to repeat the above commands, without the environment lines as pyspark works just fine for me in jupyter, using a different version and was able to get graphframes.lib, but no Pregel:

```<code>wget https://github.com/graphframes/graphframes/archive/release-0.6.0.zip
unzip release-0.6.0.zip
cd graphframes-release-0.6.0
build/sbt assembly
cd ..

# Copy necessary files to root level so we can start pyspark.
cp graphframes-release-0.6.0/target/scala-2.11/graphframes-assembly-0.6.0-spark2.3.jar .
cp -r graphframes-release-0.6.0/python/graphframes .

# Set environment to use Jupyter
export PYSPARK_DRIVER_PYTHON=jupyter
export PYSPARK_DRIVER_PYTHON_OPTS=notebook

# Launch the jupyter server.
pyspark --jars graphframes-assembly-0.6.0-spark2.3.jar
</code>```

Now I can see the graphrames.lib directory but all that's in there is aggregate_messages.py.

Finally, I tried the following but get a 404 error:

```<code>wget https://github.com/graphframes/graphframes/archive/release-0.7.0.zip
</code>```

I expected that, because I was able to import graphframes, that I'd be able to import Pregel from graphframes.lib.  It would seem that in my version, 0.6.0 now, there is a graphrames.lib but no Pregel and that there is no 0.7.0 release yet for graphframes.

答:
I was able to resolve this error using the following method:</p>

```<code>wget https://github.com/graphframes/graphframes/archive/f9e13ab4ac1a7113f8439744a1ab45710eb50a72.zip
unzip graphframes-f9e13ab4ac1a7113f8439744a1ab45710eb50a72.zip
cd graphframes-f9e13ab4ac1a7113f8439744a1ab45710eb50a72
build/sbt assembly
cd ..

# Copy necessary files to root level so we can start pyspark.
cp graphframes-f9e13ab4ac1a7113f8439744a1ab45710eb50a72/target/scala-2.11/graphframes-assembly-0.7.0-spark2.4.jar .
cp -r graphframes-f9e13ab4ac1a7113f8439744a1ab45710eb50a72/python/graphframes .

# Set environment to use Jupyter (if jupyter working with pyspark, skip)
# export PYSPARK_DRIVER_PYTHON=jupyter
# export PYSPARK_DRIVER_PYTHON_OPTS=notebook

# launch pyspark
pyspark --jars graphframes-assembly-0.7.0-spark2.4.jar
</code>```

Body:
I am using pyspark and graphframes from jupyter.  I am able to successfully import pyspark and graphframes, but when I try:

from graphframes.lib import Pregel

I get the following error:

ImportError: cannot import name 'Pregel' from 'graphframes.lib'

This post is how I was able to get graphframes to work, but without graphframes.lib:

https://github.com/graphframes/graphframes/issues/104

wget https://github.com/graphframes/graphframes/archive/release-0.2.0.zip
unzip release-0.2.0.zip
cd graphframes-release-0.2.0
build/sbt assembly
cd ..

# Copy necessary files to root level so we can start pyspark.
cp graphframes-release-0.2.0/target/scala-2.11/graphframes-release-0-2-0-assembly-0.2.0-spark2.0.jar .
cp -r graphframes-release-0.2.0/python/graphframes .

# Set environment to use Jupyter
export PYSPARK_DRIVER_PYTHON=jupyter
export PYSPARK_DRIVER_PYTHON_OPTS=notebook

# Launch the jupyter server.
pyspark --jars graphframes-release-0-2-0-assembly-0.2.0-spark2.0.jar

I tried to repeat the above commands, without the environment lines as pyspark works just fine for me in jupyter, using a different version and was able to get graphframes.lib, but no Pregel:

wget https://github.com/graphframes/graphframes/archive/release-0.6.0.zip
unzip release-0.6.0.zip
cd graphframes-release-0.6.0
build/sbt assembly
cd ..

# Copy necessary files to root level so we can start pyspark.
cp graphframes-release-0.6.0/target/scala-2.11/graphframes-assembly-0.6.0-spark2.3.jar .
cp -r graphframes-release-0.6.0/python/graphframes .

# Set environment to use Jupyter
export PYSPARK_DRIVER_PYTHON=jupyter
export PYSPARK_DRIVER_PYTHON_OPTS=notebook

# Launch the jupyter server.
pyspark --jars graphframes-assembly-0.6.0-spark2.3.jar

Now I can see the graphrames.lib directory but all that's in there is aggregate_messages.py.

Finally, I tried the following but get a 404 error:

wget https://github.com/graphframes/graphframes/archive/release-0.7.0.zip

I expected that, because I was able to import graphframes, that I'd be able to import Pregel from graphframes.lib.  It would seem that in my version, 0.6.0 now, there is a graphrames.lib but no Pregel and that there is no 0.7.0 release yet for graphframes.

Body:
I was able to resolve this error using the following method:

wget https://github.com/graphframes/graphframes/archive/f9e13ab4ac1a7113f8439744a1ab45710eb50a72.zip
unzip graphframes-f9e13ab4ac1a7113f8439744a1ab45710eb50a72.zip
cd graphframes-f9e13ab4ac1a7113f8439744a1ab45710eb50a72
build/sbt assembly
cd ..

# Copy necessary files to root level so we can start pyspark.
cp graphframes-f9e13ab4ac1a7113f8439744a1ab45710eb50a72/target/scala-2.11/graphframes-assembly-0.7.0-spark2.4.jar .
cp -r graphframes-f9e13ab4ac1a7113f8439744a1ab45710eb50a72/python/graphframes .

# Set environment to use Jupyter (if jupyter working with pyspark, skip)
# export PYSPARK_DRIVER_PYTHON=jupyter
# export PYSPARK_DRIVER_PYTHON_OPTS=notebook

# launch pyspark
pyspark --jars graphframes-assembly-0.7.0-spark2.4.jar


问:
How to create a lambda permission for a custom websocket request authorizer with CloudFormation for API Gateway?
I've been trying to create a lambda permission for a websockets request authorizer for API Gateway and the $connect route. The AWS documenation mentions nothing about creating the proper lambda permission for a websocket authorizer. I keep getting a 500 error when I try to connect to my custom authorizer.

Since the AWS documenation (https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-lambda-auth.html) does not mention anything about this permission, I assume one would have to create a lambda permission just like a TOKEN authorizer.

The custom authorizer was created with my CloudFormation script as follows:

```<code># ***************************************************************
# API Gateway Websocket Authorizer
# ***************************************************************
WebsocketAuthorizer:
    Type: 'AWS::ApiGatewayV2::Authorizer'
    DependsOn: Lambda
    Properties:
        Name: WebsocketAuthorizer
        ApiId:
            Fn::ImportValue:
                !Sub ${Env}-${AWS::Region}-altosignal-global-websockets
        AuthorizerType: REQUEST
        AuthorizerCredentialsArn:
            Fn::ImportValue:
                !Sub ${Env}-${AWS::Region}-global-iamprocesscommandsfromapigateway-arn
        AuthorizerUri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Lambda.Arn}/invocations
        IdentitySource:
            - route.request.querystring.token
</code>```

When I create an authorizer manually using the console, I get this prompt at the end:

This manually created authorizer works once I hook it up with the $connect endpoint.

So, it is this permission I'm trying to set in my CloudFormation script. I have tried the following settings but it does not work. I keep getting a 500 error:

```<code>   LambdaPermission:
        Type: AWS::Lambda::Permission
        DependsOn:
            - Lambda
            - WebsocketAuthorizer
        Properties:
            Action: lambda:*
            FunctionName: !GetAtt Lambda.Arn
            Principal: apigateway.amazonaws.com
            SourceArn: !Sub
                - arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${apiId}/${Env}/$connect
                - apiId:
                    Fn::ImportValue:
                        !Sub ${Env}-${AWS::Region}-altosignal-global-websockets
</code>```

Does anybody know the proper lambda permission settings for a custom authorizer for a websocket API Gateway $connect endpoint?

答:
You definitely need a Lambda Permission. This is what I have for setting the permission and it works well. The only difference I can see here is that the SourceArn is not given. This will hopefully give you a place to start.</p>

```<code>Permission:
    Type: "AWS::Lambda::Permission"
    DependsOn:
    - "WebsocketApi"
    Properties:
      Action: "lambda:InvokeFunction"
      FunctionName: !GetAtt Lambda.Arn
      Principal:
        Fn::Join:
        - ""
        - - "apigateway."
          - Ref: "AWS::URLSuffix"
</code>```

Body:
I've been trying to create a lambda permission for a websockets request authorizer for API Gateway and the $connect route. The AWS documenation mentions nothing about creating the proper lambda permission for a websocket authorizer. I keep getting a 500 error when I try to connect to my custom authorizer.

Since the AWS documenation (https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-lambda-auth.html) does not mention anything about this permission, I assume one would have to create a lambda permission just like a TOKEN authorizer.

The custom authorizer was created with my CloudFormation script as follows:

# ***************************************************************
# API Gateway Websocket Authorizer
# ***************************************************************
WebsocketAuthorizer:
    Type: 'AWS::ApiGatewayV2::Authorizer'
    DependsOn: Lambda
    Properties:
        Name: WebsocketAuthorizer
        ApiId:
            Fn::ImportValue:
                !Sub ${Env}-${AWS::Region}-altosignal-global-websockets
        AuthorizerType: REQUEST
        AuthorizerCredentialsArn:
            Fn::ImportValue:
                !Sub ${Env}-${AWS::Region}-global-iamprocesscommandsfromapigateway-arn
        AuthorizerUri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Lambda.Arn}/invocations
        IdentitySource:
            - route.request.querystring.token

When I create an authorizer manually using the console, I get this prompt at the end:

This manually created authorizer works once I hook it up with the $connect endpoint.

So, it is this permission I'm trying to set in my CloudFormation script. I have tried the following settings but it does not work. I keep getting a 500 error:

   LambdaPermission:
        Type: AWS::Lambda::Permission
        DependsOn:
            - Lambda
            - WebsocketAuthorizer
        Properties:
            Action: lambda:*
            FunctionName: !GetAtt Lambda.Arn
            Principal: apigateway.amazonaws.com
            SourceArn: !Sub
                - arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${apiId}/${Env}/$connect
                - apiId:
                    Fn::ImportValue:
                        !Sub ${Env}-${AWS::Region}-altosignal-global-websockets

Does anybody know the proper lambda permission settings for a custom authorizer for a websocket API Gateway $connect endpoint?

Body:
You definitely need a Lambda Permission. This is what I have for setting the permission and it works well. The only difference I can see here is that the SourceArn is not given. This will hopefully give you a place to start.

Permission:
    Type: "AWS::Lambda::Permission"
    DependsOn:
    - "WebsocketApi"
    Properties:
      Action: "lambda:InvokeFunction"
      FunctionName: !GetAtt Lambda.Arn
      Principal:
        Fn::Join:
        - ""
        - - "apigateway."
          - Ref: "AWS::URLSuffix"


问:
Weather app: button doesn't show any data
I'm trying to create a simple Weather app using openweathermap.org using ajax and I can't seem to return my data when I press the button I created. The console doesn't show me any errors and I can't figure out where I made a mistake or what I am missing.

Here is my code and what I have tried: https://jsfiddle.net/AdelinaLipsa/kyzmao9n/

I want the button next to my input to return the selected city temperature(for the moment). Where did I make a mistake? Thank you for your help.

答:
JSFiddle currently blocks insecure requests over HTTP

change your jquery import to https

change your api request $.ajax call to https

One code issue:

fix a typo on your $.ajax call succes to success, the callback was never being called as it was incorrectly named

example fiddle with weather now populated: https://jsfiddle.net/3vdx98go/1/


问:
Expand operator executing more times than expected
I am trying to build some code that paginates result a result set using the expand operator and does so until a certain number of resources has been fetched. This is what I have so far (removed the actual async call logic):

import { Observable } from 'rxjs';

const items = [
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    [21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
];

const call = () => {
    console.log('#call');

    const batch = items.shift();

    if (batch) {
        return Observable.of(batch).delay(100);
    }

    return Observable.empty();
}

const o$ = call().expand((values) => {
    console.log('expansion');
    return call();
}).flatMap((val: any) => val);

const log = (prefix: string) => (...args: any[]) => console.log(prefix, ...args);

o$.take(9).subscribe(log('next'), log('error'), log('complete'));
```

<p>The output is different than what I would want:</p>

```#call
takeWhile.ts:26 next 1
takeWhile.ts:26 next 2
takeWhile.ts:26 next 3
takeWhile.ts:26 next 4
takeWhile.ts:26 next 5
takeWhile.ts:26 next 6
takeWhile.ts:26 next 7
takeWhile.ts:26 next 8
takeWhile.ts:26 next 9
takeWhile.ts:26 complete
takeWhile.ts:22 expansion
takeWhile.ts:10 #call
```

I get my 9 items, which is what I requested using take, then the stream completes, but one additional expansion happens and the mock async API gets called once again.

Is there any way to make my code be less greedy and not start another recursive iteration?

答:
The behavior you're seeing is expected: expand is called before take, so

we get the first value

expand is executed and the call is made

then the whole observable is immediately unsubscribed from

Therefore you need to make the recursion call asynchronous to let the take unsubscribe before that call is made. The simplest fix seem to be to substitute return call(); with a return timer(4).map(call); (or even a return defer(call);, that magically works :/ ).

See this example in action

So generally, you need to make a "pause" / "gap" to let the limiter kick in and unsubscribe.

Hope this helps

Body:
I am trying to build some code that paginates result a result set using the expand operator and does so until a certain number of resources has been fetched. This is what I have so far (removed the actual async call logic):

import { Observable } from 'rxjs';

const items = [
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    [21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
];

const call = () => {
    console.log('#call');

    const batch = items.shift();

    if (batch) {
        return Observable.of(batch).delay(100);
    }

    return Observable.empty();
}

const o$ = call().expand((values) => {
    console.log('expansion');
    return call();
}).flatMap((val: any) => val);

const log = (prefix: string) => (...args: any[]) => console.log(prefix, ...args);

o$.take(9).subscribe(log('next'), log('error'), log('complete'));

The output is different than what I would want:

#call
takeWhile.ts:26 next 1
takeWhile.ts:26 next 2
takeWhile.ts:26 next 3
takeWhile.ts:26 next 4
takeWhile.ts:26 next 5
takeWhile.ts:26 next 6
takeWhile.ts:26 next 7
takeWhile.ts:26 next 8
takeWhile.ts:26 next 9
takeWhile.ts:26 complete
takeWhile.ts:22 expansion
takeWhile.ts:10 #call

I get my 9 items, which is what I requested using take, then the stream completes, but one additional expansion happens and the mock async API gets called once again.

Is there any way to make my code be less greedy and not start another recursive iteration?


问:
how to determine maximum route cost in a n high numeric pyramid
I've got a numeric pyramid like this

```<code>       7
      4 8
     1 8 9
    2 4 6 7
   4 6 7 4 9
  4 9 7 3 8 8

routes: 32
</code>```

every number indexed by how powerful in its line.

```<code> 0 ( 9 => 1 ) 1 ( 8 => 5 ) 2 ( 8 => 4 ) 3 ( 7 => 2 ) 4 ( 4 => 0 ) 5 ( 3 => 3 )
 0 ( 9 => 4 ) 1 ( 7 => 2 ) 2 ( 6 => 1 ) 3 ( 4 => 3 ) 4 ( 4 => 0 )
 0 ( 7 => 3 ) 1 ( 6 => 2 ) 2 ( 4 => 1 ) 3 ( 2 => 0 )
 0 ( 9 => 2 ) 1 ( 8 => 1 ) 2 ( 1 => 0 )
 0 ( 8 => 1 ) 1 ( 4 => 0 )
 0 ( 7 => 0 )
</code>```

in this pyramid there are 2^(n-1) routes (you can go 2 way form each number)
If the pyramid high this low, it's easy you can calculate all the routes, and compare to eachother. But if you have an 50 high pyramid with 562949953421312 routes, the problem little bit more difficult.

I thoght I start from the bottom beginning from the most powerful numbers, but soon i realized, the max route cost ain't necesserly start or end up in big numbers.

Then I thought maybe secoundary indexes (where can you go futher from a number) will help, but I didin't even implement that becouse I assumed its still uses much resources and not optimal.

And now i'm confused how to restart thinking about this problem... any advice appreciated

答:
I suggest you look at Dijkstra's Algorithm and A*.

I believe Dijkstra's is more accurate than A*, but slower.

Body:
I've got a numeric pyramid like this

       7
      4 8
     1 8 9
    2 4 6 7
   4 6 7 4 9
  4 9 7 3 8 8

routes: 32

every number indexed by how powerful in its line.

 0 ( 9 => 1 ) 1 ( 8 => 5 ) 2 ( 8 => 4 ) 3 ( 7 => 2 ) 4 ( 4 => 0 ) 5 ( 3 => 3 )
 0 ( 9 => 4 ) 1 ( 7 => 2 ) 2 ( 6 => 1 ) 3 ( 4 => 3 ) 4 ( 4 => 0 )
 0 ( 7 => 3 ) 1 ( 6 => 2 ) 2 ( 4 => 1 ) 3 ( 2 => 0 )
 0 ( 9 => 2 ) 1 ( 8 => 1 ) 2 ( 1 => 0 )
 0 ( 8 => 1 ) 1 ( 4 => 0 )
 0 ( 7 => 0 )

in this pyramid there are 2^(n-1) routes (you can go 2 way form each number)
If the pyramid high this low, it's easy you can calculate all the routes, and compare to eachother. But if you have an 50 high pyramid with 562949953421312 routes, the problem little bit more difficult.

I thoght I start from the bottom beginning from the most powerful numbers, but soon i realized, the max route cost ain't necesserly start or end up in big numbers.

Then I thought maybe secoundary indexes (where can you go futher from a number) will help, but I didin't even implement that becouse I assumed its still uses much resources and not optimal.

And now i'm confused how to restart thinking about this problem... any advice appreciated


问:
PostgreSQL function with table and column names as parameters,and would return min and max of that column (any type) as an array?
I am trying to make a function in PostgreSQL, which would take a name of a table and a name of one column in that table (in varchars); and return an array which would have minimum and maximum values of that column.

In advance I do not (want to) know the type of the column, ie. type of the elements in the return array.

I've tried to create a function with polymorphic anyarray as a return type, but keep hitting syntax errors. Have tried both in SQL and in PLPQSQL.

```<code>CREATE OR REPLACE FUNCTION minmax(tablename character varying, columnname character varying, OUT minmaxa anyarray) AS
$function$
BEGIN
   EXECUTE FORMAT('SELECT array_agg(foo) AS %s FROM (
            (SELECT min(%s) AS foo FROM %s)
            UNION
            (SELECT max(%s) AS foo FROM %s)
    ) AS foobar'
       , columnname
       , columnname, tablename
       , columnname, tablename)
   INTO minmaxa;
END
$function$
LANGUAGE plpgsql;
</code>```

The string from the FORMAT would be:

```<code>SELECT array_agg(foo) AS columnname FROM (
  (SELECT min(columnname) AS foo FROM tablename)
  UNION
  (SELECT max(columnname) AS foo FROM tablename)
 ) AS foobar
</code>```

Then test case:

```<code>create table example (columnA int, columnB varchar);
insert into example (columnA, columnB) values (1, 'ac'), (2, 'ab'), (3, 'aa');

select minmax('example', 'columnA');
select minmax('example', 'columnB');

select array_agg(columnA) from (
   (select min(columnA) AS columnA from example)
   UNION
   (select max(columnA) AS columnA from example)
  ) AS foobar;
</code>```

Should return:

{1,3}

  {'aa','ac'}

  {1,3}

But currently function definition gives:
"SQL Error [42P13]: ERROR: cannot determine result data type
  Detail: A function returning a polymorphic type must have at least one polymorphic argument."

I can make it work, by defining a dummy function parameter 'foo':

```<code>CREATE OR REPLACE FUNCTION minmax(tablename character varying, columnname character varying, foo anyelement, OUT minmaxa anyarray)
</code>```

(Note. A function parameter foo is not used in function body anywhere.)

And now giving a dummy typed value of the correct type of returning array when calling the function:

```<code>select minmax('example', 'columnB', ''::varchar);
select minmax('example', 'columnA', 0::int);
</code>```

Then it works, but what should be changed to I would not need that dummy function parameter?

答:
I am afraid it is not possible to easily get rid of that attribute you call foo. As I understand from the documentation the value passed as foo parameter is required to tell Postgres what the return data type would be.

38.2.5. Polymorphic Types

Five pseudo-types of special interest are anyelement, anyarray, anynonarray, anyenum, and anyrange, which are collectively called polymorphic types. Any function declared using these types is said to be a polymorphic function. A polymorphic function can operate on many different data types, with the specific data type(s) being determined by the data types actually passed to it in a particular call.

Polymorphic arguments and results are tied to each other and are resolved to a specific data type when a query calling a polymorphic function is parsed. Each position (either argument or return value) declared as anyelement is allowed to have any specific actual data type, but in any given call they must all be the same actual type.

So how I understand this is that with polymorphic function removing attribute foo is not possible and your function seems to be such since it uses pseudo-type anyarray.

Body:
I am trying to make a function in PostgreSQL, which would take a name of a table and a name of one column in that table (in varchars); and return an array which would have minimum and maximum values of that column.

In advance I do not (want to) know the type of the column, ie. type of the elements in the return array.

I've tried to create a function with polymorphic anyarray as a return type, but keep hitting syntax errors. Have tried both in SQL and in PLPQSQL.

CREATE OR REPLACE FUNCTION minmax(tablename character varying, columnname character varying, OUT minmaxa anyarray) AS
$function$
BEGIN
   EXECUTE FORMAT('SELECT array_agg(foo) AS %s FROM (
            (SELECT min(%s) AS foo FROM %s)
            UNION
            (SELECT max(%s) AS foo FROM %s)
    ) AS foobar'
       , columnname
       , columnname, tablename
       , columnname, tablename)
   INTO minmaxa;
END
$function$
LANGUAGE plpgsql;

The string from the FORMAT would be:

SELECT array_agg(foo) AS columnname FROM (
  (SELECT min(columnname) AS foo FROM tablename)
  UNION
  (SELECT max(columnname) AS foo FROM tablename)
 ) AS foobar

Then test case:

create table example (columnA int, columnB varchar);
insert into example (columnA, columnB) values (1, 'ac'), (2, 'ab'), (3, 'aa');

select minmax('example', 'columnA');
select minmax('example', 'columnB');

select array_agg(columnA) from (
   (select min(columnA) AS columnA from example)
   UNION
   (select max(columnA) AS columnA from example)
  ) AS foobar;

Should return:

{1,3}

  {'aa','ac'}

  {1,3}

But currently function definition gives:
"SQL Error [42P13]: ERROR: cannot determine result data type
  Detail: A function returning a polymorphic type must have at least one polymorphic argument."

I can make it work, by defining a dummy function parameter 'foo':

CREATE OR REPLACE FUNCTION minmax(tablename character varying, columnname character varying, foo anyelement, OUT minmaxa anyarray)

(Note. A function parameter foo is not used in function body anywhere.)

And now giving a dummy typed value of the correct type of returning array when calling the function:

select minmax('example', 'columnB', ''::varchar);
select minmax('example', 'columnA', 0::int);

Then it works, but what should be changed to I would not need that dummy function parameter?


问:
Use custom `Manager` for "default values" creation and fetching
I have and Employee model and an EmployeeType model, with Employee having an attribute called Employee.employee_type which is of type EmployeeType.

Currently on the creation of a client, we run something to create "default" values for the EmployeeType.  Right now, that logic is in a method within the module that handles a new client being created... but I was thinking a better place for this would be to be with the EmployeeType model.

My question is - would it be appropriate to make a custom Manager attribute on the EmployeeType that does the creation and/or fetching of these default types? See the following for what I believe I am trying to accomplish:

    class DefaultValueEmployeeTypeManager(models.Manager):
        def get_or_create_default_values(self):
            first_type = self.model.objects.get_or_create(name='First Type')
            second_type = self.model.objects.get_or_create(name='Second Type')
            third_type = self.model.objects.get_or_create(name='Third Type')

            return (first_type, second_type, third_type)

    class Employee(models.Model):
        first_name = models.CharField(max_length=255)
        last_name = models.CharField(max_length=255)

        defaults = DefaultValueEmployeeTypeManager()

    # Code in another file, handling the setup of a new client
    from models import EmployeeType

    def create_new_client(client):
        # make sure the default values are there
        EmployeeType.defaults.get_or_create_default_values()
```

My question is whether or not this is acceptable/expected behavior for a Manager object to handle? Or should this just be some sort of @classmethod (or similar) on the EmployeeType model?

答:
You can do that, but I wouldn't rename the manager to defaults because now objects is undefined and all queries on EmployeeType need to use defaults, e.g. EmployeeType.defaults.all() which doesn't really make sense.

You just want to add an extra method, like described here.

Just name your manager objects = EmployeeTypeManager(), call your manager EmployeeTypeManager (it's still the default manager) and your method is just an extra manager method.

Note that get_or_create returns a two-tuple of the object and whether or not it was created. So in your code first_type is (<the object>, False) if the type already exists.

Note also you put this under the Employee model, but it's of course an EmployeeType model manager.

Body:
I have and Employee model and an EmployeeType model, with Employee having an attribute called Employee.employee_type which is of type EmployeeType.

Currently on the creation of a client, we run something to create "default" values for the EmployeeType.  Right now, that logic is in a method within the module that handles a new client being created... but I was thinking a better place for this would be to be with the EmployeeType model.

My question is - would it be appropriate to make a custom Manager attribute on the EmployeeType that does the creation and/or fetching of these default types? See the following for what I believe I am trying to accomplish:

    class DefaultValueEmployeeTypeManager(models.Manager):
        def get_or_create_default_values(self):
            first_type = self.model.objects.get_or_create(name='First Type')
            second_type = self.model.objects.get_or_create(name='Second Type')
            third_type = self.model.objects.get_or_create(name='Third Type')

            return (first_type, second_type, third_type)

    class Employee(models.Model):
        first_name = models.CharField(max_length=255)
        last_name = models.CharField(max_length=255)

        defaults = DefaultValueEmployeeTypeManager()

    # Code in another file, handling the setup of a new client
    from models import EmployeeType

    def create_new_client(client):
        # make sure the default values are there
        EmployeeType.defaults.get_or_create_default_values()

My question is whether or not this is acceptable/expected behavior for a Manager object to handle? Or should this just be some sort of @classmethod (or similar) on the EmployeeType model?


问:
Google ML Training task can't find module
I am trying to run my ml Python script using gcloud ml, but keep getting the error:

```<code>/usr/bin/python: No module named trainer
</code>```

The command I am using is:

```<code>gcloud ai-platform jobs submit training "job6" --job-dir \
"gs://comp10001cards/root" --package-path "C:\\Users\hjstu\desktop" \
 --module-name trainer.task --region "us-central1"
</code>```

"C:\\Users\hjstu\desktop" contains a __init__.py file.
"gs://comp10001cards/root" contains the folder trainer which contains an __init__.py file and my script task.py.

Where is /usr/bin/python? What am I doing wrong? If it helps, I am using the gcloud tool from cmd within C:\\Users\hjstu\desktop.

答:
Your trainer module and task.py script should sit locally on your computer when you run the jobs submit training command. The command will package your code, send it to the cloud, and execute it.

Have a look at the relevant documentation page for more information.

Body:
I am trying to run my ml Python script using gcloud ml, but keep getting the error:

/usr/bin/python: No module named trainer

The command I am using is:

gcloud ai-platform jobs submit training "job6" --job-dir \
"gs://comp10001cards/root" --package-path "C:\\Users\hjstu\desktop" \
 --module-name trainer.task --region "us-central1"

"C:\\Users\hjstu\desktop" contains a __init__.py file.
"gs://comp10001cards/root" contains the folder trainer which contains an __init__.py file and my script task.py.

Where is /usr/bin/python? What am I doing wrong? If it helps, I am using the gcloud tool from cmd within C:\\Users\hjstu\desktop.


问:
While Loop Won't Run With Nested Elif Statement?
I am still newish to programming and I'm just developing simple programs to automate routine office work.

I have the most barebones code written but whenever I try to add a while loop it breaks the code and causes the program to close after I enter a value for Y. The print text after entering the Y value never appears. I have my code listed below.

Everything works fine until I try to throw it into a while loop, then it stops working. Is there something I am missing here?

Removing the while loop and having everything work again

```<code>import os
import subprocess
import time
import pyautogui

x=15
y=10
while x != 333:
    print("test")
    print("15 - 10 is",x-y)
    print("Okay now tell me what X should be")
    x = input("Enter a numerical value for X: ")
    print("Excellent! Now let's do Y")
    y = input("Enter a numerical value for Y: ")
    print("Okay now let's compare X and Y")
    #don't forget to indent!
    if x > y:
        print("X is greater than Y!")
    elif y > x:
        print("Y is greater than X!")
    else:
        print("X is equal to Y!")
    print(x, y)
time.sleep(2)
input("Press Enter to continue...")
</code>```

答: input returns a string, so when the loop comes back around and tries to print x-y, it crashes because you can't substract strings.

Body:
I am still newish to programming and I'm just developing simple programs to automate routine office work.

I have the most barebones code written but whenever I try to add a while loop it breaks the code and causes the program to close after I enter a value for Y. The print text after entering the Y value never appears. I have my code listed below.

Everything works fine until I try to throw it into a while loop, then it stops working. Is there something I am missing here?

Removing the while loop and having everything work again

import os
import subprocess
import time
import pyautogui

x=15
y=10
while x != 333:
    print("test")
    print("15 - 10 is",x-y)
    print("Okay now tell me what X should be")
    x = input("Enter a numerical value for X: ")
    print("Excellent! Now let's do Y")
    y = input("Enter a numerical value for Y: ")
    print("Okay now let's compare X and Y")
    #don't forget to indent!
    if x > y:
        print("X is greater than Y!")
    elif y > x:
        print("Y is greater than X!")
    else:
        print("X is equal to Y!")
    print(x, y)
time.sleep(2)
input("Press Enter to continue...")


问:
(SSR) How to get Fragment Types for IntrospectionFragmentMatcher on the server?
Is it possible to get the fragmentTypes directly from a schema?

The docs for IntrospectionFragmentMatcher show how to fetch and store these in a JSON file - but it seems to only consider client-side use and assumes that you have a running server.

Since I'm using the same server as my GraphQL API as well as for rendering the app, it's a chicken-and-egg thing. I could still do this as the docs say with an extra build step, but that would essentially mean:

Build from src

Start up server

Run script to extract fragment types, write these to a .json file

Restart the server

I feel like there must be a simpler way to do this.

When rendering the app, I'm creating a new Apollo client with a SchemaLink, so I already have a fully prepared schema.

Right now I have this:

import introspectionQueryResultData from './fragmentTypes.json'

const fragmentMatcher = new IntrospectionFragmentMatcher({
    introspectionQueryResultData,
})

const render = ({ schema, context }) => async (req, res, next) => {
    const client = new ApolloClient({
        ssrMode: true,
        link: new SchemaLink({ schema, context }),
        cache: new InMemoryCache({
            fragmentMatcher,
        }),
    })
    // ...
```

<p>I'd like to be able to something like this instead:</p>

<pre class="lang-js prettyprint-override"><code>const render = ({ schema, context }) => async (req, res, next) => {
    const client = new ApolloClient({
        ssrMode: true,
        link: new SchemaLink({ schema, context }),
        cache: new InMemoryCache({
            fragmentMatcher: fragmentMatcherFromSchema(schema)
        }),
    })
    // ...
</code>```

Is this possible?

答:
It is most likely possible, but I haven't tried doing it fully in runtime yet.
However, you can avoid having to have to run start the server to extract the fragments.

If you are using GraphQL-Code-Generator (https://graphql-code-generator.com/docs/getting-started/).

You can specify various input for the schema other than a url where the server is running see:
https://graphql-code-generator.com/docs/getting-started/schema-field#available-formats

You can at least reduce the steps to

Build from src

Run script to extract fragment types, write these to a .json file

Start up server

Body:
Is it possible to get the fragmentTypes directly from a schema?

The docs for IntrospectionFragmentMatcher show how to fetch and store these in a JSON file - but it seems to only consider client-side use and assumes that you have a running server.

Since I'm using the same server as my GraphQL API as well as for rendering the app, it's a chicken-and-egg thing. I could still do this as the docs say with an extra build step, but that would essentially mean:

Build from src

Start up server

Run script to extract fragment types, write these to a .json file

Restart the server

I feel like there must be a simpler way to do this.

When rendering the app, I'm creating a new Apollo client with a SchemaLink, so I already have a fully prepared schema.

Right now I have this:

import introspectionQueryResultData from './fragmentTypes.json'

const fragmentMatcher = new IntrospectionFragmentMatcher({
    introspectionQueryResultData,
})

const render = ({ schema, context }) => async (req, res, next) => {
    const client = new ApolloClient({
        ssrMode: true,
        link: new SchemaLink({ schema, context }),
        cache: new InMemoryCache({
            fragmentMatcher,
        }),
    })
    // ...

I'd like to be able to something like this instead:

const render = ({ schema, context }) => async (req, res, next) => {
    const client = new ApolloClient({
        ssrMode: true,
        link: new SchemaLink({ schema, context }),
        cache: new InMemoryCache({
            fragmentMatcher: fragmentMatcherFromSchema(schema)
        }),
    })
    // ...

Is this possible?


问:
Add timestamp to existing table
I have a SQL Server table with just 3 columns, one of which is of type varbinary. The data in this column is actually a Json document which among other properties contains information about when the data was last modified. Unfortunately the SQL table itself does not contain information about when its rows were modified.

Now when doing sorting and filtering of the data I of course don't want fetch all rows in order to find e.g. the latest 100 entries.

So my question is: does SQL Server somehow remember when a row was added/modified? I have tried adding a timestamp and this is applied to all existing rows but this is applied randomly I think, because the sorting doesn't work. I don't need a datetime or anything, I just want to be able sort the records based on when they were last modified.

Thanks

答:
For those looking to insert a tamestamp column of type DateTime into an existing DB table, you can do this like so:

```<code>ALTER TABLE TestTable
ADD DateInserted DATETIME NOT NULL DEFAULT (GETDATE());
</code>```

The existing records will automatically get the value equal to the date/time of the moment when column is added.
New records will get up-to-date value upon insertion.

Title: Add timestamp to existing table

Body:
For those looking to insert a tamestamp column of type DateTime into an existing DB table, you can do this like so:

ALTER TABLE TestTable
ADD DateInserted DATETIME NOT NULL DEFAULT (GETDATE());

The existing records will automatically get the value equal to the date/time of the moment when column is added.
New records will get up-to-date value upon insertion.


问:
Rails - How to do If and else statements in a text field
I am trying to do a if and else statement in this text field with the value:

```<code><%= f.text_field :rating, :size => 1, :min => 1, :max => 6, :value => if vind.ratings == 0 else vind.rating_score/vind.ratings, :type => 'range', :step => '1', :id => "#{vind.id}" %>
</code>```

I get this error in view:

```<code>SyntaxError in Kategoris#show

Showing C:/Rails/konkurranceportalen/app/views/kategoris/_rating.html.erb where line #3 raised:

C:/Rails/konkurranceportalen/app/views/kategoris/_rating.html.erb:3: syntax error, unexpected keyword_else, expecting keyword_then or ';' or '\n'
...ue => if vind.ratings == 0 else vind.rating_score/vind.ratin...
...                               ^
C:/Rails/konkurranceportalen/app/views/kategoris/_rating.html.erb:3: syntax error, unexpected ',', expecting ')'
...vind.rating_score/vind.ratings, :type => 'range', :step => '...
...                               ^
</code>```

答:
I usually use the shorthand, like this:</p>

```<code>:value => ( vind.ratings == 0 ? 0 : vind.rating_score/vind.ratings )
</code>```

Body:
I am trying to do a if and else statement in this text field with the value:

<%= f.text_field :rating, :size => 1, :min => 1, :max => 6, :value => if vind.ratings == 0 else vind.rating_score/vind.ratings, :type => 'range', :step => '1', :id => "#{vind.id}" %>

I get this error in view:

SyntaxError in Kategoris#show

Showing C:/Rails/konkurranceportalen/app/views/kategoris/_rating.html.erb where line #3 raised:

C:/Rails/konkurranceportalen/app/views/kategoris/_rating.html.erb:3: syntax error, unexpected keyword_else, expecting keyword_then or ';' or '\n'
...ue => if vind.ratings == 0 else vind.rating_score/vind.ratin...
...                               ^
C:/Rails/konkurranceportalen/app/views/kategoris/_rating.html.erb:3: syntax error, unexpected ',', expecting ')'
...vind.rating_score/vind.ratings, :type => 'range', :step => '...
...                               ^

Body:
I usually use the shorthand, like this:

:value => ( vind.ratings == 0 ? 0 : vind.rating_score/vind.ratings )


问:
How to 'convert' amount of Hz to appropriate byte/bit format?
I am programming an IR camera. But the internal register for storing the refreshrate (bits 9, 8 and 7 of 16 in total) only accepts 1's and 0's of course. For example, a value of 0b110 means 32Hz and 0b111, 64Hz. How can I best convert a given integer (refresh_rate) value between 0.5Hz and 64Hz to 0b000 - 0b111 when looking at the Refresh rate control picture?

To be honest, I haven't tried anything yet because I think it requires a weird hack. However I have been thinking of a lookuptable, I think that is possible but requires more memory and it has to be memory efficient since it is programmed on an Arduino Due.

Note: You can see that the refresh_rate is an uint16_t which does not accept floating point values such as 0.5Hz, however, I am probably not going to use the 0.5Hz setting.

// the end-user currently has to pass a value between 7 and 0
void mlx90650::set_refresh_rate(uint16_t refresh_rate) {
    // So the refresh rate may not be higher than 7 (0b111) since this register only
accepts 3 bits. The memory map is:
/*
    | decimal | bits  |
    (decimal 0) 0 0 0 IR refresh rate = 0.5Hz
    (decimal 1) 0 0 1 IR refresh rate = 1Hz
    (decimal 2) 0 1 0 IR refresh rate = 2Hz (default)
    (decimal 3) 0 1 1 IR refresh rate = 4Hz
    (decimal 4) 1 0 0 IR refresh rate = 8Hz
    (decimal 5) 1 0 1 IR refresh rate = 16Hz
    (decimal 6) 1 1 0 IR refresh rate = 32Hz
    (decimal 7) 1 1 1 IR refresh rate = 64H
*/
}
```

I want that this function converts for example 64Hz to 0b111.

答:
First convert the Hz value in the register to a number:

```<code>register_value >>= 7;  // Shift right 7 bits.
register_value &= 7;  // Keep remaining 3 bits.
</code>```

Next, calculate the Hz by left shifting.

```<code>herz = (1 << register_value) / 2.;
</code>```

To set the Herz value, perform the opposite (reverse) steps.

Body:
I am programming an IR camera. But the internal register for storing the refreshrate (bits 9, 8 and 7 of 16 in total) only accepts 1's and 0's of course. For example, a value of 0b110 means 32Hz and 0b111, 64Hz. How can I best convert a given integer (refresh_rate) value between 0.5Hz and 64Hz to 0b000 - 0b111 when looking at the Refresh rate control picture?

To be honest, I haven't tried anything yet because I think it requires a weird hack. However I have been thinking of a lookuptable, I think that is possible but requires more memory and it has to be memory efficient since it is programmed on an Arduino Due.

Note: You can see that the refresh_rate is an uint16_t which does not accept floating point values such as 0.5Hz, however, I am probably not going to use the 0.5Hz setting.

// the end-user currently has to pass a value between 7 and 0
void mlx90650::set_refresh_rate(uint16_t refresh_rate) {
    // So the refresh rate may not be higher than 7 (0b111) since this register only
accepts 3 bits. The memory map is:
/*
    | decimal | bits  |
    (decimal 0) 0 0 0 IR refresh rate = 0.5Hz
    (decimal 1) 0 0 1 IR refresh rate = 1Hz
    (decimal 2) 0 1 0 IR refresh rate = 2Hz (default)
    (decimal 3) 0 1 1 IR refresh rate = 4Hz
    (decimal 4) 1 0 0 IR refresh rate = 8Hz
    (decimal 5) 1 0 1 IR refresh rate = 16Hz
    (decimal 6) 1 1 0 IR refresh rate = 32Hz
    (decimal 7) 1 1 1 IR refresh rate = 64H
*/
}

I want that this function converts for example 64Hz to 0b111.

Body:
First convert the Hz value in the register to a number:

register_value >>= 7;  // Shift right 7 bits.
register_value &= 7;  // Keep remaining 3 bits.

Next, calculate the Hz by left shifting.

herz = (1 << register_value) / 2.;

To set the Herz value, perform the opposite (reverse) steps.