1

I can call objects out of a method in a fashion that explicitly defines an endpoint ( this REST API is really a collection of RESTful endpoints ):

ie:

import symphony

pod_object = symphony.Pod(self.__uri__, self.__session__, self.__keymngr__)
agent_object = symphony.Agent(self.__uri__, self.__session__, self.__keymngr__)

agent.userid_by_email(email)
pod.send_message(userid, message)

This would be the __init__.py for a Module symphony

from .Agent import Agent
from .Auth import Auth
from .Config import Config
from .Crypt import Crypt
from .Mml import Mml
from .Pod import Pod
from .RESTful import RESTful

This would be the __init__.py for a Class Pod in Module symphony

class Pod():
    # basic methods
    from .base import sessioninfo
    # user methods
    from .users import get_userid_by_email
    from .users import get_user_id_by_user
    from .users import user_feature_update
    from .users import search_user
    # streams methods
    from .streams import adduser_to_stream
    from .streams import create_stream
    from .streams import create_stream_ni
    from .streams import create_room
    from .streams import update_room
    # group methods
    from .groups import ib_group_list
    from .groups import ib_group_member_list
    from .groups import ib_group_member_add
    from .groups import ib_group_policy_list
    # connection methods
    from .connections import list_connections
    from .connections import connection_status
    from .connections import accept_connection
    from .connections import create_connection

    def __init__(self, url, session, keymngr):
        self.__url__ = url
        self.__session__ = session
        self.__keymngr__ = keymngr
        self.__rest__ = symphony.RESTful(self.__url__, self.__session__, self.__keymngr__)

I have something like this. And I really like this.. Cause I can then have many classes divided into Dir structures with many methods divided into many files. Clean simple code. All registering to a module later on down the line.

But I have to individually register methods, instead of the entire file.

Is there some way to cleanly import all the methods in the files?

Python2 / 3 compatible way?

Matt Joyce
  • 2,010
  • 2
  • 20
  • 31
  • 1
    Good grief. Nope, that's not clean and simple in my book, sorry. All those names you import will need to have `self` as a first argument, and being outside of a class context, that'll be nice and confusing to other Python developers coming to the project. Why not just inheritance at the very least? – Martijn Pieters Apr 22 '17 at 19:13
  • not really... you import it into the base module and it becomes as easy as instantiating the module and accessing all the methods direct from the module. ie: from .Pod import Pod in __init__.py for tld of module. – Matt Joyce Apr 22 '17 at 19:17
  • You could `update` the class dictionary with (a slightly filtered version of) the module dictionaries? But honestly the whole thing looks horrenous to me. – DavidW Apr 22 '17 at 19:19
  • 1
    Whether or not this is a good way to organize your code, keep in mind the fact that it will break 0-argument `super`, since the weird compiler hacks used to implement 0-argument `super` rely on the method definition being lexically within the class body. – user2357112 Apr 22 '17 at 19:19
  • 2
    Buliding on what Martijn said above, Python's inheritance model makes it pretty easy to create [mixin classes](http://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful). IMO packaging the above into `UserMixin`, `StreamsMixin`, `GroupMixin`, etc. would be much cleaner. – ChrisGPT was on strike Apr 22 '17 at 19:20
  • I'll take a look at mixins, haven't used them before. Welcome to other ideas, though. – Matt Joyce Apr 22 '17 at 19:22

1 Answers1

-1

That is, well, really, ah, not all that pretty.

If your class is too big to fit in one file, it's just too big and should be divided into several classes.

The obvious solution is to create several base classes, one for user methods, one for stream methods, one for group methods and so on. Then have your Pod class inherit from all of them.

Without knowing more about the problem you're trying to solve it's impossible to recommend a different way to design your classes. It does feel as if you're not doing the right thing, though, even if you do get Pod to inherit from all those base classes.

zmbq
  • 38,013
  • 14
  • 101
  • 171