简体   繁体   English

访问和更新模块中定义的python3集

[英]access and update a python3 set defined in the module

I am using https://github.com/sciunto/python-bibtexparser as a library module for my code(installed with pip3). 我正在使用https://github.com/sciunto/python-bibtexparser作为我的代码的库模块(随pip3安装)。 So, I don't have writing access to the module. 因此,我没有对该模块的写入权限。

But, the set it is using is rather limited, as defined in standard_types in https://github.com/sciunto/python-bibtexparser/blob/master/bibtexparser/bibdatabase.py . 但是,它所使用的集合相当有限,如https://github.com/sciunto/python-bibtexparser/blob/master/bibtexparser/bibdatabase.py中的standard_types所定义。

Is it possible, that, I redefine/append the set from the calling code? 是否有可能我从调用代码中重新定义/追加集合? eg 例如

#foo.py
import bibtexparser

#redefine standard_types
bibtexparser.STANDARD_TYPES = set([the complete set I need])

#or append the standard_types
bibtexparser.STANDARD_TYPES.update = set([append to the set])

?

Update : Actually I cant access the variable STANDARD_TYPES. 更新 :实际上我无法访问变量STANDARD_TYPES。 I am trying to do: 我正在尝试做:

from bibtexparser import bibdatabase as bd

class Window(Gtk.ApplicationWindow):
    def __init__(self, application, giofile=None):
        Gtk.ApplicationWindow.__init__(self,
                                       application=application,
                                       default_width=1000,
                                       default_height=200,
                                       border_width=5)


        print(bd)
        print(bd.STANDARD_TYPES)

Which is yielding: 这产生了:

    <module 'bibtexparser.bibdatabase' from '/usr/lib/python3.5/site-packages/bibtexparser/bibdatabase.py'>
......
        print(bd.STANDARD_TYPES)
    AttributeError: module 'bibtexparser.bibdatabase' has no attribute 'STANDARD_TYPES'

It looks like other parts of the package perform from bibtextparser import STANDARD_TYPES , so you can't just replace it; 看起来该包的其他部分from bibtextparser import STANDARD_TYPES执行,因此您不能仅仅替换它;而是直接替换它。 the modules that have already imported it will keep the old version. 已经导入的模块将保留旧版本。 Using update would work fine though you need to fix your syntax: 尽管需要修复语法,但是使用update可以正常工作:

bibtexparser.STANDARD_TYPES.update({append to the set})

That said, it looks like BibTexParser can be initialized passing it ignore_nonstandard_types=True and it won't even check STANDARD_TYPES , which is probably what you really want; 就是说,看起来BibTexParser可以通过将其传递给ignore_nonstandard_types=True进行初始化,它甚至不会检查STANDARD_TYPES ,这可能是您真正想要的; it doesn't involve hacky monkey-patching at all. 它根本不涉及破解猴子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM