Re: [Tutor] rstrip in list?

2010-02-09 Thread Steven D'Aprano
do it: #1: modify the list in a for-loop for i, item in enumerate(mylist): mylist[i] = item.rstrip() #2: make a new list with a list comprehension mylist = [item.rstrip() for item in mylist] Of the two, I prefer the second. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] string to list

2010-02-10 Thread Steven D'Aprano
strings "1", "2" etc. >>> li = map(int, li) # convert the strings to actual ints >>> print li [1, 2, 3, 4] Naturally the string s would come from the shell, otherwise there is no point! If you are typing the data directly into the Python interpreter, you wou

Re: [Tutor] aliasing an imported module

2010-02-13 Thread Steven D'Aprano
" as needed, each with their own state: >>> f = FuncCalculator() >>> g = FuncCalculator() >>> h = FuncCalculator() >>> f() 0.5 >>> f() 0.83326 >>> f() 1.0833 >>> g() 0.5 >>> g() 0.83326 >>> h() 0.5 >>> f() 1.2832 Hope this helps! -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

tutor@python.org

2010-02-13 Thread Steven D'Aprano
be more than 32 bits, but since I'm a fallible human, I'd rather find out about an error in my logic as soon as possible"). (2) If the caller cares enough about speed to object to the tiny little cost of the assertion, he or she can disable it by passing the -O (O for Optimise) switc

Re: [Tutor] Tutor list as pair progamming plush toy

2010-02-13 Thread Steven D'Aprano
e same? Yes! -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] asigning class variables

2010-02-13 Thread Steven D'Aprano
Python makes it really easy to accidentally hide a class attribute with an instance attribute with the same name, but fortunately having shared data like this is quite rare, so it's unusual to be a problem. Hope this helps, -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Getting caller name without the help of "sys._getframe(1).f_code.co_name" ?

2010-02-14 Thread Steven D'Aprano
>>> e.filename 'no such file.txt' > And this is not enough for developer : where that error happened ? > what class ? what method ? All these things are displayed by the default traceback mechanism. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] A Stuborn Tab Problem in IDLE

2010-02-14 Thread Steven D'Aprano
ero results. What next? I had been modifying the > program repeatedly over several hours, and executing it without any > trouble like this. Can you copy and paste the exact error message displayed? -- Steven D'Aprano ___ Tutor maillist - T

tutor@python.org

2010-02-14 Thread Steven D'Aprano
any of my Pythons (2.5,2.6 and > 3.1) It won't work in 2.5 or 2.6. You're probably trying this: 123.bit_length() and getting a syntax error. That's because the Python parser sees the . and interprets it as a float, and 123.bit_length is not a valid decimal float. You need to ei

Re: [Tutor] command error

2010-02-16 Thread Steven D'Aprano
sage if the test never becomes true: for i in range(10, 0, -1): # 10, 9, 8, ... , 1 if i <= 4: # the test print "we exit the loop early" break print "Loop number", i else: # for...else print

Re: [Tutor] pure symbol -- __subtype__

2010-02-18 Thread Steven D'Aprano
tegers. > Actually, what I need is a kind of __subtype__ magic method that acts > for subtyping the same way __init__ does for instanciation. That's what metaclasses are for. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.o

Re: [Tutor] Reading large bz2 Files

2010-02-19 Thread Steven D'Aprano
lose() >>> i 2 My guess is one of two things: (1) You are mistaken that the file is bigger than 4311 lines. (2) You are using Windows, and somehow there is a Ctrl-Z (0x26) character in the file, which Windows interprets as End Of File when reading files in text mode. Try ch

Re: [Tutor] Is it possible for a Python Program to send commands to the Python Interpreter?

2010-02-19 Thread Steven D'Aprano
raise ValueError('unsafe string') If your needs are more complicated, then sanitising the string becomes exponentially more difficult. It will probably be less work to write your own safe parser than to sanitise the input. Have I scared you about using eval? If so, good. Don&#x

Re: [Tutor] Superclass call problem

2010-02-20 Thread Steven D'Aprano
__init__(self) That has the effect of passing self *twice*, when __init__ expects to get self *once*, hence the error message you see: > When the super().__init__ line runs I get the error "__init__() takes > exactly 1 positional argum

Re: [Tutor] Replacing part of a URL

