1

I have a class which has the function of parsing data.

class DataContainer(object):
    def parser1(data):
        # Handle data in one way
        self.parsed_data = parsed_data

    def parser2(data):
        # Handle data another way
        self.parsed_data = parsed_data

The parser functions popular the instance variables of the class. This parser may be changed or have many variations, so I would like to import another file with the functions, something like this:

class DataContainer(object):
    import parsers # Contains all the parsing functions which can then be called from instances

Is there a particular 'pythonic' way to do this?

4
  • Those aren't valid method definitions; there is no argument named self. Commented Aug 4, 2016 at 19:07
  • This code won't even run as it's riddled with NameErrors. But to answer your question, no there isn't because what you are trying to do isn't Pythonic. What is better about having the methods in a different file rather than just in the class? What problem does this solve? Why is this even a class if it has no attributes? Commented Aug 4, 2016 at 19:09
  • 1
    Why do they all need to belong to the same class? The more usual way would be to have subclasses of a parser base class, I think. Commented Aug 4, 2016 at 19:10
  • Nothing wrong except some bad syntax. If you use functions that you import, it's okay to import them at the top of the file, and just implement the DataContainer class in this file. Since you imported them up top, you can use them in the class. Commented Aug 4, 2016 at 19:23

1 Answer 1

1

It depends on exactly how you want to use your object, but I would import parsers, and then have your DataContainer serve as an interface to those functions

import parsers

class DataContainer(object):

    def __init__(self):
        # If this kind of thing is needed for the library
        self.parsers = parsers.Parser() 

    def parser1(self,data):
        # prep data however you need
        parsed_data = self.parsers.parse_method1(prep_data)
        # set instance variables from parsed_data
Sign up to request clarification or add additional context in comments.

2 Comments

I would usually import just the specific functions/classes that are desired. "from parsers import parser1, parser2"
This makes sense. My only concern is that the parse_method needs access to the instance variables, so something like this. For example, if the 'DataContainer' class has instance variables self.sorted_data_1, self.sorted_data_2, self.sorted_data_3, the parser should assign values to all those variables from the raw data. I supposed the cleaner thing to do might be to make a static method that outputs the required class variables, and then assign them to the instance variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.