问:
How do I not print a DataFrame's unique ID?
I've got a data frame that prints a unique ID in the same cell as my id.

```<code>data = pd.DataFrame(video_details, columns = ['id', 'name', 'description'])
</code>```

Code to save the data:

```<code>with open('data.csv', 'w') as csvfile:
    fieldnames = ['ID']
    writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
    writer.writeheader()
    writer.writerow({"ID":data})
</code>```

My output looks like this: 0       data

I know CSV files require a "," between items, but how do I insert that comma into a unique ID? I tried to pop it but it always returned.

Body:
I've got a data frame that prints a unique ID in the same cell as my id.

data = pd.DataFrame(video_details, columns = ['id', 'name', 'description'])

Code to save the data:

with open('data.csv', 'w') as csvfile:
    fieldnames = ['ID']
    writer = csv.DictWriter(csvfile, fieldnames = fieldnames)
    writer.writeheader()
    writer.writerow({"ID":data})

My output looks like this: 0       data

I know CSV files require a "," between items, but how do I insert that comma into a unique ID? I tried to pop it but it always returned.


来源: stackexchange

问:
Repetitive Query in MongoDB
I am creating a query in MongoDB which works correctly for me but in the end I have to do an extra grouping to get the response grouped by year. This is the query:

```<code>db.sales.aggregate([
  {
    $group: {
      _id: {
        month: {
          $month: "$createdAt"
        },
        year: {
          $year: "$createdAt"
        },
        dayOfWeek: {
            $dayOfWeek: "$createdAt"
        },
        stringDay: {
            $dateToString:
            { format: "%Y-%m-%d", date: "$createdAt"}
        },
        week: {
            $isoWeek: "$createdAt"
        }
      },
      total: { $sum: '$total'},
      count: { $sum: 1 },
      totalAverage: { $avg: '$total'}
    }
  },
  {
    $sort : {
    "_id.month" : 1
    }
  },
  {
    $project: {
      total:  { $round: [ "$total", 2 ] },
      year: "$_id.year",
      date: "$_id.date",
      week: "$_id.week",
      numVentas: "$count",
      month: "$_id.month",
      dayOfWeek: "$_id.dayOfWeek",
      stringDay:"$_id.stringDay",
      count: "$count",
      totalAverage: { $round: [ "$totalAverage", 2 ] },
      stringMonth: {
        $arrayElemAt: [
          [
            "",
            "Jan",
            "Feb",
            "Mar",
            "Apr",
            "May",
            "Jun",
            "Jul",
            "Aug",
            "Sep",
            "Oct",
            "Nov",
            "Dec"
          ],
          "$_id.month"
        ]
      },
      stringWeek: {
        $switch: {
            branches:[
                { case: { $eq: ["$_id.dayOfWeek", 1] }, then: "Lunes" },
                { case: { $eq: ["$_id.dayOfWeek", 2] }, then: "Martes" },
                { case: { $eq: ["$_id.dayOfWeek", 3] }, then: "Miércoles" },
                { case: { $eq: ["$_id.dayOfWeek", 4] }, then: "Jueves" },
                { case: { $eq: ["$_id.dayOfWeek", 5] }, then: "Viernes" },
                { case: { $eq: ["$_id.dayOfWeek", 6] }, then: "Sábado" },
                { case: { $eq: ["$_id.dayOfWeek", 7] }, then: "Domingo" }
            ],
            default: "Día desconocido"
            }
        }
    }
  },
  {
    $group: {
        _id:  { month: "$stringMonth", year: "$year"},
        count: { $sum: "$count" },
        total: { $sum: "$total" },
        totalAverage: { $sum: "$totalAverage" },
        sales: {
            $push: {
                numberDay: "$dayOfWeek",
                stringWeek: "$stringWeek",
                date: "$stringDay",
                total: "$total",
                count: "$count",
                totalAverage: { $round: [ "$totalAverage", 2 ] }
            }
        }
    }
  },
  {
    $group: {
        _id: "$_id.year",
        monthsWithSales: { $sum: 1 },
        count: { $sum: "$count" },
        total: { $sum: "$total" },
        totalAverage: { $sum: "$totalAverage" },
        sales: {
            $push: {
                mes: "$_id.month",
                count: "$count",
                total: "$total",
                totalAverage: "$totalAverage",
                sales:"$sales"
            }
        }
    }
  }

])
</code>```

And I get this response:

```<code>
[
  {
    "_id": 2022,
    "monthsWithSales": 4,
    "count": 57,
    "total": 22324.8,
    "totalAverage": 7765.799999999999,
    "sales": [
      {
        "mes": "Oct",
        "count": 10,
        "total": 1936,
        "totalAverage": 1233.6,
        "sales": [
          {
            "numberDay": 6,
            "stringWeek": "Sábado",
            "date": "2022-10-21",
            "total": 526.8,
            "count": 3,
            "totalAverage": 175.6
          },
          {
            "numberDay": 1,
            "stringWeek": "Lunes",
            "date": "2022-10-02",
            "total": 85.6,
            "count": 1,
            "totalAverage": 85.6
          },
          {
            "numberDay": 7,
            "stringWeek": "Domingo",
            "date": "2022-10-22",
            "total": 526.8,
            "count": 3,
            "totalAverage": 175.6
          },
          {
            "numberDay": 3,
            "stringWeek": "Miércoles",
            "date": "2022-10-04",
            "total": 180,
            "count": 1,
            "totalAverage": 180
          },
          {
            "numberDay": 4,
            "stringWeek": "Jueves",
            "date": "2022-10-12",
            "total": 531.2,
            "count": 1,
            "totalAverage": 531.2
          },
          {
            "numberDay": 3,
            "stringWeek": "Miércoles",
            "date": "2022-10-25",
            "total": 85.6,
            "count": 1,
            "totalAverage": 85.6
          }
        ]
      },
      {
        "mes": "Nov",
        "count": 7,
        "total": 2205.2,
        "totalAverage": 1014.8,
        "sales": [
          {
            "numberDay": 4,
            "stringWeek": "Jueves",
            "date": "2022-11-02",
            "total": 526.8,
            "count": 3,
            "totalAverage": 175.6
          },
          {
            "numberDay": 6,
            "stringWeek": "Sábado",
            "date": "2022-11-25",
            "total": 171.2,
            "count": 2,
            "totalAverage": 85.6
          },
          {
            "numberDay": 7,
            "stringWeek": "Domingo",
            "date": "2022-11-12",
            "total": 1507.2,
            "count": 2,
            "totalAverage": 753.6
          }
        ]
      },
      {
        "mes": "Dec",
        "count": 33,
        "total": 12587.6,
        "totalAverage": 4074.5,
        "sales": [
          {
            "numberDay": 3,
            "stringWeek": "Miércoles",
            "date": "2022-12-06",
            "total": 850,
            "count": 1,
            "totalAverage": 850
          },
          {
            "numberDay": 6,
            "stringWeek": "Sábado",
            "date": "2022-12-02",
            "total": 8737.6,
            "count": 25,
            "totalAverage": 349.5
          },
          {
            "numberDay": 7,
            "stringWeek": "Domingo",
            "date": "2022-12-10",
            "total": 900,
            "count": 1,
            "totalAverage": 900
          },
          {
            "numberDay": 1,
            "stringWeek": "Lunes",
            "date": "2022-12-04",
            "total": 200,
            "count": 1,
            "totalAverage": 200
          },
          {
            "numberDay": 2,
            "stringWeek": "Martes",
            "date": "2022-12-05",
            "total": 500,
            "count": 1,
            "totalAverage": 500
          },
          {
            "numberDay": 5,
            "stringWeek": "Viernes",
            "date": "2022-12-08",
            "total": 250,
            "count": 2,
            "totalAverage": 125
          },
          {
            "numberDay": 4,
            "stringWeek": "Jueves",
            "date": "2022-12-07",
            "total": 250,
            "count": 1,
            "totalAverage": 250
          },
          {
            "numberDay": 6,
            "stringWeek": "Sábado",
            "date": "2022-12-09",
            "total": 900,
            "count": 1,
            "totalAverage": 900
          }
        ]
      },
      {
        "mes": "Sep",
        "count": 7,
        "total": 5596,
        "totalAverage": 1442.8999999999999,
        "sales": [
          {
            "numberDay": 2,
            "stringWeek": "Martes",
            "date": "2022-09-12",
            "total": 5069.2,
            "count": 4,
            "totalAverage": 1267.3
          },
          {
            "numberDay": 6,
            "stringWeek": "Sábado",
            "date": "2022-09-02",
            "total": 526.8,
            "count": 3,
            "totalAverage": 175.6
          }
        ]
      }
    ]
  },
  {
    "_id": 2021,
    "monthsWithSales": 1,
    "count": 2,
    "total": 608,
    "totalAverage": 608,
    "sales": [
      {
        "mes": "Dec",
        "count": 2,
        "total": 608,
        "totalAverage": 608,
        "sales": [
          {
            "numberDay": 1,
            "stringWeek": "Lunes",
            "date": "2021-12-12",
            "total": 171.2,
            "count": 1,
            "totalAverage": 171.2
          },
          {
            "numberDay": 4,
            "stringWeek": "Jueves",
            "date": "2021-12-22",
            "total": 436.8,
            "count": 1,
            "totalAverage": 436.8
          }
        ]
      }
    ]
  }
]

</code>```

It is correct, but as you can see at the end I make two groups to obtain the data grouped by year and then the sales array grouped by month.

Is there any way to improve this query without so much grouping?

ContentLicense: CC BY-SA 4.0

Title: Repetitive Query in MongoDB

Body:
I am creating a query in MongoDB which works correctly for me but in the end I have to do an extra grouping to get the response grouped by year. This is the query:

db.sales.aggregate([
  {
    $group: {
      _id: {
        month: {
          $month: "$createdAt"
        },
        year: {
          $year: "$createdAt"
        },
        dayOfWeek: {
            $dayOfWeek: "$createdAt"
        },
        stringDay: {
            $dateToString:
            { format: "%Y-%m-%d", date: "$createdAt"}
        },
        week: {
            $isoWeek: "$createdAt"
        }
      },
      total: { $sum: '$total'},
      count: { $sum: 1 },
      totalAverage: { $avg: '$total'}
    }
  },
  {
    $sort : {
    "_id.month" : 1
    }
  },
  {
    $project: {
      total:  { $round: [ "$total", 2 ] },
      year: "$_id.year",
      date: "$_id.date",
      week: "$_id.week",
      numVentas: "$count",
      month: "$_id.month",
      dayOfWeek: "$_id.dayOfWeek",
      stringDay:"$_id.stringDay",
      count: "$count",
      totalAverage: { $round: [ "$totalAverage", 2 ] },
      stringMonth: {
        $arrayElemAt: [
          [
            "",
            "Jan",
            "Feb",
            "Mar",
            "Apr",
            "May",
            "Jun",
            "Jul",
            "Aug",
            "Sep",
            "Oct",
            "Nov",
            "Dec"
          ],
          "$_id.month"
        ]
      },
      stringWeek: {
        $switch: {
            branches:[
                { case: { $eq: ["$_id.dayOfWeek", 1] }, then: "Lunes" },
                { case: { $eq: ["$_id.dayOfWeek", 2] }, then: "Martes" },
                { case: { $eq: ["$_id.dayOfWeek", 3] }, then: "Miércoles" },
                { case: { $eq: ["$_id.dayOfWeek", 4] }, then: "Jueves" },
                { case: { $eq: ["$_id.dayOfWeek", 5] }, then: "Viernes" },
                { case: { $eq: ["$_id.dayOfWeek", 6] }, then: "Sábado" },
                { case: { $eq: ["$_id.dayOfWeek", 7] }, then: "Domingo" }
            ],
            default: "Día desconocido"
            }
        }
    }
  },
  {
    $group: {
        _id:  { month: "$stringMonth", year: "$year"},
        count: { $sum: "$count" },
        total: { $sum: "$total" },
        totalAverage: { $sum: "$totalAverage" },
        sales: {
            $push: {
                numberDay: "$dayOfWeek",
                stringWeek: "$stringWeek",
                date: "$stringDay",
                total: "$total",
                count: "$count",
                totalAverage: { $round: [ "$totalAverage", 2 ] }
            }
        }
    }
  },
  {
    $group: {
        _id: "$_id.year",
        monthsWithSales: { $sum: 1 },
        count: { $sum: "$count" },
        total: { $sum: "$total" },
        totalAverage: { $sum: "$totalAverage" },
        sales: {
            $push: {
                mes: "$_id.month",
                count: "$count",
                total: "$total",
                totalAverage: "$totalAverage",
                sales:"$sales"
            }
        }
    }
  }

])

And I get this response:

[
  {
    "_id": 2022,
    "monthsWithSales": 4,
    "count": 57,
    "total": 22324.8,
    "totalAverage": 7765.799999999999,
    "sales": [
      {
        "mes": "Oct",
        "count": 10,
        "total": 1936,
        "totalAverage": 1233.6,
        "sales": [
          {
            "numberDay": 6,
            "stringWeek": "Sábado",
            "date": "2022-10-21",
            "total": 526.8,
            "count": 3,
            "totalAverage": 175.6
          },
          {
            "numberDay": 1,
            "stringWeek": "Lunes",
            "date": "2022-10-02",
            "total": 85.6,
            "count": 1,
            "totalAverage": 85.6
          },
          {
            "numberDay": 7,
            "stringWeek": "Domingo",
            "date": "2022-10-22",
            "total": 526.8,
            "count": 3,
            "totalAverage": 175.6
          },
          {
            "numberDay": 3,
            "stringWeek": "Miércoles",
            "date": "2022-10-04",
            "total": 180,
            "count": 1,
            "totalAverage": 180
          },
          {
            "numberDay": 4,
            "stringWeek": "Jueves",
            "date": "2022-10-12",
            "total": 531.2,
            "count": 1,
            "totalAverage": 531.2
          },
          {
            "numberDay": 3,
            "stringWeek": "Miércoles",
            "date": "2022-10-25",
            "total": 85.6,
            "count": 1,
            "totalAverage": 85.6
          }
        ]
      },
      {
        "mes": "Nov",
        "count": 7,
        "total": 2205.2,
        "totalAverage": 1014.8,
        "sales": [
          {
            "numberDay": 4,
            "stringWeek": "Jueves",
            "date": "2022-11-02",
            "total": 526.8,
            "count": 3,
            "totalAverage": 175.6
          },
          {
            "numberDay": 6,
            "stringWeek": "Sábado",
            "date": "2022-11-25",
            "total": 171.2,
            "count": 2,
            "totalAverage": 85.6
          },
          {
            "numberDay": 7,
            "stringWeek": "Domingo",
            "date": "2022-11-12",
            "total": 1507.2,
            "count": 2,
            "totalAverage": 753.6
          }
        ]
      },
      {
        "mes": "Dec",
        "count": 33,
        "total": 12587.6,
        "totalAverage": 4074.5,
        "sales": [
          {
            "numberDay": 3,
            "stringWeek": "Miércoles",
            "date": "2022-12-06",
            "total": 850,
            "count": 1,
            "totalAverage": 850
          },
          {
            "numberDay": 6,
            "stringWeek": "Sábado",
            "date": "2022-12-02",
            "total": 8737.6,
            "count": 25,
            "totalAverage": 349.5
          },
          {
            "numberDay": 7,
            "stringWeek": "Domingo",
            "date": "2022-12-10",
            "total": 900,
            "count": 1,
            "totalAverage": 900
          },
          {
            "numberDay": 1,
            "stringWeek": "Lunes",
            "date": "2022-12-04",
            "total": 200,
            "count": 1,
            "totalAverage": 200
          },
          {
            "numberDay": 2,
            "stringWeek": "Martes",
            "date": "2022-12-05",
            "total": 500,
            "count": 1,
            "totalAverage": 500
          },
          {
            "numberDay": 5,
            "stringWeek": "Viernes",
            "date": "2022-12-08",
            "total": 250,
            "count": 2,
            "totalAverage": 125
          },
          {
            "numberDay": 4,
            "stringWeek": "Jueves",
            "date": "2022-12-07",
            "total": 250,
            "count": 1,
            "totalAverage": 250
          },
          {
            "numberDay": 6,
            "stringWeek": "Sábado",
            "date": "2022-12-09",
            "total": 900,
            "count": 1,
            "totalAverage": 900
          }
        ]
      },
      {
        "mes": "Sep",
        "count": 7,
        "total": 5596,
        "totalAverage": 1442.8999999999999,
        "sales": [
          {
            "numberDay": 2,
            "stringWeek": "Martes",
            "date": "2022-09-12",
            "total": 5069.2,
            "count": 4,
            "totalAverage": 1267.3
          },
          {
            "numberDay": 6,
            "stringWeek": "Sábado",
            "date": "2022-09-02",
            "total": 526.8,
            "count": 3,
            "totalAverage": 175.6
          }
        ]
      }
    ]
  },
  {
    "_id": 2021,
    "monthsWithSales": 1,
    "count": 2,
    "total": 608,
    "totalAverage": 608,
    "sales": [
      {
        "mes": "Dec",
        "count": 2,
        "total": 608,
        "totalAverage": 608,
        "sales": [
          {
            "numberDay": 1,
            "stringWeek": "Lunes",
            "date": "2021-12-12",
            "total": 171.2,
            "count": 1,
            "totalAverage": 171.2
          },
          {
            "numberDay": 4,
            "stringWeek": "Jueves",
            "date": "2021-12-22",
            "total": 436.8,
            "count": 1,
            "totalAverage": 436.8
          }
        ]
      }
    ]
  }
]

It is correct, but as you can see at the end I make two groups to obtain the data grouped by year and then the sales array grouped by month.

Is there any way to improve this query without so much grouping?


问:
Specific dotted words (profile, htaccess) are breaking the website (403 Forbidden)
I've discovered that if I put some specific words starting with a dot into URL of my client's website – the website breaks.

I was able to get the most clear view of the situation when I've set an empty htaccess and try to query a test PHP file directly.

E.g.:
This kind of urls work fine, return 200s:

[mydomain]/test.php

[mydomain]/test.php?foo=bar

[mydomain]/test.php?foo=.bar

[mydomain]/test.php?foo=.profil

But these urls return 403 Forbidden:

[mydomain]/test.php?e=.profile

[mydomain]/test.php?e=.profiler

[mydomain]/test.php?.profile

[mydomain]/test.php?foo=.htaccess

[mydomain]/test.php?foo=.htpasswd

Unfortunately, I don't have access to SSH or cPanel, only FTP.
My guess is that the SELinunx is enforced. But I am much noob on the subject, so I wonder if there is any other way to deduct the cause of the problem.


问:
how to marge multiple video clips in cloud storage into one m3u8 file?
I have a library with multiple cloud video files. I would like to create a list m3u8 after firing in the player I will be able to change the video files. Is possible ??

https:The link to one video file plays without any problem. How to merge multiple files into one link containing a list of all files?


问:
ERROR: Flyway Teams Edition upgrade required: jdbc-secretsmanager is not supported by Flyway Community Edition
I am using flyway community edition v-7.0.4.
while trying to work it out with AWS jdbc-secretsmanager, ref. https://flywaydb.org/documentation/configuration/secretsManagement#dapr-secret-store, it throughs error ERROR: Flyway Teams Edition upgrade required: jdbc-secretsmanager is not supported by Flyway Community Edition
does AWS secretsmanager only supported for flyway teams which is commercial edition?

Thanks

Tried installing version 9 but have had same issue


问:
NoClassDefFoundError when calling apkanalyzer running cordova
I'm setting up an enviroment for deploying android app with cordova. The OS is Xubuntu 20.04.

I downloaded the sdk and jdk11 and set the enviroment variables in ~/.bashrc as follows

```<code>export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export ANDROID_SDK_ROOT=/home/nik/Android/Sdk
export ANDROID_HOME=/home/nik/Android/Sdk
export PATH=$ANDROID_HOME/build-tools:$PATH
export PATH=$ANDROID_HOME/emulator:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
</code>```

I created a project with cordova create, I add browser and ran it, everithing went good.
Building for android was a little tricky but i figured out. If i try to run with cordova run android i always get this error when is called apkanalyzer on my project, this is the output of cordova run android:

```<code>Checking Java JDK and Android SDK versions
ANDROID_HOME=/home/nik/Android/Sdk (recommended setting)
ANDROID_SDK_ROOT=/home/nik/Android/Sdk (DEPRECATED)
Using Android SDK: /home/nik/Android/Sdk
Subproject Path: CordovaLib
Subproject Path: app
Starting a Gradle Daemon (subsequent builds will be faster)

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.4.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 12s
50 actionable tasks: 50 up-to-date
Built the following apk(s):
        /home/nik/Desktop/cordova/first/platforms/android/app/build/outputs/apk/debug/app-debug.apk
Checking Java JDK and Android SDK versions
ANDROID_HOME=/home/nik/Android/Sdk (recommended setting)
ANDROID_SDK_ROOT=/home/nik/Android/Sdk (DEPRECATED)
Using Android SDK: /home/nik/Android/Sdk
Command failed with exit code 1: apkanalyzer manifest target-sdk /home/nik/Desktop/cordova/first/platforms/android/app/build/outputs/apk/debug/app-debug.apk
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
        at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
        at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
        at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
        at com.android.tools.apk.analyzer.ApkAnalyzerCli.getAaptInvokerFromSdk(ApkAnalyzerCli.java:277)
        at com.android.tools.apk.analyzer.ApkAnalyzerCli.main(ApkAnalyzerCli.java:129)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 5 more
</code>```

