A Hundred Days of Code, Day 019 - Future Exercise Addendum

Ok, think I have the problem of writing code, licked. Will start with one problem from all the Lerner Courses, that I still have to do. And another problem, I ask my friends to give me. This should prove for a promising start. If you, dear reader, have problems in Python that you’d like me, a budding Python programmer to solve, please mail them to me at jason at this domain. It’ll add to my pool of problems and give me plenty to practice. ...

July 27, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 019 - Python Iterators and Generators, Done!

Starting up with the last of the Lerner courses, I got. Iterators and Generators. Hopefully I get done with this and use the same intensity with actually writing code. What shape will that take and how will I write about it? I have no clue For now, notes follow. Notes ** Part 1, Iterators ** If we run the iter function on something and it comes back with an iterator object, then it’s iterable; I can iterate on it. Behind scenes in a for loop, Python does something similar. It asks if the object that has to be looped over is iterable with iter. And then keeps doing next with it to get the next result/item, until it comes across a StopIteration which lets it know, that the values are exhausted and it can stop looping. The for loop is a very thin shell, that asks the appropriate questions of the iterator. But it is always the iterator that determines, what exactly is returned. iterable vs iterator iterable means that it can be put into a “for” loop in Python things like strings, lists, dictionaries, sets, files and objects (that I will soon create) that implement that iterator protocol. it responds positively to the iter function’s question, Are you iterable? It returns an iterator object instead of raising errors (TypeErrors in this case) an iterator is the object returned by the iterable, the thing on which the next function is run. it could be the original object (the iterable) itself. it could also be a seperate object returned by the iterable. when the iterator object is exhausted, it returns StopIteration these are what give us the values. How do I make my objects iterable? By implementing the iterator protocol the object must respond to iter, by returning an iterator object it should respond to the next function with values it should raise a StopIteration error, when it’s done. Best to create a helper class, pass the data to it and return that as the iterator instance. helps with state, since I can then use the same object and iterate over it multiple times independantly. ** Part 2, Generators ** ...

July 27, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 018 - Python Comprehensions, Done!

Continuing my comprehensions journey. Hopefully it makes more sense this time around Notes ** Set Comprehensions ** If I want a set of uniques from a group, I could use a set. Or like Reuven likes to joke, a set is a dictionary with no values; an immoral dictionary 😂 Create it with a somesetcomp = set('somecondition' for a something in somethingbig) or in a simpler manner, somesetcomp = {'somecondition' for something in somethingbig} Protip, better to start with a list comprehension, be done with figuring out stuff and then just switch the brackets at the end. Fewer issues that way. ** Dictionary Comprehensions ** ...

July 26, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 017 - Python, Advanced Objects, Done!

And we are done with objects! This course finally gave me what I was looking for all these months. The ability to think of and reason about Python, so that I can then think of and reason about, how to build my own programs. Notes Abstract base classes Sounds all high and mighty, but does something pretty logical and simple Helps me test if my object is of a generic (of sorts) type. I don’t want to test if 5 is an integer or a float or any of the other various county types in Python. I just want to see if it’s a number, of some sort. The abstract base class numbers helps me do that. There are such classes for strings and files, I hear. Will go into learning more about them in detail later, as I actually write more Python. But at least I know what they are now. ** Part 3, Context Managers** ...

July 25, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 016 - Python, Advanced Objects.

Started up with Python Objects today. This seems like a long, hard one. Here’s hoping I learn lots. Knowing Reuven, I know I will. Notes Part 1, Advanced Methods. This section focuses on the dunder methods in Python. By default objects are not equal to other objects even they they are from the same class, with the same attributes and methods. We could implement our own equals method though. The point being, we need to be intentional and methodical, when we design and implement our classses Static methods are plain old functions that I write in my class for use by both the class and the instance. Could be used to clean up input or similar such utility functions. I create them by putting a @staticmethod decorator atop my method. ...

July 24, 2020 · Mario Jason Braganza

A Hundred Days of Code, Day 015 - Python, Advanced Functions, Done!

Delving deeper into Python functions and learning more about them, using Reuven Lerner’s Advanced Python Functions Course Notes The co_* series of attributes inside the __code__ attribute inside most functions gives a wealth of information about a function. For example, looking up the argcount attribute for some function hello, with hello.__code__.co_argcount will give us the number of arguments hello takes. And doing a hello.__code__.co_varnames will give us a tuple of all the local variables in a function hello.__code__.co_code contains the compiled bytecode for the function. Importing the disassembler import dis and running it on the bytecode, dis.dis(hello) will show us what’s in there. hello.__code__.co_flags keeps a track of whether the function has * arguments or not via a binary switch. (It holds many parameters, one of which is the on/off switch for *args) if said switch is set, the function checks for such arguments at runtime and turns them into a tuple to process. they are set for *args via the hello.__code__co_argcount switch (0/1) and for keyword arguments with the hello.__code__.co_kwonlyargcount switch. the __defaults__ attribute keeps all the functions default variable values. If I don’t supply a value to some parameter, the function will look in here, to see if a default value exists. hello.__defaults__ could hold a last name default, for example, if I want my hello function to work even if no last name was supplied, and it actually expected a full name. do not use mutable defaults. an empty list or dictionary for example. set the default to None first in such case and then create the list when the function runs at runtime. The last thing I learnt about were nested functions. I can nest them two/three/as many levels deep as I wish. Practically, most folks do two or three. When an inner function takes the variables of its enclosure and does something with it and returns stuff…? those kinds of functions are called closures. Learning / Feedback/ Experiences from doing exercises The course so far is Reuven making simple functions and then breaking them down and explaining functionality in painstaking detail. Reuven is an awesome teacher! I would gush about him every post, but then it would get too much 😂. So once per course is good I kept looking at the disassembled code and wondering, what language it was. I still don’t know. My ‘educated’ guess is that it is a level of standard pseudo-assembly code for some phantom standardised processor. Once I had that mental model in mind though, all of it started making a lot more sense. This brought memories of Z80 assembly, I learnt in the mid 90s This was fun to do! Onwards to Advanced Objects! Read all about my Python Functions journey here ...

July 24, 2020 · Mario Jason Braganza