问:
SequelizeDatabaseError: Cannot truncate a table referenced in a foreign key constraint
I am using "sequelize": "^5.8.6" and I have defined two models, company and dividend.

My company.js model looks like the following:

```<code>'use strict';
module.exports = (sequelize, DataTypes) => {
  const Company = sequelize.define('Company', {
    company_name: DataTypes.STRING,
    company_link: DataTypes.STRING,
    ticker: DataTypes.STRING,
    description: DataTypes.STRING
  }, {});
  Company.associate = function(models) {
    Company.hasMany(models.Rating, { onDelete: 'cascade' });
    Company.hasMany(models.Dividend, { onDelete: 'cascade' });
  };
  return Company;
};
</code>```

My dividend.js model looks like the following:

```<code>'use strict';
module.exports = (sequelize, DataTypes) => {
  const Dividend = sequelize.define('Dividend', {
    period: DataTypes.STRING,
    amount: DataTypes.FLOAT,
    payable_date: DataTypes.DATE,
  }, {});
  Dividend.associate = function(models) {
    Dividend.belongsTo(models.Company, { onDelete: 'cascade' });
  };
  return Dividend;
};
</code>```

My truncate.js function looks like the following:

```<code>const models = require('../models');

const truncateTable = (modelName) =>
  models[modelName].destroy({
    where: {},
    force: true,
  });

module.exports = async function truncate(model) {
  if (model) {
    return truncateTable(model);
  }

  return Promise.all(
    Object.keys(models).map((key) => {
      if (['sequelize', 'Sequelize'].includes(key)) return null;
      return truncateTable(key);
    })
  );
}
</code>```

When trying to truncate both models I get the following error:

```<code>{ SequelizeDatabaseError: Cannot truncate a table referenced in a foreign key constraint (`test-db`.`dividends`, CONSTRAINT
`dividends_ibfk_1` FOREIGN KEY (`CompanyId`) REFERENCES `test-db`.`companies` (`id`))
    at Query.formatError (c:\test\node_modules\sequelize\lib\dialects\mysql\query.js:239:16)
    at Query.handler [as onResult] (c:\test\node_modules\sequelize\lib\dialects\mysql\query.js:46:23)
    at Query.execute (c:\test\node_modules\mysql2\lib\commands\command.js:30:14)
    at Connection.handlePacket (c:\test\node_modules\mysql2\lib\connection.js:449:32)
    at PacketParser.Connection.packetParser.p [as onPacket] (c:\test\node_modules\mysql2\lib\connection.js:72:12)
    at PacketParser.executeStart (c:\test\node_modules\mysql2\lib\packet_parser.js:75:16)
    at Socket.Connection.stream.on.data (c:\test\node_modules\mysql2\lib\connection.js:79:25)
    at Socket.emit (events.js:189:13)
    at addChunk (_stream_readable.js:284:12)
    at readableAddChunk (_stream_readable.js:265:11)
    at Socket.Readable.push (_stream_readable.js:220:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  name: 'SequelizeDatabaseError',
  parent:
   { Error: Cannot truncate a table referenced in a foreign key constraint (`test-db`.`dividends`, CONSTRAINT `dividends_ibfk_1` FOREIGN KEY (`CompanyId`) REFERENCES `test-db`.`companies` (`id`))
       at Packet.asError (c:\test\node_modules\mysql2\lib\packets\packet.js:684:17)
       at Query.execute (c:\test\node_modules\mysql2\lib\commands\command.js:28:26)
       at Connection.handlePacket (c:\test\node_modules\mysql2\lib\connection.js:449:32)
       at PacketParser.Connection.packetParser.p [as onPacket] (c:\test\node_modules\mysql2\lib\connection.js:72:12)
       at PacketParser.executeStart (c:\test\node_modules\mysql2\lib\packet_parser.js:75:16)
       at Socket.Connection.stream.on.data (c:\test\node_modules\mysql2\lib\connection.js:79:25)
       at Socket.emit (events.js:189:13)
       at addChunk (_stream_readable.js:284:12)
       at readableAddChunk (_stream_readable.js:265:11)
       at Socket.Readable.push (_stream_readable.js:220:10)
       at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
     code: 'ER_TRUNCATE_ILLEGAL_FK',
     errno: 1701,
     sqlState: '42000',
     sqlMessage:
      'Cannot truncate a table referenced in a foreign key constraint (`test-db`.`dividends`, CONSTRAINT `dividends_ibfk_1`
FOREIGN KEY (`CompanyId`) REFERENCES `test-db`.`companies` (`id`))',
     sql: 'TRUNCATE `Companies`' },
  original:
   { Error: Cannot truncate a table referenced in a foreign key constraint (`test-db`.`dividends`, CONSTRAINT `dividends_ibfk_1` FOREIGN KEY (`CompanyId`) REFERENCES `test-db`.`companies` (`id`))
       at Packet.asError (c:\test\node_modules\mysql2\lib\packets\packet.js:684:17)
       at Query.execute (c:\test\node_modules\mysql2\lib\commands\command.js:28:26)
       at Connection.handlePacket (c:\test\node_modules\mysql2\lib\connection.js:449:32)
       at PacketParser.Connection.packetParser.p [as onPacket] (c:\test\node_modules\mysql2\lib\connection.js:72:12)
       at PacketParser.executeStart (c:\test\node_modules\mysql2\lib\packet_parser.js:75:16)
       at Socket.Connection.stream.on.data (c:\test\node_modules\mysql2\lib\connection.js:79:25)
       at Socket.emit (events.js:189:13)
       at addChunk (_stream_readable.js:284:12)
       at readableAddChunk (_stream_readable.js:265:11)
       at Socket.Readable.push (_stream_readable.js:220:10)
       at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
     code: 'ER_TRUNCATE_ILLEGAL_FK',
     errno: 1701,
     sqlState: '42000',
     sqlMessage:
      'Cannot truncate a table referenced in a foreign key constraint (`test-db`.`dividends`, CONSTRAINT `dividends_ibfk_1`
FOREIGN KEY (`CompanyId`) REFERENCES `test-db`.`companies` (`id`))',
     sql: 'TRUNCATE `Companies`' },
  sql: 'TRUNCATE `Companies`' }
</code>```

Any suggestions why I get this error?

I appreciate your replies!

答:
I know its quiet late but hope it helps someone.
I am using sequelize 3.35.1 and this resolved the issue for me:

```<code>    sequelize = new Sequelize("database_name", "Username", "password");

    sequelize.query('SET GLOBAL FOREIGN_KEY_CHECKS = 0;', { raw: true });
    // Do Some Action
    sequelize.query('SET GLOBAL FOREIGN_KEY_CHECKS = 1;', { raw: true });
</code>```

Only SET GLOBAL FOREIGN_KEY_CHECKS = 0; was not doing the trick for me.

Body:
I am using "sequelize": "^5.8.6" and I have defined two models, company and dividend.

My company.js model looks like the following:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Company = sequelize.define('Company', {
    company_name: DataTypes.STRING,
    company_link: DataTypes.STRING,
    ticker: DataTypes.STRING,
    description: DataTypes.STRING
  }, {});
  Company.associate = function(models) {
    Company.hasMany(models.Rating, { onDelete: 'cascade' });
    Company.hasMany(models.Dividend, { onDelete: 'cascade' });
  };
  return Company;
};

My dividend.js model looks like the following:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Dividend = sequelize.define('Dividend', {
    period: DataTypes.STRING,
    amount: DataTypes.FLOAT,
    payable_date: DataTypes.DATE,
  }, {});
  Dividend.associate = function(models) {
    Dividend.belongsTo(models.Company, { onDelete: 'cascade' });
  };
  return Dividend;
};

My truncate.js function looks like the following:

const models = require('../models');

const truncateTable = (modelName) =>
  models[modelName].destroy({
    where: {},
    force: true,
  });

module.exports = async function truncate(model) {
  if (model) {
    return truncateTable(model);
  }

  return Promise.all(
    Object.keys(models).map((key) => {
      if (['sequelize', 'Sequelize'].includes(key)) return null;
      return truncateTable(key);
    })
  );
}

When trying to truncate both models I get the following error:

{ SequelizeDatabaseError: Cannot truncate a table referenced in a foreign key constraint (`test-db`.`dividends`, CONSTRAINT
`dividends_ibfk_1` FOREIGN KEY (`CompanyId`) REFERENCES `test-db`.`companies` (`id`))
    at Query.formatError (c:\test\node_modules\sequelize\lib\dialects\mysql\query.js:239:16)
    at Query.handler [as onResult] (c:\test\node_modules\sequelize\lib\dialects\mysql\query.js:46:23)
    at Query.execute (c:\test\node_modules\mysql2\lib\commands\command.js:30:14)
    at Connection.handlePacket (c:\test\node_modules\mysql2\lib\connection.js:449:32)
    at PacketParser.Connection.packetParser.p [as onPacket] (c:\test\node_modules\mysql2\lib\connection.js:72:12)
    at PacketParser.executeStart (c:\test\node_modules\mysql2\lib\packet_parser.js:75:16)
    at Socket.Connection.stream.on.data (c:\test\node_modules\mysql2\lib\connection.js:79:25)
    at Socket.emit (events.js:189:13)
    at addChunk (_stream_readable.js:284:12)
    at readableAddChunk (_stream_readable.js:265:11)
    at Socket.Readable.push (_stream_readable.js:220:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  name: 'SequelizeDatabaseError',
  parent:
   { Error: Cannot truncate a table referenced in a foreign key constraint (`test-db`.`dividends`, CONSTRAINT `dividends_ibfk_1` FOREIGN KEY (`CompanyId`) REFERENCES `test-db`.`companies` (`id`))
       at Packet.asError (c:\test\node_modules\mysql2\lib\packets\packet.js:684:17)
       at Query.execute (c:\test\node_modules\mysql2\lib\commands\command.js:28:26)
       at Connection.handlePacket (c:\test\node_modules\mysql2\lib\connection.js:449:32)
       at PacketParser.Connection.packetParser.p [as onPacket] (c:\test\node_modules\mysql2\lib\connection.js:72:12)
       at PacketParser.executeStart (c:\test\node_modules\mysql2\lib\packet_parser.js:75:16)
       at Socket.Connection.stream.on.data (c:\test\node_modules\mysql2\lib\connection.js:79:25)
       at Socket.emit (events.js:189:13)
       at addChunk (_stream_readable.js:284:12)
       at readableAddChunk (_stream_readable.js:265:11)
       at Socket.Readable.push (_stream_readable.js:220:10)
       at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
     code: 'ER_TRUNCATE_ILLEGAL_FK',
     errno: 1701,
     sqlState: '42000',
     sqlMessage:
      'Cannot truncate a table referenced in a foreign key constraint (`test-db`.`dividends`, CONSTRAINT `dividends_ibfk_1`
FOREIGN KEY (`CompanyId`) REFERENCES `test-db`.`companies` (`id`))',
     sql: 'TRUNCATE `Companies`' },
  original:
   { Error: Cannot truncate a table referenced in a foreign key constraint (`test-db`.`dividends`, CONSTRAINT `dividends_ibfk_1` FOREIGN KEY (`CompanyId`) REFERENCES `test-db`.`companies` (`id`))
       at Packet.asError (c:\test\node_modules\mysql2\lib\packets\packet.js:684:17)
       at Query.execute (c:\test\node_modules\mysql2\lib\commands\command.js:28:26)
       at Connection.handlePacket (c:\test\node_modules\mysql2\lib\connection.js:449:32)
       at PacketParser.Connection.packetParser.p [as onPacket] (c:\test\node_modules\mysql2\lib\connection.js:72:12)
       at PacketParser.executeStart (c:\test\node_modules\mysql2\lib\packet_parser.js:75:16)
       at Socket.Connection.stream.on.data (c:\test\node_modules\mysql2\lib\connection.js:79:25)
       at Socket.emit (events.js:189:13)
       at addChunk (_stream_readable.js:284:12)
       at readableAddChunk (_stream_readable.js:265:11)
       at Socket.Readable.push (_stream_readable.js:220:10)
       at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
     code: 'ER_TRUNCATE_ILLEGAL_FK',
     errno: 1701,
     sqlState: '42000',
     sqlMessage:
      'Cannot truncate a table referenced in a foreign key constraint (`test-db`.`dividends`, CONSTRAINT `dividends_ibfk_1`
FOREIGN KEY (`CompanyId`) REFERENCES `test-db`.`companies` (`id`))',
     sql: 'TRUNCATE `Companies`' },
  sql: 'TRUNCATE `Companies`' }

