Created
December 27, 2020 01:24
-
-
Save vitalizzare/193bbe017230e24d23f42ba84f8ac589 to your computer and use it in GitHub Desktop.
Split module into multiple files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .social_media_post import SocialMediaPost | |
from .social_media_post_publisher import SocialMediaPostPublisher | |
# This is used by 'from socsocial_media_post import *' | |
# As far as there is nothing else here and in app.py | |
# we import the classes explicitely, this part can be ommited | |
__all__ = [ | |
'SocialMediaPost', | |
'SocialMediaPostPublisher' | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from social_media_post import ( | |
SocialMediaPost, | |
SocialMediaPostPublisher, | |
) | |
post = SocialMediaPost('Hey guys!') | |
# NOTE: here SocialMediaPostPublisher is identified by media, not by post | |
publisher = SocialMediaPostPublisher(['Facebook', 'Twitter']) | |
publisher.publish(post) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Facebook: | |
# See notes for Twitter | |
def publish(self, post): | |
print(f'Publish on Facebook via its API: {post.content}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedIn: | |
# See notes for Twitter | |
def publish(self, post): | |
print(f'Publish on Linkedin via its API: {post.content}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' NOTE: gist.github.com does not allow directory creation. | |
Helper to make a social_media_post directory and move there files, | |
derived from social_media_post.py | |
''' | |
from pathlib import Path | |
module_folder = Path('social_media_post') | |
module_folder.mkdir() | |
files = [ | |
'__init__.py', | |
'twitter.py', | |
'facebook.py', | |
'linkedin.py', | |
'social_media_post_publisher.py', | |
'social_media_post.py', | |
] | |
files = [Path(file) for file in files] | |
for file in files: | |
file.replace(module_folder/file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Twitter: | |
# NOTE: see for the opportunity to use a metaclass to force derived classes | |
# explicitly declare the publish method | |
def publish(self, post): | |
# TODO: replace 'Twitter' with self.__class__.__name__ | |
print(f'Publish on Twitter via its API: {post.content}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python make_project_structure.py
python app.py
See code on github
See original tweet