问:
SQL select students logins
I have a table STUDENT_LAST_LOGIN, which contains data about students last logins.

```<code>ID  STUDENT_ID   DATE               TIME
1    A          2020-02-01 12:00    15 MIN
2    B          2020-02-02 12:00    45 MIN
3    C          2020-02-03 12:00    25 MIN
</code>```

In addition there is STUDENT_LOGIN table, which contains data about students all logins.

```<code>ID  STUDENT_ID   DATE               TIME
1    A          2020-02-01 12:00    15 MIN
4    A          2020-01-01 14:00    33 MIN
2    B          2020-02-02 12:00    45 MIN
5    B          2020-01-02 13:30    47 MIN
10   B          2020-01-03 13:30    27 MIN
6    B          2020-01-02 10:00    44 MIN
3    C          2020-02-03 12:00    25 MIN
7    C          2020-01-03 10:00    12 MIN
8    C          2020-01-03 18:00    56 MIN
9    C          2020-01-04 12:00    88 MIN
</code>```

As a result I need to get something like this:

```<code>STUDENT_ID    LAST_LOGIN         LAST_LOGIN_ONE_MONTH_AGO    TIME     TIME_ONE_MONTH_AGO
A            2020-02-01 12:00    2020-01-01 14:00            15 min   33 min
B            2020-02-02 12:00    2020-01-02 13:30            15 min   47 min
C            2020-02-03 12:00    2020-01-03 18:00            25 min   56 min
</code>```

Can you help me write this?

答:
I can only speculate that you want the most recent login and then the most recent login from the calendar month before that.  I would suggest conditional aggregation:

```<code>select sll.student_id,
       max(case when month_seqnum = 1 then last_login end),
       max(case when month_seqnum = 2 then last_login end),
       max(case when month_seqnum = 1 then time end),
       max(case when month_seqnum = 2 then time end)
from (select sll.*,
             row_number() over (partition by student_id, to_char(date, 'YYYY-MM')
                                order by date desc
                               ) as seqnum,
             dense_rank() over (partition by student_id order by to_char(date, 'YYYY-MM')) as month_seqnum
      from student_last_login sll
     ) sll
where month_seqnum in (1, 2) and seqnum = 1
group by student_id;
</code>```

I think this returns the values that you specify.

Title: SQL select students logins

Body:
I have a table STUDENT_LAST_LOGIN, which contains data about students last logins.

ID  STUDENT_ID   DATE               TIME
1    A          2020-02-01 12:00    15 MIN
2    B          2020-02-02 12:00    45 MIN
3    C          2020-02-03 12:00    25 MIN

In addition there is STUDENT_LOGIN table, which contains data about students all logins.

ID  STUDENT_ID   DATE               TIME
1    A          2020-02-01 12:00    15 MIN
4    A          2020-01-01 14:00    33 MIN
2    B          2020-02-02 12:00    45 MIN
5    B          2020-01-02 13:30    47 MIN
10   B          2020-01-03 13:30    27 MIN
6    B          2020-01-02 10:00    44 MIN
3    C          2020-02-03 12:00    25 MIN
7    C          2020-01-03 10:00    12 MIN
8    C          2020-01-03 18:00    56 MIN
9    C          2020-01-04 12:00    88 MIN

As a result I need to get something like this:

STUDENT_ID    LAST_LOGIN         LAST_LOGIN_ONE_MONTH_AGO    TIME     TIME_ONE_MONTH_AGO
A            2020-02-01 12:00    2020-01-01 14:00            15 min   33 min
B            2020-02-02 12:00    2020-01-02 13:30            15 min   47 min
C            2020-02-03 12:00    2020-01-03 18:00            25 min   56 min

Can you help me write this?

Body:
I can only speculate that you want the most recent login and then the most recent login from the calendar month before that.  I would suggest conditional aggregation:

select sll.student_id,
       max(case when month_seqnum = 1 then last_login end),
       max(case when month_seqnum = 2 then last_login end),
       max(case when month_seqnum = 1 then time end),
       max(case when month_seqnum = 2 then time end)
from (select sll.*,
             row_number() over (partition by student_id, to_char(date, 'YYYY-MM')
                                order by date desc
                               ) as seqnum,
             dense_rank() over (partition by student_id order by to_char(date, 'YYYY-MM')) as month_seqnum
      from student_last_login sll
     ) sll
where month_seqnum in (1, 2) and seqnum = 1
group by student_id;

I think this returns the values that you specify.


来源: stackexchange

问:
JSoup, Extract specific text or image-link from website
This is in my HTML and I need to extract either the link of the image or the fileId to create a link.

```<code>{
      "created": "2018-11-06T06:46:21.181Z",
      "inRiverId": "58515",
      "mediaInformation": {
        "bildid": 67708,
        "description": "ABC",
        "excelImportField": "ABC",
        "fileId": "41964",
        "filename": "ABC",
        "imageAccess": true,
        "imageItemType": "ABC",
        "imageStatus": "ABC",
        "imageType": "ABC",
        "itcl": "ABC",
        "photographer": "ABC",
        "projectName": "ABC",
        "projectType": "Webb",
        "type": "Bild",
        "url": "https://static.john.com/images/products/41964.jpg"
      },
</code>```

The fileId is dynamic, therefor is the link also dynamic depending on which article I'm visiting on my website. The link will always start with "https://static.john.com/images/products/" and then it adds the fileId and ".jpeg" automatically.

The code above is of course just a piece of the code on the website, so there is more images on it, so it needs to extract it specifically.

The dream would be if Jsoup could get this output:
"https://static.john.com/images/products/" + "fileId" + ".jpeg"

I'm a beginner in Android Studio, and crazy-new to JSoup.

答:
Bad news. Jsoup can't do this. Jsoup parses only HTML but this is a JSON fragment which will be used by Javascript after the page is loaded in the browser.
However my idea is to get this URL using regular expressions so try using this code:

```<code>String json = "{\n" +
        "      \"created\": \"2018-11-06T06:46:21.181Z\",\n" +
        "      \"inRiverId\": \"58515\",\n" +
        "      \"mediaInformation\": {\n" +
        "        \"bildid\": 67708,\n" +
        "        \"description\": \"ABC\",\n" +
        "        \"excelImportField\": \"ABC\",\n" +
        "        \"fileId\": \"41964\",\n" +
        "        \"filename\": \"ABC\",\n" +
        "        \"imageAccess\": true,\n" +
        "        \"imageItemType\": \"ABC\",\n" +
        "        \"imageStatus\": \"ABC\",\n" +
        "        \"imageType\": \"ABC\",\n" +
        "        \"itcl\": \"ABC\",\n" +
        "        \"photographer\": \"ABC\",\n" +
        "        \"projectName\": \"ABC\",\n" +
        "        \"projectType\": \"Webb\",\n" +
        "        \"type\": \"Bild\",\n" +
        "        \"url\": \"https://static.john.com/images/products/41964.jpg\"\n" +
        "      },";
Pattern p = Pattern.compile("\"url\": \"(https://static.john.com/images/products/\\d+.jp[e]*g)\"");
Matcher m = p.matcher(json);
if (m.find()) {
    String imageUrl = m.group(1);
    System.out.println(imageUrl);
}
</code>```

and the output is:  https://static.john.com/images/products/41964.jpg
In your case instead of json variable you should use document.html().

ContentLicense: CC BY-SA 4.0

Body:
This is in my HTML and I need to extract either the link of the image or the fileId to create a link.

{
      "created": "2018-11-06T06:46:21.181Z",
      "inRiverId": "58515",
      "mediaInformation": {
        "bildid": 67708,
        "description": "ABC",
        "excelImportField": "ABC",
        "fileId": "41964",
        "filename": "ABC",
        "imageAccess": true,
        "imageItemType": "ABC",
        "imageStatus": "ABC",
        "imageType": "ABC",
        "itcl": "ABC",
        "photographer": "ABC",
        "projectName": "ABC",
        "projectType": "Webb",
        "type": "Bild",
        "url": "https://static.john.com/images/products/41964.jpg"
      },

The fileId is dynamic, therefor is the link also dynamic depending on which article I'm visiting on my website. The link will always start with "https://static.john.com/images/products/" and then it adds the fileId and ".jpeg" automatically.

The code above is of course just a piece of the code on the website, so there is more images on it, so it needs to extract it specifically.

The dream would be if Jsoup could get this output:
"https://static.john.com/images/products/" + "fileId" + ".jpeg"

I'm a beginner in Android Studio, and crazy-new to JSoup.

Body:
Bad news. Jsoup can't do this. Jsoup parses only HTML but this is a JSON fragment which will be used by Javascript after the page is loaded in the browser.
However my idea is to get this URL using regular expressions so try using this code:

String json = "{\n" +
        "      \"created\": \"2018-11-06T06:46:21.181Z\",\n" +
        "      \"inRiverId\": \"58515\",\n" +
        "      \"mediaInformation\": {\n" +
        "        \"bildid\": 67708,\n" +
        "        \"description\": \"ABC\",\n" +
        "        \"excelImportField\": \"ABC\",\n" +
        "        \"fileId\": \"41964\",\n" +
        "        \"filename\": \"ABC\",\n" +
        "        \"imageAccess\": true,\n" +
        "        \"imageItemType\": \"ABC\",\n" +
        "        \"imageStatus\": \"ABC\",\n" +
        "        \"imageType\": \"ABC\",\n" +
        "        \"itcl\": \"ABC\",\n" +
        "        \"photographer\": \"ABC\",\n" +
        "        \"projectName\": \"ABC\",\n" +
        "        \"projectType\": \"Webb\",\n" +
        "        \"type\": \"Bild\",\n" +
        "        \"url\": \"https://static.john.com/images/products/41964.jpg\"\n" +
        "      },";
Pattern p = Pattern.compile("\"url\": \"(https://static.john.com/images/products/\\d+.jp[e]*g)\"");
Matcher m = p.matcher(json);
if (m.find()) {
    String imageUrl = m.group(1);
    System.out.println(imageUrl);
}

and the output is:  https://static.john.com/images/products/41964.jpg
In your case instead of json variable you should use document.html().


问:
IndentationError: unindent does not match any outer indent
we have a python code as following, surprisingly, it encounters error when run. It say "IndentationError: unindent does not match any outer indentation level"

physOptions=["doStage0","doacggh"]
for po in physOptions:
    if 'doStage0' in po:
        acstage= True
        print "doing stage0"
    elif 'doacttbar' in po:
        acttbar= True
        print "doing tth AC ttbar"
    elif 'doacggh' in po:
        acggh= True
        print "doing tth AC ggH"
    elif 'singlemu' in po:
        singlemu= True
        print "doing single mu"
```

<p>But I change the code into below format, it works, why?</p>

<pre class="lang-py prettyprint-override"><code>physOptions=["doStage0","doacggh"]
for po in physOptions:
    if 'doStage0' in po:
        acstage= True
        print "doing stage0"
    elif 'doacttbar' in po:
        acttbar= True
        print "doing tth AC ttbar"
    elif 'doacggh' in po:
        acggh= True
            print "doing tth AC ggH"
    elif 'singlemu' in po:
        singlemu= True
        print "doing single mu"
</code>```

答:
please read article about indentation in Python:
    https://docs.python.org/2.0/ref/indentation.html
    Indentation in Python it's like {} in other languages.</p>

```<code>physOptions=["doStage0","doacggh"]
for po in physOptions:
    if 'doStage0' in po:
        acstage= True
        print "doing stage0"
    elif 'doacttbar' in po:
        acttbar= True
        print "doing tth AC ttbar"
    elif 'doacggh' in po:
        acggh= True
# line bellow not will be works because it should be on the same level indentation
            print "doing tth AC ggH"
    elif 'singlemu' in po:
        singlemu= True
        print "doing single mu"

</code>```

Body:
we have a python code as following, surprisingly, it encounters error when run. It say "IndentationError: unindent does not match any outer indentation level"

physOptions=["doStage0","doacggh"]
for po in physOptions:
    if 'doStage0' in po:
        acstage= True
        print "doing stage0"
    elif 'doacttbar' in po:
        acttbar= True
        print "doing tth AC ttbar"
    elif 'doacggh' in po:
        acggh= True
        print "doing tth AC ggH"
    elif 'singlemu' in po:
        singlemu= True
        print "doing single mu"

But I change the code into below format, it works, why?

physOptions=["doStage0","doacggh"]
for po in physOptions:
    if 'doStage0' in po:
        acstage= True
        print "doing stage0"
    elif 'doacttbar' in po:
        acttbar= True
        print "doing tth AC ttbar"
    elif 'doacggh' in po:
        acggh= True
            print "doing tth AC ggH"
    elif 'singlemu' in po:
        singlemu= True
        print "doing single mu"

Body:
please read article about indentation in Python:
    https://docs.python.org/2.0/ref/indentation.html
    Indentation in Python it's like {} in other languages.

physOptions=["doStage0","doacggh"]
for po in physOptions:
    if 'doStage0' in po:
        acstage= True
        print "doing stage0"
    elif 'doacttbar' in po:
        acttbar= True
        print "doing tth AC ttbar"
    elif 'doacggh' in po:
        acggh= True
# line bellow not will be works because it should be on the same level indentation
            print "doing tth AC ggH"
    elif 'singlemu' in po:
        singlemu= True
        print "doing single mu"


问:
EF Core queries all columns in SQL when mapping to object in Select
While trying to organize some data access code using EF Core I noticed that the generated queries were worse than before, they now queried columns that were not needed. The basic query is just selecting from one table and mapping a subset of columns to a DTO. But after rewriting it now all columns are fetched, not just the ones in the DTO.

I created a minimal example with some queries that show the problem:

```<code>ctx.Items.ToList();
// SELECT i."Id", i."Property1", i."Property2", i."Property3" FROM "Items" AS i

ctx.Items.Select(x => new
{
  Id = x.Id,
  Property1 = x.Property1
}
).ToList();
// SELECT i."Id", i."Property1" FROM "Items" AS i

ctx.Items.Select(x => new MinimalItem
{
  Id = x.Id,
  Property1 = x.Property1
}
).ToList();
// SELECT i."Id", i."Property1" FROM "Items" AS i

ctx.Items.Select(
  x => x.MapToMinimalItem()
).ToList();
// SELECT i."Id", i."Property1", i."Property2", i."Property3" FROM "Items" AS i

ctx.Items.Select(
  x => new MinimalItem(x)
).ToList();

// SELECT i."Id", i."Property1", i."Property2", i."Property3" FROM "Items" AS i
</code>```

The objects are defined like this:

```<code>  public class Item
  {
    public int Id { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }

  }

  public class MinimalItem
  {
    public MinimalItem() { }

    public MinimalItem(Item source)
    {
      Id = source.Id;
      Property1 = source.Property1;
    }
    public int Id { get; set; }
    public string Property1 { get; set; }
  }

  public static class ItemExtensionMethods
  {
    public static MinimalItem MapToMinimalItem(this Item source)
    {
      return new MinimalItem
      {
        Id = source.Id,
        Property1 = source.Property1
      };
    }
  }
</code>```

The first query queries all columns as intended, and the second query with an anonymous object only queries the selected queries, that works all fine. Using my MinimalItem DTO also works as long as it is created directly in the Select method. But the last two queries fetch all columns even though they do exactly the same thing as the third query, just moved to a constructor or an extension method, respectively.

Obviously EF Core can't follow this code and determine that it only needs the two columns if I move it out of the Select method. But I'd really like to do that to be able to reuse the mapping code, and make the actual query code easier to read. How can I extract this kind of straightforward mapping code without making EF Core inefficiently fetching all columns all the time?

答:
Entity Framework does not know anything about your MapToMinimalItem method and how to translate it into SQL, so it fetches whole entity and performs the Select on the client side.

If you take a closer look at the EF LINQ method signatures, you will see, that IQueryable operates with Expression's of Func (Select for example) instead of Funcs as it's IEnumerable counterpart, so underlying provider could analyze the code and generate what is needed (SQL in this case).

So if you want to move the projection code into separate method this method should return Expression, so EF could transform it into SQL. For example:

```<code>public static class ItemExtensionMethods
{
    public static readonly Expression<Func<Item, MinimalItem>> MapToMinimalItemExpr =
        source => new MinimalItem
        {
            Id = source.Id,
            Property1 = source.Property1
        };
}
</code>```

though it will have limited usability caused you will not able to reuse it nested projections, only in simple like this:

```<code>ctx.Items.Select(ItemExtensionMethods.MapToMinimalItemExpr)
</code>```

Body:
While trying to organize some data access code using EF Core I noticed that the generated queries were worse than before, they now queried columns that were not needed. The basic query is just selecting from one table and mapping a subset of columns to a DTO. But after rewriting it now all columns are fetched, not just the ones in the DTO.

I created a minimal example with some queries that show the problem:

ctx.Items.ToList();
// SELECT i."Id", i."Property1", i."Property2", i."Property3" FROM "Items" AS i

ctx.Items.Select(x => new
{
  Id = x.Id,
  Property1 = x.Property1
}
).ToList();
// SELECT i."Id", i."Property1" FROM "Items" AS i

ctx.Items.Select(x => new MinimalItem
{
  Id = x.Id,
  Property1 = x.Property1
}
).ToList();
// SELECT i."Id", i."Property1" FROM "Items" AS i

ctx.Items.Select(
  x => x.MapToMinimalItem()
).ToList();
// SELECT i."Id", i."Property1", i."Property2", i."Property3" FROM "Items" AS i

ctx.Items.Select(
  x => new MinimalItem(x)
).ToList();

// SELECT i."Id", i."Property1", i."Property2", i."Property3" FROM "Items" AS i

The objects are defined like this:

  public class Item
  {
    public int Id { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }

  }

  public class MinimalItem
  {
    public MinimalItem() { }

    public MinimalItem(Item source)
    {
      Id = source.Id;
      Property1 = source.Property1;
    }
    public int Id { get; set; }
    public string Property1 { get; set; }
  }

  public static class ItemExtensionMethods
  {
    public static MinimalItem MapToMinimalItem(this Item source)
    {
      return new MinimalItem
      {
        Id = source.Id,
        Property1 = source.Property1
      };
    }
  }

The first query queries all columns as intended, and the second query with an anonymous object only queries the selected queries, that works all fine. Using my MinimalItem DTO also works as long as it is created directly in the Select method. But the last two queries fetch all columns even though they do exactly the same thing as the third query, just moved to a constructor or an extension method, respectively.

Obviously EF Core can't follow this code and determine that it only needs the two columns if I move it out of the Select method. But I'd really like to do that to be able to reuse the mapping code, and make the actual query code easier to read. How can I extract this kind of straightforward mapping code without making EF Core inefficiently fetching all columns all the time?

Body:
Entity Framework does not know anything about your MapToMinimalItem method and how to translate it into SQL, so it fetches whole entity and performs the Select on the client side.

If you take a closer look at the EF LINQ method signatures, you will see, that IQueryable operates with Expression's of Func (Select for example) instead of Funcs as it's IEnumerable counterpart, so underlying provider could analyze the code and generate what is needed (SQL in this case).

So if you want to move the projection code into separate method this method should return Expression, so EF could transform it into SQL. For example:

public static class ItemExtensionMethods
{
    public static readonly Expression<Func<Item, MinimalItem>> MapToMinimalItemExpr =
        source => new MinimalItem
        {
            Id = source.Id,
            Property1 = source.Property1
        };
}

though it will have limited usability caused you will not able to reuse it nested projections, only in simple like this:

ctx.Items.Select(ItemExtensionMethods.MapToMinimalItemExpr)


问:
One-hot encoding using model.matrix
There is something I do not understand in model.matrix. When I enter a single binary variable without an intercept it returns two levels. </p>

```<code>> temp.data <- data.frame('x' = sample(c('A', 'B'), 1000, replace = TRUE))
> temp.data.table <- model.matrix( ~ 0 + x, data = temp.data)
> head(temp.data.table)
  xA xB
1  1  0
2  0  1
3  0  1
4  0  1
5  1  0
6  0  1
</code>```

<p>However, when I enter another binary level, it creates only 3 columns. Why is that? What makes the behavior of the function suddenly different? and how can I avoid it? </p>

```<code>> temp.data <- data.frame('x' = sample(c('A', 'B'), 1000, replace = TRUE),
+                         'y' = sample(c('J', 'D'), 1000, replace = TRUE))
> temp.data.table <- model.matrix( ~ 0 + x + y, data = temp.data)
> head(temp.data.table)
  xA xB yJ
1  0  1  0
2  0  1  1
3  0  1  1
4  0  1  0
5  1  0  1
6  0  1  0
</code>```

答:
You need to work with factors and set the contrasts to FALSE. Try this:

```<code>n <- 10
temp.data <- data.frame('x'=sample(c('A', 'B'), n, replace=TRUE),
                        'y'=factor(sample(c('J', 'D'), n, replace=TRUE)))
model.matrix( ~ 0 + x + y, data=temp.data,
              contrasts=list(y=contrasts(temp.data$y, contrasts=FALSE)))

#    xA xB yD yJ
# 1   0  1  1  0
# 2   1  0  0  1
# 3   0  1  1  0
# 4   1  0  0  1
# 5   0  1  0  1
# 6   1  0  1  0
# 7   1  0  1  0
# 8   0  1  1  0
# 9   0  1  0  1
# 10  0  1  1  0
# attr(,"assign")
# [1] 1 1 2 2
# attr(,"contrasts")
# attr(,"contrasts")$x
# [1] "contr.treatment"
#
# attr(,"contrasts")$y
#   D J
# D 1 0
# J 0 1
</code>```

To understand why this happens, try:

```<code>contrasts(temp.data$y)
#   J
# D 0
# J 1

contrasts(temp.data$y, contrasts=F)
#   D J
# D 1 0
# J 0 1
</code>```

With your x variable this happens automatically by setting 0 + to remove the intercept. (Actually x also should be coded as factor).

The reason is, that in linear regression the levels of factor variables are usually compared to a reference level (which you could change using relevel). In your model matrix, with 0 + you remove the intercept for your first variable but not to the following (try model.matrix( ~ 0 + y + x, data=temp.data) where you get only one x but to y). This is determined in the standard contrasts setting using treatment contrasts by default.

You may want to read a relevant post of Rose Maier (2015) explaining this in great detail:

Contrasts in R

Title: One-hot encoding using model.matrix

Body:
There is something I do not understand in model.matrix. When I enter a single binary variable without an intercept it returns two levels.

> temp.data <- data.frame('x' = sample(c('A', 'B'), 1000, replace = TRUE))
> temp.data.table <- model.matrix( ~ 0 + x, data = temp.data)
> head(temp.data.table)
  xA xB
1  1  0
2  0  1
3  0  1
4  0  1
5  1  0
6  0  1

However, when I enter another binary level, it creates only 3 columns. Why is that? What makes the behavior of the function suddenly different? and how can I avoid it?

> temp.data <- data.frame('x' = sample(c('A', 'B'), 1000, replace = TRUE),
+                         'y' = sample(c('J', 'D'), 1000, replace = TRUE))
> temp.data.table <- model.matrix( ~ 0 + x + y, data = temp.data)
> head(temp.data.table)
  xA xB yJ
1  0  1  0
2  0  1  1
3  0  1  1
4  0  1  0
5  1  0  1
6  0  1  0

Body:
You need to work with factors and set the contrasts to FALSE. Try this:

n <- 10
temp.data <- data.frame('x'=sample(c('A', 'B'), n, replace=TRUE),
                        'y'=factor(sample(c('J', 'D'), n, replace=TRUE)))
model.matrix( ~ 0 + x + y, data=temp.data,
              contrasts=list(y=contrasts(temp.data$y, contrasts=FALSE)))

#    xA xB yD yJ
# 1   0  1  1  0
# 2   1  0  0  1
# 3   0  1  1  0
# 4   1  0  0  1
# 5   0  1  0  1
# 6   1  0  1  0
# 7   1  0  1  0
# 8   0  1  1  0
# 9   0  1  0  1
# 10  0  1  1  0
# attr(,"assign")
# [1] 1 1 2 2
# attr(,"contrasts")
# attr(,"contrasts")$x
# [1] "contr.treatment"
#
# attr(,"contrasts")$y
#   D J
# D 1 0
# J 0 1

To understand why this happens, try:

contrasts(temp.data$y)
#   J
# D 0
# J 1

contrasts(temp.data$y, contrasts=F)
#   D J
# D 1 0
# J 0 1

With your x variable this happens automatically by setting 0 + to remove the intercept. (Actually x also should be coded as factor).

The reason is, that in linear regression the levels of factor variables are usually compared to a reference level (which you could change using relevel). In your model matrix, with 0 + you remove the intercept for your first variable but not to the following (try model.matrix( ~ 0 + y + x, data=temp.data) where you get only one x but to y). This is determined in the standard contrasts setting using treatment contrasts by default.

You may want to read a relevant post of Rose Maier (2015) explaining this in great detail:

Contrasts in R


问:
MySQL connection from command line, how can be a command line utility so unstable?
Since a few hours I'm trying to connect to a local sql environment. It works in one second with MySQL Workbench, but from the commandline MySQL is not very verbose :)

```<code>mysql -u root -p root --host=127.0.0.1 --port=10011 -password'root'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
</code>```

Somewhere they report to use -u, while somewhere else they suggest --u

How do I find out if mysql is able to reach the server?

What else can I do for troubleshooting?

答:
MySQL command line is a mess, the solution was

mysql -uroot -h127.0.0.1 --port=10011 -proot

Some with -- like port and some with - like p, and with no space.

Body:
Since a few hours I'm trying to connect to a local sql environment. It works in one second with MySQL Workbench, but from the commandline MySQL is not very verbose :)

mysql -u root -p root --host=127.0.0.1 --port=10011 -password'root'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Somewhere they report to use -u, while somewhere else they suggest --u

How do I find out if mysql is able to reach the server?

What else can I do for troubleshooting?


问:
mysql query: how to get the number of yes/no votes per day
I have to create a mysql query to get a voting distribution of each day exceeding a particular date, something like this...

```<code>    date          yes_votes    no_votes
------------------------------------------
    2010-01-07    21           22
    2010-01-07    2            0
</code>```

My table is like this..

```<code>    post_votes
--------------------------
    id(longint)
    date(timestamp)
    flag(tinyint) // this stores the yes/no votes 1-yes, 2-no
</code>```

I am stuck at this....

```<code>SELECT COUNT(*) AS count, DATE(date) FROM post_votes WHERE date > '2010-07-01' GROUP BY DATE(date)
</code>```

this gives the total number of votes per day, but not the distribution that I want.

答:
Sum it:</p>

```<code>select date(date) as date,
       sum(case when flag = 1 then 1 else 0) as yes,
       sum(case when flag = 2 then 1 else 0) as no
from post_votes
where date > '2010-07-01'
group by date(date)
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I have to create a mysql query to get a voting distribution of each day exceeding a particular date, something like this...

    date          yes_votes    no_votes
------------------------------------------
    2010-01-07    21           22
    2010-01-07    2            0

My table is like this..

    post_votes
--------------------------
    id(longint)
    date(timestamp)
    flag(tinyint) // this stores the yes/no votes 1-yes, 2-no

I am stuck at this....

SELECT COUNT(*) AS count, DATE(date) FROM post_votes WHERE date > '2010-07-01' GROUP BY DATE(date)

this gives the total number of votes per day, but not the distribution that I want.

Body:
Sum it:

select date(date) as date,
       sum(case when flag = 1 then 1 else 0) as yes,
       sum(case when flag = 2 then 1 else 0) as no
from post_votes
where date > '2010-07-01'
group by date(date)


问:
How to make specific options to appear in select menu?
I have two select fields in a program. Both select fields contain options with values 1 to 10. the code is as below:

<select id="noc">
       <option value="1">1</option>
       <option value="2">2</option>
       <option value="3">3</option>
       <option value="4">4</option>
       <option value="5">5</option>
       <option value="6">6</option>
       <option value="7">7</option>
       <option value="8">8</option>
       <option value="9">9</option>
       <option value="10">10</option>
</select>

<select id="po">
       <option value="1">1</option>
       <option value="2">2</option>
       <option value="3">3</option>
       <option value="4">4</option>
       <option value="5">5</option>
       <option value="6">6</option>
       <option value="7">7</option>
       <option value="8">8</option>
       <option value="9">9</option>
       <option value="10">10</option>
</select>```

I want to achieve the following effect using JavaScript :

(Only for example) If the user has selected the option 8 in the first select field (id="noc"), the options below 8 (i.e. 9 and 10) should not be visible in the second select field (id="po").

答:
You can do it with using onChange method  and with querySelectorAll.

display or visibility attributes doesn't work for cross browsers. So you should recreate options.

var noc=document.getElementById("noc");
var po=document.getElementById("po");
var options=po.querySelectorAll("option");
function nochange(){
  po.innerHTML="";
  [...options].filter(x=>parseInt(x.value)<=parseInt(noc.value)).forEach(x=>{po.append(x)})
  po.value=noc.value;

}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><select id="noc"onChange="nochange()">
       <option value="1">1</option>
       <option value="2">2</option>
       <option value="3">3</option>
       <option value="4">4</option>
       <option value="5">5</option>
       <option value="6">6</option>
       <option value="7">7</option>
       <option value="8">8</option>
       <option value="9">9</option>
       <option value="10">10</option>
</select><br/>

<!--Some text---->

<select id="po">
       <option value="1">1</option>
       <option value="2">2</option>
       <option value="3">3</option>
       <option value="4">4</option>
       <option value="5">5</option>
       <option value="6">6</option>
       <option value="7">7</option>
       <option value="8">8</option>
       <option value="9">9</option>
       <option value="10">10</option>
</select></code>```

Body:
I have two select fields in a program. Both select fields contain options with values 1 to 10. the code is as below:

<select id="noc">
       <option value="1">1</option>
       <option value="2">2</option>
       <option value="3">3</option>
       <option value="4">4</option>
       <option value="5">5</option>
       <option value="6">6</option>
       <option value="7">7</option>
       <option value="8">8</option>
       <option value="9">9</option>
       <option value="10">10</option>
</select>

<select id="po">
       <option value="1">1</option>
       <option value="2">2</option>
       <option value="3">3</option>
       <option value="4">4</option>
       <option value="5">5</option>
       <option value="6">6</option>
       <option value="7">7</option>
       <option value="8">8</option>
       <option value="9">9</option>
       <option value="10">10</option>
</select>

I want to achieve the following effect using JavaScript :

(Only for example) If the user has selected the option 8 in the first select field (id="noc"), the options below 8 (i.e. 9 and 10) should not be visible in the second select field (id="po").

Body:
You can do it with using onChange method  and with querySelectorAll.

display or visibility attributes doesn't work for cross browsers. So you should recreate options.

var noc=document.getElementById("noc");
var po=document.getElementById("po");
var options=po.querySelectorAll("option");
function nochange(){
  po.innerHTML="";
  [...options].filter(x=>parseInt(x.value)<=parseInt(noc.value)).forEach(x=>{po.append(x)})
  po.value=noc.value;

}

<select id="noc"onChange="nochange()">
       <option value="1">1</option>
       <option value="2">2</option>
       <option value="3">3</option>
       <option value="4">4</option>
       <option value="5">5</option>
       <option value="6">6</option>
       <option value="7">7</option>
       <option value="8">8</option>
       <option value="9">9</option>
       <option value="10">10</option>
</select><br/>

<!--Some text---->

<select id="po">
       <option value="1">1</option>
       <option value="2">2</option>
       <option value="3">3</option>
       <option value="4">4</option>
       <option value="5">5</option>
       <option value="6">6</option>
       <option value="7">7</option>
       <option value="8">8</option>
       <option value="9">9</option>
       <option value="10">10</option>
</select>


问:
Is there a function in spark df that can be replaced by applymap?
Below is the code written for pandas df, because of memory issue i had to move to PySpark which is why i need to convert this code so that it can be executed for spark df. I tried running this directly but it generates an error.What can be the alternative for the code below in PySpark?

```<code>def units(x):
    if x <= 0:
        return 0
    if x >= 1:
        return 1

sets = df.applymap(units)
</code>```

Here, is the error i'm getting:

```<code>AttributeErrorTraceback (most recent call last)
<ipython-input-20-7e54b4e7a7e7> in <module>()
----> 1 sets = pivoted.applymap(units)

/usr/lib/spark/python/pyspark/sql/dataframe.py in __getattr__(self, name)
   1180         if name not in self.columns:
   1181             raise AttributeError(
-> 1182                 "'%s' object has no attribute '%s'" % (self.__class__.__name__, name))
   1183         jc = self._jdf.apply(name)
   1184         return Column(jc)

AttributeError: 'DataFrame' object has no attribute 'applymap'
</code>```

答:
You can wrap your units function as a UDF:</p>

```<code>from pyspark.sql.types import LongType
from pyspark.sql.functions import udf, col

def units(x):
    if x <= 0:
        return 0
    if x >= 1:
        return 1

units_udf = udf(lambda x: units(x), LongType())

df = spark.createDataFrame([(-1,), (0,), (1,), (2,)], ['id'])

df.show()
+---+
| id|
+---+
| -1|
|  0|
|  1|
|  2|
+---+

sets = df.withColumn("id", units_udf(col("id")))
sets.show()
+---+
| id|
+---+
|  0|
|  0|
|  1|
|  1|
+---+
</code>```

Body:
Below is the code written for pandas df, because of memory issue i had to move to PySpark which is why i need to convert this code so that it can be executed for spark df. I tried running this directly but it generates an error.What can be the alternative for the code below in PySpark?

def units(x):
    if x <= 0:
        return 0
    if x >= 1:
        return 1

sets = df.applymap(units)

Here, is the error i'm getting:

AttributeErrorTraceback (most recent call last)
<ipython-input-20-7e54b4e7a7e7> in <module>()
----> 1 sets = pivoted.applymap(units)

/usr/lib/spark/python/pyspark/sql/dataframe.py in __getattr__(self, name)
   1180         if name not in self.columns:
   1181             raise AttributeError(
-> 1182                 "'%s' object has no attribute '%s'" % (self.__class__.__name__, name))
   1183         jc = self._jdf.apply(name)
   1184         return Column(jc)

AttributeError: 'DataFrame' object has no attribute 'applymap'

Body:
You can wrap your units function as a UDF:

from pyspark.sql.types import LongType
from pyspark.sql.functions import udf, col

def units(x):
    if x <= 0:
        return 0
    if x >= 1:
        return 1

units_udf = udf(lambda x: units(x), LongType())

df = spark.createDataFrame([(-1,), (0,), (1,), (2,)], ['id'])

df.show()
+---+
| id|
+---+
| -1|
|  0|
|  1|
|  2|
+---+

sets = df.withColumn("id", units_udf(col("id")))
sets.show()
+---+
| id|
+---+
|  0|
|  0|
|  1|
|  1|
+---+


问:
Change ViewController's Back Button Image for all screens
I have this below code that changes the back button image on next screen.

I have 30 screens in my app and i want back button to be same across all 30 screens. Is it possible that I don't copy paste this code on all 30 screens and just write it once and rather reuse it across 30 screens.

Also, the code attached with back button should work fine on all screens when i reuse it

I am using iOS 8 and Xcode 6.

```<code>override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let backButtonImage: UIImage = UIImage(named: "back")!
    var backBarItem: UIBarButtonItem = UIBarButtonItem(image: backButtonImage, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("method"))

    segue.destinationViewController.navigationItem.leftBarButtonItem = backBarItem;
}

func method() {
    self.navigationController?.popViewControllerAnimated(true)
}
</code>```

答:
You can change it globally

```<code>UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back")
</code>```

Or per navigation controller.

```<code>override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.backIndicatorImage = UIImage(named: "custom-back")
    navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "custom-back")
}
</code>```

Full detail here https://sarunw.com/posts/how-to-change-back-button-image/

Body:
I have this below code that changes the back button image on next screen.

I have 30 screens in my app and i want back button to be same across all 30 screens. Is it possible that I don't copy paste this code on all 30 screens and just write it once and rather reuse it across 30 screens.

Also, the code attached with back button should work fine on all screens when i reuse it

I am using iOS 8 and Xcode 6.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let backButtonImage: UIImage = UIImage(named: "back")!
    var backBarItem: UIBarButtonItem = UIBarButtonItem(image: backButtonImage, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("method"))

    segue.destinationViewController.navigationItem.leftBarButtonItem = backBarItem;
}

func method() {
    self.navigationController?.popViewControllerAnimated(true)
}

Body:
You can change it globally

UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back")

Or per navigation controller.

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.backIndicatorImage = UIImage(named: "custom-back")
    navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "custom-back")
}

Full detail here https://sarunw.com/posts/how-to-change-back-button-image/


问:
Margin bottom auto depending on the view height
How can I make this body margin to bottom if the view height is big like here in the picture?

For example, if the height of the body goes above the 1000px how can I margin the body so I wont face that blank space below the footer?

答:
Try adding this style on the element that should be fully expanded on the screen:</p>

```<code>min-height: 100vh;
</code>```

Body:
Try adding this style on the element that should be fully expanded on the screen:

min-height: 100vh;


答:
Use view height, vh, as the measure in your CSS. E.g.

```<code>.bigpurplediv {
    height: 100vh;
}
</code>```

Title: Margin bottom auto depending on the view height

Body:
How can I make this body margin to bottom if the view height is big like here in the picture?

For example, if the height of the body goes above the 1000px how can I margin the body so I wont face that blank space below the footer?

Body:
Use view height, vh, as the measure in your CSS. E.g.

.bigpurplediv {
    height: 100vh;
}


问:
hibernate search configuration
Im using hibernate search and elastic search as backend . When i config hibernate and hibernatesearch in xml file  everything is ok but whene i config it in java class this warning occur and nothing found in my search .
xml config is:

```<code><properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3399/elastic" />
        <property name="hibernate.connection.username" value="admin" />
        <property name="hibernate.connection.password" value="admin" />

        <property name="hibernate.show_sql" value="true" />

        <property name="hibernate.search.default.indexmanager" value="elasticsearch"/>
        <property name="hibernate.search.default.elasticsearch.host" value="http://127.0.0.1:9400"/>
        <property name="hibernate.search.default.elasticsearch.index_schema_management_strategy" value="CREATE"/>
        <property name="hibernate.search.default.elasticsearch.required_index_status" value="YELLOW"/>

    </properties>
