[Tutor] How to wrap ctype functions
Hi there, I wonder what's the best way to wrap given function calls (in this case ctype function calls but more generally built-in functions and those kinds). I have a huge c library and almost all functions return an error code. The library also contains a function, that returns the corresponding error message to the error code. So, what I need to do for every call to one of the libraries functions looks like this: error_code = my_c_library.SOME_FUNCTION_ CALL(argument) if error_code != 0: error_message = my_c_library.GET_ERROR_TEXT(error_code) print "error in function call SOME_FUNCTION_CALL" print error_message my_c_library.EXIT_AND_CLEAN_UP() Also, for some function calls I would need to do some preperations like: error_code = my_c_library.LOG_IN_AS_ADMINISTRATOR(admin_user, admin_password) error_code = my_c_library.SOME_FUNCTION_CALL(argument) I like the decorator idea, but I can't figure out if it's applicable here. To be able to call the function in a manner like this would be great, e.g. @change_user(admin_user, admin_password) @error_handling my_c_library.SOME_FUNCTION_CALL(argument) Best regards, Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] reading binary file on windows and linux
Hello, I've got some trouble reading binary files with struct.unpack on windows. According to the documentation of the binary file's content, at the beginning there're some simple bytes (labeled as 'UChar: 8-bit unsigned byte'). Within those bytes there's a sequence to check the file's sanity. The sequence is (in ascii C-Notation): " " "\n" "\r" "\n" " " I've downloaded the file from the same website from two machines. One is a Windows 7 64-Bit, the other one is a virtual Linux machine. Now the trouble is while on linux everything is fine, on windows the carriage return does not appear when reading the file with struct.unpack. The file sizes on Linux and Windows are exaktly the same, and also my script determines the file sizes correctly on both plattforms (according to the OS). When I open the file on Windows in an editor and display the whitespaces, the linefeed and cariage-return are shown a expected. The code I'm using to check the first 80 bytes of the file is: import struct import sys with open(sys.argv[1]) as source: size = struct.calcsize("80B") raw_data = struct.unpack("80B", source.read(size)) for i, data in enumerate(raw_data): print i, data, chr(data) source.seek(0, 2) print source.tell() Any suggestions are highly appreciated. Cheers, Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Design Question: File Object used everywhere
Hi there, I'm working on a code to read and write large amounts of binary data according to a given specification. In the specification there are a lot of "segments" defined. The segments in turn have defintions of datatypes and what they represent, how many of some of the data values are present in the file and sometimes the offset from the beginning of the file. Now I wonder, what would be a good way to model the code. Currently I have one class, that is the "FileReader". This class holds the file object, information about the endianess and also a method to read data (using the struct module). Then, I have more classes representing the segements. In those classes I define data-formats, call the read-method of the FileReader object and hold the data. Currently I'm passing the FileReader object as arguement. Here some examples, first the "FileReader" class: class JTFile(): def __init__(self, file_obj): self.file_stream = file_obj self.version_string = "" self.endian_format_prefix = "" def read_data(self, fmt, pos = None): format_size = struct.calcsize(fmt) if pos is not None: self.file_stream.seek(pos) return struct.unpack_from(self.endian_format_prefix + fmt, self.file_stream.read(format_size)) and here an example for a segment class that uses a FileReader instance (file_stream): class LSGSegement(): def __init__(self, file_stream): self.file_stream = file_stream self.lsg_root_element = None self._read_lsg_root() def _read_lsg_root(self): fmt = "80Bi" raw_data = self.file_stream.read_data(fmt) self.lsg_root_element = LSGRootElement(raw_data[:79], raw_data[79]) So, now I wonder, what would be a good pythonic way to model the FileReader class. Maybe use a global functions to avoid passing the FileReader object around? Or something like "Singleton" I've heard about but never used it? Or keept it like that? Cheers, Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] video watermark
Am 04.06.2010 09:21, schrieb Igor Nemilentsev: Hi everyone. Can someone suggest a python module so that it would be able to do a video processing and adding watermark in video? Once I had to blur the part of the viewing area of a screencast which showed some confident CAD model. First, I exported the video as sequence of images with virtualdub. The result was something like 3000 *.bmp files on my harddrive. Then I created a little python script using PIL (http://www.pythonware.com/products/pil/). That script processed the files file by file, so no worries about huge movie files and memory. Finally, I've merged the new images with virtual dub. It all worked very well. Otherwise I'm not aware of any video processing library in Python. Also, there're filters for virtualdub to add watermarks to a movie. That might be the easiest way to get your results. Cheers, Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Multiple inheritance for mixin attributes
Thanks a lot, your explanations are very helpful getting a better (right) understanding about the ideas behind inheritance. Cheers, Jan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor