Markdig – rendering the Markdown

ติดตั้ง Markdig

PM> NuGet\Install-Package Markdig -Version 0.30.4

Usage

The main entry point for the API is the Markdig.Markdown class:

By default, without any options, Markdig is using the plain CommonMark parser:

using Markdig;

var result = Markdown.ToHtml("This is a text with some *emphasis*");
Console.WriteLine(result);   // prints: <p>This is a text with some <em>emphasis</em></p>

In order to activate most of all advanced extensions (except Emoji, SoftLine as HardLine, Bootstrap, YAML Front Matter, JiraLinks and SmartyPants)

using Markdig;

// Configure the pipeline with all advanced extensions active
var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
var result = Markdown.ToHtml("This is a text with some *emphasis*", pipeline);

Python – GPG

ทดสอบบน Ubuntu 20.04.5 LTS ใน WSL 2

  1. Generate a key
  2. Export keys
  3. Import keys
  4. List keys
  5. Encrypt a string
  6. Decrypt a string
  7. Encrypt a file
  8. Decrypt a file

Installation

ติดตั้ง gnupg (น่าจะติดตั้งอยู่แล้ว)

sudo apt-get install gnupg 
$ gpg --version
gpg (GnuPG) 2.2.19
libgcrypt 1.8.5
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: /home/jack/.gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
        CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2

ติดตั้ง python-gnupg

pip install python-gnupg

1. Generate a key

import os
import gnupg

os.system('rm -rf /home/jack/gpghome')
os.system('mkdir  /home/jack/gpghome')

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
input_data = gpg.gen_key_input(
    key_length=4096,
    name_email='mr.phaisarn@gmail.com',
    passphrase='my passphrase')
key = gpg.gen_key(input_data)
print(key)
84511C0301E2D51E59D79C91B5432E86F4B8925A

2. Export keys

export ค่า public key และ private key ไว้ในไฟล์ mykeyfile.asc

import gnupg

key='84511C0301E2D51E59D79C91B5432E86F4B8925A'

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
ascii_armored_public_keys = gpg.export_keys(key,passphrase='my passphrase')
ascii_armored_private_keys = gpg.export_keys(key, True, passphrase='my passphrase')
with open('mykeyfile.asc', 'w') as f:
    f.write(ascii_armored_public_keys)
    f.write(ascii_armored_private_keys)
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBGNqDu4BEADY6n5U0Fs4YBhAejiPZu5hI5i3oYbscK5xQSSXvgkUwoQkmHKQ
JVv+Chq4+jCguBSlmVXXMNnk45F7OhjuxpfrXDSxQPivwchXTh0117UEtTi5jE2s
eZTP5fMSdHomdD4NgU9PfSUiSPNH/PGnSQ8AFstFdFff846ry+dPu3XJdbkkVtEP
Cbv6idGCa2u/ZMukj2o2ZbvOXNVj1M5RFOpqJ6m52UQXA1W8S/Qr3g7gJ41jTWE3
oOP1oSfw5IbuO6yTrhOjwELTM2BexXwUAEtW59A8e+1JVh1xtbiTRxmnC+WHZ1T1
RQ9a7rFbFyqmboZ5RE8wUX7SsklzPyXtZhCnsFfBt3OVJNyD5hPoZ4qq7ihGeIIa
OD2g2VDu+uHeJg4Vqd8YcafFruLs0WR0BAdliYHClhhGSTQ2mFK7jQDfAVZYuFhZ
kKqd7WV7++e/oyLViWxDvhBN1XQ7Wbw5o1hkgeH6cyFg9nX/psVecXCriOJJUyVh
AKeFypLgfUiXkNoNuiWIcyJer55wIzjV0oTcLmHLtedh4Bh0+qvI+EI2UnKW2Ih0
Xas9jE3nx97T5365MD7hDjjm7sqmbg3xbih2VXUC+WjPuaisqbGgM/3vO06VA2A4
jg3j1SUm0N4IN8LSFzmAtvoSc5kvHDzTgySCYwlC3YowjKPciMu24jfjXwARAQAB
tClBdXRvZ2VuZXJhdGVkIEtleSA8b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BLOCK-----

3. Import keys

