问:
How to store and read millions of records in Azure with secondary indexing
I need to store tens of millions of records in Azure. I need to be able to sort them by insert date in descending order. Finally, I need to issue where clauses on 3 fields.

```<code>public class Record
{
   public DateTime CreatedOn {get; set;}
   public string Filter1 {get; set;}
   public string Filter2 {get; set;}
   public bool Filter3 {get; set;}
}
</code>```

I believe Azure Table Storage does not support secondary indexes, so I'm considering CosmosDB. The problem is each logical partition on CosmosDB is limited to 20 GBs. I can't dump everything in a single partition. I'm thinking of slicing data horizontally based on the CreatedOn field (dd/mm/yyyy without the time component). This helps me distribute the data, but I'm stuck with the query part to read it back.

The data is user generated; thus, I have no idea what values CreatedOn will be assigned. If I partition on CreatedOn, how can I make my query smart so that I know what the next partition key is when I reach the end of the previous partition?

Example:

Record1 CreatedOn => 5/28/2020

Record2 CreatedOn => 5/28/2020

Record3 CreatedOn => 5/22/2020

Record4 CreatedOn => 5/10/2020

There could be a large gap between dates, so I don't want to brute force the next partition by scanning the range day by day.

Regarding queries:

Filter1 and Filter2 can have 5 and 12 different values respectively. Filter3 is just a boolean.

A sample query would look like:

```<code>select top 20 from record
where Filter1 = 'Value1' and Filter2 = 'Value2' and Filter3 = false
order by CreatedOn desc
</code>```

Another sample query is the same as above without Filter2.

```<code>select top 20 from record
where Filter1 = 'Value1' and Filter3 = false
order by CreatedOn desc
</code>```

答:
Use Cosmos SQL API. You can use Order by to sort on query.
All the fields are indexed by default. You can customize the indexing policy to include composite indices to make queries more efficient.

Pick a PK which will have a wide range of possible values to spread the request unit (RU) consumption and data storage evenly across all logical partitions evenly. Also, you should know the PK value for majority of your queries. You will use it in you where clause so that the query engine will only query specific partitions where the data of interest resides.

In you case what should be that PK property?

```<code>public class Record
{
   public DateTime CreatedOn {get; set;}
   public string Filter1 {get; set;}
   public string Filter2 {get; set;}
   public bool Filter3 {get; set;}
}
</code>```

"CreatedOn" is not a good choice. First, like you mentioned you would not know its value during query time, so you will have to always do cross partition queries. Second, all your writes for a given day will go to the same partition resulting in hot partition which will result in rate-limiting and in inefficient use of the provisioned throughput, and higher costs.

Do any of the other properties help the cause? Maybe you must create another property. Maybe UserID , I am just making one up since I don’t know the context.

```<code>public class Record
{
   public DateTime CreatedOn {get; set;}
   public string Filter1 {get; set;}
   public string Filter2 {get; set;}
   public bool Filter3 {get; set;}
   public string UserID {get; set;}
}
</code>```

Now you can query

```<code>select top 20 from record
where Filter1 = 'Value1' and Filter2 = 'Value2' and Filter3 = false  and UserID = 'somevalue' order by CreatedOn desc
</code>```

Body:
I need to store tens of millions of records in Azure. I need to be able to sort them by insert date in descending order. Finally, I need to issue where clauses on 3 fields.

public class Record
{
   public DateTime CreatedOn {get; set;}
   public string Filter1 {get; set;}
   public string Filter2 {get; set;}
   public bool Filter3 {get; set;}
}

I believe Azure Table Storage does not support secondary indexes, so I'm considering CosmosDB. The problem is each logical partition on CosmosDB is limited to 20 GBs. I can't dump everything in a single partition. I'm thinking of slicing data horizontally based on the CreatedOn field (dd/mm/yyyy without the time component). This helps me distribute the data, but I'm stuck with the query part to read it back.

The data is user generated; thus, I have no idea what values CreatedOn will be assigned. If I partition on CreatedOn, how can I make my query smart so that I know what the next partition key is when I reach the end of the previous partition?

Example:

Record1 CreatedOn => 5/28/2020

Record2 CreatedOn => 5/28/2020

Record3 CreatedOn => 5/22/2020

Record4 CreatedOn => 5/10/2020

There could be a large gap between dates, so I don't want to brute force the next partition by scanning the range day by day.

Regarding queries:

Filter1 and Filter2 can have 5 and 12 different values respectively. Filter3 is just a boolean.

A sample query would look like:

select top 20 from record
where Filter1 = 'Value1' and Filter2 = 'Value2' and Filter3 = false
order by CreatedOn desc

Another sample query is the same as above without Filter2.

select top 20 from record
where Filter1 = 'Value1' and Filter3 = false
order by CreatedOn desc

Body:
Use Cosmos SQL API. You can use Order by to sort on query.
All the fields are indexed by default. You can customize the indexing policy to include composite indices to make queries more efficient.

Pick a PK which will have a wide range of possible values to spread the request unit (RU) consumption and data storage evenly across all logical partitions evenly. Also, you should know the PK value for majority of your queries. You will use it in you where clause so that the query engine will only query specific partitions where the data of interest resides.

In you case what should be that PK property?

public class Record
{
   public DateTime CreatedOn {get; set;}
   public string Filter1 {get; set;}
   public string Filter2 {get; set;}
   public bool Filter3 {get; set;}
}

"CreatedOn" is not a good choice. First, like you mentioned you would not know its value during query time, so you will have to always do cross partition queries. Second, all your writes for a given day will go to the same partition resulting in hot partition which will result in rate-limiting and in inefficient use of the provisioned throughput, and higher costs.

Do any of the other properties help the cause? Maybe you must create another property. Maybe UserID , I am just making one up since I don’t know the context.

public class Record
{
   public DateTime CreatedOn {get; set;}
   public string Filter1 {get; set;}
   public string Filter2 {get; set;}
   public bool Filter3 {get; set;}
   public string UserID {get; set;}
}

Now you can query

select top 20 from record
where Filter1 = 'Value1' and Filter2 = 'Value2' and Filter3 = false  and UserID = 'somevalue' order by CreatedOn desc


来源: stackexchange

问:
Using Loop to Export Figures in R
I used R to create 3 individual scatter plots with same X variable, but different outcomes (Y1, Y2, Y3) using:

```<code>png(filename="Y1.png")
plot(X,Y1)
dev.off()

png(filename="Y2.png")
plot(X,Y2)
dev.off()

png(filename="Y3.png")
plot(X,Y3)
dev.off()
</code>```

Now that my coauthor wants 15 more plots (Y1, Y2, Y3, Y4, Y5, ... Y18), I was wondering if there was a loop I could use to do the task I am trying to do.

答:
If all "y" variables are strictly upper-case "Y" and numbers, then

yvars <- ls(pattern="^Y\\d+")
for (yv in yvars) {
  png(filename = paste0(yv, ".png"))
  plot(X, get(yv))
  dev.off()
}
```

<p>If they are variables in a frame, then</p>

<pre class="lang-r prettyprint-override"><code>yvars <- grep("^Y\\d+", colnames(dat), value = TRUE)
for (yv in yvars) {
  png(filename = paste0(yv, ".png")
  plot(X, dat[[yv]])
  dev.off()
}
</code>```

You can define the yvars variable any way you want, manually (i.e., c(...)) or programmatically (as above), where the balance between the two is based on your risk (what if you "find" too many Y variables, some of which are not needed or correct).

ContentLicense: CC BY-SA 4.0

Title: Using Loop to Export Figures in R

Body:
I used R to create 3 individual scatter plots with same X variable, but different outcomes (Y1, Y2, Y3) using:

png(filename="Y1.png")
plot(X,Y1)
dev.off()

png(filename="Y2.png")
plot(X,Y2)
dev.off()

png(filename="Y3.png")
plot(X,Y3)
dev.off()

Now that my coauthor wants 15 more plots (Y1, Y2, Y3, Y4, Y5, ... Y18), I was wondering if there was a loop I could use to do the task I am trying to do.

Body:
If all "y" variables are strictly upper-case "Y" and numbers, then

yvars <- ls(pattern="^Y\\d+")
for (yv in yvars) {
  png(filename = paste0(yv, ".png"))
  plot(X, get(yv))
  dev.off()
}

If they are variables in a frame, then

yvars <- grep("^Y\\d+", colnames(dat), value = TRUE)
for (yv in yvars) {
  png(filename = paste0(yv, ".png")
  plot(X, dat[[yv]])
  dev.off()
}

You can define the yvars variable any way you want, manually (i.e., c(...)) or programmatically (as above), where the balance between the two is based on your risk (what if you "find" too many Y variables, some of which are not needed or correct).


问:
How to retrieve well formatted JSON from AWS Lambda using Python
I have a function in AWS Lambda that connects to the Twitter API and returns the tweets which match a specific search query I provided via the event. A simplified version of the function is below. There's a few helper functions I use like get_secret to manage API keys and process_tweet which limits what data gets sent back and does things like convert the created at date to a string. The net result is that I should get back a list of dictionaries.

```<code>def lambda_handler(event, context):
    twitter_secret = get_secret("twitter")

    auth = tweepy.OAuthHandler(twitter_secret['api-key'],
                               twitter_secret['api-secret'])
    auth.set_access_token(twitter_secret['access-key'],
                          twitter_secret['access-secret'])
    api = tweepy.API(auth)

    cursor = tweepy.Cursor(api.search,
                           q=event['search'],
                           include_entities=True,
                           tweet_mode='extended',
                           lang='en')

    tweets = list(cursor.items())
    tweets = [process_tweet(t) for t in tweets if not t.retweeted]

    return json.dumps({"tweets": tweets})
</code>```

From my desktop then, I have code which invokes the lambda function.

```<code>aws_lambda = boto3.client('lambda', region_name="us-east-1")
payload = {"search": "paint%20protection%20film filter:safe"}
lambda_response = aws_lambda.invoke(FunctionName="twitter-searcher",
                                    InvocationType="RequestResponse",
                                    Payload=json.dumps(payload))
results = lambda_response['Payload'].read()
tweets = results.decode('utf-8')
</code>```

The problem is that somewhere between json.dumpsing the output in lambda and reading the payload in Python, the data has gotten screwy.  For example, a line break which should be \n becomes \\\\n, all of the double quotes are stored as \\" and Unicode characters are all prefixed by \\. So, everything that was escaped, when it was received by Python on my desktop with the escaping character being escaped. Consider this element of the list that was returned (with manual formatting).

```<code>'{\\"userid\\": 190764134,
  \\"username\\": \\"CapitalGMC\\",
  \\"created\\": \\"2018-09-02 15:00:00\\",
  \\"tweetid\\": 1036267504673337344,
  \\"text\\": \\"Protect your vehicle\'s paint! Find out how on this week\'s blog.
              \\\\ud83d\\\\udc47\\\\n\\\\nhttps://url/XYMxPhVhdH https://url/mFL2Zv8nWW\\"}'
</code>```

I can use regex to fix some problems (\\" and \\\\n) but the Unicode is tricky because even if I match it, how do I replace it with a properly escaped character?  When I do this in R, using the aws.lambda package, everything is fine, no weird escaped escapes.

What am I doing wrong on my desktop with the response from AWS Lambda that's garbling the data?

Update

The process tweet function is below. It literally just pulls out the bits I care to keep, formats the datetime object to be a string and returns a dictionary.

```<code>def process_tweet(tweet):
    bundle = {
        "userid": tweet.user.id,
        "username": tweet.user.screen_name,
        "created": str(tweet.created_at),
        "tweetid": tweet.id,
        "text": tweet.full_text
    }
    return bundle
</code>```

Just for reference, in R the code looks like this.

```<code>payload = list(search="paint%20protection%20film filter:safe")
results = aws.lambda::invoke_function("twitter-searcher"
                                      ,payload = jsonlite::toJSON(payload
                                                              ,auto_unbox=TRUE)
                                      ,type = "RequestResponse"
                                      ,key = creds$key
                                      ,secret = creds$secret
                                      ,session_token = creds$session_token
                                      ,region = creds$region)
tweets = jsonlite::fromJSON(results)
str(tweets)

#> 'data.frame':    133 obs. of  5 variables:
#>  $ userid  : num  2231994854 407106716 33553091 7778772 782310 ...
#>  $ username: chr  "adaniel_080213" "Prestige_AdamL" "exclusivedetail" "tedhu" ...
#>  $ created : chr  "2018-09-12 14:07:09" "2018-09-12 11:31:56" "2018-09-12 10:46:55" "2018-09-12 07:27:49" ...
#>  $ tweetid : num  1039878080968323072 1039839019989983232 1039827690151444480 1039777586975526912 1039699310382931968 ...
#>  $ text    : chr  "I liked a @YouTube video https://url/97sRShN4pM Tesla Model 3 - Front End Package - Suntek Ultra Paint Protection Film" "Another #Corvette #ZO6 full body clearbra wrap completed using @xpeltech ultimate plus PPF ... Paint protection"| __truncated__ "We recently protected this Tesla Model 3 with Paint Protection Film and Ceramic Coating.#teslamodel3 #charlotte"| __truncated__ "Tesla Model 3 - Front End Package - Suntek Ultra Paint Protection Film https://url/AD1cl5dNX3" ...

tweets[131,]
#>        userid   username             created             tweetid
#> 131 190764134 CapitalGMC 2018-09-02 15:00:00 1036267504673337344
#>          text
#> 131 Protect your vehicle's paint! Find out how on this week's blog.\n\nhttps://url/XYMxPhVhdH https://url/mFL2Zv8nWW
</code>```

答:
Don't use json.dumps()

I had a similar issue, and when I just returned "body": content instead of "body": json.dumps(content) I could easily access and manipulate my data. Before that, I got that weird form that looks like JSON, but it's not.

Body:
I have a function in AWS Lambda that connects to the Twitter API and returns the tweets which match a specific search query I provided via the event. A simplified version of the function is below. There's a few helper functions I use like get_secret to manage API keys and process_tweet which limits what data gets sent back and does things like convert the created at date to a string. The net result is that I should get back a list of dictionaries.

def lambda_handler(event, context):
    twitter_secret = get_secret("twitter")

    auth = tweepy.OAuthHandler(twitter_secret['api-key'],
                               twitter_secret['api-secret'])
    auth.set_access_token(twitter_secret['access-key'],
                          twitter_secret['access-secret'])
    api = tweepy.API(auth)

    cursor = tweepy.Cursor(api.search,
                           q=event['search'],
                           include_entities=True,
                           tweet_mode='extended',
                           lang='en')

    tweets = list(cursor.items())
    tweets = [process_tweet(t) for t in tweets if not t.retweeted]

    return json.dumps({"tweets": tweets})

From my desktop then, I have code which invokes the lambda function.

aws_lambda = boto3.client('lambda', region_name="us-east-1")
payload = {"search": "paint%20protection%20film filter:safe"}
lambda_response = aws_lambda.invoke(FunctionName="twitter-searcher",
                                    InvocationType="RequestResponse",
                                    Payload=json.dumps(payload))
results = lambda_response['Payload'].read()
tweets = results.decode('utf-8')

The problem is that somewhere between json.dumpsing the output in lambda and reading the payload in Python, the data has gotten screwy.  For example, a line break which should be \n becomes \\\\n, all of the double quotes are stored as \\" and Unicode characters are all prefixed by \\. So, everything that was escaped, when it was received by Python on my desktop with the escaping character being escaped. Consider this element of the list that was returned (with manual formatting).

'{\\"userid\\": 190764134,
  \\"username\\": \\"CapitalGMC\\",
  \\"created\\": \\"2018-09-02 15:00:00\\",
  \\"tweetid\\": 1036267504673337344,
  \\"text\\": \\"Protect your vehicle\'s paint! Find out how on this week\'s blog.
              \\\\ud83d\\\\udc47\\\\n\\\\nhttps://url/XYMxPhVhdH https://url/mFL2Zv8nWW\\"}'

I can use regex to fix some problems (\\" and \\\\n) but the Unicode is tricky because even if I match it, how do I replace it with a properly escaped character?  When I do this in R, using the aws.lambda package, everything is fine, no weird escaped escapes.

What am I doing wrong on my desktop with the response from AWS Lambda that's garbling the data?

Update

The process tweet function is below. It literally just pulls out the bits I care to keep, formats the datetime object to be a string and returns a dictionary.

def process_tweet(tweet):
    bundle = {
        "userid": tweet.user.id,
        "username": tweet.user.screen_name,
        "created": str(tweet.created_at),
        "tweetid": tweet.id,
        "text": tweet.full_text
    }
    return bundle

Just for reference, in R the code looks like this.

payload = list(search="paint%20protection%20film filter:safe")
results = aws.lambda::invoke_function("twitter-searcher"
                                      ,payload = jsonlite::toJSON(payload
                                                              ,auto_unbox=TRUE)
                                      ,type = "RequestResponse"
                                      ,key = creds$key
                                      ,secret = creds$secret
                                      ,session_token = creds$session_token
                                      ,region = creds$region)
tweets = jsonlite::fromJSON(results)
str(tweets)

#> 'data.frame':    133 obs. of  5 variables:
#>  $ userid  : num  2231994854 407106716 33553091 7778772 782310 ...
#>  $ username: chr  "adaniel_080213" "Prestige_AdamL" "exclusivedetail" "tedhu" ...
#>  $ created : chr  "2018-09-12 14:07:09" "2018-09-12 11:31:56" "2018-09-12 10:46:55" "2018-09-12 07:27:49" ...
#>  $ tweetid : num  1039878080968323072 1039839019989983232 1039827690151444480 1039777586975526912 1039699310382931968 ...
#>  $ text    : chr  "I liked a @YouTube video https://url/97sRShN4pM Tesla Model 3 - Front End Package - Suntek Ultra Paint Protection Film" "Another #Corvette #ZO6 full body clearbra wrap completed using @xpeltech ultimate plus PPF ... Paint protection"| __truncated__ "We recently protected this Tesla Model 3 with Paint Protection Film and Ceramic Coating.#teslamodel3 #charlotte"| __truncated__ "Tesla Model 3 - Front End Package - Suntek Ultra Paint Protection Film https://url/AD1cl5dNX3" ...

tweets[131,]
#>        userid   username             created             tweetid
#> 131 190764134 CapitalGMC 2018-09-02 15:00:00 1036267504673337344
#>          text
#> 131 Protect your vehicle's paint! Find out how on this week's blog.\n\nhttps://url/XYMxPhVhdH https://url/mFL2Zv8nWW


问:
UITableViewCell animate resize when user touchdown or tap Cell
I want to animate a UITableViewCell like in the following image (The cell gets resized smaller) before detailedViewController is show. I have tried it from last two days and tried other all the answers I found for similar questions.

I want the cell to animate/resize as soon as user put his finger on the UITableViewCell.

The UITableViewCell is a custom cell. I have tried setting a new frame to cell.contentView in UITableViewCell subclass's setSelected:(BOOL)selected animated:(BOOL)animated method.  The UITableViewCell didn't resize as I expected.

What I have also observed is this method is called when user lifts his finger up from the UITableViewCell. Actually I want it to happen as soon as user taps the cell. And if the cell is not selected and user moves to some other cell, cell should get resized back to normal size.

Then I did some searching on receiving touch events from UITableViewCell. And I tried to add a UILongPressGestureRecognizer and UITapGestureRecognizer and in the selector I tried to change cell.contentView frame like this

```<code>cell.contentView.frame = CGRectMake(cell.frame.origin.x +20, cell.frame.origin.y, cell.frame.size.width-20, cell.frame.size.height);
[cell setNeedsDisplay];
</code>```

But the selected  cell is only flashing(cell background) it is not resizing as I expected.

Any suggestions would be really helpful. Thanks in advance

答:
Here is an simpler way to da cell animation on tap in Swift 5.0:</p>

```<code>func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath)
{
    let cell = tableView.cellForRow(at: indexPath)

    UIView.animate(withDuration: 0.3) {

        cell!.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
    }
}

func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath)
{
    let cell = tableView.cellForRow(at: indexPath)

    UIView.animate(withDuration: 0.3) {

        cell!.transform = .identity
    }
}
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I want to animate a UITableViewCell like in the following image (The cell gets resized smaller) before detailedViewController is show. I have tried it from last two days and tried other all the answers I found for similar questions.

I want the cell to animate/resize as soon as user put his finger on the UITableViewCell.

The UITableViewCell is a custom cell. I have tried setting a new frame to cell.contentView in UITableViewCell subclass's setSelected:(BOOL)selected animated:(BOOL)animated method.  The UITableViewCell didn't resize as I expected.

What I have also observed is this method is called when user lifts his finger up from the UITableViewCell. Actually I want it to happen as soon as user taps the cell. And if the cell is not selected and user moves to some other cell, cell should get resized back to normal size.

Then I did some searching on receiving touch events from UITableViewCell. And I tried to add a UILongPressGestureRecognizer and UITapGestureRecognizer and in the selector I tried to change cell.contentView frame like this

cell.contentView.frame = CGRectMake(cell.frame.origin.x +20, cell.frame.origin.y, cell.frame.size.width-20, cell.frame.size.height);
[cell setNeedsDisplay];

But the selected  cell is only flashing(cell background) it is not resizing as I expected.

Any suggestions would be really helpful. Thanks in advance

Body:
Here is an simpler way to da cell animation on tap in Swift 5.0:

func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath)
{
    let cell = tableView.cellForRow(at: indexPath)

    UIView.animate(withDuration: 0.3) {

        cell!.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
    }
}

func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath)
{
    let cell = tableView.cellForRow(at: indexPath)

    UIView.animate(withDuration: 0.3) {

        cell!.transform = .identity
    }
}


问:
Convert h.264 stream obtained from rtp session to video file using gstreamer
I need to play video from *.rtpdump or *.pcap file which contain rtp stream. I correctly parse rtp packages and get payload from that. Can I using gstreamer or something else tool convert it to correct h.264 format?</p>

答:
```<code>void H264parser::parse(const rtp_info &rtpInfo, const unsigned char *data, size_t size) {
    const int type_fu_a = 28;
    const int type_stap_a = 24;
    nalu_header nalu;
    nalu.f = static_cast<uint8_t>((data[0] >> 7) & 0x1);
    nalu.nri = static_cast<uint8_t>((data[0] >> 5) & 0x3);
    nalu.type = static_cast<uint8_t>(data[0] & 0x1f);

    unsigned char identifier[4] = {0, 0, 0, 1};  // H.264 identifier.
    if (nalu.type == type_fu_a) {                  // fu_a
        fu_a_header fu_a;
        fu_a.s = static_cast<uint8_t>((data[1] >> 7) & 0x1);
        fu_a.e = static_cast<uint8_t>((data[1] >> 6) & 0x1);
        fu_a.r = static_cast<uint8_t>((data[1] >> 5) & 0x1);
        fu_a.type = static_cast<uint8_t>((data[1]) & 0x1f);
        // fprintf(stdout, "fu_a: s = %d, e = %d, type = %d\n", fu_a.s, fu_a.e, fu_a.type);
        if (fu_a.s == 1) {
            assert(cache.size() == 0);
            cache.add(identifier, 4);
            uint8_t reassembled_nalu = 0;
            reassembled_nalu |= (nalu.f << 7);
            reassembled_nalu |= (nalu.nri << 5);
            reassembled_nalu |= (fu_a.type);
            cache.add(&reassembled_nalu, 1);
        }
        data += 2;
        size -= 2;
        cache.add(data, size);
        if (fu_a.e == 1) {
            FILE *file = fopen("test.h264", "ab");
            fwrite(cache.get(), cache.size(), 1, file);
            fclose(file);
            cache.reset();
        }
    } else if (nalu.type == type_stap_a) {
        fprintf(stdout, "not supported yet\n");
    } else {   // NALU in a single packet
        cache.add(identifier, 4);
        cache.add(data, size);
        FILE *file = fopen("test.h264", "ab");
        fwrite(cache.get(), cache.size(), 1, file);
        fclose(file);
        cache.reset();
    }
}
</code>```


问:
How to produce a sectional view of 3d-mesh by CGAL
I am running CGAL's 3d mesh examples and modifying them in my own way. But I want to have cut views of those generated meshes just like what CGAL website displays, but CGAL doesn't seem to have given any suggestion of software or code examples to do so. Any ideas to help me out?</p>

答: I'd just write the mesh out to a file and use ParaView. They have the crinkle clip feature you're looking for.


问:
List assignment/creation in Python showing unexpected behaviour
I was playing around with a block of code during which I stumbled upon some unexpected behaviour of Python.

I defined a list as follows:

```<code>a = [1, 2, 3]
</code>```

Made another list with a loop:

```<code>res = (item for item in a if a.count(item) > 0)
print(list(res)) # [1, 2, 3] -- as expected -- Consider this is line 1
</code>```

Then later I changed the initial value of first list:

```<code>a = [7, 2, 9]
</code>```

I was expecting no change in res as the changes I made to a were after res was created, but to my surprise, the value of res was changed.

```<code>print(list(res)) # prints [] -- Consider this as line 2
</code>```

What's more shocking is that the result of print depends on print statement on line 1, if I comment out print statement at line1 then line 2 prints [2]

Can someone please explain me what's going on here?

答:
res = (item for item in a if a.count(x) > 0) creates a generator that is exhausted by calling list(res).

When you call the next list(res) it returns empty list - []

consider creating a list first, for example res = [item for item in a if a.count(x) > 0].

Body:
I was playing around with a block of code during which I stumbled upon some unexpected behaviour of Python.

I defined a list as follows:

a = [1, 2, 3]

Made another list with a loop:

res = (item for item in a if a.count(item) > 0)
print(list(res)) # [1, 2, 3] -- as expected -- Consider this is line 1

Then later I changed the initial value of first list:

a = [7, 2, 9]

I was expecting no change in res as the changes I made to a were after res was created, but to my surprise, the value of res was changed.

print(list(res)) # prints [] -- Consider this as line 2

What's more shocking is that the result of print depends on print statement on line 1, if I comment out print statement at line1 then line 2 prints [2]

Can someone please explain me what's going on here?


问:
Kernel or Core - when to use them?
I have noticed that term "Core" is mainly used for CPU cores and "Kernel" for OS kernels. This question at English SE clarifies that Core and Kernel are used to denote the central part of a fruit or nut. But what about IT? When should I use "Core" and when "Kernel"? How should I call the central part of a software? In my native language we have a single word to denote CPU core and OS Kernel, so two English words confuse me.</p>

答:
Outside of domains where they're part of the established terminology (e.g. those you mentioned, CPU cores, OS kernels, etc.), it's very much up to your personal preference, as long as you're trying to express/reflect the architectural layering of a system, emphasizing markedly different "worlds" inside and outside of it.

As both strongly suggest a well-defined boundary (usually a documented interface, like an API, and/or some sort of a deployment/executive barrier etc.), you should be fine with either.

Accordingly, e.g. web frameworks come to mind that call their "inner circles" core (e.g. Drupal), or kernel (e.g. Symfony), or both at the same time, quite freely. Even though they, of course, rarely mean the exact same thing (according to the specifics of their own systems), if you'd force them to swap and use the other word, it could hardly cause any confusion (barring clashes with existing/external dependencies etc., obviously).

(E.g. I myself have been oscillating between the two in my designs for >30 years, annoying the hell out of myself with that, so I feel for anyone being frustrated about this word choice... As a rule of thumb for myself: if a system repeatedly reminds me of an OS, for whatever reason, I just give in and call it's core "the kernel", and if it's some higher-level, less generic (or less robust, or just lighter), more frameworkish thing, then I just call its kernel "the core", even if it may be counter-intuitive to a biologist. :) )

Title: Kernel or Core - when to use them?


问:
Determine if datetime is before or after midday
I'm working in small test code to determine if a generated datetime is before or after the midday like the code below.

```<code>import random
from datetime import datetime, timedelta
from pytz import timezone

current_date = datetime.now(timezone('America/Sao_Paulo'))
new_date = current_date - timedelta(days=2)
print (new_date.strftime("%Y-%m-%d %H:%M:%S"))

while new_date <= current_date:
  new_date = new_date + timedelta(minutes=10)
  print (new_date.strftime("%Y-%m-%d %H:%M:%S"))
  if new_date < datetime.time(12):
    print("test")
</code>```

The problem is, I can't verify if the new_date is under the midday. Probably I'm doing something wrong in the if condition, right?

I would like to print some results if the new_dateis after midday and another message if it's after midday to midnight.

Any suggestions how I can solve this?

答:
You need to compare just the time from new_date, with 12 converted to a datetime object.
Because of the way you did the imports, when you use datetime.time() it means you are calling datetime.datetime.time() and not datetime.time(), which is why the conversion fails.
You can solve this by

```<code>from datetime import datetime, timedelta, time
</code>```

and then use

```<code>time()
</code>```

or by

```<code>import datetime as dt
</code>```

then using in the appropriate places

```<code>dt.datetime(), dt.timedelta(), dt.time()
</code>```

Complete code:

```<code>import random
from datetime import datetime, timedelta, time
from pytz import timezone

current_date = datetime.now(timezone('America/Sao_Paulo'))
new_date = current_date - timedelta(days=2)
print (new_date.strftime("%Y-%m-%d %H:%M:%S"))

while new_date <= current_date:
  new_date = new_date + timedelta(minutes=10)
  print (new_date.strftime("%Y-%m-%d %H:%M:%S"))
  if new_date.time() < time(12):
    print("test")
</code>```

Body:
I'm working in small test code to determine if a generated datetime is before or after the midday like the code below.

import random
from datetime import datetime, timedelta
from pytz import timezone

current_date = datetime.now(timezone('America/Sao_Paulo'))
new_date = current_date - timedelta(days=2)
print (new_date.strftime("%Y-%m-%d %H:%M:%S"))

while new_date <= current_date:
  new_date = new_date + timedelta(minutes=10)
  print (new_date.strftime("%Y-%m-%d %H:%M:%S"))
  if new_date < datetime.time(12):
    print("test")

The problem is, I can't verify if the new_date is under the midday. Probably I'm doing something wrong in the if condition, right?

I would like to print some results if the new_dateis after midday and another message if it's after midday to midnight.

Any suggestions how I can solve this?

Body:
You need to compare just the time from new_date, with 12 converted to a datetime object.
Because of the way you did the imports, when you use datetime.time() it means you are calling datetime.datetime.time() and not datetime.time(), which is why the conversion fails.
You can solve this by

from datetime import datetime, timedelta, time

and then use

time()

or by

import datetime as dt

then using in the appropriate places

dt.datetime(), dt.timedelta(), dt.time()

Complete code:

import random
from datetime import datetime, timedelta, time
from pytz import timezone

current_date = datetime.now(timezone('America/Sao_Paulo'))
new_date = current_date - timedelta(days=2)
print (new_date.strftime("%Y-%m-%d %H:%M:%S"))

while new_date <= current_date:
  new_date = new_date + timedelta(minutes=10)
  print (new_date.strftime("%Y-%m-%d %H:%M:%S"))
  if new_date.time() < time(12):
    print("test")


问:
SSRS Report expands table width on page 4 only on PDF
My report is doing this:

This only happens when I export the report to PDF.  HTML view looks just fine...

I am not sure where to even start.  The first 3 pages are perfect.  It is just page 4 that gets messed up.

Any guesses of where I should look to fix this are welcome.

答:
I've seen this same thing in SSRS 2008 and it was extremely frustrating; it was definitely a weird bug of some kind and not just a page-overlap kind of thing (I turned on borders, and made sure none of the cells weren't meant to expand).

From memory, recreating the tablix from scratch made it go away. Although, around the same time I was updating to 2008 SP2 so you might want to check what revision of service pack you're on, too.


问:
Get Length of array JSON.Net
How can I get the length of a JSON Array I get using json.net in C#? After sending a SOAP call I get a JSON string as answer, I use json.net to parse it.

Example of the json I got:

```<code>{"JSONObject": [
    {"Id":"ThisIsMyId","Value":"ThisIsMyValue"},
    {"Id":"ThisIsMyId2","Value":"ThisIsMyValue2"}
]}
</code>```

And I parse it and write it in console:

```<code>var test = JObject.Parse (json);
Console.WriteLine ("Id: {0} Value: {1}", (string)test["JSONObject"][0]["Id"], (string)test["JSONObject"][0]["Value"]);
</code>```

This works like a spell, only I don't know the length of the JSONObject, but I need to do it in a for loop. I only have no idea how I can get the length of test["JSONObject"]

But something like test["JSONObject"].Length would be too easy I guess :(..

答:
This worked for me supposing the json data is in a json file.
In this case, .Length works but no intellisence is available:</p>

```<code>    public ActionResult Index()
    {
        string jsonFilePath = "C:\\folder\\jsonLength.json";
        var configFile = System.IO.File.ReadAllText(jsonFilePath);

        JavaScriptSerializer jss = new JavaScriptSerializer();
        var d = jss.Deserialize<dynamic>(configFile);

        var jsonObject = d["JSONObject"];
        int jsonObjectLength = jsonObject.Length;
        return View(jsonObjectLength);
    }
</code>```

Title: Get Length of array JSON.Net

Body:
How can I get the length of a JSON Array I get using json.net in C#? After sending a SOAP call I get a JSON string as answer, I use json.net to parse it.

Example of the json I got:

{"JSONObject": [
    {"Id":"ThisIsMyId","Value":"ThisIsMyValue"},
    {"Id":"ThisIsMyId2","Value":"ThisIsMyValue2"}
]}

And I parse it and write it in console:

var test = JObject.Parse (json);
Console.WriteLine ("Id: {0} Value: {1}", (string)test["JSONObject"][0]["Id"], (string)test["JSONObject"][0]["Value"]);

This works like a spell, only I don't know the length of the JSONObject, but I need to do it in a for loop. I only have no idea how I can get the length of test["JSONObject"]

But something like test["JSONObject"].Length would be too easy I guess :(..

Body:
This worked for me supposing the json data is in a json file.
In this case, .Length works but no intellisence is available:

    public ActionResult Index()
    {
        string jsonFilePath = "C:\\folder\\jsonLength.json";
        var configFile = System.IO.File.ReadAllText(jsonFilePath);

        JavaScriptSerializer jss = new JavaScriptSerializer();
        var d = jss.Deserialize<dynamic>(configFile);

        var jsonObject = d["JSONObject"];
        int jsonObjectLength = jsonObject.Length;
        return View(jsonObjectLength);
    }


问:
How do I set up node app to access PostgreSQL using typeorm?
I found a website https://github.com/typeorm/typeorm that, in a few steps, creates a typescript application able to connect to a PostgreSQL database. I followed the steps and modified the example to connect to my own existing database and successfully fetch the contents of one table. What my goal is though is to create a REST service deployed to nodeJS that's able to provide access to persistent data stored in the PostgreSQL database. I found another website with an example (over 2 parts) of all that I wanted to do and more. https://itnext.io/production-ready-node-js-rest-apis-setup-using-typescript-postgresql-and-redis-a9525871407 Part 2 was to go into the integration of PostgreSQL. It seems that Part 2 was never published, so I was left with a node app that looks like a great start. So I tried by myself to integrate the missing pieces so that the code deployed to the node REST service could access my database.
While the code works as expected when using this script to start it:

```<code>"start": "ts-node src/index.ts"
</code>```

This script causes an error in the entity source on the import statement to pull in the typeorm required classes:

```<code>"dev": "tsc-watch --onSuccess \"node ./build/src/index.js\""
</code>```

Here's the error:

```<code>/Users/jboss/projects/typescript/backend/src/entity/Banners.ts:1
import { Column, Entity, BaseEntity } from "typeorm";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:983:16)
    at Module._compile (internal/modules/cjs/loader.js:1033:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1103:10)
    at Module.load (internal/modules/cjs/loader.js:914:32)
    at Function.Module._load (internal/modules/cjs/loader.js:822:14)
    at Module.require (internal/modules/cjs/loader.js:956:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Function.PlatformTools.load (/Users/jboss/projects/typescript/backend/node_modules/typeorm/platform/PlatformTools.js:114:28)
    at /Users/jboss/projects/typescript/backend/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:39:69
    at Array.map (<anonymous>)
</code>```

This is the ormconfig content:

```<code>{
   "type": "postgres",
   "host": "localhost",
   "port": 5432,
   "username": "jboss",
   "password": "bluegrass",
   "database": "video",
   "synchronize": false,
   "logging": true,
   "entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],
   "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
   }
}
</code>```

Here's the package.json content:

```<code>{
   "name": "backend",
   "version": "0.0.1",
   "description": "Awesome project developed with TypeORM.",
   "devDependencies": {
      "ts-node": "3.3.0",
      "@types/node": "^8.0.29",
      "typescript": "3.3.3333"
   },
   "dependencies": {
      "pg": "^7.18.2",
      "reflect-metadata": "^0.1.13",
      "typeorm": "^0.2.24"
   },
   "scripts": {
      "dev": "tsc-watch --onSuccess \"node ./build/src/index.js\"",
      "start": "ts-node src/index.ts",
      "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js"
   }
}
</code>```

Here's the tsconfig.json content:

```<code>{
   "compilerOptions": {
      "lib": [
         "es5",
         "es6"
      ],
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "outDir": "./build",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "sourceMap": true
   }
}
</code>```

I'm hoping someone can give me a hint as to what I need to do to make this work.

答:
You want to check the "entities" property in your ormconfig.json.

It should be something like

```<code>{
  //...
  "entities": ["build/src/entity/*.*"]
}
</code>```

Body:
I found a website https://github.com/typeorm/typeorm that, in a few steps, creates a typescript application able to connect to a PostgreSQL database. I followed the steps and modified the example to connect to my own existing database and successfully fetch the contents of one table. What my goal is though is to create a REST service deployed to nodeJS that's able to provide access to persistent data stored in the PostgreSQL database. I found another website with an example (over 2 parts) of all that I wanted to do and more. https://itnext.io/production-ready-node-js-rest-apis-setup-using-typescript-postgresql-and-redis-a9525871407 Part 2 was to go into the integration of PostgreSQL. It seems that Part 2 was never published, so I was left with a node app that looks like a great start. So I tried by myself to integrate the missing pieces so that the code deployed to the node REST service could access my database.
While the code works as expected when using this script to start it:

"start": "ts-node src/index.ts"

This script causes an error in the entity source on the import statement to pull in the typeorm required classes:

"dev": "tsc-watch --onSuccess \"node ./build/src/index.js\""

Here's the error:

/Users/jboss/projects/typescript/backend/src/entity/Banners.ts:1
import { Column, Entity, BaseEntity } from "typeorm";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:983:16)
    at Module._compile (internal/modules/cjs/loader.js:1033:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1103:10)
    at Module.load (internal/modules/cjs/loader.js:914:32)
    at Function.Module._load (internal/modules/cjs/loader.js:822:14)
    at Module.require (internal/modules/cjs/loader.js:956:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Function.PlatformTools.load (/Users/jboss/projects/typescript/backend/node_modules/typeorm/platform/PlatformTools.js:114:28)
    at /Users/jboss/projects/typescript/backend/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:39:69
    at Array.map (<anonymous>)

This is the ormconfig content:

{
   "type": "postgres",
   "host": "localhost",
   "port": 5432,
   "username": "jboss",
   "password": "bluegrass",
   "database": "video",
   "synchronize": false,
   "logging": true,
   "entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],
   "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
   }
}

Here's the package.json content:

{
   "name": "backend",
   "version": "0.0.1",
   "description": "Awesome project developed with TypeORM.",
   "devDependencies": {
      "ts-node": "3.3.0",
      "@types/node": "^8.0.29",
      "typescript": "3.3.3333"
   },
   "dependencies": {
      "pg": "^7.18.2",
      "reflect-metadata": "^0.1.13",
      "typeorm": "^0.2.24"
   },
   "scripts": {
      "dev": "tsc-watch --onSuccess \"node ./build/src/index.js\"",
      "start": "ts-node src/index.ts",
      "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js"
   }
}

Here's the tsconfig.json content:

{
   "compilerOptions": {
      "lib": [
         "es5",
         "es6"
      ],
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "outDir": "./build",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "sourceMap": true
   }
}

I'm hoping someone can give me a hint as to what I need to do to make this work.

Body:
You want to check the "entities" property in your ormconfig.json.

It should be something like

{
  //...
  "entities": ["build/src/entity/*.*"]
}


问:
MS Word Macro: How to replace a duplicate paragraph with a new paragraph?
I wonder if you could help me with the following problem.

I have a huge document that contains many duplicate paragraphs. I've found a macro on the Internet that can find them all and delete them:

```<code>Sub DeleteDuplicateParagraphs()
'PURPOSE: Remove Duplicate Paragraphs Throughout the Entire Word Document
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim p1 As Paragraph
Dim p2 As Paragraph
Dim DupCount As Long

DupCount = 0

For Each p1 In ActiveDocument.Paragraphs
  If p1.Range.Text <> vbCr Then 'Ignore blank paragraphs

    For Each p2 In ActiveDocument.Paragraphs
      If p1.Range.Text = p2.Range.Text Then
        DupCount = DupCount + 1
        If p1.Range.Text = p2.Range.Text And DupCount > 1 Then p2.Range.Delete
      End If
    Next p2

  End If

  'Reset Duplicate Counter
    DupCount = 0

Next p1

End Sub

</code>```

However, I would like to know if there is a way to simulate Enter key press in order to create a new paragraph in the place where each duplicate was deleted. In other words, I need to replace the duplicate paragraphs with a new empty paragraph OR to delete the duplicate text without actually removing the paragraph.
I hope I was clear enough to make you see what I mean.
Thank you anyway.

答:
Simply change:

```<code>p2.Range.Delete
</code>```

to:

```<code>p2.Range.Text = vbCr
</code>```

Body:
I wonder if you could help me with the following problem.

I have a huge document that contains many duplicate paragraphs. I've found a macro on the Internet that can find them all and delete them:

Sub DeleteDuplicateParagraphs()
'PURPOSE: Remove Duplicate Paragraphs Throughout the Entire Word Document
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim p1 As Paragraph
Dim p2 As Paragraph
Dim DupCount As Long

DupCount = 0

For Each p1 In ActiveDocument.Paragraphs
  If p1.Range.Text <> vbCr Then 'Ignore blank paragraphs

    For Each p2 In ActiveDocument.Paragraphs
      If p1.Range.Text = p2.Range.Text Then
        DupCount = DupCount + 1
        If p1.Range.Text = p2.Range.Text And DupCount > 1 Then p2.Range.Delete
      End If
    Next p2

  End If

  'Reset Duplicate Counter
    DupCount = 0

Next p1

End Sub

However, I would like to know if there is a way to simulate Enter key press in order to create a new paragraph in the place where each duplicate was deleted. In other words, I need to replace the duplicate paragraphs with a new empty paragraph OR to delete the duplicate text without actually removing the paragraph.
I hope I was clear enough to make you see what I mean.
Thank you anyway.

Body:
Simply change:

p2.Range.Delete

to:

p2.Range.Text = vbCr


问:
Server.Htmlencode performance versus Regex.Replace versus String.Replace
Which is fastest in an ASP.NET app? Trying to catch all manners of naughty XSS/CSRF and the like.

Server.HtmlEncode before displaying, String.Replace calls (one after another), or a complicated regex followed by a Regex.Replace?

Thanks for any info

答: I'd argue that you will likely never recover the amount of time needed to post this question.  Server.HtmlEncode does the job, does it right, and is "fast enough".  Are you actually seeing performance issues with it?</p>


问:
os.makedirs() / os.mkdir() on Windows. No error, but no (visible) folder created
I just try to create some new folders with Python's (3.7.3) os.makedirs() and os mkdir().
Apparently, it works fine because no error occurs (Win 10 Home). But as I try to find the created folder in the windows explorer it isn't there.

Trying to create it again with python I get an error:
'[WinError 183] Cannot create a file when that file already exists:'

Strange thing is, all this worked fine on my computer at work (Wind 10 too), and also on my android tablet.

I've already tried to use relative & absolute paths in makedirs / mkdir.

Ok, it's all about these few lines:

```<code>import os

# print(os.getcwd()) shows: C:\Users\Andrej\Desktop

# tried relative path..
os.makedirs('Level1/Level2/Level3')

# tried out some absolute paths like...
os.makedirs('C:/Users/Andrej/Desktop/Level1/Level2/Level3')
os.makedirs('C:\\Users\\Andrej\\Desktop\\Level1\\Level2\\Level3')
</code>```

UPDATE: It works perfectly when I write makedirs command directly in the Windows powershell. The issue above only occurs when I write this code in Visual Code Studio and then start the file from the powershell by typing in: python makedirs.py...

答:
I just had the same thing happen to me, except I was creating a folder in the AppData/Roaming directory and was using the version of Python from the Microsoft Store.

Apparently, files and directories created in AppData using that version of Python, will instead be created in:

```<code>%LocalAppData%\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache
</code>```

Body:
I just try to create some new folders with Python's (3.7.3) os.makedirs() and os mkdir().
Apparently, it works fine because no error occurs (Win 10 Home). But as I try to find the created folder in the windows explorer it isn't there.

Trying to create it again with python I get an error:
'[WinError 183] Cannot create a file when that file already exists:'

Strange thing is, all this worked fine on my computer at work (Wind 10 too), and also on my android tablet.

I've already tried to use relative & absolute paths in makedirs / mkdir.

Ok, it's all about these few lines:

import os

# print(os.getcwd()) shows: C:\Users\Andrej\Desktop

# tried relative path..
os.makedirs('Level1/Level2/Level3')

# tried out some absolute paths like...
os.makedirs('C:/Users/Andrej/Desktop/Level1/Level2/Level3')
os.makedirs('C:\\Users\\Andrej\\Desktop\\Level1\\Level2\\Level3')

UPDATE: It works perfectly when I write makedirs command directly in the Windows powershell. The issue above only occurs when I write this code in Visual Code Studio and then start the file from the powershell by typing in: python makedirs.py...

Body:
I just had the same thing happen to me, except I was creating a folder in the AppData/Roaming directory and was using the version of Python from the Microsoft Store.

Apparently, files and directories created in AppData using that version of Python, will instead be created in:

%LocalAppData%\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache


问:
Waiting for another flutter command to release the startup lock
When I run my flutter application it show

Waiting for another flutter command to release the startup lock

this messages and not proceed further.

答:
You can remove flutter and "install again", so fastest way is to:

1) Check where you have flutter (as executable)

To check it, you can run:

```<code>which flutter
</code>```

And you can expect such output:

```<code>/your_user/your_path/flutter/bin/flutter
</code>```

2) Go to this directory

Above path is directly to flutter, but we would like to go to directory so "one earlier" in path:

```<code>cd /your_user/your_path/flutter/bin
</code>```

3) Check is it git repository

It should be, but it's worth to check.

Run one of the git command e.g.

```<code>git status
</code>```

or

```<code>git branch
</code>```

When it's git repository you will receive some info about it or correct list of branches.

4) Remove all of the files

```<code>rm -R *
</code>```

5) "revert" removal to have only necessary files:

```<code>git co -- .
</code>```

6) Voila!

You can run just flutter version and commands should work (and Flutter will fetch some necessary data).

Body:
You can remove flutter and "install again", so fastest way is to:

1) Check where you have flutter (as executable)

To check it, you can run:

which flutter

And you can expect such output:

/your_user/your_path/flutter/bin/flutter

2) Go to this directory

Above path is directly to flutter, but we would like to go to directory so "one earlier" in path:

cd /your_user/your_path/flutter/bin

3) Check is it git repository

It should be, but it's worth to check.

Run one of the git command e.g.

git status

or

git branch

When it's git repository you will receive some info about it or correct list of branches.

4) Remove all of the files

rm -R *

5) "revert" removal to have only necessary files:

git co -- .

6) Voila!

You can run just flutter version and commands should work (and Flutter will fetch some necessary data).


问:
Why does the generated view binding .inflate() method requires root parameter?
I was following the example from the documentation on a personal project.

I have a layout called activity_main.xml, for which an ActivityMainBinding class is generated. When I call ActivityMainBinding.inflate(layoutInflater), I get an error saying that the method requires the "root" parameter of type ViewGroup.

Why isn't the method with only one argument (the inflater) available?

I've tried cleaning and rebuilding the project.

build.gradle (app)

```<code>apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.teddy"
        minSdkVersion 22
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    viewBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
}
</code>```

activity_main.xml

```<code><?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/navigation" />

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/navdrawer" />
    </androidx.drawerlayout.widget.DrawerLayout>
</merge>
</code>```

MainActivity.kt

```<code>package com.example.teddy

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.example.teddy.databinding.ActivityMainBinding
import com.google.android.material.navigation.NavigationView

class MainActivity : AppCompatActivity() {
    private lateinit var drawerLayout: DrawerLayout
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)

        val navController = this.findNavController(R.id.nav_host_fragment)
        drawerLayout = binding.drawerLayout

        val appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayout)
        findViewById<NavigationView>(R.id.navView)
            .setupWithNavController(navController)
    }
}
</code>```

Does anyone know what I am missing?

EDIT
The exact error from the failed build output:

No value passed for parameter 'parent’

The tooltip on the underlined error in the code says

No value passed for parameter 'root'

答:
I might have solved the problem by changing the merge tag by a layout tag in the activity_main.xml. Not really sure why I had a merge there... Probably legacy of a template.

Thank you for looking into it !

Body:
I was following the example from the documentation on a personal project.

I have a layout called activity_main.xml, for which an ActivityMainBinding class is generated. When I call ActivityMainBinding.inflate(layoutInflater), I get an error saying that the method requires the "root" parameter of type ViewGroup.

Why isn't the method with only one argument (the inflater) available?

I've tried cleaning and rebuilding the project.

build.gradle (app)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.teddy"
        minSdkVersion 22
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    viewBinding {
        enabled = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/navigation" />

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/navdrawer" />
    </androidx.drawerlayout.widget.DrawerLayout>
</merge>

MainActivity.kt

package com.example.teddy

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.example.teddy.databinding.ActivityMainBinding
import com.google.android.material.navigation.NavigationView

class MainActivity : AppCompatActivity() {
    private lateinit var drawerLayout: DrawerLayout
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)

        val navController = this.findNavController(R.id.nav_host_fragment)
        drawerLayout = binding.drawerLayout

        val appBarConfiguration = AppBarConfiguration(navController.graph, drawerLayout)
        findViewById<NavigationView>(R.id.navView)
            .setupWithNavController(navController)
    }
}

Does anyone know what I am missing?

EDIT
The exact error from the failed build output:

No value passed for parameter 'parent’

The tooltip on the underlined error in the code says

No value passed for parameter 'root'


问:
Kotlin Android - Is there a way to define the Views only one time in a class?
In my code I make use of the following Views in XML:

```<code>        val googleButton: Button = findViewById<View>(R.id.google_login) as Button
        val loginWithEmailText: TextView = findViewById(R.id.login_with_email_text)
        val emailLoginButton: Button = findViewById(R.id.email_login_button)
        val createAccountButton: Button = findViewById(R.id.email_create_account_button)
</code>```

This code is extracted from a function inside my Kotlin class. Whenever I have to access these views, I need to write this code all over again.

Is there any way that I can access them from only one place in my class code? I tried putting them outside but the app won't start.

Thank you

答:
You need to define these fields as a part of your class and initialize them once you set the layout resource for your Activity/Fragment. If you put these lines 1:1 in the class body, the initialization will fail, since the layout has not been inflated yet.

Please get familiar with the concept of lifecycle, so that you can understand how to approach View related topics: https://developer.android.com/guide/components/activities/activity-lifecycle

Please check out this snippet for a sample code:

class MyActivity: Activity() {
    lateinit var textView: TextView
    lateinit var button: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my)

        // initialize your views here
        textView = findViewById(R.id.text_view_id)
        button = findViewById(R.id.button_id)
    }

    fun someOtherFunction(){
        // you can reference your views here like normal properties
        button.setOnClickListener { v -> callAnotherFunction() }
        // ...
    }
}
```

Since you are on Android, you might be interested in using Kotlin synthetic properties for referencing views without the whole boilerplate of finding them: https://antonioleiva.com/kotlin-android-extensions/. It's no longer a  recommended practice to make use of it, but it's handy in some cases anyway.

Body:
In my code I make use of the following Views in XML:

        val googleButton: Button = findViewById<View>(R.id.google_login) as Button
        val loginWithEmailText: TextView = findViewById(R.id.login_with_email_text)
        val emailLoginButton: Button = findViewById(R.id.email_login_button)
        val createAccountButton: Button = findViewById(R.id.email_create_account_button)

This code is extracted from a function inside my Kotlin class. Whenever I have to access these views, I need to write this code all over again.

Is there any way that I can access them from only one place in my class code? I tried putting them outside but the app won't start.

Thank you

Body:
You need to define these fields as a part of your class and initialize them once you set the layout resource for your Activity/Fragment. If you put these lines 1:1 in the class body, the initialization will fail, since the layout has not been inflated yet.

Please get familiar with the concept of lifecycle, so that you can understand how to approach View related topics: https://developer.android.com/guide/components/activities/activity-lifecycle

Please check out this snippet for a sample code:

class MyActivity: Activity() {
    lateinit var textView: TextView
    lateinit var button: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my)

        // initialize your views here
        textView = findViewById(R.id.text_view_id)
        button = findViewById(R.id.button_id)
    }

    fun someOtherFunction(){
        // you can reference your views here like normal properties
        button.setOnClickListener { v -> callAnotherFunction() }
        // ...
    }
}

Since you are on Android, you might be interested in using Kotlin synthetic properties for referencing views without the whole boilerplate of finding them: https://antonioleiva.com/kotlin-android-extensions/. It's no longer a  recommended practice to make use of it, but it's handy in some cases anyway.


问:
Using Bot Framework v4, how can I determine if an OAuthPrompt was just logged in or if the token already existed?
I'm building a teams bot and in it I have an OAuthPrompt to gather credentials for an external service. After initially logging in, I would like to display a "You are now logged in" message. The problem I have is determining if the login is the initial login (or a token refresh which I have to handle manually since the service doesn't provide token life or refresh abilities) or if it is gathering an existing token. I was checking for the activity "invoke" which worked initial, but as my bot gets more complicated, this method is no longer working.

