打印测试执行时间并使用py.test固定慢速测试

问题:打印测试执行时间并使用py.test固定慢速测试

我正在使用py.test在CI服务器上运行单元测试。测试使用通过网络获取的外部资源。有时测试跑步者花费的时间太长,导致测试跑步者被中止。我不能在本地重复这些问题。

有没有一种方法可以使py.test打印出(缓慢)测试的执行时间,因此固定有问题的测试变得更加容易?

I am running unit tests on a CI server using py.test. Tests use external resources fetched over network. Sometimes test runner takes too long, causing test runner to be aborted. I cannot repeat the issues locally.

Is there a way to make py.test print out execution times of (slow) test, so pinning down problematic tests become easier?


回答 0

我不确定这是否可以解决您的问题,但是您可以在测试套件完成后通过--durations=N以打印最慢的N测试。

使用--durations=0打印所有。

I’m not sure this will solve your problem, but you can pass --durations=N to print the slowest N tests after the test suite finishes.

Use --durations=0 to print all.


回答 1

您可以使用 --durations

pytest --durations=0 — Show all times for tests and setup and teardown

pytest --durations=1 — Just show me the slowest

pytest --durations=50 — Slowest 50, with times,  etc

请参阅:https : //medium.com/@brianokken/pytest-durations-0-show-all-times-for-tests-and-setup-and-teardown-848dccac85db

或者:https : //docs.pytest.org/en/latest/usage.html#profiling-test-execution-duration

You can pass the number with --durations

pytest --durations=0 — Show all times for tests and setup and teardown

pytest --durations=1 — Just show me the slowest

pytest --durations=50 — Slowest 50, with times, … etc

Take refer in: https://medium.com/@brianokken/pytest-durations-0-show-all-times-for-tests-and-setup-and-teardown-848dccac85db

Or: https://docs.pytest.org/en/latest/usage.html#profiling-test-execution-duration


字符串如何串联?

问题:字符串如何串联?

如何在python中连接字符串?

例如:

Section = 'C_type'

将其与Sec_形成字符串:

Sec_C_type

How to concatenate strings in python?

For example:

Section = 'C_type'

Concatenate it with Sec_ to form the string:

Sec_C_type

回答 0

最简单的方法是

Section = 'Sec_' + Section

但为了提高效率,请参阅:https : //waymoot.org/home/python_string/

The easiest way would be

Section = 'Sec_' + Section

But for efficiency, see: https://waymoot.org/home/python_string/


回答 1

您也可以这样做:

section = "C_type"
new_section = "Sec_%s" % section

这样,您不仅可以追加,还可以在字符串中的任意位置插入:

section = "C_type"
new_section = "Sec_%s_blah" % section

you can also do this:

section = "C_type"
new_section = "Sec_%s" % section

This allows you not only append, but also insert wherever in the string:

section = "C_type"
new_section = "Sec_%s_blah" % section

回答 2

只是一条评论,就像有人可能会发现它很有用-您可以一次连接多个字符串:

>>> a='rabbit'
>>> b='fox'
>>> print '%s and %s' %(a,b)
rabbit and fox

Just a comment, as someone may find it useful – you can concatenate more than one string in one go:

>>> a='rabbit'
>>> b='fox'
>>> print '%s and %s' %(a,b)
rabbit and fox

回答 3

连接字符串的更有效方法是:

加入():

效率很高,但有点难读。

>>> Section = 'C_type'  
>>> new_str = ''.join(['Sec_', Section]) # inserting a list of strings 
>>> print new_str 
>>> 'Sec_C_type'

字符串格式:

易于阅读,在大多数情况下比“ +”级联更快

>>> Section = 'C_type'
>>> print 'Sec_%s' % Section
>>> 'Sec_C_type'

More efficient ways of concatenating strings are:

join():

Very efficent, but a bit hard to read.

>>> Section = 'C_type'  
>>> new_str = ''.join(['Sec_', Section]) # inserting a list of strings 
>>> print new_str 
>>> 'Sec_C_type'

String formatting:

Easy to read and in most cases faster than ‘+’ concatenating

>>> Section = 'C_type'
>>> print 'Sec_%s' % Section
>>> 'Sec_C_type'

回答 4

使用+字符串连接为:

section = 'C_type'
new_section = 'Sec_' + section

Use + for string concatenation as:

section = 'C_type'
new_section = 'Sec_' + section

回答 5

要在python中连接字符串,请使用“ +”号

参考:http : //www.gidnetwork.com/b-40.html

To concatenate strings in python you use the “+” sign

ref: http://www.gidnetwork.com/b-40.html


回答 6

对于附加到现有字符串末尾的情况:

string = "Sec_"
string += "C_type"
print(string)

结果是

Sec_C_type

For cases of appending to end of existing string:

string = "Sec_"
string += "C_type"
print(string)

results in

Sec_C_type

如何包括外部Python代码以在其他文件中使用?

问题:如何包括外部Python代码以在其他文件中使用?

如果文件中有一组方法,是否可以将这些文件包含在另一个文件中,但不带任何前缀(即文件前缀)调用它们?

所以,如果我有:

[Math.py]
def Calculate ( num )

我怎么这样称呼它:

[Tool.py]
using Math.py

for i in range ( 5 ) :
    Calculate ( i )

If you have a collection of methods in a file, is there a way to include those files in another file, but call them without any prefix (i.e. file prefix)?

So if I have:

[Math.py]
def Calculate ( num )

How do I call it like this:

[Tool.py]
using Math.py

for i in range ( 5 ) :
    Calculate ( i )

回答 0

您将需要将其他文件作为模块导入,如下所示:

import Math

如果您不想在Calculate函数名称前加上模块名称,请执行以下操作:

from Math import Calculate

如果要导入模块的所有成员,请执行以下操作:

from Math import *

编辑: 这是Dive Into Python 的精彩一章,在该主题上有更深入的介绍。

You will need to import the other file as a module like this:

import Math

If you don’t want to prefix your Calculate function with the module name then do this:

from Math import Calculate

If you want to import all members of a module then do this:

from Math import *

Edit: Here is a good chapter from Dive Into Python that goes a bit more in depth on this topic.


回答 1

只需编写“ include”命令:

import os

def include(filename):
    if os.path.exists(filename): 
        execfile(filename)


include('myfile.py')

@Deleet:

@bfieck的评论是正确的,对于python 2和3的兼容性,您需要:

Python 2和3:替代方法1

from past.builtins import execfile

execfile('myfile.py')

Python 2和3:替代2

exec(compile(open('myfile.py').read()))

Just write the “include” command :

import os

def include(filename):
    if os.path.exists(filename): 
        execfile(filename)


include('myfile.py')

@Deleet :

@bfieck remark is correct, for python 2 and 3 compatibility, you need either :

Python 2 and 3: alternative 1

from past.builtins import execfile

execfile('myfile.py')

Python 2 and 3: alternative 2

exec(compile(open('myfile.py').read()))

回答 2

如果您使用:

import Math

那么这将允许您使用Math的函数,但是您必须执行Math.Calculate,因此显然这是您不想要的。

如果要导入模块的功能而不必加上前缀,则必须显式命名它们,例如:

from Math import Calculate, Add, Subtract

现在,您可以仅按其名称引用“计算”​​,“添加”和“减去”。如果要从Math导入ALL函数,请执行以下操作:

from Math import *

但是,对不确定内容的模块执行此操作时应非常小心。如果导入两个包含相同功能名称定义的模块,则一个功能将覆盖另一个功能,而没有一个则更明智。

If you use:

import Math

then that will allow you to use Math’s functions, but you must do Math.Calculate, so that is obviously what you don’t want.

If you want to import a module’s functions without having to prefix them, you must explicitly name them, like:

from Math import Calculate, Add, Subtract

Now, you can reference Calculate, Add, and Subtract just by their names. If you wanted to import ALL functions from Math, do:

from Math import *

However, you should be very careful when doing this with modules whose contents you are unsure of. If you import two modules who contain definitions for the same function name, one function will overwrite the other, with you none the wiser.


回答 3

我发现python inspect模块非常有用

例如,使用teststuff.py

import inspect

def dostuff():
    return __name__

DOSTUFF_SOURCE = inspect.getsource(dostuff)

if __name__ == "__main__":

    dostuff()

并从另一个脚本或python控制台

import teststuff

exec(DOSTUFF_SOURCE)

dostuff()

现在dostuff应该在本地范围内,并且dostuff()将返回控制台或脚本_ name _,而执行test.dostuff()将返回python模块名称。

I’ve found the python inspect module to be very useful

For example with teststuff.py

import inspect

def dostuff():
    return __name__

DOSTUFF_SOURCE = inspect.getsource(dostuff)

if __name__ == "__main__":

    dostuff()

And from the another script or the python console

import teststuff

exec(DOSTUFF_SOURCE)

dostuff()

And now dostuff should be in the local scope and dostuff() will return the console or scripts _name_ whereas executing test.dostuff() will return the python modules name.


列表理解甚至在理解范围之后也会重新绑定名称。这是正确的吗?

问题:列表理解甚至在理解范围之后也会重新绑定名称。这是正确的吗?

理解与范围界定存在一些意外的相互作用。这是预期的行为吗?

我有一个方法:

def leave_room(self, uid):
  u = self.user_by_id(uid)
  r = self.rooms[u.rid]

  other_uids = [ouid for ouid in r.users_by_id.keys() if ouid != u.uid]
  other_us = [self.user_by_id(uid) for uid in other_uids]

  r.remove_user(uid) # OOPS! uid has been re-bound by the list comprehension above

  # Interestingly, it's rebound to the last uid in the list, so the error only shows
  # up when len > 1

冒着抱怨的危险,这是错误的残酷来源。在编写新代码时,偶尔会由于重新绑定而发现非常奇怪的错误-即使现在我知道这是一个问题。我需要制定一条规则,例如“始终在下划线的列表理解中使用temp vars开头”,但是即使这样也不是万无一失的。

这种随机定时炸弹等待的事实否定了列表推导的所有“易用性”。

Comprehensions are having some unexpected interactions with scoping. Is this the expected behavior?

I’ve got a method:

def leave_room(self, uid):
  u = self.user_by_id(uid)
  r = self.rooms[u.rid]

  other_uids = [ouid for ouid in r.users_by_id.keys() if ouid != u.uid]
  other_us = [self.user_by_id(uid) for uid in other_uids]

  r.remove_user(uid) # OOPS! uid has been re-bound by the list comprehension above

  # Interestingly, it's rebound to the last uid in the list, so the error only shows
  # up when len > 1

At the risk of whining, this is a brutal source of errors. As I write new code, I just occasionally find very weird errors due to rebinding — even now that I know it’s a problem. I need to make a rule like “always preface temp vars in list comprehensions with underscore”, but even that’s not fool-proof.

The fact that there’s this random time-bomb waiting kind of negates all the nice “ease of use” of list comprehensions.


回答 0

列表推导泄漏了Python 2中的循环控制变量,但没有泄漏到Python 3中。这里是Guido van Rossum(Python的创建者)解释了其背后的历史:

我们还对Python 3进行了另一处更改,以改善列表理解与生成器表达式之间的等效性。在Python 2中,列表理解将“循环”控制变量“泄漏”到周围的范围内:

x = 'before'
a = [x for x in 1, 2, 3]
print x # this prints '3', not 'before'

这是列表理解的原始实现的产物。多年来,它一直是Python的“肮脏的小秘密”之一。它起初是一种有意的折衷,目的是使列表理解迅速变得盲目,虽然对于初学者来说这不是一个常见的陷阱,但它肯定偶尔会刺痛人们。对于生成器表达式,我们无法执行此操作。生成器表达式是使用生成器实现的,生成器的执行需要单独的执行框架。因此,生成器表达式(特别是如果它们在短序列上进行迭代)比列表理解的效率低。

但是,在Python 3中,我们决定通过使用与生成器表达式相同的实现策略来修复列表理解的“肮脏的小秘密”。因此,在Python 3中,上述示例(修改为使用print(x):-之后)将打印“ before”,证明列表理解中的“ x”会暂时遮盖阴影,但不会覆盖周围的“ x”范围。

List comprehensions leak the loop control variable in Python 2 but not in Python 3. Here’s Guido van Rossum (creator of Python) explaining the history behind this:

We also made another change in Python 3, to improve equivalence between list comprehensions and generator expressions. In Python 2, the list comprehension “leaks” the loop control variable into the surrounding scope:

x = 'before'
a = [x for x in 1, 2, 3]
print x # this prints '3', not 'before'

This was an artifact of the original implementation of list comprehensions; it was one of Python’s “dirty little secrets” for years. It started out as an intentional compromise to make list comprehensions blindingly fast, and while it was not a common pitfall for beginners, it definitely stung people occasionally. For generator expressions we could not do this. Generator expressions are implemented using generators, whose execution requires a separate execution frame. Thus, generator expressions (especially if they iterate over a short sequence) were less efficient than list comprehensions.

However, in Python 3, we decided to fix the “dirty little secret” of list comprehensions by using the same implementation strategy as for generator expressions. Thus, in Python 3, the above example (after modification to use print(x) :-) will print ‘before’, proving that the ‘x’ in the list comprehension temporarily shadows but does not override the ‘x’ in the surrounding scope.


回答 1

是的,列表理解在Python 2.x中“泄漏”其变量,就像for循环一样。

回想起来,这被认为是错误的,生成器表达式可以避免这种情况。编辑:正如 Matt B.指出的那样,从Python 3向后移植set和dictionary comprehension语法时,也避免了这种情况。

列表推导的行为必须保留在Python 2中,但在Python 3中已完全修复。

这意味着:

list(x for x in a if x>32)
set(x//4 for x in a if x>32)         # just another generator exp.
dict((x, x//16) for x in a if x>32)  # yet another generator exp.
{x//4 for x in a if x>32}            # 2.7+ syntax
{x: x//16 for x in a if x>32}        # 2.7+ syntax

这些x始终是表达式的局部变量,而这些条件是:

[x for x in a if x>32]
set([x//4 for x in a if x>32])         # just another list comp.
dict([(x, x//16) for x in a if x>32])  # yet another list comp.

在Python 2.x中,所有x变量都会泄漏到周围的范围内。


Python 3.8(?)的更新PEP 572将引入:=赋值运算符,该运算符故意泄漏出理解力和生成器表达式!它主要是由两个用例驱动的:从早期终止的功能(如any()和)中捕获“见证” all()

if any((comment := line).startswith('#') for line in lines):
    print("First comment:", comment)
else:
    print("There are no comments")

并更新可变状态:

total = 0
partial_sums = [total := total + v for v in values]

有关准确的作用域,请参见附录B。除非在函数中声明了或,否则变量会在最接近的def或中分配。lambdanonlocalglobal

Yes, list comprehensions “leak” their variable in Python 2.x, just like for loops.

In retrospect, this was recognized to be a mistake, and it was avoided with generator expressions. EDIT: As Matt B. notes it was also avoided when set and dictionary comprehension syntaxes were backported from Python 3.

List comprehensions’ behavior had to be left as it is in Python 2, but it’s fully fixed in Python 3.

This means that in all of:

list(x for x in a if x>32)
set(x//4 for x in a if x>32)         # just another generator exp.
dict((x, x//16) for x in a if x>32)  # yet another generator exp.
{x//4 for x in a if x>32}            # 2.7+ syntax
{x: x//16 for x in a if x>32}        # 2.7+ syntax

the x is always local to the expression while these:

[x for x in a if x>32]
set([x//4 for x in a if x>32])         # just another list comp.
dict([(x, x//16) for x in a if x>32])  # yet another list comp.

in Python 2.x all leak the x variable to the surrounding scope.


UPDATE for Python 3.8(?): PEP 572 will introduce := assignment operator that deliberately leaks out of comprehensions and generator expressions! It’s motivated by essentially 2 use cases: capturing a “witness” from early-terminating functions like any() and all():

if any((comment := line).startswith('#') for line in lines):
    print("First comment:", comment)
else:
    print("There are no comments")

and updating mutable state:

total = 0
partial_sums = [total := total + v for v in values]

See Appendix B for exact scoping. The variable is assigned in closest surrounding def or lambda, unless that function declares it nonlocal or global.


回答 2

是的,分配就在那里发生,就像for循环一样。没有新的作用域被创建。

这绝对是预期的行为:在每个循环中,该值都绑定到您指定的名称。例如,

>>> x=0
>>> a=[1,54,4,2,32,234,5234,]
>>> [x for x in a if x>32]
[54, 234, 5234]
>>> x
5234

一旦意识到这一点,似乎很容易避免:不要在理解范围内使用现有名称作为变量。

Yes, assignment occurs there, just like it would in a for loop. No new scope is being created.

This is definitely the expected behavior: on each cycle, the value is bound to the name you specify. For instance,

>>> x=0
>>> a=[1,54,4,2,32,234,5234,]
>>> [x for x in a if x>32]
[54, 234, 5234]
>>> x
5234

Once that’s recognized, it seems easy enough to avoid: don’t use existing names for the variables within comprehensions.


回答 3

有趣的是,这不会影响字典或设置理解力。

>>> [x for x in range(1, 10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x
9
>>> {x for x in range(1, 5)}
set([1, 2, 3, 4])
>>> x
9
>>> {x:x for x in range(1, 100)}
{1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15, 16: 16, 17: 17, 18: 18, 19: 19, 20: 20, 21: 21, 22: 22, 23: 23, 24: 24, 25: 25, 26: 26, 27: 27, 28: 28, 29: 29, 30: 30, 31: 31, 32: 32, 33: 33, 34: 34, 35: 35, 36: 36, 37: 37, 38: 38, 39: 39, 40: 40, 41: 41, 42: 42, 43: 43, 44: 44, 45: 45, 46: 46, 47: 47, 48: 48, 49: 49, 50: 50, 51: 51, 52: 52, 53: 53, 54: 54, 55: 55, 56: 56, 57: 57, 58: 58, 59: 59, 60: 60, 61: 61, 62: 62, 63: 63, 64: 64, 65: 65, 66: 66, 67: 67, 68: 68, 69: 69, 70: 70, 71: 71, 72: 72, 73: 73, 74: 74, 75: 75, 76: 76, 77: 77, 78: 78, 79: 79, 80: 80, 81: 81, 82: 82, 83: 83, 84: 84, 85: 85, 86: 86, 87: 87, 88: 88, 89: 89, 90: 90, 91: 91, 92: 92, 93: 93, 94: 94, 95: 95, 96: 96, 97: 97, 98: 98, 99: 99}
>>> x
9

但是,如上所述,已将其固定为3。

Interestingly this doesn’t affect dictionary or set comprehensions.

>>> [x for x in range(1, 10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x
9
>>> {x for x in range(1, 5)}
set([1, 2, 3, 4])
>>> x
9
>>> {x:x for x in range(1, 100)}
{1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15, 16: 16, 17: 17, 18: 18, 19: 19, 20: 20, 21: 21, 22: 22, 23: 23, 24: 24, 25: 25, 26: 26, 27: 27, 28: 28, 29: 29, 30: 30, 31: 31, 32: 32, 33: 33, 34: 34, 35: 35, 36: 36, 37: 37, 38: 38, 39: 39, 40: 40, 41: 41, 42: 42, 43: 43, 44: 44, 45: 45, 46: 46, 47: 47, 48: 48, 49: 49, 50: 50, 51: 51, 52: 52, 53: 53, 54: 54, 55: 55, 56: 56, 57: 57, 58: 58, 59: 59, 60: 60, 61: 61, 62: 62, 63: 63, 64: 64, 65: 65, 66: 66, 67: 67, 68: 68, 69: 69, 70: 70, 71: 71, 72: 72, 73: 73, 74: 74, 75: 75, 76: 76, 77: 77, 78: 78, 79: 79, 80: 80, 81: 81, 82: 82, 83: 83, 84: 84, 85: 85, 86: 86, 87: 87, 88: 88, 89: 89, 90: 90, 91: 91, 92: 92, 93: 93, 94: 94, 95: 95, 96: 96, 97: 97, 98: 98, 99: 99}
>>> x
9

However it has been fixed in 3 as noted above.


回答 4

不适用于python 2.6的一些解决方法

# python
Python 2.6.6 (r266:84292, Aug  9 2016, 06:11:56)
Type "help", "copyright", "credits" or "license" for more information.
>>> x=0
>>> a=list(x for x in xrange(9))
>>> x
0
>>> a=[x for x in xrange(9)]
>>> x
8

some workaround, for python 2.6, when this behaviour is not desirable

# python
Python 2.6.6 (r266:84292, Aug  9 2016, 06:11:56)
Type "help", "copyright", "credits" or "license" for more information.
>>> x=0
>>> a=list(x for x in xrange(9))
>>> x
0
>>> a=[x for x in xrange(9)]
>>> x
8

回答 5

在python3中,当在列表理解中时,变量的作用域超出范围后并不会发生变化,但是当我们使用简单的for循环时,变量会被重新分配到作用域之外。

i = 1 print(i)print([i in range(5)])print(i)i的值将仅保留1。

现在,仅使用for循环即可重新分配i的值。

In python3 while in list comprehension the variable is not getting change after it’s scope over but when we use simple for-loop the variable is getting reassigned out of scope.

i = 1 print(i) print([i in range(5)]) print(i) Value of i will remain 1 only.

Now just use simply for loop the value of i will be reassigned.


Python中的raw_input函数

问题:Python中的raw_input函数

功能是什么raw_input?是用户界面吗?我们什么时候使用它?

What is the raw_input function? Is it a user interface? When do we use it?


回答 0

它提出了一个提示给用户(可选argraw_input([arg])),从用户获得输入,并通过在一个字符串的用户返回数据的输入。请参阅有关的文档raw_input()

例:

name = raw_input("What is your name? ")
print "Hello, %s." % name

这与input() 后者的不同之处在于后者试图解释用户给出的输入。通常最好避免input()并坚持使用raw_input()自定义的解析/转换代码。

注意:这适用于Python 2.x

It presents a prompt to the user (the optional arg of raw_input([arg])), gets input from the user and returns the data input by the user in a string. See the docs for raw_input().

Example:

name = raw_input("What is your name? ")
print "Hello, %s." % name

This differs from input() in that the latter tries to interpret the input given by the user; it is usually best to avoid input() and to stick with raw_input() and custom parsing/conversion code.

Note: This is for Python 2.x


回答 1

raw_input()input()在Python 3中被重命名为。

来自http://docs.python.org/dev/py3k/whatsnew/3.0.html

raw_input() was renamed to input() in Python 3.

From http://docs.python.org/dev/py3k/whatsnew/3.0.html


回答 2

“输入”功能将您输入的输入转换为Python代码。“ raw_input”不会转换输入,而是按照给定的方式接受输入。建议对所有内容使用raw_input。用法:

>>a = raw_input()
>>5
>>a
>>'5'

The “input” function converts the input you enter as if it were python code. “raw_input” doesn’t convert the input and takes the input as it is given. Its advisable to use raw_input for everything. Usage:

>>a = raw_input()
>>5
>>a
>>'5'

回答 3

raw_input是一种输入形式,采用字符串形式的参数,而input函数则根据您的输入来采用值。假设a = input(5)返回a作为值为5的整数,而a = raw_input(5)返回a作为字符串“ 5”

raw_input is a form of input that takes the argument in the form of a string whereas the input function takes the value depending upon your input. Say, a=input(5) returns a as an integer with value 5 whereas a=raw_input(5) returns a as a string of “5”


回答 4

如果需要使代码更简单,则另一个示例方法是使用print混合提示。

格式:-

x = raw_input()-这将以字符串形式返回用户输入

x = int(raw_input())-从raw_input()以字符串形式获取输入数字,然后使用int()将其转换为整数。

print '\nWhat\'s your name ?', 
name = raw_input('--> ')
print '\nHow old are you, %s?' % name,
age = int(raw_input())
print '\nHow tall are you (in cms), %s?' % name,
height = int(raw_input())
print '\nHow much do you weigh (in kgs), %s?' % name,
weight = int(raw_input())

print '\nSo, %s is %d years old, %d cms tall and weighs %d kgs.\n' %(
name, age, height, weight)

Another example method, to mix the prompt using print, if you need to make your code simpler.

Format:-

x = raw_input () — This will return the user input as a string

x= int(raw_input()) — Gets the input number as a string from raw_input() and then converts it to an integer using int().

print '\nWhat\'s your name ?', 
name = raw_input('--> ')
print '\nHow old are you, %s?' % name,
age = int(raw_input())
print '\nHow tall are you (in cms), %s?' % name,
height = int(raw_input())
print '\nHow much do you weigh (in kgs), %s?' % name,
weight = int(raw_input())

print '\nSo, %s is %d years old, %d cms tall and weighs %d kgs.\n' %(
name, age, height, weight)

回答 5

如果我让raw_input这样,就不要Josh或其他任何东西。我想这是一个变量,但我不理解她的角色:-(

raw_input函数提示您输入,并以字符串形式返回。这肯定对我有用。您不需要闲置。只需打开“ DOS提示符”并运行程序。

这就是我的样子:

C:\temp>type test.py
print "Halt!"
s = raw_input("Who Goes there? ")
print "You may pass,", s

C:\temp>python test.py
Halt!
Who Goes there? Magnus
You may pass, Magnus

[Enter在程序打印“谁去那里?”之后,我键入我的名字并按]。

If I let raw_input like that, no Josh or anything else. It’s a variable,I think,but I don’t understand her roll :-(

The raw_input function prompts you for input and returns that as a string. This certainly worked for me. You don’t need idle. Just open a “DOS prompt” and run the program.

This is what it looked like for me:

C:\temp>type test.py
print "Halt!"
s = raw_input("Who Goes there? ")
print "You may pass,", s

C:\temp>python test.py
Halt!
Who Goes there? Magnus
You may pass, Magnus

I types my name and pressed [Enter] after the program had printed “Who Goes there?”


与python中的“意外缩进”怎么办?

问题:与python中的“意外缩进”怎么办?

如何纠正python中的“意外缩进”错误?

How do I rectify the error “unexpected indent” in python?


回答 0

Python在行的开头使用空格来确定代码块的开始和结束时间。您可以获得的错误是:

意外缩进。这行代码的开头比以前的空格多,但前面的不是子块的开头(例如if / while / for语句)。块中的所有代码行必须以完全相同的空格字符串开头。例如:

>>> def a():
...   print "foo"
...     print "bar"
IndentationError: unexpected indent

当以交互方式运行python时,这一点尤其常见:请确保不要在命令前放置任何多余的空格。(在复制粘贴示例代码时非常烦人!)

>>>   print "hello"
IndentationError: unexpected indent

Unindent与任何外部缩进级别都不匹配。这行代码的开头空格比以前的空格少,但是同样,它与它可能包含的任何其他块也不匹配。Python无法决定其去向。例如,在下面,最终的打印是否应该包含在if子句中?

>>> if user == "Joey":
...     print "Super secret powers enabled!"
...   print "Revealing super secrets"
IndendationError: unindent does not match any outer indentation level

预期缩进的块。这行代码的开头与前面的空格数量相同,但是最后一行应开始一个块(例如,if / while / for语句,函数定义)。

>>> def foo():
... print "Bar"
IndentationError: expected an indented block

如果您想要一个不执行任何操作的函数,请使用“ no-op”命令传递

>>> def foo():
...     pass

允许混合使用制表符和空格(至少在我的Python版本中),但是Python假定制表符的长度为8个字符,可能与您的编辑器不匹配。只需对标签说“不”即可。大多数编辑器允许将它们自动替换为空格。

避免这些问题的最佳方法是在缩进子块时始终使用一致数量的空格,并且理想情况下使用可以为您解决问题的良好IDE。这也将使您的代码更具可读性。

Python uses spacing at the start of the line to determine when code blocks start and end. Errors you can get are:

Unexpected indent. This line of code has more spaces at the start than the one before, but the one before is not the start of a subblock (e.g. if/while/for statement). All lines of code in a block must start with exactly the same string of whitespace. For instance:

>>> def a():
...   print "foo"
...     print "bar"
IndentationError: unexpected indent

This one is especially common when running python interactively: make sure you don’t put any extra spaces before your commands. (Very annoying when copy-and-pasting example code!)

>>>   print "hello"
IndentationError: unexpected indent

Unindent does not match any outer indentation level. This line of code has fewer spaces at the start than the one before, but equally it does not match any other block it could be part of. Python cannot decide where it goes. For instance, in the following, is the final print supposed to be part of the if clause, or not?

>>> if user == "Joey":
...     print "Super secret powers enabled!"
...   print "Revealing super secrets"
IndendationError: unindent does not match any outer indentation level

Expected an indented block. This line of code has the same number of spaces at the start as the one before, but the last line was expected to start a block (e.g. if/while/for statement, function definition).

>>> def foo():
... print "Bar"
IndentationError: expected an indented block

If you want a function that doesn’t do anything, use the “no-op” command pass:

>>> def foo():
...     pass

Mixing tabs and spaces is allowed (at least on my version of Python), but Python assumes tabs are 8 characters long, which may not match your editor. Just say “no” to tabs. Most editors allow them to be automatically replaced by spaces.

The best way to avoid these issues is to always use a consistent number of spaces when you indent a subblock, and ideally use a good IDE that solves the problem for you. This will also make your code more readable.


回答 1

在Python中,间距非常重要,这给出了代码块的结构。当您弄乱代码结构时会发生此错误,例如:

def test_function() :
   if 5 > 3 :
   print "hello"

您的文件中可能还会包含选项卡和空格。

我建议您使用python语法感知编辑器,例如PyScripterNetbeans

In Python, the spacing is very important, this gives the structure of your code blocks. This error happens when you mess up your code structure, for example like this :

def test_function() :
   if 5 > 3 :
   print "hello"

You may also have a mix of tabs and spaces in your file.

I suggest you use a python syntax aware editor like PyScripter, or Netbeans


回答 2

使用-tt选项运行您的代码,以查明您是否不一致地使用了制表符和空格

Run your code with the -tt option to find out if you are using tabs and spaces inconsistently


回答 3

在使用的任何编辑器中打开可见的空白,并打开带空格的替换选项卡。

虽然可以将选项卡与Python混合使用,但通常将选项卡和空格混合会导致您遇到错误。建议使用4个空格替换制表符,这是编写Python代码的推荐方法。

Turn on visible whitespace in whatever editor you are using and turn on replace tabs with spaces.

While you can use tabs with Python mixing tabs and space usually leads to the error you are experiencing. Replacing tabs with 4 spaces is the recommended approach for writing Python code.


回答 4

通过使用正确的缩进。Python具有空格意识,因此您需要按照其块的缩进指南进行操作,否则会出现缩进错误。

By using correct indentation. Python is whitespace aware, so you need to follow its indentation guidlines for blocks or you’ll get indentation errors.


回答 5

如果您使用Sublime编写Python并出现缩进错误,

查看->缩进->将缩进转换为空格

我描述的问题是由Sublime文本编辑器引起的。同样的问题也可能由其他编辑器引起。从本质上讲,这个问题与Python希望以空格来处理缩进有关,而各种编辑器都以制表符来编写缩进。

If you’re writing Python using Sublime and getting indentation errors,

view -> indentation -> convert indentation to spaces

The issue I’m describing is caused by the Sublime text editor. The same issue could be caused by other editors as well. Essentially, the issue has to do with Python wanting to treat indentations in terms of spaces versus various editors coding the indentations in terms of tabs.


回答 6

确保在编辑器中使用选项“插入空格而不是制表符”。然后,您可以选择所需的制表符宽度,例如4。您可以在gedit中的edit-> preferences-> editor下找到这些选项。

底线:使用空间而不是选项卡

Make sure you use the option “insert spaces instead of tabs” in your editor. Then you can choose you want a tab width of, for example 4. You can find those options in gedit under edit–>preferences–>editor.

bottom line: USE SPACES not tabs


回答 7

将某些内容粘贴到Python解释器(终端/控制台)中时,也会发生此错误。

请注意,解释器会将空行解释为表达式的末尾,因此如果您粘贴类似

def my_function():
    x = 3

    y = 7

解释器将在解释之前将空行解释y = 7为表达式的末尾,也就是说,您已经完成了对函数的定义,而下一行- y = 7由于它是新表达式,因此缩进不正确。

This error can also occur when pasting something into the Python interpreter (terminal/console).

Note that the interpreter interprets an empty line as the end of an expression, so if you paste in something like

def my_function():
    x = 3

    y = 7

the interpreter will interpret the empty line before y = 7 as the end of the expression, i.e. that you’re done defining your function, and the next line – y = 7 will have incorrect indentation because it is a new expression.


回答 8

似乎没有提到的一个问题是,由于与缩进无关的代码问题,此错误可能会出现。

例如,使用以下脚本:

def add_one(x):
    try:
        return x + 1
add_one(5)

IndentationError: unexpected unindent当问题当然是缺少except:语句时,这将返回。

我的观点:检查上面报告意外(un)缩进的代码!

One issue which doesn’t seem to have been mentioned is that this error can crop up due to a problem with the code that has nothing to do with indentation.

For example, take the following script:

def add_one(x):
    try:
        return x + 1
add_one(5)

This returns an IndentationError: unexpected unindent when the problem is of course a missing except: statement.

My point: check the code above where the unexpected (un)indent is reported!


回答 9

如果缩进没问题,请查看您的编辑器是否具有“查看空白”选项。启用它应该可以找到空格和制表符混合的位置。

If the indentation looks ok then have a look to see if your editor has a “View Whitespace” option. Enabling this should allow to find where spaces and tabs are mixed.


回答 10

有一个对我总是有用的技巧:

如果您意外缩进,并且发现所有代码均已缩进,请尝试使用其他编辑器将其打开,然后您会看到未缩进的代码行。

当我使用vim,gedit或类似的编辑器时,这件事发生在我身上。

尝试仅将1个编辑器用于您的代码。

There is a trick that always worked for me:

If you got and unexpected indent and you see that all the code is perfectly indented, try opening it with another editor and you will see what line of code is not indented.

It happened to me when used vim, gedit or editors like that.

Try to use only 1 editor for your code.


回答 11

只需复制您的脚本,然后在整个代码“””下放入“”即可…

在变量中指定此行。

a = """ your python script """
print a.replace('here please press tab button it will insert some space"," here simply press space bar four times")
# here we replacing tab space by four char space as per pep 8 style guide..

现在在Sublime Editor中使用ctrl + b执行此代码,现在它将在控制台中打印缩进的代码。而已

Simply copy your script and put under “”” your entire code “”” …

specify this line in a variable.. like,

a = """ your python script """
print a.replace('here please press tab button it will insert some space"," here simply press space bar four times")
# here we replacing tab space by four char space as per pep 8 style guide..

now execute this code, in Sublime Editor using ctrl+b, now it will print indented code in console. that’s it


回答 12

您需要做的就是从以下代码的开头删除空格或制表符空格

from django.contrib import admin

# Register your models here.
from .models import Myapp
admin.site.register(Myapp)

All You need to do is remove spaces or tab spaces from the start of following codes

from django.contrib import admin

# Register your models here.
from .models import Myapp
admin.site.register(Myapp)

回答 13

运行以下命令以解决该问题:

autopep8 -i <filename>.py

这将更新您的代码并解决所有缩进错误:)

希望这能解决

Run the following command to get it solved :

autopep8 -i <filename>.py

This will update your code and solve all indentation Errors :)

Hope this will solve


回答 14

Notepad ++提供了正确的制表符空间,但最终在Sublime文本编辑器中发现了缩进问题。

使用Sublime文本编辑器并逐行进行

Notepad++ was giving the tab space correct but the indentation problem was finally found in Sublime text editor.

Use Sublime text editor and go line by line


回答 15

与许多其他编程语言不同,Python中的缩进很重要,这并不是为了代码可读性。如果连续命令之间的代码中有空格或制表符,则python将给出此错误,因为Python对此很敏感。当我们将代码复制并粘贴到任何Python时,我们很可能会遇到此错误。 确保使用文本编辑器(如Notepad ++)标识并删除这些空格,或者从出现错误的代码行中手动删除空格。

Step1 :Gives error 
L = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
print(L[2: ])

Step2: L = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]print(L[2: ])

Step3: No error after space was removed
L = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
print(L[2: ])
OUTPUT: [[7, 8, 9, 10]]

谢谢!

Indentation in Python is important and this is just not for code readability, unlike many other programming languages. If there is any white space or tab in your code between consecutive commands, python will give this error as Python is sensitive to this. We are likely to get this error when we do copy and paste of code to any Python. Make sure to identify and remove these spaces using a text editor like Notepad++ or manually remove the whitespace from the line of code where you are getting an error.

Step1 :Gives error 
L = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
print(L[2: ])

Step2: L = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]print(L[2: ])

Step3: No error after space was removed
L = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
print(L[2: ])
OUTPUT: [[7, 8, 9, 10]]

Thanks!


在Django网站中将HTML渲染为PDF

问题:在Django网站中将HTML渲染为PDF

对于我的django网站,我正在寻找一种将动态html页面转换为pdf的简单解决方案。

页面包含HTML和来自Google可视化API的图表(该图表基于javascript,但必须包含这些图表)。

For my django powered site, I am looking for an easy solution to convert dynamic html pages to pdf.

Pages include HTML and charts from Google visualization API (which is javascript based, yet including those graphs is a must).


回答 0

尝试从Reportlab解决方案。

下载并像往常一样使用python setup.py install安装

您还需要安装以下模块:具有easy_install的xhtml2pdf,html5lib,pypdf。

这是一个用法示例:

首先定义此功能:

import cStringIO as StringIO
from xhtml2pdf import pisa
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from cgi import escape


def render_to_pdf(template_src, context_dict):
    template = get_template(template_src)
    context = Context(context_dict)
    html  = template.render(context)
    result = StringIO.StringIO()

    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))

然后,您可以像这样使用它:

def myview(request):
    #Retrieve data or whatever you need
    return render_to_pdf(
            'mytemplate.html',
            {
                'pagesize':'A4',
                'mylist': results,
            }
        )

模板:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>My Title</title>
        <style type="text/css">
            @page {
                size: {{ pagesize }};
                margin: 1cm;
                @frame footer {
                    -pdf-frame-content: footerContent;
                    bottom: 0cm;
                    margin-left: 9cm;
                    margin-right: 9cm;
                    height: 1cm;
                }
            }
        </style>
    </head>
    <body>
        <div>
            {% for item in mylist %}
                RENDER MY CONTENT
            {% endfor %}
        </div>
        <div id="footerContent">
            {%block page_foot%}
                Page <pdf:pagenumber>
            {%endblock%}
        </div>
    </body>
</html>

希望能帮助到你。

Try the solution from Reportlab.

Download it and install it as usual with python setup.py install

You will also need to install the following modules: xhtml2pdf, html5lib, pypdf with easy_install.

Here is an usage example:

First define this function:

import cStringIO as StringIO
from xhtml2pdf import pisa
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from cgi import escape


def render_to_pdf(template_src, context_dict):
    template = get_template(template_src)
    context = Context(context_dict)
    html  = template.render(context)
    result = StringIO.StringIO()

    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))

Then you can use it like this:

def myview(request):
    #Retrieve data or whatever you need
    return render_to_pdf(
            'mytemplate.html',
            {
                'pagesize':'A4',
                'mylist': results,
            }
        )

The template:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>My Title</title>
        <style type="text/css">
            @page {
                size: {{ pagesize }};
                margin: 1cm;
                @frame footer {
                    -pdf-frame-content: footerContent;
                    bottom: 0cm;
                    margin-left: 9cm;
                    margin-right: 9cm;
                    height: 1cm;
                }
            }
        </style>
    </head>
    <body>
        <div>
            {% for item in mylist %}
                RENDER MY CONTENT
            {% endfor %}
        </div>
        <div id="footerContent">
            {%block page_foot%}
                Page <pdf:pagenumber>
            {%endblock%}
        </div>
    </body>
</html>

Hope it helps.


回答 1

https://github.com/nigma/django-easy-pdf

模板:

{% extends "easy_pdf/base.html" %}

{% block content %}
    <div id="content">
        <h1>Hi there!</h1>
    </div>
{% endblock %}

视图:

from easy_pdf.views import PDFTemplateView

class HelloPDFView(PDFTemplateView):
    template_name = "hello.html"

如果要在Python 3上使用django-easy-pdf,请检查此处建议的解决方案。

https://github.com/nigma/django-easy-pdf

Template:

{% extends "easy_pdf/base.html" %}

{% block content %}
    <div id="content">
        <h1>Hi there!</h1>
    </div>
{% endblock %}

View:

from easy_pdf.views import PDFTemplateView

class HelloPDFView(PDFTemplateView):
    template_name = "hello.html"

If you want to use django-easy-pdf on Python 3 check the solution suggested here.


回答 2

我只是为CBV打了个招。未在生产中使用,但会为我生成PDF。可能需要为错误报告方面的事情工作,但到目前为止仍能解决问题。

import StringIO
from cgi import escape
from xhtml2pdf import pisa
from django.http import HttpResponse
from django.template.response import TemplateResponse
from django.views.generic import TemplateView

class PDFTemplateResponse(TemplateResponse):

    def generate_pdf(self, retval):

        html = self.content

        result = StringIO.StringIO()
        rendering = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)

        if rendering.err:
            return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))
        else:
            self.content = result.getvalue()

    def __init__(self, *args, **kwargs):
        super(PDFTemplateResponse, self).__init__(*args, mimetype='application/pdf', **kwargs)
        self.add_post_render_callback(self.generate_pdf)


class PDFTemplateView(TemplateView):
    response_class = PDFTemplateResponse

像这样使用:

class MyPdfView(PDFTemplateView):
    template_name = 'things/pdf.html'

I just whipped this up for CBV. Not used in production but generates a PDF for me. Probably needs work for the error reporting side of things but does the trick so far.

import StringIO
from cgi import escape
from xhtml2pdf import pisa
from django.http import HttpResponse
from django.template.response import TemplateResponse
from django.views.generic import TemplateView

class PDFTemplateResponse(TemplateResponse):

    def generate_pdf(self, retval):

        html = self.content

        result = StringIO.StringIO()
        rendering = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)

        if rendering.err:
            return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))
        else:
            self.content = result.getvalue()

    def __init__(self, *args, **kwargs):
        super(PDFTemplateResponse, self).__init__(*args, mimetype='application/pdf', **kwargs)
        self.add_post_render_callback(self.generate_pdf)


class PDFTemplateView(TemplateView):
    response_class = PDFTemplateResponse

Used like:

class MyPdfView(PDFTemplateView):
    template_name = 'things/pdf.html'

回答 3

使用以下包装之一尝试wkhtmltopdf

django-wkhtmltopdfpython-pdfkit

这对我来说非常有效,支持javascript和css或webkit浏览器支持的任何功能。

有关更多详细的教程,请参见此博客文章。

Try wkhtmltopdf with either one of the following wrappers

django-wkhtmltopdf or python-pdfkit

This worked great for me,supports javascript and css or anything for that matter which a webkit browser supports.

For more detailed tutorial please see this blog post


回答 4

在尝试使它工作了许多小时之后,我终于找到了这个:https : //github.com/vierno/django-xhtml2pdf

这是https://github.com/chrisglass/django-xhtml2pdf的一个分支,它为基于类的通用视图提供了mixin。我这样使用它:

    # views.py
    from django_xhtml2pdf.views import PdfMixin
    class GroupPDFGenerate(PdfMixin, DetailView):
        model = PeerGroupSignIn
        template_name = 'groups/pdf.html'

    # templates/groups/pdf.html
    <html>
    <style>
    @page { your xhtml2pdf pisa PDF parameters }
    </style>
    </head>
    <body>
        <div id="header_content"> (this is defined in the style section)
            <h1>{{ peergroupsignin.this_group_title }}</h1>
            ...

填充模板字段时,请使用您在视图中定义的所有小写字母的模型名称。由于它是GCBV,因此您可以在urls.py中将其称为“ .as_view”:

    # urls.py (using url namespaces defined in the main urls.py file)
    url(
        regex=r"^(?P<pk>\d+)/generate_pdf/$",
        view=views.GroupPDFGenerate.as_view(),
        name="generate_pdf",
       ),

After trying to get this to work for too many hours, I finally found this: https://github.com/vierno/django-xhtml2pdf

It’s a fork of https://github.com/chrisglass/django-xhtml2pdf that provides a mixin for a generic class-based view. I used it like this:

    # views.py
    from django_xhtml2pdf.views import PdfMixin
    class GroupPDFGenerate(PdfMixin, DetailView):
        model = PeerGroupSignIn
        template_name = 'groups/pdf.html'

    # templates/groups/pdf.html
    <html>
    <style>
    @page { your xhtml2pdf pisa PDF parameters }
    </style>
    </head>
    <body>
        <div id="header_content"> (this is defined in the style section)
            <h1>{{ peergroupsignin.this_group_title }}</h1>
            ...

Use the model name you defined in your view in all lowercase when populating the template fields. Because its a GCBV, you can just call it as ‘.as_view’ in your urls.py:

    # urls.py (using url namespaces defined in the main urls.py file)
    url(
        regex=r"^(?P<pk>\d+)/generate_pdf/$",
        view=views.GroupPDFGenerate.as_view(),
        name="generate_pdf",
       ),

回答 5

您可以使用iReport编辑器定义布局,并在jasper报表服务器中发布报表。发布后,您可以调用rest api以获取结果。

这是功能测试:

from django.test import TestCase
from x_reports_jasper.models import JasperServerClient

"""
    to try integraction with jasper server through rest
"""
class TestJasperServerClient(TestCase):

    # define required objects for tests
    def setUp(self):

        # load the connection to remote server
        try:

            self.j_url = "http://127.0.0.1:8080/jasperserver"
            self.j_user = "jasperadmin"
            self.j_pass = "jasperadmin"

            self.client = JasperServerClient.create_client(self.j_url,self.j_user,self.j_pass)

        except Exception, e:
            # if errors could not execute test given prerrequisites
            raise

    # test exception when server data is invalid
    def test_login_to_invalid_address_should_raise(self):
        self.assertRaises(Exception,JasperServerClient.create_client, "http://127.0.0.1:9090/jasperserver",self.j_user,self.j_pass)

    # test execute existent report in server
    def test_get_report(self):

        r_resource_path = "/reports/<PathToPublishedReport>"
        r_format = "pdf"
        r_params = {'PARAM_TO_REPORT':"1",}

        #resource_meta = client.load_resource_metadata( rep_resource_path )

        [uuid,out_mime,out_data] = self.client.generate_report(r_resource_path,r_format,r_params)
        self.assertIsNotNone(uuid)

这是调用实现的示例:

from django.db import models
import requests
import sys
from xml.etree import ElementTree
import logging 

# module logger definition
logger = logging.getLogger(__name__)

# Create your models here.
class JasperServerClient(models.Manager):

    def __handle_exception(self, exception_root, exception_id, exec_info ):
        type, value, traceback = exec_info
        raise JasperServerClientError(exception_root, exception_id), None, traceback

    # 01: REPORT-METADATA 
    #   get resource description to generate the report
    def __handle_report_metadata(self, rep_resourcepath):

        l_path_base_resource = "/rest/resource"
        l_path = self.j_url + l_path_base_resource
        logger.info( "metadata (begin) [path=%s%s]"  %( l_path ,rep_resourcepath) )

        resource_response = None
        try:
            resource_response = requests.get( "%s%s" %( l_path ,rep_resourcepath) , cookies = self.login_response.cookies)

        except Exception, e:
            self.__handle_exception(e, "REPORT_METADATA:CALL_ERROR", sys.exc_info())

        resource_response_dom = None
        try:
            # parse to dom and set parameters
            logger.debug( " - response [data=%s]"  %( resource_response.text) )
            resource_response_dom = ElementTree.fromstring(resource_response.text)

            datum = "" 
            for node in resource_response_dom.getiterator():
                datum = "%s<br />%s - %s" % (datum, node.tag, node.text)
            logger.debug( " - response [xml=%s]"  %( datum ) )

            #
            self.resource_response_payload= resource_response.text
            logger.info( "metadata (end) ")
        except Exception, e:
            logger.error( "metadata (error) [%s]" % (e))
            self.__handle_exception(e, "REPORT_METADATA:PARSE_ERROR", sys.exc_info())


    # 02: REPORT-PARAMS 
    def __add_report_params(self, metadata_text, params ):
        if(type(params) != dict):
            raise TypeError("Invalid parameters to report")
        else:
            logger.info( "add-params (begin) []" )
            #copy parameters
            l_params = {}
            for k,v in params.items():
                l_params[k]=v
            # get the payload metadata
            metadata_dom = ElementTree.fromstring(metadata_text)
            # add attributes to payload metadata
            root = metadata_dom #('report'):

            for k,v in l_params.items():
                param_dom_element = ElementTree.Element('parameter')
                param_dom_element.attrib["name"] = k
                param_dom_element.text = v
                root.append(param_dom_element)

            #
            metadata_modified_text =ElementTree.tostring(metadata_dom, encoding='utf8', method='xml')
            logger.info( "add-params (end) [payload-xml=%s]" %( metadata_modified_text )  )
            return metadata_modified_text



    # 03: REPORT-REQUEST-CALL 
    #   call to generate the report
    def __handle_report_request(self, rep_resourcepath, rep_format, rep_params):

        # add parameters
        self.resource_response_payload = self.__add_report_params(self.resource_response_payload,rep_params)

        # send report request

        l_path_base_genreport = "/rest/report"
        l_path = self.j_url + l_path_base_genreport
        logger.info( "report-request (begin) [path=%s%s]"  %( l_path ,rep_resourcepath) )

        genreport_response = None
        try:
            genreport_response = requests.put( "%s%s?RUN_OUTPUT_FORMAT=%s" %(l_path,rep_resourcepath,rep_format),data=self.resource_response_payload, cookies = self.login_response.cookies )
            logger.info( " - send-operation-result [value=%s]"  %( genreport_response.text) )
        except Exception,e:
            self.__handle_exception(e, "REPORT_REQUEST:CALL_ERROR", sys.exc_info())


        # parse the uuid of the requested report
        genreport_response_dom = None

        try:
            genreport_response_dom = ElementTree.fromstring(genreport_response.text)

            for node in genreport_response_dom.findall("uuid"):
                datum = "%s" % (node.text)

            genreport_uuid = datum      

            for node in genreport_response_dom.findall("file/[@type]"):
                datum = "%s" % (node.text)
            genreport_mime = datum

            logger.info( "report-request (end) [uuid=%s,mime=%s]"  %( genreport_uuid, genreport_mime) )

            return [genreport_uuid,genreport_mime]
        except Exception,e:
            self.__handle_exception(e, "REPORT_REQUEST:PARSE_ERROR", sys.exc_info())

    # 04: REPORT-RETRIEVE RESULTS 
    def __handle_report_reply(self, genreport_uuid ):


        l_path_base_getresult = "/rest/report"
        l_path = self.j_url + l_path_base_getresult 
        logger.info( "report-reply (begin) [uuid=%s,path=%s]"  %( genreport_uuid,l_path) )

        getresult_response = requests.get( "%s%s/%s?file=report" %(self.j_url,l_path_base_getresult,genreport_uuid),data=self.resource_response_payload, cookies = self.login_response.cookies )
        l_result_header_mime =getresult_response.headers['Content-Type']

        logger.info( "report-reply (end) [uuid=%s,mime=%s]"  %( genreport_uuid, l_result_header_mime) )
        return [l_result_header_mime, getresult_response.content]

    # public methods ---------------------------------------    

    # tries the authentication with jasperserver throug rest
    def login(self, j_url, j_user,j_pass):
        self.j_url= j_url

        l_path_base_auth = "/rest/login"
        l_path = self.j_url + l_path_base_auth

        logger.info( "login (begin) [path=%s]"  %( l_path) )

        try:
            self.login_response = requests.post(l_path , params = {
                    'j_username':j_user,
                    'j_password':j_pass
                })                  

            if( requests.codes.ok != self.login_response.status_code ):
                self.login_response.raise_for_status()

            logger.info( "login (end)" )
            return True
            # see http://blog.ianbicking.org/2007/09/12/re-raising-exceptions/

        except Exception, e:
            logger.error("login (error) [e=%s]" % e )
            self.__handle_exception(e, "LOGIN:CALL_ERROR",sys.exc_info())
            #raise

    def generate_report(self, rep_resourcepath,rep_format,rep_params):
        self.__handle_report_metadata(rep_resourcepath)
        [uuid,mime] = self.__handle_report_request(rep_resourcepath, rep_format,rep_params)
        # TODO: how to handle async?
        [out_mime,out_data] = self.__handle_report_reply(uuid)
        return [uuid,out_mime,out_data]

    @staticmethod
    def create_client(j_url, j_user, j_pass):
        client = JasperServerClient()
        login_res = client.login( j_url, j_user, j_pass )
        return client


class JasperServerClientError(Exception):

    def __init__(self,exception_root,reason_id,reason_message=None):
        super(JasperServerClientError, self).__init__(str(reason_message))
        self.code = reason_id 
        self.description = str(exception_root) + " " + str(reason_message)
    def __str__(self):
        return self.code + " " + self.description

You can use iReport editor to define the layout, and publish the report in jasper reports server. After publish you can invoke the rest api to get the results.

Here is the test of the functionality:

from django.test import TestCase
from x_reports_jasper.models import JasperServerClient

"""
    to try integraction with jasper server through rest
"""
class TestJasperServerClient(TestCase):

    # define required objects for tests
    def setUp(self):

        # load the connection to remote server
        try:

            self.j_url = "http://127.0.0.1:8080/jasperserver"
            self.j_user = "jasperadmin"
            self.j_pass = "jasperadmin"

            self.client = JasperServerClient.create_client(self.j_url,self.j_user,self.j_pass)

        except Exception, e:
            # if errors could not execute test given prerrequisites
            raise

    # test exception when server data is invalid
    def test_login_to_invalid_address_should_raise(self):
        self.assertRaises(Exception,JasperServerClient.create_client, "http://127.0.0.1:9090/jasperserver",self.j_user,self.j_pass)

    # test execute existent report in server
    def test_get_report(self):

        r_resource_path = "/reports/<PathToPublishedReport>"
        r_format = "pdf"
        r_params = {'PARAM_TO_REPORT':"1",}

        #resource_meta = client.load_resource_metadata( rep_resource_path )

        [uuid,out_mime,out_data] = self.client.generate_report(r_resource_path,r_format,r_params)
        self.assertIsNotNone(uuid)

And here is an example of the invocation implementation:

from django.db import models
import requests
import sys
from xml.etree import ElementTree
import logging 

# module logger definition
logger = logging.getLogger(__name__)

# Create your models here.
class JasperServerClient(models.Manager):

    def __handle_exception(self, exception_root, exception_id, exec_info ):
        type, value, traceback = exec_info
        raise JasperServerClientError(exception_root, exception_id), None, traceback

    # 01: REPORT-METADATA 
    #   get resource description to generate the report
    def __handle_report_metadata(self, rep_resourcepath):

        l_path_base_resource = "/rest/resource"
        l_path = self.j_url + l_path_base_resource
        logger.info( "metadata (begin) [path=%s%s]"  %( l_path ,rep_resourcepath) )

        resource_response = None
        try:
            resource_response = requests.get( "%s%s" %( l_path ,rep_resourcepath) , cookies = self.login_response.cookies)

        except Exception, e:
            self.__handle_exception(e, "REPORT_METADATA:CALL_ERROR", sys.exc_info())

        resource_response_dom = None
        try:
            # parse to dom and set parameters
            logger.debug( " - response [data=%s]"  %( resource_response.text) )
            resource_response_dom = ElementTree.fromstring(resource_response.text)

            datum = "" 
            for node in resource_response_dom.getiterator():
                datum = "%s<br />%s - %s" % (datum, node.tag, node.text)
            logger.debug( " - response [xml=%s]"  %( datum ) )

            #
            self.resource_response_payload= resource_response.text
            logger.info( "metadata (end) ")
        except Exception, e:
            logger.error( "metadata (error) [%s]" % (e))
            self.__handle_exception(e, "REPORT_METADATA:PARSE_ERROR", sys.exc_info())


    # 02: REPORT-PARAMS 
    def __add_report_params(self, metadata_text, params ):
        if(type(params) != dict):
            raise TypeError("Invalid parameters to report")
        else:
            logger.info( "add-params (begin) []" )
            #copy parameters
            l_params = {}
            for k,v in params.items():
                l_params[k]=v
            # get the payload metadata
            metadata_dom = ElementTree.fromstring(metadata_text)
            # add attributes to payload metadata
            root = metadata_dom #('report'):

            for k,v in l_params.items():
                param_dom_element = ElementTree.Element('parameter')
                param_dom_element.attrib["name"] = k
                param_dom_element.text = v
                root.append(param_dom_element)

            #
            metadata_modified_text =ElementTree.tostring(metadata_dom, encoding='utf8', method='xml')
            logger.info( "add-params (end) [payload-xml=%s]" %( metadata_modified_text )  )
            return metadata_modified_text



    # 03: REPORT-REQUEST-CALL 
    #   call to generate the report
    def __handle_report_request(self, rep_resourcepath, rep_format, rep_params):

        # add parameters
        self.resource_response_payload = self.__add_report_params(self.resource_response_payload,rep_params)

        # send report request

        l_path_base_genreport = "/rest/report"
        l_path = self.j_url + l_path_base_genreport
        logger.info( "report-request (begin) [path=%s%s]"  %( l_path ,rep_resourcepath) )

        genreport_response = None
        try:
            genreport_response = requests.put( "%s%s?RUN_OUTPUT_FORMAT=%s" %(l_path,rep_resourcepath,rep_format),data=self.resource_response_payload, cookies = self.login_response.cookies )
            logger.info( " - send-operation-result [value=%s]"  %( genreport_response.text) )
        except Exception,e:
            self.__handle_exception(e, "REPORT_REQUEST:CALL_ERROR", sys.exc_info())


        # parse the uuid of the requested report
        genreport_response_dom = None

        try:
            genreport_response_dom = ElementTree.fromstring(genreport_response.text)

            for node in genreport_response_dom.findall("uuid"):
                datum = "%s" % (node.text)

            genreport_uuid = datum      

            for node in genreport_response_dom.findall("file/[@type]"):
                datum = "%s" % (node.text)
            genreport_mime = datum

            logger.info( "report-request (end) [uuid=%s,mime=%s]"  %( genreport_uuid, genreport_mime) )

            return [genreport_uuid,genreport_mime]
        except Exception,e:
            self.__handle_exception(e, "REPORT_REQUEST:PARSE_ERROR", sys.exc_info())

    # 04: REPORT-RETRIEVE RESULTS 
    def __handle_report_reply(self, genreport_uuid ):


        l_path_base_getresult = "/rest/report"
        l_path = self.j_url + l_path_base_getresult 
        logger.info( "report-reply (begin) [uuid=%s,path=%s]"  %( genreport_uuid,l_path) )

        getresult_response = requests.get( "%s%s/%s?file=report" %(self.j_url,l_path_base_getresult,genreport_uuid),data=self.resource_response_payload, cookies = self.login_response.cookies )
        l_result_header_mime =getresult_response.headers['Content-Type']

        logger.info( "report-reply (end) [uuid=%s,mime=%s]"  %( genreport_uuid, l_result_header_mime) )
        return [l_result_header_mime, getresult_response.content]

    # public methods ---------------------------------------    

    # tries the authentication with jasperserver throug rest
    def login(self, j_url, j_user,j_pass):
        self.j_url= j_url

        l_path_base_auth = "/rest/login"
        l_path = self.j_url + l_path_base_auth

        logger.info( "login (begin) [path=%s]"  %( l_path) )

        try:
            self.login_response = requests.post(l_path , params = {
                    'j_username':j_user,
                    'j_password':j_pass
                })                  

            if( requests.codes.ok != self.login_response.status_code ):
                self.login_response.raise_for_status()

            logger.info( "login (end)" )
            return True
            # see http://blog.ianbicking.org/2007/09/12/re-raising-exceptions/

        except Exception, e:
            logger.error("login (error) [e=%s]" % e )
            self.__handle_exception(e, "LOGIN:CALL_ERROR",sys.exc_info())
            #raise

    def generate_report(self, rep_resourcepath,rep_format,rep_params):
        self.__handle_report_metadata(rep_resourcepath)
        [uuid,mime] = self.__handle_report_request(rep_resourcepath, rep_format,rep_params)
        # TODO: how to handle async?
        [out_mime,out_data] = self.__handle_report_reply(uuid)
        return [uuid,out_mime,out_data]

    @staticmethod
    def create_client(j_url, j_user, j_pass):
        client = JasperServerClient()
        login_res = client.login( j_url, j_user, j_pass )
        return client


class JasperServerClientError(Exception):

    def __init__(self,exception_root,reason_id,reason_message=None):
        super(JasperServerClientError, self).__init__(str(reason_message))
        self.code = reason_id 
        self.description = str(exception_root) + " " + str(reason_message)
    def __str__(self):
        return self.code + " " + self.description

回答 6

我得到了从html模板生成PDF的代码:

    import os

    from weasyprint import HTML

    from django.template import Template, Context
    from django.http import HttpResponse 


    def generate_pdf(self, report_id):

            # Render HTML into memory and get the template firstly
            template_file_loc = os.path.join(os.path.dirname(__file__), os.pardir, 'templates', 'the_template_pdf_generator.html')
            template_contents = read_all_as_str(template_file_loc)
            render_template = Template(template_contents)

            #rendering_map is the dict for params in the template 
            render_definition = Context(rendering_map)
            render_output = render_template.render(render_definition)

            # Using Rendered HTML to generate PDF
            response = HttpResponse(content_type='application/pdf')
            response['Content-Disposition'] = 'attachment; filename=%s-%s-%s.pdf' % \
                                              ('topic-test','topic-test', '2018-05-04')
            # Generate PDF
            pdf_doc = HTML(string=render_output).render()
            pdf_doc.pages[0].height = pdf_doc.pages[0]._page_box.children[0].children[
                0].height  # Make PDF file as single page file 
            pdf_doc.write_pdf(response)
            return response

    def read_all_as_str(self, file_loc, read_method='r'):
        if file_exists(file_loc):
            handler = open(file_loc, read_method)
            contents = handler.read()
            handler.close()
            return contents
        else:
            return 'file not exist'  

I get the code to generate the PDF from html template :

    import os

    from weasyprint import HTML

    from django.template import Template, Context
    from django.http import HttpResponse 


    def generate_pdf(self, report_id):

            # Render HTML into memory and get the template firstly
            template_file_loc = os.path.join(os.path.dirname(__file__), os.pardir, 'templates', 'the_template_pdf_generator.html')
            template_contents = read_all_as_str(template_file_loc)
            render_template = Template(template_contents)

            #rendering_map is the dict for params in the template 
            render_definition = Context(rendering_map)
            render_output = render_template.render(render_definition)

            # Using Rendered HTML to generate PDF
            response = HttpResponse(content_type='application/pdf')
            response['Content-Disposition'] = 'attachment; filename=%s-%s-%s.pdf' % \
                                              ('topic-test','topic-test', '2018-05-04')
            # Generate PDF
            pdf_doc = HTML(string=render_output).render()
            pdf_doc.pages[0].height = pdf_doc.pages[0]._page_box.children[0].children[
                0].height  # Make PDF file as single page file 
            pdf_doc.write_pdf(response)
            return response

    def read_all_as_str(self, file_loc, read_method='r'):
        if file_exists(file_loc):
            handler = open(file_loc, read_method)
            contents = handler.read()
            handler.close()
            return contents
        else:
            return 'file not exist'  

回答 7

如果您的html模板中包含上下文数据以及css和js。比使用pdfjs的选择更好

在您的代码中,您可以像这样使用。

from django.template.loader import get_template
import pdfkit
from django.conf import settings

context={....}
template = get_template('reports/products.html')
html_string = template.render(context)
pdfkit.from_string(html_string, os.path.join(settings.BASE_DIR, "media", 'products_report-%s.pdf'%(id)))

在您的HTML中,您可以链接外部或内部的CSS和js,它将生成最佳质量的pdf。

If you have context data along with css and js in your html template. Than you have good option to use pdfjs.

In your code you can use like this.

from django.template.loader import get_template
import pdfkit
from django.conf import settings

context={....}
template = get_template('reports/products.html')
html_string = template.render(context)
pdfkit.from_string(html_string, os.path.join(settings.BASE_DIR, "media", 'products_report-%s.pdf'%(id)))

In your HTML you can link extranal or internal css and js, it will generate best quality of pdf.


从sklearn导入时出现ImportError:无法导入名称check_build

问题:从sklearn导入时出现ImportError:无法导入名称check_build

尝试从sklearn导入时出现以下错误:

>>> from sklearn import svm

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
   from sklearn import svm
  File "C:\Python27\lib\site-packages\sklearn\__init__.py", line 16, in <module>
   from . import check_build
ImportError: cannot import name check_build

我正在使用python 2.7,scipy-0.12.0b1 superpack,numpy-1.6.0 superpack,scikit-learn-0.11我有一台Windows 7机器

我已经检查了几个有关此问题的答案,但没有一个给出解决此错误的方法。

I am getting the following error while trying to import from sklearn:

>>> from sklearn import svm

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
   from sklearn import svm
  File "C:\Python27\lib\site-packages\sklearn\__init__.py", line 16, in <module>
   from . import check_build
ImportError: cannot import name check_build

I am using python 2.7, scipy-0.12.0b1 superpack, numpy-1.6.0 superpack, scikit-learn-0.11 I have a windows 7 machine

I have checked several answers for this issue but none of them gives a way out of this error.


回答 0

安装scipy后为我工作。

Worked for me after installing scipy.


回答 1

>>> from sklearn import preprocessing, metrics, cross_validation

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    from sklearn import preprocessing, metrics, cross_validation
  File "D:\Python27\lib\site-packages\sklearn\__init__.py", line 31, in <module>
    from . import __check_build
ImportError: cannot import name __check_build
>>> ================================ RESTART ================================
>>> from sklearn import preprocessing, metrics, cross_validation
>>> 

因此,只需尝试重新启动Shell!

>>> from sklearn import preprocessing, metrics, cross_validation

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    from sklearn import preprocessing, metrics, cross_validation
  File "D:\Python27\lib\site-packages\sklearn\__init__.py", line 31, in <module>
    from . import __check_build
ImportError: cannot import name __check_build
>>> ================================ RESTART ================================
>>> from sklearn import preprocessing, metrics, cross_validation
>>> 

So, simply try to restart the shell!


回答 2

我针对Python 3.6.5 64位Windows 10的解决方案:

  1. pip uninstall sklearn
  2. pip uninstall scikit-learn
  3. pip install sklearn

无需重新启动命令行,但是您可以根据需要执行此操作。我花了一天的时间来修复此错误。希望能有所帮助。

My solution for Python 3.6.5 64-bit Windows 10:

  1. pip uninstall sklearn
  2. pip uninstall scikit-learn
  3. pip install sklearn

No need to restart command-line but you can do this if you want. It took me one day to fix this bug. Hope this help.


回答 3

安装numpyscipysklearn 仍然有错误

解:

设置PathPython的系统变量和PYTHONPATH环境变量

系统变量:添加C:\Python34到路径中用户变量:添加新:(名称)PYTHONPATH(值)C:\Python34\Lib\site-packages;

After installing numpy , scipy ,sklearn still has error

Solution:

Setting Up System Path Variable for Python & the PYTHONPATH Environment Variable

System Variables: add C:\Python34 into path User Variables: add new: (name)PYTHONPATH (value)C:\Python34\Lib\site-packages;


回答 4

通常,当我遇到此类错误时,打开__init__.py文件并四处浏览会有所帮助。转到目录,C:\Python27\lib\site-packages\sklearn并确保首先有一个子目录__check_build。在我的机器(有工作sklearn安装,Mac OSX版,Python的2.7.3)我有__init__.pysetup.py及其相关的.pyc文件和二进制_check_build.so

闲逛在__init__.py该目录中,我会采取下一步行动就是去sklearn/__init__.py进出import语句评论—只是检查,事情被正确编译check_build的东西,它似乎并没有做任何事情,但调用预编译二进制 当然,这需要您自担风险,而且(肯定)可以解决。如果构建失败,您可能很快就会遇到其他更大的问题。

Usually when I get these kinds of errors, opening the __init__.py file and poking around helps. Go to the directory C:\Python27\lib\site-packages\sklearn and ensure that there’s a sub-directory called __check_build as a first step. On my machine (with a working sklearn installation, Mac OSX, Python 2.7.3) I have __init__.py, setup.py, their associated .pyc files, and a binary _check_build.so.

Poking around the __init__.py in that directory, the next step I’d take is to go to sklearn/__init__.py and comment out the import statement—the check_build stuff just checks that things were compiled correctly, it doesn’t appear to do anything but call a precompiled binary. This is, of course, at your own risk, and (to be sure) a work around. If your build failed you’ll likely soon run into other, bigger problems.


回答 5

我在Windows上遇到了同样的问题。通过安装numpy的+ MKL解决它http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy(有它的建议依赖于它的其他软件包之前安装numpy的+ MKL)通过建议这个答案

I had the same issue on Windows. Solved it by installing Numpy+MKL from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy (there it’s recommended to install numpy+mkl before other packages that depend on it) as suggested by this answer.


回答 6

从python.org安装新的64位版本的Python 3.4后,导入SKLEARN时遇到问题。

原来是SCIPY模块损坏了,当我尝试“导入scipy”时alos失败了。

解决方案是卸载scipy并使用pip3重新安装它:

C:\> pip uninstall scipy

[lots of reporting messages deleted]

Proceed (y/n)? y
  Successfully uninstalled scipy-1.0.0

C:\Users\>pip3 install scipy

Collecting scipy
  Downloading scipy-1.0.0-cp36-none-win_amd64.whl (30.8MB)
    100% |████████████████████████████████| 30.8MB 33kB/s
Requirement already satisfied: numpy>=1.8.2 in c:\users\johnmccurdy\appdata\loca
l\programs\python\python36\lib\site-packages (from scipy)
Installing collected packages: scipy
Successfully installed scipy-1.0.0

C:\Users>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
>>>
>>> import sklearn
>>>

I had problems importing SKLEARN after installing a new 64bit version of Python 3.4 from python.org.

Turns out that it was the SCIPY module that was broken, and alos failed when I tried to “import scipy”.

Solution was to uninstall scipy and reinstall it with pip3:

C:\> pip uninstall scipy

[lots of reporting messages deleted]

Proceed (y/n)? y
  Successfully uninstalled scipy-1.0.0

C:\Users\>pip3 install scipy

Collecting scipy
  Downloading scipy-1.0.0-cp36-none-win_amd64.whl (30.8MB)
    100% |████████████████████████████████| 30.8MB 33kB/s
Requirement already satisfied: numpy>=1.8.2 in c:\users\johnmccurdy\appdata\loca
l\programs\python\python36\lib\site-packages (from scipy)
Installing collected packages: scipy
Successfully installed scipy-1.0.0

C:\Users>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
>>>
>>> import sklearn
>>>

回答 7

如果您使用Anaconda 2.7 64位,请尝试

conda upgrade scikit-learn

并重新启动python shell,对我有用。

当我遇到相同的问题并解决时,进行第二次编辑:

conda upgrade scikit-learn

对我也有用

If you use Anaconda 2.7 64 bit, try

conda upgrade scikit-learn

and restart the python shell, that works for me.

Second edit when I faced the same problem and solved it:

conda upgrade scikit-learn

also works for me


回答 8

没有其他答案对我有用。经过一番修补后,我卸载了sklearn:

pip uninstall sklearn

然后我从这里删除了sklearn文件夹:(调整系统和python版本的路径)

C:\Users\%USERNAME%\AppData\Roaming\Python\Python36\site-packages

并从此站点通过转轮安装了它:链接

该错误在那里可能是由于与其他地方安装的sklearn版本冲突。

None of the other answers worked for me. After some tinkering I unsinstalled sklearn:

pip uninstall sklearn

Then I removed sklearn folder from here: (adjust the path to your system and python version)

C:\Users\%USERNAME%\AppData\Roaming\Python\Python36\site-packages

And the installed it from wheel from this site: link

The error was there probably because of a version conflict with sklearn installed somewhere else.


回答 9

对我来说,我是通过使用最新的python版本(3.7)从全新安装Anaconda来将现有代码升级为新设置的,为此,

from sklearn import cross_validation, 
from sklearn.grid_search import GridSearchCV

from sklearn.model_selection import GridSearchCV,cross_validate

For me, I was upgrading the existing code into new setup by installing Anaconda from fresh with latest python version(3.7) For this,

from sklearn import cross_validation, 
from sklearn.grid_search import GridSearchCV

to

from sklearn.model_selection import GridSearchCV,cross_validate

回答 10

无需卸载然后重新安装sklearn

试试这个:

from sklearn.model_selection import train_test_split

no need to uninstall & then re-install sklearn

try this:

from sklearn.model_selection import train_test_split

回答 11

我在重新安装anaconda时遇到了同样的问题,为我解决了这个问题

i had the same problem reinstalling anaconda solved the issue for me


回答 12

在Windows中:

我试图从外壳中删除sklearn:pip卸载sklearn,然后重新安装它,但不起作用..

解决方案:

1- open the cmd shell.
2- cd c:\pythonVERSION\scripts
3- pip uninstall sklearn
4- open in the explorer: C:\pythonVERSION\Lib\site-packages
5- look for the folders that contains sklearn and delete them ..
6- back to cmd: pip install sklearn

In windows:

I tried to delete sklearn from the shell: pip uninstall sklearn, and re install it but doesn’t work ..

the solution:

1- open the cmd shell.
2- cd c:\pythonVERSION\scripts
3- pip uninstall sklearn
4- open in the explorer: C:\pythonVERSION\Lib\site-packages
5- look for the folders that contains sklearn and delete them ..
6- back to cmd: pip install sklearn

通过索引访问Python字典的元素

问题:通过索引访问Python字典的元素

考虑一个像

mydict = {
  'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},
  'Grapes':{'Arabian':'25','Indian':'20'} }

例如,如何访问该词典的特定元素?例如,我想在对Apple的第一个元素进行某种格式设置后再打印第一个元素,在我们的例子中,该格式仅是“ American”?

附加信息上面的数据结构是通过在python函数中解析输入文件创建的。一旦创建,它在该运行中将保持不变。

我在函数中使用此数据结构。

因此,如果文件发生更改,则下次运行此应用程序时,文件的内容将有所不同,因此此数据结构的内容将有所不同,但格式将相同。因此,您在我的职能中看到我,我不知道Apple中的第一个元素是“ American”或其他任何元素,因此我不能直接使用“ American”作为键。

Consider a dict like

mydict = {
  'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},
  'Grapes':{'Arabian':'25','Indian':'20'} }

How do I access for instance a particular element of this dictionary? for instance, I would like to print the first element after some formatting the first element of Apple which in our case is ‘American’ only?

Additional information The above data structure was created by parsing an input file in a python function. Once created however it remains the same for that run.

I am using this data structure in my function.

So if the file changes, the next time this application is run the contents of the file are different and hence the contents of this data structure will be different but the format would be the same. So you see I in my function I don’t know that the first element in Apple is ‘American’ or anything else so I can’t directly use ‘American’ as a key.


回答 0

鉴于它是字典,您可以使用键来访问它。获取存储在“ Apple”下的词典,请执行以下操作:

>>> mydict["Apple"]
{'American': '16', 'Mexican': 10, 'Chinese': 5}

并让其中有多少是美国人(16),请执行以下操作:

>>> mydict["Apple"]["American"]
'16'

Given that it is a dictionary you access it by using the keys. Getting the dictionary stored under “Apple”, do the following:

>>> mydict["Apple"]
{'American': '16', 'Mexican': 10, 'Chinese': 5}

And getting how many of them are American (16), do like this:

>>> mydict["Apple"]["American"]
'16'

回答 1

如果问题是,如果我知道我有一个包含“ Apple”作为水果和“ American”作为一种苹果的字典,我将使用:

myDict = {'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},
          'Grapes':{'Arabian':'25','Indian':'20'} }


print myDict['Apple']['American']

正如其他人建议的那样。如果问题是,则当您将任意文件读入dict数据结构的dict中时,您不知道是否存在“ Apple”作为水果,是否存在“ American”作为“ Apple”类型,您可以执行以下操作:

print [ftype['American'] for f,ftype in myDict.iteritems() if f == 'Apple' and 'American' in ftype]

还是更好,所以如果您知道只有Apple拥有American类型,就不必不必要地遍历整个dict。

if 'Apple' in myDict:
    if 'American' in myDict['Apple']:
        print myDict['Apple']['American']

在所有这些情况下,字典实际存储条目的顺序都无关紧要。如果您确实担心订单,则可以考虑使用OrderedDict

http://docs.python.org/dev/library/collections.html#collections.OrderedDict

If the questions is, if I know that I have a dict of dicts that contains ‘Apple’ as a fruit and ‘American’ as a type of apple, I would use:

myDict = {'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},
          'Grapes':{'Arabian':'25','Indian':'20'} }


print myDict['Apple']['American']

as others suggested. If instead the questions is, you don’t know whether ‘Apple’ as a fruit and ‘American’ as a type of ‘Apple’ exist when you read an arbitrary file into your dict of dict data structure, you could do something like:

print [ftype['American'] for f,ftype in myDict.iteritems() if f == 'Apple' and 'American' in ftype]

or better yet so you don’t unnecessarily iterate over the entire dict of dicts if you know that only Apple has the type American:

if 'Apple' in myDict:
    if 'American' in myDict['Apple']:
        print myDict['Apple']['American']

In all of these cases it doesn’t matter what order the dictionaries actually store the entries. If you are really concerned about the order, then you might consider using an OrderedDict:

http://docs.python.org/dev/library/collections.html#collections.OrderedDict


回答 2

正如我注意到您的描述一样,您只知道解析器将为您提供一个字典,其值也就是字典,如下所示:

sampleDict = {
              "key1": {"key10": "value10", "key11": "value11"},
              "key2": {"key20": "value20", "key21": "value21"}
              }

因此,您必须迭代父词典。如果要打印或访问sampleDict.values()列表中的所有第一个词典键,则可以使用以下方法:

for key, value in sampleDict.items():
    print value.keys()[0]

如果您只想访问中第一个项目的第一个键sampleDict.values(),这可能会很有用:

print sampleDict.values()[0].keys()[0]

如果使用您在问题中给出的示例,我的意思是:

sampleDict = {
              'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},
              'Grapes':{'Arabian':'25','Indian':'20'}
              }

第一个代码的输出是:

American
Indian

第二个代码的输出是:

American

As I noticed your description, you just know that your parser will give you a dictionary that its values are dictionary too like this:

sampleDict = {
              "key1": {"key10": "value10", "key11": "value11"},
              "key2": {"key20": "value20", "key21": "value21"}
              }

So you have to iterate over your parent dictionary. If you want to print out or access all first dictionary keys in sampleDict.values() list, you may use something like this:

for key, value in sampleDict.items():
    print value.keys()[0]

If you want to just access first key of the first item in sampleDict.values(), this may be useful:

print sampleDict.values()[0].keys()[0]

If you use the example you gave in the question, I mean:

sampleDict = {
              'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},
              'Grapes':{'Arabian':'25','Indian':'20'}
              }

The output for the first code is:

American
Indian

And the output for the second code is:

American

EDIT 1:

Above code examples does not work for version 3 and above of python; since from version 3, python changed the type of output of methods keys and values from list to dict_values. Type dict_values is not accepting indexing, but it is iterable. So you need to change above codes as below:

First One:

for key, value in sampleDict.items():
    print(list(value.keys())[0])

Second One:

print(list(list(sampleDict.values())[0].keys())[0])

回答 3

作为奖励,我想为您的问题提供一种不同的解决方案。您似乎正在处理嵌套字典,这通常很乏味,尤其是当您必须检查内部键的存在时。

在pypi上有一些与此有关的有趣库,这是您的快速搜索

在您的特定情况下,dict_digger似乎很合适。

>>> import dict_digger
>>> d = {
  'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},
  'Grapes':{'Arabian':'25','Indian':'20'} 
}

>>> print(dict_digger.dig(d, 'Apple','American'))
16
>>> print(dict_digger.dig(d, 'Grapes','American'))
None

As a bonus, I’d like to offer kind of a different solution to your issue. You seem to be dealing with nested dictionaries, which is usually tedious, especially when you have to check for existence of an inner key.

There are some interesting libraries regarding this on pypi, here is a quick search for you.

In your specific case, dict_digger seems suited.

>>> import dict_digger
>>> d = {
  'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},
  'Grapes':{'Arabian':'25','Indian':'20'} 
}

>>> print(dict_digger.dig(d, 'Apple','American'))
16
>>> print(dict_digger.dig(d, 'Grapes','American'))
None

回答 4

您可以使用dict['Apple'].keys()[0]获取Apple字典中的第一个键,但是不能保证它会是American。词典中键的顺序可以根据词典的内容和键的添加顺序而变化。

You can use dict['Apple'].keys()[0] to get the first key in the Apple dictionary, but there’s no guarantee that it will be American. The order of keys in a dictionary can change depending on the contents of the dictionary and the order the keys were added.


回答 5

我知道这是8岁,但似乎没有人真正阅读并回答过这个问题。

您可以在字典上调用.values()以获得内部字典的列表,从而按索引访问它们。

>>> mydict = {
...  'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},
...  'Grapes':{'Arabian':'25','Indian':'20'} }

>>>mylist = list(mydict.values())
>>>mylist[0]
{'American':'16', 'Mexican':10, 'Chinese':5},
>>>mylist[1]
{'Arabian':'25','Indian':'20'}

>>>myInnerList1 = list(mylist[0].values())
>>>myInnerList1
['16', 10, 5]
>>>myInnerList2 = list(mylist[1].values())
>>>myInnerList2
['25', '20']

I know this is 8 years old, but no one seems to have actually read and answered the question.

You can call .values() on a dict to get a list of the inner dicts and thus access them by index.

>>> mydict = {
...  'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},
...  'Grapes':{'Arabian':'25','Indian':'20'} }

>>>mylist = list(mydict.values())
>>>mylist[0]
{'American':'16', 'Mexican':10, 'Chinese':5},
>>>mylist[1]
{'Arabian':'25','Indian':'20'}

>>>myInnerList1 = list(mylist[0].values())
>>>myInnerList1
['16', 10, 5]
>>>myInnerList2 = list(mylist[1].values())
>>>myInnerList2
['25', '20']

回答 6

您不能依赖字典的顺序。但是您可以尝试以下方法:

dict['Apple'].items()[0][0]

如果您希望保留订单,则可以使用以下网址http : //www.python.org/dev/peps/pep-0372/#ordered-dict-api

You can’t rely on order on dictionaries. But you may try this:

dict['Apple'].items()[0][0]

If you want the order to be preserved you may want to use this: http://www.python.org/dev/peps/pep-0372/#ordered-dict-api


回答 7

简单示例以了解如何访问字典中的元素:-

创建字典

d = {'dog' : 'bark', 'cat' : 'meow' } 
print(d.get('cat'))
print(d.get('lion'))
print(d.get('lion', 'Not in the dictionary'))
print(d.get('lion', 'NA'))
print(d.get('dog', 'NA'))

探索有关Python词典的更多信息,并在此处进行交互学习…

Simple Example to understand how to access elements in the dictionary:-

Create a Dictionary

d = {'dog' : 'bark', 'cat' : 'meow' } 
print(d.get('cat'))
print(d.get('lion'))
print(d.get('lion', 'Not in the dictionary'))
print(d.get('lion', 'NA'))
print(d.get('dog', 'NA'))

Explore more about Python Dictionaries and learn interactively here…


回答 8

尽管有很多问题的答案,但很少有人指出字典是无序的映射,因此(直到使用Python 3.7祝福插入顺序)字典中“第一个”条目的想法实际上是没道理 甚至OrderedDict只能通过使用诸如以下的丑陋的数字索引来访问mydict[mydict.keys()[0]](仅Python 2,因为在Python 3中keys()是不可下标的迭代器。)

从3.7开始,并在3,6中付诸实践-引入了新的行为,但直到3.7才包含在语言规范中-在字典的键,值或项上进行迭代(我相信,也设置)将首先产生最近插入的对象。仍然没有简单的方法可以通过数字插入索引来访问它们。

关于选择和“格式化”项目的问题,如果您知道要在字典中检索的键,通常可以将该键用作下标来检索它(my_var = mydict['Apple'])。

如果您确实希望能够通过条目号为项目建立索引(而忽略了特定条目号会随着插入而改变的事实),那么适当的结构可能就是一个由两个元素组成的元组的列表。代替

mydict = {
  'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},
  'Grapes':{'Arabian':'25','Indian':'20'} }

您可以使用:

mylist = [
    ('Apple', {'American':'16', 'Mexican':10, 'Chinese':5}),
    ('Grapes', {'Arabian': '25', 'Indian': '20'}
]

在这种情况下,第一个条目采用mylist[0]经典的列表扩展名形式,其值为('Apple', {'American':'16', 'Mexican':10, 'Chinese':5})。您可以如下遍历整个列表:

for (key, value) in mylist:  # unpacks to avoid tuple indexing
    if key == 'Apple':
        if 'American' in value:
            print(value['American'])

但是,如果您知道要查找键“ Apple”,那么为什么不使用字典呢?

您可以通过缓存键列表来引入更高级别的间接访问,但是保持两个数据结构同步的复杂性将不可避免地增加代码的复杂性。

Few people appear, despite the many answers to this question, to have pointed out that dictionaries are un-ordered mappings, and so (until the blessing of insertion order with Python 3.7) the idea of the “first” entry in a dictionary literally made no sense. And even an OrderedDict can only be accessed by numerical index using such uglinesses as mydict[mydict.keys()[0]] (Python 2 only, since in Python 3 keys() is a non-subscriptable iterator.)

From 3.7 onwards and in practice in 3,6 as well – the new behaviour was introduced then, but not included as part of the language specification until 3.7 – iteration over the keys, values or items of a dict (and, I believe, a set also) will yield the least-recently inserted objects first. There is still no simple way to access them by numerical index of insertion.

As to the question of selecting and “formatting” items, if you know the key you want to retrieve in the dictionary you would normally use the key as a subscript to retrieve it (my_var = mydict['Apple']).

If you really do want to be able to index the items by entry number (ignoring the fact that a particular entry’s number will change as insertions are made) then the appropriate structure would probably be a list of two-element tuples. Instead of

mydict = {
  'Apple': {'American':'16', 'Mexican':10, 'Chinese':5},
  'Grapes':{'Arabian':'25','Indian':'20'} }

you might use:

mylist = [
    ('Apple', {'American':'16', 'Mexican':10, 'Chinese':5}),
    ('Grapes', {'Arabian': '25', 'Indian': '20'}
]

Under this regime the first entry is mylist[0] in classic list-endexed form, and its value is ('Apple', {'American':'16', 'Mexican':10, 'Chinese':5}). You could iterate over the whole list as follows:

for (key, value) in mylist:  # unpacks to avoid tuple indexing
    if key == 'Apple':
        if 'American' in value:
            print(value['American'])

but if you know you are looking for the key “Apple”, why wouldn’t you just use a dict instead?

You could introduce an additional level of indirection by cacheing the list of keys, but the complexities of keeping two data structures in synchronisation would inevitably add to the complexity of your code.


回答 9

使用以下小功能,挖掘树形字典变得非常容易:

def dig(tree, path):
    for key in path.split("."):
        if isinstance(tree, dict) and tree.get(key):
            tree = tree[key]
        else:
            return None
    return tree

现在,dig(mydict, "Apple.Mexican")返回10,而dig(mydict, "Grape")产生子树{'Arabian':'25','Indian':'20'}。如果字典中不包含键,则dig返回None

请注意,您可以轻松地从’。’更改(甚至参数化)分隔符char。到“ /”,“ |” 等等

With the following small function, digging into a tree-shaped dictionary becomes quite easy:

def dig(tree, path):
    for key in path.split("."):
        if isinstance(tree, dict) and tree.get(key):
            tree = tree[key]
        else:
            return None
    return tree

Now, dig(mydict, "Apple.Mexican") returns 10, while dig(mydict, "Grape") yields the subtree {'Arabian':'25','Indian':'20'}. If a key is not contained in the dictionary, dig returns None.

Note that you can easily change (or even parameterize) the separator char from ‘.’ to ‘/’, ‘|’ etc.


来自字符串源的Python xml ElementTree?

问题:来自字符串源的Python xml ElementTree?

ElementTree.parse从文件读取,如果字符串中已经有XML数据,该如何使用?

也许我在这里错过了一些东西,但是必须有一种使用ElementTree的方法,而无需将字符串写出到文件中并再次读取它。

xml.etree.elementtree

The ElementTree.parse reads from a file, how can I use this if I already have the XML data in a string?

Maybe I am missing something here, but there must be a way to use the ElementTree without writing out the string to a file and reading it again.

xml.etree.elementtree


回答 0

如果您xml.etree.ElementTree.parse用于从文件中进行解析,则可以使用xml.etree.ElementTree.fromstring从文本中进行解析。

参见xml.etree.ElementTree

If you’re using xml.etree.ElementTree.parse to parse from a file, then you can use xml.etree.ElementTree.fromstring to parse from text.

See xml.etree.ElementTree


回答 1

您可以将文本解析为字符串,从而创建一个Element,并使用该Element创建ElementTree。

import xml.etree.ElementTree as ET
tree = ET.ElementTree(ET.fromstring(xmlstring))

我刚遇到这个问题,而文档虽然完整,但对于parse()fromstring()方法之间用法的区别并不是很简单。

You can parse the text as a string, which creates an Element, and create an ElementTree using that Element.

import xml.etree.ElementTree as ET
tree = ET.ElementTree(ET.fromstring(xmlstring))

I just came across this issue and the documentation, while complete, is not very straightforward on the difference in usage between the parse() and fromstring() methods.


回答 2

你需要 xml.etree.ElementTree.fromstring(text)

from xml.etree.ElementTree import XML, fromstring
myxml = fromstring(text)

You need the xml.etree.ElementTree.fromstring(text)

from xml.etree.ElementTree import XML, fromstring
myxml = fromstring(text)

回答 3

io.StringIO是将XML放入xml.etree.ElementTree的另一种选择:

import io
f = io.StringIO(xmlstring)
tree = ET.parse(f)
root = tree.getroot()

因此,它不会影响假定的XML声明tree(尽管ElementTree.write()需要此声明)。请参见如何使用xml.etree.ElementTree编写XML声明

io.StringIO is another option for getting XML into xml.etree.ElementTree:

import io
f = io.StringIO(xmlstring)
tree = ET.parse(f)
root = tree.getroot()

Hovever, it does not affect the XML declaration one would assume to be in tree (although that’s needed for ElementTree.write()). See How to write XML declaration using xml.etree.ElementTree.


有趣好用的Python教程

退出移动版
微信支付
请使用 微信 扫码支付