patternpythonMinor
Tabulating a report
Viewed 0 times
reporttabulatingstackoverflow
Problem
I have a method for generating a report in Python. I want to organize the section about the date into a separate method so I can repeat the report for different dates.
What's the best way to organize this? Should I create a method using just *args or **kwargs where I can dump in all the various dicts...etc.? Does it make sense to be creating all these dicts in the first place?
Here's a link to the other modules.
The whole thing just feels messy and I'm not sure how or where to break it down into smaller chunks.
```
import sys
import os
from collection import Coll # a module I wrote
import pymongo
from pymongo import MongoClient
from datetime import datetime as dt
from agents import Agents # a module I wrote
from user import Users # a module I wrote
from propertie import Propertie # a module I wrote
from emails import Emails # a module I wrote
from bson.objectid import ObjectId
import time
import datetime
from tabulate import tabulate
import collections
import pdb
class Report(object):
"""creat the report from all the data"""
def __init__(self, database_name, db_collections):
self.database_name = database_name
self.db_collections = db_collections
def RcreateReport(self):
agents = Agents(self.database_name) #agent class in agent module
user = Users(self.database_name) # collections_dict['user']
propertie = Propertie(self.database_name) #collections_dict['property']
emails = Emails(self.database_name)
agent_coll_obj = agents.returnCollection()
user_coll_obj = user.returnCollection()
propertie_coll_obj = propertie.returnCollection()
email_coll_obj = emails.returnCollection()
#### this next section, I think, should be organized into a separate method
#### as the code is repeated below using dates.
user_id_email_dict = user.CidToEmailD() # dict of all user ids and emails
user_email_id_dict = user.Cema
What's the best way to organize this? Should I create a method using just *args or **kwargs where I can dump in all the various dicts...etc.? Does it make sense to be creating all these dicts in the first place?
Here's a link to the other modules.
The whole thing just feels messy and I'm not sure how or where to break it down into smaller chunks.
```
import sys
import os
from collection import Coll # a module I wrote
import pymongo
from pymongo import MongoClient
from datetime import datetime as dt
from agents import Agents # a module I wrote
from user import Users # a module I wrote
from propertie import Propertie # a module I wrote
from emails import Emails # a module I wrote
from bson.objectid import ObjectId
import time
import datetime
from tabulate import tabulate
import collections
import pdb
class Report(object):
"""creat the report from all the data"""
def __init__(self, database_name, db_collections):
self.database_name = database_name
self.db_collections = db_collections
def RcreateReport(self):
agents = Agents(self.database_name) #agent class in agent module
user = Users(self.database_name) # collections_dict['user']
propertie = Propertie(self.database_name) #collections_dict['property']
emails = Emails(self.database_name)
agent_coll_obj = agents.returnCollection()
user_coll_obj = user.returnCollection()
propertie_coll_obj = propertie.returnCollection()
email_coll_obj = emails.returnCollection()
#### this next section, I think, should be organized into a separate method
#### as the code is repeated below using dates.
user_id_email_dict = user.CidToEmailD() # dict of all user ids and emails
user_email_id_dict = user.Cema
Solution
Follow standard formatting
It is common practice to put standard modules first and then personal or non standard ones, like:
Where the first block is Standard, the second one is easily downloadable and the third is your own.
You use huge amounts of spaces (e.x.
Stop writing classes
In Python functions can float freely around in the global namespace, classes should be used only to combine data-structures with data.
Close automatically
It is nice that you remembered
Try and use named constants
For example the filename
And yes you should use smaller functions
Try and extract the smaller pieces of functionality inside theyr own function, you only can do it as you are the one with the most experience with this code.
It is common practice to put standard modules first and then personal or non standard ones, like:
import sys
import os
from datetime import datetime as dt
import time
import datetime
import collections
import pymongo
from pymongo import MongoClient
from tabulate import tabulate
from bson.objectid import ObjectId
import pdb
from collection import Coll
from agents import Agents
from user import Users
from propertie import Propertie
from emails import EmailsWhere the first block is Standard, the second one is easily downloadable and the third is your own.
You use huge amounts of spaces (e.x.
user_id_email_dict = user.CidToEmailD() # dict of all user ids and emails
that are usually avoided in Python.Stop writing classes
In Python functions can float freely around in the global namespace, classes should be used only to combine data-structures with data.
Close automatically
It is nice that you remembered
f.close() but you could easily have forgotten it, instead I suggest using with to handle close automatically.Try and use named constants
For example the filename
"report.txt" is arbitrary, I would define a top module constant REPORT_FILE = report.txt".And yes you should use smaller functions
Try and extract the smaller pieces of functionality inside theyr own function, you only can do it as you are the one with the most experience with this code.
Code Snippets
import sys
import os
from datetime import datetime as dt
import time
import datetime
import collections
import pymongo
from pymongo import MongoClient
from tabulate import tabulate
from bson.objectid import ObjectId
import pdb
from collection import Coll
from agents import Agents
from user import Users
from propertie import Propertie
from emails import EmailsContext
StackExchange Code Review Q#91938, answer score: 2
Revisions (0)
No revisions yet.