I have a waterfall dialog the follows the flow:

GetServiceTokenStep (calls GetServiceTokenDialog) -> DoSomethingWithTokenStep

In DoSomethingWithTokenStep, one of my actions redirects to another dialog VerifyRequestDialog. In that dialog, I call GetServiceTokenDialog again to retrieve the token I got earlier (or get a new one if needed). The problem is if they had to get the token in GetServiceTokenStep, then when it reaches VerifyRequestDialog then the activity is still "invoke" so it shows the "Hello" message again, even though is already has the token.

What can I check to see if the OAuthPrompt returned an existing token or got one from a new login?

答:
Teams behaves slightly differently than other channels in this regard. Specifically, an Invoke Activity is sent to the bot rather than the Event Activity used by other channels. This Invoke Activity must be forwarded to the dialog if the OAuthPrompt is being used. This is done by subclassing the ActivityHandler and this sample includes a reusable TeamsActivityHandler. Have you tried using the sample 46.teams-auth?

The sample uses the bot authentication capabilities in Azure Bot Service, providing features to make it easier to develop a bot that authenticates users to various identity providers such as Azure AD (Azure Active Directory), GitHub, Uber, etc. The OAuth token is then used to make basic Microsoft Graph queries. Using this sample, you get an option to view your token.

Hope this helps!!


问:
Does emmeans use lm_robust cluster-robust standard errors when calculating confidence intervals for transformed outcome variables?
I'm examining interactions between two continuous predictor variables using the emmeans package. I'm using lm_robust() from the estimatr package to perform the linear regression and obtain cluster-robust standard errors. The outcome variable is centered and scaled to SD unit variance. For example:

fit <- lm_robust(scale(Y) ~ X1 * X2 + X3 + X4, data = mydata, cluster = school, se_type = 'CR2')

I can then perform pairwise contrasts or visualise the lines at three levels of X2 using code similar to:

```<code>emmip(fit, X2 ~ X1, CIs = TRUE, at = list(X2 = c(mean(X2) - sd(X2),
                                                 mean(X2),
                                                 mean(X2) + sd(X2))))
</code>```

I do not wish to back transform the outcome variable to its original scale.

My question is whether emmeans uses the cluster-robust standard errors to calculate the confidence intervals or the p-values that it reports and does this behaviour depend on whether the outcome variable is on its original scale or transformed? A short example on the estimatr package creators' website suggests that lm_robust objects can be used with emmeans, but I can't see lm_robust listed as a supported model on the "Models supported by emmeans" vignette page or the package documentation.

答:
I believe thar lm_robust objects are an extension of lm, so it uses the emmeans support for lm. In turn, that would imply that the estimates are ovtained via coef(model) and their SEs are derived using vcov(model). So if vcov() returns the robust variances you need, emmeans will use them.

