问:
No module named 'encodings' on OpenSuse
A whole host of actions keep returning to this problem:

pip install encodings

Fatal Python error: Py_Initialize: Unable to get the locale encoding

ModuleNotFoundError: No module named 'encodings'

python3

Fatal Python error: Py_Initialize: Unable to get the locale encoding

ModuleNotFoundError: No module named 'encodings'

libreoffice --safe-mode

Fatal Python error: Py_Initialize: Unable to get the locale encoding

ModuleNotFoundError: No module named 'encodings'

zypper se python |grep '^i '

```<code>i  | libpython3_6m1_0                                         | Python Interpreter shared library                                                     | package
i  | libsamba-policy0-python3                                 | Active Directory Group Policy library                                                 | package
i  | python-qt3d-qt5-sip                                      | Sip files for python-qt3d-qt5                                                         | package
i  | python-qt5-utils                                         | Common files for PyQt5 for python2 and python3                                        | package
i  | python-qtdatavis3d-qt5-sip                               | Sip files for python-qtdatavis3d-qt5                                                  | package
i  | python-qtwebengine-qt5-sip                               | Sip files for python-qtwebengine-qt5                                                  | package
i  | python-rpm-generators                                    | Dependency generator dependencies for Python RPMs                                     | package
i  | python-rpm-macros                                        | RPM macros for building of Python modules                                             | package
i  | python-sip-common                                        | SIP tool to create python bindings -- common files                                    | package
i  | python-websockify-common                                 | Common data files for the Websockify TCP proxy/bridge                                 | package
i  | python3-Automat                                          | Self-service finite-state machines for the programmer on the go                       | package
i  | python3-Babel                                            | Internationalization utilities                                                        | package
i  | python3-Bottleneck                                       | A collection of fast NumPy array functions                                            | package
i  | python3-Brotli                                           | Python bindings for the Brotli compression library                                    | package
i  | python3-Cycler                                           | Composable style cycles                                                               | package
i  | python3-Cython                                           | The Cython compiler for writing C extensions for the Python language                  | package
i  | python3-Genshi                                           | A toolkit for generation of output for the web                                        | package
i  | python3-HeapDict                                         | A heap with decrease-key and increase-key operations                                  | package
i  | python3-Jinja2                                           | A fast and easy to use template engine written in pure Python                         | package
i  | python3-MarkupSafe                                       | Implements a XML/HTML/XHTML Markup safe string for Python                             | package
i  | python3-Pillow                                           | Python Imaging Library (Fork)                                                         | package
i  | python3-PyHamcrest                                       | Hamcrest framework for matcher objects                                                | package
i  | python3-PyJWT                                            | JSON Web Token implementation in Python                                               | package
i  | python3-PyNaCl                                           | Python binding to the Networking and Cryptography (NaCl) library                      | package
i  | python3-PySocks                                          | A Python SOCKS client module                                                          | package
i  | python3-PyWavelets                                       | PyWavelets is a Python wavelet transforms module                                      | package
i  | python3-PyYAML                                           | YAML parser and emitter for Python                                                    | package
i  | python3-Pygments                                         | A syntax highlighting package written in Python                                       | package
i  | python3-QtPy                                             | Abstraction layer on top of Qt bindings                                               | package
i  | python3-SQLAlchemy                                       | Database Abstraction Library                                                          | package
etc...
</code>```

I think I've only got python 3.6 installed but the only suggestion I've seen is where there is more than one  version installed.

Any suggestions on what I should test now to find the source of this problem?

My Env:

uname -a

Linux localhost.localdomain 5.3.18-150300.59.49-preempt #1 SMP PREEMPT Mon Feb 7 14:40:20 UTC 2022 (77d9d02) x86_64 x86_64 x86_64 GNU/Linux

python

-bash: /usr/bin/python: No such file or directory

python3

Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> (then exited from python console)

echo $PYTHONHOME returns nothing

echo $PYTHONPATH returns nothing

I have subsequently reinstalled the OS with no different result (surprising to me) and I've tried strace -o pythonstrace.txt python3.6 The (presumably) relevant part of the strace output is:

```<code>munmap(0x7f780442e000, 262144)          = 0
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f780442e000
fstat(0, {st_mode=S_IFCHR|0600, st_rdev=makedev(0x88, 0x7), ...}) = 0
stat("/sbin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/sbin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/local/sbin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/root/bin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/local/bin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/bin/python3.6", {st_mode=S_IFREG|0755, st_size=10472, ...}) = 0
readlink("/usr/bin/python3.6", 0x7ffff6517fa0, 4096) = -1 EINVAL (Invalid argument)
openat(AT_FDCWD, "/usr/bin/pyvenv.cfg", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/pyvenv.cfg", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/bin/Modules/Setup", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/bin/lib64/python3.6/os.py", 0x7ffff6518f40) = -1 ENOENT (No such file or directory)
stat("/usr/bin/lib64/python3.6/os.pyc", 0x7ffff6518f40) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/os.py", {st_mode=S_IFREG|0644, st_size=37526, ...}) = 0
stat("/usr/bin/pybuilddir.txt", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/bin/lib64/python3.6/lib-dynload", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/lib-dynload", {st_mode=S_IFDIR|0755, st_size=4802, ...}) = 0
brk(0x55a4968ff000)                     = 0x55a4968ff000
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f78043ee000
sysinfo({uptime=132115, loads=[4256, 3872, 448], totalram=16583553024, freeram=1984724992, sharedram=68530176, bufferram=122511360, totalswap=2146430976, freeswap=2146430976, procs=990, totalhigh=0, freehigh=0, mem_unit=1}) = 0
brk(0x55a496921000)                     = 0x55a496921000
sigaltstack({ss_sp=0x55a4968d9c20, ss_flags=0, ss_size=8192}, {ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=0}) = 0
stat("/usr/lib/python36.zip", 0x7ffff6529bd0) = -1 ENOENT (No such file or directory)
stat("/usr/lib", {st_mode=S_IFDIR|0755, st_size=10808, ...}) = 0
stat("/usr/lib/python36.zip", 0x7ffff65293b0) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6", {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
stat("/usr/lib64/python3.6", {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
stat("/usr/lib64/python3.6", {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3
fstat(3, {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
getdents64(3, /* 204 entries */, 32768) = 6800
getdents64(3, /* 0 entries */, 32768)   = 0
close(3)                                = 0
stat("/usr/lib64/python3.6/encodings/__init__.cpython-36m-x86_64-linux-gnu.so", 0x7ffff65296f0) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/encodings/__init__.abi3.so", 0x7ffff65296f0) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/encodings/__init__.so", 0x7ffff65296f0) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/encodings/__init__.py", {st_mode=S_IFREG|0644, st_size=5642, ...}) = 0
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f78043ae000
stat("/usr/lib64/python3.6/encodings/__init__.py", {st_mode=S_IFREG|0644, st_size=5642, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/encodings/__pycache__/__init__.cpython-36.pyc", O_RDONLY|O_CLOEXEC) = 3
fcntl(3, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
fstat(3, {st_mode=S_IFREG|0644, st_size=3922, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=3922, ...}) = 0
read(3, "3\r\r\n@*\273a\n\26\0\0\343\0\0\0\0\0\0\0\0\0\0\0\0\5\0\0\0@\0\0"..., 3923) = 3922
read(3, "", 1)                          = 0
close(3)                                = 0
stat("/usr/lib64/python3.6", {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
stat("/usr/lib64/python3.6/codecs.py", {st_mode=S_IFREG|0644, st_size=36276, ...}) = 0
stat("/usr/lib64/python3.6/codecs.py", {st_mode=S_IFREG|0644, st_size=36276, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/__pycache__/codecs.cpython-36.pyc", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=33902, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=33902, ...}) = 0
read(3, "3\r\r\n@*\273a\264\215\0\0\343\0\0\0\0\0\0\0\0\0\0\0\0:\0\0\0@\0\0"..., 33903) = 33902
read(3, "", 1)                          = 0
close(3)                                = 0
stat("/usr/lib64/python3.6/encodings", {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
stat("/usr/lib64/python3.6/encodings", {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
stat("/usr/lib64/python3.6/encodings", {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/encodings", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3
fstat(3, {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
getdents64(3, /* 128 entries */, 32768) = 4336
getdents64(3, /* 0 entries */, 32768)   = 0
close(3)                                = 0
stat("/usr/lib64/python3.6/encodings/aliases.py", {st_mode=S_IFREG|0644, st_size=15577, ...}) = 0
stat("/usr/lib64/python3.6/encodings/aliases.py", {st_mode=S_IFREG|0644, st_size=15577, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/encodings/__pycache__/aliases.cpython-36.pyc", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=6266, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=6266, ...}) = 0
read(3, "3\r\r\n@*\273a\331<\0\0\343\0\0\0\0\0\0\0\0\0\0\0\0D\1\0\0@\0\0"..., 6267) = 6266
read(3, "", 1)                          = 0
close(3)                                = 0
stat("/usr/lib64/python3.6/encodings", {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
stat("/usr/lib64/python3.6/encodings/utf_8.py", {st_mode=S_IFREG|0644, st_size=1005, ...}) = 0
stat("/usr/lib64/python3.6/encodings/utf_8.py", {st_mode=S_IFREG|0644, st_size=1005, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/encodings/__pycache__/utf_8.cpython-36.pyc", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=1584, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
</code>```

This seems to show that it can't find a whole bunch of files, from a variety of different directories and they're not even even all in a common directory branch! a selection of examples: /sbin/python3.6, /usr/sbin/python3.6, /usr/local/sbin/python3.6, /usr/local/bin/python3.6, /usr/lib64/python3.6

Just also tried from my home bash prompt:

```<code>me@myhost:~> python
ImportError: No module named site
me@myhost:~> python2
ImportError: No module named site
me@myhost:~> python2.7
ImportError: No module named site
me@myhost:~> python3
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f5e446c0740 (most recent call first):
Aborted (core dumped)
me@myhost:~> python3.6
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f0e8c018740 (most recent call first):
Aborted (core dumped)
greg@TRANQUILITY:~>

</code>```

And as root:

```<code>HOST:~ # python
Python 2.7.18 (default, Mar 04 2021, 23:25:57) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[2]+  Stopped                 python
HOST:~ # python2
Python 2.7.18 (default, Mar 04 2021, 23:25:57) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[3]+  Stopped                 python2
HOST:~ # python2.7
Python 2.7.18 (default, Mar 04 2021, 23:25:57) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[4]+  Stopped                 python2.7
HOST:~ # python3
Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[5]+  Stopped                 python3
HOST:~ # python3.6
Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
</code>```

I checked env and added export LC_CTYPE=en_GB.UTF-8 to ~/.bashrc (mine, not root's) - made no difference from a new terminal

I also used 'strace python3.6as both me and root and saved the output to two files and then in VSCode diffed them. I don't have the tech knowledge to make sense of it but the 'root' strace is much longer and works, the 'greg' strace is shorter and on line 216, sayswrite(2, "No module named 'encodings'", 27No module named 'encodings') = 27`

As greg
https://www.dropbox.com/s/p55uen19njil5zb/stracePython3.6-greg.lua?dl=0

As root
https://www.dropbox.com/s/dlxu43yeqnb7li2/stracePython3.6-root.lua?dl=0

答:
Looking at the strace output for both root and greg, the problem seems clear.

For the root user, python 3.6 finds the libraries in /usr/lib64/python3.6.

However, for greg, it only looks under /usr/bin/python3 for subdirectories. That doesn't work because /usr/bin/python3 is a file.

I suspect that the user greg has PYTOHNHOME set erroneously to the location of the Python binary , and that is causing the issue.

Remove PYTOHNHOME from your environment, log out and log in again.

Note: the stuff below is probably barking up the wrong tree. I'll leave it for information.

The encodings module is an (undocumented) part of the python standard library. It is used by the locale module.

Based on the output I suspect that your  Python installation has been damaged or corrupted. Try re-installing python.

EDIT:

If a forced re-install doesn't fix the problem, check that the directory encodings exist in your Python stdlib directory, and is accessible for all users.

To find out which directory that is:

```<code>python
>>> import sysconfig
>>> sysconfig.get_path('stdlib')
'/usr/local/lib/python3.9'
</code>```

Check accessability:

```<code>ls -ld /usr/local/lib/python3.9/encodings
drwxr-xr-x  3 root  wheel  5632 Dec 11 14:34 /usr/local/lib/python3.9/encodings/
</code>```

Read and execute for all users, so that is OK.

EDIT2:

Looking at the trace results.

I don't think the missing files are something to worry about. That's just Python looking for the different locations/names that files can have, given that Python is a multiplatform program.

The interesting bit is near the end:

```<code>stat("/usr/lib64/python3.6/encodings/__init__.py", {st_mode=S_IFREG|0644, st_size=5642, ...}) = 0
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f78043ae000
</code>```

This shows that it can find the directory, and map __init__.py into memory.

EDIT3:

Since both Python 2.7 and 3.6 are end-of-life now, I would suggest removing them, and install python 3.10 instead.

EDIT4:

The directory structure is determined by how you install Python. WRT permissions, directories usually have permissions 0755 and files 0644. Traditionally on UNIX, files in /usr/ are owned by root, while the group differs among systems.
The last number (permission for "others" being neither the owner nor the group) are probably most relevant.

You can fix permissions with a combination of find and chmod. For example:

```<code>find /usr/lib64/python3.6 -type d -exec chmod 755 {} \;
find /usr/lib64/python3.6 -type f -exec chmod 644 {} \;
</code>```

should set a good set of permissions for both files and directories in the Python tree.

Title: No module named 'encodings' on OpenSuse

Body:
A whole host of actions keep returning to this problem:

pip install encodings

Fatal Python error: Py_Initialize: Unable to get the locale encoding

ModuleNotFoundError: No module named 'encodings'

python3

Fatal Python error: Py_Initialize: Unable to get the locale encoding

ModuleNotFoundError: No module named 'encodings'

libreoffice --safe-mode

Fatal Python error: Py_Initialize: Unable to get the locale encoding

ModuleNotFoundError: No module named 'encodings'

zypper se python |grep '^i '

i  | libpython3_6m1_0                                         | Python Interpreter shared library                                                     | package
i  | libsamba-policy0-python3                                 | Active Directory Group Policy library                                                 | package
i  | python-qt3d-qt5-sip                                      | Sip files for python-qt3d-qt5                                                         | package
i  | python-qt5-utils                                         | Common files for PyQt5 for python2 and python3                                        | package
i  | python-qtdatavis3d-qt5-sip                               | Sip files for python-qtdatavis3d-qt5                                                  | package
i  | python-qtwebengine-qt5-sip                               | Sip files for python-qtwebengine-qt5                                                  | package
i  | python-rpm-generators                                    | Dependency generator dependencies for Python RPMs                                     | package
i  | python-rpm-macros                                        | RPM macros for building of Python modules                                             | package
i  | python-sip-common                                        | SIP tool to create python bindings -- common files                                    | package
i  | python-websockify-common                                 | Common data files for the Websockify TCP proxy/bridge                                 | package
i  | python3-Automat                                          | Self-service finite-state machines for the programmer on the go                       | package
i  | python3-Babel                                            | Internationalization utilities                                                        | package
i  | python3-Bottleneck                                       | A collection of fast NumPy array functions                                            | package
i  | python3-Brotli                                           | Python bindings for the Brotli compression library                                    | package
i  | python3-Cycler                                           | Composable style cycles                                                               | package
i  | python3-Cython                                           | The Cython compiler for writing C extensions for the Python language                  | package
i  | python3-Genshi                                           | A toolkit for generation of output for the web                                        | package
i  | python3-HeapDict                                         | A heap with decrease-key and increase-key operations                                  | package
i  | python3-Jinja2                                           | A fast and easy to use template engine written in pure Python                         | package
i  | python3-MarkupSafe                                       | Implements a XML/HTML/XHTML Markup safe string for Python                             | package
i  | python3-Pillow                                           | Python Imaging Library (Fork)                                                         | package
i  | python3-PyHamcrest                                       | Hamcrest framework for matcher objects                                                | package
i  | python3-PyJWT                                            | JSON Web Token implementation in Python                                               | package
i  | python3-PyNaCl                                           | Python binding to the Networking and Cryptography (NaCl) library                      | package
i  | python3-PySocks                                          | A Python SOCKS client module                                                          | package
i  | python3-PyWavelets                                       | PyWavelets is a Python wavelet transforms module                                      | package
i  | python3-PyYAML                                           | YAML parser and emitter for Python                                                    | package
i  | python3-Pygments                                         | A syntax highlighting package written in Python                                       | package
i  | python3-QtPy                                             | Abstraction layer on top of Qt bindings                                               | package
i  | python3-SQLAlchemy                                       | Database Abstraction Library                                                          | package
etc...

I think I've only got python 3.6 installed but the only suggestion I've seen is where there is more than one  version installed.

Any suggestions on what I should test now to find the source of this problem?

My Env:

uname -a

Linux localhost.localdomain 5.3.18-150300.59.49-preempt #1 SMP PREEMPT Mon Feb 7 14:40:20 UTC 2022 (77d9d02) x86_64 x86_64 x86_64 GNU/Linux

python

-bash: /usr/bin/python: No such file or directory

python3

Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> (then exited from python console)

echo $PYTHONHOME returns nothing

echo $PYTHONPATH returns nothing

I have subsequently reinstalled the OS with no different result (surprising to me) and I've tried strace -o pythonstrace.txt python3.6 The (presumably) relevant part of the strace output is:

munmap(0x7f780442e000, 262144)          = 0
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f780442e000
fstat(0, {st_mode=S_IFCHR|0600, st_rdev=makedev(0x88, 0x7), ...}) = 0
stat("/sbin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/sbin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/local/sbin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/root/bin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/local/bin/python3.6", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/bin/python3.6", {st_mode=S_IFREG|0755, st_size=10472, ...}) = 0
readlink("/usr/bin/python3.6", 0x7ffff6517fa0, 4096) = -1 EINVAL (Invalid argument)
openat(AT_FDCWD, "/usr/bin/pyvenv.cfg", O_RDONLY) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/pyvenv.cfg", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/bin/Modules/Setup", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/bin/lib64/python3.6/os.py", 0x7ffff6518f40) = -1 ENOENT (No such file or directory)
stat("/usr/bin/lib64/python3.6/os.pyc", 0x7ffff6518f40) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/os.py", {st_mode=S_IFREG|0644, st_size=37526, ...}) = 0
stat("/usr/bin/pybuilddir.txt", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/bin/lib64/python3.6/lib-dynload", 0x7ffff6521070) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/lib-dynload", {st_mode=S_IFDIR|0755, st_size=4802, ...}) = 0
brk(0x55a4968ff000)                     = 0x55a4968ff000
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f78043ee000
sysinfo({uptime=132115, loads=[4256, 3872, 448], totalram=16583553024, freeram=1984724992, sharedram=68530176, bufferram=122511360, totalswap=2146430976, freeswap=2146430976, procs=990, totalhigh=0, freehigh=0, mem_unit=1}) = 0
brk(0x55a496921000)                     = 0x55a496921000
sigaltstack({ss_sp=0x55a4968d9c20, ss_flags=0, ss_size=8192}, {ss_sp=NULL, ss_flags=SS_DISABLE, ss_size=0}) = 0
stat("/usr/lib/python36.zip", 0x7ffff6529bd0) = -1 ENOENT (No such file or directory)
stat("/usr/lib", {st_mode=S_IFDIR|0755, st_size=10808, ...}) = 0
stat("/usr/lib/python36.zip", 0x7ffff65293b0) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6", {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
stat("/usr/lib64/python3.6", {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
stat("/usr/lib64/python3.6", {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3
fstat(3, {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
getdents64(3, /* 204 entries */, 32768) = 6800
getdents64(3, /* 0 entries */, 32768)   = 0
close(3)                                = 0
stat("/usr/lib64/python3.6/encodings/__init__.cpython-36m-x86_64-linux-gnu.so", 0x7ffff65296f0) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/encodings/__init__.abi3.so", 0x7ffff65296f0) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/encodings/__init__.so", 0x7ffff65296f0) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/python3.6/encodings/__init__.py", {st_mode=S_IFREG|0644, st_size=5642, ...}) = 0
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f78043ae000
stat("/usr/lib64/python3.6/encodings/__init__.py", {st_mode=S_IFREG|0644, st_size=5642, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/encodings/__pycache__/__init__.cpython-36.pyc", O_RDONLY|O_CLOEXEC) = 3
fcntl(3, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
fstat(3, {st_mode=S_IFREG|0644, st_size=3922, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=3922, ...}) = 0
read(3, "3\r\r\n@*\273a\n\26\0\0\343\0\0\0\0\0\0\0\0\0\0\0\0\5\0\0\0@\0\0"..., 3923) = 3922
read(3, "", 1)                          = 0
close(3)                                = 0
stat("/usr/lib64/python3.6", {st_mode=S_IFDIR|0755, st_size=4054, ...}) = 0
stat("/usr/lib64/python3.6/codecs.py", {st_mode=S_IFREG|0644, st_size=36276, ...}) = 0
stat("/usr/lib64/python3.6/codecs.py", {st_mode=S_IFREG|0644, st_size=36276, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/__pycache__/codecs.cpython-36.pyc", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=33902, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=33902, ...}) = 0
read(3, "3\r\r\n@*\273a\264\215\0\0\343\0\0\0\0\0\0\0\0\0\0\0\0:\0\0\0@\0\0"..., 33903) = 33902
read(3, "", 1)                          = 0
close(3)                                = 0
stat("/usr/lib64/python3.6/encodings", {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
stat("/usr/lib64/python3.6/encodings", {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
stat("/usr/lib64/python3.6/encodings", {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/encodings", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3
fstat(3, {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
getdents64(3, /* 128 entries */, 32768) = 4336
getdents64(3, /* 0 entries */, 32768)   = 0
close(3)                                = 0
stat("/usr/lib64/python3.6/encodings/aliases.py", {st_mode=S_IFREG|0644, st_size=15577, ...}) = 0
stat("/usr/lib64/python3.6/encodings/aliases.py", {st_mode=S_IFREG|0644, st_size=15577, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/encodings/__pycache__/aliases.cpython-36.pyc", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=6266, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=6266, ...}) = 0
read(3, "3\r\r\n@*\273a\331<\0\0\343\0\0\0\0\0\0\0\0\0\0\0\0D\1\0\0@\0\0"..., 6267) = 6266
read(3, "", 1)                          = 0
close(3)                                = 0
stat("/usr/lib64/python3.6/encodings", {st_mode=S_IFDIR|0755, st_size=2708, ...}) = 0
stat("/usr/lib64/python3.6/encodings/utf_8.py", {st_mode=S_IFREG|0644, st_size=1005, ...}) = 0
stat("/usr/lib64/python3.6/encodings/utf_8.py", {st_mode=S_IFREG|0644, st_size=1005, ...}) = 0
openat(AT_FDCWD, "/usr/lib64/python3.6/encodings/__pycache__/utf_8.cpython-36.pyc", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=1584, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0

This seems to show that it can't find a whole bunch of files, from a variety of different directories and they're not even even all in a common directory branch! a selection of examples: /sbin/python3.6, /usr/sbin/python3.6, /usr/local/sbin/python3.6, /usr/local/bin/python3.6, /usr/lib64/python3.6

Just also tried from my home bash prompt:

me@myhost:~> python
ImportError: No module named site
me@myhost:~> python2
ImportError: No module named site
me@myhost:~> python2.7
ImportError: No module named site
me@myhost:~> python3
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f5e446c0740 (most recent call first):
Aborted (core dumped)
me@myhost:~> python3.6
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f0e8c018740 (most recent call first):
Aborted (core dumped)
greg@TRANQUILITY:~>

And as root:

HOST:~ # python
Python 2.7.18 (default, Mar 04 2021, 23:25:57) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[2]+  Stopped                 python
HOST:~ # python2
Python 2.7.18 (default, Mar 04 2021, 23:25:57) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[3]+  Stopped                 python2
HOST:~ # python2.7
Python 2.7.18 (default, Mar 04 2021, 23:25:57) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[4]+  Stopped                 python2.7
HOST:~ # python3
Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[5]+  Stopped                 python3
HOST:~ # python3.6
Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

I checked env and added export LC_CTYPE=en_GB.UTF-8 to ~/.bashrc (mine, not root's) - made no difference from a new terminal

I also used 'strace python3.6as both me and root and saved the output to two files and then in VSCode diffed them. I don't have the tech knowledge to make sense of it but the 'root' strace is much longer and works, the 'greg' strace is shorter and on line 216, sayswrite(2, "No module named 'encodings'", 27No module named 'encodings') = 27`

As greg
https://www.dropbox.com/s/p55uen19njil5zb/stracePython3.6-greg.lua?dl=0

As root
https://www.dropbox.com/s/dlxu43yeqnb7li2/stracePython3.6-root.lua?dl=0

Body:
Looking at the strace output for both root and greg, the problem seems clear.

For the root user, python 3.6 finds the libraries in /usr/lib64/python3.6.

However, for greg, it only looks under /usr/bin/python3 for subdirectories. That doesn't work because /usr/bin/python3 is a file.

I suspect that the user greg has PYTOHNHOME set erroneously to the location of the Python binary , and that is causing the issue.

Remove PYTOHNHOME from your environment, log out and log in again.

Note: the stuff below is probably barking up the wrong tree. I'll leave it for information.

The encodings module is an (undocumented) part of the python standard library. It is used by the locale module.

Based on the output I suspect that your  Python installation has been damaged or corrupted. Try re-installing python.

EDIT:

If a forced re-install doesn't fix the problem, check that the directory encodings exist in your Python stdlib directory, and is accessible for all users.

To find out which directory that is:

python
>>> import sysconfig
>>> sysconfig.get_path('stdlib')
'/usr/local/lib/python3.9'

Check accessability:

ls -ld /usr/local/lib/python3.9/encodings
drwxr-xr-x  3 root  wheel  5632 Dec 11 14:34 /usr/local/lib/python3.9/encodings/

Read and execute for all users, so that is OK.

EDIT2:

Looking at the trace results.

I don't think the missing files are something to worry about. That's just Python looking for the different locations/names that files can have, given that Python is a multiplatform program.

The interesting bit is near the end:

stat("/usr/lib64/python3.6/encodings/__init__.py", {st_mode=S_IFREG|0644, st_size=5642, ...}) = 0
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f78043ae000

This shows that it can find the directory, and map __init__.py into memory.

EDIT3:

Since both Python 2.7 and 3.6 are end-of-life now, I would suggest removing them, and install python 3.10 instead.

EDIT4:

The directory structure is determined by how you install Python. WRT permissions, directories usually have permissions 0755 and files 0644. Traditionally on UNIX, files in /usr/ are owned by root, while the group differs among systems.
The last number (permission for "others" being neither the owner nor the group) are probably most relevant.

You can fix permissions with a combination of find and chmod. For example:

find /usr/lib64/python3.6 -type d -exec chmod 755 {} \;
find /usr/lib64/python3.6 -type f -exec chmod 644 {} \;

should set a good set of permissions for both files and directories in the Python tree.


来源: stackexchange

问:
How to import module from other directory in python
I have a file tree like this:

```<code>app
|---src
|    |---vlep/config.py
|
|---tests
     |---conftest.py
</code>```

from conftest.py I am trying to import vlep.confi as config and I am getting the ModulenotFoundError: No module named 'vlep'. I am using venv and I added absolute paths for app, src and vlep directories to venv/bin/activate, so when I activate the virtualenv I can have these directories in the PYTHONPATH env. I thought that this would do the job, but no...and have no idea why. What am I doing wrong?

答: Tks for your time guys. After all there was not wronging to what I was doing to allow the import. The export PYTHONPATH=":/home/user/app/src/vlep" added to the end of venv/bin/activate was enough. The problem was that I was running a script that was changing the value of PYTHONPATH.

ContentLicense: CC BY-SA 4.0

Body:
I have a file tree like this:

app
|---src
|    |---vlep/config.py
|
|---tests
     |---conftest.py

from conftest.py I am trying to import vlep.confi as config and I am getting the ModulenotFoundError: No module named 'vlep'. I am using venv and I added absolute paths for app, src and vlep directories to venv/bin/activate, so when I activate the virtualenv I can have these directories in the PYTHONPATH env. I thought that this would do the job, but no...and have no idea why. What am I doing wrong?


问:
EF Core add/save by Id OR entity reference
I have an entity I'm initiating to be saved as a record via EF Core code-first.

Sometimes I will have the related entity model - usually when I'm creating a new related entity at the same time, so it won't have an Id yet.

But sometimes I will only have the ForeignKey Id of the related entity, particularly when I'm adding just this new record related to an existing record.

I haven't been able to find anything on this specific dual-create-mode pattern, is this the correct convention in this situation?

```<code>public class ClownModel {
    public ClownModel (){}
    public ClownModel (CarModel car){
        Car = car;
    }
    public Clown (int carId){
        CarId = carId;
    }

    public int Id {get;set;}
    public int? CarId {get;set;}
    [ForeignKey("CarId")]
    public CarModel Car {get;set;} = null!;
}
</code>```

My thought is that the FK can be nullable on the entity, so when it's also creating a new related entity it will be assigned during the save?

答:
I recommend using one or the other, not both. Typically when you are doing something like creating a row and want to associate another existing row to it, you should explicitly validate that the ID of the associated entity exists to handle things more gracefully than simply waiting for a SaveChanges to fail. Creating a new Clown would pass the details for the clown and a CarId when assigning them to a specific car. In this case my Clown entity would use a Car reference with a shadow property for the CarId. I request the Car from the DbContext via the CarId and handle the situation where a Car might not be found. (Another user deleted it, a bug in the code, or someone is hacking at the request payload)

Alternatively in situations where I either need raw performance or don't care about the entity association, I will just map the Id with no navigation property. Examples of this would be a bounded context that handles background batch-like jobs, or references to FKs that I don't ever need more than the Key for anyways. Such as something like a "Status" where I do have a Status table and FK for referential integrity, but since those statuses have relevance through business logic decisions I would use an enumeration in the code to represent them. I don't care about a Status entity, just it's key.

The reason I don't recommend using both is that it presents a situation where you have two sources of truth. Clown.CarId, and Clown.Car.CarId. If you want to change a clown's car, setting Clown.CarId would have a different behaviour whether Clown.Car was loaded or not. (whether eager loaded, lazy loaded, or populated by the DbContext if it was already tracking the car when the clown was requested)

Body:
I have an entity I'm initiating to be saved as a record via EF Core code-first.

Sometimes I will have the related entity model - usually when I'm creating a new related entity at the same time, so it won't have an Id yet.

But sometimes I will only have the ForeignKey Id of the related entity, particularly when I'm adding just this new record related to an existing record.

I haven't been able to find anything on this specific dual-create-mode pattern, is this the correct convention in this situation?

public class ClownModel {
    public ClownModel (){}
    public ClownModel (CarModel car){
        Car = car;
    }
    public Clown (int carId){
        CarId = carId;
    }

    public int Id {get;set;}
    public int? CarId {get;set;}
    [ForeignKey("CarId")]
    public CarModel Car {get;set;} = null!;
}

My thought is that the FK can be nullable on the entity, so when it's also creating a new related entity it will be assigned during the save?


问:
My CS50 Credit code is not producing any output after adding a check for type of credit card
The calculating if the card is valid is correct as I tested that and the code performed correctly there, so I removed that section from under the Do section loop to appease stackoverflow. The problem comes from deciding what type of card was used; the program does nothing and waits for the next command in the console. I'm just not seeing what the issue is. Below is my code and I tested with card number 4003600000000014, the result should've printed out "Visa":</p>
```<code>#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
    // Array to store the individual digits of the card
    long card, card_copy;
    int pos = 0, sum = 0, card_id;

    do
    {
        card = get_long("CC#: ");
        card_copy = card;
        while(card != 0)
        {
            if(pos % 2 != 0)
            {
                int temp = 2 * (card % 10);
                // For #s > 9
                if(temp > 9)
                {
                    sum += (temp % 10 + temp / 10);
                }
                else
                {
                    sum += temp;
                }
            }
            else
            {
                sum += card % 10;
            }
        card /= 10; // Divides card # to remove the decimal place
                    // and moves to the next digit in CC
        pos++;
        }
    }
    while(card != 0);

    if(sum % 10 == 0)
    {
       // Divides card to only have the first 2 digits
       card_id = card_copy / (pow(10, pos - 1));

       // Divides card to only have first digit; 4 for Visa
       int visa_id = card_copy / (pow(10, pos - 2));

       // 16-digit; start with 51, 52, 53, 54, or 55
       if(pos == 15 && (card_id >= 51 && card_id <= 55))
       {
           printf("MasterCard");
       }
       // 15-digit; start with 34 or 37
       else if(pos == 14 && (card_id == 34 || card_id == 37))
       {
           printf("American Express");
       }
       // 13-digit; start with 4
       else if(pos == 12 && visa_id == 4)
       {
           printf("Visa");
       }
       // 16-digit; start with 4
       else if(pos == 15 && visa_id == 4)
       {
           printf("Visa");
       }
    }
    else
    {
        printf("Invalid: %d\n", sum);
    }

}
</code>```

答:
The long type has a maximum value of 2147483647. This wouldn't hold the shortest card type I know which is 12 digits.

Either change to unsigned long long or better still use a string.

Body:
The calculating if the card is valid is correct as I tested that and the code performed correctly there, so I removed that section from under the Do section loop to appease stackoverflow. The problem comes from deciding what type of card was used; the program does nothing and waits for the next command in the console. I'm just not seeing what the issue is. Below is my code and I tested with card number 4003600000000014, the result should've printed out "Visa":

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
    // Array to store the individual digits of the card
    long card, card_copy;
    int pos = 0, sum = 0, card_id;

    do
    {
        card = get_long("CC#: ");
        card_copy = card;
        while(card != 0)
        {
            if(pos % 2 != 0)
            {
                int temp = 2 * (card % 10);
                // For #s > 9
                if(temp > 9)
                {
                    sum += (temp % 10 + temp / 10);
                }
                else
                {
                    sum += temp;
                }
            }
            else
            {
                sum += card % 10;
            }
        card /= 10; // Divides card # to remove the decimal place
                    // and moves to the next digit in CC
        pos++;
        }
    }
    while(card != 0);

    if(sum % 10 == 0)
    {
       // Divides card to only have the first 2 digits
       card_id = card_copy / (pow(10, pos - 1));

       // Divides card to only have first digit; 4 for Visa
       int visa_id = card_copy / (pow(10, pos - 2));

       // 16-digit; start with 51, 52, 53, 54, or 55
       if(pos == 15 && (card_id >= 51 && card_id <= 55))
       {
           printf("MasterCard");
       }
       // 15-digit; start with 34 or 37
       else if(pos == 14 && (card_id == 34 || card_id == 37))
       {
           printf("American Express");
       }
       // 13-digit; start with 4
       else if(pos == 12 && visa_id == 4)
       {
           printf("Visa");
       }
       // 16-digit; start with 4
       else if(pos == 15 && visa_id == 4)
       {
           printf("Visa");
       }
    }
    else
    {
        printf("Invalid: %d\n", sum);
    }

}


问:
Why newff() does not work properly when I use its arguments!
My data-set includes 29 inputs and 6 outputs. When I use

```<code>net = newff(minmax(Pl),[14 12 8 6]);
</code>```

to build my feed forward MLP network and train it by

```<code>net.trainParam.epochs=50;
net=train(net,Pl,Tl);
</code>```

the network can not learn my data-set and its error does not decrease below 0.7, but when I use arguments of newff function like this:

```<code>net=newff(minmax(Pl),[14 12 8 6],{'tansig' 'tansig' 'tansig' 'purelin'},'trainlm');
</code>```

the error is decreased very fast and it comes below 0.0001! The unusual note is that when I use the previous code using only one layer including 2 neurons:

```<code>net=newff(minmax(Pl),[2 6],{'tansig' 'purelin'},'trainlm');
</code>```

the error is decreased below 0.2 again and it is doubtful!
Please give me some tips and help me to know what is difference between:

```<code>net = newff(minmax(Pl),[14 12 8 6]);
</code>```

and

```<code>net=newff(minmax(Pl),[14 12 8 myANN.m],{'tansig' 'tansig' 'tansig' 'purelin'},'trainlm');
</code>```

?

答:
I think that the second argument to NEWFF (link requires login) is supposed to be the target vectors, not the size of hidden layers (which is the third argument).

