1
Vote

rrule.rulestr()

description

I receive an 'write to closed file' exception when executing a sample console vb.net application. I'm trying to use a dateutil rrule.rulestr() function. Works fine until I add the keyword 'UNTIL' to the string. I set the path for pyEngine.GetSearchPaths() to "C:\Python27\Lib" and "C:\Python27\Lib\site-packages\dateutil"
 
The error seems to be thrown in a call to Pythons parser.py class _timelex(object):
 
Thank You
George
 
Below is my code,
 
Imports System
Imports IronPython.Hosting
Imports Microsoft.Scripting.Hosting
' we get access to Action and Func on .Net 2.0
Imports Microsoft.Scripting.Utils
 
Module Module1
 
Sub Main()
    Dim x As TestCallIronPython.Program = New TestCallIronPython.Program()
    x.main2()
 
End Sub
 
End Module
 
 
 
Namespace TestCallIronPython
 
Public Class Program
 
    Delegate Function ConvertMethod(ByVal inString As String) As String
 
    Public Sub main2()
 
 
 
        Console.WriteLine("Hello World!")
 
        Dim options = New Dictionary(Of String, Object)()
        options("Frames") = True
        options("FullFrames") = True
        options("Debug") = True
 
        Dim pyEngine As ScriptEngine = Python.CreateEngine(options)
        Dim pyScope As ScriptScope = pyEngine.CreateScope()
        pyScope.SetVariable("test", "test me")
 
        Dim dir As String = System.IO.Path.GetDirectoryName("C:\Python27\Lib\site-packages\dateutil")
        Dim paths As System.Collections.Generic.ICollection(Of String) = pyEngine.GetSearchPaths()
        If Not String.IsNullOrEmpty(dir) Then
            paths.Add(dir)
            paths.Add("C:\Python27\Lib")
            paths.Add("C:\Python27\Lib\site-packages\dateutil")
            paths.Add("C:\Python27\Lib\site-packages\dateutil\zoneinfo")
        Else
            paths.Add(System.Environment.CurrentDirectory)
        End If
 
 
        pyEngine.SetSearchPaths(paths)
 
 
        Dim code As String = "" + vbCrLf
        code = code + "import sys" + vbCrLf
        code = code + "import calendar" + vbCrLf
        code = code + "import datetime" + vbCrLf
        code = code + "from dateutil import rrule" + vbCrLf
        code = code + "from dateutil import parser" + vbCrLf
        code = code + "" + vbCrLf
        code = code + "print 'test = ' + test" + vbCrLf
        code = code + "class MyClass:" + vbCrLf
        code = code + "    def __init__(self):" + vbCrLf
        code = code + "        pass" + vbCrLf
        code = code + "" + vbCrLf
        code = code + "    def somemethod(self):" + vbCrLf
        code = code + "        print 'in some method'" + vbCrLf
        code = code + "" + vbCrLf
        code = code + "    def isodd(self, n):" + vbCrLf
        code = code + "        return 1 == n % 2" + vbCrLf
        code = code + "" + vbCrLf
        code = code + "    def getRule(self, data, y, m, d):" + vbCrLf
        code = code + "        print 'This is data: ',data" + vbCrLf
        code = code + "        start_date = datetime.datetime(y, m, d)" + vbCrLf
        code = code + "        return rrule.rrulestr(data, dtstart=start_date)" + vbCrLf
        code = code + "" + vbCrLf
 
        Dim source As ScriptSource = pyEngine.CreateScriptSourceFromString(code)
        Dim compiled As CompiledCode = source.Compile()
        compiled.Execute(pyScope)
 
        ' Get the Python Class
        Dim [MyClass] As Object = pyEngine.Operations.Invoke(pyScope.GetVariable("MyClass"))
        ' Invoke a method of the class
        pyEngine.Operations.InvokeMember([MyClass], "somemethod", New Object(-1) {})
 
        ' create a callable function to 'somemethod'
        Dim SomeMethod2 As Action = pyEngine.Operations.GetMember(Of Action)([MyClass], "somemethod")
        SomeMethod2()
 
        ' create a callable function to 'isodd'
        Dim IsOdd As Func(Of Integer, Boolean) = pyEngine.Operations.GetMember(Of Func(Of Integer, Boolean))([MyClass], "isodd")
        Console.WriteLine(IsOdd(1).ToString())
        Console.WriteLine(IsOdd(2).ToString())
 
        Dim GetRule As Func(Of String, Int32, Int32, Int32, Object) = pyEngine.Operations.GetMember(Of Func(Of String, Int32, Int32, Int32, Object))([MyClass], "getRule")
        Dim obj1 As Object
        obj1 = GetRule("RRULE:FREQ=DAILY;INTERVAL=10;COUNT=42", 2012, 9, 12)
        Dim obj2 As Object
        obj2 = GetRule("RRULE:FREQ=DAILY;INTERVAL=10", 2012, 9, 12)
 
 
        Dim obj3 As Object
        'FAILURE
        obj3 = GetRule("RRULE:FREQ=MONTHLY;BYDAY=+2WE;UNTIL=20130101T000000Z", 2012, 9, 12)  'UNTIL=20150921T000000
        'FAILURE: Does not like "UNTIL" 
 
 
        Console.Write("Press any key to continue . . . ")
        Console.ReadKey(True)
 
 
 
 
    End Sub
End Class
End Namespace

comments