Posts

Showing posts from November, 2021

New Uses for Matplotlib

Image
Today I discovered the matplotlib_venn module. It allows you to produce Venn diagrams. I coded up a program to simulate a questionnaire about different sports that students play. Of course one of the sports questions was about football. But another question asked if any other sports was played. Wanted to show both of these questions, plus the overlap. It would have been nice to label the circles "Football" and "Other". However I found that while I could add those labels, the original A and B labels remained. The result was labels written over top of each other. I could not easily find out how to replace those original labels. Might need to dig deeper into this new module.

From Zero to Hero

Image
I was stuck reading this book Python Playground. But I was not learning anything. The projects did not allow me to try to implement them myself. The description of the projects were interlaced with the solutions. After getting lost in a chapter about OpenGL, I decided to punt. Surprised I made it through almost 10 chapters of this book. Instead I moved on to a book called Math Adventures with Python. I am a bit apprehensive. One reviewer claimed that this book actually does not use Python. Instead it uses a program called Processing. For now, the beginning of the book indeed uses Python. If it skips to Processing, I will bounce quickly to another book. The first chapter has me drawing cool stuff with the turtle module. I produced a circle using only squares. The pattern is neat, if asymmetrical. I hope there is a lot of Python and minimal Processing involved in future chapters.

Making a Mosaic

Image
I made a cool program today. Not sure if it works exactly how it should. Still cool though. I started with some image I got from the Internet. It was some art work. My goal was to slice up the image into a grid. Then I would swap out each tile from that grid with another image that was closest in color to the original grid image. The goal was that the resulting image would be made of a whole lot of smaller images. And at a distance, you could see the shape and outlines of the original images if you looked at the new mosaic of small images. Well my final resulting image does not look a lot like my original image at any distance. It was still a cool experiment. My source of mini images to swap in was a directory full of around 50 celebrity images. Maybe if I had a lot more images, and they were not just pictures of celebrities, then my resulting mosaic could look a bit more like the original image. The funny part of this exercise was when there were bugs with my logic. Then my final imag...

Game of Life

Image
I am working through projects in a new book. This chapter has us implementing the Game of Life. The book solution has you employing the numpy and matplotlib modules. I chose pyGame instead. I wanted to make my job easier. So I just forced the border to be all black (off) cells. And I never update them. That way I can check all the neighbors around the cells I do update, and don't have to worry about going off the grid. It turns out Python is just too fast computing the new state of all the cells. Left alone, it proceeds so fast, I cannot experience the life cycle. So I just end up putting myself to sleep for a half second every iteration. That does the trick. When I let the simulation run, just as in the real world, everything dies out. Then I end up with a black screen. I had thought there might be some scenario where life repeats in a never ending pattern.

Book is no Playground

Image
I started reading the book Python Playground today. Went through the first 2 chapters. With a subtitle of "Geeky Projects for the Curious Programmer", I thought I had a hit on hand. Well it turns out this might have some projects. But the book just gives you the code for the projects. I don't have a chance to implement the projects myself. That is just no fun. Might have to just toss this book aside. There is one small hope. In the next chapter, I read enough of a description before looking at the code. And I think I just may be able to try to implement the program myself. However if the rest of the book is anything like the first 2 chapters, this thing is definitely going to get tossed. The real crime is that I purchased a new print copy of this book this year. It feels like that was a royal waste of money. The lesson learned is that I need to be able to predict whether a book will give me the opportunity to write code for the projects presented. Not sure how I will acco...

Mission is a Failure

Image
I am 10 chapters into a book on how to code a space adventure game in Python. Normally I like to roll my own code to implement ideas from books. This started out okay. However the infrastructure for this game is very hard to work with. For example, there is an array which represents a grid of rooms. But this is only a single dimensional array. Plus there is a secret item at location 0 that is not part of the grid. It is a pain to easily, say, do something to the rooms on the right side of the grid. There are also a lot of other weird things going on in the sample code. Lot of magic numbers that correspond to either room numbers or codes for certain types of tiles to display. And there are lists of tuples where you need to know what field is in each position of the tuple. I have just about given up trying to follow the code that is presented. I also thought I would be learning some Pygame Zero modules details. Nope. Not yet at least. I am trying to gain some wisdom on the algorithms and...

Mission Accepted

Image
I started a new programming book today called Mission Python. The whole book is teaching you how to create a graphic adventure game. I already downloaded the finished product. While I was unimpressed by the game since I kept getting killed, I think this was because I did not understand the controls well enough. Decided to give this book a go. I was pleased that by the end of chapter 1, we have draw the planet Mars, a spaceship, and an astronaut. The astronaut can move and is controlled by the arrow keys. And when the astronaut move past the spaceship, it "goes behind" the spaceship. Pretty good for chapter 1. The only drawback is that this game requires Pygame Zero. I wish we could have done the whole thing with only Pygame. But maybe the extra module makes things a bit easier.

Tkinter Annoyances

Image
I am on the last chapter in this kids book Creative Coding in Python. It has me creating a game akin to Kaboom for the Atari 2600. I even grabbed an image for the bad guy in that game for my own game. Balloons (or candies) come down the screen. You have to catch them. But avoid red candies! One final tweak I did was to make that candies speed up every third one you catch. That makes for a quick game. I did have some problems display the user graphic initially. The problems came back to haunt me during collision detection. The graphic just would not display where I wanted it. Eventually I figured out that when I specify an x and y coordinate to the create_image() function of the canvas, those coordinates are not treated as the upper left hand corner of the image. What a strange default behavior. I had to specify an anchor value of "NW" to make that happen. I also had some problems getting the key press detection working. Luckily I ran into this problem before. I had to call th...

Getting Scapy with it

Image
Every so often, I check Indeed for job postings. Normally I look for companies that want Python experience. One posting said they wanted you to know Scapy. I was not familiar with it. Figured it was some interesting Python module. I decided to look for more info on it. I like reading books to gather my info. However the only book I could find was Python Scapy Dot 11 . And it costs $30. I was not that interested in this module. There was one book I found that had a chapter on Scapy. This was Hands-On Enterprise Automation with Python . The Scapy chapter was the second to last one of the book. This book had 400 pages. It talked about a lot of things. I found out Scapy's specialty is generating network packets. It can also capture packets from the network. Scapy has a Domain Specific Language to specify the packets. It makes it easy to generate network traffic, as it fills in defaults for most fields. You can do all kinds of things if you have full control over the network traffic. Fo...