问:
Reduce code duplication without weakening types when processing a discriminated union
I'm trying to figure out how to eliminate some duplicate code when handling a discriminated union in a way that does not weaken the type checking:

const handlers = {
  foo: (input: string): void => console.log(`foo ${input}`),
  bar: (input: number): void => console.log(`bar ${input}`),
};

type Handlers = typeof handlers;
type Values<T> = T[keyof T];
type DiscriminatedInput = Values<{
  [Id in keyof Handlers]: {type: Id; value: Parameters<Handlers[Id]>[0]};
}>;

const inputs: DiscriminatedInput[] = [
  JSON.parse('{"type": "foo", "value": "hello world"}'),
  JSON.parse('{"type": "bar", "value": 42}'),
];
for (const input of inputs) {
  // This doesn't work:
  //
  //     handlers[input.type](input.value);
  //                          ^^^^^^^^^^^
  //         error: Argument of type 'string | number' is not assignable to
  //         parameter of type 'never'.
  //
  // So I do this instead, which has a lot of duplication and must be kept in
  // sync with `handlers` above:
  switch (input.type) {
    case 'foo': handlers[input.type](input.value); break;
    case 'bar': handlers[input.type](input.value); break;
    default: ((exhaustiveCheck: never) => {})(input);
  }
}
```

Inside the for loop above, handlers[input.type] is guaranteed to be a function whose first parameter always matches the type of input.value, regardless of input.type. It seems to me that TypeScript should be able to see that, but it doesn't.

Am I doing something wrong, or is this is a limitation of TypeScript?

If it's a limitation of TypeScript, is there an existing bug report? Is there something I can do to help TypeScript narrow input to a foo- or bar-specific type so that I can eliminate that switch statement? Or refactor DisciminatedInput?

I could use a type assertion to weaken the type checking, but that adds complexity and reduces readability just to work around a language limitation. I'd rather work with the language instead of against it.

答:
This answer is in response to your latest question revision and your comment:

You can view an assertion as widening (weakening) types, but what's really happening is that you are artificially narrowing (strengthening) the types by asserting what gets parsed from the JSON strings (which is actually any), and then having to fight against what you asserted to the compiler.

If you:

don't want to use a type assertion, and

must parse JSON inputs according to the schema provided in your question

then you can refactor your handlers to include the typeof runtime value of each handler's input parameter. This will enable you to then validate that there is a correlation between it and the typeof the input value parsed from each JSON object: using a type predicate function to satisfy the compiler.

In summary: this replaces the discussed assertion with a predicate, which uses a runtime check to enforce simple validation of the parsed JSON input before invoking its associated handler.

Here's an example of such a refactor:

TS Playground

type Values<T> = T[keyof T];
type Handler<Param> = (input: Param) => void;

type HandlerData<Param> = {
  inputType: string;
  fn: Handler<Param>;
};

/**
 * This identity function preservees the type details of the provided
 * argument object, while enforcing that it extends the constraint (which
 * is used later in a predicate to ensure a level of type-safety of the parsed JSON)
 */
function createHandlers <T extends Record<string, HandlerData<any>>>(handlers: T): T {
  return handlers;
}

const handlers = createHandlers({
  foo: {
    inputType: 'string',
    fn: (input: string): void => console.log(`foo ${input}`),
  },
  bar: {
    inputType: 'number',
    fn: (input: number): void => console.log(`bar ${input}`),
  },
});

type Handlers = typeof handlers;

type DiscriminatedInput = Values<{
  [Key in keyof Handlers]: {
    type: Key;
    value: Parameters<Handlers[Key]['fn']>[0];
  };
}>;

// This type is required for the usage in the following predicate
type HandlerDataWithInput<Param, Value> = HandlerData<Param> & { value: Value };

function dataIsTypeSafe <T = DiscriminatedInput['value']>(data: HandlerDataWithInput<any, any>): data is HandlerDataWithInput<T, T> {
  return typeof data.value === data.inputType;
}

const inputs: DiscriminatedInput[] = [
  JSON.parse('{"type": "foo", "value": "hello world"}'),
  JSON.parse('{"type": "bar", "value": 42}'),
];

for (const input of inputs) {
  const data = {...handlers[input.type], value: input.value};
  if (dataIsTypeSafe(data)) data.fn(data.value);
}

```

Body:
I'm trying to figure out how to eliminate some duplicate code when handling a discriminated union in a way that does not weaken the type checking:

const handlers = {
  foo: (input: string): void => console.log(`foo ${input}`),
  bar: (input: number): void => console.log(`bar ${input}`),
};

type Handlers = typeof handlers;
type Values<T> = T[keyof T];
type DiscriminatedInput = Values<{
  [Id in keyof Handlers]: {type: Id; value: Parameters<Handlers[Id]>[0]};
}>;

const inputs: DiscriminatedInput[] = [
  JSON.parse('{"type": "foo", "value": "hello world"}'),
  JSON.parse('{"type": "bar", "value": 42}'),
];
for (const input of inputs) {
  // This doesn't work:
  //
  //     handlers[input.type](input.value);
  //                          ^^^^^^^^^^^
  //         error: Argument of type 'string | number' is not assignable to
  //         parameter of type 'never'.
  //
  // So I do this instead, which has a lot of duplication and must be kept in
  // sync with `handlers` above:
  switch (input.type) {
    case 'foo': handlers[input.type](input.value); break;
    case 'bar': handlers[input.type](input.value); break;
    default: ((exhaustiveCheck: never) => {})(input);
  }
}

Inside the for loop above, handlers[input.type] is guaranteed to be a function whose first parameter always matches the type of input.value, regardless of input.type. It seems to me that TypeScript should be able to see that, but it doesn't.

Am I doing something wrong, or is this is a limitation of TypeScript?

If it's a limitation of TypeScript, is there an existing bug report? Is there something I can do to help TypeScript narrow input to a foo- or bar-specific type so that I can eliminate that switch statement? Or refactor DisciminatedInput?

I could use a type assertion to weaken the type checking, but that adds complexity and reduces readability just to work around a language limitation. I'd rather work with the language instead of against it.

Body:
This answer is in response to your latest question revision and your comment:

You can view an assertion as widening (weakening) types, but what's really happening is that you are artificially narrowing (strengthening) the types by asserting what gets parsed from the JSON strings (which is actually any), and then having to fight against what you asserted to the compiler.

If you:

don't want to use a type assertion, and

must parse JSON inputs according to the schema provided in your question

then you can refactor your handlers to include the typeof runtime value of each handler's input parameter. This will enable you to then validate that there is a correlation between it and the typeof the input value parsed from each JSON object: using a type predicate function to satisfy the compiler.

In summary: this replaces the discussed assertion with a predicate, which uses a runtime check to enforce simple validation of the parsed JSON input before invoking its associated handler.

Here's an example of such a refactor:

<a href="https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true&target=99&useUnknownInCatchVariables=true&exactOptionalPropertyTypes=true#code/C4TwDgpgBAaghgGwK4QM4B4AqA+KBeKTAbQGsIQB7AM0IF0BuAWAChRIoAJOAOwBMEIAJ3QAFOILgBbXAQAUAS25gkwAFxQxEyQEp8uAG4V5vJsxZtoXPgMEAROMDijxUmVADeLKFEXLgmcAh1VGBBRQBzU28qbnUrfiFnLWxTAF9TFgB6ACpsr2zCAAt5VB9eCG5geVAoKiRuAGMqim4oMEE0IX0INChgQugLKHLHeQRS6j6BtsEKfWMIXnyocXCkSQrgKAoAIwArCCaAGigAd2KBKAqqCkEGiKmHHy2IAA9gCt5S-ugGlpCJIotrJzvIGoVliUoEhUIsoAgHEIfK04DNFmDEX0KFduKgkB0VvCIN0ENsaBYALSoOBUCA1SY-NriWG8KAAKQAygB5ABy2nymRYdUazVaDQ6iPiNlKWCu70+pQASodbrx0ACIicpUJ7I50DwQNgjbJCjwEoJUOpMNorR4vFAOsB8a1TdYhKg0iwWH9cVtXebSgRxRBJWbpbJPMxohQKOpI95vL4VAFIOoAOQa7jhNNHe3RWJQBRKFTBUIRG1QQzGPRQH2oCgCAB0CAo4VkAAMbtiACTuJPAVLt7S5qNQVIj7w7cRxvPIvwpoJQNPcdY7IQ52cxdRFvzqFeSNeCCtV1l4XB1hsQZutjtTwRQXv9wfD+3jlipbQZViBThh934PpAkmf1pS-IZbBKcV5EkRREV4ABJYstgIeBkDQdB4ygIgAGlyGRKAyEoGhtQtWgZ1HbwLHUXCQCiBN9EQFB1E0KQ6XddASNQHDyFoIg0xiNNaGwIgAAYGFfNIUi9ZhMkyIooSGKEOgARyQeQOlZG570ZGE4HCaBFCmaAbgQFtTgedp0QaRFzB-EjdTgAB1apCkQvwkikE5UJQNx7IcJwWOkKAADIPErRjF286B0mk4UmnkFphn8+DUAXDkaWgWUCAg1AoJg7g4LclQ+IYtDBOwWReH8uI-zsfznP6IrgH1bgQBOA1sArKrHB8Uo-McBrXKQrAThwO1R0dZ1AMgSZurgRtSpQfA8AIObG37BdPTMZg6y2ftLSgHK8tgj4EKQohaAAoh7U5XlGzAZkIFkNN3AAIgsV71FertXpOV7FogT6oFegZTOxU5bgQXhXtSNMX1HW6eXux7nrej6vrvX7gYBoGABYACZYfh8Ttq0wtdrnFQyUp4BUF0TCKbmgD3EbVmQPdIh+0bCxaBOAH1C5gGYtHeQaEq5LUsCdLaXFxxtF0NaYll+aAc-d8WCAA" rel="nofollow noreferrer">TS Playground

type Values<T> = T[keyof T];
type Handler<Param> = (input: Param) => void;

type HandlerData<Param> = {
  inputType: string;
  fn: Handler<Param>;
};

/**
 * This identity function preservees the type details of the provided
 * argument object, while enforcing that it extends the constraint (which
 * is used later in a predicate to ensure a level of type-safety of the parsed JSON)
 */
