突突唧之家

我的疑问 & 我的解决方案

You can take advantage of the complex type:

# build a complex array of your points
z = np.array([complex(p.x, p.y) for p in points])

First Solution

# mesh this array so that you will have all combinations
m, n = np.meshgrid(z, z)
# get the distance via the norm
distance_matrix = abs(m - n)

Second Solution

Meshing is the main idea. But numpy is clever, so you don't have to generate m & n. Just compute the difference using a transposed version of z. The mesh is done automatically:

distance_matrix = abs(z[..., np.newaxis] - z)

And if z is directly set as a 2-dimensional array, you can use z.T instead of the weird z[..., np.newaxis]. So finally, your code will look like this:

z = np.array([[complex(p.x, p.y) for p in points]])  # notice the [[ ... ]]
distance_matrix = abs(z.T - z)

Matroska (MKV)

This will stream copy (re-mux) all streams:

ffmpeg -i input.ts -map 0 -c copy output.mkv

The -map 0 option is used to include all streams. Otherwise it will use the default stream selection behavior which would only result in one stream per stream type being selected. Since Matroska can handle most arbitrary streams I included -map 0.

MP4

This will stream copy (re-mux) all streams:

ffmpeg -i input.ts -map 0 -c copy output.mp4

If your inputs formats are not compatible with MP4 you will get an error. Your player/device may not support all arbitrary, less common, or legacy formats even if they are supported by MP4. If in doubt re-encode to H.264 + AAC as shown below.

This will re-encode the video to H.264 and stream copy the audio:

ffmpeg -i input.ts -c:v libx264 -c:a copy output.mp4

The next example will re-encode both video and audio:

ffmpeg -i input.ts -c:v libx264 -c:a aac output.mp4

Lossless H.264 example:

ffmpeg -i input.ts -c:v libx264 -crf 0 -c:a copy output.mp4

Lossless files will be huge.

For example I have two dicts:

dict_a = {'a': 1, 'b': 2, 'c': 3}
dict_b = {'b': 3, 'c': 4, 'd': 5}

I need a pythonic way of 'combining' two dicts such that the result is:

{'a': 1, 'b': 5, 'c': 7, 'd': 5}

That is to say: if a key appears in both dicts, add their values, if it appears in only one dict, keep its value.

Use collections.Counter:

from collections import Counter
a = Counter({'a':1, 'b':2, 'c':3})
b = Counter({'b':3, 'c':4, 'd':5})
c = a + b # c = Counter({'c': 7, 'b': 5, 'd': 5, 'a': 1})

Counters are basically a subclass of dict, so you can still do everything else with them you'd normally do with that type, such as iterate over their keys and values.

I have a beamer template for a presentation. In this template, the headline shows the section name and the subsection to which the current slide belongs to. However, even if I have defined a short title as in

\section[Short title]{The very long title that surely is way too long}

the headline always shows the long title.

Here is the code from the style file which shows the section name

\pgftext[at=\pgfpoint{\beamer@headline@lmargin}{-0.55\beamer@headline@height},left,center]{
\begin{beamercolorbox}[dp=0.5ex]{section in head/foot}
\shadowtextline{\insertsection}
\end{beamercolorbox}
}

So my question: Is there any other command for \insertsection in order to show the short title?

The outer theme should use \insertsectionhead instead of \insertsection:

1
2
3
4
5
6
7
8
9
10
11
12
13
\documentclass{beamer}

\begin{document}

\section[Short section title]{Long section title}

\begin{frame}
Long title: \insertsection

Short title: \insertsectionhead
\end{frame}

\end{document}

What are the differences between \def and \newcommand?

\def is a TeX primitive, \newcommand is a LaTeX overlay on top of \def. The most obvious benefits of \newcommand over \def are:

  1. \newcommand checks whether or not the command already exists.
  2. \newcommand allows you to define an optional argument.

In general, anything that \newcommand does can be done by \def, but it usually involves a little trickery and unless you want something that \newcommand can't do, there's no point in reinventing the wheel.


Q1: Is it possible to have parameters passed to \defed commands, both optional and required?

A1: Yes and No. A command defined using \def has to know exactly what its options are, and how they will be presented to it. However, a TeX command can be a bit clever and examine things other than its arguments. The way that optional commands are handled is to look at the next character in the stream and if it is [ then one version of the command is called, whereas if it isn't then another is called. So if you're prepared to do some hackery, then optional arguments are possible via \def, but it isn't half so easy as \newcommand.


Q2: Is there a \redef command equivalent to \renewcommand?

A2: No. Since \def doesn't check for a command already being defined, you can redefine stuff simply using \def again. To get the checking, you need to use the \@ifundefined command (note the ampersat (@) sign!).


Q3: I have only ever used \newcommand -- is there any reason I should change that habit?

A3: No.

The siunitx package has this sort of facility.

\num[group-separator={,}]{1234567890}

Should give you "1,234,567,890".

Also you can use this as a package option like so:

\usepackage[group-separator={,}]{siunitx}

Be warned, this doesn't seem to work with older versions of siunitx.

可以直接使用:

SELECT * FROM data_table
WHERE created_at < NOW() - INTERVAL 1 DAY;

下面是一个常见的错误写法:

DELETE FROM data_table
WHERE TIME_TO_SEC(TIMEDIFF(NOW(), created_at)) / 3600 >= 24

因为 timediff() 可接受参数的取值范围和 time 数据类型的范围一致,这样写可能会遇到 Truncated incorrect time value 错误。

MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or 'HHH:MM:SS' format for large hours values). TIME values may range from '-838:59:59' to '838:59:59'.

Here is a useful solution that works for various operating systems, including Linux, Windows, etc.:

import psutil
process = psutil.Process()
print(process.memory_info().rss) # in bytes

The Docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The Docker daemon always runs as the root user.

If you don't want to preface the docker command with sudo, create a Unix group called docker and add users to it. When the Docker daemon starts, it creates a Unix socket accessible by members of the docker group.

The docker group grants privileges equivalent to the root user. For details on how this impacts security in your system, see Docker Daemon Attack Surface.

To run Docker without root privileges, see Run the Docker daemon as a non-root user (Rootless mode).

To create the docker group and add your user:

  1. Create the docker group.

    sudo groupadd docker
  2. Add your user to the docker group.

    sudo usermod -aG docker $USER
  3. Log out and log back in so that your group membership is re-evaluated.

    If testing on a virtual machine, it may be necessary to restart the virtual machine for changes to take effect.

    On a desktop Linux environment such as X Windows, log out of your session completely and then log back in.

    On Linux, you can also run the following command to activate the changes to groups:

    newgrp docker
  4. Verify that you can run docker commands without sudo.

    docker run hello-world

    This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits.

    If you initially ran Docker CLI commands using sudo before adding your user to the docker group, you may see the following error, which indicates that your ~/.docker/ directory was created with incorrect permissions due to the sudo commands.

    WARNING: Error loading config file: /home/user/.docker/config.json - stat /home/user/.docker/config.json: permission denied

    To fix this problem, either remove the ~/.docker/ directory (it is recreated automatically, but any custom settings are lost), or change its ownership and permissions using the following commands:

    sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
    sudo chmod g+rwx "$HOME/.docker" -R