Py++ is an object-oriented framework for creating a code generator for Boost.Python library and ctypes package.
Boost is repository of free peer-reviewed portable C++ source libraries.
Boost works on almost any modern operating system, including UNIX and Windows variants.
Boost libraries have good interface, documentation and unit tests. A lot of people contributed their knowledge and experience to those libraries. Ten Boost libraries are already included in the C++ Standards Committee’s Library Technical Report ( TR1 ) as a step toward becoming part of a future C++ Standard. More Boost libraries are proposed for the upcoming TR2.
I believe that Py++ is ready for hard work. It is quick, stable and flexible. Py++ is a new tool and in my opinion I should prove its usefulness. Using Py++, I exposed the following libraries to Python:
There are few reasons I choose to expose those libraries.
- good documentation
- well-defined interfaces
- clear concepts
- comprehensive unit tests
I spent different amount of time on each library. It took me one week, to expose the Boost.Date_Time library. I added few missing features to Py++, polished the existing ones. Most of the time I spent translating tests from C++ to Python. It took me 3 days to expose all other libraries.
Right now, pyboost package does not have documentation. In my opinion it is fairly simple to read original documentation and “translate” it to Python.
One picture worth thousands words.
C++ code: http://boost.org/libs/random/index.html
Python code:
import time
from pyboost import boost_random
rng = boost_random.mt19937( int( time.time() ) ) #setting initial seed
six = boost_random.uniform_int(1,6)
die = boost_random.variate_generator( rng, six )
print die()
C++ code: http://www.boost.org/doc/html/date_time/examples/general_usage_examples.html
Python code:
import os
from pyboost import date_time
from pyboost.date_time import gregorian
from pyboost.date_time import posix_time
from pyboost.date_time import local_time
from pyboost.date_time import to_simple_string
#Date programming
weekstart = gregorian.date(2002, date_time.Feb,1)
print 'weekstart: ', to_simple_string( weekstart )
weekend = weekstart + gregorian.weeks(1)
print 'weekend: ', to_simple_string( weekend )
today = gregorian.day_clock.local_day()
d2 = weekstart + gregorian.days(5)
if d2 >= today: #comparison operator
pass
thisWeek = gregorian.date_period(today,d2)
if thisWeek.contains(today):
pass
#date generator functions
d5 = gregorian.next_weekday(today, date_time.Sunday); #calculate Sunday following d4
print 'd5: ', to_simple_string( d5 )
#US labor day is first Monday in Sept
first = gregorian.nth_day_of_the_week_in_month.first
labor_day = gregorian.nth_day_of_the_week_in_month(first, date_time.Monday, date_time.Sep)
#calculate a specific date for 2004 from functor
print 'labor day 2004: ', to_simple_string( labor_day.get_date(2004) )
#Time programming:
d = gregorian.date(2002,date_time.Feb,1)#an arbitrary date
t1 = posix_time.ptime(d, posix_time.hours(5) + posix_time.millisec(100)); #date + time of day offset
print 't1: ', to_simple_string( t1 )
t2 = t1 - posix_time.minutes(4) + posix_time.seconds(2)
print 't2: ', to_simple_string( t2 )
now = posix_time.second_clock.local_time(); #use the clock
print 'now: ', to_simple_string( now )
today = now.date() #Get the date part out of the time
print 'today: ', to_simple_string( today )
tomorrow = today + gregorian.date_duration(1)
print 'tomorrow: ', to_simple_string( tomorrow )
#Local time programming:
#setup some timezones for creating and adjusting times
#first time zone uses the time zone file for regional timezone definitions
tz_db = local_time.time_zone_database()
tz_db.load_from_file( os.path.join( date_time.__path__[0], "date_time_zonespec.csv") )
nyc_tz = tz_db.time_zone_from_region("America/New_York")
#This timezone uses a posix time zone string definition to create a time zone
phx_tz = local_time.posix_time_zone("MST-07:00:00")
#local departure time in phoenix is 11 pm on April 2 2005
#Note that New York changes to daylight savings on Apr 3 at 2 am)
phx_departure = local_time.local_date_time(
gregorian.date(2005, date_time.Apr, 2)
, posix_time.hours(23)
, phx_tz
, local_time.local_date_time.NOT_DATE_TIME_ON_ERROR)
flight_length = posix_time.hours(4) + posix_time.minutes(30)
phx_arrival = phx_departure + flight_length
#convert the phx time to a nyz time
nyc_arrival = phx_arrival.local_time_in(nyc_tz, posix_time.time_duration() )
print "New York arrival: ", nyc_arrival.to_string() #//2005-Apr-03 06:30:00 EDT
C++ code: http://boost.org/libs/rational/rational_example.cpp
Python code:
import unittest
from pyboost import rational
half = rational.rational( 1, 2 )
one = rational.rational( 1 )
two = rational.rational( 2 )
#Some basic checks
assert half.numerator() == 1
assert half.denominator() == 2
assert float(half) == 0.5
#Arithmetic
assert half + half == one == 1
assert one - half == half
assert two * half == one == 1
assert one / half == two == 2
Python code:
import os
import sys
from pyboost import crc
if __name__ == '__main__':
if sys.argv:
files = sys.argv
else:
files = [ sys.executable ]
try:
result = crc.crc_32_type()
for file_name in files:
ifs = file( file_name, 'rb' )
for line in ifs:
result.process_bytes( line )
print hex( result.checksum() ).upper()
except Exception, error:
sys.stderr.write( "Found an exception with'%s'%s" %( str( error ), os.linesep ) )
sys.exit( 1 )