我只看过__repr__在类定义中设置方法的示例.是否__repr__可以在定义中或定义后更改for函数?
我试图没有成功......
>>> def f():
pass
>>> f
<function f at 0x1026730c8>
>>> f.__repr__ = lambda: '<New repr>'
>>> f
<function __main__.f>
Run Code Online (Sandbox Code Playgroud) 我已经使用自制软件在新的Mac Lion安装上安装python,并且一直试图用pip安装virtualenv和virtualenvwrapper,但是当我开始一个新的终端会话时,我得到了这个回溯:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named virtualenvwrapper.hook_loader
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenv has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
Run Code Online (Sandbox Code Playgroud)
使用的python和pip来自自制软件,但它似乎要我使用Apple的默认python.我得到以下内容
~$ which python | xargs ls -l
lrwxr-xr-x 1 beard admin 33 Jun 24 16:11 /usr/local/bin/python -> ../Cellar/python/2.7.3/bin/python
~$ echo $VIRTUALENVWRAPPER_PYTHON
/usr/local/bin/python
~$ which pip | xargs ls -l
-rwxr-xr-x …Run Code Online (Sandbox Code Playgroud) 如果我有以下Dataframe
>>> df = pd.DataFrame({'Name': ['Bob'] * 3 + ['Alice'] * 3, \
'Destination': ['Athens', 'Rome'] * 3, 'Length': np.random.randint(1, 6, 6)})
>>> df
Destination Length Name
0 Athens 3 Bob
1 Rome 5 Bob
2 Athens 2 Bob
3 Rome 1 Alice
4 Athens 3 Alice
5 Rome 5 Alice
Run Code Online (Sandbox Code Playgroud)
我可以通过名字和目的地来集合......
>>> grouped = df.groupby(['Name', 'Destination'])
>>> for nm, gp in grouped:
>>> print nm
>>> print gp
('Alice', 'Athens')
Destination Length Name
4 Athens 3 Alice
('Alice', 'Rome') …Run Code Online (Sandbox Code Playgroud) 是否有一些等同于from __future__ import print_function前向端口print来自python 2.x 的语句?
一个涉及一些ipython魔法的答案也可以让我在原型制作过程中不需要周围的parens进行打印.
对于模型
class User(db.Model, BaseUser):
name = CharField()
phone = CharField()
age = IntegerField()
points = IntegerField()
Run Code Online (Sandbox Code Playgroud)
和一个字段列表, lst = ['phone', 'name', 'points']
有没有办法让您的查询返回字段lst?
我在文档中找不到一个例子,但似乎Django的ORM有类似的东西...get().values(lst).
我尝试将列表作为参数传递User.select(),但得到
TypeError: issubclass() arg 1 must be a class
Run Code Online (Sandbox Code Playgroud)
我想我可以做一些像[getattr(obj, field) for field in lst]生成的对象,但似乎应该有更好的方法?
更新:valuesDjango文档中的链接在这里.
有没有办法在启动时在vim中回显自定义消息?我尝试echo "Message"在我的vimrc中使用,但似乎没有出现.
有没有办法使用py2neo迭代neo4j数据库中的每个节点?
我的第一个想法是迭代GraphDatabaseService,但这不起作用.如果没有办法用py2neo做,那么还有另一个python接口可以让我吗?
编辑:我现在正在接受@Nicholas的答案,但如果有人能给我一种返回发电机的方法,我会更新它.
我想用peewee 从csv 创建记录.看起来语法需要关键字args:
user = User.create(username='admin', password='test')
Run Code Online (Sandbox Code Playgroud)
如果csv中的行看起来像(admin, test),那么知道字段名称('username', 'password')以形成要传入的字典会很方便(看起来我不能传入模型的值列表来推断相应的字段是什么).
是否有一个属性User具有按其定义的顺序列出的字段名称?
谢谢
我尝试了几种方法来更改定义中的函数名称,但是它们都失败了.
>>> def f():
pass
>>> f.__name__
'f'
>>> def f():
f.__name__ = 'new name'
>>> f.__name__
'f'
>>> def f():
self.__name__ = 'new name'
>>> f.__name__
'f'
Run Code Online (Sandbox Code Playgroud)
但我可以在定义后更改name属性.
>>> def f():
pass
>>> f.__name__ = 'new name'
>>> f.__name__
'new name'
Run Code Online (Sandbox Code Playgroud)
有什么方法可以在定义中更改/设置它(除了使用装饰器)?
我正在尝试调试Aquamacs.和其他emacsen一样,我刚刚完成了emacs --debug-init,但是当我尝试使用时aquamacs --debug-init,我得到了
touch: illegal option -- -
usage:
touch [-A [-][[hh]mm]SS] [-acfhm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...
open: unrecognized option `--debug-init'
Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames] [--args arguments]
Help: Open opens files from a shell.
By default, opens each file using the default application for that file.
If the file is in the form of a URL, the file will be opened as …Run Code Online (Sandbox Code Playgroud)