问:
Python - Fetching data from API
I have the below code that fetches data from a API. This works fine, however I see that it only returns back 25 rows. I am trying to see how could I extract all of the data from the API call and not limit to 25</p>

```<code>import requests
import pandas as pd

API_KEY = API_KEY

url = 'https://api.pagerduty.com/incidents/'
headers = {
    'Accept': 'application/vnd.pagerduty+json;version=2',
    'Authorization': 'Token token={token}'.format(token=API_KEY)}
r = requests.get(url, headers=headers)
data = r.content
data_dict = json.loads(data)
data_df = pd.DataFrame(data_dict['incidents'])
</code>```

答:
Check documentation here.

You can pass the query parameter for example

```<code>url = 'https://api.pagerduty.com/incidents?limit=50'
</code>```

The maximum limit as per docs is 100 so you will have to send multiple GET requests. If there are thousands of records make sure your python script send GET request at specific time interval because of rate-limiting else will through Too Many Requests error.

Title: Python - Fetching data from API

Body:
I have the below code that fetches data from a API. This works fine, however I see that it only returns back 25 rows. I am trying to see how could I extract all of the data from the API call and not limit to 25

import requests
import pandas as pd

API_KEY = API_KEY

url = 'https://api.pagerduty.com/incidents/'
headers = {
    'Accept': 'application/vnd.pagerduty+json;version=2',
    'Authorization': 'Token token={token}'.format(token=API_KEY)}
r = requests.get(url, headers=headers)
data = r.content
data_dict = json.loads(data)
data_df = pd.DataFrame(data_dict['incidents'])

Body:
Check documentation here.

You can pass the query parameter for example

url = 'https://api.pagerduty.com/incidents?limit=50'

The maximum limit as per docs is 100 so you will have to send multiple GET requests. If there are thousands of records make sure your python script send GET request at specific time interval because of rate-limiting else will through Too Many Requests error.


来源: stackexchange

问:
Shared preferences issue
I'm new to android development

In my application I want to keep log-in status of user in my activity. I have 2 edit boxes for entering name and password. And 1 check-box for user if he wants to keep log-in.

When user clicks on login button I'm saving name and password in shared preference. And in onCreate method I want to retrieve that value. If user has clicks check-box then he should directly redirect to next page. But its not working.

Here is my code:

```<code>signUpButton=(Button)findViewById(R.id.signUpLoginId);
signUpButton.setOnClickListener(this);

//toggleButtonOnOff=(ToggleButton)findViewById(R.id.togglebutton);

loginEditText = (EditText) findViewById(R.id.loginNameID);
passwordEditText=(EditText)findViewById(R.id.passwordId);
box=(CheckBox)findViewById(R.id.checkbox);

preferences=PreferenceManager.getDefaultSharedPreferences(context);

String n=preferences.getString(PREF_USERNAME, null);
String p=preferences.getString(PREF_PASSWORD, null);

loginEditText.setText(n1);
passwordEditText.setText(p1);

Intent intent1=new Intent(LoginActivity.this,FindFishMenuActivity.class);
startActivity(intent1);

LoginActivity.this.finish();

if(box.isChecked())
{
    box.setChecked(true);
    Toast.makeText(LoginActivity.this,"box value set to true",Toast.LENGTH_LONG).show();
    password=passwordEditText.getText().toString();
    preferences=context.getSharedPreferences(key_str, MODE_PRIVATE);
    editor=preferences.edit();
    editor.putString(PREF_USERNAME, name);
    editor.putString(PREF_PASSWORD, password);
    editor.commit();
    intent=new Intent(LoginActivity.this,FindFishMenuActivity.class);
    startActivity(intent);
    LoginActivity.this.finish();
}
</code>```

答:
If you finish your Activity, the Code after it is never reached!

But I would put the code which saves your Name and Password to the SharedPreference before you fire the Intent. Maybe the Preference-Object isn't saved yet when you try to read from it in the other Activity. Try debuging this using LogCat.

ContentLicense: CC BY-SA 3.0

Title: Shared preferences issue

Body:
I'm new to android development

In my application I want to keep log-in status of user in my activity. I have 2 edit boxes for entering name and password. And 1 check-box for user if he wants to keep log-in.

When user clicks on login button I'm saving name and password in shared preference. And in onCreate method I want to retrieve that value. If user has clicks check-box then he should directly redirect to next page. But its not working.

Here is my code:

signUpButton=(Button)findViewById(R.id.signUpLoginId);
signUpButton.setOnClickListener(this);

//toggleButtonOnOff=(ToggleButton)findViewById(R.id.togglebutton);

loginEditText = (EditText) findViewById(R.id.loginNameID);
passwordEditText=(EditText)findViewById(R.id.passwordId);
box=(CheckBox)findViewById(R.id.checkbox);

preferences=PreferenceManager.getDefaultSharedPreferences(context);

String n=preferences.getString(PREF_USERNAME, null);
String p=preferences.getString(PREF_PASSWORD, null);

loginEditText.setText(n1);
passwordEditText.setText(p1);

Intent intent1=new Intent(LoginActivity.this,FindFishMenuActivity.class);
startActivity(intent1);

LoginActivity.this.finish();

if(box.isChecked())
{
    box.setChecked(true);
    Toast.makeText(LoginActivity.this,"box value set to true",Toast.LENGTH_LONG).show();
    password=passwordEditText.getText().toString();
    preferences=context.getSharedPreferences(key_str, MODE_PRIVATE);
    editor=preferences.edit();
    editor.putString(PREF_USERNAME, name);
    editor.putString(PREF_PASSWORD, password);
    editor.commit();
    intent=new Intent(LoginActivity.this,FindFishMenuActivity.class);
    startActivity(intent);
    LoginActivity.this.finish();
}


问:
Add user data to Firestore on sign up with Firebase Authentication with Google Sign-In with Swift
I need to store some data in a "users" collection in the Firestore when the user signs in with Google for the first time on my iOS app. I am using Firebase Authentication. The data to be stored is:

id: the user's UID

display name: the user's full name

photoURL: the url of the user's google account

points: this will be 0 when the user first signs in

knownLanguageCodes: this will be an empty array when the user first signs in

At the moment, nothing is saved to the Firestore.
This the section on my app delegate where signing in is handled.

```<code>class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        FirebaseApp.configure()
        GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
        GIDSignIn.sharedInstance().delegate = self

        return true
    }

    //Only available on iOS 9 and later
    @available(iOS 9.0, *)
    func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url)
    }

    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
        if let error = error {
            print(error.localizedDescription)
            return
        }

        guard let authentication = user.authentication else { return }
        let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                 accessToken: authentication.accessToken)

        Auth.auth().signIn(with: credential) { (res, err) in

            if let err = err {
                print(err.localizedDescription)
                return
            }

            //I'm not sure if this block of code is in the right place
            let db = Firestore.firestore()
            db.collection("user").document(String((res?.user.uid)!)).setData([
                "id" : String((res?.user.uid)!),
                "displayName" : (res?.user.displayName)!,
                "photoURL" : (res?.user.photoURL)!
                "points" : 0
                "knownLanguageCodes" : []
            ], merge: true)
        }
    }

    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {

    }
    //...
</code>```

I'm using SwiftUI for the UI.

答:
Yes everything is correct but you are missing calling this function
       GIDSignIn.sharedInstance().signIn()</p>

ContentLicense: CC BY-SA 4.0

Body:
I need to store some data in a "users" collection in the Firestore when the user signs in with Google for the first time on my iOS app. I am using Firebase Authentication. The data to be stored is:

id: the user's UID

display name: the user's full name

photoURL: the url of the user's google account

points: this will be 0 when the user first signs in

knownLanguageCodes: this will be an empty array when the user first signs in

At the moment, nothing is saved to the Firestore.
This the section on my app delegate where signing in is handled.

class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        FirebaseApp.configure()
        GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
        GIDSignIn.sharedInstance().delegate = self

        return true
    }

    //Only available on iOS 9 and later
    @available(iOS 9.0, *)
    func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url)
    }

    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
        if let error = error {
            print(error.localizedDescription)
            return
        }

        guard let authentication = user.authentication else { return }
        let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                 accessToken: authentication.accessToken)

        Auth.auth().signIn(with: credential) { (res, err) in

            if let err = err {
                print(err.localizedDescription)
                return
            }

            //I'm not sure if this block of code is in the right place
            let db = Firestore.firestore()
            db.collection("user").document(String((res?.user.uid)!)).setData([
                "id" : String((res?.user.uid)!),
                "displayName" : (res?.user.displayName)!,
                "photoURL" : (res?.user.photoURL)!
                "points" : 0
                "knownLanguageCodes" : []
            ], merge: true)
        }
    }

    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {

    }
    //...

I'm using SwiftUI for the UI.


问:
Python copy to clipboard the content of a variable
I have a variable that stores text that the user types in a dialogue box.
I need to copy this text and then paste it in another field (to make a search)

I tried pyperclip but it works only with plain text, not with variables. Here is the code just for the dialogue box and e is my variable.

```<code>from tkinter import *
master = Tk()
e = Entry(master)
e.pack()

e.focus_set()

def callback():
    print(e.get())  # This is the text I want to use later

b = Button(master, text="insert", width=10, command=callback)
b.pack()

mainloop()
</code>```

答:
You need to create tkinter string to store this value, i have included this in your code below.

```<code>from tkinter import *
master = Tk()

estring = StringVar(master)

e = Entry(master, textvariable = estring,)
e.pack()

e.focus_set()

def callback():
    print(estring.get())  # This is the text I want to use later

b = Button(master,  text="insert", width=10, command=callback)
b.pack()

mainloop()
</code>```

Below is the exmaple using pyperclip, to copy the input text to the clipboard.

```<code>from tkinter import *
import pyperclip

master = Tk()

estring = StringVar(master)

e = Entry(master, textvariable = estring,)
e.pack()

e.focus_set()

def callback():
    pyperclip.copy(estring.get())

b = Button(master,  text="copy", width=10, command=callback)
b.pack()

mainloop()
</code>```

Once text is entered and the copy button is pressed the text is now on the clipboard.

Body:
I have a variable that stores text that the user types in a dialogue box.
I need to copy this text and then paste it in another field (to make a search)

I tried pyperclip but it works only with plain text, not with variables. Here is the code just for the dialogue box and e is my variable.

from tkinter import *
master = Tk()
e = Entry(master)
e.pack()

e.focus_set()

def callback():
    print(e.get())  # This is the text I want to use later

b = Button(master, text="insert", width=10, command=callback)
b.pack()

mainloop()

Body:
You need to create tkinter string to store this value, i have included this in your code below.

from tkinter import *
master = Tk()

estring = StringVar(master)

e = Entry(master, textvariable = estring,)
e.pack()

e.focus_set()

def callback():
    print(estring.get())  # This is the text I want to use later

b = Button(master,  text="insert", width=10, command=callback)
b.pack()

mainloop()

Below is the exmaple using pyperclip, to copy the input text to the clipboard.

from tkinter import *
import pyperclip

master = Tk()

estring = StringVar(master)