2010-02-20 Thread Steven D'Aprano
nd > then replace sometihing, and join them together again. > > But - wouldn't it make more sense to do this with re.sub? Heavens no! Why do you need a 80 pound sledgehammer to crack a peanut??? "Some people, when confronted with a pro

Re: [Tutor] os.path.basename() issue with path slashes

2010-02-20 Thread Steven D'Aprano
ame) if os.name == 'nt': fullpath.replace('\\', '/') print '' % (fullpath, fullpath) Hope that helps. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Functions returning multiple values

2010-02-21 Thread Steven D'Aprano
;>> g() 1 >>> g.func_defaults[0] 0 The answer is that ints are immutable: you can't change their value. When you do x+=1, it doesn't modify the int 0 in place, turning it into 1. It leaves 0 as zero, and gives you a new int equal to one. So the

Re: [Tutor] fast sampling with replacement

2010-02-21 Thread Steven D'Aprano
e in sample_with_replacement() much more than any other function > or method. Thanks in advance for any advice you can give me. You don't tell us whether that actually matters. Is it taking 3 hours in a 6 hour run time? If so, then it is worth spendi

Re: [Tutor] Regex to find files ending with one of a given set of extensions

2010-02-21 Thread Steven D'Aprano
r: '_sre.SRE_Pattern' object is not callable The error is the line findImages = imageRx(someFiles) You don't call regexes, you have to use the match or search methods. And you can't call it on a list of file names, you have to call it on each fi

Re: [Tutor] Python file escaping issue?

2010-02-21 Thread Steven D'Aprano
end a raw string with a backslash. >>> path = r'C:\directory\' File "", line 1 path = r'C:\directory\' ^ SyntaxError: EOL while scanning single-quoted string The best advice is to remember that Windows allows both forward and backwards slashes as the path separator, and just write all your paths using the forward slash: 'C:/directory/' 'C:textfile.txt' -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Drawing faces

2010-02-22 Thread Steven D'Aprano
. > I want the function to be able to draw three differnet faces in a > single window,when i invoke drawFace(center,size,win) into def > main(). > thanks > kola Do you actually have a question? Please show the code you have already written, and tell us what doesn&

Re: [Tutor] Python file escaping issue?

2010-02-22 Thread Steven D'Aprano
ages like bash and (I think) C++. There are three main ways to deal with an unrecognised escape sequence: * raise an error * ignore the backslash, e.g. \d -> d * keep the backslash, e.g. \d -> \d There are good arguments for all three, so I don't t

Re: [Tutor] Encryption

2010-02-22 Thread Steven D'Aprano
te are not up to modern standards and are NOT cryptographically secure. Do not use this where serious security is required. Otherwise, google for "python encryption". You might also like to ask on the Python newsgroup comp.lang.python for advice. -- Steven D'Aprano _

Re: [Tutor] Python 3 Statistics?

2010-02-23 Thread Steven D'Aprano
to get numpy and scipy? If you need to go back to the 2.x series, you should use 2.6 not 2.5. The Windows installer for numpy only supports 2.5, but the source install should work with any recent Python 2.x version. -- Steven D'Aprano ___ Tutor m

Re: [Tutor] ask

2010-02-24 Thread Steven D'Aprano
ist integer from 10 to 29, we can write: > range(10,30). I was going to show a list of number from 1.0 to 1.9, > and I did this in the same way as integer: range(1.0,2.0,0.1), but it > doesn't work. Can you help me? Thank you! Hope this helps: http://code.activestate.com/recipes/57

Re: [Tutor] strange bidi requirement

2010-02-24 Thread Steven D'Aprano
r at the console, and then process it in reverse: >>> s = raw_input("Please enter a decimal number: ") Please enter a decimal number: 123.456 >>> >>> print s 123.456 >>> >>> for c in reversed(s): ... print c ... 6 5 4 . 3 2 1 Hope this helps

Re: [Tutor] raising number to a power

2010-02-25 Thread Steven D'Aprano
rapper around the native C maths library. The builtin pow function has more capabilities, and came before the ** operator. > Did they create a sum function As a matter of fact they did: http://docs.python.org/library/math.html#math.fsum -- Steven

Re: [Tutor] raising number to a power

2010-02-25 Thread Steven D'Aprano
6.0. Is that about right? Pretty much, but the builtin pow also takes an optional third argument: >>> pow(4, 4, 65) # same as 4**4 % 65 only more efficient 61 By the way, are you aware that you can get help in the interactive interpreter? help(pow)

Re: [Tutor] Verifying My Troublesome Linkage Claim between Python and Win7

2010-02-27 Thread Steven D'Aprano
gram in folder1 (one). What? "Pointing back", as in a Shortcut? Or a symlink? If you've created a shortcut instead of a copy, I'm not surprised you are executing it in the "wrong" folder. That's what shortcuts do. -- Steven D'Aprano __

Re: [Tutor] Verifying My Troublesome Linkage Claim between Python and Win7

2010-02-27 Thread Steven D'Aprano
t even know they exist, even though some of them go back all the way to Windows NT. But as far as I can tell, there is no way for you to have created a symlink from Explorer.) -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Over-riding radians as default for trig calculations

2010-02-28 Thread Steven D'Aprano
in radians. No, you can't change that anywhere, but you can do this: >>> math.cos(math.radians(45)) 0.70710678118654757 So of course you can write your own function: def cos(x): return math.cos(math.radians(x)) and call that. -- Steven D'Aprano ___

Re: [Tutor] Any Tutor there ? Removing redundant parameters in a models file having include files.

2010-03-01 Thread Steven D'Aprano
on, used by people wanting to experiment with Python compilers. "Python for S60" is a version of Python written for Nokia's S60 devices. CapPython is an experimental version of Python designed for security. There are many others, they are all Python, but they

Re: [Tutor] Any Tutor there ? Removing redundant par ameters in a models file having include files.

2010-03-02 Thread Steven D'Aprano
g merged with CPython too, which will be a great benefit. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] parsing a "chunked" text file

2010-03-02 Thread Steven D'Aprano
en("my_file.dat", "r") data = {} # don't shadow the built-in dict non_blank_lines = skip_blanks(fp) sections = collate_sections(non_blank_lines) for (header, lines) in sections: data[header] = lines Of course you can add your own error checking. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] List comprehension possible with condition statements?

2010-03-03 Thread Steven D'Aprano
't a mistake. Likewise for "item != None" versus "item is not None". -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] unittest

2010-03-03 Thread Steven D'Aprano
On Thu, 4 Mar 2010 04:27:23 am C.T. Matsumoto wrote: > Hello, > > Can someone tell me the difference between unittests assertEqual and > assertEquals? assertEqual, assertEquals and failUnless are three spellings for the same thing. There is no difference. -- Ste

Re: [Tutor] sorting algorithm

2010-03-03 Thread Steven D'Aprano
sort is that the algorithm is easy to code. Otherwise it is horrible in just about every imaginable way. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] How to wrap ctype functions

2010-03-03 Thread Steven D'Aprano
ely above a function definition: @error_handling def some_function(): ... Otherwise, you use the decorator like a function, you give the name of another function as argument, and it returns a new function: new_function = error_handling(some_function) -- Steven D'Aprano __

Re: [Tutor] List comprehension possible with condition statements?

2010-03-03 Thread Steven D'Aprano
On Thu, 4 Mar 2010 05:18:40 am Alan Gauld wrote: > "Steven D'Aprano" wrote > > > Comparisons with None almost always should be one of: > > > > item is None > > item is not None > > Yes, but the reason I changed it (I originally had "is no

Re: [Tutor] unittest

2010-03-04 Thread Steven D'Aprano
On Thu, 4 Mar 2010 05:32:22 pm you wrote: > Steven D'Aprano wrote: > > On Thu, 4 Mar 2010 04:27:23 am C.T. Matsumoto wrote: > >> Hello, > >> > >> Can someone tell me the difference between unittests assertEqual > >> and assertEquals? > >

Re: [Tutor] lazy? vs not lazy? and yielding

2010-03-04 Thread Steven D'Aprano
.__getitem__(0), __getitem__(1), __getitem__(2), and so on, until it raises IndexError. If thing doesn't have a __getitem__ method either, then the loop fails with TypeError. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] object representation

2010-03-04 Thread Steven D'Aprano
to this as "the type", and ignore the fact that it's actually a pointer. The type data structure (which itself will be an object) itself is not embedded in every object! And of course, other Pythons may use some other mechanism for linking objects back to their type. --

Re: [Tutor] object representation

2010-03-04 Thread Steven D'Aprano
re, which means bigger code and more bugs. They don't fit in very well with CPython's memory management system. And they use a large number of pointers, which can be wasteful. E.g. a trie needs six pointers just to represent the single key "python": '' ->

Re: [Tutor] Understanding (Complex) Modules

2010-03-04 Thread Steven D'Aprano
hanic. > Is there some relationship between modules and objects that I'm not > seeing that could be of value? Modules are themselves objects. Everything in Python is an object: strings, ints, floats, lists, tuples, everything. Modules are compound objects, in that th

Re: [Tutor] Really miss the C preprocessor!!

2010-03-05 Thread Steven D'Aprano
t exception: time.sleep(t) t *= 2 raise exception("no connection after %d attempts" % numretries) result = exponential_backoff(check_some_website, ("http://example.com";, 42), HTTPError, 8) Any time you think you need e

Re: [Tutor] Really miss the C preprocessor!!

2010-03-06 Thread Steven D'Aprano
url, x): ... I haven't tested the above code, but it should do the trick. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] recursive generator

2010-03-07 Thread Steven D'Aprano
ch a yield, execution is halted, but the state of the generator is remembered. Then when you call the next() method, execution resumes. > (But then, is it at all possible to have a call in a > generator? Or does the issue only appear whan the callee is a > generator itself?) Else, the must be an obvious error in my code. I don't understand what you mean. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] recursive generator

2010-03-07 Thread Steven D'Aprano
On Mon, 8 Mar 2010 01:49:21 am Stefan Behnel wrote: > Steven D'Aprano, 07.03.2010 14:27: > > __iter__ should be an ordinary function, not a generator. Something > > like this should work: [...] > That's just an unnecessarily redundant variation on the above. It's

Re: [Tutor] __iter__: one obvious way to do it

2010-03-07 Thread Steven D'Aprano
and preferably only one --obvious way to do > it" http://www.python.org/dev/peps/pep-0020/ This doesn't mean that there should be *only* one way to do something. It means that the should be one OBVIOUS way to do it. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Printing time without "if" statement

2010-03-07 Thread Steven D'Aprano
active interpreter, when the final quit message is printed: the interpreter doesn't notice it needs to redraw the prompt. But other than that, it should be fine. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] use of __new__

2010-03-11 Thread Steven D'Aprano
if errors is None: errors = cls._ERRORS obj = super(Unicode, cls).__new__( Unicode, string, encoding, errors) else: # Never attempt decoding. obj = super(Unicode, cls).__new__(Unicode, string) assert isinstance(obj, Unicode) return obj &g

Re: [Tutor] use of __new__

2010-03-11 Thread Steven D'Aprano
where __new__ returns something else, __init__ is never called: >>> class AnotherClass(MyClass): ... def __new__(cls): ... ignore = super(AnotherClass, cls).__new__(cls) ... return 42 ... >>> o = AnotherClass() Calling __new__ on object >

Re: [Tutor] use of __new__

2010-03-11 Thread Steven D'Aprano
On Fri, 12 Mar 2010 11:53:16 am Steven D'Aprano wrote: > I have tried to match the behaviour of the built-in unicode as close > as I am able. See here: > http://docs.python.org/library/functions.html#unicode And by doing so, I entirely forgot that you want to change the default

Re: [Tutor] use of __new__

2010-03-11 Thread Steven D'Aprano
st(): assert Unicode() == u'' assert Unicode('abcd') == u'abcd' u = 'cdef'.decode('utf-16') assert u == u'\u6463\u6665' s = u.encode('utf-8') assert Unicode(s) == u try:

Re: [Tutor] use of __new__

2010-03-12 Thread Steven D'Aprano
hen it will work correctly: >>> class K(Unicode): ... pass ... >>> type(K()) You might be tempted to change the first reference to Unicode to cls as well, but sadly that does not work. The reason is complicated, and to be honest I don't remember it, but you will probably

Re: [Tutor] sorting algorithm

2010-03-12 Thread Steven D'Aprano
rt sentences! Thank you. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] %s %r with cutom type

2010-03-12 Thread Steven D'Aprano
set it to ISO 8859-1, I get this: >>> list("éâÄ") ['\xe9', '\xe2', '\xc4'] As far as I know, the behaviour of stuffing unicode characters into byte-strings is not well-defined in Python, and will depend on external factors like the terminal you are running in, if any. It may or may not work as you expect. It is better to do this: u = u"éâÄ" s = u.encode('uft-8') which will always work consistently so long as you declare a source encoding at the top of your module: # -*- coding: UTF-8 -*- -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] First program

2010-03-12 Thread Steven D'Aprano
teError: 'str' object has no attribute 'format' The original poster is using Python 3.0 or 3.1, you are using an earlier version. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] First program

2010-03-12 Thread Steven D'Aprano
s to str(), it isn't clear what is the template and what is being inserted into the template. It is too easy to miss a quote and get a SyntaxError, or to forget to add spaces where needed. To me, this is MUCH easier: template = "The area of a %s is %s x %s equals %s sq

Re: [Tutor] %s %r with cutom type

2010-03-13 Thread Steven D'Aprano
functionality to do everything for the user. That is a bad idea. > Anyway, all that source of troubles > disappears with py3 :-) > Then, I only need __str__ to produce nice, clear, unpolluted output. > > > which will always work consistently so long as you declare a source > > encoding at the top of your module: > > > > # -*- coding: UTF-8 -*- > > Yes, this applies to my own code. But what about user code calling my > lib? (This is the reason for Unicode.ENCODING config param). That is their responsibility, not yours. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Escaping a single quote mark in a triple quoted string.

2010-03-13 Thread Steven D'Aprano
he string includes: * wrap the string contents in single quotes ' * unless the string contains single quotes, in which case wrap it in double quotes " and display the single quotes unescaped * unless the string contains double quotes as well, in which case wrap it in single quotes and escape the inner single quotes. But remember: this is only the display of the string, not the contents. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Declaring a compound dictionary structure.

2010-03-13 Thread Steven D'Aprano
entry = entries.get(date, {}) x = entry.get(address, {}) x.append(visitor) entry[address] = x entries[date] = entry The trick is to use get to look up the dictionary: entries.get(date, {}) looks up date in entries, and if it isn't found, it returns

Re: [Tutor] raw_input()

2010-03-15 Thread Steven D'Aprano
s "A": def condition(line): line = line.strip() fields = line.split('\t') return fields[7].strip().upper() == "A" datafile = open("somefile.data", "r") for line in datafile: if condition(line): print

Re: [Tutor] raw_input()

2010-03-15 Thread Steven D'Aprano
On Tue, 16 Mar 2010 10:22:55 am kumar s wrote: > thanks Benno. > > supplying 3.6 GB file is over-kill for the script. Then supply a smaller file. > This is the reason I chose to input lines on fly. I don't understand what you are trying to say. --

Re: [Tutor] raw_input()

2010-03-16 Thread Steven D'Aprano
generator, right? So I don't think it's a problem - > correct me if I'm wrong. No, you are correct -- "for line in file" reads one line at a time. Beware, though, if the file isn't line-oriented, then each "line" (separate

Re: [Tutor] os.popen4 help!

2010-03-16 Thread Steven D'Aprano
command.com or cmd.exe or whatever it is called) and run: opcenum.exe/IOPCServerList/EnumClassesofCategory 63D5F432-CFE4-11d1-B2C8-0060083BA1FB and see what it does. Once you get the syntax right in the shell, then use the exact same syntax in popen4. -- Steven D'Aprano __

Re: [Tutor] difflib context to string-object?

2010-03-19 Thread Steven D'Aprano
perate string-objects instead of > writing them to files? Use StringIO objects, which are fake files that can be read as strings. Untested and entirely from memory, use: from StringIO import StringIO fromfile = StringIO("some text goes here") tofile = StringIO("some different t

Re: [Tutor] using pythnn to open a password protected website

2010-03-19 Thread Steven D'Aprano
owser via my code, and preferably > auto-submit the form. You might like to look at Mechanize, which is a third-party Python project for dealing with just that sort of problem. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To u

Re: [Tutor] Finding duplicates entry in file

2010-03-20 Thread Steven D'Aprano
1 > 2 > 2 > 3 > 4 > 4 > 5 > 6 > 6 > > The newly revised file should be: > > 1 > 2 > 3 > 4 > 5 > 6 Unless the file is huge, something like this should do: # Untested lines = open("myfile").readlines() f = open("myfi

Re: [Tutor] Efficiency and speed

2010-03-20 Thread Steven D'Aprano
ng that *maybe* you want to get rid of. index() is an O(N) operation -- slow but not painfully slow. Unless your list is short, this is another "maybe" to remove. remove() is another expensive operation. Unless your list is very short, this is something else you want to avoid when possible. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Efficiency and speed

2010-03-20 Thread Steven D'Aprano
2) 2 If z is not in the bins, returns -1: >>> search_bins(bins, 5.1) -1 """ for i, value in enumerate(nx): if z > value: return i-1 return -1 -- Steven D'Aprano ___

Re: [Tutor] Finding duplicates entry in file

2010-03-20 Thread Steven D'Aprano
in text part and a HTML part. Any decent mail client I know of gives you the choice of which to view. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] parsing pyc files

2010-03-23 Thread Steven D'Aprano
de which is different from the version they actually ran, then run it on the same data. If it doesn't give the exact same results, send it back with this link: http://en.wikipedia.org/wiki/Scientific_misconduct -- Steven D'Aprano ___ Tutor

Re: [Tutor] parsing pyc files

2010-03-23 Thread Steven D'Aprano
38 STORE_FAST 1 (y) 41 JUMP_ABSOLUTE 19 >> 44 POP_BLOCK 5 >> 45 LOAD_FAST 0 (x) 48 LOAD_FAST1 (y) 51 BINARY_ADD 52 RETURN_VALUE -- Steven D'Aprano _

Re: [Tutor] Press Enter to quit. Silently maybe.

2010-03-23 Thread Steven D'Aprano
to quit') > sys.exit() What yes/no prompt? How do you select No? -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Interpolation function

2010-03-25 Thread Steven D'Aprano
sophisticated. Unless there happens to be an experienced numpy/scipy user hanging around here, any answer we give would be just guessing. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription option

Re: [Tutor] Odds and even exercise

2010-03-27 Thread Steven D'Aprano
hings. See what they do. See if you can predict what they will do before you do them. For example, what would these give? range(1, 101, 3) range(2, 101, 4) Try it yourself and see if you predicted correctly. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Odds and even exercise

2010-03-27 Thread Steven D'Aprano
t; > #A way to display all odd numbers > > odd = numbers[::2] > > instead i do this: > odd=[] > for y in range(1,101,2): > odd.append(y) This does just as much unnecessary work as above. -- Steven D'Aprano __

Re: [Tutor] inter-module global variable

2010-03-28 Thread Steven D'Aprano
this instead: class Code(object): w = None # Better to define default settings here. def __init__(self, w=None): if w is not None: self.w = w If no w is provided, then lookups for instance.w will find the shared class attribute w. [...] > But the '###' line looks like an ugly trick to me. (Not the fact > that it's a class attribute; as a contrary, I often use them eg for > config, and find them a nice tool for clarity.) The issue is that > Code.w has to be exported. It is ugly, and fragile. It means any caller is *expected* to modify the w used everywhere else, in strange and hard-to-predict ways. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] help

2010-03-30 Thread Steven D'Aprano
eport any errors. You can also run: which python and see what it says. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Script Feedback

2010-03-30 Thread Steven D'Aprano
, filename, tarname): print "Processing ..." tar_bzip2_directories(...) if __name__ == '__main__': try: main() except ... # as above Now anyone can import the module and call individual functions, or even call main, or they can run

Re: [Tutor] Script Feedback

2010-03-31 Thread Steven D'Aprano
s to call that function with every item in the sequence. There's nothing special about callbacks, except that they have to take the arguments which the library function promises to supply. -- Steven D'Aprano ___ Tutor maillist - Tu

Re: [Tutor] Help with simple text book example that doesn't work!!!

2010-04-04 Thread Steven D'Aprano
^ SyntaxError: invalid syntax >>> print("Hello world") Hello world Alternatively, sometimes if you have an error on one line, the interpreter doesn't see it until you get to the next, and then you get a SyntaxError on one line pas

Re: [Tutor] Simple bank account oriented object

2010-04-05 Thread Steven D'Aprano
extra. Again, inheritance makes it easy: def affiche_solde(self): # Display everything the super class understands. CompteBancaire.affiche_soldeafficheself) # Display the parts that only the subclass understands. print "Le solde de votre marge de crédit est de %d $CAD" % (self.mar

Re: [Tutor] ask-why I cannot run it, and I am so confused about the traceback

2010-04-07 Thread Steven D'Aprano
re the "rich text" (HTML) attachment and just display the plain text? -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Declaring methods in modules.

2010-04-11 Thread Steven D'Aprano
the fine existing IP address modules already written), or write functions in the module and just call them: import ISPdetector ipAddress = "123.123.123.123" emails = find_email_from(ipAddress) Remember, in Python methods are just syntactic sugar for function calls. obj.method(arg) is just

Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Steven D'Aprano
"??? What on earth does it do? It's worse than Perl code!!! *half a wink* > What fine manual should I be reading? I am not > asking for code, rather just a link to the right documentation. See the shell utilities module: import shutil -- Steven D'Aprano ___

Re: [Tutor] Sequences of letter

2010-04-12 Thread Steven D'Aprano
using Python 2.6 or better: >>> import itertools >>> for x in itertools.product('abc', 'abc', 'abc'): ... print ''.join(x) ... aaa aab aac aba abb abc [many more lines...] ccc If you don't like the repeated 'abc' in the call

Re: [Tutor] "value" ~ "data" ~ "object"

2010-04-15 Thread Steven D'Aprano
oks", "a SET of spanners", or "a GROUP of people". The term you are thinking of is mass noun. See: http://en.wikipedia.org/wiki/Collective_noun http://en.wikipedia.org/wiki/Mass_noun http://en.wikipedia.org/wiki/Data > How to denote a single > unit of data

Re: [Tutor] Creating class instances through iteration

2010-04-15 Thread Steven D'Aprano
, you need to operate on all of them. So what is the purpose of the names? Just drop the names, and work on a collection of objects: objects = [] for row in csvimport: tuple = (row[0],row[1],row[2]) objects.append(Classname(tuple)) # more code goes here # ... # much later for obj in objects:

Re: [Tutor] Loop comparison

2010-04-16 Thread Steven D'Aprano
+ 101 + 101 + ... 101 = 100*101 so sum = 1/2 * 100 * 101 = 5050 While it is uncertain whether this specific story is actually true, it is certain that Gauss was a child prodigy and a genius of the first degree. -- Steven D'Aprano ___ T

Re: [Tutor] Loop comparison

2010-04-16 Thread Steven D'Aprano
2.5. The joys of low end hardware! Are you sure you got the right number of zeroes? > whereas the same with > "range" gives an error due to insufficient memory. I'm not even going to try... -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Raw string

2010-04-18 Thread Steven D'Aprano
because the file doesn't exist. If you actually have a file called Programs\rating in the C:/Python25/ directory, you will open it. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] the binary math "wall"

2010-04-20 Thread Steven D'Aprano
18.2541666 which compares well to my HP-48GX: 18.15 HMS-> gives 18.25, and: 18.1515 HMS-> gives 18.254167. Note though that this still fails with some valid input. I will leave fixing it as an exercise (or I might work on it later, time permitting). -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] set and sets.Set

2010-04-21 Thread Steven D'Aprano
bject was introduced in Python 2.4 and is re-written in C for speed. Functionally, they are identical. Speed-wise, the built-in version is much faster, so unless you need to support Python 2.3, always use the built-in set type. -- Steven D'Aprano ___

Re: [Tutor] the binary math "wall"

2010-04-21 Thread Steven D'Aprano
> traverse-accumulating little computer errors along the way-the end > result could be catastrophically wrong. YES!!! And just by being aware of this potential problem, you are better off than 90% of programmers who are blithely unaware that floats are not real numbers. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] sys.path and the path order

2010-04-22 Thread Steven D'Aprano
sys.html#sys.path So the answer to your question is, Yes. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Class Inheritance

2010-04-22 Thread Steven D'Aprano
e >>> turtle.ScrolledCanvas > As a sidenote, I ended up removing the from turtle import * > line from Tkinter which resolved the problem(the example I was using > didn't have a canvas, and I'm pretty sure Tkinter was defaulting > to the ScrolledCanvas). I should say so! Tkinter doesn't have a "from turtle import *" line, as that would set up a circular import dependency. No wonder you have had problems, making random changes to libraries without understanding them. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Class Inheritance

2010-04-23 Thread Steven D'Aprano
t mytkinter as TK class ScrolledCanvas(TK.Frame): pass > P.S. I bet you've been waiting since you got your first condescending > response to a similar question, to lay it on someone about touching > the Almighty Library. > > Way to keep up the cycle. Don't try to psychoanalyse people you've never met, you aren't any good at it. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Binary search question

2010-04-23 Thread Steven D'Aprano
too large, you hunt in the opposite direction, with i-1 to i, i-2 to i, etc.) Once you have bracketed the item you are searching for, you *search* for it within those limits, using binary search or even a linear search. If your searches are clustered, most of the hunt phases will be short an

  1   2   3   4   5   6   7   8   9   10   >