Skip to content

Commit 7486fd2

Browse files
author
hartsantler
committed
updated README
1 parent be6a23d commit 7486fd2

2 files changed

Lines changed: 71 additions & 2 deletions

File tree

README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,73 @@ irc freenode::
313313
#pythonjs
314314

315315

316-
![bitdeli](https://d2weczhvl823v0.cloudfront.net/PythonJS/pythonjs/trend.png)
316+
pythonjs.configure
317+
------------------
318+
The special function call `pythonjs.configure` can be inserted anywhere in your code to turn off an on dynamic
319+
features of the language.
320+
321+
If the option `direct_keys` is True then dictionary key lookups are done directly (faster),
322+
objects can not be used as keys, only strings and numbers can then be used as dictionary keys.
323+
324+
The option `direct_operator` controls operator overloading for a given operator: '+', '*'.
325+
If '+' is declared a direct operator then `__add__` overload methods are not called,
326+
the operands are always assumed to be compatible with javascript addition.
327+
328+
The option `runtime_exceptions` if False disables extra runtime checking of expressions and assignments,
329+
note this is always False in javascript mode.
330+
331+
```
332+
pythonjs.configure(
333+
javascript=True/False, ## default False
334+
runtime_exceptions=True/False, ## default True
335+
direct_keys=True/False, ## default False
336+
direct_operator=string ## default 'None'
337+
)
338+
```
339+
340+
Gotchas
341+
---------
342+
1. The calling context of `this` must be taken into account when using fast javascript mode, code that comes after: `pythonjs.configure(javascript=True)` or is inside a `with javascript:` block. When in javascript mode, passing a method as a callback, or setting it as an attribute on another object, requires you call `f.bind(self)` to ensure that `self` within the method points to the class instance. This is not required when using classes defined normal mode, because the `this` calling context is automatically managed.
343+
344+
```
345+
class A:
346+
def method(self):
347+
print(self)
348+
349+
a = A()
350+
351+
with javascript:
352+
class B:
353+
def method(self):
354+
print(self)
355+
356+
b = B()
357+
a.b_method1 = b.method
358+
a.b_method2 = b.method.bind(b)
359+
360+
a.method() ## OK: prints a
361+
a.b_method1() ## FAILS: prints a, should have printed b
362+
a.b_method2() ## OK: prints b
363+
364+
b.a_method = a.method
365+
b.a_method() ## OK: prints a
366+
367+
```
368+
369+
2. When using direct operators, builtins are also affected. List + list will no longer return a new array of items from both lists. String * N will no longer return the string multipled by the number.
370+
371+
```
372+
a = [1,2] + [3,4] ## OK: a is [1,2,3,4]
373+
pythonjs.configure(direct_operator="+")
374+
b = [1,2] + [3,4] ## FAILS
375+
376+
c = "HI" * 2 ## OK: c is "HIHI"
377+
pythonjs.configure(direct_operator="*")
378+
d = "HI" * 2 ## FAILS
379+
380+
```
381+
382+
3. The syntax `from mymodule import *` allows you to import another python script from the same folder,
383+
but both mymodule and the parent will share the same namespace, mymodule can use global variables defined in the parent.
384+
385+

pythonjs/python_to_pythonjs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def __init__(self, source=None, module=None, module_path=None, dart=False, coffe
197197
self._js_classes = dict()
198198
self._in_js_class = False
199199
self._in_assign_target = False
200-
self._with_runtime_exceptions = False
200+
self._with_runtime_exceptions = True ## this is only used in full python mode.
201201

202202
self._iter_ids = 0
203203
self._addop_ids = 0

0 commit comments

Comments
 (0)