import gnupg
from pprint import pprint

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
key_data = open('mykeyfile.asc').read()
import_result = gpg.import_keys(key_data)
pprint(import_result.results)
[{'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A',
  'ok': '0',
  'text': 'Not actually changed\n'},
 {'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A',
  'ok': '0',
  'text': 'Not actually changed\n'},
 {'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A',
  'ok': '16',
  'text': 'Not actually changed\nContains private key\n'}]

4. List keys

import gnupg
from pprint import pprint

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
public_keys = gpg.list_keys()
private_keys = gpg.list_keys(True)
print('public keys:')
pprint(public_keys)
print('private keys:')
pprint(private_keys)
public keys:
[{'algo': '1',
  'cap': 'escaESCA',
  'compliance': '23',
  'curve': '',
  'date': '1667895022',
  'dummy': '',
  'expires': '',
  'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A',
  'flag': '',
  'hash': '',
  'issuer': '',
  'keygrip': 'AADC0ABF3CA1285034094D0381E0142B513D5514',
  'keyid': 'B5432E86F4B8925A',
  'length': '4096',
  'origin': '0',
  'ownertrust': 'u',
  'sig': '',
  'sigs': [],
  'subkeys': [],
  'token': '',
  'trust': 'u',
  'type': 'pub',
  'uids': ['Autogenerated Key <mr.phaisarn@gmail.com>'],
  'updated': ''}]
private keys:
[{'algo': '1',
  'cap': 'escaESCA',
  'compliance': '23',
  'curve': '',
  'date': '1667895022',
  'dummy': '',
  'expires': '',
  'fingerprint': '84511C0301E2D51E59D79C91B5432E86F4B8925A',
  'flag': '',
  'hash': '',
  'issuer': '',
  'keygrip': 'AADC0ABF3CA1285034094D0381E0142B513D5514',
  'keyid': 'B5432E86F4B8925A',
  'length': '4096',
  'origin': '0',
  'ownertrust': 'u',
  'sig': '',
  'sigs': [],
  'subkeys': [],
  'token': '+',
  'trust': 'u',
  'type': 'sec',
  'uids': ['Autogenerated Key <mr.phaisarn@gmail.com>'],
  'updated': ''}]

5. Encrypt a string

import gnupg

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
unencrypted_string = 'Who are you? How did you get in my house?'
encrypted_data = gpg.encrypt(unencrypted_string, 'mr.phaisarn@gmail.com')
encrypted_string = str(encrypted_data)

print('ok: ', encrypted_data.ok)
print('status: ', encrypted_data.status)
print('stderr: ', encrypted_data.stderr)
print('unencrypted_string: ', unencrypted_string)
print('encrypted_string: ', encrypted_string)
ok:  True
status:  encryption ok
stderr:  gpg: WARNING: unsafe permissions on homedir '/home/jack/gpghome'
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 2
[GNUPG:] ENCRYPTION_COMPLIANCE_MODE 23
[GNUPG:] BEGIN_ENCRYPTION 2 9
[GNUPG:] END_ENCRYPTION

unencrypted_string:  Who are you? How did you get in my house?
encrypted_string:  -----BEGIN PGP MESSAGE-----

hQIMA7VDLob0uJJaAQ//czHG42eeN1rtFTo6qdvoHSa/B3uJNeFSobUi4gUCxceh
yVbZ0La8pnLMy2JmA2agYaOvC6OhAXfNOhPXV5jinMTvwGB+dKC1qaAYXMknQjyF
5whvmQeBbuQX8AoKxs7hAYmlQO8mrHDBoP2xSdY/AgLeqdlnbZNcDR+NIpoPJS8a
zr+KHYAhEyZT0Bm6FAw7DvhriB4nJRLeMLoIQKIJTN3ZeEdlcZAt5xYL24dZfz8/
4Qpn++ItqsCQfXJ1leJIIE/oB4E83/J4FZuS1xh7n9OomBT2fRG8hmGlSzFYepX/
jDzjApmA/R4HGd6BuJZdNy6SV2DERs2NlTRko98IDFdCIg2Jp7jsSGSZowsKD7z5
rIU110iwR6hRropsuX18jlRk6QM1Um+dSGbHXTUk1OzUDE5uC3hBZsB0s9M2q5DS
HN+EbyatT9hB9X5Obf7w9Vj+Jta9q5voL/riZ2d+GFr8oR0d594An7Dup12zBff/
kdV7DgibAqorCD1MvVePOpvYw2NdhukRluOsVtXySkcAGy8VssBLoW3kqilOvrhq
pCJUG157TnhKnYW0l4BJ7KVoNFwBGsJKzS0Drh5Iq6LmiqHsKv7neK9LkApDeehD
EhdNoGN79fPvEYmync2bbGbVq6F7czHADwVKqNv0aDxaCrFHASsSXkGNbqMQ+tnS
YgFfzEOmYoDR7UjEog/OrBJAnPSgDcwn3I0xuU3RtDaEvlHCt6aX+NHEyIUvsIe+
Don8uUXi4VkjO2uoCOyNgwndoigDuouh8NDv5DIWcKt+KMGPkgKEmfbOxJNA0Rl5
TK5E
=Jyon
-----END PGP MESSAGE-----

