问:
Given a list of filenames, group filenames with all derivatives based on similarity of the filename
I might not be searching the best terms to find a solution but so far nothing I've found has been able to solve my problem and I really don't know where to start or even what mechanisms to investigate.

I have a large list of image files in various locations on my hard drive and I'm trying to clean it up by removing the duplicates. Most of these are easy to find using hash codes but I have a lot of corrupted or edited versions which aren't so easy to find. I know I'll need some user interaction to identify and delete (archive) the unwanted files and I'll be doing some further processing to make sure metadata such as dates and geotagging are correct (also used to potentially match files) and then display similar images with all known data through a simple html interface.

One of the steps I've identified is grouping similarly named files or files which have part of another filename in its name. Sometimes these can be completely unrelated and so the user interaction will be required.

Below is a sample of files, what I would like is to group them into filenames which are similar, disregarding path and file extension.

```<code>[
"/Users/stu/Photos/2014/IMG_20140413_072335(2).jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335(3).jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335.jpg",
"/Users/stu/Documents/Backup/IMG_20140413_072335(4).png",
"/Users/stu/Documents/Backup/IMG_20140413_072335(5).jpg",
"/Users/stu/Documents/Backup/IMG_20140413_072335(6).jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335(7).jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335(1).jpg",
"/Users/stu/Photos/2013/IMAG0097.jpg",
"/Users/stu/Photos/2014/IMAG0097.jpg",
"/Users/stu/Photos/2013/IMAG0126.jpg",
"/Users/stu/Photos/Holidays/IMAG0132.jpg",
"/Users/stu/Photos/2014/IMG_20140322_142557-edited.jpg",
"/Users/stu/Downloads/Photos/IMG_20140330_200132.jpg",
"/Users/stu/Downloads/Photos/IMG_20140412_195105.png",
"/Users/stu/Photos/2014/IMG_20140412_195110.png",
"/Users/stu/Photos/2014/IMG_20140413_143245(6).png",
"/Users/stu/Photos/2014/IMG_20140413_143245(7).png",
"/Users/stu/Photos/2014/IMG_20140413_143245(1).jpg",
"/Users/stu/Downloads/Photos/IMG_20140413_143245(2).jpg",
"/Users/stu/Downloads/Photos/IMG_20140413_143245(11).jpg",
"/Users/stu/Photos/2014/IMG_20140413_143245(10).jpg",
"/Users/stu/Photos/2014/IMG_20140413_143245.png",
"/Users/stu/Photos/2014/IMG_20140413_143245(3).jpg",
"/Users/stu/Photos/2014/IMG_20140413_143245(8).jpg",
"/Users/stu/Photos/2014/IMG_20140413_143245(4).jpg",
"/Users/stu/Downloads/Photos/IMG_20140413_143245(5).jpg",
"/Users/stu/Downloads/Photos/IMG_20140413_143245(9).png",
"/Users/stu/Downloads/Photos/IMG_20140413_143245(3)-edited.jpg",
"/Users/stu/Photos/2014/IMG_20140413_143245(8)-edited.jpg",
"/Users/stu/Photos/Holidays/IMG_20140413_143245(4)-edited.jpg",
"/Users/stu/Photos/Holidays/IMG_20140413_143245(5)-edited.jpg",
"/Users/stu/Photos/2014/IMG_20140413_143245(9)-edited.png",
"/Users/stu/Photos/2013/IMG_20140413_072335.jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335_01.jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335_9352.jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335-9237.jpg",
"/Users/stu/Photos/2013/IMAG0126-edited.jpg",
"/Users/stu/Photos/2013/IMAG0126546.jpg"
]
</code>```

The list of files above should output something like this:

```<code>{
    "IMG_20140413_072335": [
        "/Users/stu/Photos/2014/IMG_20140413_072335(2).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335(3).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335.jpg",
        "/Users/stu/Documents/Backup/IMG_20140413_072335(4).png",
        "/Users/stu/Documents/Backup/IMG_20140413_072335(5).jpg",
        "/Users/stu/Documents/Backup/IMG_20140413_072335(6).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335(7).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335(1).jpg",
        "/Users/stu/Photos/2013/IMG_20140413_072335.jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335_01.jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335_9352.jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335-9237.jpg"
    ],
    "IMAG0097": [
        "/Users/stu/Photos/2013/IMAG0097.jpg",
        "/Users/stu/Photos/2014/IMAG0097.jpg"
    ],
    "IMAG0126": [
        "/Users/stu/Photos/2013/IMAG0126.jpg",
        "/Users/stu/Photos/2013/IMAG0126-edited.jpg",
        "/Users/stu/Photos/2013/IMAG0126546.jpg"
    ],
    "IMAG0132": [
        "/Users/stu/Photos/Holidays/IMAG0132.jpg"
    ],
    "IMG_20140322_142557": [
        "/Users/stu/Photos/2014/IMG_20140322_142557-edited.jpg"
    ],
    "IMG_20140330_200132": [
        "/Users/stu/Downloads/Photos/IMG_20140330_200132.jpg"
    ],
    "IMG_20140412_195105": [
        "/Users/stu/Downloads/Photos/IMG_20140412_195105.png"
    ],
    "IMG_20140412_195110": [
        "/Users/stu/Photos/2014/IMG_20140412_195110.png"
    ],
    "IMG_20140413_143245": [
        "/Users/stu/Photos/2014/IMG_20140413_143245(6).png",
        "/Users/stu/Photos/2014/IMG_20140413_143245(7).png",
        "/Users/stu/Photos/2014/IMG_20140413_143245(1).jpg",
        "/Users/stu/Downloads/Photos/IMG_20140413_143245(2).jpg",
        "/Users/stu/Downloads/Photos/IMG_20140413_143245(11).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_143245(10).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_143245.png",
        "/Users/stu/Photos/2014/IMG_20140413_143245(3).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_143245(8).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_143245(4).jpg",
        "/Users/stu/Downloads/Photos/IMG_20140413_143245(5).jpg",
        "/Users/stu/Downloads/Photos/IMG_20140413_143245(9).png",
        "/Users/stu/Downloads/Photos/IMG_20140413_143245(3)-edited.jpg",
        "/Users/stu/Photos/2014/IMG_20140413_143245(8)-edited.jpg",
        "/Users/stu/Photos/Holidays/IMG_20140413_143245(4)-edited.jpg",
        "/Users/stu/Photos/Holidays/IMG_20140413_143245(5)-edited.jpg",
        "/Users/stu/Photos/2014/IMG_20140413_143245(9)-edited.png"
    ]
}
</code>```

Any ideas how to do this in Python3?

Thanks

Edit: I just added a few more examples to the sample set of filenames.

答:
So I worked out a solution that gives me what I'm after. Not sure that it's the very best way to solve this but certainly does the job.

Firstly, I created a dict with full path as the key and filename minus extension as the value. This is then sorted by value length so that as I iterate through I'm able to start with shorter values and work up. Then I simply iterate through and check against all lower entities looking for a match within the string and grouping matches together. I've also allowed for small filenames by comparing lengths of values and only matching if the threshold is reached (0.5 in the example below).

```<code>import os
from pprint import pprint

files = [
    "/Users/stu/Photos/2014/IMG_20140413_072335(2).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335(3).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335.jpg",
    "/Users/stu/Documents/Backup/IMG_20140413_072335(4).png",
    "/Users/stu/Documents/Backup/IMG_20140413_072335(5).jpg",
    "/Users/stu/Documents/Backup/IMG_20140413_072335(6).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335(7).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335(1).jpg",
    "/Users/stu/Photos/2013/IMAG0097.jpg",
    "/Users/stu/Photos/2014/IMAG0097.jpg",
    "/Users/stu/Photos/2013/IMAG0126.jpg",
    "/Users/stu/Photos/Holidays/IMAG0132.jpg",
    "/Users/stu/Photos/2014/IMG_20140322_142557-edited.jpg",
    "/Users/stu/Downloads/Photos/IMG_20140330_200132.jpg",
    "/Users/stu/Downloads/Photos/IMG_20140412_195105.png",
    "/Users/stu/Photos/2014/IMG_20140412_195110.png",
    "/Users/stu/Photos/2014/IMG_20140413_143245(6).png",
    "/Users/stu/Photos/2014/IMG_20140413_143245(7).png",
    "/Users/stu/Photos/2014/IMG_20140413_143245(1).jpg",
    "/Users/stu/Downloads/Photos/IMG_20140413_143245(2).jpg",
    "/Users/stu/Downloads/Photos/IMG_20140413_143245(11).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_143245(10).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_143245.png",
    "/Users/stu/Photos/2014/IMG_20140413_143245(3).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_143245(8).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_143245(4).jpg",
    "/Users/stu/Downloads/Photos/IMG_20140413_143245(5).jpg",
    "/Users/stu/Downloads/Photos/IMG_20140413_143245(9).png",
    "/Users/stu/Downloads/Photos/IMG_20140413_143245(3)-edited.jpg",
    "/Users/stu/Photos/2014/IMG_20140413_143245(8)-edited.jpg",
    "/Users/stu/Photos/Holidays/IMG_20140413_143245(4)-edited.jpg",
    "/Users/stu/Photos/Holidays/IMG_20140413_143245(5)-edited.jpg",
    "/Users/stu/Photos/2014/IMG_20140413_143245(9)-edited.png",
    "/Users/stu/Photos/2013/IMG_20140413_072335.jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335_01.jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335_9352.jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335-9237.jpg",
    "/Users/stu/Photos/2013/IMAG0126-edited.jpg",
    "/Users/stu/Photos/20.png",
    "/Users/stu/Photos/203.png",
    "/Users/stu/Photos/2021.png",
    "/Users/stu/Photos/2021q.png",
    "/User/2.jpg"
]

relevanceFactor = 0.5

rawFiles = {}

for file in files:
    rawFiles[file] = os.path.splitext(os.path.basename(file))[0]

sortedFiles = sorted(rawFiles.items(), key=lambda kv: (len(kv[1]), kv[0]))

alreadyGrouped = []
groupedFiles = {}

for i, file in enumerate(sortedFiles):
    fullPath = file[0]
    cluster = file[1]
    if cluster not in alreadyGrouped:
        groupedFiles[cluster] = [fullPath]
        for compareFile in sortedFiles[i+1:]:
            compareFullPath = compareFile[0]
            compareCluster = compareFile[1]
            if len(cluster)/len(compareCluster) < relevanceFactor:
                break
            if (compareCluster not in alreadyGrouped
                and cluster in compareCluster):
                alreadyGrouped.append(compareCluster)
                groupedFiles[cluster].append(compareFullPath)
        if cluster not in alreadyGrouped:
            alreadyGrouped.append(cluster)

pprint(groupedFiles)
</code>```

Body:
I might not be searching the best terms to find a solution but so far nothing I've found has been able to solve my problem and I really don't know where to start or even what mechanisms to investigate.

I have a large list of image files in various locations on my hard drive and I'm trying to clean it up by removing the duplicates. Most of these are easy to find using hash codes but I have a lot of corrupted or edited versions which aren't so easy to find. I know I'll need some user interaction to identify and delete (archive) the unwanted files and I'll be doing some further processing to make sure metadata such as dates and geotagging are correct (also used to potentially match files) and then display similar images with all known data through a simple html interface.

One of the steps I've identified is grouping similarly named files or files which have part of another filename in its name. Sometimes these can be completely unrelated and so the user interaction will be required.

Below is a sample of files, what I would like is to group them into filenames which are similar, disregarding path and file extension.

[
"/Users/stu/Photos/2014/IMG_20140413_072335(2).jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335(3).jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335.jpg",
"/Users/stu/Documents/Backup/IMG_20140413_072335(4).png",
"/Users/stu/Documents/Backup/IMG_20140413_072335(5).jpg",
"/Users/stu/Documents/Backup/IMG_20140413_072335(6).jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335(7).jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335(1).jpg",
"/Users/stu/Photos/2013/IMAG0097.jpg",
"/Users/stu/Photos/2014/IMAG0097.jpg",
"/Users/stu/Photos/2013/IMAG0126.jpg",
"/Users/stu/Photos/Holidays/IMAG0132.jpg",
"/Users/stu/Photos/2014/IMG_20140322_142557-edited.jpg",
"/Users/stu/Downloads/Photos/IMG_20140330_200132.jpg",
"/Users/stu/Downloads/Photos/IMG_20140412_195105.png",
"/Users/stu/Photos/2014/IMG_20140412_195110.png",
"/Users/stu/Photos/2014/IMG_20140413_143245(6).png",
"/Users/stu/Photos/2014/IMG_20140413_143245(7).png",
"/Users/stu/Photos/2014/IMG_20140413_143245(1).jpg",
"/Users/stu/Downloads/Photos/IMG_20140413_143245(2).jpg",
"/Users/stu/Downloads/Photos/IMG_20140413_143245(11).jpg",
"/Users/stu/Photos/2014/IMG_20140413_143245(10).jpg",
"/Users/stu/Photos/2014/IMG_20140413_143245.png",
"/Users/stu/Photos/2014/IMG_20140413_143245(3).jpg",
"/Users/stu/Photos/2014/IMG_20140413_143245(8).jpg",
"/Users/stu/Photos/2014/IMG_20140413_143245(4).jpg",
"/Users/stu/Downloads/Photos/IMG_20140413_143245(5).jpg",
"/Users/stu/Downloads/Photos/IMG_20140413_143245(9).png",
"/Users/stu/Downloads/Photos/IMG_20140413_143245(3)-edited.jpg",
"/Users/stu/Photos/2014/IMG_20140413_143245(8)-edited.jpg",
"/Users/stu/Photos/Holidays/IMG_20140413_143245(4)-edited.jpg",
"/Users/stu/Photos/Holidays/IMG_20140413_143245(5)-edited.jpg",
"/Users/stu/Photos/2014/IMG_20140413_143245(9)-edited.png",
"/Users/stu/Photos/2013/IMG_20140413_072335.jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335_01.jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335_9352.jpg",
"/Users/stu/Photos/2014/IMG_20140413_072335-9237.jpg",
"/Users/stu/Photos/2013/IMAG0126-edited.jpg",
"/Users/stu/Photos/2013/IMAG0126546.jpg"
]

The list of files above should output something like this:

{
    "IMG_20140413_072335": [
        "/Users/stu/Photos/2014/IMG_20140413_072335(2).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335(3).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335.jpg",
        "/Users/stu/Documents/Backup/IMG_20140413_072335(4).png",
        "/Users/stu/Documents/Backup/IMG_20140413_072335(5).jpg",
        "/Users/stu/Documents/Backup/IMG_20140413_072335(6).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335(7).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335(1).jpg",
        "/Users/stu/Photos/2013/IMG_20140413_072335.jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335_01.jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335_9352.jpg",
        "/Users/stu/Photos/2014/IMG_20140413_072335-9237.jpg"
    ],
    "IMAG0097": [
        "/Users/stu/Photos/2013/IMAG0097.jpg",
        "/Users/stu/Photos/2014/IMAG0097.jpg"
    ],
    "IMAG0126": [
        "/Users/stu/Photos/2013/IMAG0126.jpg",
        "/Users/stu/Photos/2013/IMAG0126-edited.jpg",
        "/Users/stu/Photos/2013/IMAG0126546.jpg"
    ],
    "IMAG0132": [
        "/Users/stu/Photos/Holidays/IMAG0132.jpg"
    ],
    "IMG_20140322_142557": [
        "/Users/stu/Photos/2014/IMG_20140322_142557-edited.jpg"
    ],
    "IMG_20140330_200132": [
        "/Users/stu/Downloads/Photos/IMG_20140330_200132.jpg"
    ],
    "IMG_20140412_195105": [
        "/Users/stu/Downloads/Photos/IMG_20140412_195105.png"
    ],
    "IMG_20140412_195110": [
        "/Users/stu/Photos/2014/IMG_20140412_195110.png"
    ],
    "IMG_20140413_143245": [
        "/Users/stu/Photos/2014/IMG_20140413_143245(6).png",
        "/Users/stu/Photos/2014/IMG_20140413_143245(7).png",
        "/Users/stu/Photos/2014/IMG_20140413_143245(1).jpg",
        "/Users/stu/Downloads/Photos/IMG_20140413_143245(2).jpg",
        "/Users/stu/Downloads/Photos/IMG_20140413_143245(11).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_143245(10).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_143245.png",
        "/Users/stu/Photos/2014/IMG_20140413_143245(3).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_143245(8).jpg",
        "/Users/stu/Photos/2014/IMG_20140413_143245(4).jpg",
        "/Users/stu/Downloads/Photos/IMG_20140413_143245(5).jpg",
        "/Users/stu/Downloads/Photos/IMG_20140413_143245(9).png",
        "/Users/stu/Downloads/Photos/IMG_20140413_143245(3)-edited.jpg",
        "/Users/stu/Photos/2014/IMG_20140413_143245(8)-edited.jpg",
        "/Users/stu/Photos/Holidays/IMG_20140413_143245(4)-edited.jpg",
        "/Users/stu/Photos/Holidays/IMG_20140413_143245(5)-edited.jpg",
        "/Users/stu/Photos/2014/IMG_20140413_143245(9)-edited.png"
    ]
}

Any ideas how to do this in Python3?

Thanks

Edit: I just added a few more examples to the sample set of filenames.

Body:
So I worked out a solution that gives me what I'm after. Not sure that it's the very best way to solve this but certainly does the job.

Firstly, I created a dict with full path as the key and filename minus extension as the value. This is then sorted by value length so that as I iterate through I'm able to start with shorter values and work up. Then I simply iterate through and check against all lower entities looking for a match within the string and grouping matches together. I've also allowed for small filenames by comparing lengths of values and only matching if the threshold is reached (0.5 in the example below).

import os
from pprint import pprint

files = [
    "/Users/stu/Photos/2014/IMG_20140413_072335(2).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335(3).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335.jpg",
    "/Users/stu/Documents/Backup/IMG_20140413_072335(4).png",
    "/Users/stu/Documents/Backup/IMG_20140413_072335(5).jpg",
    "/Users/stu/Documents/Backup/IMG_20140413_072335(6).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335(7).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335(1).jpg",
    "/Users/stu/Photos/2013/IMAG0097.jpg",
    "/Users/stu/Photos/2014/IMAG0097.jpg",
    "/Users/stu/Photos/2013/IMAG0126.jpg",
    "/Users/stu/Photos/Holidays/IMAG0132.jpg",
    "/Users/stu/Photos/2014/IMG_20140322_142557-edited.jpg",
    "/Users/stu/Downloads/Photos/IMG_20140330_200132.jpg",
    "/Users/stu/Downloads/Photos/IMG_20140412_195105.png",
    "/Users/stu/Photos/2014/IMG_20140412_195110.png",
    "/Users/stu/Photos/2014/IMG_20140413_143245(6).png",
    "/Users/stu/Photos/2014/IMG_20140413_143245(7).png",
    "/Users/stu/Photos/2014/IMG_20140413_143245(1).jpg",
    "/Users/stu/Downloads/Photos/IMG_20140413_143245(2).jpg",
    "/Users/stu/Downloads/Photos/IMG_20140413_143245(11).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_143245(10).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_143245.png",
    "/Users/stu/Photos/2014/IMG_20140413_143245(3).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_143245(8).jpg",
    "/Users/stu/Photos/2014/IMG_20140413_143245(4).jpg",
    "/Users/stu/Downloads/Photos/IMG_20140413_143245(5).jpg",
    "/Users/stu/Downloads/Photos/IMG_20140413_143245(9).png",
    "/Users/stu/Downloads/Photos/IMG_20140413_143245(3)-edited.jpg",
    "/Users/stu/Photos/2014/IMG_20140413_143245(8)-edited.jpg",
    "/Users/stu/Photos/Holidays/IMG_20140413_143245(4)-edited.jpg",
    "/Users/stu/Photos/Holidays/IMG_20140413_143245(5)-edited.jpg",
    "/Users/stu/Photos/2014/IMG_20140413_143245(9)-edited.png",
    "/Users/stu/Photos/2013/IMG_20140413_072335.jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335_01.jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335_9352.jpg",
    "/Users/stu/Photos/2014/IMG_20140413_072335-9237.jpg",
    "/Users/stu/Photos/2013/IMAG0126-edited.jpg",
    "/Users/stu/Photos/20.png",
    "/Users/stu/Photos/203.png",
    "/Users/stu/Photos/2021.png",
    "/Users/stu/Photos/2021q.png",
    "/User/2.jpg"
]

relevanceFactor = 0.5

rawFiles = {}

for file in files:
    rawFiles[file] = os.path.splitext(os.path.basename(file))[0]

sortedFiles = sorted(rawFiles.items(), key=lambda kv: (len(kv[1]), kv[0]))

alreadyGrouped = []
groupedFiles = {}

for i, file in enumerate(sortedFiles):
    fullPath = file[0]
    cluster = file[1]
    if cluster not in alreadyGrouped:
        groupedFiles[cluster] = [fullPath]
        for compareFile in sortedFiles[i+1:]:
            compareFullPath = compareFile[0]
            compareCluster = compareFile[1]
            if len(cluster)/len(compareCluster) < relevanceFactor:
                break
            if (compareCluster not in alreadyGrouped
                and cluster in compareCluster):
                alreadyGrouped.append(compareCluster)
                groupedFiles[cluster].append(compareFullPath)
        if cluster not in alreadyGrouped:
            alreadyGrouped.append(cluster)

pprint(groupedFiles)


来源: stackexchange

问:
Build a data frame from the existing data frame which has the distinct id
I have a data frame like in the figure.

I have multiple id with different columns.

I want to build another dataframe that only has the distinct id and also the dataframes has all the columns for that id.

could you help me with that? Thank you so much

```<code>import pandas as pd
import numpy as np

df = pd.DataFrame()
df['id'] = [2, 3, 3,2,1]
df['c2'] = [1, 10, 6, 7, 4]
df['c3'] = [11, 23, 9, 10,23]
print(df)

dfn = pd.DataFrame()
dfn['id'] = [2, 3,1]
dfn['c2'] = [1, 10, 4]
dfn['c3'] = [11, 23, 23]
dfn['c4'] = [7, 6,  0]
dfn['c5'] = [10, 9,  0]
dfn
</code>```

The first one is that I have and the second one is that I want

答:
Here is a different approach:

```<code>df.filter(regex='c\d+', axis=1) \
    .groupby(df['id'], sort=False).apply(np.ravel).apply(pd.Series) \
    .rename(lambda x: f"c{x + 2}", axis=1).reset_index().fillna(0)
</code>```

Output:

```<code>   id    c2    c3   c4    c5
0   1   4.0  23.0  0.0   0.0
1   2   1.0  11.0  7.0  10.0
2   3  10.0  23.0  6.0   9.0
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I have a data frame like in the figure.

I have multiple id with different columns.

I want to build another dataframe that only has the distinct id and also the dataframes has all the columns for that id.

could you help me with that? Thank you so much

import pandas as pd
import numpy as np

df = pd.DataFrame()
df['id'] = [2, 3, 3,2,1]
df['c2'] = [1, 10, 6, 7, 4]
df['c3'] = [11, 23, 9, 10,23]
print(df)

dfn = pd.DataFrame()
dfn['id'] = [2, 3,1]
dfn['c2'] = [1, 10, 4]
dfn['c3'] = [11, 23, 23]
dfn['c4'] = [7, 6,  0]
dfn['c5'] = [10, 9,  0]
dfn

The first one is that I have and the second one is that I want

Body:
Here is a different approach:

df.filter(regex='c\d+', axis=1) \
    .groupby(df['id'], sort=False).apply(np.ravel).apply(pd.Series) \
    .rename(lambda x: f"c{x + 2}", axis=1).reset_index().fillna(0)

Output:

   id    c2    c3   c4    c5
0   1   4.0  23.0  0.0   0.0
1   2   1.0  11.0  7.0  10.0
2   3  10.0  23.0  6.0   9.0


问:
JQUERY Sorting an Array of Objects
I have an array that is holding a number of  objects.

Array:

```<code> var activeMembers=[];
</code>```

The DIV objects in the above array look like as follows - each was added one at a time:

```<code> <div id="mary" class="chatmember 1011"></div>
 <div id="steven" class="chatmember 1051"></div>
 <div id="adam" class="chatmember 1701"></div>
 <div id="bob" class="chatmember 1099"></div>
 <div id="peter" class="chatmember 1123"></div>
</code>```

Is there a quick way to sort theses DIV objects in the array by the ID from A-Z?

thx

答:
Try this:</p>

```<code>activeMembers.sort(function(a,b){
   var aid = $(a).attr('id');
   var bid = $(b).attr('id');

   return (aid==bid) ? 0 : (aid > bid) ? 1 : -1;

});
</code>```

ContentLicense: CC BY-SA 3.0

Title: JQUERY Sorting an Array of Objects

Body:
I have an array that is holding a number of  objects.

Array:

 var activeMembers=[];

The DIV objects in the above array look like as follows - each was added one at a time:

Is there a quick way to sort theses DIV objects in the array by the ID from A-Z?

thx

Body:
Try this:

activeMembers.sort(function(a,b){
   var aid = $(a).attr('id');
   var bid = $(b).attr('id');

   return (aid==bid) ? 0 : (aid > bid) ? 1 : -1;

});


问:
Parse SID as a parameter
I have 2 contained databases in Microsoft SQL 2019. In DB01, a user with password has been created:

```<code>USE DB01
CREATE USER [username] WITH PASSWORD = '1234'
</code>```

In DB02, a user without login/password needs to be created with the same SID.
If I run the command and fill out the SID manually, it works without any issues:

```<code>USE DB02
CREATE USER [username] WITHOUT LOGIN WITH DEFAULT_SCHEMA = [schema_1], SID = 0x010500000000000903000000AD91E331ECFC284B938E65DAF644BAF6
</code>```

However, if I try to parse the SID as a parameter, it fails with the error "Incorrect syntax near '@usersid'"

```<code>USE DB01
DECLARE @usersid varbinary(MAX) = (SELECT sid FROM sys.sysusers WHERE name = 'username')
USE DB02
CREATE USER [username] WITHOUT LOGIN WITH DEFAULT_SCHEMA = [schema_1], SID = @usersid
</code>```

Any idea why?

EDIT

Following the recommendations of @Stephan (many thanks!!!), I came with the following solution. I had to modify the query a little as the DB names actually come from variables as well. I don't know if it is the best way, but it works :)

```<code>DECLARE @DB01 nvarchar(30) = 'DB01'
DECLARE @DB02 nvarchar(30) = 'DB02'

DECLARE @usersid varbinary(85)
DECLARE @get_sid nvarchar(max) = 'SELECT @usersid = sid FROM ' + @DB01 + '.sys.database_principals WHERE [name] = ''username'''
EXEC sp_executesql @get_sid, N'@usersid varbinary(85) OUTPUT', @usersid OUTPUT

DECLARE @create_user nvarchar(max) = CONCAT(N'USE ',@DB02,N' CREATE USER [username] WITHOUT LOGIN WITH DEFAULT_SCHEMA = [dbo],SID = ',CONVERT(NVARCHAR(MAX),@usersid,1))
EXEC(@create_user)
</code>```

答:
As others mentioned, you must use dynamic SQL as CREATE USER is not design to accept input parameters, only literal strings.

Dynamic SQL to Create User with Specific SID

```<code>DECLARE @UserSID varbinary(85) = (SELECT sid FROM sys.database_principals WHERE [name] = N'UserName');

DECLARE @SQL NVARCHAR(MAX) = CONCAT(N'CREATE USER [username] WITHOUT LOGIN WITH DEFAULT_SCHEMA = dbo,SID = ',CONVERT(NVARCHAR(MAX),@UserSID,1))

EXEC(@SQL)

</code>```

Title: Parse SID as a parameter

Body:
I have 2 contained databases in Microsoft SQL 2019. In DB01, a user with password has been created:

USE DB01
CREATE USER [username] WITH PASSWORD = '1234'

In DB02, a user without login/password needs to be created with the same SID.
If I run the command and fill out the SID manually, it works without any issues:

USE DB02
CREATE USER [username] WITHOUT LOGIN WITH DEFAULT_SCHEMA = [schema_1], SID = 0x010500000000000903000000AD91E331ECFC284B938E65DAF644BAF6

However, if I try to parse the SID as a parameter, it fails with the error "Incorrect syntax near '@usersid'"

USE DB01
DECLARE @usersid varbinary(MAX) = (SELECT sid FROM sys.sysusers WHERE name = 'username')
USE DB02
CREATE USER [username] WITHOUT LOGIN WITH DEFAULT_SCHEMA = [schema_1], SID = @usersid

Any idea why?

EDIT

Following the recommendations of @Stephan (many thanks!!!), I came with the following solution. I had to modify the query a little as the DB names actually come from variables as well. I don't know if it is the best way, but it works :)

DECLARE @DB01 nvarchar(30) = 'DB01'
DECLARE @DB02 nvarchar(30) = 'DB02'

DECLARE @usersid varbinary(85)
DECLARE @get_sid nvarchar(max) = 'SELECT @usersid = sid FROM ' + @DB01 + '.sys.database_principals WHERE [name] = ''username'''
EXEC sp_executesql @get_sid, N'@usersid varbinary(85) OUTPUT', @usersid OUTPUT

DECLARE @create_user nvarchar(max) = CONCAT(N'USE ',@DB02,N' CREATE USER [username] WITHOUT LOGIN WITH DEFAULT_SCHEMA = [dbo],SID = ',CONVERT(NVARCHAR(MAX),@usersid,1))
EXEC(@create_user)

Body:
As others mentioned, you must use dynamic SQL as CREATE USER is not design to accept input parameters, only literal strings.

Dynamic SQL to Create User with Specific SID

DECLARE @UserSID varbinary(85) = (SELECT sid FROM sys.database_principals WHERE [name] = N'UserName');

DECLARE @SQL NVARCHAR(MAX) = CONCAT(N'CREATE USER [username] WITHOUT LOGIN WITH DEFAULT_SCHEMA = dbo,SID = ',CONVERT(NVARCHAR(MAX),@UserSID,1))

EXEC(@SQL)


问:
How to check merge conflicts in a gitlab repository between two commits using GitLab RestAPI?
I have a FastAPI server with two endpoints, which are two commit ids.

How can I get the excact rows, where are conflicts between these two commits?

答:
Unfortunately, there is no official GitLab REST API endpoint for this.

You should also consider that not all conflicts necessarily deal with content of files. The merge strategy can also impact whether a conflict exists or not (for example, a squash & merge may work where a regular merge commit would not) among other considerations.

When the nature of the conflict allows it to be surfaced in the GitLab UI, the information can be seen at https://gitlab.com/<namespace>/<project>/-/merge_requests/<MR_ID>/conflicts however there is no API to retrieve this information -- it is strictly available in the UI.

Gitaly has a list conflicts API (which is used by GitLab, in part, to render the conflicts UI) but this is not exposed directly through GitLab's REST APIs.

So, in principle, this is possible to do, but probably requires use of git, as the implementation in Gitaly does, to list conflicts in merge requests.

It is possible to use the Merge Requests API to detect whether a conflict exists if an MR is actually open. However it requires an MR to be opened (which won't support arbitrary refs) and it won't describe the exact nature or content of the conflict.


问:
When using run_in_executor in asyncio, is the event loop executed in the main thread?
I am experimenting with multi-thread execution using the asyncio feature.

It is my understanding that loop manages threads and executes functions on threads.

If this is the case, then it should not be possible to launch a new function while the main thread is still processing.

However, when I run the following code, it starts executing a new function while I am asleep. Could you tell me the reason?

ref: https://stackoverflow.com/a/60747799

import asyncio
from concurrent.futures.thread import ThreadPoolExecutor
from time import sleep
import logging

logging.basicConfig(
    level=logging.DEBUG, format="%(asctime)s %(thread)s %(funcName)s %(message)s"
)

def long_task(t):
    """Simulate long IO bound task."""
    logging.info("2. t: %s", t)
    sleep(t)
    logging.info("5. t: %s", t)
    return t ** 2

async def main():
    loop = asyncio.get_running_loop()
    executor = ThreadPoolExecutor(max_workers=2)
    inputs = range(1, 5)
    logging.info("1.")
    futures = [loop.run_in_executor(executor, long_task, i) for i in inputs]
    logging.info("3.")
    sleep(3)
    logging.info("4.")
    results = await asyncio.gather(*futures)
    logging.info("6.")

if __name__ == "__main__":
    asyncio.run(main())
```
<p>expected output</p>
```2022-02-08 22:59:08,896 139673219430208 __init__ Using selector: EpollSelector
2022-02-08 22:59:08,896 139673219430208 main 1.
2022-02-08 22:59:08,897 139673194632960 long_task 2. t: 1
2022-02-08 22:59:08,897 139673186240256 long_task 2. t: 2
2022-02-08 22:59:08,897 139673219430208 main 3.
2022-02-08 22:59:09,898 139673194632960 long_task 5. t: 1
2022-02-08 22:59:10,898 139673186240256 long_task 5. t: 2
2022-02-08 22:59:13,400 139673219430208 main 4.
2022-02-08 22:59:09,898 139673194632960 long_task 2. t: 3
2022-02-08 22:59:10,899 139673186240256 long_task 2. t: 4
2022-02-08 22:59:12,902 139673194632960 long_task 5. t: 3
2022-02-08 22:59:14,903 139673186240256 long_task 5. t: 4
2022-02-08 22:59:14,903 139673219430208 main 6.
```
<p>actual output</p>
```2022-02-08 22:59:08,896 139673219430208 __init__ Using selector: EpollSelector
2022-02-08 22:59:08,896 139673219430208 main 1.
2022-02-08 22:59:08,897 139673194632960 long_task 2. t: 1
2022-02-08 22:59:08,897 139673186240256 long_task 2. t: 2
2022-02-08 22:59:08,897 139673219430208 main 3.
2022-02-08 22:59:09,898 139673194632960 long_task 5. t: 1
2022-02-08 22:59:09,898 139673194632960 long_task 2. t: 3
2022-02-08 22:59:10,898 139673186240256 long_task 5. t: 2
2022-02-08 22:59:10,899 139673186240256 long_task 2. t: 4
2022-02-08 22:59:12,902 139673194632960 long_task 5. t: 3
2022-02-08 22:59:13,400 139673219430208 main 4.
2022-02-08 22:59:14,903 139673186240256 long_task 5. t: 4
2022-02-08 22:59:14,903 139673219430208 main 6.
```

Between 3 and 4, sleep(3) is being executed in the main thread. I understand that the end of longtask(1) and longtask(2) running earlier in Threadpool is printed during this time, but why is the next task running during this time? If event_loop is in the main thread, then sleep(3) should not allow the execution of the new function.

答:
When using run_in_executor in asyncio, is the event loop executed in the main thread?

Yes, it is - but run_in_executor submits the callables to an executor, allowing them to run without assistance from the event loop.

Between 3 and 4, sleep(3) is being executed in the main thread. I understand that the end of longtask(1) and longtask(2) running earlier in Threadpool is printed during this time, but why is the next task running during this time? If event_loop is in the main thread, then sleep(3) should not allow the execution of the new function.

ThreadPoolExecutor(max_workers=2) creates a thread pool that can scale to up to two workers. run_in_executor is a wrapper around Executor.submit that ensures that the final result is propagated to asyncio. Its implementation could look like this (the actual code is a bit more complex because it handles cancellation and other concerns, but this is the gist):

```<code>class EventLoop:
    # ...
    def run_in_executor(self, executor, f, *args):
        async_future = self.create_future()
        handle = executor.submit(f, *args)
        def when_done(_):
            self.call_soon_threadsafe(async_future.set_result, handle.result())
        handle.add_done_callback(when_done)
        return async_future
</code>```

The call to submit pushes the callable and its arguments into a multi-threaded queue. The pool's workers run in an infinite loop that consumes that queue, exiting only when the executor is told to shut down.

If you submit more tasks than there are workers in the pool, the additional tasks will still be placed in the queue, waiting for their turn to be processed. (The queue is an unbounded channel, so Executor.submit() never blocks.) Once a worker is done with a task, it will request the next task off the queue, which is why your extra tasks get executed. It doesn't matter that the main thread is stuck in time.sleep() at that point - the functions were submitted to the executor prior to that, and are sitting in the queue, so the workers can get to them just fine.

Finally, in normal asyncio code, an async function must never call time.sleep(), it must await asyncio.sleep() instead. (I'm aware that you did it intentionally to block the thread running the event loop, but it something that beginners are often not aware of, so it needs to be pointed out.)

Body:
I am experimenting with multi-thread execution using the asyncio feature.

It is my understanding that loop manages threads and executes functions on threads.

If this is the case, then it should not be possible to launch a new function while the main thread is still processing.

However, when I run the following code, it starts executing a new function while I am asleep. Could you tell me the reason?

ref: https://stackoverflow.com/a/60747799

import asyncio
from concurrent.futures.thread import ThreadPoolExecutor
from time import sleep
import logging

logging.basicConfig(
    level=logging.DEBUG, format="%(asctime)s %(thread)s %(funcName)s %(message)s"
)

def long_task(t):
    """Simulate long IO bound task."""
    logging.info("2. t: %s", t)
    sleep(t)
    logging.info("5. t: %s", t)
    return t ** 2

async def main():
    loop = asyncio.get_running_loop()
    executor = ThreadPoolExecutor(max_workers=2)
    inputs = range(1, 5)
    logging.info("1.")
    futures = [loop.run_in_executor(executor, long_task, i) for i in inputs]
    logging.info("3.")
    sleep(3)
    logging.info("4.")
    results = await asyncio.gather(*futures)
    logging.info("6.")

if __name__ == "__main__":
    asyncio.run(main())

expected output

2022-02-08 22:59:08,896 139673219430208 __init__ Using selector: EpollSelector
2022-02-08 22:59:08,896 139673219430208 main 1.
2022-02-08 22:59:08,897 139673194632960 long_task 2. t: 1
2022-02-08 22:59:08,897 139673186240256 long_task 2. t: 2
2022-02-08 22:59:08,897 139673219430208 main 3.
2022-02-08 22:59:09,898 139673194632960 long_task 5. t: 1
2022-02-08 22:59:10,898 139673186240256 long_task 5. t: 2
2022-02-08 22:59:13,400 139673219430208 main 4.
2022-02-08 22:59:09,898 139673194632960 long_task 2. t: 3
2022-02-08 22:59:10,899 139673186240256 long_task 2. t: 4
2022-02-08 22:59:12,902 139673194632960 long_task 5. t: 3
2022-02-08 22:59:14,903 139673186240256 long_task 5. t: 4
2022-02-08 22:59:14,903 139673219430208 main 6.

actual output

2022-02-08 22:59:08,896 139673219430208 __init__ Using selector: EpollSelector
2022-02-08 22:59:08,896 139673219430208 main 1.
2022-02-08 22:59:08,897 139673194632960 long_task 2. t: 1
2022-02-08 22:59:08,897 139673186240256 long_task 2. t: 2
2022-02-08 22:59:08,897 139673219430208 main 3.
2022-02-08 22:59:09,898 139673194632960 long_task 5. t: 1
2022-02-08 22:59:09,898 139673194632960 long_task 2. t: 3
2022-02-08 22:59:10,898 139673186240256 long_task 5. t: 2
2022-02-08 22:59:10,899 139673186240256 long_task 2. t: 4
2022-02-08 22:59:12,902 139673194632960 long_task 5. t: 3
2022-02-08 22:59:13,400 139673219430208 main 4.
2022-02-08 22:59:14,903 139673186240256 long_task 5. t: 4
2022-02-08 22:59:14,903 139673219430208 main 6.

Between 3 and 4, sleep(3) is being executed in the main thread. I understand that the end of longtask(1) and longtask(2) running earlier in Threadpool is printed during this time, but why is the next task running during this time? If event_loop is in the main thread, then sleep(3) should not allow the execution of the new function.

Body:
When using run_in_executor in asyncio, is the event loop executed in the main thread?

Yes, it is - but run_in_executor submits the callables to an executor, allowing them to run without assistance from the event loop.

Between 3 and 4, sleep(3) is being executed in the main thread. I understand that the end of longtask(1) and longtask(2) running earlier in Threadpool is printed during this time, but why is the next task running during this time? If event_loop is in the main thread, then sleep(3) should not allow the execution of the new function.

ThreadPoolExecutor(max_workers=2) creates a thread pool that can scale to up to two workers. run_in_executor is a wrapper around Executor.submit that ensures that the final result is propagated to asyncio. Its implementation could look like this (the actual code is a bit more complex because it handles cancellation and other concerns, but this is the gist):

class EventLoop:
    # ...
    def run_in_executor(self, executor, f, *args):
        async_future = self.create_future()
        handle = executor.submit(f, *args)
        def when_done(_):
            self.call_soon_threadsafe(async_future.set_result, handle.result())
        handle.add_done_callback(when_done)
        return async_future

The call to submit pushes the callable and its arguments into a multi-threaded queue. The pool's workers run in an infinite loop that consumes that queue, exiting only when the executor is told to shut down.

If you submit more tasks than there are workers in the pool, the additional tasks will still be placed in the queue, waiting for their turn to be processed. (The queue is an unbounded channel, so Executor.submit() never blocks.) Once a worker is done with a task, it will request the next task off the queue, which is why your extra tasks get executed. It doesn't matter that the main thread is stuck in time.sleep() at that point - the functions were submitted to the executor prior to that, and are sitting in the queue, so the workers can get to them just fine.

Finally, in normal asyncio code, an async function must never call time.sleep(), it must await asyncio.sleep() instead. (I'm aware that you did it intentionally to block the thread running the event loop, but it something that beginners are often not aware of, so it needs to be pointed out.)


问:
Should I be completing my subject after nexted in angular ngOnDestroy
I usually use takeUntil(this.destroySub) when I need to manually subscribe to a stream in my components. Does it matter (for the subject (garbage collection or whatnot) - since nexting is enough to complete the stream) whether I complete the subject or not in the ngOnDestroy?

@Component({...})
class MyComponent {
  private destroySub = new Subject<void>();

  constructor() {
    of('some stream').pipe(
      takeUntil(this.destroySub)
    ).subscribe()
  }

  ngOnDestroy():void {
    this.destroySub.next();
    this.destroySub.complete();
  }
}
```

答:
Short Answer.

takeUntil(this.destroySub) is how you're completing all the other observables in your component, but this doesn't complete this.destroySub itself.

Yes, subjects don't have a finite lifetime, so you should complete them when you don't need them anymore.

The Longer Answer

Just like any other object, the GC will still collect a Subject once there are no more pointers to it. The issue with this way of thinking is that it's oh so very easy to accidentally leak a reference to this subject. Because it's an asynchronous library, references can be on both the current stack and the also the event loop.

Your working inside a framework that may try to optimize your components, so there's no guarantee the reference to your component is dropped inside Angular after ngOnDestroy is called. You're also passing references to the RxJS library for every time you use takeUntil(this.destroySub). RxJS proxies and manages emissions for you in many cases, so you may to surprised by a relatively mundane line of code causing a memory leak.

Completing an observable cleans up all the internal instrumentation that RxJS keeps for any given observable and leaves your conscious clear in that regard.

So yes, even though (if you're careful and well informed) the GC may clean up an incomplete observable, I wouldn't count on it. If your observable isn't finite, then make it finite somehow. this.destroySub.complete(); does that work for you.


问:
Flutter shared_preferences module not found - iOS Xcode build
I am using shared_preferences dependency in Flutter.

Building Xcode (Product->archive - for Generic target) fails with

```<code>module 'shared_preferences' not found
</code>```

I tried flutter clean, flutter pub cache repair, tried moving Flutter lib/ into new project (flutter create .) and I building using older Xcode version (11.4.1)..currently I have 12.0 beta 3.

What is weird is that running in Emulator from my IDE (IntelliJ) works just fine with no errors.

These are my dependencies (pubspec.yaml)

```<code>dependencies:
  flutter:
    sdk: flutter
  http: ^0.12.2
  intl: any
  flutter_spinkit: ^4.1.2
  redux: any
  flutter_redux: any
  redux_thunk: any
  shared_preferences: 0.5.1+2
</code>```

答:
I had this issue aswell with flutter upgrading xcode and flutter 2.12.

to fix this:

inside ios folder, delete all folders and files, keep the ios folder in your flutter project.

within the flutter project execute > flutter create .

open the ios/Runner.xworkspace with xcode

within xcode, there will be a yellow alert icon top right, click that and fix the suggested issues.

build with xcode

Body:
I am using shared_preferences dependency in Flutter.

Building Xcode (Product->archive - for Generic target) fails with

module 'shared_preferences' not found

I tried flutter clean, flutter pub cache repair, tried moving Flutter lib/ into new project (flutter create .) and I building using older Xcode version (11.4.1)..currently I have 12.0 beta 3.

What is weird is that running in Emulator from my IDE (IntelliJ) works just fine with no errors.

These are my dependencies (pubspec.yaml)

dependencies:
  flutter:
    sdk: flutter
  http: ^0.12.2
  intl: any
  flutter_spinkit: ^4.1.2
  redux: any
  flutter_redux: any
  redux_thunk: any
  shared_preferences: 0.5.1+2


问:
User Prompt Array Question for Javascript
EDIT: Thanks to someone's response, I got a portion working. It now adds topping prices no problem. However, it adds a price even when a user doesn't put toppings into the prompt. I've updated my code below.

EDIT 2: For full code, here's my github link. It's updated with the below code (and the rest of the coding) https://github.com/cas389/m4-hw6-ju-cheryl.

I'm in a JavaScript class, and I am stuck on this portion of my homework assignment. Basically, we are creating a pizza ordering system where a user would fill out if they want thin/thick/pan crust, which toppings they want, if they want extra cheese and finally if they want delivery.

I have everything except the pizza toppings part. This is the information for this section (including the comments the teacher gave us as "hints") along with what I've been trying to make work. I've cut out all the sections I've already finished.

```<code>function getPizzaOrder() {
  var extraCheeseUpcharge = 1.5
  var thickCrustUpcharge = 2
  var deliveryFee = 3.5
  var toppingsFee = 1.5
  var basePrice = 10

  var toppings = prompt("Please enter additional toppings (comma separated)")
      // HINT: prompt() will return an empty string "" if the user presses 'OK' without entering a value
      // if the user enters toppings, use .split(",") to separate toppings into an array
      // if no toppings are given, make sure pizza.toppings is set to []
      // if the user has added toppings, add toppingsFee multiplied by
      // the number of toppings added to pizza.cost
      // YOUR CODE HERE

       pizza.toppings = [];
       pizza.toppings = toppings.split(',');
       for (let i = 0; i < pizza.toppings.length; i++){
           pizza.cost += toppingsFee;
       }
       console.log(pizza.toppings);

  return pizza
}
</code>```

答: I figured it out with some help of the comments. I needed to create an if / else statement around my for statement :) I got it to work now!</p>

Body:
EDIT: Thanks to someone's response, I got a portion working. It now adds topping prices no problem. However, it adds a price even when a user doesn't put toppings into the prompt. I've updated my code below.

EDIT 2: For full code, here's my github link. It's updated with the below code (and the rest of the coding) https://github.com/cas389/m4-hw6-ju-cheryl.

I'm in a JavaScript class, and I am stuck on this portion of my homework assignment. Basically, we are creating a pizza ordering system where a user would fill out if they want thin/thick/pan crust, which toppings they want, if they want extra cheese and finally if they want delivery.

I have everything except the pizza toppings part. This is the information for this section (including the comments the teacher gave us as "hints") along with what I've been trying to make work. I've cut out all the sections I've already finished.

function getPizzaOrder() {
  var extraCheeseUpcharge = 1.5
  var thickCrustUpcharge = 2
  var deliveryFee = 3.5
  var toppingsFee = 1.5
  var basePrice = 10

  var toppings = prompt("Please enter additional toppings (comma separated)")
      // HINT: prompt() will return an empty string "" if the user presses 'OK' without entering a value
      // if the user enters toppings, use .split(",") to separate toppings into an array
      // if no toppings are given, make sure pizza.toppings is set to []
      // if the user has added toppings, add toppingsFee multiplied by
      // the number of toppings added to pizza.cost
      // YOUR CODE HERE

       pizza.toppings = [];
       pizza.toppings = toppings.split(',');
       for (let i = 0; i < pizza.toppings.length; i++){
           pizza.cost += toppingsFee;
       }
       console.log(pizza.toppings);

  return pizza
}


问:
Port not found when mapping a virtual serial port in Windows Mobile 6 Device Emulator
I'm working on a Windows Mobile 6.5 application that utilises a barcode scanner. I'm attempting to emulate the behaviour of the scanner on the Windows Mobile 6 Device Emulator in my development environment.

I've created a pair of virtual serial ports using com0com ('COM2' and 'COMA'). I've confirmed both of these  virtual serial ports are working using HyperTerminal and Portmon. I've mapped COM2 to the first serial port ('Serial port 0') in the emulator. As I understand, this port should then be accessible as 'COM1' on the device.

Calling SerialPort.GetPortNames() on the device reports three serial ports:- 'COM1', 'COM3', 'COM5'. On attempting to open this port an IO exception is being thrown stating that the serial port does not exist.

Any suggestions as to possible causes for this problem?

As requested, the code that opens the port:-

```<code>        // Set up the serial port and open
        serialPort.PortName = "COM1";
        serialPort.BaudRate = 9600;
        serialPort.DataBits = 8;
        serialPort.Handshake = Handshake.None;
        serialPort.Parity = Parity.None;
        serialPort.StopBits = StopBits.One;
        serialPort.ReadTimeout = 250;
        serialPort.Open();
</code>```

答: This turned out to be an issue with the 6.5 device emulator images. Using a Windows Mobile 6 image instead works fine.</p>

Body:
I'm working on a Windows Mobile 6.5 application that utilises a barcode scanner. I'm attempting to emulate the behaviour of the scanner on the Windows Mobile 6 Device Emulator in my development environment.

I've created a pair of virtual serial ports using com0com ('COM2' and 'COMA'). I've confirmed both of these  virtual serial ports are working using HyperTerminal and Portmon. I've mapped COM2 to the first serial port ('Serial port 0') in the emulator. As I understand, this port should then be accessible as 'COM1' on the device.

Calling SerialPort.GetPortNames() on the device reports three serial ports:- 'COM1', 'COM3', 'COM5'. On attempting to open this port an IO exception is being thrown stating that the serial port does not exist.

Any suggestions as to possible causes for this problem?

As requested, the code that opens the port:-

        // Set up the serial port and open
        serialPort.PortName = "COM1";
        serialPort.BaudRate = 9600;
        serialPort.DataBits = 8;
        serialPort.Handshake = Handshake.None;
        serialPort.Parity = Parity.None;
        serialPort.StopBits = StopBits.One;
        serialPort.ReadTimeout = 250;
        serialPort.Open();


问:
find command in bash vs find command in cli
I need to accommodate spaces in filepaths. Why does "find" not work from the script, but works from the cli?

```<code>MyLaptop$ ./my-bash-script.sh

find: '/Sandbox/test folder/testfiles-good/ResidentFile_1.pcapng': No such file or directory

MyLaptop$ find '/Sandbox/test folder/testfiles-good/ResidentFile_1.pcapng'

/Sandbox/test folder/testfiles-good/ResidentFile_1.pcapng

Using echo find -f "'$line'"

output: find -f '/Sandbox/test folder/testfiles-good/ResidentFile_1.pcapng'
</code>```

But in this case: FOUND="$(find -f "'$line'")"
it does not

答:
In this case ...

```<code>FOUND="$(find -f "'$line'")"
</code>```

... you are asking find for a file whose name contains leading and trailing ' characters.  That is unlikely to be what you intended.  Though it may look strange, this is probably what you meant:

```<code>FOUND="$(find -f "$line")"
</code>```

The " characters inside the command substitution do not pair with those outside (and if they did then the original command substitution would be wrong a different way).  On the other hand, word splitting is not performed on the result of the command substitution when it is used on the right-hand side of a variable assignment, so you could also just use

```<code>FOUND=$(find -f "$line")
</code>```

Body:
I need to accommodate spaces in filepaths. Why does "find" not work from the script, but works from the cli?

MyLaptop$ ./my-bash-script.sh

find: '/Sandbox/test folder/testfiles-good/ResidentFile_1.pcapng': No such file or directory

MyLaptop$ find '/Sandbox/test folder/testfiles-good/ResidentFile_1.pcapng'

/Sandbox/test folder/testfiles-good/ResidentFile_1.pcapng

Using echo find -f "'$line'"

output: find -f '/Sandbox/test folder/testfiles-good/ResidentFile_1.pcapng'

But in this case: FOUND="$(find -f "'$line'")"
it does not

Body:
In this case ...

FOUND="$(find -f "'$line'")"

... you are asking find for a file whose name contains leading and trailing ' characters.  That is unlikely to be what you intended.  Though it may look strange, this is probably what you meant:

FOUND="$(find -f "$line")"

The " characters inside the command substitution do not pair with those outside (and if they did then the original command substitution would be wrong a different way).  On the other hand, word splitting is not performed on the result of the command substitution when it is used on the right-hand side of a variable assignment, so you could also just use

FOUND=$(find -f "$line")


问:
Creating a reactive password confirmation form control?
I'm trying to create a material password confirmation component that can be used with Angular Reactive Forms.  This way the same component can be used in a Registration Form and a Reset Password Form.

I've created a self contained Stackblitz Demo here that works fine (This is when the control is used standalone without being embedded in another reactive form).  The password field has min and max length validation constraints on it and the confirm password field is required.

The submit button will not enable until all the validation constraints are satisfied and the passwords match.

So now I'd like to be able to use the component in Angular Material Reactive Forms, and according to this article the control must implement the ControlValueAccessor interface and the Validator interface.

So I've added these to the control and I've implemented a demo here.

The form looks like this:

```<code><mat-card class="PasswordFormTestCard">
  <form class="RegistrationForm" [formGroup]="form" (ngSubmit)="submit()">
    <mat-form-field>
      <input
        matInput
        placeholder="username"
        type="text"
        formControlName="username"
      />
      <mat-hint *ngIf="!username">Example Monica</mat-hint>
      <mat-error>Please enter your username</mat-error>
    </mat-form-field>

    <fs-password-form formControlName="password"></fs-password-form>

    <button mat-button type="submit" [disabled]="!form.valid">Submit</button>
  </form>
</mat-card>

</code>```

As can be seen the custom password form control is bound like this:

```<code><fs-password-form formControlName="password"></fs-password-form>
</code>```

And this "Almost" works.

The submit button enables as soon as the confirmPassword dialogs has any characters in it.

The validate function on the control should prevent this from happening.  I've implemented it like this:

```<code>  //=============================================
  // Validator API Methods
  //=============================================
  validate(control: AbstractControl): ValidationErrors | null {
    if (
      this.passwordForm.valid &&
      this.confirmPasswordControl.valid &&
      this.passwordControl.valid
    ) {
      console.log('BOTH THE FORM AND THE CONTROLS ARE VALID');
      return null;
    }

    let errors: any = {};

    errors = this.addControlErrors(errors, 'password');
    errors = this.addControlErrors(errors, 'confirmPassword');

    return errors;
  }

  addControlErrors(allErrors: any, controlName: string) {
    const errors = { ...allErrors };
    const controlErrors = this.passwordForm.controls[controlName].errors;

    if (controlErrors) {
      errors[controlName] = controlErrors;
    }
    return errors;
  }
</code>```

And I thought checking that the the form and the controls are valid before returning null like this:

```<code>     this.passwordForm.valid &&
      this.confirmPasswordControl.valid &&
      this.passwordControl.valid
</code>```

Would ensure that the custom PasswordForm control does not indicate that it is valid before it actually is, but it's returning null early for some reason.

Anyone have any ideas on why this is?

答:
You have a timing problem here. When your validate function is called the validity of the FormGroup hasn´t been updated yet. The fix is quite easy. Add the following lines at the top of your validate function:

```<code>this.passwordForm.updateValueAndValidity({
  onlySelf: true,
  emitEvent: false,
});
</code>```

I don´t think it will have side-effects.

Body:
I'm trying to create a material password confirmation component that can be used with Angular Reactive Forms.  This way the same component can be used in a Registration Form and a Reset Password Form.

I've created a self contained Stackblitz Demo here that works fine (This is when the control is used standalone without being embedded in another reactive form).  The password field has min and max length validation constraints on it and the confirm password field is required.

The submit button will not enable until all the validation constraints are satisfied and the passwords match.

So now I'd like to be able to use the component in Angular Material Reactive Forms, and according to this article the control must implement the ControlValueAccessor interface and the Validator interface.

So I've added these to the control and I've implemented a demo here.

The form looks like this:

<mat-card class="PasswordFormTestCard">
  <form class="RegistrationForm" [formGroup]="form" (ngSubmit)="submit()">
    <mat-form-field>
      <input
        matInput
        placeholder="username"
        type="text"
        formControlName="username"
      />
      <mat-hint *ngIf="!username">Example Monica</mat-hint>
      <mat-error>Please enter your username</mat-error>
    </mat-form-field>

    <fs-password-form formControlName="password"></fs-password-form>

    <button mat-button type="submit" [disabled]="!form.valid">Submit</button>
  </form>
</mat-card>

As can be seen the custom password form control is bound like this:

<fs-password-form formControlName="password"></fs-password-form>

And this "Almost" works.

The submit button enables as soon as the confirmPassword dialogs has any characters in it.

The validate function on the control should prevent this from happening.  I've implemented it like this:

  //=============================================
  // Validator API Methods
  //=============================================
  validate(control: AbstractControl): ValidationErrors | null {
    if (
      this.passwordForm.valid &&
      this.confirmPasswordControl.valid &&
      this.passwordControl.valid
    ) {
      console.log('BOTH THE FORM AND THE CONTROLS ARE VALID');
      return null;
    }

    let errors: any = {};

    errors = this.addControlErrors(errors, 'password');
    errors = this.addControlErrors(errors, 'confirmPassword');

    return errors;
  }

  addControlErrors(allErrors: any, controlName: string) {
    const errors = { ...allErrors };
    const controlErrors = this.passwordForm.controls[controlName].errors;

    if (controlErrors) {
      errors[controlName] = controlErrors;
    }
    return errors;
  }

And I thought checking that the the form and the controls are valid before returning null like this:

     this.passwordForm.valid &&
      this.confirmPasswordControl.valid &&
      this.passwordControl.valid

Would ensure that the custom PasswordForm control does not indicate that it is valid before it actually is, but it's returning null early for some reason.

Anyone have any ideas on why this is?

Body:
You have a timing problem here. When your validate function is called the validity of the FormGroup hasn´t been updated yet. The fix is quite easy. Add the following lines at the top of your validate function:

this.passwordForm.updateValueAndValidity({
  onlySelf: true,
  emitEvent: false,
});

I don´t think it will have side-effects.


问:
Replace external jar dependency with local intellij project
So I have an intellij IDEA project (project A) which includes a bunch of external jar libraries. I am currently working on one of those external jar libraries (project B) and I want to replace it with the local project (project B) on my computer.

So, in short:
I have Project A which depends on jar B
I want to replace jar B with my local project (project B)

That way, when I run project A, it uses Project B local rather than Jar B external. Anyone know any good easy ways of doing this?

答:
Using Composite Builds

Your use case is exactly what composite builds in Gradle are made for. The docs even mention your precise use case:

Composite builds allow you to […] combine builds that are usually developed independently, for instance when trying out a bug fix in a library that your application uses

A composite build allows you to temporarily and easily replace an external dependency library with a locally available build project of that library. (It would even work with multiple different dependencies.)

Complete Example

Here’s how you’d set this up for your two projects (leaving out Gradle Wrapper files and Java source code for conciseness):

├── projectA
│   ├── build.gradle
│   └── settings.gradle
└── projectB
    ├── build.gradle
    └── settings.gradle
```
<p>Note that the two project directories don’t actually have to be siblings. I’ve only done that here to keep it simple. In my minimal sample build, the two <code>settings.gradle</code> files can be empty. The two build scripts look as follows.</p>
<h3><code>projectA/build.gradle</code></h3>
<pre class="lang-groovy prettyprint-override"><code>plugins {
    id 'java'
}

dependencies {
    implementation 'com.example:projectB:1.2.+'
}

repositories {
    // where you get the projectB dependency from normally
}
</code>```

projectB/build.gradle

plugins {
    id 'java-library'
}

group = 'com.example'
version = '1.2.3'
```

Running the Sample

On the Command Line

Under projectA/, simply run ./gradlew --include-build ../projectB build (or any other Gradle task(s) you’re interested in). Make sure to use the right path to the directory of projectB. The --include-build option automatically creates the composite build on-the-fly.

In IntelliJ IDEA

You can also create a composite build in IntelliJ. To do that:

Import projectA as usual (via its build.gradle file to make sure that IntelliJ automatically uses the Gradle configuration).

In the Gradle tool window, you click the + button (“Link Gradle Project”) and select the build.gradle file of projectB.

Right click projectA in the Gradle tool window and select “Composite Build Configuration”. In the opening dialog, select projectB and click “OK”.

In the Gradle tool window, click the  button (“Reload All Gradle Projects”).

That’s it. You should now be able to use projectA with its locally available dependency projectB.

Body:
Using Composite Builds

Your use case is exactly what composite builds in Gradle are made for. The docs even mention your precise use case:

Composite builds allow you to […] combine builds that are usually developed independently, for instance when trying out a bug fix in a library that your application uses

A composite build allows you to temporarily and easily replace an external dependency library with a locally available build project of that library. (It would even work with multiple different dependencies.)

Complete Example

Here’s how you’d set this up for your two projects (leaving out Gradle Wrapper files and Java source code for conciseness):

├── projectA
│   ├── build.gradle
│   └── settings.gradle
└── projectB
    ├── build.gradle
    └── settings.gradle

Note that the two project directories don’t actually have to be siblings. I’ve only done that here to keep it simple. In my minimal sample build, the two settings.gradle files can be empty. The two build scripts look as follows.

projectA/build.gradle

plugins {
    id 'java'
}

dependencies {
    implementation 'com.example:projectB:1.2.+'
}

repositories {
    // where you get the projectB dependency from normally
}

projectB/build.gradle

plugins {
    id 'java-library'
}

group = 'com.example'
version = '1.2.3'

Running the Sample

On the Command Line

Under projectA/, simply run ./gradlew --include-build ../projectB build (or any other Gradle task(s) you’re interested in). Make sure to use the right path to the directory of projectB. The --include-build option automatically creates the composite build on-the-fly.

In IntelliJ IDEA

You can also create a composite build in IntelliJ. To do that:

Import projectA as usual (via its build.gradle file to make sure that IntelliJ automatically uses the Gradle configuration).

In the Gradle tool window, you click the + button (“Link Gradle Project”) and select the build.gradle file of projectB.

Right click projectA in the Gradle tool window and select “Composite Build Configuration”. In the opening dialog, select projectB and click “OK”.

In the Gradle tool window, click the  button (“Reload All Gradle Projects”).

That’s it. You should now be able to use projectA with its locally available dependency projectB.


问:
mapquest not recognized in angular
I am trying to use mapquest for adding a traffic layer in angular . I believe i have included all the scripts needed to run them in index.html

Feel free to share some other way to add traffic using leaflet if you know .

```<code> var trafficlayer = L.map('map', {
      layers: L.mapquest.mapLayer(),
      center: [ 40.731701, -73.993411 ],
      zoom: 12
    });
</code>```

The error is
Property 'mapquest' does not exist on type 'typeof import("/Users/anandcr/Detect/one_map/node_modules/@types/leaflet/index")'.ts(2339)

index.html

added these scripts in index.html and trying to use them in an angular component

```<code><script src="https://www.mapquestapi.com/sdk/leaflet/v2.2/mq-map.js?key=yeblECUdfo04G8hKloADL2DXqP2h0Z2U"></script>
  <script src="https://www.mapquestapi.com/sdk/leaflet/v2.2/mq-traffic.js?key=yeblECUdfo04G8hKloADL2DXqP2h0Z2U"></script>
  <script src="https://api.mqcdn.com/sdk/mapquest-js/v1.3.2/mapquest.js"></script>
  <link type="text/css" rel="stylesheet" href="https://api.mqcdn.com/sdk/mapquest-js/v1.3.2/mapquest.css"/>
</code>```

答: While I can't help with the Angular question, I highly recommend only using the latest version of the MapQuest SDK - MapQuest.js. It looks like you're including both the MapQuest.js and the MapQuest Leaflet Plugin in the script tags. I also recommend removing the key.</p>

Title: mapquest not recognized in angular

Body:
I am trying to use mapquest for adding a traffic layer in angular . I believe i have included all the scripts needed to run them in index.html

Feel free to share some other way to add traffic using leaflet if you know .

 var trafficlayer = L.map('map', {
      layers: L.mapquest.mapLayer(),
      center: [ 40.731701, -73.993411 ],
      zoom: 12
    });

The error is
Property 'mapquest' does not exist on type 'typeof import("/Users/anandcr/Detect/one_map/node_modules/@types/leaflet/index")'.ts(2339)

index.html

added these scripts in index.html and trying to use them in an angular component

  <link type="text/css" rel="stylesheet" href="https://api.mqcdn.com/sdk/mapquest-js/v1.3.2/mapquest.css"/>


问:
Popup modal does not close when Ajax throws and error
I am submitting a form using Ajax and then notify the user.
I have three different pop-up modals for wait, success and error respectively.
When the submit button is clicked, the wait modal pops while the form gets submitted. Thereafter either the success or error modal pops up. Issue is that on HTTP error the wait modal fails to close and as a result overlays the error modal. How can I solve this?

```<code><!-- Success Modal-->
        <div class="modal fade" id="notify" tabindex="-1" role="dialog" aria-labelledby="ModalCenterTitle" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="ModalTitle">Success</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">×</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        @{
                            string action = ViewContext.RouteData.Values["action"].ToString().ToLower();
                            if (action == "Create")
                            {
                                action = "created";
                            }
                            else
                            {
                                action = "updated";
                            }
                        }
                        Record has been successfully @action.

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="Close" data-dismiss="modal" onclick="closeModals()">Close</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- Error Modal-->
        <div class="modal fade" id="error" tabindex="-1" role="dialog" aria-labelledby="ModalCenterTitle" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                    <div class="modal-header" id="error-header">
                        <h5 class="modal-title" id="ModalTitle">Error</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">×</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        Failed to @ViewContext.RouteData.Values["action"].ToString().ToLower() record.
                    </div>
                    <div class="modal-footer">
                        <button type="button" id="modalClose" data-dismiss="modal" class="CloseError" onclick="closeModals()">Close</button>
                    </div>
                </div>
            </div>
        </div>

        <!--Please Wait Modal-->
        <div class="modal fade" id="wait" role="dialog" aria-labelledby="ModalCenterTitle" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered" role="document" style="background-color: transparent; text-align: center; border-style: none;">
                <div class="modal-content" style="background-color: transparent; text-align: center;border-style:none;">
                    <div class="modal-body" style="background-color: transparent; text-align: center;border-style:none;">
                        <img id="waitImage" src="~/images/orderit_logo.png" height="100" width="100" />
                        <div style="color: #f9a51a;">
                            please wait...
                        </div>

                    </div>
                </div>
            </div>
        </div>

       <script>
            $("#Save").on("click", function (event) {
                $('#wait').modal('toggle');
                var formData = new FormData(document.getElementById("form"));
                $.ajax({
                    url: window.location.pathname,
                    type: 'POST',
                    data: formData,
                    success: function (data) {
                        $('#wait').modal('hide');
                        $('#notify').modal('toggle');

                    },
                    error: function (data) {
                        $('#wait').modal('hide');
                        $('#error').modal('toggle');
                    },
                    failure: function (data) {
                        $('#wait').modal('hide');
                        $('#error').modal('toggle');

                    },
                    cache: false,
                    contentType: false,
                    processData: false
                });
                return false;
            })

        });

    </script>
</code>```

答:
I resolved this by using a normal div that fills entire page and set its opacity to 80%.

```<code>  <div id="wait">
        <img id="waitImage" src="~/images/orderit_logo.png" height="100" />
        <div style="color: #f9a51a; top: 65%; position: absolute; text-align:center;width:100%;">
            please wait...
        </div>
    </div>

#wait {
    position: absolute;
    top: 0px;
    bottom: 0px;
    width: 100%;
    background-color: rgba(19, 33, 106, 0.8); /*#f9a51a*/
    text-align: center;
    border-style: none;
    z-index: 3000;
    display:none;
}
</code>```

then call:

```<code>  document.getElementById('wait').style.display = 'block';
</code>```

or

```<code>  document.getElementById('wait').style.display = 'none';
</code>```

Body:
I am submitting a form using Ajax and then notify the user.
I have three different pop-up modals for wait, success and error respectively.
When the submit button is clicked, the wait modal pops while the form gets submitted. Thereafter either the success or error modal pops up. Issue is that on HTTP error the wait modal fails to close and as a result overlays the error modal. How can I solve this?

Success

                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            ×
                        </button>

                        @{
                            string action = ViewContext.RouteData.Values["action"].ToString().ToLower();
                            if (action == "Create")
                            {
                                action = "created";
                            }
                            else
                            {
                                action = "updated";
                            }
                        }
                        Record has been successfully @action.

                        <button type="button" class="Close" data-dismiss="modal" onclick="closeModals()">Close</button>

Error

                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            ×
                        </button>

                        Failed to @ViewContext.RouteData.Values["action"].ToString().ToLower() record.

                        <button type="button" id="modalClose" data-dismiss="modal" class="CloseError" onclick="closeModals()">Close</button>

                            please wait...

Body:
I resolved this by using a normal div that fills entire page and set its opacity to 80%.

  <div id="wait">
        <img id="waitImage" src="~/images/orderit_logo.png" height="100" />
        <div style="color: #f9a51a; top: 65%; position: absolute; text-align:center;width:100%;">
            please wait...
        </div>
    </div>

#wait {
    position: absolute;
    top: 0px;
    bottom: 0px;
    width: 100%;
    background-color: rgba(19, 33, 106, 0.8); /*#f9a51a*/
    text-align: center;
    border-style: none;
    z-index: 3000;
    display:none;
}

then call:

  document.getElementById('wait').style.display = 'block';

or

  document.getElementById('wait').style.display = 'none';


问:
load event not firing in jsonstore
team,

I'm having a jsonstore to load my list. I'm loading store using inline data without any proxy config. I wanted to call load event of my store. Kindly let me know how to do it.

Below is my store code:

Store = new Ext.data.JsonStore({
    model  : 'SettingModel',

```<code>getGroupString:function(){
        return 'Set Duration';
        },
autoLoad:false,
data: [
    {name: '5 mins',   value: '5'},
    {name: '15 mins',   value: '15'},
    {name: '25 mins',   value: '25'},
    {name: '45 mins',   value: '45'}
],
listeners : {
    'load': function(){
        Ext.Msg.alert('App',"load");
    }
}});
</code>```

Store.load();

with the above code i get the following error message : "Uncaught Error: You are using a ServerProxy but have not supplied it with a url"

if i remove the "Store.load();" line, im not getting error and list is populating with data. But the load event is not fired.

Kindly help me how to make load event to fire in this scenario.

Thanks in Advance

答:
Ext seems to be pushing for stores to be used with models etc.
It feels like, because you are using a store, it expects you to do an ajax call to get the data.
You can overwrite this by specifying a proxy:

```<code>        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'items'
            }
        },
</code>```

Straight from the Ext docs for store: http://dev.sencha.com/deploy/touch/docs/

```<code>Ext.regModel('User', {
fields: [
    {name: 'firstName', type: 'string'},
    {name: 'lastName',  type: 'string'},
    {name: 'age',       type: 'int'},
    {name: 'eyeColor',  type: 'string'}
]
});
</code>```

oh look, they don't have a memory proxy in their local store!

```<code>new Ext.data.Store({
model: 'User',
data : [
    {firstName: 'Ed',    lastName: 'Spencer'},
    {firstName: 'Tommy', lastName: 'Maintz'},
    {firstName: 'Aaron', lastName: 'Conran'},
    {firstName: 'Jamie', lastName: 'Avins'}
]
});
</code>```

And as a fun bonus, JsonStore isn't even in the touch docs.

http://jsfiddle.net/6WV3m/3/ is extjs 4.0 code, but it works on my touch locally.

Title: load event not firing in jsonstore

Body:
team,

I'm having a jsonstore to load my list. I'm loading store using inline data without any proxy config. I wanted to call load event of my store. Kindly let me know how to do it.

Below is my store code:

Store = new Ext.data.JsonStore({
    model  : 'SettingModel',

getGroupString:function(){
        return 'Set Duration';
        },
autoLoad:false,
data: [
    {name: '5 mins',   value: '5'},
    {name: '15 mins',   value: '15'},
    {name: '25 mins',   value: '25'},
    {name: '45 mins',   value: '45'}
],
listeners : {
    'load': function(){
        Ext.Msg.alert('App',"load");
    }
}});

Store.load();

with the above code i get the following error message : "Uncaught Error: You are using a ServerProxy but have not supplied it with a url"

if i remove the "Store.load();" line, im not getting error and list is populating with data. But the load event is not fired.

Kindly help me how to make load event to fire in this scenario.

Thanks in Advance

Body:
Ext seems to be pushing for stores to be used with models etc.
It feels like, because you are using a store, it expects you to do an ajax call to get the data.
You can overwrite this by specifying a proxy:

        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                root: 'items'
            }
        },

Straight from the Ext docs for store: http://dev.sencha.com/deploy/touch/docs/

Ext.regModel('User', {
fields: [
    {name: 'firstName', type: 'string'},
    {name: 'lastName',  type: 'string'},
    {name: 'age',       type: 'int'},
    {name: 'eyeColor',  type: 'string'}
]
});

oh look, they don't have a memory proxy in their local store!

new Ext.data.Store({
model: 'User',
data : [
    {firstName: 'Ed',    lastName: 'Spencer'},
    {firstName: 'Tommy', lastName: 'Maintz'},
    {firstName: 'Aaron', lastName: 'Conran'},
    {firstName: 'Jamie', lastName: 'Avins'}
]
});

And as a fun bonus, JsonStore isn't even in the touch docs.

http://jsfiddle.net/6WV3m/3/ is extjs 4.0 code, but it works on my touch locally.


问:
Perlin noise in python
I have searched everywhere for an answer for this question but no luck. I want to figure out how to take the image generated by python's noise library and export it. does anybody know how?</p>

答:
The GitHub repository for the noise library has an examples folder. One of them is an example of how to generate 2D perlin noise and write it to a file.

If you want more standard file formats, such as a PNG or TIFF, you can use Numpy to create an array and write the perlin noise values to the array, and then save the array as an image file using OpenCV.

Title: Perlin noise in python


问:
Is there a way to have Browsersynch on gulp watch my WP php files?
So I downloaded this Worpress template with gulp and browsersynch which watches all .js files and .css files. The wierd thing is that whenever I want to use gulp and BroswerSynch I have to type npm run watch So what I am trying to add on my gulp file, is to be able to watch for changes on my php files from my template (/wp-content/theme/MYTEMPLATE) so whenever I am doing modifications I am able to see them in real time. Is it possible to do this?

Here is my gulpfile.js code

```<code>   const gulp = require( 'gulp' ),
    fancylog = require( 'fancy-log' ),
    browserSync = require( 'browser-sync' ),
    server = browserSync.create(),
    dev_url = 'http://localhost:10013/';

/**
 * Define all source paths
 */

var paths = {
    styles: {
        src: './assets/*.scss',
        dest: './assets/css'
    },
    scripts: {
        src: './assets/*.js',
        dest: './assets/js'
    }
};

/**
 * Webpack compilation: http://webpack.js.org, https://github.com/shama/webpack-stream#usage-with-gulp-watch
 *
 * build_js()
 */

function build_js() {
    const compiler = require( 'webpack' ),
        webpackStream = require( 'webpack-stream' );

    return gulp.src( paths.scripts.src )
        .pipe(
            webpackStream( {
                config: require( './webpack.config.js' )
            },
                compiler
            )
        )
        .pipe(
            gulp.dest( paths.scripts.dest )
        )
        .pipe(
            server.stream() // Browser Reload
        );
}

/**
 * SASS-CSS compilation: https://www.npmjs.com/package/gulp-sass
 *
 * build_css()
 */

function build_css() {
    const sass = require( 'gulp-sass' )( require( 'sass' ) ),
        postcss = require( 'gulp-postcss' ),
        sourcemaps = require( 'gulp-sourcemaps' ),
        autoprefixer = require( 'autoprefixer' ),
        cssnano = require( 'cssnano' );

    const plugins = [
        autoprefixer(),
        cssnano(),
    ];

    return gulp.src( paths.styles.src )
        .pipe(
            sourcemaps.init()
        )
        .pipe(
            sass()
                .on( 'error', sass.logError )
        )
        .pipe(
            postcss( plugins )
        )
        .pipe(
            sourcemaps.write( './' )
        )
        .pipe(
            gulp.dest( paths.styles.dest )
        )
        .pipe(
            server.stream() // Browser Reload
        );
}

/**
 * Watch task: Webpack + SASS
 *
 * $ gulp watch
 */

gulp.task( 'watch',
    function () {
        // Modify "dev_url" constant and uncomment "server.init()" to use browser sync
        server.init({
            proxy: dev_url,
        } );

        gulp.watch( paths.scripts.src, build_js );
        gulp.watch( [ paths.styles.src, './assets/scss/*.scss' ], build_css );
    }
);
</code>```

答:
You will need to tweak this to fit your build but I setup something similar to make it a bit more portable.

Set the cfg as a variable and create a gulpconfig.json (could call it whatever you want.)

gulp.js

```<code>cfg = require('./gulpconfig.json'),

gulp.task('browser-sync', () => {

    browserSync.init(cfg.browserSyncWatchFiles, cfg.browserSyncOptions);

});
</code>```

gulpconfig.json

```<code>{
    "browserSyncOptions": {
        "proxy": "example.com",
        "notify": true,
        "browser": [
            "google chrome"
        ]
    },
    "browserSyncWatchFiles": [
        "./**/*.php",
        "./images/*.{png,jpg,gif}"
    ]
}
</code>```

Body:
So I downloaded this Worpress template with gulp and browsersynch which watches all .js files and .css files. The wierd thing is that whenever I want to use gulp and BroswerSynch I have to type npm run watch So what I am trying to add on my gulp file, is to be able to watch for changes on my php files from my template (/wp-content/theme/MYTEMPLATE) so whenever I am doing modifications I am able to see them in real time. Is it possible to do this?

Here is my gulpfile.js code

   const gulp = require( 'gulp' ),
    fancylog = require( 'fancy-log' ),
    browserSync = require( 'browser-sync' ),
    server = browserSync.create(),
    dev_url = 'http://localhost:10013/';

/**
 * Define all source paths
 */

var paths = {
    styles: {
        src: './assets/*.scss',
        dest: './assets/css'
    },
    scripts: {
        src: './assets/*.js',
        dest: './assets/js'
    }
};

/**
 * Webpack compilation: http://webpack.js.org, https://github.com/shama/webpack-stream#usage-with-gulp-watch
 *
 * build_js()
 */

function build_js() {
    const compiler = require( 'webpack' ),
        webpackStream = require( 'webpack-stream' );

    return gulp.src( paths.scripts.src )
        .pipe(
            webpackStream( {
                config: require( './webpack.config.js' )
            },
                compiler
            )
        )
        .pipe(
            gulp.dest( paths.scripts.dest )
        )
        .pipe(
            server.stream() // Browser Reload
        );
}

/**
 * SASS-CSS compilation: https://www.npmjs.com/package/gulp-sass
 *
 * build_css()
 */

function build_css() {
    const sass = require( 'gulp-sass' )( require( 'sass' ) ),
        postcss = require( 'gulp-postcss' ),
        sourcemaps = require( 'gulp-sourcemaps' ),
        autoprefixer = require( 'autoprefixer' ),
        cssnano = require( 'cssnano' );

    const plugins = [
        autoprefixer(),
        cssnano(),
    ];

    return gulp.src( paths.styles.src )
        .pipe(
            sourcemaps.init()
        )
        .pipe(
            sass()
                .on( 'error', sass.logError )
        )
        .pipe(
            postcss( plugins )
        )
        .pipe(
            sourcemaps.write( './' )
        )
        .pipe(
            gulp.dest( paths.styles.dest )
        )
        .pipe(
            server.stream() // Browser Reload
        );
}

/**
 * Watch task: Webpack + SASS
 *
 * $ gulp watch
 */

gulp.task( 'watch',
    function () {
        // Modify "dev_url" constant and uncomment "server.init()" to use browser sync
        server.init({
            proxy: dev_url,
        } );

        gulp.watch( paths.scripts.src, build_js );
        gulp.watch( [ paths.styles.src, './assets/scss/*.scss' ], build_css );
    }
);

Body:
You will need to tweak this to fit your build but I setup something similar to make it a bit more portable.

Set the cfg as a variable and create a gulpconfig.json (could call it whatever you want.)

gulp.js

cfg = require('./gulpconfig.json'),

gulp.task('browser-sync', () => {

    browserSync.init(cfg.browserSyncWatchFiles, cfg.browserSyncOptions);

});

gulpconfig.json

{
    "browserSyncOptions": {
        "proxy": "example.com",
        "notify": true,
        "browser": [
            "google chrome"
        ]
    },
    "browserSyncWatchFiles": [
        "./**/*.php",
        "./images/*.{png,jpg,gif}"
    ]
}


问:
When running matplotlib.pyplot.savefig in a subprocess, what can cause a deadlock or a freeze?
In order to speed things up in a scientific computing software written in Python, I want to offload the construction and saving of intermediate figures to subprocesses using a multiprocessing.Pool().

The implementation is straightforward.

Most of the time it works fine but sometimes the subprocesses go into a deadlock and my program freezes.

I am running python on a UNIX machine.

答:
I spent a bunch of days not knowing what to google or what the issue could be and finally found that blog post : forking vs spawn.

This QA is really here to have it better referenced and easier to find.

It boils down to the fact that matplotlib is not thread safe, and forking isn't either. So when running matplotlib methods in a subprocess, please make sure this subprocess is created using the spawn context.


问:
Nested ternary operators
I have this code:

```<code>_viewModel.PhoneDefault = user == null ? "" :
    (string.IsNullOrEmpty(user.PhoneDay) ?
        (string.IsNullOrEmpty(user.PhoneEvening) ?
            (string.IsNullOrEmpty(user.Mobile) ? "" :
                user.Mobile) :
            user.PhoneEvening) :
         user.PhoneDay);
</code>```

Is there a better way to write this to make it more readable?

答:
Adding some more parenthesis might make it more readable.  You could also break it down into a series of if/else statements</p>

```<code>if (user != null)
{
  if (!string.IsNullOrEmpty(user.PhoneDay))
    _viewModel.PhoneDefault = user.PhoneDay;
  else if (!string.IsNullOrEmpty(user.PhoneEvening))
    _viewModel.PhoneDefault = user.PhoneEvening;
  else if (!string.IsNullOrEmpty(user.Mobile))
    _viewModel.PhoneDefault = user.Mobile;
  else
    _viewModel.PhoneDefault = "";
}
</code>```

Title: Nested ternary operators

Body:
I have this code:

_viewModel.PhoneDefault = user == null ? "" :
    (string.IsNullOrEmpty(user.PhoneDay) ?
        (string.IsNullOrEmpty(user.PhoneEvening) ?
            (string.IsNullOrEmpty(user.Mobile) ? "" :
                user.Mobile) :
            user.PhoneEvening) :
         user.PhoneDay);

Is there a better way to write this to make it more readable?

Body:
Adding some more parenthesis might make it more readable.  You could also break it down into a series of if/else statements

if (user != null)
{
  if (!string.IsNullOrEmpty(user.PhoneDay))
    _viewModel.PhoneDefault = user.PhoneDay;
  else if (!string.IsNullOrEmpty(user.PhoneEvening))
    _viewModel.PhoneDefault = user.PhoneEvening;
  else if (!string.IsNullOrEmpty(user.Mobile))
    _viewModel.PhoneDefault = user.Mobile;
  else
    _viewModel.PhoneDefault = "";
}


问:
API Gateway Web-socket route selection not working
I have created a basic example APIGateway for websocket and integrated same lambda to all $connect $disconnect and a test route

Route selection expression being $request.body.action, I am trying to send a message to my test route. But it doesn't seems to be happening and its being routed to $default in every case.

I have tried using postman, writing a client Node Application using ws npm module and using wscat too.

Code using NodeJS and ws module

```<code>const WebSocket = require('ws');

const ws = new WebSocket('wss://2liojtynmju.execute-api.us-east-1.amazonaws.com/dev');

ws.on('open', () => {
    console.log('connected, sending message');
    ws.send(JSON.stringify({
        action: 'test'
    }));
});
ws.on('message', data => console.log(`From server: ${data}`));
ws.on('close', () => {
    console.log('disconnected');
    process.exit();
});
</code>```

Logs in cloudwatch looking like this (route key is still $default)

```<code>requestContext: {
    routeKey: '$default',
    messageId: 'XXXXXX',
    eventType: 'MESSAGE',
    extendedRequestId: 'XXXXXX=',
    requestTime: '23/Sep/2021:08:24:54 +0000',
    messageDirection: 'XX',
    stage: 'dev',
    connectedAt: XXXXXXXXXXXXX,
    requestTimeEpoch: XXXXXXXX,
    identity: { sourceIp: 'XXXXXXXXXXXX' },
    requestId: 'XXXXXXXXXX=',
    domainName: 'XXXXXXXXXXXXXXXXXXXXX',
    connectionId: 'XXXXXXXXXXXXXXXX',
    apiId: 'XXXXXXXXXX'
  },
  body: '{"action":"test"}',
  isBase64Encoded: false
}
</code>```

I am most certain that I am missing some key piece of information but couldn't find anything on internet so far.

答:
I had this problem and finally solved it by changing the integration request type to LAMBDA PROXY.

The rest of your code looks like it should word.

Body:
I have created a basic example APIGateway for websocket and integrated same lambda to all $connect $disconnect and a test route

Route selection expression being $request.body.action, I am trying to send a message to my test route. But it doesn't seems to be happening and its being routed to $default in every case.

I have tried using postman, writing a client Node Application using ws npm module and using wscat too.

Code using NodeJS and ws module

const WebSocket = require('ws');

const ws = new WebSocket('wss://2liojtynmju.execute-api.us-east-1.amazonaws.com/dev');

ws.on('open', () => {
    console.log('connected, sending message');
    ws.send(JSON.stringify({
        action: 'test'
    }));
});
ws.on('message', data => console.log(`From server: ${data}`));
ws.on('close', () => {
    console.log('disconnected');
    process.exit();
});

Logs in cloudwatch looking like this (route key is still $default)

requestContext: {
    routeKey: '$default',
    messageId: 'XXXXXX',
    eventType: 'MESSAGE',
    extendedRequestId: 'XXXXXX=',
    requestTime: '23/Sep/2021:08:24:54 +0000',
    messageDirection: 'XX',
    stage: 'dev',
    connectedAt: XXXXXXXXXXXXX,
    requestTimeEpoch: XXXXXXXX,
    identity: { sourceIp: 'XXXXXXXXXXXX' },
    requestId: 'XXXXXXXXXX=',
    domainName: 'XXXXXXXXXXXXXXXXXXXXX',
    connectionId: 'XXXXXXXXXXXXXXXX',
    apiId: 'XXXXXXXXXX'
  },
  body: '{"action":"test"}',
  isBase64Encoded: false
}

I am most certain that I am missing some key piece of information but couldn't find anything on internet so far.


问:
TypeError: fs.readFileSync is not a function in Next.js
In a JSON file, I save data for a bot in total.json.

```<code>{ "guilds": 3, "users": 21 }
</code>```

In my index.tsx, I would like to put this data in the page, so I try this:

```<code>import fs from 'fs';

function stats() {
  const botcount = JSON.parse(fs.readFileSync(`../util/total.json`, { encoding: 'utf8' }));
  const userscount = botcount.users;

  console.log(userscount);
  return userscount;
}
</code>```

In the terminal, the function correctly returned the number (21), but in my page, I found this error:

```<code>TypeError: fs__WEBPACK_IMPORTED_MODULE_6___default(...).readFileSync is not a function
</code>```

答:
As @JaivBhup already mentioned, you can't use fs since it's not browser compatible.

A better approach IMO is to use a backend and fetch data from there (axios is a great package for this). If you don't have a backend of some kind, you should consider using the Next.js api routes.

You can use it as if you have been using Node.js!

See the docs or this could also be useful for you.

```<code>// File: pages/api/my-json.js

import fs from 'fs'
import path from 'path'

export default (req, res) => {
  // Let's say your json is in /public/assets/my-json.json
  const filePath = path.resolve('./public', 'assets', 'my-json.json');
  const json = fs.readFileSync(filePath);

  res.statusCode = 200
  res.json(json);
}
</code>```

The important part is path.resolve(...), which instructs vercel to include the scanned path in the serverless lambda. The shown code works to read images (or other files from your fs) both locally and remotely on vercel!

I tweaked it a little bit, so it loads the json file instead of filenames.

Body:
In a JSON file, I save data for a bot in total.json.

{ "guilds": 3, "users": 21 }

In my index.tsx, I would like to put this data in the page, so I try this:

import fs from 'fs';

function stats() {
  const botcount = JSON.parse(fs.readFileSync(`../util/total.json`, { encoding: 'utf8' }));
  const userscount = botcount.users;

  console.log(userscount);
  return userscount;
}

In the terminal, the function correctly returned the number (21), but in my page, I found this error:

TypeError: fs__WEBPACK_IMPORTED_MODULE_6___default(...).readFileSync is not a function

Body:
As @JaivBhup already mentioned, you can't use fs since it's not browser compatible.

A better approach IMO is to use a backend and fetch data from there (axios is a great package for this). If you don't have a backend of some kind, you should consider using the Next.js api routes.

You can use it as if you have been using Node.js!

See the docs or this could also be useful for you.

// File: pages/api/my-json.js

import fs from 'fs'
import path from 'path'

export default (req, res) => {
  // Let's say your json is in /public/assets/my-json.json
  const filePath = path.resolve('./public', 'assets', 'my-json.json');
  const json = fs.readFileSync(filePath);

  res.statusCode = 200
  res.json(json);
}

The important part is path.resolve(...), which instructs vercel to include the scanned path in the serverless lambda. The shown code works to read images (or other files from your fs) both locally and remotely on vercel!

I tweaked it a little bit, so it loads the json file instead of filenames.


问:
Problem Using Cursor to Execute a Database Query
I'm trying to do a query similar to this:

```<code>ctx = snowflake.connector.connect(
          host=host,
          user=user,
          password=password,
          account=account,
          warehouse=warehouse,
          database=database,
          schema=schema,
          protocol='https',
          port=port)

## Create a cursor object.
cur = ctx.cursor()

## Execute a statement that will generate a result set.
sql = "select * from t"
cur.execute(sql)
</code>```

My query looks something like this:

```<code>select NUMBER, ID, DATE, CODE from DATABASE.SCHEMA.TABLE
where (cast(CODE as string) like 'ABC12%'
and (DATE < '2021-12-31' and DATE >= '2021-10-01')
</code>```

I'm getting this error:

```<code>ProgrammingError: 001003 (42000): SQL compilation error:
syntax error line 3 at position 52 unexpected '<EOF>'.
</code>```

I've looked up this error code and tried removing any special characters that might be there, but no luck.

Thanks in advance

答:
If we re-format the SQL:

```<code>select
    NUMBER,
    ID,
    DATE,
    CODE
from DATABASE.SCHEMA.TABLE
where (
    cast(CODE as string) like 'ABC12%'
    and (
        DATE < '2021-12-31' and DATE >= '2021-10-01'
        )
</code>```

we can see you are missing the close bracket/paren to the WHERE clause.

But you don't actually need any brackets on this:

```<code>select
    NUMBER,
    ID,
    DATE,
    CODE
from DATABASE.SCHEMA.TABLE
where CODE::text like 'ABC12%'
    and DATE < '2021-12-31' and DATE >= '2021-10-01'
</code>```

Body:
I'm trying to do a query similar to this:

ctx = snowflake.connector.connect(
          host=host,
          user=user,
          password=password,
          account=account,
          warehouse=warehouse,
          database=database,
          schema=schema,
          protocol='https',
          port=port)

## Create a cursor object.
cur = ctx.cursor()

## Execute a statement that will generate a result set.
sql = "select * from t"
cur.execute(sql)

My query looks something like this:

select NUMBER, ID, DATE, CODE from DATABASE.SCHEMA.TABLE
where (cast(CODE as string) like 'ABC12%'
and (DATE < '2021-12-31' and DATE >= '2021-10-01')

I'm getting this error:

ProgrammingError: 001003 (42000): SQL compilation error:
syntax error line 3 at position 52 unexpected '<EOF>'.

I've looked up this error code and tried removing any special characters that might be there, but no luck.

Thanks in advance

Body:
If we re-format the SQL:

select
    NUMBER,
    ID,
    DATE,
    CODE
from DATABASE.SCHEMA.TABLE
where (
    cast(CODE as string) like 'ABC12%'
    and (
        DATE < '2021-12-31' and DATE >= '2021-10-01'
        )

we can see you are missing the close bracket/paren to the WHERE clause.

But you don't actually need any brackets on this:

select
    NUMBER,
    ID,
    DATE,
    CODE
from DATABASE.SCHEMA.TABLE
where CODE::text like 'ABC12%'
    and DATE < '2021-12-31' and DATE >= '2021-10-01'