问:
WEBP support not installed error with Pillow included in Anaconda
I have written a small code to open webp image in the Anaconda prompt.

from PIL import Image
im = Image.open('test.webp')
```

<p>It causes the following error:</p>

```C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py:2655: UserWarning: image file could not be identified because WEBP support not installed
  warnings.warn(message)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 2657, in open
    % (filename if filename else fp))
OSError: cannot identify image file 'test.webp'
```

<p>The same code works fine in python-3.7.2-amd64 installed in another directory with Pillow 5.4.1.  </p>

```Package    Version
---------- -------
Pillow     5.4.1
pip        18.1
setuptools 40.6.2
```

<p>The above code causes the same error in Anaconda prompt, Spyder and Jupyter Notebook.  Still the same error even after reinstallation the Anaconda or trying on the different PC.</p>

<p>Anaconda or Miniconda version:</p>

<p>Anaconda 2018.12 for Windows Installer<br>
Python 3.7 version 64-Bit Graphical Installer<br>
Anaconda3-2018.12-Windows-x86_64.exe<br></p>

<p>Operating System:</p>

<p>Windows 10 Professional 64-Bit</p>

<p>The same issue was reported on GitHub #10737.</p>

<code>conda info</code>

<p></p>

```     active environment : base
    active env location : C:\ProgramData\Anaconda3
            shell level : 1
       user config file : C:\Users\chen\.condarc
 populated config files : C:\Users\chen\.condarc
          conda version : 4.5.12
    conda-build version : 3.17.6
         python version : 3.7.1.final.0
       base environment : C:\ProgramData\Anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/win-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/pro/win-64
                          https://repo.anaconda.com/pkgs/pro/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : C:\ProgramData\Anaconda3\pkgs
                          C:\Users\chen\AppData\Local\conda\conda\pkgs
       envs directories : C:\ProgramData\Anaconda3\envs
                          C:\Users\chen\AppData\Local\conda\conda\envs
                          C:\Users\chen\.conda\envs
               platform : win-64
             user-agent : conda/4.5.12 requests/2.21.0 CPython/3.7.1 Windows/10 Windows/10.0.17763
          administrator : True
             netrc file : None
           offline mode : False
```

<p></p>

<code>conda list --show-channel-urls</code>

<p></p>

```# packages in environment at C:\ProgramData\Anaconda3:
#
# Name                    Version                   Build  Channel
_ipyw_jlab_nb_ext_conf    0.1.0                    py37_0    defaults
alabaster                 0.7.12                   py37_0    defaults
anaconda                  2018.12                  py37_0    defaults
anaconda-client           1.7.2                    py37_0    defaults
anaconda-navigator        1.9.6                    py37_0    defaults
anaconda-project          0.8.2                    py37_0    defaults
asn1crypto                0.24.0                   py37_0    defaults
astroid                   2.1.0                    py37_0    defaults
astropy                   3.1              py37he774522_0    defaults
atomicwrites              1.2.1                    py37_0    defaults
attrs                     18.2.0           py37h28b3542_0    defaults
babel                     2.6.0                    py37_0    defaults
backcall                  0.1.0                    py37_0    defaults
backports                 1.0                      py37_1    defaults
backports.os              0.1.1                    py37_0    defaults
backports.shutil_get_terminal_size 1.0.0                    py37_2    defaults
beautifulsoup4            4.6.3                    py37_0    defaults
bitarray                  0.8.3            py37hfa6e2cd_0    defaults
bkcharts                  0.2                      py37_0    defaults
blas                      1.0                         mkl    defaults
blaze                     0.11.3                   py37_0    defaults
bleach                    3.0.2                    py37_0    defaults
blosc                     1.14.4               he51fdeb_0    defaults
bokeh                     1.0.2                    py37_0    defaults
boto                      2.49.0                   py37_0    defaults
bottleneck                1.2.1            py37h452e1ab_1    defaults
bzip2                     1.0.6                hfa6e2cd_5    defaults
ca-certificates           2018.03.07                    0    defaults
certifi                   2018.11.29               py37_0    defaults
cffi                      1.11.5           py37h74b6da3_1    defaults
chardet                   3.0.4                    py37_1    defaults
click                     7.0                      py37_0    defaults
cloudpickle               0.6.1                    py37_0    defaults
clyent                    1.2.2                    py37_1    defaults
colorama                  0.4.1                    py37_0    defaults
comtypes                  1.1.7                    py37_0    defaults
conda                     4.5.12                   py37_0    defaults
conda-build               3.17.6                   py37_0    defaults
conda-env                 2.6.0                         1    defaults
conda-verify              3.1.1                    py37_0    defaults
console_shortcut          0.1.1                         3    defaults
contextlib2               0.5.5                    py37_0    defaults
cryptography              2.4.2            py37h7a1dbc1_0    defaults
curl                      7.63.0            h2a8f88b_1000    defaults
cycler                    0.10.0                   py37_0    defaults
cython                    0.29.2           py37ha925a31_0    defaults
cytoolz                   0.9.0.1          py37hfa6e2cd_1    defaults
dask                      1.0.0                    py37_0    defaults
dask-core                 1.0.0                    py37_0    defaults
datashape                 0.5.4                    py37_1    defaults
decorator                 4.3.0                    py37_0    defaults
defusedxml                0.5.0                    py37_1    defaults
distributed               1.25.1                   py37_0    defaults
docutils                  0.14                     py37_0    defaults
entrypoints               0.2.3                    py37_2    defaults
et_xmlfile                1.0.1                    py37_0    defaults
fastcache                 1.0.2            py37hfa6e2cd_2    defaults
filelock                  3.0.10                   py37_0    defaults
flask                     1.0.2                    py37_1    defaults
flask-cors                3.0.7                    py37_0    defaults
freetype                  2.9.1                ha9979f8_1    defaults
future                    0.17.1                   py37_0    defaults
get_terminal_size         1.0.0                h38e98db_0    defaults
gevent                    1.3.7            py37he774522_1    defaults
glob2                     0.6                      py37_1    defaults
greenlet                  0.4.15           py37hfa6e2cd_0    defaults
h5py                      2.8.0            py37h3bdd7fb_2    defaults
hdf5                      1.10.2               hac2f561_1    defaults
heapdict                  1.0.0                    py37_2    defaults
html5lib                  1.0.1                    py37_0    defaults
icc_rt                    2019.0.0             h0cc432a_1    defaults
icu                       58.2                 ha66f8fd_1    defaults
idna                      2.8                      py37_0    defaults
imageio                   2.4.1                    py37_0    defaults
imagesize                 1.1.0                    py37_0    defaults
importlib_metadata        0.6                      py37_0    defaults
intel-openmp              2019.1                      144    defaults
ipykernel                 5.1.0            py37h39e3cac_0    defaults
ipython                   7.2.0            py37h39e3cac_0    defaults
ipython_genutils          0.2.0                    py37_0    defaults
ipywidgets                7.4.2                    py37_0    defaults
isort                     4.3.4                    py37_0    defaults
itsdangerous              1.1.0                    py37_0    defaults
jdcal                     1.4                      py37_0    defaults
jedi                      0.13.2                   py37_0    defaults
jinja2                    2.10                     py37_0    defaults
jpeg                      9b                   hb83a4c4_2    defaults
jsonschema                2.6.0                    py37_0    defaults
jupyter                   1.0.0                    py37_7    defaults
jupyter_client            5.2.4                    py37_0    defaults
jupyter_console           6.0.0                    py37_0    defaults
jupyter_core              4.4.0                    py37_0    defaults
jupyterlab                0.35.3                   py37_0    defaults
jupyterlab_server         0.2.0                    py37_0    defaults
keyring                   17.0.0                   py37_0    defaults
kiwisolver                1.0.1            py37h6538335_0    defaults
krb5                      1.16.1               hc04afaa_7    defaults
lazy-object-proxy         1.3.1            py37hfa6e2cd_2    defaults
libarchive                3.3.3                h0643e63_5    defaults
libcurl                   7.63.0            h2a8f88b_1000    defaults
libiconv                  1.15                 h1df5818_7    defaults
libpng                    1.6.35               h2a8f88b_0    defaults
libsodium                 1.0.16               h9d3ae62_0    defaults
libssh2                   1.8.0                h7a1dbc1_4    defaults
libtiff                   4.0.9                h36446d0_2    defaults
libxml2                   2.9.8                hadb2253_1    defaults
libxslt                   1.1.32               hf6f1972_0    defaults
llvmlite                  0.26.0           py37ha925a31_0    defaults
locket                    0.2.0                    py37_1    defaults
lxml                      4.2.5            py37hef2cd61_0    defaults
lz4-c                     1.8.1.2              h2fa13f4_0    defaults
lzo                       2.10                 h6df0209_2    defaults
m2w64-gcc-libgfortran     5.3.0                         6    defaults
m2w64-gcc-libs            5.3.0                         7    defaults
m2w64-gcc-libs-core       5.3.0                         7    defaults
m2w64-gmp                 6.1.0                         2    defaults
m2w64-libwinpthread-git   5.0.0.4634.697f757               2    defaults
markupsafe                1.1.0            py37he774522_0    defaults
matplotlib                3.0.2            py37hc8f65d3_0    defaults
mccabe                    0.6.1                    py37_1    defaults
menuinst                  1.4.14           py37hfa6e2cd_0    defaults
mistune                   0.8.4            py37he774522_0    defaults
mkl                       2019.1                      144    defaults
mkl-service               1.1.2            py37hb782905_5    defaults
mkl_fft                   1.0.6            py37h6288b17_0    defaults
mkl_random                1.0.2            py37h343c172_0    defaults
more-itertools            4.3.0                    py37_0    defaults
mpmath                    1.1.0                    py37_0    defaults
msgpack-python            0.5.6            py37he980bc4_1    defaults
msys2-conda-epoch         20160418                      1    defaults
multipledispatch          0.6.0                    py37_0    defaults
navigator-updater         0.2.1                    py37_0    defaults
nbconvert                 5.4.0                    py37_1    defaults
nbformat                  4.4.0                    py37_0    defaults
networkx                  2.2                      py37_1    defaults
nltk                      3.4                      py37_1    defaults
nose                      1.3.7                    py37_2    defaults
notebook                  5.7.4                    py37_0    defaults
numba                     0.41.0           py37hf9181ef_0    defaults
numexpr                   2.6.8            py37hdce8814_0    defaults
numpy                     1.15.4           py37h19fb1c0_0    defaults
numpy-base                1.15.4           py37hc3f5095_0    defaults
numpydoc                  0.8.0                    py37_0    defaults
odo                       0.5.1                    py37_0    defaults
olefile                   0.46                     py37_0    defaults
openpyxl                  2.5.12                   py37_0    defaults
openssl                   1.1.1a               he774522_0    defaults
packaging                 18.0                     py37_0    defaults
pandas                    0.23.4           py37h830ac7b_0    defaults
pandoc                    1.19.2.1             hb2460c7_1    defaults
pandocfilters             1.4.2                    py37_1    defaults
parso                     0.3.1                    py37_0    defaults
partd                     0.3.9                    py37_0    defaults
path.py                   11.5.0                   py37_0    defaults
pathlib2                  2.3.3                    py37_0    defaults
patsy                     0.5.1                    py37_0    defaults
pep8                      1.7.1                    py37_0    defaults
pickleshare               0.7.5                    py37_0    defaults
pillow                    5.3.0            py37hdc69c19_0    defaults
pip                       18.1                     py37_0    defaults
pkginfo                   1.4.2                    py37_1    defaults
pluggy                    0.8.0                    py37_0    defaults
ply                       3.11                     py37_0    defaults
prometheus_client         0.5.0                    py37_0    defaults
prompt_toolkit            2.0.7                    py37_0    defaults
psutil                    5.4.8            py37he774522_0    defaults
py                        1.7.0                    py37_0    defaults
pycodestyle               2.4.0                    py37_0    defaults
pycosat                   0.6.3            py37hfa6e2cd_0    defaults
pycparser                 2.19                     py37_0    defaults
pycrypto                  2.6.1            py37hfa6e2cd_9    defaults
pycurl                    7.43.0.2         py37h7a1dbc1_0    defaults
pyflakes                  2.0.0                    py37_0    defaults
pygments                  2.3.1                    py37_0    defaults
pylint                    2.2.2                    py37_0    defaults
pyodbc                    4.0.25           py37ha925a31_0    defaults
pyopenssl                 18.0.0                   py37_0    defaults
pyparsing                 2.3.0                    py37_0    defaults
pyqt                      5.9.2            py37h6538335_2    defaults
pysocks                   1.6.8                    py37_0    defaults
pytables                  3.4.4            py37he6f6034_0    defaults
pytest                    4.0.2                    py37_0    defaults
pytest-arraydiff          0.3              py37h39e3cac_0    defaults
pytest-astropy            0.5.0                    py37_0    defaults
pytest-doctestplus        0.2.0                    py37_0    defaults
pytest-openfiles          0.3.1                    py37_0    defaults
pytest-remotedata         0.3.1                    py37_0    defaults
python                    3.7.1                h8c8aaf0_6    defaults
python-dateutil           2.7.5                    py37_0    defaults
python-libarchive-c       2.8                      py37_6    defaults
pytz                      2018.7                   py37_0    defaults
pywavelets                1.0.1            py37h8c2d366_0    defaults
pywin32                   223              py37hfa6e2cd_1    defaults
pywinpty                  0.5.5                 py37_1000    defaults
pyyaml                    3.13             py37hfa6e2cd_0    defaults
pyzmq                     17.1.2           py37hfa6e2cd_0    defaults
qt                        5.9.7            vc14h73c81de_0  [vc14]  defaults
qtawesome                 0.5.3                    py37_0    defaults
qtconsole                 4.4.3                    py37_0    defaults
qtpy                      1.5.2                    py37_0    defaults
requests                  2.21.0                   py37_0    defaults
rope                      0.11.0                   py37_0    defaults
ruamel_yaml               0.15.46          py37hfa6e2cd_0    defaults
scikit-image              0.14.1           py37ha925a31_0    defaults
scikit-learn              0.20.1           py37h343c172_0    defaults
scipy                     1.1.0            py37h29ff71c_2    defaults
seaborn                   0.9.0                    py37_0    defaults
send2trash                1.5.0                    py37_0    defaults
setuptools                40.6.3                   py37_0    defaults
simplegeneric             0.8.1                    py37_2    defaults
singledispatch            3.4.0.3                  py37_0    defaults
sip                       4.19.8           py37h6538335_0    defaults
six                       1.12.0                   py37_0    defaults
snappy                    1.1.7                h777316e_3    defaults
snowballstemmer           1.2.1                    py37_0    defaults
sortedcollections         1.0.1                    py37_0    defaults
sortedcontainers          2.1.0                    py37_0    defaults
sphinx                    1.8.2                    py37_0    defaults
sphinxcontrib             1.0                      py37_1    defaults
sphinxcontrib-websupport  1.1.0                    py37_1    defaults
spyder                    3.3.2                    py37_0    defaults
spyder-kernels            0.3.0                    py37_0    defaults
sqlalchemy                1.2.15           py37he774522_0    defaults
sqlite                    3.26.0               he774522_0    defaults
statsmodels               0.9.0            py37h452e1ab_0    defaults
sympy                     1.3                      py37_0    defaults
tblib                     1.3.2                    py37_0    defaults
terminado                 0.8.1                    py37_1    defaults
testpath                  0.4.2                    py37_0    defaults
tk                        8.6.8                hfa6e2cd_0    defaults
toolz                     0.9.0                    py37_0    defaults
tornado                   5.1.1            py37hfa6e2cd_0    defaults
tqdm                      4.28.1           py37h28b3542_0    defaults
traitlets                 4.3.2                    py37_0    defaults
unicodecsv                0.14.1                   py37_0    defaults
urllib3                   1.24.1                   py37_0    defaults
vc                        14.1                 h0510ff6_4    defaults
vs2015_runtime            14.15.26706          h3a45250_0    defaults
wcwidth                   0.1.7                    py37_0    defaults
webencodings              0.5.1                    py37_1    defaults
werkzeug                  0.14.1                   py37_0    defaults
wheel                     0.32.3                   py37_0    defaults
widgetsnbextension        3.4.2                    py37_0    defaults
win_inet_pton             1.0.1                    py37_1    defaults
win_unicode_console       0.5                      py37_0    defaults
wincertstore              0.2                      py37_0    defaults
winpty                    0.4.3                         4    defaults
wrapt                     1.10.11          py37hfa6e2cd_2    defaults
xlrd                      1.2.0                    py37_0    defaults
xlsxwriter                1.1.2                    py37_0    defaults
xlwings                   0.15.1                   py37_0    defaults
xlwt                      1.3.0                    py37_0    defaults
xz                        5.2.4                h2fa13f4_4    defaults
yaml                      0.1.7                hc54c509_2    defaults
zeromq                    4.2.5                he025d50_1    defaults
zict                      0.1.3                    py37_0    defaults
zlib                      1.2.11               h62dcd97_3    defaults
zstd                      1.3.7                h508b16e_0    defaults
```

答:
I have finally come up with this workaround, which assumes that dwebp from the libwebp library is available on the system:

import os
import subprocess
from PIL import Image, UnidentifiedImageError
from io import BytesIO

class dwebpException(Exception):
    pass

def dwebp(file: str):
    webp = subprocess.run(
        f"dwebp {file} -quiet -o -", shell=True, capture_output=True
    )
    if webp.returncode != 0:
        raise dwebpException(webp.stderr.decode())
    else:
        return Image.open(BytesIO(webp.stdout))

filename = 'test.webp'

try:
    img = Image.open(filename)
except UnidentifiedImageError:
    if os.path.splitext(filename)[1].lower() == ".webp":
        img = dwebp(filename)
    else:
        raise
```

Body:
I have written a small code to open webp image in the Anaconda prompt.

from PIL import Image
im = Image.open('test.webp')

It causes the following error:

C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py:2655: UserWarning: image file could not be identified because WEBP support not installed
  warnings.warn(message)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 2657, in open
    % (filename if filename else fp))
OSError: cannot identify image file 'test.webp'

The same code works fine in python-3.7.2-amd64 installed in another directory with Pillow 5.4.1.

Package    Version
---------- -------
Pillow     5.4.1
pip        18.1
setuptools 40.6.2

The above code causes the same error in Anaconda prompt, Spyder and Jupyter Notebook.  Still the same error even after reinstallation the Anaconda or trying on the different PC.

Anaconda or Miniconda version:

Anaconda 2018.12 for Windows Installer

Python 3.7 version 64-Bit Graphical Installer

Anaconda3-2018.12-Windows-x86_64.exe

Operating System:

Windows 10 Professional 64-Bit

The same issue was reported on GitHub #10737.

conda info

     active environment : base
    active env location : C:\ProgramData\Anaconda3
            shell level : 1
       user config file : C:\Users\chen\.condarc
 populated config files : C:\Users\chen\.condarc
          conda version : 4.5.12
    conda-build version : 3.17.6
         python version : 3.7.1.final.0
       base environment : C:\ProgramData\Anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/win-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/pro/win-64
                          https://repo.anaconda.com/pkgs/pro/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : C:\ProgramData\Anaconda3\pkgs
                          C:\Users\chen\AppData\Local\conda\conda\pkgs
       envs directories : C:\ProgramData\Anaconda3\envs
                          C:\Users\chen\AppData\Local\conda\conda\envs
                          C:\Users\chen\.conda\envs
               platform : win-64
             user-agent : conda/4.5.12 requests/2.21.0 CPython/3.7.1 Windows/10 Windows/10.0.17763
          administrator : True
             netrc file : None
           offline mode : False

conda list --show-channel-urls

# packages in environment at C:\ProgramData\Anaconda3:
#
# Name                    Version                   Build  Channel
_ipyw_jlab_nb_ext_conf    0.1.0                    py37_0    defaults
alabaster                 0.7.12                   py37_0    defaults
anaconda                  2018.12                  py37_0    defaults
anaconda-client           1.7.2                    py37_0    defaults
anaconda-navigator        1.9.6                    py37_0    defaults
anaconda-project          0.8.2                    py37_0    defaults
asn1crypto                0.24.0                   py37_0    defaults
astroid                   2.1.0                    py37_0    defaults
astropy                   3.1              py37he774522_0    defaults
atomicwrites              1.2.1                    py37_0    defaults
attrs                     18.2.0           py37h28b3542_0    defaults
babel                     2.6.0                    py37_0    defaults
backcall                  0.1.0                    py37_0    defaults
backports                 1.0                      py37_1    defaults
backports.os              0.1.1                    py37_0    defaults
backports.shutil_get_terminal_size 1.0.0                    py37_2    defaults
beautifulsoup4            4.6.3                    py37_0    defaults
bitarray                  0.8.3            py37hfa6e2cd_0    defaults
bkcharts                  0.2                      py37_0    defaults
blas                      1.0                         mkl    defaults
blaze                     0.11.3                   py37_0    defaults
bleach                    3.0.2                    py37_0    defaults
blosc                     1.14.4               he51fdeb_0    defaults
bokeh                     1.0.2                    py37_0    defaults
boto                      2.49.0                   py37_0    defaults
bottleneck                1.2.1            py37h452e1ab_1    defaults
bzip2                     1.0.6                hfa6e2cd_5    defaults
ca-certificates           2018.03.07                    0    defaults
certifi                   2018.11.29               py37_0    defaults
cffi                      1.11.5           py37h74b6da3_1    defaults
chardet                   3.0.4                    py37_1    defaults
click                     7.0                      py37_0    defaults
cloudpickle               0.6.1                    py37_0    defaults
clyent                    1.2.2                    py37_1    defaults
colorama                  0.4.1                    py37_0    defaults
comtypes                  1.1.7                    py37_0    defaults
conda                     4.5.12                   py37_0    defaults
conda-build               3.17.6                   py37_0    defaults
conda-env                 2.6.0                         1    defaults
conda-verify              3.1.1                    py37_0    defaults
console_shortcut          0.1.1                         3    defaults
contextlib2               0.5.5                    py37_0    defaults
cryptography              2.4.2            py37h7a1dbc1_0    defaults
curl                      7.63.0            h2a8f88b_1000    defaults
cycler                    0.10.0                   py37_0    defaults
cython                    0.29.2           py37ha925a31_0    defaults
cytoolz                   0.9.0.1          py37hfa6e2cd_1    defaults
dask                      1.0.0                    py37_0    defaults
dask-core                 1.0.0                    py37_0    defaults
datashape                 0.5.4                    py37_1    defaults
decorator                 4.3.0                    py37_0    defaults
defusedxml                0.5.0                    py37_1    defaults
distributed               1.25.1                   py37_0    defaults
docutils                  0.14                     py37_0    defaults
entrypoints               0.2.3                    py37_2    defaults
et_xmlfile                1.0.1                    py37_0    defaults
fastcache                 1.0.2            py37hfa6e2cd_2    defaults
filelock                  3.0.10                   py37_0    defaults
flask                     1.0.2                    py37_1    defaults
flask-cors                3.0.7                    py37_0    defaults
freetype                  2.9.1                ha9979f8_1    defaults
future                    0.17.1                   py37_0    defaults
get_terminal_size         1.0.0                h38e98db_0    defaults
gevent                    1.3.7            py37he774522_1    defaults
glob2                     0.6                      py37_1    defaults
greenlet                  0.4.15           py37hfa6e2cd_0    defaults
h5py                      2.8.0            py37h3bdd7fb_2    defaults
hdf5                      1.10.2               hac2f561_1    defaults
heapdict                  1.0.0                    py37_2    defaults
html5lib                  1.0.1                    py37_0    defaults
icc_rt                    2019.0.0             h0cc432a_1    defaults
icu                       58.2                 ha66f8fd_1    defaults
idna                      2.8                      py37_0    defaults
imageio                   2.4.1                    py37_0    defaults
imagesize                 1.1.0                    py37_0    defaults
importlib_metadata        0.6                      py37_0    defaults
intel-openmp              2019.1                      144    defaults
ipykernel                 5.1.0            py37h39e3cac_0    defaults
ipython                   7.2.0            py37h39e3cac_0    defaults
ipython_genutils          0.2.0                    py37_0    defaults
ipywidgets                7.4.2                    py37_0    defaults
isort                     4.3.4                    py37_0    defaults
itsdangerous              1.1.0                    py37_0    defaults
jdcal                     1.4                      py37_0    defaults
jedi                      0.13.2                   py37_0    defaults
jinja2                    2.10                     py37_0    defaults
jpeg                      9b                   hb83a4c4_2    defaults
jsonschema                2.6.0                    py37_0    defaults
jupyter                   1.0.0                    py37_7    defaults
jupyter_client            5.2.4                    py37_0    defaults
jupyter_console           6.0.0                    py37_0    defaults
jupyter_core              4.4.0                    py37_0    defaults
jupyterlab                0.35.3                   py37_0    defaults
jupyterlab_server         0.2.0                    py37_0    defaults
keyring                   17.0.0                   py37_0    defaults
kiwisolver                1.0.1            py37h6538335_0    defaults
krb5                      1.16.1               hc04afaa_7    defaults
lazy-object-proxy         1.3.1            py37hfa6e2cd_2    defaults
libarchive                3.3.3                h0643e63_5    defaults
libcurl                   7.63.0            h2a8f88b_1000    defaults
libiconv                  1.15                 h1df5818_7    defaults
libpng                    1.6.35               h2a8f88b_0    defaults
libsodium                 1.0.16               h9d3ae62_0    defaults
libssh2                   1.8.0                h7a1dbc1_4    defaults
libtiff                   4.0.9                h36446d0_2    defaults
libxml2                   2.9.8                hadb2253_1    defaults
libxslt                   1.1.32               hf6f1972_0    defaults
llvmlite                  0.26.0           py37ha925a31_0    defaults
locket                    0.2.0                    py37_1    defaults
lxml                      4.2.5            py37hef2cd61_0    defaults
lz4-c                     1.8.1.2              h2fa13f4_0    defaults
lzo                       2.10                 h6df0209_2    defaults
m2w64-gcc-libgfortran     5.3.0                         6    defaults
m2w64-gcc-libs            5.3.0                         7    defaults
m2w64-gcc-libs-core       5.3.0                         7    defaults
m2w64-gmp                 6.1.0                         2    defaults
m2w64-libwinpthread-git   5.0.0.4634.697f757               2    defaults
markupsafe                1.1.0            py37he774522_0    defaults
matplotlib                3.0.2            py37hc8f65d3_0    defaults
mccabe                    0.6.1                    py37_1    defaults
menuinst                  1.4.14           py37hfa6e2cd_0    defaults
mistune                   0.8.4            py37he774522_0    defaults
mkl                       2019.1                      144    defaults
mkl-service               1.1.2            py37hb782905_5    defaults
mkl_fft                   1.0.6            py37h6288b17_0    defaults
mkl_random                1.0.2            py37h343c172_0    defaults
more-itertools            4.3.0                    py37_0    defaults
mpmath                    1.1.0                    py37_0    defaults
msgpack-python            0.5.6            py37he980bc4_1    defaults
msys2-conda-epoch         20160418                      1    defaults
multipledispatch          0.6.0                    py37_0    defaults
navigator-updater         0.2.1                    py37_0    defaults
nbconvert                 5.4.0                    py37_1    defaults
nbformat                  4.4.0                    py37_0    defaults
networkx                  2.2                      py37_1    defaults
nltk                      3.4                      py37_1    defaults
nose                      1.3.7                    py37_2    defaults
notebook                  5.7.4                    py37_0    defaults
numba                     0.41.0           py37hf9181ef_0    defaults
numexpr                   2.6.8            py37hdce8814_0    defaults
numpy                     1.15.4           py37h19fb1c0_0    defaults
numpy-base                1.15.4           py37hc3f5095_0    defaults
numpydoc                  0.8.0                    py37_0    defaults
odo                       0.5.1                    py37_0    defaults
olefile                   0.46                     py37_0    defaults
openpyxl                  2.5.12                   py37_0    defaults
openssl                   1.1.1a               he774522_0    defaults
packaging                 18.0                     py37_0    defaults
pandas                    0.23.4           py37h830ac7b_0    defaults
pandoc                    1.19.2.1             hb2460c7_1    defaults
pandocfilters             1.4.2                    py37_1    defaults
parso                     0.3.1                    py37_0    defaults
partd                     0.3.9                    py37_0    defaults
path.py                   11.5.0                   py37_0    defaults
pathlib2                  2.3.3                    py37_0    defaults
patsy                     0.5.1                    py37_0    defaults
pep8                      1.7.1                    py37_0    defaults
pickleshare               0.7.5                    py37_0    defaults
pillow                    5.3.0            py37hdc69c19_0    defaults
pip                       18.1                     py37_0    defaults
pkginfo                   1.4.2                    py37_1    defaults
pluggy                    0.8.0                    py37_0    defaults
ply                       3.11                     py37_0    defaults
prometheus_client         0.5.0                    py37_0    defaults
prompt_toolkit            2.0.7                    py37_0    defaults
psutil                    5.4.8            py37he774522_0    defaults
py                        1.7.0                    py37_0    defaults
pycodestyle               2.4.0                    py37_0    defaults
pycosat                   0.6.3            py37hfa6e2cd_0    defaults
pycparser                 2.19                     py37_0    defaults
pycrypto                  2.6.1            py37hfa6e2cd_9    defaults
pycurl                    7.43.0.2         py37h7a1dbc1_0    defaults
pyflakes                  2.0.0                    py37_0    defaults
pygments                  2.3.1                    py37_0    defaults
pylint                    2.2.2                    py37_0    defaults
pyodbc                    4.0.25           py37ha925a31_0    defaults
pyopenssl                 18.0.0                   py37_0    defaults
pyparsing                 2.3.0                    py37_0    defaults
pyqt                      5.9.2            py37h6538335_2    defaults
pysocks                   1.6.8                    py37_0    defaults
pytables                  3.4.4            py37he6f6034_0    defaults
pytest                    4.0.2                    py37_0    defaults
pytest-arraydiff          0.3              py37h39e3cac_0    defaults
pytest-astropy            0.5.0                    py37_0    defaults
pytest-doctestplus        0.2.0                    py37_0    defaults
pytest-openfiles          0.3.1                    py37_0    defaults
pytest-remotedata         0.3.1                    py37_0    defaults
python                    3.7.1                h8c8aaf0_6    defaults
python-dateutil           2.7.5                    py37_0    defaults
python-libarchive-c       2.8                      py37_6    defaults
pytz                      2018.7                   py37_0    defaults
pywavelets                1.0.1            py37h8c2d366_0    defaults
pywin32                   223              py37hfa6e2cd_1    defaults
pywinpty                  0.5.5                 py37_1000    defaults
pyyaml                    3.13             py37hfa6e2cd_0    defaults
pyzmq                     17.1.2           py37hfa6e2cd_0    defaults
qt                        5.9.7            vc14h73c81de_0  [vc14]  defaults
qtawesome                 0.5.3                    py37_0    defaults
qtconsole                 4.4.3                    py37_0    defaults
qtpy                      1.5.2                    py37_0    defaults
requests                  2.21.0                   py37_0    defaults
rope                      0.11.0                   py37_0    defaults
ruamel_yaml               0.15.46          py37hfa6e2cd_0    defaults
scikit-image              0.14.1           py37ha925a31_0    defaults
scikit-learn              0.20.1           py37h343c172_0    defaults
scipy                     1.1.0            py37h29ff71c_2    defaults
seaborn                   0.9.0                    py37_0    defaults
send2trash                1.5.0                    py37_0    defaults
setuptools                40.6.3                   py37_0    defaults
simplegeneric             0.8.1                    py37_2    defaults
singledispatch            3.4.0.3                  py37_0    defaults
sip                       4.19.8           py37h6538335_0    defaults
six                       1.12.0                   py37_0    defaults
snappy                    1.1.7                h777316e_3    defaults
snowballstemmer           1.2.1                    py37_0    defaults
sortedcollections         1.0.1                    py37_0    defaults
sortedcontainers          2.1.0                    py37_0    defaults
sphinx                    1.8.2                    py37_0    defaults
sphinxcontrib             1.0                      py37_1    defaults
sphinxcontrib-websupport  1.1.0                    py37_1    defaults
spyder                    3.3.2                    py37_0    defaults
spyder-kernels            0.3.0                    py37_0    defaults
sqlalchemy                1.2.15           py37he774522_0    defaults
sqlite                    3.26.0               he774522_0    defaults
statsmodels               0.9.0            py37h452e1ab_0    defaults
sympy                     1.3                      py37_0    defaults
tblib                     1.3.2                    py37_0    defaults
terminado                 0.8.1                    py37_1    defaults
testpath                  0.4.2                    py37_0    defaults
tk                        8.6.8                hfa6e2cd_0    defaults
toolz                     0.9.0                    py37_0    defaults
tornado                   5.1.1            py37hfa6e2cd_0    defaults
tqdm                      4.28.1           py37h28b3542_0    defaults
traitlets                 4.3.2                    py37_0    defaults
unicodecsv                0.14.1                   py37_0    defaults
urllib3                   1.24.1                   py37_0    defaults
vc                        14.1                 h0510ff6_4    defaults
vs2015_runtime            14.15.26706          h3a45250_0    defaults
wcwidth                   0.1.7                    py37_0    defaults
webencodings              0.5.1                    py37_1    defaults
werkzeug                  0.14.1                   py37_0    defaults
wheel                     0.32.3                   py37_0    defaults
widgetsnbextension        3.4.2                    py37_0    defaults
win_inet_pton             1.0.1                    py37_1    defaults
win_unicode_console       0.5                      py37_0    defaults
wincertstore              0.2                      py37_0    defaults
winpty                    0.4.3                         4    defaults
wrapt                     1.10.11          py37hfa6e2cd_2    defaults
xlrd                      1.2.0                    py37_0    defaults
xlsxwriter                1.1.2                    py37_0    defaults
xlwings                   0.15.1                   py37_0    defaults
xlwt                      1.3.0                    py37_0    defaults
xz                        5.2.4                h2fa13f4_4    defaults
yaml                      0.1.7                hc54c509_2    defaults
zeromq                    4.2.5                he025d50_1    defaults
zict                      0.1.3                    py37_0    defaults
zlib                      1.2.11               h62dcd97_3    defaults
zstd                      1.3.7                h508b16e_0    defaults


来源: stackexchange

问:
Silverlight datagrid twoway binding
I have datagrid in which one of the column is Textbox control, have set the binding as twoway. the grid is binded to a observable collection.
Issue : If I enter some value in text box, collection get populated. now if I clears that textbox the same has not been reflected to the collection. my binded property is nullable.

Thanks N Advance

答:
I'm not sure where your problem might be exactly, but I've tried the following and it works as expected:

I have a simple Person class:

```<code>public class Person : ObservableObject
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            if (name == value)
                return;
            name = value;
            RaisePropertyChanged(() => Name);
        }
    }

    private int? age;
    public int? Age
    {
        get { return age; }
        set
        {
            if (age == value)
                return;
            age = value;
            RaisePropertyChanged(() => Age);
        }
    }
}
</code>```

I also have a simple ViewModel with an ObservableCollection<Person> called Data. Ans, here's my XAML:

```<code><sdk:DataGrid Name="dataGrid" ItemsSource="{Binding Data}" AutoGenerateColumns="False"
              SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" Grid.Row="1">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn Binding="{Binding Name, Mode=TwoWay}"/>
        <sdk:DataGridTextColumn Binding="{Binding Age, Mode=TwoWay}"/>
    </sdk:DataGrid.Columns>
</sdk:DataGrid>
</code>```

I checked how it's working by setting a breakpoint in the Setter for my Age property, and it shows the value comming in as null

Hope this helps ;)

ContentLicense: CC BY-SA 3.0

Title: Silverlight datagrid twoway binding

Body:
I'm not sure where your problem might be exactly, but I've tried the following and it works as expected:

I have a simple Person class:

public class Person : ObservableObject
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            if (name == value)
                return;
            name = value;
            RaisePropertyChanged(() => Name);
        }
    }

    private int? age;
    public int? Age
    {
        get { return age; }
        set
        {
            if (age == value)
                return;
            age = value;
            RaisePropertyChanged(() => Age);
        }
    }
}

I also have a simple ViewModel with an ObservableCollection<Person> called Data. Ans, here's my XAML:

<sdk:DataGrid Name="dataGrid" ItemsSource="{Binding Data}" AutoGenerateColumns="False"
              SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" Grid.Row="1">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn Binding="{Binding Name, Mode=TwoWay}"/>
        <sdk:DataGridTextColumn Binding="{Binding Age, Mode=TwoWay}"/>
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

I checked how it's working by setting a breakpoint in the Setter for my Age property, and it shows the value comming in as null

Hope this helps ;)


问:
Implementing ResNet50 on 56x56x1 Input
I am trying to implement ResNet50 with an input layer of shape (56,56,1) with 10 output classifications.

I instantiate the model as so

```<code>model = tf.keras.applications.resnet50.ResNet50(
    weights = None, #random
    input_shape = ( 56, 56, 1 ),
    classes = 10
)
</code>```

I train the model, and feed it one of the training examples to see what it does. It seems like the output shape is not what I am expecting

```<code>a = model.predict(x_test_preprocess[15288])
print(a.shape)
</code>```

(56, 10)

Doing a bit of poking, it looks like the output layer shape makes sense

```<code>print(len(model.layers))
print(model.layers[176].input_shape)
print(model.layers[176].output_shape)
</code>```

177

(None, 2048)

(None, 10)

So if the output_shape of the last layer is (None,10), why am I getting a tensor shape of (56, 10) for my prediction?

Am I handling color channels wrong? Am I allowed to simply give it a single channel input shape as I have or do I need to duplicate the greyscale over the RGB channels and build it with a (56,56,3) shape?

答:
The issue here is input is not same as the input_shape. Model expects input to be (None, 56, 56, 1) but when you try to put x_test_preprocess[15288], your input shape will become (56, 56, 1). When this happens keras automaticaly try to fit network with what it got, which will not produce corret results. You can do the following to fix this issue:

a = model.predict(x_test_preprocess[15288].reshape([-1,56,56,1])
```

ContentLicense: CC BY-SA 4.0

Title: Implementing ResNet50 on 56x56x1 Input

Body:
I am trying to implement ResNet50 with an input layer of shape (56,56,1) with 10 output classifications.

I instantiate the model as so

model = tf.keras.applications.resnet50.ResNet50(
    weights = None, #random
    input_shape = ( 56, 56, 1 ),
    classes = 10
)

I train the model, and feed it one of the training examples to see what it does. It seems like the output shape is not what I am expecting

a = model.predict(x_test_preprocess[15288])
print(a.shape)

(56, 10)

Doing a bit of poking, it looks like the output layer shape makes sense

print(len(model.layers))
print(model.layers[176].input_shape)
print(model.layers[176].output_shape)

177

(None, 2048)

(None, 10)

So if the output_shape of the last layer is (None,10), why am I getting a tensor shape of (56, 10) for my prediction?

Am I handling color channels wrong? Am I allowed to simply give it a single channel input shape as I have or do I need to duplicate the greyscale over the RGB channels and build it with a (56,56,3) shape?


问:
Getting "SyntaxError: Unexpected token" in Observable
I was trying vega-lite-api on observablehq here.

This is my code:

Putting these two in different cells work:

```<code>obj = vl.markPoint()
  .data(df)
  .encode(vl.y().fieldN('city'));
</code>```

In next cell:

```<code>obj.render()
</code>```

If I put both in the same cell, it gives SyntaxError: Unexpected token:

```<code>obj2 = vl.markPoint()
  .data(df)
  .encode(vl.y().fieldN('city'));

obj2.render();
^
</code>```

Why is this so?

答:
If you want to put the code into the same cell, you can either chain the call to render like so:

vl.markPoint()
  .data(df)
  .encode(vl.y().fieldN('city'))
  .render()
```
<p>Or you can use a block cell with curly braces like so:</p>
<pre class="lang-js prettyprint-override"><code>{
  const obj = vl.markPoint()
    .data(df)
    .encode(vl.y().fieldN('city'));

  return obj.render();
}
</code>```

For more on Observable JavaScript:

https://observablehq.com/@observablehq/observables-not-javascript

Body:
I was trying vega-lite-api on observablehq here.

This is my code:

Putting these two in different cells work:

obj = vl.markPoint()
  .data(df)
  .encode(vl.y().fieldN('city'));

In next cell:

obj.render()

If I put both in the same cell, it gives SyntaxError: Unexpected token:

obj2 = vl.markPoint()
  .data(df)
  .encode(vl.y().fieldN('city'));

obj2.render();
^

Why is this so?

Body:
If you want to put the code into the same cell, you can either chain the call to render like so:

vl.markPoint()
  .data(df)
  .encode(vl.y().fieldN('city'))
  .render()

Or you can use a block cell with curly braces like so:

{
  const obj = vl.markPoint()
    .data(df)
    .encode(vl.y().fieldN('city'));

  return obj.render();
}

For more on Observable JavaScript:

https://observablehq.com/@observablehq/observables-not-javascript


问:
CORS issue on Localhost, MULTIPLE workarounds have failed
I am in tears as I type this, sobbing into my keyboard

I have wasted over a week trying to understand what is wrong. I can no longer learn or develop. I am going to have to give up learning to code if I cannot fix this.

I understand what CORS is. If I had the ability to edit the API server, I would, but I do not. I must use a 3rd party API. I do not have the time to develop my own to work with. I am UNABLE to develop using anything but a local server. I need to stop CORS issues.

I have been trying to fix this problem for days. I am at a complete standstill with my learning. A week of tears and frustratiion has ben spent on this.

I CANNOT:

use https://cors-anywhere.herokuapp.com. This fails and CORS still screams:

Access to XMLHttpRequest at 'https://cors-anywhere.herokuapp.com/https://randomuser.me/api/?page=1&results=10' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

Develop non-locally

Proxy. Package.json just ignores it. I have made multiple attempts to use proxying to avoid CORS. All attempts have ended in complete failure.

I legitimately do not know what to do. This is my code. Please help me, I am in tears and I cannot learn React anymore until this is fixed.

import axios from "axios";

let getFriends = () => {
  const config = {
    method: "GET",
    url: "https://cors-anywhere.herokuapp.com/https://randomuser.me/api/?page=1&results=10",
    crossdomain: true,
    headers: {
      "Content-Type": "application/json"
    },
  };
  return axios(config);
};

export { getFriends };```

答:
The reason you are going crazy trying to find a solution is because there is nothing YOU can do about CORS. Changing any header as the other solution recommends won't work, the server will still flag any origins which it does not allow calls from!

This is a value controlled by the SERVER whose API you are calling. And, as the error says, the server you are calling does not accept connections from localhost:3000.

As long as you are making requests from a browser that is on localhost:3000 to this API, you'll be hit with CORS issues.

If you want to ensure that the API itself is not broken, use a different tool like Postman to call to the API. This is like a 'server to server' call, and as such does not require CORS checks.

EDIT: The following call to the random user API seems to work:

```<code>import React, { useEffect } from 'react';

function App() {

  const fetchRandomUsers = async () => {
    const response = await fetch("https://randomuser.me/api/?page=1&results=10")
    const json = await response.json()
    json.results.forEach((result) => {
      console.log(result)
    })
  }

  // On mount, fetch random users
  useEffect(() => {
    fetchRandomUsers()
  }, [])

  return (
    <></>
  );
}

export default App;

</code>```

Body:
The reason you are going crazy trying to find a solution is because there is nothing YOU can do about CORS. Changing any header as the other solution recommends won't work, the server will still flag any origins which it does not allow calls from!

This is a value controlled by the SERVER whose API you are calling. And, as the error says, the server you are calling does not accept connections from localhost:3000.

As long as you are making requests from a browser that is on localhost:3000 to this API, you'll be hit with CORS issues.

If you want to ensure that the API itself is not broken, use a different tool like Postman to call to the API. This is like a 'server to server' call, and as such does not require CORS checks.

EDIT: The following call to the random user API seems to work:

import React, { useEffect } from 'react';

function App() {

  const fetchRandomUsers = async () => {
    const response = await fetch("https://randomuser.me/api/?page=1&results=10")
    const json = await response.json()
    json.results.forEach((result) => {
      console.log(result)
    })
  }

  // On mount, fetch random users
  useEffect(() => {
    fetchRandomUsers()
  }, [])

  return (
    <></>
  );
}

export default App;


问:
Connect to WiFi Access Point programmatically with Android 10
With Android 10, I'm using the following method to connect to my Wifi Access Point :

```<code>@RequiresApi(api = Build.VERSION_CODES.Q)
public static void connectToWifiAccessPoint(String AP_SSID, String AP_PASSWORD, ConnectivityManager connectivityManager) {

    WifiNetworkSpecifier.Builder builder = null;
    builder = new WifiNetworkSpecifier.Builder();

    builder.setSsid(AP_SSID);
    builder.setWpa2Passphrase(AP_PASSWORD);

    WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
    NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
    networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    networkRequestBuilder.setNetworkSpecifier(wifiNetworkSpecifier);
    NetworkRequest networkRequest = networkRequestBuilder.build();

    connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(@NonNull Network network) {
                super.onAvailable(network);
                connectivityManager.bindProcessToNetwork(network);
            }
    });
}
</code>```

A few seconds after calling this method, the OS displays this pop-up:

After hitting the connect button, the same pop-up appears a few seconds later, and the
smartphone is still not connected to the access point.

Any idea how to fix this please ?

答: Try to forget the wifi Access point from saved networks</p>

Body:
With Android 10, I'm using the following method to connect to my Wifi Access Point :

@RequiresApi(api = Build.VERSION_CODES.Q)
public static void connectToWifiAccessPoint(String AP_SSID, String AP_PASSWORD, ConnectivityManager connectivityManager) {

    WifiNetworkSpecifier.Builder builder = null;
    builder = new WifiNetworkSpecifier.Builder();

    builder.setSsid(AP_SSID);
    builder.setWpa2Passphrase(AP_PASSWORD);

    WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
    NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
    networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    networkRequestBuilder.setNetworkSpecifier(wifiNetworkSpecifier);
    NetworkRequest networkRequest = networkRequestBuilder.build();

    connectivityManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(@NonNull Network network) {
                super.onAvailable(network);
                connectivityManager.bindProcessToNetwork(network);
            }
    });
}

A few seconds after calling this method, the OS displays this pop-up:

After hitting the connect button, the same pop-up appears a few seconds later, and the
smartphone is still not connected to the access point.

Any idea how to fix this please ?


问:
Faster way to remove stop words in Python
I am trying to remove stopwords from a string of text:

```<code>from nltk.corpus import stopwords
text = 'hello bye the the hi'
text = ' '.join([word for word in text.split() if word not in (stopwords.words('english'))])
</code>```

I am processing 6 mil of such strings so speed is important.  Profiling my code, the slowest part is the lines above, is there a better way to do this?  I'm thinking of using something like regex's re.sub but I don't know how to write the pattern for a set of words.  Can someone give me a hand and I'm also happy to hear other possibly faster methods.

Note:  I tried someone's suggest of wrapping stopwords.words('english') with set() but that made no difference.

Thank you.

答:
Using just a regular dict seems to be the fastest solution by far.

Surpassing even the Counter solution by about 10%

from nltk.corpus import stopwords
stopwords_dict = {word: 1 for word in stopwords.words("english")}
text = 'hello bye the the hi'
text = " ".join([word for word in text.split() if word not in stopwords_dict])
```
<p>Tested using the cProfile profiler</p>
<p>You can find the test code used here:
https://gist.github.com/maxandron/3c276924242e7d29d9cf980da0a8a682</p>
<p>EDIT:</p>
<p>On top of that if we replace the list comprehension with a loop we get another 20% increase in performance</p>
<pre class="lang-py prettyprint-override"><code>from nltk.corpus import stopwords
stopwords_dict = {word: 1 for word in stopwords.words("english")}
text = 'hello bye the the hi'

new = ""
for word in text.split():
    if word not in stopwords_dict:
        new += word
text = new
</code>```

Body:
I am trying to remove stopwords from a string of text:

from nltk.corpus import stopwords
text = 'hello bye the the hi'
text = ' '.join([word for word in text.split() if word not in (stopwords.words('english'))])

I am processing 6 mil of such strings so speed is important.  Profiling my code, the slowest part is the lines above, is there a better way to do this?  I'm thinking of using something like regex's re.sub but I don't know how to write the pattern for a set of words.  Can someone give me a hand and I'm also happy to hear other possibly faster methods.

Note:  I tried someone's suggest of wrapping stopwords.words('english') with set() but that made no difference.

Thank you.

Body:
Using just a regular dict seems to be the fastest solution by far.

Surpassing even the Counter solution by about 10%

from nltk.corpus import stopwords
stopwords_dict = {word: 1 for word in stopwords.words("english")}
text = 'hello bye the the hi'
text = " ".join([word for word in text.split() if word not in stopwords_dict])

Tested using the cProfile profiler

You can find the test code used here:
https://gist.github.com/maxandron/3c276924242e7d29d9cf980da0a8a682

EDIT:

On top of that if we replace the list comprehension with a loop we get another 20% increase in performance

from nltk.corpus import stopwords
stopwords_dict = {word: 1 for word in stopwords.words("english")}
text = 'hello bye the the hi'

new = ""
for word in text.split():
    if word not in stopwords_dict:
        new += word
text = new


问:
Why is Lisp so often connected to "Symbolic computation"
We know mathematics have both symbolic and numeric computation.  But why is Lisp, as a common programming language, connected to symbolic computation more closely?

What parts of Lisp make it good for symbolic problems?

答: In LISP you can enter a symbol without predeclaration. Also, complex data structures are supported as linked lists of arbitrary complexity. Since manipulation of symbols is best accomplished in the context of complex data structures, LISP is a perfect fit. Additionally, when using complex data structures to model a problem other languages require you to spend a lot of effort in walking the data structures. In lisp the data structure traversal is automated and you can work at a higher level of abstraction.</p>


问:
onClick not working React js
I am trying to call a function when a user clicks a div (using onClick in react). I don't need to pass any arguments at this moment, just need to call the function. I'm fairly new to react.js so apologies in advance for my ignorance. Thanks.</p>

```<code>var Test = React.createClass({

btnTapped: function(){
    console.log('tapped!');
},
render: function() {
    var stationComponents = this.props.stations.map(function(station, index) {

    return <div onClick={btnTapped()}><img src="img/test.png" />{station}</div>;

    });
    return <div>{stationComponents}</div>;
   }
});

var cards = ["amazon", "aeo", "aerie", "barnes", "bloomingdales", "bbw","bestbuy", "regal", "cvs", "ebay", "gyft", "itunes", "jcp", "panera", "staples", "walmart", "target", "sephora", "walgreens", "starbucks"];

ReactDOM.render(<Test stations={cards} />, document.getElementById('test-div'));
</code>```

答:
If you are using webpack, make sure your output file which you configured in webpack.config.js (usually bundle.js) is not added in your index.html file.

This solved the problem for me.

Title: onClick not working React js

Body:
I am trying to call a function when a user clicks a div (using onClick in react). I don't need to pass any arguments at this moment, just need to call the function. I'm fairly new to react.js so apologies in advance for my ignorance. Thanks.

var Test = React.createClass({

btnTapped: function(){
    console.log('tapped!');
},
render: function() {
    var stationComponents = this.props.stations.map(function(station, index) {

    return
{station}
;

    });
    return
{stationComponents}
;
   }
});

var cards = ["amazon", "aeo", "aerie", "barnes", "bloomingdales", "bbw","bestbuy", "regal", "cvs", "ebay", "gyft", "itunes", "jcp", "panera", "staples", "walmart", "target", "sephora", "walgreens", "starbucks"];

ReactDOM.render(<Test stations={cards} />, document.getElementById('test-div'));


问:
Command not found: django-admin.py
I am a complete beginner to Python/Django, but I want to dive right in and start experimenting. Thus I was following this guide on installing Python/Django http://devcenter.heroku.com/articles/django.

Everything is working fine until the step

django-admin.py startproject hellodjango

Where I get

command not found: django-admin.py

Now I have tried a few things, but none have really worked out. Is there someone kind enough to point me in the right direction?

P.S. Is there a great guide out there on running Python/Django locally on a Mac to run and test apps?

I'm on Mac OS X Lion, Python 2.7.

答:
Since you're just starting out. It is very important to adopt best practices. You will face many dependency related issues with this approach of development. In this case it is always recommended to work with a virtual environment for each python project.

This will ensure fresh installation of project-specific dependencies that do not overlap with what the system you are running on already has.

If you have not already noticed, you will come across more issues such as python3 not working with earlier versions of django. pip will skip install as it checks and finds a version of django already installed. So this cannot be stressed enough, always use a virtual environment for local setups.

to do so:

cd [your project path]

virtualenv venv

you can active your environment by :

source ./venv/bin/active

install your requirements packages with pip :

pip install -r
or pip install
you can also install your requirements modules without activate the environment

./venv/bin/pip install

to run your python script use :

python <.py file>

and if you didn't activate your env use :

./venv/bin/python <.py file>

Title: Command not found: django-admin.py


问:
Unable to connect oracle database using cx_oracle from AWS Glue
I am trying to connect oracle database from AWS glue using cx_oracle but i am getting this error message

DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for help

I tried to downoad the so files as per the document and stored it in S3 which has been linked to Glue with --extra-files parameter but still getting same error message

I have tried this stackoverflow question and also tried to set rpath with s3 url but no success. Any idea would really help

答:
Credits

This answer is a compilation of this and this and a lot of discussion around the former in the comments. Credit for the rpath patching solution goes to @harjeet-singh, the original author of the abovementioned answer and for the libaio to @good-will, but there are still some steps around these solutions that keep confusing people, so that is why I am going to consolidate everything in a single step-by-step answer here.

Background

In order to connect to an Oracle database using cx-Oracle from a Python shell AWS Glue job, we need to bundle the oracle client libraries with it. Furthermore, the libraries have to be patched with a correct rpath in order to load correctly, because in Glue runtime we have filesystem write access only to /tmp, which is where our archive will be, but cx-Oracle can't know that and expects a different directory by default. And the LD_LIBRARY_PATH hack cannot be implemented because we have no control over how the Glue job is launched.

Step-By-Step Guide

Download Instant Client Basic ZIP package for x86-64 Linux from here. This guide uses version 21.5.0.0.0

wget https://download.oracle.com/otn_software/linux/instantclient/instantclient-basic-linuxx64.zip
```
<ol start="2">
<li>Unzip the archive</li>
</ol>
<pre class="lang-sh prettyprint-override"><code>unzip instantclient-basic-linuxx64.zip
</code>```

Remove symlinks from the archive and move the file they point to (in this case libclntsh.so.21.1 to the one that is going to be looked up when you use cx-Oracle: libclntsh.so). This is done because whatever loads these libraries dynamically apparently does not resolve symlinks. Maybe it will in the future, but I had to do this to make it work.

cd instantclient_21_5/
find . -type l -name "libclntsh.so*" -delete
mv libclntsh.so.21.1 libclntsh.so
```
<p>Do the same with the other files with symlinks if after completing the whole guide and running your job you still have problems with libraries like</p>
<blockquote>
<p>DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libsomething.so: cannot open shared object file: No such file or directory"</p>
</blockquote>
<ol start="4">
<li>Patch the <code>rpath</code> to point to a static directory that we are going to be using from inside the Glue job
For example, if your archive is named <code>instant-client-basic-linux.x64-21.5.0.0.0</code> and it contains a folder named <code>instantclient_21_5</code> with all the libraries. When the job runs, this archive is going to be available in a random directory under <code>/tmp</code> (more on that below. We need to find our archive in one of those directories and extract it to a static directory under <code>/tmp</code>, for example <code>/tmp/libs</code>. So then, your <code>rpath</code> would be <code>/tmp/libs/instant-client-basic-linux.x64-21.5.0.0.0/instantclient_21_5</code>, because that in the absolute path to client libraries.</li>
</ol>
<pre class="lang-sh prettyprint-override"><code>sudo apt-get update
sudo apt-get install patchelf -y
patchelf --set-rpath /tmp/libs/instant-client-basic-linux.x64-21.5.0.0.0/instantclient_21_5 libclntsh.so
</code>```

Put libaio.so.1 in the archive

cd ..
wget https://src.fedoraproject.org/lookaside/pkgs/libaio/libaio-0.3.110.tar.gz/2a35602e43778383e2f4907a4ca39ab8/libaio-0.3.110.tar.gz
tar xzvf libaio-0.3.110.tar.gz
cd libaio-0.3.110
make prefix=`pwd`/usr install
find ./usr/lib/ -type l -name "libclntsh.so*" -delete
mv ./usr/lib/libaio.so.1.0.1 ../instantclient_21_5/libaio.so.1
```
<p><strong>Note:</strong> you might want to check with a newer version of <code>libaio</code> if there is one.
6. Zip the archive</p>
<pre class="lang-sh prettyprint-override"><code>cd ..
zip -T -r instantclient-basic-linuxx64_patched.zip instantclient_21_5/
</code>```

Download cx-Oracle wheel

wget https://files.pythonhosted.org/packages/a9/b7/c2d0223fb4f1013b090cf82f3ce56f36f33b79a48f9c33b36717c2977b04/cx_Oracle-8.3.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
```
<ol start="8">
<li>Upload the archive and <code>cx-Oracle</code> to S3</li>
</ol>
<pre class="lang-sh prettyprint-override"><code>aws s3 cp instantclient-basic-linuxx64_patched.zip s3://<mybucket>/glue_libs
aws s3 cp cx_Oracle-8.3.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl s3://<mybucket>/glue_libs
</code>```

Configure your Glue job

I assume your glue job is already created and we are going to just configure it.

Put the S3 URL to the archive in "Referenced files path" configuration parameter

Put the S3 URL to the cx-Oracle wheel in "Python library path" configuration parameter

Add a bit of code to the glue job to set up the libraries.
This code must be executed before any usage of cx-Oracle. It can be executed after import, but before the usage.

Iterate through random directories in /tmp, find an archive you created and extract it to a static directory that we set in rpath previously. Then initialize the cx-Oracle client and you are good to go.
Here is an example of an implementation:

import zipfile

from pathlib import Path

import cx_Oracle

filename = 'instantclient-basic-linuxx64_patched.zip'
oracle_archive = next(Path('./tmp').glob(f'**/{filename}'))
with zipfile.ZipFile(oracle_archive, 'r') as f:
    Path('./tmp/libs').mkdir()
    f.extractall('./tmp/libs')

cx_Oracle.init_oracle_client(lib_dir=f'/tmp/libs/{filename}/instantclient_21_5')
```
<h1>TLDR</h1>
<p>Run this on your Linux machine (substitute your bucket name in the end):</p>
<pre class="lang-sh prettyprint-override"><code>wget https://download.oracle.com/otn_software/linux/instantclient/instantclient-basic-linuxx64.zip
unzip instantclient-basic-linuxx64.zip
cd instantclient_21_5/
find . -type l -name "libclntsh.so*" -delete
mv libclntsh.so.21.1 libclntsh.so
sudo apt-get update
sudo apt-get install patchelf -y
patchelf --set-rpath /tmp/libs/instant-client-basic-linux.x64-21.5.0.0.0/instantclient_21_5 instantclient_21_5/libclntsh.so
cd ..
wget https://src.fedoraproject.org/lookaside/pkgs/libaio/libaio-0.3.110.tar.gz/2a35602e43778383e2f4907a4ca39ab8/libaio-0.3.110.tar.gz
tar xzvf libaio-0.3.110.tar.gz
cd libaio-0.3.110
make prefix=`pwd`/usr install
find ./usr/lib/ -type l -name "libclntsh.so*" -delete
mv ./usr/lib/libaio.so.1.0.1 ../instantclient_21_5/libaio.so.1
cd ..
zip -T -r instantclient-basic-linuxx64_patched.zip instantclient_21_5/
wget https://files.pythonhosted.org/packages/a9/b7/c2d0223fb4f1013b090cf82f3ce56f36f33b79a48f9c33b36717c2977b04/cx_Oracle-8.3.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
aws s3 cp instantclient-basic-linuxx64_patched.zip s3://<mybucket>/glue_libs/
aws s3 cp cx_Oracle-8.3.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl s3://<mybucket>/glue_libs/
</code>```

