Re: What is self.file = file for?
If you are familiar to C++ or a similar language, the concept of the this pointer might not be alien to you. self in this context is basically a reference to the class itself. Hence self.file is creating a class member and setting to the input from file. As Gary pointed out, during initialization, only the latter parameter i.e. file is being passed to __init__ You can get a brief tutorial from http://docs.python.org/tut/node11.html about classes in python. On May 14, 3:08 am, [EMAIL PROTECTED] wrote: > Hello! > > I have trouble understanding something in this code snippet: > > class TextReader: > """Print and number lines in a text file.""" > def __init__(self, file): > self.file = file > . > . > . > > When would you do a thing like self.file = file ? I really don't > find an answer on this. Please help me understand this. -- http://mail.python.org/mailman/listinfo/python-list
Re: built in list generator?
range(50,100,2) returns a list of numbers starting from 50 and less than 100 with a step size of 2. list() takes any iterable datatype and converts it into a list. e.g. list((1, 2, 3)) would return [1,2,3] & list([1, 2]) would return [1,2] In this case there is no point of calling range within list since the output of range is already a list. Note list(xrange(50,100,2)) would have made sense if range didnt exist and you needed a list, but since range does exist, I dont see the point. Diez is right when he says to use list to iterate, because creating a big list just for the sake of iteration would be a terrible waste of space. On May 14, 1:16 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > globalrev schrieb: > > > if i want a list with all numbers between x and y is there a way to > > do this with an inbult function. > > > i mean i can always construct a function to do this but is there > > soemthing like: > > > nbrs = list(range(50,100, 2)) > > range *does* that. use xrange if all you want is to iterate. > > Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: usage of python
If I were you, I'd show them actual code and how easy it is to get things done. Showing them how to implement a GTalk Bot[http:// code.google.com/p/pygtalkrobot/] or how to build simple arcade games with PyGame[http://www.pygame.org/news.html] would harbor much more interest in my opinion because it'll show them their own power. There may be some people who oppose this approach under the pretext that students taking introductory programming courses should be taught C because it incorporates the discipline that is much needed if they want to become hard core CS researchers. Anyways something that you can show and they will like is Frets On Fire : [http://fretsonfire.sourceforge.net/]. Plus Pixar uses Python in its rendering pipeline [Linux Mag July 2007]. My 0.02 Republican Credits On May 13, 10:10 pm, Rajarshi <[EMAIL PROTECTED]> wrote: > Hi, I teach an introductory programming course in Python. As part of > the introduction I'd like to highlight the usage of Python in > industry. The idea is to show that there are big players using Python > for a variety of tasks. Given that the students come from a variety of > backgrounds and are not necessarily 'hackers', I'm trying to look for > examples with a bit of wow-factor. > > Is there any list with people/groups/companies using Python for > impressive things? > > Any pointers would be appreciated > > Thanks, > Rajarshi -- http://mail.python.org/mailman/listinfo/python-list
Re: FTP upload issue
First of all, it would be better to use:- ftp.storlines("STOR " + remoteFileName, open(localFileName, "rb")) rather than:- ftp.storlines("STOR" + filename, file(filename)) Since the Python Documentation has this to say for open(): [Although ] When opening a file, it's preferable to use open() instead of invoking this constructor directly. file is more suited to type testing (for example, writing "isinstance(f, file)"). Secondly Referring to another mail thread : http://mail.python.org/pipermail/tutor/2006-February/045347.html Quoting Kent : fname should be just a bare name, e.g. 'mac.info2.txt'. You have to ftp.cwd() to the correct directory yourself. If it does not exists, create a tmp directory. You're currently passing /tmp/mac.info2.txt to the ftp. This should hopefully solve your problem. P.S. I personally used to use ftputil[Really easy to use] and now I use twisted. These libraries abstract a lot of stuff out for you. On May 16, 1:25 am, davidj411 <[EMAIL PROTECTED]> wrote: > I am having difficulty uploading a text file using Python 2.5 on MAC > OSX. > > SCRIPT > > filename='/tmp/mac.info2.txt' > fh=open(filename,'w') > fh.write('yes, i have a mac but don't hold that against me - just > example data') > fh.close() > > from ftplib import FTP > 'host, username, and password are string variables that have already > been defined. > ftp = FTP(host) > ftp.login(username,password) > 'so far, logged in, all is well , error is on next line. > > ftp.storlines("STOR" + filename, file(filename)) > ftp.quit() > > What am i doing wrong? the file DOES exist, the path is correct, and > the file was closed after being written. file(filename) should open > it again for the upload? > > http://www.python.org/doc/lib/ftp-objects.htmlsays that the command > should be an appropriate "STOR" command: "STOR filename". file is an > open file object which is read... > > ERROR > File "mac.inventory.py", line 44, in > ftp.storlines("STOR " + filename, file(filename)) > File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/ > python2.5/ftplib.py", line 437, in storlines > conn = self.transfercmd(cmd) > File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/ > python2.5/ftplib.py", line 356, in transfercmd > return self.ntransfercmd(cmd, rest)[0] > File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/ > python2.5/ftplib.py", line 327, in ntransfercmd > resp = self.sendcmd(cmd) > File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/ > python2.5/ftplib.py", line 241, in sendcmd > return self.getresp() > File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/ > python2.5/ftplib.py", line 216, in getresp > raise error_perm, resp > ftplib.error_perm: 550 /tmp/mac.info2.txt: The system cannot find the > path specified. -- http://mail.python.org/mailman/listinfo/python-list
Re: Making Variable Text Output More Pythonic?
Arnaud's code wont work if self.opt1 is None, an empty list, an empty tuple, False, etc, because all these evaluate to false. They wont print the internal state of these variables. [Just an informational notice, this may be the behavior you expect] Secondly, I'm not sure if you know the variable names from before hand in which case Casey's approach will work, or you need to know them via introspection. http://www.ibm.com/developerworks/library/l-pyint.html [Scroll down to attributes]. On May 16, 1:44 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > Casey <[EMAIL PROTECTED]> writes: > > Hi, > > > I have some classes that print variable outputs depending on their > > internal state, like so: > > > def __str__(self): > > out = [] > > if self.opt1: out += ['option 1 is %s' % self.opt1'] > > if self.opt2: out += ['option 2 is %s' % self.opt2'] > > > > return '\n'.join(out) > > > Is there any way to make this cleaner? > > Maybe. > > Have a dictionary of options rather than individual attributes; > options not in the dictionary are not set. E.g. > > mask = { > 'opt1': 'option 1 is %s', > 'opt2': 'option 2 is %s', > ... > } > > def __str__(self): > return '\n'.join(mask[o] % v for o,v in self.options.iteritems()) > > -- > Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: ?
The following proposed solution is not intended to be a solution, it goes completely against the zen of python. [Type import this into the python command interpreter] I brought it down to two lines:- l = range(6) [1 if b!=4 else l.__delslice__(0,len(l)) for b in l][:-1] itertools would still be a better approach in my opinion. Just because I'm curious to know, can anyone bring it shorter[even if its cryptic] than this without invoking any Python Library. P.S. Once again I would not recommend using this as Explicit is better than Implicit P.P.S. It is strongly undesirable for us humans to use anything starting with __ :) On May 15, 5:10 pm, "Geoffrey Clements" <[EMAIL PROTECTED]> wrote: > "urikaluzhny" <[EMAIL PROTECTED]> wrote in message > > news:[EMAIL PROTECTED] > On May 15, 10:06 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > > > "urikaluzhny" <[EMAIL PROTECTED]> wrote in message > > >news:[EMAIL PROTECTED] > > | It seems that I rather frequently need a list or iterator of the form > > | [x for x in <> while <>] > > > I can think of two ways to interpret that. > >> I mean like [x for x in if ], only that it breaks the loop when > >> the expression is false. > > def gen(a): > for x in a: > if B: break > yield x > > a_gen = gen(A) > > # now iterate over a_gen > > -- > Geoff -- http://mail.python.org/mailman/listinfo/python-list
Re: ?
According to http://en.wikipedia.org/wiki/Expression_(programming) "An expression in a programming language is a combination of values, variables, operators, and functions that are interpreted (evaluated) according to the particular rules of precedence and of association for a particular programming language, which computes and then produces (returns, in a stateful environment) another value." l.__delslice__(0,len(l)) is an expression because it returns None [which also happens to be a value] in this case. On May 16, 4:23 am, castironpi <[EMAIL PROTECTED]> wrote: > On May 15, 6:07 pm, afrobeard <[EMAIL PROTECTED]> wrote: > > > > > The following proposed solution is not intended to be a solution, it > > goes completely against the zen of python. [Type import this into the > > python command interpreter] > > > I brought it down to two lines:- > > > l = range(6) > > [1 if b!=4 else l.__delslice__(0,len(l)) for b in l][:-1] > > > itertools would still be a better approach in my opinion. > > > Just because I'm curious to know, can anyone bring it shorter[even if > > its cryptic] than this without invoking any Python Library. > > > P.S. Once again I would not recommend using this as Explicit is better > > than Implicit > > P.P.S. It is strongly undesirable for us humans to use anything > > starting with __ :) > > > On May 15, 5:10 pm, "Geoffrey Clements" > > > <[EMAIL PROTECTED]> wrote: > > > "urikaluzhny" <[EMAIL PROTECTED]> wrote in message > > > >news:[EMAIL PROTECTED] > > > On May 15, 10:06 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > > > > > "urikaluzhny" <[EMAIL PROTECTED]> wrote in message > > > > >news:[EMAIL PROTECTED] > > > > | It seems that I rather frequently need a list or iterator of the form > > > > | [x for x in <> while <>] > > > > > I can think of two ways to interpret that. > > > >> I mean like [x for x in if ], only that it breaks the loop when > > > >> the expression is false. > > > > def gen(a): > > > for x in a: > > > if B: break > > > yield x > > > > a_gen = gen(A) > > > > # now iterate over a_gen > > > > -- > > > Geoff- Hide quoted text - > > > - Show quoted text - > > In your original, you have: > > > l = range(6) > > [1 if b!=4 else l.__delslice__(0,len(l)) for b in l][:-1] > > You may be hyperextending the use of '..if..else..', which is one of > my fears regarding 'with x as y'. "l.__delslice__(0,len(l))" is not > an expression. -- http://mail.python.org/mailman/listinfo/python-list
Re: ?
l.__delslice__(0,len(l)) is an expression as it returns None which is a value On May 16, 4:23 am, castironpi <[EMAIL PROTECTED]> wrote: > On May 15, 6:07 pm, afrobeard <[EMAIL PROTECTED]> wrote: > > > > > The following proposed solution is not intended to be a solution, it > > goes completely against the zen of python. [Type import this into the > > python command interpreter] > > > I brought it down to two lines:- > > > l = range(6) > > [1 if b!=4 else l.__delslice__(0,len(l)) for b in l][:-1] > > > itertools would still be a better approach in my opinion. > > > Just because I'm curious to know, can anyone bring it shorter[even if > > its cryptic] than this without invoking any Python Library. > > > P.S. Once again I would not recommend using this as Explicit is better > > than Implicit > > P.P.S. It is strongly undesirable for us humans to use anything > > starting with __ :) > > > On May 15, 5:10 pm, "Geoffrey Clements" > > > <[EMAIL PROTECTED]> wrote: > > > "urikaluzhny" <[EMAIL PROTECTED]> wrote in message > > > >news:[EMAIL PROTECTED] > > > On May 15, 10:06 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > > > > > "urikaluzhny" <[EMAIL PROTECTED]> wrote in message > > > > >news:[EMAIL PROTECTED] > > > > | It seems that I rather frequently need a list or iterator of the form > > > > | [x for x in <> while <>] > > > > > I can think of two ways to interpret that. > > > >> I mean like [x for x in if ], only that it breaks the loop when > > > >> the expression is false. > > > > def gen(a): > > > for x in a: > > > if B: break > > > yield x > > > > a_gen = gen(A) > > > > # now iterate over a_gen > > > > -- > > > Geoff- Hide quoted text - > > > - Show quoted text - > > In your original, you have: > > > l = range(6) > > [1 if b!=4 else l.__delslice__(0,len(l)) for b in l][:-1] > > You may be hyperextending the use of '..if..else..', which is one of > my fears regarding 'with x as y'. "l.__delslice__(0,len(l))" is not > an expression. -- http://mail.python.org/mailman/listinfo/python-list
Re: usage of python
On May 14, 9:13 pm, Rajarshi <[EMAIL PROTECTED]> wrote: > Thanks to the all posters. This will be very useful! No Problem :). Let me know if you need any code snippets. P.S. I wouldn't mind working with you to prepare introductory material and examples for beginners if it becomes public property. -- http://mail.python.org/mailman/listinfo/python-list
Simple Doc Test failing without any reason [Help Needed]
The following following code fails with the failiure:- File "test.py", line 27, in __main__.sanitize_number Failed example: sanitize_number('0321-4683113') Expected: '03214683113' Got: '03214683113' Expected and Got looks the same. The value should verify. What am I doing wrong here ? Thanks in advance. I really appreciate your time. P.S. I tested this Python2.5 installed on both Ubuntu Hardy Heron and Windows XP /// Code / import os; import string; def sanitize_number(input_number): """ Sanitize Number Module Please make Test Cases in Here >>> sanitize_number('0321-4683113') '03214683113' >>> sanitize_number('03214683113') '03214683113' >>> sanitize_number('0321-468-3113') '03214683113' >>> sanitize_number('0321 4683113') '03214683113' >>> sanitize_number('0321 468 3113') '03214683113' >>> sanitize_number('3214683113') '03214683113' >>> sanitize_number('923214683113') '03214683113' >>> sanitize_number('00923214683113') '03214683113' >>> sanitize_number('+923214683113') '03214683113' >>> sanitize_number('+923214+683113') False """ countries = { '92' : 'Pakistan' } """ Reference http://en.wikipedia.org/wiki/List_of_mobile_codes_in_Pakistan Please update original wiki article if more codes are discovered """ providers = { '300' : 'Mobilink', '301' : 'Mobilink', '302' : 'Mobilink', '306' : 'Mobilink', '307' : 'Mobilink', '308' : 'Mobilink', '309' : 'Mobilink', '342' : 'Telenor', '343' : 'Telenor', '344' : 'Telenor', '345' : 'Telenor', '346' : 'Telenor', '321' : 'Warid', '322' : 'Warid', '323' : 'Warid', '331' : 'Ufone', '332' : 'Ufone', '333' : 'Ufone', '334' : 'Ufone', '313' : 'Zong', '314' : 'Zong', '320' : 'Instaphone', '335' : 'SCOM' } #Rule 1 Numbers can only contain spaces, + and input_number = string.join(input_number.split(), '') # Split Spaces and join input_number = string.join(input_number.split('-'),'') # Split Spaces and join #print "After Split Join", input_number if input_number.startswith('00'): input_number = input_number[2:] elif input_number.startswith('0'): input_number = input_number[1:] elif input_number.startswith('+'): input_number = input_number[1:] #print "Phase1", input_number #The number should now have either 10 or 12 digits depending on whether or not country code is included if len(input_number) == 10 or len(input_number) == 12: if len(input_number) == 12: for code in countries.keys(): if input_number.startswith(code): input_number = input_number[2:] break; else: return False # Country Code not Supported for code in providers.keys(): if input_number.startswith(code): #input_number = input_number[3:] input_number = '0'+input_number #print "Result", input_number return input_number return False #print sanitize_number('+923214+683113') == False def _test(): import doctest doctest.testmod() if __name__ == "__main__": _test() -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple Doc Test failing without any reason [Help Needed]
I copied the text off here into a new file and it worked!. I then took a diff between the version that didnt work and the version that worked and the only difference was a couple of spaces after this line:- >>> sanitize_number('0321-4683113')>> brackets> Apparently they caused the test case to fail on this. Weird behavior :/ Thanks for your time Gerard and thanks to everyone else too. On May 28, 5:11 pm, Gerard Flanagan <[EMAIL PROTECTED]> wrote: > On May 28, 1:48 pm, afrobeard <[EMAIL PROTECTED]> wrote: > > > > > The following following code fails with the failiure:- > > > File "test.py", line 27, in __main__.sanitize_number > > Failed example: > > sanitize_number('0321-4683113') > > Expected: > > '03214683113' > > Got: > > '03214683113' > > > Expected and Got looks the same. The value should verify. What am I > > doing wrong here ? > > > Thanks in advance. I really appreciate your time. > > > P.S. I tested this Python2.5 installed on both Ubuntu Hardy Heron and > > Windows XP > > Your code works for me (Python 2.5 WinXP). > > Are you mixing tabs and spaces? > > G. -- http://mail.python.org/mailman/listinfo/python-list
Re: Compare 2 files and discard common lines
Another way of doing this might be to use the module difflib to calculate the differences. It has a sequence matcher under it which has the function get_matching_blocks difflib is included with python. On May 29, 2:02 pm, Chris <[EMAIL PROTECTED]> wrote: > On May 29, 10:36 am, loial <[EMAIL PROTECTED]> wrote: > > > I have a requirement to compare 2 text files and write to a 3rd file > > only those lines that appear in the 2nd file but not in the 1st file. > > > Rather than re-invent the wheel I am wondering if anyone has written > > anything already? > > How large are the files ? You could load up the smallest file into > memory then while iterating over the other one just do 'if line in > other_files_lines:' and do your processing from there. By your > description it doesn't sound like you want to iterate over both files > simultaneously and do a line for line comparison because that would > mean if someone plonks an extra newline somewhere it wouldn't gel. -- http://mail.python.org/mailman/listinfo/python-list