问:
How to repair a Parallel for()-loop not to return a different value than a serial for()-loop?
I'm having an issue with the following code. The code works with no errors but I'm receiving different output values when using a parallel for loop vs a regular for loop. I need to get the parallel for loop working properly because I run this code thousands of times. Does anyone know why my parallel for loop is returning different outputs?

```<code>private object _lock = new object();

public double CalculatePredictedRSquared()
    {
        double press = 0, tss = 0, press2 = 0, press1 = 0;
        Vector<double> output = CreateVector.Dense(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)).ToArray());
        List<double> input1 = new List<double>(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)));
        List<double> input2 = new List<double>(Enumerable.Range(200, 400).Select(i => Convert.ToDouble(i)));

            Parallel.For(0, output.Count, i =>
            {
                ConcurrentBag<MultipleRegressionInfo> listMRInfoBag = new ConcurrentBag<MultipleRegressionInfo>(listMRInfo);
                ConcurrentBag<double> vectorArrayBag = new ConcurrentBag<double>(output);
                ConcurrentBag<double[]> matrixList = new ConcurrentBag<double[]>();

                lock (_lock)
                {
                    matrixList.Add(input1.Where((v, k) => k != i).ToArray());
                    matrixList.Add(input2.Where((v, k) => k != i).ToArray());
                }

                var matrixArray2 = CreateMatrix.DenseOfColumnArrays(matrixList);
                var actualResult = vectorArrayBag.ElementAt(i);
                var newVectorArray = CreateVector.Dense(vectorArrayBag.Where((v, j) => j != i).ToArray());
                var items = FindBestMRSolution(matrixArray2, newVectorArray);
                double estimate1 = 0;

                if (items != null)
                {
                    lock (_lock)
                    {
                        var y = 0d;
                        var independentCount = matrixArray2.RowCount;
                        var dependentCount = newVectorArray.Count;

                        if (independentCount == dependentCount)
                        {
                            var populationCount = independentCount;
                            y = newVectorArray.Average();

                            for (int l = 0; l < matrixArray2.ColumnCount; l++)
                            {
                                var avg = matrixArray2.Column(l).Average();
                                y -= avg * items[l];
                            }
                        }

                        for (int m = 0; m < 2; m++)
                        {
                            var coefficient = items[m];

                            if (m == 0)
                            {
                                estimate1 += input1.ElementAt(i) * coefficient;
                            }
                            else
                            {
                                estimate1 += input2.ElementAt(i) * coefficient;
                            }
                        }

                        estimate1 += y;
                    }
                }
                else
                {
                    lock (_lock)
                    {
                        estimate1 = 0;
                    }
                }

                lock (_lock)
                {
                    press1 += Math.Pow(actualResult - estimate1, 2);
                }
            });

            for (int i = 0; i < output.Count; i++)
            {
                List<double[]> matrixList = new List<double[]>();
                matrixList.Add(input1.Where((v, k) => k != i).ToArray());
                matrixList.Add(input2.Where((v, k) => k != i).ToArray());
                var matrixArray = CreateMatrix.DenseOfColumnArrays(matrixList);
                var actualResult = output.ElementAt(i);
                var newVectorArray = CreateVector.Dense(output.Where((v, j) => j != i).ToArray());
                var items = FindBestMRSolution(matrixArray, newVectorArray);
                double estimate = 0;

                if (items != null)
                {
                    var y = CalculateYIntercept(matrixArray, newVectorArray, items);
                    for (int m = 0; m < 2; m++)
                    {
                        var coefficient = items[m];

                        if (m == 0)
                        {
                            estimate += input1.ElementAt(i) * coefficient;
                        }
                        else
                        {
                            estimate += input2.ElementAt(i) * coefficient;
                        }
                    }
                }
                else
                {
                    estimate = 0;
                }

                press2 += Math.Pow(actualResult - estimate, 2);
            }

            tss = CalculateTotalSumOfSquares(vectorArray.ToList());
            var test1 = 1 - (press1 / tss);
            var test2 = 1 - (press2 / tss);
}

public Vector<double> CalculateWithQR(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = MultipleRegression.QR(x, y);

            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }

        return result;
    }

    public Vector<double> CalculateWithNormal(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = MultipleRegression.NormalEquations(x, y);

            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }

        return result;
    }

    public Vector<double> CalculateWithSVD(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = MultipleRegression.Svd(x, y);

            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }

        return result;
    }

    public Vector<double> FindBestMRSolution(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = CalculateWithNormal(x, y);

            if (result != null)
            {
                return result;
            }
            else
            {
                result = CalculateWithSVD(x, y);

                if (result != null)
                {
                    return result;
                }
                else
                {
                    result = CalculateWithQR(x, y);

                    if (result != null)
                    {
                        return result;
                    }
                }
            }

        return result;
    }

public double CalculateTotalSumOfSquares(List<double> dependentVariables)
    {
        double tts = 0;

            for (int i = 0; i < dependentVariables.Count; i++)
            {
                tts += Math.Pow(dependentVariables.ElementAt(i) - dependentVariables.Average(), 2);
            }

        return tts;
    }
</code>```

Actual Output (Updated results):

```<code>test1 = 137431.12889999992 (parallel for loop)

test2 = 7.3770258447689254E- (regular for loop)
</code>```

Epilogue: How to setup an MCVE-compliant testing

This may be a fair way to prepare an indeed fully reproducible setup of an MCVE-code + A/B/C/... DataSET-s, put inside a ready-to-run [IDE & Testing Sandbox, hyperlinked here][1], so that Community members can click a re-run button and focus on root-cause analysis, not on decoding and re-engineering the heaps of incomplete SLOCs.

If this runs for the O/P, it will run for other Community Members, whom the O/P has asked for an answer or help.

Try it online!

My new version of the code:

```<code>public double CalculatePredictedRSquared()
    {
        Vector<double> output = CreateVector.Dense(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)).ToArray());
        List<double> input1 = new List<double>(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)));
        List<double> input2 = new List<double>(Enumerable.Range(200, 400).Select(i => Convert.ToDouble(i)));
        double tss = CalculateTotalSumOfSquares(output.ToList());

        IEnumerable<int> range = Enumerable.Range(0, output.Count);
        var query = range.Select(i => DoIt(i, output, input1, input2));
        var result = 1 - (query.Sum() / tss);
        return result;
    }

    public double DoIt(int i, Vector<double> output, List<double> input1, List<double> input2)
    {
        List<double[]> matrixList = new List<double[]>
                {
                    input1.Where((v, k) => k != i).ToArray(),
                    input2.Where((v, k) => k != i).ToArray()
                };
        var matrixArray = CreateMatrix.DenseOfColumnArrays(matrixList);
        var actualResult = output.ElementAt(i);
        var newVectorArray = CreateVector.Dense(output.Where((v, j) => j != i).ToArray());
        var items = FindBestMRSolution(matrixArray, newVectorArray);
        double estimate = 0;

        if (items != null)
        {
            var y = CalculateYIntercept(matrixArray, newVectorArray, items);
            for (int m = 0; m < 2; m++)
            {
                var coefficient = items[m];

                if (m == 0)
                {
                    estimate += input1.ElementAt(i) * coefficient;
                }
                else
                {
                    estimate += input2.ElementAt(i) * coefficient;
                }
            }
        }
        else
        {
            estimate = 0;
        }

       return Math.Pow(actualResult - estimate, 2);
    }
</code>```

答:
This whole thing is a dog's breakfast; you should abandon that attempt at parallelism entirely.

Start over.  Here's what I want you to do.  I want you to write a method DoIt that returns double and takes a int i and whatever other state is required to do a single iteration of the loop.

You will then rewrite your method as follows:

```<code>public double CalculatePredictedRSquared()
{
    Vector<double> output = whatever;
    // Whatever other state you need here
    IEnumerable<int> range = Enumerable.Range(0, output.Count);
    var query = range.Select(i => DoIt(i, whatever_other_state));
    return query.Sum();
}
</code>```

Got it?  DoIt is the thing that is inside your loop right now.  It must take in i, and output and whatever other vectors you need to pass into it. It must only compute a double -- in this case, the square of the estimate error -- and return that double.

It must be pure: It must not read or write any non-local variable, it must not call any non-pure method, and it must give exactly the same results when given the same inputs, every time.  Pure methods are the easiest methods to write, to read, to understand, to test and to parallelize; always try to write pure methods when doing math computations.

Write test cases for DoIt, and test the heck out of it.  It's a pure method; you should be able to write lots of test cases.  Similarly test any of the pure methods called by DoIt.

Once you are satisfied that DoIt is both correct and pure, then the magic happens. Just change it to:

```<code>range.AsParallel().Select...
</code>```

Then compare the parallel and non-parallel versions. They should produce the same result; if not, then something was impure.  Figure out what it was.

Then, verify that the parallel version was faster. If not, then you have failed to do enough work in DoIt to justify parallelism; see https://en.wikipedia.org/wiki/Amdahl%27s_law for details.

Body:
I'm having an issue with the following code. The code works with no errors but I'm receiving different output values when using a parallel for loop vs a regular for loop. I need to get the parallel for loop working properly because I run this code thousands of times. Does anyone know why my parallel for loop is returning different outputs?

private object _lock = new object();

