how to use the python str.format from a python verticle ?

620 views
Skip to first unread message

tsv

unread,
Dec 14, 2013, 7:29:45 AM12/14/13
to ve...@googlegroups.com
Hi,

Am new to vertx and thanks for this awesome project! 

In trying some basics to familiarize writing a python verticle, came across a blocker. Have narrowed it down to the following simple code. (Am running it on Windows 7 (64bit) for learning but looking forward to use it on a linux distro)

$ vertx version
2.1M2 (built 2013-12-01 13:02:34)

$ cat test.py
import vertx

vertx.logger().info( '1+1 is {0}'.format(2) )

$ vertx run test.py
Traceback (most recent call last):
  File "test.py", line 9, in <module>
  File "test.py", line 4, in f__VertxInternalVert__1
AttributeError: 'str' object has no attribute 'format'

Traceback (most recent call last):
  File "test.py", line 9, in <module>
  File "test.py", line 4, in f__VertxInternalVert__1
AttributeError: 'str' object has no attribute 'format'


Is the str.format method not supported ? Or am I missing a required import ?
Thanks!


Tim Yates

unread,
Dec 14, 2013, 9:07:55 AM12/14/13
to ve...@googlegroups.com

I'm no expert, but isn't str.format a python 3 thing?

The jython language mod currently uses jython 2.5.3

https://github.com/vert-x/mod-lang-jython/blob/master/gradle.properties

Tim

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

tsv

unread,
Dec 14, 2013, 11:18:23 AM12/14/13
to ve...@googlegroups.com
Thanks for the quick reply Tim!
I looked up the Python documentation and yes - it is in PEP 3101. I have Python 2.7.3 on my dev box and str.format works fine there. But I understand that is different from Jython implementation and wouldn't expect it.
However, before posting this question, I looked up the Jython documentation http://www.jython.org/docs/library/string.html#string-formatting  (section 7.1.3 Format String Syntax) and it did specify str.format. At the top of the page, the breadcrumbs says "Jython v2.5.2 documentation", but it could be a documentation bug too! 

Just noticed in Jython website that they have two other versions coming up:
Jython 2.5.4rc1
Jython 2.7beta1

"The Jython 2.7beta1 release is also available for download. Jython 2.7b1 brings us up to language level compatibility with the 2.7 version of CPython. We have focused largely on CPython compatibility, and so this release of Jython can run more pure Python apps then any previous release."

Hoping to see vertx with Jython 2.7 soon :)

Now, to the actual reason why I got into this: I want to parse REST request JSON payload and also generate JSON responses. I tried to "import json" and it was not available. So, I copied (I know - that throws away portability and all - just wanted to try!) over the json folder from my local Python 2.7.1 lib to the same folder where my verticle file is. Then ran the verticle and it failed with the str.format as listed above. 
Is there a vertx native way to do JSON in a python verticle ? Or, is that limited by the Jython version too ? Thanks again for helping me.

-Venkat

kuujo

unread,
Dec 15, 2013, 5:58:33 AM12/15/13
to ve...@googlegroups.com
Vert.x core does of course have a Jackson-based Json parser. The Java JsonObject and JsonArray constructors accept a json string as argument (which will be parsed to a json object or array respectively). So, it could be used by doing something like:

import org.vertx.java.core.json.JsonObject
from core.javautils import map_from_java

json_string = '{"foo":"bar","bar":["baz","foo"]}'
some_dict = map_from_java(org.vertx.java.core.json.JsonObject(json_string).toMap())

This is obviously not ideal. These are really internal Python module functions, and the Jython module does not itself support an interface for parsing json strings. Perhaps we could add it though since it is an accessible feature of the Vert.x API.

tsv

unread,
Dec 15, 2013, 1:40:04 PM12/15/13
to ve...@googlegroups.com
Awesome! thanks. Tried and it worked.
Should be good enough as this unblocks me in what I am trying to do :)

-Venkat
Reply all
Reply to author
Forward
0 new messages