Enumerating the properties of an object - rebol

I have an object of which I would like to view all the properties, but using probe prints out pages and pages of information which I cannot possibly read, and using mold prints out only the first bit.
>> mold input
== {make object! [
type: 'face
offset: 20x20
size: 400x150
span: none
pane: none
text: "Text"
color: 25...

I am used to use help on objects as in
>> help system
SYSTEM is an object of value:
version tuple! 2.7.8.3.1
build date! 1-Jan-2011/16:39:07-8:00
product word! View
core tuple! 2.7.8
components block! length: 60
words object! [unset! error! datatype! context! native! action! ...
license string! {REBOL End User License Agreement IMPORTANT. READ ...
options object! [home script path boot args do-arg link-url server...
user object! [name email home words]
script object! [title header parent path args words]
console object! [history keys prompt result escape busy tab-size b...
ports object! [input output echo system serial wait-list]
network object! [host host-address]
schemes object! [default Finger Whois Daytime SMTP ESMTP POP IMAP ...
error object! [throw note syntax script math access command resv...
standard object! [script port port-flags email face sound error por...
view object! [screen-face focal-face caret highlight-start high...
stats native! System statistics. Default is to return total memo...
locale object! [months days]
user-license object! [name email id message]
And instead of first on objects you should now use words-of
>> help words-of
USAGE:
WORDS-OF value
DESCRIPTION:
Returns a copy of the words of a function or object.
WORDS-OF is a function value.
ARGUMENTS:
value -- (Type: any)
>> probe words-of system
== [version build product core components words license options user script console ports network schemes error standard view
stats locale user-license]

You can get all the properties of an object using first, which operates on a large number of types, including objects. When passing an object, it returns a list of words representing the properties.
>> ? first
USAGE:
FIRST series
DESCRIPTION:
Returns the first value of a series.
FIRST is an action value.
ARGUMENTS:
series -- (Type: series pair event money date object port time tuple any-function library struct event)
So in my example, first input gives the following:
>> probe first input
[self type offset size span pane text color image effect data edge font para feel saved-area rate show? options
parent-face old-offset old-size line-list changes face-flags action state access style alt-action facets related
words colors texts images file var keycode reset styles init multi blinker pane-size dirty? help user-data flag
s doc]

Related

How to close a QInputDialog with after a defined amount of time

I'm currently working on an application that run in the background and sometime create an input dialog for the user to answer. If the user doesn't interact, I'd like to close the dialog after 30 seconds. I made a QThread that act like a timer and the "finished" signal should close the dialog. I unfortunately cannot find a way to close it.
At this point I'm pretty much lost. I completely new to QThread and a beginner in PyQt5
Here is a simplified version of the code (we are inside a class running a UI):
def Myfunction(self,q):
# q : [q1,q2,q3]
self.popup = counter_thread()
self.popup.start()
self.dial = QInputDialog
self.popup.finished.connect(self.dial.close)
text, ok = self.dial.getText(self, 'Time to compute !', '%s %s %s = ?'%(q[0], q[2], q[1]))
#[...]
I tried ".close()" and others but i got this error message:
TypeError: close(self): first argument of unbound method must have type 'QWidget'
I did it in a separated function but got the same problem...
You cannot close it because the self.dial you created is just an alias (another reference) to a class, not an instance.
Also, getText() is a static function that internally creates the dialog instance, and you have no access to it.
While it is possible to get that dialog through some tricks (installing an event filter on the QApplication), there's no point in complicating things: instead of using the static function, create a full instance of QInputDialog.
def Myfunction(self,q):
# q : [q1,q2,q3]
self.popup = counter_thread()
self.dial = QInputDialog(self) # <- this is an instance!
self.dial.setInputMode(QInputDialog.TextInput)
self.dial.setWindowTitle('Time to compute !')
self.dial.setLabelText('%s %s %s = ?'%(q[0], q[2], q[1]))
self.popup.finished.connect(self.dial.reject)
self.popup.start()
if self.dial.exec():
text = self.dial.textValue()
Note that I started the thread just before showing the dialog, in the rare case it may return immediately, and also because, for the same reason, the signal should be connected before starting it.

Ryu Controller Drop Packet

How do I send a flow entry to drop a package using Ryu? I've learned from tutorials how to send package out flow entry:
I define the action:
actions = [ofp_parser.OFPActionOutput(ofp.OFPP_FLOOD)]
Then the entry itself:
out = ofp_parser.OFPPacketOut(datapath=dp, buffer_id=msg.buffer_id, in_port=msg.in_port,actions=actions)
Send the message to the switch:
dp.send_msg(out)
I'm trying to find the documentation to make this code drop the package instead of flooding, without success. I imagine I'll have to change actions on the first step and fp_parser.OFPPacketOut on the second step. I need someone more experienced on Ryu and developing itself to point me to the right direction. Thank you.
The default disposition of a packet in OpenFlow is to drop the packet. Therefore if you have a Flow Rule that when it matches you want to drop the packet, you should simply have an instruction to CLEAR_ACTIONS and then no other instruction, which means that no other tables will be processed since there is no instruction to process (go to) another table and no actions on it.
Remember to keep in mind your flow priorities. If you have more than one flow rule that will match the packet, the one with the highest priority will be the one to take effect. So your "drop packet" could be hidden behind a higher priority flow rule.
Here is some code that I have that will drop all traffic that matches a given EtherType, assuming that no higher priority packet matches. The function is dependent on a couple of instance variables, namely datapath, proto, and parser.
def dropEthType(self,
match_eth_type = 0x0800):
parser = self.parser
proto = self.proto
match = parser.OFPMatch(eth_type = match_eth_type)
instruction = [
parser.OFPInstructionActions(proto.OFPIT_CLEAR_ACTIONS, [])
]
msg = parser.OFPFlowMod(self.datapath,
table_id = OFDPA_FLOW_TABLE_ID_ACL_POLICY,
priority = 1,
command = proto.OFPFC_ADD,
match = match,
instructions = instruction
)
self._log("dropEthType : %s" % str(msg))
reply = api.send_msg(self.ryuapp, msg)
if reply:
raise Exception

error : can't read .... no such variable in TCL script using "after"

I am working in a TCL automation environent at my workplace.
I'm trying to run a delayed command in my script using "after".
The problem i encounter is that when i try to specify commands with variables inside a code block under an "after" , the vars are not recognized and i get an error message.
I'll quote the relevant parts of the code.
proc test_script1 {{bh0 0} ...more vars....} {
.
.
.
after [expr 20000] {
set some_value[ ns1::probe_TCP_connections $bh0 $main_duration 1 15] }
puts "the value i got is $some_value"
}
And i seem to get an error:
can't read "some_value": no such variable
Can anyone suggets what is the problem , and how to oversome it?
thx
You can capture the values of local variables with the help of apply (and it's advisable to use list to build callbacks when they get even slightly non-trivial). Since the body of an apply is a lambda expression with its own scope — a little nameless procedure — you have to use global in it to access state that will persist, and in any case after callbacks are called from the global namespace always (since the mechanism doesn't know how to keep arbitrary stack frames around for the duration of the asynchronous operation).
after 20000 [list apply {{bh0 main_duration} {
global some_value
set some_value [ns1::probe_TCP_connections $bh0 $main_duration 1 15]
}} $bh0 $main_duration]
global some_value
vwait some_value
puts "The value was changed to $some_value"
It's possible to get even more sophisticated, especially in Tcl 8.6 which has a coroutine system that can be used to hide the complexity of using continuation passing style programming.
Here is a proc that will do something similar to what you're trying to do:
proc foo {} {
# Make 'a' available to both the global scope and local scope
global a
# This is to check the current level
puts "Current level: [info level]"
# The command to be run in 500 ms, and I have added another check for the level
after 500 {puts "Current level: [info level]"; set a 100}
# Wait for further execution until the global variable 'a' changes
vwait a
# Prints a
puts "Value of \$a: $a"
}
The output of the above will be:
Current level: 1
# After 500 ms, the below will print
Current level: 0
Value of $a: 100
When you use after, the variable a is being created in the global scope, that is not accessible to the proc unless explicitly given access. One of the simplest ways is to first make sure that a within the proc is accessible both globally and locally, with global (level 0).

Rebol 3 - How do you debug using an equivalent of view/new for Saphirion's Rebol

I'm trying to do some testing while working on GUIs, but I am running into trouble having console access at the same time.
Rebol 3
>> view/new [button]
** Script error: view has no refinement called new
>> help view
USAGE:
VIEW spec /options opts /modal /no-wait /across /as-is /maximized /minimized /on-error error-handler
DESCRIPTION:
Displays a window view from a layout block, face (layout), or low level graphics object (gob).
VIEW is a function value.
ARGUMENTS:
spec -- Layout block, face object, or gob type (block! object! gob!)
REFINEMENTS:
/options
opts -- Optional features, in name: value format (block!)
/modal -- Display a modal window (pop-up)
/no-wait -- Return immediately - do not wait
/across -- Use horizontal layout-mode for top layout (rather than vertical)
/as-is -- Use GOB exactly as passed - do not add a parent gob
/maximized -- Open window in maximized state
/minimized -- Open window in minimized state
/on-error
error-handler -- specify global error handler (block!)
Looking at the help, I tried
view/no-wait
and this gives console access, but the REB-GUI window locks up. What is going on with this? Is there a way to be able to access commands through the console while playing with the GUI?
There isn't a way to have both console access and the gui concurrently.
But you could use an area face to enter commands and a button to evaluate those commands in your GUI.

Erlang process event error

I'm basically following the tutorial on this site Learn you some Erlang:Designing a concurrent application and I tried to run the code below with the following commands and got an error on line 48. I did turn off my firewall just in case that was the problem but no luck. I'm on windows xp SP3.
9> c(event).
{ok,event}
10> f().
ok
11> event:start("Event",0).
=ERROR REPORT==== 9-Feb-2013::15:05:07 ===
Error in process <0.61.0> with exit value: {function_clause,[{event,time_to_go,[0],[{file,"event.erl"},{line,48}]},{event,init,3,[{file,"event.erl"},{line,31}]}]}
<0.61.0>
12>
-module(event).
-export([start/2, start_link/2, cancel/1]).
-export([init/3, loop/1]).
-record(state, {server,
name="",
to_go=0}).
%%% Public interface
start(EventName, DateTime) ->
spawn(?MODULE, init, [self(), EventName, DateTime]).
start_link(EventName, DateTime) ->
spawn_link(?MODULE, init, [self(), EventName, DateTime]).
cancel(Pid) ->
%% Monitor in case the process is already dead
Ref = erlang:monitor(process, Pid),
Pid ! {self(), Ref, cancel},
receive
{Ref, ok} ->
erlang:demonitor(Ref, [flush]),
ok;
{'DOWN', Ref, process, Pid, _Reason} ->
ok
end.
%%% Event's innards
init(Server, EventName, DateTime) ->
loop(#state{server=Server,
name=EventName,
to_go=time_to_go(DateTime)}).
%% Loop uses a list for times in order to go around the ~49 days limit
%% on timeouts.
loop(S = #state{server=Server, to_go=[T|Next]}) ->
receive
{Server, Ref, cancel} ->
Server ! {Ref, ok}
after T*1000 ->
if Next =:= [] ->
Server ! {done, S#state.name};
Next =/= [] ->
loop(S#state{to_go=Next})
end
end.
%%% private functions
time_to_go(TimeOut={{_,_,_}, {_,_,_}}) ->
Now = calendar:local_time(),
ToGo = calendar:datetime_to_gregorian_seconds(TimeOut) -
calendar:datetime_to_gregorian_seconds(Now),
Secs = if ToGo > 0 -> ToGo;
ToGo =< 0 -> 0
end,
normalize(Secs).
%% Because Erlang is limited to about 49 days (49*24*60*60*1000) in
%% milliseconds, the following function is used
normalize(N) ->
Limit = 49*24*60*60,
[N rem Limit | lists:duplicate(N div Limit, Limit)].
It's running purely locally on your machine so the firewall will not affect it.
The problem is the second argument you gave when you started it event:start("Event",0).
The error reason:
{function_clause,[{event,time_to_go,[0],[{file,"event.erl"},{line,48}]},{event,init,3,[{file,"event.erl"},{line,31}]}]}
says that it is a function_clause error which means that there was no clause in the function definition which matched the arguments. It also tells you that it was the function event:time_to_go/1 on line 48 which failed and that it was called with the argument 0.
It you look at the function time_to_go/ you will see that it expects its argument to be a tuple of 2 elements where each element is a tuple of 3 elements:
time_to_go(TimeOut={{_,_,_}, {_,_,_}}) ->
The structure of this argument is {{Year,Month,Day},{Hour,Minute,Second}}. If you follow this argument backwards you that time_to_go/ is called from init/3 where the argument to time_to_go/1, DateTime, is the 3rd argument to init/3. Almost there now. Now init/3 is the function which the process spawned in start/2 (and start_link/2) and the 3rd argument toinit/3is the second argument tostart/2`.
So when you call event:start("Event",0). it is the 0 here which is passed into the call time_to_go/1 function in the new peocess. And the format is wrong. You should be calling it with something like event:start("Event", {{2013,3,24},{17,53,62}}).
To add background to rvirding's answer, you get the error because the
example works up until the final code snippet as far
as I know. The normalize function is used first, which deals with the
problem. Then the paragraph right after the example in the question
above, the text says:
And it works! The last thing annoying with the event module is that we
have to input the time left in seconds. It would be much better if we
could use a standard format such as Erlang's datetime ({{Year, Month,
Day}, {Hour, Minute, Second}}). Just add the following function that
will calculate the difference between the current time on your
computer and the delay you inserted:
The next snippet introduces the code bit that takes only a date/time and
changes it to the final time left.
I could not easily link to all transitional versions of the file, which
is why trying the linked file directly with the example doesn't work
super easily in this case. If the code is followed step by step, snippet
by snippet, everything should work fine. Sorry for the confusion.