Page 1 of 2

Python standard library

PostPosted: Sat Apr 18, 2009 3:58 pm
by arisawa
When I coding the cobra and want to use a Python standard library, is there the way?

Re: Python standard library

PostPosted: Sun Apr 19, 2009 11:40 am
by Charles
Not currently. Someday Cobra will support Microsoft's Dynamic Language Runtime (DLR), using it for dynamic dispatch. Then you should be able to use pure Python libraries through IronPython. But we don't have that right now.

Re: Python standard library

PostPosted: Sat Apr 25, 2009 1:39 am
by Charles
By the way, was there a particular library or libraries that you wanted to use from Python? Just curious.

Re: Python standard library

PostPosted: Sun Apr 26, 2009 3:00 am
by arisawa
Hi, Chuck.
It is a glob module that I wanted to use.

Re: Python standard library

PostPosted: Thu May 21, 2009 3:55 am
by arisawa
sorry, off topic

Does not Cobra follow it by built-in function of Python?
Does Cobra have pound-bang line? the example?

because I learn Cobra, Python and C# in side by side, I meet with such a confusion and questions well.

Re: Python standard library

PostPosted: Thu May 21, 2009 9:57 pm
by Charles
arisawa wrote:sorry, off topic
These questions are fine.

arisawa wrote:Does not Cobra follow it by built-in function of Python?
Cobra uses the .NET/Mono libraries. So if you want to find the built-in functions for something you can search Google for "C# read directories" or whatever you like. Eventually, we'll have a cross platform library that runs on both .NET and JVM, but that won't be for awhile.

arisawa wrote:Does Cobra have pound-bang line? the example?
It's supposed to, but I just tried "#!/usr/bin/cobra" on my Mac and it failed for reasons that I researched before. Basically, unix is not very flexible in allowing the pound-bang command to be another script which is what "/usr/bin/cobra" is. I was able to do this though:
#!/bin/bash cobra

class X

def main
print 'Hello.'
Or if you prefer, you can make the path to cobra absolute:
#!/bin/bash /usr/bin/cobra
...

arisawa wrote:because I learn Cobra, Python and C# in side by side, I meet with such a confusion and questions well.
No problem.

Re: Python standard library

PostPosted: Wed May 27, 2009 9:55 pm
by eric.sellon
Just for fun, here's a sample Cobra program calling into IronPython

Code: Select all
use IronPython.Hosting
use IronPython.Runtime.Types
use IronPython.Runtime.Operations

class Program

    def main is shared
        """ Test docstring """
        engine = PythonEngine()
        code = 'print "This is IronPython"'
        engine.execute(code)


I called this program CallingIronPython.cobra. I used IronPython 1.1.0 (the API changed with IronPython 2.0) and had IronPython.dll and IronMath.dll in the same directory as the cobra file. Ran Cobra with the command line: cobra -r:IronPython.dll CallingIronPython
A good resource (especially if you want to work with IronPython 2.0) is http://www.voidspace.org.uk/ironpython/embedding.shtml
Hope that helps.

Re: Python standard library

PostPosted: Thu May 28, 2009 12:27 am
by Charles
That's great. Would make a nice wiki page. hint, hint. ;-)

Re: Python standard library

PostPosted: Fri May 29, 2009 10:53 pm
by eric.sellon
Chuck wrote:Would make a nice wiki page. hint, hint. ;-)


Done. Made a Cobra Cookbook on the wiki and added this recipe -> http://cobra-language.com/trac/cobra/wiki/CallingIronPythonRecipe

Re: Python standard library

PostPosted: Sat May 30, 2009 2:46 am
by arisawa
Wow! It is a helpful idea. :D
I wrote a sample to try:
use System.Windows.Forms
use IronPython.Hosting

class Calc inherits Form
var tb as TextBox
var pe as PythonEngine = PythonEngine()
var s as Str = Str()

cue init
base.init
.text = 'SimpleCalc'
flp = FlowLayoutPanel(parent=this, dock=DockStyle.Fill, flowDirection=FlowDirection.TopDown)
.tb = TextBox(parent=flp)
b = Button(text='done', parent=flp)
listen b.click, ref .handleClick
.pe.globals.add('TextBox', .tb)
.pe.globals.add('Str', .s)

def handleClick(sender, args as EventArgs)
.pe.execute('TextBox.Text =' + 'Str.Pack('+.tb.text+')')

class Str
def pack(n as int) as String
return n.toString

class Program
def main is shared
Application.run(Calc())

In the tried feeling
Python code seems to run if it does in this way:
engine = PythonEngine()
file = 'test.py'
engine.executeFile(file)

but, still rocky.