Googling I found out the problem could be the java version and so I tried to set JAVA_HOME to jdk8 and then to jdk17 and nothing changed.
The real problem is I can not test the app on my laptop but it actually works, after it was built I just moved the .apk on smartphone and installed it and got no problem.

I also follwed this link but I not solved.

Hope someone can help me, thank you in advance

Body:
I'm setting up an enviroment for deploying android app with cordova. The OS is Xubuntu 20.04.

I downloaded the sdk and jdk11 and set the enviroment variables in ~/.bashrc as follows

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export ANDROID_SDK_ROOT=/home/nik/Android/Sdk
export ANDROID_HOME=/home/nik/Android/Sdk
export PATH=$ANDROID_HOME/build-tools:$PATH
export PATH=$ANDROID_HOME/emulator:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH

I created a project with cordova create, I add browser and ran it, everithing went good.
Building for android was a little tricky but i figured out. If i try to run with cordova run android i always get this error when is called apkanalyzer on my project, this is the output of cordova run android:

Checking Java JDK and Android SDK versions
ANDROID_HOME=/home/nik/Android/Sdk (recommended setting)
ANDROID_SDK_ROOT=/home/nik/Android/Sdk (DEPRECATED)
Using Android SDK: /home/nik/Android/Sdk
Subproject Path: CordovaLib
Subproject Path: app
Starting a Gradle Daemon (subsequent builds will be faster)

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.4.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 12s
50 actionable tasks: 50 up-to-date
Built the following apk(s):
        /home/nik/Desktop/cordova/first/platforms/android/app/build/outputs/apk/debug/app-debug.apk
Checking Java JDK and Android SDK versions
ANDROID_HOME=/home/nik/Android/Sdk (recommended setting)
ANDROID_SDK_ROOT=/home/nik/Android/Sdk (DEPRECATED)
Using Android SDK: /home/nik/Android/Sdk
Command failed with exit code 1: apkanalyzer manifest target-sdk /home/nik/Desktop/cordova/first/platforms/android/app/build/outputs/apk/debug/app-debug.apk
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
        at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
        at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
        at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
        at com.android.tools.apk.analyzer.ApkAnalyzerCli.getAaptInvokerFromSdk(ApkAnalyzerCli.java:277)
        at com.android.tools.apk.analyzer.ApkAnalyzerCli.main(ApkAnalyzerCli.java:129)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 5 more

Googling I found out the problem could be the java version and so I tried to set JAVA_HOME to jdk8 and then to jdk17 and nothing changed.
The real problem is I can not test the app on my laptop but it actually works, after it was built I just moved the .apk on smartphone and installed it and got no problem.

I also follwed this link but I not solved.

Hope someone can help me, thank you in advance


问:
Dynamic array not behaving as expected
Replacing words/phrases in larger project. I can do it first time around. But when attempting to build on the changed line I have an issue with the dynamic array listings[]
Summarizing the issue. Here is some basic Python:

```<code># concurrentTest.py

import re

subString = [' string1',' string2',' string3']
word = ['new_word1', 'new_word2', 'new_word3']
lineString = ['This line, needs new_word','This line, needs new_word', 'This line, needs new_word']

# matches word with underscore
wordsRegex = "(\S+_\S+[a-z])"
# matches word ending in comma
stringRegex = "(\s[a-z]+,)"

# store results
listings = []

# make the first change to our line string with a new word for each line
for (x, y) in zip(subString, lineString):
    listings = re.sub(stringRegex, x, y)
    # print(listings)               # This shows that listings have been changed with a string number 'This string1 needs a new_word'

# try to make the second change to give 'This string1 needs a new_word1' etc
for (x, y) in zip(word, listings):
    listings = re.sub(wordsRegex, x, y)
    # print(listings)                 # If listings is declared [] like lineString why just first 3 letters 'Thi' printed?
</code>```

Title: Dynamic array not behaving as expected

Body:
Replacing words/phrases in larger project. I can do it first time around. But when attempting to build on the changed line I have an issue with the dynamic array listings[]
Summarizing the issue. Here is some basic Python:

# concurrentTest.py

import re

subString = [' string1',' string2',' string3']
word = ['new_word1', 'new_word2', 'new_word3']
lineString = ['This line, needs new_word','This line, needs new_word', 'This line, needs new_word']

# matches word with underscore
wordsRegex = "(\S+_\S+[a-z])"
# matches word ending in comma
stringRegex = "(\s[a-z]+,)"

# store results
listings = []

# make the first change to our line string with a new word for each line
for (x, y) in zip(subString, lineString):
    listings = re.sub(stringRegex, x, y)
    # print(listings)               # This shows that listings have been changed with a string number 'This string1 needs a new_word'

# try to make the second change to give 'This string1 needs a new_word1' etc
for (x, y) in zip(word, listings):
    listings = re.sub(wordsRegex, x, y)
    # print(listings)                 # If listings is declared [] like lineString why just first 3 letters 'Thi' printed?


问:
How to use tensor function of gam model in train function of caret package in r?
I am using gam method with both spline and tensor interaction functions(s and ti) inside the train function (for test and train).
I know for spline functions in gam we can use method = "gam" in train function. for example:

```<code>fit <- gam(Y ~ s(x1)  + s(x2) + s(x3) , data=df)
Prediction_gam <- as.numeric(predict(fit , data=df , type = "response"))
</code>```

can be changed to follow for test and train in caret package:

```<code>fit_train <-  train(Y ~ x1  + x2 + x3 , data = train_df, method = "gam", trControl = train.control)
</code>```

but I don't know how to add tensor interaction function of gam in train function for example:

```<code>fit <- gam(Y ~ s(x1)  + s(x2) + s(x3) + ti(x1,x2) , data=df)
</code>```

any suggestion would be appreciated.
the full codes are as follow:

```<code>library(caret)
df <- data.frame(Y=rnorm(100), x1=rnorm(100),x2=rnorm(100), x3=rnorm(100))
df <- as.data.frame(do.call(cbind, df))
set.seed(1)
training.samples <- df$x1%>%createDataPartition(p = 0.8, list = FALSE)
train_df  <- df[training.samples, ]
test_df <- df[-training.samples, ]
train.control <- trainControl(method = "repeatedcv", number = 10, repeats = 2)
fit_train <-  train(Y ~ x1  + x2 + x3 , data = train_df, method = "gam", trControl = train.control)
Prediction_train <- as.numeric(predict(fit_train , data=train_df , type = "raw"))
Prediction_test  <- as.numeric(predict(fit_train , newdata =test_df , type = "raw"))
</code>```

Body:
I am using gam method with both spline and tensor interaction functions(s and ti) inside the train function (for test and train).
I know for spline functions in gam we can use method = "gam" in train function. for example:

fit <- gam(Y ~ s(x1)  + s(x2) + s(x3) , data=df)
Prediction_gam <- as.numeric(predict(fit , data=df , type = "response"))

can be changed to follow for test and train in caret package:

fit_train <-  train(Y ~ x1  + x2 + x3 , data = train_df, method = "gam", trControl = train.control)

but I don't know how to add tensor interaction function of gam in train function for example:

fit <- gam(Y ~ s(x1)  + s(x2) + s(x3) + ti(x1,x2) , data=df)

any suggestion would be appreciated.
the full codes are as follow:

library(caret)
df <- data.frame(Y=rnorm(100), x1=rnorm(100),x2=rnorm(100), x3=rnorm(100))
df <- as.data.frame(do.call(cbind, df))
set.seed(1)
training.samples <- df$x1%>%createDataPartition(p = 0.8, list = FALSE)
train_df  <- df[training.samples, ]
test_df <- df[-training.samples, ]
train.control <- trainControl(method = "repeatedcv", number = 10, repeats = 2)
fit_train <-  train(Y ~ x1  + x2 + x3 , data = train_df, method = "gam", trControl = train.control)
Prediction_train <- as.numeric(predict(fit_train , data=train_df , type = "raw"))
Prediction_test  <- as.numeric(predict(fit_train , newdata =test_df , type = "raw"))


问:
comparison of variables in python
I have 2 variables in python as mentioned below and both have same integer values

>>> a = 256

>>> b = 256

When I compare them, I get True as mentioned below

>>> a is b

True

but again when I compare other 2 equal variables I get false

```<code>    c = 257

    d = 257

    c is d

    False
</code>```

Is there any problem in python Interpreter?

I didn't understood the logic behind it

Title: comparison of variables in python

Body:
I have 2 variables in python as mentioned below and both have same integer values

>>> a = 256

>>> b = 256

When I compare them, I get True as mentioned below

>>> a is b

True

but again when I compare other 2 equal variables I get false

    c = 257

    d = 257

    c is d

    False

Is there any problem in python Interpreter?

I didn't understood the logic behind it


问:
Document On Change Based on Parameter
I have a js file with 2 jQuery calls that call the same function toggleButton(). But the class name is a bit different - one is [name='./image/isBeautified'], and the other is [name='./headerImage/isBeautified']. What's the better way to avoid repetition and not call document on change twice?

```<code>(function (document, $, ns) {
    "use strict";

    function toggleButton(isBeautified) {
       //some logic to toggle
    }

    $(document).on("change", "[name='./image/isBeautified']", function(e) {
        toggleButton(e.target);
    });

    $(document).on("change", "[name='./headerImage/isBeautified']", function(e) {
        toggleButton(e.target);
    });

})(document, Granite.$, Granite.author);
</code>```

Title: Document On Change Based on Parameter

Body:
I have a js file with 2 jQuery calls that call the same function toggleButton(). But the class name is a bit different - one is [name='./image/isBeautified'], and the other is [name='./headerImage/isBeautified']. What's the better way to avoid repetition and not call document on change twice?

(function (document, $, ns) {
    "use strict";

    function toggleButton(isBeautified) {
       //some logic to toggle
    }

    $(document).on("change", "[name='./image/isBeautified']", function(e) {
        toggleButton(e.target);
    });

    $(document).on("change", "[name='./headerImage/isBeautified']", function(e) {
        toggleButton(e.target);
    });

})(document, Granite.$, Granite.author);


