问:
No declaration found for element 'x:books'
I have an xml and an xsd. Both in the same directory. I am trying to validate it using xmlcopyeditor but it returns the following error.

```<code>Error at line 2, column 63: No declaration found for element "x:books"
</code>```

Here is the xml.

file:books.xml

```<code><?xml version="1.0"?>
<x:books xmlns:x="urn:books" x:noNamespaceSchemaLocation="books.xsd">
   <book id="bk001">
      <author>Writer</author>
      <title>The First Book</title>
      <genre>Fiction</genre>
      <price>44.95</price>
      <pub_date>2000-10-01</pub_date>
      <review>An amazing story of nothing.</review>
   </book>

   <book id="bk002">
      <author>Poet</author>
      <title>The Poet's First Poem</title>
      <genre>Poem</genre>
      <price>24.95</price>
      <review>Least poetic poems.</review>
   </book>
</x:books>
</code>```

here is the xsd

file:books.xsd

```<code><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:books"
            xmlns:bks="urn:books">

  <xsd:element name="books" type="bks:BooksForm"/>

  <xsd:complexType name="BooksForm">
    <xsd:sequence>
      <xsd:element name="book"
                  type="bks:BookForm"
                  minOccurs="0"
                  maxOccurs="unbounded"/>
      </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="BookForm">
    <xsd:sequence>
      <xsd:element name="author"   type="xsd:string"/>
      <xsd:element name="title"    type="xsd:string"/>
      <xsd:element name="genre"    type="xsd:string"/>
      <xsd:element name="price"    type="xsd:float" />
      <xsd:element name="pub_date" type="xsd:date" />
      <xsd:element name="review"   type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id"   type="xsd:string"/>
  </xsd:complexType>
</xsd:schema>
</code>```

I found some similar question like No declaration found for element and No declaration found for element ' ' but I have not been able to fix it. I don't understand what's wrong I should be referencing the xsd correctly like that.

答:
There are multiple issues in your XML and XSD, including:

Use schemaLocation, not noNamespaceSchemaLocation, for namespaced XML.

See How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?

Use xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance".

See xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?

Use elementFormDefault in your XSD.

See What does elementFormDefault do in XSD?

The XML and XSD below have all issues resolved.  The fixed XML is now valid against the fixed XSD.

XML

```<code><?xml version="1.0"?>
<books xmlns="urn:books"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="urn:books books.xsd">
   <book id="bk001">
      <author>Writer</author>
      <title>The First Book</title>
      <genre>Fiction</genre>
      <price>44.95</price>
      <pub_date>2000-10-01</pub_date>
      <review>An amazing story of nothing.</review>
   </book>
   <book id="bk002">
      <author>Poet</author>
      <title>The Poet's First Poem</title>
      <genre>Poem</genre>
      <price>24.95</price>
      <review>Least poetic poems.</review>
   </book>
</books>
</code>```

XSD

```<code><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:books"
            xmlns:bks="urn:books"
            elementFormDefault="qualified">
  <xsd:element name="books" type="bks:BooksForm"/>
  <xsd:complexType name="BooksForm">
    <xsd:sequence>
      <xsd:element name="book"
                   type="bks:BookForm"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="BookForm">
    <xsd:sequence>
      <xsd:element name="author"   type="xsd:string"/>
      <xsd:element name="title"    type="xsd:string"/>
      <xsd:element name="genre"    type="xsd:string"/>
      <xsd:element name="price"    type="xsd:float" />
      <xsd:element name="pub_date" type="xsd:date" />
      <xsd:element name="review"   type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id"   type="xsd:string"/>
  </xsd:complexType>
</xsd:schema>
</code>```

Body:
I have an xml and an xsd. Both in the same directory. I am trying to validate it using xmlcopyeditor but it returns the following error.

Error at line 2, column 63: No declaration found for element "x:books"

Here is the xml.

file:books.xml

<?xml version="1.0"?>
<x:books xmlns:x="urn:books" x:noNamespaceSchemaLocation="books.xsd">
   <book id="bk001">
      <author>Writer</author>
      <title>The First Book</title>
      <genre>Fiction</genre>
      <price>44.95</price>
      <pub_date>2000-10-01</pub_date>
      <review>An amazing story of nothing.</review>
   </book>

   <book id="bk002">
      <author>Poet</author>
      <title>The Poet's First Poem</title>
      <genre>Poem</genre>
      <price>24.95</price>
      <review>Least poetic poems.</review>
   </book>
</x:books>

here is the xsd

file:books.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:books"
            xmlns:bks="urn:books">

  <xsd:element name="books" type="bks:BooksForm"/>

  <xsd:complexType name="BooksForm">
    <xsd:sequence>
      <xsd:element name="book"
                  type="bks:BookForm"
                  minOccurs="0"
                  maxOccurs="unbounded"/>
      </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="BookForm">
    <xsd:sequence>
      <xsd:element name="author"   type="xsd:string"/>
      <xsd:element name="title"    type="xsd:string"/>
      <xsd:element name="genre"    type="xsd:string"/>
      <xsd:element name="price"    type="xsd:float" />
      <xsd:element name="pub_date" type="xsd:date" />
      <xsd:element name="review"   type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id"   type="xsd:string"/>
  </xsd:complexType>
</xsd:schema>

I found some similar question like No declaration found for element and No declaration found for element ' ' but I have not been able to fix it. I don't understand what's wrong I should be referencing the xsd correctly like that.

Body:
There are multiple issues in your XML and XSD, including:

Use schemaLocation, not noNamespaceSchemaLocation, for namespaced XML.

See How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?

Use xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance".

See xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?

Use elementFormDefault in your XSD.

See What does elementFormDefault do in XSD?

The XML and XSD below have all issues resolved.  The fixed XML is now valid against the fixed XSD.

XML

<?xml version="1.0"?>
<books xmlns="urn:books"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="urn:books books.xsd">
   <book id="bk001">
      <author>Writer</author>
      <title>The First Book</title>
      <genre>Fiction</genre>
      <price>44.95</price>
      <pub_date>2000-10-01</pub_date>
      <review>An amazing story of nothing.</review>
   </book>
   <book id="bk002">
      <author>Poet</author>
      <title>The Poet's First Poem</title>
      <genre>Poem</genre>
      <price>24.95</price>
      <review>Least poetic poems.</review>
   </book>
</books>

XSD

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:books"
            xmlns:bks="urn:books"
            elementFormDefault="qualified">
  <xsd:element name="books" type="bks:BooksForm"/>
  <xsd:complexType name="BooksForm">
    <xsd:sequence>
      <xsd:element name="book"
                   type="bks:BookForm"
                   minOccurs="0"
                   maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="BookForm">
    <xsd:sequence>
      <xsd:element name="author"   type="xsd:string"/>
      <xsd:element name="title"    type="xsd:string"/>
      <xsd:element name="genre"    type="xsd:string"/>
      <xsd:element name="price"    type="xsd:float" />
      <xsd:element name="pub_date" type="xsd:date" />
      <xsd:element name="review"   type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id"   type="xsd:string"/>
  </xsd:complexType>
</xsd:schema>


来源: stackexchange

问:
Where do you create the ini file? (Uwsgi)
my reference:
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/uwsgi/

my project name:

'mysite'

my directories:

I create 'uwsgi.ini'.

then, I written in ini file.

```<code>[uwsgi]
chdir=/var/www/html/portfolio/mysite
module=mysite.wsgi:application
master=True
pidfile=/tmp/project-master.pid
vacuum=True
max-requests=5000
daemonize=/var/log/uwsgi/yourproject.log
single-interpreter=True
</code>```

then, I command 'service apache2 restart'

I command this 'uwsgi --ini uwsgi.ini'

```<code># uwsgi --ini uwsgi.ini
# open("/var/log/uwsgi/yourproject.log"): No such file or directory [core/logging.c line 288]
</code>```

答:
reference(japanese)

Ubuntu server fails

```<code>(console)
# pip3 install uwsgi
  Installing collected packages: uwsgi
  Successfully installed uwsgi-2.0.20
# mkdir -p /etc/uwsgi/vassals
# cd /etc/uwsgi/vassals
# source /var/www/html/venv/bin/activate

# vi uwsgi.ini
</code>```
```<code>(uwsgi.ini(new))
[uwsgi]
chdir=/var/www/html/portfolio/mysite
module=mysite.wsgi:application
master=True
pidfile=/tmp/project-master.pid
vacuum=True
max-requests=5000
daemonize=/var/log/uwsgi/uwsgi.log
single-interpreter=True
</code>```
```<code>(console)
# adduser --group uwsgi-data
# adduser --home /etc/uwsgi --no-create-home --shell /sbin/nologin --ingroup uwsgi-data --disabled-login uwsgi-data
  Adding user `uwsgi-data' ...
  Adding new user `uwsgi-data' (1001) with group `uwsgi-data' ...
  Not creating home directory `/etc/uwsgi'.
  Changing the user information for uwsgi-data
  Enter the new value, or press ENTER for the
  default
        Full Name []:  # Empty and OK
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
  Is the information correct? [Y/n] Y
</code>```
```<code>(console)
# chown -R uwsgi-data:uwsgi-data /etc/uwsgi
# mkdir /var/log/uwsgi
# chown -R uwsgi-data:uwsgi-data /var/log/uwsgi
# vi /etc/systemd/system/emperor.uwsgi.service
</code>```
```<code>(emperor.uwsgi.service)
[Unit]
Description=uWSGI Emperor
After=syslog.service

[Service]
# Find the configuration file that exists in "/etc/uwsgi/vassals" and start the uWSGI daemon.
ExecStart=/var/www/html/venv/bin/uwsgi --master --emperor /etc/uwsgi/vassals
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target
</code>```
```<code>(console)
# chmod 755 /etc/systemd/system/emperor.uwsgi.service
# vi /etc/uwsgi/vassals/emperor.ini
</code>```
```<code>(emperor.ini)
[uwsgi]
emperor = /etc/uwsgi/vassals
uid = uwsgi-data
gid = uwsgi-data
</code>```
```<code>(console)
# systemctl start emperor.uwsgi.service
# systemctl status emperor.uwsgi.service
  ● emperor.uwsgi.service - uWSGI Emperor
     Loaded: loaded (/etc/systemd/system/emperor.uwsgi.service; disabled; vendor preset: enabled)
     Active: active (running)

# service apache2 restart
</code>```

ContentLicense: CC BY-SA 4.0

Body:
my reference:
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/uwsgi/

my project name:

'mysite'

my directories:

I create 'uwsgi.ini'.

then, I written in ini file.

[uwsgi]
chdir=/var/www/html/portfolio/mysite
module=mysite.wsgi:application
master=True
pidfile=/tmp/project-master.pid
vacuum=True
max-requests=5000
daemonize=/var/log/uwsgi/yourproject.log
single-interpreter=True

then, I command 'service apache2 restart'

I command this 'uwsgi --ini uwsgi.ini'

# uwsgi --ini uwsgi.ini
# open("/var/log/uwsgi/yourproject.log"): No such file or directory [core/logging.c line 288]

Body:
reference(japanese)

Ubuntu server fails

(console)
# pip3 install uwsgi
  Installing collected packages: uwsgi
  Successfully installed uwsgi-2.0.20
# mkdir -p /etc/uwsgi/vassals
# cd /etc/uwsgi/vassals
# source /var/www/html/venv/bin/activate

# vi uwsgi.ini

(uwsgi.ini(new))
[uwsgi]
chdir=/var/www/html/portfolio/mysite
module=mysite.wsgi:application
master=True
pidfile=/tmp/project-master.pid
vacuum=True
max-requests=5000
daemonize=/var/log/uwsgi/uwsgi.log
single-interpreter=True

(console)
# adduser --group uwsgi-data
# adduser --home /etc/uwsgi --no-create-home --shell /sbin/nologin --ingroup uwsgi-data --disabled-login uwsgi-data
  Adding user `uwsgi-data' ...
  Adding new user `uwsgi-data' (1001) with group `uwsgi-data' ...
  Not creating home directory `/etc/uwsgi'.
  Changing the user information for uwsgi-data
  Enter the new value, or press ENTER for the
  default
        Full Name []:  # Empty and OK
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
  Is the information correct? [Y/n] Y

(console)
# chown -R uwsgi-data:uwsgi-data /etc/uwsgi
# mkdir /var/log/uwsgi
# chown -R uwsgi-data:uwsgi-data /var/log/uwsgi
# vi /etc/systemd/system/emperor.uwsgi.service

(emperor.uwsgi.service)
[Unit]
Description=uWSGI Emperor
After=syslog.service

[Service]
# Find the configuration file that exists in "/etc/uwsgi/vassals" and start the uWSGI daemon.
ExecStart=/var/www/html/venv/bin/uwsgi --master --emperor /etc/uwsgi/vassals
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

(console)
# chmod 755 /etc/systemd/system/emperor.uwsgi.service
# vi /etc/uwsgi/vassals/emperor.ini

(emperor.ini)
[uwsgi]
emperor = /etc/uwsgi/vassals
uid = uwsgi-data
gid = uwsgi-data

(console)
# systemctl start emperor.uwsgi.service
# systemctl status emperor.uwsgi.service
  ● emperor.uwsgi.service - uWSGI Emperor
     Loaded: loaded (/etc/systemd/system/emperor.uwsgi.service; disabled; vendor preset: enabled)
     Active: active (running)

# service apache2 restart


问:
How to encode periods for URLs in Javascript?
The SO post below is comprehensive, but all three methods described fail to encode for periods.

Post: Encode URL in JavaScript?

For instance, if I run the three methods (i.e., escape, encodeURI, encodeURIComponent), none of them encode periods.

So "food.store" comes out as "food.store," which breaks the URL.  It breaks the URL because the Rails app cannot recognize the URL as valid and displays the 404 error page.  Perhaps it's a configuration mistake in the Rails routes file?

What's the best way to encode periods with Javascript for URLs?

答:
I know this is an old thread, but I didn't see anywhere here any examples of URLs that were causing the original problem. I encountered a similar problem myself a couple of days ago with a Java application. In my case, the string with the period was at the end of the path element of the URL eg.

http://myserver.com/app/servlet/test.string

In this case, the Spring library I'm using was only passing me the 'test' part of that string to the relevant annotated method parameter of my controller class, presumably because it was treating the '.string' as a file extension and stripping it away. Perhaps this is the same underlying issue with the original problem above?

Anyway, I was able to workaround this simply by adding a trailing slash to the URL. Just throwing this out there in case it is useful to anybody else.

John

ContentLicense: CC BY-SA 2.5

ContentLicense: CC BY-SA 3.0


问:
Where is SPL BSS? SRAM or SDRAM? What's the difference?
The SPL linker script defines two segments:

arch/arm/cpu/armv8/u-boot-spl.lds

```<code>MEMORY { .sram : ORIGIN = IMAGE_TEXT_BASE,
        LENGTH = IMAGE_MAX_SIZE }
MEMORY { .sdram : ORIGIN = CONFIG_SPL_BSS_START_ADDR,
        LENGTH = CONFIG_SPL_BSS_MAX_SIZE }
</code>```

Some boards, for example imx8ulp_evk, The address of SPL BSS is defined in SRAM:

```<code>#ifdef CONFIG_SPL_BUILD
#define CONFIG_SPL_STACK        0x22050000
#define CONFIG_SPL_BSS_START_ADDR   0x22048000
#define CONFIG_SPL_BSS_MAX_SIZE     0x2000  /* 8 KB */
#define CONFIG_SYS_SPL_MALLOC_START 0x22040000
#define CONFIG_SYS_SPL_MALLOC_SIZE  0x8000  /* 32 KB */

</code>```

But other defined in SDRAM.

What's the difference?

答:
The SPL linker script defines two segments:

The SRAM segment is for the memory occupied by SPL text, data, and stack.

The SDRAM segment is the destination memory for the image loaded by the SPL.

SRAM (static RAM) does not require any initialization prior to using it. After a system reset, the processor can immediately use (load/store) SRAM. Therefore some SoCs (i.e. MPUs rather than MCUs) integrate a small chunk of SRAM into the chip as "always available memory" for the CPU. There are other  attributes that differentiate SRAM from DRAM (e.g. speed, die size, cost), but are irrelevant to your question.

SDRAM is a type of dynamic RAM (i.e. synchronous DRAM), memory that requires a refresh controller that needs to be initialized (by software) before use. The DRAM is typically external to the SoC for board design flexibility.
Beware that some/many people insist on referring to the DRAM by the more-specific implementation technology, e.g. "DDR2", "LPDDR3", or as you did "SDRAM".  Regardless, the salient property of such memories is that it's dynamic RAM and there's a DRAM controller to perform refresh.

Before U-Boot can be loaded into main memory (implemented using some type of dynamic memory, e.g. SDRAM, DDR3), a previous boot program must perform that DRAM controller initialization. That's the rationale for existence of the (internal) SRAM and use of an (additional) SPL program.

In theory the ROM boot program could perform this DRAM controller initialization.  But that assumes that all of the necessary parameters (of external memory chips) are somehow available to this ROM boot program.  To cope with different board configurations, such DRAM parameters would have to be stored in a NVM somewhere.  But such a scheme is atypical for ARM SoCs; ARM SoCs try to avoid the inflexibility of (masked) ROM by deferring more board initialization tasks to (board-specific) 2nd-stage and 3rd-stage boot programs.

Note there are also other boot schemes, for instance the use of the XIP capability of NOR flash.

Some boards, for example imx8ulp_evk, The address of SPL BSS is defined in SRAM:

...

But other defined in SDRAM.

(You neglect to provide any example(s) for the later case.)

The SPL code would be loaded into SRAM by the ROM bootloader (of the SoC), and also use SRAM for the initial stack & data.  But the SPL functionality is bisected by the initialization of the DRAM controller.

The early stage of SPL should not depend on the availability of BSS, so code should use only stack variables and global_data, and not use static/global variables.

Once the DRAM controller is initialized, the BSS is cleared and available, and all static/global variables can be used.  Even the stack can be optionally be moved to DRAM.
SPL can then perform its primary function, and load the U-Boot image (e.g. u-boot.bin) from the designated NVM to main memory (DRAM).

These code restrictions are documented in the U-Boot README file, the Board Initialisation Flow section.

What's the difference?

The primary difference would be available memory size.  The code restrictions on how the BSS section can be used in SPL code (as well as U-Boot itself) allow the BSS to be defined in either static or dynamic RAM.  Since the internal SRAM is limited in size, and external DRAM is board dependent but presumably much, much larger, that may decide the assignment.

The Kconfig entry for CONFIG_SPL_SEPARATE_BSS in common/spl/Kconfig explains the reason for this option:

```<code>config SPL_SEPARATE_BSS
    bool "BSS section is in a different memory region from text"
    help
      Some platforms need a large BSS region in SPL and can provide this
      because RAM is already set up. In this case BSS can be moved to RAM.
      This option should then be enabled so that the correct device tree
      location is used. Normally we put the device tree at the end of BSS
      but with this option enabled, it goes at _image_binary_end.
</code>```

Body:
The SPL linker script defines two segments:

arch/arm/cpu/armv8/u-boot-spl.lds

MEMORY { .sram : ORIGIN = IMAGE_TEXT_BASE,
        LENGTH = IMAGE_MAX_SIZE }
MEMORY { .sdram : ORIGIN = CONFIG_SPL_BSS_START_ADDR,
        LENGTH = CONFIG_SPL_BSS_MAX_SIZE }

Some boards, for example imx8ulp_evk, The address of SPL BSS is defined in SRAM:

#ifdef CONFIG_SPL_BUILD
#define CONFIG_SPL_STACK        0x22050000
#define CONFIG_SPL_BSS_START_ADDR   0x22048000
#define CONFIG_SPL_BSS_MAX_SIZE     0x2000  /* 8 KB */
#define CONFIG_SYS_SPL_MALLOC_START 0x22040000
#define CONFIG_SYS_SPL_MALLOC_SIZE  0x8000  /* 32 KB */

But other defined in SDRAM.

What's the difference?

Body:
The SPL linker script defines two segments:

The SRAM segment is for the memory occupied by SPL text, data, and stack.

The SDRAM segment is the destination memory for the image loaded by the SPL.

SRAM (static RAM) does not require any initialization prior to using it. After a system reset, the processor can immediately use (load/store) SRAM. Therefore some SoCs (i.e. MPUs rather than MCUs) integrate a small chunk of SRAM into the chip as "always available memory" for the CPU. There are other  attributes that differentiate SRAM from DRAM (e.g. speed, die size, cost), but are irrelevant to your question.

SDRAM is a type of dynamic RAM (i.e. synchronous DRAM), memory that requires a refresh controller that needs to be initialized (by software) before use. The DRAM is typically external to the SoC for board design flexibility.
Beware that some/many people insist on referring to the DRAM by the more-specific implementation technology, e.g. "DDR2", "LPDDR3", or as you did "SDRAM".  Regardless, the salient property of such memories is that it's dynamic RAM and there's a DRAM controller to perform refresh.

Before U-Boot can be loaded into main memory (implemented using some type of dynamic memory, e.g. SDRAM, DDR3), a previous boot program must perform that DRAM controller initialization. That's the rationale for existence of the (internal) SRAM and use of an (additional) SPL program.

In theory the ROM boot program could perform this DRAM controller initialization.  But that assumes that all of the necessary parameters (of external memory chips) are somehow available to this ROM boot program.  To cope with different board configurations, such DRAM parameters would have to be stored in a NVM somewhere.  But such a scheme is atypical for ARM SoCs; ARM SoCs try to avoid the inflexibility of (masked) ROM by deferring more board initialization tasks to (board-specific) 2nd-stage and 3rd-stage boot programs.

Note there are also other boot schemes, for instance the use of the XIP capability of NOR flash.

Some boards, for example imx8ulp_evk, The address of SPL BSS is defined in SRAM:

...

But other defined in SDRAM.

(You neglect to provide any example(s) for the later case.)

The SPL code would be loaded into SRAM by the ROM bootloader (of the SoC), and also use SRAM for the initial stack & data.  But the SPL functionality is bisected by the initialization of the DRAM controller.

The early stage of SPL should not depend on the availability of BSS, so code should use only stack variables and global_data, and not use static/global variables.

Once the DRAM controller is initialized, the BSS is cleared and available, and all static/global variables can be used.  Even the stack can be optionally be moved to DRAM.
SPL can then perform its primary function, and load the U-Boot image (e.g. u-boot.bin) from the designated NVM to main memory (DRAM).

These code restrictions are documented in the U-Boot README file, the Board Initialisation Flow section.

What's the difference?

The primary difference would be available memory size.  The code restrictions on how the BSS section can be used in SPL code (as well as U-Boot itself) allow the BSS to be defined in either static or dynamic RAM.  Since the internal SRAM is limited in size, and external DRAM is board dependent but presumably much, much larger, that may decide the assignment.

The Kconfig entry for CONFIG_SPL_SEPARATE_BSS in common/spl/Kconfig explains the reason for this option:

config SPL_SEPARATE_BSS
    bool "BSS section is in a different memory region from text"
    help
      Some platforms need a large BSS region in SPL and can provide this
      because RAM is already set up. In this case BSS can be moved to RAM.
      This option should then be enabled so that the correct device tree
      location is used. Normally we put the device tree at the end of BSS
      but with this option enabled, it goes at _image_binary_end.


问:
How to calculate the average of all vertical, horizontal and diagonal neighbours of a cell in a matrix (2D-list)?
Given a 2D-list of integers, how can I calculate the average of the vertical, horizontal and diagonal neighbors of each element? The results need to be printed as floating point numbers and in a matrix with every row on one line.

So an example 2D-list is:

```<code>matrix = [[3, 9, 0],
          [4, 1, 4],
          [3, 8, 5]]
