patternpythonModerate
Reducing lines in methods of Python class to manage tweets
Viewed 0 times
reducingmanagemethodspythontweetsclasslines
Problem
How could I reduce the length of my functions restricting them to max 10 lines?
Other comments are welcome on the code in general.
```
import config_files
import math
from datetime import datetime
import importlib
import sys
from scan_twittosphere import *
from tweets import *
import nltk
debugger = importlib.import_module('config_files.debugger')
"""
Performs operations of allocating lr users to pertinent tweets and users and sending the result back
to create_json object
"""
class filter_tweets:
"""
method: constructor
input:
String: lr user screen name
String List: List of user keywords
Object: Twitter api connection object
Object: Config file object
Integer: User Autofavorite mode
String: environment of execution
output: None
"""
def __init__(self,screen_name, keywords, api, config,mode_auto_favorite, env):
self.screen_name = screen_name
self.keywords = keywords
self.api = api
self.config = config
self.mode_auto_favorite = mode_auto_favorite
self.MongoObj = MongodbConnections(env)
self.count = 0
"""
method: This method returns list of tweets and meta data back
input:
Object: scan_twittosphere object
output:
Dictionary list: Twitter dictionary list element containing
Integer: Tweet id
Float: lr tweet score
String: Keyword of tweet
"""
def stream_score_tweets(self,scan_twittosphere_obj):
tweets_list = []
'''
search UserFactory
'''
for keyword in self.keywords:
self.count = 0
if self.count = self.config.max_tweet:
return tweets_list
return tweets_list
"""
method: This method adds a tweet to the favorites section of the user
input:
Integer: Tweet id
output:
Boolean: True or (Fal
Other comments are welcome on the code in general.
```
import config_files
import math
from datetime import datetime
import importlib
import sys
from scan_twittosphere import *
from tweets import *
import nltk
debugger = importlib.import_module('config_files.debugger')
"""
Performs operations of allocating lr users to pertinent tweets and users and sending the result back
to create_json object
"""
class filter_tweets:
"""
method: constructor
input:
String: lr user screen name
String List: List of user keywords
Object: Twitter api connection object
Object: Config file object
Integer: User Autofavorite mode
String: environment of execution
output: None
"""
def __init__(self,screen_name, keywords, api, config,mode_auto_favorite, env):
self.screen_name = screen_name
self.keywords = keywords
self.api = api
self.config = config
self.mode_auto_favorite = mode_auto_favorite
self.MongoObj = MongodbConnections(env)
self.count = 0
"""
method: This method returns list of tweets and meta data back
input:
Object: scan_twittosphere object
output:
Dictionary list: Twitter dictionary list element containing
Integer: Tweet id
Float: lr tweet score
String: Keyword of tweet
"""
def stream_score_tweets(self,scan_twittosphere_obj):
tweets_list = []
'''
search UserFactory
'''
for keyword in self.keywords:
self.count = 0
if self.count = self.config.max_tweet:
return tweets_list
return tweets_list
"""
method: This method adds a tweet to the favorites section of the user
input:
Integer: Tweet id
output:
Boolean: True or (Fal
Solution
There is nothing inherently wrong with having methods longer than 10 lines. If there are no reusable elements in those methods then the only reason to break them into more methods is aesthetic (and can also improve readability if you name your methods well).
A couple of comments on the rest of the code:
Imports
Try to avoid
and if you are just using a few bit and pieces:
Docstrings
While your docstrings are nicely laid out and readable, they should reside inside the method, function or class they pertain to, not above. See here for the appropriate PEP257 docstring conventions, http://legacy.python.org/dev/peps/pep-0257/.
New-style classes
New-style classes (https://www.python.org/doc/newstyle/) should inherit from
should be:
Naming conventions
So generally it good to follow PEP8 guidelines (http://legacy.python.org/dev/peps/pep-0008/) for naming of variables, functions, classes, etc.
Each word in a class name should be capitalised and there should be no underscores:
goes to:
also to quote PEP8:
A style guide is about consistency. Consistency with this style guide
is important. Consistency within a project is more important.
Consistency within one module or function is most important.
This applies to your
you aren't using those return values anywhere. Also it may be worth catching the certain errors that might be caused by your code, like invalid types for
All in all the code looks pretty good, with nice documentation and (even more importantly) descriptive variable names.
A couple of comments on the rest of the code:
Imports
from scan_twittosphere import *
from tweets import *Try to avoid
import * whenever possible. It clutters the namespace and makes the code much less readable to people who do not already know the libraries you are using well. If you are using many methods or classes from these modules use:import scan_twittosphere as stw
import tweets as twand if you are just using a few bit and pieces:
from scan_twittosphere import ,,
from tweets import ,,Docstrings
While your docstrings are nicely laid out and readable, they should reside inside the method, function or class they pertain to, not above. See here for the appropriate PEP257 docstring conventions, http://legacy.python.org/dev/peps/pep-0257/.
New-style classes
New-style classes (https://www.python.org/doc/newstyle/) should inherit from
object. i.e. class filter_tweets:should be:
class filter_tweets(object):Naming conventions
So generally it good to follow PEP8 guidelines (http://legacy.python.org/dev/peps/pep-0008/) for naming of variables, functions, classes, etc.
Each word in a class name should be capitalised and there should be no underscores:
class filter_tweets(object):goes to:
class FilterTweets(object):also to quote PEP8:
A style guide is about consistency. Consistency with this style guide
is important. Consistency within a project is more important.
Consistency within one module or function is most important.
This applies to your
addFavorite method, which should really be add_favorite. Speaking of this method:def addFavorite(self,tweet_id):
if self.mode_auto_favorite == 1:
try:
self.api.create_favorite(tweet_id)
return True
except:
return Falseyou aren't using those return values anywhere. Also it may be worth catching the certain errors that might be caused by your code, like invalid types for
tweet_id (as python is dynamically typed). This isn't a big issue, but it can often help with debugging.All in all the code looks pretty good, with nice documentation and (even more importantly) descriptive variable names.
Code Snippets
from scan_twittosphere import *
from tweets import *import scan_twittosphere as stw
import tweets as twfrom scan_twittosphere import <names of>,<functions>,<or classes>
from tweets import <names of>,<functions>,<or classes>class filter_tweets:class filter_tweets(object):Context
StackExchange Code Review Q#46162, answer score: 11
Revisions (0)
No revisions yet.