问:
Custom pandas groupby on a list of intervals
I have a dataframe df:

```<code>     A    B
0   28  abc
1   29  def
2   30  hij
3   31  hij
4   32  abc
5   28  abc
6   28  abc
7   29  def
8   30  hij
9   28  abc
10  29  klm
11  30  nop
12  28  abc
13  29  xyz

df.dtypes

A    object        # A is a string column as well
B    object
dtype: object
</code>```

I want to use the values from this list to groupby:

```<code>i = np.array([ 3,  5,  6,  9, 12, 14])
</code>```

Basically, all rows in df with index 0, 1, 2 are in the first group, rows with index 3, 4 are in the second group, rows with index 5 are in the third group, and so on.

My end goal is this:

```<code>A              B
28,29,30       abc,def,hij
31,32          hij,abc
28             abc
28,29,30       abc,def,hij
28,29,30       abc,klm,nop
28,29          abc,xyz
</code>```

Solution so far using groupby + pd.cut:

```<code>df.groupby(pd.cut(df.index, bins=np.append([0], i)), as_index=False).agg(','.join)

          A            B
0  29,30,31  def,hij,hij
1     32,28      abc,abc
2        28          abc
3  29,30,28  def,hij,abc
4  29,30,28  klm,nop,abc
5        29          xyz
</code>```

The result is incorrect :-(

How can I do this properly?

答:
I think this could be fast ..</p>

```<code>df['G']=0
np.put(df.G,i-1,[1]*len(i))
df.groupby(df.G.iloc[::-1].cumsum())[['A','B']].agg(lambda x: ','.join(x.astype(str))).sort_index(ascending =False)
Out[772]:
          A            B
G
6  28,29,30  abc,def,hij
5     31,32      hij,abc
4        28          abc
3  28,29,30  abc,def,hij
2  28,29,30  abc,klm,nop
1     28,29      abc,xyz
</code>```

Body:
I have a dataframe df:

     A    B
0   28  abc
1   29  def
2   30  hij
3   31  hij
4   32  abc
5   28  abc
6   28  abc
7   29  def
8   30  hij
9   28  abc
10  29  klm
11  30  nop
12  28  abc
13  29  xyz

df.dtypes

A    object        # A is a string column as well
B    object
dtype: object

I want to use the values from this list to groupby:

i = np.array([ 3,  5,  6,  9, 12, 14])

Basically, all rows in df with index 0, 1, 2 are in the first group, rows with index 3, 4 are in the second group, rows with index 5 are in the third group, and so on.

My end goal is this:

A              B
28,29,30       abc,def,hij
31,32          hij,abc
28             abc
28,29,30       abc,def,hij
28,29,30       abc,klm,nop
28,29          abc,xyz

Solution so far using groupby + pd.cut:

df.groupby(pd.cut(df.index, bins=np.append([0], i)), as_index=False).agg(','.join)

          A            B
0  29,30,31  def,hij,hij
1     32,28      abc,abc
2        28          abc
3  29,30,28  def,hij,abc
4  29,30,28  klm,nop,abc
5        29          xyz

The result is incorrect :-(

How can I do this properly?

Body:
I think this could be fast ..

df['G']=0
np.put(df.G,i-1,[1]*len(i))
df.groupby(df.G.iloc[::-1].cumsum())[['A','B']].agg(lambda x: ','.join(x.astype(str))).sort_index(ascending =False)
Out[772]:
          A            B
G
6  28,29,30  abc,def,hij
5     31,32      hij,abc
4        28          abc
3  28,29,30  abc,def,hij
2  28,29,30  abc,klm,nop
1     28,29      abc,xyz


来源: stackexchange

问:
How to combine data according to timestamps?
I have a series of text files containing data with two separate timestamps and would like to find out the sum of all values at a given time. The files may have different amounts of rows, but always three columns: value timestamp1 timestamp2, with entries such as

```<code>6.2 1 4
4.3 2 9
7.2 3 10
</code>```

or

```<code>1.2 2 3
0.3 3 9
0.1 5 12
</code>```

Below is an explanation of how the output is formed:

The timestamps from the two inputs are unified into a vector of unique values (so for the above example {1,2,3}∪{2,3,5} -> {1,2,3,5} or {4,9,10}∪{3,9,12} -> {3,4,9,10,12}).

For every unique timestamp a data point is selected from each of the inputs such that:

If the queried timestamp is lower than the smallest available one, the first data value is taken.

Otherwise, the data value having a lower or equal timestamp is taken.

The two values are summed and the next unique timestamp is processed (if available).

If I apply this algorithm on the example data above using timestamp1, I would get:

```<code>7.4 1  %6.2+1.2
5.5 2  %4.3+0.3
7.5 3  %7.2+0.3
7.3 5  %7.2+0.1
</code>```

and for timestamp2:

```<code>7.4 3  %6.2+1.2
7.4 4  %6.2+1.2
4.6 9  %4.3+0.3
7.5 10 %7.2+0.3
7.3 12 %7.2+0.1
</code>```

I think I need to do something with time series so for that I already have the following converter code:

```<code>logs = dir('log1/*.txt');
k=1
for log = logs'

    t{k}=timeseries(load(log.name))
    k=k+1
end
</code>```

I guess the next step would be something like sum(t) but that doesn't work. Does anybody know how to combine them like the above?

For anyone interested these are cpu and realtime timestamps (since algorithm start), for measuring the performance of an algorithm.

答:
As I see it, the two different timestamps is a red herring - you could define your problem for one timestamp, ignoring the other.

As I understand it, you want to:

consider all the times which appear in both data sets (here, consider just the timestamp1, is [1,2,3,5])

interpolate/extrapolate using the nearest neighbour for any missing data points in both lists: (5 is missing from the first dataset, 1 is missing from the second dataset)

Return the sum of the values with the missing points filled.

Without the reading operations, this is how I see your processing:

```<code>times1 = [1,2,3];
values1 = [6.2, 4.3, 7.2];

times2 = [2, 3, 5];
values2 = [1.2, 0.3, 0.1];

all_times = union(times1, times2)';
values1_interp = interp1(times1, values1, all_times, 'nearest', 'extrap');
values2_interp = interp1(times2, values2, all_times, 'nearest', 'extrap');
v_sum = values1_interp + values2_interp;
</code>```

Can see the results:

```<code>>> table(v_sum, all_times)

ans =

    v_sum    all_times
    _____    _________

    7.4      1
    5.5      2
    7.5      3
    7.3      5
</code>```

If instead we were to use

```<code>times1 = [4, 9, 10];
times2 = [3, 9, 12];
</code>```

then we would get

```<code>>> table(v_sum, all_times)

ans =

    v_sum    all_times
    _____    _________

    7.4       3
    7.4       4
    4.6       9
    7.5      10
    7.3      12
</code>```

Edit: From the OP's comment, we don't quite want nearest neighbour, but instead a most recent neighbour, but we use the first point if we are extrapolating a time before our times have begun (e.g., extrapolating values1 to time 1 when our times1 is [2,3,4]): For this you could use something like

```<code>function [vq] = interp_left(x, v, xq)
%INTERP_LEFT Interpolate to the left-nearest point
% x must be sorted.
vq = nan(size(xq));
for ii = 1:length(xq)
  % Find the index in x nearest to xq, only considering smaller x
  [~,jj] = max(x(x<=xq(ii)));
  % Special case, there are no smaller x; extrapolate using [x(1),v(1)]
  if isempty(jj)
    vq(ii) = v(1);
  else
    vq(ii) = v(jj);
  end % if
end % for
end % function
</code>```

then usage with

```<code>times1 = [1,2,3];
values1 = [6.2, 4.3, 7.2];

times2 = [2, 3, 5];
values2 = [1.2, 0.3, 0.1];

all_times = union(times1, times2)';
values1_interp = interp_left(times1, values1, all_times);
values2_interp = interp_left(times2, values2, all_times);
v_sum = values1_interp + values2_interp;
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I have a series of text files containing data with two separate timestamps and would like to find out the sum of all values at a given time. The files may have different amounts of rows, but always three columns: value timestamp1 timestamp2, with entries such as

6.2 1 4
4.3 2 9
7.2 3 10

or

1.2 2 3
0.3 3 9
0.1 5 12

Below is an explanation of how the output is formed:

The timestamps from the two inputs are unified into a vector of unique values (so for the above example {1,2,3}∪{2,3,5} -> {1,2,3,5} or {4,9,10}∪{3,9,12} -> {3,4,9,10,12}).

For every unique timestamp a data point is selected from each of the inputs such that:

If the queried timestamp is lower than the smallest available one, the first data value is taken.

Otherwise, the data value having a lower or equal timestamp is taken.

The two values are summed and the next unique timestamp is processed (if available).

If I apply this algorithm on the example data above using timestamp1, I would get:

7.4 1  %6.2+1.2
5.5 2  %4.3+0.3
7.5 3  %7.2+0.3
7.3 5  %7.2+0.1

and for timestamp2:

7.4 3  %6.2+1.2
7.4 4  %6.2+1.2
4.6 9  %4.3+0.3
7.5 10 %7.2+0.3
7.3 12 %7.2+0.1

I think I need to do something with time series so for that I already have the following converter code:

logs = dir('log1/*.txt');
k=1
for log = logs'

    t{k}=timeseries(load(log.name))
    k=k+1
end

I guess the next step would be something like sum(t) but that doesn't work. Does anybody know how to combine them like the above?

For anyone interested these are cpu and realtime timestamps (since algorithm start), for measuring the performance of an algorithm.

Body:
As I see it, the two different timestamps is a red herring - you could define your problem for one timestamp, ignoring the other.

As I understand it, you want to:

consider all the times which appear in both data sets (here, consider just the timestamp1, is [1,2,3,5])

interpolate/extrapolate using the nearest neighbour for any missing data points in both lists: (5 is missing from the first dataset, 1 is missing from the second dataset)

Return the sum of the values with the missing points filled.

Without the reading operations, this is how I see your processing:

times1 = [1,2,3];
values1 = [6.2, 4.3, 7.2];

times2 = [2, 3, 5];
values2 = [1.2, 0.3, 0.1];

all_times = union(times1, times2)';
values1_interp = interp1(times1, values1, all_times, 'nearest', 'extrap');
values2_interp = interp1(times2, values2, all_times, 'nearest', 'extrap');
v_sum = values1_interp + values2_interp;

Can see the results:

>> table(v_sum, all_times)

ans =

    v_sum    all_times
    _____    _________

    7.4      1
    5.5      2
    7.5      3
    7.3      5

If instead we were to use

times1 = [4, 9, 10];
times2 = [3, 9, 12];

then we would get

>> table(v_sum, all_times)

ans =

    v_sum    all_times
    _____    _________

    7.4       3
    7.4       4
    4.6       9
    7.5      10
    7.3      12

Edit: From the OP's comment, we don't quite want nearest neighbour, but instead a most recent neighbour, but we use the first point if we are extrapolating a time before our times have begun (e.g., extrapolating values1 to time 1 when our times1 is [2,3,4]): For this you could use something like

function [vq] = interp_left(x, v, xq)
%INTERP_LEFT Interpolate to the left-nearest point
% x must be sorted.
vq = nan(size(xq));
for ii = 1:length(xq)
  % Find the index in x nearest to xq, only considering smaller x
  [~,jj] = max(x(x<=xq(ii)));
  % Special case, there are no smaller x; extrapolate using [x(1),v(1)]
  if isempty(jj)
    vq(ii) = v(1);
  else
    vq(ii) = v(jj);
  end % if
end % for
end % function

then usage with

times1 = [1,2,3];
values1 = [6.2, 4.3, 7.2];

times2 = [2, 3, 5];
values2 = [1.2, 0.3, 0.1];

all_times = union(times1, times2)';
values1_interp = interp_left(times1, values1, all_times);
values2_interp = interp_left(times2, values2, all_times);
v_sum = values1_interp + values2_interp;


问:
Calculate how many clients ordered less than one month after their first, second, ... order
Let's start with my tables (simplified for my question) (using MySQL):

```<code>client :
+----------+----------------------+
| idClient |    insctiptionDate   |
+----------+----------------------+
|     1    | 2017-07-31 14:51:22  |
+----------+----------------------+
|     2    | 2017-07-18 08:25:36  |
+----------+----------------------+

delivery :
+----------+----------------------+
| fkClient |         date         |
+----------+----------------------+
|     1    | 2017-07-31 14:51:22  |
+----------+----------------------+
|     1    | 2017-08-10 13:30:22  |
+----------+----------------------+
|     2    | 2017-07-18 08:25:36  |
+----------+----------------------+
</code>```

The inscriptionDate column is equal to the date of the first delivery for each client.

I need to know how many clients ordered a second time in the 28 firsts days after their first command.

So far, I have this :

```<code>SELECT
  YEARWEEK(tmp.first) AS Week,
  COUNT(tmp.id) AS Total
FROM (
  SELECT
    c.idClient as id,
    (SELECT
        MIN(d.date)
     FROM delivery d
     WHERE c.idClient = d.fkClient
    ) AS first
  FROM client c
  ORDER BY first ASC
  ) AS tmp
WHERE (
  SELECT
    MIN(d.date)
  FROM delivery d
  WHERE tmp.id = d.fkClient
  AND d.date != tmp.first
  AND DATEDIFF(d.date, tmp.first) <= 28
  ) IS NOT NULL
 GROUP BY YEARWEEK(first)
 ORDER BY YEARWEEK(first);
</code>```

I first get the first delivery for all my clients, then I count how many of them ordered a second time in the next 28 days.

My problems are:

I'm not sure this is the most elegant solution or even if this really work so I'm open to any suggestion here

I am limited to only one SELECT to do this (I am using the SQL mode
of Chartio to visualize the results)

I need to expand this to also know how many clients order after their
second delivery, their third, ... and I am really stuck here!

Thank you for your help!

答:
I can't test it but i think you should do something like that :</p>

```<code>SELECT IdClient, SUM(NumberCommand)
FROM
(   SELECT
        SUM(
            CASE
                WHEN
                    @previousId = d.fkClient
                    THEN
                    CASE
                        WHEN @prevDate <>d.date
                            AND DATEDIFF(d.date,@prevDate)<=28
                        THEN 1
                        ELSE 0
                    END
                ELSE
                    1
        ) AS NumberCommand,

        (@previousId := d.fkClient) as IdClient,
        (@prevDate := d.date) as DateDelivery
    FROM
    delivery d,
    INNER JOIN (SELECT @prevDate := inscriptionDate, idClient FROM client) b
        ON b.idClient = d.fkClient
    CROSS JOIN (SELECT @previousId:=0) a
    GROUP BY IdClient, DateDelivery
    ORDER BY IdClient, d.date) C
GROUP BY IdClient
</code>```

Body:
Let's start with my tables (simplified for my question) (using MySQL):

client :
+----------+----------------------+
| idClient |    insctiptionDate   |
+----------+----------------------+
|     1    | 2017-07-31 14:51:22  |
+----------+----------------------+
|     2    | 2017-07-18 08:25:36  |
+----------+----------------------+

delivery :
+----------+----------------------+
| fkClient |         date         |
+----------+----------------------+
|     1    | 2017-07-31 14:51:22  |
+----------+----------------------+
|     1    | 2017-08-10 13:30:22  |
+----------+----------------------+
|     2    | 2017-07-18 08:25:36  |
+----------+----------------------+

The inscriptionDate column is equal to the date of the first delivery for each client.

I need to know how many clients ordered a second time in the 28 firsts days after their first command.

So far, I have this :

SELECT
  YEARWEEK(tmp.first) AS Week,
  COUNT(tmp.id) AS Total
FROM (
  SELECT
    c.idClient as id,
    (SELECT
        MIN(d.date)
     FROM delivery d
     WHERE c.idClient = d.fkClient
    ) AS first
  FROM client c
  ORDER BY first ASC
  ) AS tmp
WHERE (
  SELECT
    MIN(d.date)
  FROM delivery d
  WHERE tmp.id = d.fkClient
  AND d.date != tmp.first
  AND DATEDIFF(d.date, tmp.first) <= 28
  ) IS NOT NULL
 GROUP BY YEARWEEK(first)
 ORDER BY YEARWEEK(first);

I first get the first delivery for all my clients, then I count how many of them ordered a second time in the next 28 days.

My problems are:

I'm not sure this is the most elegant solution or even if this really work so I'm open to any suggestion here

I am limited to only one SELECT to do this (I am using the SQL mode
of Chartio to visualize the results)

I need to expand this to also know how many clients order after their
second delivery, their third, ... and I am really stuck here!

Thank you for your help!

Body:
I can't test it but i think you should do something like that :

SELECT IdClient, SUM(NumberCommand)
FROM
(   SELECT
        SUM(
            CASE
                WHEN
                    @previousId = d.fkClient
                    THEN
                    CASE
                        WHEN @prevDate <>d.date
                            AND DATEDIFF(d.date,@prevDate)<=28
                        THEN 1
                        ELSE 0
                    END
                ELSE
                    1
        ) AS NumberCommand,

        (@previousId := d.fkClient) as IdClient,
        (@prevDate := d.date) as DateDelivery
    FROM
    delivery d,
    INNER JOIN (SELECT @prevDate := inscriptionDate, idClient FROM client) b
        ON b.idClient = d.fkClient
    CROSS JOIN (SELECT @previousId:=0) a
    GROUP BY IdClient, DateDelivery
    ORDER BY IdClient, d.date) C
GROUP BY IdClient


问:
How To Remove Multiple Spaces from String jQuery
I have an excel spreadsheet that has 3 columns.

```<code>    1st        |    2nd    |    3rd
----------------------------------------
  xxxxxxxx           x        xxxxxxxx
</code>```

When I copy all 3 of those cells and paste them into my textbox I get a value that looks likes this:

I need to eliminate the spaces, but what I have researched isn't working.

Here is what I have:

```<code>$(document).ready(function() {
    $("#MyTextBox").blur(function() {
        var myValue = $(this).val();
        alert(myValue);
        var test = myValue.replace(' ', '');
        alert(test);
        $("#MyTextBox").val(test);

    });
});
</code>```

When I alert test it looks the exact same as the original and the value for MyTextBox isn't being replaced.

I have a JSFiddle where I'm trying to replicate the issue, but in this instance, only the 1st space is replaced but the new value is populating into the textbox.

What am I doing wrong?

Any help is appreciated.

答:
Using a regular expression would replace any number of spaces.</p>

```<code>$(document).ready(function() {
    var str = 'Your string';
    var stringWithoutSpace = str.replace(/\s/g, '')
});
</code>```

Body:
I have an excel spreadsheet that has 3 columns.

    1st        |    2nd    |    3rd
----------------------------------------
  xxxxxxxx           x        xxxxxxxx

When I copy all 3 of those cells and paste them into my textbox I get a value that looks likes this:

I need to eliminate the spaces, but what I have researched isn't working.

Here is what I have:

$(document).ready(function() {
    $("#MyTextBox").blur(function() {
        var myValue = $(this).val();
        alert(myValue);
        var test = myValue.replace(' ', '');
        alert(test);
        $("#MyTextBox").val(test);

    });
});

When I alert test it looks the exact same as the original and the value for MyTextBox isn't being replaced.

I have a JSFiddle where I'm trying to replicate the issue, but in this instance, only the 1st space is replaced but the new value is populating into the textbox.

What am I doing wrong?

Any help is appreciated.

Body:
Using a regular expression would replace any number of spaces.

$(document).ready(function() {
    var str = 'Your string';
    var stringWithoutSpace = str.replace(/\s/g, '')
});


问:
Spring Mongo > How to get list AggregationOperations from Aggregation
I have a function that receive an Aggregation aggregation as a param.

I would like to get all AggregationOperation from aggregation. Is there any way to do it?

```<code>public Aggregation newCustomAggregation(Aggregation aggregation, Criteria c) {
    // How to get list operation aggregation there?
    listOperation.push(Aggregation.match(c));
    return Aggregation
            .newAggregation(listOperations);
}
</code>```

My purpose is new another Aggregation with my custom MatchAggregation.

答:
You can create your own custom aggregation implementation by subclassing the aggregation to access the protected operations field.

Something like

```<code>public class CustomAggregation extends Aggregation {
      List<AggregationOperation> getAggregationOperations() {
      return operations;
   }
}

public Aggregation newCustomAggregation(Aggregation aggregation, Criteria c) {
     CustomAggregation customAggregation = (CustomAggregation) aggregation;
     List<AggregationOperation> listOperations = customAggregation.getAggregationOperations();
     listOperations.add(Aggregation.match(c));
     return Aggregation .newAggregation(listOperations);
 }
</code>```

Body:
I have a function that receive an Aggregation aggregation as a param.

I would like to get all AggregationOperation from aggregation. Is there any way to do it?

public Aggregation newCustomAggregation(Aggregation aggregation, Criteria c) {
    // How to get list operation aggregation there?
    listOperation.push(Aggregation.match(c));
    return Aggregation
            .newAggregation(listOperations);
}

My purpose is new another Aggregation with my custom MatchAggregation.

Body:
You can create your own custom aggregation implementation by subclassing the aggregation to access the protected operations field.

Something like

public class CustomAggregation extends Aggregation {
      List<AggregationOperation> getAggregationOperations() {
      return operations;
   }
}

public Aggregation newCustomAggregation(Aggregation aggregation, Criteria c) {
     CustomAggregation customAggregation = (CustomAggregation) aggregation;
     List<AggregationOperation> listOperations = customAggregation.getAggregationOperations();
     listOperations.add(Aggregation.match(c));
     return Aggregation .newAggregation(listOperations);
 }


问:
golang http allow certain domain name both with www and without
I have a golang api I'm writting. I use the following function for cors

```<code>func ResponseWithJSON(w http.ResponseWriter, json []byte, code int) {
    w.Header().Set("Content-Type", "application/json; charset=utf-8")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.WriteHeader(code)
    w.Write(json)
}
</code>```

This allows my api to be accessible by anyone. I would like to limit it to my domain name. Because that sounds more secure. Lets call it www.example.com

I can Change it to

```<code> w.Header().Set("Access-Control-Allow-Origin", "http://www.example.com")
</code>```

And this will allow me to make calls when the url is www.example.com but not example.com

I can then change it to

```<code>w.Header().Set("Access-Control-Allow-Origin", "http://example.com")
</code>```

Now I can access my api from example.com but not www.example.com

Adding both does not work
Neither this way

```<code>w.Header().Set("Access-Control-Allow-Origin", "http://www.example.com,http://example.com")
</code>```

Nor This way

```<code>w.Header().Set("Access-Control-Allow-Origin", "http://www.example.com")
w.Header().Set("Access-Control-Allow-Origin", "http://example.com")
</code>```

So, is there a way for me to get the requesting origin? so I can dynamically allow the domain? Is there another way for me to solve this problem?

答:
I found that the Origin information is in the http.Request object. You can get the origin  with

```<code>origin := r.Header.Get("Origin");
</code>```

Assuming you have a object some where like

```<code>r *http.Request
</code>```

If the object is coming from example.com it will return example.com, likewise www.example.com. You can then test if it is one of these two values as a way to authenticate.

Body:
I have a golang api I'm writting. I use the following function for cors

func ResponseWithJSON(w http.ResponseWriter, json []byte, code int) {
    w.Header().Set("Content-Type", "application/json; charset=utf-8")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.WriteHeader(code)
    w.Write(json)
}

This allows my api to be accessible by anyone. I would like to limit it to my domain name. Because that sounds more secure. Lets call it www.example.com

I can Change it to

 w.Header().Set("Access-Control-Allow-Origin", "http://www.example.com")

And this will allow me to make calls when the url is www.example.com but not example.com

I can then change it to

w.Header().Set("Access-Control-Allow-Origin", "http://example.com")

Now I can access my api from example.com but not www.example.com

Adding both does not work
Neither this way

w.Header().Set("Access-Control-Allow-Origin", "http://www.example.com,http://example.com")

Nor This way

w.Header().Set("Access-Control-Allow-Origin", "http://www.example.com")
w.Header().Set("Access-Control-Allow-Origin", "http://example.com")

So, is there a way for me to get the requesting origin? so I can dynamically allow the domain? Is there another way for me to solve this problem?

Body:
I found that the Origin information is in the http.Request object. You can get the origin  with

origin := r.Header.Get("Origin");

Assuming you have a object some where like

r *http.Request

If the object is coming from example.com it will return example.com, likewise www.example.com. You can then test if it is one of these two values as a way to authenticate.


问:
Rowmeans for N sequential columns
In a dataframe with n*3 columns, how to calculate row means for every three (or any other number) sequential columns, e.g. for columns (1,2,3), (4,5,6), (7,8,9), etc?

There is a solution here on Code Review-Mean of many subsets of a dataframe, but I wonder if there is a more elegant method.

答:
You can use the following. This example is based on the data-set provided in the linked question and for every two consecutive columns.</p>

```<code>dat <- data.frame(a1 = 9:11, a2 = 2:4, b1 = 3:5,
              b2 = 4:6, c1 = 5:7, c2 = 1:3)

n <- 2
t(rowsum(t(dat), as.integer(gl(ncol(dat), n, ncol(dat))))) / n

##        1   2 3
## [1,] 5.5 3.5 3
## [2,] 6.5 4.5 4
## [3,] 7.5 5.5 5
</code>```

ContentLicense: CC BY-SA 4.0

Title: Rowmeans for N sequential columns

Body:
You can use the following. This example is based on the data-set provided in the linked question and for every two consecutive columns.

dat <- data.frame(a1 = 9:11, a2 = 2:4, b1 = 3:5,
              b2 = 4:6, c1 = 5:7, c2 = 1:3)

n <- 2
t(rowsum(t(dat), as.integer(gl(ncol(dat), n, ncol(dat))))) / n

##        1   2 3
## [1,] 5.5 3.5 3
## [2,] 6.5 4.5 4
## [3,] 7.5 5.5 5


问:
Convert pandas concat of dataframes to multiindex
Context

So I'm iterating through a bunch of files where each file is a subject, and in each file there are 3 columns, each representing the x,y,z axis at a certain point (the lengths across files are not the same). I want to put all of them into a multi-index PD df.

What I've tried

I found this post and when I do it, it seems to work

```<code>d_ = dict()
DATA_ROOT = "../sample_data/chest_mounted/"
cutoff_min = 0
for fileName in os.listdir(DATA_ROOT):
    if ".csv" in fileName and '.swp' not in fileName:
        with open(DATA_ROOT + fileName) as f:
            data = np.asarray(list(map(lambda x: x.strip().split(",")[1:-1], f.readlines())), dtype=np.int)
            subj_key = "Subject_" + str(fileName.split(".")[0])
            d_[subj_key] = pd.DataFrame(data, columns=['x_acc', 'y_acc', 'z_acc'])
df = pd.concat(d_.values(), keys=d_.keys())
</code>```

When I do df.head() it looks exactly like what I want (I think?)

```<code>                x_acc   y_acc   z_acc
Subject_1   0   1502    2215    2153
            1   1667    2072    2047
            2   1611    1957    1906
            3   1601    1939    1831
            4   1643    1965    1879
</code>```

The Problem

However, when I try to index by Subject_x I get an error. Instead, I have to first do something like

```<code>df["x_acc"]["Subject_1"]
</code>```

where I access the x_acc first then the Subject_1.

Questions

1) I had the impression that I was creating a multi-index but trying df["x_acc"]["Subject_1"] that does not seem to be the case. How do I transform it to that?

2) Is there any way to change the index so that I access by Subject first?

答:
Use loc for selecting - first by level of MultiIndex and then by column name or xs implemented for simple selections:

```<code>df = df.loc['Subject_1', 'x_acc']
print (df)
0    1502
1    1667
2    1611
3    1601
4    1643
Name: x_acc, dtype: int64

df = df.xs('Subject_1')
print (df)
   x_acc  y_acc  z_acc
0   1502   2215   2153
1   1667   2072   2047
2   1611   1957   1906
3   1601   1939   1831
4   1643   1965   1879
</code>```

And for more complicated selections use slicers:

```<code>idx = pd.IndexSlice

df = df.loc['Subject_1', idx['x_acc','y_acc']]
print (df)
   x_acc  y_acc
0   1502   2215
1   1667   2072
2   1611   1957
3   1601   1939
4   1643   1965
</code>```

Also it seems your code should be simplify by read_csv:

```<code>d_ = dict()
DATA_ROOT = "../sample_data/chest_mounted/"
cutoff_min = 0
for fileName in os.listdir(DATA_ROOT):
    if ".csv" in fileName and '.swp' not in fileName:
        subj_key = "Subject_" + str(fileName.split(".")[0])
        d_[subj_key] = pd.read_csv(fileName,  names=['x_acc', 'y_acc', 'z_acc'])

df = pd.concat(d_)
</code>```

Body:
Context

So I'm iterating through a bunch of files where each file is a subject, and in each file there are 3 columns, each representing the x,y,z axis at a certain point (the lengths across files are not the same). I want to put all of them into a multi-index PD df.

What I've tried

I found this post and when I do it, it seems to work

d_ = dict()
DATA_ROOT = "../sample_data/chest_mounted/"
cutoff_min = 0
for fileName in os.listdir(DATA_ROOT):
    if ".csv" in fileName and '.swp' not in fileName:
        with open(DATA_ROOT + fileName) as f:
            data = np.asarray(list(map(lambda x: x.strip().split(",")[1:-1], f.readlines())), dtype=np.int)
            subj_key = "Subject_" + str(fileName.split(".")[0])
            d_[subj_key] = pd.DataFrame(data, columns=['x_acc', 'y_acc', 'z_acc'])
df = pd.concat(d_.values(), keys=d_.keys())

When I do df.head() it looks exactly like what I want (I think?)

                x_acc   y_acc   z_acc
Subject_1   0   1502    2215    2153
            1   1667    2072    2047
            2   1611    1957    1906
            3   1601    1939    1831
            4   1643    1965    1879

The Problem

However, when I try to index by Subject_x I get an error. Instead, I have to first do something like

df["x_acc"]["Subject_1"]

where I access the x_acc first then the Subject_1.

Questions

1) I had the impression that I was creating a multi-index but trying df["x_acc"]["Subject_1"] that does not seem to be the case. How do I transform it to that?

2) Is there any way to change the index so that I access by Subject first?

Body:
Use loc for selecting - first by level of MultiIndex and then by column name or xs implemented for simple selections:

df = df.loc['Subject_1', 'x_acc']
print (df)
0    1502
1    1667
2    1611
3    1601
4    1643
Name: x_acc, dtype: int64

df = df.xs('Subject_1')
print (df)
   x_acc  y_acc  z_acc
0   1502   2215   2153
1   1667   2072   2047
2   1611   1957   1906
3   1601   1939   1831
4   1643   1965   1879

And for more complicated selections use slicers:

idx = pd.IndexSlice

df = df.loc['Subject_1', idx['x_acc','y_acc']]
print (df)
   x_acc  y_acc
0   1502   2215
1   1667   2072
2   1611   1957
3   1601   1939
4   1643   1965

Also it seems your code should be simplify by read_csv:

d_ = dict()
DATA_ROOT = "../sample_data/chest_mounted/"
cutoff_min = 0
for fileName in os.listdir(DATA_ROOT):
    if ".csv" in fileName and '.swp' not in fileName:
        subj_key = "Subject_" + str(fileName.split(".")[0])
        d_[subj_key] = pd.read_csv(fileName,  names=['x_acc', 'y_acc', 'z_acc'])

df = pd.concat(d_)


问:
C++ static_cast downcast validity
Is this static_cast downcast valid?

```<code>// non-virtual, may be non-trivially copyable
struct Base{
    int m_object;
};

// Derived class have only non-virtual functions
struct A : Base{
    void arggh(){
        std::cout << "Arrghh " << m_object;
    }
};

int main() {
    Base base{190};
    A& a = static_cast<A&>(base);
    a.arggh();
    return 0;
}
</code>```

I mean, CREATE base class, and then cast to derived.

Live

答:
static_cast for performing downcast does not perform any safety checks. Since it's possible for a Base& to be referencing an instance of A, the cast proceeds and since it's NOT actually referencing an A, we enter undefined behavior territory*.

A dynamic_cast on the other hand is safer. It will perform a check and throw an exception in the case of reference casting, or return nullptr in the case of pointer casting†.

However, since your base class lacks any virtual functions, dynamic_cast is impossible, so you'd need to modify it with at least a virtual destructor:

```<code>class Base{
public:
    int m_object;
    virtual ~Base()=default;
};
</code>```

Now if we tried to cast:

```<code>A* a = dynamic_cast<A*>(&base);
if (a)
    a->arggh();
else
    std::cout << "null\n";
</code>```

Our output would be

null

*Relevant standardese can be found in [expr.static.cast]:

[for a cast like static_cast<D&>(b) where b is an instance of a base class for D], If the object of type “cv1 B” is actually a base class subobject of an object of type D, the result refers to the enclosing object of type D. Otherwise, the behavior is undefined.

†Relevant standardese at [expr.dynamic.cast]

The value of a failed cast to pointer type is the null pointer value of the required result type. A failed cast to reference type throws an exception of a type that would match a handler of type
  std::bad_cast

Title: C++ static_cast downcast validity

Body:
Is this static_cast downcast valid?

// non-virtual, may be non-trivially copyable
struct Base{
    int m_object;
};

// Derived class have only non-virtual functions
struct A : Base{
    void arggh(){
        std::cout << "Arrghh " << m_object;
    }
};

int main() {
    Base base{190};
    A& a = static_cast(base);
    a.arggh();
    return 0;
}

I mean, CREATE base class, and then cast to derived.

Live

Body:
static_cast for performing downcast does not perform any safety checks. Since it's possible for a Base& to be referencing an instance of A, the cast proceeds and since it's NOT actually referencing an A, we enter undefined behavior territory*.

A dynamic_cast on the other hand is safer. It will perform a check and throw an exception in the case of reference casting, or return nullptr in the case of pointer casting†.

However, since your base class lacks any virtual functions, dynamic_cast is impossible, so you'd need to modify it with at least a virtual destructor:

class Base{
public:
    int m_object;
    virtual ~Base()=default;
};

Now if we tried to cast:

A* a = dynamic_cast<A*>(&base);
if (a)
    a->arggh();
else
    std::cout << "null\n";

Our output would be

null

*Relevant standardese can be found in [expr.static.cast]:

[for a cast like static_cast<D&>(b) where b is an instance of a base class for D], If the object of type “cv1 B” is actually a base class subobject of an object of type D, the result refers to the enclosing object of type D. Otherwise, the behavior is undefined.

†Relevant standardese at [expr.dynamic.cast]

The value of a failed cast to pointer type is the null pointer value of the required result type. A failed cast to reference type throws an exception of a type that would match a handler of type
  std::bad_cast


问:
Power BI : Monthly averages
I know similar questions have been asked and answered previous to this, but for the life of me, cannot get any of those working with my limited knowledge on Power BI.

I have a table which contains 8 weeks worth of data which for most parts, spans 3 months. I currently calculate the daily average which is the "total" divided by the "total in". However what I need to do, is to display the average based upon the calendar month. Therefore, September will have a difference average to that of October and that of November.

Here's a sample of the data:

```<code>Date,Total In,Total Out,Daily Average,Total,Month

27 September 2017,10773,264,97.61,11037,September
28 September 2017,11198,382,96.70,11580,September
29 September 2017,17753,1122,94.06,18875,September
30 September 2017,9568,649,93.65,10217,September
28 October 2017,11434,938,92.42,12372,October
29 October 2017,1541,60,96.25,1601,October
30 October 2017,918,4,99.57,922,October
31 October 2017,8565,24,99.72,8589,October
01 November 2017,11452,635,94.75,12087,November
02 November 2017,7785,531,93.61,8316,November
</code>```

So for November I would like to have the figure as (11452 + 7785) / (12087 + 8316) * 100 = 94.29%. Obviously, that figure would be present for all dates in November as an extra column. For October it would be (11434 + 1541 + 918 + 8565) / (12372 + 1601 + 922 + 8589) * 100 = 95.63%. I would then use the extra column and plot it on a line/bar chart.

So the above data would become:

```<code>Date,Total In,Total Out,Daily Average,Total,Month,Monthly Average

27 September 2017,10773,264,97.61,11037,September,96.00
28 September 2017,11198,382,96.70,11580,September,96.00
29 September 2017,17753,1122,94.06,18875,September,96.00
30 September 2017,9568,649,93.65,10217,September,96.00
28 October 2017,11434,938,92.42,12372,October,95.63
29 October 2017,1541,60,96.25,1601,October,95.63
30 October 2017,918,4,99.57,922,October,95.63
31 October 2017,8565,24,99.72,8589,October,95.63
01 November 2017,11452,635,94.75,12087,November,94.29
02 November 2017,7785,531,93.61,8316,November,94.29
</code>```

I am having trouble getting my head around the SUMMARIZE functions etc in order to get this working without any help. So any help and explanation would be greatly appreciated.

Thanks.

答:
Create a DAX using the EARLIER function should do the trick:

```<code>Month Average =
VAR sum_total_in =
CALCULATE(
    SUM('table'[Total In]),
    FILTER(
        'table',
        'table'[Month] = EARLIER('table'[Month])
    )
)

VAR sum_total =
CALCULATE(
    SUM('table'[Total]),
    FILTER(
        'table',
        'table'[Month] = EARLIER('table'[Month])
    )
)

RETURN sum_total_in / sum_total
</code>```

So basically it sums the Total In and Total within the same month and returns the division.

Results:

P.S. You'll need to add the year to the filter as well if you have data across years with the same month.

Title: Power BI : Monthly averages

Body:
I know similar questions have been asked and answered previous to this, but for the life of me, cannot get any of those working with my limited knowledge on Power BI.

I have a table which contains 8 weeks worth of data which for most parts, spans 3 months. I currently calculate the daily average which is the "total" divided by the "total in". However what I need to do, is to display the average based upon the calendar month. Therefore, September will have a difference average to that of October and that of November.

Here's a sample of the data:

Date,Total In,Total Out,Daily Average,Total,Month

27 September 2017,10773,264,97.61,11037,September
28 September 2017,11198,382,96.70,11580,September
29 September 2017,17753,1122,94.06,18875,September
30 September 2017,9568,649,93.65,10217,September
28 October 2017,11434,938,92.42,12372,October
29 October 2017,1541,60,96.25,1601,October
30 October 2017,918,4,99.57,922,October
31 October 2017,8565,24,99.72,8589,October
01 November 2017,11452,635,94.75,12087,November
02 November 2017,7785,531,93.61,8316,November

So for November I would like to have the figure as (11452 + 7785) / (12087 + 8316) * 100 = 94.29%. Obviously, that figure would be present for all dates in November as an extra column. For October it would be (11434 + 1541 + 918 + 8565) / (12372 + 1601 + 922 + 8589) * 100 = 95.63%. I would then use the extra column and plot it on a line/bar chart.

So the above data would become:

Date,Total In,Total Out,Daily Average,Total,Month,Monthly Average

27 September 2017,10773,264,97.61,11037,September,96.00
28 September 2017,11198,382,96.70,11580,September,96.00
29 September 2017,17753,1122,94.06,18875,September,96.00
30 September 2017,9568,649,93.65,10217,September,96.00
28 October 2017,11434,938,92.42,12372,October,95.63
29 October 2017,1541,60,96.25,1601,October,95.63
30 October 2017,918,4,99.57,922,October,95.63
31 October 2017,8565,24,99.72,8589,October,95.63
01 November 2017,11452,635,94.75,12087,November,94.29
02 November 2017,7785,531,93.61,8316,November,94.29

I am having trouble getting my head around the SUMMARIZE functions etc in order to get this working without any help. So any help and explanation would be greatly appreciated.

Thanks.

Body:
Create a DAX using the EARLIER function should do the trick:

Month Average =
VAR sum_total_in =
CALCULATE(
    SUM('table'[Total In]),
    FILTER(
        'table',
        'table'[Month] = EARLIER('table'[Month])
    )
)

VAR sum_total =
CALCULATE(
    SUM('table'[Total]),
    FILTER(
        'table',
        'table'[Month] = EARLIER('table'[Month])
    )
)

RETURN sum_total_in / sum_total

So basically it sums the Total In and Total within the same month and returns the division.

Results:

P.S. You'll need to add the year to the filter as well if you have data across years with the same month.


问:
Simple question on how to format a number
my question is the following and I guess is really simple.
I have a number echoed with PHP. For example 37452.
I would like to have a dot and be displayed like 37.452

My var is $numposts

How is this possible ?
Thank you

答:
Take a look at number_format. The signature is:

```<code>string number_format (
    float $number , int $decimals = 0 ,
    string $dec_point = '.' , string $thousands_sep = ','
)
</code>```

Example:

```<code><?php

echo number_format(37452) . "\n";
// 37,452

echo number_format(37452, 0, ',', '.') . "\n";
// 37.452

?>
</code>```

ContentLicense: CC BY-SA 2.5

Body:
Take a look at number_format. The signature is:

string number_format (
    float $number , int $decimals = 0 ,
    string $dec_point = '.' , string $thousands_sep = ','
)

Example:

<?php

echo number_format(37452) . "\n";
// 37,452

echo number_format(37452, 0, ',', '.') . "\n";
// 37.452

?>


问:
Odoo v8 PHP Insert One2many or Many2many field
I am trying in Odoo v8 to create an invoice and its invoice lines using PHP.
However, when creating an invoice line I need to population invoice_line_tax_id which is a many2many field.

I have tried to read this page but I cannot figure it out : https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.write

Here is how I create an invoice line

```<code>$result = $models->execute_kw($db, $uid, $password,
    'account.invoice.line', 'create',
    array(array(
        'invoice_id'=> 15,
        'product_id'=> 2,
        'quantity'=> 1,
        'name'=> 'Abonnement standard' ,
        'price_unit' => 50 ,
        'invoice_line_tax_id' => array( 0 , false , array( 2 ) )
    )));
</code>```

I have the following error in return :

string 'Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/openerp/service/wsgi_server.py", line 75, in xmlrpc_return
    result = openerp.http.dispatch_rpc(service, method, params)
  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 114, in dispatch_rpc
    result = dispatch(method, params)
  File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 37, in dispatch
    res = fn(db, uid, *params)
  File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", li'... (length=2004)

If I just put the invoice_line_tax_id this way :

```<code>'invoice_line_tax_id' => array( 0 , false , 2 )
</code>```

It works but no tax is inserted.

Any idea how to make this work ?

Thanks in advance.

答:
For those (like me) who struggle with odoo 10 :

Assuming your tax_id 1 exist in account_tax, correct syntax is invoice_line_tax_ids (note the final s) :

```<code>   'invoice_line_tax_ids'=> [[4,[1],false]]
</code>```

Body:
I am trying in Odoo v8 to create an invoice and its invoice lines using PHP.
However, when creating an invoice line I need to population invoice_line_tax_id which is a many2many field.

I have tried to read this page but I cannot figure it out : https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.write

Here is how I create an invoice line

$result = $models->execute_kw($db, $uid, $password,
    'account.invoice.line', 'create',
    array(array(
        'invoice_id'=> 15,
        'product_id'=> 2,
        'quantity'=> 1,
        'name'=> 'Abonnement standard' ,
        'price_unit' => 50 ,
        'invoice_line_tax_id' => array( 0 , false , array( 2 ) )
    )));

I have the following error in return :

string 'Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/openerp/service/wsgi_server.py", line 75, in xmlrpc_return
    result = openerp.http.dispatch_rpc(service, method, params)
  File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 114, in dispatch_rpc
    result = dispatch(method, params)
  File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 37, in dispatch
    res = fn(db, uid, *params)
  File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", li'... (length=2004)

If I just put the invoice_line_tax_id this way :

'invoice_line_tax_id' => array( 0 , false , 2 )

It works but no tax is inserted.

Any idea how to make this work ?

Thanks in advance.

Body:
For those (like me) who struggle with odoo 10 :

Assuming your tax_id 1 exist in account_tax, correct syntax is invoice_line_tax_ids (note the final s) :

   'invoice_line_tax_ids'=> [[4,[1],false]]


答:
The Access-Control-Allow-Origin header supports only a single value, so you have to inspect the Host request header and then make a decision based on that:

```<code>package main

import "net/http"

func myHandler(w http.ResponseWriter, r *http.Request) {
    switch host := r.Header.Get("Host"); host {
    case "www.example.com", "example.com":
            w.Header().Set("Access-Control-Allow-Origin", "http://"+host)
    }
}
</code>```

Not that the Host header is sometimes changed by proxy servers. They then usually add an X-Forwarded-Host header or similar. Only inspect that if you trust the proxy, though (and how to establish that trust is a different question).

Title: golang http allow certain domain name both with www and without

Body:
The Access-Control-Allow-Origin header supports only a single value, so you have to inspect the Host request header and then make a decision based on that:

package main

import "net/http"

func myHandler(w http.ResponseWriter, r *http.Request) {
    switch host := r.Header.Get("Host"); host {
    case "www.example.com", "example.com":
            w.Header().Set("Access-Control-Allow-Origin", "http://"+host)
    }
}

Not that the Host header is sometimes changed by proxy servers. They then usually add an X-Forwarded-Host header or similar. Only inspect that if you trust the proxy, though (and how to establish that trust is a different question).


问:
Assert that element from List<webelement> contains some text
I'm not sure how can I assert that I have a list of webelements where each webelement has the text inside, And I want to verify that on this list of webelements exists some text.

I not sure how this should looks assertion in this case.

答:
Simple get stream, map to string (text) and check if any matches:

```<code>boolean anyMatch = webelements.stream()
  .map(WebElement::getText)
  .anyMatch(text -> "inside".equals(text));
</code>```

To be safe filter out null's and trim:

```<code>boolean anyMatch = webelements.stream()
  .map(WebElement::getText)
  .filter(Objects::nonNull)
  .map(String::trim)
  .anyMatch(text -> "inside".equals(text));
</code>```

Body:
Simple get stream, map to string (text) and check if any matches:

boolean anyMatch = webelements.stream()
  .map(WebElement::getText)
  .anyMatch(text -> "inside".equals(text));

To be safe filter out null's and trim:

boolean anyMatch = webelements.stream()
  .map(WebElement::getText)
  .filter(Objects::nonNull)
  .map(String::trim)
  .anyMatch(text -> "inside".equals(text));


问:
nodejs json endpoint from api json stream
I am connecting to an api and pulling a set of json data. the javascript outputs the json as the variable feedData and when i include that in a html  i get the json on a html page as expected. What i want to do is output it as a json endpoint. I tried to get fancy and when i  tried:

```<code>var express = require('express');
var app = express();
app.get('/', function (req, res) {
  res.send(feedData);
});
app.listen(3000, function () {
 console.log('posting data');
});
</code>```

the problem is that i need to import an api.js file and when i attempt to load this i get an error related to the api.js file which is windows is not defined.  Like i said, the html

```<code> document.getElementById('mydiv').innerHTML += JSON.stringify(feedData, undefined,2);
</code>```

then

```<code><pre id="mydiv">```
```

works fine but i will also to recall every x seconds because this is a live json feed.

Currently, i just decided to connect via python, loading it into mongodb and creating a nodejs endpoint from there, which works fine,  but it seems there should be a way here.

答:
Instead of

```<code>res.send(feedData);
</code>```

use

```<code>res.json(feedData);
</code>```

Hope this helps

Body:
I am connecting to an api and pulling a set of json data. the javascript outputs the json as the variable feedData and when i include that in a html  i get the json on a html page as expected. What i want to do is output it as a json endpoint. I tried to get fancy and when i  tried:

var express = require('express');
var app = express();
app.get('/', function (req, res) {
  res.send(feedData);
});
app.listen(3000, function () {
 console.log('posting data');
});

the problem is that i need to import an api.js file and when i attempt to load this i get an error related to the api.js file which is windows is not defined.  Like i said, the html

 document.getElementById('mydiv').innerHTML += JSON.stringify(feedData, undefined,2);

then

works fine but i will also to recall every x seconds because this is a live json feed.

Currently, i just decided to connect via python, loading it into mongodb and creating a nodejs endpoint from there, which works fine,  but it seems there should be a way here.

Body:
Instead of

res.send(feedData);

use

res.json(feedData);

Hope this helps


问:
Please suggest a good configuration for Remote Java Development?
We have a remote Linux box running Ubuntu, which we access using SSH. The box is running apache-2, ajp13 connectors for tomcat-6, mySQL-5 for a relational db, Spring libraries (dbcp) for db-connection pools, etc. The main program is a servlet and associated data/session management systems, together with several applets and communication between those using ObjectIO streams.

The question: What is a good way to manage the development effort for such a project, in this kind of environment? This is what we are doing for now:

o Edit source code with VIM
o Compile at command line with javac
o Build, sign applets and place appropriate jar files with "jar cvf" in shell scripts.
o At times develop small parts locally and port the source code for integration.
o Start up servlet running perl script that feed different input variables to init().

It works so far, but is not a robust, scalable system accessible by multiple developers. Moreover, we lack the capacity of load-testing (not very important at this point, but missing, nontheless). If you have worked with such a remote system, could you tell us about your experience with the different tools please. Thank you. Please note that:

o We may not have sufficient bandwidth to run X-server on Linux and run VNC
o We cannot click on some arrowhead on eclipse - it is a remote system

If there were an Eclipse server running on Linux and a client on local Windows connecting to this server through ssh, that would be ideal, although we'd have to retrain ourselves to use the system. Looking forward to your answers.

答:
If you use the Java EE version of Eclipse (as suggested from your question) then you will find the RSE (Remote System Explorer) perspective useful.

From the developer's perspective, all files on the remote system are viewed like locally through ssh.  That also means editable of course.  You can create your own filters (hint like env variables).

Coupled with shelled, the shell scripts are colourised.  You can cut and paste files from your workspace to the remote machine etc...

Of course, the RSE can connect to several servers (dev, int and prod).

A great time saver IMO.

Adding this note in the answer to put all the info in the same place:

In your case, IIUC, the Linux is the remote machine and the dev stations are presumably Wintel boxes.  The Linux box does not need to run eclipse at all.

Eclipse RSE supports several kinds of "transport" options based on ECF (the Eclipse Communication Framework), ssh and DStore among them.  True DStore is a little bit weird to use but you can do nearly everything with just the ssh protocol.

I do not have any other tutorials than what's at hand on the Eclipse site.

As for build and deploy, we use maven, integrated with Eclipse WTP (nowadays both maven eclipse plugin, q4e and m2, are compatible with the WTP publishing model) on the developer stations and there are basically two options:

Either you develop locally with maven/wtp and copy your target jar/wars from times to times to the Linux box with a simple cut and paste using RSE (in the long run that's a faster approach).

Or you configure maven to deploy your artifacts each time on the Linux box (in the long run that's probably tedious and would need some sync if there is more than one developer).

As for now I did not find (but would be interested in ;-) using the WTP model over RSE/SSH or any other similar solution.


问:
How do I delete bad certificates from Xcode 8?
I'm new to iOS development, and I can't figure out how to delete bad certificates from Xcode 8. I feel like I've searched everywhere, but I can't find a straightforward answer.

Here's what I tried (couple images below):

In Xcode, I go to Preferences and then the Accounts tab, and then I click on my Apple ID login. I have 2 different teams - a personal one, and a company enterprise team.

When I click on my personal account, and go to Manage Certificates, there's an old certificate that uses an incorrect email I used before, and it says status Not in Keychain. But when I right click on it all the options, such as Delete Certificate are grayed out. How can I delete this? I really don't want it there because it seems messy.

Then when I go to my company team, and go to Manage Certificates, there's a certificate that I had accidentally revoked through the Portal that is now named Untitled. And when I right click on it, the Delete Certificate is also grayed out.

How can I get rid of these certificates?

答: I had a couple of Untitled certificates as well and after a lot of digging was able to delete them through the keychain app. When you open keychain, under login / My Certificates I had a few certificates which were either expired or revoked causing them to show up as Untitled in Xcode. I simply deleted the certs in keychain and they then disappeared in Xcode. </p>


问:
Understanding page directories
I read some manuals about page directories and page tables and still very confused about these concepts. For example here I found that making only a single level paging is wasting memory. So in x86 architecture we use 2-level paging.

```<code>cr3 --> PageDirectory --> PageTable --> Page
</code>```

So consider the following linear address.

```<code>01001010101 1010101101 10101110101010
 page tbl     page      offset
</code>```

Using page tbl bits we find the page directory entry physical address. PD = cr3 + L * (page tbl) which contains page table physical address PT. Now we are looking for our page P = PT + L * page. So in single level we had one page table 4MiB. Now we have 1024 page tables 4KiB = 4MiB in total. Don't see memory economy.

What did I miss?

答: Normally, the page table is far from full.  With a flat page table, if only the first few and the last few entries are needed, still the entire table needs to be allocated.  With page directories, all but the first and last page table can be omitted, thus saving a lot of memory.  The gains seem to be small in today's age of vast RAM, but it was really a lot back when computers might only have 4 MiB of RAM in total.</p>

Title: Understanding page directories

Body:
I read some manuals about page directories and page tables and still very confused about these concepts. For example here I found that making only a single level paging is wasting memory. So in x86 architecture we use 2-level paging.

cr3 --> PageDirectory --> PageTable --> Page

So consider the following linear address.

01001010101 1010101101 10101110101010
 page tbl     page      offset

Using page tbl bits we find the page directory entry physical address. PD = cr3 + L * (page tbl) which contains page table physical address PT. Now we are looking for our page P = PT + L * page. So in single level we had one page table 4MiB. Now we have 1024 page tables 4KiB = 4MiB in total. Don't see memory economy.

What did I miss?


问:
Is there a filter for divide for Django Template?
I noticed there is  built-in add filter, but I wasn't able to find divide.

I am new to Django and not sure if there is a such filter.

答:
There is a Python module to do math operations in your templates: Django-Mathfilters.

It contains add as you said, but also div to divide:

```<code> 8 / 3 = {{ 8|div:3 }}
</code>```

Body:
There is a Python module to do math operations in your templates: Django-Mathfilters.

It contains add as you said, but also div to divide:

 8 / 3 = {{ 8|div:3 }}


问:
How can I make continuous animation attached to an svg inside a button on hover smooth?
I am working on a codewars link (a div). I would like the svg inside the div to rotate continuously on hover. The resulting hover effect is far from smooth, the svg jumps when the mouse rolls over it. The mouseout event is commented out because it makes it worth. What is the best way to optimize this? Codepen: https://codepen.io/forTheLoveOfCode/pen/MOvRNb

CSS:

```<code>#codewars-butn{
  position: absolute;
  top: 20%;
  left: 20%;
  background-color: #B21C15;
  border: 2px solid;
  border-radius: 10px;
}
body{
  background-color: #3F2740;
}
.codewars{
  display: inline-block;
  stroke:#000;
  stroke-dasharray: 1 5;
}
</code>```

HTML:

```<code><body>
  <div id="codewars-butn">
    <svg class="codewars" width="100" height="100">
      <defs>
          <path id="curve" d="M50 50 A 20 20 0 1 0 80 70" stroke-width="10" stroke-linecap="round" fill="none"/>
      </defs>
      <use href="#curve"/>
      <use href="#curve" transform="rotate(60, 50, 50)"/>
      <use href="#curve" transform="rotate(120, 50, 50)"/>
      <use href="#curve" transform="rotate(180, 50, 50)"/>
      <use href="#curve" transform="rotate(240, 50, 50)"/>
      <use href="#curve" transform="rotate(300, 50, 50)"/>

      <animateTransform attributeName="transform"
                              attributeType="XML"
                              type="rotate"
                              from="0 0 0"
                              to="360 0 0"
                              dur="5s"
                              begin="mouseover"
<!--                               end="mouseout" -->
                              repeatCount="indefinite"/>
    </svg>
  </div>
</body>
</code>```

答:
Your problem is that you are starting the animation when the mouse is "over" the SVG.  However what's happening is that when the SVG rotates, the paths are passing under the mouse pointer, and so a mouseout event is being fired, then another mouseover once the path has gone past.

The solution is to prevent the <path> elements from triggering mouse events. The way to do that is to add the following to your path.

```<code>pointer-events="none"
</code>```

#codewars-butn{
  position: absolute;
  top: 20%;
  left: 20%;
  background-color: #B21C15;
  border: 2px solid;
  border-radius: 10px;
}
body{
  background-color: #3F2740;
}
.codewars{
  display: inline-block;
  stroke:#000;
  stroke-dasharray: 1 5;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code>  <div id="codewars-butn">
    <svg class="codewars" width="100" height="100">
      <defs>
          <path id="curve" d="M50 50 A 20 20 0 1 0 80 70" stroke-width="10"
                stroke-linecap="round" fill="none" pointer-events="none"/>
      </defs>
      <use href="#curve"/>
      <use href="#curve" transform="rotate(60, 50, 50)"/>
      <use href="#curve" transform="rotate(120, 50, 50)"/>
      <use href="#curve" transform="rotate(180, 50, 50)"/>
      <use href="#curve" transform="rotate(240, 50, 50)"/>
      <use href="#curve" transform="rotate(300, 50, 50)"/>

      <animateTransform attributeName="transform"
                              attributeType="XML"
                              type="rotate"
                              from="0 0 0"
                              to="360 0 0"
                              dur="5s"
                              begin="mouseover"
                              repeatCount="indefinite"/>
    </svg>
  </div></code>```

Body:
I am working on a codewars link (a div). I would like the svg inside the div to rotate continuously on hover. The resulting hover effect is far from smooth, the svg jumps when the mouse rolls over it. The mouseout event is commented out because it makes it worth. What is the best way to optimize this? Codepen: https://codepen.io/forTheLoveOfCode/pen/MOvRNb

CSS:

#codewars-butn{
  position: absolute;
  top: 20%;
  left: 20%;
  background-color: #B21C15;
  border: 2px solid;
  border-radius: 10px;
}
body{
  background-color: #3F2740;
}
.codewars{
  display: inline-block;
  stroke:#000;
  stroke-dasharray: 1 5;
}

HTML:

    <svg class="codewars" width="100" height="100">
      <defs>
          <path id="curve" d="M50 50 A 20 20 0 1 0 80 70" stroke-width="10" stroke-linecap="round" fill="none"/>
      </defs>
      <use href="#curve"/>
      <use href="#curve" transform="rotate(60, 50, 50)"/>
      <use href="#curve" transform="rotate(120, 50, 50)"/>
      <use href="#curve" transform="rotate(180, 50, 50)"/>
      <use href="#curve" transform="rotate(240, 50, 50)"/>
      <use href="#curve" transform="rotate(300, 50, 50)"/>

      <animateTransform attributeName="transform"
                              attributeType="XML"
                              type="rotate"
                              from="0 0 0"
                              to="360 0 0"
                              dur="5s"
                              begin="mouseover"

                              repeatCount="indefinite"/>
    </svg>

Body:
Your problem is that you are starting the animation when the mouse is "over" the SVG.  However what's happening is that when the SVG rotates, the paths are passing under the mouse pointer, and so a mouseout event is being fired, then another mouseover once the path has gone past.

The solution is to prevent the <path> elements from triggering mouse events. The way to do that is to add the following to your path.

pointer-events="none"

#codewars-butn{
  position: absolute;
  top: 20%;
  left: 20%;
  background-color: #B21C15;
  border: 2px solid;
  border-radius: 10px;
}
body{
  background-color: #3F2740;
}
.codewars{
  display: inline-block;
  stroke:#000;
  stroke-dasharray: 1 5;
}

  <div id="codewars-butn">
    <svg class="codewars" width="100" height="100">
      <defs>
          <path id="curve" d="M50 50 A 20 20 0 1 0 80 70" stroke-width="10"
                stroke-linecap="round" fill="none" pointer-events="none"/>
      </defs>
      <use href="#curve"/>
      <use href="#curve" transform="rotate(60, 50, 50)"/>
      <use href="#curve" transform="rotate(120, 50, 50)"/>
      <use href="#curve" transform="rotate(180, 50, 50)"/>
      <use href="#curve" transform="rotate(240, 50, 50)"/>
      <use href="#curve" transform="rotate(300, 50, 50)"/>

      <animateTransform attributeName="transform"
                              attributeType="XML"
                              type="rotate"
                              from="0 0 0"
                              to="360 0 0"
                              dur="5s"
                              begin="mouseover"
                              repeatCount="indefinite"/>
    </svg>
  </div>


问:
UnityContainerExtensions.LoadConfiguration() Error
Below error I am getting while container.LoadConfiguration("")

An unhandled exception of type 'System.ArgumentNullException' occurred in Unity.Configuration.dll

Additional information: Value cannot be null.

Few details:
I am using .NET 4.5.2
Unity 5.2.1

Below is the code

```<code> IUnityContainer container = new UnityContainer();
        UnityConfigurationSection section = new UnityConfigurationSection();
        container.LoadConfiguration("DataAccessProvider");  //Here it is throwing error

        section.Configure(container);

        var dataAccessProvider = container.Resolve<IDataProvider>("SQLDataAccessProvider");
        return dataAccessProvider;
</code>```

App.config as below

Please let me know if you need more details.

答:
I currently have the following configuration working without any issues - .NET 4.5.2 Unity 5.2.1

```<code>var unityContainer = new UnityContainer();
unityContainer.LoadConfiguration("DataAccessProvider");
var dataAccessProvider = unityContainer.Resolve<IDataProvider>("SQLDataAccessProvider");
return dataAccessProvider;
</code>```

Config:

Hope that helps.

Body:
Below error I am getting while container.LoadConfiguration("")

An unhandled exception of type 'System.ArgumentNullException' occurred in Unity.Configuration.dll

Additional information: Value cannot be null.

Few details:
I am using .NET 4.5.2
Unity 5.2.1

Below is the code

 IUnityContainer container = new UnityContainer();
        UnityConfigurationSection section = new UnityConfigurationSection();
        container.LoadConfiguration("DataAccessProvider");  //Here it is throwing error

        section.Configure(container);

        var dataAccessProvider = container.Resolve<IDataProvider>("SQLDataAccessProvider");
        return dataAccessProvider;

App.config as below

Please let me know if you need more details.

Body:
I currently have the following configuration working without any issues - .NET 4.5.2 Unity 5.2.1

var unityContainer = new UnityContainer();
unityContainer.LoadConfiguration("DataAccessProvider");
var dataAccessProvider = unityContainer.Resolve<IDataProvider>("SQLDataAccessProvider");
return dataAccessProvider;

Config:

Hope that helps.


问:
Python Tkinter, destroy toplevel after function
I'm programming some drives with python using Tkinter as GUI. When my machine is running, I'd like to show the user a toplevel window with some information which should close itself after the function completes. This is my minimal example:

```<code>from Tkinter import *
import time

def button_1():
     window = Toplevel()
     window.title("info")
     msg = Message(window, text='running...', width=200)
     msg.pack()
     time.sleep(5.0)
     window.destroy()

master = Tk()
frame = Frame(width=500,height=300)
frame.grid()
button_one = Button(frame, text ="Button 1", command = button_1)
button_one.grid(row = 0, column = 0, sticky = W + E)
mainloop()
</code>```

The main problem is, that the toplevel window just appears after 5 seconds are over. Any suggestions?
Thanks!

答:
A general rule of thumb is that you should never call sleep in the thread that the GUI is running in. The reason is that sleep does exactly what it says, it puts the whole program to sleep. That means that it is unable to refresh the window or react to any events.

If you want to do something after a period of time, the correct way to do that is with after. For example, this will destroy the window after five seconds:

```<code>window.after(5000, window.destroy)
</code>```

Body:
I'm programming some drives with python using Tkinter as GUI. When my machine is running, I'd like to show the user a toplevel window with some information which should close itself after the function completes. This is my minimal example:

from Tkinter import *
import time

def button_1():
     window = Toplevel()
     window.title("info")
     msg = Message(window, text='running...', width=200)
     msg.pack()
     time.sleep(5.0)
     window.destroy()

master = Tk()
frame = Frame(width=500,height=300)
frame.grid()
button_one = Button(frame, text ="Button 1", command = button_1)
button_one.grid(row = 0, column = 0, sticky = W + E)
mainloop()

The main problem is, that the toplevel window just appears after 5 seconds are over. Any suggestions?
Thanks!

Body:
A general rule of thumb is that you should never call sleep in the thread that the GUI is running in. The reason is that sleep does exactly what it says, it puts the whole program to sleep. That means that it is unable to refresh the window or react to any events.

If you want to do something after a period of time, the correct way to do that is with after. For example, this will destroy the window after five seconds:

window.after(5000, window.destroy)


问:
Use paste inside datatables, with a string vector as an input
```<code>library(datasets)
library(dplyr)
library(data.table)

data(iris)
iris <- iris %>% as.data.table()

iris[ , pasted := paste(Species, Petal.Width)]
</code>```

I would like an alternative of the above function so as the inputs of the paste function to be in a string vector. (The reason is that I may rerun the script and the names or numbers of variables in the paste function may differ ever time.

I tried the following

```<code>names <- c('Species', "Petal.Width")
iris[ , pasted := paste(names %>% get())]
</code>```

or with using the .SD

```<code> dt[, pasted:=paste0(.SD), .SDcols = names]
</code>```

but they do not  work.

Any ideas?

答:
If you pass a vector to .SDcols, .SD is a data frame (and therefore list) of those columns. You can't directly paste a data frame usefully, which is why the original code fails.

You can, however, use do.call to invoke a function like paste on a list to be passed as parameters, e.g.

library(data.table)

# passing parameters directly to `paste` works...
paste(x = c('a', 'b'), y = c(1, 2))
#> [1] "a 1" "b 2"

# ...but passing it a data frame gets weird (working in series instead of parallel)...
paste(data.table(x = c('a', 'b'), y = c(1, 2)))
#> [1] "c(\"a\", \"b\")" "c(1, 2)"

# ...so `do.call` turns the call here into the first version
do.call(paste, data.table(x = c('a', 'b'), y = c(1, 2)))
#> [1] "a 1" "b 2"
```

<p>In context, then,</p>

<pre class="lang-r prettyprint-override"><code>data(iris)
setDT(iris)
cols <- c("Species", "Petal.Width")

iris[, pasted := do.call(paste, .SD), .SDcols = cols]

iris[, c(cols, "pasted"), with = FALSE]
#>        Species Petal.Width        pasted
#>   1:    setosa         0.2    setosa 0.2
#>   2:    setosa         0.2    setosa 0.2
#>   3:    setosa         0.2    setosa 0.2
#>   4:    setosa         0.2    setosa 0.2
#>   5:    setosa         0.2    setosa 0.2
#>  ---
#> 146: virginica         2.3 virginica 2.3
#> 147: virginica         1.9 virginica 1.9
#> 148: virginica         2.0   virginica 2
#> 149: virginica         2.3 virginica 2.3
#> 150: virginica         1.8 virginica 1.8
</code>```

Alternatives to using .SDcols are the experimental .. notation:

iris[, pasted := do.call(paste, .SD[, ..cols])]
```

<p>or Ananda's elegant <code>mget</code>, which returns a list of the variable whose names you pass it:</p>

<pre class="lang-r prettyprint-override"><code>iris[, pasted := do.call(paste, mget(cols))]
</code>```

All return the same thing.

Body:
library(datasets)
library(dplyr)
library(data.table)

data(iris)
iris <- iris %>% as.data.table()

iris[ , pasted := paste(Species, Petal.Width)]

I would like an alternative of the above function so as the inputs of the paste function to be in a string vector. (The reason is that I may rerun the script and the names or numbers of variables in the paste function may differ ever time.

I tried the following

names <- c('Species', "Petal.Width")
iris[ , pasted := paste(names %>% get())]

or with using the .SD

 dt[, pasted:=paste0(.SD), .SDcols = names]

but they do not  work.

Any ideas?

Body:
If you pass a vector to .SDcols, .SD is a data frame (and therefore list) of those columns. You can't directly paste a data frame usefully, which is why the original code fails.

You can, however, use do.call to invoke a function like paste on a list to be passed as parameters, e.g.

library(data.table)

# passing parameters directly to `paste` works...
paste(x = c('a', 'b'), y = c(1, 2))
#> [1] "a 1" "b 2"

# ...but passing it a data frame gets weird (working in series instead of parallel)...
paste(data.table(x = c('a', 'b'), y = c(1, 2)))
#> [1] "c(\"a\", \"b\")" "c(1, 2)"

# ...so `do.call` turns the call here into the first version
do.call(paste, data.table(x = c('a', 'b'), y = c(1, 2)))
#> [1] "a 1" "b 2"

In context, then,

data(iris)
setDT(iris)
cols <- c("Species", "Petal.Width")

iris[, pasted := do.call(paste, .SD), .SDcols = cols]

iris[, c(cols, "pasted"), with = FALSE]
#>        Species Petal.Width        pasted
#>   1:    setosa         0.2    setosa 0.2
#>   2:    setosa         0.2    setosa 0.2
#>   3:    setosa         0.2    setosa 0.2
#>   4:    setosa         0.2    setosa 0.2
#>   5:    setosa         0.2    setosa 0.2
#>  ---
#> 146: virginica         2.3 virginica 2.3
#> 147: virginica         1.9 virginica 1.9
#> 148: virginica         2.0   virginica 2
#> 149: virginica         2.3 virginica 2.3
#> 150: virginica         1.8 virginica 1.8

Alternatives to using .SDcols are the experimental .. notation:

iris[, pasted := do.call(paste, .SD[, ..cols])]

or Ananda's elegant mget, which returns a list of the variable whose names you pass it:

iris[, pasted := do.call(paste, mget(cols))]

All return the same thing.


问:
Error when using system classloader with grapes
I'm writing a simple Groovy program with Java that reads something from the database:

```<code>@Grab('com.oracle:ojdbc6:11.2.0.1.0')
@Grab(group='org.codehaus.groovy', module='groovy-sql', version='2.4.6')

import groovy.sql.Sql

def db = [url: "jdbc:oracle:thin:@localhost:1521:XE",
          user: 'user_kshitiz', password: 'a', driver: 'oracle.jdbc.driver.OracleDriver']
def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
</code>```

However, this gives me a java.sql.SQLException: No suitable driver found error.
So I configure grapes to use system classloader:

```<code>@Grapes([
        @Grab('com.oracle:ojdbc6:11.2.0.1.0'),
        @GrabConfig(systemClassLoader = true)
])
</code>```

This too results in following error:

```<code>Information:Groovyc: java.lang.NoSuchMethodException: org.codehaus.groovy.runtime.dgm$518.<init>(java.lang.String, org.codehaus.groovy.reflection.CachedClass, java.lang.Class, [Ljava.lang.Class;)
    at java.lang.Class.getConstructor0(Class.java:2849)
    at java.lang.Class.getConstructor(Class.java:1718)
    at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.createProxy(GeneratedMetaMethod.java:92)
    at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.proxy(GeneratedMetaMethod.java:83)
    at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.isValidMethod(GeneratedMetaMethod.java:68)
    at groovy.lang.MetaClassImpl.chooseMethodInternal(MetaClassImpl.java:3172)
    at groovy.lang.MetaClassImpl.chooseMethod(MetaClassImpl.java:3134)
    at groovy.lang.MetaClassImpl.getMethodWithCachingInternal(MetaClassImpl.java:1330)
    at groovy.lang.MetaClassImpl.createPojoCallSite(MetaClassImpl.java:3367)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createPojoSite(CallSiteArray.java:132)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:166)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at groovy.grape.GrapeIvy.grab(GrapeIvy.groovy:271)
    at groovy.grape.Grape.grab(Grape.java:167)
    at groovy.grape.GrabAnnotationTransformation.visit(GrabAnnotationTransformation.java:378)
    at org.codehaus.groovy.transform.ASTTransformationVisitor$3.call(ASTTransformationVisitor.java:321)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:931)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546)
    at org.jetbrains.groovy.compiler.rt.GroovyCompilerWrapper.compile(GroovyCompilerWrapper.java:58)
    at org.jetbrains.groovy.compiler.rt.DependentGroovycRunner.runGroovyc(DependentGroovycRunner.java:116)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jetbrains.groovy.compiler.rt.GroovycRunner.intMain2(GroovycRunner.java:132)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.runGroovycInThisProcess(InProcessGroovyc.java:156)
    at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.access$000(InProcessGroovyc.java:51)
    at org.jetbrains.jps.incremental.groovy.InProcessGroovyc$1.call(InProcessGroovyc.java:85)
    at org.jetbrains.jps.incremental.groovy.InProcessGroovyc$1.call(InProcessGroovyc.java:82)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
</code>```

How do I create a database connection in Groovy?

答:
AFAIK, Oracle drivers are not publicly accessable in Maven Central. You have some options though:

Get access to some non public Maven repository containing Oracle JDBC drivers

Manually download Oracle JDBC driver from Oracle site, import it into your local Maven repository or Local artifactory-like service, and include this repository in your build script

Body:
I'm writing a simple Groovy program with Java that reads something from the database:

@Grab('com.oracle:ojdbc6:11.2.0.1.0')
@Grab(group='org.codehaus.groovy', module='groovy-sql', version='2.4.6')

import groovy.sql.Sql

def db = [url: "jdbc:oracle:thin:@localhost:1521:XE",
          user: 'user_kshitiz', password: 'a', driver: 'oracle.jdbc.driver.OracleDriver']
def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)

However, this gives me a java.sql.SQLException: No suitable driver found error.
So I configure grapes to use system classloader:

@Grapes([
        @Grab('com.oracle:ojdbc6:11.2.0.1.0'),
        @GrabConfig(systemClassLoader = true)
])

This too results in following error:

Information:Groovyc: java.lang.NoSuchMethodException: org.codehaus.groovy.runtime.dgm$518.<init>(java.lang.String, org.codehaus.groovy.reflection.CachedClass, java.lang.Class, [Ljava.lang.Class;)
    at java.lang.Class.getConstructor0(Class.java:2849)
    at java.lang.Class.getConstructor(Class.java:1718)
    at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.createProxy(GeneratedMetaMethod.java:92)
    at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.proxy(GeneratedMetaMethod.java:83)
    at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.isValidMethod(GeneratedMetaMethod.java:68)
    at groovy.lang.MetaClassImpl.chooseMethodInternal(MetaClassImpl.java:3172)
    at groovy.lang.MetaClassImpl.chooseMethod(MetaClassImpl.java:3134)
    at groovy.lang.MetaClassImpl.getMethodWithCachingInternal(MetaClassImpl.java:1330)
    at groovy.lang.MetaClassImpl.createPojoCallSite(MetaClassImpl.java:3367)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createPojoSite(CallSiteArray.java:132)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:166)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at groovy.grape.GrapeIvy.grab(GrapeIvy.groovy:271)
    at groovy.grape.Grape.grab(Grape.java:167)
    at groovy.grape.GrabAnnotationTransformation.visit(GrabAnnotationTransformation.java:378)
    at org.codehaus.groovy.transform.ASTTransformationVisitor$3.call(ASTTransformationVisitor.java:321)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:931)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546)
    at org.jetbrains.groovy.compiler.rt.GroovyCompilerWrapper.compile(GroovyCompilerWrapper.java:58)
    at org.jetbrains.groovy.compiler.rt.DependentGroovycRunner.runGroovyc(DependentGroovycRunner.java:116)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jetbrains.groovy.compiler.rt.GroovycRunner.intMain2(GroovycRunner.java:132)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.runGroovycInThisProcess(InProcessGroovyc.java:156)
    at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.access$000(InProcessGroovyc.java:51)
    at org.jetbrains.jps.incremental.groovy.InProcessGroovyc$1.call(InProcessGroovyc.java:85)
    at org.jetbrains.jps.incremental.groovy.InProcessGroovyc$1.call(InProcessGroovyc.java:82)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

How do I create a database connection in Groovy?


问:
Install4j: Executing installer in CentOS 7 Server fails to start with exception
I have an installer which is supposed to fall back to console mode if my Unix system does not have any X11 server or GUI mode available. The following is the configuration I have set for the Installer:

Now according to the fallback option, it should automatically run if console execution is enabled even if I do not run the installer using [installer] -c command (it should also be the normal use case). But unfortunately, whenever I execute the installer directly in the console mode, it is throwing an exception. The exception log is the following:

```<code>java.lang.NoClassDefFoundError: java.awt.Container
    at com.install4j.runtime.installer.frontend.headless.AbstractHeadlessScreenExecutor.init(AbstractHeadlessScreenExecutor.java:67)
    at com.install4j.runtime.installer.frontend.headless.ConsoleScreenExecutor.<init>(ConsoleScreenExecutor.java:24)
    at com.install4j.runtime.installer.frontend.headless.InstallerConsoleScreenExecutor.<init>(InstallerConsoleScreenExecutor.java:6)
    at com.install4j.runtime.installer.Installer.getScreenExecutor(Installer.java:88)
    at com.install4j.runtime.installer.Installer.runInProcess(Installer.java:57)
    at com.install4j.runtime.installer.Installer.main(Installer.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.exe4j.runtime.LauncherEngine.launch(LauncherEngine.java:81)
    at com.install4j.runtime.launcher.UnixLauncher.main(UnixLauncher.java:62)
</code>```

I have tried with Install4j 6.1.4 then also upgraded it with 7.0.3, the behavior is the same. My Linux installation is CentOS 7 Server and the installer has Java JRE 1.7.80 bundled with it.

Is there anything missing in my CentOS that install4j requires? Or do I have to configure anything else in my Installer so that it functions properly? I tried with the -c and it works in that case.

答:
The fallback currently tries to initialize a window and failing that switches to headless mode. On your system, headless mode does not seems to work in that case.

I'll add a different kind of headless check using GraphicsEnvironment.isHeadless(), please ask support@ej-technologies.com for a build where this is available.

Body:
I have an installer which is supposed to fall back to console mode if my Unix system does not have any X11 server or GUI mode available. The following is the configuration I have set for the Installer:

Now according to the fallback option, it should automatically run if console execution is enabled even if I do not run the installer using [installer] -c command (it should also be the normal use case). But unfortunately, whenever I execute the installer directly in the console mode, it is throwing an exception. The exception log is the following:

java.lang.NoClassDefFoundError: java.awt.Container
    at com.install4j.runtime.installer.frontend.headless.AbstractHeadlessScreenExecutor.init(AbstractHeadlessScreenExecutor.java:67)
    at com.install4j.runtime.installer.frontend.headless.ConsoleScreenExecutor.<init>(ConsoleScreenExecutor.java:24)
    at com.install4j.runtime.installer.frontend.headless.InstallerConsoleScreenExecutor.<init>(InstallerConsoleScreenExecutor.java:6)
    at com.install4j.runtime.installer.Installer.getScreenExecutor(Installer.java:88)
    at com.install4j.runtime.installer.Installer.runInProcess(Installer.java:57)
    at com.install4j.runtime.installer.Installer.main(Installer.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.exe4j.runtime.LauncherEngine.launch(LauncherEngine.java:81)
    at com.install4j.runtime.launcher.UnixLauncher.main(UnixLauncher.java:62)

I have tried with Install4j 6.1.4 then also upgraded it with 7.0.3, the behavior is the same. My Linux installation is CentOS 7 Server and the installer has Java JRE 1.7.80 bundled with it.

Is there anything missing in my CentOS that install4j requires? Or do I have to configure anything else in my Installer so that it functions properly? I tried with the -c and it works in that case.


问:
C Buffer Overflow - how many bytes to input
I made a simple C program to try understand buffer overflows. I am trying to overflow the input buffer so that flag gets changed to true and the program outputs "got here". The program is here (assume you have password.txt with hey in it):

```<code>#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv){
  int flag = 0;

  char pwd[5];
  char input[5];

  FILE *f = fopen("password.txt", "r");

  fscanf(f, "%s", pwd);
  fclose(f);

  strcpy(input, argv[1]);

  if(strcmp(input, pwd)){
    printf("wrong password!\n");
  }
  else{
    flag = 1;
    printf("correct password!\n");
  }
  if(flag){
    printf("you got into the secret place\n");
  }
  return 0;
}
</code>```

So on the stack we have something like this I guess:

```<code>[high addresses]
flag   ---> 4 bytes
pwd    ---> 8 bytes
input  ---> 8 bytes
[low addresses]
</code>```

So I think I just need to give the program just 17 bytes to overwrite the flag variable. So i gave it aaaaaaaaaaaaaaaaa but that didn't work. I had to give it a 23 times, so 23 bytes. Why is 17 bytes not enough?

答:
Disclaimer: I used Ubuntu 14.04 with gcc version 4.8.4, and complied your
code thusly gcc -m32 -g -ansi -pedantic -Wall temp.c -o temp.  Different
compilers, or different options to gcc will, in all probability, give
different results.

I also modified you code slightly to make it easier to find things,

I changed line 6 to  int flag = 0x41414141;

I changed line 25 to if(flag==1){

After compilation I run the executable under GDB, with a break point set
on main.  And then disassembled main (after setting disassembly-flavor to
Intel), we get:

```<code>(gdb) disass
Dump of assembler code for function main:
   0x0804857d <+0>: push   ebp
   0x0804857e <+1>: mov    ebp,esp
   0x08048580 <+3>: and    esp,0xfffffff0
   0x08048583 <+6>: sub    esp,0x30
   0x08048586 <+9>: mov    eax,DWORD PTR [ebp+0xc]
   0x08048589 <+12>:    mov    DWORD PTR [esp+0xc],eax
=> 0x0804858d <+16>:    mov    eax,gs:0x14
   0x08048593 <+22>:    mov    DWORD PTR [esp+0x2c],eax
   0x08048597 <+26>:    xor    eax,eax
   0x08048599 <+28>:    mov    DWORD PTR [esp+0x18],0x41414141
</code>```

The first four lines are the prolog for main  The important thing to
notice is the line sub    esp,0x30 where we are setting up the
stack frame for the function.  As you can see we are subtracting 48 bytes
from esp.  (actually a bit more because we first aligned the stack frame
to a 16-byte boundary).

Now, we can look at where our stack frame is by looking at the values
for ESP and EBP:

```<code>(gdb) info registers esp
 esp            0xffffd110  0xffffd110

(gdb) info registers ebp
ebp            0xffffd148   0xffffd148
</code>```

and we can find where things are located in the stack frame;

```<code>(gdb) print &pwd
$3 = (char (*)[5]) 0xffffd132

(gdb) print &flag
$4 = (int *) 0xffffd128

(gdb) print &input
$5 = (char (*)[5]) 0xffffd137

(gdb) print &f
$6 = (FILE **) 0xffffd12c
</code>```

From this we can now deduce our stack layout.  This memory image is
taken after running past where the program reads the command line
argument which was the string BBBBB  (recall that the ASCII code for B is 0x42, so it is easy to see a sequence of 0x42 bytes)

```<code>(gdb) x/56xb $esp
0xffffd110: 0x37    0xd1    0xff    0xff
0xffffd114: 0xbf    0xd3    0xff    0xff
0xffffd118: 0x32    0xd1    0xff    0xff
0xffffd11c: 0xe4    0xd1    0xff    0xff
0xffffd120: 0x02    0x00    0x00    0x00
0xffffd124: 0xe4    0xd1    0xff    0xff
0xffffd128: 0x41    0x41    0x41    0x41         (flag)
0xffffd12c: 0x08    0xb0    0x04    0x08         (f)
0xffffd130: 0xc4    0xf3
0xffffd132: 0x68    0x65    0x79    0x00    0xff (pwd buffer)
0xffffd137: 0x42    0x42    0x42    0x42    0x42 (input buffer)
0xffffd13c: 0x00    0xd5    0x61    0x5d
0xffffd140: 0x60    0x86    0x04    0x08
0xffffd144: 0x00    0x00    0x00    0x00
</code>```

Also note, that if I set the command line argument to BBBBBBBB we have this
for the contents of our stack frame

```<code>(gdb) x/56xb $esp
0xffffd110: 0x37    0xd1    0xff    0xff    0xbc    0xd3    0xff    0xff
0xffffd118: 0x32    0xd1    0xff    0xff    0xe4    0xd1    0xff    0xff
0xffffd120: 0x02    0x00    0x00    0x00    0xe4    0xd1    0xff    0xff
0xffffd128: 0x41    0x41    0x41    0x41    0x08    0xb0    0x04    0x08
0xffffd130: 0xc4    0xf3    0x68    0x65    0x79    0x00    0xff    0x42
0xffffd138: 0x42    0x42    0x42    0x42    0x42    0x42    0x42    0x00
0xffffd140: 0x60    0x86    0x04    0x08    0x00    0x00    0x00    0x00
</code>```

Notice that the contents of the flag variable remains unchanged, but the contents of the now overflown input buffer moves toward the top of the stack.  Recall that in x86 the stack grows downward (lower memory addresses).  Also, because the buffer grows upward is why we can use a buffer overflow to over write stored EIP on the stack.

So on my system, I do not believe that it is possible to overwrite the flag variable with user input.  You system might lay things out differently on the stack (you would need to do a similar exercise to verify this).

Also, note that the location of the variable on the stack does not correlate with the order that they are declared in the source file.

Body:
I made a simple C program to try understand buffer overflows. I am trying to overflow the input buffer so that flag gets changed to true and the program outputs "got here". The program is here (assume you have password.txt with hey in it):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv){
  int flag = 0;

  char pwd[5];
  char input[5];

  FILE *f = fopen("password.txt", "r");

  fscanf(f, "%s", pwd);
  fclose(f);

  strcpy(input, argv[1]);

  if(strcmp(input, pwd)){
    printf("wrong password!\n");
  }
  else{
    flag = 1;
    printf("correct password!\n");
  }
  if(flag){
    printf("you got into the secret place\n");
  }
  return 0;
}

So on the stack we have something like this I guess:

[high addresses]
flag   ---> 4 bytes
pwd    ---> 8 bytes
input  ---> 8 bytes
[low addresses]

So I think I just need to give the program just 17 bytes to overwrite the flag variable. So i gave it aaaaaaaaaaaaaaaaa but that didn't work. I had to give it a 23 times, so 23 bytes. Why is 17 bytes not enough?

Body:
Disclaimer: I used Ubuntu 14.04 with gcc version 4.8.4, and complied your
code thusly gcc -m32 -g -ansi -pedantic -Wall temp.c -o temp.  Different
compilers, or different options to gcc will, in all probability, give
different results.

I also modified you code slightly to make it easier to find things,

I changed line 6 to  int flag = 0x41414141;

I changed line 25 to if(flag==1){

After compilation I run the executable under GDB, with a break point set
on main.  And then disassembled main (after setting disassembly-flavor to
Intel), we get:

(gdb) disass
Dump of assembler code for function main:
   0x0804857d <+0>: push   ebp
   0x0804857e <+1>: mov    ebp,esp
   0x08048580 <+3>: and    esp,0xfffffff0
   0x08048583 <+6>: sub    esp,0x30
   0x08048586 <+9>: mov    eax,DWORD PTR [ebp+0xc]
   0x08048589 <+12>:    mov    DWORD PTR [esp+0xc],eax
=> 0x0804858d <+16>:    mov    eax,gs:0x14
   0x08048593 <+22>:    mov    DWORD PTR [esp+0x2c],eax
   0x08048597 <+26>:    xor    eax,eax
   0x08048599 <+28>:    mov    DWORD PTR [esp+0x18],0x41414141

The first four lines are the prolog for main  The important thing to
notice is the line sub    esp,0x30 where we are setting up the
stack frame for the function.  As you can see we are subtracting 48 bytes
from esp.  (actually a bit more because we first aligned the stack frame
to a 16-byte boundary).

Now, we can look at where our stack frame is by looking at the values
for ESP and EBP:

(gdb) info registers esp
 esp            0xffffd110  0xffffd110

(gdb) info registers ebp
ebp            0xffffd148   0xffffd148

and we can find where things are located in the stack frame;

(gdb) print &pwd
$3 = (char (*)[5]) 0xffffd132

(gdb) print &flag
$4 = (int *) 0xffffd128

(gdb) print &input
$5 = (char (*)[5]) 0xffffd137

(gdb) print &f
$6 = (FILE **) 0xffffd12c

From this we can now deduce our stack layout.  This memory image is
taken after running past where the program reads the command line
argument which was the string BBBBB  (recall that the ASCII code for B is 0x42, so it is easy to see a sequence of 0x42 bytes)

(gdb) x/56xb $esp
0xffffd110: 0x37    0xd1    0xff    0xff
0xffffd114: 0xbf    0xd3    0xff    0xff
0xffffd118: 0x32    0xd1    0xff    0xff
0xffffd11c: 0xe4    0xd1    0xff    0xff
0xffffd120: 0x02    0x00    0x00    0x00
0xffffd124: 0xe4    0xd1    0xff    0xff
0xffffd128: 0x41    0x41    0x41    0x41         (flag)
0xffffd12c: 0x08    0xb0    0x04    0x08         (f)
0xffffd130: 0xc4    0xf3
0xffffd132: 0x68    0x65    0x79    0x00    0xff (pwd buffer)
0xffffd137: 0x42    0x42    0x42    0x42    0x42 (input buffer)
0xffffd13c: 0x00    0xd5    0x61    0x5d
0xffffd140: 0x60    0x86    0x04    0x08
0xffffd144: 0x00    0x00    0x00    0x00

Also note, that if I set the command line argument to BBBBBBBB we have this
for the contents of our stack frame

(gdb) x/56xb $esp
0xffffd110: 0x37    0xd1    0xff    0xff    0xbc    0xd3    0xff    0xff
0xffffd118: 0x32    0xd1    0xff    0xff    0xe4    0xd1    0xff    0xff
0xffffd120: 0x02    0x00    0x00    0x00    0xe4    0xd1    0xff    0xff
0xffffd128: 0x41    0x41    0x41    0x41    0x08    0xb0    0x04    0x08
0xffffd130: 0xc4    0xf3    0x68    0x65    0x79    0x00    0xff    0x42
0xffffd138: 0x42    0x42    0x42    0x42    0x42    0x42    0x42    0x00
0xffffd140: 0x60    0x86    0x04    0x08    0x00    0x00    0x00    0x00

Notice that the contents of the flag variable remains unchanged, but the contents of the now overflown input buffer moves toward the top of the stack.  Recall that in x86 the stack grows downward (lower memory addresses).  Also, because the buffer grows upward is why we can use a buffer overflow to over write stored EIP on the stack.

So on my system, I do not believe that it is possible to overwrite the flag variable with user input.  You system might lay things out differently on the stack (you would need to do a similar exercise to verify this).

Also, note that the location of the variable on the stack does not correlate with the order that they are declared in the source file.