[Tutor] creating a buffer object from a file ?
Iyer
maseriyer at yahoo.com
Wed May 23 17:50:10 CEST 2007
thanks, alan for your helpful response.
in reality what is a buffer object used for ? reading
a file itself creates a string as in itself,
file_handle = file ("path_to_file")
file_data = file_handle.read()
# file_data is a string, so why is a buffer object is
needed ?
the data in the binary file is just raw binary.
I apologize for replying to the existing subject.
Thanks for letting me know. I shall make sure this
doesn't happen again.
thanks
iyer
--- Alan Gauld <alan.gauld at btinternet.com> wrote:
> "Iyer" <maseriyer at yahoo.com> wrote
>
> > How do I go about creating a buffer object from
> > a file containing binary data ? I have a function
> > that accepts only buffer objects for it's
> parameters
>
> Can you define what you mean by a buffer object?
> Python uses duck typing so, unless the function has
> been badly coded with an explicit type check, it
> should accept any object that supports the methods
> used.
>
> If you really do need a buffer the docs say:
>
> -----
> Buffer objects are not directly supported by Python
> syntax, but can be created by calling the builtin
> function buffer(). They don't support concatenation
> or repetition.
> -----
>
> Which was new to me. But some experimentation
> with the interpreter shows:
> ----
> class buffer(object)
> | buffer(object [, offset[, size]])
> |
> | Create a new buffer object which references the
> given object.
> | The buffer will reference a slice of the target
> object from the
> | start of the object (or at the specified
> offset). The slice will
> | extend to the end of the target object (or with
> the specified
> size).
> ---
> and
> ----
> >>> b = buffer('fredrica', 2,4)
> >>> b[:]
> 'edri'
> ----
>
> So we can see how to create a buffer object.
> You want to do it with a binary file. You can read
> the content
> of a binary file using the struct module. But you
> need to know
> what kind of data is in your file. To create a
> buffer you need
> a string. So do you want your buffer to process the
> raw binary
> bytes as if they were a string? Or do you want to
> convert the
> binary data and then convert it again into a string
> representation?
>
> Either is possible but you need to decide which you
> need.
>
> BTW Please don't post new subjects to the list by
> replying
> to an existing subject. For those using threaded
> readers it
> buries your post insife another thread, in this case
> 3 levels
> deep in one about MSSQL! I only just noticed it. Its
> better
> to start a fresh message. After all its not exactly
> difficult to
> type tutor at python.org in the to line! :-)
>
> HTH,
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
____________________________________________________________________________________Luggage? GPS? Comic books?
Check out fitting gifts for grads at Yahoo! Search
http://search.yahoo.com/search?fr=oni_on_mail&p=graduation+gifts&cs=bz
More information about the Tutor
mailing list