function createHandlers <T extends Record<string, HandlerData<any>>>(handlers: T): T {
  return handlers;
}

const handlers = createHandlers({
  foo: {
    inputType: 'string',
    fn: (input: string): void => console.log(`foo ${input}`),
  },
  bar: {
    inputType: 'number',
    fn: (input: number): void => console.log(`bar ${input}`),
  },
});

type Handlers = typeof handlers;

type DiscriminatedInput = Values<{
  [Key in keyof Handlers]: {
    type: Key;
    value: Parameters<Handlers[Key]['fn']>[0];
  };
}>;

// This type is required for the usage in the following predicate
type HandlerDataWithInput<Param, Value> = HandlerData<Param> & { value: Value };

function dataIsTypeSafe <T = DiscriminatedInput['value']>(data: HandlerDataWithInput<any, any>): data is HandlerDataWithInput<T, T> {
  return typeof data.value === data.inputType;
}

const inputs: DiscriminatedInput[] = [
  JSON.parse('{"type": "foo", "value": "hello world"}'),
  JSON.parse('{"type": "bar", "value": 42}'),
];

for (const input of inputs) {
  const data = {...handlers[input.type], value: input.value};
  if (dataIsTypeSafe(data)) data.fn(data.value);
}


来源: stackexchange

问:
Why the stopwords won't be filtered in my program
I used principally the stopwords list of NLTK like what the codes show

```<code>from nltk.corpus import stopwords`
stopword_nltk=stopwords.words('french')
motoutil=['après', 'avant', 'avex', 'chez', '\ba\b', 'et', 'concernant', 'contre', 'dans', 'depuis', 'derrière', 'dès', 'devant', 'durant', 'en', 'entre', 'envers', 'hormis', 'hors', 'jusque', 'malgré', 'moyennant', 'nonobstant', 'outre', 'par', 'parmi pendant', 'pour', 'près', 'sans', 'sauf', 'selon', 'sous', 'suivant', 'sur', 'touchant', 'vers', 'via', 'tout','tous', 'toute', 'toutes', 'jusqu']
stopwords_list=stopword_nltk+motoutil
</code>```

It's not because I added another list to stopword_nltk that the program doesn't feed my need. Even though I remove motoutil, it doesn't work, either.

And this is the part where I plan to delete the stopwords:

```<code>for line in f_in.readlines():
    new_line=re.sub('\W',' ', line.lower())
    list_word=new_line.split()
    for element in list_word:
        if element in stopwords_list:
            cleaned_line=re.sub(element, ' ', new_line)
            f_out_trameur.write(cleaned_line)
            f_out_cleaned.write(cleaned_line)
</code>```

It has two problems:

Firstly, the stopwords listed won't be all removed, such as 'et'.

Secondly, I want to also delete the word 'de' and 'ce' but not the two portions in the middle of a word. for example, in the extract "madame monsieur le président de  l'assemblée nationale", the de  preceding the word président should be cleaned, but not the "de" in the word président, with my actual script, président will be "prési nt"

答:
Do I see it right that you are creating and writing the cleaned line within the inner loop that iterates over the tokens in a line produced by new_line.split()? And if there was nothing found to clean it is not written at all?

This would result in multiple versions of lines with stopwords (one version with each of the stopwords removed), while lines which do no contain stopwords are just skipped.

What I would suggest is since you already have the tokens (you used split()) you just use these to write the new line instead of replacing the tokens in the new line.

This also allows you to convert the list of stopwords to a set and making the check if element in stopwords_list much faster, since this is usually a large list and can get slow for large amounts of words. This is almost always a good way to speed things up when using NLTKs stopwords.

I would also recommend to use a list comprehension to avoid too many nested loops and conditions and make it more readable, but this is just a personal preference.

```<code>from nltk.corpus import stopwords
stopword_nltk=stopwords.words('french')
motoutil=['après', 'avant', 'avex', 'chez', '\ba\b', 'et', 'concernant', 'contre', 'dans', 'depuis', 'derrière', 'dès', 'devant', 'durant', 'en', 'entre', 'envers', 'hormis', 'hors', 'jusque', 'malgré', 'moyennant', 'nonobstant', 'outre', 'par', 'parmi pendant', 'pour', 'près', 'sans', 'sauf', 'selon', 'sous', 'suivant', 'sur', 'touchant', 'vers', 'via', 'tout','tous', 'toute', 'toutes', 'jusqu']
stopwords_set=set(stopword_nltk+motoutil)

for line in f_in.readlines():
    new_line = re.sub('\W',' ', line.lower())
    list_word = [word for word in new_line.split() if word not in stopwords_set]
    cleaned_line = ' '.join(list_word)
    f_out_trameur.write(cleaned_line)
    f_out_cleaned.write(cleaned_line)
</code>```

Note that write() does not add a newline character \n, so you might have to add this (f_out_trameur.write(cleaned_line+'\n') and f_out_cleaned.write(cleaned_line+'\n')) depending on how you want your output file to look.

ContentLicense: CC BY-SA 4.0

Body:
I used principally the stopwords list of NLTK like what the codes show

from nltk.corpus import stopwords`
stopword_nltk=stopwords.words('french')
motoutil=['après', 'avant', 'avex', 'chez', '\ba\b', 'et', 'concernant', 'contre', 'dans', 'depuis', 'derrière', 'dès', 'devant', 'durant', 'en', 'entre', 'envers', 'hormis', 'hors', 'jusque', 'malgré', 'moyennant', 'nonobstant', 'outre', 'par', 'parmi pendant', 'pour', 'près', 'sans', 'sauf', 'selon', 'sous', 'suivant', 'sur', 'touchant', 'vers', 'via', 'tout','tous', 'toute', 'toutes', 'jusqu']
stopwords_list=stopword_nltk+motoutil

It's not because I added another list to stopword_nltk that the program doesn't feed my need. Even though I remove motoutil, it doesn't work, either.

And this is the part where I plan to delete the stopwords:

for line in f_in.readlines():
    new_line=re.sub('\W',' ', line.lower())
    list_word=new_line.split()
    for element in list_word:
        if element in stopwords_list:
            cleaned_line=re.sub(element, ' ', new_line)
            f_out_trameur.write(cleaned_line)
            f_out_cleaned.write(cleaned_line)

It has two problems:

Firstly, the stopwords listed won't be all removed, such as 'et'.

Secondly, I want to also delete the word 'de' and 'ce' but not the two portions in the middle of a word. for example, in the extract "madame monsieur le président de  l'assemblée nationale", the de  preceding the word président should be cleaned, but not the "de" in the word président, with my actual script, président will be "prési nt"

Body:
Do I see it right that you are creating and writing the cleaned line within the inner loop that iterates over the tokens in a line produced by new_line.split()? And if there was nothing found to clean it is not written at all?

This would result in multiple versions of lines with stopwords (one version with each of the stopwords removed), while lines which do no contain stopwords are just skipped.

What I would suggest is since you already have the tokens (you used split()) you just use these to write the new line instead of replacing the tokens in the new line.

This also allows you to convert the list of stopwords to a set and making the check if element in stopwords_list much faster, since this is usually a large list and can get slow for large amounts of words. This is almost always a good way to speed things up when using NLTKs stopwords.

I would also recommend to use a list comprehension to avoid too many nested loops and conditions and make it more readable, but this is just a personal preference.

from nltk.corpus import stopwords
stopword_nltk=stopwords.words('french')
motoutil=['après', 'avant', 'avex', 'chez', '\ba\b', 'et', 'concernant', 'contre', 'dans', 'depuis', 'derrière', 'dès', 'devant', 'durant', 'en', 'entre', 'envers', 'hormis', 'hors', 'jusque', 'malgré', 'moyennant', 'nonobstant', 'outre', 'par', 'parmi pendant', 'pour', 'près', 'sans', 'sauf', 'selon', 'sous', 'suivant', 'sur', 'touchant', 'vers', 'via', 'tout','tous', 'toute', 'toutes', 'jusqu']
stopwords_set=set(stopword_nltk+motoutil)

for line in f_in.readlines():
    new_line = re.sub('\W',' ', line.lower())
    list_word = [word for word in new_line.split() if word not in stopwords_set]
    cleaned_line = ' '.join(list_word)
    f_out_trameur.write(cleaned_line)
    f_out_cleaned.write(cleaned_line)

Note that write() does not add a newline character \n, so you might have to add this (f_out_trameur.write(cleaned_line+'\n') and f_out_cleaned.write(cleaned_line+'\n')) depending on how you want your output file to look.


问:
edit a string by removing characters without creating a new string, is it legit or not?
EDIT: I think I've understood how this concept works, this is my code

```<code>void delete_duplicate(char* str) {
    if (str == NULL) {
        exit(1);
    }
    for (size_t i = 0; str[i] != 0; i++) {
        if (str[i] == str[i + 1]) {
            str[i] = '\0';
        }
    }
}

    int main(void) {
    char str[] = "hhhhhhhheeeeeeeeyyyyyyyyyyy";
    delete_duplicate(str);
    return 0;
}
</code>```

the output string is "00...0h0...000e0...000y" (with lots of zeros). if the string is "abbbb", the string becomes "a" and not "ab".

I was thinking about an algorithm for this exercise:

given a string, for example "ssttringg" the output must be "string". the function has to remove one character if and only if the current character and the next character are the same.

the exercise expects a function declared like this:

```<code>extern void delete_consecutive(char* str);
</code>```

this is my algorithm:

loop: if the current character is equal to the next character, increment length by 1, and repeat until zero terminator is reached.

critical part: dynamic memory allocation.

allocate enough memory to store the output. Why did I say this is the critical part? because I have to do it in a void function, therefore I can't create a second string, allocate enough memory with malloc, and then return it at the end. I need to edit the original string instead. I think I can do it by means of a realloc, and by decrementing the size of the string. But I don't know how to do it, and more important: I don't know if it's legit to do it, without losing data. I think I'm overcomplicating the homework, but it's a void function, therefore I can't simply create a new string and copying characters in it, because I can't return it.

if it's not clear, I'll edit the question.

答:
You can modify the string passed as a parameter if it is modifiable.

Example:

```<code>void deleteFirstChar(char *str)
{
    if(str && *str)
    {

        memmove(str, str + 1, strlen(str));
    }
}

//illegal call
//string literals cannot be changed
void foo(void)
{
    char *str = "Hello";
    deleteFirstChar(str);
}

//legal call
void bar(void)
{
    char str[] = "Hello";
    deleteFirstChar(str);
}
</code>```

Body:
EDIT: I think I've understood how this concept works, this is my code

void delete_duplicate(char* str) {
    if (str == NULL) {
        exit(1);
    }
    for (size_t i = 0; str[i] != 0; i++) {
        if (str[i] == str[i + 1]) {
            str[i] = '\0';
        }
    }
}

    int main(void) {
    char str[] = "hhhhhhhheeeeeeeeyyyyyyyyyyy";
    delete_duplicate(str);
    return 0;
}

the output string is "00...0h0...000e0...000y" (with lots of zeros). if the string is "abbbb", the string becomes "a" and not "ab".

I was thinking about an algorithm for this exercise:

given a string, for example "ssttringg" the output must be "string". the function has to remove one character if and only if the current character and the next character are the same.

the exercise expects a function declared like this:

extern void delete_consecutive(char* str);

this is my algorithm:

loop: if the current character is equal to the next character, increment length by 1, and repeat until zero terminator is reached.

critical part: dynamic memory allocation.

allocate enough memory to store the output. Why did I say this is the critical part? because I have to do it in a void function, therefore I can't create a second string, allocate enough memory with malloc, and then return it at the end. I need to edit the original string instead. I think I can do it by means of a realloc, and by decrementing the size of the string. But I don't know how to do it, and more important: I don't know if it's legit to do it, without losing data. I think I'm overcomplicating the homework, but it's a void function, therefore I can't simply create a new string and copying characters in it, because I can't return it.

if it's not clear, I'll edit the question.

Body:
You can modify the string passed as a parameter if it is modifiable.

Example:

void deleteFirstChar(char *str)
{
    if(str && *str)
    {

        memmove(str, str + 1, strlen(str));
    }
}

//illegal call
//string literals cannot be changed
void foo(void)
{
    char *str = "Hello";
    deleteFirstChar(str);
}

//legal call
void bar(void)
{
    char str[] = "Hello";
    deleteFirstChar(str);
}


问:
PDF API for Android
Someone knows about an API to edit PDF Files in Android? I need something that helps me with the zoom functionality and the rendering of big images.</p>

答: Maybe you can use a WebView and put the image in the html in that view. The WebView already has good zoom functionality.</p>

ContentLicense: CC BY-SA 3.0

Title: PDF API for Android


问:
How to recover a hidden ID from a query string from an XHR GET request?
I'm trying to use the hidden airbnb api. I need to reverse engineer where the ID comes from in the query string of a GET request. For example, take this listing:

https://www.airbnb.ca/rooms/47452643

The "public" ID is shown to be 47452643. However, another ID is needed to use the API.

If you look at the XHR requests in Chrome, you'll see a request starting with " StaysPdpSections?operationName". This is the request I want to replicate. If I copy the request in Insomnia or Postman, I see a variable in the query string starting with:

"variables":"{"id":"U3RheUxpc3Rpbmc6NDc0NTI2NDM="

The hidden ID "U3RheUxpc3Rpbmc6NDc0NTI2NDM" is what I need. It is needed to get the data from this request and must be inserted into the query string. How can I recover the hidden ID "U3RheUxpc3Rpbmc6NDc0NTI2NDM" for each listing dynamically?

答:
That target id is burried really deep in the html....

```<code>import requests
from bs4 import BeautifulSoup as bs
import json

url = 'https://www.airbnb.ca/rooms/47452643'
req = requests.get(url)

soup = bs(req.content, 'html.parser')
script = soup.select_one('script[type="application/json"][id="data-state"]')
data = json.loads(script.text)

target  = data.get('niobeMinimalClientData')[2][1]['variables']
print(target.get('id'))
</code>```

Output:

```<code>U3RheUxpc3Rpbmc6NDc0NTI2NDM=
</code>```

Body:
That target id is burried really deep in the html....

import requests
from bs4 import BeautifulSoup as bs
import json

url = 'https://www.airbnb.ca/rooms/47452643'
req = requests.get(url)

soup = bs(req.content, 'html.parser')
script = soup.select_one('script[type="application/json"][id="data-state"]')
data = json.loads(script.text)

target  = data.get('niobeMinimalClientData')[2][1]['variables']
print(target.get('id'))

Output:

U3RheUxpc3Rpbmc6NDc0NTI2NDM=


问:
read : not a valid identifier while reading input in bash
So i have this program in Shell Script its basic but what i want is to read 3 values from the stdin while i start the program and then i want to print those values but for some reason it gives an error.

Expected output:

```<code>./my_script.sh hello bye 2
hello
bye
2
</code>```

my output:

```<code>./my_script.sh hello bye 2
Word1=1
meu_script.sh: line 2: read: `Word2=': not a valid identifier
hello
bye
2
</code>```

Program:

```<code>#!/bin/bash
read -p "Word1=" $1 "Word2=" $2 "Num=" $3

Word1="${1}"
Word2="${2}"
Num="${3}"
echo ${1}
echo ${2}
echo ${3}
</code>```

Can someone explain me whats going because i dont understand btw im fairly knew in programming in shell script

答:
I just discovered the answer

The text file should be saved with UNIX Line Feeds instead of Mac OS CR or Windows CRLF

Body:
So i have this program in Shell Script its basic but what i want is to read 3 values from the stdin while i start the program and then i want to print those values but for some reason it gives an error.

Expected output:

./my_script.sh hello bye 2
hello
bye
2

my output:

./my_script.sh hello bye 2
Word1=1
meu_script.sh: line 2: read: `Word2=': not a valid identifier
hello
bye
2

Program:

#!/bin/bash
read -p "Word1=" $1 "Word2=" $2 "Num=" $3

Word1="${1}"
Word2="${2}"
Num="${3}"
echo ${1}
echo ${2}
echo ${3}

Can someone explain me whats going because i dont understand btw im fairly knew in programming in shell script


问:
Getting a page to reload where it was when back or forward button was clicked
I have this page that has two tables. The left table is a list of links that point to items in the right table using A href='#itemid' with the corresponding A id='itemid' in the right table. It works fine when you're just clicking links in the left table, but if I click the back or forward button the page reloads but doesn't return to the position it was in. The page URL includes the #itemid, so I don't understand why the page doesn't return to that position when the back or forward button is clicked. Also, the first load is just the webpage name without a hashtag, so I also need to know how to tell the browser to reload the page with both tables at the top. Currently, the only way I can get it to cooperate is by holding SHIFT and clicking the refresh button.

I have tried every refresh example I can find and none of them work.

```<code>META http-equiv='refresh' content='0'
</code>```

This was disastrous, just kept constantly refreshing the page.

```<code>window.location,refresh();
document.location.refresh();
location.refresh();
</code>```

All of those in a unLoad() attribute in the BODY tag, with and without true as the argument to the refresh function. I really don't want my web host's server constantly having to serve the page anyway, so true isn't an option even if it did work.

I am out of ideas. I also understand that the history is a single doubly-linked list and the forward and back buttons just move through the list in each direction, but you would think the action would result in the page being reloaded the way it looked the last time you were on that page, not like you're still on the page you were last on.

答:
You need to use the history API, to push and pop states according to your needs.

Every time you click one of your links, you need to push a state and a location so that when you press the back button, that state is restored with the given location.

Below is an example on how you might use it. I would not recommend managing the whole page content with innerHTML. This is just done for simplicity. What you are doing in the callback and with the state, and under what circumstances you use saveState to push extra state, is up to you.

```<code>const router = (hook = console.log) => {
  window.onpopstate = ({ state }) => hook(new URL(window.location.href), state);
  hook(new URL(window.location.href));
  return {
    anchorHandler: e => {
      e.preventDefault();
      window.history.pushState(null, "", e.target.href);
      hook(new URL(e.target.href));
      return false;
    },
    saveState: (state, href = window.location.href) => {
      window.history.pushState(state, "", href);
    }
  };
};

const locations = {
  "/": (state) => `<h1>Home</h1>
    <p>Type something in this input. If you blur it, it will be saved as state.
      Now you can navigate back and forth to remove/restore it. <br />
      For example, type something and navigate to the about page. Then press the back button.
      </p>
      <br />
    <input
      value="${state?.text || ''}"
      onblur="saveState({text: this.value})"
    />
  `,
  "/about": (state) => `<h1>About</h1>`,
  "/contact": (state) => `<h1>Contact</h1>`,
}

pageContent = document.getElementById("page-content");

const { saveState, anchorHandler } = router((location, state) =>
  pageContent.innerHTML = locations[location.pathname]
    ? locations[location.pathname](state)
    : locations["/"](state)
);

document
  .querySelectorAll("a")
  .forEach(a => a.addEventListener("click", anchorHandler));
</code>```
```<code><nav>
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/about">About</a></li>
    <li><a href="/contact">Contact</a></li>
  </ul>
</nav>
<main>
  <div id="page-content"></div>
</main>
</code>```

I cannot show it as snippet because, SO, as security limitations around the usage of the history API. You can check pull this repo and run it locally https://github.com/bluebrown/spa-router-example, or visit this page where I have deployed that repo, https://spa-router-example.pages.dev/.

Body:
I have this page that has two tables. The left table is a list of links that point to items in the right table using A href='#itemid' with the corresponding A id='itemid' in the right table. It works fine when you're just clicking links in the left table, but if I click the back or forward button the page reloads but doesn't return to the position it was in. The page URL includes the #itemid, so I don't understand why the page doesn't return to that position when the back or forward button is clicked. Also, the first load is just the webpage name without a hashtag, so I also need to know how to tell the browser to reload the page with both tables at the top. Currently, the only way I can get it to cooperate is by holding SHIFT and clicking the refresh button.

I have tried every refresh example I can find and none of them work.

META http-equiv='refresh' content='0'

This was disastrous, just kept constantly refreshing the page.

window.location,refresh();
document.location.refresh();
location.refresh();

All of those in a unLoad() attribute in the BODY tag, with and without true as the argument to the refresh function. I really don't want my web host's server constantly having to serve the page anyway, so true isn't an option even if it did work.

I am out of ideas. I also understand that the history is a single doubly-linked list and the forward and back buttons just move through the list in each direction, but you would think the action would result in the page being reloaded the way it looked the last time you were on that page, not like you're still on the page you were last on.

Body:
You need to use the history API, to push and pop states according to your needs.

Every time you click one of your links, you need to push a state and a location so that when you press the back button, that state is restored with the given location.

Below is an example on how you might use it. I would not recommend managing the whole page content with innerHTML. This is just done for simplicity. What you are doing in the callback and with the state, and under what circumstances you use saveState to push extra state, is up to you.

const router = (hook = console.log) => {
  window.onpopstate = ({ state }) => hook(new URL(window.location.href), state);
  hook(new URL(window.location.href));
  return {
    anchorHandler: e => {
      e.preventDefault();
      window.history.pushState(null, "", e.target.href);
      hook(new URL(e.target.href));
      return false;
    },
    saveState: (state, href = window.location.href) => {
      window.history.pushState(state, "", href);
    }
  };
};

const locations = {
  "/": (state) => `<h1>Home</h1>
    <p>Type something in this input. If you blur it, it will be saved as state.
      Now you can navigate back and forth to remove/restore it. <br />
      For example, type something and navigate to the about page. Then press the back button.
      </p>
      <br />
    <input
      value="${state?.text || ''}"
      onblur="saveState({text: this.value})"
    />
  `,
  "/about": (state) => `<h1>About</h1>`,
  "/contact": (state) => `<h1>Contact</h1>`,
}

pageContent = document.getElementById("page-content");

const { saveState, anchorHandler } = router((location, state) =>
  pageContent.innerHTML = locations[location.pathname]
    ? locations[location.pathname](state)
    : locations["/"](state)
);

document
  .querySelectorAll("a")
  .forEach(a => a.addEventListener("click", anchorHandler));

<nav>
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/about">About</a></li>
    <li><a href="/contact">Contact</a></li>
  </ul>
</nav>
<main>
  <div id="page-content"></div>
</main>

I cannot show it as snippet because, SO, as security limitations around the usage of the history API. You can check pull this repo and run it locally https://github.com/bluebrown/spa-router-example, or visit this page where I have deployed that repo, https://spa-router-example.pages.dev/.


问:
How to select multiple values from a Dictionary using Linq as simple as possible
I need to select a number of values (into a List) from a Dictionary based on a subset of keys.

I'm trying to do this in a single line of code using Linq but what I have found so far seems quite long and clumsy. What would be the shortest (cleanest) way to do this?

This is what I have now (the keys are Strings and keysToSelect is a List of keys to select):

```<code>List<ValueType> selectedValues = dictionary1.Where(x => keysToSelect.Contains(x.Key))
                                            .ToDictionary<String, valueType>(x => x.Key,
                                                                             x => x.Value)
                                            .Values.ToList;
</code>```

Thank you.

答:
The accepted solution is still not the most efficient option from the point of lookups as you still have to check if the key is in the dictionary twice: once to filter the keys, once to lookup the object.

This solution does not have that issue:

```<code>var selectedValues = keysToSelect
  .Select(_ => {
     var found = dict.TryGetValue(_, out TValue? result);
     return (found, result);
  })
  .Where(_ => _.found)
  .Select(_ => _.result!)
  .ToList();
</code>```

Body:
I need to select a number of values (into a List) from a Dictionary based on a subset of keys.

I'm trying to do this in a single line of code using Linq but what I have found so far seems quite long and clumsy. What would be the shortest (cleanest) way to do this?

This is what I have now (the keys are Strings and keysToSelect is a List of keys to select):

List<ValueType> selectedValues = dictionary1.Where(x => keysToSelect.Contains(x.Key))
                                            .ToDictionary<String, valueType>(x => x.Key,
                                                                             x => x.Value)
                                            .Values.ToList;

Thank you.

Body:
The accepted solution is still not the most efficient option from the point of lookups as you still have to check if the key is in the dictionary twice: once to filter the keys, once to lookup the object.

This solution does not have that issue:

var selectedValues = keysToSelect
  .Select(_ => {
     var found = dict.TryGetValue(_, out TValue? result);
     return (found, result);
  })
  .Where(_ => _.found)
  .Select(_ => _.result!)
  .ToList();


问:
input.read() func. lock in the while loop
After connecting to server, I run some commands on server and then trying to take the server knowledge to console with;

```<code>while(i!=-1){
                String c="";
                String line = "";

                    try {
                        while ((i = input.read()) != 10 && i!=-1) {
                            bx[0] = (byte) i;
                            c = new String(bx);
                            line = line + c ;
                            System.out.print(c);
                        }
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                    File outfile = new File("calltrak.txt");
                    boolean append = true;
                    try
                    {
                        if (!outfile.exists())
                        {
                            append = false;
                        }
                        FileWriter fout1 = new FileWriter("calltrak.txt",append);
                        PrintWriter fileout = new PrintWriter(fout1,true);
                        fileout.println(line);
                        fileout.flush();
                        fileout.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    disp.append(line);
            }
</code>```

But the problem is when the program read all lines from server windows, in server it waits to new input and my prog still tring to read the line and so it locked... How can I solve this problem... (Note:Using a timer isn't a way to solve because the lines which the program read can be 100 line or 100000 and sometimes server can work slow) (In the code "disp" is Jpanel name)

答: There are several problems performance wise with your code, but to answer your question you should let the server send a EndOfText 0x3 or EndOfTransmission 0x4 at the end see AsciiTable this way you can terminate then.</p>

Body:
After connecting to server, I run some commands on server and then trying to take the server knowledge to console with;

while(i!=-1){
                String c="";
                String line = "";

                    try {
                        while ((i = input.read()) != 10 && i!=-1) {
                            bx[0] = (byte) i;
                            c = new String(bx);
                            line = line + c ;
                            System.out.print(c);
                        }
                    } catch (IOException e2) {
                        e2.printStackTrace();
                    }
                    File outfile = new File("calltrak.txt");
                    boolean append = true;
                    try
                    {
                        if (!outfile.exists())
                        {
                            append = false;
                        }
                        FileWriter fout1 = new FileWriter("calltrak.txt",append);
                        PrintWriter fileout = new PrintWriter(fout1,true);
                        fileout.println(line);
                        fileout.flush();
                        fileout.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    disp.append(line);
            }

But the problem is when the program read all lines from server windows, in server it waits to new input and my prog still tring to read the line and so it locked... How can I solve this problem... (Note:Using a timer isn't a way to solve because the lines which the program read can be 100 line or 100000 and sometimes server can work slow) (In the code "disp" is Jpanel name)


问:
Avoid double counting - only count first occurrence in table
I am trying to do a count by month of the total number of items (serialnumber) that appears in inventory.

This usually can be easily solved with distinct, however, I only want to count if it is the first occurrence that it appears (first insert).

This query gets me most of the way there.

```<code>select date_trunc (‘month’,date) as Date,productid, count(distinct serialnumber) from inventory
where date_trunc(‘month’,date)>= ‘2016-01-01’ and productID in ('1','2') and status = ‘INSERT’
group by date_trunc(‘month’,date), productid
order by date_trunc(‘month’,date) desc
</code>```

But I realize I am double/triple/quadruple counting some serial numbers because an item can reappear in our inventory multiple times over the course of its lifecycle.

The query above covers these scenarios since the serial numbers appear once:

Shows up as new

Shows up as used

Below are the use cases where I realize I may be double/triple/quadruple counting:

Shows up as new then comes back around as used (no limit to how many times it can appear used)

Shows up used then comes back again as used (no limit to how many times it can appear used)

Here's an example I ran into.

(Note: I have added the condition column to better illustrate this). But the particular serial number has been in inventory three times (first as new, then as used twice)

Date
ProductID
Count
Condition

7-1-21
1
1
u

11-1-18
1
1
u

2-1-17
1
1
n

In my current query results, each insert gets counted (once in Feb 2017, once in Nov 2018 and once in July 2021).

How can I amend my query to make sure I'm only counting the very first instance (insert) a particular serial number appears in the inventory table?

答:
In the subquery calculate first insert date only of each product/item using min aggregate function. Then count the items on that result:

```<code>select Date, productid, count(serialnumber)
from (
    select min(date_trunc(‘month’,date)) as Date, productid, serialnumber
    from inventory
    where date_trunc(‘month’,date) >= ‘2016-01-01’
    and productID in ('1','2')
    and status = ‘INSERT’
    group by productid, serialnumber
) x
group by Date, productid
order by Date desc;
</code>```

Body:
I am trying to do a count by month of the total number of items (serialnumber) that appears in inventory.

This usually can be easily solved with distinct, however, I only want to count if it is the first occurrence that it appears (first insert).

This query gets me most of the way there.

select date_trunc (‘month’,date) as Date,productid, count(distinct serialnumber) from inventory
where date_trunc(‘month’,date)>= ‘2016-01-01’ and productID in ('1','2') and status = ‘INSERT’
group by date_trunc(‘month’,date), productid
order by date_trunc(‘month’,date) desc

But I realize I am double/triple/quadruple counting some serial numbers because an item can reappear in our inventory multiple times over the course of its lifecycle.

The query above covers these scenarios since the serial numbers appear once:

Shows up as new

Shows up as used

Below are the use cases where I realize I may be double/triple/quadruple counting:

Shows up as new then comes back around as used (no limit to how many times it can appear used)

Shows up used then comes back again as used (no limit to how many times it can appear used)

Here's an example I ran into.

(Note: I have added the condition column to better illustrate this). But the particular serial number has been in inventory three times (first as new, then as used twice)

Date
ProductID
Count
Condition

7-1-21
1
1
u

11-1-18
1
1
u

2-1-17
1
1
n

In my current query results, each insert gets counted (once in Feb 2017, once in Nov 2018 and once in July 2021).

How can I amend my query to make sure I'm only counting the very first instance (insert) a particular serial number appears in the inventory table?

Body:
In the subquery calculate first insert date only of each product/item using min aggregate function. Then count the items on that result:

select Date, productid, count(serialnumber)
from (
    select min(date_trunc(‘month’,date)) as Date, productid, serialnumber
    from inventory
    where date_trunc(‘month’,date) >= ‘2016-01-01’
    and productID in ('1','2')
    and status = ‘INSERT’
    group by productid, serialnumber
) x
group by Date, productid
order by Date desc;


问:
ESP32 SPIRAM / PSRAM management for VSCode platformio
I am troubleshooting my firmware (brand new but nearing dev completion) memory management: it runs on a custom PCB with a WROVER module.

I want to take advantage of the extra PSRAM/SPIRAM so that the PSRAM is used automatically by the libs.

But I have doubt that this is working as I expect.

Therefore I wanted to make sure the following features are actually triggered as I ask them from the platformio.ini:

SPIRAM is taken into account;

And all libs takes advantage of it.

[UPDATE]

According to the Espressif doc (https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/external-ram.html#external-ram-config-malloc), this should be the default behaviour (Provide External RAM via malloc() (default))

Here is my platformio.ini:

```<code>[env:esp32dev]
platform = espressif32
board = esp-wrover-kit
;esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
    adafruit/Adafruit NeoPixel@^1.10.0
    plerup/EspSoftwareSerial@^6.13.2
    lbernstone/Tone32@^1.0.0
    miwagner/ESP32CAN@^0.0.1

board_build.partitions = huge_app.csv
;no_ota.csv
extra_scripts = pre:build_script_versioning.py

build_flags =
    -DBOARD_HAS_PSRAM
    -mfix-esp32-psram-cache-issue
    -DCONFIG_MBEDTLS_DYNAMIC_BUFFER=1
    -DCONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST=1
    -DCONFIG_SPIRAM_CACHE_WORKAROUND=1

</code>```

And the information related to my ESP32 WROVER:

```<code>b1453|—| nano: Firmware v0.1 (build 1453) by Sdl
b1453|—| nano: -- only suited for PCB REV 3 --
b1453|—| nano: Free heap size: 212076
b1453|—| nano: Min free heap size: 206600
b1453|—| nano: Max alloc heap size: 113792
b1453|—| nano: PsRam size: 4194252
b1453|—| nano: Runing on chip:
b1453|—| nano: - model 1 (1 is ESP32)
b1453|—| nano: - rev. 1
b1453|—| nano: Chip has the following features:
b1453|—| nano: - has flash. 0
b1453|—| nano: - has 2.4GHz. 1
b1453|—| nano: - has BLE. 1
b1453|—| nano: - has BT classic. 1
</code>```

[UPDATE]

As you can see in my platformIo ini file, I use the following flags:

-mfix-esp32-psram-cache-issue: passed to gcc is similar to CONFIG_SPIRAM_CACHE_WORKAROUND=1;

-DCONFIG_MBEDTLS_DYNAMIC_BUFFER=1: this should tell mbedTLS to allocate the buffer with a malloc, hence use my PSRAM;

-DCONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST=1: this should let the Bluetooth to use the PSRAM for its own allocations;

-DCONFIG_SPIRAM_CACHE_WORKAROUND=1: and this is similar to the first point (so probably is redundant with -mfix-esp32-psram-cache-issue);

How can I make sure that each and every above flag is taken into account.

Additionnally, for example, how can I make sure that the iot module actually allocate the TLS from the PSRAM?

Is there any code I can run from my firmware?

And any tips on PSRAM usage is VERY WELCOME.

答:
According to the Espressif documentation, there are four ways to use the PSRAM. If you want to use PSRAM explicitly to store something, you need to use ps_malloc() to allocate  the memory. This simple sketch will show the allocation of memory from PSRAM before, after of PSRAM memory allocation and after freeing the memory.

```<code>void setup() {
  log_d("Used PSRAM: %d", ESP.getPsramSize() - ESP.getFreePsram());  byte* psdRamBuffer = (byte*)ps_malloc(500000);
  log_d("Used PSRAM: %d", ESP.getPsramSize() - ESP.getFreePsram());
  free(psdRamBuffer);
  log_d("Used PSRAM: %d", ESP.getPsramSize() - ESP.getFreePsram());
}
</code>```

Please noted that there is a threshold when a single allocation should prefer external memory, when allocating a size less than the threshold, the allocator will try internal memory first. Read the documentation for more details.

Body:
I am troubleshooting my firmware (brand new but nearing dev completion) memory management: it runs on a custom PCB with a WROVER module.

I want to take advantage of the extra PSRAM/SPIRAM so that the PSRAM is used automatically by the libs.

But I have doubt that this is working as I expect.

Therefore I wanted to make sure the following features are actually triggered as I ask them from the platformio.ini:

SPIRAM is taken into account;

And all libs takes advantage of it.

[UPDATE]

According to the Espressif doc (https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/external-ram.html#external-ram-config-malloc), this should be the default behaviour (Provide External RAM via malloc() (default))

Here is my platformio.ini:

[env:esp32dev]
platform = espressif32
board = esp-wrover-kit
;esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
    adafruit/Adafruit NeoPixel@^1.10.0
    plerup/EspSoftwareSerial@^6.13.2
    lbernstone/Tone32@^1.0.0
    miwagner/ESP32CAN@^0.0.1

board_build.partitions = huge_app.csv
;no_ota.csv
extra_scripts = pre:build_script_versioning.py

build_flags =
    -DBOARD_HAS_PSRAM
    -mfix-esp32-psram-cache-issue
    -DCONFIG_MBEDTLS_DYNAMIC_BUFFER=1
    -DCONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST=1
    -DCONFIG_SPIRAM_CACHE_WORKAROUND=1

And the information related to my ESP32 WROVER:

b1453|—| nano: Firmware v0.1 (build 1453) by Sdl
b1453|—| nano: -- only suited for PCB REV 3 --
b1453|—| nano: Free heap size: 212076
b1453|—| nano: Min free heap size: 206600
b1453|—| nano: Max alloc heap size: 113792
b1453|—| nano: PsRam size: 4194252
b1453|—| nano: Runing on chip:
b1453|—| nano: - model 1 (1 is ESP32)
b1453|—| nano: - rev. 1
b1453|—| nano: Chip has the following features:
b1453|—| nano: - has flash. 0
b1453|—| nano: - has 2.4GHz. 1
b1453|—| nano: - has BLE. 1
b1453|—| nano: - has BT classic. 1

[UPDATE]

As you can see in my platformIo ini file, I use the following flags:

-mfix-esp32-psram-cache-issue: passed to gcc is similar to CONFIG_SPIRAM_CACHE_WORKAROUND=1;

-DCONFIG_MBEDTLS_DYNAMIC_BUFFER=1: this should tell mbedTLS to allocate the buffer with a malloc, hence use my PSRAM;

-DCONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST=1: this should let the Bluetooth to use the PSRAM for its own allocations;

-DCONFIG_SPIRAM_CACHE_WORKAROUND=1: and this is similar to the first point (so probably is redundant with -mfix-esp32-psram-cache-issue);

How can I make sure that each and every above flag is taken into account.

Additionnally, for example, how can I make sure that the iot module actually allocate the TLS from the PSRAM?

Is there any code I can run from my firmware?

And any tips on PSRAM usage is VERY WELCOME.

Body:
According to the Espressif documentation, there are four ways to use the PSRAM. If you want to use PSRAM explicitly to store something, you need to use ps_malloc() to allocate  the memory. This simple sketch will show the allocation of memory from PSRAM before, after of PSRAM memory allocation and after freeing the memory.

void setup() {
  log_d("Used PSRAM: %d", ESP.getPsramSize() - ESP.getFreePsram());  byte* psdRamBuffer = (byte*)ps_malloc(500000);
  log_d("Used PSRAM: %d", ESP.getPsramSize() - ESP.getFreePsram());
  free(psdRamBuffer);
  log_d("Used PSRAM: %d", ESP.getPsramSize() - ESP.getFreePsram());
}

Please noted that there is a threshold when a single allocation should prefer external memory, when allocating a size less than the threshold, the allocator will try internal memory first. Read the documentation for more details.


问:
How to convert this nested loop to numpy broadcast?
I want to rearrange my data (two even-length 1d arrays):

cs = [w x y z]
rs = [a b c d e f]
```
<p>to make a result like this:</p>
<pre class="lang-py prettyprint-override"><code>[[a b w x]
 [c d w x]
 [e f w x]
 [a b y z]
 [c d y z]
 [e f y z]]
</code>```

This is what I have tried (it works):

ls = []
for c in range(0,len(cs),2):
  for r in range(0,len(rs),2):
    item = [rs[r], rs[r+1], cs[c], cs[c+1]]
    ls.append(item)
```

But I want to get the same result using reshaping/broadcasting or other numpy functions.
What is the idiomatic way to do this task in numpy?

答:
You could tile the elements of rs, repeat the elements of cs and then arrange those as columns for a 2D array:

import numpy as np

cs = np.array(['w', 'x', 'y', 'z'])
rs = np.array(['a', 'b', 'c', 'd', 'e', 'f'])
res = np.c_[np.tile(rs[::2], len(cs) // 2), np.tile(rs[1::2], len(cs) // 2),
            np.repeat(cs[::2], len(rs) // 2), np.repeat(cs[1::2], len(rs) // 2)]
```
<p>Result:</p>
<pre class="lang-py prettyprint-override"><code>array([['a', 'b', 'w', 'x'],
       ['c', 'd', 'w', 'x'],
       ['e', 'f', 'w', 'x'],
       ['a', 'b', 'y', 'z'],
       ['c', 'd', 'y', 'z'],
       ['e', 'f', 'y', 'z']], dtype='<U1')
</code>```

An alternative:

np.c_[np.tile(rs.reshape(-1, 2), (len(cs) // 2, 1)),
      np.repeat(cs.reshape(-1, 2), len(rs) // 2, axis=0)]
```

Body:
I want to rearrange my data (two even-length 1d arrays):

cs = [w x y z]
rs = [a b c d e f]

to make a result like this:

[[a b w x]
 [c d w x]
 [e f w x]
 [a b y z]
 [c d y z]
 [e f y z]]

This is what I have tried (it works):

ls = []
for c in range(0,len(cs),2):
  for r in range(0,len(rs),2):
    item = [rs[r], rs[r+1], cs[c], cs[c+1]]
    ls.append(item)

But I want to get the same result using reshaping/broadcasting or other numpy functions.
What is the idiomatic way to do this task in numpy?

Body:
You could tile the elements of rs, repeat the elements of cs and then arrange those as columns for a 2D array:

import numpy as np

cs = np.array(['w', 'x', 'y', 'z'])
rs = np.array(['a', 'b', 'c', 'd', 'e', 'f'])
res = np.c_[np.tile(rs[::2], len(cs) // 2), np.tile(rs[1::2], len(cs) // 2),
            np.repeat(cs[::2], len(rs) // 2), np.repeat(cs[1::2], len(rs) // 2)]

Result:

array([['a', 'b', 'w', 'x'],
       ['c', 'd', 'w', 'x'],
       ['e', 'f', 'w', 'x'],
       ['a', 'b', 'y', 'z'],
       ['c', 'd', 'y', 'z'],
       ['e', 'f', 'y', 'z']], dtype='<U1')

An alternative:

np.c_[np.tile(rs.reshape(-1, 2), (len(cs) // 2, 1)),
      np.repeat(cs.reshape(-1, 2), len(rs) // 2, axis=0)]


问:
Prevent submit form with ENTER jquery not working with alerts
I have a form with a text input.

I managed to prevent ENTER from submitting the form by doing this:

```<code>jQuery("#inputTags").keydown(function(event) {
      if (event.keyCode == '13') {
         event.preventDefault();

         tagManagerCreate(true);

         return false;
      }
    });
</code>```

tagManagerCreate() does some stuff. Sometimes, this function triggers a javascript "alert".
For some reason when that happens, the form still gets submitted!

答:
Can you try to wrap tagManagerCreate(true); in a try/catch block and see?

```<code>try{

   tagManagerCreate(true);

}catch(e){}
</code>```

Body:
I have a form with a text input.

I managed to prevent ENTER from submitting the form by doing this:

jQuery("#inputTags").keydown(function(event) {
      if (event.keyCode == '13') {
         event.preventDefault();

         tagManagerCreate(true);

         return false;
      }
    });

tagManagerCreate() does some stuff. Sometimes, this function triggers a javascript "alert".
For some reason when that happens, the form still gets submitted!

Body:
Can you try to wrap tagManagerCreate(true); in a try/catch block and see?

try{

   tagManagerCreate(true);

}catch(e){}


问:
vue-formulate group custom remove not working
I have a piece of code that use Vue-Formulate with group feature and I try to implement custom button to remove nested items;

<FormulateInput
  type="group"
  :name="field.name"
  v-bind="field.attributes"
  remove-position="after"
>

      <FormulateInput
        :type="_field.type"
        :name="_field.name"
        ....
      />

    <FormulateInput
      type="button"
    >
      Remove Student
    </FormulateInput>

</FormulateInput>
```

Such addiction change default ...> link to button but functionality of removing item being lost.
Documentation of this slot says:

"The remove button when repeatable. The context object in this slot
includes the index and a removeItem function that should be called to
remove that item."

I am not sure how to add removeItem function call to it.

答:
The removeItem slot prop is only provided to repeatable groups, so make sure to set repeatable on the FormulateInput with type=group.

To insert a custom remove-button in the remove scoped slot, wrap the button in a <template v-slot:remove="{ removeItem }">, and set removeItem as the button's click-handler via the v-on directive (@click for shorthand):

<FormulateInput type="group" repeatable 1⃣>
  <FormulateInput name="name" label="Student’s name" />
  <FormulateInput type="email" name="email" label="Student’s email" />

  2⃣
  <template v-slot:remove="{ removeItem }">
    <FormulateInput type="button" @click="removeItem"> Remove Student </FormulateInput>
  </template>
</FormulateInput>
```

demo

Body:
I have a piece of code that use Vue-Formulate with group feature and I try to implement custom button to remove nested items;

<FormulateInput
  type="group"
  :name="field.name"
  v-bind="field.attributes"
  remove-position="after"
>

      <FormulateInput
        :type="_field.type"
        :name="_field.name"
        ....
      />

    <FormulateInput
      type="button"
    >
      Remove Student
    </FormulateInput>

</FormulateInput>

Such addiction change default  link to button but functionality of removing item being lost.
Documentation of this slot says:

"The remove button when repeatable. The context object in this slot
includes the index and a removeItem function that should be called to
remove that item."

I am not sure how to add removeItem function call to it.

Body:
The removeItem slot prop is only provided to repeatable groups, so make sure to set repeatable on the FormulateInput with type=group.

To insert a custom remove-button in the remove scoped slot, wrap the button in a <template v-slot:remove="{ removeItem }">, and set removeItem as the button's click-handler via the v-on directive (@click for shorthand):

<FormulateInput type="group" repeatable 1⃣>
  <FormulateInput name="name" label="Student’s name" />
  <FormulateInput type="email" name="email" label="Student’s email" />

  2⃣
  <template v-slot:remove="{ removeItem }">
    <FormulateInput type="button" @click="removeItem"> Remove Student </FormulateInput>
  </template>
</FormulateInput>

demo


问:
How to correctly write a UI test for a Swift UI Toggle
Does anyone know how to correctly write a UI test for a Toggle? Even in a brand new project with just a Toggle and nothing else in the entire UI I keep getting this sort of error:

```<code>Failed to get matching snapshot: Multiple matching elements found for <XCUIElementQuery: 0x60000108c410>.
Sparse tree of matches:
→Application, pid: 26580, label: 'TestToggle'
 ↳Window (Main)
  ↳Other
   ↳Other
    ↳Other
     ↳Other
      ↳Switch, label: 'Test switch', value: 1
       ↳Switch, label: 'Test switch', value: 1
</code>```

UI looks like this:

```<code>struct ContentView: View {
  @State private var toggleValue = true
  var body: some View {
    Toggle("Test switch", isOn: $toggleValue)
      .padding()
  }
}
</code>```

Test looks like this (either of those lines gives me this same error):

```<code>     func testExample() throws {
        let app = XCUIApplication()
        app.launch()

        XCTAssertTrue(app.switches["Test switch"].value as? String == "1")
//        XCTAssertTrue(app.switches["Test switch"].isEnabled)
    }
</code>```

Surely I’m doing something wrong. How can there be two switches showing up if there’s only one? None of the online articles seem to mention anything about this that I’ve seen. Any help appreciated. Thanks :)

答:
I got a few responses in some slack channels and it turns out that for some reason using a Toggle I can't rely on the default label for testing (unlike Text and Button) and need to create a custom accessibility identifier.

So just add something like this:

```<code>    Toggle("Test switch", isOn: $toggleValue)
        .padding()
        .accessibilityIdentifier("testSwitch")
</code>```

and then test like this works:

```<code>func testExample() throws {
    let app = XCUIApplication()
    app.launch()
    XCTAssertTrue(app.switches["testSwitch"].isEnabled)
    XCTAssertTrue(app.switches["testSwitch"].value as? String == "1")
}
</code>```

Body:
Does anyone know how to correctly write a UI test for a Toggle? Even in a brand new project with just a Toggle and nothing else in the entire UI I keep getting this sort of error:

Failed to get matching snapshot: Multiple matching elements found for <XCUIElementQuery: 0x60000108c410>.
Sparse tree of matches:
→Application, pid: 26580, label: 'TestToggle'
 ↳Window (Main)
  ↳Other
   ↳Other
    ↳Other
     ↳Other
      ↳Switch, label: 'Test switch', value: 1
       ↳Switch, label: 'Test switch', value: 1

UI looks like this:

struct ContentView: View {
  @State private var toggleValue = true
  var body: some View {
    Toggle("Test switch", isOn: $toggleValue)
      .padding()
  }
}

Test looks like this (either of those lines gives me this same error):

     func testExample() throws {
        let app = XCUIApplication()
        app.launch()

        XCTAssertTrue(app.switches["Test switch"].value as? String == "1")
//        XCTAssertTrue(app.switches["Test switch"].isEnabled)
    }

Surely I’m doing something wrong. How can there be two switches showing up if there’s only one? None of the online articles seem to mention anything about this that I’ve seen. Any help appreciated. Thanks :)

Body:
I got a few responses in some slack channels and it turns out that for some reason using a Toggle I can't rely on the default label for testing (unlike Text and Button) and need to create a custom accessibility identifier.

So just add something like this:

    Toggle("Test switch", isOn: $toggleValue)
        .padding()
        .accessibilityIdentifier("testSwitch")

and then test like this works:

func testExample() throws {
    let app = XCUIApplication()
    app.launch()
    XCTAssertTrue(app.switches["testSwitch"].isEnabled)
    XCTAssertTrue(app.switches["testSwitch"].value as? String == "1")
}


问:
mypy reports errors on custom defined builtins
I'm using the gettext package to perform translations in the Python application. There's a custom Translation class which serves as a wrapper around strings and also defines a custom built-in function

```<code>class Wrapper:
    def __init__(self, message: str):
        self.message = message

    @classmethod
    def install(cls):
        import builtins
        builtins._ = cls

test = _('translate me')
</code>```

When running mypy over this code I'm receiving the error

```<code>test.py: error: Name "_" is not defined
</code>```

Is there a way to tell mypy abou the custom created built-in function?
In flake8 I was able to set a config with

```<code>[flake8]
builtins = _
</code>```

答:
If I understand well, you need something as:</p>
```<code>_: Type[Wrapper]

test = _('translate me')
</code>```

Body:
I'm using the gettext package to perform translations in the Python application. There's a custom Translation class which serves as a wrapper around strings and also defines a custom built-in function

class Wrapper:
    def __init__(self, message: str):
        self.message = message

    @classmethod
    def install(cls):
        import builtins
        builtins._ = cls

test = _('translate me')

When running mypy over this code I'm receiving the error

test.py: error: Name "_" is not defined

Is there a way to tell mypy abou the custom created built-in function?
In flake8 I was able to set a config with

[flake8]
builtins = _

Body:
If I understand well, you need something as:

_: Type[Wrapper]

test = _('translate me')


问:
Initialize an array in a struct
Is there already a way where I can initialize an array directly in a struct? Like this:

```<code>struct S
{
  int arr[50] = {5};
}
</code>```

I know this only initializes the first element of the array, but is there any way to write something similar with g++ but that can initialize all elements of the array with 5?

I've read that with gcc we can use designated intializers int arr[50] = {[0 ... 49] = 5}; but this won't be possible in C++.

答:
Since a struct object needs to have its constructor called when being initialized you can just perform this assignment inside the constructor, e.g.:

```<code>struct S
{
  int arr[50];
  S() {
    for (int& val : arr) val = 5;
  }
};
</code>```

Or similarly you can use std::fill from the algorithm header

```<code>#include <algorithm>
struct S
{
  int arr[50];
  S() {
    std::fill(std::begin(arr), std::end(arr), 5);
  }
};
</code>```

Title: Initialize an array in a struct

Body:
Is there already a way where I can initialize an array directly in a struct? Like this:

struct S
{
  int arr[50] = {5};
}

I know this only initializes the first element of the array, but is there any way to write something similar with g++ but that can initialize all elements of the array with 5?

I've read that with gcc we can use designated intializers int arr[50] = {[0 ... 49] = 5}; but this won't be possible in C++.

Body:
Since a struct object needs to have its constructor called when being initialized you can just perform this assignment inside the constructor, e.g.:

struct S
{
  int arr[50];
  S() {
    for (int& val : arr) val = 5;
  }
};

Or similarly you can use std::fill from the algorithm header

#include <algorithm>
struct S
{
  int arr[50];
  S() {
    std::fill(std::begin(arr), std::end(arr), 5);
  }
};


问:
Exporting In-Memory HSQL Database Content as SQL During Maven Build
During a Maven build's integration test phase I am populating an in-memory HSQL database based on the performed tests. Afterwards, I would like to capture this state by exporting the database content as SQL statements (for later import).

Is there some Maven plugin or command line tool suitable for this task? For MySQL we are using mysqldump, so I am basically looking for an equivalent for HSQL.

答: There's the dbunit plugin, for one, which should work for a variety of databases.</p>


问:
how to calculate theta in univariate linear regression model?
I have hypothesis function h(x) = theta0 + theta1*x.

How can I select theta0 and theta1 value for the linear regression model?

答: If you are asking about the hypothesis function in linear regression, then those theta values are selected by an algorithm called gradient descent. This helps in finding the theta values to minimize the cost function.</p>


问:
fatal no pg_hba.conf entry error when using Heroku and Postgresql
I'm getting this error when pushing my Django project to Heroku:

```<code>Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/app/.heroku/python/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL:  password authentication failed for user "jkkflotmzeoxrq"
FATAL:  no pg_hba.conf entry for host "54.171.72.104", user "jkkflotmzeoxrq", database "dcbe6ld5353s18", SSL off
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 92, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 216, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
    self.ensure_connection()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/app/.heroku/python/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL:  password authentication failed for user "jkkflotmzeoxrq"
FATAL:  no pg_hba.conf entry for host "54.171.72.104", user "jkkflotmzeoxrq", database "dcbe6ld5353s18", SSL off
</code>```

I've looked at the postgres database that is attached to my heroku project and the credentials are not the same as displayed we have not made any changes to the database only the django project itself. Heroku was working up until like 10 days ago and now every time we push to heroku the deployment fails because of this error.

My settings.py file:

```<code>import os

import dj_database_url

from reportr.settings.local.email_settings import *
from reportr.settings.packages.aws_settings import *
from reportr.settings_base import *

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_env_variable("SECRET_KEY", )

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = int(get_env_variable("DEBUG", "1"))

ALLOWED_HOSTS = ['*']

INSTALLED_APPS.append('storages')

# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {}
DATABASES['default'] = dj_database_url.parse(
    get_env_variable('DATABASE_URL'), conn_max_age=600)

MIDDLEWARE.insert(1, 'whitenoise.middleware.WhiteNoiseMiddleware')

MEDIA_URL = '/uploads/'

if os.getenv("EXTRA_STATICFILES_DIRS"):
    STATIC_ROOT = os.getenv("EXTRA_STATICFILES_DIRS")

ADMINS = []
for admin in get_env_variable('ADMINS', '-').split(", "):
    if admin:
        admin = tuple(admin.split(":", 1))
        if len(admin) == 2:
            ADMINS.append(admin)
</code>```

Error after updating the database variable:

```<code>-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> Using Python version specified in runtime.txt
-----> No change in requirements detected, installing from cache
-----> Using cached install of python-3.8.12
-----> Installing pip 21.3.1, setuptools 57.5.0 and wheel 0.37.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
celery 5.2.3 requires setuptools<59.7.0,>=59.1.1, but you have setuptools 57.5.0 which is incompatible.
-----> Installing SQLite3
-----> Installing requirements with pip
       Collecting setuptools
         Downloading setuptools-59.6.0-py3-none-any.whl (952 kB)
       Installing collected packages: setuptools
         Attempting uninstall: setuptools
           Found existing installation: setuptools 57.5.0
           Uninstalling setuptools-57.5.0:
             Successfully uninstalled setuptools-57.5.0
       Successfully installed setuptools-59.6.0
-----> $ python manage.py collectstatic --noinput
       Traceback (most recent call last):
         File "manage.py", line 22, in <module>
           main()
         File "manage.py", line 18, in main
           execute_from_command_line(sys.argv)
         File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
           utility.execute()
         File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 345, in execute
           settings.INSTALLED_APPS
         File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 83, in __getattr__
           self._setup(name)
         File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 70, in _setup
           self._wrapped = Settings(settings_module)
         File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 177, in __init__
           mod = importlib.import_module(self.SETTINGS_MODULE)
         File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
           return _bootstrap._gcd_import(name[level:], package, level)
         File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
         File "<frozen importlib._bootstrap>", line 991, in _find_and_load
         File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
         File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
         File "<frozen importlib._bootstrap_external>", line 843, in exec_module
         File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
         File "/tmp/build_fcfd3411/reportr/settings_prod.py", line 23, in <module>
           DATABASES['default'].update(db_from_env)
       KeyError: 'default'
 !     Error while running '$ python manage.py collectstatic --noinput'.
       See traceback above for details.
       You may need to update application code to resolve this error.
       Or, you can disable collectstatic for this application:
          $ heroku config:set DISABLE_COLLECTSTATIC=1
       https://devcenter.heroku.com/articles/django-assets
 !     Push rejected, failed to compile Python app.
 !     Push failed
</code>```

答:
Perhaps Heroku changed your credentials.  That would mean if you hard coded DATABASE_URL the password authentication would fail.  Resolution: run heroku config or visit your apps settings and select "Reveal Config Vars" to get the new credentials.

Here's the exert from the Heroku docs:

Occasionally we will roll Heroku Postgres credentials, this can happen
for several reasons.

Normally this is around maintenance or hardware failures but we also
perform credential rolls for security reasons - typically this
involves us setting the new configuration variable, DATABASE_URL for
primary app databases, and restarting your application. This can cause
issues if you have manually copy and pasted these credentials
elsewhere, or have hardcoded them into your application.

You can get the new credentials by running this command via the CLI:
heroku config or visiting your app's Settings page and clicking the
"Reveal Config Vars".

Edit

I see that you do indeed have the DATABASE_URL hardcoded, and it should NOT be.

Change

DATABASES = {}
# This next line shows that you you are getting
# the DATABASE_URL from your .env file, but this is
# what has changed, and is no longer valid.
DATABASES['default'] = dj_database_url.parse(
    get_env_variable('DATABASE_URL'), conn_max_age=600)
```
<p>To <em>just</em></p>
<pre class="lang-py prettyprint-override"><code>DATABASES = {'default': dj_database_url.config(default=get_env_variable('DATABASE_URL')), conn_max_age=600}
</code>```

This should get the updated DATABASE_URL that Heroku generates.

Edit 2

I changed the above to deal with the DATABASE['default'] KeyError, but I see you also have a separate issue with running collectstatic.  Check out this answer

Edit 3

Is it possible you have more than one database attached?  Install the Heroku CLI if you don't have it already then

Run

```<code>heroku pg:info
</code>```

That should list the databases.  Then promote the correct one, by running

```<code>heroku pg:promote HEROKU_POSTGRESQL_<COLOR>_URL -a example-app
</code>```

Where example-app is the name of your app, and replace <COLOR> with the correct database.
Finally, destroy the unwanted databases with

```<code>heroku addons:destroy HEROKU_POSTGRESQL_<COLOR>
</code>```

Where <COLOR> is the database you want destroyed.

Another option is to delete the app, then re-add the postgres addon after you add the app in again.

GOOD LUCK!

Body:
I'm getting this error when pushing my Django project to Heroku:

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/app/.heroku/python/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL:  password authentication failed for user "jkkflotmzeoxrq"
FATAL:  no pg_hba.conf entry for host "54.171.72.104", user "jkkflotmzeoxrq", database "dcbe6ld5353s18", SSL off
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 92, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 216, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
    self.ensure_connection()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/app/.heroku/python/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL:  password authentication failed for user "jkkflotmzeoxrq"
FATAL:  no pg_hba.conf entry for host "54.171.72.104", user "jkkflotmzeoxrq", database "dcbe6ld5353s18", SSL off

I've looked at the postgres database that is attached to my heroku project and the credentials are not the same as displayed we have not made any changes to the database only the django project itself. Heroku was working up until like 10 days ago and now every time we push to heroku the deployment fails because of this error.

My settings.py file:

import os

import dj_database_url

from reportr.settings.local.email_settings import *
from reportr.settings.packages.aws_settings import *
from reportr.settings_base import *

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_env_variable("SECRET_KEY", )

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = int(get_env_variable("DEBUG", "1"))

ALLOWED_HOSTS = ['*']

INSTALLED_APPS.append('storages')

# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {}
DATABASES['default'] = dj_database_url.parse(
    get_env_variable('DATABASE_URL'), conn_max_age=600)

MIDDLEWARE.insert(1, 'whitenoise.middleware.WhiteNoiseMiddleware')

MEDIA_URL = '/uploads/'

if os.getenv("EXTRA_STATICFILES_DIRS"):
    STATIC_ROOT = os.getenv("EXTRA_STATICFILES_DIRS")

ADMINS = []
for admin in get_env_variable('ADMINS', '-').split(", "):
    if admin:
        admin = tuple(admin.split(":", 1))
        if len(admin) == 2:
            ADMINS.append(admin)

Error after updating the database variable:

-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> Using Python version specified in runtime.txt
-----> No change in requirements detected, installing from cache
-----> Using cached install of python-3.8.12
-----> Installing pip 21.3.1, setuptools 57.5.0 and wheel 0.37.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
celery 5.2.3 requires setuptools<59.7.0,>=59.1.1, but you have setuptools 57.5.0 which is incompatible.
-----> Installing SQLite3
-----> Installing requirements with pip
       Collecting setuptools
         Downloading setuptools-59.6.0-py3-none-any.whl (952 kB)
       Installing collected packages: setuptools
         Attempting uninstall: setuptools
           Found existing installation: setuptools 57.5.0
           Uninstalling setuptools-57.5.0:
             Successfully uninstalled setuptools-57.5.0
       Successfully installed setuptools-59.6.0
-----> $ python manage.py collectstatic --noinput
       Traceback (most recent call last):
         File "manage.py", line 22, in <module>
           main()
         File "manage.py", line 18, in main
           execute_from_command_line(sys.argv)
         File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
           utility.execute()
         File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 345, in execute
           settings.INSTALLED_APPS
         File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 83, in __getattr__
           self._setup(name)
         File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 70, in _setup
           self._wrapped = Settings(settings_module)
         File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 177, in __init__
           mod = importlib.import_module(self.SETTINGS_MODULE)
         File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
           return _bootstrap._gcd_import(name[level:], package, level)
         File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
         File "<frozen importlib._bootstrap>", line 991, in _find_and_load
         File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
         File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
         File "<frozen importlib._bootstrap_external>", line 843, in exec_module
         File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
         File "/tmp/build_fcfd3411/reportr/settings_prod.py", line 23, in <module>
           DATABASES['default'].update(db_from_env)
       KeyError: 'default'
 !     Error while running '$ python manage.py collectstatic --noinput'.
       See traceback above for details.
       You may need to update application code to resolve this error.
       Or, you can disable collectstatic for this application:
          $ heroku config:set DISABLE_COLLECTSTATIC=1
       https://devcenter.heroku.com/articles/django-assets
 !     Push rejected, failed to compile Python app.
 !     Push failed

Body:
Perhaps Heroku changed your credentials.  That would mean if you hard coded DATABASE_URL the password authentication would fail.  Resolution: run heroku config or visit your apps settings and select "Reveal Config Vars" to get the new credentials.

Here's the exert from the Heroku docs:

Occasionally we will roll Heroku Postgres credentials, this can happen
for several reasons.

Normally this is around maintenance or hardware failures but we also
perform credential rolls for security reasons - typically this
involves us setting the new configuration variable, DATABASE_URL for
primary app databases, and restarting your application. This can cause
issues if you have manually copy and pasted these credentials
elsewhere, or have hardcoded them into your application.

You can get the new credentials by running this command via the CLI:
heroku config or visiting your app's Settings page and clicking the
"Reveal Config Vars".

Edit

I see that you do indeed have the DATABASE_URL hardcoded, and it should NOT be.

Change

DATABASES = {}
# This next line shows that you you are getting
# the DATABASE_URL from your .env file, but this is
# what has changed, and is no longer valid.
DATABASES['default'] = dj_database_url.parse(
    get_env_variable('DATABASE_URL'), conn_max_age=600)

To just

DATABASES = {'default': dj_database_url.config(default=get_env_variable('DATABASE_URL')), conn_max_age=600}

This should get the updated DATABASE_URL that Heroku generates.

Edit 2

I changed the above to deal with the DATABASE['default'] KeyError, but I see you also have a separate issue with running collectstatic.  Check out this answer

Edit 3

Is it possible you have more than one database attached?  Install the Heroku CLI if you don't have it already then

Run

heroku pg:info

That should list the databases.  Then promote the correct one, by running

heroku pg:promote HEROKU_POSTGRESQL_<COLOR>_URL -a example-app

Where example-app is the name of your app, and replace <COLOR> with the correct database.
Finally, destroy the unwanted databases with

heroku addons:destroy HEROKU_POSTGRESQL_<COLOR>

Where <COLOR> is the database you want destroyed.

Another option is to delete the app, then re-add the postgres addon after you add the app in again.

GOOD LUCK!


问:
How do I know which version of vCard my android phone "Contacts APP" support?
I am basically creating a simple text file which is named test.vcf with below content as an example:

```<code>BEGIN:VCARD
VERSION:3.0
FN:AAA BBB CCC
N:AAA BBB CCC
TEL;TYPE=cell:9876543210
END:VCARD
</code>```

The above file worked when I Imported in my phone. where as below content did not work.

```<code>BEGIN:VCARD
VERSION:4.0
FN:AAA BBB CCC
TEL;TYPE=cell:9876543210
END:VCARD
</code>```

This is making me question which version of vcf file my android phone's "Contacts APP" supports.

Here is my android phone's "Contacts APP" About section

```<code>Contacts
Version 9.3.4.340
</code>```

If finding the "Contacts APP" that supports the vcf file version is not necessary, then is there anything wrong with the content I used above for VERSION:4.0?

答:
I used the following, and it worked:

```<code>BEGIN:VCARD
VERSION:4.0
FIRST:Firstname
LAST:Lastname
TEL:00123456798765
FN:Firstname Lastname
REV:1644704181779
END:VCARD
</code>```

Could it be that you need the name by parts? or that something's different with the TYPE property?

I suggest trying multiple combinations, but this particular one worked for me on Android 10, using the "Contacts" app from Google. I hope it helps whoever is looking for it in the future.

Body:
I am basically creating a simple text file which is named test.vcf with below content as an example:

BEGIN:VCARD
VERSION:3.0
FN:AAA BBB CCC
N:AAA BBB CCC
TEL;TYPE=cell:9876543210
END:VCARD

The above file worked when I Imported in my phone. where as below content did not work.

BEGIN:VCARD
VERSION:4.0
FN:AAA BBB CCC
TEL;TYPE=cell:9876543210
END:VCARD

This is making me question which version of vcf file my android phone's "Contacts APP" supports.

Here is my android phone's "Contacts APP" About section

Contacts
Version 9.3.4.340

If finding the "Contacts APP" that supports the vcf file version is not necessary, then is there anything wrong with the content I used above for VERSION:4.0?

Body:
I used the following, and it worked:

BEGIN:VCARD
VERSION:4.0
FIRST:Firstname
LAST:Lastname
TEL:00123456798765
FN:Firstname Lastname
REV:1644704181779
END:VCARD

Could it be that you need the name by parts? or that something's different with the TYPE property?

I suggest trying multiple combinations, but this particular one worked for me on Android 10, using the "Contacts" app from Google. I hope it helps whoever is looking for it in the future.


问:
Is there a good obfuscater for Perl code?
Does anyone know of a good code obsfucator for Perl? I'm being ask to look into the option of obsfucating code before releasing it to a client. I know obsfucated code can still be reverse engineered, but that's not our main concern.

Some clients are making small changes to the source code that we give them and it's giving us nightmares when something goes wrong and we have to fix it, or when we release a patch that doesn't work with what they've changed. So the intention is just to make it so that it's difficult for them to make their own changes to the code(they're not supposed to be doing that anyway).

答:
Don't. Just don't.

Write it into the contract (or revise the contract if you have to), that you are not responsible for changes they make to the software. If they're f-ing up your code and then expecting you to fix it, you have client problems that aren't going to be solved by obfuscating the code. And if you obfuscate it and they encounter an actual problem, good luck in getting them to accurately report line number, etc., in the bug report.

ContentLicense: CC BY-SA 2.5

LastEditorDisplayName: brian d foy

OwnerDisplayName: rjray


问:
WCF exception - "Service not found" when retrieving large collection
my WCF service it's used by a Silverlight application to retrieve data. I've no problem,

```<code>    [OperationContract]
MyCollectionClass GetList(int sessID, string name);

  [CollectionDataContract]
public class MyCollectionClass : List<MyClass>{ }

  [DataContract]
public class MyClass {

  [DataMember]
  public string Prop1 { get; set; }

  [DataMember]
  public string Prop2 { get; set; }
</code>```

}

But.. when MyCollectionClass have a less then 3000+ "record" it works. When the number of records is greater the WCF service seems to work, but on the completed event of the Silverlight app an exception occurs: "Service Not Found".

I've found that could be related to service configuration and i've tryied to use both:

```<code>maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"
</code>```

on WCF and Client configuration. Also added:

```<code>readerQuotas:
  maxArrayLength="2000000"
  maxStringContentLength="2000000"/>
</code>```

(also changed the values found) But seems to not working.
I think that the problem is that the message exceed the max number of byte per "message", but I do not understand why data is not spanned on different message.
 Any tips is appreciated.

Giorgio

答:
I use these 2 statement right after InitializeComponent:

binding.MaxReceivedMessageSize = 5000000
binding.MaxBufferSize = 5000000

You can change the numbers to what you want but I had to do this in order to recieve a large amount of data on the Silverlgiht client.  My binding object is defines as:

Private binding As New BasicHttpBinding

This is in vb.net.  Works like a charm after I include these items.

OwnerDisplayName: krumikaze

Body:
my WCF service it's used by a Silverlight application to retrieve data. I've no problem,

    [OperationContract]
MyCollectionClass GetList(int sessID, string name);

  [CollectionDataContract]
public class MyCollectionClass : List<MyClass>{ }

  [DataContract]
public class MyClass {

  [DataMember]
  public string Prop1 { get; set; }

  [DataMember]
  public string Prop2 { get; set; }

}

But.. when MyCollectionClass have a less then 3000+ "record" it works. When the number of records is greater the WCF service seems to work, but on the completed event of the Silverlight app an exception occurs: "Service Not Found".

I've found that could be related to service configuration and i've tryied to use both:

maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"

on WCF and Client configuration. Also added:

readerQuotas:
  maxArrayLength="2000000"
  maxStringContentLength="2000000"/>

(also changed the values found) But seems to not working.
I think that the problem is that the message exceed the max number of byte per "message", but I do not understand why data is not spanned on different message.
 Any tips is appreciated.

Giorgio

OwnerDisplayName: Bill