You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
That is all, ``app.py.js`` is the result of the compilation of a ``app.py`` with the following command::
22
+
That is all, ``app.py.js`` is the result of the compilation of a ``app.py`` with the following command:
21
23
22
-
pythonscript < app.py > app.py.js
24
+
.. code-block:: sh
23
25
26
+
pythonscript < app.py > app.py.js
24
27
25
28
.. note:: The following tests can be done in the `online editor <http://apppyjs.appspot.com/>`_.
26
29
27
-
.. note:: Or you can use the mini `deli Django project <https://github.com/amirouche/PythonScript/tree/master/django-demo>`_.
30
+
.. note:: Or you can use the mini `demo Django project <https://github.com/amirouche/PythonScript/tree/master/django-demo>`_.
28
31
29
32
.. note:: Whatever route you go you will need `firebug <https://addons.mozilla.org/fr/firefox/addon/firebug/>`_.
30
33
@@ -36,7 +39,7 @@ The old ``print`` statement will be converted to Javascript ``console.log`` whic
36
39
Dealing with variables
37
40
----------------------
38
41
39
-
This is very important! Just like in Javascript you **must** «declare» the variables using the ``var`` function. For instance ``var(spam)``. It can take several arguments ``var(spam, egg, bottle)``. This is because PythonScript use the underlying Javascript variable scope and in Javascript a variable is by default global. The partical consequence is that you **must** declare every usage of a variable *except* if you want to use a global (which seldom happens). Typical code in PythonScript looks like:
42
+
This is very important! Just like in Javascript you **must** «declare» the variables using the ``var`` function. For instance ``var(spam)``. It can take several arguments ``var(spam, egg, bottle)``. This is because PythonScript use the underlying Javascript variable scope and in Javascript a variable is by default global. The pratical consequence is that you **must** declare every usage of a variable *except* if you want to use a global (which seldom happens). Typical code in PythonScript looks like:
40
43
41
44
.. code-block:: python
42
45
@@ -58,7 +61,9 @@ A function is defined just like in Python:
58
61
59
62
my_function(1,2,3,4,5,spam=6,egg=7)
60
63
61
-
The above code will print in a browser console the following::
64
+
The above code will print in a browser console the following:
@@ -81,7 +86,9 @@ Let's define a function that is more verbose:
81
86
82
87
my_function(1,2,3,4,5,spam=6,egg=7)
83
88
84
-
It will print in the browser console the following::
89
+
It will print in the browser console the following:
90
+
91
+
.. code-block:: javascript
85
92
86
93
foo
87
94
@@ -111,23 +118,15 @@ What we expected.
111
118
112
119
Also, as in Python, functions are objects so you can use them as such.
113
120
114
-
**Becarful**, ``*args`` and ``**kwargs`` are supported in definition but not in calling, this means that the following:
115
-
116
-
.. code-block:: python
117
-
118
-
args =list()
119
-
kwargs =dict()
120
-
my_function(*args, **kwargs)
121
-
122
-
Will **not** work.
123
-
121
+
.. versionchanged:: 0.7
122
+
Added support for ``*`` and ``**`` in calling, pratically it means that given ``args`` a ``list`` and ``kwargs`` a ``dict`` you can use the following call ``function(*args, **kwargs)``.
124
123
125
124
How to work with classes?
126
125
-------------------------
127
126
128
127
Once functions are done, classes are just a piece of cake, except there is yet no ``__get_attribute__`` or ``__getattr__`` hook it similar to CPython. Data descriptors works the same way. And metaclass is explained in the following paragraph.
129
128
130
-
.. warning:: You don't have to inherit ``object`` actually there is no ``object`` object in PythonScript yet.
129
+
.. warning:: You don't have to inherit ``object`` actually there is no ``object`` object in PythonScript.
Copy file name to clipboardExpand all lines: doc/source/internals.rst
+9-6Lines changed: 9 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,11 @@ Compilation
18
18
JSGenerator -> "Javascript" [folded];
19
19
}
20
20
21
-
This is the description of ``pythonscript`` command. Mind the fact that there is no actual ``pythontopythonjs`` command this is done on the fly.
21
+
This is the description of ``pythonscript`` command.
22
+
23
+
.. versionchanged:: 0.7
24
+
There is now ``python_to_pythonjs`` command that allow you to review the python code generated before it's converted to javascript
25
+
22
26
23
27
Execution
24
28
---------
@@ -50,12 +54,12 @@ In the following we will see in order:
50
54
``pythonjs``
51
55
------------
52
56
53
-
``pythonjs`` knows how to translate:
57
+
``pythonjs`` command knows how to translate:
54
58
55
-
- Python functions to Javascript ones
59
+
- Restricted Python functions to Javascript
56
60
- It handles attribute access e.g. ``spam.egg.foobarbaz``
57
61
- Add brackets and semi-colons
58
-
- ``print(*args)`` are converted to ``console.log(*args)``
62
+
- ``print(foo, bar, flower)`` are converted to ``console.log(foo, bar, flower)``
59
63
- ``for i in jsrange(int)`` are converted to Javascript for loops
60
64
61
65
It also support two special functions ``JS(str)`` and ``var(*args)``:
@@ -78,7 +82,7 @@ Will be translated to the following Javascript code:
78
82
d =newArray();
79
83
d.push(1)
80
84
81
-
This makes it possible to:
85
+
This makes possible to:
82
86
83
87
- Support Javascript syntax that is not supported by Python.
84
88
- Bypass translation process as it will be shown later.
@@ -128,7 +132,6 @@ Create an array of integers that can be iterated over by the ``for`` loop genera
128
132
``create_array`` will create a Javascript array with ``args``. This function circunvent a behavior in Javascript where ``new Array(42)`` and ``new Array(42, 24)`` behaves differently.
0 commit comments