Tag Archive - function

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.

Make A Custom Function Available In The Entire Magento System

One of my co-workers recently started working with Magento. Until recently he was primarily working in a custom system we had developed.  One of the functions that he was very used to using was a debug function that simply was a wrapper for PHP’s print_r() that echoed out the beginning and ending pre tags.

Now this was not a show stopped for him, but it quickly became irritating for him to not have quick function available to him in Magento (especially since Magento has some massive objects) and he forgot to put in his pre tags.

Since I am a nice guy, I decided to see if there was a way that I could bring this little function into the system for him and it was easier than I thought it would have been.

In order to add a function that is available in every part of the Magento system you have to remember how a request flows through Magento. Everything starts in the index.php file. All I had to do was add this function to this file and it was available everywhere.

Now a few things to consider when doing this.

  • I would not do this in production (it probably would not hurt in this case, but you never know)
  • In terms of Magento coding standards, I would call this a little “dirty” the more “correct” way to do this would be to put this code somewhere in a header in a module.

If you are curious what the actual function looked like I have posted it below:

function print_array($var, $echo=false) {
  echo "<pre>";
  $str = print_r($var, $echo)
  echo "</pre>";
  return $str;
}

So there you have it. A quick and dirty way to bring in your own functions to the entire Magento system.

PHP Function – str_replace

The str_replace function is used to replace part of a string with another string. The syntax of this PHP function is detailed below:

<?
mixed str_replace( $search, $replace, $subject, $count );
?>

$search – This can either be a string value or an array of strings. This will be the string or strings you want to be replaced.

$replace – This can either be a string or an array of strings. These will be the value that is replaced when one of the $search items are found.

$subject – The string that will be searched and have replacements placed inside of it.

$count (optional) – The maximum number of times a replace should occur.

There are a number of reasons why you would want to use this function. One of the ways that I use str_replace is when I need to remove line breaks from the a large string that you are working with. You would do that by doing the following:

<?
$input = str_replace( "\n", '', $input );
?>

You may have noticed that I used $input as the return variable and the string that is having the replace performed on it. This is a quirk that I use in order to not have to create extra variables and you cannot pass the $input to the function as reference.

Trim a String in jQuery

This will be a quick tutorial on how to remove the excess whitespace from the beginning and end of a string using jQuery. All you have to do is call the trim function like this:

var new_string = jQuery.trim(string_to_trim);

Just a quick look at a very basic function.