<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Bugmenot's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 23:35:45 GMT</pubDate>
    <description>DZone Snippets: Bugmenot's Code Snippets</description>
    <item>
      <title>Allow saving of nested objects</title>
      <link>http://snippets.dzone.com/posts/show/3333</link>
      <description>Small extention to allow save data from form with nested data from inputs like "organization[place_data][address]"&lt;br /&gt;&lt;br /&gt;Inspired by &lt;a href="http://weblog.jamisbuck.org/2007/1/11/moving-associated-creations-to-the-model"&gt;http://weblog.jamisbuck.org/2007/1/11/moving-associated-creations-to-the-model&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ActiveRecord::Base::ClassMethods.class_eval do&lt;br /&gt;  # Allows set data it nested way from form for has_one and belongs_to associations&lt;br /&gt;  # add &lt;tt&gt;association_data=&lt;/tt&gt; method that asset Hash&lt;br /&gt;  # Example: &lt;tt&gt;allow_data_update_for :place&lt;/tt&gt; to allow saving input fields with names like "organization[place_data][address]"&lt;br /&gt;  # Inspired by http://weblog.jamisbuck.org/2007/1/11/moving-associated-creations-to-the-model&lt;br /&gt;  def allow_data_update_for(association)&lt;br /&gt;    define_method "#{association}_data=" do |data|&lt;br /&gt;      if self.send(association)&lt;br /&gt;        self.send(association).attributes = data&lt;br /&gt;        instance_eval "@update_#{association}_on_save = true"&lt;br /&gt;      else&lt;br /&gt;        self.send("build_#{association}", data)&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    before_update do |obj|&lt;br /&gt;      obj.send(association).save if obj.instance_variable_get("@update_#{association}_on_save")&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 19 Jan 2007 16:49:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3333</guid>
      <author>bugmenot (BugMeNot)</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>test</title>
      <link>http://snippets.dzone.com/posts/show/3103</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?&lt;br /&gt;	if (in_array($r["type"], Array(1, print str_replace("; ;","",(get_part_of_text($desc, 300)));&lt;br /&gt;	else { $sql-&gt;query("SELECT of.phone FROM offers o, offices of&lt;br /&gt;			WHERE o.office_id=of.id AND o.id=".$r["id"]);&lt;br /&gt;		list($office_phone) = $sql-&gt;getrow(&lt;br /&gt;print (str_replace("; ;","",(get_part_of_text($desc,300))))."&lt;br/&gt;&lt;b&gt;Contacts&lt;/b&gt;:".$office_phone;&lt;br /&gt;	}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 07 Dec 2006 17:47:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3103</guid>
      <author>bugmenot (BugMeNot)</author>
    </item>
    <item>
      <title>Latex2wiki</title>
      <link>http://snippets.dzone.com/posts/show/2847</link>
      <description>Translate a subset of LaTeX into MoinMoin wiki syntax.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;&lt;br /&gt;#    Copyright (C) 2003, Maxime Biais &lt;maxime@biais.org&gt;&lt;br /&gt;#&lt;br /&gt;#    This program is free software; you can redistribute it and/or modify&lt;br /&gt;#    it under the terms of the GNU General Public License as published by&lt;br /&gt;#    the Free Software Foundation; either version 2 of the License, or&lt;br /&gt;#    (at your option) any later version.&lt;br /&gt;#&lt;br /&gt;#    This program is distributed in the hope that it will be useful,&lt;br /&gt;#    but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the&lt;br /&gt;#    GNU General Public License for more details.&lt;br /&gt;#&lt;br /&gt;#    You should have received a copy of the GNU General Public License&lt;br /&gt;#    along with this program; if not, write to the Free Software&lt;br /&gt;#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA&lt;br /&gt;#&lt;br /&gt;# $Id: latex2wiki.py,v 1.1.1.1 2004/03/14 18:31:50 max Exp $&lt;br /&gt;&lt;br /&gt;import sys, re&lt;br /&gt;&lt;br /&gt;def dummy(d):&lt;br /&gt;    pass&lt;br /&gt;&lt;br /&gt;NONE = "__@NONE@__"&lt;br /&gt;&lt;br /&gt;tr_list = [&lt;br /&gt;    (r"\\includegraphics.*{(.*)\.eps}", "attachment::%s.png", dummy),&lt;br /&gt;    (r"\\caption{.*}", "", dummy),&lt;br /&gt;    (r"\\label{.*}", "", dummy),&lt;br /&gt;    (r"(.*)\\emph{(.*)}(.*)", """%s'''%s'''%s""", dummy),&lt;br /&gt;    (r"\\item (.*)", " * %s", dummy),&lt;br /&gt;    (r"\\begin{.*}", "", dummy),&lt;br /&gt;    (r"\\end{.*}", "", dummy),&lt;br /&gt;    (r"(.*)``(.*)''(.*)", "%s\"%s\"%s", dummy),&lt;br /&gt;    (r"\\chapter{(.*)}", NONE, dummy),&lt;br /&gt;    (r"\\paragraph{(.*)}", "==== %s ====", dummy),&lt;br /&gt;    (r"\\subsubsection{(.*)}", "==== %s ====", dummy),&lt;br /&gt;    (r"\\subsection{(.*)}", "=== %s ===", dummy),&lt;br /&gt;    (r"\\section{(.*)}", "== %s ==", dummy),&lt;br /&gt;    (r"(.*)\\fig{.*}(.*)", "%s suivant %s", dummy)&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;in_stream  = open(sys.argv[1], "r")&lt;br /&gt;if len(sys.argv) &lt; 3:&lt;br /&gt;    out_stream = sys.stdout&lt;br /&gt;else:&lt;br /&gt;    out_stream = open(sys.argv[2], "w")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;for i in in_stream.readlines():&lt;br /&gt;    cur_write = 0&lt;br /&gt;    for reg in tr_list:&lt;br /&gt;        m = re.search(reg[0], i)&lt;br /&gt;        if m:&lt;br /&gt;            reg[2](i)&lt;br /&gt;            cur_write = 1&lt;br /&gt;            if reg[1] == NONE:&lt;br /&gt;                break&lt;br /&gt;            print &gt;&gt; out_stream, reg[1] % m.groups()&lt;br /&gt;            break&lt;br /&gt;    if not cur_write:&lt;br /&gt;        out_stream.write(i)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 17 Oct 2006 12:10:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2847</guid>
      <author>bugmenot (BugMeNot)</author>
    </item>
    <item>
      <title>Mirkwood Mana Gear</title>
      <link>http://snippets.dzone.com/posts/show/2378</link>
      <description>Item	Location	Mana	Level	Weight	Area	Mob	Other&lt;br /&gt;Bic Lighter	01 &#8211; Light	30	50	2	Rocky Horror		Hit +2&lt;br /&gt;Orb Of Higher Thought	01 &#8211; Light	30	60	5	Ethereal		Hit -1, Dam -2&lt;br /&gt;Human Skull	02 &#8211; Head	60	75	20	Labyrinth		&lt;br /&gt;Gold Crown	02 &#8211; Head	40	68	30	Dungeons of the Empire		Wis/Int +2, Dex -2&lt;br /&gt;Big Red Face	02 &#8211; Head	35	45	7	Ultima		HP +20&lt;br /&gt;Green Mane of Demon Hair	02 &#8211; Head	30	83	5	Mordor		&lt;br /&gt;Helm of the Overlord	02 &#8211; Head	25	90	15	Tarius		HP + 30&lt;br /&gt;Crown of Lord British	02 &#8211; Head	25	58	10	Ultima		Wis/Int +2&lt;br /&gt;Glowing Red Stone	02 &#8211; Head	22	40	9	Kull's		Int/Str +1&lt;br /&gt;A Pair of Cool Shades	02 &#8211; Head	15	75	5	Restaurant		Int/Hit +1&lt;br /&gt;A Beanie	02 &#8211; Head	15	42	10	Wonderland		HP +48, wis -2, int -1&lt;br /&gt;Helmet of the Sentri	02 &#8211; Head	15	33	30	Ultima	Sentri	Wis +2&lt;br /&gt;Elven Chainmail Coif	02 &#8211; Head	10	30	10	Lothlorian	Legolas	Wis +2&lt;br /&gt;Elf Wrought Mithril Coif	02 &#8211; Head	5	25	10	Lothlorian	Rumil	Dex +1&lt;br /&gt;Amulet of Ra	03 &#8211; Neck	50	57	1	Olympus	Ra	&lt;br /&gt;Priests's Color	03 &#8211; Neck	40	60	20	Abbey	Abbott	&lt;br /&gt;Sun Blazoned Ankh	03 &#8211; Neck	30	25	10	4th Tower		&lt;br /&gt;Evil Charm	03 &#8211; Neck	25	36	3	Ultima		Int +1&lt;br /&gt;Radient Necklace	03 &#8211; Neck	25	54	2	Olympus	Athena	&lt;br /&gt;Amulet of Laurelin	03 &#8211; Neck	25	74	4	Myrillin		Will -5&lt;br /&gt;Cold Steel Ankh	03 &#8211; Neck	18	20	5	4th Tower		&lt;br /&gt;Holy Symbol	03 &#8211; Neck	15	21	2	Dragon Tower		&lt;br /&gt;Small Diamond Brooch	03 &#8211; Neck	15	19	2	Dragon Tower		&lt;br /&gt;Officer's Brooch	03 &#8211; Neck	10	31	3	Dragon Tower		HP +10&lt;br /&gt;Magic Cloak	03 &#8211; Neck	10	33	6	Ultima		HP +10&lt;br /&gt;Golden Holy Symbol	03 &#8211; Neck	10	27	3	Mahn-Tor		&lt;br /&gt;Niphrodal Shaped Amulet	03 &#8211; Neck	10	29	5	:Lothlorien	Elven Cleric	Wis +2&lt;br /&gt;Fellowship Medallion	03 &#8211; Neck	10	34	6	Ultima		Wis -1&lt;br /&gt;Bright Orange Robe	03 &#8211; Neck	10	40	15	Restaurant		&lt;br /&gt;Dark Blue Coak	03 &#8211; Neck	5	18	2	Wyvern		AC -6&lt;br /&gt;Robe of the Magi	04 &#8211; About Body	50	54	10		Magnus	Will -5&lt;br /&gt;Wispy Cloak	04 &#8211; About Body	50	76	30	Labyrinth		Wis +2&lt;br /&gt;Cloak of Fire	04 &#8211; About Body	50	80	3	Mordor		&lt;br /&gt;Aura of Wisdom	04 &#8211; About Body	20	42	3	Myrillin	Obwan	&lt;br /&gt;Linen Robe	04 &#8211; About Body	5	3	5	Dwarven Daycare		Wis +1&lt;br /&gt;Sequined Jacket	05 &#8211; On Body	50	57	15	Restaurant		&lt;br /&gt;Delion's Leather Jacket	05 &#8211; On Body	45	69	5	Museum		&lt;br /&gt;Cloak of Many Colors	05 &#8211; On Body	45	65	13	Isengard		&lt;br /&gt;Hair Shirt	05 &#8211; On Body	40	40	80	Abbey	Monk	Wis +2&lt;br /&gt;Elven Chainmail Shirt	05 &#8211; On Body	30	83	3	Helm's Deep		&lt;br /&gt;Waterfall Cloak	05 &#8211; On Body	25	27	6	Elemental Canyon	Water Ruler	Hit -2&lt;br /&gt;Black Turtleneck	05 &#8211; On Body	25	50	20	Museum		Dex +1&lt;br /&gt;Elf Wrought Mithril Jerkin	05 &#8211; On Body	5	26	10	Lothlorian	Rumil	Dex +1&lt;br /&gt;Black Sleeves	06 &#8211; Arms	40	90	15	Mordor		&lt;br /&gt;Elf Wrought Mithril Armbands	06 &#8211; Arms	5	26	10	Lothlorian	Rumil	Hit +1&lt;br /&gt;Clear Plastic Bracelet	07 &#8211; Wrist	40	70	5	Mos Eisley	Retired Jedi Knight	&lt;br /&gt;Dalis Melting Watch	07 &#8211; Wrist	35	70	10	MOMA		HP +25, Int +1&lt;br /&gt;Mom Tattoo	07 &#8211; Wrist	30	55	5	Ocean		&lt;br /&gt;Ruby Bracer	07 &#8211; Wrist	30	62	10	Mordor		&lt;br /&gt;Steel Bracer	07 &#8211; Wrist	5	80	20	Solace	Lieutenant	&lt;br /&gt;Shield of the Lesser Maiar	08 &#8211; Shield	35	81	10	Mordor		Int +2&lt;br /&gt;Shield of Defense	08 &#8211; Shield	10	11	15	Dwarven Catacombs		HP +10, Dam -1, Hit -2&lt;br /&gt;Elf Wrought Mithril Shield	08 &#8211; Shield	5	26	20	Lothlorian	Rumil	AC -3&lt;br /&gt;Mithril Armored Gauntlets	09 &#8211; Hands	5	23	5	Lothlorian	Haldir	Con +1&lt;br /&gt;Ring of Myril	10 &#8211; Finger	40	58	3	Myrillin	Farin	Wis +1&lt;br /&gt;Dwarven Ring of Power	10 &#8211; Finger	40	85	3	Mordor		Int +1, Dex +2&lt;br /&gt;Ring of Lloth	10 &#8211; Finger	40	20	10	Drow		Str +1, Perm Blind&lt;br /&gt;Ghostly Ring	10 &#8211; Finger	40	20	10	Undead Forum		Int +1&lt;br /&gt;Ring of Evil	10 &#8211; Finger	40	70	5	Tarius		Hit/Dam +2, Dex +1&lt;br /&gt;A Banded Ring	10 &#8211; Finger	30	28	2	Mahn-Tor		&lt;br /&gt;Ring of Destiny	10 &#8211; Finger	25	24	3	Galaxy		Int +2&lt;br /&gt;Small Emerald Ring	10 &#8211; Finger	20	48	5	High Tower of Sorcery	Grand Mistress	&lt;br /&gt;Ring of the Universe	10 &#8211; Finger	10	28	3	Galaxy		HP +30&lt;br /&gt;Gold Ring	10 &#8211; Finger	10	25	1	Valley of the Elves		&lt;br /&gt;Mechanic's Utility Belt	11 &#8211; Waist	50	70	10	Mos Eisley		Dam +2&lt;br /&gt;Elf Wrought Mithril Girth	11 &#8211; Waist	5	25	10	Lothlorian	Rumil	Dam +1&lt;br /&gt;Wispy Greaves	12 &#8211; Legs	30	76	30	Labyrinth		Wis/Int +2&lt;br /&gt;Fishnet Stockings	12 &#8211; Legs	30	75	4	Rocky Horror		Dex +1&lt;br /&gt;Some Elven Chainmail Graves	12 &#8211; Legs	20	83	3	Helm's Deep		&lt;br /&gt;Elf Wrought Mithril Skirt	12 &#8211; Legs	5	23	10	Lothlorian	Haldir	Wis +1&lt;br /&gt;Ruby Slippers	13 &#8211; Feet	30	40	4	Emerald City	Toto	Anti Dwarf/Treant/Giant/Orc/Male&lt;br /&gt;Mithril Armored Boots	13 &#8211; Feet	5	23	10	Lothlorian	Haldir	Wis +1&lt;br /&gt;Palantir	14 &#8211; Held	60	60	8	Isengard	Saruman	Wis/Int +3&lt;br /&gt;Note From The Author	14 &#8211; Held	50	75	1	Labyrinth		Hit/Dam +7, HP +50&lt;br /&gt;Eyeglass	14 &#8211; Held	35	65	1	Ocean		HP +25&lt;br /&gt;Rags from a Shredded Robe 	14 &#8211; Held	32	40	3	Jurassic Park	Raptor Nest	&lt;br /&gt;Bad Motivator	14 &#8211; Held	30	75	0	Mos Eisley		Hit/Dam +4, HP +30&lt;br /&gt;</description>
      <pubDate>Tue, 08 Aug 2006 12:23:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2378</guid>
      <author>bugmenot (BugMeNot)</author>
    </item>
  </channel>
</rss>
