Skip to content

Commit 2fb7bd2

Browse files
authored
Merge pull request #5 from hayat01sh1da/hayat01sh1da/optimise-docs-and-code-designs
Optimise Docs and Code Designs
2 parents ac13965 + d64d134 commit 2fb7bd2

5 files changed

Lines changed: 32 additions & 46 deletions

File tree

python/main.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,16 @@
55
sys.path.append('./src')
66
from application import Application
77

8-
username = input('Provide your username(Default: hayat01sh1da): ')
9-
unit = input('Provide your preferred unit d(daily - default), w(weekly) or m(monthly): ')
10-
year = input('Provide the specific year you would like to create working report templates for(Default: the current year): ')
8+
username = input('Provide your username(Default: hayat01sh1da): ').strip()
9+
unit = input('Provide your preferred unit d(daily - default), w(weekly) or m(monthly): ').strip()
10+
year = input('Provide the specific year you would like to create working report templates for(Default: the current year): ').strip()
1111

12-
if username and unit and year:
13-
app = Application(username = username, unit = unit, year = year)
14-
elif username:
15-
app = Application(username = username)
16-
elif unit:
17-
app = Application(unit = unit)
18-
elif year:
19-
app = Application(year = year)
20-
else:
21-
app = Application()
12+
params = dict()
13+
for key, value in { 'username': username, 'unit': unit, 'year': year }.items():
14+
if value:
15+
params[key] = value
2216

23-
app.run()
17+
Application(**params).run()
2418

2519
pycaches = glob.glob(os.path.join('.', '**', '__pycache__'), recursive = True)
2620
for pycache in pycaches:

python/src/application.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ def __export_template__(self, directory, index, day = '', month = ''):
132132
date += f'{day} '
133133
date += f'{month} {self.year}'
134134
filename = os.path.join(directory, f'{self.year}{index}{day}_{self.__full_unit__()}_working_report.md')
135-
if not os.path.exists(directory):
136-
os.makedirs(directory)
135+
os.makedirs(directory, exist_ok = True)
137136
with open(filename, 'w') as f:
138137
f.write(self.__body__(date))
139138

python/test/test_application.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,29 @@ def test_run_by_monthly_unit(self):
5757

5858
class TestIrregularCase(TestApplication):
5959
def test_initialize_with_invalid_username(self):
60-
with self.assertRaises(ValueError, msg = 'InvalidUsername is NOT a permitted username.'):
60+
with self.assertRaises(ValueError) as cm:
6161
Application(username = 'InvalidUsername', unit = 'foobar', year = self.year).run()
62+
self.assertEqual('InvalidUsername is NOT a permitted username.', str(cm.exception))
6263

6364
def test_initialize_with_invalid_unit(self):
64-
with self.assertRaises(ValueError, msg = 'Provide d, w or m as a valid unit.'):
65+
with self.assertRaises(ValueError) as cm:
6566
Application(username = self.username, unit = 'foobar', year = self.year).run()
67+
self.assertEqual('Provide d, w or m as a valid unit.', str(cm.exception))
6668

6769
def test_initialize_with_non_digit_argument(self):
68-
with self.assertRaises(ValueError, msg = 'invalid literal for int() with base 10: "foobar"'):
70+
with self.assertRaises(ValueError) as cm:
6971
Application(username = self.username, year = 'foobar')
72+
self.assertEqual('invalid literal for int() with base 10: \'foobar\'', str(cm.exception))
7073

7174
def test_initialize_with_invalid_value_as_year(self):
72-
with self.assertRaises(ValueError, msg = 'Year must be 4 digits.'):
75+
with self.assertRaises(ValueError) as cm:
7376
Application(username = self.username, year = '20233')
77+
self.assertEqual('Year must be 4 digits.', str(cm.exception))
7478

7579
def test_initialize_with_older_year(self):
76-
with self.assertRaises(ValueError, msg = 'Provide newer than or equal to the current year.'):
80+
with self.assertRaises(ValueError) as cm:
7781
Application(username = self.username, year = '2022')
82+
self.assertEqual('Provide newer than or equal to the current year.', str(cm.exception))
7883

7984
if __name__ == '__main__':
8085
unittest.main()

ruby/main.rb

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
require_relative './src/application'
22

33
puts 'Provide your username(Default: hayat01sh1da)'
4-
_username = gets.chomp
4+
username = gets.chomp.strip
55

66
puts 'Provide your preferred unit d(daily - default), w(weekly) or m(monthly)'
7-
_unit = gets.chomp
7+
unit = gets.chomp.strip
88

99
puts 'Provide the specific year you would like to create working report templates for(Default: the current year)'
10-
_year = gets.chomp
10+
year = gets.chomp.strip
1111

12-
username = _username.empty? ? nil : _username
13-
unit = _unit.empty? ? nil : _unit
14-
year = _year.empty? ? nil : _year
12+
params = { username:, unit:, year: }.reject { |_, value| value.empty? }
1513

16-
if username && unit && year
17-
Application.run(username:, unit:, year:)
18-
elsif username
19-
Application.run(username:)
20-
elsif unit
21-
Application.run(unit:)
22-
elsif year
23-
Application.run(year:)
24-
else
25-
Application.run
26-
end
14+
Application.run(**params)

ruby/src/application.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ class DigitLengthError < StandardError; end
88
USERNAMES = ['hayat01sh1da'].freeze
99

1010
def self.run(username: 'hayat01sh1da', unit: 'd', year: Time.now.year.to_s)
11-
instance = new(username, unit, year)
12-
instance.validate_username!(username)
13-
instance.validate_unit!(unit)
14-
instance.validate_year!(year)
11+
instance = new(username:, unit:, year:)
12+
instance.validate_username!
13+
instance.validate_unit!
14+
instance.validate_year!
1515
instance.run
1616
end
1717

18-
def initialize(username, unit, year)
18+
def initialize(username: 'hayat01sh1da', unit: 'd', year: Time.now.year.to_s)
1919
@username = username
2020
@unit = unit
2121
@year = year
2222
end
2323

24-
def validate_username!(username)
24+
def validate_username!
2525
raise ValueError, "#{username} is NOT a permitted username." unless USERNAMES.include?(username)
2626
end
2727

28-
def validate_unit!(unit)
28+
def validate_unit!
2929
case unit
3030
when 'd', 'w', 'm'
3131
unit
@@ -34,7 +34,7 @@ def validate_unit!(unit)
3434
end
3535
end
3636

37-
def validate_year!(year)
37+
def validate_year!
3838
Integer(year)
3939
if year.length > 4
4040
raise DigitLengthError, 'Year must be 4 digits.'

0 commit comments

Comments
 (0)