代码之家  ›  专栏  ›  技术社区  ›  Dror Helper

如何用IronPython/Python给开发人员留下深刻印象[关闭]

  •  10
  • Dror Helper  · 技术社区  · 16 年前

    我需要一个IronPython\Python示例,向C#/VB.NET开发人员展示这种语言到底有多棒。

    我可以使用代码片段或应用程序来演示Python的功能。

    13 回复  |  直到 12 年前
        1
  •  19
  •   Dominic Rodger    16 年前

    彼得·诺维格 spelling corrector

        2
  •  10
  •   Geo    16 年前

    用IronPython重写任何小型C#应用程序,并向他们展示你花了多少行代码。如果这不令人印象深刻,我不知道是什么。

    我指的是你的一个内部应用程序。

        3
  •  6
  •   Robert Rossney    16 年前

    我会快速演示一些琐碎的东西(至少在Python中),但在IDLE中很酷。例如:

    >>> text = # some nice long text, e.g. the Gettysburg Address
    >>> letters = [c.lower() for c in text if c.isalpha()]
    >>> letters
        ['f', 'o', 'u', 'r', 's', 'c', 'o', 'r', 'e', 'a', 'n', 'd', 's', 'e', 'v', 'e',
        ...
    >>> freq = {}
    >>> for c in letters:
            freq[c] = freq.get(c, 0) + 1
    
    >>> freq
        {'a': 102, 'c': 31, 'b': 14, 'e': 165, 'd': 58, 'g': 28, 'f': 27, 'i': 68, 'h': 80, 
        ...
    >>> for c in sorted(freq.keys(), key=lambda x: freq[x], reverse=True):
            print c, freq[c]
    
    e 165
    t 126
    a 102
    ...
    

    这展示了基本的列表和字典类是什么样子的,列表推导是如何工作的,命名参数,lambda表达式,交互式解释器的有用性,它用七行代码完成了一项相当复杂的任务。

    编辑:

    哦,如果你设置了,我会展示代码是如何工作的 letters

    letters = (c.lower() for c in text if c.isalpha())
    

    …也就是说,完全一样。

        4
  •  4
  •   Rohit    16 年前

    在最基础的层面上,你可以用C#和Python展示一个字符串反转程序。

    在C#中:

    public static string ReverseString(string s)
    {
        char[] arr = s.ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }
    

    在Python中:

    s[::-1]
    

        5
  •  4
  •   daftspaniel    16 年前
    import clr
    clr.AddReference('System.Speech')
    clr.AddReference('System.Xml')
    
    from System.Speech.Synthesis import SpeechSynthesizer
    from System.Net import WebClient
    from System.Xml import XmlDocument, XmlTextReader
    
    
    content = WebClient().DownloadString("http://twitter.com/statuses/public_timeline.xml")
    xmlDoc = XmlDocument()
    spk = SpeechSynthesizer()
    
    xmlDoc.LoadXml(content)
    statusesNode = xmlDoc.SelectSingleNode("statuses")
    for status in statusesNode:
        s = "<?xml version=\"1.0\"?><speak version=\"1.0\" xml:lang=\"en-US\"><break/>"
        s = s + status.SelectSingleNode("text").InnerText + "</speak>"
        spk.SpeakSsml(s)
    

    一个会说话的推特客户端。 更多示例 http://www.ironpython.info/index.php/Main_Page

        6
  •  3
  •   Rasmus Kaj    16 年前

    def isprime(n):
        return all(n%x!=0 for x in range(2, int(n**0.5)+1))
    
    def containsPrime(start, limit):
        return any(isPrime(x) for x in xrange(start, limit))
    
        7
  •  3
  •   mavnn    16 年前

    或者一些非常有用的纯python库(在我的工作中,我想到了SqlAlchemy,你的里程可能会有所不同)。

    一些简短的语法也很好,例如:

    快速浏览大型数据集:

    print data[::1000]
    

    查找所有以“a”开头的字符串:

    [s for s in list_of_strings if s.startswith('a')]
    

    the_as = (s for s in really_big_list_of_strings if s.startswith('a'))
    the_as.next()
    
        8
  •  3
  •   Darrell Hawley    16 年前

    我必须同意Geo的观点。在用IronPython编写的同一应用程序旁边显示一个C#或VB应用程序。当我完成IronPython演讲时,我成功地将C#代码转化为Python。这是一场非常戏剧化的演讲。

    我也非常喜欢展示鸭子打字如何使你的代码更具可测试性。

        9
  •  2
  •   dagoof    16 年前

    生成器,定义迭代器,简单

    http://ttsiodras.googlepages.com/yield.html

        10
  •  2
  •   Jason Baker    16 年前

    CherryPy 的helloworld示例:

    import cherrypy
    
    class HelloWorld(object):
        def index(self):
            return "Hello World!"
        index.exposed = True
    
    cherrypy.quickstart(HelloWorld())
    
        11
  •  1
  •   eduffy    16 年前

    素数生成器怎么样。

    >>> def sieve(x):
    ...    if x: return [ x[0] ] + sieve([ y for y in x if y % x[0] > 0 ])
    ...    return []
    ... 
    >>> sieve(range(2,100))
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
    
        12
  •  1
  •   Michael Dillon    16 年前

    向他们展示IronPython烹饪书中的一个例子,比如这个 DataGridView Custom Formatting 。它并不太花哨,但每个人都会熟悉它,因为几乎每个人都已经构建了一个带有网格视图的应用程序(或想要这样做)。

    请确保更改烹饪书中的示例,以显示一些 内置电池 os 模块,用于获取目录列表并用文件名、大小、创建日期等填充网格。

        13
  •  0
  •   Pablo    16 年前

    由于IronPython在运行时向类型添加新成员的能力,这样做的可能性给我留下了深刻的印象

    http://ironpython-resource.com/post/2008/08/23/IronPython-Dynamically-creating-objects-and-binding-them-to-a-form.aspx