various utils for freemt
spinner
: very primitive spinner for any terminal
from spinner import Spinner
with Spinner():
sleep(10) # to emulate a longrunning job
langcode_to_tmxcode
: need (marisa-trie
) and langcodes
.
If you don't have VC in the system, you'll need to install a whl version of marisa-trie
that can be downloaded from https://www.lfd.uci.edu/~gohlke/pythonlibs/#marisa-trie
pip install freemt-utils
To validate installation
python -c "import freemt_utils; print(freemt_utils.__version__)"
0.0.1
from pathlib import Path
import asyncio
from freemt_utils import save_tempfile, switch_to, httpx_get, make_url, arun, fetch_proxies, logger_level
with switch_to():
print(Path.cwd()) # home dir
print(Path.cwd()) # back to the current work directory
try:
arun(httpx_get('www.baidu.com'))
except Exception as exc:
print(exc) # InvalidURL: No scheme included in URL.
res = arun(httpx_get(make_url('www.baidu.com')))
print(res.headers)
# Headers([('bdpagetype', '1'), ('bdqid',...
logger_level('info')
res.encoding = 'UTF-8'
save_tempfile(res.text) # display res.text in the default browser
open_in_browser
(save_tempfile): send a str of html format to browser, useful for examing Xpaths/selectors; to send a str of plaintext, add a prefix"<pre>"
(preformatted): e.g.,text = "abc\n c d f"; open_in_browser("<pre>" + text)
langcode_to_tmxcode
: language code to code used in tmx filesspinner
: a simple and primitive spinner from the netmake_url
: attempt to deliver a valid url forhttpx
/requests
etc.mtok
/mdetok
: tokenizer/detokenizer based onsacremoses
with_func_attrs
: deco for meddling a function's attr without getting flagged bypyright
/mypy
switch_to
: switch to a directory momentarily