问:
Can we fill across columns with new fields in R?
I have a dataset that has multiple values in the same row/column.  Some are separated by "-".  I am trying to split the columns appropriately and then add the values that are implied by the "-".  I've been able to get the dataset into the right format, but I am not having success in finding a way to create a sequence between lower and upper.

sample data set here (UPDATE:  Actual data set has 'codes' with alpha characters):

```<code>grp_id <- ("grp1")
name <- ("test")
code <- ("93790, 93797 - 93798, 98960 - 98962, 98966 - 98968, 99078, G1501, G5017")
df <- data.frame(grp_id, name, code)
</code>```

what I have tried:

```<code>df %>%
  separate_rows(code, sep = ",") %>%
  mutate(rn = row_number()) %>%
  separate(code, sep = "-", into = c("lower", "upper")) %>%
  mutate(upper = case_when(is.na(upper) ~ lower,
                           TRUE ~ upper)) %>%
  mutate(implied = seq(as.numeric(lower), as.numeric(upper), by = 1))
</code>```

this works great until the last mutate where I try to create new columns for the new values.

The expected output would be :

```<code>group_id  name  lower   upper implied.1  implied.2    rn
grp1      test   97390   97390  na        na          1
grp1      test   97397   97398  na        na          2
grp1      test   98960   98962  *98961*   na          3
grp1      test   G1501                                4
grp1      test   G5017                                5
</code>```

So you can see in rn 3 the 98961 was added as it was the implied value by the "-".

Any suggestions I can try?

EDIT:  Since my actual data set has some alpha characters, this solution doesn't completely work.  I have edited as below, however it breaks down at the last mutate function.

```<code>df %>%
  separate_rows(code, sep = ",") %>%
  mutate(rn = row_number()) %>%
  separate(code, sep = "-", into = c("lower", "upper")) %>%
  mutate(upper = case_when(is.na(upper) ~ lower,
                           TRUE ~ upper)) %>%
  mutate(lower = case_when(
    str_detect(as.character(lower), '\\D') ~ lower,
    TRUE ~ as.character(as.numeric(lower))),
    upper = case_when(
      str_detect(as.character(lower), '\\D') ~ lower,
      TRUE ~ as.character(as.numeric(upper)))) %>%
  rowwise() %>%
  mutate(implied = list( seq(lower, upper, by = 1))) %>%
  tidyr::unnest_wider(implied)
</code>```

答:
Use dplyr::rowwise to create a tibble grouped by row, then make sure to wrap your seq statement in a list. Then use tidyr::unnest_wider to unnest the list, to get each value in the sequence in its own column. You could then just rename the columns with rename or rename_at.

Does this work for your needs?

```<code>df %>%
  separate_rows(code, sep = ",") %>%
  mutate(rn = row_number()) %>%
  separate(code, sep = "-", into = c("lower", "upper")) %>%
  mutate(upper = case_when(is.na(upper) ~ lower,
                           TRUE ~ upper)) %>%
  mutate(lower = as.numeric(lower), upper = as.numeric(upper)) %>%
  rowwise() %>%
  mutate(implied = list( seq(lower, upper, by = 1)) ) %>%
  tidyr::unnest_wider(implied)
</code>```

```<code># A tibble: 5 x 8
  grp_id name  lower upper    rn  ...1  ...2  ...3
  <chr>  <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 grp1   test  93790 93790     1 93790    NA    NA
2 grp1   test  93797 93798     2 93797 93798    NA
3 grp1   test  98960 98962     3 98960 98961 98962
4 grp1   test  98966 98968     4 98966 98967 98968
5 grp1   test  99078 99078     5 99078    NA    NA
</code>```

UPDATE to handle alpha characters:

```<code>df %>%
  separate_rows(code, sep = ",") %>%
  mutate(rn = row_number()) %>%
  separate(code, sep = "-", into = c("lower", "upper")) %>%
  mutate(lower = str_trim(lower),
         upper = str_trim(upper),
         alpha = str_extract(lower, "[A-Z]+"),
         lower = parse_number(lower),
         upper = case_when(
           !is.na(alpha) ~ as.character(NA),
           is.na(upper) ~ as.character(lower),
           TRUE ~ as.character(upper)
         ),
         upper = as.numeric(upper)
  ) %>%
  rowwise() %>%
  mutate(implied = ifelse(
    is.na(alpha), list( seq(lower, upper, by = 1)), NA
      )
    ) %>%
  tidyr::unnest_wider(implied) %>%
  unite(alpha, lower, col = lower, sep = "", na.rm = TRUE)
</code>```

```<code># A tibble: 7 x 8
  grp_id name  lower upper    rn  ...1  ...2  ...3
  <chr>  <chr> <chr> <dbl> <int> <dbl> <dbl> <dbl>
1 grp1   test  93790 93790     1 93790    NA    NA
2 grp1   test  93797 93798     2 93797 93798    NA
3 grp1   test  98960 98962     3 98960 98961 98962
4 grp1   test  98966 98968     4 98966 98967 98968
5 grp1   test  99078 99078     5 99078    NA    NA
6 grp1   test  G1501    NA     6    NA    NA    NA
7 grp1   test  G5017    NA     7    NA    NA    NA
</code>```

Body:
I have a dataset that has multiple values in the same row/column.  Some are separated by "-".  I am trying to split the columns appropriately and then add the values that are implied by the "-".  I've been able to get the dataset into the right format, but I am not having success in finding a way to create a sequence between lower and upper.

sample data set here (UPDATE:  Actual data set has 'codes' with alpha characters):

grp_id <- ("grp1")
name <- ("test")
code <- ("93790, 93797 - 93798, 98960 - 98962, 98966 - 98968, 99078, G1501, G5017")
df <- data.frame(grp_id, name, code)

what I have tried:

df %>%
  separate_rows(code, sep = ",") %>%
  mutate(rn = row_number()) %>%
  separate(code, sep = "-", into = c("lower", "upper")) %>%
  mutate(upper = case_when(is.na(upper) ~ lower,
                           TRUE ~ upper)) %>%
  mutate(implied = seq(as.numeric(lower), as.numeric(upper), by = 1))

this works great until the last mutate where I try to create new columns for the new values.

The expected output would be :

group_id  name  lower   upper implied.1  implied.2    rn
grp1      test   97390   97390  na        na          1
grp1      test   97397   97398  na        na          2
grp1      test   98960   98962  *98961*   na          3
grp1      test   G1501                                4
grp1      test   G5017                                5

So you can see in rn 3 the 98961 was added as it was the implied value by the "-".

Any suggestions I can try?

EDIT:  Since my actual data set has some alpha characters, this solution doesn't completely work.  I have edited as below, however it breaks down at the last mutate function.

df %>%
  separate_rows(code, sep = ",") %>%
  mutate(rn = row_number()) %>%
  separate(code, sep = "-", into = c("lower", "upper")) %>%
  mutate(upper = case_when(is.na(upper) ~ lower,
                           TRUE ~ upper)) %>%
  mutate(lower = case_when(
    str_detect(as.character(lower), '\\D') ~ lower,
    TRUE ~ as.character(as.numeric(lower))),
    upper = case_when(
      str_detect(as.character(lower), '\\D') ~ lower,
      TRUE ~ as.character(as.numeric(upper)))) %>%
  rowwise() %>%
  mutate(implied = list( seq(lower, upper, by = 1))) %>%
  tidyr::unnest_wider(implied)

Body:
Use dplyr::rowwise to create a tibble grouped by row, then make sure to wrap your seq statement in a list. Then use tidyr::unnest_wider to unnest the list, to get each value in the sequence in its own column. You could then just rename the columns with rename or rename_at.

Does this work for your needs?

df %>%
  separate_rows(code, sep = ",") %>%
  mutate(rn = row_number()) %>%
  separate(code, sep = "-", into = c("lower", "upper")) %>%
  mutate(upper = case_when(is.na(upper) ~ lower,
                           TRUE ~ upper)) %>%
  mutate(lower = as.numeric(lower), upper = as.numeric(upper)) %>%
  rowwise() %>%
  mutate(implied = list( seq(lower, upper, by = 1)) ) %>%
  tidyr::unnest_wider(implied)

# A tibble: 5 x 8
  grp_id name  lower upper    rn  ...1  ...2  ...3
  <chr>  <chr> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 grp1   test  93790 93790     1 93790    NA    NA
2 grp1   test  93797 93798     2 93797 93798    NA
3 grp1   test  98960 98962     3 98960 98961 98962
4 grp1   test  98966 98968     4 98966 98967 98968
5 grp1   test  99078 99078     5 99078    NA    NA

UPDATE to handle alpha characters:

df %>%
  separate_rows(code, sep = ",") %>%
  mutate(rn = row_number()) %>%
  separate(code, sep = "-", into = c("lower", "upper")) %>%
  mutate(lower = str_trim(lower),
         upper = str_trim(upper),
         alpha = str_extract(lower, "[A-Z]+"),
         lower = parse_number(lower),
         upper = case_when(
           !is.na(alpha) ~ as.character(NA),
           is.na(upper) ~ as.character(lower),
           TRUE ~ as.character(upper)
         ),
         upper = as.numeric(upper)
  ) %>%
  rowwise() %>%
  mutate(implied = ifelse(
    is.na(alpha), list( seq(lower, upper, by = 1)), NA
      )
    ) %>%
  tidyr::unnest_wider(implied) %>%
  unite(alpha, lower, col = lower, sep = "", na.rm = TRUE)

# A tibble: 7 x 8
  grp_id name  lower upper    rn  ...1  ...2  ...3
  <chr>  <chr> <chr> <dbl> <int> <dbl> <dbl> <dbl>
1 grp1   test  93790 93790     1 93790    NA    NA
2 grp1   test  93797 93798     2 93797 93798    NA
3 grp1   test  98960 98962     3 98960 98961 98962
4 grp1   test  98966 98968     4 98966 98967 98968
5 grp1   test  99078 99078     5 99078    NA    NA
6 grp1   test  G1501    NA     6    NA    NA    NA
7 grp1   test  G5017    NA     7    NA    NA    NA


来源: stackexchange

问:
How To exclude vertex with label name = 'Kpi' in gremlin
i have the next Query in GremlinDb:

```<code>g.V('61f4e0c0-2f17-4a0a-a874-ee7325c110e5').outE().has(label,'HasActionBook').inV().outE().has(label,'ActionBookHasSwimlane').inV().outE().inV()
</code>```

and returns to me all items but i have a item with label 'Kpi' how to do to exclude this items with this label ?

thanks for all.

答:
im solved it with this syntax.</p>

```<code>g.V('61f4e0c0-2f17-4a0a-a874-ee7325c110e5').outE().has(label,'HasActionBook').inV().outE().has(label,'ActionBookHasSwimlane').inV().outE().where(has(label, neq('SwimlaneHasKpi'))).inV()
</code>```

ContentLicense: CC BY-SA 4.0

Body:
i have the next Query in GremlinDb:

g.V('61f4e0c0-2f17-4a0a-a874-ee7325c110e5').outE().has(label,'HasActionBook').inV().outE().has(label,'ActionBookHasSwimlane').inV().outE().inV()

and returns to me all items but i have a item with label 'Kpi' how to do to exclude this items with this label ?

thanks for all.

Body:
im solved it with this syntax.

g.V('61f4e0c0-2f17-4a0a-a874-ee7325c110e5').outE().has(label,'HasActionBook').inV().outE().has(label,'ActionBookHasSwimlane').inV().outE().where(has(label, neq('SwimlaneHasKpi'))).inV()


问:
JQuery - is there a way to make less calls to the dom
i have this jquery function

```<code>    function update_prices(that, current_id, quantity) {
         //that == $(this)
        var wrapper = that.parents(".wrapper");
        var standard = wrapper.find(".prettyCheckbox small:eq(0)").parents("span").parents("label").prev("input").val();
        var professional = wrapper.find(".prettyCheckbox small:eq(1)").parents("span").parents("label").prev("input").val();
        var premium = wrapper.find(".prettyCheckbox small:eq(2)").parents("span").parents("label").prev("input").val();
        $.get("/shop_possystems/index.php?route=module/cart/ajax_get_individual_prices&standard_id=" + standard + "&professional_id" + professional + "&premium_id" + premium + "&quantity" + quantity;
            function(data) {
                var standard_price = data[0];
                var professional_price = data[0];
                var premium_price = data[0];
        });
        wrapper.find(".prettyCheckbox small:eq(0)").text(standard_price);
        wrapper.find(".prettyCheckbox small:eq(1)").text(professional_price);
        wrapper.find(".prettyCheckbox small:eq(2)").text(premium_price);
        return true;
    }
</code>```

and i feel like i am calling the same elements on the dom  more then i should ....is there anyone that can think of a better way to achieve what i want without making all these calls

答:
Cache the list of checkboxes and work off of that instead of the wrapper object you're working off of now:

```<code>var smallCheckboxes = that.parents(".wrapper").find(".prettyCheckbox small");
</code>```

In context, something like this:

```<code>function update_prices(that, current_id, quantity) {
    var smallCheckboxes = that.parents(".wrapper")
                              .find(".prettyCheckbox small");
    var standard = smallCheckboxes.eq(0).parents("span")
                                        .parents("label")
                                        .prev("input").val();
   //etc...
</code>```

ContentLicense: CC BY-SA 3.0

Body:
i have this jquery function

    function update_prices(that, current_id, quantity) {
         //that == $(this)
        var wrapper = that.parents(".wrapper");
        var standard = wrapper.find(".prettyCheckbox small:eq(0)").parents("span").parents("label").prev("input").val();
        var professional = wrapper.find(".prettyCheckbox small:eq(1)").parents("span").parents("label").prev("input").val();
        var premium = wrapper.find(".prettyCheckbox small:eq(2)").parents("span").parents("label").prev("input").val();
        $.get("/shop_possystems/index.php?route=module/cart/ajax_get_individual_prices&standard_id=" + standard + "&professional_id" + professional + "&premium_id" + premium + "&quantity" + quantity;
            function(data) {
                var standard_price = data[0];
                var professional_price = data[0];
                var premium_price = data[0];
        });
        wrapper.find(".prettyCheckbox small:eq(0)").text(standard_price);
        wrapper.find(".prettyCheckbox small:eq(1)").text(professional_price);
        wrapper.find(".prettyCheckbox small:eq(2)").text(premium_price);
        return true;
    }

and i feel like i am calling the same elements on the dom  more then i should ....is there anyone that can think of a better way to achieve what i want without making all these calls

Body:
Cache the list of checkboxes and work off of that instead of the wrapper object you're working off of now:

var smallCheckboxes = that.parents(".wrapper").find(".prettyCheckbox small");

In context, something like this:

