问:
How to connect to impala using impyla or to hive using pyhive?
I am trying to connect to impala using impyla with this code:

```<code>from impala.dbapi import connect
conn = connect(host='host_name.com', port=21050, user='usr', password='pass', use_ssl=True, auth_mechanism='LDAP')
cursor = conn.cursor()
cursor.execute('SHOW DATABASES')
cursor.fetchall()
</code>```

as per documentation this library needs thrift_sasl in version 0.2.1 but I am not able to install it as it shows this error

```<code>Collecting thrift_sasl==0.2.1
  Using cached https://files.pythonhosted.org/packages/80/36/16dfe92d32df63cc2b7b7be8d0e4a736617b7e52daaa7f83ae386a89d179/thrift_sasl-0.2.1.tar.gz
Collecting sasl>=0.2.1 (from thrift_sasl==0.2.1)
  Using cached https://files.pythonhosted.org/packages/8e/2c/45dae93d666aea8492678499e0999269b4e55f1829b1e4de5b8204706ad9/sasl-0.2.1.tar.gz
Collecting thriftpy (from thrift_sasl==0.2.1)
  Using cached https://files.pythonhosted.org/packages/f4/19/cca118cf7d2087310dbc8bd70dc7df0c1320f2652873a93d06d7ba356d4a/thriftpy-0.3.9.tar.gz
Requirement already satisfied: six in c:\users\psowa\appdata\local\programs\python\python36\lib\site-packages (from sasl>=0.2.1->thrift_sasl==0.2.1) (1.12.0)
Requirement already satisfied: ply<4.0,>=3.4 in c:\users\psowa\appdata\local\programs\python\python36\lib\site-packages (from thriftpy->thrift_sasl==0.2.1) (3.11)
Installing collected packages: sasl, thriftpy, thrift-sasl
  Running setup.py install for sasl ... error
    ERROR: Command errored out with exit status 1:
     command: 'c:\users\psowa\appdata\local\programs\python\python36\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-y42wej4x\\sasl\\setup.py'"'"'; __file__='"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-y42wej4x\\sasl\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\psowa\AppData\Local\Temp\pip-record-rjwkcc76\install-record.txt' --single-version-externally-managed --compile
         cwd: C:\Users\psowa\AppData\Local\Temp\pip-install-y42wej4x\sasl\
    Complete output (27 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.6
    creating build\lib.win-amd64-3.6\sasl
    copying sasl\__init__.py -> build\lib.win-amd64-3.6\sasl
    running egg_info
    writing sasl.egg-info\PKG-INFO
    writing dependency_links to sasl.egg-info\dependency_links.txt
    writing requirements to sasl.egg-info\requires.txt
    writing top-level names to sasl.egg-info\top_level.txt
    reading manifest file 'sasl.egg-info\SOURCES.txt'
    reading manifest template 'MANIFEST.in'
    writing manifest file 'sasl.egg-info\SOURCES.txt'
    copying sasl\saslwrapper.cpp -> build\lib.win-amd64-3.6\sasl
    copying sasl\saslwrapper.h -> build\lib.win-amd64-3.6\sasl
    copying sasl\saslwrapper.pyx -> build\lib.win-amd64-3.6\sasl
    running build_ext
    building 'sasl.saslwrapper' extension
    creating build\temp.win-amd64-3.6
    creating build\temp.win-amd64-3.6\Release
    creating build\temp.win-amd64-3.6\Release\sasl
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Isasl -Ic:\users\psowa\appdata\local\programs\python\python36\include -Ic:\users\psowa\appdata\local\programs\python\python36\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt" /EHsc /Tpsasl/saslwrapper.cpp /Fobuild\temp.win-amd64-3.6\Release\sasl/saslwrapper.obj
    saslwrapper.cpp
    c:\users\psowa\appdata\local\temp\pip-install-y42wej4x\sasl\sasl\saslwrapper.h(22): fatal error C1083: Cannot open include file: 'sasl/sasl.h': No such file or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\psowa\appdata\local\programs\python\python36\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-y42wej4x\\sasl\\setup.py'"'"'; __file__='"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-y42wej4x\\sasl\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\psowa\AppData\Local\Temp\pip-record-rjwkcc76\install-record.txt' --single-version-externally-managed --compile Check the logs for full command output.
</code>```

when I install newest version of thrift_sasl jupyter occurs this error :

```<code>AttributeError: 'TSSLSocket' object has no attribute 'isOpen'
</code>```

I also was trying to connect via pyhive using this code:

```<code>from pyhive import hive

host_name = "host_name.com"
port = 10000
user = "usr"
password = "pass"

def hiveconnection(host_name, port, user,password):
    conn = hive.Connection(host=host_name, port=port, username=user, password=password, auth='LDAP')
    cur = conn.cursor()
    cur.execute('SHOW DATABASES')
    result = cur.fetchall()

    return result

output = hiveconnection(host_name, port, user,password)
print(output)
</code>```

It want me to install sasl but when I am trying to do this it shows that:

```<code>Collecting sasl
  Using cached https://files.pythonhosted.org/packages/8e/2c/45dae93d666aea8492678499e0999269b4e55f1829b1e4de5b8204706ad9/sasl-0.2.1.tar.gz
Requirement already satisfied: six in c:\users\psowa\appdata\local\programs\python\python36\lib\site-packages (from sasl) (1.12.0)
Installing collected packages: sasl
  Running setup.py install for sasl ... error
    ERROR: Command errored out with exit status 1:
     command: 'c:\users\psowa\appdata\local\programs\python\python36\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-9rn_a9g0\\sasl\\setup.py'"'"'; __file__='"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-9rn_a9g0\\sasl\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\psowa\AppData\Local\Temp\pip-record-sedxucba\install-record.txt' --single-version-externally-managed --compile
         cwd: C:\Users\psowa\AppData\Local\Temp\pip-install-9rn_a9g0\sasl\
    Complete output (27 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.6
    creating build\lib.win-amd64-3.6\sasl
    copying sasl\__init__.py -> build\lib.win-amd64-3.6\sasl
    running egg_info
    writing sasl.egg-info\PKG-INFO
    writing dependency_links to sasl.egg-info\dependency_links.txt
    writing requirements to sasl.egg-info\requires.txt
    writing top-level names to sasl.egg-info\top_level.txt
    reading manifest file 'sasl.egg-info\SOURCES.txt'
    reading manifest template 'MANIFEST.in'
    writing manifest file 'sasl.egg-info\SOURCES.txt'
    copying sasl\saslwrapper.cpp -> build\lib.win-amd64-3.6\sasl
    copying sasl\saslwrapper.h -> build\lib.win-amd64-3.6\sasl
    copying sasl\saslwrapper.pyx -> build\lib.win-amd64-3.6\sasl
    running build_ext
    building 'sasl.saslwrapper' extension
    creating build\temp.win-amd64-3.6
    creating build\temp.win-amd64-3.6\Release
    creating build\temp.win-amd64-3.6\Release\sasl
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Isasl -Ic:\users\psowa\appdata\local\programs\python\python36\include -Ic:\users\psowa\appdata\local\programs\python\python36\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt" /EHsc /Tpsasl/saslwrapper.cpp /Fobuild\temp.win-amd64-3.6\Release\sasl/saslwrapper.obj
    saslwrapper.cpp
    c:\users\psowa\appdata\local\temp\pip-install-9rn_a9g0\sasl\sasl\saslwrapper.h(22): fatal error C1083: Cannot open include file: 'sasl/sasl.h': No such file or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\psowa\appdata\local\programs\python\python36\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-9rn_a9g0\\sasl\\setup.py'"'"'; __file__='"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-9rn_a9g0\\sasl\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\psowa\AppData\Local\Temp\pip-record-sedxucba\install-record.txt' --single-version-externally-managed --compile Check the logs for full command output.
</code>```

Any ideas?

答: Using python in version 2.7 fixed this. I think there is issue with compatibility.</p>

Body:
I am trying to connect to impala using impyla with this code:

from impala.dbapi import connect
conn = connect(host='host_name.com', port=21050, user='usr', password='pass', use_ssl=True, auth_mechanism='LDAP')
cursor = conn.cursor()
cursor.execute('SHOW DATABASES')
cursor.fetchall()

as per documentation this library needs thrift_sasl in version 0.2.1 but I am not able to install it as it shows this error

Collecting thrift_sasl==0.2.1
  Using cached https://files.pythonhosted.org/packages/80/36/16dfe92d32df63cc2b7b7be8d0e4a736617b7e52daaa7f83ae386a89d179/thrift_sasl-0.2.1.tar.gz
Collecting sasl>=0.2.1 (from thrift_sasl==0.2.1)
  Using cached https://files.pythonhosted.org/packages/8e/2c/45dae93d666aea8492678499e0999269b4e55f1829b1e4de5b8204706ad9/sasl-0.2.1.tar.gz
Collecting thriftpy (from thrift_sasl==0.2.1)
  Using cached https://files.pythonhosted.org/packages/f4/19/cca118cf7d2087310dbc8bd70dc7df0c1320f2652873a93d06d7ba356d4a/thriftpy-0.3.9.tar.gz
Requirement already satisfied: six in c:\users\psowa\appdata\local\programs\python\python36\lib\site-packages (from sasl>=0.2.1->thrift_sasl==0.2.1) (1.12.0)
Requirement already satisfied: ply<4.0,>=3.4 in c:\users\psowa\appdata\local\programs\python\python36\lib\site-packages (from thriftpy->thrift_sasl==0.2.1) (3.11)
Installing collected packages: sasl, thriftpy, thrift-sasl
  Running setup.py install for sasl ... error
    ERROR: Command errored out with exit status 1:
     command: 'c:\users\psowa\appdata\local\programs\python\python36\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-y42wej4x\\sasl\\setup.py'"'"'; __file__='"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-y42wej4x\\sasl\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\psowa\AppData\Local\Temp\pip-record-rjwkcc76\install-record.txt' --single-version-externally-managed --compile
         cwd: C:\Users\psowa\AppData\Local\Temp\pip-install-y42wej4x\sasl\
    Complete output (27 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.6
    creating build\lib.win-amd64-3.6\sasl
    copying sasl\__init__.py -> build\lib.win-amd64-3.6\sasl
    running egg_info
    writing sasl.egg-info\PKG-INFO
    writing dependency_links to sasl.egg-info\dependency_links.txt
    writing requirements to sasl.egg-info\requires.txt
    writing top-level names to sasl.egg-info\top_level.txt
    reading manifest file 'sasl.egg-info\SOURCES.txt'
    reading manifest template 'MANIFEST.in'
    writing manifest file 'sasl.egg-info\SOURCES.txt'
    copying sasl\saslwrapper.cpp -> build\lib.win-amd64-3.6\sasl
    copying sasl\saslwrapper.h -> build\lib.win-amd64-3.6\sasl
    copying sasl\saslwrapper.pyx -> build\lib.win-amd64-3.6\sasl
    running build_ext
    building 'sasl.saslwrapper' extension
    creating build\temp.win-amd64-3.6
    creating build\temp.win-amd64-3.6\Release
    creating build\temp.win-amd64-3.6\Release\sasl
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Isasl -Ic:\users\psowa\appdata\local\programs\python\python36\include -Ic:\users\psowa\appdata\local\programs\python\python36\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt" /EHsc /Tpsasl/saslwrapper.cpp /Fobuild\temp.win-amd64-3.6\Release\sasl/saslwrapper.obj
    saslwrapper.cpp
    c:\users\psowa\appdata\local\temp\pip-install-y42wej4x\sasl\sasl\saslwrapper.h(22): fatal error C1083: Cannot open include file: 'sasl/sasl.h': No such file or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\psowa\appdata\local\programs\python\python36\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-y42wej4x\\sasl\\setup.py'"'"'; __file__='"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-y42wej4x\\sasl\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\psowa\AppData\Local\Temp\pip-record-rjwkcc76\install-record.txt' --single-version-externally-managed --compile Check the logs for full command output.

when I install newest version of thrift_sasl jupyter occurs this error :

AttributeError: 'TSSLSocket' object has no attribute 'isOpen'

I also was trying to connect via pyhive using this code:

from pyhive import hive

host_name = "host_name.com"
port = 10000
user = "usr"
password = "pass"

def hiveconnection(host_name, port, user,password):
    conn = hive.Connection(host=host_name, port=port, username=user, password=password, auth='LDAP')
    cur = conn.cursor()
    cur.execute('SHOW DATABASES')
    result = cur.fetchall()

    return result

output = hiveconnection(host_name, port, user,password)
print(output)

It want me to install sasl but when I am trying to do this it shows that:

Collecting sasl
  Using cached https://files.pythonhosted.org/packages/8e/2c/45dae93d666aea8492678499e0999269b4e55f1829b1e4de5b8204706ad9/sasl-0.2.1.tar.gz
Requirement already satisfied: six in c:\users\psowa\appdata\local\programs\python\python36\lib\site-packages (from sasl) (1.12.0)
Installing collected packages: sasl
  Running setup.py install for sasl ... error
    ERROR: Command errored out with exit status 1:
     command: 'c:\users\psowa\appdata\local\programs\python\python36\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-9rn_a9g0\\sasl\\setup.py'"'"'; __file__='"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-9rn_a9g0\\sasl\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\psowa\AppData\Local\Temp\pip-record-sedxucba\install-record.txt' --single-version-externally-managed --compile
         cwd: C:\Users\psowa\AppData\Local\Temp\pip-install-9rn_a9g0\sasl\
    Complete output (27 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.6
    creating build\lib.win-amd64-3.6\sasl
    copying sasl\__init__.py -> build\lib.win-amd64-3.6\sasl
    running egg_info
    writing sasl.egg-info\PKG-INFO
    writing dependency_links to sasl.egg-info\dependency_links.txt
    writing requirements to sasl.egg-info\requires.txt
    writing top-level names to sasl.egg-info\top_level.txt
    reading manifest file 'sasl.egg-info\SOURCES.txt'
    reading manifest template 'MANIFEST.in'
    writing manifest file 'sasl.egg-info\SOURCES.txt'
    copying sasl\saslwrapper.cpp -> build\lib.win-amd64-3.6\sasl
    copying sasl\saslwrapper.h -> build\lib.win-amd64-3.6\sasl
    copying sasl\saslwrapper.pyx -> build\lib.win-amd64-3.6\sasl
    running build_ext
    building 'sasl.saslwrapper' extension
    creating build\temp.win-amd64-3.6
    creating build\temp.win-amd64-3.6\Release
    creating build\temp.win-amd64-3.6\Release\sasl
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Isasl -Ic:\users\psowa\appdata\local\programs\python\python36\include -Ic:\users\psowa\appdata\local\programs\python\python36\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt" /EHsc /Tpsasl/saslwrapper.cpp /Fobuild\temp.win-amd64-3.6\Release\sasl/saslwrapper.obj
    saslwrapper.cpp
    c:\users\psowa\appdata\local\temp\pip-install-9rn_a9g0\sasl\sasl\saslwrapper.h(22): fatal error C1083: Cannot open include file: 'sasl/sasl.h': No such file or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\psowa\appdata\local\programs\python\python36\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-9rn_a9g0\\sasl\\setup.py'"'"'; __file__='"'"'C:\\Users\\psowa\\AppData\\Local\\Temp\\pip-install-9rn_a9g0\\sasl\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\psowa\AppData\Local\Temp\pip-record-sedxucba\install-record.txt' --single-version-externally-managed --compile Check the logs for full command output.

Any ideas?


来源: stackexchange

问:
How to drop all tables including dependent object
When I'm trying to remove all tables with:

```<code>base.metadata.drop_all(engine)
</code>```

I'm getting following error:

```<code>ERROR:libdl.database_operations:Cannot drop table: (psycopg2.errors.DependentObjectsStillExist) cannot drop sequence <schema>.<sequence> because other objects depend on it
DETAIL:  default for table <schema>.<table> column id depends on sequence <schema>.<sequence>
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
</code>```

Is there an elegant one-line solution for that?

答:
import psycopg2
from psycopg2 import sql

cnn = psycopg2.connect('...')
cur = cnn.cursor()
cur.execute("""
    select s.nspname as s, t.relname as t
    from pg_class t join pg_namespace s on s.oid = t.relnamespace
    where t.relkind = 'r'
    and s.nspname !~ '^pg_' and s.nspname != 'information_schema'
    order by 1,2
    """)
tables = cur.fetchall()  # make sure they are the right ones

for t in tables:
    cur.execute(
        sql.SQL("drop table if exists {}.{} cascade")
        .format(sql.Identifier(t[0]), sql.Identifier(t[1])))

cnn.commit()  # goodbye
```

ContentLicense: CC BY-SA 4.0

Body:
When I'm trying to remove all tables with:

base.metadata.drop_all(engine)

I'm getting following error:

ERROR:libdl.database_operations:Cannot drop table: (psycopg2.errors.DependentObjectsStillExist) cannot drop sequence <schema>.<sequence> because other objects depend on it
DETAIL:  default for table <schema>.
 column id depends on sequence <schema>.<sequence>
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

Is there an elegant one-line solution for that?


问:
I don't know how to classify JSON data in Android
I'm a newbie in Android. I'm making a crypto currency tracker. I'm using a Korean exchange API. But, when I read JSON data as String, it's a little different from what I found in literature.

In the red bracket, there are some data but are classified as the String like "BTC, ETH". And I learned in a book like this

```<code>String timestamp;
String payment_currency; // payment currency(USD, KRW...)
ArrayList<BidAskBithumb> bids = new ArrayList<BidAskBithumb>(); // Bids
ArrayList<BidAskBithumb> asks = new ArrayList<BidAskBithumb>(); // Asks
</code>```

Every property is saved in String and Arrays. But in that JSON data, there is currency's name like BTC or ETH after payment_currency String. I don't know I have to make every class file like BTC.java or ETH.java. How can I solve this?

This is the JSON response I'm getting.

I read all the currency's data:

The original API documentation link.

答:
I don't know I have to make every class file like BTC.java or ETH.java. How can I solve this?

No, you dont have to create new class files for them, you need to create model class according to your json file. & handle its behaviour.

You can try this to create your model.

Body:
I'm a newbie in Android. I'm making a crypto currency tracker. I'm using a Korean exchange API. But, when I read JSON data as String, it's a little different from what I found in literature.

In the red bracket, there are some data but are classified as the String like "BTC, ETH". And I learned in a book like this

String timestamp;
String payment_currency; // payment currency(USD, KRW...)
ArrayList<BidAskBithumb> bids = new ArrayList<BidAskBithumb>(); // Bids
ArrayList<BidAskBithumb> asks = new ArrayList<BidAskBithumb>(); // Asks

Every property is saved in String and Arrays. But in that JSON data, there is currency's name like BTC or ETH after payment_currency String. I don't know I have to make every class file like BTC.java or ETH.java. How can I solve this?

This is the JSON response I'm getting.

I read all the currency's data:

The original API documentation link.


问:
The difference between Handler.dispatchMessage(msg) and Handler.sendMessage(msg)
When I use Handler.dispatchMessage(msg), the handleMessage(Message msg) will be run on new thread but when I use Handler.sendMessage(msg), the handleMessage(Message msg) will be run on main thread. Who can tell me the difference between them?

Thanks!

Demo:

```<code>public class MainActivity extends Activity
{
private String TAG = "MainActivity";
private Handler mHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        Log.i(TAG, "Handler:" + Thread.currentThread().getId() + " & arg1=" + msg.arg1);
        super.handleMessage(msg);
    }

};

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.i(TAG, "Main:" + Thread.currentThread().getId());
    testMethod();
}

private void testMethod()
{
    Thread thread = new Thread()
    {
        @Override
        public void run()
        {
            Log.i(TAG, "Thread:" + Thread.currentThread().getId());
            Message msg = mHandler.obtainMessage();
            msg.arg1 = 1;
            mHandler.dispatchMessage(msg);
            Message msg2 = mHandler.obtainMessage();
            msg2.arg1 = 2;
            mHandler.sendMessage(msg2);
        }

    };
    thread.start();
}
</code>```

}

Output:

```
04-19 11:32:10.452: INFO/MainActivity(774): Main:1
04-19 11:32:10.488: INFO/MainActivity(774): Thread:8
04-19 11:32:10.492: INFO/MainActivity(774): Handler:8 & arg1=1
04-19 11:32:10.635: INFO/MainActivity(774): Handler:1 & arg1=2

```

答:
If You Call the Handler.dispatchMessage() in the Main Thread Then The Message is processed in Main Thread.

If You Call The Handler.dispatchMessage() in the Worker Thread Then The Message is Processed in Worker Thread.

When You Call Handler.sendMessage(msg) The Message is Processed in the Thread Which Created The Handler.

Body:
When I use Handler.dispatchMessage(msg), the handleMessage(Message msg) will be run on new thread but when I use Handler.sendMessage(msg), the handleMessage(Message msg) will be run on main thread. Who can tell me the difference between them?

Thanks!

Demo:

public class MainActivity extends Activity
{
private String TAG = "MainActivity";
private Handler mHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        Log.i(TAG, "Handler:" + Thread.currentThread().getId() + " & arg1=" + msg.arg1);
        super.handleMessage(msg);
    }

};

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.i(TAG, "Main:" + Thread.currentThread().getId());
    testMethod();
}

private void testMethod()
{
    Thread thread = new Thread()
    {
        @Override
        public void run()
        {
            Log.i(TAG, "Thread:" + Thread.currentThread().getId());
            Message msg = mHandler.obtainMessage();
            msg.arg1 = 1;
            mHandler.dispatchMessage(msg);
            Message msg2 = mHandler.obtainMessage();
            msg2.arg1 = 2;
            mHandler.sendMessage(msg2);
        }

    };
    thread.start();
}

}

Output:

04-19 11:32:10.452: INFO/MainActivity(774): Main:1
04-19 11:32:10.488: INFO/MainActivity(774): Thread:8
04-19 11:32:10.492: INFO/MainActivity(774): Handler:8 & arg1=1
04-19 11:32:10.635: INFO/MainActivity(774): Handler:1 & arg1=2


问:
Merge JSON objects through unique field then print child node data
I have the following JSON:

```<code>data: {
    questions: "[{"id":"vzDDWL3GQvJi","title":"This is question 1","type":"opinion_scale","ref":"data_q1","properties":[]},{"id":"okT0ieWJm74d","title":"This  is question 2","type":"opinion_scale","ref":"data_q2","properties":[]},

    answers: "[{"type":"number","number":2,"field":{"id":"vzDDWL3GQvJi","type":"opinion_scale","ref":"data_q1"}},{"type":"number","number":4,"field":{"id":"okT0ieWJm74d","type":"opinion_scale","ref":"data_q2"}},

    createdDate: "2020-02-14T07:43:02.000000Z"
}
</code>```

A tidy version of the above is:

Question (questions object)

title : "This is question 1"

ref" : "data_q1"

Answer (answers object)

ref" : "data_q1"

number : 2

So, for question 1 (with the ref: data_q1) the number (score) is 2.

What I'm trying to do, is to merge both questions answers together based on ref. I want to do this so that I can get the number. I.e. question and answer for data_q1 is 2.

I have the following:

```<code>// Get questions
var questionData = data.data.questions;
var questions = JSON.parse(questionData);

// get answers
var answerData = data.data.answers;
var answers = JSON.parse(answerData);
</code>```

What I've tried:

```<code>var answersInfo = answers.map( function(order) {
if( answers.ref === "RefIDHere"){
     var info = { "number": answers.number}
     return info;
 }
});
console.log(answersInfo);
</code>```

However, the issue with the above is, in answers.ref ===, I don't know what to pass because the questions and answers haven't been mapped together yet.

答:
Please try below solution.

```<code>finalResult = [];
questions.map( que =>
  answers.map(ans =>  {
    if(que.ref === ans.field.ref){
      finalResult.push({question: que, answer: ans})
}
return finalResult;
}));
</code>```

Now if you see finalResult has the question and respective answer and you can access the number.
Hope it helps.

Body:
I have the following JSON:

data: {
    questions: "[{"id":"vzDDWL3GQvJi","title":"This is question 1","type":"opinion_scale","ref":"data_q1","properties":[]},{"id":"okT0ieWJm74d","title":"This  is question 2","type":"opinion_scale","ref":"data_q2","properties":[]},

    answers: "[{"type":"number","number":2,"field":{"id":"vzDDWL3GQvJi","type":"opinion_scale","ref":"data_q1"}},{"type":"number","number":4,"field":{"id":"okT0ieWJm74d","type":"opinion_scale","ref":"data_q2"}},

    createdDate: "2020-02-14T07:43:02.000000Z"
}

A tidy version of the above is:

Question (questions object)

title : "This is question 1"

ref" : "data_q1"

Answer (answers object)

ref" : "data_q1"

number : 2

So, for question 1 (with the ref: data_q1) the number (score) is 2.

What I'm trying to do, is to merge both questions answers together based on ref. I want to do this so that I can get the number. I.e. question and answer for data_q1 is 2.

I have the following:

// Get questions
var questionData = data.data.questions;
var questions = JSON.parse(questionData);

// get answers
var answerData = data.data.answers;
var answers = JSON.parse(answerData);

What I've tried:

var answersInfo = answers.map( function(order) {
if( answers.ref === "RefIDHere"){
     var info = { "number": answers.number}
     return info;
 }
});
console.log(answersInfo);

However, the issue with the above is, in answers.ref ===, I don't know what to pass because the questions and answers haven't been mapped together yet.

Body:
Please try below solution.

finalResult = [];
questions.map( que =>
  answers.map(ans =>  {
    if(que.ref === ans.field.ref){
      finalResult.push({question: que, answer: ans})
}
return finalResult;
}));

Now if you see finalResult has the question and respective answer and you can access the number.
Hope it helps.


问:
Correct offset of characters with extra white spaces, newlines, etc
I am trying to implement a simple solution for offset correction due to 'junk' characters, for example:

string_1 = "London is the capital of the UK"

-> chars location: ("capital", 14,21) and ("UK", 29, 31)

however, in the presence of newlines etc, the char location are changed:

string_2 = "London       is the\n\ncapital of the\n UK"

-> chars location ("capital" are (21,28)), ("UK" are (36,38))

moreover, the string may contain any number of newlines and other artefacts as well as any number of key-words.

My question is, given a text with extra characters (ASCII, newlines, etc), and some cleaning function, how to adjust the locations of certain keywords in the cleaned text?

```<code>string_2 = cleaning_txt(string_1)
</code>```

-> ("capital", 14, 21) --> ("capital", 21, 28)
-> ("UK", 29, 31) --> ("UK", 36, 38)

答:
You can clean your string this way:

```<code>x =  "London       is the\n\ncapital of the UK"
x = x.replace('\n','')

while '  ' in x:
    x = x.replace('  ', ' ')

print(x)
</code>```

Gives:

```<code>London is thecapital of the UK
</code>```

Body:
I am trying to implement a simple solution for offset correction due to 'junk' characters, for example:

string_1 = "London is the capital of the UK"

-> chars location: ("capital", 14,21) and ("UK", 29, 31)

however, in the presence of newlines etc, the char location are changed:

string_2 = "London       is the\n\ncapital of the\n UK"

-> chars location ("capital" are (21,28)), ("UK" are (36,38))

moreover, the string may contain any number of newlines and other artefacts as well as any number of key-words.

My question is, given a text with extra characters (ASCII, newlines, etc), and some cleaning function, how to adjust the locations of certain keywords in the cleaned text?

string_2 = cleaning_txt(string_1)

-> ("capital", 14, 21) --> ("capital", 21, 28)
-> ("UK", 29, 31) --> ("UK", 36, 38)

Body:
You can clean your string this way:

x =  "London       is the\n\ncapital of the UK"
x = x.replace('\n','')

while '  ' in x:
    x = x.replace('  ', ' ')

print(x)

Gives:

London is thecapital of the UK


答:
str.find works fine for this purpose:

```<code>string_1 = "London is the capital of the UK"
string_2 = "London       is the\n\ncapital of the UK"

def find_pos(s, match):
    pos = s.find(match)
    return (pos, pos+len(match))

match = 'capital'

find_pos(string_1, match)
# (14, 21)

find_pos(string_2, match)
# (21, 28)
</code>```

Title: Correct offset of characters with extra white spaces, newlines, etc

Body:
str.find works fine for this purpose:

string_1 = "London is the capital of the UK"
string_2 = "London       is the\n\ncapital of the UK"

def find_pos(s, match):
    pos = s.find(match)
    return (pos, pos+len(match))

match = 'capital'

find_pos(string_1, match)
# (14, 21)

find_pos(string_2, match)
# (21, 28)


问:
extract week number from date postgres
I would like to extract the week number as:

```<code>2015-52
</code>```

from a date formatted as:

```<code>2015-12-27
</code>```

How can I perform this in postgres?

my weeks are calculated from monday to sunday.

答:
I have done like this </p>

```<code>extract(week from cast(current_date as date))
extract(year from cast(current_date as date))
</code>```

Title: extract week number from date postgres

Body:
I would like to extract the week number as:

2015-52

from a date formatted as:

2015-12-27

How can I perform this in postgres?

my weeks are calculated from monday to sunday.

Body:
I have done like this

extract(week from cast(current_date as date))
extract(year from cast(current_date as date))


问:
Finding common parent in perfect binary tree
This question:

Are there any efficient ways to populate a balanced tree structure

provides an analytical way of labelling a perfect binary tree of any depth.

In such a tree, is there any way of efficiently figuring out the first common parent of an arbitrary subset of leaf nodes?

For instance, given the following binary tree:

I am looking for a way where if:

Input is: 3,5 Output is: 0

Input is: 3,5,6 Output is: 0

Input is: 3,4 Output is: 1

The only way I can think of is to traverse up to the root (0) from each of the given leaf nodes, and then picking the first common one.

Is there any closed-form analytical way in which the first common parent can be found?

答:
Let H be height of your tree, and let k be the count of numbers in your input.

If you start numeration of nodes from 1 and write nodes' binary numbers, it will become clear that number of every node is prefix of numbers of all its children. So, to find the lowest common ancestor, you should find the greatest common prefix (GCP) of binary numbers.

Let min and max be minimal and maximal numbers in your subset. If numbers in your input are already sorted, your can find min and max in O(1). If they are not sorted, you can find min and max in O(k * H).

The GCP of all binary numbers is the GCP of min and max - you can find it trivially in O(H).

This algorithm works in O(H) if numbers are sorted and in O(k * H) otherwise (although your algorithm works in O(k * H) too if you implement it right).


问:
Override the style in one component A when the component B is present on the view - Angular
I have two components on same level:

```<code><app-parent>
    <app-component-a></app-component-a>
    <app-component-b></app-component-b>
</app-parent>
</code>```

The Component-A:

Has the below styles:

```<code>:host {
    .container {
       height: 10rem;
     }
}
</code>```

But I want to change the height of the container of the component-a, when the component-b is present on the view. So let's say the componen-b is hidden and the default height from above can be used, but when component-b is visible the style below should override the height given above.

```<code>height: 2rem;
</code>```

Any suggestions on how to achieve this.
I have tried using the below on component-b but it doesn't help:

```<code>:host-context(.container) {
 height: 2rem;
}
</code>```

Have updated the code to make this work if there is a [ngClass] added to the code:

```<code><div class="container" [ngClass]="{'area-height': showArea}">
</code>```

Adding the Stackblitz code for easy understanding

Now I want to see if there is a way if we can connect the boolean value from the other component?
Added the random boolean generator, for now, to show/hide the component-b

答:
you can definitely achieve this using :host-content provided by angular. Let say we have two components - component A and component B as we have in your requirement.

I am hiding component B from a button present in App component. Just toggle a css class to an ancestor of Component A in response to toggle of component B. I have created a stackblitz for you,  please let me know if i am missing anything.

Stackblitz link- https://stackblitz.com/edit/angular-cfkqyt

Important code you need to look is below

// app component.ts

import {
  Component
} from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular';
  isBVisible = true;

  toggleB() {
    this.isBVisible = !this.isBVisible;
  }
}```
<pre class="snippet-code-css lang-css prettyprint-override"><code>/* Component A css */

.container-a {
  border: 1px solid red;
  height: 10rem;
}

/* apply css to component a if it finds bPresent css classname in its ancestor. */

:host-context(.bPresent) .container-a {
  height: 4rem;
}</code>```

<button (click)="toggleB()">toggle b</button>

  <comp-a></comp-a>

  <comp-b></comp-b>

```

:

Body:
I have two components on same level:

<app-parent>
    <app-component-a></app-component-a>
    <app-component-b></app-component-b>
</app-parent>

The Component-A:

Has the below styles:

:host {
    .container {
       height: 10rem;
     }
}

But I want to change the height of the container of the component-a, when the component-b is present on the view. So let's say the componen-b is hidden and the default height from above can be used, but when component-b is visible the style below should override the height given above.

height: 2rem;

Any suggestions on how to achieve this.
I have tried using the below on component-b but it doesn't help:

:host-context(.container) {
 height: 2rem;
}

Have updated the code to make this work if there is a [ngClass] added to the code:

Adding the Stackblitz code for easy understanding

Now I want to see if there is a way if we can connect the boolean value from the other component?
Added the random boolean generator, for now, to show/hide the component-b

Body:
you can definitely achieve this using :host-content provided by angular. Let say we have two components - component A and component B as we have in your requirement.

I am hiding component B from a button present in App component. Just toggle a css class to an ancestor of Component A in response to toggle of component B. I have created a stackblitz for you,  please let me know if i am missing anything.

Stackblitz link- https://stackblitz.com/edit/angular-cfkqyt

Important code you need to look is below

// app component.ts

import {
  Component
} from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular';
  isBVisible = true;

  toggleB() {
    this.isBVisible = !this.isBVisible;
  }
}

/* Component A css */

.container-a {
  border: 1px solid red;
  height: 10rem;
}

/* apply css to component a if it finds bPresent css classname in its ancestor. */

:host-context(.bPresent) .container-a {
  height: 4rem;
}

<!-- app component html -->

<button (click)="toggleB()">toggle b</button>
<div [class.bPresent]="isBVisible">
  <comp-a></comp-a>
</div>
<div *ngIf="isBVisible">
  <comp-b></comp-b>
</div>

:


问:
logback not generating new log file
I am using below logback.xml in my Micronaut project, it is not generating new log file as per rollingPolicy provided in xml configuration. I tried with SizeAndTimeBasedRollingPolicy and TimeBasedRollingPolicy but it did't worked.

Micronaut version: 1.2.2

logback-classic : 1.2.3

File: logback.xml

```<code><configuration>

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>logs/my-app.log</file>
        <encoder>
            <pattern>%cyan(%d{yyyy-MM-dd HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}):%line- %msg%n</pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.RollingFileAppender">
            <fileNamePattern>
                logs/my-app.log-%d{yyyy-MM-dd}-%i.log.gz
            </fileNamePattern>
            <maxHistory>30</maxHistory>
            <totalSizeCap>1MB</totalSizeCap>
        </rollingPolicy>

    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%cyan(%d{yyyy-MM-dd HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}):%line- %msg%n</pattern>
        </encoder>
    </appender>

    <root level="ALL">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
    </root>

</configuration>
</code>```

答:
If im not wrong, your maxHistory policy is preventing you from building more than one file. Instead it deletes the old one and just creates a new one for the new day or when the 1mb in size is reached. </p>

<p>maxHistory in this case has to be combined with the TimeBasedRollingPolicy as you stated and maxHistory is the parameter for days then. Without the TimeBasedRollingPolicy it might just be the number of files. </p>

Title: logback not generating new log file

Body:
I am using below logback.xml in my Micronaut project, it is not generating new log file as per rollingPolicy provided in xml configuration. I tried with SizeAndTimeBasedRollingPolicy and TimeBasedRollingPolicy but it did't worked.

Micronaut version: 1.2.2

logback-classic : 1.2.3

File: logback.xml

<configuration>

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>logs/my-app.log</file>
        <encoder>
            <pattern>%cyan(%d{yyyy-MM-dd HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}):%line- %msg%n</pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.RollingFileAppender">
            <fileNamePattern>
                logs/my-app.log-%d{yyyy-MM-dd}-%i.log.gz
            </fileNamePattern>
            <maxHistory>30</maxHistory>
            <totalSizeCap>1MB</totalSizeCap>
        </rollingPolicy>

    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%cyan(%d{yyyy-MM-dd HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}):%line- %msg%n</pattern>
        </encoder>
    </appender>

    <root level="ALL">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

Body:
If im not wrong, your maxHistory policy is preventing you from building more than one file. Instead it deletes the old one and just creates a new one for the new day or when the 1mb in size is reached.

maxHistory in this case has to be combined with the TimeBasedRollingPolicy as you stated and maxHistory is the parameter for days then. Without the TimeBasedRollingPolicy it might just be the number of files.


问:
Python "list order"
I have 2 troubles with my data, can anyone help me:

How can I get from this:

1.

```<code>k=[['1','7', 'U1'],
 ['1.5', '8', 'U1'],
 ['2', '5.5', 'U1']]
</code>```

get this

```<code>1,7,U1
1.5,8,U1
2,5.5,U1
</code>```

EDIT 2   I MAKE SOME CHANGE ON  second case:, still searching solution for this one:

2. And how to get, from this

```<code>l=array([[[ 4.24231542], 'U1'],
       [[ 3.41424819], 'U1'],
       [[ 2.17214734], 'U1'],], dtype=object)
</code>```

get

```<code>4.24231542,U1
3.41424819,U1
2.17214734,U1
</code>```

Thanks

答:
```<code>for a,b,c in k: print "%s,%s,%s" % (a,b,c)

for a,b in l: print "%s,%s" % (a[0],b)
</code>```

ContentLicense: CC BY-SA 3.0

Title: Python "list order"

Body:
I have 2 troubles with my data, can anyone help me:

How can I get from this:

1.

k=[['1','7', 'U1'],
 ['1.5', '8', 'U1'],
 ['2', '5.5', 'U1']]

get this

1,7,U1
1.5,8,U1
2,5.5,U1

EDIT 2   I MAKE SOME CHANGE ON  second case:, still searching solution for this one:

2. And how to get, from this

l=array([[[ 4.24231542], 'U1'],
       [[ 3.41424819], 'U1'],
       [[ 2.17214734], 'U1'],], dtype=object)

get

4.24231542,U1
3.41424819,U1
2.17214734,U1

Thanks


问:
How to compare 2 iframes and get difference visually?
Case :

I have 2 iframes and both have lot of divs and other controls so both iframes are like the medium size of HTML websites.  I want to compare both and find out differences.

I thought different options here :

Solution 1: Take a full screenshot of 2 iframes and compare both screenshots using the pillow library of Python which draws the grid on the mismatch area in a screenshot. But here the issue is I did not find any code on the internet which can take full iframe screenshots (I have a long iframe with a scroll bar). I tried almost all answers on SO but all are working for a normal page but not for the iframe.

Reference : https://blog.rinatussenov.com/automating-manual-visual-regression-tests-with-python-and-selenium-be66be950196

Solution 2: Get somehow all HTML code from both iframe and compare it, but this won't be easy to analyze result because it will find some HTML code that is different or have a mismatch in 2 iframes. This will be more like text compare and not a good solution I believe.

So I am looking for either code which can take a full screenshot of iframe using Python or Javascript OR some better option which allows me to compare 2 iframes and find out differences.

I tried almost all answers which google find our as per below :

Sample Iframe is given here where whole html is within iframe : https://grapesjs.com/demo.html , If some code can take full screenshot of this iframe then it will be easy to compare for me.

答:
Use html2canvas for taking screenshot

```<code>html2canvas(document.getElementById("img_dots")).then(
  function (canvas2) {
    var img_data2 = canvas2.toDataURL('image/png');
    var im_data2 = img_data2.replace('data:image/png;base64,', '');
    $.ajax({
      type: "POST",
      url: "send_image_to_backend",
      data: {
        "base64data": im_data2,
        // "filename": filename.split(".")[0]
        "filename": new_filename + ".png"
      }, success: function () {
         //send second image and compare
      }
    });
  }
);
</code>```

This will enable you to  send images to back-end.

Use this thread to tweak html2canvas to fetch entire image

Once you have both images you can use openCV to find difference between 2 images
you can refer to this - https://www.pyimagesearch.com/2017/06/19/image-difference-with-opencv-and-python/

Body:
Use html2canvas for taking screenshot

html2canvas(document.getElementById("img_dots")).then(
  function (canvas2) {
    var img_data2 = canvas2.toDataURL('image/png');
    var im_data2 = img_data2.replace('data:image/png;base64,', '');
    $.ajax({
      type: "POST",
      url: "send_image_to_backend",
      data: {
        "base64data": im_data2,
        // "filename": filename.split(".")[0]
        "filename": new_filename + ".png"
      }, success: function () {
         //send second image and compare
      }
    });
  }
);

This will enable you to  send images to back-end.

Use this thread to tweak html2canvas to fetch entire image

Once you have both images you can use openCV to find difference between 2 images
you can refer to this - https://www.pyimagesearch.com/2017/06/19/image-difference-with-opencv-and-python/


问:
data not passing inside function
I have created a function that passes multiple data from JSON using for-each and then called that inside render,  but am not getting any response inside that scop, any idea why is it so?

my function looks as follows:

```<code> color=(data)=>{
       this.state.data.colors.forEach(function(color){
        console.log(color)
        return(
          <View>
<TouchableOpacity>
            <Button value= {color.color} />
</TouchableOpacity>
          </View>
        )
       })
      }
</code>```

And I have called it inside my render function as follows:

```<code><View>
{this.color()}
<View>
</code>```

My logs shows this.state.data.colors as undefined inside this scope,can anyone please suggest what am doing wrong?

Any help would be useful, do tell me if you require anything else.

答:
To get the best help, please update your question with the colors array. Although you can try these.

1)
Your function this.color() is expecting an argument data. But when it was called in the Views component, no argument was passed.

Remove data from the argument passed into this.color(). I see you are not using it in your function.

2)
Confirm that your State is structured like so data.colors. Meaning the colors is an array is inside the data node. I suspect colors itself is the parent node not data. Hence, the right way to obtain your state values will be this.state.colors, not this.state.data.colors

3)
Confirm that color is a  key in the colors array.

i.e

```<code> colors:[{color: #00FFDE}, {color: #00FFDE}]
</code>```

Per your function, you are right! color.color is correct

But if it is like so

```<code>colors:[{name: #00FFDE}, {name: #00FFDE}]
</code>```

Per your function, the right thing will be color.name

Hope this helps!.

Title: data not passing inside function

Body:
I have created a function that passes multiple data from JSON using for-each and then called that inside render,  but am not getting any response inside that scop, any idea why is it so?

my function looks as follows:

 color=(data)=>{
       this.state.data.colors.forEach(function(color){
        console.log(color)
        return(
          <View>
<TouchableOpacity>
            <Button value= {color.color} />
</TouchableOpacity>
          </View>
        )
       })
      }

And I have called it inside my render function as follows:

<View>
{this.color()}
<View>

My logs shows this.state.data.colors as undefined inside this scope,can anyone please suggest what am doing wrong?

Any help would be useful, do tell me if you require anything else.

Body:
To get the best help, please update your question with the colors array. Although you can try these.

1)
Your function this.color() is expecting an argument data. But when it was called in the Views component, no argument was passed.

Remove data from the argument passed into this.color(). I see you are not using it in your function.

2)
Confirm that your State is structured like so data.colors. Meaning the colors is an array is inside the data node. I suspect colors itself is the parent node not data. Hence, the right way to obtain your state values will be this.state.colors, not this.state.data.colors

3)
Confirm that color is a  key in the colors array.

i.e

 colors:[{color: #00FFDE}, {color: #00FFDE}]

Per your function, you are right! color.color is correct

But if it is like so

colors:[{name: #00FFDE}, {name: #00FFDE}]

Per your function, the right thing will be color.name

Hope this helps!.


问:
Trivial C++ code passes in a test project and fails in the main one
These two trivial lines of code:

```<code>const bool equal = (HUGE_VALF == HUGE_VALF);
static_assert(equal == true, "Fatal error");
</code>```

in a test program work perfectly (no warnings and no errors).

When I copy them in the main project, I get this error on the assert:

error C2057: expected constant expression

Here the equal object is a constant with value = true.

Those lines of code are in both cases inside functions.

The two projects have identical settings (C++11, Visual C++ 2013 compiler, Visual C++ 2019 IDE, /W4, no optimizations).

What could be the reason of this different behaviour from the compiler?

答:
When a newer Visual Studio IDE (like VS 2019) loads an older project for the first time, it asks to retarget the project. If You have the old Visual Studio installed, You can reject this action. Then the compiler from the original VS will be used by the newer IDE.

After loading the solution, in the Solution Explorer next to the project name, You will have the Visual Studio which will be used to compile the project.
If the paranthesis is missing, that means the compiler and standard library from the current Visual Studio is been used.

As mentioned in the comments:

I have no parenthesis after the project name (if I am looking in the right place). However, I realised about the issue in the include file paths: Test Project: 14.24.28314\include\cmath; Main Project: Microsoft Visual Studio 12.0\VC\include\cmath, i.e. a much older version, that internally still relies on the C math.h header. So, same compiler (VS2013), different libraries...

This indicates that You have most probably retargeted the project or created a new project in VS 2019.

To make a project in VS 2019 make use of the older compiler, You need to got to the project Properties -> Configuration Properties -> General -> Platform Toolset and change it to the appropriate Visual Studio.

Also make sure that all configurations (Debug, Release,...) for all machine types (32bit, 64 bit) have this setting the same value.

Because the test project is using VS 2019 - it has all the required definitions - so it works.

So the issue is that most probably, VS 2013 standard library is not C++11 complete and does NOT have a definition for HUGE_VALF.
But it could contain a definition for HUGE_VAL

Body:
These two trivial lines of code:

const bool equal = (HUGE_VALF == HUGE_VALF);
static_assert(equal == true, "Fatal error");

in a test program work perfectly (no warnings and no errors).

When I copy them in the main project, I get this error on the assert:

error C2057: expected constant expression

Here the equal object is a constant with value = true.

Those lines of code are in both cases inside functions.

The two projects have identical settings (C++11, Visual C++ 2013 compiler, Visual C++ 2019 IDE, /W4, no optimizations).

What could be the reason of this different behaviour from the compiler?


问:
Create a file system corruption on a embedded device
I'm working on a project where we are using a file system (NAND Flash) on our device.

We noteiced that sometimes, when we have a power shut down or we are resseting the software (by debugger).

The file system is corrupted after restart.

Now we try to do several improvements, like adding journaling or garbage collection.

But my main question is now, how could I create a filesystem corruption by my own, to test if the new mechanism is working.

Currently it is very hard to reproduce the failure state and I want to find a clear way to reproduce the failure state.

Has anyone some hints?

答:
Techniques such as journaling protect against unplanned shutdown that would cause incomplete operations to leave data structures in an invalid state.  That is not quite the same as "corruption" more broadly.

A spontaneous modification of some random location asynchronous to and independent of file-system operations are unlikely to be detected or healed by journaling.  It is arguably an unlikely failure mode, and not the failure mode you are experiencing when you have a power loss.

NAND flash itself is inherently unreliable and NAND controllers have built-in error detection and correction means.

The point is that the techniques you have applied prevent the filesystem from becoming invalid rather then healing corruption, so simply corrupting the filesystem will not prove the effectiveness of your solution; it will merely corrupt the file system.

The "failure state" addresses by journaling is to have an incomplete operaton - a file opened for write to which data has been written but not closed at the point power-loss or reset.

Rather then try to invoke some kind of corruption, you might do better to replicate the conditions under which failure previously occurred, by having test code that continuously creates and writes files and verifies they are valid while asynchronously and randomly cycling the power (ideally through a relay or "smart" power-supply that can be controlled by a PC or the EUT itself for example), or simply random timer interrupt that issues a reset asynchronous to the file operations.

You need to understand how your journaling system works.  At its simplest it will simply leave the file in the state it was before it was opened - so it does not prevent data loss, merely filesystem data structure errors rendering files or the whole filesystem unusable.


问:
convert 2020-02-17 to ddd, MMM Do YYYY in angular
Hi am trying to convert convert 2020-02-17 to (ddd, MMM Do YYYY) using moment.js

```<code> transform(value: any, args?: any): any {
    console.log(value)
    console.log(moment(value).format("ddd, MMM Do YYYY"))
    return moment(value).format("ddd, MMM Do YYYY");
  }
</code>```

i have created a pipe for it but it is returning me the value based on current date
can somebody help

答: This happens if your value is sent blank or null check your value again.</p>

Body:
Hi am trying to convert convert 2020-02-17 to (ddd, MMM Do YYYY) using moment.js

 transform(value: any, args?: any): any {
    console.log(value)
    console.log(moment(value).format("ddd, MMM Do YYYY"))
    return moment(value).format("ddd, MMM Do YYYY");
  }

i have created a pipe for it but it is returning me the value based on current date
can somebody help


问:
CompanyHome reference in java API
I have a scheduler  job configured  and created an action class by extending org.quartz.StatefulJob

In execute method of Action Class (Shown below) ,  What would be the best way to get reference to  CompanyHome in execute method ?

My  objctive to create a file in company home directory , when the action invoke.  Any suggesion ?

答:
Please implement a method like this

```<code>public NodeRef getCompanyHomeNodeReference() {
        NodeRef companyHomeNodeRef = null;
        companyHomeNodeRef = (NodeRef)AuthenticationUtil.runAsSystem(new AuthenticationUtil.RunAsWork<Object>() {
            public Object doWork() throws Exception {
                StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore"));
                ResultSet rs = serviceRegistry.getSearchService().query(storeRef, SearchService.LANGUAGE_XPATH,
                        "/app:company_home");
                Object companyHome = null;
                try {
                    if (rs.length() == 0) {
                        LOG.error("Didn't find Company Home ");
                        throw new AlfrescoRuntimeException("Didn't find Company Home");
                    }
                    final NodeRef companyHomeNodeRef1 = rs.getNodeRef(0);
                    if(companyHomeNodeRef1 == null) {
                        LOG.info("Didn't find Company Homes");
                    }else {
                        companyHome = companyHomeNodeRef1;
                    }
                } finally {
                    rs.close();
                }
                return companyHome;
            }

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

import as below:

```<code>import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.repository.StoreRef;
</code>```

please see how the critical code is placed in AuthenticationUtil (this is very important).

And then use below code to create a file:

```<code>fileFolderService.create(companyNodeRef, "yourfilename", ContentModel.TYPE_CONTENT);
</code>```

Add this bean in service-context.xml

```<code><bean id="yourObj" class="MoveMonthlyDataAction">
        <property name="serviceRegistry">
            <ref bean="ServiceRegistry" />
        </property>
</bean>
</code>```

and mention in MoveMonthlyDataAction  . java as below,

```<code>public class MoveMonthlyDataAction {
    ServiceRegistry serviceRegistry;

    public void execute(){
        // your code
    }
    // getter and setter
}
</code>```

Hope this will help.

Title: CompanyHome reference in java API

Body:
Please implement a method like this

public NodeRef getCompanyHomeNodeReference() {
        NodeRef companyHomeNodeRef = null;
        companyHomeNodeRef = (NodeRef)AuthenticationUtil.runAsSystem(new AuthenticationUtil.RunAsWork<Object>() {
            public Object doWork() throws Exception {
                StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore"));
                ResultSet rs = serviceRegistry.getSearchService().query(storeRef, SearchService.LANGUAGE_XPATH,
                        "/app:company_home");
                Object companyHome = null;
                try {
                    if (rs.length() == 0) {
                        LOG.error("Didn't find Company Home ");
                        throw new AlfrescoRuntimeException("Didn't find Company Home");
                    }
                    final NodeRef companyHomeNodeRef1 = rs.getNodeRef(0);
                    if(companyHomeNodeRef1 == null) {
                        LOG.info("Didn't find Company Homes");
                    }else {
                        companyHome = companyHomeNodeRef1;
                    }
                } finally {
                    rs.close();
                }
                return companyHome;
            }

        });
        return companyHomeNodeRef;
    }

import as below:

import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.repository.StoreRef;

please see how the critical code is placed in AuthenticationUtil (this is very important).

And then use below code to create a file:

fileFolderService.create(companyNodeRef, "yourfilename", ContentModel.TYPE_CONTENT);

Add this bean in service-context.xml

<bean id="yourObj" class="MoveMonthlyDataAction">
        <property name="serviceRegistry">
            <ref bean="ServiceRegistry" />
        </property>
</bean>

and mention in MoveMonthlyDataAction  . java as below,

public class MoveMonthlyDataAction {
    ServiceRegistry serviceRegistry;

    public void execute(){
        // your code
    }
    // getter and setter
}

Hope this will help.


问:
Terraform import Kubernetes secret
I am trying to import an existing kubernetes secret into my Terraform state.

The secret resource should be located in a module, together with postgres resources. The idea is that I create the postgres user credentials and put them straight in a K8S secret.

The resource looks like this:

```<code>resource "kubernetes_secret" "database-credentials" {
  metadata {
    name = "${var.name}-database-credentials"
  }
  data = {
    username = "${var.name}_user"
    password = random_password.password.result
  }
  type = "Opaque"
}
</code>```

Then, I try to import it with the following command:
terraform import module.<module_name>.kubernetes_secret.database-credentials default/<existing-secret-name>

Now, the problem is that this command fails with the following error message:

```<code>Error: Error initializing PostgreSQL client: error detecting capabilities: error PostgreSQL version: pq: no PostgreSQL user name specified in startup packet
</code>```

I have no idea why the PostgreSQL client needs to do anything here or why it fails. There are postgres resources defined above the kubernetes secret resource config but I just want to import a k8s secret. I have defined all necessary environment variables needed. terraform apply works fine when I run it without the secret.

Could someone point me in the right direction please?

答:
Just figured it out myself.

https://github.com/terraform-providers/terraform-provider-postgresql/issues/2#issuecomment-567887609

TLDR: add expected_version = "<YOUR_POSTGRES_VERSION>" to the postgres provider block.

Title: Terraform import Kubernetes secret

Body:
I am trying to import an existing kubernetes secret into my Terraform state.

The secret resource should be located in a module, together with postgres resources. The idea is that I create the postgres user credentials and put them straight in a K8S secret.

The resource looks like this:

resource "kubernetes_secret" "database-credentials" {
  metadata {
    name = "${var.name}-database-credentials"
  }
  data = {
    username = "${var.name}_user"
    password = random_password.password.result
  }
  type = "Opaque"
}

Then, I try to import it with the following command:
terraform import module.<module_name>.kubernetes_secret.database-credentials default/<existing-secret-name>

Now, the problem is that this command fails with the following error message:

Error: Error initializing PostgreSQL client: error detecting capabilities: error PostgreSQL version: pq: no PostgreSQL user name specified in startup packet

I have no idea why the PostgreSQL client needs to do anything here or why it fails. There are postgres resources defined above the kubernetes secret resource config but I just want to import a k8s secret. I have defined all necessary environment variables needed. terraform apply works fine when I run it without the secret.

Could someone point me in the right direction please?


问:
How to make bootstrap 4 child element display block inside row class
I tried to make child div as display block using d-block class but it's not working bootstrap 4 row class will take display flex, I tried to achieve through clearfix but no result please anyone help me on this.
Please anyone help me on this

Column 3

Lorem ipsum dolor sit amet, consectetur adipisicing elit...

Ut enim ad minim veniam, quis nostrud exercitation ul

Lorem ipsum dolor sit amet, consectetur adipisicing elit...

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...

```

答:
I think you need it full width. You can set flex column in row parent by add flex-column class

```<code><div class="container">
  <div class="row flex-column">
    <div class="col-sm-4">
      <h3>Column 3</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
      <p>Ut enim ad minim veniam, quis nostrud exercitation ul</p>
    </div>
    <div class="row d-block p-0">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
      <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...</p>
    </div>
   </div>
 </div>
</code>```

Body:
I think you need it full width. You can set flex column in row parent by add flex-column class

<div class="container">
  <div class="row flex-column">
    <div class="col-sm-4">
      <h3>Column 3</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
      <p>Ut enim ad minim veniam, quis nostrud exercitation ul</p>
    </div>
    <div class="row d-block p-0">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
      <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...</p>
    </div>
   </div>
 </div>


问:
Flutter WEB, Layout Templates. pop returning void but expects Bool
I am trying to following along with this tutorial.
Progress is here:
https://github.com/vinceyoumans/flutweb001/tree/menu001

```<code>AssetNotFoundException: the_basics|lib/services/navigation_service.ddc.js
Error compiling dartdevc module:the_basics|lib/services/navigation_service.ddc.js

packages/the_basics/services/navigation_service.dart:12:38: Error: This expression has type 'void' and
can't be used.
    return navigatorKey.currentState.pop();
</code>```

The code is in:
/lib/services/navigation_services.dart

```<code>import 'package:flutter/material.dart';

class NavigationService {
  final GlobalKey<NavigatorState> navigatorKey =
      GlobalKey<NavigatorState>();

  Future<dynamic> navigateTo(String routeName) {
    return navigatorKey.currentState.pushNamed(routeName);
  }

  bool goBack() {
    return navigatorKey.currentState.pop();
  }
}
</code>```

The error I am getting:

A value of type 'void' can't be returned from method 'goBack' because it has a return type of 'bool'.dart(return_of_invalid_type)
from this line"

```<code>navigatorKey.currentState.pop();
</code>```

So it appears a void is being returned when it expects a Bool.   any thoughts on how to fix this?

答:
Also, you can change the return type of the function to void or simply remove it.</p>

```<code>goBack() {
    navigatorKey.currentState.pop();
  }
</code>```

Body:
I am trying to following along with this tutorial.
Progress is here:
https://github.com/vinceyoumans/flutweb001/tree/menu001

AssetNotFoundException: the_basics|lib/services/navigation_service.ddc.js
Error compiling dartdevc module:the_basics|lib/services/navigation_service.ddc.js

packages/the_basics/services/navigation_service.dart:12:38: Error: This expression has type 'void' and
can't be used.
    return navigatorKey.currentState.pop();

The code is in:
/lib/services/navigation_services.dart

import 'package:flutter/material.dart';

class NavigationService {
  final GlobalKey<NavigatorState> navigatorKey =
      GlobalKey<NavigatorState>();

  Future<dynamic> navigateTo(String routeName) {
    return navigatorKey.currentState.pushNamed(routeName);
  }

  bool goBack() {
    return navigatorKey.currentState.pop();
  }
}

The error I am getting:

A value of type 'void' can't be returned from method 'goBack' because it has a return type of 'bool'.dart(return_of_invalid_type)
from this line"

navigatorKey.currentState.pop();

So it appears a void is being returned when it expects a Bool.   any thoughts on how to fix this?

Body:
Also, you can change the return type of the function to void or simply remove it.

goBack() {
    navigatorKey.currentState.pop();
  }


问:
Should I normalize this table?
I have a table Items which stores fetched book data from Amazon.  This Amazon data is inserted into Items as users browse the site, so any INSERT that occurs needs to be efficient.

Here's the table:

```<code>CREATE TABLE IF NOT EXISTS `items` (
  `Item_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Item_ISBN` char(13) DEFAULT NULL,
  `Title` varchar(255) NOT NULL,
  `Edition` varchar(20) DEFAULT NULL,
  `Authors` varchar(255) DEFAULT NULL,
  `Year` char(4) DEFAULT NULL,
  `Publisher` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`Item_ID`),
  UNIQUE KEY `Item_Data` (`Item_ISBN`,`Title`,`Edition`,`Authors`,`Year`,`Publisher`),
  KEY `ISBN` (`Item_ISBN`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT AUTO_INCREMENT=1 ;
</code>```

Normalizing this table would presumably mean creating tables for Titles, Authors, and Publishers.  My concern with doing this is that the insert would become too complex.. To insert a single Item, I'd have to:

Check for the Publisher in Publishers to SELECT Publisher_ID, otherwise insert it and use mysql_insert_id() to get Publisher_ID.

Check for the Authors in Authors to SELECT Authors_ID, otherwise insert it and use mysql_insert_id() to get Authors_ID.

Check for the Title in Titles to SELECT Title_ID, otherwise insert it and use mysql_insert_id() to get Title_ID.

Use those ID's to finally insert the Item (which may in fact be a duplicate, so this whole process would have been a waste..)

Does that argue against normalization for this table?

Note: The goal of Items is not to create a comprehensive database of books, so that a user would say "Show me all the books by Publisher X."  The Items table is just used to cache Items for my users' search results.

答: You've answered your own question - don't normalize it!</p>

Title: Should I normalize this table?

Body:
I have a table Items which stores fetched book data from Amazon.  This Amazon data is inserted into Items as users browse the site, so any INSERT that occurs needs to be efficient.

Here's the table:

CREATE TABLE IF NOT EXISTS `items` (
  `Item_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Item_ISBN` char(13) DEFAULT NULL,
  `Title` varchar(255) NOT NULL,
  `Edition` varchar(20) DEFAULT NULL,
  `Authors` varchar(255) DEFAULT NULL,
  `Year` char(4) DEFAULT NULL,
  `Publisher` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`Item_ID`),
  UNIQUE KEY `Item_Data` (`Item_ISBN`,`Title`,`Edition`,`Authors`,`Year`,`Publisher`),
  KEY `ISBN` (`Item_ISBN`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT AUTO_INCREMENT=1 ;

Normalizing this table would presumably mean creating tables for Titles, Authors, and Publishers.  My concern with doing this is that the insert would become too complex.. To insert a single Item, I'd have to:

Check for the Publisher in Publishers to SELECT Publisher_ID, otherwise insert it and use mysql_insert_id() to get Publisher_ID.

Check for the Authors in Authors to SELECT Authors_ID, otherwise insert it and use mysql_insert_id() to get Authors_ID.

Check for the Title in Titles to SELECT Title_ID, otherwise insert it and use mysql_insert_id() to get Title_ID.

Use those ID's to finally insert the Item (which may in fact be a duplicate, so this whole process would have been a waste..)

Does that argue against normalization for this table?

Note: The goal of Items is not to create a comprehensive database of books, so that a user would say "Show me all the books by Publisher X."  The Items table is just used to cache Items for my users' search results.


问:
Making HTTP request and receiving multipart/x-mixed-replace response in Node.js
I need to make a HTTP GET request to foreign server to start receiving events. After request I immediately get multipart/x-mixed-replace response. When an event occurs, it's sent as XML message together with boundary indicating the end of this part.

Now I must implement it in Node.js. With normal request I use node-rest-client, call its get() method and put my logic in method's callback. Trouble is that callback is executed only when response is finished and with multipart/x-mixed-replace it isn't until connection closes.

Is there some other NPM module that does the trick? I searched NPM registry but results I found seem unappropriate to the task. Or is it better to do it in pure Node? I so, please provide an example.

答:
Here is my own implementation:

const webStream = {
    get: function (url, callback) {
        let webClient;

        if (url.startsWith("http://")) {
            webClient = require("http");
        } else if (url.startsWith("https://")) {
            webClient = require("https");
        } else {
            throw "Unsupported protocol.";
        }

        let clientRequest = webClient.get(url, function (response) {
            let context = {
                url: url,
                boundary: "",
                contentType: "",
                contentLength: 0
            };

            let headersCompleted = false;
            let bodyCompleted = false;
            let buffer = null;
            let receivedBodyChunk = 0;

            response.on("data", function (chunk) {
                if (!headersCompleted) {
                    let headers = chunk.toString().split(/\r?\n/);

                    context.boundary = headers[0].substring(2);
                    context.contentType = headers[1].split(":")[1].trim();
                    context.contentLength = parseInt(headers[2].split(":")[1]);

                    buffer = Buffer.alloc(context.contentLength);

                    headersCompleted = true;
                } else {
                    if (!bodyCompleted) {
                        if (receivedBodyChunk < context.contentLength) {
                            chunk.copy(buffer, receivedBodyChunk, 0, chunk.byteLength);

                            receivedBodyChunk += chunk.byteLength;

                            if (receivedBodyChunk === context.contentLength) {
                                bodyCompleted = true;
                            }
                        }
                    }

                    if (bodyCompleted) {
                        callback(buffer, context);

                        headersCompleted = false;
                        bodyCompleted = false;
                        buffer = null;
                        receivedBodyChunk = 0;
                    }
                }
            });
        });

        return {
            url: url,
            handler: clientRequest,
            on: function (type, listener) {
                clientRequest.on(type, listener);
            },
            abort: function () {
                clientRequest.abort();
            }
        };
    }
};

let stream = webStream.get("http://127.0.0.1:8090/", function (data, context) {
    // data: Received content (Buffer)
    // context: { url, boundary, contentType, contentLength }

    // TODO: Do something here...
});

// stream.abort();

// stream.on("error", function(e) {
//     console.log("Error: " + e.message);
// });
```


问:
How to retrieve value from json output
I would like to retrieve transcript value from below shared output of console.log.But only that transcript value required which has another key is confidence.

Output of console.log :

```<code>  Data: {
    "results": [
    {
    "keywords_result": {
    "blocked": [
    {
    "normalized_text": "blocked",
    "start_time": 1.1,
    "confidence": 0.64,
    "end_time": 1.68
    }
    ]
    },
    "alternatives": [
    {
    "confidence": 0.81,
    "transcript": "your Name is Charlie "
    },
    {
    "transcript": "your Name is Charlie and "
    },
    {
    "transcript": "your Name is Charlies "
    }
    ],
    "final": true
    }
    ],
    "result_index": 0
    }
    Close: 1000
</code>```

Expected output:

```<code>your Name is Charlie
</code>```

Please note, I am expected value should be come from console.log output to a variable which I can use further.

答:
Please refer below code.

var jsonData = {
    "results": [{
        "keywords_result": {
            "blocked": [{
                "normalized_text": "blocked",
                "start_time": 1.1,
                "confidence": 0.64,
                "end_time": 1.68
            }]
        },
        "alternatives": [{
                "confidence": 0.81,
                "transcript": "your Name is Charlie "
            },
            {
                "transcript": "your Name is Charlie and "
            },
            {
                "transcript": "your Name is Charlies "
            }
        ],
        "final": true
    }],
    "result_index": 0
};
for (var i = 0; i < jsonData.results[0].alternatives.length; i++) {
    if (jsonData.results[0].alternatives[i].confidence > 0) {
        console.log(jsonData.results[0].alternatives[i].transcript);
    }
}```

Title: How to retrieve value from json output

Body:
I would like to retrieve transcript value from below shared output of console.log.But only that transcript value required which has another key is confidence.

Output of console.log :

  Data: {
    "results": [
    {
    "keywords_result": {
    "blocked": [
    {
    "normalized_text": "blocked",
    "start_time": 1.1,
    "confidence": 0.64,
    "end_time": 1.68
    }
    ]
    },
    "alternatives": [
    {
    "confidence": 0.81,
    "transcript": "your Name is Charlie "
    },
    {
    "transcript": "your Name is Charlie and "
    },
    {
    "transcript": "your Name is Charlies "
    }
    ],
    "final": true
    }
    ],
    "result_index": 0
    }
    Close: 1000

Expected output:

your Name is Charlie

Please note, I am expected value should be come from console.log output to a variable which I can use further.


问:
Docker networking not working with eureka
I have one eureka server inside a docker container and another application inside another docker container. I have made a network using docker network create eureka-s1. So, I'm running the eureka server using the command: docker run --network eureka-s1 --name eureka-server -p 8761:8761 f595acb214d5
The config file of the eureka server is as follows:

```<code>spring:
  application:
    name: discovery-service

eureka:
  instance:
    prefer-ip-address:true
    hostname:localhost

  client:
    eureka-server-connect-timeout-seconds: 5
    enabled: true
    fetch-registry: false
    register-with-eureka: false

server:
  port: 8761
</code>```

So, its running fine. Now, when I run my application using the command: docker run --network eureka-s1 --name first-service -p 8080:8080 c16b2442b890, the application is not registering to eureka. The config file of the client is as follows:

```<code>spring:
  application:
    name: first-service
server:
  port: 8080
eureka:
  client:
    fetch-registry: true
    serviceUrl:
      defaultZone: http://eureka-server:8761/eureka
</code>```

Can someone point out the problem here? Thanks.

Edit:
 Eureka Server Dockerfile:

```<code>FROM openjdk:8
ADD target/cloud-eureka-discovery-service-1.0.0-SNAPSHOT.jar cloud-eureka-discovery-service-1.0.0-SNAPSHOT.jar
EXPOSE 8761
ENTRYPOINT ["java", "-jar", "cloud-eureka-discovery-service-1.0.0-SNAPSHOT.jar"]
</code>```

Eureka Client Dockerfile:

```<code>FROM openjdk:8
ADD target/first-service-0.0.1-SNAPSHOT.jar first-service-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "first-service-0.0.1-SNAPSHOT.jar"]
</code>```

答:
It's work fine with docker-compose.

Below is the docker-compose.yml

```<code>version: '2.1'

services:
  eureka:
    build: ${PATH_TO_EUREKA_FOLDER}
    mem_limit: 350m
    networks:
        - my-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://eureka:8761"]
      interval: 30s
      timeout: 10s
      retries: 5

  client:
    build: ${PATH_TO_YOUR_CLIENT_FOLDER}
    mem_limit: 350m
    networks:
      - my-network
    links:
      - eureka
    depends_on:
      eureka:
        condition: service_healthy

networks:
  my-network:
    name: my-network
</code>```

And add update your client config file as bellow :

```<code>spring:
    application:
        name: first-service
server:
    port: 8080
eureka:
    client:
        fetch-registry: true
        serviceUrl:
            defaultZone: http://eureka:8761/eureka
</code>```

And update the Dockerfile of the eureka server as bellow :

```<code>FROM openjdk:8

RUN apt-get update
RUN apt-get install -y curl

EXPOSE 8761

ADD ./target/*.jar app.jar

ENTRYPOINT ["java","-jar","/app.jar"]
</code>```

And next to docker-compose.yml run

```<code>docker-compose up --build
</code>```

And don't forget to run mvn clean install your client application.

Body:
I have one eureka server inside a docker container and another application inside another docker container. I have made a network using docker network create eureka-s1. So, I'm running the eureka server using the command: docker run --network eureka-s1 --name eureka-server -p 8761:8761 f595acb214d5
The config file of the eureka server is as follows:

spring:
  application:
    name: discovery-service

eureka:
  instance:
    prefer-ip-address:true
    hostname:localhost

  client:
    eureka-server-connect-timeout-seconds: 5
    enabled: true
    fetch-registry: false
    register-with-eureka: false

server:
  port: 8761

So, its running fine. Now, when I run my application using the command: docker run --network eureka-s1 --name first-service -p 8080:8080 c16b2442b890, the application is not registering to eureka. The config file of the client is as follows:

spring:
  application:
    name: first-service
server:
  port: 8080
eureka:
  client:
    fetch-registry: true
    serviceUrl:
      defaultZone: http://eureka-server:8761/eureka

Can someone point out the problem here? Thanks.

Edit:
 Eureka Server Dockerfile:

FROM openjdk:8
ADD target/cloud-eureka-discovery-service-1.0.0-SNAPSHOT.jar cloud-eureka-discovery-service-1.0.0-SNAPSHOT.jar
EXPOSE 8761
ENTRYPOINT ["java", "-jar", "cloud-eureka-discovery-service-1.0.0-SNAPSHOT.jar"]

Eureka Client Dockerfile:

FROM openjdk:8
ADD target/first-service-0.0.1-SNAPSHOT.jar first-service-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "first-service-0.0.1-SNAPSHOT.jar"]

Body:
It's work fine with docker-compose.

Below is the docker-compose.yml

version: '2.1'

services:
  eureka:
    build: ${PATH_TO_EUREKA_FOLDER}
    mem_limit: 350m
    networks:
        - my-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://eureka:8761"]
      interval: 30s
      timeout: 10s
      retries: 5

  client:
    build: ${PATH_TO_YOUR_CLIENT_FOLDER}
    mem_limit: 350m
    networks:
      - my-network
    links:
      - eureka
    depends_on:
      eureka:
        condition: service_healthy

networks:
  my-network:
    name: my-network

And add update your client config file as bellow :

spring:
    application:
        name: first-service
server:
    port: 8080
eureka:
    client:
        fetch-registry: true
        serviceUrl:
            defaultZone: http://eureka:8761/eureka

And update the Dockerfile of the eureka server as bellow :

FROM openjdk:8

RUN apt-get update
RUN apt-get install -y curl

EXPOSE 8761

ADD ./target/*.jar app.jar

ENTRYPOINT ["java","-jar","/app.jar"]

And next to docker-compose.yml run

docker-compose up --build

And don't forget to run mvn clean install your client application.


问:
Setting Linux Capabilities in Yocto ext4 Image
I intend to set some capabilities on binaries included in a Yocto image using "setcap". For some reason the solutions mentioned here did not work for me:
Linux capabilities with yocto
. I have checked that by running "getcap" on my binary within the rootfs creation directory:

```<code>getcap ${IMAGE_ROOTFS}/usr/bin/mybinary
</code>```

does not return anything. Nor do I find the capabilities in the final running sdcard image.

Next I tried the approach using IMAGE_PREPROCESS_COMMAND. I wrapped up setcap commands in small shell functions such as:

```<code>my_setcap_function() {
    sudo setcap cap_ipc_owner+ep ${IMAGE_ROOTFS}/usr/bin/mybinary
}
</code>```

and append the function names to IMAGE_PREPROCESS_COMMAND. This works to the extent that now running getcap on my binary within the {IMAGE_ROOTFS} directory does show the correct caps set. However I still do not get the capabilities in the final running sdcard image.

Also if I mount the rootfs ext4 (which is used to create the final sdcard image) on a directory using -o loop, I do not see the capabilities on my binary.
It seems to me that the capabilitiess somehow get lost when the ext4 is created using mkfs.ext4.

I had to attach sudo to setcap because otherwise it complains saying "unable to set CAP_SETFCAP effective capability: Operation not permitted". Although my understanding was that IMAGE_PREPROCESS_COMMAND commands are run using fakeroot so this sudo should not be required.

So, to summarize my question:

How can I get the capabilities on the sdcard image made using ext4 rootfs image?

I want to use a way that does not require using "sudo".

I am using Yocto Krogoth and currently cannot upgrade that.

答:
Did you really test it on the final image or in the rootfs folder from the yocto build?

I run getcap on the files in the rootfs folder, and there where nothing set.

Because yocto uses pseudo lib to intercept chown, chmod calls, track them in a sqlite db (uses LD_PRELOAD for interception).

So this attributes are not set for the files in the "rootfs" folder, however added at image/rootfs creation.

you can use setcap in recipe, you have to add

```<code>DEPENDS = "libcap-native"
</code>```

in your recipe.

Body:
I intend to set some capabilities on binaries included in a Yocto image using "setcap". For some reason the solutions mentioned here did not work for me:
Linux capabilities with yocto
. I have checked that by running "getcap" on my binary within the rootfs creation directory:

getcap ${IMAGE_ROOTFS}/usr/bin/mybinary

does not return anything. Nor do I find the capabilities in the final running sdcard image.

Next I tried the approach using IMAGE_PREPROCESS_COMMAND. I wrapped up setcap commands in small shell functions such as:

my_setcap_function() {
    sudo setcap cap_ipc_owner+ep ${IMAGE_ROOTFS}/usr/bin/mybinary
}

and append the function names to IMAGE_PREPROCESS_COMMAND. This works to the extent that now running getcap on my binary within the {IMAGE_ROOTFS} directory does show the correct caps set. However I still do not get the capabilities in the final running sdcard image.

Also if I mount the rootfs ext4 (which is used to create the final sdcard image) on a directory using -o loop, I do not see the capabilities on my binary.
It seems to me that the capabilitiess somehow get lost when the ext4 is created using mkfs.ext4.

I had to attach sudo to setcap because otherwise it complains saying "unable to set CAP_SETFCAP effective capability: Operation not permitted". Although my understanding was that IMAGE_PREPROCESS_COMMAND commands are run using fakeroot so this sudo should not be required.

So, to summarize my question:

How can I get the capabilities on the sdcard image made using ext4 rootfs image?

I want to use a way that does not require using "sudo".

I am using Yocto Krogoth and currently cannot upgrade that.

Body:
Did you really test it on the final image or in the rootfs folder from the yocto build?

I run getcap on the files in the rootfs folder, and there where nothing set.

Because yocto uses pseudo lib to intercept chown, chmod calls, track them in a sqlite db (uses LD_PRELOAD for interception).

So this attributes are not set for the files in the "rootfs" folder, however added at image/rootfs creation.

you can use setcap in recipe, you have to add

DEPENDS = "libcap-native"

in your recipe.


问:
Concurrent Fwrite in PHP?
I have a fwrite function to write log for delay insert to the database.

We have a visit rate of 20,000 Visits per hour.

so it's 0.18s per fwrite.

My question is that is it possible that PHP will miss several log when there are 2 or 3 visitor come in at the same time?

If it's then how can I make this concurrent?

My code is just normal Fopen fwrite fclose.

答:
I would avoid this if I was you.  If you must log something for each request, either use something like Gearman to queue the data for later processing, or store it in a database (or a NoSQL db) for later processing.  Don't forget that databases were designed to solve this very problem.  Don't try to re-invent a db using log files.

Not to mention that 0.18s per write is likely a lot more expensive than an insert into a DBM (MySQL for me typically returns writes inside of 0.01 seconds, depending on the table, MongoDB can be a LOT faster).

Title: Concurrent Fwrite in PHP?


问:
Getting NullPointerException in getDrawable method on Glide
I have an imageview and I'll like to get the Bitmap of it...

Here is my code:

Setting the imageview:

```<code>ImageView imageView;
imageView =  findViewById(R.id.image_wallpaper);
Glide.with(this).load(current_image.getImageURL()).into(imageView);
</code>```

Getting the bitmap:

```<code>setButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        DisplayMetrics metrics = new DisplayMetrics();

        imageView.invalidate();  // I get the crash with or without that line
        Bitmap mBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();  // CRASH HERE

        // more things using the mBitmap

    }
});
</code>```

The crash log:

```<code>Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
com.XXXX.XXXXXXX.ImageActivity$4.onClick (ImageActivity.java:170)
android.view.View.performClick (View.java:5246)
android.view.View$PerformClick.run (View.java:21200)
android.os.Handler.handleCallback (Handler.java:739)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:145)
android.app.ActivityThread.main (ActivityThread.java:6946)
java.lang.reflect.Method.invoke (Method.java)
java.lang.reflect.Method.invoke (Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1404)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1199)
</code>```

I'm using last version of Glide:

```<code>implementation 'com.github.bumptech.glide:glide:4.9.0'                   // glide
</code>```

This crash log comes from Firebase Crashlytics, a lot of users get that error, but I'm unable to reproduce it on my devices.

What's the problem? How can I fix it?

Thanks

答:
Add dependency :

```<code>implementation 'com.github.bumptech.glide:glide:3.7.0'
</code>```

and write below code snippet:

```<code>                             try {
                                Glide.with(getApplicationContext())
                                        .load(YOUR_IMAGE_URL)
                                        .asBitmap()
                                        .into(new SimpleTarget<Bitmap>() {
                                            @Override
                                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                                Uri bmpUri = Common.getLocalBitmapUri(resource, context);
                                    your_image_view.setImageBitmap(resource);

                                            }

                                            @Override
                                            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                                                super.onLoadFailed(e, errorDrawable);
                                            }
                                        });
                            } catch (Exception e) {
                                e.getStackTrace();
                            }
</code>```

Hope this may helps you

Body:
I have an imageview and I'll like to get the Bitmap of it...

Here is my code:

Setting the imageview:

ImageView imageView;
imageView =  findViewById(R.id.image_wallpaper);
Glide.with(this).load(current_image.getImageURL()).into(imageView);

Getting the bitmap:

setButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        DisplayMetrics metrics = new DisplayMetrics();

        imageView.invalidate();  // I get the crash with or without that line
        Bitmap mBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();  // CRASH HERE

        // more things using the mBitmap

    }
});

The crash log:

Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
com.XXXX.XXXXXXX.ImageActivity$4.onClick (ImageActivity.java:170)
android.view.View.performClick (View.java:5246)
android.view.View$PerformClick.run (View.java:21200)
android.os.Handler.handleCallback (Handler.java:739)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:145)
android.app.ActivityThread.main (ActivityThread.java:6946)
java.lang.reflect.Method.invoke (Method.java)
java.lang.reflect.Method.invoke (Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1404)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1199)

I'm using last version of Glide:

implementation 'com.github.bumptech.glide:glide:4.9.0'                   // glide

This crash log comes from Firebase Crashlytics, a lot of users get that error, but I'm unable to reproduce it on my devices.

What's the problem? How can I fix it?

Thanks

Body:
Add dependency :

implementation 'com.github.bumptech.glide:glide:3.7.0'

and write below code snippet:

                             try {
                                Glide.with(getApplicationContext())
                                        .load(YOUR_IMAGE_URL)
                                        .asBitmap()
                                        .into(new SimpleTarget<Bitmap>() {
                                            @Override
                                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                                Uri bmpUri = Common.getLocalBitmapUri(resource, context);
                                    your_image_view.setImageBitmap(resource);

                                            }

                                            @Override
                                            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                                                super.onLoadFailed(e, errorDrawable);
                                            }
                                        });
                            } catch (Exception e) {
                                e.getStackTrace();
                            }

Hope this may helps you


问:
UML for multithreading dataflow
I want to paint a diagram where you can see the dataflow of a java program, and if there are one or multiple threads handling the data.

Sequence charts don't show multithreading and get very confusion when you have more than 5 different entities.

Class diagrams are good for showing the relations but the dataflow is missing.

I would like to paint boxes (like class/package diagram) and connect them. Maybe having a box doubled and ... in between to show that there can be more than one thread.

What diagram does UML have for this kind of dataflow diagram?

What editor is good for that diagram? If possible freeware or open source.

答: If you want to visualize multiple threads' states (like many profilers do), take a look at PlantUML timing diagrams. It isn't ideal tool for showing many inter-thread communications but suits well for showing transitions between threads' states:

Title: UML for multithreading dataflow