code stringlengths 1 1.72M | language stringclasses 1 value |
|---|---|
from sys import stdin
n = int(stdin.readline().strip())
print "%.3lf" % sum([1.0/x for x in range(1,n+1)])
| Python |
from sys import stdin
print len(stdin.readline().strip())
| Python |
from itertools import product
from math import *
def issqrt(n):
s = int(floor(sqrt(n)))
return s*s == n
aabb = [a*1100+b*11 for a,b in product(range(1,10),range(10))]
print ' '.join(map(str, filter(issqrt, aabb)))
| Python |
from sys import stdin
a = map(int, stdin.readline().strip().split())
print "%d %d %.3lf" % (min(a), max(a), float(sum(a)) / len(a))
| Python |
from sys import stdin
def cycle(n):
if n == 1: return 0
elif n % 2 == 1: return cycle(n*3+1) + 1
else: return cycle(n/2) + 1
n = int(stdin.readline().strip())
print cycle(n)
| Python |
from sys import stdin
n = int(stdin.readline().strip())
count = n*2-1
for i in range(n):
print ' '*i + '#'*count
count -= 2
| Python |
for abc in range(123, 329):
big = str(abc) + str(abc*2) + str(abc*3)
if(''.join(sorted(big)) == '123456789'): print abc, abc*2, abc*3
| Python |
from sys import stdin
n, m = map(int, stdin.readline().strip().split())
print "%.5lf" % sum([1.0/i/i for i in range(n,m+1)])
| Python |
from sys import stdin
data = map(int, stdin.readline().strip().split())
n, m = data[0], data[-1]
data = data[1:-1]
print len(filter(lambda x: x < m, data))
| Python |
from sys import stdin
def solve(a, b, c):
for i in range(10, 101):
if i % 3 == a and i % 5 == b and i % 7 == c:
print i
return
print 'No answer'
a, b, c = map(int, stdin.readline().strip().split())
solve(a, b, c)
| Python |
from itertools import product
sol = [a*100+b*10+c for a,b,c in product(range(1,10), range(10), range(10)) if a**3+b**3+c**3 == a*100+b*10+c]
print '\n'.join(map(str, sol))
| Python |
from sys import stdin
from math import *
n = int(stdin.readline().strip())
print sum(map(factorial, range(1,n+1))) % (10**6)
| Python |
#!/usr/bin/python
# Copyright 2011 Google, Inc. All Rights Reserved.
# simple script to walk source tree looking for third-party licenses
# dumps resulting html page to stdout
import os, re, mimetypes, sys
# read source directories to scan from command line
SOURCE = sys.argv[1:]
# regex to find /* */ style comment blocks
COMMENT_BLOCK = re.compile(r"(/\*.+?\*/)", re.MULTILINE | re.DOTALL)
# regex used to detect if comment block is a license
COMMENT_LICENSE = re.compile(r"(license)", re.IGNORECASE)
COMMENT_COPYRIGHT = re.compile(r"(copyright)", re.IGNORECASE)
EXCLUDE_TYPES = [
"application/xml",
"image/png",
]
# list of known licenses; keys are derived by stripping all whitespace and
# forcing to lowercase to help combine multiple files that have same license.
KNOWN_LICENSES = {}
class License:
def __init__(self, license_text):
self.license_text = license_text
self.filenames = []
# add filename to the list of files that have the same license text
def add_file(self, filename):
if filename not in self.filenames:
self.filenames.append(filename)
LICENSE_KEY = re.compile(r"[^\w]")
def find_license(license_text):
# TODO(alice): a lot these licenses are almost identical Apache licenses.
# Most of them differ in origin/modifications. Consider combining similar
# licenses.
license_key = LICENSE_KEY.sub("", license_text).lower()
if license_key not in KNOWN_LICENSES:
KNOWN_LICENSES[license_key] = License(license_text)
return KNOWN_LICENSES[license_key]
def discover_license(exact_path, filename):
# when filename ends with LICENSE, assume applies to filename prefixed
if filename.endswith("LICENSE"):
with open(exact_path) as file:
license_text = file.read()
target_filename = filename[:-len("LICENSE")]
if target_filename.endswith("."): target_filename = target_filename[:-1]
find_license(license_text).add_file(target_filename)
return None
# try searching for license blocks in raw file
mimetype = mimetypes.guess_type(filename)
if mimetype in EXCLUDE_TYPES: return None
with open(exact_path) as file:
raw_file = file.read()
# include comments that have both "license" and "copyright" in the text
for comment in COMMENT_BLOCK.finditer(raw_file):
comment = comment.group(1)
if COMMENT_LICENSE.search(comment) is None: continue
if COMMENT_COPYRIGHT.search(comment) is None: continue
find_license(comment).add_file(filename)
for source in SOURCE:
for root, dirs, files in os.walk(source):
for name in files:
discover_license(os.path.join(root, name), name)
print "<html><head><style> body { font-family: sans-serif; } pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; } </style></head><body>"
for license in KNOWN_LICENSES.values():
print "<h3>Notices for files:</h3><ul>"
filenames = license.filenames
filenames.sort()
for filename in filenames:
print "<li>%s</li>" % (filename)
print "</ul>"
print "<pre>%s</pre>" % license.license_text
print "</body></html>"
| Python |
#print 'Content-Type: application/xml'
#print ''
#
#f = open( 'voter-info-gadget.xml', 'r' )
#xml = f.read()
#f.close()
#
#print xml
#import re
#from pprint import pformat, pprint
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
#def dumpRequest( req ):
# return pformat({
# 'environ': req.environ,
# 'url': req.url,
# 'headers': req.headers,
# })
#def addDump( xml, req ):
# dr = dumpRequest( req )
# dr = re.sub( r'\}', '\n}', dr )
# dr = re.sub( r"'wsgi[^\n]+\n", '', dr )
# dr = re.sub ( r'\n\s*', ' ', dr )
# dr = re.sub ( r',\s*\}', '}', dr )
# return xml.replace( 'var opt =', 'alert( (' + dr + ').toSource() );\n\n\tvar opt =' ) # poor man's template
class GadgetHandler( webapp.RequestHandler ):
def get( self, dump, debug ):
self.response.headers['Content-Type'] = 'application/xml'
if debug == None: debug = ''
f = open( 'voter-info-gadget.xml', 'r' )
xml = f.read()
f.close()
xml = xml.replace( '{{debug}}', debug ) # poor man's template
#if dump:
# xml = addDump( xml, self.request )
self.response.out.write( xml )
application = webapp.WSGIApplication([
( r'/(dump-)?(.+)?voter-info-gadget\.xml', GadgetHandler )
], debug = True )
def main():
run_wsgi_app( application )
if __name__ == '__main__':
main()
| Python |
#!/usr/bin/env python
import math
# z() and zz() are a quick and dirty hack to deal with the Aleutian Islands.
# We should use a more correct algorithm for extendBounds like the one
# in the Maps API, but this is good enough to fix the immediate problem.
def z( n ):
if n > 0.0:
return n - 360.0
return n
def zz( n ):
if n < -180.0:
return n + 360.0
return n
def minlat( a, b ):
if a == None: return b
return min( a, b )
def maxlat( a, b ):
if a == None: return b
return max( a, b )
def minlng( a, b ):
if a == None: return b
return zz( min( z(a), z(b) ) )
def maxlng( a, b ):
if a == None: return b
return zz( max( z(a), z(b) ) )
class Geo:
def __init__( self, zoom=0, tilesize=256 ):
self.zoom = zoom
self.tilesize = tilesize
def extendBounds( self, a, b ):
return [
[ minlng( a[0][0], b[0][0] ), minlat( a[0][1] , b[0][1] ) ],
[ maxlng( a[1][0], b[1][0] ), maxlat( a[1][1] , b[1][1] ) ]
]
def inflateBounds( self, a, n ):
return [
[ a[0][0] - n, a[0][1] - n ],
[ a[1][0] + n, a[1][1] + n ]
]
def offsetBounds( self, a, pt ):
return [
[ a[0][0] + pt[0], a[0][1] + pt[1] ],
[ a[1][0] + pt[0], a[1][1] + pt[1] ]
]
def offsetBoundsMinus( self, a, pt ):
return [
[ a[0][0] - pt[0], a[0][1] - pt[1] ],
[ a[1][0] - pt[0], a[1][1] - pt[1] ]
]
def scalePoint( self, pt, scale ):
return [ pt[0] * scale, pt[1] * scale ]
def scaleBounds( self, a, scale ):
return [ self.scalePoint( a[0], scale ), self.scalePoint( a[1], scale ) ]
def tileBounds( self, bounds ):
def lo( n ): return int( n / self.tilesize ) * self.tilesize
def hi( n ): return ( int( n / self.tilesize ) + 1 ) * self.tilesize
min = bounds[0]; max = bounds[1]
offset = [ lo(min[0]), lo(min[1]) ]
size = [ hi(max[0]) - offset[0], hi(max[1]) - offset[1] ]
return offset, size
def pixFromGeoPoint( self, point ):
lng = point[0]
if lng > 180.0: lng -= 360.0
lng = lng / 360.0 + 0.5
lat = point[1]
lat = 0.5 - ( math.log( math.tan( ( math.pi / 4.0 ) + ( lat * math.pi / 360.0 ) ) ) / math.pi / 2.0 );
scale = ( 1 << self.zoom ) * self.tilesize
return [ int( lng * scale ), int( lat * scale ) ]
def pixFromGeoBounds( self, bounds ):
a = self.pixFromGeoPoint(bounds[0])
b = self.pixFromGeoPoint(bounds[1])
return [
[ a[0], b[1] ],
[ b[0], a[1] ]
]
#print geoToPixel( [ 0.0, 0.0 ], 0 ) == [ 128, 128 ]
#print geoToPixel( [ 0.0, 0.0 ], 1 ) == [ 256, 256 ]
#
#print geoToPixel( [ -60.0, 45.0 ], 0 ) == [ 85, 92 ]
#print geoToPixel( [ -60.0, 45.0 ], 1 ) == [ 170, 184 ]
#
#print geoToPixel( [ 0.0, 0.0 ], 13 )
| Python |
#!/usr/bin/env python
array = [
{
'abbr': 'AL',
'name': 'Alabama',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'AK',
'name': 'Alaska',
'parties': {
'dem': { 'date': '02-05', 'type': 'caucus' },
'gop': { 'date': '02-05', 'type': 'caucus' }
}
},
{
'abbr': 'AZ',
'name': 'Arizona',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'AR',
'name': 'Arkansas',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'CA',
'name': 'California',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'CO',
'name': 'Colorado',
'parties': {
'dem': { 'date': '02-05', 'type': 'caucus' },
'gop': { 'date': '02-05', 'type': 'caucus' }
}
},
{
'abbr': 'CT',
'name': 'Connecticut',
'votesby': 'town',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'DE',
'name': 'Delaware',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'DC',
'name': 'District of Columbia',
'parties': {
'dem': { 'date': '02-12' },
'gop': { 'date': '02-12' }
}
},
{
'abbr': 'FL',
'name': 'Florida',
'parties': {
'dem': { 'date': '01-29' },
'gop': { 'date': '01-29' }
}
},
{
'abbr': 'GA',
'name': 'Georgia',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'HI',
'name': 'Hawaii',
'parties': {
'dem': { 'date': '02-19', 'type': 'caucus' },
'gop': { 'date': '01-25', 'type': 'caucus' }
}
},
{
'abbr': 'ID',
'name': 'Idaho',
'parties': {
# TEMP FOR 5-27 IDAHO PRIMARY
#'dem': { 'date': '02-05', 'type': 'caucus' },
'dem': { 'date': '05-27' },
'gop': { 'date': '05-27' }
}
},
{
'abbr': 'IL',
'name': 'Illinois',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'IN',
'name': 'Indiana',
'parties': {
'dem': { 'date': '05-06' },
'gop': { 'date': '05-06' }
}
},
{
'abbr': 'IA',
'name': 'Iowa',
'parties': {
'dem': { 'date': '01-03', 'type': 'caucus' },
'gop': { 'date': '01-03', 'type': 'caucus' }
}
},
{
'abbr': 'KS',
'name': 'Kansas',
'votesby': 'district',
'parties': {
'dem': { 'date': '02-05', 'type': 'caucus' },
'gop': { 'date': '02-09', 'type': 'caucus' }
}
},
{
'abbr': 'KY',
'name': 'Kentucky',
'parties': {
'dem': { 'date': '05-20' },
'gop': { 'date': '05-20' }
}
},
{
'abbr': 'LA',
'name': 'Louisiana',
'parties': {
'dem': { 'date': '02-09' },
'gop': { 'date': '01-22', 'type': 'caucus' }
}
},
{
'abbr': 'ME',
'name': 'Maine',
'parties': {
'dem': { 'date': '02-10', 'type': 'caucus' },
'gop': { 'date': '02-01', 'type': 'caucus' }
}
},
{
'abbr': 'MD',
'name': 'Maryland',
'parties': {
'dem': { 'date': '02-12' },
'gop': { 'date': '02-12' }
}
},
{
'abbr': 'MA',
'name': 'Massachusetts',
'votesby': 'town',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'MI',
'name': 'Michigan',
'parties': {
'dem': { 'date': '01-15' },
'gop': { 'date': '01-15' }
}
},
{
'abbr': 'MN',
'name': 'Minnesota',
'parties': {
'dem': { 'date': '02-05', 'type': 'caucus' },
'gop': { 'date': '02-05', 'type': 'caucus' }
}
},
{
'abbr': 'MS',
'name': 'Mississippi',
'parties': {
'dem': { 'date': '03-11' },
'gop': { 'date': '03-11' }
}
},
{
'abbr': 'MO',
'name': 'Missouri',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'MT',
'name': 'Montana',
'parties': {
'dem': { 'date': '06-03' },
'gop': { 'date': '02-05', 'type': 'caucus' }
}
},
{
'abbr': 'NE',
'name': 'Nebraska',
'parties': {
'dem': { 'date': '02-09', 'type': 'caucus' },
'gop': { 'date': '05-13' }
}
},
{
'abbr': 'NV',
'name': 'Nevada',
'parties': {
'dem': { 'date': '01-19', 'type': 'caucus' },
'gop': { 'date': '01-19', 'type': 'caucus' }
}
},
{
'abbr': 'NH',
'name': 'New Hampshire',
'votesby': 'town',
'parties': {
'dem': { 'date': '01-08' },
'gop': { 'date': '01-08' }
}
},
{
'abbr': 'NJ',
'name': 'New Jersey',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'NM',
'name': 'New Mexico',
'parties': {
'dem': { 'date': '02-05', 'type': 'caucus' },
'gop': { 'date': '06-03' }
}
},
{
'abbr': 'NY',
'name': 'New York',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'NC',
'name': 'North Carolina',
'parties': {
'dem': { 'date': '05-06' },
'gop': { 'date': '05-06' }
}
},
{
'abbr': 'ND',
'name': 'North Dakota',
'parties': {
'dem': { 'date': '02-05', 'type': 'caucus' },
'gop': { 'date': '02-05', 'type': 'caucus' }
}
},
{
'abbr': 'OH',
'name': 'Ohio',
'parties': {
'dem': { 'date': '03-04' },
'gop': { 'date': '03-04' }
}
},
{
'abbr': 'OK',
'name': 'Oklahoma',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'OR',
'name': 'Oregon',
'parties': {
'dem': { 'date': '05-20' },
'gop': { 'date': '05-20' }
}
},
{
'abbr': 'PA',
'name': 'Pennsylvania',
'parties': {
'dem': { 'date': '04-22' },
'gop': { 'date': '04-22' }
}
},
{
'abbr': 'PR',
'name': 'Puerto Rico',
'parties': {
'dem': { 'date': '06-01' },
'gop': { 'date': '02-24' }
}
},
{
'abbr': 'RI',
'name': 'Rhode Island',
'parties': {
'dem': { 'date': '03-04' },
'gop': { 'date': '03-04' }
}
},
{
'abbr': 'SC',
'name': 'South Carolina',
'parties': {
'dem': { 'date': '01-26' },
'gop': { 'date': '01-19' }
}
},
{
'abbr': 'SD',
'name': 'South Dakota',
'parties': {
'dem': { 'date': '06-03' },
'gop': { 'date': '06-03' }
}
},
{
'abbr': 'TN',
'name': 'Tennessee',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'TX',
'name': 'Texas',
'parties': {
'dem': { 'date': '03-04' },
'gop': { 'date': '03-04' }
}
},
{
'abbr': 'UT',
'name': 'Utah',
'parties': {
'dem': { 'date': '02-05' },
'gop': { 'date': '02-05' }
}
},
{
'abbr': 'VT',
'name': 'Vermont',
'votesby': 'town',
'parties': {
'dem': { 'date': '03-04' },
'gop': { 'date': '03-04' }
}
},
{
'abbr': 'VA',
'name': 'Virginia',
'parties': {
'dem': { 'date': '02-12' },
'gop': { 'date': '02-12' }
}
},
{
'abbr': 'WA',
'name': 'Washington',
'parties': {
'dem': { 'date': '02-09', 'type': 'caucus' },
'gop': { 'date': '02-09', 'type': 'caucus' }
}
},
{
'abbr': 'WV',
'name': 'West Virginia',
'parties': {
'dem': { 'date': '05-13' },
'gop': { 'date': '05-13' }
}
},
{
'abbr': 'WI',
'name': 'Wisconsin',
'parties': {
'dem': { 'date': '02-19' },
'gop': { 'date': '02-19' }
}
},
{
'abbr': 'WY',
'name': 'Wyoming',
'parties': {
'dem': { 'date': '03-08', 'type': 'caucus' },
'gop': { 'date': '01-05', 'type': 'caucus' }
}
}
]
byAbbr = {}
for state in array:
byAbbr[ state['abbr'] ] = state
byName = {}
for state in array:
byName[ state['name'] ] = state
| Python |
#!/usr/bin/env python
# shpUtils.py
# Original version by Zachary Forest Johnson
# http://indiemaps.com/blog/index.php/code/pyShapefile.txt
# This version modified by Michael Geary
from struct import unpack
import dbfUtils
XY_POINT_RECORD_LENGTH = 16
db = []
def loadShapefile( filename ):
# open dbf file and get features as a list
global db
dbfile = open( filename[0:-4] + '.dbf', 'rb' )
db = list( dbfUtils.dbfreader(dbfile) )
dbfile.close()
fp = open( filename, 'rb' )
# get basic shapefile configuration
fp.seek(32)
filetype = readAndUnpack('i', fp.read(4))
bounds = readBounds( fp )
# fetch Records
fp.seek(100)
features = []
while True:
feature = createRecord(fp)
if feature == False: break
getPolyInfo( feature )
features.append( feature )
return { 'type': filetype, 'bounds': bounds, 'features': features }
record_class = { 0:'RecordNull', 1:'RecordPoint', 8:'RecordMultiPoint', 3:'RecordPolyLine', 5:'RecordPolygon' }
def createRecord(fp):
# read header
record_number = readAndUnpack('>L', fp.read(4))
if record_number == '': return False
content_length = readAndUnpack('>L', fp.read(4))
rectype = readAndUnpack('<L', fp.read(4))
shape = readRecordAny(fp,rectype)
shape['type'] = rectype
info = {}
names = db[0]
values = db[record_number+1]
for i in xrange(len(names)):
value = values[i]
if isinstance( value, str ):
value = value.strip()
info[ names[i] ] = value
return { 'shape':shape, 'info':info }
# Reading defs
def readRecordAny(fp, rectype):
if rectype==0:
return readRecordNull(fp)
elif rectype==1:
return readRecordPoint(fp)
elif rectype==8:
return readRecordMultiPoint(fp)
elif rectype==3 or rectype==5:
return readRecordPolyLine(fp)
else:
return False
def readRecordNull(fp):
return {}
point_count = 0
def readRecordPoint(fp):
global point_count
point = [ readAndUnpack('d', fp.read(8)), readAndUnpack('d', fp.read(8)) ]
point_count += 1
return point
def readRecordMultiPoint(fp):
shape = { 'bounds': readBounds(fp) }
points = shape['points'] = []
nPoints = readAndUnpack('i', fp.read(4))
for i in xrange(nPoints):
points.append(readRecordPoint(fp))
return shape
def readRecordPolyLine(fp):
shape = { 'bounds': readBounds(fp) }
nParts = readAndUnpack('i', fp.read(4))
nPoints = readAndUnpack('i', fp.read(4))
if readAndUnpack('i', fp.read(4)):
print 'ERROR: First part offset must be 0'
counts = []; prev = 0
for i in xrange(nParts-1):
next = readAndUnpack('i', fp.read(4))
counts.append( next - prev )
prev = next
counts.append( nPoints - prev )
parts = shape['parts'] = []
for i in xrange(nParts):
part = {}
parts.append( part )
points = part['points'] = []
for j in xrange(counts[i]):
points.append(readRecordPoint(fp))
return shape
# General defs
def readBounds(fp):
return [
[ readAndUnpack('d',fp.read(8)), readAndUnpack('d',fp.read(8)) ],
[ readAndUnpack('d',fp.read(8)), readAndUnpack('d',fp.read(8)) ]
]
def readAndUnpack(fieldtype, data):
if data=='': return data
return unpack(fieldtype, data)[0]
def getPolyInfo( feature ):
nPoints = cx = cy = 0
shape = feature['shape']
shapetype = shape['type']
if shapetype == 3 or shapetype ==5:
for part in shape['parts']:
getPartInfo( part )
def getPartInfo( part ):
points = part['points']
n = len(points)
area = cx = cy = 0
xmin = ymin = 360
xmax = ymax = -360
pt = points[n-1]; xx = pt[0]; yy = pt[1]
for pt in points:
x = pt[0]; y = pt[1]
# bounds
xmin = min( x, xmin ); ymin = min( y, ymin )
xmax = max( x, xmax ); ymax = max( y, ymax )
# area and centroid
a = xx * y - x * yy
area += a
cx += ( x + xx ) * a
cy += ( y + yy ) * a
# next
xx = x; yy = y
area /= 2
if area:
centroid = [ cx / area / 6, cy / area / 6 ]
else:
centroid = None
part.update({
'area': abs(area),
'bounds': [ [ xmin, ymin ], [ xmax, ymax ] ],
'center': [ ( xmin + xmax ) / 2, ( ymin + ymax ) / 2 ],
'centroid': centroid,
'extent': [ abs( xmax - xmin ), abs( ymax - ymin ) ]
})
def getBoundCenters(features):
for feature in features:
bounds = feature['shape']['bounds']
min = bounds[0]; max = bounds[1]
bounds['center'] = [
( min[0] + max[0] ) / 2,
( min[1] + max[1] ) / 2
]
def getMAT(features):
print 'feature not yet available'
def dumpFeatureInfo( features ):
fields = []
rows = []
for feature in features:
info = feature['info']
if not len(fields):
for key in info:
fields.append( key )
rows.append( ','.join(fields) )
cols = []
for field in fields:
cols.append( str( feature['info'][field] ) )
rows.append( ','.join(cols) )
return '\r\n'.join(rows)
| Python |
#!/usr/bin/env python
# makepolys.py
import codecs
import json
import math
import os
import random
import re
import shutil
import stat
import sys
import time
from geo import Geo
import shpUtils
import states
#states = json.load( open('states.json') )
jsonpath = 'json'
shapespath = 'shapefiles'
geo = Geo()
keysep = '|'
states.byNumber = {}
useOther = {
'CT': ( 'towns', 'cs09_d00' ),
'MA': ( 'towns', 'cs25_d00' ),
'NH': ( 'towns', 'cs33_d00' ),
'VT': ( 'towns', 'cs50_d00' ),
'KS': ( 'congressional', 'cd20_110' ),
'NE': ( 'congressional', 'cd31_110' ),
'NM': ( 'congressional', 'cd35_110' ),
}
districtNames = {
'CD1': 'First Congressional District',
'CD2': 'Second Congressional District',
'CD3': 'Third Congressional District',
'CD4': 'Fourth Congressional District',
}
def loadshapefile( filename ):
print 'Loading shapefile %s' % filename
t1 = time.time()
shapefile = shpUtils.loadShapefile( '%s/%s' %( shapespath, filename ) )
t2 = time.time()
print '%0.3f seconds load time' %( t2 - t1 )
return shapefile
#def randomColor():
# def hh(): return '%02X' %( random.random() *128 + 96 )
# return hh() + hh() + hh()
featuresByName = {}
def featureByName( feature ):
info = feature['info']
name = info['NAME']
if name not in featuresByName:
featuresByName[name] = {
'feature': feature #,
#'color': randomColor()
}
return featuresByName[name]
#def filterCONUS( features ):
# result = []
# for feature in features:
# shape = feature['shape']
# if shape['type'] != 5: continue
# info = feature['info']
# state = int(info['STATE'])
# if state == 2: continue # Alaska
# if state == 15: continue # Hawaii
# if state == 72: continue # Puerto Rico
# result.append( feature )
# return result
def featuresBounds( features ):
bounds = [ [ None, None ], [ None, None ] ]
for feature in features:
shape = feature['shape']
if shape['type'] == 5:
for part in shape['parts']:
bounds = geo.extendBounds( bounds, part['bounds'] )
return bounds
def writeFile( filename, data ):
f = open( filename, 'wb' )
f.write( data )
f.close()
def readShapefile( filename ):
print '----------------------------------------'
print 'Loading %s' % filename
shapefile = loadshapefile( filename )
features = shapefile['features']
print '%d features' % len(features)
#conus = filterCONUS( features )
#conusBounds = featuresBounds( conus )
#stateFeatures = filterCONUS( stateFeatures )
#print '%d features in CONUS states' % len(stateFeatures)
#writeFile( 'features.csv', shpUtils.dumpFeatureInfo(features) )
nPoints = nPolys = 0
places = {}
for feature in features:
shape = feature['shape']
if shape['type'] != 5: continue
info = feature['info']
name = info['NAME'].decode( 'cp850' ).encode( 'utf-8' )
name = re.sub( '^(\d+)\x00.*$', 'CD\\1', name ) # congressional district
name = districtNames.get( name, name )
state = info['STATE']
key = name + keysep + state
if key not in places:
places[key] = {
'name': name,
'state': state,
'maxarea': 0.0,
'bounds': [ [ None, None ], [ None, None ] ],
'shapes': []
}
place = places[key]
shapes = place['shapes']
for part in shape['parts']:
nPolys += 1
points = part['points']
n = len(points) - 1
nPoints += n
pts = []
area = part['area']
if area == 0: continue
bounds = part['bounds']
place['bounds'] = geo.extendBounds( place['bounds'], bounds )
centroid = part['centroid']
if area > place['maxarea']:
place['centroid'] = centroid
place['maxarea'] = area
points = part['points']
for j in xrange(n):
point = points[j]
#pts.append( '[%.4f,%.4f]' %( float(point[0]), float(point[1]) ) )
pts.append( '{x:%.4f,y:%.4f}' %( float(point[0]), float(point[1]) ) )
#shapes.append( '{area:%.4f,bounds:[[%.4f,%.4f],[%.4f,%.4f]],centroid:[%.4f,%.4f],points:[%s]}' %(
shapes.append( '{points:[%s]}' %(
#area,
#bounds[0][0], bounds[0][1],
#bounds[1][0], bounds[1][1],
#centroid[0], centroid[1],
','.join(pts)
) )
print '%d points in %d places' %( nPoints, len(places) )
return shapefile, places
def writeUS( places, path ):
json = []
keys = places.keys()
keys.sort()
for key in keys:
abbr = states.byNumber[ places[key]['state'] ]['abbr'].lower()
writeJSON( '%s.js' % abbr, getPlaceJSON( places, key, abbr, 'state' ) )
#def writeStates( places, path ):
# p = {}
# for k in places:
# if places[k] != None:
# p[k] = places[k]
# places = p
# keys = places.keys()
# keys.sort()
# for key in keys:
# name, number = key.split(keysep)
# state = states.byNumber[number]
# state['json'].append( getPlaceJSON( places, key, state['abbr'].lower(), 'county' ) )
# for state in states.array:
# writeJSON( path, state['abbr'].lower(), state['json'] )
def writeJSON( path, json ):
file = '%s/%s' %( jsonpath, path )
print 'Writing %s' % file
writeFile( file, 'GoogleElectionMap.shapeReady(%s)' %( json ) )
def getPlaceJSON( places, key, state, type ):
place = places[key]
if not place: return ''
bounds = place['bounds']
centroid = place['centroid']
return '{name:"%s", type:"%s",state:"%s",bounds:[[%.4f,%.4f],[%.4f,%.4f]],centroid:[%.4f,%.4f],shapes:[%s]}' %(
key.split(keysep)[0],
type, state,
bounds[0][0], bounds[0][1],
bounds[1][0], bounds[1][1],
centroid[0], centroid[1],
','.join(place['shapes'])
)
def generateUS( detail, path='' ):
shapefile, places = readShapefile( 'states/st99_d00_shp-%s/st99_d00.shp' % detail )
for key in places:
name, number = key.split(keysep)
state = states.byName[name]
state['json'] = []
state['counties'] = []
state['number'] = number
states.byNumber[number] = state
writeUS( places, path )
#def generateStates( detail, path ):
# shapefile, places = readShapefile( 'counties/co99_d00_shp-%s/co99_d00.shp' % detail )
# for key, place in places.iteritems():
# name, number = key.split(keysep)
# state = states.byNumber[number]
# abbr = state['abbr']
# if abbr not in useOther:
# state['counties'].append( place )
# else:
# places[key] = None
# for abbr, file in useOther.iteritems():
# state = states.byAbbr[abbr]
# number = state['number']
# othershapefile, otherplaces = readShapefile(
# '%(base)s/%(name)s_shp-%(detail)s/%(name)s.shp' %{
# 'base': file[0],
# 'name': file[1],
# 'detail': detail
# } )
# for key, place in otherplaces.iteritems():
# name, number = key.split(keysep)
# state = states.byNumber[number]
# state['counties'].append( place )
# places[key] = place
# writeStates( places, path )
#generateUS( 0, 'full' )
#generateUS( 25, '25' )
generateUS( '00' )
#generateStates( 80, 'detailed' )
print 'Done!'
| Python |
#!/usr/bin/env python
# dbfUtils.py
# By Raymond Hettinger
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362715
import struct, datetime, decimal, itertools
def dbfreader(f):
"""Returns an iterator over records in a Xbase DBF file.
The first row returned contains the field names.
The second row contains field specs: (type, size, decimal places).
Subsequent rows contain the data records.
If a record is marked as deleted, it is skipped.
File should be opened for binary reads.
"""
# See DBF format spec at:
# http://www.pgts.com.au/download/public/xbase.htm#DBF_STRUCT
numrec, lenheader = struct.unpack('<xxxxLH22x', f.read(32))
numfields = (lenheader - 33) // 32
fields = []
for fieldno in xrange(numfields):
name, typ, size, deci = struct.unpack('<11sc4xBB14x', f.read(32))
name = name.replace('\0', '') # eliminate NULs from string
fields.append((name, typ, size, deci))
yield [field[0] for field in fields]
yield [tuple(field[1:]) for field in fields]
terminator = f.read(1)
assert terminator == '\r'
fields.insert(0, ('DeletionFlag', 'C', 1, 0))
fmt = ''.join(['%ds' % fieldinfo[2] for fieldinfo in fields])
fmtsiz = struct.calcsize(fmt)
for i in xrange(numrec):
record = struct.unpack(fmt, f.read(fmtsiz))
if record[0] != ' ':
continue # deleted record
result = []
for (name, typ, size, deci), value in itertools.izip(fields, record):
if name == 'DeletionFlag':
continue
if typ == "N":
value = value.replace('\0', '').lstrip()
if value == '':
value = 0
elif deci:
value = decimal.Decimal(value)
else:
value = int(value)
elif typ == 'D':
y, m, d = int(value[:4]), int(value[4:6]), int(value[6:8])
value = datetime.date(y, m, d)
elif typ == 'L':
value = (value in 'YyTt' and 'T') or (value in 'NnFf' and 'F') or '?'
result.append(value)
yield result
def dbfwriter(f, fieldnames, fieldspecs, records):
""" Return a string suitable for writing directly to a binary dbf file.
File f should be open for writing in a binary mode.
Fieldnames should be no longer than ten characters and not include \x00.
Fieldspecs are in the form (type, size, deci) where
type is one of:
C for ascii character data
M for ascii character memo data (real memo fields not supported)
D for datetime objects
N for ints or decimal objects
L for logical values 'T', 'F', or '?'
size is the field width
deci is the number of decimal places in the provided decimal object
Records can be an iterable over the records (sequences of field values).
"""
# header info
ver = 3
now = datetime.datetime.now()
yr, mon, day = now.year-1900, now.month, now.day
numrec = len(records)
numfields = len(fieldspecs)
lenheader = numfields * 32 + 33
lenrecord = sum(field[1] for field in fieldspecs) + 1
hdr = struct.pack('<BBBBLHH20x', ver, yr, mon, day, numrec, lenheader, lenrecord)
f.write(hdr)
# field specs
for name, (typ, size, deci) in itertools.izip(fieldnames, fieldspecs):
name = name.ljust(11, '\x00')
fld = struct.pack('<11sc4xBB14x', name, typ, size, deci)
f.write(fld)
# terminator
f.write('\r')
# records
for record in records:
f.write(' ') # deletion flag
for (typ, size, deci), value in itertools.izip(fieldspecs, record):
if typ == "N":
value = str(value).rjust(size, ' ')
elif typ == 'D':
value = value.strftime('%Y%m%d')
elif typ == 'L':
value = str(value)[0].upper()
else:
value = str(value)[:size].ljust(size, ' ')
assert len(value) == size
f.write(value)
# End of file
f.write('\x1A')
| Python |
#!/usr/bin/env python
# get-strings.py
# By Michael Geary - http://mg.to/
# See UNLICENSE or http://unlicense.org/ for public domain notice.
# Reads the JSON feed for a Google Docs spreadsheet containing the
# localized strings for the Google Election Center gadget, then writes
# the strings for each language into a JSONP file for that language.
# The JSONP output has line breaks and alphabetized keys for better
# version control, e.g.:
#
# loadStrings({
# "areYouRegistered": "Are you registered to vote?",
# "dateFormat": "{{monthName}} {{dayOfMonth}}",
# "yourHome": "Your Home",
# "yourVotingLocation": "Your Voting Location"
# })
import json, re, urllib2
url = 'https://spreadsheets.google.com/feeds/list/0AuiC0EUz_p_xdHE3R2U5cTE0aFdHcWpTVVhPQVlzUmc/1/public/values?alt=json'
langs = {}
feed = json.load( urllib2.urlopen(url) )['feed']
for entry in feed['entry']:
id = entry['gsx$id']['$t']
for col in entry:
match = re.match( 'gsx\$text-(\w+)$', col )
if match:
lang = match.group( 1 )
if lang not in langs: langs[lang] = {}
langs[lang][id] = entry[col]['$t']
for lang in langs:
j = json.dumps( langs[lang], indent=0, sort_keys=True )
file = 'lang-%s.js' % lang
print 'Writing ' + file
open( file, 'wb' ).write( 'loadStrings(%s)' % j )
| Python |
#!/usr/bin/env python
# coding: utf-8
# make-hi.py - special HI processing for 2010
# Copyright (c) 2010 Michael Geary - http://mg.to/
# Use under either the MIT or GPL license
# http://www.opensource.org/licenses/mit-license.php
# http://www.opensource.org/licenses/gpl-2.0.php
import re
def convert( input, output ):
print 'Converting %s to %s' %( input, output )
input = open( input, 'r' )
output = open( output, 'wb' )
output.write( '{\n' )
for line in input:
line = line.rstrip('\n').split('\t')
if len(line) > 12:
precinct = line[10]
url = line[12]
pdfnum = re.findall( '/(\d+)EN\.pdf$', url )
if len(pdfnum):
output.write( '"%s":"%s",\n' %( precinct, pdfnum[0] ) )
output.write( '}\n' )
output.close()
input.close()
def main():
convert( 'hi-ballot-urls.tsv', 'hi-ballot-urls.json' )
print 'Done!'
if __name__ == "__main__":
main()
| Python |
#!/usr/bin/env python
import codecs
import re
import jinja2
import markdown
def process_slides():
with codecs.open('../../presentation-output.html', 'w', encoding='utf8') as outfile:
md = codecs.open('slides.md', encoding='utf8').read()
md_slides = md.split('\n---\n')
print 'Compiled %s slides.' % len(md_slides)
slides = []
# Process each slide separately.
for md_slide in md_slides:
slide = {}
sections = md_slide.split('\n\n')
# Extract metadata at the beginning of the slide (look for key: value)
# pairs.
metadata_section = sections[0]
metadata = parse_metadata(metadata_section)
slide.update(metadata)
remainder_index = metadata and 1 or 0
# Get the content from the rest of the slide.
content_section = '\n\n'.join(sections[remainder_index:])
html = markdown.markdown(content_section)
slide['content'] = postprocess_html(html, metadata)
slides.append(slide)
template = jinja2.Template(open('base.html').read())
outfile.write(template.render(locals()))
def parse_metadata(section):
"""Given the first part of a slide, returns metadata associated with it."""
metadata = {}
metadata_lines = section.split('\n')
for line in metadata_lines:
colon_index = line.find(':')
if colon_index != -1:
key = line[:colon_index].strip()
val = line[colon_index + 1:].strip()
metadata[key] = val
return metadata
def postprocess_html(html, metadata):
"""Returns processed HTML to fit into the slide template format."""
if metadata.get('build_lists') and metadata['build_lists'] == 'true':
html = html.replace('<ul>', '<ul class="build">')
html = html.replace('<ol>', '<ol class="build">')
return html
if __name__ == '__main__':
process_slides()
| Python |
#!/usr/bin/env python
## Copyright (c) 2012 The WebM project authors. All Rights Reserved.
##
## Use of this source code is governed by a BSD-style license
## that can be found in the LICENSE file in the root of the source
## tree. An additional intellectual property rights grant can be found
## in the file PATENTS. All contributing project authors may
## be found in the AUTHORS file in the root of the source tree.
##
"""Classes for representing diff pieces."""
__author__ = "jkoleszar@google.com"
import re
class DiffLines(object):
"""A container for one half of a diff."""
def __init__(self, filename, offset, length):
self.filename = filename
self.offset = offset
self.length = length
self.lines = []
self.delta_line_nums = []
def Append(self, line):
l = len(self.lines)
if line[0] != " ":
self.delta_line_nums.append(self.offset + l)
self.lines.append(line[1:])
assert l+1 <= self.length
def Complete(self):
return len(self.lines) == self.length
def __contains__(self, item):
return item >= self.offset and item <= self.offset + self.length - 1
class DiffHunk(object):
"""A container for one diff hunk, consisting of two DiffLines."""
def __init__(self, header, file_a, file_b, start_a, len_a, start_b, len_b):
self.header = header
self.left = DiffLines(file_a, start_a, len_a)
self.right = DiffLines(file_b, start_b, len_b)
self.lines = []
def Append(self, line):
"""Adds a line to the DiffHunk and its DiffLines children."""
if line[0] == "-":
self.left.Append(line)
elif line[0] == "+":
self.right.Append(line)
elif line[0] == " ":
self.left.Append(line)
self.right.Append(line)
else:
assert False, ("Unrecognized character at start of diff line "
"%r" % line[0])
self.lines.append(line)
def Complete(self):
return self.left.Complete() and self.right.Complete()
def __repr__(self):
return "DiffHunk(%s, %s, len %d)" % (
self.left.filename, self.right.filename,
max(self.left.length, self.right.length))
def ParseDiffHunks(stream):
"""Walk a file-like object, yielding DiffHunks as they're parsed."""
file_regex = re.compile(r"(\+\+\+|---) (\S+)")
range_regex = re.compile(r"@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))?")
hunk = None
while True:
line = stream.readline()
if not line:
break
if hunk is None:
# Parse file names
diff_file = file_regex.match(line)
if diff_file:
if line.startswith("---"):
a_line = line
a = diff_file.group(2)
continue
if line.startswith("+++"):
b_line = line
b = diff_file.group(2)
continue
# Parse offset/lengths
diffrange = range_regex.match(line)
if diffrange:
if diffrange.group(2):
start_a = int(diffrange.group(1))
len_a = int(diffrange.group(3))
else:
start_a = 1
len_a = int(diffrange.group(1))
if diffrange.group(5):
start_b = int(diffrange.group(4))
len_b = int(diffrange.group(6))
else:
start_b = 1
len_b = int(diffrange.group(4))
header = [a_line, b_line, line]
hunk = DiffHunk(header, a, b, start_a, len_a, start_b, len_b)
else:
# Add the current line to the hunk
hunk.Append(line)
# See if the whole hunk has been parsed. If so, yield it and prepare
# for the next hunk.
if hunk.Complete():
yield hunk
hunk = None
# Partial hunks are a parse error
assert hunk is None
| Python |
#!/usr/bin/env python
## Copyright (c) 2012 The WebM project authors. All Rights Reserved.
##
## Use of this source code is governed by a BSD-style license
## that can be found in the LICENSE file in the root of the source
## tree. An additional intellectual property rights grant can be found
## in the file PATENTS. All contributing project authors may
## be found in the AUTHORS file in the root of the source tree.
##
"""Wraps paragraphs of text, preserving manual formatting
This is like fold(1), but has the special convention of not modifying lines
that start with whitespace. This allows you to intersperse blocks with
special formatting, like code blocks, with written prose. The prose will
be wordwrapped, and the manual formatting will be preserved.
* This won't handle the case of a bulleted (or ordered) list specially, so
manual wrapping must be done.
Occasionally it's useful to put something with explicit formatting that
doesn't look at all like a block of text inline.
indicator = has_leading_whitespace(line);
if (indicator)
preserve_formatting(line);
The intent is that this docstring would make it through the transform
and still be legible and presented as it is in the source. If additional
cases are handled, update this doc to describe the effect.
"""
__author__ = "jkoleszar@google.com"
import textwrap
import sys
def wrap(text):
if text:
return textwrap.fill(text, break_long_words=False) + '\n'
return ""
def main(fileobj):
text = ""
output = ""
while True:
line = fileobj.readline()
if not line:
break
if line.lstrip() == line:
text += line
else:
output += wrap(text)
text=""
output += line
output += wrap(text)
# Replace the file or write to stdout.
if fileobj == sys.stdin:
fileobj = sys.stdout
else:
fileobj.seek(0)
fileobj.truncate(0)
fileobj.write(output)
if __name__ == "__main__":
if len(sys.argv) > 1:
main(open(sys.argv[1], "r+"))
else:
main(sys.stdin)
| Python |
#!/usr/bin/python
## Copyright (c) 2012 The WebM project authors. All Rights Reserved.
##
## Use of this source code is governed by a BSD-style license
## that can be found in the LICENSE file in the root of the source
## tree. An additional intellectual property rights grant can be found
## in the file PATENTS. All contributing project authors may
## be found in the AUTHORS file in the root of the source tree.
##
"""Performs style checking on each diff hunk."""
import getopt
import os
import StringIO
import subprocess
import sys
import diff
SHORT_OPTIONS = "h"
LONG_OPTIONS = ["help"]
TOPLEVEL_CMD = ["git", "rev-parse", "--show-toplevel"]
DIFF_CMD = ["git", "diff"]
DIFF_INDEX_CMD = ["git", "diff-index", "-u", "HEAD", "--"]
SHOW_CMD = ["git", "show"]
CPPLINT_FILTERS = ["-readability/casting", "-runtime/int"]
class Usage(Exception):
pass
class SubprocessException(Exception):
def __init__(self, args):
msg = "Failed to execute '%s'"%(" ".join(args))
super(SubprocessException, self).__init__(msg)
class Subprocess(subprocess.Popen):
"""Adds the notion of an expected returncode to Popen."""
def __init__(self, args, expected_returncode=0, **kwargs):
self._args = args
self._expected_returncode = expected_returncode
super(Subprocess, self).__init__(args, **kwargs)
def communicate(self, *args, **kwargs):
result = super(Subprocess, self).communicate(*args, **kwargs)
if self._expected_returncode is not None:
try:
ok = self.returncode in self._expected_returncode
except TypeError:
ok = self.returncode == self._expected_returncode
if not ok:
raise SubprocessException(self._args)
return result
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], SHORT_OPTIONS, LONG_OPTIONS)
except getopt.error, msg:
raise Usage(msg)
# process options
for o, _ in opts:
if o in ("-h", "--help"):
print __doc__
sys.exit(0)
if args and len(args) > 1:
print __doc__
sys.exit(0)
# Find the fully qualified path to the root of the tree
tl = Subprocess(TOPLEVEL_CMD, stdout=subprocess.PIPE)
tl = tl.communicate()[0].strip()
# See if we're working on the index or not.
if args:
diff_cmd = DIFF_CMD + [args[0] + "^!"]
else:
diff_cmd = DIFF_INDEX_CMD
# Build the command line to execute cpplint
cpplint_cmd = [os.path.join(tl, "tools", "cpplint.py"),
"--filter=" + ",".join(CPPLINT_FILTERS),
"-"]
# Get a list of all affected lines
file_affected_line_map = {}
p = Subprocess(diff_cmd, stdout=subprocess.PIPE)
stdout = p.communicate()[0]
for hunk in diff.ParseDiffHunks(StringIO.StringIO(stdout)):
filename = hunk.right.filename[2:]
if filename not in file_affected_line_map:
file_affected_line_map[filename] = set()
file_affected_line_map[filename].update(hunk.right.delta_line_nums)
# Run each affected file through cpplint
lint_failed = False
for filename, affected_lines in file_affected_line_map.iteritems():
if filename.split(".")[-1] not in ("c", "h", "cc"):
continue
if args:
# File contents come from git
show_cmd = SHOW_CMD + [args[0] + ":" + filename]
show = Subprocess(show_cmd, stdout=subprocess.PIPE)
lint = Subprocess(cpplint_cmd, expected_returncode=(0, 1),
stdin=show.stdout, stderr=subprocess.PIPE)
lint_out = lint.communicate()[1]
else:
# File contents come from the working tree
lint = Subprocess(cpplint_cmd, expected_returncode=(0, 1),
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
stdin = open(os.path.join(tl, filename)).read()
lint_out = lint.communicate(stdin)[1]
for line in lint_out.split("\n"):
fields = line.split(":")
if fields[0] != "-":
continue
warning_line_num = int(fields[1])
if warning_line_num in affected_lines:
print "%s:%d:%s"%(filename, warning_line_num,
":".join(fields[2:]))
lint_failed = True
# Set exit code if any relevant lint errors seen
if lint_failed:
return 1
except Usage, err:
print >>sys.stderr, err
print >>sys.stderr, "for help use --help"
return 2
if __name__ == "__main__":
sys.exit(main())
| Python |
#!/usr/bin/env python
## Copyright (c) 2012 The WebM project authors. All Rights Reserved.
##
## Use of this source code is governed by a BSD-style license
## that can be found in the LICENSE file in the root of the source
## tree. An additional intellectual property rights grant can be found
## in the file PATENTS. All contributing project authors may
## be found in the AUTHORS file in the root of the source tree.
##
"""Calculates the "intersection" of two unified diffs.
Given two diffs, A and B, it finds all hunks in B that had non-context lines
in A and prints them to stdout. This is useful to determine the hunks in B that
are relevant to A. The resulting file can be applied with patch(1) on top of A.
"""
__author__ = "jkoleszar@google.com"
import sys
import diff
def FormatDiffHunks(hunks):
"""Re-serialize a list of DiffHunks."""
r = []
last_header = None
for hunk in hunks:
this_header = hunk.header[0:2]
if last_header != this_header:
r.extend(hunk.header)
last_header = this_header
else:
r.extend(hunk.header[2])
r.extend(hunk.lines)
r.append("\n")
return "".join(r)
def ZipHunks(rhs_hunks, lhs_hunks):
"""Join two hunk lists on filename."""
for rhs_hunk in rhs_hunks:
rhs_file = rhs_hunk.right.filename.split("/")[1:]
for lhs_hunk in lhs_hunks:
lhs_file = lhs_hunk.left.filename.split("/")[1:]
if lhs_file != rhs_file:
continue
yield (rhs_hunk, lhs_hunk)
def main():
old_hunks = [x for x in diff.ParseDiffHunks(open(sys.argv[1], "r"))]
new_hunks = [x for x in diff.ParseDiffHunks(open(sys.argv[2], "r"))]
out_hunks = []
# Join the right hand side of the older diff with the left hand side of the
# newer diff.
for old_hunk, new_hunk in ZipHunks(old_hunks, new_hunks):
if new_hunk in out_hunks:
continue
old_lines = old_hunk.right
new_lines = new_hunk.left
# Determine if this hunk overlaps any non-context line from the other
for i in old_lines.delta_line_nums:
if i in new_lines:
out_hunks.append(new_hunk)
break
if out_hunks:
print FormatDiffHunks(out_hunks)
sys.exit(1)
if __name__ == "__main__":
main()
| Python |
#!/usr/bin/python
import getopt
import subprocess
import sys
LONG_OPTIONS = ["shard=", "shards="]
BASE_COMMAND = "./configure --enable-internal-stats --enable-experimental"
def RunCommand(command):
run = subprocess.Popen(command, shell=True)
output = run.communicate()
if run.returncode:
print "Non-zero return code: " + str(run.returncode) + " => exiting!"
sys.exit(1)
def list_of_experiments():
experiments = []
configure_file = open("configure")
list_start = False
for line in configure_file.read().split("\n"):
if line == 'EXPERIMENT_LIST="':
list_start = True
elif line == '"':
list_start = False
elif list_start:
currently_broken = ["csm"]
experiment = line[4:]
if experiment not in currently_broken:
experiments.append(experiment)
return experiments
def main(argv):
# Parse arguments
options = {"--shard": 0, "--shards": 1}
if "--" in argv:
opt_end_index = argv.index("--")
else:
opt_end_index = len(argv)
try:
o, _ = getopt.getopt(argv[1:opt_end_index], None, LONG_OPTIONS)
except getopt.GetoptError, err:
print str(err)
print "Usage: %s [--shard=<n> --shards=<n>] -- [configure flag ...]"%argv[0]
sys.exit(2)
options.update(o)
extra_args = argv[opt_end_index + 1:]
# Shard experiment list
shard = int(options["--shard"])
shards = int(options["--shards"])
experiments = list_of_experiments()
base_command = " ".join([BASE_COMMAND] + extra_args)
configs = [base_command]
configs += ["%s --enable-%s" % (base_command, e) for e in experiments]
my_configs = zip(configs, range(len(configs)))
my_configs = filter(lambda x: x[1] % shards == shard, my_configs)
my_configs = [e[0] for e in my_configs]
# Run configs for this shard
for config in my_configs:
test_build(config)
def test_build(configure_command):
print "\033[34m\033[47mTesting %s\033[0m" % (configure_command)
RunCommand(configure_command)
RunCommand("make clean")
RunCommand("make")
if __name__ == "__main__":
main(sys.argv)
| Python |
"""
* Copyright (c) 2012 The WebM project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
"""
#!/usr/bin/env python
import sys,string,os,re,math,numpy
scale = 2**16
def dist(p1,p2):
x1,y1 = p1
x2,y2 = p2
if x1==x2 and y1==y2 :
return 1.0
return 1/ math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))
def gettaps(p):
def l(b):
return int(math.floor(b))
def h(b):
return int(math.ceil(b))
def t(b,p,s):
return int((scale*dist(b,p)+s/2)/s)
r,c = p
ul=[l(r),l(c)]
ur=[l(r),h(c)]
ll=[h(r),l(c)]
lr=[h(r),h(c)]
sum = dist(ul,p)+dist(ur,p)+dist(ll,p)+dist(lr,p)
t4 = scale - t(ul,p,sum) - t(ur,p,sum) - t(ll,p,sum);
return [[ul,t(ul,p,sum)],[ur,t(ur,p,sum)],
[ll,t(ll,p,sum)],[lr,t4]]
def print_mb_taps(angle,blocksize):
theta = angle / 57.2957795;
affine = [[math.cos(theta),-math.sin(theta)],
[math.sin(theta),math.cos(theta)]]
radius = (float(blocksize)-1)/2
print " // angle of",angle,"degrees"
for y in range(blocksize) :
for x in range(blocksize) :
r,c = numpy.dot(affine,[y-radius, x-radius])
tps = gettaps([r+radius,c+radius])
for t in tps :
p,t = t
tr,tc = p
print " %2d, %2d, %5d, " % (tr,tc,t,),
print " // %2d,%2d " % (y,x)
i=float(sys.argv[1])
while i <= float(sys.argv[2]) :
print_mb_taps(i,float(sys.argv[4]))
i=i+float(sys.argv[3])
"""
taps = []
pt=dict()
ptr=dict()
for y in range(16) :
for x in range(16) :
r,c = numpy.dot(affine,[y-7.5, x-7.5])
tps = gettaps([r+7.5,c+7.5])
j=0
for tp in tps :
p,i = tp
r,c = p
pt[y,x,j]= [p,i]
try:
ptr[r,j,c].append([y,x])
except:
ptr[r,j,c]=[[y,x]]
j = j+1
for key in sorted(pt.keys()) :
print key,pt[key]
lr = -99
lj = -99
lc = 0
shuf=""
mask=""
for r,j,c in sorted(ptr.keys()) :
for y,x in ptr[r,j,c] :
if lr != r or lj != j :
print "shuf_"+str(lr)+"_"+str(lj)+"_"+shuf.ljust(16,"0"), lc
shuf=""
lc = 0
for i in range(lc,c-1) :
shuf = shuf +"0"
shuf = shuf + hex(x)[2]
lc =c
break
lr = r
lj = j
# print r,j,c,ptr[r,j,c]
# print
for r,j,c in sorted(ptr.keys()) :
for y,x in ptr[r,j,c] :
print r,j,c,y,x
break
"""
| Python |
#lots to do here
##[Unit11213]
##Name=NONE
##Direction=2
##NumAtoms=11
##NumCells=11
##UnitNumber=11213
##UnitType=3
##NumberBlocks=1
##
##[Unit11213_Block1]
##Name=SRS_LIKE26_s_at
##BlockNumber=1
##NumAtoms=11
##NumCells=11
##StartPosition=0
##StopPosition=10
##CellHeader=X Y PROBE FEAT QUAL EXPOS POS CBASE PBASE TBASE ATOM INDEX CODONIND CODON REGIONTYPE REGION
##Cell1=403 355 N control SRS_LIKE26_s_at 0 13 C G C 0 170093 -1 -1 99
##Cell2=185 69 N control SRS_LIKE26_s_at 1 13 A T A 1 33167 -1 -1 99
##Cell3=27 62 N control SRS_LIKE26_s_at 2 13 C G C 2 29663 -1 -1 99
##Cell4=420 10 N control SRS_LIKE26_s_at 3 13 T A T 3 5200 -1 -1 99
##Cell5=155 29 N
##
##X - The X coordinate of the cell.
##Y - The Y coordinate of the cell.
##PROBE - The probe sequence of the cell. Typically set to "N".
##FEAT - Unused string.
##QUAL - The same as the block name.
##EXPOS - Ranges from 0 to the number of atoms - 1 for expression arrays. For CustomSeq it provides some positional information for the probe.
##POS - An index to the base position within the probe where the mismatch occurs.
##CBASE - Not used.
##PBASE - The probe base at the substitution position.
##TBASE - The base of the target where the probe interrogates at the substitution position.
##ATOM - An index used to group probe pairs or quartets.
##INDEX - An index used to look up the corresponding cell data in the CEL file.
##CODONIND - Always set to -1. Only available in version 2 and above.
##CODON -Always set to -1. Only available in version 2 and above.
##REGIONTYPE - Always set to 99. Only available in version 2 and above.
##REGION - Always set to a blank character. Only available in version 2 and above.
##Celli This con
class affycdf():
read_tag = dict(inttype = int(rowpartition[2].strip()),
stringtype = str(rowpartition[2].strip()),
tupletype = tuple(row[1:])
)
header_data_types =dict(X=int,
Y=int,
PROBE=str,
FEAT=str,
QUAL=str,
EXPOS=int,
POS=int,
CBASE=str,
PBASE=str,
TBASE=str,
ATOM=int,
INDEX=int,
CODONIND=str,
CODON=str,
REGIONTYPE=str,
REGION=str,
Celli=tuple,
PROB=str,
PLEN=int,
INDEX=int,
MATCH=bool,
BG=bool,
CYCLES=tuple,
)
# some of he data elements appear in multiple section of the file. The definition for each is listed.
## If there is a different method needed to parse the two different occurances it is specified
alltags=dict()
#Version The version number. Should always be set to "GC1.0", "GC2.0" or "GC3.0". This document only describes GC3.0 version CDF files.
alltags[Version] = read_tag['stringtype']
#Type Defines the type of QC probe set. The defined types are:
alltags['Type'] = read_tag['inttype']
#NumCells The number of cells in the block.
alltags['NumberCells'] = read_tag['inttype']
# [QC], CellHeader Defines the data contained in the subsequent lines, separated by tabs. The value depends on the type of unit. The possible values are:
# [Unit] CellHeader Defines the data contained in the subsequent lines, separated by tabs. The values are:
alltags['CellHeader'] = header_reader
alltags['Cell'] = read_data_row # Data rows start with "CELL" the number of colums is set by the header, Parsing is the same, /t seperated values
# [CHIP], Name This item is not used by the software.
# [UNIT], Name The name of the unit or "NONE" for expression units.
# [Unit_Block],Name The name of the block. For expression units this is the name of the probe set.
alltags['name'] = read_tag['stringtype']
# Direction Defines if the probes are interrogating a sense target or anti-sense target (1 - sense, 2 - anti-sense).
# Direction Defines if the probes are interrogating a sense target or anti-sense target (0 - no direction, 1 - sense, 2 - anti-sense).
##This value is available in version 3 and above.
alltags['Direction'] = read_tag['inttype']
#1, NumAtoms The number of atoms (probe pairs for expression and genotyping and probe quartets for CustomSeq) in the entire probe set.
##This TAG name contain two values after the equal sign. The first is the number of atoms and the second (if found) is the number of cells in each atom.
##It is assumed that there are 2 cells per atom for expression probe sets and 4 for CustomSeq probe sets.
#2, NumAtoms The number of atoms (probe pairs for expression and genotyping and probe quartets for CustomSeq) in the block.
alltags['NumAtoms'] = read_tag['inttype']
#NumCells The number of cells in the entire probe set.
alltags['NumCells'] = read_tag['inttype']
#StartPosition The position of the first atom.
alltags['StartPosition'] = read_tag['inttype']
#StopPosition The position of the last atom.
alltags['StopPosition'] = read_tag['inttype']
#UnitNumber An arbitrary index value for the probe set.
alltags['UnitNumber'] = read_tag['inttype']
#UnitType Defines the type of unit (1 - CustomSeq, 2 - genotyping, 3 - expression, 7 - tag/GenFlex).
alltags['UnitType'] = read_tag['inttype']
#NumberBlocks The number of blocks or groups in the probe set.
alltags['NumberBlocks'] = read_tag['inttype']
#MutationType Used for CYP450 arrays only in defining the type of polymorphism (0 - substitution, 1 - insertion, 2 - deletion).
##This value is available in version 2 and above and only if the UnitType=2.
alltags['MutationType'] = read_tag['inttype']
#BlockNumber An index to the block.
alltags['BlockNumber'] = read_tag['inttype']
#Rows The number of rows of cells on the array.
alltags['Rows'] = read_tag['inttype']
#Cols The number of columns of cells on the array.
alltags['Cols'] = read_tag['inttype']
#NumberOfUnits The number of units in the array not including QC units. The term unit is an internal term which means probe set.
alltags['NumberOfUnits'] = read_tag['inttype']
#MaxUnit Each unit is given a unique number. This value is the maximum of the unit numbers of all the units in the array (not including QC units).
alltags['MaxUnit'] = read_tag['inttype']
#NumQCUnits The number of QC units. QC units are defined in version 2 and above.
alltags['NumQCUnits'] = read_tag['inttype']
#ChipReference Used for CustomSeq, HIV and P53 arrays only. This is the reference sequence displayed by the Affymetrix software.
##The sequence may contain spaces. This value is defined for version 2 and above.
alltags['ChipReference'] = read_tag['tupletype']
#The CDF file describes the layout for an Affymetrix GeneChip array. There are two formats of this file --
##the first is an ASCII text format used by the MAS and GCOS 1.0 software and the second is a binary format used by later versions of GCOS - this format is also referred to as the XDA format.
alltags['CDF']= section_reader(row, '[CDF]')
#The "Chip" section
alltags['Chip']= section_reader(row, '[Chip]')
#"QC" defines the QC units or probe sets in the array. There are NumQCUnits (from the Chip section) QC sections.
##Each section name is a combination of "QC" and an index ranging from 1 to NumQCUnits-1. QC units are defined for version 2 and above.
alltags['QC'] = section_reader(row, 'QC')
#"Unit" defines the probes that are a member of the unit (probe set).
##Each probe set is divided into subsections termed "Blocks" which are referred to as "groups" in the Files SDK documentation.
##A group will list the probes as its members. For expression probe sets there is 1 and only 1 group per probe set.
##Each section name is a combination of "Unit" and an index. There is no meaning to the index value.
##Immediately following the "Unit" section there will be the "Block" sections for that unit before the next unit is defined.
alltags['Unit'] = section_reader(row, '[Unit]')
#There are as many "Unit_Block" sections as defined in the "Unit" section.
##Removed becuase it is inclueded in reading in the "Unit" data
##alltags['Unit_Block'] = section_reader(row, '[Unit_Block]')
def section_reader(row, section):
if section == 'CDF':
row = reader.next()
#self.alltags[str(row)] == alltags[str(row)](row)
elif sectionn == 'Chip':
elif section == 'QC'
elif section == 'Unit'
def read_cdf(self, file):
reader = csv.reader(open(filename, "U"),delimiter='\t')
self.filename = os.path.split(filename)[1]
for row in reader:
if row: #skip blank rows
alltags[row[0].partition('=').strip(' []1234567890] ')[0]](row) #Looks up the correct way to read the line based on the tag. This work for all line, ie if there is no "=" no error is raised
rows =[]
for row in range(NumCells):
rows.append(tuple(row))
celldata = np.array(rows, [(col,row_data_type[str(col)] for col in CellHeader)])
| Python |
import csv, os, glob
import sys
import numpy
class affycel:
def _int_(self, filename, version, header, intensityCells, intensity, maskscells, masks, outlierCells, outliers, modifiedCells, modified):
self.filename = filename
self.version = version
self.header = {}
self.intensityCells = intensityCells
self.intensity = intensity
self.masksCells = maskscells
self.masks = masks
self.outliersCells = outlierCells
self.outliers = outliers
self.modifiedCells = modifiedCells
self.modified = modified
self.custom = {} # plan to allow a custom section to be added to the CEL file
def read_cel(self, filename):
reader = csv.reader(open(filename, "U"),delimiter='\t')
self.filename = os.path.split(filename)[1]
def read_selector(areader):
for row in areader:
if row:
if any(("[CEL]" in row, "[HEADER]" in row, "[INTENSITY]" in row, "[MASKS]" in row, "[OUTLIERS]" in row, "[MODIFIED]" in row)):
rsel[row[0]](row, areader)
else: print '*****something went wrong*******'
def Rcel(row, areader):
if '[CEL]' in row: #row passed in should contain '[CEL]'
for row in areader: #Skips '[CEL]' row that was passed in
if row: # skips blank rows
#print 'cell', row
if not any(("[HEADER]" in row, "[INTENSITY]" in row, "[MASKS]" in row, "[OUTLIERS]" in row, "[MODIFIED]" in row)):
self.version = int(row[0].partition('=')[2])
#print self.version
#self.version = row
else:
rsel[row[0]](row, areader) # Go to correct section
def Rheader(row, areader):
if '[HEADER]' in row: #row passed in should contain '[HEADER]'
self.header = {} #self.header is a dictionary
for row in reader: # skips the section heading row
if row: #skips blank rows
if not any(("[CEL]" in row, "[INTENSITY]" in row, "[MASKS]" in row, "[OUTLIERS]" in row, "[MODIFIED]" in row)):
self.header[str(row[0].partition('=')[0])] = str(row[0].partition('=')[2])
else:
rsel[row[0]](row, areader) # Go to correct section
def Rintensity(row, areader):
#print 'start intencity', row
data = []
if "[INTENSITY]" in row: #row passed in should contain '[INTENSITY]'
row = areader.next() # moves to the row after "[INTENSITY]"
self.intensityCells = int(row[0].partition('=')[2]) #gets the number of intensities
areader.next() #skips the colmn headings
for row in reader:
if row:
if not any(("[CEL]" in row, "[HEADER]" in row, "[MASKS]" in row, "[OUTLIERS]" in row, "[MODIFIED]" in row)):
data.append(tuple(row))
else:
self.intensity = numpy.array(data, [('x',numpy.int),('y',numpy.int),('mean',numpy.float64),('stdv',numpy.float64),('npixcels',numpy.int)])
rsel[row[0]](row, areader)
def Rmasks(row, areader):
data = []
maskstype = [('x', int), ('y', int)]
if "[MASKS]" in row:
row = areader.next() # moves to the row after "[INTENSITY]"
self.masksCells = int(row[0].partition('=')[2]) #gets the number of intensities
areader.next() #skips the colmn headings
for row in reader:
if row:
if not any(("[CEL]" in row, "[HEADER]" in row, "[INTESITY]" in row, "[OUTLIERS]" in row, "[MODIFIED]" in row)):
data.append(tuple(row))
else:
self.masks = numpy.array(data, [('x',numpy.int),('y',numpy.int)])
rsel[row[0]](row, areader)
def Routliers(row, areader):
data = []
if "[OUTLIERS]" in row:
row = areader.next() # moves to the row after "[INTENSITY]"
self.outliersCells = int(row[0].partition('=')[2]) #gets the number of intensities
areader.next() #skips the colmn headings
for row in reader:
if row:
if not any(("[CEL]" in row, "[HEADER]" in row, "[INTESITY]" in row, "[MASKS]" in row, "[MODIFIED]" in row)):
data.append(tuple(row))
else:
self.outliers = numpy.array(data, [('x', numpy.int), ('y', numpy.int)])
rsel[row[0]](row, areader)
def Rmodified(row, areader):
data = []
if "[MODIFIED]" in row:
row = areader.next() # moves to the row after "[INTENSITY]"
self.modifiedCells = int(row[0].partition('=')[2]) #gets the number of intensities
areader.next() #skips the colmn headings
for row in reader:
if row:
if not any(("[CEL]" in row, "[HEADER]" in row, "[INTESITY]" in row, "[MASKS]" in row, "[OUTLIERS]" in row)):
print 'modified1'
data.append(tuple(row))
#else, there is no else statment when there are now more rows continue on to convert data to array
self.modified = numpy.array(data, [('x', numpy.int), ('y', numpy.int), ('origmean', numpy.float64)] )
#rsel[row[0]](row, areader) This should be the last item in the file
rsel = {}
rsel['[CEL]'] = Rcel
rsel['[HEADER]']= Rheader
rsel['[INTENSITY]']= Rintensity
rsel['[MASKS]']= Rmasks
rsel['[OUTLIERS]']= Routliers
rsel['[MODIFIED]']= Rmodified
read_selector(reader)
def simple_normilize(self):
"""empty"""
def cmean(self):
return numpy.mean(self.intensity['mean'])
def csum(self):
return numpy.sum(self.intensity['mean'])
def csumDobs(self):
return self.csum()/len(self.intensity['mean'])
if __name__ == "__main__":
a = affycel()
a.read_cel('example.CEL')
print a.cmean()
print a.csum()
print a.csumDobs()
testlist = (a.filename, a.version, a.header.items(), a.intensityCells, a.intensity[:5], a.masksCells, a.masks, a.outliersCells, a.outliers[:5], a.modifiedCells, a.modified[:5])
for test in testlist:
print 'Test', test
| Python |
import web
import json
from base import Base
class XTask(Base):
def GET(self):
results = self.db.query("SELECT * FROM Pet")
return self.query_serializer(results) | Python |
import web
import json
from datetime import datetime
#thanks: http://stackoverflow.com/questions/11875770/how-to-overcome-datetime-datetime-not-json-serializable-in-python
class DateTimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat()
return json.JSONEncoder.default(self, obj)
class Base:
"""A base class that contains a static mysql database connector that connects to our database in the CoC"""
db = web.database(
dbn="mysql",
host="academic-mysql.cc.gatech.edu",
db="cs4400_Team_11",
user="cs4400_Team_11",
pw="w01goZoX"
)
def query_serializer(self, query):
query = [dict(result) for result in query]
query = DateTimeEncoder().encode(query)
return query
| Python |
import web
import time
from base import Base
class AddPet(Base):
def POST(self):
input = web.input()
input["age"] = int(input["age"])
print(input)
input = dict(input)
results = self.db.query("INSERT into Pet(Shelter_Name, Age, Gender, Pet_Name) values ($shelter_name, $age, $gender, $pet_name)",
vars=input
)
return str(results)
| Python |
import web
from addpet import AddPet
from xtask import XTask
urls = (
'/addpet', 'AddPet',
'/xtask', 'XTask' #sample task
)
app = web.application(urls, globals())
if __name__ == "__main__":
app.run() | Python |
from sys import stdin
a, b, c = map(int, stdin.readline().strip().split())
print "%.3lf" % ((a+b+c)/3.0)
| Python |
from sys import stdin
from math import *
r, h = map(float, stdin.readline().strip().split())
print "Area = %.3lf" % (pi*r*r*2 + 2*pi*r*h)
| Python |
from sys import stdin
from math import *
n, = map(int, stdin.readline().strip().split())
rad = radians(n)
print "%.3lf %.3lf" % (sin(rad), cos(rad))
| Python |
from sys import stdin
n, = map(int, stdin.readline().strip().split())
print n*(n+1)/2
| Python |
from sys import stdin
a, b = map(int, stdin.readline().strip().split())
print b, a
| Python |
from sys import stdin
n, m = map(int, stdin.readline().strip().split())
a = (4*n-m)/2
b = n-a
if m % 2 == 1 or a < 0 or b < 0: print "No answer"
else: print a, b
| Python |
from sys import stdin
n, = map(int, stdin.readline().strip().split())
money = n * 95
if money >= 300: money *= 0.85
print "%.2lf" % money
| Python |
from sys import stdin
n, = map(int, stdin.readline().strip().split())
print ["yes", "no"][n % 2]
| Python |
from sys import stdin
from math import *
x1, y1, x2, y2 = map(float, stdin.readline().strip().split())
print "%.3lf" % hypot((x1-x2), (y1-y2))
| Python |
from sys import stdin
n = stdin.readline().strip().split()[0]
print '%c%c%c' % (n[2], n[1], n[0])
| Python |
from sys import stdin
x, = map(float, stdin.readline().strip().split())
print abs(x)
| Python |
from sys import stdin
from calendar import isleap
year, = map(int, stdin.readline().strip().split())
if isleap(year): print "yes"
else: print "no"
| Python |
from sys import stdin
f, = map(float, stdin.readline().strip().split())
print "%.3lf" % (5*(f-32)/9)
| Python |
from sys import stdin
a, b, c = map(int, stdin.readline().strip().split())
if a*a + b*b == c*c or a*a + c*c == b*b or b*b + c*c == a*a: print "yes"
elif a + b <= c or a + c <= b or b + c <= a: print "not a triangle"
else: print "no"
| Python |
from sys import stdin
a = map(int, stdin.readline().strip().split())
a.sort()
print a[0], a[1], a[2]
| Python |
s = i = 0
while True:
term = 1.0 / (i*2+1)
s += term * ((-1)**i)
if term < 1e-6: break
i += 1
print "%.6lf" % s
| Python |
from sys import stdin
from decimal import *
a, b, c = map(int, stdin.readline().strip().split())
getcontext().prec = c
print Decimal(a) / Decimal(b)
| Python |
from sys import stdin
n = int(stdin.readline().strip())
print "%.3lf" % sum([1.0/x for x in range(1,n+1)])
| Python |
from sys import stdin
print len(stdin.readline().strip())
| Python |
from itertools import product
from math import *
def issqrt(n):
s = int(floor(sqrt(n)))
return s*s == n
aabb = [a*1100+b*11 for a,b in product(range(1,10),range(10))]
print ' '.join(map(str, filter(issqrt, aabb)))
| Python |
from sys import stdin
a = map(int, stdin.readline().strip().split())
print "%d %d %.3lf" % (min(a), max(a), float(sum(a)) / len(a))
| Python |
from sys import stdin
def cycle(n):
if n == 1: return 0
elif n % 2 == 1: return cycle(n*3+1) + 1
else: return cycle(n/2) + 1
n = int(stdin.readline().strip())
print cycle(n)
| Python |
from sys import stdin
n = int(stdin.readline().strip())
count = n*2-1
for i in range(n):
print ' '*i + '#'*count
count -= 2
| Python |
for abc in range(123, 329):
big = str(abc) + str(abc*2) + str(abc*3)
if(''.join(sorted(big)) == '123456789'): print abc, abc*2, abc*3
| Python |
from sys import stdin
n, m = map(int, stdin.readline().strip().split())
print "%.5lf" % sum([1.0/i/i for i in range(n,m+1)])
| Python |
from sys import stdin
data = map(int, stdin.readline().strip().split())
n, m = data[0], data[-1]
data = data[1:-1]
print len(filter(lambda x: x < m, data))
| Python |
from sys import stdin
def solve(a, b, c):
for i in range(10, 101):
if i % 3 == a and i % 5 == b and i % 7 == c:
print i
return
print 'No answer'
a, b, c = map(int, stdin.readline().strip().split())
solve(a, b, c)
| Python |
from itertools import product
sol = [a*100+b*10+c for a,b,c in product(range(1,10), range(10), range(10)) if a**3+b**3+c**3 == a*100+b*10+c]
print '\n'.join(map(str, sol))
| Python |
from sys import stdin
from math import *
n = int(stdin.readline().strip())
print sum(map(factorial, range(1,n+1))) % (10**6)
| Python |
'''
Module which brings history information about files from Mercurial.
@author: Rodrigo Damazio
'''
import re
import subprocess
REVISION_REGEX = re.compile(r'(?P<hash>[0-9a-f]{12}):.*')
def _GetOutputLines(args):
'''
Runs an external process and returns its output as a list of lines.
@param args: the arguments to run
'''
process = subprocess.Popen(args,
stdout=subprocess.PIPE,
universal_newlines = True,
shell = False)
output = process.communicate()[0]
return output.splitlines()
def FillMercurialRevisions(filename, parsed_file):
'''
Fills the revs attribute of all strings in the given parsed file with
a list of revisions that touched the lines corresponding to that string.
@param filename: the name of the file to get history for
@param parsed_file: the parsed file to modify
'''
# Take output of hg annotate to get revision of each line
output_lines = _GetOutputLines(['hg', 'annotate', '-c', filename])
# Create a map of line -> revision (key is list index, line 0 doesn't exist)
line_revs = ['dummy']
for line in output_lines:
rev_match = REVISION_REGEX.match(line)
if not rev_match:
raise 'Unexpected line of output from hg: %s' % line
rev_hash = rev_match.group('hash')
line_revs.append(rev_hash)
for str in parsed_file.itervalues():
# Get the lines that correspond to each string
start_line = str['startLine']
end_line = str['endLine']
# Get the revisions that touched those lines
revs = []
for line_number in range(start_line, end_line + 1):
revs.append(line_revs[line_number])
# Merge with any revisions that were already there
# (for explict revision specification)
if 'revs' in str:
revs += str['revs']
# Assign the revisions to the string
str['revs'] = frozenset(revs)
def DoesRevisionSuperceed(filename, rev1, rev2):
'''
Tells whether a revision superceeds another.
This essentially means that the older revision is an ancestor of the newer
one.
This also returns True if the two revisions are the same.
@param rev1: the revision that may be superceeding the other
@param rev2: the revision that may be superceeded
@return: True if rev1 superceeds rev2 or they're the same
'''
if rev1 == rev2:
return True
# TODO: Add filename
args = ['hg', 'log', '-r', 'ancestors(%s)' % rev1, '--template', '{node|short}\n', filename]
output_lines = _GetOutputLines(args)
return rev2 in output_lines
def NewestRevision(filename, rev1, rev2):
'''
Returns which of two revisions is closest to the head of the repository.
If none of them is the ancestor of the other, then we return either one.
@param rev1: the first revision
@param rev2: the second revision
'''
if DoesRevisionSuperceed(filename, rev1, rev2):
return rev1
return rev2 | Python |
#!/usr/bin/python
'''
Entry point for My Tracks i18n tool.
@author: Rodrigo Damazio
'''
import mytracks.files
import mytracks.translate
import mytracks.validate
import sys
def Usage():
print 'Usage: %s <command> [<language> ...]\n' % sys.argv[0]
print 'Commands are:'
print ' cleanup'
print ' translate'
print ' validate'
sys.exit(1)
def Translate(languages):
'''
Asks the user to interactively translate any missing or oudated strings from
the files for the given languages.
@param languages: the languages to translate
'''
validator = mytracks.validate.Validator(languages)
validator.Validate()
missing = validator.missing_in_lang()
outdated = validator.outdated_in_lang()
for lang in languages:
untranslated = missing[lang] + outdated[lang]
if len(untranslated) == 0:
continue
translator = mytracks.translate.Translator(lang)
translator.Translate(untranslated)
def Validate(languages):
'''
Computes and displays errors in the string files for the given languages.
@param languages: the languages to compute for
'''
validator = mytracks.validate.Validator(languages)
validator.Validate()
error_count = 0
if (validator.valid()):
print 'All files OK'
else:
for lang, missing in validator.missing_in_master().iteritems():
print 'Missing in master, present in %s: %s:' % (lang, str(missing))
error_count = error_count + len(missing)
for lang, missing in validator.missing_in_lang().iteritems():
print 'Missing in %s, present in master: %s:' % (lang, str(missing))
error_count = error_count + len(missing)
for lang, outdated in validator.outdated_in_lang().iteritems():
print 'Outdated in %s: %s:' % (lang, str(outdated))
error_count = error_count + len(outdated)
return error_count
if __name__ == '__main__':
argv = sys.argv
argc = len(argv)
if argc < 2:
Usage()
languages = mytracks.files.GetAllLanguageFiles()
if argc == 3:
langs = set(argv[2:])
if not langs.issubset(languages):
raise 'Language(s) not found'
# Filter just to the languages specified
languages = dict((lang, lang_file)
for lang, lang_file in languages.iteritems()
if lang in langs or lang == 'en' )
cmd = argv[1]
if cmd == 'translate':
Translate(languages)
elif cmd == 'validate':
error_count = Validate(languages)
else:
Usage()
error_count = 0
print '%d errors found.' % error_count
| Python |
'''
Module which prompts the user for translations and saves them.
TODO: implement
@author: Rodrigo Damazio
'''
class Translator(object):
'''
classdocs
'''
def __init__(self, language):
'''
Constructor
'''
self._language = language
def Translate(self, string_names):
print string_names | Python |
'''
Module which compares languague files to the master file and detects
issues.
@author: Rodrigo Damazio
'''
import os
from mytracks.parser import StringsParser
import mytracks.history
class Validator(object):
def __init__(self, languages):
'''
Builds a strings file validator.
Params:
@param languages: a dictionary mapping each language to its corresponding directory
'''
self._langs = {}
self._master = None
self._language_paths = languages
parser = StringsParser()
for lang, lang_dir in languages.iteritems():
filename = os.path.join(lang_dir, 'strings.xml')
parsed_file = parser.Parse(filename)
mytracks.history.FillMercurialRevisions(filename, parsed_file)
if lang == 'en':
self._master = parsed_file
else:
self._langs[lang] = parsed_file
self._Reset()
def Validate(self):
'''
Computes whether all the data in the files for the given languages is valid.
'''
self._Reset()
self._ValidateMissingKeys()
self._ValidateOutdatedKeys()
def valid(self):
return (len(self._missing_in_master) == 0 and
len(self._missing_in_lang) == 0 and
len(self._outdated_in_lang) == 0)
def missing_in_master(self):
return self._missing_in_master
def missing_in_lang(self):
return self._missing_in_lang
def outdated_in_lang(self):
return self._outdated_in_lang
def _Reset(self):
# These are maps from language to string name list
self._missing_in_master = {}
self._missing_in_lang = {}
self._outdated_in_lang = {}
def _ValidateMissingKeys(self):
'''
Computes whether there are missing keys on either side.
'''
master_keys = frozenset(self._master.iterkeys())
for lang, file in self._langs.iteritems():
keys = frozenset(file.iterkeys())
missing_in_master = keys - master_keys
missing_in_lang = master_keys - keys
if len(missing_in_master) > 0:
self._missing_in_master[lang] = missing_in_master
if len(missing_in_lang) > 0:
self._missing_in_lang[lang] = missing_in_lang
def _ValidateOutdatedKeys(self):
'''
Computers whether any of the language keys are outdated with relation to the
master keys.
'''
for lang, file in self._langs.iteritems():
outdated = []
for key, str in file.iteritems():
# Get all revisions that touched master and language files for this
# string.
master_str = self._master[key]
master_revs = master_str['revs']
lang_revs = str['revs']
if not master_revs or not lang_revs:
print 'WARNING: No revision for %s in %s' % (key, lang)
continue
master_file = os.path.join(self._language_paths['en'], 'strings.xml')
lang_file = os.path.join(self._language_paths[lang], 'strings.xml')
# Assume that the repository has a single head (TODO: check that),
# and as such there is always one revision which superceeds all others.
master_rev = reduce(
lambda r1, r2: mytracks.history.NewestRevision(master_file, r1, r2),
master_revs)
lang_rev = reduce(
lambda r1, r2: mytracks.history.NewestRevision(lang_file, r1, r2),
lang_revs)
# If the master version is newer than the lang version
if mytracks.history.DoesRevisionSuperceed(lang_file, master_rev, lang_rev):
outdated.append(key)
if len(outdated) > 0:
self._outdated_in_lang[lang] = outdated
| Python |
'''
Module for dealing with resource files (but not their contents).
@author: Rodrigo Damazio
'''
import os.path
from glob import glob
import re
MYTRACKS_RES_DIR = 'MyTracks/res'
ANDROID_MASTER_VALUES = 'values'
ANDROID_VALUES_MASK = 'values-*'
def GetMyTracksDir():
'''
Returns the directory in which the MyTracks directory is located.
'''
path = os.getcwd()
while not os.path.isdir(os.path.join(path, MYTRACKS_RES_DIR)):
if path == '/':
raise 'Not in My Tracks project'
# Go up one level
path = os.path.split(path)[0]
return path
def GetAllLanguageFiles():
'''
Returns a mapping from all found languages to their respective directories.
'''
mytracks_path = GetMyTracksDir()
res_dir = os.path.join(mytracks_path, MYTRACKS_RES_DIR, ANDROID_VALUES_MASK)
language_dirs = glob(res_dir)
master_dir = os.path.join(mytracks_path, MYTRACKS_RES_DIR, ANDROID_MASTER_VALUES)
if len(language_dirs) == 0:
raise 'No languages found!'
if not os.path.isdir(master_dir):
raise 'Couldn\'t find master file'
language_tuples = [(re.findall(r'.*values-([A-Za-z-]+)', dir)[0],dir) for dir in language_dirs]
language_tuples.append(('en', master_dir))
return dict(language_tuples)
| Python |
'''
Module which parses a string XML file.
@author: Rodrigo Damazio
'''
from xml.parsers.expat import ParserCreate
import re
#import xml.etree.ElementTree as ET
class StringsParser(object):
'''
Parser for string XML files.
This object is not thread-safe and should be used for parsing a single file at
a time, only.
'''
def Parse(self, file):
'''
Parses the given file and returns a dictionary mapping keys to an object
with attributes for that key, such as the value, start/end line and explicit
revisions.
In addition to the standard XML format of the strings file, this parser
supports an annotation inside comments, in one of these formats:
<!-- KEEP_PARENT name="bla" -->
<!-- KEEP_PARENT name="bla" rev="123456789012" -->
Such an annotation indicates that we're explicitly inheriting form the
master file (and the optional revision says that this decision is compatible
with the master file up to that revision).
@param file: the name of the file to parse
'''
self._Reset()
# Unfortunately expat is the only parser that will give us line numbers
self._xml_parser = ParserCreate()
self._xml_parser.StartElementHandler = self._StartElementHandler
self._xml_parser.EndElementHandler = self._EndElementHandler
self._xml_parser.CharacterDataHandler = self._CharacterDataHandler
self._xml_parser.CommentHandler = self._CommentHandler
file_obj = open(file)
self._xml_parser.ParseFile(file_obj)
file_obj.close()
return self._all_strings
def _Reset(self):
self._currentString = None
self._currentStringName = None
self._currentStringValue = None
self._all_strings = {}
def _StartElementHandler(self, name, attrs):
if name != 'string':
return
if 'name' not in attrs:
return
assert not self._currentString
assert not self._currentStringName
self._currentString = {
'startLine' : self._xml_parser.CurrentLineNumber,
}
if 'rev' in attrs:
self._currentString['revs'] = [attrs['rev']]
self._currentStringName = attrs['name']
self._currentStringValue = ''
def _EndElementHandler(self, name):
if name != 'string':
return
assert self._currentString
assert self._currentStringName
self._currentString['value'] = self._currentStringValue
self._currentString['endLine'] = self._xml_parser.CurrentLineNumber
self._all_strings[self._currentStringName] = self._currentString
self._currentString = None
self._currentStringName = None
self._currentStringValue = None
def _CharacterDataHandler(self, data):
if not self._currentString:
return
self._currentStringValue += data
_KEEP_PARENT_REGEX = re.compile(r'\s*KEEP_PARENT\s+'
r'name\s*=\s*[\'"]?(?P<name>[a-z0-9_]+)[\'"]?'
r'(?:\s+rev=[\'"]?(?P<rev>[0-9a-f]{12})[\'"]?)?\s*',
re.MULTILINE | re.DOTALL)
def _CommentHandler(self, data):
keep_parent_match = self._KEEP_PARENT_REGEX.match(data)
if not keep_parent_match:
return
name = keep_parent_match.group('name')
self._all_strings[name] = {
'keepParent' : True,
'startLine' : self._xml_parser.CurrentLineNumber,
'endLine' : self._xml_parser.CurrentLineNumber
}
rev = keep_parent_match.group('rev')
if rev:
self._all_strings[name]['revs'] = [rev] | Python |
import os, re, sys, urllib
from time import sleep
from shutil import move
# Disable traceback
sys.tracebacklimit = 0
# Global for finding images
htmldl = '<a href="//images.4chan.org/'
htmlboardfind = '[<a href="res/[0-9]+" class="replylink">Reply</a>]'
# Dynamic Print Function
def dynamic_print(msg):
sys.stdout.write("\r"+msg)
sys.stdout.flush()
# Puts html content into a variable
def geturl(url):
f = urllib.urlopen(url)
fread = f.read()
return fread
def countfiles(path):
number_of_files = sum(1 for item in os.listdir(path) if os.path.isfile(os.path.join(path, item)))
return number_of_files
# Check url to know if it's a board, a thread or an invalid link
def checkurl(argurl):
rthread = re.findall('http://boards.4chan.org/[a-z]+/res/', argurl)
rboard = re.findall('http://boards.4chan.org/[a-z]+/$', argurl)
if rthread:
return 'thread'
elif rboard:
return 'board'
else:
return 'error'
# Returns thread number
def threadnumber(argurl):
thread = 'http://boards.4chan.org/[a-z]+/res/'
rthread = argurl.split('/res/')
return rthread[1]
# Returns board letter.
def boardletter(argurl):
board = 'http://boards.4chan.org/[a-z]+'
rboard = re.findall(board, argurl)
return rboard[0].split('.org/')[1]
# Resolves the path to put the images based on "board/thread_number"
def path(threadnumber):
board = boardletter(argurl)
if os.path.isdir(board+'/'+threadnumber):
return True
elif os.path.isdir(board):
os.path.join(board)
os.mkdir(os.path.expanduser(board+'/'+threadnumber))
else:
os.mkdir(board)
os.mkdir(os.path.expanduser(board+'/'+threadnumber))
# Dump the thread.
def dump_thread(url, boardletter, threadnumber):
print url
fread = geturl(url)
x = 1
p = 1
if fread.count(htmldl) > 0:
while x <= fread.count(htmldl):
p = fread.find(htmldl, p)
concatenate = ''
filename = ''
# Set concatenate and filename
for i in range(p+11, len(htmldl)+p+30):
if fread[i] == '"':
break
concatenate = concatenate + fread[i]
if fread[i] == '/':
filename = ''
else:
filename = filename + fread[i]
# Print status
msg = "[%i/%i] %s" % (x,fread.count(htmldl),str(filename))
if x == fread.count(htmldl):
dynamic_print(msg)
dynamic_print("")
else:
dynamic_print(msg)
# Download and handle file/folders
# If already downloaded, jump
if os.path.isfile(boardletter+'/'+threadnumber+'/'+filename):
jump = True
# If incomplete, remove it, download and move
elif os.path.isfile(filename):
os.remove(filename)
urllib.urlretrieve('http://'+concatenate, str(filename))
move(filename, boardletter+'/'+threadnumber)
# Download and move
else:
urllib.urlretrieve('http://'+concatenate, str(filename))
move(filename, boardletter+'/'+threadnumber)
p += 1
x += 1
else:
return False
def dump_board(argurl):
page = 1
board = str(boardletter(argurl))
result = []
x = 0
print 'Dumping /'+board+'/'
while page > 0:
fread = geturl(argurl+'/'+str(page))
threads = re.findall(htmlboardfind, fread)
for t in threads:
url = 'http://boards.4chan.org/'+board+'/res/'+t.split('res/')[1].split('"')[0]
result.append(url)
x += 1
if len(threads) == 0:
break
page += 1
return result
def update():
for dirname, dirnames, filenames in os.walk('.'):
if len(dirname.split('\\')) > 2:
dirurl = 'http://boards.4chan.org/'+dirname.split('\\')[1]+'/res/'+dirname.split('\\')[2]
if countfiles(dirname.split('\\')[1]+'/'+dirname.split('\\')[2]) != geturl(dirurl).count(htmldl):
print dirurl
dump_thread(geturl(dirurl), boardletter(dirurl), threadnumber(dirurl))
else:
print dirurl
def doupdate(continuous=False, timer=60):
if continuous == False:
update()
while continuous != False:
update()
print 'Waiting '+str(timer)+' seconds to refresh...'
sleep(timer)
###### Program execution starts here #####
# Get url from argument
try:
arguments = sys.argv[1:]
except IndexError:
print "I've failed, master."
# Threat the given url/command.
if len(arguments) > 0:
noarg = False
for argurl in arguments:
if checkurl(argurl) == 'thread':
path(threadnumber(argurl))
dump_thread(argurl, boardletter(argurl), threadnumber(argurl))
elif checkurl(argurl) == 'board':
for i in dump_board(argurl):
path(threadnumber(i))
dump_thread(i, boardletter(i), threadnumber(i))
elif argurl == '--update':
doupdate()
elif argurl == '-u':
doupdate(True, 120)
else:
print "This does not seem to be valid link or option."
else:
noarg = True
if noarg == True:
print ''
print ' ================================================='
print ' usage: %s url | Download all threads' % sys.argv[0]
print ' usage: %s -u | Update' % sys.argv[0]
print ' -------------------------------------------------'
print ' Note:'
print ' if you use a board for the url, it will get'
print ' all the threads available at the moment.'
print ' ================================================='
sys.exit(1) | Python |
#!/usr/bin/python -w
import subprocess
import math
# This code does not need to be very fast; it will have maybe 2-3 ops per frame.
class Matrix4(object):
def __init__(self, coefficients):
"""Initialize the matrix from the given coefficients"""
assert len(coefficients) == 4
for c in coefficients:
assert len(c) == 4
self.coeff = tuple(map(tuple,coefficients))
def __mul__(self, other):
return Matrix4([ [ sum(self[i][k] * other[k][j] for k in range(4))
for j in range(4) ]
for i in range(4) ])
def __getitem__(self, n):
return self.coeff[n]
@classmethod
def _Rotation(cls, r1, r2, rad):
c = range(4)
mat = [ [ (1 if i == j else 0) for i in c ] for j in c]
mat[r1][r1] = math.cos(rad)
mat[r1][r2] = -math.sin(rad)
mat[r2][r1] = math.sin(rad)
mat[r2][r2] = math.cos(rad)
return cls(mat)
@classmethod
def Identity(cls):
c = range(4)
mat = [ [ (1 if i == j else 0) for i in c ] for j in c]
return cls(mat)
# SO(4) transformations
@classmethod
def RotWX(cls, rad):
return cls._Rotation(0,1,rad)
@classmethod
def RotXY(cls, rad):
return cls._Rotation(1,2,rad)
@classmethod
def RotYZ(cls, rad):
return cls._Rotation(2,3,rad)
@classmethod
def RotZW(cls, rad):
return cls._Rotation(3,1,rad)
@classmethod
def RotWY(cls, rad):
return cls._Rotation(0,2,rad)
@classmethod
def RotXZ(cls, rad):
return cls._Rotation(1,3,rad)
def as_list(self):
return [self[i][j]
for i in range(4)
for j in range(4)]
class Driver(object):
"""Helper class for generating movies. Carries basic state between
frames, which can be modiied using the various modifier functions.
Use write_image() to save the current frame to disk"""
def __init__(self):
self.xform = Matrix4.Identity()
self.frame = 0
def write_image(self):
proc = subprocess.Popen(['./mk_image', '-b', '-i', 'starting_points.txt'],
stdin=subprocess.PIPE)
s = " ".join(map(str, [150,200, 70,170, 0,80] + self.xform.as_list() + ["py_%09d.png" % self.frame])) + "\n"
print s
proc.stdin.write(s)
proc.stdin.close()
self.frame += 1
print proc.wait()
def load_identity(self):
self.xform = Matrix4.Identity()
return self
def rot_WX(self,r):
self.xform *= Matrix4.RotWX(r)
return self
def rot_XY(self,r):
self.xform *= Matrix4.RotXY(r)
return self
def rot_YZ(self,r):
self.xform *= Matrix4.RotYZ(r)
return self
def rot_ZW(self,r):
self.xform *= Matrix4.RotZW(r)
return self
def rot_WY(self,r):
self.xform *= Matrix4.RotWY(r)
return self
def rot_XZ(self,r):
self.xform *= Matrix4.RotXZ(r)
return self
# Shortcuts...
I = load_identity
WX = rot_WX
XY = rot_XY
YZ = rot_YZ
ZW = rot_ZW
WY = rot_WY
XZ = rot_XZ
F = write_image
def main():
d = Driver()
d.load_identity()
for x in range(4096):
print x
d.WY(0.01).XZ(0.01).XY(0.005).write_image()
if __name__ == '__main__':
main()
| Python |
#!/usr/bin/python -w
import subprocess
import math
# This code does not need to be very fast; it will have maybe 2-3 ops per frame.
class Matrix4(object):
def __init__(self, coefficients):
"""Initialize the matrix from the given coefficients"""
assert len(coefficients) == 4
for c in coefficients:
assert len(c) == 4
self.coeff = tuple(map(tuple,coefficients))
def __mul__(self, other):
return Matrix4([ [ sum(self[i][k] * other[k][j] for k in range(4))
for j in range(4) ]
for i in range(4) ])
def __getitem__(self, n):
return self.coeff[n]
@classmethod
def _Rotation(cls, r1, r2, rad):
c = range(4)
mat = [ [ (1 if i == j else 0) for i in c ] for j in c]
mat[r1][r1] = math.cos(rad)
mat[r1][r2] = -math.sin(rad)
mat[r2][r1] = math.sin(rad)
mat[r2][r2] = math.cos(rad)
return cls(mat)
@classmethod
def Identity(cls):
c = range(4)
mat = [ [ (1 if i == j else 0) for i in c ] for j in c]
return cls(mat)
# SO(4) transformations
@classmethod
def RotWX(cls, rad):
return cls._Rotation(0,1,rad)
@classmethod
def RotXY(cls, rad):
return cls._Rotation(1,2,rad)
@classmethod
def RotYZ(cls, rad):
return cls._Rotation(2,3,rad)
@classmethod
def RotZW(cls, rad):
return cls._Rotation(3,1,rad)
@classmethod
def RotWY(cls, rad):
return cls._Rotation(0,2,rad)
@classmethod
def RotXZ(cls, rad):
return cls._Rotation(1,3,rad)
def as_list(self):
return [self[i][j]
for i in range(4)
for j in range(4)]
class Driver(object):
"""Helper class for generating movies. Carries basic state between
frames, which can be modiied using the various modifier functions.
Use write_image() to save the current frame to disk"""
def __init__(self):
self.xform = Matrix4.Identity()
self.frame = 0
def write_image(self):
proc = subprocess.Popen(['./mk_image', '-b', '-i', 'starting_points.txt'],
stdin=subprocess.PIPE)
s = " ".join(map(str, [150,200, 70,170, 0,80] + self.xform.as_list() + ["py_%09d.png" % self.frame])) + "\n"
print s
proc.stdin.write(s)
proc.stdin.close()
self.frame += 1
print proc.wait()
def load_identity(self):
self.xform = Matrix4.Identity()
return self
def rot_WX(self,r):
self.xform *= Matrix4.RotWX(r)
return self
def rot_XY(self,r):
self.xform *= Matrix4.RotXY(r)
return self
def rot_YZ(self,r):
self.xform *= Matrix4.RotYZ(r)
return self
def rot_ZW(self,r):
self.xform *= Matrix4.RotZW(r)
return self
def rot_WY(self,r):
self.xform *= Matrix4.RotWY(r)
return self
def rot_XZ(self,r):
self.xform *= Matrix4.RotXZ(r)
return self
# Shortcuts...
I = load_identity
WX = rot_WX
XY = rot_XY
YZ = rot_YZ
ZW = rot_ZW
WY = rot_WY
XZ = rot_XZ
F = write_image
def main():
d = Driver()
d.load_identity()
for x in range(4096):
print x
d.WY(0.01).XZ(0.01).XY(0.005).write_image()
if __name__ == '__main__':
main()
| Python |
'''###########################################################################################################################
### File: Player.py
### Name: Patrick Delaney, Tom WIlliams, John Mannix
### Class: CSE 487
### Instructor: Dr.Zmuda
### Assignment: Assignment 3
### Files included: Utilities.py, brickout.py
### Description: This class contains all of my methods that were global in nature in brickout.py
################
'''
### Name:
### Author: Patrick Delaney
### Parameters:
### Description:
'''
###########################################################################################################################'''
import direct.directbase.DirectStart # starts Panda
import sys
from pandac.PandaModules import * # basic Panda modules
from direct.showbase.DirectObject import DirectObject # for event handling
from direct.actor.Actor import Actor # Actor class
from direct.interval.IntervalGlobal import * # Intervals (Parallel, Sequence, etc)
from direct.task import Task # for task contants
import math
import random
######CONSTANTS######
#CHOICE OF MODELS
RALPH = 0
SONIC = 1
TAILS = 2
EVE = 3
BUNNY = 4
STARTING_VELOCITY = Vec3(0,0,-10)
#################
SONIC_SCALE = .25
STARTING_POS = Vec3(0,0,0)
#####################
class Player:
'''
### Name: __init__ (Overriden Constructor)
### Author: Patrick Delaney
### Parameters: choice - Choice of Model
### Description: Constructs the Player Object
'''
def __init__(self,choice):
#self.avatarNode = render.attachNewNode("player_dummy_node")
if(choice == RALPH):
self.avatar = loader.loadModel("models/ralph")
elif(choice == SONIC):
self.avatar = loader.loadModel("models/sonic")
self.avatar.setScale(SONIC_SCALE)
elif(choice == TAILS):
self.avatar = loader.loadModel("models/tails")
self.avatar.setScale(SONIC_SCALE)
elif(choice == EVE):
self.avatar = loader.loadModel("models/eve")
elif(choice == BUNNY):
self.avatar = loader.loadModel("models/bunny")
self.avatar.reparentTo(render)
self.avatar.setPos(STARTING_POS)
self.avatar.setHpr(180,90,0)
self.avatar.setTransparency(False)
self.velocity = STARTING_VELOCITY
self.avatarChoice = choice
def getVelocity(self):
return self.velocity
def setVelocity(self,val):
self.velocity = val
| Python |
from pandac.PandaModules import *
class Picker:
def __init__(self, parent=None, fromMask=BitMask32(0), collideWithGeom=False, intoMask=BitMask32(0)):
if (parent==None):
parent = camera
self.cRay = CollisionRay()
self.cNode = CollisionNode('PickRay')
self.cNode.addSolid(self.cRay)
self.cNode.setIntoCollideMask(intoMask)
if collideWithGeom:
fromMask = fromMask | GeomNode.getDefaultCollideMask()
self.cNode.setFromCollideMask(fromMask)
self.cNodePath = parent.attachNewNode(self.cNode)
# self.cNodePath.show()
self.cHandler = CollisionHandlerQueue()
self.cTrav = CollisionTraverser()
self.cTrav.addCollider(self.cNodePath,self.cHandler)
def pick(self,traverseRoot=None):
return self.pickFromScreen(base.mouseWatcherNode.getMouseX(),base.mouseWatcherNode.getMouseY(),traverseRoot)
def pickFromScreen(self, screenX, screenY, traverseRoot=None):
self.cRay.setFromLens(base.camNode,screenX,screenY)
dir = self.cRay.getDirection()
self.cRay.setDirection(dir)
return self.__doPick(traverseRoot)
def __doPick(self,traverseRoot=None):
if (not traverseRoot):
traverseRoot=render
self.cTrav.traverse(traverseRoot)
self.cHandler.sortEntries()
for i in range(self.cHandler.getNumEntries()):
entry = self.cHandler.getEntry(i)
if (not (isinstance(entry.getIntoNode(),GeomNode) and entry.getIntoNodePath().isHidden()) and entry.hasSurfacePoint()):
return entry
return None
| Python |
'''###########################################################################################################################
### File: Game.py
### Names: Patrick Delaney, Tom Williams, John Mannix
### Class: CSE 487
### Instructor: Dr.Zmuda
### Assignment: Assignment 4
### Files included:
### Description:
### Note: Sonic the Hedgehog and other names are registered trademarks of SEGA. This game is only made for academic purposes.
################
'''
### Name:
### Author:
### Parameters:
### Description:
'''
###########################################################################################################################'''
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from direct.gui.DirectGui import *
from direct.showbase.DirectObject import *
from math import sin, cos, pi
from random import randint, choice, random
#import cPickle, sys
from direct.task.Task import Task
import math
import copy
from Player import *
from Picker import Picker
from Objects import *
from LevelGenerator import *
#import os
#from Utilities import *
#Note: If you have a favorite font you like (and you have the .ttf for it) you can use this line of code in the command line
# to convert it to a .egg
# egg-mkfont -o fontName.egg fontName.ttf
'''
###TODO LIST/ IDEAS:
### Come up with a better name
### Create a Splash Screen
### Create the Level Select Screen
### Create the Help Screen
### Create Custom Level Loaders and 10-20 Levels
### Add more objects
### Implement an end to a level
### Add a results screen (Return back to level select screen afterwards?)
### Add a button that causes the players avatar to speed up / slow down
'''
'''
### Name: collGeom
### Author: Dr. Zmuda
### Parameters: obj- object, name - name of the object, fromMask - Bit Mask for from collide check, intoMask - Bit mask for
### into collide check, geomList - list of collision geometic objects
### Description: Factored out code that simplifies creating collision objects.
'''
def collGeom(obj, name, fromMask, intoMask, geomList):
cNode = CollisionNode(name)
cNode.setFromCollideMask(BitMask32(fromMask))
cNode.setIntoCollideMask(BitMask32(intoMask))
for g in geomList:
cNode.addSolid(g)
cNodePath = obj.attachNewNode(cNode)
#cNodePath.show()
return cNodePath
######CONSTANTS######
#These are used in loading the avatars for the character selection screen.
LIST_OF_AVATARS = [ "models/ralph", "models/sonic","models/tails","models/eve"]
LIST_OF_SCALES = [ 1, .25,.25,1]
LIST_OF_POSITIONS = [Vec3(-15,-5,0), Vec3(-7,-5,0), Vec3(7,-5,0), Vec3(15,-5,0)]
LIST_OF_NAMES = ["Ralph","Sonic","Tails", "Eve"]
LIST_OF_TEXT_POS = [Vec3(-1.1,0,-0.5), Vec3(-0.55,0,-0.5), Vec3(0.55,0,-0.5), Vec3(1.1,0,-0.5)]
#Used in Avatar Movement
X_STRAFE = .5
Y_STRAFE = .5
#Points
LOOP_SCORE = 100
RING_SCORE = 0
ANVIL_LOSS = 5
#Used in the level select scene
LIST_OF_DIFFICULT = ["easy","normal","hard","intense","insane","bunnies"]
DSCALE = Vec3(4,1,16)
LIST_OF_DPOS = [Vec3(-10,0,0),Vec3(-6,0,0),Vec3(-2,0,0),Vec3(2,0,0),Vec3(6,0,0),Vec3(10,0,0)]
#Difficulty levels
SCALE_OF_HARDNESS = [5,10,15,20,25,25]
BUNNIES = 25
class World(DirectObject):
'''
### Name: __init__ (Constructor)
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters:
### Description:
'''
def __init__(self):
#Controls the size of the window
wp = WindowProperties()
#wp.setFullscreen(1)
wp.setSize(1024, 768)
base.openMainWindow()
base.win.requestProperties(wp)
base.graphicsEngine.openWindows()
# turn off default mouse motion and position camera
# remember! if the mouse control is on, you can't reposition the camera
base.disableMouse()
self.cameraPos = Vec3(0,0,50) #I'm using this to keep track of the camera's position since it won't get me its position
base.camera.setPosHpr(self.cameraPos, Vec3(0, -90, 0))
#Initialize Picker
self.picker = Picker(fromMask=BitMask32(0x01), collideWithGeom=True)
#Character Selection
self.avatars = []
self.names = []
#Level selection
self.buttons = []
self.curDiff = -1
#Object Storage
self.objects = []
#Storage for rings
self.rings = []
self.types = []
#Storage for anvils
self.anvils = []
#Storage for collisionNodes
self.cNodePaths = []
self.helpScreenOn = 0
self.inGame = False
#State Variables
self.notSelected = True
self.curAvatar = -1 #Used to determine which avatar to load
self.score = 0 #Used to keep track of the player's score.
self.numRings = 0 #Used to keep track of how many Rings Collected
self.alreadyDisplayed = False
self.endOfLevel = False #Used to keep track of when the player hits the end of the level.
self.resetGame = False #Used to decide when to reset the game
#A dictionary of what keys are currently being pressed
#The key events update this list, and our task will query it as input
self.keys = {"moveLeft" : 0, "moveRight" : 0, "moveUp" : 0, "moveDown":0, "levelStart": 0}
#Arrow Keys, used in movement
self.accept("arrow_left", self.keys.update, [{"moveLeft":1}] )
self.accept("arrow_left-up", self.keys.update, [{"moveLeft":0}] )
self.accept("arrow_right", self.keys.update, [{"moveRight":1}] )
self.accept("arrow_right-up", self.keys.update, [{"moveRight":0}] )
self.accept("arrow_up", self.keys.update, [{"moveUp":1}] )
self.accept("arrow_up-up", self.keys.update, [{"moveUp":0}] )
self.accept("arrow_down", self.keys.update, [{"moveDown":1}] )
self.accept("arrow_down-up", self.keys.update, [{"moveDown":0}] )
self.accept("h", self.displayHelp)
self.accept("mouse1", self.introClick)
#Quit game
self.accept("escape", sys.exit)
self.introWaitTask = taskMgr.add(self.introWait, "introWait")
#Music for the game
self.ringLoss = base.loader.loadSfx("music/RingLoss.mp3")
self.ringGained = base.loader.loadSfx("music/RingGained.mp3")
self.stageSelectMusic = base.loader.loadSfx("music/LevelSelect.mp3")
self.helpMusic = base.loader.loadSfx("music/HelpMusic.mp3")
self.characterSelectMusic = base.loader.loadSfx("music/SelectACharacter.mp3")
#self.gameMusic = base.loader.loadSfx("music/special_stage.mp3")
self.gameMusic = base.loader.loadSfx("music/special_stage_2.mp3")
self.introMusic = base.loader.loadSfx("music/IntroMusic.mp3")
self.badEndingMusic = base.loader.loadSfx("music/StageClearBad.mp3")
self.goodEndingMusic = base.loader.loadSfx("music/StageClear.mp3")
self.introScreen()
'''
### Name: introScreen
### Author: John Mannix
### Parameters: Nothing
### Description: Sets up the intro screen
'''
def introScreen(self):
self.introMusic.play()
self.readyClick = 0
self.titleScreen = loader.loadModel("models/plane")
tex = loader.loadTexture("models/titleScreen.png")
self.titleScreen.setTexture(tex, 1)
self.titleScreen.setCollideMask(0x01)
self.titleScreen.reparentTo(render)
self.titleScreen.setScale(Vec3(16,1,16))
self.titleScreen.setPos(Vec3(0,0,0))
self.titleScreen.setP(-90)
'''
### Name: introClick
### Author: John Mannix
### Parameters: nothing
### Description: sets up state for the next screen after the intro screen
'''
def introClick(self):
self.ignore("mouse1")
self.readyClick = 1
'''
### Name: introWait
### Author: John Mannix
### Parameters: task - required for task methods
### Description: The task that handles the introscreen.
'''
def introWait(self, task):
if self.readyClick == 0:
return task.cont
else:
self.titleScreen.removeNode()
if self.introMusic.status() == self.introMusic.PLAYING:
self.introMusic.stop()
self.selectLevel()
self.accept("mouse1", self.buttonPick) #Picking, Used in character selection and level selection.
self.buttonSelectTask = taskMgr.add(self.buttonSelect, "buttonSelect")
return task.done
'''
### Name: loadButton
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: path - path to texture, scale - scale of button, pos- position of button
### Description: Loads the buttons for the difficulty select screen
'''
def loadButton(self,path,scale,pos):
button = loader.loadModel("models/plane")
tex = loader.loadTexture("models/button-"+path+".png") #Load the texture
button.setTexture(tex, 1)
button.setCollideMask(0x01)
button.reparentTo(render)
button.setScale(scale)
button.setPos(pos)
button.setP(-90)
return button
'''
### Name: selectLevel
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: None
### Description: Sets up the difficulty select screen
'''
def selectLevel(self):
self.stageSelectMusic.setLoop(True)
self.stageSelectMusic.play()
text = "Select your Level!"
self.titleText = self.loadText("fonts/centbold.egg","Title", text,TextNode.ACenter,VBase4(0,0,1,1),Vec3(0,0,0.90),0.1)
for i in range(len(LIST_OF_DIFFICULT)):
self.buttons.append(self.loadButton(LIST_OF_DIFFICULT[i], DSCALE, LIST_OF_DPOS[i]))
'''
### Name: buttonSelect
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: task - required for task methods
### Description: The task that handles the difficulty select screen
'''
def buttonSelect(self,task):
if(self.curDiff < 0): # If the player has not selected a level
return task.cont
else:
self.cleanUpButtons()
self.stageSelectMusic.stop()
self.accept("mouse1", self.pick) # change picker to handle Avatars now.
self.difficulty = SCALE_OF_HARDNESS[self.curDiff]
self.selectScreen()
self.avatarSelectTask = taskMgr.add(self.avatarSelect, "avatarSelect")
#Start Character Select Music
self.characterSelectMusic.setLoop(True)
self.characterSelectMusic.play()
return task.done
'''
### Name: buttonPick
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: nothing
### Description: Handles picking for the buttons
'''
def buttonPick(self):
cEntry = self.picker.pick()
if cEntry:
buttonGeomHit = cEntry.getIntoNodePath()
for i in range(len(self.buttons)):
p = self.buttons[i]
if p.isAncestorOf(buttonGeomHit):
self.curDiff = i
return
'''
### Name: cleanUpButtons
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: nothing
### Description: Removes all the buttons from the screen
'''
def cleanUpButtons(self):
self.titleText.removeNode()
for i in range(len(LIST_OF_DIFFICULT)):
self.buttons[i].removeNode()
'''
### Name: selectScreen
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: None
### Description: Loads the select screen.
'''
def selectScreen(self):
text = "Select a Character! Click on any of the four below!"
self.titleText = self.loadText("fonts/centbold.egg","Title", text,TextNode.ACenter,VBase4(0,0,1,1),Vec3(0,0,0.90),0.1)
#Load the avatars on the screen for initial selection
#Load the text below the avatars as well
if(self.curDiff <> 5):
for i in range(len(LIST_OF_AVATARS)):
self.avatars.append(self.loadAvatar(LIST_OF_AVATARS[i],LIST_OF_SCALES[i],LIST_OF_POSITIONS[i]))
self.names.append(self.loadText("fonts/centbold.egg",LIST_OF_NAMES[i], LIST_OF_NAMES[i],
TextNode.ACenter,VBase4(0,0,1,1),LIST_OF_TEXT_POS[i],0.1) )
else:
self.avatars.append(self.loadAvatar("models/bunny",1,Vec3(0,0,0)))
self.names.append(self.loadText("fonts/centbold.egg","Bunny", "Bunny",
TextNode.ACenter,VBase4(0,0,1,1),Vec3(0,-10,0),0.1) )
self.curAvatar = BUNNY
'''
### Name: avatarSelect
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: task
### Description: This method is a task that handles the character selection. Once the player has selected a character, it
### removes itself from the task queue and adds the gameLoop task to the queue as well as loads the initial
### gamestate.
'''
def avatarSelect(self,task):
if(self.curAvatar < 0): # If the player has not selected a character
return task.cont
else:
self.cleanUpAvatars()
self.player = Player(self.curAvatar)
self.ignore("mouse1") #We don't need picking anymore
#Now that the player has picked an avatar
#Prepare initial game state and add gameLoop to the task list.
self.loadInitialGameState()
self.characterSelectMusic.stop()
self.gameMusic.setLoop(True)
self.gameMusic.play()
return task.done
'''
### Name: loadAvatar
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: path - path to Model, Scale - scale for the model, pos - position of the model
### Description: Loads an "avatar" for the player unto the screen. This is used in the character select screen.
'''
def loadAvatar(self,path,scale,pos):
avatar = loader.loadModel(path)
avatar.setCollideMask(0x01)
avatar.reparentTo(render)
avatar.setScale(scale)
avatar.setPos(pos)
avatar.setP(-90)
return avatar
'''
### Name: loadObjects
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: none
### Description: Loads the objects in from the random level generator
'''
def loadObjects(self):
level = LevelGenerator( self.rings , self.difficulty, self.anvils);
'''
### Name: loadInitialGameState
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: none
### Description: Prepares the initial game state and starts the main game loop.
'''
def loadInitialGameState(self):
self.gameTask = taskMgr.add(self.gameLoop, "gameloop")
self.gameTask.last = 0
#Load environment
self.env = loader.loadModel("models/env")
self.env.reparentTo(render)
self.env.setScale(1000)
self.env.setH(0)
self.env.setPos(0,0,-1000)
#Load objects
self.loadObjects()
#Setup Collisions
self.setupCollisions()
self.inGame = True
#Load score Font
self.scoreText = self.loadText("fonts/centbold.egg","Score", "Score: " + `self.score`,TextNode.ACenter,
VBase4(1,1,0,1),Vec3(-1.1,0,0.90),0.1)
#Load Rings Font
self.ringText = self.loadText("fonts/centbold.egg","Rings", "Rings: " + `self.numRings`,TextNode.ACenter,
VBase4(1,1,0,1),Vec3(-1.1,0,0.70),0.1)
#Allow Spacebar to be pressed to start the level
self.accept("space", self.keys.update, [{"levelStart":1}] )
'''
### Name: loadText
### Author: Patrick Delaney
### Parameters: path - path to Font file,nodeName - name for text node, str - String to be displayed
### align- How to align the text, see Panda3D manual, color - color to set, MUST be of type VBase4
### pos - position of the text, scale - scale of the text, MUST be small because we are using
### aspect2d, (Because aspect2d coords are between -1 and 1)
### Description: Loads a text unto the screen. See parameters for what to input.
'''
def loadText(self,path,nodeName,str,align, color,pos,scale):
font = loader.loadFont(path)
text = TextNode(nodeName)
text.setFont(font)
text.setText(str)
text.setAlign(align)
textPath = aspect2d.attachNewNode(text)
textPath.setColor(color)
textPath.setPos(pos)
textPath.setScale(scale)
return textPath
'''
### Name: cleanUpAvatars
### Author: Patrick Delaney
### Parameters: None
### Description: Deletes everything from the character selection screen.
'''
def cleanUpAvatars(self):
self.titleText.removeNode()
for i in range(len(self.avatars)):
self.avatars[i].removeNode()
self.names[i].removeNode()
'''
### Name: pick
### Author: Dr.Zmuda - Modified by Patrick Delaney
### Parameters: Nothing
### Description: A modified picker method created by Dr.Zmuda, instead of deleting the selected item, it sets the curAvatar
### state variable to the avatars number. This makes that avatar, the players avatar for the game.
'''
def pick(self):
cEntry = self.picker.pick()
if cEntry:
avatarGeomHit = cEntry.getIntoNodePath()
for i in range(len(self.avatars)):
p = self.avatars[i]
if p.isAncestorOf(avatarGeomHit):
self.curAvatar = i
return
'''
### Name: gameLoop
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters:
### Description:
'''
def gameLoop(self, task):
#Getting the change in time since the last task.
dt = task.time - task.last
task.last = task.time
if self.helpScreenOn == 1: #doesn't update while game is paused
return task.cont
if(self.keys["levelStart"]):
if( not self.endOfLevel):
#Remove the Instruction Text from the screen
self.startText.removeNode()
#Will also need to update the camera's position
self.updatePlayer(dt)
#Updating the camera.
self.updateCamera()
#Get the rings to rotate
self.updateRings(dt)
else:
self.resultScreen()
return task.done
else:
if(not self.alreadyDisplayed):
self.startText = self.loadText("fonts/centbold.egg","Start", "Press 'Spacebar' to start",TextNode.ACenter,
VBase4(0,0,0,1),Vec3(0,0,0),0.1)
self.alreadyDisplayed = True
return task.cont #Makes game loop infinite
'''
### Name: updatePlayer
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: dt - change in time
### Description: Updates the player using simple position equation. Physically stops the player from moving since the
### collision with the ground was not working right.
'''
def updatePlayer(self,dt):
curPos = self.player.avatar.getPos()
if (curPos[2] <= -995):
self.endOfLevel = True
return
if(self.keys["moveLeft"]):
curPos[0] -= X_STRAFE
if(self.keys["moveRight"]):
curPos[0] += X_STRAFE
if(self.keys["moveUp"]):
curPos[1] += Y_STRAFE
if(self.keys["moveDown"]):
curPos[1] -= Y_STRAFE
newPos = curPos + self.player.getVelocity()*dt*10
self.player.avatar.setPos(newPos)
def updateCamera(self):
base.camera.setPos(self.player.avatar.getPos() + Vec3(0,0,50))
'''
### Name: updateRings
### Author: Patrick Delaney
### Parameters: dt - change in time since the last frame
### Description: Causes the rings to rotate.
'''
def updateRings(self,dt):
if(len(self.rings) is not 0):
for ring in self.rings:
rotate = HprInterval(ring,ring.getHpr() +Vec3(0,0,1),dt)
rotate.start()
'''
### Name: setupCollisions
### Author: Patrick Delaney
### Parameters: None
### Description: Sets up all of the collision objects.
'''
def setupCollisions(self):
# use an event collision handler (sends events on collisions)
self.cHandler = CollisionHandlerEvent()
# set the pattern for the event sent on collision
# "enter" plus the name of the object collided into
self.cHandler.addInPattern("collected-%in")
# make a traverser and make it the default traverser
self.cTrav = CollisionTraverser()
base.cTrav = self.cTrav
bounds = self.player.avatar.getBounds()
center = bounds.getCenter()
radius = bounds.getRadius()
#TODO:Need to create a specfic collision solid for each avatar
#Creating Specific Collision Solids for each character
if(self.player.avatarChoice == RALPH):
avatarNode = collGeom(self.player.avatar, 'ralph', 0x01, 0x00,
[CollisionSphere(Point3(center + Point3(0.3,0,4.5)),radius*0.3),
CollisionSphere(Point3(center + Point3(0.3,0,2.5)),radius*0.3),
CollisionSphere(Point3(center + Point3(0.3,0,1.0)),radius*0.3)])
elif(self.player.avatarChoice == SONIC):
avatarNode = collGeom(self.player.avatar, 'sonic', 0x01, 0x00,
[CollisionSphere(Point3(center + Point3(0.2,0,25)),radius*1.1),
CollisionSphere(Point3(center + Point3(0,0,15)),radius*1.1),
CollisionSphere(Point3(center + Point3(-1,0,7)),radius)])
elif(self.player.avatarChoice == TAILS):
avatarNode = collGeom(self.player.avatar, 'tails', 0x01, 0x00,
[CollisionSphere(Point3(center + Point3(0,0,20)),radius*1.2),
CollisionSphere(Point3(center + Point3(0,0,10)),radius*1.2),
CollisionSphere(Point3(center + Point3(0,0,2)),radius)])
elif(self.player.avatarChoice == EVE):
avatarNode = collGeom(self.player.avatar, 'eve', 0x01, 0x00,
[CollisionSphere(Point3(center + Point3(0,0,3.5)),radius*0.35),
CollisionSphere(Point3(center + Point3(0,0,1.75)),radius*0.35),
CollisionSphere(Point3(center + Point3(0,0,0.35)),radius*0.3)])
elif(self.player.avatarChoice == BUNNY):
avatarNode = collGeom(self.player.avatar, 'bunny', 0x01, 0x00,
[CollisionSphere(Point3(center + Point3(0,0,3.5)),radius*0.35),
CollisionSphere(Point3(center + Point3(0,0,1.75)),radius*0.35),
CollisionSphere(Point3(center + Point3(0,0,0.35)),radius*0.3)])
self.cTrav.addCollider(avatarNode,self.cHandler)
self.cNodePaths.append(avatarNode)
#Set up collisions for rings
for ring in self.rings:
self.cNodePaths.append(collGeom(ring,"ring", 0x00,0x01,[CollisionSphere(Point3(0,0,0),1)]) )
for anvil in self.anvils:
self.cNodePaths.append(collGeom(anvil,"anvil", 0x00,0x01,[CollisionSphere(Point3(0,0,0),0.5)]) )
self.accept("collected-ring",self.collectRing)
self.accept("collected-anvil",self.collectAnvil)
def helpScreenToggle(self, tog):
if tog == 1:
self.help1 = self.loadText("fonts/centbold.egg","RingResult", "Controls",TextNode.ACenter,VBase4(1,1,0,1),Vec3(0,0,0.7),0.1)
self.help2 = self.loadText("fonts/centbold.egg","RingResult", "Movement: Arrow Keys",TextNode.ACenter,VBase4(1,1,0,1),Vec3(0,0,0.5),0.1)
self.help3 = self.loadText("fonts/centbold.egg","RingResult", "Help: H",TextNode.ACenter,VBase4(1,1,0,1),Vec3(0,0,0.3),0.1)
self.help4 = self.loadText("fonts/centbold.egg","RingResult", "Exit Game: Escape",TextNode.ACenter,VBase4(1,1,0,1),Vec3(0,0,0.1),0.1)
self.help5 = self.loadText("fonts/centbold.egg","RingResult", "Collect as many rings as you can",
TextNode.ACenter,VBase4(1,1,0,1),Vec3(0,0,-0.1),0.1)
self.help6 = self.loadText("fonts/centbold.egg","RingResult", "to increase your score!",
TextNode.ACenter,VBase4(1,1,0,1),Vec3(0,0,-0.2 ),0.1)
self.help7 = self.loadText("fonts/centbold.egg","RingResult", "Watch out for Obstacles!",
TextNode.ACenter,VBase4(1,1,0,1),Vec3(0,0,-0.5),0.1)
else:
self.help1.removeNode()
self.help2.removeNode()
self.help3.removeNode()
self.help4.removeNode()
self.help5.removeNode()
self.help6.removeNode()
self.help7.removeNode()
def displayHelp(self):
if(self.inGame): # Note only displays while in the game. Will not display otherwise. (Causes game to crash)
if self.helpScreenOn == 0:
self.helpScreenOn = 1
self.env.hide()
self.player.avatar.hide()
for ring in self.rings:
ring.hide()
for anvil in self.anvils:
anvil.hide()
self.gameMusic.stop()
self.helpMusic.setLoop(True)
self.helpMusic.play()
self.helpScreenToggle(1)
else:
self.helpScreenOn = 0
self.helpScreenToggle(0)
self.env.show()
self.player.avatar.show()
for ring in self.rings:
ring.show()
for anvil in self.anvils:
anvil.show()
self.helpMusic.stop()
self.gameMusic.play()
return
'''
### Name: collectAnvil
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: cEntry - required for collision events
### Description: Causes the player to lose 5 rings if they hit an anvil
'''
def collectAnvil(self,cEntry):
print "You hit an anvil!"
self.anvils.remove(cEntry.getIntoNodePath().getParent())
cEntry.getIntoNodePath().getParent().remove()
if(self.numRings < ANVIL_LOSS):
self.numRings = 0
else:
self.numRings -= ANVIL_LOSS
self.ringLoss.play()
self.ringText.removeNode()
self.ringText = self.loadText("fonts/centbold.egg","Rings", "Rings: " + `self.numRings`,
TextNode.ACenter,VBase4(1,1,0,1),Vec3(-1.1,0,0.70),0.1)
'''
### Name: collectRing
### Author: Patrick Delaney, Tom Williams, John Mannix
### Parameters: cEntry - required for collision events
### Description: Causes the player to gain a ring if the hit an onscreen ring
'''
def collectRing(self,cEntry):
print "You collected a ring!"
self.rings.remove(cEntry.getIntoNodePath().getParent())
cEntry.getIntoNodePath().getParent().remove()
self.numRings += 1
self.ringGained.play()
#Update the Ring Text
self.ringText.removeNode()
self.ringText = self.loadText("fonts/centbold.egg","Rings", "Rings: " + `self.numRings`,
TextNode.ACenter,VBase4(1,1,0,1),Vec3(-1.1,0,0.70),0.1)
def resultScreen(self):
self.env.hide()
self.player.avatar.hide()
self.gameMusic.stop()
if self.numRings < 5:
self.badEndingMusic.play()
else:
self.goodEndingMusic.play()
self.ringText.removeNode()
self.scoreText.removeNode()
self.resultText = self.loadText("fonts/centbold.egg","Result", "RESULTS",
TextNode.ACenter,VBase4(1,1,0,1),Vec3(0,0,0.9),0.1)
collectedRings = 0
self.ringResult = self.loadText("fonts/centbold.egg","RingResult", "You've Collected: " + `self.numRings`
+ " rings",TextNode.ACenter,VBase4(1,1,0,1),Vec3(-0.6,0,0.7),0.1)
self.score += self.numRings*10
self.scoreResultText = self.loadText("fonts/centbold.egg","scoreRes","Your score is now: " + `self.score`
,TextNode.ACenter,VBase4(1,1,0,1),Vec3(-0.6,0,0.5),0.1)
self.continueText = self.loadText("fonts/centbold.egg","reset","Press r to go back to the Main Menu"
,TextNode.ACenter,VBase4(1,1,0,1),Vec3(0.0,0,0.3),0.1)
self.accept("r", self.reset)
self.inGame = False
self.waitTask = taskMgr.add(self.wait, "wait")
def wait(self,task):
if(not self.resetGame):
return task.cont
else:
self.ignore("r")
if self.badEndingMusic.status() == self.badEndingMusic.PLAYING:
self.badEndingMusic.stop()
if self.goodEndingMusic.status() == self.goodEndingMusic.PLAYING:
self.goodEndingMusic.stop()
self.selectLevel()
self.resetGame = False
self.buttonSelectTask = taskMgr.add(self.buttonSelect, "buttonSelect")
return task.done
def reset(self):
#Reset state variables
self.notSelected = True
self.resetGame = True
self.endOfLevel = False
self.alreadyDisplayed = False
self.curAvatar = -1
self.keys["levelStart"] = 0
self.curDiff = -1
self.numRings = 0
self.inGame = False
#Remove Objects
self.env.removeNode()
self.player.avatar.removeNode()
for path in self.cNodePaths:
path.removeNode()
for ring in self.rings:
ring.removeNode()
for anvil in self.anvils:
anvil.removeNode()
#Remove text
self.resultText.removeNode()
self.ringResult.removeNode()
self.scoreResultText.removeNode()
self.continueText.removeNode()
#Empty object storage
del self.rings[:]
del self.anvils[:]
del self.buttons[:]
del self.avatars[:]
del self.names[:]
#Reset camera
self.cameraPos = Vec3(0,0,50) #I'm using this to keep track of the camera's position since it won't get me its position
base.camera.setPosHpr(self.cameraPos, Vec3(0, -90, 0))
#Set up mouse to click buttons again
self.accept("mouse1", self.buttonPick)
world = World() #Creates the world
run() #Runs the game. | Python |
from Objects import *;
from random import randint, choice, random;
import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from direct.gui.DirectGui import *
from direct.showbase.DirectObject import *
from math import sin, cos, pi
import math
import copy
from Player import *
from Picker import Picker
from LevelGenerator import *
class LevelGenerator:
NUMBEROFRINGS = 50; # density of the rings
HEIGHT = -800; # length of the course
SECTIONHEIGHT = HEIGHT / NUMBEROFRINGS; # a constant that is used.
def __init__(self, rings, diff, anvils):
random.seed();
totalsign = 0;
totalsign2 = 0;
self.rings = rings;
self.diff = diff;
self.anvils = anvils
lastRing = [0,0];
currentRing = [0,0];
sign = [0,0]
for i in range( self.NUMBEROFRINGS ):
if(lastRing[0] <0):
sign[0] = random.random() -.5; #make it more often plus when negative
else:
sign[0] = random.random() - .7; #make it more often minus when positive
if(lastRing[1] <0):
sign[1] = random.random() - .5; #make it more often plus when negative
else:
sign[1] = random.random() - .7; #make it more often minus when positive
currentRing = [lastRing[0] + ( sign[0] / math.fabs(sign[0])) * ( random.random() * (1 * self.diff/5) ) - ( .5 * self.diff/5 ) ,
lastRing[1] + ( sign[1] / math.fabs(sign[1])) * ( random.random() * (1 * self.diff/5) ) - ( .5 * self.diff/5 )];
if(diff > 5 and random.random() < diff/100.0):
anvil = Objects( ANVIL , Vec3(currentRing[0] + sign[0] * (40-diff)*(random.random() + .5), currentRing[1] + sign[0] * (40-diff)*(random.random() + .5), i * self.SECTIONHEIGHT));
self.anvils.append(anvil.object)
totalsign += ( sign[0] / math.fabs(sign[0]));
totalsign2 += ( sign[1] / math.fabs(sign[1]));
ring = Objects( TORUS , Vec3(currentRing[0], currentRing[1], i * self.SECTIONHEIGHT));
self.rings.append(ring.object);
lastRing[0] = currentRing[0];
lastRing[1] = currentRing[1];
print totalsign;
print "\n"
print totalsign2; | Python |
'''###########################################################################################################################
### File: Objects.py
### Name: Patrick Delaney, Tom Williams, John Mannix
### Class: CSE 487
### Instructor: Dr.Zmuda
### Assignment: Assignment 3
### Files included: Utilities.py, brickout.py
### Description: This class makes all of the objects for our game. (An Object Factory)
################
'''
### Name:
### Author: Patrick Delaney
### Parameters:
### Description:
'''
###########################################################################################################################'''
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from direct.gui.DirectGui import *
from direct.showbase.DirectObject import *
from math import sin, cos, pi,sqrt
from random import randint, choice, random
import cPickle, sys
#####CONSTANTS######
#Identifiers for objects
TORUS = 1 #Loop
RING = 2 # Used as a different score modifer / Possible win condition. Just an idea. - Patrick
ANVIL = 3
#Ideas for TORUS
#Speed up loop - Makes you move faster
#Slow down loop - Makes you move slower
# These can stack/ undo each other
#Double Points loop - Doubles the amount of points you can get
#Damage loop - Takes points away
# All these loops could be designated by color
class Objects:
#Once we determine what objects we want to use - I will modify the constructor here. It will be similar to the player
#Constructor. We should also implement a score. Maybe a special attribute as well. - Patrick
'''
### Name: __init__ (Overridden Constructor)
### Author: Patrick Delaney
### Parameters: type - integer identifer signifying what type of object. See Constants above.
### pos - position, num- Used in naming the object's collision node. E.g Ring1
### Description:
'''
def __init__(self,type,pos):
self.type = type;
if(type == TORUS):
self.object = loader.loadModel("models/torus")
self.object.setScale(2)
self.object.setColor(255,215,0,1) # Gold
elif(type == RING):
self.object = loader.loadModel("models/torus")
self.object.setScale(0.5)
self.object.setColor(255,215,0,1) # Gold
self.object.setTransparency(True)
elif( type == ANVIL):
self.object = loader.loadModel("models/anvil");
self.object.setTransparency( True);
self.object.setScale(5);
else:
self.object = loader.loadModel("models/torus")
self.object.setPos(pos)
self.object.reparentTo(render)
| Python |
'''###########################################################################################################################
### File: Utilities.py
### Name: Patrick Delaney
### Class: CSE 487
### Instructor: Dr.Zmuda
### Assignment: Assignment 3
### Files included: Utilities.py, brickout.py
### Description: This class contains all of my methods that were global in nature in brickout.py
################
'''
### Name:
### Author: Patrick Delaney
### Parameters:
### Description:
'''
###########################################################################################################################'''
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from direct.gui.DirectGui import *
from direct.showbase.DirectObject import *
from math import sin, cos, pi,sqrt
from random import randint, choice, random
import cPickle, sys
#############METHODS USED FROM TUT-ASTEROIDS###############################################################################
#This helps reduce the amount of code used by loading objects, since all of the
#objects are pretty much the same.
def loadObject(tex = None, pos = Point2(0,0), depth = 0, scale = 15, transparency = True):
obj = loader.loadModelCopy("plane") #Every object uses the plane model
obj.reparentTo(render)
obj.setPos(Vec3(pos.getX(), pos.getY(), depth)) #Set initial position
obj.setHpr(Vec3(0, -90, 0)) # -90 since egg file is Y-Up format
obj.setScale(scale) #Set initial scale
obj.setBin("unsorted", 0) #This tells Panda not to worry about the
#order this is drawn in. (it prevents an
#effect known as z-fighting)
obj.setDepthTest(True ) #Tells panda not to check if something
#has already drawn in front of it
#(Everything in this game is at the same
#depth anyway)
if transparency: obj.setTransparency(1) #All of our objects are trasnparent
if tex:
tex = loader.loadTexture("textures/"+tex+".png") #Load the texture
obj.setTexture(tex, 1) #Set the texture
return obj
def setVelocity(obj, val):
list = [val[0], val[1], val[2]]
obj.setTag("velocity", cPickle.dumps(list))
def getVelocity(obj):
list = cPickle.loads(obj.getTag("velocity"))
return Vec3(list[0], list[1], list[2])
def genLabelText(text, i):
return OnscreenText(text = text, pos = (-1.25, .95-.05*i), fg=(1,0.3,0.3,1),
align = TextNode.ALeft, scale = .05)
##############################END METHODS USED FROM TUT-ASTEROIDS###################################################
'''
### Name: reflection
### Author: Dr.Zmuda (Modifed by Patrick Delaney, I wasn't sure if my reflection code was wrong, so I used the solution from the
### s drive)
### Parameters: wallEndPt1 - First endpoint of the wall, wallEndPt2 - second endpoint of the wall,
### projectileStartingPt - Starting point of the projectile,
### projectileIntersectionPt - where the projectile hits the wall
### Description: Computes the reflection of the projectile with a wall and returns the reflection vector
'''
def reflection(wallEndPt1, wallEndPt2, projectileStartingPt, projectileIntersectionPt):
wallVec = wallEndPt1 - wallEndPt2
n = Vec2(-wallVec[1], wallVec[0])
n.normalize()
v = projectileStartingPt - projectileIntersectionPt
r = n * 2 * v.dot(n) - v
r.normalize()
return r
'''
### Name: distance
### Author: Patrick Delaney
### Parameters: objOne - objectOne, objTwo - objectTwo
### Description: Computes the distance between two objects in the xy plane.
'''
def distance(objOne,objTwo):
return sqrt( (objTwo[1] - objOne[1])**2 + (objTwo[0] - objOne[0])**2)
'''
### Name: setHits
### Author: Patrick Delaney
### Parameters: obj - brick, val - value to set the hits at
### Description: Sets the amount of hits a brick can take
'''
def setHits(obj,val):
obj.setTag("hits", cPickle.dumps(val))
'''
### Name: getHits
### Author: Patrick Delaney
### Parameters: obj - brick
### Description: Returns the amount of hits a brick has left
'''
def getHits(obj):
return cPickle.loads(obj.getTag("hits"))
'''
### Name: genLabelText2
### Author: Patrick Delaney
### Parameters: text - String to be displayed, x - xPos, y - yPos
### Description: Displays the string inputted on the screen at the x and y position.
'''
def genLabelText2(text, x,y):
return OnscreenText(text = text, pos = (x,y), fg=(1,0.3,0.3,1),
align = TextNode.ALeft, scale = .1)
| Python |
#!/usr/bin/env python
import time
t = time.time()
u = time.gmtime(t)
s = time.strftime('%a, %e %b %Y %T GMT', u)
print 'Content-Type: text/javascript'
print 'Cache-Control: no-cache'
print 'Date: ' + s
print 'Expires: ' + s
print ''
print 'var timeskew = new Date().getTime() - ' + str(t*1000) + ';'
| Python |
#!/usr/bin/python2.6
#
# Simple http server to emulate api.playfoursquare.com
import logging
import shutil
import sys
import urlparse
import SimpleHTTPServer
import BaseHTTPServer
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""Handle playfoursquare.com requests, for testing."""
def do_GET(self):
logging.warn('do_GET: %s, %s', self.command, self.path)
url = urlparse.urlparse(self.path)
logging.warn('do_GET: %s', url)
query = urlparse.parse_qs(url.query)
query_keys = [pair[0] for pair in query]
response = self.handle_url(url)
if response != None:
self.send_200()
shutil.copyfileobj(response, self.wfile)
self.wfile.close()
do_POST = do_GET
def handle_url(self, url):
path = None
if url.path == '/v1/venue':
path = '../captures/api/v1/venue.xml'
elif url.path == '/v1/addvenue':
path = '../captures/api/v1/venue.xml'
elif url.path == '/v1/venues':
path = '../captures/api/v1/venues.xml'
elif url.path == '/v1/user':
path = '../captures/api/v1/user.xml'
elif url.path == '/v1/checkcity':
path = '../captures/api/v1/checkcity.xml'
elif url.path == '/v1/checkins':
path = '../captures/api/v1/checkins.xml'
elif url.path == '/v1/cities':
path = '../captures/api/v1/cities.xml'
elif url.path == '/v1/switchcity':
path = '../captures/api/v1/switchcity.xml'
elif url.path == '/v1/tips':
path = '../captures/api/v1/tips.xml'
elif url.path == '/v1/checkin':
path = '../captures/api/v1/checkin.xml'
elif url.path == '/history/12345.rss':
path = '../captures/api/v1/feed.xml'
if path is None:
self.send_error(404)
else:
logging.warn('Using: %s' % path)
return open(path)
def send_200(self):
self.send_response(200)
self.send_header('Content-type', 'text/xml')
self.end_headers()
def main():
if len(sys.argv) > 1:
port = int(sys.argv[1])
else:
port = 8080
server_address = ('0.0.0.0', port)
httpd = BaseHTTPServer.HTTPServer(server_address, RequestHandler)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
if __name__ == '__main__':
main()
| Python |
#!/usr/bin/python
import os
import subprocess
import sys
BASEDIR = '../main/src/com/joelapenna/foursquare'
TYPESDIR = '../captures/types/v1'
captures = sys.argv[1:]
if not captures:
captures = os.listdir(TYPESDIR)
for f in captures:
basename = f.split('.')[0]
javaname = ''.join([c.capitalize() for c in basename.split('_')])
fullpath = os.path.join(TYPESDIR, f)
typepath = os.path.join(BASEDIR, 'types', javaname + '.java')
parserpath = os.path.join(BASEDIR, 'parsers', javaname + 'Parser.java')
cmd = 'python gen_class.py %s > %s' % (fullpath, typepath)
print cmd
subprocess.call(cmd, stdout=sys.stdout, shell=True)
cmd = 'python gen_parser.py %s > %s' % (fullpath, parserpath)
print cmd
subprocess.call(cmd, stdout=sys.stdout, shell=True)
| Python |
#!/usr/bin/python
"""
Pull a oAuth protected page from foursquare.
Expects ~/.oget to contain (one on each line):
CONSUMER_KEY
CONSUMER_KEY_SECRET
USERNAME
PASSWORD
Don't forget to chmod 600 the file!
"""
import httplib
import os
import re
import sys
import urllib
import urllib2
import urlparse
import user
from xml.dom import pulldom
from xml.dom import minidom
import oauth
"""From: http://groups.google.com/group/foursquare-api/web/oauth
@consumer = OAuth::Consumer.new("consumer_token","consumer_secret", {
:site => "http://foursquare.com",
:scheme => :header,
:http_method => :post,
:request_token_path => "/oauth/request_token",
:access_token_path => "/oauth/access_token",
:authorize_path => "/oauth/authorize"
})
"""
SERVER = 'api.foursquare.com:80'
CONTENT_TYPE_HEADER = {'Content-Type' :'application/x-www-form-urlencoded'}
SIGNATURE_METHOD = oauth.OAuthSignatureMethod_HMAC_SHA1()
AUTHEXCHANGE_URL = 'http://api.foursquare.com/v1/authexchange'
def parse_auth_response(auth_response):
return (
re.search('<oauth_token>(.*)</oauth_token>', auth_response).groups()[0],
re.search('<oauth_token_secret>(.*)</oauth_token_secret>',
auth_response).groups()[0]
)
def create_signed_oauth_request(username, password, consumer):
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
consumer, http_method='POST', http_url=AUTHEXCHANGE_URL,
parameters=dict(fs_username=username, fs_password=password))
oauth_request.sign_request(SIGNATURE_METHOD, consumer, None)
return oauth_request
def main():
url = urlparse.urlparse(sys.argv[1])
# Nevermind that the query can have repeated keys.
parameters = dict(urlparse.parse_qsl(url.query))
password_file = open(os.path.join(user.home, '.oget'))
lines = [line.strip() for line in password_file.readlines()]
if len(lines) == 4:
cons_key, cons_key_secret, username, password = lines
access_token = None
else:
cons_key, cons_key_secret, username, password, token, secret = lines
access_token = oauth.OAuthToken(token, secret)
consumer = oauth.OAuthConsumer(cons_key, cons_key_secret)
if not access_token:
oauth_request = create_signed_oauth_request(username, password, consumer)
connection = httplib.HTTPConnection(SERVER)
headers = {'Content-Type' :'application/x-www-form-urlencoded'}
connection.request(oauth_request.http_method, AUTHEXCHANGE_URL,
body=oauth_request.to_postdata(), headers=headers)
auth_response = connection.getresponse().read()
token = parse_auth_response(auth_response)
access_token = oauth.OAuthToken(*token)
open(os.path.join(user.home, '.oget'), 'w').write('\n'.join((
cons_key, cons_key_secret, username, password, token[0], token[1])))
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer,
access_token, http_method='POST', http_url=url.geturl(),
parameters=parameters)
oauth_request.sign_request(SIGNATURE_METHOD, consumer, access_token)
connection = httplib.HTTPConnection(SERVER)
connection.request(oauth_request.http_method, oauth_request.to_url(),
body=oauth_request.to_postdata(), headers=CONTENT_TYPE_HEADER)
print connection.getresponse().read()
#print minidom.parse(connection.getresponse()).toprettyxml(indent=' ')
if __name__ == '__main__':
main()
| Python |
#!/usr/bin/python
import datetime
import sys
import textwrap
import common
from xml.dom import pulldom
PARSER = """\
/**
* Copyright 2009 Joe LaPenna
*/
package com.joelapenna.foursquare.parsers;
import com.joelapenna.foursquare.Foursquare;
import com.joelapenna.foursquare.error.FoursquareError;
import com.joelapenna.foursquare.error.FoursquareParseException;
import com.joelapenna.foursquare.types.%(type_name)s;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Auto-generated: %(timestamp)s
*
* @author Joe LaPenna (joe@joelapenna.com)
* @param <T>
*/
public class %(type_name)sParser extends AbstractParser<%(type_name)s> {
private static final Logger LOG = Logger.getLogger(%(type_name)sParser.class.getCanonicalName());
private static final boolean DEBUG = Foursquare.PARSER_DEBUG;
@Override
public %(type_name)s parseInner(XmlPullParser parser) throws XmlPullParserException, IOException,
FoursquareError, FoursquareParseException {
parser.require(XmlPullParser.START_TAG, null, null);
%(type_name)s %(top_node_name)s = new %(type_name)s();
while (parser.nextTag() == XmlPullParser.START_TAG) {
String name = parser.getName();
%(stanzas)s
} else {
// Consume something we don't understand.
if (DEBUG) LOG.log(Level.FINE, "Found tag that we don't recognize: " + name);
skipSubTree(parser);
}
}
return %(top_node_name)s;
}
}"""
BOOLEAN_STANZA = """\
} else if ("%(name)s".equals(name)) {
%(top_node_name)s.set%(camel_name)s(Boolean.valueOf(parser.nextText()));
"""
GROUP_STANZA = """\
} else if ("%(name)s".equals(name)) {
%(top_node_name)s.set%(camel_name)s(new GroupParser(new %(sub_parser_camel_case)s()).parse(parser));
"""
COMPLEX_STANZA = """\
} else if ("%(name)s".equals(name)) {
%(top_node_name)s.set%(camel_name)s(new %(parser_name)s().parse(parser));
"""
STANZA = """\
} else if ("%(name)s".equals(name)) {
%(top_node_name)s.set%(camel_name)s(parser.nextText());
"""
def main():
type_name, top_node_name, attributes = common.WalkNodesForAttributes(
sys.argv[1])
GenerateClass(type_name, top_node_name, attributes)
def GenerateClass(type_name, top_node_name, attributes):
"""generate it.
type_name: the type of object the parser returns
top_node_name: the name of the object the parser returns.
per common.WalkNodsForAttributes
"""
stanzas = []
for name in sorted(attributes):
typ, children = attributes[name]
replacements = Replacements(top_node_name, name, typ, children)
if typ == common.BOOLEAN:
stanzas.append(BOOLEAN_STANZA % replacements)
elif typ == common.GROUP:
stanzas.append(GROUP_STANZA % replacements)
elif typ in common.COMPLEX:
stanzas.append(COMPLEX_STANZA % replacements)
else:
stanzas.append(STANZA % replacements)
if stanzas:
# pop off the extranious } else for the first conditional stanza.
stanzas[0] = stanzas[0].replace('} else ', '', 1)
replacements = Replacements(top_node_name, name, typ, [None])
replacements['stanzas'] = '\n'.join(stanzas).strip()
print PARSER % replacements
def Replacements(top_node_name, name, typ, children):
# CameCaseClassName
type_name = ''.join([word.capitalize() for word in top_node_name.split('_')])
# CamelCaseClassName
camel_name = ''.join([word.capitalize() for word in name.split('_')])
# camelCaseLocalName
attribute_name = camel_name.lower().capitalize()
# mFieldName
field_name = 'm' + camel_name
if children[0]:
sub_parser_camel_case = children[0] + 'Parser'
else:
sub_parser_camel_case = (camel_name[:-1] + 'Parser')
return {
'type_name': type_name,
'name': name,
'top_node_name': top_node_name,
'camel_name': camel_name,
'parser_name': typ + 'Parser',
'attribute_name': attribute_name,
'field_name': field_name,
'typ': typ,
'timestamp': datetime.datetime.now(),
'sub_parser_camel_case': sub_parser_camel_case,
'sub_type': children[0]
}
if __name__ == '__main__':
main()
| Python |
#!/usr/bin/python
import logging
from xml.dom import minidom
from xml.dom import pulldom
BOOLEAN = "boolean"
STRING = "String"
GROUP = "Group"
# Interfaces that all FoursquareTypes implement.
DEFAULT_INTERFACES = ['FoursquareType']
# Interfaces that specific FoursqureTypes implement.
INTERFACES = {
}
DEFAULT_CLASS_IMPORTS = [
]
CLASS_IMPORTS = {
# 'Checkin': DEFAULT_CLASS_IMPORTS + [
# 'import com.joelapenna.foursquare.filters.VenueFilterable'
# ],
# 'Venue': DEFAULT_CLASS_IMPORTS + [
# 'import com.joelapenna.foursquare.filters.VenueFilterable'
# ],
# 'Tip': DEFAULT_CLASS_IMPORTS + [
# 'import com.joelapenna.foursquare.filters.VenueFilterable'
# ],
}
COMPLEX = [
'Group',
'Badge',
'Beenhere',
'Checkin',
'CheckinResponse',
'City',
'Credentials',
'Data',
'Mayor',
'Rank',
'Score',
'Scoring',
'Settings',
'Stats',
'Tags',
'Tip',
'User',
'Venue',
]
TYPES = COMPLEX + ['boolean']
def WalkNodesForAttributes(path):
"""Parse the xml file getting all attributes.
<venue>
<attribute>value</attribute>
</venue>
Returns:
type_name - The java-style name the top node will have. "Venue"
top_node_name - unadultured name of the xml stanza, probably the type of
java class we're creating. "venue"
attributes - {'attribute': 'value'}
"""
doc = pulldom.parse(path)
type_name = None
top_node_name = None
attributes = {}
level = 0
for event, node in doc:
# For skipping parts of a tree.
if level > 0:
if event == pulldom.END_ELEMENT:
level-=1
logging.warn('(%s) Skip end: %s' % (str(level), node))
continue
elif event == pulldom.START_ELEMENT:
logging.warn('(%s) Skipping: %s' % (str(level), node))
level+=1
continue
if event == pulldom.START_ELEMENT:
logging.warn('Parsing: ' + node.tagName)
# Get the type name to use.
if type_name is None:
type_name = ''.join([word.capitalize()
for word in node.tagName.split('_')])
top_node_name = node.tagName
logging.warn('Found Top Node Name: ' + top_node_name)
continue
typ = node.getAttribute('type')
child = node.getAttribute('child')
# We don't want to walk complex types.
if typ in COMPLEX:
logging.warn('Found Complex: ' + node.tagName)
level = 1
elif typ not in TYPES:
logging.warn('Found String: ' + typ)
typ = STRING
else:
logging.warn('Found Type: ' + typ)
logging.warn('Adding: ' + str((node, typ)))
attributes.setdefault(node.tagName, (typ, [child]))
logging.warn('Attr: ' + str((type_name, top_node_name, attributes)))
return type_name, top_node_name, attributes
| Python |
#====================================================================
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# ====================================================================
#
# This software consists of voluntary contributions made by many
# individuals on behalf of the Apache Software Foundation. For more
# information on the Apache Software Foundation, please see
# <http://www.apache.org/>.
#
import os
import re
import tempfile
import shutil
ignore_pattern = re.compile('^(.svn|target|bin|classes)')
java_pattern = re.compile('^.*\.java')
annot_pattern = re.compile('import org\.apache\.http\.annotation\.')
def process_dir(dir):
files = os.listdir(dir)
for file in files:
f = os.path.join(dir, file)
if os.path.isdir(f):
if not ignore_pattern.match(file):
process_dir(f)
else:
if java_pattern.match(file):
process_source(f)
def process_source(filename):
tmp = tempfile.mkstemp()
tmpfd = tmp[0]
tmpfile = tmp[1]
try:
changed = False
dst = os.fdopen(tmpfd, 'w')
try:
src = open(filename)
try:
for line in src:
if annot_pattern.match(line):
changed = True
line = line.replace('import org.apache.http.annotation.', 'import net.jcip.annotations.')
dst.write(line)
finally:
src.close()
finally:
dst.close();
if changed:
shutil.move(tmpfile, filename)
else:
os.remove(tmpfile)
except:
os.remove(tmpfile)
process_dir('.')
| Python |
from sys import stdin
a, b, c = map(int, stdin.readline().strip().split())
print "%.3lf" % ((a+b+c)/3.0)
| Python |
from sys import stdin
from math import *
r, h = map(float, stdin.readline().strip().split())
print "Area = %.3lf" % (pi*r*r*2 + 2*pi*r*h)
| Python |
from sys import stdin
from math import *
n, = map(int, stdin.readline().strip().split())
rad = radians(n)
print "%.3lf %.3lf" % (sin(rad), cos(rad))
| Python |
from sys import stdin
n, = map(int, stdin.readline().strip().split())
print n*(n+1)/2
| Python |
from sys import stdin
a, b = map(int, stdin.readline().strip().split())
print b, a
| Python |
from sys import stdin
n, m = map(int, stdin.readline().strip().split())
a = (4*n-m)/2
b = n-a
if m % 2 == 1 or a < 0 or b < 0: print "No answer"
else: print a, b
| Python |
from sys import stdin
n, = map(int, stdin.readline().strip().split())
money = n * 95
if money >= 300: money *= 0.85
print "%.2lf" % money
| Python |
from sys import stdin
n, = map(int, stdin.readline().strip().split())
print ["yes", "no"][n % 2]
| Python |
from sys import stdin
from math import *
x1, y1, x2, y2 = map(float, stdin.readline().strip().split())
print "%.3lf" % hypot((x1-x2), (y1-y2))
| Python |
from sys import stdin
n = stdin.readline().strip().split()[0]
print '%c%c%c' % (n[2], n[1], n[0])
| Python |
from sys import stdin
x, = map(float, stdin.readline().strip().split())
print abs(x)
| Python |
from sys import stdin
from calendar import isleap
year, = map(int, stdin.readline().strip().split())
if isleap(year): print "yes"
else: print "no"
| Python |
from sys import stdin
f, = map(float, stdin.readline().strip().split())
print "%.3lf" % (5*(f-32)/9)
| Python |
from sys import stdin
a, b, c = map(int, stdin.readline().strip().split())
if a*a + b*b == c*c or a*a + c*c == b*b or b*b + c*c == a*a: print "yes"
elif a + b <= c or a + c <= b or b + c <= a: print "not a triangle"
else: print "no"
| Python |
from sys import stdin
a = map(int, stdin.readline().strip().split())
a.sort()
print a[0], a[1], a[2]
| Python |
Subsets and Splits
SQL Console for ajibawa-2023/Python-Code-Large
Provides a useful breakdown of language distribution in the training data, showing which languages have the most samples and helping identify potential imbalances across different language groups.