问:
Cross correlaing two data sets to find similarity
I have data sets of Heart Rate Variability data and i am trying to determine if the data collected each day is similar or related for data collected the next day. Please how can i go about this. I am looking to see if Dynamic time warping or cross correlation can do this but i am confused as to how to go about it. I am open to suggestions. I am hoping to write my code using either Matlab or Python

I have tried using Dynamic time warping and Cross correlation to compare the signals


问:
How to modify size of matplotlib bar graph?
As of now, my matplotlib graph has an awkward squished look as such in the following image...

What part of my code could I do to make it look normal like this example of a horizontal bar graph on Matplotlib's site: https://matplotlib.org/stable/gallery/lines_bars_and_markers/barh.html#sphx-glr-gallery-lines-bars-and-markers-barh-py

Here is what my code looks like which is partially modified from the one in the link. I wish I could make my bar graphs gradient, but I'm having trouble integrating that too since the data for the graph is from my own dictionaries:

```<code>np.random.seed(19680801)

    def gradient_image(ax, extent, direction=0.3, cmap_range=(0, 1), **kwargs):
        """
        Draw a gradient image based on a colormap.

        Parameters
        ----------
        ax : Axes
            The axes to draw on.
        extent
            The extent of the image as (xmin, xmax, ymin, ymax).
            By default, this is in Axes coordinates but may be
            changed using the *transform* keyword argument.
        direction : float
            The direction of the gradient. This is a number in
            range 0 (=vertical) to 1 (=horizontal).
        cmap_range : float, float
            The fraction (cmin, cmax) of the colormap that should be
            used for the gradient, where the complete colormap is (0, 1).
        **kwargs
            Other parameters are passed on to `.Axes.imshow()`.
            In particular useful is *cmap*.
        """
        phi = direction * np.pi / 2
        v = np.array([np.cos(phi), np.sin(phi)])
        X = np.array([[v @ [1, 0], v @ [1, 1]],
                      [v @ [0, 0], v @ [0, 1]]])
        a, b = cmap_range
        X = a + (b - a) / X.max() * X
        im = ax.imshow(X, extent=extent, interpolation='bicubic',
                       vmin=0, vmax=1, **kwargs)
        return im

    plt.rcdefaults()
    fig, ax = plt.subplots()

    # background image
    gradient_image(ax, direction=1, extent=(0, 1, 0, 1), transform=ax.transAxes,
                   cmap=plt.cm.RdYlGn, cmap_range=(0.2, 0.8), alpha=0.5)

    # Example data
    people = list(new_budgetdict.keys())
    y_pos = np.arange(len(people))
    budget = list(new_budgetdict.values())

    col = '#5DADE2'

    ax.barh(y_pos, budget, align='center', color=col)
    ax.set_yticks(y_pos, labels=people)
    ax.invert_yaxis()  # labels read top-to-bottom
    ax.set_xlabel('Budget ($)')
    ax.set_title('Weekly Budget of Clients')

    plt.show()
</code>```

Body:
As of now, my matplotlib graph has an awkward squished look as such in the following image...

What part of my code could I do to make it look normal like this example of a horizontal bar graph on Matplotlib's site: https://matplotlib.org/stable/gallery/lines_bars_and_markers/barh.html#sphx-glr-gallery-lines-bars-and-markers-barh-py

Here is what my code looks like which is partially modified from the one in the link. I wish I could make my bar graphs gradient, but I'm having trouble integrating that too since the data for the graph is from my own dictionaries:

np.random.seed(19680801)

    def gradient_image(ax, extent, direction=0.3, cmap_range=(0, 1), **kwargs):
        """
        Draw a gradient image based on a colormap.

        Parameters
        ----------
        ax : Axes
            The axes to draw on.
        extent
            The extent of the image as (xmin, xmax, ymin, ymax).
            By default, this is in Axes coordinates but may be
            changed using the *transform* keyword argument.
        direction : float
            The direction of the gradient. This is a number in
            range 0 (=vertical) to 1 (=horizontal).
        cmap_range : float, float
            The fraction (cmin, cmax) of the colormap that should be
            used for the gradient, where the complete colormap is (0, 1).
        **kwargs
            Other parameters are passed on to `.Axes.imshow()`.
            In particular useful is *cmap*.
        """
        phi = direction * np.pi / 2
        v = np.array([np.cos(phi), np.sin(phi)])
        X = np.array([[v @ [1, 0], v @ [1, 1]],
                      [v @ [0, 0], v @ [0, 1]]])
        a, b = cmap_range
        X = a + (b - a) / X.max() * X
        im = ax.imshow(X, extent=extent, interpolation='bicubic',
                       vmin=0, vmax=1, **kwargs)
        return im

    plt.rcdefaults()
    fig, ax = plt.subplots()

    # background image
    gradient_image(ax, direction=1, extent=(0, 1, 0, 1), transform=ax.transAxes,
                   cmap=plt.cm.RdYlGn, cmap_range=(0.2, 0.8), alpha=0.5)

    # Example data
    people = list(new_budgetdict.keys())
    y_pos = np.arange(len(people))
    budget = list(new_budgetdict.values())

    col = '#5DADE2'

    ax.barh(y_pos, budget, align='center', color=col)
    ax.set_yticks(y_pos, labels=people)
    ax.invert_yaxis()  # labels read top-to-bottom
    ax.set_xlabel('Budget ($)')
    ax.set_title('Weekly Budget of Clients')

    plt.show()


问:
How to decrypt an Apple Server-To-Server notification using PHP
I have been struggling with this subject for several days now. I am not sure whether Apple's Documentation is bad or the lack of code examples is the one thing that complicates everything. I am also surprised that it would seem almost no one is using it as I wasn't able to find good examples online.

I did the following process:

I added a sandbox webhook in App Information and I am running it via ngrok (tunnel) to check the information I receive.

After a while I was able to figure out what exactly I had to base64 url decode from the response and I ended up with an object with this structure.

Array
(
    [headers] => stdClass Object
        (
            [alg] => ES256
            [x5c] => Array
                (
                    [0] => ...,
                    [1] => ...,
                    [2] => ...,
                )

        )

    [payload] => stdClass Object
        (
            [notificationType] => DID_RENEW
            [notificationUUID] => ...uuid...
            [data] => stdClass Object
                (
                    [bundleId] => ...app.id...
                    [bundleVersion] => 1.5
                    [environment] => Sandbox
                    [signedTransactionInfo] => ... //long string of encrypted info
                )

            [version] => 2.0
            [signedDate] => 1670890743027
        )

    [signature] => ... // signature string
)

```

I have reduced the fields because of size, but it's basically as follows according to the docs:

headers->alg - is the algorithm used

headers->x5c - is an array of 3 keys

Citing Apple's Docs

The App Store signs transaction and renewal
information that you receive in App Store Server Notifications V2 and
in the App Store Server API. It uses the following x5c certificate
chain, in order:

A certificate that contains the public key that corresponds to the key
the App Store uses to digitally sign the JWS.

An Apple intermediate certificate from the Apple PKI site that starts with Worldwide Developer Relations.

An Apple root certificate.

After that:

payload is an object that contains the relevant information, the one being relevante here would be

payload->data->signedTransactionInfo

And last there's the

signature property which I am not sure what to do with.

From what I understand I need to decrypt a JWT using the keys given in the response. I tried using this PHP library, but I am not sure how to proceed. The data is very different and I am not sure what's the correspondence. I only know that it uses ES256, but not sure where to go from there.

Body:
I have been struggling with this subject for several days now. I am not sure whether Apple's Documentation is bad or the lack of code examples is the one thing that complicates everything. I am also surprised that it would seem almost no one is using it as I wasn't able to find good examples online.

I did the following process:

I added a sandbox webhook in App Information and I am running it via ngrok (tunnel) to check the information I receive.

After a while I was able to figure out what exactly I had to base64 url decode from the response and I ended up with an object with this structure.

Array
(
    [headers] => stdClass Object
        (
            [alg] => ES256
            [x5c] => Array
                (
                    [0] => ...,
                    [1] => ...,
                    [2] => ...,
                )

        )

    [payload] => stdClass Object
        (
            [notificationType] => DID_RENEW
            [notificationUUID] => ...uuid...
            [data] => stdClass Object
                (
                    [bundleId] => ...app.id...
                    [bundleVersion] => 1.5
                    [environment] => Sandbox
                    [signedTransactionInfo] => ... //long string of encrypted info
                )

            [version] => 2.0
            [signedDate] => 1670890743027
        )

    [signature] => ... // signature string
)

I have reduced the fields because of size, but it's basically as follows according to the docs:

headers->alg - is the algorithm used

headers->x5c - is an array of 3 keys

Citing Apple's Docs

The App Store signs transaction and renewal
information that you receive in App Store Server Notifications V2 and
in the App Store Server API. It uses the following x5c certificate
chain, in order:

A certificate that contains the public key that corresponds to the key
the App Store uses to digitally sign the JWS.

An Apple intermediate certificate from the Apple PKI site that starts with Worldwide Developer Relations.

An Apple root certificate.

After that:

payload is an object that contains the relevant information, the one being relevante here would be

payload->data->signedTransactionInfo

And last there's the

signature property which I am not sure what to do with.

From what I understand I need to decrypt a JWT using the keys given in the response. I tried using this PHP library, but I am not sure how to proceed. The data is very different and I am not sure what's the correspondence. I only know that it uses ES256, but not sure where to go from there.


问:
How to draw a colourmap that distincly shows the points when there is a lot of data?
I am currently plotting a graph where the x and y axes help me represent a unique point and at that point, I use a certain colour to indicate the frequency of the point. The points have a frequency from 0 to all the way until 200,000. Now having such a large frequency makes it really tough to distinguish between the colours in the graph and as a result, my graph looks as the following :

Now all the colours seem the same so it does not really help me to understand the frequency distribution.

My code :

```<code>x,y,z = zip(*_list)
plt.scatter(x,y, c=z, s=10, cmap="cividis", edgecolor="k")
plt.colorbar(label="values")

plt.show()
</code>```

So any help that will help me distinguish the colours would be really good.
Also, how can I change it from a frequency distribution to a probability distribution?

Body:
I am currently plotting a graph where the x and y axes help me represent a unique point and at that point, I use a certain colour to indicate the frequency of the point. The points have a frequency from 0 to all the way until 200,000. Now having such a large frequency makes it really tough to distinguish between the colours in the graph and as a result, my graph looks as the following :

Now all the colours seem the same so it does not really help me to understand the frequency distribution.

My code :

x,y,z = zip(*_list)
plt.scatter(x,y, c=z, s=10, cmap="cividis", edgecolor="k")
plt.colorbar(label="values")

plt.show()

So any help that will help me distinguish the colours would be really good.
Also, how can I change it from a frequency distribution to a probability distribution?


问:
How to write Global Channels to separate files, boost
I have a BOOST_LOG_GLOBAL_LOGGER(myLogger, severity_channel_logger_mt)
I created/added a console logger to this global logger within the BOOST_LOG_GLOBAL_LOGGER_INIT. how do I add a file_log that only logs records from specific channels. I have two file sinks setup, but log records end up in both files.

```<code>//in some function
shared_ptr<text_file_backend> backend = make_shared<text_file_backend>(
  keywords::filename = // provided path,
  keywords::filter = channel == "default");
