问:
Use index column directly when sending dataframe from wide to long
I am trying to bring dataframe from wide to long through pd.wide_to_long() given all the columns share a string+year pattern. However, I am wondering if there's a way I can do a one-liner by using the index column directly like pd.wide_to_long(df, ["A", "B"], i = df.index, j = "year").reset_index(). I tried and it didn't seem to accept the practice. Is there a way to do it?

```<code>df = pd.DataFrame({"cid" : {0 : "cd1", 1 : "cd2", 2 : "cd3"},
                   "A1970" : {0 : 3.2, 1 : 3.5, 2 : .4},
                   "A1980" : {0 : 3.1, 1 : 3.6, 2 : .5},
                   "B1970" : {0 : 2.5, 1 : 1.2, 2 : .7},
                   "B1980" : {0 : 3.2, 1 : 1.3, 2 : .1}
                  })
df = df.set_index(['cid'])

     A1970  A1980  B1970  B1980
cid
cd1    3.2    3.1    2.5    3.2
cd2    3.5    3.6    1.2    1.3
cd3    0.4    0.5    0.7    0.1
</code>```
```<code>df['cid'] = df.index
pd.wide_to_long(df, ["A", "B"], i = 'cid', j = "year").reset_index()
</code>```
```<code>Expected output:
   cid  year    A    B
0  cd1  1970  3.2  2.5
1  cd2  1970  3.5  1.2
2  cd3  1970  0.4  0.7
3  cd1  1980  3.1  3.2
4  cd2  1980  3.6  1.3
5  cd3  1980  0.5  0.1
</code>```

答:
Can reset index inside so that wide_to_long sees the reseted dataframe. Your index already has a name so when resetted, the column name of it becomes that ('cid') and we put that to i. (In default it would be 'index')

```<code>#               this is inside
pd.wide_to_long(df.reset_index(), ["A", "B"], i='cid', j="year").reset_index()
</code>```
```<code>   cid  year    A    B
0  cd1  1970  3.2  2.5
1  cd2  1970  3.5  1.2
2  cd3  1970  0.4  0.7
3  cd1  1980  3.1  3.2
4  cd2  1980  3.6  1.3
5  cd3  1980  0.5  0.1
</code>```

Body:
I am trying to bring dataframe from wide to long through pd.wide_to_long() given all the columns share a string+year pattern. However, I am wondering if there's a way I can do a one-liner by using the index column directly like pd.wide_to_long(df, ["A", "B"], i = df.index, j = "year").reset_index(). I tried and it didn't seem to accept the practice. Is there a way to do it?

df = pd.DataFrame({"cid" : {0 : "cd1", 1 : "cd2", 2 : "cd3"},
                   "A1970" : {0 : 3.2, 1 : 3.5, 2 : .4},
                   "A1980" : {0 : 3.1, 1 : 3.6, 2 : .5},
                   "B1970" : {0 : 2.5, 1 : 1.2, 2 : .7},
                   "B1980" : {0 : 3.2, 1 : 1.3, 2 : .1}
                  })
df = df.set_index(['cid'])

     A1970  A1980  B1970  B1980
cid
cd1    3.2    3.1    2.5    3.2
cd2    3.5    3.6    1.2    1.3
cd3    0.4    0.5    0.7    0.1

df['cid'] = df.index
pd.wide_to_long(df, ["A", "B"], i = 'cid', j = "year").reset_index()

Expected output:
   cid  year    A    B
0  cd1  1970  3.2  2.5
1  cd2  1970  3.5  1.2
2  cd3  1970  0.4  0.7
3  cd1  1980  3.1  3.2
4  cd2  1980  3.6  1.3
5  cd3  1980  0.5  0.1

Body:
Can reset index inside so that wide_to_long sees the reseted dataframe. Your index already has a name so when resetted, the column name of it becomes that ('cid') and we put that to i. (In default it would be 'index')

#               this is inside
pd.wide_to_long(df.reset_index(), ["A", "B"], i='cid', j="year").reset_index()

   cid  year    A    B
0  cd1  1970  3.2  2.5
1  cd2  1970  3.5  1.2
2  cd3  1970  0.4  0.7
3  cd1  1980  3.1  3.2
4  cd2  1980  3.6  1.3
5  cd3  1980  0.5  0.1


来源: stackexchange

问:
PVlib: Temperature coefficients
I'm modeling a module not found in the databases, so the data I got on the module is from the datasheet.

What is the recommended way of doing this?

The temperature coefficients in the datasheet is given with delta tempearature percentage gain.

in pvlib it's delta temperature * temperature coeficient.

If I understand this correctly I would use an exponential function to calculate the short circuit current given a temperature like so:

Isc * (1 + a_isc^(temperature - referance temperature))

while the way pvlib does it is linear? %/C vs. A/C

Anyway, how to give the best number to pvlib?

Edit: link to datasheet
https://www.jasolar.com.cn/uploadfile/2020/1128/20201128025603510.pdf

Edit2: Example conversion

From datasheet:
Isc = 11.58A
a_Isc = 0.044%/C
(STC values)

conversion:
a_Isc / 100 * 11.58 = 0,0051 A/C

答:
Isc * (1 + a_isc^(temperature - referance temperature))

Maybe exponential-style temperature coefficients like that exist, but I've never seen one in PV.  This particular equation doesn't make much sense anyway -- when temperature == reference temperature, it should return Isc right?  But this equation would return 2*Isc.

Anyway, to give these coefficients to pvlib, checking the units is the main thing.  Temperature coefficients can be "raw" and have units A/°C, V/°C, or W/°C, or they can be normalized by their STC values and have units %/°C or 1/°C.  The values in that datasheet are normalized (values in %/°C, convert to 1/°C by dividing by 100).  Coefficients in 1/°C can be converted to non-normalized units by multiplying by the corresponding STC value.

It's a shame that pvlib is not consistent in what units it wants; some pvlib functions want the coefficients to be normalized and some don't.  You have to check the documentation of the function you're using to see what units it wants.  For example the documentation for pvlib.pvsystem.calcparams_cec says:

```<code>alpha_sc (float) – The short-circuit current temperature coefficient
of the module in units of A/C.
</code>```

And pvlib.pvsystem.pvwatts_dc (docs):

```<code>gamma_pdc (numeric) – The temperature coefficient of power.
Typically -0.002 to -0.005 per degree C. [1/C]
</code>```

ContentLicense: CC BY-SA 4.0

Title: PVlib: Temperature coefficients

Body:
Isc * (1 + a_isc^(temperature - referance temperature))

Maybe exponential-style temperature coefficients like that exist, but I've never seen one in PV.  This particular equation doesn't make much sense anyway -- when temperature == reference temperature, it should return Isc right?  But this equation would return 2*Isc.

Anyway, to give these coefficients to pvlib, checking the units is the main thing.  Temperature coefficients can be "raw" and have units A/°C, V/°C, or W/°C, or they can be normalized by their STC values and have units %/°C or 1/°C.  The values in that datasheet are normalized (values in %/°C, convert to 1/°C by dividing by 100).  Coefficients in 1/°C can be converted to non-normalized units by multiplying by the corresponding STC value.

It's a shame that pvlib is not consistent in what units it wants; some pvlib functions want the coefficients to be normalized and some don't.  You have to check the documentation of the function you're using to see what units it wants.  For example the documentation for pvlib.pvsystem.calcparams_cec says:

alpha_sc (float) – The short-circuit current temperature coefficient
of the module in units of A/C.

And pvlib.pvsystem.pvwatts_dc (docs):

gamma_pdc (numeric) – The temperature coefficient of power.
Typically -0.002 to -0.005 per degree C. [1/C]


问:
Jackson Ignor From Another Java Class
I want the Employee object to only display the id property when adding a CV. When I put @JsonIgnore on the employee object in the CV, the id doesn't come either. How can I hide property that I do not want to appear in employee class (password, national identity) on the CV class?
Cv Class :

```<code>@Entity
@Table(name = "cvs")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CV {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @OneToOne
    @JoinColumn(name = "employee_id")
    private Employee employee;

    @OneToMany(mappedBy = "cv", cascade = CascadeType.ALL, orphanRemoval = true)
    List<Education> educations;

    @OneToMany(mappedBy = "cv", cascade = CascadeType.ALL, orphanRemoval = true)
    List<Work> works;

    @OneToMany(mappedBy = "cv", cascade = CascadeType.ALL, orphanRemoval = true)
    List<Languege> langueges;

    @OneToMany(mappedBy = "cv", cascade = CascadeType.ALL, orphanRemoval = true)
    List<Technology> technologies;

    @Column(name = "github")
    private String github;

    @Column(name = "linkedin")
    private String linkedin;

    @NotNull
    @NotBlank
    @Column(name = "cover_letter")
    private String coverLetter;

    @Column(name = "photo")
    private String photo;

}
</code>```

Employee Class :

```<code>@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "employees")
@Entity

@PrimaryKeyJoinColumn(name = "user_id", referencedColumnName = "id")
public class Employee extends User {

    @NotNull
    @NotBlank
    @Column(name = "first_name", length = 50)
    private String firstName;

    @NotNull
    @NotBlank
    @Column(name = "last_name", length = 50)
    private String lastName;

    @NotNull
    @NotBlank
    @Column(name = "national_identity", length = 11)
    private String nationalIdentity;

    @NotNull
    @Column(name = "year_of_birth")
    private int yearOfBirth;

    @JsonIgnore
    @OneToOne(mappedBy = "employee")
    private CV cv;
}
</code>```

I read data this format;

```<code>{
  "id": 0,
  "employee": {
    "id": 0, //visible
    "email": "string", //not visible
    "password": "string", //not visible
    "firstName": "string",
    "lastName": "string",
    "nationalIdentity": "string", //not visible
    "yearOfBirth": 0 //not visible
  },
  "educations": [
    {
      "id": 0,
      "schoolName": "string",
      "department": "string",
      "startingDate": "2022-02-11",
      "graduationDate": "2022-02-11"
    }
  ],
  "works": [
    {
      "id": 0,
      "workplace": "string",
      "job": {
        "id": 0,
        "jobName": "string"
      },
      "startingDate": "2022-02-11",
      "endDate": "2022-02-11"
    }
  ],
  "langueges": [
    {
      "id": 0,
      "languege": "string",
      "level": 0
    }
  ],
  "technologies": [
    {
      "id": 0,
      "technology": "string"
    }
  ],
  "github": "string",
  "linkedin": "string",
  "coverLetter": "string",
  "photo": "string"
}
</code>```

答:
You can use a custom serializer that you would use on your CV employee field : @JsonSerialize(using = CustomEmployeeSerializer.class)

```<code>public class CustomEmployeeSerializer extends SerializerBase<Employee> {

public CustomEmployeeSerializer() {
    super(Employee.class, true);
}

@Override
public void serialize(Employee employee, JsonGenerator jgen, SerializerProvider provider)
    throws IOException, JsonProcessingException {
    jgen.writeString(employee.getId());
}
</code>```

Title: Jackson Ignor From Another Java Class

Body:
I want the Employee object to only display the id property when adding a CV. When I put @JsonIgnore on the employee object in the CV, the id doesn't come either. How can I hide property that I do not want to appear in employee class (password, national identity) on the CV class?
Cv Class :

@Entity
@Table(name = "cvs")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CV {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @OneToOne
    @JoinColumn(name = "employee_id")
    private Employee employee;

    @OneToMany(mappedBy = "cv", cascade = CascadeType.ALL, orphanRemoval = true)
    List<Education> educations;

    @OneToMany(mappedBy = "cv", cascade = CascadeType.ALL, orphanRemoval = true)
    List<Work> works;

    @OneToMany(mappedBy = "cv", cascade = CascadeType.ALL, orphanRemoval = true)
    List<Languege> langueges;

    @OneToMany(mappedBy = "cv", cascade = CascadeType.ALL, orphanRemoval = true)
    List<Technology> technologies;

    @Column(name = "github")
    private String github;

    @Column(name = "linkedin")
    private String linkedin;

    @NotNull
    @NotBlank
    @Column(name = "cover_letter")
    private String coverLetter;

    @Column(name = "photo")
    private String photo;

}

Employee Class :

@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "employees")
@Entity

@PrimaryKeyJoinColumn(name = "user_id", referencedColumnName = "id")
public class Employee extends User {

    @NotNull
    @NotBlank
    @Column(name = "first_name", length = 50)
    private String firstName;

    @NotNull
    @NotBlank
    @Column(name = "last_name", length = 50)
    private String lastName;

    @NotNull
    @NotBlank
    @Column(name = "national_identity", length = 11)
    private String nationalIdentity;

    @NotNull
    @Column(name = "year_of_birth")
    private int yearOfBirth;

    @JsonIgnore
    @OneToOne(mappedBy = "employee")
    private CV cv;
}

I read data this format;

{
  "id": 0,
  "employee": {
    "id": 0, //visible
    "email": "string", //not visible
    "password": "string", //not visible
    "firstName": "string",
    "lastName": "string",
    "nationalIdentity": "string", //not visible
    "yearOfBirth": 0 //not visible
  },
  "educations": [
    {
      "id": 0,
      "schoolName": "string",
      "department": "string",
      "startingDate": "2022-02-11",
      "graduationDate": "2022-02-11"
    }
  ],
  "works": [
    {
      "id": 0,
      "workplace": "string",
      "job": {
        "id": 0,
        "jobName": "string"
      },
      "startingDate": "2022-02-11",
      "endDate": "2022-02-11"
    }
  ],
  "langueges": [
    {
      "id": 0,
      "languege": "string",
      "level": 0
    }
  ],
  "technologies": [
    {
      "id": 0,
      "technology": "string"
    }
  ],
  "github": "string",
  "linkedin": "string",
  "coverLetter": "string",
  "photo": "string"
}

Body:
You can use a custom serializer that you would use on your CV employee field : @JsonSerialize(using = CustomEmployeeSerializer.class)

public class CustomEmployeeSerializer extends SerializerBase<Employee> {

public CustomEmployeeSerializer() {
    super(Employee.class, true);
}

@Override
public void serialize(Employee employee, JsonGenerator jgen, SerializerProvider provider)
    throws IOException, JsonProcessingException {
    jgen.writeString(employee.getId());
}


问:
Extracting filesnames in bash with regex
Can please someone help me to set up a regular expression.

I have a large LaTeX3 TeXDoc file.  LaTeX3 TeXDoc defines the macro \TestFiles{}, which should be used, to list the names of files, which itself should be used as an unit tests.  You can name more than one file between the braces.  So \TestFiles{foo-bar} and \TestFiles{foo-bar, bar+baz,foo_bar_baz} are syntactical correct use cases for this macro.

I would like to write a bash script, to extract all the uni test files, named in the \TestFiles{} macros, to compile them with pdflatex and check, if pdflatex will be able to produce an output file successfully.

I have something like this in my script:

function get_filenames () {
  ## This regex works but is not sensible enough
  # regex='\\TestFiles{(.*)}'
  ## This works also, but is again not precise enough
  regex='\\TestFiles{([0-9a-zA-Z+-_, ]*)}'
  ## This should give more than one matching group
  ## (separated by ", " or ","), but this regex doesn't
  ## work.  I have no idea why or how to modify, to get
  ## it working

  while read -r line ; do
    if [[ $line =~ $regex ]] ; then
      i=1
        while [ $i -le 3 ]; do
          echo "Match $i: \"${BASH_REMATCH[$i]}\""
          i=$(( i + 1 ))
        done
      echo
    fi
  done < mystyle.dtx
}
```
<p>Here is an excerpt of the DTX file</p>
<pre class="lang-latex prettyprint-override"><code>\TestFiles{foo-bar}

\TestFiles{foo-bar, bar+baz,foo_bar_baz}
</code>```

(You can store this as mystyle.dtx, in order to reproduce the next example.)

Using the above noted examples, my script gives me the following results:

get_filenames
Match 1: "foo-bar"
Match 2: ""
Match 3: ""

Match 1: "foo-bar, bar+baz,foo_bar_baz"
Match 2: ""
Match 3: ""

```
<p>I wasn't able, to modify my <code>regex</code> expression, to split the content of the last <code>\TestFiles{foo-bar, bar+baz,foo_bar_baz}</code> example into three matching results.</p>
<p>I tried a regular expression like this <code>regex='\\TestFiles{([[:alnum:]+-_]*)[,]+[ ]*}'</code>.  I thought the <code>[:alnum:]+-_]*</code> would match the filenames.  As far as I understand regular expressions, the <code>(...)</code> should form a group, that should be listed afterwards in the bash array <code>BASH_REMATCH[$i]</code>.</p>
<p>The part <code>[,]+</code> should reflect that every file name must be separated by at least one comma.  Between the filenames there might be some white space, so something like <code>[[:space:]]*</code> or at least <code>[ ]*</code> should represent this.  The quantifier <code>*</code> means any repetition, ranging from 0 to ..., while <code>+</code> should at least appear one or more times.</p>
<p>But that regular expression did not work at all, if had no matching results.</p>
<p>How must <code>regex</code> be defined, to store each filenames as a matching group?  I am searching for the correct regular expression, to get this result:</p>
<pre class="lang-sh prettyprint-override"><code>get_filenames
Match 1: "foo-bar"
Match 2: ""
Match 3: ""

Match 1: "foo-bar"
Match 2: "bar+baz"
Match 3: "foo_bar_baz"

</code>```

EDIT: in my real world files, there may be (and are) more than tree test files.

Thanks in advance.

答:
I believe this is the regular expression you're looking for:

```<code>(?<=\\TestFiles{.*)([\w\d\-\+_]+)[, }]+
</code>```

You can see it working, modify it and have an explanation on what it does in the following link: https://regex101.com/r/0W8PBi/1

Body:
Can please someone help me to set up a regular expression.

I have a large LaTeX3 TeXDoc file.  LaTeX3 TeXDoc defines the macro \TestFiles{}, which should be used, to list the names of files, which itself should be used as an unit tests.  You can name more than one file between the braces.  So \TestFiles{foo-bar} and \TestFiles{foo-bar, bar+baz,foo_bar_baz} are syntactical correct use cases for this macro.

I would like to write a bash script, to extract all the uni test files, named in the \TestFiles{} macros, to compile them with pdflatex and check, if pdflatex will be able to produce an output file successfully.

I have something like this in my script:

function get_filenames () {
  ## This regex works but is not sensible enough
  # regex='\\TestFiles{(.*)}'
  ## This works also, but is again not precise enough
  regex='\\TestFiles{([0-9a-zA-Z+-_, ]*)}'
  ## This should give more than one matching group
  ## (separated by ", " or ","), but this regex doesn't
  ## work.  I have no idea why or how to modify, to get
  ## it working

  while read -r line ; do
    if [[ $line =~ $regex ]] ; then
      i=1
        while [ $i -le 3 ]; do
          echo "Match $i: \"${BASH_REMATCH[$i]}\""
          i=$(( i + 1 ))
        done
      echo
    fi
  done < mystyle.dtx
}

Here is an excerpt of the DTX file

\TestFiles{foo-bar}

\TestFiles{foo-bar, bar+baz,foo_bar_baz}

(You can store this as mystyle.dtx, in order to reproduce the next example.)

Using the above noted examples, my script gives me the following results:

get_filenames
Match 1: "foo-bar"
Match 2: ""
Match 3: ""

Match 1: "foo-bar, bar+baz,foo_bar_baz"
Match 2: ""
Match 3: ""

I wasn't able, to modify my regex expression, to split the content of the last \TestFiles{foo-bar, bar+baz,foo_bar_baz} example into three matching results.

I tried a regular expression like this regex='\\TestFiles{([[:alnum:]+-_]*)[,]+[ ]*}'.  I thought the [:alnum:]+-_]* would match the filenames.  As far as I understand regular expressions, the (...) should form a group, that should be listed afterwards in the bash array BASH_REMATCH[$i].

The part [,]+ should reflect that every file name must be separated by at least one comma.  Between the filenames there might be some white space, so something like [[:space:]]* or at least [ ]* should represent this.  The quantifier * means any repetition, ranging from 0 to ..., while + should at least appear one or more times.

But that regular expression did not work at all, if had no matching results.

How must regex be defined, to store each filenames as a matching group?  I am searching for the correct regular expression, to get this result:

get_filenames
Match 1: "foo-bar"
Match 2: ""
Match 3: ""

Match 1: "foo-bar"
Match 2: "bar+baz"
Match 3: "foo_bar_baz"

EDIT: in my real world files, there may be (and are) more than tree test files.

Thanks in advance.

Body:
I believe this is the regular expression you're looking for:

(?<=\\TestFiles{.*)([\w\d\-\+_]+)[, }]+

You can see it working, modify it and have an explanation on what it does in the following link: https://regex101.com/r/0W8PBi/1


问:
Period background sync does't fire after successfully registered
I'm trying to make periodic bg sync where service worker updates badge.
When I run my page and test it via Chrome DevTools, Service worker process the request. But when the page is closed, it doesnt't do anything. Same on mobile phone.

On my page (this part is working and output in console is periodic update set):

```<code>navigator.permissions.query({name:'periodic-background-sync'}).then(function(result) {
  if (result.state === 'granted') {
    console.log('periodic background granted');
    navigator.serviceWorker.ready.then(function(registration){
      if ('periodicSync' in registration) {
        try {
          registration.periodicSync.register('update-badge', {
            minInterval:  60 * 60 * 1000,
          }).then(function(ret){
            console.log('periodic update set');
          });
        } catch (error) {
          console.log('Periodic background sync cannot be used.');
        }
      }
    });
  }
});
</code>```

Service worker:

```<code>async function updateBadge() {
  const unreadCount = 5; //fixed value for testing
  navigator.setAppBadge(unreadCount).catch((error) => {
      console.log('Error setting badge.');
  });
}

self.addEventListener('periodicsync', (event) => {
  if (event.tag === 'update-badge') {
    event.waitUntil(updateBadge());
  }
});
</code>```

So when I manually fire background sync from DevTools, badge is set, but not automatically in the background as I thought it will work.

答:
I can't find anything wrong with the code. I've seen variants where you request periodic-background-sync after navigator.serviceWorker.ready but I think that both work (especially since you can trigger the registered event manually).

I think the problem is that not all conditions for periodic background sync to fire are true. These are the conditions from the initial implementation in Chrome (2019):

Chrome version 80 or later

The PWA needs to be installed

The website needs to have an engagement score (can be viewed here chrome://site-engagement/). Websites with 0 engagement is deleted from the list and won't trigger the periodicsync event, even if it is installed. Once a user hasn’t interacted with a website for 2 hours, its engagement levels begin to decay. With minimal engagement, a periodicsync event will be triggered at an interval of 36h. source

In your case I think your code is correct and working but you haven't interacted enough with you app continuously over 36h so the engagement is purged and periodicsync fire timer cancelled (if you have installed your PWA).

For the record, here is a complete working demo (event registration) (and sw code).

Body:
I'm trying to make periodic bg sync where service worker updates badge.
When I run my page and test it via Chrome DevTools, Service worker process the request. But when the page is closed, it doesnt't do anything. Same on mobile phone.

On my page (this part is working and output in console is periodic update set):

navigator.permissions.query({name:'periodic-background-sync'}).then(function(result) {
  if (result.state === 'granted') {
    console.log('periodic background granted');
    navigator.serviceWorker.ready.then(function(registration){
      if ('periodicSync' in registration) {
        try {
          registration.periodicSync.register('update-badge', {
            minInterval:  60 * 60 * 1000,
          }).then(function(ret){
            console.log('periodic update set');
          });
        } catch (error) {
          console.log('Periodic background sync cannot be used.');
        }
      }
    });
  }
});

Service worker:

async function updateBadge() {
  const unreadCount = 5; //fixed value for testing
  navigator.setAppBadge(unreadCount).catch((error) => {
      console.log('Error setting badge.');
  });
}

self.addEventListener('periodicsync', (event) => {
  if (event.tag === 'update-badge') {
    event.waitUntil(updateBadge());
  }
});

So when I manually fire background sync from DevTools, badge is set, but not automatically in the background as I thought it will work.


问:
Python zip unzip progress bar
I have a PyQt5 application that does zip/unzip operations under the hood and a general progress bar that indicates the status.

For example: if I download a file, I can track the received_bytes & total_size and calculate the current percentage.

The same can be done for the zip since it has a for loop and I can calculate the amount of processed files.

```<code>with zipfile.ZipFile(f'{path}', 'w') as z:
  for file in files:
    z.write(file_path, compress_type=zipfile.ZIP_STORED)
</code>```

I would like to know is it possible for me somehow to track the progress of the unpack operation?

```<code>shutil.unpack_archive(path_from, path_to)
</code>```

答:
Got a solution with the ZipFile

```<code>from zipfile import ZipFile

class ZipFileCustom(ZipFile):
    """ Custom ZipFile class with a callback on extractall. """

    def extractall(self, path=None, members=None, pwd=None, fn_progress=None):
        if members is None:
            members = self.namelist()

        if path is None:
            path = os.getcwd()
        else:
            path = os.fspath(path)

        for index, member in enumerate(members):
            if fn_progress:
                fn_progress(len(members), index + 1)
            self._extract_member(member, path, pwd)
</code>```

As you can see I've added the fn_progress into the extractall and a bit modified its for loop.

Now can use this method together with the function to calculate percents:

```<code>def calculate_percentage(total: int, current: int):
    percent = float(current) / total
    percent = round(percent * 100)
    return percent

with ZipFileCustom('./test.zip') as zfc:
    zfc.extractall('./out', fn_progress=lambda total, current: print(f'{calculate_percentage(total, current)} %'))
</code>```

The output is:

```<code>4%
8%
12%
17%
...
88%
92%
96%
100%
</code>```

Title: Python zip unzip progress bar

Body:
I have a PyQt5 application that does zip/unzip operations under the hood and a general progress bar that indicates the status.

For example: if I download a file, I can track the received_bytes & total_size and calculate the current percentage.

The same can be done for the zip since it has a for loop and I can calculate the amount of processed files.

with zipfile.ZipFile(f'{path}', 'w') as z:
  for file in files:
    z.write(file_path, compress_type=zipfile.ZIP_STORED)

I would like to know is it possible for me somehow to track the progress of the unpack operation?

shutil.unpack_archive(path_from, path_to)

Body:
Got a solution with the ZipFile

from zipfile import ZipFile

class ZipFileCustom(ZipFile):
    """ Custom ZipFile class with a callback on extractall. """

    def extractall(self, path=None, members=None, pwd=None, fn_progress=None):
        if members is None:
            members = self.namelist()

        if path is None:
            path = os.getcwd()
        else:
            path = os.fspath(path)

        for index, member in enumerate(members):
            if fn_progress:
                fn_progress(len(members), index + 1)
            self._extract_member(member, path, pwd)

As you can see I've added the fn_progress into the extractall and a bit modified its for loop.

Now can use this method together with the function to calculate percents:

def calculate_percentage(total: int, current: int):
    percent = float(current) / total
    percent = round(percent * 100)
    return percent

with ZipFileCustom('./test.zip') as zfc:
    zfc.extractall('./out', fn_progress=lambda total, current: print(f'{calculate_percentage(total, current)} %'))

The output is:

4%
8%
12%
17%
...
88%
92%
96%
100%


问:
Checking for empty Queue in python's multiprocessing
I have a program using python's packages multiprocessing and Queue. One of my functions have this structure:

```<code>from multiprocessing import Process, Queue
def foo(queue):
   while True:
       try:
           a = queue.get(block = False)
           doAndPrintStuff(a)
       except:
           print "the end"
           break

   if __name__ == "__main__"
     nthreads = 4
     queue = Queue.Queue()
     # put stuff in the queue here
     for stuff in moreStuff:
         queue.put(stuff)
     procs = [Process(target = foo, args = (queue,)) for i in xrange(nthreads)]
     for p in procs:
       p.start()
     for p in procs:
       p.join()
</code>```

the idea is that when I try to extract from the queue and it is empty, it'll raise an exception and terminate the loop. So I have two questions:

1) is this a safe idiom? Are there better ways to do this?

2) I tried to find what is the exact exception that is raised when I try to .get() from an empty queue. Currently my program is catching all exceptions, which sucks when the error is somewhere else and I only get a "the end" message.

I tried:

```<code>  import Queue
  queue = Queue.Queue()
  [queue.put(x) for x in xrange(10)]
  try:
       print queue.get(block = False)
  except Queue.Empty:
       print "end"
       break
</code>```

but I got the error as if I hadn't caught the exception. What's the correct exception to catch?

答:
The exception should be Queue.Empty. But are you sure you got the same error? In your second example, you also switched the queue itself from multiprocessing.Queue to Queue.Queue, which I think may be the problem.

It might seem strange, but you have to use the multiprocessing.Queue class, but use the Queue.Empty exception (which you have to import yourself from the Queue module)

ContentLicense: CC BY-SA 3.0

Body:
I have a program using python's packages multiprocessing and Queue. One of my functions have this structure:

from multiprocessing import Process, Queue
def foo(queue):
   while True:
       try:
           a = queue.get(block = False)
           doAndPrintStuff(a)
       except:
           print "the end"
           break

   if __name__ == "__main__"
     nthreads = 4
     queue = Queue.Queue()
     # put stuff in the queue here
     for stuff in moreStuff:
         queue.put(stuff)
     procs = [Process(target = foo, args = (queue,)) for i in xrange(nthreads)]
     for p in procs:
       p.start()
     for p in procs:
       p.join()

the idea is that when I try to extract from the queue and it is empty, it'll raise an exception and terminate the loop. So I have two questions:

1) is this a safe idiom? Are there better ways to do this?

2) I tried to find what is the exact exception that is raised when I try to .get() from an empty queue. Currently my program is catching all exceptions, which sucks when the error is somewhere else and I only get a "the end" message.

I tried:

  import Queue
  queue = Queue.Queue()
  [queue.put(x) for x in xrange(10)]
  try:
       print queue.get(block = False)
  except Queue.Empty:
       print "end"
       break

but I got the error as if I hadn't caught the exception. What's the correct exception to catch?


问:
how to type prisma objects in parameters
I have a function that expects a prisma model / instance

How do I actually type this function signature? What I want is the type that is returned from a prisma.SOMETABLE.find like:

const item = await prisma.nftCollection.findFirst()

short code snippet below.

import { Prisma, PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

export class Buyer {

    async findColl() {
        const item = await prisma.nftCollection.findFirst()
        await this.buyItem(item)
    }

    // this is the param i want to type
    async buyItem(item: SOMETYPE) {
        clog.info('todo - buy', item)
    }
}
```

more detail here:
https://github.com/prisma/prisma/discussions/11737

答:
In @prisma/client (see node_modules/.prisma/client/index.d.ts) you find the types of your models, which you defined in your Prisma schema. Let's say Item is your model.

Therefore you could do the following:

import { Prisma, PrismaClient, Item } from '@prisma/client';

const prisma = new PrismaClient();

export class Buyer {

  async findColl(): Promise<void> {
    const item = await prisma.nftCollection.findFirst();
    await this.buyItem(item);
  }

  async buyItem(item: Item | null): Promise<void> {
    clog.info('todo - buy', item);
  }
}
```

Item is the accordant type (imported from @prisma/client) and buyItem accepts an argument of Item | null since findFirst returns an Item or null. Of course you could check if item is null before calling buyItem, then it would be just item: Item.

If you add a selection set to findFirst, you might need to use Partial<Item>.

Edit: Just read your Github link. Instead of Item using collection should also be fine. Check node_modules/.prisma/client/index.d.ts if you find the definition of type collection { ... }.

Body:
I have a function that expects a prisma model / instance

How do I actually type this function signature? What I want is the type that is returned from a prisma.SOMETABLE.find like:

const item = await prisma.nftCollection.findFirst()

short code snippet below.

import { Prisma, PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

export class Buyer {

    async findColl() {
        const item = await prisma.nftCollection.findFirst()
        await this.buyItem(item)
    }

    // this is the param i want to type
    async buyItem(item: SOMETYPE) {
        clog.info('todo - buy', item)
    }
}

more detail here:
https://github.com/prisma/prisma/discussions/11737

Body:
In @prisma/client (see node_modules/.prisma/client/index.d.ts) you find the types of your models, which you defined in your Prisma schema. Let's say Item is your model.

Therefore you could do the following:

import { Prisma, PrismaClient, Item } from '@prisma/client';

const prisma = new PrismaClient();

export class Buyer {

  async findColl(): Promise<void> {
    const item = await prisma.nftCollection.findFirst();
    await this.buyItem(item);
  }

  async buyItem(item: Item | null): Promise<void> {
    clog.info('todo - buy', item);
  }
}

Item is the accordant type (imported from @prisma/client) and buyItem accepts an argument of Item | null since findFirst returns an Item or null. Of course you could check if item is null before calling buyItem, then it would be just item: Item.

If you add a selection set to findFirst, you might need to use Partial<Item>.

Edit: Just read your Github link. Instead of Item using collection should also be fine. Check node_modules/.prisma/client/index.d.ts if you find the definition of type collection { ... }.


问:
I want to find which publisher had the highest global sales between the specific dates. Am I doing this correctly?

So far i've got:

```<code>SELECT Publisher, MAX(Global_Sales) AS Most_Sales
FROM Vgsales
WHERE Year BETWEEN 1980 AND 1990;
</code>```

but not sure whether this is correct or not.

答:
Among the countless ways, you can use something like:</p>
```<code>WITH cte AS (
  SELECT SUM(global_sales) total, publisher FROM VGSALES
  WHERE year BETWEEN 1999 AND 2000
  GROUP BY publisher
)
SELECT * FROM cte WHERE total = (SELECT MAX(total) FROM cte)
</code>```

Body:
So far i've got:

SELECT Publisher, MAX(Global_Sales) AS Most_Sales
FROM Vgsales
WHERE Year BETWEEN 1980 AND 1990;

but not sure whether this is correct or not.

Body:
Among the countless ways, you can use something like:

WITH cte AS (
  SELECT SUM(global_sales) total, publisher FROM VGSALES
  WHERE year BETWEEN 1999 AND 2000
  GROUP BY publisher
)
SELECT * FROM cte WHERE total = (SELECT MAX(total) FROM cte)


问:
Asyncio combined with custom modules and aiohttp
I am trying to sleep a module for a couple seconds, while sleeping, the script should continue running.

When the sleep is over, the module should put data["found"] = True, but the code never reaches past asyncio.sleep()

The code should print:

"bulbasaur"

"do stuff" x 5

"bulbasaur"

But bulbasaur never comes back.

main.py:

```<code># main.py
urls = {"pokemon": pokemon.data, "nba": nba.data}

async def get_data(session, url):
    async with session.get(url, headers=headers) as r:
        return await r.json()

async def execute_modules(data):
    if pokemon.data["found"]:
        asyncio.create_task(nba.find_name(data[0]))
    else:
        asyncio.create_task(pokemon.find_name(data[0]))
        asyncio.create_task(nba.find_name(data[1]))

async def main():
    async with aiohttp.ClientSession() as session:
        tasks = []
        for key, value in urls.items():
            if not value["found"]:
                tasks.append(asyncio.create_task(get_data(session, value["url"])))

        data = await asyncio.gather(*tasks)
        # print(data[1])
        await execute_modules(data)

if __name__ == "__main__":
    while True:
        asyncio.run(main())
        time.sleep(2)
</code>```

pokemon.py:

```<code>import asyncio

data = {
    "url": "https://pokeapi.co/api/v2/pokemon/1",
    "found": False
}

async def find_name(pokemon):
    if pokemon["name"] == "bulbasaur":
        data["found"] = True
        print("bulbasaur found")
        await asyncio.sleep(10)
        data["found"] = False
    else:
        print("no bulbasaur")
</code>```

nba.py

```<code>data = {
    "url": "https://www.balldontlie.io/api/v1/players/1",
    "found": False
}

async def find_name(nba):
    print("do stuff")
</code>```

答:
The issue lies in execute_models(). There you spawn 1-2 tasks but never wait for them to complete.

This means that the task is scheduled to run the next event loop iteration but there never comes another event loop iteration since you never await anything after spawning the task. So main() exits and asyncio.run() cancels all tasks left. What you need to do is use asyncio.gather() inside execute_models() or have execute_models() return the tasks it creates and put another asyncio.gather() inside main().

async def execute_modules(data):
    tasks = []
    if pokemon.data["found"]:
        tasks.append(asyncio.create_task(nba.find_name(data[0])))
    else:
        tasks.append(asyncio.create_task(pokemon.find_name(data[0])))
        tasks.append(asyncio.create_task(nba.find_name(data[1])))
    await asyncio.gather(*tasks)
```

Since it seems like you want the script to continue running and not wait for the sleep, you may want to put the while True loop inside main() and change time.sleep(2) to asyncio.sleep(2) instead of what I proposed above.

The important aspect here is that the event loop is kept alive so that the tasks aren't cancelled and continue to run.

Body:
I am trying to sleep a module for a couple seconds, while sleeping, the script should continue running.

When the sleep is over, the module should put data["found"] = True, but the code never reaches past asyncio.sleep()

The code should print:

"bulbasaur"

"do stuff" x 5

"bulbasaur"

But bulbasaur never comes back.

main.py:

# main.py
urls = {"pokemon": pokemon.data, "nba": nba.data}

async def get_data(session, url):
    async with session.get(url, headers=headers) as r:
        return await r.json()

async def execute_modules(data):
    if pokemon.data["found"]:
        asyncio.create_task(nba.find_name(data[0]))
    else:
        asyncio.create_task(pokemon.find_name(data[0]))
        asyncio.create_task(nba.find_name(data[1]))

async def main():
    async with aiohttp.ClientSession() as session:
        tasks = []
        for key, value in urls.items():
            if not value["found"]:
                tasks.append(asyncio.create_task(get_data(session, value["url"])))

        data = await asyncio.gather(*tasks)
        # print(data[1])
        await execute_modules(data)

if __name__ == "__main__":
    while True:
        asyncio.run(main())
        time.sleep(2)

pokemon.py:

import asyncio

data = {
    "url": "https://pokeapi.co/api/v2/pokemon/1",
    "found": False
}

async def find_name(pokemon):
    if pokemon["name"] == "bulbasaur":
        data["found"] = True
        print("bulbasaur found")
        await asyncio.sleep(10)
        data["found"] = False
    else:
        print("no bulbasaur")

nba.py

data = {
    "url": "https://www.balldontlie.io/api/v1/players/1",
    "found": False
}

async def find_name(nba):
    print("do stuff")

Body:
The issue lies in execute_models(). There you spawn 1-2 tasks but never wait for them to complete.

This means that the task is scheduled to run the next event loop iteration but there never comes another event loop iteration since you never await anything after spawning the task. So main() exits and asyncio.run() cancels all tasks left. What you need to do is use asyncio.gather() inside execute_models() or have execute_models() return the tasks it creates and put another asyncio.gather() inside main().

async def execute_modules(data):
    tasks = []
    if pokemon.data["found"]:
        tasks.append(asyncio.create_task(nba.find_name(data[0])))
    else:
        tasks.append(asyncio.create_task(pokemon.find_name(data[0])))
        tasks.append(asyncio.create_task(nba.find_name(data[1])))
    await asyncio.gather(*tasks)

Since it seems like you want the script to continue running and not wait for the sleep, you may want to put the while True loop inside main() and change time.sleep(2) to asyncio.sleep(2) instead of what I proposed above.

The important aspect here is that the event loop is kept alive so that the tasks aren't cancelled and continue to run.


问:
How can I create a destroy function to delete from database and if its saved change color of button
How can I create a destroy function to delete from the database, and if it is saved in the database, the color of the button changes to red, otherwise green. My form stores data correctly, but I can not destroy it.

Blade

```<code>@foreach (range(1, 3) as $item)
    <form method="post" action="/seats">
        @csrf
        <input type="hidden" name="user_id" value="{{Auth::user()->id}}"/>
        <input type="hidden" name="row_seats" value="4"/>
        <input type="hidden" name="seat_id" value="{{ $item }}"/>
        <button type="submit" style="width:60px; margin-left:10px;"
            class="btn btn-success">
            {{$item}}
        </button>
    </form>

    <form method="POST" action="/seats/{{ $seats->id }}">
        <button type="submit" style="margin-left:10px;" class="btn btn-danger">
            Unreserve {{$item}}
        </button>
    </form>
@endforeach
</code>```

Destroy function

```<code>public function destroy($id)
{
    $seats = Seats::find($id);
    $seats->delete();

    return redirect('/seats');
}
</code>```

Route

```<code>Route::post('/seats/{seats}',[App\Http\Controllers\SeatsController::class, 'destroy'])
    ->name('seats.destroy');
</code>```

答:
My destroy function already work.Now i am trying if it's already saved in database it is showing only red button for delete.

```<code> @foreach (range(1, 3) as $item)

@if($seat_id > 0 )
<form method="post" action="{{ route('seats.destroy', $item) }}">
   @csrf
   @method('delete')

   <button type="submit"  style="margin-left:10px;"  class="btn btn-danger">Unreserve {{$item}}</button>

    </form>

    @else
<form method="post" action="/seats">
@csrf

    <input type="hidden" name="row_seats" value="4"/>
    <input type="hidden" name="seat_id" value="{{ $item }}"/>
    <button type="submit"  style="width:60px; margin-left:10px;"  class="btn btn-success">{{$item}}</button>

    </form>
    @endif

@endforeach
</code>```

Index function

```<code> $seat_id = $seats->id;
</code>```

if somebody need to see destroy function how it is created

```<code>public function destroy($id)
    {
    $seats = Seats::find($id);

    if ($seats != null) {
        $seats->delete();
        return redirect('/seats')->with('success', 'Post Removed Succesfully !');
    }

    return redirect('/seats')->with('status','Something went wrong !');

    }
</code>```

Body:
How can I create a destroy function to delete from the database, and if it is saved in the database, the color of the button changes to red, otherwise green. My form stores data correctly, but I can not destroy it.

Blade

@foreach (range(1, 3) as $item)
    <form method="post" action="/seats">
        @csrf
        <input type="hidden" name="user_id" value="{{Auth::user()->id}}"/>
        <input type="hidden" name="row_seats" value="4"/>
        <input type="hidden" name="seat_id" value="{{ $item }}"/>
        <button type="submit" style="width:60px; margin-left:10px;"
            class="btn btn-success">
            {{$item}}
        </button>
    </form>

    <form method="POST" action="/seats/{{ $seats->id }}">
        <button type="submit" style="margin-left:10px;" class="btn btn-danger">
            Unreserve {{$item}}
        </button>
    </form>
@endforeach

Destroy function

public function destroy($id)
{
    $seats = Seats::find($id);
    $seats->delete();

    return redirect('/seats');
}

Route

Route::post('/seats/{seats}',[App\Http\Controllers\SeatsController::class, 'destroy'])
    ->name('seats.destroy');

Body:
My destroy function already work.Now i am trying if it's already saved in database it is showing only red button for delete.

 @foreach (range(1, 3) as $item)

@if($seat_id > 0 )
<form method="post" action="{{ route('seats.destroy', $item) }}">
   @csrf
   @method('delete')

   <button type="submit"  style="margin-left:10px;"  class="btn btn-danger">Unreserve {{$item}}</button>

    </form>

    @else
<form method="post" action="/seats">
@csrf

    <input type="hidden" name="row_seats" value="4"/>
    <input type="hidden" name="seat_id" value="{{ $item }}"/>
    <button type="submit"  style="width:60px; margin-left:10px;"  class="btn btn-success">{{$item}}</button>

    </form>
    @endif

@endforeach

Index function

 $seat_id = $seats->id;

if somebody need to see destroy function how it is created

public function destroy($id)
    {
    $seats = Seats::find($id);

    if ($seats != null) {
        $seats->delete();
        return redirect('/seats')->with('success', 'Post Removed Succesfully !');
    }

    return redirect('/seats')->with('status','Something went wrong !');

    }


问:
Rounding numbers to n decimals in clojure
When I do, for example, (/ 1.0 7.0), I only get 17 digits: 0.14285714285714285. How can I get more then that?</p>

Body:
Use BigDecimal numbers and with-precision:

(with-precision 50 (/ 1M 7))
=> 0.14285714285714285714285714285714285714285714285714M


问:
I am getting Null check operator used on a null value
I am new to flutter and making an app with null safety but getting this error!
I think the error is caused by misc.dart specifically while declaring the firestore reference.
I tried dart migrate as well but it didn't help.
I got this error when I am running my simple flutter App. I could not figure out why this error occurred.

I am pasting the code below:
Thankyou

Stack Traces:

```<code>══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _CastError was thrown building DashboardPage(dirty, dependencies:
[_InheritedProviderScope<DocumentSnapshot<Misc>?>, _LocalizationsScope-[GlobalKey#55bd8],
_InheritedTheme], state: _DashboardPageState#de55a):
Null check operator used on a null value

The relevant error-causing widget was:
  DashboardPage
  DashboardPage:file:///Users/inductor/Desktop/vitcc_electrical_issues-main/lib/main.dart:42:22

When the exception was thrown, this was the stack:
#0      MiscSnapshotExtension.misc
</code>```

misc.dart

```<code>import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'misc.freezed.dart';

typedef MiscSnapshot = DocumentSnapshot<Misc>;

@freezed
class Misc with _$Misc {
  const Misc._();

  const factory Misc._create({
    required List<String> locationBlocks,
    required int activeIssuesCount,
    required int resolvedIssuesCount,
  }) = _Misc;

  static const _LocationBlocksKey = 'location-blocks';
  static const _ActiveIssuesCountKey = 'active-issues-count';
  static const _ResolvedIssuesCountKey = 'resolved-issues-count';

  static final _ref =
      FirebaseFirestore.instance.collection('misc').withConverter<Misc>(
    fromFirestore: (snapshot, options) {
      final data = snapshot.data()!;

      return Misc._create(
          locationBlocks: data[_LocationBlocksKey].cast<String>(),
          activeIssuesCount: data[_ActiveIssuesCountKey],
          resolvedIssuesCount: data[_ResolvedIssuesCountKey]);
    },
    toFirestore: (value, options) {
      return {
        _LocationBlocksKey: value.locationBlocks,
      };
    },
  ).doc('default');

  static get ref => _ref;

  static Stream<MiscSnapshot> get watch => _ref.snapshots();

  static Future<MiscSnapshot> get read => _ref.get();

  static Future<void> informIssueCreated() async {
    await _ref.update({
      _ActiveIssuesCountKey: FieldValue.increment(1),
    });
  }

  static Future<void> informIssueResolved() async {
    await _ref.update({
      _ActiveIssuesCountKey: FieldValue.increment(-1),
      _ResolvedIssuesCountKey: FieldValue.increment(1),
    });
  }

  static Future<void> informActiveIssuePurged() async {
    await _ref.update({
      _ActiveIssuesCountKey: FieldValue.increment(-1),
    });
  }

  static Future<void> updateActiveIssuesCount(int value) async {
    assert(value >= 0, 'bad argument: cannot write negative value');

    if (value < 0) {
      return;
    }

    await _ref.update({_ActiveIssuesCountKey: value});
  }
}

extension MiscSnapshotExtension on MiscSnapshot {
  Misc get misc => this.data()!;
}
</code>```

Dashboard.dart

```<code>class DashboardPage extends StatefulWidget {
  const DashboardPage({Key? key}) : super(key: key);

  @override
  _DashboardPageState createState() => _DashboardPageState();

  static _DashboardPageState of(BuildContext context) =>
      context.findAncestorStateOfType<_DashboardPageState>()!;
}

class _DashboardPageState extends State<DashboardPage> {
  bool _raiseNewIssueFormIsShown = false;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    final misc = Provider.of<MiscSnapshot>(context).misc;

    final userSnapshot = Provider.of<UserSnapshot>(context);
    final user = userSnapshot.user;

    NetworkImage? userPhoto;

    final photoUrl = Provider.of<User>(context).photoURL;

    if (photoUrl != null) {
      userPhoto = NetworkImage(photoUrl);
    }

    return Scaffold(
      appBar: AppBar(
        title: FadeInLeft(
          preferences: const AnimationPreferences(
            duration: Duration(milliseconds: 400),
          ),
          child: Text(
            _raiseNewIssueFormIsShown
                ? 'Raise an issue'
                : ElectricalIssueTrackerApp.appName,
          ),
        ),
        actions: [
          GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () => showDialog(
              context: context,
              builder: (_) => MiscellaneousDialog(userSnapshot),
            ),
            child: Padding(
              padding: const EdgeInsets.all(10),
              child: PhysicalModel(
                color: Colors.transparent,
                elevation: 3,
                shape: BoxShape.circle,
                child: CircleAvatar(
                  backgroundColor: theme.primaryColor,
                  child: Text(
                    ('${user.name}${user.email}-'[0]).toUpperCase(),
                    style: TextStyle(
                      color: theme.colorScheme.onPrimary,
                    ),
                  ),
                  foregroundImage: userPhoto,
                ),
              ),
            ),
          ),
        ],
      ),
      body: CupertinoScrollbar(
        child: SingleChildScrollView(
          physics: const BouncingScrollPhysics(),
          padding: const EdgeInsets.all(8),
          child: Wrap(
            spacing: 10,
            runSpacing: 30,
            children: [
              // The analytics widget
              ActiveAndResolvedIssueCounters(misc: misc),

              // View all active issues if user has permission
              if (user.scope.canViewActiveIssues) ActiveIssuesSection(),

              if (user.scope.canViewResolvedIssues)
                buildResolvedIssuesButton(misc, userSnapshot),

              // Raise an issue section
              if (user.scope.canCreateIssue) RaiseAnIssueSection(),

              // All issues raised by the user.
              MyIssuesSection(),

              // Footer
              SizedBox(height: 30),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> showRaiseNewIssueForm(BuildContext context) async {
    setState(() => _raiseNewIssueFormIsShown = true);

    await Scaffold.of(context)
        .showBottomSheet((_) => RaiseNewIssueBottomSheet())
        .closed;

    setState(() => _raiseNewIssueFormIsShown = false);
  }

  Widget buildResolvedIssuesButton(Misc misc, UserSnapshot userSnapshot) {
    return Center(
      child: OutlinedButton(
        child: Text('View already resolved issues'),
        onPressed: () => Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) {
              return MultiProvider(
                providers: [
                  Provider.value(value: userSnapshot),
                  Provider.value(value: misc),
                ],
                child: ResolvedIssuesPage(),
              );
            },
          ),
        ),
      ),
    );
  }
}
</code>```

答: Null check is ! so it probably means one of your variables value where you are using null check returns null so check those variables and maybe use if(data != null) {...} else {...}.

Body:
I am new to flutter and making an app with null safety but getting this error!
I think the error is caused by misc.dart specifically while declaring the firestore reference.
I tried dart migrate as well but it didn't help.
I got this error when I am running my simple flutter App. I could not figure out why this error occurred.

I am pasting the code below:
Thankyou

Stack Traces:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _CastError was thrown building DashboardPage(dirty, dependencies:
[_InheritedProviderScope<DocumentSnapshot<Misc>?>, _LocalizationsScope-[GlobalKey#55bd8],
_InheritedTheme], state: _DashboardPageState#de55a):
Null check operator used on a null value

The relevant error-causing widget was:
  DashboardPage
  DashboardPage:file:///Users/inductor/Desktop/vitcc_electrical_issues-main/lib/main.dart:42:22

When the exception was thrown, this was the stack:
#0      MiscSnapshotExtension.misc

misc.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'misc.freezed.dart';

typedef MiscSnapshot = DocumentSnapshot<Misc>;

@freezed
class Misc with _$Misc {
  const Misc._();

  const factory Misc._create({
    required List<String> locationBlocks,
    required int activeIssuesCount,
    required int resolvedIssuesCount,
  }) = _Misc;

  static const _LocationBlocksKey = 'location-blocks';
  static const _ActiveIssuesCountKey = 'active-issues-count';
  static const _ResolvedIssuesCountKey = 'resolved-issues-count';

  static final _ref =
      FirebaseFirestore.instance.collection('misc').withConverter<Misc>(
    fromFirestore: (snapshot, options) {
      final data = snapshot.data()!;

      return Misc._create(
          locationBlocks: data[_LocationBlocksKey].cast<String>(),
          activeIssuesCount: data[_ActiveIssuesCountKey],
          resolvedIssuesCount: data[_ResolvedIssuesCountKey]);
    },
    toFirestore: (value, options) {
      return {
        _LocationBlocksKey: value.locationBlocks,
      };
    },
  ).doc('default');

  static get ref => _ref;

  static Stream<MiscSnapshot> get watch => _ref.snapshots();

  static Future<MiscSnapshot> get read => _ref.get();

  static Future<void> informIssueCreated() async {
    await _ref.update({
      _ActiveIssuesCountKey: FieldValue.increment(1),
    });
  }

  static Future<void> informIssueResolved() async {
    await _ref.update({
      _ActiveIssuesCountKey: FieldValue.increment(-1),
      _ResolvedIssuesCountKey: FieldValue.increment(1),
    });
  }

  static Future<void> informActiveIssuePurged() async {
    await _ref.update({
      _ActiveIssuesCountKey: FieldValue.increment(-1),
    });
  }

  static Future<void> updateActiveIssuesCount(int value) async {
    assert(value >= 0, 'bad argument: cannot write negative value');

    if (value < 0) {
      return;
    }

    await _ref.update({_ActiveIssuesCountKey: value});
  }
}

extension MiscSnapshotExtension on MiscSnapshot {
  Misc get misc => this.data()!;
}

Dashboard.dart

class DashboardPage extends StatefulWidget {
  const DashboardPage({Key? key}) : super(key: key);

  @override
  _DashboardPageState createState() => _DashboardPageState();

  static _DashboardPageState of(BuildContext context) =>
      context.findAncestorStateOfType<_DashboardPageState>()!;
}

class _DashboardPageState extends State<DashboardPage> {
  bool _raiseNewIssueFormIsShown = false;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    final misc = Provider.of<MiscSnapshot>(context).misc;

    final userSnapshot = Provider.of<UserSnapshot>(context);
    final user = userSnapshot.user;

    NetworkImage? userPhoto;

    final photoUrl = Provider.of<User>(context).photoURL;

    if (photoUrl != null) {
      userPhoto = NetworkImage(photoUrl);
    }

    return Scaffold(
      appBar: AppBar(
        title: FadeInLeft(
          preferences: const AnimationPreferences(
            duration: Duration(milliseconds: 400),
          ),
          child: Text(
            _raiseNewIssueFormIsShown
                ? 'Raise an issue'
                : ElectricalIssueTrackerApp.appName,
          ),
        ),
        actions: [
          GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () => showDialog(
              context: context,
              builder: (_) => MiscellaneousDialog(userSnapshot),
            ),
            child: Padding(
              padding: const EdgeInsets.all(10),
              child: PhysicalModel(
                color: Colors.transparent,
                elevation: 3,
                shape: BoxShape.circle,
                child: CircleAvatar(
                  backgroundColor: theme.primaryColor,
                  child: Text(
                    ('${user.name}${user.email}-'[0]).toUpperCase(),
                    style: TextStyle(
                      color: theme.colorScheme.onPrimary,
                    ),
                  ),
                  foregroundImage: userPhoto,
                ),
              ),
            ),
          ),
        ],
      ),
      body: CupertinoScrollbar(
        child: SingleChildScrollView(
          physics: const BouncingScrollPhysics(),
          padding: const EdgeInsets.all(8),
          child: Wrap(
            spacing: 10,
            runSpacing: 30,
            children: [
              // The analytics widget
              ActiveAndResolvedIssueCounters(misc: misc),

              // View all active issues if user has permission
              if (user.scope.canViewActiveIssues) ActiveIssuesSection(),

              if (user.scope.canViewResolvedIssues)
                buildResolvedIssuesButton(misc, userSnapshot),

              // Raise an issue section
              if (user.scope.canCreateIssue) RaiseAnIssueSection(),

              // All issues raised by the user.
              MyIssuesSection(),

              // Footer
              SizedBox(height: 30),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> showRaiseNewIssueForm(BuildContext context) async {
    setState(() => _raiseNewIssueFormIsShown = true);

    await Scaffold.of(context)
        .showBottomSheet((_) => RaiseNewIssueBottomSheet())
        .closed;

    setState(() => _raiseNewIssueFormIsShown = false);
  }

  Widget buildResolvedIssuesButton(Misc misc, UserSnapshot userSnapshot) {
    return Center(
      child: OutlinedButton(
        child: Text('View already resolved issues'),
        onPressed: () => Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) {
              return MultiProvider(
                providers: [
                  Provider.value(value: userSnapshot),
                  Provider.value(value: misc),
                ],
                child: ResolvedIssuesPage(),
              );
            },
          ),
        ),
      ),
    );
  }
}


问:
Docker not starting on Windows 11 with WSL 2
I'm trying to setup docker with WSL 2 to run a Dockerfile. I downloaded Docker Desktop, and when I tried to follow the quick start guide, I got the following error:

```<code>docker: error during connect: This error may indicate that the docker daemon is not running.: Post "http://%2F%2F.%2Fpipe%d2Fdocker_engine/v1.24/containers/create?name=repo": open //./pipe/docker_enginer: The system cannot find the file specified.
</code>```

I set com.docker.service to run in the Task Manager, and have run:

```<code>“c:\Program Files\Docker\Docker\DockerCli.exe” -SwitchDaemon
</code>```

I have also quit Docker Desktop and reopened it in admin mode, and I still get the message that Docker Desktop has stopped.

Please let me know if there's any other options, thanks!

答:
If you are running Docker Desktop for Windows 4.5.0 then you should be aware of an existing issue where the default backend selected after installing is not the correct one.

You can switch to the correct backend manually by editing the file located at: %AppData%\Docker\settings.json (full path: C:\Users\%UserName%\AppData\Roaming\Docker\settings.json) and at the bottom of the file change the value for the wslEngineEnabled field to true. After that Docker Desktop should start correctly.

Similar issue and solution is mentioned here by the user mccaa25.

Body:
I'm trying to setup docker with WSL 2 to run a Dockerfile. I downloaded Docker Desktop, and when I tried to follow the quick start guide, I got the following error:

docker: error during connect: This error may indicate that the docker daemon is not running.: Post "http://%2F%2F.%2Fpipe%d2Fdocker_engine/v1.24/containers/create?name=repo": open //./pipe/docker_enginer: The system cannot find the file specified.

I set com.docker.service to run in the Task Manager, and have run:

“c:\Program Files\Docker\Docker\DockerCli.exe” -SwitchDaemon

I have also quit Docker Desktop and reopened it in admin mode, and I still get the message that Docker Desktop has stopped.

Please let me know if there's any other options, thanks!


问:
Some python argparse usage questions: retrieving arguments
Some questions that I couldn't figure out from reading the docs and some other questions.

1: I was stumped by "how do I actually retrieve the arguments" so I looked around and someone suggested to use the __dict __ function to access it like a dictionary. Which works, but is that the only way? This seems like a rather common thing but it doesn't appear to be anywhere obvious in the docs. If I missed it, maybe someone can point it out? It definitely wasn't at the top.

2: argparse was introduced in 2.7, but some people refuse to get newer versions of python and continue to stick to older ones like 2.5, 2.6 for reasons unknown to me. My solution for dealing with them is to take the argparse module and put it in my own script directory. Is there any problem with this solution? It seems to be working at least.

答:
Ad 1: Retrieving the values is pretty easy:

```<code>parser.add_argument('--some_arg', action='store')
parser.add_argument('--some_flag', action='store_true')

args = parser.parse_args()
</code>```

now the values can be accessed with args.some_arg or args.some_flag as can be seen in the documentation.

Ad 2: Since argparse was introduced in 2.7 many people stick to the older version optparse for backwards compatibility since the module might not be available. The syntax is pretty equal. My solution is to try to parse with argparse and use optparse as a fallback.

Body:
Ad 1: Retrieving the values is pretty easy:

parser.add_argument('--some_arg', action='store')
parser.add_argument('--some_flag', action='store_true')

args = parser.parse_args()

now the values can be accessed with args.some_arg or args.some_flag as can be seen in the documentation.

Ad 2: Since argparse was introduced in 2.7 many people stick to the older version optparse for backwards compatibility since the module might not be available. The syntax is pretty equal. My solution is to try to parse with argparse and use optparse as a fallback.


问:
NullPointerException with Atomikos and Spring Batch JpaPagingItemReader
I use Atomikos with spring boot, spring batch, spring data jpa, hibernate and Postgresql. I have two datasources configured in XA Datasource with the Atomikos pool.

I encounter two difficulties:

The first is that I can't disable the Atomikos pool autocommit. I've searched everywhere and can't find an autocommit-like property that I can set to false.

The second is a bit complex. My distributed transaction manager works because I have my two databases updating.

But when I use springbatch to read data from my second database with a JpaPagingItemReaderBuilder I have a nullPointeurException.
Below is the configuration of my second datasource and my second entitymanager for connecting to my second database.

```<code>@Bean
@ConfigurationProperties(prefix = "spring.jta.atomikos.datasource.secondary")
public DataSource dataSourceSecondary(){
    return new AtomikosDataSourceBean();
}

@Bean
@PersistenceContext(unitName = "entityManagerSecondaryFactory")
public LocalContainerEntityManagerFactoryBean entityManagerSecondaryFactory(){

    Properties properties;
    HibernateJpaVendorAdapter jpaVendorAdapter;
    LocalContainerEntityManagerFactoryBean factory;

    factory = new LocalContainerEntityManagerFactoryBean();
    factory.setDataSource(this.dataSourceSecondary());

    factory.setPackagesToScan(this.deltaProperties.getRepositoryPackageScanSecondary());

    //Conf JPAVendorAdapter
    jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setGenerateDdl(Boolean.parseBoolean(this.deltaProperties.getGenerateDdl()));
    jpaVendorAdapter.setDatabasePlatform(this.deltaProperties.getDatabase());
    factory.setJpaVendorAdapter(jpaVendorAdapter);

    //Propriété de l'implémentation du Provider JPA
    properties = new Properties();
    properties.setProperty("hibernate.cache.use_query_cache", this.deltaProperties.getUseQueryCache());
    properties.setProperty("hibernate.cache.use_second_level_cache", this.deltaProperties.getUseSecondLevelCache());
    properties.setProperty("hibernate.cache.region.factory_class", this.deltaProperties.getRegionFactoryClass());
    properties.setProperty("hibernate.generate_statistics", this.deltaProperties.getGenerateStatistics());
    properties.setProperty("hibernate.jdbc.batch_size", Integer.toString(this.deltaProperties.getBatchSize()));
    properties.setProperty("hibernate.order_inserts", this.deltaProperties.getOrderInserts());
    properties.setProperty("hibernate.order_updates", this.deltaProperties.getOrderUpdates());
    properties.setProperty("hibernate.hbm2ddl.auto", this.deltaProperties.getHbm2ddlAuto());
    properties.setProperty("hibernate.id.optimizer.pooled.preferred",
            this.deltaProperties.getIdOptimizerPooledPreferred());
    properties.setProperty("hibernate.format_sql", this.deltaProperties.getFormatSql());
    properties.setProperty("hibernate.use_sql_comments", this.deltaProperties.getCommentsSql());
    properties.setProperty("hibernate.default_schema", this.deltaProperties.getDefautSchema());
    properties.setProperty("hibernate.dialect", this.deltaProperties.getDialect());
    properties.setProperty("hibernate.connection.provider_disables_autocommit",
            this.deltaProperties.getDisablesAutoCommit());
    properties.setProperty("org.hibernate.envers.audit_table_suffix", this.deltaProperties.getHisto());
    properties.setProperty("hibernate.jdbc.time_zone", this.deltaProperties.getUtc());
    properties.setProperty("hibernate.connection.autocommit", "false");

    properties.setProperty("hibernate.transaction.jta.plateform", AtomikosJtaPlatform.class.getName());

    factory.setJpaProperties(properties);

    JpaDialect dialect = new HibernateJpaDialect();
    factory.setJpaDialect(dialect);

    return factory;
}

...

@Bean
@StepScope
public ItemReader<ResponseEntity> responseSendItemReader(
        @Value("#{jobExecutionContext['num']}") String num) {

    Map<String, Object> parameterValues;

    parameterValues = new HashMap<>();

    //parameter
    parameterValues.put("num", num);

    return new JpaPagingItemReaderBuilder<ResponseEntity>()
            .name("responseSendItemReader")
            .entityManagerFactory(this.entityManagerSecondaryFactory)
            .queryString("SELECT r FROM ResponseEntity r "
                    + " WHERE r.num = :num "
                    + " ORDER BY r.dateEmission ASC")

            .parameterValues(parameterValues)
            .pageSize(25)
            .build();

}
</code>```

Here is my properties file for my second base

```<code>spring.jta.atomikos.datasource.secondary.unique-resource-name=dataSourceSecondary
spring.jta.atomikos.datasource.secondary.max-pool-size=50
spring.jta.atomikos.datasource.secondary.min-pool-size=10
spring.jta.atomikos.datasource.secondary.max-life-time=0
spring.jta.atomikos.datasource.secondary.borrow-connection-timeout=10000
spring.jta.atomikos.datasource.secondary.defaultAutoCommit=false
spring.jta.atomikos.datasource.secondary.xa-data-source-class-name=@deltaie.ds.driver.class.name@
spring.jta.atomikos.datasource.secondary.xa-properties.url=url
spring.jta.atomikos.datasource.secondary.xa-properties.user=user
spring.jta.atomikos.datasource.secondary.xa-properties.password=pwd
</code>```

Here is my transactions.properties file:

```<code># Atomikos properties

com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.log_base_dir=./atomikos/log
com.atomikos.icatch.log_base_name=deltaieTransaction
com.atomikos.icatch.default_jta_timeout=10000
com.atomikos.icatch.oltp_retry_interval=10000
com.atomikos.icatch.checkpoint_interval=500
com.atomikos.icatch.max_timeout=300000
com.atomikos.icatch.recovery_delay=10000
com.atomikos.icatch.max_actives=50
</code>```

And here is the error I got

```<code>onReadError GunSendResponseStepListener java.lang.NullPointerException: null
at org.springframework.batch.item.database.JpaPagingItemReader.doReadPage(JpaPagingItemReader.java:192)
at org.springframework.batch.item.database.AbstractPagingItemReader.doRead(AbstractPagingItemReader.java:110)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:93)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:137)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:124)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
at com.sun.proxy.$Proxy300.read(Unknown Source)
at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:99)
at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:87)
at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:126)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145)
at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:118)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:71)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:407)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:331)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:273)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:208)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:152)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:68)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:68)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:137)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:320)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:149)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
</code>```

Thanks for your help

答:
If it helps, it was because the function defining my JpaPagingItemReader returned the IteamReader interface and not the JpapagingItemReader class.

Since this function is of Step scope, it is recommended to use the JpaPagingItemReader class rather than the ItemReader interface because of the proxy.

Body:
I use Atomikos with spring boot, spring batch, spring data jpa, hibernate and Postgresql. I have two datasources configured in XA Datasource with the Atomikos pool.

I encounter two difficulties:

The first is that I can't disable the Atomikos pool autocommit. I've searched everywhere and can't find an autocommit-like property that I can set to false.

The second is a bit complex. My distributed transaction manager works because I have my two databases updating.

But when I use springbatch to read data from my second database with a JpaPagingItemReaderBuilder I have a nullPointeurException.
Below is the configuration of my second datasource and my second entitymanager for connecting to my second database.

@Bean
@ConfigurationProperties(prefix = "spring.jta.atomikos.datasource.secondary")
public DataSource dataSourceSecondary(){
    return new AtomikosDataSourceBean();
}

@Bean
@PersistenceContext(unitName = "entityManagerSecondaryFactory")
public LocalContainerEntityManagerFactoryBean entityManagerSecondaryFactory(){

    Properties properties;
    HibernateJpaVendorAdapter jpaVendorAdapter;
    LocalContainerEntityManagerFactoryBean factory;

    factory = new LocalContainerEntityManagerFactoryBean();
    factory.setDataSource(this.dataSourceSecondary());

    factory.setPackagesToScan(this.deltaProperties.getRepositoryPackageScanSecondary());

    //Conf JPAVendorAdapter
    jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setGenerateDdl(Boolean.parseBoolean(this.deltaProperties.getGenerateDdl()));
    jpaVendorAdapter.setDatabasePlatform(this.deltaProperties.getDatabase());
    factory.setJpaVendorAdapter(jpaVendorAdapter);

    //Propriété de l'implémentation du Provider JPA
    properties = new Properties();
    properties.setProperty("hibernate.cache.use_query_cache", this.deltaProperties.getUseQueryCache());
    properties.setProperty("hibernate.cache.use_second_level_cache", this.deltaProperties.getUseSecondLevelCache());
    properties.setProperty("hibernate.cache.region.factory_class", this.deltaProperties.getRegionFactoryClass());
    properties.setProperty("hibernate.generate_statistics", this.deltaProperties.getGenerateStatistics());
    properties.setProperty("hibernate.jdbc.batch_size", Integer.toString(this.deltaProperties.getBatchSize()));
    properties.setProperty("hibernate.order_inserts", this.deltaProperties.getOrderInserts());
    properties.setProperty("hibernate.order_updates", this.deltaProperties.getOrderUpdates());
    properties.setProperty("hibernate.hbm2ddl.auto", this.deltaProperties.getHbm2ddlAuto());
    properties.setProperty("hibernate.id.optimizer.pooled.preferred",
            this.deltaProperties.getIdOptimizerPooledPreferred());
    properties.setProperty("hibernate.format_sql", this.deltaProperties.getFormatSql());
    properties.setProperty("hibernate.use_sql_comments", this.deltaProperties.getCommentsSql());
    properties.setProperty("hibernate.default_schema", this.deltaProperties.getDefautSchema());
    properties.setProperty("hibernate.dialect", this.deltaProperties.getDialect());
    properties.setProperty("hibernate.connection.provider_disables_autocommit",
            this.deltaProperties.getDisablesAutoCommit());
    properties.setProperty("org.hibernate.envers.audit_table_suffix", this.deltaProperties.getHisto());
    properties.setProperty("hibernate.jdbc.time_zone", this.deltaProperties.getUtc());
    properties.setProperty("hibernate.connection.autocommit", "false");

    properties.setProperty("hibernate.transaction.jta.plateform", AtomikosJtaPlatform.class.getName());

    factory.setJpaProperties(properties);

    JpaDialect dialect = new HibernateJpaDialect();
    factory.setJpaDialect(dialect);

    return factory;
}

...

@Bean
@StepScope
public ItemReader<ResponseEntity> responseSendItemReader(
        @Value("#{jobExecutionContext['num']}") String num) {

    Map<String, Object> parameterValues;

    parameterValues = new HashMap<>();

    //parameter
    parameterValues.put("num", num);

    return new JpaPagingItemReaderBuilder<ResponseEntity>()
            .name("responseSendItemReader")
            .entityManagerFactory(this.entityManagerSecondaryFactory)
            .queryString("SELECT r FROM ResponseEntity r "
                    + " WHERE r.num = :num "
                    + " ORDER BY r.dateEmission ASC")

            .parameterValues(parameterValues)
            .pageSize(25)
            .build();

}

Here is my properties file for my second base

spring.jta.atomikos.datasource.secondary.unique-resource-name=dataSourceSecondary
spring.jta.atomikos.datasource.secondary.max-pool-size=50
spring.jta.atomikos.datasource.secondary.min-pool-size=10
spring.jta.atomikos.datasource.secondary.max-life-time=0
spring.jta.atomikos.datasource.secondary.borrow-connection-timeout=10000
spring.jta.atomikos.datasource.secondary.defaultAutoCommit=false
spring.jta.atomikos.datasource.secondary.xa-data-source-class-name=@deltaie.ds.driver.class.name@
spring.jta.atomikos.datasource.secondary.xa-properties.url=url
spring.jta.atomikos.datasource.secondary.xa-properties.user=user
spring.jta.atomikos.datasource.secondary.xa-properties.password=pwd

Here is my transactions.properties file:

# Atomikos properties

com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.log_base_dir=./atomikos/log
com.atomikos.icatch.log_base_name=deltaieTransaction
com.atomikos.icatch.default_jta_timeout=10000
com.atomikos.icatch.oltp_retry_interval=10000
com.atomikos.icatch.checkpoint_interval=500
com.atomikos.icatch.max_timeout=300000
com.atomikos.icatch.recovery_delay=10000
com.atomikos.icatch.max_actives=50

And here is the error I got

onReadError GunSendResponseStepListener java.lang.NullPointerException: null
at org.springframework.batch.item.database.JpaPagingItemReader.doReadPage(JpaPagingItemReader.java:192)
at org.springframework.batch.item.database.AbstractPagingItemReader.doRead(AbstractPagingItemReader.java:110)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:93)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:137)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:124)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
at com.sun.proxy.$Proxy300.read(Unknown Source)
at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:99)
at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:87)
at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:126)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145)
at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:118)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:71)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:407)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:331)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:273)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:208)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:152)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:68)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:68)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:137)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:320)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:149)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)

Thanks for your help


问:
Is it better to add two numbers to a new variable or adding them together while displaying?
Which is better:

int sum = a + b;
std::cout << sum;
```
<pre class="lang-cpp prettyprint-override"><code>std::cout << a + b;
</code>```

In terms of performance and efficiency?

答:
Is it better two add two numbers to a new variable

No, it's not better in general.

or adding them together while displaying?

No, it's not better in general.

Either is usually fine. The intermediate local variable has advantages such as ability to give an understandable name, and it reduces the complexity of individual expressions. But adding a variable increases the total number of names which is another form of complexity. It's a matter of finding a good balance, and the "bestness" is subjective.

In terms of performance and efficiency?

Not using a local variable is pretty much never more efficient than using a variable. But there is no difference in the shown example. Furthermore, there is no difference in most simple cases.

The difference - if any - is primarily in readability.

Consider another example where you use the result of the operation multiple times. In that case, it would be rare for a program that repeats an operation to be more efficient than another program that stores the result in a variable. But there's still no difference if the case is simple.

Body:
Which is better:

int sum = a + b;
std::cout << sum;

std::cout << a + b;

In terms of performance and efficiency?


问:
Git Push Error: insufficient permission for adding an object to repository database
When I try to push to a shared git remote, I get the following error:
insufficient permission for adding an object to repository database

Then I read about a fix here: Fix  This worked for the next push, since all of the files were of the correct group, but the next time someone pushed up a change it made a new item in the objects folder that had their default group as the group. The only thing I can think of is to change all of the developer's default group for items they check in, but that seems like a hack.  Any ideas? Thanks.

答:
Make sure you open the command line prompt as admin. Then, make sure project files are not read-only.

In windows, you can check this by right-clicking on project folder -> click "Show more options" -> click "Properties" -> deselect "Read-only" -> click "Apply"


问:
Where can I find out why creators of modules in Python create multiple packages based on hardware
I use lxml and some other third party packages that I download and install using their MSI executables.  However, I have long wondered why they specify different processor architectures.

Here is the list of downloads for lxml's 2.3 release

http://pypi.python.org/pypi/lxml/2.3#downloads

I don't see one for Win-Intel64  however, I did find this on the Python download page.

The binaries for AMD64 will also work on processors that implement the Intel 64 architecture (formerly EM64T), i.e. the architecture that Microsoft calls x64, and AMD called x86-64 before calling it AMD64. They will not work on Intel Itanium Processors (formerly IA-64).

So my assumption is that any package with the Win-AMD64 designation will work on the Intel Core line of 64 bit processors but are there edge cases where this is not true?  Any direction to some documentation or explanation about this would be helpful

答:
Simple answer:  if the module/package has machine code as well as Python code, architecture matters, otherwise it does not.

Caveat:  if it is pure Python code, but interfaces with the hardware in some way, then architecture may matter.

Looking at lxml, I see at least two .pyd files, which are machine code.

ContentLicense: CC BY-SA 2.5


问:
Parsing CSV File and Maintaining An Average
For each day compute the average of 'new_cases' of the previous 7 days for every location. (if there are less than 7 days from the current day, calculate the average from the beginning of the first case in that location to the current day)

I'm trying to solve this problem using python. I need to parse a CSV file which has the following information:

date,location,new_cases,new_deaths,total_cases,total_deaths,weekly_cases,weekly_deaths,biweekly_cases,biweekly_deaths

and for each country, on each day compute the average of new_cases from the previous 7 days (or if it hasn't been 7 days yet, then how many ever days it has been so far).

I've been able to mantain a dictionary for each country that contains the new_cases on each day in the CSV. For example: 'China': [{'2019-12-31': '27'}, {'2020-01-01': '0'}, {'2020-01-02': '0'}, {'2020-01-03': '17'}], 'Finland': [{'2019-12-31': '0'}, {'2020-01-01': '0'}, {'2020-01-02': '0'}, {'2020-01-03': '0'}].

I'm struggling on getting to the part where I can compute the average of the previous 7 days for every day for every location. Here is my code so far:

```<code>with open('data.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)

    next(csv_reader)

    country_dict = {}

    for line in csv_reader:
        date = line[0]
        country = line[1]
        new_cases = line[2] if line[2] != '' else 0

        if country not in country_dict:
            country_dict[country] = [{date: new_cases}]
        else:
            country_dict[country].append({date: new_cases})

    print(country_dict)
</code>```

I'm new to python so I'm looking to solve this without any complicated functions / lambdas. Thanks!

答:
I'd say your code does a decent job so far. Your only issue is in calculating the running total. To do that, we just need to make some small adjustments to your existing code. First, instead of using a dictionary to store your data, we should create a class:

```<code>class DateEntry:
    def __init__(self, date, daily_cases, running_total):
        self.date = date
        self.daily_cases = daily_cases
        self.running_total = running_total
</code>```

This will make working with your data much easier. Next, let's inject this code into your calculation and add in the running total calculations:

```<code>
for line in csv_reader:
    date = line[0]
    country = line[1]
    new_cases = int(line[2]) if line[2] != '' else 0

    if country in country_dict:
        country_dict[country].append(DateEntry(date, new_cases, 0))
    else:
        country_dict[country] = [DateEntry(date, new_cases, 0)]

for country, entries in country_dict.items():
    sorted(entries, key = lambda e: e.date)
    for i in range(0, len(entries)):
        entries[i].running_total = sum(
            map(lambda e: e.daily_cases, entries[max(i - 6, 0):i]),
                entries[i].daily_cases) # Calculation step
</code>```

With the exception of me switching the cases on your if-else statement and the inclusion of the usage of the class object, the only change here is the second for-loop. This loop iterates over all the data associated with each country, sorts the entries by oldest to newest (you don't need to do this step if your data is already sorted) and then calculates the running total for each. The calculation step works like this:

entries[max(i - 6, 0):i] retrieves the last six entries (so the last six days of data) or from the beginning of the list if there aren't six entries available.

map(lambda e: e.daily_cases, entries[max(i - 6, 0):i]) converts the list of DateEntry objects to a list of integers where each integer represents the daily cases associated with the entry

sum(map(lambda e: e.daily_cases, entries[max(i - 6, 0):i]), entries[i].daily_cases) adds up all the cases of the last six days and then adds the cases from the current day to that total

Alternatively, if you want to get the same results without all the lambda functions, you could do it like this:

```<code>for country, entries in country_dict.items():
    sorted(entries, key = lambda e: e.date)
    for i in range(0, len(entries)):
        entries[i].running_total = entries[i].daily_cases
        for entry in entries[max(i - 6, 0):i]:
            entries[i].running_total += entry.daily_cases
</code>```

Body:
For each day compute the average of 'new_cases' of the previous 7 days for every location. (if there are less than 7 days from the current day, calculate the average from the beginning of the first case in that location to the current day)

I'm trying to solve this problem using python. I need to parse a CSV file which has the following information:

date,location,new_cases,new_deaths,total_cases,total_deaths,weekly_cases,weekly_deaths,biweekly_cases,biweekly_deaths

and for each country, on each day compute the average of new_cases from the previous 7 days (or if it hasn't been 7 days yet, then how many ever days it has been so far).

I've been able to mantain a dictionary for each country that contains the new_cases on each day in the CSV. For example: 'China': [{'2019-12-31': '27'}, {'2020-01-01': '0'}, {'2020-01-02': '0'}, {'2020-01-03': '17'}], 'Finland': [{'2019-12-31': '0'}, {'2020-01-01': '0'}, {'2020-01-02': '0'}, {'2020-01-03': '0'}].

I'm struggling on getting to the part where I can compute the average of the previous 7 days for every day for every location. Here is my code so far:

with open('data.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)

    next(csv_reader)

    country_dict = {}

    for line in csv_reader:
        date = line[0]
        country = line[1]
        new_cases = line[2] if line[2] != '' else 0

        if country not in country_dict:
            country_dict[country] = [{date: new_cases}]
        else:
            country_dict[country].append({date: new_cases})

    print(country_dict)

I'm new to python so I'm looking to solve this without any complicated functions / lambdas. Thanks!

Body:
I'd say your code does a decent job so far. Your only issue is in calculating the running total. To do that, we just need to make some small adjustments to your existing code. First, instead of using a dictionary to store your data, we should create a class:

class DateEntry:
    def __init__(self, date, daily_cases, running_total):
        self.date = date
        self.daily_cases = daily_cases
        self.running_total = running_total

This will make working with your data much easier. Next, let's inject this code into your calculation and add in the running total calculations:

for line in csv_reader:
    date = line[0]
    country = line[1]
    new_cases = int(line[2]) if line[2] != '' else 0

    if country in country_dict:
        country_dict[country].append(DateEntry(date, new_cases, 0))
    else:
        country_dict[country] = [DateEntry(date, new_cases, 0)]

for country, entries in country_dict.items():
    sorted(entries, key = lambda e: e.date)
    for i in range(0, len(entries)):
        entries[i].running_total = sum(
            map(lambda e: e.daily_cases, entries[max(i - 6, 0):i]),
                entries[i].daily_cases) # Calculation step

With the exception of me switching the cases on your if-else statement and the inclusion of the usage of the class object, the only change here is the second for-loop. This loop iterates over all the data associated with each country, sorts the entries by oldest to newest (you don't need to do this step if your data is already sorted) and then calculates the running total for each. The calculation step works like this:

entries[max(i - 6, 0):i] retrieves the last six entries (so the last six days of data) or from the beginning of the list if there aren't six entries available.

map(lambda e: e.daily_cases, entries[max(i - 6, 0):i]) converts the list of DateEntry objects to a list of integers where each integer represents the daily cases associated with the entry

sum(map(lambda e: e.daily_cases, entries[max(i - 6, 0):i]), entries[i].daily_cases) adds up all the cases of the last six days and then adds the cases from the current day to that total

Alternatively, if you want to get the same results without all the lambda functions, you could do it like this:

for country, entries in country_dict.items():
    sorted(entries, key = lambda e: e.date)
    for i in range(0, len(entries)):
        entries[i].running_total = entries[i].daily_cases
        for entry in entries[max(i - 6, 0):i]:
            entries[i].running_total += entry.daily_cases