小编scy*_*ale的帖子

你怎么抓住这个例外?

这段代码在django/db/models/fields.py中创建/定义了一个异常?

class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjectDescriptorMethods)):
    # This class provides the functionality that makes the related-object
    # managers available as attributes on a model class, for fields that have
    # a single "remote" value, on the class that defines the related field.
    # In the example "choice.poll", the poll attribute is a
    # ReverseSingleRelatedObjectDescriptor instance.
    def __init__(self, field_with_rel):
        self.field = field_with_rel
        self.cache_name = self.field.get_cache_name()

    @cached_property
    def RelatedObjectDoesNotExist(self):
        # The exception can't be created at initialization time since the
        # related model might not …
Run Code Online (Sandbox Code Playgroud)

python django exception

146
推荐指数
5
解决办法
5万
查看次数

org.apache.maven.plugins目标执行失败

我正在尝试使用maven生成一个war文件但是我收到了这个错误(大部分下载日志输出被省略以使日志更清晰):

Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-war-plugin/2.1.1/maven-war-plugin-2.1.1.pom
Downloaded: 
...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building tn.talan.selenium Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ tn.talan.selenium ---
...
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ tn.talan.selenium ---
...
[INFO] --- maven-war-plugin:2.1.1:war (default-cli) @ tn.talan.selenium ---
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.4.1/maven-archiver-2.4.1.pom
Downloaded: 
...
[INFO] BUILD FAILURE
...
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-cli) on project tn.talan.selenium: Execution default-cli of goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war failed: Plugin org.apache.maven.plugins:maven-war-plugin:2.1.1 or one of its dependencies could not be resolved: Could not transfer artifact org.codehaus.plexus:plexus-io:jar:1.0.1 …
Run Code Online (Sandbox Code Playgroud)

maven-plugin maven

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

使python程序等到Twisted deferred返回一个值

我有一个程序从其他页面获取信息并使用BeautifulSoup和Twisted的getPage解析它们.稍后在程序中我打印延迟进程创建的信息.目前我的程序试图在不同的返回信息之前打印它.我怎么能让它等待?

def twisAmaz(contents): #This parses the page (amazon api xml file)
    stonesoup = BeautifulStoneSoup(contents)
    if stonesoup.find("mediumimage") == None:
       imageurl.append("/images/notfound.png")
    else:
      imageurl.append(stonesoup.find("mediumimage").url.contents[0])

    usedPdata = stonesoup.find("lowestusedprice")
    newPdata = stonesoup.find("lowestnewprice")
    titledata = stonesoup.find("title")
    reviewdata = stonesoup.find("editorialreview")

    if stonesoup.find("asin") != None:
        asin.append(stonesoup.find("asin").contents[0])
    else:
        asin.append("None")
    reactor.stop()


deferred = dict()
for tmpISBN in isbn:  #Go through ISBN numbers and get Amazon API information for each
    deferred[(tmpISBN)] = getPage(fetchInfo(tmpISBN))
    deferred[(tmpISBN)].addCallback(twisAmaz)
    reactor.run()

.....print info on each ISBN
Run Code Online (Sandbox Code Playgroud)

python twisted deferred

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

如何从发电机中取出元组?最佳实践

我是一个新手程序员,我想创建一个生成器,它将返回两个值,我将在另一个函数中用作元组.

我不明白为什么tuple(function_1(a,b))回报((1, 2),),而tuple(function_2(a,b))会返回一个正确的元组.

我想知道这里发生了什么,最好的语法是什么,最终知道是否有可能从中检索元组function_1.

提前感谢您的任何解释!

>>> def function_1(a,b):
...     yield a,b
...
>>> def function_2(a,b):
...     yield a
...     yield b
...
>>> a = 1
>>> b = 2
>>>
>>> function_1(a,b)
<generator object function_1 at 0x1007931b0>
>>> function_2(a,b)
<generator object function_2 at 0x1007931f8>
>>> tuple(function_1(a,b))
((1, 2),)
>>> tuple(function_2(a,b))
(1, 2)
>>> for item in function_1(a,b):
...    print(item)
...
(1, 2)
>>> for item in function_2(a,b):
...    print(item)
...
1 …
Run Code Online (Sandbox Code Playgroud)

python syntax tuples generator

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

如何使用PowerShell检查特定服务是否在远程计算机上运行

我有3台服务器,运行3项服务:

  • SERVER-A正在运行的serv1.exe 服务
  • SERVER-B运行serv2.exe 服务
  • SERVER-C运行serv3.exe 服务

使用PowerShell如何查询每个服务以检查它们是否正在运行并将信息输出到文本文件?

powershell

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

扭曲的延迟/回调和异步执行

我试图找出如何使用twisted来使我的代码更加异步.

  • 函数返回延迟对象
  • 然后我添加一个回调列表
  • 在延迟函数提供一些结果后,将调用第一个回调 deferred_obj.callback
  • 然后,在回调链中,第一个回调将对数据执行某些操作并调用第二个回调
  • 等等.

但链式回调不会被认为是异步的,因为它们是链接的,并且事件循环将继续同时触发它们中的每一个,直到没有更多,对吧?

但是,如果我有一个延迟对象,并且我将deferred_obj.callback作为其回调附加,d.addCallback(deferred_obj.callback)那么这将被视为异步,因为deferred_obj正在等待数据,然后传递数据的方法正在等待数据同样,一旦我d.callback'd'对象处理数据然后它调用deferred_obj.callback但是因为这个对象是延迟的,与链式回调的情况不同,它将异步执行...正确吗?

假设我的所有代码都是非阻塞的,这意味着链式回调不是异步的,而链式延迟是正确的吗?

python asynchronous twisted callback deferred

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

如何覆盖 celery 任务的后端

我们使用 Redis 作为结果后端。然而,对于一项任务,我们希望重写它以使用 RabbitMQ。

Task.backend的文档说:

用于此任务的结果存储后端。默认为 CELERY_RESULT_BACKEND 设置

所以我假设我们可以设置Task.backendCELERY_RESULT_BACKEND.

所以我尝试这个:

celeryconfig.py

CELERY_RESULT_BACKEND = "redis://redis-host:7777"
Run Code Online (Sandbox Code Playgroud)

tasks.py

@app.task(backend='amqp://guest@localhost/tasks-stg')
def my_task(params):
    ...
Run Code Online (Sandbox Code Playgroud)

然而,工人失败了:

[2015-05-07 13:33:49,264: ERROR/Worker-1] Process Worker-1
Traceback (most recent call last):
  File "/project/python2.7_x64/lib/python2.7/site-packages/billiard/process.py", line 292, in _bootstrap
    self.run()
  File "/project/python2.7_x64/lib/python2.7/site-packages/billiard/pool.py", line 286, in run
    self.after_fork()
  File "/project/python2.7_x64/lib/python2.7/site-packages/billiard/pool.py", line 389, in after_fork
    self.initializer(*self.initargs)
  File "/project/python2.7_x64/lib/python2.7/site-packages/celery/concurrency/prefork.py", line 81, in process_initializer
    app=app)
  File "/project/python2.7_x64/lib/python2.7/site-packages/celery/app/trace.py", line 178, in build_tracer
    store_result = backend.store_result
AttributeError: 'str' object has no attribute 'store_result'
Run Code Online (Sandbox Code Playgroud)

python celery celery-task

5
推荐指数
1
解决办法
4626
查看次数

在sqlalchemy上使用python模拟库

我正在使用sqlalchemy查询数据库中的项目。并行地说,我是单元测试的新手,我正在尝试学习如何进行单元测试来测试数据库。我尝试使用模拟库进行测试,但到目前为止,这似乎非常困难。

因此,我编写了一段创建Session对象的代码。该对象用于连接数据库。

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.exc import OperationalError, ArgumentError

test_db_string = 'postgresql+psycopg2://testdb:hello@localhost/' \
                   'test_databasetable'
def get_session(database_connection_string):
    try:
        Base = declarative_base()
        engine = create_engine(database_connection_string)
        Base.metadata.bind = engine
        DBSession = sessionmaker(bind=engine)
        session = DBSession()
        connection = session.connection()
        return session
    except OperationalError:
        return None
    except ArgumentError:
        return None
Run Code Online (Sandbox Code Playgroud)

因此,我为此功能制作了一个单元测试用例:

import mock
import unittest
from mock import sentinel
import get_session

class TestUtilMock(unittest.TestCase):

    @mock.patch('app.Utilities.util.create_engine')  # mention the whole path
    @mock.patch('app.Utilities.util.sessionmaker')
    @mock.patch('app.Utilities.util.declarative_base')
    def test_get_session1(self, …
Run Code Online (Sandbox Code Playgroud)

python unit-testing sqlalchemy mocking python-mock

5
推荐指数
1
解决办法
6963
查看次数

如何增加Twisted的连接池大小?

我使用Twisted 8.1.0作为套接字服务器引擎.反应堆 - epoll.数据库服务器是MySQL 5.0.67.操作系统 - Ubuntu Linux 8.10 32位

/etc/mysql/my.cnf:

max_connections        = 1000
Run Code Online (Sandbox Code Playgroud)

在源代码中:

adbapi.ConnectionPool("MySQLdb", ..., use_unicode=True, charset='utf8', 
                      cp_min=3, cp_max=700, cp_noisy=False)
Run Code Online (Sandbox Code Playgroud)

但实际上,SHOW PROCESSLIST当应用程序在高负载下运行时,我只能看到200(或更少)打开的连接().这还不够我的应用程序:(

我认为这是线程池的限制.有任何想法吗?

python connection-pooling twisted

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

无法从ec2服务器连接

我正在尝试登录ec2实例服务器,但我收到的错误就像

ubuntu的身份验证失败太多,

要么

权限被拒绝(公钥).

当我连接到服务器时 ssh -i "pem_file" ec2_name@public_ip

昨天,我登录服务器,它工作,但退出服务器后再次尝试登录它给我的错误,这是上面提到的.

amazon-ec2

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