Tag: python

算法找到正确的数字集

我将采用c#解决方案的python 我有大约200个数字: 19.16 98.48 20.65 122.08 26.16 125.83 473.33 125.92 3,981.21 16.81 100.00 43.58 54.19 19.83 3,850.97 20.83 20.83 86.81 37.71 36.33 6,619.42 264.53 … … 我知道在这组数字中,有一个数字组合将加起来一定数量让我们说它是2341.42 我怎样才能找出哪些数字组合会加起来呢? 我正在帮助会计人员跟踪正确的数字

通过C ++桥从.NET调用Python

每隔几个月我会谷歌搜索“从.NET调用Python”,因为我太累了,无法重新发明/移植东西并在Python中找到一些很酷的代码。 两种流行的解决方案是启动外部流程或使用IronPython IronPython不适合我,因为我可能想要使用Python的原因通常是一些依赖于NumPy / ScyPy的库。 最近我开始学习C ++的基础知识,并发现可以在C ++中嵌入Python,并找到了一篇带有示例的文章 。 通过P / Invoke从.NET调用C ++非常容易。 所以我的方案是用.NET中的数据调用一些Python脚本并返回结果。 在我开始深入挖掘之前,我想问一下是否有人尝试过这种方法,这是可行的还是有更好的方法? 特别, 所有Python库是否都可以在嵌入时工作,例如NumPy,ScyPy,Pandas,PyMC等? 嵌入式Python是否可以像使用标准C ++一样使用C ++ / CLI? 如何将复杂数据传递给Python:序列化为JSON还是类型之间存在映射? 性能是一个问题:主要用例是在实时数据的滑动窗口上进行一些数字工作。 我认为外部流程对于频繁的通话来说非常慢 – 如果我错了,请纠正我?

C#中的Struct.Pack等效

我正在构建一个连接到渲染应用程序的C#客户端,并且失败了! 我通过剖析一个对此行起作用的python客户端来缩小问题范围: def Startclient_Click(self, sender, e): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, int(port))) message = b’message “Render”‘ msg = struct.pack(‘<l',len(message))+struct.pack('<l',0)+message #print(msg) s.sendall(msg) data = s.recv(1024) data.decode("utf-8") self.datatxt.Text ="data: " +str(data) s.close() return except: self.datatxt.Text ="No Server Connection" return C#中的等价物是什么? 根据我的理解,它需要在消息之前8个字节 抱歉这个愚蠢的问题,但我已经在这几天了,我失去了生活的意愿,谢谢

检索Python脚本的返回值

我有一个外部C#程序,它使用Process类执行Python脚本。 我的脚本返回一个数字代码,我想从我的C#程序中检索它。 这可能吗? 问题是,我得到python.exe的返回码而不是我的脚本返回的代码。 (例如, 3 。)

削减uuid进一步做短串

我需要为给定的唯一字符串生成唯一的记录ID。 我尝试使用uuid格式似乎很好。 但我们觉得这很长。 所以我们需要将uuid字符串9f218a38-12cd-5942-b877-80adc0589315减少到更小。 通过删除’ – ‘我们可以节省4个字符。 从uuid中删除最安全的部分是什么? 我们不需要普遍唯一的id,但我们喜欢使用uuid作为源,但减少了字符串。 我们需要特定于站点/数据库的唯一ID(SQL Server / ADO.NET数据服务)。 任何语言的任何想法或样本都可以 提前致谢

在C#中实现方法装饰器

在python中可以实现function decorators来扩展函数和方法的行为。 特别是我正在将设备库从python迁移到C# 。 与设备的通信可能会产生错误,应该使用自定义exception重新启动。 在python我会这样写: @device_error_wrapper(“Device A”, “Error while setting output voltage.”) def set_voltage(self, voltage): “”” Safely set the output voltage of device. “”” self.__handle.write(“:source:voltage:level {0}”.format(voltage)) 此方法调用将扩展为 try: self.__handle.write(“:source:voltage:level {0}”.format(voltage)) except Error: raise DeviceError(“Error while setting output voltage.”, “DeviceA”) 使用此模式,您可以轻松地包装和扩展方法,而无需在每个方法中编写每个try-except子句。 是否可以使用C#实现类似的模式? 如果需要实现装饰器( device_error_wrapper ),请告诉我。

IronPython:意外的令牌’来自’

我使用IronPython从.net运行python脚本,下面是我的python脚本 import tensorflow as tf print(‘Tensorflow Imported’) 下面是C#代码 using System; using System.Text; using System.IO; using IronPython.Hosting; using System.Collections.Generic; using Microsoft.Scripting.Hosting; namespace ConsoleApplication1 { class Program { private static void Main() { var py = Python.CreateEngine(); List searchPaths = new List(); searchPaths.Add(@”C:\Users\Admin\AppData\Local\Programs\Python\Python35\Lib)”); searchPaths.Add(@”C:\Users\Admin\AppData\Local\Programs\Python\Python35\Lib\site-packages)”); py.SetSearchPaths(searchPaths); try { py.ExecuteFile(“script.py”); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } […]

在不编写文件的情况下在Python和C#之间传递数据

我想在Python和C#之间传递二进制信息。 我假设您可以打开一个标准的输入/输出通道,并像文件一样读写,但是有很多可移动的部分,而且我不太了解C#。 我想做这种事情,但不写文件。 # python code with open(DATA_PIPE_FILE_PATH, ‘wb’) as fid: fid.write(blob) subprocess.Popen(C_SHARP_EXECUTABLE_FILE_PATH) with open(DATA_PIPE_FILE_PATH, ‘rb’) as fid: ‘Do stuff with the data’ // C# code static int Main(string[] args){ byte[] binaryData = File.ReadAllBytes(DataPipeFilePath); byte[] outputData; // Create outputData File.WriteAllBytes(outputData) 我已经尝试了几种使用标准输入/输出的不同方法,但我没有运气匹配它们,就像我说的那样,有许多活动部件。 我尝试过类似的东西 p = subprocess.Popen(C_SHARP_EXECUTABLE_FILE_PATH, stdin=subprocess.PIPE, stdout=subprocess.PIPE) p.stdin.write(blob) p.stdin.close() 要么 p = subprocess.Popen(C_SHARP_EXECUTABLE_FILE_PATH, stdin=subprocess.PIPE, stdout=subprocess.PIPE) […]

什么是部分课程?

它是什么以及如何在C#中使用。 你能在Python / Perl中使用相同的概念吗?

C#:python try / catch / else块的等价物

在Python中,有一个有用的exception处理代码: try: # Code that could raise an exception except Exception: # Exception handling else: # Code to execute if the try block DID NOT fail 我认为能够将可能引发的代码与普通代码分开是很有用的。 在Python中,这可能如上所示,但是我在C#中找不到类似的东西。 假设该特征或类似特征不存在,将标准代码放入try块或catch块之后是标准做法吗? 我问的原因是因为我有以下代码: if (!IsReadOnly) { T newobj; try { newobj = DataPortal.Update(this); List keys = new List(BasicProperties.Keys); foreach (string key in keys) { BasicProperties[key] = newobj.BasicProperties[key]; } […]