how to import on Qpython - python-import

I'm a newbie to python trying to run this code on Qpython:
from sys import argv
script, first, second, third = argv
print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third is:", third
but the console keeps returning this value error: need more than one value to unpack.
Pls help..

You don't have enough arguments to unpack in this step
script, first, second, third = argv
try
script, = argv

Related

How to do this exercise using ios pythonista 3?

I am new to python, I am learning it through reading a book(Learn Python 3 the hard way). There is an exercise(exercise14)in the book can’t be run using iOS Pythonista 3.
The script is below:
from sys import argv
script, user_name = argv
prompt = '> '
print(f"Hi {user_name}, I'm the {script} script.")
print("I'd like to ask you a few questions.")
print(f"Do you like me {user_name}?")
likes = input(prompt)
print(f"Where do you live {user_name}?")
lives = input(prompt)
print("What kind of computer do you have?")
computer = input(prompt)
print(f"""
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer. Nice.
""")
But then I got an error:
File "/private/var/mobile/Containers/Shared/AppGroup/726BC931-58AE-44A6-9BE5-067EF23667A4/Pythonista3/Documents/Untitled.py", line 2, in <module>
script, user_name = argv
ValueError: not enough values to unpack (expected 2, got 1)
Please help me to solve this problem , I am very new.
Thank you guys!
If you long-press the run ("play") button on Pythonista, you can add the command-line argument that your script is expecting (user name, the script name it gets by default).

F# Parse command line arguments, expected incomplete structured construct at or before this point ; or ;;

I have a basic F# CLI that mimics F# touch command , my program takes a file name , creates the file if it exists or updates access time if it doesn't.
Core functionality is done, but it is supposed to mimic Linux' command Touch in terms of syntax. The syntax should be 'touch file1.text' for file creation or 'touch --version' for a message I want to print when option is given.
My core problems are:-
1. Having my code follow Linux' syntax -
Current - >>>Test.text = File created.
Expected - >>> touch test.txt or
touch --version = File created or touch command information printed.
Taking --version and --help or similar valid input and printed the corresponding information.
Current result
test.txt // File created
0 //
Expected
touch test.text //file created
0 //
or
Expected
touch --version / touch --help
/* Touch information
Touch information
Touch information
Touch information*/
Tried using the Argu library but seems out of my current scope, currently attempting with argument parser module.
// Learn more about F# at http://fsharp.org
open System
open System.IO
open System.Text
type filename = String
let touch path =
//Console.ReadLine()
if File.Exists path
then File.SetLastWriteTime(path, DateTime.Now)
else
if not(File.Exists path)
then File.WriteAllText(path, " ")
Console.ReadKey()|>ignore
[<EntryPoint>]
let main argv =
printfn "Touch Command - Built using F#"
printfn "Please enter the file you want to touch"
if argv |> Array.contains "Help " then
printfn "Display help here"
exit(0)
printfn "Version information"
exit(0)
for filename in argv do
touch(filename)
0
// return an integer exit code
```
Current result
> test.txt // File created
> 0 // internal
Expected
>touch test.text //file created
>0 //internal
or
Expected
>touch --version / touch --help
>/* Touch information
Touch information
Touch information
Touch information*/
No current errors
When argu library attempted, to many errors to bring to solution board.
argv is an array of strings, so the simplest way to tell if --help or --version is in that array is to use the Array.contains function:
[<EntryPoint>]
let main argv =
printfn "Argv: %A" argv // Helpful in debugging, remove in final version
if argv |> Array.contains "--help" then
printfn "Display help here"
exit(0)
if argv |> Array.contains "--version" then
printfn "Version information"
exit(0)
for filename in argv do
touch(filename)
Note that this example is assuming that you rewrite your touch function to take a filename parameter instead of reading the filename from the console via Console.ReadLine.
Also note that after an attribute like [<EntryPoint>], there's no need to add a second level of indentation. The attribute should be indented at the same level as the let declaration that it's modifying. So you should write your let main argv = line at the top level of your module (i.e., with zero indentation).

dxl CreateProcess failed for system cmd instruction

I want to call run a file called csvplot.vbs (from this site) to turn a .csv file I have written using dxl (has 5 columns, each with a heading and then just numerical data) into a graph (stored as .png).
I have run the following instruction directly through cmd with success:
#echo off
cscript //nologo C:\Users\Administrator\csvplot.vbs C:\PROGRA~1\IBM\Rational\DOORS\9.6\lib\dxl\addins\Verification\Statistics\statGenTest_Top_Level.csv C:\PROGRA~1\IBM\Rational\DOORS\9.6\lib\dxl\addins\Verification\Statistics\statGenTest_Top_Level.png 800 600 1 3 1 4 1 5
pause
This produces the desired .png file.
What I want, however, is to be able to execute this through DOORS, so that whenever the script that generates the raw data is run, it also produces a graph.
What I have is this as my test case:
string echostr = "#echo off"
string commands = "cscript //nologo C:\\Users\\Administrator\\csvplot.vbs C:\\PROGRA~1\\IBM\\Rational\\DOORS\\9.6\\lib\\dxl\\addins\\Verification\\Statistics\\statGenTest_Top_Level.csv C:\\PROGRA~1\\IBM\\Rational\\DOORS\\9.6\\lib\\dxl\\addins\\Verification\\Statistics\\statGenTest_Top_Level.png 800 600 1 3 1 4 1 5"
system("cmd /c start #echo off") // doesn't recognise echo command
system("cmd /c start " commands "")
I get an error:
"Windows cannot find '#echo'. Make sure you typed the name correctly,
and then try again."
I am at a loss on how to get the script to run though cmd from dxl, and I would appreciate any help. I've only had one previous foray into system() prompts through dxl, and it was only to open a .pdf. In the meantime I will keep trying to work this out. Please let me know if I can provide any further information.
Edit: Further Information
#echo: I removed the # to see how it operates, it brings up a blank
cmd window and performs no further action. In order to even run the things in the points below, I left the # off.
I deleted "/c start" from the second system() line: this opens one command line with the usual white text at the top, and a second over the top that is completely blank.
I changed the first line as follows, and commented out the second:
system("cmd /c start echo off" "\n" commands "")
--- this got a similar result to the second dot-point, but only with one cmd window, the black (no text one)
If I don't include the "\n" marker then I get a cmd window with text of "off" commands (where commands is the defined string above).
If I only have the system("cmd /c start " commands "") line, and not the echo line, then a cmd window briefly flashes and disappears and no further results demonstrating the success of the script appear.
So my issue is this: I know this script works when run directly through command line, the problem I have is that I cannot now run it through dxl.
I have developed a solid work-around that does exactly what I need.
The issue was that the input I had dxl writing was not going through command line correctly.
Knowing that the script ran from cmd correctly and, in turn, that the script executed from a batch file correctly, and that I could run the batch file from dxl, my solution was as follows:
Define the paths in dxl using the format C:\PROGRA~1\PATHNAME\
Using the Stream write() command to write the instructions directly
to a .bat file
Then using the system() command to run the .bat file
I have included some of my code, so that maybe it might help someone attempting to do the same thing. (I'll gladly take any advice on better programming conventions.)
// functions used: genFileName(), assume if a variable is not declared here, it was declared under my globals
// genFileName() returns a string of the file name, replacing any " " with "_" so cmd doesn't cry when I run it
string basename = genFileName()
string fcsv = basename ".csv"
string csvPath = "blahblahthefilepath" fcsv
if(fileExists_(csvPath)) isFile = true
Stream fOut = append(csvPath)
// === if file does not exist, create, give column names
if( !isFile){
fOut << "Date and Time,count1,count2,count3,count4" "\n"
}
else ack ("File name exists, append stats to file?" // may not be necessary
// === print to file ===
fOut << datetime "," ctot "," ctc "," cti "," ctnc "\n"
// ===== Create Batch file to run grapher ===
string columnsToPlot = "1 3 1 4 1 5" // ==> may develop this to allow user to choose
string graphDim = "800 600" // ==> px dim, may develop for user choice
string fbat = basename ".bat"
string batPath = "blahblahthefilepath"
Stream batOut = write(batPath fbat)
batOut << "#echo off" "\n"
batOut << "title Batch file to plot statistics for " fcsv "\n"
batOut << "cscript //nologo " batPath "csvplot.vbs " batPath fcsv " " batPath basename ".png " graphDim " " columnsToPlot ""
system("cmd /c start " batPath fbat "")
// some infoBox feedback DB to tell the user that the files were created
Good luck to anyone else who is attempting something similar, and I hope this is of use to someone.
Does running the dxl script without the # in front of the echo command work?

Hello world won't compile with "The value or constructor is not defined"

let prefix prefixString baseString =
prefixString + " " + baseString
prefix "Hello" "World"
With the code above I'm getting the error Stuff.fs(34,1): error FS0039: The value or constructor 'prefix' is not defined.
I'm not entirely sure why this is happening, as I'm watching a video series on F# in which literally the same code is being compiled and ran successfully. Is there something wrong with my environment?
In the comment, you mentioned that you are running the snippet using "run selection" command. This command runs the selected piece of code in F# Interactive which initially contains no definitions. So, if you select and run just the last line, you will get:
> prefix "Hello" "World";;
stdin(1,1): error FS0039: The value or constructor 'prefix' is not defined
This is because F# Interactive does not know what the definition of prefix is - it does not look for it automatically in your file. You can fix this by selecting everything and running all code in a single interaction, or you can first run the definition and then the last line, i.e.:
> let prefix prefixString baseString =
prefixString + " " + baseString;;
val prefix : prefixString:string -> baseString:string -> string
> prefix "Hello" "World";;
val it : string = "Hello World"
Note that when you run the first command, F# Interactive will print the type of the defined functions, so you can see what has just been defined.
The fact that F# Interactive has its own separate "state of the world" is quite important, as it also means that you need to re-run functions after you change them so that subsequent commands use the new definition.

Getting return status AND program output

I need to use Lua to run a binary program that may write something in its stdout and also returns a status code (also known as "exit status").
I searched the web and couldn't find something that does what I need. However I found out that in Lua:
os.execute() returns the status code
io.popen() returns a file handler that can be used to read process output
However I need both. Writing a wrapper function that runs both functions behind the scene is not an option because of process overhead and possibly changes in result on consecutive runs. I need to write a function like this:
function run(binpath)
...
return output,exitcode
end
Does anyone has an idea how this problem can be solved?
PS. the target system rung Linux.
With Lua 5.2 I can do the following and it works
-- This will open the file
local file = io.popen('dmesg')
-- This will read all of the output, as always
local output = file:read('*all')
-- This will get a table with some return stuff
-- rc[1] will be true, false or nil
-- rc[3] will be the signal
local rc = {file:close()}
I hope this helps!
I can't use Lua 5.2, I use this helper function.
function execute_command(command)
local tmpfile = '/tmp/lua_execute_tmp_file'
local exit = os.execute(command .. ' > ' .. tmpfile .. ' 2> ' .. tmpfile .. '.err')
local stdout_file = io.open(tmpfile)
local stdout = stdout_file:read("*all")
local stderr_file = io.open(tmpfile .. '.err')
local stderr = stderr_file:read("*all")
stdout_file:close()
stderr_file:close()
return exit, stdout, stderr
end
This is how I do it.
local process = io.popen('command; echo $?') -- echo return code of last run command
local lastline
for line in process:lines() do
lastline = line
end
print(lastline) -- the return code is the last line of output
If the last line has fixed length you can read it directly using file:seek("end", -offset), offset should be the length of the last line in bytes.
This functionality is provided in C by pclose.
Upon successful return, pclose() shall return the termination status
of the command language interpreter.
The interpreter returns the termination status of its child.
But Lua doesn't do this right (io.close always returns true). I haven't dug into these threads but some people are complaining about this brain damage.
http://lua-users.org/lists/lua-l/2004-05/msg00005.html
http://lua-users.org/lists/lua-l/2011-02/msg00387.html
If you're running this code on Win32 or in a POSIX environment, you could try this Lua extension: http://code.google.com/p/lua-ex-api/
Alternatively, you could write a small shell script (assuming bash or similar is available) that:
executes the correct executable, capturing the exit code into a shell variable,
prints a newline and terminal character/string onto standard out
prints the shell variables value (the exit code) onto standard out
Then, capture all the output of io.popen and parse backward.
Full disclosure: I'm not a Lua developer.
yes , your are right that os.execute() has returns and it's very simple if you understand how to run your command with and with out lua
you also may want to know how many variables it returns , and it might take a while , but i think you can try
local a, b, c, d, e=os.execute(-what ever your command is-)
for my example a is an first returned argument , b is the second returned argument , and etc.. i think i answered your question right, based off of what you are asking.

Resources