Note that the default transfer function for hidden layers is tansig and for output layer is purelin, and the default training algorithm is trainlm.

Finally, remember that if you want to get reproducible results, you have to manually reset the random number generator to a fixed state at the beginning of each run.

ContentLicense: CC BY-SA 3.0

Body:
My data-set includes 29 inputs and 6 outputs. When I use

net = newff(minmax(Pl),[14 12 8 6]);

to build my feed forward MLP network and train it by

net.trainParam.epochs=50;
net=train(net,Pl,Tl);

the network can not learn my data-set and its error does not decrease below 0.7, but when I use arguments of newff function like this:

net=newff(minmax(Pl),[14 12 8 6],{'tansig' 'tansig' 'tansig' 'purelin'},'trainlm');

the error is decreased very fast and it comes below 0.0001! The unusual note is that when I use the previous code using only one layer including 2 neurons:

net=newff(minmax(Pl),[2 6],{'tansig' 'purelin'},'trainlm');

the error is decreased below 0.2 again and it is doubtful!
Please give me some tips and help me to know what is difference between:

net = newff(minmax(Pl),[14 12 8 6]);

and

net=newff(minmax(Pl),[14 12 8 myANN.m],{'tansig' 'tansig' 'tansig' 'purelin'},'trainlm');

?


问:
Django - How to get attribute from queryset objects list
I have a list of queryset objects like this: [<QuerySet [<Stuff: Phone>]>, <QuerySet [<Stuff: Vodka>]>, <QuerySet [<Stuff: Dictionary>]>]
Now i need to get attributes of each object from it.
Here is my html template

```<code><html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Changes saved</title>
</head>
<body>
<p>Изменены объекты:</p>
{% for el in elements %}
<p>{{el.name}} new price: {{el.price}}</p>
{% endfor %}
</body>
</html>

</code>```

And in my browser i have just "new price:" 3 times

答:
Looks like from your sample, you are sending an array of querysets to the template, there are two ways you can get the data to show:

Get the first element from each queryset when looping

{% for el in elements %}

{{el.first.name}} new price: {{el.first.price}}

{% endfor %}
```

Change the view to send in an array of objects not an array of querysets

Body:
I have a list of queryset objects like this: [<QuerySet [<Stuff: Phone>]>, <QuerySet [<Stuff: Vodka>]>, <QuerySet [<Stuff: Dictionary>]>]
Now i need to get attributes of each object from it.
Here is my html template

    <meta charset="UTF-8">
    <title>Changes saved</title>

Изменены объекты:

{% for el in elements %}

{{el.name}} new price: {{el.price}}

{% endfor %}

And in my browser i have just "new price:" 3 times

Body:
Looks like from your sample, you are sending an array of querysets to the template, there are two ways you can get the data to show:

Get the first element from each queryset when looping

{% for el in elements %}
    <p>{{el.first.name}} new price: {{el.first.price}}</p>
{% endfor %}

Change the view to send in an array of objects not an array of querysets


问:
How do I get my Julia code to assign the scalar value of a variable to an array without changing it as they variable mutates later on?
For my simulation, I have a field that is called particle.current_theta. When this field is a single variable, I assign it a new value that is called just "theta" on my line 177. This theta has its value changed further down within my code, on lines 202 and 206. I want what I have printed in my terminal as tree_theta and current_theta to be very similar to each other but not quite identical (This part of the code basically detects whether or not my particle is entering or exiting a region). You can see all this in the image below:

Now, I need to make the field I have called particle.current_theta a [1x1] array, and assign the entry in my [1x1] array the "theta" value, as usual. However, simply making particle.current_theta a [1x1] array radically changes its value within the terminal and causes my simulation to break. You can see how the value for particle.theta (printed in terminal as "current theta") is now drastically different in the code below:
I suspect that making particle.currenttheta an array is making it mutate whenever theta is changed  in some of those lines below. How do I prevent that from happening, and get results that are identical to using just a single variable. To be precise, I want particle.current_theta to save the numerical information that theta has at line 177 of code but not be changed afterwards. Because of the large size of my code now and the function calls within function calls, it would be infeasible for me to be able to create a mwe that replicates this issue. However, all help and advice is appreciated, and I will respond to and clarify any questions that people may have.

答:
If theta is a scalar (and it appears to be), then it's unlikely that changing its value is what is changing particle.currenttheta. What is more likely is that you're passing the currenttheta to some function, and changing the value of the passed argument inside the function. Julia arrays are passed to functions "by reference", in the sense that a copy is not made, and instead any changes made inside the function change the original array. When you had currenttheta as a scalar (which are passed "by value" instead), when you pass that to a function, changes made inside the function do not affect the original currenttheta's value.

So if you're passing currenttheta to a function somewhere and don't want it to be modified inside the function, pass copy(particle.currenttheta) in that call instead.

If you're not doing that, or that doesn't solve the problem, we need more of the code to figure out where the change might actually be happening. If not the whole of it, at least the parts that handle currenttheta, and the parts that print it. (Also, it would massively help with clarity if you would use consistent names in the output. Sometimes it's treetheta and current theta, other times it's theta and particle.currentheta, and it's not clear where these are being printed from and what the difference - if any - is.)


问:
For loop in function to move the item of the list
I want to move the negative numbers to the end of the list until I encounter the positive number.

List:

```<code>a=[-1,-2,2,3,4,-5]
</code>```

The result i want:

```<code>[-2, 2, 3, 4, -5, -1]
[2, 3, 4, -5, -1, -2]
</code>```

My function:

```<code>def f1(A):
  for i in A:
    if i>=0:
      break
    if 'Z' not in globals() or 'Z' not in locals():
     X=A
     X1=X[0]
     X.remove(X1)
     X.append(X1)
     Z=X
    else:
      X=Z
      X1=X[0]
      X.remove(X1)
      X.append(X1)
      Z=X
    print(Z)

a=[-1,-2,2,3,4,-5]
print (f1 (a))
</code>```

But the result

```<code>[-2, 2, 3, 4, -5, -1]
None
</code>```

can we solve it with this function?

答:
You fell into one of python's traps.   If A is a list and you do X=A, you do not get a new list, you just get a new name.   Anything you do to list X will be done to list A and visa versa.  So when you change list X you change list A
which is the list you are looping over.   Do not modify the thing you are looping over.  To get a copy of a list use X=A.copy().  Now you can modify X without changing A.</p>

Body:
I want to move the negative numbers to the end of the list until I encounter the positive number.

List:

a=[-1,-2,2,3,4,-5]

The result i want:

[-2, 2, 3, 4, -5, -1]
[2, 3, 4, -5, -1, -2]

My function:

def f1(A):
  for i in A:
    if i>=0:
      break
    if 'Z' not in globals() or 'Z' not in locals():
     X=A
     X1=X[0]
     X.remove(X1)
     X.append(X1)
     Z=X
    else:
      X=Z
      X1=X[0]
      X.remove(X1)
      X.append(X1)
      Z=X
    print(Z)

a=[-1,-2,2,3,4,-5]
print (f1 (a))

But the result

[-2, 2, 3, 4, -5, -1]
None

can we solve it with this function?


问:
How to check a substring in a string up until a certain occurrence of a character in python?
I receive orders that contain the name of customer, price of order and the quantity of the order.

The format of the order looks like this: {'first order': ['Alex', '100@2']}

(100 refers to a price and 2 refers to a quantity).

So I have different orders: {'first order': ['Alex', '99@2'], 'second order': ['Ann', '101@2'], 'third order': ['Nick', '110@3']}

We need to compare the prices and see which is the highest price and which is the lowest.

I was thinking of doing this by cutting the substring into two parts, the first part before the '@' symbol and the second part after the '@' symbol, then extract the numbers from the first part and compare them with others.

What's the most efficient way you can tell to solve this issue?

Thank you

答:
I'd suggest to transform the dictionary to a list of dictionaries and convert the string to two floats. For example:

orders = {
    "first order": ["Alex", "99@2"],
    "second order": ["Ann", "101@2"],
    "third order": ["Nick", "110@3"],
}

orders = [
    {
        "order": k,
        "name": name,
        "price": float(pq.split("@")[0]),
        "quantity": float(pq.split("@")[1]),
    }
    for k, (name, pq) in orders.items()
]
```
<p>Then if you want to find highest and lowest price you can use <code>min</code>/<code>max</code> function easily:</p>
<pre class="lang-py prettyprint-override"><code>highest = max(orders, key=lambda k: k["price"])
lowest = min(orders, key=lambda k: k["price"])

print(highest)
print(lowest)
</code>```

Prints:

{'order': 'third order', 'name': 'Nick', 'price': 110.0, 'quantity': 3.0}
{'order': 'first order', 'name': 'Alex', 'price': 99.0, 'quantity': 2.0}
```

Body:
I'd suggest to transform the dictionary to a list of dictionaries and convert the string to two floats. For example:

orders = {
    "first order": ["Alex", "99@2"],
    "second order": ["Ann", "101@2"],
    "third order": ["Nick", "110@3"],
}

orders = [
    {
        "order": k,
        "name": name,
        "price": float(pq.split("@")[0]),
        "quantity": float(pq.split("@")[1]),
    }
    for k, (name, pq) in orders.items()
]

Then if you want to find highest and lowest price you can use min/max function easily:

highest = max(orders, key=lambda k: k["price"])
lowest = min(orders, key=lambda k: k["price"])

print(highest)
print(lowest)

Prints:

{'order': 'third order', 'name': 'Nick', 'price': 110.0, 'quantity': 3.0}
{'order': 'first order', 'name': 'Alex', 'price': 99.0, 'quantity': 2.0}


问:
Filtering data after fetching in React
I need to make a list of objects based on combined data from 2 arrays, one comes from a localStorage and the second one from Django backend. First of all objects from localStorage are displayed by showCart() function

```<code>export const showCart = () => {
    if (typeof window !== undefined) {
        if (localStorage.getItem("cart")) {
            return JSON.parse(localStorage.getItem("cart"));
        };
    };
};
</code>```

it returns data in this format: FE: { id: 1, amount: 7, size: "L", product: 1 }. product is the Foreign Key needed to match data from other array.

The second array comes form a backend and it is feched by getAllProducts() function

```<code>export const getAllProducts = () => {
    return fetch(`${url}/products/`, {method: "GET"})
    .then((response) => {
        return response.json();
    })
    .catch((error) => console.log(error))
};

</code>```

It returns data in this format: FE { name: "Red", id: 3, price: 33, image:"some-url"}

Now I need to create another list of objects by merging then by product of an object in first array with id of an object from the second one. The objects in the third array need to contain amount and size from first array as well as name, price and image from the second one. In the end I want to store it in useState().

This is what I came up with, I guess my code stops working arter first for loop:

```<code>const [cart, setCart] = useState([]);

   const CheckAnonymousCart = () => {
        getAllProducts()
        .then((data) => {
            const localCart = showCart();
            var products = [];
            for (let i = 0; i < localCart.lenght; i++) {
                for (let y = 0; y < data.lenght; y++) {
                    if (localCart[i].product === data[y].id) {
                        console.log(localCart[i].product, data[y].id)
                        const item = {
                            name: data[y].name,
                            price: data[y].price,
                            image: data[y].image,
                            amount: localCart[i].amount,
                            size: localCart[i].size,
                        }
                        products.push(item)
                        break;
                    }
                }
            }
            setCart(products);
        })
        .catch((error) => console.log(error))
    };

</code>```

Any thoughts?

答:
In addition to Jacob's comment, you probably want to avoid FETCH'ing all products from the DB, because it requires more DB resources, most of the info is not required, and it makes the for-loop take longer to JOIN both lists.

Ideally, you would use a parameterized query like so:

```<code>return fetch(`${url}/products/?id=1&id=2&id=3`, {method: "GET"})
</code>```

Where ?id=1&id=2&id=3 are a subset of the product IDs that you're retrieving.

Note: You will also want to sanitize/validate the product IDs in localStorage, because the data can be modified by the end-user, which is a potential attack vector by malicious users.

Title: Filtering data after fetching in React

Body:
I need to make a list of objects based on combined data from 2 arrays, one comes from a localStorage and the second one from Django backend. First of all objects from localStorage are displayed by showCart() function

export const showCart = () => {
    if (typeof window !== undefined) {
        if (localStorage.getItem("cart")) {
            return JSON.parse(localStorage.getItem("cart"));
        };
    };
};

it returns data in this format: FE: { id: 1, amount: 7, size: "L", product: 1 }. product is the Foreign Key needed to match data from other array.

The second array comes form a backend and it is feched by getAllProducts() function

export const getAllProducts = () => {
    return fetch(`${url}/products/`, {method: "GET"})
    .then((response) => {
        return response.json();
    })
    .catch((error) => console.log(error))
};

It returns data in this format: FE { name: "Red", id: 3, price: 33, image:"some-url"}

Now I need to create another list of objects by merging then by product of an object in first array with id of an object from the second one. The objects in the third array need to contain amount and size from first array as well as name, price and image from the second one. In the end I want to store it in useState().

This is what I came up with, I guess my code stops working arter first for loop:

const [cart, setCart] = useState([]);

   const CheckAnonymousCart = () => {
        getAllProducts()
        .then((data) => {
            const localCart = showCart();
            var products = [];
            for (let i = 0; i < localCart.lenght; i++) {
                for (let y = 0; y < data.lenght; y++) {
                    if (localCart[i].product === data[y].id) {
                        console.log(localCart[i].product, data[y].id)
                        const item = {
                            name: data[y].name,
                            price: data[y].price,
                            image: data[y].image,
                            amount: localCart[i].amount,
                            size: localCart[i].size,
                        }
                        products.push(item)
                        break;
                    }
                }
            }
            setCart(products);
        })
        .catch((error) => console.log(error))
    };

Any thoughts?

Body:
In addition to Jacob's comment, you probably want to avoid FETCH'ing all products from the DB, because it requires more DB resources, most of the info is not required, and it makes the for-loop take longer to JOIN both lists.

Ideally, you would use a parameterized query like so:

return fetch(`${url}/products/?id=1&id=2&id=3`, {method: "GET"})

Where ?id=1&id=2&id=3 are a subset of the product IDs that you're retrieving.

Note: You will also want to sanitize/validate the product IDs in localStorage, because the data can be modified by the end-user, which is a potential attack vector by malicious users.


问:
How to scrollIntoView() inside a specific dropdown(div) in Python
I am trying to scrape a website that requires me to first fill out certain dropdowns.

However, most of the dropdown selections are hidden and only appear in the DOM tree when I scroll down WITHIN the dropdown. Is there a solution I can use to somehow mimic a scroll wheel, or are there other libraries that could complement Selenium?

答:
requests and BeautifulSoup are two libraries in python that can assist with scraping data. They allow you to get the url and make instances within the html language.

In order to inspect a specific part of a website you just need to right click & inspect on the item you want to scrape. This will open all the hidden paths you speak of to that specific tag.


问:
Extract values at specific location in string in Python
Given a data stream, how do I extract the information (between \x15 and \x15\n) that comes right after a *PAR?

Here is the data stream.

```<code>'%gra:bla bla bla\n',
'*PAR:\tthe cat wants\n',
'\tcookies . \x159400_14000\x15\n',
'%mor:\tdet:art|the adj|cat\n',
'\tpro:rel|which aux|be&3S part|tip-PRESP coord|and pro:sub|he\n',
'\tv|want-3S n|cookie-PL .\n',
'%gra:\t1|3|DET 2|3|MOD 3|4|SUBJ 4|0|ROOT 5|4|JCT 6|7|DET 7|5|POBJ 8|10|LINK\n',
'\t9|10|AUX 10|7|CMOD 11|13|LINK 12|13|SUBJ 13|10|CJCT 14|13|OBJ 15|16|INF\n',
'\t16|14|XMOD 17|16|JCT 18|19|DET 19|17|POBJ 20|4|PUNCT\n',
'*PAR:\ cookies biscuit\n',
'\tis eating a cookie . \x1514000_18647\x15\n',
</code>```

My output should be:

```<code>"9400_14000"
"14000_18647"
...
</code>```

答:
Go over the data line by line and try to look for the desired pattern just on lines that follow *PAR:

import re
[re.search('\x15(.*)\x15\n', line).groups()[0]
 for i, line in enumerate(data) if '*PAR' in data[i - 1]]
```
<p>This code will throw an exception if the pattern cannot be matched on a line that follows <code>*PAR</code>. To get all valid matches use:</p>
<pre class="lang-py prettyprint-override"><code>[match
 for i, line in enumerate(data) if '*PAR' in data[i - 1]
 for match in re.findall('\x15(.*)\x15\n', line)]
</code>```

If you expect more than a single pair of \x15 on a line you can use this regex instead to find the shortest match:

'\x15(.*?)\x15\n'
```

Body:
Given a data stream, how do I extract the information (between \x15 and \x15\n) that comes right after a *PAR?

Here is the data stream.

'%gra:bla bla bla\n',
'*PAR:\tthe cat wants\n',
'\tcookies . \x159400_14000\x15\n',
'%mor:\tdet:art|the adj|cat\n',
'\tpro:rel|which aux|be&3S part|tip-PRESP coord|and pro:sub|he\n',
'\tv|want-3S n|cookie-PL .\n',
'%gra:\t1|3|DET 2|3|MOD 3|4|SUBJ 4|0|ROOT 5|4|JCT 6|7|DET 7|5|POBJ 8|10|LINK\n',
'\t9|10|AUX 10|7|CMOD 11|13|LINK 12|13|SUBJ 13|10|CJCT 14|13|OBJ 15|16|INF\n',
'\t16|14|XMOD 17|16|JCT 18|19|DET 19|17|POBJ 20|4|PUNCT\n',
'*PAR:\ cookies biscuit\n',
'\tis eating a cookie . \x1514000_18647\x15\n',

My output should be:

"9400_14000"
"14000_18647"
...

Body:
Go over the data line by line and try to look for the desired pattern just on lines that follow *PAR:

import re
[re.search('\x15(.*)\x15\n', line).groups()[0]
 for i, line in enumerate(data) if '*PAR' in data[i - 1]]

This code will throw an exception if the pattern cannot be matched on a line that follows *PAR. To get all valid matches use:

[match
 for i, line in enumerate(data) if '*PAR' in data[i - 1]
 for match in re.findall('\x15(.*)\x15\n', line)]

If you expect more than a single pair of \x15 on a line you can use this regex instead to find the shortest match:

'\x15(.*?)\x15\n'


问:
Can Selenium automation be tracked?
Is it possible to track if you are using automation tool to do things automatically?

Can someone please answer my question?

答:
Selenium driven ChromeDriver / GeckoDriver initiated google-chrome / firefox Browsing Context can be easily detected deploying either of the following Bot Management services:

Imperva Advanced Bot Protection formerly known as Distil.

Akamai Bot Manager

DataDome

Cloudflare

You can find a relevant detailed discussion in Can a website detect when you are using Selenium with chromedriver?

Title: Can Selenium automation be tracked?


问:
Do you need to create a web service to be able to support payments/donations via Apple Pay in a React Native app?
I'm creating an app using React Native (using Expo, for what it's worth) and would like the app to be able to support donations via Apple Pay.

Expo's Stripe documentation includes a Snack that demonstrates how to support Apple Pay, and the documentation mentions that the Snack uses a Glitch server.

Furthermore, the Stripe documentation also seems to suggest that I need to create a web service to make things work ("For security reasons, your app can’t create these objects. Instead, add an endpoint on your server that...").

I had assumed that Apple Pay took care of these sorts of things behind the scenes and that payments would be processed by Apple's own servers. Do I really need to create a web service to support donations via Apple Pay?

答:
Yes, absolutely, without doubt, you need a server for that. The payment intent is created with a secret key and your secret key needs to stay secret on your server.
Anything you put on the client is insecure and can be manipulated by those with bad intent.

If you wanted to have a client side only way to collect payments you can use Stripe Checkout, but from what I hear, it's fairly limited Related Question


问:
How to find the average total per order in R
I'm trying to find the average total of an order across the entire data set. Here is what a little bit of the data set looks like:

order_id
qty
price
discount

20752
1
58
5

20753
1
34
0

20753
1
64
0

20753
1
78
0

20754
2
34
0

20755
1
62
0

What I'm looking to do is find the average total of each order.

I'm able to find the average of each order by using this code but I'm not sure how to find the average of each order across the entire data set.

```<code>'''
project %>%

    group_by(order_id) %>%

    summarise(Avg_total = mean(price, na.rm=TRUE))
'''
</code>```

This gives me the following table:

order_id
Avg_total

20752
58

20753
58.7

20754
34

20755
62

However, what I want is this:

Avg_total

89.75

Here is how I calculated that number -

order 20752 has a total of 53

order 20753 has a total of 176

order 20754 has a total of 68

order 20755 has a total of 62

53 + 176 + 68 + 62 = 359 / 4 (# of orders) = 89.75 average total per order across the whole set.

If possible, I would also like to filter out any order totals above 500 to remove any outliers.

答:
I was able to actually able to find the answer using this code</p>
```<code>Avg_order <- project %>%
  group_by(order_id) %>%
  summarise(Sum_total= ((price * qty) - discount), na.rm=TRUE) %>%
  summarise(Avg = mean(Sum_total, na.rm=TRUE))

Avg_order %>%
  summarise(avg_price_per_order = sum(Avg) / n_distinct(order_id))
