问:
java: No implementation was created for ProductMapper due to having a problem in the erroneous element java.util.ArrayList
I'm trying to create very simple JUnit test into Spring Cloud project:

```<code>@SpringBootTest(classes = {ProductMapper.class })
public class TestingWebApplicationTests {

    @Test
    public void contextLoads() {
    }

}

import org.mapstruct.Mapper;

@Mapper(config = BaseMapperConfig.class)
public interface ProductMapper {

    ProductDTO toDTO(Product product);

    ProductFullDTO toFullDTO(Product product);

    Product map(ProductFullDTO productDTO);

    ProductFilter toFilter(ProductFilterDTO dto);
}
</code>```

I get this error when I try to run the test into the latest Intelij.

```<code>java: No implementation was created for ProductMapper due to having a problem in the erroneous element java.util.ArrayList. Hint: this often means that some other annotation processor was supposed to process the erroneous element. You can also enable MapStruct verbose mode by setting -Amapstruct.verbose=true as a compilation argument.
</code>```

Do you know how I can fix this issue?

答:
It's not exactly related to MapStruct itself, but maybe it will save some devs a few minutes / hours when searching for the problem.

I had a similar problem in a project, where I was supposed to add new API version and mark old one as deprecated.

When I tried compiling with maven, I got errors

```<code>[ERROR] /src/main/java/com/example/MapStructMapperClass.java: No implementation was created for MapStructMapperClass.java due to having a problem in the erroneous element java.util.ArrayList. Hint: this often means that some other annotation processor was supposed to process the erroneous element. You can also enable MapStruct verbose mode by setting -Amapstruct.verbose=true as a compilation argument.
</code>```

The project uses Lombok, so I checked annotation processor configs. Looked fine and worked properly on a simple, ad hoc created project.
Tried bumping MapStruct and Lpmbok versions. Still the same problem.

Then, I noticed a few lines in the log:

```<code>[WARNING] COMPILATION WARNING :
[INFO] ------------------------------
[WARNING] /src/main/java/com/example/MyRestController.java: com.example.MyOldService in MyRestController has been deprecated
[INFO] 1 warning
[INFO] ----------------------
[ERROR] COMPILATION ERROR :
[INFO] ----------------------
[ERROR] warnings found and -Werror specified
</code>```

And the config in maven pom.xml

```<code><plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <failOnWarning>true</failOnWarning>
  <configuration>
</plugin>
</code>```

As Javac JDK 8 states:

```<code>-Werror
    Terminate compilation if warnings occur.
</code>```

So, my problem was that I used @Deprecated annotation and someone turned treating compilation warnings as errors on. Due to the project using MapStruct and having pleeeeenty of mappers with the same compilation error, I focused on solving the problem with the annotations processors, completely ignoring those "insignificant" warnings.

Body:
I'm trying to create very simple JUnit test into Spring Cloud project:

@SpringBootTest(classes = {ProductMapper.class })
public class TestingWebApplicationTests {

    @Test
    public void contextLoads() {
    }

}

import org.mapstruct.Mapper;

@Mapper(config = BaseMapperConfig.class)
public interface ProductMapper {

    ProductDTO toDTO(Product product);

    ProductFullDTO toFullDTO(Product product);

    Product map(ProductFullDTO productDTO);

    ProductFilter toFilter(ProductFilterDTO dto);
}

I get this error when I try to run the test into the latest Intelij.

java: No implementation was created for ProductMapper due to having a problem in the erroneous element java.util.ArrayList. Hint: this often means that some other annotation processor was supposed to process the erroneous element. You can also enable MapStruct verbose mode by setting -Amapstruct.verbose=true as a compilation argument.

Do you know how I can fix this issue?

Body:
It's not exactly related to MapStruct itself, but maybe it will save some devs a few minutes / hours when searching for the problem.

I had a similar problem in a project, where I was supposed to add new API version and mark old one as deprecated.

When I tried compiling with maven, I got errors

[ERROR] /src/main/java/com/example/MapStructMapperClass.java: No implementation was created for MapStructMapperClass.java due to having a problem in the erroneous element java.util.ArrayList. Hint: this often means that some other annotation processor was supposed to process the erroneous element. You can also enable MapStruct verbose mode by setting -Amapstruct.verbose=true as a compilation argument.

The project uses Lombok, so I checked annotation processor configs. Looked fine and worked properly on a simple, ad hoc created project.
Tried bumping MapStruct and Lpmbok versions. Still the same problem.

Then, I noticed a few lines in the log:

[WARNING] COMPILATION WARNING :
[INFO] ------------------------------
[WARNING] /src/main/java/com/example/MyRestController.java: com.example.MyOldService in MyRestController has been deprecated
[INFO] 1 warning
[INFO] ----------------------
[ERROR] COMPILATION ERROR :
[INFO] ----------------------
[ERROR] warnings found and -Werror specified

And the config in maven pom.xml

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <failOnWarning>true</failOnWarning>
  <configuration>
</plugin>

As Javac JDK 8 states:

-Werror
    Terminate compilation if warnings occur.

So, my problem was that I used @Deprecated annotation and someone turned treating compilation warnings as errors on. Due to the project using MapStruct and having pleeeeenty of mappers with the same compilation error, I focused on solving the problem with the annotations processors, completely ignoring those "insignificant" warnings.


来源: stackexchange

问:
constrains on django model with ForeignKey
Suppose we have this django models:

```<code>from django.db import models

# Create your models here.
class Artist(models.Model):
    name = models.CharField(max_length=10)

class Album(models.Model):
    name = models.CharField(max_length=10)
    date = models.DateField()
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
</code>```

So I can write:

```<code>artist_one = models.Artist.objects.create(name='Santana')
album_one = models.Album.objects.create(name='Abraxas', date = datetime.date.today(), artist=artist_one)
album_two = models.Album.objects.create(name='Supernatural', date = datetime.date.today(), artist=artist_one)
</code>```

How can I add a constrain to the Album class as to say an artist cannot publish two albums the same year?

答:
Use validators on model</p>
```<code>from django.core.exceptions import ValidationError
def validate_atrist_album(value):
    #logic to check if artist has album from same year here
    if not user_can_publish:
        raise ValidationError('Artist can't publish')

class Album(models.Model):
    name = models.CharField(max_length=10)
    date = models.DateField()
    artist = models.ForeignKey(
        Artist,
        on_delete=models.CASCADE,
        validators=[
            validate_artist_album,
        ],
    )
</code>```

ContentLicense: CC BY-SA 4.0

Body:
Suppose we have this django models:

from django.db import models

# Create your models here.
class Artist(models.Model):
    name = models.CharField(max_length=10)

class Album(models.Model):
    name = models.CharField(max_length=10)
    date = models.DateField()
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE)

So I can write:

artist_one = models.Artist.objects.create(name='Santana')
album_one = models.Album.objects.create(name='Abraxas', date = datetime.date.today(), artist=artist_one)
album_two = models.Album.objects.create(name='Supernatural', date = datetime.date.today(), artist=artist_one)

How can I add a constrain to the Album class as to say an artist cannot publish two albums the same year?

Body:
Use validators on model

from django.core.exceptions import ValidationError
def validate_atrist_album(value):
    #logic to check if artist has album from same year here
    if not user_can_publish:
        raise ValidationError('Artist can't publish')

class Album(models.Model):
    name = models.CharField(max_length=10)
    date = models.DateField()
    artist = models.ForeignKey(
        Artist,
        on_delete=models.CASCADE,
        validators=[
            validate_artist_album,
        ],
    )


问:
ImportError: No module named gi.repository
I'm trying to launch python script on Ubuntu 10.04:

```from gi.repository import Nautilus, GObject```

It doesn't work:

```<code>Traceback (most recent call last):
  File "script.py", line 1, in <module>
    from gi.repository import Nautilus, GObject
   ImportError: No module named gi.repository
</code>```

I installed python-gobject-dev, python-gobject, python-nautilus, but it didn't help.
Has anyone had this problem?

答:
very simple.
open /usr/bin/gnome-terminal,you can see the interpreter is python3:

```<code>#!/usr/bin/python3
</code>```

Change the path of the interpreter to python3.5:

```<code>#!/usr/bin/python3.5
</code>```

Fixed it for me on Ubuntu 16.04.

Body:
I'm trying to launch python script on Ubuntu 10.04:

from gi.repository import Nautilus, GObject

It doesn't work:

Traceback (most recent call last):
  File "script.py", line 1, in <module>
    from gi.repository import Nautilus, GObject
   ImportError: No module named gi.repository

I installed python-gobject-dev, python-gobject, python-nautilus, but it didn't help.
Has anyone had this problem?

Body:
very simple.
open /usr/bin/gnome-terminal,you can see the interpreter is python3:

#!/usr/bin/python3

Change the path of the interpreter to python3.5:

#!/usr/bin/python3.5

Fixed it for me on Ubuntu 16.04.


问:
load style file using bundleName in angular
I have light and dark theme files and mentioned in angular.json file as below:

```<code>"styles": [
    "src/styles.scss",
    {
       "input": "src/styles/themes/light.theme.scss",
       "bundleName": "light-theme",
       "inject": false
    },
    {
       "input": "src/styles/themes/dark.theme.scss",
       "bundleName": "dark-theme",
       "inject": false
    }
],
</code>```

and I want to inject each of the file dynamically via this code

```<code>loadStyle(styleName: string = 'light-theme' | 'dark-theme') {
  const head = this.document.getElementsByTagName('head')[0];

  let themeLink = this.document.getElementById('client-theme') as HTMLLinkElement;

  if (themeLink) {
    themeLink.href = styleName;
  } else {
    const style = this.document.createElement('link');
    style.id = 'client-theme';
    style.rel = 'stylesheet';
    style.href = `${styleName}`;

    head.appendChild(style);
  }
}
</code>```

The above code creates link as

```<code><link id="client-theme" rel="stylesheet" href="dark-theme"> // href="light-theme"
</code>```

but nothing happens because the actual theme file is not being injected in the head-tag.

Update

Accoring to Angular Material docs

You can define multiple themes in separate files by creating multiple theme files per Defining a theme, adding each of the files to the styles of your angular.json. However, you must additionally set the inject option for each of these files to false in order to prevent all the theme files from being loaded at the same time. When setting this property to false, your application becomes responsible for manually loading the desired file. The approach for this loading depends on your application.
https://material.angular.io/guide/theming#multiple-themes-across-separate-files

but the process of loading styles files in not there in the docs :(

Any suggestion/solution would be highly appreciable!!! :)

答:
I don't think you can directly use the bundleName as href. Maybe try something like this:</p>
```<code>style.href = `styles/themes/${styleName}.css`;
</code>```

Body:
I have light and dark theme files and mentioned in angular.json file as below:

"styles": [
    "src/styles.scss",
    {
       "input": "src/styles/themes/light.theme.scss",
       "bundleName": "light-theme",
       "inject": false
    },
    {
       "input": "src/styles/themes/dark.theme.scss",
       "bundleName": "dark-theme",
       "inject": false
    }
],

and I want to inject each of the file dynamically via this code

loadStyle(styleName: string = 'light-theme' | 'dark-theme') {
  const head = this.document.getElementsByTagName('head')[0];

  let themeLink = this.document.getElementById('client-theme') as HTMLLinkElement;

  if (themeLink) {
    themeLink.href = styleName;
  } else {
    const style = this.document.createElement('link');
    style.id = 'client-theme';
    style.rel = 'stylesheet';
    style.href = `${styleName}`;

    head.appendChild(style);
  }
}

The above code creates link as

<link id="client-theme" rel="stylesheet" href="dark-theme"> // href="light-theme"

but nothing happens because the actual theme file is not being injected in the head-tag.

Update

Accoring to Angular Material docs

You can define multiple themes in separate files by creating multiple theme files per Defining a theme, adding each of the files to the styles of your angular.json. However, you must additionally set the inject option for each of these files to false in order to prevent all the theme files from being loaded at the same time. When setting this property to false, your application becomes responsible for manually loading the desired file. The approach for this loading depends on your application.
https://material.angular.io/guide/theming#multiple-themes-across-separate-files

but the process of loading styles files in not there in the docs :(

Any suggestion/solution would be highly appreciable!!! :)

Body:
I don't think you can directly use the bundleName as href. Maybe try something like this:

style.href = `styles/themes/${styleName}.css`;


问:
How to control transitions with multiple CSS class assignments from a single Javascript function?
I have one div, with buttons that trigger JS functions to add/remove CSS classes to the div. I understand this is a common way of doing simple animations ("transitions"). Try pressing the first two buttons, one after another. The red div will teleport up and become semi transparent, then fade in and slide back down. It always works.

The third button simply executes the same code as the first two buttons, but from one function. I expected it to have the same visible effect: the div immediately would jump up from the translate and be transparent, then during the course of 1 second it would slide back to its normal spot and fade in to full opacity. But it does not - the button has no visible effect.

Why? How can I make this work?

Here is the JS, see the whole thing at the codepen link.

```<code>const div = document.getElementById('red-box')

function translateUp() {
  div.classList.remove('no-translate');
  div.classList.add('translate-up');
}

function noTranslate() {
  div.classList.remove('translate-up');
  div.classList.add('no-translate');
}

//why does this function not show any transition animation?
function both() {
  translateUp();
  noTranslate();
}
</code>```

https://codepen.io/DMcCreepy/pen/BampPyB

No jQuery please :)

答:
The reason is that the functions are executed immediately one after the another, with no time to see the effect. transition in CSS refers to when the class is applied (could be related to a CSS state like :hover), not to switching classes via JavaScript.

To add the 1 second delay in the JS, you can use window.setTimeout:

```<code>function both() {
  translateUp();
  window.setTimeout(noTranslate, 1000);
}
</code>```

Body:
I have one div, with buttons that trigger JS functions to add/remove CSS classes to the div. I understand this is a common way of doing simple animations ("transitions"). Try pressing the first two buttons, one after another. The red div will teleport up and become semi transparent, then fade in and slide back down. It always works.

The third button simply executes the same code as the first two buttons, but from one function. I expected it to have the same visible effect: the div immediately would jump up from the translate and be transparent, then during the course of 1 second it would slide back to its normal spot and fade in to full opacity. But it does not - the button has no visible effect.

Why? How can I make this work?

Here is the JS, see the whole thing at the codepen link.

const div = document.getElementById('red-box')

function translateUp() {
  div.classList.remove('no-translate');
  div.classList.add('translate-up');
}

function noTranslate() {
  div.classList.remove('translate-up');
  div.classList.add('no-translate');
}

//why does this function not show any transition animation?
function both() {
  translateUp();
  noTranslate();
}

https://codepen.io/DMcCreepy/pen/BampPyB

No jQuery please :)

Body:
The reason is that the functions are executed immediately one after the another, with no time to see the effect. transition in CSS refers to when the class is applied (could be related to a CSS state like :hover), not to switching classes via JavaScript.

To add the 1 second delay in the JS, you can use window.setTimeout:

function both() {
  translateUp();
  window.setTimeout(noTranslate, 1000);
}


问:
Dynamically resize WinForms controls when Form is resized
I have a WinForms project on which I would like all of the controls to grow proportionally along with the form as the form is resized. This is what the form looks like in normal state: Normal State Form

I have tried setting the Anchor properties to their appropriate values given the location of each control on the form, and while it does move the controls, they remain the same size. I tried using the AutoSize property, but also to no avail. Here is what the form looks like after being maximized with the Anchor properties set: Maximized Form

I also tried using a formula from Shaun Halverson to dynamically resize everything but it does not relocate the control properly, and I can't seem to figure out why. Here is the code I used to try and resize dynamically:

```<code>        private void Main_Load(object sender, EventArgs e)
        {
            originalFormSize = new Rectangle(this.Location.X, this.Location.Y, this.Size.Width, this.Size.Height);
            submitBtnOriginal = new Rectangle(submitButton.Location.X, submitButton.Location.Y, submitButton.Width, submitButton.Height);
        }
        private void Main_Resize(object sender, EventArgs e)
        {
            resizeControl(submitBtnOriginal, submitButton);
        }

        private void resizeControl(Rectangle r, Control c)
        {
            float xRatio = (float)(this.Width) / (float)(originalFormSize.Width);
            float yRatio = (float)(this.Height) / (float)(originalFormSize.Height);

            int newWidth = (int)(r.Width * xRatio);
            int newHeight = (int)(r.Height * yRatio);

            int newX = (int)(r.Width * xRatio);
            int newY = (int)(r.Height * yRatio);

            c.Location = new Point(newX, newY);
            c.Size = new Size(newWidth, newHeight);
        }
</code>```

When I run this code, it moves the button to the opposite corner of the form, but it resizes it properly.

This would obviously be quite redundant given that I have to get an original size for every control I want to resize, but I would be fine with that if I could get dynamic resizing to work. I am surprised that this is not a more common problem, and I couldn't find hardly anything on this specific topic other than to use the Anchor and Dock properties. Is there an easy way to do this that I am missing? Is this a more difficult problem than it seems?

答: Put TextBox anchor property values as Top, Bottom, Left, Right and resize the form. That should work.

Body:
I have a WinForms project on which I would like all of the controls to grow proportionally along with the form as the form is resized. This is what the form looks like in normal state: Normal State Form

I have tried setting the Anchor properties to their appropriate values given the location of each control on the form, and while it does move the controls, they remain the same size. I tried using the AutoSize property, but also to no avail. Here is what the form looks like after being maximized with the Anchor properties set: Maximized Form

I also tried using a formula from Shaun Halverson to dynamically resize everything but it does not relocate the control properly, and I can't seem to figure out why. Here is the code I used to try and resize dynamically:

        private void Main_Load(object sender, EventArgs e)
        {
            originalFormSize = new Rectangle(this.Location.X, this.Location.Y, this.Size.Width, this.Size.Height);
            submitBtnOriginal = new Rectangle(submitButton.Location.X, submitButton.Location.Y, submitButton.Width, submitButton.Height);
        }
        private void Main_Resize(object sender, EventArgs e)
        {
            resizeControl(submitBtnOriginal, submitButton);
        }

        private void resizeControl(Rectangle r, Control c)
        {
            float xRatio = (float)(this.Width) / (float)(originalFormSize.Width);
            float yRatio = (float)(this.Height) / (float)(originalFormSize.Height);

            int newWidth = (int)(r.Width * xRatio);
            int newHeight = (int)(r.Height * yRatio);

            int newX = (int)(r.Width * xRatio);
            int newY = (int)(r.Height * yRatio);

            c.Location = new Point(newX, newY);
            c.Size = new Size(newWidth, newHeight);
        }

When I run this code, it moves the button to the opposite corner of the form, but it resizes it properly.

This would obviously be quite redundant given that I have to get an original size for every control I want to resize, but I would be fine with that if I could get dynamic resizing to work. I am surprised that this is not a more common problem, and I couldn't find hardly anything on this specific topic other than to use the Anchor and Dock properties. Is there an easy way to do this that I am missing? Is this a more difficult problem than it seems?


问:
Expected ',' or '...' before numeric const
I am new in C programming and trying to figure out some problems encountered.
When I wrote </p>

```<code>#define N 5

void Sort(int *const array,  int N);
</code>```

<p>the compiler gave me the message "Expected ',' or '...' before numeric const". After searching on the internet and found out that the problem may be casued by the #define being debugged as numeric values. I did get through the compiler by not using the #define for the constant N. However, I wondered how should I modified the above two lines so that I won't get that error message?  </p>

答:
You're using the preprocessor which runs on the raw text of the program before the compiler even sees it. So in this case all occurrences of N (when the N is by itself, not part of another word) get replaced by 5. By the time the program gets to the compiler, due to the preprocessor, all it sees is:

```<code>void Sort(int *const array,  int 5);
</code>```

Which is invalid syntax, and that's why you're getting the error.

C or C++

Either use a different name for the #define, or change your parameter name to any other valid identifier. The latter would look like this:

```<code>#define N 5

void Sort(int *const array,  int number);
</code>```

One other option would be to not use variable names in the function prototype:

```<code>#define N 5

void Sort(int *const, int);
</code>```

But then you still have the problem of naming it in the actual function definition.

Note that even just changing the case of the letter (from N to n) would fix it, but don't do that because it might look ambiguous to someone.

C++

If you are using C++, you shouldn't use #define (avoid it as much as you can, because they are bad in myriad ways). You should prefer a const int:

```<code>const int N = 5;
</code>```

You can use this just about anywhere you could use the other, plus it won't break when people try to use that name for themselves, and it won't cause multiple definitions like it would in C (thanks Jens). For example, your original code would have worked if you'd used a constant instead of #define:

```<code>const int N = 5;

void Sort(int *const array,  int N); // works because N is a real variable
</code>```

ContentLicense: CC BY-SA 3.0

Body:
I am new in C programming and trying to figure out some problems encountered.
When I wrote

#define N 5

void Sort(int *const array,  int N);

the compiler gave me the message "Expected ',' or '...' before numeric const". After searching on the internet and found out that the problem may be casued by the #define being debugged as numeric values. I did get through the compiler by not using the #define for the constant N. However, I wondered how should I modified the above two lines so that I won't get that error message?

Body:
You're using the preprocessor which runs on the raw text of the program before the compiler even sees it. So in this case all occurrences of N (when the N is by itself, not part of another word) get replaced by 5. By the time the program gets to the compiler, due to the preprocessor, all it sees is:

void Sort(int *const array,  int 5);

Which is invalid syntax, and that's why you're getting the error.

C or C++

Either use a different name for the #define, or change your parameter name to any other valid identifier. The latter would look like this:

#define N 5

void Sort(int *const array,  int number);

One other option would be to not use variable names in the function prototype:

#define N 5

void Sort(int *const, int);

But then you still have the problem of naming it in the actual function definition.

Note that even just changing the case of the letter (from N to n) would fix it, but don't do that because it might look ambiguous to someone.

C++

If you are using C++, you shouldn't use #define (avoid it as much as you can, because they are bad in myriad ways). You should prefer a const int:

const int N = 5;

You can use this just about anywhere you could use the other, plus it won't break when people try to use that name for themselves, and it won't cause multiple definitions like it would in C (thanks Jens). For example, your original code would have worked if you'd used a constant instead of #define:

const int N = 5;

void Sort(int *const array,  int N); // works because N is a real variable


问:
How to get the data from mongodb with all the struct variables?
In this code, I am trying to access the Cardname from the MongoDB database but it is giving me an empty string. Although it prints all the variables before Cardname but does not print the variables after Cardname and Cardname itself.

```<code>type UserCredentials struct {
    Fname      string
    Lname      string
    Email      string
    Password   string
    Phone      string
    Country    string
    State      string
    Faddress   string
    Laddress   string
    Postal     string
    Company    string
    Cardname   string
    Cardnumber string
    Expmonth   string
    Expyear    string
}

func FindCard(email, password string) {
    var uc UserCredentials
    collection := Connect.Database("eCommerce").Collection("register")
    if err := collection.FindOne(context.TODO(), bson.M{"email": email, "password": password}).Decode(&uc); err != nil {
        log.Fatal(err)
    }
    fmt.Println(uc.Cardname)
}
</code>```

答:
The mongo driver will not magically find out which document field you want to set into which struct field.

There are some "common sense" rules, such as field names matching properties (even if the first letter is not capitalized), but the field name Cardname will not be matched with the property name "card name".

You have to tell the mapping using struct tags, namely the bson struct tag (this is what the mongo-go driver uses).

For example:

```<code>type UserCredentials struct {
    Fname      string
    Lname      string
    Email      string
    Password   string
    Phone      string
    Country    string
    State      string
    Faddress   string
    Laddress   string
    Postal     string
    Company    string
    Cardname   string `bson:"card name"`
    Cardnumber string `bson:"card number"`
    Expmonth   string `bson:"expiry month"`
    Expyear    string `bson:"expiry year"`
}
</code>```

Body:
In this code, I am trying to access the Cardname from the MongoDB database but it is giving me an empty string. Although it prints all the variables before Cardname but does not print the variables after Cardname and Cardname itself.

type UserCredentials struct {
    Fname      string
    Lname      string
    Email      string
    Password   string
    Phone      string
    Country    string
    State      string
    Faddress   string
    Laddress   string
    Postal     string
    Company    string
    Cardname   string
    Cardnumber string
    Expmonth   string
    Expyear    string
}

func FindCard(email, password string) {
    var uc UserCredentials
    collection := Connect.Database("eCommerce").Collection("register")
    if err := collection.FindOne(context.TODO(), bson.M{"email": email, "password": password}).Decode(&uc); err != nil {
        log.Fatal(err)
    }
    fmt.Println(uc.Cardname)
}

Body:
The mongo driver will not magically find out which document field you want to set into which struct field.

There are some "common sense" rules, such as field names matching properties (even if the first letter is not capitalized), but the field name Cardname will not be matched with the property name "card name".

You have to tell the mapping using struct tags, namely the bson struct tag (this is what the mongo-go driver uses).

For example:

type UserCredentials struct {
    Fname      string
    Lname      string
    Email      string
    Password   string
    Phone      string
    Country    string
    State      string
    Faddress   string
    Laddress   string
    Postal     string
    Company    string
    Cardname   string `bson:"card name"`
    Cardnumber string `bson:"card number"`
    Expmonth   string `bson:"expiry month"`
    Expyear    string `bson:"expiry year"`
}


问:
How to get typeof generic model's parameter inside of another type?
I have one interface which takes type T:

```<code>export interface ValueTimestampModel<T> {
    value: T;
    timestamp: string;
}
</code>```

And I have a type which should use the above to transform a given model of type T by turning every parameter of T into ValueTimestampModel:

```<code>export type ModelWithTimestamp<T> = {
    [P in keyof T]?: ValueTimestampModel<typeof T[P]>
}
</code>```

So the desired result would be -> if we have model:

```<code>{
   street: string;
   streetNo: number;
}
</code>```

then ModelWithTimestamp version should be like:

```<code>{
   street: {
      value: string,
      timestamp: string
   },
   streetNo: {
      value: number,
      timestamp: string
   }
}
</code>```

Unfortunately the part typeof T[P] returns the compilation error:

```<code>'T' only refers to a type, but is being used as a value here.
</code>```

It seems typescript wont accept typeof in this context. Are there any alternatives to achieve what I'm looking for?

答:
You don't need typeof, T[P] is already the type of property P in type T:

```<code>export interface ValueTimestampModel<T> {
    value: T;
    timestamp: string;
}

export type ModelWithTimestamp<T> = {
    [P in keyof T]?: ValueTimestampModel<T[P]>
}

</code>```

Playground Link

Body:
I have one interface which takes type T:

export interface ValueTimestampModel<T> {
    value: T;
    timestamp: string;
}

And I have a type which should use the above to transform a given model of type T by turning every parameter of T into ValueTimestampModel:

export type ModelWithTimestamp<T> = {
    [P in keyof T]?: ValueTimestampModel<typeof T[P]>
}

So the desired result would be -> if we have model:

{
   street: string;
   streetNo: number;
}

then ModelWithTimestamp version should be like:

{
   street: {
      value: string,
      timestamp: string
   },
   streetNo: {
      value: number,
      timestamp: string
   }
}

Unfortunately the part typeof T[P] returns the compilation error:

'T' only refers to a type, but is being used as a value here.

It seems typescript wont accept typeof in this context. Are there any alternatives to achieve what I'm looking for?

Body:
You don't need typeof, T[P] is already the type of property P in type T:

export interface ValueTimestampModel<T> {
    value: T;
    timestamp: string;
}

export type ModelWithTimestamp<T> = {
    [P in keyof T]?: ValueTimestampModel<T[P]>
}

Playground Link


问:
Parsing CSV datafile with nested string Dictionary
I'm trying to use a large amount (20GB, approx 1000 files) of csv data from Telegram collected via Telethon over a couple weeks. I collected other data previously, but unfortunately, I completely messed up, used the wrong script and let my collection script run without attempting to parse the new output until it was complete. I've been banging my head on this for a couple days.

I need to fix the data so I can read it into a DataFrame.

Data structure from the CSV:

Index: integer

parent_msg_id: integer

message_dict : string dictionary

Example of the header and first line of the csv file.

```<code>,parent_msg_id,message_dict
0,2103,"{...}"
</code>```

message_dict is giving me the real problem. It is not valid JSON that I'm able to convert to dictionary (with json.loads or ast.literal_eval) easily due to a few issues.

Use of ' instead of " ('message' item often has both as punctuation, so a simple str.replace() can't be used on the entire string)

datetime wasn't converted to text format and was saved as datetime object (sometimes occurs in 'date' and 'edit_date' value)

datetime formatting inconsistent

'media' values can be very long and contain linebreak codes

An example below:

```<code>"{'_': 'Message',
'id': 29903,
'peer_id': {'_': 'PeerChannel', 'channel_id': 1333360824},
'date': datetime.datetime(2021, 12, 15, 2, 31, 58, tzinfo=datetime.timezone.utc),
'message': 'Let's all go here: https://twitter.com/i/status/1470455120513142792',
'out': False,
'mentioned': False,
'media_unread': False,
'silent': False,
'post': False,
'from_scheduled': False,
'legacy': False,
'edit_hide': False,
'pinned': False,
'from_id': {'_': 'PeerUser', 'user_id': 331122594},
'fwd_from': None,
'via_bot_id': None,
'reply_to': {'_': 'MessageReplyHeader', 'reply_to_msg_id': 29738, 'reply_to_peer_id': None, 'reply_to_top_id': None},
'media': {'_': 'MessageMediaWebPage', 'webpage': {'_': 'WebPage', 'id': 8720606894401618080, 'url': 'https://twitter.com/status/1470455120513142792', 'display_url': 'twitter.com/status/1470455120513142792', 'hash': 0, 'type': 'photo', 'site_name': 'Twitter', 'title': 'title', 'description': 'wordswordwords', 'photo': {'_': 'Photo', 'id': 5890060159542209872, 'access_hash': 6113394333987761493, 'file_reference': b'\x00a\xed\xd6|\x0e\x83\xbe(\xc0\xb9^u\x8a\x19\xb9\xe6\xe5\xbf\xd5v', 'date': datetime.datetime(2021, 12, 13, 18, 12, 41, tzinfo=datetime.timezone.utc), 'sizes': [{'_': 'PhotoStrippedSize', 'type': 'i', 'bytes': b'\x01\x17(g\x98\x80p\x94\xe8\xa4\x05r\xea?\x01T\xee\x18\xa1\n28\xcd6)\x88\x95\x0b\x1f\x978#\xda\x804%\xf2\xc8\x04\xc7\xd4qL\x08\x87<b\xadj\x11\x84\x81\n\x91\x81\xeb\xde\xa9\x15!2d\x07\xb6\x07n\xb4\xef\xa0%\xae\xa4\x8c""\'\x0b\x19\xa2\xaa\xa4\xa7p\xe4dQT\x996\x18F\xf3\x9e\xb9\xe2\x9c\xa8\xa3\xf8:\xf7\xa2\x8a\x83A\xd2\xcb+\x95\xcb\x16\xdb\xd0\x13Q\x05|\x92\x7f\x9d\x14P!6\xb6\xec\x9e\x87\xbd\x14Q@\x1f'}, {'_': 'PhotoSize', 'type': 'm', 'w': 320, 'h': 180, 'size': 13886}, {'_': 'PhotoSize', 'type': 'x', 'w': 800, 'h': 450, 'size': 48906}, {'_': 'PhotoSizeProgressive', 'type': 'y', 'w': 1200, 'h': 675, 'sizes': [10804, 24072, 30765, 43577, 73327]}], 'dc_id': 4, 'has_stickers': False, 'video_sizes': []}, 'embed_url': None, 'embed_type': None, 'embed_width': None, 'embed_height': None, 'duration': None, 'author': '@who', 'document': None, 'cached_page': None, 'attributes': []}},
'reply_markup': None,
'entities': [{'_': 'MessageEntityUrl', 'offset': 0, 'length': 48}],
'views': None,
'forwards': None,
'replies': None,
'edit_date': None,
'post_author': None,
'grouped_id': None,
'restriction_reason': [],
'ttl_period': None}"
</code>```

What I've tried:

I created a very... inventive hacker-ish attempt by throwing the kitchen sink at the problem. Using regex to separate out the message text, replace the other ' with ", regex again to identify datetime text, convert datetime object text to isoformat strings, and delete some of the troublesome metadata. The regex code is very slow, but it works. I would really appreciate some sort of feedback. At this rate, it might be faster to just start over.

```<code>rmsg = re.compile("'message': (.*?), 'out': ")
rdate = re.compile('"date": (.*?), "message": ')
rmedia = re.compile('"media": (.*?), "reply_markup": ')
reditdate = re.compile('"edit_date": (.*?), "post_author": ')

for filename in workingfiles:
    df = pd.read_csv(filename, index_col=0)
    if len(df) == 0:
        continue
    new_df = pd.DataFrame()
    for row in df.iterrows():
        #the actual dictionary that needs to be fixed
        messagedict = row[1]['message_dict']

        #return the message text and save it
        msg = rmsg.search(messagedict)
        if msg:
            messagetxt = msg.group(1)

        #split dictionary before and after message text
        splitmsg = messagedict.split(messagetxt,1)

        #replace ' with "
        part1 = splitmsg[0].replace("'", '"')
        part2 = splitmsg[1].replace("'", '"')
        #combine
        correctedmsg = part1 + messagetxt + part2

        #pull datetime string
        mdate = rdate.search(correctedmsg)
        if mdate:
            datetime_string = mdate.group(1)

        #convert datetime to isoformat
        datestrformat = 'datetime.datetime(%Y, %m, %d, %H, %M, %S, tzinfo=datetime.timezone.utc)'
        datestrformat2 = 'datetime.datetime(%Y, %m, %d, %H, %M, tzinfo=datetime.timezone.utc)'
        try:
            datetext = str(datetime.strptime(datetime_string,datestrformat).isoformat())
            #print(datetext)
        except Exception as e:
            #print (e)
            datetext = str(datetime.strptime(datetime_string,datestrformat2).isoformat())

        mmedia = rmedia.search(correctedmsg)
        if mmedia:
            media = mmedia.group(1)

        #replace media text with 'yes' to indicate it was a media message
        if media != 'None':
            part1 = correctedmsg.split(media,1)[0]
            part2 = correctedmsg.split(media,1)[1]
            correctedmsg = part1 + '"' + 'Yes' + '"' + part2

        newline = correctedmsg.split(datetime_string)[0] + '"' + datetext + '"' + correctedmsg.split(datetime_string)[1]

        meditdate = reditdate.search(correctedmsg)
        if meditdate:
            editdate = meditdate.group(1)
            replacedate = 'None'
        if editdate != 'None':
            part1 = newline.split(editdate,1)[0]
            part2 = newline.split(editdate,1)[1]
            newline = part1 + '"' + 'Yes' + '"' + part2

        linedict = ast.literal_eval(newline)

        new_df = new_df.append(linedict, ignore_index=True)

    df = pd.concat([df['parent_msg_id'],new_df], axis=1)

</code>```

答:
Let's write a parser !

```<code>import datetime  # seems unused, but actually required to `eval` the datetimes
import json
import string
from typing import Tuple, Union, Dict, Any, List

def find_next_single_quote(text: str, start_pos: int, allow_unescaped=False) -> Tuple[str, int]:
    assert text[start_pos] == "'"
    # find the next unescaped single-quote
    current_pos = start_pos + 1
    while True:
        end_pos = text.find("'", current_pos)
        if end_pos == -1:
            raise ValueError("could not find closing quote")
        # it may be an escaped quote
        count_of_backslashes_before = len(tuple(c for c in text[:end_pos - 1:-1] if c == "\\"))
        if count_of_backslashes_before % 2 == 0:
            # there is no backslash, or only backslashes themselves escaped (pairs)
            # but it can be an unescaped quote, so we try to detect for that
            if allow_unescaped and end_pos + 1 <= len(text) and text[end_pos + 1] not in ",}]":  # end of token
                current_pos = end_pos + 1
                continue
            else:
                return text[start_pos + 1:end_pos], end_pos + 1
        else:
            # false positive
            current_pos = end_pos + 1
            continue

def get_json_key(text: str, start_pos: int) -> Tuple[str, int]:
    return find_next_single_quote(text, start_pos)

def get_json_val(text: str, start_pos: int) -> Tuple[Union[str, float, Dict[str, Any], bool, None, bytes, List[Any]], int]:
    if text[start_pos] == "'":  # it's a string
        return find_next_single_quote(text, start_pos, allow_unescaped=True)
    elif text[start_pos] in string.digits:
        current_pos = start_pos + 1
        while text[current_pos] in string.digits:
            current_pos += 1
        return int(text[start_pos:current_pos]), current_pos
    elif text[start_pos] == "{":
        return read_dict(text, start_pos)
    elif text[start_pos:].startswith("datetime"):
        end_pos = text.find(")", start_pos + 1)
        result_datetime = eval(text[start_pos:end_pos + 1])  # quick but dirty
        return result_datetime, end_pos + 1
    elif text[start_pos:].startswith("False"):
        return False, start_pos + 5
    elif text[start_pos:].startswith("True"):
        return True, start_pos + 4
    elif text[start_pos:].startswith("None"):
        return None, start_pos + 4
    elif text[start_pos] == 'b':
        return find_next_single_quote(text, start_pos + 1, allow_unescaped=True)
    elif text[start_pos] == "[":
        return read_list(text, start_pos)

def seek_next_item(text: str, start_pos: int) -> int:
    assert text[start_pos] == ","
    assert text[start_pos + 1] == " "
    return start_pos + 2

def read_list(text: str, start_pos: int=0) -> Tuple[List[Any], int]:
    assert text[start_pos] == "["
    current_pos = start_pos + 1
    result = []
    first = True
    while text[current_pos] != "]":
        if first:
            first = False
        else:
            current_pos = seek_next_item(text, current_pos)
        item, current_pos = get_json_val(text, current_pos)
        result.append(item)
    return result, current_pos + 1

def seek_next_key(text: str, start_pos: int) -> int:
    assert text[start_pos] == ","
    assert text[start_pos + 1] == " "
    if text[start_pos + 2] == "\n":  # top-level dict
        assert text[start_pos + 3] == "'"
        return start_pos + 3
    else:
        assert text[start_pos + 2] == "'"
        return start_pos + 2

def read_dict(text: str, start_pos: int=0) -> Tuple[Dict[str, Any], int]:
    assert text[start_pos] == "{"
    current_pos = start_pos + 1
    result = {}
    first = True
    while text[current_pos] != "}":
        if first:
            first = False
        else:
            current_pos = seek_next_key(text, current_pos)
        key, current_pos = get_json_key(text, current_pos)
        # print("key", repr(key), current_pos)
        assert text[current_pos] == ":"
        assert text[current_pos + 1] == " "
        current_pos += 2
        # val, current_pos = get_json_val(text, current_pos)
        print("val", repr(val), current_pos)
        result[key] = val
    return result, current_pos + 1

full_result, _ = read_dict("""{'_': 'Message',
'id': 29903,
'peer_id': {'_': 'PeerChannel', 'channel_id': 1333360824},
'date': datetime.datetime(2021, 12, 15, 2, 31, 58, tzinfo=datetime.timezone.utc),
'message': 'Let's all go here: https://twitter.com/i/status/1470455120513142792',
'out': False,
'mentioned': False,
'media_unread': False,
'silent': False,
'post': False,
'from_scheduled': False,
'legacy': False,
'edit_hide': False,
'pinned': False,
'from_id': {'_': 'PeerUser', 'user_id': 331122594},
'fwd_from': None,
'via_bot_id': None,
'reply_to': {'_': 'MessageReplyHeader', 'reply_to_msg_id': 29738, 'reply_to_peer_id': None, 'reply_to_top_id': None},
'media': {'_': 'MessageMediaWebPage', 'webpage': {'_': 'WebPage', 'id': 8720606894401618080, 'url': 'https://twitter.com/status/1470455120513142792', 'display_url': 'twitter.com/status/1470455120513142792', 'hash': 0, 'type': 'photo', 'site_name': 'Twitter', 'title': 'title', 'description': 'wordswordwords', 'photo': {'_': 'Photo', 'id': 5890060159542209872, 'access_hash': 6113394333987761493, 'file_reference': b'\x00a\xed\xd6|\x0e\x83\xbe(\xc0\xb9^u\x8a\x19\xb9\xe6\xe5\xbf\xd5v', 'date': datetime.datetime(2021, 12, 13, 18, 12, 41, tzinfo=datetime.timezone.utc), 'sizes': [{'_': 'PhotoStrippedSize', 'type': 'i', 'bytes': b'\x01\x17(g\x98\x80p\x94\xe8\xa4\x05r\xea?\x01T\xee\x18\xa1\n28\xcd6)\x88\x95\x0b\x1f\x978#\xda\x804%\xf2\xc8\x04\xc7\xd4qL\x08\x87<b\xadj\x11\x84\x81\n\x91\x81\xeb\xde\xa9\x15!2d\x07\xb6\x07n\xb4\xef\xa0%\xae\xa4\x8c""\'\x0b\x19\xa2\xaa\xa4\xa7p\xe4dQT\x996\x18F\xf3\x9e\xb9\xe2\x9c\xa8\xa3\xf8:\xf7\xa2\x8a\x83A\xd2\xcb+\x95\xcb\x16\xdb\xd0\x13Q\x05|\x92\x7f\x9d\x14P!6\xb6\xec\x9e\x87\xbd\x14Q@\x1f'}, {'_': 'PhotoSize', 'type': 'm', 'w': 320, 'h': 180, 'size': 13886}, {'_': 'PhotoSize', 'type': 'x', 'w': 800, 'h': 450, 'size': 48906}, {'_': 'PhotoSizeProgressive', 'type': 'y', 'w': 1200, 'h': 675, 'sizes': [10804, 24072, 30765, 43577, 73327]}], 'dc_id': 4, 'has_stickers': False, 'video_sizes': []}, 'embed_url': None, 'embed_type': None, 'embed_width': None, 'embed_height': None, 'duration': None, 'author': '@who', 'document': None, 'cached_page': None, 'attributes': []}},
'reply_markup': None,
'entities': [{'_': 'MessageEntityUrl', 'offset': 0, 'length': 48}],
'views': None,
'forwards': None,
'replies': None,
'edit_date': None,
'post_author': None,
'grouped_id': None,
'restriction_reason': [],
'ttl_period': None}""")
print(
    json.dumps({
        "message": full_result["message"],
        "date": full_result["date"],
        "media": full_result["media"],
        "edit_date": full_result["edit_date"]
    }, indent=4, sort_keys=True, default=str)
)
</code>```

