nhpip

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

Showing Posts 1 to 9

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @nhpip did you try inspect/1 ?

i_am_a_term_now_a_string = inspect(term, limit: :infinity)
nhpip

nhpip OP

Sorry, my copy/paste screwed up…it missed the most important line:

{:ok, [term_some_time_later]} = :file.consult('/tmp/a_term_on_your_disk')
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@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

nhpip OP

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

A bit of info will be helpful. The way to handle (most) terms in a human readable way is to use inspect to dump the data to disk, and then Code.eval_string to turn it back into a value. However this has a variety of trade offs and won’t handle arbitrary terms because things like pid values for example don’t have an output from inspect that will turn back into a pid when evaled. Whether that matters or not depends on your use case.

c4710n

c4710n

If you think quoted expression is human-readable. Try to use:

  • quote
  • Code.eval_quoted

Or, just use :erlang.term_to_binary and :erlang.binary_to_term. When you want to inspect it, use something like following code instead of reading it directly.

File.read!("path/to/file")
|> :erlang.binary_to_term()
|> IO.inspect()

I think it is better to tell us your scenario. It’s an XY problem probably.

nhpip

nhpip OP

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:

b64term =  term |> :erlang.term_to_binary() |> Base.encode64()
File.write("/tmp/foo.term", b64term) # this is really a log file

Then

term = File.read!("/tmp/foo.term") |> Base.decode64!() |> :erlang.binary_to_term()
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Gotcha! Yeah so for this purpose I would definitely use term_to_binary/1 because 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 by inspecting the value you loaded via binary_to_term/1

derek-zhou

derek-zhou

I’d use ~tp.~n so 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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
kszambelanczyk
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
RemyXRenard
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
velrest
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
samoloth
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
FlyingNoodle
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
nseaSeb
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 Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews