我目前教大学一年级学生python,我很惊讶地发现,input我的一些学生已经决定使用(并且被奇怪的行为搞糊涂了)看似无害的功能,正在隐藏eval它背后的电话.
所以我的问题是,input函数调用的原因是eval什么,以及这对哪些函数更有用raw_input呢?我知道Python 3已经改变了,但它首先似乎是一个不寻常的设计决定.
在编写将使用Sphinx处理的RST时,我无法在引用数字时使用Sphinx LaTeX输出来使用图号.例如,这段代码:
The lemmings are attacking, as can be seen in :ref:`figlem`.
.. _figlem:
.. figure:: _static/lemming_invasion.*
They're coming!
Run Code Online (Sandbox Code Playgroud)
将转换成这个:
旅行者正在攻击,正如他们即将到来的那样!
/ image到这里/
图1.1:他们来了!
但我想要的是"标准"LaTeX引用数字的方式,如下所示:
旅鼠正在攻击,如图1.1所示
我该如何实现这一目标?我目前使用的代码是Sphinx手册推荐的代码,但它不会产生我想要的输出.
当我使用Django shell时,它显示错误; 这是错误:
>>> from django.db import models
>>> class Poll(models.Model):
... question = models.CharField(max_length=200)
... pub_date = models.DateTimeField('date published')
...
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "D:\Python25\lib\site-packages\django\db\models\base.py", line 51, in __new__
kwargs = {"app_label": model_module.__name__.split('.')[-2]}
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)
我能做什么?
我的问题与C#Generics中的"默认"类型参数有合理的方法有关吗?,但使用内部泛型类,该方法不起作用.
给出这样的代码:
using System;
public class FooEventArgs<T> : EventArgs
{
// ... T properties and a constructor
}
public class Foo<T>
{
public delegate void EventHandler<FooEventArgs>(object sender, FooEventArgs<T> e);
public event EventHandler<FooEventArgs<T>> Changed
}
Run Code Online (Sandbox Code Playgroud)
并使用它像这样:
public class User
{
public Foo<int> foo1;
public Foo<object> foo2;
public User()
{
foo1 = new Foo<int>();
foo2 = new Foo<object>();
foo1.Changed += foo1_Changed;
foo2.Changed += foo2_Changed;
}
protected void foo1_Changed(object sender, FooEventArgs<int> e) { ... }
protected void foo2_Changed(object sender, …Run Code Online (Sandbox Code Playgroud) 我试图使用Sphinx来记录我的Python类.我这样做是使用autodoc:
.. autoclass:: Bus
:members:
Run Code Online (Sandbox Code Playgroud)
虽然它正确地获取我的方法的文档字符串,那些装饰:
@checkStale
def open(self):
"""
Some docs.
"""
# Code
Run Code Online (Sandbox Code Playgroud)
与@checkStale存在
def checkStale(f):
@wraps(f)
def newf(self, *args, **kwargs):
if self._stale:
raise Exception
return f(self, *args, **kwargs)
return newf
Run Code Online (Sandbox Code Playgroud)
有一个不正确的原型,如open(*args, **kwargs).
我怎样才能解决这个问题?我的印象是使用@wraps会修复这种事情.
除了Twistar之外还有其他异步Python ORM 吗?
我正在寻找基于龙卷风的非阻塞API的轻量级ORM.当然,我可以使用momoko编写原始SQL查询,但我想使用对象.
我试图弄清楚为什么Android的AsyncTask提供参数execute以及为什么传递给构造函数似乎没有完成(至少在文档中).
这是我认为最有意义的方式(当然,我正在使用的实际任务不仅仅是一个总和计算器):
public class FooTask extends AsyncTask<Void, Integer, Long> {
private ProgressBar progressBar;
private int[] data;
public FooTask(ProgressBar progressBar, int... data) {
this.progressBar = progressBar;
this.data = data;
}
protected void onPreExecute() {
progressBar.setMax(data.length);
progressBar.setProgress(0);
}
protected Long doInBackground(Void... _) {
long sum = 0;
for (int i = 0; i < data.length; i++) {
sum += data[i];
publishProgress(i);
}
return sum;
}
protected void onProgressUpdate(Integer... progress) {
progressBar.setProgress(progress[0]);
}
protected void onPostExecute(Long result) {
Log.i(TAG, "Sum: " …Run Code Online (Sandbox Code Playgroud) (或者,"可以使用PyPy/RPython将Python编译/转换为C/C++ 而不需要Python运行时吗?")
我试图通过它的RPython及其Python,它的运行和编译以及它的翻译来理解PyPy,并且有些失败.
我有一个假设的Python项目(适用于Windows); 我想保持它的大小,大约100千字节(ONO),而不是使用py2exe所需的几兆字节(在UPX之后).我可以以任何方式使用PyPy 1来生成不依赖于Python26.dll的独立可执行文件吗?如果可以的话,它是否需要遵循RPython限制,例如for只处理内置类型,还是完整的Python语法?
我确实意识到,如果可以做到这一点,我几乎肯定不能直接使用Python的C模块.
1 (自提问时起,情况变得更加清晰,工具链的这一部分更明确地被称为RPython而不是PyPy; 2010年情况并非如此.)
我已经安装了Sphinx,以便记录一些我正在研究的python模块和类.虽然标记语言看起来非常好,但我还是没有设法自动记录python代码.
基本上,我有以下python模块:
SegLib.py
Run Code Online (Sandbox Code Playgroud)
还有一个叫它的班级Seg.我想在生成的sphinx文档中显示类和模块的文档字符串,并为其添加更多格式化文本.
我index.rst看起来像这样:
Contents:
.. toctree::
:maxdepth: 2
chapter1.rst
Run Code Online (Sandbox Code Playgroud)
并且chapter1.rst:
This is a header
================
Some text, *italic text*, **bold text**
* bulleted list. There needs to be a space right after the "*"
* item 2
.. note::
This is a note.
See :class:`Seg`
Run Code Online (Sandbox Code Playgroud)
但是Seg只是以粗体打印,而不是与类的自动生成的文档相关联.
尝试:参见:class:Seg
Module:mod:'SegLib'模块:mod:'SegLib.py'
也没有帮助.任何想法或良好的教程链接?
编辑:将SegLib更改为段(感谢,iElectric!),并将chapter1.rst更改为
::mod:segmentsModule ------------------------- -
.. automodule:: segments.segments
.. autoclass:: segments.segments.Seg
Run Code Online (Sandbox Code Playgroud)
但是,无法让sphinx直接记录类中的函数,或者更好 - 将类中的所有函数自动添加到文档中.试着
.. autofunction:: segments.segments.Seg.sid …Run Code Online (Sandbox Code Playgroud) 我正在为Sphinx编写一些文档,我想打印出一些仅用于HTML文档的文本块,而不是用于LaTeX文档.有些东西告诉我,我应该能够做到这一点,sphinx.ext.ifconfig但我无法弄清楚如何.有谁知道如何做到这一点?