Forums

Python standard library

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Python standard library

Postby arisawa » Sat Apr 18, 2009 3:58 pm

When I coding the cobra and want to use a Python standard library, is there the way?
arisawa
 
Posts: 51

Re: Python standard library

Postby Charles » Sun Apr 19, 2009 11:40 am

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Python standard library

Postby Charles » Sat Apr 25, 2009 1:39 am

By the way, was there a particular library or libraries that you wanted to use from Python? Just curious.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Python standard library

Postby arisawa » Sun Apr 26, 2009 3:00 am

Hi, Chuck.
It is a glob module that I wanted to use.
arisawa
 
Posts: 51

Re: Python standard library

Postby arisawa » Thu May 21, 2009 3:55 am

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.
arisawa
 
Posts: 51

Re: Python standard library

Postby Charles » Thu May 21, 2009 9:57 pm

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Python standard library

Postby eric.sellon » Wed May 27, 2009 9:55 pm

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.
eric.sellon
 
Posts: 24

Re: Python standard library

Postby Charles » Thu May 28, 2009 12:27 am

That's great. Would make a nice wiki page. hint, hint. ;-)
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Python standard library

Postby eric.sellon » Fri May 29, 2009 10:53 pm

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
eric.sellon
 
Posts: 24

Re: Python standard library

Postby arisawa » Sat May 30, 2009 2:46 am

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.
arisawa
 
Posts: 51

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 45 guests