Tag Archive - code

Hello Python

As I talked about earlier, I am beginning to learn Python. I thought that this could be a good opportunity to document what I am learning and share my thoughts, experience and code.

To be totally cliche, I figured I would first demonstrate how to create the Hello World program in Python.

The most simplistic method to accomplish this is to simply print out the phrase:

print "Hello World!"

This looks almost exactly like PHP code expect you will notice there is no semicolon at the end of the statement. That is because Python does not use the semicolon to end a line.

Ok, let’s take this to the next level now. Let’s put this string inside a function that takes in a single parameter.

The function code looks like this:

def helloWorld(name):
  return "Hello " + name

So as you can see a function definition in Python begins with the word “def” followed by the function name (in this case helloWorld), and finally parameters in parenthesis with a colon at the end of the line.

Now this is where things get a little different. Instead of using regular block characters, we simply only indent the code to signify the code is apart of that block. It does not necessarily matter how many spaces or tabs you use in your code, however you must be consistent with how many spaces you use.

Now as you can see this function simply returns the word Hello with the name appended to it (with a space in between).

So now you can use the function in your code:

print helloWorld("Python")

That’s it. You can how say Hello World in Python.

Magento Debugging Basics

Magento is a big system. If you are developing for Magento it is inevitable that you are going to either need to trace down a big or just figure out how a particular process works.

The best thing to do is to start with the controller that is triggered when the process starts. If you are not able to figure out which controller you are starting from, I suggest you buy Commerce Bug by Alan Storm. It will take all the guess work out of that for you as well as tell you a bunch of other information about the page you are currently viewing.

Now that you know where to start, lets talk about the functions and commands I find most useful when debugging Magento.

echo “captured”; exit(); – This does exactly what it looks like. Echos out the word “captured” and stops execution. This is helpful so that you can verify that the code you think is running, is in fact running. It is also a good to use in conjunction with the other commands so you can stop the process without loading the layout or template of the site.

echo get_class($obj); – This will simply tell you the name of the class that $obj is. Since class names in Magneto (and the Zend Framework for that matter) tell you where the actual file is located, this is very helpful so you can follow the next step of the code.

print_r(get_class_methods($obj)); – get_class_methods returns a list of explicitly defines methods that $obj currently has. I say explicitly defines because it does not display functions that come from Magento’s magic functions (all the nice getters and setters).

debug_stacktrace() – This function is good when you know the end point of the code you want to debug but want to see where it was called from. This displays a complete stack trace of the code from the point this function is called.

print_r($obj->debug()); – Nearly every single object in Magento extends Varien_Object. One of the methods that Varien_Object has is debug. This function will show you the data that this object currently holds.

Those are the very basics, but you can trace Magento code very easily with these few functions. What other functions do you use to debug Magento?

Have Your Magento Caches Update Themselves

Magento has a lot of caches. They are (in my opinion) the biggest reason Magento is usable as a platform (If you think Magento runs slowly now – turn them off for a little bit).

One thing that I as a programmer find odd is that the caches in Magento are intelligent enough to flag themselves as invalidated, however they generally require that they be refreshed by hand. This can be especially irritating when you are running Magento Enterprise Edition and your full page cache is constantly being invalidated due to product and category updates.

In order to improve our site performance I wanted to find a practical way to run with the full page cache on all the time without requiring me to babysit it 24 hours a day. I figured I had to find the code that was triggered when you told the admin panel to update the cache.

It turns out that like most things in Magento, unless you know how it works, it is not immediately obvious where this code is located. One tool I have found invaluable is Commerce Bug by Alan Storm. This tool told me that the cache admin page is controller by the controller that is located at:

/app/code/core/Mage/Adminhtml/controllers/CacheController.php

Now in this controller you will find a number of actions. The one that is going to help us is massRefreshAction(). Inside you will see that there is code that actually refreshes the cache

$tags = Mage::app()->getCacheInstance()->cleanType($type);

This is close but not quite what we need yet. The $type is a string code for the cache. We don’t want to always refresh the cache. Just the ones that have been flagged as invalidated.

Looking deeper into the ->getCacheInstance() class that is returned shows us an interesting function that is available called ->getInvalidatedTypes(). BINGO! That is exactly what we are looking for.

Looking at what this function returns we find an array of objects that contain the codes of caches that have been invalidated. Now all we have to do is iterate through this list and refresh those caches.

The final script will look like the following:

$types = Mage::app()->getCacheInstance()->getInvalidatedTypes();
foreach($types as $type) {
     Mage::app()->getCacheInstance()->cleanType($type->getId());
}

Now that we have a script that is refreshing the caches, we just need this script to run every so often and it will refresh any caches as required. I set it to run every 2 minutes, but your mileage may vary.

Get Your Magento System’s Time

Magento handles time in a pretty different way. Instead of just using the time on the server it automatically converts the time to the selected time zone in Magento’s system.

The way to get the current time is by using the following code:

Mage::getModel('core/date')->timestamp(time());

What this will do is take the timestamp that you get from the time() function and converts it to the timestamp that Magento says what time it is. So you can wrap this function into PHP standard date() function to output in a format you are used to.