e = Entry(master, textvariable = estring,)
e.pack()

e.focus_set()

def callback():
    pyperclip.copy(estring.get())

b = Button(master,  text="copy", width=10, command=callback)
b.pack()

mainloop()

Once text is entered and the copy button is pressed the text is now on the clipboard.


问:
Extract part of string in multiple places
How can I extract part of this string

```<code>declare @string varchar(1024)
set @string = 'Total # of bytes             : 128270200832 (119.46GB)'
select Substring (@string, Charindex( ':', @string )+2 , Len(@string))
</code>```

I only need the numbers after: and without the (119.46GB)

答:
Try this:</p>

```<code>declare @string varchar(1024)
set @string = 'Total # of bytes             : 128270200832 (119.46GB)'
select substring(@string,charindex(':',@string) + 2,(charindex('(',@string) - charindex(':',@string)-2))
</code>```

Body:
How can I extract part of this string

declare @string varchar(1024)
set @string = 'Total # of bytes             : 128270200832 (119.46GB)'
select Substring (@string, Charindex( ':', @string )+2 , Len(@string))

I only need the numbers after: and without the (119.46GB)

Body:
Try this:

declare @string varchar(1024)
set @string = 'Total # of bytes             : 128270200832 (119.46GB)'
select substring(@string,charindex(':',@string) + 2,(charindex('(',@string) - charindex(':',@string)-2))


问:
multiple button close not working in reactjs
I have a datepicker and dates are displayed in textfield with button close(x).
On Selecting the date from datepicker, will add date as shown,

But close button is not working,
on clicking close, should remove the specific date field.
link
https://codesandbox.io/s/strange-http-zhj4f?fontsize=14&hidenavigation=1&theme=dark

答:
Change your code a little bit.

```<code><DatePicker
     locale="en-GB"
     onChange={this.buttonHanlder}
     value={this.state.closedate}
/>
</code>```

And in your buttonHandler method, use the below code

```<code>buttonHanlder = value => {
    if (value) {
        var dateformat = [
            ("0" + value.getDate()).slice(-2) +
            "-" +
            ("0" + (value.getMonth() + 1)).slice(-2) +
            "-" +
            value.getFullYear()
        ];
        this.setState(prevState => ({
            closedate: value,
            closedatearr: [...prevState.closedatearr, ...dateformat]
        }));
    } else {
        this.setState({
            closedate: ''
        });
    }
};
</code>```

Reason to do so: When you click on X(close button) react-date-picker calls buttonHandler with null value which means that user has cleared out the date. So you need to handle null value according to your requirements.

Body:
Change your code a little bit.

<DatePicker
     locale="en-GB"
     onChange={this.buttonHanlder}
     value={this.state.closedate}
/>

And in your buttonHandler method, use the below code

buttonHanlder = value => {
    if (value) {
        var dateformat = [
            ("0" + value.getDate()).slice(-2) +
            "-" +
            ("0" + (value.getMonth() + 1)).slice(-2) +
            "-" +
            value.getFullYear()
        ];
        this.setState(prevState => ({
            closedate: value,
            closedatearr: [...prevState.closedatearr, ...dateformat]
        }));
    } else {
        this.setState({
            closedate: ''
        });
    }
};

Reason to do so: When you click on X(close button) react-date-picker calls buttonHandler with null value which means that user has cleared out the date. So you need to handle null value according to your requirements.


问:
Varnish: Take image from browser cache if image is still up to date in varnish cache and not updated in app
I want an object (image) to be cached in browser. But before delivering it shall be checked if the object in varnish cache is still up to date.
What do I have to configure in the varnish vcl file?

app has to purge the object to/in varnish

...

答:
The best way to achieve this goal is by leveraging so-called Conditional Requests.

How conditional requests work

Conditional requests will perform a low-impact validation to check if the object in cache is still considered fresh.

The goal is to only serve the full object if the validation doesn't match. If the validation succeeds, the backend server will not return any payload and will use the HTTP 304 status code to indicate that the value is still the same.

There are 2 ways to do this:

The Etag response header

The Last-Modified response header

Etag

An Etag is an HTTP response header that transmits a fingerprint of the response, allowing browsers to check if the version they have is still up to date.

As soon as a browser receives an Etag, it will stores its value and it will transmit this value upon every subsequent request through the If-None-Match header.

It's up to the application to compare the If-None-Match value to the Etag value it's about to expose. If the values are identical, the browser still has the most recent version and doesn't need to get the full payload back.

In this case, it's up to the webserver or the application to return an HTTP 304, NOT MODIFIED header without sending any payload.

If the webserver or application are optimized for detecting conditional requests and to return HTTP 304 responses, the performance impact can be huge.

Last-Modified

Instead of including an Etag header in your HTTP response, you can also include the Last-Modified header, which represents the last time this page was modified.

The browser will look for this value, store it, and upon subsequent requests will transmit that value through a If-Modified-Since header.

The rest of the story is the same: the webserver or the application should look out for that header and at the appropriate time return an HTTP 304.

How to configure the Etag

In Apache, you can make sure an Etag is automatically added by configuring the FileETag setting. See https://httpd.apache.org/docs/2.4/mod/core.html#fileetag

Nginx has a similar configuration setting: http://nginx.org/en/docs/http/ngx_http_core_module.html#etag

If  you want to configure this yourself for HTML or JSON output, I advise taking a SHA or MD5 hash of the output.

What about Varnish?

Varnish supports both Etag & Last-Modified in both directions, meaning:

If a browser sends a request containing an If-None-Match header, Varnish can return a HTTP 304 if the value matches the stored ETag and if the cached object is still fresh.

If Varnish needs to revalidate an expired object, it will use the same If-None-Match value that the browser sent.

What about Cache-Control TTLs?

It's important to know that conditional requests only happen when the TTL has expired. If you want frequent revalidation, set your Cache-Control TTL value quite low.

As long as your backend platform (webserver or application) is optimized to handle those conditional requests without using too much server resources, you'll be fine.

Varnish will perform this conditional requests asynchronously to the backend and will serve stale data to the browser while it's performing this request.


问:
Strange decompilation when using gcc with different optimization
I am running on linux 5.4.18-1-MANJARO with gcc version 9.2.0 (GCC)

Filename:a.c

```<code>#include<stdio.h>

int
main(void) {
    int a;

    scanf("%d", &a);

    if (a < 5 || a > 6)
        puts("fail");
    else
        puts("succeed");
}
</code>```

Then I run:

```<code>gcc a.c -O0 -o a.out
gcc a.c -O1 -o b.out
</code>```

I decompile a.out with r2, and I got this

```<code>undefined8 main(void)
{
    undefined8 uVar1;
    int64_t in_FS_OFFSET;
    int32_t var_ch;
    int64_t canary;

    canary = *(int64_t *)(in_FS_OFFSET + 0x28);
    sym.imp.__isoc99_scanf(0x2004, &var_ch);
    if ((var_ch < 5) || (6 < var_ch)) {
        sym.imp.puts("fail");
    } else {
        sym.imp.puts("succeed");
    }
    uVar1 = 0;
    if (canary != *(int64_t *)(in_FS_OFFSET + 0x28)) {
        uVar1 = sym.imp.__stack_chk_fail();
    }
    return uVar1;
}
</code>```

This is what I expected.

But when I decompile b.out, I got this

```<code>undefined8 main(void)
{
    undefined8 uVar1;
    undefined8 extraout_RDX;
    int64_t iVar2;
    int32_t *piVar3;
    uint32_t uVar4;
    int64_t in_FS_OFFSET;
    int32_t iStack20;
    int64_t iStack16;

    iStack16 = *(int64_t *)(in_FS_OFFSET + 0x28);
    piVar3 = &iStack20;
    sym.imp.__isoc99_scanf(0x2004, piVar3);
    if (iStack20 - 5U < 2) {
        uVar4 = 0x200c;
        sym.imp.puts("succeed");
    } else {
        uVar4 = 0x2007;
        sym.imp.puts("fail");
    }
    if (iStack16 != *(int64_t *)(in_FS_OFFSET + 0x28)) {
        sym.imp.__stack_chk_fail();
        sym._init();
        iVar2 = 0;
        do {
            uVar1 = (**(code **)(segment.LOAD3 + iVar2 * 8))((uint64_t)uVar4, piVar3, extraout_RDX);
            iVar2 = iVar2 + 1;
        } while (iVar2 != 1);
        return uVar1;
    }
    return 0;
}
</code>```

which seems only check if iStack20 < 7, and b.out runs fine.

I don't understand how it works.

答:
Here, an integer underflow is exploited. The operation

```<code>iStack20 - 5U
</code>```

is performed unsigned due to the implicit conversion rules when the two operands differ in type (C standard 6.3.1.8(1)):

Otherwise [signedness differs],  if  the  operand  that  has  unsigned  integer  type  has  rank  greater  or
  equal  to  the  rank  of  the  type  of  the  other  operand,  then  the  operand  with
  signed  integer  type  is  converted  to  the  type  of  the  operand  with  unsigned
  integer type.

So if iStack20 is < 5, then the result will be very large (somewhere near UINT_MAX) , hence the comparison

```<code>iStack20 - 5U < 2
</code>```

will be false. If iStack20 is > 6, then the result will be greater than 2 anyway. So the program logic is preserved.

Body:
I am running on linux 5.4.18-1-MANJARO with gcc version 9.2.0 (GCC)

Filename:a.c

#include<stdio.h>

int
main(void) {
    int a;

    scanf("%d", &a);

    if (a < 5 || a > 6)
        puts("fail");
    else
        puts("succeed");
}

Then I run:

gcc a.c -O0 -o a.out
gcc a.c -O1 -o b.out

I decompile a.out with r2, and I got this

undefined8 main(void)
{
    undefined8 uVar1;
    int64_t in_FS_OFFSET;
    int32_t var_ch;
    int64_t canary;

    canary = *(int64_t *)(in_FS_OFFSET + 0x28);
    sym.imp.__isoc99_scanf(0x2004, &var_ch);
    if ((var_ch < 5) || (6 < var_ch)) {
        sym.imp.puts("fail");
    } else {
        sym.imp.puts("succeed");
    }
    uVar1 = 0;
    if (canary != *(int64_t *)(in_FS_OFFSET + 0x28)) {
        uVar1 = sym.imp.__stack_chk_fail();
    }
    return uVar1;
}

This is what I expected.

But when I decompile b.out, I got this

undefined8 main(void)
{
    undefined8 uVar1;
    undefined8 extraout_RDX;
    int64_t iVar2;
    int32_t *piVar3;
    uint32_t uVar4;
    int64_t in_FS_OFFSET;
    int32_t iStack20;
    int64_t iStack16;

    iStack16 = *(int64_t *)(in_FS_OFFSET + 0x28);
    piVar3 = &iStack20;
    sym.imp.__isoc99_scanf(0x2004, piVar3);
    if (iStack20 - 5U < 2) {
        uVar4 = 0x200c;
        sym.imp.puts("succeed");
    } else {
        uVar4 = 0x2007;
        sym.imp.puts("fail");
    }
    if (iStack16 != *(int64_t *)(in_FS_OFFSET + 0x28)) {
        sym.imp.__stack_chk_fail();
        sym._init();
        iVar2 = 0;
        do {
            uVar1 = (**(code **)(segment.LOAD3 + iVar2 * 8))((uint64_t)uVar4, piVar3, extraout_RDX);
            iVar2 = iVar2 + 1;
        } while (iVar2 != 1);
        return uVar1;
    }
    return 0;
}

