@# Quotes DB     useful, funny, interesting





Google
 
Web www.quotesdb.info
Undernet  |  EFnet  |  Quakenet  |  Freenode  |  Dalnet  |  Ircnet  |  Galaxynet
Page: 1 2



Comments:

<0> is there a way to check if a array index of out of range without using "try/exception"?
<1> yea, check if index < len(list) - 1
<1> er
<1> < len(list)
<0> got it
<0> thanks
<1> np
<2> well, 0 <= index < len(list)
<1> ya, better
<3> i'm writing a python script to take the output from 'ps -aux' and turn it into an html table, but i'm running into a problem when commands in the 'ps -aux' command listing contain spaces
<3> any ideas/
<4> ubuntunoo: strip spaces with a regexp?
<4> I have a webapp which gets xml data from a https source
<4> at the moment, each incoming request to my webapp generates a new https connection to the data source
<4> is there any pre-existing code that would let me have a persistent https connection to the datasource so I can serve requests quicker?
<5> HI GUYZ!



<1> is there a searchable container in Python, something that is maybe a balanced BST in implementation?
<6> You have dicts, which are hash tables for now.
<1> yea I know
<1> but nothing ordered? perhaps in __future__ ?
<2> put it in a list and sort
<6> You can make a list of the dict and then sort that. If that's too ineffective for your needs, you can roll your own cl*** where you're putting elements from a linked list in a dict.
<6> Or make your own BST.
<1> this isnt' for anything specific..was just curious
<1> ok, thansk
<7> re
<7> It's a FAQ
<7> I wonder how is it such a pervasive trait to feel the need for ordered dicts
<7> and yet a custom type never seems to be worth it :)
<1> hah
<1> I suppose that's a project for the weekend, an RB tree
<8> not all python objects have an order..
<8> the keys would have to be the same type
<1> yes
<8> or able to be compared among themselves
<8> so I guess numbers would work
<2> not complex numbers
<8> true
<7> madewokhe: mixed-type lists sort very gracefully, this is not a problem
<8> lists would also work
<2> except if you have complex numbers in there :-)
<1> haha
<8> oh
<8> wait
<1> madewokhe: as long as they're comparable, it doesn't amtter
<8> cmp(5,"b") => -1
<1> what yason said
<2> that's really operator error, though
<8> cmp("b",(1, 2, 3)) => -1
<8> cmp((1, 2, 3),5) => 1
<8> amazing
<2> if you have a sorted data structure and you're putting something in it that doesn't compare well with the other things, that's your fault, not the data structure implementor's
<1> aye
<8> yep
<2> now try cmp(1, 1+1j)
<8> TypeError: no ordering relation is defined for complex numbers
<2> Guido's unwise decision
<8> well, how would you do that?
<1> 1 = 1 + 0j
<8> cmp(1,1+0j) => 0
<2> well, traditionally there is of course no ordering on the complex numbers
<8> right..
<2> but n-tuples are vectors and there's traditionally no ordering on them either
<2> but Python has one
<2> basically everything "behaves" in the sense of not breaking horribly ... except complex numbers
<8> but then weird things happen
<8> when you have two complex numbers that are slightly off but "close enough"
<8> don't they?
<2> even if you define objects which don't have a well-ordering, aList.sort still doesn't freak out
<2> same with floats
<2> the issue isn't that complex numbers should inherently have some ordering
<8> cmp({},{1:2}) => -1
<2> it's just that they're the ONLY type in Python that doesn't play well
<8> what if I make a custom type and don't define __cmp__ ?
<1> are there plans to change that, or..
<1> ?
<8> or __le__ etc



<2> it uses id
<2> but here's the fun part: 1 < 1+1j is an exception
<2> but 1 < 'a' < 1+1j isn't
<8> :o
<2> (since they short circuit)
<1> huh, how come?
<2> a < b < c is the same as a < b and b < c
<8> because 1 isn't less than 'a' ?
<1> ah right
<2> so if a < b is false, b < c never gets evaluated
<2> yes
<8> oh
<2> complex numbers and comparisons are Python's dirty little secret
<1> hmm that's not good
<8> I thought you were saying both comparisons were valid
<2> but then again, who gives a **** about complex numbers anyway :-)
<8> especially as keys
<2> well you can hash complex numbers fine
<2> so they can be keys
<8> cmp('a', 1+1j) => 1
<2> err, right
<2> that's because it just sorts based on type
<2> it never looks at the value
<8> does it sort the types by id?
<8> or is there something else?
<2> by something arbitrary you can't guess
<8> ok
<2> it'll be consistent from run to run and probably from version to version but you shouldn't rely on it
<8> that rules "name" out
<2> basically the feature here is "sort won't break"
<2> not that you can do something meaningful with it
<8> I have to figure it out now
<8> cmp('a', 1) => 1
<8> cmp('a', 1.0) => 1
<8> hmm
<2> it ight be the id of the type
<7> of course you can inherit complex and write __lt__ and __gt__ that give comparison results for complex number objects using some arbitrary rule
<2> true
<2> but that won't help you when you use complex literals
<8> I didn't think of that
<7> but in Python that never _feels_ like a native type
<7> I wonder you could modify the complex cl*** itself by using meta cl***es?
<7> at least you can't just ***ign to complex.__lt__ anything
<8> so wait
<8> I can sort anything
<8> except complex
<8> and the order will be consistent?
<8> I doubt you could modify the complex cl*** in just python
<8> I don't see how metacl***es could help
<7> madewokhe: theoretically you could tweak the complex cl*** on the fly
<7> madewokhe: but that may or may not work depending on how "internally" python creates complex objects. And i'm not sure whether creating objects through literals is implemented
<8> if you compile a function with a literal complex number in it, it probably does store it as a constant
<8> but that should be ok if you change the complex cl***
<2> you can't really change complex numbers themselves but you could derive a cl*** that's well-behavior
<2> behaved*
<2> you could also write a cl*** that overriddes __cmp__, etc. to deliberately make weird results
<2> so .sort would not do anything useful, but it wouldn't freak out
<8> ***uming you make sure __cmp__ is consistent within your type though
<7> you don't seem to be able to change the complex cl*** itself but does anyone know if complex is in practice initialized through the default metacl***?
<8> would sort be consistent?
<2> well you could deliberately make one that ISN'T consistent
<2> but if yours is consistent then yes, .sort definitely would be consistent
<8> awesome
<8> so if someone did this BST thing, you could use lists as keys..
<2> you could use anything, other than complex numbers
<2> in general
<8> are AVL trees supposed to store the balance factor?
<8> I guess that's easily calculated if you know the size..
<8> never mind
<8> can I count on cmp to not raise an AttributeError?
<8> nm
<8> this is not easy
<8> no wonder no one does it
<9> ..


Name:

Comments:

Please enter the result of the sum 63 + 46 (to avoid spam):






Return to #python
or
Go to some related logs:

#vmware
#sex
geordie cheeseman
#politics
akrapovic.fr
#freebsd
#hardware
#delphi
failed to open /root/.cvspass
warmeetings



Home  |  disclaimer  |  contact  |  submit quotes