小编Vic*_*Yan的帖子

为什么Ruby比Python更适合Rails?

Python和Ruby通常被认为是具有相似表现力和力量的近亲(虽然具有完全不同的历史包袱).但有些人认为Rails框架的巨大成功确实与它所构建的语言有很大关系:Ruby本身.那么为什么Ruby比Python更适合这样的框架呢?

ruby python ruby-on-rails web-frameworks

90
推荐指数
9
解决办法
1万
查看次数

为什么Python生成器中的异常没有被捕获?

我有以下实验代码,其功能类似于zip内置.它试图做的应该是简单明了的,试图一次一个地返回压缩的元组,直到IndexError我们停止发电机时发生.

def my_zip(*args):
    i = 0
    while True:
        try:
            yield (arg[i] for arg in args)
        except IndexError:
            raise StopIteration
        i += 1
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试执行以下代码时,IndexError没有被捕获但是被生成器抛出:

gen = my_zip([1,2], ['a','b'])
print(list(next(gen)))
print(list(next(gen)))
print(list(next(gen)))


IndexError                                Traceback (most recent call last)
I:\Software\WinPython-32bit-3.4.2.4\python-3.4.2\my\temp2.py in <module>()
     12 print(list(next(gen)))
     13 print(list(next(gen)))
---> 14 print(list(next(gen)))

I:\Software\WinPython-32bit-3.4.2.4\python-3.4.2\my\temp2.py in <genexpr>(.0)
      3     while True:
      4         try:
----> 5             yield (arg[i] for arg in args)
      6         except IndexError:
      7             raise StopIteration
IndexError: list index out of range …
Run Code Online (Sandbox Code Playgroud)

python generator python-3.x

21
推荐指数
1
解决办法
1065
查看次数

为什么asyncio库比这个I/O绑定操作的线程慢?

我正在编写一个用于枚举网站域名的python程序.例如,'a.google.com'.

首先,我使用该threading模块执行此操作:

import string
import time
import socket
import threading
from threading import Thread
from queue import Queue

'''
enumerate a site's domain name like this:
1-9 a-z + .google.com
1.google.com
2.google.com
.
.
1a.google.com
.
.
zz.google.com

'''

start = time.time()
def create_host(char):
    '''
    if char is '1-9a-z'
    create char like'1,2,3,...,zz'
    '''
    for i in char:
        yield i
    for i in create_host(char):
        if len(i)>1:
            return False
        for c in char:
            yield c + i


char = string.digits …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-asyncio

19
推荐指数
1
解决办法
7275
查看次数

在Python中使用正则表达式匹配嵌套结构

我似乎记得DotNet中的正则表达式有一个特殊的机制,允许嵌套结构的正确匹配,如" ( (a ( ( c ) b ) ) ( d ) e )"中的分组.

什么是python相当于这个功能?这可以使用正则表达式实现一些解决方法吗?(虽然这似乎是当前正则表达式的实现不是为此而设计的那种问题)

python regex recursive-regex

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

Google App引擎模板unicode解码问题

尝试在Google App Engine中呈现Django模板文件时

来自google.appengine.ext.webapp导入模板

templatepath = os.path.join(os.path.dirname(file),'template.html')
self.response.out.write(template.render(templatepath,template_values))

我遇到以下错误:

<type'exception.UnicodeDecodeError'>:'ascii'编解码器无法解码17692位的字节0xe2:序号不在范围内(128)
args =('ascii','<!DOCTYPE html PUBLIC" - // W3C // DTD XHTML 1.0海峡... 07/A-美丽-方法找到的和平的,心灵/ - >
",17692,17693, '在范围(128)')序数不
编码= 'ASCII'
端= 17693
message =''
object ='<!DOCTYPE html PUBLIC" - // W3C // DTD XHTML 1.0 Str ... 07/a-beautiful-method-to-find-peace-of-mind/ - >
reason ='序数不在范围内(128)'
start = 17692

似乎底层的django模板引擎已经采用了"ascii"编码,它应该是"utf-8".谁知道什么可能导致麻烦以及如何解决?谢谢.

python django unicode google-app-engine

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