nhpip
Hi,
I want to write a term (nested structs) in human-readable form to a text file. This is what I’ve got, there must be a better way…
i_am_a_term_now_a_string = :io_lib.format("~p.", [i_am_a_term])
|> List.flatten()
|> to_string()
File.write!("/tmp/a_term_on_your_disk", i_am_a_term_now_a_string)
Thanks
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
Hey @nhpip did you try
inspect/1?nhpip
Sorry, my copy/paste screwed up…it missed the most important line:
benwilson512
@nhpip Can you explain what this does and what you’re trying to do? Are you trying to turn that term back into an Elixir value?
nhpip
In a nutshell yes. I want to be able to write a term to disk as text. Then read it again later, the file needs to be human-readable. I can explain why if you are interested.
benwilson512
A bit of info will be helpful. The way to handle (most) terms in a human readable way is to use
inspectto dump the data to disk, and thenCode.eval_stringto turn it back into a value. However this has a variety of trade offs and won’t handle arbitrary terms because things likepidvalues for example don’t have an output frominspectthat will turn back into apidwhen evaled. Whether that matters or not depends on your use case.c4710n
If you think quoted expression is human-readable. Try to use:
quoteCode.eval_quotedOr, just use
:erlang.term_to_binaryand:erlang.binary_to_term. When you want to inspect it, use something like following code instead of reading it directly.nhpip
Basically we have a deployment that is in a very secure enclave. We don’t have remote shell access and the normal ways you would debug an Elixir application.
What we do have is the ability to view logs. What I had thought is having the ability to write to a log terms that need analyzing and re-load them for later analysis at work (well my desk in the living room). It would allow us to replay the terms locally.
However, if I’m wiling to lose the ability to make whatever is logged human-readable, I think I can do this:
Then
benwilson512
Gotcha! Yeah so for this purpose I would definitely use
term_to_binary/1because this will be a far more reliable way of getting the exact values loaded into a shell later. You can always make it human readable on your local machine byinspecting the value you loaded viabinary_to_term/1derek-zhou
I’d use
~tp.~nso there is unicode characters. and a newline between terms. also you don’t need to flatten the iostring prior to write.Most Erlang config files are written like this and read back by consult. It is very nice and simple. Or you can use other serialization format like json, or toml but they don’t support all erlang types.