<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ast code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 04:33:22 GMT</pubDate>
    <description>DZone Snippets: ast code</description>
    <item>
      <title>Visit Python Abstract Syntax Tree</title>
      <link>http://snippets.dzone.com/posts/show/3360</link>
      <description>Simplest AST visitor. More on this blog post :&lt;br /&gt;&lt;a href="http://www.biais.org/blog/index.php/2007/01/10/9-visit-python-abstract-syntax-tree"&gt;http://www.biais.org/blog/index.php/2007/01/10/9-visit-python-abstract-syntax-tree&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import compiler&lt;br /&gt; &lt;br /&gt;class CodePrinter:&lt;br /&gt;    def __init__(self):&lt;br /&gt;        self.src = ''&lt;br /&gt; &lt;br /&gt;    def visitName(self,t):&lt;br /&gt;        self.src += t.name&lt;br /&gt; &lt;br /&gt;    def visitConst(self,t):&lt;br /&gt;        self.src += str(t.value)&lt;br /&gt; &lt;br /&gt;    def visitStmt(self, t):&lt;br /&gt;        for i in t:&lt;br /&gt;            a = pretty_print(i)&lt;br /&gt;            self.src += a + "\n"&lt;br /&gt; &lt;br /&gt;    def visitAssName(self, t):&lt;br /&gt;        self.src += t.name + " = "&lt;br /&gt; &lt;br /&gt;def pretty_print(node):&lt;br /&gt;    myvisitor = CodePrinter()&lt;br /&gt;    # compiler.walk return the visitor instance : 2nd arg&lt;br /&gt;    return compiler.walk(node, myvisitor).src&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 24 Jan 2007 13:03:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3360</guid>
      <author>maxme (Maxime Biais)</author>
    </item>
    <item>
      <title>Ruby Client for Amazon Alexa Site Thumbnail (AST) Service</title>
      <link>http://snippets.dzone.com/posts/show/3087</link>
      <description>It's scrappy, but it does the job.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'cgi'&lt;br /&gt;require 'openssl'&lt;br /&gt;require 'base64'&lt;br /&gt;require 'open-uri'&lt;br /&gt;&lt;br /&gt;access_id = 'YOUR_ACCESS_ID'&lt;br /&gt;secret_id = 'YOUR_SECRET_ID'&lt;br /&gt;&lt;br /&gt;source_url = ARGV.first&lt;br /&gt;&lt;br /&gt;timestamp = Time.now.strftime("%Y-%m-%dT%H:%M:%SZ")&lt;br /&gt;sig = Base64.encode64(OpenSSL::HMAC::digest(OpenSSL::Digest::Digest.new('SHA1'), secret_id, 'Thumbnail' + timestamp)).strip&lt;br /&gt;&lt;br /&gt;url = "http://ast.amazonaws.com/Xino?Action=Thumbnail&amp;AWSAccessKeyId=" + access_id&lt;br /&gt;url &lt;&lt; "&amp;Signature=" + CGI.escape(sig)&lt;br /&gt;url &lt;&lt; "&amp;Timestamp=" + CGI.escape(timestamp)&lt;br /&gt;url &lt;&lt; "&amp;Url=" +  source_url&lt;br /&gt;&lt;br /&gt;begin&lt;br /&gt;  doc = open(url).read&lt;br /&gt;rescue&lt;br /&gt;  puts "Could not access AWS"&lt;br /&gt;  exit&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;m = doc.match(/\&lt;aws:thumbnail[^\&gt;]+exists=\"true\"\&gt;(.+?)\&lt;\//i)&lt;br /&gt;&lt;br /&gt;if m &amp;&amp; m[1]&lt;br /&gt;  thumb_url = m[1]&lt;br /&gt;  thumb_url.gsub!(/\&amp;amp;/, '&amp;')&lt;br /&gt;  File.open("#{source_url}.jpg", "w") { |f| f.write open(thumb_url).read }&lt;br /&gt;  puts "Saved to #{source_url}.jpg"&lt;br /&gt;elsif m &amp;&amp; m.match(/exists=\"false\"/)&lt;br /&gt;  puts "No thumbnail for #{source_url}"&lt;br /&gt;else&lt;br /&gt;  puts "Error"&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 05 Dec 2006 18:31:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3087</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
  </channel>
</rss>