which seems only check if iStack20 < 7, and b.out runs fine.

I don't understand how it works.

Body:
Here, an integer underflow is exploited. The operation

iStack20 - 5U

is performed unsigned due to the implicit conversion rules when the two operands differ in type (C standard 6.3.1.8(1)):

Otherwise [signedness differs],  if  the  operand  that  has  unsigned  integer  type  has  rank  greater  or
  equal  to  the  rank  of  the  type  of  the  other  operand,  then  the  operand  with
  signed  integer  type  is  converted  to  the  type  of  the  operand  with  unsigned
  integer type.

So if iStack20 is < 5, then the result will be very large (somewhere near UINT_MAX) , hence the comparison

iStack20 - 5U < 2

will be false. If iStack20 is > 6, then the result will be greater than 2 anyway. So the program logic is preserved.


问:
Unknown property cdc
when I set CDC to true on a table using cqlsh it gives me an error

Unknown property CDC

do you have any idea how can solve this error?

答: Cassandra implemented CDC from 3.8 and you should upgrade your Cassandra installation.

Title: Unknown property cdc


问:
Docker container can't access mapped directory from host
I'm running grafana and influxdb containers in a CentOS environment, but when I start the containers, both says that can't create the files on the mapped directories.

I have the same very environment running on Ubuntu with no troubles.

I have already chown'ed the directory to 472:472
I have mapped in docker-compose.yml the user Id of the owner (in ubuntu it solved the problem)
I'm running out of ideas!

Here is the command to start the containers

```<code>CURRENT_UID=$(id -u):$(id -g) docker-compose -f /home/rastreamento/docker-compose.yml up -d
</code>```

Here is the yml for docker-composer that works fine on ubuntu:

```<code>version: '3'

services:
  grafana:
    image: "grafana/grafana"
    environment:
     - WORKING_DIR=/var/lib/grafana/working_dir
     - GF_SECURITY_DISABLE_GRAVATAR=true
     - GF_DEFAULT_INSTANCE_NAME=Valid
     - GF_SERVER_DOMAIN=200.166.40.21
     - GF_SERVER_ENFORCE_DOMAIN=false
     - GF_SERVER_ROOT_URL=http://200.166.40.21
     - GF_ANALYTICS_CHECK_FOR_UPDATES=false
     - GF_USERS_ALLOW_SIGN_UP=false
     - GF_USERS_VIEWERS_CAN_EDIT=false
    ports:
     - "1982:3000"
    volumes:
     - /home/dev/grafana:/var/lib/grafana
    links:
     - influxdb
    networks:
      backend:
        ipv4_address: 172.20.0.3
    restart: unless-stopped
    depends_on:
     - influxdb
    user: ${CURRENT_UID}
  influxdb:
    image: "influxdb"
    environment:
     - INFLUXDB_ADMIN_ENABLED=true
     - INFLUXDB_DATA_MAX_SERIES_PER_DATABASE=0
     - INFLUXDB_DATA_MAX_VALUES_PER_TAG=0
    ports:
     - "8086:8086"
     - "8083:8083"
    volumes:
     - /home/dev/influxdb:/var/lib/influxdb
    networks:
      backend:
        ipv4_address: 172.20.0.2
    restart: unless-stopped
    user: ${CURRENT_UID}
networks:
  backend:
    driver: bridge
    ipam:
      config:
      - subnet: 172.20.0.0/24
</code>```

Here is one output from the container running Grafana.

grafana_1   | mkdir: cannot create directory
  '/var/lib/grafana/plugins': Permission denied

InfluxDB output:

influxdb_1  | run: create server: mkdir all: mkdir
  /var/lib/influxdb/meta: permission denied

I'm out of ideas now after 2 days of research to solve it.

thanks

EDIT 1:

As asked by Jan Garaj, the Docker information follows below:
ps. I changed the wished host mapping to enable the container to run and access it's bash.

docker info

```<code>[rastreamento@VMINDUS ~]$ docker info
Containers: 30
 Running: 1
 Paused: 0
 Stopped: 29
Images: 2
Server Version: 1.13.1
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current
containerd version:  (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: 9c3c5f853ebf0ffac0d087e94daef462133b69c7 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
 seccomp
  WARNING: You're not using the default seccomp profile
  Profile: /etc/docker/seccomp.json
 selinux
Kernel Version: 3.10.0-1062.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 2
Total Memory: 3.666 GiB
Name: VMINDUS
ID: ZOZE:KEGT:SKTP:SG5J:3DKD:LCT3:STRW:FDO4:SFDP:FJJK:IAKQ:72DW
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Registries: docker.io (secure)
</code>```

Filesystem permissions

```<code>[rastreamento@VMINDUS ~]$ ls -l
total 12
-rwxrwxrwx. 1 rastreamento docker 1221 Feb 11 15:05 docker-compose.yml
drwxr-xr-x. 2 root         root      6 Feb 11 15:54 grafana
drwxr-xr-x. 2 root         root      6 Feb 11 15:54 influxdb
-rwxrwxrwx. 1 rastreamento docker  116 Feb 17 14:46 start_containers.sh
-rwxrwxrwx. 1 rastreamento docker   20 Feb 22  2019 stop_containers.sh
</code>```

ID from Grafana

```<code>[rastreamento@VMINDUS ~]$ docker exec -it 691c907ed1f1 /bin/bash
grafana@691c907ed1f1:/usr/share/grafana$ id
uid=472(grafana) gid=472(grafana) groups=472(grafana)
</code>```

For a matter of testing I started Grafana docker mapping the user to root and the directory /home/grafana as follows:

```<code>[rastreamento@VMINDUS ~]$ sudo docker run --user 0:0 -p 3000:3000 --volume /home/rastreamento/grafana:/home/grafana/ d0454da13c84
</code>```

In the container shell I checked the id running the instance and tried to list the files in the /home/grafana directory

```<code>grafana@2263170898d0:/var/lib$ [rastreamento@VMINDUS ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED    STATUS              PORTS                    NAMES
345145c9a591        d0454da13c84        "/run.sh"           7 seconds ago       Up 4 seconds        0.0.0.0:3000->3000/tcp   heuristic_wescoff
[rastreamento@VMINDUS ~]$ docker exec -it 345145c9a591 /bin/bash
root@345145c9a591:/usr/share/grafana# cd /home/grafana/
root@345145c9a591:/home/grafana# ls
ls: cannot open directory '.': Permission denied
root@345145c9a591:/home/grafana# id
uid=0(root) gid=0(root) groups=0(root)
</code>```

As you can see, the user and group Ids seems to have been changed as it's supposed to be when the container was started, but even though, I can't get access to the host mapped directory.

Still no clues of why it works on Ubuntu but it doesn't in CentOs. I've never used CentOs, is there any other configuration to enable applications to access the host resources? Some security configuration? I'm really having a hard time with it.

答:
As assigned by Avi Farada in this post, adding a volume bind solved my problem.

:z - will add permissions to all containers using label 'svirt_sandbox_file_t'
:Z - will add permissions only to the current container label

Just add to docker run ':z' like this:

```<code>docker run -v ./api:/usr/src/app:Z
</code>```

or in my case, in docker-composer:

```<code>volumes:
   - ./api:/usr/src/app:Z
</code>```

More information on this check the docker official documentation.

Another clever suggestion was addressed by Jan Garaj, and the different behaviour I had between the two machines was probably due to a version mismatch. I used the CentOS package manager to install docker, while in the other hand, on Ubuntu I downloaded the latest version from the official repo.

[ edit ]

I used the Docker from the package repo of CentOS. That was completely outdated!!!

On Docker official installation guide, the first thing they do is uninstall the stock docker on the machine, in case you've might done the same dumb thing I did.

Just install Docker by following the instructions on:

https://docs.docker.com/engine/install/centos/

Body:
I'm running grafana and influxdb containers in a CentOS environment, but when I start the containers, both says that can't create the files on the mapped directories.

I have the same very environment running on Ubuntu with no troubles.

I have already chown'ed the directory to 472:472
I have mapped in docker-compose.yml the user Id of the owner (in ubuntu it solved the problem)
I'm running out of ideas!

Here is the command to start the containers

CURRENT_UID=$(id -u):$(id -g) docker-compose -f /home/rastreamento/docker-compose.yml up -d

Here is the yml for docker-composer that works fine on ubuntu:

version: '3'

services:
  grafana:
    image: "grafana/grafana"
    environment:
     - WORKING_DIR=/var/lib/grafana/working_dir
     - GF_SECURITY_DISABLE_GRAVATAR=true
     - GF_DEFAULT_INSTANCE_NAME=Valid
     - GF_SERVER_DOMAIN=200.166.40.21
     - GF_SERVER_ENFORCE_DOMAIN=false
     - GF_SERVER_ROOT_URL=http://200.166.40.21
     - GF_ANALYTICS_CHECK_FOR_UPDATES=false
     - GF_USERS_ALLOW_SIGN_UP=false
     - GF_USERS_VIEWERS_CAN_EDIT=false
    ports:
     - "1982:3000"
    volumes:
     - /home/dev/grafana:/var/lib/grafana
    links:
     - influxdb
    networks:
      backend:
        ipv4_address: 172.20.0.3
    restart: unless-stopped
    depends_on:
     - influxdb
    user: ${CURRENT_UID}
  influxdb:
    image: "influxdb"
    environment:
     - INFLUXDB_ADMIN_ENABLED=true
     - INFLUXDB_DATA_MAX_SERIES_PER_DATABASE=0
     - INFLUXDB_DATA_MAX_VALUES_PER_TAG=0
    ports:
     - "8086:8086"
     - "8083:8083"
    volumes:
     - /home/dev/influxdb:/var/lib/influxdb
    networks:
      backend:
        ipv4_address: 172.20.0.2
    restart: unless-stopped
    user: ${CURRENT_UID}
networks:
  backend:
    driver: bridge
    ipam:
      config:
      - subnet: 172.20.0.0/24

Here is one output from the container running Grafana.

grafana_1   | mkdir: cannot create directory
  '/var/lib/grafana/plugins': Permission denied

InfluxDB output:

influxdb_1  | run: create server: mkdir all: mkdir
  /var/lib/influxdb/meta: permission denied

I'm out of ideas now after 2 days of research to solve it.

thanks

EDIT 1:

As asked by Jan Garaj, the Docker information follows below:
ps. I changed the wished host mapping to enable the container to run and access it's bash.

docker info

[rastreamento@VMINDUS ~]$ docker info
Containers: 30
 Running: 1
 Paused: 0
 Stopped: 29
Images: 2
Server Version: 1.13.1
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current
containerd version:  (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: 9c3c5f853ebf0ffac0d087e94daef462133b69c7 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
 seccomp
  WARNING: You're not using the default seccomp profile
  Profile: /etc/docker/seccomp.json
 selinux
Kernel Version: 3.10.0-1062.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 2
Total Memory: 3.666 GiB
Name: VMINDUS
ID: ZOZE:KEGT:SKTP:SG5J:3DKD:LCT3:STRW:FDO4:SFDP:FJJK:IAKQ:72DW
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false
Registries: docker.io (secure)

Filesystem permissions

[rastreamento@VMINDUS ~]$ ls -l
total 12
-rwxrwxrwx. 1 rastreamento docker 1221 Feb 11 15:05 docker-compose.yml
drwxr-xr-x. 2 root         root      6 Feb 11 15:54 grafana
drwxr-xr-x. 2 root         root      6 Feb 11 15:54 influxdb
-rwxrwxrwx. 1 rastreamento docker  116 Feb 17 14:46 start_containers.sh
-rwxrwxrwx. 1 rastreamento docker   20 Feb 22  2019 stop_containers.sh

ID from Grafana

[rastreamento@VMINDUS ~]$ docker exec -it 691c907ed1f1 /bin/bash
grafana@691c907ed1f1:/usr/share/grafana$ id
uid=472(grafana) gid=472(grafana) groups=472(grafana)

For a matter of testing I started Grafana docker mapping the user to root and the directory /home/grafana as follows:

[rastreamento@VMINDUS ~]$ sudo docker run --user 0:0 -p 3000:3000 --volume /home/rastreamento/grafana:/home/grafana/ d0454da13c84

In the container shell I checked the id running the instance and tried to list the files in the /home/grafana directory

grafana@2263170898d0:/var/lib$ [rastreamento@VMINDUS ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED    STATUS              PORTS                    NAMES
345145c9a591        d0454da13c84        "/run.sh"           7 seconds ago       Up 4 seconds        0.0.0.0:3000->3000/tcp   heuristic_wescoff
[rastreamento@VMINDUS ~]$ docker exec -it 345145c9a591 /bin/bash
root@345145c9a591:/usr/share/grafana# cd /home/grafana/
root@345145c9a591:/home/grafana# ls
ls: cannot open directory '.': Permission denied
root@345145c9a591:/home/grafana# id
uid=0(root) gid=0(root) groups=0(root)

As you can see, the user and group Ids seems to have been changed as it's supposed to be when the container was started, but even though, I can't get access to the host mapped directory.

Still no clues of why it works on Ubuntu but it doesn't in CentOs. I've never used CentOs, is there any other configuration to enable applications to access the host resources? Some security configuration? I'm really having a hard time with it.

Body:
As assigned by Avi Farada in this post, adding a volume bind solved my problem.

:z - will add permissions to all containers using label 'svirt_sandbox_file_t'
:Z - will add permissions only to the current container label

Just add to docker run ':z' like this:

docker run -v ./api:/usr/src/app:Z

or in my case, in docker-composer:

volumes:
   - ./api:/usr/src/app:Z

More information on this check the docker official documentation.

Another clever suggestion was addressed by Jan Garaj, and the different behaviour I had between the two machines was probably due to a version mismatch. I used the CentOS package manager to install docker, while in the other hand, on Ubuntu I downloaded the latest version from the official repo.

[ edit ]

I used the Docker from the package repo of CentOS. That was completely outdated!!!

On Docker official installation guide, the first thing they do is uninstall the stock docker on the machine, in case you've might done the same dumb thing I did.

Just install Docker by following the instructions on:

https://docs.docker.com/engine/install/centos/


问:
Python Beautifulsoup Scraper not scraping images
I have the below code:-

```<code>import requests
from bs4 import BeautifulSoup

url = ""

page = requests.get(url, headers=HEADERS, verify=True)
soup = BeautifulSoup(page.content, 'html.parser')
images = soup.find_all("img")
for image in images:
    image_url = image['src']
    print(image_url)
    if url not in image_url:  #This is to identify absolute/relative links
        link = urllib.parse.urljoin(url, image_url)
    else:
        link = image_url
</code>```

I am working my way through thousands of URLs that I have agreement to scrape images from. Nonetheless, the URLs for some images aren't being detected. An example of a URL that returns no links in below below:-

https://www.lerryn-cornwall.co.uk/

Checking the source view, there are image links such as:-

```<code>link rel="apple-touch-icon" href="https://primarysite-prod.s3.amazonaws.com/theme/LerrynCofEPrimarySchool/apple-touch-icon-precomposed.png">
</code>```

Any suggestions on handling every possible eventuality of image links in thousands of web pages? I'm thinking of instead of looking for the img['src'] tag, I simply process 'soup' and look for any strings containing '.png' for example and then append these to a list. Is there a better means?

答: As @johannchopin said, you should scrape css files of each website too, you can find the css file url via the html by scraping link tags, then you need to search in the css file string the url for each image, although this url might be stored for example in the same directory as the css file or not, you should keep in mind that in the css file you'll only receieve the location of the file according to where the css file is stored. If the css file is stored in a seperate folder the location of the img file will look like this "../img/someimg.png" the ".." sign means upper directory, like you go back on explorer, it basically means switching directory to the parent directory of the current file. I hope it helped you.</p>

Body:
I have the below code:-

import requests
from bs4 import BeautifulSoup

url = ""

page = requests.get(url, headers=HEADERS, verify=True)
soup = BeautifulSoup(page.content, 'html.parser')
images = soup.find_all("img")
for image in images:
    image_url = image['src']
    print(image_url)
    if url not in image_url:  #This is to identify absolute/relative links
        link = urllib.parse.urljoin(url, image_url)
    else:
        link = image_url

I am working my way through thousands of URLs that I have agreement to scrape images from. Nonetheless, the URLs for some images aren't being detected. An example of a URL that returns no links in below below:-

https://www.lerryn-cornwall.co.uk/

Checking the source view, there are image links such as:-

link rel="apple-touch-icon" href="https://primarysite-prod.s3.amazonaws.com/theme/LerrynCofEPrimarySchool/apple-touch-icon-precomposed.png">

Any suggestions on handling every possible eventuality of image links in thousands of web pages? I'm thinking of instead of looking for the img['src'] tag, I simply process 'soup' and look for any strings containing '.png' for example and then append these to a list. Is there a better means?


问:
Is there a way to use Google App Script to navigate between the thumbnails in the Slides filmstrip?
Is there a way to use Google App Script to change from one slide to another in a presentation while editing it?</p>

答:
Unfortunately what you want cannot be achieved because there are no onEdit() triggers available for Google Slides. The only types of triggers available are:

onOpen() triggers;

onInstall() triggers;

time-driven triggers;

What you can do instead is to file a Feature Request on Issue Tracker here and provide all the necessary details.

Reference

Apps Script Triggers


问:
How to use foreign keys in SQL Buddy?
I need to use foreign keys for update and cascade, etc.

```<code>ALTER TABLE topics
  ADD FOREIGN KEY(topic_by) REFERENCES users(user_id)
  ON DELETE RESTRICT ON UPDATE CASCADE;
</code>```

but I am not able to make foreign keys in SQL Buddy.

Any way to do that?

答:
Try this query:</p>

```<code>ALTER TABLE topics
ADD CONSTRAINT topic_by
FOREIGN KEY (user_id) REFERENCES users(user_id);
</code>```

Title: How to use foreign keys in SQL Buddy?

Body:
I need to use foreign keys for update and cascade, etc.

ALTER TABLE topics
  ADD FOREIGN KEY(topic_by) REFERENCES users(user_id)
  ON DELETE RESTRICT ON UPDATE CASCADE;

but I am not able to make foreign keys in SQL Buddy.

Any way to do that?

Body:
Try this query:

ALTER TABLE topics
ADD CONSTRAINT topic_by
FOREIGN KEY (user_id) REFERENCES users(user_id);


问:
SQL limiting the output to a number of results
I have this query.

SELECT * FROM Customer WHERE TAGID IN ('UK', 'Germany');

This outputs the results to show all customers in UK and Germany, but as you can imagine, there will be loads of outputs, and I just require it to output 2 results for UK and 2 results for Germany?

Is this possible?

答:
UNION ALL comes to mind:

```<code>(SELECT c.*
 FROM Customer c
 WHERE TAGID = 'UK'
 LIMIT 2
) UNION ALL
(SELECT c.*
 FROM Customer c
 WHERE TAGID = 'Germany'
 LIMIT 2
);
</code>```

Note that this can take advantage of an index onCustomer(TagID).  Also, this returns two arbitrary rows.  You can use an ORDER BY to return the newest, oldest, biggest, smallest, reddest, bluest or whatever.

Here is a db<>fiddle.

Body:
UNION ALL comes to mind:

(SELECT c.*
 FROM Customer c
 WHERE TAGID = 'UK'
 LIMIT 2
) UNION ALL
(SELECT c.*
 FROM Customer c
 WHERE TAGID = 'Germany'
 LIMIT 2
);

Note that this can take advantage of an index onCustomer(TagID).  Also, this returns two arbitrary rows.  You can use an ORDER BY to return the newest, oldest, biggest, smallest, reddest, bluest or whatever.

Here is a db<>fiddle.


问:
"ModuleNotFoundError: No module named 'pycocotools'" on google cloud compute
When I run the object detection API for custom dataset and initialize on google cloud compute. I got "no module error".
I have all the module installed in conda env, then why the AI training didnt access the env python3.6 and its moduleenter image description here
I have been through google cloud platform' initial documentation, but no material on this.

```<code>gcloud ai-platform jobs submit training object_detection_`date +%m_%d_%Y_%H_%M_%S` \
    --job-dir=gs://spike_detector/train \
    --packages /home/sajid/phd/spike_detector/models/research/dist/object_detection-0.1.tar.gz,/home/sajid/phd/spike_detector/models/research/slim/dist/slim-0.1.tar.gz \
    --module-name  object_detection.model_main \
    --region us-central1 \
    --python-version 3.7 \
    --runtime-version 1.15
    --config /home/sajid/phd/spike_detector/spike_detector_gscloud/training.yaml \
    -- \
    --train_dir=gs://spike_detector/train \
    --pipeline_config_path=gs://spike_detector/data/faster_rcnn_resnet101_pets.config
</code>```

I got error ModuleNotFoundError: No module named 'pycocotools'

```<code>ERROR   2020-02-16 17:43:06 +0500   master-replica-0            from pycocotools import coco[enter image description here][2]
ERROR   2020-02-16 17:43:06 +0500   master-replica-0        ModuleNotFoundError: No module named
</code>```

答:
AI Platform runs its jobs in virtual machines specially configured for training jobs, so each dependency your job uses should be installed in those machines (having them on your local machine is not enough). Those virtual machines come with many packages already installed which can be checked here.

However pycocotools is not installed by default and you should explicitly instruct AI Platform to install it by specifying it in the job's setup.py file as explained here. This will download and install the dependency on the virtual machines using pip install.

Another way to install dependencies is to reference packages located in your local machine that will be staged in the cloud and then installed into the training machines, using the --packages flag as explained here. I see that you are already utilizing this feature to import other packages; you could try adding to it the location of the package pycocotools.

I hope that this can help you solve the error!

Body:
When I run the object detection API for custom dataset and initialize on google cloud compute. I got "no module error".
I have all the module installed in conda env, then why the AI training didnt access the env python3.6 and its moduleenter image description here
I have been through google cloud platform' initial documentation, but no material on this.

gcloud ai-platform jobs submit training object_detection_`date +%m_%d_%Y_%H_%M_%S` \
    --job-dir=gs://spike_detector/train \
    --packages /home/sajid/phd/spike_detector/models/research/dist/object_detection-0.1.tar.gz,/home/sajid/phd/spike_detector/models/research/slim/dist/slim-0.1.tar.gz \
    --module-name  object_detection.model_main \
    --region us-central1 \
    --python-version 3.7 \
    --runtime-version 1.15
    --config /home/sajid/phd/spike_detector/spike_detector_gscloud/training.yaml \
    -- \
    --train_dir=gs://spike_detector/train \
    --pipeline_config_path=gs://spike_detector/data/faster_rcnn_resnet101_pets.config

I got error ModuleNotFoundError: No module named 'pycocotools'

ERROR   2020-02-16 17:43:06 +0500   master-replica-0            from pycocotools import coco[enter image description here][2]
ERROR   2020-02-16 17:43:06 +0500   master-replica-0        ModuleNotFoundError: No module named


问:
google-cloud-sql - max_allowed_packet
I was moving a database to using Google Cloud SQL which previously had a max_allowed_packet of 20M.

Currently the Google Cloud SQL default for max_allowed_packet is 1M.

Is there any way to increase this variable to 20M? I have already tried the following:

```<code>set global max_allowed_packet = 20971520;
</code>```

Which returns:

Error Code: 1621. SESSION variable 'max_allowed_packet' is read-only. Use SET GLOBAL to assign the value

and then:

```<code>set global max_allowed_packet = 20971520;
</code>```

This returns the error:

Error Code: 1227. Access denied; you need (at least one of) the SUPER privilege(s) for this operation

Thank you in advance for your help!

答: In my case I couldn't update the max_allowed_packet setting because I had a flag of sql_mode=TRADITIONAL which expects the value to be a multiple of 1024.

Title: google-cloud-sql - max_allowed_packet

Body:
I was moving a database to using Google Cloud SQL which previously had a max_allowed_packet of 20M.

Currently the Google Cloud SQL default for max_allowed_packet is 1M.

Is there any way to increase this variable to 20M? I have already tried the following:

set global max_allowed_packet = 20971520;

Which returns:

Error Code: 1621. SESSION variable 'max_allowed_packet' is read-only. Use SET GLOBAL to assign the value

and then:

set global max_allowed_packet = 20971520;

This returns the error:

Error Code: 1227. Access denied; you need (at least one of) the SUPER privilege(s) for this operation

Thank you in advance for your help!


问:
How are array elements stored when the array has null elements?
I know this is basic but I'm not finding sources that confirms my interpretation.

The goal is to understand how arrays are stored in memory, below you will find my interpretation.

If a 4 size Array is like:

```<code>array[0] = 0

array[1] = null

array[2] = 2

array[3] = 3
</code>```

Is it stored adjacently in memory without counting nulls, like this for instance?

```<code>[0], [2], [3]
</code>```

if you add an element in a null position ([1]), than the next elements are pulled 1 position in memory?

[0], [1] (New), [2] (Pulled), [3] (Pulled)

References (Amoung others):

Arrays in Java

答:
A null is generally represented in memory by all the bits being zero. That is true regardless of whether the null is stored in an array or anywhere else.

I think there is some confusion in your question regarding what an array is. An array is a fixed-length sequence where every element in the sequence has the same type. The length of the array is determined when the array is created, and the length cannot change later (though you can create a new array with a different length if necessary).

The above is what you'll get from any textbook, but keep in mind that some languages like Javascript and PHP use the word "array" to mean things that are not arrays. A Javascript "array" is really a list, and a PHP "array" is really a strange hybrid between a list and an associative array. A Python list is not an array, though some sources mistakenly refer to it as one. These other data structures all allow you to insert an element at an index, but "insert an element" is not an operation on an array, because that would require increasing the length of the array by 1. The only operations on arrays are to get the value at an index, set the value at an index, and (if it's a length-prefixed array) get the array's length. You can use an array to represent a variable-length sequence, but you shouldn't confuse the array for what it represents.

There is no special treatment of null with regard to memory locations; if the array has a null at index 1, then the memory location corresponding to index 1 contains the memory representation of null. The whole point of an array is that each index corresponds with a memory location, so you can use the index to find the memory location directly. The formula for the memory address of an array element is:

(address for array[index]) = (address of array start) + index * (bytes per array element)

If the element at index 1 didn't take up a memory location, then the element at index 2 wouldn't be in the right place according to this formula.

The natural follow-up question is, if null in an array is represented in memory the same way as the number 0, how can the program tell whether the value is null or 0?

Since all the array elements have to be the same type of thing, null in an array only makes sense if the array elements are references, rather than primitive integers. For example, in Java you could have null in an Integer[] array but not an int[] array. So the zero bits representing a null would not be confused with the zero bits representing the number 0, because anyone reading an array of references is supposed to interpret the contents as references, not as integers; and anyone reading an array of primitive integers should interpret those bits as the number 0, not a null reference.

Body:
I know this is basic but I'm not finding sources that confirms my interpretation.

The goal is to understand how arrays are stored in memory, below you will find my interpretation.

If a 4 size Array is like:

array[0] = 0

array[1] = null

array[2] = 2

array[3] = 3

Is it stored adjacently in memory without counting nulls, like this for instance?

[0], [2], [3]

if you add an element in a null position ([1]), than the next elements are pulled 1 position in memory?

[0], [1] (New), [2] (Pulled), [3] (Pulled)

References (Amoung others):

Arrays in Java


问:
CUDA convolutionFFT2D example - I can't understand it
I studied the Cooley Tukey algorithm and I understood it. I got everything in the CUDA convolutionFFT2D example till these kernels:

spProcess2D calls -> spProcess2D_kernel which calls a lot of -> spPostprocessC2C, mulAndScale and spPreprocessC2C

Here's the complete code:
http://nopaste.info/30c13e44fe.html (convolutionFFT2D.cu, here is the spProcess2D function)
http://nopaste.info/78d22afac2.html (convolutionFFT2D.cuh, here are the other functions)

I already read all the nvidia sdk papers but I can't still figure out what these function do (they use twiddles, but nothing seems like a Cooley Tukey algorithm there)

Please help me if you can, or at least point me out where to solve my problem

Update: I found this link: http://cnx.org/content/m16336/latest/#uid38
Maybe these functions are performing a breadth-first algorithm? I still can't say that but the shape seems the same

答:
spPostprocessC2C looks like a single FFT butterfly. The complexity in the calling routines just comes from fitting the FFT algorithm into a SIMT model for CUDA.

Perhaps if you explained what it is that you are trying to achieve (beyond just understanding how this particular FFT implementation works) then you might get some more specific answers.


问:
Passing variable from type std::function fails although the caller expect the exact same type
for some reason I cannot pass value to method with the exact type.

But if I cast the variable I wish to pass using decltype of itself, it somehow works (?)
Perhaps anybody know what's the reason.

Also, When I pass the lambda block to std::function it doesn't require any casting, although they're not exactly the same.

the following example demonstrate my findings :

```<code>#include <functional>

class A {
public:
    A(std::function<void(std::string&, int)> &&f): _f(f) { }

    std::function<void(std::string&,int)> _f;
};

class B : public A {
public:
    //B(std::function<void(std::string&, int)> &&f) : A(f) { } --> fail on casting error, why ?
    B(std::function<void(std::string&, int)> &&f) : A((decltype(f)) f) { }
};

class C {
public:
    C(std::function<void(std::string&,int)> f) {
        //b = new B(f); --> this one trigger casting error, why ?
        b = new B((decltype(f)) f);
    }
    B *b;
};

int main(int argc, const char * argv[]) {
    // this works for some reason, even though C c'tor expect std::function ... why ?
    C c([=](std::string&a, int b) {
        printf("%s %d\n", a.c_str(), b);
    });
}

</code>```

答:
in:
B(std::function<void(std::string&, int)> &&f) : A((decltype(f)) f) { }

as f has a name, it is a lvalue, but A's constructor expects a rvalue.

(decltype(f)) f here is equivalent to std::move(f).

Body:
for some reason I cannot pass value to method with the exact type.

But if I cast the variable I wish to pass using decltype of itself, it somehow works (?)
Perhaps anybody know what's the reason.

Also, When I pass the lambda block to std::function it doesn't require any casting, although they're not exactly the same.

the following example demonstrate my findings :

#include <functional>

class A {
public:
    A(std::function<void(std::string&, int)> &&f): _f(f) { }

    std::function<void(std::string&,int)> _f;
};

class B : public A {
public:
    //B(std::function<void(std::string&, int)> &&f) : A(f) { } --> fail on casting error, why ?
    B(std::function<void(std::string&, int)> &&f) : A((decltype(f)) f) { }
};

class C {
public:
    C(std::function<void(std::string&,int)> f) {
        //b = new B(f); --> this one trigger casting error, why ?
        b = new B((decltype(f)) f);
    }
    B *b;
};

int main(int argc, const char * argv[]) {
    // this works for some reason, even though C c'tor expect std::function ... why ?
    C c([=](std::string&a, int b) {
        printf("%s %d\n", a.c_str(), b);
    });
}


问:
Highmap Bubble exclude points not on map
Have a dataset of global data with lat/lon info and can display a map of type 'mapbubble.'

Say, however, I only want to display one country. Currently if I use this dataset and display with say, Canada or China, the image of the country shrinks so as to allow all the points -- from all over the world -- to display.

Is there a way to tell Highmap to not display data that is not within the bounds of the current map?

Thanks!

Update: adding sample code. Note that the 4th point is the north pole and not part of China, yet it is displayed (oddly, far to the left and with the bubble not AFAIK the right size).

```<code><html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.6/proj4.js">
</script>
<script src="https://code.highcharts.com/maps/highmaps.js"></script>
<script src="https://code.highcharts.com/maps/modules/data.js"></script>
<script src="https://code.highcharts.com/maps/modules/exporting.js"></script>
<script src="https://code.highcharts.com/maps/modules/offline-exporting.js">
</script>
 <script src="https://code.highcharts.com/mapdata/countries/us/us-all.js">
</script>
 <script src="https://code.highcharts.com/mapdata/countries/cn/cn-all.js">
</script>

 <style>
     #container {
        height: 500px;
        min-width: 310px;
        max-width: 800px;
        margin: 0 auto;
      }
     .loading {
       margin-top: 10em;
       text-align: center;
       color: gray;
 }</style>

 <div id="container"></div>
 </head>

 <body>
 <script>
 var H = Highcharts,
        map = H.maps['countries/cn/cn-all'],
       chart;
 var data =  [
    {
        lat:39,
        lon:80,
        z:500
    },
    [{
        lat:31,
        lon:117,
        z:500
    }],
    {
        lat:39,
        lon:82,
        z:600
    },
    {
        lat:0,
        lon:0,
        z:500
    }

];

chart = Highcharts.mapChart('container', {
    title: {
        text: 'Highmaps lat/lon demo'
    },

    series: [{
        name: 'Basemap',
        mapData: map,
        borderColor: '#606060',
        nullColor: 'rgba(200, 200, 200, 0.2)',
        showInLegend: false
    },
    {
        name: "test",
        type:"mapbubble",
        minSize: 20,
        maxSize: '30%',
        data: data,
        color: 'rgba(200,0,0,0.5)'
    }]

});
console.log(chart);

</script>
</code>```

答:
You can set min and max properties for axes:

```<code>xAxis: {
  min: -999,
  max: 9851
},

yAxis: {
  min: -9851,
  max: -927
}
</code>```

Live demo: http://jsfiddle.net/BlackLabel/6m4e8x0y/4876/

API Reference:

https://api.highcharts.com/highmaps/xAxis.min

https://api.highcharts.com/highmaps/yAxis.max

Body:
You can set min and max properties for axes:

xAxis: {
  min: -999,
  max: 9851
},

yAxis: {
  min: -9851,
  max: -927
}

Live demo: http://jsfiddle.net/BlackLabel/6m4e8x0y/4876/

API Reference:

https://api.highcharts.com/highmaps/xAxis.min

https://api.highcharts.com/highmaps/yAxis.max


问:
SQLAlchemy many-to-many relationship with SHARED composite keys
I created a SQLAlchemy many-to-many relationship with composite primary-keys. Therefore also the foreign keys between the three tables are composite. One of the primary-key columns is shared. It has to be of the same value.

The code I used seems to work, however I do now get warnings that say: relationship 'A.bs' will copy column B.group to column A_B.group, which conflicts with relationship(s): 'A.bs' (copies A.b to A_B.group). Consider applying viewonly=True to read-only relationships, or provide a primaryjoin condition marking writable columns with the foreign() annotation.

```<code>class A(Model):
  group = Column(Int, primary_key=True)
  id = Column(Int, primary_key=True)
  bs = relationship('B', secondary=Table('A_B',
    Column('group', Int, primary_key=True),
    Column('a', Int, primary_key=True),
    Column('b', Int, primary_key=True),
    ForeignKeyConstraint(['group', 'a'], ['A.group', 'A.id']),
    ForeignKeyConstraint(['group', 'b'], ['B.group', 'B.id'])
  ))

class B(Model):
  group = Column(Int, primary_key=True)
  id = Column(Int, primary_key=True)
</code>```

I cannot set the relationship to viewonly. How do I have to modify my code to make it work without the warning?

答:
In addition to providing the primaryjoin with annotations like in the error message, you have to provide the secondaryjoin when making a many to many relationship through a secondary table:

```<code>a_b = Table('A_B',
  Column('group', Integer, primary_key=True),
  Column('a', Integer, primary_key=True),
  Column('b', Integer, primary_key=True),
  ForeignKeyConstraint(['group', 'a'], ['a.group', 'a.id']),
  ForeignKeyConstraint(['group', 'b'], ['b.group', 'b.id'])
)

class A(Model):
  group = Column(Integer, primary_key=True)
  id = Column(Integer, primary_key=True)
  bs = relationship(
    'B', secondary=a_b,
    primaryjoin='and_(A.group == foreign(A_B.c.group), A.id == foreign(A_B.c.a))',
    secondaryjoin='and_(B.group == A_B.c.group, B.id == foreign(A_B.c.b))')

class B(Model):
  group = Column(Integer, primary_key=True)
  id = Column(Integer, primary_key=True)
</code>```

The A_B.c.group is not annotated as foreign() in the secondary join, so SQLAlchemy does not copy it twice. The downside of that is that it is possible to do unexpected things such as:

```<code>In [4]: a1 = A(group=1, id=1)

In [5]: a2 = A(group=2, id=1)

In [6]: session.add_all([a1, a2])

In [7]: b_oops = B(group=1, id=2)

In [8]: session.add(b_oops)

In [9]: a1.bs = [B(group=1, id=1), B(group=2, id=2)]

In [10]: session.commit()

In [11]: b_oops in a1.bs
Out[11]: True
</code>```

So B(group=2, id=2) is inserted to the database, but b_oops gets associated with a1 instead.

Body:
I created a SQLAlchemy many-to-many relationship with composite primary-keys. Therefore also the foreign keys between the three tables are composite. One of the primary-key columns is shared. It has to be of the same value.

The code I used seems to work, however I do now get warnings that say: relationship 'A.bs' will copy column B.group to column A_B.group, which conflicts with relationship(s): 'A.bs' (copies A.b to A_B.group). Consider applying viewonly=True to read-only relationships, or provide a primaryjoin condition marking writable columns with the foreign() annotation.

class A(Model):
  group = Column(Int, primary_key=True)
  id = Column(Int, primary_key=True)
  bs = relationship('B', secondary=Table('A_B',
    Column('group', Int, primary_key=True),
    Column('a', Int, primary_key=True),
    Column('b', Int, primary_key=True),
    ForeignKeyConstraint(['group', 'a'], ['A.group', 'A.id']),
    ForeignKeyConstraint(['group', 'b'], ['B.group', 'B.id'])
  ))

class B(Model):
  group = Column(Int, primary_key=True)
  id = Column(Int, primary_key=True)

I cannot set the relationship to viewonly. How do I have to modify my code to make it work without the warning?

Body:
In addition to providing the primaryjoin with annotations like in the error message, you have to provide the secondaryjoin when making a many to many relationship through a secondary table:

a_b = Table('A_B',
  Column('group', Integer, primary_key=True),
  Column('a', Integer, primary_key=True),
  Column('b', Integer, primary_key=True),
  ForeignKeyConstraint(['group', 'a'], ['a.group', 'a.id']),
  ForeignKeyConstraint(['group', 'b'], ['b.group', 'b.id'])
)

class A(Model):
  group = Column(Integer, primary_key=True)
  id = Column(Integer, primary_key=True)
  bs = relationship(
    'B', secondary=a_b,
    primaryjoin='and_(A.group == foreign(A_B.c.group), A.id == foreign(A_B.c.a))',
    secondaryjoin='and_(B.group == A_B.c.group, B.id == foreign(A_B.c.b))')

class B(Model):
  group = Column(Integer, primary_key=True)
  id = Column(Integer, primary_key=True)

The A_B.c.group is not annotated as foreign() in the secondary join, so SQLAlchemy does not copy it twice. The downside of that is that it is possible to do unexpected things such as:

In [4]: a1 = A(group=1, id=1)

In [5]: a2 = A(group=2, id=1)

In [6]: session.add_all([a1, a2])

In [7]: b_oops = B(group=1, id=2)

In [8]: session.add(b_oops)

In [9]: a1.bs = [B(group=1, id=1), B(group=2, id=2)]

In [10]: session.commit()

In [11]: b_oops in a1.bs
Out[11]: True

So B(group=2, id=2) is inserted to the database, but b_oops gets associated with a1 instead.


问:
I cannot pull branch to my local repository
I want to pull branches to my local repository but I cannot. I tried revert my last commit now all remote branches like that . What is '29e74ac'? Is it can be about references/HEAD ? .  I am using eclipse and trying from team->switch to->other when try to switch branch.

答:
First, You need to go to a known branch (ex: master). Because you are not incorrect branch

```<code>git checkout master
</code>```

If you are trying to pull the newly created branch, you need to fetch

```<code>git fetch
</code>```

then you can see all the branches

```<code>git branch -a
</code>```

Now you can check out whatever branch you want

```<code>git checkout branch_name
</code>```

Then you can get pull using

```<code>git pull origin branch_name
</code>```

Body:
First, You need to go to a known branch (ex: master). Because you are not incorrect branch

git checkout master

If you are trying to pull the newly created branch, you need to fetch

git fetch

then you can see all the branches

git branch -a

Now you can check out whatever branch you want

git checkout branch_name

Then you can get pull using

git pull origin branch_name


问:
run a javascript function for specific css .class selectors
I have a JavaScript library and it uses this function for example to convert English numbers to Persian:

```<code>persianJs("345").englishNumber().toString(); //returns: ۳۴۵
</code>```

I have an html structure to show some dynamic numerical values like this:

```<code><span class="price"><!-- price amount  --></span>
</code>```

How can I run the JavaScript function in header or footer to display any price amount in Persian format?

答:
The following code will convert all the .price span to english numbers on your code base.</p>

```<code>    window.onload = function(){
      var els = document.querySelectorAll('.price');
      els.forEach(function(item) {
        item.textContent = persianJs(item.textContent).englishNumber().toString();
      });
    }
</code>```

Body:
I have a JavaScript library and it uses this function for example to convert English numbers to Persian:

persianJs("345").englishNumber().toString(); //returns: ۳۴۵

I have an html structure to show some dynamic numerical values like this:

How can I run the JavaScript function in header or footer to display any price amount in Persian format?

Body:
The following code will convert all the .price span to english numbers on your code base.

    window.onload = function(){
      var els = document.querySelectorAll('.price');
      els.forEach(function(item) {
        item.textContent = persianJs(item.textContent).englishNumber().toString();
      });
    }


问:
Sticky Sidebar with scroll Angular
I am developing an webapplication with angular, what I wanted to do now is a StickySidebar with a scrollfunction, but right now the sidebar only shows but is not scrolling down. I tried a few things like overvlow=scroll or overflow-y=auto pr cdk-scrollable on the html side, but those things didnt help me, can someone give me an pointer maybe?

CSS

```<code> @media only screen and (min-width: 992px){
  .desktopss {

    overflow: scroll;
    -ms-scrollbar-base-color: #232323;

    &.filter-map-mode, &.screen-height {
      margin-top: -63px;
      padding: 31px 0px 30px 0px;
      overflow: scroll;
    }
    &.filter-map-mode{
      min-height: 627px;
      max-height: calc(100vh - 276px);
      height: 100%;
    }
    &.screen-height{
      height: 100vh;
      padding-bottom: 81px
    }
  }

}

@media only screen and  (max-width: 991px){
  .desktopss{
    margin-top: 0px !important;
  }
  .filter-map-mode{
    height: unset;
    overflow: unset;

  }
  .filter-map-mode::-webkit-scrollbar{
    display: unset;
  }
}
</code>```

HTML

```<code><div class="sticky-gate">
        <div class="sticky-scrollable" cdk-scrollable>
          <div class="desktop-filter">
            <div class="filter-container" *ngIf="isDesktopView">
              <div class="filter-wrap" (mouseenter)="cursorOnFilter = true; " (mouseleave)="cursorOnFilter = false">

                <landwirtde1-farm-list-filter-ui [filter]="filter" [mapMode]="mapIsViewing"  [coverScreenHeight]="true"
                                                 [filterScrollWhenMap]="filterScrollWhenMap" (onScrolledInFilterUIAtMap)="filterScrollWhenMap = $event"
                                                 (onScrollYMaxIsDetermined)="filterScrollMax = $event">
                </landwirtde1-farm-list-filter-ui>
              </div>
            </div>
          </div>
        </div>
      </div>
</code>```

答:
I would suggest to give it a try with position: sticky CSS property.

This feature is widely supported by the major browsers:
https://caniuse.com/#feat=css-sticky

More info from the Mozilla documentation.

Title: Sticky Sidebar with scroll Angular

Body:
I am developing an webapplication with angular, what I wanted to do now is a StickySidebar with a scrollfunction, but right now the sidebar only shows but is not scrolling down. I tried a few things like overvlow=scroll or overflow-y=auto pr cdk-scrollable on the html side, but those things didnt help me, can someone give me an pointer maybe?

CSS

 @media only screen and (min-width: 992px){
  .desktopss {

    overflow: scroll;
    -ms-scrollbar-base-color: #232323;

    &.filter-map-mode, &.screen-height {
      margin-top: -63px;
      padding: 31px 0px 30px 0px;
      overflow: scroll;
    }
    &.filter-map-mode{
      min-height: 627px;
      max-height: calc(100vh - 276px);
      height: 100%;
    }
    &.screen-height{
      height: 100vh;
      padding-bottom: 81px
    }
  }

}

@media only screen and  (max-width: 991px){
  .desktopss{
    margin-top: 0px !important;
  }
  .filter-map-mode{
    height: unset;
    overflow: unset;

  }
  .filter-map-mode::-webkit-scrollbar{
    display: unset;
  }
}

HTML

                <landwirtde1-farm-list-filter-ui [filter]="filter" [mapMode]="mapIsViewing"  [coverScreenHeight]="true"
                                                 [filterScrollWhenMap]="filterScrollWhenMap" (onScrolledInFilterUIAtMap)="filterScrollWhenMap = $event"
                                                 (onScrollYMaxIsDetermined)="filterScrollMax = $event">
                </landwirtde1-farm-list-filter-ui>


问:
Visual Studio install issue
Trying to install the RHEL version of Visual Studio on one of my RHEL 7 Linux boxes.

Installation instructions state:

```<code>sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
</code>```

I receive the following when doing so:

```<code>[root@desectlas014 ~]# rpm --import https://packages.microsoft.com/keys/microsoft.asc
curl: (7) Failed connect to packages.microsoft.com:443; Connection timed out                                     error: https://packages.microsoft.com/keys/microsoft.asc: import read
failed(2).
</code>```

Can you describe what the issue is?

Thanks

答: The reason of this message is your computer can't reach the host packages.microsoft.com on port 443

Title: Visual Studio install issue

Body:
Trying to install the RHEL version of Visual Studio on one of my RHEL 7 Linux boxes.

Installation instructions state:

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc

I receive the following when doing so:

[root@desectlas014 ~]# rpm --import https://packages.microsoft.com/keys/microsoft.asc
curl: (7) Failed connect to packages.microsoft.com:443; Connection timed out                                     error: https://packages.microsoft.com/keys/microsoft.asc: import read
failed(2).

Can you describe what the issue is?

Thanks


问:
Have my Spring test slice scan a single class instead of the whole package
I was asked to create integration tests for an existing SpringBoot project, whose organisation is not as modular as I would prefer. For example, there is a package yielding all repositories associated with all services. This became a problem for me when I was attempting to create a @WebMvcTest test slice, because when I use @ComponentScan, @EnableJpaRepositories, @EntityScan to read my target classes it ends up scanning all other unnecessary ones that share the same package.

Since changing the project structure is not really a decision I can make on my own, my question is whether it is possible to have my test scan pick a specific class and disregard all others within the same package?

Thank you for your attention

答:
I was finally able to achieve all the required filtering, thanks to Josef's answer and these:

https://stackoverflow.com/a/21022853/4828060,

https://stackoverflow.com/a/8648748/4828060

Components and Services can be configured to yield filters hence we can specify our target services and controllers and exclude everything else at the same time:

```<code> @ComponentScan(
        basePackageClasses = {
                MyTargetService.class,
                MyTargetController.class
        },
        useDefaultFilters = false,
        includeFilters = {
                @ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = MyTargetService.class),
                @ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = MyTargetController.class)

        }
)
</code>```

Repositories. This is unlikely to work for repositories, but fortunately the @EnableJpaRepositories supports the same type of filters:

```<code>  @EnableJpaRepositories(
       basePackageClasses = {
            MyTargetRepository.class
       },
       includeFilters = {
            @ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = MyTargetRepository.class)
       }
  )
</code>```

Entities. This part is more tricky because @EntityScan does not support these filters. Although the entities do not reference Spring beans, I prefer loading only the entities necessary for my test. I was not able to find any annotation for entities that supports filtering, but we can filter them programmatically using a PersistenceUnitPostProcessor in our EntityManagerFactory. Here is my full solution:

```<code>   //add also the filtered @ComponentScan and @EnableJpaRepositories annotations here
   @Configuration
   public class MyConfig {

    //here we specify the packages of our target entities
    private static String[] MODEL_PACKAGES = {
            "com.full.path.to.entity.package1",
            "com.full.path.to.entity.package2"
    };

    //here we specify our target entities
    private static Set<String> TARGET_ENTITIES = new HashSet<>(Arrays.asList(
            "com.full.path.to.entity.package1.MyTargetEntity1",
            "com.full.path.to.entity.package2.MyTargetEntity2"
    ));

    @Bean
    public DataSource getDataSource() {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.H2).build();
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() {

        ReflectionsPersistenceUnitPostProcessor reflectionsPersistenceUnitPostProcessor = new ReflectionsPersistenceUnitPostProcessor();

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setShowSql(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan(MODEL_PACKAGES);
        factory.setDataSource(getDataSource());
        factory.setPersistenceUnitPostProcessors(reflectionsPersistenceUnitPostProcessor);
        factory.afterPropertiesSet();

        return factory.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }

    public class ReflectionsPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor {

        @Override
        public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {

            Reflections r = new Reflections("", new TypeAnnotationsScanner());
            Set<Class<?>> entityClasses = r.getTypesAnnotatedWith(Entity.class, true);
            Set<Class<?>> mappedSuperClasses = r.getTypesAnnotatedWith(MappedSuperclass.class, true);

            pui.getManagedClassNames().clear(); //here we remove all entities

            //here we add only the ones we are targeting
            for (Class<?> clzz : mappedSuperClasses) {
                if (TARGET_ENTITIES.contains(clzz.getName())) {
                    pui.addManagedClassName(clzz.getName());
                }
            }
            for (Class<?> clzz : entityClasses) {
                if (TARGET_ENTITIES.contains(clzz.getName())) {
                    pui.addManagedClassName(clzz.getName());
                }
            }

        }

    }

}
</code>```

Body:
I was finally able to achieve all the required filtering, thanks to Josef's answer and these:

https://stackoverflow.com/a/21022853/4828060,

https://stackoverflow.com/a/8648748/4828060

Components and Services can be configured to yield filters hence we can specify our target services and controllers and exclude everything else at the same time:

 @ComponentScan(
        basePackageClasses = {
                MyTargetService.class,
                MyTargetController.class
        },
        useDefaultFilters = false,
        includeFilters = {
                @ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = MyTargetService.class),
                @ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = MyTargetController.class)

        }
)

Repositories. This is unlikely to work for repositories, but fortunately the @EnableJpaRepositories supports the same type of filters:

  @EnableJpaRepositories(
       basePackageClasses = {
            MyTargetRepository.class
       },
       includeFilters = {
            @ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = MyTargetRepository.class)
       }
  )

Entities. This part is more tricky because @EntityScan does not support these filters. Although the entities do not reference Spring beans, I prefer loading only the entities necessary for my test. I was not able to find any annotation for entities that supports filtering, but we can filter them programmatically using a PersistenceUnitPostProcessor in our EntityManagerFactory. Here is my full solution:

   //add also the filtered @ComponentScan and @EnableJpaRepositories annotations here
   @Configuration
   public class MyConfig {

    //here we specify the packages of our target entities
    private static String[] MODEL_PACKAGES = {
            "com.full.path.to.entity.package1",
            "com.full.path.to.entity.package2"
    };

    //here we specify our target entities
    private static Set<String> TARGET_ENTITIES = new HashSet<>(Arrays.asList(
            "com.full.path.to.entity.package1.MyTargetEntity1",
            "com.full.path.to.entity.package2.MyTargetEntity2"
    ));

    @Bean
    public DataSource getDataSource() {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.H2).build();
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() {

        ReflectionsPersistenceUnitPostProcessor reflectionsPersistenceUnitPostProcessor = new ReflectionsPersistenceUnitPostProcessor();

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setShowSql(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan(MODEL_PACKAGES);
        factory.setDataSource(getDataSource());
        factory.setPersistenceUnitPostProcessors(reflectionsPersistenceUnitPostProcessor);
        factory.afterPropertiesSet();

        return factory.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }

    public class ReflectionsPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor {

        @Override
        public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {

            Reflections r = new Reflections("", new TypeAnnotationsScanner());
            Set<Class<?>> entityClasses = r.getTypesAnnotatedWith(Entity.class, true);
            Set<Class<?>> mappedSuperClasses = r.getTypesAnnotatedWith(MappedSuperclass.class, true);

            pui.getManagedClassNames().clear(); //here we remove all entities

            //here we add only the ones we are targeting
            for (Class<?> clzz : mappedSuperClasses) {
                if (TARGET_ENTITIES.contains(clzz.getName())) {
                    pui.addManagedClassName(clzz.getName());
                }
            }
            for (Class<?> clzz : entityClasses) {
                if (TARGET_ENTITIES.contains(clzz.getName())) {
                    pui.addManagedClassName(clzz.getName());
                }
            }

        }

    }

}


问:
How to avoid Duplicates on PayPal plans
I am looking for a way to avoid the same user to resubscribe to the same PayPal plan. I have a landing page which is not demand from the user any input - no login, email or other info - and via this page the user can purchase a subscription. </p>

<p>Is there a away to setup the plan that way that a PayPal user can be active subscribed to a plan only once? </p>

答: PayPal will not keep track of whether a user has already subscribed to a plan, and prevent them from subscribing multiple times. If you need that feature, you must implement it yourself.</p>

Title: How to avoid Duplicates on PayPal plans

Body:
I am looking for a way to avoid the same user to resubscribe to the same PayPal plan. I have a landing page which is not demand from the user any input - no login, email or other info - and via this page the user can purchase a subscription.

Is there a away to setup the plan that way that a PayPal user can be active subscribed to a plan only once?


问:
how to build docker image by docker-compose.yml
Here is my docker-compose.yml:

```<code>version: '2'
services:
web:
  build: .
  context: ./
  dockerfile: Dockerfile
  ports:
    - "8080:8080"
  container_name: demo
  volumes:
    - .:/images
</code>```

I got an error:

ERROR: The Compose file './docker-compose.yml' is invalid because:
  Unsupported config option for services.web: 'dockerfile'

So I would like to build the container and also run. I will have a few more containers to build and run. Is it possible by docker-compose?

答:
You need to respect the indentations , here is an example for building Nginx docker image https://github.com/wshihadeh/three_methods_to_share_assets/blob/master/precompile_during_docker_build/docker-compose.yml </p>

```<code>version: '3.7'
services:
  web:
    build:
      context: nginx
      dockerfile: Dockerfile
    command: server
    ports:
      - 80:80

</code>```

Body:
Here is my docker-compose.yml:

version: '2'
services:
web:
  build: .
  context: ./
  dockerfile: Dockerfile
  ports:
    - "8080:8080"
  container_name: demo
  volumes:
    - .:/images

I got an error:

ERROR: The Compose file './docker-compose.yml' is invalid because:
  Unsupported config option for services.web: 'dockerfile'

So I would like to build the container and also run. I will have a few more containers to build and run. Is it possible by docker-compose?

Body:
You need to respect the indentations , here is an example for building Nginx docker image https://github.com/wshihadeh/three_methods_to_share_assets/blob/master/precompile_during_docker_build/docker-compose.yml

version: '3.7'
services:
  web:
    build:
      context: nginx
      dockerfile: Dockerfile
    command: server
    ports:
      - 80:80


问:
Laravel echo: is it possible to listen to all events instead of a specific one?
I have implemented Laravel Broadcasting in my project. Everything is working fine but I'm wondering if it's possible to listen to all events instead of just a specific one?

Currently I have this code on my front-end:

```<code>window.Echo.channel('office-dashboard')
  .listen('CompanyUpdated', (e) => {
    console.log(e.company);
  });
  .listen('CompanyDeleted', (e) => {
    console.log(e.company);
  });
</code>```

I want to structure my event in such a way that I can grab what kind of event it exactly was, and what kind of action was performed. But that's useless if I still have to listen to each event specifically, like I do now. I want to listen to all events in a channel, is that possible?

I read the docs, but those only talk about how to listen to a specific event.

答:
I came across the same situation. I think you need to listen to individual channels. but you can write the code a bit cleaner like below.</p>

```<code>    const channel = window.Echo.channel('office-dashboard')

    const eventsTolisten = [
      'CompanyUpdated',
      'CompanyDeleted',
    ]

    eventsTolisten.forEach(event => {
      channel.listen(event, e => {
        this.handleSocketEvents({
          name: event,
          data: e.data
        })
      })
    })
</code>```

Body:
I have implemented Laravel Broadcasting in my project. Everything is working fine but I'm wondering if it's possible to listen to all events instead of just a specific one?

Currently I have this code on my front-end:

window.Echo.channel('office-dashboard')
  .listen('CompanyUpdated', (e) => {
    console.log(e.company);
  });
  .listen('CompanyDeleted', (e) => {
    console.log(e.company);
  });

I want to structure my event in such a way that I can grab what kind of event it exactly was, and what kind of action was performed. But that's useless if I still have to listen to each event specifically, like I do now. I want to listen to all events in a channel, is that possible?

I read the docs, but those only talk about how to listen to a specific event.

Body:
I came across the same situation. I think you need to listen to individual channels. but you can write the code a bit cleaner like below.

    const channel = window.Echo.channel('office-dashboard')

    const eventsTolisten = [
      'CompanyUpdated',
      'CompanyDeleted',
    ]

    eventsTolisten.forEach(event => {
      channel.listen(event, e => {
        this.handleSocketEvents({
          name: event,
          data: e.data
        })
      })
    })


问:
Reverse geocoding with python to get googlemaps url
I have two columns df['latitude'] and df['longitude'] and I am trying to reverse geocode them into full address google maps in df['Address']. However, I am not very successful. Is there a better way of doing this? I am using Geocoder package.

This is a sample from dataframe

\\
lat, lon
48.7082,2.2797
48.7577,2.2188
47.8333,2.2500
48.9833,1.7333
47.9333,1.9181
46.2735,4.2586
\\

答:
You can use the google maps API . You will need to use python 3.5 or higher and need to get a service key. You can get a API key using cloud console . There are wrappers out there that should make your job easier.

Google  Maps API for client services

Wrapper


问:
Node stream returns strings in strange formatting
I'm piping a node stream to stdout but it always writes it in strange manner, every line looks like an object, also as you can see the new line identifier is written.

```<code>stream.setEncoding('utf8');
stream.pipe(process.stdout, { end: true });
</code>```

This is how the console log looks like. The output comes from creating a docker container using dockerode.

```<code>{"stream":"\n"}
{"stream":" ---\u003e Using cache\n"}
{"stream":" ---\u003e 7b0c642899ce\n"}
{"stream":"\n"}
{"stream":" ---\u003e Using cache\n"}
{"stream":" ---\u003e e01f16a7ed86\n"}
{"stream":"Step 8/11 : WORKDIR /home"}
</code>```

Why is this the case and how can I fix it?

答:
Fixed it by doing this instead of stream.pipe(process.stdout)

```<code>stream.on('data', data => {
  try {
    console.log(JSON.parse(data.toString()).stream);
  } catch (error) {
    return;
  }
});
</code>```

Body:
I'm piping a node stream to stdout but it always writes it in strange manner, every line looks like an object, also as you can see the new line identifier is written.

stream.setEncoding('utf8');
stream.pipe(process.stdout, { end: true });

This is how the console log looks like. The output comes from creating a docker container using dockerode.

{"stream":"\n"}
{"stream":" ---\u003e Using cache\n"}
{"stream":" ---\u003e 7b0c642899ce\n"}
{"stream":"\n"}
{"stream":" ---\u003e Using cache\n"}
{"stream":" ---\u003e e01f16a7ed86\n"}
{"stream":"Step 8/11 : WORKDIR /home"}

Why is this the case and how can I fix it?

Body:
Fixed it by doing this instead of stream.pipe(process.stdout)

stream.on('data', data => {
  try {
    console.log(JSON.parse(data.toString()).stream);
  } catch (error) {
    return;
  }
});


问:
setInterval on a for loop?
```<code>for (var i=list.length; i>=0; i--){
    //do something....
}
</code>```

I want to use a setInterval to make this process take 1 whole min no matter how many items are in the list. So if there are 10 items it would trigger every 6sec, 30items, every two seconds, etc.

Thanks for any help!

答:
You'd do </p>

```<code>function code(i) {
    return function() { alert(i); };
}

var period = 60 * 1000 / (list.length - 1);
for (var i=list.length; i>=1; i--){
    setTimeout(code(list[i - 1]), period * (i - 1));
}
</code>```

Title: setInterval on a for loop?

Body:
for (var i=list.length; i>=0; i--){
    //do something....
}

I want to use a setInterval to make this process take 1 whole min no matter how many items are in the list. So if there are 10 items it would trigger every 6sec, 30items, every two seconds, etc.

Thanks for any help!

Body:
You'd do

function code(i) {
    return function() { alert(i); };
}

var period = 60 * 1000 / (list.length - 1);
for (var i=list.length; i>=1; i--){
    setTimeout(code(list[i - 1]), period * (i - 1));
}


问:
Clearing and resizing tables in excel VBA
I have a table that I use to display data. I want it to change its size depending on the size of the data that is to be displayed. I already implemented that part.

However, I am having problems with cleaning the table afterwards. I want to write a function that clears all the fields in the tables DataBodyRange and resized it to 1 without changing anything else in the table. I would also like to not rely on code like

```<code>Worksheets("Sheet").ListObjects("table").Resize Range("B13:K15")
</code>```

that uses reference to certain static cells like B13 or K15, as moving the table would break the function if I used that.

What I am looking for is more a "Resize the DataBodyRange to 1 row". How would I do that?

EDIT:

Daghan's answer basically does what I want, however, I cannot select my tables DataBodyRange any more after using the cleanup function.

My code:

Cleanup-function:

```<code>Sub clearAllocationsView()
    Worksheets("sheet").ListObjects("table").DataBodyRange.ClearContents
    Worksheets("sheet").ListObjects("table").Resize Range("table[#All]").Resize(2, 10)
End Sub
</code>```

Table fill function:

```<code>Sub getAllocations()

    Dim cmd As New ADODB.Command

    Set t = Worksheets("sheet").ListObjects("table").DataBodyRange
    data = transposeArray(cn.Execute("select * from some_table").GetRows)
    t.Resize(UBound(data, 1) - LBound(data, 1) + 1, UBound(data, 2) - LBound(data, 2) + 1) = data

End Sub
</code>```

Where transposeArray is just a self-written transpose function because VBA standard transpose has problems with null values.

To give more detail for the current situation: my table fill function works (filling the data and resizing the table depending on data size), the cleanup function works, however, after calling the cleanup function I can no longer call either the table fill or the cleanup function, both error with

object variable with block variable not set

regarding the DataBodyRange of the table.

Any ideas?

答:
Try this

```<code>Worksheets("Sheet").ListObjects("table").DataBodyRange.ClearContents

Worksheets("Sheet").ListObjects("table").Resize Range("table[#All]").Resize(2, 10)
</code>```

Where 2 is the header+ 1 data row (the minimum for tables).

Where 10 is the amount of columns from B to K.

Body:
I have a table that I use to display data. I want it to change its size depending on the size of the data that is to be displayed. I already implemented that part.

However, I am having problems with cleaning the table afterwards. I want to write a function that clears all the fields in the tables DataBodyRange and resized it to 1 without changing anything else in the table. I would also like to not rely on code like

Worksheets("Sheet").ListObjects("table").Resize Range("B13:K15")

that uses reference to certain static cells like B13 or K15, as moving the table would break the function if I used that.

What I am looking for is more a "Resize the DataBodyRange to 1 row". How would I do that?

EDIT:

Daghan's answer basically does what I want, however, I cannot select my tables DataBodyRange any more after using the cleanup function.

My code:

Cleanup-function:

Sub clearAllocationsView()
    Worksheets("sheet").ListObjects("table").DataBodyRange.ClearContents
    Worksheets("sheet").ListObjects("table").Resize Range("table[#All]").Resize(2, 10)
End Sub

Table fill function:

Sub getAllocations()

    Dim cmd As New ADODB.Command

    Set t = Worksheets("sheet").ListObjects("table").DataBodyRange
    data = transposeArray(cn.Execute("select * from some_table").GetRows)
    t.Resize(UBound(data, 1) - LBound(data, 1) + 1, UBound(data, 2) - LBound(data, 2) + 1) = data

End Sub

Where transposeArray is just a self-written transpose function because VBA standard transpose has problems with null values.

To give more detail for the current situation: my table fill function works (filling the data and resizing the table depending on data size), the cleanup function works, however, after calling the cleanup function I can no longer call either the table fill or the cleanup function, both error with

object variable with block variable not set

regarding the DataBodyRange of the table.

Any ideas?

Body:
Try this

Worksheets("Sheet").ListObjects("table").DataBodyRange.ClearContents

Worksheets("Sheet").ListObjects("table").Resize Range("table[#All]").Resize(2, 10)

Where 2 is the header+ 1 data row (the minimum for tables).

Where 10 is the amount of columns from B to K.