Created
June 23, 2015 14:39
-
-
Save prashanthpai/4646148ba6c82af4ebaf to your computer and use it in GitHub Desktop.
gfapi binary file test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from ctypes import * | |
from gluster.gfapi import Volume | |
from array import array | |
class Point(Structure): | |
_fields_ = [('name', c_char_p), | |
('x', c_double), ('y', c_double), ('z', c_double)] | |
v = Volume("localhost", "test") | |
v.mount() | |
buf = bytearray(100) | |
with v.fopen("xyz", 'wb') as f: | |
p1 = Point("corner1", 1.6, 2.8, 3) | |
p2 = Point("center point", 1.1, -2, 0) | |
f.write(p1) | |
f.write(p2) | |
a = array('i', [1, 2, 3]) | |
f.write(a) | |
f.fsync() | |
with v.fopen("xyz", 'rb') as f: | |
p = Point() | |
for i in xrange(0, 2): | |
# Read directly into the object | |
f.readinto(p) | |
print p.name | |
print p.x | |
print p.y | |
print p.z | |
# Reading the array | |
s = f.read() # Returns a string | |
a = array('i') | |
a.fromstring(s) | |
print a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment