问:
Merge data frames and replace NONE with values in R
I have two data.frames:

```<code>data.frame1:
CustID  FirstName   LastName    Address         DOB         City    Phone
132    Mary         K               999 Drive   1/1/2011    Chicago 888-0000
133    Mona         J               222 Road    1/4/2002    NY      999-8888
188    Jack         S               122 Street  9/2/2009    Washin  777-9999
None    Helen       L               111 Rd      1/4/2010
None    John        M               888 Lane    4/2/2002
None    Sally       K               222 Street  2/3/2002

data.frame2
CustID FirstName LastName Address   DOB         City
132    Mary      K        999 Drive 1/1/2011    Chicago
133    Mona      J         222 Road 1/4/2002    NY
188    Jack      S      122 Street  9/2/2009    Washington
3338    Helen   L         111 Rd    1/4/2010
882     John    M       888 Lane    4/2/2002
976    Sally    K     222 Street    2/3/2002
</code>```

Data.frame1 contains None in CustID column. I need to replace these Nones with a CustID from data.frame2 and make sure that columns FirstName, LastName, Address, DOB match from both data sets, because some names can match from both data sets but have different address and DOB - these are not the same people.
I have converted these columns into character from factor (not sure if it matters), and applied match() function but received 0 matches (which i know is wrong)
this is my code:

```<code>data.frame1$ID[match(c(data.framr2$'FirstName',
                     data.frame2$'LastName',
                     data.frame2$'DOB',
                     data.frame2$'Address'),
                     c(data.frame1$'FirstName',
                     data.frame1$'LastName',
                     data.frame1$'DOB',
                     data.frame1$'Address'))]
</code>```

答:
Here is one way using dplyr.

```<code>  library(dplyr)

  df1 <- read.table(text =
       "CustID  FirstName   LastName    Address         DOB         City    Phone
  132    Mary         K               999Drive   1/1/2011    Chicago 888-0000
  133    Mona         J               222Road    1/4/2002    NY      999-8888
  188    Jack         S               122Street  9/2/2009    Washin  777-9999
  None    Helen       L               111Rd      1/4/2010     ''     ''
  None    John        M               888Lane    4/2/2002       ''   ''
  None    Sally       K               222Street  2/3/2002        ''  ''"
  , header = T, stringsAsFactors = F)

  df2 <- read.table(text=
  "CustID FirstName LastName Address   DOB         City
  132    Mary      K        999Drive 1/1/2011    Chicago
  133    Mona      J         222Road 1/4/2002    NY
  188    Jack      S      122Street  9/2/2009    Washington
  3338    Helen   L         111Rd    1/4/2010     ''
  882     John    M       888Lane    4/2/2002       ''
  976    Sally    K     222Street    2/3/2002    ''"
  , header = T, stringsAsFactors = F)

  df1 %>% left_join(df2 %>% select(-City), by = c('FirstName', 'LastName', 'DOB', 'Address')) %>%
       mutate(CustID = ifelse(CustID.y == "None", CustID.x, CustID.y)) %>% select(-CustID.x, -CustID.y)

        FirstName LastName   Address      DOB    City    Phone CustID
1      Mary        K  999Drive 1/1/2011 Chicago 888-0000    132
2      Mona        J   222Road 1/4/2002      NY 999-8888    133
3      Jack        S 122Street 9/2/2009  Washin 777-9999    188
4     Helen        L     111Rd 1/4/2010                    3338
5      John        M   888Lane 4/2/2002                     882
6     Sally        K 222Street 2/3/2002                     976
</code>```

Body:
I have two data.frames:

data.frame1:
CustID  FirstName   LastName    Address         DOB         City    Phone
132    Mary         K               999 Drive   1/1/2011    Chicago 888-0000
133    Mona         J               222 Road    1/4/2002    NY      999-8888
188    Jack         S               122 Street  9/2/2009    Washin  777-9999
None    Helen       L               111 Rd      1/4/2010
None    John        M               888 Lane    4/2/2002
None    Sally       K               222 Street  2/3/2002

data.frame2
CustID FirstName LastName Address   DOB         City
132    Mary      K        999 Drive 1/1/2011    Chicago
133    Mona      J         222 Road 1/4/2002    NY
188    Jack      S      122 Street  9/2/2009    Washington
3338    Helen   L         111 Rd    1/4/2010
882     John    M       888 Lane    4/2/2002
976    Sally    K     222 Street    2/3/2002

Data.frame1 contains None in CustID column. I need to replace these Nones with a CustID from data.frame2 and make sure that columns FirstName, LastName, Address, DOB match from both data sets, because some names can match from both data sets but have different address and DOB - these are not the same people.
I have converted these columns into character from factor (not sure if it matters), and applied match() function but received 0 matches (which i know is wrong)
this is my code:

data.frame1$ID[match(c(data.framr2$'FirstName',
                     data.frame2$'LastName',
                     data.frame2$'DOB',
                     data.frame2$'Address'),
                     c(data.frame1$'FirstName',
                     data.frame1$'LastName',
                     data.frame1$'DOB',
                     data.frame1$'Address'))]

Body:
Here is one way using dplyr.

  library(dplyr)

  df1 <- read.table(text =
       "CustID  FirstName   LastName    Address         DOB         City    Phone
  132    Mary         K               999Drive   1/1/2011    Chicago 888-0000
  133    Mona         J               222Road    1/4/2002    NY      999-8888
  188    Jack         S               122Street  9/2/2009    Washin  777-9999
  None    Helen       L               111Rd      1/4/2010     ''     ''
  None    John        M               888Lane    4/2/2002       ''   ''
  None    Sally       K               222Street  2/3/2002        ''  ''"
  , header = T, stringsAsFactors = F)

  df2 <- read.table(text=
  "CustID FirstName LastName Address   DOB         City
  132    Mary      K        999Drive 1/1/2011    Chicago
  133    Mona      J         222Road 1/4/2002    NY
  188    Jack      S      122Street  9/2/2009    Washington
  3338    Helen   L         111Rd    1/4/2010     ''
  882     John    M       888Lane    4/2/2002       ''
  976    Sally    K     222Street    2/3/2002    ''"
  , header = T, stringsAsFactors = F)

  df1 %>% left_join(df2 %>% select(-City), by = c('FirstName', 'LastName', 'DOB', 'Address')) %>%
       mutate(CustID = ifelse(CustID.y == "None", CustID.x, CustID.y)) %>% select(-CustID.x, -CustID.y)

        FirstName LastName   Address      DOB    City    Phone CustID
1      Mary        K  999Drive 1/1/2011 Chicago 888-0000    132
2      Mona        J   222Road 1/4/2002      NY 999-8888    133
3      Jack        S 122Street 9/2/2009  Washin 777-9999    188
4     Helen        L     111Rd 1/4/2010                    3338
5      John        M   888Lane 4/2/2002                     882
6     Sally        K 222Street 2/3/2002                     976


来源: stackexchange

问:
Bash help tallying/parsing substrings
I have a shell script I wrote a while back, that reads a word list (HITLIST), and recursively searches a directory for all occurrences of those words. Each line containing a "hit" is appended to file (HITOUTPUT).

I have used this script a couple of times over the last year or so, and have noticed that we often get hits from frequent offenders, and that it would be nice if we kept a count of each "super-string" that is triggered, and automatically remove repeat offenders.

For instance, if my word list contains "for" I might get a hundred hits or so for "foreign" or "form" or "force". Instead of validating each of these lines, it would be nice to simply wipe them all with one "yes/no" dialog per super-string.

I was thinking the best way to do this would be to start with a word from the hitlist, and record each unique occurrence of the super-string for that word (go until you are book-ended by why space) and go from there.

So on to the questions ...

What would be a good and efficient way to do this? My current idea
was to read in the file as a string, perform my counts, remove
repeat offenders from the the file input string, and output, but this is
proving to be a little more painful that I first suspected.

Would any specific data type/structure be preferred for this type of
work?

I have also thought about building the super-string count as I
create the HitOutput file, but I could not figure out a clean way of
doing this either. Any thoughts or suggestions?

A sample of the file I am reading in, and my code for reading in and traversing the hitlist and creating the HitOutput file below:

```<code># Loop through hitlist list

    while read -re hitlist || [[ -n "$hitlist" ]]
    do

        # If first character is "#" it's a comment, or line is blank, skip
        if [ "$(echo $hitlist | head -c 1)" != "#" ]; then

            if [ ! -z "$hitlist" -a "$histlist" != "" ]; then

                # Parse comma delimited hitlist
                IFS=',' read -ra categoryWords <<< "$hitlist"

                # Search for occurrences/hits for each hit
                for categoryWord in "${categoryWords[@]}"; do
                    # Append results to hit output string
                    eval 'find "$DIR" -type f -print0 | xargs -0 grep -HniI "$categoryWord"' >> HITOUTPUT
                done

            fi
        fi
done < "$HITLIST"
</code>```

src/fakescript.sh:1:Never going to win the war you mother!

src/open_source_licenses.txt:6147:May you share freely, never taking more than you give.

src/open_source_licenses.txt:8764:May you share freely, never taking more than you give.

src/open_source_licenses.txt:21711:No Third Party Beneficiaries. You agree that, except as otherwise expressly provided in this TOS, there shall be no third party beneficiaries to this Agreement. Waiver and Severability of Terms. The failure of UBM LLC to exercise or enforce any right or provision of the TOS shall
  not constitute a waiver of such right or provision. If any provision of the TOS is found by a court of competent jurisdiction to be invalid, the parties nevertheless agree that the court should endeavor to give effect to the parties' intentions as reflected in the provision, and the other provisions of the TOS remain in full force and effect.

src/fakescript.sh:1:Never going to win the war you mother!

An example of my hitlist file:

```<code># Comment out any category word lines that you do not want processed (the comma delimited lines)
# -----------------

# MEH
never,going,to give,you up
# ----------------

# blah
word to,your,mother
</code>```

答:
Let's divide this problem into two parts.  First, we will update the hitlist interactively as requires by your customer.  Second, we will find all matches to the updated hitlist.

1. Updating the hitlist

This searches for all words in files under directory dir that contain any word on the hitlist:

```<code>#!/bin/bash
grep -Erowhf <(sed -E 's/.*/([[:alpha:]]+&[[:alpha:]]*|[[:alpha:]]*&[[:alpha:]]+)/' hitlist) dir |
    sort |
    uniq -c |
    while read n word
    do
       read -u 2 -p "$word occurs $n times.  Include (y/n)? " a
       [ "$a" = y ] && echo "$word" >>hitlist
    done
</code>```

This script runs interactively.  As an example, suppose that dir contains these two files:

```<code>$ cat dir/file1.txt
for all  foreign or catapult also cat.
The catapult hit the catermaran.
The form of a foreign formula
$ cat dir/file2.txt
dog and cat and formula, formula, formula
</code>```

And hitlist contains two words:

```<code>$ cat hitlist
for
cat
</code>```

If we then run our script, it looks like:

```<code>$ bash script.sh
catapult occurs 2 times.  Include (y/n)? y
catermaran occurs 1 times.  Include (y/n)? n
foreign occurs 2 times.  Include (y/n)? y
form occurs 1 times.  Include (y/n)? n
formula occurs 4 times.  Include (y/n)? n
</code>```

After the script is run, the file hitlist is updated with all the words that you want to include.  We are now ready to proceed to the next step:

2. Finding matches to the updated hitlist

To read each word from a "hitlist" and search recursively for matches while ignoring, foreign even if the hitlist contains for, try:

```<code>grep -wrFf ../hitlist dir
</code>```

-w tells grep to look only for full-words.  Thus foreign will be ignored.

-r tells grep to search recursively.

-F tells grep to treat the hitlist as word, not regular expressions. (optional)

-f ../hitlist tells grep to read words from the file ../hitlist.

Following on with the example above, we would have:

```<code>$ grep -wrFf ./hitlist dir
dir/file2.txt:dog and cat and formula, formula, formula
dir/file1.txt:for all  foreign or catapult also cat.
dir/file1.txt:The catapult hit the catermaran.
dir/file1.txt:The form of a foreign formula
</code>```

If we don't want the file names displayed, use the -h option:

```<code>$ grep -hwrFf ./hitlist dir
dog and cat and formula, formula, formula
for all  foreign or catapult also cat.
The catapult hit the catermaran.
The form of a foreign formula
</code>```

Automatic update for counts 10 or less

```<code>#!/bin/bash
grep -Erowhf <(sed -E 's/.*/([[:alpha:]]+&[[:alpha:]]*|[[:alpha:]]*&[[:alpha:]]+)/' hitlist) dir |
    sort |
    uniq -c |
    while read n word
    do
       a=y
       [ "$n" -gt 10 ] && read -u 2 -p "$word occurs $n times.  Include (y/n)? " a
       [ "$a" = y ] && echo "$word" >>hitlist
    done
</code>```

Reformatting the customer's hitlist

I see that your customer's hitlist has extra formatting, including comments, empty lines, and duplicated words.  For example:

```<code>$ cat hitlist.source
# MEH
never,going,to give,you up
# ----------------

# blah
word to,your,mother
</code>```

To convert that to format useful here, try:

```<code>$ sed -E 's/#.*//; s/[[:space:],]+/\n/g; s/\n\n+/\n/g; /^$/d' hitlist.source | grep . | sort -u >hitlist
$ cat hitlist
give
going
mother
never
to
up
word
you
your
</code>```

ContentLicense: CC BY-SA 3.0

Title: Bash help tallying/parsing substrings

Body:
I have a shell script I wrote a while back, that reads a word list (HITLIST), and recursively searches a directory for all occurrences of those words. Each line containing a "hit" is appended to file (HITOUTPUT).

I have used this script a couple of times over the last year or so, and have noticed that we often get hits from frequent offenders, and that it would be nice if we kept a count of each "super-string" that is triggered, and automatically remove repeat offenders.

For instance, if my word list contains "for" I might get a hundred hits or so for "foreign" or "form" or "force". Instead of validating each of these lines, it would be nice to simply wipe them all with one "yes/no" dialog per super-string.

I was thinking the best way to do this would be to start with a word from the hitlist, and record each unique occurrence of the super-string for that word (go until you are book-ended by why space) and go from there.

So on to the questions ...

What would be a good and efficient way to do this? My current idea
was to read in the file as a string, perform my counts, remove
repeat offenders from the the file input string, and output, but this is
proving to be a little more painful that I first suspected.

Would any specific data type/structure be preferred for this type of
work?

I have also thought about building the super-string count as I
create the HitOutput file, but I could not figure out a clean way of
doing this either. Any thoughts or suggestions?

A sample of the file I am reading in, and my code for reading in and traversing the hitlist and creating the HitOutput file below:

# Loop through hitlist list

    while read -re hitlist || [[ -n "$hitlist" ]]
    do

        # If first character is "#" it's a comment, or line is blank, skip
        if [ "$(echo $hitlist | head -c 1)" != "#" ]; then

            if [ ! -z "$hitlist" -a "$histlist" != "" ]; then

                # Parse comma delimited hitlist
                IFS=',' read -ra categoryWords <<< "$hitlist"

                # Search for occurrences/hits for each hit
                for categoryWord in "${categoryWords[@]}"; do
                    # Append results to hit output string
                    eval 'find "$DIR" -type f -print0 | xargs -0 grep -HniI "$categoryWord"' >> HITOUTPUT
                done

            fi
        fi
done < "$HITLIST"

src/fakescript.sh:1:Never going to win the war you mother!

src/open_source_licenses.txt:6147:May you share freely, never taking more than you give.

src/open_source_licenses.txt:8764:May you share freely, never taking more than you give.

src/open_source_licenses.txt:21711:No Third Party Beneficiaries. You agree that, except as otherwise expressly provided in this TOS, there shall be no third party beneficiaries to this Agreement. Waiver and Severability of Terms. The failure of UBM LLC to exercise or enforce any right or provision of the TOS shall
  not constitute a waiver of such right or provision. If any provision of the TOS is found by a court of competent jurisdiction to be invalid, the parties nevertheless agree that the court should endeavor to give effect to the parties' intentions as reflected in the provision, and the other provisions of the TOS remain in full force and effect.

src/fakescript.sh:1:Never going to win the war you mother!

An example of my hitlist file:

# Comment out any category word lines that you do not want processed (the comma delimited lines)
# -----------------

# MEH
never,going,to give,you up
# ----------------

# blah
word to,your,mother

Body:
Let's divide this problem into two parts.  First, we will update the hitlist interactively as requires by your customer.  Second, we will find all matches to the updated hitlist.

1. Updating the hitlist

This searches for all words in files under directory dir that contain any word on the hitlist:

#!/bin/bash
grep -Erowhf <(sed -E 's/.*/([[:alpha:]]+&[[:alpha:]]*|[[:alpha:]]*&[[:alpha:]]+)/' hitlist) dir |
    sort |
    uniq -c |
    while read n word
    do
       read -u 2 -p "$word occurs $n times.  Include (y/n)? " a
       [ "$a" = y ] && echo "$word" >>hitlist
    done

This script runs interactively.  As an example, suppose that dir contains these two files:

$ cat dir/file1.txt
for all  foreign or catapult also cat.
The catapult hit the catermaran.
The form of a foreign formula
$ cat dir/file2.txt
dog and cat and formula, formula, formula

And hitlist contains two words:

$ cat hitlist
for
cat

If we then run our script, it looks like:

$ bash script.sh
catapult occurs 2 times.  Include (y/n)? y
catermaran occurs 1 times.  Include (y/n)? n
foreign occurs 2 times.  Include (y/n)? y
form occurs 1 times.  Include (y/n)? n
formula occurs 4 times.  Include (y/n)? n

After the script is run, the file hitlist is updated with all the words that you want to include.  We are now ready to proceed to the next step:

2. Finding matches to the updated hitlist

To read each word from a "hitlist" and search recursively for matches while ignoring, foreign even if the hitlist contains for, try:

grep -wrFf ../hitlist dir

-w tells grep to look only for full-words.  Thus foreign will be ignored.

-r tells grep to search recursively.

-F tells grep to treat the hitlist as word, not regular expressions. (optional)

-f ../hitlist tells grep to read words from the file ../hitlist.

Following on with the example above, we would have:

$ grep -wrFf ./hitlist dir
dir/file2.txt:dog and cat and formula, formula, formula
dir/file1.txt:for all  foreign or catapult also cat.
dir/file1.txt:The catapult hit the catermaran.
dir/file1.txt:The form of a foreign formula

If we don't want the file names displayed, use the -h option:

$ grep -hwrFf ./hitlist dir
dog and cat and formula, formula, formula
for all  foreign or catapult also cat.
The catapult hit the catermaran.
The form of a foreign formula

Automatic update for counts 10 or less

#!/bin/bash
grep -Erowhf <(sed -E 's/.*/([[:alpha:]]+&[[:alpha:]]*|[[:alpha:]]*&[[:alpha:]]+)/' hitlist) dir |
    sort |
    uniq -c |
    while read n word
    do
       a=y
       [ "$n" -gt 10 ] && read -u 2 -p "$word occurs $n times.  Include (y/n)? " a
       [ "$a" = y ] && echo "$word" >>hitlist
    done

Reformatting the customer's hitlist

I see that your customer's hitlist has extra formatting, including comments, empty lines, and duplicated words.  For example:

$ cat hitlist.source
# MEH
never,going,to give,you up
# ----------------

# blah
word to,your,mother

To convert that to format useful here, try:

$ sed -E 's/#.*//; s/[[:space:],]+/\n/g; s/\n\n+/\n/g; /^$/d' hitlist.source | grep . | sort -u >hitlist
$ cat hitlist
give
going
mother
never
to
up
word
you
your


问:
Ploting building surfaces from CityGML data
I've been struggling with 3D ploting some coordinates since a long ago and now I'm really frustrated, so your help will be really appreciated.

I'd like to plot the facade of a building from a CityGML file (which is originally simply an XML file). I have no problem with parsing the CityGML file using XML.etree and extracting the coordinates. But after extracting the coordinates, I cann't find a way to 3D plot them.

```<code>from xml.etree import ElementTree as ET

tree = ET.parse('3860_5819__.gml')
root = tree.getroot()

namespaces = {
    'ns0': "http://www.opengis.net/citygml/1.0",
    'ns1': "http://www.opengis.net/gml",
    'ns2': "http://www.opengis.net/citygml/building/1.0"
}

c = 0
wallString = []
for wallSurface in root.findall('.//ns2:WallSurface', namespaces):

    for posList in wallSurface.findall('.//ns1:posList', namespaces):
        c += 1
        wallCoordinates = posList.text
        wallCoordinates = wallCoordinates.split()
        wallString.append(wallCoordinates)

verts = []
for string in wallString:
    X, Y, Z = [], [], []

    c = 0
    for value in string:
        value = float(value)

        if c % 3 == 0:
            X.append(value)
        elif c % 3 == 1:
            Y.append(value)
        else:
            Z.append(value)

        c += 1
        if c > len(string) - 3:
            break

    vert = [list(zip(X, Y, Z))]
    verts.append(vert)

from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt

fig = plt.figure()
ax = Axes3D(fig)

for vert in verts:
    ax.add_collection3d(Poly3DCollection(vert))

ax.autoscale_view(tight=True, scalex=True, scaley=True, scalez=True)
plt.show()
plt.close()
</code>```

Could the problem be that I can't make my plot "tight"? And if not, is there something I'm doing fundamentally wrong?

If relevant, the CityGML file in this case is related to TU Berlin center of entrepreneurship which can be taken from here.

答:
Just realized that nothing was wrong with the main part of the code. The only issue was that the axis were not set. I change the plot part like this:

```<code>import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d as mpl3

fig = plt.figure()
ax = mpl3.Axes3D(fig)

for vert in verts:
    poly = mpl3.art3d.Poly3DCollection(vert)
    ax.add_collection3d(poly)
ax.set_xlim3d(left=386284-50,right=386284+50)
ax.set_ylim3d(bottom=5819224-50, top=5819224+50)
ax.set_zlim3d(bottom=32-10,top=32+20)

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

Now it works perfectly fine.

Body:
I've been struggling with 3D ploting some coordinates since a long ago and now I'm really frustrated, so your help will be really appreciated.

I'd like to plot the facade of a building from a CityGML file (which is originally simply an XML file). I have no problem with parsing the CityGML file using XML.etree and extracting the coordinates. But after extracting the coordinates, I cann't find a way to 3D plot them.

from xml.etree import ElementTree as ET

tree = ET.parse('3860_5819__.gml')
root = tree.getroot()

namespaces = {
    'ns0': "http://www.opengis.net/citygml/1.0",
    'ns1': "http://www.opengis.net/gml",
    'ns2': "http://www.opengis.net/citygml/building/1.0"
}

c = 0
wallString = []
for wallSurface in root.findall('.//ns2:WallSurface', namespaces):

    for posList in wallSurface.findall('.//ns1:posList', namespaces):
        c += 1
        wallCoordinates = posList.text
        wallCoordinates = wallCoordinates.split()
        wallString.append(wallCoordinates)

verts = []
for string in wallString:
    X, Y, Z = [], [], []

    c = 0
    for value in string:
        value = float(value)

        if c % 3 == 0:
            X.append(value)
        elif c % 3 == 1:
            Y.append(value)
        else:
            Z.append(value)

        c += 1
        if c > len(string) - 3:
            break

    vert = [list(zip(X, Y, Z))]
    verts.append(vert)

from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt

fig = plt.figure()
ax = Axes3D(fig)

for vert in verts:
    ax.add_collection3d(Poly3DCollection(vert))

ax.autoscale_view(tight=True, scalex=True, scaley=True, scalez=True)
plt.show()
plt.close()

Could the problem be that I can't make my plot "tight"? And if not, is there something I'm doing fundamentally wrong?

If relevant, the CityGML file in this case is related to TU Berlin center of entrepreneurship which can be taken from here.

Body:
Just realized that nothing was wrong with the main part of the code. The only issue was that the axis were not set. I change the plot part like this:

import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d as mpl3

fig = plt.figure()
ax = mpl3.Axes3D(fig)

for vert in verts:
    poly = mpl3.art3d.Poly3DCollection(vert)
    ax.add_collection3d(poly)
ax.set_xlim3d(left=386284-50,right=386284+50)
ax.set_ylim3d(bottom=5819224-50, top=5819224+50)
ax.set_zlim3d(bottom=32-10,top=32+20)

plt.show()
plt.close()

Now it works perfectly fine.


问:
last entry per group with additional conditions
I have the following code for MYSQL.

```<code>SELECT p1.*
  FROM `tags` p1
  LEFT
  JOIN `tags` p2
    ON (p1.cabinetid = p2.cabinetid AND p1.timestamp < p2.timestamp)
 WHERE p2.timestamp IS NULL
</code>```

I want to add the conditional "WHERE unitid = 5".  I'm having a brain fart moment and cannot figure out where to put that.

the source table looks like this:

```<code>Let me clarify what I want this to do.  I have one table.
</code>```

The structure looks like this :

```<code>unitid - cabinetid - timestamp
5        -      6          - timestamp
5        -       7        - timestamp
6        -       6        -   timestamp
7       -        8        - timestamp
</code>```

I want to get all entries for unitid 5, and get the latest entry for each cabinetid

答: From which table unitid belongs then give table.unitid =5</p>

Body:
I have the following code for MYSQL.

SELECT p1.*
  FROM `tags` p1
  LEFT
  JOIN `tags` p2
    ON (p1.cabinetid = p2.cabinetid AND p1.timestamp < p2.timestamp)
 WHERE p2.timestamp IS NULL

I want to add the conditional "WHERE unitid = 5".  I'm having a brain fart moment and cannot figure out where to put that.

the source table looks like this:

Let me clarify what I want this to do.  I have one table.

The structure looks like this :

unitid - cabinetid - timestamp
5        -      6          - timestamp
5        -       7        - timestamp
6        -       6        -   timestamp
7       -        8        - timestamp

I want to get all entries for unitid 5, and get the latest entry for each cabinetid


问:
Haxe: Native Interface properties implementable?
I've got this compiletime errors when I make some class implement an interface with properties that have been fromerly defined in some native sub class, like openfl.display.Sprite. It occurs when I'm targeting flash, not js.

```<code>Field get_someValue needed by SomeInterface is missing
Field set_someValue needed by SomeInterface is missing
Field someValue has different property access than in SomeInterface (var should be (get,set))
</code>```

In contrast, there's no problem with interface definitions of 'native' methods or 'non-native' properties. Those work.

Do I have to avoid that (not so typical) use of interfaces with haxe and rewrite my code? Or is there any way to bypass this problem?

Thanks in advance.

Example:

```<code>class NativePropertyInterfaceImplTest
{
    public function new()
    {
        var spr:FooSprite = new FooSprite();
        spr.visible = !spr.visible;
    }
}

class FooSprite extends Sprite implements IFoo
{
    public function new()
    {
        super();
    }
}

interface IFoo
{
    public var visible (get, set):Bool; // Cannot use this ):
}
</code>```

答:
TL;DR

You need to use a slightly different signature on the Flash target:

interface IFoo
{
    #if flash
    public var visible:Bool;
    #else
    public var visible (get, set):Bool;
    #end
}
```

<h3>Additional Information</h3>

<p>Haxe <code>get</code> and <code>set</code> imply that <code>get_property():T</code> and <code>set_property(value:T):T</code> both exist. OpenFL uses this syntax for many properties, including <code>displayObject.visible</code>.</p>

<p>Core ActionScript VM classes (such as <code>Sprite</code>) don't use Haxe <code>get</code>/<code>set</code>, but are native properties. This is why they look different.</p>

<h3>Overriding Core Properties</h3>

<p>If you ever need to override core properties like this, here is an example of how you would do so for both Flash and other targets on OpenFL:</p>

<pre class="lang-js prettyprint-override"><code>class CustomSprite extends Sprite {

    private var _visible:Bool = true;

    public function new () {

        super ();

    }

    #if flash

    @:getter(visible) private function get_visible ():Bool { return _visible; }
    @:setter(visible) private function set_visible (value:Bool):Void { _visible = value; }

    #else

    private override function get_visible ():Bool { return _visible; }
    private override function set_visible (value:Bool):Bool { return _visible = value; }

    #end

}
</code>```

Overriding Custom Properties

This is not needed for custom properties, which are the same on all platforms:

class BaseClass {

    public var name (default, set):String;

    public function new () {

    }

    private function set_name (value:String) {

        return this.name = value;

    }

}

class SuperClass {

    public function new () {

        super ();

    }

    private override function set_name (value:String):String {

        return this.name = value + " Q. Public";

    }

}
```

Body:
I've got this compiletime errors when I make some class implement an interface with properties that have been fromerly defined in some native sub class, like openfl.display.Sprite. It occurs when I'm targeting flash, not js.

Field get_someValue needed by SomeInterface is missing
Field set_someValue needed by SomeInterface is missing
Field someValue has different property access than in SomeInterface (var should be (get,set))

In contrast, there's no problem with interface definitions of 'native' methods or 'non-native' properties. Those work.

Do I have to avoid that (not so typical) use of interfaces with haxe and rewrite my code? Or is there any way to bypass this problem?

Thanks in advance.

Example:

class NativePropertyInterfaceImplTest
{
    public function new()
    {
        var spr:FooSprite = new FooSprite();
        spr.visible = !spr.visible;
    }
}

class FooSprite extends Sprite implements IFoo
{
    public function new()
    {
        super();
    }
}

interface IFoo
{
    public var visible (get, set):Bool; // Cannot use this ):
}

Body:
TL;DR

You need to use a slightly different signature on the Flash target:

interface IFoo
{
    #if flash
    public var visible:Bool;
    #else
    public var visible (get, set):Bool;
    #end
}

Additional Information

Haxe get and set imply that get_property():T and set_property(value:T):T both exist. OpenFL uses this syntax for many properties, including displayObject.visible.

Core ActionScript VM classes (such as Sprite) don't use Haxe get/set, but are native properties. This is why they look different.

Overriding Core Properties

If you ever need to override core properties like this, here is an example of how you would do so for both Flash and other targets on OpenFL:

class CustomSprite extends Sprite {

    private var _visible:Bool = true;

    public function new () {

        super ();

    }

    #if flash

    @:getter(visible) private function get_visible ():Bool { return _visible; }
    @:setter(visible) private function set_visible (value:Bool):Void { _visible = value; }

    #else

    private override function get_visible ():Bool { return _visible; }
    private override function set_visible (value:Bool):Bool { return _visible = value; }

    #end

}

Overriding Custom Properties

This is not needed for custom properties, which are the same on all platforms:

class BaseClass {

    public var name (default, set):String;

    public function new () {

    }

    private function set_name (value:String) {

        return this.name = value;

    }

}

class SuperClass {

    public function new () {

        super ();

    }

    private override function set_name (value:String):String {

        return this.name = value + " Q. Public";

    }

}


问:
Prevent "Send error report to Microsoft"
I'm working on a rather large project, and its unlikely will catch everything. I've found the event that notifies me of unhandled exceptions, however I haven't found a way to programmatically shut off the windows error dialog. Ideally, if there is an unhandled exception, I would like to have that event fired, provide a dialog box telling the user that there is a problem, and then to close gracefully. Is there any way to do this? I realize I could wrap the highest layer in a try catch, but I was hoping for something a little more elegant. </p>

答:
You may try playing with HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\AeDebug

That tip is form DrWatson days .. may or may not work.

OwnerDisplayName: Steve

ContentLicense: CC BY-SA 2.5

LastEditorDisplayName: Rich B

OwnerDisplayName: VardhanDotNet


问:
How to deny other users/roles from creating EC2 instances with an IAM role
I have an S3 bucket with confidential data.

I added a bucket policy to allow only a limited set of roles within the account. This stops other user from accessing the s3 bucket from console.

One of the allowed roles, say "foo-role" is created for EC2 instances to read the S3 bucket.

Now, even the denied roles can create a VM, assign the "foo-role" to this VM, ssh into this VM and look at the bucket content.

Is there a way that I can prevent other users from assigning the "foo-role" to their EC2 instances.

答:
Add this policy to your IAM Users. This policy will prevent a user from associating or replacing a role to an EC2 instance.</p>

```<code>{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "DENY",
      "Action": [
         "ec2:AssociateIamInstanceProfile",
         "ec2:ReplaceIamInstanceProfileAssociation",
         "iam:PassRole"
      ],
      "Resource": "*"
    }
  ]
}
</code>```

Body:
Add this policy to your IAM Users. This policy will prevent a user from associating or replacing a role to an EC2 instance.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "DENY",
      "Action": [
         "ec2:AssociateIamInstanceProfile",
         "ec2:ReplaceIamInstanceProfileAssociation",
         "iam:PassRole"
      ],
      "Resource": "*"
    }
  ]
}


问:
Is it possible to retry an XMLHttpRequest that I Intercepted?
I am overriding the XMLHttpRequest.prototype.send = function(a,b) { } and XMLHttpRequest.prototype.open = function(a,b) { } so I can intercept every single response I get from every ajax call. Sometimes, some of those requests fail, and when that happens I need to update the cookies for the DOM and retry the failed original XHRs.

Taking in consideration that I have no control over those XHRs, meaning that I don't create them, or execute them, would it be possible for me to retry those XHR with new updated cookies?

Right now, I save the original XHR in a new variable, and when it fails, I do an xhr.open.apply() and then xhr.send.apply(). Using an HTTP Web Proxy like Charles I am able to check my response and I always get error message: Invalid request... I also realized that the difference between the original XHR and the retried XHR is that the X-Request-With header is always missing.

Any ideas on how to solve this issue?

答:
Well, it is possible and I managed to do it.

I created a function to retry the original request, where I pass the same parameters that were passed to the XMLHTTPRequest.prototype.send function that intercepted the XMLHTTPRequest before being send.

```<code>function retryOriginalRequest(a,b, args) {
   var originalRequest = new XMLHttpRequest();
   originalRequest.open(a,b, true);
   // The request headers need to be set up after the open call and before the send.
   originalRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
   originalRequest.send(args, null);
};
</code>```

Body:
Well, it is possible and I managed to do it.

I created a function to retry the original request, where I pass the same parameters that were passed to the XMLHTTPRequest.prototype.send function that intercepted the XMLHTTPRequest before being send.

function retryOriginalRequest(a,b, args) {
   var originalRequest = new XMLHttpRequest();
   originalRequest.open(a,b, true);
   // The request headers need to be set up after the open call and before the send.
   originalRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
   originalRequest.send(args, null);
};


问:
Change DB design to improve Spring Data JPA soft delete and authentication on queries
I hope this question is not opinion based, but here we go.
I am developing a small API with spring boot, spring data jpa, and JWT Token as the authentication system. The API is pretty straightforward, is an exam generator, where you can register professor, course, question, choice etc.

For example:
[13/11/2017 - edited to include AbstractEntity]

```<code>@MappedSuperclass
public class AbstractEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;
    protected boolean enabled = true;
    //equals, hashcode...
}

public class Professor extends AbstractEntity{
    private String email;
}

public class Course extends AbstractEntity {
    @ManyToOne(optional = false)
    private Professor professor;
}

public class Question extends AbstractEntity {
    @ManyToOne(optional = false)
    private Course course;
}
</code>```

Because the authentication system is JWT token for all queries I have to include the professor's information.

For example

```<code>public interface CourseRepository extends PagingAndSortingRepository<Course, Long> {
    @Query("select c from Course c where c.id = ?1 and c.professor = ?#{principal.professor} and c.enabled = true")
    Course findOne(Long id);
}
</code>```

The problem is that for every crud query I will have to write my own custom query, because of professor's information and also the enabled field.

I know there is a way to extends the PagingAndSortingRepository and write your own custom query, like this:

```<code>public interface CustomPagingAndSortRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
    @Override
    @Query("select e from #{#entityName} e where e.id = ?1 and e.professor = ?#{principal.professor} and e.enabled = true")
    T findOne(ID id);
    //More fields overrided here
}
</code>```

This CustomPagingAndSortRepository would work perfectly for all classes if I only had enabled to worry about. This class will work perfectly for Course because e.professor = ?#{principal.professor} will find the Professor there.  But it will not work for Question, for example, because I need a join with the course class to get the professor, like e.course.professor = ?#{principal.professor}

The point is f I put an association with Professor in all classes I can make that generic code work, save tons of code but sacrificing database design's normalization.
The question is: Is there another way? or maybe is it valid to do it?

PS: The Question, Course etc will never change owner, it means that only the Professor who created will be able to use it.

答:
Ok, I will answer my own question. So, the question was:

The point is if I put an association with Professor in all classes I can make that generic code work, save tons of code but sacrificing database design's normalization. The question is: Is there another way? or maybe is it valid to do it?

And I actually did, based on this answer and it reduced a lot of code.

Now I have my model classes like this (Included Professor in all other classes too):

```<code>@MappedSuperclass
public class AbstractEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;
    @Column(columnDefinition = "boolean default true", nullable = false)
    private boolean enabled = true;
// ...
}

@Entity
public class Course extends AbstractEntity {
    private String name;
    @ManyToOne(optional = false)
    private Professor professor;
    //...
}

@Entity
public class Question extends AbstractEntity {
    private String title;
    @ManyToOne(optional = false)
    private Course course;
    @ManyToOne(optional = false)
    private Professor professor;
    // ...
}
</code>```

And a CustomPagingAndSortRepository like this

```<code>@NoRepositoryBean
public interface CustomPagingAndSortRepository<T extends AbstractEntity, ID extends Long>
        extends PagingAndSortingRepository<T,ID> {
    @Override
    @Query("select e from #{#entityName} e where e.professor = ?#{principal.professor} and e.enabled = true")
    Iterable<T> findAll(Sort sort);

    @Override
    @Query("select e from #{#entityName} e where e.professor = ?#{principal.professor} and e.enabled = true")
    Page<T> findAll(Pageable pageable);

    @Override
    @Query("select e from #{#entityName} e where e.id =?1 and e.professor = ?#{principal.professor} and e.enabled = true")
    T findOne(Long id);

    @Override
    default boolean exists(Long id){
        return findOne(id) != null;
    }

    @Override
    @Query("select e from #{#entityName} e where e.professor = ?#{principal.professor} and e.enabled = true")
    Iterable<T> findAll();

    @Override
    @Query("select e from #{#entityName} e where e.professor = ?#{principal.professor} and e.enabled = true")
    Iterable<T> findAll(Iterable<ID> iterable);

    @Override
    @Query("select count(e) from #{#entityName} e where e.professor = ?#{principal.professor} and e.enabled = true")
    long count();

    @Override
    @Transactional
    @Modifying
    @Query("update #{#entityName} e set e.enabled=false where e.id=?1 and e.professor = ?#{principal.professor}")
    void delete(Long id);

    @Override
    @Transactional
    @Modifying
    default void delete(T t){
        delete(t.getId());
    }

    @Override
    @Transactional
    @Modifying
    default void delete(Iterable<? extends T> iterable){
        iterable.forEach(entity -> delete(entity.getId()));
    }

    @Override
    @Transactional
    @Modifying
    @Query("update #{#entityName} e set e.enabled=false where e.professor = ?#{principal.professor}")
    void deleteAll();
}
</code>```

Doing this, all my basic queries will include automatically the name of the entity, the professor and if it is enabled. For example, my QuestionRepository is like this:

```<code>public interface QuestionRepository extends CustomPagingAndSortRepository<Question, Long> {
    @Query("select q from Question q where q.course.id = ?1 and q.title like %?2% and q.professor = ?#{principal.professor} and q.enabled = true")
    List<Question> listQuestionsByCourseAndTitle(long courseId, String title);
}
</code>```

Now I don't have to worry about creating a custom delete, or custom findALL or deleteAll etc. to include Professor and enabled for every single class that I create.

So, the answer to my own question: in my opinion, it is valid to add an association with a professor in all classes (in cases like mine, using JWT Authentication). I was able to reduce a lot of code and it is a 100% Spring solution.

PS: Didn't test findAll with Sort and Pageable yet.

Body:
I hope this question is not opinion based, but here we go.
I am developing a small API with spring boot, spring data jpa, and JWT Token as the authentication system. The API is pretty straightforward, is an exam generator, where you can register professor, course, question, choice etc.

For example:
[13/11/2017 - edited to include AbstractEntity]

@MappedSuperclass
public class AbstractEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;
    protected boolean enabled = true;
    //equals, hashcode...
}

public class Professor extends AbstractEntity{
    private String email;
}

public class Course extends AbstractEntity {
    @ManyToOne(optional = false)
    private Professor professor;
}

public class Question extends AbstractEntity {
    @ManyToOne(optional = false)
    private Course course;
}

Because the authentication system is JWT token for all queries I have to include the professor's information.

For example

public interface CourseRepository extends PagingAndSortingRepository<Course, Long> {
    @Query("select c from Course c where c.id = ?1 and c.professor = ?#{principal.professor} and c.enabled = true")
    Course findOne(Long id);
}

The problem is that for every crud query I will have to write my own custom query, because of professor's information and also the enabled field.

I know there is a way to extends the PagingAndSortingRepository and write your own custom query, like this:

public interface CustomPagingAndSortRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
    @Override
    @Query("select e from #{#entityName} e where e.id = ?1 and e.professor = ?#{principal.professor} and e.enabled = true")
    T findOne(ID id);
    //More fields overrided here
}

This CustomPagingAndSortRepository would work perfectly for all classes if I only had enabled to worry about. This class will work perfectly for Course because e.professor = ?#{principal.professor} will find the Professor there.  But it will not work for Question, for example, because I need a join with the course class to get the professor, like e.course.professor = ?#{principal.professor}

The point is f I put an association with Professor in all classes I can make that generic code work, save tons of code but sacrificing database design's normalization.
The question is: Is there another way? or maybe is it valid to do it?

PS: The Question, Course etc will never change owner, it means that only the Professor who created will be able to use it.

Body:
Ok, I will answer my own question. So, the question was:

The point is if I put an association with Professor in all classes I can make that generic code work, save tons of code but sacrificing database design's normalization. The question is: Is there another way? or maybe is it valid to do it?

And I actually did, based on this answer and it reduced a lot of code.

Now I have my model classes like this (Included Professor in all other classes too):

@MappedSuperclass
public class AbstractEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;
    @Column(columnDefinition = "boolean default true", nullable = false)
    private boolean enabled = true;
// ...
}

@Entity
public class Course extends AbstractEntity {
    private String name;
    @ManyToOne(optional = false)
    private Professor professor;
    //...
}

@Entity
public class Question extends AbstractEntity {
    private String title;
    @ManyToOne(optional = false)
    private Course course;
    @ManyToOne(optional = false)
    private Professor professor;
    // ...
}

And a CustomPagingAndSortRepository like this

@NoRepositoryBean
public interface CustomPagingAndSortRepository<T extends AbstractEntity, ID extends Long>
        extends PagingAndSortingRepository<T,ID> {
    @Override
    @Query("select e from #{#entityName} e where e.professor = ?#{principal.professor} and e.enabled = true")
    Iterable<T> findAll(Sort sort);

    @Override
    @Query("select e from #{#entityName} e where e.professor = ?#{principal.professor} and e.enabled = true")
    Page<T> findAll(Pageable pageable);

    @Override
    @Query("select e from #{#entityName} e where e.id =?1 and e.professor = ?#{principal.professor} and e.enabled = true")
    T findOne(Long id);

    @Override
    default boolean exists(Long id){
        return findOne(id) != null;
    }

    @Override
    @Query("select e from #{#entityName} e where e.professor = ?#{principal.professor} and e.enabled = true")
    Iterable<T> findAll();

    @Override
    @Query("select e from #{#entityName} e where e.professor = ?#{principal.professor} and e.enabled = true")
    Iterable<T> findAll(Iterable<ID> iterable);

    @Override
    @Query("select count(e) from #{#entityName} e where e.professor = ?#{principal.professor} and e.enabled = true")
    long count();

    @Override
    @Transactional
    @Modifying
    @Query("update #{#entityName} e set e.enabled=false where e.id=?1 and e.professor = ?#{principal.professor}")
    void delete(Long id);

    @Override
    @Transactional
    @Modifying
    default void delete(T t){
        delete(t.getId());
    }

    @Override
    @Transactional
    @Modifying
    default void delete(Iterable<? extends T> iterable){
        iterable.forEach(entity -> delete(entity.getId()));
    }

    @Override
    @Transactional
    @Modifying
    @Query("update #{#entityName} e set e.enabled=false where e.professor = ?#{principal.professor}")
    void deleteAll();
}

Doing this, all my basic queries will include automatically the name of the entity, the professor and if it is enabled. For example, my QuestionRepository is like this:

public interface QuestionRepository extends CustomPagingAndSortRepository<Question, Long> {
    @Query("select q from Question q where q.course.id = ?1 and q.title like %?2% and q.professor = ?#{principal.professor} and q.enabled = true")
    List<Question> listQuestionsByCourseAndTitle(long courseId, String title);
}

Now I don't have to worry about creating a custom delete, or custom findALL or deleteAll etc. to include Professor and enabled for every single class that I create.

So, the answer to my own question: in my opinion, it is valid to add an association with a professor in all classes (in cases like mine, using JWT Authentication). I was able to reduce a lot of code and it is a 100% Spring solution.

PS: Didn't test findAll with Sort and Pageable yet.


问:
Sending BlackMagic DeckLink Studio 4K over RTMP streams with FFmpeg
I'm trying to send a stream of video that's coming into a BlackMagic DeckLink Studio 4K capture card over a few different RTMP streams at once with FFmpeg. The command that I am doing it with is this:

ffmpeg -re -format_code Hi59 -f decklink -i 'DeckLink Studio 4K' -map 0 -flags +global_header -vcodec libx264 -crf 25 -preset medium -pix_fmt yuv422p -acodec aac -f tee "[f=flv]rtmp://ip1/live/test|[f=flv]rtmp://ip2/live/test.

However, whenever I send this video out, I just get color bars when looking at the stream. I tried using a different video source (the testsrc supplied by FFmpeg), and that sends out fine over RTMP to multiple stream destinations.

Is there something weird with how tee and the decklink stuff work in FFmpeg? Or is there an issue with my command?

答:
If you see colors bars, that means that ffmpeg is connecting to the card and streaming fine but the card is giving the bars. Your command says that ffmpeg is expecting 1920X1080@29.97 interlaced, make sure that is the format into the Decklink. You can also try explicitly setting the connection type, example:

```<code>ffmpeg -re -format_code Hi59 -video_input sdi -f decklink -i 'DeckLink Studio 4K' -map 0 -flags +global_header -vcodec libx264 -crf 25 -preset medium -pix_fmt yuv422p -acodec aac -f tee "[f=flv]rtmp://ip1/live/test|[f=flv]rtmp://ip2/live/test
</code>```

If you are still running into issues, make sure that the BlackMagic software can see the video signal, and it's the format you expect.

One last thing to check, if its an HDMI input make sure it is not HDCP; it's not supported.

Body:
If you see colors bars, that means that ffmpeg is connecting to the card and streaming fine but the card is giving the bars. Your command says that ffmpeg is expecting 1920X1080@29.97 interlaced, make sure that is the format into the Decklink. You can also try explicitly setting the connection type, example:

ffmpeg -re -format_code Hi59 -video_input sdi -f decklink -i 'DeckLink Studio 4K' -map 0 -flags +global_header -vcodec libx264 -crf 25 -preset medium -pix_fmt yuv422p -acodec aac -f tee "[f=flv]rtmp://ip1/live/test|[f=flv]rtmp://ip2/live/test

If you are still running into issues, make sure that the BlackMagic software can see the video signal, and it's the format you expect.

One last thing to check, if its an HDMI input make sure it is not HDCP; it's not supported.


问:
return list of words in dictionary.txt
I'm a beginner to Python. I am begging to learn about functions and files. I need to write a code that will return a list of all the words in dictionary.txt using a function, but I can't seem to figure it out.

Current code

```<code>def get_dictionary_wordlist(dic):
     dic = open("dictionary.txt", "r")
     for word in dic:
         return word
</code>```

答:
this should work:</p>

```<code>dictionary = ('Italy', 'pizza'), ('Germany', 'sauerkraut'), ('Spain', 'paella'), ('USA', 'Hamburger') # Opening the dictionary

def listWords(dic): # Creating the function
    y = dict(dic) # Making sure dic is a dic
    x = [] #creating the list
    for key in dic: # Iterating through every item in dic
        x.append(key) # Adding the item to the end
    return x #returning the results

print listWords(dictionary) #printing the results for the original text, dictionary
</code>```

Title: return list of words in dictionary.txt

Body:
I'm a beginner to Python. I am begging to learn about functions and files. I need to write a code that will return a list of all the words in dictionary.txt using a function, but I can't seem to figure it out.

Current code

def get_dictionary_wordlist(dic):
     dic = open("dictionary.txt", "r")
     for word in dic:
         return word

Body:
this should work:

dictionary = ('Italy', 'pizza'), ('Germany', 'sauerkraut'), ('Spain', 'paella'), ('USA', 'Hamburger') # Opening the dictionary

def listWords(dic): # Creating the function
    y = dict(dic) # Making sure dic is a dic
    x = [] #creating the list
    for key in dic: # Iterating through every item in dic
        x.append(key) # Adding the item to the end
    return x #returning the results

print listWords(dictionary) #printing the results for the original text, dictionary


问:
View Flip Issue
I have a single view controller controller controlling the flip of two views. This is something similar to Phone app in iPhone wherein when you make a call and tap on the keyboard item, the view flips.

Now when I am trying to flip the view on some selection, it flips correctly to show my second view but on second flip to show back my first screen, flip is not smooth and I can see my first view flipping, staying for a second, then second view goes away and then first view come up. This happens everytime.

Below is the code I am executing when tapping on the first view to flip to second view:

```<code> [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.75];

        [UIView setAnimationTransition:([self.mainView superview] ?
                                        UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
                               forView:self.containerView cache:YES];
[self.mainView removeFromSuperview];
[self loadRequestView];
        [UIView commitAnimations];
</code>```

Below is the code I am executing when tapping on the second view to flip to first view:

```<code>[UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.75];

    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                           forView:self.containerView cache:YES];

    [self.mainView removeFromSuperview];
    [self loadLandingView];
    [UIView commitAnimations];
</code>```

Both loadRequestView and loadLandingView are setting my self.mainView to appropriate view.

Any clue whats wrong here?

答: Since it looks like you are juggling a lot with your views, might it be that the caching is getting in the way? If you set cache:YES the animation will be made with a cached image of the original view and the new view only gets redrawn after the animation is finished. Try setting cache:NO </p>

Title: View Flip Issue

Body:
I have a single view controller controller controlling the flip of two views. This is something similar to Phone app in iPhone wherein when you make a call and tap on the keyboard item, the view flips.

Now when I am trying to flip the view on some selection, it flips correctly to show my second view but on second flip to show back my first screen, flip is not smooth and I can see my first view flipping, staying for a second, then second view goes away and then first view come up. This happens everytime.

Below is the code I am executing when tapping on the first view to flip to second view:

 [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.75];

        [UIView setAnimationTransition:([self.mainView superview] ?
                                        UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
                               forView:self.containerView cache:YES];
[self.mainView removeFromSuperview];
[self loadRequestView];
        [UIView commitAnimations];

Below is the code I am executing when tapping on the second view to flip to first view:

[UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.75];

    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                           forView:self.containerView cache:YES];

    [self.mainView removeFromSuperview];
    [self loadLandingView];
    [UIView commitAnimations];

Both loadRequestView and loadLandingView are setting my self.mainView to appropriate view.

Any clue whats wrong here?


问:
Can't resolve tree conflict with SVN
I recently encountered a very weird behavior of subversion.

I just merged my local copy of a branch with a remote branch. Everything went smooth, but I've got 1 tree conflict (local delete, remote update).

Okay, thought I, modified the working copy appropriately and ran "svn resolve --accept=working -R .".

Subversion told that it has resolved my problems and "svn st" no longer showed any issues. So, I tried to commit, but svn told me that one of the inner folders (inside my conflicted one) was out of date and suggested to svn up, BUT it made the folder to be in conflict again!

What shall I do to get out of this visious circle?

答:
This is what worked for me to abandon all local changes and go with the files from the server repository:

```<code>svn update --accept theirs-full

svn resolve --accept theirs-full <pathname>
</code>```

This message appears: W155027: Tree conflict can only be resolved to 'working'

Unintuitive next step but this actually cuts the catch-22:

```<code>svn resolve  --accept=working <pathname>
</code>```

NOW revert all the "working" changes recursively. This undid all my local changes.

```<code>svn revert -R .
</code>```

Back to normal, with no errors:

```<code>svn update
</code>```

Title: Can't resolve tree conflict with SVN

Body:
This is what worked for me to abandon all local changes and go with the files from the server repository:

svn update --accept theirs-full

svn resolve --accept theirs-full <pathname>

This message appears: W155027: Tree conflict can only be resolved to 'working'

Unintuitive next step but this actually cuts the catch-22:

svn resolve  --accept=working <pathname>

NOW revert all the "working" changes recursively. This undid all my local changes.

svn revert -R .

Back to normal, with no errors:

svn update


问:
one or the other in if statement
first we have this:

```<code>def some(a):
    if a is None or "*":
         print "*"
    else:
         print a

>>> some("*")
    *
>>> some("d")
    *
</code>```

it failed but I couldn't give myself a good enough reason why it failed.
Apparently it is because I wrote it wrong, a working version would be:

```<code>def some(a):
    if a in [None, "*"]: # or write [ if a is None or a is "*" ]
         print "*"
    else:
         print a

   >>> some("*")
       *
   >>> some("d")
       d
</code>```

Although it's almost intuitive enough to just write the correct version but I couldn't explain to myself why the first version failed.

so the question is, why the first version failed?

答:
This will always evaluate as True

```<code> if a is None or "*":
</code>```

You could have said this if you wanted

```<code> if a is None or a== "*":
</code>```

Title: one or the other in if statement

Body:
first we have this:

def some(a):
    if a is None or "*":
         print "*"
    else:
         print a

>>> some("*")
    *
>>> some("d")
    *

it failed but I couldn't give myself a good enough reason why it failed.
Apparently it is because I wrote it wrong, a working version would be:

def some(a):
    if a in [None, "*"]: # or write [ if a is None or a is "*" ]
         print "*"
    else:
         print a

   >>> some("*")
       *
   >>> some("d")
       d

Although it's almost intuitive enough to just write the correct version but I couldn't explain to myself why the first version failed.

so the question is, why the first version failed?

Body:
This will always evaluate as True

 if a is None or "*":

You could have said this if you wanted

 if a is None or a== "*":


答:
The following statement doesn't do what you're expecting:

```<code>if a is None or "*":
</code>```

This is equivalent to:

```<code>if (a is None) or "*":
</code>```

so it's always True.  To get the behavior you want, use:

```<code>if a is None or a == "*":
</code>```

Title: one or the other in if statement

Body:
The following statement doesn't do what you're expecting:

if a is None or "*":

This is equivalent to:

if (a is None) or "*":

so it's always True.  To get the behavior you want, use:

if a is None or a == "*":


问:
Can I replace a vectorized column with another vectorized column using "ifelse" in R?
I tried the below code:

```<code>Variable<-c("x","y")
Value<-c(0.25,0.9)
Count<-1
df<-data.frame(Variable, Value)
df$col1<-ifelse(Count == 0,df$Variable,df$Value)
</code>```

And I thought that df$col1 would be set to df$Value as the conditional is false. However, it outputs c(0.25, 0.25), which is incorrect.

Am I using ifelse incorrectly here? Thank you!

答: ifelse(test, yes, no) returns a value with the same shape as test, filled with values from yes or no depending on the conditional. So here ifelse returned only .25, the first value of Value.

Body:
I tried the below code:

Variable<-c("x","y")
Value<-c(0.25,0.9)
Count<-1
df<-data.frame(Variable, Value)
df$col1<-ifelse(Count == 0,df$Variable,df$Value)

And I thought that df$col1 would be set to df$Value as the conditional is false. However, it outputs c(0.25, 0.25), which is incorrect.

Am I using ifelse incorrectly here? Thank you!


答:
The statement:

```<code>if a is None or "*":
</code>```

Semantically means

```<code>if ((a is None) is True) or (the value "*" is True)
</code>```

And the value "*", being a non empty string is always True.  Thus, the whole expression will evaluate to True.

Title: one or the other in if statement

Body:
The statement:

if a is None or "*":

Semantically means

if ((a is None) is True) or (the value "*" is True)

And the value "*", being a non empty string is always True.  Thus, the whole expression will evaluate to True.


问:
How do I create a surface plot with matplotlib of a closed loop revolve about an axis given coordinate data of the 2D profile?

I have the closed loop stored as a two column by N row numpy array.

The last row of the array is the same as the first row, implying that it is, indeed, a closed loop.

The number of angular divisions in the rotation (as in, "slices of pie" so to speak) ought be set by a variable called 'angsteps'

The profile in question is plotted in the x-y coordinate plane, and is rotated about the 'x-axis'.

You can find the profile in question plotted here.  https://i.imgur.com/yJoKIEp.png

I apologize for the lack of code, but the profile data has so many interdependencies that I can't post the code that generates it without basically taking a shortcut to plugging the github page for it.

a downsampled version of the curve data looks like this.

```<code>    bulkmat =         [[  5.2          0.        ]
     [  0.381        0.        ]
     [  0.381        3.164     ]
     [  2.           3.164     ]
     [  2.           4.1       ]
     [  3.78         4.1       ]
     [  3.78         6.477     ]
     [  1.898        6.477     ]
     [  1.898        7.        ]
     [  3.18         7.        ]
     [  3.18         9.6       ]
     [  1.898        9.6       ]
     [  1.898        9.6       ]
     [  2.31987929  12.42620027]
     [  3.4801454   15.24663923]
     [  5.22074074  17.97407407]
     [  7.38360768  20.521262  ]
     [  9.81068861  22.80096022]
     [ 12.34392593  24.72592593]
     [ 14.825262    26.20891632]
     [ 17.09663923  27.16268861]
     [ 19.          27.5       ]
     [ 19.          27.5       ]
     [ 19.62962963  27.44718793]
     [ 20.18518519  27.29972565]
     [ 20.66666667  27.07407407]
     [ 21.07407407  26.7866941 ]
     [ 21.40740741  26.45404664]
     [ 21.66666667  26.09259259]
     [ 21.85185185  25.71879287]
     [ 21.96296296  25.34910837]
     [ 22.          25.        ]
     [ 22.          25.        ]
     [ 21.12125862  24.17043472]
     [ 18.91060645  23.59946824]
     [ 15.97201646  22.9218107 ]
     [ 12.84280513  21.85346069]
     [  9.96762011  20.14089993]
     [  7.67242798  17.51028807]
     [  6.13850192  13.61665735]
     [  5.37640942   7.99310742]
     [  5.2          0.        ]]
</code>```

答:
The following would be an example of a solid of revolution plotted around the z axis. As input we take some points and then create the necessary 2D arrays from them.

```<code>import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d

# input xy coordinates
xy = np.array([[1,0],[2,1],[2,2],[1,1.5],[1,0]])
# radial component is x values of input
r = xy[:,0]
# angular component is one revolution of 60 steps
phi = np.linspace(0, 2*np.pi, 60)
# create grid
R,Phi = np.meshgrid(r,phi)
# transform to cartesian coordinates
X = R*np.cos(Phi)
Y = R*np.sin(Phi)
# Z values are y values, repeated 60 times
Z = np.tile(xy[:,1],len(Y)).reshape(Y.shape)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax2 = fig.add_axes([0.05,0.7,0.15,.2])
ax2.plot(xy[:,0],xy[:,1], color="k")

ax.plot_surface(X, Y, Z, alpha=0.5, color='gold', rstride=1, cstride=1)

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

Body:
I have the closed loop stored as a two column by N row numpy array.

The last row of the array is the same as the first row, implying that it is, indeed, a closed loop.

The number of angular divisions in the rotation (as in, "slices of pie" so to speak) ought be set by a variable called 'angsteps'

The profile in question is plotted in the x-y coordinate plane, and is rotated about the 'x-axis'.

You can find the profile in question plotted here.  https://i.imgur.com/yJoKIEp.png

I apologize for the lack of code, but the profile data has so many interdependencies that I can't post the code that generates it without basically taking a shortcut to plugging the github page for it.

a downsampled version of the curve data looks like this.

    bulkmat =         [[  5.2          0.        ]
     [  0.381        0.        ]
     [  0.381        3.164     ]
     [  2.           3.164     ]
     [  2.           4.1       ]
     [  3.78         4.1       ]
     [  3.78         6.477     ]
     [  1.898        6.477     ]
     [  1.898        7.        ]
     [  3.18         7.        ]
     [  3.18         9.6       ]
     [  1.898        9.6       ]
     [  1.898        9.6       ]
     [  2.31987929  12.42620027]
     [  3.4801454   15.24663923]
     [  5.22074074  17.97407407]
     [  7.38360768  20.521262  ]
     [  9.81068861  22.80096022]
     [ 12.34392593  24.72592593]
     [ 14.825262    26.20891632]
     [ 17.09663923  27.16268861]
     [ 19.          27.5       ]
     [ 19.          27.5       ]
     [ 19.62962963  27.44718793]
     [ 20.18518519  27.29972565]
     [ 20.66666667  27.07407407]
     [ 21.07407407  26.7866941 ]
     [ 21.40740741  26.45404664]
     [ 21.66666667  26.09259259]
     [ 21.85185185  25.71879287]
     [ 21.96296296  25.34910837]
     [ 22.          25.        ]
     [ 22.          25.        ]
     [ 21.12125862  24.17043472]
     [ 18.91060645  23.59946824]
     [ 15.97201646  22.9218107 ]
     [ 12.84280513  21.85346069]
     [  9.96762011  20.14089993]
     [  7.67242798  17.51028807]
     [  6.13850192  13.61665735]
     [  5.37640942   7.99310742]
     [  5.2          0.        ]]

Body:
The following would be an example of a solid of revolution plotted around the z axis. As input we take some points and then create the necessary 2D arrays from them.

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d

# input xy coordinates
xy = np.array([[1,0],[2,1],[2,2],[1,1.5],[1,0]])
# radial component is x values of input
r = xy[:,0]
# angular component is one revolution of 60 steps
phi = np.linspace(0, 2*np.pi, 60)
# create grid
R,Phi = np.meshgrid(r,phi)
# transform to cartesian coordinates
X = R*np.cos(Phi)
Y = R*np.sin(Phi)
# Z values are y values, repeated 60 times
Z = np.tile(xy[:,1],len(Y)).reshape(Y.shape)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax2 = fig.add_axes([0.05,0.7,0.15,.2])
ax2.plot(xy[:,0],xy[:,1], color="k")

ax.plot_surface(X, Y, Z, alpha=0.5, color='gold', rstride=1, cstride=1)

plt.show()


问:
how can i reduce the execution time for this code
the problem is that the user enters N,1<=N<=1000000,then N lines and a Q number 1<=Q<=1000000 then Q lines each contains two space separated a,b numbers.For each  line i need to print total of numbers from i entred from line a to b.
for example i enter:

```<code>5     // N
1     // First line
2     // Second line ... to fifth
3
4
5
3    // Q
0 4  // the two numbers a, b... Q times
1 3
2 2
</code>```

the output will be

```<code>15 // from 0 to 4 : 1+2+3+4+5=15
9  // from 1 to 3 : 2+3+4=9
3  // from 2 to 2 : 3
</code>```

my code takes more than 5 second to execute if Q and N are big 100000

```<code>#include <bits/stdc++.h>
#include<vector>
#include<numeric>
#include<functional>
using namespace std;

int main() {
int a,b,c,p1,p2,sum(0);
vector<int>t(0);
cin>>a;
for(int i(0);i<a;++i)
{
    cin>>b;
    t.push_back(b);
}
cin>>c;
for(int i(0);i<c;++i)
{
    sum=0;
    cin>>p1>>p2;
    sum=accumulate(t.begin()+p1,t.begin()+p2+1,0);
    cout<<sum<<endl;
}
return 0;
}
</code>```

答:
Rather than saving the numbers in an array and computing the totals each time, compute a running sum and store that sum in the array.

For example, given the numbers [1, 2, 3, 4, 5], your t vector will contain [1, 3, 6, 10, 15]. You can easily modify your code to build that:

```<code>int sum = 0;
t.push_back(sum);  // see discussion below
for(int i(0);i<a;++i)
{
    cin>>b;
    sum += b;
    t.push_back(sum);
}
</code>```

Then, when asked for a sum, it's a simple matter of subtracting the value that comes before the starting index from the number at the ending index. For example:

```<code>1,3 : result = t[3] - t[0]
2,2 : result = t[2] - t[1]
</code>```

If the starting index is 0, then you don't subtract anything at all. The reason I pushed a 0 sum to the vector first is to handle the case when p1 = 0. Otherwise I'd have to special-case that. (Thanks to @max66 for the suggestion.)

Your compute loop becomes:

```<code>for(int i(0);i<c;++i)
{
    cin>>p1>>p2;
    sum=t[p2+1] - t[p1];
    cout<<sum<<endl;
}
</code>```

Body:
the problem is that the user enters N,1<=N<=1000000,then N lines and a Q number 1<=Q<=1000000 then Q lines each contains two space separated a,b numbers.For each  line i need to print total of numbers from i entred from line a to b.
for example i enter:

5     // N
1     // First line
2     // Second line ... to fifth
3
4
5
3    // Q
0 4  // the two numbers a, b... Q times
1 3
2 2

the output will be

15 // from 0 to 4 : 1+2+3+4+5=15
9  // from 1 to 3 : 2+3+4=9
3  // from 2 to 2 : 3

my code takes more than 5 second to execute if Q and N are big 100000

#include <bits/stdc++.h>
#include<vector>
#include<numeric>
#include<functional>
using namespace std;

int main() {
int a,b,c,p1,p2,sum(0);
vector<int>t(0);
cin>>a;
for(int i(0);i>b;
    t.push_back(b);
}
cin>>c;
for(int i(0);i<c;++i)
{
    sum=0;
    cin>>p1>>p2;
    sum=accumulate(t.begin()+p1,t.begin()+p2+1,0);
    cout<<sum<<endl;
}
return 0;
}

Body:
Rather than saving the numbers in an array and computing the totals each time, compute a running sum and store that sum in the array.

For example, given the numbers [1, 2, 3, 4, 5], your t vector will contain [1, 3, 6, 10, 15]. You can easily modify your code to build that:

int sum = 0;
t.push_back(sum);  // see discussion below
for(int i(0);i<a;++i)
{
    cin>>b;
    sum += b;
    t.push_back(sum);
}

Then, when asked for a sum, it's a simple matter of subtracting the value that comes before the starting index from the number at the ending index. For example:

1,3 : result = t[3] - t[0]
2,2 : result = t[2] - t[1]

If the starting index is 0, then you don't subtract anything at all. The reason I pushed a 0 sum to the vector first is to handle the case when p1 = 0. Otherwise I'd have to special-case that. (Thanks to @max66 for the suggestion.)

Your compute loop becomes:

for(int i(0);i<c;++i)
{
    cin>>p1>>p2;
    sum=t[p2+1] - t[p1];
    cout<<sum<<endl;
}


问:
which is better "for(int i = 0; i != 5; ++i)" or "for(int i = 0; i <= 5; i++)"?
which is better for(int i = 0; i != 5; ++i) or for(int i = 0; i <= 5; i++)?

Please explain the rationale, if possible.

I read somewhere that != operator is better than comparison operators. also pre-increment operator is better than post-increment operator, since it doesn't require any temporary variable to store the intermediate value.

Is there any better form of for loop than these two?

p.s: I use the former one as suggested by one of the sources, which i don't remember now.

答:
Using "i < 5" would be the form most people would expect to see on a for loop based on common usage. There is nothing wrong of course with "i != 5" or "i <= 5" (expect they'll give different loop counts) but if I see either of those forms I have to stop and think because it's not the most common form.

So I would always write "i < 5" unless there was a strong reason to do otherwise


问:
Scikitlearn LinearSVC Bad input shape
I'm trying to use LinearSVC on my data! My code below:

```<code>from sklearn import svm

clf2 = svm.LinearSVC()
clf2.fit(X_train, y_train)
</code>```

Results in the following error:

```<code>ValueError: bad input shape (2190, 9)
</code>```

I've used one-hot encoding on my y value before splitting into y_test and y_train, and believe this to be the issue. I've tried implementing similar fixes (sklearn (Bad Input Shape) ValueError) but still get errors when I try and re-shape.

After one hot-encoding, I have a target variable (y) that has 9 classes, and there are a total of 2190 samples i'm running. It seems I need to reduce these 9 classes to 1 class in order to fit the SVM.

Any suggestions would be greatly appreciated!

答:
You need to reshape the arrays. Here is an example using random data and as target variable a variable that contains 5 classes:

```<code>import numpy as np
from sklearn import svm

# 100 samples and 10 features
x = np.random.rand(100, 10)

#5 classes
y = [1,2,3,4,5] * 20

x = np.asarray(x)
y = np.asarray(y)

print(x.shape)
print(y.shape)

clf2 = svm.LinearSVC()
clf2.fit(x, y)
</code>```

Results:

```<code>(100, 10)

(100,)

LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
     intercept_scaling=1, loss='squared_hinge', max_iter=1000,
     multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
     verbose=0)
