Post AYtcuN7XQhRFaSXWT2 by bartek@sfba.social
(DIR) More posts by bartek@sfba.social
(DIR) Post #AYtVuvoZjpntVfzQVE by simon@fedi.simonwillison.net
2023-08-19T16:38:43Z
0 likes, 0 repeats
New TIL: A simple pattern for inlining binary content in a Python scriptTLDR: pprint(binary_data) will output a pleasingly wrapped newline-separated sequence of ~80 character bytestrings that you can drop into a Python script in a way that won't result in super-long lineshttps://til.simonwillison.net/python/inlining-binary-data
(DIR) Post #AYtWe3vOQrmPh4hNaa by simon@fedi.simonwillison.net
2023-08-19T16:47:16Z
0 likes, 0 repeats
Also illustrates a trick I've been leaning on more recently: for little things like this I can't be bothered to write a reusable script, so I like to put together a copy-and-pasteable one-liner using "python -c" instead
(DIR) Post #AYtZQhYoZPqgQkSUU4 by arigesher@socialnotwork.net
2023-08-19T17:18:15Z
0 likes, 0 repeats
@simon look up repr() - tl:dr it’s “render this data as Python code” - which is what pprint is essentially doing here.https://docs.python.org/3/library/functions.html#repr
(DIR) Post #AYtaxPu7KxvdBFaPvU by bartek@sfba.social
2023-08-19T17:27:16Z
0 likes, 0 repeats
@arigesher @simon __repr__() won't cut it up into multiple reasonably sized lines, it'll just output it as one very long byte-string.
(DIR) Post #AYtaxQeYYJ4jVHJVJI by simon@fedi.simonwillison.net
2023-08-19T17:35:40Z
0 likes, 0 repeats
@bartek @arigesher yeah I tried repr() first but I wanted it to be wrapped as multiple lines, not just one super-long one
(DIR) Post #AYtb8rxXMwX9NSeApU by arigesher@socialnotwork.net
2023-08-19T17:36:52Z
0 likes, 0 repeats
@bartek @simon ah-hah! Yes, that makes sense. Putting the pretty in pprint()!
(DIR) Post #AYtbKaY9vFCirHMfho by adamchainz@fosstodon.org
2023-08-19T17:39:00Z
0 likes, 0 repeats
@simon Black with `—preview` will auto split and wrap strings too.
(DIR) Post #AYtbp9xsoakzNoyTOS by bartek@sfba.social
2023-08-19T17:45:11Z
0 likes, 0 repeats
@simon @arigesher I'm curious though what's the use case for this? Why not base64, which can be directly inlined in HTML, Markdown, Jupyter, etc, via `data:image/png,{base64(binary)}` URI.?
(DIR) Post #AYtc1p91XH6MZhDxFA by bartek@sfba.social
2023-08-19T17:46:12Z
0 likes, 0 repeats
@simon @arigesher I guess though the pprint() trick would still make sense for splitting into lines.
(DIR) Post #AYtc1pweYknh3cRabI by simon@fedi.simonwillison.net
2023-08-19T17:47:04Z
0 likes, 0 repeats
@bartek @arigesher Yeah this feels better than base64 to me - there's less work needed to decode it and the code is pretty much the same in terms of how ugly it looks
(DIR) Post #AYtci69KhZhXTeWi6y by simon@fedi.simonwillison.net
2023-08-19T17:55:17Z
0 likes, 0 repeats
Just used this pattern to clean up an old test suite that embeds some binary image files: https://github.com/simonw/datasette-render-images/blob/c7a3c287ef53e597df984506ac5161c9e279fb7b/test_datasette_render_images.py
(DIR) Post #AYtcuN7XQhRFaSXWT2 by bartek@sfba.social
2023-08-19T17:57:34Z
0 likes, 0 repeats
@simon @arigesher yeah, that's why I asked about the use case. If you want to serve as binary data or output to a file, raw binary makes more sense. If you want to inline in CSS, HTML, Markdown, etc. then base64 makes more sense as it can be used directly without any additional processing.
(DIR) Post #AYtkAPMrXeZorwIk7c by wbolster@mastodon.social
2023-08-19T19:18:35Z
0 likes, 0 repeats
@simon @bartek @arigesher base64 (the command line tool) output can be copy-pasted into a nicely formatted block, likely shorter than the repr/pprint version:data = base64.b64decode( """ AAAA """)
(DIR) Post #AYtmYFbfgWhPSBizlQ by simon@fedi.simonwillison.net
2023-08-19T19:45:26Z
0 likes, 0 repeats
@wbolster @bartek @arigesher Yeah that's a good call, this seems to work OK for wrapping it:base64 -i content.db | fold -w 64
(DIR) Post #AYtnXDaHonbKqW9GOu by wbolster@mastodon.social
2023-08-19T19:56:27Z
0 likes, 0 repeats
@simon @bartek @arigesher my (gnu) base64(1) command takes -w directly, but there's no need b/c the default (76) plus 4 spaces in front is just fine (blackened) python already
(DIR) Post #AYtsVgoOgBd2gYq56u by callemo@hachyderm.io
2023-08-19T20:52:05Z
0 likes, 0 repeats
@simon that is convenient. Sometimes, I have invoked python3 -c inside short shell scripts in the middle of a pipeline. This approach is common with awk one-liners.
(DIR) Post #AYu75kDatHJ728i9iK by simon@fedi.simonwillison.net
2023-08-19T23:32:02Z
0 likes, 0 repeats
@adamchainz huh, I have not seen it do that!
(DIR) Post #AYumgckxgzq2j61U1I by adamchainz@fosstodon.org
2023-08-20T07:21:50Z
0 likes, 0 repeats
@simon Ah, it needs space characters, and maybe others, to split on. So not helpful for your binary string use case.