Tag: python

在ipython笔记本中绘制宽度设置

我有以下的情节: 如果它们具有相同的宽度,它会更好看。 你有任何想法如何在ipython笔记本中使用%matplotlib inline ? 更新: 要生成这两个数字我使用以下function: import numpy as np import matplotlib.pyplot as plt def show_plots2d(title, plots, points, xlabel = '', ylabel = ''): """ Shows 2D plot. Arguments: title : string Title of the plot. plots : array_like of pairs like array_like and array_like List of pairs, where first element is x axis […]

Python在句子上分割文本

我有一个文本文件。 我需要得到一个句子列表。 这怎么能被执行? 有很多微妙之处,比如缩写中使用的点。 我的旧正则expression式工作不好。 re.compile('(\. |^|!|\?)([AZ][^;↑\.<>@\^&/\[\]]*(\.|!|\?) )',re.M)

如果不立即重新生成exception追踪,则隐藏

我有一个类似于这样的代码: import sys def func1(): func2() def func2(): raise Exception('test error') def main(): err = None try: func1() except: err = sys.exc_info()[1] pass # some extra processing, involving checking err details (if err is not None) # need to re-raise err so caller can do its own handling if err: raise err if __name__ == […]

使用TLS请求不支持SNI

我正在使用请求与django应用程序通信,但是 当我尝试 requests.get('https://mysite.com', verify=True) 我得到的错误: 主机名“mysite.com”与“* .myhost.com”,“myhost.com” 但是,当我浏览器,或http://www.digicert.com/help/证书看起来很好,花花公子。 我的主机build议,由于缺乏来自请求的SNI支持(Github似乎确认https://github.com/kennethreitz/requests/issues/749 )。 有没有人find使用请求的解决方法?

如何在primefaces编辑器中隐藏* pyc文件

开始使用https://atom.io/来回Python / Django开发,并希望隐藏侧边栏中的所有*.pyc文件。 如何configuration?

在Python shell中使用参数执行一个文件

我想在Python Shell中运行一个命令来执行一个带有参数的文件。 例如: execfile("abc.py")但如何添加2个参数?

你怎么能设置python中的variables参数(kwargs)的类属性

假设有一个具有构造函数(或其他函数)的类,它接受可变数量的参数,然后有条件地将它们设置为类属性。 我可以手动设置它们,但似乎variables参数在python中已经足够普遍,应该有一个常见的习惯用法。 但我不知道如何dynamic地做到这一点。 我有一个使用eval的例子,但这不是安全的。 我想知道这样做的正确方法 – 也许与lambda? class Foo: def setAllManually(self, a=None, b=None, c=None): if a!=None: self.a = a if b!=None: self.b = b if c!=None: self.c = c def setAllWithEval(self, **kwargs): for key in **kwargs: if kwargs[param] != None eval("self." + key + "=" + kwargs[param])

在Python 3中将int转换为字节

我试图在Python 3中构build这个字节对象: b'3\r\n' 所以我尝试了明显的(对于我),并发现一个奇怪的行为: >>> bytes(3) + b'\r\n' b'\x00\x00\x00\r\n' 显然: >>> bytes(10) b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 我一直无法看到为什么字节转换以这种方式读取文档的任何指针。 不过,我在这个Python问题中发现了一些关于向字节添加format惊喜消息(另请参阅Python 3字节格式 ): http://bugs.python.org/issue3982 这与现在返回零的字节(int)之类的奇怪相互作用甚至更差 和: 如果bytes(int)返回了那个int的ASCII值,那对我来说会更方便; 但说实话,即使一个错误会比这个行为更好。 (如果我想要这种行为 – 我从来没有 – 我宁愿它是一个类方法,调用像“bytes.zeroes(n)”。) 有人能解释我这个行为的来源吗?

TypeError:ObjectId('')不是JSON可序列化的

我在使用Python查询文档上的聚合函数后,从MongoDB返回的响应,它返回有效的响应,我可以打印它,但不能返回它。 错误: TypeError: ObjectId('51948e86c25f4b1d1c0d303c') is not JSON serializable 打印: {'result': [{'_id': ObjectId('51948e86c25f4b1d1c0d303c'), 'api_calls_with_key': 4, 'api_calls_per_day': 0.375, 'api_calls_total': 6, 'api_calls_without_key': 2}], 'ok': 1.0} 但是,当我尝试返回: TypeError: ObjectId('51948e86c25f4b1d1c0d303c') is not JSON serializable 这是RESTfull电话: @appv1.route('/v1/analytics') def get_api_analytics(): # get handle to collections in MongoDB statistics = sldb.statistics objectid = ObjectId("51948e86c25f4b1d1c0d303c") analytics = statistics.aggregate([ {'$match': {'owner': objectid}}, {'$project': {'owner': "$owner", […]

DateTimeField不显示在pipe理系统中

我的“date”字段怎么没有出现在pipe理系统中? 在我的admin.py文件中 from django.contrib import admin from glasses.players.models import * admin.site.register(Rating) 和评级模型有一个叫做“date”的字段,看起来像这样 date = models.DateTimeField(editable=True, auto_now_add=True) 但是,在pipe理系统中,即使editable设置为True ,该字段也不会显示。 有人有什么主意吗?