<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: function code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 16 May 2008 14:32:27 GMT</pubDate>
    <description>DZone Snippets: function code</description>
    <item>
      <title>ARGV Parser</title>
      <link>http://snippets.dzone.com/posts/show/5099</link>
      <description>This function parse ARGV and return a string.&lt;br /&gt;See exemples for more informations :&lt;br /&gt;&lt;br /&gt;// Go : http://blackh.badfile.net/wordz/&lt;br /&gt;&lt;br /&gt;Function :&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def options(param)&lt;br /&gt;  &lt;br /&gt;	i = 0&lt;br /&gt;		ARGV.each  { |valeur|&lt;br /&gt;		&lt;br /&gt;    		if (valeur == '-' + param.to_s)&lt;br /&gt;				return ARGV[i+1]&lt;br /&gt;			elseif (valeur != '-' + param.to_s)&lt;br /&gt;				return false&lt;br /&gt;			end&lt;br /&gt;		i += 1&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;   end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Usage :&lt;br /&gt;&lt;br /&gt;// cmd&gt; ruby test.rb -o foo&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;	out =  self.options('o')&lt;br /&gt;&lt;br /&gt;	if (out != false and out.empty? == false)&lt;br /&gt;                   puts out # print -&gt; foo&lt;br /&gt;	end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 03 Feb 2008 20:24:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5099</guid>
      <author>Black_H (Black_H)</author>
    </item>
    <item>
      <title>Using the XSLT sum function</title>
      <link>http://snippets.dzone.com/posts/show/4976</link>
      <description>This XML and XSLT code is an example of storing and displaying a car mileage log. Notably the XSLT sum() function displays the total no of miles.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;file: car_log.xsl&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?xml version="1.0"?&gt;&lt;br /&gt;&lt;br /&gt;    &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;&lt;br /&gt;      &lt;br /&gt;    &lt;xsl:template match="car_log"&gt;&lt;br /&gt;      &lt;div&gt;      &lt;br /&gt;        &lt;p&gt;Total miles this month: &lt;xsl:value-of select="sum(records/entry/miles)" /&gt;&lt;/p&gt;&lt;br /&gt;        &lt;xsl:apply-templates select="records"/&gt;&lt;br /&gt;      &lt;/div&gt;&lt;br /&gt;    &lt;/xsl:template&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;xsl:template match="car_log/records/entry"&gt;&lt;xsl:variable name="pos" select="position()"/&gt;&lt;br /&gt;    &lt;div class="record"&gt;&lt;br /&gt;      &lt;dl&gt;&lt;br /&gt;        &lt;dt&gt;&lt;label for="trip"&gt;&lt;xsl:value-of select="@title"/&gt;trip&lt;/label&gt;&lt;/dt&gt;&lt;br /&gt;        &lt;dd&gt;&lt;br /&gt;          &lt;input type="text" id="trip{$pos}" name="trip{$pos}" value="{trip}" size="{@size}" /&gt;&lt;br /&gt;        &lt;/dd&gt;&lt;br /&gt;          &lt;br /&gt;        &lt;dt&gt;&lt;label for="miles"&gt;&lt;xsl:value-of select="@title"/&gt;miles&lt;/label&gt;&lt;/dt&gt;&lt;br /&gt;        &lt;dd&gt;&lt;br /&gt;          &lt;input type="text" id="miles{$pos}" name="miles{$pos}" value="{miles}" size="2" /&gt;&lt;br /&gt;        &lt;/dd&gt;&lt;br /&gt;          &lt;br /&gt;        &lt;dt&gt;&lt;label for="description"&gt;&lt;xsl:value-of select="@title"/&gt;description&lt;/label&gt;&lt;/dt&gt;&lt;br /&gt;        &lt;dd&gt;&lt;br /&gt;          &lt;input type="text" id="description{$pos}" name="description{$pos}" value="{description}"  /&gt;&lt;br /&gt;        &lt;/dd&gt;        &lt;br /&gt;      &lt;/dl&gt;&lt;br /&gt;    &lt;/div&gt;&lt;br /&gt;    &lt;/xsl:template&gt;&lt;br /&gt;    &lt;/xsl:stylesheet&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;file: car_log.xml&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;car_log&gt;&lt;br /&gt;  &lt;summary&gt;&lt;br /&gt;    &lt;project&gt;car_log&lt;/project&gt;&lt;br /&gt;  &lt;/summary&gt;&lt;br /&gt;  &lt;records&gt;&lt;br /&gt;    &lt;entry id='17962'&gt;&lt;date&gt;Wed Dec 12 20:00:33 +0000 2007&lt;/date&gt;&lt;trip&gt;trip to the supermarket&lt;/trip&gt;&lt;miles&gt;11.8&lt;/miles&gt;&lt;description&gt;my first trip with the GPS installed&lt;/description&gt;&lt;/entry&gt;&lt;br /&gt;    &lt;entry id='18024'&gt;&lt;date&gt;Fri Dec 14 11:45:09 +0000 2007&lt;/date&gt;&lt;trip&gt;driving around the park&lt;/trip&gt;&lt;miles&gt;26.1&lt;/miles&gt;&lt;description/&gt;&lt;/entry&gt;&lt;br /&gt;    &lt;entry id='18173'&gt;&lt;date&gt;Thu Dec 20 01:21:27 +0000 2007&lt;/date&gt;&lt;trip&gt;trip to the supermarket&lt;/trip&gt;&lt;miles&gt; 8.5&lt;/miles&gt;&lt;description/&gt;&lt;/entry&gt;&lt;br /&gt;    &lt;entry id='18174'&gt;&lt;date&gt;Thu Dec 20 01:34:26 +0000 2007&lt;/date&gt;&lt;trip&gt;visiting parents&lt;/trip&gt;&lt;miles&gt;12.7&lt;/miles&gt;&lt;description/&gt;&lt;br /&gt;    &lt;/entry&gt;&lt;br /&gt;  &lt;/records&gt;&lt;br /&gt;&lt;/car_log&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Total miles this month: 59.1&lt;br /&gt;...&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 12 Jan 2008 11:41:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4976</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>create and call an oracle function</title>
      <link>http://snippets.dzone.com/posts/show/4109</link>
      <description>&lt;code&gt;&lt;br /&gt;create or replace function "ORACLE_FOO"&lt;br /&gt;(foo_arg1 in NUMBER)&lt;br /&gt;return NUMBER&lt;br /&gt;is&lt;br /&gt;rt NUMBER := 0;&lt;br /&gt;begin&lt;br /&gt;  IF foo_arg1 &gt; 1 THEN&lt;br /&gt;    SELECT 1 INTO rt;&lt;br /&gt;  ELSE&lt;br /&gt;    SELECT 2 INTO rt;&lt;br /&gt;  END IF;&lt;br /&gt;  RETURN (rt);&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;SELECT ORACLE_FOO(1) FROM dual;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 06 Jun 2007 21:22:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4109</guid>
      <author>davetrane (David Davis)</author>
    </item>
    <item>
      <title>Validate email and domain</title>
      <link>http://snippets.dzone.com/posts/show/4022</link>
      <description>// Vlidate email and domain name&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;function validate_email($email){&lt;br /&gt;&lt;br /&gt;$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";&lt;br /&gt;&lt;br /&gt;if(eregi($exp,$email)){&lt;br /&gt;	if(checkdnsrr(array_pop(explode("@",$email)),"MX")){&lt;br /&gt;		print("$email is ok.&lt;br&gt;");&lt;br /&gt;			}else{&lt;br /&gt;			print("$email is ok. But domain is not.&lt;br&gt;");&lt;br /&gt;			}&lt;br /&gt;				}else{&lt;br /&gt;				print("$email is not ok.&lt;br&gt;");&lt;br /&gt;				}   &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;validate_email("shantanu.ok");&lt;br /&gt;validate_email("shantanu.ok@gmail.com");&lt;br /&gt;validate_email("shantanu.ok@fsjaldkfjlsfjsljflsfjsldk.com");&lt;br /&gt;&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 15 May 2007 07:48:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4022</guid>
      <author>shantanuo (shantanu oak)</author>
    </item>
    <item>
      <title>PHP Copyright Updater</title>
      <link>http://snippets.dzone.com/posts/show/3652</link>
      <description>// PHP Copyright Updater&lt;br /&gt;// by Evan Walsh&lt;br /&gt;// NothingConcept.com&lt;br /&gt;&lt;br /&gt;This code will automatically change the copyright on your site as the year changes.  Tested and approved by me.&lt;br /&gt;&lt;br /&gt;Just call this in one of your PHP files by including the file with this code in it:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;//Licensed under the GPL v2&lt;br /&gt;//by Evan Walsh of nothingconcept.com&lt;br /&gt;function copyright($site,$year) {&lt;br /&gt;    $current = date(Y);&lt;br /&gt;    if($year == $current) { $eyear = $year; }&lt;br /&gt;    else { $eyear = "$year - $current"; }&lt;br /&gt;    echo "All content &amp;copy; $eyear $site";&lt;br /&gt;}&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example: &lt;?php include('functions.php'); ?&gt;&lt;br /&gt;&lt;br /&gt;Then place &lt;?php copyright("Sitename","2007"); ?&gt; or something similar in the place you want the copyright to display.&lt;br /&gt;&lt;br /&gt;Simple as that.</description>
      <pubDate>Fri, 09 Mar 2007 22:41:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3652</guid>
      <author>GreyFox (Evan Walsh)</author>
    </item>
    <item>
      <title>Particle Swarm Optimization</title>
      <link>http://snippets.dzone.com/posts/show/3327</link>
      <description>Particle Swarm Optimization written in Python, more infos and a pretty printer here : &lt;a href="http://www.biais.org/blog/index.php/2007/01/14/13-metaheuristic-particle-swarm-optimization-pso-in-python"&gt;http://www.biais.org/blog/index.php/2007/01/14/13-metaheuristic-particle-swarm-optimization-pso-in-python&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Particle swarm optimization&lt;br /&gt;# Maxime Biais : &lt;http://www.biais.org/blog/&gt;&lt;br /&gt;&lt;br /&gt;from random import uniform&lt;br /&gt; &lt;br /&gt;class PSO:&lt;br /&gt;    def __init__(self, pop_size, min, max, phi, phi2, lr, maxiter, func):&lt;br /&gt;        self.func = func&lt;br /&gt;        self.pop = []&lt;br /&gt;        # 0: position, 1: velocity, 2: fitness&lt;br /&gt;        self.min = min&lt;br /&gt;        self.max = max&lt;br /&gt;        for i in xrange(pop_size):&lt;br /&gt;            self.pop.append([uniform(self.min, self.max), &lt;br /&gt;                                   uniform(-1, 1), 0])&lt;br /&gt;        self.evaluate()&lt;br /&gt;        self.gdest = self.pop[0]&lt;br /&gt;        self.pdest = self.pop[0]&lt;br /&gt;        self.phi = phi&lt;br /&gt;        self.phi2 = phi2&lt;br /&gt;        self.lr = lr&lt;br /&gt;        self.maxiter = maxiter&lt;br /&gt;    &lt;br /&gt;    def update_velocity(self):&lt;br /&gt;        for i in self.pop:&lt;br /&gt;            i[1] = self.lr * i[1] + uniform(0, self.phi) \&lt;br /&gt;                    * (self.pdest[0] - i[0]) + uniform(0, self.phi2) \&lt;br /&gt;                    * (self.gdest[0] - i[0])&lt;br /&gt; &lt;br /&gt;    def evaluate(self):&lt;br /&gt;        for i in self.pop:&lt;br /&gt;            i[2] = self.func(i[0])&lt;br /&gt; &lt;br /&gt;    def move(self):&lt;br /&gt;        for i in self.pop:&lt;br /&gt;            i[0] += i[1]&lt;br /&gt; &lt;br /&gt;    def __cmp_by_fitness(self, a, b):&lt;br /&gt;        return cmp(a[2], b[2])&lt;br /&gt;    &lt;br /&gt;    def run(self, update_func=False):&lt;br /&gt;        for i in xrange(self.maxiter):&lt;br /&gt;            if update_func:&lt;br /&gt;                update_func()&lt;br /&gt;            self.update_velocity()&lt;br /&gt;            self.move()&lt;br /&gt;            self.evaluate()&lt;br /&gt;            self.pop.sort(self.__cmp_by_fitness, reverse=0)&lt;br /&gt;            self.pdest = self.pop[0]&lt;br /&gt;            if self.pdest[2] &lt; self.gdest[2]:&lt;br /&gt;                self.gdest = self.pdest&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 19 Jan 2007 00:14:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3327</guid>
      <author>bugmenot (BugMeNot)</author>
    </item>
    <item>
      <title>PHP: Dynamically call a function from list of allowed actions</title>
      <link>http://snippets.dzone.com/posts/show/3234</link>
      <description>This function simply calls another function based on the argument, assuming that argument ($action) is in the "allowed" list of actions.&lt;br /&gt;&lt;br /&gt;settings-config.php&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$settings['actions'] = "list,view,delete,update";&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Catalog-class.php&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public function process_action($action) {&lt;br /&gt;	global $settings;&lt;br /&gt;	&lt;br /&gt;	$allowed_actions = explode(",",$settings['actions']);&lt;br /&gt;	if (is_numeric(array_search($action, $allowed_actions))) {&lt;br /&gt;		$f_name = "product_" . $action;&lt;br /&gt;		$this-&gt;$f_name();&lt;br /&gt;	} else {&lt;br /&gt;		$this-&gt;error_msg = 'ACTION_NOT_ALLOWED';&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;private function product_list() {&lt;br /&gt;}&lt;br /&gt;private function product_view() {&lt;br /&gt;}&lt;br /&gt;private function product_delete() {&lt;br /&gt;}&lt;br /&gt;private function product_update() {&lt;br /&gt;}&lt;br /&gt;private function product_create() {&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Even if product_create() exists, if you do process_action('create'), it will not work... it's basically like a function wrapper.. or something like that...&lt;br /&gt;&lt;br /&gt;UPDATE: Yes I know I could have created an array like &lt;code&gt;$settions['actions'] = array('list','view','delete','update');&lt;/code&gt; but I've modified this code a bit for snippet/display purposes... anyways this is just a reminder for myself, not of actual use or interest for others... comments are welcome though :D&lt;br /&gt;</description>
      <pubDate>Wed, 03 Jan 2007 12:15:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3234</guid>
      <author>alexwilliams (Alex Williams)</author>
    </item>
    <item>
      <title>PyS60 - DaemonS60</title>
      <link>http://snippets.dzone.com/posts/show/3178</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import appuifw&lt;br /&gt;import e32&lt;br /&gt;import thread&lt;br /&gt;&lt;br /&gt;lock = e32.Ao_lock()&lt;br /&gt;lockTHR = thread.allocate_lock()&lt;br /&gt;&lt;br /&gt;###### Sostituisci questa funzione con quello che vuoi fare eseguire&lt;br /&gt;i = 0&lt;br /&gt;def funzDaemon():&lt;br /&gt;	global i&lt;br /&gt;	while(1):&lt;br /&gt;		lockTHR.acquire()&lt;br /&gt;		e32.ao_sleep(1)&lt;br /&gt;		open('E:\\Others\\tmp.txt', 'a').write(str(i)+"\n")&lt;br /&gt;		i+=1&lt;br /&gt;		lockTHR.release()&lt;br /&gt;####################################################################&lt;br /&gt;&lt;br /&gt;appuifw.app.title = u'DaemonS60'&lt;br /&gt;appuifw.app.exit_key_handler = lambda:lock.signal()&lt;br /&gt;&lt;br /&gt;print 'DaemonS60 - Start'&lt;br /&gt;&lt;br /&gt;thread.start_new_thread(funzDaemon, ())&lt;br /&gt;&lt;br /&gt;lock.wait()&lt;br /&gt;&lt;br /&gt;print 'DaemonS60 - Stop'&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 19 Dec 2006 23:56:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3178</guid>
      <author>whitetiger ()</author>
    </item>
    <item>
      <title>Just #@$% do it!</title>
      <link>http://snippets.dzone.com/posts/show/3076</link>
      <description>If you have an unreliable network feed and want to run something like rsync, scp, etc. without having to babysit it a simple function can be used to prefix a command.  The function looks like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Function to force a command to try until it works.&lt;br /&gt;# Name means "JUST #@$% DO IT!"&lt;br /&gt;JFDI () {&lt;br /&gt;  COMMAND=$*&lt;br /&gt;  while ! $COMMAND ; do echo "Retrying..." ; done&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Using it is simplicity itself:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# command that can fail and annoy&lt;br /&gt;rsync -avz /my/local/directory/ myuser@host:~/my/remote/directory/&lt;br /&gt;&lt;br /&gt;# command that won't give up ever&lt;br /&gt;JFDI rsync -avz /my/local/directory/ myuser@host:~/my/remote/directory/&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 04 Dec 2006 17:32:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3076</guid>
      <author>ttmrichter (Michael T. Richter)</author>
    </item>
    <item>
      <title>Functions, that returns more</title>
      <link>http://snippets.dzone.com/posts/show/3039</link>
      <description>// Self explaining.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function return_more() {&lt;br /&gt;	return array('first', 'second');&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;list($ret1, $ret2) = return_more();&lt;br /&gt;&lt;br /&gt;echo $ret1.'&lt;br&gt;';&lt;br /&gt;echo $ret2.'&lt;br&gt;';&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 26 Nov 2006 17:16:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3039</guid>
      <author>warona (ak)</author>
    </item>
  </channel>
</rss>
