#π Any way to cut down on this boilerplate?
29 messages Β· Page 1 of 1 (latest)
@waxen bramble
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
people seem to really hate this, but it's working pretty well for me
was genuinely interested in if there was a better way to do this
I saw importlib could help, but i didn't want to add another dependency at the time
now that things are working, i can revisit some of this structure
It does seem like quite the odd way of doing this. Would merging the files together into a single one work better? What do you want to achieve with this structure?
the file would be huge
if that's the way it's generally done in python, that's fine, but a 1000+ line file would be very difficult to maintain
The entire file-per-method thing is probably okay given that this looks like a typical HTTP API. What is probably being objected here is the way you're importing them in the main file:
from .teams import _get_all_teams
def get_all_teams(self):
return self._get_all_teams()
from .users import _update_user_by_id
def update_user_by_id(self, user_id):
return self._update_user_by_id(user_id)
It is clear from the _ at the start that these are meant to be private methods not meant to be used externally. So there should be no issue with putting them at the top.
but the name conflicts
The main thing I would say is to not use relative imports if you can, since that makes everything a pain if you ever want to run an individual file. Besides that, while this is a bit more segmented than I normally do, I have no problems with splitting class internals into different files.
If the import would cause a performance detriment (maybe it depends on a huge library or something), perhaps putting it inside the function would work:
def update_user_by_id(self, user_id):
from .users import _update_user_by_id
return _update_user_by_id(user_id)
i'm not overly concerned with performance, i can address that if/when it becomes a problem
I'm assuming that get_all_teams is inside the MyClient class. If that's the case, it should not conflict with the other import.
hm, i'll have to take a closer look at that
This also might be a confusion of intent due to the naming. Normally, if an anything is prefixed by an underscore _something, that means it is an internal, and not supposed to be used elsewhere. This also includes elsewhere in your code, so anything with an underscore prefix in say users should only be used in users.
yeah that makes sense
oh, i can just:
from .alerts import update_alerts_as_read
and nix the:
def update_alerts_as_read(self, alert_ids):
return self.update_alerts_as_read(alert_ids)
entirely i think
would that still pass self as the first parameter?
it would if you import it inside your class definition
i wouldn't have to additionally do something like:
from somewhere import bar
self.bar = bar
I'm never sure of something I'm about to say so I tested it and this seems to be the behaviour that you're looking for
yeah you wouldn't have to do the self.bar stuff unless you wanted to assign the methods in the object's __init__ function (plus we don't have a sense of 'self' in the scope that I'm importing some_method)
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
π Any way to cut down on this boilerplate?