Commits

Anonymous committed f6e1dca Merge

$ hg merge europython-2011 # done so merge

  • Participants
  • Parent commits 8e97a87, 95ef889

Comments (0)

Files changed (13)

File js/create.js

+    var _create_fn = function(){};
+    var create = function(parent){
+
+        _create_fn.prototype = parent;
+        var instance = new _create_fn();
+        return instance;
+    };

File js/get-set.js

+var make_get_set = function(){
+
+  var v = null;
+
+  var fset = function(value){
+    v = value;
+  };
+
+  var fget = function(){
+
+    return v;
+  };
+
+  return [fset, fget];
+};

File source/conf.py

 
 # Add any Sphinx extension module names here, as strings. They can be extensions
 # coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
-extensions = []
+extensions = ['sphinx.ext.doctest']
 
 # Add any paths that contain templates here, relative to this directory.
 templates_path = ['_templates']
 
 # General information about the project.
 project = u'JavaScript for Python Programmers'
-copyright = u'2010, Jonathan Fine'
+copyright = u'2010-11, Jonathan Fine'
 
 # The version info for the project you're documenting, acts as replacement for
 # |version| and |release|, also used in various other places throughout the
 
 # The name for this set of Sphinx documents.  If None, it defaults to
 # "<project> v<release> documentation".
-#html_title = None
+html_title = 'Javascript for Python Programmers - EuroTeX 2011'
 
 # A shorter title for the navigation bar.  Default is the same as html_title.
 #html_short_title = None
 # http://groups.google.com/group/sphinx-dev/browse_thread/thread/ee301b14175ad5bb/c06c6600192a17da
 latex_elements = {
     'classoptions': ',openany,english',
-} 
+}

File source/counters-example.rst

 
 In general library code is a good idea.  This example is designed to
 teach you the basics of JavaScript, and not the use of a library.  We
-hope that what you learn here will help you choose (and develop) a
-library.
+hope that what you learn here will help you choose (and contribute to)
+a library.
 
 
 counters.html

File source/easiest-quiz-answers.rst

-Answers to the easiest quiz (draft)
-===================================
+Answers to the easiest quiz
+===========================
 
 Answer 2: Equality is transitive
 ----------------------------------
 Thus
 
 .. code-block:: javascript
-   
+
    js> x = 'abc'
    js> y = x.charAt(2)
 
    en
 
 follow because in JavaScript the value of an assignment expression is
-always the right-hand side of the assignment.  
+always the right-hand side of the assignment.
 
 In Python, assignments are statements, not expressions.  In Python,
 multiple assignment is arises from the parsing rules and not from

File source/europython-2010-tutorial.rst

