为什么在Python中这样评估: >>> False is False is False True 但是当用括号试图performance如预期的时候: >>> (False is False) is False False
作为一个例子,可以说我想列出string中每个字母的字母的频率。 最简单的方法是什么? 这是我想到的一个例子…问题是如何使allTheLetters等于所说的字母,而不像allTheLetters =“abcdefg … xyz”。 在许多其他语言中,我可以只写字母++,然后按字母顺序递增,但是到目前为止,我还没有碰到过用python做这个的方法。 def alphCount(text): lowerText = text.lower() for letter in allTheLetters: print letter + ":", lowertext.count(letter)
我有一个在Python中的格式:'nn.nnnnn'的string,我想将其转换为一个整数。 直接转换失败: >>> s = '23.45678' >>> i = int(s) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '23.45678' 我可以将其转换为小数使用: >>> from decimal import * >>> d = Decimal(s) >>> print d 23.45678 我也可以拆分'。',然后从零中减去小数,然后将其添加到整数… yuck。 但我宁愿把它作为一个整数,没有不必要的types转换或机动。
from django import forms class UserForm(forms.ModelForm): first_name = forms.TextField(label=_(u'First name'), required=False) last_name = forms.TextField(label=_(u'Last name')) 它给了我一个“ AttributeError:”模块“对象没有属性”TextField“ 。似乎一切都好,除了缺less的TextField: ipdb> forms <module 'django.forms' from '/usr/local/lib/python2.7/dist-packages/django/forms/__init__.pyc'> ipdb> forms. forms.BaseForm forms.EmailField forms.MultiWidget forms.TypedChoiceField forms.BaseModelForm forms.Field forms.MultipleChoiceField forms.TypedMultipleChoiceField forms.BooleanField forms.FileField forms.MultipleHiddenInput forms.URLField forms.CharField forms.FileInput forms.NullBooleanField forms.ValidationError forms.CheckboxInput forms.FilePathField forms.NullBooleanSelect forms.Widget forms.CheckboxSelectMultiple forms.FloatField forms.PasswordInput forms.fields forms.ChoiceField forms.Form forms.RadioSelect forms.fields_for_model forms.ClearableFileInput […]
我的PyCharm不再识别python文件(附加截图)解释器path设置正确。
尝试从维基百科浏览某个页面时遇到了一个奇怪的错误。 这是页面: http://en.wikipedia.org/wiki/OpenCola_(drink) 这是shell会话: >>> f = urllib2.urlopen('http://en.wikipedia.org/wiki/OpenCola_(drink)') Traceback (most recent call last): File "C:\Program Files\Wing IDE 4.0\src\debug\tserver\_sandbox.py", line 1, in <module> # Used internally for debug sandbox under external interpreter File "c:\Python26\Lib\urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "c:\Python26\Lib\urllib2.py", line 397, in open response = meth(req, response) File "c:\Python26\Lib\urllib2.py", line 510, […]
我有一个关于Django的问题。 我在这里有ManyToMany模型 class Product(models.Model): name = models.CharField(max_length=255) price = models.DecimalField(default=0.0, max_digits=9, decimal_places=2) stock = models.IntegerField(default=0) def __unicode__(self): return self.name class Cart(models.Model): customer = models.ForeignKey(Customer) products = models.ManyToManyField(Product, through='TransactionDetail') t_date = models.DateField(default=datetime.now()) t_sum = models.FloatField(default=0.0) def __unicode__(self): return str(self.id) class TransactionDetail(models.Model): product = models.ForeignKey(Product) cart = models.ForeignKey(Cart) amount = models.IntegerField(default=0) 对于创build的1个购物车对象,我可以插入尽可能多的新的TransactionDetail对象(产品和数量)。 我的问题是。 我怎样才能实现触发器? 我想要的是无论何时创build一个交易细节,我都希望产品的库存量减去transactiondetail中的金额。 我读过关于post_save(),但我不知道如何实现它。 也许这样的事情 […]
我使用的是Ubuntu 12.04,我试图用pip来安装virtualenv,但突然间我得到了这个错误。 samuel@sampc:~$ pip install virtualenv Downloading/unpacking virtualenv Running setup.py egg_info for package virtualenv warning: no previously-included files matching '*' found under directory 'docs/_templates' warning: no previously-included files matching '*' found under directory 'docs/_build' Installing collected packages: virtualenv Running setup.py install for virtualenv error: could not create '/usr/local/lib/python2.7/dist-packages/virtualenv_support': Permission denied Complete output from command /usr/bin/python […]
在Python中使用unittest模块时,如何暂时禁用单个unit testing?
我想知道为什么Python不是完全面向对象的。 例如,它不支持私人,公共,受保护的访问级别修改器。 这有什么优点和缺点? 通过这些expression式,Python适用于哪些应用程序(桌面,科学,Web或其他)?