问:
Numpy: division by zero error, but mathematically function is apparently defined
I'm testing out some functions to fit with data, and one of them (in 2-D) is

```<code>f(x) = (1/(1-x)) / (1 + 1/(1-x))
</code>```

Which, according to Wolfram and the Google plotters, gives you the result

```<code>f(1) = 1
</code>```

I've tried to get this to work without hard coding the case

```<code>if x == 1:
    return 1
</code>```

but I end up with a nan and a RunTimeWarning informing me that I have indeed divided by zero.

import numpy as np

def f(x):
    return 1/(1-x) / (1 + 1/(1-x))

x_range = np.linspace(0, 1, 50)
y = f(x_range)
print(y)
```

Is there a more elegant solution than to simply introduce a hard-coded if?

Body:
I'm testing out some functions to fit with data, and one of them (in 2-D) is

f(x) = (1/(1-x)) / (1 + 1/(1-x))

Which, according to Wolfram and the Google plotters, gives you the result

f(1) = 1

I've tried to get this to work without hard coding the case

if x == 1:
    return 1

but I end up with a nan and a RunTimeWarning informing me that I have indeed divided by zero.

import numpy as np

def f(x):
    return 1/(1-x) / (1 + 1/(1-x))

x_range = np.linspace(0, 1, 50)
y = f(x_range)
print(y)

Is there a more elegant solution than to simply introduce a hard-coded if?

Body:
Just simplify the equation for f(x) = (1/(1-x)) / (1 + 1/(1-x)). The simplified equation will be (1/(2-x)). Now update the program as:

import numpy as np
def f(x):
    return 1/(2-x)
x_range = np.linspace(0, 1, 50)
y = f(x_range)
print(y)

output:

[0.5        0.50515464 0.51041667 0.51578947 0.5212766  0.52688172
 0.5326087  0.53846154 0.54444444 0.5505618  0.55681818 0.56321839
 0.56976744 0.57647059 0.58333333 0.59036145 0.59756098 0.60493827
 0.6125     0.62025316 0.62820513 0.63636364 0.64473684 0.65333333
 0.66216216 0.67123288 0.68055556 0.69014085 0.7        0.71014493
 0.72058824 0.73134328 0.74242424 0.75384615 0.765625   0.77777778
 0.79032258 0.80327869 0.81666667 0.83050847 0.84482759 0.85964912
 0.875      0.89090909 0.90740741 0.9245283  0.94230769 0.96078431
 0.98       1.        ]


来源: stackexchange

问:
Mark range based on identifier
This is based on a question I asked yesterday. It got very muddled, so I am trying again with a clearer question.

I have a large data set.

```<code>>head(raw)

 ps cond pass sample stim gsr
1  1    2    0      0    0 100
2  1    2    0      1    0 100
3  1    2    0      2    0 100
4  1    2    0      3    0 100
5  1    2    0      4    0 100
6  1    2    0      5    0 100
</code>```

The $stim column is composed of various periods numbered 1-11 (each period lasts 20 $sample), with longer blocks of 0 (lasting for 140 $sample).

For every $stim==10, I need to mark a subsequent range:

e.g.,

count(raw$sample[raw$ps==1 & raw$stim==10]) #this counts for 1 subject as an example

```<code>      x freq
1  1100    1
2  1101    1
3  1102    1
4  1103    1
5  1104    1
6  1105    1
7  1106    1
8  1107    1
9  1108    1
10 1109    1
11 1110    1
12 1111    1
13 1112    1
14 1113    1
15 1114    1
16 1115    1
17 1116    1
18 1117    1
19 1118    1
20 1119    1
</code>```

So I want the beginning of the range to start 10 cells after the last cell with $stim==10 (in this example it is 1119, so we start from 1120. We need to count 10 from this place: 1130. The end of the range is 50 $sample from 1130 = 1180.

So. What I think I need is for a new column in my raw file, which marks TRUE those cells to be used in the analysis. In the above example, they would be the range between $sample == 1130 and 1180.

I don't want to go through by hand. I am looking for a more automated way of ticking off the ranges.

I hope it is now clearer what I am aiming for?

Further information:

```<code>> sort(unique(rle(raw$n.filter)$length))
 40   50  590 1080 1130 1240 1400 1560 1720 1880 2030 2040 2200 2360

> summary(raw$stim)
     0      1      2      3      4      5      6      7      8      9     10     11
286440   3720   3720   3720   3720   3720   3720   3720   3720   3720   3720   3720

> summary(raw$stim[raw$ps==1])
   0    1    2    3    4    5    6    7    8    9   10   11
1540   20   20   20   20   20   20   20   20   20   20   20

> summary(raw$stim[raw$ps==186])
   0    1    2    3    4    5    6    7    8    9   10   11
1540   20   20   20   20   20   20   20   20   20   20   20
</code>```

答:
Edited answer due to an error in previous one:

There are two ways of getting what you want. One way is vectorized (and fast), the other one is with a loop and slow.

1.Vectorized:

```<code>tmp <- which(raw$stim == 10)
ltmp <- 1:length(tmp)

raw$n.filter <- FALSE
raw[tmp + 30,"n.filter"] <- TRUE
raw[tmp + 50,"n.filter"] <- TRUE
raw[tmp[ltmp[(ltmp%%20) > 0 & (ltmp%%20) < 11]]+70,"n.filter"] <- TRUE
rle(raw$n.filter)
</code>```

2.With Loop:

```<code>raw$n.filter <- FALSE

for (counter in 2:(nrow(raw))) {
    if ( (raw[counter-1, "stim"] == 10) & raw[counter, "stim"] != 10) raw[(counter+10):(counter+59),"n.filter"] <- TRUE
}

rle(raw$n.filter)
</code>```

I was too lazy to wait for the loop version to finish. it is the best you copy the result of one version, then run the other and see whether they are all.equal().

ContentLicense: CC BY-SA 3.0

Title: Mark range based on identifier

Body:
This is based on a question I asked yesterday. It got very muddled, so I am trying again with a clearer question.

I have a large data set.

>head(raw)

 ps cond pass sample stim gsr
1  1    2    0      0    0 100
2  1    2    0      1    0 100
3  1    2    0      2    0 100
4  1    2    0      3    0 100
5  1    2    0      4    0 100
6  1    2    0      5    0 100

The $stim column is composed of various periods numbered 1-11 (each period lasts 20 $sample), with longer blocks of 0 (lasting for 140 $sample).

For every $stim==10, I need to mark a subsequent range:

e.g.,

count(raw$sample[raw$ps==1 & raw$stim==10]) #this counts for 1 subject as an example

      x freq
1  1100    1
2  1101    1
3  1102    1
4  1103    1
5  1104    1
6  1105    1
7  1106    1
8  1107    1
9  1108    1
10 1109    1
11 1110    1
12 1111    1
13 1112    1
14 1113    1
15 1114    1
16 1115    1
17 1116    1
18 1117    1
19 1118    1
20 1119    1

So I want the beginning of the range to start 10 cells after the last cell with $stim==10 (in this example it is 1119, so we start from 1120. We need to count 10 from this place: 1130. The end of the range is 50 $sample from 1130 = 1180.

So. What I think I need is for a new column in my raw file, which marks TRUE those cells to be used in the analysis. In the above example, they would be the range between $sample == 1130 and 1180.

I don't want to go through by hand. I am looking for a more automated way of ticking off the ranges.

I hope it is now clearer what I am aiming for?

Further information:

> sort(unique(rle(raw$n.filter)$length))
 40   50  590 1080 1130 1240 1400 1560 1720 1880 2030 2040 2200 2360

> summary(raw$stim)
     0      1      2      3      4      5      6      7      8      9     10     11
286440   3720   3720   3720   3720   3720   3720   3720   3720   3720   3720   3720

> summary(raw$stim[raw$ps==1])
   0    1    2    3    4    5    6    7    8    9   10   11
1540   20   20   20   20   20   20   20   20   20   20   20

> summary(raw$stim[raw$ps==186])
   0    1    2    3    4    5    6    7    8    9   10   11
1540   20   20   20   20   20   20   20   20   20   20   20

Body:
Edited answer due to an error in previous one:

There are two ways of getting what you want. One way is vectorized (and fast), the other one is with a loop and slow.

1.Vectorized:

tmp <- which(raw$stim == 10)
ltmp <- 1:length(tmp)

raw$n.filter <- FALSE
raw[tmp + 30,"n.filter"] <- TRUE
raw[tmp + 50,"n.filter"] <- TRUE
raw[tmp[ltmp[(ltmp%%20) > 0 & (ltmp%%20) < 11]]+70,"n.filter"] <- TRUE
rle(raw$n.filter)

2.With Loop:

raw$n.filter <- FALSE

for (counter in 2:(nrow(raw))) {
    if ( (raw[counter-1, "stim"] == 10) & raw[counter, "stim"] != 10) raw[(counter+10):(counter+59),"n.filter"] <- TRUE
}

rle(raw$n.filter)

I was too lazy to wait for the loop version to finish. it is the best you copy the result of one version, then run the other and see whether they are all.equal().


问:
How can I map an Array or an Object to an Object?
Normally a map method returns an Array but I have had a lot use-cases where I would like to create an Object instead. Is there a JavaScript native way of doing this or a very simple and performant implementation? jQuery solutions are also welcome.

This is how the native Array map method works (EcmaScript 6):

```<code>console.log( [ 1, 2, 4 ].map( item => item * 10) );
// => [ 10, 20, 40 ]
</code>```

This is how I imagine the method to work (in case it is JavaScript native):

```<code>console.log( [ 1, 2, 4 ].mapToObject(resolveMultiplyWithTen) );
// => { 1: 10, 2: 20, 4: 40 }
</code>```

In this example, the method mapToObject should behave the same like map but return an Object instead of an Array. Callback resolveMultiplyWithTen would multiply the item with ten.

A problem which I see here is that the callback function can not return two values (key and value, e.g.: 1 and 20), which would be needed to efficiently create an Object.

This is how I always used to create an Object from Array:

```<code>/* Loop over Array and create key:value pairs in Object */
const arr = [ 1, 2, 4];
const obj = {};
for ( let i = 0; i < arr.length; i++ )
  obj[arr[i]] = arr[i] * 10;
console.log(obj);
// => { 1: 10, 2: 20, 4: 40 }
</code>```

This is the most suiting solution I could think of:

```<code>/* Create an Object of each item via map and apply to Object.assign */
console.log(Object.assign.apply(null, [ 1, 2, 4 ].map( item => {
  const out = {};
  out[item] = item * 10;
  return out;
})));
// => { 1: 10, 2: 20, 4: 40 }
</code>```

Is there any native JavaScript solution to this problem or a more performant way?

答:
You can just use reduce

let obj = [ 1, 2, 4 ].reduce( (op,inp) => (op[inp] = inp * 10, op),{} )

console.log(obj)```
</div>
</div>
</p>

<p>or you can use <code>forEach</code></p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>let data = [ 1, 2, 4 ]
let obj = {}

data.forEach(val=>{
  obj[val] = val * 10
})

console.log(obj)</code>```

ContentLicense: CC BY-SA 4.0

Body:
Normally a map method returns an Array but I have had a lot use-cases where I would like to create an Object instead. Is there a JavaScript native way of doing this or a very simple and performant implementation? jQuery solutions are also welcome.

This is how the native Array map method works (EcmaScript 6):

console.log( [ 1, 2, 4 ].map( item => item * 10) );
// => [ 10, 20, 40 ]

This is how I imagine the method to work (in case it is JavaScript native):

console.log( [ 1, 2, 4 ].mapToObject(resolveMultiplyWithTen) );
// => { 1: 10, 2: 20, 4: 40 }

In this example, the method mapToObject should behave the same like map but return an Object instead of an Array. Callback resolveMultiplyWithTen would multiply the item with ten.

A problem which I see here is that the callback function can not return two values (key and value, e.g.: 1 and 20), which would be needed to efficiently create an Object.

This is how I always used to create an Object from Array:

/* Loop over Array and create key:value pairs in Object */
const arr = [ 1, 2, 4];
const obj = {};
for ( let i = 0; i < arr.length; i++ )
  obj[arr[i]] = arr[i] * 10;
console.log(obj);
// => { 1: 10, 2: 20, 4: 40 }

This is the most suiting solution I could think of:

/* Create an Object of each item via map and apply to Object.assign */
console.log(Object.assign.apply(null, [ 1, 2, 4 ].map( item => {
  const out = {};
  out[item] = item * 10;
  return out;
})));
// => { 1: 10, 2: 20, 4: 40 }

Is there any native JavaScript solution to this problem or a more performant way?

Body:
You can just use reduce

let obj = [ 1, 2, 4 ].reduce( (op,inp) => (op[inp] = inp * 10, op),{} )

console.log(obj)

or you can use forEach

let data = [ 1, 2, 4 ]
let obj = {}

data.forEach(val=>{
  obj[val] = val * 10
})

console.log(obj)


问:
How to load assets from .pug template with html-webpack-plugin
I am going through a CSS course and have got tired of typing out similar HTML structures by hand, so I hooked up pug templates and webpack-dev-server to serve it.

NOTE: my entry point is .scss file not .js

I have two issues:

a) only assets from css get loaded to build folder, but images and svg's that are referenced in src attributes in .pug template are left out.

b) browser reloading doesn't work in webpack-dev-server

My webpack.config.js:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: './sass/main.scss',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'css/style.css',
  },
  mode: 'development',
  module: {
    rules: [
      {
        test: /\.pug$/,
        use: ['pug-loader'],
      },
      {
        test: /\.(css|sass|scss)$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
            },
            {
              loader: 'postcss-loader',
            },
            {
              loader: 'sass-loader',
            },
          ]
        })
      },
      {
        test: /\.(gif|png|jpe?g|svg)$/i,
        use: [
          // {
          //   loader: 'file-loader',
          //   options: {
          //     name: 'img/[name].[ext]',
          //   }
          // },
          {
            loader: 'url-loader',
            options: {
              limit: 10000,
              publicPath: "../",
              name: 'img/[name].[ext]',
            }
          }
        ],
      }
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './test.pug',
      filename: 'index.html',
      favicon: 'img/favicon.png',
      cache: false,
    }),
    new ExtractTextPlugin('css/style.css'),
  ],
  devServer: {
    contentBase: path.join(__dirname, 'build'),
    compress: true,
    open: 'firefox',
    port: 8080,
    hot: true,
  },
};
```

Please advise.

答:
It seems like this topic is of no interest to anybody, but I've found the solution and here it is:

a) in order to trigger webpack to include your assets from .pug template into dependency graph require them:

```<code>img(
    alt="Your image"
    src=require("./img/image.jpg")
)
</code>```

However you can not do the same with SVG's if you access all of them from a single sprite. Because:

```<code>svg
   use(xlink:href=require("./img/sprite.svg#icon"))
</code>```

will throw and exception saying that it couldn't find a file by that name.

So what I did was requiring the whole sprite in the beginning of the template require("./img/sprite.svg")
and then just specified icons in usual way:

```<code>svg
   use(xlink:href="./img/sprite.svg#icon")
</code>```

b) As for the reloading issue, it wasn't that easy. I played around with MiniCssExtractPlugin options but still couldn't get it to reload. So I ended up taking ideas from this gist and this repo. Had to change the structure of my project little and create index.js as an entry point though.

Body:
I am going through a CSS course and have got tired of typing out similar HTML structures by hand, so I hooked up pug templates and webpack-dev-server to serve it.

NOTE: my entry point is .scss file not .js

I have two issues:

a) only assets from css get loaded to build folder, but images and svg's that are referenced in src attributes in .pug template are left out.

b) browser reloading doesn't work in webpack-dev-server

My webpack.config.js:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: './sass/main.scss',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'css/style.css',
  },
  mode: 'development',
  module: {
    rules: [
      {
        test: /\.pug$/,
        use: ['pug-loader'],
      },
      {
        test: /\.(css|sass|scss)$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
            },
            {
              loader: 'postcss-loader',
            },
            {
              loader: 'sass-loader',
            },
          ]
        })
      },
      {
        test: /\.(gif|png|jpe?g|svg)$/i,
        use: [
          // {
          //   loader: 'file-loader',
          //   options: {
          //     name: 'img/[name].[ext]',
          //   }
          // },
          {
            loader: 'url-loader',
            options: {
              limit: 10000,
              publicPath: "../",
              name: 'img/[name].[ext]',
            }
          }
        ],
      }
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './test.pug',
      filename: 'index.html',
      favicon: 'img/favicon.png',
      cache: false,
    }),
    new ExtractTextPlugin('css/style.css'),
  ],
  devServer: {
    contentBase: path.join(__dirname, 'build'),
    compress: true,
    open: 'firefox',
    port: 8080,
    hot: true,
  },
};

Please advise.

Body:
It seems like this topic is of no interest to anybody, but I've found the solution and here it is:

a) in order to trigger webpack to include your assets from .pug template into dependency graph require them:

img(
    alt="Your image"
    src=require("./img/image.jpg")
)

However you can not do the same with SVG's if you access all of them from a single sprite. Because:

svg
   use(xlink:href=require("./img/sprite.svg#icon"))

will throw and exception saying that it couldn't find a file by that name.

So what I did was requiring the whole sprite in the beginning of the template require("./img/sprite.svg")
and then just specified icons in usual way:

svg
   use(xlink:href="./img/sprite.svg#icon")

b) As for the reloading issue, it wasn't that easy. I played around with MiniCssExtractPlugin options but still couldn't get it to reload. So I ended up taking ideas from this gist and this repo. Had to change the structure of my project little and create index.js as an entry point though.


问:
No able to convert Spark dataset to Iterator using build in toLocalIterator()
I am trying to convert a spark dataset to iterator in order to write the dataset to influxdb. After I constructed the dataset I want, I need to convert the dataset to an iterator to pass to the influxdb writer.

However, the problem happens when using toLocalIterator() build in function for dataset class.

I got the following exception:

```<code>override def gatherTimeMetrics(df: DataFrame)
    (implicit params: ConversionParams, config: Config): Dataset[TimeMetric] = {
        df
            .select($ "download_date", $ "unixtime".cast("long") as "unixtime")
            .groupBy("download_date", "unixtime")
            .agg(count("*") as "rows")
            .repartition(1)
            .as[(String, Long, Long)]
            .map {
                case (downloadDate, unixtime, rows) =>
                TimeMetric(
                    unixtime,
                    Map(
                        "rows" - > rows
                    ),
                    Map(
                        "download_date" - > downloadDate
                    )
                )
            }
    }
</code>```

The returned dataset is used here :

```<code>def run(df: DataFrame) (implicit params: T, config: Config): Unit =
    metricsService.write(Metrics(getMeasurementName, gatherTimeMetrics(df).toLocalIterator(), getCommonTags))
</code>```

I expect the build in toLocalIterator() will convert to Iterator but,

I got this exception :

```<code>Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.apache.spark.sql.execution.datasources.parquet.ParquetSchemaConverter$
    at org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat.buildReaderWithPartitionValues(ParquetFileFormat.scala:301)
    at org.apache.spark.sql.execution.FileSourceScanExec.inputRDD$lzycompute(DataSourceScanExec.scala:285)
    at org.apache.spark.sql.execution.FileSourceScanExec.inputRDD(DataSourceScanExec.scala:283)
    at org.apache.spark.sql.execution.FileSourceScanExec.inputRDDs(DataSourceScanExec.scala:303)
    at org.apache.spark.sql.execution.ProjectExec.inputRDDs(basicPhysicalOperators.scala:42)
    at org.apache.spark.sql.execution.aggregate.HashAggregateExec.inputRDDs(HashAggregateExec.scala:141)
    at org.apache.spark.sql.execution.WholeStageCodegenExec.doExecute(WholeStageCodegenExec.scala:386)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:138)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:135)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:116)
    at org.apache.spark.sql.execution.exchange.ShuffleExchange.prepareShuffleDependency(ShuffleExchange.scala:88)
    at org.apache.spark.sql.execution.exchange.ShuffleExchange$$anonfun$doExecute$1.apply(ShuffleExchange.scala:124)
    at org.apache.spark.sql.execution.exchange.ShuffleExchange$$anonfun$doExecute$1.apply(ShuffleExchange.scala:115)
    at org.apache.spark.sql.catalyst.errors.package$.attachTree(package.scala:52)
    at org.apache.spark.sql.execution.exchange.ShuffleExchange.doExecute(ShuffleExchange.scala:115)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:138)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:135)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:116)
    at org.apache.spark.sql.execution.InputAdapter.inputRDDs(WholeStageCodegenExec.scala:252)
    at org.apache.spark.sql.execution.aggregate.HashAggregateExec.inputRDDs(HashAggregateExec.scala:141)
    at org.apache.spark.sql.execution.DeserializeToObjectExec.inputRDDs(objects.scala:79)
    at org.apache.spark.sql.execution.MapElementsExec.inputRDDs(objects.scala:215)
    at org.apache.spark.sql.execution.SerializeFromObjectExec.inputRDDs(objects.scala:116)
    at org.apache.spark.sql.execution.WholeStageCodegenExec.doExecute(WholeStageCodegenExec.scala:386)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:138)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:135)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:116)
    at org.apache.spark.sql.execution.SparkPlan.getByteArrayRdd(SparkPlan.scala:228)
    at org.apache.spark.sql.execution.SparkPlan.executeToIterator(SparkPlan.scala:290)
    at org.apache.spark.sql.Dataset$$anonfun$toLocalIterator$1.apply(Dataset.scala:2421)
    at org.apache.spark.sql.Dataset$$anonfun$toLocalIterator$1.apply(Dataset.scala:2416)
    at org.apache.spark.sql.Dataset$$anonfun$55.apply(Dataset.scala:2842)
    at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:65)
    at org.apache.spark.sql.Dataset.withAction(Dataset.scala:2841)
    at org.apache.spark.sql.Dataset.toLocalIterator(Dataset.scala:2416)
</code>```

答:
This is the dependency which has the class org.apache.spark.sql.execution.datasources.parquet.ParquetSchemaConverter

```<code><!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-sql_2.10</artifactId>
    <version>1.6.1</version>
</dependency>
</code>```

to debug/print all the jars in classpath below will be helpful to understand which jars in your classpath.

```<code>def urlsinclasspath(cl: ClassLoader): Array[java.net.URL] = cl match {
    case null => Array()
    case u: java.net.URLClassLoader => u.getURLs() ++ urlsinclasspath(cl.getParent)
    case _ => urlsinclasspath(cl.getParent)
  }
</code>```

Caller would be...

```<code>val  urls = urlsinclasspath(getClass.getClassLoader).foreach(println)
</code>```

Body:
I am trying to convert a spark dataset to iterator in order to write the dataset to influxdb. After I constructed the dataset I want, I need to convert the dataset to an iterator to pass to the influxdb writer.

However, the problem happens when using toLocalIterator() build in function for dataset class.

I got the following exception:

override def gatherTimeMetrics(df: DataFrame)
    (implicit params: ConversionParams, config: Config): Dataset[TimeMetric] = {
        df
            .select($ "download_date", $ "unixtime".cast("long") as "unixtime")
            .groupBy("download_date", "unixtime")
            .agg(count("*") as "rows")
            .repartition(1)
            .as[(String, Long, Long)]
            .map {
                case (downloadDate, unixtime, rows) =>
                TimeMetric(
                    unixtime,
                    Map(
                        "rows" - > rows
                    ),
                    Map(
                        "download_date" - > downloadDate
                    )
                )
            }
    }

The returned dataset is used here :

def run(df: DataFrame) (implicit params: T, config: Config): Unit =
    metricsService.write(Metrics(getMeasurementName, gatherTimeMetrics(df).toLocalIterator(), getCommonTags))

I expect the build in toLocalIterator() will convert to Iterator but,

I got this exception :

Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.apache.spark.sql.execution.datasources.parquet.ParquetSchemaConverter$
    at org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat.buildReaderWithPartitionValues(ParquetFileFormat.scala:301)
    at org.apache.spark.sql.execution.FileSourceScanExec.inputRDD$lzycompute(DataSourceScanExec.scala:285)
    at org.apache.spark.sql.execution.FileSourceScanExec.inputRDD(DataSourceScanExec.scala:283)
    at org.apache.spark.sql.execution.FileSourceScanExec.inputRDDs(DataSourceScanExec.scala:303)
    at org.apache.spark.sql.execution.ProjectExec.inputRDDs(basicPhysicalOperators.scala:42)
    at org.apache.spark.sql.execution.aggregate.HashAggregateExec.inputRDDs(HashAggregateExec.scala:141)
    at org.apache.spark.sql.execution.WholeStageCodegenExec.doExecute(WholeStageCodegenExec.scala:386)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:138)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:135)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:116)
    at org.apache.spark.sql.execution.exchange.ShuffleExchange.prepareShuffleDependency(ShuffleExchange.scala:88)
    at org.apache.spark.sql.execution.exchange.ShuffleExchange$$anonfun$doExecute$1.apply(ShuffleExchange.scala:124)
    at org.apache.spark.sql.execution.exchange.ShuffleExchange$$anonfun$doExecute$1.apply(ShuffleExchange.scala:115)
    at org.apache.spark.sql.catalyst.errors.package$.attachTree(package.scala:52)
    at org.apache.spark.sql.execution.exchange.ShuffleExchange.doExecute(ShuffleExchange.scala:115)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:138)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:135)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:116)
    at org.apache.spark.sql.execution.InputAdapter.inputRDDs(WholeStageCodegenExec.scala:252)
    at org.apache.spark.sql.execution.aggregate.HashAggregateExec.inputRDDs(HashAggregateExec.scala:141)
    at org.apache.spark.sql.execution.DeserializeToObjectExec.inputRDDs(objects.scala:79)
    at org.apache.spark.sql.execution.MapElementsExec.inputRDDs(objects.scala:215)
    at org.apache.spark.sql.execution.SerializeFromObjectExec.inputRDDs(objects.scala:116)
    at org.apache.spark.sql.execution.WholeStageCodegenExec.doExecute(WholeStageCodegenExec.scala:386)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$1.apply(SparkPlan.scala:117)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery$1.apply(SparkPlan.scala:138)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:135)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:116)
    at org.apache.spark.sql.execution.SparkPlan.getByteArrayRdd(SparkPlan.scala:228)
    at org.apache.spark.sql.execution.SparkPlan.executeToIterator(SparkPlan.scala:290)
    at org.apache.spark.sql.Dataset$$anonfun$toLocalIterator$1.apply(Dataset.scala:2421)
    at org.apache.spark.sql.Dataset$$anonfun$toLocalIterator$1.apply(Dataset.scala:2416)
    at org.apache.spark.sql.Dataset$$anonfun$55.apply(Dataset.scala:2842)
    at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:65)
    at org.apache.spark.sql.Dataset.withAction(Dataset.scala:2841)
    at org.apache.spark.sql.Dataset.toLocalIterator(Dataset.scala:2416)

Body:
This is the dependency which has the class org.apache.spark.sql.execution.datasources.parquet.ParquetSchemaConverter

<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-sql_2.10</artifactId>
    <version>1.6.1</version>
</dependency>

to debug/print all the jars in classpath below will be helpful to understand which jars in your classpath.

def urlsinclasspath(cl: ClassLoader): Array[java.net.URL] = cl match {
    case null => Array()
    case u: java.net.URLClassLoader => u.getURLs() ++ urlsinclasspath(cl.getParent)
    case _ => urlsinclasspath(cl.getParent)
  }

Caller would be...

val  urls = urlsinclasspath(getClass.getClassLoader).foreach(println)


问:
C# - Converting a RDF file to specific XML file
We have a requirement in which we need to convert a given rdf file into a specific xml file. So we have three inputs RDFS file, RDF file and XSD file. With these we need to create XML file (which should conform to the XSD) having the data of the RDF file. The RDF schema contains simple, complex, relation entities. The relations defined in the RDF are different from the relations defined in the XSD.

We see couple of ways of implementing the same:

Create an XSLT defining the logic for converting the RDF file to XML file.

Build a custom C# app. for doing the same (using RDF libraries like LinqToRDF etc.)

Please guide regarding which option would be better and any pointers for the same.

Regards,

答:
XSLT is generally a bad idea for converting RDF/XML into other formats because the same RDF data can be encoded in RDF/XML in different ways.

So unless your RDF/XML input is tightly controlled a pure XSLT transformation is usually quite difficult and hard to maintain.

For C# manipulation of RDF you might want to try dotNetRDF (disclaimer - I develop the library) which gives you an API for working with RDF at the Triple level.  If your RDF has regular repeating structures in it then the easiest way to extract that data will be to use SPARQL queries to get the data of relevance to you out of the RDF.  Once you have the results (which are similar to a DataTable in structure and can be cast to a DataTable if that is easier for you to work with) you can generate your XML file as desired.

If this looks like it might be a viable option for you then feel free to drop me an email on the project mailing lists if you want any help/advice.


问:
passing image from collectionViewCell which is located tableviewCell
I ask for help with determining the right approach

when I click on the picture I need to pass text and her to new VC, but the problem is that the project has a great depth struct

the cell lies in the collectionView, which is in the tableViewCell

How do I properly implement the transfer?
(the number of cells is generated automatically)

Another complication is that the code of the cell with which I work lies in another cell.

those. the transfer of information must also be written in it.

```<code>import UIKit
import moa

class RealPhotoInfoTableViewCell: UITableViewCell {

    @IBOutlet private weak var MyCollection: UICollectionView!

    var model: RealPhoto!

    func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate, forRow row: Int) {
        MyCollection.delegate = self
        MyCollection.dataSource = self
        MyCollection.reloadData()
    }
}

extension RealPhotoInfoTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print(model?.data.count as Any)
        return model?.data.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RPhotoCell", for: indexPath) as! RealPhotoCollectionViewCell

        cell.realPhotoImage.moa.url = model?.data[indexPath.row].photo

        return cell
    }
}
</code>```

答:
Add a var

```<code>class RealPhotoInfoTableViewCell: UITableViewCell {
   weak var delegate:VCName?
</code>```

Inside cellForRowAt of the table do

```<code>cell.delegate = self
</code>```

Now you can do this inside didSelectItemAt of the collection

```<code>delegate?.sendData(image)
</code>```

Body:
I ask for help with determining the right approach

when I click on the picture I need to pass text and her to new VC, but the problem is that the project has a great depth struct

the cell lies in the collectionView, which is in the tableViewCell

How do I properly implement the transfer?
(the number of cells is generated automatically)

Another complication is that the code of the cell with which I work lies in another cell.

those. the transfer of information must also be written in it.

import UIKit
import moa

class RealPhotoInfoTableViewCell: UITableViewCell {

    @IBOutlet private weak var MyCollection: UICollectionView!

    var model: RealPhoto!

    func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate, forRow row: Int) {
        MyCollection.delegate = self
        MyCollection.dataSource = self
        MyCollection.reloadData()
    }
}

extension RealPhotoInfoTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print(model?.data.count as Any)
        return model?.data.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RPhotoCell", for: indexPath) as! RealPhotoCollectionViewCell

        cell.realPhotoImage.moa.url = model?.data[indexPath.row].photo

        return cell
    }
}

Body:
Add a var

