Re: Determine attributes of calling method

2011-06-04 Thread Ian Kelly
On Fri, Jun 3, 2011 at 2:35 PM, Joe wrote: >    foo.__dict__['color']='blue' >    fu.__dict__['color']='red' You don't need to use __dict__ to set function attributes. Just do: foo.color = 'blue' -- http://mail.python.org/mailman/listinfo/python-list

Re: how to avoid leading white spaces

2011-06-06 Thread Ian Kelly
On Mon, Jun 6, 2011 at 9:29 AM, Steven D'Aprano wrote: > [...] >> I would expect >> any regex processor to compile the regex into an FSM. > > Flying Spaghetti Monster? > > I have been Touched by His Noodly Appendage!!! Finite State Machine. -- http://mail.python.org/mailman/listinfo/python-list

Re: how to avoid leading white spaces

2011-06-06 Thread Ian Kelly
On Mon, Jun 6, 2011 at 10:08 AM, Neil Cerutti wrote: > import re > > print("re solution") > with open("data.txt") as f: >    for line in f: >        fixed = re.sub(r"(TABLE='\S+)\s+'", r"\1'", line) >        print(fixed, end='') > > print("non-re solution") > with open("data.txt") as f: >    for l

Re: how to avoid leading white spaces

2011-06-06 Thread Ian Kelly
The main thing I wanted to fix was that the second .index() call had the possibility of raising an unhandled ValueError. There are really two things we have to search for in the line, either of which could be missing, and catching them both with the same except: clause feels better to

Re: how to avoid leading white spaces

2011-06-06 Thread Ian Kelly
On Mon, Jun 6, 2011 at 11:48 AM, Ethan Furman wrote: > I like the readability of this version, but isn't generating an exception on > every other line going to kill performance? I timed it on the example data before I posted and found that it was still 10 times as fast as the regex version. I di

Re: python + php encrypt/decrypt

2011-06-06 Thread Ian Kelly
DDING > EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))) > DecodeAES = lambda c, e: > c.decrypt(base64.b64decode(e)).rstrip(PADDING) Stylistic note: is it really necessary to use lambda here? For readability, just use def. It's worth having to hit Enter a couple ext

Re: new string formatting with local variables

2011-06-06 Thread Ian Kelly
On Mon, Jun 6, 2011 at 6:11 PM, Ben Finney wrote: >> You must use prefix-** in the call to unpack the mapping as keyword >> arguments. Note that using locals() like this isn't best-practice. > > Who says so, and do you find their argument convincing? Do you have a > reference for that so we can se

Re: new string formatting with local variables

2011-06-06 Thread Ian Kelly
I can see the appeal, but I tend to avoid it because it has the same icky feeling as doing an import *. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: Function call arguments in stack trace?

2011-06-07 Thread Ian Kelly
On Tue, Jun 7, 2011 at 1:31 PM, Dun Peal wrote: > On Jun 7, 1:23 pm, Neil Cerutti wrote: >> Use pdb. > > Neil, thanks for the tip; `pdb` is indeed a great debugging tool. > > Still, it doesn't obviate the need for arguments in the stack trace. Your program could use sys.excepthook to generate a

Re: GIL in alternative implementations

2011-06-07 Thread Ian Kelly
assignment) atomic to make this concurrent. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: GIL in alternative implementations

2011-06-07 Thread Ian Kelly
On Tue, Jun 7, 2011 at 2:22 PM, Ian Kelly wrote: > from functools import partial > > def g(value): >    print(value) >    return partial(g, value+1) > > f = partial(0) > for i in range(1): >    f = f() The "partial(0)" should read "partial(g,

Re: How good is security via hashing

2011-06-07 Thread Ian Kelly
On Tue, Jun 7, 2011 at 2:42 PM, Paul Rubin wrote: > geremy condra writes: >> # adds random junk to the filename- should make it hard to guess >> rrr = os.urandom(16) >> fname += base64.b64encode(rrr) > > Don't use b64 output in a filename -- it can have slashes in it!  :-( > > Simplest is to use

Re: the stupid encoding problem to stdout

2011-06-10 Thread Ian Kelly
2011/6/10 Sérgio Monteiro Basto : > ok after thinking about this, this problem exist because Python want be > smart with ttys, which is in my point of view is wrong, should not encode to > utf-8, because tty is in utf-8. Python should always encode to the same > thing. If the default is ascii, shou

Re: how to inherit docstrings?

2011-06-10 Thread Ian Kelly
descriptor that can be inherited (thanks to the metaclass), can be accessed from either the class or the instance (thanks to the descriptor), and can easily be modified to generate the doc string dynamically at call-time if desired. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

ContextDecorator via contextmanager: broken?

2011-06-10 Thread Ian Kelly
t;) RuntimeError: generator didn't stop So as far as I can tell, generator-based context managers simply can't be used as ContextDecorators. Furthermore, the documentation's claim that they can is actually harmful, since they *appear* to work at first. Or am I simply missing something here? Cheers, Ian [1] http://docs.python.org/py3k/library/contextlib.html#contextlib.ContextDecorator -- http://mail.python.org/mailman/listinfo/python-list

Re: ContextDecorator via contextmanager: broken?

2011-06-10 Thread Ian Kelly
On Fri, Jun 10, 2011 at 4:57 PM, Ian Kelly wrote: > So as far as I can tell, generator-based context managers simply can't > be used as ContextDecorators.  Furthermore, the documentation's claim > that they can is actually harmful, since they *appear* to work at > first.  

Re: How to avoid "()" when writing a decorator accepting optional arguments?

2011-06-11 Thread Ian Kelly
ing like this: deprecated_default = deprecated() @deprecated_default def foo(): return 0 But this hardly seems worthwhile to me just to avoid typing an extra couple of parentheses. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: __dict__ is neato torpedo!

2011-06-11 Thread Ian Kelly
such as ints, this doesn't matter. For mutable objects such as lists, it can: >>> class X(object): ... pass ... >>> a = X() >>> b = X() >>> a.foo = ['apples'] >>> b.__dict__.update(a.__dict__) >>> a.foo ['apples']

Re: __dict__ is neato torpedo!

2011-06-11 Thread Ian Kelly
= actor ... >>> a = Action(Actor(World())) >>> b = deepcopy(a) >>> a.actor is b.actor False >>> a.actor.world is b.actor.world False The intention here is probably that a and b should both be part of the same World, but as you can see that is not the case; the World got copied along with everything else. Python provides machinery to let you avoid deep copying absolutely everything, but it's important to be aware of cases like this. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improve the Dvorak Layout?

2011-06-15 Thread Ian Kelly
On Tue, Jun 14, 2011 at 12:11 AM, Xah Lee wrote: > numerical keypad is useful to many. Most people can't touch type. Even > for touch typist, many doesn't do the number keys. So, when they need > to type credit, phone number, etc, they go for the number pad. It's not about being *able* to touch t

Re: HTTPConncetion - HEAD request

2011-06-16 Thread Ian Kelly
est("HEAD", "/", "HTTP 1.0") >>>> r = h.getresponse() >>>> r.read() > b'' You mean why does it return an empty byte sequence? Because the HEAD method only requests the response headers, not the body, so the body is empty. If you want to see the response body, use GET. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: break in a module

2011-06-16 Thread Ian Kelly
On Tue, Jun 14, 2011 at 4:57 PM, MRAB wrote: > To me, the obvious choice would be "return", not "break". No, "return" returns a value. Modules do not return values. Therefore "return" would be inappropriate. If this feature were deemed desirable, "break" would make more sense to me. -- http://

Re: break in a module

2011-06-16 Thread Ian Kelly
On Thu, Jun 16, 2011 at 7:21 PM, Erik Max Francis wrote: >> This would, if I understand imports correctly, have ham() operate in >> one namespace and spam() in another. Depending on what's being done, >> that could be quite harmless, or it could be annoying (no sharing >> module-level constants, e

Re: break in a module

2011-06-16 Thread Ian Kelly
On Thu, Jun 16, 2011 at 10:24 PM, Erik Max Francis wrote: > True.  So let's use `in` to represent breaking out of the top-level code of > a module.  Why not, it's not the first time a keyword has been reused, > right? > > The point is, if it's not obvious already from that facetious proposal, it's

Re: break in a module

2011-06-16 Thread Ian Kelly
On Thu, Jun 16, 2011 at 10:21 PM, Erik Max Francis wrote: > Ethan Furman wrote: >> >> The Context: >> >> "It's quite consistent on which control structures you can break out of" >> >> Hmmm Nope, nothing there to suggest you were talking about the 'break' >> keyword. > > That's what I wrote, al

Re: How to avoid "()" when writing a decorator accepting optional arguments?

2011-06-17 Thread Ian Kelly
On Fri, Jun 17, 2011 at 4:27 AM, bruno.desthuilli...@gmail.com wrote: > On Jun 11, 10:28 pm, Ian Kelly wrote: >> >> Since there is no way to distinguish the two cases by the arguments, > > def deprecated(func=None, replacement=None): >    if replacement: >    