shared_ptr<synchronous_sink<text_file_backend>> sink= shared_ptr<synchronous_sink<text_file_backend>>(new synchronous_sink<text_file_backend>(backend));
logging::core::get()->add_sink(sink);

//------------------

//in some other function
shared_ptr<text_file_backend> backend = make_shared<text_file_backend>(
  keywords::filename = // provided path,
  keywords::filter = channel == // provided channel);
shared_ptr<synchronous_sink<text_file_backend>> sink= shared_ptr<synchronous_sink<text_file_backend>>(new synchronous_sink<text_file_backend>(backend));
logging::core::get()->add_sink(sink);
</code>```
```<code>

I have tried adding another file sink with a filter to check a specific channel but it does not work .
</code>```

Body:
I have a BOOST_LOG_GLOBAL_LOGGER(myLogger, severity_channel_logger_mt)
I created/added a console logger to this global logger within the BOOST_LOG_GLOBAL_LOGGER_INIT. how do I add a file_log that only logs records from specific channels. I have two file sinks setup, but log records end up in both files.

//in some function
shared_ptr<text_file_backend> backend = make_shared<text_file_backend>(
  keywords::filename = // provided path,
  keywords::filter = channel == "default");
shared_ptr<synchronous_sink<text_file_backend>> sink= shared_ptr<synchronous_sink<text_file_backend>>(new synchronous_sink<text_file_backend>(backend));
logging::core::get()->add_sink(sink);

//------------------

//in some other function
shared_ptr<text_file_backend> backend = make_shared<text_file_backend>(
  keywords::filename = // provided path,
  keywords::filter = channel == // provided channel);
shared_ptr<synchronous_sink<text_file_backend>> sink= shared_ptr<synchronous_sink<text_file_backend>>(new synchronous_sink<text_file_backend>(backend));
logging::core::get()->add_sink(sink);

I have tried adding another file sink with a filter to check a specific channel but it does not work .


问:
ValueError: Calling `Model.predict` in graph mode is not supported when the `Model` instance was constructed with eager mode enabled
I am trying to make a prediction within a training loop, but I get the following error:

```<code>ValueError: Calling `Model.predict` in graph mode is not supported when the `Model` instance was constructed with eager mode enabled. Please construct your `Model` instance in graph mode or call `Model.predict` with eager mode enabled.
</code>```

I believe this was working without any issue using some early version of tensorflow2, but anything >= 2.5 returns this.

The code:

train function

generator

model.predict()

The latter contains the following part, which raises the error message:

```<code>import os
from scipy import signal
import soundfile
import matplotlib.pyplot as plt
import generator
import tensorflow as tf
from tensorflow.python.keras.backend import set_session

with args.graph.as_default():
        set_session(args.sess)

        X, Y_gt = generator.create_batch(args, args.load_val_data_path, [file_path], args.frame_neigh)

        # estimate mask
        Y_pred = args.model.predict(X)
</code>```

Body:
I am trying to make a prediction within a training loop, but I get the following error:

ValueError: Calling `Model.predict` in graph mode is not supported when the `Model` instance was constructed with eager mode enabled. Please construct your `Model` instance in graph mode or call `Model.predict` with eager mode enabled.

I believe this was working without any issue using some early version of tensorflow2, but anything >= 2.5 returns this.

The code:

train function

generator

model.predict()

The latter contains the following part, which raises the error message:

import os
from scipy import signal
import soundfile
import matplotlib.pyplot as plt
import generator
import tensorflow as tf
from tensorflow.python.keras.backend import set_session

with args.graph.as_default():
        set_session(args.sess)

        X, Y_gt = generator.create_batch(args, args.load_val_data_path, [file_path], args.frame_neigh)

        # estimate mask
        Y_pred = args.model.predict(X)


问:
What is considered `sys` time in bash time util?
I've been profiling my linux app and in my single threaded build I noticed real/user/sys being 1900/1250/140. If I add up user and sys its 1390 which is far from 1900. 1900-is 510 which is a LARGE percentage.

What's considered as part of sys? I thought it was all system calls. After measuring some more it appears calling write takes up 500ms which is much larger than what sys is reporting and seems to be almost the entire missing time.


问:
OR-Tools CP-Sat: Efficiently optimize toward an average value
I'm new to OR-Tools and CP-Sat I'm looking for guidance on how to most efficiently optimize sums of variables toward a common average value. Here's a very simplified example to help explain:

from ortools.sat.python import cp_model
from random import randint

model = cp_model.CpModel()

# There are three groups of 5 boolean variables: a, b, and c
a_variables = [model.NewBoolVar(f"a{n}") for n in range(5)]
sum_a = model.NewIntVar(0, 5, "sum_a")
model.Add(sum_a == cp_model.LinearExpr.Sum(a_variables))

b_variables = [model.NewBoolVar(f"b{n}") for n in range(5)]
sum_b = model.NewIntVar(0, 5, "sum_b")
model.Add(sum_b == cp_model.LinearExpr.Sum(b_variables))

c_variables = [model.NewBoolVar(f"c{n}") for n in range(5)]
sum_c = model.NewIntVar(0, 5, "sum_c")
model.Add(sum_c == cp_model.LinearExpr.Sum(c_variables))

# Assign a random sum for the boolean variables
model.Add(cp_model.LinearExpr.Sum(a_variables + b_variables + c_variables) == randint(0, 15))

# We want to optimize so that each group of variables a, b, and c has the same sum,
# or as close as possible.
desired_sum = 3

objectives = []
# Add objectives here

model.Minimize(cp_model.LinearExpr.Sum(objectives))

solver = cp_model.CpSolver()
solver.parameters.log_search_progress = True
solver.parameters.num_search_workers = 1
solver.Solve(model)

solution = {}
solution["sum_a"] = solver.Value(sum_a)
solution["sum_b"] = solver.Value(sum_b)
solution["sum_c"] = solver.Value(sum_c)
return solution
```
<p>It seems to me in order to guarantee a good result, the objectives should penalize differences non-linearly, so large differences get disproportionately heavier penalties compared to small distances. My best effort so far is to get the absolute value of the difference, then create a series of boolean variables that test for increasingly large differences, and penalize each of these variables more than the last.</p>
<pre class="lang-py prettyprint-override"><code>diff_a = model.NewIntVar(0, 3, "diff_a")
model.AddAbsEquality(diff_a, desired_sum - sum_a)
diff_b = model.NewIntVar(0, 3, "diff_b")
model.AddAbsEquality(diff_b, desired_sum - sum_b)
diff_c = model.NewIntVar(0, 3, "diff_c")
model.AddAbsEquality(diff_c, desired_sum - sum_c)

objectives = []

diff_a_penalties = [model.NewBoolVar(f"diff_a_{n}") for n in range(3)]
[model.Add(diff_a > n).OnlyEnforceIf(penalty) for n, penalty in enumerate(diff_a_penalties)]
[model.Add(diff_a <= n).OnlyEnforceIf(penalty.Not()) for n, penalty in enumerate(diff_a_penalties)]
objectives.append(cp_model.LinearExpr.WeightedSum(diff_a_penalties, range(1, 4)))

diff_b_penalties = [model.NewBoolVar(f"diff_b_{n}") for n in range(3)]
[model.Add(diff_b > n).OnlyEnforceIf(penalty) for n, penalty in enumerate(diff_b_penalties)]
[model.Add(diff_b <= n).OnlyEnforceIf(penalty.Not()) for n, penalty in enumerate(diff_b_penalties)]
objectives.append(cp_model.LinearExpr.WeightedSum(diff_b_penalties, range(1, 4)))

diff_c_penalties = [model.NewBoolVar(f"diff_c_{n}") for n in range(3)]
[model.Add(diff_c > n).OnlyEnforceIf(penalty) for n, penalty in enumerate(diff_c_penalties)]
[model.Add(diff_c <= n).OnlyEnforceIf(penalty.Not()) for n, penalty in enumerate(diff_c_penalties)]
objectives.append(cp_model.LinearExpr.WeightedSum(diff_c_penalties, range(1, 4)))
</code>```

This works well and is much faster than my first attempt, which was using AddMultiplicationEquality to square the differences. But I'm wondering if anyone has come up with more efficient ways to accomplish this optimization. My method certainly adds a lot of variables to the model.

Body:
I'm new to OR-Tools and CP-Sat I'm looking for guidance on how to most efficiently optimize sums of variables toward a common average value. Here's a very simplified example to help explain:

from ortools.sat.python import cp_model
from random import randint

model = cp_model.CpModel()

# There are three groups of 5 boolean variables: a, b, and c
a_variables = [model.NewBoolVar(f"a{n}") for n in range(5)]
sum_a = model.NewIntVar(0, 5, "sum_a")
model.Add(sum_a == cp_model.LinearExpr.Sum(a_variables))

b_variables = [model.NewBoolVar(f"b{n}") for n in range(5)]
sum_b = model.NewIntVar(0, 5, "sum_b")
model.Add(sum_b == cp_model.LinearExpr.Sum(b_variables))

c_variables = [model.NewBoolVar(f"c{n}") for n in range(5)]
sum_c = model.NewIntVar(0, 5, "sum_c")
model.Add(sum_c == cp_model.LinearExpr.Sum(c_variables))

# Assign a random sum for the boolean variables
model.Add(cp_model.LinearExpr.Sum(a_variables + b_variables + c_variables) == randint(0, 15))

# We want to optimize so that each group of variables a, b, and c has the same sum,
# or as close as possible.
desired_sum = 3

objectives = []
# Add objectives here

model.Minimize(cp_model.LinearExpr.Sum(objectives))

solver = cp_model.CpSolver()
solver.parameters.log_search_progress = True
solver.parameters.num_search_workers = 1
solver.Solve(model)

solution = {}
solution["sum_a"] = solver.Value(sum_a)
solution["sum_b"] = solver.Value(sum_b)
solution["sum_c"] = solver.Value(sum_c)
return solution

It seems to me in order to guarantee a good result, the objectives should penalize differences non-linearly, so large differences get disproportionately heavier penalties compared to small distances. My best effort so far is to get the absolute value of the difference, then create a series of boolean variables that test for increasingly large differences, and penalize each of these variables more than the last.

diff_a = model.NewIntVar(0, 3, "diff_a")
model.AddAbsEquality(diff_a, desired_sum - sum_a)
diff_b = model.NewIntVar(0, 3, "diff_b")
model.AddAbsEquality(diff_b, desired_sum - sum_b)
diff_c = model.NewIntVar(0, 3, "diff_c")
model.AddAbsEquality(diff_c, desired_sum - sum_c)

objectives = []

diff_a_penalties = [model.NewBoolVar(f"diff_a_{n}") for n in range(3)]
[model.Add(diff_a > n).OnlyEnforceIf(penalty) for n, penalty in enumerate(diff_a_penalties)]
[model.Add(diff_a <= n).OnlyEnforceIf(penalty.Not()) for n, penalty in enumerate(diff_a_penalties)]
objectives.append(cp_model.LinearExpr.WeightedSum(diff_a_penalties, range(1, 4)))

diff_b_penalties = [model.NewBoolVar(f"diff_b_{n}") for n in range(3)]
[model.Add(diff_b > n).OnlyEnforceIf(penalty) for n, penalty in enumerate(diff_b_penalties)]
[model.Add(diff_b <= n).OnlyEnforceIf(penalty.Not()) for n, penalty in enumerate(diff_b_penalties)]
objectives.append(cp_model.LinearExpr.WeightedSum(diff_b_penalties, range(1, 4)))

diff_c_penalties = [model.NewBoolVar(f"diff_c_{n}") for n in range(3)]
[model.Add(diff_c > n).OnlyEnforceIf(penalty) for n, penalty in enumerate(diff_c_penalties)]
[model.Add(diff_c <= n).OnlyEnforceIf(penalty.Not()) for n, penalty in enumerate(diff_c_penalties)]
objectives.append(cp_model.LinearExpr.WeightedSum(diff_c_penalties, range(1, 4)))

This works well and is much faster than my first attempt, which was using AddMultiplicationEquality to square the differences. But I'm wondering if anyone has come up with more efficient ways to accomplish this optimization. My method certainly adds a lot of variables to the model.


问:
Having this "Duplicate callback outputs" error using dash in Python . Any Clue?
**I have a dash App with a bootstrap sidebar where I push some buttons to jump from page to page. I have 7 of them, but one doesn't work properly  **

```<code>app = dash.Dash(__name__, external_stylesheets=[dbc.themes.JOURNAL])
</code>```
```<code>content = (html.Div(id="page-content", style={"padding": "2rem"}))

app.layout = dbc.Container([
    dcc.Location(id="url"),
    dbc.Row([
        dbc.Col(sidebar, width=2),
        dbc.Col(content, width=9, style={"margin-left": "18rem"})
    ])
], fluid=True)

@app.callback(
    Output("page-content", "children"),
    [Input("url", "pathname")]
)

def render_page_content(pathname):  # Home page
    if pathname == "/":
        return (...) # Here I return a image and some text --> Works well

    elif pathname == "/page-1":
      return (...) # Here I return some graphs and some text --> Works well
</code>```

(... pages 2-4 and 6-7... are similar to page 3) --> Work well
but in page 5 I have this:

```<code>    elif pathname == "/page-5":

        @app.callback(
            Output("histogram-graph", "figure"),
            [Input("radio-buttons", "value")]
        )

        def update_histogram_chart(value):
            if value == "age":
                ttl = "age"
</code>```

(... continue with some more elif...)

```<code>            px.histogram(df, x=value,... # --> **Generate an histogram**

radio_buttons = dcc.RadioItems(
    id="radio-buttons",
    options=[
        {"label": "Age", "value": "age"},
</code>```

(... continue with radio buttons options...)

And then I return

```<code>return (
    html.H1('Some text'),
    html.Br(),
    html.Div(
        children=[
            radio_buttons,
            dcc.Graph(id="histogram-graph")
        ])
)

if __name__ == '__main__':
    app.run_server(debug=True)

</code>```

It seems that the solution is to combine the two callback in one. I tried to do it, but it doesn't work, because the first one is to allow me to move from page to page when I push the buttons, and the other is to display each graphs binded  with the radio_button options.
Like the code is written sometime it works and sometimes not and give me the error.
Have been some day in this and nothing. Any tips?

Body:
**I have a dash App with a bootstrap sidebar where I push some buttons to jump from page to page. I have 7 of them, but one doesn't work properly  **

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.JOURNAL])

content = (html.Div(id="page-content", style={"padding": "2rem"}))

app.layout = dbc.Container([
    dcc.Location(id="url"),
    dbc.Row([
        dbc.Col(sidebar, width=2),
        dbc.Col(content, width=9, style={"margin-left": "18rem"})
    ])
], fluid=True)

@app.callback(
    Output("page-content", "children"),
    [Input("url", "pathname")]
)

def render_page_content(pathname):  # Home page
    if pathname == "/":
        return (...) # Here I return a image and some text --> Works well

    elif pathname == "/page-1":
      return (...) # Here I return some graphs and some text --> Works well

(... pages 2-4 and 6-7... are similar to page 3) --> Work well
but in page 5 I have this:

    elif pathname == "/page-5":

        @app.callback(
            Output("histogram-graph", "figure"),
            [Input("radio-buttons", "value")]
        )

        def update_histogram_chart(value):
            if value == "age":
                ttl = "age"

(... continue with some more elif...)

            px.histogram(df, x=value,... # --> **Generate an histogram**

radio_buttons = dcc.RadioItems(
    id="radio-buttons",
    options=[
        {"label": "Age", "value": "age"},

(... continue with radio buttons options...)

And then I return

return (
    html.H1('Some text'),
    html.Br(),
    html.Div(
        children=[
            radio_buttons,
            dcc.Graph(id="histogram-graph")
        ])
)

if __name__ == '__main__':
    app.run_server(debug=True)

It seems that the solution is to combine the two callback in one. I tried to do it, but it doesn't work, because the first one is to allow me to move from page to page when I push the buttons, and the other is to display each graphs binded  with the radio_button options.
Like the code is written sometime it works and sometimes not and give me the error.
Have been some day in this and nothing. Any tips?


问:
Weird bug saying vector must be length of 2 while I did explicitly assign it as expected

I have this weird bug that says

center must be a vector of length 2

I really don't understand this bug because I explicitly assigned centre as a vector of size 2. Can anyone shed any light?

Thanks in advance! Sorry if this can be formulated better.

This bug is so strange that I really don't know how to formulate it better.


问:
How to use full_join command (dplyr) in R Shiny?
I am trying to join two .csv data frames in R Shiny based on common elements between the two files. I have managed to do this in R by using the full_join function from the dplyr package but I am quite new to R Shiny and am struggling with how to get it to work there...

This is the code I have used in R to get it to work:

```<code>file3 = full_join(file1, file2, by = c("DNA.Barcode" = "Tube.ID"))
</code>```

"DNA.Barcode" and "Tube.ID" are the columns in the two different files that have the same values. These two columns are of different lengths and the IDs are in different orders. I am trying to match up the rows with the common values to output one merged data frame, but when I try to convert this code to R shiny it keeps giving me errors.

This is the R Shiny code I have been using so far:

```<code>library(shiny)
library(DT)
library(dplyr)

ui<-fluidPage(sidebarLayout(

    sidebarPanel(
        fileInput("file1", "Upload first file (.csv)", multiple = FALSE, accept = ".csv"),
        fileInput("file2", "Upload second file (.csv)", multiple = FALSE, accept = ".csv"),
    ),

    mainPanel(
        DTOutput("combine")
    )

))

server <- function(input,output,session){

    DAT_A <- reactive({
        req(input$file1)
        inData1 <- input$file1
        if (is.null(inData1)){ return(NULL) }
        mydata1 <- read.csv(inData1$datapath)
    })

    DAT_B <- reactive({
        req(input$file2)
        inData2 <- input$file2
        if (is.null(inData2)){ return(NULL) }
        mydata2 <- read.csv(inData2$datapath)
    })

    output$combine = renderDT({
        c_all <- full_join(DAT_A(), DAT_B(), by = c("DNA.Barcode" = "Tube.ID"))

    })

}

shinyApp(ui = ui, server = server)
</code>```

This gives two errors after I have uploaded the two files within the Shiny app:

```<code>Warning: Error in full_join: Join columns must be present in data.
✖ Problem with `DNA.Barcode`.
</code>```

...and...

```<code>Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
</code>```

I've done a few variations of this code but keep getting errors. I am 90% sure it's to do with how I've named the columns that I want to join together, but I haven't been using R Shiny long enough to figure out what to do to fix it and I can't seem to find anything online with people using full_join in an R Shiny context.

Any suggestions or other ways I could achieve this would be a huge help.

Body:
I am trying to join two .csv data frames in R Shiny based on common elements between the two files. I have managed to do this in R by using the full_join function from the dplyr package but I am quite new to R Shiny and am struggling with how to get it to work there...

This is the code I have used in R to get it to work:

file3 = full_join(file1, file2, by = c("DNA.Barcode" = "Tube.ID"))

"DNA.Barcode" and "Tube.ID" are the columns in the two different files that have the same values. These two columns are of different lengths and the IDs are in different orders. I am trying to match up the rows with the common values to output one merged data frame, but when I try to convert this code to R shiny it keeps giving me errors.

This is the R Shiny code I have been using so far:

library(shiny)
library(DT)
library(dplyr)

ui<-fluidPage(sidebarLayout(

    sidebarPanel(
        fileInput("file1", "Upload first file (.csv)", multiple = FALSE, accept = ".csv"),
        fileInput("file2", "Upload second file (.csv)", multiple = FALSE, accept = ".csv"),
    ),

    mainPanel(
        DTOutput("combine")
    )

))

server <- function(input,output,session){

    DAT_A <- reactive({
        req(input$file1)
        inData1 <- input$file1
        if (is.null(inData1)){ return(NULL) }
        mydata1 <- read.csv(inData1$datapath)
    })

    DAT_B <- reactive({
        req(input$file2)
        inData2 <- input$file2
        if (is.null(inData2)){ return(NULL) }
        mydata2 <- read.csv(inData2$datapath)
    })

    output$combine = renderDT({
        c_all <- full_join(DAT_A(), DAT_B(), by = c("DNA.Barcode" = "Tube.ID"))

    })

}

shinyApp(ui = ui, server = server)

This gives two errors after I have uploaded the two files within the Shiny app:

Warning: Error in full_join: Join columns must be present in data.
✖ Problem with `DNA.Barcode`.

...and...

Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.

I've done a few variations of this code but keep getting errors. I am 90% sure it's to do with how I've named the columns that I want to join together, but I haven't been using R Shiny long enough to figure out what to do to fix it and I can't seem to find anything online with people using full_join in an R Shiny context.

Any suggestions or other ways I could achieve this would be a huge help.


问:
Getting TypeError int() argument must be a string, a bytes-like object or a number, not 'NoneType'
I am creating a shopping cart using Django.  I can add items from a product detail view but once I get to the shopping cart I can't update items.  I can remove them but when I update I get the above error

I think that the form is not getting what I am putting into the function but I don't know why.  Any help or guidance would be really appreciated.

Form
Add to Cart View
Adjust Cart View
Error Message
traceback


问:
Why my express-session keeps getting initialized every request?
It seems that the session keeps getting initialized.
When a request is received from one server, the information stored in the session keeps getting lost.

Currently using express-session / connect-pg-simple (postgresql DB) and tested using express-session, fileStore, memoryStore.

ex) login (post request) userID and PW are compared with the DB, and if there is a corresponding user, userID and is_logined: true are stored in the session, and then when another page is requested using redirect (“/“) req.session.user_id , req.session.is_logined not found (undefined)

my app.js

```<code>//modules
import express from "express";
import expressEjsLayouts from "express-ejs-layouts";
import ejs from "ejs";
import cookieParser from "cookie-parser";
import session from "express-session";
import pgSession from "connect-pg-simple";
let pgSessions = pgSession(session);
import loger from "morgan";
import path from 'path';
import { fileURLToPath } from 'url';
import pg from 'pg';
import randomString from 'randomstring';
import db_connection from './config/db_config.json' assert {type: "json"};

//route
import mainRoute from "./router/main.js";
import userRoute from "./router/user.js";
import partnerRoute from "./router/partner.js";
import estimateRoute from "./router/estimate.js";
import paymentRoute from "./router/payment.js";
import statisticsRoute from "./router/statistics.js";
import authRoute from "./router/auth.js";

//config
const app = express();

//path const variable
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

//session setting
const sessionDBaccess = new pg.Pool(db_connection);
const sessionMiddleware = {
    store: new pgSessions({
        pool: sessionDBaccess,
        tableName: 'session'
    }),
    name: 'SID',
    secret: "secret key",
    resave: false,
    saveUninitialized: true,
    cookie: {
        maxAge: 1000 * 60 * 60 * 24 * 7,
        sameSite: "none",
        secure: false // ENABLE ONLY ON HTTPS
    }
};
app.use(session(sessionMiddleware));

//static file setting
app.use(express.static(path.join(__dirname, "public")));
app.set("views", path.join(__dirname, "/views"));

//set templete, templete engine
app.use(expressEjsLayouts);
app.set('layout', 'layout/layout');
app.set('layout extractScripts', true);
app.set('view engine', 'ejs');
app.engine('html', ejs.renderFile);

//port setting
app.set("port", process.env.PORT || 3002);

//app log settings
app.use(loger('dev'));

//request middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

//request cookie parser middleware
app.use(cookieParser(process.env.COOKIE_SECRET));

//server setting
const server = app.listen(app.get("port"), () => {
    console.log(app.get("port"), "번 포트에서 실행중");
})

// route

app.use('/', mainRoute);

app.use('/user', userRoute);

app.use('/partner', partnerRoute);

app.use('/estimate', estimateRoute);

app.use('/payment', paymentRoute);

app.use('/statistics', statisticsRoute);

app.use('/auth', authRoute);
</code>```

my main.js

```<code>import express from "express";
import database from "../config/db.js";

const router = express.Router();
const conn = database;

router.get("/", (req, res) => {
    console.log("메인페이지");
    var session = req.session;
    console.log(session);
    // if (!req.session.userInfo) {
    //     res.redirect('/auth');
    // } else {
    //     res.render('mainPage.ejs', { title: '메인페이지' });
    // }'
    res.render('mainPage.ejs', { title: '메인페이지' });

});

export default router;
</code>```

my auth.js

```<code>import express from "express";
import database from "../config/db.js";

const router = express.Router();
const conn = database;

let authData = {
    id: '',
    password: '',
}

router.get('/', (req, res) => {
    console.log(req.session);
    res.render('login.ejs', { title: '로그인페이지', id: null });
})
router.post('/login', (req, res) => {
    authData.id = req.body.id;
    authData.password = req.body.password;
    var sess = req.session;
    const loginSQL = `SELECT * FROM ACCOUNT_TEST WHERE UID = '${authData.id}' AND UPW = '${authData.password}'`;
    conn.query(loginSQL, (error, result) => {
        if (error) {
            console.log(error);
        } else {
            console.log(result.rowCount);
            if (result.rowCount) {
                let userInfo = {
                    user_id: authData.id,
                    is_logined: true
                }
                sess.user_id = authData.id;
                sess.is_logined = true;
                sess.save(err => {
                    if (err) throw err;
                    setTimeout(() => res.redirect(302, '/'), 5000);

                });
                // req.session.userInfo = userInfo;
                // req.session.save(err => {
                    // if(err) throw err;
                    // res.redirect('/');
                // });
            }
            else { res.redirect('/auth') };
        }
    });
});

export default router;
</code>```

And i tried just project for express-session practice (is not es module)
It using express-generator

ex) I made a logic that does session.num += 1 when requesting get (“/“), but num continues to be initialized and output as 1.

app.js

```<code>var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require("express-session");

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser("sdfgdlkfjngjk45er#@"));
app.use(
  session({
    secret: "sdfgdlkfjngjk45er#@",
    resave: false,
    saveUninitialized: true,
    cookie: {
      httpOnly: true,
      secure: false
    }
  })
); app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

</code>```

index.js

```<code>var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function (req, res, next) {
  console.log(req.session);
  if (req.session.num === undefined) {
    req.session.num = 1;
  } else {
    req.session.num++;
  }
  console.log(req.session);

  res.render('index', { title: 'Express', req: req.session.num });
});

module.exports = router;

</code>```

index.ejs

```<code><!DOCTYPE html>
<html>

<head>
  <title><%= title %></title>
  <link rel='stylesheet' href='/stylesheets/style.css' />
</head>

<body>
  <h1><%= title %></h1>
  <p>Welcome to <%= req %></p>
  <a href="/">adsfasdfasdf</a>
</body>

</html>
</code>```

I'm expecting login functions and session keeps (using dbstore).
I want to how to using session correctly

I resolved. problem is chrome browser did cookie block

Body:
It seems that the session keeps getting initialized.
When a request is received from one server, the information stored in the session keeps getting lost.

Currently using express-session / connect-pg-simple (postgresql DB) and tested using express-session, fileStore, memoryStore.

ex) login (post request) userID and PW are compared with the DB, and if there is a corresponding user, userID and is_logined: true are stored in the session, and then when another page is requested using redirect (“/“) req.session.user_id , req.session.is_logined not found (undefined)

my app.js

//modules
import express from "express";
import expressEjsLayouts from "express-ejs-layouts";
import ejs from "ejs";
import cookieParser from "cookie-parser";
import session from "express-session";
import pgSession from "connect-pg-simple";
let pgSessions = pgSession(session);
import loger from "morgan";
import path from 'path';
import { fileURLToPath } from 'url';
import pg from 'pg';
import randomString from 'randomstring';
import db_connection from './config/db_config.json' assert {type: "json"};

//route
import mainRoute from "./router/main.js";
import userRoute from "./router/user.js";
import partnerRoute from "./router/partner.js";
import estimateRoute from "./router/estimate.js";
import paymentRoute from "./router/payment.js";
import statisticsRoute from "./router/statistics.js";
import authRoute from "./router/auth.js";

//config
const app = express();

//path const variable
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

//session setting
const sessionDBaccess = new pg.Pool(db_connection);
const sessionMiddleware = {
    store: new pgSessions({
        pool: sessionDBaccess,
        tableName: 'session'
    }),
    name: 'SID',
    secret: "secret key",
    resave: false,
    saveUninitialized: true,
    cookie: {
        maxAge: 1000 * 60 * 60 * 24 * 7,
        sameSite: "none",
        secure: false // ENABLE ONLY ON HTTPS
    }
};
app.use(session(sessionMiddleware));

//static file setting
app.use(express.static(path.join(__dirname, "public")));
app.set("views", path.join(__dirname, "/views"));

//set templete, templete engine
app.use(expressEjsLayouts);
app.set('layout', 'layout/layout');
app.set('layout extractScripts', true);
app.set('view engine', 'ejs');
app.engine('html', ejs.renderFile);

//port setting
app.set("port", process.env.PORT || 3002);

//app log settings
app.use(loger('dev'));

//request middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

//request cookie parser middleware
app.use(cookieParser(process.env.COOKIE_SECRET));

//server setting
const server = app.listen(app.get("port"), () => {
    console.log(app.get("port"), "번 포트에서 실행중");
})

// route

app.use('/', mainRoute);

app.use('/user', userRoute);

app.use('/partner', partnerRoute);

app.use('/estimate', estimateRoute);

app.use('/payment', paymentRoute);

app.use('/statistics', statisticsRoute);

app.use('/auth', authRoute);

my main.js

import express from "express";
import database from "../config/db.js";

const router = express.Router();
const conn = database;

router.get("/", (req, res) => {
    console.log("메인페이지");
    var session = req.session;
    console.log(session);
    // if (!req.session.userInfo) {
    //     res.redirect('/auth');
    // } else {
    //     res.render('mainPage.ejs', { title: '메인페이지' });
    // }'
    res.render('mainPage.ejs', { title: '메인페이지' });

});

export default router;

my auth.js

import express from "express";
import database from "../config/db.js";

const router = express.Router();
const conn = database;

let authData = {
    id: '',
    password: '',
}

router.get('/', (req, res) => {
    console.log(req.session);
    res.render('login.ejs', { title: '로그인페이지', id: null });
})
router.post('/login', (req, res) => {
    authData.id = req.body.id;
    authData.password = req.body.password;
    var sess = req.session;
    const loginSQL = `SELECT * FROM ACCOUNT_TEST WHERE UID = '${authData.id}' AND UPW = '${authData.password}'`;
    conn.query(loginSQL, (error, result) => {
        if (error) {
            console.log(error);
        } else {
            console.log(result.rowCount);
            if (result.rowCount) {
                let userInfo = {
                    user_id: authData.id,
                    is_logined: true
                }
                sess.user_id = authData.id;
                sess.is_logined = true;
                sess.save(err => {
                    if (err) throw err;
                    setTimeout(() => res.redirect(302, '/'), 5000);

                });
                // req.session.userInfo = userInfo;
                // req.session.save(err => {
                    // if(err) throw err;
                    // res.redirect('/');
                // });
            }
            else { res.redirect('/auth') };
        }
    });
});

export default router;

And i tried just project for express-session practice (is not es module)
It using express-generator

ex) I made a logic that does session.num += 1 when requesting get (“/“), but num continues to be initialized and output as 1.

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var session = require("express-session");

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser("sdfgdlkfjngjk45er#@"));
app.use(
  session({
    secret: "sdfgdlkfjngjk45er#@",
    resave: false,
    saveUninitialized: true,
    cookie: {
      httpOnly: true,
      secure: false
    }
  })
); app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

index.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function (req, res, next) {
  console.log(req.session);
  if (req.session.num === undefined) {
    req.session.num = 1;
  } else {
    req.session.num++;
  }
  console.log(req.session);

  res.render('index', { title: 'Express', req: req.session.num });
});

module.exports = router;

index.ejs

<!DOCTYPE html>

  <title><%= title %></title>
  <link rel='stylesheet' href='/stylesheets/style.css' />

<%= title %>

Welcome to <%= req %>

  adsfasdfasdf

I'm expecting login functions and session keeps (using dbstore).
I want to how to using session correctly

I resolved. problem is chrome browser did cookie block


问:
dataclass json for python make optional attribute
Having this dataclass wish to be reuse in 2 different scenario

```<code>class TestResponse:
    name: str
    parameters: Optional[list[ActionParameters]]
</code>```

when call it with

```<code>TestResponse(
            name=name,
        )
</code>```

without adding parameter attribute, will hit an error. how to make this attribute to be something like optional?

Body:
Having this dataclass wish to be reuse in 2 different scenario

class TestResponse:
    name: str
    parameters: Optional[list[ActionParameters]]

when call it with

TestResponse(
            name=name,
        )

without adding parameter attribute, will hit an error. how to make this attribute to be something like optional?


问:
I have a question how removes widget with ids kivy
Do not know how to remove widget With ids, example remove a label using ids with the remove widget

Ignore is part and only to be able to emviaIgnore is part and only to be able to send


问:
Can't create a chrome window and update its html from a background script
Problem

I'm building a chrome extension that injects some buttons into a webpage and, when clicked, allows a user to send some files to my backend server. After it sends those files, I get a response back from the server.

I want to display that response in a separate window, but when I create the window, it doesn't change the display text and uses the default text I put in the p tag.

Additionally, I get this error from my service worker when trying to send the window a message from my background.js that contains the data I want to be displayed.

"Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist."

Background

The communication line I have setup is as follows.

I have a content script that calls my injection script to inject buttons into the web page.

When the buttons send a message to the server and the response is received, I send that information back to the content script via window.postMessage().

My content script has a listener, sending the message to the background script.

The background script has an onMessage listener that creates a new chrome tab and window. The chrome tab itself uses the chrome.runtime.getURL to create the HTML page. The HTML page is linked to a script called notification.js. Notification js has a listener that should receive messages sent from the background.js which will modify what is displayed.

Currently, though the page only pops up and its text isn't modified.

Is there another method I can use to update my newly created window? Or is this the incorrect way to be sending messages for chrome extensions.

Code

Content Script

```<code>function injectScript(file_path, tag){
    let node = document.body;
    let script = document.createElement("script");
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', file_path);
    console.log(script);

    node.appendChild(script);
}

let script_url = chrome.runtime.getURL("scripts/inject_script.js");
injectScript(script_url, 'body');

try {
    window.addEventListener("message", function(event) {

        if (event.data.type
            && (event.data.type == "FROM_PAGE")) {

            console.log("sending message to bg")
            chrome.runtime.sendMessage({output: event.data = "waiting"}, (response) => {
                console.log(response);
            });
        }

    }, false);
}catch (e) {
    console.log(e);
}
</code>```

Inject Script

```<code>        fileInput.addEventListener("change", function (){
            const formData = new FormData();
            formData.append("files", fileInput.files[0]);
            formData.append("courseId", courseId);
            formData.append("assignmentId", assignmentId);
            formData.append("userType", "STUDENT")

            fetch(endpoint, {
                method: "POST",
                headers: new Headers({
                    'Authorization': bearerToken
                }),
                body: formData
            }).catch(console.error).then( async response =>  {
                let fileSubmit_response = await response.json();

                console.log(fileSubmit_response);
                alert(fileSubmit_response.output);

                let output = fileSubmit_response.output;

                console.log("sending message to content")
                window.postMessage( {type: "FROM_PAGE", output});

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

Background Script

```<code>try {

    chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
        console.log(message.output);
        console.log(sender);

        sendResponse("msg recieved");

        if (message.output === 'waiting') {
            chrome.tabs.create({url: chrome.runtime.getURL('components/compilation_notification.html'), active: false}, function(tab)
            {
                // After the tab has been created, open a window to inject the tab
                chrome.windows.create({
                    tabId: tab.id,
                    type: 'popup',
                    focused: true
                    // incognito, top, left, ...
                })

                console.log("before message is sent")

                //should send data to newly created tab
                chrome.tabs.sendMessage(tab.id, {output: message.output, type: "compilation_results"}, (response) => {
                    console.log(response);
                });

            });
        }
    })
}

catch (e) {
    console.log(e)
}

</code>```

Notification.js

```<code>//script that is being used in compilation_notification.html
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {

    if (msg.type === 'compilation_results') {

        sendResponse("confirmed");

        document.getElementById('update-text').value = msg.output;
    }
});
</code>```

Body:
Problem

I'm building a chrome extension that injects some buttons into a webpage and, when clicked, allows a user to send some files to my backend server. After it sends those files, I get a response back from the server.

I want to display that response in a separate window, but when I create the window, it doesn't change the display text and uses the default text I put in the p tag.

Additionally, I get this error from my service worker when trying to send the window a message from my background.js that contains the data I want to be displayed.

"Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist."

Background

The communication line I have setup is as follows.

I have a content script that calls my injection script to inject buttons into the web page.

When the buttons send a message to the server and the response is received, I send that information back to the content script via window.postMessage().

My content script has a listener, sending the message to the background script.

The background script has an onMessage listener that creates a new chrome tab and window. The chrome tab itself uses the chrome.runtime.getURL to create the HTML page. The HTML page is linked to a script called notification.js. Notification js has a listener that should receive messages sent from the background.js which will modify what is displayed.

Currently, though the page only pops up and its text isn't modified.

Is there another method I can use to update my newly created window? Or is this the incorrect way to be sending messages for chrome extensions.

Code

Content Script

function injectScript(file_path, tag){
    let node = document.body;
    let script = document.createElement("script");
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', file_path);
    console.log(script);

    node.appendChild(script);
}

let script_url = chrome.runtime.getURL("scripts/inject_script.js");
injectScript(script_url, 'body');

try {
    window.addEventListener("message", function(event) {

        if (event.data.type
            && (event.data.type == "FROM_PAGE")) {

            console.log("sending message to bg")
            chrome.runtime.sendMessage({output: event.data = "waiting"}, (response) => {
                console.log(response);
            });
        }

    }, false);
}catch (e) {
    console.log(e);
}

Inject Script

        fileInput.addEventListener("change", function (){
            const formData = new FormData();
            formData.append("files", fileInput.files[0]);
            formData.append("courseId", courseId);
            formData.append("assignmentId", assignmentId);
            formData.append("userType", "STUDENT")

            fetch(endpoint, {
                method: "POST",
                headers: new Headers({
                    'Authorization': bearerToken
                }),
                body: formData
            }).catch(console.error).then( async response =>  {
                let fileSubmit_response = await response.json();

                console.log(fileSubmit_response);
                alert(fileSubmit_response.output);

                let output = fileSubmit_response.output;

                console.log("sending message to content")
                window.postMessage( {type: "FROM_PAGE", output});

            });
        });

Background Script

try {

    chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
        console.log(message.output);
        console.log(sender);

        sendResponse("msg recieved");

        if (message.output === 'waiting') {
            chrome.tabs.create({url: chrome.runtime.getURL('components/compilation_notification.html'), active: false}, function(tab)
            {
                // After the tab has been created, open a window to inject the tab
                chrome.windows.create({
                    tabId: tab.id,
                    type: 'popup',
                    focused: true
                    // incognito, top, left, ...
                })

                console.log("before message is sent")

                //should send data to newly created tab
                chrome.tabs.sendMessage(tab.id, {output: message.output, type: "compilation_results"}, (response) => {
                    console.log(response);
                });

            });
        }
    })
}

catch (e) {
    console.log(e)
}

Notification.js

//script that is being used in compilation_notification.html
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {

    if (msg.type === 'compilation_results') {

        sendResponse("confirmed");

        document.getElementById('update-text').value = msg.output;
    }
});


问:
Kafka-Node HighLevelProducer sends all messages to only one partition
I have a Kafka producer written in NodeJS using the Kafka-Node library.  The producer works, except for the topic the message are being sent to only gets placed into 1 partition (partition 0).  The topic has 4 partitions but no message ever goes to those other partitions.  Im using the HighLevelProducer from the Kafka-Node library.  I thought that the HighLevelProducer produces the message to go to all the available partitions in a round robin fashion.

The code is simple, but maybe im missing something to make sure it round robins instead of just dumping all messages into one partition.

```<code>const messages = data.map(JSON.stringify)
        console.log(`Sending ${data.length} Messages`)
        producer.send([{ topic, messages }], (err, data) => {
            if (err) reject(err)
            console.log(`Sent Messages`)
            resolve()
        })
</code>```

Code of what I have tried expected for messages to go to all 4 available partitions

```<code>const messages = data.map(JSON.stringify)
        console.log(`Sending ${data.length} Messages`)
        producer.send([{ topic, messages }], (err, data) => {
            if (err) reject(err)
            console.log(`Sent Messages`)
            resolve()
        })
</code>```

Body:
I have a Kafka producer written in NodeJS using the Kafka-Node library.  The producer works, except for the topic the message are being sent to only gets placed into 1 partition (partition 0).  The topic has 4 partitions but no message ever goes to those other partitions.  Im using the HighLevelProducer from the Kafka-Node library.  I thought that the HighLevelProducer produces the message to go to all the available partitions in a round robin fashion.

The code is simple, but maybe im missing something to make sure it round robins instead of just dumping all messages into one partition.

const messages = data.map(JSON.stringify)
        console.log(`Sending ${data.length} Messages`)
        producer.send([{ topic, messages }], (err, data) => {
            if (err) reject(err)
            console.log(`Sent Messages`)
            resolve()
        })

Code of what I have tried expected for messages to go to all 4 available partitions

const messages = data.map(JSON.stringify)
        console.log(`Sending ${data.length} Messages`)
        producer.send([{ topic, messages }], (err, data) => {
            if (err) reject(err)
            console.log(`Sent Messages`)
            resolve()
        })