You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Apache Wiki <wi...@apache.org> on 2005/04/07 01:52:10 UTC

[Jakarta-velocity Wiki] Update of "JythonUberspect" by JasonBriggs

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-velocity Wiki" for change notification.

The following page has been changed by JasonBriggs:
http://wiki.apache.org/jakarta-velocity/JythonUberspect

------------------------------------------------------------------------------
  NOTE: this is a work-in-progress.
+ 
+ UPDATE:  added support for integers in the get method.  (JRB)
+ 
+ See below the code for more related code samples (only 1 at the moment).
  
  {{{
  
@@ -78, +82 @@

      * get the method for a jython object (or pass up)
      */
      public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i) throws Exception
-     {
+     {   
          if (obj instanceof PyObject)
          {
              return new PyMethod(methodName);
@@ -166, +170 @@

                  rtn = meth.__call__();
              }
              else
-             {
+             {   
                  // build a python params array
                  PyObject[] pparams = new PyObject[params.length];
                  for (int i = 0; i < pparams.length; i++)
@@ -179, +183 @@

                      {
                          pparams[i] = (PyObject)params[i];
                      }
+                     else if (params[i] instanceof Integer)
+                     {
+                         pparams[i] = new PyInteger(((Integer)params[i]).intValue());   
+                     }
                      else
                      {
+                         System.err.println("unsupported param type " + params[i].getClass().getName());
                          log.error("unsupported param type : " + params[i].getClass().getName());
                          return null;
                      }
@@ -380, +389 @@

  
  }}}
  
+ 
+ The following Jython class can be used to get an element by index from a list.  You would put the listtool into the context (in your jython servlet) and then use it in the template as such:  $listtool.get($myjythonlist, 4)
+ 
+ {{{
+ class listtool:
+     def get(self, obj, idx):
+         if type(obj) in (types.TupleType, types.ListType) and idx < len(obj):
+             return obj[idx]
+         else:
+             return None
+ }}}
+