Re: Python and Lisp : car and cdr

2011-06-17 Thread Ian Kelly
On Fri, Jun 17, 2011 at 8:45 AM, Franck Ditter wrote: > Hi, I'm just wondering about the complexity of some Python operations > to mimic Lisp car and cdr in Python... > > def length(L) : >  if not L : return 0 >  return 1 + length(L[1:]) > > Should I think of the slice L[1:] as (cdr L) ? I mean, i

Re: Fun and games with lambda

2011-06-17 Thread Ian Kelly
On Fri, Jun 17, 2011 at 10:56 AM, Wolfgang Rohdewald wrote: > On Freitag 17 Juni 2011, Steven D'Aprano wrote: >> run this one- >> liner and wonder no more... > > looks like something dangerous to me. What does > it do? rm -rf ? The thread at the link discusses what it does in great detail. -- ht

Re: Best way to insert sorted in a list

2011-06-17 Thread Ian Kelly
On Fri, Jun 17, 2011 at 3:02 PM, Shashank Singh wrote: > Correct me if I am wrong here but isn't the second one is O(log N)? > Binary search? > That is when you have an already sorted list from somewhere and you > are inserting just one new value. Finding the position to insert is O(log n), but t

Re: Best way to insert sorted in a list

2011-06-17 Thread Ian Kelly
On Fri, Jun 17, 2011 at 3:48 PM, Chris Torek wrote: > If len(large_list) is m, this is O(m).  Inserting each item in > the "right place" would be O(m log (n + m)).  But we still > have to sort: > >    a.sort() > > This is O(log (n + m)), hence likely better than repeatedly inserting > in the corre

Re: What's the best way to write this base class?

2011-06-18 Thread Ian Kelly
='high') -- or maybe just HighElf(), which inherits from Elf). Save inheritance for broad categories of what it means to be a character (e.g. PlayerCharacter vs. NonPlayerCharacter or MobileCharacter vs. MagicMirror, etc., any of which might have the Wizard character class). Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: What's the best way to write this base class?

2011-06-20 Thread Ian Kelly
On Mon, Jun 20, 2011 at 5:57 AM, Mel wrote: > Battle for Wesnoth is set up this way.  I don't know what the code does, but > you can go wild creating new classes of character by mixing up new > combinations of attribute settings in new configuration files, and injecting > them into the standard ga

Re: Parsing a dictionary from a format string

2011-06-20 Thread Ian Kelly
On Mon, Jun 20, 2011 at 12:14 PM, Tim Johnson wrote: > Currently using python 2.6, but am serving some systems that have > older versions of python (no earlier than. > Question 1: >  With what version of python was str.format() first implemented? 2.6 > Question 2: >  Given the following string:

Re: Is there any advantage or disadvantage to using sets over list comps to ensure a list of unique entries?

2011-06-20 Thread Ian Kelly
On Mon, Jun 20, 2011 at 1:43 PM, deathweaselx86 wrote: > Howdy guys, I am new. > > I've been converting lists to sets, then back to lists again to get > unique lists. > e.g > > Python 2.5.2 (r252:60911, Jan 20 2010, 21:48:48) > [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2 > Type "help", "copyrigh

Re: Better way to iterate over indices?

2011-06-21 Thread Ian Kelly
On Tue, Jun 21, 2011 at 12:05 PM, Billy Mays wrote: > I know I could use enumerate: > > for i, v in enumerate(myList): >    doStuff(i, myList[i]) > > ...but that stiff seems clunky. Why not: for i, v in enumerate(myList): doStuff(i, v) -- http://mail.python.org/mailman/listinfo/python-list

Re: How to get return values of a forked process

2011-06-21 Thread Ian Kelly
On Tue, Jun 21, 2011 at 12:26 PM, Ian wrote: > myForkedScript has code like this: > if fail: >os._exit(1) > else: >os._exit(os.EX_OK) > > Is using os._exit() the correct way to get a return value back to the > main process? sys.exit() is the preferred way.

Re: How to get return values of a forked process

2011-06-21 Thread Ian Kelly
> Where did you find the Unix docs you pasted in?  I didn't find it in > the man pages.  Thank you.  Based on what you say, I will change my > os._exit() to sys.exit(). http://docs.python.org/library/os.html#os.wait http://docs.python.org/library/os.html#os.waitpid I don't know what man pages you

Re: How can I speed up a script that iterates over a large range (600 billion)?

2011-06-21 Thread Ian Kelly
[:] > >    for n in number_list: >        for x in range(2, n): >            if n % x == 0: >                primes.remove(n) >                break > >    return primes Also, primality testing and factorization are very similar problems, and the same range optimizati

Re: sorry, possibly too much info. was: Re: How can I speed up a script that iterates over a large range (600 billion)?

2011-06-21 Thread Ian Kelly
On Tue, Jun 21, 2011 at 3:09 PM, John Salerno wrote: > Don't worry, I was still unclear about what to do after reading all > the responses, even yours! But one thing that made me feel better was > that I wasn't having a Python problem as much as a *math* problem. I > changed my get_factors functio

Re: Using django ORM from web browser and from command line apps

2011-06-21 Thread Ian Kelly
also to commit everything immediately. Since there is no HTTP request, the TransactionMiddleware does not get invoked even if enabled. For controlled transactions you will need to use the commit_on_success or commit_manually decorator / context-managers: https://docs.djangoproject.com/en/1.3/topics/db/t

Re: using only the django ORM (DB access model) and nothing else.

2011-06-21 Thread Ian Kelly
On Tue, Jun 21, 2011 at 6:42 PM, News123 wrote: > ### > If running myapp.py I get following output: > > yes this line is executed > Traceback (most recent call last): >  File "./myapp.py", line 11, in >    class Mini(models.Model): >  File > "/opt/my_py

Re: Using django ORM from web browser and from command line apps

2011-06-21 Thread Ian Kelly
On Tue, Jun 21, 2011 at 6:21 PM, News123 wrote: > Out of curiousity: Do you know whether the imports would be executed for > each potential command as soon as I call manage.py or only > 'on demand'? Off the top of my head, I don't know. -- http://mail.python.org/mailman/listinfo/python-list

Re: How can I speed up a script that iterates over a large range (600 billion)?

2011-06-22 Thread Ian Kelly
On Tue, Jun 21, 2011 at 11:58 PM, Chris Torek wrote: > I was curious about implementing prime factorization as a generator, > using a prime-number generator to come up with the factors, and > doing memoization of the generated primes to produce a program that > does what "factor" does, e.g.: This

Re: How can I speed up a script that iterates over a large range (600 billion)?

2011-06-22 Thread Ian Kelly
between their squares are composite, thereby failing to add the next prime to the table until after its square has been reached. This seems a rather unlikely scenario, but I can't say for certain that it never happens. The Haskell version does not contain this flaw, as far as I can tell. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: writable iterators?

2011-06-23 Thread Ian Kelly
On Wed, Jun 22, 2011 at 3:54 PM, Steven D'Aprano wrote: > Fortunately, that's not how it works, and far from being a "limitation", > it would be *disastrous* if iterables worked that way. I can't imagine > how many bugs would occur from people reassigning to the loop variable, > forgetting that it

Re: How do you print a string after it's been searched for an RE?

2011-06-23 Thread Ian Kelly
On Thu, Jun 23, 2011 at 1:58 PM, John Salerno wrote: > After I've run the re.search function on a string and no match was > found, how can I access that string? When I try to print it directly, > it's an empty string, I assume because it has been "consumed." How do > I prevent this? This has noth

Re: Using decorators with argument in Python

2011-06-28 Thread Ian Kelly
's either @memoize or @memoize(foo), but never just the confusing @memoize(). Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: How to create n number of threads

2011-06-28 Thread Ian Kelly
On Tue, Jun 28, 2011 at 1:33 PM, hisan wrote: > How to create n number thread in python. i want iterate over the for loop and > create a thread for each iteration . > sample code >  for i in range(o,50): >  i want to create 50 thread here which call the same function. how start and > stop each t

Re: what is the airspeed velocity of an unladen swallow

2011-06-28 Thread Ian Kelly
On Tue, Jun 28, 2011 at 1:33 PM, Xah Lee wrote: > this will be of interest to those bleeding-edge pythoners. > > “what… is the airspeed velocity of an unladen swallow?” > > xahlee.org/funny/unladen_swallow.html More interesting to me is not the ad but that Wolfram Alpha will actually answer the q

Re: Using decorators with argument in Python

2011-06-29 Thread Ian Kelly
On Wed, Jun 29, 2011 at 1:30 PM, Ethan Furman wrote: > How about just having one bit of code that works either way? How would you adapt that code if you wanted to be able to decorate a function that takes arguments? This also won't work if the argument to the decorator is itself a callable, such

Re: Using decorators with argument in Python

2011-06-29 Thread Ian Kelly
On Wed, Jun 29, 2011 at 3:29 PM, Ethan Furman wrote: > 8< > class enclose(object): >    func = None >    def __init__(self, char='#'): >        self.char = char >        if callable(char):  # was a function passed in directly? >      

Re: how to call a function for evry 10 secs

2011-06-30 Thread Ian Kelly
; What should it do in Python 3.2? Exception or max(seconds, 0)? sleep(0) has some special semantics on Windows. Raising ValueError (or IOError to match Linux) makes the most sense to me. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: Why won't this decorator work?

2011-07-02 Thread Ian Kelly
Isn't that what the decorator syntax is essentially doing? No. The decorator syntax is doing: roll_die = move(roll_die) If you then call roll_die, that is equivalent to "move(roll_die)()", which is not the same thing as "move(roll_die())". Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: Testing if a global is defined in a module

2011-07-04 Thread Ian Kelly
aken to be a sequence of strings which are the exported names. Otherwise, the exported names are taken to be all the names in the module dict that don't begin with an underscore. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: emacs lisp text processing example (html5 figure/figcaption)

2011-07-05 Thread Ian Kelly
ity, but for completeness they should be stripped out of the final regex. The possibility of nested HTML in the caption is allowed for by using a negative look-ahead assertion to accept any tag except a closing . It would break if you had nested tags, but then that would be invalid html anyway. C

Re: emacs lisp text processing example (html5 figure/figcaption)

2011-07-05 Thread Ian Kelly
it works: >>> import re >>> s = ''' ... ... jamie's cat! Her blog is http://example.com/ ... jamie/">http://example.com/jamie/ ... ''' >>> print re.sub(pattern, replace, s) jamie's cat! Her blog is http://example.com/ jamie/">http://example.com/jamie/ Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: Implicit initialization is EXCELLENT

2011-07-06 Thread Ian Kelly
On Wed, Jul 6, 2011 at 12:49 AM, Ulrich Eckhardt wrote: > Mel wrote: >> In wx, many of the window classes have Create methods, for filling in >> various attributes in "two-step construction".  I'm not sure why, because >> it works so well to just supply all the details when the class is called >>

Re: interactive plots

2011-07-06 Thread Ian Kelly
On Wed, Jul 6, 2011 at 9:04 AM, Mihai Badoiu wrote: > How do I do interactive plots in python?  Say I have to plot f(x) and g(x) > and I want in the plot to be able to click on f and make it disappear.  Any > python library that does this? Matplotlib can be integrated with either wxPython or PyQt

Re: trouble creating tooltips using Wx in Tk canvas

2011-07-06 Thread Ian Kelly
kinter event loop rather than the wxPython event loop. If you want to use the wxToolTip widget, then you should write your program to use wxPython only. Alternatively, googling for "tkinter tooltip" turns up a couple of recipes; you could try one of those. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: Implicit initialization is EVIL!

2011-07-06 Thread Ian Kelly
On Wed, Jul 6, 2011 at 12:36 PM, Andrew Berg wrote: > On 2011.07.06 01:19 PM, rantingrick wrote: >> ## >>  The Roman Stawman Sketch >> ## > Nice try, but you have to use a Monty Python sketch (and you have to > spell correctly :-P ). Seriously. The

Re: Does hashlib support a file mode?

2011-07-06 Thread Ian Kelly
On Wed, Jul 6, 2011 at 1:07 PM, Phlip wrote: > If I call m = md5() twice, I expect two objects. > > I am now aware that Python bends the definition of "call" based on > where the line occurs. Principle of least surprise. There is no definition-bending. The code: """ def file_to_hash(path, m = h

Re: Large number multiplication

2011-07-06 Thread Ian Kelly
On Wed, Jul 6, 2011 at 1:30 PM, Billy Mays wrote: > I was looking through the python source and noticed that long multiplication > is done using the Karatsuba method (O(~n^1.5)) rather than using FFTs O(~n > log n).  I was wondering if there was a reason the Karatsuba method was > chosen over the

Re: Large number multiplication

2011-07-06 Thread Ian Kelly
On Wed, Jul 6, 2011 at 2:21 PM, Billy Mays wrote: > Side note: Are Numpy/Scipy the libraries you are referring to? I was thinking more of gmpy or mpmath, but I'm not personally well acquainted with any of them. -- http://mail.python.org/mailman/listinfo/python-list

Re: Large number multiplication

2011-07-07 Thread Ian Kelly
On Thu, Jul 7, 2011 at 2:30 AM, Ulrich Eckhardt wrote: > Even worse, most people would actually pay for its use, because they don't > use numbers large enough to merit the Schönhage–Strassen algorithm. As it stands, Karatsuba is only used for numbers greater than a specific threshold. Adding Sch

Re: Large number multiplication

2011-07-07 Thread Ian Kelly
On Thu, Jul 7, 2011 at 9:49 AM, Ian Kelly wrote: > On Thu, Jul 7, 2011 at 2:30 AM, Ulrich Eckhardt > wrote: >> Even worse, most people would actually pay for its use, because they don't >> use numbers large enough to merit the Schönhage–Strassen algorithm. > > As

Re: Implicit initialization is EXCELLENT

2011-07-07 Thread Ian Kelly
On Thu, Jul 7, 2011 at 1:12 AM, Ulrich Eckhardt wrote: >>> Just guessing, is it legacy, C-with-classes code rather than C++ code >>> perhaps? Haven't looked at wx for a while. Such code typically lacks >>> understanding of exceptions, which are the only way to signal failure >>> from e.g. construc

Re: The end to all language wars and the great unity API to come!

2011-07-08 Thread Ian Kelly
On Fri, Jul 8, 2011 at 1:05 PM, sal migondis wrote: >> I believe... > > Shifting from 'belief' to 'believe', the latter having a considerably > wider semantic scope. Wider how? Would you care to give an example of something that is believed but is not a belief? -- http://mail.python.org/mailman

Re: String concatenation vs. string formatting

2011-07-08 Thread Ian Kelly
On Fri, Jul 8, 2011 at 3:23 PM, Benjamin Kaplan wrote: > String formatting is the One Right Way here. It's fine to use string > concatenation for a few things, but the operation is O(n^2) because each > concat occurs one at a time: Python allocates space for a string the size of > the first 2 thin

Re: String concatenation vs. string formatting

2011-07-08 Thread Ian Kelly
;out = out + v" 100 loops, best of 3: 6.59 usec per loop $ python -m timeit -s "v = 'x' * 10; out = ''" "out = v + out" 10 loops, best of 3: 268 usec per loop Good to know. I had no idea such an optimization existed. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: String concatenation vs. string formatting

2011-07-08 Thread Ian Kelly
On Sat, Jul 9, 2011 at 12:16 AM, Chris Angelico wrote: > Has the same optimization been implemented for Unicode? The page > doesn't mention Python 3 at all, and I would guess that the realloc > optimization would work fine for both types of string. Seems to be implemented for strs in 3.2, but not

Re: python xauth

2011-07-11 Thread Ian Kelly
n" (xauth.org) scheme? The name is unfortunately overloaded. Second, have you tried googling for "python xauth"? Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: An interesting beginner question: why we need colon at all in the python language?

2011-07-11 Thread Ian Kelly
On Mon, Jul 11, 2011 at 8:50 AM, Sébastien Volle wrote: > Could it have been made optional, like the trailing comma in list > declaration? Cobra makes the colons optional, so probably yes. -- http://mail.python.org/mailman/listinfo/python-list

Re: Property setter and lambda question

2011-07-11 Thread Ian Kelly
 snippet I used to demonstrate the keyword property: What Thomas said. But also, please note that "property" is a builtin, not a keyword. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: Property setter and lambda question

2011-07-11 Thread Ian Kelly
On Mon, Jul 11, 2011 at 10:53 AM, Anthony Kong wrote: > Thanks again for your input, Thomas. > I normally prefer > not_here = property(lambda self: self.__get_not_here(), lambda self, v: > self.__set_not_here(v)) > than > not_here = property(__get_not_here, __set_not_here) > Because it allows me t

Re: Property setter and lambda question

2011-07-11 Thread Ian Kelly
On Mon, Jul 11, 2011 at 11:21 AM, Anthony Kong wrote: > Awesome, Thomas. The trick only works if there is only one leading > underscore in the method names. > The following example works as I expected for the derived class B. > class A(object): >     def __init__(self): >         self.__not_here =

Re: Set run vars with each call

2011-07-12 Thread Ian Kelly
On Tue, Jul 12, 2011 at 11:50 AM, Andrew Berg wrote: > On 2011.07.12 12:32 PM, Gnarlodious wrote: >> OK, that sets a value at init time. But is there a similar built-in >> to run whenever the class instance is called? > What do you mean by call an instance? Do you want to run certain code > whenev

Re: What is the difference between PyPy and Python? are there lot of differences?

2011-07-13 Thread Ian Kelly
On Wed, Jul 13, 2011 at 8:19 AM, Anthony Kong wrote: > One of the main difference is that pypy supports only R-Python, which stands > for 'Restricted Python". > It is a subset of C-python language. This is wrong. The PyPy *interpreter* is written in RPython. At the application level, PyPy suppo

Re: Functional style programming in python: what will you talk about if you have an hour on this topic?

2011-07-13 Thread Ian Kelly
On Wed, Jul 13, 2011 at 10:29 AM, Terry Reedy wrote: > The iteration protocol and the notion of iteraables as the common data > exchange format, with associated notions of iterators, generator functions, > and generators, are important features of Python. Not really functional > style, I guess. X

ANN: Urwid 0.9.9.2 - Console UI Library

2011-07-13 Thread Ian Ward
Announcing Urwid 0.9.9.2 Urwid home page: http://excess.org/urwid/ Screen shots: http://excess.org/urwid/examples.html Tarball: http://excess.org/urwid/urwid-0.9.9.2.tar.gz About this release: === This release is *not* the big, exciting, wow-look

Re: How can I make a program automatically run once per day?

2011-07-14 Thread Ian Kelly
On Thu, Jul 14, 2011 at 11:00 AM, monkeys paw wrote: > You could use the below code. time.sleep(# seconds in a day) > where i == 30 would run once a day for a month > > import time > i=0 > while (1): >        print 'hello' >        time.sleep(2)   # Change this to number of seconds in a day >    

Re: Possible File iteration bug

2011-07-14 Thread Ian Kelly
On Thu, Jul 14, 2011 at 1:46 PM, Billy Mays wrote: > def getLines(f): >    lines = [] >    for line in f: >        lines.append(line) >    return lines > > with open('/var/log/syslog', 'rb') as f: >    lines = getLines(f) >    # do some processing with lines >    # /var/log/syslog gets updated in

Re: Tabs -vs- Spaces: Tabs should have won.

2011-07-17 Thread Ian Kelly
On Sat, Jul 16, 2011 at 9:09 PM, Steven D'Aprano wrote: >> Personally, I like to use the tab _key_ as an input device, but to have >> my editor write real spaces to the file in consequence. With pure >> spaces, the text is laid out reliably for us both. And so I have my >> editor set to that behav

Re: Tabs -vs- Spaces: Tabs should have won.

2011-07-17 Thread Ian Kelly
On Sun, Jul 17, 2011 at 1:29 AM, Andrew Berg wrote: > You're right. TabError is only raised if the initial indentation is > inconsistent. > Not legal: > def spam(): > print('Wonderful spam!\n') > <4 spaces>print('Bloody Vikings!') > > Legal: > def eggs(): > print( > 'Blech!\n','Whaddya mean, "blec

Re: Tabs -vs- Spaces: Tabs should have won.

2011-07-17 Thread Ian Kelly
On Sun, Jul 17, 2011 at 9:15 AM, rantingrick wrote: >>  I can write my code to 80 >> columns using 4-space tabs, but if somebody later tries to edit the >> file using 8-space tabs, their lines will be too long. > > THEIR LINES is the key words. A tab control is a tab control is a (you > guessed it

Re: Tabs -vs- Spaces: Tabs should have won.

2011-07-17 Thread Ian Kelly
On Sun, Jul 17, 2011 at 10:29 AM, rantingrick wrote: > I hate  vertical white-space. I follow Python style guide suggestions, > and then some! I hate when people insert spaces into code blocks and > function/method bodies. If you feel a space must be inserted then that > is a good clue you should

Re: Tabs -vs- Spaces: Tabs should have won.

2011-07-17 Thread Ian Kelly
On Sun, Jul 17, 2011 at 1:54 PM, rantingrick wrote: > On Jul 17, 1:48 pm, Ian Kelly wrote: > >> Let me get this straight.  You want us to use tabs so that individuals >> can set their tab width to however many spaces they want, but then you >> want everybody to set thei

Re: Tabs -vs- Spaces: Tabs should have won.

2011-07-17 Thread Ian Kelly
On Sun, Jul 17, 2011 at 2:12 PM, rantingrick wrote: > On the face of it one might think vertical tabs are a good idea > however newlines work just fine. There is no reason for expanding > vertical whitespace to create readble code. If you can offer a good > reason i'm listening. Also be sure to po

Re: Crazy what-if idea for function/method calling syntax

2011-07-17 Thread Ian Kelly
2011/7/17 ΤΖΩΤΖΙΟΥ : > Jumping in: > > What if a construct > > xx(*args1, **kwargs1)yy(*args2, **kwargs2) > > was interpreted as > > xxyy(*(args1+args2), **(kwargs1+kwargs2)) > > (Note: with **(kwargs1+kwargs2) I mean "put keyword arguments in the > order given", since dicts can't be added) > >

Re: a little parsing challenge ☺

2011-07-18 Thread Ian Kelly
On Mon, Jul 18, 2011 at 11:12 AM, Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9...@myhashismyemail.com> wrote: > I gave it a shot.  It doesn't do any of the Unicode delims, because let's > face it, Unicode is for goobers. Uh, okay... Your script also misses the requirement of outputting the i

Re: Partial Function Application -- Advantages over normal function?

2011-07-19 Thread Ian Kelly
On Tue, Jul 19, 2011 at 12:22 AM, Thomas Jollans wrote: >> Supplemental: The above can be simplified to >> >> def makeadder(y): return lambda x: x + y >> > > In turn: > > makeadder = lambda y: lambda x: x + y That's not an improvement. lambda is for making anonymous functions. If you're going to

Re: Partial Function Application -- Advantages over normal function?

2011-07-19 Thread Ian Kelly
On Tue, Jul 19, 2011 at 10:58 AM, Thomas Jollans wrote: > No, it's not an improvement. It's an illustration. I get that. The difference I pointed out between your "simplification" and the other Thomas's is the reason why yours would be unpythonic whilst his is fine. -- http://mail.python.org/ma

Re: a little parsing challenge ☺

2011-07-19 Thread Ian Kelly
On Tue, Jul 19, 2011 at 10:54 AM, Xah Lee wrote: > On Sunday, July 17, 2011 2:48:42 AM UTC-7, Raymond Hettinger wrote: >> On Jul 17, 12:47 am, Xah Lee wrote: >> > i hope you'll participate. Just post solution here. Thanks. >> >> http://pastebin.com/7hU20NNL > > just installed py3. > there seems t

Re: I am fed up with Python GUI toolkits...

2011-07-20 Thread Ian Kelly
On Tue, Jul 19, 2011 at 8:12 PM, sturlamolden wrote: > 3. Unpythonic memory management: Python references to deleted C++ > objects (PyQt). Manual dialog destruction (wxPython). Parent-child > ownership might be smart in C++, but in Python we have a garbage > collector. Perhaps you already know th

Re: a little parsing challenge ☺

2011-07-20 Thread Ian Kelly
On Wed, Jul 20, 2011 at 12:29 AM, jmfauth wrote: >> Then it is hard to code precisely. >> > > Not really. The trick is to count the different opener/closer > separately. > That is what I am doing to check balanced brackets in > chemical formulas. The rules are howerver not the same > as in math.

Re: total_ordering behaviour

2011-07-20 Thread Ian Kelly
On Wed, Jul 20, 2011 at 4:18 AM, risboo6909 wrote: > Hello all, > > I've noticed some strange behaviour of functools.total_ordering > decorator, at least it seems strange to me. Looks like this is already known and fixed as of March: http://bugs.python.org/issue10042 -- http://mail.python.org/m

Re: a little parsing challenge ☺

2011-07-21 Thread Ian Kelly
On Thu, Jul 21, 2011 at 6:58 AM, Xah Lee wrote: > Thanks a lot for the fix Raymond. That fix was from Thomas Jollans, not Raymond Hettinger. > Though, the code seems to have a minor problem. > It works, but the report is wrong. > e.g. output: > > 30068: c:/Users/h3/web/xahlee_org/p/time_machine\

Re: Writing a MUD Console

2011-07-22 Thread Ian Kelly
lhall.com/RosMudAndroid.py and > give it a whirl! I also have a half-finished (but fully usable) MUD client written in Python. It uses Twisted and wxPython and is cross-platform. I would be willing to share the code if somebody is interested. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list

Re: PEP 8 and extraneous whitespace

2011-07-22 Thread Ian Kelly
On Fri, Jul 22, 2011 at 6:59 AM, Neil Cerutti wrote: > Under the assumption that leading white space is important for > code formatting, but that all alignment after that is > unimportant. ...unless you're trying to adhere to a line length limit. "80 characters" is a lot easier to do in a fixed-

Re: PEP 8 and extraneous whitespace

2011-07-22 Thread Ian Kelly
On Fri, Jul 22, 2011 at 11:43 AM, rusi wrote: > Also it is more optimized. For the same size -- and therefore > readability -- a proportional font packs in more text. More text == less readability. This is one of the reasons I limit my line lengths. -- http://mail.python.org/mailman/listinfo/py

Re: Decorator behavior

2011-07-22 Thread Ian Kelly
On Fri, Jul 22, 2011 at 2:38 PM, mhearne808[insert-at-sign-here]gmail[insert-dot-here]com wrote: > I am just trying to wrap my head around decorators in Python, and I'm > confused about some behavior I'm seeing.  Run the code below (slightly > adapted from a Bruce Eckel article), and I get the fol

<    5   6   7   8   9   10   11   12   13   14   >