Any suggestions why I get this error?

I appreciate your replies!

Body:
I know its quiet late but hope it helps someone.
I am using sequelize 3.35.1 and this resolved the issue for me:

    sequelize = new Sequelize("database_name", "Username", "password");

    sequelize.query('SET GLOBAL FOREIGN_KEY_CHECKS = 0;', { raw: true });
    // Do Some Action
    sequelize.query('SET GLOBAL FOREIGN_KEY_CHECKS = 1;', { raw: true });

Only SET GLOBAL FOREIGN_KEY_CHECKS = 0; was not doing the trick for me.


来源: stackexchange

问:
Rails model validators break earlier migrations
I have a sequence of migrations in a rails app which includes the following steps:

Create basic version of the 'user' model

Create an instance of this model - there needs to be at least one initial user in my system so that you can log in and start using it

Update the 'user' model to add a new field / column.

Now I'm using "validates_inclusion_of" on this new field/column.  This worked fine on my initial development machine, which already had a database with these migrations applied.  However, if I go to a fresh machine and run all the migrations, step 2 fails, because validates_inclusion_of fails, because the field from migration 3 hasn't been added to the model class yet.

As a workaround, I can comment out the "validates_..." line, run the migrations, and uncomment it, but that's not nice.

Better would be to re-order my migrations so the user creation (step 2) comes last, after all columns have been added.

I'm a rails newbie though, so I thought I'd ask what the preferred way to handle this situation is :)

答:
The easiest way to avoid this issue is to use rake db:schema:load on the second machine, instead of db:migrate. rake db:schema:load uses schema.rb to load the most current version of your schema, as opposed to migrating it up form scratch.

If you run into this issue when deploying to a production machine (where preserving data is important), you'll probably have to consolidate your migrations into a single file without conflicts.

OwnerDisplayName: Luke H

ContentLicense: CC BY-SA 2.5

ContentLicense: CC BY-SA 4.0


问:
"Left side cannot be assigned to" for record type properties in Delphi
I'm curious to know why Delphi treats record type properties as read only:

```<code>  TRec = record
    A : integer;
    B : string;
  end;

  TForm1 = class(TForm)
  private
    FRec : TRec;
  public
    procedure DoSomething(ARec: TRec);
    property Rec : TRec read FRec write FRec;
  end;
</code>```

If I try to assign a value to any of the members of Rec property, I'll get "Left side cannot be assigned to" error:

```<code>procedure TForm1.DoSomething(ARec: TRec);
begin
  Rec.A := ARec.A;
end;
</code>```

while doing the same with the underlying field is allowed:

```<code>procedure TForm1.DoSomething(ARec: TRec);
begin
  FRec.A := ARec.A;
end;
</code>```

Is there any explanation for that behavior?

答: The compiler is stopping you from assigning to a temporary. The equivalent in C# is permitted, but it has no effect; the return value of the Rec property is a copy of the underlying field, and assigning to the field on the copy is a nop.</p>

OwnerDisplayName: vcldeveloper

Body:
I'm curious to know why Delphi treats record type properties as read only:

  TRec = record
    A : integer;
    B : string;
  end;

  TForm1 = class(TForm)
  private
    FRec : TRec;
  public
    procedure DoSomething(ARec: TRec);
    property Rec : TRec read FRec write FRec;
  end;

If I try to assign a value to any of the members of Rec property, I'll get "Left side cannot be assigned to" error:

procedure TForm1.DoSomething(ARec: TRec);
begin
  Rec.A := ARec.A;
end;

while doing the same with the underlying field is allowed:

procedure TForm1.DoSomething(ARec: TRec);
begin
  FRec.A := ARec.A;
end;

Is there any explanation for that behavior?

OwnerDisplayName: Barry Kelly


问:
ggplot2 fill/grouping causes unwanted lines to connect in geom_ribbon
I'm trying to plot a stock price and some color above it as an indicator. The problem is that the color should not always be present which is indicated by having missing values, but using geom_ribbon causes the missing values to appear. The code is as follows:

```<code>library(dplyr)
library(ggplot2)
library(lubridate)
library(padr)

# Does not work correctly
ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = a, inherit.aes = FALSE, aes(
    x = Date2,
    ymin = Price * 1.1,
    ymax = Price,
    y = Price,
    fill = Group
  ), alpha = 0.9) +
  geom_line()

# Add missing values as NAs
b <- pad(a)

# Works but does not show colors
ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = b,
              inherit.aes = FALSE,
              aes(x = Date2,
                  ymin = Price * 1.1,
                  ymax = Price,
                  y = Price#,
                  #fill = Group
              ),
              alpha = 0.9) +
  geom_line()

# Does not work correctly because of the grouping caused by the fill
ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = b,
              inherit.aes = FALSE,
              aes(x = Date2,
                  ymin = Price * 1.1,
                  ymax = Price,
                  y = Price,
                  fill = Group),
              alpha = 0.9) +
  geom_line()

</code>```

It should look like this but without the missing values being filled in:

Padding the missing values with NAs allows to not show the missing values. Only the following points should have a ribbon above them, but it seems impossible to color them:

If the ribbons are colored after padding, then the data get grouped and the missing values get interpolated, which should not be the case:

Data:

```<code>x <- structure(list(Date = structure(c(18213, 18214, 18215, 18218,
                                       18219, 18220, 18221, 18222, 18225, 18226, 18227, 18229, 18232,
                                       18233, 18234, 18235, 18236, 18239, 18240, 18241, 18242, 18243,
                                       18246, 18247, 18248, 18249, 18250, 18253, 18254, 18256, 18257,
                                       18260, 18261, 18263, 18264, 18267, 18268, 18269, 18270, 18271,
                                       18274, 18275, 18276, 18277, 18278, 18282, 18283, 18284, 18285,
                                       18288, 18289, 18290, 18291, 18292, 18295, 18296, 18297, 18298,
                                       18299, 18302, 18303, 18304, 18305, 18306, 18310, 18311, 18312,
                                       18313, 18316, 18317, 18318, 18319, 18320, 18324, 18325, 18326,
                                       18327, 18328, 18331, 18332, 18333, 18334, 18335, 18338, 18339,
                                       18340, 18341, 18342, 18345, 18346, 18347, 18348, 18349, 18352,
                                       18353, 18354, 18355, 18356, 18359, 18360, 18361, 18362, 18363,
                                       18366, 18367, 18368, 18369, 18373, 18374, 18375, 18376, 18377,
                                       18380, 18381, 18382, 18383, 18384, 18387, 18388, 18389, 18390,
                                       18391, 18394, 18395, 18396, 18397, 18398, 18401, 18402, 18403,
                                       18404, 18405, 18409, 18410, 18411, 18412, 18415, 18416, 18417,
                                       18418, 18419, 18422, 18423, 18424, 18425, 18426, 18429, 18430,
                                       18431, 18432, 18433, 18436, 18437, 18438, 18439, 18440, 18443,
                                       18444, 18445, 18446, 18450, 18451, 18452, 18453, 18454, 18457,
                                       18458, 18459, 18460, 18461, 18464, 18465, 18466, 18467, 18468,
                                       18471, 18472, 18473, 18474, 18475, 18478, 18479, 18480, 18481,
                                       18482, 18485, 18486, 18487, 18488, 18489, 18492, 18493, 18494,
                                       18495, 18496, 18499, 18500, 18501, 18502, 18503, 18507, 18508,
                                       18509, 18510, 18513, 18514, 18515, 18516, 18517, 18520, 18521,
                                       18522, 18523, 18524, 18527, 18528, 18529, 18530, 18531, 18534,
                                       18535, 18536, 18537, 18538, 18541, 18542, 18543, 18544, 18545,
                                       18548, 18549, 18550, 18551, 18552, 18555, 18556, 18557, 18558,
                                       18559, 18562, 18563, 18564, 18565, 18566, 18569, 18570, 18571,
                                       18572, 18573, 18576, 18577, 18578, 18579, 18580, 18583, 18584,
                                       18585, 18586, 18587, 18590, 18591, 18592, 18594, 18597, 18598,
                                       18599, 18600, 18601, 18604, 18605, 18606, 18607, 18608, 18611,
                                       18612, 18613, 18614, 18615, 18618, 18619, 18620, 18622, 18625,
                                       18626, 18627), class = "Date"),
                    Stock = c(32.302406, 32.783638,
                              32.753559, 33.084404, 32.723484, 32.377602, 32.663322, 32.994171,
                              32.422722, 32.332485, 32.106918, 32.00164, 31.445225, 31.294838,
                              31.249725, 30.738419, 31.580574, 31.881338, 31.941496, 31.460262,
                              31.370037, 31.430185, 31.685837, 31.730949, 32.531425, 32.305305,
                              32.320374, 32.893227, 32.817848, 32.516354, 32.697247, 32.531425,
                              33.013821, 31.883207, 31.702311, 31.084244, 31.219913, 31.129463,
                              30.963646, 31.536489, 31.023943, 31.38574, 31.476185, 31.219913,
                              31.250069, 31.280209, 31.415892, 30.692297, 30.119457, 30.07423,
                              29.938557, 29.305418, 29.606909, 29.395864, 28.265259, 29.124521,
                              29.471237, 30.05916, 30.104383, 29.938557, 30.330505, 30.360651,
                              30.119457, 30.360651, 29.998857, 28.958696, 29.486313, 29.712437,
                              29.727509, 29.245119, 28.732574, 28.476297, 27.767788, 25.627169,
                              26.727627, 26.622101, 26.079411, 25.883436, 25.868364, 25.20507,
                              24.858349, 23.652367, 24.662378, 25.551794, 26.667328, 25.868364,
                              25.159845, 25.597015, 25.597015, 26.652252, 26.652252, 26.727627,
                              26.546728, 27.828083, 28.024061, 27.767788, 27.390923, 27.481363,
                              26.893454, 27.044199, 27.285393, 26.953745, 27.225088, 26.365831,
                              25.627169, 26.184935, 27.601965, 26.652252, 26.411057, 26.395979,
                              25.702539, 24.978949, 26.184935, 26.124634, 26.139708, 26.139708,
                              25.732691, 25.883436, 26.380905, 27.737633, 27.782866, 27.586891,
                              27.933609, 28.325556, 28.702429, 28.370781, 28.642126, 29.395864,
                              29.682287, 30.013933, 29.727509, 30.044086, 29.410936, 29.471237,
                              29.712437, 29.214966, 29.426016, 29.772738, 29.848104, 29.953634,
                              30.345573, 30.436029, 30.541553, 30.782749, 30.390808, 30.601849,
                              30.194832, 30.209909, 30.963646, 30.707371, 30.586777, 30.888273,
                              31.491264, 31.226501, 30.954172, 31.075211, 30.606213, 30.454916,
                              31.271893, 31.302149, 31.196245, 31.211376, 31.710632, 31.150858,
                              31.105465, 30.863405, 30.575953, 30.969309, 28.89662, 29.501785,
                              28.836103, 29.123556, 28.972263, 29.093298, 28.639423, 28.20068,
                              27.610645, 28.351974, 27.882971, 27.928352, 28.125036, 27.232416,
                              26.521349, 26.808798, 27.550123, 27.958612, 27.746809, 28.246065,
                              28.397356, 28.291456, 28.881491, 29.138687, 29.108423, 29.516909,
                              28.972263, 29.486652, 29.380754, 29.229462, 28.321712, 27.686293,
                              27.338318, 29.00252, 28.715067, 29.380754, 29.00252, 28.730202,
                              29.032782, 28.20068, 28.185551, 27.535, 27.988873, 27.580385,
                              27.429092, 27.277803, 26.823933, 27.247543, 27.988873, 27.262672,
                              27.171898, 26.415443, 26.127991, 25.764894, 25.94644, 25.94644,
                              26.521349, 25.825409, 26.11286, 25.47744, 25.053823, 25.295889,
                              25.250502, 25.462311, 24.887403, 25.386665, 25.053823, 25.295889,
                              24.660463, 24.509178, 24.963047, 24.645338, 25.038694, 24.766371,
                              25.961571, 25.537954, 25.159723, 25.038694, 24.811756, 23.782978,
                              24.176332, 24.645338, 24.478916, 24.61508, 23.767849, 23.979656,
                              23.934263, 24.040173, 24.085558, 24.5243, 24.947918, 25.038694,
                              24.312494, 23.979656, 22.618032, 22.723938, 21.649773, 22.118773,
                              21.558996, 21.649773, 20.409182, 20.424313, 19.985567, 18.835754,
                              18.260849, 16.173027, 16.309193, 18.383097, 17.293257, 19.008156,
                              19.745405, 19.376781, 19.825541, 20.130056, 18.206802, 19.55308,
                              18.383097)),
               class = "data.frame", row.names = c(NA, -285L))

a <- structure(list(Price = c(16.173027, 16.309193, 25.627169, 26.079411,
                              25.883436, 25.868364, 25.20507, 24.858349, 23.652367, 24.662378,
                              25.551794, 25.868364, 25.159845, 25.597015, 25.597015, 26.365831,
                              25.627169, 26.184935, 26.411057, 26.395979, 25.702539, 24.978949,
                              26.184935, 26.124634, 26.139708, 26.139708, 25.732691, 25.883436,
                              26.380905, 26.415443, 26.127991, 25.764894, 25.94644, 25.94644,
                              25.825409, 26.11286, 25.47744, 25.053823, 25.295889, 25.250502,
                              25.462311, 24.887403, 25.386665, 25.053823, 25.295889, 24.660463,
                              24.509178, 24.963047, 24.645338, 25.038694, 24.766371, 25.961571,
                              25.537954, 25.159723, 25.038694, 24.811756, 23.782978, 24.176332,
                              24.645338, 24.478916, 24.61508, 23.767849, 23.979656, 23.934263,
                              24.040173, 24.085558, 24.5243, 24.947918, 25.038694, 24.312494,
                              23.979656, 22.618032, 22.723938, 21.649773, 22.118773, 21.558996,
                              21.649773, 20.409182, 20.424313, 19.985567, 18.835754, 18.260849,
                              18.383097, 17.293257, 19.008156, 19.745405, 19.376781, 19.825541,
                              20.130056, 18.206802, 19.55308, 18.383097),
                    Date2 = structure(c(18611,
                                        18612, 18324, 18327, 18328, 18331, 18332, 18333, 18334, 18335,
                                        18338, 18340, 18341, 18342, 18345, 18366, 18367, 18368, 18374,
                                        18375, 18376, 18377, 18380, 18381, 18382, 18383, 18384, 18387,
                                        18388, 18534, 18535, 18536, 18537, 18538, 18542, 18543, 18544,
                                        18545, 18548, 18549, 18550, 18551, 18552, 18555, 18556, 18557,
                                        18558, 18559, 18562, 18563, 18564, 18565, 18566, 18569, 18570,
                                        18571, 18572, 18573, 18576, 18577, 18578, 18579, 18580, 18583,
                                        18584, 18585, 18586, 18587, 18590, 18591, 18592, 18594, 18597,
                                        18598, 18599, 18600, 18601, 18604, 18605, 18606, 18607, 18608,
                                        18613, 18614, 18615, 18618, 18619, 18620, 18622, 18625, 18626,
                                        18627), class = "Date"),
                    Group = c("b", "b", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a")),
               class = "data.frame", row.names = c(NA,
                                                   -92L))
</code>```

答:
Now that I understood the logic of teunbrand's answer, I came up with a much clearer solution:</p>

```<code>b <- a %>%
  mutate(run = as.character(
    cumsum(ifelse(Date2 - lag(Date2) <= 1 | is.na(lag(Date2)),
                  ifelse(lag(Group) == Group | is.na(lag(Date2)), 0, 1), 1))))
</code>```

Body:
I'm trying to plot a stock price and some color above it as an indicator. The problem is that the color should not always be present which is indicated by having missing values, but using geom_ribbon causes the missing values to appear. The code is as follows:

library(dplyr)
library(ggplot2)
library(lubridate)
library(padr)

# Does not work correctly
ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = a, inherit.aes = FALSE, aes(
    x = Date2,
    ymin = Price * 1.1,
    ymax = Price,
    y = Price,
    fill = Group
  ), alpha = 0.9) +
  geom_line()

# Add missing values as NAs
b <- pad(a)

# Works but does not show colors
ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = b,
              inherit.aes = FALSE,
              aes(x = Date2,
                  ymin = Price * 1.1,
                  ymax = Price,
                  y = Price#,
                  #fill = Group
              ),
              alpha = 0.9) +
  geom_line()

# Does not work correctly because of the grouping caused by the fill
ggplot(x, aes(x = Date, y = Stock)) +
  geom_ribbon(data = b,
              inherit.aes = FALSE,
              aes(x = Date2,
                  ymin = Price * 1.1,
                  ymax = Price,
                  y = Price,
                  fill = Group),
              alpha = 0.9) +
  geom_line()

It should look like this but without the missing values being filled in:

Padding the missing values with NAs allows to not show the missing values. Only the following points should have a ribbon above them, but it seems impossible to color them:

If the ribbons are colored after padding, then the data get grouped and the missing values get interpolated, which should not be the case:

Data:

x <- structure(list(Date = structure(c(18213, 18214, 18215, 18218,
                                       18219, 18220, 18221, 18222, 18225, 18226, 18227, 18229, 18232,
                                       18233, 18234, 18235, 18236, 18239, 18240, 18241, 18242, 18243,
                                       18246, 18247, 18248, 18249, 18250, 18253, 18254, 18256, 18257,
                                       18260, 18261, 18263, 18264, 18267, 18268, 18269, 18270, 18271,
                                       18274, 18275, 18276, 18277, 18278, 18282, 18283, 18284, 18285,
                                       18288, 18289, 18290, 18291, 18292, 18295, 18296, 18297, 18298,
                                       18299, 18302, 18303, 18304, 18305, 18306, 18310, 18311, 18312,
                                       18313, 18316, 18317, 18318, 18319, 18320, 18324, 18325, 18326,
                                       18327, 18328, 18331, 18332, 18333, 18334, 18335, 18338, 18339,
                                       18340, 18341, 18342, 18345, 18346, 18347, 18348, 18349, 18352,
                                       18353, 18354, 18355, 18356, 18359, 18360, 18361, 18362, 18363,
                                       18366, 18367, 18368, 18369, 18373, 18374, 18375, 18376, 18377,
                                       18380, 18381, 18382, 18383, 18384, 18387, 18388, 18389, 18390,
                                       18391, 18394, 18395, 18396, 18397, 18398, 18401, 18402, 18403,
                                       18404, 18405, 18409, 18410, 18411, 18412, 18415, 18416, 18417,
                                       18418, 18419, 18422, 18423, 18424, 18425, 18426, 18429, 18430,
                                       18431, 18432, 18433, 18436, 18437, 18438, 18439, 18440, 18443,
                                       18444, 18445, 18446, 18450, 18451, 18452, 18453, 18454, 18457,
                                       18458, 18459, 18460, 18461, 18464, 18465, 18466, 18467, 18468,
                                       18471, 18472, 18473, 18474, 18475, 18478, 18479, 18480, 18481,
                                       18482, 18485, 18486, 18487, 18488, 18489, 18492, 18493, 18494,
                                       18495, 18496, 18499, 18500, 18501, 18502, 18503, 18507, 18508,
                                       18509, 18510, 18513, 18514, 18515, 18516, 18517, 18520, 18521,
                                       18522, 18523, 18524, 18527, 18528, 18529, 18530, 18531, 18534,
                                       18535, 18536, 18537, 18538, 18541, 18542, 18543, 18544, 18545,
                                       18548, 18549, 18550, 18551, 18552, 18555, 18556, 18557, 18558,
                                       18559, 18562, 18563, 18564, 18565, 18566, 18569, 18570, 18571,
                                       18572, 18573, 18576, 18577, 18578, 18579, 18580, 18583, 18584,
                                       18585, 18586, 18587, 18590, 18591, 18592, 18594, 18597, 18598,
                                       18599, 18600, 18601, 18604, 18605, 18606, 18607, 18608, 18611,
                                       18612, 18613, 18614, 18615, 18618, 18619, 18620, 18622, 18625,
                                       18626, 18627), class = "Date"),
                    Stock = c(32.302406, 32.783638,
                              32.753559, 33.084404, 32.723484, 32.377602, 32.663322, 32.994171,
                              32.422722, 32.332485, 32.106918, 32.00164, 31.445225, 31.294838,
                              31.249725, 30.738419, 31.580574, 31.881338, 31.941496, 31.460262,
                              31.370037, 31.430185, 31.685837, 31.730949, 32.531425, 32.305305,
                              32.320374, 32.893227, 32.817848, 32.516354, 32.697247, 32.531425,
                              33.013821, 31.883207, 31.702311, 31.084244, 31.219913, 31.129463,
                              30.963646, 31.536489, 31.023943, 31.38574, 31.476185, 31.219913,
                              31.250069, 31.280209, 31.415892, 30.692297, 30.119457, 30.07423,
                              29.938557, 29.305418, 29.606909, 29.395864, 28.265259, 29.124521,
                              29.471237, 30.05916, 30.104383, 29.938557, 30.330505, 30.360651,
                              30.119457, 30.360651, 29.998857, 28.958696, 29.486313, 29.712437,
                              29.727509, 29.245119, 28.732574, 28.476297, 27.767788, 25.627169,
                              26.727627, 26.622101, 26.079411, 25.883436, 25.868364, 25.20507,
                              24.858349, 23.652367, 24.662378, 25.551794, 26.667328, 25.868364,
                              25.159845, 25.597015, 25.597015, 26.652252, 26.652252, 26.727627,
                              26.546728, 27.828083, 28.024061, 27.767788, 27.390923, 27.481363,
                              26.893454, 27.044199, 27.285393, 26.953745, 27.225088, 26.365831,
                              25.627169, 26.184935, 27.601965, 26.652252, 26.411057, 26.395979,
                              25.702539, 24.978949, 26.184935, 26.124634, 26.139708, 26.139708,
                              25.732691, 25.883436, 26.380905, 27.737633, 27.782866, 27.586891,
                              27.933609, 28.325556, 28.702429, 28.370781, 28.642126, 29.395864,
                              29.682287, 30.013933, 29.727509, 30.044086, 29.410936, 29.471237,
                              29.712437, 29.214966, 29.426016, 29.772738, 29.848104, 29.953634,
                              30.345573, 30.436029, 30.541553, 30.782749, 30.390808, 30.601849,
                              30.194832, 30.209909, 30.963646, 30.707371, 30.586777, 30.888273,
                              31.491264, 31.226501, 30.954172, 31.075211, 30.606213, 30.454916,
                              31.271893, 31.302149, 31.196245, 31.211376, 31.710632, 31.150858,
                              31.105465, 30.863405, 30.575953, 30.969309, 28.89662, 29.501785,
                              28.836103, 29.123556, 28.972263, 29.093298, 28.639423, 28.20068,
                              27.610645, 28.351974, 27.882971, 27.928352, 28.125036, 27.232416,
                              26.521349, 26.808798, 27.550123, 27.958612, 27.746809, 28.246065,
                              28.397356, 28.291456, 28.881491, 29.138687, 29.108423, 29.516909,
                              28.972263, 29.486652, 29.380754, 29.229462, 28.321712, 27.686293,
                              27.338318, 29.00252, 28.715067, 29.380754, 29.00252, 28.730202,
                              29.032782, 28.20068, 28.185551, 27.535, 27.988873, 27.580385,
                              27.429092, 27.277803, 26.823933, 27.247543, 27.988873, 27.262672,
                              27.171898, 26.415443, 26.127991, 25.764894, 25.94644, 25.94644,
                              26.521349, 25.825409, 26.11286, 25.47744, 25.053823, 25.295889,
                              25.250502, 25.462311, 24.887403, 25.386665, 25.053823, 25.295889,
                              24.660463, 24.509178, 24.963047, 24.645338, 25.038694, 24.766371,
                              25.961571, 25.537954, 25.159723, 25.038694, 24.811756, 23.782978,
                              24.176332, 24.645338, 24.478916, 24.61508, 23.767849, 23.979656,
                              23.934263, 24.040173, 24.085558, 24.5243, 24.947918, 25.038694,
                              24.312494, 23.979656, 22.618032, 22.723938, 21.649773, 22.118773,
                              21.558996, 21.649773, 20.409182, 20.424313, 19.985567, 18.835754,
                              18.260849, 16.173027, 16.309193, 18.383097, 17.293257, 19.008156,
                              19.745405, 19.376781, 19.825541, 20.130056, 18.206802, 19.55308,
                              18.383097)),
               class = "data.frame", row.names = c(NA, -285L))

a <- structure(list(Price = c(16.173027, 16.309193, 25.627169, 26.079411,
                              25.883436, 25.868364, 25.20507, 24.858349, 23.652367, 24.662378,
                              25.551794, 25.868364, 25.159845, 25.597015, 25.597015, 26.365831,
                              25.627169, 26.184935, 26.411057, 26.395979, 25.702539, 24.978949,
                              26.184935, 26.124634, 26.139708, 26.139708, 25.732691, 25.883436,
                              26.380905, 26.415443, 26.127991, 25.764894, 25.94644, 25.94644,
                              25.825409, 26.11286, 25.47744, 25.053823, 25.295889, 25.250502,
                              25.462311, 24.887403, 25.386665, 25.053823, 25.295889, 24.660463,
                              24.509178, 24.963047, 24.645338, 25.038694, 24.766371, 25.961571,
                              25.537954, 25.159723, 25.038694, 24.811756, 23.782978, 24.176332,
                              24.645338, 24.478916, 24.61508, 23.767849, 23.979656, 23.934263,
                              24.040173, 24.085558, 24.5243, 24.947918, 25.038694, 24.312494,
                              23.979656, 22.618032, 22.723938, 21.649773, 22.118773, 21.558996,
                              21.649773, 20.409182, 20.424313, 19.985567, 18.835754, 18.260849,
                              18.383097, 17.293257, 19.008156, 19.745405, 19.376781, 19.825541,
                              20.130056, 18.206802, 19.55308, 18.383097),
                    Date2 = structure(c(18611,
                                        18612, 18324, 18327, 18328, 18331, 18332, 18333, 18334, 18335,
                                        18338, 18340, 18341, 18342, 18345, 18366, 18367, 18368, 18374,
                                        18375, 18376, 18377, 18380, 18381, 18382, 18383, 18384, 18387,
                                        18388, 18534, 18535, 18536, 18537, 18538, 18542, 18543, 18544,
                                        18545, 18548, 18549, 18550, 18551, 18552, 18555, 18556, 18557,
                                        18558, 18559, 18562, 18563, 18564, 18565, 18566, 18569, 18570,
                                        18571, 18572, 18573, 18576, 18577, 18578, 18579, 18580, 18583,
                                        18584, 18585, 18586, 18587, 18590, 18591, 18592, 18594, 18597,
                                        18598, 18599, 18600, 18601, 18604, 18605, 18606, 18607, 18608,
                                        18613, 18614, 18615, 18618, 18619, 18620, 18622, 18625, 18626,
                                        18627), class = "Date"),
                    Group = c("b", "b", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
                              "a", "a", "a", "a", "a", "a", "a", "a")),
               class = "data.frame", row.names = c(NA,
                                                   -92L))

Body:
Now that I understood the logic of teunbrand's answer, I came up with a much clearer solution:

b <- a %>%
  mutate(run = as.character(
    cumsum(ifelse(Date2 - lag(Date2) <= 1 | is.na(lag(Date2)),
                  ifelse(lag(Group) == Group | is.na(lag(Date2)), 0, 1), 1))))


问:
summing probability elements based on specific conditions from its associated list python
I have 4 lists and 2 of them have a one to one association, and Im using the other two to impose conditions. The A list is just a lists of things, while B is a list of 'probabilities' associated with each element in A. for example, this first element in A, [a, b] is associated with the first element in B, [0.12].

```<code>A=[[a, b], [c, f], [g, m], [h, j], [c, f], [g, m]]
B=[[0.12], [0.45], [0.98], [0.56], [0.34], [0.78]]
</code>```

The third and fourth list (C, D) is as fallows,

```<code>C=[[c, f], [g, m]]
D=[[[a, b], [h, j]]
</code>```

and Id like to iterate thru A to find all the common elements between A and C and put them into nested list inside E, and do the same for A and D (and put them inside another list in E). As well, sum up there associated probabilities into nested list in F. The code Ive worked out is as fallows,

```<code>E=[[ ], [ ]]
F=[[0], [0]]
for i in A:
    if i in C:
        E[0].append(i)
        F[0]= F[0]+ B[i]
for i in A:
    if i in D:
        E[1].append(i)
        F[1]= F[1]+B[i]
</code>```

This gives out an error for "indices must be integers, not list". Im not sure what the problem is exactly, or if there is a better overall approach to the probability problem, but Id appreciate some help/advice.

答:
I was able to understand what you're trying to do after reading carefully.

You can use this for your task :

```<code>A=[['a', 'b'], ['c', 'f'], ['g', 'm'], ['h', 'j'], ['c', 'f'], ['g', 'm']]
B=[[0.12], [0.45], [0.98], [0.56], [0.34], [0.78]]

C=[['c', 'f'], ['g', 'm']]
D=[['a', 'b'], ['h', 'j']]

# E=[[ ], [ ]]
# F=[[0], [0]]

common_in_a_c = {'lists_found': [], 'probabilities': []}
common_in_a_d = {'lists_found': [], 'probabilities': []}

for i in range(len(A)):
    if A[i] in C:
        common_in_a_c['lists_found'].append(A[i])
        common_in_a_c['probabilities'].append(B[i][0])
    if A[i] in D:
        common_in_a_d['lists_found'].append(A[i])
        common_in_a_d['probabilities'].append(B[i][0])

print(common_in_a_c)
print(common_in_a_d)
</code>```

I have removed E and F which you had used . Made of of better approach to store what you desire - common_in_a_c and common_in_a_d are dictionaries - which will hold values of your interest.
Here I have stored entire list in common_in_a_c['probabilities'] => on which you can perform sum() to get sum of probabilities

The problem in your code was the B[i] what you had used in the loop => B[i] won't work because i wasn't index but was elements in A  . So I have changed the logic to use index-based-loop .

Body:
I have 4 lists and 2 of them have a one to one association, and Im using the other two to impose conditions. The A list is just a lists of things, while B is a list of 'probabilities' associated with each element in A. for example, this first element in A, [a, b] is associated with the first element in B, [0.12].

A=[[a, b], [c, f], [g, m], [h, j], [c, f], [g, m]]
B=[[0.12], [0.45], [0.98], [0.56], [0.34], [0.78]]

The third and fourth list (C, D) is as fallows,

C=[[c, f], [g, m]]
D=[[[a, b], [h, j]]

and Id like to iterate thru A to find all the common elements between A and C and put them into nested list inside E, and do the same for A and D (and put them inside another list in E). As well, sum up there associated probabilities into nested list in F. The code Ive worked out is as fallows,

E=[[ ], [ ]]
F=[[0], [0]]
for i in A:
    if i in C:
        E[0].append(i)
        F[0]= F[0]+ B[i]
for i in A:
    if i in D:
        E[1].append(i)
        F[1]= F[1]+B[i]

This gives out an error for "indices must be integers, not list". Im not sure what the problem is exactly, or if there is a better overall approach to the probability problem, but Id appreciate some help/advice.

Body:
I was able to understand what you're trying to do after reading carefully.

You can use this for your task :

A=[['a', 'b'], ['c', 'f'], ['g', 'm'], ['h', 'j'], ['c', 'f'], ['g', 'm']]
B=[[0.12], [0.45], [0.98], [0.56], [0.34], [0.78]]

C=[['c', 'f'], ['g', 'm']]
D=[['a', 'b'], ['h', 'j']]

# E=[[ ], [ ]]
# F=[[0], [0]]

common_in_a_c = {'lists_found': [], 'probabilities': []}
common_in_a_d = {'lists_found': [], 'probabilities': []}

for i in range(len(A)):
    if A[i] in C:
        common_in_a_c['lists_found'].append(A[i])
        common_in_a_c['probabilities'].append(B[i][0])
    if A[i] in D:
        common_in_a_d['lists_found'].append(A[i])
        common_in_a_d['probabilities'].append(B[i][0])

print(common_in_a_c)
print(common_in_a_d)

I have removed E and F which you had used . Made of of better approach to store what you desire - common_in_a_c and common_in_a_d are dictionaries - which will hold values of your interest.
Here I have stored entire list in common_in_a_c['probabilities'] => on which you can perform sum() to get sum of probabilities

The problem in your code was the B[i] what you had used in the loop => B[i] won't work because i wasn't index but was elements in A  . So I have changed the logic to use index-based-loop .


问:
How to select almost equally distributed classes in training,validation,test samples?
I am working on MNIST Sign Language dataset to classify images using Keras. There are 24 different classes in the dataset. But the problem is that the distribution of classes of very different.

I used sklearn.model_selection.train_test_split to stratify=df['label'] but still some classes have 5% while others have 3% of the whole data. How can I make them to choose a data that is around 4% distributed among the classes.

My test_df has 7172 rows and 785 columns one of which is a label column and remaining 784 are grayscale pixel values (28*28)

```<code>test_df = pd.read_csv(TEST_PATH)

# shuffle and split validation,test data
test_df = test_df.sample(frac=1.0,random_state=SEED).iloc[:2000,:] # shuffle the whole data, get first 2000 rows
val_df,test_df = train_test_split(test_df,test_size=0.5,random_state=SEED,stratify=test_df['label'])
# stratify the labels so that distribution of classes is almost same

# extract pixels and labels for both validation,test data
X_val = val_df.drop('label',axis=1).values.reshape((val_df.shape[0],28,28))/255.0 # validation images
y_val = val_df['label'].ravel() # validation label

X_test = test_df.drop('label',axis=1).values.reshape((test_df.shape[0],28,28))/255.0 # test images
y_val = test_df['label'].ravel() # test label
</code>```

答:
this line enables you to have a uniform distribution with val and test. you can play also with the number of samples</p>

```<code>SEED = 42
n_classes = 24

test_df = pd.read_csv(TEST_PATH)

test_df = [test_df.loc[test_df.label==i].sample(n=int(2000/n_classes),random_state=SEED) for i in test_df.label.unique()]
test_df = pd.concat(test_df, axis=0, ignore_index=True)

val_df,test_df = train_test_split(test_df,test_size=0.5,random_state=SEED,stratify=test_df['label'])
</code>```

Body:
I am working on MNIST Sign Language dataset to classify images using Keras. There are 24 different classes in the dataset. But the problem is that the distribution of classes of very different.

I used sklearn.model_selection.train_test_split to stratify=df['label'] but still some classes have 5% while others have 3% of the whole data. How can I make them to choose a data that is around 4% distributed among the classes.

My test_df has 7172 rows and 785 columns one of which is a label column and remaining 784 are grayscale pixel values (28*28)

test_df = pd.read_csv(TEST_PATH)

# shuffle and split validation,test data
test_df = test_df.sample(frac=1.0,random_state=SEED).iloc[:2000,:] # shuffle the whole data, get first 2000 rows
val_df,test_df = train_test_split(test_df,test_size=0.5,random_state=SEED,stratify=test_df['label'])
# stratify the labels so that distribution of classes is almost same

# extract pixels and labels for both validation,test data
X_val = val_df.drop('label',axis=1).values.reshape((val_df.shape[0],28,28))/255.0 # validation images
y_val = val_df['label'].ravel() # validation label

X_test = test_df.drop('label',axis=1).values.reshape((test_df.shape[0],28,28))/255.0 # test images
y_val = test_df['label'].ravel() # test label

Body:
this line enables you to have a uniform distribution with val and test. you can play also with the number of samples

SEED = 42
n_classes = 24

test_df = pd.read_csv(TEST_PATH)

test_df = [test_df.loc[test_df.label==i].sample(n=int(2000/n_classes),random_state=SEED) for i in test_df.label.unique()]
test_df = pd.concat(test_df, axis=0, ignore_index=True)

val_df,test_df = train_test_split(test_df,test_size=0.5,random_state=SEED,stratify=test_df['label'])


问:
Rendering an atmosphere around a planet with shading
I have a made a planet and wanted to make an atmosphere around it. So I was referring to this site:

Click to visit site

I don't understand this:

As with the lookup table proposed in Nishita et al. 1993, we can get the optical depth for the ray to the sun from any sample point in the atmosphere. All we need is the height of the sample point (x) and the angle from vertical to the sun (y), and we look up (x, y) in the table. This eliminates the need to calculate one of the out-scattering integrals. In addition, the optical depth for the ray to the camera can be figured out in the same way, right? Well, almost. It works the same way when the camera is in space, but not when the camera is in the atmosphere. That's because the sample rays used in the lookup table go from some point at height x all the way to the top of the atmosphere. They don't stop at some point in the middle of the atmosphere, as they would need to when the camera is inside the atmosphere.

Fortunately, the solution to this is very simple. First we do a lookup from sample point P to the camera to get the optical depth of the ray passing through the camera to the top of the atmosphere. Then we do a second lookup for the same ray, but starting at the camera instead of starting at P. This will give us the optical depth for the part of the ray that we don't want, and we can subtract it from the result of the first lookup. Examine the rays starting from the ground vertex (B 1) in Figure 16-3 for a graphical representation of this.

First Question - isn't optical depth dependent on how you see that is, on the viewing angle? If yes, the table just gives me the optical depth of the rays going from land to the top of the atmosphere in a straight line. So what about the case where the rays pierce the atmosphere to reach the camera? How do I get the optical depth in this case?

Second Question - What is the vertical angle it is talking about...like, is it the same as the angle with the z-axis as we use in polar coordinates?

Third Question - The article talks about scattering of the rays going to the sun..shouldn't it be the other way around? like coming from the sun to a point?

Any explanation on the article or on my questions will help a lot.

Thanks in advance!

答:
I am no expert in the matter but have played with Atmospheric scattering and various physical and optical simulations. I strongly recommend to look at this:

my VEEERRRYYY Simplified version of atmospheric scattering in GLSL

It odes not do the full volume intergration but just linear path integration along the ray and does only the Rayleight scatering with isotropic coefficients. As you can see its still good enough.

In real scattering the viewing angle is impacting the real scattering  equation as the scattering coefficients are different in different angles (against main light source and viewer) So answer to your first question is Yes it does.

Not sure what you are refer to in your second question. The scattering itself is dependent on angle between light source, particle and camera. That lies on arbitrary plane. However if the Earth surface is accounted to the equation too then its dependent on the horizontal and vertical angles (against terrain) so azimuth,elevation as usually more light is reflected when camera is facing sun (azimuth) and the reflected rays are closer to your elevation. So my guess is that's what the horizontal angle is about accounting for reflected light from the surface.

To answer your 3th question is called back ray tracing. You can cast rays both ways (from camera or from sun) however if you start from light source you do not know which way to go to hit a pixel on camera screen so you need to cast a lot of rays to increase the probability of hit enough to fill the screen which is too slow and inaccurate (produce holes). If you start from screen pixel then you cast just single or per wavelength ray instead which is much much faster. The resulting color is the same.

[Edit1] vertical angle

OK I read the linked topic a bit and this is How I understand it:

So its just angle between surface normal and the casted ray. Its scaled so vert.angle=0 means that ray and normal are the same and vert.angle=1 means the are opposite directions.


问:
$(document).ready() doesn't run after f:ajax rendering
I have some jQuery code that runs on every $(document).ready() event.
I also have some <f:ajax> tags that do re-rendering of some parts of the page.

I noticed that when I rerender a component, the $(document).ready() doesn't get called.

Is there a way to run javascript code after every <f:ajax> rerendering?
(I can technically use the onevent tag but that's the unfavourable solution to me since I will have to call the function on every <f:ajax>)

Just for illustration, this is the jQuery Code:

```<code>$(document).ready(function() { ...CODE... }
</code>```

And the JSF code:

```<code><f:ajax event="click" render=":someComponentID"/>
</code>```

Thanks!

UPDATE This question might be a duplicate of this one. I'm looking into it.

UPDATE2
This is easily solvable using the JSF Javascript event binding:

```<code>jsf.ajax.addOnEvent(function(data){
  if (data.status === 'success') {
    // Do stuff here
  }
}
</code>```

Read more about it under the JSF 2 reference

答:
The $(document).ready() event fires once, when the DOM has fully loaded from the initial load.

An ajax event may change the DOM, but it doesn't reload the page, so the $(document).ready() won't fire again.

I noticed the way Google solved this problem in Gmail is by using a high frequency timer.

ContentLicense: CC BY-SA 3.0

Body:
I have some jQuery code that runs on every $(document).ready() event.
I also have some <f:ajax> tags that do re-rendering of some parts of the page.

I noticed that when I rerender a component, the $(document).ready() doesn't get called.

Is there a way to run javascript code after every <f:ajax> rerendering?
(I can technically use the onevent tag but that's the unfavourable solution to me since I will have to call the function on every <f:ajax>)

Just for illustration, this is the jQuery Code:

$(document).ready(function() { ...CODE... }

And the JSF code:

<f:ajax event="click" render=":someComponentID"/>

Thanks!

UPDATE This question might be a duplicate of this one. I'm looking into it.

UPDATE2
This is easily solvable using the JSF Javascript event binding:

jsf.ajax.addOnEvent(function(data){
  if (data.status === 'success') {
    // Do stuff here
  }
}

Read more about it under the JSF 2 reference


问:
C console application print out $ dollar sign
This is my program:

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

int main ()
{
    printf("%c", "\n");
}
</code>```

I need to know why is it printing $ on the output,

答: you’re passing a pointer to an argument taking a char. the dollar is probably the most significant byte  value of your pointer.</p>

Body:
This is my program:

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

int main ()
{
    printf("%c", "\n");
}

I need to know why is it printing $ on the output,


问:
R summary function within cat function causing issues
I'm having trouble printing out the summary of linear regression model with cat function to a text file. The line of code producing the error

```<code>cat(summary(model), file = "results.txt", sep = "\n")
</code>```

This line produces an error:

argument 1 (type 'list') cannot be handled by 'cat'

I can't figure out a way to fix this. What is causing this?

答:
Here is a capture.output solution.

```<code>model <- lm(Sepal.Length ~ Sepal.Width, iris)
capture.output(summary(model), file = "results.txt")
</code>```

Body:
I'm having trouble printing out the summary of linear regression model with cat function to a text file. The line of code producing the error

cat(summary(model), file = "results.txt", sep = "\n")

This line produces an error:

argument 1 (type 'list') cannot be handled by 'cat'

I can't figure out a way to fix this. What is causing this?

Body:
Here is a capture.output solution.

model <- lm(Sepal.Length ~ Sepal.Width, iris)
capture.output(summary(model), file = "results.txt")


问:
backup distributed cache data to cloud storage
I want to backup the REDIS data on google storage bucket as flat file, is there any existing utility to do that?
Although, I do not fully agree to idea of backing up of cache data on cloud. I was wondering if there is any existing utility rather than reinventing the wheel.</p>

答:
If you are using Cloud Memorystore for Redis you can simply refer to the following documentation. Notice that you can simply use the following gcloud command:

gcloud redis instances export gs://[BUCKET_NAME]/[FILE_NAME].rdb [INSTANCE_ID] --region=[REGION] --project=[PROJECT_ID]

or use the Export operation from the Cloud Console.

If you manage your own instance (e.g. you have the Redis instance hosted on a Compute Engine Instance) you could simply use the SAVE or BGSAVE (preferred) commands to take a snapshot of the instance and then upload the .rdb file to Google Cloud Storage using any of the available methods, from which I think the most convenient one would be gsutil (notice that it will require the following installation procedure) in a similar fashion to:

gsutil cp path/to/your-file.rdb gs://[DESTINATION_BUCKET_NAME]/


问:
blackberry chat implementation
I'm currently developing an chat client for Blackberry. What is the best way to display the chat messages? I'm looking for something that can look similar to the iPhone chat look.</p>

答:
If you're trying to implement iPhone-esque look and feel, take a look at Implement advanced buttons, fields, and managers in the BB knowledge base (about 2/3 down). You can go further and add an image to the sides of the chat bubbles if you'll be having multiple people in it.

That KB article is actually a good read to help you learn about custom layouts in general.

Title: blackberry chat implementation


问:
How to make a variable that can be detected as multiple items
```<code>bot.on('message', msg=>{
   if(msg.content.includes(“hi” or? “hello” or? “hey”)){
      msg.reply(“hi” or? “hello” or? “hey”);
   }
})
</code>```

I’m coding a discord bot on my server and I want to add a feature where it responds to people. I want to make it so that my bot can detect multiple ways a user greets and have it greet them back.

答:
How to check if a string contains text from an array of substrings in JavaScript?</p>

```<code>if (substrings.some(v => str.includes(v))) {
    // There's at least one
}
</code>```

Body:
bot.on('message', msg=>{
   if(msg.content.includes(“hi” or? “hello” or? “hey”)){
      msg.reply(“hi” or? “hello” or? “hey”);
   }
})

I’m coding a discord bot on my server and I want to add a feature where it responds to people. I want to make it so that my bot can detect multiple ways a user greets and have it greet them back.

Body:
How to check if a string contains text from an array of substrings in JavaScript?

if (substrings.some(v => str.includes(v))) {
    // There's at least one
}


问:
Unity doesn't generate xcworkspace, Podfile using iOS Resolver - macOS Catalina
I'm trying to build an iOS project in Unity 2019.3.14f1 but when I tried to install CocoaPods using /Assets/External Dependency Manager/Install Cocoapods it returns a warning:

```<code>gem install cocoa pods --user-install
WARNING: You don't have /Users/danieoquelis/.gem/ruby/2.6.0/bin in your PATH,
gem executables will not run

.....
Done installing documentation for cocoapods after 2 seconds
1 gem installed
</code>```

I went to my terminal which uses '.zsh' and I added the PATH in the file '.zshrc' and '.zshenv' then I did 'source' to the files and restarted Unity to try to build the project again but it only generates xcodeproject without xcworkspace or Podfile.

I tried to uninstall cocoapods from the terminal and I installed it again and from the terminal the warning doesn't appear and when I do 'echo $PATH' it returns:

/Users/danieloquelis/.gem/ruby/2.6.0/bin:/Users/danieloquelis/Library/Android/flutter/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

I don't know if Unity doesn't read the shell based on 'zsh' and it only works with 'bash' I update iOS Resolver, Cocoa Pods, Unity, I don't know what else I can do.

CocoaPods 1.9.3
Ruby 2.6.0
iOS Resolver 1.2.155
Unity 2019.3.14f1
macOS Catalina 10.15

Thanks for your help!!!

答: I fixed it, I forgot to add ARKit Unity Plugin.</p>

Body:
I'm trying to build an iOS project in Unity 2019.3.14f1 but when I tried to install CocoaPods using /Assets/External Dependency Manager/Install Cocoapods it returns a warning:

gem install cocoa pods --user-install
WARNING: You don't have /Users/danieoquelis/.gem/ruby/2.6.0/bin in your PATH,
gem executables will not run

.....
Done installing documentation for cocoapods after 2 seconds
1 gem installed

I went to my terminal which uses '.zsh' and I added the PATH in the file '.zshrc' and '.zshenv' then I did 'source' to the files and restarted Unity to try to build the project again but it only generates xcodeproject without xcworkspace or Podfile.

I tried to uninstall cocoapods from the terminal and I installed it again and from the terminal the warning doesn't appear and when I do 'echo $PATH' it returns:

/Users/danieloquelis/.gem/ruby/2.6.0/bin:/Users/danieloquelis/Library/Android/flutter/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

I don't know if Unity doesn't read the shell based on 'zsh' and it only works with 'bash' I update iOS Resolver, Cocoa Pods, Unity, I don't know what else I can do.

CocoaPods 1.9.3
Ruby 2.6.0
iOS Resolver 1.2.155
Unity 2019.3.14f1
macOS Catalina 10.15

Thanks for your help!!!


Title: Forward fill NaN by id up to last value and 2 more times after last value

Body:
Task

I have the following df and want to forward fill the NaN by each id with the previous value. On the last value in each column, I want it to only forward fill 2 more times.

I currently use this:

df.set_index(['id', 'date'], inplace=True)
df = df.ffill().where(df.bfill().notnull())

However, it forward fills more than the desired 2 times. Any help would be super!

df

   id      date     compname   fyr
1   1   2016-02-29     a        5.0
2   1   2016-03-31    NaN       NaN
3   1   2016-04-30    NaN       NaN
4   1   2016-05-31     a        5.0
5   1   2016-06-30    NaN       NaN
6   1   2016-07-31    NaN       NaN
7   1   2016-08-31     a        5.0
8   1   2016-09-30    NaN       NaN
9   1   2016-10-31    NaN       NaN
10  1   2016-11-30     a        5.0
11  1   2016-12-31    NaN       NaN
12  1   2017-01-31    NaN       NaN
13  1   2017-02-28    NaN       NaN
14  1   2017-03-31    NaN       NaN
15  2   2016-02-29     b        5.0
16  2   2016-03-31    NaN       NaN
17  2   2016-04-30    NaN       NaN
18  2   2016-05-31     b        5.0
19  2   2016-06-30    NaN       NaN
20  2   2016-07-31    NaN       NaN
21  2   2016-08-31     b        5.0
22  2   2016-09-30    NaN       NaN
23  2   2016-10-31    NaN       NaN
24  2   2016-11-30     b        5.0
25  2   2016-12-31    NaN       NaN
26  2   2017-01-31    NaN       NaN
27  2   2017-02-28    NaN       NaN
28  2   2017-03-31    NaN       NaN

desired df

   id      date     compname   fyr
1   1   2016-02-29     a       5.0
2   1   2016-03-31     a       5.0
3   1   2016-04-30     a       5.0
4   1   2016-05-31     a       5.0
5   1   2016-06-30     a       5.0
6   1   2016-07-31     a       5.0
7   1   2016-08-31     a       5.0
8   1   2016-09-30     a       5.0
9   1   2016-10-31     a       5.0
10  1   2016-11-30     a       5.0
11  1   2016-12-31     a       5.0
12  1   2017-01-31     a       5.0
13  1   2017-02-28    NaN      NaN
14  1   2017-03-31    NaN      NaN
15  2   2016-02-29     b       5.0
16  2   2016-03-31     b       5.0
17  2   2016-04-30     b       5.0
18  2   2016-05-31     b       5.0
19  2   2016-06-30     b       5.0
20  2   2016-07-31     b       5.0
21  2   2016-08-31     b       5.0
22  2   2016-09-30     b       5.0
23  2   2016-10-31     b       5.0
24  2   2016-11-30     b       5.0
25  2   2016-12-31     b       5.0
26  2   2017-01-31     b       5.0
27  2   2017-02-28    NaN      NaN
28  2   2017-03-31    NaN      NaN

Body:
One way is to first do a groupby and bfill, then another groupby with fillna:

df[["compname","fyr"]] = df.groupby("id")["compname","fyr"].bfill()

print (df.groupby("id").apply(lambda d: d.fillna(d.shift(2))))

    id        date compname  fyr
1    1  2016-02-29        a  5.0
2    1  2016-03-31        a  5.0
3    1  2016-04-30        a  5.0
4    1  2016-05-31        a  5.0
5    1  2016-06-30        a  5.0
6    1  2016-07-31        a  5.0
7    1  2016-08-31        a  5.0
8    1  2016-09-30        a  5.0
9    1  2016-10-31        a  5.0
10   1  2016-11-30        a  5.0
11   1  2016-12-31        a  5.0
12   1  2017-01-31        a  5.0
13   1  2017-02-28      NaN  NaN
14   1  2017-03-31      NaN  NaN
15   2  2016-02-29        b  5.0
16   2  2016-03-31        b  5.0
17   2  2016-04-30        b  5.0
18   2  2016-05-31        b  5.0
19   2  2016-06-30        b  5.0
20   2  2016-07-31        b  5.0
21   2  2016-08-31        b  5.0
22   2  2016-09-30        b  5.0
23   2  2016-10-31        b  5.0
24   2  2016-11-30        b  5.0
25   2  2016-12-31        b  5.0
26   2  2017-01-31        b  5.0
27   2  2017-02-28      NaN  NaN
28   2  2017-03-31      NaN  NaN


问:
Why is my Spring Batch multi-threaded step executing all reads before any processing?
I'm attempting to write a Spring Batch process for converting millions of entries in a legacy DB, with a sprawling schema, into a streamlined JSON format and publishing that JSON to GCP PubSub. In order to make this process as efficient as possible, I'm attempting to leverage a Spring-Batch multi-threaded Step.

To test my process, I've started small, with a page size and chunk size of 5, a limit of 20 entries total to process, and a thread-pool of just 1 thread. I'm attempting to step through the process to validate it's working as I expected - but it's not.

I expected that configuring my RepositoryItemReader with a page size of 5, would cause it to read just 5 records from the DB - and then process those records in a single chunk of 5 before reading the next 5. But that's not what's happening. Instead, in the logs, since I have hibernate show-sql enabled, I can see the reader reads ALL 20 records before any processing starts.

Why is my multithreaded step peforming ALL of its reading before executing any processing? Have I misconfigured it? Obviously I wouldn't want my Job trying to load millions of DTOs into memory before it starts processing anything...

Here's how I've configured my job:

@Configuration
public class ConversionBatchJobConfig {

    @Bean
    public SimpleCompletionPolicy processChunkSize(@Value("${commit.chunk.size:5}") Integer chunkSize) {
        return new SimpleCompletionPolicy(chunkSize);
    }

    @Bean
    @StepScope
    public ItemStreamReader<DbProjection> dbReader(
            MyDomainRepository myDomainRepository,
            @Value("#{jobParameters[pageSize]}") Integer pageSize, //pageSize and chunkSize both 5 for now
            @Value("#{jobParameters[limit]}") Integer limit) { //limit is 40
        RepositoryItemReader<DbProjection> myDomainRepositoryReader = new RepositoryItemReader<>();
        myDomainRepositoryReader.setRepository(myDomainRepository);
        myDomainRepositoryReader.setMethodName("findActiveDbDomains"); //A native query
        myDomainRepositoryReader.setArguments(new ArrayList<Object>() {{
            add("ACTIVE");
        }});
        myDomainRepositoryReader.setSort(new HashMap<String, Sort.Direction>() {{
            put("update_date", Sort.Direction.ASC);
        }});
        myDomainRepositoryReader.setPageSize(pageSize);
        myDomainRepositoryReader.setMaxItemCount(limit);
        myDomainRepositoryReader.setSaveState(false);
        return myDomainRepositoryReader;
    }

    @Bean
    @StepScope
    public ItemProcessor<DbProjection, JsonMessage> dataConverter(DataRetrievalService dataRetrievalService) {
        return new DbProjectionToJsonMessageConverter(dataRetrievalService);
    }

    @Bean
    @StepScope
    public ItemWriter<JsonMessage> jsonPublisher(GcpPubsubPublisherService publisherService) {
        return new JsonMessageWriter(publisherService);
    }

    @Bean
    public Step conversionProcess(SimpleCompletionPolicy processChunkSize,
                                  ItemStreamReader<DbProjection> dbReader,
                                  ItemProcessor<DbProjection, JsonMessage> dataConverter,
                                  ItemWriter<JsonMessage> jsonPublisher,
                                  StepBuilderFactory stepBuilderFactory,
                                  TaskExecutor conversionThreadPool,
                                  @Value("${conversion.failure.limit:20}") int maximumFailures) {
        return stepBuilderFactory.get("conversionProcess")
                .<DbProjection, JsonMessage>chunk(processChunkSize)
                .reader(dbReader)
                .processor(dataConverter)
                .writer(jsonPublisher)
                .faultTolerant()
                .skipPolicy(new MyCustomConversionSkipPolicy(maximumFailures))
                            //  ^ for now this returns true for everything until 20 failures
                    .listener(new MyConversionSkipListener(processStatus))
                              //  ^ for now this just logs the error
                .taskExecutor(conversionThreadPool)
                .build();
    }

    @Bean
    public Job conversionJob(Step conversionProcess,
                             JobBuilderFactory jobBuilderFactory) {
        return jobBuilderFactory.get("conversionJob")
                .start(conversionProcess)
                .build();
    }
}
```

答:
You need to check the value of hibernate.jdbc.fetch_size and set it accordingly.

The pageSize and fetchSize are different parameters. You can find more details on the difference here: https://stackoverflow.com/a/58058009/5019386. So in your case, if the fetchSize is bigger than pageSize, then it's possible that more records are fetched than the page size.


问:
How to show a triangle image in an html tree
I am trying to use this tree:
https://www.w3schools.com/howto/howto_js_treeview.asp
Can someone tell me what part of the code shows the triangle image being shown with every expandable li? I would like to reduce the size of it, but I cant figure out where it is coming from.</p>

答:
It's the part:

.caret::before {
  content: "\25B6";
  color: black;
  display: inline-block;
  margin-right: 6px;
}
```

The \25B6 is the unicode for the triangle.

Body:
It's the part:

.caret::before {
  content: "\25B6";
  color: black;
  display: inline-block;
  margin-right: 6px;
}

The \25B6 is the unicode for the triangle.


问:
Flutter app, Theme.of(context) style doesn't work on text
Trying to apply a text color to my header tiles in my list view. I define a headline text style with red text color (yes I have also tried this with Colors.red).

When I create the tile, I use the function _headerTile. Trying to load the style via Theme.of(context).textTheme.headline. However the text does not use ANY of the three properties I have defined in headline when I do this.

Is there something I'm doing wrong?

```<code>class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'App',
        home: Scaffold(
            appBar: AppBar(
                title: const Text(
              'App',
            )),
            body: _buildList(context)),
        theme: ThemeData(
          textTheme: TextTheme(
            headline: TextStyle(
                color: Color.fromRGBO(255, 0, 0, 1),
                fontSize: 72.0,
                fontWeight: FontWeight.bold),
          ),
        ));
  }
}

Widget _buildList(BuildContext context) => ListView(
      children: [
        _headerTile(context, "About Us")
      ],
    );

ListTile _headerTile(BuildContext context, String title) => ListTile(
      title: Text(title,
          style: Theme.of(context)
              .textTheme
              .headline
      contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 8),
    );
</code>```

答: Remove MaterialApp that exists after your build. Then it won't overwrite your customization.

Body:
Trying to apply a text color to my header tiles in my list view. I define a headline text style with red text color (yes I have also tried this with Colors.red).

When I create the tile, I use the function _headerTile. Trying to load the style via Theme.of(context).textTheme.headline. However the text does not use ANY of the three properties I have defined in headline when I do this.

Is there something I'm doing wrong?

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'App',
        home: Scaffold(
            appBar: AppBar(
                title: const Text(
              'App',
            )),
            body: _buildList(context)),
        theme: ThemeData(
          textTheme: TextTheme(
            headline: TextStyle(
                color: Color.fromRGBO(255, 0, 0, 1),
                fontSize: 72.0,
                fontWeight: FontWeight.bold),
          ),
        ));
  }
}

Widget _buildList(BuildContext context) => ListView(
      children: [
        _headerTile(context, "About Us")
      ],
    );

ListTile _headerTile(BuildContext context, String title) => ListTile(
      title: Text(title,
          style: Theme.of(context)
              .textTheme
              .headline
      contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 8),
    );


答: When you inspect carrets, you will see caret-down when expended and just carret on initial state. In css inspector you should see, .caret-down::before and .caret::before css. These are the css that draws triangles.</p>

Title: How to show a triangle image in an html tree

Body:
I am trying to use this tree:
https://www.w3schools.com/howto/howto_js_treeview.asp
Can someone tell me what part of the code shows the triangle image being shown with every expandable li? I would like to reduce the size of it, but I cant figure out where it is coming from.


问:
Form Closing on X C#
After an immense amount of reasearch I am still no closer to a solution.

I have a simple app, 2 forms .NETv3.5 C#.

The application loads the first form, i press 'X' on the windows bar.

The application hides the form and continues to run without exiting the application even though the only form loaded is closed.

The main form Closing method does not get called when the X is pressed because the form is hidden when the x is pressed. Form.Deactivated is called but not Form.Closing.

How can i catch the the even when the X is pressed ? (Then I can implement Application.Exit())

答: Check if you have timer or thread that is running. If so stop those.</p>

Title: Form Closing on X C#


问:
listbox click vba excel
I have in an excel worksheet data arranged like this

I want to write a code to show the équipements (Column A to G) in a listbox and when I click on the equipement name I have the list of persons who know how to use the equipement to be displayed in another listox, the 2 listboxes are in the same userform.

I wrote this code but I didnt kow how to finish it : what should I write after additem ?

```<code>Private Sub ListBox_Equip_Click()

    fin_liste_equip = ws_Liste_Equip.Cells(1, 256).End(xlToLeft).Column

    curVal = Me.ListBox_Equip.Value
    For x = 2 To fin_liste_equip
        If ws_Liste_Equip.Cells(x, fin_liste_equip) = curVal Then
            Me.ListBox_Pers_Mait.AddItem ws_Liste_Equip
        End

 If
Next x
</code>```

you can see in the following picture that  ive only known how to populate my 1st listbox

答:
Or maybe something like this ?

```<code>Private Sub ListBox_Equip_Click()
'fin_liste_equip = ws_Liste_Equip.Cells(1, 256).End(xlToLeft).Column
curVal = Me.ListBox_Equip.Value
Set c = Rows(1).Find(curVal, lookat:=xlWhole)
If c.Offset(1, 0).Value = "" Then Exit Sub

Set Rng = Range(c.Offset(1, 0), c.End(xlDown))
ListBox_Pers_Mait.Clear
If Rng.Rows.Count = 1 Then ListBox_Pers_Mait.AddItem c.Offset(1, 0).Value
If Rng.Rows.Count > 1 Then ListBox_Pers_Mait.List = Application.Transpose(Rng)

'For x = 2 To fin_liste_equip
'    If ws_Liste_Equip.Cells(x, fin_liste_equip) = curVal Then
'        Me.ListBox_Pers_Mait.AddItem ws_Liste_Equip
'    End
End Sub
</code>```

The code assumes that there won't be a duplicate équipements name across the column of row 1. Or in other words, your ListBox_Equip item is unique.

Title: listbox click vba excel

Body:
I have in an excel worksheet data arranged like this

I want to write a code to show the équipements (Column A to G) in a listbox and when I click on the equipement name I have the list of persons who know how to use the equipement to be displayed in another listox, the 2 listboxes are in the same userform.

I wrote this code but I didnt kow how to finish it : what should I write after additem ?

Private Sub ListBox_Equip_Click()

    fin_liste_equip = ws_Liste_Equip.Cells(1, 256).End(xlToLeft).Column

    curVal = Me.ListBox_Equip.Value
    For x = 2 To fin_liste_equip
        If ws_Liste_Equip.Cells(x, fin_liste_equip) = curVal Then
            Me.ListBox_Pers_Mait.AddItem ws_Liste_Equip
        End

 If
Next x

you can see in the following picture that  ive only known how to populate my 1st listbox

Body:
Or maybe something like this ?

Private Sub ListBox_Equip_Click()
'fin_liste_equip = ws_Liste_Equip.Cells(1, 256).End(xlToLeft).Column
curVal = Me.ListBox_Equip.Value
Set c = Rows(1).Find(curVal, lookat:=xlWhole)
If c.Offset(1, 0).Value = "" Then Exit Sub

Set Rng = Range(c.Offset(1, 0), c.End(xlDown))
ListBox_Pers_Mait.Clear
If Rng.Rows.Count = 1 Then ListBox_Pers_Mait.AddItem c.Offset(1, 0).Value
If Rng.Rows.Count > 1 Then ListBox_Pers_Mait.List = Application.Transpose(Rng)

'For x = 2 To fin_liste_equip
'    If ws_Liste_Equip.Cells(x, fin_liste_equip) = curVal Then
'        Me.ListBox_Pers_Mait.AddItem ws_Liste_Equip
'    End
End Sub

The code assumes that there won't be a duplicate équipements name across the column of row 1. Or in other words, your ListBox_Equip item is unique.


问:
Python Creating Person Class and loading function from txt.file
I am trying to configure code to define a class person and have a function that reads data from txt.file that will be used to create the objects. I am unable to change the arguments in the methods due to restrictions.

The purpose of the Person class is to represent a person who is linked to other people by social connections. Therefore each Person object must be able to store a set of references to other Person objects. (You will need to think about a suitable data type for storing a set of objects, and how you would need to initialize the empty set within the class definition.) In addition to storing the person’s friends, your Person class should also contain instance variables to keep track of the person’s name. If you wish to do so, you can extend this class by adding more methods.

A small snippet of the text file is
enter image description here

```<code>Gill Bates: Jodee Killam, Natacha Osterhoudt, Jom Tones, Verdie Tong, Ossie Digangi

Jom Tones: Marry Blakely, Masako Miguel, Gill Bates

Joey Trammell: Jettie Wilham

Rozanne Schiavo: Mikki Yerby

Deena Massi: Milo Charlebois, Vicenta Seidel

Enrique Litz: Levi Strasser, Alton Justis

Sindy Worley: Vallie Taormina, Alysa Robichaux, Ossie Digangi

Yahaira Briones: Zoila Tarpey, Zandra Marsch

Tanesha Vanwinkle: Nestor Valentine, Shawana Mell, Jamar Mcclane
</code>```

the Gill Bates and Jom Tones are unique objects and I need to ensure that they do not duplicate

the load function does the following:
1. Opens the file which contains the data set given.
2. Creates a new Person object for each record (line) in the file, which contains the name of the person
represented by that record.
3. Where a person’s record indicates that they have friends, you should use the addFriend() method to add
each friend to that Person object.3
4. Finally, return a list of all the Person objects that have been created from the file records (making sure to
have closed the file first).

```<code>class Person:

    def __init__(self, first_name, last_name):
        self.firs_name = first_name
        self.last_name = last_name

    def add_friend(self, friend_person):
        self.friend_person = friend_person
# This method should add a new social connection to be stored in this Person object.
        pass # placeholder only.

    def get_name(self):
        full_name = str(self.first_name)+" "+str(self.last_name)
        return(full_name)
        pass # placeholder only.

    def get_friends(self):
        pass # placeholder only.

def load_people():
    file = open("a2_sample_set.txt", "r")

    pass # placeholder only. To be implemented in task 1.

if __name__ == '__main__':
    pass # placeholder only. You may add your own testing code within this
         # main block to check if the code is working the way you expect.
</code>```

I have tried to start but am having difficulties in going about this.

答:
Here I make the code to get all the names of each person's friends for you, assume that the txt file's format is exactly like that (a new line between each row).

```<code>file = open('names.txt', 'r')
listOfNames = []
for lines in file: // read txt file line by line
    if lines == '\n':
        continue
    else:
        listOfNames.append(lines.split(': '))
listOfFriends = [] // of each person
for eachlist in listOfNames:
    friendsOfEachPerson = eachlist[1].split(', ')
    friendsOfEachPerson[-1] = friendsOfEachPerson[-1].replace('\n', '')
    listOfFriends.append(friendsOfEachPerson)
print(listOfFriends)
</code>```

You can create each Person object base on these names, and add_friend these Person to each other. If you want distinct names, manipulate both listOfNames and listOfFriends to get what you need. Hope it helps.

Body:
I am trying to configure code to define a class person and have a function that reads data from txt.file that will be used to create the objects. I am unable to change the arguments in the methods due to restrictions.

The purpose of the Person class is to represent a person who is linked to other people by social connections. Therefore each Person object must be able to store a set of references to other Person objects. (You will need to think about a suitable data type for storing a set of objects, and how you would need to initialize the empty set within the class definition.) In addition to storing the person’s friends, your Person class should also contain instance variables to keep track of the person’s name. If you wish to do so, you can extend this class by adding more methods.

A small snippet of the text file is
enter image description here

Gill Bates: Jodee Killam, Natacha Osterhoudt, Jom Tones, Verdie Tong, Ossie Digangi

Jom Tones: Marry Blakely, Masako Miguel, Gill Bates

Joey Trammell: Jettie Wilham

Rozanne Schiavo: Mikki Yerby

Deena Massi: Milo Charlebois, Vicenta Seidel

Enrique Litz: Levi Strasser, Alton Justis

Sindy Worley: Vallie Taormina, Alysa Robichaux, Ossie Digangi

Yahaira Briones: Zoila Tarpey, Zandra Marsch

Tanesha Vanwinkle: Nestor Valentine, Shawana Mell, Jamar Mcclane

the Gill Bates and Jom Tones are unique objects and I need to ensure that they do not duplicate

the load function does the following:
1. Opens the file which contains the data set given.
2. Creates a new Person object for each record (line) in the file, which contains the name of the person
represented by that record.
3. Where a person’s record indicates that they have friends, you should use the addFriend() method to add
each friend to that Person object.3
4. Finally, return a list of all the Person objects that have been created from the file records (making sure to
have closed the file first).

class Person:

    def __init__(self, first_name, last_name):
        self.firs_name = first_name
        self.last_name = last_name

    def add_friend(self, friend_person):
        self.friend_person = friend_person
# This method should add a new social connection to be stored in this Person object.
        pass # placeholder only.

    def get_name(self):
        full_name = str(self.first_name)+" "+str(self.last_name)
        return(full_name)
        pass # placeholder only.

    def get_friends(self):
        pass # placeholder only.

def load_people():
    file = open("a2_sample_set.txt", "r")

    pass # placeholder only. To be implemented in task 1.

if __name__ == '__main__':
    pass # placeholder only. You may add your own testing code within this
         # main block to check if the code is working the way you expect.

I have tried to start but am having difficulties in going about this.

Body:
Here I make the code to get all the names of each person's friends for you, assume that the txt file's format is exactly like that (a new line between each row).

file = open('names.txt', 'r')
listOfNames = []
for lines in file: // read txt file line by line
    if lines == '\n':
        continue
    else:
        listOfNames.append(lines.split(': '))
listOfFriends = [] // of each person
for eachlist in listOfNames:
    friendsOfEachPerson = eachlist[1].split(', ')
    friendsOfEachPerson[-1] = friendsOfEachPerson[-1].replace('\n', '')
    listOfFriends.append(friendsOfEachPerson)
print(listOfFriends)

You can create each Person object base on these names, and add_friend these Person to each other. If you want distinct names, manipulate both listOfNames and listOfFriends to get what you need. Hope it helps.


问:
gcc compile, getting "clang: error: unsupported option '-fopenmp'"
I am running the command

```<code>gcc -o openmptest -fopenmp openmptest.c
</code>```

however am getting the error:

```<code>clang: error: unsupported option '-fopenmp'
</code>```

I am running this on Mac Mojave. In addition I am using VS code which is telling me

cannot open source file "omp.h"

for my include statement.

答:
I think it could work if you install libomp by

brew install libomp

and then,

clang -Xpreprocessor -fopenmp -I/usr/local/include -L/usr/local/lib -lomp  openmptest.c -o main

This should work, I was having the same issue and and managed to compile my code by using the above.

Body:
I am running the command

gcc -o openmptest -fopenmp openmptest.c

however am getting the error:

clang: error: unsupported option '-fopenmp'

I am running this on Mac Mojave. In addition I am using VS code which is telling me

cannot open source file "omp.h"

for my include statement.


问:
Unable retrieve records from SQLite database for smart device application
I am using SQLite.NET with C#[ visual studio 2008] and added the 'System.Data.SQLite.dll' as reference. The following code works with Windows Application for fetching data from my database file into a datagrid.

```<code>    private void SetConnection()
    {
        sql_con = new SQLiteConnection("Data Source=emp.db;Version=3;");
    }

    public DataTable LoadData()
    {
        SetConnection();
        sql_con.Open();
        sql_cmd = sql_con.CreateCommand();
        string CommandText = "select * from employeedetails";
        transaction=sql_con.BeginTransaction();
        DA = new SQLiteDataAdapter(CommandText, sql_con);
        DS.Reset();
        DA.Fill(DS);
        DT = DS.Tables[0];
        transaction.Commit();
        sql_con.Close();
        return DT;
    }
</code>```

Invoking in:

```<code>    private void Form1_Load(object sender, EventArgs e)
    {
        EmpTable obj = new EmpTable();
        this.dataGridView1.DataSource = obj.LoadData();
    }
</code>```

The same code not working with C# 'Smart Device Application'. It shows an exception: no such table 'employeedetails'.

For Smart Device Application I have added the System.Data.SQLite.dll from "SQLite/CompactFramework" folder.

Please help me to resolve this.

Thanks in advance.

答:
I'm not sure if this is what is causing your issue, but we've had issues in the past with mobile devices and sqlite where this solved it:

For an application using the compact framework, you need to include SQLite.Interop.xxx.DLL in your project (in addition to the regular System.Data.SQLite.DLL). Make sure you set the file to copy if newer or copy always.

Keep in mind that you can't set a reference to the interop dll. You must include it in the project by adding it as a file.

For more information check this website under the Distributing The SQLite Engine and ADO.NET Assembly section.

Body:
I am using SQLite.NET with C#[ visual studio 2008] and added the 'System.Data.SQLite.dll' as reference. The following code works with Windows Application for fetching data from my database file into a datagrid.

    private void SetConnection()
    {
        sql_con = new SQLiteConnection("Data Source=emp.db;Version=3;");
    }

    public DataTable LoadData()
    {
        SetConnection();
        sql_con.Open();
        sql_cmd = sql_con.CreateCommand();
        string CommandText = "select * from employeedetails";
        transaction=sql_con.BeginTransaction();
        DA = new SQLiteDataAdapter(CommandText, sql_con);
        DS.Reset();
        DA.Fill(DS);
        DT = DS.Tables[0];
        transaction.Commit();
        sql_con.Close();
        return DT;
    }

Invoking in:

    private void Form1_Load(object sender, EventArgs e)
    {
        EmpTable obj = new EmpTable();
        this.dataGridView1.DataSource = obj.LoadData();
    }

The same code not working with C# 'Smart Device Application'. It shows an exception: no such table 'employeedetails'.

For Smart Device Application I have added the System.Data.SQLite.dll from "SQLite/CompactFramework" folder.

Please help me to resolve this.

Thanks in advance.


问:
How to use Gauss-Lobatto and Gauss-Laguerre quadrature code
I'm using MATLAB and this function:

```<code>function [x,w,P]=lglnodes(N)

% Truncation + 1
N1=N+1;

x=cos(pi*(0:N)/N)';

% Legendre Vandermonde Matrix
P=zeros(N1,N1);

xold=2;

while max(abs(x-xold))>eps

    xold=x;

    P(:,1)=1;    P(:,2)=x;

    for k=2:N
        P(:,k+1)=( (2*k-1)*x.*P(:,k)-(k-1)*P(:,k-1) )/k;
    end

    x=xold-( x.*P(:,N1)-P(:,N) )./( N1*P(:,N1) );

end

w=2./(N*N1*P(:,N1).^2);
</code>```

With this function, I get weights and nodes over which I can integrate my favorite functions. For example, I can call lglnodes(400 - 1) and get nodes and weights for N = 400. Then I have this code:

Gauss-Laguerre

I would like to do the same with Gauss-Laguerre (GaussLaguerre(n, alpha) function), but I cannot understand how to use the inputs to get about the same weights as above. Can you explain to me how to tune the inputs? How to set n and alpha?

答:
Gauss-Lobatto and Gauss-Laguerre are integral points/weights for two different integrals:

Lobatto is for unweighted integrals in (-1, 1),

Laguerre is for (0, infty) with weight x^alpha exp(-x).

You cannot expect to ever get the same weights.

Body:
I'm using MATLAB and this function:

function [x,w,P]=lglnodes(N)

% Truncation + 1
N1=N+1;

x=cos(pi*(0:N)/N)';

% Legendre Vandermonde Matrix
P=zeros(N1,N1);

xold=2;

while max(abs(x-xold))>eps

    xold=x;

    P(:,1)=1;    P(:,2)=x;

    for k=2:N
        P(:,k+1)=( (2*k-1)*x.*P(:,k)-(k-1)*P(:,k-1) )/k;
    end

    x=xold-( x.*P(:,N1)-P(:,N) )./( N1*P(:,N1) );

end

w=2./(N*N1*P(:,N1).^2);

With this function, I get weights and nodes over which I can integrate my favorite functions. For example, I can call lglnodes(400 - 1) and get nodes and weights for N = 400. Then I have this code:

Gauss-Laguerre

I would like to do the same with Gauss-Laguerre (GaussLaguerre(n, alpha) function), but I cannot understand how to use the inputs to get about the same weights as above. Can you explain to me how to tune the inputs? How to set n and alpha?


问:
Creating a df starting from a timestamp value and ending on a timestamp value increment by one hour in Spark scala
Can anyone help me to create a data frame were starting and ending timestamp value is given and we need to print all the hour increment by one?

Example
Starting timestamp: 02-05-2020 01:00

Ending timestamp: 03-05-2020 02:00

So the df should have all those values:

```<code>
02-05-2020 01:00
02-05-2020 02:00
02-05-2020 03:00
02-05-2020 04:00
02-05-2020 05:00
.
.
.
03-05-2020 02:00

</code>```

Body:
Can anyone help me to create a data frame were starting and ending timestamp value is given and we need to print all the hour increment by one?

Example
Starting timestamp: 02-05-2020 01:00

Ending timestamp: 03-05-2020 02:00

So the df should have all those values:

02-05-2020 01:00
02-05-2020 02:00
02-05-2020 03:00
02-05-2020 04:00
02-05-2020 05:00
.
.
.
03-05-2020 02:00

Body:
Try this

df = (spark
      .sql(f"SELECT explode(sequence(1588381200, 1588471200, 3600)) as date_col")
      .withColumn('date_col', F.from_unixtime('date_col'))
     )

# Result dataframe
+-------------------+
|           date_col|
+-------------------+
|2020-05-02 01:00:00|
|2020-05-02 02:00:00|
|2020-05-02 03:00:00|
|2020-05-02 04:00:00|
|2020-05-02 05:00:00|
...
|2020-05-03 01:00:00|
|2020-05-03 02:00:00|
+-------------------+

sequence(start, stop, step) takes three parameters.

Find out the unix time for your start and end date (unix time are counted in seconds),
I used https://www.unixtimestamp.com/index.php

1588381200 is unix time for 02-05-2020 01:00

1588471200 is unit time for 03-05-2020 02:00

3600 is the interval i.e. 1 hour = 3600 seconds