<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Guillermo Cruz</title>
	<atom:link href="http://wuilly.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://wuilly.com</link>
	<description></description>
	<lastBuildDate>Sun, 17 Feb 2013 06:44:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>Running Standalone SQLAlchemy Scripts in Pyramid</title>
		<link>http://wuilly.com/running-standalone-sqlalchemy-scripts-in-pyramid/</link>
		<comments>http://wuilly.com/running-standalone-sqlalchemy-scripts-in-pyramid/#comments</comments>
		<pubDate>Sun, 17 Feb 2013 05:34:12 +0000</pubDate>
		<dc:creator>webjunkie01</dc:creator>
				<category><![CDATA[Cron]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[Paste]]></category>
		<category><![CDATA[Pyramid]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SQLAlchemy]]></category>

		<guid isPermaLink="false">http://wuilly.com/?p=144</guid>
		<description><![CDATA[From time to time there comes the need to run automated scripts managed by either Python or Cron. In a recent project I had to run a standalone script that checks records on a database at certain hours. So here&#8217;s what I came up with. Most of the script is based on this post by [...]]]></description>
			<content:encoded><![CDATA[<p>From time to time there comes the need to run automated scripts managed by either Python or Cron. In a recent project I had to run a standalone script that checks records on a database at certain hours. So here&#8217;s what I came up with. Most of the script is based on <a title="this post" href="http://cysec.org/content/using-pyramid-sqlalchemy-models-outside-pyramid" target="_blank">this post</a> by Daniel Mayer.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> paste.<span style="color: black;">deploy</span> <span style="color: #ff7700;font-weight:bold;">import</span> appconfig
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy <span style="color: #ff7700;font-weight:bold;">import</span> engine_from_config
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy.<span style="color: black;">orm</span>.<span style="color: black;">exc</span> <span style="color: #ff7700;font-weight:bold;">import</span> NoResultFound, MultipleResultsFound
&nbsp;
<span style="color: #808080; font-style: italic;">#here is where sqlalchemy objects are imported</span>
<span style="color: #ff7700;font-weight:bold;">from</span> PRJ.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> DBSession <span style="color: #ff7700;font-weight:bold;">as</span> db,DBRecords
&nbsp;
<span style="color: #808080; font-style: italic;">#import the session manager. This way commits will be handled automatically by the Zope Transaction Manager</span>
<span style="color: #ff7700;font-weight:bold;">import</span> transaction
&nbsp;
<span style="color: #808080; font-style: italic;"># Load Application Configuration and Return as Dictionary</span>
conf = appconfig<span style="color: black;">&#40;</span><span style="color: #483d8b;">'config:'</span> + <span style="color: #483d8b;">'development.ini'</span>, 
        relative_to=<span style="color: #483d8b;">&quot;/var/www/PRJ&quot;</span>,
        name=<span style="color: #483d8b;">&quot;main&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Bind Engine Based on Config</span>
engine = engine_from_config<span style="color: black;">&#40;</span>conf, <span style="color: #483d8b;">'sqlalchemy.'</span><span style="color: black;">&#41;</span>
db.<span style="color: black;">configure</span><span style="color: black;">&#40;</span>bind=engine<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Query the DB</span>
data = db.<span style="color: black;">query</span><span style="color: black;">&#40;</span>DBRecords<span style="color: black;">&#41;</span>.<span style="color: black;">one</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">with</span> transaction.<span style="color: black;">manager</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> record <span style="color: #ff7700;font-weight:bold;">in</span> data:
    <span style="color: #808080; font-style: italic;">#query or update DB</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://wuilly.com/running-standalone-sqlalchemy-scripts-in-pyramid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook FQL Request Error</title>
		<link>http://wuilly.com/facebook-fql-request-error/</link>
		<comments>http://wuilly.com/facebook-fql-request-error/#comments</comments>
		<pubDate>Mon, 14 Jan 2013 03:00:23 +0000</pubDate>
		<dc:creator>webjunkie01</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Pylons]]></category>

		<guid isPermaLink="false">http://wuilly.com/?p=128</guid>
		<description><![CDATA[Today while attempting to query Events data through FQL, I received the following error: Impersonated access tokens can only be used with the Graph API. It was a strange error since I had already created my access token with the right scope. To query the data I&#8217;m using the pythonforfacebook SDK which uses https://api.facebook.com/method/fql.query as [...]]]></description>
			<content:encoded><![CDATA[<p>Today while attempting to query Events data through FQL, I received the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Impersonated access tokens can only be used with the Graph API.</pre></div></div>

<p>It was a strange error since I had already created my access token with the right scope. To query the data I&#8217;m using the <a href="https://github.com/pythonforfacebook/facebook-sdk/" title="pythonforfacebook">pythonforfacebook SDK</a> which uses  <a href="https://api.facebook.com/method/fql.query" title="https://api.facebook.com/method/fql.query">https://api.facebook.com/method/fql.query</a> as the url for FQL requests (REST API).</p>
<p>I then tried the graph method (https://graph.facebook.com/fql) which worked perfectly, but the response didn&#8217;t include Event pictures and I really need pictures to be present on my events listings. So what worked for me  was to make a GET request thought the Graph API instead of a POST. </p>
<p>I use <a href="http://docs.python-requests.org/en/latest/" title="Requests">Requests</a> for this so the code is very simple.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">data = db.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Model<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#retrieve my access_token from DB</span>
payload = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'q'</span> : query, <span style="color: #483d8b;">'access_token'</span>: data.<span style="color: black;">access_token</span>,<span style="color: #483d8b;">'format'</span> : <span style="color: #483d8b;">'json'</span><span style="color: black;">&#125;</span>
res = requests.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'https://graph.facebook.com/fql'</span>, params=payload<span style="color: black;">&#41;</span></pre></div></div>

<p>With this change everything worked as expected.</p>
]]></content:encoded>
			<wfw:commentRss>http://wuilly.com/facebook-fql-request-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simpleform Localization in Pyramid</title>
		<link>http://wuilly.com/simpleform-localization-in-pyramid/</link>
		<comments>http://wuilly.com/simpleform-localization-in-pyramid/#comments</comments>
		<pubDate>Mon, 11 Jun 2012 17:56:02 +0000</pubDate>
		<dc:creator>webjunkie01</dc:creator>
				<category><![CDATA[Pyramid]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://wuilly.com/?p=84</guid>
		<description><![CDATA[In a recent project I had to localize the errors thrown by the pyramid_simpleform package. Googling for information I couldn&#8217;t find how to do it, so here&#8217;s what worked for me at the end. from pyramid.i18n import get_locale_name from pyramid_simpleform import Form,State &#160; from formencode import api as formencode_api &#160; def includeme&#40;config&#41;: config.scan&#40;__name__&#41; config.add_route&#40;'login', '/login'&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent project I had to localize the errors thrown by the <a href="http://packages.python.org/pyramid_simpleform/">pyramid_simpleform</a> package. Googling for information I couldn&#8217;t find how to do it, so here&#8217;s what worked for me at the end.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">  <span style="color: #ff7700;font-weight:bold;">from</span> pyramid.<span style="color: black;">i18n</span> <span style="color: #ff7700;font-weight:bold;">import</span> get_locale_name
  <span style="color: #ff7700;font-weight:bold;">from</span> pyramid_simpleform <span style="color: #ff7700;font-weight:bold;">import</span> Form,State
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">from</span> formencode <span style="color: #ff7700;font-weight:bold;">import</span> api <span style="color: #ff7700;font-weight:bold;">as</span> formencode_api
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> includeme<span style="color: black;">&#40;</span>config<span style="color: black;">&#41;</span>:
    config.<span style="color: black;">scan</span><span style="color: black;">&#40;</span>__name__<span style="color: black;">&#41;</span>
    config.<span style="color: black;">add_route</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'login'</span>, <span style="color: #483d8b;">'/login'</span><span style="color: black;">&#41;</span>
&nbsp;
  @view_config<span style="color: black;">&#40;</span>route_name=<span style="color: #483d8b;">'login'</span>,renderer=<span style="color: #483d8b;">'website/login.mak'</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">def</span> login<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
     <span style="color: #483d8b;">&quot;&quot;&quot;
     set the language in FormEncode according to the request url param _LOCALE_
     &quot;&quot;&quot;</span>
     formencode_api.<span style="color: black;">set_stdtranslation</span><span style="color: black;">&#40;</span>languages=<span style="color: black;">&#91;</span>get_locale_name<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
     form = Form<span style="color: black;">&#40;</span>request,
           defaults=<span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>request.<span style="color: black;">params</span><span style="color: black;">&#41;</span>,
           schema=MySchema,
           state=State<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
     <span style="color: black;">&#41;</span>
     <span style="color: #483d8b;">&quot;&quot;&quot;
     set an empty gettext translation function,
     since FormEncode has one already
     configured in the set_stdtranslation function
     &quot;&quot;&quot;</span>
     form.<span style="color: black;">state</span>._ = <span style="color: #483d8b;">''</span>
     <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>renderer=FormRenderer<span style="color: black;">&#40;</span>form<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>And that&#8217;s it, try it for example http://mysite.com/login?_LOCALE_=fr. Make sure the action param in your form passes the _LOCALE_ value if the method is set to post.</p>
]]></content:encoded>
			<wfw:commentRss>http://wuilly.com/simpleform-localization-in-pyramid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manage cron jobs with python-crontab</title>
		<link>http://wuilly.com/manage-cron-jobs-with-python-crontab/</link>
		<comments>http://wuilly.com/manage-cron-jobs-with-python-crontab/#comments</comments>
		<pubDate>Wed, 16 May 2012 00:26:04 +0000</pubDate>
		<dc:creator>webjunkie01</dc:creator>
				<category><![CDATA[Cron]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Postgres]]></category>
		<category><![CDATA[Pylons]]></category>
		<category><![CDATA[SDK]]></category>

		<guid isPermaLink="false">http://wuilly.com/?p=70</guid>
		<description><![CDATA[Cron is the main time based scheduler for any linux based system and is available in almost every distro. And in a recent project I had the task to manage jobs in cron from python. Searching for a good cron manager I came across python-crontab. It makes it really easy to manage jobs directly from [...]]]></description>
			<content:encoded><![CDATA[<p>Cron is the main time based scheduler for any linux based system and is available in almost every distro. And in a recent project I had the task to manage jobs in cron from python. Searching for a good cron manager I came across <a href="http://pypi.python.org/pypi/python-crontab/">python-crontab</a>. It makes it really easy to manage jobs directly from cron, here are some examples:</p>
<p><strong>NOTE:</strong> This examples used version 0.9.6, there&#8217;s a new version available 1.2 on <a href="http://pypi.python.org/pypi?name=python-crontab&#038;:action=display">pypi</a> along with some examples, the main difference is that the API has been changed from slice calls to be properties instead of methods.</p>
<h4>Installing python-crontab is easy as pie. First we install our virtual enviroment:</h4>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">cd /var/www
python virtualenv.py --no-site-packages prj-env
cd prj-env
bin/activate</pre></div></div>

<h4>Then we proceed to install python-crontab</h4>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">pip install python-crontab 
or
easy_install python-crontab</pre></div></div>

<h4>Let&#8217;s schedule a job to be executed everyday at 12pm</h4>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> crontab <span style="color: #ff7700;font-weight:bold;">import</span> CronTab
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;
Here the object can take two parameters one for setting 
the user cron jobs, it defaults to the current user 
executing the script if ommited. The fake_tab parameter 
sets a testing variable. So you can print what could be 
written to the file onscreen instead or writting directly
into the crontab file. 
&quot;&quot;&quot;</span>
tab = CronTab<span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span>=<span style="color: #483d8b;">'www'</span>,fake_tab=<span style="color: #483d8b;">'True'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">cmd</span> = <span style="color: #483d8b;">'/var/www/pjr-env/bin/python /var/www/PRJ/job.py'</span>
<span style="color: #808080; font-style: italic;"># You can even set a comment for this command</span>
cron_job = tab.<span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span>, comment=<span style="color: #483d8b;">'This is the main command'</span><span style="color: black;">&#41;</span>
cron_job.<span style="color: black;">minute</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">on</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
cron_job.<span style="color: black;">hour</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">on</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">12</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;">#writes content to crontab</span>
tab.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> tab.<span style="color: black;">render</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>It will print out</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">0 12 * * * /var/www/pjr-env/bin/python /var/www/PRJ/job.py</pre></div></div>

<h4>If we want to schedule a job to be executed every five minutes we could do something like this</h4>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> crontab <span style="color: #ff7700;font-weight:bold;">import</span> CronTab
&nbsp;
tab = CronTab<span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span>=<span style="color: #483d8b;">'www'</span>,fake_tab=<span style="color: #483d8b;">'True'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">cmd</span> = <span style="color: #483d8b;">'/var/www/pjr-env/bin/python /var/www/PRJ/job.py'</span>
cron_job = tab.<span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span><span style="color: black;">&#41;</span>
cron_job.<span style="color: black;">minute</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">every</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;">#writes content to crontab</span>
tab.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> tab.<span style="color: black;">render</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>It will print out</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">*/5 * * * * /var/www/pjr-env/bin/python /var/www/PRJ/job.py</pre></div></div>

<h4>If we want to schedule a job for a specific range of hours for example only working hours, we could do the following</h4>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> crontab <span style="color: #ff7700;font-weight:bold;">import</span> CronTab
&nbsp;
tab = CronTab<span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span>=<span style="color: #483d8b;">'www'</span>,fake_tab=<span style="color: #483d8b;">'True'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">cmd</span> = <span style="color: #483d8b;">'/var/www/pjr-env/bin/python /var/www/PRJ/job.py'</span>
cron_job = tab.<span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span><span style="color: black;">&#41;</span>
cron_job.<span style="color: black;">minute</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">on</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
cron_job.<span style="color: black;">hour</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">during</span><span style="color: black;">&#40;</span>09,<span style="color: #ff4500;">18</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;">#writes content to crontab</span>
tab.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> tab.<span style="color: black;">render</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>It will print out</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">0 09-18 * * * /var/www/pjr-env/bin/python /var/www/PRJ/job.py</pre></div></div>

<h4>Now to schedule a job to run twice a day at 11 and 16 hrs, we could do the following</h4>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> crontab <span style="color: #ff7700;font-weight:bold;">import</span> CronTab
&nbsp;
tab = CronTab<span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span>=<span style="color: #483d8b;">'www'</span>,fake_tab=<span style="color: #483d8b;">'True'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">cmd</span> = <span style="color: #483d8b;">'/var/www/pjr-env/bin/python /var/www/PRJ/job.py'</span>
cron_job = tab.<span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span><span style="color: black;">&#41;</span>
cron_job.<span style="color: black;">minute</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">on</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
cron_job.<span style="color: black;">hour</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">on</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'11,16'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#writes content to crontab</span>
tab.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> tab.<span style="color: black;">render</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>it will print out</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">0 11,16 * * * /var/www/pjr-env/bin/python /var/www/PRJ/job.py</pre></div></div>

<h4>Let&#8217;s delete the previous command</h4>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> crontab <span style="color: #ff7700;font-weight:bold;">import</span> CronTab
<span style="color: #dc143c;">cmd</span> = <span style="color: #483d8b;">'/var/www/pjr-env/bin/python /var/www/PRJ/job.py'</span>
tab = CronTab<span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span>=<span style="color: #483d8b;">'www'</span>,fake_tab=<span style="color: #483d8b;">'True'</span><span style="color: black;">&#41;</span>
&nbsp;
cron_job = tab.<span style="color: black;">find_command</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>cron_job<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:
    tab.<span style="color: black;">remove_all</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span><span style="color: black;">&#41;</span> 
<span style="color: #808080; font-style: italic;">#writes content to crontab</span>
tab.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>So there you have it, examples that make python-crontab a great python manager for cron jobs. Now I know there are pure Python implementations like <a href="http://stackoverflow.com/questions/373335/suggestions-for-a-cron-like-scheduler-in-python">this</a>, an event scheduler named <a href="http://docs.python.org/library/sched.html">sched</a> and libraries like <a href="http://www.razorvine.net/download/kronos.py">Kronos</a>. But I decided to keep things simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://wuilly.com/manage-cron-jobs-with-python-crontab/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Hey Facebook, your help support center is crap.</title>
		<link>http://wuilly.com/hey-facebook-your-help-support-is-crap/</link>
		<comments>http://wuilly.com/hey-facebook-your-help-support-is-crap/#comments</comments>
		<pubDate>Fri, 30 Mar 2012 16:44:45 +0000</pubDate>
		<dc:creator>webjunkie01</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://wuilly.com/?p=54</guid>
		<description><![CDATA[It&#8217;s been almost a month since a friend of mine had her account &#8220;Temporarily deactivated&#8221;, now here the word temporarily is misleading because she hasn&#8217;t recovered her account yet. The worst part is that Facebook doesn&#8217;t tell her why the account was blocked. After many hours we came to the conclusion that maybe a jealous [...]]]></description>
			<content:encoded><![CDATA[<p>  It&#8217;s been almost a month since a friend of mine had her account &#8220;Temporarily deactivated&#8221;, now here the word temporarily is misleading because she hasn&#8217;t recovered her account yet. The worst part is that Facebook doesn&#8217;t tell her why the account was blocked. After many hours we came to the conclusion that maybe a jealous friend might have reported her account. </p>
<p>  Now can you imagine if she also had a paying subscription to one of those online services that have Facebook based auth logins only?, now she could also be losing money because of this.</p>
<p>  So she tried to have Facebook send her a code to a cellphone but their system didn&#8217;t sent shit. We live in México so maybe our cellphone carrier is not supported, but do Facebook cares to let you know that?, of course not. So after a few tries for a couple of days Facebook now just shows a message which says &#8220;The system is overloaded&#8221;. Overloaded with what!?, fuck this is just so frustrating because that message is the only thing that appears from now on. </p>
<p>  After a long period of time she received an email from Facebook but it didn&#8217;t help at all, they didn&#8217;t give any info on why her account was blocked or anything they just sent her a link to the same page where you tell Facebook to send you a code.</p>
<p>  The worst thing of all this is that Facebook has top notch engineers working for them, why not try to improve the help support center?. Oh no they can&#8217;t they are too busy developing all those innovating features right?.</p>
<p>  The good thing is that now I know how to deactivate my account in just one simple step. Just tell a friend to report your profile and Facebook will not give a fuck and deactivate your account for you without a notice. This way you won&#8217;t have to receive those fucking emails from them trying to convince you to come back and activate your account.</p>
<p>  Facebook please get your shit together. This mechanism of recovering your account is unfriendly, inhuman, it frustrates users and leads to posts like this one. My friend since then has been feeling sad, desperate and depress from all this, it saddens me to see her like this.</p>
]]></content:encoded>
			<wfw:commentRss>http://wuilly.com/hey-facebook-your-help-support-is-crap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setup The Newrelic Monitoring Agent On A Pylons App</title>
		<link>http://wuilly.com/setup-the-newrelic-monitoring-agent-on-a-pylons-app/</link>
		<comments>http://wuilly.com/setup-the-newrelic-monitoring-agent-on-a-pylons-app/#comments</comments>
		<pubDate>Sat, 04 Feb 2012 04:53:31 +0000</pubDate>
		<dc:creator>webjunkie01</dc:creator>
				<category><![CDATA[Pylons]]></category>
		<category><![CDATA[WSGI]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://wuilly.com/?p=32</guid>
		<description><![CDATA[Today I decide to signup for a free trial of the newrelic monitoring agent and I wanted to write on how to setup the agent on a Pylons app. EDIT: Commenter Graham Dumpleton has advised against this setup in the comments. There&#8217;s the potential risk of the agent initializing twice and additional modules not working [...]]]></description>
			<content:encoded><![CDATA[<p>Today I decide to signup for a free trial of the <a href="http://newrelic.com">newrelic monitoring agent</a> and I wanted to write on how to setup the agent on a Pylons app.</p>
<p><strong>EDIT:</strong> Commenter Graham Dumpleton has advised against this setup in the comments. There&#8217;s the potential risk of the agent initializing twice and additional modules not working correctly. So please don&#8217;t use this setup on a production app.</p>
<p>Open the middleware.py file located on PRJNAME/config and add the following. Make sure to set the correct path where your newrelic.ini file is located</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#import the agent</span>
<span style="color: #ff7700;font-weight:bold;">import</span> newrelic.<span style="color: black;">agent</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#create a middleware class to initialize the agent</span>
<span style="color: #ff7700;font-weight:bold;">class</span> NewRelicAgent<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, app<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">app</span> = app
        newrelic.<span style="color: black;">agent</span>.<span style="color: black;">initialize</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/path/to/the/file/newrelic.ini'</span><span style="color: black;">&#41;</span>
&nbsp;
    @newrelic.<span style="color: black;">agent</span>.<span style="color: black;">wsgi_application</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__call__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, environ, start_response<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">app</span><span style="color: black;">&#40;</span>environ, start_response<span style="color: black;">&#41;</span></pre></div></div>

<p>Now it&#8217;s time to add our custom middleware inside the make_app def</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)</span>
app = NewRelicAgent<span style="color: black;">&#40;</span>app<span style="color: black;">&#41;</span></pre></div></div>

<p>And that&#8217;s it. Now it&#8217;s time to collect some stats <img src='http://wuilly.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://wuilly.com/setup-the-newrelic-monitoring-agent-on-a-pylons-app/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Facebook SDK For Python Deprecated</title>
		<link>http://wuilly.com/facebook-sdk-for-python-deprecated/</link>
		<comments>http://wuilly.com/facebook-sdk-for-python-deprecated/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 17:58:29 +0000</pubDate>
		<dc:creator>webjunkie01</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SDK]]></category>
		<category><![CDATA[uWSGI]]></category>

		<guid isPermaLink="false">http://mercadoinmobi.com/?p=8</guid>
		<description><![CDATA[Yesterday Facebook without a notice or warning decided it to delete their SDK github repo for Python. Today the repo is back online but with a notice saying that they have no plans to update it anymore. I&#8217;ve been seeing this coming since they stop adding new features awhile ago. So anyways there&#8217;s an alternative [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday Facebook without a notice or warning decided it to delete their SDK <a href="https://github.com/facebook/python-sdk">github repo for Python</a>. Today the repo is back online but with a notice saying that they have no plans to update it anymore. I&#8217;ve been seeing this coming since they stop adding new features awhile ago. So anyways there&#8217;s an <a href="https://github.com/pythonforfacebook/facebook-sdk">alternative</a> that will help us continue developing applications for Facebook even without their official support.</p>
<p>The move of deleting the repo really took me by surprise. All I have to say is that I really feel frustrated with this company, because they have abandon the Python comunity.</p>
]]></content:encoded>
			<wfw:commentRss>http://wuilly.com/facebook-sdk-for-python-deprecated/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UUID Objects in Pylons with SQLAlchemy</title>
		<link>http://wuilly.com/uuid-objects-in-pylons-with-sqlalchemy/</link>
		<comments>http://wuilly.com/uuid-objects-in-pylons-with-sqlalchemy/#comments</comments>
		<pubDate>Fri, 07 Oct 2011 18:01:21 +0000</pubDate>
		<dc:creator>webjunkie01</dc:creator>
				<category><![CDATA[Postgres]]></category>
		<category><![CDATA[Pylons]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SQLAlchemy]]></category>
		<category><![CDATA[UUID]]></category>
		<category><![CDATA[uWSGI]]></category>

		<guid isPermaLink="false">http://mercadoinmobi.com/?p=10</guid>
		<description><![CDATA[Recently I had to generate and store uuid objects into a Postgres database, but to my surprise SQLAlchemy kept showing the following error: sqlalchemy.exc.ProgrammingError: (ProgrammingError) can't adapt type 'UUID' So the solution was that I had to change my field type from varchar to uuid on my Postgres database, import the psycopg2 extras functions and [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had to generate and store uuid objects into a Postgres database, but to my surprise SQLAlchemy kept showing the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">sqlalchemy.exc.ProgrammingError: (ProgrammingError) can't adapt type 'UUID'</pre></div></div>

<p>So the solution was that I had to change my field type from varchar to  uuid on my Postgres database, import the psycopg2 extras functions and register the uuid type:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> psycopg2.<span style="color: black;">extras</span>
<span style="color: #ff7700;font-weight:bold;">import</span> sqlalchemy
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy <span style="color: #ff7700;font-weight:bold;">import</span> create_engine
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy <span style="color: #ff7700;font-weight:bold;">import</span> MetaData,Column, Table, ForeignKey
&nbsp;
engine = create_engine<span style="color: black;">&#40;</span><span style="color: #483d8b;">'postgresql://user:pass@localhost/db'</span>,echo=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
metadata = MetaData<span style="color: black;">&#40;</span>bind=engine<span style="color: black;">&#41;</span>
hash_key = uuid.<span style="color: black;">uuid4</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
psycopg2.<span style="color: black;">extras</span>.<span style="color: black;">register_uuid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
conn = engine.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
query = conn.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;insert into tbl (uuid_col) values(%(uuid)s&quot;</span>,uuid=hash_key<span style="color: black;">&#41;</span></pre></div></div>

<p>And voila, values were inserted correctly on my database.</p>
]]></content:encoded>
			<wfw:commentRss>http://wuilly.com/uuid-objects-in-pylons-with-sqlalchemy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing a text based captcha in Pylons</title>
		<link>http://wuilly.com/implementing-a-text-based-captcha-in-pylons/</link>
		<comments>http://wuilly.com/implementing-a-text-based-captcha-in-pylons/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 18:03:01 +0000</pubDate>
		<dc:creator>webjunkie01</dc:creator>
				<category><![CDATA[Captcha]]></category>
		<category><![CDATA[Pylons]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[uWSGI]]></category>

		<guid isPermaLink="false">http://mercadoinmobi.com/?p=12</guid>
		<description><![CDATA[One of the must have elements on any html form as an anti-spam measure is a captcha, and the captcha technique that has worked pretty well for me is text based. So here&#8217;s how to implement one. Create a controller and name it captcha ~$ paster controller captcha Add this function to your helpers.py file [...]]]></description>
			<content:encoded><![CDATA[<p>One of the must have elements on any html form as an anti-spam measure is a captcha, and the captcha technique that has worked pretty well for me is text based. So here&#8217;s how to implement one.</p>
<h3>Create a controller and name it captcha</h3>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"> ~$ paster controller captcha</pre></div></div>

<h3>Add this function to your helpers.py file</h3>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> makeCaptcha<span style="color: black;">&#40;</span>lang<span style="color: black;">&#41;</span>:
      label = <span style="color: black;">&#123;</span>
          <span style="color: #ff4500;">1</span> : <span style="color: #483d8b;">'one'</span>,
          <span style="color: #ff4500;">2</span> : <span style="color: #483d8b;">'two'</span>,
          <span style="color: #ff4500;">3</span> : <span style="color: #483d8b;">'three'</span>,
          <span style="color: #ff4500;">4</span> : <span style="color: #483d8b;">'four'</span>,
          <span style="color: #ff4500;">5</span> : <span style="color: #483d8b;">'five'</span>,
          <span style="color: #ff4500;">6</span> : <span style="color: #483d8b;">'six'</span>,
          <span style="color: #ff4500;">7</span> : <span style="color: #483d8b;">'seven'</span>,
          <span style="color: #ff4500;">8</span> : <span style="color: #483d8b;">'eight'</span>,
          <span style="color: #ff4500;">9</span> : <span style="color: #483d8b;">'nine'</span>,
          <span style="color: #ff4500;">10</span> : <span style="color: #483d8b;">'ten'</span>
      <span style="color: black;">&#125;</span>
      num1 = <span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
      num2 = <span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
      res = num1 + num2
      session<span style="color: black;">&#91;</span><span style="color: #483d8b;">'captcha'</span><span style="color: black;">&#93;</span> = res
      session.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>label<span style="color: black;">&#91;</span>num1<span style="color: black;">&#93;</span>,label<span style="color: black;">&#91;</span>num2<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span></pre></div></div>

<h3>On the templates folder create an html file with an example form</h3>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;${url(controller='captcha',action='contact')}&quot; id=&quot;contact-frm&quot;&gt;
  &lt;fieldset&gt;
    &lt;%
      capt = h.makeCaptcha()
      c.captcha =  u&quot;What is the result of the sum %s and %s?.&quot; % (capt[0],capt[1])
    %&gt;
    &lt;span&gt;${u&quot;As an  anti spam measure, please answer the following math question.&quot;}&lt;/span&gt;&lt;br /&gt;
    &lt;label for=&quot;captcha&quot;&gt;${c.captcha}&lt;/label&gt;
    &lt;!-- we set a maxlength of 2 characters on this input field because the answer should be numeric --&gt;
    ${h.text('captcha',maxlength=2)}
 &lt;/fieldset&gt;
&lt;/form&gt;</pre></div></div>

<p>Save it and call it captcha.html</p>
<h3>Add the following to captcha controller</h3>
<p>I decided to create a validation schema using formencode:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> formencode
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> formencode <span style="color: #ff7700;font-weight:bold;">import</span> variabledecode
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> formencode <span style="color: #ff7700;font-weight:bold;">import</span> validators
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> formencode.<span style="color: black;">validators</span> <span style="color: #ff7700;font-weight:bold;">import</span> Invalid, FancyValidator
&nbsp;
<span style="color: #808080; font-style: italic;">#this class will validate the captcha value entered by the user</span>
<span style="color: #ff7700;font-weight:bold;">class</span> CaptchaValidator<span style="color: black;">&#40;</span>formencode.<span style="color: black;">FancyValidator</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> _to_python<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,values,state<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> session.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'captcha'</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>values.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'captcha'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
          <span style="color: #ff7700;font-weight:bold;">raise</span> formencode.<span style="color: black;">Invalid</span><span style="color: black;">&#40;</span>u<span style="color: #483d8b;">&quot;The math answer is incorrect&quot;</span>,values, state<span style="color: black;">&#41;</span>
          <span style="color: #ff7700;font-weight:bold;">return</span> values
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> NewInquiry<span style="color: black;">&#40;</span>formencode.<span style="color: black;">Schema</span><span style="color: black;">&#41;</span>:
  allow_extra_fields = <span style="color: #008000;">True</span>
  filter_extra_field = <span style="color: #008000;">True</span>
  captcha = formencode.<span style="color: black;">validators</span>.<span style="color: black;">String</span><span style="color: black;">&#40;</span>
    not_empty = <span style="color: #008000;">True</span>,
    messages = <span style="color: black;">&#123;</span>
      <span style="color: #483d8b;">'empty'</span> : <span style="color: #483d8b;">'You need to answer the math question.'</span>
  <span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
  <span style="color: #808080; font-style: italic;"># chain the captcha validator</span>
  chained_validators = <span style="color: black;">&#91;</span>CaptchaValidator<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># our contact view</span>
<span style="color: #ff7700;font-weight:bold;">def</span> contact<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> request.<span style="color: black;">method</span> == <span style="color: #483d8b;">'POST'</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
          values = <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>request.<span style="color: black;">params</span><span style="color: black;">&#41;</span>
          schema = NewInquiry<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
          results = schema.<span style="color: black;">to_python</span><span style="color: black;">&#40;</span>values<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> Invalid, e:
          <span style="color: #808080; font-style: italic;">#raise error if something went wrong</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
          <span style="color: #808080; font-style: italic;">#send to contacts</span>
      <span style="color: #ff7700;font-weight:bold;">return</span> render<span style="color: black;">&#40;</span><span style="color: #483d8b;">'captcha.html'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>And that&#8217;s pretty much it. The only drawback I can find is that isn&#8217;t very intuitive at times since users my try to answer with words instead of numeric values. That&#8217;s why I added a maxlength of 2 characters to the input field.</p>
<p>But you could easily implement a Javascript validation function to notify the user to type numeric values instead of characters before she submits the form.</p>
]]></content:encoded>
			<wfw:commentRss>http://wuilly.com/implementing-a-text-based-captcha-in-pylons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML lists with jQuery for mobile devices</title>
		<link>http://wuilly.com/html-lists-with-jquery-for-mobile-devices/</link>
		<comments>http://wuilly.com/html-lists-with-jquery-for-mobile-devices/#comments</comments>
		<pubDate>Sat, 30 Jul 2011 18:04:59 +0000</pubDate>
		<dc:creator>webjunkie01</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[uWSGI]]></category>

		<guid isPermaLink="false">http://mercadoinmobi.com/?p=13</guid>
		<description><![CDATA[Recently I had to transform a group of li elements into an accordion for a mobile version of a client website. So this is what I came up with. I had an html that looked like this &#60;ul&#62; &#60;li&#62;&#60;span&#62;Category1&#60;/span&#62; &#60;ul&#62; &#60;li&#62;Subcategory1&#60;/li&#62; &#60;li&#62;Subcategory2&#60;/li&#62; &#60;/ul&#62; &#60;/li&#62; &#60;li&#62;&#60;span&#62;Category2&#60;/span&#62; &#60;ul&#62; &#60;li&#62;Subcategory&#60;/li&#62; &#60;li&#62;Subcategory1&#60;/li&#62; &#60;/ul&#62; &#60;/li&#62; &#160; &#60;/ul&#62; And I wanted [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had to transform a group of <em>li</em> elements into an accordion for a mobile version of a client website. So this is what I came up with.</p>
<p>I had an html that looked like this</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;ul&gt;
&lt;li&gt;&lt;span&gt;Category1&lt;/span&gt;
  &lt;ul&gt;
    &lt;li&gt;Subcategory1&lt;/li&gt;
    &lt;li&gt;Subcategory2&lt;/li&gt;
  &lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Category2&lt;/span&gt;
  &lt;ul&gt;
    &lt;li&gt;Subcategory&lt;/li&gt;
    &lt;li&gt;Subcategory1&lt;/li&gt;
  &lt;/ul&gt;
&lt;/li&gt;
&nbsp;
&lt;/ul&gt;</pre></div></div>

<p>And I wanted to collapse all the subcategories on load leaving only the categories visible and when they click/touched each category subcategories will expand. So I wrote this script based off from <a href="http://samcroft.co.uk/2011/mobile-webkit-accordion-jquery-plugin/">Sam Croft</a> script.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">accordion</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> el <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> catHeight<span style="color: #339933;">;</span>
    catHeight <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    el.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.category ul'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> category <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      catHeight<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> category.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      category.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'closed'</span><span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      el.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.category-header '</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'touchstart'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> toExpand <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> toExpand.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li ul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>toExpand.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'active'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          toExpand
          .<span style="color: #660066;">removeAttr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span>
          .<span style="color: #660066;">removeAttr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'style'</span><span style="color: #009900;">&#41;</span>
          .<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'closed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> active <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#active'</span><span style="color: #009900;">&#41;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>active.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          active
          .<span style="color: #660066;">removeAttr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span>
          .<span style="color: #660066;">removeAttr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'style'</span><span style="color: #009900;">&#41;</span>
          .<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'closed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        toExpand
        .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'active'</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'height'</span><span style="color: #339933;">,</span> catHeight<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'closed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And with this script I had to modify my html lists to look like this</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;ul class=&quot;list&quot;&gt;
&lt;li class=&quot;category&quot;&gt;&lt;a class=&quot;category-header&quot; href=&quot;#&quot;&gt;Category1&lt;/a&gt;
  &lt;ul&gt;
    &lt;li&gt;Subcategory1&lt;/li&gt;
    &lt;li&gt;Subcategory2&lt;/li&gt;
  &lt;/ul&gt;
&lt;/li&gt;
&lt;li class=&quot;category&quot;&gt;&lt;a class=&quot;category-header&quot; href=&quot;#&quot;&gt;Category2&lt;/a&gt;
  &lt;ul&gt;
    &lt;li&gt;Subcategory&lt;/li&gt;
    &lt;li&gt;Subcategory1&lt;/li&gt;
  &lt;/ul&gt;
&lt;/li&gt;
&nbsp;
&lt;/ul&gt;</pre></div></div>

<p>My css file</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.closed</span> <span style="color: #00AA00;">&#123;</span>  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#active</span><span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">block</span><span style="color: #00AA00;">&#125;</span></pre></div></div>

<h3>Usage</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.list'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">accordion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>You can see a working example <a href="http://jsfiddle.net/webjunkie01/cq3Rm/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://wuilly.com/html-lists-with-jquery-for-mobile-devices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
