Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Raphael Jolivet http://www.lalala.fr

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

gzip pipe

#! /usr/bin/python

from gzip import GzipFile
from StringIO import StringIO

class GZipPipe(StringIO) :
"""This class implements a compression pipe suitable for asynchronous
process.

Only one buffer of data is read/compressed at a time.
The process doesn't read the whole file at once : This improves performance
and prevent hight memory consumption for big files."""

# Size of the internal buffer
CHUNCK_SIZE = 1024

def __init__(self, source = None, name = "data") :
"""Constructor

@param source Source data to compress (as a stream/File/Buffer - anything with a read() method)
@param name Name of the data within the zip file"""

# Source file
self.source = source

# OEF reached for source ?
self.source_eof = False

# Buffer
self.buffer = ""

StringIO.__init__(self)

# Inherited constructor

# Init ZipFile that writes to us (the StringIO buffer)
self.zipfile = GzipFile(name, 'wb', 9, self)

def write(self, data) :
"""The write mzthod shouldn't be called from outside.
A GZipFile was created with this current object as a output buffer anbd it
fills it whenever we write to it (calling the read method of this object will do it for you)
"""

self.buffer += data

def read(self, size = -1) :
"""Calling read() on a zip pipe will suck data from the source stream.

@param size Maximum size to read - Read whole compressed file if not specified.
@return Compressed data"""

# Feed the zipped buffer by writing source data to the zip stream
while ((len(self.buffer) < size) or (size == -1)) and not self.source_eof :

# No source given in input
if self.source == None: break

# Get a chunk of source data
chunk = self.source.read(GZipPipe.CHUNCK_SIZE)

# Feed the source zip file (that fills the compressed buffer)
self.zipfile.write(chunk)

# End of source file ?
if (len(chunk) < GZipPipe.CHUNCK_SIZE) :
self.source_eof = True
self.zipfile.flush()
self.zipfile.close()
break


# We have enough data in the buffer (or source file is EOF): Give it to the output
if size == 0:
result = ""
if size >= 1 :
result = self.buffer[0:size]
self.buffer = self.buffer[size:]
else : # size < 0 : All requested
result = self.buffer
self.buffer = ""

return result

Change system date of photos to EXIF date

This script takes a list of files as arguments.
It set the system datetime (both modifitation and access) to the date
the photo was taken.

It use the EXIF module for that, that needs to be in the same folder as the script :
You can find it here :
http://home.cfl.rr.com/genecash/digital_camera/EXIF.py

   1  
   2  #!/usr/bin/python
   3  
   4  import sys
   5  import EXIF
   6  from datetime import *
   7  import time
   8  import os
   9  
  10  # Loop on arguments (files)
  11  for arg in sys.argv[1:] :
  12      
  13      # Do nothing of dirs
  14      if os.path.isdir(arg) :
  15          continue
  16  
  17      # Open the file
  18      f=open(arg, 'rb')
  19   
  20      # Read exif data
  21      tags = EXIF.process_file(f)
  22      
  23      # Ensure date is present 
  24      if not tags.has_key("Image DateTime") :
  25          continue 
  26          
  27      # Get date of photo
  28      date = tags["Image DateTime"]
  29      date = datetime(*(time.strptime(date.values, "%Y:%m:%d %H:%M:%S")[0:6]))
  30      timestamp = int(time.mktime(date.timetuple()))
  31      
  32      # Some traces
  33      print "File:%s - Time:%s " % (arg, timestamp)
  34  
  35      # Change the date
  36      os.utime(arg, (timestamp, timestamp))
  37  
  38  

base64

Simple python script to trnasform std input to base64 encoding

   1  
   2  #! /usr/bin/python
   3  
   4  import base64
   5  import sys
   6  
   7  encoded = base64.b64encode(sys.stdin.read())
   8  
   9  print encoded
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS