Re: [Tutor] Dictionary Comprehensions
Lie Ryan dixit: > note that: > >>> [(y, x) for y in list("khalid") for x in range(6)] > [('k', 0), ('k', 1), ('k', 2), ('k', 3), ('k', 4), ('k', 5), ('h', 0), > ('h', 1), ('h', 2), ('h', 3), ('h', 4), ('h', 5), ('a', 0), ('a', 1), > ('a', 2), ('a', 3), ('a', 4), ('a', 5), ('l', 0), ('l', 1), ('l', 2), > ('l', 3), ('l', 4), ('l', 5), ('i', 0), ('i', 1), ('i', 2), ('i', 3), > ('i', 4), ('i', 5), ('d', 0), ('d', 1), ('d', 2), ('d', 3), ('d', 4), > ('d', 5)] > > and when that big list is turned into a dict it gives: > >>> dict(_) > {'a': 5, 'd': 5, 'i': 5, 'h': 5, 'k': 5, 'l': 5} ... because a dict holds a single value per key, so last value overrides previous ones. Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionary Comprehensions
Hugo Arts dixit: > bc = {y: x for x, y in enumerate("khalid")} > > Note that your output is like so: > {'a': 2, 'd': 5, 'i': 4, 'h': 1, 'k': 0, 'l': 3} > > The first character in your original string gets a zero, the second a > one, so on and so forth. I'm hoping that's what you meant. If you > really want this: > > {'a': 0, 'd': 1, 'i': 2, 'h': 3, 'k': 4, 'l': 5} > > I'm not sure how to do that programmatically. # first need a list of sorted chars # otherwise python cannot guess what order you mean: chars = sorted(list("khalid")) print chars# ==> ['a', 'd', 'h', 'i', 'k', 'l'] # enumerate gives a list of (index, value) pairs # from which you can construct a dict: #~ dc = {index:char for (index,char) in enumerate(chars)} # or (python version < 3) dc = dict((index,char) for (index,char) in enumerate(chars)) print dc # ==> {0: 'a', 1: 'd', 2: 'h', 3: 'i', 4: 'k', 5: 'l'} Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] saving output data in a file
Dave Kuhlman dixit: > On Fri, Dec 04, 2009 at 01:13:42PM +0530, Prasad Mehendale wrote: > > I am a beginner. I want to save the output data of the following programme > > in > > a file through the programme. Please suggest me the way. I am using Python > > 2.3.3 on mandrake linux 10 and using "Idle" to save the output to a file > > presently. > > Thanks in advance. > > > > #programme to calculate various parameters for a dc-generator. > > import math > > #import os > > #flux is assumed to be .005Wb, and A=parallel paths = 2 for wave winding > > polerpm=[] > > for ConductorsPerSlot in range(1,11): > > """ we consider that output voltage is 20 V DC """ > > PoleRpmProduct=2/ConductorsPerSlot > > polerpm.append(PoleRpmProduct) > > print '(Pole*RPM) product for various values of conductors/slot is: \n', > > polerpm > > for poles in range(2,18,2): > > print > > print '\n For number of poles='+str(poles) +' RPM values are: ' > > for i in range(len(polerpm)): > >rpm=polerpm[i]/poles > >print rpm, > > > > > > Another suggestion is to define a class that contains a method > named "write" which takes one argument which is the text to be > printed. > > This approach is useful when there are print statements in code > that is not under your control, for example imported modules. > > There is a note about this here: > > http://docs.python.org/library/sys.html#sys.stdout > > Here is an example: > > # > import sys > > class Redirect(object): > def __init__(self, filename): > self.outfile = open(filename, 'w') > self.count = 0 > def write(self, msg): > self.count += 1 > self.outfile.write('%d %s\n' % (self.count, msg, )) > def close(self): > self.outfile.close() > > def test(): > print 'starting' > save_stdout = sys.stdout > redir = Redirect('/tmp/tmp1.txt') > sys.stdout = redir > print 'something' > print 'something else' > redir.close() > sys.stdout = save_stdout > print 'finished' > > test() > # Hello, Thank you Dave for your class, sure I will use it often. -1- print & sys.stdout.write() Just discovered that print(text) is not equivalent to sys.stdout.write(text+'\n') but to sys.stdout.write(text) sys.stdout.write('\n') As shown below due to line numbers. I just added a debug output to (original sys.stdout I call) console. # change class Redirect(object): def __init__(self, filename): self.console = sys.stdout self.outfile = open(filename, 'w') self.count = 0 def write(self, msg): self.count += 1 self.outfile.write('%d %s\n' % (self.count, msg, )) self.console.write('%d %s\n' % (self.count, msg, )) def close(self): self.outfile.close() # ==> Hello starting 1 something 2 3 something else 4 finished === I find this behaviour rather annoying. Requires an ugly trick to workaround. -2- file subtype Is there a reason why you don't make Redirect directly (lol) a subtype of file? Actually I just tried it and got an error "bad file descriptor": # this is my new stdout: # when trying to print: File "__essai__.py", line 74, in test() File "__essai__.py", line 68, in test print 'something' IOError: [Errno 9] Bad file descriptor Below code of new class. Denis class Out(file): def __init__(self, filename, toconsole=True, numberlines=True): file.__init__(self, filename, 'r') print self # debug output to console self.toconsole = toconsole # line numbering self.numberlines = numberlines if self.numberlines: self.linenumber = 0 # save default stdout self.console = sys.stdout def write(self, msg): if self.numberlines: self.linenumber += 1 linenumber = "%3d " % self.linenumber else: linenumber = "" text = "%s%s\n" %(linenumber, msg) self.write(text) if self.toconsole: self.console.write(text) def close(self): # restore default stdout sys.stdout = self.console # close file self.close() Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] mod_python authentication
I need to setup a login page for a web application but I am not finding any code in the mod_python doc that shows me how to do this. What is need is the code to tell apache to get this login data from a login page. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] mod_python authentication
On Sat, Dec 5, 2009 at 8:26 PM, Rayon wrote: > I need to setup a login page for a web application but I am not finding any > code in the mod_python doc that shows me how to do this. > > What is need is the code to tell apache to get this login data from a login > page. If you want Apache to directly handle logins via HTTP auth then you don't need to write any code, just configure your vhost or .htaccess file (see for example the AuthUserFile directive). OTOH if you want to build your own login system (e.g. with user details stored in a database) then you: 1) Make a regular HTML form with username and password fields 2) Write whatever login processing code you need, and have the form submit to it. 3) Check for an active login session on every page that requires authentication, and redirect them back to the login form if necessary. HTH, benno. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] mod_python authentication
I need to setup a login page for a web application but I am not finding any code in the mod_python doc that shows me how to do this. What is need is the code to tell apache to get this login data from a login page. OTOH if you want to build your own login system (e.g. with user details stored in a database) then you: 1) Make a regular HTML form with username and password fields 2) Write whatever login processing code you need, and have the form submit to it. 3) Check for an active login session on every page that requires authentication, and redirect them back to the login form if necessary. I need to know how to configure the httpd file to allow for this al so. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor