问:
How to retry after exception?
I have a loop starting with for i in range(0, 100). Normally it runs correctly, but sometimes it fails due to network conditions. Currently I have it set so that on failure, it will continue in the except clause (continue on to the next number for i).

Is it possible for me to reassign the same number to i and run through the failed iteration of the loop again?

答:
Alternatives to retrying: tenacity and backoff (2020 update)

The retrying library was previously the way to go, but sadly it has some bugs and it hasn't got any updates since 2016. Other alternatives seem to be backoff and tenacity. During the time of writing this, the tenacity had more GItHub stars (2.3k vs 1.2k) and was updated more recently, hence I chose to use it. Here is an example:

from functools import partial
import random # producing random errors for this example

from tenacity import retry, stop_after_delay, wait_fixed, retry_if_exception_type

# Custom error type for this example
class CommunicationError(Exception):
    pass

# Define shorthand decorator for the used settings.
retry_on_communication_error = partial(
    retry,
    stop=stop_after_delay(10),  # max. 10 seconds wait.
    wait=wait_fixed(0.4),  # wait 400ms
    retry=retry_if_exception_type(CommunicationError),
)()

@retry_on_communication_error
def do_something_unreliable(i):
    if random.randint(1, 5) == 3:
        print('Run#', i, 'Error occured. Retrying.')
        raise CommunicationError()

for i in range(100):
    do_something_unreliable(i)
```
<p>The above code outputs something like:</p>
```Run# 3 Error occured. Retrying.
Run# 5 Error occured. Retrying.
Run# 6 Error occured. Retrying.
Run# 6 Error occured. Retrying.
Run# 10 Error occured. Retrying.
.
.
.
```

More settings for the tenacity.retry are listed on the tenacity GitHub page.

Title: How to retry after exception?

Body:
Alternatives to retrying: tenacity and backoff (2020 update)

The retrying library was previously the way to go, but sadly it has some bugs and it hasn't got any updates since 2016. Other alternatives seem to be backoff and tenacity. During the time of writing this, the tenacity had more GItHub stars (2.3k vs 1.2k) and was updated more recently, hence I chose to use it. Here is an example:

from functools import partial
import random # producing random errors for this example

from tenacity import retry, stop_after_delay, wait_fixed, retry_if_exception_type

# Custom error type for this example
class CommunicationError(Exception):
    pass

# Define shorthand decorator for the used settings.
retry_on_communication_error = partial(
    retry,
    stop=stop_after_delay(10),  # max. 10 seconds wait.
    wait=wait_fixed(0.4),  # wait 400ms
    retry=retry_if_exception_type(CommunicationError),
)()

@retry_on_communication_error
def do_something_unreliable(i):
    if random.randint(1, 5) == 3:
        print('Run#', i, 'Error occured. Retrying.')
        raise CommunicationError()

for i in range(100):
    do_something_unreliable(i)

The above code outputs something like:

Run# 3 Error occured. Retrying.
Run# 5 Error occured. Retrying.
Run# 6 Error occured. Retrying.
Run# 6 Error occured. Retrying.
Run# 10 Error occured. Retrying.
.
.
.

More settings for the tenacity.retry are listed on the tenacity GitHub page.


来源: stackexchange

问:
How can I enter number digit by digit in field /selenium
So I have verification code that contains 7 fields and I have verification code itself. How can I enter this code by number in every field? I tried to do List, but my skills are not that good. </p>

答:
I suggest you to not use a list. For every field you should use another function. Because in time, devs will delete or rename or move.. any field and your test will failed because they do a simple action with just one of field. So I think you should use one function for each field.

typeDigital(String number){
driver.findElemeny(By.css("css"))).sendKeys(number));}

here you will set your number from parapeter. If you have more question about this write me.

ContentLicense: CC BY-SA 4.0


问:
Getting back old copy paste behaviour in tmux, with mouse
This is what I used to do in tmux to copy-paste (using the mouse, the keyboard works differently and it is not what I am interested about):

Select text with mouse, left-button pressed

Paste text with middle-button

I have upgraded my OS, and this has gotten a new tmux version. I have not changed my .tmux.conf config file.

This is what I have to do with the current version of tmux, 1.6 (which comes pre-packaged in the latest crunchbang linux):

Select text with mouse, left-button pressed and shift key

Paste text with middle-button

Terminal gets blocked, a litte information area shows some numbers on the top right of the current pane (i.e. [0/24], probably something related to how many characters have been pasted), which mean little to me and I do not need / want (edit: it seems copy-mode is entered automatically here)

I have to press the q key to get a functional terminal again.

This is too much hassle for something I do dozens of times a day. How to get the old mechanism working again?

答:
Based on the other answers posted here, I have created a concise solution that works with (at least) tmux 2.8 and tmux 3.1.

You can toggle tmux mouse support on and off, by pressing prefix-M. In the .tmux.conf file, include this line:

```<code>bind-key -T prefix m run "m=$(tmux show -g mou|grep -q on;echo $?);tmux set -g mou $m;tmux display mouse:\$m"
</code>```

This will display mouse: 1 when enabled and mouse: 0 when disabled.  Repeatedly pressing prefix-M will toggle the mouse mode between on and off.

See also: tmux mouse support

EDIT: If you see a command returned 1 error message then you will need to use the following instead for tmux 3.1 or tmux 3.2:

```<code>bind-key -T prefix m set -g mouse\; display 'Mouse: #{?mouse,ON,OFF}'
</code>```

(this solution was previously mentioned)

ContentLicense: CC BY-SA 3.0

Body:
Based on the other answers posted here, I have created a concise solution that works with (at least) tmux 2.8 and tmux 3.1.

You can toggle tmux mouse support on and off, by pressing prefix-M. In the .tmux.conf file, include this line:

bind-key -T prefix m run "m=$(tmux show -g mou|grep -q on;echo $?);tmux set -g mou $m;tmux display mouse:\$m"

This will display mouse: 1 when enabled and mouse: 0 when disabled.  Repeatedly pressing prefix-M will toggle the mouse mode between on and off.

See also: tmux mouse support

EDIT: If you see a command returned 1 error message then you will need to use the following instead for tmux 3.1 or tmux 3.2:

bind-key -T prefix m set -g mouse\; display 'Mouse: #{?mouse,ON,OFF}'

(this solution was previously mentioned)


问:
Oracle - get data from first day of previous month at particular time to first day of current month at particular time
My requirement is to get the data of a particular date range in Oracle as follows -

```<code>CompletedDate = Range between First Day of Previous Month @12:00AM to First Day of Current Month @4:00PM
</code>```

And also

```<code>CompleteDate = Range between previous Friday at 4:31 PM and current Friday at 4:30 PM
</code>```

Can you please give me a solution.

答:
You seem to want:</p>

```<code>where (CompleteDate >= add_months(trunc(sysdate, 'MON'), -1)  and
       CompleteDate < trunc(sysdate, 'MON')
      ) or
      (CompleteDate < next_day(trunc(sysdate), 'FRIDAY') + interval '14' hour + interval '30' minute and
       CompleteDate > next_day(trunc(sysdate), 'FRIDAY') - interval '7' day + interval '14' hour + interval '30' minute
      )
</code>```

Body:
My requirement is to get the data of a particular date range in Oracle as follows -

CompletedDate = Range between First Day of Previous Month @12:00AM to First Day of Current Month @4:00PM

And also

CompleteDate = Range between previous Friday at 4:31 PM and current Friday at 4:30 PM

Can you please give me a solution.

Body:
You seem to want:

where (CompleteDate >= add_months(trunc(sysdate, 'MON'), -1)  and
       CompleteDate < trunc(sysdate, 'MON')
      ) or
      (CompleteDate < next_day(trunc(sysdate), 'FRIDAY') + interval '14' hour + interval '30' minute and
       CompleteDate > next_day(trunc(sysdate), 'FRIDAY') - interval '7' day + interval '14' hour + interval '30' minute
      )


问:
Navigate a URL with Query string values on click of hyperlink
I have a control with 'email' and 'password' textboxes and a 'autoLogin' checkbox. All are web form controls (not html controls). And there is a 'Login' hyperlink. When I click on heperlink, I want to move to other page using NavigateUrl property like below:

```<code>NavigateUrl="~/DoLogin.aspx?email={0}&pwd={1}&autoLogin={3}"
</code>```

how to pass and how to get the query string?

Thanks in Advance...

答:
On Login Click write this code</p>

```<code>Response.Redirect("~/DoLogin.aspx?email=" + txtEmail.txt + "&pwd=" + txtPwd.text)
</code>```

Body:
I have a control with 'email' and 'password' textboxes and a 'autoLogin' checkbox. All are web form controls (not html controls). And there is a 'Login' hyperlink. When I click on heperlink, I want to move to other page using NavigateUrl property like below:

NavigateUrl="~/DoLogin.aspx?email={0}&pwd={1}&autoLogin={3}"

how to pass and how to get the query string?

Thanks in Advance...

Body:
On Login Click write this code

Response.Redirect("~/DoLogin.aspx?email=" + txtEmail.txt + "&pwd=" + txtPwd.text)


问:
Desktop app opening a server-side Blazor page protected by AD auth
I have a server-side Blazor app running on Azure AppService protected by AAD auth. Authorized AD users (admins) are able to access and use the app.

What I'm want to achieve is to allow other users to access only a specific page of the Blazor app. Users would access the app from WPF app, themselves not necessarily being AD users, but AD application user (daemon app). WPF app gets the token (v2.0) and opens the page in a browser with the Authorization header (bearer token), however Blazor doesn't not interpret the auth header in request and threats the user as unauthenticated.
App registrations should have been setup properly as I'm able to access API when using this approach.

It this scenario possible? If so, any ideas what I could be doing wrong?

答:
This scenario sounds like it is going quite a lot against general recommendations.

You really should not be doing "daemon app" authentication from a WPF app as it is not a confidential client application (and neither a daemon app). It runs on a user device, exposing the application's credential to any user.
Authentication flows that use secrets should never be used from a user device.

Secondly, a Blazor application is not generally one that can accept requests from an application.
Your WPF application should be calling an API, not a UI application.

What you should instead have is:

WPF app authenticates the user and acquires a token on their behalf to your API

WPF app calls the API with the access token

API validates the access token, authorizes access for the user and app, returns data

The Blazor app could be using this same API in its front-end code in a similar manner.
But I don't think you are going to be able to put it in the middle of your client app and the API.


问:
NodeJS: how to access TextRow?
I am using sequalize to fetch data with node and angular but I am getting the below result.

result:

```<code>count---- [ TextRow { totalCount: 39, total_rating: 0, number_of_review: null } ]
</code>```

Now I want to return totalCount from above result. I have tried everything but not able to access it.

答:
Please try

```<code>output[0].totalCount
</code>```

I am considering output as your property name. in console it is displayed as array. Thats why i am getting index 0 then totalCount property of object at index 0.

Title: NodeJS: how to access TextRow?

Body:
I am using sequalize to fetch data with node and angular but I am getting the below result.

result:

count---- [ TextRow { totalCount: 39, total_rating: 0, number_of_review: null } ]

Now I want to return totalCount from above result. I have tried everything but not able to access it.

Body:
Please try

output[0].totalCount

I am considering output as your property name. in console it is displayed as array. Thats why i am getting index 0 then totalCount property of object at index 0.


问:
Credit card validation check in C programming
This a problem from CS50 finding validation of credit card number using luhn's algorithm. I wrote this code but whenever I put any credit card number, the output comes as invalid.Can anyone please help me what my mistake here and how can I move forward with this code.

This is luhn's algorithm formula--
most cards use an algorithm invented by Hans Peter Luhn of IBM. According to Luhn’s algorithm, you can determine if a credit card number is (syntactically) valid as follows:

Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.
Add the sum to the sum of the digits that weren’t multiplied by 2.
If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!

```<code>//Project Using Luhn's Algorithm - Credit Card validation