Follow steps 9 and 10 above to configure your job.

Hope anyone who reads this will get this right on the first try, because I sure didn't.

Body:
Credits

This answer is a compilation of this and this and a lot of discussion around the former in the comments. Credit for the rpath patching solution goes to @harjeet-singh, the original author of the abovementioned answer and for the libaio to @good-will, but there are still some steps around these solutions that keep confusing people, so that is why I am going to consolidate everything in a single step-by-step answer here.

Background

In order to connect to an Oracle database using cx-Oracle from a Python shell AWS Glue job, we need to bundle the oracle client libraries with it. Furthermore, the libraries have to be patched with a correct rpath in order to load correctly, because in Glue runtime we have filesystem write access only to /tmp, which is where our archive will be, but cx-Oracle can't know that and expects a different directory by default. And the LD_LIBRARY_PATH hack cannot be implemented because we have no control over how the Glue job is launched.

Step-By-Step Guide

Download Instant Client Basic ZIP package for x86-64 Linux from here. This guide uses version 21.5.0.0.0

wget https://download.oracle.com/otn_software/linux/instantclient/instantclient-basic-linuxx64.zip

Unzip the archive

unzip instantclient-basic-linuxx64.zip

Remove symlinks from the archive and move the file they point to (in this case libclntsh.so.21.1 to the one that is going to be looked up when you use cx-Oracle: libclntsh.so). This is done because whatever loads these libraries dynamically apparently does not resolve symlinks. Maybe it will in the future, but I had to do this to make it work.

cd instantclient_21_5/
find . -type l -name "libclntsh.so*" -delete
mv libclntsh.so.21.1 libclntsh.so

Do the same with the other files with symlinks if after completing the whole guide and running your job you still have problems with libraries like

DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libsomething.so: cannot open shared object file: No such file or directory"

