#πŸ”’ Any way to cut down on this boilerplate?

29 messages Β· Page 1 of 1 (latest)

waxen bramble
night belfryBOT
#

@waxen bramble

Python help channel opened

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.

waxen bramble
#

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

dense stirrup
#

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?

waxen bramble
#

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

dense stirrup
#

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.

waxen bramble
#

but the name conflicts

celest gust
dense stirrup
waxen bramble
#

i'm not overly concerned with performance, i can address that if/when it becomes a problem

dense stirrup
waxen bramble
#

hm, i'll have to take a closer look at that

celest gust
#

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.

waxen bramble
#

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?

hasty yoke
#

it would if you import it inside your class definition

waxen bramble
#

i wouldn't have to additionally do something like:

from somewhere import bar
self.bar = bar
hasty yoke
#

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

waxen bramble
#

that's awesome

#

thanks!

hasty yoke
#

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)

night belfryBOT
#
Python help channel closed

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?