public double CalculatePredictedRSquared()
    {
        double press = 0, tss = 0, press2 = 0, press1 = 0;
        Vector<double> output = CreateVector.Dense(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)).ToArray());
        List<double> input1 = new List<double>(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)));
        List<double> input2 = new List<double>(Enumerable.Range(200, 400).Select(i => Convert.ToDouble(i)));

            Parallel.For(0, output.Count, i =>
            {
                ConcurrentBag<MultipleRegressionInfo> listMRInfoBag = new ConcurrentBag<MultipleRegressionInfo>(listMRInfo);
                ConcurrentBag<double> vectorArrayBag = new ConcurrentBag<double>(output);
                ConcurrentBag<double[]> matrixList = new ConcurrentBag<double[]>();

                lock (_lock)
                {
                    matrixList.Add(input1.Where((v, k) => k != i).ToArray());
                    matrixList.Add(input2.Where((v, k) => k != i).ToArray());
                }

                var matrixArray2 = CreateMatrix.DenseOfColumnArrays(matrixList);
                var actualResult = vectorArrayBag.ElementAt(i);
                var newVectorArray = CreateVector.Dense(vectorArrayBag.Where((v, j) => j != i).ToArray());
                var items = FindBestMRSolution(matrixArray2, newVectorArray);
                double estimate1 = 0;

                if (items != null)
                {
                    lock (_lock)
                    {
                        var y = 0d;
                        var independentCount = matrixArray2.RowCount;
                        var dependentCount = newVectorArray.Count;

                        if (independentCount == dependentCount)
                        {
                            var populationCount = independentCount;
                            y = newVectorArray.Average();

                            for (int l = 0; l < matrixArray2.ColumnCount; l++)
                            {
                                var avg = matrixArray2.Column(l).Average();
                                y -= avg * items[l];
                            }
                        }

                        for (int m = 0; m < 2; m++)
                        {
                            var coefficient = items[m];

                            if (m == 0)
                            {
                                estimate1 += input1.ElementAt(i) * coefficient;
                            }
                            else
                            {
                                estimate1 += input2.ElementAt(i) * coefficient;
                            }
                        }

                        estimate1 += y;
                    }
                }
                else
                {
                    lock (_lock)
                    {
                        estimate1 = 0;
                    }
                }

                lock (_lock)
                {
                    press1 += Math.Pow(actualResult - estimate1, 2);
                }
            });

            for (int i = 0; i < output.Count; i++)
            {
                List<double[]> matrixList = new List<double[]>();
                matrixList.Add(input1.Where((v, k) => k != i).ToArray());
                matrixList.Add(input2.Where((v, k) => k != i).ToArray());
                var matrixArray = CreateMatrix.DenseOfColumnArrays(matrixList);
                var actualResult = output.ElementAt(i);
                var newVectorArray = CreateVector.Dense(output.Where((v, j) => j != i).ToArray());
                var items = FindBestMRSolution(matrixArray, newVectorArray);
                double estimate = 0;

                if (items != null)
                {
                    var y = CalculateYIntercept(matrixArray, newVectorArray, items);
                    for (int m = 0; m < 2; m++)
                    {
                        var coefficient = items[m];

                        if (m == 0)
                        {
                            estimate += input1.ElementAt(i) * coefficient;
                        }
                        else
                        {
                            estimate += input2.ElementAt(i) * coefficient;
                        }
                    }
                }
                else
                {
                    estimate = 0;
                }

                press2 += Math.Pow(actualResult - estimate, 2);
            }

            tss = CalculateTotalSumOfSquares(vectorArray.ToList());
            var test1 = 1 - (press1 / tss);
            var test2 = 1 - (press2 / tss);
}

public Vector<double> CalculateWithQR(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = MultipleRegression.QR(x, y);

            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }

        return result;
    }

    public Vector<double> CalculateWithNormal(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = MultipleRegression.NormalEquations(x, y);

            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }

        return result;
    }

    public Vector<double> CalculateWithSVD(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = MultipleRegression.Svd(x, y);

            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }

        return result;
    }

    public Vector<double> FindBestMRSolution(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

            result = CalculateWithNormal(x, y);

            if (result != null)
            {
                return result;
            }
            else
            {
                result = CalculateWithSVD(x, y);

                if (result != null)
                {
                    return result;
                }
                else
                {
                    result = CalculateWithQR(x, y);

                    if (result != null)
                    {
                        return result;
                    }
                }
            }

        return result;
    }

public double CalculateTotalSumOfSquares(List<double> dependentVariables)
    {
        double tts = 0;

            for (int i = 0; i < dependentVariables.Count; i++)
            {
                tts += Math.Pow(dependentVariables.ElementAt(i) - dependentVariables.Average(), 2);
            }

        return tts;
    }

Actual Output (Updated results):

test1 = 137431.12889999992 (parallel for loop)

test2 = 7.3770258447689254E- (regular for loop)

Epilogue: How to setup an MCVE-compliant testing

This may be a fair way to prepare an indeed fully reproducible setup of an MCVE-code + A/B/C/... DataSET-s, put inside a ready-to-run [IDE & Testing Sandbox, hyperlinked here][1], so that Community members can click a re-run button and focus on root-cause analysis, not on decoding and re-engineering the heaps of incomplete SLOCs.

If this runs for the O/P, it will run for other Community Members, whom the O/P has asked for an answer or help.

<a href="https://tio.run/##7VnfT@M4EH7vX@F7S46SK9W9QZFYYE9IC8u1iNVphU4mdYvBSbq2U6h2@du5sZ3fcdIUyu3L5qFN7PE342/G44nji11f@C8vsaDhHE1WQpJgv1d88o4jxogvaRQK7y8SEk79isQnGn6rNF2RJ1ltuuMET6HBu8LiQaS951jeXRDpXcSBghbeCRWS09tYa2yUAp0E8yM2J7ccr5EakzknQgBey9SOo9CPOSdho91NWiryJxTPw0hI3dMLcUDEAvsEXXIypb4k07H4FmN46H3vIYQW8S2jPvIZFgIdY@bHDGuLVKeWUEKcLrEkKLq9B3vRvyzyH9AIheQxaXJc0JVATSP4JxkWyRVPjGLH1bApOEpHLBRLADvoI5neEJhIoFTrJy0xLNzvqfv9DOgaTIn4gcE7RFEsF7EEkWOgUBLT652QUBDnNFT0YZDzxjicEwcg/xwMXG9ClE8cikaHCJyyJFx6V9GJhnSo68LDEed45bhurvgTBE2mloagdS/hp9jzeqWtqoadVA0HGyjLtKnrEnMMgcq8jxFXFhtaIWLjUPaRgimJfy89qSuP7Q94fnAeM0kXjOTL4iycRYeIgfnnY3UPUsmUOo108pEFnuzKU@KWOha0I5u0pUya6XZE/npziCBeOX1S3mjBBUGnSrS69NJy9Apza511atWV6/OOplPHhJ/35Y5w4jjLPnpwlacf0G8jRO3huw5t@Cq05/rslpgnCvSwYbY2z3WjWZufZ5AY4yDUIsLJDbLoUIDYlzFmYyIgPACw7FrvlJEAmD@CgG8YDy66zsfY00UFNKfjXtNx34EOpYpChlaZ7SMNpx@IitoJTFXlW6dIS79ikwUtyZlpfkwSYU2MzpBjlIKBYcxY16BqjcPmYelEFY2D6X6rDHBAFgR@QqlzCQwpkuCNo0fd3o5SwyhT5yUQjRiaoZolowqu2zi@mYfUwkW0SDbV1MSqvv1WjFV9UkeQtDEkdbdlYuqaRVzNTiKm4wP@Dsocm5VmjEBsZ8dthWufa7Yel/OqK40ah7lFy9dhrdDuSIP9btbNV3bTPui519zTW0tRYCgKgKIh/LVxsd7nfkRmM@pTYvytzQ9u1nhLhWKgYm/wVjfkaWFnlNQjpUQIlBYsfC2rWhMTZOvGDt/B2JYQKBmwsit47q1vsXKx9fRaSfldjd1GpZHU3MCSegnxLqNHp7T57ubG9dHQWhKUnqrpK1uL1KxFCmuxWG1CS3VZ1g0tlMHWaqzcb0tE2yuotldMVSqn9yicEqa3UzAlYO9bKFnqpO1VQGkZk73I/nMWSsJ9spBtNvSN7Q3726a7TXuVtfEu02WHaU/Y2Qv52zaW5jzduqFsaNtwi7ZteQMoHmx0enVKzj465N566q3AmdOVLK6vIonZJA4@z8wBjSi@7cBKVRmktlBV@EnQqHahPVDuJHvDHwq9QXZYkh1mss/Z2VHlACcz8QuVd3@PHZPssu6nfnXAqnq0VOnnaa5TiaCyTrK@@jGDB7pB2arLfmVgNtivFDtLzGIVCsngcgq2pjRzXOOdiQt84ejhLvrxA2XNZ@GMhlSukr6uSY8TGfMwoWfN7p0blowy1ptxSW8Hv15EPMDs5/nW6D@F0Nfnnr8c/V6Onlyf/DwvT5bTX57domcthdn/41tb7rA5VjGcDLIWfnXyLBTYaatt8zYsm7lqBdhs7WLvWofb7H5zmWKfR8N2uMlU2qu5NVPqUo@tje2Gz1X1aqj0xSU7uLvGnKrPK6Lha5aUwnIma0s3dcgNUo9SU6wJLWClGnjXpi47lGstHRMaQWPKof55fnn5Dw" rel="nofollow noreferrer" title="C# (Visual C# Compiler) – Try It Online">Try it online!

My new version of the code:

public double CalculatePredictedRSquared()
    {
        Vector<double> output = CreateVector.Dense(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)).ToArray());
        List<double> input1 = new List<double>(Enumerable.Range(0, 400).Select(i => Convert.ToDouble(i)));
        List<double> input2 = new List<double>(Enumerable.Range(200, 400).Select(i => Convert.ToDouble(i)));
        double tss = CalculateTotalSumOfSquares(output.ToList());

        IEnumerable<int> range = Enumerable.Range(0, output.Count);
        var query = range.Select(i => DoIt(i, output, input1, input2));
        var result = 1 - (query.Sum() / tss);
        return result;
    }

    public double DoIt(int i, Vector<double> output, List<double> input1, List<double> input2)
    {
        List<double[]> matrixList = new List<double[]>
                {
                    input1.Where((v, k) => k != i).ToArray(),
                    input2.Where((v, k) => k != i).ToArray()
                };
        var matrixArray = CreateMatrix.DenseOfColumnArrays(matrixList);
        var actualResult = output.ElementAt(i);
        var newVectorArray = CreateVector.Dense(output.Where((v, j) => j != i).ToArray());
        var items = FindBestMRSolution(matrixArray, newVectorArray);
        double estimate = 0;

        if (items != null)
        {
            var y = CalculateYIntercept(matrixArray, newVectorArray, items);
            for (int m = 0; m < 2; m++)
            {
                var coefficient = items[m];

                if (m == 0)
                {
                    estimate += input1.ElementAt(i) * coefficient;
                }
                else
                {
                    estimate += input2.ElementAt(i) * coefficient;
                }
            }
        }
        else
        {
            estimate = 0;
        }

       return Math.Pow(actualResult - estimate, 2);
    }

Body:
This whole thing is a dog's breakfast; you should abandon that attempt at parallelism entirely.

Start over.  Here's what I want you to do.  I want you to write a method DoIt that returns double and takes a int i and whatever other state is required to do a single iteration of the loop.

You will then rewrite your method as follows:

public double CalculatePredictedRSquared()
{
    Vector<double> output = whatever;
    // Whatever other state you need here
    IEnumerable<int> range = Enumerable.Range(0, output.Count);
    var query = range.Select(i => DoIt(i, whatever_other_state));
    return query.Sum();
}

Got it?  DoIt is the thing that is inside your loop right now.  It must take in i, and output and whatever other vectors you need to pass into it. It must only compute a double -- in this case, the square of the estimate error -- and return that double.