class RealPhotoInfoTableViewCell: UITableViewCell {
   weak var delegate:VCName?

Inside cellForRowAt of the table do

cell.delegate = self

Now you can do this inside didSelectItemAt of the collection

delegate?.sendData(image)


问:
World Cup Cricket Live Match Streaming API
If I want to develop an android application for live streaming cricket match in cricket world cup, how can I get the API for live streaming match?</p>

答:
Cricinfo provides api to get the information about various matches and current ongoing matches. You can evaluate the api. I provide below the link.

https://www.cricapi.com/


问:
MongoDB HashTable Averages
I am using c#,along with MongoDB.
I have a class that can be resembled by this.
Its a sample, that represents something, please dont comment on the class design

```<code>[CollectionName("Venues")]
public class Venue
{
   public string Name { get; set; }
   public dictionary<string,object> Properties { get; set; }
}

var venue = new Venue
{
  Name = "Venue 1",
  Properties = new Dictionary<string,object>
  {
    { "Chairs", "18" },
    { "Tables", "4" },
    { "HasWaterfall", true }
  }
}
</code>```

Assuming I have an object in a collection, that looks like that.
I would like to find out of it is possible to do two things.

1: Load from the database, only a single item from the dictionary,
   currently I can only see how this can be done, by loading the entire
   record from the database and then manually getting the value by key.

2: Determine the average of a single item within the database.
   For example, across all records I would like to work out the average
   chairs, again without loading all records and then doing it in memory with
   linq etc....

答:
Basically your sample document gets stored as a below JSON:

```<code>{
    "_id" : ObjectId("..."),
    "Name" : "Venue 1",
    "Properties" : {
            "Chairs" : "18",
            "Tables" : "4",
            "HasWaterfall" : true
    }
}
</code>```

This gives you a possibility to define a projection using dot notation:

```<code>var filter = Builders<Venue>.Filter.Eq(f => f.Name, "Venue 1");
var projection = Builders<Venue>.Projection.Include("Properties.Chairs");

List<BsonDocument> data = Col.Find(filter).Project(projection).ToList();
</code>```

which returns below following BsonDocument:

```<code>{ "_id" : ObjectId("..."), "Properties" : { "Chairs" : "18" } }
</code>```

To get the average you need to use $toInt operator introduced in MongoDB 4.0 to convert your values from string to int. Try:

```<code>var project = new BsonDocument()
{
    { "chairs", new BsonDocument() { { "$toInt", "$Properties.Chairs" } } }
};

var group = new BsonDocument()
{
    { "_id", "null" },
    { "avg", new BsonDocument() { { "$avg", "$chairs" }  } }
};

var avg = Col.Aggregate().Project(project).Group(group).First();
</code>```

Title: MongoDB HashTable Averages

Body:
I am using c#,along with MongoDB.
I have a class that can be resembled by this.
Its a sample, that represents something, please dont comment on the class design

[CollectionName("Venues")]
public class Venue
{
   public string Name { get; set; }
   public dictionary<string,object> Properties { get; set; }
}

var venue = new Venue
{
  Name = "Venue 1",
  Properties = new Dictionary<string,object>
  {
    { "Chairs", "18" },
    { "Tables", "4" },
    { "HasWaterfall", true }
  }
}

Assuming I have an object in a collection, that looks like that.
I would like to find out of it is possible to do two things.

1: Load from the database, only a single item from the dictionary,
   currently I can only see how this can be done, by loading the entire
   record from the database and then manually getting the value by key.

2: Determine the average of a single item within the database.
   For example, across all records I would like to work out the average
   chairs, again without loading all records and then doing it in memory with
   linq etc....

Body:
Basically your sample document gets stored as a below JSON:

{
    "_id" : ObjectId("..."),
    "Name" : "Venue 1",
    "Properties" : {
            "Chairs" : "18",
            "Tables" : "4",
            "HasWaterfall" : true
    }
}

This gives you a possibility to define a projection using dot notation:

var filter = Builders<Venue>.Filter.Eq(f => f.Name, "Venue 1");
var projection = Builders<Venue>.Projection.Include("Properties.Chairs");

List<BsonDocument> data = Col.Find(filter).Project(projection).ToList();

which returns below following BsonDocument:

{ "_id" : ObjectId("..."), "Properties" : { "Chairs" : "18" } }

To get the average you need to use $toInt operator introduced in MongoDB 4.0 to convert your values from string to int. Try:

var project = new BsonDocument()
{
    { "chairs", new BsonDocument() { { "$toInt", "$Properties.Chairs" } } }
};

var group = new BsonDocument()
{
    { "_id", "null" },
    { "avg", new BsonDocument() { { "$avg", "$chairs" }  } }
};

var avg = Col.Aggregate().Project(project).Group(group).First();


答:
Use reduce()

let res = [ 1, 2, 4 ].reduce((ac,a) => (ac[a] = a * 10,ac),{})
console.log(res)```
</div>
</div>
</p>

<p>Or you can also use <code>Object.fromEntries</code></p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>let res = Object.fromEntries([ 1, 2, 4 ].map(x => [x, x * 10]))
console.log(res)</code>```

Title: How can I map an Array or an Object to an Object?

Body:
Use reduce()

let res = [ 1, 2, 4 ].reduce((ac,a) => (ac[a] = a * 10,ac),{})
console.log(res)

Or you can also use Object.fromEntries

let res = Object.fromEntries([ 1, 2, 4 ].map(x => [x, x * 10]))
console.log(res)


问:
Angular showing "Cannot read property 'friends' of null"
Hi I am trying trying to get data from my nodejs backend to angular. And i get the below error message in nodejs

```<code>Cannot read property 'friends' of null
    at UserModel.findOne.select.lean.exec

</code>```

The Api Works fine, if i try to use in postman. But when used in angular i get the error message in nodejs

The server side code is:

```<code>let findFriends = ()=>{
        return new Promise((resolve, reject)=>{
            UserModel.findOne({userId: req.body.userId || req.query.userId})
                .select('-__v -_id -password')
                .lean()
                .exec((err, userFriends)=>{
                    console.log(userFriends.friends.length)
                    if(err){
                        logger.error(err.message, ' friendsController: getAllNonFriends, findFriends', 5)
                        let apiResponse = response.generate(true, `Failed to Find Friends`, 500, null)
                        reject(apiResponse)
                    }else {
                        if(userFriends.friends.length !== 0){
                            resolve(userFriends.friends)
                        }else {
                            resolve(userFriends)
                        }
                    }
                })
        })
    } // end find requests
</code>```

the code in angular:

```<code>public allNonFriends: any = ()=>{
    this.http.getNonFriendsList(this.currentUser).subscribe(
      (apiResponse)=>{
        console.log(apiResponse)
        if(apiResponse.status === 200){
          console.log(apiResponse.data)
        } else {
          console.log(apiResponse.message)
        }
      },
      (err)=>{
        console.log(err)
      }
    )
  } // end all non friends
</code>```

答: I found the solution. I change the parameter from body to query and it worked</p>

Body:
Hi I am trying trying to get data from my nodejs backend to angular. And i get the below error message in nodejs

Cannot read property 'friends' of null
    at UserModel.findOne.select.lean.exec

The Api Works fine, if i try to use in postman. But when used in angular i get the error message in nodejs

The server side code is:

let findFriends = ()=>{
        return new Promise((resolve, reject)=>{
            UserModel.findOne({userId: req.body.userId || req.query.userId})
                .select('-__v -_id -password')
                .lean()
                .exec((err, userFriends)=>{
                    console.log(userFriends.friends.length)
                    if(err){
                        logger.error(err.message, ' friendsController: getAllNonFriends, findFriends', 5)
                        let apiResponse = response.generate(true, `Failed to Find Friends`, 500, null)
                        reject(apiResponse)
                    }else {
                        if(userFriends.friends.length !== 0){
                            resolve(userFriends.friends)
                        }else {
                            resolve(userFriends)
                        }
                    }
                })
        })
    } // end find requests

the code in angular:

public allNonFriends: any = ()=>{
    this.http.getNonFriendsList(this.currentUser).subscribe(
      (apiResponse)=>{
        console.log(apiResponse)
        if(apiResponse.status === 200){
          console.log(apiResponse.data)
        } else {
          console.log(apiResponse.message)
        }
      },
      (err)=>{
        console.log(err)
      }
    )
  } // end all non friends


问:
firestore security rules get subcollection document
I'm facing insufficient permissions for this firestore security check.

```<code>service cloud.firestore
{
  match /databases/{database}/documents
  {
    match /events/{eventID}
    {
      allow read:
        if get(/databases/$(database)/documents/events/$(eventID)/authorizations/$(request.auth.uid)).data.EVENT_READ == true;
    }
  }
}
</code>```

the get document is hardcoded in the firestore database and the simulator returns true but for the actual query returns insufficient privileges.

I tested and moved the authorizations subcollection to the same level as users collection and it works. Am i missing out anything?

Additional testing: Reading the document directly does not result in insufficient privileges. I'm testing to see if it's an issue with listing but to my knowledge read should cover both get and list in firestore security rules.

Update: Listing seems to be the issue here. I tried to list the entire collection with only one document and it results in the insufficient privileges.

Works:

```<code>this.angularFirestore.collection('events').doc(eventID).valueChanges();
</code>```

Doesn't work (updated):

```<code>this.angularFirestore.collection('events', query => query.where('admins', 'array-contains', auth.uid)).valueChanges()
</code>```

My firestore database:

```<code>/events/event1_id
- field 1: some string
- field 2: some string
- admins: array of uid strings
    /authorizations/<uid>  #uid for $(request.auth.uid)
    - EVENT_READ: true
</code>```

Update 2: Updated the doesn't work query string which I tried out. It is intriguing that if i move the /authorizations sub collection out to be the same level as /events collections, the query will not fail.

答:
Your first query works because it's accessing the events collection with a specific document.  That specific document meets the rules criteria, because you've arranged for the get() to allow it.

Your second query doesn't work because it's attempting to get all of the documents in events collection.  Your rule does not specifically allow that.  It looks like you expect your rule to filter out the events that aren't allowed access based on the contents an unknown number of other documents.  You need to be aware that security rules are not filters.  Please click through to the documentation and read that section.  The client must only request documents that are known to be readable according to rules.  It can't depend on rules to filter documents that are not allowed.

If you want to be able to query for all events that the current user has access to, your current database structure will not work.  You will need to put all the relevant information in the events collection itself.  This means you should consider something like putting the UID of each user that's allowed to read the event in the document itself, then filter on that field.  Or have some other collection that you can query in this way.

Body:
I'm facing insufficient permissions for this firestore security check.

service cloud.firestore
{
  match /databases/{database}/documents
  {
    match /events/{eventID}
    {
      allow read:
        if get(/databases/$(database)/documents/events/$(eventID)/authorizations/$(request.auth.uid)).data.EVENT_READ == true;
    }
  }
}

the get document is hardcoded in the firestore database and the simulator returns true but for the actual query returns insufficient privileges.

I tested and moved the authorizations subcollection to the same level as users collection and it works. Am i missing out anything?

Additional testing: Reading the document directly does not result in insufficient privileges. I'm testing to see if it's an issue with listing but to my knowledge read should cover both get and list in firestore security rules.

Update: Listing seems to be the issue here. I tried to list the entire collection with only one document and it results in the insufficient privileges.

Works:

this.angularFirestore.collection('events').doc(eventID).valueChanges();

Doesn't work (updated):

this.angularFirestore.collection('events', query => query.where('admins', 'array-contains', auth.uid)).valueChanges()

My firestore database:

/events/event1_id
- field 1: some string
- field 2: some string
- admins: array of uid strings
    /authorizations/<uid>  #uid for $(request.auth.uid)
    - EVENT_READ: true

Update 2: Updated the doesn't work query string which I tried out. It is intriguing that if i move the /authorizations sub collection out to be the same level as /events collections, the query will not fail.


问:
Excel VBA Dir() Error when file type changes
I'm trying to better understand the Dir function. I have a Dir loop to take action on all .csv files in the directory, but when the loop comes across another file type, like .txt, it will error instead of moving on to the next .csv. item.

This is the relevant portion of my code.

```<code>strSourceExcelLocation = "\\rilinfs001\Interdepartmental\Billings & Deductions\Billing Coordinators\MCB Reports\East\Monthly Quality MCBs (CMQ & SECMQ)\2019\Individual_Reports\" & fldr & "\"

strWorkbook = Dir(strSourceExcelLocation & "*.csv*")

Do While Len(strWorkbook) > 0
    'Open the workbook
    Set wbktoExport = Workbooks.Open(Filename:=strSourceExcelLocation & strWorkbook)

    'Export all sheets as single PDF
    Call Export_Excel_as_PDF(wbktoExport)

    'Get next workbook
    strWorkbook = Dir

    'Close Excel workbook without making changes
    wbktoExport.Close False
Loop
</code>```

So if there are only  .csv files in the directory, then this works fine. When it comes across another file type, an error occurs.

The error is on line

```<code>strWorkbook = Dir
</code>```

Run-time error 5: Invalid procedure call or argument

Am I missing something with the way I use the wildcards in the .csv at the beginning?

Thank you

答:
Try to hardcode the path and give it a try again. Probably the error is something really small in the hardcoding. E.g., in the code below, replace C:\Users\username\Desktop\AAA\ with the path of the file. Then run it. Do not forget the last \. It should work:

```<code>Sub TestMe()

    Dim workbookPath As String
    workbookPath = Dir("C:\Users\username\Desktop\AAA\" & "*.csv")

    Do While Len(workbookPath) > 0
        Debug.Print workbookPath
        workbookPath = Dir
    Loop

End Sub
</code>```

Body:
I'm trying to better understand the Dir function. I have a Dir loop to take action on all .csv files in the directory, but when the loop comes across another file type, like .txt, it will error instead of moving on to the next .csv. item.

This is the relevant portion of my code.

strSourceExcelLocation = "\\rilinfs001\Interdepartmental\Billings & Deductions\Billing Coordinators\MCB Reports\East\Monthly Quality MCBs (CMQ & SECMQ)\2019\Individual_Reports\" & fldr & "\"

strWorkbook = Dir(strSourceExcelLocation & "*.csv*")

Do While Len(strWorkbook) > 0
    'Open the workbook
    Set wbktoExport = Workbooks.Open(Filename:=strSourceExcelLocation & strWorkbook)

    'Export all sheets as single PDF
    Call Export_Excel_as_PDF(wbktoExport)

    'Get next workbook
    strWorkbook = Dir

    'Close Excel workbook without making changes
    wbktoExport.Close False
Loop

So if there are only  .csv files in the directory, then this works fine. When it comes across another file type, an error occurs.

The error is on line

strWorkbook = Dir

Run-time error 5: Invalid procedure call or argument

Am I missing something with the way I use the wildcards in the .csv at the beginning?

Thank you

Body:
Try to hardcode the path and give it a try again. Probably the error is something really small in the hardcoding. E.g., in the code below, replace C:\Users\username\Desktop\AAA\ with the path of the file. Then run it. Do not forget the last \. It should work:

Sub TestMe()

    Dim workbookPath As String
    workbookPath = Dir("C:\Users\username\Desktop\AAA\" & "*.csv")

    Do While Len(workbookPath) > 0
        Debug.Print workbookPath
        workbookPath = Dir
    Loop

End Sub


问:
Remove multiple rows with same index values in python dataframe
I have a dataset with number of startstation IDS, endstation IDS and the duration of travel for bikes in a city.
The data dates back to 2017 and hence now certain stations do not exist.
I have the list of those station IDs. How can I remove rows from the dataframe which either starts or ends at those stations?

For example, if I want to remove StartStation ID = 135 which is in index 4 and 5, what should I do? This entends for a million rows where 135 can be present anywhere.

```<code>Bike   Id      StartStation Id  EndStation Id   Duration
0      395     573              137.0           660.0
1      12931   399              507.0           420.0
2      7120    399              507.0           420.0
3      1198    599              616.0           300.0
4      10739   135              486.0           1260.0
5      10949   135              486.0           1260.0
6      8831    193              411.0           540.0
7      8778    266              770.0           600.0
8      700     137              294.0           540.0
9      5017    456              39.0            3000.0
10     4359    444              445.0           240.0
11     2801    288              288.0           5340.0
12     9525    265              592.0           300.0
</code>```

答:
I'm calling your list of ids to remove removed_ids.</p>

```<code>df=df.loc[
    (~df['StartStation ID'].isin(removed_ids)) &\
    (~df['EndStation ID'].isin(removed_ids))
]
</code>```

Body:
I have a dataset with number of startstation IDS, endstation IDS and the duration of travel for bikes in a city.
The data dates back to 2017 and hence now certain stations do not exist.
I have the list of those station IDs. How can I remove rows from the dataframe which either starts or ends at those stations?

For example, if I want to remove StartStation ID = 135 which is in index 4 and 5, what should I do? This entends for a million rows where 135 can be present anywhere.

Bike   Id      StartStation Id  EndStation Id   Duration
0      395     573              137.0           660.0
1      12931   399              507.0           420.0
2      7120    399              507.0           420.0
3      1198    599              616.0           300.0
4      10739   135              486.0           1260.0
5      10949   135              486.0           1260.0
6      8831    193              411.0           540.0
7      8778    266              770.0           600.0
8      700     137              294.0           540.0
9      5017    456              39.0            3000.0
10     4359    444              445.0           240.0
11     2801    288              288.0           5340.0
12     9525    265              592.0           300.0

Body:
I'm calling your list of ids to remove removed_ids.

df=df.loc[
    (~df['StartStation ID'].isin(removed_ids)) &\
    (~df['EndStation ID'].isin(removed_ids))
]


问:
An array converting question
I have an array below,

```<code>var array = {
    "Id":[1,2,3],
    "Name":["one","two","five"],
    "row":[8,9,7]
}
</code>```

but I want to transform it into

```<code>var array2 =
{"data":
    [
    {"Id":1,"Name":"one","Row:8"},
    {"Id":2,"Name":"two","Row:9"},
    {"Id":3,"Name":"five","Row:7"},
    ]
}
</code>```

Is this possible?

答:
```<code>var array2 = {data: []};
for (i in array.Id) {
    array2.data.push({
        Id: array.Id[i],
        Name: array.Name[i],
        row: array.row[i]
    });
}
</code>```

Didn't test it

Title: An array converting question

Body:
I have an array below,

var array = {
    "Id":[1,2,3],
    "Name":["one","two","five"],
    "row":[8,9,7]
}

but I want to transform it into

var array2 =
{"data":
    [
    {"Id":1,"Name":"one","Row:8"},
    {"Id":2,"Name":"two","Row:9"},
    {"Id":3,"Name":"five","Row:7"},
    ]
}

Is this possible?

Body:
var array2 = {data: []};
for (i in array.Id) {
    array2.data.push({
        Id: array.Id[i],
        Name: array.Name[i],
        row: array.row[i]
    });
}

Didn't test it


问:
Pixel Launcher isn’t responding on emulator Android
I tried to compile the application but in the emulator I get the message  "pixel launcher isn t responding". I changed the performance in Software, but still it appears.plzzzz help, i ve tried 3 days to resolve this but with no results</p>

答:
clearing launcher app data may help,

```<code>adb shell pm clear com.google.android.apps.nexuslauncher
</code>```

I hope it would helpful for you

Body:
clearing launcher app data may help,

adb shell pm clear com.google.android.apps.nexuslauncher

I hope it would helpful for you


问:
How could I make example for sigma type in Coq?
For that type:

```<code>Record Version := mkVersion {
  major  : nat;
  minor  : nat;
  branch : {b:nat| b > 0 /\ b <= 9};
  hotfix : {h:nat| h > 0 /\ h < 8}
}.
</code>```

I'm trying to make an example:

```<code>Example ex1 := mkVersion 3 2 (exist _ 5) (exist _ 5).
</code>```

And it failed with:

The term "exist ?P 5" has type  "?P 5 -> {x : nat | ?P x}" while it is
  expected to have type  "{b : nat | b > 0 /\ b <= 9}".

What am I missing?

答:
The reason it fails is that you need not only provide a witness (b and h in this case) but also a proof that the corresponding condition holds for the provided witness.

I would switch to booleans to make my life easier, because this allows proof by computation, which is basically what eq_refl does in the snippet below:

```<code>From Coq Require Import Bool Arith.

Coercion is_true : bool >-> Sortclass.

Record Version := mkVersion {
  major  : nat;
  minor  : nat;
  branch : {b:nat| (0 <? b) && (b <=? 9)};
  hotfix : {h:nat| (0 <? h) && (h <? 8)}
}.

Example ex1 := mkVersion 3 2 (exist _ 5 eq_refl) (exist _ 5 eq_refl).
</code>```

We could introduce a notation allowing a nicer representation of literals:

```<code>Notation "<| M ',' m ',' b '~' h |>" :=
  (mkVersion M m (exist _ b eq_refl) (exist _ h eq_refl)).

Example ex2 := <| 3,2,5~5 |>.
</code>```

If there is a need to add manual proofs then I'd suggest to use Program mechanism:

```<code>From Coq Require Import Program.

Program Definition ex3 b h (condb : b =? 5) (condh : h =? 1) :=
  mkVersion 3 2 (exist _ b _) (exist _ h _).
Next Obligation.
  now unfold is_true in * |-; rewrite Nat.eqb_eq in * |-; subst. Qed.
Next Obligation.
  now unfold is_true in * |-; rewrite Nat.eqb_eq in * |-; subst. Qed.
</code>```

or refine tactic:

```<code>Definition ex3' b h (condb : b =? 5) (condh : h =? 1) : Version.
Proof.
  now refine (mkVersion 3 2 (exist _ b _) (exist _ h _));
  unfold is_true in * |-; rewrite Nat.eqb_eq in * |-; subst.
Qed.
</code>```

Body:
For that type:

Record Version := mkVersion {
  major  : nat;
  minor  : nat;
  branch : {b:nat| b > 0 /\ b <= 9};
  hotfix : {h:nat| h > 0 /\ h < 8}
}.

I'm trying to make an example:

Example ex1 := mkVersion 3 2 (exist _ 5) (exist _ 5).

And it failed with:

The term "exist ?P 5" has type  "?P 5 -> {x : nat | ?P x}" while it is
  expected to have type  "{b : nat | b > 0 /\ b <= 9}".

What am I missing?

Body:
The reason it fails is that you need not only provide a witness (b and h in this case) but also a proof that the corresponding condition holds for the provided witness.

I would switch to booleans to make my life easier, because this allows proof by computation, which is basically what eq_refl does in the snippet below:

From Coq Require Import Bool Arith.

Coercion is_true : bool >-> Sortclass.

Record Version := mkVersion {
  major  : nat;
  minor  : nat;
  branch : {b:nat| (0 <? b) && (b <=? 9)};
  hotfix : {h:nat| (0 <? h) && (h <? 8)}
}.

Example ex1 := mkVersion 3 2 (exist _ 5 eq_refl) (exist _ 5 eq_refl).

We could introduce a notation allowing a nicer representation of literals:

Notation "<| M ',' m ',' b '~' h |>" :=
  (mkVersion M m (exist _ b eq_refl) (exist _ h eq_refl)).

Example ex2 := <| 3,2,5~5 |>.

If there is a need to add manual proofs then I'd suggest to use Program mechanism:

From Coq Require Import Program.

Program Definition ex3 b h (condb : b =? 5) (condh : h =? 1) :=
  mkVersion 3 2 (exist _ b _) (exist _ h _).
Next Obligation.
  now unfold is_true in * |-; rewrite Nat.eqb_eq in * |-; subst. Qed.
Next Obligation.
  now unfold is_true in * |-; rewrite Nat.eqb_eq in * |-; subst. Qed.

or refine tactic:

Definition ex3' b h (condb : b =? 5) (condh : h =? 1) : Version.
Proof.
  now refine (mkVersion 3 2 (exist _ b _) (exist _ h _));
  unfold is_true in * |-; rewrite Nat.eqb_eq in * |-; subst.
Qed.


问:
From dataFrame to grouped JSON in R
I have a data frame in R:

```<code>ID   var1  var2
1    12    Name1
1    13    Name4
2    13    Name2
3    14    Name3
</code>```

and i wish to transform in json grouping the ones that have the same ID:

```<code>{
"group":
[
{"ID" : 1, "var1": 12, "var2": "Name1" },
{"ID" : 1, "var1": 13, "var2": "Name4" }
]
},
{
"group":
[
{"ID" : 2, "var1": 13, "var2": "Name2" }
]
},
{
"group":
[
{"ID" : 3, "var1": 14, "var2": "Name3" }
]
}
</code>```

I also want to save each group on a JSON file, so, for the example above, I will have three JSON files

But I can't figure out a way to do this, neither to mount the JSON as I want nor to save each group separately on a JSON file.

答:
We can use jsonlite

```<code>library(jsonlite)
library(purrr)
lst1 <-  map(split(df1, df1$ID), toJSON)
names(lst1) <- paste0('group', names(lst1))
toJSON(lst1)
</code>```

Title: From dataFrame to grouped JSON in R

Body:
I have a data frame in R:

ID   var1  var2
1    12    Name1
1    13    Name4
2    13    Name2
3    14    Name3

and i wish to transform in json grouping the ones that have the same ID:

{
"group":
[
{"ID" : 1, "var1": 12, "var2": "Name1" },
{"ID" : 1, "var1": 13, "var2": "Name4" }
]
},
{
"group":
[
{"ID" : 2, "var1": 13, "var2": "Name2" }
]
},
{
"group":
[
{"ID" : 3, "var1": 14, "var2": "Name3" }
]
}

I also want to save each group on a JSON file, so, for the example above, I will have three JSON files

But I can't figure out a way to do this, neither to mount the JSON as I want nor to save each group separately on a JSON file.

Body:
We can use jsonlite

library(jsonlite)
library(purrr)
lst1 <-  map(split(df1, df1$ID), toJSON)
names(lst1) <- paste0('group', names(lst1))
toJSON(lst1)


问:
Incomplete JSON data on GET request in ASP.NET Core
Why I get not all data from base by link
https://localhost:XXXXX/api/comments
(GET request)
After update page data no longer appears ..

Responce:
[{"id":1,"text":"Comment1","userId":1,"parentCommentId":null,"user":null,"parentComment":null,"childrenComments":null},{"id":2,"text":"Comment2","userId":1,"parentCommentId":1,"user":null,"parentComment":{"id":1,"text":"Comment1","userId":1,"parentCommentId":null,"user":null,"parentComment":null,"childrenComments":[

Does not load subordinate item ..
What am I doing wrong?

```<code>// GET: api/Comments
[HttpGet]
public IEnumerable<Comment> GetComments()
{
    return _context.Comments;
}
</code>```

答:
You must load the relationships as well. The two primary ways of doing that are eager-loading via Include or lazy-loading. However, lazy-loading should be avoided in general, and especially so in cases like this. When you're serializing an object, you could end up issuing hundreds or even thousands of queries inadvertently with lazy-loading.

Long and short, add Include clauses for the relationships you care about:

```<code>return _context.Comments
    .Include(x => x.User)
    .Include(x => x.parentComment)
    .Include(x => x.childrenComments);
</code>```

If you want more flexibility, you can employ either OData or GraphQL. Either will allow the client to selectively include the relationships they want/need, meaning you won't necessarily need to join everything every time.

Body:
Why I get not all data from base by link
https://localhost:XXXXX/api/comments
(GET request)
After update page data no longer appears ..

Responce:
[{"id":1,"text":"Comment1","userId":1,"parentCommentId":null,"user":null,"parentComment":null,"childrenComments":null},{"id":2,"text":"Comment2","userId":1,"parentCommentId":1,"user":null,"parentComment":{"id":1,"text":"Comment1","userId":1,"parentCommentId":null,"user":null,"parentComment":null,"childrenComments":[

Does not load subordinate item ..
What am I doing wrong?

// GET: api/Comments
[HttpGet]
public IEnumerable<Comment> GetComments()
{
    return _context.Comments;
}

Body:
You must load the relationships as well. The two primary ways of doing that are eager-loading via Include or lazy-loading. However, lazy-loading should be avoided in general, and especially so in cases like this. When you're serializing an object, you could end up issuing hundreds or even thousands of queries inadvertently with lazy-loading.

Long and short, add Include clauses for the relationships you care about:

return _context.Comments
    .Include(x => x.User)
    .Include(x => x.parentComment)
    .Include(x => x.childrenComments);

If you want more flexibility, you can employ either OData or GraphQL. Either will allow the client to selectively include the relationships they want/need, meaning you won't necessarily need to join everything every time.


问:
How to calculate yesterday records with today records using progress 4gl?
I have written a program and i need to calculate yesterday records(timing 12:00 AM to 05:00 AM) with today records

```<code>for each womf_worder of sfcf_au_ship where
         womf_worder.word_production_status EQ "B"                      and
         womf_worder.word_build_date        EQ today - 1                and
         womf_worder.word_build_time        GE tt_shift.shft_start_hour and
         womf_worder.word_build_time        LE tt_shift.shft_stop_hour
         no-lock:

    assign i = i + 1.
end.
</code>```

But the problem is I need to calculate the orders only on those mentioned time(12:00 AM to 05:00 AM) with today records. Could you please help this case?

答:
From what I understand it looks you need to use brackets and an "OR" operator,
so you'll need something like this:

```<code>FOR EACH  womf_worder OF sfcf_au_ship no-lock
   WHERE  womf_worder.word_production_status EQ "B"
     AND (womf_worder.word_build_date        EQ today - 1
     AND  womf_worder.word_build_time        GE tt_shift.shft_start_hour)
      OR (womf_worder.word_build_date        EQ today
     AND  womf_worder.word_build_time        LE tt_shift.shft_stop_hour)
          :
   ASSIGN i = i + 1.
END.
</code>```

Hope this helps.

Body:
I have written a program and i need to calculate yesterday records(timing 12:00 AM to 05:00 AM) with today records

for each womf_worder of sfcf_au_ship where
         womf_worder.word_production_status EQ "B"                      and
         womf_worder.word_build_date        EQ today - 1                and
         womf_worder.word_build_time        GE tt_shift.shft_start_hour and
         womf_worder.word_build_time        LE tt_shift.shft_stop_hour
         no-lock:

    assign i = i + 1.
end.

But the problem is I need to calculate the orders only on those mentioned time(12:00 AM to 05:00 AM) with today records. Could you please help this case?

Body:
From what I understand it looks you need to use brackets and an "OR" operator,
so you'll need something like this:

FOR EACH  womf_worder OF sfcf_au_ship no-lock
   WHERE  womf_worder.word_production_status EQ "B"
     AND (womf_worder.word_build_date        EQ today - 1
     AND  womf_worder.word_build_time        GE tt_shift.shft_start_hour)
      OR (womf_worder.word_build_date        EQ today
     AND  womf_worder.word_build_time        LE tt_shift.shft_stop_hour)
          :
   ASSIGN i = i + 1.
END.

Hope this helps.


问:
Optical Flow: What exactly is the temporal derivative?
I'm trying to understand what the meaning of a temporal derivative is in an image. While I understand the brightness constancy equation, I don't understand why taking the difference between two images gives me the temporal derivative.

Taking the difference between two frames gives me the difference in pixel intensity per pixel between the two, but how is that the same as asking how much the image changed over a certain span of time?

答:
The temporal derivative dI/dt of the image I(x,y,t) is the rate of change of the image over time at a particular position. As you noted, this is the difference in pixel intensity between the two frames. Considering a single pixel at (x,y), the finite difference approximation to the derivative is

f_d = ( I(x,y,t+delta) - I(x,y,t) ) / delta so that f_d -> dI/dt as delta -> 0.

In this case delta is simply set to one. So we are approximating the image derivative (with respect to time) by the difference between adjacent frames.

One aspect that may be confusing is how that relates to the movement of objects in the image. If you have some physics background, for instance, you might think about the difference between Eulerian and Lagrangian frames of reference: in the more intuitive Lagrangian viewpoint, you consider an object moving by tracking it over the pixels (space) in which it moves, e.g. watching a cat as it hops over a fence. The Eulerian view, which is closer to what we do in optical flow, is to track what happens at a single pixel, and never take our eyes off of it. As the cat passes over that area of (pixel) space, the pixel's values will change, and then go back to "normal" when it's gone.

These two views are in some sense equivalent, but may be useful in difference situations. In computer vision, tracking an object is hard, while computing these Eulerian-like temporal derivatives is easy. Ideally, we could track the cat: consider a point p(t)=(x_p(t),y_p(t)) on say its head, then compute dp/dt and figure out p(t) for all t, and use that for downstream processing. Unfortunately, this is hard, so instead we hope that brightness constancy is usually locally true, and use the optical flow to estimate dp/dt. Of course, dI/dt often does not correspond well to dp/dt (this is why the brightness constancy is an assumption). For instance, consider a light moving around a stationary sphere: dI/dt will be large, but dp/dt will be zero.


问:
Selecting adjacent and previous siblings in CSS
Say for instance I have the following HTML

```<code><ul>
  <li><a href="a">A</a></li>
  <li><a href="b">B</a></li>
  <li class="myclass"><a href="c">C</a></li>
  <li><a href="d">D</a></li>
  <li><a href="e">E</a></li>
</ul>
</code>```

Now I can detemine the next sibling to the li with the class="myclass" by using the following css .someclass + li {}. However if I wanted to apply a CSS rule to previous li, is it possible to do this using css rather than jQuery's $("#some-id").prevAll() or writing some JavaScript to find this?

I'm not that confident as I've never heard of this and my searching hasn't provided a solution.

Thanks

答: Nope, in CSS you can't 'traverse' the dom structure. You can only inherit style definitions recursively.</p>

Body:
Say for instance I have the following HTML

A

B

C

D

E

Now I can detemine the next sibling to the li with the class="myclass" by using the following css .someclass + li {}. However if I wanted to apply a CSS rule to previous li, is it possible to do this using css rather than jQuery's $("#some-id").prevAll() or writing some JavaScript to find this?

I'm not that confident as I've never heard of this and my searching hasn't provided a solution.

Thanks


问:
I wrote some jquery, html and php code for e-mail form contact but something is wrong
I wrote a code in html, php and jquery in order to be able to make e-mail form on a web site.

However, some problems occurred while testing:

1) problem with validations: in my jquery code, in order to prevent submitting the form before it checks if all the fields are ok, I put ~ e.preventDefault(); ~ insde the jquery code.
Then if I try to submit the form, the script will check if the fields are ok and show errors inside input fields for inputs which are not ok. And, offcourse, it wont send the mail.
But, if I fill all the fields correctly, It won't send the mail either.

2) on the other hand, if I remove the above mentioned ~ e.preventDefault(); ~ code, than the script bypasses field validation checks and sends alerts (for the fields not correctly written) through the Exception alert messages.

3) if all other fields are correctly filled, the script requires the attachment to be sent as well. How to prevent that.

Please help, obviously I made some mistakes inside the code.

My code is:

HTML code:

```<code><form id="kontakt_obrazac" name="kontakt_obrazac" method="POST" action="php/kontakt.php" enctype="multipart/form-data">
    <ul>
        <li id="ime"><input type="text" name="ime" placeholder="Ime i prezime" id="input_ime" /></li>
        <li id="mail"><input type="text" name="mail" placeholder="Mail" id="input_mail" /></li>
        <li id="naslov"><input type="text" name="naslov" placeholder="Naslov" id="input_naslov" /></li>
        <li id="poruka"><textarea name="poruka" placeholder="Poruka" id="input_poruka"></textarea></li>
        <li id="file"><input type="file" name="attachment" value="" id="input_file" /></li>
        <li id="posalji"><input type="submit" name="submit_btn" value="Pošalji" class="submit_btn" /></li>
        <li id="ponisti"><input type="reset" name="reset_btn" value="Poništi" /></li>
    </ul>
</form>
</code>```

PHP code:

```<code><?php
/* Namespace alias. */
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

/* Include the Composer generated autoload.php file. */
require 'C:\xampp\composer\vendor\autoload.php';

/* Create a new PHPMailer object. Passing TRUE to the constructor enables exceptions. */
$mail = new PHPMailer(TRUE);

/* Open try/catch block. */
try
{
if(isset($_POST['submit_btn']))
{

    $user_name = $_POST['ime'];
    $user_mail = $_POST['mail'];
    $subject = $_POST['naslov'];
    $message = $_POST['poruka'];

    /* Set the mail sender. */
    $mail->setFrom($_POST['mail'], $_POST['ime']);

    /* Add a recipient. */
    $mail->addAddress('nebojsano@gmail.com');

    /* Set the subject. */
    $mail->Subject = $_POST['naslov'];

    /* Set the mail message body. */
    $mail->Body = ($_POST['poruka']);

    /* SMTP parameters. */

    /* Tells PHPMailer to use SMTP. */
    $mail->isSMTP();

    /* SMTP server address. */
    $mail->Host = 'smtp.gmail.com';

    /* Use SMTP authentication. */
    $mail->SMTPAuth = TRUE;

    /* Set the encryption system. */
    $mail->SMTPSecure = 'tls';

    /* SMTP authentication username. */
    $mail->Username = 'nebojsano@gmail.com';

    /* SMTP authentication password. */
    $mail->Password = 'mmajke';

    /* Set the SMTP port. */
    $mail->Port = 587;

    /* Add an attachment */
    $mail->addAttachment($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name']);

    /* Set a reply-to address */
    $mail->addReplyTo($_POST['mail'], $_POST['ime']);

    /* Add CC and BCC recipients */
    // $mail->addCC('stones2n@hotmail.com');

    /* Enable SMTP debug output. */
    //$mail->SMTPDebug = 4;

    /* Disable some SSL checks. */
        $mail->SMTPOptions = array(
            'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
          )
       );

    /* Finally send the mail. */
    if(!$mail->Send())
    {
        $error = "Mailer Error: " . $mail->ErrorInfo;
        echo "Problem in Sending Mail.";
    }
    else
    {
        echo "Mail Sent.";
    }

}
}
catch (Exception $e)
{
    echo $e->errorMessage();
}
catch (\Exception $e)
{
    echo $e->getMessage();
}
?>
</code>```

JQUERY code:

```<code><script>

$(document).ready(function() {

    $('#kontakt_obrazac').submit(function(e) {
    e.preventDefault();
        var ime = $('#input_ime').val();
        var mail = $('#input_mail').val();
        var naslov = $('#input_naslov').val();
        var poruka = $('#input_poruka').val();
        var attachment = $('#input_file').val();

        $(".error").remove();

        if (ime.length < 1) {
            $('#input_ime').css({color: "#ff0000"});
            $('#input_ime').css({border: "1px solid #ff0000"});
            $('#input_ime').attr('placeholder', 'Obavezno polje');
        } else {
            $('#input_ime').css({color: "#006600"});
            $('#input_ime').css({border: "none"});
        }

        if (mail.length < 1) {
            $('#input_mail').css({color: "#ff0000"});
            $('#input_mail').css({border: "1px solid #ff0000"});
            $('#input_mail').attr('placeholder', 'Obavezno polje');
        } else {
            var reg_ex = /^[A-Za-z0-9][A-Za-z0-9._%+-]{0,63}@(?:[A-Za-z0-9-]{1,63}\.){1,125}[A-Za-z]{2,63}$/;
            var valid_mail = reg_ex.test(mail);
            if (valid_mail) {
                $('#input_mail').css({color: "#006600"});
                $('#input_mail').css({border: "none"});
            } else {
                $('#input_mail').css({color: "#ff0000"});
                $('#input_mail').css({border: "1px solid #ff0000"});
            }
        }

        if (naslov.length < 1) {
            $('#input_naslov').css({color: "#ff0000"});
            $('#input_naslov').css({border: "1px solid #ff0000"});
            $('#input_naslov').attr('placeholder', 'Obavezno polje');
        } else {
            $('#input_naslov').css({color: "#006600"});
            $('#input_naslov').css({border: "none"});
        }

        if (poruka.length < 1) {
            $('#input_poruka').css({color: "#ff0000"});
            $('#input_poruka').attr('placeholder', 'Obavezno polje');
        } else {
            $('#input_poruka').css({color: "green"});
        }

    });

});

</script>
</code>```

答:
In your jquery code when you do the preventDefault, at the end of your validation you can return false if the validation failed, and return true to continue the post of the form.
Not sure what you mean with your 3rd problem.</p>

Body:
I wrote a code in html, php and jquery in order to be able to make e-mail form on a web site.

However, some problems occurred while testing:

1) problem with validations: in my jquery code, in order to prevent submitting the form before it checks if all the fields are ok, I put ~ e.preventDefault(); ~ insde the jquery code.
Then if I try to submit the form, the script will check if the fields are ok and show errors inside input fields for inputs which are not ok. And, offcourse, it wont send the mail.
But, if I fill all the fields correctly, It won't send the mail either.

2) on the other hand, if I remove the above mentioned ~ e.preventDefault(); ~ code, than the script bypasses field validation checks and sends alerts (for the fields not correctly written) through the Exception alert messages.

3) if all other fields are correctly filled, the script requires the attachment to be sent as well. How to prevent that.

Please help, obviously I made some mistakes inside the code.

My code is:

HTML code:

<form id="kontakt_obrazac" name="kontakt_obrazac" method="POST" action="php/kontakt.php" enctype="multipart/form-data">

<input type="text" name="ime" placeholder="Ime i prezime" id="input_ime" />

<input type="text" name="mail" placeholder="Mail" id="input_mail" />

<input type="text" name="naslov" placeholder="Naslov" id="input_naslov" />

<textarea name="poruka" placeholder="Poruka" id="input_poruka"></textarea>

<input type="file" name="attachment" value="" id="input_file" />

<input type="submit" name="submit_btn" value="Pošalji" class="submit_btn" />

<input type="reset" name="reset_btn" value="Poništi" />

</form>

PHP code:

<?php
/* Namespace alias. */
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

/* Include the Composer generated autoload.php file. */
require 'C:\xampp\composer\vendor\autoload.php';

/* Create a new PHPMailer object. Passing TRUE to the constructor enables exceptions. */
$mail = new PHPMailer(TRUE);

/* Open try/catch block. */
try
{
if(isset($_POST['submit_btn']))
{

    $user_name = $_POST['ime'];
    $user_mail = $_POST['mail'];
    $subject = $_POST['naslov'];
    $message = $_POST['poruka'];

    /* Set the mail sender. */
    $mail->setFrom($_POST['mail'], $_POST['ime']);

    /* Add a recipient. */
    $mail->addAddress('nebojsano@gmail.com');

    /* Set the subject. */
    $mail->Subject = $_POST['naslov'];

    /* Set the mail message body. */
    $mail->Body = ($_POST['poruka']);

    /* SMTP parameters. */

    /* Tells PHPMailer to use SMTP. */
    $mail->isSMTP();

    /* SMTP server address. */
    $mail->Host = 'smtp.gmail.com';

    /* Use SMTP authentication. */
    $mail->SMTPAuth = TRUE;

    /* Set the encryption system. */
    $mail->SMTPSecure = 'tls';

    /* SMTP authentication username. */
    $mail->Username = 'nebojsano@gmail.com';

    /* SMTP authentication password. */
    $mail->Password = 'mmajke';

    /* Set the SMTP port. */
    $mail->Port = 587;

    /* Add an attachment */
    $mail->addAttachment($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name']);

    /* Set a reply-to address */
    $mail->addReplyTo($_POST['mail'], $_POST['ime']);

    /* Add CC and BCC recipients */
    // $mail->addCC('stones2n@hotmail.com');

    /* Enable SMTP debug output. */
    //$mail->SMTPDebug = 4;

    /* Disable some SSL checks. */
        $mail->SMTPOptions = array(
            'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
          )
       );

    /* Finally send the mail. */
    if(!$mail->Send())
    {
        $error = "Mailer Error: " . $mail->ErrorInfo;
        echo "Problem in Sending Mail.";
    }
    else
    {
        echo "Mail Sent.";
    }

}
}
catch (Exception $e)
{
    echo $e->errorMessage();
}
catch (\Exception $e)
{
    echo $e->getMessage();
}
?>

JQUERY code:


问:
The database cannot be opened because it is version 782. This server supports version 706 and earlier. A downgrade path is not supported
I have created a sample database using SQL Server 2014 Express and added it to my Windows Form solution. When double click on it to open I get this error.

The database cannot be opened because it is version 782. This server
  supports version 706 and earlier. A downgrade path is not supported

I am using Visual Studio 2013. I really don't understand that I am using the two latest versions of Microsoft products and they are incompatible. Am I missing something? How can I open this database?

答:
I use VS 2017. By default SQL Server Instance name is (LocalDB)\MSSQLLocalDB.
However, downgrading the compatibility level of the database to 110 as in @user3390927's answer, I could attach the database file in VS, choosing Server Name as "localhost\SQLExpress".</p>


问:
Looping over Pymongo cursor returns bson.errors.InvalidBSON error after some iterations
I'm trying making a simple query with pymongo and looping over the results.

This is the code I'm using:

```<code>data = []
tam = db.my_collection.find({'timestamp': {'$gte': start, '$lte':end}}).count()
for i,d in enumerate(table.find({'timestamp': {'$gte': start, '$lte':end}}):
    print('%s of %s' % (i,tam))
    data.append(d)
</code>```

start and end variables are datetime python objects. Everything runs fine until I get the following output:

```<code>2987 of 12848
2988 of 12848
2989 of 12848
2990 of 12848
2991 of 12848
2992 of 12848
Traceback (most recent call last):
  File "db_extraction\extract_data.py", line 68, in <module>
    data = extract_data(yesterday,days = 1)
  File "db_extraction\extract_data.py", line 24, in extract_data
    for i,d in enumerate(table.find({'timestamp': {'$gte': start, '$lte':end}}).limit(100000)):
  File "\venv\lib\site-packages\pymongo\cursor.py", line 1169, in next
    if len(self.__data) or self._refresh():
  File "\venv\lib\site-packages\pymongo\cursor.py", line 1106, in _refresh
    self.__send_message(g)
  File "\venv\lib\site-packages\pymongo\cursor.py", line 971, in __send_message
    codec_options=self.__codec_options)
  File "\venv\lib\site-packages\pymongo\cursor.py", line 1055, in _unpack_response
    return response.unpack_response(cursor_id, codec_options)
  File "\venv\lib\site-packages\pymongo\message.py", line 945, in unpack_response
    return bson.decode_all(self.documents, codec_options)
bson.errors.InvalidBSON
</code>```

First thing I've tried is changing the range of the query to check if it is data related, and it's not. Another range stops at 1615 of 6360 and same error.

I've also tried list(table.find({'timestamp': {'$gte': start, '$lte':end}}) and same error.

Another maybe relevant info is that first queries are really fast. It freezes on the last number for a while before returning the error.

So I need some help. Am I hitting limits here? Or any clue on whats going on?

This is might be related with this 2013 question, but the author says that he gets no error output.

Thanks!

EDIT:

First thank you all for your time and suggestions. Unfortunately, I've tested all sugestions and I get the same error at the same spot. I've printed the problematic file using mongo shell and it is pretty much the same as all others.
I changed the range of the query and tried picking up other days. Same problem in all days, until I found one random run that gave me a MEMORY ERROR.

```<code>1737 of 8011
1738 of 8011
1739 of 8011
1740 of 8011
1741 of 8011
Traceback (most recent call last):
  File "db_extraction\pymongo_test.py", line 14, in <module>
    for post in all_posts:
  File "\python_modules\venv\lib\site-packages\pymongo\cursor.py", line 1189, in next
    if len(self.__data) or self._refresh():
  File "\python_modules\venv\lib\site-packages\pymongo\cursor.py", line 1126, in _refresh
    self.__send_message(g)
  File "\python_modules\venv\lib\site-packages\pymongo\cursor.py", line 931, in __send_message
    operation, exhaust=self.__exhaust, address=self.__address)
  File "\python_modules\venv\lib\site-packages\pymongo\mongo_client.py", line 1145, in _send_message_with_response
    exhaust)
  File "\python_modules\venv\lib\site-packages\pymongo\mongo_client.py", line 1156, in _reset_on_error
    return func(*args, **kwargs)
  File "\python_modules\venv\lib\site-packages\pymongo\server.py", line 106, in send_message_with_response
    reply = sock_info.receive_message(request_id)
  File "\python_modules\venv\lib\site-packages\pymongo\pool.py", line 612, in receive_message
    self._raise_connection_failure(error)
  File "\python_modules\venv\lib\site-packages\pymongo\pool.py", line 745, in _raise_connection_failure
    raise error
  File "\python_modules\venv\lib\site-packages\pymongo\pool.py", line 610, in receive_message
    self.max_message_size)
  File "\python_modules\venv\lib\site-packages\pymongo\network.py", line 191, in receive_message
    data = _receive_data_on_socket(sock, length - 16)
  File "\python_modules\venv\lib\site-packages\pymongo\network.py", line 227, in _receive_data_on_socket
    buf = bytearray(length)
MemoryError
</code>```

This is intermitent. I ran again without changing anything and got the old invalidBSON error, and ran again and got Memory Error.

I started the task manager and ran again, and the memory indeed grows fast up to 95% usage and hangs there. The query should retrieve something like 1GB of data in 8GB RAM machine so... I dont know if this is suposed to happen. Anyway a code suggestion that retrieves the data from mongoDB with pymongo and writes to a file without putting everything into memory probably will do the job. The bonus would be if someone could explain why I'm getting an invalid BSON instead of MemoryError (for vast majority of runs) in my case.

Thanks

答:
I ran into this same exact problem myself, it ended up having nothing to do with the documents themselves but the amount of memory that the program was taking up during large queries.

In our specific case, when running the broken query that was giving us this exact bug by itself in a separate script, the bug didn't occur. Eventually we found that we were using a uwsgi config setting:

```<code>limit-as = 512
</code>```

This would immediately kill our process when address space reached 512M, resulting in either an InvalidBSON error OR a MemoryError interchangeably, seemingly at random.

We fixed this by changing the limit-as setting it to reload-on-as instead:

```<code>reload-on-as = 512
</code>```

Ultimately we ended up with deciding to break up large queries like this into smaller pieces and performing them sequentially instead of all at once anyway, but we at least determined it was an external cause instead of an issue with the pymongo driver itself.

Body:
I'm trying making a simple query with pymongo and looping over the results.

This is the code I'm using:

data = []
tam = db.my_collection.find({'timestamp': {'$gte': start, '$lte':end}}).count()
for i,d in enumerate(table.find({'timestamp': {'$gte': start, '$lte':end}}):
    print('%s of %s' % (i,tam))
    data.append(d)

start and end variables are datetime python objects. Everything runs fine until I get the following output:

2987 of 12848
2988 of 12848
2989 of 12848
2990 of 12848
2991 of 12848
2992 of 12848
Traceback (most recent call last):
  File "db_extraction\extract_data.py", line 68, in <module>
    data = extract_data(yesterday,days = 1)
  File "db_extraction\extract_data.py", line 24, in extract_data
    for i,d in enumerate(table.find({'timestamp': {'$gte': start, '$lte':end}}).limit(100000)):
  File "\venv\lib\site-packages\pymongo\cursor.py", line 1169, in next
    if len(self.__data) or self._refresh():
  File "\venv\lib\site-packages\pymongo\cursor.py", line 1106, in _refresh
    self.__send_message(g)
  File "\venv\lib\site-packages\pymongo\cursor.py", line 971, in __send_message
    codec_options=self.__codec_options)
  File "\venv\lib\site-packages\pymongo\cursor.py", line 1055, in _unpack_response
    return response.unpack_response(cursor_id, codec_options)
  File "\venv\lib\site-packages\pymongo\message.py", line 945, in unpack_response
    return bson.decode_all(self.documents, codec_options)
bson.errors.InvalidBSON

First thing I've tried is changing the range of the query to check if it is data related, and it's not. Another range stops at 1615 of 6360 and same error.

I've also tried list(table.find({'timestamp': {'$gte': start, '$lte':end}}) and same error.

Another maybe relevant info is that first queries are really fast. It freezes on the last number for a while before returning the error.

So I need some help. Am I hitting limits here? Or any clue on whats going on?

This is might be related with this 2013 question, but the author says that he gets no error output.

Thanks!

EDIT:

First thank you all for your time and suggestions. Unfortunately, I've tested all sugestions and I get the same error at the same spot. I've printed the problematic file using mongo shell and it is pretty much the same as all others.
I changed the range of the query and tried picking up other days. Same problem in all days, until I found one random run that gave me a MEMORY ERROR.

1737 of 8011
1738 of 8011
1739 of 8011
1740 of 8011
1741 of 8011
Traceback (most recent call last):
  File "db_extraction\pymongo_test.py", line 14, in <module>
    for post in all_posts:
  File "\python_modules\venv\lib\site-packages\pymongo\cursor.py", line 1189, in next
    if len(self.__data) or self._refresh():
  File "\python_modules\venv\lib\site-packages\pymongo\cursor.py", line 1126, in _refresh
    self.__send_message(g)
  File "\python_modules\venv\lib\site-packages\pymongo\cursor.py", line 931, in __send_message
    operation, exhaust=self.__exhaust, address=self.__address)
  File "\python_modules\venv\lib\site-packages\pymongo\mongo_client.py", line 1145, in _send_message_with_response
    exhaust)
  File "\python_modules\venv\lib\site-packages\pymongo\mongo_client.py", line 1156, in _reset_on_error
    return func(*args, **kwargs)
  File "\python_modules\venv\lib\site-packages\pymongo\server.py", line 106, in send_message_with_response
    reply = sock_info.receive_message(request_id)
  File "\python_modules\venv\lib\site-packages\pymongo\pool.py", line 612, in receive_message
    self._raise_connection_failure(error)
  File "\python_modules\venv\lib\site-packages\pymongo\pool.py", line 745, in _raise_connection_failure
    raise error
  File "\python_modules\venv\lib\site-packages\pymongo\pool.py", line 610, in receive_message
    self.max_message_size)
  File "\python_modules\venv\lib\site-packages\pymongo\network.py", line 191, in receive_message
    data = _receive_data_on_socket(sock, length - 16)
  File "\python_modules\venv\lib\site-packages\pymongo\network.py", line 227, in _receive_data_on_socket
    buf = bytearray(length)
MemoryError

This is intermitent. I ran again without changing anything and got the old invalidBSON error, and ran again and got Memory Error.

I started the task manager and ran again, and the memory indeed grows fast up to 95% usage and hangs there. The query should retrieve something like 1GB of data in 8GB RAM machine so... I dont know if this is suposed to happen. Anyway a code suggestion that retrieves the data from mongoDB with pymongo and writes to a file without putting everything into memory probably will do the job. The bonus would be if someone could explain why I'm getting an invalid BSON instead of MemoryError (for vast majority of runs) in my case.

Thanks

Body:
I ran into this same exact problem myself, it ended up having nothing to do with the documents themselves but the amount of memory that the program was taking up during large queries.

In our specific case, when running the broken query that was giving us this exact bug by itself in a separate script, the bug didn't occur. Eventually we found that we were using a uwsgi config setting:

limit-as = 512

This would immediately kill our process when address space reached 512M, resulting in either an InvalidBSON error OR a MemoryError interchangeably, seemingly at random.

We fixed this by changing the limit-as setting it to reload-on-as instead:

reload-on-as = 512

Ultimately we ended up with deciding to break up large queries like this into smaller pieces and performing them sequentially instead of all at once anyway, but we at least determined it was an external cause instead of an issue with the pymongo driver itself.


问:
 vs
 vs <Fragment>
I got confused about the listed elements.

What I already know is:

They all don't have real meanings.

Fragment is not a real element.

Can you explain what are the key differences between them? When should we choose one over the other?

答:
I'm not sure about <fragment> but can tell about differences of
 and .

The difference between  and
 is that a  element is in-line and usually used for a small chunk of HTML inside a line (such as inside a paragraph), whereas a
 (division) element is block-line (which is basically equivalent to having a line-break before and after it) and used to group larger chunks of code.