#include<cs50.h>
#include<stdio.h>
#include<string.h>
int main()
{
   long long ccnum = 0;

    int digitcounter = 0 ;
    char result[11] ;
    int sum = 0 ;
    int  divisor = 10 ;

    do
    {
      ccnum = get_long("Enter your credit card number: ") ;
    }
    while (ccnum <= 0) ;

//--------------------CHECKSUM--------------------------------

    //Case-01(Multiply every other digit by 2):

    long long tempccnum = ccnum ;
    while (tempccnum > 0)
    {
        int lastdigit = tempccnum % 10 ;
        sum = sum + lastdigit ;
        tempccnum = tempccnum / 100 ;
    }

    // Case-02(Multiplication every other digit by 2):

    tempccnum = ccnum / 10 ;
    while(tempccnum > 0)
    {
        int secondlastdigit = (tempccnum % 10) ;

        int tempmultiply = secondlastdigit*2 ;

        sum = sum + (tempmultiply % 10) + (tempmultiply / 10)  ;

        tempccnum = tempccnum / 100 ;
    }
//----------first two digit extraction------------

   long long initdigit = 0, tempinitdigit = 0 ;

   while(ccnum)
   {
     initdigit = tempinitdigit  ; // initdigit will hold the first two digits.
     tempinitdigit = ccnum  ;
     ccnum = ccnum/10 ;
   }

//-------------Digit counter----------
    while(ccnum != 0)
    {
          // iterate until ccnum becomes 0
         // remove last digit from ccnum in each iteration
        // increase digitcounter by 1 in each iteration

        ccnum = ccnum / 10 ;
        digitcounter++ ;
    }
  //------------------Final part - Condition checking------------------------

    if(sum % 10 == 0)
    {
        if (digitcounter == 15 && (initdigit == 34 || initdigit == 37))
        {
            strcpy(result, "AMEX\n") ;
        }

        else if (digitcounter == 16 && (initdigit >= 51 && initdigit <=55))
        {
             strcpy(result,"MASTERCARD\n") ;
        }

        else if ((digitcounter == 13 || digitcounter == 16) && tempinitdigit == 4)
        {
             strcpy(result,"VISA\n") ;
        }

        else
        {
             strcpy(result,"INVALID\n") ;
        }

    }

    else
    {
        strcpy(result,"INVALID\n") ;
    }
    printf("%s\n", result) ;
}
</code>```

答:
in the first loop, you state this:

```<code>    while(ccnum != 0)
    {
          // iterate until ccnum becomes 0
         // remove last digit from ccnum in each iteration
        // increase digitcounter by 1 in each iteration

        ccnum = ccnum / 10 ;
        digitcounter++ ;
    }
</code>```

(you say, literally: iterate until ccnum becomes 0)
If you remove all digits of ccnum and get 0, how are you going to process all those digits?  The second, and next loops you have in your code receive 0 as the value of ccnum (you have been shotting at it until it died, remember :) )

Body:
This a problem from CS50 finding validation of credit card number using luhn's algorithm. I wrote this code but whenever I put any credit card number, the output comes as invalid.Can anyone please help me what my mistake here and how can I move forward with this code.

This is luhn's algorithm formula--
most cards use an algorithm invented by Hans Peter Luhn of IBM. According to Luhn’s algorithm, you can determine if a credit card number is (syntactically) valid as follows:

Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.
Add the sum to the sum of the digits that weren’t multiplied by 2.
If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!

//Project Using Luhn's Algorithm - Credit Card validation

#include<cs50.h>
#include<stdio.h>
#include<string.h>
int main()
{
   long long ccnum = 0;

    int digitcounter = 0 ;
    char result[11] ;
    int sum = 0 ;
    int  divisor = 10 ;

    do
    {
      ccnum = get_long("Enter your credit card number: ") ;
    }
    while (ccnum <= 0) ;

//--------------------CHECKSUM--------------------------------

    //Case-01(Multiply every other digit by 2):

    long long tempccnum = ccnum ;
    while (tempccnum > 0)
    {
        int lastdigit = tempccnum % 10 ;
        sum = sum + lastdigit ;
        tempccnum = tempccnum / 100 ;
    }

    // Case-02(Multiplication every other digit by 2):

    tempccnum = ccnum / 10 ;
    while(tempccnum > 0)
    {
        int secondlastdigit = (tempccnum % 10) ;

        int tempmultiply = secondlastdigit*2 ;

        sum = sum + (tempmultiply % 10) + (tempmultiply / 10)  ;

        tempccnum = tempccnum / 100 ;
    }
//----------first two digit extraction------------

   long long initdigit = 0, tempinitdigit = 0 ;

   while(ccnum)
   {
     initdigit = tempinitdigit  ; // initdigit will hold the first two digits.
     tempinitdigit = ccnum  ;
     ccnum = ccnum/10 ;
   }

//-------------Digit counter----------
    while(ccnum != 0)
    {
          // iterate until ccnum becomes 0
         // remove last digit from ccnum in each iteration
        // increase digitcounter by 1 in each iteration

        ccnum = ccnum / 10 ;
        digitcounter++ ;
    }
  //------------------Final part - Condition checking------------------------

    if(sum % 10 == 0)
    {
        if (digitcounter == 15 && (initdigit == 34 || initdigit == 37))
        {
            strcpy(result, "AMEX\n") ;
        }

        else if (digitcounter == 16 && (initdigit >= 51 && initdigit <=55))
        {
             strcpy(result,"MASTERCARD\n") ;
        }

        else if ((digitcounter == 13 || digitcounter == 16) && tempinitdigit == 4)
        {
             strcpy(result,"VISA\n") ;
        }

        else
        {
             strcpy(result,"INVALID\n") ;
        }

    }

    else
    {
        strcpy(result,"INVALID\n") ;
    }
    printf("%s\n", result) ;
}

Body:
in the first loop, you state this:

    while(ccnum != 0)
    {
          // iterate until ccnum becomes 0
         // remove last digit from ccnum in each iteration
        // increase digitcounter by 1 in each iteration

        ccnum = ccnum / 10 ;
        digitcounter++ ;
    }

(you say, literally: iterate until ccnum becomes 0)
If you remove all digits of ccnum and get 0, how are you going to process all those digits?  The second, and next loops you have in your code receive 0 as the value of ccnum (you have been shotting at it until it died, remember :) )


问:
What is the difference between these two C++ statements to concatenate a string?
I'm new to C++ and I was wondering what the difference is between these two lines:

```<code>cout << "John " << "Doe" << endl;
cout << "John " + "Doe" << endl;
</code>```

The first one works but the second one doesn't. Any ideas?

答:
For starters there is no concatenation of strings.

In the first statement

```<code>cout << "John " << "Doe" << endl;
</code>```

there are outputted sequentially two strings.

In the second statement

```<code>cout << "John " + "Doe" << endl;
</code>```

there is an attempt to perform the binary operation + with two pointers of the type const char * to which the used string literals having array types are implicitly converted. However the operator + is not defined for pointers.

You may use the operator - for pointers like

```<code>std::cout << "John " - "Doe" << std::endl;
</code>```

This statement syntactically correct but semantically has undefined behavior because the pointers do not point to elements of the same array.

Built-in array types do not have the binary operator +.

This operator is overloaded for the user-defined class template std::basic_string.

You could write for example

```<code>#include <iostream>
#include <string>

int main()
{
    std::cout << std::string( "John " ) + "Doe" << std::endl;

    return 0;
}
</code>```

Or you could use a user-defined string literal like

```<code>#include <iostream>
#include <string>