-EuroPython 2010 Tutorial
-========================
-
-Time, date and location
------------------------
-
-This tutorial will take place on Saturday 17th July, from 2pm to
-5.45pm in Lecture Room 2, the Birmingham Conservatoire, Birmingham,
-UK.
-
-
-Installed software
-------------------
-
-Participants should bring with them a laptop computer with the
-following installed:
-
-1.  The Firefox web browser, with the Firebug add-on.
-
-2.  A programmer's editor, preferably with a JavaScript mode.
-
-3.  A command line JavaScript interpreter.
-
-    * For Linux I suggest SpiderMonkey.
-      
-         .. code-block:: bash
-
-             $ sudo apt-get install spidermonkey-bin   # Ubuntu
-
-     (No longer, I'm told, available for Ubuntu 10.4.)
-     
-    * For Windows and Mac I suggest JSDB, from
-      http://www.jsdb.org/download.html.  It also works on Linux.
-
-4.  This documentation, downloaded from 
-    `Bitbucket downloads <http://bitbucket.org/jfine/javascript-for-python-programmers/downloads>`_.
-
-5.  The Python documentation, download from 
-    `Python docs site <http://docs.python.org/download.html>`_.
-
-
-Timetable
----------
-
-The tutorial consist of two 90 minutes sessions, separated by a 15
-minute break.  We start at 2pm prompt, with software already installed
-if possible.
-
-I hope to run six sessions, each 30 minutes long.  I hope each session
-will be 15 minutes of me talking followed by 15 minutes of
-programming. The topics I'm intending to cover are
-c
-1. Demonstration and discussion of the counters example application.
-
-2. JavaScript gotchas.
-
-3. Objects and inheritance, class basics.
-
-Here we have a 15 minute break.
-
-4. JavaScript's *this* pseudo-variable (including *call* and *apply*).
-
-5. Closures, modules and memory leaks.
-
-6. Delegation.
-
-We finish at 5.15pm.
-
-
-Is this tutorial for me?
-------------------------
-
-This tutorial aimed at Python web developers who already know a bit of
-JavaScript, and who need to understand JavaScript better.
-
-The tutorial has two related objectives. One is a good understanding
-of the counters example.  The other is a good understanding of the
-things that make JavaScript so different from Python (apart from
-JavaScript being the only language supported by web browsers).
-
-If you're thinking of taking this tutorial take the :doc:`gotcha-quiz`
-and read through the :doc:`counters-example`.  If you understand
-what's there a bit, and would like to understand it more, then this
-tutorial is for you.
-
-Let me know
------------
-
-If you're going to attend the tutorial, I'd appreciate an email from
-you that tells me a little bit about yourself and what you'd like to
-get from the tutorial.  If you have specific questions about
-JavaScript, I'd like to here them also.  (I can be contacted at
-Jonathan.Fine1 at gmail.com.)
-
-You can also, I believe, just turn up on the day, if there is
-space. Don't forget to install the software on your laptop.

File source/europython-tutorial.rst

+EuroPython 2011 Tutorial
+========================
+
+Time, date and location
+-----------------------
+
+This tutorial will take place on Tuesday 21st June, from 14.30 to
+18.30 (2.30pm to 6.30pm) in Training Pizza Napoli.
+
+
+Installed software
+------------------
+
+Participants should bring with them a laptop computer with the
+following installed:
+
+1.  The Firefox web browser, with the Firebug add-on.
+
+2.  A programmer's editor, preferably with a JavaScript mode.
+
+    I use emacs, together with js2-mode.
+
+3.  A command line JavaScript interpreter.
+
+    * For Linux I suggest SpiderMonkey.
+
+         .. code-block:: bash
+
+             $ sudo apt-get install spidermonkey-bin   # Ubuntu
+
+     (No longer, I'm told, available for Ubuntu 10.4.)
+
+    * For Windows and Mac I suggest JSDB, from
+      http://www.jsdb.org/download.html.  It also works on Linux.
+
+4.  This documentation, downloaded from
+    `Bitbucket downloads <http://bitbucket.org/jfine/javascript-for-python-programmers/downloads>`_.
+
+5.  The Python documentation, download from
+    `Python docs site <http://docs.python.org/download.html>`_.
+
+
+Timetable
+---------
+
+We start at 14.30 prompt, with software already
+installed if possible.
+
+I hope to run seven sessions, each about 30 minutes long.  They'll be
+about 20-30 minutes for breaks, as we need then.  I hope each session
+will be 15 minutes of me talking followed by 15 minutes of
+programming. The topics I'm intending to cover are
+
+1. Demonstration and discussion of the counters example application.
+
+2. JavaScript gotchas.
+
+3. Objects and inheritance, class basics.
+
+4. JavaScript's *this* pseudo-variable (including *call* and *apply*).
+
+We'll certainly have a break here.
+
+5. Closures, modules and memory leaks.
+
+6. Delegation.
+
+7. Review of counters example application.
+
+8. JavaScript and Python objects and classes compared.
+
+We finish at 18.30.
+
+
+Is this tutorial for me?
+------------------------
+
+This tutorial aimed at Python web developers who already know a bit of
+JavaScript, and who need to understand JavaScript better.
+
+The tutorial has two related objectives. One is a good understanding
+of the counters example.  The other is a good understanding of the
+things that make JavaScript so different from Python (apart from
+JavaScript being the only language supported by web browsers).
+
+If you're thinking of taking this tutorial take the :doc:`gotcha-quiz`
+and read through the :doc:`counters-example`.  If you understand
+what's there a bit, and would like to understand it more, then this
+tutorial is for you.
+
+Let me know
+-----------
+
+If you're going to attend the tutorial, I'd appreciate an email from
+you that tells me a little bit about yourself and what you'd like to
+get from the tutorial.  If you have specific questions about
+JavaScript, I'd like to here them also.  (I can be contacted at
+Jonathan.Fine1 at gmail.com.)
+
+You can also, I believe, just turn up on the day, if there is
+space. Don't forget to install the software on your laptop.

File source/index.rst

    :maxdepth: 2
 
    intro
-   europython-2010-tutorial
+   europython-tutorial
 
    gotcha-quiz
    gotcha-answers
-
+   easiest-quiz-answers
 
    counters-example
    counters-discuss
 .. toctree::
    :maxdepth: 2
 
+   know-in-python
+
    objects
+   javascript-objects
    inheritance
    functions
    functions-and-this
    classes
 
-Draft pages
+.. ReferenceStubs::
 
-.. toctree::
-   :maxdepth: 2
-
-   easiest-quiz-answers
-   know-in-python
-   objects-are-dicts
-
-
-
-
-Reference stubs:
-
-.. toctree::
+.. AAAtoctree::
    :maxdepth: 2
 
    functions-and-scope
    downloads
 
 
-
-Indices and tables
-==================
-
-* :ref:`genindex`
-* :ref:`modindex`
 * :ref:`search`
 

File source/javascript-objects.rst

+JavaScript objects
+==================
+
+Like Python classes
+-------------------
+
+In JavaScript all objects are part of an inheritance tree.  The
+**create** function adds a node to the inheritance tree.
+
+.. code-block:: javascript
+
+   // A JavaScript object.
+   js> root = {}
+
+   // Prototype inheritance.
+   js> create = function (obj) {
+           var f = function () {return this;};
+           f.prototype = obj;
+           return new f;
+       }
+
+   js> a = create(root)
+   js> b = create(a)
+
+   js> a.name = 5
+   js> a.name
+   5
+   js> b.name
+   5
+
+In Python classes inherit in the same way.
+
+.. doctest::
+
+   >>> root = type              # Most classes are instance of type.
+   >>> class a(root): pass
+   >>> class b(a): pass         # Class inheritance.
+
+   >>> a.name = 5               # Just like JavaScript.
+   >>> a.name
+   5
+   >>> b.name
+   5
+
+class explanation
+^^^^^^^^^^^^^^^^^
+
+In Python we can subclass anything whose type is **type** (or a
+subclass of type).  A subclass (and its instances) inherits properties
+from the super-class.
+
+   >>> type(root) == type(a) == type(b) == type
+   True
+
+
+
+Custom item methods
+-------------------
+
+In JavaScript attribute and item access are the same.
+
+.. code-block:: javascript
+
+   js> a = create(root)
+
+   js> a.name = 5
+   js> a['name']
+   5
+
+   js> a['key'] = 6
+   js> a.key
+   6
+
+   js> a[1] = 6
+   js> a['1']
+   6
+
+In Python we can defined our own item methods.  (The programmer owns
+the dot.)
+
+.. doctest::
+
+   >>> class A(object):
+   ...
+   ...     def __getitem__(self, key):
+   ...         return getattr(self, str(key))
+   ...     def __setitem__(self, key, value):
+   ...         return setattr(self, str(key), value)
+
+   >>> a = A()
+   >>> a.name = 5
+
+   >>> a['name']
+   5
+
+   >>> a['key'] = 6
+   >>> a.key
+   6
+
+   >>> a[1] = 6
+   >>> a['1']
+   6
+
+Because **type(a)** is **A**, which has the special item methods, we
+get the special item behaviour.
+
+.. doctest::
+
+   >>> type(a) is A
+   True
+
+
+On metaclass
+------------
+
+Using previous definition, we cannot subclass **a** to create **b**.
+
+.. doctest::
+
+   >>> class b(a): pass
+   Traceback (most recent call last):
+       class b(a): pass
+   TypeError: Error when calling the metaclass bases
+       object.__new__() takes no parameters
+
+This is because **a** is not a type.  The solution involves Python
+metaclasses (an advanced topic).
+
+.. doctest::
+
+   >>> isinstance(a, type)
+   False
+
+
+metaclass construction
+^^^^^^^^^^^^^^^^^^^^^^
+We will subclass type, not object, and add to it the special item
+methods.
+
+
+.. doctest::
+
+   >>> class ObjectType(type):
+   ...
+   ...     def __getitem__(self, key):
+   ...         return getattr(self, str(key))
+   ...
+   ...     def __setitem__(self, key, value):
+   ...         return setattr(self, str(key), value)
+
+Here is a fancy way of calling **ObjectType**.
+
+  .. doctest::
+
+   >>> class root(object):
+   ...     __metaclass__ = ObjectType
+
+
+Here is a more direct (and equivalent) construction (create an
+instance of **ObjectType**, whose instances are objects).
+
+.. doctest::
+
+   >>> root = ObjectType('root', (object,), {})
+   >>> isinstance(root(), object)
+   True
+
+metaclass demonstration
+^^^^^^^^^^^^^^^^^^^^^^^
+.. doctest::
+
+   >>> class a(root): pass
+   >>> class b(a): pass
+
+   >>> a.name = 5
+   >>> a.name
+   5
+   >>> b.name
+   5
+   >>> a['name']
+   5
+   >>> b['name']
+   5
+
+   >>> a[1] = 6
+   >>> a['1']
+   6
+
+
+metaclass explanation
+^^^^^^^^^^^^^^^^^^^^^
+
+Because **type(root)** is a subclass of type we can subclass root.
+
+.. doctest::
+
+   >>> issubclass(type(root), type)
+   True
+
+Because the **type(root)** is **ObjectType**, which has special item
+methods, we get the special item behaviour.
+
+.. doctest::
+
+   >>> type(root) == type(a) == type(b) == ObjectType
+   True
+
+
+Never instantiated
+------------------
+
+We can't call JavaScript objects (unless they are a function).  But
+**create** creates ordinary JavaScript objects.
+
+.. code-block:: javascript
+
+   js> a = create(root)
+   js> a(1, 2, 3)
+   TypeError: a is not a function
+
+
+We will monkey-patch the previous Python class, to provide custom
+behaviour when called.
+
+.. doctest::
+
+   >>> def raise_not_a_function(obj, *argv, **kwargs):
+   ...     raise TypeError, obj.__name__ + ' is not a function'
+
+   >>> ObjectType.__call__ = raise_not_a_function
+
+   >>> a(1, 2, 3)
+   Traceback (most recent call last):
+        a(1, 2, 3)
+   TypeError: a is not a function
+
+
+Conclusion
+----------
+
+JavaScript objects are like Python classes (because they inherit like
+Python classes).
+
+For JavaScript attribute and item access are the same.  This is
+achieved in Python by providing custom item methods.
+
+In Python the custom item methods must be placed on the type of the
+object (or a superclass of its type).
+
+Ordinary JavaScript objects are not functions and cannot be called.  A
+Python class can be called (to create an instance of the object).  But
+we can override this behaviour by supplying a custom method for call.
+
+To summarize:
+..
+
+  **JavaScript objects are like Python classes with custom item
+  methods (on the metaclass) that are never instantiated.**
+
+It's worth saying again:
+
+..
+
+   **JavaScript objects are like Python classes with custom item
+   methods (on the metaclass) that are never instantiated.**
+
+
+

