问:
Mode Calculation Using Teradata
I am trying to calculate a mode by groups within Teradata. Below is what has been recommended:

```<code>select retl_a
from vpp_fpp
group by dpci_lbl_t
qualify rank() over (order by count(*) desc) = 1;
</code>```

But I receive an error saying Selected non-aggregate values must be part of the associated group. Any help is appreciated.

Below is an example:

```<code>data vpp_fpp
dpci_lbl_t retl_a
a 4
a 4
a 3
b 3
b 1
Expected result:
a 4
b 2
</code>```

答:
```<code>select retl_a
from vpp_fpp
group by dpci_lbl_t
qualify rank() over (order by count(*) desc) = 1;
</code>```

Your initial query returns the "non-aggregate values" error because you are just returning a value "retl_a" without doing any aggregate computation on it.  You can either do something like MAX(retla_a) or move it to your GROUP BY clause.

To get your mode...first you need to get your number of rows per (dpci_lbl_t, retl_a) combo.  You can try something like this (similar to a previous answer):

```<code>SELECT dpci_lbl_t, retl_a, COUNT(*) Count_per_pair
FROM vpp_fpp
GROUP BY 1, 2;
</code>```

Then you need to add your QUALIFY to get the pairs with the highest occurring frequency.  Something like this:

```<code>SELECT dpci_lbl_t, retl_a, COUNT(*) Count_per_pair
FROM vpp_fpp
GROUP BY 1, 2
QUALIFY RANK() OVER(PARTITION BY dpci_lbl_t ORDER BY Count_per_pair) = 1;
</code>```

To handle the case of a tie...you can wrap that around another SELECT and just take the AVERAGE of the sub-select's "retl_a" mode values:

```<code>SELECT dpci_lbl_t, AVERAGE(retl_a) AS MyMode
FROM (
    SELECT dpci_lbl_t, retl_a, COUNT(*) Count_per_pair
    FROM vpp_fpp
    GROUP BY 1, 2
    QUALIFY RANK() OVER(PARTITION BY dpci_lbl_t ORDER BY Count_per_pair) = 1
) src
GROUP BY 1, 2;
</code>```

Keep in mind, aggregates (i.e. MAX/COUNT...GROUP BY) and their HAVING clauses are processed before window functions (i.e. RANK(), ROW_NUMBER()) and their QUALIFY clauses.

To sum up:

The COUNT/GROUP BY functions get the number of rows per (dpci_lbl_t, retl_a) combination.  This represents how often each "retl_a" value occurs per "dpci_lbl_t" group.

The RANK() function then partitions the resulting rows by "dpci_lbl_t", orders them by the "Count_per_pair" value, and assigns a ranking based on that value.

The QUALIFY then filters out that result, only returning rows with RANK() = 1.  Think of it as a WHERE clause for window functions.

The AVERAGE() in the outer SELECT returns the "mode" values, including an average for any ties.

I don't have access to a TD system to verify, but give it a try and let me know if it works.

Title: Mode Calculation Using Teradata

Body:
I am trying to calculate a mode by groups within Teradata. Below is what has been recommended:

select retl_a
from vpp_fpp
group by dpci_lbl_t
qualify rank() over (order by count(*) desc) = 1;

But I receive an error saying Selected non-aggregate values must be part of the associated group. Any help is appreciated.

Below is an example:

data vpp_fpp
dpci_lbl_t retl_a
a 4
a 4
a 3
b 3
b 1
Expected result:
a 4
b 2

Body:
select retl_a
from vpp_fpp
group by dpci_lbl_t
qualify rank() over (order by count(*) desc) = 1;

Your initial query returns the "non-aggregate values" error because you are just returning a value "retl_a" without doing any aggregate computation on it.  You can either do something like MAX(retla_a) or move it to your GROUP BY clause.

To get your mode...first you need to get your number of rows per (dpci_lbl_t, retl_a) combo.  You can try something like this (similar to a previous answer):

SELECT dpci_lbl_t, retl_a, COUNT(*) Count_per_pair
FROM vpp_fpp
GROUP BY 1, 2;

Then you need to add your QUALIFY to get the pairs with the highest occurring frequency.  Something like this:

SELECT dpci_lbl_t, retl_a, COUNT(*) Count_per_pair
FROM vpp_fpp
GROUP BY 1, 2
QUALIFY RANK() OVER(PARTITION BY dpci_lbl_t ORDER BY Count_per_pair) = 1;

To handle the case of a tie...you can wrap that around another SELECT and just take the AVERAGE of the sub-select's "retl_a" mode values:

SELECT dpci_lbl_t, AVERAGE(retl_a) AS MyMode
FROM (
    SELECT dpci_lbl_t, retl_a, COUNT(*) Count_per_pair
    FROM vpp_fpp
    GROUP BY 1, 2
    QUALIFY RANK() OVER(PARTITION BY dpci_lbl_t ORDER BY Count_per_pair) = 1
) src
GROUP BY 1, 2;

Keep in mind, aggregates (i.e. MAX/COUNT...GROUP BY) and their HAVING clauses are processed before window functions (i.e. RANK(), ROW_NUMBER()) and their QUALIFY clauses.

To sum up:

The COUNT/GROUP BY functions get the number of rows per (dpci_lbl_t, retl_a) combination.  This represents how often each "retl_a" value occurs per "dpci_lbl_t" group.

The RANK() function then partitions the resulting rows by "dpci_lbl_t", orders them by the "Count_per_pair" value, and assigns a ranking based on that value.

The QUALIFY then filters out that result, only returning rows with RANK() = 1.  Think of it as a WHERE clause for window functions.

The AVERAGE() in the outer SELECT returns the "mode" values, including an average for any ties.

I don't have access to a TD system to verify, but give it a try and let me know if it works.


来源: stackexchange

问:
Error when calling UDF using broadcasted objects in PySpark
I am trying to invoke a UDF that uses a broadcasted object in PySpark.

Here is a minimal example that reproduces the situation and error:

import pyspark.sql.functions as sf
from pyspark.sql.types import LongType

class SquareClass:
    def compute(self, n):
        return n ** 2

square = SquareClass()
square_sc = sc.broadcast(square)

def f(n):
    return square_sc.value.compute(n)

numbers = sc.parallelize([{'id': i} for i in range(10)]).toDF()
f_udf = sf.udf(f, LongType())

numbers.select(f_udf(numbers.id)).show(10)
```

<p>The stacktrace and error message that this snippet produces:</p>

<pre class="lang-py prettyprint-override"><code>Traceback (most recent call last)
<ipython-input-75-6e38c014e4b2> in <module>()
     13 f_udf = sf.udf(f, LongType())
     14
---> 15 numbers.select(f_udf(numbers.id)).show(10)

/usr/hdp/current/spark-client/python/pyspark/sql/dataframe.py in show(self, n, truncate)
    255         +---+-----+
    256         """
--> 257         print(self._jdf.showString(n, truncate))
    258
    259     def __repr__(self):

/usr/local/lib/python3.5/dist-packages/py4j/java_gateway.py in __call__(self, *args)
   1131         answer = self.gateway_client.send_command(command)
   1132         return_value = get_return_value(
-> 1133             answer, self.gateway_client, self.target_id,

<snip>

An error occurred while calling o938.showString.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 1 in stage 49.0 failed 1 times, most recent failure: Lost task 1.0 in stage 49.0 (TID 587, localhost): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
</code>```

答:
When calling the attributes of square_sc you're calling the module SquareClass which is not present on the workers.

If you want to use a python package, class, function in a UDF, workers should be able to have access to it you can achieve this by putting the code in a python script and deploying it using --py-files when running you spark-submit, pyspark

ContentLicense: CC BY-SA 3.0

Body:
I am trying to invoke a UDF that uses a broadcasted object in PySpark.

Here is a minimal example that reproduces the situation and error:

import pyspark.sql.functions as sf
from pyspark.sql.types import LongType

class SquareClass:
    def compute(self, n):
        return n ** 2

square = SquareClass()
square_sc = sc.broadcast(square)

def f(n):
    return square_sc.value.compute(n)

numbers = sc.parallelize([{'id': i} for i in range(10)]).toDF()
f_udf = sf.udf(f, LongType())

numbers.select(f_udf(numbers.id)).show(10)

The stacktrace and error message that this snippet produces:

Traceback (most recent call last)
<ipython-input-75-6e38c014e4b2> in <module>()
     13 f_udf = sf.udf(f, LongType())
     14
---> 15 numbers.select(f_udf(numbers.id)).show(10)

/usr/hdp/current/spark-client/python/pyspark/sql/dataframe.py in show(self, n, truncate)
    255         +---+-----+
    256         """
--> 257         print(self._jdf.showString(n, truncate))
    258
    259     def __repr__(self):

/usr/local/lib/python3.5/dist-packages/py4j/java_gateway.py in __call__(self, *args)
   1131         answer = self.gateway_client.send_command(command)
   1132         return_value = get_return_value(
-> 1133             answer, self.gateway_client, self.target_id,

<snip>

An error occurred while calling o938.showString.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 1 in stage 49.0 failed 1 times, most recent failure: Lost task 1.0 in stage 49.0 (TID 587, localhost): org.apache.spark.api.python.PythonException: Traceback (most recent call last):


问:
Java: Comparing two lists, if either contains the value of the other, add all values of both to a new list
I have a HashMap with String keys and ArrayList of MyObject values and now I want to make a new list of lists which contain all overlapping MyObjects.

For example: if ArrayList1 contains MyObjectA, MyObjectB, MyObjectC and ArrayList2 contains MyObjectA, MyObjectD, MyObjectE, then I want to add MyObjectA - E to a new list and put all of these lists in a master list. I want to essentially combine all of each ArrayList's values into a new list if any of the values overlap.

So far I'm just iterating through the map, and over each list, then nesting an iteration again, and if any value matches, doing another iteration of both ArrayLists in the nest to add them to a different list, but this is leading to duplication within the new list of lists.

Sorry if this isn't very clear.

Does anyone have any suggestions or perhaps a better way to accomplish this?

Thank You!

Here's my code:

```<code>public class DetermineOverlaps {

HashMap<String, ArrayList<CustomObject>> pgsPerStMap;

HashSet<HashSet<String>> competingPgs;

public DetermineOverlaps (HashMap<String, ArrayList<CustomObject>> pgsPerStMap2){

    pgsPerStMap = new HashMap<String, ArrayList<CustomObject>>(pgsPerStMap2);

    competingPgs = calculateCompetingPgs();

}

public HashSet<HashSet<String>> calculateCompetingPgs (){

    //This will be the hashset which gets returned from this method
    HashSet<HashSet<String>> competingProdGros = new HashSet<HashSet<String>>();

    //I will iterate over each term (key) within the pgsPerStMap map, which contains search terms | all customObject for that search term
    for (String searchTerm : pgsPerStMap.keySet()){

        //I will iterate over each customObject for the search term
        ArrayList<CustomObject> searchTermsPgs = pgsPerStMap.get(searchTerm);

        for (CustomObject curProdGroup : searchTermsPgs){

            String curProdGroupName = curProdGroup.key;

            //I will store all found matches in this hashset, which I will later put in the competingProdGros map
            HashSet<String> tempPgSet = new HashSet<String>();

            //Compare every other key/value combination of the map to every other key/value combination of the map
            for (String searchTerm2ndLevel : pgsPerStMap.keySet()){
                //Iterate over the customObject
                ArrayList<CustomObject> searchTermsPgsLev2 = pgsPerStMap.get(searchTerm2ndLevel);

                for (CustomObject curProdGroup2ndLevel : searchTermsPgsLev2){

                     String curProdGroupLevel2Name = curProdGroup2ndLevel.key;
                    //If these are different keys, but the same value, i.e. you've got a value which has multiple
                    //overlapping keys, and the temporary hashset doesn't contain the value already
                    //Then add all values from both arraylist<CustomObject> into the temporary hashset...
                    if (!searchTerm2ndLevel.equals(searchTerm) && curProdGroupLevel2Name.equals(curProdGroupName)
                    && !tempPgSet.contains(curProdGroupLevel2Name)){

                        for (CustomObject levelOnePg : searchTermsPgs){

                            String levelOnePgKey = levelOnePg.key;

                            tempPgSet.add(levelOnePgKey);

                        }

                        for (CustomObject levelTwoPg : searchTermsPgsLev2){

                            String levelTwoPgKey = levelTwoPg.key;
                            tempPgSet.add(levelTwoPgKey);

                        }

                    }

                }

            }

            //Add the temporary hashset into the competingProdGros hashset
            if(!competingProdGros.contains(tempPgSet)){competingProdGros.add(tempPgSet);}

        }

    }

    //return the competingprodgros hashset
    return competingProdGros;

}
</code>```

答:
I can't make heads or tails of what it is you're trying to do, but I can give you some ideas to help you implement it.

You said you want a list of objects A-E, so I would say you are not allowing for duplicates. In which case you could use a map of String to Sets instead to cut down on the size of your master list

I'm going to give you 4 scenarios:

Scenario 1: you do not allow for duplicates and lists are references to the objects with unique contents

```<code>Map<String, Set<MyObject> map = new HashMap<String, Set<MyObject>>();
Set<MyObject> set1 = new LinkedHashSet<MyObject>();
Set<MyObject> set2 = new LinkedHashSet<MyObject>();
MyObject a = new MyObject("A");
MyObject b = new MyObject("B");
MyObject c = new MyObject("C");
MyObject d = new MyObject("D");
MyObject e = new MyObject("E");
set1.add(a);
set1.add(b);
set1.add(c);
set2.add(c);
set2.add(d);
set2.add(e);
set2.removeAll(set1)
set1.addAll(set2);
map.put("Mykey", set1);
</code>```

What this says is the objects are the same among the sets and thus will be removed between the two sets. The contents of set1 are:

```<code>[ "A", "B", "C", "D", "E" ]
</code>```

Scenario 2: You do allow for duplicates and lists are references to the same objects. Here I'm guessing you would want a 1-to-1 removal, so something you loop through a set of intersections until is empty then add the list to the combined list. If you need to maintain the values of your list, you can always instantiate a new one and pass it the contents of your list

A couple last notes: removing the element of a tree set in an enhanced for loop risks throwing a ConcurrentModificationException

```<code>MyObject a = new MyObject("A");
MyObject b = new MyObject("B");
MyObject c = new MyObject("C");
MyObject d = new MyObject("D");
MyObject e = new MyObject("E");
List<CodeTester> l = new ArrayList<CodeTester>();
List<CodeTester> l2 = new ArrayList<CodeTester>();
l.add(a);
l.add(c);
l.add(c);
l.add(b);
l.add(b);
l.add(d);
l2.add(c);
l2.add(d);
l2.add(d);
l2.add(e);
Set<CodeTester> set = new HashSet<CodeTester>(l);
set.retainAll(l2); // get the intersections
while(!set.isEmpty()) {
    l2.remove(0);
    set.retainAll(l2);
}
map.put("myKey", set);
</code>```

The output of these contents is:

```<code>[ "A", "C", "C", "B", "B", "D", "D", "E" ]
</code>```

Notice that 1 instance of "C" and "D" were excluded from the final list because the other list had them

Scenario 3: You do not allow for duplicates but list elements are not guaranteed to be references to objects with unique contents

If I had to guess I'd say this is the one you're looking for.
Here MyObject instantiates new instances with different hash codes/ids. This makes all MyObject instances unique regardless of contents, so if you declare two MyObjects with the same String, they will not be removed/retained by default Collections operations. To compare contents, you'll need to implement a few a few overridden methods. Then you'll be able to use the above procedures the same way. Only difference is to user TreeSet instead of HashSet to invoke the overridden equals method then we can either implement Comparable or implement a Comparator to use the TreeSet and avoid the ClassCastException it would throw from its default Comparator.

For Sets to enforce uniqueness:

```<code>public class MyObject implements Comparable<MyObject>
String s;
Map<String, Set<MyObject> map = new HashMap<String, Set<MyObject>>();
Map<String, List<MyObject> map = new HashMap<String, List<MyObject>>();

public MyObject(String s){
    this.s = s;
}

public void myExample1(){
    Set<MyObject> set1 = new TreeSet<MyObject>();
    Set<MyObject> set2 = new TreeSet<MyObject>();
    MyObject a = new MyObject("A");
    MyObject b = new MyObject("B");
    MyObject c = new MyObject("C");
    MyObject d = new MyObject("D");
    MyObject e = new MyObject("E");
    MyObject b2 = new MyObject(new StringBuilder("F").replace(0, 1, "B").toString());
    MyObject d2 = new MyObject(new StringBuilder("G").replace(0, 1, "D").toString());
    set1.add(a);
    set1.add(b);
    set1.add(c);
    set2.add(b2);
    set2.add(d);
    set2.add(d2);
    set2.add(e);
    set2.removeAll(set1)
    set1.addAll(set2);
    mapS.put("Mykey", set1);
}

public void myExample2(){
    List<MyObject> l = new ArrayList<MyObject>();
    List<MyObject> l2 = new ArrayList<MyObject>();
    l.add(a);
    l.add(c);
    l.add(c);
    l.add(b2);
    l.add(d);
    l2.add(b);
    l2.add(d);
    l2.add(d2);
    l2.add(e);
    Set<MyObject> set = new TreeSet<MyObject>(l);
    set.retainAll(l2); // get the intersections
    Set<MyObject> set2 = new HashSet<MyObject>(set);
    while(!set.isEmpty()) {
        l2.remove(0);
        set.retainAll(l2);
    }
    l.addAll(l2);
    mapL.put("myKey", l);
}

@Override
public int compareTo(MyObject o){
    return s.compareTo(o.getS);
}

public String getS(){
    return s;
}

@Override
public boolean equals(Object o){
    return o instanceof MyObject && compareTo((MyObject) o);
}
</code>```

}

myExample1 Outputs:

```<code>[ "A", "B", "C", "D", "E" ]
</code>```

With has codes to show uniqueness:

```<code>[A 865113938, B 1442407170, C 1028566121] [B 1118140819, D 1975012498, E 1808253012]
[A 865113938, B 1442407170, C 1028566121, D 1975012498, E 1808253012]
</code>```

myExample1 Outputs:

```<code>[ "A", "B", "C", "D", "E" ]
</code>```

With hash codes to show uniqueness:

```<code>[A 865113938, C 1442407170, C 1442407170, B 1028566121, D 1118140819] [B 1975012498, D 1118140819, D 1808253012, E 589431969]
[A 865113938, C 1442407170, C 1442407170, B 1028566121, D 1118140819, E 589431969]
</code>```

Body:
I have a HashMap with String keys and ArrayList of MyObject values and now I want to make a new list of lists which contain all overlapping MyObjects.

For example: if ArrayList1 contains MyObjectA, MyObjectB, MyObjectC and ArrayList2 contains MyObjectA, MyObjectD, MyObjectE, then I want to add MyObjectA - E to a new list and put all of these lists in a master list. I want to essentially combine all of each ArrayList's values into a new list if any of the values overlap.

So far I'm just iterating through the map, and over each list, then nesting an iteration again, and if any value matches, doing another iteration of both ArrayLists in the nest to add them to a different list, but this is leading to duplication within the new list of lists.

Sorry if this isn't very clear.

Does anyone have any suggestions or perhaps a better way to accomplish this?

Thank You!

Here's my code:

public class DetermineOverlaps {

HashMap<String, ArrayList<CustomObject>> pgsPerStMap;

HashSet<HashSet<String>> competingPgs;

public DetermineOverlaps (HashMap<String, ArrayList<CustomObject>> pgsPerStMap2){

    pgsPerStMap = new HashMap<String, ArrayList<CustomObject>>(pgsPerStMap2);

    competingPgs = calculateCompetingPgs();

}

public HashSet<HashSet<String>> calculateCompetingPgs (){

    //This will be the hashset which gets returned from this method
    HashSet<HashSet<String>> competingProdGros = new HashSet<HashSet<String>>();

    //I will iterate over each term (key) within the pgsPerStMap map, which contains search terms | all customObject for that search term
    for (String searchTerm : pgsPerStMap.keySet()){

        //I will iterate over each customObject for the search term
        ArrayList<CustomObject> searchTermsPgs = pgsPerStMap.get(searchTerm);

        for (CustomObject curProdGroup : searchTermsPgs){

            String curProdGroupName = curProdGroup.key;

            //I will store all found matches in this hashset, which I will later put in the competingProdGros map
            HashSet<String> tempPgSet = new HashSet<String>();

            //Compare every other key/value combination of the map to every other key/value combination of the map
            for (String searchTerm2ndLevel : pgsPerStMap.keySet()){
                //Iterate over the customObject
                ArrayList<CustomObject> searchTermsPgsLev2 = pgsPerStMap.get(searchTerm2ndLevel);

                for (CustomObject curProdGroup2ndLevel : searchTermsPgsLev2){

                     String curProdGroupLevel2Name = curProdGroup2ndLevel.key;
                    //If these are different keys, but the same value, i.e. you've got a value which has multiple
                    //overlapping keys, and the temporary hashset doesn't contain the value already
                    //Then add all values from both arraylist<CustomObject> into the temporary hashset...
                    if (!searchTerm2ndLevel.equals(searchTerm) && curProdGroupLevel2Name.equals(curProdGroupName)
                    && !tempPgSet.contains(curProdGroupLevel2Name)){

                        for (CustomObject levelOnePg : searchTermsPgs){

                            String levelOnePgKey = levelOnePg.key;

                            tempPgSet.add(levelOnePgKey);

                        }

                        for (CustomObject levelTwoPg : searchTermsPgsLev2){

                            String levelTwoPgKey = levelTwoPg.key;
                            tempPgSet.add(levelTwoPgKey);

                        }

                    }

                }

            }

            //Add the temporary hashset into the competingProdGros hashset
            if(!competingProdGros.contains(tempPgSet)){competingProdGros.add(tempPgSet);}

        }

    }

    //return the competingprodgros hashset
    return competingProdGros;

}

Body:
I can't make heads or tails of what it is you're trying to do, but I can give you some ideas to help you implement it.

You said you want a list of objects A-E, so I would say you are not allowing for duplicates. In which case you could use a map of String to Sets instead to cut down on the size of your master list

I'm going to give you 4 scenarios:

Scenario 1: you do not allow for duplicates and lists are references to the objects with unique contents

Map<String, Set<MyObject> map = new HashMap<String, Set<MyObject>>();
Set<MyObject> set1 = new LinkedHashSet<MyObject>();
Set<MyObject> set2 = new LinkedHashSet<MyObject>();
MyObject a = new MyObject("A");
MyObject b = new MyObject("B");
MyObject c = new MyObject("C");
MyObject d = new MyObject("D");
MyObject e = new MyObject("E");
set1.add(a);
set1.add(b);
set1.add(c);
set2.add(c);
set2.add(d);
set2.add(e);
set2.removeAll(set1)
set1.addAll(set2);
map.put("Mykey", set1);

What this says is the objects are the same among the sets and thus will be removed between the two sets. The contents of set1 are:

[ "A", "B", "C", "D", "E" ]

Scenario 2: You do allow for duplicates and lists are references to the same objects. Here I'm guessing you would want a 1-to-1 removal, so something you loop through a set of intersections until is empty then add the list to the combined list. If you need to maintain the values of your list, you can always instantiate a new one and pass it the contents of your list

A couple last notes: removing the element of a tree set in an enhanced for loop risks throwing a ConcurrentModificationException

MyObject a = new MyObject("A");
MyObject b = new MyObject("B");
MyObject c = new MyObject("C");
MyObject d = new MyObject("D");
MyObject e = new MyObject("E");
List<CodeTester> l = new ArrayList<CodeTester>();
List<CodeTester> l2 = new ArrayList<CodeTester>();
l.add(a);
l.add(c);
l.add(c);
l.add(b);
l.add(b);
l.add(d);
l2.add(c);
l2.add(d);
l2.add(d);
l2.add(e);
Set<CodeTester> set = new HashSet<CodeTester>(l);
set.retainAll(l2); // get the intersections
while(!set.isEmpty()) {
    l2.remove(0);
    set.retainAll(l2);
}
map.put("myKey", set);

The output of these contents is:

[ "A", "C", "C", "B", "B", "D", "D", "E" ]

Notice that 1 instance of "C" and "D" were excluded from the final list because the other list had them

Scenario 3: You do not allow for duplicates but list elements are not guaranteed to be references to objects with unique contents

If I had to guess I'd say this is the one you're looking for.
Here MyObject instantiates new instances with different hash codes/ids. This makes all MyObject instances unique regardless of contents, so if you declare two MyObjects with the same String, they will not be removed/retained by default Collections operations. To compare contents, you'll need to implement a few a few overridden methods. Then you'll be able to use the above procedures the same way. Only difference is to user TreeSet instead of HashSet to invoke the overridden equals method then we can either implement Comparable or implement a Comparator to use the TreeSet and avoid the ClassCastException it would throw from its default Comparator.

For Sets to enforce uniqueness:

public class MyObject implements Comparable<MyObject>
String s;
Map<String, Set<MyObject> map = new HashMap<String, Set<MyObject>>();
Map<String, List<MyObject> map = new HashMap<String, List<MyObject>>();

public MyObject(String s){
    this.s = s;
}

public void myExample1(){
    Set<MyObject> set1 = new TreeSet<MyObject>();
    Set<MyObject> set2 = new TreeSet<MyObject>();
    MyObject a = new MyObject("A");
    MyObject b = new MyObject("B");
    MyObject c = new MyObject("C");
    MyObject d = new MyObject("D");
    MyObject e = new MyObject("E");
    MyObject b2 = new MyObject(new StringBuilder("F").replace(0, 1, "B").toString());
    MyObject d2 = new MyObject(new StringBuilder("G").replace(0, 1, "D").toString());
    set1.add(a);
    set1.add(b);
    set1.add(c);
    set2.add(b2);
    set2.add(d);
    set2.add(d2);
    set2.add(e);
    set2.removeAll(set1)
    set1.addAll(set2);
    mapS.put("Mykey", set1);
}

public void myExample2(){
    List<MyObject> l = new ArrayList<MyObject>();
    List<MyObject> l2 = new ArrayList<MyObject>();
    l.add(a);
    l.add(c);
    l.add(c);
    l.add(b2);
    l.add(d);
    l2.add(b);
    l2.add(d);
    l2.add(d2);
    l2.add(e);
    Set<MyObject> set = new TreeSet<MyObject>(l);
    set.retainAll(l2); // get the intersections
    Set<MyObject> set2 = new HashSet<MyObject>(set);
    while(!set.isEmpty()) {
        l2.remove(0);
        set.retainAll(l2);
    }
    l.addAll(l2);
    mapL.put("myKey", l);
}

@Override
public int compareTo(MyObject o){
    return s.compareTo(o.getS);
}

public String getS(){
    return s;
}

@Override
public boolean equals(Object o){
    return o instanceof MyObject && compareTo((MyObject) o);
}

}

myExample1 Outputs:

[ "A", "B", "C", "D", "E" ]

With has codes to show uniqueness:

[A 865113938, B 1442407170, C 1028566121] [B 1118140819, D 1975012498, E 1808253012]
[A 865113938, B 1442407170, C 1028566121, D 1975012498, E 1808253012]

myExample1 Outputs:

[ "A", "B", "C", "D", "E" ]

With hash codes to show uniqueness:

[A 865113938, C 1442407170, C 1442407170, B 1028566121, D 1118140819] [B 1975012498, D 1118140819, D 1808253012, E 589431969]
[A 865113938, C 1442407170, C 1442407170, B 1028566121, D 1118140819, E 589431969]


问:
Tests stop working under xcode 8 TEST_HOST error
I want to start tests under Xcode 8 and it fail in the beginning. My error is:

Could not determine bundle identifier for MyProjectTest's TEST_HOST:
  "/Users/jakubmazur/Library/Developer/Xcode/DerivedData/MyProject-ejeepybggxvekxajlyngopeahiex/Build/Intermediates/CodeCoverage/Products/Testing-iphonesimulator/MyProject.app"

Any idea what's wrong here? I try to clean project - nothing.
In the Build Settings in my target in Packaging i change Product Bundle Identifier depends on configuration in my schema. It can have something to do with that, but not sure.

-- EDIT

Ok figure how to narrow the problem. When i change settings in scheme to use Build Configuration Debug instead of Testing seems to work, but I need separate configuration for testing.

答:
Quitting Xcode and removing DerivedData folder at fixed the problem for me.

```<code>$ sudo rm -Rf Library/Developer/Xcode/DerivedData
</code>```

Shame on you, Apple!

Body:
Quitting Xcode and removing DerivedData folder at fixed the problem for me.

$ sudo rm -Rf Library/Developer/Xcode/DerivedData

Shame on you, Apple!


问:
Remove beginning and trailing new line characters from NSString
How can one remove only the beginning and trailing new line \n from a NSString.

For e.g.

```<code>@"\n\n\n\Hi\nHow are you\nToday.\n\n\n\n"
</code>```

output:

```<code>@"Hi\nHow are you\nToday."
</code>```

Thanks.

答:
Swift 3.0

```<code>var yourString = "\n your string \n"
yourString = yourString.trimmingCharacters(in: .whitespacesAndNewlines)
</code>```

Output "your string"

Body:
How can one remove only the beginning and trailing new line \n from a NSString.

For e.g.

@"\n\n\n\Hi\nHow are you\nToday.\n\n\n\n"

output:

@"Hi\nHow are you\nToday."

Thanks.

Body:
Swift 3.0

var yourString = "\n your string \n"
yourString = yourString.trimmingCharacters(in: .whitespacesAndNewlines)

Output "your string"


问:
Extract Freestyle Jobs and create pipeline Jobs in another Jenkins instance
I have two jenkins instances (jenkins1 and jenkins2)

Jenkins1 - Contains freestyle jobs (all runs on a specific template)

I need to extract all the jobs from jenkins1 and create those jobs as pipeline jobs in jenkins2.

I know simply copying the jobs doesnt work (because it is two different templates Freestyle and pipeline)

How can I do it in efficient way using a groovy/shell script  to achieve this?

答:
Every job has a config.xml where all the job step are listed in xml.

Parse that file and extract all the information than convert them in a pipeline job routine.

I think groovy/shell scripts are a perfect way to achieve it, just use the config.xml as source of information.

The below resources can help:

https://jenkinsworld20162017.sched.com/event/Bk3r/auto-convert-your-freestyle-jenkins-jobs-to-coded-pipeline?iframe=no&w=100%&sidebar=yes&bg=no

https://github.com/visualphoenix/jenkins-xml-to-jobdsl


问:
How to store timestamp message from aws iot button to DynamoDB
I am sending message from my AWS iot button and storing it to the DynamoDB. My partition key is "SerialNumber" and the sort key is "ClickType". The message is stored according to the key. But it is overwriting the previous data if there is already same ClickType. I do not want to overwrite my previous data and also want to see the time of the sending message. Can somebody provide me any suggestion or any link about how to do it?</p>

答:
On your AWS IoT rule for getting the message to lambda has the timestamp when the message was captured by Aws Iot rules engine. You could catch that on your rule and store on your Dynamodb table.
To do that, you have to use the timestamp() function from AWs IoT </p>


问:
Javascript sort algorithm for linked sections objects array
I have the following array of objects which need to be sorted in a special way:

```<code>var sections = [
    {begin:"test3", end:"test4"},
    {begin:"test5", end:"test2"},
    {begin:"test2", end:"test3"},
];
</code>```

All sections are linked together via sectionA.end == sectionB.begin so the result of the sort operation should be:

```<code>var sectionsSorted = [
    {begin:"test5", end:"test2"},
    {begin:"test2", end:"test3"},
    {begin:"test3", end:"test4"}
];
</code>```

I want to do this in the Array.prototype.sort() method. I realised that the beginning section could be found if the begin is not an end in any section but from there on I am lot. Has anybody an idea how to implement something like this?

I did a JSFiddle: https://jsfiddle.net/fxmnxh8L/1/

答:
Try this:

var sections = [
    {begin:"test3", end:"test4"},
    {begin:"test5", end:"test2"},
    {begin:"test2", end:"test3"},
];

sections.sort((s1, s2) => {
  return s1.end === s2.begin ? -1 : 1;
});

console.log(sections);```
</div>
</div>
</p>

<p>EDIT: the above solution doesn't work (see comments to know why). Take a look at the below solution that use a recursive approach to compare two given sections:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var sections = [
    {begin: "test4", end: "test7"},
    {begin: "test5", end: "test2"},
    {begin: "test7", end: "test8"},
    {begin: "test2", end: "test3"},
    {begin: "test3", end: "test4"},
    {begin: "test8", end: "test9"}
];

var sectionsMap = sections.reduce((m, o) => {
    m[o.begin] = o;
    return m;
}, {});

function compare(a, b) {
    if (!sectionsMap[a.end]) {
        return 1;
    } else if (sectionsMap[a.end].begin === b.begin) {
        return -1;
    } else {
        return compare(sectionsMap[a.end], b);
    }
}

sections.sort(compare);

console.log(sections);</code>```

Body:
I have the following array of objects which need to be sorted in a special way:

var sections = [
    {begin:"test3", end:"test4"},
    {begin:"test5", end:"test2"},
    {begin:"test2", end:"test3"},
];

All sections are linked together via sectionA.end == sectionB.begin so the result of the sort operation should be:

var sectionsSorted = [
    {begin:"test5", end:"test2"},
    {begin:"test2", end:"test3"},
    {begin:"test3", end:"test4"}
];

I want to do this in the Array.prototype.sort() method. I realised that the beginning section could be found if the begin is not an end in any section but from there on I am lot. Has anybody an idea how to implement something like this?

I did a JSFiddle: https://jsfiddle.net/fxmnxh8L/1/

Body:
Try this:

var sections = [
    {begin:"test3", end:"test4"},
    {begin:"test5", end:"test2"},
    {begin:"test2", end:"test3"},
];

sections.sort((s1, s2) => {
  return s1.end === s2.begin ? -1 : 1;
});

console.log(sections);

EDIT: the above solution doesn't work (see comments to know why). Take a look at the below solution that use a recursive approach to compare two given sections:

var sections = [
    {begin: "test4", end: "test7"},
    {begin: "test5", end: "test2"},
    {begin: "test7", end: "test8"},
    {begin: "test2", end: "test3"},
    {begin: "test3", end: "test4"},
    {begin: "test8", end: "test9"}
];

var sectionsMap = sections.reduce((m, o) => {
    m[o.begin] = o;
    return m;
}, {});

function compare(a, b) {
    if (!sectionsMap[a.end]) {
        return 1;
    } else if (sectionsMap[a.end].begin === b.begin) {
        return -1;
    } else {
        return compare(sectionsMap[a.end], b);
    }
}

sections.sort(compare);

console.log(sections);


问:
rename all files and folders in a path
i want find all files and folders in special path recursively and i do it with this code

```<code>public static function getDirContents($dir, &$results = array()){
    $files = scandir($dir);

    foreach($files as $key => $value){
        $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
        if(!is_dir($path)) {
            $results[] = $path;
        } else if($value != "." && $value != "..") {
            self::getDirContents($path, $results);
            $results[] = $path;
        }
    }

    return $results;
}
</code>```

and i want replace name with space ,Underscore
so i do it with this code

```<code>$files = FileHelper::getDirContents($path_from);
                if (isset($files)) {
                    $replacers = array(" ", "  ", "-", "!", ":", ";", "#", "@", "'");
                    foreach ($files as $file) {
                        $newName = str_replace($replacers, "_", $file);
                        if ($newName != $file) {
                            Logger::setLog('renaming', "Renaming: $file to $newName");
                            rename($file, $newName);
                        }
                    }
                }
</code>```

but the problem occure when i rename parent folder so the system couldnt rename child folder and files because the paths changed or missed
so how can i solve my problem ?

答:
Here is the working code to rename file and folder recursively,

```<code><?php
    renameFiles('C:\assets\images\\','doctor'); // target path is C:\assets\images\doctor
    function renameFiles($base,$path){
      $replacers = array(" ", "  ", "-", "!", ":", ";", "#", "@", "'");
      foreach (new DirectoryIterator($base.$path) as $object) {
          if($object->isDot()) continue;
          if(is_dir($base.$path.'\\'.$object->getFilename())){
            renameFiles($base,$path.'\\'.$object->getFilename());
          }
          $oldName = $object->getFilename();
          $newName = str_replace($replacers, "_", $oldName);
          $oldFullName = $object->getPath() . DIRECTORY_SEPARATOR . $oldName;
          $newFullName = $object->getPath() . DIRECTORY_SEPARATOR . $newName;
          rename($oldFullName, $newFullName);
          echo "old : $oldName , new: $newName <br/>\n";
          //echo $object->getFilename() . "<br>\n";
      }
    }
?>
</code>```

I the above example target path is C:\assets\images\doctor

Title: rename all files and folders in a path

Body:
i want find all files and folders in special path recursively and i do it with this code

public static function getDirContents($dir, &$results = array()){
    $files = scandir($dir);

    foreach($files as $key => $value){
        $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
        if(!is_dir($path)) {
            $results[] = $path;
        } else if($value != "." && $value != "..") {
            self::getDirContents($path, $results);
            $results[] = $path;
        }
    }

    return $results;
}

and i want replace name with space ,Underscore
so i do it with this code

$files = FileHelper::getDirContents($path_from);
                if (isset($files)) {
                    $replacers = array(" ", "  ", "-", "!", ":", ";", "#", "@", "'");
                    foreach ($files as $file) {
                        $newName = str_replace($replacers, "_", $file);
                        if ($newName != $file) {
                            Logger::setLog('renaming', "Renaming: $file to $newName");
                            rename($file, $newName);
                        }
                    }
                }

but the problem occure when i rename parent folder so the system couldnt rename child folder and files because the paths changed or missed
so how can i solve my problem ?

Body:
Here is the working code to rename file and folder recursively,

<?php
    renameFiles('C:\assets\images\\','doctor'); // target path is C:\assets\images\doctor
    function renameFiles($base,$path){
      $replacers = array(" ", "  ", "-", "!", ":", ";", "#", "@", "'");
      foreach (new DirectoryIterator($base.$path) as $object) {
          if($object->isDot()) continue;
          if(is_dir($base.$path.'\\'.$object->getFilename())){
            renameFiles($base,$path.'\\'.$object->getFilename());
          }
          $oldName = $object->getFilename();
          $newName = str_replace($replacers, "_", $oldName);
          $oldFullName = $object->getPath() . DIRECTORY_SEPARATOR . $oldName;
          $newFullName = $object->getPath() . DIRECTORY_SEPARATOR . $newName;
          rename($oldFullName, $newFullName);
          echo "old : $oldName , new: $newName <br/>\n";
          //echo $object->getFilename() . "<br>\n";
      }
    }
?>

I the above example target path is C:\assets\images\doctor


问:
Evaluate Excel VBA boolean condition (not a formula)
I have text in a cell A1 as below

```<code>((True And False Or True) And (True And (Not True))) And (False Or True)
</code>```

I need to evaluate this text and put Boolean result (True / False) in another cell B1 using VBA code.

I tried to use Evaluate function, it is not working.

When I read this cell, it always return string type with double quote enclose in both side. Hence it is treated as string and not evaluating the Boolean expression.

```<code>"((True And True Or True) And (True And (True))) And (True Or True)"
</code>```

I want to write this way, but it is not working

```<code>If Range("A1").Value = True Then
    Range("B1").Value = True
Else
    Range("B1").Value = False
End If
</code>```

I tried to store in Boolean variable also

```<code>Dim Result as Boolean
Result = CBool(Range("A1").Value)
</code>```

as it is string, I am getting type mismatch when I tried to convert using CBool.

答:
This is what you can do with VBA .Evaluate:

```<code>Option Explicit

Public Sub TestMe()

    Dim cell01  As Range
    Dim cell02  As Range

    Set cell01 = Range("A1")
    Set cell02 = Range("A2")

    Range("A1") = "1+2+3+4+5"
    Range("A2") = "TRUE and FALSE"

    Debug.Print Evaluate(CStr(cell01))
    'Debug.Print CBool(cell02) - this will be an error!
    Debug.Print Evaluate(CBool("True") And CBool("False"))

    Debug.Print Evaluate("=AND(TRUE,FALSE)")
    Debug.Print Evaluate("=AND(TRUE,TRUE)")
    Debug.Print Evaluate("=OR(TRUE,TRUE)")

End Sub
</code>```

If you want to parse the TRUE and FALSE thing (commented in my answer), try to build a formula out of it and to evaluate it.

E.g., TRUE AND FALSE, should be translated to =AND(TRUE,FALSE). This gets evaluated easily by VBA as it is an Excel Formula. The translation is not a trivial task, but an interesting one.

ContentLicense: CC BY-SA 4.0

Body:
I have text in a cell A1 as below

((True And False Or True) And (True And (Not True))) And (False Or True)

I need to evaluate this text and put Boolean result (True / False) in another cell B1 using VBA code.

I tried to use Evaluate function, it is not working.

When I read this cell, it always return string type with double quote enclose in both side. Hence it is treated as string and not evaluating the Boolean expression.

"((True And True Or True) And (True And (True))) And (True Or True)"

I want to write this way, but it is not working

If Range("A1").Value = True Then
    Range("B1").Value = True
Else
    Range("B1").Value = False
End If

I tried to store in Boolean variable also

Dim Result as Boolean
Result = CBool(Range("A1").Value)

as it is string, I am getting type mismatch when I tried to convert using CBool.

Body:
This is what you can do with VBA .Evaluate:

Option Explicit

Public Sub TestMe()

    Dim cell01  As Range
    Dim cell02  As Range

    Set cell01 = Range("A1")
    Set cell02 = Range("A2")

    Range("A1") = "1+2+3+4+5"
    Range("A2") = "TRUE and FALSE"

    Debug.Print Evaluate(CStr(cell01))
    'Debug.Print CBool(cell02) - this will be an error!
    Debug.Print Evaluate(CBool("True") And CBool("False"))

    Debug.Print Evaluate("=AND(TRUE,FALSE)")
    Debug.Print Evaluate("=AND(TRUE,TRUE)")
    Debug.Print Evaluate("=OR(TRUE,TRUE)")

End Sub

If you want to parse the TRUE and FALSE thing (commented in my answer), try to build a formula out of it and to evaluate it.

E.g., TRUE AND FALSE, should be translated to =AND(TRUE,FALSE). This gets evaluated easily by VBA as it is an Excel Formula. The translation is not a trivial task, but an interesting one.


问:
Laravel Excel/PHP Excel: Import, modify, download
I'm struggeling with Laravel-Excel (Laravel Package of PHPExcel).

I'm not able to modify a file. My file has one workbook (called template). I want to load the document, modify it and let the user download it:

```<code>Excel::selectSheets('template') -> load($source, function($file)
{

}) -> download('xls');
</code>```

Code above works so far. But I don't get how to fill cells. I don't want to export an eloquent model directly, I need to follow a certain structure. The Excel file was not created by me, I just need to fill it with data.

So how can I for example fill cell A2 with the firstname of the current user?

I search for something like

```<code>Excel::cell('A2') -> content = $user -> name;
</code>```

This is just an example of what I want to achive, this is not working code!

Thanks,
LuMa

答:
You can use following for multiple sheets.</p>

```<code>$somevalue = array('abc', 'xyz');

Excel::load('file.xlsx', function($file) use($somevalue) {
   $file->sheet( 'sheetName', function ($sheet) use($somevalue){
        $sheet->setCellValue('A1', $somevalue);
   });
})->export('xlsx');
</code>```

Body:
I'm struggeling with Laravel-Excel (Laravel Package of PHPExcel).

I'm not able to modify a file. My file has one workbook (called template). I want to load the document, modify it and let the user download it:

Excel::selectSheets('template') -> load($source, function($file)
{

}) -> download('xls');

Code above works so far. But I don't get how to fill cells. I don't want to export an eloquent model directly, I need to follow a certain structure. The Excel file was not created by me, I just need to fill it with data.

So how can I for example fill cell A2 with the firstname of the current user?

I search for something like

Excel::cell('A2') -> content = $user -> name;

This is just an example of what I want to achive, this is not working code!

Thanks,
LuMa

Body:
You can use following for multiple sheets.

$somevalue = array('abc', 'xyz');

Excel::load('file.xlsx', function($file) use($somevalue) {
   $file->sheet( 'sheetName', function ($sheet) use($somevalue){
        $sheet->setCellValue('A1', $somevalue);
   });
})->export('xlsx');


问:
Static data memory limit in C++ application
Someone has written function in our C++ application and is already in the production and I don't know why it's not crashing the application yet. Below is the code.

```<code>char *str_Modify()
{
    char buffer[70000] = { 0 };
    char targetString[70000] = { 0 };

    memset(targetString, '\0', sizeof(targetString));

    ...
    ...
    ...
    return targetString;
}
</code>```

As you can see, the function is returning the address of a local variable and the allocated memory will be released once the function returns.
My question

Wanted to know what is the static data memory limit?

What can be the quick fix for this code? Is it a good practice to make the variable targetString static?

答:
Returning targetString is indeed UB as other answers have said. But there's another supplemental reason why it might crash on some platforms (especially embedded ones): Stack size. The stack segment, where auto variables usually live, is often limited to a few kilobytes; 64K may be common. Two 70K arrays might not be safe to use.

Making targetString static fixes both problems and is an unalloyed improvement IMO;  but might still be problematic if the code is used re-entrantly from multiple threads. In some circumstances it could also be considered an inefficent use of memory.

An alternative approach might be to allocate the return buffer dynamically, return the pointer, and have the calling code free it when no longer required.

As for why might it not crash: if the stack segment is large enough and no other function uses enough of it to overwrite buffer[] and that gets pushed first; then targetString[] might survive unscathed, hanging just below the used stack, effectively in a world of its own. Very unsafe though!

Body:
Someone has written function in our C++ application and is already in the production and I don't know why it's not crashing the application yet. Below is the code.

char *str_Modify()
{
    char buffer[70000] = { 0 };
    char targetString[70000] = { 0 };

    memset(targetString, '\0', sizeof(targetString));

    ...
    ...
    ...
    return targetString;
}

As you can see, the function is returning the address of a local variable and the allocated memory will be released once the function returns.
My question

Wanted to know what is the static data memory limit?

What can be the quick fix for this code? Is it a good practice to make the variable targetString static?


问:
"Unable to start debugging" message in VS 2008
I have this message when I start debug in VS 2008:

Error while trying to run project: Unable to start debugging

Because of this I cannot debug any programs any more. Any ideas what causes this?

答: I received this error after our sys admins pushed new security settings. Added the ASP user to the Administrators group and that resolved it.</p>

ContentLicense: CC BY-SA 2.5


问:
how to pass two columns values to plot line chart in C# interop
I am trying to plot line chart after generating excel file using Microsoft interop.
Let my column A is Date, B is Rate and column C is volume and I want to print a chart between Volume and Date.

I am using this code to plot line chart.

```<code>var charts = worKsheeT.ChartObjects();
var chartObject = charts.Add(60, 10, 300, 300);
var chart = chartObject.Chart;
var range = worKsheeT.get_Range("C1", "C9");
chart.SetSourceData(range);
 chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;
</code>```

But it is giving me a chart which has yAxis as Volume and xAxis is simple 1,2,3,4,5....

Thanks in Advance.

答:
After trying a lot I got this solution</p>

```<code>Excel.Series series;
Excel.SeriesCollection seriesCollection;
series = seriesCollection.NewSeries();
var Values = "=Data!$B$2:$B$50";//this is range of columns
var XValues = "=Data!$A$2:$A$50";//this is range of columns
series.Name = "series name";
series.Values = Values;
series.XValues = XValues;
series.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;
</code>```

Body:
I am trying to plot line chart after generating excel file using Microsoft interop.
Let my column A is Date, B is Rate and column C is volume and I want to print a chart between Volume and Date.

I am using this code to plot line chart.

var charts = worKsheeT.ChartObjects();
var chartObject = charts.Add(60, 10, 300, 300);
var chart = chartObject.Chart;
var range = worKsheeT.get_Range("C1", "C9");
chart.SetSourceData(range);
 chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;

But it is giving me a chart which has yAxis as Volume and xAxis is simple 1,2,3,4,5....

Thanks in Advance.

Body:
After trying a lot I got this solution

Excel.Series series;
Excel.SeriesCollection seriesCollection;
series = seriesCollection.NewSeries();
var Values = "=Data!$B$2:$B$50";//this is range of columns
var XValues = "=Data!$A$2:$A$50";//this is range of columns
series.Name = "series name";
series.Values = Values;
series.XValues = XValues;
series.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;


问:
How to set value of textbox created in code behind in javascript
I'm creating a textbox in the code behind:

```<code>TextBox txt = new TextBox();
txt.ID = "txtRef" + count + dr["DataField"].ToString();
div.Controls.Add(txt);
</code>```

And I'm trying to set the value for this textbox in a jquery function but nothing I have tried so far is updating the textbox.

Using document.getElementById('txtRef1j.3rdPartRef').value = 'test'; returns the error Cannot set property 'value' of null.

I also tried $('#txtRef1j.3rdPartRef').val('test') and $('[id$="txtRef1j.3rdPartRef"]').value = 'test'; but the textbox doesn't update.

答:
You can try this.

$('#txtRef1j.3rdPartRef').text('test')

Body:
I'm creating a textbox in the code behind:

TextBox txt = new TextBox();
txt.ID = "txtRef" + count + dr["DataField"].ToString();
div.Controls.Add(txt);

And I'm trying to set the value for this textbox in a jquery function but nothing I have tried so far is updating the textbox.

Using document.getElementById('txtRef1j.3rdPartRef').value = 'test'; returns the error Cannot set property 'value' of null.

I also tried $('#txtRef1j.3rdPartRef').val('test') and $('[id$="txtRef1j.3rdPartRef"]').value = 'test'; but the textbox doesn't update.


问:
Remove item from ArrayList<ArrayList<T>>>
In following code, I want to remove items which are null or size of arraylist is zero from ArrayList in first "for loop". After running first loop, again I  have a second loop. In second loop, I have tested whether it contains any item which is null or size of item is zero. Although I erase in first loop, ArrayList BS contains items which was deleted from list.</p>

```<code>ArrayList<ArrayList<Record>> BS = new ArrayList<>();
.
.// some codes
.

for (int j = 0; j < BS.size(); j++) {
    if(BS.get(j) == null || BS.get(j).size() == 0){
        BS.remove(j);
    }
}

for (int j = 0; j < BS.size(); j++) {
    if(BS.get(j) == null || BS.get(j).size() == 0){
            System.out.println("Again, fall into if condition");
    }
}
</code>```

答:
The problem you've got here is that you won't check the item immediately after the one you removed.

Either:

```<code>BS.removeIf(a -> a == null || a.isEmpty());
</code>```

Or iterate the list in reverse:

```<code>for (int j = BS.size() - 1; j >= 0; j--) { ... }
</code>```

Or use an Iterator, and use the remove() method.

```<code>Iterator<? extends List<Record>> it = BS.iterator();
while (it.hasNext()) {
  List<Record> list = it.next();
  if (list == 0 || list.isEmpty()) it.remove();
}
</code>```

removeIf is the best approach, as it is optimized to know how to remove many elements efficiently: removing anything but the last element of an ArrayList requires all of the elements with greater index to be shifted along in the internal array, so removing elements by simply calling remove is quadratic in time complexity.

However, it is available in Java 8+ only. (You can implement the efficient removal quite easily in earlier versions of Java; it's just a little more work).

Body:
In following code, I want to remove items which are null or size of arraylist is zero from ArrayList in first "for loop". After running first loop, again I  have a second loop. In second loop, I have tested whether it contains any item which is null or size of item is zero. Although I erase in first loop, ArrayList BS contains items which was deleted from list.

ArrayList<ArrayList<Record>> BS = new ArrayList<>();
.
.// some codes
.

for (int j = 0; j < BS.size(); j++) {
    if(BS.get(j) == null || BS.get(j).size() == 0){
        BS.remove(j);
    }
}

for (int j = 0; j < BS.size(); j++) {
    if(BS.get(j) == null || BS.get(j).size() == 0){
            System.out.println("Again, fall into if condition");
    }
}

Body:
The problem you've got here is that you won't check the item immediately after the one you removed.

Either:

BS.removeIf(a -> a == null || a.isEmpty());

Or iterate the list in reverse:

for (int j = BS.size() - 1; j >= 0; j--) { ... }

Or use an Iterator, and use the remove() method.

Iterator<? extends List<Record>> it = BS.iterator();
while (it.hasNext()) {
  List<Record> list = it.next();
  if (list == 0 || list.isEmpty()) it.remove();
}

removeIf is the best approach, as it is optimized to know how to remove many elements efficiently: removing anything but the last element of an ArrayList requires all of the elements with greater index to be shifted along in the internal array, so removing elements by simply calling remove is quadratic in time complexity.

However, it is available in Java 8+ only. (You can implement the efficient removal quite easily in earlier versions of Java; it's just a little more work).


问:
Determine the type of an object?
Is there a simple way to determine if a variable is a list, dictionary, or something else?</p>

答:
be careful using isinstance

```<code>isinstance(True, bool)
True
>>> isinstance(True, int)
True
</code>```

but type

```<code>type(True) == bool
True
>>> type(True) == int
False
</code>```

Title: Determine the type of an object?

Body:
be careful using isinstance

isinstance(True, bool)
True
>>> isinstance(True, int)
True

but type

type(True) == bool
True
>>> type(True) == int
False


问:
What are the most common/uncommon error messages one encounters in Objective-C and what causes them?
I've been trying to maintain a list of all the error messages I ever encounter while developing and their common causes and fixes. What are the common (and the not so common) error messages you encountered and how did you fix them?  </p>

答: EXC_BAD_ACCESS when referencing a pointer that points to an object that has been dealloc'ed


问:
object destructing inside constructor in nodejs to assign values to member variable not working
I am trying ES6 object destructuring inside a constructor hoping to assign value to a member variable. It is not working. Its showing undefined when I am
printing the value inside a member function. Its printing correctly if I print inside the constructor.Is this valid?</p>

```<code>    class Environment {
        constructor(env) {
            const { name, version } = env
            console.log(name)
        }

        printName() {
            console.log(this.name)
        }
    }
    var obj = { "name": "inst1", "version": "12.2.7" };
    var envObj = new Environment(obj);
    envObj.printName();
</code>```

答:
You can assign destructured properties directly to object props using aliases and a wrapping the expression in parentheses:

class Environment {
    constructor(env) {
        ({ name: this.name, version: this.version } = env);
    }

    printName() {
        console.log(this.name)
    }
}
var obj = { "name": "inst1", "version": "12.2.7" };
var envObj = new Environment(obj);
envObj.printName();```
</div>
</div>
</p>

<p>If <code>env</code> contains only the properties that you want (name, version), you can Object#assign directly to <code>this</code>:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>class Environment {
    constructor(env) {
        Object.assign(this, env);
    }

    printName() {
        console.log(this.name)
    }
}
var obj = { "name": "inst1", "version": "12.2.7" };
var envObj = new Environment(obj);
envObj.printName();</code>```

Body:
I am trying ES6 object destructuring inside a constructor hoping to assign value to a member variable. It is not working. Its showing undefined when I am
printing the value inside a member function. Its printing correctly if I print inside the constructor.Is this valid?

    class Environment {
        constructor(env) {
            const { name, version } = env
            console.log(name)
        }

        printName() {
            console.log(this.name)
        }
    }
    var obj = { "name": "inst1", "version": "12.2.7" };
    var envObj = new Environment(obj);
    envObj.printName();

Body:
You can assign destructured properties directly to object props using aliases and a wrapping the expression in parentheses:

class Environment {
    constructor(env) {
        ({ name: this.name, version: this.version } = env);
    }

    printName() {
        console.log(this.name)
    }
}
var obj = { "name": "inst1", "version": "12.2.7" };
var envObj = new Environment(obj);
envObj.printName();

If env contains only the properties that you want (name, version), you can Object#assign directly to this:

class Environment {
    constructor(env) {
        Object.assign(this, env);
    }

    printName() {
        console.log(this.name)
    }
}
var obj = { "name": "inst1", "version": "12.2.7" };
var envObj = new Environment(obj);
envObj.printName();


问:
reflection method invocation for a dynamic object
I am creating a class (named haan.java) at runtime of my java application.
    The object of this class is inserted in a Oracle Coherence Cache
    When I get an object(tempHaan) from the cache, it is of type haan. But when I try to invoke the method of this object(tempHaan), I am getting the error "java.lang.IllegalArgumentException: object is not an instance of declaring class"

Error occurs at the line "Object invoke = methodgetHash.invoke(tempHaan, null);"

```<code>PFB the code:

NamedCache cacheConn;
CacheFactory.ensureCluster();
NamedCache cacheConnHaan = CacheFactory.getCache("Haan");
Class cls = null;
File f = new File(rtomProperties.getPropertyValue("pojoToBuild"));

try {
    ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
    URLClassLoader cl = new URLClassLoader(new URL[] { f.toURI().toURL() },
        currentThreadClassLoader);
    Object tempHaan;
    cls = Class.forName(rtomProperties.getPropertyValue("pojoPackageLocation").concat(".haan"), true, cl);
    System.out.println("*************** the class is **********"
        + cls.newInstance().getClass().toString());
    System.out.println("******DONE LOADING");

    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class clazz = URLClassLoader.class;

    // Use reflection
    Method m = clazz.getDeclaredMethod("addURL", new Class[] { URL.class });
    m.setAccessible(true);
    URL ur = f.toURI().toURL();
    m.invoke(classLoader, new Object[] { ur });

    Thread.currentThread().getContextClassLoader().getResourceAsStream("context.xml");

    tempHaan = cacheConnHaan.get(aan);

    System.out.println("*************AFTER" + tempHaan.getClass().toString());

    System.out.println("*************tempHaan: "+tempHaan.toString());
    Class[] paramObject = new Class[1];
    paramObject[0] = Object.class;
    Method methodgetHash = null;
    Class noparams[] = {};
    methodgetHash = cls.getDeclaredMethod("getHash", noparams);
    Object temp = cls.newInstance();

    Object invoke = methodgetHash.invoke(tempHaan, null);
    key = (String) invoke;

    System.out.println("key for the record: " + key);
} catch (ClassNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (InstantiationException e) {
    // TODO Auto-generated catch block
    System.out.println("******not instantiated");
    e.printStackTrace();
} catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (NullPointerException npe) {
    System.out.println("Hash-Aan mapping not found for aan " + aan);
    continue;
}
</code>```

答:
The code that worked for me:
ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
                            URLClassLoader classLoader = (URLClassLoader) currentThreadClassLoader.getSystemClassLoader();
                            cls = URLClassLoader.class;
                            Method m = cls.getDeclaredMethod("addURL", new Class[] { URL.class });
                            m.setAccessible(true);
                            URL ur = f.toURI().toURL();
                            m.invoke(classLoader, new Object[] { ur });

                            cls = Class.forName(rtomProperties.getPropertyValue("pojoPackageLocation").concat(".haan"),
                                    true, currentThreadClassLoader);

Body:
I am creating a class (named haan.java) at runtime of my java application.
    The object of this class is inserted in a Oracle Coherence Cache
    When I get an object(tempHaan) from the cache, it is of type haan. But when I try to invoke the method of this object(tempHaan), I am getting the error "java.lang.IllegalArgumentException: object is not an instance of declaring class"

Error occurs at the line "Object invoke = methodgetHash.invoke(tempHaan, null);"

PFB the code:

NamedCache cacheConn;
CacheFactory.ensureCluster();
NamedCache cacheConnHaan = CacheFactory.getCache("Haan");
Class cls = null;
File f = new File(rtomProperties.getPropertyValue("pojoToBuild"));

try {
    ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
    URLClassLoader cl = new URLClassLoader(new URL[] { f.toURI().toURL() },
        currentThreadClassLoader);
    Object tempHaan;
    cls = Class.forName(rtomProperties.getPropertyValue("pojoPackageLocation").concat(".haan"), true, cl);
    System.out.println("*************** the class is **********"
        + cls.newInstance().getClass().toString());
    System.out.println("******DONE LOADING");

    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class clazz = URLClassLoader.class;

    // Use reflection
    Method m = clazz.getDeclaredMethod("addURL", new Class[] { URL.class });
    m.setAccessible(true);
    URL ur = f.toURI().toURL();
    m.invoke(classLoader, new Object[] { ur });

    Thread.currentThread().getContextClassLoader().getResourceAsStream("context.xml");

    tempHaan = cacheConnHaan.get(aan);

    System.out.println("*************AFTER" + tempHaan.getClass().toString());

    System.out.println("*************tempHaan: "+tempHaan.toString());
    Class[] paramObject = new Class[1];
    paramObject[0] = Object.class;
    Method methodgetHash = null;
    Class noparams[] = {};
    methodgetHash = cls.getDeclaredMethod("getHash", noparams);
    Object temp = cls.newInstance();

    Object invoke = methodgetHash.invoke(tempHaan, null);
    key = (String) invoke;

    System.out.println("key for the record: " + key);
} catch (ClassNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (InstantiationException e) {
    // TODO Auto-generated catch block
    System.out.println("******not instantiated");
    e.printStackTrace();
} catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (NullPointerException npe) {
    System.out.println("Hash-Aan mapping not found for aan " + aan);
    continue;
}


问:
Recursive regex pattern
I'm struggling with a regex expression. Here is the text I'm working on:

```<code>* [[February 1]] – ''[[Brave New World]]'', a novel by [[Aldous Huxley]], is first published.
* [[February 2]]
** A general [[World Disarmament Conference]] begins in [[Geneva]]. The principal issue at the conference is the demand made by Germany for ''gleichberechtigung'' ("equality of status" i.e. abolishing Part V of the Treaty of Versailles, which had disarmed Germany) and the French demand for ''sécurité'' ("security" i.e. maintaining Part V).
** The [[League of Nations]] again recommends negotiations between the [[Republic of China (1912–49)|Republic of China]] and Japan.
** The [[Reconstruction Finance Corporation]] begins operations in Washington, D.C.
* [[February 4]]
** The [[1932 Winter Olympics]] open in [[Lake Placid, New York]].
** Japan occupies [[Harbin]], China.
* [[February 9]] – [[Junnosuke Inoue]], prominent Japanese businessman, banker and former governor of the Bank of Japan is assassinated by right-wing extremist group the League of Blood in the [[League of Blood Incident]].
* [[February 11]] – [[Pope Pius XI]] meets [[Benito Mussolini]] in [[Vatican City]].
</code>```

I would like to have a regex to match all lines which begin with an * which is followed by any number of lines beginning with **. Ideally, I would like to have each lines with ** in a group.

Here is the result I'd like to have:

```<code>> Match 1:
>> Group 1: "* [[February 2]]"

>> Group 2: "** A general [...] Part V)."

>> Group 3: "** The [[League of Nations]] [...] and Japan."

>> Group 4: "** The [[Reconstruction Finance Corporation]] begins operations in Washington, D.C."

> Match 2:
>> Group 1: "* [[February 4]]"

>> Group 2: "** The [[1932 Winter Olympics]] open in [[Lake Placid, New York]]."

>> Group 3: "** Japan occupies [[Harbin]], China."
</code>```

(I've put [...] for shortening purpose.)

Here is
I come to this pattern: /(*ANY)^\*{1} (.*)\n(?>(^\*{2}(.*?)\n)+)/gm, here is a link to regex101 where I test my regex: https://regex101.com/r/ubtnMg/1.

Here is a description of my pattern:
* (*ANY) to match any newline sequences because I'm not sure which newline character they use in text.
* ^\*{1} (.*)\n to match any line which begin with *, capture the text of the line until there is a newline.
* (?>(^\*{2}(.*?)\n)+) is the tricky part. It's supposed to match every line after ^\*{1} (.*)\n which begin with **, capture the text until the end of line in a group, and until it find a new line which begin with *

It actually give me this:

```<code>> Match 1: "* [[February 2]]
** A general [[World Disarmament Conference]] begins in [[Geneva]]. The principal issue at the conference is the demand made by Germany for ''gleichberechtigung'' ("equality of status" i.e. abolishing Part V of the Treaty of Versailles, which had disarmed Germany) and the French demand for ''sécurité'' ("security" i.e. maintaining Part V).
** The [[League of Nations]] again recommends negotiations between the [[Republic of China (1912–49)|Republic of China]] and Japan.
** The [[Reconstruction Finance Corporation]] begins operations in Washington, D.C."
>> Group 1: "[[February 2]]"

>> Group 2: "** The [[Reconstruction Finance Corporation]] begins operations in Washington, D.C."

>> Group 3: "The [[Reconstruction Finance Corporation]] begins operations in Washington, D.C."

> Match 2: "* [[February 4]]
** The [[1932 Winter Olympics]] open in [[Lake Placid, New York]].
** Japan occupies [[Harbin]], China."
>> Group 1: "[[February 4]]"

>> Group 2: "** Japan occupies [[Harbin]], China"

>> Group 3: " Japan occupies [[Harbin]], China."
</code>```

I hope I've been clear enough and you can help me with this. Don't hesitate to ask for more details.

答:
Thanks to the comment of Rawing, I've come across this solution:

First, I use this pattern: /(*ANY)^\*{1} (.*)\n(^\*{2}(.*?)\n)+/gm to match every block of text like this:

```<code>* [[February 2]]
** A general [[World Disarmament Conference]] begins in [[Geneva]]. The principal issue at the conference is the demand made by Germany for ''gleichberechtigung'' ("equality of status" i.e. abolishing Part V of the Treaty of Versailles, which had disarmed Germany) and the French demand for ''sécurité'' ("security" i.e. maintaining Part V).
** The [[League of Nations]] again recommends negotiations between the [[Republic of China (1912–49)|Republic of China]] and Japan.
** The [[Reconstruction Finance Corporation]] begins operations in Washington, D.C.
</code>```

Then I use this pattern to get the line which begin with *: /^\*{1}(.*)/g.
And I also use this pattern to get every line which begin with **: /^\*{2}(.*)$/gm

Title: Recursive regex pattern

Body:
I'm struggling with a regex expression. Here is the text I'm working on:

* [[February 1]] – ''[[Brave New World]]'', a novel by [[Aldous Huxley]], is first published.
* [[February 2]]
** A general [[World Disarmament Conference]] begins in [[Geneva]]. The principal issue at the conference is the demand made by Germany for ''gleichberechtigung'' ("equality of status" i.e. abolishing Part V of the Treaty of Versailles, which had disarmed Germany) and the French demand for ''sécurité'' ("security" i.e. maintaining Part V).
** The [[League of Nations]] again recommends negotiations between the [[Republic of China (1912–49)|Republic of China]] and Japan.
** The [[Reconstruction Finance Corporation]] begins operations in Washington, D.C.
* [[February 4]]
** The [[1932 Winter Olympics]] open in [[Lake Placid, New York]].
** Japan occupies [[Harbin]], China.
* [[February 9]] – [[Junnosuke Inoue]], prominent Japanese businessman, banker and former governor of the Bank of Japan is assassinated by right-wing extremist group the League of Blood in the [[League of Blood Incident]].
* [[February 11]] – [[Pope Pius XI]] meets [[Benito Mussolini]] in [[Vatican City]].

I would like to have a regex to match all lines which begin with an * which is followed by any number of lines beginning with **. Ideally, I would like to have each lines with ** in a group.

Here is the result I'd like to have:

> Match 1:
>> Group 1: "* [[February 2]]"

>> Group 2: "** A general [...] Part V)."

>> Group 3: "** The [[League of Nations]] [...] and Japan."

>> Group 4: "** The [[Reconstruction Finance Corporation]] begins operations in Washington, D.C."

> Match 2:
>> Group 1: "* [[February 4]]"

>> Group 2: "** The [[1932 Winter Olympics]] open in [[Lake Placid, New York]]."

>> Group 3: "** Japan occupies [[Harbin]], China."

(I've put [...] for shortening purpose.)

Here is
I come to this pattern: /(*ANY)^\*{1} (.*)\n(?>(^\*{2}(.*?)\n)+)/gm, here is a link to regex101 where I test my regex: https://regex101.com/r/ubtnMg/1.

Here is a description of my pattern:
* (*ANY) to match any newline sequences because I'm not sure which newline character they use in text.
* ^\*{1} (.*)\n to match any line which begin with *, capture the text of the line until there is a newline.
* (?>(^\*{2}(.*?)\n)+) is the tricky part. It's supposed to match every line after ^\*{1} (.*)\n which begin with **, capture the text until the end of line in a group, and until it find a new line which begin with *

It actually give me this:

> Match 1: "* [[February 2]]
** A general [[World Disarmament Conference]] begins in [[Geneva]]. The principal issue at the conference is the demand made by Germany for ''gleichberechtigung'' ("equality of status" i.e. abolishing Part V of the Treaty of Versailles, which had disarmed Germany) and the French demand for ''sécurité'' ("security" i.e. maintaining Part V).
** The [[League of Nations]] again recommends negotiations between the [[Republic of China (1912–49)|Republic of China]] and Japan.
** The [[Reconstruction Finance Corporation]] begins operations in Washington, D.C."
>> Group 1: "[[February 2]]"

>> Group 2: "** The [[Reconstruction Finance Corporation]] begins operations in Washington, D.C."

>> Group 3: "The [[Reconstruction Finance Corporation]] begins operations in Washington, D.C."

> Match 2: "* [[February 4]]
** The [[1932 Winter Olympics]] open in [[Lake Placid, New York]].
** Japan occupies [[Harbin]], China."
>> Group 1: "[[February 4]]"

>> Group 2: "** Japan occupies [[Harbin]], China"

>> Group 3: " Japan occupies [[Harbin]], China."

I hope I've been clear enough and you can help me with this. Don't hesitate to ask for more details.

Body:
Thanks to the comment of Rawing, I've come across this solution:

First, I use this pattern: /(*ANY)^\*{1} (.*)\n(^\*{2}(.*?)\n)+/gm to match every block of text like this:

* [[February 2]]
** A general [[World Disarmament Conference]] begins in [[Geneva]]. The principal issue at the conference is the demand made by Germany for ''gleichberechtigung'' ("equality of status" i.e. abolishing Part V of the Treaty of Versailles, which had disarmed Germany) and the French demand for ''sécurité'' ("security" i.e. maintaining Part V).
** The [[League of Nations]] again recommends negotiations between the [[Republic of China (1912–49)|Republic of China]] and Japan.
** The [[Reconstruction Finance Corporation]] begins operations in Washington, D.C.

Then I use this pattern to get the line which begin with *: /^\*{1}(.*)/g.
And I also use this pattern to get every line which begin with **: /^\*{2}(.*)$/gm


问:
Tornado: How to send default file if not found in StaticFileHandler
When a requested static file is not found, I want to send back a default file. eg. /images/dog.png -> /images/default.png.

After digging into the source code here.

```<code>if (os.path.isdir(absolute_path) and
    self.default_filename is not None):
</code>```

Setting default_filename is used for request like /images/ -> /images/default.png.

答:
Interesting validate_absolute_path function, why not override this to provide your default file if not exist ?

import tornado
import tornado.web
import tornado.ioloop

import os

DEFAULT_ABSPATH = os.path.dirname(os.path.abspath(__file__))

class StaticFileOrDefaultHandler(tornado.web.StaticFileHandler):

    def validate_absolute_path(self, root, absolute_path):
        try:
            return super(StaticFileOrDefaultHandler, self).validate_absolute_path(root, absolute_path)
        except tornado.web.HTTPError as e:
            if e.status_code == 404:
                return os.path.join(DEFAULT_ABSPATH, 'default.png')
            raise e

app = tornado.web.Application([
    (r'/(.*)', StaticFileOrDefaultHandler, { 'path': DEFAULT_ABSPATH }),
])

if __name__ == '__main__':
    app.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
```

edit

To avoid override HTTPError(403) check with the status_code if the raised error is a 404.

Body:
When a requested static file is not found, I want to send back a default file. eg. /images/dog.png -> /images/default.png.

After digging into the source code here.

if (os.path.isdir(absolute_path) and
    self.default_filename is not None):

Setting default_filename is used for request like /images/ -> /images/default.png.

Body:
Interesting validate_absolute_path function, why not override this to provide your default file if not exist ?

import tornado
import tornado.web
import tornado.ioloop

import os

DEFAULT_ABSPATH = os.path.dirname(os.path.abspath(__file__))

class StaticFileOrDefaultHandler(tornado.web.StaticFileHandler):

    def validate_absolute_path(self, root, absolute_path):
        try:
            return super(StaticFileOrDefaultHandler, self).validate_absolute_path(root, absolute_path)
        except tornado.web.HTTPError as e:
            if e.status_code == 404:
                return os.path.join(DEFAULT_ABSPATH, 'default.png')
            raise e

app = tornado.web.Application([
    (r'/(.*)', StaticFileOrDefaultHandler, { 'path': DEFAULT_ABSPATH }),
])

if __name__ == '__main__':
    app.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

edit

To avoid override HTTPError(403) check with the status_code if the raised error is a 404.


问:
message queue thinking
We have implemented a message queue by using C# Queue. We know we only have ONE consumer to take available message out from the queue for processing with a while loop. We also know there is only ONE producer to put message onto the queue.

We have a lock on above message queue to make sure the consumer and producer cannot access the queue simultaneously.

My question is is that lock necessary? If the Queue increase its Count property AFTER an item is actually added and if the consumer check the Count before retrieve, the consumer should get a complete message item even we don't have that lock. Right? So we will not face a partial message item issue. Then we can get rid of that lock?

That lock will slow down the system and occasionally we can see the retrieve thread is blocked for a while because we have a very heavy producer.

EDIT:

Unfortunately we are using .Net 3.5.

答:
The real problem is that internal data structures of the queue may be being mutated whilst an enqueue or dequeue is happening, and during this period the data structure is in an indeterminate state from the point-of-view of another thread.

For example, an enqueue may require internal data structures to be expanded, where a new structure is created, and old items are copied form an old structure to a new structure. There will be many steps involved in this process where at any time it would be dangerous for another thread to access the queue as the operations are not complete.

Therefore, during enqueue\dequeue you have to lock to make these operation appear to be logically atomic.

You may try the new ConcurrentQueue class in .Net 4.0 as this possibly has better performance characteristics as it uses a non-locking algorithm.

Title: message queue thinking


问:
Why does my getColor function always return false?
I have a function that gets user input and its supposed to validate the user input from a list of options. The last part of the function, where I cycle through results and return true, if there's a true boolean value in results doesn't seem to be working properly. It keeps returning false. </p>

```<code>    function getColor(){
        //gets user color and makes all characters lower case;
        let color=prompt("Pick a color, your options are white, yellow, brown, black, tan, red, orange").toLowerCase();

        //stores acceptable colors
        let acceptableColors=["white","yellow", "brown", "black","tan","red","orange"];

        function validate(){
            let results=[];
            for(let i=0; i<acceptableColors.length; i++){//loops through and stores a true value in results if user color is in list of acceptableColors
                    if(color===acceptableColors[i]){
                        results.push(true);
                    }
                    else{
                        results.push(false);
                    }

            }

            results.forEach(function(item){//loops through results to search for true value and returns true, if it finds one
                if(item===true){
                    return true
                }
            });
             return false;//returns false if user entered invalid color

        }
        return validate();

    }
</code>```

答:
Here is a simple way: </p>

```<code>function validate(){
        let results=[];
        for(let i=0; i<acceptableColors.length; i++){//loops through and stores a true value in results if user color is in list of acceptableColors
                if(color===acceptableColors[i]){
                    return true;
                }
        }
        return false;//returns false if user entered invalid color

    }
</code>```

Body:
I have a function that gets user input and its supposed to validate the user input from a list of options. The last part of the function, where I cycle through results and return true, if there's a true boolean value in results doesn't seem to be working properly. It keeps returning false.

    function getColor(){
        //gets user color and makes all characters lower case;
        let color=prompt("Pick a color, your options are white, yellow, brown, black, tan, red, orange").toLowerCase();

        //stores acceptable colors
        let acceptableColors=["white","yellow", "brown", "black","tan","red","orange"];

        function validate(){
            let results=[];
            for(let i=0; i<acceptableColors.length; i++){//loops through and stores a true value in results if user color is in list of acceptableColors
                    if(color===acceptableColors[i]){
                        results.push(true);
                    }
                    else{
                        results.push(false);
                    }

            }

            results.forEach(function(item){//loops through results to search for true value and returns true, if it finds one
                if(item===true){
                    return true
                }
            });
             return false;//returns false if user entered invalid color

        }
        return validate();

    }

Body:
Here is a simple way:

function validate(){
        let results=[];
        for(let i=0; i<acceptableColors.length; i++){//loops through and stores a true value in results if user color is in list of acceptableColors
                if(color===acceptableColors[i]){
                    return true;
                }
        }
        return false;//returns false if user entered invalid color

    }


答:
Problem is when a item is removed next item will not be removed if the condition is true. For that you should decrement the index when you remove an item.

Change as follow.

```<code>ArrayList<ArrayList<Record>> BS = new ArrayList<>();
.
.// some codes
.

for (int j = 0; j < BS.size(); j++) {
    if(BS.get(j) == null || BS.get(j).size() == 0){
        BS.remove(j);
        j--;
    }
}
</code>```

Title: Remove item from ArrayList<ArrayList<T>>>

Body:
Problem is when a item is removed next item will not be removed if the condition is true. For that you should decrement the index when you remove an item.

Change as follow.

ArrayList<ArrayList<Record>> BS = new ArrayList<>();
.
.// some codes
.

for (int j = 0; j < BS.size(); j++) {
    if(BS.get(j) == null || BS.get(j).size() == 0){
        BS.remove(j);
        j--;
    }
}


问:
django ajax MultiValueDictKeyError
I am receiving this error:

```<code>MultiValueDictKeyError at /orders/ajax/add_order_line
"'cart'"
</code>```

Here is my script

```<code>var cart = {
    0: {
        id: "1",
        quantity: 50
    }
}

$.ajax({
    url: myURL,
    type: "post",
    data: {cart: cart},
    success: function() {},
    error: function(){}
});
</code>```

Meanwhile in my django views, the error was found in this line:

```<code>def something(request):
    cart = request.POST['cart']
</code>```

答:
Your data is a nested array, so you can't send it using the default default application/x-www-form-urlencoded content type.

You can send the data as json:

```<code>$.ajax({
    url: myURL,
    type: "post",
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({cart: cart}),
    success: function() {},
    error: function(){}
});
</code>```

Then in your view, you have to load the json string from request.body instead of using request.POST (which is for form-encoded data only).

```<code>import json

def my_view(reqest):
    data = json.loads(request.body.decode('utf-8'))
    cart = data.get('cart')
</code>```

Title: django ajax MultiValueDictKeyError

Body:
I am receiving this error:

MultiValueDictKeyError at /orders/ajax/add_order_line
"'cart'"

Here is my script

var cart = {
    0: {
        id: "1",
        quantity: 50
    }
}

$.ajax({
    url: myURL,
    type: "post",
    data: {cart: cart},
    success: function() {},
    error: function(){}
});

Meanwhile in my django views, the error was found in this line:

def something(request):
    cart = request.POST['cart']

Body:
Your data is a nested array, so you can't send it using the default default application/x-www-form-urlencoded content type.

You can send the data as json:

$.ajax({
    url: myURL,
    type: "post",
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({cart: cart}),
    success: function() {},
    error: function(){}
});

Then in your view, you have to load the json string from request.body instead of using request.POST (which is for form-encoded data only).

import json

def my_view(reqest):
    data = json.loads(request.body.decode('utf-8'))
    cart = data.get('cart')


问:
How to play the video from raw folder using intent?
I am getting the exception as:

Android.Content.ActivityNotFoundException: No Activity found to
  handle Intent.

```<code>var video = Android.Net.Uri.Parse(@"android.resource://" +
Forms.Context.PackageName + "/Raw/" + Resource.Raw.sampleVideo);
Intent intent = new Intent(Intent.ActionView, video);
intent.SetDataAndType(video,"video/*");
Forms.Context.StartActivity(intent);
</code>```

答: Chech whether you have declared your intent in AndroidManifest file. For playing video you can check this url https://stackoverflow.com/a/16020402/5376678</p>

Body:
I am getting the exception as:

Android.Content.ActivityNotFoundException: No Activity found to
  handle Intent.

var video = Android.Net.Uri.Parse(@"android.resource://" +
Forms.Context.PackageName + "/Raw/" + Resource.Raw.sampleVideo);
Intent intent = new Intent(Intent.ActionView, video);
intent.SetDataAndType(video,"video/*");
Forms.Context.StartActivity(intent);


问:
How can I do my logic in `http://127.0.0.1:8000/rest-auth/login/` of `django-rest-auth`?
How can I do my logic in http://127.0.0.1:8000/rest-auth/login/ of django-rest-auth

When I login it is like below and the url is provide by django-rest-auth.

How can I write my own logic when I login?

答:
That page have information about which api endpoints you can override
http://django-rest-auth.readthedocs.io/en/latest/configuration.html

for login you can use:
'LOGIN_SERIALIZER': 'path.to.custom.LoginSerializer',
For realize custom registration logic and serializer I'm recommend to you read documentation of http://www.django-rest-framework.org/.

1. set it in your settings.py as shown in the docs,

```<code>REST_AUTH_SERIALIZERS = {
    'LOGIN_SERIALIZER': 'path.to.custom.LoginSerializer',
    ...
}
</code>```

2. Create Serailizer class

This is default Login Serializer class (copy and paste to url used above and edit as you want )

Note: You can use model Serializer also

```<code>class LoginSerializer(serializers.Serializer):
    username = serializers.CharField(required=False, allow_blank=True)
    email = serializers.EmailField(required=False, allow_blank=True)
    password = serializers.CharField(style={'input_type': 'password'})

    def _validate_email(self, email, password):
        user = None

        if email and password:
            user = authenticate(email=email, password=password)
        else:
            msg = _('Must include "email" and "password".')
            raise exceptions.ValidationError(msg)

        return user

    def _validate_username(self, username, password):
        user = None

        if username and password:
            user = authenticate(username=username, password=password)
        else:
            msg = _('Must include "username" and "password".')
            raise exceptions.ValidationError(msg)

        return user

    def _validate_username_email(self, username, email, password):
        user = None

        if email and password:
            user = authenticate(email=email, password=password)
        elif username and password:
            user = authenticate(username=username, password=password)
        else:
            msg = _('Must include either "username" or "email" and "password".')
            raise exceptions.ValidationError(msg)

        return user

    def validate(self, attrs):
        username = attrs.get('username')
        email = attrs.get('email')
        password = attrs.get('password')

        user = None

        if 'allauth' in settings.INSTALLED_APPS:
            from allauth.account import app_settings

            # Authentication through email
            if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.EMAIL:
                user = self._validate_email(email, password)

            # Authentication through username
            if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.USERNAME:
                user = self._validate_username(username, password)

            # Authentication through either username or email
            else:
                user = self._validate_username_email(username, email, password)

        else:
            # Authentication without using allauth
            if email:
                try:
                    username = UserModel.objects.get(email__iexact=email).get_username()
                except UserModel.DoesNotExist:
                    pass

            if username:
                user = self._validate_username_email(username, '', password)

        # Did we get back an active user?
        if user:
            if not user.is_active:
                msg = _('User account is disabled.')
                raise exceptions.ValidationError(msg)
        else:
            msg = _('Unable to log in with provided credentials.')
            raise exceptions.ValidationError(msg)

        # If required, is the email verified?
        if 'rest_auth.registration' in settings.INSTALLED_APPS:
            from allauth.account import app_settings
            if app_settings.EMAIL_VERIFICATION == app_settings.EmailVerificationMethod.MANDATORY:
                email_address = user.emailaddress_set.get(email=user.email)
                if not email_address.verified:
                    raise serializers.ValidationError(_('E-mail is not verified.'))

        attrs['user'] = user
        return attrs
</code>```

Body:
That page have information about which api endpoints you can override
http://django-rest-auth.readthedocs.io/en/latest/configuration.html

for login you can use:
'LOGIN_SERIALIZER': 'path.to.custom.LoginSerializer',
For realize custom registration logic and serializer I'm recommend to you read documentation of http://www.django-rest-framework.org/.

1. set it in your settings.py as shown in the docs,

REST_AUTH_SERIALIZERS = {
    'LOGIN_SERIALIZER': 'path.to.custom.LoginSerializer',
    ...
}

2. Create Serailizer class

This is default Login Serializer class (copy and paste to url used above and edit as you want )

Note: You can use model Serializer also

class LoginSerializer(serializers.Serializer):
    username = serializers.CharField(required=False, allow_blank=True)
    email = serializers.EmailField(required=False, allow_blank=True)
    password = serializers.CharField(style={'input_type': 'password'})

    def _validate_email(self, email, password):
        user = None

        if email and password:
            user = authenticate(email=email, password=password)
        else:
            msg = _('Must include "email" and "password".')
            raise exceptions.ValidationError(msg)

        return user

    def _validate_username(self, username, password):
        user = None

        if username and password:
            user = authenticate(username=username, password=password)
        else:
            msg = _('Must include "username" and "password".')
            raise exceptions.ValidationError(msg)

        return user

    def _validate_username_email(self, username, email, password):
        user = None

        if email and password:
            user = authenticate(email=email, password=password)
        elif username and password:
            user = authenticate(username=username, password=password)
        else:
            msg = _('Must include either "username" or "email" and "password".')
            raise exceptions.ValidationError(msg)

        return user

    def validate(self, attrs):
        username = attrs.get('username')
        email = attrs.get('email')
        password = attrs.get('password')

        user = None

        if 'allauth' in settings.INSTALLED_APPS:
            from allauth.account import app_settings

            # Authentication through email
            if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.EMAIL:
                user = self._validate_email(email, password)

            # Authentication through username
            if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.USERNAME:
                user = self._validate_username(username, password)

            # Authentication through either username or email
            else:
                user = self._validate_username_email(username, email, password)

        else:
            # Authentication without using allauth
            if email:
                try:
                    username = UserModel.objects.get(email__iexact=email).get_username()
                except UserModel.DoesNotExist:
                    pass

            if username:
                user = self._validate_username_email(username, '', password)

        # Did we get back an active user?
        if user:
            if not user.is_active:
                msg = _('User account is disabled.')
                raise exceptions.ValidationError(msg)
        else:
            msg = _('Unable to log in with provided credentials.')
            raise exceptions.ValidationError(msg)

        # If required, is the email verified?
        if 'rest_auth.registration' in settings.INSTALLED_APPS:
            from allauth.account import app_settings
            if app_settings.EMAIL_VERIFICATION == app_settings.EmailVerificationMethod.MANDATORY:
                email_address = user.emailaddress_set.get(email=user.email)
                if not email_address.verified:
                    raise serializers.ValidationError(_('E-mail is not verified.'))

        attrs['user'] = user
        return attrs