 example:

London

London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.

```

<p><code><span></code> example:</p>

<pre class="lang-html prettyprint-override"><code><h1>My <span style="color:red">Important</span> Heading</h1>
</code>```

A final example is when you use

Hello

World

```

<p>that gives this:</p>

<blockquote>
  <p>Hello<br>
  World</p>
</blockquote>

<p><code><div></code> gives a new line between each because it is a block-level element, whereas <code><span></code> doesn't:</p>

<pre class="lang-html prettyprint-override"><code><span>Hello</span>
<span>World</span>
</code>```

Hello World

Title: <span> vs <div> vs <Fragment>

Body:
I'm not sure about <fragment> but can tell about differences of <div> and <span>.

The difference between <span> and <div> is that a <span> element is in-line and usually used for a small chunk of HTML inside a line (such as inside a paragraph), whereas a <div> (division) element is block-line (which is basically equivalent to having a line-break before and after it) and used to group larger chunks of code.

<div> example:

<div style="background-color:black;color:white;padding:20px;">
  <h2>London</h2>
  <p>London is the capital city of England. It is the most populous city in the United Kingdom, with a metropolitan area of over 13 million inhabitants.</p>
</div>

<span> example:

<h1>My <span style="color:red">Important</span> Heading</h1>

A final example is when you use

<div>Hello</div>
<div>World</div>

that gives this:

Hello

  World

<div> gives a new line between each because it is a block-level element, whereas <span> doesn't:

<span>Hello</span>
<span>World</span>

Hello World