Wednesday, November 20, 2013

brython (32) - python

2013-8-23 08:28


=========================


flicker 彩虹炫 | 编辑 删除Big 2013-8-23 08:29
if __name__== "__main__": 

type(var); 
obj.display="none"    # to hide

# 1. Unix
# 2. this module
# 3. imports (modules)
# 4. variables (globals)
# 5. classes
# 6. functions
# 7. main()

        #-  block a statement
        #.  comment on the below
        #..  comment on the above
        # END of a block
        ##  notes
flicker 彩虹炫 | 编辑 删除Big 2013-8-23 08:29
#.  declaration and availability
        class contains 
                properties (local var) and 
                methods (def func(self, ...), having its own local var), 
                no functions
        class must be defind before be invoked
        __init__() can invoke any method defined later
        sub-functions of a function must be defined before be invoked
        methods of a class can be invoked before be defined

        #.  indentation
        backlash (\), tuple pair ((..)), list pair ([..]), dictionary pair ({..}) 
                will ignore indentation

#.  assignment
        a = b = c  ====  (a = ( b = c))  ====  b = c, then a = b
        a,b = b,c  ====  (a,b) = (t1,t2) = (b,c)

#.  if
        if cond: pass; elif cond: pass; else: pass;
        try: pass; expect: pass; finally: pass; 
        var = a if cond else b
        var = a if cond else b if cond else c
                ====  var = (a if cond else (b if cond else c))

#.  list
        LIST.sort ([func])
        sorted (LIST, cmp=func2, key=func1, reverse=True)
                func2 (arg1, arg2_next);  func1 (arg1)
        zip ((1,2,3),(7,8))  ==> [(1,7),(2,8)]
        list (map (func, seq1, seq2...)); func (elm1, elm2...)
        filter (func1_filter, seq)
        reduce (func2, seq)  ==> func2(func2(e1,e2),e3)...

#.  naming of short names
        xxx2 - local var in function
        xxx3 - function name, class name, argument name
        xxx4 - global var

#.  list comprehension
        [x for x in X if cond(x)]
        [func(x,y) for x in X for y in Y]    # len(XY) = len(X) * len(Y)
        [[func(x,y) for x in X] for y in Y]    # len(XY) = len(Y), len(xy) = len(X)
flicker 彩虹炫 | 编辑 删除Big 2013-8-23 08:32
__doc__
__name__
__self__
__function__

__dict__ : dictionary of attributes and their values
__dir__
__code__
__globals__

__getattr__ : attribute unfound
__getattribute__
__setattr__
__delattr__
__call__

__get__ : attribute
__set__
__delete__

__init__ : invoked as substanization (initialization)
__new__
__str__  : string to read;  default:: __str__ = __repr__
__repr__ : string for eval()


dir()  : list of functions in module
help() : munual of a function in module
flicker 彩虹炫 | 编辑 删除Big 2013-8-23 08:33
HTML - ruby
        html.RUBY ("base-1" + "base-0" + html.RT("title"))
                title - base-0 - base-1
        html.RT (html.RP("left") + "title" + html.RP("right"))
                left - title - right

re.search(pattern, string, flags=0)
        - return a object, with group(0)

re.findall(pattern, string, flags=0)
        - return a list
flicker 彩虹炫 | 编辑 删除Big 2013-8-23 08:33
doc <= "string"
        doc['id'] <= "string"

        doc['zone'] <= html.H1("Introducing Brython")

        link1 = html.BR() + html.A('Brython2', href='http://www.brython.info')
        link2 = html.BR() + html.A(html.B('Python'), href='http://www.python.org')
        doc['zone'] <= link1 + link2

        d1 = html.DIV('Brython3', style={'height':100, 'width':200})
        d2 = html.DIV('Brython4',Id="zone",Class="container")
        doc['zone'] <= html.BR() + d1 + "wher I am" + d2

        doc['zone'] <= 'Official Python Website: ' + link
        doc['zone'] <= html.P() + 'I repeat: the site is ' + link.clone()
flicker 彩虹炫 | 编辑 删除Big 2013-8-23 08:35
from ... import ... as, 

class, 
def ... return ... pass, 
def ... yield, 
global (external var), 

lambda (inline def; VAR = lambda), 
assert (False, then interrution), 
try ... except ... finally, 

while ... continue ... break, 
for (VAR in) ... continue ... break, 
if ... elif ... else, 
False, True, 
is (same object), 

None, 
del,

flicker 彩虹炫 | 编辑 删除Big 2013-8-23 08:40
// bryext.js  for brython.js

pyjsf = (function(){
        // to load py code, in a js file as comment
        // HTML loads: (1) head-script, (2) body-script, (3) body-onload

        /*****
        USAGE:: 
                1. embed the py code in function as comment, in a javascript file
                2. the header and footer: (remove . from /.* and *./)
                        header: pyjsf.load ( (function x(){/.*!
                        footer: *./return x}()).toString().slice(17,-12));
                3. or, the header and footer: (remove . from /.* and *./)
                        header: pyjsf.add ( (function x(){/.*!
                        footer: *./return x}()).toString().slice(17,-12));
                        ..... pyjsf.eval()

                pyjsf.load ( (function x(){/.*!
                def xx:
                        pass
                yy = 3
                *./return x}()).toString().slice(17,-12));
        *****/

        //. load together, after adding to buffer
        var pyjsList = []
        function add2 (src31){
                pyjsList [pyjsList.length] = src31
        }
        function eval2 (){
                for (var i=0;i                        load2 (pyjsList[ i])
                        pyjsList[ i] = ""
                }
                pyjsList = []
        }

        //. load right away
        function load2 (src31){
                // source::  brython.js -- function brython()

                // var $src=($elt.innerHTML || $elt.textContent)
                var $src = src31
                __BRYTHON__.$py_module_path['__main__']='.' 
                
                try{
                        var $root=__BRYTHON__.py2js($src,'__main__')
                        var $js=$root.to_js()
                        eval($js)
                }catch($err){
                        throw $err
                }
        }

        // -- -- --

        function importModule (mName){
                //
                var undefined
                var $moduleTemp = $module || undefined
                $module = undefined

                var path2 = arguments[1] || "../libs/"
                var script = document.createElement('script')
                script.src = path2 +mName +'\.js'
                        //.. __BRYTHON__.brython_path+'libs/'
                script.type = 'text/javascript'
                script.onload = function(){
                                eval(mName +"=JSObject(\$module)"); 
                                $module = $moduleTemp
                }
                script.onreadystatechange = script.onload 

                document.getElementsByTagName('head')[0].appendChild(script)


        }
        //- importModule21 ("html")

        // -- -- --

        function init (){
                // source::  brython.js - prython()
                // part of initiation
                
                document.$py_src={}
                __BRYTHON__.$py_module_path={}
                __BRYTHON__.$py_module_alias={}
                __BRYTHON__.modules={}
                __BRYTHON__.$py_next_hash=-Math.pow(2,53)

                document.$debug=0
                var options={'debug':0}
                __BRYTHON__.$options=options
                __BRYTHON__.exception_stack=[]
                __BRYTHON__.scope={}
                __BRYTHON__.path=[]

        }
        init()

        function objToString (obj) {
                var out = ''
                var p; 
                for (p in obj) {
                        out += p + ': ' + obj[p] + '\n';
                }
                return out;
        }
        function objAlert (obj) {alert(objToString(obj))}
        function objConsole (obj) {console.log(objToString(obj))}
        function printToConsole (x) {console.log(x)}

        function JSObject2 (obj){
                // pythonize a js object
                obj.__getattr__ = function(attr){return this[attr]}
                return obj
        }
        //. the below from brython.js; purpose unknown
        JSObject2.__class__=$type
        JSObject2.__str__=function(){return ""}
        JSObject2.toString=JSObject2.__str__

        pyjsLink = {var:1, func:JSObject2}
        pyjsLink.__getattr__ = function(attr){return this[attr]}


        // -- -- --

        var obj = {
                test:123, pj:pyjsLink, 
                load:load2, add:add2, eval:eval2, 
                objStr:objToString, objC:objConsole, objA:objAlert, 
                mod:importModule, 
                JSObj:JSObject2, prnCon:printToConsole, 
        }
        obj.__getattr__ = function(attr){return this[attr]}
        //.. pythonize
        return obj

}());  // END of pyjsf


$module = {
        __getattr__ : function(attr){return this[attr]},
        pj : pyjsf, 
        //.. pj : JSObject (pyjsf)   // pythonize a js object
}
$module.__class__ = $module // defined in $py_utils
$module.__str__ = function(){return ""}
tool31 = $module


No comments:

Post a Comment