int main()
{
    using namespace std::string_literals;
    std::cout << "John "s + "Doe" << std::endl;

    return 0;
}
</code>```

Or in the C++ 17 Standard there is defined new class template std::string_view that you also could use to concatenate strings.

Body:
I'm new to C++ and I was wondering what the difference is between these two lines:

cout << "John " << "Doe" << endl;
cout << "John " + "Doe" << endl;

The first one works but the second one doesn't. Any ideas?

Body:
For starters there is no concatenation of strings.

In the first statement

cout << "John " << "Doe" << endl;

there are outputted sequentially two strings.

In the second statement

cout << "John " + "Doe" << endl;

there is an attempt to perform the binary operation + with two pointers of the type const char * to which the used string literals having array types are implicitly converted. However the operator + is not defined for pointers.

You may use the operator - for pointers like

std::cout << "John " - "Doe" << std::endl;

This statement syntactically correct but semantically has undefined behavior because the pointers do not point to elements of the same array.

Built-in array types do not have the binary operator +.

This operator is overloaded for the user-defined class template std::basic_string.

You could write for example

#include <iostream>
#include <string>

int main()
{
    std::cout << std::string( "John " ) + "Doe" << std::endl;

    return 0;
}

Or you could use a user-defined string literal like

#include <iostream>
#include <string>

int main()
{
    using namespace std::string_literals;
    std::cout << "John "s + "Doe" << std::endl;

    return 0;
}

Or in the C++ 17 Standard there is defined new class template std::string_view that you also could use to concatenate strings.


问:
Smooth FPS change via FFMPEG
I have a 240FPS video from GoPro and I want to slow down it to 30FPS linearly. For this I'm using:

```<code>ffmpeg -i raw.mp4 -filter_complex "[v:0]setpts='lerp(2,8,T/5)*PTS" -r 30 output.mp4
</code>```

* 5 is video duration in seconds

The problem is that the resulting video stutters pretty heavily. But if I use a fixed setpts, like setpts=8*PTS, then everything is fine.

How to make smooth FPS transition via FFMPEG?

答:
For video,

```<code>ffmpeg -i raw.mp4 -filter:v "setpts='if(between(T,0,1),PTS,(((T-1)*(1+7*T/4+1-7/4)/2+1)/T)*PTS)'" -an output.mp4
</code>```

should do that, with the exception of the first second being at a normal frame rate.

Conceptually, I found it easiest to do this graphically. Please see the image, where x axis is time and y axis is PTS.

Where

I am using the setpts filter.

The PTS (presentation timestamp) tells when to show the frame, so PTS*1 means to show it at the same time in the output as in the input, where PTS*0.5 tells to show it at 50% of input time etc.

So

```<code>setpts='if(between(T,0,1),PTS
</code>```

means to keep the framerate same for the first second (this mostly due to the fact that one cannot divide with a zero). After which, the Area A1 is 1*1 = 1.

At t1=1s we start to decrease the framerate by telling each frame to come at a later time than in the input. The area A2 is a trapezoid where the area is calculated by the average of the sides times the base. Base is T - 1 and average of sides is ( 1 + ( 7 * T/4 + 1 - 7/4 ) ) / 2 (from creating a function for the line). To get the PTS of any given frame between 1 and 5 seconds, we must add the area of all (one in this case) previous areas and divide by the area of PTS*1, which equals T.

Thus from 1s to 5s, the PTS is calculated by

```<code>((T-1)*(1+7*T/4+1-7/4)/2+1)/T)*PTS
</code>```

Unfortunately using the same method for audio in ffmpeg drops a lot of data and results in badly breaking sound (for speed-up at least). As I can accurately calculate the duration of the video (it is the same as the areas A1+A2), I ended up using audacity to try and fit each variable rate portion manually.

Title: Smooth FPS change via FFMPEG

Body:
I have a 240FPS video from GoPro and I want to slow down it to 30FPS linearly. For this I'm using:

ffmpeg -i raw.mp4 -filter_complex "[v:0]setpts='lerp(2,8,T/5)*PTS" -r 30 output.mp4

* 5 is video duration in seconds

The problem is that the resulting video stutters pretty heavily. But if I use a fixed setpts, like setpts=8*PTS, then everything is fine.

How to make smooth FPS transition via FFMPEG?

Body:
For video,

ffmpeg -i raw.mp4 -filter:v "setpts='if(between(T,0,1),PTS,(((T-1)*(1+7*T/4+1-7/4)/2+1)/T)*PTS)'" -an output.mp4

should do that, with the exception of the first second being at a normal frame rate.

Conceptually, I found it easiest to do this graphically. Please see the image, where x axis is time and y axis is PTS.

Where

I am using the setpts filter.

The PTS (presentation timestamp) tells when to show the frame, so PTS*1 means to show it at the same time in the output as in the input, where PTS*0.5 tells to show it at 50% of input time etc.

So

setpts='if(between(T,0,1),PTS

means to keep the framerate same for the first second (this mostly due to the fact that one cannot divide with a zero). After which, the Area A1 is 1*1 = 1.

At t1=1s we start to decrease the framerate by telling each frame to come at a later time than in the input. The area A2 is a trapezoid where the area is calculated by the average of the sides times the base. Base is T - 1 and average of sides is ( 1 + ( 7 * T/4 + 1 - 7/4 ) ) / 2 (from creating a function for the line). To get the PTS of any given frame between 1 and 5 seconds, we must add the area of all (one in this case) previous areas and divide by the area of PTS*1, which equals T.

Thus from 1s to 5s, the PTS is calculated by

((T-1)*(1+7*T/4+1-7/4)/2+1)/T)*PTS

Unfortunately using the same method for audio in ffmpeg drops a lot of data and results in badly breaking sound (for speed-up at least). As I can accurately calculate the duration of the video (it is the same as the areas A1+A2), I ended up using audacity to try and fit each variable rate portion manually.


问:
Can I create a function that compares numbers of a column to a mean for different columns?
For 6 different columns, I have to change all values to 'high'/'low' depending on whether the values are bigger or smaller than the mean of the column. I wrote a function that checks whether the values in a column are smaller/bigger than the mean, but it is only applicable to one of the columns:

```<code>calc.binary <- function(number){
  number[number > mean(modality$auditory)] <- 'high'
  number[!(number %in% 'high')] <- 'low'
  number
}

modality_bin <- mutate_at(modality, vars(auditory:visual), list(calc.binary))
</code>```

I want to apply this function to all 6 columns at once now (columns auditory to visual), but I know I'd have to change the calc.binary function for that. How do I change my function so that it takes the mean of each column and compares the number in that column to that mean? So I can apply the same function to all columns.

答:
A solution with lapply() looks like this, using the mtcars data set as an example. We use a numeric vector to process the columns of mtcars, and add a reference to mtcars as a second argument to lapply(). We create the comparisons, rename the columns, and merge with the original data.

```<code>meansCompares <- lapply(1:ncol(mtcars),function(x,y){
     ifelse(y[x] >= mean(y[[x]]),"High","Low")
},mtcars)
compares <- do.call(cbind,meansCompares)
# rename cols so we can join with original data
colnames(compares) <- paste0("comp_",colnames(compares))
head(cbind(mtcars,compares))
</code>```

...and the output:

```<code>> head(cbind(mtcars,compares))
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb comp_mpg comp_cyl comp_disp comp_hp comp_drat
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4     High      Low       Low     Low      High
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4     High      Low       Low     Low      High
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1     High      Low       Low     Low      High
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1     High      Low      High     Low       Low
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2      Low     High      High    High       Low
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1      Low      Low       Low     Low       Low
                  comp_wt comp_qsec comp_vs comp_am comp_gear comp_carb
Mazda RX4             Low       Low     Low    High      High      High
Mazda RX4 Wag         Low       Low     Low    High      High      High
Datsun 710            Low      High    High    High      High       Low
Hornet 4 Drive        Low      High    High     Low       Low       Low
Hornet Sportabout    High       Low     Low     Low       Low       Low
Valiant              High      High    High     Low       Low       Low
</code>```

Body:
For 6 different columns, I have to change all values to 'high'/'low' depending on whether the values are bigger or smaller than the mean of the column. I wrote a function that checks whether the values in a column are smaller/bigger than the mean, but it is only applicable to one of the columns:

calc.binary <- function(number){
  number[number > mean(modality$auditory)] <- 'high'
  number[!(number %in% 'high')] <- 'low'
  number
}

modality_bin <- mutate_at(modality, vars(auditory:visual), list(calc.binary))

I want to apply this function to all 6 columns at once now (columns auditory to visual), but I know I'd have to change the calc.binary function for that. How do I change my function so that it takes the mean of each column and compares the number in that column to that mean? So I can apply the same function to all columns.

Body:
A solution with lapply() looks like this, using the mtcars data set as an example. We use a numeric vector to process the columns of mtcars, and add a reference to mtcars as a second argument to lapply(). We create the comparisons, rename the columns, and merge with the original data.

meansCompares <- lapply(1:ncol(mtcars),function(x,y){
     ifelse(y[x] >= mean(y[[x]]),"High","Low")
},mtcars)
compares <- do.call(cbind,meansCompares)
# rename cols so we can join with original data
colnames(compares) <- paste0("comp_",colnames(compares))
head(cbind(mtcars,compares))

...and the output:

> head(cbind(mtcars,compares))
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb comp_mpg comp_cyl comp_disp comp_hp comp_drat
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4     High      Low       Low     Low      High
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4     High      Low       Low     Low      High
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1     High      Low       Low     Low      High
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1     High      Low      High     Low       Low
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2      Low     High      High    High       Low
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1      Low      Low       Low     Low       Low
                  comp_wt comp_qsec comp_vs comp_am comp_gear comp_carb
Mazda RX4             Low       Low     Low    High      High      High
Mazda RX4 Wag         Low       Low     Low    High      High      High
Datsun 710            Low      High    High    High      High       Low
Hornet 4 Drive        Low      High    High     Low       Low       Low
Hornet Sportabout    High       Low     Low     Low       Low       Low
Valiant              High      High    High     Low       Low       Low


问:
Heroku rails console automatically exits
I'm running a simple rails active query command on 300,000 records.

Why does the heroku console automatically exit?

```<code>def is_number? string
  true if Float(string) rescue false
end
documents = []
DocumentText.all.each do |t|
  if is_number?(t.src)
    documents << t
  end
end
</code>```

答:
best would be to do this in batches, so you won't load all records at once into memory

https://api.rubyonrails.org/classes/ActiveRecord/Batches.html#method-i-find_each

```<code>DocumentText.find_each do |t|
  next unless !!Float(t.src) rescue false
  documents << t
end; nil
</code>```

nil at the end won't print result into the console, you can also use a number 0

Body:
I'm running a simple rails active query command on 300,000 records.

Why does the heroku console automatically exit?

def is_number? string
  true if Float(string) rescue false
end
documents = []
DocumentText.all.each do |t|
  if is_number?(t.src)
    documents << t
  end
end

Body:
best would be to do this in batches, so you won't load all records at once into memory

https://api.rubyonrails.org/classes/ActiveRecord/Batches.html#method-i-find_each

DocumentText.find_each do |t|
  next unless !!Float(t.src) rescue false
  documents << t
end; nil

nil at the end won't print result into the console, you can also use a number 0


问:
Proper way to create JSON data with PHP/MySQL
Updated based on answers below:

Based on the answers below, I now have the following PHP script:

```<code>header('Content-type:application/json');

function getdata($the_query)
{
    $connection = mysql_connect('server', 'user', 'pass') or die (mysql_error());
    $db = mysql_select_db('db_name', $connection) or die (mysql_error());

    $results = mysql_query($the_query) or die(mysql_error());

    header('Content-type:application/json');

    $the_data['rss']['channels']['title'] = $title;
    $the_data['rss']['channels']['link'] = $link;
    $the_data['rss']['channels']['description'] = $description;

    while($row = mysql_fetch_array($result))
    {
        extract($row);

        $the_data['rss']['channels']['items']['title'] = $item_title;
        $the_data['rss']['channels']['items']['link'] = "$item_link;
        $the_data['rss']['channels']['items']['date'] = $item_date;
        $the_data['rss']['channels']['items']['description'] = $item_description;
    }

    mysql_close($connection);

    return json_encode($the_data);
}
</code>```

Which returns the following:

```<code>{
    "rss":
    {
        "channels":
        {
            "title":"title goes here",
            "link":"link goes here",
            "description":"description goes here",
            "items":
            {
                "title":"'title goes here",
                "link":"link goes here",
                "date":"date goes here",
                "description":"description goes here"
            }
        }
    }
}
</code>```

It should be returning many items based on the number of rows returned from the database, why am I only getting 1 items?

答:
try this one:</p>

```<code><?php
$channel = array(
     'title' => 'title goes here',
     'link' => 'link here',
     'description' => 'description',
     'items' => array()
);
while($row = mysql_fetch_array($results))
{
    extract($row);
    $channel['items'][] = array(
        'title' => $title,
        'link' => $link,
        'guid' => $guid,
        'pubDate' => $date,
        'description' => $description
    );
}
$channels = array($channel);
$rss = (object) array('rss'=> array('channels'=>$channels));
$json = json_encode($rss);
echo $json;

?>
</code>```

Body:
Updated based on answers below:

Based on the answers below, I now have the following PHP script:

header('Content-type:application/json');

function getdata($the_query)
{
    $connection = mysql_connect('server', 'user', 'pass') or die (mysql_error());
    $db = mysql_select_db('db_name', $connection) or die (mysql_error());

    $results = mysql_query($the_query) or die(mysql_error());

    header('Content-type:application/json');

    $the_data['rss']['channels']['title'] = $title;
    $the_data['rss']['channels']['link'] = $link;
    $the_data['rss']['channels']['description'] = $description;

    while($row = mysql_fetch_array($result))
    {
        extract($row);

        $the_data['rss']['channels']['items']['title'] = $item_title;
        $the_data['rss']['channels']['items']['link'] = "$item_link;
        $the_data['rss']['channels']['items']['date'] = $item_date;
        $the_data['rss']['channels']['items']['description'] = $item_description;
    }

    mysql_close($connection);

    return json_encode($the_data);
}

Which returns the following:

{
    "rss":
    {
        "channels":
        {
            "title":"title goes here",
            "link":"link goes here",
            "description":"description goes here",
            "items":
            {
                "title":"'title goes here",
                "link":"link goes here",
                "date":"date goes here",
                "description":"description goes here"
            }
        }
    }
}

It should be returning many items based on the number of rows returned from the database, why am I only getting 1 items?

Body:
try this one:

<?php
$channel = array(
     'title' => 'title goes here',
     'link' => 'link here',
     'description' => 'description',
     'items' => array()
);
while($row = mysql_fetch_array($results))
{
    extract($row);
    $channel['items'][] = array(
        'title' => $title,
        'link' => $link,
        'guid' => $guid,
        'pubDate' => $date,
        'description' => $description
    );
}
$channels = array($channel);
$rss = (object) array('rss'=> array('channels'=>$channels));
$json = json_encode($rss);
echo $json;

?>


问:
Composer failed to open stream: Permission denied
am trying to install illuminate/html package in Laravel 5.1 using command composer require illuminate/html . But I get this error.

```<code>[ErrorException]
file_put_contents(C:\ProgramData\ComposerSetup\bin):
failed to open stream:  Permission denied

