recover the 0 in #<SB-IMPL::PROCESS :EXITED 0> - sbcl

on my Debian wheezy with SBCL:
1) Alsaplayer play a CD audio:
* (run-program "/usr/bin/alsaplayer" '("-q" "CD.cdda") :wait nil)
#<SB-IMPL::PROCESS 7199 :RUNNING>
2) ps tells me that the audio CD plays: (EXITED 0)
* (run-program "/bin/ps" '("-C" "alsaplayer"))
#<SB-IMPL::PROCESS :EXITED 0>
Problem:
I want to recover the values 0 in the part 2)
but let's say that I put the result of 2) in the variable var0.
* (type-of var0)
SB-IMPL::PROCESS
I would like to know if there is a direct way to get the 0 ?
to recover it I did:
*(setf str0 (format nil "~a" var0))
"#<PROCESS :EXITED 0>"
* (aref str0 18)
#\0
but it doesn't seem very clever ?
Thanks in advance for any clue.
Gerard

Try
(sb-ext:process-exit-code var0)
It's in the SBCL manual.

Related

Python- Return a verified variable from a function to the main program

Can anyone please direct me to an example of where one can send a user input variable to a checking function or module & return the validated input assigning / updating the initialised variable?. I am trying to re-create something I did in C++ many years ago where I am trying to validate an integer! In this particular case that the number of bolts input in a building frame connection is such. Any direction would be greatly appreciated as my internet searches and trawls through my copy of Python A Crash Course have yet to shed any light! Many thanks in anticipation that someone will feel benevolent towards a Python newbie!
Regards Steve
Below is one on my numerous attempts at this, really I would just like to abandon and use While and a function call. In this one apparently I am not allowed to put > (line 4) between str and int, this desite my attempt to force N to be int - penultimate line!
def int_val(N):
#checks
# check 1. n > 0 for real entries
N > 0
isinstance(N, int)
N=N
return N
print("N not ok enter again")
#N = input("Input N the Number of bolts ")
# Initialiase N=0
#N = 0
# Enter the number of bolts
N = input("Input N the Number of bolts ")
int_val(N)
print("no of bolts is", N)
Is something like this what you have in mind? It takes advantage of the fact that using the built-in int function will convert a string to an integer if possible, but otherwise throw a ValueError.
def str_to_posint(s):
"""Return value if converted and greater than zero else None."""
try:
num = int(s)
return num if num > 0 else None
except ValueError:
return None
while True:
s = input("Enter number of bolts: ")
if num_bolts := str_to_posint(s):
break
print(f"Sorry, \"{s}\" is not a valid number of bolts.")
print(f"{num_bolts = }")
Output:
Enter number of bolts: twenty
Sorry, "twenty" is not a valid number of bolts.
Enter number of bolts: 20
num_bolts = 20
def str_to_posint(s):
"""Return value if converted and greater than zero else None."""
try:
num = int(s)
return num if num > 0 else None
except ValueError:
return None
while True:
s = input("Enter number of bolts: ")
if num_bolts := str_to_posint(s):
break
print(f"Sorry, "{s}" is not a valid number of bolts.")
print(f"{num_bolts = }")

Mysql in Erlang

I want get data in Mysql by Erlang but the data output I get it show all col_name and it not render one by one
This is my output:
{selected,["id","first_name","last_name"],
[{1,"Matt","Williamson"},
{2,"Matt","Williamson2"},
{3,"Matt","Williamson3"}]}}
I'm using this code :
application:start(odbc),
ConnString = "DSN=mysqldb",
{ok, Conn} = odbc:connect(ConnString, []),
Results = odbc:sql_query(Conn, "SELECT * FROM test_table"),
io:format("~p",[Results]).
How can I don't get the {selected, [col_name]}?
And how to render data I get one by one like for loop in php using Erlang?
It render
{1,"Matt","Williamson"}{2,"Matt","Williamson2"}{3,"Matt","Williamson3"} have
I want it render like this
1 have Matt Williamson
2 have Matt Williamson2
3 have Matt Williamson3
You may use pattern matching like this :
{selected, _Column, Data} = odbc:sql_query(Conn, "SELECT * FROM test_table"),
then use io:format("~p",[Data]).
and you will get :
[
{1,"Matt","Williamson"},
{2,"Matt","Williamson2"},
{3,"Matt","Williamson3"}
]
You just need a little more pattern-matching and the lists:foreach function:
{selected, _, Records} = odbc:sql_query(Conn, "SELECT * FROM test_table"),
lists:foreach(fun({Id, FName, LName}) ->
io:format("~p have ~s ~s~n", [Id, FName, LName])
end, Records).
Output:
1 have Matt Williamson
2 have Matt Williamson2
3 have Matt Williamson3
Ty for help me I find a way to do that is use
{% for key,value1,value2 in username %}
in dtl file

Why is the stream blocking in this iex session?

I'm trying to parse some CSVs using elixir:
iex -S mix
Erlang/OTP 18 [erts-7.2] [source-e6dd627] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.3.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> a = File.stream!("test/lib/fit_notes/fit_notes_export.csv") |> CSV.decode
#Function<49.97003610/2 in Stream.transform/3>
iex(2)> Stream.take(a, 1)
#Stream<[enum: #Function<49.97003610/2 in Stream.transform/3>,
funs: [#Function<38.97003610/1 in Stream.take/2>]]>
iex(3)> Enum.take(a, 1)
[["Date", "Exercise", "Category", "Weight (kgs)", "Reps", "Distance",
"Distance Unit", "Time"]]
iex(4)> Enum.take(a, 2)
^ this just blocks
The first Enum.take that I issue works, the second blocks forever. Can you tell me what am I doing wrong? I'm using this library for CSV parsing.
If you follow the example from http://hexdocs.pm/csv/CSV.html
your code would end up something like this:
a = File.stream!("test/lib/fit_notes/fit_notes_export.csv") |> CSV.decode |>
Stream.take(1) |>
Stream.take(1) |>
Enum.take(2)
Note I changed your first Enum.take(1) in to a Stream.take(1) so that the Stream doesn't get prematurely terminated. Also note doing two Stream.take(1) will be better converted into a single Stream.take(2). Also note how the stream piping works by adding a |> to the end of the each line until you reach an Enum call - which then fires the whole operation.
Added:
For Streams with side-effects (like logging) see
https://github.com/elixir-lang/elixir/issues/1831 where they recommend Stream.each

tail() function in XQuery

Is there a way in XQuery to do something like a tail() function?
What I'm trying to accomplish is to get the contents of a file (using "xdmp:filesystem-file($path)") and then display only the last 100 lines. I can't seem to find a good way to do this. Any ideas?
Thank you.
In plain XQuery, this can be accomplished by splitting into lines and getting the desired number of lines from the end of the sequence, then rejoining them, if necessary, i.e.
declare function local:tail($content as xs:string, $number as xs:integer)
{
let $linefeed := "
"
let $lines := tokenize($content, $linefeed)
let $tail := $lines[position() > last() - $number]
return string-join($tail, $linefeed)
};
A pure and short XPath 2.0 solution -- can be used not only in XQuery but in XSLT or in any other PL hosting XPath 2.0:
for $numLines in count(tokenize(., '
'))
return
tokenize(., '
')[position() gt $numLines -100]
Or:
for $numLines in count(tokenize(., '
'))
return
subsequence(tokenize(., '
'), $numLines -100 +1)
if your xdmp:file-xx is a nature of text file then you could use something like
let $f := 'any file system path'
return fn:tokenize(xdmp:filesystem-file($f), '[\n\r]+')[ fn:last() - 2 to fn:last()]
here
i have used newline & carriage return as my token splitter. if you need something else to tokenize u could. but simple log file tailing then this solution works fine.
given example tails last 2 lines of a given file. if you want more than alter fn:last()-2 to fn:last() - x

How to trace MySql queries using MySql-Proxy?

I just downloaded the mysql-proxy and created this script lua (found in Mysql docs):
function read_query(packet)
if string.byte(packet) == proxy.COM_QUERY then
print("QUERY: " .. string.sub(packet, 2))
end
end
This is the command-line I'm using:
mysql-proxy -P localhost:1234 -b localhost:3306 --proxy-lua-script=profile.lua --plugins=proxy
When I run a simple query (like "select * from table1"), this error is reported: "failed: .\lua-scope.c:241: stat(C:...\profile.lua) failed: No error (0)"
Note: If I run mysql-proxy without lua script, no error occurs.
I need to install something to get mysql-proxy and query tracing working?
My environment is Windows 7 Professional x64.
Sorry the bad english.
The error you're getting is caused by --proxy-lua-script pointing to a file that mysql-proxy can't find. Either you've typed the name in wrong, you've typed the path in wrong, or you are expecting it in your CWD and it's not there. Or actually, looking at the entire error a little more closely, it seems possible that mysql-proxy itself sees the file in CWD itself OK, but one of the underlying modules doesn't like it (possibly because mysql-proxy changes the CWD somehow?)
Try saving profile.lua to the root of your C: drive and trying different versions of the option like so:
--proxy-lua-script=c:\profile.lua
--proxy-lua-script=\profile.lua
--proxy-lua-script=/profile.lua
One of those would probably work
simple query log lua script:
require("mysql.tokenizer")
local fh = io.open("/var/log/mysql/proxy.query.log", "a+")
fh:setvbuf('line',4096)
local the_query = "";
local seqno = 0;
function read_query( packet )
if string.byte(packet) == proxy.COM_QUERY then
seqno = seqno + 1
the_query = (string.gsub(string.gsub(string.sub(packet, 2), "%s%s*", ' '), "^%s*(.-)%s*$", "%1"))
fh:write(string.format("%s %09d %09d : %s (%s) -- %s\n",
os.date('%Y-%m-%d %H:%M:%S'),
proxy.connection.server.thread_id,
seqno,
proxy.connection.client.username,
proxy.connection.client.default_db,
the_query))
fh:flush()
return proxy.PROXY_SEND_QUERY
else
query = ""
end
end