Regarding most transformations, it will work as described in the vignette on transformations. In particular, specifying type = "response" results in the estimates and confidence limits to be back-transformed, the P values to be left alone, and the SEs to be computed by the delta method (but not used in the CIs and tests).

Additional information

First, I found out that lm_robust does not inherit from lm; rather, the estimatr package incorporates its own support for emmeans. Not many details are given, but that developer of estimatr must believe that what is provided must be appropriate.

The scale() transformation is not built-in, because it is complicated. Just saying we used "scale" isn't as simple as saying it is "log", say, because to work with scale() results, we need to know what was used to center and divide the results.

The workaround is to create the object that emmeans() and its relatives need to invert the transformation; and that is a list of functions of the form returned by stats::make.link() or emmeans::make.tran(). Here is a function that will serve that purpose:

```<code>make.scaletran = function(y, ...) {
    sy = scale(y, ...)
    if(is.null(m <- attr(sy, "scaled:center")))
       m = 0
    if(is.null(s <- attr(sy, "scaled:scale")))
        s = 1
    list(
        linkfun = function(mu) (mu - m) / s,
        linkinv = function(eta) s * eta + m,
        mu.eta = function(eta) s,
        valideta = function(eta) TRUE,
        name = paste0("scale(", signif(m, 3), ", ", signif(s, 3), ")")
    )
}
</code>```

To use this, you need to manually specify the transformation since it isn't auto-detected. Here is an example using the warpbreaks data already available in R:

```<code>> warp.lmr = lm_robust(scale(breaks) ~ tension, cluster = wool,
+     se_type = 'CR2', data = warpbreaks)

> tran = make.scaletran(warpbreaks$breaks)

> emmeans(warp.lmr, "tension", tran = tran)
 tension emmean    SE df lower.CL upper.CL
 L        0.624 0.619 51   -0.618   1.8666
 M       -0.133 0.181 51   -0.497   0.2301
 H       -0.491 0.219 51   -0.930  -0.0517

Results are given on the scale(28.1, 13.2) (not the response) scale.
Confidence level used: 0.95

> emmeans(warp.lmr, "tension", tran = tran, type = "response")
 tension response   SE df lower.CL upper.CL
 L           36.4 8.17 51     20.0     52.8
 M           26.4 2.39 51     21.6     31.2
 H           21.7 2.89 51     15.9     27.5

Confidence level used: 0.95
Intervals are back-transformed from the scale(28.1, 13.2) scale
</code>```

The code in the OP for the emmip() call is not correct, as it uses the specs for emmeans(), not emmip().

I will consider adding this scale transformation option to emmeans::make.tran() in a future update.

Body:
I'm examining interactions between two continuous predictor variables using the emmeans package. I'm using lm_robust() from the estimatr package to perform the linear regression and obtain cluster-robust standard errors. The outcome variable is centered and scaled to SD unit variance. For example:

fit <- lm_robust(scale(Y) ~ X1 * X2 + X3 + X4, data = mydata, cluster = school, se_type = 'CR2')

I can then perform pairwise contrasts or visualise the lines at three levels of X2 using code similar to:

emmip(fit, X2 ~ X1, CIs = TRUE, at = list(X2 = c(mean(X2) - sd(X2),
                                                 mean(X2),
                                                 mean(X2) + sd(X2))))

I do not wish to back transform the outcome variable to its original scale.

My question is whether emmeans uses the cluster-robust standard errors to calculate the confidence intervals or the p-values that it reports and does this behaviour depend on whether the outcome variable is on its original scale or transformed? A short example on the estimatr package creators' website suggests that lm_robust objects can be used with emmeans, but I can't see lm_robust listed as a supported model on the "Models supported by emmeans" vignette page or the package documentation.

Body:
I believe thar lm_robust objects are an extension of lm, so it uses the emmeans support for lm. In turn, that would imply that the estimates are ovtained via coef(model) and their SEs are derived using vcov(model). So if vcov() returns the robust variances you need, emmeans will use them.

Regarding most transformations, it will work as described in the vignette on transformations. In particular, specifying type = "response" results in the estimates and confidence limits to be back-transformed, the P values to be left alone, and the SEs to be computed by the delta method (but not used in the CIs and tests).

Additional information

First, I found out that lm_robust does not inherit from lm; rather, the estimatr package incorporates its own support for emmeans. Not many details are given, but that developer of estimatr must believe that what is provided must be appropriate.

The scale() transformation is not built-in, because it is complicated. Just saying we used "scale" isn't as simple as saying it is "log", say, because to work with scale() results, we need to know what was used to center and divide the results.

The workaround is to create the object that emmeans() and its relatives need to invert the transformation; and that is a list of functions of the form returned by stats::make.link() or emmeans::make.tran(). Here is a function that will serve that purpose:

make.scaletran = function(y, ...) {
    sy = scale(y, ...)
    if(is.null(m <- attr(sy, "scaled:center")))
       m = 0
    if(is.null(s <- attr(sy, "scaled:scale")))
        s = 1
    list(
        linkfun = function(mu) (mu - m) / s,
        linkinv = function(eta) s * eta + m,
        mu.eta = function(eta) s,
        valideta = function(eta) TRUE,
        name = paste0("scale(", signif(m, 3), ", ", signif(s, 3), ")")
    )
}

To use this, you need to manually specify the transformation since it isn't auto-detected. Here is an example using the warpbreaks data already available in R:

> warp.lmr = lm_robust(scale(breaks) ~ tension, cluster = wool,
+     se_type = 'CR2', data = warpbreaks)

> tran = make.scaletran(warpbreaks$breaks)

> emmeans(warp.lmr, "tension", tran = tran)
 tension emmean    SE df lower.CL upper.CL
 L        0.624 0.619 51   -0.618   1.8666
 M       -0.133 0.181 51   -0.497   0.2301
 H       -0.491 0.219 51   -0.930  -0.0517

Results are given on the scale(28.1, 13.2) (not the response) scale.
Confidence level used: 0.95

> emmeans(warp.lmr, "tension", tran = tran, type = "response")
 tension response   SE df lower.CL upper.CL
 L           36.4 8.17 51     20.0     52.8
 M           26.4 2.39 51     21.6     31.2
 H           21.7 2.89 51     15.9     27.5

Confidence level used: 0.95
Intervals are back-transformed from the scale(28.1, 13.2) scale

The code in the OP for the emmip() call is not correct, as it uses the specs for emmeans(), not emmip().

I will consider adding this scale transformation option to emmeans::make.tran() in a future update.


问:
Split cells containing ALLCAPS words
I have a .csv file containing a column "First + Last name". I'd like to split the cells to get 2 columns (First Name and Last name). In each cell, the last name is written ALLCAPS. So here is my file right now :

```<code>First + Last name
-----------------------------
John DOE
Marie-Helen ANDRE-JACQUES
Jean-Claude DOE
</code>```

And i'd like to split cells so i have :

```<code>First name               |  Last name
--------------------------------------------------
John                     |  DOE
Marie-Helen              |  ANDRE-JACQUES
Jean-Claude              |  DOE
</code>```

How would i do this in excel (or numbers) ?

答:
A good way to solve this problem is to use the FLASH-FILL Function of Excel.

https://support.office.com/en-us/article/using-flash-fill-in-excel-3f9bcf1e-db93-4890-94a0-1578341f73f7?ui=en-US&rs=en-US&ad=US

Title: Split cells containing ALLCAPS words

Body:
I have a .csv file containing a column "First + Last name". I'd like to split the cells to get 2 columns (First Name and Last name). In each cell, the last name is written ALLCAPS. So here is my file right now :

First + Last name
-----------------------------
John DOE
Marie-Helen ANDRE-JACQUES
Jean-Claude DOE

And i'd like to split cells so i have :

First name               |  Last name
--------------------------------------------------
John                     |  DOE
Marie-Helen              |  ANDRE-JACQUES
Jean-Claude              |  DOE

How would i do this in excel (or numbers) ?


问:
How do I generate a uniqueidentifier for a set of records with a distinct key?
I have a record set that looks like this:

| key_sk | unique_id                            |
|--------|--------------------------------------|
| 2      | null                                 |
| 2      | null                                 |
| 3      | 83a1c90b-e58d-4db4-b438-a79edfb28e60 |
| 3      | 83a1c90b-e58d-4db4-b438-a79edfb28e60 |
| 4      | 4ce66783-0b84-4e8a-a0de-c3284e4d9cd0 |
| 5      | null                                 |
```

<p>I would like to generate a unique id for each unique <code>key_sk</code> set where the <code>unique_id</code> is null. For the above I'd like <code>key_sk 2</code> to have a a single <code>unique_id</code> like <code>key_sk 3</code> does.</p>

<p>My attempt below generated a different <code>uniqueidentifier</code> for each set. I think this is because of the recursive nature of common table expressions: each join to the CTE causes <code>NEWID()</code> to be called.</p>

<pre class="lang-sql prettyprint-override"><code>;with update_id_cte as
(
  select distinct hr.key_sk
        ,NEWID() as gened_unique_id
    from history_record hr
   where hr.unique_id is null
)
update hr
   set hr.unique_id = cte.gened_unique_id
  from history_record hr
       join update_id_cte cte
         on hr.key_sk = cte.key_sk
</code>```

There's probably a simpler way than utilizing a CTE to do this. How can I generate and update the history_record table with a single uniqueidentifier for each distinct key_sk?

答:
In at least older version of MySQL this can be a pain as you are checking and want to check the same column, one way to do it is with temporary tables.

This is not one query but rather part of a stored procedure, but if you only going to do it once it's you can just run it.

