Friday, December 16, 2011

the point of Perl

I should mention - the reason I'm trying to familiarize myself with Perl is because I'm hoping to get a toy LAMPP server going to run some experiments.

In the early 2000's I had an XAMPP install and learned a lot about Apache, PHP, SQL, and all that other good stuff. It was super useful and that knowledge allowed me to run a server in the lab I used to work in for data storage and general web stuff (wiki, image hosting, light PHP scripts for various things).

Re-familiarizing myself with that stuff wouldn't hurt, and it might come in handy in the near future.

I don't have a PC anymore to run it. Hopefully I can get away with doing it on my netbook. The netbook is on paper twice as fast as my old web server.

Not the camel

I keep forgetting it's NOT the "camel" Perl book I have. It's the llama.

The Camel book is a thousand pages long - the Llama book covers syntax and control structures.

I'll pick up the Camel book after the holidays (hopefully I'll have some Amazon gift cards or something).

Tuesday, December 13, 2011

Looking back at the security camera program

The Python+OpenCV program I wrote last summer is pretty handy.

http://chrislearnsc.blogspot.com/2011/03/added-timing.html

I use a modified version of that to take a picture every time I boot my laptop.

If I can find a way to get webcam images without using OpenCV I'd like to flesh the program out a bit and maybe add more options (command line or GUI with PyGTK). It's necessary to get away from OpenCV because it's serious overkill for just grabbing webcam images. Also, it's horrifying to install - it took me weeks to get it to work on my laptop and if it ever broke I wouldn't know where to start. I think that there are handy ways of grabbing webcam frames in Linux, but I don't know about OS X or Windows.

Monday, December 12, 2011

Perl

I stumbled across a Perl book I have sitting around (the camel book).

So far I haven't run into a problem that readily called for Perl, and honestly I wonder what kind of people use it on a day-to-day basis. I did a search for reasons to learn Perl, but most reasons have to do with parsing large text files.

I don't have any large text files.

Wednesday, December 7, 2011

Don't work harder, work smarter

10 minutes of code saved me an hour of copying and pasting repetitive lines in a script.


EDIT: Oof, bad newbie - no treat. Forgot to close the file. Leaving it up as a shameful reminder.

Monday, December 5, 2011

Project Euler 13 WIP

I meant to have this finished by now but I got distracted. I already know how to finish it, but I don't yet know when I will be able to. Here's what I have so far.


Here's data.h:



What's left to do is pad the storage array so that the remainders can add up and I'll be able to reconstruct the sum in this artificial manor. The printf() where it dumps everything out was my sanity check before I went any further.

Wednesday, November 30, 2011

Updates

I went through and tagged most of my Project Euler related posts. The final code, code that was related (like preliminary prime finder code), any rigorous pre-code discussion was tagged. I didn't tag a lot of the work-in-progress code since it's just unfinished final code that isn't all that useful to review.

The wife wanted me scarce while she studied so I went up to the office and knocked out a lot of Project Euler 13. I should have final code this week. It's a really interesting problem because it deals with adding numbers that can't be stored in even a 128-bit integer, and as a result it's forced me to really hash out what it means to add large numbers in a methodical fashion, which I worked out on paper first.

While going through and tagging my posts I realized that I only started the PE problems in February of this year. I'll probably get through a few more so that would make around 14 problems done this year. That doesn't seem like a lot but it's 14 more than I could have done when I first started learning C so I'll take it as a win.

Tuesday, November 29, 2011

Note to self on overriding dealloc in Obj-C

I went through the chapter in Obj-C:TBNRG about ARC and retain cycles and got to the end with my head spinning. I understand what's going on but following which instances of what class own who is dizzying. The Objective-C style of having two files per class (one header file one implementation file) makes it hard to follow since you can't see everything at once. Well... maybe having everything in one big file isn't the way to go either.

Anyways, I intend on going through it again for good measure.

Something that had been bothering me is that in the classes you make for the example you override the dealloc method so that you can toss a print statement in to say when it's being deallocated (in an effort to show what actions on the data cause it to deallocate automatically). I realized that the implication would be that the data isn't actually getting deallocated! So, to the internets I go and I find that The Big Nerd Ranch has a forum for its books, wherein I find this:

http://forums.bignerdranch.com/viewtopic.php?f=151&t=3223

Basically: the magic of ARC saves you by somehow knowing to call the dealloc of the parent class (or the parent's parent class which inherits NSObject I think in the example). I don't like magic, but I'll roll with it for now.

Project Euler 12 Solution

Some quick lunchtime coding:
http://projecteuler.net/problem=12


It took about 18 minutes to run. The biggest speed fix I could make is to change the divisor counter to only check up to the square root of the number entered. Another one is to not start from scratch every time I want to make a new triangle number. I should store the last result and then add the next number.

I read somewhere to optimize only after things are working since optimizing on the fly can be tricky. Also, I didn't want to spend too long on this since I had a sandwich to focus on, too.

Sunday, November 27, 2011

A pointer blunder I made today

Every time I think I'm getting half-decent at recreational-level C programming I do something dumb that knocks me down a few pegs. In my last post I mentioned using malloc() and free() to run some very simple tests. The tests turned out fine but I ran into an error that took me a few minutes to resolve.

This illustrates what I tried to do, and what I actually meant to do:



Basically I tried to alter where ptr was pointing, instead of filling in the reserved space ptr pointed to. The error happened when I tried to free the location of x instead of my malloc() allocated location.

I consider myself pointer-competent but I still make really dumb errors. The difference between now and a year ago is that I'd have had to write a post about how I was stuck on an error I couldn't figure out!

The trouble for newbs with Objective-C, Cocoa, and XCode

I've been working much more with Objective-C: The Big Nerd Ranch Guide than I have been with Programming in Objective-C (3rd edition) lately. I decided to pop back over to the PiO-C book to see how much the other book had already covered (and see if it didn't cover anything since PiO-C is supposed to be the more in-depth book) and I discovered something I should have noticed before.

At some point XCode started using either a different compiler (Clang/LLVM instead of GCC) or a different way of handling memory allocation called Automatic Reference Counting (ARC). A pretty big change happened in the default way new projects are created that really makes it tough to learn from older books (if August 2011 is considered old!).

What it comes down to for the newbie is basically this:

Old way new projects were generated:



New way new projects are generated:



This wouldn't be a super big deal except that the old way where you explicitly handle the NSAutoreleasePool isn't legit in newly generated projects. Going back and forth requires really mucking with the project settings to get it to not use ARC. If I was brand new at learning programming and decided to use Objective-C with XCode as a first language/environment it wouldn't take long to get exasperated. I'm already pretty irked that a new book I bought is obsolete!

I need to track down a step-by-step guide to editing the project settings to go back and forth from the old way and the new way in case I want to use any examples in the old book to learn something. If there was a way to tell it which to use at the start of the new project then that would be great.

I understand the benefit of the new way on a superficial level (just as superficial as I understood what the old way was doing). Coming from C I'm not afraid of doing my own memory management, but I get that it's something you don't want to mess with once you start working on larger frameworks. Even GDK/GTK did a lot of stuff for you (similar to "new" in C++).

For kicks I threw in a few malloc() and free() assignments inside and outside the @autoreleasepool block and I didn't get any complaints. That's a relief - I take very seriously the claim that you can always revert back to straight C and Obj-C is just a strict superset of features that doesn't change any of the known rules.

I just found a placeholder for the 4th edition (releases in a month or so) of PiO-C on Amazon that specifically mentions in the description coverage of XCode 4.2, ARC, and iOS 5. If TBNRG doesn't cover everything then I'll have to drop $30 on the latest edition of a book I already own. Well... that's the game isn't it?

Maybe in a few weeks I'll follow up this post with instructions on reverting to pre-ARC compiler options, but for now I'll stick with TBNRG.

One big disappointment is that although TBNRG or PiO-C goes into some depth about what NSAutoreleasePool or @autoreleasepool{} are really doing, I don't think it discusses much of when to use it. I've seen snippets of code that have multiple groups of @autoreleasepool{} blocks (and in the older book multiple NSAutoreleasePool allocations) but it's not super obvious when to use them. In comparison malloc() and free() look downright friendly.

It's horribly ironic. I started this journey as an almost spiteful motion to clear up my ignorance of pointers and memory. I got to a certain competence in C and now I'm hitting a roadblock with an entirely new flavor of memory management.

Maybe I should ditch XCode and do some AVR/PIC stuff.


Friday, November 25, 2011

GitHub for Mac test

I struggled with geting Git and GitHub to be useful on my Windows and Linux machines, but there's a super easy GitHub app for Mac (and XCode uses Git as a source control option).

https://github.com/cheydrick/Project-Euper-9

Unfortunately as a test I pick the PE problem where I misspelled "Euler".

Pastebin test

Here's a test to see if I can jump to the "HTML" view to do Pastebin stuff:


 

Yea, I think that works better.

Back on the horse

I'm getting through some more of the Big Nerd Ranch Objective-C book today. It took the free time of the Thanksgiving holiday and some gentle prodding from my wife to do it (she preferes that I indulge my more productive hobbies rather than play Skyrim all day).

It's been a while so I'm having to play catch-up. A quick note to self about Objective-C class organization:

Class header file:




Class implementation (.m) file:




main.m:




The new Blogger interface doesn't take well to the Pastebin inserts. I'm working on it.


Thursday, November 17, 2011

Society for Neuroscience 2011 done

I always get back from SfN with sore feet and wounded pride. It's humbling talking to people that do for a living what I aspire to as a hobby (it's not just neuroscientists that go to these things - lots of engineers and programmers and math whizzes and such).

Instead of being downtrodden I've allowed the bruised ego to fuel my resolve to get back on the programming horse. I have several new project ideas. One of my past larger projects involved reading in data from a known file format. The next logical step is to do something with that data. I need to sit down and think it through once the post-show rush is over.

Monday, October 31, 2011

buried

The big conference I go to for work every year is coming up. It's not necessarily taking up all my spare time but the prep is wearing me out enough to keep me from programming.

I spent some time with K&R this week. My hope is that I'll have a good "getting to know getchar()" post soon. It's important to know for the middle sections of K&R since it's how you get the data you apply the advanced data structures to. Anyways it's complicated and I want to write it all out so that I never have to think about it again.

EDIT: Oh yea and Battlefield 3 has not been  letting me do other stuff that isn't Battlefield 3.

Saturday, October 22, 2011

pre progress report and more of the gap

I'm having a good time learning Objective-C so far. I had thought that it was some really crazy new way of thinking about classes and program organization and it would be hard to learn. It actually is a crazy new way of thinking about classes (in implementation, not general idea of OOP) and it's a stickler for organization but doesn't rigidly enforce it. Even a smallish Obj-C program can be a half-dozen files since every class needs a .h and .m file (not NEEDS, just prefers).

Over the past year I've read the occasional tutorial or sample code and left with my head swimming. The really goofy stuff I saw makes sense now, and the goofy stuff is actually pretty neat and makes for faster implementation.

The future post about my progress will go over what I've learned so far and my thoughts on the language in general.

There are lots of C related stuff I'm kicking myself over not getting around to doing, like the more advanced data structures. Cocoa/Foundation has its own easy to use versions of linked lists and dynamic arrays and whatnot (I'm sure C++ has this in the STL somewheres) but I'll feel bad about using them before I implement it myself at least once so that I really know what it's all about. Hashing, too. I want to learn about it since it comes up from time to time.

My mind has been on what I call "the gap" again. The first gap every new programmer crosses is the gap between variables and getting those variables presented to the user (typically via printf or equivalent). The second gap (for me) was going from console-land to a modern style GUI. It took me until I was 28 to get to that point! My third gap (perhaps third and fourth) are 2d and 3d graphics. I was reading something about Perlin noise and the code for generating that noise was written out and the author had some nice pretty pictures and I realized something - the code he had in his article was just the math to generate noise. There were no bits of code for turning those functions into images. How did he get from one to the other? So that's been on my mind and I'm worried there is some big obvious thing I'm missing or that I'm making it out to be more complicated than it really is. From what I could tell the functions for generating the noise accepted an x and y position and spit out a value between -1 and 1. Scanning through all the x and y positions in the image, getting back this number, and shading/coloring it appropriately is probably all that was going on.

Therefore I promise this: when I figure out how to do anything important I will always in full present the code used to generate the end result. The entire reason this journal exists is to present what I'm learning in a clear enough way such that the 15 year old me would have a much easier time learning about those concepts. I did a much better job of this when I was learning the basics of C (especially pointers) but I've slacked a bit.

Friday, October 21, 2011

so far so good



This is part of an "apply what you know" part of the Objective-C Programming:TBNRG book. It's supposed to help solidify using object methods (and reading the Apple documentation on string methods I guess).

Thursday, October 20, 2011

Come and Git it.

I started reading the first few chapters of Advanced Mac OS X Programming: The Big Nerd Ranch Guide and so far it's been super enlightening. There are a lot of things I don't understand about C that aren't really about C at all and are compiler or platform specific, like what's available to you in the pre-processor. The book has a great super clear explanation of how to use all those fancy #define and #ifdef bits work. That's the kind of stuff that I'm not abundantly familiar with that's preventing me from moving on beyond being a beginner C programmer to an intermediate C programmer. I had no idea you could set #define information in the GCC command line, either. Where do people learn this stuff? I feel super fortunate that I came across this book because I'd still be in the dark otherwise.

I thumbed through Programming in Objective-C and realized I can probably skip the first nine or so chapters since it covers basic C stuff like data types and control structures. The juicy bits are how classes are implemented.

Someone on Reddit posted a link to a site that has the full text (.pdf) of Michael Abrash's Graphics Programming Black Book. I didn't realize that it was legit released for free online. I just picked a chapter and page at random and had my mind blown. He's a really good writer.

This morning I made a GitHub account and did the quick tutorial on making a repository and pushing a file up. At this point I'm just following directions because I have no idea what any of the stuff I'm typing in does. I'm kind of irked because it has a great walk-through on creating a repository, making a new file, and commiting it, but it doesn't say what to do after you edit the file. I can't tell if the commands in the tutorial were only for new files or for any file. I don't plan on using Git or any source control for every quick program I write (unless I feel like I need the practice) but it will help make the case for trying to work on more long term projects.

EDIT: Also a good (free) read: C Elements of Style


Sunday, October 16, 2011

Instance methods and class methods

I have a somewhat decent grasp of common OOP terms like class, method, inheritance, polymorphism, etc. Something that I keep seeing in my casual reading about Objective-C is that some methods in class definitions have + preceding them, and others -.

The + means the method is a class method, and the - means it's an instance method. An example in the book I'm reading today (Objective-C Programming: The Big Nerd Ranch Guide) illustrated one difference between the two.

The NSDate class has a method called date which returns a pointer to an instance of the NSDate class.

NSDate *sometime = [NSDate date];

date is a class method.

There is another method called timeIntervalSince1970 which returns a double (seconds since 1970). This is an instance variable, so you call it on the instance.

double timepassed = [sometime timeIntervalSince1970];

It's not legit to say:

double timepassed = [NSDate timeIntervalSince1970];

and you'll get warned that no such class method exists.

So there's OOP with Objective-C lesson one. There are class methods and instance methods. I tried doing a few different things, like:

NSDate *now = [NSDate];

but I got an error "Expected identifier". Is an identifier a class method that has to be run? Is this the concept as a constructor in C++? I peeked into NSDate.h and found a few other class methods alongside date. I found an instance method called init and thought I should try it too

NSDate *now = [NSDate init];

and this didn't give me an error. So that's confusing - both init and timeIntervalSince1970 are instance methods but the former works and the latter fails. Going back to NSdate.h I found that timeIntervalSince1970 isn't quite in the same place as init is, so maybe it has different rules. Further confusing the matter is that init is of type id which is a generic pointer to an Obj-C object, and timeIntervalSince1970 is a double.

Well shoot. I mean this post to be about how I learned something and now I'm just more confused.

Friday, October 14, 2011

Compiling GTK 3.0 programs in CodeBlocks

I spent some free time installing Ubuntu 11.10 in a VirtualBox session hosted by OS X. The trials involved with getting that to work may be the subject of another post. The punchline? VirtualBox and Unity 3d don't get along. Install the host drivers, update, then log in with Unity 2d selected. Doing those first two steps is tricky when the desktop windows don't redraw unless you go to the desktop switcher. Wowie. This was true in 11.04 and 11.10 beta 2.

My motivation for moving to 11.10 from 10.10 was so that I could get a handle on GTK 3.0. A few weekends ago I really beat myself into a pulp trying to make a basic "hello world" sort of program using Cairo in GTK and failed. A lot of the issues I had were with the basic initialization and packing a Cairo window into a GTK container. I also couldn't hash out the relationship between Cairo and GTK pixbuffs. That functionality seems to have changed a lot in GTK 3.0 and I want to try again.

I use CodeBlocks out of habit. Eclipse has a weird project organization that rubbed me the wrong way, and Anjuta was just plain hard to use. I don't know of any other popular IDEs in Linux (emacs and vim worry me).

