小编Rus*_*ell的帖子

如何创建只有一个元素的元组

在下面的例子中,我希望所有元素都是元组,为什么元组只包含一个字符串时转换为字符串?

>>> a = [('a'), ('b'), ('c', 'd')]
>>> a
['a', 'b', ('c', 'd')]
>>> 
>>> for elem in a:
...     print type(elem)
... 
<type 'str'>
<type 'str'>
<type 'tuple'>
Run Code Online (Sandbox Code Playgroud)

python

88
推荐指数
3
解决办法
4万
查看次数

你如何在byobu(tmux)中分离远程屏幕会话?

我目前正处于一个byobu-tmux会话中,并且进入了一个屏幕会话.如何在不分离byobu-tmux会话的情况下分离远程屏幕会话?有些事情需要注意,我不能运行byobu-config,因为我在osx上并且没有安装python-newt(w/snack).并且,我在Emacs模式下运行byobu-ctrl-a,但这似乎不允许我从远程屏幕会话中进行控制.

gnu-screen tmux byobu

23
推荐指数
6
解决办法
3万
查看次数

XSD错误:不允许使用字符内容,因为内容类型为空

我从以下XSD收到验证错误:

<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="People">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="Person" maxOccurs="unbounded">
                 <xsd:complexType>
                    <xsd:attribute name="name" type="xsd:string" use="required" />
                 </xsd:complexType>
             </xsd:element>
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)

使用以下XML进行验证时:

<?xml version="1.0" ?>
<People>
    <Person name='john'>
        a nice person
    </Person>
    <Person name='sarah'>
        a very nice person
    </Person>
    <Person name='chris'>
        the nicest person in the world
   </Person>
</People>
Run Code Online (Sandbox Code Playgroud)

返回以下错误:

lxml.etree.XMLSyntaxError: Element 'Person': Character content is not allowed, because the content type is empty.
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

xml xsd

8
推荐指数
1
解决办法
2万
查看次数

删除仅包含python文件对象的文件

假设我打开一个以前不存在的文件来编写:

f = open('/tmp/test.txt', 'w')
Run Code Online (Sandbox Code Playgroud)

执行此行后,将创建文件'/tmp/test.txt'.删除(删除)仅包含文件对象(f)而不是路径的文件的最简洁方法是什么?

python

6
推荐指数
2
解决办法
1万
查看次数

dict.update会影响函数的argspec吗?

import inspect
class Test:
  def test(self, p, d={}):
    d.update(p)
    return d
print inspect.getargspec(getattr(Test, 'test'))[3]
print Test().test({'1':True})
print inspect.getargspec(getattr(Test, 'test'))[3]
Run Code Online (Sandbox Code Playgroud)

我希望Test.test的argspec不会改变,但是因为dict.update它会改变.为什么?

python inspect

4
推荐指数
1
解决办法
263
查看次数

rails模型中的虚拟属性

在我的控制器中,我在for循环中调用@ hour.shopper.add_product.

我的模型看起来像:

class Shopper < ActiveRecord::Base

  attr_accessor :quantity

  def add_product
     if self.quantity.nil? || \
        self.quantity == 0
      self.quantity = 1 
    else 
      self.quantity += 1
    end 
    self.save
  end 

end
Run Code Online (Sandbox Code Playgroud)

当我打印@ hour.shopper.quantity时,它总是说'nil'.好像它没有在@ hour.shopper对象中保存quantity属性.

提前致谢!

ruby ruby-on-rails

4
推荐指数
1
解决办法
5283
查看次数

将数据传递到django表单

class Test(forms.Form):

    def set_choices(self, choices):
        self.choices = choices

    def get_choices(self):
        return self.choices

    options  = forms.ChoiceField(choices=get_choices())

f = Test()
f.set_choices(...)
Run Code Online (Sandbox Code Playgroud)

为什么这不可能?
如何才能实现将数据传递到Test类的目标?
提前致谢.

python django

3
推荐指数
1
解决办法
2791
查看次数

postgresql删除表

我有两个表(tbl和tbl_new)都使用相同的序列(tbl_id_seq).我想放弃其中一张桌子.在tbl上,我删除了修饰符"not null default nextval('tbl_id_seq':: regclass)"但该修饰符仍保留在tbl_new上.我收到以下错误:

错误:无法删除表tbl,因为其他对象依赖于它DETAIL:表tbl_new列id的默认值取决于序列tbl_id_seq

在查看http://www.postgresql.org/docs/9.1/static/sql-droptable.html之后 看起来只有CASCADE和RESTRICT作为选项.

postgresql

3
推荐指数
1
解决办法
1457
查看次数

luasql零值

我尝试按照http://www.keplerproject.org/luasql/examples.html上的示例进行操作

Lua 5.2.0  Copyright (C) 1994-2011 Lua.org, PUC-Rio
> require "luasql.postgres"
> env = assert (luasql.postgres())
stdin:1: attempt to index global 'luasql' (a nil value)
stack traceback:
    stdin:1: in main chunk
    [C]: in ?
> 
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

lua luasql

3
推荐指数
1
解决办法
2727
查看次数

为什么从ruby中的列表中删除元素在for循环中不起作用?

some_list = ['a', 'b', 'c']

for l in some_list
  some_list.delete_at(some_list.index(l))
end

puts some_list.inspect
Run Code Online (Sandbox Code Playgroud)

事实证明,在执行结束时some_list等于["b"].它不应该删除一切吗?

ruby arrays

2
推荐指数
1
解决办法
1440
查看次数

标签 统计

python ×4

ruby ×2

arrays ×1

byobu ×1

django ×1

gnu-screen ×1

inspect ×1

lua ×1

luasql ×1

postgresql ×1

ruby-on-rails ×1

tmux ×1

xml ×1

xsd ×1