```<code>CREATE TEMPORARY TABLE IF NOT EXISTS tmp
select distinct hr.key_sk ,NEWID() as gened_unique_id
from history_record hr
where hr.unique_id is null;

update hr
set hr.unique_id = tmp.gened_unique_id
from history_record hr
inner join tmp on hr.key_sk = tmp.key_sk;
</code>```

Body:
I have a record set that looks like this:

| key_sk | unique_id                            |
|--------|--------------------------------------|
| 2      | null                                 |
| 2      | null                                 |
| 3      | 83a1c90b-e58d-4db4-b438-a79edfb28e60 |
| 3      | 83a1c90b-e58d-4db4-b438-a79edfb28e60 |
| 4      | 4ce66783-0b84-4e8a-a0de-c3284e4d9cd0 |
| 5      | null                                 |

I would like to generate a unique id for each unique key_sk set where the unique_id is null. For the above I'd like key_sk 2 to have a a single unique_id like key_sk 3 does.

My attempt below generated a different uniqueidentifier for each set. I think this is because of the recursive nature of common table expressions: each join to the CTE causes NEWID() to be called.

;with update_id_cte as
(
  select distinct hr.key_sk
        ,NEWID() as gened_unique_id
    from history_record hr
   where hr.unique_id is null
)
update hr
   set hr.unique_id = cte.gened_unique_id
  from history_record hr
       join update_id_cte cte
         on hr.key_sk = cte.key_sk

There's probably a simpler way than utilizing a CTE to do this. How can I generate and update the history_record table with a single uniqueidentifier for each distinct key_sk?

Body:
In at least older version of MySQL this can be a pain as you are checking and want to check the same column, one way to do it is with temporary tables.

This is not one query but rather part of a stored procedure, but if you only going to do it once it's you can just run it.

CREATE TEMPORARY TABLE IF NOT EXISTS tmp
select distinct hr.key_sk ,NEWID() as gened_unique_id
from history_record hr
where hr.unique_id is null;

update hr
set hr.unique_id = tmp.gened_unique_id
from history_record hr
inner join tmp on hr.key_sk = tmp.key_sk;


问:
full image with rectangle with text inside of it
Trying to put on my image a red rectangle with white text in it.
Same as its shown here

So far, I can put the text in the top left corner but I still miss the full-width rectangle with the text inside it.

Where do I put the rectangle

```<code>from PIL import Image, ImageDraw, ImageFont
base = Image.open('C:\\Users\\t3cho\\Desktop\\DSC15178.jpg').convert('RGBA')
width, height = base.size
txt = Image.new('RGBA', base.size, (255,255,255,0))
fnt = ImageFont.truetype('arial.ttf', 80)
d = ImageDraw.Draw(txt)
d.text((0,0), "10% OFF FOR THIS ITEM", font=fnt, fill=(255,0,0,128))
out = Image.alpha_composite(base, txt)
out.show()
</code>```

答:
```<code>from PIL import Image, ImageFont, ImageDraw, ImageEnhance
from io import BytesIO

source_img = Image.open("C:\\Users\\t3cho\\Desktop\\DSC15178.jpg").convert("RGBA")
font = ImageFont.truetype(font=BytesIO(open("C:\\Windows\\Fonts\\COOPBL.TTF", "rb").read()),size=65)  #ImageFont.truetype("cooper black",65)
text = "This is my text centered"

# get text size
text_size = font.getsize(text)
# set button size + 10px margins
button_size =  (source_img.size[0], 100)  # (text_size[0]+20, text_size[1]+20)
# create image with correct size and black background
button_img = Image.new('RGBA', button_size, "red")
positionx = (float(source_img.size[0]) - float(text_size[0])) / float(2)
# put text on button with 10px margins
button_draw = ImageDraw.Draw(button_img)
button_draw.text((positionx, 10), text, font=font)
# put button on source image in position (0, 0)
source_img.paste(button_img, (0, 0))
# save in new file
im = source_img.convert("RGB")
im.save("C:\\Users\\t3cho\\Desktop\\DSC15178-out.jpg")
</code>```

Body:
Trying to put on my image a red rectangle with white text in it.
Same as its shown here

So far, I can put the text in the top left corner but I still miss the full-width rectangle with the text inside it.

Where do I put the rectangle

from PIL import Image, ImageDraw, ImageFont
base = Image.open('C:\\Users\\t3cho\\Desktop\\DSC15178.jpg').convert('RGBA')
width, height = base.size
txt = Image.new('RGBA', base.size, (255,255,255,0))
fnt = ImageFont.truetype('arial.ttf', 80)
d = ImageDraw.Draw(txt)
d.text((0,0), "10% OFF FOR THIS ITEM", font=fnt, fill=(255,0,0,128))
out = Image.alpha_composite(base, txt)
out.show()


问:
Why is beautifoulsount find_all missing this element when searching if text contains a string?
I have this test HTML page

```<code><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>    <html lang="en">
     <head>
      <title></title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta name="created" content="2020-05-29T11:12:00.0000000" />
     </head>
     <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
      <div id="div:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{216}" style="position:absolute;left:72px;top:43px;width:696px">
       <p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{218}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Test1###yrdy</span></p>
       <p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{220}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test2###qweqwe</p>
       <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{22}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="color:red">Test3</span> ###qweqeqwe</p>
       <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{17}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test4 ### sfsfsdfds</p>
       <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{19}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test5### 121212</p>
      </div>
     </body>
    </html></code>```

```

<p>After turning the above into a soup I am doing this</p>

``` for element in soup.find_all(["p"],text=re.compile("###")):
     print(element)
```

<p>The above prints these     </p>

```

Test1###yrdy

Test2###qweqwe

Test4 ### sfsfsdfds

Test5### 121212
```
</div>
</div>

</code>```

Why is it skipping the Test3 corresponding p?

Update:

Andrej, your suggestion works but it puzzles me because searching for

 soup.find_all(["p"],text=re.compile("###")) should have the same effect. This

```<code> for p in soup.html.body.find_all("p"):
    print(p.text)
</code>```

Test1###yrdy

Test2###qweqwe

Test3 ###qweqeqwe

Test4 ### sfsfsdfds

Test5### 121212

答:
Test3 does not have ### with in span, P tag, to fix that use soup.select('p')

Or simply use for element in soup.find_all(['p']): for all p tag

Example

```<code>import re

from bs4 import BeautifulSoup

html = """
    <html lang="en">
        <head>
            <title></title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="created" content="2020-05-29T11:12:00.0000000" />
        </head>
        <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
            <div id="div:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{216}" style="position:absolute;left:72px;top:43px;width:696px">
                <p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{218}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Test1###yrdy</span></p>
                <p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{220}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test2###qweqwe</p>
                <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{22}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="color:red">Test3 </span> ###qweqeqwe</p>
                <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{17}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test4 ### sfsfsdfds</p>
                <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{19}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test5### 121212</p>
            </div>
        </body>
    </html>
"""

soup = BeautifulSoup(html, features='html.parser')
for element in soup.select('p'):
    print(element)
</code>```

Prints out all

```<code><p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{218}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Test1###yrdy</span></p>
<p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{220}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test2###qweqwe</p>
<p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{22}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="color:red">Test3 </span> ###qweqeqwe</p>
<p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{17}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test4 ### sfsfsdfds</p>
<p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{19}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test5### 121212</p>

Process finished with exit code 0
</code>```

Body:
I have this test HTML page

      <title></title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta name="created" content="2020-05-29T11:12:00.0000000" />

Test1###yrdy

Test2###qweqwe

Test3 ###qweqeqwe

Test4 ### sfsfsdfds

Test5### 121212

After turning the above into a soup I am doing this

 for element in soup.find_all(["p"],text=re.compile("###")):
     print(element)

The above prints these

Test1###yrdy

Test2###qweqwe

Test4 ### sfsfsdfds

Test5### 121212

Why is it skipping the Test3 corresponding p?

Update:

Andrej, your suggestion works but it puzzles me because searching for

 soup.find_all(["p"],text=re.compile("###")) should have the same effect. This

 for p in soup.html.body.find_all("p"):
    print(p.text)

Test1###yrdy

Test2###qweqwe

Test3 ###qweqeqwe

Test4 ### sfsfsdfds

Test5### 121212

Body:
Test3 does not have ### with in span, P tag, to fix that use soup.select('p')

Or simply use for element in soup.find_all(['p']): for all p tag

Example

import re

from bs4 import BeautifulSoup

html = """
    <html lang="en">
        <head>
            <title></title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="created" content="2020-05-29T11:12:00.0000000" />
        </head>
        <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
            <div id="div:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{216}" style="position:absolute;left:72px;top:43px;width:696px">
                <p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{218}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Test1###yrdy</span></p>
                <p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{220}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test2###qweqwe</p>
                <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{22}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="color:red">Test3 </span> ###qweqeqwe</p>
                <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{17}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test4 ### sfsfsdfds</p>
                <p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{19}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test5### 121212</p>
            </div>
        </body>
    </html>
"""

soup = BeautifulSoup(html, features='html.parser')
for element in soup.select('p'):
    print(element)

Prints out all

<p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{218}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Test1###yrdy</span></p>
<p id="p:{659babcd-9de3-0e7a-27ba-7fa0325a40f7}{220}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test2###qweqwe</p>
<p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{22}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt"><span style="color:red">Test3 </span> ###qweqeqwe</p>
<p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{17}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test4 ### sfsfsdfds</p>
<p id="p:{d59b11dc-654f-0d5c-0ee2-f66181a6fa4b}{19}" lang="en-US" style="font-size:10.5pt;margin-top:0pt;margin-bottom:0pt">Test5### 121212</p>

Process finished with exit code 0