function update_prices(that, current_id, quantity) {
    var smallCheckboxes = that.parents(".wrapper")
                              .find(".prettyCheckbox small");
    var standard = smallCheckboxes.eq(0).parents("span")
                                        .parents("label")
                                        .prev("input").val();
   //etc...


问:
Objective-C NSString memory leak with objc_setAssociatedObject
I found a memory leak for NSString with objc_setAssociatedObject

Test Code:

```<code>int i = 0;
while (YES) {
    @autoreleasepool {
        NSString *string = [[NSString alloc] initWithFormat:@"%d", i];
        // Comment this line. Then the memory leak is gone.
        objc_setAssociatedObject(string, "key", [[NSObject alloc] init], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    i++;
}
</code>```

NSObject can't release. Not sure for NSString.

If I comment the code objc_setAssociatedObject(string, "key", [[NSObject alloc] init], OBJC_ASSOCIATION_RETAIN_NONATOMIC);. There is no memory leak.

Does anyone know what happened?

答:
Maybe related to Tagged Pointer Strings

Tagger Pointer Strings are actually invalid 64 bit pointers where the content is stored within the pointer itself

this helps prevent unnecessary memory allocation

They are invalid pointers in the sense that they don't point to any real memory value

"""Objects are aligned in memory, such that their address is always at least a multiple of the pointer size, and in practice typically a multiple of 16. Object pointers are stored as a full 64-bit integer, but this alignment means that some of the bits will always be zero.

Tagged pointers take advantage of this fact to give special meaning to object pointers where those bits are not zero. In Apple's 64-bit Objective-C implementation, object pointers with the least significant bit set to one (which is to say, odd numbers) are considered tagged pointers. Instead of doing the standard isa dereference to figure out the class, the next three bits are considered as an index into a tagged class table. This index is used to look up the class of the tagged pointer. The remaining 60 bits are then left up to the tagged class to use as they please."""
This is a beautiful hack.

I think for NSTaggedPointerString teh associated objects might be getting cleared in the next runloop

Body:
I found a memory leak for NSString with objc_setAssociatedObject

Test Code:

int i = 0;
while (YES) {
    @autoreleasepool {
        NSString *string = [[NSString alloc] initWithFormat:@"%d", i];
        // Comment this line. Then the memory leak is gone.
        objc_setAssociatedObject(string, "key", [[NSObject alloc] init], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    i++;
}

NSObject can't release. Not sure for NSString.

If I comment the code objc_setAssociatedObject(string, "key", [[NSObject alloc] init], OBJC_ASSOCIATION_RETAIN_NONATOMIC);. There is no memory leak.

Does anyone know what happened?


问:
How to enable CORS in flask
I am trying to make a cross origin request using jquery but it keeps being reject with the message

XMLHttpRequest cannot load http://... No 'Access-Control-Allow-Origin'
  header is present on the requested resource. Origin ... is therefore
  not allowed access.

I am using flask, heroku, and jquery

the client code looks like this:

```<code>$(document).ready(function() {
    $('#submit_contact').click(function(e){
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'http://...',
            // data: [
            //      { name: "name", value: $('name').val()},
            //      { name: "email", value: $('email').val() },
            //      { name: "phone", value: $('phone').val()},
            //      { name: "description", value: $('desc').val()}
            //
            // ],
            data:"name=3&email=3&phone=3&description=3",
            crossDomain:true,
            success: function(msg) {
                alert(msg);
            }
        });
    });
});
</code>```

on the heroku side i am using flask and it is like this

```<code>from flask import Flask,request
from flask.ext.mandrill import Mandrill
try:
    from flask.ext.cors import CORS  # The typical way to import flask-cors
except ImportError:
    # Path hack allows examples to be run without installation.
    import os
    parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    os.sys.path.insert(0, parentdir)

    from flask.ext.cors import CORS
app = Flask(__name__)

app.config['MANDRILL_API_KEY'] = '...'
app.config['MANDRILL_DEFAULT_FROM']= '...'
app.config['QOLD_SUPPORT_EMAIL']='...'
app.config['CORS_HEADERS'] = 'Content-Type'

mandrill = Mandrill(app)
cors = CORS(app)

@app.route('/email/',methods=['POST'])
def hello_world():
    name=request.form['name']
    email=request.form['email']
    phone=request.form['phone']
    description=request.form['description']

    mandrill.send_email(
        from_email=email,
        from_name=name,
        to=[{'email': app.config['QOLD_SUPPORT_EMAIL']}],
        text="Phone="+phone+"\n\n"+description
    )

    return '200 OK'

if __name__ == '__main__':
    app.run()
</code>```

答:
If you can't find your problem and you're code should work, it may be that your request is just reaching the maximum of time heroku allows you to make a request. Heroku cancels requests if it takes more than 30 seconds.

Reference: https://devcenter.heroku.com/articles/request-timeout

Title: How to enable CORS in flask

Body:
I am trying to make a cross origin request using jquery but it keeps being reject with the message

XMLHttpRequest cannot load http://... No 'Access-Control-Allow-Origin'
  header is present on the requested resource. Origin ... is therefore
  not allowed access.

I am using flask, heroku, and jquery

the client code looks like this:

$(document).ready(function() {
    $('#submit_contact').click(function(e){
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'http://...',
            // data: [
            //      { name: "name", value: $('name').val()},
            //      { name: "email", value: $('email').val() },
            //      { name: "phone", value: $('phone').val()},
            //      { name: "description", value: $('desc').val()}
            //
            // ],
            data:"name=3&email=3&phone=3&description=3",
            crossDomain:true,
            success: function(msg) {
                alert(msg);
            }
        });
    });
});

on the heroku side i am using flask and it is like this

from flask import Flask,request
from flask.ext.mandrill import Mandrill
try:
    from flask.ext.cors import CORS  # The typical way to import flask-cors
except ImportError:
    # Path hack allows examples to be run without installation.
    import os
    parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    os.sys.path.insert(0, parentdir)

    from flask.ext.cors import CORS
app = Flask(__name__)

app.config['MANDRILL_API_KEY'] = '...'
app.config['MANDRILL_DEFAULT_FROM']= '...'
app.config['QOLD_SUPPORT_EMAIL']='...'
app.config['CORS_HEADERS'] = 'Content-Type'

mandrill = Mandrill(app)
cors = CORS(app)

@app.route('/email/',methods=['POST'])
def hello_world():
    name=request.form['name']
    email=request.form['email']
    phone=request.form['phone']
    description=request.form['description']

    mandrill.send_email(
        from_email=email,
        from_name=name,
        to=[{'email': app.config['QOLD_SUPPORT_EMAIL']}],
        text="Phone="+phone+"\n\n"+description
    )

    return '200 OK'

if __name__ == '__main__':
    app.run()


问:
Spring RestTemplate response body is null when trying to parse JSON
I've been trying many solutions from similar solved problems in this page but I can't make it work. I'm making a get petition to obtain an array JSON, and I want to map those values into my class.

I got this class:

```<code>public class Devices {

    private String DeviceName;
    private String DeviceDescription;

    public String getDeviceName() {
        return DeviceName;
    }

    public void setDeviceName(String deviceName) {
        this.DeviceName = deviceName;
    }

    public String getDeviceDescription() {
        return DeviceDescription;
    }

    public void setDeviceDescription(String deviceDescription) {
        this.DeviceDescription = deviceDescription;
    }

}
</code>```

The GET petition returns this JSON below:

```<code>[{"DeviceName":"AMIXT-20EC-VIDM0000","DeviceDescription":"Samsung device "},{"DeviceName":"AMIXT-E0F9-VIDM0001","DeviceDescription":"Tablet Huawei"}]
</code>```

I've tried solutions like this one (also tried with getForObject):

```<code>ResponseEntity<Devices[]> responseEntity = restTemplate.getForEntity(url, Devices[].class);
</code>```

As we can see in this photo, the body properties are null

What am I missing?

答:
The issue is related to the name of the fields in the JSON, name start with an upper-case letter.

One simple solution would be to use @JsonProperty annotation on the variables defined in the Devices class

public class Devices {

    @JsonProperty("DeviceName")
    private String deviceName;

    @JsonProperty("DeviceDescription")
    private String deviceDescription;

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public String getDeviceDescription() {
        return deviceDescription;
    }

    public void setDeviceDescription(String deviceDescription) {
        this.deviceDescription = deviceDescription;
    }
}
```

Body:
I've been trying many solutions from similar solved problems in this page but I can't make it work. I'm making a get petition to obtain an array JSON, and I want to map those values into my class.

I got this class:

public class Devices {

    private String DeviceName;
    private String DeviceDescription;

    public String getDeviceName() {
        return DeviceName;
    }

    public void setDeviceName(String deviceName) {
        this.DeviceName = deviceName;
    }

    public String getDeviceDescription() {
        return DeviceDescription;
    }

    public void setDeviceDescription(String deviceDescription) {
        this.DeviceDescription = deviceDescription;
    }

}

The GET petition returns this JSON below:

[{"DeviceName":"AMIXT-20EC-VIDM0000","DeviceDescription":"Samsung device "},{"DeviceName":"AMIXT-E0F9-VIDM0001","DeviceDescription":"Tablet Huawei"}]

I've tried solutions like this one (also tried with getForObject):

ResponseEntity<Devices[]> responseEntity = restTemplate.getForEntity(url, Devices[].class);

As we can see in this photo, the body properties are null

What am I missing?


问:
XML Nodeset to R dataframe
I have a nodeset I've extracted from an XML file that looks like this:

```<code>   <section id="A00-A09">
      <desc>Intestinal infectious diseases (A00-A09)</desc>
      <diag>
        <name>A00</name>
        <desc>Cholera</desc>
        <diag>
          <name>A00.0</name>
          <desc>Cholera due to Vibrio cholerae 01, biovar cholerae</desc>
          <inclusionTerm>
            <note>Classical cholera</note>
          </inclusionTerm>
        </diag>
        <diag>
          <name>A00.1</name>
          <desc>Cholera due to Vibrio cholerae 01, biovar eltor</desc>
          <inclusionTerm>
            <note>Cholera eltor</note>
          </inclusionTerm>
        </diag>
        <diag>
          <name>A00.9</name>
          <desc>Cholera, unspecified</desc>
        </diag>
      </diag>
</code>```

How can I convert this into a dataframe in R that would look like:

```<code>name      desc
A00       Cholera
A00.0     biovar cholerae
A00.1     biovar eltor
A00.9     Cholera, unspecified
</code>```

I've looked at a couple of posts covering XML to df in R but they all seem to be specified to the xml file one is working with (which makes sense given xml), but I'm new to extracting data from xml and haven't been able to find something that works on 44487 elements in my xml_NodeSet. FYI, I used xml2 for parsing the xml file.

答:
Thanks @Nadia for the suggestion. There was no URL for the XML file I was working with so I had to use xml_nodes() & xml_text().

I did have trouble with the missing values for note & luckily after looking at the data this would not be as valuable as I previously anticipated. Thus, I was able to drop it & that made the ask much more reasonable.

Here is what I did:

```<code>#after extracting the nodeset from my xml file
names <- xml_text(xml_nodes(diags, "name"))
desc <- xml_text(xml_nodes(diags, "desc"))

#convert to lists
names_list <- list(names)
desc_list <- list(desc)

#bring into a dataframe
df <- do.call(rbind, Map(data.frame, A=names_list, B=desc_list))

head(df)
  icdcode                                               name
1     A00                                            Cholera
2   A00.0 Cholera due to Vibrio cholerae 01, biovar cholerae
3   A00.1    Cholera due to Vibrio cholerae 01, biovar eltor
4   A00.9                               Cholera, unspecified
5     A01                     Typhoid and paratyphoid fevers
6   A01.0                                      Typhoid fever
</code>```

Title: XML Nodeset to R dataframe

Body:
I have a nodeset I've extracted from an XML file that looks like this:

      <desc>Intestinal infectious diseases (A00-A09)</desc>
      <diag>
        <name>A00</name>
        <desc>Cholera</desc>
        <diag>
          <name>A00.0</name>
          <desc>Cholera due to Vibrio cholerae 01, biovar cholerae</desc>
          <inclusionTerm>
            <note>Classical cholera</note>
          </inclusionTerm>
        </diag>
        <diag>
          <name>A00.1</name>
          <desc>Cholera due to Vibrio cholerae 01, biovar eltor</desc>
          <inclusionTerm>
            <note>Cholera eltor</note>
          </inclusionTerm>
        </diag>
        <diag>
          <name>A00.9</name>
          <desc>Cholera, unspecified</desc>
        </diag>
      </diag>

How can I convert this into a dataframe in R that would look like:

name      desc
A00       Cholera
A00.0     biovar cholerae
A00.1     biovar eltor
A00.9     Cholera, unspecified

I've looked at a couple of posts covering XML to df in R but they all seem to be specified to the xml file one is working with (which makes sense given xml), but I'm new to extracting data from xml and haven't been able to find something that works on 44487 elements in my xml_NodeSet. FYI, I used xml2 for parsing the xml file.

Body:
Thanks @Nadia for the suggestion. There was no URL for the XML file I was working with so I had to use xml_nodes() & xml_text().

I did have trouble with the missing values for note & luckily after looking at the data this would not be as valuable as I previously anticipated. Thus, I was able to drop it & that made the ask much more reasonable.

Here is what I did:

#after extracting the nodeset from my xml file
names <- xml_text(xml_nodes(diags, "name"))
desc <- xml_text(xml_nodes(diags, "desc"))

#convert to lists
names_list <- list(names)
desc_list <- list(desc)

#bring into a dataframe
df <- do.call(rbind, Map(data.frame, A=names_list, B=desc_list))

head(df)
  icdcode                                               name
1     A00                                            Cholera
2   A00.0 Cholera due to Vibrio cholerae 01, biovar cholerae
3   A00.1    Cholera due to Vibrio cholerae 01, biovar eltor
4   A00.9                               Cholera, unspecified
5     A01                     Typhoid and paratyphoid fevers
6   A01.0                                      Typhoid fever


问:
Adapter list item update issue
I'm working on a screen where I have to display twice the same collection of items. I'm using two RecyclerView's each with a different instance of the same Adapter

Besides the two RecyclerViews in the screen I have a continue button to go to the next screen.

I want to put some constraints on the button so that the user will not be able to proceed to the next screen if he doesn't select an item from each RecyclerView

The following method is called when the user taps on an item.

```<code>fun setSelection(item: Item) {
    list.forEach {
        it.isSelected = false
        if (it.id == item.id) it.isSelected = true
    }
    notifyDataSetChanged()
}
</code>```

And to verify if the list has at least one selected item I use this:

```<code>fun isAnySelected(): Boolean {
    return list.any { item -> item.isSelected }
}
</code>```

In the button's clickListener I added this verification:

```<code>if ((first_recycler.adapter as CustomAdapter).isAnySelected() &&
   (second_recycler.adapter as CustomAdapter).isAnySelected())
{ //go to next screen }
else{
// select an item before continue }
</code>```

My problem is that if I select an item only from one RecyclerView, when I tap on the continue button both of them will return true.

Why does (second_recycler.adapter as CustomAdapter).isAnySelected() returns true if only

```<code>(first_massage_display.mdl_recycler.adapter as MassageAdapter).setSelection(item)
</code>```

Is called?

答:
Sounds like the lists in each adapter are referencing the same list item instances. This could be because you passed the same list to both adapters, or simply that you have not created distinct list item instances in each list. If two lists reference the same list items, than changing a list item in one list also changes it in the other.

I recommend making Item a data class so you can easily make copies. Then create a copy of the list where every member of the list is also copied, and pass that to the second Adapter. You can use map() to do both steps of copying the list and the items within it all at once:

```<code>val secondList = firstList.map(Item::copy)
</code>```

Title: Adapter list item update issue

Body:
I'm working on a screen where I have to display twice the same collection of items. I'm using two RecyclerView's each with a different instance of the same Adapter

Besides the two RecyclerViews in the screen I have a continue button to go to the next screen.

I want to put some constraints on the button so that the user will not be able to proceed to the next screen if he doesn't select an item from each RecyclerView

The following method is called when the user taps on an item.

fun setSelection(item: Item) {
    list.forEach {
        it.isSelected = false
        if (it.id == item.id) it.isSelected = true
    }
    notifyDataSetChanged()
}

And to verify if the list has at least one selected item I use this:

fun isAnySelected(): Boolean {
    return list.any { item -> item.isSelected }
}

In the button's clickListener I added this verification:

if ((first_recycler.adapter as CustomAdapter).isAnySelected() &&
   (second_recycler.adapter as CustomAdapter).isAnySelected())
{ //go to next screen }
else{
// select an item before continue }

My problem is that if I select an item only from one RecyclerView, when I tap on the continue button both of them will return true.

Why does (second_recycler.adapter as CustomAdapter).isAnySelected() returns true if only

(first_massage_display.mdl_recycler.adapter as MassageAdapter).setSelection(item)

Is called?

Body:
Sounds like the lists in each adapter are referencing the same list item instances. This could be because you passed the same list to both adapters, or simply that you have not created distinct list item instances in each list. If two lists reference the same list items, than changing a list item in one list also changes it in the other.

I recommend making Item a data class so you can easily make copies. Then create a copy of the list where every member of the list is also copied, and pass that to the second Adapter. You can use map() to do both steps of copying the list and the items within it all at once:

val secondList = firstList.map(Item::copy)


问:
Jetty SSL problem after upgrade - unknown protocol
I upgraded Jetty 9.3.6 to Jetty 9.4.27 and I have a problem with SSL connection.

When I run curl on some supported operation I get
error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (error 35) whereas when I run the same command on the server running Jetty 9.3.6 it is all working fine.

I configured jetty's newer version in the same way as was the older one (including keystore path and enabling https support). Do you have a clue what could have gone wrong during upgrade or what I could have missed?

Thanks a lot for your support.

答:
Jetty 9.3.x and 9.4.x have different Cipher Suite exclusions.

Jetty 9.3.6.v20151106 looks like this ...

https://github.com/eclipse/jetty.project/blob/jetty-9.3.6.v20151106/jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslContextFactory.java#L248-L260

        addExcludeProtocols("SSL", "SSLv2", "SSLv2Hello", "SSLv3");
        setExcludeCipherSuites(
                "SSL_RSA_WITH_DES_CBC_SHA",
                "SSL_DHE_RSA_WITH_DES_CBC_SHA",
                "SSL_DHE_DSS_WITH_DES_CBC_SHA",
                "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
                "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
                "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
                "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
```

<p>Jetty 9.4.29.v20200521 looks like this ...</p>

<p>https://github.com/eclipse/jetty.project/blob/jetty-9.4.29.v20200521/jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslContextFactory.java#L119-L138</p>

<pre class="lang-java prettyprint-override"><code>    /**
     * Default Excluded Protocols List
     */
    private static final String[] DEFAULT_EXCLUDED_PROTOCOLS = {"SSL", "SSLv2", "SSLv2Hello", "SSLv3"};
    /**
     * Default Excluded Cipher Suite List
     */
    private static final String[] DEFAULT_EXCLUDED_CIPHER_SUITES = {
        // Exclude weak / insecure ciphers
        "^.*_(MD5|SHA|SHA1)$",
        // Exclude ciphers that don't support forward secrecy
        "^TLS_RSA_.*$",
        // The following exclusions are present to cleanup known bad cipher
        // suites that may be accidentally included via include patterns.
        // The default enabled cipher list in Java will not include these
        // (but they are available in the supported list).
        "^SSL_.*$",
        "^.*_NULL_.*$",
        "^.*_anon_.*$"
    };
</code>```

You'll want to evaluate why you need known vulnerable cipher suites to function.

Also, if you are using an IBM JVM all bets are off, because the IBM uses non-standard Cipher Suite names (unlike all other JVMs that use the RFC Registered Cipher Suite names).

Body:
Jetty 9.3.x and 9.4.x have different Cipher Suite exclusions.

Jetty 9.3.6.v20151106 looks like this ...

https://github.com/eclipse/jetty.project/blob/jetty-9.3.6.v20151106/jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslContextFactory.java#L248-L260

        addExcludeProtocols("SSL", "SSLv2", "SSLv2Hello", "SSLv3");
        setExcludeCipherSuites(
                "SSL_RSA_WITH_DES_CBC_SHA",
                "SSL_DHE_RSA_WITH_DES_CBC_SHA",
                "SSL_DHE_DSS_WITH_DES_CBC_SHA",
                "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
                "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA",
                "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
                "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");

Jetty 9.4.29.v20200521 looks like this ...

https://github.com/eclipse/jetty.project/blob/jetty-9.4.29.v20200521/jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SslContextFactory.java#L119-L138

    /**
     * Default Excluded Protocols List
     */
    private static final String[] DEFAULT_EXCLUDED_PROTOCOLS = {"SSL", "SSLv2", "SSLv2Hello", "SSLv3"};
    /**
     * Default Excluded Cipher Suite List
     */
    private static final String[] DEFAULT_EXCLUDED_CIPHER_SUITES = {
        // Exclude weak / insecure ciphers
        "^.*_(MD5|SHA|SHA1)$",
        // Exclude ciphers that don't support forward secrecy
        "^TLS_RSA_.*$",
        // The following exclusions are present to cleanup known bad cipher
        // suites that may be accidentally included via include patterns.
        // The default enabled cipher list in Java will not include these
        // (but they are available in the supported list).
        "^SSL_.*$",
        "^.*_NULL_.*$",
        "^.*_anon_.*$"
    };

You'll want to evaluate why you need known vulnerable cipher suites to function.

Also, if you are using an IBM JVM all bets are off, because the IBM uses non-standard Cipher Suite names (unlike all other JVMs that use the RFC Registered Cipher Suite names).


问:
Templating does not work in Airflow's on_failure_callback
Issue: Templates in operators used in on_failure_callback are not rendered.

def report_failure(context):
    send_email = EmailOperator(
        task_id="email_failed",
        to=emailreceipients,
        subject="{{execution_date}}",
        html_content=get_email_body() # Which returns "Body: {{execution_date}}"
    )

    # One solution I tried fails on: AttributeError: 'EmailOperator' object has no attribute 'render_template_fields'
    # Which is weird as this method is present on EmailOperator's base: BaseOperator
    # send_email.render_template_fields(context)

    send_email.execute(context)

default_args = {
    'on_failure_callback': report_failure
}
```

The email getting sent includes literally {{execution_date}} instead of rendered value.

In this simple case I could format these templated strings in place using .format(**context), but I have this email sending reused in other places where context is not available and templating works fine.

The bigger goal is to have this "error handler" fired anytime any steps of the DAG (or its subdags) fail.

答:
Need help on rendering the jinja template email ID in the On_failure_callback ..

It works fine with Variable.get('email_edw_alert'), but i don't want to use Variable method to avoid hitting DB

Below is the Dag file

```<code>import datetime
import os
from functools import partial
from datetime import timedelta
from airflow.models import DAG,Variable
from airflow.contrib.operators.snowflake_operator import SnowflakeOperator
from alerts.email_operator import dag_failure_email

def get_db_dag(
    *,
    dag_id,
    start_date,
    schedule_interval,
    max_taskrun,
    max_dagrun,
    proc_nm,
    load_sql
):

    default_args = {
        'owner': 'airflow',
        'start_date': start_date,
        'provide_context': True,
        'execution_timeout': timedelta(minutes=max_taskrun),
        'retries': 0,
        'retry_delay': timedelta(minutes=3),
        'retry_exponential_backoff': True,
        'email_on_retry': False,
    }

    dag = DAG(
        dag_id=dag_id,
        schedule_interval=schedule_interval,
        dagrun_timeout=timedelta(hours=max_dagrun),
        template_searchpath=tmpl_search_path,
        default_args=default_args,
        max_active_runs=1,
        catchup='{{var.value.dag_catchup}}',
        on_failure_callback=partial(dag_failure_email, config={'email_address': '{{var.value.email_edw_alert}}'}),
    )

    load_table = SnowflakeOperator(
        task_id='load_table',
        sql=load_sql,
        snowflake_conn_id=CONN_ID,
        autocommit=True,
        dag=dag,
    )

    load_table

    return dag

# ======== DAG DEFINITIONS #

edw_table_A = get_db_dag(
    dag_id='edw_table_A',
    start_date=datetime.datetime(2020, 5, 21),
    schedule_interval='0 5 * * *',
    max_taskrun=3,  # Minutes
    max_dagrun=1,  # Hours
    load_sql='recon/extract.sql',
)
</code>```

Below is the python code

```<code>import logging
from airflow.utils.email import send_email
from airflow.models import Variable

logger = logging.getLogger(__name__)

TIME_FORMAT = "%Y-%m-%d %H:%M:%S"

def dag_failure_email(context, config=None):

    config = {} if config is None else config
    task_id = context.get('task_instance').task_id
    dag_id = context.get("dag").dag_id
    execution_time = context.get("execution_date").strftime(TIME_FORMAT)
    reason = context.get("reason")

    alerting_email_address = config.get('email_address')

    dag_failure_html_body = f"""<html>
    <header><title>The following DAG has failed!</title></header>
    <body>
    <b>DAG Name</b>: {dag_id}<br/>
    <b>Task Id</b>: {task_id}<br/>
    <b>Execution Time (UTC)</b>: {execution_time}<br/>
    <b>Reason for Failure</b>: {reason}<br/>
    </body>
    </html>
    """

    try:
        if reason != 'dagrun_timeout':
            send_email(
                to=alerting_email_address,
                subject=f"Airflow alert: <DagInstance: {dag_id} - {execution_time} [failed]",
                html_content=dag_failure_html_body,
            )
    except Exception as e:
        logger.error(
            f'Error in sending email to address {alerting_email_address}: {e}',
            exc_info=True,
        )
</code>```

I have also tried another way too, even below one is not working

```<code> try:
        if reason != 'dagrun_timeout':
            send_email = EmailOperator(
               to=alerting_email_address,
               task_id='email_task',
               subject=f"Airflow alert: <DagInstance: {dag_id} - {execution_time} [failed]",
               params={'content1': 'random'},
               html_content=dag_failure_html_body,
           )
            send_email.dag = context['dag']
            #send_email.to = send_email.get_template_env().from_string(send_email.to).render(**context)
            send_email.to = send_email.render_template(alerting_email_address, send_email.to, context)
            send_email.execute(context)
    except Exception as e:
        logger.error(
            f'Error in sending email to address {alerting_email_address}: {e}',
            exc_info=True,
        )
</code>```

Body:
Issue: Templates in operators used in on_failure_callback are not rendered.

def report_failure(context):
    send_email = EmailOperator(
        task_id="email_failed",
        to=emailreceipients,
        subject="{{execution_date}}",
        html_content=get_email_body() # Which returns "Body: {{execution_date}}"
    )

    # One solution I tried fails on: AttributeError: 'EmailOperator' object has no attribute 'render_template_fields'
    # Which is weird as this method is present on EmailOperator's base: BaseOperator
    # send_email.render_template_fields(context)

    send_email.execute(context)

default_args = {
    'on_failure_callback': report_failure
}

The email getting sent includes literally {{execution_date}} instead of rendered value.

In this simple case I could format these templated strings in place using .format(**context), but I have this email sending reused in other places where context is not available and templating works fine.

The bigger goal is to have this "error handler" fired anytime any steps of the DAG (or its subdags) fail.

Body:
Need help on rendering the jinja template email ID in the On_failure_callback ..

It works fine with Variable.get('email_edw_alert'), but i don't want to use Variable method to avoid hitting DB

Below is the Dag file

import datetime
import os
from functools import partial
from datetime import timedelta
from airflow.models import DAG,Variable
from airflow.contrib.operators.snowflake_operator import SnowflakeOperator
from alerts.email_operator import dag_failure_email

def get_db_dag(
    *,
    dag_id,
    start_date,
    schedule_interval,
    max_taskrun,
    max_dagrun,
    proc_nm,
    load_sql
):

    default_args = {
        'owner': 'airflow',
        'start_date': start_date,
        'provide_context': True,
        'execution_timeout': timedelta(minutes=max_taskrun),
        'retries': 0,
        'retry_delay': timedelta(minutes=3),
        'retry_exponential_backoff': True,
        'email_on_retry': False,
    }

    dag = DAG(
        dag_id=dag_id,
        schedule_interval=schedule_interval,
        dagrun_timeout=timedelta(hours=max_dagrun),
        template_searchpath=tmpl_search_path,
        default_args=default_args,
        max_active_runs=1,
        catchup='{{var.value.dag_catchup}}',
        on_failure_callback=partial(dag_failure_email, config={'email_address': '{{var.value.email_edw_alert}}'}),
    )

    load_table = SnowflakeOperator(
        task_id='load_table',
        sql=load_sql,
        snowflake_conn_id=CONN_ID,
        autocommit=True,
        dag=dag,
    )

    load_table

    return dag

# ======== DAG DEFINITIONS #

edw_table_A = get_db_dag(
    dag_id='edw_table_A',
    start_date=datetime.datetime(2020, 5, 21),
    schedule_interval='0 5 * * *',
    max_taskrun=3,  # Minutes
    max_dagrun=1,  # Hours
    load_sql='recon/extract.sql',
)

Below is the python code

import logging
from airflow.utils.email import send_email
from airflow.models import Variable

logger = logging.getLogger(__name__)

TIME_FORMAT = "%Y-%m-%d %H:%M:%S"

def dag_failure_email(context, config=None):

    config = {} if config is None else config
    task_id = context.get('task_instance').task_id
    dag_id = context.get("dag").dag_id
    execution_time = context.get("execution_date").strftime(TIME_FORMAT)
    reason = context.get("reason")

    alerting_email_address = config.get('email_address')

    dag_failure_html_body = f"""<html>
    <header><title>The following DAG has failed!</title></header>
    <body>
    <b>DAG Name</b>: {dag_id}<br/>
    <b>Task Id</b>: {task_id}<br/>
    <b>Execution Time (UTC)</b>: {execution_time}<br/>
    <b>Reason for Failure</b>: {reason}<br/>
    </body>
    </html>
    """

    try:
        if reason != 'dagrun_timeout':
            send_email(
                to=alerting_email_address,
                subject=f"Airflow alert: <DagInstance: {dag_id} - {execution_time} [failed]",
                html_content=dag_failure_html_body,
            )
    except Exception as e:
        logger.error(
            f'Error in sending email to address {alerting_email_address}: {e}',
            exc_info=True,
        )

I have also tried another way too, even below one is not working

 try:
        if reason != 'dagrun_timeout':
            send_email = EmailOperator(
               to=alerting_email_address,
               task_id='email_task',
               subject=f"Airflow alert: <DagInstance: {dag_id} - {execution_time} [failed]",
               params={'content1': 'random'},
               html_content=dag_failure_html_body,
           )
            send_email.dag = context['dag']
            #send_email.to = send_email.get_template_env().from_string(send_email.to).render(**context)
            send_email.to = send_email.render_template(alerting_email_address, send_email.to, context)
            send_email.execute(context)
    except Exception as e:
        logger.error(
            f'Error in sending email to address {alerting_email_address}: {e}',
            exc_info=True,
        )


问:
Add and implement a spinner in fragment
I have seen guides and tutorials implementing spinner for activities. However the same method doesn't work for fragments. This was my code inside the onCreateView method of a fragment class

```<code>val personnames = arrayOf("akshat" , "mourya")
    val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, personnames)
    spinner.adapter = arrayAdapter
    spinner.onItemSelectedListener = object :

    AdapterView.OnItemSelectedListener {
        override fun onNothingSelected(parent: AdapterView<*>?) {

        }

        override fun onItemSelected(
            parent: AdapterView<*>?,
            view: View?,
            position: Int,
            id: Long
        ) {

            spinnerText.text = personnames[position]
        }
</code>```

When the ArrayAdapter method is called on line 2, I get a type mismatch error because the first parameter requires a context and in my code it is a fragment.
I tried doing the same thing in the activity and it worked.
Fragment are supposed to be like mini activities, so why this error propping up?

Also is there any way I can add spinner to my Fragment?

答:
Fragment is not a subclass of Context so you can't pass this as the first argument of the ArrayAdapter constructor.
When you need a Context object inside a Fragment you need to use either one of the two functions requireContext() or requireActivity().

Title: Add and implement a spinner in fragment

Body:
I have seen guides and tutorials implementing spinner for activities. However the same method doesn't work for fragments. This was my code inside the onCreateView method of a fragment class

val personnames = arrayOf("akshat" , "mourya")
    val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, personnames)
    spinner.adapter = arrayAdapter
    spinner.onItemSelectedListener = object :

    AdapterView.OnItemSelectedListener {
        override fun onNothingSelected(parent: AdapterView<*>?) {

        }

        override fun onItemSelected(
            parent: AdapterView<*>?,
            view: View?,
            position: Int,
            id: Long
        ) {

            spinnerText.text = personnames[position]
        }

When the ArrayAdapter method is called on line 2, I get a type mismatch error because the first parameter requires a context and in my code it is a fragment.
I tried doing the same thing in the activity and it worked.
Fragment are supposed to be like mini activities, so why this error propping up?

Also is there any way I can add spinner to my Fragment?


问:
Spring: Roles, authorities and permissions for entities
Good afternoon.
How can I configure permissions and roles for a user relative to a specific controller for a specific entity (something similar to RBAC/ABAC) within one application without usage of different OAUTH2 services?

Example:

App has global USER roles: ROLE_USER, ROLE_ADMIN.

Also there's entity named Project. There may be many Projects that USER may be a member of.
There're some Project USER roles: PROJECT_ADMIN, PROJECT_EDITOR, PROJECT_USER.
Each project role can have their own permissions (authorities) like add user to project, edit project and so on. All role configs are stored in 'project_roles' and 'project_permissions' tables.

As I understood it can be implemented through PreAuthorize annotation, but how can current global user be matched to project role?

```<code>@PreAuthorize("@projectService.hasPermission('create_project', #projectEditRequest.id)") // How to pass GlobalUserID?
@PostMapping
public ProjectDto create(@RequestBody @Validated ProjectEditRequest projectEditRequest) {
   ...
   ...
}

</code>```

Maybe there're examples of such case implementation?

Thanks in advance!

Analog questions:

Spring boot security roles per entity

Roles connected with entities in Java Spring (without answer)

答:
First of all OAuth2 is not the right way to do this at all, since you are talking about fine grained, entity-based authorizations. Something like this you would implement with a library like Spring ACL. Your authorization model would then usually reside in the same database as your secured entities and Spring Security would grant requested CRUD operations by the ACL (Access Control List) and the current spring security (user) context.

From OAuth2 you would basically just use the subject/user as the owner of existing ACL entries.

Body:
Good afternoon.
How can I configure permissions and roles for a user relative to a specific controller for a specific entity (something similar to RBAC/ABAC) within one application without usage of different OAUTH2 services?

Example:

App has global USER roles: ROLE_USER, ROLE_ADMIN.

Also there's entity named Project. There may be many Projects that USER may be a member of.
There're some Project USER roles: PROJECT_ADMIN, PROJECT_EDITOR, PROJECT_USER.
Each project role can have their own permissions (authorities) like add user to project, edit project and so on. All role configs are stored in 'project_roles' and 'project_permissions' tables.

As I understood it can be implemented through PreAuthorize annotation, but how can current global user be matched to project role?

@PreAuthorize("@projectService.hasPermission('create_project', #projectEditRequest.id)") // How to pass GlobalUserID?
@PostMapping
public ProjectDto create(@RequestBody @Validated ProjectEditRequest projectEditRequest) {
   ...
   ...
}

Maybe there're examples of such case implementation?

Thanks in advance!

Analog questions:

Spring boot security roles per entity

Roles connected with entities in Java Spring (without answer)


问:
Swift - converting string to date returning one day prior to correct date
I'm trying to convert a date string to a Date in Swift. My date string is returning as:

"28/05/2020"

I have this method:

```<code>    private func getDate(date: String) -> Date? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd/MM/yyyy"
    return dateFormatter.date(from: date)
}
</code>```

But it is returning:

```<code>Optional(2020-05-27 23:00:00 +0000)
</code>```

Why is this returning the 27th and not the 28th?

答:
Try This Out , it will work

```<code>func convertStringintoDate(dateStr:String, dateFormat format:String) -> Date{
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale.init(identifier:"en_US_POSIX")
    dateFormatter.timeZone = TimeZone.init(abbreviation: "GMT")
    dateFormatter.dateFormat = format
    if let startDate = dateFormatter.date(from: dateStr){
        return startDate as Date
    }
    return Date() as Date
}
</code>```

Call this function like this :-

```<code> print(convertStringintoDate(dateStr: "28/05/2020", dateFormat: "dd/MM/yyyy"))
</code>```

Body:
I'm trying to convert a date string to a Date in Swift. My date string is returning as:

"28/05/2020"

I have this method:

    private func getDate(date: String) -> Date? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd/MM/yyyy"
    return dateFormatter.date(from: date)
}

But it is returning:

Optional(2020-05-27 23:00:00 +0000)

Why is this returning the 27th and not the 28th?

Body:
Try This Out , it will work

func convertStringintoDate(dateStr:String, dateFormat format:String) -> Date{
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale.init(identifier:"en_US_POSIX")
    dateFormatter.timeZone = TimeZone.init(abbreviation: "GMT")
    dateFormatter.dateFormat = format
    if let startDate = dateFormatter.date(from: dateStr){
        return startDate as Date
    }
    return Date() as Date
}

Call this function like this :-

 print(convertStringintoDate(dateStr: "28/05/2020", dateFormat: "dd/MM/yyyy"))


问:
Azure Graph Api Fetch External User's Information
I set up a webhook for Azure Graph CallRecords to capture data on Teams calls. All of the data comes back perfect.

There is though one instance I don't know how to handle. Having a Teams call with external user, not part of our AD. The CallRecord response return a object as follow for the external user:

```<code>"user": {
            "id": "<user-id>",
            "displayName": "External user",
            "tenantId": "<tenant-id>"
        }
</code>```

I though that since we invited the person to our call he will be automatically be added as a guest user to our AD. But seems not to be the case. So we can't link that external user to substantial information like an email. I tried running a graph user api query; But it return resource not found.

Am I missing something, is there a way I can query the external user's information?

答:
In teams, there's a difference between a guest user and an external user. a guest user is a user that someone has added to any team in your tenant and thus your azure ad tenant. an external user is a user already on teams or skype but not in your tenant. they are federated only and can only call and chat, cannot send files or join a team.  in order to have a user in your azure ad, that user must be added to a team or manually added as a guest in your azure ad. if you just chat or call an external federated user without adding them to your tenant, it's simply an federated chat/call with no relation whatsoever with your tenant.
through graph api or any other means you won't be able to get very much information from those users that are externally federated. whereas you will be able to do basically full reporting on a guest users.

hopefully this helps clarify.

update
once you add them as a guest, they have to accept and join your tenant as a guest. then in their teams they will have the option to select your tenant.

Body:
I set up a webhook for Azure Graph CallRecords to capture data on Teams calls. All of the data comes back perfect.

There is though one instance I don't know how to handle. Having a Teams call with external user, not part of our AD. The CallRecord response return a object as follow for the external user:

"user": {
            "id": "<user-id>",
            "displayName": "External user",
            "tenantId": "<tenant-id>"
        }

I though that since we invited the person to our call he will be automatically be added as a guest user to our AD. But seems not to be the case. So we can't link that external user to substantial information like an email. I tried running a graph user api query; But it return resource not found.

Am I missing something, is there a way I can query the external user's information?


问:
Replacing Fill Colors in JSFL
I am attempting to write a JSFL script that will replace all contour fills that match given colors on selected frames.

My problem is that if I call shape.beginEdit() and shape.endEdit() as specified by the JSFL documentation, the script crashes whenever I access a fill attribute on a contour (even read only access). The error message is "The following JavaScript error(s) occurred:" with no additional information.

If I omit the shape.begin/endEdit() calls,  the script does not crash but the colors are not actually updated.

I have tried running this script in Flash CS5.5, CS6 and CC with the same results.

I would appreciate it if someone could point out what I am missing.

The script:

```<code>var replaceFills = {
    '#ff0000': '#33cccc',
    '#33cccc': '#ff0000',
    '#66ff00': '#00ffff',
    '#00ffff': '#66ff00',

    '#2173a6': '#ff00ff',
    '#ff00ff': '#2173a6',

    '#195480': '#ff00ff',
    '#ff00ff': '#195480',
};

function recolor(element) {
    if (element.elementType == "shape") {
        element.beginEdit();

        for (var j = 0; j < element.contours.length; j++) {
            var c = element.contours[j];
            if (c.interior && c.fill) {
                if (c.fill.style == 'solid') {
                    if (c.fill.color in replaceFills) {
                        c.fill.color = replaceFills[c.fill.color];
                    }
                }
            }
        }
        element.endEdit();
    }
}

function recolorFrame(frame) {
    var elements = frame.elements;
    for (var i = 0; i < elements.length; i++)
        recolor(elements[i]);
}

var curSelected = fl.getDocumentDOM().getTimeline().getSelectedFrames();

for (var i = 0; i < curSelected.length; i += 3) {
    var layerIndex = curSelected[i];
    var startIndex = curSelected[i + 1];
    var endIndex = curSelected[i + 2];

    var layer = fl.getDocumentDOM().getTimeline().layers[layerIndex];

    for (var j = startIndex; j < endIndex; j++) {
        var frame = layer.frames[j];
        if (j == frame.startFrame)
            recolorFrame(frame);
    }
}
</code>```

答:
Mike's answer did change the colour, but unfortunately if a shape is made up of multiple contours with different colours, they are all changed.

Instead, getting the fill, changing the colour, and the reassigning the fill to the contour does the trick.

```<code>var fill = contour.fill;

if(fill.color in replaceFills)
{
    fill.color = replaceFills[fill.color];
    contour.fill = fill;
}
</code>```

Title: Replacing Fill Colors in JSFL

Body:
I am attempting to write a JSFL script that will replace all contour fills that match given colors on selected frames.

My problem is that if I call shape.beginEdit() and shape.endEdit() as specified by the JSFL documentation, the script crashes whenever I access a fill attribute on a contour (even read only access). The error message is "The following JavaScript error(s) occurred:" with no additional information.

If I omit the shape.begin/endEdit() calls,  the script does not crash but the colors are not actually updated.

I have tried running this script in Flash CS5.5, CS6 and CC with the same results.

I would appreciate it if someone could point out what I am missing.

The script:

var replaceFills = {
    '#ff0000': '#33cccc',
    '#33cccc': '#ff0000',
    '#66ff00': '#00ffff',
    '#00ffff': '#66ff00',

    '#2173a6': '#ff00ff',
    '#ff00ff': '#2173a6',

    '#195480': '#ff00ff',
    '#ff00ff': '#195480',
};

function recolor(element) {
    if (element.elementType == "shape") {
        element.beginEdit();

        for (var j = 0; j < element.contours.length; j++) {
            var c = element.contours[j];
            if (c.interior && c.fill) {
                if (c.fill.style == 'solid') {
                    if (c.fill.color in replaceFills) {
                        c.fill.color = replaceFills[c.fill.color];
                    }
                }
            }
        }
        element.endEdit();
    }
}

function recolorFrame(frame) {
    var elements = frame.elements;
    for (var i = 0; i < elements.length; i++)
        recolor(elements[i]);
}

var curSelected = fl.getDocumentDOM().getTimeline().getSelectedFrames();

for (var i = 0; i < curSelected.length; i += 3) {
    var layerIndex = curSelected[i];
    var startIndex = curSelected[i + 1];
    var endIndex = curSelected[i + 2];

    var layer = fl.getDocumentDOM().getTimeline().layers[layerIndex];

    for (var j = startIndex; j < endIndex; j++) {
        var frame = layer.frames[j];
        if (j == frame.startFrame)
            recolorFrame(frame);
    }
}

Body:
Mike's answer did change the colour, but unfortunately if a shape is made up of multiple contours with different colours, they are all changed.

Instead, getting the fill, changing the colour, and the reassigning the fill to the contour does the trick.

var fill = contour.fill;

if(fill.color in replaceFills)
{
    fill.color = replaceFills[fill.color];
    contour.fill = fill;
}


问:
pandas dataframe - does filtering / selecting cols by String preserve order?
I have a use case where I have say 10 cols out of which 5 start with the string 'Region'. I need to get a resulting dataframe which only contains those cols (starting with string 'Region'). Not only that, I need to make sure the order is preserved (e.g. if in original df, the col order is 'Region 1', 'Region 2', 'Region 3' -- this should be preserved and not result in 'Region 3', 'Region 2', 'Region 1' instead).

Would following the 'accepted answer' for this question preserve the order or is there some other method to achieve that?

stackoverflow - find-column-whose-name-contains-a-specific-string

答:
if your data frame is similar to :

```<code>print(df)

   Region 3  Region 2  Region 1  Custom  UnwantedCol
0         0         0         0       0            0
</code>```

we can use the sorted method to sort your columns by the number:

```<code>nat_cols_sort = dict(sorted(
    {col: int(col.split(" ")[1]) for col in df.filter(regex='^Region').columns}.items(),
    key=lambda x: x[1],
))

print(df[nat_sort.keys()])

   Region 1  Region 2  Region 3
0         0         0         0
</code>```

Body:
if your data frame is similar to :

print(df)

   Region 3  Region 2  Region 1  Custom  UnwantedCol
0         0         0         0       0            0

we can use the sorted method to sort your columns by the number:

nat_cols_sort = dict(sorted(
    {col: int(col.split(" ")[1]) for col in df.filter(regex='^Region').columns}.items(),
    key=lambda x: x[1],
))

print(df[nat_sort.keys()])

   Region 1  Region 2  Region 3
0         0         0         0


问:
Extract sub-page links by Looping Python
Extract sub-page links by Looping Python

I need to extract only the links of the "Next" at the bottom of all the "A category " stock pages using a dynamic loop.
I am new to python, but coded the same in VB and running in fine.
This code must be infested with syntax errors , do bear with it.

Help will be appreciated.

```<code>import requests
from bs4 import BeautifulSoup
import pandas as pd
import re

sub_link=[]
sub_link_edit=[]

 def convert(url):
  if not url.startswith('http://'):
    return 'http:' + url
  return url

next_link = 'https://money.rediff.com/companies/groups/A'

while next_link:
 page = requests.get(next_link)
 soup = BeautifulSoup(page.content,,'html.parser')

 pagination_container_company = soup.find_all("table", class_="pagination-
 container-company")[0].text

sub_link = re.search('href="(.*)">Next',
pagination_container_company).group(1)
sub_link_edit.append(convert(sub_link))
next_link=convert(sub_link)

data_df = pd.DataFrame()
df = pd.DataFrame(
 {
    'Link': sub_link_edit
 })
data_df = pd.concat([data_df, df], sort=False)

print(df.shape)
tot_sub=len(sub_link_edit)
print(tot_sub)

data_df.to_csv('results_1.csv')
</code>```

答:
@MaxSmith060 is probably right, but I wanted to offer a technical explanation as well.

I didn't take too much of a detailed look at your code but at a high level, this is what I would do if I wanted to iteratively get the link for the "next" page.

First, inspect element on the link:

Then, filter down to something unique - class=pagination-container-company looks promising.

next_link = "..."

while next_link:
    soup = BeautifulSoup(next_link)

    pagination_container_company = soup.find_all("table", class_="pagination-container-company")[0].text

    next_link = re.search('href="(.*)">Next', pagination_container_company).group(1)
```

Some simple grouping regex to extract the link and then you have the link.

Body:
Extract sub-page links by Looping Python

I need to extract only the links of the "Next" at the bottom of all the "A category " stock pages using a dynamic loop.
I am new to python, but coded the same in VB and running in fine.
This code must be infested with syntax errors , do bear with it.

Help will be appreciated.

import requests
from bs4 import BeautifulSoup
import pandas as pd
import re

sub_link=[]
sub_link_edit=[]

 def convert(url):
  if not url.startswith('http://'):
    return 'http:' + url
  return url

next_link = 'https://money.rediff.com/companies/groups/A'

while next_link:
 page = requests.get(next_link)
 soup = BeautifulSoup(page.content,,'html.parser')

 pagination_container_company = soup.find_all("table", class_="pagination-
 container-company")[0].text

sub_link = re.search('href="(.*)">Next',
pagination_container_company).group(1)
sub_link_edit.append(convert(sub_link))
next_link=convert(sub_link)

data_df = pd.DataFrame()
df = pd.DataFrame(
 {
    'Link': sub_link_edit
 })
data_df = pd.concat([data_df, df], sort=False)

print(df.shape)
tot_sub=len(sub_link_edit)
print(tot_sub)

data_df.to_csv('results_1.csv')

Body:
@MaxSmith060 is probably right, but I wanted to offer a technical explanation as well.

I didn't take too much of a detailed look at your code but at a high level, this is what I would do if I wanted to iteratively get the link for the "next" page.

First, inspect element on the link:

Then, filter down to something unique - class=pagination-container-company looks promising.

next_link = "..."

while next_link:
    soup = BeautifulSoup(next_link)

    pagination_container_company = soup.find_all("table", class_="pagination-container-company")[0].text

    next_link = re.search('href="(.*)">Next', pagination_container_company).group(1)

Some simple grouping regex to extract the link and then you have the link.


问:
convert unix executable file to plain text
I see many people convert text to unix file. Is there some way to do the opposite, unix executable file to .txt?

(I need to read unix executable files under mac os. If it is impossible to convert the file, is there a way I can read them? UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 468: invalid start byte
)

答: I have noticed that nano seems to convert to text automatically, although I want to be able to view it in a text editor, so I am still trying to find another way.


问:
is there a way to hide mobile menu links when drawer is closed?
I have a mobile menu that I hide using clip-path when isOpen = false. But when tabbing through site on desktop it tabs through the links in navigation header and after the last one, it starts going through hidden links from mobile menu. Is there a way to hide links or unmount component completely?

```<code>import styled, { css } from 'styled-components'

import ActiveLink from '../utils/active-link'

const Menu = ({ isOpen, setOpen }) => {
  return (
    <MobileMenu open={isOpen}>
      <MobileMenuList>
        <li>
          <ActiveLink href='/' activeClassName='active' passHref>
            <MobileMenuLink onClick={() => setOpen(!isOpen)}>
              Home
            </MobileMenuLink>
          </ActiveLink>
        </li>
        <li>
          <ActiveLink href='/about' activeClassName='active' passHref>
            <MobileMenuLink onClick={() => setOpen(!isOpen)}>
              About
            </MobileMenuLink>
          </ActiveLink>
        </li>
        <li>
          <ActiveLink href='/blog' activeClassName='active' passHref>
            <MobileMenuLink onClick={() => setOpen(!isOpen)}>
              Blog
            </MobileMenuLink>
          </ActiveLink>
        </li>
        <li>
          <ActiveLink href='/podcast' activeClassName='active' passHref>
            <MobileMenuLink onClick={() => setOpen(!isOpen)}>
              Podcast
            </MobileMenuLink>
          </ActiveLink>
        </li>
      </MobileMenuList>
    </MobileMenu>
  )
}

export default Menu

const MobileMenu = styled.nav`
  ${({ theme, open }) => css`
    background: ${theme.colors.secondary};
    clip-path: ${open ? 'circle(100% at center)' : 'circle(0% at top right)'};
    position: fixed;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100vh;
    top: 0px;
    left: 0px;
    transition: clip-path 300ms linear;
  `}
`
</code>```

答:
```<code><li>
  <ActiveLink href='/' activeClassName='active' passHref>
    <MobileMenuLink
      onClick={() => setOpen(!isOpen)}
      tabIndex={`${!isOpen ? '-1' : '1'}`}
     >
      Home
    </MobileMenuLink>
   </ActiveLink>
 </li>

</code>```

Body:
I have a mobile menu that I hide using clip-path when isOpen = false. But when tabbing through site on desktop it tabs through the links in navigation header and after the last one, it starts going through hidden links from mobile menu. Is there a way to hide links or unmount component completely?

import styled, { css } from 'styled-components'

import ActiveLink from '../utils/active-link'

const Menu = ({ isOpen, setOpen }) => {
  return (
    <MobileMenu open={isOpen}>
      <MobileMenuList>

          <ActiveLink href='/' activeClassName='active' passHref>
            <MobileMenuLink onClick={() => setOpen(!isOpen)}>
              Home
            </MobileMenuLink>
          </ActiveLink>

          <ActiveLink href='/about' activeClassName='active' passHref>
            <MobileMenuLink onClick={() => setOpen(!isOpen)}>
              About
            </MobileMenuLink>
          </ActiveLink>

          <ActiveLink href='/blog' activeClassName='active' passHref>
            <MobileMenuLink onClick={() => setOpen(!isOpen)}>
              Blog
            </MobileMenuLink>
          </ActiveLink>

          <ActiveLink href='/podcast' activeClassName='active' passHref>
            <MobileMenuLink onClick={() => setOpen(!isOpen)}>
              Podcast
            </MobileMenuLink>
          </ActiveLink>

      </MobileMenuList>
    </MobileMenu>
  )
}

export default Menu

const MobileMenu = styled.nav`
  ${({ theme, open }) => css`
    background: ${theme.colors.secondary};
    clip-path: ${open ? 'circle(100% at center)' : 'circle(0% at top right)'};
    position: fixed;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100vh;
    top: 0px;
    left: 0px;
    transition: clip-path 300ms linear;
  `}
`


问:
Is there a better way to write an AVG() SQL query for multiple tables (or a way to improve my code)
im kinda new to managing sql queries with big amounts of data and im trying to improve performance on a request.
My issue is that i have multiple tables with different names that have the same structure, so i have to avg the value of temp from all of them to use later.

I'm currently working with this code :

```<code>//these value changes on each request, here are some examples to help you understand
$ano_desde = 2019;
$mes_hasta = 4;
$id_t = 1123;
//

for ($ano = $ano_desde; $ano <= $ano_hasta; $ano++) {
            for ($mes = $mes_hasta; $mes <= 12; $mes++) {

                $sql = " SELECT AVG(temp) FROM table".$ano."_".$mes." WHERE id_t = $id_t";

                //HERE I WOULD SEND THE QUERY TO MY DB AND SAVE RESP ON $TEMP

                $temp = queryResp;

                if ($temp != 0) {
                    $temps[] = floatval($temps);
                }
            }
        }
</code>```

This does the work but it comes with a hit on my response of 4 to 5 seconds and i would love to improve it, be it in the php code or in my query.

Thanks in advance, i apologize for any spelling errors as english is not my first language.

答:
I think what you are looking for is sql that look something like
It would join all the tables and then do a avg. (if you do it for each table you will get the average of an average.

```<code>SELECT AVG(temp) from (
SELECT temp FROM table.ano._mes1 WHERE id_t = id_t
UNION ALL
SELECT temp FROM table.ano._mes1 WHERE id_t = id_t
UNION ALL
SELECT temp FROM table.ano._mes1 WHERE id_t = id_t
) a
</code>```

Actually you call also do it like this for better performance

```<code>SELECT s/c avgtmp from (
SELECT sum(temp) s, count(temp) c FROM table.ano._mes1 WHERE id_t = id_t
UNION ALL
SELECT sum(temp) s, count(temp) c FROM table.ano._mes1 WHERE id_t = id_t
UNION ALL
SELECT sum(temp) s, count(temp) c FROM table.ano._mes1 WHERE id_t = id_t
) a
</code>```

Body:
im kinda new to managing sql queries with big amounts of data and im trying to improve performance on a request.
My issue is that i have multiple tables with different names that have the same structure, so i have to avg the value of temp from all of them to use later.

I'm currently working with this code :

//these value changes on each request, here are some examples to help you understand
$ano_desde = 2019;
$mes_hasta = 4;
$id_t = 1123;
//

for ($ano = $ano_desde; $ano <= $ano_hasta; $ano++) {
            for ($mes = $mes_hasta; $mes <= 12; $mes++) {

                $sql = " SELECT AVG(temp) FROM table".$ano."_".$mes." WHERE id_t = $id_t";

                //HERE I WOULD SEND THE QUERY TO MY DB AND SAVE RESP ON $TEMP

                $temp = queryResp;

                if ($temp != 0) {
                    $temps[] = floatval($temps);
                }
            }
        }

This does the work but it comes with a hit on my response of 4 to 5 seconds and i would love to improve it, be it in the php code or in my query.

Thanks in advance, i apologize for any spelling errors as english is not my first language.

Body:
I think what you are looking for is sql that look something like
It would join all the tables and then do a avg. (if you do it for each table you will get the average of an average.

SELECT AVG(temp) from (
SELECT temp FROM table.ano._mes1 WHERE id_t = id_t
UNION ALL
SELECT temp FROM table.ano._mes1 WHERE id_t = id_t
UNION ALL
SELECT temp FROM table.ano._mes1 WHERE id_t = id_t
) a

Actually you call also do it like this for better performance

SELECT s/c avgtmp from (
SELECT sum(temp) s, count(temp) c FROM table.ano._mes1 WHERE id_t = id_t
UNION ALL
SELECT sum(temp) s, count(temp) c FROM table.ano._mes1 WHERE id_t = id_t
UNION ALL
SELECT sum(temp) s, count(temp) c FROM table.ano._mes1 WHERE id_t = id_t
) a


问:
What are the performance considerations of storing files in MySQL?
I'm creating an application where users can edit their "files" for various purposes. Each user will have his / her own sandbox of files. The question is whether these files should actually exist on a drive or as long pieces of text in a MySQL DB?</p>

答: Everytime I face this problem it turns for me that storing files in filesystem ( or S3 ) is better solution. But for example Sharepoint stores all files in DB, so it depends on your project. You could also take a look at MongoDB, but I haven't tried it yet.  </p>


问:
Opengl:Unexpected behaviour when trying to draw a line to visualise mouse Ray cast
I was experimenting with ray casting to implement mouse picking in my application.

What I wanted to do is literally draw the line that is cast from the screen when the mouse is clicked.

Here's what i did:

```<code>float xNormalised = ((float)200/w.getWidth())*2 -1;
float yNormalised = -(((float)300/w.getHeight())*2 -1);

glm::vec4 nearP = glm::vec4(xNormalised,yNormalised,-1,1);
glm::vec4 farP = glm::vec4(xNormalised,yNormalised,1,1);
</code>```

For simplicity i hard code the mouse coordinates from which the ray should be cast,
i map them to normalised device coordinates and then i create two vectors representing two points: the point nearest to the screen and the point farthest on the same line.

```<code>worldNear = getInversedPoint(nearP,proj, view);
worldFar = getInversedPoint(farP,proj, view);
</code>```

Then i call this function on each of the points to basically reverse the pipeline to get their version in the world space, here's what it does:

```<code>glm::vec4 getInversedPoint(glm::vec4 point,glm::mat4& proj,glm::mat4 view){

    //glm::mat4 inv = inverse(proj*view);
    point = point*inverse(proj);
    auto v = vec4(point.x/point.w,point.y/point.w,point.z/point.w,1);
    //auto v = vec4(point.x/point.w,point.y/point.w,point.z/point.w,point.w);
    point = v*inverse(view);
    return vec4(point.x,point.y,point.z,point.w);
}
</code>```

After this i should have gotten the two points in world space if i didn't get it wrong,
so i put these two points in a buffer and call gldraw to draw the line that connects them.

What i'm expecting is basically a red dot in the coordinates that i put, because it is a straight line from the click point to the point in front of it... but i get a strange line more or less in the center of the screen, which is not where i specified..

What am i doing wrong?

You can see the red line in the center, near the sideways triangle.

My vertex shader:

```<code>#version 330 core

layout(location = 1)in vec4 pos;
uniform mat4 projectMat;
uniform mat4 viewMat;

void main(){
    gl_Position = pos;
}
</code>```

I also tried to multiply the points by the two matrices as well but it only slightly change.

答:
You can use glm::unproject to get you far and near points :

glm::vec3 near = glm::unProject(glm::vec3(mouseX, winSize.y - mouseY, 0), // the screen-space coordinate
                                camera.view,
                                camera.projection,
                                glm::vec4(0, 0, winSize)); // your viewport

glm::vec3 far = glm::unProject(glm::vec3(mouseX, winSize.y - mouseY, 0), // the screen-space coordinate
                               camera.view,
                               camera.projection,
                               glm::vec4(0, 0, winSize)); // your viewport

```

<p>You could also get the point under the mouse directly by reading the framebuffer :</p>

<pre class="lang-cpp prettyprint-override"><code>/** read depth buffer and get the point under the cursor **/
        float depth;
        glReadPixels(mouseX, winSize.y - mouseY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
        if (depth < 1.0)
            point_on_plane = glm::unProject(glm::vec3(mouseX, winSize.y - mouseY, depth), camera.view,
                                            camera.projection,
                                            glm::vec4(0, 0, winSize));

</code>```

Body:
I was experimenting with ray casting to implement mouse picking in my application.

What I wanted to do is literally draw the line that is cast from the screen when the mouse is clicked.

Here's what i did:

float xNormalised = ((float)200/w.getWidth())*2 -1;
float yNormalised = -(((float)300/w.getHeight())*2 -1);

glm::vec4 nearP = glm::vec4(xNormalised,yNormalised,-1,1);
glm::vec4 farP = glm::vec4(xNormalised,yNormalised,1,1);

For simplicity i hard code the mouse coordinates from which the ray should be cast,
i map them to normalised device coordinates and then i create two vectors representing two points: the point nearest to the screen and the point farthest on the same line.

worldNear = getInversedPoint(nearP,proj, view);
worldFar = getInversedPoint(farP,proj, view);

Then i call this function on each of the points to basically reverse the pipeline to get their version in the world space, here's what it does:

glm::vec4 getInversedPoint(glm::vec4 point,glm::mat4& proj,glm::mat4 view){

    //glm::mat4 inv = inverse(proj*view);
    point = point*inverse(proj);
    auto v = vec4(point.x/point.w,point.y/point.w,point.z/point.w,1);
    //auto v = vec4(point.x/point.w,point.y/point.w,point.z/point.w,point.w);
    point = v*inverse(view);
    return vec4(point.x,point.y,point.z,point.w);
}

After this i should have gotten the two points in world space if i didn't get it wrong,
so i put these two points in a buffer and call gldraw to draw the line that connects them.

What i'm expecting is basically a red dot in the coordinates that i put, because it is a straight line from the click point to the point in front of it... but i get a strange line more or less in the center of the screen, which is not where i specified..

What am i doing wrong?

You can see the red line in the center, near the sideways triangle.

My vertex shader:

#version 330 core

layout(location = 1)in vec4 pos;
uniform mat4 projectMat;
uniform mat4 viewMat;

void main(){
    gl_Position = pos;
}

I also tried to multiply the points by the two matrices as well but it only slightly change.

Body:
You can use glm::unproject to get you far and near points :

glm::vec3 near = glm::unProject(glm::vec3(mouseX, winSize.y - mouseY, 0), // the screen-space coordinate
                                camera.view,
                                camera.projection,
                                glm::vec4(0, 0, winSize)); // your viewport

glm::vec3 far = glm::unProject(glm::vec3(mouseX, winSize.y - mouseY, 0), // the screen-space coordinate
                               camera.view,
                               camera.projection,
                               glm::vec4(0, 0, winSize)); // your viewport

You could also get the point under the mouse directly by reading the framebuffer :

/** read depth buffer and get the point under the cursor **/
        float depth;
        glReadPixels(mouseX, winSize.y - mouseY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
        if (depth < 1.0)
            point_on_plane = glm::unProject(glm::vec3(mouseX, winSize.y - mouseY, depth), camera.view,
                                            camera.projection,
                                            glm::vec4(0, 0, winSize));


问:
Using apply or iterrows in python instead of groupby?
```<code>df = pd.DataFrame([[15000, 2015], [20000,2015], [25000,2015], [15000, 2016], [20000,2016], [25000,2016], [10500, 2017], [54000,2017], [34000,2017]], columns=['income', 'year'])

income          year
15000           2015
20000           2015
25000           2015
19000           2016
36000           2016
20000           2016
10500           2017
54000           2017
34000           2017
</code>```

Hello,

If I have a dataframe like the one above and I want to loop through each year in python and create a median income value for each year, how would I go about it?

Would the apply function or the groupby function be best?

I can get this to work:

```<code>df.groupby(df.year)[['income']].median()
</code>```

I was wondering whether there was an alternative such as apply or iterrows?

Many thanks.

答:
df.groupby is the best way to go when you are doing certain aggregations.

This is the right way to use it:

```<code>In [85]: df.groupby('year', as_index=False)['income'].median()
Out[85]:
   year  income
0  2015   20000
1  2016   20000
2  2017   34000
</code>```

After OP's comment:

```<code>In [239]: res = df.groupby('year', as_index=False)['income'].median()
In [259]: d = res.set_index('year').to_dict()['income']
</code>```

Then you can query the above dict d to get mean for a certain year, like this:

```<code>In [268]: d.get(2015)
Out[268]: 20000

In [269]: d.get(2016)
Out[269]: 20000

In [270]: d.get(2017)
Out[270]: 34000
</code>```

Body:
df = pd.DataFrame([[15000, 2015], [20000,2015], [25000,2015], [15000, 2016], [20000,2016], [25000,2016], [10500, 2017], [54000,2017], [34000,2017]], columns=['income', 'year'])

income          year
15000           2015
20000           2015
25000           2015
19000           2016
36000           2016
20000           2016
10500           2017
54000           2017
34000           2017

Hello,

If I have a dataframe like the one above and I want to loop through each year in python and create a median income value for each year, how would I go about it?

Would the apply function or the groupby function be best?

I can get this to work:

df.groupby(df.year)[['income']].median()

I was wondering whether there was an alternative such as apply or iterrows?

Many thanks.

Body:
df.groupby is the best way to go when you are doing certain aggregations.

This is the right way to use it:

In [85]: df.groupby('year', as_index=False)['income'].median()
Out[85]:
   year  income
0  2015   20000
1  2016   20000
2  2017   34000

After OP's comment:

In [239]: res = df.groupby('year', as_index=False)['income'].median()
In [259]: d = res.set_index('year').to_dict()['income']

Then you can query the above dict d to get mean for a certain year, like this:

In [268]: d.get(2015)
Out[268]: 20000

In [269]: d.get(2016)
Out[269]: 20000

In [270]: d.get(2017)
Out[270]: 34000


问:
Uncaught TypeError: Cannot read property 'codiCombi' of undefined
why I am getting this error:

Thats my code:

```<code>function cargaEnquesta(sessionActual, tActivitatsArray){
    //some stuff
    var objectJson = JSON.parse(JSON.stringify(tActivitatsArray[count]));
    var nameActivity= "";
    console.log("count "  + count+" " + JSON.stringify(tActivitatsArray[count]));
    if (tActivitatsArray[contador].length>1){
        fTrobarNom(tActivitatsArray[count],"",objectJson[count].codiCombi,"","activ");
    }
    else{
      //other stuff
    }
    //other stuff
}

</code>```

Thats the stringify result and the console output:

```<code>count 0 [{"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Visita Museu","codiActiv":"1","tipus":"S","nomCombi":"Visita Museu + Taller A","codiCombi":"1","idioma":"0"},{"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Taller A","codiActiv":"3","tipus":"S","nomCombi":"Visita Museu + Taller A","codiCombi":"1","idioma":"0"}]

count 1 [{"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Visita Museu","codiActiv":"1","tipus":"S","idioma":"0"}]

count 2 [{"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Visita Museu","codiActiv":"1","tipus":"S","nomCombi":"Visita Museu + Taller A","codiCombi":"1","idioma":"0"},{"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Taller A","codiActiv":"3","tipus":"S","nomCombi":"Visita Museu + Taller A","codiCombi":"1","idioma":"0"}]

mainFunctions.js?v2.3:2643 Uncaught TypeError: Cannot read property 'codiCombi' of undefined
    at Object.<anonymous> (mainFunctions.js?v2.3:2643)
    at Function.each (jquery-1.12.4.js:370)
</code>```

Why I am getting the error: Uncaught TypeError: Cannot read property 'codiCombi' of undefined
by doing objectJson[count].codiCombi

And why the error appears when the count is 2? As stringify shows, I got the property codiCombi.

Also I don't understand why the error doesnt appear when the count is 0.

答:
When count is equal to 2, this is the value of your tActivitatsArray:

[
  {"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Visita Museu","codiActiv":"1","tipus":"S","nomCombi":"Visita Museu + Taller A","codiCombi":"1","idioma":"0"},
  {"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Taller A","codiActiv":"3","tipus":"S","nomCombi":"Visita Museu + Taller A","codiCombi":"1","idioma":"0"}
]
```

<p>It only has 2 elements. When you call <code>objectJson[count]</code> when <code>count</code> is 2, it returns <code>undefined</code>, as there is no "third" element (element at index 2 is the third element).</p>

<pre class="lang-js prettyprint-override"><code>objectJson[2].codiCombi;

// is equal to

undefined.codiCombi;
</code>```

Body:
why I am getting this error:

Thats my code:

function cargaEnquesta(sessionActual, tActivitatsArray){
    //some stuff
    var objectJson = JSON.parse(JSON.stringify(tActivitatsArray[count]));
    var nameActivity= "";
    console.log("count "  + count+" " + JSON.stringify(tActivitatsArray[count]));
    if (tActivitatsArray[contador].length>1){
        fTrobarNom(tActivitatsArray[count],"",objectJson[count].codiCombi,"","activ");
    }
    else{
      //other stuff
    }
    //other stuff
}

Thats the stringify result and the console output:

count 0 [{"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Visita Museu","codiActiv":"1","tipus":"S","nomCombi":"Visita Museu + Taller A","codiCombi":"1","idioma":"0"},{"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Taller A","codiActiv":"3","tipus":"S","nomCombi":"Visita Museu + Taller A","codiCombi":"1","idioma":"0"}]

count 1 [{"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Visita Museu","codiActiv":"1","tipus":"S","idioma":"0"}]

count 2 [{"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Visita Museu","codiActiv":"1","tipus":"S","nomCombi":"Visita Museu + Taller A","codiCombi":"1","idioma":"0"},{"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Taller A","codiActiv":"3","tipus":"S","nomCombi":"Visita Museu + Taller A","codiCombi":"1","idioma":"0"}]

mainFunctions.js?v2.3:2643 Uncaught TypeError: Cannot read property 'codiCombi' of undefined
    at Object.<anonymous> (mainFunctions.js?v2.3:2643)
    at Function.each (jquery-1.12.4.js:370)

Why I am getting the error: Uncaught TypeError: Cannot read property 'codiCombi' of undefined
by doing objectJson[count].codiCombi

And why the error appears when the count is 2? As stringify shows, I got the property codiCombi.

Also I don't understand why the error doesnt appear when the count is 0.

Body:
When count is equal to 2, this is the value of your tActivitatsArray:

[
  {"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Visita Museu","codiActiv":"1","tipus":"S","nomCombi":"Visita Museu + Taller A","codiCombi":"1","idioma":"0"},
  {"mostrIdioma":"S","calendariWeb":"S","nomActiv":"Taller A","codiActiv":"3","tipus":"S","nomCombi":"Visita Museu + Taller A","codiCombi":"1","idioma":"0"}
]

It only has 2 elements. When you call objectJson[count] when count is 2, it returns undefined, as there is no "third" element (element at index 2 is the third element).

objectJson[2].codiCombi;

// is equal to

undefined.codiCombi;