How to replace new line characters \r\n with a newline in VIM

Plain Text posted 1 day ago by christian

To get the ^M character, type CTRL+V and press enter:

   1  :%s/\\r\\n/^M/g

Tagged vim, newline, search, replace

How to fetch URLs in parallell with EventMachine and Ruby

Ruby posted 5 days ago by christian

Save time by doing things in parallell:

   1  require 'rubygems'
   2  require 'eventmachine'
   3  require 'open-uri'
   4  require 'pp'
   5  require 'thread'
   6  require 'benchmark'
   7  
   8  class Worker
   9    include EM::Deferrable
  10  
  11    def run
  12      get_google
  13      set_deferred_status :succeeded
  14    end
  15  end
  16  
  17  def get_google
  18    # sorry for spamming you
  19    open('http://www.google.com/') do |f|
  20      #pp f.meta
  21    end
  22  end
  23  
  24  def asynchronous(i)
  25    worker = Worker.new
  26    # on success
  27    worker.callback do
  28      p "#{Thread.current} done #{i}!"
  29    end 
  30    worker.errback do 
  31      p "Unexpected error"    
  32      EM.stop  
  33    end
  34    #
  35    Thread.new do
  36      worker.run
  37      EM.stop
  38    end 
  39    #puts "scheduling done!"
  40  end
  41  
  42  def synchronous(i)
  43    get_google
  44  end
  45  
  46  # on error
  47  EM.error_handler do |e|  
  48    p "Unexpected error: #{e}" 
  49  end
  50  
  51  EM.run do
  52    seconds = Benchmark.realtime do
  53      50.times do |i|
  54        asynchronous i
  55      end
  56    end
  57    p "With EventMachine: #{seconds} elapsed..."
  58  
  59    seconds = Benchmark.realtime do
  60      50.times do |i|
  61        synchronous i
  62      end
  63    end
  64    p "Without EventMachine: #{seconds} elapsed..."
  65  end

Output:

   1  With EventMachine: 9.05974316596985 elapsed...
   2  Without EventMachine: 19.1381118297577 elapsed...

Conclusion

  • Speeds up blocking operations.
  • EventMachine is currently limited to one CPU core (native thread) per process.

References

Tagged eventmachine, ruby, asynchronous, job

How to parse RSS/Atom feeds with Scala and the Rome library

Java posted 5 days ago by christian

This snippet shows how to parse feeds with Scala and the Rome library:

   1  import com.sun.syndication.io._
   2  import com.sun.syndication.feed.synd._
   3  import java.net.URL
   4  
   5  object FeedParser {
   6    def main(args: Array[String]): Unit = {
   7      try {
   8        val sfi = new SyndFeedInput()
   9  
  10        val urls = List("http://hbl.fi/rss.xml")
  11        
  12        urls.foreach(url => {
  13          val feed = sfi.build(new XmlReader(new URL(url)))
  14  
  15          val entries = feed.getEntries()
  16  
  17          System.out.println(feed.getTitle())
  18          System.out.println(entries.size())
  19        })
  20      } catch {
  21        case e => throw new RuntimeException(e)
  22      }
  23      
  24    }
  25  }

Tagged scala, feed, atom, rss, parse

How to retrieve information about Python errors in a C extension

Python posted 23 days ago by christian

   1  result = PyEval_CallObject(tmp_callback, args);
   2      // result == NULL means an error occured
   3      if (PyErr_Occurred()) {
   4          PyObject* ptype;
   5          PyObject* pvalue;
   6          PyObject* ptraceback;
   7          PyErr_Fetch(&ptype, &pvalue, &ptraceback);
   8          printf("Error occurred on line: %d", ((PyTracebackObject*)ptraceback)->tb_lineno);
   9          // Restore exception instead of disposing of it
  10          PyErr_Restore(ptype, pvalue, ptraceback);
  11          PyErr_Print();
  12  
  13          Py_XDECREF(ptype);
  14          Py_XDECREF(pvalue);
  15          Py_XDECREF(ptraceback);
  16      }

via http://www.ragestorm.net/tutorial?id=21

Tagged python, pyeval_callobject, pyerr_fetch

How to view a Git repository in your browser with "git instaweb"

Shell Script (Bash) posted 28 days ago by christian

git instaweb -d webrick:

   1  % git instaweb -d webrick      
   2  [2010-02-18 17:27:01] INFO  WEBrick 1.3.1
   3  [2010-02-18 17:27:01] INFO  ruby 1.8.7 (2008-08-11) [i686-darwin10.0.0]
   4  No known browser available.
   5  http://127.0.0.1:1234

Now open http://127.0.0.1:1234 in your browser.

You can also add this this to ./gitconfig:

   1  [instaweb]
   2  httpd=webrick

Tagged git, instaweb, browser