Speaking of hidden Python tools, I'm a big fan of re.Scanner[0]. It's a regex-based tokenizer[1] in the `re` module, that for reasons is completely missing from any official documentation. You give it a pattern for each token type, and a function to be called on each match, and you get back a list of processed tokens. Importantly, it processes the list in one pass and ensures the matches are contiguous, where a naive…
Amazing! I had just written a regex tokenizer in python the other day, this would have been great!
CLI tools hidden in the Python standard library
81–90 of 160 posts
Re: CLI tools hidden in the Python standard library
#82Speaking of hidden Python tools, I'm a big fan of re.Scanner[0]. It's a regex-based tokenizer[1] in the `re` module, that for reasons is completely missing from any official documentation. You give it a pattern for each token type, and a function to be called on each match, and you get back a list of processed tokens. Importantly, it processes the list in one pass and ensures the matches are contiguous, where a naive…
use regex_automata::{
meta::Regex,
util::iter::Searcher,
Anchored, Input,
};
#[derive(Clone, Copy, Debug)]
enum Token {
Integer,
Identifier,
Punctuation,
}
fn main() {
let re = Regex::new_many(&[
r"[0-9]+",
r"[a-z_]+",
r"[,.]+",
r"\s+",
]).unwrap();
let hay = "45 pigeons, 23 cows, 11 spiders";
let input = Input::new(hay).anchored(Anchored::Yes);
let mut it = Searcher::new(input).into_matches_iter(|input| {
Ok(re.search(input))
}).infallible();
for m in &mut it {
let token = match m.pattern().as_usize() {
0 => Token::Integer,
1 => Token::Identifier,
2 => Token::Punctuation,
3 => continue,
pid => unreachable!("unrecognized pattern ID: {:?}", pid),
};
println!("{:?}: {:?}", token, &hay[m.range()]);
}
let remainder = &hay[it.input().get_span()];
if !remainder.is_empty() {
println!("did not consume entire haystack");
}
}
And its output: $ cargo run
Compiling regex-scanner v0.1.0 (/home/andrew/tmp/scratch/regex-scanner)
Finished dev [unoptimized + debuginfo] target(s) in 0.31s
Running `target/debug/regex-scanner`
Integer: "45"
Identifier: "pigeons"
Punctuation: ","
Integer: "23"
Identifier: "cows"
Punctuation: ","
Integer: "11"
Identifier: "spiders"
A bit more verbose than the Python, but the library is exposing much lower level components. You have to do a little more stitching to get the `Scanner` behavior. But it does everything the Python does: a single scan (using finite automata and not backtracking like Python), skip certain token types and guarantees that the entirety of the haystack is consumed.Re: CLI tools hidden in the Python standard library
#83https://www.google.com/search?q=site%3Adocs.python.org+%22py...
Re: CLI tools hidden in the Python standard library
#84 python3 -m http.server
I use it all the timeRe: CLI tools hidden in the Python standard library
#85Earlier quoted context omitted.
If you serve on localhost you can usually access from other devices by using the "servers" ip address. So if your desktop where you're running the server has ip 192.168.1.10 then you can go to http://192.168.1.10 in the browser of another device on the same network.
Ohh, that makes complete sense… thanks for pointing this out!
For example if your Ubuntu Linux desktop machine has host name foobar, and you run a http server on for example port 8000 then you can use your iPhone and other mDNS / Bonjour capable things to open
And likewise say you have a macOS laptop with hostname “amogus” and for example http server is listening on port 8081, you can navigate on other mDNS / Bonjour capable devices and machines to
Re: CLI tools hidden in the Python standard library
#86Earlier quoted context omitted.
I find the entire premise of the post to be pretty baffling. > Seth pointed out this is useful if you are on Windows and don't have the gzip utility installed. Okay, so instead of installing gzip (or just using the decompressors that aren't the official gzip utility but that do support the format and already ship with Windows by default[1]), you install Python...? Even if the premise weren't muddy from the start, the…
Most people who read this blog already have python installed
The topic of this thread is the safety and security of running Python on (or merely next to) arbitrary content.
Even ignoring that: is "most people" greater than, less than, or the same amount as "all"?
Re: CLI tools hidden in the Python standard library
#87Re: CLI tools hidden in the Python standard library
#88Earlier quoted context omitted.
> completely missing from any official documentation To be fair, most things are missing from the official documentation. When I learned kotlin, I read through their official docs, and knew about most language features in a day. When I learned python, I constantly got surprised by things I hadn't seen come up in the docs. For instance decorators was (still is?) not mentioned at all in the official tutorial.
Decorators seem to be documented now: https://docs.python.org/3/glossary.html#term-decorator https://docs.python.org/3/reference/compound_stmts.html#func...
Re: CLI tools hidden in the Python standard library
#89Re: CLI tools hidden in the Python standard library
#90Python 3.12 will include a SQLite CLI/REPL in the standard library too[0][1]. This is useful because most operating systems have sqlite3 and python3, but are missing the SQLite CLI. [0]: https://github.com/python/cpython/blob/3fb7c608e5764559a718c... [1]: https://docs.python.org/3.12/library/sqlite3.html#command-li...
It's weird that they're adding code to the stdlib without type annotations.