Tag: python

从IronPython调用C#对象

我有以下C#代码将其编译为MyMath.dll程序集。 namespace MyMath { public class Arith { public Arith() {} public int Add(int x, int y) { return x + y; } } } 我有以下IronPython代码来使用此对象。 import clr clr.AddReferenceToFile(“MyMath.dll”) import MyMath arith = Arith() print arith.Add(10,20) 当我使用IronPython运行此代码时,我收到以下错误。 回溯(最近的呼叫最后): 在初始化中将文件ipycallcs,行未知 NameError:未定义名称“Arith” 可能有什么问题? 添加 arith = Arith()应该是arith = MyMath.Arith()

从C#执行Python脚本

我试图通过以下方式从C#执行python脚本: int ExitCode; ProcessStartInfo ProcessInfo; Process Process; ProcessInfo = new ProcessStartInfo(); ProcessInfo.FileName = “C:\Python27\python.exe”; ProcessInfo.Arguments = “C:\generate.py book1.pdf”; ProcessInfo.CreateNoWindow = true; ProcessInfo.UseShellExecute = false; ProcessInfo.RedirectStandardOutput = true; Process = Process.Start(ProcessInfo); Process.WaitForExit(); ExitCode = Process.ExitCode; Process.Close(); 当我在服务器上执行此操作时,我将ExitCode设置为1.但是相同的代码在本地工作正常。 此外,当我从cmd提示符运行此命令时,python脚本执行没有任何问题。 这个python脚本实际上用于将PDF页面转换为SWF文件,从页面中提取文本并使用各种开源创建pdg页面的缩略图。 任何人都可以帮助我了解上述C#代码可能存在的问题,还是需要在服务器上设置任何权限? 提前致谢,

C#Parallel.Foreach相当于Python

我有96个txt文件需要处理。 现在我正在使用for循环并一次执行一个,这个过程非常慢。 生成的96个文件,不需要合并。 有没有办法使它们并行运行,ala Parallel.foreach在C#中? 当前代码: for src_name in glob.glob(source_dir+’/*.txt’): outfile = open (…) with open(…) as infile: for line in infile: –PROCESS– for –condition–: outfile.write(…) infile.close() outfile.close() 希望此进程并行运行source_dir中的所有文件。

如何在Python中重现System.Security.Cryptography.SHA1Managed结果

这是交易:我正在将.NET网站迁移到Python。 我有一个使用System.Security.Cryptography.SHA1Managed实用程序密码哈希的数据库。 我正在使用以下代码在.NET中创建哈希: string hashedPassword = Cryptographer.CreateHash(“MYHasher”, userInfo.Password); MYHasher块看起来像这样: 因此,对于给定的密码,我回来并在数据库中存储一个48字节的盐渍sha1。 我假设最后8个字节是盐。 我试图通过执行sha1(salt +密码)和sha1(密码+ salt)来重现python中的散列过程,但我没有运气。 我向你提问: 如何使用公钥? 如何使用salt重新密码。 盐是如何产生的? (例如,当我说saltEnabled =“true”时,会发生什么额外的魔法?) 我需要不仅仅引用其他.NET库的具体细节,我正在寻找黑盒中发生的实际操作逻辑。 谢谢!

从c#调用python脚本,意外标记’from’错误

尝试在Visual Studio中开始使用IronPython。 创建了一个C#winforms应用程序来运行python脚本。 从“错误”中获取“意外令牌”。 python脚本包含一行代码: import numpy as np 而已。 我无法理解错误。 我尝试了一个print语句,但是任何导入都会抛出“from”错误。 这是C#代码: var py = Python.CreateEngine(); String main = ; String dir = String.Empty; ICollection paths = py.GetSearchPaths(); paths.Add(dir); dir = main + @”\Lib\site-packages”; paths.Add(dir); dir = main + @”\Lib”; paths.Add(dir); py.SetSearchPaths(paths); py.ExecuteFile(“Test.py”); MessageBox.Show(“Done”); 有谁知道发生了什么事?

