你能在IronPython中使用LINQ类型和扩展方法吗?
-
06-07-2019 - |
题
是否可以在IronPython中使用LINQ类型和扩展方法?
如果是这样怎么样?还有更多的pythonic做同样的事情吗?
解决方案
IronPython 2.7最终通过 clr.ImportExtensions
方法填补了这一空白,该方法将扩展方法从命名空间添加到目标类型,例如。
>& 'C:\Program Files\IronPython 2.7\ipy.exe'
IronPython 2.7 (2.7.0.40) on .NET 4.0.30319.225
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> clr.AddReference("System.Core")
>>> from System.Collections.Generic import List
>>> dir (List)
['Add', 'AddRange', 'AsReadOnly', 'BinarySearch', 'Capacity', 'Clear', 'Contains', 'ConvertAll', 'CopyTo', 'Count', 'Enu
merator', 'Equals', 'Exists', 'Find', 'FindAll', 'FindIndex', 'FindLast', 'FindLastIndex', 'ForEach', 'GetEnumerator', '
GetHashCode', 'GetRange', 'GetType', 'IndexOf', 'Insert', 'InsertRange', 'IsReadOnly', 'IsSynchronized', 'Item', 'LastIn
dexOf', 'MemberwiseClone', 'ReferenceEquals', 'Remove', 'RemoveAll', 'RemoveAt', 'RemoveRange', 'Reverse', 'Sort', 'Sync
Root', 'ToArray', 'ToString', 'TrimExcess', 'TrueForAll', '__add__', '__class__', '__contains__', '__delattr__', '__doc_
_', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__len__', '__new__', '__reduce
__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__']
>>> import System
>>> clr.ImportExtensions(System.Linq)
>>> dir (List)
['Add', 'AddRange', 'Aggregate', 'All', 'Any', 'AsEnumerable', 'AsParallel', 'AsQueryable', 'AsReadOnly', 'Average', 'Bi
narySearch', 'Capacity', 'Cast', 'Clear', 'Concat', 'Contains', 'ConvertAll', 'CopyTo', 'Count', 'DefaultIfEmpty', 'Dist
inct', 'ElementAt', 'ElementAtOrDefault', 'Enumerator', 'Equals', 'Except', 'Exists', 'Find', 'FindAll', 'FindIndex', 'F
indLast', 'FindLastIndex', 'First', 'FirstOrDefault', 'ForEach', 'GetEnumerator', 'GetHashCode', 'GetRange', 'GetType',
'GroupBy', 'GroupJoin', 'IndexOf', 'Insert', 'InsertRange', 'Intersect', 'IsReadOnly', 'IsSynchronized', 'Item', 'Join',
'Last', 'LastIndexOf', 'LastOrDefault', 'LongCount', 'Max', 'MemberwiseClone', 'Min', 'OfType', 'OrderBy', 'OrderByDesc
ending', 'ReferenceEquals', 'Remove', 'RemoveAll', 'RemoveAt', 'RemoveRange', 'Reverse', 'Select', 'SelectMany', 'Sequen
ceEqual', 'Single', 'SingleOrDefault', 'Skip', 'SkipWhile', 'Sort', 'Sum', 'SyncRoot', 'Take', 'TakeWhile', 'ToArray', '
ToDictionary', 'ToList', 'ToLookup', 'ToString', 'TrimExcess', 'TrueForAll', 'Union', 'Where', 'Zip', '__add__', '__clas
s__', '__contains__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__',
'__iter__', '__len__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__'
, '__str__', '__subclasshook__']
>>>
使其符合IronRuby 1.1的 using_clr_extensions
方法。
其他提示
您可以使用列表推导完成LINQ的一些操作:
[myFunc(i) for i in numbers if i > 3]
或者你可以使用map,reduce和filter:
map(myFunc, filter(lambda x: x > 3, numbers))
但是列表理解更多的是“Pythonic”。而不是使用函数式编程结构。为减少事情,请考虑使用“”。“
请记住这些翻译:
Select -> map
Where -> filter
Aggregate -> reduce
你会很顺利的!
在 IronPython 2.7.1 中,您有针对此用例的 clr.ImportExtensions
import clr
clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)
# will print 3 and 4 :)
[2, 3, 4].Where(lambda x: x != 2).ToList().ForEach(System.Console.WriteLine)
一点背景: IronPython 2.7 最初引入了这个功能,但是有一个问题阻止它真正可用。
我描述了一个围绕LINQ扩展方法的C#包装器类,以实现类似于IronPython中C#的”链式扩展方法“语法的语法。
我们的想法是在 IEnumerable
周围设置一种简单调用扩展方法的装饰器类。可能这个包装类也可以在IronPython中编写,但我还不能熟练使用python: - )
public class ToLinq<T> : IEnumerable<T>
{
private readonly IEnumerable<T> _wrapped;
public ToLinq(IEnumerable<T> wrapped)
{
_wrapped = wrapped;
}
public ToLinq<T> Where(Func<T, bool> predicate)
{
return new ToLinq<T>(_wrapped.Where(predicate));
}
// ... similar methods for other operators like Select, Count, Any, ...
}
这允许使用与此类似的语法:
johns = ToLinq[Customer](customers)\
.Where(lambda c: c.Name.StartsWith("John"))\
.Select(lambda c: c.Name)
免责声明:这是我尝试作为学习练习的东西,我没有在现实世界的项目中使用它。
不隶属于 StackOverflow