Package org.python.core

Examples of org.python.core.PyDictionary


     * @return PyDictionary globals
     */
    public PyDictionary getGlobals(String text)
    {
        PyModule module = new PyModule( "main",
                                        new PyDictionary( ) );

        PyObject locals = module.__dict__;      

        Py.exec( Py.compile_flags( text,
                                   "<string>",
View Full Code Here


        String type;
        Class clazz;
        int nestedClassPosition;
        int dotPosition;

        PyDictionary dict = new PyDictionary( );

        // dict.setdefault( new PyString( "q" ), qFunc ); //add tenerary
        // function

        RuleBaseContext ruleBaseContext = rule.getRuleSet( ).getRuleBaseContext( );
        ClassLoader cl = (ClassLoader) ruleBaseContext.get( "smf-classLoader" );
        if ( cl == null )
        {
            cl = Thread.currentThread( ).getContextClassLoader( );
            ruleBaseContext.put( "smf-classLoader",
                                 cl );
        }

        if ( cl == null )
        {
            cl = getClass( ).getClassLoader( );
            ruleBaseContext.put( "smf-classLoader",
                                 cl );
        }
       

        while ( declIter.hasNext( ) )
        {
            eachDecl = (Declaration) declIter.next( );

            dict.setdefault( new PyString( eachDecl.getIdentifier( ).intern( ) ),
                             Py.java2py( tuple.get( eachDecl ) ) );

            objectType = eachDecl.getObjectType( );

            if ( objectType instanceof ClassObjectType )
            {
                clazz = ((ClassObjectType) objectType).getType( );
                type = clazz.getName( );

                nestedClassPosition = type.indexOf( '$' );

                if ( nestedClassPosition != -1 )
                {
                    type = type.substring( 0,
                                           nestedClassPosition );
                    clazz = cl.loadClass( type );
                }

                if ( type.indexOf( "java.lang" ) == -1 )
                {
                    dotPosition = type.lastIndexOf( '.' );
                    if ( dotPosition != -1 )
                    {
                        type = type.substring( dotPosition + 1 );
                    }
                    dict.setdefault( new PyString( type.intern( ) ),
                                     Py.java2py( clazz ) );
                }
            }

            WorkingMemory workingMemory = tuple.getWorkingMemory( );

            dict.setdefault( new PyString( "drools".intern( ) ),
                             Py.java2py( new DefaultKnowledgeHelper( this.rule,
                                                                     tuple ) ) );

            Map appDataMap = workingMemory.getApplicationDataMap( );

            for ( Iterator keyIter = appDataMap.keySet( ).iterator( ); keyIter.hasNext( ); )
            {
                String key = (String) keyIter.next( );
                Object value = appDataMap.get( key );

                dict.setdefault( new PyString( key.intern( ) ),
                                 Py.java2py( value ) );

                clazz = value.getClass( );
                type = clazz.getName( );

                nestedClassPosition = type.indexOf( '$' );

                if ( nestedClassPosition != -1 )
                {
                    type = type.substring( 0,
                                           nestedClassPosition );
                    clazz = cl.loadClass( type );
                }

                if ( type.indexOf( "java.lang" ) == -1 )
                {
                    dotPosition = type.lastIndexOf( '.' );
                    if ( dotPosition != -1 )
                    {
                        type = type.substring( dotPosition + 1 );
                    }
                    dict.setdefault( new PyString( type.intern( ) ),
                                     Py.java2py( clazz ) );
                }
            }
        }
View Full Code Here

            for (Tuple bagTuple : (DataBag) object) {
                list.add(pigTupleToPyTuple(bagTuple));
            }
            return list;
        } else if (object instanceof Map<?, ?>) {
            PyDictionary newMap = new PyDictionary();
            for (Map.Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) {
                newMap.put(entry.getKey(), pigToPython(entry.getValue()));
            }
            return newMap;
        } else if (object instanceof DataByteArray) {
            return Py.java2py(((DataByteArray) object).get());
        } else {
View Full Code Here

    }

    public PyDictionary getTestDataArgumentsDictionary(String[] dataNames, int numberDataToSkip, Method method) throws QTasteDataException {
        Class<?>[] parametersClasses = method.getParameterTypes();
        if (parametersClasses.length == dataNames.length) {
            PyDictionary dictionary = new PyDictionary();
            for (int i = numberDataToSkip; i < dataNames.length; i++) {
                String dataName = dataNames[i];
                Class<?> parameterClass = parametersClasses[i];
                try {
                    testData.getValue(dataName);
                } catch (QTasteDataException e) {
                    continue;
                }
                Object dataValue;
                if (parameterClass == String.class || parameterClass == File.class) {
                    dataValue = testData.getValue(dataName);
                } else if (parameterClass == int.class || parameterClass == Integer.class) {
                    dataValue = testData.getIntValue(dataName);
                } else if (parameterClass == double.class || parameterClass == Double.class || parameterClass == float.class || parameterClass == Float.class) {
                    dataValue = testData.getDoubleValue(dataName);
                } else if (parameterClass == boolean.class || parameterClass == Boolean.class) {
                    dataValue = testData.getBooleanValue(dataName);
                } else if (parameterClass == DoubleWithPrecision.class) {
                    dataValue = testData.getDoubleWithPrecisionValue(dataName);
                } else {
                    logger.error("Unsupported method argument type " + parameterClass.getName());
                    continue;
                }
                dictionary.__setitem__(dataName, Py.java2py(dataValue));
            }
            return dictionary;
        } else {
            logger.error("parametersClasses.length != dataNames.length");
            return null;
View Full Code Here

            for (Tuple bagTuple : (DataBag) object) {
                list.add(pigTupleToPyTuple(bagTuple));
            }
            return list;
        } else if (object instanceof Map<?, ?>) {
            PyDictionary newMap = new PyDictionary();
            for (Map.Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) {
                newMap.put(entry.getKey(), pigToPython(entry.getValue()));
            }
            return newMap;
        } else if (object instanceof DataByteArray) {
            return Py.java2py(((DataByteArray) object).get());
        } else {
View Full Code Here

                            page.getResultItems().put(pairs.getKey().toString(), pairs.getValue());
                        }
                        break;
                    case Jython:
                        engine.eval(defines + "\n" + script, context);
                        PyDictionary oJython = (PyDictionary) engine.get("result");
                        Iterator it = oJython.entrySet().iterator();
                        while (it.hasNext()) {
                            Map.Entry pairs = (Map.Entry) it.next();
                            page.getResultItems().put(pairs.getKey().toString(), pairs.getValue());
                        }
                        break;
View Full Code Here

   * Get PyMap from a map column
   *
   * @return PyMap
   */
  public PyDictionary getMap() {
    PyDictionary dict = new PyDictionary();
    dict.putAll(column.getMap());
    return dict;
  }
View Full Code Here

            for (Tuple bagTuple : (DataBag) object) {
                list.add(pigTupleToPyTuple(bagTuple));
            }
            return list;
        } else if (object instanceof Map<?, ?>) {
            PyDictionary newMap = new PyDictionary();
            for (Map.Entry<?, ?> entry : ((Map<?, ?>) object).entrySet()) {
                newMap.put(entry.getKey(), pigToPython(entry.getValue()));
            }
            return newMap;
        } else if (object instanceof DataByteArray) {
            return Py.java2py(((DataByteArray) object).get());
        } else {
View Full Code Here

    public PyObject groupdict(PyObject[] args, String[] kws) {
        ArgParser ap = new ArgParser("groupdict", args, kws, "default");
        PyObject def = ap.getPyObject(0, Py.None);

        PyObject result = new PyDictionary();

        if (pattern.groupindex == null)
            return result;

        PyObject keys = pattern.groupindex.invoke("keys");

        PyObject key;
        for (int i = 0; (key = keys.__finditem__(i)) != null; i++) {
            PyObject item = getslice(key, def);
            result.__setitem__(key, item);
        }
        return result;
    }
View Full Code Here

    PyObject foo = fooClass.__call__();

    PyObject mapResult = getMapFunc.__call__(foo);
    assertTrue(mapResult instanceof PyDictionary);
    PyDictionary pyMapResult = ((PyDictionary) mapResult);
    assertEquals(2, pyMapResult.size());
    Object thirtyTwo = pyMapResult.get("32");
    assertTrue(thirtyTwo instanceof Integer);
    assertEquals(32, ((Integer) thirtyTwo).intValue());
    Object fourPointThree = pyMapResult.get("4.3");
    assertTrue(fourPointThree instanceof Double);
    assertEquals(4.3, (Double) fourPointThree, DELTA);

    PyObject listResult = getListFunc.__call__(foo);
    assertTrue(listResult instanceof PyList);
View Full Code Here

TOP

Related Classes of org.python.core.PyDictionary

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.