Tag: python

用python将目录内容复制到一个目录

我有一个目录/ a / b / c有文件和子目录。 我需要复制/ x / y / z目录中的/ a / b / c / *。 我可以使用哪些python方法? 我尝试了shutil.copytree("a/b/c", "/x/y/z") ,但python试图创build/ x / y / z并引发一个error "Directory exists" 。

Django反向查找外键

我有一个场地,这个场地有许多事件发生在那里。 我的模型是这样的: class Event(models.Model): title = models.CharField(max_length=200) date_published = models.DateTimeField('published date',default=datetime.now, blank=True) date_start = models.DateTimeField('start date') date_end = models.DateTimeField('end date') def __unicode__(self): return self.title description = models.TextField() price = models.IntegerField(null=True, blank=True) venue = models.ForeignKey(Venue) class Venue(models.Model): title = models.CharField(max_length=200) date_published = models.DateTimeField('published date',default=datetime.now, blank=True) venue_latitude = models.CharField(max_length=200) venue_longitude = models.CharField(max_length=200) venue_address = models.CharField(max_length=200) venue_city = […]

PyMongo的集合对象是不可调用的错误

遵循PyMongo 教程 ,在集合上调用insert_one方法时出现错误。 In [1]: import pymongo In [2]: from pymongo import MongoClient In [3]: client = MongoClient() In [4]: db = client.new_db In [5]: db Out[5]: Database(MongoClient('localhost', 27017), u'new_db') In [6]: posts = db.posts In [7]: posts.insert_one({'a':1}) ————————————————————————— TypeError Traceback (most recent call last) <ipython-input-7-2271c01f9a85> in <module>() —-> 1 posts.insert_one({'a':1}) C:\Anaconda\lib\site-packages\pymongo-2.8-py2.7-win32.egg\pymongo\collection.py in __call__(self, *a […]

在Python中保留内存列表?

当用Python进行编程时,是否可以为列表预留内存,列表中将填充已知数量的项目,以便在构build列表时不会多次重新分配列表? 我查看了Python列表types的文档,并没有发现任何似乎这样做的东西。 然而,这种types的列表build筑出现在我的代码的几个热点,所以我想尽可能高效。 编辑:另外,它甚至是有意义的,像Python这样的语言做这样的事情? 我是一个相当有经验的程序员,但是对于Python来说还是一个新手,并且仍然感觉到它的做事方式。 Python是否内部分配所有的对象在不同的​​堆空间中,破坏了试图最小化分配的目的,还是直接存储在列表中的int,float等原语?

如何用Python C API创build一个生成器/迭代器?

如何使用Python C API复制以下Python代码? class Sequence(): def __init__(self, max): self.max = max def data(self): i = 0 while i < self.max: yield i i += 1 到目前为止,我有这样的: #include <Python/Python.h> #include <Python/structmember.h> /* Define a new object class, Sequence. */ typedef struct { PyObject_HEAD size_t max; } SequenceObject; /* Instance variables */ static PyMemberDef Sequence_members[] = { […]

具有常规属性(不是关系)的SQLAlchemy DetachedInstanceError

我刚开始使用SQLAlchemy并得到一个DetachedInstanceError,并且无法在任何地方find很多信息。 我在会话外部使用实例,所以很自然的是,如果SQLAlchemy没有加载任何关系,它是无法加载的,但是我访问的属性并不是一个关系,事实上这个对象根本就没有关系。 我发现解决scheme,如急于加载,但我不能适用于此,因为这不是一个关系。 我甚至在closures会话之前试着“触摸”这个属性,但是它仍然不能防止这个exception。 甚至在它被成功访问一次之前,什么可能导致这个非关系属性的exception呢? 任何帮助在debugging这个问题表示赞赏。 我将同时尝试获得可重复的独立场景并在此处进行更新。 更新:这是几个堆栈的实际exception消息: File "/home/hari/bin/lib/python2.6/site-packages/SQLAlchemy-0.6.1-py2.6.egg/sqlalchemy/orm/attributes.py", line 159, in __get__ return self.impl.get(instance_state(instance), instance_dict(instance)) File "/home/hari/bin/lib/python2.6/site-packages/SQLAlchemy-0.6.1-py2.6.egg/sqlalchemy/orm/attributes.py", line 377, in get value = callable_(passive=passive) File "/home/hari/bin/lib/python2.6/site-packages/SQLAlchemy-0.6.1-py2.6.egg/sqlalchemy/orm/state.py", line 280, in __call__ self.manager.deferred_scalar_loader(self, toload) File "/home/hari/bin/lib/python2.6/site-packages/SQLAlchemy-0.6.1-py2.6.egg/sqlalchemy/orm/mapper.py", line 2323, in _load_scalar_attributes (state_str(state))) DetachedInstanceError: Instance <ReportingJob at 0xa41cd8c> is not bound to a Session; attribute refresh operation […]

Objective-C(cocoa)相当于python的endswith / beginwith

Python有string.startswith()和string.endswith()函数,这是非常有用的。 我可以使用哪些NSString方法来实现相同的function?

哪一个我应该使用:os.sep或os.path.sep?

他们是一样的,但我应该使用哪一个? http://docs.python.org/library/os.html : os.sep 操作系统用来分隔path名组件的字符。 这是POSIX的“/”和Windows的“\”。 请注意,知道这不足以parsing或连接path名 – 使用os.path.split()和os.path.join() – 但它有时是有用的。 也可以通过os.path获得。

什么,在Python意味着什么?

我想知道是什么,=或, =意味着在Python? 来自matplotlib的示例: plot1, = ax01.plot(t,yp1,'b-')

如何从Python脚本中获取当前的Python解释器path?

我想从一个Python脚本运行一个Python脚本和subprocess ,我希望使用相同的解释器为他们每个人做。 我使用的是virtualenv,所以我想要做一些事情: subprocess.Popen('%s script.py' % python_bin) 我如何得到python_bin ? 它应该是virtualenv之外的/usr/bin/python ,以及virtualenv中的/path/to/env/bin/python 。