It must be pure: It must not read or write any non-local variable, it must not call any non-pure method, and it must give exactly the same results when given the same inputs, every time.  Pure methods are the easiest methods to write, to read, to understand, to test and to parallelize; always try to write pure methods when doing math computations.

Write test cases for DoIt, and test the heck out of it.  It's a pure method; you should be able to write lots of test cases.  Similarly test any of the pure methods called by DoIt.

Once you are satisfied that DoIt is both correct and pure, then the magic happens. Just change it to:

range.AsParallel().Select...

Then compare the parallel and non-parallel versions. They should produce the same result; if not, then something was impure.  Figure out what it was.

Then, verify that the parallel version was faster. If not, then you have failed to do enough work in DoIt to justify parallelism; see https://en.wikipedia.org/wiki/Amdahl%27s_law for details.


来源: stackexchange

问:
Swift - Get last 7 days starting from today in array
How to get previous 7 days of the month I know how to get if today is 18, but what if today id 3rd November? How to get the last 4 days from the previous month(October) in Int?</p>

答:
I update

Alejandro Luengo's code. It works Swift4

```<code>func getPastDates(days: Int) -> NSMutableArray {

        let dates = NSMutableArray()
        let calendar = Calendar.current

        var today = calendar.startOfDay(for: Date())

        for _ in 1 ... days {

            let day = calendar.component(.day, from: today)
            let month = calendar.component(.month, from: today)
            let year = calendar.component(.year, from: today)

            let date = NSMutableDictionary()

            date.setValue(day, forKey: "day")
            date.setValue(month, forKey: "month")
            date.setValue(year, forKey: "year")

            dates.add(date)
            today = calendar.date(byAdding: .day, value: -1, to: today)!
        }

        return dates

    }
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I update

Alejandro Luengo's code. It works Swift4

func getPastDates(days: Int) -> NSMutableArray {

        let dates = NSMutableArray()
        let calendar = Calendar.current

        var today = calendar.startOfDay(for: Date())

        for _ in 1 ... days {

            let day = calendar.component(.day, from: today)
            let month = calendar.component(.month, from: today)
            let year = calendar.component(.year, from: today)

            let date = NSMutableDictionary()

            date.setValue(day, forKey: "day")
            date.setValue(month, forKey: "month")
            date.setValue(year, forKey: "year")

            dates.add(date)
            today = calendar.date(byAdding: .day, value: -1, to: today)!
        }

        return dates

    }


问:
Highlight and Scroll inside bootstrap-treeview
I am building a bootstrap tree inside a bootstrap modal. On Search all the nodes are searched and highlights whenever a match happens. My modal has fixed height and If the searched element is present at the bottom part of the tree I have to scroll to view the element. Is it possible to scroll the automatically to the first matched element when there is a match. Here is the plug in I am using.
Bootstrap-TreeView

Some Code for reference

```<code><div class="row">
    <hr>
    <h2>Searchable Tree</h2>
    <div class="col-sm-4">
      <h2>Input</h2>
      <!-- <form> -->
        <div class="form-group">
          <label for="input-search" class="sr-only">Search Tree:</label>
          <input type="input" class="form-control" id="input-search" placeholder="Type to search..." value="">
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox" class="checkbox" id="chk-ignore-case" value="true" checked>
            Ignore Case
          </label>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox" class="checkbox" id="chk-exact-match" value="false">
            Exact Match
          </label>
        </div>
        <div class="checkbox">
          <label>
            <input type="checkbox" class="checkbox" id="chk-reveal-results" value="true" checked>
            Reveal Results
          </label>
        </div>
        <button type="button" class="btn btn-success" id="btn-search">Search</button>
        <button type="button" class="btn btn-default" id="btn-clear-search">Clear</button>
      <!-- </form> -->
    </div>
    <div class="col-sm-4">
      <h2>Tree</h2>
      <div id="treeview-searchable" class="treeview"></div>

    </div>
    <div class="col-sm-4">
      <h2>Results</h2>
      <div id="search-output"></div>
    </div>
  </div>

      <div id="tree"></div>
</code>```

Javascript:

```<code>    var tree1 =[ {
    text:"GrandParent",
    nodes:[
    {
  text: "Parent 1",

},
{
  text: "Parent 2"
},
{
  text: "Parent 3"
},
{
  text: "Parent 4"
},
{
  text: "Parent 5",
   nodes: [
    {
      text: "Child 5",
      nodes: [
        {
          text: "Grandchild 4"
         },
        {
          text: "Grandchild 5"
        }
      ]
    },
      {
        text: "Child 2"
      }
    ]
  },
  {
    text: "Parent 6",
      nodes: [
     {
        text: "Child 6",
        nodes: [
          {
            text: "Grandchild 8"
          },
          {
            text: "Grandchild 9"
          }
        ]
      },
      {
        text: "Child 10"
      }
    ]
  }
]
}];

 function getTree() {
  // Some logic to retrieve, or generate tree structure
  return tree1;
  }

  var $searchableTree = $('#treeview-searchable').treeview({
     data: getTree(),
  });

var search = function(e) {
var pattern = $('#input-search').val();
var options = {
  ignoreCase: $('#chk-ignore-case').is(':checked'),
  exactMatch: $('#chk-exact-match').is(':checked'),
  revealResults: $('#chk-reveal-results').is(':checked')
    };
   var results = $searchableTree.treeview('search', [ pattern, options ]);

   var output = '<p>' + results.length + ' matches found</p>';
   $.each(results, function (index, result) {
  output += '<p>- ' + result.text + '</p>';
  });
  $('#search-output').html(output);
 }

 $('#btn-search').on('click', search);
 $('#input-search').on('keyup', search);

 $('#btn-clear-search').on('click', function (e) {
 $searchableTree.treeview('clearSearch');
 $('#input-search').val('');
 $('#search-output').html('');
});
</code>```

Here is the fiddle I have made for reference

  https://jsfiddle.net/whw3j59o/3/

Assuming the tree is present inside a fixed height bootstrap modal is it possible to the auto scroll to first match?

答:
I recently started working with bootstrap-treeview and had the same question.

Solution:

We can use scrollIntoView() function.
Bootstrap-treeview already has a search option which highlights the value, you can extend it.

I used the filter / search list  and instead of filtering it scrolls down to the value that is being searched.

Code Reference:

HTML:

```<code><div class="row" style="margin:5px;overflow-y:auto;">
  <div class="col-sm-4">
  <div class="form-group">
    <input type="input" class="form-control" id="input-search"
      placeholder="Type to search..." value="">
  </div>
  <button type="button" class="btn btn-success" id="btn-
   search">Search</button>
  <button type="button" class="btn btn-default" id="btn-clear-
   search">Clear</button>

  </div>
  <div class="col-sm-4" style="margin:5px;">
  <h2>Tree</h2>
  <div id="tree" style=" overflow-y:auto; height: 250px; border: 1px solid
   #ccc ;"></div>
  </div>
</div>
</code>```

Javascript:

```<code>var myTree = [{
  text: "Parent1",
  nodes: [{
    text: "Child11",
    nodes: [{
      text: "GrandChild111"
    }, {
      text: "GrandChild112"
    }]
  }, {
    text: "Child12"
  }]
}, {
  text: "Parent2",
  nodes: [{
    text: "Child21"
  }, {
    text: "Child22"
  }]
}, {
  text: "Parent3",
  nodes: [{
    text: "Child31"
  }, {
    text: "Child32"
  }, {
    text: "Child33"
  }]
}, {
  text: "Parent4"
}, {
  text: "Parent5",
  nodes: [{
    text: "Child51"
  }, {
    text: "Child52"
  }, {
    text: "Child33"
  }]
}, {
  text: "Parent6"
}, {
  text: "Parent7",
  nodes: [{
    text: "Child71",
    nodes: [{
      text: "GrandChild711"
    }, {
      text: "GrandChild712"
    }]
  }, {
    text: "Child72",
    nodes: [{
      text: "GrandChild711"
    }, {
      text: "GrandChild712"
    }]
  }]
}, {
  text: "Parent8"
}, {
  text: "Parent9",
  nodes: [{
    text: "Child91"
  }, {
    text: "Child92"
  }]
}, {
  text: "Parent10"
}];

function getTree() {
  // Some logic to retrieve, or generate tree structure
  return myTree;
}
$('#tree').bind('mousewheel', function(e) {
  $(this).scrollTop($(this).scrollTop() - e.originalEvent.wheelDeltaY);
  return false;
});
var $searchableTree = $('#tree').treeview({
  data: getTree(),
  levels: 1
});
var search = function(e) {
  var input, filter, div, ul, li, a, i;
  var pattern = $('#input-search').val();
  var options = {
    ignoreCase: true,
    exactMatch: false,
    revealResults: true,
  };
  $searchableTree.treeview('search', [pattern, options]);

  //Scrolling to the element that is searched
  input = document.getElementById("input-search");
  filter = input.value.toUpperCase();
  div = document.getElementById("tree");
  li = div.getElementsByTagName("li");
  for (i = 0; i < li.length; i++) {
    a = li[i];
    if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
      li[i].scrollIntoView(false)[0];

    }
  }
}
$('#btn-search').on('click', search);

$('#btn-clear-search').on('click', function(e) {
  $searchableTree.treeview('clearSearch');
  $('#input-search').val('');
  $('#search-output').html('');
});
</code>```

This part of the code filters and scrolls to the matched value:

```<code>input = document.getElementById("input-search");
filter = input.value.toUpperCase();
div = document.getElementById("tree");
li = div.getElementsByTagName("li");
for (i = 0; i < li.length; i++) {
a = li[i];
        if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
          li[i].scrollIntoView(false)[0];

        }
      }
</code>```

Ive added the zero in the end of this line so that it scrolls to the first match in the list

li[i].scrollIntoView(false)[0];

Heres a link to the demo - DEMO

Body:
I am building a bootstrap tree inside a bootstrap modal. On Search all the nodes are searched and highlights whenever a match happens. My modal has fixed height and If the searched element is present at the bottom part of the tree I have to scroll to view the element. Is it possible to scroll the automatically to the first matched element when there is a match. Here is the plug in I am using.
Bootstrap-TreeView

Some Code for reference

Searchable Tree

Input

          Search Tree:
          <input type="input" class="form-control" id="input-search" placeholder="Type to search..." value="">

            <input type="checkbox" class="checkbox" id="chk-ignore-case" value="true" checked>
            Ignore Case

            <input type="checkbox" class="checkbox" id="chk-exact-match" value="false">
            Exact Match

            <input type="checkbox" class="checkbox" id="chk-reveal-results" value="true" checked>
            Reveal Results

        <button type="button" class="btn btn-success" id="btn-search">Search</button>
        <button type="button" class="btn btn-default" id="btn-clear-search">Clear</button>

Tree

Results

Javascript:

    var tree1 =[ {
    text:"GrandParent",
    nodes:[
    {
  text: "Parent 1",

},
{
  text: "Parent 2"
},
{
  text: "Parent 3"
},
{
  text: "Parent 4"
},
{
  text: "Parent 5",
   nodes: [
    {
      text: "Child 5",
      nodes: [
        {
          text: "Grandchild 4"
         },
        {
          text: "Grandchild 5"
        }
      ]
    },
      {
        text: "Child 2"
      }
    ]
  },
  {
    text: "Parent 6",
      nodes: [
     {
        text: "Child 6",
        nodes: [
          {
            text: "Grandchild 8"
          },
          {
            text: "Grandchild 9"
          }
        ]
      },
      {
        text: "Child 10"
      }
    ]
  }
]
}];

 function getTree() {
  // Some logic to retrieve, or generate tree structure
  return tree1;
  }

  var $searchableTree = $('#treeview-searchable').treeview({
     data: getTree(),
  });

var search = function(e) {
var pattern = $('#input-search').val();
var options = {
  ignoreCase: $('#chk-ignore-case').is(':checked'),
  exactMatch: $('#chk-exact-match').is(':checked'),
  revealResults: $('#chk-reveal-results').is(':checked')
    };
   var results = $searchableTree.treeview('search', [ pattern, options ]);

   var output = '
' + results.length + ' matches found
';
   $.each(results, function (index, result) {
  output += '
- ' + result.text + '
';
  });
  $('#search-output').html(output);
 }

 $('#btn-search').on('click', search);
 $('#input-search').on('keyup', search);

 $('#btn-clear-search').on('click', function (e) {
 $searchableTree.treeview('clearSearch');
 $('#input-search').val('');
 $('#search-output').html('');
});

Here is the fiddle I have made for reference

  https://jsfiddle.net/whw3j59o/3/

Assuming the tree is present inside a fixed height bootstrap modal is it possible to the auto scroll to first match?

Body:
I recently started working with bootstrap-treeview and had the same question.

Solution:

We can use scrollIntoView() function.
Bootstrap-treeview already has a search option which highlights the value, you can extend it.

I used the filter / search list  and instead of filtering it scrolls down to the value that is being searched.

Code Reference:

HTML:

<div class="row" style="margin:5px;overflow-y:auto;">
  <div class="col-sm-4">
  <div class="form-group">
    <input type="input" class="form-control" id="input-search"
      placeholder="Type to search..." value="">
  </div>
  <button type="button" class="btn btn-success" id="btn-
   search">Search</button>
  <button type="button" class="btn btn-default" id="btn-clear-
   search">Clear</button>

  </div>
  <div class="col-sm-4" style="margin:5px;">
  <h2>Tree</h2>
  <div id="tree" style=" overflow-y:auto; height: 250px; border: 1px solid
   #ccc ;"></div>
  </div>
</div>

Javascript:

var myTree = [{
  text: "Parent1",
  nodes: [{
    text: "Child11",
    nodes: [{
      text: "GrandChild111"
    }, {
      text: "GrandChild112"
    }]
  }, {
    text: "Child12"
  }]
}, {
  text: "Parent2",
  nodes: [{
    text: "Child21"
  }, {
    text: "Child22"
  }]
}, {
  text: "Parent3",
  nodes: [{
    text: "Child31"
  }, {
    text: "Child32"
  }, {
    text: "Child33"
  }]
}, {
  text: "Parent4"
}, {
  text: "Parent5",
  nodes: [{
    text: "Child51"
  }, {
    text: "Child52"
  }, {
    text: "Child33"
  }]
}, {
  text: "Parent6"
}, {
  text: "Parent7",
  nodes: [{
    text: "Child71",
    nodes: [{
      text: "GrandChild711"
    }, {
      text: "GrandChild712"
    }]
  }, {
    text: "Child72",
    nodes: [{
      text: "GrandChild711"
    }, {
      text: "GrandChild712"
    }]
  }]
}, {
  text: "Parent8"
}, {
  text: "Parent9",
  nodes: [{
    text: "Child91"
  }, {
    text: "Child92"
  }]
}, {
  text: "Parent10"
}];

function getTree() {
  // Some logic to retrieve, or generate tree structure
  return myTree;
}
$('#tree').bind('mousewheel', function(e) {
  $(this).scrollTop($(this).scrollTop() - e.originalEvent.wheelDeltaY);
  return false;
});
var $searchableTree = $('#tree').treeview({
  data: getTree(),
  levels: 1
});
var search = function(e) {
  var input, filter, div, ul, li, a, i;
  var pattern = $('#input-search').val();
  var options = {
    ignoreCase: true,
    exactMatch: false,
    revealResults: true,
  };
  $searchableTree.treeview('search', [pattern, options]);

  //Scrolling to the element that is searched
  input = document.getElementById("input-search");
  filter = input.value.toUpperCase();
  div = document.getElementById("tree");
  li = div.getElementsByTagName("li");
  for (i = 0; i < li.length; i++) {
    a = li[i];
    if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
      li[i].scrollIntoView(false)[0];

    }
  }
}
$('#btn-search').on('click', search);

$('#btn-clear-search').on('click', function(e) {
  $searchableTree.treeview('clearSearch');
  $('#input-search').val('');
  $('#search-output').html('');
});

This part of the code filters and scrolls to the matched value:

input = document.getElementById("input-search");
filter = input.value.toUpperCase();
div = document.getElementById("tree");
li = div.getElementsByTagName("li");
for (i = 0; i < li.length; i++) {
a = li[i];
        if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
          li[i].scrollIntoView(false)[0];

        }
      }

Ive added the zero in the end of this line so that it scrolls to the first match in the list

li[i].scrollIntoView(false)[0];

Heres a link to the demo - DEMO


问:
ffmpeg: convert audio-only flv to swf
My Flex application records audio-only FLV files using red5--I'd like to convert these to SWF files so I can embed them in other SWF files.  (I could also convert to MP3 and then embed them into SWFs myself, but I'd prefer a one step solution.)

Anyone have experience doing this?

What I've tried:

The following naive ffmpeg command fails:

```<code>> ffmpeg -i 3139747641.flv -vn movie.swf
FFmpeg version SVN-r21751-snapshot, Copyright (c) 2000-2010 Fabrice Bellard, et al.
  built on Feb 11 2010 09:15:42 with gcc 4.2.1 (SUSE Linux)
  configuration: --enable-gpl --enable-nonfree
  libavutil     50. 9. 0 / 50. 9. 0
  libavcodec    52.53. 0 / 52.53. 0
  libavformat   52.52. 0 / 52.52. 0
  libavdevice   52. 2. 0 / 52. 2. 0
  libswscale     0.10. 0 /  0.10. 0
[flv @ 0x8a5e3a0]Could not find codec parameters (Video: 0x0000)
[flv @ 0x8a5e3a0]Estimating duration from bitrate, this may be inaccurate
Input #0, flv, from '3139747641.flv':
  Metadata:
    audiocodecid    : -1
    duration        : 0
    videocodecid    : -1
    canSeekToEnd    : true
  Duration: 00:00:14.88, start: 0.000000, bitrate: N/A
    Stream #0.0: Video: 0x0000, 1k tbr, 1k tbn, 1k tbc
    Stream #0.1: Audio: nellymoser, 8000 Hz, mono, s16
Output #0, swf, to 'movie.swf':
    Stream #0.0: Audio: 0x0000, 8000 Hz, mono, s16, 64 kb/s
Stream mapping:
  Stream #0.1 -> #0.0
Encoder (codec id 86017) not found for output stream #0.0
</code>```

I've tried adding a dummy video, but this also failed:

```<code>> ffmpeg -i dummy.mov -i 3139747641.flv movie.swf
<!-- snip -->
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'dummy.mov':
  Metadata:
    major_brand     : qt
    minor_version   : 512
    compatible_brands: qt
    encoder         : Lavf52.52.0
    encoder-eng     : Lavf52.52.0
  Duration: 00:00:00.10, start: 0.000000, bitrate: 62 kb/s
    Stream #0.0(eng): Video: mpeg4, yuv420p, 10x10 [PAR 1:1 DAR 1:1], 1 kb/s, 10 fps, 10 tbr, 10 tbn, 10 tbc
[flv @ 0x8a67fa0]Could not find codec parameters (Video: 0x0000)
[flv @ 0x8a67fa0]Estimating duration from bitrate, this may be inaccurate
Input #1, flv, from '3139747641.flv':
  Metadata:
    audiocodecid    : -1
    duration        : 0
    videocodecid    : -1
    canSeekToEnd    : true
  Duration: 00:00:14.88, start: 0.000000, bitrate: N/A
    Stream #1.0: Video: 0x0000, 1k tbr, 1k tbn, 1k tbc
    Stream #1.1: Audio: nellymoser, 8000 Hz, mono, s16
picture size invalid (0x0)
Cannot allocate temp picture, check pix fmt
</code>```

答:
Install the unstripped and extra libraries</p>

```<code>sudo apt-get install ffmpeg libavcodec-extra-53
</code>```

ContentLicense: CC BY-SA 2.5

Title: ffmpeg: convert audio-only flv to swf

Body:
My Flex application records audio-only FLV files using red5--I'd like to convert these to SWF files so I can embed them in other SWF files.  (I could also convert to MP3 and then embed them into SWFs myself, but I'd prefer a one step solution.)

Anyone have experience doing this?

What I've tried:

The following naive ffmpeg command fails:

> ffmpeg -i 3139747641.flv -vn movie.swf
FFmpeg version SVN-r21751-snapshot, Copyright (c) 2000-2010 Fabrice Bellard, et al.
  built on Feb 11 2010 09:15:42 with gcc 4.2.1 (SUSE Linux)
  configuration: --enable-gpl --enable-nonfree
  libavutil     50. 9. 0 / 50. 9. 0
  libavcodec    52.53. 0 / 52.53. 0
  libavformat   52.52. 0 / 52.52. 0
  libavdevice   52. 2. 0 / 52. 2. 0
  libswscale     0.10. 0 /  0.10. 0
[flv @ 0x8a5e3a0]Could not find codec parameters (Video: 0x0000)
[flv @ 0x8a5e3a0]Estimating duration from bitrate, this may be inaccurate
Input #0, flv, from '3139747641.flv':
  Metadata:
    audiocodecid    : -1
    duration        : 0
    videocodecid    : -1
    canSeekToEnd    : true
  Duration: 00:00:14.88, start: 0.000000, bitrate: N/A
    Stream #0.0: Video: 0x0000, 1k tbr, 1k tbn, 1k tbc
    Stream #0.1: Audio: nellymoser, 8000 Hz, mono, s16
Output #0, swf, to 'movie.swf':
    Stream #0.0: Audio: 0x0000, 8000 Hz, mono, s16, 64 kb/s
Stream mapping:
  Stream #0.1 -> #0.0
Encoder (codec id 86017) not found for output stream #0.0

I've tried adding a dummy video, but this also failed:

> ffmpeg -i dummy.mov -i 3139747641.flv movie.swf

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'dummy.mov':
  Metadata:
    major_brand     : qt
    minor_version   : 512
    compatible_brands: qt
    encoder         : Lavf52.52.0
    encoder-eng     : Lavf52.52.0
  Duration: 00:00:00.10, start: 0.000000, bitrate: 62 kb/s
    Stream #0.0(eng): Video: mpeg4, yuv420p, 10x10 [PAR 1:1 DAR 1:1], 1 kb/s, 10 fps, 10 tbr, 10 tbn, 10 tbc
[flv @ 0x8a67fa0]Could not find codec parameters (Video: 0x0000)
[flv @ 0x8a67fa0]Estimating duration from bitrate, this may be inaccurate
Input #1, flv, from '3139747641.flv':
  Metadata:
    audiocodecid    : -1
    duration        : 0
    videocodecid    : -1
    canSeekToEnd    : true
  Duration: 00:00:14.88, start: 0.000000, bitrate: N/A
    Stream #1.0: Video: 0x0000, 1k tbr, 1k tbn, 1k tbc
    Stream #1.1: Audio: nellymoser, 8000 Hz, mono, s16
picture size invalid (0x0)
Cannot allocate temp picture, check pix fmt

Body:
Install the unstripped and extra libraries

sudo apt-get install ffmpeg libavcodec-extra-53


问:
How do I receive the default value of a form control in my custom control (ControlValueAccessor)
Let's say I have a form like this:

```<code>this.form = fb.group({
        genders: [['MALE']],
    });
</code>```

And I've implemented my own custom form control which implements the ControlValueAccessor interface. I understand that we have the writeValue method, but this method is only called when using either setValue or patchValue. How do I get the default value which was set in the FormBuilder's group method?

答:
you want to receive the default from group value , try in your component :

```<code>this.form.value.genders
</code>```

Or in your .html file you can use : {{ form.value. genders }}

Body:
Let's say I have a form like this:

this.form = fb.group({
        genders: [['MALE']],
    });

And I've implemented my own custom form control which implements the ControlValueAccessor interface. I understand that we have the writeValue method, but this method is only called when using either setValue or patchValue. How do I get the default value which was set in the FormBuilder's group method?

Body:
you want to receive the default from group value , try in your component :

this.form.value.genders

Or in your .html file you can use : {{ form.value. genders }}


问:
Plot the most (n) occurring values of a column with all of data of other column
I have following data in a Pandas DataFrame from a sql query:

```<code>      latin_brands   group phone_brand_chinese_match only_latin_brands
0           xiaomi  M32-38                        小米            xiaomi
1           xiaomi  M32-38                        小米            xiaomi
2           xiaomi  M32-38                        小米            xiaomi
3           xiaomi  M29-31                        小米            xiaomi
4           xiaomi  M29-31                        小米            xiaomi
5             None  F24-26                      OPPO              OPPO
6          coolpad  M32-38                        酷派           coolpad
7           xiaomi  M32-38                        小米            xiaomi
8             None  M32-38                      vivo              vivo
9          samsung  F33-42                        三星           samsung
10          huawei  M29-31                        华为            huawei
11          huawei  F33-42                        华为            huawei
12         samsung  F27-28                        三星           samsung
13          huawei  M32-38                        华为            huawei
14         aiyouni    M39+                       艾优尼           aiyouni
15          huawei  F27-28                        华为            huawei
16          xiaomi  M32-38                        小米            xiaomi
17          xiaomi  M32-38                        小米            xiaomi
18           meizu    M39+                        魅族             meizu
19          xiaomi  M32-38                        小米            xiaomi
20         samsung  F33-42                        三星           samsung
21          xiaomi  M23-26                        小米            xiaomi
22          huawei  M23-26                        华为            huawei
23         samsung  M27-28                        三星           samsung
24          xiaomi  M29-31                        小米            xiaomi
25         samsung  M32-38                        三星           samsung
26         samsung  M32-38                        三星           samsung
27         samsung  F33-42                        三星           samsung
28         samsung  M32-38                        三星           samsung
29         samsung  M32-38                        三星           samsung
...            ...     ...                       ...               ...
74809       huawei  M27-28                        华为            huawei
74810         None  M29-31                       TCL               TCL
</code>```

I want to map two columns and plot this on a line chart.
My approach:

```<code>phones = phones.groupby(['only_latin_brands', 'group']).size()
phones = phones.unstack()
phones = phones.fillna(0)
phones.head()
phones.plot(kind='line')
plt.show()
</code>```

I want to plot the relation between the group and the only_latin_brands.

How can I plot only the most occurring 20 only_latin_brands column with their groups?

答:
You can use groupby size and then the nlargest method:

```<code>In [11]: df.groupby("only_latin_brands").size()
Out[11]:
only_latin_brands
OPPO        1
aiyouni     1
coolpad     1
huawei      5
meizu       1
samsung     9
vivo        1
xiaomi     11
dtype: int64

In [12]: df.groupby("only_latin_brands").size().nlargest(2)
Out[12]:
only_latin_brands
xiaomi     11
samsung     9
dtype: int64
</code>```

Then use isin to filter out just those rows:

```<code>In [13]: df[df["only_latin_brands"].isin(df.groupby("only_latin_brands").size().nlargest(2).index)]
Out[13]:
   latin_brands   group phone_brand_chinese_match only_latin_brands