Installing CodeBlocks and Synaptic Package Manager (to get the GTK 3.0 development files) was easy enough through the Ubuntu Software Center. No problems there, although I was very surprised that Synaptic wasn't pre-installed like in 10.10.

Expecting the default GTK program (the code that is generated when you start a new GTK project) to compile the first time was naive of me (well... it worked for GTK 2.0). The errors were about not being able to find the header files. I went error to error and added the paths to the headers in the compiler search options (Project->Build Options->Search directories->Compiler tab). Once all the missing files errors went away I got a bunch of new errors about how all the functions weren't defined. This basically means the libraries it needed to link against couldn't be found. Hooray.

I sat and thought about it and realized that if I were to compile this in the terminal I'd use pkg-config. pkg-config is a tool that looks at some different file (.pc files?) for all the compiler flags you'd need to use a certain package, like GTK.

If I type into the terminal: pkg-config --cflags gtk+-3.0

I get this out:

-pthread -DGSEAL_ENABLE -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/gtk-3.0

Similarly, if I type in: pkg-config --cflags gtk+-3.0

I get this:

-pthread -lgtk-3 -lgdk-3 -latk-1.0 -lcairo-gobject -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lrt -lglib-2.0 

GCC has this neat feature (or maybe it's a Linux thing?) where you can embed commands with text output into the GCC options when you run it if you surround the command with these: ''

So the compile command would look like this:

gcc program.c -o program`pkg-config --cflags --libs gtk+-2.0`

And you'd get all of those extra instructions into the gcc command. Pretty neat, right?

Ok, so I got real excited and found precedent for using pkg-config in CodeBlocks via StackOverflow in a question very similar to my own:

http://stackoverflow.com/questions/5921460/how-to-setup-gtk-to-develop-with-codeblocks-on-ubuntu-linux

However, when I did this (add the pkg-config lines to the compiler/linker "Other options") the error message I got said there was no input file. It's the same error you get if you just run gcc in the terminal.

As a last resort I simply copied the output of "pkg-config --cflags gtk+-2.0" into the compiler "Other options" and the output of "pkg-config --libs gtk+-2.0" into the linker "Other options" and now everything works just fine. This seems like a bit of a hack but I can't argue with the results.

I'm glad I finally have a functional development environment, but this raises a lot of questions. Why did it automagically work with GTK 2.0 in Ubuntu 10.10? Why did implementing the solution in the StackOverflow thread not work? Is this how everyone else has to do it?

This is a pretty strong argument in favor of avoiding IDEs for everything but editing. I should either learn Make or create a shell script that runs gcc with the pkg-config lines that takes in the .c file as an argument.

New stuff

I've been using Blogger since 2003 (prior to the Google purchase of the service). It's about time I start using some of the more advanced features and widgets and stuff. I've added an incomplete "about" page, a label list, and a Twitter account.

I'm going to try and keep the posts here to important things (excluding this post of course) that are worth the time it takes to write out. The purpose of this journal is to write out things I've learned in such a way that a teenage version of myself (the one that tried and failed at learning C the first time) could understand it.

I debated whether or not to end this journal and start a new one since I've pretty much stopped only focusing on C and have moved on to new languages like Python and soon Objective-C, as well as new frameworks like GTK and soon Cocoa. If I were to move to a new one I'd probably try to get my own domain name and a more complex journaling software like Wordpress. Maybe someday, but this serves my needs for now.

Wednesday, October 12, 2011

Throwing books at the problem

I ordered a few books on Objective-C and Cocoa programming. My reasons are as follows.

1) One of my motivations for learning to program was to try and eventually build some good mobile application ideas I had. I own an iPhone and bought a Macbook to learn C/Python/etc on so I might as well go whole hog.

2) OSX uses GCC and shares a great deal of common UNIX programming paradigms. One of the books I picked up has a good section on GCC and GDB which will carry over into Linux.

3) I like working in Linux and using GTK, but it's getting cumbersome to always do it in a virtual environment. I've also gotten far enough along in GTK to realize that I could easily spend the rest of my hobby time for years learning the deeper bits and I'd rather spend that time learning something that will help point number 1.

4) I already have a few C++ books collecting dust on my shelf, and honestly what I've seen of C++ programming frightens me. I don't want to learn Java. I feel like I could quickly get competent enough at both in a short amount of time if I really had to.

This are the most important points:

5) My life has always been about the niche skills or uncommon elements. I play drums, I have a neuroscience degree, I have pet rats, I do tech support for a niche product, and so on. Objective-C is an uncommon (relative to C++/Java) language typically learned for a specific niche market. Why fight it?

6) Objective-C is a strict super-set of C. C++ claims to be sometimes, but there are so many exceptions. Nothing I learned will have been lost or need to be checked against special rules.

7) I started out trying to learn C, but that goal has evolved to simply learning to program.

8) I'm not in this to make a career move. That would be an unreasonable expectation. If I meant to switch careers I'd double down on Python and Java since those are more marketable skills. That being said, a little Python knowledge has gone a long way at the office.

I'm giving myself the winter to explore this. From now until the end of February. I'd like to be at least as competent at Cocoa (native OS X framework) as I was at GDK/GTK, and also have a solid handle on Objective-C syntax common object oriented patterns (hopefully my work in Python will have helped).

Monday, October 10, 2011

deep

http://www.slideshare.net/olvemaudal/deep-c

Long, but pretty sweet dialog on what a deeper meaning of c/c++ means.

Sunday, October 9, 2011

the week off

I took the week off from coding, but not from planning the next stage. Since GTK3 seems to be the way to go I spent time installing Ubuntu 11.04 and 11.10 beta2 in a virtualbox instance. The new "unity" interface (I think it's called) doesn't play nice but I'm working on it.

The whole week wasn't code-free. Out of necessity I did some Python stuff. My inability to parse Python's documentation is holding me back.

Monday, October 3, 2011

no go

I burned a lot of time over the weekend on the Cairo/GTK problem.

My assumption was that putting a Cairo-drawable window in a GTK window would be like initializing and attaching any other widget (like a button, form, text box, etc). The examples and tutorials I could find only did this very simply - it initialized a main GTK window and then put the Cairo window in that. What I wanted to do was not much more complex - I wanted a button in addition to the Cairo window. What I figured would happen is I could have a main Window, embed a VBox which is a container that holds other widgets in a vertical arrangement, and then embed the button and Cairo window into that. What emerged however is that there is no Cairo window. You have to have a different container (unknown) and then somehow set it as drawable (unknown) and hook a callback for doing the drawing with the "expose-event" (why?). Every example had all the drawing functions in the "expose-event" callback - what if I wanted to alter the drawing when I press a button? Do I have to artificially kick that event from the point of view of the drawable window?

I came to the conclusion late Sunday that this was too much work, not well documented, and nobody online has written up a detailed next-step tutorial for practical applications of Cairo in a GTK program.

But THEN I remembered something - I'm using GTK2 because that's what Ubuntu 10.10 supports out of the box. GTK3 changed up how it interacts with Cairo. My brief investigation revealed that the way GTK2 initialized and updated Cairo-based windows completely changed in GTK3. Not only is everything I painfully learned all wrong now, it also doesn't seem to be as painful to do.

So that's how I wasted a weekend where I had more free time than usual on a wild obsolete goose chase. I don't know if trying again next weekend is worth it - there are no tutorials that I can find online for GTK3 and Cairo, and too much of the official documentation assumes that you knew how the old system works. Lots of "instead of doing it like this, do this", but not knowing how the first way of doing it worked is crippling.

I also discovered that the relationship with GTK and GDK is not what I thought it was. I had assumed GTK was the GUI aspect and GDK was everything else (data types, etc), but I found that GDK is a whole layer of drawing libraries between GTK and X. GLib is the "everything else".

I genuinely took away nothing meaningful from the weekend. It might as well have never happened.

EDIT: I wish I had seen this yesterday: http://zetcode.com/tutorials/gtktutorial/gtkevents/

The "timer" example has exactly what I needed to know. I think the missing info is that there are GDK (not GTK) specific functions for interfacing Cairo drawing functionality with GTK widgets. In the example a GTK drawing area (which I'm sure doesn't exist in GTK3) is embedded in a window - this is in contrast to drawing directly on the window which I didn't want to do.

Friday, September 30, 2011

Leaving some notes for myself here

I'd like to get this code to compile:

http://zetcode.com/tutorials/cairographicstutorial/basicdrawing/

I have a fun idea for a program but I can't do it unless I get a handle on embedding a cairo-based drawing in a GTK window.

http://developer.gnome.org/gdk/stable/gdk-Cairo-Interaction.html

Thursday, September 29, 2011

quick detour

I took a brief lull in activity at work to knock out another important piece of code in my case analysis program (not so much analysis right now - mostly just parsing).

Python lacks a #define statement so I can't alias words to numbers. I had really hoped to do this to pair column names (human readable) to numbers (parser readable).

Instead of:

#define OPENDATE 0
#define CLOSEDATE 1
#define NOTES 2

I am faced with just making them proper variables:

OPENDATE = 0
CLOSEDATE = 1
NOTES = 2

and so on.

I stumbled across a different solution I like in example 3.20: http://diveintopython.org/native_data_types/declaring_variables.html#odbchelper.multiassign

I'll flip a coin to decide which is more readable and then put it in. The code for returning a specific column is already in place. Took me longer than it should have - I've forgotten too much about Python in the past few months.

More divisor function thoughts

I had some time to sit down with a pen and paper to figure out a better way of getting all the divisors out of a number.

I don't think there are any clever tricks using recursion I can use. I went down that path because it worked for something else but I don't think it will work here.

This makes sense in hindsight (and it's a testament to how poor at basic arithmetic I am) that I only need to test to half the number.

For example, the number 90 has 10 divisors: 1, 2, 3, 5, 9, 10, 18, 30, 45, 90. Obviously I don't need to test beyond half the number (which holds true for odd numbers, too).

I might have even stumbled upon a way of detecting square numbers.

64's divisors are 1, 2, 8, 16, 32, 64. If you see the symmetry in both 90 and 64's divisors you can see a pattern.

90: 1,2,3,5,9,10,18,30,45,90
Taking the outer numbers and working out way pairwise inward we have:
(1,90)(2,45)(3,30)(5,18)(9,10)
These are factors of 90.

64:1,2,4,8,16,32,64
Doing the same thing we find a slight difference - there are an odd number of divisors.
(1,64)(2,32)(4,16)(8,?)
We of course know that 8*8 is 64. This makes me wonder if all squares have an odd number of divisors, or if there are number with an odd number of divisors that
aren't squares.

25 is a square and has an odd number: 1,5,25

So, just some thoughts. I could quickly spin off my program to test for stuff like that.

Anyways I can quickly change my program to only loop n mod x and test for 0 such that x only goes to half of n instead of all the way up to n.

Tuesday, September 27, 2011

New GTK+

http://www.webupd8.org/2011/09/gtk-32-released-with-html5-allows.html

You know... I'm not really taking advantage of all that VirtualBox allows me to do. I'm still running Ubuntu 10.10 because I'm scared of the big UI refit that 11+ added. There's no reason for me not to just make another VM and test the waters.

Win32

One of my (ex) coworkers had to get up to speed on Windows programming and learned enough of the Win32 API (not MFC, or not much of MFC if I recall) to get the job done.

I spent last night perusing some Win32 tutorials and it doesn't look that hard. It has its own special weird things you have to do "just because", and the event handling is called message handling but other than that it looks rather analogous such that it wouldn't be too tough to get familiar in a hurry if I had a free weekend.

The way it handles messages is (at first glance) organized different than how GTK does it. In GTK you explicitly list all the signals you want to catch and pair it with a callback function. In Win32 you have one callback function paired to a window and in that callback function you use a big switch statement to handle any incoming messages.

The reason I mention this is that I'm still really fuzzy on all these Windows programming options. I know that .net is a framework and MFC is sort of the C++ option for Win32 (right?) but I have no idea what WPF or COM or any of those other TLA mean (beyond a quick Google search anyways).

As an amateur level hobbyist programmer that wants to evolve beyond simple ~100 line console programs to useful GUI-based programs that do interesting things I feel very nervous about hitching my wagon to one particular OS-specific framework. I picked GTK because it has decent cross-platform support (excluding OS X unfortunately) but it concerns me I almost never see programs for Windows written in GTK.

I'm just being paranoid about spending time learning something that isn't useful in a lot of situations. That being said I'm happy that the time spent with GTK at least allowed me to understand the Win32 API on a superficial level.

Sunday, September 25, 2011

Divisor count results for 3 million

The number between 1 and 3 million with the largest number of divisors is 2882880 with 336 divisors.

Execution time was over six hours.

The actual output of the program (with CodeBlocks' own time counting):

---

The largest is 2882880 with 336 divisors

Process returned 0 (0x0) execution time : 23027.710 s
Press any key to continue.

Notes on divisors

Project Euler 12 requires counting how many divisors a number has. It only wants positive divisors, and 1 and the number itself count. So, the number 12 has six divisors: 1, 2, 3, 4, 6, and 12.

You're trying to find the first triangle number that has 500 divisors. Well, I'd like to not bother calculating triangle numbers that aren't going to have anywhere near 500 divisors so I want to know how high numbers have to get before they start approaching having 500 divisors.

I wrote this program to loop from 1 to a high number, count how many divisors each number has (and dump it to a text file), report which number had the highest number of divisors, and the number of divisors it had.



I had hoped to graph it out see it generally rise and maybe guess about where it would hit the 500 mark, but I ran it from 1 to a million and it only got to around 200.

The program would eventually get me the answer I want but once the numbers get high my CountDivisors() function starts really slowing down. Now I'm researching faster options using recursion.

This exercise is also revealing how much basic math I've lost over the years. Like the difference between divisors and factors. Divisors will divide a number with no remainder and factors will multiply into the number (which can be broken down into a set of prime numbers). Terminology, woof. I need a fifth grade math book on hand.

EDIT: I set the upper limit of numbers to get divisors from to 3 million. I started it this morning and it's still going. I'm about to head out for the afternoon so I'll leave it running. I think I'll stop it tomorrow morning if it isn't done yet. I got rid of the printf statements since they do indeed slow down the process but now I'm not sure where it's at in the loop.

Wednesday, September 21, 2011

Project Euler 11 Solution



This one wasn't so hard. The difficulty was resisting the urge to make it sleeker.

A better way to do this (ah, hindsight) would be to have one loop and at each position in the 2d array do all the horizontal/vertical/diagonal math right then. It would have been a bit tricky to keep up with all the "are there enough numbers available to do this" checks that would have been needed.

This problem reminded me of the some time I was reading about object oriented programming - specifically about why the methodology is used. I had read that a class should contain a set of data and all the operations you would want to perform on that set. I could rewrite this program such that an initialized class would load up the relevant array and then the methods of that class would get each of the different products I'd need.

EDIT: I looked at the solutions other people created and it seems that this brute force method is very popular.

Project Euler 11 WIP

It's not pretty code, but so far so good.





I have the horizontal and vertical sets functional. The diagonal ones shouldn't be too much more difficult but I need to sit and pencil/paper it out before I take time on it.

This is one of those programs I worry about not because it's tricky but because someone more familiar with the language could probably do it in a quarter of the lines.

fixed 2d array program

I fixed some big naming errors (I swapped rows and columns).

Tuesday, September 20, 2011

My Everest

A ray tracer would be my Everest. It requires competency at two things I desperately want to be good at: programming and math.

Someday there will be a Chris Learns Math journal. I'll probably start with pre-calculus algebra as a review. I actually excelled at that kind of thing and I recall doing very well on the SAT IIC which tested for algebra/geometry/trig. I'd rather focus after that on linear algebra and maybe calculus (which I still maintain a deep well earned fear of). It wouldn't kill me to do some statistics as well - another subject I was good at although the course I took was not statistics for engineers.

off topic

I've been playing Minecraft again now that another big update has come out and it makes me think of two things.

1) One of the game's big features is that it's very retro (for lack of a better word). It employs the use of "pixel art" (the quotes are because all digital art is pixel art but nevermind) and it gives off an 8-bit vibe like it would have been our favorite game on the original Nintendo had it been capable of 3d graphics. What occurs to me is that in another decade or so perhaps the blocky 3d graphics of the 90s (like the original Quake) will be used for some popular retro-looking game - probably once games start trying to do real-time ray tracing or something. Minecraft by virtue of not setting out to be a realistic looking game can get away with having low system requirements and have a massive map size.

2) I really wish I had a few more years of programming experience before Minecraft came out. I think of neat ideas for utilities or mods and I'm kind of just stuck still doing simple Project Euler problems. I know that in time I'll reach the next plateau but I feel like a kid that can't wait to grow up.

On the second point I have been having a hard time motivating myself to program. I'm at a point where everything is just grunt work like plowing through the GTK book or banging my head over the PE problems. Yes they are indeed extremely important but it's starting to become a chore. I need a pet project like a ray tracer or game mod or something that has neat flashy results that would improve over time with me.

Friday, September 16, 2011

Rethinking my smart phone choice

I had no idea Android phones could run Python.

This Reddit post is of a time lapse video - the OP put the code on GitHub.

2d array sanity check



This wasn't to hard to figure out - noodling about with 2d arrays is something I've always had an easy time doing (that is to say nested for loops don't scare me).

Ok - good sanity check. Hopefully this will make the rest of PE11 a bit less stressful.

Tuesday, September 13, 2011

Project Euler 11 WIP

Project Euler Problem 11 is a pretty dramatic departure from the prior 10 problems (except maybe with the exception of problem 8).

I wrote out a smaller (6 x 6) version on paper and realized that it's not as hard as it initially looks. I'll have to brush up on my 2d array initialization.

The plan is at each element of the array to check if there are four numbers to the right, down, diagonal left, and diagonal right. That SHOULD handle every case of where there are four numbers lined up according to the rules of the problem. For example I don't need to check the diagonal to the upper left on element (6,5) because it was already handled by the diagonal lower right of element (3,2).

Traversing rows for checking or multiplying is easy. At element (3,2) the other three elements I need for the lower right diagonal are (4,3)(5,4)(6,5) - basically I just add +1 to the x and y three times. Similar rules apply for the other rows I need. I can likely combine the test of whether or not an element exists with the multiplying - if I try to add or subtract from an x or y point such that it falls out of bounds of the array I can simply not store the result and move on.

If I was feeling really fancy I'd do this in GTK and visualize the array and highlight the numbers I'm currently checking. It might be a good excuse to learn some Cairo.

Monday, September 12, 2011

Project Euler 10 Solution

This was annoying. I got the program right but the formatting of the eventual answer wrong. The acceptable format characters vary dramatically from compiler to compiler and the warnings (or lack of) can't always be trusted.

For reference (if I get around to optimizing this program) it took 8.5 minutes to run. OH and all the other stuff in the program for generating the list of primes under 2,000,000 to a text file was just for fun.



EDIT: I worked out on paper how to do the prime test faster. The next time I need it I'll implement it. I bounce between doing these problems on Linux, OS X, and Windows so that I can be exposed to the variety of platform and compiler differences that come up even in simple problems (such as this one). It's been a real learning experience but the big downside is that all my code is scattered between three machines. I did once make a fast prime finder but I have no idea where it is and my file organization for my programs is not very good. I'm considering using Dropbox.

Sunday, September 11, 2011

Project Euler 10

PE10 asks you to sum all the primes below 2 million. I've written the program with my old prime finder function but it's apparent I'll need a smarter primer finder. I can sum all the primes below 1 million in a reasonable amount of time (under a minute) but I left it running for the 2 million sum for a while and it's taking too long and really heating up my laptop.

There is a good prime finding solution using recursion I was shown once that I need to figure out for myself. Time get a pencil and paper.

Saturday, September 10, 2011

Project Euler 9 Solution



Today's lesson is twofold. 1) Read the problem twice before diving in with both feet. 2) Wikipedia is your friend (and I have Matt to thank for that tip).

I completely misread the problem, but in my defense it's a poorly written problem. Thankfully Euclid gave us a handy way of generating Pythagorean Triples and the rest was easy.

wait now

I think I misread the problem and that square root test isn't necessary. Hm.

the fix for OSX

I had to change the square root test to:


if ((int)(sqrt(n) * (int)sqrt(n) == n))

This takes the square root of n and truncates everything after the decimal via cast to integer before multiplying against itself. There are many intricacies of number types that fall into the category of "not really important until it suddenly is".

Friday, September 9, 2011

one foot in front of the other

The next Project Euler problem I'm working on requires testing if a number is a square. There are some very complicated solutions for this but the one I'm going to try isn't very complicated at all which makes me wonder if I'm doing something wrong.

Basically if I have a number X I'm taking the square root of that number, multiplying it by itself and testing to see if I get the original X back out again. Usually that would result in all numbers passing (says my calculator), but for reasons I don't completely understand yet my implementation works. I thought that I would have to take the result of the square root (which would be returned as a float), cast it to an integer, and then square and compare. This isn't the case for some reason. I need to read up on what sqrt() is actually doing.



EDIT: On a hunch I built this program on my Mac and it totally doesn't work right. I got lucky on Windows.

EDIT 2: For the life of me I can't find the implementation of that function anywhere. That's ok. I'll just be really explicit about casting and I should get the results I want.

Tuesday, September 6, 2011

long weekend

I haven't been doing much programming the past week. I spent a few frustrating hours trying to find information on making XCode 4 work with SDL but didn't have any luck. There was also some reviewing of my old code to remember what I was up to and to make sure I could still read it. Luckily my Python code I was doing for work is well documented else I'd be totally lost. For whatever reason C is like riding a bike but Python is like trying to remember the plot to Mission Impossible 2.

Saturday, September 3, 2011

comforting

I'ts nice to know Linus Torvalds has similar structure to his programs (which he admits this was done while learning GTK at the same time). At the least it's nice to have my bracket style validated.

https://github.com/torvalds/diveclog

Monday, August 29, 2011

Back to it, I hope

Wedding and honeymoon are accomplished so now it's time to get back to my regularly scheduled hobbies.

It's been a while so I need to get back to C as my primary goal. What I'd like to do is figure out what I'm strong at (loop structures, pointers/variables) and focus on what I'm weak at (recursion, memory management) and inexperienced at (advanced data structures, higher math computations, string handling). I think the best thing for me to do is return my focus to Project Euler problems and throw in some GTK when it fits like I did for problem 7.

I did a pretty good job of documenting my Python progress (here and elsewhere) so I'm not worried about stepping away from it for a bit.

I'm on Problem 9 now and I'd like to get through Problem 15 before the end of September. I think that's a reasonable goal.

Thursday, August 11, 2011

A break

I'll likely not be able to do any programming until after the wedding/honeymoon business is over. After that is the "busy season" at work, so we'll see. I need to get back to my C exploration and leave Python to studies of OOP and work-related stuff.

Monday, August 8, 2011

OOf

My mind keeps dwelling on this.

I started the project in earnest this evening, documentation and all. I'm hoping that once I show the functional application at the office I'll be allowed to spend work time on it so that I don't have to eat into my limited free time at home anymore. It's been a fun exercise but I need to get back to C and it is starting to feel more like work than play!

Another way

I always labor over some program and then think of a better way to do it soon after I finish.

The way I should have done it is to have a base class for loading the file. Subsequent classes that inherit that base class should be for paring down the data in the file for specific tasks.

So the closedcases class needs to only contain the data for the closed cases and any functions I'd want to run on that data. I need a way to mimic an array of structures so badly! I want to strip down the caselist to an array of paired start and end dates. I'd say that qualifies as an list of tuples, but I'm totally guessing because I'm still not strong on the Python data types.

The other looming issue is that column format is still in flux. I just made a change to the real case list that breaks compatibility and I'll have to add that to my test file and modify the program accordingly. It would be really awesome to have some kind of config file that pre-defined the column header names and matched them to an index (number).

I learned enough doing this to warrant a quick rewrite attempt. The code is in a "it will do the job but it ain't pretty" state but I'd like to future proof myself ASAP to prevent more intricate rewrites once the project gets fleshed out.

EDIT: I'd also like to restructure it to be called as a library from another program. Running the case reader program would simply initialize a test/sanity check program. There is a neat way to do this by having code in there that detects when it's run as a standalone program and runs things based on that condition. Like this:

if __name__ == "__main__":
stuff()

Saturday, August 6, 2011

slight modification?

I think the date subtraction function should be in the closeddates class.

One time I read a description of proper object/class use that went something like this: An object should contain data and all of the function necessary to manipulate that data.

That might be a oversimplification but it kind of rings true with Python. All lists are an instance of a class, and the .append() method is a part of that class. I'm not calling append() which then initializes the class and makes the manipulation. So, the act of manipulating the closed case data should be inside the class.

Then I can just say:

c = closedcases(filename).subtract_dates()

right?

Friday, August 5, 2011

The most frustrating program I've ever written



I think it's hard for anyone to take data in one format and get it into another to perform calculations. It's double hard when the language fights you a bit. I did everything I could to keep things "Pythony" but I'm still drastically unfamiliar with the language. I have a horrible suspicion that someone familiar with the language could do this in three lines of code.

Basically what the program does is this:
1) Read in a .csv file such that every row has its columns separated as individual elements in an array (Python list). Basically it makes a 2d array. List. Whatever

2) Remove the first row because it's the column names

3) Go through every row and copy over the ones that have something in the open date and close date columns. This makes a new list.

4) Take the new list and extract for each row the first and second column

5) Convert these extracted elements into a 3 element list

6) convert THAT list into a list of integers

7) convert that list of integers into a date and use it to calculate days between the close date and the open date

8) emit heavy sobs when it takes all damn evening to write, rewrite, write special simplified functions, rewrite again, insert a ton of print statements to get a handle on what's actually happening, finally figure out how to manipulate lists like you want, and then clean up any misc bugs and finally at your last nerve see it work.

I'm frustrated that I still don't know when to use classes vs a bunch of functions.

I'm frustrated that scope is horrifyingly ambiguous.

I'm frustrated that in a week I won't be able to read this code and make sense of it.

I'm happy that I tackled the hardest but most meaningful part of the analysis I want to do.

I can't balk at the fact it only took me a week to get well versed enough to first get through enough of LPTHW to comprehend the language and knock out the program. I started working on it this morning. I think I spent 3.5 hours on this total? Not bad, but it was an infuriating 3.5 hours. That time isn't counting when I walked away in frustration.

missing the point?

The thing is I feel like this an unnecessary use of objects. The whole caselist class could just be replaced by a function. The bulk of it IS a function!

Why encapsulate inside a class? What benefit does this provide?

The answers to those questions are one of the reasons I plowed into Python. I want to get the answer before I get mired in the minutiae of C++ or Obj-C.

a note on scope

Found something interesting here: http://docs.python.org/tutorial/classes.html

If a name is declared global, then all references and assignments go directly to the middle scope containing the module’s global names. Otherwise, all variables found outside of the innermost scope are read-only (an attempt to write to such a variable will simply create a new local variable in the innermost scope, leaving the identically named outer variable unchanged).

That certainly explains what I saw a few days ago.

It's a start



This gets me to the bare minimum of getting the .csv file into a form I can work with. I'm not certain that this is the "pythony" way of doing things, but I know what to do with an array.

Thursday, August 4, 2011

Just to really beat it in...

Ok, here are some simple examples to drive the point home.

First is a script that has a variable assigned and a function called can see this variable.



The output of this script is:

Here is a variable I can see: 42
Here is an outside variable as seen from a function: 42

Ok, this makes sense. All well and good.

Here is a script that has a variable assigned, and a function called can see it BUT when it tries to modify it I get an error.



The output:

Here is a variable I can see: 42
Traceback (most recent call last):
File "scopetest2.py", line 10, in
afunction()
File "scopetest2.py", line 4, in afunction
print "Here is an outside variable as seen from a function: %d" % im_outside
UnboundLocalError: local variable 'im_outside' referenced before assignment

If, however, I simply first inside the function state "global im_outside" it becomes allowed to modify the variable.

Alright, let us see a C example:



Output is:

Here is a variable I can see: 42
Here is an outside variable as seen from a function 42

And altering the function to modify the variable:



And I get the result I wanted:

Here is a variable I can see: 42
Here is an outside variable as seen from a function 42
But now I'm going to modify it
And here it is: 27

I am certain I understand how scope works in C. It's pretty straightforward and a lot of the difficulty with understanding passing values/references to functions is a consequence of that simple straightforwardness. I think what this means is that I don't understand scope in Python. I just think if a function can "see" a variable it should also be able to modify that variable. Look, I get it that maybe having the "global" description happen elsewhere is a good idea because then you can let SOME functions modify the var but not others, but man is it not intuitive.

scumbag python

Ok wow.

So, you have to say:

global wumpus_room

BUT you can't declare it as global AND assign it at the same time.

Oh, and the kicker? You're after-the-fact declaring at as a global variable INSIDE the function you want to modify it in!

So:




I guess by "declaring" inside the function that it needs to be on the lookout for a outside-declared instance of wumpus_room that this makes it ok to alter. This does NOT AT ALL explain why I can see the initial value of the variable as defined outside of the function.

Is "global" only necessary to MODIFY a variable outside of the scope... but not read the value? Doesn't that make the concept of "scope" twisted?

EDIT: Here's the site I got from my google searching on the subject http://www.saltycrane.com/blog/2008/01/python-variable-scope-notes/

Python woes



Woof, I don't understand variable scope in Python.

I have what I thought was a global variable called "wumpus_room" that is a random whole number between (and including) 0 to 3. The script is borking at the fact that I have a line that could alter this variable inside of a function. If I comment out the block starting with

elif choice.lower() == "shoot north":

then there is no complaint.

This would not be confusing if it was crabbing about trying to alter a variable out of scope, but it gives a cryptic error:

UnboundLocalError: local variable 'wumpus_room' referenced before assignment

See.... I'd BELIEVE that if not for the fact I can comment out that reassignment line and everything works fine - this tells me the variable is indeed getting assigned!

How the heck do I declare a global variable and then edit that variable if necessary?!

Wednesday, August 3, 2011

LPTHW Exercise 35 and 36

LPTHW exercise 35 brought home a lot of function and control structure concepts with an example of a small text adventure game.

http://learnpythonthehardway.org/book/ex35.html

Exercise 36 asks that you take what you learned and make one of your own. I'm doing basically a five room "hunt the wumpus" game. The structure is basically every room has its own function, and each room asks which way you want to go or where you want to fire your arrow which then either calls the respective room's function, or the fire arrow function. It's not a "proper" way of organizing data for a game but it's just to drive home functions and control structures.

This really brings me back to middle school doing some QBasic programming along the same lines. I have a really strong grasp of control structures because of all the nested if-loops and while-loops of the text adventure games my friends and I used to make together. It sucks that ASCII graphics is now really hard to do since terminal interactivity is now considered an anachronism (among most folks, anyways).

The Python documentation makes no sense to me

Here's how a Python class function (method?) prototype is presented:

class datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])

I read this as tzinfo is an argument of microsecond which is an argument of second which is an argument of minute which is an argument of hour which is an argument of day. But this isn't the case. It's just a list of arguments that can be passed separated by a comma. So... why the brackets?!

Furthermore in THIS class function the non-bracketed variables are stated as required, and the bracketed ones are optional. So I say to myself "Ah, that's how they define optional and non-optional arguments. However, here:

class datetime.time(hour[, minute[, second[, microsecond[, tzinfo]]]])

the documentation says that all arguments are optional, which blows away that theory of operation.

So yea, the biggest hindrance to getting to the next level in Python is that I can't read the documentation.

labels

Huh, I guess I can tag my posts now. That's cool.

Tuesday, August 2, 2011

even more progress

I'm on exercise 27 now in LPTHW. I think I've spent a total of four or five hours on it. It really helps already being familiar with C since I don't have any issues comprehending the "advanced" stuff so far (functions, return values, taking arguments, importing functions from other files). I'm still weak on some of the formatting like %d and %s and %r in the print statements, but basically it's numbers, strings, and a different way of formatting string statements.

a little progress

I got through the first five exercises in LPTHW, which isn't saying much since it's just typing out the examples (no copy/paste is the rule in this book) and it only took about 45 minutes (including setting up the environment). So far It's all just printing and some light variable usage.

Here's where I stopped: http://learnpythonthehardway.org/book/ex5.html

Sunday, July 31, 2011

Bum weekend

I didn't read about, do, or think about coding this weekend. Too much driving around and doing pre-wedding chores. So it goes.

Add to the list of things I'd like to experiment with:

Ray tracers
databases

Wednesday, July 27, 2011

GTK program organization

Ok, so source control methods aside the one other thing I want to try differently is overall program structure.

The organization so far has had all of the GUI code right in the main() function. This is generally probably not a good idea. I've been trying to hash out the behavior of what's really happening when I'm writing some of this GUI code.

See, MOST of the stuff I'm writing is initializing pointers to GTK objects with some basic behaviors and then connecting signals to functions. The real business doesn't start until gtk_main() is called. What that means is I can offload widget initializations to other functions (in other .c files) and then call those functions in main() right before I call gtk_main().

So, here's an example of this in action:



It's important to remember that there's a lot happening in DrawWindow but all it's doing is initializing pointers that aren't going to go out of scope once the function is over. Calling the function DrawWindow was dumb since it's not doing the drawing. The function gtk_widget_show_all can be in main() instead of DrawWindow... I'm not sure which is "proper" but in this simple example it doesn't seem to make a difference.

I might add in a little interactivity just to verify things work like I expect them to, but hey I'll call tonights efforts a win.

workflow

I think the problem is I don't understand the source control workflow. Edit, compile, then push? Does it only work for the code files or can my IDE project file go with the push?

EDIT: OK well, nevermind for now. I can deal with not using source control for the first version and I have another major thing to bang out before I proper start.

Tuesday, July 26, 2011

hosting

I've hashed out what I want the first "finished" release of the front end program to be. It's going to be just a selector for single player or multiplayer, and then if multiplayer a choice between hosting or joining. My hope is that it will be a solid and clear base that will be easy to just keep adding to.

The reason I haven't started proper yet is because I want to try and host the code somewhere but I can't figure out what the best solution is. There are many options: http://en.wikipedia.org/wiki/Comparison_of_open_source_software_hosting_facilities

It's not super necessary to host it - I just want practice with something relatively simple.

Github seems all the rage, but I don't really even understand Git.

Monday, July 25, 2011

research

I spent some of my free time this weekend (and time procrastinating) researching the file selection widget in GTK. Seems straightforward. Another interesting concept I stumbled across is "graying out" text entry boxes, buttons, and other selection widgets. Good info.

Man I'm behind on Project Euler stuff. And I haven't touched much Python nor started on any of those Python books.

Bad overextended hobbyist. No biscuit.

Friday, July 22, 2011

launcher idea

I've been fiddling with GZDoom a little bit and there are several command line options for hosting and connecting to multiplayer games. There are even more for what mode of multiplayer and maps to play.

I did a little research and figured out how to run a program from C using a Windows function called ShellExecute(). It's pretty straightforward and I confirmed I could use it by making it run Notepad. The nice thing is that it's running it just like if I ran it from the console or double clicked an icon (as in, not dependent on the calling program staying open or anything).

There are several front end programs for GZDoom/ZDoom but none that I can find that are open source or written in GTK (and therefore could be cross platform easily if I can find similar program running functions in Linux/OSX).

I've been itching for some way to write a potentially useful program that would be complex enough and useful enough to a community to justify learning Git (so I can use Github) or hosting on SourceForge or something.

The only thing I don't immediately know how to do is the file selection dialog. I'd need the ability to point to the .exe and .wad files and it would be slick to figure out if the .wad files have any useful info inside of them like level names I could extract. My quick skim of the GTK documentation gives me the impression that it's not excessively tough to do a file selector. This makes sense considering how common that feature is!

Other than that it's just a matter of layout and how options should be toggled. The obvious first push should handle the most common ones. I'm getting better at string handling so storing all the options enabled and shuttling them to a string to be executed as a shell command shouldn't be too tough.

I really wish I was at the chapter in the GTK book that handles Glade so I wouldn't have to do the UI by hand, but maybe I need the practice!

old school

http://www.brackeen.com/vga/

It's super tempting to download DJGPP and Dosbox and try some of this out. Turbo-C and DJGPP we're my first C compilers. DJGPP used to be bundled with the Allegro graphics/audio/input library and I remember spending many hours reading the example code and compiling the sample games with minor variations to adjust physics and other properties.

Thursday, July 21, 2011

python download script

The second answer to this question on StackOverflow is exactly what I need to test my download speed.

http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python

I've already tested it and it works as advertised - it downloads a 10MB file. I should be able to pretty quickly adapt some of the code from my security camera program to handle the timing so that it can run every 30 minutes and record a completion time to a text file.

Wednesday, July 20, 2011

Commas

http://mindtribe.com/2011/07/forgotten-c-the-comma-operator/

I saw a comma in a goofy place recently while reading someone else's code. I wonder if it was an instance of using it as an operator.

Friday, July 15, 2011

Prepping a library for Python

I've been reading a bit about the CTypes module in Python and I want to see if I can call a library I wrote to do something interesting, like read something out of a binary file - something I can set up pretty quickly in C now but wouldn't know where to start in Python. So, it follows I should write a library for that purpose in C and call it from Python. I'm almost done with it - here's the meat of it:



I'm writing two versions of a function that gets an array of long integers out of the file. One version of the function returns a pointer to an allocated array that contains all the data. The other version of the function (TBD) requires this allocation be done before-hand and you pass it the address of the array for it to fill. I did this because I'm not sure which is the most appropriate way to do things in Python. Hopefully both will work and that will give me a really good feel for that module and make fleshing out the library much easier.

I went a bit overboard compartmentalizing functions. The "skip" functions could have been one function but I went for verbosity over brevity.

An early draft of the function for getting data involved an elaborate flagging system where you passed NULL as the array to be filled and an address of an integer if you wanted to just get the number of data to be returned, and the opposite (address of array to fill and NULL of integer address) to get the data out. This I decided was too complicated AND I realized that the capability to overload function names (use the same function name in different contexts depending on how you call it) in C++ would have been useful.

I banged this out in two evenings but to be honest I did recycle some code from prior similar projects (maybe a dozen lines total recycled). This weekend is going to be nutty but I hope to have this code in a static library next week for some Python testing. Right now it's just a .c file in a vanilla console program project in XCode and in main.c I have some quick verification tests where I ask for data I know the values of.

Onwards and onwards.

EDIT: This project has certainly been a big retesting of my pointer knowledge - especially since I do more allocating here than I've ever done. FYI the function that returns a pointer to an allocated array assumes that you free it later.

One year

I started this journal one year ago with a netbook and enough anger at my prior failures to learn C to motivate me to push through the hard parts.

The best way to summarize my progress over the past year is this: I'm more overwhelmed by the things I can do now than the things I have yet to learn. That's a clumsy way of saying that the past year's progress has unlocked the capacity to pick any arbitrary next step and go along with it. The next step I've gone the farthest with is GTK and I'm learning now that Python and C are not mutually exclusive and I'm really itching to learn how to write Python modules.

My goal as stated in the journal header was to gain competency in C before I'm 30. I've learned now that this isn't exactly a coherent statement. Learning C is not learning how to program any more than learning the alphabet is learning how to write.

There are still several big topics that elude me, but they only remain that way because I have yet to sit down and hash through them. These topics are less about C specifically and more about data structures and programming methodologies common to most languages.

In the immediate future I'd like to get through a few more Project Euler problems and get through a few more (extremely dry) chapters of the GTK book. I'm learning about Python on the side but what's keeping me interested in that Python itself is written in C and it's well supported to write custom modules in C.

On the long term list I'd like to get a better handle on OOP and pick a (non-interpreted) language to explore that with such as C++ or Objective-C. Getting a familiarity with network programming (sending/receiving packets), a graphics library (SDL or OpenGL) and databases would be fantastic, too.

That's pretty much my one year report.

Last night I started rewriting my file reader to have all the important functions kept in a static library that I can load into Python with the CTypes module. Eventually I want to write an actual Python module to import data from the files and see if I get better results.

I'll do another general progress report in April when I turn 30.

Wednesday, July 13, 2011

moving

The move derailed my programming progress but everything is back in place and my internet connection is restored so I think I'll be continuing soon after this weekend.

I've given more thought to how to handle the Python case manager in a OO way. I had a column of database information that contained slash-delineated items and the tag column that had space-delineated items. I realized if I made them both space-delineated I could have a class for storing each item into a list (or array or whatever Python data type is best) and then have a subclass that inherits that feature and adds functions specific for the database info and another class that has specific functions for the tag info. This way if I ever have any other space-delineated items I'll always have the class for handling that which I can add on to with a sub-class (or... whatever it's called in OO terminology).

EDIT: I've been all talk and no code for WEEKS now, which is double shameful with Python since it's quick and easy to prototype something out.

Wednesday, July 6, 2011

Cases organization

I think I'm right about having class (lets call it Case) representing the data structure and methods to act on that data structure. What I also need is a class (lets call it Worklist) that contains at the least an array (or the Python equivalent) containing Case instances (instances?) and probably it will need the function for populating that array from a .csv file.

Lots to think about.

first OO step

I want to keep my C learning as straight C right now and not try to dabble in C++ or Obj-C quite yet. I'm going to use Python to experiment with object oriented programming. Hopefully once I get my head around OOP it will translate to C++ or Obj-C if I get around to that.

There isn't a Project Euler for OOP concepts (to my knowledge) but I think I have a good idea for a project. I mentioned before my interest in using Python to parse a CSV file so that I can extract certain information from it that's not quickly doable by hand. I made a test CSV file:

 

It's just example data that mimics what my support tracking spreadsheet looks like (but converted to CSV). There are many ways of formatting CSV and it's important to note that columns that include commas that aren't to be used as separators of columns have quotes around them. The columns are as such:

Date Added: This is the date I entered the case. They are always in chronological order.
Date Closed: The date that the issue is resolved. If this column is blank it means the case is not closed.
Group: The company the user is with
Contact: The person I interact with in the company on the issue. It's usually just one person but every so often I'll include two names separated by a comma.
Issue: A description of what's going wrong.
Notes: This is where I keep track of my thoughts on the matter. In the real thing I have a system for tracking my thoughts across multiple days by prefacing an entry with "UPDATE 7/5/2011: " but I left that out for simplicity until I learn more about parsing CSV in Python.
Database: If there are returns or orders made in our shipping database in regards to a support case I include the relevant numbers or names here.
Tags: This is experimental but I really want to use it. Tags are semi-standardized classifications that I'd love to use for searching. My goal is to be able to search for any case with a specific tag or tags, and obviously count cases with certain tags to keep track of what's having the most problems.

I'm an OOP newb so I'm not quite sure how to arrange the data and operation on the data into classes. If I were to do this in straight C I'd define a struct that has each of the columns as a member, and then I'd have an array (or linked list) of these structs such that each element of the array held all the info of one case (one row in the CSV file). If I wanted to print out every case that had the tag "XYZ" I could easily scan through the array and find them. This seems very straightforward to me but the goal here is to use OOP which right now doesn't seem straightforward but hopefully it will later.

Should I define a class that represents a case? What methods would that class contain, and how would the data for that class be stored? Maybe one useful method in a class would be to return how long it took for that case to close (and return NULL if the case isn't closed).

But then how to I store all of the unique instances of the classes that basically represent every line in the CSV? Then we're back to arrays. Can there be an array of instanced classes? How do I initialize each class such that it will be filled with the data from one line of the CSV, and how do I keep track of where I am in parsing the CSV? Should the functions that do the parsing be contained in a class of its own?

Headache. I feel like this is unnecessarily difficult but maybe by the time I work through those intro to Python documents I'll know enough to ask better questions.

Tuesday, July 5, 2011

long weekend

I purposefully didn't do much this weekend. My brain needed a break and I need some time to think about what direction I'm going with all this.

Unfortunately I'm swinging into a less creative mindset (which allows me to program or write music) and into a more consuming-oriented mindset (video games and TV). Some would call that "being a lazy bum", which sounds about right.

Thursday, June 30, 2011

chrislearnspython

If I can get through LPTHW and DITP I'll pick up the O'Reilly Python book.

LPTHW takes a really, really slow approach and doesn't get into functions and classes until the end and is more of a "learn programming" instead of "learn Python". DITP is the opposite.

Wednesday, June 29, 2011

Python CSV module

Oof, Python works in mysterious and curiously documented ways. No, that's not fair - I'm just not down with all the lingo.

I wanted to quick and test to see if I can pull in some CSV data.

test = csv.reader(open ("Support Worklist.csv"))

test is now a type of storage variable called an "iterator". An iterator is an object that uhm.. iterates? It's a bizarre concept because it has basically one method called next() that delivers the next line of the data and once it reaches the end of the data it stops. There is no "previous" or "rewind" method which is really baffling. I have no idea what use it is! Maybe I'm just not thinking very Pythony yet. I need to get through those real popular intro documents "Learning Python the Hard Way" and "Diving Into Python".

So all this quick test did was reveal that I'm pretty ignorant of Python's data types and it's preventing me from doing anything interesting.

Dang. I was really hoping the CSV module actually DID things with CSV files other than read/write them.

EDIT:
Hmm... http://docs.python.org/library/csv.html#csv.DictReader

EDIT x2:
StackOverflow's definition of the "iterator" tag is pretty revealing:
An iterator is an object-oriented programming pattern which for the most part functions similarly to a pointer to an object inside a collection with the added ability to traverse through the collection in a defined manner, agnostic of the actual implementation or even object addresses in physical memory. Iterators may be further limited in particular traversal directions.

Python project

http://docs.python.org/library/csv.html

I'm itching to try and write a quick and dirty program for zipping through an Excel spreadsheet I keep for work stuff and giving me some basis stats. Nothing fancy - it's just a list of support cases where every row is a new case and the first colum is the date I entered it in MM/DD/YYYY format. I was trying to find a module that read in .xlsx files but then discovered that there is a built in CVS reading module. Score. Now I gotta figure out how to extract all the dates and then (for example) count how many entries were made per month (per month that exists - I started this spreadsheet in January of this year).

Using a shared library written in C with Python

I'm trying to combine some C and Python. Did some "light" reading on the ctypes module in Python.

There are several ways of using C libraries in Python. Ctypes, SWIG, Cython, and plain making a Python module in C. Ctypes seemed the most straightforward.

Ok, to start with I needed a library. I jumped into Ubuntu and started a new shared library project in CodeBlocks and did the following:

This compiles to a .so file. I named the project sayhello so it compiled libsayhello.so. I put this file in /user/lib.
Alrighty so I have my library. Time to get into Python. I don't have any Python IDE so I just use the terminal. I want to use Python to call my sayhello() function.

And there you have it - a VERY simple example of calling a C function in Python.
There is a lot about shared libraries I don't understand. I have a superficial knowledge of what's happening behind the scenes (and I know what the benefits of using dynamic link libraries are). I keep reading things about "exporting" or "importing" functions but I'm not sure what that means, and I don't understand some of the stuff I see in Windows .dll source code.

Aaaanyway. I've been up since 6am on this and I am done thinking about this for today.

Tuesday, June 28, 2011

win7

NewEgg is having a memorial day sale so I bought another copy of Windows 7 for my laptop to dual boot to. I've basically proven I can't get any real work done on my PC since it's where my games are, so a copy of Windows on the laptop will be for programming and work stuff.

The topic of Python came up at work in a meeting again. I should resume reading those Python tutorials just to make sure I have a good heads up on what's what if that ever comes around.

Monday, June 27, 2011

OO jump

I haven't made any move to learn object oriented programming because I haven't been in the middle of a program and thought to myself that I could really do this or that faster/better with objects. I'll admit my knowledge of OOP is extremely superficial but the bits I know aren't really geared towards the small program I'm writing.

On the other hand I do totally get why it would be useful with GTK (indeed a lot of GTK is mimicking OO behavior) in the readability department. I think the C++ bindings look more like:

windowwidget.setsize() instead of SetWindowWidgetSize(*window, size)

Which would be nice sometimes, but not important enough to make the switch just yet.

Sunday, June 26, 2011

is this cheating?

If I take the square root of a number and then square the result I'll get the same number.

If I take a number and take the square root, then obliterate anything after the decimal point (cast to int perhaps) then square that result I can compare it to the original number and see if they're the same. If they're the same then the square root must have been a whole number.

That's legit, right?

Saturday, June 25, 2011

PE9 finding if a number is a square

http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer

Ok, maybe it's not such a simple problem!

EDIT: Wait maybe I'm overcomplicating this.

Let's take 40 and 36.

The factors of 40 are 1,2,4,5,8,10.

The factors of 36 are 1,2,3,4,6,12.

Is there any way I can get the factors of a number and then square each one to see if they make the product?

So for 40:
1x1 != 40
2x2 != 40
4x4 != 40
5x5 != 40
8x8 != 40
10x10 != 40

For 36:
1x1 != 36
2x2 != 36
3x3 != 36
4x4 != 36
6x6 = 36
12x12 != 36

Hmmmmm. I can't think of any situations where it wouldn't work.

The nagging suspicion that I'm misinterpreting this problem is creeping up on me.

PE9 WIP

Every time I take another look at PE 9 I confuse myself. Woof.

I did PE 8 in XCode and it really helped to have the debugger telling me what all my variables are as the loop progressed. Every problem I was having I diagnosed through that. That's an extremely superficial use of the debugger from what I can tell.

EDIT: Thought of a first step. Remember that a < b < c, so the largest that c can be is 997 (in the instance of a = 1 and b = 2). Obviously that's not a real life situation, but what other constraints are there? b can only be 499 at the most when a = 1, b = 499, and c = 500. Again, not ever actually going to happen but it's a good place as any to make my loop end.

What's an instance where a ^ 2 + b ^ 2 = c ^ 2 such that c isn't a natural number?

2 ^ 2 + 5 ^ 2 = 29. Ok, how do I know that the square root of 29 isn't a natural number? I need to find a way to test for square-rootness. I'm sure there's a good way to do it that's just not jumping out at me.

Project Euler 9

I have no idea how to go about this...

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.


I think I can figure out a way to brute force it, but it's not pretty. Well, there are only so many numbers for a+b+c=1000. 1+1+998, but 1 ^ 2 + 1 ^ 2 != 998 ^ 2.

No, maybe that won't work? Shoot, this is the first PE problem where it wasn't obvious how to brute force it. Oh wait, there's the additional constraint that a < b < c. Maybe that's helpful? Ok so start with 1 < 2 < ? That's 1 ^ 2 + 2 ^ 2 = 5. I can check that 5 isn't a square number (I hope). IF the sum isn't a square I increment b (nested loop?). If it is square, I check for a+b+c=1000. Once I hit a square number as c that causes a+b+c < 1000 then I know I need to start over with incrementing a to 2 and starting b at 3. Then repeat.

That's the way to brute force it... maybe.