【python – 实现保留docstring的类属性】教程文章相关的互联网学习教程文章

python – 查看功能列表时的Calltips / Docstring?【代码】

我刚刚转到Komodo进行Python编程,到目前为止我很喜欢它.我喜欢如果我键入一个函数名称,然后是open-paren(它会打开calltip / docstring.我也很喜欢如果我键入一个模块名称,然后是.,它会打开一个可用函数列表.我的问题当我有函数列表时,是否有可能弹出calltip / docstring?换句话说,我希望能够在插入之前看到每个函数的作用(docstring)并打开参数列表(.原因是我发现自己需要一个函数,滚动函数列表并插入看起来相关的函数来调出文档字...

python – Matplotlib pyplot.title(string)返回错误【代码】

当我调用pyplot.title(‘some string’)时会抛出异常,’str’对象不可调用’.我从matplotlib在线文档中复制了以下内容:mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000)# the histogram of the data n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis(...

C相当于Python String Slice?【代码】

在python中,我能够切割部分字符串;换句话说,只需在某个位置后打印字符.在C中有相同的吗? Python代码:text= "Apple Pear Orange" print text[6:]会打印:梨橙解决方法:是的,它是substr方法:basic_string substr( size_type pos = 0,size_type count = npos ) const;Returns a substring [pos, pos+count). If the requested substring extends past the end of the string, or if count == npos, the returned substring is [pos...

python – 在Pandas中将列名从int转换为string【代码】

我有一个混合列名的pandas数据帧: 1,2,3,4,5,’班级’ 当我将此数据帧保存到h5file时,它表示由于混合类型,性能将受到影响.如何在pandas中将整数转换为字符串?解决方法:你可以简单地使用df.columns = df.columns.astype(str):In [26]: df = pd.DataFrame(np.random.random((3,6)), columns=[1,2,3,4,5,'Class'])In [27]: df Out[27]: 1 2 3 4 5 Class 0 0.773423 0.865091 0.614956 0.2...

python – 确定Shapely点是否在LineString / MultiLineString中【代码】

我试图在函数内使用Shapely来进行LineString和Point文件的“空间连接”(FYI,点文件是使用LineString上的插值函数生成的).问题是 – 什么都没有归还.# this condition is never satisfied if point.within(line):# here I write stuff to a file哪里:point = POINT (-9763788.9782693591000000 5488878.3678984242000000) line = LINESTRING (-9765787.998118492 5488940.974948905, -9748582.801636808 5488402.127570709)我错过了...

python – PySpark:TypeError:condition应该是string或Column【代码】

我试图过滤基于如下的RDD:spark_df = sc.createDataFrame(pandas_df) spark_df.filter(lambda r: str(r['target']).startswith('good')) spark_df.take(5)但是得到了以下错误:TypeErrorTraceback (most recent call last) <ipython-input-8-86cfb363dd8b> in <module>()1 spark_df = sc.createDataFrame(pandas_df) ----> 2 spark_df.filter(lambda r: str(r['target']).startswith('good'))3 spark_df.take(5)/usr/local/spark-...

Python Enum类(使用tostring fromstring)【代码】

我找到了一种简单的方法来实现(破解)枚举到Python:class MyEnum:VAL1, VAL2, VAL3 = range(3)我可以这样称呼它:bob = MyEnum.VAL1性感! 好吧,现在我希望能够在给定字符串时获得数值,或者如果给定数值则获得字符串.假设我希望字符串与Enum键完全匹配 我能想到的最好的是这样的:class MyEnum:VAL1, VAL2, VAL3 = range(3)@classmethoddef tostring(cls, val):if (val == cls.VAL1):return "VAL1"elif (val == cls.VAL2):return "...

在Python中只获取String中的第一个Number【代码】

我目前面临的问题是我有一个字符串,我只想提取第一个数字.我的第一步是从字符串中提取数字.Headline = "redirectDetail('27184','2 -New-York-Explorer-Pass')" print (re.findall('\d+', headline )) Output is ['27184', '2']在这种情况下它返回了两个数字,但我只想要第一个“27184”. 因此,我尝试使用以下代码:print (re.findall('/^[^\d]*(\d+)/', headline ))但它不起作用:Output:[]你能帮助我吗?任何反馈都表示赞赏解决方...

Python将set转换为string,反之亦然【代码】

设置为字符串.明显:>>> s = set([1,2,3]) >>> s set([1, 2, 3]) >>> str(s) 'set([1, 2, 3])'要设置的字符串?也许是这样的?>>> set(map(int,str(s).split('set([')[-1].split('])')[0].split(','))) set([1, 2, 3])非常难看.是否有更好的序列化/反序列化集的方法?解决方法:使用repr和eval:>>> s = set([1,2,3]) >>> strs = repr(s) >>> strs 'set([1, 2, 3])' >>> eval(strs) set([1, 2, 3])请注意,如果字符串的来源未知,则ev...

什么放在python模块docstring?【代码】

好的,所以我已经阅读了PEP 8和PEP 257,并且我已经为函数和类编写了很多文档字符串,但是我对模块docstring中的内容有点不确定.我想,至少它应该记录模块导出的函数和类,但我也看到了一些列出作者姓名,版权信息等的模块.有没有人有一个好的python文档字符串如何应该的例子结构化?解决方法:想想有人在交互式口译员的提示下做帮助(你的模块) – 他们想知道什么? (提取和显示信息的其他方法大致相当于信息量方面的帮助).所以如果你有x....

python – create_string_buffer抛出错误TypeError:str / bytes而不是str实例【代码】

我正在尝试这个简单的ctypes示例并获得提到的错误>>> from ctypes import create_string_buffer >>> str = create_string_buffer("hello") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python32\lib\ctypes\__init__.py", line 59, in create_string_buffer buf.value = init TypeError: str/bytes expected instead of str instance有谁知道我做错了什么? 在同一个注释中,我试图从我的pyt...

Python相当于unix“strings”实用程序【代码】

我正在尝试编写一个脚本,它将从可执行二进制文件中提取字符串并将其保存在文件中.将此文件换行换行不是一种选择,因为字符串本身可能有换行符.然而,这也意味着使用unix“strings”实用程序不是一个选项,因为它只打印出所有新行分隔的字符串,这意味着只能通过查看输出来判断哪些字符串包含换行符. “弦”.因此,我希望找到一个python函数或库,它实现了“strings”的相同功能,但它会将这些字符串作为变量,这样我就可以避免换行问题. 谢...

在Python中删除String中的引号【代码】

我有一个python代码,可以使用谷歌STT引擎识别语音并给我回复结果,但我得到的结果是带有“引号”的字符串.我不想在我的代码中使用引号,因为我将使用它来运行许多命令,但它不起作用.到目前为止,我没有尝试任何东西,因为我没有尝试任何东西!这是python代码中将识别语音的函数:def recog():p = subprocess.Popen(['./speech-recog.sh'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)global out,errout, err = p.communicate()pri...

python – 当测试string ==各种OR’ed替代品时总是如此【代码】

所以我目前遇到的问题是我的程序总是调用我已定义的’md5cypher’类,即使输入不在该列表中:def enc():global toeif toe=='md5' or 'M' or 'm' or 'Md5' or 'MD5':print("Md5 Encryption Cypher")md5cypher()else:print("Sha1 Encryption Cypher")shacypher()我究竟做错了什么?解决方法:实际上你正在检查:if (toe=='md5') or 'M' or 'm' or....因为bool(‘M’)是True,所以你总能成功检查.试试这个:if toe.lower() in ('md5', 'm...

python3(三十六)StringIO BytesIO【代码】

""" StringIO和BytesIO """ __author__on__ = shaozhiqi 2019/9/23# !/usr/bin/env python3 # -*- coding: utf-8 -*-# 很多时候,数据读写不一定是文件,也可以在内存中读写。 # StringIO顾名思义就是在内存中读写str。 # 要把str写入StringIO,我们需要先创建一个StringIO,然后,像文件一样写入即可: from io import StringIOf = StringIO() f.write(hello) f.write( ) f.write(world!) print(f.getvalue()) # hello world!# g...