Skip to content

Commit

Permalink
fix Weekday constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
sirfoga committed Oct 13, 2018
1 parent 314ed86 commit ef9aada
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 37 deletions.
4 changes: 2 additions & 2 deletions hal/__version__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
__title__ = 'PyHal'
__description__ = 'Your swiss knife to perform fast and easy pythonic stuff'
__url__ = 'https://sirfoga.github.io/pyhal/'
__version__ = '10.2.3'
__build__ = 'e6597adf0c42bc4db47cfd48714525c0297603c8'
__version__ = '10.2.6'
__build__ = '91b542ee617b39ed1cf0be82cdaed388bb0ab72b'
__author__ = 'Stefano Fogarollo'
__author_email__ = 'sirfoga@protonmail.com'
__license__ = 'MIT'
Expand Down
18 changes: 13 additions & 5 deletions hal/files/models/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,23 @@ def move_file_to_file(old_path, new_path):
os.rename(os.path.join(target_directory, old_file),
os.path.join(target_directory, target_file))

@staticmethod
def write_data_to_file(data, out_file):
def write_data(self, data):
"""Writes given data to given path file
:param data: data to write to file
:param out_file: path to output file
"""
with open(out_file, "w") as out_f:
out_f.write(data)
with open(self.path, "w") as writer:
writer.write(data)

def read_data(self):
"""
:return: contents of file
"""
try:
with open(self.path, "r") as reader:
return reader.read()
except:
return None

@staticmethod
def extract_name_extension(file_name):
Expand Down
2 changes: 1 addition & 1 deletion hal/files/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_lines(self):
:return: Lines in file
"""
with open(self.path) as data:
with open(self.path, "r") as data:
self.lines = data.readlines() # store data in arrays

return self.lines
Expand Down
2 changes: 1 addition & 1 deletion hal/profile/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ def run(self):
file_name = "EightQueenTest" + str(int(random.random() * 10000)) \
+ ".log"
log_file = os.path.join(os.getcwd(), file_name)
Document.write_data_to_file(self.benchmark, log_file)
Document(log_file).write_data(self.benchmark)

print("The log file is", log_file)
6 changes: 2 additions & 4 deletions hal/tests/gen.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os

from hal.files.models.files import Document
from hal.meta.attributes import get_modules, ModuleFile, MODULE_SEP

DOCS = "\"\"\"{}\"\"\""
Expand Down Expand Up @@ -154,7 +155,4 @@ def write_tests(self, output_folder):
file_name = mod.package.replace(MODULE_SEP, "_")
file_name = "test_" + file_name + ".py"
output_file = os.path.join(output_folder, file_name)

with open(output_file, "w") as writer:
contents = self.get_module_tests(mod)
writer.write(contents)
Document(output_file).write_data(self.get_module_tests(mod))
42 changes: 21 additions & 21 deletions hal/times/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,46 @@ class Weekday(Enum):
SATURDAY = 5
SUNDAY = 6

def __init__(self, weekday):
"""
:param weekday: day of week
"""
self.weekday = weekday
@staticmethod
def get_next(weekday, including_today=False):
"""Gets next day of week
def get_next(self, including_today=False):
"""
:param weekday: day of week
:param including_today: If today is sunday and requesting next sunday
:return: Date of next monday, tuesday ...
"""
now = datetime.datetime.now()
if now.weekday() == self.weekday.value and self.including_today:
if now.weekday() == weekday.value and including_today:
delta = datetime.timedelta(days=0)
elif now.weekday() == self.weekday.value and not including_today:
elif now.weekday() == weekday.value and not including_today:
delta = datetime.timedelta(days=7)
else:
delta = datetime.timedelta(
(7 + self.weekday.value - now.weekday()) % 7
(7 + weekday.value - now.weekday()) % 7
) # times delta to next instance
return Day(now + delta).get_just_date()

def get_last(self, including_today=False):
"""
@staticmethod
def get_last(weekday, including_today=False):
"""Gets last day of week
:param weekday: day of week
:param including_today: If today is sunday and requesting last sunday
:return: Date of last monday, tuesday ...
"""
now = datetime.datetime.now()
if now.weekday() == self.weekday.value and including_today:
if now.weekday() == weekday.value and including_today:
delta = datetime.timedelta(days=0)
elif now.weekday() == self.weekday.value and not including_today:
elif now.weekday() == weekday.value and not including_today:
delta = - datetime.timedelta(days=7)
else:
if now.weekday() > self.weekday.value:
if now.weekday() > weekday.value:
delta = - datetime.timedelta(
now.weekday() - self.weekday.value
now.weekday() - weekday.value
) # times delta
else:
delta = - datetime.timedelta(
now.weekday() + (7 - self.weekday.value)
now.weekday() + (7 - weekday.value)
) # times delta
return Day(now + delta).get_just_date()

Expand Down Expand Up @@ -88,8 +88,8 @@ def is_in_this_week(self):
:return: True iff date is in this week (from sunday to sunday)
"""
return self.is_date_in_between(
get_last_weekday(self.week_end, including_today=True),
get_next_weekday(self.week_end)
Weekday.get_last(self.week_end, including_today=True),
Weekday.get_next(self.week_end)
)

def is_date_in_between(self, start, end):
Expand All @@ -113,7 +113,7 @@ def get_next_weekday(self, including_today=False):
:return: Date of next monday, tuesday ...
"""
weekday = self.date_time.weekday()
return Weekday(weekday).get_next(including_today=including_today)
return Weekday.get_next(weekday, including_today=including_today)

def get_last_weekday(self, including_today=False):
"""Gets last week day
Expand All @@ -122,4 +122,4 @@ def get_last_weekday(self, including_today=False):
:return: Date of last monday, tuesday ...
"""
weekday = self.date_time.weekday()
return Weekday(weekday).get_last(including_today=including_today)
return Weekday.get_last(weekday, including_today=including_today)
8 changes: 7 additions & 1 deletion tests/test_hal_files_models_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ def test_move_file_to_file():
pass # todo auto generated method stub

@staticmethod
def test_write_data_to_file():
def test_write_data():
"""Tests hal.files.models.files.Document.write_data_to_file method"""

pass # todo auto generated method stub

@staticmethod
def test_read_data():
"""Tests hal.files.models.files.Document.read_data_to_file method"""

pass # todo auto generated method stub

@staticmethod
def test_extract_name_extension():
"""Tests hal.files.models.files.Document.extract_name_extension method"""
Expand Down
18 changes: 16 additions & 2 deletions tests/test_hal_times_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

"""Tests hal.times.dates implementation"""

import datetime

from hal.times.dates import Weekday


class TestWeekday:
"""Tests Weekday class"""
Expand All @@ -11,13 +15,23 @@ class TestWeekday:
def test_get_next():
"""Tests hal.times.dates.Weekday.get_next method"""

pass # todo auto generated method stub
day = Weekday.MONDAY
next_day = Weekday.get_next(day)
today = datetime.datetime.today()
delta = next_day - today

assert delta.days < 7

@staticmethod
def test_get_last():
"""Tests hal.times.dates.Weekday.get_last method"""

pass # todo auto generated method stub
day = Weekday.MONDAY
last_day = Weekday.get_last(day)
today = datetime.datetime.today()
delta = today - last_day

assert delta.days < 7


class TestDay:
Expand Down

0 comments on commit ef9aada

Please sign in to comment.