File source/know-in-python.rst

-What do you need to know in Python (draft)
-==========================================
+What do you need to know in Python
+==================================
 
 Global and local scopes
 -----------------------
         global g
 
         # The *global* variable *g* is assigned the value of *local*
-        # variable *x* 
+        # variable *x*
         g = x
 
-    my_function(3) 
+    my_function(3)
 
 In JavaScript you have a whole hierarchy of scopes -- each new nested function
 creates a new scope. The scopes are searched beginning from the innermost up to
 closure.
 
 An example use of a closure is a "private" variable that can be only accessed
-by *get* and *set* functions. 
+by *get* and *set* functions.
 
 .. code-block:: python
 

File source/objects-are-dicts.rst

-Objects are dictionaries (draft)
-================================
-
-.. code-block:: python
-
-   # class to represent JavaScript behavour, based on dict
-   class Jso(dict):
-       # Obj. & Obj[] are the same thing in JavaScript 
-       def __getitem__(self, key):
-           return dict.get(self, key)
-       __getattribute__ = __getitem__
-   
-       __setattr__ = __setitem__
-
-       __delattr__ = __delitem__
-
-
-.. code-block:: python
-
-   >>> # create js object
-   ... j = Jso()
-   >>> j
-   {}
-   >>> # set by attribute
-   >>> j.x = 3
-   >>> # get by item
-   >>> j['x']
-   3
-   >>> # get by attribute
-   >>> j.x
-   3
-   >>> j
-   {'x': 3}
-   >>> 
-   >>> # set by item
-   >>> j['x'] = 3
-   >>> j['x']
-   3
-   >>> j.x
-   3
-   >>> # missing items return undefined
-   >>> j['y']
-   undefined
-   >>> # so do missing attributes
-   >>> j.y
-   undefined
-   >>> 
-   >>> # delete by attribute
-   >>> del j.x
-   >>> j.x
-   undefined
-   >>> j['x']
-   undefined
-   >>> 
-   >>> # put the value back in
-   >>> j.x = 3
-   >>> # delete by item
-   >>> del j['x']
-   >>> j.x
-   undefined
-   >>> j['x']
-   undefined
-
-
-

File source/objects.rst

 The two objects jso and pyo have important similarities and
 differences (mostly differences).
 
+
+Late news
+---------
+
+Not incorporated in this document is a recent discovery, that 
+
+    **JavaScript objects are like Python classes with custom item
+    methods (on the metaclass) that are never instantiated.**
+
+I've put this material in a page on :doc:`javascript-objects`.
+
+
 Similarities
 ------------
 
+\begin{frame}
+
+\frametitle{JavaScript for Python Programmers}
+
+The course slides are available on http://bitbucket.org/jfine.
+
+
+\bigskip
+Go to 
+javascript-for-python-programmers/downloads.
+
+\bigskip
+Download latest version of js4py.
+
+\end{frame}
+
+\begin{frame}
+
+\frametitle{JavaScript for Python Programmers}
+
+A major part of the tutorial is to understand properly the
+\emph{counters example} in the tutorial.
+
+\begin{itemize}
+\item gotchas
+
+\item object tree
+
+\item this vs. self
+
+\item binding
+
+\item delegation
+
+\item objects and classes in Python and JavaScript
+
+\end{itemize}
+
+
+\end{frame}
+
+\begin{frame}
+
+\frametitle{To finish, if there is time}
+
+
+We will show why \emph{JavaScript objects} are like Python classes
+which are never instantiated, and which have special attribute and
+item access methods on the metaclass.
+
+\end{frame}
+
+\end{document}
+