</code>```

Body:
I'm trying to find the average total of an order across the entire data set. Here is what a little bit of the data set looks like:

order_id
qty
price
discount

20752
1
58
5

20753
1
34
0

20753
1
64
0

20753
1
78
0

20754
2
34
0

20755
1
62
0

What I'm looking to do is find the average total of each order.

I'm able to find the average of each order by using this code but I'm not sure how to find the average of each order across the entire data set.

'''
project %>%

    group_by(order_id) %>%

    summarise(Avg_total = mean(price, na.rm=TRUE))
'''

This gives me the following table:

order_id
Avg_total

20752
58

20753
58.7

20754
34

20755
62

However, what I want is this:

Avg_total

89.75

Here is how I calculated that number -

order 20752 has a total of 53

order 20753 has a total of 176

order 20754 has a total of 68

order 20755 has a total of 62

53 + 176 + 68 + 62 = 359 / 4 (# of orders) = 89.75 average total per order across the whole set.

If possible, I would also like to filter out any order totals above 500 to remove any outliers.

Body:
I was able to actually able to find the answer using this code

Avg_order <- project %>%
  group_by(order_id) %>%
  summarise(Sum_total= ((price * qty) - discount), na.rm=TRUE) %>%
  summarise(Avg = mean(Sum_total, na.rm=TRUE))

Avg_order %>%
  summarise(avg_price_per_order = sum(Avg) / n_distinct(order_id))


问:
Permissions VSCode and Linux/Apache
Here is the problem I'm facing:

I use Linux Mint 20.3 Cinnamon.

I have user called user-test which is in the group user-test - user-test:user-test.
I have a directory with WordPress project in var/www/project .
VSCode, that uses the user user-test and Apache that uses www-data in the group www-data.

When I open the project with VScode, if I change the project folder owner to www-data, every time I try to save a file it shows this notification: Notification - Permissions
If I change the user to: user-test:user-test I have a problem with every action I want to take in the project.
For example: I'm not able to upload files, update plugins or the system /in the admin panel of WordPress/.

So whenever I want to update/upgrade or upload something in the admin panel I Need to change the user to www-data:www-data and whenever I want to change something with VSCode I need to change the user to user-test:user-test.
I don't want to change the permissions of the folder to 0777 or other than 0755, because they are automatically deployed to a live server after the changes are pushed to Git.

I can't remember for a solution for this problem. The only one I do is to set the VSCode to open with www-data by default, but If somebody has a better solution I would appreciate your advices.

答:
You could change git config to ignore permission changes with

```<code>git config core.fileMode false
</code>```

https://git-scm.com/docs/git-config#Documentation/git-config.txt-corefileMode

Title: Permissions VSCode and Linux/Apache

Body:
You could change git config to ignore permission changes with

git config core.fileMode false

https://git-scm.com/docs/git-config#Documentation/git-config.txt-corefileMode


问:
Query regarding Expandable subfile
In load all subfile the max number of data that can be loaded in the buffer is 9999, is there any similar space limitation in case of an expandable subfile or a single page subfile?</p>

答:
An expandable subfile is basically a load-all subfile. Programmatically, you just add more and more records to it beyond SFLSIZ, and the OS takes care of proper expansion of the memory area. It handles scroll-back for you.

A single-page subfile displays just one single page of data. The programmer is responsible for erasing and filling it accordingly, handling the scrolling keys, and so on. Thus, there are no limitations whatsoever.

Also see my Github project (German).

Title: Query regarding Expandable subfile


答:
I like to use this function

def get_between(string:str,start:str,end:str,with_brackets=True):
    #re.escape escapes all characters that need to be escaped
    new_start=re.escape(start)
    new_end=re.escape(end)
    pattern=f"{new_start}.*?{new_end}"
    res=re.findall(pattern,string)
    if with_brackets:
        return res #-> this is with the brackets
    else:
        return [x[len(start):-len(end)] for x in res]#-> this is without the brackets
```
<p>To use it in your example do this:</p>
<pre class="lang-py prettyprint-override"><code>result = []
for i,string in enumerate(data_stream):
 if i>0 and "*PAR" in data_stream[i-1]:
  result+=get_between(string,"\x15","\x15\n",False)
print(result)

</code>```

Title: Extract values at specific location in string in Python

Body:
I like to use this function

def get_between(string:str,start:str,end:str,with_brackets=True):
    #re.escape escapes all characters that need to be escaped
    new_start=re.escape(start)
    new_end=re.escape(end)
    pattern=f"{new_start}.*?{new_end}"
    res=re.findall(pattern,string)
    if with_brackets:
        return res #-> this is with the brackets
    else:
        return [x[len(start):-len(end)] for x in res]#-> this is without the brackets

To use it in your example do this:

result = []
for i,string in enumerate(data_stream):
 if i>0 and "*PAR" in data_stream[i-1]:
  result+=get_between(string,"\x15","\x15\n",False)
print(result)


问:
How to find unicode character class in PHP
I am having hard times finding a way to get the unicode class of a char.

list of unicode classes: https://www.php.net/manual/en/regexp.reference.unicode.php

The desired function in python: https://docs.python.org/3/library/unicodedata.html#unicodedata.category

I just want the PHP equivalent to this python function.

For example, if I called the x function like this: x('-') it would return Pd because Pd is the class hyphen belongs to.

Thanks.

答:
I'm posting this as it might be useful. Have done this before on a very large scale.

Below is a condensed way to do it using PHP.

Notes:

A single regex is generated once at startup.

It contains a Lookahead Assertion with a capture group for each Property.

Example:  (?=(\p{Property1}))?(?=(\p{Property2}))? ... (?=(\p{PropertyN}))?

Each character in the target is checked for all the properties in the array.

Each capture group represents an index into the character array $General_Cat_Props

that is it's association when a match is analyzed

for printing.

This solves the issues that each character can be matched by many properties.

Basically add the properties of interest to $General_Cat_Props.

No other change is necessary.

There are 2 functions:

Get_UniCategories_From_Char( $char ) analyze a character at a time.

Get_UniCategories_From_String( $str ) for strings ( calls 1 on each character ).

Obviously it is noteworthy that the array $General_Cat_Props below can be added to or removed from as needed, for a custom filter.

There can be many specific constant property arrays as needed for special checks. The array order of the properties is irrelevant.

Regex101 quick global test bed

```<code>/(?=.)(?=(\p{Cn}))?(?=(\p{Cc}))?(?=(\p{Cf}))?(?=(\p{Co}))?(?=(\p{Cs}))?(?=(\p{Lu}))?(?=(\p{Ll}))?(?=(\p{Lt}))?(?=(\p{Lm}))?(?=(\p{Lo}))?(?=(\p{Mn}))?(?=(\p{Me}))?(?=(\p{Mc}))?(?=(\p{Pd}))?(?=(\p{Ps}))?(?=(\p{Pe}))?(?=(\p{Pc}))?(?=(\p{Po}))?(?=(\p{Pi}))?(?=(\p{Pf}))?(?=(\p{Sm}))?(?=(\p{Sc}))?(?=(\p{Sk}))?(?=(\p{So}))?(?=(\p{Zs}))?(?=(\p{Zl}))?(?=(\p{Zp}))?/su
</code>```

https://regex101.com/r/fvVZX0/1

PHP

Mod: After realizing php only populates the $match array up until the last optional group matched, a check was added when creating the result (see $last_grp_matched = sizeof($matches);).

Previously it was being forced by adding a capture group (.) at the end. The old code still works, use/see previous version if needed.

http://sandbox.onlinephpfunctions.com/code/f1aeca3d9a99d1b2d1bfc72c3dd004ad232bc29e

```<code><?php

// The prop array
$General_Cat_Props = [
"",
"Cn", "Cc", "Cf", "Co", "Cs",
"Lu", "Ll", "Lt", "Lm", "Lo",
"Mn", "Me", "Mc", // "Nd", "Nl", "No",
"Pd", "Ps", "Pe", "Pc", "Po", "Pi", "Pf",
"Sm", "Sc", "Sk", "So",
"Zs", "Zl", "Zp"
];

// The Rx
$GCRx;

// One-time make function
function makeGCRx()
{
    global $General_Cat_Props, $GCRx ;
    $rxstr = "(?=.)";     // Start of regex, something must be ahead
    for ($i = 1; $i < sizeof( $General_Cat_Props ); $i++) {
        $rxstr .= "(?=(\\p{" . $General_Cat_Props[ $i ] . "}))?";
    }
    $GCRx = "/$rxstr/su";
}

makeGCRx();
// print_r($GCRx . "\n");

function Get_UniCategories_From_Char( $char )
{
    global $General_Cat_Props, $GCRx;
    $ret = "";
    if ( preg_match( $GCRx, $char, $matches )) {
        $last_grp_matched = sizeof($matches);
        for ($i = 1; $i < sizeof( $General_Cat_Props ), $i < $last_grp_matched; $i++) {
            if ( $matches[ $i ] != null ) {
                $ret .= $General_Cat_Props[ $i ] . " ";
            }
        }
    }
    return $ret;
}

function Get_UniCategories_From_String( $str )
{
    $ret = "";
    for ($i = 0; $i < strlen( $str ); $i++) {
        $ret .= $str[ $i ] . "  " . Get_UniCategories_From_Char( $str[ $i ] ) . "\n";
    }
    return $ret;
}

print_r( "-  " . Get_UniCategories_From_Char( "-" ) . "\n--------\n" );
// or
print_r( Get_UniCategories_From_String( "Hello 270 -,+?" ) . "\n" );
</code>```

Output:

```<code>-  Pd
--------
H  Lu
e  Ll
l  Ll
l  Ll
o  Ll
   Zs
2
7
0
   Zs
