text:
Q: ERROR Instrumentation: org.apache.spark.SparkException: Task not serializable I am new to Scala and Spark. I am trying building a machine learning model. when i try to fit the model using training data using val model = pipeline.fit(trainingData) It throws error saying Task is not serializable. Please help me to resolve this issue. Please find below code for the same.
package com.skillassure.spark

import org.apache.spark._
import org.apache.spark.sql._
import org.apache.log4j._
import org.apache.spark.sql.functions._
import org.apache.spark.ml._
import org.apache.spark.ml.feature.Bucketizer
import org.apache.spark.ml.feature.{RegexTokenizer, IDF}
import org.apache.spark.ml.feature.StopWordsRemover
import org.apache.spark.ml.feature.{CountVectorizer, CountVectorizerModel}
import org.apache.spark.ml.classification.LogisticRegression
import org.apache.spark.ml.classification.LogisticRegressionModel
import org.apache.spark.sql.functions._
import org.apache.spark.sql.types._
import org.apache.spark.ml.feature._
import org.apache.spark.ml.classification._
import org.apache.spark.ml.evaluation._
import org.apache.spark.ml.tuning._
import scala.reflect.api.materializeTypeTag

object ReviewAnalysis extends java.io.Serializable {

def main(args: Array[String]): Unit =  {

    // Set the log level to only print errors
    Logger.getLogger("org").setLevel(Level.ERROR)
    val spark = SparkSession.builder
                  .appName("SparkSessionExample")
                  .master("local[*]")
                  .getOrCreate;

    var file ="src/Resource/review-sample.json";

    val df0  = spark.read.format("json")
     .option("inferSchema", "true")
     .load(file);
    val df = df0.withColumn("reviewTS",df0.col("summary")+ " " + df0.col("reviewText"))
                 .drop("helpful")
                 .drop("reviewerID")
                 .drop("reviewerName")
                 .drop("reviewTime");

    //df.printSchema;

    //df.show(5);
    df.describe("overall").show;

    //
    val df1 = df.filter("overall !=3");
    val bucketizer = new Bucketizer()
                          .setInputCol("overall")
                          .setOutputCol("label")
                          .setSplits(Array(Double.NegativeInfinity, 4.0,
                           Double.PositiveInfinity))
    val df2= bucketizer.transform(df1);
    df2.show(5);
    //val df3 = df2.groupBy("overall","label").count.show;
    //df2.groupBy("label").count.show(5);

    val dff = df2.selectExpr("asin", "cast(overall as string) overall","reviewText","summary","unixReviewTime","cast(reviewTS as string) reviewTS","label");
    dff.show(10);
    val fractions = Map(1.0 -> .1, 0.0 -> 1.0);
    val df3 = dff.stat.sampleBy("label", fractions, 36L);
    df3.show(3);
    //df2.stat.sampleBy("label", fractions, 36L);
    //df3.groupBy("label").count().show;
    df3.printSchema();
    val splitSeed = 5043
    val Array(trainingData, testData) = dff.randomSplit(Array(0.8, 0.2), splitSeed)

    val tokenizer = new RegexTokenizer()
                        .setInputCol("reviewTS")
                        .setOutputCol("reviewTokensUf")
                        .setPattern("\\s+|[,.()\"]");
    val remover = new StopWordsRemover()
                        .setStopWords(StopWordsRemover
                        .loadDefaultStopWords("english"))
                        .setInputCol("reviewTokensUf")
                        .setOutputCol("reviewTokens");

    val cv = new CountVectorizer()
                        .setInputCol("reviewTokens")
                        .setOutputCol("cv")
                        .setVocabSize(200000);

    // list of feature columns
    val idf = new IDF()
                .setInputCol("cv")
                .setOutputCol("features");

    // create Logistic Regression estimator
    // regularizer parameters avoid overfitting
    val lr = new LogisticRegression()
                    .setMaxIter(100)
                    .setRegParam(0.02)
                    .setElasticNetParam(0.3);

    //val steps =  Array( tokenizer, remover, cv, idf,lr);
    val pipeline = new Pipeline().setStages(Array( tokenizer, remover, cv, idf,lr))
    //val pipeline = new Pipeline().setStages(steps);

    val model = pipeline.fit(trainingData)

}

}
Please find the complete error:
    20/06/14 06:08:51 ERROR Instrumentation: org.apache.spark.SparkException: Task not serializable
    at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:396)
    at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:386)
    at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:159)
    at org.apache.spark.SparkContext.clean(SparkContext.scala:2379)
    at org.apache.spark.rdd.RDD.$anonfun$mapPartitionsWithIndex$1(RDD.scala:886)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:388)
    at org.apache.spark.rdd.RDD.mapPartitionsWithIndex(RDD.scala:885)
    at org.apache.spark.sql.execution.WholeStageCodegenExec.doExecute(WholeStageCodegenExec.scala:720)
    at org.apache.spark.sql.execution.SparkPlan.$anonfun$execute$1(SparkPlan.scala:173)
    at org.apache.spark.sql.execution.SparkPlan.$anonfun$executeQuery$1(SparkPlan.scala:211)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:208)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:169)
    at org.apache.spark.sql.execution.DeserializeToObjectExec.doExecute(objects.scala:96)
    at org.apache.spark.sql.execution.SparkPlan.$anonfun$execute$1(SparkPlan.scala:173)
    at org.apache.spark.sql.execution.SparkPlan.$anonfun$executeQuery$1(SparkPlan.scala:211)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:208)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:169)
    at org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:110)
    at org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:109)
    at org.apache.spark.sql.Dataset.rdd$lzycompute(Dataset.scala:3073)
    at org.apache.spark.sql.Dataset.rdd(Dataset.scala:3071)
    at org.apache.spark.ml.feature.CountVectorizer.fit(CountVectorizer.scala:191)
    at org.apache.spark.ml.feature.CountVectorizer.fit(CountVectorizer.scala:149)
    at org.apache.spark.ml.Pipeline.$anonfun$fit$5(Pipeline.scala:155)
    at org.apache.spark.ml.MLEvents.withFitEvent(events.scala:132)
    at org.apache.spark.ml.MLEvents.withFitEvent$(events.scala:125)
    at org.apache.spark.ml.util.Instrumentation.withFitEvent(Instrumentation.scala:42)
    at org.apache.spark.ml.Pipeline.$anonfun$fit$4(Pipeline.scala:155)
    at scala.collection.Iterator.foreach(Iterator.scala:929)
    at scala.collection.Iterator.foreach$(Iterator.scala:929)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1417)
    at scala.collection.IterableViewLike$Transformed.foreach(IterableViewLike.scala:44)
    at scala.collection.IterableViewLike$Transformed.foreach$(IterableViewLike.scala:44)
    at scala.collection.SeqViewLike$AbstractTransformed.foreach(SeqViewLike.scala:37)
    at org.apache.spark.ml.Pipeline.$anonfun$fit$2(Pipeline.scala:151)
    at org.apache.spark.ml.MLEvents.withFitEvent(events.scala:132)
    at org.apache.spark.ml.MLEvents.withFitEvent$(events.scala:125)
    at org.apache.spark.ml.util.Instrumentation.withFitEvent(Instrumentation.scala:42)
    at org.apache.spark.ml.Pipeline.$anonfun$fit$1(Pipeline.scala:137)
    at org.apache.spark.ml.util.Instrumentation$.$anonfun$instrumented$1(Instrumentation.scala:191)
    at scala.util.Try$.apply(Try.scala:209)
    at org.apache.spark.ml.util.Instrumentation$.instrumented(Instrumentation.scala:191)
    at org.apache.spark.ml.Pipeline.fit(Pipeline.scala:137)
    at com.skillassure.spark.ReviewAnalysis$.main(ReviewAnalysis.scala:111)
    at com.skillassure.spark.ReviewAnalysis.main(ReviewAnalysis.scala)
Caused by: java.io.NotSerializableException: scala.runtime.LazyRef
Serialization stack:
    - object not serializable (class: scala.runtime.LazyRef, value: LazyRef thunk)
    - element of array (index: 2)
    - array (class [Ljava.lang.Object;, size 3)
    - field (class: java.lang.invoke.SerializedLambda, name: capturedArgs, type: class [Ljava.lang.Object;)
    - object (class java.lang.invoke.SerializedLambda, SerializedLambda[capturingClass=class org.apache.spark.sql.catalyst.expressions.ScalaUDF, functionalInterfaceMethod=scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;, implementation=invokeStatic org/apache/spark/sql/catalyst/expressions/ScalaUDF.$anonfun$f$2:(Lscala/Function1;Lorg/apache/spark/sql/catalyst/expressions/Expression;Lscala/runtime/LazyRef;Lorg/apache/spark/sql/catalyst/InternalRow;)Ljava/lang/Object;, instantiatedMethodType=(Lorg/apache/spark/sql/catalyst/InternalRow;)Ljava/lang/Object;, numCaptured=3])
    - writeReplace data (class: java.lang.invoke.SerializedLambda)
    - object (class org.apache.spark.sql.catalyst.expressions.ScalaUDF$$Lambda$2506/239902985, org.apache.spark.sql.catalyst.expressions.ScalaUDF$$Lambda$2506/239902985@3f64d943)
    - field (class: org.apache.spark.sql.catalyst.expressions.ScalaUDF, name: f, type: interface scala.Function1)
    - object (class org.apache.spark.sql.catalyst.expressions.ScalaUDF, bucketizer_0(knownnotnull(overall#9)))
    - field (class: org.apache.spark.sql.catalyst.expressions.If, name: falseValue, type: class org.apache.spark.sql.catalyst.expressions.Expression)
    - object (class org.apache.spark.sql.catalyst.expressions.If, if (isnull(overall#9)) null else bucketizer_0(knownnotnull(overall#9)))
    - field (class: org.apache.spark.sql.catalyst.expressions.Alias, name: child, type: class org.apache.spark.sql.catalyst.expressions.Expression)
    - object (class org.apache.spark.sql.catalyst.expressions.Alias, if (isnull(overall#9)) null else bucketizer_0(knownnotnull(overall#9)) AS label#133)
    - element of array (index: 5)
    - array (class [Ljava.lang.Object;, size 6)
    - field (class: scala.collection.mutable.ArrayBuffer, name: array, type: class [Ljava.lang.Object;)
    - object (class scala.collection.mutable.ArrayBuffer, ArrayBuffer(asin#7, cast(overall#9 as string) AS overall#187, reviewText#10, summary#14, unixReviewTime#15L, if (isnull(overall#9)) null else bucketizer_0(knownnotnull(overall#9)) AS label#133))
    - field (class: org.apache.spark.sql.execution.ProjectExec, name: projectList, type: interface scala.collection.Seq)
    - object (class org.apache.spark.sql.execution.ProjectExec, Project [asin#7, cast(overall#9 as string) AS overall#187, reviewText#10, summary#14, unixReviewTime#15L, if (isnull(overall#9)) null else bucketizer_0(knownnotnull(overall#9)) AS label#133]
+- Filter (isnotnull(overall#9) AND NOT (overall#9 = 3.0))
   +- BatchScan[asin#7, overall#9, reviewText#10, summary#14, unixReviewTime#15L] JsonScan Location: InMemoryFileIndex[file:/C:/SparkScala/SparkLearning/src/Resource/review-sample.json], ReadSchema: struct<asin:string,overall:double,reviewText:string,summary:string,unixReviewTime:bigint>
)
    - field (class: org.apache.spark.sql.execution.SortExec, name: child, type: class org.apache.spark.sql.execution.SparkPlan)
    - object (class org.apache.spark.sql.execution.SortExec, Sort [asin#7 ASC NULLS FIRST, overall#187 ASC NULLS FIRST, reviewText#10 ASC NULLS FIRST, summary#14 ASC NULLS FIRST, unixReviewTime#15L ASC NULLS FIRST, label#133 ASC NULLS FIRST], false, 0
+- Project [asin#7, cast(overall#9 as string) AS overall#187, reviewText#10, summary#14, unixReviewTime#15L, if (isnull(overall#9)) null else bucketizer_0(knownnotnull(overall#9)) AS label#133]
   +- Filter (isnotnull(overall#9) AND NOT (overall#9 = 3.0))
      +- BatchScan[asin#7, overall#9, reviewText#10, summary#14, unixReviewTime#15L] JsonScan Location: InMemoryFileIndex[file:/C:/SparkScala/SparkLearning/src/Resource/review-sample.json], ReadSchema: struct<asin:string,overall:double,reviewText:string,summary:string,unixReviewTime:bigint>
)
    - element of array (index: 0)
    - array (class [Ljava.lang.Object;, size 15)
    - element of array (index: 1)
    - array (class [Ljava.lang.Object;, size 3)
    - field (class: java.lang.invoke.SerializedLambda, name: capturedArgs, type: class [Ljava.lang.Object;)
    - object (class java.lang.invoke.SerializedLambda, SerializedLambda[capturingClass=class org.apache.spark.sql.execution.WholeStageCodegenExec, functionalInterfaceMethod=scala/Function2.apply:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;, implementation=invokeStatic org/apache/spark/sql/execution/WholeStageCodegenExec.$anonfun$doExecute$4$adapted:(Lorg/apache/spark/sql/catalyst/expressions/codegen/CodeAndComment;[Ljava/lang/Object;Lorg/apache/spark/sql/execution/metric/SQLMetric;Ljava/lang/Object;Lscala/collection/Iterator;)Lscala/collection/Iterator;, instantiatedMethodType=(Ljava/lang/Object;Lscala/collection/Iterator;)Lscala/collection/Iterator;, numCaptured=3])
    - writeReplace data (class: java.lang.invoke.SerializedLambda)
    - object (class org.apache.spark.sql.execution.WholeStageCodegenExec$$Lambda$2181/2096690266, org.apache.spark.sql.execution.WholeStageCodegenExec$$Lambda$2181/2096690266@79d14037)
    at org.apache.spark.serializer.SerializationDebugger$.improveException(SerializationDebugger.scala:41)
    at org.apache.spark.serializer.JavaSerializationStream.writeObject(JavaSerializer.scala:47)
    at org.apache.spark.serializer.JavaSerializerInstance.serialize(JavaSerializer.scala:101)
    at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:393)
    ... 48 more

Exception in thread "main" org.apache.spark.SparkException: Task not serializable
    at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:396)
    at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:386)
    at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:159)
    at org.apache.spark.SparkContext.clean(SparkContext.scala:2379)
    at org.apache.spark.rdd.RDD.$anonfun$mapPartitionsWithIndex$1(RDD.scala:886)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:388)
    at org.apache.spark.rdd.RDD.mapPartitionsWithIndex(RDD.scala:885)
    at org.apache.spark.sql.execution.WholeStageCodegenExec.doExecute(WholeStageCodegenExec.scala:720)
    at org.apache.spark.sql.execution.SparkPlan.$anonfun$execute$1(SparkPlan.scala:173)
    at org.apache.spark.sql.execution.SparkPlan.$anonfun$executeQuery$1(SparkPlan.scala:211)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:208)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:169)
    at org.apache.spark.sql.execution.DeserializeToObjectExec.doExecute(objects.scala:96)
    at org.apache.spark.sql.execution.SparkPlan.$anonfun$execute$1(SparkPlan.scala:173)
    at org.apache.spark.sql.execution.SparkPlan.$anonfun$executeQuery$1(SparkPlan.scala:211)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:208)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:169)
    at org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:110)
    at org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:109)
    at org.apache.spark.sql.Dataset.rdd$lzycompute(Dataset.scala:3073)
    at org.apache.spark.sql.Dataset.rdd(Dataset.scala:3071)
    at org.apache.spark.ml.feature.CountVectorizer.fit(CountVectorizer.scala:191)
    at org.apache.spark.ml.feature.CountVectorizer.fit(CountVectorizer.scala:149)
    at org.apache.spark.ml.Pipeline.$anonfun$fit$5(Pipeline.scala:155)
    at org.apache.spark.ml.MLEvents.withFitEvent(events.scala:132)
    at org.apache.spark.ml.MLEvents.withFitEvent$(events.scala:125)
    at org.apache.spark.ml.util.Instrumentation.withFitEvent(Instrumentation.scala:42)
    at org.apache.spark.ml.Pipeline.$anonfun$fit$4(Pipeline.scala:155)
    at scala.collection.Iterator.foreach(Iterator.scala:929)
    at scala.collection.Iterator.foreach$(Iterator.scala:929)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1417)
    at scala.collection.IterableViewLike$Transformed.foreach(IterableViewLike.scala:44)
    at scala.collection.IterableViewLike$Transformed.foreach$(IterableViewLike.scala:44)
    at scala.collection.SeqViewLike$AbstractTransformed.foreach(SeqViewLike.scala:37)
    at org.apache.spark.ml.Pipeline.$anonfun$fit$2(Pipeline.scala:151)
    at org.apache.spark.ml.MLEvents.withFitEvent(events.scala:132)
    at org.apache.spark.ml.MLEvents.withFitEvent$(events.scala:125)
    at org.apache.spark.ml.util.Instrumentation.withFitEvent(Instrumentation.scala:42)
    at org.apache.spark.ml.Pipeline.$anonfun$fit$1(Pipeline.scala:137)
    at org.apache.spark.ml.util.Instrumentation$.$anonfun$instrumented$1(Instrumentation.scala:191)
    at scala.util.Try$.apply(Try.scala:209)
    at org.apache.spark.ml.util.Instrumentation$.instrumented(Instrumentation.scala:191)
    at org.apache.spark.ml.Pipeline.fit(Pipeline.scala:137)
    at com.skillassure.spark.ReviewAnalysis$.main(ReviewAnalysis.scala:111)
    at com.skillassure.spark.ReviewAnalysis.main(ReviewAnalysis.scala)
Caused by: java.io.NotSerializableException: scala.runtime.LazyRef
Serialization stack:
    - object not serializable (class: scala.runtime.LazyRef, value: LazyRef thunk)
    - element of array (index: 2)
    - array (class [Ljava.lang.Object;, size 3)
    - field (class: java.lang.invoke.SerializedLambda, name: capturedArgs, type: class [Ljava.lang.Object;)
    - object (class java.lang.invoke.SerializedLambda, SerializedLambda[capturingClass=class org.apache.spark.sql.catalyst.expressions.ScalaUDF, functionalInterfaceMethod=scala/Function1.apply:(Ljava/lang/Object;)Ljava/lang/Object;, implementation=invokeStatic org/apache/spark/sql/catalyst/expressions/ScalaUDF.$anonfun$f$2:(Lscala/Function1;Lorg/apache/spark/sql/catalyst/expressions/Expression;Lscala/runtime/LazyRef;Lorg/apache/spark/sql/catalyst/InternalRow;)Ljava/lang/Object;, instantiatedMethodType=(Lorg/apache/spark/sql/catalyst/InternalRow;)Ljava/lang/Object;, numCaptured=3])
    - writeReplace data (class: java.lang.invoke.SerializedLambda)
    - object (class org.apache.spark.sql.catalyst.expressions.ScalaUDF$$Lambda$2506/239902985, org.apache.spark.sql.catalyst.expressions.ScalaUDF$$Lambda$2506/239902985@3f64d943)
    - field (class: org.apache.spark.sql.catalyst.expressions.ScalaUDF, name: f, type: interface scala.Function1)
    - object (class org.apache.spark.sql.catalyst.expressions.ScalaUDF, bucketizer_0(knownnotnull(overall#9)))
    - field (class: org.apache.spark.sql.catalyst.expressions.If, name: falseValue, type: class org.apache.spark.sql.catalyst.expressions.Expression)
    - object (class org.apache.spark.sql.catalyst.expressions.If, if (isnull(overall#9)) null else bucketizer_0(knownnotnull(overall#9)))
    - field (class: org.apache.spark.sql.catalyst.expressions.Alias, name: child, type: class org.apache.spark.sql.catalyst.expressions.Expression)
    - object (class org.apache.spark.sql.catalyst.expressions.Alias, if (isnull(overall#9)) null else bucketizer_0(knownnotnull(overall#9)) AS label#133)
    - element of array (index: 5)
    - array (class [Ljava.lang.Object;, size 6)
    - field (class: scala.collection.mutable.ArrayBuffer, name: array, type: class [Ljava.lang.Object;)
    - object (class scala.collection.mutable.ArrayBuffer, ArrayBuffer(asin#7, cast(overall#9 as string) AS overall#187, reviewText#10, summary#14, unixReviewTime#15L, if (isnull(overall#9)) null else bucketizer_0(knownnotnull(overall#9)) AS label#133))
    - field (class: org.apache.spark.sql.execution.ProjectExec, name: projectList, type: interface scala.collection.Seq)
    - object (class org.apache.spark.sql.execution.ProjectExec, Project [asin#7, cast(overall#9 as string) AS overall#187, reviewText#10, summary#14, unixReviewTime#15L, if (isnull(overall#9)) null else bucketizer_0(knownnotnull(overall#9)) AS label#133]
+- Filter (isnotnull(overall#9) AND NOT (overall#9 = 3.0))
   +- BatchScan[asin#7, overall#9, reviewText#10, summary#14, unixReviewTime#15L] JsonScan Location: InMemoryFileIndex[file:/C:/SparkScala/SparkLearning/src/Resource/review-sample.json], ReadSchema: struct<asin:string,overall:double,reviewText:string,summary:string,unixReviewTime:bigint>
)
    - field (class: org.apache.spark.sql.execution.SortExec, name: child, type: class org.apache.spark.sql.execution.SparkPlan)
    - object (class org.apache.spark.sql.execution.SortExec, Sort [asin#7 ASC NULLS FIRST, overall#187 ASC NULLS FIRST, reviewText#10 ASC NULLS FIRST, summary#14 ASC NULLS FIRST, unixReviewTime#15L ASC NULLS FIRST, label#133 ASC NULLS FIRST], false, 0
+- Project [asin#7, cast(overall#9 as string) AS overall#187, reviewText#10, summary#14, unixReviewTime#15L, if (isnull(overall#9)) null else bucketizer_0(knownnotnull(overall#9)) AS label#133]
   +- Filter (isnotnull(overall#9) AND NOT (overall#9 = 3.0))
      +- BatchScan[asin#7, overall#9, reviewText#10, summary#14, unixReviewTime#15L] JsonScan Location: InMemoryFileIndex[file:/C:/SparkScala/SparkLearning/src/Resource/review-sample.json], ReadSchema: struct<asin:string,overall:double,reviewText:string,summary:string,unixReviewTime:bigint>
)
    - element of array (index: 0)
    - array (class [Ljava.lang.Object;, size 15)
    - element of array (index: 1)
    - array (class [Ljava.lang.Object;, size 3)
    - field (class: java.lang.invoke.SerializedLambda, name: capturedArgs, type: class [Ljava.lang.Object;)
    - object (class java.lang.invoke.SerializedLambda, SerializedLambda[capturingClass=class org.apache.spark.sql.execution.WholeStageCodegenExec, functionalInterfaceMethod=scala/Function2.apply:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;, implementation=invokeStatic org/apache/spark/sql/execution/WholeStageCodegenExec.$anonfun$doExecute$4$adapted:(Lorg/apache/spark/sql/catalyst/expressions/codegen/CodeAndComment;[Ljava/lang/Object;Lorg/apache/spark/sql/execution/metric/SQLMetric;Ljava/lang/Object;Lscala/collection/Iterator;)Lscala/collection/Iterator;, instantiatedMethodType=(Ljava/lang/Object;Lscala/collection/Iterator;)Lscala/collection/Iterator;, numCaptured=3])
    - writeReplace data (class: java.lang.invoke.SerializedLambda)
    - object (class org.apache.spark.sql.execution.WholeStageCodegenExec$$Lambda$2181/2096690266, org.apache.spark.sql.execution.WholeStageCodegenExec$$Lambda$2181/2096690266@79d14037)
    at org.apache.spark.serializer.SerializationDebugger$.improveException(SerializationDebugger.scala:41)
    at org.apache.spark.serializer.JavaSerializationStream.writeObject(JavaSerializer.scala:47)
    at org.apache.spark.serializer.JavaSerializerInstance.serialize(JavaSerializer.scala:101)
    at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:393)
    ... 48 more

A: Problematic code is not in the mentioned object ReviewAnalysis in the description. Please try compiling this by commenting/removing below classes in your project-
com.spark.programming.foodreview$.main(foodreview.scala:154)
at com.spark.programming.foodreview.main(foodreview.scala)


text:
Q: How do I loop through a dictionary, and apply a function using key: value pairs as arguments Dictionary = {File1: "location1", File2: "location2", File3: "location3"}

def fancy_function1(location, file):
  df = pd.read_csv(location)
  df["new_column"] = df[file]
return df

need help needed writing this for loop or any other suggestions
for key in Dictionary:
   ##pass key value pairs into function
    df = fancy_function(key, value)
    return df

I want to then merge all 3 dataframes (created from fancy_function()) or assign each dataframe to variables e.g. df1, df2, df3 etc.

A: I don't know if this helps.
for key in Dictionary:
    value = Dictionary[key]
    df = fancy_function(key, value)
    return df

For me this is strange because you are returning outside a function, if you want to create multiple data frames I suggest the following.
dataframes = []
for key in Dictionary:
    value = Dictionary[key]
    df = fancy_function(key, value)
    dataframes.append(df)

The append method appends to a list so you have a list with all your data frames, be careful because this can consume a lot of ram if you have a lot of data frames or some are big.
I hope this helps.

A: for key in Dictionary:
##pass key value pairs into function
    if df in locals():
        df = fancy_function(key, value)
    else
        df=pd.concat([df, fancy_function(key, value)])
    return df

redpajama_set_name: RedPajamaStackExchange


text:
Q: After updating json file with groovy, the file data contains extra curly brackets and "content" object This is sort of data I got in my json file
{"globals":{"code":"1111","country_code":"8888","hits":80,"extra_hit":1,"keep_money":true},"time_window":{"from":"2020.12.14 08:40:00","to":"2020.12.14 08:45:00"},"car":{"have":"nope"}}

After I run it through this groovy code in jmeter:
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
import groovy.json.JsonOutput

def jsonSlurper = new JsonSlurper().parse(new File("C:/pathToFile/test.json"))

log.info(jsonSlurper.toString())

jsonSlurper.globals.hits = 70
jsonSlurper.time_window.from = "2020.12.14 08:42:00"
jsonSlurper.time_window.to = "2020.12.14 08:48:00"

def builder = new JsonBuilder(jsonSlurper)

log.info(builder.toString())

def json_str = JsonOutput.toJson(builder)
def json_beauty = JsonOutput.prettyPrint(json_str)

log.info(json_beauty.toString())

File file = new File("C:/pathToFile/test.json")
file.write(json_beauty)

the json file is updated, but all data are wrapped in new object "content"
"content": {
        "globals": {
            "code":"1111",
            "country_code": "8888",
            "hits": 70,
            "extra_hit": 1,
            "keep_money": true
        },
        "time_window": {
            "from": "2020.12.14 08:42:00",
            "to": "2020.12.14 08:48:00"
        },
        "car": {
            "have": "nope"
        }
    }
}

How to avoid that wrapping into "content" object?

A: Copying and pasting the code from Internet without having any idea what it is doing is not the best way to proceed, at some point you will end up running a Barmin's patch
My expectation is that you're looking for JsonBuilder.toPrettyString() function so basically everything which goes after this line:
def builder = new JsonBuilder(jsonSlurper)

can be replaced with:
new File("C:/pathToFile/test.json").text = builder.toPrettyString()

More information:

*

*Apache Groovy: Parsing and producing JSON

*Apache Groovy - Why and How You Should Use It


text:
Q: Stream or file is not closing I have he following code
    HttpPostedFileBase files = Request.Files[0];
    Stream fileStream = files.InputStream;

    using (Stream file = System.IO.File.OpenWrite(originalImage))
    {
        StreamUtil.CopyStream(fileStream, file);
        file.Close();
        fileStream.close();
        fileStream.dispose();
    }

    // here is CopyStream method

    public static void CopyStream(Stream input, Stream output)
    {
        var buffer = new byte[8*1024];
        int len;
        while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, len);
        }
    }

When i try to write the same file twice i get
The process cannot access the file \u0027D:FILENAME because it is being used by another process

How can i close this ?so once the request the writing is done, it will be closed?

A: I wanted to write it as a comment but it is not clear so:
Try doing something like this:
MemoryStream fileStream = new MemoryStream();

fileStream.WriteByte(04);

using (Stream file = System.IO.File.OpenWrite(originalImage))
{
    CopyStream(fileStream, file);
}

using (Stream file = System.IO.File.OpenWrite(originalImage))
{
    CopyStream(fileStream, file);
}

Do you get the same error on the second using line?


text:
Q: axios.create is sharing defaults, is that normal? I'm trying to create 2 axios clients, but they seem to be sharing headers. My code is as follows:
var client1 = axios.create();
var client2 = axios.create();

client2.defaults.headers.common['Authorization'] = 123;

console.log(client1.defaults.headers.common);
console.log(client2.defaults.headers.common);

And both have the Authorization header set to 123.
https://jsfiddle.net/yebqu22d/
Am I doing something wrong or is this expected behavior?

A: That should be expected. And exactly what defaults means.
Update:
In my opinion, client1.defaults, client2.defaults and even axios.defaults point to the same reference. No matter which one you changed, it shares the value to others.
But it's possible to have specify config for each instance by create. Check below:
var client1 = axios.create({
        headers: {
        common: {
            Authorization: 123
        }
      }
});
var client2 = axios.create();

//client2.defaults.headers.common['Authorization'] = 123;

console.log(client1.defaults.headers.common);
console.log(client2.defaults.headers.common);

A: Looks like it's a bug with axios: https://github.com/mzabriskie/axios/issues/385


text:
Q: Тестирование классов в графических и консольных приложениях Я для диагностики классов всегда пишу функцию печати внутри самого класса для вывода в консоль. Не отразится ли это на графическом приложении? Стоит ли ее удалять после завершения?
A: *

*Тесты, как правило, должны быть отделены от тестируемого функционала. Это верно на любом уровне, от юнит-тестов, до сложных интеграционных.

*Вы выводите какие-то результаты вызова функции или работы класса в консоль и проверяете их правильность визуально. Это чаще всего непродуктивный подход. Вы не сможете так проверять постоянно возрастающее количество функций и фич. В данном случае необходимы юнит-тесты. Это специально написанные функции, которые вызывают тестируемые функции с различными параметрами и проверяют соответствие реального и ожидаемого результата. Юнит-тесты обеспечивают регрессионное тестирование и позволяют сразу заметить, что в ранее работающий код был внесен баг. Достаточно хорошо про них отвечено здесь: Когда нужно начинать писать Unit тесты?

*Использовать вывод напрямую в консоль нежелательно. Используйте логгер — это специальный класс (или библиотека), который получает входящий поток сообщений и обрабатывает его заданным образом. Как правило, в логгерах доступны следующие функции:

*

*Выбор уровня логирования. Вы можете показывать только сообщения об ошибках, либо их + обычные сообщения о работе, либо это всё + скрип каждой маленькой шестеренки.

*Выбор способа вывода логов. Это может быть консоль, текстовый файл, что угодно еще или вообще ничего.

С логгером вам не придется ничего удалять. Наоборот, во всех случаях возможной ошибки позаботьтесь о том, чтобы логировались все подробности ошибки, которые могут вам быть полезны для ее исправления. Когда вы будете делать билд приложения для пользователей, вы можете просто отключить логирование; можно также писать логи ошибок в файл и как-то пересылать файл разработчикам — это если клиентов немного, а поддержка важна.
Не уверен точно, но подозреваю, что если приложение пишет напрямую в std::cout, то можно запустить его из командной строки и читать логи. Не всегда эти логи предназначены для глаз пользователя.
К сожалению, я не могу подсказать вам конкретных решений на c++, но я уверен, что они существуют, и не в единственном экземпляре.
Примеры:

*

*Юнит-тесты для Git.

*Код с логами для Chromium, см. строчки с DVLOG() (спасибо Abyx за ссылку).


text:
Q: How to use password managers without using the clipboard? I use Keepass, but to type the passwords I need to copy them to the clipboard, which makes the password vulnerable to interception.
I see that equivalent applications for android avoid this issue by using android-specific techniques (alternative keyboard).
Is there any way to use password managers more securely?

A: Yes you can use KeePass more securely than how you are currently and you do not need to switch to another password manager. KeePass2 contains a feature called Auto-Type Obfuscation. Instead of copy-pasting the password you can right-click on the password and select 'Auto-Type' and it will then ALT-TAB (switch) to the last window that had focus and start typing in the username and password (or you can configure it to type other data too if you have more or less fields).
To enable obfuscation on the auto-type right-click on the password record and edit/view the record. Click the Auto-Type tab and at the bottom there will be a textbox that says 'Two-channel auto-type obfuscation'.
When this is enabled the username and password details are obfuscated by jumbling it up and entering it out of order and it is also split into parts with some parts in the clipboard and some parts being entered as a keyboard. This way it would be extremely difficult for a keylogger or clipboard catcher to get the whole password or have any idea how to reconstruct it in full.
Ref: http://keepass.info/help/v2/autotype_obfuscation.html


text:
Q: run Python script as a system command from terminal like "ls" in linux OS I have a python script which i want to run from terminal in linux without using "python" keyword in the beginning.
For example my script name is helloworld.py which is accepting 2 arguments from terminal.
I want to run this script as "helloworld" from terminal instead of "python helloworld.py"

A: To make the file an executable, rename and change the mode for the access permissions, e.g.:
$ mv helloworld.py helloworld
$ chmod +x helloworld

And edit the first line of the file be a shebang:
#!/usr/bin/env python

There are at least three ways to run it from a shell:

*

*Use something like ./helloworld or /path/to/helloworld

*Move it to a directory on your PATH, e.g. /usr/local/bin or ~/bin

*Modify PATH to add the directory, e.g. export PATH=$PATH:/to/dir


text:
Q: Umbraco 7 UmbracoAuthorizedApiController returns 405 (Method Not Allowed) I'm creating a custom tree inside umbraco7 and and have a button which will call an UmbracoAuthorizedApiController called ExportApiController i added a constructor and set a breakpoint on it to see if this controller is indeed instanced and it is. But when i try to call the call my action it is returning an 405 (Method Not Allowed)
And in the response i see the following text
{"Message":"The requested resource does not support http method 'GET'."}

above my controller action i have the HttpGet attribute and i also tried the put or post but nothing helps. But i think that when i use post i need to do something else inside Angular to call it using a submit on a form? But i'm not sure how or why.
If you need more information please tell me and i will post it.
Thanks.

A: I had the same problem and for me the problem was that I accidently had HttpGetAttribute.HttpGet instead of System.Web.Http.HttpGet.

A: What is the name of your method ? I find that if you don't actually have "get" in the front of the name it would throw out this error so
public string CorrectTime(string time)  < -- would fail but
public string GetCorrectTime(string time) <-- would pass

A: Because you want to export data, I guess you are trying to use this controller for the big public.  However, UmbracoAuthorizedApiController are only for back-end users as mentioned in the documentation.  You should not use these to expose data outside the Umbraco back-end.
If you want to expose data, and you want to validate against the members (not users) you should prefix your UmbracoApiController with the MemberAuthorize attribute


text:
Q: Converting char* to lower case throws segfault error
Possible Duplicate:
Is it possible to modify a string of char in C?

I have a function to convert a char* to lower case. This is the function:
void toLower(char* word, int length)
{
    int i;

    for(i = 0; i < length; i++) {
        // this I can do
        // printf("%c", tolower(word[i]));

        // this throws segfault
        word[i] = tolower(word[i]);
    }
}

When I call it like this from main, it throws a segfault error:
char* needle = "foobar";
toLower(needle, strlen(needle));

I'm certain the issue is in the assignment here:
word[i] = tolower(word[i]);

But I can't seem to figure out the corect way to do it. I tried passing it as char**, or doing *(word+i), but all cause the same issue.

A: You are attempting to change the constant string "foobar". Try:
char needle[] = "foobar";

This will create an array containing the string "foobar" (the compiler arranges to copy the data from the constant string "foobar" to your array needle where you can modify it).

A: You cannot modify a string literal. You can create a dynamic string:
char *str = strdup(needle);
toLower(str, strlen(str));
/* ... */
free(str);

A: The problem is that char *needle = "foobar" is a string literal -- it's a const char. In order the compiler to generate a writable string, use
char needle[] = "foobar";

instead.

A: Changing a string literal is not possible.
You can do this
 word[i] = tolower(word[i]);

only in two cases, either
char needle[] = "foobar";

or
first create memory for char * using malloc and then assign a string to it, like this
char * str = (char *) malloc(size0f(char)*10);
strcpy(str,"foobar");

Now you can use this


text:
Q: Can't see the last point in Highstock chart I've been working with Highstock chart and I noticed that when there are many points in the chart it's not possible to take a look at the last one using a tooltip:

You can see that the tooltip show point for 9th May, while there is another one for 10th May (as you can see in the range selector about: 24th May 2004 - 10th May 2011).
Is there a way to present to the user the last point, instead of the penultimate one ?

A: Issue was caused by dataGrouping, disable it:
plotOptions: {
    series: {
         dataGrouping: {
             enabled: false
         }
    }
}


text:
Q: Avoid blinking of script I am using a Javascript to push divs down to display a Notice. However I find it irritating that it blinks on load and that makes it not very smooth. Is there a way to work around this? I can make it smooth by adding slideDown(1000);, then there is no blinking, but I don't want it to slideDown on every page, I just want it to be displayed without blinking.
Live test
Ideas?
#fixed {
    position: relative;
}

#notice {
    background: #31384a;
    color: #FFF;
    padding: 5px;
    position: fixed;
    z-index: 999912312399;
    width: 100%;
    top: 0;
    font-weight: 400;
    font-size: 12px;
    text-align: center;
    display: none;

    .exit {
        position: absolute;
        right: 10px;
        top: 5px;
        cursor: pointer;

        img {
            width: 10px;
            height: 10px;
        }
    }
}

JS
var clearCookie = function () {
    var result = $.removeCookie('JSFiddleCookieNotification');
    if (result) {
        alert('Cookie removed, please reload.');
    } else {
        alert('Error: Cookie not deleted');
    }
};

var closeCookie = function () {
    $("#notice").slideUp(400, function(){
        $(this).remove();
    });
    $('#fixed').animate({
        top: 0
    }, 400);
    $.cookie('JSFiddleCookieNotification', 'Notified', {
        expires: 7
    });
};

// Bind the buttons
$("#clearCookie").on("click", clearCookie);
$(".exit").on("click", closeCookie);

// Now display the cookie if we need to
if ($.cookie('JSFiddleCookieNotification') == null) {
    $('#notice').show();

    var timer = setInterval(function () {
        $('navmenu, navmenu-mobile, #fixed').css({
            top: $('#notice').outerHeight()
        });
        if ($('#notice').outerHeight() == $('#fixed').css('top')) {
            clearInterval(timer);
        }
    }, 10);
}

A: I haven't gotten a chance to read all your code, but a simple fix would be to use slideDown(0)
Basically, that will slide your div down in 0 milliseconds, which would achieve the same thing as .show() but would also stop the blinking


text:
Q: Computing $\lim\limits_{x\to\infty}\ln(x)\cdot \ln(1-e^{-x})$ Limit as $x$ approaches infinity of $\ln(x)\cdot  \ln(1-e^{-x})$:
$$
\lim_{x\to\infty}\ln(x)\cdot  \ln(1-e^{-x})
$$
The only thing I can think to do is rewrite the ln(x) on the bottom as $(lnx)^{-1}$ and use L'Hôpital's rule, but I've done two iterations now and it keeps getting back to the 0/0 or infinity*0 indeterminate case. Any help on how to proceed will be much appreciated! Thanks

A: We have
$$\lim_{x\to\infty} e^{-x}  = 0 $$
We know that, $X=0$
$$\lim_{X\to 0^+}\frac{\ln(1-X)}{X} =-1 $$
By setting $X= e^{-x}$
$$ \lim_{x\to \infty}\frac{\ln(1-e^{-x})}{e^{-x}}=-1$$
Therfore,
$$\lim_{x\to \infty} \ln(x)\cdot  \ln(1-e^{-x})= \lim_{x\to \infty}\frac{\ln(1-e^{-x})}{e^{-x}}\frac{\ln(x)}{e^{x}} \\=-\lim_{x\to \infty} \frac{\ln(x)}{e^{x}}=-\lim_{x\to \infty} \frac{x}{e^{x}} \frac{\ln(x)}{x}\\=-\left(\lim_{x\to \infty} \frac{x}{e^{x}} \right)\left(\lim_{x\to \infty}\frac{\ln(x)}{x} \right)=0$$

A: We have $1-\frac{1}{t}\leq  \log t  \leq t-1$ for all $t>0$. If $ x>1$ then $t=1-e^{-x}>0$  and
$$
1-\frac{1}{1-e^{-x}}\leq  \log (1-e^{-x})  \leq -e^{-x}.
$$
Multiplying the first, the second and third members of the above  inequality by the first, second and third members of the inequality $ 1-\frac{1}{x}< \log x< x-1$, for $x>1$,
$$
\underbrace{\Big(1-\frac{1}{x}\Big)}_{(1-\frac{1}{x})\to 1 }\Big(1-\frac{1}{\underbrace{1-e^{-x}}_{(1-e^{-x})\to 0}}\Big)\leq  \log (x)\log (1-e^{-x})  \leq \underbrace{-e^{-x}(x-1)}_{-e^{-x}(x-1)\to 0}.
$$
Once
$$
\lim_{x\to \infty}\Big(1-\frac{1}{x}\Big)\Big(1-\frac{1}{e^{x}}\Big)
 =
\lim_{x\to\infty}-e^{-x}(x-1)=0
$$
we have
$$
\lim_{x\to \infty} \log (x)\log (1-e^{-x}) =0
$$

A: $\displaystyle \lim_{x\to\infty}\ln(x)\cdot  \ln(1-e^{-x})$
$\ln(x)\cdot  \ln(1-e^{-x})=-\ln x\cdot e^{-x}\cdot\dfrac{\ln(1-e^{-x})}{-e^{-x}}=-e^{\ln e^{-x}+\ln(\ln x)}\cdot\dfrac{\ln(1-e^{-x})}{-e^{-x}}=...$
$-e^{-x+\ln(\ln x)}\cdot\dfrac{\ln(1-e^{-x})}{-e^{-x}} =-\exp\bigg(-x+\ln x\cdot\dfrac{\ln(\ln x)}{\ln x}\bigg)\cdot\dfrac{\ln(1-e^{-x})}{-e^{-x}}=...$
$=-\exp\bigg[-x\bigg(1-\underbrace{\dfrac{\ln x}{x}}_{0}\cdot\underbrace{\dfrac{\ln(\ln x)}{\ln x}}_{0}\bigg)\bigg]\cdot\underbrace{\dfrac{\ln(1-e^{-x})}{-e^{-x}}}_{1}\underset{x\to +\infty}{\longrightarrow}0$


text:
Q: How to use numpy to interpolate between pairs of values in a list I have lists like these:
x_data = [3, 5, 7, 8, 5, 2]
y_data = [15, 20, 22, 23, 21, 14]

I'd like to interpolate between pairs of items in the list so that instead of have list of length 6, it's length n with equally space values between each pair of items within the list. My current approach is to use a list comprehension to go through pairs in the list and np.extend an empty list with the results. Is there a better, ready-made function to do this?
My current approach:
import numpy as np

x_data = [3, 5, 7, 8, 5, 2]
y_data = [15, 20, 22, 23, 21, 14]
result_x = []
result_y = []
[result_x.extend(np.linspace(first, second, 5)) for first, second, in  zip(x_data, x_data[1:])]
[result_y.extend(np.linspace(first, second, 5)) for first, second, in  zip(y_data, y_data[1:])]
print(result_x, '\n'*2, result_y)

Out: [3.0, 3.5, 4.0, 4.5, 5.0, 5.0, 5.5, 6.0, 6.5, 7.0, 7.0, 7.25, 7.5, 7.75, 8.0, 8.0, 7.25, 6.5, 5.75, 5.0, 5.0, 4.25, 3.5, 2.75, 2.0]

[15.0, 16.25, 17.5, 18.75, 20.0, 20.0, 20.5, 21.0, 21.5, 22.0, 22.0, 22.25, 22.5, 22.75, 23.0, 23.0, 22.5, 22.0, 21.5, 21.0, 21.0, 19.25, 17.5, 15.75, 14.0]

A: I think this function does what you want using np.interp:
import numpy as np

def interpolate_vector(data, factor):
    n = len(data)
    # X interpolation points. For factor=4, it is [0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, ...]
    x = np.linspace(0, n - 1, (n - 1) * factor + 1)
    # Alternatively:
    # x = np.arange((n - 1) * factor + 1) / factor
    # X data points: [0, 1, 2, ...]
    xp = np.arange(n)
    # Interpolate
    return np.interp(x, xp, np.asarray(data))

Example:
x_data = [3, 5, 7, 8, 5, 2]
y_data = [15, 20, 22, 23, 21, 14]

print(interpolate_vector(x_data, 4))
# [3.   3.5  4.   4.5  5.   5.5  6.   6.5  7.   7.25 7.5  7.75 8.   7.25
#  6.5  5.75 5.   4.25 3.5  2.75 2.  ]
print(interpolate_vector(y_data, 4))
# [15.   16.25 17.5  18.75 20.   20.5  21.   21.5  22.   22.25 22.5  22.75
#  23.   22.5  22.   21.5  21.   19.25 17.5  15.75 14.  ]

A: Scipy has an interpolation functions that will easily handle this type of approach. You just provide your current data and the new "x" values that the interpolated data will be based on.
from scipy import interpolate

x_data = [3, 5, 7, 8, 5, 2]
y_data = [15, 20, 22, 23, 21, 14]
t1 = np.linspace(0, 1, len(x_data))
t2 = np.linspace(0, 1, len(y_data))

n = 50
t_new = np.linspace(0, 1, n)

f = interpolate.interp1d(t1, x_data)
x_new = f(t_new)

f = interpolate.interp1d(t2, y_data)
y_new = f(t_new)


text:
Q: Convert number string to weekday How do I convert 20230102 to Monday?
Using python, I need to accomplish this. I have a column of numbers in the format yyyymmdd.

A: Parse with strptime and format with strftime:
>>> from datetime import datetime
>>> n = 20230102
>>> datetime.strptime(str(n), "%Y%m%d").strftime("%A")
'Monday'

See strftime() and strptime() Format Codes for documentation of the % strings.

A: You can do it with weekday() method.
from datetime import date import calendar
my_date = date.today()
calendar.day_name[my_date.weekday()]  #Friday

A: You can convert number string into weekday using "datetime" module
import datetime

def get_weekday(date):
    date = datetime.datetime.strptime(date, '%Y%m%d')
    return date.strftime('%A')

print(get_weekday('20230102'))

This is how you can achieve your desired output.


text:
Q: HierarchyRequestError: Failed to execute 'appendChild' on 'Node' Can someone please explain why the following error occurs and what it means?
Uncaught HierarchyRequestError: Failed to execute 'appendChild' on 'Node': Nodes of type 'HTML' may not be inserted inside nodes of type '#document'.
I am not sure I am able to reproduce it but it occurs from time to time, usually when I import more than one custom element. It's extremely hard giving exact context since I am working on integrating polymer's paper/core elements for our in-house widgets. Broadly speaking what are the cases that I would see this error? Any kind of help is appreciated. Thank you.
Regards,
Raja

A: I just had this same issue. I was importing core-icon.html
I discovered that the core-meta.html also includes the polymer.html, which I had already imported. I commented out the line in the core-meta.html that imported the polymer.html and my errors went away.


text:
Q: How do I implement a custom protocol in Twisted? I'm trying to implement a simple ZeroMQ socket server as a twisted protocol, my code is currently:
# Setup
context = zmq.Context()
socket = context.socket(zmq.PULL)
socket.bind("tcp://127.0.0.1:5558")
poller = zmq.Poller()
poller.register(socket, zmq.POLLIN)

# Respond to incoming messages.
while True:
    if poller.poll(1000):
        msg = self.socket.recv()
        print "Received a message ..."
    else:
        time.sleep(0.05)

and I found:
http://twistedmatrix.com/documents/11.0.0/api/twisted.internet.interfaces.IProtocol.html
but honestly can't make sense of it. Where should I be putting the code for setup, and code for receiving data?


text:
Q: Binary Sparse Coding In this binary sparse coding paper referenced in the Goodfellow/Bengio/Courville deep learning book (https://fias.uni-frankfurt.de/~bornschein/papers/HennigesEtAl_lva2010.pdf), the parameter $\pi=p(s_i = 1)$ is learned and not a hyperparameter. As far as I can tell, there is no prior imposed on the value of $\pi$.
My question is: why should we expect the learned codes to be sparse? Couldn't $\pi$ be quite large? I suppose if $\pi$ is close to 1.0, then we can consider the "opposite" code as being sparse. But I don't see why $\pi$ shouldn't want to be 0.50.
If there is no reason to expect that $\pi$ is far from 0.50, I don't understand why the scheme is called Binary Sparse Coding or why the codes values are mostly 0s or mostly 1s.
Thank you humans of CrossValidated for your help!

A: After corresponding with the author, I think I understand.
If we have too many hidden units, it is entirely possible that the weights for these units may be used to model noise in the dataset.
However, even when we recover exactly the correct factors, and if the code values are real numbers instead of binary values, then the best code $h$ for a visible $v$ will use some small values on the irrelevant factors for this example, simply because every factor has probability $1.0$ of being not-orthogonal to the noise in $v$.
The binary sparse coding technique avoids this simply by disallowing you from using "small" scalings of factors. Every factor is either in or out.

A: Binary Sparse Coding is just the method itself.
This method has to be trained, for instance using variational EM.
The model selection prior $p(s|\Theta) = \prod_h^H (\pi_h)^{s_h} (1-\pi_h)^{1-s_h}$ however does imply a specific sparsity due to the selection of the design variables $x$ (if given) and coefficients $w_{ij}$.
In fact, minimizing the Kullback-Leibler divergence between the approximated posterior $q(\theta)$ and the real posterior $p(s|y,\Theta)$ does imply minimizing the noise deviation, which is $||y-Ws||^2_2$ for Gaussian noise.
But the sparsity, assuming the noise distribution hits the real noise good enough, does come from the selection prior and the data itself. The model is forced to describe as many data $y$ as possible by turning on and off the right components and refining them in order to the data.
The more independent those components are from each other, the better the model can do so and the higher the approximate posterior probability for specific units given a specific data point $y^{(n)}$. (see https://www.researchgate.net/publication/220848054_Binary_Sparse_Coding)
The less the true noise differs from a Gaussian the the less the weights should look like noise, except if the data is too occupied. For example, if knowing the exact causes and generating target data $y$ with around $\bar\pi=0.5$ or higher, the number of latents must be higher than the exact number of causes. And even than it might be too occupied to extract the right causes, especially with very less data.
For example, in regression problems, the design variable intersection per data point might be very high, but the true causes are considered sparse (see https://arxiv.org/abs/1904.07150).


text:
Q: heroku node js + socket.io listion to POST from php I have a node.js server at heroku side this is working on localhost but when I commit to heroku the app crash.
   'use strict';

const express = require('express');
const socketIO = require('socket.io');
const path = require('path');

const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, 'index.html');

const server = express()
  .use((req, res) => res.sendFile(INDEX) )
  .listen(PORT, () => console.log(`Listening on ${ PORT }`));

const io = socketIO(server);

io.on('connection', (socket) => {
  console.log('Client connected');
  socket.on('disconnect', () => console.log('Client disconnected'));
});

var app = express();
app.set('port', (process.env.PORT || 5000));

var bodyParser = require('body-parser')
app.use(bodyParser.json());

app.use(express.static(__dirname + '/public'))

app.post('/phpcallback', function(req, res) {
    var content = req.body;
    console.log('message received from php: ' + content.msg);
    //to-do: forward the message to the connected nodes.
    res.end(content.msg);
});

app.listen(app.get('port'), function() {
  console.log("Node app is running at localhost:" + app.get('port'))
});

the app need to listen for POST request that come form my PHP server and then emit to user at js client side, the code working good when its on localhost but when I commit and push to heroku the app crashed.
anyone know what is the problem? i am new to node.js and socket.io

A: Try like this (improved syntax and general usage)
var express = require('express');
var io = require('socket.io');
var path = require('path');
var PORT = process.env.PORT || 3000;
var INDEX = path.join(__dirname, 'index.html');
// Express & Middleware
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
// SocketIO
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){
  console.log('[socket]','client connected');
  socket.on('disconnect', () => console.log('[socket]','client disconnected'));
});
server.listen(PORT,function(){console.log("[SERVER] listening @ port:",PORT);});
//GET Routes
app.use(express.static(__dirname + '/public'))
app.get('/',function(req,res){res.sendFile(INDEX);});
//POST Routes
app.post('/phpcallback', function(req, res) {
  var content = req.body;
  if(content.msg){
    console.log('message received from php: ' + content.msg); //
    /*to-do: forward the message to the connected nodes.*/
    //io.sockets.on('forward',content.msg); //forward to ALL connected socket.io clients (requires client-side event)
    res.send(content.msg); //forward response with content.msg to the original request (only) of this route.
  }else{console.log('[/phpcallback] : missing message (content.msg)');}
});

I saw your to-do, if you want to forward a message to all connected clients socket.io  :
io.sockets.emit('your_event','your_message');

This might come handy : SocketIO Emit CheatSheet


text:
Q: Mysql and Php (Could not enter data) I need assistance to what might be causing this error, I get this error everytime I have single inverted commas in the data I am trying to post to the database. (field name: abtext)
Error message:

Could not enter data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dsf'ds'f'ds'fds'f'ds'f'dsfds','fdsfdd'fd''fds'f'dsf'ds fd 'fds'f'df'ds ds''fd'sf' at line 3

Data entered to the db:
field name: (abtext) of type text
data entered on the field=
fdsfdd'fd''fds'f'dsf'ds fd 'fds'f'df'ds ds''fd'sf'ds'f'dsfds'sdf
My insert code:
 $sql = "INSERT INTO poster
          (titlepaper,abtext,authorTitle1,authorName1,authorIntials1,authorSurname1,authorJt1,authorcompany1,authoremail1,authornumber1,presenting1,additional,authorTitle2,authorName2,authorIntials2,authorSurname2,authorJt2,authorcompany2,authoremail2,authornumber2,presenting2,correspondence,authorTitle3,authorName3,authorIntials3,authorSurname3,authorJt3,authorcompany3,authoremail3,authornumber3,presenting3)

          VALUES ('$titlepaper','$abtext','$authorTitle1','$authorName1','$authorIntials1','$authorSurname1','$authorJt1','$authorcompany1','$authoremail1','$authornumber1','$presenting1','$additional','$authorTitle2','$authorName2','$authorIntials2','$authorSurname2','$authorJt2','$authorcompany2','$authoremail2','$authornumber2','$presenting2','$correspondence','$authorTitle3','$authorName3','$authorIntials3','$authorSurname3','$authorJt3','$authorcompany3','$authoremail3','$authornumber3','$presenting3')";

       mysql_select_db('database');
       $retval = mysql_query( $sql, $conn );

       if(! $retval ) {
          die('Could not enter data: ' . mysql_error());
       }

Your help will be much appreciated.

A: Please try by using : htmlspecialchars($abtext)
so your sql query would be like,
$sql = "INSERT INTO poster
        (titlepaper,abtext,authorTitle1,authorName1,authorIntials1,authorSurname1,
        authorJt1,authorcompany1,authoremail1,authornumber1,presenting1,additional,
        authorTitle2,authorName2,authorIntials2,authorSurname2,authorJt2,authorcompany2,
        authoremail2,authornumber2,presenting2,correspondence,authorTitle3,authorName3,
        authorIntials3,authorSurname3,authorJt3,authorcompany3,authoremail3,
        authornumber3,presenting3)

        VALUES
        ('$titlepaper','htmlspecialchars($abtext)','$authorTitle1','$authorName1',
        '$authorIntials1','$authorSurname1','$authorJt1','$authorcompany1',
        '$authoremail1','$authornumber1','$presenting1','$additional',
        '$authorTitle2','$authorName2','$authorIntials2','$authorSurname2',
        '$authorJt2','$authorcompany2','$authoremail2','$authornumber2',
        '$presenting2','$correspondence','$authorTitle3','$authorName3',
        '$authorIntials3','$authorSurname3','$authorJt3','$authorcompany3',
        '$authoremail3','$authornumber3','$presenting3')";

A: Try this,
$sql = " INSERT INTO poster(titlepaper,abtext,authorTitle1,authorName1,authorIntials1,authorSurname1,authorJt1,authorcompany1,authoremail1,authornumber1,presenting1,additional,authorTitle2,authorName2,authorIntials2,authorSurname2,authorJt2,authorcompany2,authoremail2,authornumber2,presenting2,correspondence,authorTitle3,authorName3,authorIntials3,authorSurname3,authorJt3,authorcompany3,authoremail3,authornumber3,presenting3)
                     VALUES (" . mysql_escape_string($titlepaper) . "," . mysql_escape_string($abtext) . "," . mysql_escape_string($authorTitle1) . "," . mysql_escape_string($authorName1) . "," . mysql_escape_string($authorIntials1) . "," . mysql_escape_string($authorSurname1) . "," . mysql_escape_string($authorJt1) . "," . mysql_escape_string($authorcompany1) . "," . mysql_escape_string($authoremail1) . "," . mysql_escape_string($authornumber1) . "," . mysql_escape_string($presenting1) . "," . mysql_escape_string($additional) . "," . mysql_escape_string($authorTitle2) . "," . mysql_escape_string($authorName2) . "," . mysql_escape_string($authorIntials2) . "," . mysql_escape_string($authorSurname2) . "," . mysql_escape_string($authorJt2) . "," . mysql_escape_string($authorcompany2) . "," . mysql_escape_string($authoremail2) . "," . mysql_escape_string($authornumber2) . "," . mysql_escape_string($presenting2) . "," . mysql_escape_string($correspondence) . "," . mysql_escape_string($authorTitle3) . "," . mysql_escape_string($authorName3) . "," . mysql_escape_string($authorIntials3) . "," . mysql_escape_string($authorSurname3) . "," . mysql_escape_string($authorJt3) . "," . mysql_escape_string($authorcompany3) . "," . mysql_escape_string($authoremail3) . "," . mysql_escape_string($authornumber3) . "," . mysql_escape_string($presenting3) . ")";

            mysql_select_db('database');
            $retval = mysql_query($sql, $conn);

            if (!$retval) {
                die('Could not enter data: ' . mysql_error());
            }

i have changed insert area of insert command. Used mysql_escape_string for escaping unwanted char in insertion variables.

A: $sql = "INSERT INTO individual
          (titlepaper,abtext,authorTitle1,authorName1,authorIntials1,authorSurname1,authorJt1,authorcompany1,authoremail1,authornumber1,presenting1,additional)

          VALUES ('". mysql_escape_string($titlepaper) . "','" . mysql_escape_string($abtext) . "','" . mysql_escape_string($authorTitle1) . "','" . mysql_escape_string($authorName1) . "','" . mysql_escape_string($authorIntials1) . "','" . mysql_escape_string($authorSurname1) . "','" . mysql_escape_string($authorJt1) . "','" . mysql_escape_string($authorcompany1) ."','" . mysql_escape_string($authoremail1) . "','" . mysql_escape_string($authornumber1) . "','" . mysql_escape_string($presenting1) . "','" . mysql_escape_string($additional) . "')";

This worked! THANKS ALOT GUYS MUCH APPRECIATED!


text:
Q: JQuery ajax issue - PHP script not getting passed values I have an ajax call given below:
var fd = new FormData();
fd.append('aid', qid);
require(['jquery'], function($) {
    $.ajax({
        type: 'POST',
        url: M.cfg.wwwroot + '/mod/quiz/accessrule/mictest/miccheck.php',
        data: fd,
        processData: false,
        contentType: false
    }).done(function(data) {
        console.log(data);
    });
});

I am passing a value (int) to the miccheck.php script where I want to set a session variable using that value. Here is my PHP script (miccheck.php)
$aid = $_POST['aid'];
$SESSION->miccheckpassed[$aid] = 1;

But somehow, the session variable is not set. Any help, please.

A: Note sure if you want this
$aid = $_POST['aid'];
$_SESSION['miccheckpassed'][$aid] = 1;

A: Start session on the top of your php script and then assign the data to the variable in session. Something like below might help
<?php
session_start();
$aid = $_POST['aid'];
$_SESSION['miccheckpassed'][$aid] = 1;
?>

Hope it helps !


text:
Q: F# type provider in DAL I have a solution with 2 projects:

*

*F# project (for data access + other functions)

*C# web project (will consume functions from F#)

I will need to call SQL Server stored procedures, and am using FSharp.Data.TypeProviders to do the job.
type dbSchema = SqlDataConnection< "...", StoredProcedures=true >

let getDbContext connString =
    match connString with
    | "" -> dbSchema.GetDataContext()
    | _ -> dbSchema.GetDataContext(connString)

Insert:
let insertItem dbContext item =
    dbContext.CreateStoredProc(item.Stuff)

Or
let insertItem connString item =
    use dbContext = getDbContext connString
    dbContext.CreateStoredProc(item.Stuff)

*

*How would I consume this from C# so that I'm not recreating the dbContext every time I do an insert?

I don't want to expose the whole dbContext, just certain stored procedures through the F# DAL.
Note: I need to pass in the connection string from web.config
Similar question here doesn't fully provide my answer: F# Type Provider for SQL in a class

*I am able to set getDbContext to internal or private, but not dbSchema. Any way to not expose it?

A: If I'm not much mistaken, the context is an instance of DataContext, which isn't thread-safe. None of the data context base classes I'm aware of, are thread-safe, and since you want to use them in a web application, you must, at the very least, create one instance per HTTP request; otherwise, the behaviour of your DAL will be defective.
On the other hand, within a single request, it may be worthwhile to reuse the same instance. Therefore, I'd go with this function design:
let insertItem dbContext item =
    dbContext.CreateStoredProc(item.Stuff)

because that would let you create a single dbContext value associated with a single HTTP request, and reuse that for multiple database operations, because you can pass the same dbContext value to more than one function.
If you want to make all of this accessible from C#, it would be easiest on C# client developers if you wrap all the functionality in classes. Assuming the above insertItem function, as well as the getDbContext function from the OP, you could define classes like these:
type ContextShim internal(ctx) =
    member x.InsertItem(item : Item) =
        insertItem ctx item

type ContextFactory() =
    member x.CreateContext connectionString =
        let ctx = getDbContext connectionString
        ContextShim ctx

This will enable a C# client to use a Singleton instance of the ContextFactory class, and for each HTTP request, use its CreateContext method to create an instance of the ContextShim class, and then use the members on the ContextShim instance, such as InsertItem.


text:
Q: Google Calendar API conditionals I am trying to build conditions on a events-list query, but cant find documentation for specifying OR conditions, in this example:
gapi.client.calendar.events.list({
            'calendarId': 'primary',
            //'timeMin': from_datetime,     // pick everything since we already search by sharedExtendedProperty
            'showDeleted': false,
            'singleEvents': true,
            'maxResults': 100,
            'orderBy': 'startTime',
            **'sharedExtendedProperty': ['createdBy=someapp-alpha', 'createdBy=someapp-beta', 'createdBy=someapp-prod']**
        }).then(
            function (res) {

I'd like to be able to query for stuff with sharedExtendedProperty x OR sharedExtendedProperty y OR sharedExtendedProperty z ...
Adding both 3 possibilities to an array gives me no result. So I guess it is doing some AND instead of OR. So, any clues ????
Thank you !

A: It is not possible to incorporate an OR query into the calendar.events.list request for extended properties
You need to do it manually by

*

*first making 3 separate list requests

*subsequently programmatically merge the results


text:
Q: Python to use shell from sdk I am trying to execute a few shell commands from python. I was able to do that with 'os' functions. But, the default shell /bin/sh is used for execution. I need the shell to be used from my sdk.
I tried setting the os.environ['SHELL'] = '${base_prefix}/bin/sh' where is base_prefix is path. But still the required shell is not used. Instead of /bin/sh the /usr/bin/env is used. Can anyone know how this can be resolved.
FYI... below is code snippet of my application.
#!/usr/bin/env python3
import os, sys
os.unsetenv('LD_LIBRARY_PATH')
os.environ['SHELL'] = '${base_prefix}/bin/sh'
os.execlp($CC, $@)


text:
Q: SQLPS equivalent to SqlServer Get-SqlServer? I currently am working with a script that utilizes the SQLPS module. This is due to the current version of my PowerShell, 4.0. I have a script that works on a coworkers machine, but not mine, that utilizes the SqlServer module, specifically the cmdlets Get-SqlServer and Get-SqlDatabase.
While I have seen that there is a Get-SqlDatabase cmdlet in the SQLPS module, I am unsure of what command will accomplish the same for me as the SqlServer command Get-SqlServer.
Does anyone know how I could convert the code to work with SQLPS?
@{ login = $_ ; server = Get-SqlServer $_.name $_.user $_.pass }

Hopefully that is enough information.


text:
Q: php ERR Can't get lock. Mailbox in use
I connected to mail server throw php and I had this error:
ERR Can't get lock. Mailbox in use

I can't get a new mail.
In php i use POP3 class.
How can I unlock it?
I know that is a problem with IMAP and POP3.
Thanks
***UPDATE
PHP POP 3 mail class debug:

AUTH_SASL NOT PRESENT!
DISABLING METHOD DIGEST-MD5
DISABLING METHOD CRAM-MD5
S:+OK POP3 ns13.datateks.lv 2004.89 server ready
C: CAPA
S:+OK Capability list follows:
S:TOP
S:LOGIN-DELAY 180
S:UIDL
S:STLS
S:USER
S:SASL LOGIN
S:.
C: AUTH LOGIN
S:+ VXNlciBOYW1lAA==
C: YW5kcmVqcy5hbGVrc2VqZXZzQG5wZGNvbmNlcHQuY29t
S:+ UGFzc3dvcmQA
C: bnBkMjAzMA==
S:-ERR Can't get lock.  Mailbox in use**

A: I don't think you can do anything in PHP to avoid this. Contact your mail server administrator to enable concurrent connections to a mailbox. If that is already enabled, increase the number of simultaneous connections allowed. Every mail server has different ways to achieve this.


text:
Q: Custom Django Adminsite Doesn't show link to read_only ForeignKey field In the default Django Admin Site, if a model is registered with ForegingKey fields and those are included in readonly_fields (using the model property or the get_readonly_fields method) the value of the field is rendered with a link to the Change View, but this Doesn't work on a custom Django Admin Site.
E.g.: I have this two models:
class ModelA(models.Model):
    field_a = models.CharField(max_length=50)

class ModelB(models.Model):
    model_a = models.ForeignKey(ModelA, on_delete=models.PROTECT)

I registered in the default Django Admin Site:
admin.register(ModelA)

@register(ModelB)
class ModelBAdmin(admin.ModelAdmin):
    model = ModelB
    readonly_fields = ["model_a"]

So I get in the Change View of ModelB in the Admin the value of the field (str of the model) with a link to the Change View of the related model:
Link pointing to Chang View
But if I register the models in a Custom Admin Site, the link is not generated.
How can I extends my Custom Admin Site in order to generate those links?
PD1: I know I can code custom methods to build the links, but this is not a DRY way of do it and doesn't work well with get_readonly_fields method
PD2: If I register the related model (ModelA in the example) in the default Admin Site the link is genereted, but point to the default Admin Site, brokening the purpose of the Custom Admin Site.

A: I posted the same question in the official Django forum:
forum.djangoproject.com/t/9472/5
It has generated a ticket because is a missing line in the method get_admin_url in the helper class AdminReadOnlyField, The temporal solution is in the ticket: code.djangoproject.com/ticket/33077
PD: Why someone marks this post like a bad question? It has generates a ticket for a patch so it was a fair problem, this is my first question in StackOverflow and this is discouraging.


text:
Q: Can/will AMD modules load in-between in-line script tags? For reasons that aren't relevant to the question, my coworker needs to load a script that uses the Universal Module Definition pattern. Our environment usually has an AMD tool loaded, but for more irrelevant reasons, my coworker needs the script to define a global rather than registering a module through AMD. The approach that is currently checked in on their branch is something along the lines of this:
<script>
    var backupDefine = define;
    define = null;
</script>
<script src="../path/to/some/script/using/UMD.js"></script>
<script>
    define = backupDefine;
    backupDefine = null;
</script>

My question is: Is this a horrible idea? Is there a guarantee in the way browsers load scripts from script tags that will ensure nothing other than loading the UMD-based script will happen between undefining define and restoring define? We have a very large, very heavily async asset load primarily based around AMD modules, so what I am concerned with is an AMD module attempting to define itself in that intermittent state where define is currently not defined.

A: So long as UMD.js in no way modifies the scripts in the DOM, those scripts are guaranteed to execute in the order that they're authored in before any asynchronous callbacks that may have been queued before the first script executes.

A: I see this as a bad idea and spec breaking even if the case where define is always necessary is rare or even non-existent due to <script> load order considering your case.  In an AMD environment, define, require and the like should basically be treated as first class keywords since their goal is to help you to remove globals.
Realistically, you're treading into undefined behavior as far as I can tell and writing code that is hard to maintain.  You're relying on a tricky case with a spec where you have to undefine something and them immediately redefine it hoping that nothing tried to use it in the mean time.  I'd say that that's "unsafe".
If you really need this to happen, I'd comment and document it heavily to make sure a future developer doesn't misunderstand what you're doing.  However, I would say the better course of action is to rewrite the UMD.js file so that you export your global your own way.  Rhetorically, why are you trying to use UMD if you don't want it to UMD things?
You're writing this module to support AMD through UMD but then you say that you don't want it to be used by AMD.  Rewrite the file to just export to the global and avoid messing with define before you accidentally conflict with an additional library that does something tricky with define.


text:
Q: Is it possible to get the min and max value with jQuery and render it in another input-field? and there we go:
I'm writing(in fact it feels more like trying and asking) a GreaseMonkey script using JS and including(also using) the jQuery library.
I created another table with some input-fields. Example(input-fields-only):

  + "   < input type=\" text\"  id=\" field_1\"  name=\" min_field_1\" value=\"\" > "
  + "   < input type=\" text\"  id=\" field_2\"  name=\" min_field_2\" value=\"\" > "
  + "   < input type=\" text\"  id=\" field_3\"  name=\" min_field_3\" value=\"\" > "
  + "   < input type=\" text\"  id=\" field_4\"  name=\" min_field_4\" value=\"\" > "

//the results should be rendered in another input-field in this case with id=field_result_min
  + "   < input type=\" text\" id=\" field_result_min\"  name=\"min_field_result\" > "
  + "   < input type=\" text\"  id=\" field_result_max\"  name=\"max_field_result\" > "
  + "   < input type=\" text\"  id=\" field_result_avg\"  name=\"avg_field_result\" > "

My aim was to get the min,max and avg value of the fields and render the result in another input field.
//returns the min value of all input-fields starting with a name attribute of min
$("input[name^='min']")min.()
This won't work because jQuery library hasn't any min() or max() like JS does.
So i tried sth. like

<input type="button" value="=" onclick="window.document.getElementById('field_result_min') = Math.min(window.document.getElementById('field_1').value,window.document.getElementById('field_2').value,window.document.getElementById('field_3').value,window.document.getElementById('field_4').value)" >
Hm...again I'm stuck and confused - even while I'm explaining.
Question: How do I render the min and max values of some input-fields in one input-field?
Any help would be much appreciated.
Faili
Update
This is how I solved it. Well, it's just the avg and max(min is deriveable). Any changes or mistakes should come from copy and paste and changing sth. in the code here at stackoverflow.
function calculateAvg() {

var fields = [1*window.document.getElementById('field_1').value,1*window.document.getElementById('field2').value,1*window.document.getElementById('field').value,1*window.document.getElementById('field4').value,1*window.document.getElementById('field5').value,1*window.document.getElementById('field6').value];

var total = 0;
var anzahl = 0;
    for(i=0;i 0) {
    total = total+fields[i];
    anzahl=anzahl+1;}
}
var durchschnitt = (total/anzahl);
window.document.getElementById('fieldavg').value = durchschnitt;
}
 $("#fieldavg").blur  (calculateAvg);

function calculateMax() {

var fields = [1*window.document.getElementById('field_1').value,1*window.document.getElementById('field2').value,1*window.document.getElementById('field').value,1*window.document.getElementById('field4').value,1*window.document.getElementById('field5').value,1*window.document.getElementById('field6').value];

var max = 0;
for(i=0; i<fields.length;i++) {
      if(fields[i] > 0) {
if(max<fields[i]){
max=fields[i];
}
}
}
window.document.getElementById('field8').value = max;

}

$("'field_8").blur  (calculateMax);

So, thanks for your help. I really needed it.
Faili

A: Using other libraries are one way to do it, but solving small issues by hand [using raw javascript] would be nicer.
Change the selectors in code below, and check.
$(function(){
    sel = $('.field')
    min_field = $('#min');
    max_field = $('#max');
    avg_field = $('#avg');
    len = $(sel).length;
    $(sel).change(function(){
       sum = 0
       list = []
       $(sel).each(function(){
            val = Number($(this).val());
            list.push(val);
            sum += val;
        })
        $(min_field).val(Math.min.apply(Math, list))
        $(max_field).val(Math.max.apply(Math, list))
        $(avg_field).val(sum/len);
     })
})

Happy Coding.

A: Try this:
var values = $("input[name^='min']").map(function(){
    return isNaN(this.value) ? [] : +this.value;
}).get();

var min = Math.min.apply(null, values);

And then you can add that min-value to another input field like so:
$('#some-input').val(min);

A: You might want to have a look at the jQuery Calculation Plug-in. The syntax for your example would be something like this:
var MinVal=$("input[name^='min_field_']").min(); // Get minimum value
var MaxVal=$("input[name^='min_field_']").max(); // Get maximum value
var AvgVal=$("input[name^='min_field_']").avg(); // Get average value

This would be faster version:
var MinField=$("input[name^='min_field_']");

var MinVal=MinField.min(); // Get minimum value
var MaxVal=MinField.max(); // Get maximum value
var AvgVal=MinField.avg(); // Get average value

A: Without plugins you can do something like this JSFiddle
Array.prototype.avg = function() {
    var total = this[0];
    var len = this.length;
    for (var i = 1; i != len; i++) total += this[i];
    return total / len;
}

Array.prototype.min = function() {
    return Math.min.apply( Math, this );
}

Array.prototype.max = function() {
    return Math.max.apply( Math, this );
}

var values = $("input[id^='field_']").map(function(){
    var value = $(this).val();
    if (value.length == 0 || isNaN(value)) {
        return 0;
    } else {
        return parseFloat(value);
    }
}).get();

$('#max').val(values.max());
$('#min').val(values.min());
$('#avg').val(values.avg());

The prototypes should help in future.


text:
Q: Generate coordinate grid Is there some way to generate a 2D matrix of the following form:
(0, 0)  (1, 0)  (2, 0)  (3, 0)
(0, 1)  (1, 1)  (2, 1)  (3, 1)
(0, 2)  (1, 2)  (2, 2)  (3, 2)
(0, 3)  (1, 3)  (2, 3)  (3, 3)

In index notation, this matrix may be expressed as grid[j, i] = (i, j).
A trivial way to generate this grid would be:
auto grid = cv::Mat(height, width, CV_32FC2);

for (auto j = 0; j < height; j++) {
    for (auto i = 0; i < width; i++) {
        grid.at<cv::Vec2f>(j, i) = cv::Point2f(i, j);
    }
}

But this is slow, "manual", and a little ugly.
In Python, one can do this:
grid = np.dstack(np.meshgrid(np.arange(width), np.arange(height)))

What would be a good way to generate this grid of type cv::Mat in C++?

A: The C++ loop is likely as fast as the hardware allows. I assume the implicit static_cast<float>(j) is moved outside the inner loop by the optimizer.
C++ does have std::iota but it only works on a 1D container.
The OpenCV approach would be cv::Mat::forEach, which will call your function passing an int position[2] for each pixel. The OpenCV docs have a 3D example but it's almost the same as what you're trying to do.

A: It's going to be faster if you don't use the at() method as it is the slowest way of accessing individual values (see the docs for Mat). Use ptr() instead:
for (auto j = 0; j < height; j++) {

    cv::Vec2f* row = grid.ptr<cv::Vec2f>(j);

    for (auto i = 0; i < width; i++) {
        row[i] = cv::Point2f(i, j);
    }
}

or pointer to data member:
cv::Vec2f* arr = (cv::Vec2f*) grid.data;

for (auto j = 0; j < width; j++) {
    for (auto i = 0; i < height; i++) {
        arr[grid.cols*i + j] = cv::Point2f(i, j);
    }
}


text:
Q: Is there a way to merge two iterators into the same Vec? I have two iterators that will end up merging into the same Vec, but I need to perform a filter on the end result before merging. For example:
let a = vec![1, 2, 3, 4].into_iter().map(|x| x * 2);
let b = vec![0, 3, 5, 6, 7].into_iter().map(|x| x * 3);
let c = a + b;
assert_eq!(
    c.filter(|&x| x > 5).collect::<Vec<u8>>(),
    vec![6, 8, 9, 15, 18, 21]
);

I could do something like this:
let mut a = vec![1, 2, 3, 4]
    .into_iter()
    .map(|x| x * 2)
    .collect::<Vec<u8>>();
let b = vec![0, 3, 5, 6, 7]
    .into_iter()
    .map(|x| x * 3)
    .collect::<Vec<u8>>();
a.extend(b);
assert_eq!(
    a.into_iter().filter(|&x| x > 5).collect::<Vec<u8>>(),
    vec![6, 8, 9, 15, 18, 21]
);

But the extra allocation kills performance in my case (yes, I checked!)

A: You're looking for Iterator::chain
let a = vec![1, 2, 3, 4].into_iter().map(|x| x * 2);
let b = vec![0, 3, 5, 6, 7].into_iter().map(|x| x * 3);
let c = a.chain(b);
assert_eq!(
    c.filter(|&x| x > 5).collect::<Vec<u8>>(),
    vec![6, 8, 9, 15, 18, 21]
);

A: Alternatively, you could use extend():
let a = (0..500000).map(|x| x * 2);
let b = (0..500000).map(|x| x * 3);
let mut c = Vec::with_capacity(a.size_hint().1.unwrap() + b.size_hint().1.unwrap());
c.extend(a);
c.extend(b);

This requires you to explicitly use with_capacity() which chain() and collect() would have done for you. In the situation where a is already constructed, extend() will be suitable instead of building a temporary vector.
I did not find any benchmark difference between chain() and extend in this case (benchmark)

This doesn't do the filtering. Sebastian Redl

Correct! Fixing this mistake shows that for some reason, LLVM no longer optimizes chain() now. The version with extend:
let a = (0..500000).map(|x| x * 2);
let b = (0..500000).map(|x| x * 3);
let mut c = Vec::with_capacity(a.size_hint().1.unwrap() + b.size_hint().1.unwrap());
c.extend(a.filter(|&x| x > 5));
c.extend(b.filter(|&x| x > 5));

Is twice as fast than the version with chain (this is what I expected in the first place):
let a = (0..500000).map(|x| x * 2);
let b = (0..500000).map(|x| x * 3);
let _c: Vec<_> = a.chain(b).filter(|&x| x > 5).collect();

(benchmark)


text:
Q: Bot Framework needs to be hosted? Do I need to have my Bot hosted in order to use the embeded web chat option using the Iframe?
(https://webchat.botframework.com/embed/botme?s=YOUR_SECRET_HERE'>)
I want to do some test with localhost and with the embed. Mostly a demo.
Best Regards.

A: If your bot is already registered in Dev Portal and endpoint is already set then the easiest way to test embed chat locally without having any web-site is just to paste this as URL on any browser:
data:text/html, <iframe style="height:480px; width:400px" src='https://webchat.botframework.com/embed/botme?s=YOUR_SECRET_HERE'></iframe>

Yeah, it may look strange, but just paste it on browser's address bar and press Enter. Another quick option is to put that iframe to your bot's default.htm page (which should exist on your solution) so when you run the project and browser is automatically open, you'll see the embed chat control there.

A: The easiest way to demo is to use the bot framework emulator, https://github.com/Microsoft/BotFramework-Emulator , you can run your bot code locally.
The emulator looks the same as the web chat.
If you want to use the real web chat you can still host your bot locally but provide a temporary public url to the bot framework using ngrok. So, you should register your bot and set the bot url obtained form ngrok, get the secret and use it in your web chat.
As alternative you can use one of free hosting to host your bot code. For NodeJs version you can use services like heroku.


text:
Q: tuning regularization parameter in backpropagation, by using backpropagation When doing regularization in neural network backpropagation you have to set a reasonable value for the regularization parameter (e.g. if doing L2 regularization). I'm used to: 1. people either just setting the value constant at somewhat resonable value based on what other people say and not actually tuning it, or 2. saying that you could tune the hyperparameter using crossvalidation by doing a grid search on that parameter space and taking the argmin.
But then I read somewhere that you could tune the regularization parameter at the same time in backpropagation by calculate the gradient of the cost wrt the regularization parameter, just like any weight? But wouldn't tuning this way always drive the regularization parameter to 0 because for any positive value of the reg. parameter, it  will increase the cost function (since if using L2 or L1 norm regularization the term for the weights wil always be positive)?

A: My guess is that you would backpropagate only the error term for purposes of calculating the loss w.r.t. lambda, so that lambda will go up if larger weights would lead to higher error, etc. The weights themselves would still be shrunk as usual.


text:
Q: Unitary gauge for non-abelian case I'm reading Chapter 19 of Mandle and Shaw's Quantum field theory. In the first section it is explained that one can go with a $SU(2)$ followed by a $U(1)$ transformation from
$$\begin{bmatrix}\eta_1(x) + i\eta_2(x) \\ v + \sigma(x) +i \eta_3(x)\end{bmatrix} $$ to the state
$$\begin{bmatrix} 0 \\ v + \sigma(x)\end{bmatrix}. $$
I tried to first make of this first vector a 'down isospin' by multiplying with an generic element of $SU(2)$
$$\begin{bmatrix} \alpha & \beta \\ -\beta^\star &\alpha^\star\end{bmatrix}, $$
where $|a|^2+|b|^2=1$.
Is there maybe a easier representation if $SU(2)$ which leads to the correct needed transformation?
($v$ is part of $\sigma$ and may be absorbed in it.)

A: There most definitely is, and your text should have used it in defining the unitary gauge more conventionally: the SU(2) group element parameterization of physics, that is the rotation matrix for spinors R. Absorb v into the definition of σ, where it belongs and from where it can re-emerge at will.
$$R=\exp (i\theta ~\hat{n}\cdot\vec{\sigma})=I \cos \theta +i  \hat{n}\cdot\vec{\sigma} \sin \theta= \begin{bmatrix}\cos \theta +i n_3\sin \theta & i\sin \theta (n_1-in_2) \\ i\sin \theta (n_1+in_2) &\cos \theta -i n_3\sin \theta \end{bmatrix}. $$
It is easier, and, in fact, more conventional, to work backwards,
$$R \begin{bmatrix} 0 \\ \rho\end{bmatrix}  =\rho   \begin{bmatrix} \sin\theta (i n_1+n_2) \\ \cos\theta -in_3\sin \theta\end{bmatrix}  =
 \begin{bmatrix}\eta_1(x) + i\eta_2(x) \\ \sigma(x) +i \eta_3(x)\end{bmatrix}, $$
that is
$$
\eta_1=n_2 \rho \sin \theta, \quad
\eta_2=n_1 \rho \sin \theta, \quad
\eta_3=-n_3 \rho \sin \theta, \quad
\sigma= \rho \cos \theta,
$$
so that $~\rho^2=\sigma^2+\vec{\eta}^2~$ and  $~\vec{\eta}^2=\rho^2 \sin^2\theta=\sigma^2 \tan^2\theta~$ .
So, then, $R^{-1}$ is the SU(2) group element you were seeking, with $\theta$ and $\hat{n}$ solvable as above. Note $\rho \neq\sigma$, as you sought, as easy to confirm from the modulus/length of the two respective spinors! Your text was being slightly "metaphorical".
Also, the sign of $\eta_3$ in your expression is unconventional: most texts prefer a minus sign, and interchange the roles of $\eta_1$ and $\eta_2$, to (only) then have
$$
(I \sigma+i~\vec{\eta}\cdot\vec{\sigma}) \begin{bmatrix} 0 \\ 1\end{bmatrix} .
$$


text:
Q: How can i use multiple path parameters from serverless framework I'm trying to deploy my serverless app.
But have a problem like below.
An error occurred: ApiGatewayResourceServicesServiceidVar - A sibling ({id}) of this resource already has a variable path part -- only one is allowed

And below is my code.
updateApplication:
    handler: handler.updateApplication
    memorySize: 3008
    description: Update application
    timeout: 30
    events:
      - http:
          path: services/{serviceId}/applications/{applicationId}
          method: post
          cors: true
          authorizer: authorize
          request:
            parameters:
              paths:
                serviceId: true
                applicationId: true

Any advice or suggestion would be appreciated. Thank you in advance.

A: The serverless framework seems to be complaining that you have defined the path parameters twice. Since you have declared it there directly below -http: you can remove the request: parameters: paths: block.
In other words, try this:
updateApplication:
    handler: handler.updateApplication
    memorySize: 3008
    description: Update application
    timeout: 30
    events:
      - http:
          path: services/{serviceId}/applications/{applicationId}
          method: post
          cors: true
          authorizer: authorize

Happy coding!


text:
Q: Angular: injecting state into controllers (or binding "models" to controllers) Suppose I have a general purpose controller, TableController, that can be used in multiple places in the app to display a table of Key x Value pairs via a custom directive, ui-table, that generates an HTML table.
angular.module('ui.table', [])
  .controller('TableController', ['$scope', 'data',
     function($scope, data) { $scope.data = data; }])
  .directive('uiTable', function() {
    return { templateUrl: 'table.html' }
});

I could use the controller in the following template:

And create a factory to pass data to this controller.
.factory('data', function() {
  return [{'App':'Demo', 'Version':'0.0.1'}];
})

But, I have multiple controllers (sometimes in the same views), so I need to "bind" a particular factory to a particular controller (e.g., UserProfile, AppData, etc.)
I have started to look at angular-ui-router's $stateProvider, but it seems too complicated for what must be a typical use case? What I'd really like to be able to do is use the template to annotate which factory (what I think of as a model) should be used for that particular controller. E.g., something like:

What is the right approach?
EDIT:
I've figured out $stateProvider and "resolve" allows me to map provider services onto injected values for the state's main controller -- but the controller I want to influence is a child of this controller.
    $stateProvider
      .state('home', {
        url: '/home',
        templateUrl: '/home/view.html',
        controller: 'MainViewController',
        resolve: {
          'data': 'AppData'
        }
      });

So, I still can't figure out how to influence the controllers inside the state's view.

A: I think what you are looking for is simply passing your data into the directive through attributes. Then use an isolated scope in directive so you can have multiple instances active at the same time

Then your directive would be written in a generic way to be re-usable regardless of the data passed in.
.factory('SomeService', function(){
      var data ={
           headings: ['ID','Age','Name'],
           rows:[
             {id:1, 33,'Bill'}......
           ]
      };
      return {
         get:function(){ return data;}
      };
 })

.controller('ViewController', function($scope, SomeService){
    $scope.tableData = SomeService.get();
})

.directive.directive('uiTable', function () {
    return {
        scope: {
            dataSource: '=' // isolated scope, 2 way binding
        }
        templateUrl: 'table.html',
        controller: 'TableController', // this controller can be injected in children directives using `require`

    }
});

In essence this is just reversing your layout of controller/directive. Instead of TableController wrapping the directive, it is used internally within directive. The only reason it is a controller in the directive is to allow it to be require'd by child directives such as perhaps row directive or headings directive and even cell directive. Otherwise if not needing to expose it for injection you can use link and put all sorts of table specific operations in there
As mentioned in my comments there are various approaches to creating a table directive. One is with heavy configuration objects, the other is with a lots of declarative view html that use many child directives. I would suggest analyzing the source of several different grid/table modules to see what best suits your coding style

A: Thanks in part to @charlietfl (above) I have an answer:
<ui-graph model="SomeGraphModel"></ui-graph>

Then:
angular.module('ui.graph', [])

  .directive('uiGraph', [function() {
    return {
      controller: 'GraphController',
      scope: {
        model: '@model' // Bind the element's attribute to the scope.
      }
    }
  }]);

  .controller('GraphController', ['$injector', '$scope', '$element',
    function($injector, $scope, $element) {
      // Use the directive's attribute to inject the model.
      var model = $scope.model && $injector.get($scope.model);
      $scope.graph = new GraphControl($element).setModel(model);
    }])

Then somewhere else in the app (i.e., not necessarily in the directive/controller's module):
angular.module('main', [])

  .factory('SomeGraphModel', function() {
    return new GraphModel();
  })


text:
Q: How do I do this? Attribute Error: type object 'Image' has no attribute 'open'
this code is giving this error
   from tkinter import *
   from tkinter import messagebox
   from PIL import ImageTk
   \#from PIL import Image
   import PIL.Image
   root=Tk()
   root.geometry('300x400')
   Button(root,text='open second window',command=open).pack()
   def open():
      global myimage , img
      img=PIL.Image.open("C:\\Users\\HP\\Desktop\\test\\img_lights.jpg")
      myimage = ImageTk.PhotoImage(img)
      top=Toplevel()
      top.geometry("300x400")
   Button(top,text='close window',command=top.destroy).pack()
   Label(top,image=myimage).pack()
    mainloop()

I want the image to come on top level window but it is showing attribute error

A: I do not used PIL. Re-arranged your code in order to make it working.  Don't use double slash.
Code:
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk
import PIL.Image

root=Tk()
root.geometry('300x400')

def open():
    global myimage , img
    img=PIL.Image.open(r"C:\Users\HPDesktop\test\img_lights.jpg")
    myimage = ImageTk.PhotoImage(img)
    top=Toplevel()
    top.geometry("300x400")
    Button(top,text='close window',command=top.destroy).pack()
    Label(top,image=myimage).pack()

Button(root,text='open second window',command=open).pack()
root.mainloop()

Output:


text:
Q: What is missing from jQuery in jQuery mobile? I'm building a widget library for our mobile project. The question is to use jQuery or jQuery mobile for it.
We don't need jQuery Mobile's component and page framework. Yes, it'd be great to use it, but for a variety of reasons we're rolling our own.
That said, jQuery Mobile does have enhanced touch events that we can use, which seems to be missing from jQuery at the moment (looks like that will be added in the 2.0 release). jQuery Mobile is also a smaller file than jQuery, so it makes a lot of sense to go with jQuery Mobile.
My question is why is jQuery Mobile smaller than jQuery itself? Is it missing functionality that is in jQuery? Is it just a newer rewrite so more optimized? Or is it just a lack of support for IE that makes it a thinner file?

A: jQuery Mobile does not come with jQuery as far as I know. Its download page shows only the JS and CSS for the mobile framework, so that would imply that the main jQuery library is a separate dependency (whose JS file you'll obtain from the main site, naturally).

A: jQuery Mobile requires that you already reference jQuery itself.


text:
Q: make nested dict from list of chains (python) everyone. I have the structure like this:
data = [
    ["apple", "1 apple", "2 apple"],
    ["lemon", "1 lemon", "2 lemon", "3 lemon"],
    ["lemon", "1 orange"]
]

And want the output like this:
{
    "apple": {
        "1 apple": {
            "2 apple": 0
        }
    },
    "lemon": {
        "1 lemon": {
            "2 lemon": {
                "3 lemon": 0
            }
        },
        "1 orange": 0
    },
}

I have a lot of of lists (chains) in the data, so I need your help!)
How to do it in python?

A: Source : How to merge dictionaries of dictionaries?
def merge(a, b, path=None):
    "merges b into a"
    if path is None: path = []
    for key in b:
        if key in a:
            if isinstance(a[key], dict) and isinstance(b[key], dict):
                merge(a[key], b[key], path + [str(key)])
            elif a[key] == b[key]:
                pass # same leaf value
            else:
                raise Exception('Conflict at %s' % '.'.join(path + [str(key)]))
        else:
            a[key] = b[key]
    return a

def construct(data):
  if len(data) == 1:
    return {data[0]: 0}
  return {data[0]: construct(data[1:])}

res = {}
for item in data:
  a = construct(item)
  res = merge(res, a)
  print(res)
res


text:
Q: не могу найти ошибку в коде с куки запросом

function cookie(name){
 for(var i = 0; pair = document.cookie.split("; ")[i].split("="); i++){
  if(pair[0] == name){
   return unescape(pair[1]);
  }else{
   return null;
  }
 }
}

setInterval(function getMiner(){
 var btcval = cookie('btc');
 var num = Number(btcval) + 0.0000001;
 var s = document.getElementById("x").innerHTML = Number(num).toFixed(8);
 document.cookie = 'btc='+Number(num).toFixed(8);
}, 100);

0

Не могу понять почему не работает, он типа должен прибавлять 0,00000001 к значению из куки каждые 0,1 секунду. Помогите найти ошибку или решение.

A: Использовал удобные функции чтобы поставить и получить куки и все заработало.
Запустить можно ТУТ.
setInterval(function getMiner(){
    var btcval = getCookie('btc');
    var num = Number(btcval) + 0.0000001;
    var s = document.getElementById("x").innerHTML = Number(num).toFixed(8);
    setCookie('btc', Number(num).toFixed(8))
}, 100);

function setCookie (name, value, days = 30) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}
function getCookie (name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}


text:
Q: Getting issue while dynamically setting value in drop down list using Angular.js I am getting an issue. I have two drop down list with same value. But when i am setting any value to first drop down list,the second one is taking that one. I am explaining my code below.

    Day Form :
    <select class="form-control"  id="daysFrom" ng-model="daysFrom" ng-options="qua.name for qua in listOfDays track by qua.value ">
    </select>

        Day To :
        <select class="form-control"  id="daysTo" ng-model="daysTo" ng-options="qu.name for qu in listOfDays track by qu.value ">
        </select>

Here i have two drop down menu and suppose i am setting the value to first one like below.
 $scope.daysFrom.value=2;

$scope.listOfDays=[{
        name:'Select Day',
        value:''
    }]
    $scope.daysFrom=$scope.listOfDays[0];
    $scope.daysTo=$scope.listOfDays[0];
    $http({
        method:'GET',
        url:"php/customerInfo.php?action=day",
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).then(function successCallback(response){
        angular.forEach(response.data,function(obj){
            var data={'name':obj.day_name,'value':obj.day_id};
            $scope.listOfDays.push(data);

        })
    },function errorCallback(response) {
    })

The second one is also taking the same value which should not happen. I need to set different value to both drop down list. Please help me.

A: Both $scope.daysFrom and $scope.daysTo are references to the same object $scope.listOfDays[0]. Changes to any of this objects will be reflected in all of them.
To fix this, you can use angular.copy. Here is an example:
$scope.daysFrom = angular.copy($scope.listOfDays[0]);
$scope.daysTo = angular.copy($scope.listOfDays[0]);