<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Cdvddt's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 28 Aug 2008 11:08:46 GMT</pubDate>
    <description>DZone Snippets: Cdvddt's Code Snippets</description>
    <item>
      <title>gzip pipe</title>
      <link>http://snippets.dzone.com/posts/show/5644</link>
      <description>#! /usr/bin/python&lt;br /&gt;&lt;br /&gt;from gzip import GzipFile&lt;br /&gt;from StringIO import StringIO&lt;br /&gt;&lt;br /&gt;class GZipPipe(StringIO) :&lt;br /&gt;    """This class implements a compression pipe suitable for asynchronous &lt;br /&gt;    process.&lt;br /&gt;&lt;br /&gt;    Only one buffer of data is read/compressed at a time.&lt;br /&gt;    The process doesn't read the whole file at once : This improves performance&lt;br /&gt;    and prevent hight memory consumption for big files."""&lt;br /&gt;&lt;br /&gt;    # Size of the internal buffer&lt;br /&gt;    CHUNCK_SIZE = 1024 &lt;br /&gt;&lt;br /&gt;    def __init__(self, source = None, name = "data") :&lt;br /&gt;        """Constructor&lt;br /&gt;        &lt;br /&gt;        @param source   Source data to compress (as a stream/File/Buffer - anything with a read() method)&lt;br /&gt;        @param name     Name of the data within the zip file"""&lt;br /&gt;        &lt;br /&gt;        # Source file&lt;br /&gt;        self.source = source&lt;br /&gt;&lt;br /&gt;        # OEF reached for source ?&lt;br /&gt;        self.source_eof = False&lt;br /&gt;&lt;br /&gt;        # Buffer&lt;br /&gt;        self.buffer = ""&lt;br /&gt;&lt;br /&gt;        StringIO.__init__(self)&lt;br /&gt;&lt;br /&gt;        # Inherited constructor&lt;br /&gt;&lt;br /&gt;        # Init ZipFile that writes to us (the StringIO buffer)&lt;br /&gt;        self.zipfile = GzipFile(name, 'wb', 9, self)&lt;br /&gt;    &lt;br /&gt;    def write(self, data) :&lt;br /&gt;        """The write mzthod shouldn't be called from outside.&lt;br /&gt;        A GZipFile was created with this current object as a output buffer anbd it &lt;br /&gt;        fills it whenever we write to it (calling the read method of this object will do it for you)&lt;br /&gt;        """&lt;br /&gt;        &lt;br /&gt;        self.buffer += data&lt;br /&gt;&lt;br /&gt;    def read(self, size = -1) :&lt;br /&gt;        """Calling read() on a zip pipe will suck data from the source stream.&lt;br /&gt;&lt;br /&gt;        @param  size Maximum size to read - Read whole compressed file if not specified.&lt;br /&gt;        @return Compressed data"""&lt;br /&gt;&lt;br /&gt;        # Feed the zipped buffer by writing source data to the zip stream&lt;br /&gt;        while ((len(self.buffer) &lt; size) or (size == -1)) and not self.source_eof :&lt;br /&gt;           &lt;br /&gt;            # No source given in input&lt;br /&gt;            if self.source == None: break&lt;br /&gt;&lt;br /&gt;            # Get a chunk of source data&lt;br /&gt;            chunk = self.source.read(GZipPipe.CHUNCK_SIZE)&lt;br /&gt;            &lt;br /&gt;            # Feed the source zip file (that fills the compressed buffer)&lt;br /&gt;            self.zipfile.write(chunk)&lt;br /&gt;            &lt;br /&gt;            # End of source file ?&lt;br /&gt;            if (len(chunk) &lt; GZipPipe.CHUNCK_SIZE) :&lt;br /&gt;                self.source_eof = True&lt;br /&gt;                self.zipfile.flush()&lt;br /&gt;                self.zipfile.close()&lt;br /&gt;                break&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        # We have enough data in the buffer (or source file is EOF): Give it to the output&lt;br /&gt;        if size == 0:&lt;br /&gt;            result = ""&lt;br /&gt;        if size &gt;= 1 :&lt;br /&gt;            result = self.buffer[0:size]&lt;br /&gt;            self.buffer = self.buffer[size:]&lt;br /&gt;        else : # size &lt; 0 : All requested&lt;br /&gt;            result = self.buffer&lt;br /&gt;            self.buffer = ""&lt;br /&gt;&lt;br /&gt;        return result&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Sun, 15 Jun 2008 08:50:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5644</guid>
      <author>cdvddt (Raphael Jolivet)</author>
    </item>
    <item>
      <title>Change system date of photos to EXIF date</title>
      <link>http://snippets.dzone.com/posts/show/4089</link>
      <description>This script takes a list of files as arguments.&lt;br /&gt;It set the system datetime (both modifitation and access) to the date &lt;br /&gt;the photo was taken.&lt;br /&gt;&lt;br /&gt;It use the EXIF module for that, that needs to be in the same folder as the script :&lt;br /&gt;You can find it here :&lt;br /&gt;http://home.cfl.rr.com/genecash/digital_camera/EXIF.py &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/python&lt;br /&gt;&lt;br /&gt;import sys&lt;br /&gt;import EXIF&lt;br /&gt;from datetime import *&lt;br /&gt;import time&lt;br /&gt;import os&lt;br /&gt;&lt;br /&gt;# Loop on arguments (files)&lt;br /&gt;for arg in sys.argv[1:] :&lt;br /&gt;    &lt;br /&gt;    # Do nothing of dirs&lt;br /&gt;    if os.path.isdir(arg) :&lt;br /&gt;        continue&lt;br /&gt;&lt;br /&gt;    # Open the file&lt;br /&gt;    f=open(arg, 'rb')&lt;br /&gt; &lt;br /&gt;    # Read exif data&lt;br /&gt;    tags = EXIF.process_file(f)&lt;br /&gt;    &lt;br /&gt;    # Ensure date is present &lt;br /&gt;    if not tags.has_key("Image DateTime") :&lt;br /&gt;        continue &lt;br /&gt;        &lt;br /&gt;    # Get date of photo&lt;br /&gt;    date = tags["Image DateTime"]&lt;br /&gt;    date = datetime(*(time.strptime(date.values, "%Y:%m:%d %H:%M:%S")[0:6]))&lt;br /&gt;    timestamp = int(time.mktime(date.timetuple()))&lt;br /&gt;    &lt;br /&gt;    # Some traces&lt;br /&gt;    print "File:%s - Time:%s " % (arg, timestamp)&lt;br /&gt;&lt;br /&gt;    # Change the date&lt;br /&gt;    os.utime(arg, (timestamp, timestamp))&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 02 Jun 2007 00:10:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4089</guid>
      <author>cdvddt (Raphael Jolivet)</author>
    </item>
    <item>
      <title>base64</title>
      <link>http://snippets.dzone.com/posts/show/4072</link>
      <description>Simple python script to trnasform std input to base64 encoding&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#! /usr/bin/python&lt;br /&gt;&lt;br /&gt;import base64&lt;br /&gt;import sys&lt;br /&gt;&lt;br /&gt;encoded = base64.b64encode(sys.stdin.read())&lt;br /&gt;&lt;br /&gt;print encoded&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 30 May 2007 10:31:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4072</guid>
      <author>cdvddt (Raphael Jolivet)</author>
    </item>
  </channel>
</rss>
