patternpythonMinor
Activity time & duration tracker
Viewed 0 times
durationactivitytrackertime
Problem
This is my first attempt at creating a simple Python tool that tracks the time of certain activities retrieved from a database. After stopping the timer, this tool writes the duration of the activity into the database.
Can you please provide me some tricks to make this code more Pythonic?
objects.py
```
#!/usr/bin/env python
# -- coding: utf-8 --
class Project:
'''Class that holds the projects'''
projects = []
def __init__(self, name):
self.name = name
self.tasks = []
type(self).projects.append(self)
def __str__(self):
return self.name
@property
def name(self):
return self._name
@name.setter
def name(self, name):
if not name:
raise ValueError(_('You need to specify a value!'))
self._name = name
def add_task(self, task):
if (task not in self.tasks):
self.tasks.append(task)
def get_tasks(self):
return self.tasks
@staticmethod
def get_project_by_name(name):
for project in Project.projects:
if name == project.name:
return project
class Task:
'''A task for a project'''
def __init__(self, id, name):
self.id = id
self.name = name
def __str__(self):
return '{0}:{1}'.format(self.id, self.name)
def __eq__(self, other):
return self.id == other.id and self.name == other.name
@property
def name(self):
return self._name
@name.setter
def name(self, name):
if not name:
raise ValueError(_('You need to specify a value!'))
self._name = name
class Entry:
'''A timesheet entry that contains the task, the date and the time spent'''
def __init__(self, task, date, duration):
self.task = task
self.date = date
self.time_in_minutes = self._get_dur
Can you please provide me some tricks to make this code more Pythonic?
objects.py
```
#!/usr/bin/env python
# -- coding: utf-8 --
class Project:
'''Class that holds the projects'''
projects = []
def __init__(self, name):
self.name = name
self.tasks = []
type(self).projects.append(self)
def __str__(self):
return self.name
@property
def name(self):
return self._name
@name.setter
def name(self, name):
if not name:
raise ValueError(_('You need to specify a value!'))
self._name = name
def add_task(self, task):
if (task not in self.tasks):
self.tasks.append(task)
def get_tasks(self):
return self.tasks
@staticmethod
def get_project_by_name(name):
for project in Project.projects:
if name == project.name:
return project
class Task:
'''A task for a project'''
def __init__(self, id, name):
self.id = id
self.name = name
def __str__(self):
return '{0}:{1}'.format(self.id, self.name)
def __eq__(self, other):
return self.id == other.id and self.name == other.name
@property
def name(self):
return self._name
@name.setter
def name(self, name):
if not name:
raise ValueError(_('You need to specify a value!'))
self._name = name
class Entry:
'''A timesheet entry that contains the task, the date and the time spent'''
def __init__(self, task, date, duration):
self.task = task
self.date = date
self.time_in_minutes = self._get_dur
Solution
Fix indentation in you objects.py, use 4 spaces instead of tabs+spaces
so it will look like this:
You don't need parenthesis here
Just write
You might want to store projects as dict(project.name: project) instead of list so this:
will be just:
In case if you will have multiple projects with the same name you can make it dict(project.name: [projects_list])
in database.py
I would not keep db connection always open, I would connect to it each time I need it.
then
will be prettier if:
in main.py
its not clear why you need state to be a global variable.
so it will look like this:
class Project:
'''Class that holds the projects'''
projects = []
def __init__(self, name):
self.name = name
self.tasks = []
type(self).projects.append(self)
...You don't need parenthesis here
if (task not in self.tasks):Just write
if task not in self.tasks:You might want to store projects as dict(project.name: project) instead of list so this:
@staticmethod
def get_project_by_name(name):
for project in Project.projects:
if name == project.name:
return projectwill be just:
@staticmethod
def get_project_by_name(name):
return Project.projects[name]In case if you will have multiple projects with the same name you can make it dict(project.name: [projects_list])
in database.py
I would not keep db connection always open, I would connect to it each time I need it.
then
for result in query:
task = Task(result[0], result[1])
Project.get_project_by_name(project_name).add_task(task)will be prettier if:
for _id, name in query:
task = Task(_id, name)
Project.get_project_by_name(project_name).add_task(task)in main.py
its not clear why you need state to be a global variable.
Code Snippets
class Project:
'''Class that holds the projects'''
projects = []
def __init__(self, name):
self.name = name
self.tasks = []
type(self).projects.append(self)
...if (task not in self.tasks):if task not in self.tasks:@staticmethod
def get_project_by_name(name):
for project in Project.projects:
if name == project.name:
return project@staticmethod
def get_project_by_name(name):
return Project.projects[name]Context
StackExchange Code Review Q#144729, answer score: 2
Revisions (0)
No revisions yet.