小编kir*_*iri的帖子

Git在修改提交后阻止推送

通常,我只是跑

git add file
git commit
git push
Run Code Online (Sandbox Code Playgroud)

但如果我推送之前修改了提交(with git commit --amend),则下一次推送失败

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Run Code Online (Sandbox Code Playgroud)

如何让git在不合并分支的情况下推送更改?我只有一个分支(master),我是唯一一个使用这个回购的人,为什么这么说呢?

git branch -a:

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
Run Code Online (Sandbox Code Playgroud)

编辑:使用gitk HEAD @{u},我看到我有2个分支,一个用原始提交,另一个用修改提交.

git git-push git-amend git-commit

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

Python子进程将子输出到文件和终端?

我正在运行一个脚本,通过使用执行许多可执行文件

subprocess.call(cmdArgs,stdout=outf, stderr=errf)
Run Code Online (Sandbox Code Playgroud)

when outf/ errf是None或文件描述符(stdout/的不同文件stderr).

有什么方法可以执行每个exe,以便将stdout和stderr一起写入文件和终端?

python subprocess parent

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

为什么模块在另一个模块中作为_ <name>导入?

Python标准库中有几种情况,其中导入的模块名称中带有前导下划线(_).我想知道为什么这是必要的,因为它仍然在模块的名称下,from ... import *并将尊重__all__变量导入的内容.

有谁知道为什么这是使用/必要的?

一个例子是argparse具有以下导入:

import collections as _collections
import copy as _copy
import os as _os
import re as _re
import sys as _sys
import textwrap as _textwrap
Run Code Online (Sandbox Code Playgroud)

python namespaces

15
推荐指数
2
解决办法
1391
查看次数

Python打印最后的追溯只?

请考虑以下代码和回溯:

>>> try:
...  raise KeyboardInterrupt
... except KeyboardInterrupt:
...  raise Exception
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
Exception
>>> 
Run Code Online (Sandbox Code Playgroud)

我想只打印最近的追溯(其中一个Exception被提出).
怎么能实现这一目标?


从上面的例子中,我想打印以下内容,就像raise Exceptionexcept子句之外调用一样.

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
Exception
Run Code Online (Sandbox Code Playgroud)

python traceback python-3.x

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

更新tkinter菜单栏项目的标签?

是否可以使用tkinter更改菜单中项目的标签?

在下面的示例中,我想将其从"示例项"(在"文件"菜单中)更改为其他值.

from tkinter import *

root = Tk()
menu_bar = Menu(root)

file_menu = Menu(menu_bar, tearoff=False)
file_menu.add_command(label="An example item", command=lambda: print('clicked!'))
menu_bar.add_cascade(label="File", menu=file_menu)

root.config(menu=menu_bar)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

python tkinter menubar python-3.x

9
推荐指数
2
解决办法
5009
查看次数

如何使用排序的键列表对OrderedDict进行排序?

假设我有一个collections.OrderedDict对象和一个重新排列的键列表:

ordereddict = collections.OrderedDict((
    ('key_78', 'value'),
    ('key_40', 'value'),
    ('key_96', 'value'),
    ('key_53', 'value'),
    ('key_04', 'value'),
    ('key_89', 'value'),
    ('key_52', 'value'),
    ('key_86', 'value'),
    ('key_16', 'value'),
    ('key_63', 'value'),
))

# Example only; actual list will **not** == sorted(ordereddict)
key_list = ['key_04', 'key_16', 'key_40', 'key_52', 'key_53', 'key_63', 'key_78', 'key_86', 'key_89', 'key_96']
Run Code Online (Sandbox Code Playgroud)

我怎样才能OrderedDict对它进行排序,使其按照与key_list?相同的方式进行排序?

python sorting

9
推荐指数
2
解决办法
4487
查看次数

如何转义格式字符串?

是否可以使用Python的str.format(key=value)语法来仅替换某些键.

考虑这个例子:

my_string = 'Hello {name}, my name is {my_name}!'

my_string = my_string.format(name='minerz029')
Run Code Online (Sandbox Code Playgroud)

返回

KeyError: 'my_name'
Run Code Online (Sandbox Code Playgroud)

有没有办法实现这个目标?

python string string.format

6
推荐指数
3
解决办法
2920
查看次数

如何获取 ZipFile 对象的文件路径?

如果我有一个zipfile.ZipFile对象,如何从对象中确定 zip 文件的文件路径?

a = zipfile.ZipFile('C:\\path\\zipfile.zip')

a.get_file_path()
Run Code Online (Sandbox Code Playgroud)

get_file_path()应该返回哪里'C:\\path\\zipfile.zip'

python zip filepath zipfile python-3.x

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

函数返回值中的 dict[str, dict] 是什么意思?

我在 PyCharm 的文档片段中看到了这一点。

def a_func() 
Inferred type: () -> dict[str, dict]
Run Code Online (Sandbox Code Playgroud)

我知道->函数定义中的意思是“返回值”,但这部分是什么dict[str, dict]意思?


演示这一点的最简单的函数是:

def a_func() 
Inferred type: () -> dict[str, dict]
Run Code Online (Sandbox Code Playgroud)

它由编辑器 → 代码完成中的“自动弹出文档”设置显示。启用此功能后,将鼠标悬停在上面的代码片段上时会显示。this()

python return-value pycharm

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

如何使用自定义的类文件对象作为子进程stdout / stderr?

考虑下面的代码,其中subprocess.Popen产生了a。我想写入子流程stdoutstderr转到我的自定义文件对象的.write()方法,但是事实并非如此。

import subprocess

class Printer:

    def __init__(self):
        pass

    def write(self, chunk):
        print('Writing:', chunk)

    def fileno(self):
        return 0

    def close(self):
        return

proc = subprocess.Popen(['bash', '-c', 'echo Testing'], 
                        stdout=Printer(),
                        stderr=subprocess.STDOUT)
proc.wait()
Run Code Online (Sandbox Code Playgroud)

为什么.write()不使用该方法stdout=?在这种情况下,指定参数有什么用?

python subprocess

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