Checking word inside a string in Coregraphe - python

I have to check with a Python box if a word I've told to Pepper (saved externally from a dialog box) is inside a list (created as string,and saved into ALMemory from SSH in Matlab), and do something if yes or not.
How can I do this?
def onInput_onStart(self):
#self.onStopped() #activate the output of the box
picklist = ALProxy("ALMemory")
list=picklist.getData("myFood")
def food(self):
if food in list:
tts=ALProxy("ALDialog")
tts.say("Available")

I personally would just manage it on the web using js, when it comes to this kind of stuff boxes give more trouble than it's worth. Raise an event with the string you want and check if the word is inside the list. After that you can either use the tts (as you seem to be trying to do) or raise and event (sending the true/false as a parameter) and use it to trigger whatever you want.
Javascript:
session = null
QiSession(connected, disconnected, location.host);
tts = null;
function connected(s) {
console.log("Session connected");
session = s;
startSubscribe();
session.service("ALTextToSpeech").then(function (t) {
tts = t;
});
}
function disconnected(error) {
console.log("Session disconnected");
}
function startSubscribe() {
session.service("ALMemory").then(function (memory) {
memory.subscriber("toTablet").then(function (subscriber) {
subscriber.signal.connect(functionThatChecks)
});
});
}
function functionThatChecks(word)
{
tts.stopAll();
/*Check if exists*/
tts.say("It exists"); //Or raise an event
}
Dialog
u: (word) $eventName="word"
Choregraphe

You need to use self.list before other functions can access it.
You also need to pass users_food to the function when you call food().
Assuming that list is a list of strings, and users_food is a string.
def onInput_onStart(self):
#self.onStopped() #activate the output of the box
picklist = ALProxy("ALMemory")
self.list=picklist.getData("myFood")
def food(self, users_food):
if users_food in self.list:
tts=ALProxy("ALDialog")
tts.say("Available")

Related

Running a python script in C# after publishing C# project [duplicate]

I have been working on a problem for a while now which I cannot seem to resolve so I need some help! The problem is that I am writing a program in C# but I require a function from a Python file I created. This in itself is no problem:
...Usual Stuff
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
namespace Program
{
public partial class Form1 : Form
{
Microsoft.Scripting.Hosting.ScriptEngine py;
Microsoft.Scripting.Hosting.ScriptScope s;
public Form1()
{
InitializeComponent();
py = Python.CreateEngine(); // allow us to run ironpython programs
s = py.CreateScope(); // you need this to get the variables
}
private void doPython()
{
//Step 1:
//Creating a new script runtime
var ironPythonRuntime = Python.CreateRuntime();
//Step 2:
//Load the Iron Python file/script into the memory
//Should be resolve at runtime
dynamic loadIPython = ironPythonRuntime.;
//Step 3:
//Invoke the method and print the result
double n = loadIPython.add(100, 200);
numericUpDown1.Value = (decimal)n;
}
}
}
However, this requires for the file 'first.py' to be wherever the program is once compiled. So if I wanted to share my program I would have to send both the executable and the python files which is very inconvenient. One way I thought to resolve this is by adding the 'first.py' file to the resources and running from there... but I don't know how to do this or even if it is possible.
Naturally the above code will not work for this as .UseFile method takes string arguments not byte[]. Does anyone know how I may progress?
Lets start with the simplest thing that could possibly work, you've got some code that looks a little like the following:
// ...
py = Python.CreateEngine(); // allow us to run ironpython programs
s = py.CreateScope(); // you need this to get the variables
var ironPythonRuntime = Python.CreateRuntime();
var x = py.CreateScriptSourceFromFile("SomeCode.py");
x.Execute(s);
var myFoo = s.GetVariable("myFoo");
var n = (double)myFoo.add(100, 200);
// ...
and we'd like to replace the line var x = py.CreateScriptSourceFromFile(... with something else; If we could get the embedded resource as a string, we could use ScriptingEngine.CreateScriptSourceFromString().
Cribbing this fine answer, we can get something that looks a bit like this:
string pySrc;
var resourceName = "ConsoleApplication1.SomeCode.py";
using (var stream = System.Reflection.Assembly.GetExecutingAssembly()
.GetManifestResourceStream(resourceName))
using (var reader = new System.IO.StreamReader(stream))
{
pySrc = reader.ReadToEnd();
}
var x = py.CreateScriptSourceFromString(pySrc);

Testing a Pipeline containing ValueProviders

I created a pipeline with ValueProviders in order to use it as a template. However, I can't figure out how to use ValueProviders when testing the Pipeline. I can't just use values directly to test because my PTransforms are waiting for ValueProviders.
I do not know about Python, but in Java you may use StaticValue provider.
E.g. if you have the following interface for options:
interface BaseOptions extends DataflowPipelineOptions {
void setSource(ValueProvider<String> source);
ValueProvider<String> getSource();
}
Then you may use ValueProvider.StaticValueProvider.of(...) to initialise your parameter. Something like this:
BaseOptions options = PipelineOptionsFactory.fromArgs(args).as(BaseOptions.class);
options.setSource(ValueProvider.StaticValueProvider.of("/path/to/file"));
Pipeline p = Pipeline.create(options);
p.apply(TextIO.read().from(options.getSource()))
.apply("just print",
new ParDo().of(new DoFn<String, String>() {
#ProcessElement
public void processElement(ProcessContext c) {
System.out.println(c.element());
}
}));
p.run();
I put default values for the Value Providers in my pipeline opions :
class MypipelineOptions(PipelineOptions):
#classmethod
def _add_argparse_args(cls,parser):
parser.add_value_provider_argument('--variable',
type=float,
dest='variable',
default=5)
So that when I call MyPipelineOptions in the test file, it automatically uses the default values.

how to respond back to Python from an NSNotification?

I'm calling into Obj from Python using this code:
print "Login begin";
nc = Foundation.NSDistributedNotificationCenter.defaultCenter();
userInfo = NSDictionary.dictionaryWithObjectsAndKeys_("7","operation",user,"username",password,"password",None);
nc.postNotificationName_object_userInfo_deliverImmediately_(SIMULATOR_NOTIFICATION,"",userInfo,1);
return;
The ObjC receives it thusly:
- (void) recievedNotification:(NSNotification *) notification
{
NSDictionary *userInfo = [notification userInfo];
NSControl *postingObject = [notification object]; // the object that posted the notification
NSMutableDictionary *response = [NSMutableDictionary dictionaryWithCapacity:1];
int switcher = [[userInfo objectForKey:#"operation"] intValue];
switch (switcher) {
My question is: how do I return a value (such as a success/failure boolean) back to my Python code?
BTW it's not my code, and yes I can see various problems with it, but that dev left and now I've been asked to update it. You know how it is.

Regex to capture the codes between specific function

I have been experimenting with python re, trying to capture specific variables between specific functions. To illustrate let me give an example of file contents of a php file :-
public function executeSomething ()
{
$this->title = 'Edit something';
$this->action = 'Edit';
$this->headerTitle = 'Edit something';
return true;
}
public function executeSomethingEdit ()
{
if (strlen ($this->somethingElse) > 0)
{
$this->titleText = "Update";
$title = 'Edit something';
}
else
{
$this->titleText = "Create";
$title = 'Create something';
}
$this->title = $title;
$this->headerTitle = $title;
$this->formTitle = 'Something details'
return true;
}
What the python script needs to do now is iterate through this file in search for functions that starts with 'public function execute' and get the content within the braces i.e { }. I have already came up with python code to achieve this i.e :-
r = re.compile(r"public function execute(.*?)\(\).*?{(.*?)}", re.DOTALL)
The problem occurs when I have a validation within the function i.e if else statement such as the one in the function executeSomethingEdit. The script doesn't takes into account whatever codes below the if statements closing braces '}'. Therefore I need to further enhance the python code to include the function declaration below i.e something like this :-
r = re.compile(r"public function execute(.*?)\(\).*?{(.*?)}.*?public function", re.DOTALL)
At the moment this code is not working/producing the result that I wanted. I need to used python's re specifically because i need to further analyse the content of {(.*?)}. I'm very new to python so I hope someone could direct me in the right direction or at least tell me what I'm doing wrong. Thanks in advance.
If the input PHP has no bugs and has consistent indentation, you could check for a non-space character before the closing brace.
r = re.compile(r'public function execute(.*?)\(\).*?{(.*?)[^ ]}', re.DOTALL)

Get the title of the current active Window/Document in Mac OS X

Refering to a previously asked question, I would like to know how to get the title of the current active document.
I tried the script mention in the answers to the question above. This works, but only gives me the name of the application. For example, I am writing this question: When I fire up the script it gives me the name of the application, i.e. "Firefox". This is pretty neat, but does not really help. I would rather like to capture the title of my current active document. See the image.
Firefox title http://img.skitch.com/20090126-nq2egknhjr928d1s74i9xixckf.jpg
I am using Leopard, so no backward compatibility needed. Also I am using Python's Appkit to gain access to the NSWorkspace class, but if you tell me the Objective-C code, I could figure out the translation to Python.
Ok, I've got a solution which is not very satisfing, thats why I don't mark Koen Bok's answer. At least not yet.
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
set window_name to name of front window
end if
end tell
Save as script and invoke it with osascript from the shell.
As far as I know your best bet is wrapping an AppleScript. But AppleScript is magic to me so I leave it as an exercise for the questioner :-)
This might help a little: A script to resize frontmost two windows to fill screen - Mac OS X Hints
In Objective-C, the short answer, using a little Cocoa and mostly the Carbon Accessibility API is:
// Get the process ID of the frontmost application.
NSRunningApplication* app = [[NSWorkspace sharedWorkspace]
frontmostApplication];
pid_t pid = [app processIdentifier];
// See if we have accessibility permissions, and if not, prompt the user to
// visit System Preferences.
NSDictionary *options = #{(id)kAXTrustedCheckOptionPrompt: #YES};
Boolean appHasPermission = AXIsProcessTrustedWithOptions(
(__bridge CFDictionaryRef)options);
if (!appHasPermission) {
return; // we don't have accessibility permissions
// Get the accessibility element corresponding to the frontmost application.
AXUIElementRef appElem = AXUIElementCreateApplication(pid);
if (!appElem) {
return;
}
// Get the accessibility element corresponding to the frontmost window
// of the frontmost application.
AXUIElementRef window = NULL;
if (AXUIElementCopyAttributeValue(appElem,
kAXFocusedWindowAttribute, (CFTypeRef*)&window) != kAXErrorSuccess) {
CFRelease(appElem);
return;
}
// Finally, get the title of the frontmost window.
CFStringRef title = NULL;
AXError result = AXUIElementCopyAttributeValue(window, kAXTitleAttribute,
(CFTypeRef*)&title);
// At this point, we don't need window and appElem anymore.
CFRelease(window);
CFRelease(appElem);
if (result != kAXErrorSuccess) {
// Failed to get the window title.
return;
}
// Success! Now, do something with the title, e.g. copy it somewhere.
// Once we're done with the title, release it.
CFRelease(title);
Alternatively, it may be simpler to use the CGWindow API, as alluded to in this StackOverflow answer.
refered to https://stackoverflow.com/a/23451568/11185460
package main
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
int
GetFrontMostAppPid(void){
NSRunningApplication* app = [[NSWorkspace sharedWorkspace]
frontmostApplication];
pid_t pid = [app processIdentifier];
return pid;
}
CFStringRef
GetAppTitle(pid_t pid) {
CFStringRef title = NULL;
// Get the process ID of the frontmost application.
// NSRunningApplication* app = [[NSWorkspace sharedWorkspace]
// frontmostApplication];
// pid_t pid = [app processIdentifier];
// See if we have accessibility permissions, and if not, prompt the user to
// visit System Preferences.
NSDictionary *options = #{(id)kAXTrustedCheckOptionPrompt: #YES};
Boolean appHasPermission = AXIsProcessTrustedWithOptions(
(__bridge CFDictionaryRef)options);
if (!appHasPermission) {
return title; // we don't have accessibility permissions
}
// Get the accessibility element corresponding to the frontmost application.
AXUIElementRef appElem = AXUIElementCreateApplication(pid);
if (!appElem) {
return title;
}
// Get the accessibility element corresponding to the frontmost window
// of the frontmost application.
AXUIElementRef window = NULL;
if (AXUIElementCopyAttributeValue(appElem,
kAXFocusedWindowAttribute, (CFTypeRef*)&window) != kAXErrorSuccess) {
CFRelease(appElem);
return title;
}
// Finally, get the title of the frontmost window.
AXError result = AXUIElementCopyAttributeValue(window, kAXTitleAttribute,
(CFTypeRef*)&title);
// At this point, we don't need window and appElem anymore.
CFRelease(window);
CFRelease(appElem);
if (result != kAXErrorSuccess) {
// Failed to get the window title.
return title;
}
// Success! Now, do something with the title, e.g. copy it somewhere.
// Once we're done with the title, release it.
CFRelease(title);
return title;
}
static inline CFIndex cfstring_utf8_length(CFStringRef str, CFIndex *need) {
CFIndex n, usedBufLen;
CFRange rng = CFRangeMake(0, CFStringGetLength(str));
return CFStringGetBytes(str, rng, kCFStringEncodingUTF8, 0, 0, NULL, 0, need);
}
*/
import "C"
import (
"github.com/shirou/gopsutil/v3/process"
"reflect"
"unsafe"
)
//import "github.com/shirou/gopsutil/v3/process"
func cfstringGo(cfs C.CFStringRef) string {
var usedBufLen C.CFIndex
n := C.cfstring_utf8_length(cfs, &usedBufLen)
if n <= 0 {
return ""
}
rng := C.CFRange{location: C.CFIndex(0), length: n}
buf := make([]byte, int(usedBufLen))
bufp := unsafe.Pointer(&buf[0])
C.CFStringGetBytes(cfs, rng, C.kCFStringEncodingUTF8, 0, 0, (*C.UInt8)(bufp), C.CFIndex(len(buf)), &usedBufLen)
sh := &reflect.StringHeader{
Data: uintptr(bufp),
Len: int(usedBufLen),
}
return *(*string)(unsafe.Pointer(sh))
}
func main() {
pid := C.GetFrontMostAppPid()
ps, _ := process.NewProcess(int32(pid))
title_ref := C.CFStringRef(C.GetAppTitle(pid))
println(pid) // pid
println(ps.Name()) // process name
println(cfstringGo(title_ref)) // active window title
}
I then found this property wont change after it is called.
By this, only after we implement NSWorkspaceDidActivateApplicationNotification, we can monitor the change of activity window. But I didn't find any solution which can implement NSWorkspaceDidActivateApplicationNotification in golang.
A workaround method is compile one go program and call it by another go program. I then try full Objective-C code in here

Categories

Resources