</code>```

java class config is :

```<code>java.util.Properties settings = new java.util.Properties();
            settings.put(org.hibernate.cfg.Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
            settings.put(org.hibernate.cfg.Environment.URL, "jdbc:mysql://127.0.0.1:3399/elastic");
            settings.put(org.hibernate.cfg.Environment.USER, "admin");
            settings.put(org.hibernate.cfg.Environment.PASS, "admin");
           settings.put(org.hibernate.cfg.Environment.DIALECT,"org.hibernate.dialect.MySQL8Dialect");
            settings.put(org.hibernate.cfg.Environment.SHOW_SQL, "true");
            settings.put(org.hibernate.cfg.Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
            settings.put(org.hibernate.cfg.Environment.POOL_SIZE, "5");
settings.put("hibernate.search.default.indexmanager", "elasticsearch");
            settings.put("hibernate.search.default.elasticsearch.host", "http://127.0.0.1:9400");
            settings.put("hibernate.search.default.elasticsearch.index_schema_management_strategy", "CREATE");
            settings.put("hibernate.search.default.elasticsearch.required_index_status", "YELLOW");
</code>```

Warning is :

WARNING: request [HEAD http://127.0.0.1:9400/com.radar.elasticsearch.videogame] returned 1 warnings: [299 Elasticsearch-6.8.0-65b6179 "[types removal] The parameter include_type_name should be explicitly specified in get indices requests to prepare for 7.0. In 7.0 include_type_name will default to 'false', which means responses will omit the type name in mapping definitions."]

答:
You are using Elasticsearch 6.8 where types are deprecated. Please refer removal of types for more info,

You need to pass _doc instead of type name otherwise use include_type_name if you are using your own type name in all the API.

Please refer this official blog for more info

If you plan to upgrade without downtime in a rolling fashion, you
  should upgrade to 6.8 first, which is the only 6.x release to have
  some features like support for the include_type_name parameter, which
  is required to upgrade to 7.0 smoothly.

Title: hibernate search configuration

Body:
Im using hibernate search and elastic search as backend . When i config hibernate and hibernatesearch in xml file  everything is ok but whene i config it in java class this warning occur and nothing found in my search .
xml config is:

<properties>
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3399/elastic" />
        <property name="hibernate.connection.username" value="admin" />
        <property name="hibernate.connection.password" value="admin" />

        <property name="hibernate.show_sql" value="true" />

        <property name="hibernate.search.default.indexmanager" value="elasticsearch"/>
        <property name="hibernate.search.default.elasticsearch.host" value="http://127.0.0.1:9400"/>
        <property name="hibernate.search.default.elasticsearch.index_schema_management_strategy" value="CREATE"/>
        <property name="hibernate.search.default.elasticsearch.required_index_status" value="YELLOW"/>

    </properties>

java class config is :

java.util.Properties settings = new java.util.Properties();
            settings.put(org.hibernate.cfg.Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
            settings.put(org.hibernate.cfg.Environment.URL, "jdbc:mysql://127.0.0.1:3399/elastic");
            settings.put(org.hibernate.cfg.Environment.USER, "admin");
            settings.put(org.hibernate.cfg.Environment.PASS, "admin");
           settings.put(org.hibernate.cfg.Environment.DIALECT,"org.hibernate.dialect.MySQL8Dialect");
            settings.put(org.hibernate.cfg.Environment.SHOW_SQL, "true");
            settings.put(org.hibernate.cfg.Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
            settings.put(org.hibernate.cfg.Environment.POOL_SIZE, "5");
settings.put("hibernate.search.default.indexmanager", "elasticsearch");
            settings.put("hibernate.search.default.elasticsearch.host", "http://127.0.0.1:9400");
            settings.put("hibernate.search.default.elasticsearch.index_schema_management_strategy", "CREATE");
            settings.put("hibernate.search.default.elasticsearch.required_index_status", "YELLOW");

Warning is :

WARNING: request [HEAD http://127.0.0.1:9400/com.radar.elasticsearch.videogame] returned 1 warnings: [299 Elasticsearch-6.8.0-65b6179 "[types removal] The parameter include_type_name should be explicitly specified in get indices requests to prepare for 7.0. In 7.0 include_type_name will default to 'false', which means responses will omit the type name in mapping definitions."]


问:
Read values from multiple YAML files in spring boot
spring.config.location can accept a list of directories which host the yaml files for a spring boot application.

But the listed directories must have file named as applicaion.yml, else they are not picked up.

if my application allows users to place yaml files with any names then I can't use this option.

I have then manually add the file names to the property like below

spring.config.location=/location1/file1.yml,/location2/file2.yml

Every time a new file is added, I have to modify this property.

I was able to use YamlPropertySourceLoader to read all the files and put all properties into a Map, but is there another, more elegant, way of doing this which allows me to bind these values directly into the @Value annotation.

updating with the code snippet that I am currently using, but looking for any other better alternatives.

```<code>Resource[] resources = applicationContext.getResources("classpath*:*.properties");

        Map<Object, Object> map = new HashMap<>();

        Arrays.stream(resources).forEach(res -> {
            try {
                propertiesPropertySourceLoader.load("resources", res)
                        .forEach(prop -> map.putAll((Map<?, ?>) prop.getSource()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
</code>```

答:
IF you have multiple yml/properties file in you application then you can use @PropertySource("classpath:file.properties") on you configuration class and accordingly you will be able to read the values from that config file.

For more info view

Body:
spring.config.location can accept a list of directories which host the yaml files for a spring boot application.

But the listed directories must have file named as applicaion.yml, else they are not picked up.

if my application allows users to place yaml files with any names then I can't use this option.

I have then manually add the file names to the property like below

spring.config.location=/location1/file1.yml,/location2/file2.yml

Every time a new file is added, I have to modify this property.

I was able to use YamlPropertySourceLoader to read all the files and put all properties into a Map, but is there another, more elegant, way of doing this which allows me to bind these values directly into the @Value annotation.

updating with the code snippet that I am currently using, but looking for any other better alternatives.

Resource[] resources = applicationContext.getResources("classpath*:*.properties");

        Map<Object, Object> map = new HashMap<>();

        Arrays.stream(resources).forEach(res -> {
            try {
                propertiesPropertySourceLoader.load("resources", res)
                        .forEach(prop -> map.putAll((Map<?, ?>) prop.getSource()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });


问:
Migrating from EJB3 to Spring, Hibernate
We have a desktop based application based upon EJB3, Oracle 10 and JBoss 4. This was created around three years back. JPA entities were used for ORM and the business logic was implemented in the Stateless Session beans. The client was developed using Swing API.

Now there is a requirement to replace the previous technology with Spring, Hibernate and JBoss for the next release of application. Client will still be in Swing. The plan is to replace the entities with POJOs and put the business logic from Session Beans to Spring Beans i.e. Data Access Objects (which extend HibernateDaoSupport).

So the question is, is it possible that we completely free our application from Session Beans and move the business logic to Spring Dao? Or do we still have to keep the Session Beans? If Session Beans are completely avoided, how will the client application be able to access the business methods? Like in case of JavaEE based app, the session beans were accessible through Jndi lookup.

Any suggestion are greatly appreciated.

答:
Spring beans are not only "DAOs", you can also have "services" to implement business logic (see http://biese.wordpress.com/2007/10/08/service-layer-and-dao-architecture/).

Services will be dependency injected in the presentation layer. If you need RMI between presentation layer, and business layer, you should use Spring Remoting http://static.springsource.org/spring/docs/2.5.x/reference/remoting.html (with RMI).


问:
Cannot read property 'map' of undefined with react hooks
Im trying to use fortnite api to display current item shop and i'm getting error likein the title, when I console log result it show an array but i cant map through it ? whY?

```<code>import React, { useState, useEffect } from 'react'
import axios from 'axios';
export default function itemShop() {
    const [shop, setShop] = useState('')
    const API = 'https://fortnite-public-api.theapinetwork.com/prod09/store/get?language={en}'
    useEffect(() => {
        const fetchData = async () => {
            const result = await axios(
                API,
            );

            setShop(result.data);
        };
        fetchData();
    }, []);

    const { items } = shop;
    console.log(items)
    return (
        <div>
            <p>{shop.date}</p>
            {items.map(item => <p>{item.name}</p>)}
        </div>
    )
}
</code>```

code sandbox : https://codesandbox.io/embed/km10wq4y5

答:
For me I forget to add await in axios after calling async method. like the code below. Make sure to check your code again if above methods didn't help you.

```<code>async function getCategories() {
      try {
        const response = axios.get("API");
        setCategories(response.data);
      } catch (error) {
        console.log("error", error);
      }
    }
</code>```

Body:
Im trying to use fortnite api to display current item shop and i'm getting error likein the title, when I console log result it show an array but i cant map through it ? whY?

import React, { useState, useEffect } from 'react'
import axios from 'axios';
export default function itemShop() {
    const [shop, setShop] = useState('')
    const API = 'https://fortnite-public-api.theapinetwork.com/prod09/store/get?language={en}'
    useEffect(() => {
        const fetchData = async () => {
            const result = await axios(
                API,
            );

            setShop(result.data);
        };
        fetchData();
    }, []);

    const { items } = shop;
    console.log(items)
    return (

{shop.date}

            {items.map(item =>
{item.name}
)}

    )
}

code sandbox : https://codesandbox.io/embed/km10wq4y5

Body:
For me I forget to add await in axios after calling async method. like the code below. Make sure to check your code again if above methods didn't help you.

async function getCategories() {
      try {
        const response = axios.get("API");
        setCategories(response.data);
      } catch (error) {
        console.log("error", error);
      }
    }


问:
How do I enable my Android Application users to download a WebView for later use dart and Flutter
Hi I'm trying to create a reading app that allows user to save pages for offline use from my online learning website. It uses the web view widget to access the webpage.</p>
```<code>import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

 class ScholarshipsPage extends StatefulWidget {
  @override
 _ScholarshipsPageState createState() => _ScholarshipsPageState();
 }

 class _ScholarshipsPageState extends State<ScholarshipsPage> {
 Completer<WebViewController> _controller = Completer<WebViewController>();
  @override
  Widget build(BuildContext context) {
  return new Scaffold(
    appBar: AppBar(
      title: Text('Scholarship Research Program'),
      backgroundColor: Colors.deepPurple,
      elevation: 0,
      centerTitle: true,
    ),
    body: WebView(
              initialUrl: 'https://mydigitalbackpack.net/scholarshipresearchprogram/',
              javascriptMode: JavascriptMode.unrestricted,
              onWebViewCreated: (WebViewController controller) {
                _controller.complete(controller);
              },
    )
    );
     }
    }
</code>```

答:
This is not possible with the webview_flutter plugin because those features are missing.

Instead, you can use my plugin flutter_inappwebview, which is a Flutter plugin that allows you to add inline WebViews or open an in-app browser window and has a lot of events, methods, and options to control WebViews.

In your case, you can use the InAppWebViewController.android.saveWebArchive method to save the current WebView as a web archive. At the moment, this method is available only for Android (it is implemented through the WebView#saveWebArchive official Android API)!

I will check how to implement the same thing for iOS if possible.

Instead, if you need to download files: to be able to recognize downloadable files, you need to set the useOnDownloadStart: true option, and then you can listen the onDownloadStart event!

Also, for example, on Android you need to add write permission inside your AndroidManifest.xml file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
```
<p>Then, you need to ask permission using the permission_handler plugin. Instead, to effectively download your file, you can use the flutter_downloader plugin.</p>
<p>Here is a complete example using http://ovh.net/files/ (in particular, the http://ovh.net/files/1Mio.dat as URL) to test the download:</p>
<pre class="lang-dart prettyprint-override"><code>import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FlutterDownloader.initialize(
      debug: true // optional: set false to disable printing logs to console
  );
  await Permission.storage.request();
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  InAppWebViewController webView;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('InAppWebView Example'),
        ),
        body: Container(
            child: Column(children: <Widget>[
          Expanded(
              child: InAppWebView(
            initialUrl: "http://ovh.net/files/1Mio.dat",
            initialHeaders: {},
            initialOptions: InAppWebViewGroupOptions(
              crossPlatform: InAppWebViewOptions(
                debuggingEnabled: true,
                useOnDownloadStart: true
              ),
            ),
            onWebViewCreated: (InAppWebViewController controller) {
              webView = controller;
            },
            onLoadStart: (InAppWebViewController controller, String url) {

            },
            onLoadStop: (InAppWebViewController controller, String url) {

            },
            onDownloadStart: (controller, url) async {
              print("onDownloadStart $url");
              final taskId = await FlutterDownloader.enqueue(
                url: url,
                savedDir: (await getExternalStorageDirectory()).path,
                showNotification: true, // show download progress in status bar (for Android)
                openFileFromNotification: true, // click on notification to open downloaded file (for Android)
              );
            },
          ))
        ])),
      ),
    );
  }
}
</code>```

Here, as you can see, I'm using also the path_provider plugin to get the folder where I want to save the file.

Body:
Hi I'm trying to create a reading app that allows user to save pages for offline use from my online learning website. It uses the web view widget to access the webpage.

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

 class ScholarshipsPage extends StatefulWidget {
  @override
 _ScholarshipsPageState createState() => _ScholarshipsPageState();
 }

 class _ScholarshipsPageState extends State<ScholarshipsPage> {
 Completer<WebViewController> _controller = Completer<WebViewController>();
  @override
  Widget build(BuildContext context) {
  return new Scaffold(
    appBar: AppBar(
      title: Text('Scholarship Research Program'),
      backgroundColor: Colors.deepPurple,
      elevation: 0,
      centerTitle: true,
    ),
    body: WebView(
              initialUrl: 'https://mydigitalbackpack.net/scholarshipresearchprogram/',
              javascriptMode: JavascriptMode.unrestricted,
              onWebViewCreated: (WebViewController controller) {
                _controller.complete(controller);
              },
    )
    );
     }
    }

Body:
This is not possible with the webview_flutter plugin because those features are missing.

Instead, you can use my plugin flutter_inappwebview, which is a Flutter plugin that allows you to add inline WebViews or open an in-app browser window and has a lot of events, methods, and options to control WebViews.

In your case, you can use the InAppWebViewController.android.saveWebArchive method to save the current WebView as a web archive. At the moment, this method is available only for Android (it is implemented through the WebView#saveWebArchive official Android API)!

I will check how to implement the same thing for iOS if possible.

Instead, if you need to download files: to be able to recognize downloadable files, you need to set the useOnDownloadStart: true option, and then you can listen the onDownloadStart event!

Also, for example, on Android you need to add write permission inside your AndroidManifest.xml file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Then, you need to ask permission using the permission_handler plugin. Instead, to effectively download your file, you can use the flutter_downloader plugin.

Here is a complete example using http://ovh.net/files/ (in particular, the http://ovh.net/files/1Mio.dat as URL) to test the download:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await FlutterDownloader.initialize(
      debug: true // optional: set false to disable printing logs to console
  );
  await Permission.storage.request();
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  InAppWebViewController webView;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('InAppWebView Example'),
        ),
        body: Container(
            child: Column(children: <Widget>[
          Expanded(
              child: InAppWebView(
            initialUrl: "http://ovh.net/files/1Mio.dat",
            initialHeaders: {},
            initialOptions: InAppWebViewGroupOptions(
              crossPlatform: InAppWebViewOptions(
                debuggingEnabled: true,
                useOnDownloadStart: true
              ),
            ),
            onWebViewCreated: (InAppWebViewController controller) {
              webView = controller;
            },
            onLoadStart: (InAppWebViewController controller, String url) {

            },
            onLoadStop: (InAppWebViewController controller, String url) {

            },
            onDownloadStart: (controller, url) async {
              print("onDownloadStart $url");
              final taskId = await FlutterDownloader.enqueue(
                url: url,
                savedDir: (await getExternalStorageDirectory()).path,
                showNotification: true, // show download progress in status bar (for Android)
                openFileFromNotification: true, // click on notification to open downloaded file (for Android)
              );
            },
          ))
        ])),
      ),
    );
  }
}

Here, as you can see, I'm using also the path_provider plugin to get the folder where I want to save the file.


问:
Result type of a polyvariadic function in haskell
While studying polyvariadic functions in Haskell I stumbled across the following SO questions:

How to create a polyvariadic haskell function?

Haskell, polyvariadic function and type inference

and thought I will give it a try by implementing a function which takes a variable number of strings and concatenates/merges them into a single string:

{-# LANGUAGE FlexibleInstances #-}

class MergeStrings r where
    merge :: String -> r
instance MergeStrings String where
    merge = id
instance (MergeStrings r) => MergeStrings (String -> r) where
    merge acc = merge . (acc ++)

```

<p>This works so far if I call merge with at least one string argument and if I provide the final type.</p>

<pre class="lang-hs prettyprint-override"><code>foo :: String
foo = merge "a" "b" "c"
</code>```

Omitting the final type results in an error, i.e., compiling the following

bar = merge "a" "b" "c"
```

<p>results in</p>

```test.hs:12:7: error:
    • Ambiguous type variable ‘t0’ arising from a use of ‘merge’
      prevents the constraint ‘(MergeStrings t0)’ from being solved.
      Relevant bindings include bar :: t0 (bound at test.hs:12:1)
      Probable fix: use a type annotation to specify what ‘t0’ should be.
      These potential instances exist:
        instance MergeStrings r => MergeStrings (String -> r)
          -- Defined at test.hs:6:10
        instance MergeStrings String -- Defined at test.hs:4:10
    • In the expression: merge "a" "b" "c"
      In an equation for ‘bar’: bar = merge "a" "b" "c"
   |
12 | bar = merge "a" "b" "c"
   |
```

<p>The error message makes perfect sense since I could easily come up with, for example</p>

```bar :: String -> String
bar = merge "a" "b" "c"

baz = bar "d"
```

rendering bar not into a single string but into a function which takes and returns one string.

Is there a way to tell Haskell that the result type must be of type String? For example, Text.Printf.printf "hello world" evaluates to type String without explicitly defining.

答:
Brad (in a comment) and Max are not wrong saying that the defaulting of printf "…" … to IO ( ) is the reason for it working in ghci without type annotations. But it is not the end of the story. There are things we can do to make your definition of bar work.

First, I should mention the «monomorphism restriction» — an obscure and unintuitive type inference rule we have in Haskell. For whatever reason, the designers of Haskell decided that a top level definition without a type signature should have no polymorphic variables in its inferred type — that is, be monomorphic. bar is polymorphic, so you can see that it would be affected.

Some type classes (particularly numbers) have defaulting rules that allow you to say x = 13 without a type signature and have it inferred that x :: Integer — or whatever other type you set as default. Type defaulting is only available for a few blessed classes, so you cannot have it for your own class, and without a designated default GHC cannot decide what particular monomorphic type to choose.

But you can do other things, beside defaulting, to make the type checker happy — either:

Disable the monomorphism restriction.

Assign an explicit polymorphic type signature: bar :: MergeStrings r => r

Now bar is polymorphic and works as you would expect. See:

```<code>λ putStrLn bar
abc
λ putStrLn (bar "x")
abcx
λ putStrLn (bar "x" "y")
abcxy
</code>```

You can also use defaulting to make expressions such as show bar work. Since Show is among the classes that you can default when extended default rules are enabled, you can issue default (String) in the module where you want to use show bar and it will work as you would expect.

Body:
While studying polyvariadic functions in Haskell I stumbled across the following SO questions:

How to create a polyvariadic haskell function?

Haskell, polyvariadic function and type inference

and thought I will give it a try by implementing a function which takes a variable number of strings and concatenates/merges them into a single string:

{-# LANGUAGE FlexibleInstances #-}

class MergeStrings r where
    merge :: String -> r
instance MergeStrings String where
    merge = id
instance (MergeStrings r) => MergeStrings (String -> r) where
    merge acc = merge . (acc ++)

This works so far if I call merge with at least one string argument and if I provide the final type.

foo :: String
foo = merge "a" "b" "c"

Omitting the final type results in an error, i.e., compiling the following

bar = merge "a" "b" "c"

results in

test.hs:12:7: error:
    • Ambiguous type variable ‘t0’ arising from a use of ‘merge’
      prevents the constraint ‘(MergeStrings t0)’ from being solved.
      Relevant bindings include bar :: t0 (bound at test.hs:12:1)
      Probable fix: use a type annotation to specify what ‘t0’ should be.
      These potential instances exist:
        instance MergeStrings r => MergeStrings (String -> r)
          -- Defined at test.hs:6:10
        instance MergeStrings String -- Defined at test.hs:4:10
    • In the expression: merge "a" "b" "c"
      In an equation for ‘bar’: bar = merge "a" "b" "c"
   |
12 | bar = merge "a" "b" "c"
   |

The error message makes perfect sense since I could easily come up with, for example

bar :: String -> String
bar = merge "a" "b" "c"

baz = bar "d"

rendering bar not into a single string but into a function which takes and returns one string.

Is there a way to tell Haskell that the result type must be of type String? For example, Text.Printf.printf "hello world" evaluates to type String without explicitly defining.

Body:
Brad (in a comment) and Max are not wrong saying that the defaulting of printf "…" … to IO ( ) is the reason for it working in ghci without type annotations. But it is not the end of the story. There are things we can do to make your definition of bar work.

First, I should mention the «monomorphism restriction» — an obscure and unintuitive type inference rule we have in Haskell. For whatever reason, the designers of Haskell decided that a top level definition without a type signature should have no polymorphic variables in its inferred type — that is, be monomorphic. bar is polymorphic, so you can see that it would be affected.

Some type classes (particularly numbers) have defaulting rules that allow you to say x = 13 without a type signature and have it inferred that x :: Integer — or whatever other type you set as default. Type defaulting is only available for a few blessed classes, so you cannot have it for your own class, and without a designated default GHC cannot decide what particular monomorphic type to choose.

But you can do other things, beside defaulting, to make the type checker happy — either:

Disable the monomorphism restriction.

Assign an explicit polymorphic type signature: bar :: MergeStrings r => r

Now bar is polymorphic and works as you would expect. See:

λ putStrLn bar
abc
λ putStrLn (bar "x")
abcx
λ putStrLn (bar "x" "y")
abcxy

You can also use defaulting to make expressions such as show bar work. Since Show is among the classes that you can default when extended default rules are enabled, you can issue default (String) in the module where you want to use show bar and it will work as you would expect.


问:
Druid Too much memory consumption
I have deployed Druid Single server using command (./bin/start-micro-quickstart)

My server specification is 8vCPU and 32GB RAM (EC2 t2.2Xlarge) and also added 100GB SWAP
I'm trying to ingesting 27M records from kafka to Druid.

Now at this point I have 4M records shown in Druid Datasource with total size is 506 MB and segment are 5400 (Average Segment size is 92.99 KB)

And my Memory usage is

```<code>             total        used        free      shared  buff/cache   available
Mem:            31G         30G        248M         24K        207M         96M
Swap:           99G         78G         21G
</code>```

My DataSource size is 506 MB So why RAM Consumption is 108 GB ?

And are those all segment is in memory?

Whice druid service uses CPU and Which Druid Service uses Memory?

答: How many peon task you are running ? Since you are using kafka to ingest, I am assuming you are using superviser spec. If you have too many topics, and a supervisor spect for each topic, it will take memory. Check direct memory requirements. https://druid.apache.org/docs/latest/configuration/index.html</p>

Title: Druid Too much memory consumption

Body:
I have deployed Druid Single server using command (./bin/start-micro-quickstart)

My server specification is 8vCPU and 32GB RAM (EC2 t2.2Xlarge) and also added 100GB SWAP
I'm trying to ingesting 27M records from kafka to Druid.

Now at this point I have 4M records shown in Druid Datasource with total size is 506 MB and segment are 5400 (Average Segment size is 92.99 KB)

And my Memory usage is

             total        used        free      shared  buff/cache   available
Mem:            31G         30G        248M         24K        207M         96M
Swap:           99G         78G         21G

My DataSource size is 506 MB So why RAM Consumption is 108 GB ?

And are those all segment is in memory?

Whice druid service uses CPU and Which Druid Service uses Memory?


问:
What is the proper way to dynamically add HTML to a Pug (Jade) template?
Say we want to create a webpage using Pug and NodeJS that displays a list with items based on an external data source, e.g.:

```<code>doctype html
  ...
  body
    p Welcome to #{siteName}
    each item in variableWithExternalData
        ul
          li= item
</code>```

Current approach:

User visits page

Server sends request to external API

Server processes external data and stores this in data

Render the page using res.render('index', {siteName: 'MyName', variableWithExternalData: data});

While this works fine, it is not particularly a swift client-side experience as the server first has to process the external data before it renders the page. What would be much more preferred is something like the following:

User visits page

Server renders the page

Server sends request to external API

Server processes external data

Client side retrieves data from server and dynamically adds it to the page

This would be beneficial as the page can render before retrieving and processing the external data, especially since the ul is not directly visible when the page loads. I am aware that it is possible to do something like the above by making a request from the client side using Ajax and subsequently add the items to the list using JavaScript. However, I wonder if there is a more serverside based approach allowed by Pug, e.g. some form of callback in the pug.render()?

答:
You can send part of the page (HTML markup), using chunked response, then after more data is processed on the back-end, send another chunk of the page. However this approach has some limitations, you cannot insert content in-between already sent data.

As for Pug, it is templating library and has nothing to do with sending data back to client. You can use Pug to create HTML for full page, or just a part of it. Then send output from Pug according to your wishes.

Example #1

Below I've written simple NodeJS demo showcasing sending HTML in chunks. New chunk will be sent each second. After 10 chunks, closing tags will be sent. Each chunk should be rendered in the browser as soon as it arrives.

const http = require('http')
const port = 3000
const hostname = 'localhost'
const server = http.createServer(main)

function main (req, res) {
    res.setHeader('Content-Type', 'text/html; charset=UTF-8')
    res.setHeader('Transfer-Encoding', 'chunked')

    let chunksLeft = 10

    res.write('<DOCTYPE html><title>Chunked response demo</title>')
    res.write(`<h1>Hello chunks!</h1><p>I will be sending ${chunksLeft} entries.</p><ol>`)

    function sendChunk () {
        chunksLeft -= 1
        res.write(`<li>Entry</li>`)
        if (chunksLeft > 0) {
            setTimeout(sendChunk, 1000)
        } else {
            res.end('
')
        }
    }

    sendChunk()
}

function message () {
    console.log(`Listening on ${hostname}:${port}...`)
}

server.listen(port, hostname, message)
```

<h1>Example #2</h1>

<p>Below is code showcasing using axios and pug libraries for fetching and rendering data from external API, using concepts from previous example.</p>

<pre class="lang-js prettyprint-override"><code>const http = require('http')
const axios = require('axios')
const pug = require('pug')
const port = 3000
const hostname = 'localhost'
const server = http.createServer(main)

const template =
`doctype html
html
    head
        title Chunked response with pug and axios
    body
        h1 Hello chunks!
        p Below is example data downloaded from jsonplaceholder and transformed to HTML.`

const list =
`ol
    each t in todos
        li= t.title`

function main (req, res) {
    res.setHeader('Content-Type', 'text/html; charset=UTF-8')
    res.setHeader('Transfer-Encoding', 'chunked')

    res.write(pug.render(template))

    axios.get('https://jsonplaceholder.typicode.com/todos').then(reply => {
        res.end(pug.render(list, { todos: reply.data }))
    }).catch(() => {
        res.end(pug.render('p Error: could not download todos!'))
    })
}

function message () {
    console.log(`Listening on ${hostname}:${port}...`)
}

server.listen(port, hostname, message)
</code>```

More information:

https://en.wikipedia.org/wiki/Chunked_transfer_encoding

https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_response_write_chunk_encoding_callback

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding

https://strongloop.com/strongblog/streaming-chunked-html-node-js-data/

Body:
Say we want to create a webpage using Pug and NodeJS that displays a list with items based on an external data source, e.g.:

doctype html
  ...
  body
    p Welcome to #{siteName}
    each item in variableWithExternalData
        ul
          li= item

Current approach:

User visits page

Server sends request to external API

Server processes external data and stores this in data

Render the page using res.render('index', {siteName: 'MyName', variableWithExternalData: data});

While this works fine, it is not particularly a swift client-side experience as the server first has to process the external data before it renders the page. What would be much more preferred is something like the following:

User visits page

Server renders the page

Server sends request to external API

Server processes external data

Client side retrieves data from server and dynamically adds it to the page

This would be beneficial as the page can render before retrieving and processing the external data, especially since the ul is not directly visible when the page loads. I am aware that it is possible to do something like the above by making a request from the client side using Ajax and subsequently add the items to the list using JavaScript. However, I wonder if there is a more serverside based approach allowed by Pug, e.g. some form of callback in the pug.render()?

Body:
You can send part of the page (HTML markup), using chunked response, then after more data is processed on the back-end, send another chunk of the page. However this approach has some limitations, you cannot insert content in-between already sent data.

As for Pug, it is templating library and has nothing to do with sending data back to client. You can use Pug to create HTML for full page, or just a part of it. Then send output from Pug according to your wishes.

Example #1

Below I've written simple NodeJS demo showcasing sending HTML in chunks. New chunk will be sent each second. After 10 chunks, closing tags will be sent. Each chunk should be rendered in the browser as soon as it arrives.

const http = require('http')
const port = 3000
const hostname = 'localhost'
const server = http.createServer(main)

function main (req, res) {
    res.setHeader('Content-Type', 'text/html; charset=UTF-8')
    res.setHeader('Transfer-Encoding', 'chunked')

    let chunksLeft = 10

    res.write('<DOCTYPE html><html><head><title>Chunked response demo</title></head><body>')
    res.write(`<h1>Hello chunks!</h1><p>I will be sending ${chunksLeft} entries.</p><ol>`)

    function sendChunk () {
        chunksLeft -= 1
        res.write(`<li>Entry</li>`)
        if (chunksLeft > 0) {
            setTimeout(sendChunk, 1000)
        } else {
            res.end('</ol></html>')
        }
    }

    sendChunk()
}

function message () {
    console.log(`Listening on ${hostname}:${port}...`)
}

server.listen(port, hostname, message)

Example #2

Below is code showcasing using axios and pug libraries for fetching and rendering data from external API, using concepts from previous example.

const http = require('http')
const axios = require('axios')
const pug = require('pug')
const port = 3000
const hostname = 'localhost'
const server = http.createServer(main)

const template =
`doctype html
html
    head
        title Chunked response with pug and axios
    body
        h1 Hello chunks!
        p Below is example data downloaded from <a href="https://jsonplaceholder.typicode.com/todos">jsonplaceholder</a> and transformed to HTML.`

const list =
`ol
    each t in todos
        li= t.title`

function main (req, res) {
    res.setHeader('Content-Type', 'text/html; charset=UTF-8')
    res.setHeader('Transfer-Encoding', 'chunked')

    res.write(pug.render(template))

    axios.get('https://jsonplaceholder.typicode.com/todos').then(reply => {
        res.end(pug.render(list, { todos: reply.data }))
    }).catch(() => {
        res.end(pug.render('p Error: could not download todos!'))
    })
}

function message () {
    console.log(`Listening on ${hostname}:${port}...`)
}

server.listen(port, hostname, message)

More information:

https://en.wikipedia.org/wiki/Chunked_transfer_encoding

https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_response_write_chunk_encoding_callback

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding

https://strongloop.com/strongblog/streaming-chunked-html-node-js-data/


问:
True or false output based on a probability
Is there a standard function for Python which outputs True or False probabilistically based on the input of a random number from 0 to 1?

example of what I mean:

```<code>def decision(probability):
    ...code goes here...
    return ...True or False...
</code>```

the above example if given an input of, say, 0.7 will return True with a 70% probability and false with a 30% probability

答:
I use this to generate a random boolean in python with a probability:

from random import randint
n=8 # inverse of probability
rand_bool=randint(0,n*n-1)%n==0
```

<p>so to expand that :</p>

<pre class="lang-py prettyprint-override"><code>def rand_bool(prob):
    s=str(prob)
    p=s.index('.')
    d=10**(len(s)-p)
    return randint(0,d*d-1)%d<int(s[p+1:])
</code>```

I came up with this myself but it seems to work.

Body:
Is there a standard function for Python which outputs True or False probabilistically based on the input of a random number from 0 to 1?

example of what I mean:

def decision(probability):
    ...code goes here...
    return ...True or False...

the above example if given an input of, say, 0.7 will return True with a 70% probability and false with a 30% probability

Body:
I use this to generate a random boolean in python with a probability:

from random import randint
n=8 # inverse of probability
rand_bool=randint(0,n*n-1)%n==0

so to expand that :

def rand_bool(prob):
    s=str(prob)
    p=s.index('.')
    d=10**(len(s)-p)
    return randint(0,d*d-1)%d<int(s[p+1:])

I came up with this myself but it seems to work.


问:
Should GC.SuppressFinalize be called on objects that do not have a finalizer?
For some reason FXCop seems to think I should be calling GC.SuppressFinalize in Dispose, regardless of whether I have a finalizer or not. </p>

<p>Am I missing something? Is there a reason to call GC.SuppressFinalize on objects that have no finalizer defined? </p>

答: I don't see any need to call SuppressFinalize() if there's no finalizer defined. If you want to be defensive then it can be good to have a finalizer as well as Dispose(), so you don't need to rely on clients to always call Dispose().  Then you won't leak resources when they forget.</p>

ContentLicense: CC BY-SA 2.5

Body:
For some reason FXCop seems to think I should be calling GC.SuppressFinalize in Dispose, regardless of whether I have a finalizer or not.

Am I missing something? Is there a reason to call GC.SuppressFinalize on objects that have no finalizer defined?

LastEditorDisplayName: Mitch Wheat

OwnerDisplayName: cxfx


问:
How do you access THEN and ELSE tag in TYPO3Fluid AbstractViewHelper?
There is a new AbstractViewHelper in TYPO3 (TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper) and I try to implement a very simple InArrayViewHelper to my project with that.

The problem is you should use render() if you like to access then or else sub tags according to https://docs.typo3.org/m/typo3/book-extbasefluid/master/en-us/8-Fluid/8-developing-a-custom-viewhelper.html#renderstatic-method

So I do something like

```<code><?php
namespace Vendor\Project\ViewHelpers;

use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

class InArrayViewHelper extends AbstractViewHelper {

    //use CompileWithRenderStatic;

    public function initializeArguments() {
        $this->registerArgument('haystack', 'mixed', 'View helper haystack ', TRUE);
        $this->registerArgument('needle', 'string', 'View helper needle', TRUE);
    }

    public function render() {
        $needle = $this->arguments['needle'];
        $haystack = $this->arguments['haystack'];
        if(!is_array($haystack)) {
            return $this->renderElseChild();
        }
        if(in_array($needle, $haystack)) {
            return $this->renderThenChild();
        } else {
            return $this->renderElseChild();
        }
    }

}
</code>```

Well but they removed renderThenChild and renderElseChild in there. What is the right way to do so. Can I do it without using \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper?

答:
First of all: if you want your ViewHelper to be a condition ViewHelper you should be subclassing TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper, not TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper. The former contains a collection of assistance methods, e.g. renderThenChild (which renders either the f:then node or returns the then argument, whichever is present).

Second: access to the tags (as opposed to a closure which renders either the tag or the compiled execution) can only happen while the template is not yet compiled, and can be achieved through overriding public static function postParseEvent which receives the ViewHelperNode which allows you to read the child nodes with getChildNodes and/or add additional child nodes through addChildNode.

Implementations based on TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper and using manually registered then and else arguments can also work, but will not support child nodes like f:then without replicating almost all of the assistance methods from TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper.

So the answer is: yes, you can do it without AbstractConditionViewHelper - but you shouldn't.

Body:
There is a new AbstractViewHelper in TYPO3 (TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper) and I try to implement a very simple InArrayViewHelper to my project with that.

The problem is you should use render() if you like to access then or else sub tags according to https://docs.typo3.org/m/typo3/book-extbasefluid/master/en-us/8-Fluid/8-developing-a-custom-viewhelper.html#renderstatic-method

So I do something like

<?php
namespace Vendor\Project\ViewHelpers;

use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

class InArrayViewHelper extends AbstractViewHelper {

    //use CompileWithRenderStatic;

    public function initializeArguments() {
        $this->registerArgument('haystack', 'mixed', 'View helper haystack ', TRUE);
        $this->registerArgument('needle', 'string', 'View helper needle', TRUE);
    }

    public function render() {
        $needle = $this->arguments['needle'];
        $haystack = $this->arguments['haystack'];
        if(!is_array($haystack)) {
            return $this->renderElseChild();
        }
        if(in_array($needle, $haystack)) {
            return $this->renderThenChild();
        } else {
            return $this->renderElseChild();
        }
    }

}

Well but they removed renderThenChild and renderElseChild in there. What is the right way to do so. Can I do it without using \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper?


问:
One-hot encoding using model.matrix
There is something I do not understand in model.matrix. When I enter a single binary variable without an intercept it returns two levels. </p>

```<code>> temp.data <- data.frame('x' = sample(c('A', 'B'), 1000, replace = TRUE))
> temp.data.table <- model.matrix( ~ 0 + x, data = temp.data)
> head(temp.data.table)
  xA xB
1  1  0
2  0  1
3  0  1
4  0  1
5  1  0
6  0  1
</code>```

<p>However, when I enter another binary level, it creates only 3 columns. Why is that? What makes the behavior of the function suddenly different? and how can I avoid it? </p>

```<code>> temp.data <- data.frame('x' = sample(c('A', 'B'), 1000, replace = TRUE),
+                         'y' = sample(c('J', 'D'), 1000, replace = TRUE))
> temp.data.table <- model.matrix( ~ 0 + x + y, data = temp.data)
> head(temp.data.table)
  xA xB yJ
1  0  1  0
2  0  1  1
3  0  1  1
4  0  1  0
5  1  0  1
6  0  1  0
</code>```

答:
You need to reset the contrasts of the factor variables. See this post.</p>

```<code>temp.data <- data.frame('x' = sample(c('A', 'B'), 1000, replace = TRUE),
+                         'y' = sample(c('J', 'D'), 1000, replace = TRUE))

dat = model.matrix(~ -1 +., data=temp.data, contrasts.arg = lapply(temp.data[,1:2], contrasts, contrasts=FALSE))
head(dat)

  xA xB yD yJ
1  0  1  0  1
2  1  0  0  1
3  1  0  0  1
4  1  0  0  1
5  0  1  1  0
6  0  1  0  1
</code>```

Title: One-hot encoding using model.matrix

Body:
There is something I do not understand in model.matrix. When I enter a single binary variable without an intercept it returns two levels.

> temp.data <- data.frame('x' = sample(c('A', 'B'), 1000, replace = TRUE))
> temp.data.table <- model.matrix( ~ 0 + x, data = temp.data)
> head(temp.data.table)
  xA xB
1  1  0
2  0  1
3  0  1
4  0  1
5  1  0
6  0  1

However, when I enter another binary level, it creates only 3 columns. Why is that? What makes the behavior of the function suddenly different? and how can I avoid it?

> temp.data <- data.frame('x' = sample(c('A', 'B'), 1000, replace = TRUE),
+                         'y' = sample(c('J', 'D'), 1000, replace = TRUE))
> temp.data.table <- model.matrix( ~ 0 + x + y, data = temp.data)
> head(temp.data.table)
  xA xB yJ
1  0  1  0
2  0  1  1
3  0  1  1
4  0  1  0
5  1  0  1
6  0  1  0

Body:
You need to reset the contrasts of the factor variables. See this post.

temp.data <- data.frame('x' = sample(c('A', 'B'), 1000, replace = TRUE),
+                         'y' = sample(c('J', 'D'), 1000, replace = TRUE))

dat = model.matrix(~ -1 +., data=temp.data, contrasts.arg = lapply(temp.data[,1:2], contrasts, contrasts=FALSE))
head(dat)

  xA xB yD yJ
1  0  1  0  1
2  1  0  0  1
3  1  0  0  1
4  1  0  0  1
5  0  1  1  0
6  0  1  0  1