问:
How to POST data to a URL from NestJs code?
I have a login form with username and password. I'm trying to validate these credentials using the Nest Js authentication strategy here. So in the corresponding auth.service.ts file, I'm using "nativescript core modules http" to do a POST request to OAuth URL to validate credentials . But this doesn't work:

```<code>
        import { Injectable } from '@nestjs/common';
        import { request } from "tns-core-modules/http";

        const OAUTH_URL = 'url';

        @Injectable()
        export class AuthService {

        async validateUser(username: string, password: string): Promise<any> {

            let data = new FormData();
            data.set('client_id', 'sb-nestjs-app!t36258');
            data.set('client_secret', 'XrHuBRhyvuVNYNJNHlWLgcuBIyc=');
            data.set('username', username);
            data.set('password', password);
            data.set('grant_type', 'password');
            data.set('response_type', 'token');

            request({
                url: OAUTH_URL,
                method: "POST",
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                    "Accept": "application/json;charset=utf8"
                },
                content: data
            }).then((response) => {
                console.log('response => ' + response + ' statuscode ' + response.statusCode);
                if (response.statusCode == 200) {
                    const token = response.content['access_token'];
                    //TODO:
                    // need to send scope also
                    return token;
                }
            }, (e) => {
                console.log('error' + e);
                return null;
            });

            return null;
        }
    }

</code>```

When I run 'nest start' after above code in place, I receive Error: Cannot find module './http-request'

I'm not sure what is going here, I tried "npm install http-request" it didn't work either. Basically, I need to POST credentials to a OAuth url in NestJs. Any guidance? Thank you.

答:
Try with HttpModule from NestJS.
Also you can try request from npm, but they deprecated this package. From what I saw on their discussion, the package still works but you will not have support for it, or anything. Here are some alternatives to it .

I'm not sure you are using the correct request npm module. I'm talking about:

```<code>import { request } from "tns-core-modules/http"
</code>```

Good Luck!

Body:
I have a login form with username and password. I'm trying to validate these credentials using the Nest Js authentication strategy here. So in the corresponding auth.service.ts file, I'm using "nativescript core modules http" to do a POST request to OAuth URL to validate credentials . But this doesn't work:

        import { Injectable } from '@nestjs/common';
        import { request } from "tns-core-modules/http";

        const OAUTH_URL = 'url';

        @Injectable()
        export class AuthService {

        async validateUser(username: string, password: string): Promise<any> {

            let data = new FormData();
            data.set('client_id', 'sb-nestjs-app!t36258');
            data.set('client_secret', 'XrHuBRhyvuVNYNJNHlWLgcuBIyc=');
            data.set('username', username);
            data.set('password', password);
            data.set('grant_type', 'password');
            data.set('response_type', 'token');

            request({
                url: OAUTH_URL,
                method: "POST",
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                    "Accept": "application/json;charset=utf8"
                },
                content: data
            }).then((response) => {
                console.log('response => ' + response + ' statuscode ' + response.statusCode);
                if (response.statusCode == 200) {
                    const token = response.content['access_token'];
                    //TODO:
                    // need to send scope also
                    return token;
                }
            }, (e) => {
                console.log('error' + e);
                return null;
            });

            return null;
        }
    }

When I run 'nest start' after above code in place, I receive Error: Cannot find module './http-request'

I'm not sure what is going here, I tried "npm install http-request" it didn't work either. Basically, I need to POST credentials to a OAuth url in NestJs. Any guidance? Thank you.

Body:
Try with HttpModule from NestJS.
Also you can try request from npm, but they deprecated this package. From what I saw on their discussion, the package still works but you will not have support for it, or anything. Here are some alternatives to it .

I'm not sure you are using the correct request npm module. I'm talking about:

import { request } from "tns-core-modules/http"

Good Luck!


来源: stackexchange

问:
How to subset the data frame based on selected variable with limited column?
i would like to subset limited column and selected variable as i have multiple column in my data frame.

my sample data:

df <- data.frame('ID'=c('A','B','C'),'YEAR'=c('2020','2020','2020'),'MONTH'=c('1','1','1'),'DAY'=c('16','16','16'),'HOUR'=c('15','15','15'),'VALUE1'=c(1,2,3))

i would like to subset ID'='C' and column name 'VALUE1'
Expected output:-

```<code>  ID VALUE1
1  C      3
</code>```

Appreciate any help...!

What i have tried so far is.

```<code>df1 <- subset(df,df$ID=='C')
df2 <- subset(df1,select=c('ID','VALUE1')

</code>```

Is there any efficient way to do that as creating multiple data frame when we have multiple is not good.

答:
We can have both subset and select

```<code>subset(df, subset = ID=='C', select = c('ID', 'VALUE1'))
</code>```

ContentLicense: CC BY-SA 4.0

Body:
i would like to subset limited column and selected variable as i have multiple column in my data frame.

my sample data:

df <- data.frame('ID'=c('A','B','C'),'YEAR'=c('2020','2020','2020'),'MONTH'=c('1','1','1'),'DAY'=c('16','16','16'),'HOUR'=c('15','15','15'),'VALUE1'=c(1,2,3))

i would like to subset ID'='C' and column name 'VALUE1'
Expected output:-

  ID VALUE1
1  C      3

Appreciate any help...!

What i have tried so far is.

df1 <- subset(df,df$ID=='C')
df2 <- subset(df1,select=c('ID','VALUE1')

Is there any efficient way to do that as creating multiple data frame when we have multiple is not good.

Body:
We can have both subset and select

subset(df, subset = ID=='C', select = c('ID', 'VALUE1'))


问:
A program to send an image file from client to server in python using TCP
This is my code. Currently it transfers about 75% of the image file (cropped at the bottom). I want it transferred as a whole. This is how I've been working on it:

Create a folder in server pc called "server"

Create a folder in client pc called "client"

Also include the image file you want to transfer in the same "client" folder

Client side ask "name of the file you want to send", I type in the name of the image file in the folder

Aerver side ask me "name of the file you want to receive", I type any random name.

The file transfers and gets saved to "server" folder in the server PC.

client.py

```<code>from socket import *

def establish_connection(server_IP, server_port):
    client_socket = socket(AF_INET, SOCK_STREAM)
    client_socket.connect((server_IP, server_port))
    return client_socket

def send_recv_message(client_socket, message, buffer_size):
    file = open(filename,'rb')
    file_data = file.read(buffer_size)
    client_socket.send(file_data)
    text=print("File transfered")
    return text

server_IP = input("Please, enter server IP address: ")
server_port = input("Please, enter server TCP connection port number: ")
client_socket = establish_connection(server_IP, int(server_port))
print("Connection established\n")

filename= input(str("enter filename"))

received_message = send_recv_message(client_socket,filename,1048576)
</code>```

server.py

```<code>from socket import *

def create_socket(server_port):
    server_socket = socket(AF_INET, SOCK_STREAM)
    server_socket.bind(('', server_port))
    server_socket.listen(1)
    return server_socket

def recv_message(server_socket, buffer_size):
    file = open(filename, 'wb')
    connection_socket, address = server_socket.accept()
    file_data= connection_socket.recv(buffer_size)
    file.write(file_data)
    file.close()
    text=("file recieved")
    return text

server_port = input("Please, enter TCP connection port number: ")
server_socket = create_socket(int(server_port))

while (True):
    filename=input(str("enter filename for the incoming file:"))
    incoming_message = recv_message(server_socket,  1048576)
    print ("{}" .format(incoming_message))
</code>```

答:
send may not send the entire buffer. You need to use sendall instead.

recv may not receive all the available data. You need to call recv in a loop until the connection is closed.

Body:
This is my code. Currently it transfers about 75% of the image file (cropped at the bottom). I want it transferred as a whole. This is how I've been working on it:

Create a folder in server pc called "server"

Create a folder in client pc called "client"

Also include the image file you want to transfer in the same "client" folder

Client side ask "name of the file you want to send", I type in the name of the image file in the folder

Aerver side ask me "name of the file you want to receive", I type any random name.

The file transfers and gets saved to "server" folder in the server PC.

client.py

from socket import *

def establish_connection(server_IP, server_port):
    client_socket = socket(AF_INET, SOCK_STREAM)
    client_socket.connect((server_IP, server_port))
    return client_socket

def send_recv_message(client_socket, message, buffer_size):
    file = open(filename,'rb')
    file_data = file.read(buffer_size)
    client_socket.send(file_data)
    text=print("File transfered")
    return text

server_IP = input("Please, enter server IP address: ")
server_port = input("Please, enter server TCP connection port number: ")
client_socket = establish_connection(server_IP, int(server_port))
print("Connection established\n")

filename= input(str("enter filename"))

received_message = send_recv_message(client_socket,filename,1048576)

server.py

from socket import *

def create_socket(server_port):
    server_socket = socket(AF_INET, SOCK_STREAM)
    server_socket.bind(('', server_port))
    server_socket.listen(1)
    return server_socket

def recv_message(server_socket, buffer_size):
    file = open(filename, 'wb')
    connection_socket, address = server_socket.accept()
    file_data= connection_socket.recv(buffer_size)
    file.write(file_data)
    file.close()
    text=("file recieved")
    return text

server_port = input("Please, enter TCP connection port number: ")
server_socket = create_socket(int(server_port))

while (True):
    filename=input(str("enter filename for the incoming file:"))
    incoming_message = recv_message(server_socket,  1048576)
    print ("{}" .format(incoming_message))


问:
Program that prints one letter at a time
So, I basically need help on how to make words into a "rainbow", for example:

H

He

Hel

Hell

Hello

Hell

Hel

He

H

He

Hel

And so on.

The only thing I kinda managed to do is this in Python is the first letter to the last, H to Hello, but then I have no idea how to reverse it. Please show me how you would do this.

答:
This is my answer in C#:

```<code>using System;

public class Simple {
  public static void Main() {
    string Text = "Hello";
    for (int i=0;i < Text.Length;i++)
        Console.WriteLine(Text.Substring(0,i+1));
    for (int i=Text.Length;i > 1;i--)
        Console.WriteLine(Text.Substring(0,i-1));
  }
}
</code>```

In Python:

```<code>text = "Hello"
for x in range(1, len(text)+1):
  print(text[0:x])
for x in range (1, len(text)):
  print(text[0:len(text)-x])
</code>```

You can also wrap the code above in a function and call it as many times as you need, in order to repeat your rainbow word as many times as you want.

For example, if you want to run it 10 times, you can do something like this:

```<code>def my_function(text):
    for x in range(1, len(text)+1):
      print(text[0:x])
    for x in range (1, len(text)):
      print(text[0:len(text)-x])

for x in range(1,10): #It will run it 10 times
    my_function("Hello")
</code>```

Body:
This is my answer in C#:

using System;

public class Simple {
  public static void Main() {
    string Text = "Hello";
    for (int i=0;i < Text.Length;i++)
        Console.WriteLine(Text.Substring(0,i+1));
    for (int i=Text.Length;i > 1;i--)
        Console.WriteLine(Text.Substring(0,i-1));
  }
}

In Python:

text = "Hello"
for x in range(1, len(text)+1):
  print(text[0:x])
for x in range (1, len(text)):
  print(text[0:len(text)-x])

You can also wrap the code above in a function and call it as many times as you need, in order to repeat your rainbow word as many times as you want.

For example, if you want to run it 10 times, you can do something like this:

def my_function(text):
    for x in range(1, len(text)+1):
      print(text[0:x])
    for x in range (1, len(text)):
      print(text[0:len(text)-x])

for x in range(1,10): #It will run it 10 times
    my_function("Hello")


问:
FirebaseRecyclerAdapter is not displaying data in my RecyclerView layout
Please note there are lot of questions like these but none of them worked for me.
I have been trying to display data in a recycler view and it isnt displaying the data and not throwing any errors
Here is my code.

I have included the latest version of Firebase Dependencies and Latest version of Firebase UI Database

Dependencies Version

```<code>// Firebase
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-storage:19.1.1'

// Firebase UI
implementation 'com.firebaseui:firebase-ui-database:6.0.2'
</code>```

This is the Films class
Films.java

```<code>package com.dipeshpatil.in;

public class Films {

    public String name, year;

    public Films(String name, String year) {
        this.name = name;
        this.year = year;
    }

    public Films() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }
}
</code>```

This is the ViewHolder Class FilmsViewHolder.java

```<code>package com.dipeshpatil.in;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

public class FilmsViewHolder extends RecyclerView.ViewHolder {
    public TextView mFilmName;
    public TextView mFilmYear;

    public FilmsViewHolder(View itemView){
        super(itemView);

        mFilmName = itemView.findViewById(R.id.user_single_name);
        mFilmYear = itemView.findViewById(R.id.user_single_status);
    }
}
</code>```

This is the activity where i am supposed to display the data in recycler view FilmsActivity.java

```<code>public class FilmsActivity extends AppCompatActivity {

    private RecyclerView mFilmsList;
    private DatabaseReference mFilmsDatabase;
    private FirebaseRecyclerOptions<Films> options;
    private FirebaseRecyclerAdapter<Films, FilmsViewHolder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_films);

        mFilmsList = findViewById(R.id.films_list);
        mFilmsList.setHasFixedSize(true);

        mFilmsDatabase = FirebaseDatabase.getInstance().getReference().child("Films");

        options = new FirebaseRecyclerOptions.Builder<Films>()
                .setQuery(mFilmsDatabase, Films.class)
                .build();

        adapter = new FirebaseRecyclerAdapter<Films, FilmsViewHolder>(options) {
            @Override
            protected void onBindViewHolder(FilmsViewHolder filmsViewHolder, int i, Films films) {

                filmsViewHolder.mFilmName.setText(films.getName());
                filmsViewHolder.mFilmYear.setText(films.getYear());
            }

            @NonNull
            @Override
            public FilmsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

                View mView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.users_single_layout, parent, false);

                return new FilmsViewHolder(mView);
            }
        };
        mFilmsList.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}
</code>```

Now honestly i am not getting any kind of error which would help me debug the code and check for problems and at the same time it is also not displaying my desired output

Firebase Database for Films

```<code>"Films" : {
    "01" : {
      "name" : "The Prestige",
      "year" : 2002
    },
    "02" : {
      "name" : "Fight Club",
      "year" : 1999
    }
}
</code>```

Layout File activity_films.xml

```<code><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".FilmsActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        android:id="@+id/films_list" />
</LinearLayout>
</code>```

答:
com.google.firebase.database.DatabaseException: Failed to convert
  value of type java.lang.Long to String

First of all, it's should be a InvocationTargetException [according your database structure] as you try to cast Long as String in your model. Try to update your model like below:

```<code>public class Films {
    public String name;
    public int year;

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

Then, You should set LayoutManager to your RecyclerView

```<code>LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mFilmsList.setLayoutManager(linearLayoutManager);
</code>```

Update: You have to update your onBindViewHolder to convert int to String before setting it to TextView using String.valueOf

```<code>@Override
protected void onBindViewHolder(FilmsViewHolder filmsViewHolder, int i, Films films) {

    filmsViewHolder.mFilmName.setText(films.getName());
    filmsViewHolder.mFilmYear.setText(String.valueOf(films.getYear()));
}
</code>```

Body:
Please note there are lot of questions like these but none of them worked for me.
I have been trying to display data in a recycler view and it isnt displaying the data and not throwing any errors
Here is my code.

I have included the latest version of Firebase Dependencies and Latest version of Firebase UI Database

Dependencies Version

// Firebase
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-storage:19.1.1'

// Firebase UI
implementation 'com.firebaseui:firebase-ui-database:6.0.2'

This is the Films class
Films.java

package com.dipeshpatil.in;

public class Films {

    public String name, year;

    public Films(String name, String year) {
        this.name = name;
        this.year = year;
    }

    public Films() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }
}

This is the ViewHolder Class FilmsViewHolder.java

package com.dipeshpatil.in;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

public class FilmsViewHolder extends RecyclerView.ViewHolder {
    public TextView mFilmName;
    public TextView mFilmYear;

    public FilmsViewHolder(View itemView){
        super(itemView);

        mFilmName = itemView.findViewById(R.id.user_single_name);
        mFilmYear = itemView.findViewById(R.id.user_single_status);
    }
}

This is the activity where i am supposed to display the data in recycler view FilmsActivity.java

public class FilmsActivity extends AppCompatActivity {

    private RecyclerView mFilmsList;
    private DatabaseReference mFilmsDatabase;
    private FirebaseRecyclerOptions<Films> options;
    private FirebaseRecyclerAdapter<Films, FilmsViewHolder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_films);

        mFilmsList = findViewById(R.id.films_list);
        mFilmsList.setHasFixedSize(true);

        mFilmsDatabase = FirebaseDatabase.getInstance().getReference().child("Films");

        options = new FirebaseRecyclerOptions.Builder<Films>()
                .setQuery(mFilmsDatabase, Films.class)
                .build();

        adapter = new FirebaseRecyclerAdapter<Films, FilmsViewHolder>(options) {
            @Override
            protected void onBindViewHolder(FilmsViewHolder filmsViewHolder, int i, Films films) {

                filmsViewHolder.mFilmName.setText(films.getName());
                filmsViewHolder.mFilmYear.setText(films.getYear());
            }

            @NonNull
            @Override
            public FilmsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

                View mView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.users_single_layout, parent, false);

                return new FilmsViewHolder(mView);
            }
        };
        mFilmsList.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

Now honestly i am not getting any kind of error which would help me debug the code and check for problems and at the same time it is also not displaying my desired output

Firebase Database for Films

"Films" : {
    "01" : {
      "name" : "The Prestige",
      "year" : 2002
    },
    "02" : {
      "name" : "Fight Club",
      "year" : 1999
    }
}

Layout File activity_films.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".FilmsActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        android:id="@+id/films_list" />
</LinearLayout>

Body:
com.google.firebase.database.DatabaseException: Failed to convert
  value of type java.lang.Long to String

First of all, it's should be a InvocationTargetException [according your database structure] as you try to cast Long as String in your model. Try to update your model like below:

public class Films {
    public String name;
    public int year;

    ....
}

Then, You should set LayoutManager to your RecyclerView

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mFilmsList.setLayoutManager(linearLayoutManager);

Update: You have to update your onBindViewHolder to convert int to String before setting it to TextView using String.valueOf

@Override
protected void onBindViewHolder(FilmsViewHolder filmsViewHolder, int i, Films films) {

    filmsViewHolder.mFilmName.setText(films.getName());
    filmsViewHolder.mFilmYear.setText(String.valueOf(films.getYear()));
}


问:
HTTPS load balancer in Google Container Engine
I'm trying to set up an HTTPS load balancer for GKE using HTTPS L7 load balancer but for some reason is not working. Even the HTTP load balancer in the HTTP Load Balancing walkthrough. The forwarding rule's IP address is created and I'm able to ping and telnet to port 80. But when request via curl it give me a error.

```<code><title>502 Server Error</title> </head> <body text=#000000
bgcolor=#ffffff> <h1>Error: Server Error</h1> <h2>The server
encountered a temporary error and could not complete your request.
<p>Please try again in 30 seconds.</h2> <h2></h2> </body></html>
</code>```

All the steps were fine and I created a firewall without any tags for the ${NODE_PORT} but it didn't work.

Has anyone encountered this problem?

答: Adding a Firewall Rule for  Source: 130.211.0.0/22(the Load Balancer range on GCP) for the tcp:$NODEPORTIP fixed this for me.

Body:
I'm trying to set up an HTTPS load balancer for GKE using HTTPS L7 load balancer but for some reason is not working. Even the HTTP load balancer in the HTTP Load Balancing walkthrough. The forwarding rule's IP address is created and I'm able to ping and telnet to port 80. But when request via curl it give me a error.

<title>502 Server Error</title>
Error: Server Error

The server
encountered a temporary error and could not complete your request.

Please try again in 30 seconds.

All the steps were fine and I created a firewall without any tags for the ${NODE_PORT} but it didn't work.

Has anyone encountered this problem?


问:
Does anyone know how to implement useDropzone hook and not <Dropzone /> (component) together with react-final-form
for example
if i have </p>

```<code>const ImageInput = ({fieldPropsFromFinalForm}) => {
    const { getRootProps, getInputProps, open } = useDropzone({ onDrop, accept: 'image/jpeg, image/png' });
    .
    .
    .
    return (<input {...getInputProps()} {...fieldPropsFromFinalForm} /> )

}
</code>```

<p>that way input doesn't work obvious but i have no idea how to make it work how to handle them together. i'm new to react and i hope that i haven't asked wrong question. </p>

答:
You have to invoke onChange of the fieldPropsFromFinalForm in the onDrop callback

Please find this sandbox for example

Please find the Code for <ImageInput/> Component

ImageInput

```<code> const ImageInput = props => {
  const onDrop = useCallback(acceptedFiles => {
    // Do something with the files
    props.input.onChange(acceptedFiles);
  }, []);
  const { getRootProps, getInputProps, isDragActive } = useDropzone({
    onDrop,
    accept: "image/jpeg, image/png"
  });
  return (
    <div {...getRootProps()}>
      <input {...getInputProps()} {...props} />
      {isDragActive ? (
        <p>Drop the files here ...</p>
      ) : (
        <p>Drag 'n' drop some files here, or click to select files</p>
      )}
      {props.input.value
        ? props.input.value.map(file => {
            return <div>{file.path}</div>;
          })
        : null}
    </div>
  );
};
</code>```

Sample Form

```<code>function SampleForm() {
  const onSubmit = values => {
    alert(JSON.stringify(values));
  };
  return (
    <div>
      <Form onSubmit={onSubmit}>
        {props => (
          <form onSubmit={props.handleSubmit}>
            <Field name="myField">
              {props => (
                <div>
                  <ImageInput {...props} />
                </div>
              )}
            </Field>
            <button type="submit">Submit</button>
          </form>
        )}
      </Form>
    </div>
  );
}
</code>```

Body:
for example
if i have

const ImageInput = ({fieldPropsFromFinalForm}) => {
    const { getRootProps, getInputProps, open } = useDropzone({ onDrop, accept: 'image/jpeg, image/png' });
    .
    .
    .
    return (<input {...getInputProps()} {...fieldPropsFromFinalForm} /> )

}

that way input doesn't work obvious but i have no idea how to make it work how to handle them together. i'm new to react and i hope that i haven't asked wrong question.

Body:
You have to invoke onChange of the fieldPropsFromFinalForm in the onDrop callback

Please find this sandbox for example

Please find the Code for <ImageInput/> Component

ImageInput

 const ImageInput = props => {
  const onDrop = useCallback(acceptedFiles => {
    // Do something with the files
    props.input.onChange(acceptedFiles);
  }, []);
  const { getRootProps, getInputProps, isDragActive } = useDropzone({
    onDrop,
    accept: "image/jpeg, image/png"
  });
  return (
    <div {...getRootProps()}>
      <input {...getInputProps()} {...props} />
      {isDragActive ? (
        <p>Drop the files here ...</p>
      ) : (
        <p>Drag 'n' drop some files here, or click to select files</p>
      )}
      {props.input.value
        ? props.input.value.map(file => {
            return <div>{file.path}</div>;
          })
        : null}
    </div>
  );
};

Sample Form

function SampleForm() {
  const onSubmit = values => {
    alert(JSON.stringify(values));
  };
  return (
    <div>
      <Form onSubmit={onSubmit}>
        {props => (
          <form onSubmit={props.handleSubmit}>
            <Field name="myField">
              {props => (
                <div>
                  <ImageInput {...props} />
                </div>
              )}
            </Field>
            <button type="submit">Submit</button>
          </form>
        )}
      </Form>
    </div>
  );
}


问:
may COM server reallocate ([in, out] CACLSID * arg)?
With a COM interface method declared as this:

```<code>[ object,
  uuid(....),
]
interface IFoo : IUnknown
{
  HRESULT Foo([in, out] CACLSID * items);
}
</code>```

With regards to marshalling, is the server allowed to reallocate the counted array? (I think it is, but I am not sure anymore)

Its current implementation only replaces the existing ID's, but I'd like to implement a change (that would not break contract) that may return more items without introducing a new interface.

[edit] please note that CACLSID is already an array, containing a count and a pointer.

答:
First, if you want Foo to accept an in array, you should add a paramter that gives the count, e.g.:

```<code>HRESULT Foo([in] int cItems, [in, out, size_is(cItems)] CACLSID * items);
</code>```

Warning: this code has not been compiled, just going off documentation.

Secondly, you cannot modify the extern behavior of this method without changing its declaration. To support resizing, you need to be able to reallocate the array and pass back its address. You can use a SAFEARRAY or declare cItems and items as pointers to the original type Foo takes, e.g.:

```<code>HRESULT FooMutate([in, out] int *cItems, [in, out, size_is(*cItems)] CACLSID **items);
</code>```

Again, not compiled, so you will actually have to know what you're doing if you use this.

OwnerDisplayName: peterchen

ContentLicense: CC BY-SA 2.5

Body:
With a COM interface method declared as this:

[ object,
  uuid(....),
]
interface IFoo : IUnknown
{
  HRESULT Foo([in, out] CACLSID * items);
}

With regards to marshalling, is the server allowed to reallocate the counted array? (I think it is, but I am not sure anymore)

Its current implementation only replaces the existing ID's, but I'd like to implement a change (that would not break contract) that may return more items without introducing a new interface.

[edit] please note that CACLSID is already an array, containing a count and a pointer.

LastEditorDisplayName: peterchen

Body:
First, if you want Foo to accept an in array, you should add a paramter that gives the count, e.g.:

HRESULT Foo([in] int cItems, [in, out, size_is(cItems)] CACLSID * items);

Warning: this code has not been compiled, just going off documentation.

Secondly, you cannot modify the extern behavior of this method without changing its declaration. To support resizing, you need to be able to reallocate the array and pass back its address. You can use a SAFEARRAY or declare cItems and items as pointers to the original type Foo takes, e.g.:

HRESULT FooMutate([in, out] int *cItems, [in, out, size_is(*cItems)] CACLSID **items);

Again, not compiled, so you will actually have to know what you're doing if you use this.

OwnerDisplayName: MSN


问:
How can I safely make a float *x into a const float *const *y?
I have a function audioReceived (float * input, int bufferSize, int nChannels) inside of which I want to call a function from a library that needs const float *const *inputBuffers.

Obviously casting const float *const *inputBuffers = (const float* const*)input; compiles but is a terrible idea, crashes the program, kills kittens, etc. Nobody ever needs to modify the original float* input, it's incoming audio data that's being processed.

How do I do it the right way?

EDIT: Here's some more code. audioReceived is:

```<code>void testApp::audioReceived (float * input, int bufferSize, int nChannels){

     Vamp::RealTime rt = Vamp::RealTime::fromMilliseconds(ofGetSystemTime());
     float const *const tmp[] = { input, 0 };
     Vamp::Plugin::FeatureSet fs = myPlugin->process(tmp, rt);
 }
</code>```

Library function process is virtually defined in base class:

```<code> /**
 * Process a single block of input data.
 *
 * If the plugin's inputDomain is TimeDomain, inputBuffers will
 * point to one array of floats per input channel, and each of
 * these arrays will contain blockSize consecutive audio samples
 * (the host will zero-pad as necessary).  The timestamp in this
 * case will be the real time in seconds of the start of the
 * supplied block of samples.
 *
 * If the plugin's inputDomain is FrequencyDomain, inputBuffers
 * will point to one array of floats per input channel, and each
 * of these arrays will contain blockSize/2+1 consecutive pairs of
 * real and imaginary component floats corresponding to bins
 * 0..(blockSize/2) of the FFT output.  That is, bin 0 (the first
 * pair of floats) contains the DC output, up to bin blockSize/2
 * which contains the Nyquist-frequency output.  There will
 * therefore be blockSize+2 floats per channel in total.  The
 * timestamp will be the real time in seconds of the centre of the
 * FFT input window (i.e. the very first block passed to process
 * might contain the FFT of half a block of zero samples and the
 * first half-block of the actual data, with a timestamp of zero).
 *
 * Return any features that have become available after this
 * process call.  (These do not necessarily have to fall within
 * the process block, except for OneSamplePerStep outputs.)
 */
virtual FeatureSet process(const float *const *inputBuffers,
               RealTime timestamp) = 0;
</code>```

and here in the actual header:

```<code>FeatureSet process(const float *const *inputBuffers, Vamp::RealTime timestamp);
</code>```

I think the EXC_BAD_ACCESS may be stemming from the library function wanting a zero-padded array, and my not giving it one. (a) Does that sound reasonable, and (b) If so, is it time to ask a different SO question?

Thanks all for your help so far, it's very enlightening/clarifying/educational/interesting.

答:
```<code>float *
</code>```

is not the same as

```<code>float **
</code>```

so casting input to inputBuffers isn't going to work.

Generally, casting from non-const to const is implicit, you don't need to do anything special. You can't cast from const to non-const as easily. Which is logical when you think about it.

ContentLicense: CC BY-SA 3.0

Body:
I have a function audioReceived (float * input, int bufferSize, int nChannels) inside of which I want to call a function from a library that needs const float *const *inputBuffers.

Obviously casting const float *const *inputBuffers = (const float* const*)input; compiles but is a terrible idea, crashes the program, kills kittens, etc. Nobody ever needs to modify the original float* input, it's incoming audio data that's being processed.

How do I do it the right way?

EDIT: Here's some more code. audioReceived is:

void testApp::audioReceived (float * input, int bufferSize, int nChannels){

     Vamp::RealTime rt = Vamp::RealTime::fromMilliseconds(ofGetSystemTime());
     float const *const tmp[] = { input, 0 };
     Vamp::Plugin::FeatureSet fs = myPlugin->process(tmp, rt);
 }

Library function process is virtually defined in base class:

 /**
 * Process a single block of input data.
 *
 * If the plugin's inputDomain is TimeDomain, inputBuffers will
 * point to one array of floats per input channel, and each of
 * these arrays will contain blockSize consecutive audio samples
 * (the host will zero-pad as necessary).  The timestamp in this
 * case will be the real time in seconds of the start of the
 * supplied block of samples.
 *
 * If the plugin's inputDomain is FrequencyDomain, inputBuffers
 * will point to one array of floats per input channel, and each
 * of these arrays will contain blockSize/2+1 consecutive pairs of
 * real and imaginary component floats corresponding to bins
 * 0..(blockSize/2) of the FFT output.  That is, bin 0 (the first
 * pair of floats) contains the DC output, up to bin blockSize/2
 * which contains the Nyquist-frequency output.  There will
 * therefore be blockSize+2 floats per channel in total.  The
 * timestamp will be the real time in seconds of the centre of the
 * FFT input window (i.e. the very first block passed to process
 * might contain the FFT of half a block of zero samples and the
 * first half-block of the actual data, with a timestamp of zero).
 *
 * Return any features that have become available after this
 * process call.  (These do not necessarily have to fall within
 * the process block, except for OneSamplePerStep outputs.)
 */
virtual FeatureSet process(const float *const *inputBuffers,
               RealTime timestamp) = 0;

and here in the actual header:

FeatureSet process(const float *const *inputBuffers, Vamp::RealTime timestamp);

I think the EXC_BAD_ACCESS may be stemming from the library function wanting a zero-padded array, and my not giving it one. (a) Does that sound reasonable, and (b) If so, is it time to ask a different SO question?

Thanks all for your help so far, it's very enlightening/clarifying/educational/interesting.

Body:
float *

is not the same as

float **

so casting input to inputBuffers isn't going to work.

Generally, casting from non-const to const is implicit, you don't need to do anything special. You can't cast from const to non-const as easily. Which is logical when you think about it.


问:
One hardware MFA token, multiple accounts
Is it possible to use one hardware MFA device with multiple AWS accounts? E.g. I have 80 accounts and would like to have one token to handle MFA to all of them. Having 80 separate tokens will be tedious and hard to manage.

Logic tells me that this is not possible because you need to add a serial number of a hardware MFA device to the AWS when setting up the MFA.

答:
Currently, there are three ways that allow you to setup root accounts in AWS.

Virtual MFA Device: This is those authenticator apps that you can find and install from an app store on your device. You can also manage multiple accounts within one app.

U2F Security Key: U2F is an open authentication standard hosted by the FIDO Alliance and it is a more advanced security protocol. Currently there is only one company called Yubico that supports here. Yubico has many different keys (called YubiKey) that you can plug in and tap to register or authenticate yourself. It also supports multiple accounts per key.

Other Hardware: This is where Gemalto tokens come into play. The device has a display to show the code that you can authenticate yourself. One key per account which is not so convenient if you need to manage multiple accounts.

One thing I want to point out here is the YubiKey. Although it is an U2F device, it actually can be used as a Virtual MFA Device (sorry that you can only pick one method as AWS allows you to use one way). You simply download an app called Yubico Authenticator. All the accounts (seeds) are planted into the key itself. This means it only shows the related accounts to the key and no more. With this way (YubiKey + Yubico Authenticator), you can even set up one key to one account, one key to multiple accounts, multiple keys to one account, and multiple keys to multiple accounts. To assign one key to multiple account, you just plug in keys while the same QR code is showing, and use the Yubico Authenticator to snap the QR code to assign.


问:
joined query output to csv file in mysql workbench?
I know you can export a table from mysql workbench easily however how do I output joined table to csv file?

I've checked How to export table data in MySql Workbench to csv? and following its suggestions tried

```<code>select
  *
  INTO OUTFILE '/tmp/products.csv'
  FIELDS TERMINATED BY ','
  ENCLOSED BY '"'
  ESCAPED BY '\\'
  LINES TERMINATED BY '\n'
from table_1 t_1
left join table_2 t_2
  on t_1.id= t_2.u_id;
</code>```

which does not seem to work.

I've tried querying

```<code>select
  *
from table_1 t_1
left join join table_2 t_2
  on t_1.id= t_2.u_id;
</code>```

and simply using Export in mysql workbench but it only outputs 1000rows. I know that you can change the limit but my dataset if very large.

How to output to csv?

答:
Your question lacks few details.

If you had tried the first command

```<code>select
  *
  INTO OUTFILE '/tmp/products.csv'
  ...
</code>```

workbench (or so), you might have missed the error message - OR ELSE you missed to post the error here

Check if --secure-file-priv option is set. And you shall run your first choice itself.

If you run your second choice and then export from workbench, increase this

MySQL Workbench >> Edit >> Preferences >> SQL Queries tab >> Query
  Results (Limit Rows).

Body:
I know you can export a table from mysql workbench easily however how do I output joined table to csv file?

I've checked How to export table data in MySql Workbench to csv? and following its suggestions tried

select
  *
  INTO OUTFILE '/tmp/products.csv'
  FIELDS TERMINATED BY ','
  ENCLOSED BY '"'
  ESCAPED BY '\\'
  LINES TERMINATED BY '\n'
from table_1 t_1
left join table_2 t_2
  on t_1.id= t_2.u_id;

which does not seem to work.

I've tried querying

select
  *
from table_1 t_1
left join join table_2 t_2
  on t_1.id= t_2.u_id;

and simply using Export in mysql workbench but it only outputs 1000rows. I know that you can change the limit but my dataset if very large.

How to output to csv?

Body:
Your question lacks few details.

If you had tried the first command

select
  *
  INTO OUTFILE '/tmp/products.csv'
  ...

workbench (or so), you might have missed the error message - OR ELSE you missed to post the error here

Check if --secure-file-priv option is set. And you shall run your first choice itself.

If you run your second choice and then export from workbench, increase this

MySQL Workbench >> Edit >> Preferences >> SQL Queries tab >> Query
  Results (Limit Rows).


问:
generate dataframe with different values in r
I am trying to create a dataframe such that each ab values have for each dates. Please help!

```<code>ab <- c("ab","cd")
dates <- c("Jan 2018","Jan 2019","Mar 2019")
</code>```

Expected_Output:

```<code>  ab    dates
1 ab Jan 2018
2 ab Jan 2019
3 ab Mar 2019
4 cd Jan 2018
5 cd Jan 2019
6 cd Mar 2019
</code>```

答:
With tidyr,  this can be done with crossing

```<code>library(tidyr)
crossing(ab, dates)
# A tibble: 6 x 2
#  ab    dates
#  <chr> <chr>
#1 ab    Jan 2018
#2 ab    Jan 2019
#3 ab    Mar 2019
#4 cd    Jan 2018
#5 cd    Jan 2019
#6 cd    Mar 2019
</code>```

Or using CJ from data.table

```<code>library(data.table)
CJ(ab, dates)
</code>```

Or using expand.grid from base R

```<code>expand.grid(ab = ab, dates = dates)
</code>```

Body:
I am trying to create a dataframe such that each ab values have for each dates. Please help!

ab <- c("ab","cd")
dates <- c("Jan 2018","Jan 2019","Mar 2019")

Expected_Output:

  ab    dates
1 ab Jan 2018
2 ab Jan 2019
3 ab Mar 2019
4 cd Jan 2018
5 cd Jan 2019
6 cd Mar 2019

Body:
With tidyr,  this can be done with crossing

library(tidyr)
crossing(ab, dates)
# A tibble: 6 x 2
#  ab    dates
#  <chr> <chr>
#1 ab    Jan 2018
#2 ab    Jan 2019
#3 ab    Mar 2019
#4 cd    Jan 2018
#5 cd    Jan 2019
#6 cd    Mar 2019

Or using CJ from data.table

library(data.table)
CJ(ab, dates)

Or using expand.grid from base R

expand.grid(ab = ab, dates = dates)


问:
Excluding unit tests from external library
I newly replaced spongyCastle by bouncyCastle in an Android project:

```<code>implementation "org.bouncycastle:bcpkix-jdk15on:$project.bouncyCastleVersion"
</code>```

Since then on Jenkins (our CI) it seems that there are tests being executed from this lib, could this be true? I never saw external libs with units tests executing automatically. The issue is now I see many failed tests, for example:

```<code>org.bouncycastle.pqc.crypto.qtesla.QTeslaKeyEncodingTests.testDecodeEncodePrivateKeyQT3P    27 ms   1
 org.bouncycastle.pqc.crypto.qtesla.QTeslaKeyEncodingTests.testDecodeEncodePublicKeyQT3P
</code>```

Is there a way to exlude al unit tets from an imported library in gradle?

答:
Try changing your test command to this:</p>

```<code>./gradlew --stacktrace testDebug --tests=your.package.name createDebugCoverageReport mergeAndroidReports --continue || true
</code>```

Body:
I newly replaced spongyCastle by bouncyCastle in an Android project:

implementation "org.bouncycastle:bcpkix-jdk15on:$project.bouncyCastleVersion"

Since then on Jenkins (our CI) it seems that there are tests being executed from this lib, could this be true? I never saw external libs with units tests executing automatically. The issue is now I see many failed tests, for example:

org.bouncycastle.pqc.crypto.qtesla.QTeslaKeyEncodingTests.testDecodeEncodePrivateKeyQT3P    27 ms   1
 org.bouncycastle.pqc.crypto.qtesla.QTeslaKeyEncodingTests.testDecodeEncodePublicKeyQT3P

Is there a way to exlude al unit tets from an imported library in gradle?

Body:
Try changing your test command to this:

./gradlew --stacktrace testDebug --tests=your.package.name createDebugCoverageReport mergeAndroidReports --continue || true


问:
Jquery DataTable : Table headers Appearing Twice
I am using Jquery DataTable for Displaying the data.

```<code><style type="text/css" title="currentStyle">
    @import '../css/demo_page.css';
    @import '../css/demo_table.css';
</style>
<script type="text/javascript" language="javascript" src="../scripts/jquery.js"></script>
<script type="text/javascript" language="javascript" src="../scripts/jquery.dataTables.js"></script>

<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $('#example1').dataTable({
            "bAutoWidth": false,
            "sScrollX": "100%",
            "bPaginate": false
        });
    });
</script>

<div id="demo">
    <table cellpadding="0" cellspacing="0" border="0" class="display" id="example1" >
        <thead>
            <tr>
                <th>Query GeneID</th>
                <th>Hit GeneID</th>
                <th>EXON</th>
                <th>Percentage</th>
                <th>Align Length</th>
                <th>No. of Mismatch</th>
                <th>Gaps</th>
                <th>Start Query Gene</th>
                <th>End Query Gene</th>
                <th>Hit Gene Start </th>
                <th>End Gene Start</th>
                <th>E-Value</th>
                <th>Bit-Score</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td> AA1</td>
                <td>AA3</td>
                <td>AAAAAAAAAAAAA</td>
                <td>AA3</td>
                <td> AA1</td>
                <td> AA1</td>
                <td> AA1</td>
                <td> AA1</td>
                <td> AA1</td>
                <td> AA1</td>
                <td>AA1</td>
                <td>AA1</td>
                <td>AA1</td>
            </tr>
        </tbody>
    </table>
</div>
</code>```

When I try to hit the page , data is getting shown with all fields in  appearing twice.
if I remove:

```<code>"bAutoWidth": false,
"sScrollX": "100%",
"bPaginate": false
</code>```

Then headers are displaying correctly, I want to keep the scroll enable to make the data scroll able.

Can you please tell where I am making  mistake.

Thanks,
Kapil

答:
I had a similar situation in MVC where headers and footers were both duplicating each time the table was updated from a popup.

The problem turned out to be the duplication of a script:

```<code><script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
</code>```

The above script was in both the page and the popup. When I removed it from the popup the headers and footers stopped duplicating.

Body:
I am using Jquery DataTable for Displaying the data.

                Query GeneID
                Hit GeneID
                EXON
                Percentage
                Align Length
                No. of Mismatch
                Gaps
                Start Query Gene
                End Query Gene
                Hit Gene Start
                End Gene Start
                E-Value
                Bit-Score

                 AA1
                AA3
                AAAAAAAAAAAAA
                AA3
                 AA1
                 AA1
                 AA1
                 AA1
                 AA1
                 AA1
                AA1
                AA1
                AA1

When I try to hit the page , data is getting shown with all fields in  appearing twice.
if I remove:

"bAutoWidth": false,
"sScrollX": "100%",
"bPaginate": false

Then headers are displaying correctly, I want to keep the scroll enable to make the data scroll able.

Can you please tell where I am making  mistake.

Thanks,
Kapil

Body:
I had a similar situation in MVC where headers and footers were both duplicating each time the table was updated from a popup.

The problem turned out to be the duplication of a script:

<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

The above script was in both the page and the popup. When I removed it from the popup the headers and footers stopped duplicating.


问:
How do I achieve the effect that two div with the width of 50% open simultaneously?
I try to create the effect that two gates open simultaneously, I tried modifying the width property but I achieved the desired effect only on the left gate. The idea is that the right gate be closed from the center to the right border. Thanks in advance for your suggestions

body {
    position: relative;
    margin: 0;
    background-color: goldenrod;
  }

  .gate {
    position: absolute;
    background-color: gray;
    width: 50%;
    height: 100vh;
    float: left;
    box-sizing: border-box;
    animation-duration: 5s;
    animation-fill-mode: forwards;
  }

  .left-gate {
    animation-name: left;
    border-right: 1px solid white;
    top: 0;
    left: 0;
  }

  .right-gate {
    animation-name: right;
    top: 0;
    left: 50%;
  }

  @keyframes left {
    from {
      width: 50%;
    }

    to {
      width: 0;
    }
  }

  @keyframes right {
    from {
      width: 50%;
    }

    to {
      width: 0;
    }
  }```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="gate left-gate"></div>
<div class="gate right-gate"></div></code>```

答:
Add right: 0 to the right gate:

body {
    position: relative;
    margin: 0;
    background-color: goldenrod;
  }

  .gate {
    position: absolute;
    background-color: gray;
    width: 50%;
    height: 100vh;
    float: left;
    box-sizing: border-box;
    animation-duration: 5s;
    animation-fill-mode: forwards;
  }

  .left-gate {
    animation-name: left;
    border-right: 1px solid white;
    top: 0;
    left: 0;
  }

  .right-gate {
    animation-name: right;
    top: 0;
    right: 0;
  }

  @keyframes left {
    from {
      width: 50%;
    }

    to {
      width: 0;
    }
  }

  @keyframes right {
    from {
      width: 50%;
    }

    to {
      width: 0;
    }
  }```
<pre class="snippet-code-html lang-html prettyprint-override"><code><div class="gate left-gate"></div>
<div class="gate right-gate"></div></code>```

Body:
Add right: 0 to the right gate:

body {
    position: relative;
    margin: 0;
    background-color: goldenrod;
  }

  .gate {
    position: absolute;
    background-color: gray;
    width: 50%;
    height: 100vh;
    float: left;
    box-sizing: border-box;
    animation-duration: 5s;
    animation-fill-mode: forwards;
  }

  .left-gate {
    animation-name: left;
    border-right: 1px solid white;
    top: 0;
    left: 0;
  }

  .right-gate {
    animation-name: right;
    top: 0;
    right: 0;
  }

  @keyframes left {
    from {
      width: 50%;
    }

    to {
      width: 0;
    }
  }

  @keyframes right {
    from {
      width: 50%;
    }

    to {
      width: 0;
    }
  }

<div class="gate left-gate"></div>
<div class="gate right-gate"></div>


问:
How can I check for exact length using jQuery Validation?
Using the jQuery Validation plugin to validate forms, how would you confirm that a string is exactly X characters long?</p>

答:
example:</p>

```<code>rules : {
"someFieldName":{digits:true,minlength:20,maxlength:20}
}
</code>```

Body:
example:

rules : {
"someFieldName":{digits:true,minlength:20,maxlength:20}
}


问:
How to send Suricata log to Kafka?
After install and config Suricata 5.0.2 according to document https://suricata.readthedocs.io/.

I try to change some configuration in suricata.yaml by adding:

```<code>- alert-json-log:
      enabled: yes
      filetype: kafka
      kafka:
        brokers: >
         xxx-kafka-online003:9092,
         xxx-kafka-online004:9092,
         xxx-kafka-online005:9092,
         xxx-kafka-online006:9092,
         xxx-kafka-online007:9092
        topic: nsm_event
        partitions: 5
      http: yes
</code>```

Next I run Suricata, and receive the error
Invalid entry for alert-json-log.filetype.  Expected "regular" (default), "unix_stream", "pcie" or "unix_dgram"

I don't know to configure on Suricata to enable sending log to Kafka topics.
Please help.

答:
I don't see Kafka listed as an output type, therefore "no, there is not"

Refer docs: https://suricata.readthedocs.io/en/suricata-5.0.2/output/index.html

Plus, I'm not sure I understand what you expect http: yes to do since Kafka is not an HTTP service

What you could do is set filetype: unix_stream, then I assume that is Syslog, and you can add another service like Kafka Connect or Fluentd or Logstash to route that data to Kafka.

In other words, services don't need to integrate with Kafka. Plenty of alternatives exist to read files or stdout/stderr/syslog streams

Title: How to send Suricata log to Kafka?

Body:
After install and config Suricata 5.0.2 according to document https://suricata.readthedocs.io/.

I try to change some configuration in suricata.yaml by adding:

- alert-json-log:
      enabled: yes
      filetype: kafka
      kafka:
        brokers: >
         xxx-kafka-online003:9092,
         xxx-kafka-online004:9092,
         xxx-kafka-online005:9092,
         xxx-kafka-online006:9092,
         xxx-kafka-online007:9092
        topic: nsm_event
        partitions: 5
      http: yes

Next I run Suricata, and receive the error
Invalid entry for alert-json-log.filetype.  Expected "regular" (default), "unix_stream", "pcie" or "unix_dgram"

I don't know to configure on Suricata to enable sending log to Kafka topics.
Please help.


问:
Floating on both sides separately without interfering
I would like to decorate and shape the left and right borders of my text using float and clear properties. To start I have this markup which looks like the left and right stair steps should both start at the top of the page. However, the stairs aren't vertically aligned; the second stairs seem to always start drawing at the second-to-last step on the other side.

Left

Left Two

Left Number Three

Right

Right Two

Right Number Three

... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... .```

The result (same in Chrome + Firefox):

I'd like the stairs to both start at the beginning, as they seem like they should, since I'm only clearing floats on the side they're on. Note that I'm not just shaping the borders, I'm also showing important things on the shaped sides.

答:
You could fake this with clearing the float on the 3 last items and use direction to get the right alignement for the last 3 items too.

example from a list:

/* required */

.stairCcase {
  direction: rtl;
}

.stairCcase li {
  float: left;
  clear: left;
  direction: ltr;
}

.stairCcase :nth-child(3)~li {
  float: none;
  clear: none;
  display: table; /* or width:max-content instead display */
}

/* reset & styles for demo*/

* {
  margin: 0;
  padding: 0;
  list-style-type: none;
}

ul {
  width: 10em;
  margin: auto;
}

li {
  background: green;
  padding:0.25em;
}

li:nth-child(3)~li {
  background: red;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><ul class="stairCcase">
  <li>0 </li>
  <li>0 0 </li>
  <li> 0 0 0</li>
  <li>1</li>
  <li>1 1</li>
  <li>1 1 1</li>
</ul></code>```

a few ressources about float : https://css-tricks.com/?s=float&orderby=relevance&post_type=post%2Cpage%2Cguide & direction https://css-tricks.com/?s=direction&orderby=relevance&post_type=post%2Cpage%2Cguide

for such a layout, you should consider using the grid display.

Example below

ul {
  display: grid;
  grid-template-areas:
  "left    .     .   .   .      .    right "
  "left2 left2   .   .   .    right2 right2"
  "left3 left3 left3 . right3 right3 right3"
}

li {
  grid-area: left;
  border:solid;
}

li:nth-child(2) {
  grid-area: left2
}

li:nth-child(3) {
  grid-area: left3
}

li:nth-child(4) {
  grid-area: right
}

li:nth-child(5) {
  grid-area: right2
}

li:nth-child(6) {
  grid-area: right3
}

* {
  margin: 0;
  padding: 0;
  list-style: none;
}

ul {
  counter-reset: lis
}

li:before {
  counter-increment: lis;
  content: counter(lis);
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul></code>```

https://css-tricks.com/snippets/css/complete-guide-grid/#prop-grid-template-areas

Body:
I would like to decorate and shape the left and right borders of my text using float and clear properties. To start I have this markup which looks like the left and right stair steps should both start at the top of the page. However, the stairs aren't vertically aligned; the second stairs seem to always start drawing at the second-to-last step on the other side.

Left

Left Two

Left Number Three

Right

Right Two

Right Number Three

... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... .

The result (same in Chrome + Firefox):

I'd like the stairs to both start at the beginning, as they seem like they should, since I'm only clearing floats on the side they're on. Note that I'm not just shaping the borders, I'm also showing important things on the shaped sides.

Body:
You could fake this with clearing the float on the 3 last items and use direction to get the right alignement for the last 3 items too.

example from a list:

/* required */

.stairCcase {
  direction: rtl;
}

.stairCcase li {
  float: left;
  clear: left;
  direction: ltr;
}

.stairCcase :nth-child(3)~li {
  float: none;
  clear: none;
  display: table; /* or width:max-content instead display */
}

/* reset & styles for demo*/

* {
  margin: 0;
  padding: 0;
  list-style-type: none;
}

ul {
  width: 10em;
  margin: auto;
}

li {
  background: green;
  padding:0.25em;
}

li:nth-child(3)~li {
  background: red;
}

<ul class="stairCcase">
  <li>0 </li>
  <li>0 0 </li>
  <li> 0 0 0</li>
  <li>1</li>
  <li>1 1</li>
  <li>1 1 1</li>
</ul>

a few ressources about float : https://css-tricks.com/?s=float&orderby=relevance&post_type=post%2Cpage%2Cguide & direction https://css-tricks.com/?s=direction&orderby=relevance&post_type=post%2Cpage%2Cguide

for such a layout, you should consider using the grid display.

Example below

ul {
  display: grid;
  grid-template-areas:
  "left    .     .   .   .      .    right "
  "left2 left2   .   .   .    right2 right2"
  "left3 left3 left3 . right3 right3 right3"
}

li {
  grid-area: left;
  border:solid;
}

li:nth-child(2) {
  grid-area: left2
}

li:nth-child(3) {
  grid-area: left3
}

li:nth-child(4) {
  grid-area: right
}

li:nth-child(5) {
  grid-area: right2
}

li:nth-child(6) {
  grid-area: right3
}

* {
  margin: 0;
  padding: 0;
  list-style: none;
}

ul {
  counter-reset: lis
}

li:before {
  counter-increment: lis;
  content: counter(lis);
}

<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>

https://css-tricks.com/snippets/css/complete-guide-grid/#prop-grid-template-areas


问:
AdSense (AdMob) Reporting Revenue Reporting
About the AdSense reporting api - GET  /accounts/accountId/reports

Does it retrieve AdMob only data (as an ad network) or can it include data from other mediated Ad Networks as well?

答:
I know this is old, but it does use AdMob, there are some params here for Ad Sense:
https://developers.google.com/adsense/management/metrics-dimensions

Also there is some for AdMob here:
https://developers.google.com/admob/android/reporting

You can mix and match and even try your request here to see the output:
https://developers.google.com/adsense/management/v1.4/reference/accounts/reports/generate

We use:

APP_NAME - the name of your app

APP_PLATFORM - iOS / Android

AD_UNIT_SIZE_NAME - this tells us Ad Type, Rewarded, Interstitial etc, in AdMob i think this is Ad Format.

My problem is how to send an authorised request for this from a Unity c# app sitting on a PC but that's a problem for another thread...


问:
Reshape by only two columns in pandas
This should be a simple question, but I think I'm just missing the keyword for what I should be searching for.

Suppose I have a two-column long dataset, like follows:

```<code>test = pd.DataFrame(
       {
          'color': ['white', 'white', 'white',
                    'red', 'red', 'red',
                    'black', 'black', 'black'],
           'value': [1,2,3,4,5,6,7,8,9]
       })
</code>```

How can I get it into the following format:

```<code>    white   red    black
    1        4       7
    2        5       8
    3        6       9
</code>```

I realize this should be a simple pivot function like:

test.pivot('color', 'value'), but this produces the values in the diagonal with surrounding NaN. Is there any way around this?

答:
Try:

test["id"]=test.groupby("color")["color"].cumcount()

test.pivot(index="id", columns="color", values="value")
```

<p>Outputs:</p>

<pre class="lang-py prettyprint-override"><code>color  black  red  white
id
0          7    4      1
1          8    5      2
2          9    6      3
</code>```

Title: Reshape by only two columns in pandas

Body:
This should be a simple question, but I think I'm just missing the keyword for what I should be searching for.

Suppose I have a two-column long dataset, like follows:

test = pd.DataFrame(
       {
          'color': ['white', 'white', 'white',
                    'red', 'red', 'red',
                    'black', 'black', 'black'],
           'value': [1,2,3,4,5,6,7,8,9]
       })

How can I get it into the following format:

    white   red    black
    1        4       7
    2        5       8
    3        6       9

I realize this should be a simple pivot function like:

test.pivot('color', 'value'), but this produces the values in the diagonal with surrounding NaN. Is there any way around this?

Body:
Try:

test["id"]=test.groupby("color")["color"].cumcount()

test.pivot(index="id", columns="color", values="value")

Outputs:

color  black  red  white
id
0          7    4      1
1          8    5      2
2          9    6      3


问:
Why does Microsoft suggest we use properties for static data types, rather than static fields?
Is this still relevant?

It is recommended that you use static properties instead of public static fields whenever possible.

If so, why?

EDIT: I don't think it's an opinion based-question when we are asking for clarification on why Microsoft's coding style is defined the way it is.

答:
In the linked article, there is another link, Field Usage Guidelines, containing this explanation:

Do not use instance fields that are public or protected (Public or Protected in Visual Basic). If you avoid exposing fields directly to the developer, classes can be versioned more easily because a field cannot be changed to a property while maintaining binary compatibility.

That applies to static fields as well as to non-static/instance fields. It means that you can later change how the field backing a property maybe set or get without breaking dependencies.

If you released the class with a public field, you can never change this without troubling developers already accessing that field.


问:
ELB Balancing Stateful Servers
Let's say i have this HTTP2 service, that has a list of users and this user hair color, in memory and database well.

Now i want to scale this up into multiple nodes - however i do not want the same user to be in two different servers memory - each server shall handle those specific users. This means i need to inform the load balancer where each user is being handled. In case of de-scaling, i need to inform this user is nowhere and can be routed to any server or by a given rule - IE server with less memory being used.

Would any1 know if ALB load balancer supports that ? One path i was thinking of using Query string parameter-based routing, so i could inform in the request itself something like destination_node = (int)user_id % 4 in case i had 4 nodes for instance - and this worked well in a proof of concept but that leads to a few issues:

The service itself would need to know how many instances there are to balance.

I could not guarantee even balancing, its basically a luck based balancing.

What would be the preferred approach for this, or what is a common way of solving this problem ? Does AWS ELB supports this out of the box ? I was trying to avoid having to write my own balancer, a middleware that keeps track of what services are handling what users, whose responsibility would be distributing the requests among those servers.

答:
In AWS Application Load Balancer (ALB) it is possible to write Routing-Rules on

Host Header

HTTP Header

HTTP Request Method

Path Pattern

Query String

Source IP

But at the moment there is no way to route under dynamic conditions.
If it possible to group your data, i would prefere path pattern like
/users/blond/123

Title: ELB Balancing Stateful Servers


问:
How to create 2 actions with same path but different HTTP methods DRF
So I'm trying to have different actions under the same method, but the last defined method is the only one that work, is there a way to do this?

views.py

```<code>class SomeViewSet(ModelViewSet):
    ...

    @detail_route(methods=['post'])
    def methodname(self, request, pk=None):
    ... action 1

    @detail_route(methods=['get'])
    def methodname(self, request, pk=None):
    ... action 2
</code>```

答:
The most rational method I've found here:

```<code>class MyViewSet(ViewSet):
    @action(detail=False)
    def example(self, request, **kwargs):
        """GET implementation."""

    @example.mapping.post
    def create_example(self, request, **kwargs):
        """POST implementation."""
</code>```

That method provides possibility to use self.action inside of another viewset methods with correct value.

Body:
So I'm trying to have different actions under the same method, but the last defined method is the only one that work, is there a way to do this?

views.py

class SomeViewSet(ModelViewSet):
    ...

    @detail_route(methods=['post'])
    def methodname(self, request, pk=None):
    ... action 1

    @detail_route(methods=['get'])
    def methodname(self, request, pk=None):
    ... action 2

Body:
The most rational method I've found here:

class MyViewSet(ViewSet):
    @action(detail=False)
    def example(self, request, **kwargs):
        """GET implementation."""

    @example.mapping.post
    def create_example(self, request, **kwargs):
        """POST implementation."""

That method provides possibility to use self.action inside of another viewset methods with correct value.


问:
Python Lists: Appending a string and removing ALL quotations
The code below is taken from a larger project I'm working on.

The aim of this section is to get the output formatted as a list, with no quotation marks anywhere.

However when I try format the output as a list, it has quotation marks around the outside, I've looked through multiple forums and couldn't find anyone whose had the exact issue.

Coded Input:

```<code>colourChart = []
tempColour = tempAlpha

tempColour = "%s" % (', '.join(tempColour)) #! Creates a string, I need it in a List format.
print("What I need it to be formatted as:\n [" + tempColour + "]")

colourChart.append(tempColour) #! Takes previous string, appends to List, adds quotes tho :(
print("What it actually gets formatted as:\n",colourChart)
</code>```

Output:

```<code>What I need it to be formatted as:
 [colourAlpha(0.3), colourAlpha(0.4), colourAlpha(0.5), colourAlpha(0.6)]

What it actually gets formatted as:
 ['colourAlpha(0.3), colourAlpha(0.4), colourAlpha(0.5), colourAlpha(0.6)']
</code>```

This link has the Python script I was testing this with:
https://drive.google.com/drive/folders/1y1-SkyUoDYrR1UU0ssJd6izjfNsbwZUJ?usp=sharing

答:
Do </p>

```<code>colourChart.append(tempColour.split(","))
print("What it actually gets formatted as:\n",colourChart)
</code>```

Body:
The code below is taken from a larger project I'm working on.

The aim of this section is to get the output formatted as a list, with no quotation marks anywhere.

However when I try format the output as a list, it has quotation marks around the outside, I've looked through multiple forums and couldn't find anyone whose had the exact issue.

Coded Input:

colourChart = []
tempColour = tempAlpha

tempColour = "%s" % (', '.join(tempColour)) #! Creates a string, I need it in a List format.
print("What I need it to be formatted as:\n [" + tempColour + "]")

colourChart.append(tempColour) #! Takes previous string, appends to List, adds quotes tho :(
print("What it actually gets formatted as:\n",colourChart)

Output:

What I need it to be formatted as:
 [colourAlpha(0.3), colourAlpha(0.4), colourAlpha(0.5), colourAlpha(0.6)]

What it actually gets formatted as:
 ['colourAlpha(0.3), colourAlpha(0.4), colourAlpha(0.5), colourAlpha(0.6)']

This link has the Python script I was testing this with:
https://drive.google.com/drive/folders/1y1-SkyUoDYrR1UU0ssJd6izjfNsbwZUJ?usp=sharing

Body:
Do

colourChart.append(tempColour.split(","))
print("What it actually gets formatted as:\n",colourChart)


问:
Wrap Complex C++ Class using the Python Extension API
I'm pretty new to creating C++ class that I can use from within Python. I've skimmed through a lot of posts on the internet. Be it on StackOverflow, gist, github, ... I've also read the documentation, but I'm not sure how I can solve my issue.

Basically, the idea is to do this: http://www.speedupcode.com/c-class-in-python3/
As I want to avoid the burden of creating my own python newtype, I thought that using PyCapsule_New and PyCapsule_GetPointer as in the example above could be a workaround, but maybe I'm misleading, and I still need to create complex datatype.

Here is the header of my class I want to be able to call from python:

template<typename T>
class Graph {
    public:
        Graph(const vector3D<T>& image, const std::string& similarity, size_t d) : img(image) {...}
        component<T> method1(const int k, const bool post_processing=true);

    private:
        caller_map<T> cmap;
        vector3D<T> img;  // input image with 3 channels
        caller<T> sim;  // similarity function
        size_t h;  // height of the image
        size_t w;  // width of the image
        size_t n_vertices;  // number of pixels in the input image
        size_t conn;  // radius for the number of connected pixels
        vector1D<edge<T>> edges;  // graph = vector of edges

        void create_graph(size_t d);
        tuple2 find(vector2D<subset>& subsets, tuple2 i);
        void unite(vector2D<subset>& subsets, tuple2 x, tuple2 y);
};
```

<p>So As you can see my class contains complex structures. <code>vector1D</code> is just <code>std::vector</code> but edge is a structure defined by</p>

<pre class="lang-cpp prettyprint-override"><code>template<typename T>
struct edge {
    tuple2 src;
    tuple2 dst;
    T weight;
};
</code>```

and some methods use other complex structures.

Anyway, I have created my own Python binding. Here I only put the relevant functions. I created my constructor as follow:

static PyObject *construct(PyObject *self, PyObject *args, PyObject *kwargs) {
    // Arguments passed from Python
    PyArrayObject* arr = nullptr;

    // Default if arguments not given
    const char* sim = "2000";   // similarity function used
    const size_t conn = 1;  // Number of neighbor pixels to consider

    char *keywords[] = {
        "image",
        "similarity",
        "d",
        nullptr
    };

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|sI:vGraph", keywords, PyArray_Converter, &arr, &sim, &conn)) {
        // Will need to DECRF(arr) somewhere?
        return nullptr;
    }

    set<string> sim_strings = {"1976", "1994", "2000"};

    if (sim_strings.find(sim) == sim_strings.end()) {
        PyErr_SetString(PyExc_ValueError, "This similarity function does not exist");
        Py_RETURN_NONE;
    }

    // Parse the 3D numpy array to vector3D
    vector3D<float> img = parse_PyArrayFloat<float>(arr);

    // call the Constructor
    Graph<float>* graph = new Graph<float>(img, sim, conn);

    // Create Python capsule with a pointer to the `Graph` object
    PyObject* graphCapsule = PyCapsule_New((void * ) graph, "graphptr", vgraph_destructor);

    // int success = PyCapsule_SetPointer(graphCapsule, (void *)graph);
    // Return the Python capsule with the pointer to `Graph` object
    // return Py_BuildValue("O", graphCapsule);
    return graphCapsule;
}
```

<p>While debugging my code, I can see that my constructor return my graphCapsule object and that it is different from <code>nullptr</code>.</p>

<p>then I create my <code>method1</code> function as follow:</p>

<pre class="lang-cpp prettyprint-override"><code>static PyObject *method1(PyObject *self, PyObject *args) {
    // Capsule with the pointer to `Graph` object
    PyObject* graphCapsule_;

    // Default parameters of the method1 function
    size_t k = 300;
    bool post_processing = true;

    if (!PyArg_ParseTuple(args, "O|Ip", &graphCapsule_, &k, &post_processing)) {
        return nullptr;
    }

    // Get the pointer to `Graph` object
    Graph<float>* graph = reinterpret_cast<Graph<float>* >(PyCapsule_GetPointer(graphCapsule_, "graphptr"));

    // Call method1
    component<float> ctov = graph->method1(k, post_processing);

    // Convert component<float> to a Python dict (bad because we need to copy?)
    PyObject* result = parse_component<float>(ctov);

    return result;
}
</code>```

When I compile everything, I will have a vgraph.so library and I will call it from Python using:

import vgraph
import numpy as np
import scipy.misc

class Vgraph():
    def __init__(self, img, similarity, d):
        self.graphCapsule = vgraph.construct(img, similarity, d)

    def method1(self, k=150, post_processing=True):
        vgraph.method1(self.graphCapsule, k, post_processing)

if __name__ == "__main__":
    img = scipy.misc.imread("pic.jpg")
    img = scipy.misc.imresize(img, (512, 512)) / 255

    g = Vgraph(lab_img, "1976", d=1)
    cc = g.method1(k=150, post_processing=False)
```

The idea is that I save the PyObject pointer returned by the vgraph.construct. Then I call method1 passing the PyObject pointer the int k = 150 and the bool postprocessing.

This is why in the C++ implementation of *method1, I use:
!PyArg_ParseTuple(args, "O|Ip", &graphCapsule_, &k, &post_processing) to parse these 3 objects.

The problem is, even though, when I'm debugging, I recover k=150 and post_processing=False which come from the way I'm calling the C++ from Python... I'm also getting a 0X0, that is to say a nullptr in the variable graphCapsule_...

So obviously the rest of the code cannot work...

I thought that PyObject * is a pointer to my graph of type Graph<float> *, so, I was expecting ParseTuple to recover my PyObject * pointer that I can then use in PyCapsule_GetPointer to retrieve my Object.

How can I make my code work? Do I need to define my own PyObject so that ParseTuple understand it? Is there a simpler way to do it?

Thanks a lot!

Note: If I break in my python code, I can see that my graph g contains a PyObject with the address it points to and the name of the object (here graphtr) so I was expecting my code to work...

Note2: If I need to create my own newtype, I have seen this stackoverflow post: How to wrap a C++ object using pure Python Extension API (python3)? but I think because of the complex objects of my Class, it will be quite difficult?

答:
I answer my own question.

I actually found the flaw in my code.

Both functions PyCapsule_GetPointer and PyCapsule_New work perfectly fine. As mentioned in my question, the issue arouse just after I was trying to Parse the capsule with the following code:

size_t k = 300;
bool post_processing = true;

if (!PyArg_ParseTuple(args, "O|Ip", &graphCapsule_, &k, &post_processing)) {
    return nullptr;
}

```

<p>The problem comes from the parsing of the other parameters.
Indeed, k is a <code>size_t</code> type so instead of using <code>I</code> for unsigned int, I should use <code>n</code> as the documentation mentions:</p>

```n (int) [Py_ssize_t]
Convert a Python integer to a C Py_ssize_t.
```

<p>Moreover, <code>post_processing</code> is a boolean so, even though the documentation mentions that a boolean can be parsed with a <code>p</code>:</p>

```p (bool) [int]
```

<p>I should initialize the boolean with type <code>int</code> instead of type <code>bool</code> as it is mentioned in this stackoverflow post</p>

<p>So, the working piece of code is:</p>

<pre class="lang-cpp prettyprint-override"><code>size_t k = 300;
int post_processing = true;

if (!PyArg_ParseTuple(args, "O|np", &graphCapsule_, &k, &post_processing)) {
    return nullptr;
}
</code>```

We can also use the O! options by passing the &Pycapsule_Type:

#include <pycapsule.h>
...
size_t k = 300;
int post_processing = true;

if (!PyArg_ParseTuple(args, "O!|np", &PyCapsule_Type, &graphCapsule_, &k, &post_processing)) {
    return nullptr;
}
```

<p>Finally, as mentioned in my question, It is actually straightforward to implement your own Python type based on this stackoverflow post. I have just copied/pasted and adapted the code to my need and it works like a charm without need to use <code>PyCaspule</code> anymore!</p>

<p><strong>Other useful pieces of information</strong>:</p>

<p>To debug your code (I used vscode on Linux), you can used mixed language debugging. The idea is to compile your C++ code into as shared library <code>.so</code>.</p>

<p>Once you're code is compiled you can import it in python:</p>

<pre class="lang-py prettyprint-override"><code>import my_lib
</code>```

where my_lib referred to my_lib.so file that you have generated.

To generate your .so file, you just need to execute:
g++ my_python_to_cpp_wrapper.cpp --ggdb -o my_python_to_cpp_wrapper.so

But, if you do this, you might missed to include the python library and stuff...

Fortunately, python provide a way to find the recommended flags for both compilation and linking:

you just need to execute (change your python version or look into /usr/local/bin eventually)

```<code>/usr/bin/python3.6m-config --cflags
</code>```

For me, it returned:

-I/usr/include/python3.6m -I/usr/include/python3.6m  -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.6-0aiVHW/python3.6-3.6.9=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O3 -Wall
```

<p>The same applying for linking (change your python version or look into /usr/local/bin eventually)</p>

```/usr/bin/python3.6m-config --ldflags
```

<p>gave me:</p>

```-L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu -L/usr/lib -lpython3.6m -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions
```

<p>Then, since we want to create a shared library <code>.so</code> we need to add the <code>-shared</code> flag as well as the <code>-fPIC</code> flags (otherwise it will complain). Finally, since we want to debug our code we should remove any <code>-Ox</code> such as <code>-O2</code> or <code>-O3</code> flag that optimizes the code, because during the debugging you will be prompt with <code><optimized out></code>. To avoid this, remove any optimization flags from your <code>g++</code> options. For example, in my case, my <strong>cpp</strong> file is called: <strong>vrgaph.cpp</strong> and here is how I compiled it:</p>

<pre class="lang-sh prettyprint-override"><code>g++ vgraph.cpp -ggdb -o vgraph.so -I/usr/include/python3.6m -I/usr/include/python3.6m  -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.6-0aiVHW/python3.6-3.6.9=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -Wall -L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu -L/usr/lib -lpython3.6m -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions -shared -fPIC
</code>```

You can see I'm usin -O1 instead of -O2 or -O3.

Once, compile you will have a .so file that you can import and use in python. For my example, I will have vgraph.so and in my python code, I can do:

import vgraph.so

# rest of the call that use you C++ backend code
```

Then you can easily debug your C++. There are some posts on the internet that explains how to do that with vs code/gdb/...

Body:
I'm pretty new to creating C++ class that I can use from within Python. I've skimmed through a lot of posts on the internet. Be it on StackOverflow, gist, github, ... I've also read the documentation, but I'm not sure how I can solve my issue.

Basically, the idea is to do this: http://www.speedupcode.com/c-class-in-python3/
As I want to avoid the burden of creating my own python newtype, I thought that using PyCapsule_New and PyCapsule_GetPointer as in the example above could be a workaround, but maybe I'm misleading, and I still need to create complex datatype.

Here is the header of my class I want to be able to call from python:

template<typename T>
class Graph {
    public:
        Graph(const vector3D<T>& image, const std::string& similarity, size_t d) : img(image) {...}
        component<T> method1(const int k, const bool post_processing=true);

    private:
        caller_map<T> cmap;
        vector3D<T> img;  // input image with 3 channels
        caller<T> sim;  // similarity function
        size_t h;  // height of the image
        size_t w;  // width of the image
        size_t n_vertices;  // number of pixels in the input image
        size_t conn;  // radius for the number of connected pixels
        vector1D<edge<T>> edges;  // graph = vector of edges

        void create_graph(size_t d);
        tuple2 find(vector2D<subset>& subsets, tuple2 i);
        void unite(vector2D<subset>& subsets, tuple2 x, tuple2 y);
};

So As you can see my class contains complex structures. vector1D is just std::vector but edge is a structure defined by

template<typename T>
struct edge {
    tuple2 src;
    tuple2 dst;
    T weight;
};

and some methods use other complex structures.

Anyway, I have created my own Python binding. Here I only put the relevant functions. I created my constructor as follow:

static PyObject *construct(PyObject *self, PyObject *args, PyObject *kwargs) {
    // Arguments passed from Python
    PyArrayObject* arr = nullptr;

    // Default if arguments not given
    const char* sim = "2000";   // similarity function used
    const size_t conn = 1;  // Number of neighbor pixels to consider

    char *keywords[] = {
        "image",
        "similarity",
        "d",
        nullptr
    };

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|sI:vGraph", keywords, PyArray_Converter, &arr, &sim, &conn)) {
        // Will need to DECRF(arr) somewhere?
        return nullptr;
    }

    set<string> sim_strings = {"1976", "1994", "2000"};

    if (sim_strings.find(sim) == sim_strings.end()) {
        PyErr_SetString(PyExc_ValueError, "This similarity function does not exist");
        Py_RETURN_NONE;
    }

    // Parse the 3D numpy array to vector3D
    vector3D<float> img = parse_PyArrayFloat<float>(arr);

    // call the Constructor
    Graph<float>* graph = new Graph<float>(img, sim, conn);

    // Create Python capsule with a pointer to the `Graph` object
    PyObject* graphCapsule = PyCapsule_New((void * ) graph, "graphptr", vgraph_destructor);

    // int success = PyCapsule_SetPointer(graphCapsule, (void *)graph);
    // Return the Python capsule with the pointer to `Graph` object
    // return Py_BuildValue("O", graphCapsule);
    return graphCapsule;
}

While debugging my code, I can see that my constructor return my graphCapsule object and that it is different from nullptr.

then I create my method1 function as follow:

static PyObject *method1(PyObject *self, PyObject *args) {
    // Capsule with the pointer to `Graph` object
    PyObject* graphCapsule_;

    // Default parameters of the method1 function
    size_t k = 300;
    bool post_processing = true;

    if (!PyArg_ParseTuple(args, "O|Ip", &graphCapsule_, &k, &post_processing)) {
        return nullptr;
    }

    // Get the pointer to `Graph` object
    Graph<float>* graph = reinterpret_cast<Graph<float>* >(PyCapsule_GetPointer(graphCapsule_, "graphptr"));

    // Call method1
    component<float> ctov = graph->method1(k, post_processing);

    // Convert component<float> to a Python dict (bad because we need to copy?)
    PyObject* result = parse_component<float>(ctov);

    return result;
}

When I compile everything, I will have a vgraph.so library and I will call it from Python using:

import vgraph
import numpy as np
import scipy.misc

class Vgraph():
    def __init__(self, img, similarity, d):
        self.graphCapsule = vgraph.construct(img, similarity, d)

    def method1(self, k=150, post_processing=True):
        vgraph.method1(self.graphCapsule, k, post_processing)

if __name__ == "__main__":
    img = scipy.misc.imread("pic.jpg")
    img = scipy.misc.imresize(img, (512, 512)) / 255

    g = Vgraph(lab_img, "1976", d=1)
    cc = g.method1(k=150, post_processing=False)

The idea is that I save the PyObject pointer returned by the vgraph.construct. Then I call method1 passing the PyObject pointer the int k = 150 and the bool postprocessing.

This is why in the C++ implementation of *method1, I use:
!PyArg_ParseTuple(args, "O|Ip", &graphCapsule_, &k, &post_processing) to parse these 3 objects.

The problem is, even though, when I'm debugging, I recover k=150 and post_processing=False which come from the way I'm calling the C++ from Python... I'm also getting a 0X0, that is to say a nullptr in the variable graphCapsule_...

So obviously the rest of the code cannot work...

I thought that PyObject * is a pointer to my graph of type Graph<float> *, so, I was expecting ParseTuple to recover my PyObject * pointer that I can then use in PyCapsule_GetPointer to retrieve my Object.

How can I make my code work? Do I need to define my own PyObject so that ParseTuple understand it? Is there a simpler way to do it?

Thanks a lot!

Note: If I break in my python code, I can see that my graph g contains a PyObject with the address it points to and the name of the object (here graphtr) so I was expecting my code to work...

Note2: If I need to create my own newtype, I have seen this stackoverflow post: How to wrap a C++ object using pure Python Extension API (python3)? but I think because of the complex objects of my Class, it will be quite difficult?

Body:
I answer my own question.

I actually found the flaw in my code.

Both functions PyCapsule_GetPointer and PyCapsule_New work perfectly fine. As mentioned in my question, the issue arouse just after I was trying to Parse the capsule with the following code:

size_t k = 300;
bool post_processing = true;

if (!PyArg_ParseTuple(args, "O|Ip", &graphCapsule_, &k, &post_processing)) {
    return nullptr;
}

The problem comes from the parsing of the other parameters.
Indeed, k is a size_t type so instead of using I for unsigned int, I should use n as the documentation mentions:

n (int) [Py_ssize_t]
Convert a Python integer to a C Py_ssize_t.

Moreover, post_processing is a boolean so, even though the documentation mentions that a boolean can be parsed with a p:

p (bool) [int]

I should initialize the boolean with type int instead of type bool as it is mentioned in this stackoverflow post

So, the working piece of code is:

size_t k = 300;
int post_processing = true;

if (!PyArg_ParseTuple(args, "O|np", &graphCapsule_, &k, &post_processing)) {
    return nullptr;
}

We can also use the O! options by passing the &Pycapsule_Type:

#include <pycapsule.h>
...
size_t k = 300;
int post_processing = true;

if (!PyArg_ParseTuple(args, "O!|np", &PyCapsule_Type, &graphCapsule_, &k, &post_processing)) {
    return nullptr;
}

Finally, as mentioned in my question, It is actually straightforward to implement your own Python type based on this stackoverflow post. I have just copied/pasted and adapted the code to my need and it works like a charm without need to use PyCaspule anymore!

Other useful pieces of information:

To debug your code (I used vscode on Linux), you can used mixed language debugging. The idea is to compile your C++ code into as shared library .so.

Once you're code is compiled you can import it in python:

import my_lib

where my_lib referred to my_lib.so file that you have generated.

To generate your .so file, you just need to execute:
g++ my_python_to_cpp_wrapper.cpp --ggdb -o my_python_to_cpp_wrapper.so

But, if you do this, you might missed to include the python library and stuff...

Fortunately, python provide a way to find the recommended flags for both compilation and linking:

you just need to execute (change your python version or look into /usr/local/bin eventually)

/usr/bin/python3.6m-config --cflags

For me, it returned:

-I/usr/include/python3.6m -I/usr/include/python3.6m  -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.6-0aiVHW/python3.6-3.6.9=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O3 -Wall

The same applying for linking (change your python version or look into /usr/local/bin eventually)

/usr/bin/python3.6m-config --ldflags

gave me:

-L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu -L/usr/lib -lpython3.6m -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions

Then, since we want to create a shared library .so we need to add the -shared flag as well as the -fPIC flags (otherwise it will complain). Finally, since we want to debug our code we should remove any -Ox such as -O2 or -O3 flag that optimizes the code, because during the debugging you will be prompt with <optimized out>. To avoid this, remove any optimization flags from your g++ options. For example, in my case, my cpp file is called: vrgaph.cpp and here is how I compiled it:

g++ vgraph.cpp -ggdb -o vgraph.so -I/usr/include/python3.6m -I/usr/include/python3.6m  -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.6-0aiVHW/python3.6-3.6.9=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -Wall -L/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu -L/usr/lib -lpython3.6m -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions -shared -fPIC

You can see I'm usin -O1 instead of -O2 or -O3.

Once, compile you will have a .so file that you can import and use in python. For my example, I will have vgraph.so and in my python code, I can do:

import vgraph.so

# rest of the call that use you C++ backend code

Then you can easily debug your C++. There are some posts on the internet that explains how to do that with vs code/gdb/...


问:
In Kafka, How can we get the metadata of a topic?
In Kafka, How can we get the metadata of a topic? If there is an option to get metadata and if I'm trying to get the metadata of a topic that doesn't existing, what happens?</p>

答:
kafka-topics --describe --bootstrap-server localhost:9092 --topic name

Or refer Javadoc for AdminClient class, or similar for a programming language of your choice


问:
Are there limits to the number of tables that can be joined in Oracle?
I'm writing a fairly large query, and I have 2 tables, inner joined and 15 tables left-outer joined. when I add the 16, I get ORA-03113: end-of-file on communication channel. If I remove a table to add the new one, the query works fine.

The query runs fine for SQL Server, it's just Oracle that seems to be struggling.

答:
ORA-3113 errors are encountered when the connection between the client and the database is interrupted unexpectedly. The two main reasons are:

A network problem has occured.

The server process handling your request has been terminated

If you can reliably produce an ORA-3113 with a statement, you are in the second case, most likely a bug from Oracle. When an Oracle process dies unexpectedly, it will leave a trace file in your USER_DUMP_DEST (in SQL*Plus show parameter user_dump_dest). Open the trace file with an editor and look for other more significant error message (search for ORA-).

Most likely you will find a server error like ORA-7445 or ORA-600. In that case you will need to open the Oracle support note ID 153788.1 to further identify the problem and its solution.


问:
Does putting a command in single quotes have any effect in a bash script?
I've stumbled upon a script where the author puts every external command (that is not a builtin or function) in single quotes. As I have never seen that coding style before, I'm wondering, it there any difference between the following two examples:

'cat' $myfile

vs.

cat $myfile

Please ignore the fact that the author chose not to quote the variable holding the file name. I would definitely put that in double quotes and I'm aware of the effect of quotes around the variable. I'm merely interested in whether the quotes around the command have any effect. Both examples work fine, but it is my understanding that the single quotes are superfluous here. Is that correct?

Thanks!

答:
In most cases, quoting a string that has no whitespaces or special characters is strictly equivalent to not quoting at all.

However, there are a few cases where quoting makes a difference.

Preventing alias

Many linux distributions automatically setup aliases for programs like ls or grep, e.g.:

alias ls='ls --color=auto'
alias grep='grep --color=auto'
```
<p>If you call the quoted command, the alias will not be expanded:</p>
<pre class="lang-sh prettyprint-override"><code>ls / # shows the root files and folders with colors
'ls' / # shows the root files and folders without any color
</code>```

The alias is not used even if the command is partly quoted:

'l's / # shows the root files and folders without any color

(I would strongly advise against doing that, though)

Reserved keywords

Some keywords must not be quoted. For instance this will work:

time cat $myfile

But this will fail:

'time' cat $myfile

-bash: time: command not found

Tilde expansion

The tilde character ~ may be used to reach the home directory. Alone, it points to the home directory of the current user:

ls ~

When followed directly by a username, it points to the home directory of that user:

ls ~root

When quoting the tilde character, the directory is not expanded and the shell tries look for a file containing the tilde character instead:

ls '~'

ls: cannot access ~: No such file or directory

Tests

The [[ ]] operator can make use of test commands, such as -f, -e, etc. However the directive must not be quoted.

[[ -e /tmp ]] && echo /tmp exists || echo /tmp is missing

/tmp exists

[[ '-e' /tmp ]] && echo /tmp exists || echo /tmp is missing

-bash: conditional binary operator expected

-bash: syntax error near `/tmp'

Conclusion

There are many cases where quoting does make a difference. I think the first situation is the reason why your author wrote the code.

PS. this list is not exhaustive, I especially omitted cases like whitespaces, asterisks, pipes, ampersands, etc. because I assume you already know why someone would want to quote them.

Body:
In most cases, quoting a string that has no whitespaces or special characters is strictly equivalent to not quoting at all.

However, there are a few cases where quoting makes a difference.

Preventing alias

Many linux distributions automatically setup aliases for programs like ls or grep, e.g.:

alias ls='ls --color=auto'
alias grep='grep --color=auto'

If you call the quoted command, the alias will not be expanded:

ls / # shows the root files and folders with colors
'ls' / # shows the root files and folders without any color

The alias is not used even if the command is partly quoted:

'l's / # shows the root files and folders without any color

(I would strongly advise against doing that, though)

Reserved keywords

Some keywords must not be quoted. For instance this will work:

time cat $myfile

But this will fail:

'time' cat $myfile

-bash: time: command not found

Tilde expansion

The tilde character ~ may be used to reach the home directory. Alone, it points to the home directory of the current user:

ls ~

When followed directly by a username, it points to the home directory of that user:

ls ~root

When quoting the tilde character, the directory is not expanded and the shell tries look for a file containing the tilde character instead:

ls '~'

ls: cannot access ~: No such file or directory

Tests

The [[ ]] operator can make use of test commands, such as -f, -e, etc. However the directive must not be quoted.

[[ -e /tmp ]] && echo /tmp exists || echo /tmp is missing

/tmp exists

[[ '-e' /tmp ]] && echo /tmp exists || echo /tmp is missing

-bash: conditional binary operator expected

-bash: syntax error near `/tmp'

Conclusion

There are many cases where quoting does make a difference. I think the first situation is the reason why your author wrote the code.

PS. this list is not exhaustive, I especially omitted cases like whitespaces, asterisks, pipes, ampersands, etc. because I assume you already know why someone would want to quote them.