简体   繁体   English

在Python中运行多个函数

[英]Running multiple functions in Python

I had a program that read in a text file and took out the necessary variables for serialization into turtle format and storing in an RDF graph. 我有一个程序在文本文件中读取并取出必要的变量以序列化为乌龟格式并存储在RDF图中。 The code I had was crude and I was advised to separate it into functions. 我的代码很粗糙,我被建议将它分成函数。 As I am new to Python, I had no idea how to do this. 由于我是Python的新手,我不知道如何做到这一点。 Below is some of the functions of the program. 以下是该程序的一些功能。

I am getting confused as to when parameters should be passed into the functions and when they should be initialized with self. 我很困惑何时应该将参数传递给函数以及何时应该使用self.初始化它们self. Here are some of my functions. 这是我的一些功能。 If I could get an explanation as to what I am doing wrong that would be great. 如果我能得到一个关于我做错了什么的解释,这将是伟大的。

#!/usr/bin/env python

from rdflib import URIRef, Graph
from StringIO import StringIO
import subprocess as sub

class Wordnet():

    def __init__(self, graph):
        self.graph = Graph()

    def process_file(self, file):
        file = open("new_2.txt", "r")
        return file

    def line_for_loop(self, file):
        for line in file:
            self.split_pointer_part()
            self.split_word_part()
            self.split_gloss_part()
            self.process_lex_filenum()
            self.process_synset_offset()
            +more functions............
            self.print_graph()

    def split_pointer_part(self, before_at, after_at, line):
        before_at, after_at = line.split('@', 1)
        return before_at, after_at

    def get_num_words(self, word_part, num_words):
        """ 1 as default, may want 0 as an invalid case """
        """ do if else statements on l3 variable """
        if word_part[3] == '0a':
            num_words = 10
        else:
            num_words = int(word_part[3])
        return num_words

    def get_pointers_list(self, pointers, after_at, num_pointers, pointerList):
        pointers = after_at.split()[0:0 +4 * num_pointers:4]
        pointerList = iter(pointers)
        return pointerList

    ............code to create triples for graph...............

    def print_graph(self):
        print graph.serialize(format='nt')

    def main():
        wordnet = Wordnet()
        my_file = wordnet.process_file()
        wordnet.line_for_loop(my_file)

if __name__ == "__main__":
    main()

You question is mainly a question about what object oriented programming is. 您的问题主要是关于面向对象编程是什么的问题。 I will try to explain quickly, but I recommend reading a proper tutorial on it like http://www.voidspace.org.uk/python/articles/OOP.shtml http://net.tutsplus.com/tutorials/python-tutorials/python-from-scratch-object-oriented-programming/ and/or http://www.tutorialspoint.com/python/python_classes_objects.htm 我会尽快解释,但我建议你阅读一个适当的教程,如http://www.voidspace.org.uk/python/articles/OOP.shtml http://net.tutsplus.com/tutorials/python- tutorials / python-from-scratch-object-oriented-programming /和/或http://www.tutorialspoint.com/python/python_classes_objects.htm

When you create a class and instantiate it (with mywordnet=WordNet(somegraph)), you can resue the mywordnet instance many times. 当您创建一个类并实例化它(使用mywordnet = WordNet(somegraph))时,您可以多次重新生成mywordnet实例。 Each variable you set on self. 您在自己设置的每个变量。 in WordNet, is stored in that instance. 在WordNet中,存储在该实例中。 So for instance self.graph is always available if you call any method of mywordnet. 因此,例如,如果您调用mywordnet的任何方法,则self.graph始终可用。 If you wouldn't store it in self.graph, you would need to specify it as a parameter in each method (function) that requires it. 如果不将它存储在self.graph中,则需要将其指定为需要它的每个方法(函数)中的参数。 Which would be tedious if all of these method calls require the same graph anyway. 如果所有这些方法调用都需要相同的图形,这将是乏味的。

So to look at it another way: everything you set with self. 所以以另一种方式看待它:你用自己设定的一切。 can be seen as a sort of configuration for that specific instance of Wordnet. 可以看作是Wordnet特定实例的一种配置。 It influences the Wordnet behaviour. 它会影响Wordnet的行为。 You could for instance have two Wordnet instances, each instantiated with a different graph, but all other functionality the same. 例如,您可以拥有两个Wordnet实例,每个实例都使用不同的图形进行实例化,但所有其他功能都相同。 That way you can choose which graph to print to, depending on which Wordnet instance you use, but everything else stays the same. 这样,您可以选择要打印到哪个图表,具体取决于您使用的Wordnet实例,但其他所有内容都保持不变。

I hope this helps you out a little. 我希望这会帮助你一点点。

First, I suggest you figure out the basic functional decomposition on its own - don't worry about writing a class at all. 首先,我建议你自己弄清楚基本的功能分解 - 不要担心写一个类。

For example, 例如,

def split_pointer_part(self, before_at, after_at, line):
    before_at, after_at = line.split('@', 1)
    return before_at, after_at

doesn't touch any instance variables (it never refers to self ), so it can just be a standalone function. 不触及任何实例变量(它从不引用self ),因此它可以只是一个独立的函数。

It also exhibits a peculiarity I see in your other code: you pass two arguments ( before_at , after_at ) but never use their values. 它还展示了我在其他代码中看到的特性:传递两个参数( before_atafter_at )但从不使用它们的值。 If the caller doesn't already know what they are, why pass them in? 如果来电者还不知道他们是什么,为什么要把它们传递进去呢?

So, a free function should probably look like: 所以,一个自由函数应该看起来像:

def split_pointer_part(line):
    """get tuple (before @, after @)"""
    return line.split('@', 1)

If you want to put this function in your class scope (so it doesn't pollute the top-level namespace, or just because it's a logical grouping), you still don't need to pass self if it isn't used. 如果要将此函数放在类范围内(因此它不会污染顶级命名空间,或者只是因为它是逻辑分组),如果不使用它,您仍然不需要传递self You can make it a static method: 你可以使它成为一个静态方法:

@staticmethod
def split_pointer_part(line):
    """get tuple (before @, after @)"""
    return line.split('@', 1)

One thing that would be very helpful for you is a good visual debugger. 对你有用的一件事是一个很好的可视化调试器。 There's a nice free one for Python called Winpdb . Python有一个很好的免费版本,名为Winpdb There are also excellent debuggers in the commercial products IntelliJ IDEA/PyCharm, Komodo IDE, WingIDE, and Visual Studio (with the Python Tools add-in). 商业产品IntelliJ IDEA / PyCharm,Komodo IDE,WingIDE和Visual Studio(以及Python Tools插件)中也有出色的调试器。 Probably a few others too. 可能还有其他几个。

I highly recommend setting up one of these debuggers and running your code under it. 我强烈建议您设置其中一个调试器并在其下运行代码。 It will let you step through your code line by line and see what happens with all your variables and objects. 它将让您逐行浏览代码,看看所有变量和对象会发生什么。

You may find people who tell you that real programmers don't need or shouldn't use debuggers. 你可能会找到那些告诉你真正的程序员不需要或不应该使用调试器的人。 Don't listen to them: a good debugger is one of the very best tools to help you learn a new language or to get familiar with a piece of code. 不要听他们:一个好的调试器是帮助你学习一门新语言或熟悉一段代码的最好的工具之一。

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

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