strange behaviour with nested lists

Bug #1115129 reported by ctmidi
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
ladon
New
Undecided
Unassigned

Bug Description

hello,

i am currently having a very strange behaviour when using some nested lists with Ladon 0.7.2,

here is some code to reproduce what i get , i directly put the strange behaviour of what i get inside the code.

how are we suppose to build / handle nested complex types ?

(also, please, i am not sure this is classified as a bug, so may be i should have ask in the "question part" ? if so, please accept all my apologizes and move this post)

#!/bin/env python
# -*- coding: utf-8 -*-

import sys
import platform
import os
import traceback

# ------------------------------------------------------------

from ladon.ladonizer import ladonize
from ladon.types.ladontype import LadonType
from ladon.compat import PORTABLE_STRING

# ---------- Singleton Pattern

class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

# ---------- for WS result

class Sentence(LadonType):
    words = [ PORTABLE_STRING ]

class SWS(LadonType):
    hasError = bool
    errMessage = PORTABLE_STRING
    sentences = [ Sentence ]

# ---------- WS logic

class Sentencizer0(object):

    __metaclass__ = Singleton

    @ladonize(PORTABLE_STRING, rtype = SWS )
    def sentencize(self, text):
        result = SWS()
        result.errMessage = ''
        result.hasError = False
        result.sentences = []
        sentences = text.split("\n")
        for sentence in sentences:
            S = Sentence()
            S.words = sentence.split()
            result.sentences.append(S)
        return result

import datetime
a = datetime.datetime.now()
sentencizer = Sentencizer0()
b = datetime.datetime.now()
print("loaded: %s." % (b-a))

def example():
    global sentencizer
    global R
    s = u"""aaa bbb ccc
dd ee f
ggg
hhh i jjj kkk"""
    R = sentencizer.sentencize(s)
    print R.sentences

# running it from Python IDLE, it produce what expected : List of Sentence object containing list of unicode strings
# that are two nested lists.
"""
>>>
loaded: 0:00:00.
>>> example()
[<__main__.Sentence object at 0x02B7ADF0>, <__main__.Sentence object at 0x02B7AB30>, <__main__.Sentence object at 0x02B7AD70>, <__main__.Sentence object at 0x02B7AE10>]
"""

# running it from SOAPUI, it produce a strange behaviour as inner list is now flatten
# i would have expected <sentences> containing item <sentence> containing item of (portable) unicode string
# where am i wrong ?
"""
<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="urn:Sentencizer" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <ns:sentencizeResponse>
         <errMessage/>
         <hasError>false</hasError>
         <sentences>
            <item>
               <words>
                  <item>aaa</item>
                  <item>bbb</item>
                  <item>cccdd</item>
                  <item>ee</item>
                  <item>fggghhh</item>
                  <item>i</item>
                  <item>jjj</item>
                  <item>kkk</item>
               </words>
            </item>
         </sentences>
      </ns:sentencizeResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
"""

Revision history for this message
ctmidi (ctmidi31) wrote :

so, i am almost sure that i found something buggy or using it bad...

here is what i get :

---8<---
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/ladon-0.7.2-py2.6.egg/ladon/server/wsgi_application.py", line 315, in __call__
    self.import_services(self.service_list)
  File "/usr/local/lib/python2.6/dist-packages/ladon-0.7.2-py2.6.egg/ladon/server/wsgi_application.py", line 271, in import_services
    __import__(service)
  File "/var/www/taln/ws/sentokws0.py", line 32, in <module>
    class Sentencizer0(object):
  File "/var/www/taln/ws/sentokws0.py", line 36, in Sentencizer0
    @ladonize(PORTABLE_STRING, rtype = SWS)
  File "/usr/local/lib/python2.6/dist-packages/ladon-0.7.2-py2.6.egg/ladon/ladonizer/decorator.py", line 88, in decorator
    ladon_method_info = global_service_collection().add_service_method(f,*def_args,**def_kw)
  File "/usr/local/lib/python2.6/dist-packages/ladon-0.7.2-py2.6.egg/ladon/ladonizer/collection.py", line 120, in add_service_method
    sinfo = self.source_info(src_fname)
  File "/usr/local/lib/python2.6/dist-packages/ladon-0.7.2-py2.6.egg/ladon/ladonizer/collection.py", line 79, in source_info
    a = ast.parse(src)
  File "/usr/lib/python2.6/ast.py", line 37, in parse
    return compile(expr, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 3

    ^
 SyntaxError: invalid syntax
---8<---

with the following (change split \n to character * to be sure that there is not something interfering during soap call with soapui)

---8<---

#!/bin/env python
# -*- coding: utf-8 -*-

# ------------------------------------------------------------

from ladon.ladonizer import ladonize
from ladon.types.ladontype import LadonType
from ladon.compat import PORTABLE_STRING

# ---------- Singleton

class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

# ---------- for WS result

class Sentence(LadonType):
    words = [ PORTABLE_STRING ]

class SWS(LadonType):
    hasError = bool
    errMessage = PORTABLE_STRING
    sentences = [ Sentence ]

# ---------- WS logic

class Sentencizer0(object):

    __metaclass__ = Singleton

    @ladonize(PORTABLE_STRING, rtype = SWS)
    def sentencize(self, text):
        """Sentencize...
        """
        result = SWS()
        result.errMessage = ''
        result.hasError = False
        result.sentences = []
        #
        sentences = text.split("*")
        subr = []
        for sentence in sentences:
            S = Sentence()
            S.words = sentence.split()
            subr.append(S)
        result.sentences = subr
        return result

import datetime
a = datetime.datetime.now()
sentencizer = Sentencizer0()
b = datetime.datetime.now()
print("loaded: %s." % (b-a))

def example():
    """..."""
    global sentencizer
    global R
    s = u"""aaa bbb ccc *
dd ee f *
ggg *
hhh i jjj kkk"""
    R = sentencizer.sentencize(s)
    for s in R.sentences:
        for x in s.words:
            print x,
        print
    print R.sentences

---8<---

Revision history for this message
jsgaarde (jakob-simon-gaarde) wrote :

It looks like a known bug in Python 2.6's ast module. When you parse a python module in python 2.6 it raises an exception if you have \r\n line-endings. Could that be the case?

/ Jakob

Revision history for this message
ctmidi (ctmidi31) wrote :

about : File "/usr/lib/python2.6/ast.py", line 37, in parse
     return compile(expr, filename, mode, PyCF_ONLY_AST)

yes ! actually it was the point !

i am using windows for creating code and use it on linux box, looks like the ftp transfer was not right in changing \r\n to \n while a dos2Unix correct this ! Many thanks. and also sorry as it is not a Ladon specific.

(one more question, is it possible to have directly [ [ PORTABLE_STRING ]] without having to go to specific LadonType object container)

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.