小编Hol*_*way的帖子

Python ConfigParser.NoSectionError:没有部分:

我一直收到ConfigParser.NoSectionError:没有部分:'file'错误

我的配置文件如下:

[file]
api_access_id = 123445567
api_default_org = NAME
api_secret_key = kjvhkd28unfkjs
api_base_url = www.fakeurl.com/api
Run Code Online (Sandbox Code Playgroud)

我的代码看起来像:

config = ConfigParser.RawConfigParser()
configFilePath = 'E:\Python\configfile\test.txt'
config.read(configFilePath)

try:
    api_access_id = config.get('file', 'api_access_id')
    api_secret_key = config.get('file', 'api_secret_key')
    api_default_org = config.get('file', 'api_default_org')
    api_base_url = config.get('file', 'api_base_url')
except ConfigParser.NoOptionError :
    print('could not read configuration file')
    sys.exit(1)  
Run Code Online (Sandbox Code Playgroud)

错误是:

Traceback (most recent call last):
File "E:/Python/Testapi.py", line 13, in <module>
api_access_id = config.get('file', 'api_access_id')
File "C:\Python27\lib\ConfigParser.py", line 330, in get
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'file'

Process finished with …
Run Code Online (Sandbox Code Playgroud)

python

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

ls -ali输出中的字段是什么意思

所述ls -ali外壳命令显示以下输出:

933442 -rwxrw-r--    10    root   root 2048    Jan 13 07:11 afile.exe
Run Code Online (Sandbox Code Playgroud)

前面显示的所有字段是什么?

linux shell

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

Java中哪一段代码更快?

一个) for(int i = 100000; i > 0; i--) {}

b) for(int i = 1; i < 100001; i++) {}

答案就在这个网站上(问题3).我只是想不通为什么?来自网站:

3. a

java performance micro-optimization

11
推荐指数
7
解决办法
4174
查看次数

使用cxfreeze时无法使用tkinter/python脚本设置值

我用Python编写了一个带有tkinter GUI前端的程序.从脚本运行时没有问题.

我使用cx_freeze将它打包到exe文件中,并从那里运行它适用于大多数程序.但是,它有一个问题:当尝试为其中一个函数中的StringVar对象赋值时,会引发异常并终止程序.

当从程序中的不同点访问相同的功能时,它正常运行.知道什么可能是错的吗?

导致该问题的代码部分如下(我添加了消息框,以便我可以看到它失败的原因):

if keyDetail.get('default', False):
    try:
        self.entries[key].set(keyDetail['default'])
    except Exception as err:
        messagebox.showinfo('error', 'key: %s, default: %s, error: %s'%(key, keyDetail['default'], err))
Run Code Online (Sandbox Code Playgroud)

self.entries[key]是一个tk.StringVar.

运行此命令时,我在消息框中收到以下消息:

key: orderNo, default: Order Number, error: can't set "PY_VAR16:0"

python tk-toolkit tkinter cx-freeze

11
推荐指数
0
解决办法
677
查看次数

在 Eclipse 中运行错误的项目

我在 Eclipse 中编写了一些 Java 代码。当我运行它时,我会在控制台中看到另一个项目(我之前编写的)的输出。如何查看新代码的输出?

java eclipse

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

Sqlalchemy:数据库删除错误

对于以下功能

def deleteMenuItem(restaurant_id,menu_id):
    if request.method == 'POST':
        item = session.query(MenuItem).filter_by(id = menu_id)
        session.delete(item)
        session.commit()
        return redirect(url_for('showMenu',restaurant_id =restaurant_id))
    else:
        return render_template('deletemenu.html',restaurant_id=restaurant_id,menu_id=menu_id)
Run Code Online (Sandbox Code Playgroud)

当我尝试删除一个项目时。我收到以下错误

sqlalchemy.orm.exc.UnmappedInstanceError UnmappedInstanceError:类'sqlalchemy.orm.query.Query'未映射

如果我对代码进行以下更改,则可以修复错误

item = session.query(MenuItem).filter_by(id = menu_id).one()
Run Code Online (Sandbox Code Playgroud)

我不确定为什么 .one() 解决了这个问题。由于查询本身总是会找到一个结果。那么为什么需要 .one() 呢?

python sqlalchemy

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

类实例中的上下文管理器

我想创建一个特定于另一个类的实例的上下文管理器类。我可以通过调用创建类的方法来实现,但我不确定这是最好、最好的方法:

class MyClass(object):
    def __init__(self):
        self.level = 0
        self.Nest = self.create_context_manager()
    def inclev(self):
        self.level += 1
    def declev(self):
        self.level -= 1

    def create_context_manager(self):
        self2 = self
        class Nest(object):
            def __init__(self):
                pass
            def __enter__(self):
                self2.inclev()
            def __exit__(self, exc_type, exc_value, traceback):
                self2.declev()
        return Nest

# Manually increase/decrease level
my_instance = MyClass()
print(my_instance.level)
my_instance.inclev()
print(my_instance.level)
my_instance.inclev()
print(my_instance.level)
my_instance.declev()
print(my_instance.level)
my_instance.declev()
print(my_instance.level)

# Use instance-specific context manager
other_instance = MyClass()
print(other_instance.level)
with other_instance.Nest():
    print(other_instance.level)
    with other_instance.Nest():
        print(other_instance.level)
    print(other_instance.level)
print(other_instance.level)
Run Code Online (Sandbox Code Playgroud)

python

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

我如何强制java方法运行特定的实现

我有2个重载方法,一个运行超类,另一个运行子类.我希望在发送子类时,java会知道运行特定于子类的方法.唉,它运行的是超类之一.这是一个例子:

public class Test {

    static class SuperClass {}

    static class SubClass extends SuperClass {}

    static void stuff(SuperClass superclass) {
        System.out.println("IN 1");
    }

    static void stuff(SubClass subClass) {
        System.out.println("IN 2");
    }

    public static void main(String[] args) {
        SuperClass aClass = new SubClass() ;

        stuff(aClass) ;
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望打印"IN 2",但我得到"IN 1"

所以我有两个问题:1.为什么会这样?2.我如何实现我想要的结果?

提前致谢

java

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

Java中的main方法是否必须是静态的?

主要方法(Java请求您在类中拥有)是否必须是静态的?例如,我有这个代码

public class Sheet {

    public static void main(String[] args) {
        myMethod();
    }

    public void myMethod() {
        System.out.println("hi there");
    }

}
Run Code Online (Sandbox Code Playgroud)

这给了我错误

无法对main的非静态调用方法进行静态引用

如果我清楚了,我从方法调用的任何方法都main必须是静态的,并且我从静态方法调用的每个方法都必须是静态的.

为什么我的全班(如果我们更进一步,我的整个程序)和方法必须是静态的?我怎么能避免这种情况?

java methods static program-entry-point

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

将元组分配给两个数组字段

我刚刚注意到python中的一个行为我不明白.
想象一下以下代码:

myArray  = [0] * 10   
myTuple = (1,1)
Run Code Online (Sandbox Code Playgroud)

现在我想将我的元组中的两个值分配给数组中的两个字段.由于Python允许立即更改多个值,我尝试过

myArray[2:3] = myTuple
Run Code Online (Sandbox Code Playgroud)

我对myArray的期望是

[0,0,1,1,0,0,0,0,0,0]
Run Code Online (Sandbox Code Playgroud)

但我真正得到的是

[0,0,1,1,0,0,0,0,0,0,0]
Run Code Online (Sandbox Code Playgroud)

谁能向我解释这种行为?

python arrays tuples

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

Python - Twython api的麻烦

我编写了一个使用Twython与twitter交互的python脚本.它将关注者列表下载到变量(关注者)中,并将我关注的人员列表转换为另一个变量(朋友).然后应该自动跟随每个关注我的人,但是我还没有关注谁.

for fol in followers:
    if fol not in friends:
        twitter.create_friendship(fol)
Run Code Online (Sandbox Code Playgroud)

我得到的错误是只twitter.create_friendship需要一个参数,但它被赋予了两个.我不知道如何给出两个论点,我只能看到一个.

python loops for-loop twython notin

0
推荐指数
1
解决办法
175
查看次数

python - 导入错误:无法导入名称池

代码在这里:

from multiprocessing import pool
def worker(num):
    print 'Worker:', num
    return

if __name__ == '__main__':
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=worker, args=(i,))
        jobs.append(p)
        p.start()
Run Code Online (Sandbox Code Playgroud)

对不起,我是python的新手。每当我尝试导入池时,我都会收到以下错误。它说 os.chdir(wdir) 有问题,但我不知道是什么。有什么帮助吗?

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\z080302\Desktop\WinPython-32bit-2.7.6.3\python-2.7.6\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "C:/Users/z080302/Desktop/Python_Projects/mp_test.py", line 18, in <module>
p = multiprocessing.Process(target=worker, args=(i,))
NameError: name 'multiprocessing' is not defined
Run Code Online (Sandbox Code Playgroud)

python python-multiprocessing

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

数字的地板和天花板

询问用户的号码然后打印天花板和地板.

如果我们插入4.66意味着它应该产生输出为5和4 ..但它产生5.0和4

class Challenge{  
    public static void main(String args[]){  
        Scanner scanner=new Scanner(System.in);  
        System.out.println("Enter a number:"); 
        double dob=scanner.nextDouble();
        System.out.println(Math.ceil(dob));
        System.out.println(Math.floor(dob));
    }  
}  
Run Code Online (Sandbox Code Playgroud)

java

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