oop - Tcl Object Oriented extension for porting into Python code -


i building status bar linux. made module when different keyboard plugged:

set initialteck [eval exec cat [glob /sys/bus/usb/devices/*/product]] set initialteck [string match *truly* $initialteck]  set initialkb [exec xkb-switch]   dict set table samy 0 samy dict set table samy 1 temy dict set table temy 0 samy dict set table temy 1 temy dict set table gb   0 samy dict set table gb   1 temy   proc init {} {     variable initialteck     variable initialkb     set currentteck [eval exec cat [glob /sys/bus/usb/devices/*/product]]     set currentteck [string match *truly* $currentteck]     set currentkb [exec xkb-switch]     if {$initialteck != $currentteck} {         set initialteck $currentteck         nextkb $currentkb $initialteck      } }  proc nextkb { currentkb teck } {     variable table     exec [dict $table $currentkb $teck] } while 1 { init after 1000 

}

temy .xkb layout file made "truly ergonomic keyboard", samy custom .xkb layout standard gb keyboard. teck stores state of usb port , dict hash table.

obviously there many more modules status bar, use namespaces project grows larger, decided go oop. @ same time need translate project python code share among developers.

currently tcl have many extensions oop; tcloo, snit, stooop, incr-tcl, xotcl , many more. incr-tcl similar c++, there tcl extension similar python?

none of tcl oo systems similar in flavour python; languages different , favour different ways of thinking problems. part of syntactic (most tcl programmers don't _ characters that much!) larger part of tcl oo systems don't work in same way:

  • they don't garbage-collect values because values have names can stored ordinary strings.
  • they don't have language integration allows interception of basic operations.

the net result tcl's oo systems make objects behave tcl commands (which allows great deal of flexibility, of course) , not tcl values (which tend pretty simple transparent numbers, strings, lists , dictionaries). makes different flavour of oo in python.

you can pretend @ simplest level suppose, start doing complicated you'll hit differences. more though, need pick more concrete example dig into.

here's python code:

class shape:      def __init__(self, x, y):         self.x = x         self.y = y         self.description = "this shape has not been described yet"         self.author = "nobody has claimed make shape yet"      def area(self):         return self.x * self.y      def perimeter(self):         return 2 * self.x + 2 * self.y      def describe(self, text):         self.description = text      def authorname(self, text):         self.author = text      def scalesize(self, scale):         self.x = self.x * scale         self.y = self.y * scale 

here's equivalent class definition in tcloo (my favourite):

oo::class create shape {     variable _x _y _description _author      constructor {x y} {         set _x $x         set _y $y         set _description "this shape has not been described yet"         set _author "nobody has claimed make shape yet"     }      method area {} {         return [expr {$_x * $_y}]     }      method perimeter {} {         return [expr {2 * $_x + 2 * $_y}]     }      method describe {text} {         set _description $text     }      method authorname {text} {         set _author $text     }      method scalesize {scale} {         set _x [expr {$_x * $scale}]         set _y [expr {$_y * $scale}]     } } 

apart obvious syntax differences (e.g., tcl likes braces , puts expressions inside expr command) main things notice variables should declared in class definition, , there's no need pass self in (it's automatically present command, [self]).


Comments