</code>```

and the output for this one would then be:

```<code>[4.666666666666667, 2.4, 4.666666666666667]
[4.8, 4.5, 4.6]
[4.333333333333333, 3.4, 4.333333333333333]
</code>```

As further clarification. For the element in row and column 1 it would be:

(9+4+1)/3 = 4.667

答:
Okay, so to make it easy, just distinguish that there are three cases for EACH SYMETRIC matrix:

Element is the corner - it has 3 neighbours

Element is part of the edge, but not the corner - it has 5 neighbours

Element is neither corner or edge - it has 8 neighours

There are also 4 possible corners and 4 possible eges for all 2D matrixes.(Excluding those with 1 column, row, which I would call vectors)

Pseudocode:(We assume proper matrix with equal numbers of elements in rows and columns)

```<code>matrix = [[1,2,3],[4,5,6],[7,8,9]]
rows=len(matrix)
cols=len(matrix[0])
sumation=0
for i in range(len(matrix)):
    doesleftexist=1
    doestopexist=1
    doesbottomexist=1
    doesrightexist=1
    for j in range(len(matrix[0])):
        if(i==0): #element to the top doesn't exist
            doesleftexist=0
        if(j==0): #element to the left doesn't exist
            doestopexist=0
        if(i==rows-1) #element to the bottom doesn't exist
            doesbottomexist=0
        if(j==cols-1) #element to the right doesn't exist
            doesrightexist=0
        if(doesleftexist=0 && doesbottomexist=0): #we are at bottom left
            sumation=matrix[rows-2][0]+matrix[rows-2][1]+matrix[rows-1][1]
        ... acordingly with different corners,
        each different case is else if

        if(doesbottomexist=0&&(doesleftexist==1 && doesrightexist==1)) #we are at bottom "not corner"
            sumation=matrix[i][j-1]+matrix[i-1][j-1]+matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1]
        ... acordingly with different edges
        each different case is else if

        else:
            sumation = [8 elements around the matrix[i][j]]

</code>```

I didn't apply the list sumation, but it's easy just adding the incrementation of some dummy variable, knowing that you will go in each row by column.
It's kinda long and for sure not optimal solution to the problem (17 cases in total), but for sure it will give a solution for all matrixes, which are atleast 3x3.

Body:
Given a 2D-list of integers, how can I calculate the average of the vertical, horizontal and diagonal neighbors of each element? The results need to be printed as floating point numbers and in a matrix with every row on one line.

So an example 2D-list is:

matrix = [[3, 9, 0],
          [4, 1, 4],
          [3, 8, 5]]

and the output for this one would then be:

[4.666666666666667, 2.4, 4.666666666666667]
[4.8, 4.5, 4.6]
[4.333333333333333, 3.4, 4.333333333333333]

As further clarification. For the element in row and column 1 it would be:

(9+4+1)/3 = 4.667

Body:
Okay, so to make it easy, just distinguish that there are three cases for EACH SYMETRIC matrix:

Element is the corner - it has 3 neighbours

Element is part of the edge, but not the corner - it has 5 neighbours

Element is neither corner or edge - it has 8 neighours

There are also 4 possible corners and 4 possible eges for all 2D matrixes.(Excluding those with 1 column, row, which I would call vectors)

Pseudocode:(We assume proper matrix with equal numbers of elements in rows and columns)

matrix = [[1,2,3],[4,5,6],[7,8,9]]
rows=len(matrix)
cols=len(matrix[0])
sumation=0
for i in range(len(matrix)):
    doesleftexist=1
    doestopexist=1
    doesbottomexist=1
    doesrightexist=1
    for j in range(len(matrix[0])):
        if(i==0): #element to the top doesn't exist
            doesleftexist=0
        if(j==0): #element to the left doesn't exist
            doestopexist=0
        if(i==rows-1) #element to the bottom doesn't exist
            doesbottomexist=0
        if(j==cols-1) #element to the right doesn't exist
            doesrightexist=0
        if(doesleftexist=0 && doesbottomexist=0): #we are at bottom left
            sumation=matrix[rows-2][0]+matrix[rows-2][1]+matrix[rows-1][1]
        ... acordingly with different corners,
        each different case is else if

        if(doesbottomexist=0&&(doesleftexist==1 && doesrightexist==1)) #we are at bottom "not corner"
            sumation=matrix[i][j-1]+matrix[i-1][j-1]+matrix[i-1][j]+matrix[i-1][j+1]+matrix[i][j+1]
        ... acordingly with different edges
        each different case is else if

        else:
            sumation = [8 elements around the matrix[i][j]]

I didn't apply the list sumation, but it's easy just adding the incrementation of some dummy variable, knowing that you will go in each row by column.
It's kinda long and for sure not optimal solution to the problem (17 cases in total), but for sure it will give a solution for all matrixes, which are atleast 3x3.


问:
extends number but do not restrict to a specific number
Probably been asked and answered but I didn't find the answer - so I ask.

The workaround is not great as if I want to include more type it will not be as easy to upgrade.
Any better way of doing it ?

declare const max:  (a: A) => (b: A) => A

max (2) (3)     // Argument of type '3' is not assignable to parameter of type '2'
max ('a') ('b') // Argument of type '"b"' is not assignable to parameter of type '"a"'

// Possible workaround but will become clumsy if possible type to extends grow

declare const maxClumsy: {
    (a: number): (b: number) => number
    (a: string): (b: string) => string
}

maxClumsy (2) (3)
maxClumsy ('a') ('b')
```

playground link

答:
Following up on the comment from @matt-diamond

Keep in mind that you're passing in literals here... if the input variables are typed more broadly, you won't have an issue.

If you don't want to have to pass explicitly typed variables each time, you can use this method.

```<code>type Num = number | string

type AsNum<T> = T extends string ? string :
   T extends number ? number : never

declare const max: <T extends Num>(a: T) => (b: AsNum<T>) => AsNum<T>

max(2,4)  max('2','4') // OK
max(2,'4') // Argument of type 'string' is not assignable to parameter of type 'number'
max(true,false) // Argument of type 'boolean' is not assignable to parameter of type 'string | number'
</code>```

Although it does look just as "clumsy" as your working suggestion, if not more.

Body:
Probably been asked and answered but I didn't find the answer - so I ask.

The workaround is not great as if I want to include more type it will not be as easy to upgrade.
Any better way of doing it ?

declare const max:  (a: A) => (b: A) => A

max (2) (3)     // Argument of type '3' is not assignable to parameter of type '2'
max ('a') ('b') // Argument of type '"b"' is not assignable to parameter of type '"a"'

// Possible workaround but will become clumsy if possible type to extends grow

declare const maxClumsy: {
    (a: number): (b: number) => number
    (a: string): (b: string) => string
}

maxClumsy (2) (3)
maxClumsy ('a') ('b')

<a href="https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEYD2A7AzgF3gWygDwC54AeAQXhDwxBWDXhQFdsAjEGeAH3kxgEsUAcwB88ABRQipAJTwAvKLEspshfFIBYAFDbcecQCZZYgMyz4F+AHor6mIOY0sSAGbwMATwAOCAOQnfeD56FCQsKDQ0PkEUKBYIBAwkeC9YKGwQag5Xd28-A19dfHFfKF9jXxZy61tSe0cUZzdPH3hfACIWdsDghjD4CKiYuIT3ZNSYdMz2eByWv3aobu1tG3gABSRIvniEAHckGABrWCRGWngWRiw9vggIS-AkDMQIZjQPILcvLajd3NaSQoVBodHgghgSD2Ky0oEgsAQyHQWD0AGE3tgPkQAN7aSziSQMZhsGDSIhKIhMVjsVSiKkkvGWCREXgCQRk8TKHgYfhCWnc3mCbQAXxhaIxH0MxjMRTw6PenzEpWqSqq0iAA" rel="nofollow noreferrer">playground link

Body:
Following up on the comment from @matt-diamond

Keep in mind that you're passing in literals here... if the input variables are typed more broadly, you won't have an issue.

If you don't want to have to pass explicitly typed variables each time, you can use this method.

type Num = number | string

type AsNum<T> = T extends string ? string :
   T extends number ? number : never

declare const max: <T extends Num>(a: T) => (b: AsNum<T>) => AsNum<T>

max(2,4)  max('2','4') // OK
max(2,'4') // Argument of type 'string' is not assignable to parameter of type 'number'
max(true,false) // Argument of type 'boolean' is not assignable to parameter of type 'string | number'

Although it does look just as "clumsy" as your working suggestion, if not more.


问:
Oracle - Frequently accessed tables
We're running Oracle 12c SE.  I've read a lot of postings that say v$segment_statistics may have information on which are the most frequently queried or updated objects.  However, can that be broken down?   Say that one might want to see during what times of the day certain objects are hotter than other objects, or perhaps number of physical reads or writes per hour for a given table?

Does Oracle SE offer this an any of the v$ views?

答:
It sounds like you are describing dba_hist_seg_stat which is one of the tables that is populated as part of the Automatic Workload Repository (AWR).  If you're on the standard edition, I don't believe querying these views would violate your license agreement but I don't keep up to date with changes to licensing terms particularly for the standard edition.

You could replicate this functionality yourself by putting together a job that runs every few minutes, queries v$segment_statistics, and writes the delta from the prior snap to a custom table.  You could then query that table to see what activity was going on at different points in time.

Title: Oracle - Frequently accessed tables


问:
tidyverse , plyr and dplyr
I used to use dplyr everywhere, but have included some plyr functions . To be honest I do not know what the difference is or why things have changed . The same code results in different dataframes depending on whether I have imported plyr or tidyverse .
What I wanted to have is a dataframe named newborn_stat  containing unique pids in each row  and a new c_pos column.

```<code>raw_file_contents<- data.frame( pid=c(1,2,2,3,3), C_SYMP=c("Y","N","Y","N","N"))
newborn_stat<- raw_file_contents %>%
        group_by(pid) %>%
        summarise(c_pos= any( C_SYMP == "Y", na.rm = TRUE))
</code>```

instead I would get a data frame ,newborn_stat, of 1 rows
with c_pos equal to TRUE.
If I use dplyr::  with the group_by and summarise , I think I get the right answer.
Why does this happen?
I have been using a Rmd notebook, so when I tried to run the previous chunk containing this line, things didn't work.

答:
So I assumed this was due to the functions of dplyr and plyr working differently. It was a mostly correct assumption as but both dplyr and plyr has summarize functions, while  dplyr has group_by , but plyr doesn't. If you import  plyr at a later chunk and then rerun the expression shown in the question, summarize  is assumed to be the one from plyr namespace. so you are running dplyr::group_by + plyr::summarize in that expression.

I wish they used a different name for summarize in plyr. or keep the same function names throughout , if that makes sense.

Title: tidyverse , plyr and dplyr

Body:
I used to use dplyr everywhere, but have included some plyr functions . To be honest I do not know what the difference is or why things have changed . The same code results in different dataframes depending on whether I have imported plyr or tidyverse .
What I wanted to have is a dataframe named newborn_stat  containing unique pids in each row  and a new c_pos column.

raw_file_contents<- data.frame( pid=c(1,2,2,3,3), C_SYMP=c("Y","N","Y","N","N"))
newborn_stat<- raw_file_contents %>%
        group_by(pid) %>%
        summarise(c_pos= any( C_SYMP == "Y", na.rm = TRUE))

instead I would get a data frame ,newborn_stat, of 1 rows
with c_pos equal to TRUE.
If I use dplyr::  with the group_by and summarise , I think I get the right answer.
Why does this happen?
I have been using a Rmd notebook, so when I tried to run the previous chunk containing this line, things didn't work.


问:
mypy doesn't complain when class doesn't implement protocol
I have the following code. Class A contains a function f. Class B inherits from A and overrides this function. I also have protocol SupportsG which acts as a structural supertype to all classes implementing a function g. I have class B explicitly inherit this protocol:

```<code>from typing import Protocol

class SupportsG(Protocol):
  def g(self) -> str: ...

class A:
  def f(self) -> str:
    return ""

class B(A, SupportsG):
  def f(self) -> str:
    return "test"

if __name__ == "__main__":
  b = B()
</code>```

My expectation here is that mypy would throw an error, because class B doesn't implement function g. However, if I run mypy --strict I get no errors.

What is the gap in my understanding here?

答:
def test(x: SupportsG):
  pass  # whatever

a = A()
b = B()

test(a)  # mypy should complain
test(b)  # mypy should complain

class C:
  def g(self) -> str:
    return ""

c = C()
test(c)  # should be fine
```
<p>You don't need to inherit your class from <code>SupportsG</code>.</p>
<p>Btw, one purpose for <code>Protocol</code> is also to define custom function interfaces with kwargs, which is not possible with <code>Callable</code>, like:</p>
<pre class="lang-py prettyprint-override"><code>def func(a, *, b) -> str:
  pass

class Func(Protocol):
  def __call__(self, a, *, b) -> str: ...

def test(f: Func):
  pass

test(func)  # ok!
</code>```

Body:
I have the following code. Class A contains a function f. Class B inherits from A and overrides this function. I also have protocol SupportsG which acts as a structural supertype to all classes implementing a function g. I have class B explicitly inherit this protocol:

from typing import Protocol

class SupportsG(Protocol):
  def g(self) -> str: ...

class A:
  def f(self) -> str:
    return ""

class B(A, SupportsG):
  def f(self) -> str:
    return "test"

if __name__ == "__main__":
  b = B()

My expectation here is that mypy would throw an error, because class B doesn't implement function g. However, if I run mypy --strict I get no errors.

What is the gap in my understanding here?

Body:
def test(x: SupportsG):
  pass  # whatever

a = A()
b = B()

test(a)  # mypy should complain
test(b)  # mypy should complain

class C:
  def g(self) -> str:
    return ""

c = C()
test(c)  # should be fine

You don't need to inherit your class from SupportsG.

Btw, one purpose for Protocol is also to define custom function interfaces with kwargs, which is not possible with Callable, like:

def func(a, *, b) -> str:
  pass

class Func(Protocol):
  def __call__(self, a, *, b) -> str: ...

def test(f: Func):
  pass

test(func)  # ok!


问:
Regex for parsing list of floating numbers in python
What's the best why to parse lists of floating numbers separated by space in python?

I have rows of this kind that will come out from a source:

```<code>string = "  4    1.6  8.29   0   0  3.55e-15 -1.28e-13   "
</code>```

with an unknown number of whitespaces separating the numbers and on the beggining or end of the string.

I usually just used numbers = map(float, string.split(" ")) when I could guarantee that there would be only one space between the numbers, and that there would not be spaces in the beginning or end of the string. I'm not skilled in regexps, but someone suggested me to use re.split("\s+", string) but that doesn't work because sometimes I got empty strings in the beggining of the resulting list.

I'm using now: re.split("\s+") but that doesn't work because sometimes I got empty strings in the beggining of the resulting list.

I'm using now:

```<code>res = map(float, re.findall("\d+\S*\d*", string)
</code>```

which is working somehow, but looks fragile and ugly to me. It can match many strings that will fail to produce numbers.

What's the best regexp pattern that will always match integers and floating point numbers with or without exponential notation so that I could use re.findall(patt, string) and safely recover a list of numbers?

答: No regex is needed. Simply use string.split() without any arguments; it will split on any amount of whitespace.

Body:
What's the best why to parse lists of floating numbers separated by space in python?

I have rows of this kind that will come out from a source:

string = "  4    1.6  8.29   0   0  3.55e-15 -1.28e-13   "

with an unknown number of whitespaces separating the numbers and on the beggining or end of the string.

I usually just used numbers = map(float, string.split(" ")) when I could guarantee that there would be only one space between the numbers, and that there would not be spaces in the beginning or end of the string. I'm not skilled in regexps, but someone suggested me to use re.split("\s+", string) but that doesn't work because sometimes I got empty strings in the beggining of the resulting list.

I'm using now: re.split("\s+") but that doesn't work because sometimes I got empty strings in the beggining of the resulting list.

I'm using now:

res = map(float, re.findall("\d+\S*\d*", string)

which is working somehow, but looks fragile and ugly to me. It can match many strings that will fail to produce numbers.

What's the best regexp pattern that will always match integers and floating point numbers with or without exponential notation so that I could use re.findall(patt, string) and safely recover a list of numbers?


问:
sed: delete items between multiple pattern matches (issue with greediness)
So Ive spent a few hours looking at similar articles - helpful yes but not enough to get me over the hump.

I have a line I want to keep the beginning of and then surgically isolate matching patterns after to the end of the string.

For example heres the original line (easy to focus on just one line):

```<code>ltm pool gateway_ping { description "healthcheck pool" load-balancing-mode least-connections-node members { 10.100.100.4:icmp { address 10.100.100.4 session monitor-enabled state up } server2name:https { address 1.1.1.1 session monitor-enabled state up } } monitor Pingmonitor
</code>```

This shortens and isolates the name like Im trying:

cat config.txt | sed -e 's/^ltm \(pool .*\) { .*members { /\1 members /'

gets me this:

```<code>pool gateway_ping members 10.100.100.4:icmp { address 10.100.100.4 session monitor-enabled state up } server2name:https { address 1.1.1.1 session monitor-enabled state up } } monitor Pingmonitor
</code>```

Now to pipe the results to another sed and somehow get rid of anything between the each instance of braces

```<code>{ address 10.100.100.4 session monitor-enabled state up }
{ address 1.1.1.1 session monitor-enabled state up }
</code>```

adding this:

| sed -e 's/{ address .* } / /g'

I ended up with this haircut giving me the first match correctly but blowing out all the rest:

```<code>pool gateway_ping members 10.100.100.4:icmp monitor Pingmonitor }
</code>```

I really need the other members in there - the line should read:

```<code>pool gateway_ping members 10.100.100.4:icmp server2name:https monitor Pingmonitor }
</code>```

I can get rid of the monitor Pingmonitor }    Im just stuck on how to NOT have SED be greedy and blow out all matches to the very LAST end brace... I'd like it to stop at the first end brace... then continue on matching individually.

I think I may have murdered this by trying to describe it.   Hopefully I didnt lose the audience :-)

Body:
So Ive spent a few hours looking at similar articles - helpful yes but not enough to get me over the hump.

I have a line I want to keep the beginning of and then surgically isolate matching patterns after to the end of the string.

For example heres the original line (easy to focus on just one line):

ltm pool gateway_ping { description "healthcheck pool" load-balancing-mode least-connections-node members { 10.100.100.4:icmp { address 10.100.100.4 session monitor-enabled state up } server2name:https { address 1.1.1.1 session monitor-enabled state up } } monitor Pingmonitor

This shortens and isolates the name like Im trying:

cat config.txt | sed -e 's/^ltm \(pool .*\) { .*members { /\1 members /'

gets me this:

pool gateway_ping members 10.100.100.4:icmp { address 10.100.100.4 session monitor-enabled state up } server2name:https { address 1.1.1.1 session monitor-enabled state up } } monitor Pingmonitor

Now to pipe the results to another sed and somehow get rid of anything between the each instance of braces

{ address 10.100.100.4 session monitor-enabled state up }
{ address 1.1.1.1 session monitor-enabled state up }

adding this:

| sed -e 's/{ address .* } / /g'

I ended up with this haircut giving me the first match correctly but blowing out all the rest:

pool gateway_ping members 10.100.100.4:icmp monitor Pingmonitor }

I really need the other members in there - the line should read:

pool gateway_ping members 10.100.100.4:icmp server2name:https monitor Pingmonitor }

I can get rid of the monitor Pingmonitor }    Im just stuck on how to NOT have SED be greedy and blow out all matches to the very LAST end brace... I'd like it to stop at the first end brace... then continue on matching individually.

I think I may have murdered this by trying to describe it.   Hopefully I didnt lose the audience :-)

Body:
Using sed

$ sed 's/[^ ]* \([^{]*\).*\(members\) {\([0-9.]*[^{]*\)[^}]*}\([^{]*\).*} \(}\)\(.*\)/\1\2\3\4\6 \5/' file
pool gateway_ping members 10.100.100.4:icmp  server2name:https  monitor Pingmonitor }


问:
Product of Array Except Self Javascript
I have been trying implement product of Array Except Self. My initial thought was to use indexes === array[j] and continue, but it is not working. Can anyone explain why?

```<code> var array = [1,2,3,4];
    function productOfArrayExceptSelf(array){
     var result = 0;
     var resultArray = [];
     var indexes;
    for(var i = 0; i < array.length; i++){
      for(var j = 0; j < array.length; j++){
         indexes = array[j];
         if(indexes === array[j]){
            continue;
         }
      }
    }
    return result;
    }
</code>```

答:
Here is my version of this. Not much different from other answers, but I believe the documentation and the naming of the variables goes a long way into the readability of the code.

function productExceptSelf(numbers) {

  function productOfPreviousItems(numbers) {
    let products = [];
    let productOfPreviousItems = 1;

    // Loop over the array and store the productOfPreviousItems,
    // If it is the first item store 1
    for (let i = 0; i < numbers.length; i++) {
      products[i] = productOfPreviousItems;
      productOfPreviousItems *= numbers[i];
    }
    return products;
  }

  function productOfFollowingItems(numbers, products = numbers) {
    let productOfFollowingItems = 1;

    // Loop over the array backwards and store the productOfFollowingItems,
    // If it is the last item store 1
    for (let i = numbers.length - 1; i >= 0; i--) {
      products[i] *= productOfFollowingItems;
      productOfFollowingItems *= numbers[i];
    }
    return products;
  }

  // Multiply each item by all of the preceding items not including itself
  let products = productOfPreviousItems(numbers);

  // Multiply each item by all of the following items not including itself
  // Because we are using the result of productOfPreviousItems as an
  // optional parameter, productOfFollowingItems will combine
  // productOfPreviousItems and productOfFollowingItems
  // i.e. the products of of all items not including itself
  products = productOfFollowingItems(numbers, products);

  return products;
}

console.log(productExceptSelf([1, 2, 3, 4]))
console.log(productExceptSelf([2, 3, 4, 5]))
console.log(productExceptSelf([-1, 1, 0, -3, 3]))```

Title: Product of Array Except Self Javascript

Body:
I have been trying implement product of Array Except Self. My initial thought was to use indexes === array[j] and continue, but it is not working. Can anyone explain why?

 var array = [1,2,3,4];
    function productOfArrayExceptSelf(array){
     var result = 0;
     var resultArray = [];
     var indexes;
    for(var i = 0; i < array.length; i++){
      for(var j = 0; j < array.length; j++){
         indexes = array[j];
         if(indexes === array[j]){
            continue;
         }
      }
    }
    return result;
    }


问:
How to select random option from dropdown in cypress
I am trying to choose a random option from a dropdown list.
No answers i found so far helped.
I have no issues selecting a specific option, since all values have IDs set.

This is simple/straightforward and its working:

```<code>const log = new loginPage()
            log.visit()
            log.loginEmail('test')
            log.loginPassword('test')
            log.submit()
            cy.get('#link-new-application').click()
            cy.get('#control-nationality').click()
            cy.get('#option-nationality-DZA').click()
</code>```

Any advice for random selection? I am a beginner so any help would be much appreciated.

This is HTML:

答:
One approach is to get all your options then click one randomly

cy.get('#control-nationality').click()  // open the dropdown

cy.get('id^="option-nationality-"]')    // all elements with id starting "option-nationality"
  .then($options => {
    const count = $options.length
    const randomIndex = Math.floor(Math.random() * count)
    const randomOption = $options.eq(randomIndex)
    cy.wrap(randomOption).click()
  })
```

Body:
I am trying to choose a random option from a dropdown list.
No answers i found so far helped.
I have no issues selecting a specific option, since all values have IDs set.

This is simple/straightforward and its working:

const log = new loginPage()
            log.visit()
            log.loginEmail('test')
            log.loginPassword('test')
            log.submit()
            cy.get('#link-new-application').click()
            cy.get('#control-nationality').click()
            cy.get('#option-nationality-DZA').click()

Any advice for random selection? I am a beginner so any help would be much appreciated.

This is HTML:


问:
Installing geopandas on Apple M1 chip
How do you get geopandas to work in python with the new Apple M1 chip? Using pip does not work. I tried a lot of things and I think I came up with a work around that might be useful to others.

答:
I spent the better part of today figuring out how to get geopandas to work on a new Mac with the M1 chip. According to this closed issue, you have to use conda (miniconda3 FOR the M1 chip) to install geopandas on Apple's new M1 chip. I found that to be true, but incomplete. After installing miniconda3 and running

conda install -c conda-forge geopandas pygeos

Which will take you down to Python 3.8, I was still getting an error.

Going into my .../miniconda3/lib, I found that there was a libffi.8.dylib file, but not a libffi.7.dylib file, so I just copied it cp libffi.8.dylib libffi.7.dylib and now python can import geopandas.

Hopefully that helps someone out there, or tell me why this workaround is not needed...

Title: Installing geopandas on Apple M1 chip


问:
mpv doesn't recognise ytdl_path as a key
I'm trying to use yt-dlp rather than youtube-dl due to the bottlenecking on download speeds in youtube-dl but I can't get it to work.

My mpv.conf file looks like:

script-opts=ytdl_hook-ytdl_path=/usr/local/bin/yt-dlp

When trying to get mpv to run I get this warning:

[ytdl_hook] script-opts: unknown key ytdl_path, ignoring

Does anyone know what the problem is? I've read through the mpv docs and it says this should work.

mpv is version 0.27.2

答:
So this seems to be a version issue. You could try to get the newest mpv version running (make sure you actually have lua support with it). That however can be a real pain.
A quicker solution is to just get rid of your old youtube-dl version and make a symbolic link called youtube-dl to your yt-dlp binary.</p>


问:
Resize a Rectangle which is on an angle
I have a Rectangle which is an array of 4 Point structures.  It can be rotated in place on any angle (0 to 360 degrees) and will draw properly.

The user can also drag a corner to resize the rectangle.  For example, if they move the bottom-left point, it will also update the X coordinate of the upper-left point, and the Y coordinate of the lower-right point.  In this way, it will always be a rectangle no matter which point they move.

```<code>Points[point] = newValue;

switch (point)
{
    case TopLeft:
        Points[BottomLeft].X = newValue.X;
        Points[TopRight].Y = newValue.Y;
        break;

    case BottomRight:
        Points[TopRight].X = newValue.X;
        Points[BottomLeft].Y = newValue.Y;
        break;

    case BottomLeft:
        Points[TopLeft].X = newValue.X;
        Points[BottomRight].Y = newValue.Y;
        break;

    case TopRight:
        Points[BottomRight].X = newValue.X;
        Points[TopLeft].Y = newValue.Y;
        break;
}
</code>```

Here I change any of the four points to the given input point (newValue), and then modify the linked points so that it stays a Rectangle shape.

However, I need to modify the above code to work if my rectangle is on an angle like this:

Sample code added here:

http://www.assembla.com/code/moozhe-testing/subversion/nodes/rotateRectangle

答:
I used a solution that calculates the intersection between perpendicular lines:

Given a rotated rectangle (NOT axis aligned) with points, A, B, C, and D, and a dragged point D1 (which is the new point dragged from D), the goal is to find the new points for C1 and A1. B is opposite of D and will not move.

To find C1 and A1, find the intersection of the lines with points D1, which intersect with AB and BC and that are perpendicular to lines AB and BC (right angle). Those intersections will give you the points C1 and A1. Since the lines are perpendicular to AB and BC, they will form the new rectangle. Use the linear equations to calculate the slopes, intercepts and reciprocal slopes, which will give you both lines needed to calculate the intersection between AB and D1A. Repeat for the other side to get the intersection between BC and D1C (Remember, for axis-aligned rectangles, the below is not necessary and much easier)

Solution:
Find the slopes, opposite reciprocal slopes, and y-intercepts of the lines AB and AC. And find the y-intercepts of the lines D1A and D1C. Then calculate the intersection to get x and plug x into the lienar equation of one of the lines to get y:

```<code>slope_AB = (B.y - A.y) / (B.x - A.x)
y_intercept_AB = B.y - slope_AB * B.x
reciprocal_slope_AB = -1 / slope_AB
y_intercept_AD1 = D1.y - reciprocal_slope_AB * D1.x
A1x = (y_intercept_AB - y_intercept_AD1) / (reciprocal_slope_AB - slope_AB);
A1y = (slope_AB * B1x) + y_intercept_AB;
</code>```

Repeat above 6 calculations accordingly to get C1x and C1y

Title: Resize a Rectangle which is on an angle

Body:
I have a Rectangle which is an array of 4 Point structures.  It can be rotated in place on any angle (0 to 360 degrees) and will draw properly.

The user can also drag a corner to resize the rectangle.  For example, if they move the bottom-left point, it will also update the X coordinate of the upper-left point, and the Y coordinate of the lower-right point.  In this way, it will always be a rectangle no matter which point they move.

Points[point] = newValue;

switch (point)
{
    case TopLeft:
        Points[BottomLeft].X = newValue.X;
        Points[TopRight].Y = newValue.Y;
        break;

    case BottomRight:
        Points[TopRight].X = newValue.X;
        Points[BottomLeft].Y = newValue.Y;
        break;

    case BottomLeft:
        Points[TopLeft].X = newValue.X;
        Points[BottomRight].Y = newValue.Y;
        break;

    case TopRight:
        Points[BottomRight].X = newValue.X;
        Points[TopLeft].Y = newValue.Y;
        break;
}

Here I change any of the four points to the given input point (newValue), and then modify the linked points so that it stays a Rectangle shape.

However, I need to modify the above code to work if my rectangle is on an angle like this:

Sample code added here:

http://www.assembla.com/code/moozhe-testing/subversion/nodes/rotateRectangle

Body:
I used a solution that calculates the intersection between perpendicular lines:

Given a rotated rectangle (NOT axis aligned) with points, A, B, C, and D, and a dragged point D1 (which is the new point dragged from D), the goal is to find the new points for C1 and A1. B is opposite of D and will not move.

To find C1 and A1, find the intersection of the lines with points D1, which intersect with AB and BC and that are perpendicular to lines AB and BC (right angle). Those intersections will give you the points C1 and A1. Since the lines are perpendicular to AB and BC, they will form the new rectangle. Use the linear equations to calculate the slopes, intercepts and reciprocal slopes, which will give you both lines needed to calculate the intersection between AB and D1A. Repeat for the other side to get the intersection between BC and D1C (Remember, for axis-aligned rectangles, the below is not necessary and much easier)

Solution:
Find the slopes, opposite reciprocal slopes, and y-intercepts of the lines AB and AC. And find the y-intercepts of the lines D1A and D1C. Then calculate the intersection to get x and plug x into the lienar equation of one of the lines to get y:

slope_AB = (B.y - A.y) / (B.x - A.x)
y_intercept_AB = B.y - slope_AB * B.x
reciprocal_slope_AB = -1 / slope_AB
y_intercept_AD1 = D1.y - reciprocal_slope_AB * D1.x
A1x = (y_intercept_AB - y_intercept_AD1) / (reciprocal_slope_AB - slope_AB);
A1y = (slope_AB * B1x) + y_intercept_AB;

Repeat above 6 calculations accordingly to get C1x and C1y


问:
Maven clean compile does not work
I have maven-compiler-plugin in my pom file as follows but compiler get angry with generic parameters. my maven version is "Apache Maven 3.0.3 (r1075438; 2011-02-28 19:31:09+0200)"

```<code><plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <compilerVersion>1.6</compilerVersion>
    </configuration>
</plugin>
</code>```

and the class which compiler get angry is as follows

```<code>List<EulTransactionHistory> list = hibernateTemplate.findByCriteria(criteria, -1, 1);
EulTransactionHistory existingRecord = DBUtils.oneAndOnlyOne(list);
</code>```

and the spec for DBUtils.oneAndOnlyOne is as follows

```<code>public static <T> T oneAndOnlyOne(List<T> list) {...}
</code>```

finally the exception is

```<code>Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project etlscheduler: Compilation failure: Compilation failure SchedulerDaoImpl.java:[329,18] incompatible types
found   : java.lang.Object
required: com.aric.etlscheduler.domain.EulTransactionHistory
</code>```

Moreover when I compile this project with eclipse it compiles sucessfully.

答:
If line 329 is

```<code>EulTransactionHistory existingRecord = DBUtils.oneAndOnlyOne(list);
</code>```

then it's because 1) your Maven classpath's first DBUtils class has a oneAndOnlyOne() method that returns java.lang.Object, or 2) you're not passing it the list you think you're passing it, but an unparameterized List or a List.

Title: Maven clean compile does not work

Body:
I have maven-compiler-plugin in my pom file as follows but compiler get angry with generic parameters. my maven version is "Apache Maven 3.0.3 (r1075438; 2011-02-28 19:31:09+0200)"

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <compilerVersion>1.6</compilerVersion>
    </configuration>
</plugin>

and the class which compiler get angry is as follows

List<EulTransactionHistory> list = hibernateTemplate.findByCriteria(criteria, -1, 1);
EulTransactionHistory existingRecord = DBUtils.oneAndOnlyOne(list);

and the spec for DBUtils.oneAndOnlyOne is as follows

public static <T> T oneAndOnlyOne(List<T> list) {...}

finally the exception is

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project etlscheduler: Compilation failure: Compilation failure SchedulerDaoImpl.java:[329,18] incompatible types
found   : java.lang.Object
required: com.aric.etlscheduler.domain.EulTransactionHistory

Moreover when I compile this project with eclipse it compiles sucessfully.

Body:
If line 329 is

EulTransactionHistory existingRecord = DBUtils.oneAndOnlyOne(list);

then it's because 1) your Maven classpath's first DBUtils class has a oneAndOnlyOne() method that returns java.lang.Object, or 2) you're not passing it the list you think you're passing it, but an unparameterized List or a List.


问:
SQL Before Delete IF statement
Ok so I'm writing a trigger in sql and what needs to happen is that if t is deleted and f.actDepartDateTime is not null then the table is backed up and then deleted but I can't seem to get it to work?</p>
```<code>CREATE TABLE BACKUP_table (
      f_id         number,
      t_number      number,
      s_number number
);

CREATE OR REPLACE TRIGGER t_delete
    BEFORE DELETE ON t IF f.actDepartDateTime Not Null
    FOR EACH ROW
BEGIN

   -- insert a row into the backup table

   INSERT INTO BACKUP_Table (f_id, t_number, s_number)
   VALUES(old.fid, old.tnum, old.sNum);
END;
</code>```

答:
If you want a trigger to fire conditionally, the keyword is when not if.

The syntax for doing a null comparison is is null or is not null.

In the trigger body, you need to prefix references to the :old pseudo-record with a colon

I am not sure what f.actDepartureDateTime is supposed to reference-- there is no f alias anywhere in the code you posted.  I'm guessing that actDepartureDateTime is a column in the table t and that you want to fire the trigger based on the old value of that column.

Assuming that is what you are looking for, this should work

```<code>CREATE OR REPLACE TRIGGER t_delete
    BEFORE DELETE ON t
    FOR EACH ROW
    WHEN( old.actDepartDateTime IS Not Null )
BEGIN
  INSERT INTO BACKUP_Table (f_id, t_number, s_number)
    VALUES(:old.fid, :old.tnum, :old.sNum);
END;
</code>```

Title: SQL Before Delete IF statement

Body:
Ok so I'm writing a trigger in sql and what needs to happen is that if t is deleted and f.actDepartDateTime is not null then the table is backed up and then deleted but I can't seem to get it to work?

