小编m45*_*t3r的帖子

我应该在项目的每个文件中添加__future__语句吗?

我正在为当前唯一的Python 2项目贡献代码,以允许它在Python 3上运行.我应该进行以下导入:

from __future__ import (unicode_literals, print_function,
                        absolute_imports, division)
Run Code Online (Sandbox Code Playgroud)

在项目的每个文件上或只使用我在每个文件上需要的文件?

python python-2.7 python-3.x

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

Lua:除非引用,否则将字符串拆分为单词

所以我有以下代码在空格之间拆分字符串:

text = "I am 'the text'"
for string in text:gmatch("%S+") do
    print(string)
end
Run Code Online (Sandbox Code Playgroud)

结果:

I
am
'the
text'
Run Code Online (Sandbox Code Playgroud)

但我需要这样做:

I
am
the text --[[yep, without the quotes]]
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

编辑:只是为了补充问题,我们的想法是将参数从程序传递到另一个程序.这是我正在工作的拉取请求,目前正在审核中:https://github.com/mpv-player/mpv/pull/1619

string lua lua-patterns

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

Python函数返回None

我有以下代码:

def retornaReplicas(verify_key):
    connection = sqlite3.connect('servidor.db')
    cursor = connection.cursor()

    sql = 'SELECT replicas FROM arquivos_sad WHERE verify_key="%s"' % (verify_key)
    cursor.execute(sql)
    resultado = cursor.fetchone()

    return '@'.join(resultado)
Run Code Online (Sandbox Code Playgroud)

我在Python交互式终端上调试此代码,如果我这样做:

print retornaReplicas(verify_key)
Run Code Online (Sandbox Code Playgroud)

它返回'None'.但是,如果我手动键入命令(而不是函数),如下所示:

connection = sqlite3.connect('servidor.db')
cursor = connection.cursor()

sql = 'SELECT replicas FROM arquivos_sad WHERE verify_key="%s"' % (verify_key)
cursor.execute(sql)
resultado = cursor.fetchone()
print '@'.join(resultado)
Run Code Online (Sandbox Code Playgroud)

有用.Python也没有给出任何错误.为什么这种恢复运行不起作用?

顺便说一句,verify_key是我手动设置用于调试目的的UUID(它是一个已知值),这段代码使用sqlite3数据库进行咨询.

python sqlite return

5
推荐指数
0
解决办法
824
查看次数

获取Android Instrumentation Test上的当前活动

我的Android应用程序上的MainActivity检查用户是否已登录(这是存储在SharedPreferences中),以及用户是否未登录LoginActivity.我试图使用以下代码测试它

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

private static final int TIME_OUT = 5000; /* miliseconds */

private MainActivity mMainActivity;
private Instrumentation mInstrumentation;
private SharedPreferences mLoginPrefs;

public MainActivityTest() {
    super(MainActivity.class);
}

protected void setUp() throws Exception {
    super.setUp();

    setActivityInitialTouchMode(false);

    mMainActivity = getActivity();
    mInstrumentation = getInstrumentation();
    mLoginPrefs = mInstrumentation.getTargetContext().getSharedPreferences(LoginActivity.PREFS_NAME, Context.MODE_PRIVATE);

    SharedPreferences.Editor editor = mLoginPrefs.edit();
            // User is not logged in, so it should be redirect to LoginActivity
    editor.putBoolean("logged_in", false);
    editor.commit();
}

//...

public void testC_OpenLoginActivityIfUserIsNotLoggedIn() {
    ActivityMonitor monitor = mInstrumentation.addMonitor(LoginActivity.class.getName(), null, …
Run Code Online (Sandbox Code Playgroud)

java android automated-tests junit3

4
推荐指数
2
解决办法
5581
查看次数