</code>```

Title: Scikitlearn LinearSVC Bad input shape

Body:
I'm trying to use LinearSVC on my data! My code below:

from sklearn import svm

clf2 = svm.LinearSVC()
clf2.fit(X_train, y_train)

Results in the following error:

ValueError: bad input shape (2190, 9)

I've used one-hot encoding on my y value before splitting into y_test and y_train, and believe this to be the issue. I've tried implementing similar fixes (sklearn (Bad Input Shape) ValueError) but still get errors when I try and re-shape.

After one hot-encoding, I have a target variable (y) that has 9 classes, and there are a total of 2190 samples i'm running. It seems I need to reduce these 9 classes to 1 class in order to fit the SVM.

Any suggestions would be greatly appreciated!

Body:
You need to reshape the arrays. Here is an example using random data and as target variable a variable that contains 5 classes:

import numpy as np
from sklearn import svm

# 100 samples and 10 features
x = np.random.rand(100, 10)

#5 classes
y = [1,2,3,4,5] * 20

x = np.asarray(x)
y = np.asarray(y)

print(x.shape)
print(y.shape)

clf2 = svm.LinearSVC()
clf2.fit(x, y)

Results:

(100, 10)

(100,)

LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
     intercept_scaling=1, loss='squared_hinge', max_iter=1000,
     multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
     verbose=0)


问:
Requirements for Keyword Argument Unpacking
What is required to support keyword argument unpacking for a custom class in Python? In both the Python 2.7 and Python 3.6 interpreters the following error message is given when trying to unpack an incompatible type:

```<code>>>> dict(**None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type object argument after ** must be a mapping, not NoneType
</code>```

Does mapping mean the object has to specifically subclass collections.Mapping? Or is mapping in this case one of those pseudo types such as iterable where you don't have to explicitly subclass collections.Iterable to support iter()? Does this behavior differ between Python 2 and 3?

答:
A Mapping is a generic container for associating key/value pairs.

It is not compulsory to inherit from collections.Mapping in order to use dictionary unpacking. Besides, collections.Mapping is an abstract class, so you'll still have to override some methods in order to be able to use dict unpacking with any of its subclasses.

For an instance of a class to be dict unpackable, it is enough for the class to implement a keys method and a corresponding __getitem__ method to return a value given a key:

```<code>class D(object):
  def __getitem__(self, key):
      return 2

  def keys(self):
      return ['1','2','3']

print(dict(**D()))
# {'1': 2, '2': 2, '3': 2}
</code>```

Body:
What is required to support keyword argument unpacking for a custom class in Python? In both the Python 2.7 and Python 3.6 interpreters the following error message is given when trying to unpack an incompatible type:

>>> dict(**None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type object argument after ** must be a mapping, not NoneType

Does mapping mean the object has to specifically subclass collections.Mapping? Or is mapping in this case one of those pseudo types such as iterable where you don't have to explicitly subclass collections.Iterable to support iter()? Does this behavior differ between Python 2 and 3?

Body:
A Mapping is a generic container for associating key/value pairs.

It is not compulsory to inherit from collections.Mapping in order to use dictionary unpacking. Besides, collections.Mapping is an abstract class, so you'll still have to override some methods in order to be able to use dict unpacking with any of its subclasses.

For an instance of a class to be dict unpackable, it is enough for the class to implement a keys method and a corresponding __getitem__ method to return a value given a key:

class D(object):
  def __getitem__(self, key):
      return 2

  def keys(self):
      return ['1','2','3']

print(dict(**D()))
# {'1': 2, '2': 2, '3': 2}


问:
PostgreSQL: Copy/paste resulting with headers into Excel without code
I used MS SQL Server 2008 R2 (MS SQL) where I could right click the query result, copy/paste it with headers to Excel for easy exploration. Now with PG Admin (PostgreSQL) I have to do export (File > Export > CSV) then bunch of Excel steps (Text To Columns).

Is there an easy way to copy/paste the query result with headers into Excel?

答: You have to set your query tool output to text not the grid data.  That way the Column names and the query results are all in the same cut past text file.  When you do this you are no longer doing CSV.  The whole results and field names comes over as a text file in the cut and paste process.</p>

ContentLicense: CC BY-SA 4.0


问:
how to put in varable.tf to run script with data external in terraform
This is the continutaion of the following contents i asked before.

I want to identify the public ip of the terraform execution environment and add it to the security group

My terraform direcotry structure is as follows.

```<code>root/
　├ main.tf
　├ test.sh
　├ modules/aws
　│　         └ ssh.tf/
　│　         └ variables.tf/
</code>```

test.sh

```<code>#!/bin/sh -xe

echo {\"ip\":\""`curl ipinfo.io/ip`"\"}
</code>```

main.tf

```<code>module "aws" {

  source = "./modules/aws"

}
</code>```

variables.tf

```<code>variable ssh_ip_address {
  default     = "xxx.xx.xx.xx"
}
</code>```

ssh.tf

```<code>resource "aws_security_group" "ssh" {
  name        = "${var.name}-ssh"
  description = "Allow connection by ssh"
  vpc_id      = "${aws_vpc.main.id}"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["${var.ssh_ip_address}/32"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "${var.name}",
  }
}
</code>```

I load the .tf file under the module/aws directory using the module option in main.tf

and I would like to run my local script, identify the public IP  that ran terraform and use the result in the ssh.tf file.

I'm using data external option to run my local script.

```<code>data "external" "example" {
  program = ["sh", "test.sh" ]
}
</code>```

but, in data verification of myself,
data external option is not executed properly except main.tf.

When running data external in main.tf and use it in ssh.tf,
we got error following that such value does not exist.

```<code>output 'commandout': unknown resource 'data.external.example' referenced in variable data.external.example.result
</code>```

My configuration file when I faced an error is as follows.

main.tf

```<code>data "external" "example" {
  program = ["sh", "test.sh" ]
}

module "aws" {
 source = "./modules/aws"
}
</code>```

ssh.tf

```<code>output "commandout" {
  value = "${data.external.example.result}"
}

resource "aws_security_group" "ssh" {
      name        = "${var.name}-ssh"
      description = "Allow connection by ssh"
      vpc_id      = "${aws_vpc.main.id}"

      ingress {
        from_port   = 22
        to_port     = 22
        protocol    = "tcp"
        cidr_blocks = ["${data.external.external.result}/32"]
      }

      egress {
        from_port   = 0
        to_port     = 0
        protocol    = "-1"
        cidr_blocks = ["0.0.0.0/0"]
      }

      tags = {
        Name = "${var.name}",
      }
    }
</code>```

is there a way to  run most rapidly   tf.file in terraform?

If there is such a way, i would like to use data external option to execute it first, and use it in ssh.tf

or I would like to put the result of data external execution into a certain value  of var.tf

Please let me know if there are other ways to solve this problem.
Thank you for reading my question.

答:
There are a few changes you need to make for this to work correctly.  The test.sh script needs to only output json format as a result, but as it is right now (or when I was running it) I get output from the curl command as well.  I had to wrap the curl command and ignore the output, and then use a variable to complete the json like so:

test.sh

```<code>#!/bin/sh

{
  IP_ADDRESS=$(curl ipinfo.io/ip)
} &> /dev/null

echo {\"ip\":\""$IP_ADDRESS"\"}
</code>```

Now when you are calling your aws module in the main.tf we will want to set the ssh_ip_address variable to equal the "ip" output of the test.sh script result.

Also, I noticed that in your ssh.tf file you are trying to reference the "${data.external.example.result}" inside the aws module, but since that data is defined outside of the module it won't work.  I suggest creating an addition variable of type "map" in the variables.tf which you can then set in the main.tf; in my example I have added the "command_output" variable.

Here you can see I added the command_output variable:

variable.tf

```<code>variable ssh_ip_address {
  default     = "xxx.xx.xx.xx"
}

variable "command_output" {
  type = "map"
}

variable name {
  default = "test"
}
</code>```

Here is what the main.tf not looks like (notice that we are setting the variables defined in the aws module):

main.tf

```<code>data "external" "example" {
  program = ["sh", "test.sh" ]
}

module "aws" {
  source = "./modules/aws"

  ssh_ip_address = "${data.external.example.result.ip}"

  command_output = "${data.external.example.result}"
}
</code>```

Lastly, here is the ssh.tf file:

ssh.tf

```<code>resource "aws_security_group" "ssh" {
  name        = "${var.name}-ssh"
  description = "Allow connection by ssh"
  vpc_id      = "${aws_vpc.main.id}"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["${var.ssh_ip_address}/32"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "${var.name}",
  }
}

output "commandout" {
  value = "${var.command_output}"
}
</code>```

Hope this helps!

Body:
This is the continutaion of the following contents i asked before.

I want to identify the public ip of the terraform execution environment and add it to the security group

My terraform direcotry structure is as follows.

root/
　├ main.tf
　├ test.sh
　├ modules/aws
　│　         └ ssh.tf/
　│　         └ variables.tf/

test.sh

#!/bin/sh -xe

echo {\"ip\":\""`curl ipinfo.io/ip`"\"}

main.tf

module "aws" {

  source = "./modules/aws"

}

variables.tf

variable ssh_ip_address {
  default     = "xxx.xx.xx.xx"
}

ssh.tf

resource "aws_security_group" "ssh" {
  name        = "${var.name}-ssh"
  description = "Allow connection by ssh"
  vpc_id      = "${aws_vpc.main.id}"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["${var.ssh_ip_address}/32"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "${var.name}",
  }
}

I load the .tf file under the module/aws directory using the module option in main.tf

and I would like to run my local script, identify the public IP  that ran terraform and use the result in the ssh.tf file.

I'm using data external option to run my local script.

data "external" "example" {
  program = ["sh", "test.sh" ]
}

but, in data verification of myself,
data external option is not executed properly except main.tf.

When running data external in main.tf and use it in ssh.tf,
we got error following that such value does not exist.

output 'commandout': unknown resource 'data.external.example' referenced in variable data.external.example.result

My configuration file when I faced an error is as follows.

main.tf

data "external" "example" {
  program = ["sh", "test.sh" ]
}

module "aws" {
 source = "./modules/aws"
}

ssh.tf

output "commandout" {
  value = "${data.external.example.result}"
}

resource "aws_security_group" "ssh" {
      name        = "${var.name}-ssh"
      description = "Allow connection by ssh"
      vpc_id      = "${aws_vpc.main.id}"

      ingress {
        from_port   = 22
        to_port     = 22
        protocol    = "tcp"
        cidr_blocks = ["${data.external.external.result}/32"]
      }

      egress {
        from_port   = 0
        to_port     = 0
        protocol    = "-1"
        cidr_blocks = ["0.0.0.0/0"]
      }

      tags = {
        Name = "${var.name}",
      }
    }

is there a way to  run most rapidly   tf.file in terraform?

If there is such a way, i would like to use data external option to execute it first, and use it in ssh.tf

or I would like to put the result of data external execution into a certain value  of var.tf

Please let me know if there are other ways to solve this problem.
Thank you for reading my question.

Body:
There are a few changes you need to make for this to work correctly.  The test.sh script needs to only output json format as a result, but as it is right now (or when I was running it) I get output from the curl command as well.  I had to wrap the curl command and ignore the output, and then use a variable to complete the json like so:

test.sh

#!/bin/sh

{
  IP_ADDRESS=$(curl ipinfo.io/ip)
} &> /dev/null

echo {\"ip\":\""$IP_ADDRESS"\"}

Now when you are calling your aws module in the main.tf we will want to set the ssh_ip_address variable to equal the "ip" output of the test.sh script result.

Also, I noticed that in your ssh.tf file you are trying to reference the "${data.external.example.result}" inside the aws module, but since that data is defined outside of the module it won't work.  I suggest creating an addition variable of type "map" in the variables.tf which you can then set in the main.tf; in my example I have added the "command_output" variable.

Here you can see I added the command_output variable:

variable.tf

variable ssh_ip_address {
  default     = "xxx.xx.xx.xx"
}

variable "command_output" {
  type = "map"
}

variable name {
  default = "test"
}

Here is what the main.tf not looks like (notice that we are setting the variables defined in the aws module):

main.tf

data "external" "example" {
  program = ["sh", "test.sh" ]
}

module "aws" {
  source = "./modules/aws"

  ssh_ip_address = "${data.external.example.result.ip}"

  command_output = "${data.external.example.result}"
}

Lastly, here is the ssh.tf file:

ssh.tf

resource "aws_security_group" "ssh" {
  name        = "${var.name}-ssh"
  description = "Allow connection by ssh"
  vpc_id      = "${aws_vpc.main.id}"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["${var.ssh_ip_address}/32"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "${var.name}",
  }
}

output "commandout" {
  value = "${var.command_output}"
}

Hope this helps!


问:
Why automatically omits repeated values when making the conversion?
I have the following dictionary :

```<code>dict_1={'AB':50,'ACC':20,'CEKK':75,'AB':25,'CEKK':75,'BBA':58,'BBA':58 }
</code>```

When I turn it into a df:

```<code>pd.DataFrame(data=list(dict_1.values()),index=list(dict_1.keys()),columns=['number'])
</code>```

It only provides each key from the dictionary once.

```<code>            number
 ACC         20
  AB         25
CEKK         75
 BBA         58
</code>```

The desired output would be the df with all the values including repeated ones.

```<code>            number
 ACC         20
  AB         25
CEKK         75
 BBA         58
  AB         50
CEKK         75
 BBA         58
</code>```

Is there a feasible way I could make the dataframe to include the repeated values without stating the values for the data and index explicitely?

答:
Python dictionary eliminates all duplicates in keys before it gets to Pandas.

```<code>In [466]: dict_1={'AB':50,'ACC':20,'CEKK':75,'AB':25,'CEKK':75,'BBA':58,'BBA':58 }

In [467]: dict_1
Out[467]: {'AB': 25, 'ACC': 20, 'BBA': 58, 'CEKK': 75}
</code>```

Workaround:

```<code>In [465]: pd.DataFrame({'number':[20, 25, 75, 58, 50, 75, 58]},
                       index=['ACC', 'AB', 'CEKK', 'BBA', 'AB', 'CEKK', 'BBA'])
Out[465]:
      number
ACC       20
AB        25
CEKK      75
BBA       58
AB        50
CEKK      75
BBA       58
</code>```

UPDATE: that's what is happening to the dictionary with duplicated keys:

```<code>In [79]: print({'AB':50,'ACC':20,'CEKK':75,'AB':25,'CEKK':75,'BBA':58,'BBA':58 })
{'AB': 25, 'ACC': 20, 'CEKK': 75, 'BBA': 58}
</code>```

exactly the same will happen when you pass it to pd.DataFrame() constructor. So pd.DataFrame() will receive a dictionary without duplicates.

Body:
I have the following dictionary :

dict_1={'AB':50,'ACC':20,'CEKK':75,'AB':25,'CEKK':75,'BBA':58,'BBA':58 }

When I turn it into a df:

pd.DataFrame(data=list(dict_1.values()),index=list(dict_1.keys()),columns=['number'])

It only provides each key from the dictionary once.

            number
 ACC         20
  AB         25
CEKK         75
 BBA         58

The desired output would be the df with all the values including repeated ones.

            number
 ACC         20
  AB         25
CEKK         75
 BBA         58
  AB         50
CEKK         75
 BBA         58

Is there a feasible way I could make the dataframe to include the repeated values without stating the values for the data and index explicitely?

Body:
Python dictionary eliminates all duplicates in keys before it gets to Pandas.

In [466]: dict_1={'AB':50,'ACC':20,'CEKK':75,'AB':25,'CEKK':75,'BBA':58,'BBA':58 }

In [467]: dict_1
Out[467]: {'AB': 25, 'ACC': 20, 'BBA': 58, 'CEKK': 75}

Workaround:

In [465]: pd.DataFrame({'number':[20, 25, 75, 58, 50, 75, 58]},
                       index=['ACC', 'AB', 'CEKK', 'BBA', 'AB', 'CEKK', 'BBA'])
Out[465]:
      number
ACC       20
AB        25
CEKK      75
BBA       58
AB        50
CEKK      75
BBA       58

UPDATE: that's what is happening to the dictionary with duplicated keys:

In [79]: print({'AB':50,'ACC':20,'CEKK':75,'AB':25,'CEKK':75,'BBA':58,'BBA':58 })
{'AB': 25, 'ACC': 20, 'CEKK': 75, 'BBA': 58}

exactly the same will happen when you pass it to pd.DataFrame() constructor. So pd.DataFrame() will receive a dictionary without duplicates.