CREATE TABLE BACKUP_table (
      f_id         number,
      t_number      number,
      s_number number
);

CREATE OR REPLACE TRIGGER t_delete
    BEFORE DELETE ON t IF f.actDepartDateTime Not Null
    FOR EACH ROW
BEGIN

   -- insert a row into the backup table

   INSERT INTO BACKUP_Table (f_id, t_number, s_number)
   VALUES(old.fid, old.tnum, old.sNum);
END;

Body:
If you want a trigger to fire conditionally, the keyword is when not if.

The syntax for doing a null comparison is is null or is not null.

In the trigger body, you need to prefix references to the :old pseudo-record with a colon

I am not sure what f.actDepartureDateTime is supposed to reference-- there is no f alias anywhere in the code you posted.  I'm guessing that actDepartureDateTime is a column in the table t and that you want to fire the trigger based on the old value of that column.

Assuming that is what you are looking for, this should work

CREATE OR REPLACE TRIGGER t_delete
    BEFORE DELETE ON t
    FOR EACH ROW
    WHEN( old.actDepartDateTime IS Not Null )
BEGIN
  INSERT INTO BACKUP_Table (f_id, t_number, s_number)
    VALUES(:old.fid, :old.tnum, :old.sNum);
END;


问:
G Hub LUA script for Scroll Speed
I have Logitech G604 mouse
I want to Scroll with G6 and G5 button with different Scroll Speed is it possible LUA script. if yes then how I search in G-series Lua API but haven't find anything related.
Please help.
Thanks</p>

答:
Yes, you can invoke MoveMouseWheel(120) and MoveMouseWheel(-120) to simulate scroll in both directions.

Try this:

```<code>function OnEvent(event, arg)
   if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
      repeat
         MoveMouseWheel(120)
         Sleep(50)
      until not IsMouseButtonPressed(5)
   end
end
</code>```

Mouse button 5 acts as Scroll Up.

Title: G Hub LUA script for Scroll Speed

Body:
Yes, you can invoke MoveMouseWheel(120) and MoveMouseWheel(-120) to simulate scroll in both directions.

Try this:

function OnEvent(event, arg)
   if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
      repeat
         MoveMouseWheel(120)
         Sleep(50)
      until not IsMouseButtonPressed(5)
   end
end

Mouse button 5 acts as Scroll Up.


问:
Vue 3: component `:is` in for loop fails
I'm trying to loop over a list of component described by strings (I get the name of the component from another , like const componentTreeName = ["CompA", "CompA"].

My code is a simple as:

<template>

Demo

  <template v-for="compName in componentTreeName">
    <component :is="compName"></component>
  </template>
</template>
```

Demo here

EDIT

I tried this with not much success.

答:
Use resolveComponent() on the component name to look up the global component by name:

<template>
  <component :is="myGlobalComp" />
<template>
```
<p>demo 1</p>
<p>If you have a mix of locally and globally registered components, you can use a lookup for local components, and fall back to <code>resolveComponent()</code> for globals:</p>
<pre class="lang-html prettyprint-override"><code><script setup>
import LocalComponentA from '@/components/LocalComponentA.vue'
import LocalComponentB from '@/components/LocalComponentB.vue'
import { resolveComponent, markRaw } from 'vue'

const localComponents = {
  LocalComponentA,
  LocalComponentB,
}

const lookupComponent = name => {
  const c = localComponents[name] ?? resolveComponent(name)
  return markRaw(c)
}

const componentList = [
  'GlobalComponentA',
  'GlobalComponentB',
  'LocalComponentA',
  'LocalComponentB',
].map(lookupComponent)
</script>

<template>
  <component :is="c" v-for="c in componentList" />
</template>
</code>```

demo 2

Note: markRaw is used on the component definition because no reactivity is needed on it.

Body:
I'm trying to loop over a list of component described by strings (I get the name of the component from another , like const componentTreeName = ["CompA", "CompA"].

My code is a simple as:

<template>

Demo

  <template v-for="compName in componentTreeName">
    <component :is="compName"></component>
  </template>
</template>

<a href="https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cblx0aW1wb3J0IENvbXBBIGZyb20gJy4vQ29tcEEudnVlJ1xuXHRpbXBvcnQgeyByZWYgfSBmcm9tICd2dWUnXG5cbiAgLy8gSSBkbyBOT1Qgd2FudCB0byB1c2UgW0NvbXBBLCBDb21wQV0gYmVjYXVzZSBteSBpbnB1dHMgYXJlIHN0cmluZ3Ncblx0Y29uc3QgY29tcG9uZW50VHJlZU5hbWUgPSBbXCJDb21wQVwiLCBcIkNvbXBBXCJdXG48L3NjcmlwdD5cblxuPHRlbXBsYXRlPlxuICA8aDE+RGVtbzwvaDE+XG4gIDx0ZW1wbGF0ZSB2LWZvcj1cImNvbXBOYW1lIGluIGNvbXBvbmVudFRyZWVOYW1lXCI+XG4gICAgPGNvbXBvbmVudCA6aXM9XCJjb21wTmFtZVwiPjwvY29tcG9uZW50PlxuICA8L3RlbXBsYXRlPlxuPC90ZW1wbGF0ZT4iLCJpbXBvcnQtbWFwLmpzb24iOiJ7XG4gIFwiaW1wb3J0c1wiOiB7XG4gICAgXCJ2dWVcIjogXCJodHRwczovL3NmYy52dWVqcy5vcmcvdnVlLnJ1bnRpbWUuZXNtLWJyb3dzZXIuanNcIlxuICB9XG59IiwiQ29tcEEudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlZiB9IGZyb20gJ3Z1ZSdcblxuY29uc3QgbXNnID0gcmVmKCdQdXQgdGl0bGUgc2xpZGUgaGVyZScpXG48L3NjcmlwdD5cblxuPHRlbXBsYXRlPlxuICA8aDI+e3sgbXNnIH19PC9oMj5cbiAgPGlucHV0IHYtbW9kZWw9XCJtc2dcIj5cbjwvdGVtcGxhdGU+IiwiVGFibGUudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlZiB9IGZyb20gJ3Z1ZSdcbmluamVjdChcIm1haW5EYXRhXCIpXG5jb25zdCBtc2cgPSByZWYoJ0hlbGxvIFdvcmxkIScpXG48L3NjcmlwdD5cblxuPHRlbXBsYXRlPlxuICA8aDI+XG4gICAgVGFibGUgZGVzIG1hdGnDqHJlc1xuICA8L2gyPlxuICBcbjwvdGVtcGxhdGU+In0=" rel="nofollow noreferrer">Demo here

EDIT

I tried <a href="https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cblx0aW1wb3J0IENvbXBBIGZyb20gJy4vQ29tcEEudnVlJ1xuXHRpbXBvcnQgeyByZWYsIGdldEN1cnJlbnRJbnN0YW5jZSB9IGZyb20gJ3Z1ZSdcblxuICBjb25zdCBjb21wb25lbnRzID0gZ2V0Q3VycmVudEluc3RhbmNlKCkuYXBwQ29udGV4dC5jb21wb25lbnRzO1xuICAvLyBJIGRvIE5PVCB3YW50IHRvIHVzZSBbQ29tcEEsIENvbXBBXSBiZWNhdXNlIG15IGlucHV0cyBhcmUgc3RyaW5nc1xuXHRjb25zdCBjb21wb25lbnRUcmVlTmFtZSA9IFtcIkNvbXBBXCIsIFwiQ29tcEFcIl1cbjwvc2NyaXB0PlxuXG48dGVtcGxhdGU+XG4gIDxoMT5EZW1vPC9oMT5cbiAgPHRlbXBsYXRlIHYtZm9yPVwiY29tcE5hbWUgaW4gY29tcG9uZW50VHJlZU5hbWVcIj5cbiAgICA8Y29tcG9uZW50IDppcz1cImNvbXBvbmVudHNbY29tcE5hbWVdXCI+PC9jb21wb25lbnQ+XG4gIDwvdGVtcGxhdGU+XG48L3RlbXBsYXRlPiIsImltcG9ydC1tYXAuanNvbiI6IntcbiAgXCJpbXBvcnRzXCI6IHtcbiAgICBcInZ1ZVwiOiBcImh0dHBzOi8vc2ZjLnZ1ZWpzLm9yZy92dWUucnVudGltZS5lc20tYnJvd3Nlci5qc1wiXG4gIH1cbn0iLCJDb21wQS52dWUiOiI8c2NyaXB0IHNldHVwPlxuaW1wb3J0IHsgcmVmIH0gZnJvbSAndnVlJ1xuXG5jb25zdCBtc2cgPSByZWYoJ1B1dCB0aXRsZSBzbGlkZSBoZXJlJylcbjwvc2NyaXB0PlxuXG48dGVtcGxhdGU+XG4gIDxoMj57eyBtc2cgfX08L2gyPlxuICA8aW5wdXQgdi1tb2RlbD1cIm1zZ1wiPlxuPC90ZW1wbGF0ZT4iLCJUYWJsZS52dWUiOiI8c2NyaXB0IHNldHVwPlxuaW1wb3J0IHsgcmVmIH0gZnJvbSAndnVlJ1xuaW5qZWN0KFwibWFpbkRhdGFcIilcbmNvbnN0IG1zZyA9IHJlZignSGVsbG8gV29ybGQhJylcbjwvc2NyaXB0PlxuXG48dGVtcGxhdGU+XG4gIDxoMj5cbiAgICBUYWJsZSBkZXMgbWF0acOocmVzXG4gIDwvaDI+XG4gIFxuPC90ZW1wbGF0ZT4ifQ==" rel="nofollow noreferrer">this with not much success.

Body:
Use resolveComponent() on the component name to look up the global component by name:

<script setup>
import { resolveComponent, markRaw } from 'vue'
const myGlobalComp = markRaw(resolveComponent('my-global-component'))
</script>

<template>
  <component :is="myGlobalComp" />
<template>

demo 1

If you have a mix of locally and globally registered components, you can use a lookup for local components, and fall back to resolveComponent() for globals:

<script setup>
import LocalComponentA from '@/components/LocalComponentA.vue'
import LocalComponentB from '@/components/LocalComponentB.vue'
import { resolveComponent, markRaw } from 'vue'

const localComponents = {
  LocalComponentA,
  LocalComponentB,
}

const lookupComponent = name => {
  const c = localComponents[name] ?? resolveComponent(name)
  return markRaw(c)
}

const componentList = [
  'GlobalComponentA',
  'GlobalComponentB',
  'LocalComponentA',
  'LocalComponentB',
].map(lookupComponent)
</script>

<template>
  <component :is="c" v-for="c in componentList" />
</template>

demo 2

Note: markRaw is used on the component definition because no reactivity is needed on it.


问:
converting a key value text file into a CSV file
I have a text file that needs to be converted into CSV file using pandas. A piece of it is presented in the following:

```<code>time 00:15 min
    cod,10,1=0,2=2,3=2,4=1,5=6,6=4,7=2,8=7,9=1,10=9,11=7
    cod,18,1=27,2=18,3=19,4=20,5=47,6=2,7=2,8=0,9=33,10=61,11=13,12=2,13=3,14=0,15=0