Patch the rpath to point to a static directory that we are going to be using from inside the Glue job
For example, if your archive is named instant-client-basic-linux.x64-21.5.0.0.0 and it contains a folder named instantclient_21_5 with all the libraries. When the job runs, this archive is going to be available in a random directory under /tmp (more on that below. We need to find our archive in one of those directories and extract it to a static directory under /tmp, for example /tmp/libs. So then, your rpath would be /tmp/libs/instant-client-basic-linux.x64-21.5.0.0.0/instantclient_21_5, because that in the absolute path to client libraries.

sudo apt-get update
sudo apt-get install patchelf -y
patchelf --set-rpath /tmp/libs/instant-client-basic-linux.x64-21.5.0.0.0/instantclient_21_5 libclntsh.so

Put libaio.so.1 in the archive

cd ..
wget https://src.fedoraproject.org/lookaside/pkgs/libaio/libaio-0.3.110.tar.gz/2a35602e43778383e2f4907a4ca39ab8/libaio-0.3.110.tar.gz
tar xzvf libaio-0.3.110.tar.gz
cd libaio-0.3.110
make prefix=`pwd`/usr install
find ./usr/lib/ -type l -name "libclntsh.so*" -delete
mv ./usr/lib/libaio.so.1.0.1 ../instantclient_21_5/libaio.so.1

Note: you might want to check with a newer version of libaio if there is one.
6. Zip the archive

cd ..
zip -T -r instantclient-basic-linuxx64_patched.zip instantclient_21_5/

Download cx-Oracle wheel

wget https://files.pythonhosted.org/packages/a9/b7/c2d0223fb4f1013b090cf82f3ce56f36f33b79a48f9c33b36717c2977b04/cx_Oracle-8.3.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl

Upload the archive and cx-Oracle to S3

aws s3 cp instantclient-basic-linuxx64_patched.zip s3://<mybucket>/glue_libs
aws s3 cp cx_Oracle-8.3.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl s3://<mybucket>/glue_libs

Configure your Glue job

I assume your glue job is already created and we are going to just configure it.

Put the S3 URL to the archive in "Referenced files path" configuration parameter

Put the S3 URL to the cx-Oracle wheel in "Python library path" configuration parameter

Add a bit of code to the glue job to set up the libraries.
This code must be executed before any usage of cx-Oracle. It can be executed after import, but before the usage.

Iterate through random directories in /tmp, find an archive you created and extract it to a static directory that we set in rpath previously. Then initialize the cx-Oracle client and you are good to go.
Here is an example of an implementation:

import zipfile

from pathlib import Path

import cx_Oracle

filename = 'instantclient-basic-linuxx64_patched.zip'
oracle_archive = next(Path('./tmp').glob(f'**/{filename}'))
with zipfile.ZipFile(oracle_archive, 'r') as f:
    Path('./tmp/libs').mkdir()
    f.extractall('./tmp/libs')

cx_Oracle.init_oracle_client(lib_dir=f'/tmp/libs/{filename}/instantclient_21_5')

TLDR

Run this on your Linux machine (substitute your bucket name in the end):

wget https://download.oracle.com/otn_software/linux/instantclient/instantclient-basic-linuxx64.zip
unzip instantclient-basic-linuxx64.zip
cd instantclient_21_5/
find . -type l -name "libclntsh.so*" -delete
mv libclntsh.so.21.1 libclntsh.so
sudo apt-get update
sudo apt-get install patchelf -y
patchelf --set-rpath /tmp/libs/instant-client-basic-linux.x64-21.5.0.0.0/instantclient_21_5 instantclient_21_5/libclntsh.so
cd ..
wget https://src.fedoraproject.org/lookaside/pkgs/libaio/libaio-0.3.110.tar.gz/2a35602e43778383e2f4907a4ca39ab8/libaio-0.3.110.tar.gz
tar xzvf libaio-0.3.110.tar.gz
cd libaio-0.3.110
make prefix=`pwd`/usr install
find ./usr/lib/ -type l -name "libclntsh.so*" -delete
mv ./usr/lib/libaio.so.1.0.1 ../instantclient_21_5/libaio.so.1
cd ..
zip -T -r instantclient-basic-linuxx64_patched.zip instantclient_21_5/
wget https://files.pythonhosted.org/packages/a9/b7/c2d0223fb4f1013b090cf82f3ce56f36f33b79a48f9c33b36717c2977b04/cx_Oracle-8.3.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
aws s3 cp instantclient-basic-linuxx64_patched.zip s3://<mybucket>/glue_libs/
aws s3 cp cx_Oracle-8.3.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl s3://<mybucket>/glue_libs/

Follow steps 9 and 10 above to configure your job.

Hope anyone who reads this will get this right on the first try, because I sure didn't.


问:
Receive email in real-time from many email account with Mailkit
I am using Mailkit Idle Client to receive email in real-time from one email account. It's OK.
Is it possible to do it with many accounts?

Thanks

答: Yes, it's possible - you would just need to have 1 ImapClient (and IdleClient) per account.</p>


问:
Fastest way to search a word into Dictionary in R
I want to search a word from a Dictionary using R. I used grepl for searching word, x from dictionary dutch. Finally I made this in such way, it will return how many words in a sentence are from this dictionary. My script as follow:

```<code>tw_copy$token <- tokenize_tweets(tw_copy$text)

count = function(x){
  de = c()
  other = c()
    for (token in x){
      if (grepl(token, dutch)==TRUE){
        de <- c(de, token)
      }
      else{
        other <- c(other, token)
      }
    }
  return (c(length(de)/length(x), length(other)/length(x)))
}

result <- lapply(tw_copy$token, FUN = count)

tw_copy$de =  lapply(result, "[[", 1)
</code>```

Now, the output is right. But It is really slow and can not get output for bigger dataset.

Can anyone suggest me to write it other way for faster performance?

答:
I would do something like this:

base R

```<code>tw_copy$num_dutch_words = lapply(tw_copy$token, \(x) sum(x %in% dutch))
</code>```

Input

```<code>dutch = c("this", "tweet", "one")
tw_copy = data.frame(
  author=c("a","b","c"),
  text = c("this is the first tweet",
           "this one is the second tweet",
           "and this one is the third tweet")
)
tw_copy$token = lapply(tw_copy$text, \(x) strsplit(x, " ")[[1]])
</code>```

data.table

(this was my original answer, as I assumed you might have some speed up with very large dataset, but my assumption may be wrong)

```<code>tw_copy[, num_dutch_words:=sum(token[[1]] %in% dutch), by=1:nrow(tw_copy)]
</code>```

Input Data:

```<code>dutch = c("this", "tweet", "one")

tw_copy = data.table(
  author=c("a","b","c"),
  text = c("this is the first tweet",
           "this one is the second tweet",
           "and this one is the third tweet")
)
tw_copy[, token:=list(strsplit(text," "))]
</code>```

In both cases, output like this:

```<code>   author                            text                         token num_dutch_words
1:      a         this is the first tweet       this,is,the,first,tweet               2
2:      b    this one is the second tweet  this,one,is,the,second,tweet               3
3:      c and this one is the third tweet and,this,one,is,the,third,...               3
</code>```

Body:
I want to search a word from a Dictionary using R. I used grepl for searching word, x from dictionary dutch. Finally I made this in such way, it will return how many words in a sentence are from this dictionary. My script as follow:

tw_copy$token <- tokenize_tweets(tw_copy$text)

count = function(x){
  de = c()
  other = c()
    for (token in x){
      if (grepl(token, dutch)==TRUE){
        de <- c(de, token)
      }
      else{
        other <- c(other, token)
      }
    }
  return (c(length(de)/length(x), length(other)/length(x)))
}

result <- lapply(tw_copy$token, FUN = count)

tw_copy$de =  lapply(result, "[[", 1)

Now, the output is right. But It is really slow and can not get output for bigger dataset.

Can anyone suggest me to write it other way for faster performance?

Body:
I would do something like this:

base R

tw_copy$num_dutch_words = lapply(tw_copy$token, \(x) sum(x %in% dutch))

Input

dutch = c("this", "tweet", "one")
tw_copy = data.frame(
  author=c("a","b","c"),
  text = c("this is the first tweet",
           "this one is the second tweet",
           "and this one is the third tweet")
)
tw_copy$token = lapply(tw_copy$text, \(x) strsplit(x, " ")[[1]])

data.table

(this was my original answer, as I assumed you might have some speed up with very large dataset, but my assumption may be wrong)

tw_copy[, num_dutch_words:=sum(token[[1]] %in% dutch), by=1:nrow(tw_copy)]

Input Data:

dutch = c("this", "tweet", "one")

tw_copy = data.table(
  author=c("a","b","c"),
  text = c("this is the first tweet",
           "this one is the second tweet",
           "and this one is the third tweet")
)
tw_copy[, token:=list(strsplit(text," "))]

In both cases, output like this:

   author                            text                         token num_dutch_words
1:      a         this is the first tweet       this,is,the,first,tweet               2
2:      b    this one is the second tweet  this,one,is,the,second,tweet               3
3:      c and this one is the third tweet and,this,one,is,the,third,...               3


问:
How do I dynamically allocate memory to a struct with data?
Lets say I have a program with a struct

```<code>typedef struct Room {
      char* name;
      struct Room * North;
      struct Room * South;
      struct Room * East;
      struct Room * West;
      struct Item * itemList;
     char *character[10];
     } Room;
</code>```

And I have to create a bunch of them, as well as dynamically allocate memory for them. As I am new to this, there might be some obvious answers that I might not have known, but how can I assign memory to these structs, while also assigning values to them at once? I know there are ways to do so by declaring a pointer and then manually assigning data using ->, but I want to use the format I have given below, as it would be difficult to assign data one value at a time.

```<code>  struct Room* room1p = (struct Room*) malloc(sizeof(struct Room));
  struct Room* room2p = (struct Room*) malloc(sizeof(struct Room));
  struct Room* room3p = (struct Room*) malloc(sizeof(struct Room));
  struct Room room1, room2, room3;
  room1 = (Room){roomList[1], NULL, &room3, &room2, NULL, itemRoom1, charList[1]};
  room2 = (Room){roomList[2], NULL, &room2, &room3, &room1, itemRoom2, charList[2]};
  room3 = (Room){roomList[3], NULL, &room1, NULL, &room2, itemRoom3, charList[3]};
</code>```

答:
There is not a built-in way to do this in C. Allocating memory is a separate action from initializing said memory with a value.

You could assign the allocation and initialization to a function if you want to be able to do both in one line:

```<code>struct Room* room_alloc_and_init(char* name, struct Room* north,
                                 struct Room* south, struct Room* east,
                                 struct Room* west, struct Item* item_list,
                                 char** character)
{
    // Allocate
    struct Room* new_room = (struct Room*) malloc(sizeof(struct Room));

    // Initialize
    new_room->Name = name;
    new_room->North = north;
    new_room->South = south;
    new_room->East = east;
    new_room->West = west;
    new_room->itemList = item_list;
    new_room->character = character;

    return new_room;
}

// Later on
struct Room* room1 = room_alloc_and_init(...);

// Be sure to free your memory when you are done with it!
free(room1);

// Also free any of the pointers you passed to room_alloc_and_init
// if they point to heap allocated memory
</code>```

Body:
Lets say I have a program with a struct

typedef struct Room {
      char* name;
      struct Room * North;
      struct Room * South;
      struct Room * East;
      struct Room * West;
      struct Item * itemList;
     char *character[10];
     } Room;

And I have to create a bunch of them, as well as dynamically allocate memory for them. As I am new to this, there might be some obvious answers that I might not have known, but how can I assign memory to these structs, while also assigning values to them at once? I know there are ways to do so by declaring a pointer and then manually assigning data using ->, but I want to use the format I have given below, as it would be difficult to assign data one value at a time.

  struct Room* room1p = (struct Room*) malloc(sizeof(struct Room));
  struct Room* room2p = (struct Room*) malloc(sizeof(struct Room));
  struct Room* room3p = (struct Room*) malloc(sizeof(struct Room));
  struct Room room1, room2, room3;
  room1 = (Room){roomList[1], NULL, &room3, &room2, NULL, itemRoom1, charList[1]};
  room2 = (Room){roomList[2], NULL, &room2, &room3, &room1, itemRoom2, charList[2]};
  room3 = (Room){roomList[3], NULL, &room1, NULL, &room2, itemRoom3, charList[3]};

Body:
There is not a built-in way to do this in C. Allocating memory is a separate action from initializing said memory with a value.

You could assign the allocation and initialization to a function if you want to be able to do both in one line:

struct Room* room_alloc_and_init(char* name, struct Room* north,
                                 struct Room* south, struct Room* east,
                                 struct Room* west, struct Item* item_list,
                                 char** character)
{
    // Allocate
    struct Room* new_room = (struct Room*) malloc(sizeof(struct Room));

    // Initialize
    new_room->Name = name;
    new_room->North = north;
    new_room->South = south;
    new_room->East = east;
    new_room->West = west;
    new_room->itemList = item_list;
    new_room->character = character;

    return new_room;
}

// Later on
struct Room* room1 = room_alloc_and_init(...);

// Be sure to free your memory when you are done with it!
free(room1);

// Also free any of the pointers you passed to room_alloc_and_init
// if they point to heap allocated memory


问:
How to access 2 dimensional Array Pointer in a typedef-struct over a function in c
I have Problems with accessing the 2D Array in a typedef from a other function over a pointer, even if i allocate the array in heap!

```<code>#include <stdlib.h>
#include <stdio.h>
typedef struct {
    int a;
    int b;
    int **data;
} Image;
Image *createImage(int a, int b) {
    Image createImage;
    createImage.data = malloc(a * sizeof(int *));
    for (int i = 0; i < a; i++) {
         createImage.data[i] = malloc(b * sizeof(int));
    }
    Image *imagePointer = malloc(sizeof(Image));
    imagePointer = &createImage;
    return imagePointer;
}
int main () {
     int a = 70;
     int b = 90;
     Image *imagePointer = createImage(a, b);
     for (int i = 0; i < a; i++) {
          for (int j = 0; j < b; j++) {
               imagePointer->data[i][j] = i + j;
          }
     }
}
</code>```

I get an error at load Image because i have done something wrong with accessing the allocated storage. What should i change?

Jonathan

答:
It is not 2D array only an array of pointers.

Use size_t for sizes

Use objects not types in sizeof

Always check the result of malloc

I would use flexible array member and array pointers to remove one level of indirection and ease allocation and deallocation (only one malloc/free needed)

```<code>typedef struct {
    size_t a;
    size_t b;
    unsigned data[];
} Image;

Image *createImage(const size_t a, const size_t b)
{
    Image *imagePointer;
    imagePointer = malloc(sizeof(*imagePointer) + a * b * sizeof(imagePointer -> data[0]));
    if(imagePointer)
    {
        imagePointer -> a = a;
        imagePointer -> b = b;
    }
    return imagePointer;
}

int main (void)
{
     const size_t a = 70;
     const size_t b = 90;

     Image *imagePointer = createImage(a, b);
     if(imagePointer)
     {
        unsigned (*image)[imagePointer -> b] = (unsigned (*)[imagePointer -> b])imagePointer -> data;
        for (size_t i = 0; i < a; i++)
        {
            for (size_t j = 0; j < b; j++)
            {
                image[i][j] = i + j;
            }
        }
     }
}
</code>```

Body:
I have Problems with accessing the 2D Array in a typedef from a other function over a pointer, even if i allocate the array in heap!

#include <stdlib.h>
#include <stdio.h>
typedef struct {
    int a;
    int b;
    int **data;
} Image;
Image *createImage(int a, int b) {
    Image createImage;
    createImage.data = malloc(a * sizeof(int *));
    for (int i = 0; i < a; i++) {
         createImage.data[i] = malloc(b * sizeof(int));
    }
    Image *imagePointer = malloc(sizeof(Image));
    imagePointer = &createImage;
    return imagePointer;
}
int main () {
     int a = 70;
     int b = 90;
     Image *imagePointer = createImage(a, b);
     for (int i = 0; i < a; i++) {
          for (int j = 0; j data[i][j] = i + j;
          }
     }
}

I get an error at load Image because i have done something wrong with accessing the allocated storage. What should i change?

Jonathan

Body:
It is not 2D array only an array of pointers.

Use size_t for sizes

Use objects not types in sizeof

Always check the result of malloc

I would use flexible array member and array pointers to remove one level of indirection and ease allocation and deallocation (only one malloc/free needed)

typedef struct {
    size_t a;
    size_t b;
    unsigned data[];
} Image;

Image *createImage(const size_t a, const size_t b)
{
    Image *imagePointer;
    imagePointer = malloc(sizeof(*imagePointer) + a * b * sizeof(imagePointer -> data[0]));
    if(imagePointer)
    {
        imagePointer -> a = a;
        imagePointer -> b = b;
    }
    return imagePointer;
}

int main (void)
{
     const size_t a = 70;
     const size_t b = 90;

     Image *imagePointer = createImage(a, b);
     if(imagePointer)
     {
        unsigned (*image)[imagePointer -> b] = (unsigned (*)[imagePointer -> b])imagePointer -> data;
        for (size_t i = 0; i < a; i++)
        {
            for (size_t j = 0; j < b; j++)
            {
                image[i][j] = i + j;
            }
        }
     }
}


问:
How can I create a Group_Concat field per table using values from other records
Suppose a table of First and Last Names and for each record I want to do comma-delimited list of relatives.

1ST | LAST | RELATIVES

Bob | Smith | Alice,Andrew

Alice | Smith | Bob,Andrew

Andrew |Smith | Bob,Alice

Alex | Jones | Anny, Ricky

Anny | Jones | Alex, Ricky

Ricky | Jones | Alex, Anny

As per this sqlFiddle

http://sqlfiddle.com/#!9/25d80c/1

I know how to group_contact manually for any last name but am unclear how for each record I could have it go find the records with matching last name and run the same group_concat

答:
You can put the aggregation in a lateral join, like so:

```<code>select s.First, s.Last, r.Relatives
from Surnames s,
lateral (
  select group_concat(First) Relatives
  from Surnames r
  where s.Last = r.Last AND s.First != r.First
)r
</code>```

DB<>Fiddle

Body:
You can put the aggregation in a lateral join, like so:

select s.First, s.Last, r.Relatives
from Surnames s,
lateral (
  select group_concat(First) Relatives
  from Surnames r
  where s.Last = r.Last AND s.First != r.First
)r

DB<>Fiddle