From 17028625105ea2c9ea1136ea85717b746f6986c1 Mon Sep 17 00:00:00 2001 From: zArjun Date: Thu, 2 Mar 2023 11:48:55 +0530 Subject: [PATCH 1/5] Using local scripts as imported modules --- import_local_scripts.py | 15 +++++++++++++++ useful_functions.py | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100755 import_local_scripts.py create mode 100755 useful_functions.py diff --git a/import_local_scripts.py b/import_local_scripts.py new file mode 100755 index 0000000..60feef6 --- /dev/null +++ b/import_local_scripts.py @@ -0,0 +1,15 @@ +# import_local_scripts.py +import useful_functions as uf + +scores = [88, 92, 79, 93, 85] + +mean = uf.mean(scores) +curved = uf.add_five(scores) + +mean_c = uf.mean(curved) + +print("Scores:", scores) +print("Original Mean:", mean, " New Mean:", mean_c) + +print(__name__) +print(uf.__name__) \ No newline at end of file diff --git a/useful_functions.py b/useful_functions.py new file mode 100755 index 0000000..1cd8697 --- /dev/null +++ b/useful_functions.py @@ -0,0 +1,23 @@ +# useful_functions.py +# This is imported in import_local_scripts.py import_local_scripts + +def mean(num_list): + return sum(num_list) / len(num_list) + +def add_five(num_list): + return [n + 5 for n in num_list] + +def main(): + print("Testing mean function") + n_list = [34, 44, 23, 46, 12, 24] + correct_mean = 30.5 + assert(mean(n_list) == correct_mean) + + print("Testing add_five function") + correct_list = [39, 49, 28, 51, 17, 29] + assert(add_five(n_list) == correct_list) + + print("All tests passed!") + +if __name__ == '__main__': + main() \ No newline at end of file From a20ccd5eefaa5c9d721647501286bd8bd99b7bbf Mon Sep 17 00:00:00 2001 From: zArjun Date: Thu, 2 Mar 2023 11:52:19 +0530 Subject: [PATCH 2/5] Updated comments --- import_local_scripts.py | 1 + useful_functions.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/import_local_scripts.py b/import_local_scripts.py index 60feef6..ab998a5 100755 --- a/import_local_scripts.py +++ b/import_local_scripts.py @@ -1,4 +1,5 @@ # import_local_scripts.py +# Refer the 'useful_functions.py file for functions being imported import useful_functions as uf scores = [88, 92, 79, 93, 85] diff --git a/useful_functions.py b/useful_functions.py index 1cd8697..f0faac4 100755 --- a/useful_functions.py +++ b/useful_functions.py @@ -1,5 +1,5 @@ # useful_functions.py -# This is imported in import_local_scripts.py import_local_scripts +# This is imported in import_local_scripts.py file def mean(num_list): return sum(num_list) / len(num_list) From 9ca24fb02c27876f68a8c2818ce0ebbc228a30ee Mon Sep 17 00:00:00 2001 From: zArjun Date: Sat, 4 Mar 2023 14:41:38 +0530 Subject: [PATCH 3/5] Read selected strings from a file --- flying_circus_cast.txt | 64 ++++++++++++++++++++++++++++++++++++++++ read_string_from_file.py | 14 +++++++++ 2 files changed, 78 insertions(+) create mode 100755 flying_circus_cast.txt create mode 100755 read_string_from_file.py diff --git a/flying_circus_cast.txt b/flying_circus_cast.txt new file mode 100755 index 0000000..4dd91a3 --- /dev/null +++ b/flying_circus_cast.txt @@ -0,0 +1,64 @@ +Graham Chapman, Various / ... (46 episodes, 1969-1974) +Eric Idle, Various / ... (46 episodes, 1969-1974) +Terry Jones, Various / ... (46 episodes, 1969-1974) +Michael Palin, It's Man / ... (46 episodes, 1969-1974) +Terry Gilliam, Various / ... (46 episodes, 1969-1974) +John Cleese, Announcer / ... (40 episodes, 1969-1973) +Carol Cleveland, Various / ... (34 episodes, 1969-1974) +Ian Davidson, Algy Braithwaite / ... (8 episodes, 1969-1970) +John Hughman, Alfred Lord Tennyson / ... (8 episodes, 1970-1974) +The Fred Tomlinson Singers, Amantillado Chorus / ... (7 episodes, 1969-1973) +Connie Booth, Animated Mother / ... (6 episodes, 1969-1974) +Bob Raymond, 'Dad' / ... (5 episodes, 1974) +Lyn Ashley, Algon Girl / ... (5 episodes, 1970-1972) +Rita Davies, Argument Secretary / ... (4 episodes, 1969-1972) +Stanley Mason, Clapper Man / ... (4 episodes, 1970-1971) +David Ballantyne, Ivan the Terrible / ... (3 episodes, 1970-1971) +Donna Reading, Girl in Bikini with Its Man / ... (3 episodes, 1969) +Peter Brett, Door-to-Door Martial Arts Salesman (2 episodes, 1974) +Maureen Flanagan, Anona Winn / ... (2 episodes, 1969-1970) +Katya Wyeth, Elsie / ... (2 episodes, 1969) +Frank Lester, The Late Professor Thynne (2 episodes, 1972-1974) +Neil Innes, Hesitant guitarist / ... (2 episodes, 1974) +Dick Vosburgh, Van der Berg (1 episode, 1969) +Sandra Richards, 'Semprini' Girl / ... (1 episode, 1970) +Julia Breck, Puss In Boots / ... (1 episode, 1972) +Nicki Howorth, Miss Bladder (1 episode, 1972) +Jimmy Hill, Himself (1 episode, 1974) +Barry Cryer, Herman Rodrigues (1 episode, 1969) +Jeannette Wild, Second Secretary (1 episode, 1970) +Marjorie Wilde, Dear Old Lady (1 episode, 1970) +Marie Anderson, Girl interviewing the announcer (1 episode, 1972) +Caron Gardner, Mary (1 episode, 1973) +Nosher Powell, Jack Bodell (1 episode, 1973) +Carolae Donoghue, Vera's Husband's Mistress (1 episode, 1969) +Vincent Wong, Mr. Kamikaze (1 episode, 1970) +Helena Clayton, Various Roles (1 episode, 1971) +Nigel Jones, Various (1 episode, 1972) +Roy Gunson, (1 episode, 1970) +Daphne Davey, Various Roles (1 episode, 1971) +Stenson Falke, (1 episode, 1974) +Alexander Curry, Various (1 episode, 1970) +Frank Williams, Clerk of the Court (1 episode, 1972) +Ralph Wood, (1 episode, 1970) +Rosalind Bailey, Elizabethan Girl (1 episode, 1972) +Marion Mould, (1 episode, 1974) +Sheila Sands, Stripper / ... (uncredited) (2 episodes, 1969) +Richard Baker, Himself - BBC News Anchor (uncredited) (3 episodes, 1972-1973) +Douglas Adams, Dr. Emile Koning - Surgeon / ... (uncredited) (2 episodes, 1974) +Ewa Aulin, Harrassed Woman (uncredited) (1 episode, 1969) +Reginald Bosanquet, Himself (uncredited) (1 episode, 1970) +Barbara Lindley, Bride (uncredited) (1 episode, 1970) +Roy Brent, Armoured Knight (uncredited) (1 episode, 1972) +Jonas Card, Armoured Knight (uncredited) (1 episode, 1972) +Tony Christopher, Armoured Knight (uncredited) (1 episode, 1972) +Beulah Hughes, (uncredited) (1 episode, 1972) +Peter Kodak, Armoured Knight (uncredited) (1 episode, 1972) +Lulu, Herself (uncredited) (1 episode, 1972) +Jay Neill, Armoured Knight (uncredited) (1 episode, 1972) +Graham Skidmore, Armoured Knight (uncredited) (1 episode, 1972) +Ringo Starr, Himself (uncredited) (1 episode, 1972) +Fred Tomlinson, Superintendent McGough (uncredited) (1 episode, 1972) +David Hamilton, Himself - Thames TV Announcer (uncredited) (1 episode, 1973) +Suzy Mandel, German Girl (uncredited) (1 episode, 1974) +Peter Woods, BBC Presenter (uncredited) (1 episode, 1974) \ No newline at end of file diff --git a/read_string_from_file.py b/read_string_from_file.py new file mode 100755 index 0000000..17a2674 --- /dev/null +++ b/read_string_from_file.py @@ -0,0 +1,14 @@ +def create_cast_list(filename): + cast_list = [] + #use with to open the file filename + with open (filename) as f: + for line in f: + cast_list.append(line.split(',')[0]) + #use the for loop syntax to process each line + #and add the actor name to cast_list + + return cast_list + +c_list = create_cast_list('../python-programming/flying_circus_cast.txt') +for actor in c_list: + print(actor) \ No newline at end of file From 2dbe6c9b2c904b59f5b385db230d07bbe0893d1b Mon Sep 17 00:00:00 2001 From: zArjun Date: Wed, 8 Mar 2023 20:06:04 +0530 Subject: [PATCH 4/5] Created dev branch and added read string program --- flying_circus_cast.txt | 2 +- read_string_from_file.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/flying_circus_cast.txt b/flying_circus_cast.txt index 4dd91a3..7c813d7 100755 --- a/flying_circus_cast.txt +++ b/flying_circus_cast.txt @@ -61,4 +61,4 @@ Ringo Starr, Himself (uncredited) (1 episode, 1972) Fred Tomlinson, Superintendent McGough (uncredited) (1 episode, 1972) David Hamilton, Himself - Thames TV Announcer (uncredited) (1 episode, 1973) Suzy Mandel, German Girl (uncredited) (1 episode, 1974) -Peter Woods, BBC Presenter (uncredited) (1 episode, 1974) \ No newline at end of file +Peter Woods, BBC Presenter (uncredited) (1 episode, 1974) \ No newline at end of file diff --git a/read_string_from_file.py b/read_string_from_file.py index 17a2674..96b7354 100755 --- a/read_string_from_file.py +++ b/read_string_from_file.py @@ -1,3 +1,6 @@ +## Function to read string from a plain text file +## This file refers the text file flying_circus_cast.txt file in the execution + def create_cast_list(filename): cast_list = [] #use with to open the file filename From df85f0b95e00be614f86e1c1c2aa3963411a823b Mon Sep 17 00:00:00 2001 From: zArjun Date: Thu, 9 Mar 2023 12:43:13 +0530 Subject: [PATCH 5/5] Function inside a for loop along with exception handling --- exception_n_function_in_for_loop.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100755 exception_n_function_in_for_loop.py diff --git a/exception_n_function_in_for_loop.py b/exception_n_function_in_for_loop.py new file mode 100755 index 0000000..0ed7ce5 --- /dev/null +++ b/exception_n_function_in_for_loop.py @@ -0,0 +1,21 @@ +def create_groups(items, n): + try: + size = len(items) // n + except ZeroDivisionError as e: + print("WARNING: Please use a nonzero number. Error returned is {}".format(e)) + return [] + else: + groups = [] + for i in range(0, len(items), size): + groups.append(items[i:i + size]) + return groups + finally: + print("{} groups returned.".format(n)) + +print("Creating 6 groups...") +for group in create_groups(range(64), 6): + print(list(group)) + +print("\nCreating 0 groups...") +for group in create_groups(range(32), 0): + print(list(group)) \ No newline at end of file