Re: Using String Methods In Jump Tables

2010-08-23 Thread Hrvoje Niksic
Tim Daneliuk writes: >You can get away with this because all string objects appear to point to > common >method objects. That is,: id("a".lower) == id("b".lower) A side note: your use of `id' has misled you. id(X)==id(Y) is not a perfect substitue for the X is Y. :) "a".lower and "b

Re: Contains/equals

2010-08-24 Thread Hrvoje Niksic
Lawrence D'Oliveiro writes: > In message , Alex Hall > wrote: > >> def __eq__(self, obj): >> if self.a==obj.a and self.b==obj.b: return True >> return False > > Is there a “Useless Use Of ...” award category for these “if then > return True; else return False” constructs? Well, remember

Re: speed of numpy.power()?

2010-08-25 Thread Hrvoje Niksic
Carlos Grohmann writes: > I'd like to hear from you on the benefits of using numpy.power(x,y) > over (x*x*x*x..) > > I looks to me that numpy.power takes more time to run. You can use math.pow, which is no slower than repeated multiplication, even for small exponents. Obviously, after the expon

Re: waling a directory with very many files

2009-06-15 Thread Hrvoje Niksic
Terry Reedy writes: > You did not specify version. In Python3, os.walk has become a > generater function. So, to answer your question, use 3.1. os.walk has been a generator function all along, but that doesn't help OP because it still uses os.listdir internally. This means that it both create

Re: waling a directory with very many files

2009-06-15 Thread Hrvoje Niksic
Nick Craig-Wood writes: > Here is a ctypes generator listdir for unix-like OSes. ctypes code scares me with its duplication of the contents of system headers. I understand its use as a proof of concept, or for hacks one needs right now, but can anyone seriously propose using this kind of code i

Re: waling a directory with very many files

2009-06-16 Thread Hrvoje Niksic
Nick Craig-Wood writes: > It can be done properly with gccxml though which converts structures > into ctypes definitions. That sounds interesting. > That said the dirent struct is specified by POSIX so if you get the > correct types for all the individual members then it should be > correct eve

Re: class or instance method

2009-06-17 Thread Hrvoje Niksic
Paul Johnston writes: > I would like to have a method that is both a classmethod and an > instancemethod. So: > > class MyClass(object): > @class_or_instance > def myfunc(cls_or_self): > pass > > The semantics I'd like are: > When you call MyClass.myfunc, it gets passed a class > When you

Re: Why re.match()?

2009-07-02 Thread Hrvoje Niksic
kj writes: > For a recovering Perl-head like me it is difficult to understand > why Python's re module offers both match and search. Why not just > use search with a beginning-of-string anchor? I need re.match when parsing the whole string. In that case I never want to search through the strin

Re: missing 'xor' Boolean operator

2009-07-15 Thread Hrvoje Niksic
Jean-Michel Pichavant writes: > While everyone's trying to tell the OP how to workaround the missing > xor operator, nobody answered the question "why is there no [boolean] > xor operator ?". Probably because there isn't one in C. The bitwise XOR operator, on the other hand, exists in both C an

Re: missing 'xor' Boolean operator

2009-07-15 Thread Hrvoje Niksic
Jean-Michel Pichavant writes: > Hrvoje Niksic wrote: > [snip] >> Note that in Python A or B is in fact not equivalent to not(not A and >> not B). >> >>>> l = [(True, True), (True, False), (False, True), (False, False)] >>>> for p in l: > ..

Re: proposal: add setresuid() system call to python

2009-07-20 Thread Hrvoje Niksic
"Diez B. Roggisch" writes: To emulate the os-module-type calls, it's better to raise exceptions than return negative values: > def setresuid(ruid, euid, suid): > return _setresuid(__uid_t(ruid), __uid_t(euid), __uid_t(suid)) def setresuid(ruid, euid, suid): res = _setresuid(__uid_t(ruid

Re: Help understanding the decisions *behind* python?

2009-07-20 Thread Hrvoje Niksic
Phillip B Oldham writes: > On Jul 20, 6:08 pm, Duncan Booth wrote: >> The main reason why you need both lists and tuples is that because a tuple >> of immutable objects is itself immutable you can use it as a dictionary >> key. > > Really? That sounds interesting, although I can't think of any r

Re: Help understanding the decisions *behind* python?

2009-07-20 Thread Hrvoje Niksic
Chris Rebert writes: >>> x = [2,1,3] >>> print sorted(x)[0] >>>DB> 3 >> >> What kind of Python produces that? > > Assuming you're referring to the latter example, it was added in version 2.4 > If you meant the former example, I think that's purely pseudo-Python. sorted([2, 1, 3])[0] eval

Re: non-owning references?

2009-07-24 Thread Hrvoje Niksic
Ben Finney writes: > Utpal Sarkar writes: > >> Is there a way I can tell a variable that the object it is pointing >> too is not owned by it, in the sense that if it is the only reference >> to the object it can be garbage collected? > > Python doesn't have “pointers”, and doesn't really have “v

Re: The longest word

2009-07-28 Thread Hrvoje Niksic
Piet van Oostrum writes: >> NiklasRTZ (N) wrote: > >>N> Thank you. This seems to work: >>N> sorted("a AAA aa a sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)- >>N> len(b))[-1] >>N> 'sdfsdfsdfsdf' > > simpler: > > sorted("a AAA aa a sdfsdfsdfsdf vv".split(' '), key=len)[-1] There i

Re: FTP Offset larger than file.

2009-07-28 Thread Hrvoje Niksic
Bakes writes: > The error I get is: > ftplib.error_temp: 451-Restart offset 24576 is too large for file size > 22852. > 451 Restart offset reset to 0 > which tells me that the local file is larger than the external file, > by about a kilobyte. Certainly, the local file is indeed that size, so > m

Re: FTP Offset larger than file.

2009-07-28 Thread Hrvoje Niksic
Bakes writes: >> > As a quick fix, you can add a file.flush() line after the >> > file.write(...) line, and the problem should go away. >> >> Thank you very much, that worked perfectly. > > Actually, no it didn't. That fix works seamlessly in Linux, but gave > the same error in a Windows environm

Re: New implementation of re module

2009-07-30 Thread Hrvoje Niksic
MRAB writes: > So it complains about: > > ++(RE_CHAR*)context->text_ptr > > but not about: > > ++info->repeat.count > > Does this mean that the gcc compiler thinks that the cast makes it an > rvalue? The cast operator does return an rvalue, treating it otherwise used to be an extension t

Re: Generate a new object each time a name is imported

2009-08-02 Thread Hrvoje Niksic
Steven D'Aprano writes: > I'm looking for a way to hide the generation of objects from the caller, > so I could do something like this: > > from Module import factory() as a # a == "Object #1" > from Module import factory() as b # b == "Object #2" > > except of course that syntax is illegal.

Re: proposal: add setresuid() system call to python

2009-08-25 Thread Hrvoje Niksic
travis+ml-pyt...@subspacefield.org writes: > On Mon, Jul 20, 2009 at 04:10:35PM +0200, Hrvoje Niksic wrote: >> To emulate the os-module-type calls, it's better to raise exceptions >> than return negative values: >> >> > def setresuid(ruid, euid, suid): >

Re: Question on the "csv" library

2009-08-28 Thread Hrvoje Niksic
David Smith writes: >> 2- the "C" in "CSV" does not mean "comma" for Microsoft Excel; the ";" >> comes from my regional Spanish settings > > The C really does stand for comma. I've never seen MS spit out > semi-colon separated text on a CSV format. That's because you're running MS Office in a U

Re: Are min() and max() thread-safe?

2009-09-17 Thread Hrvoje Niksic
Steven D'Aprano writes: >> min() and max() don't release the GIL, so yes, they are safe, and >> shouldn't see a list in an inconsistent state (with regard to the >> Python interpreter, but not necessarily to your application). But a >> threaded approach is somewhat silly, since the GIL ensures t

Re: Speed-up for loops

2010-09-02 Thread Hrvoje Niksic
Michael Kreim writes: > Are there any ways to speed up the for/xrange loop? > Or do I have to live with the fact that Matlab beats Python in this > example? To a point, yes. However, there are things you can do to make your Python code go faster. One has been pointed out by Peter. Another is

Re: Speed-up for loops

2010-09-03 Thread Hrvoje Niksic
Ulrich Eckhardt writes: > Tim Wintle wrote: >> [..] under the hood, cpython does something like this (in psudo-code) >> >> itterator = xrange(imax) >> while 1: >> next_attribute = itterator.next >> try: >> i = next_attribute() >> except: >> break >> a = a + 10 > > There is one th

Re: Overriding dict constructor

2010-09-20 Thread Hrvoje Niksic
Christian Heimes writes: > Am 20.09.2010 13:11, schrieb Steven D'Aprano: >> I have a dict subclass that associates extra data with each value of the >> key/value items: > [...] >> How can I fix this? > > Since the dict class is crucial to the overall performance of Python, > the dict class behav

Re: if the else short form

2010-09-29 Thread Hrvoje Niksic
Tracubik writes: > Hi all, > I'm studying PyGTK tutorial and i've found this strange form: > > button = gtk.Button(("False,", "True,")[fill==True]) > > the label of button is True if fill==True, is False otherwise. The tutorial likely predates if/else expression syntax introduced in 2.5, which w

Re: About __class__ of an int literal

2010-09-29 Thread Hrvoje Niksic
Steven D'Aprano writes: > On Wed, 29 Sep 2010 02:20:55 +0100, MRAB wrote: > >> On 29/09/2010 01:19, Terry Reedy wrote: > >>> A person using instances of a class should seldom use special names >>> directly. They are, in a sense, implementation details, even if >>> documented. The idiom "if __name

Re: Many newbie questions regarding python

2010-10-09 Thread Hrvoje Niksic
alex23 writes: > If anything, I feel like the list comp version is the correct solution > because of its reliability, whereas the multiplication form feels like > either a lucky naive approach or relies on the reader to know the type > of the initialising value and its mutability. Other than lis

Re: hashkey/digest for a complex object

2010-10-10 Thread Hrvoje Niksic
Steven D'Aprano writes: > On Sat, 09 Oct 2010 21:39:51 +0100, Arnaud Delobelle wrote: > >> 1. hash() is an idempotent function, i.e. hash(hash(x)) == hash(x) hold >> for any hashable x (this is a simple consequence of the fact that >> hash(x) == x for any int x (by 'int' I mean 2.X int)). > > It'

Re: hashkey/digest for a complex object

2010-10-10 Thread Hrvoje Niksic
Arnaud Delobelle writes: > I have learnt too that hash(-1) is not (-1), and that it seems that a > hash value is not allowed to be (-1). There is one thing left to find > out. Why can't it be (-1)? Because -1 has a special meaning in the C function equivalent to Python's hash(). PyObject_Hash

Re: if the else short form

2010-10-10 Thread Hrvoje Niksic
Antoon Pardon writes: > Personaly I don't see a reason to declare in advance that someone > who wants to treat "True" differently from non-zero numbers or > non-empty sequences and does so by a test like: > > if var == Trueorif var is True > > to have written incorrect code. I wouldn't

Re: harmful str(bytes)

2010-10-12 Thread Hrvoje Niksic
Stefan Behnel writes: > Hallvard B Furuseth, 11.10.2010 23:45: >> If there were a __plain_str__() method which was supposed to fail rather >> than start to babble Python syntax, and if there were not plenty of >> Python code around which invoked __str__, I'd agree. > > Yes, calling str() "just in

Re: How is correct use of eval()

2010-10-12 Thread Hrvoje Niksic
Nobody writes: > Oh, look what's "new in version 2.6": > > > ast.literal_eval("7") > 7 > > ast.literal_eval("7") == 7 > True Note that it doesn't work for some reasonable inputs involving unary and binary plus, such as "[-2, +1]" or "2+3j". This has been fixed in the dev

Re: annoying CL echo in interactive python / ipython

2010-10-19 Thread Hrvoje Niksic
Jed Smith writes: > On Tue, Oct 19, 2010 at 1:37 PM, kj wrote: > >> % stty -echo > > That doesn't do what you think it does. Really? Turning off tty echo sounds exactly like what he wants. Emacs shell echoes characters for you, just like interactive shells do. When you press enter, the charac

Re: annoying CL echo in interactive python / ipython

2010-10-19 Thread Hrvoje Niksic
Jed Smith writes: >> echo (-echo) >> Echo back (do not echo back) every character typed. > > I'm going to guess that the percent sign in your prompt indicates that > you're using zsh(1). With my minimally-customized zsh, the echo > option is reset every time the prompt is dis

Re: Unicode questions

2010-10-19 Thread Hrvoje Niksic
Tobiah writes: > would be shared? Why can't we just say "unicode is unicode" > and just share files the way ASCII users do. Just have a huge > ASCII style table that everyone sticks to. I'm not sure that I understand you correctly, but UCS-2 and UCS-4 encodings are that kind of thing. Many pe

Re: overriding a property

2010-10-20 Thread Hrvoje Niksic
Lucasm writes: > Thanks for the answers. I would like to override the property though > without making special modifications in the main class beforehand. Is > this possible? That will not be easy. When you access obj.return_five, python looks up 'return_five' in type(obj) to see what the retur

Re: how to scrutch a dict()

2010-10-21 Thread Hrvoje Niksic
Joost Molenaar writes: > Using a 2.7/3.x dictionary comprehension, since you don't seem to mind > creating a new dictionary: > > def _scrunched(d): >     return { key: value for (key, value) in d.items() if value is not None } Note that a dict comprehension, while convenient, is not necessary fo

mro() or __mro__?

2010-10-23 Thread Hrvoje Niksic
The documentation of the mro() method on the class object says: class.mro() This method can be overridden by a metaclass to customize the method resolution order for its instances. It is called at class instantiation, and its result is stored in __mro__. Am I interpreting it correctly

Re: Has Next in Python Iterators

2010-10-25 Thread Hrvoje Niksic
Kelson Zawack writes: > Iterators however are a different beast, they are returned by the > thing they are iterating over and thus any special cases can be > covered by writing a specific implementation for the iterable in > question. This sort of functionality is possible to implement, > becaus

Re: downcasting problem

2010-10-26 Thread Hrvoje Niksic
John Nagle writes: > On 10/25/2010 7:38 AM, Tim Chase wrote: >> While a dirty hack for which I'd tend to smack anybody who used it...you >> *can* assign to instance.__class__ > >That's an implementation detail of CPython. May not work in > IronPython, Unladen Swallow, PyPy, or Shed Skin. > >

Re: Multilingual documentation solutions

2010-10-27 Thread Hrvoje Niksic
Astley Le Jasper writes: > At the moment I'm producing a word document with screenshots that gets > translated, but this is getting very difficult to control, especially > tracking small content changes and translations. I don't know if you considered this, but you might want to look into Restru

Re: factorial of negative one (-1)

2010-11-01 Thread Hrvoje Niksic
Chris Rebert writes: > (2) The underlying double-precision floating-point number only has ~16 > decimal digits of precision, so it's pointless to print out "further" > digits. A digression which has nothing to do with Raj's desire for "better accuracy"... Printing out further digits (without qu

Re: factorial of negative one (-1)

2010-11-02 Thread Hrvoje Niksic
Ken Watford writes: > 1.1 .as_integer_ratio() >> (2476979795053773, 2251799813685248) > > Handy, but if you need the exact representation, my preference is > float.hex, which seems to be the same as C99's %a format. [...] > Granted, it's not as easy for humans to interpret, but it's useful fo

Re: functions, list, default parameters

2010-11-03 Thread Hrvoje Niksic
Paul Rudin writes: > Terry Reedy writes: > >> Suppose I write an nasty C extension that mutates tuples. What then >> would be illegal about... > > Depends on exactly what we mean by legal. If immutability is part of the > language spec (rather than an artifact of a particular implementation) > t

Re: Man pages and info pages

2010-11-03 Thread Hrvoje Niksic
Teemu Likonen writes: > Enter Follow a link (down to node) > u up node level > h help (general how-to) > ? help (commands) > s search And don't forget: l last viewed page (aka "back") That one seems to be the info reader's best-kept sec

Re: Silly newbie question - Carrot character (^)

2010-11-06 Thread Hrvoje Niksic
Seebs writes: > I'm a bit lost here. Could you highlight some of the differences > between "a reference manual for the language itself" and "something > written for language lawyers"? I don't speak for "Nobody", but to me a reference manual would be a document intended for the user of the langu

Re: Silly newbie question - Carrot character (^)

2010-11-06 Thread Hrvoje Niksic
Seebs writes: > On 2010-11-06, Hrvoje Niksic wrote: >> I don't speak for "Nobody", but to me a reference manual would be a >> document intended for the user of the language. The thing for the >> language lawyer is something intended for the implementor,

Re: Is Eval *always* Evil?

2010-11-10 Thread Hrvoje Niksic
Simon Mullis writes: > If "eval" is not the way forward, are there any suggestions for > another way to do this? ast.literal_eval might be the thing for you. -- http://mail.python.org/mailman/listinfo/python-list

Re: Is Eval *always* Evil?

2010-11-10 Thread Hrvoje Niksic
Robert Kern writes: > On 2010-11-10 15:52 , Hrvoje Niksic wrote: >> Simon Mullis writes: >> >>> If "eval" is not the way forward, are there any suggestions for >>> another way to do this? >> >> ast.literal_eval might be the thing for you. &

<    1   2   3   4   5