-  Pd
,  Po
+  Sm
?  Po
</code>```

Body:
I'm posting this as it might be useful. Have done this before on a very large scale.

Below is a condensed way to do it using PHP.

Notes:

A single regex is generated once at startup.

It contains a Lookahead Assertion with a capture group for each Property.

Example:  (?=(\p{Property1}))?(?=(\p{Property2}))? ... (?=(\p{PropertyN}))?

Each character in the target is checked for all the properties in the array.

Each capture group represents an index into the character array $General_Cat_Props

that is it's association when a match is analyzed

for printing.

This solves the issues that each character can be matched by many properties.

Basically add the properties of interest to $General_Cat_Props.

No other change is necessary.

There are 2 functions:

Get_UniCategories_From_Char( $char ) analyze a character at a time.

Get_UniCategories_From_String( $str ) for strings ( calls 1 on each character ).

Obviously it is noteworthy that the array $General_Cat_Props below can be added to or removed from as needed, for a custom filter.

There can be many specific constant property arrays as needed for special checks. The array order of the properties is irrelevant.

Regex101 quick global test bed

/(?=.)(?=(\p{Cn}))?(?=(\p{Cc}))?(?=(\p{Cf}))?(?=(\p{Co}))?(?=(\p{Cs}))?(?=(\p{Lu}))?(?=(\p{Ll}))?(?=(\p{Lt}))?(?=(\p{Lm}))?(?=(\p{Lo}))?(?=(\p{Mn}))?(?=(\p{Me}))?(?=(\p{Mc}))?(?=(\p{Pd}))?(?=(\p{Ps}))?(?=(\p{Pe}))?(?=(\p{Pc}))?(?=(\p{Po}))?(?=(\p{Pi}))?(?=(\p{Pf}))?(?=(\p{Sm}))?(?=(\p{Sc}))?(?=(\p{Sk}))?(?=(\p{So}))?(?=(\p{Zs}))?(?=(\p{Zl}))?(?=(\p{Zp}))?/su

https://regex101.com/r/fvVZX0/1

PHP

Mod: After realizing php only populates the $match array up until the last optional group matched, a check was added when creating the result (see $last_grp_matched = sizeof($matches);).

Previously it was being forced by adding a capture group (.) at the end. The old code still works, use/see previous version if needed.

http://sandbox.onlinephpfunctions.com/code/f1aeca3d9a99d1b2d1bfc72c3dd004ad232bc29e

<?php

// The prop array
$General_Cat_Props = [
"",
"Cn", "Cc", "Cf", "Co", "Cs",
"Lu", "Ll", "Lt", "Lm", "Lo",
"Mn", "Me", "Mc", // "Nd", "Nl", "No",
"Pd", "Ps", "Pe", "Pc", "Po", "Pi", "Pf",
"Sm", "Sc", "Sk", "So",
"Zs", "Zl", "Zp"
];

// The Rx
$GCRx;

// One-time make function
function makeGCRx()
{
    global $General_Cat_Props, $GCRx ;
    $rxstr = "(?=.)";     // Start of regex, something must be ahead
    for ($i = 1; $i < sizeof( $General_Cat_Props ); $i++) {
        $rxstr .= "(?=(\\p{" . $General_Cat_Props[ $i ] . "}))?";
    }
    $GCRx = "/$rxstr/su";
}

makeGCRx();
// print_r($GCRx . "\n");

function Get_UniCategories_From_Char( $char )
{
    global $General_Cat_Props, $GCRx;
    $ret = "";
    if ( preg_match( $GCRx, $char, $matches )) {
        $last_grp_matched = sizeof($matches);
        for ($i = 1; $i < sizeof( $General_Cat_Props ), $i < $last_grp_matched; $i++) {
            if ( $matches[ $i ] != null ) {
                $ret .= $General_Cat_Props[ $i ] . " ";
            }
        }
    }
    return $ret;
}

function Get_UniCategories_From_String( $str )
{
    $ret = "";
    for ($i = 0; $i < strlen( $str ); $i++) {
        $ret .= $str[ $i ] . "  " . Get_UniCategories_From_Char( $str[ $i ] ) . "\n";
    }
    return $ret;
}

print_r( "-  " . Get_UniCategories_From_Char( "-" ) . "\n--------\n" );
// or
print_r( Get_UniCategories_From_String( "Hello 270 -,+?" ) . "\n" );

Output:

-  Pd
--------
H  Lu
e  Ll
l  Ll
l  Ll
o  Ll
   Zs
2
7
0
   Zs
-  Pd
,  Po
+  Sm
?  Po


问:
Location Background - Network Connection
I am trying to post data on background and I want to receive response from in regular interval that so that I am trying to use CLLocation Manager . But didUpdateToLocation method calling only once.
Please suggest me !
enter code here</p>

```<code>    - (void)applicationDidEnterBackgroundUIApplication *)application
    {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 100 m
    [locationManager startUpdatingLocation];
    UIApplication* app = [UIApplication sharedApplication];

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
     bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_ QUEUE_PRIORITY_DEFAULT, 0), ^{

    //Do the work associated with the task
    // code for Posting Data Here
    NSLog(@"backgroundTimeRemaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
    });

    }
    - (void)locationManagerCLLocationManager *)manager
       didUpdateToLocationCLLocation *)newLocation
       fromLocationCLLocation *)oldLocation
     {

      NSLog(@"Location Change");
     }
     This Method Call Only Once.
</code>```

答:
the call should be</p>

```<code>    [locationManager startMonitoringSignificantLocationChanges];
</code>```

Body:
I am trying to post data on background and I want to receive response from in regular interval that so that I am trying to use CLLocation Manager . But didUpdateToLocation method calling only once.
Please suggest me !
enter code here

    - (void)applicationDidEnterBackgroundUIApplication *)application
    {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 100 m
    [locationManager startUpdatingLocation];
    UIApplication* app = [UIApplication sharedApplication];

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
     bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_ QUEUE_PRIORITY_DEFAULT, 0), ^{

    //Do the work associated with the task
    // code for Posting Data Here
    NSLog(@"backgroundTimeRemaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
    });

    }
    - (void)locationManagerCLLocationManager *)manager
       didUpdateToLocationCLLocation *)newLocation
       fromLocationCLLocation *)oldLocation
     {

      NSLog(@"Location Change");
     }
     This Method Call Only Once.

Body:
the call should be

    [locationManager startMonitoringSignificantLocationChanges];


问:
Xdebug V3 doesn't stop breakpoints in VSCode
I am trying to debug on my XAMPP using VSCode and didn't worked. I know there are tons of questions about this and I've tried everything I can but still won't work.

There is really 1 thing weird thou about my xdebug extension.I am currently using PHP v7.4.12 and Xdebug version 3.

I know my Xdebug works on PHP because I viewed phpinfo() and it shows just how I set it. The weird part is among the many of the tutorials out there normally zend_extension = path..,xdebug.remote_enable = 1 and xdebug.remote_autostart = 1 should be enough but when I viewed phpinfo it is as follows below:

I tried to set it with xdebug.remote_port = 9000 and xdebug.remote_host = "localhost" but still won't work. I even have read about changing localhost to 127.0.0.1 but still didn't work.

Any help would be appreciated. Here is my launch.json file and php ini file.

php.ini

zend_extension = "C:\xampp\php\ext\php_xdebug.dll"
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_log = "C:\xampp\tmp\xdebug\xdebug.log"
xdebug.remote_port = 9000
xdebug.remote_host = "127.0.0.1"
xdebug.idekey=VSCODE
```
<p><strong>launch.json</strong></p>
<pre class="lang-json prettyprint-override"><code>{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch current script in console",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "externalConsole": false,
            "port": 9000
        },
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9000
        }
    ]
}
</code>```

If it is caused maybe due to not setting "pathMappings", anyone can teach me how to use this?

By the way the xdebug.log also doesn't work

答:
OS: Windows 10

VS Code marks the breakpoints as "unverified breakpoint".

In my case, i had to add "pathMappings":

```<code>"configurations": [{
    ...,
    "pathMappings": {
        "remote/path/to/webroot/": "${workspaceFolder}",
    }
}]
</code>```

Sidenote when you are working with docker: The remote path is the path inside the docker container.

Body:
I am trying to debug on my XAMPP using VSCode and didn't worked. I know there are tons of questions about this and I've tried everything I can but still won't work.

There is really 1 thing weird thou about my xdebug extension.I am currently using PHP v7.4.12 and Xdebug version 3.

I know my Xdebug works on PHP because I viewed phpinfo() and it shows just how I set it. The weird part is among the many of the tutorials out there normally zend_extension = path..,xdebug.remote_enable = 1 and xdebug.remote_autostart = 1 should be enough but when I viewed phpinfo it is as follows below:

I tried to set it with xdebug.remote_port = 9000 and xdebug.remote_host = "localhost" but still won't work. I even have read about changing localhost to 127.0.0.1 but still didn't work.

Any help would be appreciated. Here is my launch.json file and php ini file.

php.ini

zend_extension = "C:\xampp\php\ext\php_xdebug.dll"
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_log = "C:\xampp\tmp\xdebug\xdebug.log"
xdebug.remote_port = 9000
xdebug.remote_host = "127.0.0.1"
xdebug.idekey=VSCODE

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch current script in console",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "externalConsole": false,
            "port": 9000
        },
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9000
        }
    ]
}

If it is caused maybe due to not setting "pathMappings", anyone can teach me how to use this?

By the way the xdebug.log also doesn't work

Body:
OS: Windows 10

VS Code marks the breakpoints as "unverified breakpoint".

In my case, i had to add "pathMappings":

"configurations": [{
    ...,
    "pathMappings": {
        "remote/path/to/webroot/": "${workspaceFolder}",
    }
}]

Sidenote when you are working with docker: The remote path is the path inside the docker container.