How to send a command and get the result ? API Python/C - PyRun_String - python-c-api

I would like to send a command to the interpreter and get the return of this command.
I'm actually using PyRun_String and it's working greatly but I have to pass in args Py_single_input if I want to init a variable like a = 10then I get none.
If I want to send a I have to put Py_eval_input in args then I get 10.
So my problem is how I can merge Py_single_input and Py_eval_input to use this both fonction in a same function.
or
Is there another function other than Pyrun_String?

Related

Powershell error 'no method op_addition' when trying to use RowFilter with Variables

I have a small GUI containing a DataGridView that uses a DataTable as a datasource and want to add a Filterfunction. For this I made a dropdown that lets you choose a column and enter your filter next to it. Then when you press the Filterbutton it calls a function that should apply a RowFilter with the selected parameters. The problem is that I can't get the RowFilter Syntax to work. Either I get a Syntaxerror or Method invocation failed because [System.Data.DataColumn] doesn't contain a method named 'op_Addition'.
This is the function I'm using:
function DG-Filter {
$script:AWtable1.DefaultView.RowFilter = "$script:txtFilterListe.SelectedItem LIKE '%$script:txtFilter.Text%'"
$script:dataGridView1.Refresh()
}
What am I doing wrong? Do I have to use something like the String.Format() Method?
Full Script: https://controlc.com/436266af

Transform selector to UUID array

I need to add vanilla selectors (as #a or #p, but I also want to add there selector arguments as #a[team=blue]) to my plugin command. Is there some library or even some build-in function in bukkit? I tried executing command, that returns UUID or name, but I had problem with reading its result.
I do not really understand what you want, but if you want to return Players UUID just do it like that:
player.getUniqueID();
and thats it. You can return this to a player by making a command and sending him a message with:
player.sendMessage(player.getUniqueID.toString());

Powershell script queues results of an if statement in a do while in a function

I'm using a function that I call from another script. It prompts a user for input until it gets back something that is not empty or null.
function GetUserInputValue($InputValue)
{
do{
$UserValue = Read-Host -Prompt $InputValue
if (!$UserValue) { $InputValue + ' cannot be empty' }
}while(!$UserValue)
$UserValue
return $UserValue
}
The issue is quite strange and likely a result of my lack of powershell experience. When I run the code and provide empty results, the messages from the if statement queue up and only display when I finally provide a valid input. See my console output below.
Console Results
test:
test:
test:
test:
test:
test:
test: 1
test cannot be empty
test cannot be empty
test cannot be empty
test cannot be empty
test cannot be empty
test cannot be empty
1
I can make this work however in the main file with hard coded values.
do{
$Server = Read-Host -Prompt 'Server'
if (!$Server) { 'Server cannot be empty' }
}while(!$Server)
I'm working Visual Studio Code. This is a function I have in another file I've named functions.ps1.
I call this from my main file like this,
$test = GetUserInputValue("test")
$test
When you put a naked value in a script like "here's a message" or 5 or even a variable by itself $PID what you're implicitly doing is calling Write-Output against that value.
That returns the object to the pipeline, and it gets added to the objects that that returns. So in a function, it's the return value of the function, in a ForEach-Object block it's the return value of the block, etc. This bubbles all the back up the stack / pipeline.
When it has nowhere higher to go, the host handles it.
The console host (powershell.exe) or ISE host (powershell_ise.exe) handle this by displaying the object on the console; this just happens to be the way they handle it. Another host (a custom C# application for example can host the powershell runtime) might handle it differently.
So what's happening here is that you are returning the message that you want to display, as part of the return value of your function, which is not what you want.
Instead, you should use Write-Host, as this writes directly to the host, skipping the pipeline. This is the correct command to use when you want to display a message to the user that must be shown (for other information you can use different commands like Write-Verbose, Write-Warning, Write-Error, etc.).
Doing this will give you the correct result, and prevent your informational message from being part of the return value of your function.
Speaking of which, you are returning the value twice. You don't need to do:
$UserValue
return $UserValue
The first one returns the value anyway (see the top of this answer); the second one does the same thing except that it returns immediately. Since it's at the end of the function anyway, you can use wither one, but only use one.
One more note: do not call PowerShell functions with parentheses:
$test = GetUserInputValue("test")
This works only because the function has a single parameter. If it had multiple params and you attempted to call it like a method (with parentheses and commas) it would not work correctly. You should separate arguments with spaces, and you should usually call parameters by name:
$test = GetUserInputValue "test"
# better:
$test = GetUserInputValue -InputValue "test"

pytest function without return value

I'm trying to do a pytest on a function without return value, but obviously value is None in pytets. I was wondering if there is a solution for that?
here is function which I'm trying to test:
def print_top_movies_url():
for item in movie_link[:100]:
print item.contents[1]
The best thing to do would be to separate getting the top movies and printing them.
For example, you could have a top_movie_urls which looks like this:
def top_movie_urls():
urls = []
for item in movie_link[:100]:
urls.append(item.contents[1])
return urls
(or make it a generator function)
That's easy to test, and wherever you call it, you can now just do something like print('\n'.join(top_movie_urls())).
If you really want to test the output instead, you can use pytest's output capturing to access the output of the tested function and check that.

Getting two parameters from c function in matlab

i had a code that c send back 1 number (mex)
the matlab code was
vMsg=unit32(Gateway_test_app(2))
now i added 1 more return value to Gateway_test_app(2) which is s STRING
what i need to do to get the two values back
i was thinking about something like this:
[vMsg,errMsg]=??????(Gateway_test_app(2))
what should i put in the ????? place?
thx for any help
johnny.
ps
using codegen and need not to get err when building
First call the function and store the two outputs, then run your extra function unit32 (what does it do, by the way?) on the first output only:
[vMsgOriginal, errMsg] = Gateway_test_app(2);
vMsg = unit32(vMsgOriginal);
This assumes that you don't want to process your new string output through your unit32 function.