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?