</code>```

Rows are cod,10, and cod,18 and the columns are 1, 2, 3,..., 15.
Any idea?
Regards,
Ali

答:
You can use Python standard CSV module and its DictWriter class to handle the variability in the column names.

I like to split up multi-step tasks like these to make sure each step is progressing correctly.  Here's the full code, a task-by-task description follows:

import csv

# Split text by lines, then each line by comma
cod_num_pairs = []
with open('input.txt') as f:
    next(f)  # discard first "time" line

    for line in f:
        # *pairs will hold all the pairs of 'Col_name=Val'
        cod, num, *pairs = [x.strip() for x in line.split(',')]
        cod_num_pairs.append([cod, num, pairs])

# Build headers and rows
headers = {'id': None}
all_rows = []
for cod, num, pairs in cod_num_pairs:
    id_ = cod + ' ' + num
    row = {'id': id_}

    for pair in pairs:
        col_name, val = [x.strip() for x in pair.split('=')]

        row[col_name] = val
        headers[col_name] = None

    all_rows.append(row)

# Write to CSV
with open('output.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=headers)
    writer.writeheader()
    writer.writerows(all_rows)
```
<p>When I run that, I get:</p>
<pre class="lang-none prettyprint-override"><code>id,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
cod 10,0,2,2,1,6,4,2,7,1,9,7,,,,
cod 18,27,18,19,20,47,2,2,0,33,61,13,2,3,0,0
</code>```

| id     | 1  | 2  | 3  | 4  | 5  | 6 | 7 | 8 | 9  | 10 | 11 | 12 | 13 | 14 | 15 |
|--------|----|----|----|----|----|---|---|---|----|----|----|----|----|----|----|
| cod 10 | 0  | 2  | 2  | 1  | 6  | 4 | 2 | 7 | 1  | 9  | 7  |    |    |    |    |
| cod 18 | 27 | 18 | 19 | 20 | 47 | 2 | 2 | 0 | 33 | 61 | 13 | 2  | 3  | 0  | 0  |
```
<p>My first step is to deal with the lines from the text file: getting rid of the first line, and splitting the data lines up by comma (<code>,</code>).   When that's done, <code>cod_num_pairs</code> looks like this:</p>
<pre class="lang-py prettyprint-override"><code>print('Cod, Num, and Pairs of `Col_name=Val`')
pprint.pprint(cod_num_pairs, width=130)
</code>```

Cod, Num, and Pairs of `Col_name=Val`
[
['cod', '10', ['1=0', '2=2', '3=2', '4=1', '5=6', '6=4', '7=2', '8=7', '9=1', '10=9', '11=7']],
['cod', '18', ['1=27', '2=18', '3=19', '4=20', '5=47', '6=2', '7=2', '8=0', '9=33', '10=61', '11=13', '12=2', '13=3', '14=0', '15=0']]
]
```
<p>Then I move on to splitting up the <code>Col_name=Val</code> pairs, and creating a list of dictionaries.  Each dictionary is a row that will be turned into a CSV row in the final step.  I also create a dict for the headers and make sure every <code>Col_name</code> is added to it.  When that's done:</p>
<pre class="lang-py prettyprint-override"><code>print('Headers:')
pprint.pprint(headers, width=200, sort_dicts=False)
print('Rows:')
pprint.pprint(all_rows, width=200, sort_dicts=False)
</code>```

Headers:
{'id': None, '1': None, '2': None, '3': None, '4': None, '5': None, '6': None, '7': None, '8': None, '9': None, '10': None, '11': None, '12': None, '13': None, '14': None, '15': None}
Rows:
[{'id': 'cod 10', '1': '0', '2': '2', '3': '2', '4': '1', '5': '6', '6': '4', '7': '2', '8': '7', '9': '1', '10': '9', '11': '7'},
 {'id': 'cod 18', '1': '27', '2': '18', '3': '19', '4': '20', '5': '47', '6': '2', '7': '2', '8': '0', '9': '33', '10': '61', '11': '13', '12': '2', '13': '3', '14': '0', '15': '0'}]
```

Finally, with a set of complete headers and a list of dict-rows, I just pass those to CSV DictWriter.

Body:
I have a text file that needs to be converted into CSV file using pandas. A piece of it is presented in the following:

time 00:15 min
    cod,10,1=0,2=2,3=2,4=1,5=6,6=4,7=2,8=7,9=1,10=9,11=7
    cod,18,1=27,2=18,3=19,4=20,5=47,6=2,7=2,8=0,9=33,10=61,11=13,12=2,13=3,14=0,15=0

Rows are cod,10, and cod,18 and the columns are 1, 2, 3,..., 15.
Any idea?
Regards,
Ali

Body:
You can use Python standard CSV module and its DictWriter class to handle the variability in the column names.

I like to split up multi-step tasks like these to make sure each step is progressing correctly.  Here's the full code, a task-by-task description follows:

import csv

# Split text by lines, then each line by comma
cod_num_pairs = []
with open('input.txt') as f:
    next(f)  # discard first "time" line

    for line in f:
        # *pairs will hold all the pairs of 'Col_name=Val'
        cod, num, *pairs = [x.strip() for x in line.split(',')]
        cod_num_pairs.append([cod, num, pairs])

# Build headers and rows
headers = {'id': None}
all_rows = []
for cod, num, pairs in cod_num_pairs:
    id_ = cod + ' ' + num
    row = {'id': id_}

    for pair in pairs:
        col_name, val = [x.strip() for x in pair.split('=')]

        row[col_name] = val
        headers[col_name] = None

    all_rows.append(row)

# Write to CSV
with open('output.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=headers)
    writer.writeheader()
    writer.writerows(all_rows)

When I run that, I get:

id,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
cod 10,0,2,2,1,6,4,2,7,1,9,7,,,,
cod 18,27,18,19,20,47,2,2,0,33,61,13,2,3,0,0

| id     | 1  | 2  | 3  | 4  | 5  | 6 | 7 | 8 | 9  | 10 | 11 | 12 | 13 | 14 | 15 |
|--------|----|----|----|----|----|---|---|---|----|----|----|----|----|----|----|
| cod 10 | 0  | 2  | 2  | 1  | 6  | 4 | 2 | 7 | 1  | 9  | 7  |    |    |    |    |
| cod 18 | 27 | 18 | 19 | 20 | 47 | 2 | 2 | 0 | 33 | 61 | 13 | 2  | 3  | 0  | 0  |

My first step is to deal with the lines from the text file: getting rid of the first line, and splitting the data lines up by comma (,).   When that's done, cod_num_pairs looks like this:

print('Cod, Num, and Pairs of `Col_name=Val`')
pprint.pprint(cod_num_pairs, width=130)

Cod, Num, and Pairs of `Col_name=Val`
[
['cod', '10', ['1=0', '2=2', '3=2', '4=1', '5=6', '6=4', '7=2', '8=7', '9=1', '10=9', '11=7']],
['cod', '18', ['1=27', '2=18', '3=19', '4=20', '5=47', '6=2', '7=2', '8=0', '9=33', '10=61', '11=13', '12=2', '13=3', '14=0', '15=0']]
]

Then I move on to splitting up the Col_name=Val pairs, and creating a list of dictionaries.  Each dictionary is a row that will be turned into a CSV row in the final step.  I also create a dict for the headers and make sure every Col_name is added to it.  When that's done:

print('Headers:')
pprint.pprint(headers, width=200, sort_dicts=False)
print('Rows:')
pprint.pprint(all_rows, width=200, sort_dicts=False)

Headers:
{'id': None, '1': None, '2': None, '3': None, '4': None, '5': None, '6': None, '7': None, '8': None, '9': None, '10': None, '11': None, '12': None, '13': None, '14': None, '15': None}
Rows:
[{'id': 'cod 10', '1': '0', '2': '2', '3': '2', '4': '1', '5': '6', '6': '4', '7': '2', '8': '7', '9': '1', '10': '9', '11': '7'},
 {'id': 'cod 18', '1': '27', '2': '18', '3': '19', '4': '20', '5': '47', '6': '2', '7': '2', '8': '0', '9': '33', '10': '61', '11': '13', '12': '2', '13': '3', '14': '0', '15': '0'}]

Finally, with a set of complete headers and a list of dict-rows, I just pass those to CSV DictWriter.


问:
Java Jar run as Daemon cant issue exec()
I have a java socket server I wrote to allow me to keep a web clusters code base in sync. When I run the init.d script from a shell login like so

```<code>[root@web11 www]# /etc/init.d/servermngr start
</code>```

Logout and all will work fine but if the server reboots or I run the init.d using services like so

```<code>[root@web11 www]# service servermngr start
</code>```

Any of the exec() commands passed to the socket server will not get executed on the linux box. I am assuming it has to do with the JVM having no real shell.  If I login and run

```<code>[root@web11 www]# /etc/init.d/servermngr start
</code>```

...and logout all runs nice all CVS commands are executed.

Another note when run as a service the socket server responds to status checks so it is running

Here is the init.d script

```<code>#!/bin/sh
# chkconfig: 2345 95 1
# description: Starts Daemon Using ServerManager.jar.
#
# Source function library.
. /etc/init.d/functions

start () {
    echo -n $"Starting ServerManager: "

    # start daemon
    cd /www/servermanager/
    daemon java -jar ServerManager.jar > /www/logs/ServerManager.log &
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/cups
    echo "";
    return $RETVAL
}

stop () {
    # stop daemon
    echo -n $"Stopping $prog: "
    kill `ps uax | grep -i "java -jar ServerManager.ja[r]" | head -n 1 | awk '{print $2}'`
    RETVAL=$?
    echo "";
    return $RETVAL
}

restart() {
    stop
    start
}

case $1 in
    start)
        start
    ;;
    stop)
        stop
    ;;
    *)

    echo $"Usage: servermngr {start|stop}"
    exit 3
esac

exit $RETVAL
</code>```

And the Java responsible for actually executing the code:

```<code>// Build cmd Array of Strings
            String[] cmd = {"/bin/sh", "-c", "cd /www;cvs up -d htdocs/;cvs up -d phpinclude/"};
            final Process process;
            try {
                process = Runtime.getRuntime().exec(cmd);

                BufferedReader buf = new BufferedReader(new InputStreamReader(
                        process.getInputStream()));

                // Since this is a CVS UP we return the Response to PHP
                if(input.matches(".*(cvs up).*")){
                    String line1;
                    out.println("cvsupdate-start");
                    System.out.println("CVS Update" + input);
                    while ((line1 = buf.readLine()) != null) {
                        out.println(line1);
                        System.out.println("CVS:" + line1);
                    }
                    out.println("cvsupdate-end");
                }

            } catch (IOException ex) {
                System.out.println("IOException on Run cmd " + CommandFactory.class.getName() + " " + ex);
                Logger.getLogger(CommandFactory.class.getName()).log(Level.SEVERE, null, ex);
            }
</code>```

Thx for any help

答:
Here is the startup script that fixed my issue if someone runs into an issue</p>

```<code>#!/bin/sh
# chkconfig: 2345 95 1
# description: Starts Daemon Using ServerManager.jar.
#
# Source function library.
. /etc/init.d/functions

RETVAL=0
prog="ServerManager"
servermanager="java"
serveroptions=" -jar ServerManager.jar"
pid_file="/var/run/servermanager.pid"

launch_daemon()
{
  /bin/sh << EOF
     java -Ddaemon.pidfile=$pid_file $serveroptions <&- &
     pid=\$!
     echo \${pid}
EOF
}

start () {
    echo -n $"Starting $prog: "
 if [ -e /var/lock/subsys/servermanager ]; then
                if [ -e /var/run/servermanager.pid ] && [ -e /proc/`cat /var/run/servermanager.pid` ]; then
                echo -n $"cannot start: servermanager is already running.";
                failure $"cannot start: servermanager already running.";
                echo
                return 1
                fi
        fi
    # start daemon
    cd /www/voodoo_servermanager/
    export CVSROOT=":pserver:cvsd@cvs.zzzzz.yyy:/cvsroot";
    daemon "$servermanager $serveroptions > /www/logs/ServerManager.log &"
    #daemon_pid=`launch_daemon`
    #daemon ${daemon_pid}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/servermanager && pidof $servermanager > $pid_file
    echo "";
    return $RETVAL
}

stop () {
    # stop daemon
    echo -n $"Stopping $prog: "
        if [ ! -e /var/lock/subsys/servermanager ]; then
            echo -n $"cannot stop ServerManager:  ServerManager is not running."
            failure $"cannot stop ServerManager:  ServerManager is not running."
        echo
        return 1;
        fi
        killproc $servermanager
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/servermanager;
        return $RETVAL
}

restart() {
    stop
    start
}

case $1 in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    *)

    echo $"Usage: servermngr {start|stop|restart}"
    RETVAL=1
esac

exit $RETVAL
</code>```

Body:
I have a java socket server I wrote to allow me to keep a web clusters code base in sync. When I run the init.d script from a shell login like so

[root@web11 www]# /etc/init.d/servermngr start

Logout and all will work fine but if the server reboots or I run the init.d using services like so

[root@web11 www]# service servermngr start

Any of the exec() commands passed to the socket server will not get executed on the linux box. I am assuming it has to do with the JVM having no real shell.  If I login and run

[root@web11 www]# /etc/init.d/servermngr start

...and logout all runs nice all CVS commands are executed.

Another note when run as a service the socket server responds to status checks so it is running

Here is the init.d script

#!/bin/sh
# chkconfig: 2345 95 1
# description: Starts Daemon Using ServerManager.jar.
#
# Source function library.
. /etc/init.d/functions

start () {
    echo -n $"Starting ServerManager: "

    # start daemon
    cd /www/servermanager/
    daemon java -jar ServerManager.jar > /www/logs/ServerManager.log &
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/cups
    echo "";
    return $RETVAL
}

stop () {
    # stop daemon
    echo -n $"Stopping $prog: "
    kill `ps uax | grep -i "java -jar ServerManager.ja[r]" | head -n 1 | awk '{print $2}'`
    RETVAL=$?
    echo "";
    return $RETVAL
}

restart() {
    stop
    start
}

case $1 in
    start)
        start
    ;;
    stop)
        stop
    ;;
    *)

    echo $"Usage: servermngr {start|stop}"
    exit 3
esac

exit $RETVAL

And the Java responsible for actually executing the code:

// Build cmd Array of Strings
            String[] cmd = {"/bin/sh", "-c", "cd /www;cvs up -d htdocs/;cvs up -d phpinclude/"};
            final Process process;
            try {
                process = Runtime.getRuntime().exec(cmd);

                BufferedReader buf = new BufferedReader(new InputStreamReader(
                        process.getInputStream()));

                // Since this is a CVS UP we return the Response to PHP
                if(input.matches(".*(cvs up).*")){
                    String line1;
                    out.println("cvsupdate-start");
                    System.out.println("CVS Update" + input);
                    while ((line1 = buf.readLine()) != null) {
                        out.println(line1);
                        System.out.println("CVS:" + line1);
                    }
                    out.println("cvsupdate-end");
                }

            } catch (IOException ex) {
                System.out.println("IOException on Run cmd " + CommandFactory.class.getName() + " " + ex);
                Logger.getLogger(CommandFactory.class.getName()).log(Level.SEVERE, null, ex);
            }

Thx for any help

Body:
Here is the startup script that fixed my issue if someone runs into an issue

#!/bin/sh
# chkconfig: 2345 95 1
# description: Starts Daemon Using ServerManager.jar.
#
# Source function library.
. /etc/init.d/functions

RETVAL=0
prog="ServerManager"
servermanager="java"
serveroptions=" -jar ServerManager.jar"
pid_file="/var/run/servermanager.pid"

launch_daemon()
{
  /bin/sh << EOF
     java -Ddaemon.pidfile=$pid_file $serveroptions <&- &
     pid=\$!
     echo \${pid}
EOF
}

start () {
    echo -n $"Starting $prog: "
 if [ -e /var/lock/subsys/servermanager ]; then
                if [ -e /var/run/servermanager.pid ] && [ -e /proc/`cat /var/run/servermanager.pid` ]; then
                echo -n $"cannot start: servermanager is already running.";
                failure $"cannot start: servermanager already running.";
                echo
                return 1
                fi
        fi
    # start daemon
    cd /www/voodoo_servermanager/
    export CVSROOT=":pserver:cvsd@cvs.zzzzz.yyy:/cvsroot";
    daemon "$servermanager $serveroptions > /www/logs/ServerManager.log &"
    #daemon_pid=`launch_daemon`
    #daemon ${daemon_pid}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/servermanager && pidof $servermanager > $pid_file
    echo "";
    return $RETVAL
}

stop () {
    # stop daemon
    echo -n $"Stopping $prog: "
        if [ ! -e /var/lock/subsys/servermanager ]; then
            echo -n $"cannot stop ServerManager:  ServerManager is not running."
            failure $"cannot stop ServerManager:  ServerManager is not running."
        echo
        return 1;
        fi
        killproc $servermanager
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/servermanager;
        return $RETVAL
}

restart() {
    stop
    start
}

case $1 in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    *)

    echo $"Usage: servermngr {start|stop|restart}"
    RETVAL=1
esac

exit $RETVAL


问:
Query on json / jsonb column super slow. Can I use an index?
I am trying to speed up the querying of some json data stored inside a PostgreSQL database. I inherited an application that queries a PostgreSQL table called data with a field called value where value is blob of json of type jsonb.

It is about 300 rows but takes 12 seconds to select this data from the 5 json elements. The json blobs are a bit large but the data I need is all in the top level of the json nesting if that helps.

I tried adding an index of CREATE INDEX idx_tbl_data ON data USING gin (value); but that didn't help.  Is there a different index I should be using?  The long term vision is to re-write the application to move that data out of the json but that is something is at least 30-40 man days of work due to complexity in other parts of the application so I am looking to see if I can make this faster in short term.

Not sure if it helps but the underlying data that makes up this result set doesn't change often. It's the data that is further down in the json blob that often changes.

```<code>SELECT
  value::json ->> 'name' AS name,
  value::json ->> 'mnemonic' AS mnemonic,
  value::json ->> 'urlName' AS "urlName",
  value::json ->> 'countryCode' AS "countryCode",
  value::json #>>'{team}' AS team
FROM
  data;
</code>```

答:
Like a_horse already advised (and you mentioned yourself), the proper fix is to extract those attributes to separate columns, normalizing your design to some extent.

Can an index help?

Sadly, no (as of Postgres 14).

It could work in theory. Since your values are big, an expression index with just some small attributes can be picked up by Postgres in an index-only scan, even when retrieving all rows (where it otherwise would ignore indexes).

The manual:

However, PostgreSQL's planner is currently not very smart about such cases. It considers a query to be potentially executable by index-only scan only when all columns needed by the query are available from the index.

So you would have to include value itself in the index, even if just as INCLUDE column - totally spoiling the whole idea. No go.

You probably can still do something in the short term. Two crucial quotes:

I am looking to see if I can make this faster in short term

The json blobs are a bit large

Data type

Drop the cast to json from the query. Casting every time adds pointless cost.

Compression

One major cost factor will be compression. Postgres has to "de-toast" the whole large column, just to extract some small attributes. Since Postgres 14, you can switch the compression algorithm (if support is enabled in your version!). The default is dictated by the config setting default_toast_compression, which is set to pglz by default. Currently the only available alternative is lz4. You can set that per column. Any time.

LZ4 (lz4) is considerably faster, while compressing typically a bit less. About twice as fast, but around 10 % more storage (depends!). If performance is not an issue it's best to stick to the stronger compression of the default LZ algorithm (pglz). There may be more compression algorithms to pick from in the future.

To implement:

ALTER TABLE data
  ALTER COLUMN value SET COMPRESSION lz4;
```
<p>Setting a new <code>COMPRESSION</code> for a column does not re-compress automatically. Postgres remembers the compression method and only re-compresses if it's forced to un-compress anyway. You may want to force re-compression of existing values. You can check with:</p>
```SELECT pg_column_compression(value) FROM data LIMIT 10;
```
<p>Related blog post:</p>
<ul>
<li>https://www.postgresql.fastware.com/blog/what-is-the-new-lz4-toast-compression-in-postgresql-14</li>
</ul>
<h4><code>GENERATED</code> columns</h4>
<p>While stuck with the broken design you might just add some (small!) generated columns to cover your query:</p>
```ALTER TABLE data
  ADD COLUMN name text GENERATED ALWAYS AS (value::json ->> 'name') STORED
, ADD COLUMN mnemonic text GENERATED ALWAYS AS (value::json ->> 'mnemonic') STORED
...
```
<p>And then target only those generated columns, <strong>not involving the big <code>value</code></strong> at all.</p>
```SELECT name, mnemonic, ... FROM data;
```

That would bypass the main performance issue.

See:

Computed / calculated / virtual / derived columns in PostgreSQL

However, you mentioned:

It's the data that is further down in the json blob that often changes.

Every change to value forces a re-check on the generated columns, so that adds write cost.

Body:
I am trying to speed up the querying of some json data stored inside a PostgreSQL database. I inherited an application that queries a PostgreSQL table called data with a field called value where value is blob of json of type jsonb.

It is about 300 rows but takes 12 seconds to select this data from the 5 json elements. The json blobs are a bit large but the data I need is all in the top level of the json nesting if that helps.

I tried adding an index of CREATE INDEX idx_tbl_data ON data USING gin (value); but that didn't help.  Is there a different index I should be using?  The long term vision is to re-write the application to move that data out of the json but that is something is at least 30-40 man days of work due to complexity in other parts of the application so I am looking to see if I can make this faster in short term.

Not sure if it helps but the underlying data that makes up this result set doesn't change often. It's the data that is further down in the json blob that often changes.

SELECT
  value::json ->> 'name' AS name,
  value::json ->> 'mnemonic' AS mnemonic,
  value::json ->> 'urlName' AS "urlName",
  value::json ->> 'countryCode' AS "countryCode",
  value::json #>>'{team}' AS team
FROM
  data;

Body:
Like a_horse already advised (and you mentioned yourself), the proper fix is to extract those attributes to separate columns, normalizing your design to some extent.

Can an index help?

Sadly, no (as of Postgres 14).

It could work in theory. Since your values are big, an expression index with just some small attributes can be picked up by Postgres in an index-only scan, even when retrieving all rows (where it otherwise would ignore indexes).

The manual:

However, PostgreSQL's planner is currently not very smart about such cases. It considers a query to be potentially executable by index-only scan only when all columns needed by the query are available from the index.

So you would have to include value itself in the index, even if just as INCLUDE column - totally spoiling the whole idea. No go.

You probably can still do something in the short term. Two crucial quotes:

I am looking to see if I can make this faster in short term

The json blobs are a bit large

Data type

Drop the cast to json from the query. Casting every time adds pointless cost.

Compression

One major cost factor will be compression. Postgres has to "de-toast" the whole large column, just to extract some small attributes. Since Postgres 14, you can switch the compression algorithm (if support is enabled in your version!). The default is dictated by the config setting default_toast_compression, which is set to pglz by default. Currently the only available alternative is lz4. You can set that per column. Any time.

LZ4 (lz4) is considerably faster, while compressing typically a bit less. About twice as fast, but around 10 % more storage (depends!). If performance is not an issue it's best to stick to the stronger compression of the default LZ algorithm (pglz). There may be more compression algorithms to pick from in the future.

To implement:

ALTER TABLE data
  ALTER COLUMN value SET COMPRESSION lz4;

Setting a new COMPRESSION for a column does not re-compress automatically. Postgres remembers the compression method and only re-compresses if it's forced to un-compress anyway. You may want to force re-compression of existing values. You can check with:

SELECT pg_column_compression(value) FROM data LIMIT 10;

Related blog post:

https://www.postgresql.fastware.com/blog/what-is-the-new-lz4-toast-compression-in-postgresql-14

GENERATED columns

While stuck with the broken design you might just add some (small!) generated columns to cover your query:

ALTER TABLE data
  ADD COLUMN name text GENERATED ALWAYS AS (value::json ->> 'name') STORED
, ADD COLUMN mnemonic text GENERATED ALWAYS AS (value::json ->> 'mnemonic') STORED
...

And then target only those generated columns, not involving the big value at all.

SELECT name, mnemonic, ... FROM data;

That would bypass the main performance issue.

See:

Computed / calculated / virtual / derived columns in PostgreSQL

However, you mentioned:

It's the data that is further down in the json blob that often changes.

Every change to value forces a re-check on the generated columns, so that adds write cost.