问:
Alternative way to code without loops in R and reduce looping time
I have 2 datasets with text data. dataset1: clusters11 and dataset2: transactns11. I am using For loop in the code and it takes more time to process the data. So can anyone suggest how I can reduce the time taken. Please go through the below sample datasets and the code I am using.

```<code>dataset1: clusters11
cluster_id  clusters
1   A,B,C
2   A,B
3   B,C
4   C,D,E
5   B,C,D
6   D,E,F
7   A,D,F
8   B,G,H
9   B,C,F
10  G,H,M
11  A,H,N
12  B,C,M

dataset2: transactns11
unique_id   skills
221 A,B,C
223 A,B
224 B,C
225 C,D,E,F
226 B,C,D,M
227 D,E,F,A
228 A,D,F
229 B,G,H
230 B,C,F,A
231 G,H,M
232 A,H,N
233 B,C,M
234 A,B,C
235 A,B
236 B,C
237 C,D,E
238 B,C,D
239 D,E,F
</code>```

I want to have my output:out_df like this( which ever cluster in clusters11 file matches 100% with the skills in transactns11 should appear as comma separate by cluster_id, with the column name matching_values in the output.

```<code>unique_id   skills  matching_values
221 A,B,C   1,2,3
223 A,B 2
224 B,C 3
225 C,D,E,F 4,6
226 B,C,D,M 3,5,12
227 D,E,F,A 6,7
228 A,D,F   7
229 B,G,H   8
230 B,C,F,A 1,2,3,9
231 G,H,M   10
232 A,H,N   11
233 B,C,M   3,12
234 A,B,C   1,2,3
235 A,B 2
236 B,C 3
237 C,D,E   4
238 B,C,D   3,5
239 D,E,F   6
</code>```

For this I have code and it is working fine with me

```<code>  out_df <- data.frame(matrix(0, ncol = 3, nrow = nrow(transactns11)))
  colnames(out_df) <- c("unique_id", "skills", "matching_values")

  transactns11$skills=as.character(transactns11$skills)
  clusters11$clusters=as.character(clusters11$clusters)

  for(n in 1:nrow(transactns11)) {

    trans1 <- strsplit(transactns11$skills[n], ",")
    trans1
    transvect <- unlist(trans1)
    transvect

    matching_clusters <- c("")
    out_df$unique_id[n] <- as.character(transactns11$unique_id[n])
    out_df$skills[n] <- transactns11$skills[n]

    for(i in 1:nrow(clusters11)) {

      cluster1 <- strsplit(clusters11$clusters[i],",")
      cluster1
      clustervect <- unlist(cluster1)
      clustervect

      if(length(intersect(transvect,clustervect)) == length(clustervect)) {
        matching_clusters <- paste(matching_clusters, clusters11$cluster_id[i], sep = ",")
      }
    }
    matching_clusters <- substring(matching_clusters,2)
    out_df$matching_values[n] <- matching_clusters
  }
</code>```

Problem is that, the code takes more time when the recods of the 2 files are more like 10000 or 20000( time taken will be 15 to 20 hrs).
Can any one please suggest how I can avoid this loops or any other method I can follow to improve the code and reduce the huge time taken to process.
Thanks in Advance.
Regards,
hari

答:
New solution with transactions and matrices:

This new method transforms your data to incidence matrices which make it easy to compare transactions to clusters.

```<code>library(arules)

transactions1 <- read.table(text = "unique_id   skills
                            221 A,B,C
                            223 A,B
                            224 B,C
                            225 C,D,E,F
                            226 B,C,D,M
                            227 D,E,F,A
                            228 A,D,F
                            229 B,G,H
                            230 B,C,F,A
                            231 G,H,M
                            232 A,H,N
                            233 B,C,M
                            234 A,B,C
                            235 A,B
                            236 B,C
                            237 C,D,E
                            238 B,C,D
                            239 D,E,F", header=T, stringsAsFactors=F)

clusters <- read.table(text="cluster_id  clusters
                       1   A,B,C
                       2   A,B
                       3   B,C
                       4   C,D,E
                       5   B,C,D
                       6   D,E,F
                       7   A,D,F
                       8   B,G,H
                       9   B,C,F
                       10  G,H,M
                       11  A,H,N
                       12  B,C,M", header=T, stringsAsFactors=F)

transactions2 <- sapply(split(transactions1$skills, 1:nrow(transactions1)), strsplit, split = ",")
names(transactions2) <- transactions1$unique_id

clusters2 <- sapply(split(clusters$clusters, 1:nrow(clusters)), strsplit, split=",")
names(clusters2) <- clusters$cluster_id

transactions2 <- +(as(as(transactions2, "transactions"), "matrix"))
clusters2 <- +(as(as(clusters2, "transactions"), "matrix"))

coInc <- transactions2 %*% t(clusters2)
coInc <- t(+(t(coInc) == rowSums(clusters2)))

res <- as(coInc, "transactions")
res <- as(res, "list")
res <- as.data.frame(sapply(res, paste, collapse=","))
res$skills <- transactions1$skills[match(rownames(res), transactions1$unique_id)]
res$id <- rownames(res)
colnames(res) <- c("clusters","skills","id")
res <- res[, c(3,2,1)]

#     id  skills clusters
#221 221   A,B,C    1,2,3
#223 223     A,B        2
#224 224     B,C        3
#225 225 C,D,E,F      4,6
#226 226 B,C,D,M   3,5,12
#227 227 D,E,F,A      6,7
#228 228   A,D,F        7
#229 229   B,G,H        8
#230 230 B,C,F,A  1,2,3,9
#231 231   G,H,M       10
#232 232   A,H,N       11
#233 233   B,C,M     3,12
#234 234   A,B,C    1,2,3
#235 235     A,B        2
#236 236     B,C        3
#237 237   C,D,E        4
#238 238   B,C,D      3,5
#239 239   D,E,F        6
</code>```

Body:
I have 2 datasets with text data. dataset1: clusters11 and dataset2: transactns11. I am using For loop in the code and it takes more time to process the data. So can anyone suggest how I can reduce the time taken. Please go through the below sample datasets and the code I am using.

dataset1: clusters11
cluster_id  clusters
1   A,B,C
2   A,B
3   B,C
4   C,D,E
5   B,C,D
6   D,E,F
7   A,D,F
8   B,G,H
9   B,C,F
10  G,H,M
11  A,H,N
12  B,C,M

dataset2: transactns11
unique_id   skills
221 A,B,C
223 A,B
224 B,C
225 C,D,E,F
226 B,C,D,M
227 D,E,F,A
228 A,D,F
229 B,G,H
230 B,C,F,A
231 G,H,M
232 A,H,N
233 B,C,M
234 A,B,C
235 A,B
236 B,C
237 C,D,E
238 B,C,D
239 D,E,F

I want to have my output:out_df like this( which ever cluster in clusters11 file matches 100% with the skills in transactns11 should appear as comma separate by cluster_id, with the column name matching_values in the output.

unique_id   skills  matching_values
221 A,B,C   1,2,3
223 A,B 2
224 B,C 3
225 C,D,E,F 4,6
226 B,C,D,M 3,5,12
227 D,E,F,A 6,7
228 A,D,F   7
229 B,G,H   8
230 B,C,F,A 1,2,3,9
231 G,H,M   10
232 A,H,N   11
233 B,C,M   3,12
234 A,B,C   1,2,3
235 A,B 2
236 B,C 3
237 C,D,E   4
238 B,C,D   3,5
239 D,E,F   6

For this I have code and it is working fine with me

  out_df <- data.frame(matrix(0, ncol = 3, nrow = nrow(transactns11)))
  colnames(out_df) <- c("unique_id", "skills", "matching_values")

  transactns11$skills=as.character(transactns11$skills)
  clusters11$clusters=as.character(clusters11$clusters)

  for(n in 1:nrow(transactns11)) {

    trans1 <- strsplit(transactns11$skills[n], ",")
    trans1
    transvect <- unlist(trans1)
    transvect

    matching_clusters <- c("")
    out_df$unique_id[n] <- as.character(transactns11$unique_id[n])
    out_df$skills[n] <- transactns11$skills[n]

    for(i in 1:nrow(clusters11)) {

      cluster1 <- strsplit(clusters11$clusters[i],",")
      cluster1
      clustervect <- unlist(cluster1)
      clustervect

      if(length(intersect(transvect,clustervect)) == length(clustervect)) {
        matching_clusters <- paste(matching_clusters, clusters11$cluster_id[i], sep = ",")
      }
    }
    matching_clusters <- substring(matching_clusters,2)
    out_df$matching_values[n] <- matching_clusters
  }

Problem is that, the code takes more time when the recods of the 2 files are more like 10000 or 20000( time taken will be 15 to 20 hrs).
Can any one please suggest how I can avoid this loops or any other method I can follow to improve the code and reduce the huge time taken to process.
Thanks in Advance.
Regards,
hari

Body:
New solution with transactions and matrices:

This new method transforms your data to incidence matrices which make it easy to compare transactions to clusters.

library(arules)

transactions1 <- read.table(text = "unique_id   skills
                            221 A,B,C
                            223 A,B
                            224 B,C
                            225 C,D,E,F
                            226 B,C,D,M
                            227 D,E,F,A
                            228 A,D,F
                            229 B,G,H
                            230 B,C,F,A
                            231 G,H,M
                            232 A,H,N
                            233 B,C,M
                            234 A,B,C
                            235 A,B
                            236 B,C
                            237 C,D,E
                            238 B,C,D
                            239 D,E,F", header=T, stringsAsFactors=F)

clusters <- read.table(text="cluster_id  clusters
                       1   A,B,C
                       2   A,B
                       3   B,C
                       4   C,D,E
                       5   B,C,D
                       6   D,E,F
                       7   A,D,F
                       8   B,G,H
                       9   B,C,F
                       10  G,H,M
                       11  A,H,N
                       12  B,C,M", header=T, stringsAsFactors=F)

transactions2 <- sapply(split(transactions1$skills, 1:nrow(transactions1)), strsplit, split = ",")
names(transactions2) <- transactions1$unique_id

clusters2 <- sapply(split(clusters$clusters, 1:nrow(clusters)), strsplit, split=",")
names(clusters2) <- clusters$cluster_id

transactions2 <- +(as(as(transactions2, "transactions"), "matrix"))
clusters2 <- +(as(as(clusters2, "transactions"), "matrix"))

coInc <- transactions2 %*% t(clusters2)
coInc <- t(+(t(coInc) == rowSums(clusters2)))

res <- as(coInc, "transactions")
res <- as(res, "list")
res <- as.data.frame(sapply(res, paste, collapse=","))
res$skills <- transactions1$skills[match(rownames(res), transactions1$unique_id)]
res$id <- rownames(res)
colnames(res) <- c("clusters","skills","id")
res <- res[, c(3,2,1)]

#     id  skills clusters
#221 221   A,B,C    1,2,3
#223 223     A,B        2
#224 224     B,C        3
#225 225 C,D,E,F      4,6
#226 226 B,C,D,M   3,5,12
#227 227 D,E,F,A      6,7
#228 228   A,D,F        7
#229 229   B,G,H        8
#230 230 B,C,F,A  1,2,3,9
#231 231   G,H,M       10
#232 232   A,H,N       11
#233 233   B,C,M     3,12
#234 234   A,B,C    1,2,3
#235 235     A,B        2
#236 236     B,C        3
#237 237   C,D,E        4
#238 238   B,C,D      3,5
#239 239   D,E,F        6


来源: stackexchange

问:
Where is the angular-cli package directory?
I'm trying to create a new Angular CLI project stub as instructed in the IntelliJ docs here Generating an Angular application stub using Angular CLI. One of the mandatory fields asks for the Angular CLI package directory.

I have no idea what this is or where to find this.

答:
Angular CLI field value should be something similar to: C:\Users[user name here]\AppData\Roaming\npm\node_modules\@angular\cli as @lena mentioned

The problem:  I had an old version of angular-cli installed. Even though I followed the Intellij docs for creating an Angular stub project, and installed the new @angular/cli via npm install, the new cli directory was not present.

The solution: I followed the instruction from updating-angular-cli and this installed the updated @angular\cli directory which then allowed Intellij IDEA to correctly build the Angular module. You have to uninstall old versions before installing the new cli. I was then able to point the Angular CLI field to the above path and the module generated correctly.

ContentLicense: CC BY-SA 3.0


问:
C# : Wait for n miliseconds, and test a boolean every n/m milliseconds
I have to test a boolean every 250ms, max 2 seconds, and stop when it's true.

Tried this, but Thread.Sleep isn't a good solution for me because it's freezing my software, and waiting for even more won't change anything because the boolean won't change while it's frozen.

```<code>bool onLine = false;
Stopwatch chrono = new Stopwatch();
chrono.Start();
while (chrono.ElapsedMilliseconds < 2000)
{
    if (!State().Equals(Online))
    {
        System.Threading.Thread.Sleep(250);
    }
    else
    {
        onLine = true;
        operationCompleted();
        break;
    }
}
if (!onLine)
{
    MessageBox.Show("Error");
}
</code>```

I don't know how to make the tests in an event raised by a timer if someone has a correct implementation?

答:
You can use a BackgroundWorker.

```<code>BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
</code>```

The DoWork event handler of a BackgroundWorker is executed on a deditecated thread, so your software will not freeze.

```<code>private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    bool onLine = false;

    Stopwatch chrono = new Stopwatch();
    chrono.Start();

    while (chrono.ElapsedMilliseconds < 2000)
    {
        if (!State().Equals(Online))
        {
            System.Threading.Thread.Sleep(250);
        }
        else
        {
            onLine = true;
            break;
        }
    }

    e.Result = onLine;
}
</code>```

When the background operation has completed, the RunWorkerCompleted event occurs. You can pass an object from DoWork to RunWorkerCompleted by using
e.Result.

```<code>private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    bool onLine = (bool)e.Result;
    if (onLine)
    {
        operationCompleted();
    }
    else
    {
        MessageBox.Show("Error");
    }
}
</code>```

DoWork occurs when RunWorkerAsync is called. You have to call bw.RunWorkerAsync(); to start your BackgroundWorker.

Body:
I have to test a boolean every 250ms, max 2 seconds, and stop when it's true.

Tried this, but Thread.Sleep isn't a good solution for me because it's freezing my software, and waiting for even more won't change anything because the boolean won't change while it's frozen.

bool onLine = false;
Stopwatch chrono = new Stopwatch();
chrono.Start();
while (chrono.ElapsedMilliseconds < 2000)
{
    if (!State().Equals(Online))
    {
        System.Threading.Thread.Sleep(250);
    }
    else
    {
        onLine = true;
        operationCompleted();
        break;
    }
}
if (!onLine)
{
    MessageBox.Show("Error");
}

I don't know how to make the tests in an event raised by a timer if someone has a correct implementation?

Body:
You can use a BackgroundWorker.

BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;

The DoWork event handler of a BackgroundWorker is executed on a deditecated thread, so your software will not freeze.

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    bool onLine = false;

    Stopwatch chrono = new Stopwatch();
    chrono.Start();

    while (chrono.ElapsedMilliseconds < 2000)
    {
        if (!State().Equals(Online))
        {
            System.Threading.Thread.Sleep(250);
        }
        else
        {
            onLine = true;
            break;
        }
    }

    e.Result = onLine;
}

When the background operation has completed, the RunWorkerCompleted event occurs. You can pass an object from DoWork to RunWorkerCompleted by using
e.Result.

private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    bool onLine = (bool)e.Result;
    if (onLine)
    {
        operationCompleted();
    }
    else
    {
        MessageBox.Show("Error");
    }
}

DoWork occurs when RunWorkerAsync is called. You have to call bw.RunWorkerAsync(); to start your BackgroundWorker.


问:
I want to run a t-test, how should I organise my data to do so?
In R, I want to run a statistical test to compare the averages between two categories, but I do not know how to organise my data to do so.

Mock example

My data is organised like:

```<code>structure(list(age = c(39, 45, 83, 68, 48, 52, 66, 50, 61, 67), gender =
structure(c(2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L), .Label = c("female",
"male"), class = "factor")), .Names = c("age", "gender"), row.names = c(NA,
10L), class = "data.frame")
</code>```

What I want to do is compare the average of each gender with a Welch t-test, answering the question "do women's ages are significantly different from men's ages?".

Theoretically, to run the test, I think my data should be in the form:

```<code>male  female
39    45
83    61
...
</code>```

I'm sure there is either a way to run the test directly on the original table or an easy way to transform my data into this form...

So, how should I proceed?

答:
If df is your data set, you can do

```<code>t.test(age ~ gender, data=df, alternative='two.sided')
</code>```

and there's no need to reorganise the data.

Body:
In R, I want to run a statistical test to compare the averages between two categories, but I do not know how to organise my data to do so.

Mock example

My data is organised like:

structure(list(age = c(39, 45, 83, 68, 48, 52, 66, 50, 61, 67), gender =
structure(c(2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L), .Label = c("female",
"male"), class = "factor")), .Names = c("age", "gender"), row.names = c(NA,
10L), class = "data.frame")

What I want to do is compare the average of each gender with a Welch t-test, answering the question "do women's ages are significantly different from men's ages?".

Theoretically, to run the test, I think my data should be in the form:

male  female
39    45
83    61
...

I'm sure there is either a way to run the test directly on the original table or an easy way to transform my data into this form...

So, how should I proceed?

Body:
If df is your data set, you can do

t.test(age ~ gender, data=df, alternative='two.sided')

and there's no need to reorganise the data.


问:
Same 'find' query in loopback and mongo has different behaviors in different servers
I am trying to make a query in a loopback model JS file. It is very simple and it as follow:

```<code>    //isAdmin function: gets a boolean if a given userProfileId is an admin or not.
    Userprofile.isAdmin = function(userProfileId, cb) {
        let role = app.models.Role;
        let rolemapping = app.models.RoleMapping;

        let filter = {
            "where": {
                "name": "admin"
            },
            "fields": ["id"]
        };

        role.findOne(filter, function(err, instance) {
            let filter2 = {
                "where": {
                    "roleId": instance.id
                    ,
                    "principalId": userProfileId,
                },
                "fields": ["id"]
            };

            rolemapping.find(filter2, function(err, instance) {
                if (instance != "") {
                    cb(null, true);
                } else {
                    cb(null, false);
                }
            });
        });
    };
</code>```

This works perfectly in our development server. The console.log of filter2, in that server returns:

```<code>{ where:
    { roleId: 5890ef8bbef9b73e568c6933,
        principalId: '5890ef8bbef9b73e568c6932' },
        fields: [ 'id' ]
    }
 }
</code>```

The console.log of instance.id looks like:

```<code>ObjectID { _bsontype: 'ObjectID', id: 'Xï¾ù·>Vi3 }
</code>```

The issue is in our production server. I have made the deployment process and the result is slightly different, but it does not work at all :(.

The console.log of filter2 returns:

```<code>{ where:
     { roleId: 58921dff16d9a37009e21104,
          principalId: '5890ef8bbef9b73e568c6932' },
          fields: [ 'id' ] }
}
</code>```

And, console.log(instance.id) returns:

```<code>ObjectID { _bsontype: 'ObjectID', id: Buffer [ 88, 146, 29, 255, 22, 217, 163, 112, 9, 226, 17, 4 ] }
</code>```

This query in our production server does not return any documents, even if there are documents in the DB that satisfy the query.

I have compared all the npm packages, and all are exactly the same version. As well the servers (we are using Debian 8) and mongo (v3.2.10).

Does any have an idea to resolve this?

Thanks in advance!

Pedro.

答:
I have experienced a similar issue when querying with an ID of a related model. Solved the issue by opting for like instead of equivalence like below:

```<code>{where:
   {and:[{roleId: {like:'.*'+roleIdVar+'*.', options:'i'}},
      {principalId: {like:'.*'+principalIdVar+'*.', options:'i'}}]
   }
 },
 {fields: [ 'id' ] }
</code>```

Please also note that field projection should be a separate object from the where filter.

Body:
I am trying to make a query in a loopback model JS file. It is very simple and it as follow:

    //isAdmin function: gets a boolean if a given userProfileId is an admin or not.
    Userprofile.isAdmin = function(userProfileId, cb) {
        let role = app.models.Role;
        let rolemapping = app.models.RoleMapping;

        let filter = {
            "where": {
                "name": "admin"
            },
            "fields": ["id"]
        };

        role.findOne(filter, function(err, instance) {
            let filter2 = {
                "where": {
                    "roleId": instance.id
                    ,
                    "principalId": userProfileId,
                },
                "fields": ["id"]
            };

            rolemapping.find(filter2, function(err, instance) {
                if (instance != "") {
                    cb(null, true);
                } else {
                    cb(null, false);
                }
            });
        });
    };

This works perfectly in our development server. The console.log of filter2, in that server returns:

{ where:
    { roleId: 5890ef8bbef9b73e568c6933,
        principalId: '5890ef8bbef9b73e568c6932' },
        fields: [ 'id' ]
    }
 }

The console.log of instance.id looks like:

ObjectID { _bsontype: 'ObjectID', id: 'Xï¾ù·>Vi3 }

The issue is in our production server. I have made the deployment process and the result is slightly different, but it does not work at all :(.

The console.log of filter2 returns:

{ where:
     { roleId: 58921dff16d9a37009e21104,
          principalId: '5890ef8bbef9b73e568c6932' },
          fields: [ 'id' ] }
}

And, console.log(instance.id) returns:

ObjectID { _bsontype: 'ObjectID', id: Buffer [ 88, 146, 29, 255, 22, 217, 163, 112, 9, 226, 17, 4 ] }

This query in our production server does not return any documents, even if there are documents in the DB that satisfy the query.

I have compared all the npm packages, and all are exactly the same version. As well the servers (we are using Debian 8) and mongo (v3.2.10).

Does any have an idea to resolve this?

Thanks in advance!

Pedro.

Body:
I have experienced a similar issue when querying with an ID of a related model. Solved the issue by opting for like instead of equivalence like below:

{where:
   {and:[{roleId: {like:'.*'+roleIdVar+'*.', options:'i'}},
      {principalId: {like:'.*'+principalIdVar+'*.', options:'i'}}]
   }
 },
 {fields: [ 'id' ] }

Please also note that field projection should be a separate object from the where filter.


问:
Knex:Error Pool2 - error: password authentication failed for user
I am having trouble with my migration to Ubuntu Linux.  I can use Postgres in the terminal. So I don't have a problem with the Postgres password.

When I type: knex migrate:latest --env development

I get:

Using environment: development
Knex:warning - Pool2 - Error: Pool was destroyed
Knex:Error Pool2 - error: password authentication failed for user "user"

I've read from other answers in related questions to go into the pg_hba.conf and set the method to trust. I've done this, but no change.
my knex.js file looks like this:

module.exports = {
     devolopment: {client: 'pg',
     connection: 'postres://localhost/bikesdb'
     },
     production: {
     client: 'pg',
     connection: process.env.DATABASE_URL
     }
   };

I'm not sure what I'm doing wrong.

答:
I had the same problem and it was all about the fact that knex was trying to use the username taken from the environment variables (LOGNAME) of my command line interface.
If Postgres is set to use a different user (by default postgres if you haven't changed it) there will be an authentication error.
You can solve this problem by specifying the authentication credentials inside the URL that you use to connect to the Postgres server.

```<code>module.exports = {
 devolopment: {client: 'pg',
 connection: 'postres://user:password@localhost/bikesdb'
 },
 production: {
 client: 'pg',
 connection: process.env.DATABASE_URL
 }
</code>```

};

I highlight the key point in the URL:
postres://user:password@localhost/bikesdb

You can check the name of the user to access the Postgresql server from the UI interface provided by Postgresql and the password is the one that you have defined during the installation phase (if you haven't changed it)

Body:
I had the same problem and it was all about the fact that knex was trying to use the username taken from the environment variables (LOGNAME) of my command line interface.
If Postgres is set to use a different user (by default postgres if you haven't changed it) there will be an authentication error.
You can solve this problem by specifying the authentication credentials inside the URL that you use to connect to the Postgres server.

module.exports = {
 devolopment: {client: 'pg',
 connection: 'postres://user:password@localhost/bikesdb'
 },
 production: {
 client: 'pg',
 connection: process.env.DATABASE_URL
 }

};

I highlight the key point in the URL:
postres://user:password@localhost/bikesdb

You can check the name of the user to access the Postgresql server from the UI interface provided by Postgresql and the password is the one that you have defined during the installation phase (if you haven't changed it)


问:
FirebaseListAdapter, How to update individual list views after populateView
I have implemented a firebase list adapter to load a list of items (List A). My data structure is setup such that within List A, each item also contains a reference id to some information located somewhere else (isNice) in the database. Likewise:

```<code>ListA
- ObjA
  - title : "hi"
  - id : "ObjAid"

isNice
- ObjAid : "true"
</code>```

I'm currently using another database operation to look up the id in the "isNice" child, passing the "ObjAid", the list position, and then a resultReceiver to get the result back. My problem is, when the resultReceiver get a resultData (the value "true"), I have no idea how modify the data within firebase list at the specific position.

My past experience is to use load my data into my own ArrayList and create a custom adapter for the listView, in that case, I could easily update the populated view as extra information is loaded. I would like to avoid setting up my own ArrayList to store the data for the time being, favoring the simplicity of the FirebaseListAdapter. All tips are appreciated, thx :)

答:
After some trial and error, I've instead gone with the RecyclerView + FirebaseRecyclerViewAdapter approach. Using recycler is better for the long run too, in my use case the list could get to 3K+. Using RecyclerView.findViewHolderForAdapterPosition() I can reference the specific itemView after I get the data from the result receiver likewise:                         </p>

```<code>// Reference the Item's view at the specific position
myViewHolder itemViewHolder = recyclerView.findViewHolderForAdapterPosition(position);
// Reference and set the views and here......
</code>```

Body:
I have implemented a firebase list adapter to load a list of items (List A). My data structure is setup such that within List A, each item also contains a reference id to some information located somewhere else (isNice) in the database. Likewise:

ListA
- ObjA
  - title : "hi"
  - id : "ObjAid"

isNice
- ObjAid : "true"

I'm currently using another database operation to look up the id in the "isNice" child, passing the "ObjAid", the list position, and then a resultReceiver to get the result back. My problem is, when the resultReceiver get a resultData (the value "true"), I have no idea how modify the data within firebase list at the specific position.

My past experience is to use load my data into my own ArrayList and create a custom adapter for the listView, in that case, I could easily update the populated view as extra information is loaded. I would like to avoid setting up my own ArrayList to store the data for the time being, favoring the simplicity of the FirebaseListAdapter. All tips are appreciated, thx :)

Body:
After some trial and error, I've instead gone with the RecyclerView + FirebaseRecyclerViewAdapter approach. Using recycler is better for the long run too, in my use case the list could get to 3K+. Using RecyclerView.findViewHolderForAdapterPosition() I can reference the specific itemView after I get the data from the result receiver likewise:

// Reference the Item's view at the specific position
myViewHolder itemViewHolder = recyclerView.findViewHolderForAdapterPosition(position);
// Reference and set the views and here......


问:
R: dplyr "must be a 1d atomic vector or a list" error when using filter_()
I have a data.frame in R called possibleVotes which looks like this (see end of post for code that generated this data frame, or CSV table here):

```<code>+------+------+------+------+------+-------------+--------+------+
| Var1 | Var2 | Var3 | Var4 | Var5 | nSuccesses1 | p1     | use1 |
+------+------+------+------+------+-------------+--------+------+
| 0    | 0    | 0    | 1    | 0    | 0           | 0.0225 | 0    |
+------+------+------+------+------+-------------+--------+------+
| 1    | 0    | 0    | 1    | 0    | 1           | 0.1275 | 0    |
+------+------+------+------+------+-------------+--------+------+
| 0    | 1    | 0    | 1    | 0    | 1           | 0.1275 | 0    |
+------+------+------+------+------+-------------+--------+------+
| 1    | 1    | 0    | 1    | 0    | 2           | 0.7225 | 1    |
+------+------+------+------+------+-------------+--------+------+
</code>```

I'm trying to use dplyr to filter it with code like this:

```<code>round <- 1
roundTotal <- 2

pRound1 <- possibleVotes %>%
    select_(num_range("Var", 1:2), paste0("nSuccesses", round), paste0("p", round), paste0("use", round)) %>%
    distinct_() %>%
    filter_(paste0("nSuccesses", round) == roundTotal & paste0("use", round) == 1) %>%
    select_(paste0("p", round)) %>%
    sum()
</code>```

My expected results is simply that pRound1 would be 0.7225. Unfortunately I get this error:

Error: Column p1 must be a 1d atomic vector or a list

I went through each step and discovered that:

There is no error up and through distinct_().

The error shows up at the filter_() step.

num_range() did not work (the columns Var1 and Var2 were not selected)

I have no idea what the error means, and don't know why num_range()  did not give me the desired results either...

How do I troubleshoot this? Thanks!

EDIT: After generous help from @Axeman, I tried this for filter_():

```<code>filter_(lazyeval::interp(~a == b, a = as.name(paste0("nSuccesses", round)), b = roundTotal)) %>%
filter_(lazyeval::interp(~a == b, a = as.name(paste0("use", round)), b = 1))
</code>```

Unfortunately it still gives this:

Error: Column p1 must be a 1d atomic vector or a list

EDIT 2: I'm running dplyr 0.5.0 with R 3.4.0.

EDIT 3: Here is the complete code I used to generate the original possibleVotes data frame:

```<code>possibleVotes <- expand.grid(replicate(5, c(0,1), simplify = FALSE))

pSpecies <- 0.85
round <- 1
roundFor <- 2
roundAgainst <- 0
roundTotal <- roundFor + roundAgainst

possibleVotes[paste0("nSuccesses", round)] <- rowSums(possibleVotes[1:roundTotal])
possibleVotes[paste0("p", round)] <- pSpecies^(possibleVotes[paste0("nSuccesses", round)]) * (1-pSpecies)^(round1Total - possibleVotes[paste0("nSuccesses", round)])

possibleVotes[paste0("use", round)] <- apply(possibleVotes, 1, function(x) if (sum(x[1:roundTotal]) == roundFor) 1 else 0)
</code>```

答:
One simple trick is to paste0 all the things together to create a condition, freeing from otherwise troublesome condition building process. You could build a condition like this: paste0("nSuccesses", round, "==", roundTotal, "&", "use", round, "== 1") it will give you something like "nSuccesses1==2&use1== 1", and just filter_ that.

Edit: all of my code:

```<code>dmatrix <- t(matrix(c(0,0,0,1,0,0,0.0225,0,
                    1,0,0,1,0,1,0.1275,0,
                    0,1,0,1,0,1,0.1275,0,
                    1,1,0,1,0,2,0.7225,1), nrow = 8, ncol = 4))
possibleVotes <- data.frame(dmatrix)
colnames(possibleVotes) <- c("Var1","Var2","Var3","Var4","Var5","nSuccesses1","p1","use1")

round <- 1
roundTotal <- 2

pRound1 <- possibleVotes %>%
    select_(~num_range("Var", 1:2), paste0("nSuccesses", round), paste0("p", round), paste0("use", round)) %>%
    distinct_() %>%
    filter_(paste0("nSuccesses", round, "==", roundTotal, "&", "use", round, "== 1")) %>%
    select_(paste0("p", round)) %>%
    sum()
</code>```

Body:
I have a data.frame in R called possibleVotes which looks like this (see end of post for code that generated this data frame, or CSV table here):

+------+------+------+------+------+-------------+--------+------+
| Var1 | Var2 | Var3 | Var4 | Var5 | nSuccesses1 | p1     | use1 |
+------+------+------+------+------+-------------+--------+------+
| 0    | 0    | 0    | 1    | 0    | 0           | 0.0225 | 0    |
+------+------+------+------+------+-------------+--------+------+
| 1    | 0    | 0    | 1    | 0    | 1           | 0.1275 | 0    |
+------+------+------+------+------+-------------+--------+------+
| 0    | 1    | 0    | 1    | 0    | 1           | 0.1275 | 0    |
+------+------+------+------+------+-------------+--------+------+
| 1    | 1    | 0    | 1    | 0    | 2           | 0.7225 | 1    |
+------+------+------+------+------+-------------+--------+------+

I'm trying to use dplyr to filter it with code like this:

round <- 1
roundTotal <- 2

pRound1 <- possibleVotes %>%
    select_(num_range("Var", 1:2), paste0("nSuccesses", round), paste0("p", round), paste0("use", round)) %>%
    distinct_() %>%
    filter_(paste0("nSuccesses", round) == roundTotal & paste0("use", round) == 1) %>%
    select_(paste0("p", round)) %>%
    sum()

My expected results is simply that pRound1 would be 0.7225. Unfortunately I get this error:

Error: Column p1 must be a 1d atomic vector or a list

I went through each step and discovered that:

There is no error up and through distinct_().

The error shows up at the filter_() step.

num_range() did not work (the columns Var1 and Var2 were not selected)

I have no idea what the error means, and don't know why num_range()  did not give me the desired results either...

How do I troubleshoot this? Thanks!

EDIT: After generous help from @Axeman, I tried this for filter_():

filter_(lazyeval::interp(~a == b, a = as.name(paste0("nSuccesses", round)), b = roundTotal)) %>%
filter_(lazyeval::interp(~a == b, a = as.name(paste0("use", round)), b = 1))

Unfortunately it still gives this:

Error: Column p1 must be a 1d atomic vector or a list

EDIT 2: I'm running dplyr 0.5.0 with R 3.4.0.

EDIT 3: Here is the complete code I used to generate the original possibleVotes data frame:

possibleVotes <- expand.grid(replicate(5, c(0,1), simplify = FALSE))

pSpecies <- 0.85
round <- 1
roundFor <- 2
roundAgainst <- 0
roundTotal <- roundFor + roundAgainst

possibleVotes[paste0("nSuccesses", round)] <- rowSums(possibleVotes[1:roundTotal])
possibleVotes[paste0("p", round)] <- pSpecies^(possibleVotes[paste0("nSuccesses", round)]) * (1-pSpecies)^(round1Total - possibleVotes[paste0("nSuccesses", round)])

possibleVotes[paste0("use", round)] <- apply(possibleVotes, 1, function(x) if (sum(x[1:roundTotal]) == roundFor) 1 else 0)

Body:
One simple trick is to paste0 all the things together to create a condition, freeing from otherwise troublesome condition building process. You could build a condition like this: paste0("nSuccesses", round, "==", roundTotal, "&", "use", round, "== 1") it will give you something like "nSuccesses1==2&use1== 1", and just filter_ that.

Edit: all of my code:

dmatrix <- t(matrix(c(0,0,0,1,0,0,0.0225,0,
                    1,0,0,1,0,1,0.1275,0,
                    0,1,0,1,0,1,0.1275,0,
                    1,1,0,1,0,2,0.7225,1), nrow = 8, ncol = 4))
possibleVotes <- data.frame(dmatrix)
colnames(possibleVotes) <- c("Var1","Var2","Var3","Var4","Var5","nSuccesses1","p1","use1")

round <- 1
roundTotal <- 2

pRound1 <- possibleVotes %>%
    select_(~num_range("Var", 1:2), paste0("nSuccesses", round), paste0("p", round), paste0("use", round)) %>%
    distinct_() %>%
    filter_(paste0("nSuccesses", round, "==", roundTotal, "&", "use", round, "== 1")) %>%
    select_(paste0("p", round)) %>%
    sum()


问:
What is the best way to match elements by index for update in a Scatter plot?
Using d3.v3.min.js and the example D3 Zoomable Plot as reference I built a scatter plot. The most relevant bits are:

```<code>var svg = d3.select("#scatter1")
    .append("svg")
    .attr("width", outerWidth)
    .attr("height", outerHeight)
    .attr("viewBox", "0 0 " + (width + margin.left + margin.right) + " " + (height + margin.top + margin.bottom))
    .attr("preserveAspectRatio", "xMidYMid meet")
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")

var objects = svg.append("svg")
    .classed("objects", true)
    .attr("width", width)
    .attr("height", height);

objects.selectAll(".dot")
    .data(values)
    .enter().append("circle")
    .classed("dot", true)
    .attr("r", function (d) {
        return 6 * Math.sqrt(2.0 / Math.PI);
    })
    .attr("transform", transform)
    .style("fill", colorVal);
</code>```

and some needed css:

```<code>.dot {
  fill-opacity: .5;
}

.dotHidden {
  fill-opacity: .1;
}
</code>```

My data values are defined as:

```<code>var values = [[x0, y0], [x1, y1], ....];
</code>```

with a custom property idx per element i.e.

```<code>values[i].idx === i
</code>```

Now I'd like to highlight only a subset of the dot elements, namely only those that are in an indexes list e.g. selectedIdx = [4, 8, 10, 38, 90]. How can I do that in the fastest possible way? is there an d3 js idiomatic way to do it? I have attempted to do this below and it does the job to "hide" all the dots but I actually want to hide only the ones not in the selectedIdx.

```<code>d3.select("#scatter1 svg")
  .selectAll("circle")
  .attr("class", "dotHidden");
</code>```

UPDATE: This is a follow up of the question How to get the index of the data element in a histogram on mouseover?

答:
Use classed. You need to cycle through all elements only once, then. Plus your code gets more robust because you make sure that dotHidden class is unset for all circles in selectIdx.

```<code>d3.select("#scatter1 svg")
  .selectAll("circle")
  .classed("dotHidden", function(d, i) {
    return selectIdx.indexOf(i) === -1;
  });
</code>```

If you‘re running ES2015 or use a transpiler you can improve the callback:

```<code>.classed("dotHidden", (d, i) => selectIdx.indexOf(i) === -1);
</code>```

Body:
Using d3.v3.min.js and the example D3 Zoomable Plot as reference I built a scatter plot. The most relevant bits are:

var svg = d3.select("#scatter1")
    .append("svg")
    .attr("width", outerWidth)
    .attr("height", outerHeight)
    .attr("viewBox", "0 0 " + (width + margin.left + margin.right) + " " + (height + margin.top + margin.bottom))
    .attr("preserveAspectRatio", "xMidYMid meet")
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")

var objects = svg.append("svg")
    .classed("objects", true)
    .attr("width", width)
    .attr("height", height);

objects.selectAll(".dot")
    .data(values)
    .enter().append("circle")
    .classed("dot", true)
    .attr("r", function (d) {
        return 6 * Math.sqrt(2.0 / Math.PI);
    })
    .attr("transform", transform)
    .style("fill", colorVal);

and some needed css:

.dot {
  fill-opacity: .5;
}

.dotHidden {
  fill-opacity: .1;
}

My data values are defined as:

var values = [[x0, y0], [x1, y1], ....];

with a custom property idx per element i.e.

values[i].idx === i

Now I'd like to highlight only a subset of the dot elements, namely only those that are in an indexes list e.g. selectedIdx = [4, 8, 10, 38, 90]. How can I do that in the fastest possible way? is there an d3 js idiomatic way to do it? I have attempted to do this below and it does the job to "hide" all the dots but I actually want to hide only the ones not in the selectedIdx.

d3.select("#scatter1 svg")
  .selectAll("circle")
  .attr("class", "dotHidden");

UPDATE: This is a follow up of the question How to get the index of the data element in a histogram on mouseover?

Body:
Use classed. You need to cycle through all elements only once, then. Plus your code gets more robust because you make sure that dotHidden class is unset for all circles in selectIdx.

d3.select("#scatter1 svg")
  .selectAll("circle")
  .classed("dotHidden", function(d, i) {
    return selectIdx.indexOf(i) === -1;
  });

If you‘re running ES2015 or use a transpiler you can improve the callback:

.classed("dotHidden", (d, i) => selectIdx.indexOf(i) === -1);


问:
Qt: how to pass input values from on_pushButton_clicked() function to paintGL()
I am working on a mortgage calculator with QT.
When the user clicks the push button, the "on_pushButton_clicked()" function will get the inputs.

The Question is I am confused about how to pass the inputs from "on_pushButton_clicked()" to "paintGL()" function for drawing purpose.

My last resort is using global variables to solve this issue, but I really want to avoid something like that...so it will be more than welcome if someone can enlighten with better approaches.

My code are as followed:

mainwindow.h

```<code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ui_mainwindow.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:

    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    template <class T>
    T inputcheck(QLineEdit *input, T output);
    //friend class GLwidget;
    double loan;
    int maturity;
    double rate1;
    double rate2;
    double rate3;
    int period2;
    int period3;

private slots:
    void on_pushButton_Calculate_clicked();

    void on_pushButton_Quit_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
</code>```

mainwindow.cpp

```<code>#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCoreApplication>
#include <QDesktopWidget>
#include "glwidget.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

template <class T>
T MainWindow::inputcheck(QLineEdit *input, T output)
{
    bool ok;
    output = input->text().toDouble(&ok);
    if(TRUE && output > 0)
    {
        return output;
    }
    else
    {
        input->setText("ERR");
        return 0;
    }
}

void MainWindow::on_pushButton_Calculate_clicked()
{

    // getting inputs()
    loan = inputcheck(ui->lineEdit_loan, loan);
    maturity = inputcheck(ui->lineEdit_maturity, maturity);
    rate1 = inputcheck(ui->lineEdit_rate1, rate1);
    rate2 = inputcheck(ui->lineEdit_rate2, rate2);
    rate3 = inputcheck(ui->lineEdit_rate3, rate3);
    period2 = inputcheck(ui->lineEdit_period2, period2);
    period3 = inputcheck(ui->lineEdit_period3, period3);
    printf("loan:%f, maturity:%d,rate1:%f,rate2:%f,rate3:%f, period2:%d, period3:%d\n", loan, maturity, rate1, rate2, rate3, period2, period3);

}

void MainWindow::on_pushButton_Quit_clicked()
{
    QCoreApplication::quit();

}
</code>```

glwidget.h

```<code>#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QObject>
#include <QGLWidget>

class calculation;
class GLwidget : public QGLWidget
{
    Q_OBJECT
public:
    explicit GLwidget(QWidget *parent = 0);
    void initializeGL();
    void paintGL();
    void resizeGL(int width, int height);

private:

};

#endif // GLWIDGET_H
</code>```

glwidget.cpp

```<code>#include "glwidget.h"
#include <gl/GL.h>

GLwidget::GLwidget(QWidget *parent):QGLWidget(parent)
{
}

void GLwidget::initializeGL()
{
}

void GLwidget::paintGL()
{
**// I need the inputs to be here!!**
}

void GLwidget::resizeGL(int width, int height)
{
}
</code>```

Thanks for your help:)

答:
This can be solved by using some common object oriented ideas, namely member variables with setter functions. In your subclass of the QGLWidget (glwidget.h) add some member variables.

```<code>private:
  int m_my_int_value;
  double m_my_double_value;
</code>```

You will then also want some setter functions for those values (also in glwidget.h).

```<code>public:
  void Set_my_int_value(int val);
  void Set_my_double_value(double val);
</code>```

You can the put the declarations for those functions in glwidget.cpp.

```<code>void GLwidget::Set_my_int_value(int val)
{
  if(val != m_my_int_value)
  {
    m_my_int_value = val;
    update();
  }
}

void GLwidget::Set_my_double_value(double val)
{
  if(val != m_my_double_value)
  {
    m_my_double_value = val;
    update();
  }
}
</code>```

Call those setter functions in your on_click function. Those values will then be accessible in your implementation of paintGL.

Body:
I am working on a mortgage calculator with QT.
When the user clicks the push button, the "on_pushButton_clicked()" function will get the inputs.

The Question is I am confused about how to pass the inputs from "on_pushButton_clicked()" to "paintGL()" function for drawing purpose.

My last resort is using global variables to solve this issue, but I really want to avoid something like that...so it will be more than welcome if someone can enlighten with better approaches.

My code are as followed:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "ui_mainwindow.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:

    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    template <class T>
    T inputcheck(QLineEdit *input, T output);
    //friend class GLwidget;
    double loan;
    int maturity;
    double rate1;
    double rate2;
    double rate3;
    int period2;
    int period3;

private slots:
    void on_pushButton_Calculate_clicked();

    void on_pushButton_Quit_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCoreApplication>
#include <QDesktopWidget>
#include "glwidget.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

template <class T>
T MainWindow::inputcheck(QLineEdit *input, T output)
{
    bool ok;
    output = input->text().toDouble(&ok);
    if(TRUE && output > 0)
    {
        return output;
    }
    else
    {
        input->setText("ERR");
        return 0;
    }
}

void MainWindow::on_pushButton_Calculate_clicked()
{

    // getting inputs()
    loan = inputcheck(ui->lineEdit_loan, loan);
    maturity = inputcheck(ui->lineEdit_maturity, maturity);
    rate1 = inputcheck(ui->lineEdit_rate1, rate1);
    rate2 = inputcheck(ui->lineEdit_rate2, rate2);
    rate3 = inputcheck(ui->lineEdit_rate3, rate3);
    period2 = inputcheck(ui->lineEdit_period2, period2);
    period3 = inputcheck(ui->lineEdit_period3, period3);
    printf("loan:%f, maturity:%d,rate1:%f,rate2:%f,rate3:%f, period2:%d, period3:%d\n", loan, maturity, rate1, rate2, rate3, period2, period3);

}

void MainWindow::on_pushButton_Quit_clicked()
{
    QCoreApplication::quit();

}

glwidget.h

#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QObject>
#include <QGLWidget>

class calculation;
class GLwidget : public QGLWidget
{
    Q_OBJECT
public:
    explicit GLwidget(QWidget *parent = 0);
    void initializeGL();
    void paintGL();
    void resizeGL(int width, int height);

private:

};

#endif // GLWIDGET_H

glwidget.cpp

#include "glwidget.h"
#include <gl/GL.h>

GLwidget::GLwidget(QWidget *parent):QGLWidget(parent)
{
}

void GLwidget::initializeGL()
{
}

void GLwidget::paintGL()
{
**// I need the inputs to be here!!**
}

void GLwidget::resizeGL(int width, int height)
{
}

Thanks for your help:)

Body:
This can be solved by using some common object oriented ideas, namely member variables with setter functions. In your subclass of the QGLWidget (glwidget.h) add some member variables.

private:
  int m_my_int_value;
  double m_my_double_value;

You will then also want some setter functions for those values (also in glwidget.h).

public:
  void Set_my_int_value(int val);
  void Set_my_double_value(double val);

You can the put the declarations for those functions in glwidget.cpp.

void GLwidget::Set_my_int_value(int val)
{
  if(val != m_my_int_value)
  {
    m_my_int_value = val;
    update();
  }
}

void GLwidget::Set_my_double_value(double val)
{
  if(val != m_my_double_value)
  {
    m_my_double_value = val;
    update();
  }
}

Call those setter functions in your on_click function. Those values will then be accessible in your implementation of paintGL.


问:
Sumif a certain value in column A equals the same value in the same Column
There are two columns with data I looked into the SUMIF() function but none seem to check for matching values within the same column. I cannot figure it out.

I want to sum column A with the same strings in column B. So add all the A, all the B and all the C. But without defining the strings.

答:
For SUMIF to work correctly, you would need a helper column which contain the unique strings, (Column C)

Enter the formula in Column D and drag down to get the corresponding sum,

```<code>=SUMIF(B:B,C2,A:A)
</code>```

Body:
For SUMIF to work correctly, you would need a helper column which contain the unique strings, (Column C)

Enter the formula in Column D and drag down to get the corresponding sum,

=SUMIF(B:B,C2,A:A)


问:
Powershell web scraping IE drop down not updating values
I'm currently trying to develop a script in PowerShell that opens Internet Explorer, changes two drop down values (which updates a tables values), then scrape the table to a CSV file preferably.

By the way, the site has a static URL, changing the drop down doesn't change the URL:(

I can Get IE to open and change the two drop down values, but the table is not updating its values after this...
I think it is something to do with the site code where it shows the option for this dropdown as:

```<code><select id="pricing-display-by-dropdown"
onchange="$('html').trigger('rate.changed', this.value)">
 <option value="hour">Hour</option>
 <option value="month">Month</option>
</select>
</code>```

So basically what I have is this so far, and it will change the region and pricing, but won't update the table, maybe have to fire the trigger above somehow?:

```<code>$url = "https://azure.microsoft.com/en-us/pricing/details/sql-database/"
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
while ($ie.Busy() -and $ie.ReadyState -ne 4){ sleep -Milliseconds 6000 }

$ie.Document.IHTMLDocument3_getElementByID("region-selector").value = "europe-north"

$ie.Document.IHTMLDocument3_getElementByID("pricing-display-by-dropdown").value = "month"
</code>```

Any help would be greatly appreciated, i'm new to PowerShell, and stackoverflow, so excuse any silly stuff

答:
Here you go</p>

```<code>$ie.Document.IHTMLDocument3_getElementByID("pricing-display-by-dropdown").fireEvent("onchange")
</code>```

Body:
I'm currently trying to develop a script in PowerShell that opens Internet Explorer, changes two drop down values (which updates a tables values), then scrape the table to a CSV file preferably.

By the way, the site has a static URL, changing the drop down doesn't change the URL:(

I can Get IE to open and change the two drop down values, but the table is not updating its values after this...
I think it is something to do with the site code where it shows the option for this dropdown as:

<select id="pricing-display-by-dropdown"
onchange="$('html').trigger('rate.changed', this.value)">
 <option value="hour">Hour</option>
 <option value="month">Month</option>
</select>

So basically what I have is this so far, and it will change the region and pricing, but won't update the table, maybe have to fire the trigger above somehow?:

$url = "https://azure.microsoft.com/en-us/pricing/details/sql-database/"
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
while ($ie.Busy() -and $ie.ReadyState -ne 4){ sleep -Milliseconds 6000 }

$ie.Document.IHTMLDocument3_getElementByID("region-selector").value = "europe-north"

$ie.Document.IHTMLDocument3_getElementByID("pricing-display-by-dropdown").value = "month"

Any help would be greatly appreciated, i'm new to PowerShell, and stackoverflow, so excuse any silly stuff

Body:
Here you go

$ie.Document.IHTMLDocument3_getElementByID("pricing-display-by-dropdown").fireEvent("onchange")


问:
Issues compiling Veracrypt on Mac OS X El Capitan 10.11.5
I'm having issues compiling Veracrypt on El Capitan

This is the error I get:

```<code>../Crypto/Whirlpool.c:511:3: error: unknown token in expression
                KSH(2, 3, 2, 1, 0, 7)
                ^
../Crypto/Whirlpool.c:452:2: note: expanded from macro 'KSH'
        KSH##op(b, [AS_REG_6+2*2048+8*WORD_REG(di)])\
        ^
<scratch space>:264:1: note: expanded from here
KSH2
^
../Crypto/Whirlpool.c:445:2: note: expanded from macro 'KSH2'
        AS2(movq        [SSE2_workspace+8*a], mm##a)
        ^
/Users/whatnots/Desktop/veracrypt/src/Crypto/cpu.h:149:23: note: expanded from macro 'AS2'
    #define AS2(x, y) GNU_AS2(x, y)
                      ^
/Users/whatnots/Desktop/veracrypt/src/Crypto/cpu.h:144:27: note: expanded from macro 'GNU_AS2'
    #define GNU_AS2(x, y) #x ", " #y ";" NEW_LINE
                          ^
<scratch space>:270:2: note: expanded from here
"movq [%3+8*1]"
 ^
<inline asm>:162:7: note: instantiated into assembly here
movq [%r10+8*1], mm1;
      ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
</code>```

Here is my gcc info:

```<code>Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
</code>```

This compiles fine on Ubuntu 16.04. Not sure what i need to do. I've install all the required software and I've even tried two higher versions of gcc.

答:
The inline code for the nasm assembler is not correctly expanded, see also this
issue in veracrypt repository. The culprits are located in cpu.h (lines 259 until 263 in the 1.19 release). You can try to substitute these with

```<code>#define GNU_AS1(x) #x ";"
#define GNU_AS2(x, y) #x ", " #y ";"
#define GNU_AS3(x, y, z) #x ", " #y ", " #z ";"
#define GNU_ASL(x) "\n\t#x:"
#define GNU_ASJ(x, y, z) #x " " #y #z ";"
</code>```

but it could be that in the final test for encryption you will get an error for TestPkcs5.

Addendum 2017-06-08: The latest git clone of veracrypt compiles without problems, the inline assembly in the whirlpool hashing function is disabled now.

Body:
I'm having issues compiling Veracrypt on El Capitan

This is the error I get:

../Crypto/Whirlpool.c:511:3: error: unknown token in expression
                KSH(2, 3, 2, 1, 0, 7)
                ^
../Crypto/Whirlpool.c:452:2: note: expanded from macro 'KSH'
        KSH##op(b, [AS_REG_6+2*2048+8*WORD_REG(di)])\
        ^
<scratch space>:264:1: note: expanded from here
KSH2
^
../Crypto/Whirlpool.c:445:2: note: expanded from macro 'KSH2'
        AS2(movq        [SSE2_workspace+8*a], mm##a)
        ^
/Users/whatnots/Desktop/veracrypt/src/Crypto/cpu.h:149:23: note: expanded from macro 'AS2'
    #define AS2(x, y) GNU_AS2(x, y)
                      ^
/Users/whatnots/Desktop/veracrypt/src/Crypto/cpu.h:144:27: note: expanded from macro 'GNU_AS2'
    #define GNU_AS2(x, y) #x ", " #y ";" NEW_LINE
                          ^
<scratch space>:270:2: note: expanded from here
"movq [%3+8*1]"
 ^
<inline asm>:162:7: note: instantiated into assembly here
movq [%r10+8*1], mm1;
      ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

Here is my gcc info:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

This compiles fine on Ubuntu 16.04. Not sure what i need to do. I've install all the required software and I've even tried two higher versions of gcc.

Body:
The inline code for the nasm assembler is not correctly expanded, see also this
issue in veracrypt repository. The culprits are located in cpu.h (lines 259 until 263 in the 1.19 release). You can try to substitute these with

#define GNU_AS1(x) #x ";"
#define GNU_AS2(x, y) #x ", " #y ";"
#define GNU_AS3(x, y, z) #x ", " #y ", " #z ";"
#define GNU_ASL(x) "\n\t#x:"
#define GNU_ASJ(x, y, z) #x " " #y #z ";"

but it could be that in the final test for encryption you will get an error for TestPkcs5.

Addendum 2017-06-08: The latest git clone of veracrypt compiles without problems, the inline assembly in the whirlpool hashing function is disabled now.


问:
OpenCL: Additional directories for header files
The OpenCL specification writes in 5.6.3 Build Options:

5.6.3.1 Preprocessor options

```<code>...
-I dir
    Add the directory dir to the list of directories to be searched
    for header files.
</code>```

Can someone please explain what that means? As far as I know you cannot inlcude header files into your OpenCL kernels. So what could this options be for?

EDIT: Link to the OpenCL spec: http://www.khronos.org/registry/cl/specs/opencl-1.1.pdf

EDIT2: I was under the wrong assumption that it is not allowed to include header files into ones OpenCL kernel. Now I know better, thanks to jHackTheRipper.

答: I include the headers for development, but for releases I process all of the headers and dependencies into one file, and then embed that source in the binary.</p>

ContentLicense: CC BY-SA 2.5

Body:
The OpenCL specification writes in 5.6.3 Build Options:

5.6.3.1 Preprocessor options

...
-I dir
    Add the directory dir to the list of directories to be searched
    for header files.

Can someone please explain what that means? As far as I know you cannot inlcude header files into your OpenCL kernels. So what could this options be for?

EDIT: Link to the OpenCL spec: http://www.khronos.org/registry/cl/specs/opencl-1.1.pdf

EDIT2: I was under the wrong assumption that it is not allowed to include header files into ones OpenCL kernel. Now I know better, thanks to jHackTheRipper.


问:
How do I truncate and completely rewrite a file without having leading zeros?
When truncating a file it seems to be adding additional zero bytes to the start:

```<code>configFile, err := os.OpenFile("./version.json", os.O_RDWR, 0666)
defer configFile.Close()
check(err)
//some actions happen here
configFile.Truncate(0)
configFile.Write(js)
configFile.Sync()
</code>```

As a result the file has the contents I write with a section of 0 bytes at the beginning.

How do I truncate and completely rewrite a file without having leading zeros?

答:
See the documentation on Truncate:

Truncate changes the size of the file. It does not change the I/O offset. If there is an error, it will be of type *PathError.

So you also need to seek to the beginning of the file before you write:

```<code>configFile.Truncate(0)
configFile.Seek(0,0)
</code>```

As shorthand, use the flag os.O_TRUNC when calling os.OpenFile to truncate on open.

ContentLicense: CC BY-SA 4.0

Body:
When truncating a file it seems to be adding additional zero bytes to the start:

configFile, err := os.OpenFile("./version.json", os.O_RDWR, 0666)
defer configFile.Close()
check(err)
//some actions happen here
configFile.Truncate(0)
configFile.Write(js)
configFile.Sync()

As a result the file has the contents I write with a section of 0 bytes at the beginning.

How do I truncate and completely rewrite a file without having leading zeros?

Body:
See the documentation on Truncate:

Truncate changes the size of the file. It does not change the I/O offset. If there is an error, it will be of type *PathError.

So you also need to seek to the beginning of the file before you write:

configFile.Truncate(0)
configFile.Seek(0,0)

As shorthand, use the flag os.O_TRUNC when calling os.OpenFile to truncate on open.


问:
Player often continues to move after releasing key
for some reason, my player object continues to move after the movement key is released. The velocity stays at the players movespeed until the key is released pressed and released again. while looking at console output of velX and velY i see something it toggling each frame from 8 to 16 after a few seconds of hold down. and occasionally after releasing the key the velocity will stay at 8 or -8 anyways.

```<code>//Player tick method
public void tick() {

    //Dissalow Exceess Velocity
    velX = Calculations.clamp(velX, -getMaxSpeed(), getMaxSpeed());
    velY = Calculations.clamp(velY, -getMaxSpeed(), getMaxSpeed());

    //move the player
    x += velX;
    y += velY;

    //dissalow exiting room
    x = Calculations.clamp(x, 0, Game.WIDTH - 32);
    y = Calculations.clamp(y, 0, Game.HEIGHT - 32);

    //check for collisions with objects
    collision();
}
</code>```

here is my Clamp method as used above (Calculations.clamp)

```<code>//clamp
public static int clamp(int var, int min, int max)
{
    if(var >= max)
        return var = max;
    else if(var <= min)
        return var = min;
    else return var;
}
</code>```

finally my Keyboard input method

```<code>import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter
{
    private Handler myHandler;

    public KeyInput(Handler handler)
    {
        this.myHandler = handler;

    }
    public void keyPressed(KeyEvent e)
    {
        int key = e.getKeyCode();

        for(GameObject object : myHandler.objects)
        {
            if(object.getId() == ID.player)
            {
             if(key == KeyEvent.VK_W) object.setVelY(object.getVelY() - object.getMaxSpeed());
             if(key == KeyEvent.VK_S) object.setVelY(object.getVelY() + object.getMaxSpeed());
             if(key == KeyEvent.VK_A) object.setVelX(object.getVelX() - object.getMaxSpeed());
             if(key == KeyEvent.VK_D) object.setVelX(object.getVelX() + object.getMaxSpeed());
            }
        }
    }
    public void keyReleased(KeyEvent e)
    {
        int key = e.getKeyCode();
        for(GameObject object : myHandler.objects)
        {
            if(object.getId() == ID.player)
            {
             if(key == KeyEvent.VK_W) object.setVelY(object.getVelY() + object.getMaxSpeed());
             if(key == KeyEvent.VK_S) object.setVelY(object.getVelY() - object.getMaxSpeed());
             if(key == KeyEvent.VK_A) object.setVelX(object.getVelX() + object.getMaxSpeed());
             if(key == KeyEvent.VK_D) object.setVelX(object.getVelX() - object.getMaxSpeed());
            }
        }
    }
}
</code>```

答:
I have resolved the issue, it occured when the key was released after the additional velocity was added, but before the clamp kicked in. i fixed this by adding velocity checks to the keyinput itself.</p>

```<code>public void keyPressed(KeyEvent e)
{
    int key = e.getKeyCode();

    for(GameObject object : myHandler.objects)
    {
        if(object.getId() == ID.player)
        {
            if(!(object.getVelY() == 8))
            {
                 if(key == KeyEvent.VK_S) object.setVelY(object.getVelY() + object.getMaxSpeed());
            }
            if(!(object.getVelY() == -8))
            {
             if(key == KeyEvent.VK_W) object.setVelY(object.getVelY() - object.getMaxSpeed());
            }
            if(!(object.getVelX() == -8))
            {
             if(key == KeyEvent.VK_A) object.setVelX(object.getVelX() - object.getMaxSpeed());
            }
            if(!(object.getVelX() == 8))
            {
                 if(key == KeyEvent.VK_D) object.setVelX(object.getVelX() + object.getMaxSpeed());
            }
        }
    }
}
public void keyReleased(KeyEvent e)
{
    int key = e.getKeyCode();
    for(GameObject object : myHandler.objects)
    {
        if(object.getId() == ID.player)
        {

         if(key == KeyEvent.VK_W) object.setVelY(object.getVelY() + object.getMaxSpeed());
         if(key == KeyEvent.VK_S) object.setVelY(object.getVelY() - object.getMaxSpeed());
         if(key == KeyEvent.VK_A) object.setVelX(object.getVelX() + object.getMaxSpeed());
         if(key == KeyEvent.VK_D) object.setVelX(object.getVelX() - object.getMaxSpeed());

        }
    }
}
</code>```

Body:
for some reason, my player object continues to move after the movement key is released. The velocity stays at the players movespeed until the key is released pressed and released again. while looking at console output of velX and velY i see something it toggling each frame from 8 to 16 after a few seconds of hold down. and occasionally after releasing the key the velocity will stay at 8 or -8 anyways.

//Player tick method
public void tick() {

    //Dissalow Exceess Velocity
    velX = Calculations.clamp(velX, -getMaxSpeed(), getMaxSpeed());
    velY = Calculations.clamp(velY, -getMaxSpeed(), getMaxSpeed());

    //move the player
    x += velX;
    y += velY;

    //dissalow exiting room
    x = Calculations.clamp(x, 0, Game.WIDTH - 32);
    y = Calculations.clamp(y, 0, Game.HEIGHT - 32);

    //check for collisions with objects
    collision();
}

here is my Clamp method as used above (Calculations.clamp)

//clamp
public static int clamp(int var, int min, int max)
{
    if(var >= max)
        return var = max;
    else if(var <= min)
        return var = min;
    else return var;
}

finally my Keyboard input method

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter
{
    private Handler myHandler;

    public KeyInput(Handler handler)
    {
        this.myHandler = handler;

    }
    public void keyPressed(KeyEvent e)
    {
        int key = e.getKeyCode();

        for(GameObject object : myHandler.objects)
        {
            if(object.getId() == ID.player)
            {
             if(key == KeyEvent.VK_W) object.setVelY(object.getVelY() - object.getMaxSpeed());
             if(key == KeyEvent.VK_S) object.setVelY(object.getVelY() + object.getMaxSpeed());
             if(key == KeyEvent.VK_A) object.setVelX(object.getVelX() - object.getMaxSpeed());
             if(key == KeyEvent.VK_D) object.setVelX(object.getVelX() + object.getMaxSpeed());
            }
        }
    }
    public void keyReleased(KeyEvent e)
    {
        int key = e.getKeyCode();
        for(GameObject object : myHandler.objects)
        {
            if(object.getId() == ID.player)
            {
             if(key == KeyEvent.VK_W) object.setVelY(object.getVelY() + object.getMaxSpeed());
             if(key == KeyEvent.VK_S) object.setVelY(object.getVelY() - object.getMaxSpeed());
             if(key == KeyEvent.VK_A) object.setVelX(object.getVelX() + object.getMaxSpeed());
             if(key == KeyEvent.VK_D) object.setVelX(object.getVelX() - object.getMaxSpeed());
            }
        }
    }
}

Body:
I have resolved the issue, it occured when the key was released after the additional velocity was added, but before the clamp kicked in. i fixed this by adding velocity checks to the keyinput itself.

public void keyPressed(KeyEvent e)
{
    int key = e.getKeyCode();

    for(GameObject object : myHandler.objects)
    {
        if(object.getId() == ID.player)
        {
            if(!(object.getVelY() == 8))
            {
                 if(key == KeyEvent.VK_S) object.setVelY(object.getVelY() + object.getMaxSpeed());
            }
            if(!(object.getVelY() == -8))
            {
             if(key == KeyEvent.VK_W) object.setVelY(object.getVelY() - object.getMaxSpeed());
            }
            if(!(object.getVelX() == -8))
            {
             if(key == KeyEvent.VK_A) object.setVelX(object.getVelX() - object.getMaxSpeed());
            }
            if(!(object.getVelX() == 8))
            {
                 if(key == KeyEvent.VK_D) object.setVelX(object.getVelX() + object.getMaxSpeed());
            }
        }
    }
}
public void keyReleased(KeyEvent e)
{
    int key = e.getKeyCode();
    for(GameObject object : myHandler.objects)
    {
        if(object.getId() == ID.player)
        {

         if(key == KeyEvent.VK_W) object.setVelY(object.getVelY() + object.getMaxSpeed());
         if(key == KeyEvent.VK_S) object.setVelY(object.getVelY() - object.getMaxSpeed());
         if(key == KeyEvent.VK_A) object.setVelX(object.getVelX() + object.getMaxSpeed());
         if(key == KeyEvent.VK_D) object.setVelX(object.getVelX() - object.getMaxSpeed());

        }
    }
}


问:
Closed xml error: "Removed Records: Named range from /xl/workbook.xml part" Apostrophe issue
While dynamically creating Excel workbooks in C# using Closed XML, I encountered an error in Excel when attempting to open the workbook. The issue was caused by an apostrophe (') in the name of a sheet. When Excel "repairs" and opens the workbook, the sheet's name remains unchanged and is still there with the offending apostrophe...

Excel does support apostrophes in the sheet name.

ExcelSheets with Apostrophes:

Why does it choke on this character when being generated with Closed XML?

I can easily remove this as a "special character" but would prefer not to.

This is related to: xlsx error: "Removed Records: Named range from /xl/workbook.xml part" when tried to resolve errors i

答: This may well be to do with the name of a named range, not the name of a sheet. See my answer to this question</p>


问:
Django - Handling several file upload requests at a time?
I am developing a django application which handles lots of file uploads from multiple clients periodically. Each file is around 1 to 10 megabytes.

Since uploads are thread blocking I can only serve a number of requests equivalent to the number of uwsgi workers/processes (4 in my case).

What should I do to increase throughput?

Is it advisable to increase number of processes/workers in uwsgi?

What should be the upper limit?

Is there any other solution that I can use in this scenario?

Stack: django+uwsgi+nginx running on amazon ec2 and s3 buckets used for storing zip files.

答:
What should be the upper limit?

That depends on your hardware such as quantity of core in your server

Is there any other solution that I can use in this scenario?

Consider using Celery/rabbitMQ. Celery could be used to process asynchronously file or upload to S3 and notify the events to rabbitMQ


问:
Regex to Extract Update Columns from a Sql Statement
I need a Regex Statement (run in c#) that will take a string containing a Sql Update statement as input, and will return a list of columns to be updated. It should be able to handle columns surrounded by brackets or not.

```<code>// Example Sql Statement
Update Employees
Set FirstName = 'Jim', [LastName] = 'Smith', CodeNum = codes.Num
From Employees as em
Join CodeNumbers as codes on codes.EmployeeID = em.EmployeeID
</code>```

In the end I would want to return an IEnumerable or List containing:

FirstName

LastName

CodeNum

Anyone have any good suggestions on implementation?

Update: The sql is user-generated, so I have to parse the Sql as it is given. The purpose of extracting the column names in my case is to validate that the user has permission to update the columns included in the query.

答:
You're doing it backwards.  Store the data in a broken out form, with the table to be updated, the column names, and the expressions to generate the new values all separate.  From this canonical representation, generate both the SQL (when you need it) and the list of columns being updated (when you need that instead).

If you absolutely must pull the column names out of a SQL statement, I don't think that regular expressions are the correct way to go.  For example, in the general case you may need to skip over new value expressions that contain arbitrarily nested parenthesis.  You will probably want a full SQL parser.  The book Lex & Yacc by Levine, Mason, and Brown has a chapter on parsing SQL.

Response to update:
You are in for a world of hurt.  The only way to do what you want is to fully parse the SQL, because you also need to make sure that you don't have any subexpressions that perform unauthorized actions.

I very, very strongly recommend that you come up with another way to do whatever it is that you are doing.  Maybe break out the modifiable fields into a separate table and use access controls?  Maybe come up with another interface for them to use in specifying what they want done?  Whatever it is that you're doing, there is almost certainly a better way to do it.  Down that path there be dragons.

OwnerDisplayName: Yaakov Ellis

Body:
I need a Regex Statement (run in c#) that will take a string containing a Sql Update statement as input, and will return a list of columns to be updated. It should be able to handle columns surrounded by brackets or not.

// Example Sql Statement
Update Employees
Set FirstName = 'Jim', [LastName] = 'Smith', CodeNum = codes.Num
From Employees as em
Join CodeNumbers as codes on codes.EmployeeID = em.EmployeeID

In the end I would want to return an IEnumerable or List containing:

FirstName

LastName

CodeNum

Anyone have any good suggestions on implementation?

Update: The sql is user-generated, so I have to parse the Sql as it is given. The purpose of extracting the column names in my case is to validate that the user has permission to update the columns included in the query.

LastEditorDisplayName: Yaakov Ellis

OwnerDisplayName: Glomek

LastEditorDisplayName: Glomek


问:
iPhone: Using Alerts to Help Debugging
I've been building a rather complex system and there's come the time now where I want more concise debugging. I would like to display the contents of a variable (for this example an NSString called v_string) in a notification window (the kind of window that appear when you receive an SMS text).

Is there an easy way to just call an alert with a variable?

Thanks in Advance,

Dan

答:
```<code>UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"My Debug String" message:v_string delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[message show];
[message release];
</code>```

I think this way you can see what you want.
But, as zoul said, why not to use NSLog(@"my var: %@", v_string); ?

Hope that it helps.

Title: iPhone: Using Alerts to Help Debugging

Body:
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"My Debug String" message:v_string delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[message show];
[message release];

I think this way you can see what you want.
But, as zoul said, why not to use NSLog(@"my var: %@", v_string); ?

Hope that it helps.


问:
Spark-version-info.properties not found in jenkins
I am developing a plugin which uses spark-core lib. it is ok when I run it as java application but when I run the plugin in Jenkins it show an error which says that

```<code>    java.lang.ExceptionInInitializerError
    at org.apache.spark.package$.<init>(package.scala:91)
    at org.apache.spark.package$.<clinit>(package.scala)
    at org.apache.spark.SparkContext$$anonfun$3.apply(SparkContext.scala:185)
    at org.apache.spark.SparkContext$$anonfun$3.apply(SparkContext.scala:185)
    at org.apache.spark.internal.Logging$class.logInfo(Logging.scala:54)
    at org.apache.spark.SparkContext.logInfo(SparkContext.scala:74)
    at org.apache.spark.SparkContext.<init>(SparkContext.scala:185)
    at org.apache.spark.SparkContext$.getOrCreate(SparkContext.scala:2275)
    at org.apache.spark.sql.SparkSession$Builder$$anonfun$8.apply(SparkSession.scala:831)
    at org.apache.spark.sql.SparkSession$Builder$$anonfun$8.apply(SparkSession.scala:823)
    at scala.Option.getOrElse(Option.scala:121)
    at org.apache.spark.sql.SparkSession$Builder.getOrCreate(SparkSession.scala:823)
    at com.plugin.goettingen_plugin.HelloWorldBuilder.perform(HelloWorldBuilder.java:88)
    at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:75)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:785)
    at hudson.model.Build$BuildExecution.build(Build.java:205)
    at hudson.model.Build$BuildExecution.doRun(Build.java:162)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:537)
    at hudson.model.Run.execute(Run.java:1741)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:408)
Caused by: org.apache.spark.SparkException: Error while locating file spark-version-info.properties
    at org.apache.spark.package$SparkBuildInfo$.liftedTree1$1(package.scala:75)
    at org.apache.spark.package$SparkBuildInfo$.<init>(package.scala:61)
    at org.apache.spark.package$SparkBuildInfo$.<clinit>(package.scala)
    ... 23 more
Caused by: java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at org.apache.spark.package$SparkBuildInfo$.liftedTree1$1(package.scala:64)
    ... 25 more
</code>```

I am using the following code to start a spark session:

```<code>SparkSession sparkSession = SparkSession.builder().appName("DP-App").master("local[2]").getOrCreate();
</code>```

The spark-core lib searches the file by a class named as package.java by the following code which returns null:

```<code>InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("spark-version-info.properties");
</code>```

Since the spark-version-info.properties was present inside spark-core lib, I tried to shift the file to Web-INF but still it failed to load the file.
Is there any alternative way to load the file and bypass the above code which is inside the library?

My dependencies are:

```<code>  <dependencies>
    <dependency>
      <groupId>org.jenkins-ci.plugins</groupId>
      <artifactId>credentials</artifactId>
      <version>1.9.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-mllib_2.11</artifactId>
      <version>2.0.1</version>
</dependency>
  </dependencies>
</code>```

答:
You are missing spark-version-info.properties file.

So just create one under ./core/target/extra-resources

```<code>λ ~/workspace/big_data/spark/ master* ./build/spark-build-info ./core/target/extra-resources 2.1.1
λ ~/workspace/big_data/spark/ master* cat ./core/target/extra-resources/spark-version-info.properties
version=2.1.1
user=chanhle
revision=dec9aa3b37c01454065a4d8899859991f43d4c66
branch=master
date=2017-06-07T15:12:48Z
url=https://github.com/apache/spark
</code>```

I am experiencing the same problem too when debugging Spark on IntelliJ.

Body:
I am developing a plugin which uses spark-core lib. it is ok when I run it as java application but when I run the plugin in Jenkins it show an error which says that

    java.lang.ExceptionInInitializerError
    at org.apache.spark.package$.<init>(package.scala:91)
    at org.apache.spark.package$.<clinit>(package.scala)
    at org.apache.spark.SparkContext$$anonfun$3.apply(SparkContext.scala:185)
    at org.apache.spark.SparkContext$$anonfun$3.apply(SparkContext.scala:185)
    at org.apache.spark.internal.Logging$class.logInfo(Logging.scala:54)
    at org.apache.spark.SparkContext.logInfo(SparkContext.scala:74)
    at org.apache.spark.SparkContext.<init>(SparkContext.scala:185)
    at org.apache.spark.SparkContext$.getOrCreate(SparkContext.scala:2275)
    at org.apache.spark.sql.SparkSession$Builder$$anonfun$8.apply(SparkSession.scala:831)
    at org.apache.spark.sql.SparkSession$Builder$$anonfun$8.apply(SparkSession.scala:823)
    at scala.Option.getOrElse(Option.scala:121)
    at org.apache.spark.sql.SparkSession$Builder.getOrCreate(SparkSession.scala:823)
    at com.plugin.goettingen_plugin.HelloWorldBuilder.perform(HelloWorldBuilder.java:88)
    at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:75)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:785)
    at hudson.model.Build$BuildExecution.build(Build.java:205)
    at hudson.model.Build$BuildExecution.doRun(Build.java:162)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:537)
    at hudson.model.Run.execute(Run.java:1741)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:98)
    at hudson.model.Executor.run(Executor.java:408)
Caused by: org.apache.spark.SparkException: Error while locating file spark-version-info.properties
    at org.apache.spark.package$SparkBuildInfo$.liftedTree1$1(package.scala:75)
    at org.apache.spark.package$SparkBuildInfo$.<init>(package.scala:61)
    at org.apache.spark.package$SparkBuildInfo$.<clinit>(package.scala)
    ... 23 more
Caused by: java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at org.apache.spark.package$SparkBuildInfo$.liftedTree1$1(package.scala:64)
    ... 25 more

I am using the following code to start a spark session:

SparkSession sparkSession = SparkSession.builder().appName("DP-App").master("local[2]").getOrCreate();

The spark-core lib searches the file by a class named as package.java by the following code which returns null:

InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("spark-version-info.properties");

Since the spark-version-info.properties was present inside spark-core lib, I tried to shift the file to Web-INF but still it failed to load the file.
Is there any alternative way to load the file and bypass the above code which is inside the library?

My dependencies are:

  <dependencies>
    <dependency>
      <groupId>org.jenkins-ci.plugins</groupId>
      <artifactId>credentials</artifactId>
      <version>1.9.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-mllib_2.11</artifactId>
      <version>2.0.1</version>
</dependency>
  </dependencies>

Body:
You are missing spark-version-info.properties file.

So just create one under ./core/target/extra-resources

λ ~/workspace/big_data/spark/ master* ./build/spark-build-info ./core/target/extra-resources 2.1.1
λ ~/workspace/big_data/spark/ master* cat ./core/target/extra-resources/spark-version-info.properties
version=2.1.1
user=chanhle
revision=dec9aa3b37c01454065a4d8899859991f43d4c66
branch=master
date=2017-06-07T15:12:48Z
url=https://github.com/apache/spark

I am experiencing the same problem too when debugging Spark on IntelliJ.


问:
how to concatenate join values from modal
i ve different scenario compare to rest posts. I ve two tables such as:

subscription

and
category

Now the columns Fruits,Snack,Sauce,lunch in subsscription r the id of category table. for example if Fruits is 1 then from category table that Fruit name is Apple.I want to get the names of those foods whose ids are listed in subscription table from category table column: itemname.

I m working on codeigniter and tried by using joins but i dont see any values.

```<code>$this->db->select('*');
        $this->db->from('subsscription');
        $this->db->join('addschool','addschool.id=subsscription.schoolid');
        $this->db->join('user','user.id=subsscription.studentid');
        $this->db->join('category As u1','u1.id=subsscription.Fruits', 'left OUTER');

         $this->db->join('category As u2','u2.id=subsscription.snack', 'left OUTER');
         // $this->db->where('subsscription.snack >', '0');
        $this->db->join('category As u3','u3.id=subsscription.sauce', 'left OUTER');

         $this->db->join('category As u4','u4.id=subsscription.lunchdrink', 'left OUTER');

        $this->db->join('category As u5','u5.id=subsscription.icypole', 'left OUTER');

        $this->db->join('category As u6','u6.id=subsscription.lunch', 'left OUTER');
        // $this->db->where('subsscription.Fruits','category.id');
         // $this->db->group_by('subsscription.id');

        $query = $this->db->get();

        //echo $this->db->last_query();

        if($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return false;
        }
</code>```

My output requirement is that it should be like: Apple,Strawberry Milk where Apple has row id is 1 and Strawberry Milk has row id is 4.

答:
I can't make it work by doing SELECT('*'). I could be wrong, it happens more often than not, but I don't think you can do that. The following worked for me. If someone else has an answer that actually does just grab everything in all of the tables, please feel free to add it.

I also left the school table out, so you'll have to work it back in. I created the tables with the pertinent information and didn't feel like making that one.

```<code>    $this->db->select('subsscription.id AS sub_id, user.name, u1.itemname AS fruit, u2.itemname AS snack, u3.itemname AS sauce, u4.itemname AS lunchdrink, u5.itemname AS icypole, u5.itemname AS lunch');
    $this->db->from('subsscription');
    $this->db->join('user','user.id=subsscription.studentid', 'JOIN');
    $this->db->join('category As u1','subsscription.Fruits=u1.id', 'LEFT OUTER');
    $this->db->join('category As u2','subsscription.snack=u2.id', 'LEFT OUTER');
    $this->db->join('category As u3','subsscription.sauce=u3.id', 'LEFT OUTER');
    $this->db->join('category As u4','subsscription.lunchdrink=u4.id', 'LEFT OUTER');
    $this->db->join('category As u5','subsscription.icypole=u5.id', 'LEFT OUTER');
    $this->db->join('category As u6','subsscription.lunch=u6.id', 'LEFT OUTER');
    $query = $this->db->get();
    if($query->num_rows() > 0) {
        foreach($query->result() AS $row) {
            print '```' . print_r($row, TRUE) . '```';
        }
    }
</code>```

The output of this was:

```<code>stdClass Object
(
    [sub_id] => 1
    [name] => Betty
    [fruit] => Apple
    [snack] =>
    [sauce] =>
    [lunchdrink] => Strawberry Milk
    [icypole] =>
    [lunch] =>
)
stdClass Object
(
    [sub_id] => 2
    [name] => John
    [fruit] => Banana Bread
    [snack] =>
    [sauce] => Apple
    [lunchdrink] =>
    [icypole] => Strawberry Milk
    [lunch] => Strawberry Milk
)
</code>```

I obviously used random values.

Body:
i ve different scenario compare to rest posts. I ve two tables such as:

subscription

and
category

Now the columns Fruits,Snack,Sauce,lunch in subsscription r the id of category table. for example if Fruits is 1 then from category table that Fruit name is Apple.I want to get the names of those foods whose ids are listed in subscription table from category table column: itemname.

I m working on codeigniter and tried by using joins but i dont see any values.

$this->db->select('*');
        $this->db->from('subsscription');
        $this->db->join('addschool','addschool.id=subsscription.schoolid');
        $this->db->join('user','user.id=subsscription.studentid');
        $this->db->join('category As u1','u1.id=subsscription.Fruits', 'left OUTER');

         $this->db->join('category As u2','u2.id=subsscription.snack', 'left OUTER');
         // $this->db->where('subsscription.snack >', '0');
        $this->db->join('category As u3','u3.id=subsscription.sauce', 'left OUTER');

         $this->db->join('category As u4','u4.id=subsscription.lunchdrink', 'left OUTER');

        $this->db->join('category As u5','u5.id=subsscription.icypole', 'left OUTER');

        $this->db->join('category As u6','u6.id=subsscription.lunch', 'left OUTER');
        // $this->db->where('subsscription.Fruits','category.id');
         // $this->db->group_by('subsscription.id');

        $query = $this->db->get();

        //echo $this->db->last_query();

        if($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return false;
        }

My output requirement is that it should be like: Apple,Strawberry Milk where Apple has row id is 1 and Strawberry Milk has row id is 4.

Body:
I can't make it work by doing SELECT('*'). I could be wrong, it happens more often than not, but I don't think you can do that. The following worked for me. If someone else has an answer that actually does just grab everything in all of the tables, please feel free to add it.

I also left the school table out, so you'll have to work it back in. I created the tables with the pertinent information and didn't feel like making that one.

    $this->db->select('subsscription.id AS sub_id, user.name, u1.itemname AS fruit, u2.itemname AS snack, u3.itemname AS sauce, u4.itemname AS lunchdrink, u5.itemname AS icypole, u5.itemname AS lunch');
    $this->db->from('subsscription');
    $this->db->join('user','user.id=subsscription.studentid', 'JOIN');
    $this->db->join('category As u1','subsscription.Fruits=u1.id', 'LEFT OUTER');
    $this->db->join('category As u2','subsscription.snack=u2.id', 'LEFT OUTER');
    $this->db->join('category As u3','subsscription.sauce=u3.id', 'LEFT OUTER');
    $this->db->join('category As u4','subsscription.lunchdrink=u4.id', 'LEFT OUTER');
    $this->db->join('category As u5','subsscription.icypole=u5.id', 'LEFT OUTER');
    $this->db->join('category As u6','subsscription.lunch=u6.id', 'LEFT OUTER');
    $query = $this->db->get();
    if($query->num_rows() > 0) {
        foreach($query->result() AS $row) {
            print '<pre>' . print_r($row, TRUE) . '</pre>';
        }
    }

The output of this was:

stdClass Object
(
    [sub_id] => 1
    [name] => Betty
    [fruit] => Apple
    [snack] =>
    [sauce] =>
    [lunchdrink] => Strawberry Milk
    [icypole] =>
    [lunch] =>
)
stdClass Object
(
    [sub_id] => 2
    [name] => John
    [fruit] => Banana Bread
    [snack] =>
    [sauce] => Apple
    [lunchdrink] =>
    [icypole] => Strawberry Milk
    [lunch] => Strawberry Milk
)

I obviously used random values.


问:
Committed a file, but there is no change in a repository
There is a repository with tex. files. I made some changes in one of them on my computer and committed it. So, now repository got a new version number, everything is ticked green on my computer, and I cannot commit anything else, since there are no changes. The problem is that although the file is updated on my machine, it is not updated in the repository. What should I do?</p>

答:
Using Git:

You need to push your changes to the remote branch. Currently you just committed to your local version.

```<code>git push -u origin master
</code>```

The -u tag just makes it so it remembers what branch you are pushing to. So next time you can just type push

Using SVN:

When you commit, SVN should do this automatically.

Body:
Using Git:

You need to push your changes to the remote branch. Currently you just committed to your local version.

git push -u origin master

The -u tag just makes it so it remembers what branch you are pushing to. So next time you can just type push

Using SVN:

When you commit, SVN should do this automatically.


问:
getSupportFragmentManager().findFragmentById() returns null
I am trying to implement fragment communication in android like the one in the android guide http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

but my application is crashing as the  getSupportFragmentManager().findFragmentById() returns null. What is the issue with my implementation.

The code is given below:

The program is just to send an input text from one fragment to another fragment textView area through a button click from first fragmnet.I have an activity_main.xml and two fragment layout (two separate xml files rather than part of  in activity_main.xml)

Frag1.java

```<code>public class Frag1 extends Fragment {
public Frag1(){

}

buttonClickListener buttonListener;

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
    buttonListener = (buttonClickListener) getActivity();
} catch (ClassCastException e) {
    throw new ClassCastException(activity.toString() + " must implement OnButtonPressListener");
}
}

View myFragmentView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    myFragmentView = inflater.inflate(R.layout.frag1, container, false);

    //SetValue Button
    Button setValueButton = (Button) myFragmentView.findViewById(R.id.setValueButton);
    setValueButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            buttonListener.onButtonPressed("Message received");
        }
    });

    return myFragmentView;
}
</code>```

}

