100 Days of Python: Days 26-30

Day 26

  • Learned about list comprehensions (creating a new list by executing a function on an existing list
    • New_list = [new_item for item in list]
  • Sequences = lists, ranges, strings and tuples (order matters)
  • List comprehension can iterate over these data types in order to generate the new list.
  • Reviewed Conditional List Comprehension
    • New_list = [new_item for item in list if test]
  • Apps Made (all using list comprehensions)
    • Number Squarer – Squares all of the numbers in an existing list with a single line of code.
    • Even Number Identifier – Pulls all even numbers from an existing list using a single line of code.
    • Took two lists of numbers and identified members that exist in both.
    • Optimized code from Day 25 states game.
  • Learned about dictionary comprehensions
    • New_dict = {new_key:new_value for item in list}
    • New_dict = {new_key:new_value for (key, value) in dict.items()}
  • Apps Made (all using dict comprehensions:
    • Split sentences into word lists and created a library that matched words to their letter counts.
    • Converted library of days / temps (Celsius) to a library of days / temps (Fahrenheit)
  • Learned about Pandas iterrows (loops through rows of dataframes instead of columns)
  • Apps Created:
    • NATO Alphabet Translator – Translates user input word into phonetic alphabet.  This is done using just a few lines of pandas and comprehensions. 
Comprehensions make code much more concise and readable.

Day 27

  • Learned about Tkinter
  • Generated first GUI window, added screen titles and labels.
  • Reviewed Advanced Python Arguments
  • Python parameters can use default arguments
    • Def my_function(a=1, b=2, c=3)
  • Some functions have required arguments and will fail if they are not provided.
  • Arguments with defaults will have … in the quick docs
  • Default arguments can be bypassed with user-input arguments.
  • Reviewed use of unlimited arguments
    • Def function(*args):
      • For num in args:
      • *args will be a tuple of arguments
  • Reviewed use of many keyword arguments
    • Def function(**kwargs):
    • **kwargs will be a dictionary of keywords and values as arguments
    • Self.attribute = kwargs[“key”] will yield a key error if the keyword does not exist in the argument.
    • Instead, self.attribute = kwargs.get(“key”) can be used / will return None if the keyword does not exist in the argument.
  • Tkinter was copied from Tk, which uses another syntax.  The functions were all copied to Python using kwargs.
  • Learned about the different GUI widgets available in Tkinter.
  • Learned about layout managers (pack, place, grid)
    • Pack – adds widgets logically from top to bottom with center alignment (by default).  Not good for precise placement.
    • Place – Gives X, Y coordinate placement for widget.  Very difficult to manage.
    • Grid – Splits interface into columns and rows, allows widget to be assigned to specific row/column pair.
  • Apps Created
    • Miles to KM Converter (using tkinter GUI tools)
The appearance of my apps has now reached Windows 95-caliber!

Day 28

  • Began development of Pomodoro (time management) app using tkinter.
  • Used canvas widget for creating app background
  • Used tkinter widgets to complete GUI
  • Learned about “Event Driven GUI” – app is continuously listening for events as time passes.
  • This app design allows multiple loops to coexist for different widgets in tkinter.
  • Created countdown functions and set a trigger on the start button.
  • Defined dynamic typing – a variable’s datatype being determined by the value it is assigned.
  • Experienced pulling in global variables into local namespaces.
  • This was, by far, the most trouble I had with an app to this point.  The behavior of the app, which allows multiple processes to occur simultaneously, was a dramatic change to the more linear / serial projects of the past.
For being a simple tomato that tracks time, the Pomodoro app was not nearly as simple as I expected.

Day 29

  • Reviewed Password Manager as project of the day (tkinter).
  • Created UI from scratch (only the UI was provided).
  • Created a function to save website, username and password to a text file (obviously not secure)
  • Added field validation
  • Added password confirmation
  • Added field clearing and auto-selection of the first field on the form.
  • Imported a function created on day 5 to generate a password.
  • Used list comprehensions and the join function to dramatically reduce the code footprint.
  • Used pyperclip package to copy generated password to clipboard
I won’t be replacing my legitimate password managers any time soon, but this project was a great exercise, anyway.

Day 30

  • Reviewed common Python errors that have been encountered so far.
  • Discussed handling exceptions (writing code to run in the event of a failure)
  • This process includes try, except, else, finally and raise
    • Try – Try to do something that might cause an exception.
    • Except – What to do if an exception does occur (should be limited to a specific error type, not all exceptions)
    • Else – Do this if there were no exceptions
    • Finally – Do this no matter what happens
    • Raise – Do this to raise your own exceptions.  Intentionally crashes the code if the code itself is valid, but there is another factor present that should not allow the rest of the code to execute
  • Completed several errors of using exception handling to improve existing codes (including previous projects.  E.g. added KeyError handling to the NATO alphabet converter to correct users who input non-alphabet characters)
  • Formal introduction to JSON as a method of data management.
  • Interacting with JSON Files
    • Write = json.dump() – used to take Python dictionary and push it into JSON file
    • Read = json.load() – loads data from JSON file as a Python dictionary.
    • Update = json.update() – updates the Python dictionary (that had been opened from JSON).  Subsequent run of json.dump() will push the new data to the JSON file.
  • Apps Created
    • Made improvements on Day 29 password manager
    • Used JSON file as storage mechanism instead of text file.
    • Added exception handling for JSON file not existing.
    • Added search functionality to find existing credentials (with applicable exception handling)
Knowing how to work with JSON data is going to be extremely valuable moving forward.
Tagged in:,

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *