Re: [Tutor] url parsing
use this if you want to take a hammer to crack a nut :D import os url = 'http://this/is/my/url/to/parse' os.path.basename(url) #gives "parse" os.path.dirname(url) #gives 'http://this/is/my/url/to' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] modular program
Hi, I'd like to write a python program which can be easily extended by other people. Where can I find some "best practices" for writing modular programs? I thought about a txt file containing function calls that my program will parse and execute in order, or is it better just to execute every .py file in a certain "module" folder (I don't like this as modules could need to be executed in different moments)? Can any1 point me to a relatively simple program to look at? thanks in advance! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 61, Issue 3
> From: W W > Subject: Re: [Tutor] modular program >>Where can I find some "best practices" for writing modular programs? >> I thought about a txt file containing function calls that my program will >> parse and execute in order, or is it better just to execute every .py file >> in a certain "module" folder (I don't like this as modules could need to be >> executed in different moments)? > You can pretty much take a look at any of the modules in your python > library directory. In the case of my linux box, that's > /usr/lib/python2.5/ I'm sorry, I've realized I didn't explain my needs at all. I was a little influenced by drupal's definition of modules, which is completely different from python's. With module here I meant plug-in or extension: a piece of code written by someone else that can be easily (and automaticallly) integrated into my program. My program must provide the posibility to be extended without editing its code, just like mozilla's add-ons. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Edited] Plug-in enabled Program
> -- Messaggio inoltrato -- > From: "Alan Gauld" > To: tu...@python.org > Subject: Re: [Tutor] Tutor Digest, Vol 61, Issue 3 > > OK, To do that you need to provide an intrerface in your code > that the plug-in can use. That is to say you must call a documented > set of functions/methods and then arrange your code such that the > plugin can replace the default code. Thank you Alan, that's what I wanted :) I've understood the idea, and I'll investigate it. The only doubt I have is how to adapt it to multiple plugins related to the same interface. Let's say my program downloads a file from a url and saves it in a local directory. One plugin could be written to add a prefix to the file name and another one to make the filename all-caps. In this case the same interface (let's call it "ManageFileName") should be used twice (or, in general, many times), and it's not simple to decide a priori how to merge the two actions. This lets me think about decorators, is that a possibility? Each user could write it's own decorator to the "setFileName()" method and the program will use them together. As I don't know decorators in python at all, I'm not sure this could work out, but it could be a nice push to start learning :) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
> From: dukelx2...@gmail.com > To: tu...@python.org > Honestly I'm doing tutorials, I'm not in school. I am trying to learn it for > my own sake. > I got the *'s to come up but they are not forming the way I would like it to. > So that why I was asking for help. I suppose the trick here is to add a comma at the end of the print statement, to let the next one continue printing on the same line. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
> -- Messaggio inoltrato -- > Jared White wrote: > >> ** >> * I cant get a code to work for this bottom half, Can >> anyone help me Hi Jared, try this: for i in range(-10,11): print '*'*(11-abs(i)) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
> From: Padmanaban Ganesan > Subject: Re: [Tutor] (no subject) > Could any one explain me whats this in it abs(i)??? >>> for i in range(-10,11): >>> print '*'*(11-abs(i)) I'll try to explain it: with the first line you are looping through all integer from i = -10 to 10 the idea is to use i as the number of * for each line. using print '*' * i will print * exactly i times. so one could write for i in range(-10,11): print '*'*i but this will not work for negative values of i, so we use abs(i) (i.e. the absolute value of i). Now for i in range(-10,11): print '*'*abs(i) will produce ** * *** ** * *** ** * * ** *** * ** *** * ** which is not quite what we want, and that's the reason of the 11-abs(i) part.: for i in range(-10,11): print '*' * (11-abs(i)) so, for example, when i=-10 (the first line) we want only 1 *, and in fact 11-abs(i) = 11-abs(-10) = 11-10 = 1, so we get print '*' * 1. when i = -9 the expression gives 2, and so on. with i = 0 we get 11 * and we start then going down as for i = 1 we have 10 *, for i=2 we have 9 * and so on until i=10 which gives 1 *. Hope my english was clear enough :( ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to manage an encrypted file?
> From: Wayne > If you want the most basic encryption you could simply XOR the file. It's > fairly easy to break, though, because the same character patterns will be > present as with your original file. Actually if you do it properly this kind of encryption is unbreakable, but you'd have to: 1. generate a random key long enough to cover your data 2. keep the key secure and in a separate place 3. DON'T use the key twice there must be a Security Now episode that explains the thing pretty well (could be this one http://www.grc.com/sn/sn-011.htm). Some arguments may rise against the randomness of the key, but I don't think its the case to go into that. You could also use TrueCrypt which is an extremely powerful cryptographic software (open source), I found this article that can help you linking it with python: http://blog.bjrn.se/2008/01/truecrypt-explained.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to manage an encrypted file?
> From: "Alan Gauld" > To: tu...@python.org > Date: Mon, 22 Jun 2009 14:15:55 +0100 > Subject: Re: [Tutor] how to manage an encrypted file? >> >> there must be a Security Now episode that explains the thing pretty >> well (could be this one http://www.grc.com/sn/sn-011.htm). > > Eek! I tried to read that but it was so garbled and basically illiterate > that I gave up! It's the nearest thing I've seen to plain text encryption! eheh Alan try the audio version, it's far better ;P ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] General design question
>> If I have a set of numbers, or strings etc. which have been generated and I >> then want to do something with them, for example a sum function call. Is >> the best way to put those items in a list, or similar container, before >> applying the function. > > Not only "best" but "necessary". the sum () builtin takes an > iterator -- often a list, but needn't be -- of numbers. I'm a total newbie, but wouldn't it be possible to use a set (from which I think you can get an iterator)? By the way, if you only need to sum a range(n, m) you can use the formula [ m*(m-1) - n*(n-1) ] / 2 or the equivalent (m+n-1)*(m-n)/2. Sorry for pointing out this last thing, of course it is not general at all, but maybe you'll find it useful anyway. Daniele ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Configuration File Pattern
Hi list, I've recently developed a basic python program which needs to store some data in a file (e.g. a directory path). What the program needs is a dictionary containing the data, so I used the pickle module to store the dictionary in a file and read it back when the program is launched. I wanted to know which is the common pattern in python programming in this case, because the way I've choosen only lets the user configure the data via a GUI (the file is somehow "compiled"). Is it better to store the data in a text file and then parse it and construct a dictionary? Or is there e third way? Thanks, Daniele ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Configuration File Pattern
> What I use in this situation is the INI config file parser in the > standard lib. It's easy to use > > http://docs.python.org/lib/module-ConfigParser.html > Awesome! Really easy and intuitive. Thanks a lot ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] urllib2 and php authentication
Hi, I'm trying to connect to a site that requires a php login. I used the urllib2 module in the following way: >>> import urllib2 >>> user='xxx' >>> password='yyy' >>> hp = urllib2.HTTPPasswordMgr() >>> uri = 'http://s2.ikariam.it/index.php?action=loginAvatar&function=login' >>> hp.add_password(None, uri, user, password) >>> opener = urllib2.build_opener(urllib2.HTTPBasicAuthHandler( hp)) >>> req = urllib2.Request(uri) >>> opener.open(req).readlines() Needless to say, it doesn't work. I get an html page back from the opener saying username and/or password are invalid. Can anyone point me in the right direction? thanks a lot, Daniele ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Name Generator
Hi list, I'd like to implement a Name Generator based on a grammar ( http://en.wikipedia.org/wiki/Context-free_grammar). Are there any standard modules that automatically generate words based on a grammar? thanks for help, Daniele ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] audio splitting with python
Hi list, I'd like to split an ogg audio file into pieces (small enough to fit in an audio cd). Can anybody suggest me a python module to do that? Thanks, Daniele ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How trustworthy are pseudo-random numbers?
>> [Alec Henriksen] >> > How trustworthy is the "randomness" generated by the random module? >> >> Python uses the Mersenne Twister algorithm for generating >> pseudo-random numbers, and that's one of the highest-quality methods >> known. >From here http://en.wikipedia.org/wiki/Pseudorandom_number_generator#Periodicity and here http://en.wikipedia.org/wiki/Mersenne_twister#Advantages I think it can be argued that the randomness is pretty trustworthy :o) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How trustworthy are pseudo-random numbers?
2008/10/3 Andre Engels <[EMAIL PROTECTED]>: > On Fri, Oct 3, 2008 at 4:11 PM, Daniele <[EMAIL PROTECTED]> wrote: > If you used every atom in the known universe as a computer, then let > them turn out a billion combinations a second for the entire time > since the big bang, and call the number of combination you get then > N [...].. then the number of combinations turned out by N3 computers turning > out > N3 combinations per second in the time since the big bang STILL > dwarves in comparison to that number. Excellent Proof of concept! ,-) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Default parameter in class method
Hi all, I know very little about OOP in Python, I'm working on it but, for the time being, here's my problem: I want to create a method of a class with a default value. The problem is that this default value should be an instance field of that same class. For example: class Test(): def __init__(self): self.field='Default' def myMethod(self, parameter=self.field): pass I'm getting an error like "name 'self' is not defined"; it seems I cannot access self.field in the method definition. Is there a work-around (or maybe just the proper way to do it)? Thank you very much, Daniele ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Default parameter in class method
2008/10/7 Kent Johnson <[EMAIL PROTECTED]>: > On Tue, Oct 7, 2008 at 10:08 AM, Daniele <[EMAIL PROTECTED]> wrote: > def myMethod(self, parameter=_marker): > if parameter==_marker: >parameter = self.field Thanks Kent and Alan, I've understood the point and I think I'll use Kent's solution :) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] what does the "@" operator mean?
> From: "Alan Gauld" > Subject: Re: [Tutor] what does the "@" operator mean? Thinking it's quite funny, I'll keep on with italian words: the @ is called "chiocciola" which means snail, while # is called "cancelletto" which is a small gate As you see italian words are quite close to the sign shape, like in Denmark i guess (trunk-a for @ is fantastic! :-P ) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Deleting recursive folder definition
> From: "Alan Gauld" > Date: Mon, 12 Jan 2009 19:07:23 - > Subject: [Tutor] Deleting recursive folder definition > I've managed to do something incredibly stupid on my XP box. > I've created a folder that is a link to itself - at least I think that's what > has > happened, it was a CASE tool that I was using that actually did the damage. > Any ideas? Hi Alan, I would try to boot from a linux live cd and remove the folder that way, also try the unlink unix command. Daniele ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor