Videos
I knew C before I knew Python. No offence intended, but I don't think that your C knowledge is that big a deal. Unless you read very, very slowly, just set out to learn Python. It won't take that long to skim through the material you're familiar with, and it's not as if a Python tutorial aimed at C programmers will make you a better Python programmer - it might teach you things in a different order, is all, and raise some specific things that you would do in C but that you should not do in Python.
Strings in Python actually are somewhat different from strings in C, and they're used differently. I strongly recommend learning them "from scratch", rather than thinking about them in terms of their differences from C strings. For one thing, in Python 2 it's best not to use Python's "string" class to represent strings: there's a separate unicode string class and for practical Python apps (pretty much anything involving user data), you need that. (Python 3 fixes this, making the str class a unicode string). You need to establish a good working practice for unicode/byte data and decode/encode.
A common mistake when learning a second programming language, is to think "I know how to program, I just need to translate what I do in C into Python". No, you don't. While it's true that an algorithm can be basically the same in different languages, the natural way to do a particular thing can be completely different in different languages. You will write better Python code if you learn to use Python idiomatically, than if you try to write Python like a C programmer. Many of the "tricks" you know that make sense in C will be either pointless or counter-productive in Python. Conversely many things that you should do happily in a typical Python program, like allocating and freeing a lot of memory, are things that in C you've probably learned to think twice about. Partly because the typical C program has different restrictions from the typical Python program, and partly because you just have to write more code and think harder to get that kind of thing right in C than you do in Python.
If you're learning the language because you urgently need to program a system/platform which has Python but doesn't have C, then writing Python programs that work like C programs is a reasonable interim measure. But that probably doesn't apply to you, and even if it did it's not the ultimate goal.
One thing you might be interested to look at because of your C experience, is the Python/C API. Python is great for many things, but it doesn't result in the fastest possible computational core of scientific apps [neither does C, probably, but let's not go into FORTRAN for now ;-)]. So if you're aiming to continue with scientific programming through your move in Python, and your programs are typically memory-bus- and CPU-bound doing immense amounts of number-crunching (billions of ops), then you might like to know how to escape into C if you ever need to. Consider it a last resort, though.
You do need to understand Python reasonably well before the Python/C API makes much sense, though.
Oh yes, and if you want to understand OOP in general, remember later on to take a look at something like Java, Objective-C, C++, or D. Python isn't just an OO language, it's a dynamic OO language. You might not realise it from comparing just C with Python, but dynamic vs static types is a completely independent issue from the OOP-ness of Python. Python objects are like hashtables that allow you to attach new fields willy-nilly, but objects in many other OO languages store data in ways which are much more like a C struct.
I learned everything I know about Python from the official documentation: http://docs.python.org/
And it's free.
OK, for that particular example:
- you can get the time services from the standard library here
- you can use eigen for linear algebra. It's an amazing library, I'm in love with it.
- check here to learn how to manipulate files
While using C++, you might miss some features from python, but most of them are actually provided by the boost libraries. For instance returning multiple values from a function is very easy with boost.tuple library as in here. You can use boost::shared_ptr if you don't want to bother yourself with memory management. Or if you want to keep using python to play with your c++ classes, you can use boost.python. Boost.parameter helps you define functions with named arguments. There is also Boost.lambda for lambda functions, but if your environment supports it, you can also use C++11 to have language support for lambda functions. Boost is a gold mine, never stop digging. Just assume that it's part of the standard library. I develop C++ in many different platforms, and neither eigen nor boost has let me down yet.
Here's a good FAQ for C++ best practices. This is a very important principle that you have to keep in mind at all times, while working in C++. I extend it a bit, in my mind and think; If you're going to do something dangerous such as: Allocate memory with a raw new, or index a raw C style array, pass around raw pointers, or do static_cast (even worse reinterpret_cast) etc. They should usually happen in a class somehow dedicated to them, and the code to make sure they don't cause trouble lives very close to them, so that you can see at a glance that everything is under control.
Finally, my favourite!!! Do you want to keep using generators in C++? Here's some dark magic.
Alright, lets just start with C for now.
void readH5Data(FILE *file, int ***sample); // this is for you to implement
void writeH5Data(FILE *file, int ***sample); // this is for you to implement
int main(int argc, const char *argv[])
{
#define width 3
#define height 3
#define depth 3
time_t t = time(NULL);
int ***sample = calloc(width, sizeof(*sample));
for (int i = 0; i < width; i++)
{
sample[i] = calloc(height, sizeof(**sample));
for (int j = 0; j < height; j++)
{
sample[i][j] = calloc(depth, sizeof(***sample));
}
}
for (int i = 0; i < 1000; i++)
{
char *filename[64];
sprintf(filename, "mill2sort-%i-extracted.h5", i);
// open the file
FILE *filePtr = fopen(filename, "r");
if (filePtr == NULL || ferror(filePtr))
{
fprintf(stderr, "%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
readH5Data(filePtr, sample);
fclose(filePtr);
}
char filename[] = "mill2sort-extracted-all";
FILE *writeFile = fopen(filename, "w");
if (writeFile == NULL || ferror(writeFile))
{
fprintf(stderr, "%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
writeH5Data(writeFile, sample);
fflush(writeFile);
fclose(writeFile);
printf("Done in %lli seconds\n", (long long int) (time(NULL) - t));
for (int i = 0; i < width; i++)
{
for (int j = 0; j < width; j++)
{
free(sample[i][j]);
}
free(sample[i]);
}
free(sample);
}
As long as you remember that your array is 3x3x3, you should have no problems overstepping the bounds in your 'writeH5Data' method.