Frag2.java

```<code>public class Frag2 extends Fragment {

View myFragmentView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
 myFragmentView = inflater.inflate(R.layout.frag2, container, false);
    return myFragmentView;
}

void setMessage(String msg){
    TextView txt=(TextView)myFragmentView.findViewById(R.id.textView1);
    txt.setText(msg);
}
}
</code>```

buttonClickListener.java

```<code>public interface buttonClickListener {
public void onButtonPressed(String msg);
}
</code>```

MainActivity.java

```<code>public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener, buttonClickListener {
SectionsPagerAdapter mSectionsPagerAdapter;
@Override
public void onButtonPressed(String msg) {
    // TODO Auto-generated method stub
    Frag2 fragmentObj=(Frag2) getSupportFragmentManager().findFragmentById(R.layout.frag2);

    fragmentObj.setMessage(msg);
}
</code>```

Please tell me where did I go wrong?

EDIT:
I am using fragment creation using the template generated by Android Plug-in eclipse IDE.
So the fragments are created using android.support.v4.app.Fragment

```<code>@Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch(position)
        {
        case 0:
            return new Frag1();
        case 1:
            return new Frag2();
        }
        return fragment;
    }
</code>```

The codebase is kept here for reference
https://skydrive.live.com/redir?resid=D37E0F56FEC9B499!259

答:
I used this to get a child fragment from a fragment adapter...</p>

```<code>Fragment parent = MainActivity.this.getSupportFragmentManager().findFragmentById(R.id.main_container);
        if (parent instanceof PagerFragment) {
            // do something with parent
            Fragment child = ((PagerFragment) f).adapter.getItem(childPosition);
        }
</code>```

Body:
I am trying to implement fragment communication in android like the one in the android guide http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

but my application is crashing as the  getSupportFragmentManager().findFragmentById() returns null. What is the issue with my implementation.

The code is given below:

The program is just to send an input text from one fragment to another fragment textView area through a button click from first fragmnet.I have an activity_main.xml and two fragment layout (two separate xml files rather than part of  in activity_main.xml)

Frag1.java

public class Frag1 extends Fragment {
public Frag1(){

}

buttonClickListener buttonListener;

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
    buttonListener = (buttonClickListener) getActivity();
} catch (ClassCastException e) {
    throw new ClassCastException(activity.toString() + " must implement OnButtonPressListener");
}
}

View myFragmentView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    myFragmentView = inflater.inflate(R.layout.frag1, container, false);

    //SetValue Button
    Button setValueButton = (Button) myFragmentView.findViewById(R.id.setValueButton);
    setValueButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            buttonListener.onButtonPressed("Message received");
        }
    });

    return myFragmentView;
}

}