如何使用Python for .NET正确嵌入

当我尝试使用时 PythonEngine.ImportModule(mymodulename) 尝试加载某些依赖项中的可选模块(不需要嵌入模块就不需要)。 这导致从此方法返回null,因为其中一些可选依赖项不是必需的,因此不可用。 在这个PythonNET API中使用什么方法来加载依赖于多个其他模块的用户编写的模块?

如何对静态图片流(video)进行动态编码,以便将这些从C#发送到Python?

我使用Kinect SDK( C# )从Kinect 2获得Depth和Color帧,然后使用ZeroMQ将它们发送给Python客户端。 this.shorts = new ushort[ 217088]; // 512 * 424 this.depthBytes = new Byte[ 434176]; // 512 * 424 * 2 this.colorBytes = new Byte[4147200]; // 1920 * 1080 * 4 public void SendDepthFrame(DepthFrame depthFrame) { depthFrame.CopyFrameDataToArray(this.shorts); Buffer.BlockCopy(shorts, 0, this.depthBytes, 0, this.depthBytes.Length); this.depthPublisher.SendByteArray(this.depthBytes); } public void SendColorFrame(ColorFrame colorFrame, WriteableBitmap map) { […]

尝试使用Python从C#代码生成RSA签名

我正在使用别人的代码生成用于在xbox 360保存文件中进行validation的RSA签名。 代码从文件中读取所需的值并正确生成签名。 代码是: byte[] xHash=null; RSAParameters xParams = new RSAParameters(); br.BaseStream.Position = 0x1A8; xParams.D = br.ReadBytes(0x80); xParams.Exponent = br.ReadBytes(0x4); xParams.Modulus = br.ReadBytes(0x80); xParams.P = br.ReadBytes(0x40); xParams.Q = br.ReadBytes(0x40); xParams.DP = br.ReadBytes(0x40); xParams.DQ = br.ReadBytes(0x40); xParams.InverseQ = br.ReadBytes(0x40); br.close(); br=new BinaryReader(File.OpenRead(f)); br.BaseStream.Position=0x22c; xHash = new SHA1CryptoServiceProvider().ComputeHash(br.ReadBytes(0x118)); byte[] xrsa=SignatureGenerate(xParams, xHash); public static byte[] SignatureGenerate(RSAParameters xParam, byte[] […]

在Visual Studio中使用带有库的python文件

我想使用此代码在Visual Studio WPF应用程序中使用一些python文件。 var ipy = Python.CreateRuntime(); dynamic test = ipy.UseFile(“Test.py”); string whatever = test.pythonString() MessageBox.Show(whatever); Test.py看起来像这样: def pythonString: return “Whatever” 一切都按预期工作,但如果我添加任何导入,如 from moviepy.editor import * 对于python代码,消息框不会出现。 为什么要在Visual Studio中使用库使python工作,为什么会发生这种情况? 额外信息:我在Windows 7上使用Visual Studio 2015。 我像这样设置IronPython: https : //blogs.msdn.microsoft.com/charlie/2009/10/25/running-ironpython-scripts-from-ac-4-0-program/

从C#调用Python应用程序/脚本

我正在构建一个ASP.NET MVC(C#)站点,我想在其中实现STV( Single Transferable Vote )投票 。 我之前使用OpenSTV进行投票,取得了巨大成功,但我从未以编程方式使用它。 OpenSTV Google Code项目提供了一个Python脚本 ,允许从其他应用程序使用OpenSTV: import sys sys.path.append(“path to openstv package”) from openstv.ballots import Ballots from openstv.ReportPlugins.TextReport import TextReport from openstv.plugins import getMethodPlugins (ballotFname, method, reportFname) = sys.argv[1:] methods = getMethodPlugins(“byName”) f = open(reportFname, “w”) try: b = Ballots() b.loadUnknown(ballotFname) except Exception, msg: print >> f, (“Unable to […]