require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-update]
[--update-no-dev] [--update-with-dependencies] [--ignore-platform-reqs]
[--sort-packages] [packages1] ... [packagesN]
</code>```

I believe Problem is permission to access that folder, I tried to remove read only protection but the protection return by default.

Am running Windows10

答:
I had this problem too. I put this solution here in case it works for someone else.

On my system (Windows 10), I had marked both the vendor folder and the composer.json file in my project folder as hidden, because of reasons. The error message went away when I unhid the folders and the json files, and ran the command again. I did not need to restart my command line in Administrator mode. (In fact, trying this in Administrator mode did not work either, until I changed the file and folder properties to unhidden.)

Body:
am trying to install illuminate/html package in Laravel 5.1 using command composer require illuminate/html . But I get this error.

[ErrorException]
file_put_contents(C:\ProgramData\ComposerSetup\bin):
failed to open stream:  Permission denied

require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-update]
[--update-no-dev] [--update-with-dependencies] [--ignore-platform-reqs]
[--sort-packages] [packages1] ... [packagesN]

I believe Problem is permission to access that folder, I tried to remove read only protection but the protection return by default.

Am running Windows10


问:
How to filter rows where key is not present in a large dataframe
Suppose I have a streaming dataframe A and a large static dataframe B. Assume that typically A is of size < 10000 records. However, B is a much larger dataframe with size in the range of millions.

Lets assume both A and B have a 'key' column. I want to filter rows in A where A.key is not present in B. What is the best way to achieve this.

Right now, I have tried A.join(B, Seq("key"), "left_anti"). However, the performance is not upto the mark. Is there anyway I can fasten up the process

Physical plan:

```<code>== Physical Plan ==
SortMergeJoin [domainName#461], [domain#147], LeftAnti
:- *(5) Sort [domainName#461 ASC NULLS FIRST], false, 0
:  +- StreamingDeduplicate [domainName#461], state info [ checkpoint = hdfs://MTPrime-CO4-fed/MTPrime-CO4-0/projects/BingAdsAdQuality/Test/WhoIs/WhoIsStream/checkPoint/state, runId = 9d09398b-efda-41cb-ab77-1b5550cd5da9, opId = 0, ver = 63, numPartitions = 400], 0
:     +- Exchange hashpartitioning(domainName#461, 400)
:        +- Union
:           :- *(2) Project [value#460 AS domainName#461]
:           :  +- *(2) Filter isnotnull(value#460)
:           :     +- *(2) SerializeFromObject [staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, input[0, java.lang.String, true], true, false) AS value#460]
:           :        +- MapPartitions <function1>, obj#459: java.lang.String
:           :           +- MapPartitions <function1>, obj#436: MTInterfaces.Fraud.RiskEntity
:           :              +- DeserializeToObject newInstance(class scala.Tuple3), obj#435: scala.Tuple3
:           :                 +- Exchange RoundRobinPartitioning(600)
:           :                    +- *(1) SerializeFromObject [staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, assertnotnull(input[0, scala.Tuple3, true])._1, true, false) AS _1#142, staticinvoke(class org.apache.spark.sql.catalyst.util.DateTimeUtils$, TimestampType, fromJavaTimestamp, assertnotnull(input[0, scala.Tuple3, true])._2, true, false) AS _2#143, staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, assertnotnull(input[0, scala.Tuple3, true])._3, true, false) AS _3#144]
:           :                       +- *(1) MapElements <function1>, obj#141: scala.Tuple3
:           :                          +- *(1) MapElements <function1>, obj#132: scala.Tuple3
:           :                             +- *(1) DeserializeToObject createexternalrow(Body#60.toString, staticinvoke(class org.apache.spark.sql.catalyst.util.DateTimeUtils$, ObjectType(class java.sql.Timestamp), toJavaTimestamp, EventTime#37, true, false), Timestamp#48L, Offset#27L, Partition#72.toString, PartitionKey#84.toString, Publisher#96.toString, SequenceNumber#108L, StructField(Body,StringType,true), StructField(EventTime,TimestampType,true), StructField(Timestamp,LongType,true), StructField(Offset,LongType,true), StructField(Partition,StringType,true), StructField(PartitionKey,StringType,true), StructField(Publisher,StringType,true), StructField(SequenceNumber,LongType,true)), obj#131: org.apache.spark.sql.Row
:           :                                +- *(1) Project [cast(body#608 as string) AS Body#60, enqueuedTime#612 AS EventTime#37, cast(enqueuedTime#612 as bigint) AS Timestamp#48L, cast(offset#610 as bigint) AS Offset#27L, partition#609 AS Partition#72, partitionKey#614 AS PartitionKey#84, publisher#613 AS Publisher#96, sequenceNumber#611L AS SequenceNumber#108L]
:           :                                   +- Scan ExistingRDD[body#608,partition#609,offset#610,sequenceNumber#611L,enqueuedTime#612,publisher#613,partitionKey#614,properties#615,systemProperties#616]
:           +- *(4) Project [value#453 AS domainName#455]
:              +- *(4) Filter isnotnull(value#453)
:                 +- *(4) SerializeFromObject [staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, input[0, java.lang.String, true], true, false) AS value#453]
:                    +- *(4) MapElements <function1>, obj#452: java.lang.String
:                       +- MapPartitions <function1>, obj#436: MTInterfaces.Fraud.RiskEntity
:                          +- DeserializeToObject newInstance(class scala.Tuple3), obj#435: scala.Tuple3
:                             +- ReusedExchange [_1#142, _2#143, _3#144], Exchange RoundRobinPartitioning(600)
+- *(8) Project [domain#147]
   +- *(8) Filter (isnotnull(rank#284) && (rank#284 = 1))
      +- Window [row_number() windowspecdefinition(domain#147, timestamp#151 DESC NULLS LAST, specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS rank#284], [domain#147], [timestamp#151 DESC NULLS LAST]
         +- *(7) Sort [domain#147 ASC NULLS FIRST, timestamp#151 DESC NULLS LAST], false, 0
            +- Exchange hashpartitioning(domain#147, 400)
               +- *(6) Project [domain#147, timestamp#151]
                  +- *(6) Filter isnotnull(domain#147)
                     +- *(6) FileScan csv [domain#147,timestamp#151] Batched: false, Format: CSV, Location: InMemoryFileIndex[hdfs://MTPrime-CO4-fed/MTPrime-CO4-0/projects/BingAdsAdQuality/Test/WhoIs], PartitionFilters: [], PushedFilters: [IsNotNull(domain)], ReadSchema: struct<domain:string,timestamp:string>
</code>```

Snapshots of query graph:

EDIT

 Right now I have moved the lookup data to a Cosmos DB store and created a TempView on top of it (say lookupdata). Now, I need to filter the ones that are not present in the store. I am exploring the following options:

1. create tempview on top of the streaming data as well and query

spark.sql(SELECT * FROM streamingdata s LEFT ANTI JOIN lookupdata l ON s.key = l.key")

Same as 1 but do inner sub-query instead of left anti join. i.e  spark.sql("SELECT s.* FROM streamingdata s WHERE s.key NOT IN (SELECT key FROM lookupdata l)")

Retain the streaming df as it is and do a filter op:

```<code>df.filter(x => { val key = x.getAs[String])("key")
  spark.sql("SELECT * FROM lookupdata l WHERE l.key = '"+key+"'").isEmpty
})
</code>```

which one would work better?

答:
Please try</p>

```<code>from pyspark.sql.functions import broadcast
A.join(broadcast(B), Seq("key"), "left_anti")
</code>```

Body:
Suppose I have a streaming dataframe A and a large static dataframe B. Assume that typically A is of size < 10000 records. However, B is a much larger dataframe with size in the range of millions.

Lets assume both A and B have a 'key' column. I want to filter rows in A where A.key is not present in B. What is the best way to achieve this.

Right now, I have tried A.join(B, Seq("key"), "left_anti"). However, the performance is not upto the mark. Is there anyway I can fasten up the process

Physical plan:

== Physical Plan ==
SortMergeJoin [domainName#461], [domain#147], LeftAnti
:- *(5) Sort [domainName#461 ASC NULLS FIRST], false, 0
:  +- StreamingDeduplicate [domainName#461], state info [ checkpoint = hdfs://MTPrime-CO4-fed/MTPrime-CO4-0/projects/BingAdsAdQuality/Test/WhoIs/WhoIsStream/checkPoint/state, runId = 9d09398b-efda-41cb-ab77-1b5550cd5da9, opId = 0, ver = 63, numPartitions = 400], 0
:     +- Exchange hashpartitioning(domainName#461, 400)
:        +- Union
:           :- *(2) Project [value#460 AS domainName#461]
:           :  +- *(2) Filter isnotnull(value#460)
:           :     +- *(2) SerializeFromObject [staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, input[0, java.lang.String, true], true, false) AS value#460]
:           :        +- MapPartitions <function1>, obj#459: java.lang.String
:           :           +- MapPartitions <function1>, obj#436: MTInterfaces.Fraud.RiskEntity
:           :              +- DeserializeToObject newInstance(class scala.Tuple3), obj#435: scala.Tuple3
:           :                 +- Exchange RoundRobinPartitioning(600)
:           :                    +- *(1) SerializeFromObject [staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, assertnotnull(input[0, scala.Tuple3, true])._1, true, false) AS _1#142, staticinvoke(class org.apache.spark.sql.catalyst.util.DateTimeUtils$, TimestampType, fromJavaTimestamp, assertnotnull(input[0, scala.Tuple3, true])._2, true, false) AS _2#143, staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, assertnotnull(input[0, scala.Tuple3, true])._3, true, false) AS _3#144]
:           :                       +- *(1) MapElements <function1>, obj#141: scala.Tuple3
:           :                          +- *(1) MapElements <function1>, obj#132: scala.Tuple3
:           :                             +- *(1) DeserializeToObject createexternalrow(Body#60.toString, staticinvoke(class org.apache.spark.sql.catalyst.util.DateTimeUtils$, ObjectType(class java.sql.Timestamp), toJavaTimestamp, EventTime#37, true, false), Timestamp#48L, Offset#27L, Partition#72.toString, PartitionKey#84.toString, Publisher#96.toString, SequenceNumber#108L, StructField(Body,StringType,true), StructField(EventTime,TimestampType,true), StructField(Timestamp,LongType,true), StructField(Offset,LongType,true), StructField(Partition,StringType,true), StructField(PartitionKey,StringType,true), StructField(Publisher,StringType,true), StructField(SequenceNumber,LongType,true)), obj#131: org.apache.spark.sql.Row
:           :                                +- *(1) Project [cast(body#608 as string) AS Body#60, enqueuedTime#612 AS EventTime#37, cast(enqueuedTime#612 as bigint) AS Timestamp#48L, cast(offset#610 as bigint) AS Offset#27L, partition#609 AS Partition#72, partitionKey#614 AS PartitionKey#84, publisher#613 AS Publisher#96, sequenceNumber#611L AS SequenceNumber#108L]
:           :                                   +- Scan ExistingRDD[body#608,partition#609,offset#610,sequenceNumber#611L,enqueuedTime#612,publisher#613,partitionKey#614,properties#615,systemProperties#616]
:           +- *(4) Project [value#453 AS domainName#455]
:              +- *(4) Filter isnotnull(value#453)
:                 +- *(4) SerializeFromObject [staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, input[0, java.lang.String, true], true, false) AS value#453]
:                    +- *(4) MapElements <function1>, obj#452: java.lang.String
:                       +- MapPartitions <function1>, obj#436: MTInterfaces.Fraud.RiskEntity
:                          +- DeserializeToObject newInstance(class scala.Tuple3), obj#435: scala.Tuple3
:                             +- ReusedExchange [_1#142, _2#143, _3#144], Exchange RoundRobinPartitioning(600)
+- *(8) Project [domain#147]
   +- *(8) Filter (isnotnull(rank#284) && (rank#284 = 1))
      +- Window [row_number() windowspecdefinition(domain#147, timestamp#151 DESC NULLS LAST, specifiedwindowframe(RowFrame, unboundedpreceding$(), currentrow$())) AS rank#284], [domain#147], [timestamp#151 DESC NULLS LAST]
         +- *(7) Sort [domain#147 ASC NULLS FIRST, timestamp#151 DESC NULLS LAST], false, 0
            +- Exchange hashpartitioning(domain#147, 400)
               +- *(6) Project [domain#147, timestamp#151]
                  +- *(6) Filter isnotnull(domain#147)
                     +- *(6) FileScan csv [domain#147,timestamp#151] Batched: false, Format: CSV, Location: InMemoryFileIndex[hdfs://MTPrime-CO4-fed/MTPrime-CO4-0/projects/BingAdsAdQuality/Test/WhoIs], PartitionFilters: [], PushedFilters: [IsNotNull(domain)], ReadSchema: struct<domain:string,timestamp:string>

Snapshots of query graph:

EDIT

 Right now I have moved the lookup data to a Cosmos DB store and created a TempView on top of it (say lookupdata). Now, I need to filter the ones that are not present in the store. I am exploring the following options:

1. create tempview on top of the streaming data as well and query

spark.sql(SELECT * FROM streamingdata s LEFT ANTI JOIN lookupdata l ON s.key = l.key")

Same as 1 but do inner sub-query instead of left anti join. i.e  spark.sql("SELECT s.* FROM streamingdata s WHERE s.key NOT IN (SELECT key FROM lookupdata l)")

Retain the streaming df as it is and do a filter op:

df.filter(x => { val key = x.getAs[String])("key")
  spark.sql("SELECT * FROM lookupdata l WHERE l.key = '"+key+"'").isEmpty
})

which one would work better?

Body:
Please try

from pyspark.sql.functions import broadcast
A.join(broadcast(B), Seq("key"), "left_anti")


问:
How to read in large CSV files, add multidimensions, and convert hourly to daily?
I have large CSV files that represent weather data for the US at hourly resolution for a 5-km grid. The data was saved for each day, so I concatenated them together for a yearly file. The ultimate goal is to compute daily, weekly and monthly averages of the variables (wind speed, temperature, precipitation, pressure, etc) by latitude and longitude. There are no column headers so I add column names to the file when I read it in.

When I tried reading in with Pandas in Python, it failed because it did not fit into memory. I can read in with Dask, but then I can't find a way to add the dimensions to the Dask dataframe or convert to xarray and do the same.  Is there a way to read in these too-large for memory files, add the lat, lon, datetime dimensions, compute daily, weekly and monthly averages for each lat/lon and output the file? Or, do I need to convert the csv to netCDF or similar before reading in?

答: As far as I know, you cannot load a CSV into xarray without going through pandas. So, yes, you will need to change the file format in order to use xarray. You might also consider breaking your data up into smaller files.</p>


问:
multiple regex/pattern validation Joi Hapi
Hi guys i want to make a mutiple regex validation with joi/hapi but i don't understand how to do it i already multiple try but no one works, I want the password minimun have 1 capital letter, 1 minus letter, 1 number and 1 special character here ones of my try:

```<code>const passwordValidation = (data) => {
  const schema = Joi.object({
    password: Joi.string()
      .pattern(new RegExp('^[a-z]{1,}$'))
      .pattern(new RegExp('^[A- Z]{1,}$'))
      .pattern(new RegExp('^[0 - 9]{1,}'))
      .pattern(new RegExp('^[!@#$%&*]{1,}'))
      .min(8)
      .required()
  });
  return schema.validate(data);
};
</code>```

and

```<code>const passwordValidation = (data) => {
  const schema = Joi.object({
    password: Joi.string()
      .regex('^[a-z]{1,}$')
      .regex('^[A- Z]{1,}$')
      .regex('^[0 - 9]{1,}')
      .regex('^[!@#$%&*]{1,}')
      .min(8)
      .required()
  });
  return schema.validate(data);
};
</code>```

how i can do it?

答:
regex() requires the object to be a RegExp object.

And fixes in regex

https://javascript.info/regexp-anchors

Working Demo

https://stackblitz.com/edit/js-7c78r7?file=index.js

Body:
Hi guys i want to make a mutiple regex validation with joi/hapi but i don't understand how to do it i already multiple try but no one works, I want the password minimun have 1 capital letter, 1 minus letter, 1 number and 1 special character here ones of my try:

const passwordValidation = (data) => {
  const schema = Joi.object({
    password: Joi.string()
      .pattern(new RegExp('^[a-z]{1,}$'))
      .pattern(new RegExp('^[A- Z]{1,}$'))
      .pattern(new RegExp('^[0 - 9]{1,}'))
      .pattern(new RegExp('^[!@#$%&*]{1,}'))
      .min(8)
      .required()
  });
  return schema.validate(data);
};

and

const passwordValidation = (data) => {
  const schema = Joi.object({
    password: Joi.string()
      .regex('^[a-z]{1,}$')
      .regex('^[A- Z]{1,}$')
      .regex('^[0 - 9]{1,}')
      .regex('^[!@#$%&*]{1,}')
      .min(8)
      .required()
  });
  return schema.validate(data);
};

how i can do it?


问:
How does this pointer-based strcat work in C?
I have some basic experience with C, but it usually takes me a little while to figure out how to implement something; using pointers and such is still a bit of a mystery to me.

Then I see an example like strcat implementation and I can't follow along. Would someone mind kindly explaining this to a C newcomer?

```<code>char *
my_strcat(char *dest, const char *src)
{
    char *rdest = dest;

    while (*dest)
      dest++;
    while (*dest++ = *src++)
      ;
    return rdest;
}
</code>```

When I read that, I think "rdest = ?, maybe real destination". So set a pointer to the original destination. Then "while (*dest) dest++;", what is that doing? Same with the next line. I don't follow.

Is it using any additional memory to the original two parts (src and dest)? Like in JS, if you concatenate 2 strings, it creates memory for a third string that combines the two, so you have double the memory. How is this avoided in this C implementation (if it is)?

答:
```<code>char * my_strcat(char *dest, const char *src)
{
    // Standard dictates strcat() to return dest.
    // That is pretty useless (returning a pointer to the
    // *end* of dest would have been better), but that's
    // the way it is.
    // Since we iterate dest as part of the implementation,
    // we need to "remember" its original value.
    char *rdest = dest;

    // Iterate over the characters pointed to by dest until
    // we found the end (null byte terminator), which is "false"
    while (*dest)
      dest++;

    // An assignment evaluates to the value assigned. So assigning
    // one character at a time (*dest = *src) will eventually
    // evaluate to false when we assigned the null byte terminator
    // from src (incidentially also terminating dest). Since we
    // postfix-increment both pointers during the assignment, we
    // don't need any actual body for the loop.
    while (*dest++ = *src++)
      ;

    // Return the "remembered" original dest value.
    return rdest;
}
</code>```

Is it using any additional memory to the original two parts (src and dest)? Like in JS, if you concatenate 2 strings, it creates memory for a third string that combines the two, so you have double the memory. How is this avoided in this C implementation (if it is)?

A precondition for strcat is that dest must have enough space to hold the end result. So, no, it does not need / assign additional memory. It is up to you to make sure there is enough memory, or realloc more memory before you call strcat.

Body:
I have some basic experience with C, but it usually takes me a little while to figure out how to implement something; using pointers and such is still a bit of a mystery to me.

Then I see an example like strcat implementation and I can't follow along. Would someone mind kindly explaining this to a C newcomer?

char *
my_strcat(char *dest, const char *src)
{
    char *rdest = dest;

    while (*dest)
      dest++;
    while (*dest++ = *src++)
      ;
    return rdest;
}

When I read that, I think "rdest = ?, maybe real destination". So set a pointer to the original destination. Then "while (*dest) dest++;", what is that doing? Same with the next line. I don't follow.

Is it using any additional memory to the original two parts (src and dest)? Like in JS, if you concatenate 2 strings, it creates memory for a third string that combines the two, so you have double the memory. How is this avoided in this C implementation (if it is)?

Body:
char * my_strcat(char *dest, const char *src)
{
    // Standard dictates strcat() to return dest.
    // That is pretty useless (returning a pointer to the
    // *end* of dest would have been better), but that's
    // the way it is.
    // Since we iterate dest as part of the implementation,
    // we need to "remember" its original value.
    char *rdest = dest;

    // Iterate over the characters pointed to by dest until
    // we found the end (null byte terminator), which is "false"
    while (*dest)
      dest++;

    // An assignment evaluates to the value assigned. So assigning
    // one character at a time (*dest = *src) will eventually
    // evaluate to false when we assigned the null byte terminator
    // from src (incidentially also terminating dest). Since we
    // postfix-increment both pointers during the assignment, we
    // don't need any actual body for the loop.
    while (*dest++ = *src++)
      ;

    // Return the "remembered" original dest value.
    return rdest;
}

Is it using any additional memory to the original two parts (src and dest)? Like in JS, if you concatenate 2 strings, it creates memory for a third string that combines the two, so you have double the memory. How is this avoided in this C implementation (if it is)?

A precondition for strcat is that dest must have enough space to hold the end result. So, no, it does not need / assign additional memory. It is up to you to make sure there is enough memory, or realloc more memory before you call strcat.


问:
Using sed, how do you print the first 'N' characters of a line?
Using sed what is an one liner to print the first n characters?  I am doing the following:

```<code>grep -G 'defn -test.*' OctaneFullTest.clj  | sed ....
</code>```

答:
```<code>colrm x
</code>```

For example, if you need the first 100 characters:

```<code>cat file |colrm 101
</code>```

It's been around for years and is in most linux's and bsd's (freebsd for sure), usually by default. I can't remember ever having to type apt-get install colrm.

OwnerDisplayName: Berlin Brown

Body:
Using sed what is an one liner to print the first n characters?  I am doing the following:

grep -G 'defn -test.*' OctaneFullTest.clj  | sed ....

Body:
colrm x

For example, if you need the first 100 characters:

cat file |colrm 101

It's been around for years and is in most linux's and bsd's (freebsd for sure), usually by default. I can't remember ever having to type apt-get install colrm.


问:
How to use '.' in magrittr with numerical formulas
I am unable to determine how to use (some ?) numerical formulas with magrittr. Here is a particular case:

```<code>library(magrittr)
y = c( complex(1, 1.0,2.0), complex(1, 3.0,4.0))
ymagn = y %>% sqrt(Re(.)**2 + Im(.)**2)
</code>```

Which results in

```
  Error in sqrt(., Re(.)^2 + Im(.)^2) :    2 arguments passed to 'sqrt'
  which requires 1
```

答:
This blog on magrittr http://www.win-vector.com/blog/2018/04/magrittr-and-wrapr-pipes-in-r-an-examination/ that also "introduces" wrapr  (i.e. it was new to me ) - describes the problem clearly:

Applying that to the original code snippet:

```<code>library(wrapr)
y = c( complex(1, 1.0,2.0), complex(1, 3.0,4.0))
ymagn = y %.>% sqrt(Re(.)**2 + Im(.)**2)
ymagn

[1] 2.236068 5.000000
</code>```

So wrapr is my new friend.

Body:
I am unable to determine how to use (some ?) numerical formulas with magrittr. Here is a particular case:

library(magrittr)
y = c( complex(1, 1.0,2.0), complex(1, 3.0,4.0))
ymagn = y %>% sqrt(Re(.)**2 + Im(.)**2)

Which results in

  Error in sqrt(., Re(.)^2 + Im(.)^2) :    2 arguments passed to 'sqrt'
  which requires 1

Body:
This blog on magrittr http://www.win-vector.com/blog/2018/04/magrittr-and-wrapr-pipes-in-r-an-examination/ that also "introduces" wrapr  (i.e. it was new to me ) - describes the problem clearly:

Applying that to the original code snippet:

library(wrapr)
y = c( complex(1, 1.0,2.0), complex(1, 3.0,4.0))
ymagn = y %.>% sqrt(Re(.)**2 + Im(.)**2)
ymagn

[1] 2.236068 5.000000

So wrapr is my new friend.


问:
loop through a nested dictionary to connect in multiple vendor devices in Napalm
I have NAPALM install and working to get facts from a single device at a time. However i deviced to make things a bit different, now I am trying to use a nested dictionary to connect to my devices, but I am getting a error.

```<code>import napalm
from napalm_ros import ros

database_devices={
    "cisco":{
    "hostname":"152.0.0.2",
    "type":"ios",
    "username":"cisco",
    "password":"cisco",
    "optional_args":{"secret" : "cisco"}
},
"mikrotik":{
    "hostname":"152.0.0.2",
    "type":"ros",
    "username":"mikrotik",
    "password":"mikrotik",
    "optional_args":{"port" :  "8728 "}
}
}

for key,values in database_devices.items():
    print("Connecting to " + key)
    driver=napalm.get_network_driver(values.get("type",{}))

    device=driver(
    hostname=values.get("hostname",{})
    username=values.get("username",{})
    password=values.get("password"),{})
    optional_args=values.get("optional_args",{})

    device.open()
</code>```

I tried to put comma between my devices in the device=driver , but I cant connect either.
And I think I will have another problem when I can fix this, my optional_args value is different in cisco and mikrotik, in cisco, my optional arg is my enable password, in mikrotik is a port.How I could get this values into my for ?
thanks for any help.

答:
Please try this it is working for me. Let me know
nested dictionary</p>

```<code>for k,v in database_devices.items():
      print('hostname '+v['hostname'])
      print('type:',v['type'])
      print('username: ',v['username'])
      print('password: ',v['password'])
      print('optional_args ',v['optional_args'])
</code>```

Body:
I have NAPALM install and working to get facts from a single device at a time. However i deviced to make things a bit different, now I am trying to use a nested dictionary to connect to my devices, but I am getting a error.

import napalm
from napalm_ros import ros

database_devices={
    "cisco":{
    "hostname":"152.0.0.2",
    "type":"ios",
    "username":"cisco",
    "password":"cisco",
    "optional_args":{"secret" : "cisco"}
},
"mikrotik":{
    "hostname":"152.0.0.2",
    "type":"ros",
    "username":"mikrotik",
    "password":"mikrotik",
    "optional_args":{"port" :  "8728 "}
}
}

for key,values in database_devices.items():
    print("Connecting to " + key)
    driver=napalm.get_network_driver(values.get("type",{}))

    device=driver(
    hostname=values.get("hostname",{})
    username=values.get("username",{})
    password=values.get("password"),{})
    optional_args=values.get("optional_args",{})

    device.open()

I tried to put comma between my devices in the device=driver , but I cant connect either.
And I think I will have another problem when I can fix this, my optional_args value is different in cisco and mikrotik, in cisco, my optional arg is my enable password, in mikrotik is a port.How I could get this values into my for ?
thanks for any help.

Body:
Please try this it is working for me. Let me know
nested dictionary

for k,v in database_devices.items():
      print('hostname '+v['hostname'])
      print('type:',v['type'])
      print('username: ',v['username'])
      print('password: ',v['password'])
      print('optional_args ',v['optional_args'])


问:
Oracle - get data from first day of previous month at particular time to first day of current month at particular time
My requirement is to get the data of a particular date range in Oracle as follows -

```<code>CompletedDate = Range between First Day of Previous Month @12:00AM to First Day of Current Month @4:00PM
</code>```

And also

```<code>CompleteDate = Range between previous Friday at 4:31 PM and current Friday at 4:30 PM
</code>```

Can you please give me a solution.

答:
You can use the date operations as follows:</p>

```<code>--scenario 1
SELECT * FROM YOUR_TABLE
WHERE DATE_COLUMN BETWEEN ADD_MONTHS(TRUNC(SYSDATE,'MON'), -1)
    AND TRUNC(SYSDATE,'MON') + INTERVAL '16' HOUR;

--scenario 2
SELECT * FROM YOUR_TABLE
WHERE DATE_COLUMN BETWEEN NEXT_DAY(TRUNC(SYSDATE) - INTERVAL '8' DAY,'FRI')
                          + INTERVAL '16' HOUR + INTERVAL '31' MINUTE
    AND NEXT_DAY(TRUNC(SYSDATE) - INTERVAL '1' DAY,'FRI') + INTERVAL '16' HOUR;
</code>```

Body:
My requirement is to get the data of a particular date range in Oracle as follows -

CompletedDate = Range between First Day of Previous Month @12:00AM to First Day of Current Month @4:00PM

And also

CompleteDate = Range between previous Friday at 4:31 PM and current Friday at 4:30 PM

Can you please give me a solution.

Body:
You can use the date operations as follows:

--scenario 1
SELECT * FROM YOUR_TABLE
WHERE DATE_COLUMN BETWEEN ADD_MONTHS(TRUNC(SYSDATE,'MON'), -1)
    AND TRUNC(SYSDATE,'MON') + INTERVAL '16' HOUR;

--scenario 2
SELECT * FROM YOUR_TABLE
WHERE DATE_COLUMN BETWEEN NEXT_DAY(TRUNC(SYSDATE) - INTERVAL '8' DAY,'FRI')
                          + INTERVAL '16' HOUR + INTERVAL '31' MINUTE
    AND NEXT_DAY(TRUNC(SYSDATE) - INTERVAL '1' DAY,'FRI') + INTERVAL '16' HOUR;


问:
Make div visible using only PHP as event listener
I am testing to set up an "button event listener" that would show a hidden div at button click. Below code works, my question is more if there are other ways of solving this, with only HTML/CSS and PHP ?

I am fully aware the "button event listener" in this particular case is more of a "superglobal $_POST variable" checker.

The purpose of the test was more to see if the button press can be scanned from PHP itself (skipping other logics such as JS or jquery, etc).

```<code><!DOCTYPE html>
<html lang="en">
<head>
  <title>Test</title>
</head>
<body>

<!-- Styling -->

<style>
.myDiv {display:none;}
</style>

<!-- Form -->

<form method="post">
  <button type="submit" name="button">Show div</button>
</form>

<div class="myDiv">MyDiv text...</div>

```

<?php

// Logics

// Button event listener

print_r($_POST);

if(isset($_POST['button'])) {
  echo "You pressed the button!";
  echo "

答:
first check if is post request and do visibility with php variable:</p>

```<code><!DOCTYPE html>
<html lang="en">
<head>
  <title>Test</title>
</head>
<body>
    <?php
        $display = 'none';
        if(!empty($_POST)) {
            if(array_key_exists('button', $_POST)) {
                $display = 'block';
            }
        }
    ?>
    <style>
        .myDiv {display:<?php echo $display;?>;}
    </style>

<!-- Styling -->

<!-- Form -->

<form method="post">
  <button type="submit" name="button">Show div</button>
</form>

<div class="myDiv">MyDiv text...</div>

```

<?php
print_r($_POST);

// Logics

// Button event listener

?>

</body>
</html>
</code>```

Body:
I am testing to set up an "button event listener" that would show a hidden div at button click. Below code works, my question is more if there are other ways of solving this, with only HTML/CSS and PHP ?

I am fully aware the "button event listener" in this particular case is more of a "superglobal $_POST variable" checker.

The purpose of the test was more to see if the button press can be scanned from PHP itself (skipping other logics such as JS or jquery, etc).

<!DOCTYPE html>

  <title>Test</title>

<form method="post">
  <button type="submit" name="button">Show div</button>
</form>

MyDiv text...

<?php

// Logics

// Button event listener

print_r($_POST);

if(isset($_POST['button'])) {
  echo "You pressed the button!";
  echo "

Body:
first check if is post request and do visibility with php variable:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Test</title>
</head>
<body>
    <?php
        $display = 'none';
        if(!empty($_POST)) {
            if(array_key_exists('button', $_POST)) {
                $display = 'block';
            }
        }
    ?>
    <style>
        .myDiv {display:<?php echo $display;?>;}
    </style>

<!-- Styling -->

<!-- Form -->

<form method="post">
  <button type="submit" name="button">Show div</button>
</form>

<div class="myDiv">MyDiv text...</div>

<pre>

<?php
print_r($_POST);

// Logics

// Button event listener

?>

</body>
</html>


问:
HTTP Error 500.35 - ANCM Multiple In-Process Applications in same Process ASP.NET Core 3
From this morning without any changes to the code of the project, a very simple Web API, one controller and 3 methods, with Swagger, it doesn't start anymore and I get the error:

HTTP Error 500.35 - ANCM Multiple In-Process Applications in same Process

Event viewer report the most useless message:

IIS Express AspNetCore Module V2: Failed to start application
  '/LM/W3SVC/2/ROOT/docs', ErrorCode '0x80004005'.

Restarted the system several times.

I'm using Visual Studio 2019, the application successfully compile and a few minutes ago it was working fine. No new software has been installed, no packages added.
Tried also clean and rebuild.

I've just modified the comment of a method. Obviously I've tried also to restore the previous comment but I get always the same message.

What can I do?

Is net core still too unstable to be used professionally?

UPDATE

The same code launched from the same version of Visual Studio but in another PC runs properly.

UPDATE 2

Below the code of the application:

startup.cs

```<code>using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System;
using System.IO;
using System.Reflection;

namespace WFP_GeoAPIs
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo() { Title = "Geographic APIs", Version = "v1.0.0" });
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(
                 Path.Combine(Directory.GetCurrentDirectory(), "swagger-ui")),
                RequestPath = "/swagger-ui"
            });

            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "GeoAPIs Ver 1.0.0");
                c.RoutePrefix = "docs";
                c.InjectStylesheet("/swagger-ui/custom.css");
            });
        }
    }
}
</code>```

Here is the launchsettings.json:

```<code>{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:51319",
      "sslPort": 44345
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "docs",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WFP_GeoAPIs": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "docs",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
</code>```

but coping the project on another PC with the same Visual Studio version works fine, so it looks like the is a configuration bug in the .NET Core or VIsual Studio property...

答:
I fixed the issue by deleting V2 in web.config file generated after publishing the solution.

```<code><handlers>
    <add name="aspNetCore" path="*" verb="*"
        modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
</code>```

instead of

```<code><handlers>
    <add name="aspNetCore" path="*" verb="*"
        modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
</code>```

Body:
From this morning without any changes to the code of the project, a very simple Web API, one controller and 3 methods, with Swagger, it doesn't start anymore and I get the error:

HTTP Error 500.35 - ANCM Multiple In-Process Applications in same Process

Event viewer report the most useless message:

IIS Express AspNetCore Module V2: Failed to start application
  '/LM/W3SVC/2/ROOT/docs', ErrorCode '0x80004005'.

Restarted the system several times.

I'm using Visual Studio 2019, the application successfully compile and a few minutes ago it was working fine. No new software has been installed, no packages added.
Tried also clean and rebuild.

I've just modified the comment of a method. Obviously I've tried also to restore the previous comment but I get always the same message.

What can I do?

Is net core still too unstable to be used professionally?

UPDATE

The same code launched from the same version of Visual Studio but in another PC runs properly.

UPDATE 2

Below the code of the application:

startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System;
using System.IO;
using System.Reflection;

namespace WFP_GeoAPIs
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo() { Title = "Geographic APIs", Version = "v1.0.0" });
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(
                 Path.Combine(Directory.GetCurrentDirectory(), "swagger-ui")),
                RequestPath = "/swagger-ui"
            });

            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "GeoAPIs Ver 1.0.0");
                c.RoutePrefix = "docs";
                c.InjectStylesheet("/swagger-ui/custom.css");
            });
        }
    }
}

Here is the launchsettings.json:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:51319",
      "sslPort": 44345
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "docs",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WFP_GeoAPIs": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "docs",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

but coping the project on another PC with the same Visual Studio version works fine, so it looks like the is a configuration bug in the .NET Core or VIsual Studio property...

Body:
I fixed the issue by deleting V2 in web.config file generated after publishing the solution.

<handlers>
    <add name="aspNetCore" path="*" verb="*"
        modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>

instead of

<handlers>
    <add name="aspNetCore" path="*" verb="*"
        modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>


问:
ASP.NET Map GIS Control
I need a Map GIS control that I can use on my ASP.NET web pages. Basically the control should be able to read a shape file(which has 3-4 layers). Then it should let you zoom in and out and select certain objects.
Based on the selections, I should be able to do some calculation.
For instance, if I use a certain state, I load the shape file and on the map I should be able to select certain counties only and add up their population.
Please suggest Opensource or commercial controls (<1k$) </p>

答:
Free open source: SharpMap

Commercial. ESRI have a number of options. Their Silverlight control is free. They have ASP.Net controls but I suspect they will be >1K US $

Title: ASP.NET Map GIS Control


问:
Need to find '$word;' pattern in string
I have big text file and I have to find all words starts with '$' and ends with ';' like $word;.

```<code>import re

text = "$h;BREWERY$h_end;You've built yourself a brewery."
x = re.findall("$..;", text)
print(x)
</code>```

I want my output like ['$h;', '$h_end;'] How can I do that?

答:
I have to find all words starts with '$' and ends with ';' like $word;.

I would do:

```<code>import re
text = "$h;BREWERY$h_end;You've built yourself a brewery."
result = re.findall('\$[^;]+;',text)
print(result)
</code>```

Output:

```<code>['$h;', '$h_end;']
</code>```

Note that $ needs to be escaped (\$) as it is one of special characters. Then I match 1 or more occurences of anything but ; and finally ;.

Title: Need to find '$word;' pattern in string

Body:
I have big text file and I have to find all words starts with '$' and ends with ';' like $word;.

import re

text = "$h;BREWERY$h_end;You've built yourself a brewery."
x = re.findall("$..;", text)
print(x)

I want my output like ['$h;', '$h_end;'] How can I do that?

Body:
I have to find all words starts with '$' and ends with ';' like $word;.

I would do:

import re
text = "$h;BREWERY$h_end;You've built yourself a brewery."
result = re.findall('\$[^;]+;',text)
print(result)

Output:

['$h;', '$h_end;']

Note that $ needs to be escaped (\$) as it is one of special characters. Then I match 1 or more occurences of anything but ; and finally ;.


问:
Django error, CSRF Failed: CSRF token missing or incorrect
I have Backend = Django+Django Rest+Djoser(Token based user auth app)
Fontend = React JS + Axios.

In Local Host/Development I could do user login and authentication using token. It did'nt give error in Postman too.

In production mode It gives error as CSRF failed.

Why is so ?

I think CSRF token only needed in session authentication.

I tried removing session auth but resulted in server crash

```<code>REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',

    ),
   ....
}
</code>```

middlewares

```<code>MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
</code>```

installed apps

```<code>INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'shop',
    'corsheaders',
    'frontend',
    'djoser',
    'rest_framework.authtoken',
    'users'
]

</code>```

答:
Now to disable csrf check, you can create a custom authentication class CsrfExemptSessionAuthentication which extends from the default SessionAuthentication class. In this authentication class, we will override the enforce_csrf() check which was happening inside the actual SessionAuthentication.

```<code>from rest_framework.authentication import SessionAuthentication, TokenAuthentication

class CsrfExemptSessionAuthentication(SessionAuthentication):

    def enforce_csrf(self, request):
        return  # To not perform the csrf check previously happening
</code>```

In your view, then you can define the authentication_classes to be:

```<code>authentication_classes = (CsrfExemptSessionAuthentication, TokenAuthentication)
</code>```

This should handle the csrf error.

Body:
I have Backend = Django+Django Rest+Djoser(Token based user auth app)
Fontend = React JS + Axios.

In Local Host/Development I could do user login and authentication using token. It did'nt give error in Postman too.

In production mode It gives error as CSRF failed.

Why is so ?

I think CSRF token only needed in session authentication.

I tried removing session auth but resulted in server crash

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',

    ),
   ....
}

middlewares

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

installed apps

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'shop',
    'corsheaders',
    'frontend',
    'djoser',
    'rest_framework.authtoken',
    'users'
]

Body:
Now to disable csrf check, you can create a custom authentication class CsrfExemptSessionAuthentication which extends from the default SessionAuthentication class. In this authentication class, we will override the enforce_csrf() check which was happening inside the actual SessionAuthentication.

from rest_framework.authentication import SessionAuthentication, TokenAuthentication

class CsrfExemptSessionAuthentication(SessionAuthentication):

    def enforce_csrf(self, request):
        return  # To not perform the csrf check previously happening

In your view, then you can define the authentication_classes to be:

authentication_classes = (CsrfExemptSessionAuthentication, TokenAuthentication)

This should handle the csrf error.


问:
Calling a method of a Java object passed as argument to hooked function in Frida
I am trying to obtain the SecretKey passed to the decryptAesCipherText function. I hooked the function in Frida to try to print out the arguments when the method is called but since SecretKey is an object, all attempts to print it out give output as [object Object]. However the SecretKey object has a method getEncoded() which will return a byte array which can be printed out in hex format. How can I call this method from Frida and get the result?

The java function, I am hooking to is given below

```<code>import javax.crypto.Cipher;
import javax.crypto.SecretKey;

private byte[] decryptAesCipherText(SecretKey secretKey, byte[] bArr) {
        Cipher instance = Cipher.getInstance("AES/ECB/PKCS5Padding");
        instance.init(2, secretKey);
        return decryptCipherText(instance, bArr);
}
</code>```

The javascript snippet (incomplete) to hook the function

```<code>var target_class = Java.use('com.reactlibrary.securekeystore.RNSecureKeyStoreModule');

target_class.decryptAesCipherText.overload('javax.crypto.SecretKey','[B').implementation = function(key, array){
        console.log("Inside decrypt aes");

        //Call getEncoded method on key to get byte array

        var ret = my_class.decryptAesCipherText.overload('javax.crypto.SecretKey','[B').call(this, key, array);
        return ret;
}
</code>```

答:
It seems like you can't call getEncoded on the javax.crypto.SecretKey interface.

Usually the SecretKey parameter is of type javax.crypto.spec.SecretKeySpec and if you type cast the key parameter to SecretKeySpec you can can call getEncoded() and print the used key:

```<code>function encodeHex(byteArray) {
    const HexClass = Java.use('org.apache.commons.codec.binary.Hex');
    const StringClass = Java.use('java.lang.String');
    const hexChars = HexClass.encodeHex(byteArray);
    return StringClass.$new(hexChars).toString();
}

Java.perform(function x() {
    const target_class = Java.use('com.example.myapplication.MainActivity');
    target_class.decryptAesCipherText.overload('javax.crypto.SecretKey', '[B').implementation = function (key, array) {
        console.log("Inside decrypt aes");

        const secretKeySpec = Java.cast(key, Java.use('javax.crypto.spec.SecretKeySpec'));
        const encodedKey = secretKeySpec.getEncoded();

        // print the key bytes as hex value
        console.log("KEY: " + encodeHex(encodedKey));

        var ret = my_class.decryptAesCipherText.overload('javax.crypto.SecretKey', '[B').call(this, key, array);
        return ret;
    }

});
</code>```

Body:
I am trying to obtain the SecretKey passed to the decryptAesCipherText function. I hooked the function in Frida to try to print out the arguments when the method is called but since SecretKey is an object, all attempts to print it out give output as [object Object]. However the SecretKey object has a method getEncoded() which will return a byte array which can be printed out in hex format. How can I call this method from Frida and get the result?

The java function, I am hooking to is given below

import javax.crypto.Cipher;
import javax.crypto.SecretKey;

private byte[] decryptAesCipherText(SecretKey secretKey, byte[] bArr) {
        Cipher instance = Cipher.getInstance("AES/ECB/PKCS5Padding");
        instance.init(2, secretKey);
        return decryptCipherText(instance, bArr);
}

The javascript snippet (incomplete) to hook the function

var target_class = Java.use('com.reactlibrary.securekeystore.RNSecureKeyStoreModule');

target_class.decryptAesCipherText.overload('javax.crypto.SecretKey','[B').implementation = function(key, array){
        console.log("Inside decrypt aes");

        //Call getEncoded method on key to get byte array

        var ret = my_class.decryptAesCipherText.overload('javax.crypto.SecretKey','[B').call(this, key, array);
        return ret;
}

Body:
It seems like you can't call getEncoded on the javax.crypto.SecretKey interface.

Usually the SecretKey parameter is of type javax.crypto.spec.SecretKeySpec and if you type cast the key parameter to SecretKeySpec you can can call getEncoded() and print the used key:

function encodeHex(byteArray) {
    const HexClass = Java.use('org.apache.commons.codec.binary.Hex');
    const StringClass = Java.use('java.lang.String');
    const hexChars = HexClass.encodeHex(byteArray);
    return StringClass.$new(hexChars).toString();
}

Java.perform(function x() {
    const target_class = Java.use('com.example.myapplication.MainActivity');
    target_class.decryptAesCipherText.overload('javax.crypto.SecretKey', '[B').implementation = function (key, array) {
        console.log("Inside decrypt aes");

        const secretKeySpec = Java.cast(key, Java.use('javax.crypto.spec.SecretKeySpec'));
        const encodedKey = secretKeySpec.getEncoded();

        // print the key bytes as hex value
        console.log("KEY: " + encodeHex(encodedKey));

        var ret = my_class.decryptAesCipherText.overload('javax.crypto.SecretKey', '[B').call(this, key, array);
        return ret;
    }

});


问:
How to find all enhancements in SAP system?
I need to prepare a document which includes all enhancements implemented in the SAP system of the company, what are all steps that I have to go through?</p>

答:
All the enhancements reside in ENHINCINX table and can be searched by package like this:

```<code>PARAMETERS p_pack TYPE devclass.

"Accept wildcards for package
p_pack = COND #( WHEN p_pack IS INITIAL THEN '%' ELSE replace( val = p_pack sub = `*` with = `%` occ = 0 ) ).

SELECT e~*
  FROM enhincinx AS e
 INNER JOIN tadir AS t
    ON 'R3TR' = t~pgmid
   AND 'ENHO' = t~object
   AND e~enhname = t~obj_name
 WHERE t~devclass LIKE @p_pack
  INTO TABLE @DATA(lt_enhincinx).

LOOP AT lt_enhincinx ASSIGNING FIELD-SYMBOL(<ls_enhincinx>).
 WRITE: / |{ <ls_enhincinx>-programname } / { <ls_enhincinx>-enhname }:|.
ENDLOOP.
</code>```

Body:
All the enhancements reside in ENHINCINX table and can be searched by package like this:

PARAMETERS p_pack TYPE devclass.

"Accept wildcards for package
p_pack = COND #( WHEN p_pack IS INITIAL THEN '%' ELSE replace( val = p_pack sub = `*` with = `%` occ = 0 ) ).

SELECT e~*
  FROM enhincinx AS e
 INNER JOIN tadir AS t
    ON 'R3TR' = t~pgmid
   AND 'ENHO' = t~object
   AND e~enhname = t~obj_name
 WHERE t~devclass LIKE @p_pack
  INTO TABLE @DATA(lt_enhincinx).

LOOP AT lt_enhincinx ASSIGNING FIELD-SYMBOL(<ls_enhincinx>).
 WRITE: / |{ <ls_enhincinx>-programname } / { <ls_enhincinx>-enhname }:|.
ENDLOOP.


问:
please help me to construct the reg ex in javascript
The ask is to create a regular expression in JavaScript that holds true for the following format:- "APL-101"

1) Letters before '-' should be in caps, should not contain any special character and it can be any count.

2) After '-' the string should contain digits only, no special characters and alphabets required plus it can be of any count.

3) '-' will only be the special character that is allowed in the string and it should be mandatory

Need Your help.
Thanks in advance.

答:
This one could help. try it here

[A-Z].*-[0-9].*


问:
How to customize components in the admin area of SilverStripe?
I am trying to follow along with this tutorial.

The problem is that the Injector API is slightly different than how it is used in the tutorial. Injector does not need to be imported, and many of the methods are found under certain properties (such as Injector.default.transform and Injector.default.component.transform).

I have tried:

Injector.default.component.transform('character-count-transform', (updater) => {
  updater('TextField', CharacterCounter);
});

I get no errors in the console, but the TextField is unchanged.

The injector repository even mentions that only some of the documentation will apply to this version.

答:
The code in the tutorial is correct

```<code>Injector.transform('character-count-transform', (updater) => {
  updater.component('TextField', CharacterCounter);
});
</code>```

But if you are looking at a TextField in Pages admin, you will not see the react component as that section isn't "reactified" yet.
Look in Files admin (upload a file) and look at the details form of an image, the text fields should have the updated component rendered.
Am still figuring out how to get it to work in the Page admin section, but my initial guess would be to subclass TextField and provide some react-y stuff in that class. dnadesign/silverstripe-elemental for some ideas

Body:
The code in the tutorial is correct

Injector.transform('character-count-transform', (updater) => {
  updater.component('TextField', CharacterCounter);
});

But if you are looking at a TextField in Pages admin, you will not see the react component as that section isn't "reactified" yet.
Look in Files admin (upload a file) and look at the details form of an image, the text fields should have the updated component rendered.
Am still figuring out how to get it to work in the Page admin section, but my initial guess would be to subclass TextField and provide some react-y stuff in that class. dnadesign/silverstripe-elemental for some ideas