0        xiaomi  M32-38                        小米            xiaomi
1        xiaomi  M32-38                        小米            xiaomi
2        xiaomi  M32-38                        小米            xiaomi
3        xiaomi  M29-31                        小米            xiaomi
4        xiaomi  M29-31                        小米            xiaomi
7        xiaomi  M32-38                        小米            xiaomi
9       samsung  F33-42                        三星           samsung
12      samsung  F27-28                        三星           samsung
16       xiaomi  M32-38                        小米            xiaomi
17       xiaomi  M32-38                        小米            xiaomi
19       xiaomi  M32-38                        小米            xiaomi
20      samsung  F33-42                        三星           samsung
21       xiaomi  M23-26                        小米            xiaomi
23      samsung  M27-28                        三星           samsung
24       xiaomi  M29-31                        小米            xiaomi
25      samsung  M32-38                        三星           samsung
26      samsung  M32-38                        三星           samsung
27      samsung  F33-42                        三星           samsung
28      samsung  M32-38                        三星           samsung
29      samsung  M32-38                        三星           samsung
</code>```

Now you can do the plot...

Body:
I have following data in a Pandas DataFrame from a sql query:

      latin_brands   group phone_brand_chinese_match only_latin_brands
0           xiaomi  M32-38                        小米            xiaomi
1           xiaomi  M32-38                        小米            xiaomi
2           xiaomi  M32-38                        小米            xiaomi
3           xiaomi  M29-31                        小米            xiaomi
4           xiaomi  M29-31                        小米            xiaomi
5             None  F24-26                      OPPO              OPPO
6          coolpad  M32-38                        酷派           coolpad
7           xiaomi  M32-38                        小米            xiaomi
8             None  M32-38                      vivo              vivo
9          samsung  F33-42                        三星           samsung
10          huawei  M29-31                        华为            huawei
11          huawei  F33-42                        华为            huawei
12         samsung  F27-28                        三星           samsung
13          huawei  M32-38                        华为            huawei
14         aiyouni    M39+                       艾优尼           aiyouni
15          huawei  F27-28                        华为            huawei
16          xiaomi  M32-38                        小米            xiaomi
17          xiaomi  M32-38                        小米            xiaomi
18           meizu    M39+                        魅族             meizu
19          xiaomi  M32-38                        小米            xiaomi
20         samsung  F33-42                        三星           samsung
21          xiaomi  M23-26                        小米            xiaomi
22          huawei  M23-26                        华为            huawei
23         samsung  M27-28                        三星           samsung
24          xiaomi  M29-31                        小米            xiaomi
25         samsung  M32-38                        三星           samsung
26         samsung  M32-38                        三星           samsung
27         samsung  F33-42                        三星           samsung
28         samsung  M32-38                        三星           samsung
29         samsung  M32-38                        三星           samsung
...            ...     ...                       ...               ...
74809       huawei  M27-28                        华为            huawei
74810         None  M29-31                       TCL               TCL

I want to map two columns and plot this on a line chart.
My approach:

phones = phones.groupby(['only_latin_brands', 'group']).size()
phones = phones.unstack()
phones = phones.fillna(0)
phones.head()
phones.plot(kind='line')
plt.show()

I want to plot the relation between the group and the only_latin_brands.

How can I plot only the most occurring 20 only_latin_brands column with their groups?

Body:
You can use groupby size and then the nlargest method:

In [11]: df.groupby("only_latin_brands").size()
Out[11]:
only_latin_brands
OPPO        1
aiyouni     1
coolpad     1
huawei      5
meizu       1
samsung     9
vivo        1
xiaomi     11
dtype: int64

In [12]: df.groupby("only_latin_brands").size().nlargest(2)
Out[12]:
only_latin_brands
xiaomi     11
samsung     9
dtype: int64

Then use isin to filter out just those rows:

In [13]: df[df["only_latin_brands"].isin(df.groupby("only_latin_brands").size().nlargest(2).index)]
Out[13]:
   latin_brands   group phone_brand_chinese_match only_latin_brands
0        xiaomi  M32-38                        小米            xiaomi
1        xiaomi  M32-38                        小米            xiaomi
2        xiaomi  M32-38                        小米            xiaomi
3        xiaomi  M29-31                        小米            xiaomi
4        xiaomi  M29-31                        小米            xiaomi
7        xiaomi  M32-38                        小米            xiaomi
9       samsung  F33-42                        三星           samsung
12      samsung  F27-28                        三星           samsung
16       xiaomi  M32-38                        小米            xiaomi
17       xiaomi  M32-38                        小米            xiaomi
19       xiaomi  M32-38                        小米            xiaomi
20      samsung  F33-42                        三星           samsung
21       xiaomi  M23-26                        小米            xiaomi
23      samsung  M27-28                        三星           samsung
24       xiaomi  M29-31                        小米            xiaomi
25      samsung  M32-38                        三星           samsung
26      samsung  M32-38                        三星           samsung
27      samsung  F33-42                        三星           samsung
28      samsung  M32-38                        三星           samsung
29      samsung  M32-38                        三星           samsung

Now you can do the plot...


问:
Is it possible to disable websockets with Puppeteer?
I am using puppeteer to create screenshots of websites and I want to eleminate all unneccesary traffic. Besides blocking analytics sites and the like I want to block websocket traffic as well.

I was not able to find something in the puppeteer API. Is there maybe a startup argument for this?

答:
You should be looking for Upgrade headers in http requests. Puppeteer has an API for intercepting requests here, however it's not well documented on what gets passed into that function, so you might have to inspect/debug that out a bit.

In short, all websocket requests start with an HTTP request with an Upgrade header as a handshake of sorts. If you can reject those requests then the following websocket request shouldn't ever happen.


问:
Elasticsearch 5.4 - filter by term if the term exists and don't filter when term is not present
I'm searching on multiple types. One of the types returned has a field called my_field. The other types returned do not have that field. I want all results where the term does not exist and only the results where the field has the value True when the term does exist.

It would be great if the filter on my_field didn't contribute to the query score and only filtered.

This is as close as I got. I will self-flagellate for 1 hour if you help me please.

(Don't use this it is wrong!)

```<code>body = {
    'query': {
        'bool': {
            'must': {
                'multi_match': {
                    'query': 'pangolin',
                    'fields': ['_all', '_partials']
                }
            },
            "should": {
                "must_not": {
                    "exists": {
                        "field": "my_field"
                    }
                }
            },
            "should": {
                'term': {
                    'my_field': True
                }
            }
        }
    }
}
</code>```

答:
The following seems to be what I need.
Documents must match on 'pangolin' and documents are filtered on 2 shoulds. Only 1 of the shoulds needs to match.

https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-bool-query.html (see keywords: filter and should).

```<code>body = {
    "query": {
        'bool': {
            'must': {
                'multi_match': {
                    'query': 'pangolin',
                    'fields': ['_all', '_partials']
                }
            },
            "filter": {
                "bool": {
                    "should": [{
                            "term": {
                                "my_field": True
                            }
                        },
                        {
                            "bool": {
                                "must_not": {
                                    "exists": {
                                        "field": "my_field"
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}
</code>```

Body:
I'm searching on multiple types. One of the types returned has a field called my_field. The other types returned do not have that field. I want all results where the term does not exist and only the results where the field has the value True when the term does exist.

It would be great if the filter on my_field didn't contribute to the query score and only filtered.

This is as close as I got. I will self-flagellate for 1 hour if you help me please.

(Don't use this it is wrong!)

body = {
    'query': {
        'bool': {
            'must': {
                'multi_match': {
                    'query': 'pangolin',
                    'fields': ['_all', '_partials']
                }
            },
            "should": {
                "must_not": {
                    "exists": {
                        "field": "my_field"
                    }
                }
            },
            "should": {
                'term': {
                    'my_field': True
                }
            }
        }
    }
}

Body:
The following seems to be what I need.
Documents must match on 'pangolin' and documents are filtered on 2 shoulds. Only 1 of the shoulds needs to match.

https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-bool-query.html (see keywords: filter and should).

body = {
    "query": {
        'bool': {
            'must': {
                'multi_match': {
                    'query': 'pangolin',
                    'fields': ['_all', '_partials']
                }
            },
            "filter": {
                "bool": {
                    "should": [{
                            "term": {
                                "my_field": True
                            }
                        },
                        {
                            "bool": {
                                "must_not": {
                                    "exists": {
                                        "field": "my_field"
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}


问:
React Input with disabled
I have an input with the disabled boolean propagated from the props.  I've found that with that disabled variable being set, even when it's true I cannot type into the input.  I can, however, type into the input if I hold the mouse down on it and keep it down as I type.

This happens when I render the component as both controlled and uncontrolled.

The only way to fix it is to either removing disabled or setting disabled={false} -- but I need it to be variabled.

This is my input:

```<code>  class DashboardWidgetTitle extends React.Component {

    constructor(props) {
      super(props);
      this.state = {};
      this.state.title = props.widget.getTitle();
    }

    render () {
      return <input className="title"
                    value={this.state.title}
                    disabled={!this.props.isEditMode}
                    onChange={this._onInputChange.bind(this)}/>
    }

    _onInputChange(e) {
      logit("input change");
      this.setState({title: e.target.value});
    }

  }
</code>```

Thanks!

EDIT: Extra info --

This is used in a widget within an implementation of ReactGridLayout.

The component is rendered by a widget that is rendered by the implementation of react grid layout, the "dashboard".  That Dashboard has isEditMode as a state that's changed externally via a toggle in Angular (we use React within Angular).

If I switch tabs and come back, the focus remains.  Otherwise whenever I type a letter, the input becomes unfocused.

The input isn't disabled when it shouldn't be, it just has focusing issues -- however removing the disabled attribute or setting it as a static variable, the issue doesn't remain.

When I switch the disabled toggle to be isEditMode rather than !isEditMode, everything works.  It seems to be there's an onclick on the draggable/resizable portion of the ReactGridLayout that's causing some issues.

答:
My issue was described right here: https://github.com/STRML/react-grid-layout/issues/615

My own fault of poor research.

Title: React Input with disabled

Body:
I have an input with the disabled boolean propagated from the props.  I've found that with that disabled variable being set, even when it's true I cannot type into the input.  I can, however, type into the input if I hold the mouse down on it and keep it down as I type.

This happens when I render the component as both controlled and uncontrolled.

The only way to fix it is to either removing disabled or setting disabled={false} -- but I need it to be variabled.

This is my input:

  class DashboardWidgetTitle extends React.Component {

    constructor(props) {
      super(props);
      this.state = {};
      this.state.title = props.widget.getTitle();
    }

    render () {
      return <input className="title"
                    value={this.state.title}
                    disabled={!this.props.isEditMode}
                    onChange={this._onInputChange.bind(this)}/>
    }

    _onInputChange(e) {
      logit("input change");
      this.setState({title: e.target.value});
    }

  }

Thanks!

EDIT: Extra info --

This is used in a widget within an implementation of ReactGridLayout.

The component is rendered by a widget that is rendered by the implementation of react grid layout, the "dashboard".  That Dashboard has isEditMode as a state that's changed externally via a toggle in Angular (we use React within Angular).

If I switch tabs and come back, the focus remains.  Otherwise whenever I type a letter, the input becomes unfocused.

The input isn't disabled when it shouldn't be, it just has focusing issues -- however removing the disabled attribute or setting it as a static variable, the issue doesn't remain.

When I switch the disabled toggle to be isEditMode rather than !isEditMode, everything works.  It seems to be there's an onclick on the draggable/resizable portion of the ReactGridLayout that's causing some issues.


问:
Response has nothing in it
I have been following the scrapy tutorial trying to create a very simple web scraper for warframe.market.  I have about a year of coding experience from school, but no python experience.  I simply want to get the price of an item from the website.  I used the following to scrape the page:

```<code>scrapy shell "https://warframe.market/items/hydroid_prime_set"
</code>```

then I inspected the web page to find the individual elements that I am trying to scrape.  I used this command to try to view the results I wanted:

```<code>response.css("div.order-row.d-flex.col-12").extract()
</code>```

This did not work, so I used view(response) to see what I had scraped, and my cmd just waits endlessly at this point.

Is HTTPS stopping me from scraping? Am I selecting the wrong css in my response? Is the webpage too big?  Could someone please show me where I went wrong?

Thanks

答:
The response isn't empty, but it's rendered using javascript (you can validate it inspecting the response.body), for example try this in the shell:

```<code>import json
data = json.loads(response.css('#application-state::text').extract_first())
for order in data.get('payload',{}).get('orders', []):
    print '"{}" price: {}'.format(order.get('platinum'),
                                  order.get('user',{}).get('ingame_name'))
</code>```

Title: Response has nothing in it

Body:
I have been following the scrapy tutorial trying to create a very simple web scraper for warframe.market.  I have about a year of coding experience from school, but no python experience.  I simply want to get the price of an item from the website.  I used the following to scrape the page:

scrapy shell "https://warframe.market/items/hydroid_prime_set"

then I inspected the web page to find the individual elements that I am trying to scrape.  I used this command to try to view the results I wanted:

response.css("div.order-row.d-flex.col-12").extract()

This did not work, so I used view(response) to see what I had scraped, and my cmd just waits endlessly at this point.

Is HTTPS stopping me from scraping? Am I selecting the wrong css in my response? Is the webpage too big?  Could someone please show me where I went wrong?

Thanks

Body:
The response isn't empty, but it's rendered using javascript (you can validate it inspecting the response.body), for example try this in the shell:

import json
data = json.loads(response.css('#application-state::text').extract_first())
for order in data.get('payload',{}).get('orders', []):
    print '"{}" price: {}'.format(order.get('platinum'),
                                  order.get('user',{}).get('ingame_name'))


问:
Netbeans Error: Could not find or load main class (again)
I am very experienced in Java, but not in Netbeans. I have a project that I've been working on, a little test program for a particular issue, so I have been making changes to it and running it and debugging it many times a day, every day, for several weeks. Yesterday Netbeans quit running it the way I had been running it, and I can't seem to get it started again.

The entire output when I run the project is:

```<code>run: Error: Could not find or load main class

C:\Users\rcook\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
</code>```

I note for whatever it's worth that the classname does not appear on the first line, as it seems to with others who have had the "could not find" error.

This happens whether I run the project (select the project and press F6) or the file (select the file and press control-F6). I have cleaned and rebuilt the project (which I didn't do often before, I've wondered if it caused the problem). I have restarted Netbeans. I have restarted the (Win7) computer. I am not running any memory-hogging programs except for Outlook, which I have been running the entire time it was working.

I can run the program from the command line by setting my default directory down to the 'classes' directory under the project and entering java stsclientexample3.STSClientExample3.

I have checked the "Run" configuration many times -- there are no program or VM options entered, it has the correct classname. Oddly enough, if I change the classname to something that isn't there, Netbeans displays a popup that says the invalid name class wasn't found in the project, and listing the main classes available. But when I do that and select the one to run, it gives the same "could not find" error. There's one other main class in the project, if I select THAT it gives the same "could not find" error.

I have looked at the XML file referenced in the message, but it is full of ${parameter} references that I don't know the resolution of. It would likely be helpful to know how Netbeans resolves "${classname}", for instance, or even to dump the value Netbeans has at runtime somehow.

I have commented out code and re-run. Same result.

I have deleted that class from the run configuration, restarted Netbeans, and added the class back. Same result.

By now I've read more than a dozen other queries for a similar error, on SO and other places; I've tried all the things that looked at all possible (skipping things like configuring for special-purpose libraries, etc.).

Anyone know how to fix this?

EDIT: before someone asks, another project with a main() runs using the 'run project' option.

EDIT2: I forgot to mention, also, that another post recommended that one delete the Netbeans cache (c:\Users\\AppData\Local\NetBeans\Cache), so I shut down NetBeans, deleted everything in that directory, and restarted NetBeans. Same result.

答: I had the same problem and i think the problem was in the file path, either it was too long or it had some forbidden characters(now i have seen that it was a letter from my language), when i copied a project to a different folder it worked...</p>

Body:
I am very experienced in Java, but not in Netbeans. I have a project that I've been working on, a little test program for a particular issue, so I have been making changes to it and running it and debugging it many times a day, every day, for several weeks. Yesterday Netbeans quit running it the way I had been running it, and I can't seem to get it started again.

The entire output when I run the project is:

run: Error: Could not find or load main class

C:\Users\rcook\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

I note for whatever it's worth that the classname does not appear on the first line, as it seems to with others who have had the "could not find" error.

This happens whether I run the project (select the project and press F6) or the file (select the file and press control-F6). I have cleaned and rebuilt the project (which I didn't do often before, I've wondered if it caused the problem). I have restarted Netbeans. I have restarted the (Win7) computer. I am not running any memory-hogging programs except for Outlook, which I have been running the entire time it was working.

I can run the program from the command line by setting my default directory down to the 'classes' directory under the project and entering java stsclientexample3.STSClientExample3.

I have checked the "Run" configuration many times -- there are no program or VM options entered, it has the correct classname. Oddly enough, if I change the classname to something that isn't there, Netbeans displays a popup that says the invalid name class wasn't found in the project, and listing the main classes available. But when I do that and select the one to run, it gives the same "could not find" error. There's one other main class in the project, if I select THAT it gives the same "could not find" error.

I have looked at the XML file referenced in the message, but it is full of ${parameter} references that I don't know the resolution of. It would likely be helpful to know how Netbeans resolves "${classname}", for instance, or even to dump the value Netbeans has at runtime somehow.

I have commented out code and re-run. Same result.

I have deleted that class from the run configuration, restarted Netbeans, and added the class back. Same result.

By now I've read more than a dozen other queries for a similar error, on SO and other places; I've tried all the things that looked at all possible (skipping things like configuring for special-purpose libraries, etc.).

Anyone know how to fix this?

EDIT: before someone asks, another project with a main() runs using the 'run project' option.

EDIT2: I forgot to mention, also, that another post recommended that one delete the Netbeans cache (c:\Users\\AppData\Local\NetBeans\Cache), so I shut down NetBeans, deleted everything in that directory, and restarted NetBeans. Same result.


问:
How to create Asynchronous Call using Retrofit and Observables in Kotlin?
I want to make API calls using Retrofit2 library, returns generic type observable.

I am getting an error: android.os.NetworkOnMainThreadException, while making calls.

答:
With Retrofit/RxJava, the network call will be default be performed on the thread that subscribes to the Observable returned from the stub. On Android, we are normally executing on the "main" thread, and it is not permitted to access the network on this thread, hence the error.

The solution is to tell RxJava to subscribe to the Observable on a different thread:

```<code>getDataFromAPI(/*...*/)
  .subscribeOn(Schedulers.io())
  .observerOn(AndroidSchedulers.mainThread())
  .subscribe({ /* result available in `it` */ })
</code>```

Schedulers.io() is a reference to a scheduler that uses a set of threads specifically intended to be used for IO operations.

The .observeOn allows you to handle the result safely back on the main thread.

Body:
With Retrofit/RxJava, the network call will be default be performed on the thread that subscribes to the Observable returned from the stub. On Android, we are normally executing on the "main" thread, and it is not permitted to access the network on this thread, hence the error.

The solution is to tell RxJava to subscribe to the Observable on a different thread:

getDataFromAPI(/*...*/)
  .subscribeOn(Schedulers.io())
  .observerOn(AndroidSchedulers.mainThread())
  .subscribe({ /* result available in `it` */ })

Schedulers.io() is a reference to a scheduler that uses a set of threads specifically intended to be used for IO operations.

The .observeOn allows you to handle the result safely back on the main thread.


问:
Nokogiri replace arbitrary nodes
I'm trying to scrape some piece of legislation. Here's an excerpt of the code:

```<code><h5>
<span class="expanderComparator clickable"> </span>
<span class="context-menu"> </span>
<a href="index.html#a340">
<strong>Art. 340</strong>
</a>
<sup>
<a href="#fn-#a340-1">1</a>
</sup>
<a href="index.html#a340">Mesures conservatoires</a>
</h5>
</code>```

This piece of code is repeated for each article (can be above a thousand), but its structure follows essentially the same pattern.

I'm trying to build an array as such:

```<code>a340 = { 'number' => '340', 'title' => 'Mesures conservatoires'}
</code>```

So obviously I looked at Nokogiri, which seems to be appropriate. Here are the problems though:

this piece 1 needs to be replaced by a space (" ") => h1 = @doc.at_xpath "//sup/a" h1.content = " "

all span and a tags need to be removed

ultimately, keep only what's inside the h5 tag (because there's other bloat as well that's not required) => .css("h5").text

I was essentially able to reach all of the above, yet somehow I'm unable to chain them up into one logical software.

Any help would be greatly appreciated!

Caveat: it needs to be pure ruby only, so no rails :(

答:
Maybe accessing to '#lawcontent .collapseable h5' could work:

```<code>require 'nokogiri'
require 'open-uri'

url = 'https://www.admin.ch/opc/fr/classified-compilation/20061121/index.html'
doc = Nokogiri.HTML(open url)
articles_hash = doc.css('#lawcontent .collapseable h5').map do |element|
  {
    'number' => element.css('a strong').text.sub(/\AArt. /, ''),
    'title'  => element.css('a:last-child').text.sub(/\AArt. \d+ |\A\d+/, '')
  }
end
puts articles_hash

# [
#  {"number"=>"1", "title"=>"Objet"},
#  {"number"=>"2", "title"=>"Causes de nature internationale"},
#  {"number"=>"3", "title"=>"Organisation des tribunaux et des autorit\u00E9s de conciliation"},
#  ...
# ]
</code>```

This iterates and gets until the article #407.

Title: Nokogiri replace arbitrary nodes

Body:
I'm trying to scrape some piece of legislation. Here's an excerpt of the code:

Art. 340

1

Mesures conservatoires

This piece of code is repeated for each article (can be above a thousand), but its structure follows essentially the same pattern.

I'm trying to build an array as such:

a340 = { 'number' => '340', 'title' => 'Mesures conservatoires'}

So obviously I looked at Nokogiri, which seems to be appropriate. Here are the problems though:

this piece 1 needs to be replaced by a space (" ") => h1 = @doc.at_xpath "//sup/a" h1.content = " "

all span and a tags need to be removed

ultimately, keep only what's inside the h5 tag (because there's other bloat as well that's not required) => .css("h5").text

I was essentially able to reach all of the above, yet somehow I'm unable to chain them up into one logical software.

Any help would be greatly appreciated!

Caveat: it needs to be pure ruby only, so no rails :(

Body:
Maybe accessing to '#lawcontent .collapseable h5' could work:

require 'nokogiri'
require 'open-uri'

url = 'https://www.admin.ch/opc/fr/classified-compilation/20061121/index.html'
doc = Nokogiri.HTML(open url)
articles_hash = doc.css('#lawcontent .collapseable h5').map do |element|
  {
    'number' => element.css('a strong').text.sub(/\AArt. /, ''),
    'title'  => element.css('a:last-child').text.sub(/\AArt. \d+ |\A\d+/, '')
  }
end
puts articles_hash

# [
#  {"number"=>"1", "title"=>"Objet"},
#  {"number"=>"2", "title"=>"Causes de nature internationale"},
#  {"number"=>"3", "title"=>"Organisation des tribunaux et des autorit\u00E9s de conciliation"},
#  ...
# ]

This iterates and gets until the article #407.


问:
Tensorflow Object Detection : Bounding box doesn't overlay on image
I am trying to run Tensorflow's Object Detection example(object_detection_tutorial.ipynb) as it is. But, at the end of the complete Jupyter notebook execution I don't see the bounding box.

I have followed instruction by running the following commands in the right paths-

```<code>protoc object_detection/protos/*.proto --python_out=.

export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
</code>```

And even the model builder test runs successfully-

```<code>python object_detection/builders/model_builder_test.py
</code>```

I have verified that the model has successfully downloaded. And even the images are in the right paths.

I don't see any errors in the console as well. Please can you tell me what am I doing wrong?

答:
The current model listed by the master branch is unstable.

This is related to tensorflow/models issue 2773 and there is a fix for it.
In the "Variables" section of models/research/object_detection/object_detection_tutorial.ipynb you have to change the model.

Just replace the model name so the it looks like this

```<code>MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017'
</code>```

and you will happily detect dogs and people and kites in no time :)

Body:
I am trying to run Tensorflow's Object Detection example(object_detection_tutorial.ipynb) as it is. But, at the end of the complete Jupyter notebook execution I don't see the bounding box.

I have followed instruction by running the following commands in the right paths-

protoc object_detection/protos/*.proto --python_out=.

export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

And even the model builder test runs successfully-

python object_detection/builders/model_builder_test.py

I have verified that the model has successfully downloaded. And even the images are in the right paths.

I don't see any errors in the console as well. Please can you tell me what am I doing wrong?

Body:
The current model listed by the master branch is unstable.

This is related to tensorflow/models issue 2773 and there is a fix for it.
In the "Variables" section of models/research/object_detection/object_detection_tutorial.ipynb you have to change the model.

Just replace the model name so the it looks like this

MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017'

and you will happily detect dogs and people and kites in no time :)


问:
Delete null values in nested javascript objects
I have a nested object and want to remove all key/value pairs if the value is null or undefined. I've managed to get the below code working but it doesn't check the nested key/value pairs and wondered if someone could help me figure out what needs adding to the code please?

```<code>var myObj = {
  fName:'john',
  lName:'doe',
  dob:{
    displayValue: null,
    value: null
  },
  bbb:null
};

function clean(obj) {
  for (var propName in obj) {
    if (obj[propName] === null || obj[propName] === undefined || obj[propName] === '') {
      delete obj[propName];
    }
  }
  return obj;
}
console.log(clean(myObj));
</code>```

The above code does the job to remove 'bbb' and its value and I want the same done for the nested object represented by 'dob' as well.

https://jsbin.com/mudirateso/edit?js,console,output

Any help is greatly appreciated.

答:
You're already almost there. Just have the function recurse if the property is another object:

var myObj = {
  fName:'john',
  lName:'doe',
  dob:{
    displayValue: null,
    value: null
  },
  bbb:null
};

function clean(obj) {
  for (var propName in obj) {
    if (obj[propName] === null || obj[propName] === undefined || obj[propName] === '') {
      delete obj[propName];
    } else if (typeof obj[propName] === "object") {
      // Recurse here if the property is another object.
      clean(obj[propName])
    }
  }
  return obj;
}
console.log(clean(myObj));```

Body:
I have a nested object and want to remove all key/value pairs if the value is null or undefined. I've managed to get the below code working but it doesn't check the nested key/value pairs and wondered if someone could help me figure out what needs adding to the code please?

var myObj = {
  fName:'john',
  lName:'doe',
  dob:{
    displayValue: null,
    value: null
  },
  bbb:null
};

function clean(obj) {
  for (var propName in obj) {
    if (obj[propName] === null || obj[propName] === undefined || obj[propName] === '') {
      delete obj[propName];
    }
  }
  return obj;
}
console.log(clean(myObj));

The above code does the job to remove 'bbb' and its value and I want the same done for the nested object represented by 'dob' as well.

https://jsbin.com/mudirateso/edit?js,console,output

Any help is greatly appreciated.


问:
Testing a function imported into a React component with Enzyme
I'm trying to come up with a way to test a component method that makes use of a helper function, like so:

```<code>handleUserClick = e => {
  this.setState({ somePieceOfState: e.target.value }, () => {
    someHelperFn(e.target.value);
  });
}
</code>```

This is a bit of a crude example, but basically right now I have code coverage (Jest) for the entire component except the line where the helper function is being called. I don't see how importing the helper function separately into the test file could help either, so I'm stumped.

EDIT: I'm asking this as a broad question for how to test components that make use of helper functions or functions that are not component methods, not specifically for that^ example above.

Upon request, here is a closer look at the function being called:

```<code>const someHelperFn = params => {
  const BASE_URL = '/api/public/log-client-event';
  const queryString = generateQueryString(params);
  const logClientRequest = new XMLHttpRequest();
  logClientRequest.open('GET', `${BASE_URL}${queryString}`);
  logClientRequest.send();
  return logClientRequest;
};
</code>```

It is not returning a value. It just seems like Jest is complaining because it exists within the React component and there's no way to actually test it (that I know of).

答:
In this example you provided you would mock out XMLHttpRequest. You would then test that it's been called when handleUserClick is called. It depends on what the helper function is doing to how you can test it but their is almost certainly a way.

Istanbul (not jest) is complaining because you have no test that is actually executing the function I imagine so it marks it as not covered by any test.

It's really too broad of question to get a definitive answer for testing all helper functions.

Edit:

If you must test the helper function and can't test it internally like I did above then you can use proxyQuire as a last resort to mock the helper function out into a spy and then check that it was called. I wouldn't do this as default though because it can get messy.

Body:
I'm trying to come up with a way to test a component method that makes use of a helper function, like so:

handleUserClick = e => {
  this.setState({ somePieceOfState: e.target.value }, () => {
    someHelperFn(e.target.value);
  });
}

This is a bit of a crude example, but basically right now I have code coverage (Jest) for the entire component except the line where the helper function is being called. I don't see how importing the helper function separately into the test file could help either, so I'm stumped.

EDIT: I'm asking this as a broad question for how to test components that make use of helper functions or functions that are not component methods, not specifically for that^ example above.

Upon request, here is a closer look at the function being called:

const someHelperFn = params => {
  const BASE_URL = '/api/public/log-client-event';
  const queryString = generateQueryString(params);
  const logClientRequest = new XMLHttpRequest();
  logClientRequest.open('GET', `${BASE_URL}${queryString}`);
  logClientRequest.send();
  return logClientRequest;
};

It is not returning a value. It just seems like Jest is complaining because it exists within the React component and there's no way to actually test it (that I know of).


问:
Replace Quotation in List of Lists R
I am trying to get a JSON response from an API:

```<code>test <- GET(url, add_headers(`api_key` = key))

content(test, 'parsed')
</code>```

When I run content(test, 'parsed'), I get the following error:

```<code># Error: lexical error: invalid string in json text. .Note: Final passage of the "fiscal cliff bill" on January 1
</code>```

I think this is because of the double quotations. How can I either replace the double quotes or if this is not the problem, how can I fix this issue?

Thanks!

答:
So I had run into a similar problem before, and I had intended to write a quite function to use Jeroen's fix to try to repair the JSON. Since I intended to do it anyway, here's a quick hack attempt.

NB: repairing a structured format like this is speculative at best and most certainly prone to errors. The good news is that I tried to keep this specific enough so that it will not produce false results: it'll either fix what it knows it can, or fail. The "unit-testing" really needs to check other corner-cases. If you find something that this does not fix (and should) or that this breaks (gasp!), please comment!

```<code>fix_json_quotes <- function(s) {
  if (length(s) != 1) {
    warning("the argument has length > 1 and only the first element will be used")
    s <- s[[1]]
  }
  stopifnot(is.character(s))
  val <- jsonlite::validate(s)
  while (! val) {
    ind <- attr(val, "offset") - 1
    snew <- gsub("(.*)(['\"])([[:space:],]*)$", "\\1\\\\\\2\\3", substr(s, 1, ind))
    if (snew != substr(s, 1, ind)) {
      s <- paste0(snew, substr(s, ind + 1, nchar(s)))
    } else {
      break
    }
    val <- jsonlite::validate(s)
  }
  if (! val) {
    # still not validating
    stop("unable to fix quotes")
  }
  return(s)
}
</code>```

Some sample data, unit-testing if you will (testthat is not required for use of the function):

```<code>library(testthat)
lst <- list(a="final \"cliff bill\" on")
json <- as.character(toJSON(lst))
json
# [1] "{\"a\":[\"final \\\"cliff bill\\\" on\"]}"
</code>```

Okay, there should be no change:

```<code>expect_equal(json, fix_json_quotes(json))
</code>```

Some bad data:

```<code># un-escape the double quotes
badlst <- "{\"a\":[\"final \"cliff bill\" on\"]}"
expect_error(jsonlite::fromJSON(badlst))
expect_equal(json, fix_json_quotes(badlst))
</code>```

PS: this looks specifically for double-quotes, nothing more. However, I believe that there are related errors that this might also be able to fix. I "left room" for this, in the second group within the regex (([\"])); for example, if single-quotes could also cause a problem, then the group could be changed to be ([\"']). I don't know if it's useful or even necessary.

Title: Replace Quotation in List of Lists R

Body:
I am trying to get a JSON response from an API:

test <- GET(url, add_headers(`api_key` = key))

content(test, 'parsed')

When I run content(test, 'parsed'), I get the following error:

# Error: lexical error: invalid string in json text. .Note: Final passage of the "fiscal cliff bill" on January 1

I think this is because of the double quotations. How can I either replace the double quotes or if this is not the problem, how can I fix this issue?

Thanks!

Body:
So I had run into a similar problem before, and I had intended to write a quite function to use Jeroen's fix to try to repair the JSON. Since I intended to do it anyway, here's a quick hack attempt.

NB: repairing a structured format like this is speculative at best and most certainly prone to errors. The good news is that I tried to keep this specific enough so that it will not produce false results: it'll either fix what it knows it can, or fail. The "unit-testing" really needs to check other corner-cases. If you find something that this does not fix (and should) or that this breaks (gasp!), please comment!

fix_json_quotes <- function(s) {
  if (length(s) != 1) {
    warning("the argument has length > 1 and only the first element will be used")
    s <- s[[1]]
  }
  stopifnot(is.character(s))
  val <- jsonlite::validate(s)
  while (! val) {
    ind <- attr(val, "offset") - 1
    snew <- gsub("(.*)(['\"])([[:space:],]*)$", "\\1\\\\\\2\\3", substr(s, 1, ind))
    if (snew != substr(s, 1, ind)) {
      s <- paste0(snew, substr(s, ind + 1, nchar(s)))
    } else {
      break
    }
    val <- jsonlite::validate(s)
  }
  if (! val) {
    # still not validating
    stop("unable to fix quotes")
  }
  return(s)
}

Some sample data, unit-testing if you will (testthat is not required for use of the function):

library(testthat)
lst <- list(a="final \"cliff bill\" on")
json <- as.character(toJSON(lst))
json
# [1] "{\"a\":[\"final \\\"cliff bill\\\" on\"]}"

Okay, there should be no change:

expect_equal(json, fix_json_quotes(json))

Some bad data:

# un-escape the double quotes
badlst <- "{\"a\":[\"final \"cliff bill\" on\"]}"
expect_error(jsonlite::fromJSON(badlst))
expect_equal(json, fix_json_quotes(badlst))

PS: this looks specifically for double-quotes, nothing more. However, I believe that there are related errors that this might also be able to fix. I "left room" for this, in the second group within the regex (([\"])); for example, if single-quotes could also cause a problem, then the group could be changed to be ([\"']). I don't know if it's useful or even necessary.


问:
Split Recordset Every 50 Records
I know how to use access VBA to get the total count of records in the recordset.  Which is what I do below, but I am in need of taking my main rs and splitting the rs into sub-recordsets every 50 records.  Meaning if rs.RecordCount = 200 - then I would want to create, rs1, rs2, rs3, rs4 and the recordCount of each should be 50.

How can I dynamically split a recordset into sub-recordsets as desecribed above?  This is how I get the count in the primary recordset

```<code>    Set rs = CurrentDb.OpenRecordset("Select firen, daja, globaltwo", dbOpenDynaset)

With rs
   If Not .EOF And Not .BOF Then
      .MoveLast
      .MoveFirst
      Do While Not .EOF
        qty = rst.RecordCount
        Debug.Print qty
      Loop
   End If
End With

rs.Close
Set rs = Nothing
</code>```

答:
You might want to see if the OLEDB provider supports paging.   See this https://support.microsoft.com/en-us/help/202125/how-to-page-through-a-recordset-from-asp

The given link to an Access 97 database which suggest access does support this even though the code is an Active Server Page the PageSize property is still available to VBA clients.

Title: Split Recordset Every 50 Records

Body:
I know how to use access VBA to get the total count of records in the recordset.  Which is what I do below, but I am in need of taking my main rs and splitting the rs into sub-recordsets every 50 records.  Meaning if rs.RecordCount = 200 - then I would want to create, rs1, rs2, rs3, rs4 and the recordCount of each should be 50.

How can I dynamically split a recordset into sub-recordsets as desecribed above?  This is how I get the count in the primary recordset

    Set rs = CurrentDb.OpenRecordset("Select firen, daja, globaltwo", dbOpenDynaset)

With rs
   If Not .EOF And Not .BOF Then
      .MoveLast
      .MoveFirst
      Do While Not .EOF
        qty = rst.RecordCount
        Debug.Print qty
      Loop
   End If
End With

rs.Close
Set rs = Nothing