How to access the "control" library for microbit in the micropython editor? - micropython

Trying to use the "On event from" functionality to get finer control over input from button presses. When I convert this blocks code to python in the makecode editor, I get the following code:
def on_microbit_id_button_a_evt_click():
basic.show_leds("""
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
""")
control.on_event(EventBusSource.MICROBIT_ID_BUTTON_A,
EventBusValue.MICROBIT_BUTTON_EVT_CLICK,
on_microbit_id_button_a_evt_click)
What I don't understand is how to get this control library in the python.microbit.org editor, as there doesn't seem to be any import statement in the converted code, and the from microbit import * doesn't seem to include it.
Sorry if this is a stupid question and thanks in advance!

Related

Combine 'if' and 'if not' in perl

I have got the following filter inside my httpd.conf:
ExtFilterDefine jsonfilter mode=output intype=application/json cmd="/usr/bin/perl -pe 's|^|qq(\,\") . valid . qq(\"\: ) . qq(\") . time() . \\x0D . qq(\") . qq(\\n)|e if ($==eof) && unless (-f q{/tmp/md5_filter.tmp})'"
But the way how I used the && operater is not valid. I receive no output if I request the file. The filter should only run if the md5_filter.tmp file doesn't exists and s command should only add the timestamp at the end of file (eof). Does somebody know what's wrong with my code?
There are a lot of strange things about your code.
/usr/bin/perl -pe 's|^|qq(\,\") . valid . qq(\"\: ) . qq(\") . time() . \\x0D . qq(\") . qq(\\n)|e if ($==eof) && unless (-f q{/tmp/md5_filter.tmp})'
perl -pe does not actually change the file. Not sure if that is your intent. If you do want to change the file, you need to add the -i switch. Note that this will alter your file each time you run it.
s| You have changed the delimiter of the substitution operator from / to |. This is usually done when you need to use slash / inside the pattern, and you want to avoid having to escape it \/. You however do not have slashes in your pattern, which makes this change unnecessary.
^ This denotes the start of a string. Your substitution will add from the beginning of the string. Since you want to add to the end of the file, I am not sure how you are thinking there.
qq(\,\") . valid . qq(\"\: ) . qq(\") . time() . \\x0D . qq(\") . qq(\\n) You seem to be under the impression that the concatenation operator . does something special. This statement can be written qq(,\"valid\": \") . time() . qq(\x0D\"\n) Probably. The quoting and escaping is somewhat tricky to figure out. (No need to escape , and :)
if ($==eof) is a weirdness beyond words. It means $= = eof, which assigns the return value of the eof() test to the predefined $= variable (The current page length). I feel pretty sure you didn't mean that, and only meant to check if (eof).
if ($==eof) && unless (-f q{/tmp/md5_filter.tmp}) -- This is actually two separate statements, and you cannot use post-fix if twice in a row. Nor do you need to. You would chain them into one, like this: if ( (eof) && (not -f q{...}) ).
What do we get if we combine all these corrections? Well, we get this:
/usr/bin/perl -pe' s/^/qq(,\"valid\": \") . time() . qq(\x0D\"\n)/e if ( (eof) && (not -f q{/tmp/md5_filter.tmp}) )'
But.... what are you doing with this statement? For each line in the file, you are checking if it is the end of the file. And if it is, you check for the existence of a file, and then add to the last line, from the start of the line, a new string.
I think what you want to do is wait until the last line of the file, and then print a line after it. You don't need a substitution to print, you just print. So we remove the substitution and replace it with a print. Since we want to print after the last line, we use an END block to execute code at the end.
/usr/bin/perl -pe 'END { unless (-f q{/tmp/md5_filter.tmp}) { print qq(\,\"valid\"\: \") . time() . qq(\x0D\"\n) }'
The printed string may need to start with a newline qq(\n..., if the last line of the file does not end with a newline. If, for some reason, you actually meant to write the string at the beginning of the line, things get more complicated. Then you're probably better off using the first command.

Can't call method "import" without a package or object reference

My Perl CGI program uses the import function, I am calling a .pm file.
In the following code, when $projectID is a string—for example "try"—there are no import errors. But if it is numeric—e.g. "0689"—then the following error appears
Can't call method "import" without a package or object reference at aa.cgi line 993.
my $projectID = "0689"
my $gTable = "vg" . $tm . ".pm";
my $new_vPath = $hconfig::usersPNG . $vnm;
my $gen_listPath = $usersTMP . $gTable;
if ( -e $new_venPath && -e $gen_listPath ) {
require $projectID . '/' . $gTable;
import $projectID . '/' . $gTable; # try2/vg77.pm -> no error # 0689/vg76.pm -> import error
...
}
What is going on here is that import is not a built-in Perl function. Instead, it is a method that use typically calls on a package after it is loaded - you seem to be trying to call it on a filename, which won't work.
I guess you are trying to dynamically load a package based on the contents of those variables. Here's one way of several to do that:
my $filename = '/path/to/Package/Name.pm';
my $packagename = 'Package::Name';
require $filename;
$packagename->import();
One thing to keep in mind about loading modules dynamically like this is that, unlike modules loaded by use, Perl won't know about the functions exported from those packages at compile time. The consequence is that you can't omit parentheses from calls to those functions.
So for example: If you have a module Foo that exports a function foo, you could say: use Foo; foo "bar";. But if you instead load Foo with the above code, you have to say foo("bar");.
By the way, I doubt that 0689::vg76 is the name of one of your packages. If you could explain more about what you're trying to load, i.e. what the files 0689/vg76.pm look like and what you're loading from them (like, do they actually have a sub import?), perhaps we could suggest a more fitting method of loading them.
The reason that import $projectID . '/' . $gTable; is still valid syntax is that Perl is interpreting it as Indirect Object Syntax, wherein method_name $object #parameters; is taken as $object->method_name(#parameters);.
The indirect object syntax is mostly used when printing to a filehandle, as in print $filehandle $output, but other than that, its use is discouraged, one reason being the confusing syntax issues you are observing here.
You can use use B::Deparse to see how Perl interprets your source code:
$ perl -MO=Deparse,-p -e 'import $projectID . "/" . $gTable;'
(($projectID->import . '/') . $gTable);
The reason you are getting that error message is that, on Perl releases before v5.18 (references: bug, commit, delta), the string on the left of the -> was required to be a known name, or start with an alphanumeric character. (And even though as of v5.18, you can theoretically say "0689/vg76"->import, that's almost certainly not what you want, as I explained above.)

Typo3 - record history weird behaviour

I am facing a weird problem which I've never seen before in typo3 version 6.0,
but right now I have to use typo3 4.5.29.
There's something wrong with the "record history", when I try to display change history of a page content, this is what I see:
Normally in the "Differences" column I would see the changes in green colored text and
the old values which were removed in red colored text, but I see some kind of number
which I don't even understand the meaning...
Anyone is facing the same thing ?
Thank you very much for your help.
Cindy
TYPO3 uses external software called "diff" for creating a coloured view of the difference. Have a look at t3lib/class.t3lib_diff.php for implementation details.
I guess $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'] is set wrong or diff is not available.
untested:
If you cannot ask the admin of your server, create an php-file somehow like this for testing purpose:
<?php
$GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'] = '/usr/bin/'; // do not know your system
$file1 = '';
$file2 = '';
$cmd = $GLOBALS['TYPO3_CONF_VARS']['BE']['diff_path'] . ' ' . $file1 . ' ' . $file2;
$res = array();
echo exec($cmd, $res, $returnValue)
echo $res;
?>
This file should output something like
diff: missing operand

Concatenating strings to a path

I'm having some trouble with concatenation on strings. I'm tring to create a path that will become a .txt file, but it always ends up as just ".txt" with no name. It ends up in the right fikder though.
This is what I'm doing:
open TEXT, ">/home/admin/www/build/logs/baseline".$ID."/".$platformName.".txt" or die $!;
So I want to create the file.
"/home/admin/www/build/logs/baseline45/linux.txt"
Where am I messing this up?
Thanks!
You are not messing up in the snippet provided, there are several ways of forming a string including values from variables and you are using one of them (the dot-operator) correctly.
Try checking so that $platformType really contains what you think it, and unless you haven't already; turn warnings (and preferrably strict mode) on.
Turning warnings/strict mode on might give you details of undefined variables which would be helpful in situations such as this (ie. Is $platformType really the name of the variable you are looking for?)
use warnings;
use strict;
Print the value of $platformType before trying to open the file and you will find that it is indeed an empty string (or just containing something weird that would explain the results you are getting).
Step 1:
open TEXT, '>/home/admin/www/build/logs/baseline/' . $ID . '/' . $platformName . '.txt';
Step 2:
open TEXT, ">/home/admin/www/build/logs/baseline/" . $ID . "/" . $platformName . ".txt";
I think like this..........

Print the return of a method

I know in php I can do something like this
echo "{$this->method}";
and I swear there was a way to do it in perl
Update:
What I am trying to do is print a scalar that the method returns. I was kind of hoping of doing within the string like in php, just because I'm lazy :P.
Are you just trying to evaluate an arbitrary expression inside a double quoted string? Then maybe you're thinking of
print "#{[$this->method]}";
There is also a trick to call the method in scalar context, but the syntax is a little less clean.
print "${\($this->method)}";
Well, if $this->method outputs a string or a number (like PHP, Perl can automatically convert numbers to strings when required), then you can do print $this->method . "\n";.
If $this->method outputs a data structure (eg an array reference or a hash reference), you can use Data::Dumper to look at the structure of the data. Basically, print Dumper($foo) is the Perl equivalent of PHP's var_dump($foo).
What are you trying to do, exactly?
If $this->method is returning a string, you can do this:
print $this->method . "\n";
without quotes. That will print your string. Sometimes, that can lead to a clumsy looking statement:
print "And we have " . $this->method . " and " . $that->method . " and " . $there->method . "\n";
In that case you can use a little programming trick of:
print "And we have #{[$this->method]} and #{[that->method]} and #{[$their->method]}\n";
Surrounding a function with #{[]} prints out the function's value. Someone explained this to me once, but I can't remember why it works.