Trouble with regular expressions
Hi, I'm quite new to regular expressions, and I wonder if anyone here could help me out. I'm looking to split strings that ideally look like this: Update: New item (Household) into a group. This expression works ok: '^(Update:)?(.*)(\(.*\))$' - it returns (Update, New item, (Household)) Some strings will look like this however: Update: New item (item) (Household). The expression above still does its job, as it returns (Update, New item (item), (Household)). It does not work however when there is no text in parentheses (eg Update: new item). How can I get the expression to return a tuple such as (Update:, new item, None)? Thanks in advance, Mathieu -- http://mail.python.org/mailman/listinfo/python-list
Re: Calculate sha1 hash of a binary file
Thanks all! -- http://mail.python.org/mailman/listinfo/python-list
Re: Calculate sha1 hash of a binary file
I did some testing, and calculating the hash value of a 1Gb file does take some time using this method. Would it be wise to calculate the hash value based on say for instance the first Mb? Is there a much larger chance of collusion this way (I suppose not). If it's helpful, the files would primarily be media (video) files. Thanks, Mathieu -- http://mail.python.org/mailman/listinfo/python-list
Re: Calculate sha1 hash of a binary file
On Aug 7, 2:22 pm, Paul Rubin http://[EMAIL PROTECTED] wrote: LaundroMat [EMAIL PROTECTED] writes: Would it be wise to calculate the hash value based on say for instance the first Mb? Is there a much larger chance of collusion this way (I suppose not). If it's helpful, the files would primarily be media (video) files. The usual purpose of using this type of hash is to detect corruption and/or tampering. So you want to hash the whole file, not just part of it. If you're not worried about intentional tampering, md5 should be somewhat faster than sha, but there are some attacks against it and you shouldn't use it for high security applications where you want security against forgery. It should still have almost no chance of accidental collisions. Well, what I really intend to do is store the file hashes, in order to be able to recognise the files later on when they are stored on another location, and under another filename. It's not so much tampering I'm concerned with. -- http://mail.python.org/mailman/listinfo/python-list
Calculate sha1 hash of a binary file
Hi - I'm trying to calculate unique hash values for binary files, independent of their location and filename, and I was wondering whether I'm going in the right direction. Basically, the hash values are calculated thusly: f = open('binaryfile.bin') import hashlib h = hashlib.sha1() h.update(f.read()) hash = h.hexdigest() f.close() A quick try-out shows that effectively, after renaming a file, its hash remains the same as it was before. I have my doubts however as to the usefulness of this. As f.read() does not seem to read until the end of the file (for a 3.3MB file only a string of 639 bytes is being returned, perhaps a 00-byte counts as EOF?), is there a high danger for collusion? Are there better ways of calculating hash values of binary files? Thanks in advance, Mathieu -- http://mail.python.org/mailman/listinfo/python-list
Reproducing a web page and add own content to it.
Hi - I'm working on a Django powered site where one of the required functionalities is the possibility of displaying the content of external pages, with an extra banner at the top where specific information is displayed. In other words, I'm looking for a way to reproduce an existing web page and add some HTML code to it. (I can't think of an example right now, but the idea is similar to sites that let you see an external page and have some site-specific text above it (often stating that the content below is not part of the site the user comes from)). To test this, I've been downloading an external page, adding some text to it and re-opening it in a browser (with the help of built-in modules such as urllib2 etc). This works of course, but the external page's links such as img src=hello.png, or a href=help.html are evidently no longer correct. Apart from parsing the whole file and trying to inject the external site's domain in links such as the above (with the added inconvenience of having to store the external page locally), is there an easier way of accomplishing what I want? Thanks, Mathieu -- http://mail.python.org/mailman/listinfo/python-list
Re: Reproducing a web page and add own content to it.
On Apr 8, 2:04 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote: LaundroMat wrote: Hi - I'm working on a Django powered site where one of the required functionalities is the possibility of displaying the content of external pages, with an extra banner at the top where specific information is displayed. In other words, I'm looking for a way to reproduce an existing web page and add some HTML code to it. (I can't think of an example right now, but the idea is similar to sites that let you see an external page and have some site-specific text above it (often stating that the content below is not part of the site the user comes from)). To test this, I've been downloading an external page, adding some text to it and re-opening it in a browser (with the help of built-in modules such as urllib2 etc). This works of course, but the external page's links such as img src=hello.png, or a href=help.html are evidently no longer correct. Apart from parsing the whole file and trying to inject the external site's domain in links such as the above (with the added inconvenience of having to store the external page locally), is there an easier way of accomplishing what I want? Using a frame? Diez Ack. I was too focused on importing the external web page and redisplaying the information (I've just been reading up on BeautifulSoup) instead of looking for an HTML based approach. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Reproducing a web page and add own content to it.
On Apr 8, 4:11 pm, Steve Holden [EMAIL PROTECTED] wrote: LaundroMat wrote: On Apr 8, 2:04 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote: LaundroMat wrote: Hi - I'm working on a Django powered site where one of the required functionalities is the possibility of displaying the content of external pages, with an extra banner at the top where specific information is displayed. In other words, I'm looking for a way to reproduce an existing web page and add some HTML code to it. (I can't think of an example right now, but the idea is similar to sites that let you see an external page and have some site-specific text above it (often stating that the content below is not part of the site the user comes from)). To test this, I've been downloading an external page, adding some text to it and re-opening it in a browser (with the help of built-in modules such as urllib2 etc). This works of course, but the external page's links such as img src=hello.png, or a href=help.html are evidently no longer correct. Apart from parsing the whole file and trying to inject the external site's domain in links such as the above (with the added inconvenience of having to store the external page locally), is there an easier way of accomplishing what I want? Using a frame? Diez Ack. I was too focused on importing the external web page and redisplaying the information (I've just been reading up on BeautifulSoup) instead of looking for an HTML based approach. Thanks! You could also look at adding a base tag to your generated page's head section. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ True, but I suppose that users would no longer see the top banner added by me when they click on one of the links on the external site's page. I'm a bit hesitant about using frames however, but reading up on them makes me think the application I have in mind for them might be the generally accepted exception to the rule that frames are bad :) Anyway. Thanks for the help! -- http://mail.python.org/mailman/listinfo/python-list
Re: reading id3 tags with python
On Nov 24, 5:42 pm, jeff [EMAIL PROTECTED] wrote: [snip] and what do you mean by 'id3reader' cant do directories? my for loop just does each file in the dirextory It's just a friendly warning that you shouldn't suppose that all that is scanned are indeed files, and not directories. -- http://mail.python.org/mailman/listinfo/python-list
Re: reading id3 tags with python
Heh, a description of the error would be nice indeed. Just a preliminary warning: with this code you will also be parsing directories. id3reader can't handle those ofcourse. Better add a check such as eg: if os.path.isfile(os.path.join(directory, file)): # do your thing laundro -- http://mail.python.org/mailman/listinfo/python-list
VLC-python bindings
Hi, Has anyone succeeded in compiling the vlc-python bindings on Windows? (as discussed in the vlc development wiki). I have found several compiled bindings, but none were updated for Python 2.4 (or 2.5 for that matter). As I am a complete layman for everything that is C compilation on any platform, could anyone point me to vlc-python bindings that work out of the box? Many thanks in advance, Mathieu -- http://mail.python.org/mailman/listinfo/python-list
Help me understand this iterator
Hi, I've found this script over at effbot (http://effbot.org/librarybook/os-path.htm), and I can't get my head around its inner workings. Here's the script: import os class DirectoryWalker: # a forward iterator that traverses a directory tree def __init__(self, directory): self.stack = [directory] self.files = [] self.index = 0 def __getitem__(self, index): while 1: try: file = self.files[self.index] self.index = self.index + 1 except IndexError: # pop next directory from stack self.directory = self.stack.pop() self.files = os.listdir(self.directory) self.index = 0 else: # got a filename fullname = os.path.join(self.directory, file) if os.path.isdir(fullname) and not os.path.islink(fullname): self.stack.append(fullname) return fullname for file in DirectoryWalker(.): print file Now, if I look at this script step by step, I don't understand: - what is being iterated over (what is being called by file in DirectoryWalker()?); - where it gets the index value from; - where the while 1:-loop is quitted. Thanks in advance, Mathieu -- http://mail.python.org/mailman/listinfo/python-list
Re: Help me understand this iterator
Ack, I get it now. It's not the variable's name (index) that is hard-coded, it's just that the for...in... loop sends an argument by default. That's a lot more comforting. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help me understand this iterator
Thanks all, those were some great explanations. It seems I have still still a long way for me to go before I grasp the intricacies of this language. That 'magic index' variable bugs me a little however. It gives me the same feeling as when I see hard-coded variables. I suppose the generator class has taken care of this with its next() method (although - I should have a look - __next__() probable takes self and index as its arguments). Although I'm very fond of the language (as a non-formally trained hobbyist developer), that magic bit is a tad disturbing. Still, thanks for the quick and complete replies! -- http://mail.python.org/mailman/listinfo/python-list
Re: Help me understand this iterator
On Oct 31, 3:53 pm, Fredrik Lundh [EMAIL PROTECTED] wrote: LaundroMat wrote: That 'magic index' variable bugs me a little however. It gives me the same feeling as when I see hard-coded variables.what magic index? the variable named index is an argument to the method it's used in. Yes, I reacted too quickly. Sorry. -- http://mail.python.org/mailman/listinfo/python-list
Re: What value should be passed to make a function use the default argument value?
Rob De Almeida wrote: LaundroMat wrote: Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to pass a value to f()? None doesn't seem to work.. If you *absolutely* want to pass a value and you don't know the default value (otherwise you could just pass it): import inspect v = inspect.getargspec(f)[3][0] # first default value f(v) 2 I have in fact a bunch of functions that all pass similar information to one main function. That function takes (amongst others) a template variable. If it's not being passed, it is set to a default value by the function called upon. For the moment, whenever a function calls the main function, I check whether the calling function has the template variable set: if template: return mainFunction(var, template) else: return mainFunction(var) Now, I thought this isn't the cleanest way to do things; so I was looking for ways to initialize the template variable, so that I could always return mainFunction(var, template). mainFunction() would then assign the default value to template. From your answers, this seems to be impossible. The minute my variable is initialised, there's no way I can have mainFunction() assign a value without explicitly asking it to do so. I guess the best way would then be to change mainFunction from: def mainFunction(var, template='base'): to def mainFunction(var, template): if len(template)=0: template = 'base' and have the calling functions call mainFunction (var, template) and initialise template to ''. I still have that nagging feeling nicer code could be written to solve this, but I won't try to lose any sleep over it. Thanks for all the replies. -- http://mail.python.org/mailman/listinfo/python-list
Re: What value should be passed to make a function use the default argument value?
Antoon Pardon wrote: The problem is like the following. def f(var=1): return var*2 def g(): arg = None try: arg = Try_Processing() / 3 + 1 except Nothing_To_Process: pass if arg is None: return f() else: return f(arg) Now in this case you could start by assigning arg the value 1 and eliminate the if test. However that only works if you know the default value for the argument. What he seems to be asking for is if there is an object, (let as call it Default), that would make code like: def f(var=1): Equivallent to: def f(var=Default) if var is Default) var = 1 So that we could write the following without the need of the f's default value. def g(): arg = Default try: arg = Try_Processing() / 3 + 1 except Nothing_To_Process: pass f(arg) -- Antoon Pardon Exactly. Thanks for helping out. -- http://mail.python.org/mailman/listinfo/python-list
What value should be passed to make a function use the default argument value?
Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to pass a value to f()? None doesn't seem to work.. Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list