6. Decrypt a string

import gnupg

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
unencrypted_string = 'Who are you? How did you get in my house?'
encrypted_data = gpg.encrypt(unencrypted_string, 'mr.phaisarn@gmail.com')
encrypted_string = str(encrypted_data)
decrypted_data = gpg.decrypt(encrypted_string, passphrase='my passphrase')

print('ok: ', decrypted_data.ok)
print('status: ', decrypted_data.status)
print('stderr: ', decrypted_data.stderr)
print('decrypted string: ', decrypted_data.data)
ok:  True
status:  decryption ok
stderr:  gpg: WARNING: unsafe permissions on homedir '/home/jack/gpghome'
[GNUPG:] ENC_TO B5432E86F4B8925A 1 0
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
[GNUPG:] DECRYPTION_KEY 84511C0301E2D51E59D79C91B5432E86F4B8925A 84511C0301E2D51E59D79C91B5432E86F4B8925A u
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
gpg: encrypted with 4096-bit RSA key, ID B5432E86F4B8925A, created 2022-11-08
      "Autogenerated Key <mr.phaisarn@gmail.com>"
[GNUPG:] BEGIN_DECRYPTION
[GNUPG:] DECRYPTION_COMPLIANCE_MODE 23
[GNUPG:] DECRYPTION_INFO 2 9
[GNUPG:] PLAINTEXT 62 1667895570
[GNUPG:] PLAINTEXT_LENGTH 41
[GNUPG:] DECRYPTION_OKAY
[GNUPG:] GOODMDC
[GNUPG:] END_DECRYPTION

decrypted string:  b'Who are you? How did you get in my house?'

7. Encrypt a file

import gnupg

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
open('my-unencrypted.txt', 'w').write('You need to Google Venn diagram.')
with open('my-unencrypted.txt', 'rb') as f:
    status = gpg.encrypt_file(
        f, recipients=['mr.phaisarn@gmail.com'],
        output='my-encrypted.txt.gpg')

print('ok: ', status.ok)
print('status: ', status.status)
print('stderr: ', status.stderr)
ok:  True
status:  encryption ok
stderr:  gpg: WARNING: unsafe permissions on homedir '/home/jack/gpghome'
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 2
[GNUPG:] ENCRYPTION_COMPLIANCE_MODE 23
[GNUPG:] BEGIN_ENCRYPTION 2 9
[GNUPG:] END_ENCRYPTION
-----BEGIN PGP MESSAGE-----

hQIMA7VDLob0uJJaARAAhwNuaLm5/eSCV81Uw+H2E536AzzXm/yPxM0CIjLjYxnE
+GwgvBxJsYtuuwCElUfYkIKYNzAWOGGRje961Be3+g5kjFfphvl4I1Xzwb5RKdWa
i9raKNQ1/PLQ6Hr8Fc3yh08QcgLkihyRgVKZ3suTX+KRzWd3/SS8rjS27nigQk8C
IoIuwJIsBYpB8xbBDM+Sfo2XJm938Z7n9ZfdDiuO0hEWBo5qF85Vh0jY91BVyPqD
yGYcsivK/OqRG/VrPTrp01+2+jBn5HPZoPCnjEZdyJ3p+k/bnvAymKpKmPfEHrt3
4FwJ0EIfFROx6u2xBWhoRAhl5vLjIgRqccwEHD9+HrT3aFyeQhOKcQoV0TXy5Y0w
rYLRBny2oolCGW1NqZi43pjamQXXVpJLDjOsn8pzhdbFiSxUrFZj2WXET7ItQaVd
fQQnPxpOpj7LOOncLdZ0qfGMYjNNXGoKlaPfdtJy1yQlz8mUUdZZAjAdCpKpnfca
YxuFmzMy2gGBKsYYrLB2FH6HasRbjb5uQ1qO72vybuPWCBNkX5jgWHGLddNagxR7
9EwMnvu958Oy3TiFfrr7YKWPIOQXaLx8WsgXCCl2rFJKG4Hy81ZM5k7DDQzzQFGd
w+a5FWo/cthXm6qx2LSE2ByFyAerqwe/T/wkAYmrQLIJdxZIWxp5xsuPBS6W6oLS
WwEiSHXqJPjqKHKnZwS0W9kWq/68voNf03QlEkN0+Hm3PRmEFIJnpw19BtfQEoY3
Rv/XCQXE2G6NVMajvFj72fvFzTUUyVRa8m//nu2wg9vG5JMjJbThA8L8+5E=
=WZm8
-----END PGP MESSAGE-----

ถ้า import public key แล้วนำมาใช้ encrypt จะได้ error invalid recipient

