A Hundred Days of Code, Day 021 - Swing and a miss
Only did about an hour of distracted work and exercises today. I’ll still count it though. Tomorrow is another day :)
Only did about an hour of distracted work and exercises today. I’ll still count it though. Tomorrow is another day :)
Had given myself a day, to see if I could get a good Python development environment using Elpy and Emacs. It does work. Just not well enough for me. At the end of the day today, I was happy I learnt so much about Emacs. But that is not my focus right now. Python is. Emacs knowledge can come slowly and organically. So I have kept Emacs as my regular editor for nearly everything text. And switched to the community edition of Pycharm for all my Python projects. ...
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. ...
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 ** ...
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 ** ...
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** ...