问:
How to resolve memory related errors that arise from interaction between C objects in a C++ wrapper?

The problem

I am writing a thin C++ wrapper around an object oriented C library. The idea was to automate memory management, but so far its not been very automatic. Basically when I use my wrapper classes, I get all kinds of memory access and inappropriate freeing problems.

Minimal example of C library

Lets say the C library consists of A and B classes, each of which have a few 'methods' associated with them:

```<code>#include <memory>
#include "cstring"
#include "iostream"

extern "C" {
typedef struct {
    unsigned char *string;
} A;

A *c_newA(const char *string) {
    A *a = (A *) malloc(sizeof(A)); // yes I know, don't use malloc in C++. This is a demo to simulate the C library that uses it.
    auto *s = (char *) malloc(strlen(string) + 1);
    strcpy(s, string);
    a->string = (unsigned char *) s;
    return a;
}

void c_freeA(A *a) {
    free(a->string);
    free(a);
}

void c_printA(A *a) {
    std::cout << a->string << std::endl;
}

typedef struct {
    A *firstA;
    A *secondA;
} B;

B *c_newB(const char *first, const char *second) {
    B *b = (B *) malloc(sizeof(B));
    b->firstA = c_newA(first);
    b->secondA = c_newA(second);
    return b;
}

void c_freeB(B *b) {
    c_freeA(b->firstA);
    c_freeA(b->secondA);
    free(b);
}

void c_printB(B *b) {
    std::cout << b->firstA->string << ", " << b->secondA->string << std::endl;
}

A *c_getFirstA(B *b) {
    return b->firstA;
}

A *c_getSecondA(B *b) {
    return b->secondA;
}

}
</code>```

Test the 'C lib'

```<code>void testA() {
    A *a = c_newA("An A");
    c_printA(a);
    c_freeA(a);
    // outputs: "An A"
    // valgrind is happy =]
}
</code>```
```<code>void testB() {
    B *b = c_newB("first A", "second A");
    c_printB(b);
    c_freeB(b);
    // outputs: "first A, second A"
    // valgrind is happy =]
}

</code>```

Wrapper classes for A and B

```<code>class AWrapper {

    struct deleter {
        void operator()(A *a) {
            c_freeA(a);
        }
    };

    std::unique_ptr<A, deleter> aptr_;
public:

    explicit AWrapper(A *a)
            : aptr_(a) {
    }

    static AWrapper fromString(const std::string &string) { // preferred way of instantiating
        A *a = c_newA(string.c_str());
        return AWrapper(a);
    }

    void printA() {
        c_printA(aptr_.get());
    }
};

class BWrapper {

    struct deleter {
        void operator()(B *b) {
            c_freeB(b);
        }
    };

    std::unique_ptr<B, deleter> bptr_;
public:
    explicit BWrapper(B *b)
            : bptr_(std::unique_ptr<B, deleter>(b)) {
    }

    static BWrapper fromString(const std::string &first, const std::string &second) {
        B *b = c_newB(first.c_str(), second.c_str());
        return BWrapper(b);
    }

    void printB() {
        c_printB(bptr_.get());
    }

    AWrapper getFirstA(){
        return AWrapper(c_getFirstA(bptr_.get()));
    }

    AWrapper getSecondA(){
        return AWrapper(c_getSecondA(bptr_.get()));
    }

};
</code>```

Wrapper tests

```<code>
void testAWrapper() {
    AWrapper a = AWrapper::fromString("An A");
    a.printA();
    // outputs "An A"
    // valgrind is happy =]
}

void testBWrapper() {
    BWrapper b = BWrapper::fromString("first A", "second A");
    b.printB();
    // outputs "first A"
    // valgrind is happy =]
}

</code>```

Demonstration of the problem

Great, so I move on and develop the full wrapper (lot of classes) and realise that when classes like this (i.e. aggregation relationship) are both in scope, C++ will automatically call the descructors of both classes separately, but because of the structure of the underlying library (i.e. the calls to free), we get memory problems:

```<code>void testUsingAWrapperAndBWrapperTogether() {
    BWrapper b = BWrapper::fromString("first A", "second A");
    AWrapper a1 = b.getFirstA();
    // valgrind no happy =[

}

</code>```

Valgrind output

Things I've tried

Cloning not possible

The first thing I tried was to take a copy of A, rather than having them try to free the same A. This, while a good idea, is not possible in my case because of the nature of the library I'm using. There is actually a catching mechanism in place so that when you create a new A with a string its seen before, it'll give you back the same A. See this question for my attempts at cloning A.

Custom destructors

I took the code for the C library destructors (freeA and freeB here) and copied them into my source code. Then I tried to modify them such that A does not get freed by B. This has partially worked. Some instances of memory problems have been resolved, but because this idea does not tackle the problem at hand (just kind of temporarily glosses over the main issue), new problems keep popping up, some of which are obscure and difficult to debug.

The question

So at last we arive at the question: How can I modify this C++ wrapper to resolve the memory problems that arise due to the interactions between the underlying C objects? Can I make better use of smart pointers? Should I abandon the C wrapper completly and just use the libraries pointers as is? Or is there a better way I haven't thought of?

Thanks in advance.

Edits: response to the comments

Since asking the previous question (linked above) I have restructed my code so that the wrapper is being developed and built in the same library as the one it wraps. So the objects are no longer opaque.

The pointers are generated from function calls to the library, which uses calloc or malloc to allocate.

In the real code A is raptor_uri* (typdef librdf_uri*) from raptor2 and is allocated with librdf_new_uri while B is raptor_term* (aka librdf_node*) and allocated with librdf_new_node_* functions. The librdf_node has a librdf_uri field.

Edit 2

I can also point to the line of code where the same A is returned if its the same string. See line 137 here

答:
The problem is that getFirstA and getSecondA return instances of AWrapper, which is an owning type.  This means that when constructing an AWrapper you're giving up the ownership of an A *, but getFirstA and getFirstB don't do that.  The pointers from which the returned objects are constructed are managed by a BWrapper.

The easiest solution is that you should return an A * instead of the wrapper class.  This way you're not passing the ownership of the inner A member.  I also would recommend making the constructors taking pointers in the wrapper classes private, and having a fromPointer static method similar to fromString, which takes ownership of the pointer passed to it.  This way you won't accidently make instances of the wrapper classes from raw pointers.

If you want to avoid using raw pointers or want to have methods on the returned objects from getFirstA and getSecondA you could write a simple reference wrapper, which has a raw pointer as a member.

```<code>class AReference
{
private:
    A *a_ref_;
public:
    explicit AReference(A *a_ref) : a_ref_(a_ref) {}

    // other methods here, such as print or get

};
</code>```

Body:
The problem

I am writing a thin C++ wrapper around an object oriented C library. The idea was to automate memory management, but so far its not been very automatic. Basically when I use my wrapper classes, I get all kinds of memory access and inappropriate freeing problems.

Minimal example of C library

Lets say the C library consists of A and B classes, each of which have a few 'methods' associated with them:

#include <memory>
#include "cstring"
#include "iostream"

extern "C" {
typedef struct {
    unsigned char *string;
} A;

A *c_newA(const char *string) {
    A *a = (A *) malloc(sizeof(A)); // yes I know, don't use malloc in C++. This is a demo to simulate the C library that uses it.
    auto *s = (char *) malloc(strlen(string) + 1);
    strcpy(s, string);
    a->string = (unsigned char *) s;
    return a;
}

void c_freeA(A *a) {
    free(a->string);
    free(a);
}

void c_printA(A *a) {
    std::cout <string << std::endl;
}

typedef struct {
    A *firstA;
    A *secondA;
} B;

B *c_newB(const char *first, const char *second) {
    B *b = (B *) malloc(sizeof(B));
    b->firstA = c_newA(first);
    b->secondA = c_newA(second);
    return b;
}

void c_freeB(B *b) {
    c_freeA(b->firstA);
    c_freeA(b->secondA);
    free(b);
}

void c_printB(B *b) {
    std::cout <firstA->string << ", " <secondA->string << std::endl;
}

A *c_getFirstA(B *b) {
    return b->firstA;
}

A *c_getSecondA(B *b) {
    return b->secondA;
}

}

Test the 'C lib'

void testA() {
    A *a = c_newA("An A");
    c_printA(a);
    c_freeA(a);
    // outputs: "An A"
    // valgrind is happy =]
}

void testB() {
    B *b = c_newB("first A", "second A");
    c_printB(b);
    c_freeB(b);
    // outputs: "first A, second A"
    // valgrind is happy =]
}

Wrapper classes for A and B

class AWrapper {

    struct deleter {
        void operator()(A *a) {
            c_freeA(a);
        }
    };

    std::unique_ptr aptr_;
public:

    explicit AWrapper(A *a)
            : aptr_(a) {
    }

    static AWrapper fromString(const std::string &string) { // preferred way of instantiating
        A *a = c_newA(string.c_str());
        return AWrapper(a);
    }

    void printA() {
        c_printA(aptr_.get());
    }
};

class BWrapper {

    struct deleter {
        void operator()(B *b) {
            c_freeB(b);
        }
    };

    std::unique_ptr bptr_;
public:
    explicit BWrapper(B *b)
            : bptr_(std::unique_ptr(b)) {
    }

    static BWrapper fromString(const std::string &first, const std::string &second) {
        B *b = c_newB(first.c_str(), second.c_str());
        return BWrapper(b);
    }

    void printB() {
        c_printB(bptr_.get());
    }

    AWrapper getFirstA(){
        return AWrapper(c_getFirstA(bptr_.get()));
    }

    AWrapper getSecondA(){
        return AWrapper(c_getSecondA(bptr_.get()));
    }

};

Wrapper tests

void testAWrapper() {
    AWrapper a = AWrapper::fromString("An A");
    a.printA();
    // outputs "An A"
    // valgrind is happy =]
}

void testBWrapper() {
    BWrapper b = BWrapper::fromString("first A", "second A");
    b.printB();
    // outputs "first A"
    // valgrind is happy =]
}

Demonstration of the problem

Great, so I move on and develop the full wrapper (lot of classes) and realise that when classes like this (i.e. aggregation relationship) are both in scope, C++ will automatically call the descructors of both classes separately, but because of the structure of the underlying library (i.e. the calls to free), we get memory problems:

void testUsingAWrapperAndBWrapperTogether() {
    BWrapper b = BWrapper::fromString("first A", "second A");
    AWrapper a1 = b.getFirstA();
    // valgrind no happy =[

}

Valgrind output

Things I've tried

Cloning not possible

The first thing I tried was to take a copy of A, rather than having them try to free the same A. This, while a good idea, is not possible in my case because of the nature of the library I'm using. There is actually a catching mechanism in place so that when you create a new A with a string its seen before, it'll give you back the same A. See this question for my attempts at cloning A.

Custom destructors

I took the code for the C library destructors (freeA and freeB here) and copied them into my source code. Then I tried to modify them such that A does not get freed by B. This has partially worked. Some instances of memory problems have been resolved, but because this idea does not tackle the problem at hand (just kind of temporarily glosses over the main issue), new problems keep popping up, some of which are obscure and difficult to debug.

The question

So at last we arive at the question: How can I modify this C++ wrapper to resolve the memory problems that arise due to the interactions between the underlying C objects? Can I make better use of smart pointers? Should I abandon the C wrapper completly and just use the libraries pointers as is? Or is there a better way I haven't thought of?

Thanks in advance.

Edits: response to the comments

Since asking the previous question (linked above) I have restructed my code so that the wrapper is being developed and built in the same library as the one it wraps. So the objects are no longer opaque.

The pointers are generated from function calls to the library, which uses calloc or malloc to allocate.

In the real code A is raptor_uri* (typdef librdf_uri*) from raptor2 and is allocated with librdf_new_uri while B is raptor_term* (aka librdf_node*) and allocated with librdf_new_node_* functions. The librdf_node has a librdf_uri field.

Edit 2

I can also point to the line of code where the same A is returned if its the same string. See line 137 here

Body:
The problem is that getFirstA and getSecondA return instances of AWrapper, which is an owning type.  This means that when constructing an AWrapper you're giving up the ownership of an A *, but getFirstA and getFirstB don't do that.  The pointers from which the returned objects are constructed are managed by a BWrapper.

The easiest solution is that you should return an A * instead of the wrapper class.  This way you're not passing the ownership of the inner A member.  I also would recommend making the constructors taking pointers in the wrapper classes private, and having a fromPointer static method similar to fromString, which takes ownership of the pointer passed to it.  This way you won't accidently make instances of the wrapper classes from raw pointers.

If you want to avoid using raw pointers or want to have methods on the returned objects from getFirstA and getSecondA you could write a simple reference wrapper, which has a raw pointer as a member.

class AReference
{
private:
    A *a_ref_;
public:
    explicit AReference(A *a_ref) : a_ref_(a_ref) {}

    // other methods here, such as print or get

};


来源: stackexchange

问:
What is the Pythonic way to ID when in a bytestring values stop being in a particular range?
I need a function which takes a bytestring of length n, and returns only those first m bytes that can be decoded into the Latin1 character set. (I am writing a translator for a very old file format, and one of the fields providing a human readable label is—unlike much of the rest of the file format—of fixed length, so after the human readable name, there's a byte character that is either less than b'\x20'—space—or greater than b'\xe7'—tilde—and then uninitialized byte values for the remainder of the fixed field.)

Currently I make do thus:

```<code>n = 27
def extract_name(bytestring):
  for i in range(0,n):
    if (bytestring[i:i+1]<b' ') or (bytestring[i:i+1]>b'~'):
      name_length = i
      break
    else:
      name_length = n
  name = bytestring[0:name_length].decode('Latin1')
  return name
</code>```

This works:

```<code>>>>my_string = b'My Great Name\xf0\x1e\x23\x23\xe1\x06\xbc\x1b\x8a\xf7\x00\x00\x00\x00\x00\x00\x00'
>>>extract_name(my_string)
'My Great Name'
</code>```

However, I feel clunky about the code, and wonder if I can make it more Pythonic.

答:
Regular expressions are your friend!

```<code>import re
def extract_name(bytestring):
  return re.sub(b'[\x00-\x1f].*|[\x7f-\xff].*', b'', bytestring).decode('Latin1')
</code>```

This gives the desired behavior:

```<code>>>>my_string = b'My Great Name\xf0\x1e\x23\x23\xe1\x06\xbc\x1b\x8a\xf7\x00\x00\x00\x00\x00\x00\x00'
>>>extract_name(my_string)
'My Great Name'
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I need a function which takes a bytestring of length n, and returns only those first m bytes that can be decoded into the Latin1 character set. (I am writing a translator for a very old file format, and one of the fields providing a human readable label is—unlike much of the rest of the file format—of fixed length, so after the human readable name, there's a byte character that is either less than b'\x20'—space—or greater than b'\xe7'—tilde—and then uninitialized byte values for the remainder of the fixed field.)

Currently I make do thus:

n = 27
def extract_name(bytestring):
  for i in range(0,n):
    if (bytestring[i:i+1]b'~'):
      name_length = i
      break
    else:
      name_length = n
  name = bytestring[0:name_length].decode('Latin1')
  return name

This works:

>>>my_string = b'My Great Name\xf0\x1e\x23\x23\xe1\x06\xbc\x1b\x8a\xf7\x00\x00\x00\x00\x00\x00\x00'
>>>extract_name(my_string)
'My Great Name'

However, I feel clunky about the code, and wonder if I can make it more Pythonic.

Body:
Regular expressions are your friend!

import re
def extract_name(bytestring):
  return re.sub(b'[\x00-\x1f].*|[\x7f-\xff].*', b'', bytestring).decode('Latin1')

This gives the desired behavior:

>>>my_string = b'My Great Name\xf0\x1e\x23\x23\xe1\x06\xbc\x1b\x8a\xf7\x00\x00\x00\x00\x00\x00\x00'
>>>extract_name(my_string)
'My Great Name'


问:
Inserting an image from local directory in thymeleaf spring framework (with maven)
I have developed a project using this link: https://spring.io/guides/gs/serving-web-content/ I used maven to develop above project.

I have two html files under this. abc.html and xyz.html. To insert images in the html page, I have used the url like this:

```<code><img src="https://example.com/pic_mountain.jpg" alt="Mountain View" style="width:304px;height:228px">
</code>```

But I want to use an image file located in my server instead. I tried placing the file in the same directory of html file but its not working. I even tried giving full path but of no use. This is an ubuntu OS. Please help me out here. Is there any place where I can configure the base path or basically how to put an image from my local folder.

答:
Recently I had similar issue, but in my case, the spring security was making a problem. As mentioned in other answers and documentation:

```<code><img th:src="@{/img/values.png}" alt="Media Resource"/>
</code>```

should be enough. But since the spring security has been added to my project, I had to all /img/ ** for get Requests and add addResourceHandlers. Here is the code:

```<code>@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(
                "/webjars/**",
                "/img/**",
                "/css/**",
                "/js/**")
                .addResourceLocations(
                        "classpath:/META-INF/resources/webjars/",
                        "classpath:/static/img/",
                        "classpath:/static/css/",
                        "classpath:/static/js/");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable();
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS);

        http.authorizeRequests().antMatchers(HttpMethod.GET, "/js/**", "/css/**", "/img/**" ,"/pressiplus", "/public/**", "/index", "/", "/login").permitAll();
        http.authorizeRequests()
                .antMatchers("/secure/admin/**").hasAnyRole("ADMIN","USER")
                .antMatchers("/secure/admin/**").hasAnyRole("ADMIN")
                .and()
                .formLogin()  //login configuration
                    .loginPage("/login")
                    .failureUrl("/login-error")
                    .loginProcessingUrl("/login")
                    .usernameParameter("email")
                    .passwordParameter("password")
                    .successHandler(myAuthenticationSuccessHandler())
                .and()
                .logout()    //logout configuration
                .logoutUrl("/logout")
                .logoutSuccessHandler(myLogoutSuccessHandler)
                .and()
                    .rememberMe()
                        .tokenRepository(persistentTokenRepository())
                        .tokenValiditySeconds(7 * 24 * 60 * 60) // expires in 7 days
                .and()
                    .exceptionHandling() //exception handling configuration
                .accessDeniedHandler(accessDeniedHandler());
    }

}
</code>```

I hope this helps someone in the future

ContentLicense: CC BY-SA 3.0

Body:
I have developed a project using this link: https://spring.io/guides/gs/serving-web-content/ I used maven to develop above project.

I have two html files under this. abc.html and xyz.html. To insert images in the html page, I have used the url like this:

But I want to use an image file located in my server instead. I tried placing the file in the same directory of html file but its not working. I even tried giving full path but of no use. This is an ubuntu OS. Please help me out here. Is there any place where I can configure the base path or basically how to put an image from my local folder.

Body:
Recently I had similar issue, but in my case, the spring security was making a problem. As mentioned in other answers and documentation:

<img th:src="@{/img/values.png}" alt="Media Resource"/>

should be enough. But since the spring security has been added to my project, I had to all /img/ ** for get Requests and add addResourceHandlers. Here is the code:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(
                "/webjars/**",
                "/img/**",
                "/css/**",
                "/js/**")
                .addResourceLocations(
                        "classpath:/META-INF/resources/webjars/",
                        "classpath:/static/img/",
                        "classpath:/static/css/",
                        "classpath:/static/js/");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable();
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS);

        http.authorizeRequests().antMatchers(HttpMethod.GET, "/js/**", "/css/**", "/img/**" ,"/pressiplus", "/public/**", "/index", "/", "/login").permitAll();
        http.authorizeRequests()
                .antMatchers("/secure/admin/**").hasAnyRole("ADMIN","USER")
                .antMatchers("/secure/admin/**").hasAnyRole("ADMIN")
                .and()
                .formLogin()  //login configuration
                    .loginPage("/login")
                    .failureUrl("/login-error")
                    .loginProcessingUrl("/login")
                    .usernameParameter("email")
                    .passwordParameter("password")
                    .successHandler(myAuthenticationSuccessHandler())
                .and()
                .logout()    //logout configuration
                .logoutUrl("/logout")
                .logoutSuccessHandler(myLogoutSuccessHandler)
                .and()
                    .rememberMe()
                        .tokenRepository(persistentTokenRepository())
                        .tokenValiditySeconds(7 * 24 * 60 * 60) // expires in 7 days
                .and()
                    .exceptionHandling() //exception handling configuration
                .accessDeniedHandler(accessDeniedHandler());
    }

}

I hope this helps someone in the future


问:
awk, if else conditional when record contains a value
I'm having trouble getting an awk if/else conditional to properly trigger when the record contains a value. Running this in zsh on Mac OS Catalina.

This script (issue is on second to last line)...

```<code>echo "abcdefgh" >  ./temp
echo "abc\"\(\"h" >> ./temp
echo "abcdefgh" >> ./temp
echo "abcde\(h" >> ./temp

val='"\("'
key="NEW_NEW"
file="./temp"

echo $val
echo $key
echo $file

echo ""
echo "###############"
echo ""

awk '
    BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; len=length(old) }
    ($0 ~ /old/){ s=index($0,old); print substr($0,1,s-1) new substr($0,s+len) }{ print $0 }
' $val $key $file
</code>```

outputs:

```<code>"\("
NEW_NEW
./temp

###############

abcdefgh
abc"\("h
abcdefgh
abcde\(h
</code>```

I want to fix the script so that it changes the "\(" to NEW_NEW but skips the parenthesis without the quotes...

```<code>"\("
NEW_NEW
./temp

###############

abcdefgh
abcNEW_NEWh
abcdefgh
abcde\(h
</code>```

EDIT

This is an abbreviated version of the real script that I'm working on. The answer will need to include the variable expansions that the sample above has, in order for me to use the command in the larger script. The ARGV format in use is preserving special characters, so the main question I have is why the conditional isn’t triggered as expected.

答:
This code uses the variables val, key and file but assumes you can alter the content of val in order to compensate for shell expansion when passing to awk

```<code>$ file="./temp"; key="NEW_NEW"; val='"\\\\\\("'; \
     awk --posix -v val="$val" -v key="$key" '{gsub(val, key)}1' "$file"
abcdefgh
abcNEW_NEWh
abcdefgh
abcde\(h
</code>```

Body:
I'm having trouble getting an awk if/else conditional to properly trigger when the record contains a value. Running this in zsh on Mac OS Catalina.

This script (issue is on second to last line)...

echo "abcdefgh" >  ./temp
echo "abc\"\(\"h" >> ./temp
echo "abcdefgh" >> ./temp
echo "abcde\(h" >> ./temp

val='"\("'
key="NEW_NEW"
file="./temp"

echo $val
echo $key
echo $file

echo ""
echo "###############"
echo ""

awk '
    BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; len=length(old) }
    ($0 ~ /old/){ s=index($0,old); print substr($0,1,s-1) new substr($0,s+len) }{ print $0 }
' $val $key $file

outputs:

"\("
NEW_NEW
./temp

###############

abcdefgh
abc"\("h
abcdefgh
abcde\(h

I want to fix the script so that it changes the "\(" to NEW_NEW but skips the parenthesis without the quotes...

"\("
NEW_NEW
./temp

###############

abcdefgh
abcNEW_NEWh
abcdefgh
abcde\(h

EDIT

This is an abbreviated version of the real script that I'm working on. The answer will need to include the variable expansions that the sample above has, in order for me to use the command in the larger script. The ARGV format in use is preserving special characters, so the main question I have is why the conditional isn’t triggered as expected.

Body:
This code uses the variables val, key and file but assumes you can alter the content of val in order to compensate for shell expansion when passing to awk

$ file="./temp"; key="NEW_NEW"; val='"\\\\\\("'; \
     awk --posix -v val="$val" -v key="$key" '{gsub(val, key)}1' "$file"
abcdefgh
abcNEW_NEWh
abcdefgh
abcde\(h


问:
How to check if a given ID is for a product variation? woocoomerce
I have an array full of produtcs SKU's that i search for the ID.

```<code>$ID = wc_get_product_id_by_sku($CodProduto);
</code>```

then i initialize the object and set the stock for the current amount.

```<code>$Product = new WC_Product( $ID);
$return = wc_update_product_stock( $Product, $Stock);
</code>```

So far so good! All works fine. My problem is when the $ID is for a product variation it wont initialize the object and will give an error. So i initialize all ID's with:

```<code>$Product = new WC_Product_Variation( $ID );
</code>```

And then i update the stock like i did before.
The problem is that when a product DONT have variation, the woocommerce puts the Name as (no title) but the stock works fine.

Is there a way to check if the ID is for a variation without initialize the object $Product???

答:
I think you can use another method by searching in database

For example, if not sure whether this ID is for Product or Variation Product, you can use:

( get_post( not_sure_ID ) )->post_parent;

As a child post (Variation Product) , its parent value will not be 0. On the other hand is 0 (Product).

or

( get_post( not_sure_ID ) )->post_type;

If this ID is for Variation Product, return value should be product_variation

Body:
I have an array full of produtcs SKU's that i search for the ID.

$ID = wc_get_product_id_by_sku($CodProduto);

then i initialize the object and set the stock for the current amount.

$Product = new WC_Product( $ID);
$return = wc_update_product_stock( $Product, $Stock);

So far so good! All works fine. My problem is when the $ID is for a product variation it wont initialize the object and will give an error. So i initialize all ID's with:

$Product = new WC_Product_Variation( $ID );

And then i update the stock like i did before.
The problem is that when a product DONT have variation, the woocommerce puts the Name as (no title) but the stock works fine.

Is there a way to check if the ID is for a variation without initialize the object $Product???


问:
Apply a conditional to struct and produce list that of struct type
I'm trying to write a function to produce a list of a define struct type based on a conditional but can only get it to produce the full struct instead of just the name.

Here's my attempt that produces the entire struct..

```<code>(define-struct birthday (name day month)#:transparent)

(define (bdayRange startday endday lst)
      (filter (lambda (x) (<= startday (birthday-day x) endday)) lst))
</code>```

Not sure how to get the function to produce the birthday-name parameter given a list of birthday structs and the range of the days of the month from 1 to 30. I feel like the solution to this is obvious but I just can't figure it out.

Here's my attempt..

```<code>(define (bdayRange startday endday lst)
      (filter (lambda (x) (if (<= startday (birthday-day x) endday) (birthday-name x) '()) lst))
</code>```

Would the better approach be to parse the list of structs for the birthday-name or encapsulate the range conditional?

答:
Why not just map over the filtered list of birthday structures with birthday-name?

```<code>(define (bdayRange startday endday lst)
  (map birthday-name
       (filter (lambda (x) (<= startday (birthday-day x) endday)) lst)))
</code>```

This returns a list of names in the given range:

scratch.rkt> (define bday-list (list (birthday 'sam 25 'april)
                                     (birthday 'erin 15 'april)
                                     (birthday 'james 3 'april)
                                     (birthday 'randi 7 'april)))
scratch.rkt> (bdayRange 6 17 bday-list)
'(erin randi)
```

Body:
I'm trying to write a function to produce a list of a define struct type based on a conditional but can only get it to produce the full struct instead of just the name.

Here's my attempt that produces the entire struct..

(define-struct birthday (name day month)#:transparent)

(define (bdayRange startday endday lst)
      (filter (lambda (x) (<= startday (birthday-day x) endday)) lst))

Not sure how to get the function to produce the birthday-name parameter given a list of birthday structs and the range of the days of the month from 1 to 30. I feel like the solution to this is obvious but I just can't figure it out.

Here's my attempt..

(define (bdayRange startday endday lst)
      (filter (lambda (x) (if (<= startday (birthday-day x) endday) (birthday-name x) '()) lst))

Would the better approach be to parse the list of structs for the birthday-name or encapsulate the range conditional?

Body:
Why not just map over the filtered list of birthday structures with birthday-name?

(define (bdayRange startday endday lst)
  (map birthday-name
       (filter (lambda (x) (<= startday (birthday-day x) endday)) lst)))

This returns a list of names in the given range:

scratch.rkt> (define bday-list (list (birthday 'sam 25 'april)
                                     (birthday 'erin 15 'april)
                                     (birthday 'james 3 'april)
                                     (birthday 'randi 7 'april)))
scratch.rkt> (bdayRange 6 17 bday-list)
'(erin randi)


问:
Flutter: Cannot build iOS project
It was working project on android but when I try to build on iOS I couldn't do it, please share me with knowledge if face with this issue too. Sorry for my broken English.

Here's log file:

Flutter crash report; please file at https://github.com/flutter/flutter/issues.

command

flutter build ios --release

exception

ProcessException: ProcessException: Process exited abnormally:
Command line invocation:
    /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -list

2020-05-30 10:58:59.614 xcodebuild[22085:408139] Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
xcodebuild: error: Unable to read project 'Runner.xcodeproj'.
    Reason: The project ‘Runner’ is damaged and cannot be opened due to a parse error. Examine the project file for invalid edits or unresolved source control conflicts.

Path: /Users/itinternational/AndroidStudioProjects/shipping_driver/ios/Runner.xcodeproj
  Command: /usr/bin/xcodebuild -list

```<code>#0      RunResult.throwException (package:flutter_tools/src/base/process.dart:172:5)
#1      _DefaultProcessUtils.run (package:flutter_tools/src/base/process.dart:322:19)
<asynchronous suspension>
#2      XcodeProjectInterpreter.getInfo (package:flutter_tools/src/ios/xcodeproj.dart:389:50)
#3      buildXcodeProject (package:flutter_tools/src/ios/mac.dart:108:78)
#4      BuildIOSCommand.runCommand (package:flutter_tools/src/commands/build_ios.dart:83:43)
#5      _rootRunUnary (dart:async/zone.dart:1192:38)
#6      _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#7      _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#8      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#9      Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#10     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#11     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:36:15)
#12     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:298:13)
#13     ApplicationPackageStore.getPackageForPlatform (package:flutter_tools/src/application_package.dart)
#14     _rootRunUnary (dart:async/zone.dart:1192:38)
#15     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#16     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#17     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#18     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#19     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#20     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:36:15)
#21     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:298:13)
#22     BuildableIOSApp.fromProject (package:flutter_tools/src/application_package.dart)
#23     _rootRunUnary (dart:async/zone.dart:1192:38)
#24     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#25     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#26     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#27     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#28     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#29     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:36:15)
#30     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:298:13)
#31     IosProject.productBundleIdentifier (package:flutter_tools/src/project.dart)
#32     _rootRunUnary (dart:async/zone.dart:1192:38)
#33     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#34     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#35     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#36     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#37     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#38     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:36:15)
#39     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:298:13)
#40     IosProject.buildSettings (package:flutter_tools/src/project.dart)
#41     _rootRunUnary (dart:async/zone.dart:1192:38)
#42     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#43     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#44     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#45     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#46     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#47     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:36:15)
#48     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:298:13)
#49     XcodeProjectInterpreter.getBuildSettings (package:flutter_tools/src/ios/xcodeproj.dart)
#50     _asyncErrorWrapperHelper.errorCallback (dart:async-patch/async_patch.dart:86:61)
#51     _rootRunBinary (dart:async/zone.dart:1204:38)
#52     _CustomZone.runBinary (dart:async/zone.dart:1093:19)
#53     _FutureListener.handleError (dart:async/future_impl.dart:155:20)
#54     Future._propagateToListeners.handleError (dart:async/future_impl.dart:694:47)
#55     Future._propagateToListeners (dart:async/future_impl.dart:715:24)
#56     Future._completeError (dart:async/future_impl.dart:534:5)
#57     _AsyncAwaitCompleter.completeError (dart:async-patch/async_patch.dart:43:15)
#58     _DefaultProcessUtils.run (package:flutter_tools/src/base/process.dart)
#59     _rootRunUnary (dart:async/zone.dart:1192:38)
#60     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#61     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#62     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#63     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#64     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#65     Future.wait.<anonymous closure> (dart:async/future.dart:402:22)
#66     _rootRunUnary (dart:async/zone.dart:1192:38)
#67     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#68     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#69     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#70     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#71     Future._addListener.<anonymous closure> (dart:async/future_impl.dart:391:9)
#72     _rootRun (dart:async/zone.dart:1184:13)
#73     _CustomZone.run (dart:async/zone.dart:1077:19)
#74     _CustomZone.runGuarded (dart:async/zone.dart:979:7)
#75     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1019:23)
#76     _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
#77     _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
#78     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)
#79     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:169:5)
</code>```

flutter doctor

```<code>[✓] Flutter (Channel stable, v1.17.2, on Mac OS X 10.15.5 19F96, locale en-KG)
    • Flutter version 1.17.2 at /Users/itinternational/Downloads/flutter
    • Framework revision 5f21edf8b6 (33 hours ago), 2020-05-28 12:44:12 -0700
    • Engine revision b851c71829
    • Dart version 2.8.3

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/itinternational/Library/Android/sdk
    • Platform android-29, build-tools 29.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.5, Build version 11E608c
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 45.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] Connected device (1 available)
    • iPhone SE (2nd generation) • 9AD7BC02-943F-44FE-A4E3-FED33FE669DE • ios • com.apple.CoreSimulator.SimRuntime.iOS-13-5 (simulator)

• No issues found!
</code>```

答: Try removing your ios folder and re-create it running flutter create -i swift . . Probably something inside the folder is broken.

Title: Flutter: Cannot build iOS project

Body:
It was working project on android but when I try to build on iOS I couldn't do it, please share me with knowledge if face with this issue too. Sorry for my broken English.

Here's log file:

Flutter crash report; please file at https://github.com/flutter/flutter/issues.

command

flutter build ios --release

exception

ProcessException: ProcessException: Process exited abnormally:
Command line invocation:
    /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -list

2020-05-30 10:58:59.614 xcodebuild[22085:408139] Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
xcodebuild: error: Unable to read project 'Runner.xcodeproj'.
    Reason: The project ‘Runner’ is damaged and cannot be opened due to a parse error. Examine the project file for invalid edits or unresolved source control conflicts.

Path: /Users/itinternational/AndroidStudioProjects/shipping_driver/ios/Runner.xcodeproj
  Command: /usr/bin/xcodebuild -list

#0      RunResult.throwException (package:flutter_tools/src/base/process.dart:172:5)
#1      _DefaultProcessUtils.run (package:flutter_tools/src/base/process.dart:322:19)
<asynchronous suspension>
#2      XcodeProjectInterpreter.getInfo (package:flutter_tools/src/ios/xcodeproj.dart:389:50)
#3      buildXcodeProject (package:flutter_tools/src/ios/mac.dart:108:78)
#4      BuildIOSCommand.runCommand (package:flutter_tools/src/commands/build_ios.dart:83:43)
#5      _rootRunUnary (dart:async/zone.dart:1192:38)
#6      _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#7      _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#8      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#9      Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#10     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#11     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:36:15)
#12     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:298:13)
#13     ApplicationPackageStore.getPackageForPlatform (package:flutter_tools/src/application_package.dart)
#14     _rootRunUnary (dart:async/zone.dart:1192:38)
#15     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#16     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#17     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#18     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#19     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#20     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:36:15)
#21     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:298:13)
#22     BuildableIOSApp.fromProject (package:flutter_tools/src/application_package.dart)
#23     _rootRunUnary (dart:async/zone.dart:1192:38)
#24     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#25     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#26     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#27     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#28     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#29     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:36:15)
#30     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:298:13)
#31     IosProject.productBundleIdentifier (package:flutter_tools/src/project.dart)
#32     _rootRunUnary (dart:async/zone.dart:1192:38)
#33     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#34     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#35     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#36     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#37     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#38     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:36:15)
#39     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:298:13)
#40     IosProject.buildSettings (package:flutter_tools/src/project.dart)
#41     _rootRunUnary (dart:async/zone.dart:1192:38)
#42     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#43     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#44     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#45     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#46     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#47     _AsyncAwaitCompleter.complete (dart:async-patch/async_patch.dart:36:15)
#48     _completeOnAsyncReturn (dart:async-patch/async_patch.dart:298:13)
#49     XcodeProjectInterpreter.getBuildSettings (package:flutter_tools/src/ios/xcodeproj.dart)
#50     _asyncErrorWrapperHelper.errorCallback (dart:async-patch/async_patch.dart:86:61)
#51     _rootRunBinary (dart:async/zone.dart:1204:38)
#52     _CustomZone.runBinary (dart:async/zone.dart:1093:19)
#53     _FutureListener.handleError (dart:async/future_impl.dart:155:20)
#54     Future._propagateToListeners.handleError (dart:async/future_impl.dart:694:47)
#55     Future._propagateToListeners (dart:async/future_impl.dart:715:24)
#56     Future._completeError (dart:async/future_impl.dart:534:5)
#57     _AsyncAwaitCompleter.completeError (dart:async-patch/async_patch.dart:43:15)
#58     _DefaultProcessUtils.run (package:flutter_tools/src/base/process.dart)
#59     _rootRunUnary (dart:async/zone.dart:1192:38)
#60     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#61     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#62     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#63     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#64     Future._completeWithValue (dart:async/future_impl.dart:526:5)
#65     Future.wait.<anonymous closure> (dart:async/future.dart:402:22)
#66     _rootRunUnary (dart:async/zone.dart:1192:38)
#67     _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#68     _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
#69     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
#70     Future._propagateToListeners (dart:async/future_impl.dart:711:32)
#71     Future._addListener.<anonymous closure> (dart:async/future_impl.dart:391:9)
#72     _rootRun (dart:async/zone.dart:1184:13)
#73     _CustomZone.run (dart:async/zone.dart:1077:19)
#74     _CustomZone.runGuarded (dart:async/zone.dart:979:7)
#75     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1019:23)
#76     _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
#77     _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
#78     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)
#79     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:169:5)

flutter doctor

[✓] Flutter (Channel stable, v1.17.2, on Mac OS X 10.15.5 19F96, locale en-KG)
    • Flutter version 1.17.2 at /Users/itinternational/Downloads/flutter
    • Framework revision 5f21edf8b6 (33 hours ago), 2020-05-28 12:44:12 -0700
    • Engine revision b851c71829
    • Dart version 2.8.3

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/itinternational/Library/Android/sdk
    • Platform android-29, build-tools 29.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.5, Build version 11E608c
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 45.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] Connected device (1 available)
    • iPhone SE (2nd generation) • 9AD7BC02-943F-44FE-A4E3-FED33FE669DE • ios • com.apple.CoreSimulator.SimRuntime.iOS-13-5 (simulator)

• No issues found!


问:
How to read a portion of a line in a text file?
So, I have a text file with thousands of lines formatted similarly to this:

123456:0.8525000:1590882780:91011

These files are almost always a different length, and I only need to read the first two parts of the line, being 123456:0.8525000.

I know that I can split each line using C#, but I'm unsure how to only read the first 2 parts. Anyone have any idea on how to do this? Sorry if my question doesn't make sense, I can restate it if needed.

答:
The Split function returns a string[], an array of strings.

Just take the 2 first elements of the result of Split (with  : as the separator).

```<code>    var read = "123456:0.8525000:1590882780:91011";
    var values = read.Split(":");
    Console.WriteLine(values[0]); // 123456
    Console.WriteLine(values[1]); // 0.8525000
</code>```

.NET Fiddle

Don't forget that elements of values are string and not yet int or double values. See How to convert string to integer in C# for how to convert from string to number type.

Body:
The Split function returns a string[], an array of strings.

Just take the 2 first elements of the result of Split (with  : as the separator).

    var read = "123456:0.8525000:1590882780:91011";
    var values = read.Split(":");
    Console.WriteLine(values[0]); // 123456
    Console.WriteLine(values[1]); // 0.8525000

.NET Fiddle

Don't forget that elements of values are string and not yet int or double values. See How to convert string to integer in C# for how to convert from string to number type.


问:
How to search inside a specific block of code in IntelliJ IDEA?
How I can search within a specific block of code or selection in IntelliJ IDEA?

I got used to using this feature in Eclipse. In Eclipse you can just double click on the beginning of a curly bracket, and it'll highlight the entire block of code. After which you could do Command+f

(Ctrl+f on Windows) to search ONLY in the highlighted block, or you could just highlight whatever you need and search just that block of code.

答:
Go to Settings | Keymap, search for the Find... action in the Other group. This action should have the following description:

Find a string in active editor, shows
  modal dialog

Assign a keyboard shortcut to this action (you need to make a copy of default keymap to modify it), for example Ctrl+Alt+Shift+F.

When in editor, select any block of text, then press this shortcut, a dialog will open with the Scope automatically set to Selected text:

To quickly select the method body while standing on the opening curly brace use Ctrl+W.


问:
How to predicted class label (0 or 1) for new cases with new features value after building the DNN model?
I built the DNN model for predicting the survivability of breast cancer patients, I Want to predict class label (Died or Survive)for new cases, how to implement it? In summary: What I would like to apply is to predict a new case, without conducting the training process directly, just I want to show the result of the class label either died from breast cancer (1) or survived(0). </p>

```<code>dataset=pd.read_csv("C:/Users/User/Desktop/mixed 7 12-2-2020.csv",encoding='cp1252')

array = dataset.values
X = array[:, 0:33]
y = array[:, 33]

n_features, n_outputs = X.shape[1], 1

def create_model():

    model = Sequential()
    model.add(Input(n_features))
    model.add(BatchNormalization())
    model.add(Dense(51, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(68, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(85, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(85, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(68, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(51, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(n_outputs, activation='sigmoid'))
    model.compile(loss='binary_crossentropy',
              optimizer='Adam',
              metrics=['accuracy'])
    #model.summary()
    return model
checkpoint = tf.keras.callbacks.ModelCheckpoint(
                                "model.{epoch:02d}-{val_loss:.2f}.hdf5",
                                monitor='val_accuracy',
                                verbose=1,
                                save_best_only=True)

EPOCHS = 50
BATCH_SIZE = 128
N_FOLDS = 10

ACC_array = np.array([])
sensitivity_array = np.array([])
specificity_array = np.array([])

kf = KFold(n_splits=N_FOLDS, shuffle=True)
kf = kf.split(X, y)

for train_index, valid_index in kf:
    X_train, X_valid = X[train_index], X[valid_index]
    y_train, y_valid = y[train_index], y[valid_index]

    model = create_model()
    # fit network
    model.fit(X_train, y_train, validation_data=(X_valid, y_valid),
          epochs=EPOCHS, batch_size=BATCH_SIZE,
#              callbacks=[checkpoint]
              )

   #model.load_weights("model.best.hdf5")

    X_valid = X_valid.astype(np.float32)

    predictions = model.predict(X_valid, batch_size=BATCH_SIZE)
    y_pred = np.round(predictions[:, 0])

confusion = skl.metrics.confusion_matrix(y_valid, y_pred)
TP = confusion[1, 1]
TN = confusion[0, 0]
FP = confusion[0, 1]
FN = confusion[1, 0]

# Accuracy
accuracy = (TP + TN) / float(TP + TN + FP + FN)
# Sensitivity
sensitivity = TP / float(FN + TP)
# Specificity
specificity = TN / float(TN + FP)

ACC_array = np.append(ACC_array, accuracy)
sensitivity_array = np.append(sensitivity_array, sensitivity)
specificity_array = np.append(specificity_array, specificity)

ACC_mean = np.mean(ACC_array, axis=0)
print('mean Accuracy', ACC_mean * 100)
sensitivity_mean = np.mean(sensitivity_array, axis=0)
print('mean sensitivity', sensitivity_mean * 100)
specificity_mean = np.mean(specificity_array, axis=0)
print('mean specificity', specificity_mean * 100)
</code>```

答:
This answer: Get class labels from Keras functional model may be what you are looking for. </p>

<p>I'm assuming you want to convert the raw probabilities output by your model into a discrete class (1 or 0). </p>

Body:
I built the DNN model for predicting the survivability of breast cancer patients, I Want to predict class label (Died or Survive)for new cases, how to implement it? In summary: What I would like to apply is to predict a new case, without conducting the training process directly, just I want to show the result of the class label either died from breast cancer (1) or survived(0).

dataset=pd.read_csv("C:/Users/User/Desktop/mixed 7 12-2-2020.csv",encoding='cp1252')

array = dataset.values
X = array[:, 0:33]
y = array[:, 33]

n_features, n_outputs = X.shape[1], 1

def create_model():

    model = Sequential()
    model.add(Input(n_features))
    model.add(BatchNormalization())
    model.add(Dense(51, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(68, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(85, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(85, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(68, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(51, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(n_outputs, activation='sigmoid'))
    model.compile(loss='binary_crossentropy',
              optimizer='Adam',
              metrics=['accuracy'])
    #model.summary()
    return model
checkpoint = tf.keras.callbacks.ModelCheckpoint(
                                "model.{epoch:02d}-{val_loss:.2f}.hdf5",
                                monitor='val_accuracy',
                                verbose=1,
                                save_best_only=True)

EPOCHS = 50
BATCH_SIZE = 128
N_FOLDS = 10

ACC_array = np.array([])
sensitivity_array = np.array([])
specificity_array = np.array([])

kf = KFold(n_splits=N_FOLDS, shuffle=True)
kf = kf.split(X, y)

for train_index, valid_index in kf:
    X_train, X_valid = X[train_index], X[valid_index]
    y_train, y_valid = y[train_index], y[valid_index]

    model = create_model()
    # fit network
    model.fit(X_train, y_train, validation_data=(X_valid, y_valid),
          epochs=EPOCHS, batch_size=BATCH_SIZE,
#              callbacks=[checkpoint]
              )

   #model.load_weights("model.best.hdf5")

    X_valid = X_valid.astype(np.float32)

    predictions = model.predict(X_valid, batch_size=BATCH_SIZE)
    y_pred = np.round(predictions[:, 0])

confusion = skl.metrics.confusion_matrix(y_valid, y_pred)
TP = confusion[1, 1]
TN = confusion[0, 0]
FP = confusion[0, 1]
FN = confusion[1, 0]

# Accuracy
accuracy = (TP + TN) / float(TP + TN + FP + FN)
# Sensitivity
sensitivity = TP / float(FN + TP)
# Specificity
specificity = TN / float(TN + FP)

ACC_array = np.append(ACC_array, accuracy)
sensitivity_array = np.append(sensitivity_array, sensitivity)
specificity_array = np.append(specificity_array, specificity)

ACC_mean = np.mean(ACC_array, axis=0)
print('mean Accuracy', ACC_mean * 100)
sensitivity_mean = np.mean(sensitivity_array, axis=0)
print('mean sensitivity', sensitivity_mean * 100)
specificity_mean = np.mean(specificity_array, axis=0)
print('mean specificity', specificity_mean * 100)

Body:
This answer: Get class labels from Keras functional model may be what you are looking for.

I'm assuming you want to convert the raw probabilities output by your model into a discrete class (1 or 0).


问:
How to replace characters by strtok function - C?
I really want to change all spaces ' ' in my char array for NULL -

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

void ReplaceCharactersInString(char *pcString, char *cOldChar, char *cNewChar) {
    char *p = strtok(pcString, cOldChar);
    strcpy(pcString, p);
    while (p != NULL) {
        strcat(pcString, p);
        p = strtok(cNewChar, cOldChar);
    }
}

int main() {
    char pcString[] = "I am testing";
    ReplaceCharactersInString(pcString, " ", NULL);
    printf(pcString);
}
</code>```

OUTPUT: Iamtesting

If I simply put the printf(p) function before:

```<code>p = strtok(cNewChar, cOldChar);
</code>```

In the result I  have what I need - but the problem is how to store it in pcString (directly)?

Or there is maybe a better solution to simply do it?

答: The strtok function pretty much does exactly what you want. It basically replaces the next delimiter with a '\0' character and returns the pointer to the current token. The next time you call strtok, you should pass a NULL argument (see the documentation for strtok) and it will point to the next token, which will again be delimited by '\0'. Read some more examples of correct strtok usage.

Body:
I really want to change all spaces ' ' in my char array for NULL -

#include <string.h>

void ReplaceCharactersInString(char *pcString, char *cOldChar, char *cNewChar) {
    char *p = strtok(pcString, cOldChar);
    strcpy(pcString, p);
    while (p != NULL) {
        strcat(pcString, p);
        p = strtok(cNewChar, cOldChar);
    }
}

int main() {
    char pcString[] = "I am testing";
    ReplaceCharactersInString(pcString, " ", NULL);
    printf(pcString);
}

OUTPUT: Iamtesting

If I simply put the printf(p) function before:

p = strtok(cNewChar, cOldChar);

In the result I  have what I need - but the problem is how to store it in pcString (directly)?

Or there is maybe a better solution to simply do it?


问:
how to remove Nan Value in python?
```<code>missing_values=['NAN','NaN','Nan',"na",np.nan]

df=pd.read_csv('train.csv',na_values=missing_values )

df.fillna(df['column_name'].mean())

df.isnull().sum(axis = 0)
</code>```

also tried to dropna nothing seems to work df still has missing values

答:
Try - </p>

```<code> index = np.isnan(df)
 df = np.delete(df,index)
</code>```

Title: how to remove Nan Value in python?

Body:
missing_values=['NAN','NaN','Nan',"na",np.nan]

df=pd.read_csv('train.csv',na_values=missing_values )

df.fillna(df['column_name'].mean())

df.isnull().sum(axis = 0)

also tried to dropna nothing seems to work df still has missing values

Body:
Try -

 index = np.isnan(df)
 df = np.delete(df,index)


问:
How to print more than one id values in Another grid?
hai.In .net iam doing one application.In that one grid contain the id values.When we click on one values that id information is placed in another grid.And when we select another id then print the both id numbers information on second grid.Both grids are in same page side by side.Please give me any suggestions.</p>

答: In first Gridview you can use SelectedIndexChanged event to read content of selected row and at the same time you can insert that row into second grid.


问:
How do you map an array of objects within an object?
I am trying to map an array of objects within an object and I can't seem to get it working. My code's goal is to map and then display the content within the "menu" array.

Here is the structure of the object (logged to console from a state).

```<code>Object {
  "id": 1,
  "img": "https://cuisinederue.org/wp-content/uploads/2019/08/Greg2.png",
  "menu": Array [
    Object {
      "description": "Les prix sont défini à la tête du client",
      "name": "Greg's Burger",
      "price": 1,
    },
    Object {
      "description": "Les prix sont défini à la tête du client",
      "name": "Poutine double cheese bacon",
      "price": 1,
    },
    Object {
      "description": "Les prix sont défini à la tête du client",
      "name": "Cône de poulet pop corn et frites maison",
      "price": 1,
    },
    Object {
      "description": "Les prix sont défini à la tête du client",
      "name": "Grilled cheese Philly steak",
      "price": 1,
    },
    Object {
      "description": "Les prix sont défini à la tête du client",
      "name": "Poutine Philly cheese steak",
      "price": 1,
    },
    Object {
      "description": "Les prix sont défini à la tête du client",
      "name": "Boulette de mac'n'cheese",
      "price": 1,
    },
  ],
  "name": "Greg's dinner",
  "type": "SOUTHERN COMFORT",
}
</code>```

And here is the code that I'm trying to get to work: (specifically the details.menu.map part)

```<code>
const Details = ({navigation, idTruck}) =>{
  const [details, setDetails] = useState([]);
  const [detailsDone, setDetailsDone] = useState(false);

  const getDetails = () => {
    fetch("https://foodtrack-420kbc-lg.herokuapp.com/trucks/1").then(res => res.json()).then(resp => setDetails(resp));
  }

  if(!detailsDone)
  {
    getDetails();
    setDetailsDone(true);
  }
  console.log(details);
  return (
    <View style={styles.container}>
      <Header title='Details'></Header>
      <Image name="logo" style={styles.detailsImage} source={{uri: details.img}}></Image>
      <Text name="nom">{details.name}</Text>
      <Text name="style">{details.type}</Text>

        {

          details.menu.map(({description, name, price}) =>
            <View style={styles.container}>
              <Text name="nom">{name}</Text>
              <Text name="prix">{price}</Text>
              <Text name="desc">{description}</Text>
            </View>
          )
        }

    </View>
  )
}

</code>```

The error I am getting is: TypeError: undefined is not an object (evaluating 'details.menu.map'

Any help is greatly appreciated!

答:
it looks like you're trying to map over details.menu, but when the component loads initially, details is an empty array, which does not have property menu.

One workaround would be to conditionally render something else (like a loading indicator) until the data is set into component state.

Body:
I am trying to map an array of objects within an object and I can't seem to get it working. My code's goal is to map and then display the content within the "menu" array.

Here is the structure of the object (logged to console from a state).

Object {
  "id": 1,
  "img": "https://cuisinederue.org/wp-content/uploads/2019/08/Greg2.png",
  "menu": Array [
    Object {
      "description": "Les prix sont défini à la tête du client",
      "name": "Greg's Burger",
      "price": 1,
    },
    Object {
      "description": "Les prix sont défini à la tête du client",
      "name": "Poutine double cheese bacon",
      "price": 1,
    },
    Object {
      "description": "Les prix sont défini à la tête du client",
      "name": "Cône de poulet pop corn et frites maison",
      "price": 1,
    },
    Object {
      "description": "Les prix sont défini à la tête du client",
      "name": "Grilled cheese Philly steak",
      "price": 1,
    },
    Object {
      "description": "Les prix sont défini à la tête du client",
      "name": "Poutine Philly cheese steak",
      "price": 1,
    },
    Object {
      "description": "Les prix sont défini à la tête du client",
      "name": "Boulette de mac'n'cheese",
      "price": 1,
    },
  ],
  "name": "Greg's dinner",
  "type": "SOUTHERN COMFORT",
}

And here is the code that I'm trying to get to work: (specifically the details.menu.map part)

const Details = ({navigation, idTruck}) =>{
  const [details, setDetails] = useState([]);
  const [detailsDone, setDetailsDone] = useState(false);

  const getDetails = () => {
    fetch("https://foodtrack-420kbc-lg.herokuapp.com/trucks/1").then(res => res.json()).then(resp => setDetails(resp));
  }

  if(!detailsDone)
  {
    getDetails();
    setDetailsDone(true);
  }
  console.log(details);
  return (
    <View style={styles.container}>

      <Image name="logo" style={styles.detailsImage} source={{uri: details.img}}></Image>
      <Text name="nom">{details.name}</Text>
      <Text name="style">{details.type}</Text>

        {

          details.menu.map(({description, name, price}) =>
            <View style={styles.container}>
              <Text name="nom">{name}</Text>
              <Text name="prix">{price}</Text>
              <Text name="desc">{description}</Text>
            </View>
          )
        }

    </View>
  )
}

The error I am getting is: TypeError: undefined is not an object (evaluating 'details.menu.map'

Any help is greatly appreciated!


问:
Spring Boot @GetMapping rule for multiple mapping
I have 3 different method in controller for get requests.

-the 1st one to get a user by id with a path variable:

```<code>@GetMapping(path="/{id}")
public ResponseEntity<UserInfoDTO> getUserById(@PathVariable Long id)
</code>```

The 2nd gets a user based on the username parameter:

```<code>public ResponseEntity<UserInfoDTO> getUserByUsername(@RequestParam String username)
</code>```

And finally another one to get all users

```<code>public ResponseEntity<List<UserInfoDTO>> getAllUsers()
</code>```

What should be the @GetMapping for the 2nd and 3rd method?

For exemple @GetMapping for all users and @GetMapping(path="/") for a user by username?

Or whatever...

Thanks.

答:
For example, optional username param:

```<code>    @GetMapping(path = "/")
    public ResponseEntity<?> getUserByUsername(@RequestParam(required = false) final String username) {
        if (username != null) {
            // http://localhost:8080/?username=myname
            return new ResponseEntity<>(new UserInfoDTO("by username: " + username), HttpStatus.OK);
        } else {
            // http://localhost:8080/
            return getAllUsers();
        }
    }

    private ResponseEntity<List<UserInfoDTO>> getAllUsers() {
        return new ResponseEntity<>(List.of(new UserInfoDTO("user1-of-all"), new UserInfoDTO("user2-of-all")),
            HttpStatus.OK);
    }

    public static class UserInfoDTO {
        public UserInfoDTO(final String name) {
            this.name = name;
        }

        private final String name;

        public String getName() {
            return name;
        }
    }
</code>```

Body:
I have 3 different method in controller for get requests.

-the 1st one to get a user by id with a path variable:

@GetMapping(path="/{id}")
public ResponseEntity<UserInfoDTO> getUserById(@PathVariable Long id)

The 2nd gets a user based on the username parameter:

public ResponseEntity<UserInfoDTO> getUserByUsername(@RequestParam String username)

And finally another one to get all users

public ResponseEntity<List<UserInfoDTO>> getAllUsers()

What should be the @GetMapping for the 2nd and 3rd method?

For exemple @GetMapping for all users and @GetMapping(path="/") for a user by username?

Or whatever...

Thanks.

Body:
For example, optional username param:

    @GetMapping(path = "/")
    public ResponseEntity<?> getUserByUsername(@RequestParam(required = false) final String username) {
        if (username != null) {
            // http://localhost:8080/?username=myname
            return new ResponseEntity<>(new UserInfoDTO("by username: " + username), HttpStatus.OK);
        } else {
            // http://localhost:8080/
            return getAllUsers();
        }
    }

    private ResponseEntity<List<UserInfoDTO>> getAllUsers() {
        return new ResponseEntity<>(List.of(new UserInfoDTO("user1-of-all"), new UserInfoDTO("user2-of-all")),
            HttpStatus.OK);
    }

    public static class UserInfoDTO {
        public UserInfoDTO(final String name) {
            this.name = name;
        }

        private final String name;

        public String getName() {
            return name;
        }
    }


问:
Hashicorp Consul - How to do verified TLS from Pods in Kubernetes cluster
I'm having some difficulty understanding Consul end-to-end TLS. For reference, I'm using Consul in Kubernetes (via the hashicorp/consul Helm chart). Only one datacenter and Kubernetes cluster - no external parties or concerns.

I have configured my override values.yaml file like so:

global:
  datacenter: sandbox

  gossipEncryption:
    secretName: "consul"
    secretKey: "CONSUL_GOSSIP_ENCRYPTION_KEY"

  tls:
    enabled: true
    httpsOnly: true
    enableAutoEncrypt: true
    serverAdditionalDNSSANs: ["'consul.service.consul'"]

server:
  replicas: 3
  bootstrapExpect: 3
  storage: 20Gi

dns:
  clusterIP: 172.20.53.53

ui:
  service:
    type: 'LoadBalancer'

syncCatalog:
  enabled: true
```

<p>All other values are as default from the shipped <strong>values.yaml</strong> file.</p>

<p>This works, and Consul client logs suggest that all agents area connecting nicely using TLS, with relevant certs and keys being created by (as I understand) the <strong>Auto-encryption</strong> feature of Consul.</p>

<p>What I don't understand is how to initiate a HTTPS connection from an application on Kubernetes, running in a Pod, to a Consul server. Since the Pod's container does not (presumably) have the Consul root CA cert in its trust store, all HTTPS calls fail, as per <strong>wget</strong> example below:</p>

```# Connect to Pod:
laptop$> kubectl exec -it my-pod sh

# Attempt valid HTTPS connection:
my-pod$> wget -q -O - https://consul.service.consul:8501
Connecting to consul.service.consul:8501 (10.110.1.131:8501)
ssl_client: consul.service.consul: certificate verification failed: unable to get local issuer certificate
wget: error getting response: Connection reset by peer

# Retry, but ignore certificate validity issues:
my-pod$> wget --no-check-certificate -q -O - https://consul.service.consul:8501/v1/status/leader
"10.110.1.131:8300"
```

How am I supposed to enforce end-to-end (verified) HTTPS connections from my apps on Kubernetes to Consul if the container does not recognize the certificate as valid?
Am I misunderstanding something about certificate propagation?

Many thanks - Aaron

答:
Solved with thanks to Hashicorp on their Consul discussion forum.

Create a Kubernetes secret named consul with a key named CONSUL_GOSSIP_ENCRYPTION_KEY and an appropriate encryption key value.

Generate value using consul keygen

Install the hashicorp/consul Helm chart with an values-override.yaml , such as below:

```<code>global:
  datacenter: sandbox

  gossipEncryption:
    secretName: "consul"
    secretKey: "CONSUL_GOSSIP_ENCRYPTION_KEY"

  tls:
    enabled: true
    httpsOnly: true
    enableAutoEncrypt: true
    serverAdditionalDNSSANs: ["'consul.service.consul'"]

server:
  replicas: 3
  bootstrapExpect: 3
  storage: 20Gi

dns:
  clusterIP: 172.20.53.53

ui:
  service:
    type: 'LoadBalancer'

syncCatalog:
  enabled: true

</code>```

Create an example Pod spec to represent our application.

Ensure it mounts the Consul server CA cert secret.

Ensure the Pod’s container has HOST_IP exposed as an environment variable.

```<code>apiVersion: v1
kind: Pod
metadata:
  namespace: default
  name: test-pod
spec:
  volumes:
  - name: consul-consul-ca-cert
    secret:
      secretName: consul-consul-ca-cert
  hostNetwork: false
  containers:
  - name: consul-test-pod
    image: alpine
    imagePullPolicy: IfNotPresent
    env:
    - name: HOST_IP
      valueFrom:
        fieldRef:
          fieldPath: status.hostIP
    command: ["/bin/sh"]
    args: ["-c", "while true; do sleep 24h; done"]
    volumeMounts:
    - name: consul-consul-ca-cert
      mountPath: /consul/tls/ca
</code>```

Upon creation of the Pod, kubectl exec into it, and ensure the ca-certificates and curl packages are installed (I’m using Alpine Linux in this example).

(curl is purely for testing purposes)

```<code>#> apk update
#> apk add ca-certificates curl
</code>```

Copy the mounted Consul server CA certificate into the /usr/local/share/ca-certificates/ and execute update-ca-certificates to add it to the system root CA store.

```<code>#> cp /consul/tls/ca/tls.crt /usr/local/share/ca-certificates/consul-server-ca.crt
#> update-ca-certificates  # might give a trivial warning - ignore it
</code>```

The Consul server is now accessible (and trusted) over HTTPS as below:

```<code>#> curl https://consul.service.consul:8501/v1/status/leader
## No TLS errors ##
</code>```

We also want to talk to the Consul client (instead of the server) over HTTPS, for performance reasons.

Since the Consul client has its own CA cert, we need to retrieve that from the server.

This requires the consul-k8s binary, so we need to get that.

```<code>#> cd /usr/local/bin
#> wget https://releases.hashicorp.com/consul-k8s/0.15.0/consul-k8s_0.15.0_linux_amd64.zip  # (or whatever latest version is)
#> unzip consul-k8s_0.15.0_linux_amd64.zip
#> rm consul-k8s_0.15.0_linux_amd64.zip
</code>```

Get the Consul client CA cert and install it via update-ca-certificates :

```<code>#> consul-k8s get-consul-client-ca -server-addr consul.service.consul -server-port 8501 -output-file /usr/local/share/ca-certificates/consul-client-ca.crt
#> update-ca-certificates  # might give a trivial warning - ignore it
</code>```

The Consul client is now accessible (and trusted) over HTTPS as below:

```<code>#> curl https://$HOST_IP:8501/v1/status/leader
## No TLS errors ##
</code>```

We can also access the Consul KV service from the client without issue:

```<code>#> curl https://$HOST_IP:8501/v1/kv/foo/bar/baz
## No TLS errors ##
</code>```

Naturally, all of the above should be automated by the implementer. These manual steps are purely for demonstration purposes.

Body:
I'm having some difficulty understanding Consul end-to-end TLS. For reference, I'm using Consul in Kubernetes (via the hashicorp/consul Helm chart). Only one datacenter and Kubernetes cluster - no external parties or concerns.

I have configured my override values.yaml file like so:

global:
  datacenter: sandbox

  gossipEncryption:
    secretName: "consul"
    secretKey: "CONSUL_GOSSIP_ENCRYPTION_KEY"

  tls:
    enabled: true
    httpsOnly: true
    enableAutoEncrypt: true
    serverAdditionalDNSSANs: ["'consul.service.consul'"]

server:
  replicas: 3
  bootstrapExpect: 3
  storage: 20Gi

dns:
  clusterIP: 172.20.53.53

ui:
  service:
    type: 'LoadBalancer'

syncCatalog:
  enabled: true

All other values are as default from the shipped values.yaml file.

This works, and Consul client logs suggest that all agents area connecting nicely using TLS, with relevant certs and keys being created by (as I understand) the Auto-encryption feature of Consul.

What I don't understand is how to initiate a HTTPS connection from an application on Kubernetes, running in a Pod, to a Consul server. Since the Pod's container does not (presumably) have the Consul root CA cert in its trust store, all HTTPS calls fail, as per wget example below:

# Connect to Pod:
laptop$> kubectl exec -it my-pod sh

# Attempt valid HTTPS connection:
my-pod$> wget -q -O - https://consul.service.consul:8501
Connecting to consul.service.consul:8501 (10.110.1.131:8501)
ssl_client: consul.service.consul: certificate verification failed: unable to get local issuer certificate
wget: error getting response: Connection reset by peer

# Retry, but ignore certificate validity issues:
my-pod$> wget --no-check-certificate -q -O - https://consul.service.consul:8501/v1/status/leader
"10.110.1.131:8300"

How am I supposed to enforce end-to-end (verified) HTTPS connections from my apps on Kubernetes to Consul if the container does not recognize the certificate as valid?
Am I misunderstanding something about certificate propagation?

Many thanks - Aaron

Body:
Solved with thanks to Hashicorp on their Consul discussion forum.

Create a Kubernetes secret named consul with a key named CONSUL_GOSSIP_ENCRYPTION_KEY and an appropriate encryption key value.

Generate value using consul keygen

Install the hashicorp/consul Helm chart with an values-override.yaml , such as below:

global:
  datacenter: sandbox

  gossipEncryption:
    secretName: "consul"
    secretKey: "CONSUL_GOSSIP_ENCRYPTION_KEY"

  tls:
    enabled: true
    httpsOnly: true
    enableAutoEncrypt: true
    serverAdditionalDNSSANs: ["'consul.service.consul'"]

server:
  replicas: 3
  bootstrapExpect: 3
  storage: 20Gi

dns:
  clusterIP: 172.20.53.53

ui:
  service:
    type: 'LoadBalancer'

syncCatalog:
  enabled: true

Create an example Pod spec to represent our application.

Ensure it mounts the Consul server CA cert secret.

Ensure the Pod’s container has HOST_IP exposed as an environment variable.

apiVersion: v1
kind: Pod
metadata:
  namespace: default
  name: test-pod
spec:
  volumes:
  - name: consul-consul-ca-cert
    secret:
      secretName: consul-consul-ca-cert
  hostNetwork: false
  containers:
  - name: consul-test-pod
    image: alpine
    imagePullPolicy: IfNotPresent
    env:
    - name: HOST_IP
      valueFrom:
        fieldRef:
          fieldPath: status.hostIP
    command: ["/bin/sh"]
    args: ["-c", "while true; do sleep 24h; done"]
    volumeMounts:
    - name: consul-consul-ca-cert
      mountPath: /consul/tls/ca

Upon creation of the Pod, kubectl exec into it, and ensure the ca-certificates and curl packages are installed (I’m using Alpine Linux in this example).

(curl is purely for testing purposes)

#> apk update
#> apk add ca-certificates curl

Copy the mounted Consul server CA certificate into the /usr/local/share/ca-certificates/ and execute update-ca-certificates to add it to the system root CA store.

#> cp /consul/tls/ca/tls.crt /usr/local/share/ca-certificates/consul-server-ca.crt
#> update-ca-certificates  # might give a trivial warning - ignore it

The Consul server is now accessible (and trusted) over HTTPS as below:

#> curl https://consul.service.consul:8501/v1/status/leader
## No TLS errors ##

We also want to talk to the Consul client (instead of the server) over HTTPS, for performance reasons.

Since the Consul client has its own CA cert, we need to retrieve that from the server.

This requires the consul-k8s binary, so we need to get that.

#> cd /usr/local/bin
#> wget https://releases.hashicorp.com/consul-k8s/0.15.0/consul-k8s_0.15.0_linux_amd64.zip  # (or whatever latest version is)
#> unzip consul-k8s_0.15.0_linux_amd64.zip
#> rm consul-k8s_0.15.0_linux_amd64.zip

Get the Consul client CA cert and install it via update-ca-certificates :

#> consul-k8s get-consul-client-ca -server-addr consul.service.consul -server-port 8501 -output-file /usr/local/share/ca-certificates/consul-client-ca.crt
#> update-ca-certificates  # might give a trivial warning - ignore it

The Consul client is now accessible (and trusted) over HTTPS as below:

#> curl https://$HOST_IP:8501/v1/status/leader
## No TLS errors ##

We can also access the Consul KV service from the client without issue:

#> curl https://$HOST_IP:8501/v1/kv/foo/bar/baz
## No TLS errors ##

Naturally, all of the above should be automated by the implementer. These manual steps are purely for demonstration purposes.


答:
There are TONS of ways to doing this but I am going to suggest some options that involving read the full line as its much easier to work with / understand and that your lines are of varying length. I did add a suggestion on using StreamReader on a file at the end in addendum but you may need to figure out serious work arounds on skipping lines you don't want, restarting a char iterating loop on new lines etc.

I first demonstrate the latest and greatest IAsyncEnumerable found in NetCore 3.x followed by a similar string-based approach. By sharing an Int example that is a slightly advanced and that will also be asynchronous, I hope to also help others and demonstrate a fairly modern approach in 2020. Streaming out only the data you need will be a huge benefit in keeping it fast and a low memory footprint.

public static async IAsyncEnumerable<int> StreamFileOutAsIntsAsync(string filePathName)
{
    if (string.IsNullOrWhiteSpace(filePathName)) throw new ArgumentNullException(nameof(filePathName));
    if (!File.Exists(filePathName)) throw new ArgumentException($"{filePathName} is not a valid file path.");

    using var streamReader = File.OpenText(filePathName);
    string currentLine;

    while ((currentLine = await streamReader.ReadLineAsync().ConfigureAwait(false)) != null)
    {
        if (int.TryParse(currentLine.AsSpan(), out var output))
        {
            yield return output;
        }
    }
}
```

<p>This streams every int out of a file, checking that file exists and that the filename path is not null or blank etc.</p>

<p>Streaming maybe too much for a beginner so I don't know your level.</p>

<p>You may want to start with just turning the file into a list of strings.</p>

<p>Modifying my previous example above to something less complex but split your strings for you. I recommend learning about streaming so you don't have every piece of string in memory while you work on it... or maybe you want them all. I am not here to judge.</p>

<p>Once you get your string line out from a file you can do whatever else needs to be done.</p>

<pre class="lang-cs prettyprint-override"><code>public static async Task<List<string>> GetStringsFromFileAsync(string filePathName)
{
    if (string.IsNullOrWhiteSpace(filePathName)) throw new ArgumentNullException(nameof(filePathName));
    if (!File.Exists(filePathName)) throw new ArgumentException($"{filePathName} is not a valid file path.");

    using var streamReader = File.OpenText(filePathName);

    string currentLine;
    var strings = new List<string>();

    while ((currentLine = await streamReader.ReadLineAsync().ConfigureAwait(false)) != null)
    {
        var lineAsArray = currentLine.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);

        // Simple Data Validation
        if (lineAsArray.Length == 4)
        {
            strings.Add($"{lineAsArray[0]}:{lineAsArray[1]}");
            strings.Add($"{lineAsArray[2]}:{lineAsArray[3]}");
        }
    }

    return strings;
}
</code>```

The meat of the code is really simple, open the file for reading!

using var streamReader = File.OpenText(filePathName);
```

<p>and then loop through that file...</p>

```while ((currentLine = await streamReader.ReadLineAsync()) != null)
{
    var lineAsArray = currentLine.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);

    // Simple Data Validation
    if (lineAsArray.Length == 4)
    {
        // Do whatever you need to do with the first bits of information.
        // In this case, we add them all to a list for return.
        strings.Add($"{lineAsArray[0]}:{lineAsArray[1]}");
        strings.Add($"{lineAsArray[2]}:{lineAsArray[3]}");
    }
}
```

<p>What this demonstrates is that, for every line that I read out that is not null, break into four parts (based on the ":") character removing all empty entries.</p>

<p>We then use a C# feature called String Interpolation (<code>$""</code>) to put the first two back together with ":" as a string. Then the second two. Or whatever you need to do with reading each part of the line.</p>

<p>That's really all there is to it! Hope it helps.</p>

<p>Addendum: If you really need to read parts of file, please use a StreamReader.Read and Peek()</p>

<pre class="lang-cs prettyprint-override"><code>using (var sr = new StreamReader(path))
{
    while (sr.Peek() >= 0)
    {
        Console.Write((char)sr.Read());
    }
}
</code>```

Reading each character

Title: How to read a portion of a line in a text file?

Body:
So, I have a text file with thousands of lines formatted similarly to this:

123456:0.8525000:1590882780:91011

These files are almost always a different length, and I only need to read the first two parts of the line, being 123456:0.8525000.

I know that I can split each line using C#, but I'm unsure how to only read the first 2 parts. Anyone have any idea on how to do this? Sorry if my question doesn't make sense, I can restate it if needed.

Body:
There are TONS of ways to doing this but I am going to suggest some options that involving read the full line as its much easier to work with / understand and that your lines are of varying length. I did add a suggestion on using StreamReader on a file at the end in addendum but you may need to figure out serious work arounds on skipping lines you don't want, restarting a char iterating loop on new lines etc.

I first demonstrate the latest and greatest IAsyncEnumerable found in NetCore 3.x followed by a similar string-based approach. By sharing an Int example that is a slightly advanced and that will also be asynchronous, I hope to also help others and demonstrate a fairly modern approach in 2020. Streaming out only the data you need will be a huge benefit in keeping it fast and a low memory footprint.

public static async IAsyncEnumerable<int> StreamFileOutAsIntsAsync(string filePathName)
{
    if (string.IsNullOrWhiteSpace(filePathName)) throw new ArgumentNullException(nameof(filePathName));
    if (!File.Exists(filePathName)) throw new ArgumentException($"{filePathName} is not a valid file path.");

    using var streamReader = File.OpenText(filePathName);
    string currentLine;

    while ((currentLine = await streamReader.ReadLineAsync().ConfigureAwait(false)) != null)
    {
        if (int.TryParse(currentLine.AsSpan(), out var output))
        {
            yield return output;
        }
    }
}

This streams every int out of a file, checking that file exists and that the filename path is not null or blank etc.

Streaming maybe too much for a beginner so I don't know your level.

You may want to start with just turning the file into a list of strings.

Modifying my previous example above to something less complex but split your strings for you. I recommend learning about streaming so you don't have every piece of string in memory while you work on it... or maybe you want them all. I am not here to judge.

Once you get your string line out from a file you can do whatever else needs to be done.

public static async Task<List<string>> GetStringsFromFileAsync(string filePathName)
{
    if (string.IsNullOrWhiteSpace(filePathName)) throw new ArgumentNullException(nameof(filePathName));
    if (!File.Exists(filePathName)) throw new ArgumentException($"{filePathName} is not a valid file path.");

    using var streamReader = File.OpenText(filePathName);

    string currentLine;
    var strings = new List<string>();

    while ((currentLine = await streamReader.ReadLineAsync().ConfigureAwait(false)) != null)
    {
        var lineAsArray = currentLine.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);

        // Simple Data Validation
        if (lineAsArray.Length == 4)
        {
            strings.Add($"{lineAsArray[0]}:{lineAsArray[1]}");
            strings.Add($"{lineAsArray[2]}:{lineAsArray[3]}");
        }
    }

    return strings;
}

The meat of the code is really simple, open the file for reading!

using var streamReader = File.OpenText(filePathName);

and then loop through that file...

while ((currentLine = await streamReader.ReadLineAsync()) != null)
{
    var lineAsArray = currentLine.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);

    // Simple Data Validation
    if (lineAsArray.Length == 4)
    {
        // Do whatever you need to do with the first bits of information.
        // In this case, we add them all to a list for return.
        strings.Add($"{lineAsArray[0]}:{lineAsArray[1]}");
        strings.Add($"{lineAsArray[2]}:{lineAsArray[3]}");
    }
}

What this demonstrates is that, for every line that I read out that is not null, break into four parts (based on the ":") character removing all empty entries.

We then use a C# feature called String Interpolation ($"") to put the first two back together with ":" as a string. Then the second two. Or whatever you need to do with reading each part of the line.

That's really all there is to it! Hope it helps.

Addendum: If you really need to read parts of file, please use a StreamReader.Read and Peek()

using (var sr = new StreamReader(path))
{
    while (sr.Peek() >= 0)
    {
        Console.Write((char)sr.Read());
    }
}

Reading each character