小编Har*_*wee的帖子

在不更改其名称的情况下向父类方法添加额外功能

我有两个班,一个是父母,另一个是孩子.

class Parent(object):

    def __init__(self):
        #does something

    def method_parent(self):
        print "Parent"

class Child(Parent):

    def __init__(self):
        Parent.__init__(self)

    def method_parent(self):
        print "Child"
Run Code Online (Sandbox Code Playgroud)

继承父级后,我想修改Parent方法,method_parent保留该方法的原始功能,并为该方法添加一些额外的代码行.

我知道我可以创建一个新的方法

def method_child(self):
    self.method_parent()
    #Add extra lines of code to perform something 
Run Code Online (Sandbox Code Playgroud)

但我想使用方法的原始名称.我无法复制该方法的源代码,因为该方法来自C模块

我想要实现的是这样的

def method_parent():
    #do parent_method stuff
    #do extra stuff
Run Code Online (Sandbox Code Playgroud)

这甚至可能吗?

python class

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

PyQt QDialog 返回响应是或否

我有一个 QDialog 类

confirmation_dialog = uic.loadUiType("ui\\confirmation_dialog.ui")[0]

class ConfirmationDialog(QDialog,confirmation_dialog):

    def __init__(self,parent=None):
        QDialog.__init__(self,parent)
        self.setupUi(self)
        message = "Hello, Dialog test"
        self.yes_button.clicked.connect(self.yes_clicked)
        self.no_button.clicked.connect(self.no_clicked)
        self.message_box.insertPlainText(message)

    def yes_clicked(self):
        self.emit(SIGNAL("dialog_response"),"yes")

    def no_clicked(self):
        self.emit(SIGNAL("dialog_response"),"no")
Run Code Online (Sandbox Code Playgroud)

我有一个需要确认是否继续的功能,但对于当前的实现,它不会等待QDialog关闭。

如何让我的函数等待响应QDialog,然后相应地继续。

我想实现类似于confirm功能的东西,如下所示

def function(self):
    ....
    ....
    if self.confirm() == 'yes':
        #do something
    elif self.confirm() == 'no':
        #do something

def confirm(self):
    dialog = ConfirmationDialog()
    dialog.show()
    return #response from dialog
Run Code Online (Sandbox Code Playgroud)

python pyqt

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

Django GenericForeignKey 更新

我正在尝试在 django 中将外键转换为 GenericForeignKey。我计划在三个迁移中执行此操作:mig1、mig2、mig3。

迁移1(mig1)有以下代码

class Migration(migrations.Migration):

    dependencies = [
        ('contenttypes', '0002_remove_content_type_name'),
        ('post_service', '0008_auto_20180802_1112'),
    ]

    operations = [
        migrations.AddField(
            model_name='comment',
            name='content_type',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType'),
        ),
        migrations.AddField(
            model_name='comment',
            name='object_id',
            field=models.PositiveIntegerField(null=True),
        ),
    ]
Run Code Online (Sandbox Code Playgroud)

迁移2(mig2)有以下代码

def change_posts_to_generic_key_comment(apps, schema_editor):
    Comment  = apps.get_model('post_service', 'Comment')
    db_alias = schema_editor.connection.alias
    comments = Comment.objects.using(db_alias).all()
    for comment in comments:
        Comment.objects.filter(id=comment.id).update(content_object=comment.post)

def reverse_change_posts_to_generic_key_comment(apps, schema_editor):
    Comment  = apps.get_model('post_service', 'Comment')
    db_alias = schema_editor.connection.alias
    comments = Comment.objects.using(db_alias).all()
    for comment in comments:
        Comment.objects.filter(id=comment.id).update(content_object=)

class Migration(migrations.Migration):

    dependencies = [
        ('post_service', '0009_auto_20180802_1623'),
    ]

    operations = [
        migrations.RunPython(change_posts_to_generic_key_comment, …
Run Code Online (Sandbox Code Playgroud)

django generic-foreign-key django-migrations

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

decode-encode UTF-8不会导致原始的unicode

当我试图通过再次解码和编码来分离两个Unicode字符时,我得不到相同的Unicode作为回报,但我得到一个不同的.

当我尝试这样做时,附上了回复.

>>> s ='\xf0\x9f\x93\xb1\xf0\x9f\x9a\xac'
>>> u = s.decode("utf-8")
>>> u
u'\U0001f4f1\U0001f6ac'
>>> u[0].encode("utf-8")
'\xed\xa0\xbd'
>>> u[1].encode("utf-8")
'\xed\xb3\xb1'
>>> u[0]
u'\ud83d'
>>> u[1]
u'\udcf1'
Run Code Online (Sandbox Code Playgroud)

python unicode encoding utf-8 decoding

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