<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users841</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/841" rel="self"/>
  <title>Tafkas</title>
  <updated>Thu Oct 20 03:19:46 -0700 2011</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1902</id>
    <published>2011-10-20T03:19:46-07:00</published>
    <updated>2012-01-31T08:59:32-08:00</updated>
    <title>[Python] Two loops on list</title>
    <content type="html">&lt;p&gt;I want to apply some calculation on the elements of list in python. In Java I would have done to for-loops. Is there a more pythonic way to do that?&lt;/p&gt;

&lt;p&gt;[1,2,3,4,5] --&amp;gt; 1 with 2, 1 with 3, ..., 4 with 5&lt;/p&gt;

&lt;pre&gt;def foo(mylist):
    magic  = 0
    for i in range(len(mylist)-1):
        for j in range(i, len(mylist)):
            ### do some magic ###
    return magic

&lt;/pre&gt;</content>
    <author>
      <name>Tafkas</name>
      <email>tafkasorg@yahoo.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1902-two-loops-on-list" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1233</id>
    <published>2010-03-19T23:38:19-07:00</published>
    <updated>2010-05-27T02:19:39-07:00</updated>
    <title>[Ruby] Exception Handling in Ruby</title>
    <content type="html">&lt;p&gt;I have the following code where I parse a text and try to retrieve some information to store it in a database. Unfortunately sometimes there is no data and I have to set the value to &amp;quot;null&amp;quot;. Essentially I have just a couple of these blocks there. My approach just does not seem right.&lt;/p&gt;

&lt;pre&gt;begin
  data[&amp;quot;body&amp;quot;] = content[1].split(/\W{2,}/)[0].strip()
rescue Exception =&amp;gt; e
  data[&amp;quot;body&amp;quot;] = &amp;quot;null&amp;quot;
end
begin
  data[&amp;quot;height&amp;quot;] = content[2].split(/\W{2,}/)[0].strip()
rescue Exception =&amp;gt; e
  data[&amp;quot;height&amp;quot;] = &amp;quot;null&amp;quot;
end
begin
  data[&amp;quot;status&amp;quot;] = content[7].split(/\W{2,}/)[0].strip()
rescue Exception =&amp;gt; e
  data[&amp;quot;status&amp;quot;] = &amp;quot;null&amp;quot;
end
begin
  data[&amp;quot;country&amp;quot;] = content[8].split(/\W{2,}/)[1].strip()
rescue Exception =&amp;gt; e
  data[&amp;quot;country&amp;quot;] = &amp;quot;null&amp;quot;
end
begin
  data[&amp;quot;zip&amp;quot;] = content[9].split(/\W{2,}/)[0].strip()
rescue Exception =&amp;gt; e
  data[&amp;quot;zip&amp;quot;] = &amp;quot;null&amp;quot;
end
begin
  data[&amp;quot;location&amp;quot;] = content[10].split(/\W{2,}/)[0].strip()
rescue Exception =&amp;gt; e
  data[&amp;quot;location&amp;quot;] = &amp;quot;null&amp;quot;
end
&lt;/pre&gt;</content>
    <author>
      <name>Tafkas</name>
      <email>tafkasorg@yahoo.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1233-exception-handling-in-ruby" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1159</id>
    <published>2010-01-28T19:53:25-08:00</published>
    <updated>2010-04-13T22:43:37-07:00</updated>
    <title>[Python] Return the indexes of specific elements of the list</title>
    <content type="html">&lt;p&gt;The function takes an list of floats ranging from 0.0 to 100.0 in an ascending order. It returns a list of indexes with the indexes of the elements closest to 25.0, 50.0 and 75.0.&lt;/p&gt;

&lt;p&gt;The intention is to label a time axis, where the labels should correspond to their relative position.&lt;/p&gt;

&lt;pre&gt;def get_pos(rel_dates):
    &amp;quot;&amp;quot;&amp;quot;returns the positions of the dates closest to 25%, 50%, and 75%&amp;quot;&amp;quot;&amp;quot;
    pos = [0] # first element
    dates25, dates50, dates75 = [], [], []
    for date in rel_dates:
        dates25.append(abs(date-25))
        dates50.append(abs(date-50))
        dates75.append(abs(date-75))
    pos.append(dates25.index(min(dates25)))
    pos.append(dates50.index(min(dates50)))
    pos.append(dates75.index(min(dates75)))
    pos.append(len(rel_dates)-1) #last element
    return pos&lt;/pre&gt;</content>
    <author>
      <name>Tafkas</name>
      <email>tafkasorg@yahoo.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1159-return-the-indices-of-specific-elements-of-the-list" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1153</id>
    <published>2010-01-22T12:55:27-08:00</published>
    <updated>2010-04-01T11:50:40-07:00</updated>
    <title>[Python] Scale data points with the relative distance based on time</title>
    <content type="html">&lt;p&gt;In order to print a chart that will separate each data point with the relative distance based on time the data has to be scaled.
&lt;br /&gt;The parameter &amp;quot;dates&amp;quot; is an array of unix timestamps.&lt;/p&gt;

&lt;pre&gt;def scale_dates(dates):
    rel_dates = []
    first_day = dates[0]
    last_day = dates[-1]
    d = last_day - first_day
    for date in dates:
        date = float(date-first_day)/(d)
        rel_dates.append(date)
    return rel_dates&lt;/pre&gt;</content>
    <author>
      <name>Tafkas</name>
      <email>tafkasorg@yahoo.de</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1153-scale-data-points-with-the-relative-distance-based-on-time" rel="alternate"/>
  </entry>
</feed>