ok:  False
status:  invalid recipient
stderr:  gpg: WARNING: unsafe permissions on homedir `/home/jack/gpghome'
gpg: EAECD251: There is no assurance this key belongs to the named user
[GNUPG:] INV_RECP 10 84511C0301E2D51E59D79C91B5432E86F4B8925A
gpg: [stdin]: encryption failed: Unusable public key

ให้เพิ่ม gpg.trust_keys()

import gnupg

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
gpg.trust_keys('84511C0301E2D51E59D79C91B5432E86F4B8925A', 'TRUST_ULTIMATE')
open('my-unencrypted.txt', 'w').write('You need to Google Venn diagram.')
with open('my-unencrypted.txt', 'rb') as f:
    status = gpg.encrypt_file(
        f, recipients='84511C0301E2D51E59D79C91B5432E86F4B8925A',
        output='encrypted.txt.gpg')

print('ok: ', status.ok)
print('status: ', status.status)
print('stderr: ', status.stderr)

8. Decrypt a file

import gnupg

gpg = gnupg.GPG(gnupghome='/home/jack/gpghome')
with open('my-encrypted.txt.gpg', 'rb') as f:
    status = gpg.decrypt_file(f, passphrase='my passphrase', output='my-decrypted.txt')

print('ok: ', status.ok)
print('status: ', status.status)
print('stderr: ', status.stderr)
ok:  True
status:  decryption ok
stderr:  gpg: WARNING: unsafe permissions on homedir '/home/jack/gpghome'
[GNUPG:] ENC_TO B5432E86F4B8925A 1 0
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
[GNUPG:] DECRYPTION_KEY 84511C0301E2D51E59D79C91B5432E86F4B8925A 84511C0301E2D51E59D79C91B5432E86F4B8925A u
[GNUPG:] KEY_CONSIDERED 84511C0301E2D51E59D79C91B5432E86F4B8925A 0
gpg: encrypted with 4096-bit RSA key, ID B5432E86F4B8925A, created 2022-11-08
      "Autogenerated Key <mr.phaisarn@gmail.com>"
[GNUPG:] BEGIN_DECRYPTION
[GNUPG:] DECRYPTION_COMPLIANCE_MODE 23
[GNUPG:] DECRYPTION_INFO 2 9
[GNUPG:] PLAINTEXT 62 1667895671
[GNUPG:] PLAINTEXT_LENGTH 32
[GNUPG:] DECRYPTION_OKAY
[GNUPG:] GOODMDC
[GNUPG:] END_DECRYPTION

Ubuntu Package Management

Apt

The apt command is a powerful command-line tool, which works with Ubuntu’s Advanced Packaging Tool (APT) performing such functions as installation of new software packages, upgrade of existing software packages, updating of the package list index, and even upgrading the entire Ubuntu system.

Some examples of popular uses for the apt utility:

Install a Package: Installation of packages using the apt tool is quite simple. For example, to install the nmap network scanner, type the following:

sudo apt install nmap

Remove a Package: Removal of a package (or packages) is also straightforward. To remove the package installed in the previous example, type the following:

sudo apt remove nmap

auto remove package

sudo apt autoremove

OPTIMIZE

Optimizes the layout of Delta Lake data. Optionally optimize a subset of data or colocate data by column. If you do not specify colocation, bin-packing optimization is performed.

Syntax

OPTIMIZE table_name [WHERE predicate]
  [ZORDER BY (col_name1 [, ...] ) ]

Parameters

  • table_name – Identifies an existing Delta table. The name must not include a temporal specification.
  • WHERE – Optimize the subset of rows matching the given partition predicate. Only filters involving partition key attributes are supported.
  • ZORDER BY – Colocate column information in the same set of files. Co-locality is used by Delta Lake data-skipping algorithms to dramatically reduce the amount of data that needs to be read. You can specify multiple columns for ZORDER BY as a comma-separated list. However, the effectiveness of the locality drops with each additional column.

Examples

OPTIMIZE delta.`/data/events`

OPTIMIZE events

OPTIMIZE events WHERE date >= '2022-11-18'

OPTIMIZE events
WHERE date >= current_timestamp() - INTERVAL 1 day
ZORDER BY (eventType)

VACUUM

VACUUM table_name [RETAIN num HOURS] [DRY RUN]

Parameters

  • table_name – Identifies an existing Delta table. The name must not include a temporal specification.
  • RETAIN num HOURS – The retention threshold.
  • DRY RUN – Return a list of up to 1000 files to be deleted.

แสดงรายชื่อไฟล์ที่จะถูกลบ

VACUUM table_name RETAIN 720 HOURS DRY RUN

ทำการลบไฟล์

VACUUM table_name RETAIN 720 HOURS
1 day24 HOURS
1 week168 HOURS
2 week336 HOURS
30 days720 HOURS

Configure data retention for time travel

To time travel to a previous version, you must retain both the log and the data files for that version.

The data files backing a Delta table are never deleted automatically; data files are deleted only when you run VACUUMVACUUM does not delete Delta log files; log files are automatically cleaned up after checkpoints are written.

By default you can time travel to a Delta table up to 30 days old unless you have:

  • Run VACUUM on your Delta table.
  • Changed the data or log file retention periods using the following table properties:
    • delta.logRetentionDuration = "interval <interval>": controls how long the history for a table is kept. The default is interval 30 days.Each time a checkpoint is written, Databricks automatically cleans up log entries older than the retention interval. If you set this config to a large enough value, many log entries are retained. This should not impact performance as operations against the log are constant time. Operations on history are parallel but will become more expensive as the log size increases.
    • delta.deletedFileRetentionDuration = "interval <interval>": controls how long ago a file must have been deleted before being a candidate for VACUUM. The default is interval 7 days.To access 30 days of historical data even if you run VACUUM on the Delta table, set delta.deletedFileRetentionDuration = "interval 30 days". This setting may cause your storage costs to go up.

ถ้ารัน VACUUM เลย data files ที่เกิน 7 วันจะถูกลบ

ถ้าจะเก็บ data files ให้มากกว่า 7 วัน โดยไม่ต้องมาคอยกำหนดค่า RETAIN num HOURS ให้ไป SET delta.deletedFileRetentionDuration ก่อน แล้วค่อยรัน VACUUM

SET and UNSET TBLPROPERTIES

ALTER TABLE table_name
   { RENAME TO clause |
     ADD COLUMN clause |
     ALTER COLUMN clause |
     DROP COLUMN clause |
     RENAME COLUMN clause |
     ADD CONSTRAINT clause |
     DROP CONSTRAINT clause |
     ADD PARTITION clause |
     DROP PARTITION clause |
     RENAME PARTITION clause |
     RECOVER PARTITIONS clause |
     SET TBLPROPERTIES clause |
     UNSET TBLPROPERTIES clause |
     SET SERDE clause |
     SET LOCATION clause |
     SET OWNER TO clause }

Example

ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('delta.logRetentionDuration' = 'interval 30 days');
ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('delta.deletedFileRetentionDuration' = 'interval 30 days');

ALTER TABLE dbx.tab1 UNSET TBLPROPERTIES ('delta.deletedFileRetentionDuration');

Joblib

Install

pip install joblib

การหา square root ด้วย Python

from math import sqrt
li = [sqrt(i ** 2) for i in range(10)]
print(li)
# [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

ใช้ joblib

from math import sqrt
from joblib import Parallel, delayed
li = Parallel(n_jobs=2)(delayed(sqrt)(i**2) for i in range(10))
print(li)
# [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

verbose: int, optional

The verbosity level: if non zero, progress messages are printed. Above 50, the output is sent to stdout. The frequency of the messages increases with the verbosity level. If it more than 10, all iterations are reported.

%python
from math import sqrt
from joblib import Parallel, delayed
li = Parallel(n_jobs=2, verbose=100)(delayed(sqrt)(i**2) for i in range(10))
print(li)

# [Parallel(n_jobs=2)]: Using backend LokyBackend with 2 concurrent workers.
# [Parallel(n_jobs=2)]: Done   1 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   2 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   3 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   4 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   5 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   6 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   7 tasks      | elapsed:    0.5s
# [Parallel(n_jobs=2)]: Done   8 out of  10 | elapsed:    0.5s remaining:    0.1s
# [Parallel(n_jobs=2)]: Done  10 out of  10 | elapsed:    0.5s remaining:    0.0s
# [Parallel(n_jobs=2)]: Done  10 out of  10 | elapsed:    0.5s finished
# [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

default backend เป็น loky เราสามารถเปลี่ยนเป็น threading , multiprocessing ได้

from math import sqrt
from joblib import Parallel, delayed
li = Parallel(n_jobs=2, verbose=100, backend='threading')(delayed(sqrt)(i**2) for i in range(10))
print(li)

Azure App Services ฟ้อง 404 (Not Found)

นำโปรเจ็กส์ที่สร้างด้วย Bookdown ขึ้น Azure App Services (Stack=.Net, .Net version=.Net 6) แล้วกล่อง Search ใช้งานไม่ได้ ทั้งที่ตอนพัฒนาก็รันด้วย RStudio แล้วใช้งานกล่อง Search เพื่อทำการค้นหาได้ปกติดี

เลยลอง F12 ดูที่ Console ก็เจอว่า search_index.json 404 (Not Found) ทั้งที่ไฟล์ก็มีอยู่

ลองค้น google ดูจนไปเจอกระทู้นี้ javascript – Getting “404 not found” on doing a GET on local file in Azure web app – Stack Overflow อาการเหมือนกันคือมีปัญหาว่าเรียกไฟล์ javascript จากที่อื่นได้ แต่เรียกไฟล์ javascript ที่อยู่ใน local ไม่ได้

ทางแก้คือสร้างไฟล์ web.config ตามนี้

<?xml version="1.0"?>
<configuration>
    <system.webServer>
        <staticContent>
            <mimeMap fileExtension=".json" mimeType="application/json" />
     </staticContent>
    </system.webServer>
</configuration>

แล้วเอาไปวางไว้ที่ /site/wwwroot

ลองเรียกเว็บอีกที ก็ใช้ได้ละ

Pandoc

If you need to convert files from one markup format into another, pandoc is your swiss-army knife.

Installing pandoc

The simplest way to get the latest pandoc release is to use the installer.

Download the latest installer for Windows (64-bit)

Demos – Try pandoc online

Try pandoc online!

Example

Getting started with pandoc

On Windows, you can use either the classic command prompt or the more modern PowerShell terminal. If you use Windows in desktop mode, run the cmd or powershell command from the Start menu. If you use the Windows 8 start screen instead, simply type cmd or powershell, and then run either the “Command Prompt” or “Windows Powershell” application. If you are using cmd, type chcp 65001 before using pandoc, to set the encoding to UTF-8.

สร้างตัวอย่างไฟล์ test1.md

---
title: Test ภาษาไทย
...

# Test! ภาษาไทย

This is a test of *pandoc*.

- list one
- list two
- ภาษาไทย

รันคำสั่งที่ PowerShell

> pandoc test1.md -f markdown -t docx -s -o test1.docx

Pandoc User’s Guide

Ref