{
    "date": "2021-12-15 02:31:58+00:00",
    "edit_date": null,
    "media": {
        "_": "MessageMediaWebPage",
        ...
    },
    "message": "Let's all go here: https://twitter.com/i/status/1470455120513142792"
}
```

Body:
I'm trying to use a large amount (20GB, approx 1000 files) of csv data from Telegram collected via Telethon over a couple weeks. I collected other data previously, but unfortunately, I completely messed up, used the wrong script and let my collection script run without attempting to parse the new output until it was complete. I've been banging my head on this for a couple days.

I need to fix the data so I can read it into a DataFrame.

Data structure from the CSV:

Index: integer

parent_msg_id: integer

message_dict : string dictionary

Example of the header and first line of the csv file.

,parent_msg_id,message_dict
0,2103,"{...}"

message_dict is giving me the real problem. It is not valid JSON that I'm able to convert to dictionary (with json.loads or ast.literal_eval) easily due to a few issues.

Use of ' instead of " ('message' item often has both as punctuation, so a simple str.replace() can't be used on the entire string)

datetime wasn't converted to text format and was saved as datetime object (sometimes occurs in 'date' and 'edit_date' value)

datetime formatting inconsistent

'media' values can be very long and contain linebreak codes

An example below:

"{'_': 'Message',
'id': 29903,
'peer_id': {'_': 'PeerChannel', 'channel_id': 1333360824},
'date': datetime.datetime(2021, 12, 15, 2, 31, 58, tzinfo=datetime.timezone.utc),
'message': 'Let's all go here: https://twitter.com/i/status/1470455120513142792',
'out': False,
'mentioned': False,
'media_unread': False,
'silent': False,
'post': False,
'from_scheduled': False,
'legacy': False,
'edit_hide': False,
'pinned': False,
'from_id': {'_': 'PeerUser', 'user_id': 331122594},
'fwd_from': None,
'via_bot_id': None,
'reply_to': {'_': 'MessageReplyHeader', 'reply_to_msg_id': 29738, 'reply_to_peer_id': None, 'reply_to_top_id': None},
'media': {'_': 'MessageMediaWebPage', 'webpage': {'_': 'WebPage', 'id': 8720606894401618080, 'url': 'https://twitter.com/status/1470455120513142792', 'display_url': 'twitter.com/status/1470455120513142792', 'hash': 0, 'type': 'photo', 'site_name': 'Twitter', 'title': 'title', 'description': 'wordswordwords', 'photo': {'_': 'Photo', 'id': 5890060159542209872, 'access_hash': 6113394333987761493, 'file_reference': b'\x00a\xed\xd6|\x0e\x83\xbe(\xc0\xb9^u\x8a\x19\xb9\xe6\xe5\xbf\xd5v', 'date': datetime.datetime(2021, 12, 13, 18, 12, 41, tzinfo=datetime.timezone.utc), 'sizes': [{'_': 'PhotoStrippedSize', 'type': 'i', 'bytes': b'\x01\x17(g\x98\x80p\x94\xe8\xa4\x05r\xea?\x01T\xee\x18\xa1\n28\xcd6)\x88\x95\x0b\x1f\x978#\xda\x804%\xf2\xc8\x04\xc7\xd4qL\x08\x87<b\xadj\x11\x84\x81\n\x91\x81\xeb\xde\xa9\x15!2d\x07\xb6\x07n\xb4\xef\xa0%\xae\xa4\x8c""\'\x0b\x19\xa2\xaa\xa4\xa7p\xe4dQT\x996\x18F\xf3\x9e\xb9\xe2\x9c\xa8\xa3\xf8:\xf7\xa2\x8a\x83A\xd2\xcb+\x95\xcb\x16\xdb\xd0\x13Q\x05|\x92\x7f\x9d\x14P!6\xb6\xec\x9e\x87\xbd\x14Q@\x1f'}, {'_': 'PhotoSize', 'type': 'm', 'w': 320, 'h': 180, 'size': 13886}, {'_': 'PhotoSize', 'type': 'x', 'w': 800, 'h': 450, 'size': 48906}, {'_': 'PhotoSizeProgressive', 'type': 'y', 'w': 1200, 'h': 675, 'sizes': [10804, 24072, 30765, 43577, 73327]}], 'dc_id': 4, 'has_stickers': False, 'video_sizes': []}, 'embed_url': None, 'embed_type': None, 'embed_width': None, 'embed_height': None, 'duration': None, 'author': '@who', 'document': None, 'cached_page': None, 'attributes': []}},
'reply_markup': None,
'entities': [{'_': 'MessageEntityUrl', 'offset': 0, 'length': 48}],
'views': None,
'forwards': None,
'replies': None,
'edit_date': None,
'post_author': None,
'grouped_id': None,
'restriction_reason': [],
'ttl_period': None}"

What I've tried:

I created a very... inventive hacker-ish attempt by throwing the kitchen sink at the problem. Using regex to separate out the message text, replace the other ' with ", regex again to identify datetime text, convert datetime object text to isoformat strings, and delete some of the troublesome metadata. The regex code is very slow, but it works. I would really appreciate some sort of feedback. At this rate, it might be faster to just start over.

rmsg = re.compile("'message': (.*?), 'out': ")
rdate = re.compile('"date": (.*?), "message": ')
rmedia = re.compile('"media": (.*?), "reply_markup": ')
reditdate = re.compile('"edit_date": (.*?), "post_author": ')

for filename in workingfiles:
    df = pd.read_csv(filename, index_col=0)
    if len(df) == 0:
        continue
    new_df = pd.DataFrame()
    for row in df.iterrows():
        #the actual dictionary that needs to be fixed
        messagedict = row[1]['message_dict']

        #return the message text and save it
        msg = rmsg.search(messagedict)
        if msg:
            messagetxt = msg.group(1)

        #split dictionary before and after message text
        splitmsg = messagedict.split(messagetxt,1)

        #replace ' with "
        part1 = splitmsg[0].replace("'", '"')
        part2 = splitmsg[1].replace("'", '"')
        #combine
        correctedmsg = part1 + messagetxt + part2

        #pull datetime string
        mdate = rdate.search(correctedmsg)
        if mdate:
            datetime_string = mdate.group(1)

        #convert datetime to isoformat
        datestrformat = 'datetime.datetime(%Y, %m, %d, %H, %M, %S, tzinfo=datetime.timezone.utc)'
        datestrformat2 = 'datetime.datetime(%Y, %m, %d, %H, %M, tzinfo=datetime.timezone.utc)'
        try:
            datetext = str(datetime.strptime(datetime_string,datestrformat).isoformat())
            #print(datetext)
        except Exception as e:
            #print (e)
            datetext = str(datetime.strptime(datetime_string,datestrformat2).isoformat())

        mmedia = rmedia.search(correctedmsg)
        if mmedia:
            media = mmedia.group(1)

        #replace media text with 'yes' to indicate it was a media message
        if media != 'None':
            part1 = correctedmsg.split(media,1)[0]
            part2 = correctedmsg.split(media,1)[1]
            correctedmsg = part1 + '"' + 'Yes' + '"' + part2

        newline = correctedmsg.split(datetime_string)[0] + '"' + datetext + '"' + correctedmsg.split(datetime_string)[1]

        meditdate = reditdate.search(correctedmsg)
        if meditdate:
            editdate = meditdate.group(1)
            replacedate = 'None'
        if editdate != 'None':
            part1 = newline.split(editdate,1)[0]
            part2 = newline.split(editdate,1)[1]
            newline = part1 + '"' + 'Yes' + '"' + part2

        linedict = ast.literal_eval(newline)

        new_df = new_df.append(linedict, ignore_index=True)

    df = pd.concat([df['parent_msg_id'],new_df], axis=1)

Body:
Let's write a parser !

import datetime  # seems unused, but actually required to `eval` the datetimes
import json
import string
from typing import Tuple, Union, Dict, Any, List

def find_next_single_quote(text: str, start_pos: int, allow_unescaped=False) -> Tuple[str, int]:
    assert text[start_pos] == "'"
    # find the next unescaped single-quote
    current_pos = start_pos + 1
    while True:
        end_pos = text.find("'", current_pos)
        if end_pos == -1:
            raise ValueError("could not find closing quote")
        # it may be an escaped quote
        count_of_backslashes_before = len(tuple(c for c in text[:end_pos - 1:-1] if c == "\\"))
        if count_of_backslashes_before % 2 == 0:
            # there is no backslash, or only backslashes themselves escaped (pairs)
            # but it can be an unescaped quote, so we try to detect for that
            if allow_unescaped and end_pos + 1 <= len(text) and text[end_pos + 1] not in ",}]":  # end of token
                current_pos = end_pos + 1
                continue
            else:
                return text[start_pos + 1:end_pos], end_pos + 1
        else:
            # false positive
            current_pos = end_pos + 1
            continue

def get_json_key(text: str, start_pos: int) -> Tuple[str, int]:
    return find_next_single_quote(text, start_pos)

def get_json_val(text: str, start_pos: int) -> Tuple[Union[str, float, Dict[str, Any], bool, None, bytes, List[Any]], int]:
    if text[start_pos] == "'":  # it's a string
        return find_next_single_quote(text, start_pos, allow_unescaped=True)
    elif text[start_pos] in string.digits:
        current_pos = start_pos + 1
        while text[current_pos] in string.digits:
            current_pos += 1
        return int(text[start_pos:current_pos]), current_pos
    elif text[start_pos] == "{":
        return read_dict(text, start_pos)
    elif text[start_pos:].startswith("datetime"):
        end_pos = text.find(")", start_pos + 1)
        result_datetime = eval(text[start_pos:end_pos + 1])  # quick but dirty
        return result_datetime, end_pos + 1
    elif text[start_pos:].startswith("False"):
        return False, start_pos + 5
    elif text[start_pos:].startswith("True"):
        return True, start_pos + 4
    elif text[start_pos:].startswith("None"):
        return None, start_pos + 4
    elif text[start_pos] == 'b':
        return find_next_single_quote(text, start_pos + 1, allow_unescaped=True)
    elif text[start_pos] == "[":
        return read_list(text, start_pos)

def seek_next_item(text: str, start_pos: int) -> int:
    assert text[start_pos] == ","
    assert text[start_pos + 1] == " "
    return start_pos + 2

def read_list(text: str, start_pos: int=0) -> Tuple[List[Any], int]:
    assert text[start_pos] == "["
    current_pos = start_pos + 1
    result = []
    first = True
    while text[current_pos] != "]":
        if first:
            first = False
        else:
            current_pos = seek_next_item(text, current_pos)
        item, current_pos = get_json_val(text, current_pos)
        result.append(item)
    return result, current_pos + 1

def seek_next_key(text: str, start_pos: int) -> int:
    assert text[start_pos] == ","
    assert text[start_pos + 1] == " "
    if text[start_pos + 2] == "\n":  # top-level dict
        assert text[start_pos + 3] == "'"
        return start_pos + 3
    else:
        assert text[start_pos + 2] == "'"
        return start_pos + 2

def read_dict(text: str, start_pos: int=0) -> Tuple[Dict[str, Any], int]:
    assert text[start_pos] == "{"
    current_pos = start_pos + 1
    result = {}
    first = True
    while text[current_pos] != "}":
        if first:
            first = False
        else:
            current_pos = seek_next_key(text, current_pos)
        key, current_pos = get_json_key(text, current_pos)
        # print("key", repr(key), current_pos)
        assert text[current_pos] == ":"
        assert text[current_pos + 1] == " "
        current_pos += 2
        # val, current_pos = get_json_val(text, current_pos)
        print("val", repr(val), current_pos)
        result[key] = val
    return result, current_pos + 1

full_result, _ = read_dict("""{'_': 'Message',
'id': 29903,
'peer_id': {'_': 'PeerChannel', 'channel_id': 1333360824},
'date': datetime.datetime(2021, 12, 15, 2, 31, 58, tzinfo=datetime.timezone.utc),
'message': 'Let's all go here: https://twitter.com/i/status/1470455120513142792',
'out': False,
'mentioned': False,
'media_unread': False,
'silent': False,
'post': False,
'from_scheduled': False,
'legacy': False,
'edit_hide': False,
'pinned': False,
'from_id': {'_': 'PeerUser', 'user_id': 331122594},
'fwd_from': None,
'via_bot_id': None,
'reply_to': {'_': 'MessageReplyHeader', 'reply_to_msg_id': 29738, 'reply_to_peer_id': None, 'reply_to_top_id': None},
'media': {'_': 'MessageMediaWebPage', 'webpage': {'_': 'WebPage', 'id': 8720606894401618080, 'url': 'https://twitter.com/status/1470455120513142792', 'display_url': 'twitter.com/status/1470455120513142792', 'hash': 0, 'type': 'photo', 'site_name': 'Twitter', 'title': 'title', 'description': 'wordswordwords', 'photo': {'_': 'Photo', 'id': 5890060159542209872, 'access_hash': 6113394333987761493, 'file_reference': b'\x00a\xed\xd6|\x0e\x83\xbe(\xc0\xb9^u\x8a\x19\xb9\xe6\xe5\xbf\xd5v', 'date': datetime.datetime(2021, 12, 13, 18, 12, 41, tzinfo=datetime.timezone.utc), 'sizes': [{'_': 'PhotoStrippedSize', 'type': 'i', 'bytes': b'\x01\x17(g\x98\x80p\x94\xe8\xa4\x05r\xea?\x01T\xee\x18\xa1\n28\xcd6)\x88\x95\x0b\x1f\x978#\xda\x804%\xf2\xc8\x04\xc7\xd4qL\x08\x87<b\xadj\x11\x84\x81\n\x91\x81\xeb\xde\xa9\x15!2d\x07\xb6\x07n\xb4\xef\xa0%\xae\xa4\x8c""\'\x0b\x19\xa2\xaa\xa4\xa7p\xe4dQT\x996\x18F\xf3\x9e\xb9\xe2\x9c\xa8\xa3\xf8:\xf7\xa2\x8a\x83A\xd2\xcb+\x95\xcb\x16\xdb\xd0\x13Q\x05|\x92\x7f\x9d\x14P!6\xb6\xec\x9e\x87\xbd\x14Q@\x1f'}, {'_': 'PhotoSize', 'type': 'm', 'w': 320, 'h': 180, 'size': 13886}, {'_': 'PhotoSize', 'type': 'x', 'w': 800, 'h': 450, 'size': 48906}, {'_': 'PhotoSizeProgressive', 'type': 'y', 'w': 1200, 'h': 675, 'sizes': [10804, 24072, 30765, 43577, 73327]}], 'dc_id': 4, 'has_stickers': False, 'video_sizes': []}, 'embed_url': None, 'embed_type': None, 'embed_width': None, 'embed_height': None, 'duration': None, 'author': '@who', 'document': None, 'cached_page': None, 'attributes': []}},
'reply_markup': None,
'entities': [{'_': 'MessageEntityUrl', 'offset': 0, 'length': 48}],
'views': None,
'forwards': None,
'replies': None,
'edit_date': None,
'post_author': None,
'grouped_id': None,
'restriction_reason': [],
'ttl_period': None}""")
print(
    json.dumps({
        "message": full_result["message"],
        "date": full_result["date"],
        "media": full_result["media"],
        "edit_date": full_result["edit_date"]
    }, indent=4, sort_keys=True, default=str)
)

{
    "date": "2021-12-15 02:31:58+00:00",
    "edit_date": null,
    "media": {
        "_": "MessageMediaWebPage",
        ...
    },
    "message": "Let's all go here: https://twitter.com/i/status/1470455120513142792"
}


问:
Can we add ephemeral-storage in LimitRange?
in LimitRange in k8s we can simply limit the ram and the cpu and can we do that for ephemeral-storage as wella?</p>

答:
To set default requests and limits on ephemeral storage for each container in  mytest namespace:

```<code>apiVersion: v1
kind: LimitRange
metadata:
  name: storage-limit
  namespace: mytest
spec:
  limits:
  - default:
      ephemeral-storage: 2Gi
    defaultRequest:
      ephemeral-storage: 1Gi
    type: Container
</code>```

To change scope to Pod simply change to type: Pod

Body:
To set default requests and limits on ephemeral storage for each container in  mytest namespace:

apiVersion: v1
kind: LimitRange
metadata:
  name: storage-limit
  namespace: mytest
spec:
  limits:
  - default:
      ephemeral-storage: 2Gi
    defaultRequest:
      ephemeral-storage: 1Gi
    type: Container

To change scope to Pod simply change to type: Pod


问:
Can I make an Azure storage account go offline to test my fallback code?
I am developing an Azure application (C#, .NET 6, ASP.NET Core) that uses Azure blob storage as well as table storage.

I have geo-redundancy enabled on my storage account (RA_GRS) so that if my main storage account goes down, a read-only copy will be available in another Azure region.

When reading from blob storage, as far as I understand, I should be able to get it to automatically fall back to the secondary address by setting the GeoRedundantSecondaryUri property like this:

    return new BlobServiceClient(
        new Uri($"https://{accountName}.blob.core.windows.net/"),
        sharedKeyCredential,
        new BlobClientOptions
        {
            GeoRedundantSecondaryUri = new Uri($"https://{accountName}-secondary.blob.core.windows.net/")
        });
```

How can I end-to-end test that I am doing this correctly?

Can I tell the storage account to go offline so that my application should fall back to the read-only backup? I know I can stop an app service using az webapp stop from the command line. Can I do anything similar for storage accounts? Otherwise, how can I test my fallback logic?

(I am asking not only because I want to test the built-in geo-redundancy functionality. I have other failover-related code I want to test.)

EDIT 1: I do not want to use Azure's built-in "initiate account failover" functionality. At least not at this time. I want to test that my application can continue in a kind of "read-only mode" while the primary storage is down.

EDIT 2-3: I have tried to block the primary address ({accountName}.blob.core.windows.net/) using C:\Windows\System32\drivers\etc\hosts. My blob client does not seem to fall back.

If I redirect to IP 0.0.0.0, the blob client throws this: "The requested name is valid, but no data of the requested type was found."

If I redirect to IP 127.0.0.1, the blob client throws this: "No connection could be made because the target machine actively refused it."

答:
If you just want to test the geo-redundancy of your Storage Account, you can initiate an account failover manually from Azure portal & see if your application resumes back the functionality via secondary endpoints.

This documentation might help you out in this scenario: Initiate Azure Storage Account Failover

Note that there will be a certain minimal amount of data loss if you perform a failover, so I'd recommend you to go through the implications of a failover before performing it.

Body:
I am developing an Azure application (C#, .NET 6, ASP.NET Core) that uses Azure blob storage as well as table storage.

I have geo-redundancy enabled on my storage account (RA_GRS) so that if my main storage account goes down, a read-only copy will be available in another Azure region.

When reading from blob storage, as far as I understand, I should be able to get it to automatically fall back to the secondary address by setting the GeoRedundantSecondaryUri property like this:

    return new BlobServiceClient(
        new Uri($"https://{accountName}.blob.core.windows.net/"),
        sharedKeyCredential,
        new BlobClientOptions
        {
            GeoRedundantSecondaryUri = new Uri($"https://{accountName}-secondary.blob.core.windows.net/")
        });

How can I end-to-end test that I am doing this correctly?

Can I tell the storage account to go offline so that my application should fall back to the read-only backup? I know I can stop an app service using az webapp stop from the command line. Can I do anything similar for storage accounts? Otherwise, how can I test my fallback logic?

(I am asking not only because I want to test the built-in geo-redundancy functionality. I have other failover-related code I want to test.)

EDIT 1: I do not want to use Azure's built-in "initiate account failover" functionality. At least not at this time. I want to test that my application can continue in a kind of "read-only mode" while the primary storage is down.

EDIT 2-3: I have tried to block the primary address ({accountName}.blob.core.windows.net/) using C:\Windows\System32\drivers\etc\hosts. My blob client does not seem to fall back.

If I redirect to IP 0.0.0.0, the blob client throws this: "The requested name is valid, but no data of the requested type was found."

If I redirect to IP 127.0.0.1, the blob client throws this: "No connection could be made because the target machine actively refused it."


问:
Infinite recursive call minimax algorithm
I have recently implemented the code for a 4X4 tic tac toe game which is using minimax algorithm.
However, my minimax function is calling itself recursively infinite no. of times.

Initial Board (4X4) tic tac toe ->

```<code>board = np.array([['_','_','_', '_'],['_','_','_', '_'],['_','_','_', '_'],['_','_','_', '_']])
</code>```

Code for the computer's turn ->

```<code>import math
from utility import *

def minimax(board, spotsLeft, maximizing):
    bestIdx = (0,0)
    p1 = 'X'
    p2 = 'O'
    res, stat = checkGameOver(board, spotsLeft)
    if res==True:
        if stat == 'X': return 17-spotsLeft, bestIdx
        elif stat == 'O': return -17+spotsLeft, bestIdx
        else: return 0, bestIdx

    if maximizing:
        # return max score
        bestMove = -math.inf
        for i in range(4):
            for j in range(4):
                if board[i][j] == '_':
                    board[i][j] = p1
                    score, idx = minimax(board, spotsLeft-1, False)
                    print(score, idx)
                    board[i][j] = '_'
                    if bestMove<score:
                        bestMove = score
                        bestIdx = (i,j)
        return bestMove, bestIdx
    else:
        # return min score
        bestMove = math.inf
        for i in range(4):
            for j in range(4):
                if board[i][j] == '_':
                    board[i][j] = p2
                    score, idx = minimax(board, spotsLeft-1, True)
                    print(score, idx)
                    board[i][j] = '_'
                    if bestMove>score:
                        bestMove = score
                        bestIdx = (i,j)
        return bestMove, bestIdx

def ai(board):
    spotsLeft = 16
    p1 = 'X'    # Computer
    p2 = 'O'    # Player
    turn = p1
    while True:
        res, stat = checkGameOver(board, spotsLeft)
        if res==False:
            if turn == p1:
                # AI
                boardCopy = board[:]
                score, idx = minimax(boardCopy, spotsLeft, True)
                board[idx[0]][idx[1]] = turn
                turn = p2
                spotsLeft-=1
            else:
                # Human
                inp = int(input(f"Your turn: "))
                i, j = spotToIdx(inp)
                if board[i][j]=='_':
                    board[i][j] = turn
                    turn = p1
                    spotsLeft-=1
        else: return stat
        printBoard(board)
</code>```

In the above code
spotsLeft is the empty places on board,
checkGameOver returns "X" (if Player X wins), returns "O" (if Player O wins) & returns "T" (if game is tied)

checkGameOver function ->

```<code>def checkWin(board):
    # Check Row
    for i in board:
        if len(set(i)) == 1 and i[0]!='_':
            return i[0]

    # Check Column
    for i in range(4):
        if (board[0][i] == board[1][i] == board[2][i] == board[3][i]) and board[0][i]!='_':
            return board[0][i]

    # Check Diagonals
    if (board[0][0]==board[1][1]==board[2][2]==board[3][3]) and board[0][0]!='_':
            return board[0][0]
    if (board[0][3]==board[1][2]==board[2][1]==board[3][0]) and board[0][3]!='_':
        return board[0][3]

    # No One Won Yet
    return -1

def checkGameOver(board, spotsLeft):
    # t -> Game Tied
    # x -> Player X won
    # o -> Player O won

    # if tied - all spots filled
    if spotsLeft == 0:
        return True, 'T'

    # if any player won the game
    result = checkWin(board)
    if result!=-1:
        return True, result

    return False, -1
</code>```

答:
I think your code is fine, and doing what you want it to do. The issue is most likely due to the complexity of the problem, and for a lower dimension tic tac toe it would work fine.

Let's first simplify and look at a 2x2 case. For the first turn, you call minimax from the ai function, which will at the first level call itself 4 times. At the next level, each one of those calls will also call minimax, but one time less than at the previous level. To put it as a list:

level 0 (ai function): 1 call to minimax

level 1: 4 calls

level 2: 4x3 calls (each of the 4 calls above make 3 new calls)

level 3: 4x3x2 calls

level 4: 4x3x2x1 calls

Now using n-factorial notation as n!, we can compute the total number of calls as:

```<code>4!/4! + 4!/(4-1)! + 4!/(4-2)! + 4!/(4-3)! + 4!/(4-4!)
</code>```

Which is the sum of n!/(n-k)! for k between 0 and n (included), n begin the number of cells on your board. The result here is 65 calls to minimax for a 2x2 board.

Put into a python code:

def factorial(n):
    if n > 1: return n*factorial(n-1)
    else: return 1

def tictactoeComplexity(gridsize):
    return sum([factorial(gridsize)/factorial(gridsize-k) for k in range(gridsize+1)])

print(tictactoeComplexity(2*2)) # result is 65
```
<p>Now let's check for a 3*3 board:</p>
<pre class="lang-py prettyprint-override"><code>print(tictactoeComplexity(3*3)) # result is 986,410
</code>```

Just going from 2x2 to 3x3, we go from around 100 to around 1,000,000. You can guess the result for a 4x4 board:

print(tictactoeComplexity(4*4)) # result is 56,874,039,553,217
```

So your program does what you are asking it to do, but you're asking quite a lot.

Body:
I have recently implemented the code for a 4X4 tic tac toe game which is using minimax algorithm.
However, my minimax function is calling itself recursively infinite no. of times.

Initial Board (4X4) tic tac toe ->

board = np.array([['_','_','_', '_'],['_','_','_', '_'],['_','_','_', '_'],['_','_','_', '_']])

Code for the computer's turn ->

import math
from utility import *

def minimax(board, spotsLeft, maximizing):
    bestIdx = (0,0)
    p1 = 'X'
    p2 = 'O'
    res, stat = checkGameOver(board, spotsLeft)
    if res==True:
        if stat == 'X': return 17-spotsLeft, bestIdx
        elif stat == 'O': return -17+spotsLeft, bestIdx
        else: return 0, bestIdx

    if maximizing:
        # return max score
        bestMove = -math.inf
        for i in range(4):
            for j in range(4):
                if board[i][j] == '_':
                    board[i][j] = p1
                    score, idx = minimax(board, spotsLeft-1, False)
                    print(score, idx)
                    board[i][j] = '_'
                    if bestMove<score:
                        bestMove = score
                        bestIdx = (i,j)
        return bestMove, bestIdx
    else:
        # return min score
        bestMove = math.inf
        for i in range(4):
            for j in range(4):
                if board[i][j] == '_':
                    board[i][j] = p2
                    score, idx = minimax(board, spotsLeft-1, True)
                    print(score, idx)
                    board[i][j] = '_'
                    if bestMove>score:
                        bestMove = score
                        bestIdx = (i,j)
        return bestMove, bestIdx

def ai(board):
    spotsLeft = 16
    p1 = 'X'    # Computer
    p2 = 'O'    # Player
    turn = p1
    while True:
        res, stat = checkGameOver(board, spotsLeft)
        if res==False:
            if turn == p1:
                # AI
                boardCopy = board[:]
                score, idx = minimax(boardCopy, spotsLeft, True)
                board[idx[0]][idx[1]] = turn
                turn = p2
                spotsLeft-=1
            else:
                # Human
                inp = int(input(f"Your turn: "))
                i, j = spotToIdx(inp)
                if board[i][j]=='_':
                    board[i][j] = turn
                    turn = p1
                    spotsLeft-=1
        else: return stat
        printBoard(board)

In the above code
spotsLeft is the empty places on board,
checkGameOver returns "X" (if Player X wins), returns "O" (if Player O wins) & returns "T" (if game is tied)

checkGameOver function ->

def checkWin(board):
    # Check Row
    for i in board:
        if len(set(i)) == 1 and i[0]!='_':
            return i[0]

    # Check Column
    for i in range(4):
        if (board[0][i] == board[1][i] == board[2][i] == board[3][i]) and board[0][i]!='_':
            return board[0][i]

    # Check Diagonals
    if (board[0][0]==board[1][1]==board[2][2]==board[3][3]) and board[0][0]!='_':
            return board[0][0]
    if (board[0][3]==board[1][2]==board[2][1]==board[3][0]) and board[0][3]!='_':
        return board[0][3]

    # No One Won Yet
    return -1

def checkGameOver(board, spotsLeft):
    # t -> Game Tied
    # x -> Player X won
    # o -> Player O won

    # if tied - all spots filled
    if spotsLeft == 0:
        return True, 'T'

    # if any player won the game
    result = checkWin(board)
    if result!=-1:
        return True, result

    return False, -1

Body:
I think your code is fine, and doing what you want it to do. The issue is most likely due to the complexity of the problem, and for a lower dimension tic tac toe it would work fine.

Let's first simplify and look at a 2x2 case. For the first turn, you call minimax from the ai function, which will at the first level call itself 4 times. At the next level, each one of those calls will also call minimax, but one time less than at the previous level. To put it as a list:

level 0 (ai function): 1 call to minimax

level 1: 4 calls

level 2: 4x3 calls (each of the 4 calls above make 3 new calls)

level 3: 4x3x2 calls

level 4: 4x3x2x1 calls

Now using n-factorial notation as n!, we can compute the total number of calls as:

4!/4! + 4!/(4-1)! + 4!/(4-2)! + 4!/(4-3)! + 4!/(4-4!)

Which is the sum of n!/(n-k)! for k between 0 and n (included), n begin the number of cells on your board. The result here is 65 calls to minimax for a 2x2 board.

Put into a python code:

def factorial(n):
    if n > 1: return n*factorial(n-1)
    else: return 1

def tictactoeComplexity(gridsize):
    return sum([factorial(gridsize)/factorial(gridsize-k) for k in range(gridsize+1)])

print(tictactoeComplexity(2*2)) # result is 65

Now let's check for a 3*3 board:

print(tictactoeComplexity(3*3)) # result is 986,410

Just going from 2x2 to 3x3, we go from around 100 to around 1,000,000. You can guess the result for a 4x4 board:

print(tictactoeComplexity(4*4)) # result is 56,874,039,553,217

So your program does what you are asking it to do, but you're asking quite a lot.


答:
You could override the model's save method like this.</p>
```<code>    def save(self, *args, **kwargs):
        year = self.date.year
        albums_of_year = Album.objects.filter(artist=self.artist, date__year=year)
        if albums_of_year:
            # you can raise your error here
        else:
            super(Album, self).save(*args, **kwargs)
</code>```

Title: constrains on django model with ForeignKey

Body:
Suppose we have this django models:

from django.db import models

# Create your models here.
class Artist(models.Model):
    name = models.CharField(max_length=10)

class Album(models.Model):
    name = models.CharField(max_length=10)
    date = models.DateField()
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE)

So I can write:

artist_one = models.Artist.objects.create(name='Santana')
album_one = models.Album.objects.create(name='Abraxas', date = datetime.date.today(), artist=artist_one)
album_two = models.Album.objects.create(name='Supernatural', date = datetime.date.today(), artist=artist_one)

How can I add a constrain to the Album class as to say an artist cannot publish two albums the same year?

Body:
You could override the model's save method like this.

    def save(self, *args, **kwargs):
        year = self.date.year
        albums_of_year = Album.objects.filter(artist=self.artist, date__year=year)
        if albums_of_year:
            # you can raise your error here
        else:
            super(Album, self).save(*args, **kwargs)


问:
LLVM linker places stack in the wrong place
I am trying to link Cortex-M4 firmware with clang + lld. The gcc build works fine. I am using the stock CMSIS linker script with only RAM & ROM size adjusted (bases are the same). Beginning of the script (without comments):

```<code>__ROM_BASE = 0x00000000;
__ROM_SIZE = 0x00040000; /* 256 KB */
__RAM_BASE = 0x20000000;
__RAM_SIZE = 0x00010000; /* 64 KB */
__STACK_SIZE = 0x00000400;
__HEAP_SIZE  = 0x00000C00;
MEMORY
{
  FLASH (rx)  : ORIGIN = __ROM_BASE, LENGTH = __ROM_SIZE
  RAM   (rwx) : ORIGIN = __RAM_BASE, LENGTH = __RAM_SIZE
}
</code>```

Linking fails on the assert ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack") at the very end of the script. If I remove the assert the binary links fine, but stack is at a wrong address. It ends up in flash (heap is also in the wrong place). Output from nm:

```<code>200018a8 B __bss_end__
200000d8 B __bss_start__
00004568 R __copy_table_end__
0000455c R __copy_table_start__
200000d4 D __data_end__
20000000 D __data_start__
00000000 ? __end__
00004570 A __etext
0000455c R __exidx_end
0000454c T __exidx_start
00000c00 A __HEAP_SIZE
00000c00 ? __HeapLimit
200000d4 d __init_array_end
200000d0 d __init_array_start
         w __libc_fini_array
00000445 T __libc_init_array
200000d0 d __preinit_array_end
200000d0 d __preinit_array_start
20000000 A __RAM_BASE
00010000 A __RAM_SIZE
00000000 A __ROM_BASE
00040000 A __ROM_SIZE
00000400 ? __stack
00000400 A __STACK_SIZE
00000000 ? __StackLimit
00000400 ? __StackTop
</code>```

The code of course crashes when trying to run on the Cortex-M. I found this ticket. If I change my .stack section to:

```<code>.stack (COPY) :
  {
    . = ORIGIN(RAM) + LENGTH(RAM) - __STACK_SIZE;
    __StackLimit = .;
    . = . + __STACK_SIZE;
    . = ALIGN(8);
    __StackTop = .;
  } > RAM
  PROVIDE(__stack = __StackTop);
</code>```

then I am getting overflow errors:

```<code>ld.lld: error: section '.stack' will not fit in region 'RAM': overflowed by 536879272 bytes
</code>```

That huge number is 0x200020A8 so the lld probably gets the end address correctly, but not the origin.

How can I fix the linker script to place the stack correctly at top of RAM? Thanks for help

答:
I fixed it by removing COPY and adding NOLOAD to the stack section. It builds and runs fine both with gcc and clang.

```<code> .stack (ORIGIN(RAM) + LENGTH(RAM) - __STACK_SIZE) (NOLOAD) :
   {
    . = ALIGN(8);
    __StackLimit = .;
    . = . + __STACK_SIZE;
    . = ALIGN(8);
    __StackTop = .;
   } > RAM
   PROVIDE(__stack = __StackTop);
</code>```

Body:
I am trying to link Cortex-M4 firmware with clang + lld. The gcc build works fine. I am using the stock CMSIS linker script with only RAM & ROM size adjusted (bases are the same). Beginning of the script (without comments):

__ROM_BASE = 0x00000000;
__ROM_SIZE = 0x00040000; /* 256 KB */
__RAM_BASE = 0x20000000;
__RAM_SIZE = 0x00010000; /* 64 KB */
__STACK_SIZE = 0x00000400;
__HEAP_SIZE  = 0x00000C00;
MEMORY
{
  FLASH (rx)  : ORIGIN = __ROM_BASE, LENGTH = __ROM_SIZE
  RAM   (rwx) : ORIGIN = __RAM_BASE, LENGTH = __RAM_SIZE
}

Linking fails on the assert ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack") at the very end of the script. If I remove the assert the binary links fine, but stack is at a wrong address. It ends up in flash (heap is also in the wrong place). Output from nm:

200018a8 B __bss_end__
200000d8 B __bss_start__
00004568 R __copy_table_end__
0000455c R __copy_table_start__
200000d4 D __data_end__
20000000 D __data_start__
00000000 ? __end__
00004570 A __etext
0000455c R __exidx_end
0000454c T __exidx_start
00000c00 A __HEAP_SIZE
00000c00 ? __HeapLimit
200000d4 d __init_array_end
200000d0 d __init_array_start
         w __libc_fini_array
00000445 T __libc_init_array
200000d0 d __preinit_array_end
200000d0 d __preinit_array_start
20000000 A __RAM_BASE
00010000 A __RAM_SIZE
00000000 A __ROM_BASE
00040000 A __ROM_SIZE
00000400 ? __stack
00000400 A __STACK_SIZE
00000000 ? __StackLimit
00000400 ? __StackTop

The code of course crashes when trying to run on the Cortex-M. I found this ticket. If I change my .stack section to:

.stack (COPY) :
  {
    . = ORIGIN(RAM) + LENGTH(RAM) - __STACK_SIZE;
    __StackLimit = .;
    . = . + __STACK_SIZE;
    . = ALIGN(8);
    __StackTop = .;
  } > RAM
  PROVIDE(__stack = __StackTop);

then I am getting overflow errors:

ld.lld: error: section '.stack' will not fit in region 'RAM': overflowed by 536879272 bytes

That huge number is 0x200020A8 so the lld probably gets the end address correctly, but not the origin.

How can I fix the linker script to place the stack correctly at top of RAM? Thanks for help

Body:
I fixed it by removing COPY and adding NOLOAD to the stack section. It builds and runs fine both with gcc and clang.

 .stack (ORIGIN(RAM) + LENGTH(RAM) - __STACK_SIZE) (NOLOAD) :
   {
    . = ALIGN(8);
    __StackLimit = .;
    . = . + __STACK_SIZE;
    . = ALIGN(8);
    __StackTop = .;
   } > RAM
   PROVIDE(__stack = __StackTop);


问:
i18next interpolation defaultVariables from backend
I have the following i18n setup on my react

```<code>import i18n from 'i18next'
import LanguageDetector from 'i18next-browser-languagedetector'
import {initReactI18next} from 'react-i18next'
import Backend from 'i18next-http-backend';

i18n
    .use(initReactI18next)
    .use(LanguageDetector)
    .use(Backend)
    .init({
        backend: {
            loadPath: `test.com`,
        },
        react: {
            useSuspense: false
        },
        whitelist: ['en', 'fr', 'es'],
        fallbackLng: ['en'],
        detection: {
            order: ['cookie', 'navigator'],
            lookupCookie: 'i18nextLng',
            caches: ['cookie'],
            cookieOptions: { path: '/', sameSite: 'strict' },
            checkWhitelist: true
        }
    })

export default i18n
</code>```

I have a unique product with some information as price I would like to use as defaultVariables

Reading the doc I can change the inti config adding

```<code>{
    interpolation: {
        defaultVariables: {price: 100}
    }
}
</code>```

and then {{price}} should be available on all my translations

This part is working great.

But I am getting the product from a server call and I can not find any way to update the defaultVariables once i18n has been initialized

答:
Officially, the only way to have defaultVariables injected is via init function.
This means you have to load those values before calling init(), and then pass them via init options.

But....

you can try to do this:

```<code>i18next.services.interpolator.options.interpolation.defaultVariables = { price: 100 }
</code>```

This may work, but this is for sure unsupported and not recommended.

btw: the whitelist option was replaced with supportedLngs: https://www.i18next.com/misc/migration-guide#removed-deprecated

Body:
I have the following i18n setup on my react

import i18n from 'i18next'
import LanguageDetector from 'i18next-browser-languagedetector'
import {initReactI18next} from 'react-i18next'
import Backend from 'i18next-http-backend';

i18n
    .use(initReactI18next)
    .use(LanguageDetector)
    .use(Backend)
    .init({
        backend: {
            loadPath: `test.com`,
        },
        react: {
            useSuspense: false
        },
        whitelist: ['en', 'fr', 'es'],
        fallbackLng: ['en'],
        detection: {
            order: ['cookie', 'navigator'],
            lookupCookie: 'i18nextLng',
            caches: ['cookie'],
            cookieOptions: { path: '/', sameSite: 'strict' },
            checkWhitelist: true
        }
    })

export default i18n

I have a unique product with some information as price I would like to use as defaultVariables

Reading the doc I can change the inti config adding

{
    interpolation: {
        defaultVariables: {price: 100}
    }
}

and then {{price}} should be available on all my translations

This part is working great.

But I am getting the product from a server call and I can not find any way to update the defaultVariables once i18n has been initialized

Body:
Officially, the only way to have defaultVariables injected is via init function.
This means you have to load those values before calling init(), and then pass them via init options.

But....

you can try to do this:

i18next.services.interpolator.options.interpolation.defaultVariables = { price: 100 }

This may work, but this is for sure unsupported and not recommended.

btw: the whitelist option was replaced with supportedLngs: https://www.i18next.com/misc/migration-guide#removed-deprecated


问:
Unable to create javax script engine for javascript in Javafx application
I am using NetBeans 12.0. and windows 10 64 bit.My JDK is 15.0.2 and JavaFx SDK 16. When I try to build my javafx application it shows the following build error:

C:\Users\musta\OneDrive\Documents\NetBeansProjects\JavafxTry\nbproject\jfx-impl.xml:1251: The following error occurred while executing this line:
C:\Users\musta\OneDrive\Documents\NetBeansProjects\JavafxTry\nbproject\jfx-impl.xml:1259: Unable to create javax script engine for javascript

BUILD FAILED (total time: 3 seconds)

答:
The javascript engine used to be included in the JVM but it has been removed in Java 15.

As an alternative you can use Groovy instead. Groovy looks similar to javascript. Your javascript should be easy to port to Groovy.

When you use the 'groovy-all jar', the script tag looks something like:

<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpath="path/to/groovy-all-2.4.21.jar" />
<groovy>
    // your Groovy script
</groovy>
```


问:
Selecting a div in jQuery & appending to the DOM
```<code>        var activeMsg=[];       // Active Users
        var noactiveMsg=[];     // Non-Active Users with a Pending Message
        var noactiveNomsg=[];       // Non-Active Users without a Pending Message

        $('div.chatmember').each(function() {
            currentMember=$(this);

            if($(this).children().children('div').hasClass('chatactivestatus_online')) {
                alert('online');
                activeMsg.push(currentMember);
            }
            if($(this).children().children('div').hasClass('chatactivestatus_offline')) {
                alert('offline');
                noactiveMsg.push(currentMember);
            }
        });

        $.each(noactiveMsg, function(i, e){
             $('#chatCenterMembers').append(e.html());
         })
</code>```

The above code is quering DOM structure that looks like this:

```<code> <div id="chatCenterMembers">
   <div class="chatmember">
      <a class="ststusTitle">
         <div class="chatactivestatus_online"></div>
</code>```

it all works except for currentMember=$(this);
I believe this should hold

when I do the append at the end of the code above it doesn't add
 back into the dom. It adds its children elements only.

答:
So you issue is this line here:

```<code>$('#chatCenterMembers').append(e.html());
</code>```

and more specifically: e.html()

Accourding to the jQuery spec for .html():

Get the HTML contents of the first element in the set of matched
  elements.

which means that .html() returns the child contents of the selection, not the selection itself.

What you want to do, is just use .append(), this will append the entire selected div and not just it's children

Body:
        var activeMsg=[];       // Active Users
        var noactiveMsg=[];     // Non-Active Users with a Pending Message
        var noactiveNomsg=[];       // Non-Active Users without a Pending Message

        $('div.chatmember').each(function() {
            currentMember=$(this);

            if($(this).children().children('div').hasClass('chatactivestatus_online')) {
                alert('online');
                activeMsg.push(currentMember);
            }
            if($(this).children().children('div').hasClass('chatactivestatus_offline')) {
                alert('offline');
                noactiveMsg.push(currentMember);
            }
        });

        $.each(noactiveMsg, function(i, e){
             $('#chatCenterMembers').append(e.html());
         })

The above code is quering DOM structure that looks like this:

it all works except for currentMember=$(this);
I believe this should hold

when I do the append at the end of the code above it doesn't add
 back into the dom. It adds its children elements only.

Body:
So you issue is this line here:

$('#chatCenterMembers').append(e.html());

and more specifically: e.html()

Accourding to the jQuery spec for .html():

Get the HTML contents of the first element in the set of matched
  elements.

which means that .html() returns the child contents of the selection, not the selection itself.

What you want to do, is just use .append(), this will append the entire selected div and not just it's children