Frag2.java

public class Frag2 extends Fragment {

View myFragmentView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
 myFragmentView = inflater.inflate(R.layout.frag2, container, false);
    return myFragmentView;
}

void setMessage(String msg){
    TextView txt=(TextView)myFragmentView.findViewById(R.id.textView1);
    txt.setText(msg);
}
}

buttonClickListener.java

public interface buttonClickListener {
public void onButtonPressed(String msg);
}

MainActivity.java

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener, buttonClickListener {
SectionsPagerAdapter mSectionsPagerAdapter;
@Override
public void onButtonPressed(String msg) {
    // TODO Auto-generated method stub
    Frag2 fragmentObj=(Frag2) getSupportFragmentManager().findFragmentById(R.layout.frag2);

    fragmentObj.setMessage(msg);
}

Please tell me where did I go wrong?

EDIT:
I am using fragment creation using the template generated by Android Plug-in eclipse IDE.
So the fragments are created using android.support.v4.app.Fragment

@Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch(position)
        {
        case 0:
            return new Frag1();
        case 1:
            return new Frag2();
        }
        return fragment;
    }

The codebase is kept here for reference
https://skydrive.live.com/redir?resid=D37E0F56FEC9B499!259

Body:
I used this to get a child fragment from a fragment adapter...

Fragment parent = MainActivity.this.getSupportFragmentManager().findFragmentById(R.id.main_container);
        if (parent instanceof PagerFragment) {
            // do something with parent
            Fragment child = ((PagerFragment) f).adapter.getItem(childPosition);
        }