问:
Struggling to serialize an xml file using VB.net
I want to store the below data structure in the XML. I am a beginner in VB .net and XML any help would be appreciated

This is an extension of this Question
enter link description here

```<code>Public Class PulserMeasDataPA
<XmlElement()>
Public TestData As List(Of TestData)
End Class

Public Class TestData
Public PortNumber As Integer
<XmlArray("Measurement")>
<XmlArrayItem("MeasureValues")>
Public MeasureValues As List(Of MeasureValues)
End Class

Public Class MeasureValues
Public Amplitude As String
Public Fall As String
Public Width As String
Public setWidth As Decimal
Public PRF As Decimal
Public Volt As Decimal
End Class
</code>```

I want to provide Hard-Coded Data to Serializer so that it can write data to an XML file in my desired location. I want something like this. But this is not working

```<code>        Dim newPulseData As New PulserMeasDataPA()
    Dim listofVpaData As List(Of TestData)
    listofVpaData = New List(Of TestData) From
        {
            New TestData(1, New MeasureValues(1, "2", "3", 40, 1000, 100)),
            New TestData(2, New MeasureValues(1, "2", "3", 40, 1000, 100))
    }
    newPulseData.TestData= listofVpaData
</code>```

XML Serializer Look like this

```<code>    Const OUTPUT_FILENAME As String = "c:\temp\test1.xml"
    Dim writer As XmlWriter = XmlWriter.Create(OUTPUT_FILENAME)
    Dim serializer As XmlSerializer = New XmlSerializer(GetType(PulserMeasDataPA))
    serializer.Serialize(writer, newPulseData)
</code>```

I want to Generate XML Like this from Above Data Structure

```<code><PulserMeasDataPA>
  <TestData>
   <PortNumber>1</PortNumber>
     <Measurement>
     <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>40<setWidth/>
        <PRF>1000<PRF/>
        <Volt>100<Volt/>
    </MeasureValues>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>100<setWidth/>
        <PRF>1000<PRF/>
        <Volt>100<Volt/>
    </MeasureValues>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>40<setWidth/>
        <PRF>5000<PRF/>
        <Volt>50<Volt/>
    </MeasureValues>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>100<setWidth/>
        <PRF>5000<PRF/>
        <Volt>50<Volt/>
    </MeasureValues>
 </Measurement>
</TestData>
<TestData>
 <PortNumber>2</PortNumber>
  <Measurement>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>40<setWidth/>
        <PRF>1000<PRF/>
        <Volt>100<Volt/>
    </MeasureValues>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>100<setWidth/>
        <PRF>1000<PRF/>
        <Volt>100<Volt/>
    </MeasureValues>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>40<setWidth/>
        <PRF>5000<PRF/>
        <Volt>50<Volt/>
    </MeasureValues>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>100<setWidth/>
        <PRF>5000<PRF/>
        <Volt>50<Volt/>
    </MeasureValues>
   </Measurement>
  </TestData>
</code>```

The Above XML is what I want to save from that data structure.  I want to give values to the lists which are in these classes and I want to store that hard-coded values to the new file that it will generate. in this case, some nested lists and classes are used and I am confused about how to give values to these classes which I tried above but failed.

答:
The reason the code you posted doesn't work, is because you haven't created a constructor for each class that accepts parameters.

Class: TestData

Here's a simple definition for the class:

```<code>Public Class TestData

End Class
</code>```

In the above code, since no constructors were defined, the default constructor is implied. If one were to explicitly include the default constructor, we'd have:

```<code>Public Class TestData

    'default constructor
    Public Sub New()

    End Sub

End Class
</code>```

If we were to add a property to the class, and wanted to set the value using using a constructor, we could do the following:

```<code>Public Class TestData
    Public Property PortNumber As Integer

    'default constructor
    Public Sub New()

    End Sub

    'constructor
    Public Sub New(PortNumber as Integer)
        'set property value
        Me.PortNumber = PortNumber
    End Sub

End Class
</code>```

Note: When one defines a constructor, the default constructor is no longer implicitly defined. Therefore, if it's not explicitly defined, as shown above, it won't be available. For XML serialization, a default constructor always needs to be defined.

Usage:

This creates numerous options to set the value of PortNumber. I'll cover the following:

Using the desired Property

Using an Object initializer

Using a constructor

Using a method (Sub)

Option 1 - Using the desired Property

```<code>'create new instance
Dim td as TestData = New TestData()

'set property value
td.PortNumber = 1

Debug.WriteLine("td.PortNumber: " & td.PortNumber.ToString())
</code>```

Option 2 - Using an Object initializer

Note: When using With, we use .PortNumber instead of PortNumber.

```<code>'create new instance and set property value
Dim td As TestData = New TestData() With { .PortNumber = 1 }

Debug.WriteLine("td.PortNumber: " & td.PortNumber.ToString())
</code>```

Option 3 - Using a constructor

```<code>'create new instance and set property value
Dim td as TestData = New TestData(1)

Debug.WriteLine("td.PortNumber: " & td.PortNumber.ToString())
</code>```

Option 4 - Using a method (Sub)

For this option, it's necessary to add a method (Sub) to class "TestData".

```<code>Public Class TestData
    Public Property PortNumber As Integer

    'ToDo: add desired constructor(s)

    Public Sub SetPortNumber(PortNumber As Integer)
        Me.PortNumber = PortNumber
    End Sub
End Class
</code>```

Then we can do the following:
'create new instance
Dim td as TestData = New TestData()

```<code>'set property value
td.SetPortNumber(1)

Debug.WriteLine("td.PortNumber: " & td.PortNumber.ToString())
</code>```

Now, let's look at how to use a nested class.

Create class MeasureValueInfo

Note: I've changed the name of the class from MeasureValues to MeasureValueInfo to make it easier to understand.

MeasureValueInfo:

```<code>Public Class MeasureValueInfo
    Public Property Amplitude As String
    Public Property Fall As String
    Public Property Width As String
    Public Property SetWidth As Decimal
    Public Property PRF As Decimal
    Public Property Volt As Decimal

    Public Sub New()

    End Sub

    Public Sub New(Amplitude As String, Fall As String, Width As String, SetWidth As Decimal, PRF As Decimal, Volt As Decimal)
        'ToDo: add additional parameters and use them to set any additional property values
        Me.Amplitude = Amplitude
        Me.Fall = Fall
        Me.Width = Width
        Me.SetWidth = SetWidth
        Me.PRF = PRF
        Me.Volt = Volt
    End Sub
End Class
</code>```

Let's add a List of MeasureValueInfo to class TestData.

TestData:

```<code>Public Class TestData
    Public Property PortNumber As Integer
    Public Property MeasureValues As List(Of MeasureValueInfo) = New List(Of MeasureValueInfo)

    'default constructor
    Public Sub New()

    End Sub
End Class
</code>```

Let's add a constructor which allows us to set the value of PortNumber and add also add MeasureValueInfo to our List.

```<code>Public Class TestData

    'default constructor
    Public Sub New()

    End Sub

End Class
</code>```0

So, now we'll have:

```<code>Public Class TestData

    'default constructor
    Public Sub New()

    End Sub

End Class
</code>```1

Once again we have multiple ways of setting the property values. I'll demonstrate the following:

Using the desired Property

Using an Object initializer along with a constructor

Usage:

Option 1 - Using the desired Property

```<code>Public Class TestData

    'default constructor
    Public Sub New()

    End Sub

End Class
</code>```2

Option 2a - Using an Object initializer along with a constructor

```<code>Dim td As TestData = New TestData() With {.PortNumber = 1, .MeasureValues = New List(Of MeasureValueInfo) From
    {
        New MeasureValueInfo(Amplitude:="1", Fall:="2", Width:="3", SetWidth:=40, PRF:=1000, Volt:=100),
        New MeasureValueInfo(Amplitude:="1", Fall:="2", Width:="3", SetWidth:=100, PRF:=1000, Volt:=100)
    }}

Debug.WriteLine(String.Format("td.PortNumber: {0}", td.PortNumber.ToString()))

For Each mVal As MeasureValueInfo In td.MeasureValues
    'ToDo: add properties to output
    Debug.WriteLine(String.Format("    mVal.Amplitude: {0} mVal.SetWidth: {1}", mVal.Amplitude.ToString(), mVal.SetWidth.ToString()))
Next
</code>```

Option 2b - Using an Object initializer along with a constructor

```<code>Dim td As TestData = New TestData() With {.PortNumber = 1, .MeasureValues = New List(Of MeasureValueInfo) From
    {
        New MeasureValueInfo("1", "2", "3", 40, 1000, 100),
        New MeasureValueInfo("1", "2", "3", 100, 1000, 100)
    }}

Debug.WriteLine(String.Format("td.PortNumber: {0}", td.PortNumber.ToString()))

For Each mVal As MeasureValueInfo In td.MeasureValues
    'ToDo: add properties to output
    Debug.WriteLine(String.Format("    mVal.Amplitude: {0} mVal.SetWidth: {1}", mVal.Amplitude.ToString(), mVal.SetWidth.ToString()))
Next
</code>```

In order to use the classes with XML serialization,

Add the following Imports statements:

Imports System.Xml

Imports System.Xml.Serialization

Then modify the classes so they look like the following:

Note: The code for the classes was adapted from the OP which was given as an answer in this post. The code was changed from using fields to using properties, initialization was added for the Lists, and constructors were added.

```<code>Public Class PulserMeasDataPA
    <XmlElement()>
    Public Property TestData As New List(Of TestData)
End Class

Public Class TestData
    Public Property PortNumber As Integer

    <XmlArray("Measurement")>
    <XmlArrayItem("MeasureValues")>
    Public Property MeasureValues As New List(Of MeasureValueInfo)

    Public Sub New()

    End Sub

    Public Sub New(PortNumber As Integer, measureVal As MeasureValueInfo)
        'set property value
        Me.PortNumber = PortNumber
        Me.MeasureValues.Add(measureVal)
    End Sub
End Class

Public Class MeasureValueInfo
    Public Property Amplitude As String
    Public Property Fall As String
    Public Property Width As String
    Public Property SetWidth As Decimal
    Public Property PRF As Decimal
    Public Property Volt As Decimal

    Public Sub New()

    End Sub

    Public Sub New(Amplitude As String, Fall As String, Width As String, SetWidth As Decimal, PRF As Decimal, Volt As Decimal)
        'ToDo: add additional parameters and use them to set any additional property values
        Me.Amplitude = Amplitude
        Me.Fall = Fall
        Me.Width = Width
        Me.SetWidth = SetWidth
        Me.PRF = PRF
        Me.Volt = Volt
    End Sub
End Class
</code>```

Resources:

serialization of xml file in VB.net

Objects and classes in Visual Basic

Walkthrough: Defining Classes (Visual Basic)

How to: Declare an Object by Using an Object Initializer (Visual Basic)

Properties vs Fields – Why Does it Matter?

Auto-Implemented Properties (Visual Basic)

Body:
I want to store the below data structure in the XML. I am a beginner in VB .net and XML any help would be appreciated

This is an extension of this Question
enter link description here

Public Class PulserMeasDataPA
<XmlElement()>
Public TestData As List(Of TestData)
End Class

Public Class TestData
Public PortNumber As Integer
<XmlArray("Measurement")>
<XmlArrayItem("MeasureValues")>
Public MeasureValues As List(Of MeasureValues)
End Class

Public Class MeasureValues
Public Amplitude As String
Public Fall As String
Public Width As String
Public setWidth As Decimal
Public PRF As Decimal
Public Volt As Decimal
End Class

I want to provide Hard-Coded Data to Serializer so that it can write data to an XML file in my desired location. I want something like this. But this is not working

        Dim newPulseData As New PulserMeasDataPA()
    Dim listofVpaData As List(Of TestData)
    listofVpaData = New List(Of TestData) From
        {
            New TestData(1, New MeasureValues(1, "2", "3", 40, 1000, 100)),
            New TestData(2, New MeasureValues(1, "2", "3", 40, 1000, 100))
    }
    newPulseData.TestData= listofVpaData

XML Serializer Look like this

    Const OUTPUT_FILENAME As String = "c:\temp\test1.xml"
    Dim writer As XmlWriter = XmlWriter.Create(OUTPUT_FILENAME)
    Dim serializer As XmlSerializer = New XmlSerializer(GetType(PulserMeasDataPA))
    serializer.Serialize(writer, newPulseData)

I want to Generate XML Like this from Above Data Structure

<PulserMeasDataPA>
  <TestData>
   <PortNumber>1</PortNumber>
     <Measurement>
     <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>40<setWidth/>
        <PRF>1000<PRF/>
        <Volt>100<Volt/>
    </MeasureValues>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>100<setWidth/>
        <PRF>1000<PRF/>
        <Volt>100<Volt/>
    </MeasureValues>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>40<setWidth/>
        <PRF>5000<PRF/>
        <Volt>50<Volt/>
    </MeasureValues>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>100<setWidth/>
        <PRF>5000<PRF/>
        <Volt>50<Volt/>
    </MeasureValues>
 </Measurement>
</TestData>
<TestData>
 <PortNumber>2</PortNumber>
  <Measurement>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>40<setWidth/>
        <PRF>1000<PRF/>
        <Volt>100<Volt/>
    </MeasureValues>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>100<setWidth/>
        <PRF>1000<PRF/>
        <Volt>100<Volt/>
    </MeasureValues>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>40<setWidth/>
        <PRF>5000<PRF/>
        <Volt>50<Volt/>
    </MeasureValues>
    <MeasureValues>
        <Amplitude></Amplitude>
        <Fall></Fall>
        <Width></Width>
        <setWidth>100<setWidth/>
        <PRF>5000<PRF/>
        <Volt>50<Volt/>
    </MeasureValues>
   </Measurement>
  </TestData>

The Above XML is what I want to save from that data structure.  I want to give values to the lists which are in these classes and I want to store that hard-coded values to the new file that it will generate. in this case, some nested lists and classes are used and I am confused about how to give values to these classes which I tried above but failed.

Body:
The reason the code you posted doesn't work, is because you haven't created a constructor for each class that accepts parameters.

Class: TestData

Here's a simple definition for the class:

Public Class TestData

End Class

In the above code, since no constructors were defined, the default constructor is implied. If one were to explicitly include the default constructor, we'd have:

Public Class TestData

    'default constructor
    Public Sub New()

    End Sub

End Class

If we were to add a property to the class, and wanted to set the value using using a constructor, we could do the following:

Public Class TestData
    Public Property PortNumber As Integer

    'default constructor
    Public Sub New()

    End Sub

    'constructor
    Public Sub New(PortNumber as Integer)
        'set property value
        Me.PortNumber = PortNumber
    End Sub

End Class

Note: When one defines a constructor, the default constructor is no longer implicitly defined. Therefore, if it's not explicitly defined, as shown above, it won't be available. For XML serialization, a default constructor always needs to be defined.

Usage:

This creates numerous options to set the value of PortNumber. I'll cover the following:

Using the desired Property

Using an Object initializer

Using a constructor

Using a method (Sub)

Option 1 - Using the desired Property

'create new instance
Dim td as TestData = New TestData()

'set property value
td.PortNumber = 1

Debug.WriteLine("td.PortNumber: " & td.PortNumber.ToString())

Option 2 - Using an Object initializer

Note: When using With, we use .PortNumber instead of PortNumber.

'create new instance and set property value
Dim td As TestData = New TestData() With { .PortNumber = 1 }

Debug.WriteLine("td.PortNumber: " & td.PortNumber.ToString())

Option 3 - Using a constructor

'create new instance and set property value
Dim td as TestData = New TestData(1)

Debug.WriteLine("td.PortNumber: " & td.PortNumber.ToString())

Option 4 - Using a method (Sub)

For this option, it's necessary to add a method (Sub) to class "TestData".

Public Class TestData
    Public Property PortNumber As Integer

    'ToDo: add desired constructor(s)

    Public Sub SetPortNumber(PortNumber As Integer)
        Me.PortNumber = PortNumber
    End Sub
End Class

Then we can do the following:
'create new instance
Dim td as TestData = New TestData()

'set property value
td.SetPortNumber(1)

Debug.WriteLine("td.PortNumber: " & td.PortNumber.ToString())

Now, let's look at how to use a nested class.

Create class MeasureValueInfo

Note: I've changed the name of the class from MeasureValues to MeasureValueInfo to make it easier to understand.

MeasureValueInfo:

Public Class MeasureValueInfo
    Public Property Amplitude As String
    Public Property Fall As String
    Public Property Width As String
    Public Property SetWidth As Decimal
    Public Property PRF As Decimal
    Public Property Volt As Decimal

    Public Sub New()

    End Sub

    Public Sub New(Amplitude As String, Fall As String, Width As String, SetWidth As Decimal, PRF As Decimal, Volt As Decimal)
        'ToDo: add additional parameters and use them to set any additional property values
        Me.Amplitude = Amplitude
        Me.Fall = Fall
        Me.Width = Width
        Me.SetWidth = SetWidth
        Me.PRF = PRF
        Me.Volt = Volt
    End Sub
End Class

Let's add a List of MeasureValueInfo to class TestData.

TestData:

Public Class TestData
    Public Property PortNumber As Integer
    Public Property MeasureValues As List(Of MeasureValueInfo) = New List(Of MeasureValueInfo)

    'default constructor
    Public Sub New()

    End Sub
End Class

Let's add a constructor which allows us to set the value of PortNumber and add also add MeasureValueInfo to our List.

'constructor
Public Sub New(PortNumber As Integer, measureValue As MeasureValueInfo)
    'set property value
    Me.PortNumber = PortNumber
    Me.MeasureValues.Add(measureValue) 'add
End Sub

So, now we'll have:

Public Class TestData
    Public Property PortNumber As Integer
    Public Property MeasureValues As List(Of MeasureValueInfo) = New List(Of MeasureValueInfo)

    'default constructor
    Public Sub New()

    End Sub

    'constructor
    Public Sub New(PortNumber As Integer, measureVal As MeasureValueInfo)
        'set property value
        Me.PortNumber = PortNumber
        Me.MeasureValues.Add(measureVal)
    End Sub
End Class

Once again we have multiple ways of setting the property values. I'll demonstrate the following:

Using the desired Property

Using an Object initializer along with a constructor

Usage:

Option 1 - Using the desired Property

'create new instance
Dim td as TestData = New TestData()
td.PortNumber = 1

Dim mvInfo As MeasureValueInfo = Nothing

'create new instance
mvInfo = New MeasureValueInfo()

'set property values
mvInfo.Amplitude = "1"
mvInfo.Fall = "2"
mvInfo.Width = "3"
mvInfo.SetWidth = 40
mvInfo.PRF = 1000
mvInfo.Volt = 100

'add
td.MeasureValues.Add(mvInfo)

'create new instance
mvInfo = New MeasureValueInfo()

'set property values
mvInfo.Amplitude = "1"
mvInfo.Fall = "2"
mvInfo.Width = "3"
mvInfo.SetWidth = 100
mvInfo.PRF = 1000
mvInfo.Volt = 100

'add
td.MeasureValues.Add(mvInfo)

Debug.WriteLine(String.Format("td.PortNumber: {0}", td.PortNumber.ToString()))

For Each mVal As MeasureValueInfo In td.MeasureValues
    'ToDo: add properties to output
    Debug.WriteLine(String.Format("    mVal.Amplitude: {0} mVal.SetWidth: {1}", mVal.Amplitude.ToString(), mVal.SetWidth.ToString()))
Next

Option 2a - Using an Object initializer along with a constructor

Dim td As TestData = New TestData() With {.PortNumber = 1, .MeasureValues = New List(Of MeasureValueInfo) From
    {
        New MeasureValueInfo(Amplitude:="1", Fall:="2", Width:="3", SetWidth:=40, PRF:=1000, Volt:=100),
        New MeasureValueInfo(Amplitude:="1", Fall:="2", Width:="3", SetWidth:=100, PRF:=1000, Volt:=100)
    }}

Debug.WriteLine(String.Format("td.PortNumber: {0}", td.PortNumber.ToString()))

For Each mVal As MeasureValueInfo In td.MeasureValues
    'ToDo: add properties to output
    Debug.WriteLine(String.Format("    mVal.Amplitude: {0} mVal.SetWidth: {1}", mVal.Amplitude.ToString(), mVal.SetWidth.ToString()))
Next

Option 2b - Using an Object initializer along with a constructor

Dim td As TestData = New TestData() With {.PortNumber = 1, .MeasureValues = New List(Of MeasureValueInfo) From
    {
        New MeasureValueInfo("1", "2", "3", 40, 1000, 100),
        New MeasureValueInfo("1", "2", "3", 100, 1000, 100)
    }}

Debug.WriteLine(String.Format("td.PortNumber: {0}", td.PortNumber.ToString()))

For Each mVal As MeasureValueInfo In td.MeasureValues
    'ToDo: add properties to output
    Debug.WriteLine(String.Format("    mVal.Amplitude: {0} mVal.SetWidth: {1}", mVal.Amplitude.ToString(), mVal.SetWidth.ToString()))
Next

In order to use the classes with XML serialization,

Add the following Imports statements:

Imports System.Xml

Imports System.Xml.Serialization

Then modify the classes so they look like the following:

Note: The code for the classes was adapted from the OP which was given as an answer in this post. The code was changed from using fields to using properties, initialization was added for the Lists, and constructors were added.

Public Class PulserMeasDataPA
    <XmlElement()>
    Public Property TestData As New List(Of TestData)
End Class

Public Class TestData
    Public Property PortNumber As Integer

    <XmlArray("Measurement")>
    <XmlArrayItem("MeasureValues")>
    Public Property MeasureValues As New List(Of MeasureValueInfo)

    Public Sub New()

    End Sub

    Public Sub New(PortNumber As Integer, measureVal As MeasureValueInfo)
        'set property value
        Me.PortNumber = PortNumber
        Me.MeasureValues.Add(measureVal)
    End Sub
End Class

Public Class MeasureValueInfo
    Public Property Amplitude As String
    Public Property Fall As String
    Public Property Width As String
    Public Property SetWidth As Decimal
    Public Property PRF As Decimal
    Public Property Volt As Decimal

    Public Sub New()

    End Sub

    Public Sub New(Amplitude As String, Fall As String, Width As String, SetWidth As Decimal, PRF As Decimal, Volt As Decimal)
        'ToDo: add additional parameters and use them to set any additional property values
        Me.Amplitude = Amplitude
        Me.Fall = Fall
        Me.Width = Width
        Me.SetWidth = SetWidth
        Me.PRF = PRF
        Me.Volt = Volt
    End Sub
End Class

Resources:

serialization of xml file in VB.net

Objects and classes in Visual Basic

Walkthrough: Defining Classes (Visual Basic)

How to: Declare an Object by Using an Object Initializer (Visual Basic)

Properties vs Fields – Why Does it Matter?

Auto-Implemented Properties (Visual Basic)


来源: stackexchange

问:
Getting this SQL Statement right with aliases
what i basically want to do is: compute the difference of to attributes and then get the row with the biggest difference.

So, here is what i tried:

```<code>SELECT caller_aor, (end_time - start_time) as duration
    FROM cdrs
    GROUP BY caller_aor
    HAVING duration = (SELECT MAX(end_time - start_time) FROM cdrs);
</code>```

duration in the HAVING clause gives an error but i cant figure out what i have to do there.

Help is appreciated.

答:
try this query</p>

```<code>SELECT top 1 caller_aor, Max(end_time - start_time) as duration
FROM cdrs
GROUP BY caller_aor
Order by duration
</code>```

ContentLicense: CC BY-SA 3.0

Body:
what i basically want to do is: compute the difference of to attributes and then get the row with the biggest difference.

So, here is what i tried:

SELECT caller_aor, (end_time - start_time) as duration
    FROM cdrs
    GROUP BY caller_aor
    HAVING duration = (SELECT MAX(end_time - start_time) FROM cdrs);

duration in the HAVING clause gives an error but i cant figure out what i have to do there.

Help is appreciated.

Body:
try this query

SELECT top 1 caller_aor, Max(end_time - start_time) as duration
FROM cdrs
GROUP BY caller_aor
Order by duration


问:
How to control the height of geom_tile()
With this dataframe:

```<code>df <- structure(list(region = c("AA", "BB", "CC", "DD"), event1 = c("XXX",
"XXX", "XXX", "XXX"), value = c(3, 1, 2, 2)), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))

  region event1 value
  <chr>  <chr>  <dbl>
1 AA     XXX        3
2 BB     XXX        1
3 CC     XXX        2
4 DD     XXX        2
</code>```

Using this code:

```<code>ggplot(df, aes(x = region, y = event1,  fill = value)) +
  geom_tile(height=1, colour="white", size=0.5) +
  geom_text(aes(label = value), color = "grey20", size = 5) +
  scale_fill_gradientn(colours=  c("green","white","red"),, na.value = "transparent",
                       breaks=c(1,2,3,4),
                       labels = c("1 - A", "2 - B", "3 - C", "4 - D"),
                       limits=c(0.5,4.5))
</code>```

I get this plot:

My desired output:

How can I modify the code to control the height of the tiles?

答:
One option to achieve your desired result would be to increase the expansion of the scale:

library(ggplot2)

ggplot(df, aes(x = region, y = event1,  fill = value)) +
  geom_tile(height=1, colour="white", size=0.5) +
  geom_text(aes(label = value), color = "grey20", size = 5) +
  scale_y_discrete(expand = expansion(add = c(.6, 3))) +
  scale_fill_gradientn(colours=  c("green","white","red"),, na.value = "transparent",
                       breaks=c(1,2,3,4),
                       labels = c("1 - A", "2 - B", "3 - C", "4 - D"),
                       limits=c(0.5,4.5))
```

ContentLicense: CC BY-SA 4.0

Body:
With this dataframe:

df <- structure(list(region = c("AA", "BB", "CC", "DD"), event1 = c("XXX",
"XXX", "XXX", "XXX"), value = c(3, 1, 2, 2)), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))

  region event1 value
  <chr>  <chr>  <dbl>
1 AA     XXX        3
2 BB     XXX        1
3 CC     XXX        2
4 DD     XXX        2

Using this code:

ggplot(df, aes(x = region, y = event1,  fill = value)) +
  geom_tile(height=1, colour="white", size=0.5) +
  geom_text(aes(label = value), color = "grey20", size = 5) +
  scale_fill_gradientn(colours=  c("green","white","red"),, na.value = "transparent",
                       breaks=c(1,2,3,4),
                       labels = c("1 - A", "2 - B", "3 - C", "4 - D"),
                       limits=c(0.5,4.5))

I get this plot:

My desired output:

How can I modify the code to control the height of the tiles?


问:
Which alignment causes this performance difference

What's the problem

I am benchmarking the following code for (T& x : v) x = x + x; where T is int.
When compiling with mavx2 Performance fluctuates 2 times depending on some conditions.
This does not reproduce on sse4.2

I would like to understand what's happening.

How does the benchmark work

I am using Google Benchmark. It spins the loop until the point it is sure about the time.

The main benchmarking code:

```<code>using T = int;
constexpr std::size_t size = 10'000 / sizeof(T);

NOINLINE std::vector<T> const& data()
{
    static std::vector<T> res(size, T{2});
    return res;
}

INLINE void double_elements_bench(benchmark::State& state)
{
   auto v = data();

   for (auto _ : state) {
       for (T& x : v) x = x + x;
       benchmark::DoNotOptimize(v.data());
   }
}
</code>```

Then I call double_elements_bench from multiple instances of a benchmark driver.

Machine, Compiler, Options

processor: intel 9700k

compiler: clang ~14, built from trunk.

options: -mavx2 --std=c++20 --stdlib=libc++ -DNDEBUG -g -Werror -Wall -Wextra -Wpedantic -Wno-deprecated-copy -O3

I did align all functions to 128 to try, had no effect.

Results

When duplicated 2 times I get:

```<code>------------------------------------------------------------
Benchmark                  Time             CPU   Iterations
------------------------------------------------------------
double_elements_0        105 ns          105 ns      6617708
double_elements_1        105 ns          105 ns      6664185
</code>```

Vs duplicated 3 times:

```<code>------------------------------------------------------------
Benchmark                  Time             CPU   Iterations
------------------------------------------------------------
double_elements_0       64.6 ns         64.6 ns     10867663
double_elements_1       64.5 ns         64.5 ns     10855206
double_elements_2       64.5 ns         64.5 ns     10868602
</code>```

This reproduces on bigger data sizes too.

Perf stats

I looked for counters that I know can be relevant to code alignment

LSD cache (which is off on my machine due to some security issue a few years back), DSB cache and branch predictor:

LSD.UOPS,idq.dsb_uops,UOPS_ISSUED.ANY,branches,branch-misses

Slow case

```<code>------------------------------------------------------------
Benchmark                  Time             CPU   Iterations
------------------------------------------------------------
double_elements_0        105 ns          105 ns      6663885
double_elements_1        105 ns          105 ns      6632218

 Performance counter stats for './transform_alignment_issue':

                 0      LSD.UOPS
    13,830,353,682      idq.dsb_uops
    16,273,127,618      UOPS_ISSUED.ANY
       761,742,872      branches
            34,107      branch-misses             #    0.00% of all branches

       1.652348280 seconds time elapsed

       1.633691000 seconds user
       0.000000000 seconds sys
</code>```

Fast case

```<code>------------------------------------------------------------
Benchmark                  Time             CPU   Iterations
------------------------------------------------------------
double_elements_0       64.5 ns         64.5 ns     10861602
double_elements_1       64.5 ns         64.5 ns     10855668
double_elements_2       64.4 ns         64.4 ns     10867987

 Performance counter stats for './transform_alignment_issue':

                 0      LSD.UOPS
    32,007,061,910      idq.dsb_uops
    37,653,791,549      UOPS_ISSUED.ANY
     1,761,491,679      branches
            37,165      branch-misses             #    0.00% of all branches

       2.335982395 seconds time elapsed

       2.317019000 seconds user
       0.000000000 seconds sys
</code>```

Both look to me about the same.

Code: https://github.com/DenisYaroshevskiy/small_benchmarks/blob/ade1ed42fc2113f5ad0a4313dafff5a81f9a0d20/transform_alignment_issue.cc#L1

UPD

I think this might be alignment of the data returned from malloc

0x4f2720 in fast case and
0x8e9310 in slow

So - since clang does not align - we get unaligned reads/writes.
I tested on a transform that aligns - does not seem to have this variation.

Is there a way to confirm it?

答:
Yes, data misalignment could explain your 2x slowdown for small arrays that fit in L1d.  You'd hope that with every other load/store being a cache-line split, it might only slow down by a factor of 1.5x, not 2, if a split load or  store cost 2 accesses to L1d instead of 1.

But it has extra effects like replays of uops dependent on the load result that apparently account for the rest of the problem, either making out-of-order exec less able to overlap work and hide latency, or directly running into bottlenecks like "split registers".

ld_blocks.no_sr counts number of times cache-line split loads are temporarily blocked because all resources for handling the split accesses are in use.

When a load execution unit detects that the load splits across a cache line, it has to save the first part somewhere (apparently in a "split register") and then access the 2nd cache line.  On Intel SnB-family CPUs like yours, this 2nd access doesn't require the RS to dispatch the load uop to the port again; the load execution unit just does it a few cycles later.  (But presumably can't accept another load in the same cycle as that 2nd access.)

https://chat.stackoverflow.com/transcript/message/48426108#48426108 - uops waiting for the result of a cache-split load will get replayed.

Are load ops deallocated from the RS when they dispatch, complete or some other time? But the load itself can leave the RS earlier.

How can I accurately benchmark unaligned access speed on x86_64? general stuff on split load penalties.

The extra latency of split loads, and also the potential replays of uops waiting for those loads results, is another factor, but those are also fairly direct consequences of misaligned loads.  Lots of counts for ld_blocks.no_sr tells you that the CPU actually ran out of split registers and could otherwise be doing more work, but had to stall because of the unaligned load itself, not just other effects.

You could also look for the front-end stalling due to the ROB or RS being full, if you want to investigate the details, but not being able to execute split loads will make that happen more.  So probably all the back-end stalling is a consequence of the unaligned loads (and maybe stores if commit from store buffer to L1d is also a bottleneck.)

On a 100KB I reproduce the issue: 1075ns vs 1412ns. On 1 MB I don't think I see it.

Data alignment doesn't normally make that much difference for large arrays (except with 512-bit vectors).  With a cache line (2x YMM vectors) arriving less frequently, the back-end has time to work through the extra overhead of unaligned loads / stores and still keep up.  HW prefetch does a good enough job that it can still max out the per-core L3 bandwidth.  Seeing a smaller effect for a size that fits in L2 but not L1d (like 100kiB) is expected.

Of course, most kinds of execution bottlenecks would show similar effects, even something as simple as un-optimized code that does some extra store/reloads for each vector of array data.  So this alone doesn't prove that it was misalignment causing the slowdowns for small sizes that do fit in L1d, like your 10 KiB.  But that's clearly the most sensible conclusion.

Code alignment or other front-end bottlenecks seem not to be the problem; most of your uops are coming from the DSB, according to idq.dsb_uops.  (A significant number aren't, but not a big percentage difference between slow vs. fast.)

How can I mitigate the impact of the Intel jcc erratum on gcc? can be important on Skylake-derived microarchitectures like yours; it's even possible that's why your idq.dsb_uops isn't closer to your uops_issued.any.

Body:
What's the problem

I am benchmarking the following code for (T& x : v) x = x + x; where T is int.
When compiling with mavx2 Performance fluctuates 2 times depending on some conditions.
This does not reproduce on sse4.2

I would like to understand what's happening.

How does the benchmark work

I am using Google Benchmark. It spins the loop until the point it is sure about the time.

The main benchmarking code:

using T = int;
constexpr std::size_t size = 10'000 / sizeof(T);

NOINLINE std::vector<T> const& data()
{
    static std::vector<T> res(size, T{2});
    return res;
}

INLINE void double_elements_bench(benchmark::State& state)
{
   auto v = data();

   for (auto _ : state) {
       for (T& x : v) x = x + x;
       benchmark::DoNotOptimize(v.data());
   }
}

Then I call double_elements_bench from multiple instances of a benchmark driver.

Machine, Compiler, Options

processor: intel 9700k

compiler: clang ~14, built from trunk.

options: -mavx2 --std=c++20 --stdlib=libc++ -DNDEBUG -g -Werror -Wall -Wextra -Wpedantic -Wno-deprecated-copy -O3

I did align all functions to 128 to try, had no effect.

Results

When duplicated 2 times I get:

------------------------------------------------------------
Benchmark                  Time             CPU   Iterations
------------------------------------------------------------
double_elements_0        105 ns          105 ns      6617708
double_elements_1        105 ns          105 ns      6664185

Vs duplicated 3 times:

------------------------------------------------------------
Benchmark                  Time             CPU   Iterations
------------------------------------------------------------
double_elements_0       64.6 ns         64.6 ns     10867663
double_elements_1       64.5 ns         64.5 ns     10855206
double_elements_2       64.5 ns         64.5 ns     10868602

This reproduces on bigger data sizes too.

Perf stats

I looked for counters that I know can be relevant to code alignment

LSD cache (which is off on my machine due to some security issue a few years back), DSB cache and branch predictor:

LSD.UOPS,idq.dsb_uops,UOPS_ISSUED.ANY,branches,branch-misses

Slow case

------------------------------------------------------------
Benchmark                  Time             CPU   Iterations
------------------------------------------------------------
double_elements_0        105 ns          105 ns      6663885
double_elements_1        105 ns          105 ns      6632218

 Performance counter stats for './transform_alignment_issue':

                 0      LSD.UOPS
    13,830,353,682      idq.dsb_uops
    16,273,127,618      UOPS_ISSUED.ANY
       761,742,872      branches
            34,107      branch-misses             #    0.00% of all branches

       1.652348280 seconds time elapsed

       1.633691000 seconds user
       0.000000000 seconds sys

Fast case

------------------------------------------------------------
Benchmark                  Time             CPU   Iterations
------------------------------------------------------------
double_elements_0       64.5 ns         64.5 ns     10861602
double_elements_1       64.5 ns         64.5 ns     10855668
double_elements_2       64.4 ns         64.4 ns     10867987

 Performance counter stats for './transform_alignment_issue':

                 0      LSD.UOPS
    32,007,061,910      idq.dsb_uops
    37,653,791,549      UOPS_ISSUED.ANY
     1,761,491,679      branches
            37,165      branch-misses             #    0.00% of all branches

       2.335982395 seconds time elapsed

       2.317019000 seconds user
       0.000000000 seconds sys

Both look to me about the same.

Code: https://github.com/DenisYaroshevskiy/small_benchmarks/blob/ade1ed42fc2113f5ad0a4313dafff5a81f9a0d20/transform_alignment_issue.cc#L1

UPD

I think this might be alignment of the data returned from malloc

0x4f2720 in fast case and
0x8e9310 in slow

So - since clang does not align - we get unaligned reads/writes.
I tested on a transform that aligns - does not seem to have this variation.

Is there a way to confirm it?


问:
Show GIF animation while time.sleep() in Flask
I want to display a GIF-File in a webpage while in the background (Flask-Application) is a time.sleep() function. This is my code:
app.py

```<code>from flask import Flask

app = Flask(__name__)

@app.route("/")
def test():
    #Do something
    time.sleep(2)
    #Do something different

    return render_template("template.html")

app.run()
</code>```

template.html:

```<code><title>Test</title>
<p>Finally loaded</p>
</code>```

My goal is that while the 2 seconds waiting a GIF-File should be shown. I tried this:

```<code><div id="preloader"></div>
<style>
  #preloader{
    background: white url(/static/animation.gif) no-repeat center center;
    background-size: 15%;
    height: 100vh;
    width: 100%;
    position: center;
    z-index: 1000;
    margin: auto;
    cursor: wait;
  }
</style>
<script>
  var loader = document.getElementById("preloader");
  window.addEventListener("load", function(){
    loader.style.display = "none"
  })
  window.onload = preloader;
</script>
<p>Done</p>
</code>```

..., but the gif file is only displayed (for a short time) after the countdown. Can anybody please help me?

答:
You cannot do this using only one endpoint because the time.sleep(2) blocks the response, so you only see the loading indicator after the sleep phase is finished. You need a separate endpoint for the task.

With some javascript we can call the task endpoint after the base page is loaded and during the request we can show the loading indicator. I'm using HTMX library here for the demonstration.

```<code>@app.route("/")
def index():
    # Base page that calls the task endpoint.
    return render_template("index.html")

@app.route("/task")
def task():
    time.sleep(2)
    return '<span>Done</span>'
</code>```

index.html base template:

```<code><script src="https://unpkg.com/htmx.org@1.6.1"></script>
<div hx-get="/task" hx-trigger="load">
    <img class="htmx-indicator" src="/static/animation.gif" />
</div>
</code>```

The two hx- attributes tells HTMX to load the /task endpoint on page load event and replace the innerHTML of the div element with the response. During the request, the indicator image will be visible, then it will be replaced with the response from /task.

Body:
I want to display a GIF-File in a webpage while in the background (Flask-Application) is a time.sleep() function. This is my code:
app.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def test():
    #Do something
    time.sleep(2)
    #Do something different

    return render_template("template.html")

app.run()

template.html:

<title>Test</title>

Finally loaded

My goal is that while the 2 seconds waiting a GIF-File should be shown. I tried this:

Done

..., but the gif file is only displayed (for a short time) after the countdown. Can anybody please help me?

Body:
You cannot do this using only one endpoint because the time.sleep(2) blocks the response, so you only see the loading indicator after the sleep phase is finished. You need a separate endpoint for the task.

With some javascript we can call the task endpoint after the base page is loaded and during the request we can show the loading indicator. I'm using HTMX library here for the demonstration.

@app.route("/")
def index():
    # Base page that calls the task endpoint.
    return render_template("index.html")

@app.route("/task")
def task():
    time.sleep(2)
    return '<span>Done</span>'

index.html base template:

<script src="https://unpkg.com/htmx.org@1.6.1"></script>
<div hx-get="/task" hx-trigger="load">
    <img class="htmx-indicator" src="/static/animation.gif" />
</div>

The two hx- attributes tells HTMX to load the /task endpoint on page load event and replace the innerHTML of the div element with the response. During the request, the indicator image will be visible, then it will be replaced with the response from /task.


问:
Export Table out of Access Database to Dynamic Folder Directory
I have a table in my Access data base that I would like to export to a folder directory which is updated manually according to the following methodology (YYYY is the higher-level folder, YYYYMM is the sub-folder I would like to export the report to):

2021:

202101

(...)

202112

2022

202201

(...)

So far, I have the below piece of code to add to a button in my form. One way to avoid pressing it in a later month and then having it copied to the wrong subfolder could be to go via the database name itself (Currentdb.name) as this will be named exactly in line (202112 etc.). Using the below, the report is placed in the same folder as the high-level years folder (among 2021, 2022).

```<code>Private Sub Command3_Click()
Dim reportname As String
Dim theFilePath As String
reportname = "List"
theFilePath = “\\xxx\groupshares\xxx\”
theFilePath = theFilePath & reportname & "_" & Format(Date, "yyyy-mm-dd") & ".xls"

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, reportname, theFilePath, True
MsgBox "Look in your files for the report."
End Sub
</code>```

答:
If the folders are pre-created, would changing:

```<code>TheFilePath = “\\xxx\groupshares\xxx\”

to

TheFilePath = “\\xxx\groupshares\xxx\” & format(Date(),"YYYY") & "\" format(date(),"yyyymm") & "\"
</code>```

do it?

Body:
I have a table in my Access data base that I would like to export to a folder directory which is updated manually according to the following methodology (YYYY is the higher-level folder, YYYYMM is the sub-folder I would like to export the report to):

2021:

202101

(...)

202112

2022

202201

(...)

So far, I have the below piece of code to add to a button in my form. One way to avoid pressing it in a later month and then having it copied to the wrong subfolder could be to go via the database name itself (Currentdb.name) as this will be named exactly in line (202112 etc.). Using the below, the report is placed in the same folder as the high-level years folder (among 2021, 2022).

Private Sub Command3_Click()
Dim reportname As String
Dim theFilePath As String
reportname = "List"
theFilePath = “\\xxx\groupshares\xxx\”
theFilePath = theFilePath & reportname & "_" & Format(Date, "yyyy-mm-dd") & ".xls"

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, reportname, theFilePath, True
MsgBox "Look in your files for the report."
End Sub

Body:
If the folders are pre-created, would changing:

TheFilePath = “\\xxx\groupshares\xxx\”

to

TheFilePath = “\\xxx\groupshares\xxx\” & format(Date(),"YYYY") & "\" format(date(),"yyyymm") & "\"

do it?


问:
LoadLibrary fails when including a specific file during DLL build
I'm getting really strange behavior in one of the DLLs of my C++ app. It works and loads fine until I include a single file using #include in the main file of the DLL. I then get this error message:

Loading components from D:/Targets/bin/MatrixWorkset.dll
Could not load "D:/Targets/bin/MatrixWorkset.dll": Cannot load library MatrixWorkset: Invalid access to memory location.

Now I've searched and searched through the code and google and I can't figure out what is going on. Up till now everything was in a single DLL and I've decided to split it into two smaller ones. The file that causes the problems is part of the other second library (which loads fine).

Any ideas would really be appreciated.
Thanks,
Jaco

答:
The error you describe sounds like a run-time error. Is this error displayed automatically by windows or is it one that your program emits?

I say attach a debugger to your application and trace where this error is coming from. Is Windows failing to load a dependency? Is your library somehow failing on load-up?

If you want to rule in/out this header file you're including, try pre-compiling your main source file both with and without this #include and diff the two results.

OwnerDisplayName: Jaco

ContentLicense: CC BY-SA 2.5

OwnerDisplayName: veefu


问:
Replace items in array with elements from a different set
I'm not sure the proper way to go about this recursively.
Let's say I have two arrays

```<code>array1 = [a, null, c, d, e]

array2 = [1, 2, 3]
</code>```

I want to create a resulting array of the form

```<code>[
   [a, 1, c, d, e],
   [a, 2, c, d, e],
   [a, 3, c, d, e]
]
</code>```

where each element of the second array fills in the place of null. I was able to do this just fine, but what if I have two cases of null?

```<code>array1 = [a, null, c, null, e]

array2 = [1, 2, 3]
</code>```

how do I get the following?

```<code>[
   [a, 1, c, 1, e],
   [a, 1, c, 2, e],
   [a, 1, c, 3, e],
   [a, 2, c, 1, e],
   ...
   [a, 3, c, 3, e]
]
</code>```

答:
Code on Javascript:

let array1 = ['a', null, 'c', null, 'e']
let array2 = [1, 2, 3]

let output = []
let cursorPosition = 0; // cursorPosition is a pointer for `array2` index
let nullPositions = []

// find nullable positions; in our case here will be 1, 4
for (let i = 0; i < array1.length; i++) {
  if (array1[i] === null) {
    nullPositions.push(i)
  }
}

// find total expected numbers of arrays from given input and create that array
const totalArray = Math.pow(array2.length, nullPositions.length);
for (let i = 0; i < totalArray; i++) {
  output.push([...array1])
}

// replacement logic for the `null` values
for (let i = 0; i < nullPositions.length; i++) {
  cursorPosition = 0;
  // frequency: how often the cursor needs to change (depends on null position)
  let frequency = totalArray / Math.pow(array2.length, i + 1);
  let resetCursorPositionOn = frequency;
  for (let j = 0; j < output.length; j++) {
    if (j < resetCursorPositionOn) {
      output[j][nullPositions[i]] = array2[cursorPosition];
    } else {
      resetCursorPositionOn += frequency;
      cursorPosition++;
      if (cursorPosition === array2.length) {
        cursorPosition = 0;
      }
      output[j][nullPositions[i]] = array2[cursorPosition];
    }
  }
}
console.log(output);
```
<h3>Output:</h3>
<pre class="lang-js prettyprint-override"><code>[
  ["a",1,"c",1,"e"],
  ["a",1,"c",2,"e"],
  ["a",1,"c",3,"e"],
  ["a",2,"c",1,"e"],
  ["a",2,"c",2,"e"],
  ["a",2,"c",3,"e"],
  ["a",3,"c",1,"e"],
  ["a",3,"c",2,"e"],
  ["a",3,"c",3,"e"]
]
</code>```

It works in any number of combinations.

Body:
I'm not sure the proper way to go about this recursively.
Let's say I have two arrays

array1 = [a, null, c, d, e]

array2 = [1, 2, 3]

I want to create a resulting array of the form

[
   [a, 1, c, d, e],
   [a, 2, c, d, e],
   [a, 3, c, d, e]
]

where each element of the second array fills in the place of null. I was able to do this just fine, but what if I have two cases of null?

array1 = [a, null, c, null, e]

array2 = [1, 2, 3]

how do I get the following?

[
   [a, 1, c, 1, e],
   [a, 1, c, 2, e],
   [a, 1, c, 3, e],
   [a, 2, c, 1, e],
   ...
   [a, 3, c, 3, e]
]

Body:
Code on Javascript:

let array1 = ['a', null, 'c', null, 'e']
let array2 = [1, 2, 3]

let output = []
let cursorPosition = 0; // cursorPosition is a pointer for `array2` index
let nullPositions = []

// find nullable positions; in our case here will be 1, 4
for (let i = 0; i < array1.length; i++) {
  if (array1[i] === null) {
    nullPositions.push(i)
  }
}

// find total expected numbers of arrays from given input and create that array
const totalArray = Math.pow(array2.length, nullPositions.length);
for (let i = 0; i < totalArray; i++) {
  output.push([...array1])
}

// replacement logic for the `null` values
for (let i = 0; i < nullPositions.length; i++) {
  cursorPosition = 0;
  // frequency: how often the cursor needs to change (depends on null position)
  let frequency = totalArray / Math.pow(array2.length, i + 1);
  let resetCursorPositionOn = frequency;
  for (let j = 0; j < output.length; j++) {
    if (j < resetCursorPositionOn) {
      output[j][nullPositions[i]] = array2[cursorPosition];
    } else {
      resetCursorPositionOn += frequency;
      cursorPosition++;
      if (cursorPosition === array2.length) {
        cursorPosition = 0;
      }
      output[j][nullPositions[i]] = array2[cursorPosition];
    }
  }
}
console.log(output);

Output:

[
  ["a",1,"c",1,"e"],
  ["a",1,"c",2,"e"],
  ["a",1,"c",3,"e"],
  ["a",2,"c",1,"e"],
  ["a",2,"c",2,"e"],
  ["a",2,"c",3,"e"],
  ["a",3,"c",1,"e"],
  ["a",3,"c",2,"e"],
  ["a",3,"c",3,"e"]
]

It works in any number of combinations.


问:
how to work around encoding problems in redirects
A website I try to scrape seems to have an encoding problem. The pages state, that they are encoded in utf-8, but if I try to scrape them and fetch the html source using requests, the redirect adress contains an encoding, that is not utf-8.
Browsers seem to be tolerant, so they fix this automatically, but the python requests package runs into an exception.

My code looks like this:

```<code>res= rq.get(url, allow_redirects=True)
</code>```

This runs into an exception when trying to decode the redirect string in the following code (hidden somewhere in the requests package):

```<code>string.decode(encoding)
</code>```

where string is the redirect string and encoding is 'utf8':

```<code>string= b'/aktien/herm\xe8s-aktie'
</code>```

I found out, that the encoding in fact is encoded in something like 'Windows-1252'. Actually the redirect should go on '/aktien/herm%C3%A8s-aktie'.

Now my question: how can I either get requests to be more tolerant about such encoding bugs (like the browsers), or how can I alternatively pass an encoding?

I searched for encoding settings, but what I saw so far, requests always does that automatically based on the result.

Btw. the result page of the redirect starts with (it really states to be utf-8)

```<code><!DOCTYPE html><html lang="de" prefix="og: http://ogp.me/ns#"><head><meta charset="utf-8">
</code>```

答:
You can use hooks= parameter in requests.get() method and explicitly urlencode the Location HTTP header. For example:

import requests
import urllib.parse

url = "<YOUR URL FROM EXAMPLE>"

def response_hook(hook_data, **kwargs):
    if "Location" in hook_data.headers:
        hook_data.headers["Location"] = urllib.parse.quote(
            hook_data.headers["Location"]
        )

res = requests.get(url, allow_redirects=True, hooks={"response": response_hook})
print(res.url)
```
<p>Prints:</p>
<pre class="lang-none prettyprint-override"><code>https://.../herm%C3%A8s-aktie
</code>```

Body:
A website I try to scrape seems to have an encoding problem. The pages state, that they are encoded in utf-8, but if I try to scrape them and fetch the html source using requests, the redirect adress contains an encoding, that is not utf-8.
Browsers seem to be tolerant, so they fix this automatically, but the python requests package runs into an exception.

My code looks like this:

res= rq.get(url, allow_redirects=True)

This runs into an exception when trying to decode the redirect string in the following code (hidden somewhere in the requests package):

string.decode(encoding)

where string is the redirect string and encoding is 'utf8':

string= b'/aktien/herm\xe8s-aktie'

I found out, that the encoding in fact is encoded in something like 'Windows-1252'. Actually the redirect should go on '/aktien/herm%C3%A8s-aktie'.

Now my question: how can I either get requests to be more tolerant about such encoding bugs (like the browsers), or how can I alternatively pass an encoding?

I searched for encoding settings, but what I saw so far, requests always does that automatically based on the result.

Btw. the result page of the redirect starts with (it really states to be utf-8)

<!DOCTYPE html><meta charset="utf-8">

Body:
You can use hooks= parameter in requests.get() method and explicitly urlencode the Location HTTP header. For example:

import requests
import urllib.parse

url = "<YOUR URL FROM EXAMPLE>"

def response_hook(hook_data, **kwargs):
    if "Location" in hook_data.headers:
        hook_data.headers["Location"] = urllib.parse.quote(
            hook_data.headers["Location"]
        )

res = requests.get(url, allow_redirects=True, hooks={"response": response_hook})
print(res.url)

Prints:

https://.../herm%C3%A8s-aktie


问:
GET request from local server using live-server
I am trying to fetch data from my local server and here is my server and how I handled GET requests:

```<code>const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());

app.listen(port, () => {
  console.log(`app running on port: ${port}...`);
});

const responseToClient = (req, res) => {
  res.status(200).json({
    status: 'success',
    body: 'Hello from the server!',
  });
};

app.get('/api', responseToClient);
</code>```

When I run my server and send a GET request to this address: 127.0.0.1:3000/api with Postman, it works perfectly.

The thing is I created a html page along with a js file and want to fetch data from my local server by it. Here is my fetch request on my js file:

```<code>const url = '/api';

const fetchData = async () => {
  try {
    const response = await fetch(url);
    const body = await response.json();
    alert(body);
  } catch (error) {
    alert(error);
  }
};
fetchData();
</code>```

I run my html file with live-server (extension) which runs on port 5500 by default , so the address my fetch request goes to will be 127.0.0.1:5500/api (instead of 127.0.0.1:3000/api), so it does not exists and I get an error message.

I tried to change the port of my server and set it to 5500 (the same as live-server) but it did not work.

How can I run my local server and send requests to it with live-server and my html file?

答:
If you do not want to have the HTML and JS files static-ed onto your Express server, then try this:

const url = '/api'; // bad
const url = '127.0.0.1:3000/api'; // better
```

Body:
I am trying to fetch data from my local server and here is my server and how I handled GET requests:

const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());

app.listen(port, () => {
  console.log(`app running on port: ${port}...`);
});

const responseToClient = (req, res) => {
  res.status(200).json({
    status: 'success',
    body: 'Hello from the server!',
  });
};

app.get('/api', responseToClient);

When I run my server and send a GET request to this address: 127.0.0.1:3000/api with Postman, it works perfectly.

The thing is I created a html page along with a js file and want to fetch data from my local server by it. Here is my fetch request on my js file:

const url = '/api';

const fetchData = async () => {
  try {
    const response = await fetch(url);
    const body = await response.json();
    alert(body);
  } catch (error) {
    alert(error);
  }
};
fetchData();

I run my html file with live-server (extension) which runs on port 5500 by default , so the address my fetch request goes to will be 127.0.0.1:5500/api (instead of 127.0.0.1:3000/api), so it does not exists and I get an error message.

I tried to change the port of my server and set it to 5500 (the same as live-server) but it did not work.

How can I run my local server and send requests to it with live-server and my html file?


答:
For the first question, you can use .nth()

```<code>ndf = df.sort_values('datetime').groupby('user_id',as_index = False).nth([0,-1])
</code>```

Output:

```<code>       user_id  length            datetime
5  e8172920273      10 2021-03-28 18:55:44
0  d8366132199      20 2021-03-31 17:55:44
8  e8172920273      90 2021-05-26 18:55:47
4  d8366132199      80 2021-06-28 18:55:49
</code>```

From there, to answer the second question, you can subtract the last row from the first row.

```<code>(ndf.groupby('user_id',as_index = False)
 .agg(date_diff = ('datetime',lambda x: (x.iloc[-1] - x.iloc[0]).days)))
</code>```

Output:

```<code>       user_id  date_diff
0  d8366132199         89
1  e8172920273         59
</code>```

Title: Get rows of first and last datetime records based on a column

Body:
I have a df with several columns and can't figure out in Python how to get only the rows with the first and last date/time (columns is in pandas datetime64[ns] Dtype) entry for each id. And also how to create a new df with the datetime difference in days?

Here's a df slice:

user_id
length
datetime

d8366132199
20
2021-03-31 17:55:44

d8366132199
40
2021-05-12 13:55:48

d8366132199
10
2021-04-09 16:55:47

d8366132199
70
2021-06-25 17:58:47

d8366132199
80
2021-06-28 18:55:49

e8172920273
10
2021-03-28 18:55:44

e8172920273
50
2021-03-30 18:55:45

e8172920273
60
2021-05-26 18:55:46

e8172920273
90
2021-05-26 18:55:47

Expected output:

user_id
length
datetime

d8366132199
20
2021-03-31 17:55:44

d8366132199
80
2021-06-28 18:55:49

e8172920273
10
2021-03-28 18:55:44

e8172920273
90
2021-05-26 18:55:47

New df with datetime difference, expected output:

user_id
date_diff

d8366132199
276

e8172920273
60

Many thanks!

Body:
For the first question, you can use .nth()

ndf = df.sort_values('datetime').groupby('user_id',as_index = False).nth([0,-1])

Output:

       user_id  length            datetime
5  e8172920273      10 2021-03-28 18:55:44
0  d8366132199      20 2021-03-31 17:55:44
8  e8172920273      90 2021-05-26 18:55:47
4  d8366132199      80 2021-06-28 18:55:49

From there, to answer the second question, you can subtract the last row from the first row.

(ndf.groupby('user_id',as_index = False)
 .agg(date_diff = ('datetime',lambda x: (x.iloc[-1] - x.iloc[0]).days)))

Output:

       user_id  date_diff
0  d8366132199         89
1  e8172920273         59


问:
use a loop inside a filter function
I have this dataframes and list

```<code>list1 = [(1, 'abc',234),(2,'eds',122),(3,'rte',565),(5,'asd',999),(10,'weq',90),(11,'dcd',98),
(11,'dcd',98)]
list2 = [(2, None,455),(3,'xyz',565),(4,'wer',234),(8,'ioo',878),(10,'weq',90),(11,'dcd',95),
(11,'sd',91),(11,'dd',9812)]
df1 = spark.createDataFrame(list1, ['id', 'value','number'])
df2 = spark.createDataFrame(list2, ['id', 'value','number'])

cols_eq = [[10, 11, 11], ['weq', 'dcd', 'dcd']]
</code>```

my problem is in the next line

```<code>df_dif=df2.unionAll(df1).filter(~(col('id').isin(cols_eq[0]) & col('value').isin(cols_eq[1])))
</code>```

specifically inside the filter, as you can see I am filtering the columns I am interested in by typing them one by one, but what I need is a way to do it without typing one by one.
I tried to create a for with the columns I'm interested in (id, value in this case), but I couldn't come up with a way to incorporate the "isin()" correctly.
So, if you can think of a way to do a for or maybe a pyspark function that will do what I need, I would appreciate it.

答:
You can do so by building up your filter in a loop. You'll need a list of the relevant columns you want to apply each item of cols_eq to. If they are the first n columns of your dataframe you could do something like cols = df1.columns[:n]

```<code>import pyspark.sql.functions as f

cols = ['id', 'value']
cols_eq = [[10, 11, 11], ['weq', 'dcd', 'dcd']]

fil = f.lit(True)
for column, vals in zip(cols, cols_eq):
    fil = fil & f.col(column).isin(vals)

df2.unionAll(df1).filter(~fil).show()

+---+-----+------+
| id|value|number|
+---+-----+------+
|  2| null|   455|
|  3|  xyz|   565|
|  4|  wer|   234|
|  8|  ioo|   878|
| 11|   sd|    91|
| 11|   dd|  9812|
|  1|  abc|   234|
|  2|  eds|   122|
|  3|  rte|   565|
|  5|  asd|   999|
+---+-----+------+
</code>```

Title: use a loop inside a filter function

Body:
I have this dataframes and list

list1 = [(1, 'abc',234),(2,'eds',122),(3,'rte',565),(5,'asd',999),(10,'weq',90),(11,'dcd',98),
(11,'dcd',98)]
list2 = [(2, None,455),(3,'xyz',565),(4,'wer',234),(8,'ioo',878),(10,'weq',90),(11,'dcd',95),
(11,'sd',91),(11,'dd',9812)]
df1 = spark.createDataFrame(list1, ['id', 'value','number'])
df2 = spark.createDataFrame(list2, ['id', 'value','number'])

cols_eq = [[10, 11, 11], ['weq', 'dcd', 'dcd']]

my problem is in the next line

df_dif=df2.unionAll(df1).filter(~(col('id').isin(cols_eq[0]) & col('value').isin(cols_eq[1])))

specifically inside the filter, as you can see I am filtering the columns I am interested in by typing them one by one, but what I need is a way to do it without typing one by one.
I tried to create a for with the columns I'm interested in (id, value in this case), but I couldn't come up with a way to incorporate the "isin()" correctly.
So, if you can think of a way to do a for or maybe a pyspark function that will do what I need, I would appreciate it.

Body:
You can do so by building up your filter in a loop. You'll need a list of the relevant columns you want to apply each item of cols_eq to. If they are the first n columns of your dataframe you could do something like cols = df1.columns[:n]

import pyspark.sql.functions as f

cols = ['id', 'value']
cols_eq = [[10, 11, 11], ['weq', 'dcd', 'dcd']]

fil = f.lit(True)
for column, vals in zip(cols, cols_eq):
    fil = fil & f.col(column).isin(vals)

df2.unionAll(df1).filter(~fil).show()

+---+-----+------+
| id|value|number|
+---+-----+------+
|  2| null|   455|
|  3|  xyz|   565|
|  4|  wer|   234|
|  8|  ioo|   878|
| 11|   sd|    91|
| 11|   dd|  9812|
|  1|  abc|   234|
|  2|  eds|   122|
|  3|  rte|   565|
|  5|  asd|   999|
+---+-----+------+


问:
Shared state in ES6 vs CommonJS modules
I can share state via CommonJS modules, but not via ES6 modules.

I'm wondering why, and want to know how to share state via ES6 modules.

CommonJS

main.js:

```<code>const shared = require('./shared.js');
shared.val = 2;
require('./child.js');
console.log('main.js', shared);
</code>```

child.js:

```<code>const shared = require('./shared.js');
console.log('child.js', shared);
</code>```

shared.js:

```<code>module.exports = {val:1};
</code>```

Result:

```<code>$ node main.js
child.js { val: 2 }
main.js { val: 2 }
</code>```

ES6

main.mjs:

```<code>import shared from './shared.mjs';
shared.val = 2;
import a from './child.mjs';
console.log('main.mjs', shared);
</code>```

child.mjs:

```<code>import shared from './shared.mjs';
console.log('child.mjs', shared);
export default undefined;
</code>```

shared.mjs:

```<code>export default {val:1};
</code>```

Result:

```<code>$ node main.mjs
child.mjs { val: 1 }
main.mjs { val: 2 }
</code>```

答:
You can share state in exactly the same way.

What you can't though is run code in between imports. They all should be at the top of your file because they are basically hoisted to the top.

If you change child to something like this:

```<code>import shared from './shared.mjs';

export default () => {
  console.log('child', shared);
};
</code>```

And then run that after you've changed shared:

```<code>import shared from './shared.mjs';
import runChild from './child.mjs';

shared.val = 2;

console.log('main', shared);
runChild();
</code>```

They'll both have { val: 2 }.

Title: Shared state in ES6 vs CommonJS modules

Body:
I can share state via CommonJS modules, but not via ES6 modules.

I'm wondering why, and want to know how to share state via ES6 modules.

CommonJS

main.js:

const shared = require('./shared.js');
shared.val = 2;
require('./child.js');
console.log('main.js', shared);

child.js:

const shared = require('./shared.js');
console.log('child.js', shared);

shared.js:

module.exports = {val:1};

Result:

$ node main.js
child.js { val: 2 }
main.js { val: 2 }

ES6

main.mjs:

import shared from './shared.mjs';
shared.val = 2;
import a from './child.mjs';
console.log('main.mjs', shared);

child.mjs:

import shared from './shared.mjs';
console.log('child.mjs', shared);
export default undefined;

shared.mjs:

export default {val:1};

Result:

$ node main.mjs
child.mjs { val: 1 }
main.mjs { val: 2 }

Body:
You can share state in exactly the same way.

What you can't though is run code in between imports. They all should be at the top of your file because they are basically hoisted to the top.

If you change child to something like this:

import shared from './shared.mjs';

export default () => {
  console.log('child', shared);
};

And then run that after you've changed shared:

import shared from './shared.mjs';
import runChild from './child.mjs';

shared.val = 2;

console.log('main', shared);
runChild();

They'll both have { val: 2 }.


问:
Creating .wav file from bytes
I am reading bytes from wav audio downloaded from a URL. I would like to "reconstruct" these bytes into a .wav file. I have attempted the code below, but the resulting file is pretty much static. For example, when I download audio of myself speaking, the .wav file produced is static only, but I can hear slight alterations/distortions when I know the audio should be playing my voice. What am I doing wrong?</p>

```<code>from pprint import pprint
import scipy.io.wavfile
import numpy

#download a wav audio recording from a url
>>>response = client.get_recording(r"someurl.com")
>>>pprint(response)
(b'RIFFv\xfc\x03\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x80>\x00\x00'
 ...
 b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
...
 b'\xea\xff\xfd\xff\x10\x00\x0c\x00\xf0\xff\x06\x00\x10\x00\x06\x00'
 ...)

>>>a=bytearray(response)
>>>pprint(a)
bytearray(b'RIFFv\xfc\x03\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00'
      b'\x80>\x00\x00\x00}\x00\x00\x02\x00\x10\x00LISTJ\x00\x00\x00INFOINAM'
      b'0\x00\x00\x00Conference d95ac842-08b7-4380-83ec-85ac6428cc41\x00'
      b'IART\x06\x00\x00\x00Nexmo\x00data\x00\xfc\x03\x00\xff\xff'
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
      ...
      b'\x12\x00\xf6\xff\t\x00\xed\xff\xf6\xff\xfc\xff\xea\xff\xfd\xff'
      ...)

>>>b = numpy.array(a, dtype=numpy.int16)
>>>pprint(b)
array([ 82,  73,  70, ..., 255, 248, 255], dtype=int16)

>>>scipy.io.wavfile.write(r"C:\Users\somefolder\newwavfile.wav",
16000, b)
</code>```

答:
I faced the same problem while streaming and I used the answers above to write a complete function.
In my case, the byte array was coming from streaming an audio file (the frontend) and the backend needs to process it as a ndarray.

This function simulates how the front-ends sends the audio file as chunks that are accumulated into a byte array:

```<code>audio_file_path = 'offline_input/zoom283.wav'

chunk = 1024

wf = wave.open(audio_file_path, 'rb')
audio_input = b''
d = wf.readframes(chunk)
while len(d) > 0:
    d = wf.readframes(chunk)
    audio_input = audio_input + d
</code>```

some import libraries:

```<code>import io
import wave

import numpy as np
import scipy.io.wavfile
import soundfile as sf
from scipy.io.wavfile import write
</code>```

Finally, the backend will take a byte array and convert it to ndarray:

```<code>def convert_bytearray_to_wav_ndarray(input_bytearray: bytes, sampling_rate=16000):
    bytes_wav = bytes()
    byte_io = io.BytesIO(bytes_wav)
    write(byte_io, sampling_rate, np.frombuffer(input_bytearray, dtype=np.int16))
    output_wav = byte_io.read()
    output, samplerate = sf.read(io.BytesIO(output_wav))
    return output

output = convert_bytearray_to_wav_ndarray(input_bytearray=audio_input)
</code>```

The output represents the audio file to be processed by the backend:

To check that the file has been received correctly, we write it to the desk:

```<code>scipy.io.wavfile.write("output1.wav", 16000, output)
</code>```

Title: Creating .wav file from bytes

Body:
I am reading bytes from wav audio downloaded from a URL. I would like to "reconstruct" these bytes into a .wav file. I have attempted the code below, but the resulting file is pretty much static. For example, when I download audio of myself speaking, the .wav file produced is static only, but I can hear slight alterations/distortions when I know the audio should be playing my voice. What am I doing wrong?

from pprint import pprint
import scipy.io.wavfile
import numpy

#download a wav audio recording from a url
>>>response = client.get_recording(r"someurl.com")
>>>pprint(response)
(b'RIFFv\xfc\x03\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x80>\x00\x00'
 ...
 b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
...
 b'\xea\xff\xfd\xff\x10\x00\x0c\x00\xf0\xff\x06\x00\x10\x00\x06\x00'
 ...)

>>>a=bytearray(response)
>>>pprint(a)
bytearray(b'RIFFv\xfc\x03\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00'
      b'\x80>\x00\x00\x00}\x00\x00\x02\x00\x10\x00LISTJ\x00\x00\x00INFOINAM'
      b'0\x00\x00\x00Conference d95ac842-08b7-4380-83ec-85ac6428cc41\x00'
      b'IART\x06\x00\x00\x00Nexmo\x00data\x00\xfc\x03\x00\xff\xff'
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
      ...
      b'\x12\x00\xf6\xff\t\x00\xed\xff\xf6\xff\xfc\xff\xea\xff\xfd\xff'
      ...)

>>>b = numpy.array(a, dtype=numpy.int16)
>>>pprint(b)
array([ 82,  73,  70, ..., 255, 248, 255], dtype=int16)

>>>scipy.io.wavfile.write(r"C:\Users\somefolder\newwavfile.wav",
16000, b)

Body:
I faced the same problem while streaming and I used the answers above to write a complete function.
In my case, the byte array was coming from streaming an audio file (the frontend) and the backend needs to process it as a ndarray.

This function simulates how the front-ends sends the audio file as chunks that are accumulated into a byte array:

audio_file_path = 'offline_input/zoom283.wav'

chunk = 1024

wf = wave.open(audio_file_path, 'rb')
audio_input = b''
d = wf.readframes(chunk)
while len(d) > 0:
    d = wf.readframes(chunk)
    audio_input = audio_input + d

some import libraries:

import io
import wave

import numpy as np
import scipy.io.wavfile
import soundfile as sf
from scipy.io.wavfile import write

Finally, the backend will take a byte array and convert it to ndarray:

def convert_bytearray_to_wav_ndarray(input_bytearray: bytes, sampling_rate=16000):
    bytes_wav = bytes()
    byte_io = io.BytesIO(bytes_wav)
    write(byte_io, sampling_rate, np.frombuffer(input_bytearray, dtype=np.int16))
    output_wav = byte_io.read()
    output, samplerate = sf.read(io.BytesIO(output_wav))
    return output

output = convert_bytearray_to_wav_ndarray(input_bytearray=audio_input)

The output represents the audio file to be processed by the backend:

To check that the file has been received correctly, we write it to the desk:

scipy.io.wavfile.write("output1.wav", 16000, output)


问:
How do I navigate to the start or end of a file in Android Studio?
In TextMate, for example, I use command + uparrow for the start of the file and command + downarrow for the end of the file.

Android Studio doesn't do this. How do I do the same thing in Android Studio?

Thanks.

答:
For those that want to use the cmd + ↑/↓ shortcuts and change the Keymap.

Go to Preferences (cmd + ,) > Keymap.

Search for Move Caret to Text Start and Move Caret to Text End and change the keyboard shortcut to (cmd + ↑) and (cmd + ↓) respectively.


问:
Fixed List is not updating with for loop - Flutter
I've a fixed list reservedGuest. After checking the condition in for loop I want to update the seats if the membership date has expired. The list is not updating. The code is as follows. PS. The List is filled through API on init().</p>
```<code>class MyClubController extends GetxController {

    List goldLane = List.filled(3, null, growable: false);

    void _alterLanesOnContractEnds() {
    for (var i in goldLane) {
      print("\n\n I: $i");
      if (i == null ||
          DateTime.parse(i['contractEnds']).isBefore(
            DateTime.now(),
          )) {
        i = null;
        print('Can be removed');
      } else {
        print('Cannot be removed');
      }
    }
    update();
  }

}
</code>```

答:
A for-in loop will not allow you to reassign elements of the List.  When you do:

for (var i in goldLane) {
  // ...
  i = null;
}
```
<p>you are reassigning what the <em>local</em> <code>i</code> variable refers to, not mutating the <code>goldLane</code> <code>List</code>.</p>
<p>You instead can iterate with an index:</p>
<pre class="lang-dart prettyprint-override"><code>void _alterLanesOnContractEnds() {
  for (var i = 0; i < goldLane.length; i += 1) {
    var element = goldLane[i];
    print("\n\n I: $element");
    if (element == null ||
        DateTime.parse(element['contractEnds']).isBefore(
          DateTime.now(),
        )) {
      goldLane[i] = null;
      print('Can be removed');
    } else {
      print('Cannot be removed');
    }
  }
  update();
}
</code>```

Body:
I've a fixed list reservedGuest. After checking the condition in for loop I want to update the seats if the membership date has expired. The list is not updating. The code is as follows. PS. The List is filled through API on init().

class MyClubController extends GetxController {

    List goldLane = List.filled(3, null, growable: false);

    void _alterLanesOnContractEnds() {
    for (var i in goldLane) {
      print("\n\n I: $i");
      if (i == null ||
          DateTime.parse(i['contractEnds']).isBefore(
            DateTime.now(),
          )) {
        i = null;
        print('Can be removed');
      } else {
        print('Cannot be removed');
      }
    }
    update();
  }

}

Body:
A for-in loop will not allow you to reassign elements of the List.  When you do:

for (var i in goldLane) {
  // ...
  i = null;
}

you are reassigning what the local i variable refers to, not mutating the goldLane List.

You instead can iterate with an index:

void _alterLanesOnContractEnds() {
  for (var i = 0; i < goldLane.length; i += 1) {
    var element = goldLane[i];
    print("\n\n I: $element");
    if (element == null ||
        DateTime.parse(element['contractEnds']).isBefore(
          DateTime.now(),
        )) {
      goldLane[i] = null;
      print('Can be removed');
    } else {
      print('Cannot be removed');
    }
  }
  update();
}


问:
Cannot read property 'push' of undefined in react

    import React, {PropTypes} from 'react';

export default class Login extends React.Component {
  constructor(props) {
    super(props)
    this.handleLogin = this.handleLogin.bind(this)
  }

  handleLogin(event) {
    event.preventDefault()
    // do some login logic here, and if successful:
    this.props.history.push(`/Home`)
  }

  render() {
    return (

        <form onSubmit={this.handleLogin}>
          <input type='submit' value='Login' />
        </form>

    )
  }
}```

I am getting Cannot read property 'push' of undefined in the console. Now how to access the push in the react-router v4.

thanks in advance!

答:
You have to use useNavigate if you installed v6 or more than "react-router-dom": ^6.2.1

```<code>import { useNavigate } from "react-router-dom";

 let navigate = useNavigate();

 const onSubmit = async (e) => {
    e.preventDefault();
    await axios.post("http://localhost:3001/users", user);
    navigate(`/`);
 };
</code>```

Please read this link if you want know more about it and this video has a very useful practice to understand it. The video page is 'React Router V6 Tutorial - Routes, Redirecting, UseNavigate, UseParams...' in 'PedroTech' page on YouTube.

Body:
    import React, {PropTypes} from 'react';

export default class Login extends React.Component {
  constructor(props) {
    super(props)
    this.handleLogin = this.handleLogin.bind(this)
  }

  handleLogin(event) {
    event.preventDefault()
    // do some login logic here, and if successful:
    this.props.history.push(`/Home`)
  }

  render() {
    return (

        <form onSubmit={this.handleLogin}>
          <input type='submit' value='Login' />
        </form>

    )
  }
}

I am getting Cannot read property 'push' of undefined in the console. Now how to access the push in the react-router v4.

thanks in advance!

Body:
You have to use useNavigate if you installed v6 or more than "react-router-dom": ^6.2.1

import { useNavigate } from "react-router-dom";

 let navigate = useNavigate();

 const onSubmit = async (e) => {
    e.preventDefault();
    await axios.post("http://localhost:3001/users", user);
    navigate(`/`);
 };

Please read this link if you want know more about it and this video has a very useful practice to understand it. The video page is 'React Router V6 Tutorial - Routes, Redirecting, UseNavigate, UseParams...' in 'PedroTech' page on YouTube.


问:
SocketIO throws net::ERR_CERT_AUTHORITY_INVALID on self signed certificate
I am using socket io on client:

```<code>const socket = require('socket.io-client')('https://localhost:4200', {secure: true, rejectUnauthorized: false})
</code>```

And on server:

```<code>let https = require('https')
let fs = require('fs')

let options = {
    key: fs.readFileSync('cert/my.net.key'),
    cert: fs.readFileSync('cert/my.net.cert'),
    requestCert: false,
    rejectUnauthorized: false,
};

const server = https.createServer(options, require('express')())
const io = require('socket.io')(server)
</code>```

All services are started normally, but on client I am getting polling-xhr.js:263 GET https://localhost:4200/socket.io/?EIO=3&transport=polling&t=MPa6ZuL net::ERR_CERT_AUTHORITY_INVALID

Why? Whats is wrong?

答:
Browsers don't like self-signed certificates for security reasons.

To get around this in your development environment, I see three options:

Use a certificate issued by a certification unit.

It could be something free, like https://letsencrypt.org/.

Create your server dynamically, based on the development environment, not to include certificates and work directly with HTTP and WS (and not HTTPS and WSS).

Change the configuration of your browser used in development so that it accepts self-signed certificates.

For Chrome, for example, just enable the Allow invalid certificates for resources loaded from localhost. (chrome://flags/#allow-insecure-localhost) setting.

But remember that you will not be able to use self-signed certificates in production environments.

Body:
I am using socket io on client:

const socket = require('socket.io-client')('https://localhost:4200', {secure: true, rejectUnauthorized: false})

And on server:

let https = require('https')
let fs = require('fs')

let options = {
    key: fs.readFileSync('cert/my.net.key'),
    cert: fs.readFileSync('cert/my.net.cert'),
    requestCert: false,
    rejectUnauthorized: false,
};

const server = https.createServer(options, require('express')())
const io = require('socket.io')(server)

All services are started normally, but on client I am getting polling-xhr.js:263 GET https://localhost:4200/socket.io/?EIO=3&transport=polling&t=MPa6ZuL net::ERR_CERT_AUTHORITY_INVALID

Why? Whats is wrong?


问:
how to insert some text in all django context using django middleware
this my middleware code :

```<code>from django.conf import settings
from django.template import RequestContext

class BeforeFilter(object):
    def process_request(self, request):
        settings.my_var = 'Hello World'
        request.ss = 'ssssssssss'
        return None
    def process_response(self, request, response):

        return response
</code>```

this is the settings.py:

```<code>TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
)
MIDDLEWARE_CLASSES = (
    ...
    'middleware.BeforeFilter',
)
</code>```

and the view is :

```<code>#coding:utf-8

from django.conf import settings
from django.shortcuts import render_to_response

from django.http import HttpResponse
from django.template import RequestContext

def index(request):
    context = RequestContext(request)
    context['a'] = 'aaaa'
    return render_to_response('a.html',context)
</code>```

the html is :

```<code>{{a}}fffff{{ss}}
</code>```

but it not show {{ss}}:

```<code>aaaafffff
</code>```

so how do i show :

```<code>aaaafffffssssssss
</code>```

how to insert some text in all django context  using django middleware,

so that i cant use to insert the text everytime ,

thanks

答:
The OP asked for how to do this with middleware but I found this question without needing that requirement. The currently accepted answer is outdated and the edit queue is full.

As of Django 1.10, the TEMPLATE_CONTEXT_PROCESSORS setting has been moved to the context_processors option of TEMPLATES.

context_processors is a list of dotted Python paths to callables that are used to populate the context when a template is rendered with a request. These callables take a request object as their argument and return a dict of items to be merged into the context. It defaults to an empty list.

```<code># context_processors.py

def sample_context_processor(request):
    return {'key': 'value'}
</code>```

```<code># settings.py

TEMPLATES = [
    {
        'OPTIONS': {
            'context_processors': [
                'myproject.context_processors.sample_context_processor',
            ],
        },
    },
]
</code>```

Body:
this my middleware code :

from django.conf import settings
from django.template import RequestContext

class BeforeFilter(object):
    def process_request(self, request):
        settings.my_var = 'Hello World'
        request.ss = 'ssssssssss'
        return None
    def process_response(self, request, response):

        return response

this is the settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
)
MIDDLEWARE_CLASSES = (
    ...
    'middleware.BeforeFilter',
)

and the view is :

#coding:utf-8

from django.conf import settings
from django.shortcuts import render_to_response

from django.http import HttpResponse
from django.template import RequestContext

def index(request):
    context = RequestContext(request)
    context['a'] = 'aaaa'
    return render_to_response('a.html',context)

the html is :

{{a}}fffff{{ss}}

but it not show {{ss}}:

aaaafffff

so how do i show :

aaaafffffssssssss

how to insert some text in all django context  using django middleware,

so that i cant use to insert the text everytime ,

thanks

Body:
The OP asked for how to do this with middleware but I found this question without needing that requirement. The currently accepted answer is outdated and the edit queue is full.

As of Django 1.10, the TEMPLATE_CONTEXT_PROCESSORS setting has been moved to the context_processors option of TEMPLATES.

context_processors is a list of dotted Python paths to callables that are used to populate the context when a template is rendered with a request. These callables take a request object as their argument and return a dict of items to be merged into the context. It defaults to an empty list.

# context_processors.py

def sample_context_processor(request):
    return {'key': 'value'}

# settings.py

TEMPLATES = [
    {
        'OPTIONS': {
            'context_processors': [
                'myproject.context_processors.sample_context_processor',
            ],
        },
    },
]


问:
I Cant delete multiple items using DynamoDB BatchWrite including a primary key and sort key
So I'm trying to delete two records for the same user. However, when I run the Lambda function below it really does not console log either a success or failure and the records are not deleted in DynamoDB. Any advice on how I could modify the below to get the expected result of having both records deleted would be appreciated. Thanks

Included several console logs for troubleshooting

both PK and SK are strings in DynamoDB

Both delete request are for the same user just two different entries in the same table

```<code>'use strict'
const AWS = require('aws-sdk');

AWS.config.update({ region: "us-east-1" });

exports.handler = async (event, context) => {
    var ddb = new AWS.DynamoDB.DocumentClient();

    var records = [{
        DeleteRequest : {
            Key : {
                'PK' : 'username',
                'SK' : 'C297329360'
            }
        },
        DeleteRequest : {
            Key : {
                'PK' : 'username',
                'SK' : 'R297329360'
            }
        }
    }];

    var params = {
        RequestItems : {
            'userTable' : records
        }
    };
    console.log(JSON.stringify(records));
    console.log(records);
    console.log(JSON.stringify(params));
    console.log(params);

    ddb.batchWrite(params, function(err, data) {
        if (err) {
            console.log('Batch delete unsuccessful ...');
            console.log(err, err.stack); // an error occurred
        } else {
            console.log('Batch delete successful ...');
            console.log(data); // successful response
        }

    });
}

</code>```
```<code>Test Event Name
DeleteReportById

Response
null

Function Logs
START RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 Version: $LATEST
2022-02-11T13:49:56.007Z    6c40c1f7-5cdb-4cef-95a7-306d540aa9f9    INFO    [{"DeleteRequest":{"Key":{"PK":"username","SK":"R297329360"}}}]

2022-02-11T13:49:56.046Z    6c40c1f7-5cdb-4cef-95a7-306d540aa9f9    INFO    [ { DeleteRequest: { Key: [Object] } } ]

2022-02-11T13:49:56.047Z    6c40c1f7-5cdb-4cef-95a7-306d540aa9f9    INFO    {"RequestItems":{"FitTool_Prod":[{"DeleteRequest":{"Key":{"PK":"username","SK":"R297329360"}}}]}}

2022-02-11T13:49:56.065Z    6c40c1f7-5cdb-4cef-95a7-306d540aa9f9    INFO    { RequestItems: { userTable: [ [Object] ] } }

END RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9
REPORT RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9  Duration: 589.81 ms Billed Duration: 590 ms Memory Size: 128 MB Max Memory Used: 77 MB  Init Duration: 474.35 ms

Request ID
6c40c1f7-5cdb-4cef-95a7-306d540aa9f9
</code>```

答:
You are not waiting for the batchWrite call to complete, the lambda terminates before actually sending the request to DynamoDB. Your handler is async, so you can do :

await ddb.batchWrite(...).promise()
```

Body:
So I'm trying to delete two records for the same user. However, when I run the Lambda function below it really does not console log either a success or failure and the records are not deleted in DynamoDB. Any advice on how I could modify the below to get the expected result of having both records deleted would be appreciated. Thanks

Included several console logs for troubleshooting

both PK and SK are strings in DynamoDB

Both delete request are for the same user just two different entries in the same table

'use strict'
const AWS = require('aws-sdk');

AWS.config.update({ region: "us-east-1" });

exports.handler = async (event, context) => {
    var ddb = new AWS.DynamoDB.DocumentClient();

    var records = [{
        DeleteRequest : {
            Key : {
                'PK' : 'username',
                'SK' : 'C297329360'
            }
        },
        DeleteRequest : {
            Key : {
                'PK' : 'username',
                'SK' : 'R297329360'
            }
        }
    }];

    var params = {
        RequestItems : {
            'userTable' : records
        }
    };
    console.log(JSON.stringify(records));
    console.log(records);
    console.log(JSON.stringify(params));
    console.log(params);

    ddb.batchWrite(params, function(err, data) {
        if (err) {
            console.log('Batch delete unsuccessful ...');
            console.log(err, err.stack); // an error occurred
        } else {
            console.log('Batch delete successful ...');
            console.log(data); // successful response
        }

    });
}

Test Event Name
DeleteReportById

Response
null

Function Logs
START RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9 Version: $LATEST
2022-02-11T13:49:56.007Z    6c40c1f7-5cdb-4cef-95a7-306d540aa9f9    INFO    [{"DeleteRequest":{"Key":{"PK":"username","SK":"R297329360"}}}]

2022-02-11T13:49:56.046Z    6c40c1f7-5cdb-4cef-95a7-306d540aa9f9    INFO    [ { DeleteRequest: { Key: [Object] } } ]

2022-02-11T13:49:56.047Z    6c40c1f7-5cdb-4cef-95a7-306d540aa9f9    INFO    {"RequestItems":{"FitTool_Prod":[{"DeleteRequest":{"Key":{"PK":"username","SK":"R297329360"}}}]}}

2022-02-11T13:49:56.065Z    6c40c1f7-5cdb-4cef-95a7-306d540aa9f9    INFO    { RequestItems: { userTable: [ [Object] ] } }

END RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9
REPORT RequestId: 6c40c1f7-5cdb-4cef-95a7-306d540aa9f9  Duration: 589.81 ms Billed Duration: 590 ms Memory Size: 128 MB Max Memory Used: 77 MB  Init Duration: 474.35 ms

Request ID
6c40c1f7-5cdb-4cef-95a7-306d540aa9f9


问:
Why can I click on a link outside of its text element?
I'm building a forum and I have a link that can be clicked outside of its text, how can I undo it?
I also have an image inside a tag that can be clicked outside of its image, I want to undo that too.
I have tried - position: relative; but it didn't work.

Here is the HTML code of the image.

```<code><div class="nav-list">
       <a class="home_A"href="Home.php">
          <img class="homeIcon" src="photos/icon.png">
       </a>
</div>
</code>```

CSS of the image

```<code>.homeIcon{
    width: 200px;
    display:block;
    margin:auto;
    margin: 0 auto;
}
</code>```

CSS of the  link

```<code>.signinForm a{
margin: auto;
display:block;
text-align:center;
margin-top: 7px;
color: #006db6;
font-size: 20px;
}
</code>```

HTML of the  link

```<code><form method="POST" class="signinForm">
<ul>
    <li> <h1 class="signinH1">sign in</h1></li>
    <li> <input type="text" placeholder="User name"name="username_login"class="username" value="<?php if(isset($_POST['username_login'])){echo $_POST['username_login'];}?>" required></li>
    <li> <input type="password" placeholder="Password" name="password_login" class="password" value="<?php  if(isset($_POST['password_login'])){echo $_POST['password_login'];}?>" required></li>
    <li> <input type="submit" value="Log in"class="signinbutton"></li>
    <li><a href="signUp.php">Sign up</a></li>
</ul>

</form>
</code>```

Thanks all.

答:
```<code><!--HTML_CODE-->
<div class="nav-list">
   <a class="home_A"href="Home.php">
      <img class="homeIcon" src="photos/icon.png">
   </a>
</div>

<form method="POST" class="signinForm">
<ul>
<li> <h1 class="signinH1">sign in</h1></li>
<li> <input type="text" placeholder="User
name"name="username_login"class="username" value="<?php
if(isset($_POST['username_login'])){echo $_POST['username_login'];}?>" required>
</li>
<li> <input type="password" placeholder="Password" name="password_login"
class="password" value="<?php  if(isset($_POST['password_login'])){echo
$_POST['password_login'];}?>" required></li>
<li> <input type="submit" value="Log in"class="signinbutton"></li>
<div class="signUp-block">
<li><a href="signUp.php">Sign up</a></li></div>
</ul>

</form>

/*CSS_CODE*/
.homeIcon{
width: 200px;
display:block;
margin:auto;
margin: 0 auto;
}
.signUp-block{
display: flex;
flex-direction: row;
justify-content: center;
}
.signinForm a{

margin: auto;
display:inline-block;

text-align:center;
margin-top: 7px;
color: #006db6;
font-size: 20px;
}
.homeIcon {
display: inline-block;
}
</code>```

Body:
I'm building a forum and I have a link that can be clicked outside of its text, how can I undo it?
I also have an image inside a tag that can be clicked outside of its image, I want to undo that too.
I have tried - position: relative; but it didn't work.

Here is the HTML code of the image.

CSS of the image

.homeIcon{
    width: 200px;
    display:block;
    margin:auto;
    margin: 0 auto;
}

CSS of the  link

.signinForm a{
margin: auto;
display:block;
text-align:center;
margin-top: 7px;
color: #006db6;
font-size: 20px;
}

HTML of the  link

<form method="POST" class="signinForm">

sign in

 <input type="text" placeholder="User name"name="username_login"class="username" value="<?php if(isset($_POST['username_login'])){echo $_POST['username_login'];}?>" required>

 <input type="password" placeholder="Password" name="password_login" class="password" value="<?php  if(isset($_POST['password_login'])){echo $_POST['password_login'];}?>" required>

 <input type="submit" value="Log in"class="signinbutton">

Sign up

</form>

Thanks all.


问:
Removing columns from pandas dataframe based on function output
I have the following dataframe:

```<code>d = {'col1': ['challenging', 'swimming', ',', 'it', 'my', 'there!'], 'col2': [3, 4,5,6,7,8]}
df = pd.DataFrame(data=d)
          col1  col2
0  challenging     3
1     swimming     4
2            ,     5
3           it     6
4           my     7
5       there!     8
</code>```

I have defined this function that removes punctuations and stopwords:

```<code>from nltk.corpus import stopwords
import string
stop_words = set(stopwords.words('english'))

def remove_stopwords_punc(sent_words):
   # print(sent_words)
    return [ww for ww in sent_words
            if ww.lower() not in stop_words and ww not in string.punctuation]
from nltk.tokenize import word_tokenize
remove_stopwords_punc(word_tokenize('challenging swimming , it my there!'))

['challenging', 'swimming']
</code>```

I want to run this function on the col1 and only keep the rows that are not deemed at stopwords or punctuation, in this example leaving this output:

```<code>          col1  col2
0  challenging     3
1     swimming     4
</code>```

答:
Use isin and str.contains:

```<code># Create a boolean mask per condition
m1 = df['col1'].str.lower().isin(stop_words)
m2 = df['col1'].str.contains(fr"[{string.punctuation}]")

# Filter out your dataframe according conditions
df = df[~(m1|m2)]
print(df)

# Output
          col1  col2
0  challenging     3
1     swimming     4
</code>```

Body:
I have the following dataframe:

d = {'col1': ['challenging', 'swimming', ',', 'it', 'my', 'there!'], 'col2': [3, 4,5,6,7,8]}
df = pd.DataFrame(data=d)
          col1  col2
0  challenging     3
1     swimming     4
2            ,     5
3           it     6
4           my     7
5       there!     8

I have defined this function that removes punctuations and stopwords:

from nltk.corpus import stopwords
import string
stop_words = set(stopwords.words('english'))

def remove_stopwords_punc(sent_words):
   # print(sent_words)
    return [ww for ww in sent_words
            if ww.lower() not in stop_words and ww not in string.punctuation]
from nltk.tokenize import word_tokenize
remove_stopwords_punc(word_tokenize('challenging swimming , it my there!'))

['challenging', 'swimming']

I want to run this function on the col1 and only keep the rows that are not deemed at stopwords or punctuation, in this example leaving this output:

          col1  col2
0  challenging     3
1     swimming     4

Body:
Use isin and str.contains:

# Create a boolean mask per condition
m1 = df['col1'].str.lower().isin(stop_words)
m2 = df['col1'].str.contains(fr"[{string.punctuation}]")

# Filter out your dataframe according conditions
df = df[~(m1|m2)]
print(df)

# Output
          col1  col2
0  challenging     3
1     swimming     4


问:
HTML5 - Getting Started Using Offline Storage
I am following a tutorial to setup Offline Storage.  I picked a compatible browser, FireFox 4.0.1.  According to the Site that is all I need.  Yet, somehow it does not work.

My code blows up in Javascript on this line.

var db = window.openDatabase("scores", "", "Previous Scores", 1024 * 1000);

I don't know what error is being thrown.  I F11 to try to step into this line in Firebug, but nothing happens after that.

I am surprised this line is called without checking if the DB exists (I didn't install SQL express I guess it comes with the browser now nor did I setup the DB table).

What can I do to fix this?

答: Your are confusing the (abandoned) Web SQL Database API for the Web Storage API. The SQL API is only supported in Safari, while Web Storage (also called localStorage) is widely supported.</p>


答:
Use your above return list</p>
```<code>l = ['challenging', 'swimming']

out = df.loc[df['col1'].isin(l)]
</code>```

Title: Removing columns from pandas dataframe based on function output

Body:
Use your above return list

l = ['challenging', 'swimming']

out = df.loc[df['col1'].isin(l)]