Re: python bijection

2009-11-20 Thread Joshua Bronson
On Nov 20, 3:09 pm, Terry Reedy  wrote:
> Use standard subscripting with slices, and only that, to both get and set.

i did this for __delitem__ too, so you can do e.g. del m[:'abc'].

> In fact, to emphasize the symmetry of the bijective map, consider
> disallowing m[key] as ambiguous and require m[key:]

my initial feeling is that i'd prefer to be able to say m[key], the
defense being that it's not ambiguous if it's documented, and users
who don't like it don't have to use it, while users who do like it
won't be alienated. but i am definitely still open to this.

> Note that m[slice(1,2,3):] passes slice(slice(1, 2, 3), None, None), so
> this approach does not even prevent using slices as keys/values.

except slices aren't hashable. but props for even thinking
of that!

> In __setitem__, m[a:b] which passes slice(a,b,None) would have to be an
> error. In __getitem__, it could either be a error or return True/False
> depending on whether the pair is in the map.

i went with raising an error for __getitem__(slice(a,b,None)).
returning True/False for this usage based on whether a -> b is in the
bijection is an interesting idea, but i feel like it complicates
things for no good reason: if that's what you wanted to know you'd
just ask whether m[a] == b.

> But this depends on whether __contains__ only tests keys or is modified to 
> test pairs.

i have __contains__ only testing keys, and similarly [i for i in
bijection] only gives you the keys, again on the theory that deviating
too much from the dict api increases the learning (adoption) curve, so
i think we should only do it if it buys us a tremendous usability win.

thanks again for your insightful input, i'm pretty psyched about how
this is coming along!

any further feedback is always welcome.

josh
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python simply not scaleable enough for google?

2009-11-20 Thread sturlamolden
On 20 Nov, 22:45, a...@pythoncraft.com (Aahz) wrote:

> Are you advocating a high-fiber diet?

Only if you are a ruminant.

No really...

Windows has user-space threads natively. But you must reserve some
stack space for them (from virtual memory), which mainly makes them
useful on 64 bit systems.




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python simply not scaleable enough for google?

2009-11-20 Thread Aaron Watters

> Because `language is slow' is meaningless.

Yes.  Everyone knows java is faster than Python right?
But look here:

http://gaejava.appspot.com/

(you might want to run it a couple times to see what
it does when it is 'warm').  I don't think this is
a biased test -- I think the author expected to see
Java faster.

In my runs Python is usually a bit faster on a majority
of these metrics consistently.  Why?  My guess is the
reason is that Python is less bloated than Java so
more of it can stay resident on a shared machine
whereas big hunks of Java have to be swapped in for
every access -- but it's just a guess.

By the way: I see this all the time -- for web use
Python always seems to be faster than Java in my
experience.  (With programs that I write: I tend
to avoid some of the larger Python web tools available
out there and code close to the protocol.)

Comparing language platforms using small
numeric benchmarks often completely misses the
point.

   -- Aaron Watters
  http://whiffdoc.appspot.com
  http://listtree.appspot.com

===
an apple every 8 hours will keep 3 doctors
away.  - kliban
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python and web pages

2009-11-20 Thread Daniel Dalton
On Thu, Nov 19, 2009 at 09:43:50AM +0100, Diez B. Roggisch wrote:
> >elinks, links, links2 and do the following.
> >1. Open a certain web page and find the first text box on the page, and
> >put this text into the form.
> >2. Press the submit button, and wait for the result page to load.
> >3. Click on the 15th link down the page.
> >
> >So, how do I do this, can anyone point me to some docs or modules that
> >may help out here?
> 
> Use the module "mechanize". It will mimic a browser, and allows you
> to navigate to pages, fill in forms & submit them.

Well, what seemed a very daunting task at first, proved to be quite
easy, after a little bit of research. Thanks very much, I've written my
script, and it works correctly, using the mechanize module.

Thanks for everyone's help

-- 
Cheers,
Dan

http://members.iinet.net.au/~ddalton/



signature.asc
Description: Digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Go versus Brand X

2009-11-20 Thread Mensanator
On Nov 20, 7:12 pm, a...@pythoncraft.com (Aahz) wrote:
> Comparing Go to another computer language -- do you recognize it?

No, it predates my entry into the computer biz.

>
> http://www.cowlark.com/2009-11-15-go/
> --
> Aahz (a...@pythoncraft.com)           <*>        http://www.pythoncraft.com/
>
> "Debugging is twice as hard as writing the code in the first place.
> Therefore, if you write the code as cleverly as possible, you are, by
> definition, not smart enough to debug it."  --Brian W. Kernighan

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the naming convention for accessor of a 'private' variable?

2009-11-20 Thread Gregory Ewing

Steven D'Aprano wrote:

On Wed, 18 Nov 2009 18:47:34 -0800, Chris Rebert wrote:

Accessor methods are not Pythonic. 


Accessor methods are *usually* not Pythonic, at least not the way they 
are commonly used in Java.


To elaborate, accessor methods that *only* read and write
another, ordinary attribute are completely unnecessary
in Python. In that case there is no harm in exposing the
underlying attribute directly, because you can always
replace it with a property later if you change your mind,
without requiring any change in calling code.

Also, even when you do need accessor methods, it's more
elegant to hide them behind a property than to expect
callers to use them directly. It makes the calling code
more concise, and allows for changing your mind in the
opposite direction.

--
Greg
--
http://mail.python.org/mailman/listinfo/python-list


Go versus Brand X

2009-11-20 Thread Aahz
Comparing Go to another computer language -- do you recognize it?

http://www.cowlark.com/2009-11-15-go/
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it."  --Brian W. Kernighan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Relative versus absolute paths on Windows

2009-11-20 Thread Gregory Ewing

Jason R. Coombs wrote:


In other words, paths without a drive letter are reported as absolute,
but treated as relative, except in a few special cases.


It's not clear what the result ought to be here, since
Windows drive-relative paths don't really fit into the
unix absolute/relative dichotomy. Arguments could be
made either way, and what's right probably depends on
the reason you're asking.

For cross-platform code, it's probably safest to avoid
drive-relative paths altogether -- convert them to
fully absolute paths at the earliest opportunity.


ntpath.join('d:\\foo', '\\bar')


'\\bar'


This does seem like a bug, though -- the correct result
should really be 'd:\\bar', since that's what you would
get if you used the name '\\bar' with 'd:' as your current
drive.

--
Greg
--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of [:]

2009-11-20 Thread Esmail

Diez B. Roggisch wrote:

Esmail schrieb:

None of your operations changes a. But I talked about the lists you 
bound b and c to before. Those aren't changed as well - they simply are 
not pointed to anymore. In your example, that means the will be 
garbage-collected, in other scenarios, such as this, the stay:


a = []
foo = []
bar = foo
assert bar is foo
bar = a[:]
assert bar is not foo


Got it :-) .. thanks for helping out Diez

Esmail

--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of [:]

2009-11-20 Thread Diez B. Roggisch

Esmail schrieb:

Diez B. Roggisch wrote:

Esmail schrieb:

Could someone help confirm/clarify the semantics of the [:] operator
in Python?

a = range(51,55)

# 1 ##
b = a[:] # b receives a copy of a, but they are independent

 >



# The following two are equivalent
# 2 ##
c = []
c = a[:] # c receives a copy of a, but they are independent


No, the both above are equivalent. Both just bind a name (b or c) to a 
list. This list is in both cases a shallow copy of a.





# 3 ##
d = []
d[:] = a # d receives a copy of a, but they are independent



This is a totally different beast. It modifies d in place, no 
rebinding a name. So whover had a refernce to d before, now has a 
changed object, 


I follow all of this up to here, the next sentence is giving me pause


whereas in the two cases above, the original lists aren't touched.


The original list 'a', isn't changed in any of these cases right? And
modifying b, c or d would not change 'a' either - or am I not 
understanding this correctly?


None of your operations changes a. But I talked about the lists you 
bound b and c to before. Those aren't changed as well - they simply are 
not pointed to anymore. In your example, that means the will be 
garbage-collected, in other scenarios, such as this, the stay:


a = []
foo = []
bar = foo
assert bar is foo
bar = a[:]
assert bar is not foo

Diez
--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of [:]

2009-11-20 Thread Ethan Furman

Dave Angel wrote:

Esmail wrote:

Esmail schrieb:


Could someone help confirm/clarify the semantics of the [:] operator
in Python?

a = range(51,55)

# 1 ##
b = a[:] # b receives a copy of a, but they are independent


Semi-aside, if I wanted to make local copy of a list sent to me as a
parameter, which of these is the most appropriate to use (I don't want
changes to change the original list sent).

(1) is most appropriate in that case.  In fact, unless there is other 
code mixed in  between, it's always the most appropriate.


Be aware: this is only a shallow copy -- if your copied list contains 
mutable objects that you mutate, those changes *will* show up in the 
original list.


Shallow copies are useful when, for example, you won't be mutating 
objects in the copied list but are adding, removing, or reordering the 
copied list.


Hope this helps.

~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


Re: python bijection

2009-11-20 Thread Joshua Bronson
On Nov 20, 3:09 pm, Terry Reedy  wrote:
> Joshua Bronson wrote:
> > Anyone have any other feedback? For instance, is offering the __call__
> > syntax for the inverse mapping wonderful or terrible, or maybe both?
>
> Terrible ;-)
>
> Use standard subscripting with slices, and only that, to both get and set.
>
> Let m[4] == m[4:] == 'abc' # m[4:] is suggested alternative addition
> Then m[:'abc'] == 4
>
> m[4:] passes slice(4,None,None) to __getitem__
> m[:'abc'] passes slice(None,'abc',None)
>
> It just happens that dict items and slices use the same notation, but
> they do, so take advantage of that. In fact, to emphasize the symmetry
> of the bijective map, consider disallowing m[key] as ambiguous and
> require m[key:], along with m[:key] to access and set.
>
> Note that m[slice(1,2,3):] passes slice(slice(1, 2, 3), None, None), so
> this approach does not even prevent using slices as keys/values.
>
> In __setitem__, m[a:b] which passes slice(a,b,None) would have to be an
> error. In __getitem__, it could either be a error or return True/False
> depending on whether the pair is in the map. But this depends on whether
> __contains__ only tests keys or is modified to test pairs.
>
> Terry Jan Reedy

absolutely genius. implemented in the latest version:
http://bitbucket.org/jab/toys/src/tip/bijection.py

thank you for the terrific idea!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python simply not scaleable enough for google?

2009-11-20 Thread Aahz
In article ,
sturlamolden   wrote:
>
>Also note that Windows has native support for "taskelets", regardless
>of language. They are called "fibers" (as opposed to "threads") and are
>created using the CreateFiber system call. I would not be surprised if
>Unix'es has this as well. We do not need Stackless for light-weight
>threads. We can just take Python's threading modules' C code and
>replace CreateThread with CreateFiber.

Are you advocating a high-fiber diet?
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it."  --Brian W. Kernighan
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Gray Hat Python: Python Programming for Hackers Soft copy

2009-11-20 Thread Matt Mitchell
You mean like:  http://nostarch.com/ghpython.htm

 

From: python-list-bounces+mmitchell=transparent@python.org
[mailto:python-list-bounces+mmitchell=transparent@python.org] On
Behalf Of Elf Scripter
Sent: Friday, November 20, 2009 3:31 PM
To: python-list@python.org
Subject: Gray Hat Python: Python Programming for Hackers Soft copy

 

Hi

i`m looking for a place to get a soft copy of 'Gray Hat Python: Python
Programming for Hackers' may be a pdf or chm format.

 

Thank you


 
---
The information contained in this electronic message and any attached 
document(s) is intended only for the personal and confidential use of the 
designated recipients named above. This message may be confidential. If the 
reader of this message is not the intended recipient, you are hereby notified 
that you have received this document in error, and that any review, 
dissemination, distribution, or copying of this message is strictly prohibited. 
If you have received this communication in error, please notify sender 
immediately by telephone (603) 262-6300 or by electronic mail immediately. 
Thank you.
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of [:]

2009-11-20 Thread Esmail

Thank you all for your posts - this was very enlightening
and I printed this thread off for future reference.


--
http://mail.python.org/mailman/listinfo/python-list


Re: Is an interactive command a block?

2009-11-20 Thread Ethan Furman

Steven D'Aprano wrote:

On Fri, 20 Nov 2009 08:02:38 -0800, Ethan Furman wrote:


module scope == global scope

That is, there is nothing higher than module scope.  (So, yes, global is
a slight misnomer... in Python it means 'global to a module'.)


Actually there is: built-ins.

That's why you can access (e.g.) len without having to define it, or 
import it, first. And what's more, you can do this:



len([1,2,3])


3


def len(x):  # Shadow the built-in

... return -3
...

len([1,2,3])

-3


del len
len([1,2,3])  # It's back!


3


Very good point, I had forgotten.  Of course, injecting into built-ins 
is not recommended.


~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


Re: Relative versus absolute paths on Windows

2009-11-20 Thread Ethan Furman

Jason R. Coombs wrote:

The current implementation of Python (2.6.4, 3.1.1) treats \bar as a
relative path but reports it as an absolute path.



ntpath.isabs('\\bar')


True


ntpath.abspath('\\bar')


'C:\\bar'


os.chdir('d:\\')
ntpath.abspath('\\bar')


'd:\\bar'


os.chdir('server\\share')
ntpath.abspath('\\bar')


'server\\share\\bar'

In other words, paths without a drive letter are reported as absolute,
but treated as relative, except in a few special cases.



ntpath.join('d:\\foo', '\\bar')


'\\bar'

In this case, \bar is treated as absolute, and not relative to d:\foo.


It is often said on this list that 'Python is not Java'.  It is also 
true that 'Windows is not Unix'.


Unlike the *nix world where there is a *single* root, and everything 
else is relative to that, in the Windows world there are several roots 
-- every drive has one!


So \bar is both an absolute path on whichever drive is active (or 
specified), as well as relative if you happen to have more than one 
drive, but it will not ever be relative on any specific drive.  If you 
want 'bar' to be relative to the current directory, do not specify a 
leading '\'.


[snip]


Ultimately, I don't care what the definition is. It seems to me,
however, that Python should have a function that can resolve one path
name relative to another, but due to these limitations, it does not. I
should point out that os.path.relpath is not the solution either as
the first parameter is always treated as relative to the current
directory (more info at http://bugs.python.org/issue7195).


Please note that there *is not* a relative path that can take you from 
c:\foo to d:\bar  (see the point about multiple roots above).



I've built workarounds in 
https://svn.jaraco.com/jaraco/python/jaraco.windows/jaraco/windows/filesystem.py
as join() and resolve_path(). I'm happy to continue using these
workarounds, but I wanted to bring this issue to the attention of the
community for any comments or suggestions or answers to the following
questions.

What is the benefit of treating \path as absolute?


That was not Python's decision, but Windows'.


Should Python have built-in support for resolving one path relative to
another (such as is jaraco.windows.filesystem.resolve_path does)?


As I noted above, you are not returning a relative path when the paths 
cross drive letter boundaries, or one of the paths specified is a 
drive-absolute path (such as '\bar').



Given the long established behavior of Python and other platforms for
handling absolute paths in Windows, is there a way forward that
handles these cases more elegantly, or is the best approach to just
mumble something nasty under our breath and work around these issues
on a case-by-case basis?


I just go with the mumbling, myself.

Hope this helps.

~Ethan~

P.S.
And now that I look up the comments in the bug-tracker, I see this was 
all already pointed out to you.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread Steven D'Aprano
On Fri, 20 Nov 2009 03:08:01 -0800, DreiJane wrote:

> NB: I wondered about about dict(one=1, two=2) - why not d = {one:1,
> two:2} ? 

Because it doesn't work unless you have defined names one and two.

dict(one=1, two=2) uses keyword arguments, namely one and two. This is 
the same standard mechanism by which you call functions with keyword 
arguments:

myfunc(widget=x, width=5, name='fred', flag=True)


The dict literal syntax requires names one and two to already exist, 
otherwise you have to quote them to make them strings:

d = {'one': 1, 'two': 2}

> Since you do not write L=list((1, 2)) either. 

But you certainly can. It would be wasteful, since first it constructs a 
tuple (1, 2), then it creates a list from that tuple.


> These composed
> objects as basic building blocks make Python code so dense and
> beautiful, thus using "{}" means embracing the language's concept.

I don't understand this sentence.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is an interactive command a block?

2009-11-20 Thread Steven D'Aprano
On Fri, 20 Nov 2009 08:02:38 -0800, Ethan Furman wrote:

>> Thanks, but hey, contradiction: you mention globals as an "enclosing"
>> scope, i.e. that a module can have a scope, while stating that only
>> classes and functions have their own scope (so, would a module have
>> it's not own scope?).
> 
> module scope == global scope
> 
> That is, there is nothing higher than module scope.  (So, yes, global is
> a slight misnomer... in Python it means 'global to a module'.)

Actually there is: built-ins.

That's why you can access (e.g.) len without having to define it, or 
import it, first. And what's more, you can do this:


>>> len([1,2,3])
3
>>> def len(x):  # Shadow the built-in
... return -3
...
>>> len([1,2,3])
-3
>>> del len
>>> len([1,2,3])  # It's back!
3



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: No duplicate variable

2009-11-20 Thread Terry Reedy

King wrote:

class A(object):
def __init__(self, value=0.):
self.value = value

class B(A):
def __init__(self, value=None):
A.__init__(self)
self.value = value

obj = B()

When "B" initializes, it overwrite "value" variable of "A". How do I
make sure that no variable should not be defined with names of "A" in
"B"?


By knowing the public interface of A before you inherit from it, so you 
do not do that.


--
http://mail.python.org/mailman/listinfo/python-list


Gray Hat Python: Python Programming for Hackers Soft copy

2009-11-20 Thread Elf Scripter
Hi
i`m looking for a place to get a soft copy of 'Gray Hat Python: Python
Programming for Hackers' may be a pdf or chm format.

Thank you
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Split class across multiple files

2009-11-20 Thread Terry Reedy

eric.frederich wrote:

I have a class which holds a connection to a server and a bunch of
services.
In this class I have methods that need to work with that connection
and services.

Right now there are about 50 methods some of which can be quite long.

From an organizational standpoint, I'd like to have method

implementations in their own files.

Is this possible?


A method is just function that is a class attribute.


from somemethod import somemethod # has def somemethod

class Huge:
  somemethod = somemethod

# or

class Huge:
  defs
  ...

Huge.somemethod = somemethod


 It is recommended?  Should I just stop worrying
about it and have a 5,000 line class?


I suspect some refactoring would be useful.

--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of [:]

2009-11-20 Thread Duncan Booth
Dave Angel  wrote:

> 
> 
> Esmail wrote:
>> Diez B. 
>> Roggisch wrote:
>>> Esmail schrieb:
 Could someone help confirm/clarify the semantics of the [:] operator
 in Python?

 a = range(51,55)

 # 1 ##
 b = a[:] # b receives a copy of a, but they are independent
>>>  >

 
>> 
> (1) is most appropriate in that case.  In fact, unless there is other 
> code mixed in  between, it's always the most appropriate.

An alternative view is that it is most appropriate to do:

b = list(a)

as that accepts any type of sequence for a and you don't have to remember 
obscure punctuation to know what it does. The exception of course is when 
(a) 'a' is sliceable and (b) you need to 'b' to be of the same type as 'a'.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python bijection

2009-11-20 Thread Terry Reedy

Joshua Bronson wrote:


Anyone have any other feedback? For instance, is offering the __call__
syntax for the inverse mapping wonderful or terrible, or maybe both?


Terrible ;-)

Use standard subscripting with slices, and only that, to both get and set.

Let m[4] == m[4:] == 'abc' # m[4:] is suggested alternative addition
Then m[:'abc'] == 4

m[4:] passes slice(4,None,None) to __getitem__
m[:'abc'] passes slice(None,'abc',None)

It just happens that dict items and slices use the same notation, but 
they do, so take advantage of that. In fact, to emphasize the symmetry 
of the bijective map, consider disallowing m[key] as ambiguous and 
require m[key:], along with m[:key] to access and set.


Note that m[slice(1,2,3):] passes slice(slice(1, 2, 3), None, None), so 
this approach does not even prevent using slices as keys/values.


In __setitem__, m[a:b] which passes slice(a,b,None) would have to be an 
error. In __getitem__, it could either be a error or return True/False 
depending on whether the pair is in the map. But this depends on whether 
__contains__ only tests keys or is modified to test pairs.


Terry Jan Reedy


--
http://mail.python.org/mailman/listinfo/python-list


Re: sort values from dictionary of dictionaries python 2.4

2009-11-20 Thread J Wolfe
On Nov 9, 2:27 pm, Peter Otten <__pete...@web.de> wrote:
> J Wolfe wrote:
> > I would like to sort this dictionary by the values of the inner
> > dictionary ‘ob’ key.
>
> Python's built-in dictionary is unsorted by design.
>
>
>
> > mydict =
> > {’WILW1′: {’fx’: ‘8.1′, ‘obtime’: ‘2009-11-07 06:45:00′, ‘ob’: ‘6.9′},
> > ‘GRRW1′: {’fx’: ‘12.8′, ‘obtime’: ‘2009-11-07 04:15:00′, ‘ob’: ‘6.7′},
> > ‘NASW1′: {’fx’: ‘6.8′, ‘obtime’: ‘2009-11-07 06:30:00′, ‘ob’: ‘7.1′}
> > }
>
> > In this case, this would become:
>
> > mysorteddic =
> > {’NASW1′: {’fx’: ‘6.8′, ‘obtime’: ‘2009-11-07 06:30:00′, ‘ob’: ‘7.1′},
> > ‘WILW1′: {’fx’: ‘8.1′, ‘obtime’: ‘2009-11-07 06:45:00′, ‘ob’: ‘6.9′},
> > ‘GRRW1′: {’fx’: ‘12.8′, ‘obtime’: ‘2009-11-07 04:15:00′, ‘ob’: ‘6.7′}
> > }
>
> > I have had no luck in trying to figure this out. I am restricted to
> > using python 2.4.3.
>
> > Any help would be appreciated!
>
> You may be able to work around that limitation by putting the dict items
> into a list and sort that:
>
> >>> for item in sorted(mydict.items(), key=lambda (k, v): float(v["ob"]),
>
> reverse=True):
> ...     print item
> ...
> ('NASW1', {'fx': '6.8', 'obtime': '2009-11-07 06:30:00', 'ob': '7.1'})
> ('WILW1', {'fx': '8.1', 'obtime': '2009-11-07 06:45:00', 'ob': '6.9'})
> ('GRRW1', {'fx': '12.8', 'obtime': '2009-11-07 04:15:00', 'ob': '6.7'})
>
> Peter

Thanks Peter, That worked! :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newsgroup for beginners

2009-11-20 Thread Ethan Furman

Aahz wrote:

In article ,
Grant Edwards   wrote:


You've really got to try pretty hard to create one.  But if you
want to, here's how to do it:

1) Start by complaining that your program doesn't work because
  of a bug in Python.

[...]



Post of the month!


I'll second that!  I really needed a good laugh.  Many thanks!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does turtle graphics have the wrong associations?

2009-11-20 Thread Terry Reedy

Robert Maas, http://tinyurl.com/uh3t wrote:

My proposed no-syntax
IDE *also* gets rid of the need to bother with any programming-language
syntax. I've been proposing it for years, but nobody has shown any
interest


What you describe below is similar to various systems that have been 
proposed and even implemented, including visual programming systems. And 
there has been some success with non-programmers. But for most people, 
it is simply easier to say/write what one means rather than point and 
click. Point-and-click writing reminds me of point-and-click speaking. 
Great for those who need it but a hindrance to those who do not.


This is not to say that traditional editors cannot be improved with 
better backup memory aids. Indeed, even IDLE recognizes function names 
and pops up a bar listing parameters.


Feel free to develop a Visual Python environment. I might even give it a 
try.



From: Steven D'Aprano 
I'm interested. No-syntax IDE? How is this even possible?


I guess you missed what I previously posted.


I did too and had the same question.


The basic idea is that
you start with test data, and you use menus to select appropriate
data-processing actions to perform on that data. For example, you
manually key in a file name containing test data, or copy and paste
that same file name. Then you select "open file by that name" or
"load all lines from file by that name" etc. from a menu. If you
just opened the file, you now have a stream of input, and you can
select to read one line or one s-expression or one character etc.
from that file. After loading the whole file or one unit of data,
you now have some *real* data to work from. For example, with a
line of input, you might break it into words.


Processing a single data file is a common programming task, but not the 
only general category. A specialized Visual Data Analysis with Python 
might be a better and more focused project.


When I was doing statistical data analysis on a daily basis for a 
living, I would have loved to have had a system that would read in the 
first line and let me define (and name) the fields by point and click.

(I usually worked with fixed-width, column-delimited fields.)
Instead, I had to write a format statement and some summary analysis 
code, run it, look at it for sanity, and decide if my format had been 
correct.


Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of [:]

2009-11-20 Thread Dave Angel



Esmail wrote:
Diez B. 
Roggisch wrote:

Esmail schrieb:

Could someone help confirm/clarify the semantics of the [:] operator
in Python?

a = range(51,55)

# 1 ##
b = a[:] # b receives a copy of a, but they are independent

 >






Semi-aside, if I wanted to make local copy of a list sent to me as a
parameter, which of these is the most appropriate to use (I don't want
changes to change the original list sent).

Thanks again.


(1) is most appropriate in that case.  In fact, unless there is other 
code mixed in  between, it's always the most appropriate.



DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of [:]

2009-11-20 Thread Stephen Hansen
>
> The original list 'a', isn't changed in any of these cases right? And
> modifying b, c or d would not change 'a' either - or am I not understanding
> this correctly?
>
> ## 1 ##
>
> creates a new list and copies all elements from a to b
>
>
Yes.


> ## 2 ##
>
> take an already existing list (empty) and copies all elements from a to
> it (no data is lost by c since c was empty to start out with)
>
>
No. The 'already existing list' is simply completely discarded. You're
creating it on one line, then destroying it. A brand new list is being
created and returned and bound to the same name as the already existing
list.


> ## 3 ##
>
> d is a list, and all of its contents (if it had any) would be
> filled with that of a .. but changes to a still are not reflected in d
> .. this is confusing...
>


Not exactly, if I understand you correctly. D is a list, but its contents
(if it had any) would be completely replaced. Any previous content would be
gone, and it would now contain a copy of a.


> Semi-aside, if I wanted to make local copy of a list sent to me as a
> parameter, which of these is the most appropriate to use (I don't want
> changes to change the original list sent).
>


my_copy = original[:]

--S
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Split class across multiple files

2009-11-20 Thread eric.frederich
Yeah... it does seem like a God Object.
My reasoning for putting them all into one big class is that the class
has references to all the required resources, connections, and
services.
The other option would be to have a simple object called Session which
did nothing but login and hold those references.
The problem then is that I'd have 50 methods all of which would need
to take that session object as an argument.
That is what I was trying to avoid, having a ton of methods all taking
the same first parameter.
In most cases there would only be one session.

Perhaps I could do a singleton approach and have each of these methods
implicitly use the single session object unless called with a session=
keyword argument.

How does this look?.


# Begin Session.py

class Session():

def __init__(self, host=None, username=None, password=None):
if host and username and password:
self.login(host, username, password)

def login(self, host, username, password):
self.host = host
self.username = username
self.password = password

self.connection = Something.login(host, username,
password)
self.someService= SomeService.getService(connection)
self.anotherService = AnotherService.getService(connection)

def logout(self):
return self.connection.logout()


defaultSession = Session()


# Begin MyLibrary.py

from session import defaultSession

def doSomethig(x, y, z, session=defaultSession):

return session.someService.doSomething(x, y, z)

def anotherFunction(x, y, session=defaultSession):
ret = 0
for item in session.anotherService.getStuff(x):
if session.someService.foo(item, y):
session.anotherService.bar(item, x)
ret += 1
return ret





On Nov 20, 11:31 am, "Diez B. Roggisch"  wrote:
> eric.frederich schrieb:
>
> > I have a class which holds a connection to a server and a bunch of
> > services.
> > In this class I have methods that need to work with that connection
> > and services.
>
> > Right now there are about 50 methods some of which can be quite long.
> > From an organizational standpoint, I'd like to have method
> > implementations in their own files.
>
> > Is this possible?  It is recommended?  Should I just stop worrying
> > about it and have a 5,000 line class?
>
> It is pretty easy to do, as python allows multiple inheritance. Just
> group together methods into a class, and create one that inherits from all.
>
> However, I'd say something that even splitted up is essentially one
> class with 5000 lines cries for a huge refactoring. Of course this
> depends on the use-case, but I for once don't have a single such beast.
> Looks like a god-object to me...
>
> http://en.wikipedia.org/wiki/God_object
>
> Diez

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of [:]

2009-11-20 Thread Luis Zarrabeitia
On Friday 20 November 2009 11:15:55 am Esmail wrote:
> Could someone help confirm/clarify the semantics of the [:] operator
> in Python?

In short, a[:], when "a" is a list, returns a slice ("sublist") of "a" that 
happens to contain all the elemnts of "a". In general, a[low:upper:step] will 
give you the sublist containing all the items from low to upper, with the 
specified step size. When you omit the three arguments, you get  the whole 
list.

When used on the left side, it means "replace the contents of this slice with 
the contents of this iterable". (The get slice and set slice behaviors are 
defined by the class of «a», so while this explanation applies to list, it 
may not apply, and will likely not apply, to other data structures).

Note that the sublist is always another list (even when you get the full 
slice). So, in your experiments:

> # 1 ##
> b = a[:] # b receives a copy of a, but they are independent

Indeed, a[:] would return a full slice, i.e, another list containing the same 
elements as «a»

> # The following two are equivalent
> # 2 ##
> c = []
> c = a[:] # c receives a copy of a, but they are independent
> # 3 ##
> d = []
> d[:] = a # d receives a copy of a, but they are independent

Not exactly, but the difference is not big in this particular circumstance.

In (2), you create an empty list, discard it immediately, create another list 
a[:] with the contents of «a», and assign it to «c».

In (3) you create an empty list, and then fill it with the elements of «a».

To see the difference, try:

e = []
g = e
e = a[:]
print g

and 

e = []
g = e
e[:] = a
print g

> ### 1 ### is the preferred, shorter way to do what ## 2 ## and ## 3 ##
> do.

Depends on what you want to achieve. Some times, you need to keep the list and 
change the contents. That would be (3). For instance, when updating 
the "dirnames" output of the iterator returned by os.walk . Some times, you 
need to discard de old list, and get a new one. (1) would be the way to go. 
Note that (2) and (1) are the same, in both you discard the old value, if it 
exists, but in (2) you are creating a new list only to discard it right 
away - you shouldn't do that.

> Am I correct with this?

I hope I could help.

-- 
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Too Many Values To Unpack

2009-11-20 Thread Carsten Haese
Victor Subervi wrote:
> Now what I don't understand about the above code, provided by Dennis Lee
> Bieber, is the "expand" statement.

It's not a statement. It's a function. The function is defined in your
code. Or rather, it's defined in the code that you copied from Dennis
Lee Bieber, apparently. Look at the function definition. It'll tell you
what the function does.

> Could
> someone please explain what this code does?

Maybe you should ask the person that wrote the code.

--
Carsten Haese
http://informixdb.sourceforge.net

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of [:]

2009-11-20 Thread Esmail

Diez B. Roggisch wrote:

Esmail schrieb:

Could someone help confirm/clarify the semantics of the [:] operator
in Python?

a = range(51,55)

# 1 ##
b = a[:] # b receives a copy of a, but they are independent

 >



# The following two are equivalent
# 2 ##
c = []
c = a[:] # c receives a copy of a, but they are independent


No, the both above are equivalent. Both just bind a name (b or c) to a 
list. This list is in both cases a shallow copy of a.





# 3 ##
d = []
d[:] = a # d receives a copy of a, but they are independent



This is a totally different beast. It modifies d in place, no rebinding 
a name. So whover had a refernce to d before, now has a changed object, 


I follow all of this up to here, the next sentence is giving me pause


whereas in the two cases above, the original lists aren't touched.


The original list 'a', isn't changed in any of these cases right? And
modifying b, c or d would not change 'a' either - or am I not 
understanding this correctly?


## 1 ##

creates a new list and copies all elements from a to b

## 2 ##

take an already existing list (empty) and copies all elements from a to
it (no data is lost by c since c was empty to start out with)

## 3 ##

d is a list, and all of its contents (if it had any) would be
filled with that of a .. but changes to a still are not reflected in d
.. this is confusing...



Semi-aside, if I wanted to make local copy of a list sent to me as a
parameter, which of these is the most appropriate to use (I don't want
changes to change the original list sent).

Thanks again.

--
http://mail.python.org/mailman/listinfo/python-list


Re: PyQt4 4.4.4 : a bug with highlightBlock ?

2009-11-20 Thread Snouffy
> You need to change the indexOf() calls to indexIn() calls on the QRegExp
> object:
>
>   index = expression.indexIn(text, index + length)

Thank you so much ! After looking a bit more I found out that when
using indexOf the command :
length = expression.matchedLength()
was the one causing a problem. Thus I also had to change the line :
index = text.indexOf(expression)
to :
index = expression.indexIn(text)

Now it works like a charm ! I'll test it at work where we have PyQt
4.3.3.

Thanks again !
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python simply not scaleable enough for google?

2009-11-20 Thread sturlamolden
On 20 Nov, 11:12, Robin Becker  wrote:

> Presumably that means they could potentially run in parallel on the 10 cpu
> machines of the future.
>
> I'm not so clear on whether the threadless tasklets will run on separate cpus.

You can make a user-space scheduler and run a 10 tasklets on a
threadpool. But there is a GIL in stackless as well.

Nobody wants 10 OS threads, not with Python, not with Go, not with
C.

Also note that Windows has native support for "taskelets", regardless
of language. They are called "fibers" (as opposed to "threads") and
are created using the CreateFiber system call. I would not be
surprised if Unix'es has this as well. We do not need Stackless for
light-weight threads. We can just take Python's threading modules' C
code and replace CreateThread with CreateFiber.












-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Will Not Send Email!!

2009-11-20 Thread Victor Subervi
On Fri, Nov 20, 2009 at 12:38 PM, Carsten Haese wrote:

> Thank you. This proves conclusively that there IS in fact a file called
> email.pyc (and/or email.py) in your directory next to testMail.py. It is
> this email.pyc that is being imported instead of the email.py from the
> Python library. Getting rid of both email.py and email.pyc (by renaming
> them, deleting them, or moving them somewhere else) will fix your problem.
>

Thank you! It did indeed fix that problem. Now I get this traceback:

 /var/www/html/globalsolutionsgroup.vi/mailSpreadsheet.py52
   session.sendmail(clientEmail, ourEmail1, header+msg)
53 #  session.sendmail(clientEmail, ourEmail2, header+msg)
54
55 mailSpreadsheet()
56
 *mailSpreadsheet* = 
/var/www/html/globalsolutionsgroup.vi/mailSpreadsheet.py in *mailSpreadsheet
*()47   order += 'TOTAL: $' + str(total)
48
   msg = 'Here is the order from %s:\n\n %s' % (string.replace(client,
'_', ' '), order)
49   session = smtplib.SMTP("localhost")
50   session.login(user, passwd) # only if it requires auth
51
   header = "Subject: %s \r\nContent-type: text/html;
charset=utf-8\r\n\r\n" % subject
 session *undefined*, *global* *smtplib* = , smtplib.*SMTP* = 
/usr/lib64/python2.4/smtplib.py in *__init__*(self=,
host='localhost', port=0, local_hostname=None)   242
 self.esmtp_features = {}
   243 if host:
   244 (code, msg) = self.connect(host, port)
   245 if code != 220:
   246 raise SMTPConnectError(code, msg)
 code *undefined*, msg *undefined*, *self* = , self.*
connect* = >, *host* =
'localhost', *port* = 0  /usr/lib64/python2.4/smtplib.py in
*connect*(self=, host='localhost', port=25)   305 if not self.sock:
   306 raise socket.error, msg
   307 (code, msg) = self.getreply()
   308 if self.debuglevel > 0: print>>stderr, "connect:", msg
   309 return (code, msg)
 code *undefined*, *msg* = 'getaddrinfo returns an empty list', *self* =
, self.*getreply* = >  /usr/lib64/python2.4/smtplib.py in
*getreply*(self=)   349 if line == '':
   350 self.close()
   351
 raise SMTPServerDisconnected("Connection unexpectedly closed")
   352
 if self.debuglevel > 0: print>>stderr, 'reply:', repr(line)
   353 resp.append(line[4:].strip())
 *global* *SMTPServerDisconnected* = *
SMTPServerDisconnected*: Connection unexpectedly closed
  args = ('Connection unexpectedly closed',)

Why did this connection close? How do I fix it?
TIA,
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of [:]

2009-11-20 Thread Dave Angel

Diez B. Roggisch wrote:
Esmail 
schrieb:

Could someone help confirm/clarify the semantics of the [:] operator
in Python?

a = range(51,55)

# 1 ##
b = a[:] # b receives a copy of a, but they are independent

>



# The following two are equivalent
# 2 ##
c = []
c = a[:] # c receives a copy of a, but they are independent


No, the both above are equivalent. Both just bind a name (b or c) to a 
list. This list is in both cases a shallow copy of a.





# 3 ##
d = []
d[:] = a # d receives a copy of a, but they are independent



This is a totally different beast. It modifies d in place, no 
rebinding a name. So whover had a refernce to d before, now has a 
changed object, whereas in the two cases above, the original lists 
aren't touched.


Of course, in your concrete example, the looks of it are the same. The 
distinction is crucial in larger contexts.


Diez


While Diez is correct, I think it could be less confusing.  So I'll try 
to put it clearer.  If it's not, sorry.


(2) binds an empty list to c, and immediately rebinds it to a new list, 
copied from a..  So the c=[]  line is totally irrelevant, unless there's 
some other code in between.


(3) again binds an empty list to d, but this is important, because the 
following line wouldn't be legal if there weren't already a list.  The 
only time this has a different practical effect from the other two is if 
there's some other code between the two lines.  If that other code binds 
something to the same object then it definitely matters.


I'd say (1) is the preferable form, as no visible object is in an 
in-between state.  The copy is built anonymously, then bound only when 
it has its full value.


DaveA

--
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of [:]

2009-11-20 Thread Tim Chase

Diez B. Roggisch wrote:

Esmail schrieb:

Could someone help confirm/clarify the semantics of the [:] operator
in Python?

a = range(51,55)

# 1 ##
b = a[:] # b receives a copy of a, but they are independent

 >


# The following two are equivalent
# 2 ##
c = []
c = a[:] # c receives a copy of a, but they are independent


No, the both above are equivalent. Both just bind a name (b or c) to a 
list. This list is in both cases a shallow copy of a.




# 3 ##
d = []
d[:] = a # d receives a copy of a, but they are independent



This is a totally different beast. It modifies d in place, no rebinding 
a name. So whover had a refernce to d before, now has a changed object, 
whereas in the two cases above, the original lists aren't touched.


To demonstrate what Diez is saying:

  a = range(51,55)
  d = []
  x = d
  d[:] = a
  print x  #hey, I didn't change x...oh, yeah

-tkc









--
http://mail.python.org/mailman/listinfo/python-list


Re: Too Many Values To Unpack

2009-11-20 Thread Victor Subervi
On Fri, Nov 20, 2009 at 12:14 PM, Shashank Singh <
shashank.sunny.si...@gmail.com> wrote:

> Are you sure your key values are 2-tuples in levelDict?
>

No. Here's some tweaked code:

cursor.execute('select category from categories order by category')
theTree = expand(cursor.fetchall())
print theTree

Now, categories only has one category called 'cat1'. Here's what it prints:

{'cat1': {}}

Now what I don't understand about the above code, provided by Dennis Lee
Bieber, is the "expand" statement. Nor could I find any documentation
googling either "expand python" or "expand fetchall python". Could someone
please explain what this code does?
TIA,
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Will Not Send Email!!

2009-11-20 Thread Kev Dwyer
On Fri, 20 Nov 2009 11:58:00 -0400, Victor Subervi wrote:

Hello Victor,

Carsten's well-specified instruction has identified your problem.

>[r...@13gems globalsolutionsgroup.vi]# python -c "import email; print 
>email"
>

There is a file named email.pyc, most likely in your current directory, 
that is masking the email package in the standard library.  Remove/rename 
email.pyc and email.py, if it exists.

Cheers,

Kevin

-- 
http://mail.python.org/mailman/listinfo/python-list


Newbie question about scrapy tutorial

2009-11-20 Thread Zeynel
Hello,

I have a question about scrapy tutorial and I put 2 questions in the
scrapy group

http://groups.google.com/group/scrapy-users/t/d5afae7d88672e02
http://groups.google.com/group/scrapy-users/t/4d52fa8c589a412a

but there were no answers. Can anyone here help me? The spider works
fine but I couldn't figure out where the items file is so that I can
print it to a file to transfer it to my database.

The same question in stackoverflow
http://stackoverflow.com/questions/1771151/newbie-q-about-scrapy-pipeline-py

Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: No duplicate variable

2009-11-20 Thread Diez B. Roggisch

King schrieb:

class A(object):
def __init__(self, value=0.):
self.value = value

class B(A):
def __init__(self, value=None):
A.__init__(self)
self.value = value

obj = B()

When "B" initializes, it overwrite "value" variable of "A". How do I
make sure that no variable should not be defined with names of "A" in
"B"?


To avoid these kinds of clashes, you can use the double-undescore 
syntax. It will create a mangled name that looks like


_module_class_variablename


By this, the two different variable get distinct names.

*However*, this is neither a proper way of making variables private, nor 
 are name-clashes something that should arise more than every now-and-then.


Instead, try rather renaming value to something more meaningful.

Diez
--
http://mail.python.org/mailman/listinfo/python-list


No duplicate variable

2009-11-20 Thread King
class A(object):
def __init__(self, value=0.):
self.value = value

class B(A):
def __init__(self, value=None):
A.__init__(self)
self.value = value

obj = B()

When "B" initializes, it overwrite "value" variable of "A". How do I
make sure that no variable should not be defined with names of "A" in
"B"?

Prashant
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Split class across multiple files

2009-11-20 Thread Carl Banks
On Nov 20, 8:14 am, "eric.frederich"  wrote:
> I have a class which holds a connection to a server and a bunch of
> services.
> In this class I have methods that need to work with that connection
> and services.
>
> Right now there are about 50 methods some of which can be quite long.
> From an organizational standpoint, I'd like to have method
> implementations in their own files.
>
> Is this possible?

Yes, define all your different methods in separate classes in separate
files.  Then subclasses all of those separate classes into one big
class.


> It is recommended?

Debateable.  Personally, I have no problem using inheritance for
purely mechanical reasons.  Others might.  (In my case, it was a
separation into general and customized behavior.  I wanted to define
generic behavior in one package, and specialized behavior in another,
but there was no internal logical difference between specialized and
generic methods.)  However


> Should I just stop worrying
> about it and have a 5,000 line class?

Five thousand lines is pushing the comfort level of how big a class
ought to be.  I suggest instead of worrying about how to mechanically
split off methods into different files, you should consider whether
there's a way to refactor that one big class into smaller ones.  Then
the problem you have goes away.


Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Will Not Send Email!!

2009-11-20 Thread Carsten Haese
Victor Subervi wrote:
> On Fri, Nov 20, 2009 at 11:14 AM, Carsten Haese  > wrote: 
> Please show us a copy-and-paste of your command line window contents
> that result from executing <> and then executing
> <> immediately thereafter.
> 
> 
> [r...@13gems globalsolutionsgroup.vi ]#
> python testMail.py
> Traceback (most recent call last):
>   File "testMail.py", line 2, in ?
> import smtplib
>   File "/usr/lib64/python2.4/smtplib.py", line 49, in ?
> from email.base64MIME import encode as encode_base64
> ImportError: No module named base64MIME
> [r...@13gems globalsolutionsgroup.vi ]#
> python -c "import email; print email"
> 

Thank you. This proves conclusively that there IS in fact a file called
email.pyc (and/or email.py) in your directory next to testMail.py. It is
this email.pyc that is being imported instead of the email.py from the
Python library. Getting rid of both email.py and email.pyc (by renaming
them, deleting them, or moving them somewhere else) will fix your problem.

--
Carsten Haese
http://informixdb.sourceforge.net

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Split class across multiple files

2009-11-20 Thread Diez B. Roggisch

eric.frederich schrieb:

I have a class which holds a connection to a server and a bunch of
services.
In this class I have methods that need to work with that connection
and services.

Right now there are about 50 methods some of which can be quite long.
From an organizational standpoint, I'd like to have method
implementations in their own files.

Is this possible?  It is recommended?  Should I just stop worrying
about it and have a 5,000 line class?


It is pretty easy to do, as python allows multiple inheritance. Just 
group together methods into a class, and create one that inherits from all.


However, I'd say something that even splitted up is essentially one 
class with 5000 lines cries for a huge refactoring. Of course this 
depends on the use-case, but I for once don't have a single such beast. 
Looks like a god-object to me...


http://en.wikipedia.org/wiki/God_object

Diez
--
http://mail.python.org/mailman/listinfo/python-list


Re: Too Many Values To Unpack

2009-11-20 Thread Carsten Haese
Victor Subervi wrote:
> Hi;
> At one point Dennis Lee Bieber helped me with the following slightly
> modified code:
>
> [snippage...]
>
> def getChildren(levelDict, level = 0):
>   MAXLEVEL = 7
>   if level > MAXLEVEL:
> return  #possibly the data has a cycle/loop
>   for (nm, dt) in levelDict:
> cursor.execute('''select c.name  from categories as c
>   inner join relationship as r
>   on c.ID = r.Child
>   inner join categories as p
>   on r.Parent = p.ID
>   where p.category = %s
>   order by c.name ''', (nm,))
> levelDict[nm] = expand(cursor.fetchall())
> # recursive call to do next level
> getChildren(levelDict[nm], level + 1)
>   # no data return as we are mutating dictionaries in place
>
> [snippage...]
>
> [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] for (nm,
> dt) in levelDict:
> [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] ValueError:
> too many values to unpack
Iterating over a dictionary, such as what you're doing in the line <>, only produces the keys that are in the
dictionary. This means that this line only works if the keys in
levelDict are pairs of "nm"s and "dt"s, whatever "nm" and "dt"
represent. (In case the foregoing is too subtle a clue: Choose better
variable names.)

However, the line <> indicates that the
keys in levelDict are only "nm"s, which are presumably single objects,
not pairs. Also, the "dt" that you're trying to unpack from levelDict's
keys is not used anywhere in your function.

So, this would indicate that changing the offending line to <> should fix this particular error.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: semantics of [:]

2009-11-20 Thread Diez B. Roggisch

Esmail schrieb:

Could someone help confirm/clarify the semantics of the [:] operator
in Python?

a = range(51,55)

# 1 ##
b = a[:] # b receives a copy of a, but they are independent

>



# The following two are equivalent
# 2 ##
c = []
c = a[:] # c receives a copy of a, but they are independent


No, the both above are equivalent. Both just bind a name (b or c) to a 
list. This list is in both cases a shallow copy of a.





# 3 ##
d = []
d[:] = a # d receives a copy of a, but they are independent



This is a totally different beast. It modifies d in place, no rebinding 
a name. So whover had a refernce to d before, now has a changed object, 
whereas in the two cases above, the original lists aren't touched.


Of course, in your concrete example, the looks of it are the same. The 
distinction is crucial in larger contexts.


Diez
--
http://mail.python.org/mailman/listinfo/python-list


semantics of [:]

2009-11-20 Thread Esmail

Could someone help confirm/clarify the semantics of the [:] operator
in Python?

a = range(51,55)

# 1 ##
b = a[:] # b receives a copy of a, but they are independent


# The following two are equivalent
# 2 ##
c = []
c = a[:] # c receives a copy of a, but they are independent


# 3 ##
d = []
d[:] = a # d receives a copy of a, but they are independent


### 1 ### is the preferred, shorter way to do what ## 2 ## and ## 3 ##
do.

Am I correct with this?

Thanks.

--
http://mail.python.org/mailman/listinfo/python-list


Serve Pages Under Different Ownership

2009-11-20 Thread Victor Subervi
Hi;
I'm building a new server after many years hiatus. I currently can only
serve python pages chown'd to root. How do I change this?
TIA,
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Too Many Values To Unpack

2009-11-20 Thread Shashank Singh
On Fri, Nov 20, 2009 at 9:15 PM, Victor Subervi wrote:

> Hi;
> At one point Dennis Lee Bieber helped me with the following slightly
> modified code:
>
> #!/usr/bin/python
>
> import sys,os
> sys.path.append(os.getcwd())
> import MySQLdb
> from login import login
> import re, string
>
> def printTree(aTree, level=0):
>   tree = []
>   for name in sorted(aTree.keys()):
> tree.append("%s%s") % ("\t" * level, name)
> printTree(aTree[name], level + 1)
>
> def expand(fetched):
>   aDict = {}
>   for (name, ) in fetched:
> aDict[name] = {}
>   return aDict
>

def getChildren(levelDict, level = 0):
>   MAXLEVEL = 7
>   if level > MAXLEVEL:
> return  #possibly the data has a cycle/loop
>   for (nm, dt) in levelDict:
>

Are you sure your key values are 2-tuples in levelDict?
For-each on dicts enumerates the keys AFAIK

-- 
Regards
Shashank Singh
Senior Undergraduate, Department of Computer Science and Engineering
Indian Institute of Technology Bombay
shashank.sunny.si...@gmail.com
http://www.cse.iitb.ac.in/~shashanksingh
-- 
http://mail.python.org/mailman/listinfo/python-list


Split class across multiple files

2009-11-20 Thread eric.frederich
I have a class which holds a connection to a server and a bunch of
services.
In this class I have methods that need to work with that connection
and services.

Right now there are about 50 methods some of which can be quite long.
>From an organizational standpoint, I'd like to have method
implementations in their own files.

Is this possible?  It is recommended?  Should I just stop worrying
about it and have a 5,000 line class?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python gui builders

2009-11-20 Thread sturlamolden
On 18 Nov, 22:18, David Bolen  wrote:

> With that said, for various reasons I still prefer wxPython to Qt, and
> at the moment, find wxFormBuilder the best fit for my own designs
> (even before the direct Python support, just using XRC).

Personally I prefer wxFormBuilder over QtDesigner for sizer-based
designs.

For quickly creating mock-up designs (GUI or web) there is a Windows
program called "DesignerVista".

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regexp and multiple groups (with repeats)

2009-11-20 Thread Mark Tolonen


"mk"  wrote in message news:he60ha$iv...@ger.gmane.org...

Hello,

>>> r=re.compile(r'(?:[a-zA-Z]:)([\\/]\w+)+')

>>> r.search(r'c:/tmp/spam/eggs').groups()
('/eggs',)

Obviously, I would like to capture all groups:
('/tmp', '/spam', '/eggs')

But it seems that re captures only the last group. Is there any way to 
capture all groups with repeat following it, i.e. (...)+ or (...)* ?


Even better would be:

('tmp', 'spam', 'eggs')

Yes, I know about re.split:

>>> re.split( r'(?:\w:)?[/\\]', r'c:/tmp/spam\\eggs/' )
['', 'tmp', 'spam', '', 'eggs', '']

My interest is more general in this case: how to capture many groups with 
a repeat?


re.findall is what you're looking for.  Here's all words not followed by a 
colon:



import re
re.findall(u'(\w+)(?!:)',r'c:\tmp\spam/eggs')

['tmp', 'spam', 'eggs']

-Mark


--
http://mail.python.org/mailman/listinfo/python-list


Re: Is an interactive command a block?

2009-11-20 Thread Ethan Furman

Alf P. Steinbach wrote:

* Benjamin Kaplan:

On Thu, Nov 19, 2009 at 4:42 PM, Alf P. Steinbach  wrote:

>>>
I feel that there's still something lacking in my understanding 
though, like
how/where the "really actually just pure local not also global" is 
defined

for function definition, but it's now just a vague feeling of something
missing, not a feeling of direct contradiction as I had when I believed
local != global.



It's because the only blocks in python that have their own scope are
classes and functions. For loops don't have their own scope- they use
the enclosing one, which in this case is globals.



Thanks, but hey, contradiction: you mention globals as an "enclosing" 
scope, i.e. that a module can have a scope, while stating that only 
classes and functions have their own scope (so, would a module have it's 
not own scope?).


module scope == global scope

That is, there is nothing higher than module scope.  (So, yes, global is 
a slight misnomer... in Python it means 'global to a module'.)


~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python Will Not Send Email!!

2009-11-20 Thread Victor Subervi
On Fri, Nov 20, 2009 at 11:14 AM, Carsten Haese wrote:

> Kevin neglected to mention that the new interpreter session must be
> started in the same directory as the one you're in when you run your
> testMail.py script. Since he didn't mention that, we can't be sure that
> that's what you did, so this experiment doesn't prove anything.
>
> Please show us a copy-and-paste of your command line window contents
> that result from executing <> and then executing
> <> immediately thereafter.
>

[r...@13gems globalsolutionsgroup.vi]# python testMail.py
Traceback (most recent call last):
  File "testMail.py", line 2, in ?
import smtplib
  File "/usr/lib64/python2.4/smtplib.py", line 49, in ?
from email.base64MIME import encode as encode_base64
ImportError: No module named base64MIME
[r...@13gems globalsolutionsgroup.vi]# python -c "import email; print email"

TIA,
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regexp and multiple groups (with repeats)

2009-11-20 Thread Neil Cerutti
On 2009-11-20, mk  wrote:
> Hello,
>
> >>> r=re.compile(r'(?:[a-zA-Z]:)([\\/]\w+)+')
>
> >>> r.search(r'c:/tmp/spam/eggs').groups()
> ('/eggs',)
>
> Obviously, I would like to capture all groups:
> ('/tmp', '/spam', '/eggs')

You'll have to do something else, for example:

>>> s = re.compile(r'(?:[a-zA-Z]:)')
>>> n = re.compile(r'[\\/]\w+')
>>> m = s.match('c:/tmp/spam/eggs')
>>> n.findall(m.string[m.end():])
['/tmp', '/spam', '/eggs']

-- 
Neil Cerutti
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Will Not Send Email!!

2009-11-20 Thread Victor Subervi
On Fri, Nov 20, 2009 at 11:05 AM, Kev Dwyer  wrote:

> I ran your script on a CentOS vm (5.2 server 32bit, not quite the same as
> yours but also running python 2.4.3).  It ran without error.  So I
> suspect that either you have a rogue email module/package on your machine
> or there's something wrong with the python install.
>
> You could try:
>
> import email
> email.__version__
>
> My interpreter responds "3.0.1"  If you get a different response that
> suggests a dodgy module somewhere - try email.__file__ and see where it's
> located (my interpreter returns /usr/lib/python2.4/email/__init__.pyc).
>
> If the version number is "3.0.1" on your machine then I would check the
> contents of /usr/lib64/python2.4/email/.  Perhaps the base64MIME module
> is missing.
>

>>> import email
>>> email.__version__
'3.0.1'
>>>
[r...@13gems cart]# ls /usr/lib64/python2.4/email/
base64MIME.py   Encoders.pyoGenerator.pyc  Iterators.py
MIMEAudio.pyo   MIMEMessage.pyc   MIMEText.py Parser.pyo
base64MIME.pyc  Errors.py   Generator.pyo  Iterators.pyc
MIMEBase.py MIMEMessage.pyo   MIMEText.pycquopriMIME.py
base64MIME.pyo  Errors.pyc  Header.py  Iterators.pyo
MIMEBase.pycMIMEMultipart.py  MIMEText.pyoquopriMIME.pyc
Charset.py  Errors.pyo  Header.pyc Message.py
MIMEBase.pyoMIMEMultipart.pyc _parseaddr.py   quopriMIME.pyo
Charset.pyc FeedParser.py   Header.pyo Message.pyc
MIMEImage.pyMIMEMultipart.pyo _parseaddr.pyc  test
Charset.pyo FeedParser.pyc  __init__.pyMessage.pyo
MIMEImage.pyc   MIMENonMultipart.py   _parseaddr.pyo  Utils.py
Encoders.py FeedParser.pyo  __init__.pyc   MIMEAudio.py
MIMEImage.pyo   MIMENonMultipart.pyc  Parser.py   Utils.pyc
Encoders.pycGenerator.py__init__.pyo   MIMEAudio.pyc
MIMEMessage.py  MIMENonMultipart.pyo  Parser.pyc  Utils.pyo

Any other ideas?
TIA,
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Too Many Values To Unpack

2009-11-20 Thread Victor Subervi
Hi;
At one point Dennis Lee Bieber helped me with the following slightly
modified code:

#!/usr/bin/python

import sys,os
sys.path.append(os.getcwd())
import MySQLdb
from login import login
import re, string

def printTree(aTree, level=0):
  tree = []
  for name in sorted(aTree.keys()):
tree.append("%s%s") % ("\t" * level, name)
printTree(aTree[name], level + 1)

def expand(fetched):
  aDict = {}
  for (name, ) in fetched:
aDict[name] = {}
  return aDict

def getChildren(levelDict, level = 0):
  MAXLEVEL = 7
  if level > MAXLEVEL:
return  #possibly the data has a cycle/loop
  for (nm, dt) in levelDict:
cursor.execute('''select c.name from categories as c
  inner join relationship as r
  on c.ID = r.Child
  inner join categories as p
  on r.Parent = p.ID
  where p.category = %s
  order by c.name''', (nm,))
levelDict[nm] = expand(cursor.fetchall())
# recursive call to do next level
getChildren(levelDict[nm], level + 1)
  # no data return as we are mutating dictionaries in place

def catTree():
  user, passwd, db, host = login()
  database = MySQLdb.connect(host, user, passwd, db)
  cursor = database.cursor()
  cursor.execute('''create table if not exists categories
 (ID int(3) unsigned primary key,
 Category varchar(40),
 Parent varchar(40))''')
  cursor.execute('select Category, Parent from categories;')
  data = cursor.fetchall()
  cursor.execute('select Category from categories order by Parent, ID')
  print data
  Categories = [itm[0] for itm in cursor] #untuple single column
  if len(Categories) > 0:
cursor.execute('select Parent from categories order by Parent, ID')
Parents = [itm[0] for itm in cursor]
MAXLEVEL = 15
cursor.execute('''create table if not exists categories
  (ID integer auto_increment primary key,
  Name varchar(40) not null,
  unique (Name)
  )''')
cursor.execute('''create table if not exists Relationship
  (ID integer auto_increment primary key,
  Parent integer not null,
  foreign key (Parent) references categories (ID),
  Child integer not null,
  foreign key (Child) references categories (ID),
  check (Parent <> Child) );''')
# get top level
print 'ok'
cursor.execute('select category from categories order by category')
theTree = expand(cursor.fetchall())
getChildren(theTree)
connection.commit()
return printTree(theTree)
  else:
return ['There are no categories yet.']

catTree()

This throws the error:

[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] Traceback (most
recent call last):
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58]   File
"/var/www/html/angrynates.com/cart/createCats.py", line 8, in ?
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] from catTree
import catTree
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58]   File
"/var/www/html/angrynates.com/cart/catTree.py", line 77, in ?
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] catTree()
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58]   File
"/var/www/html/angrynates.com/cart/catTree.py", line 71, in catTree
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58]
getChildren(theTree)
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58]   File
"/var/www/html/angrynates.com/cart/catTree.py", line 25, in getChildren
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] for (nm, dt)
in levelDict:
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] ValueError: too
many values to unpack

There is only one category in "categories". Please advise.
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is an interactive command a block?

2009-11-20 Thread Alf P. Steinbach

* Benjamin Kaplan:

On Thu, Nov 19, 2009 at 4:42 PM, Alf P. Steinbach  wrote:

* Steven D'Aprano:

I feel that there's still something lacking in my understanding though, like
how/where the "really actually just pure local not also global" is defined
for function definition, but it's now just a vague feeling of something
missing, not a feeling of direct contradiction as I had when I believed
local != global.



It's because the only blocks in python that have their own scope are
classes and functions. For loops don't have their own scope- they use
the enclosing one, which in this case is globals.


Thanks, but hey, contradiction: you mention globals as an "enclosing" scope, 
i.e. that a module can have a scope, while stating that only classes and 
functions have their own scope (so, would a module have it's not own scope?).


I think, having now read up and down and sideways in the docs, that the proper 
term in Python is "namespace", and that "scope" in Python refers to where the 
names in a namespace are directly accessible, with a special case for classes, 
namely that the scope of a class' namespace doesn't penetrate down into function 
definitions, hence problem with comprehensions expressed directly in a class 
definition. I believe that's a so called language "wart"  --  which C++ is full 
of, just now discovering some of Python's :-). It's almost like C++'s "most 
vexing parse", a natural and apparently meaningful source code construct that 
due to the detailed rules turns out to be meaningless. I wish language designers 
could be a little less hooked on low level consistency. Because it makes for 
high level inconsistency, very difficult to explain without zillions of details.


But anyways, it's starting to make more sense, yes.


Cheers,

- Alf
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python Will Not Send Email!!

2009-11-20 Thread Carsten Haese
Victor Subervi wrote:
> On Thu, Nov 19, 2009 at 5:01 PM, Kev Dwyer  > wrote:
> 
> On Thu, 19 Nov 2009 11:28:37 -0400, Victor Subervi wrote:
> 
> Hello Victor,
> 
> There are some pages on the internet that suggest that this problem
> my be
> caused by a module named email.py (or email.pyc) in your pythonpath.  If
> you try import smtplib in the interpreter do you get this error message?
> If so, start a new interpreter session and try import email - is the
> email module imported from the stdlib?
> 
> 
> Both of these import just fine.

Kevin neglected to mention that the new interpreter session must be
started in the same directory as the one you're in when you run your
testMail.py script. Since he didn't mention that, we can't be sure that
that's what you did, so this experiment doesn't prove anything.

Please show us a copy-and-paste of your command line window contents
that result from executing <> and then executing
<> immediately thereafter.

--
Carsten Haese
http://informixdb.sourceforge.net

-- 
http://mail.python.org/mailman/listinfo/python-list


RE: checking 'type' programmatically

2009-11-20 Thread Billy Earney
Try looking at the function 'isinstance', so for example

if isinstance(obj, str):
print "object is a string.."
elif isinstance(obj, int):
print "object is an integer.." 

-Original Message-
From: python-list-bounces+billy.earney=gmail@python.org
[mailto:python-list-bounces+billy.earney=gmail@python.org] On Behalf Of
mk
Sent: Friday, November 20, 2009 4:10 AM
To: python-list@python.org
Subject: checking 'type' programmatically


Disclaimer: this is for exploring and debugging only. Really.

I can check type or __class__ in the interactive interpreter:

Python 2.6.2 (r262:71600, Jun 16 2009, 16:49:04)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import subprocess
 >>> 
p=subprocess.Popen(['/bin/ls'],stdout=subprocess.PIPE,stderr=subprocess.PIPE
)
 >>> p

 >>> (so, se) = p.communicate()
 >>> so
'abc.txt\nbak\nbox\nbuild\ndead.letter\nDesktop\nhrs\nmbox\nmmultbench\nmmul
tbench.c\npyinstaller\nscreenlog.0\nshutdown\ntaddm_import.log\nv2\nvm\nwork
space\n'
 >>> se
''
 >>> so.__class__

 >>> type(so)

 >>> type(se)


But when I do smth like this in code that is ran non-interactively (as 
normal program):

req.write('stderr type %s' % type(se))
req.write('stderr class %s' % str(se.__class__))

then I get empty output. WTF?

How do I get the type or __class__ into some object that I can display?


Why do that: e.g. if documentation is incomplete, e.g. documentation on 
Popen.communicate() says "communicate() returns a tuple (stdoutdata, 
stderrdata)" but doesn't say what is the class of stdoutdata and 
stderrdata (a file object to read? a string?).

Regards,
mk



-- 
http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Will Not Send Email!!

2009-11-20 Thread Kev Dwyer
On Fri, 20 Nov 2009 07:58:55 -0500, Victor Subervi wrote:

> On Thu, Nov 19, 2009 at 5:01 PM, Kev Dwyer 
> wrote:
> 
>> On Thu, 19 Nov 2009 11:28:37 -0400, Victor Subervi wrote:
>>
>> Hello Victor,
>>
>> There are some pages on the internet that suggest that this problem my
>> be caused by a module named email.py (or email.pyc) in your pythonpath.
>>  If you try import smtplib in the interpreter do you get this error
>> message? If so, start a new interpreter session and try import email -
>> is the email module imported from the stdlib?
>>
>>
> Both of these import just fine.
> 
> 
>> If these steps don't help, it might be useful if you can tell us which
>> Linux distribution you are using.
>>
>>
> Python 2.4.3
> [r...@13gems ~]# uname -a
> Linux 13gems.com.13gems.com 2.6.18-028stab064.8 #1 SMP Fri Nov 6
> 11:28:25 MSK 2009 x86_64 x86_64 x86_64 GNU/Linux CentOS 5.4 final
> TIA,
> V
> On Thu, Nov 19, 2009 at 5:01 PM, Kev Dwyer
> < href="mailto:kevin.p.dw...@gmail.com";>kevin.p.dw...@gmail.com>
> wrote: On Thu, 19 Nov 2009
> 11:28:37 -0400, Victor Subervi wrote: 
> Hello Victor,
> 
> There are some pages on the internet that suggest that this problem my
> be caused by a module named email.py (or email.pyc) in your
> pythonpath.  If you try import smtplib in the interpreter do you get
> this error message? If so, start a new interpreter session and try
> import email - is the email module imported from the
> stdlib?Both of these import just
> fine.  
> If these steps don't help, it might be useful if you can tell us
> which Linux distribution you are
> using.Python
> 2.4.3[r...@13gems ~]# uname -aLinux  href="http://13gems.com.13gems.com";>13gems.com.13gems.com
> 2.6.18-028stab064.8 #1 SMP Fri Nov 6 11:28:25 MSK 2009 x86_64 x86_64
> x86_64 GNU/Linux CentOS 5.4
> finalTIA,V

Hello Victor,

I ran your script on a CentOS vm (5.2 server 32bit, not quite the same as 
yours but also running python 2.4.3).  It ran without error.  So I 
suspect that either you have a rogue email module/package on your machine 
or there's something wrong with the python install.

You could try:

import email
email.__version__

My interpreter responds "3.0.1"  If you get a different response that 
suggests a dodgy module somewhere - try email.__file__ and see where it's 
located (my interpreter returns /usr/lib/python2.4/email/__init__.pyc).

If the version number is "3.0.1" on your machine then I would check the 
contents of /usr/lib64/python2.4/email/.  Perhaps the base64MIME module 
is missing.

Cheers,

Kev

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking 'type' programmatically

2009-11-20 Thread exarkun

On 10:10 am, mrk...@gmail.com wrote:


Disclaimer: this is for exploring and debugging only. Really.

I can check type or __class__ in the interactive interpreter:

Python 2.6.2 (r262:71600, Jun 16 2009, 16:49:04)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> 
p=subprocess.Popen(['/bin/ls'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)

>>> p

>>> (so, se) = p.communicate()
>>> so
'abc.txt\nbak\nbox\nbuild\ndead.letter\nDesktop\nhrs\nmbox\nmmultbench\nmmultbench.c\npyinstaller\nscreenlog.0\nshutdown\ntaddm_import.log\nv2\nvm\nworkspace\n'
>>> se
''
>>> so.__class__

>>> type(so)

>>> type(se)


But when I do smth like this in code that is ran non-interactively (as 
normal program):


req.write('stderr type %s' % type(se))
req.write('stderr class %s' % str(se.__class__))

then I get empty output. WTF?

How do I get the type or __class__ into some object that I can display?


Hooray for HTML.

You asked a browser to render "stderr type ".  This 
isn't valid HTML, so pretty much any behavior goes.  In this case, the 
browser seems to be discarding the entire  - not too 
suprising, as it has some features in common with an html tag.


Try properly quoting your output (perhaps by generating your html with a 
real html generation library).


Jean-Paul
--
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a Carriage Return in Unicode

2009-11-20 Thread sturlamolden
On 19 Nov, 01:14, Doug  wrote:

> Thanks for your help!!

A carriage return in unicode is

   u"\r"

how this is written as bytes is dependent on the encoder.

Don't try to outsmart the UTF-8 codec, it knows how to translate "\r"
to UTF-8.


Sturla Molden
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread Diez B. Roggisch

DreiJane schrieb:

NB: I wondered about about dict(one=1, two=2) - why not d = {one:1,
two:2} ? Since you do not write L=list((1, 2)) either. These composed


because it's not working.

>>> {one : 1}
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'one' is not defined


Yes, that looks nitpicky, but that is exactly the reason one often 
prefers the dict(...)-variant. Because it uses python keywords, it 
spares you to type quotes around all the keys. Which IMHO is more aesthetic.




objects as basic building blocks make Python code so dense and
beautiful, thus using "{}" means embracing the language's concept.


The collection-literals are a great thing, no doubt. But these 
alternatives are not against any concept.


Diez
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python/HTML integration: phileas v0.3 released

2009-11-20 Thread papa hippo
On 20 nov, 09:02, Stefan Behnel  wrote:
> papa hippo, 19.11.2009 19:53:
>
> > The prime goal of 'phileas' is to enable html code to be seamlessly
> > included in python code in a natural looking syntax, without resorting
> > to templatng language.
>
> I assume you know XIST, ElementTree's ElementMaker, and all those other
> ways of generating XML/HTML from Python code in a natural looking way?
>
> Stefan

Hi Stefan,

Thanks for your feedback.

Yes,  I am aware that phileas might - on the basis of the short
description on this post - come across like a 're-invented wheel'.
There is, however, one big difference between phileas and all other
other similar packages (XIST, ELementTree, HTMLgen, HyperText,
pyhtmloo etc.) that I inspected:

Phileas uses distinct objects to generate each start and end tag,
whereas all the others use a single function call (in some cases
itself generated by a  function call)  to generate a complete well-
formed element including start-tag and (where required) end-tag. In
theory this is less neat and indeed it means one can write 'bad' HTML
(e.g. missing end of paragraphs) with phileas just as easily as when
writing pure html. In practice, however, I find it at a lot easier to
use.

While using pyhtmloo (my previous favourite HTML generator), I had
found myself using awkward complicated artificial constructions in
order to generate all but the simplest HTML - and spent much time
playing 'hunt the missing bracket'. With phileas, these complexities
seem to just fall away.

Put another way, Phileas generates HTML4.0 - warts and all; it is not
a parser or generator of XML.

I'm considering building in checks/warnings for unclosed elements
etc., probably in the next-but-one pre-release.

Larry
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python/HTML integration: phileas v0.3 released

2009-11-20 Thread papa hippo
On 19 nov, 20:18, Steve Howell  wrote:
> On Nov 19, 10:53 am, papa hippo  wrote:
>
> > The prime goal of 'phileas' is to enable html code to be seamlessly
> > included in python code in a natural looking syntax, without resorting
> > to templatng language.
>
> > see:
>
> >http://larry.myerscough.nl/phileas_project/
>
> > I intend to submit phileas to the python.announce  forum within the
> > next few days. Any feedback received now will be gratefully received
> > and may lead to improved quality of that submission.
>
> Hi Larry, looks like interesting stuff!
>
> There appears to be a problem with this page:
>
> http://larry.myerscough.nl/show_python_source.py?script_filename=./My...
>
> IOError: [Errno 2] No such file or directory: './MyPage.py'
>
Oh dear! my blunder; While syncing (with meld) from my test
environment to the live environment, I missed a file. It seems to be
fixed now.

> I do like the idea of having a more Python-oriented way to generate
> HTML.

That's good to hear.


Larry
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread DreiJane
NB: I wondered about about dict(one=1, two=2) - why not d = {one:1,
two:2} ? Since you do not write L=list((1, 2)) either. These composed
objects as basic building blocks make Python code so dense and
beautiful, thus using "{}" means embracing the language's concept.
-- 
http://mail.python.org/mailman/listinfo/python-list


Announcement: depikt - the minimalistic python gate to gtk

2009-11-20 Thread DreiJane
Hi all,

these days i make depikt, a python C-extension for building apps with
gtk. It was a challenge for me and big fun. From its short description
on sourceforge.net:

"Python-3 wrappers for GTK. A minimalistic approach - just suited for
GUI-building of apps, in no way for widget-building. Currently 1250
lines for 15 widgets with about 100 methods ... "

depikt is advanced enough now to begin the transition of an 8MB python
app of me to it - simultaneously with the transition to Python-3 and
English. During this process depikt will grow to support for perhaps
25 widgets and 200 methods. From gobject it has connect(),
connect_after(), handler_block(), handler_unblock() and
handler_is_connected(). Still its status is marked as PreAlpha and
depikt not entered to the Python wiki, because
gtk_container_set_focus_chain() is defunct now - a vital method for
fine usable applications. It's difficult to find out why - as it is
the code was working for some time. depikt is very minimalistic:

No python attributes of its classes, let alone properties

No __repr__, no standard __init__

No support for set_property, get_property

No real class hierarchy, there is one abstract base class Pikt, from
which all concrete widgets are inheriting directly

No exposing of Glib to python (but all i need from pango, cairo and
gdk)

Thus the code is very petty, well understandable and easy to extend to
support for other widgets. That was the primary design goal, together
with exact control of encoding issues, support for Python-3 and
registry- and autoconf-free installation. Exception handling is about
average now - there no mimimalism is intended, quite the contrary (for
the long run). depikt will get to some mature state in 2010 - with
perhaps about 3.000 lines then. One C-file will always be sufficient.
atk might be added by means of a second file, also support for
gtk.TreeView might get an own file - but both directly inserted by
#include (no .h planned).

Enjoy, Joost
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does turtle graphics have the wrong associations?

2009-11-20 Thread Robert Maas, http://tinyurl.com/uh3t
> > My proposed no-syntax
> > IDE *also* gets rid of the need to bother with any programming-language
> > syntax. I've been proposing it for years, but nobody has shown any
> > interest
> From: Steven D'Aprano 
> I'm interested. No-syntax IDE? How is this even possible?

I guess you missed what I previously posted. The basic idea is that
you start with test data, and you use menus to select appropriate
data-processing actions to perform on that data. For example, you
manually key in a file name containing test data, or copy and paste
that same file name. Then you select "open file by that name" or
"load all lines from file by that name" etc. from a menu. If you
just opened the file, you now have a stream of input, and you can
select to read one line or one s-expression or one character etc.
from that file. After loading the whole file or one unit of data,
you now have some *real* data to work from. For example, with a
line of input, you might break it into words.

Caveat: When I said "no syntax", I mean no programming syntax, no
syntax to indicate function calls or variable assignment etc. You
still must deal with visual representation of strings and integers
and other essential data objects, which in a way may be considered
to be syntax. But if you want, you can avoid *all* syntax, even for
data, by drawing a *picture* of the individual bits. For some
low-level machine-language training, or boolean algebra lessons,
this might actually be preferable to Arabic numerals and English
letters to represent data values. Seeing a *picture* of something
that looks like a paper tape seems more binary-friendly than seeing
arabic digits "0" and "1" in rows across a page, when studying
operations upon boolean values or bitmasks.

Now if you don't like the burden of navigating the multi-level
menus, a search engine can be available, whereby you key in
keywords for the name of some data-processing operation you either
saw before or a name you can construct in your mind based on naming
conventions.

The extreme case of search engine would be if English-language
pseudo-code can be automatically converted into a very short menu
of most likely data-processing operations. I actually am seriously
considering doing NewEco software development by this means.
Basically I contract people to brainstorm with me in a Delphi
fashion to create the toplevel design of a new computer algorithm,
then we do top-down break into pieces, and continue top-down
break-down as far as it takes until the search engine recognizes
some step as something it already knows how to do.

> The only way I can think of is some sort of wizard interface.
> E.g. instead of having to remember the syntax for a slice, you
> click the "Slice" button and the computer prompts you to enter
> some combination of start, end, step, then generates the syntax
> [a:b:c] for you.

Yeah, it'll be vaguely like that in some ways. For major data,
we'll have the data first, all parameters to the next function to
apply, and *then* we select what function to call on those
parameters. But for minor parameters, we might allow you to choose
the function before filling in the minor-parameter slots. For
example, to select the nth character of string, we might have both
the string and the integer N before we select the NTH function, but
if N is a constant we might instead have just the string and select
the NTH function and fill in the constant N. With the pseudo-code
translation, some of the constant parameters might be apparent in
the English. For example, we have a string, and we say "select the
second word" (actually that's almost HyperTalk, the scripting
language of HyperCard on Macintosh computers), at which point the
constant parameter 2 would be supplied from the word "second" in
the English.
-- 
http://mail.python.org/mailman/listinfo/python-list


Book: Programming Python 3 (Second Edition) now available

2009-11-20 Thread Mark Summerfield
Hi,

I'm delighted to announce that a new edition of my Python 3 book is
now available in the U.S.

"Programming in Python 3 (Second Edition):
A Complete Introduction to the Python Language"
ISBN 0321680561
http://www.qtrac.eu/py3book.html

The book has been fully revised and updated and now covers both Python
3.0
and 3.1, and with the language moratorium (PEP 3003), this second
edition
should be useful for many years to come. And in addition to the
thorough
updating, the book has been extended with new chapters on debugging,
testing, and profiling, and on parsing (including coverage of the
PyParsing and
PLY modules), as well as a new section on coroutines in the advanced
chapter.

The book is aimed at a wide audience, but assumes some programming
experience (not necessarily Python, not necessarily object-oriented).
It teaches solid procedural style programming, then builds on that to
teach solid object-oriented programming, and then goes on to more
advanced
topics (e.g., including a nice way to create validated attributes by
combining class decorators with descriptors). But even newcomers to
Python 3 should be able to write useful (although small and basic)
programs after reading chapter 1, and then go on to create larger and
more sophisticated programs as they work through the chapters.

All the examples are available for download from the book's web site.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is an interactive command a block?

2009-11-20 Thread Benjamin Kaplan
On Thu, Nov 19, 2009 at 4:42 PM, Alf P. Steinbach  wrote:
> * Steven D'Aprano:
>>
>> On Thu, 19 Nov 2009 21:37:17 +0100, Alf P. Steinbach wrote:
>>
>>> The CPython 3.1.1 language reference §4.1 says
>>>
>>>   "Each command typed interactively is a block."
>>>
>>> It also says
>>>
>>>   "If a name is bound in a block, it is a local variable of that block,
>>>   unless
>>>    declared as nonlocal"
>>>
>>> Even with a non-literal try-for-best-meaning reading I can't get this to
>>> mesh with the actual behavior of the interpreter, e.g.
>>>
>>>   >>> for x in "poi":
>>>   ...    fandango = 666
>>>   ...
>>>   >>> fandango
>>>   666
>>>   >>> _
>>>
>>> My current understanding is (A) that the interpreter is correct in this
>>> respect (for one would harldly want the effects of statements to be
>>> fundamentally different in interpreted mode, except the presentation of
>>> expression results), and (B), but here I'm less sure, that the
>>> documentation is incorrect.
>>
>>
>> Why do you say that? I don't see what it is in the command you typed that
>> leads you to think the documentation is incorrect.
>>
>> The first command you type is:
>>
>> for x in "poi":
>>    fandango = 666
>>
>>
>> which binds two names, x and fandango. Since you are not typing them in a
>> function or class definition, locals() is globals() and the two local names
>> you create happen to also be globals.
>
> Thanks, that may be it.
>
> In most other languages I'm familiar with, if a name is local then it isn't
> global (and vice versa), and I had that as an underlying assumption since it
> doesn't appear to be mentioned in the documentation.
>
> However, I find a language reference statement that alludes to this: "(The
> variables of the module code block are local and global.)"
>
> Hm...
>
>
>> The next two commands you type:
>>
>> fandango
>> _
>>
>> don't bind anything, so aren't relevant.
>
> Well it showed that 'fandango' had been defined as a global. :-)
>
>
>> If it helps:
>>
>>
> for x in "poi":
>>
>> ...     fandango = 666
>> ...
>
> locals() is globals()
>>
>> True
>
> fandango
>>
>> 666
>
> locals()['fandango']
>>
>> 666
>
> import __main__
> __main__.fandango
>>
>> 666
>
> Yeah, helps.
>
> I feel that there's still something lacking in my understanding though, like
> how/where the "really actually just pure local not also global" is defined
> for function definition, but it's now just a vague feeling of something
> missing, not a feeling of direct contradiction as I had when I believed
> local != global.
>

It's because the only blocks in python that have their own scope are
classes and functions. For loops don't have their own scope- they use
the enclosing one, which in this case is globals.

>
> Cheers, & thanks,
>
> - Alf
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Will Not Send Email!!

2009-11-20 Thread Victor Subervi
On Thu, Nov 19, 2009 at 5:01 PM, Kev Dwyer  wrote:

> On Thu, 19 Nov 2009 11:28:37 -0400, Victor Subervi wrote:
>
> Hello Victor,
>
> There are some pages on the internet that suggest that this problem my be
> caused by a module named email.py (or email.pyc) in your pythonpath.  If
> you try import smtplib in the interpreter do you get this error message?
> If so, start a new interpreter session and try import email - is the
> email module imported from the stdlib?
>

Both of these import just fine.

>
> If these steps don't help, it might be useful if you can tell us which
> Linux distribution you are using.
>

Python 2.4.3
[r...@13gems ~]# uname -a
Linux 13gems.com.13gems.com 2.6.18-028stab064.8 #1 SMP Fri Nov 6 11:28:25
MSK 2009 x86_64 x86_64 x86_64 GNU/Linux
CentOS 5.4 final
TIA,
V
-- 
http://mail.python.org/mailman/listinfo/python-list


Regexp and multiple groups (with repeats)

2009-11-20 Thread mk

Hello,

>>> r=re.compile(r'(?:[a-zA-Z]:)([\\/]\w+)+')

>>> r.search(r'c:/tmp/spam/eggs').groups()
('/eggs',)

Obviously, I would like to capture all groups:
('/tmp', '/spam', '/eggs')

But it seems that re captures only the last group. Is there any way to 
capture all groups with repeat following it, i.e. (...)+ or (...)* ?


Even better would be:

('tmp', 'spam', 'eggs')

Yes, I know about re.split:

>>> re.split( r'(?:\w:)?[/\\]', r'c:/tmp/spam\\eggs/' )
['', 'tmp', 'spam', '', 'eggs', '']

My interest is more general in this case: how to capture many groups 
with a repeat?


Regards,
mk


--
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread Simon Brunning
2009/11/20 Michele Simionato :
> Yes, but only in Python 3:
>
 {(i, x) for i, x in enumerate('abc')}
> {(0, 'a'), (1, 'b'), (2, 'c')}

In Python 2.x, you can do:

>>> dict((i, x) for i, x in enumerate('abc'))
{0: 'a', 1: 'b', 2: 'c'}

(Works in 2.5 - I can't remember when generator expressions were introduced.)

-- 
Cheers,
Simon B.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread Dave Angel

Peng Yu wrote:

I'm wondering if there is something similar to list comprehension for
dict (please see the example code below).


d = dict(one=1, two=2)
print d

def fun(d):#Is there a way similar to list comprehension to change the
argument d so that d is changed?
  d=dict(three=3)

fun(d)
print d

def fun1(d):
  d['one']=-1

fun1(d)
print d


L = [1, 2]
print L

def fun2(L):#this doesn't have any effect on the argument L
  L=[]

fun2(L)
print L#[1, 2]

def fun3(L):# argument L is changed
  L[:]=[1, 2, 3]

fun3(L)
print L#[1, 2, 3]

  
You confused me by calling it a list comprehension.  All you're using in 
fun3() is a slice.  Using a slice, you can give a new set of values to 
an existing list.


For a dictionary, it's just a bit trickier.  You need two steps in the 
most general case.


def fun4(d):
  d.clear()  #clear out existing entries
   d.update(new_dict)  #copy in new key:val pairs from a 
different dictionary


This function will modify the caller's dictionary, completely replacing 
the contents.


DaveA
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread Tim Golden

Michele Simionato wrote:

On Nov 20, 4:18 am, Peng Yu  wrote:

I'm wondering if there is something similar to list comprehension for
dict


Yes, but only in Python 3:


{(i, x) for i, x in enumerate('abc')}

{(0, 'a'), (1, 'b'), (2, 'c')}


Although the 2.x syntax is hardly onerous:

dict ((i+5, x) for i, x in enumerate ('abc'))


-- obviously without something like the i+5, the example
equates to dict (enumerate ('abc')) 


:)

TJG
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python/HTML integration: phileas v0.3 released

2009-11-20 Thread Daniel Fetchinson
>> The prime goal of 'phileas' is to enable html code to be seamlessly
>> included in python code in a natural looking syntax, without resorting
>> to templatng language.
>>
>> see:
>>
>> http://larry.myerscough.nl/phileas_project/
>>
>> I intend to submit phileas to the python.announce  forum within the
>> next few days. Any feedback received now will be gratefully received
>> and may lead to improved quality of that submission.
>>
>
> Hi Larry, looks like interesting stuff!
>
> There appears to be a problem with this page:
>
> http://larry.myerscough.nl/show_python_source.py?script_filename=./MyPage.py
>
> IOError: [Errno 2] No such file or directory: './MyPage.py'
>
> I do like the idea of having a more Python-oriented way to generate
> HTML.

Have you guys considered markup.py from http://markup.sourceforge.net/
? It's comparable to your project as far as I can see, a more detailed
comparison would probably be useful.

Cheers,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python simply not scaleable enough for google?

2009-11-20 Thread Robin Becker

Aahz wrote:

In article ,
Robert P. J. Day  wrote:

http://groups.google.com/group/unladen-swallow/browse_thread/thread/4edbc406f544643e?pli=1

 thoughts?


Haven't seen this elsewhere in the thread:

http://dalkescientific.com/writings/diary/archive/2009/11/15/10_tasklets.html



I looked at this and it looks very good in that stackless appears twice as fast 
as go(lang) (I used to be in the department of computing at Imperial so I 
suppose I have to side with McCabe).


Anyhow, my reading of why Pike was so proud of his set up and tear down of the 
tasks example was that these were real threads.


Presumably that means they could potentially run in parallel on the 10 cpu 
machines of the future.


I'm not so clear on whether the threadless tasklets will run on separate cpus.
--
Robin Becker

--
http://mail.python.org/mailman/listinfo/python-list


checking 'type' programmatically

2009-11-20 Thread mk


Disclaimer: this is for exploring and debugging only. Really.

I can check type or __class__ in the interactive interpreter:

Python 2.6.2 (r262:71600, Jun 16 2009, 16:49:04)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> 
p=subprocess.Popen(['/bin/ls'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)

>>> p

>>> (so, se) = p.communicate()
>>> so
'abc.txt\nbak\nbox\nbuild\ndead.letter\nDesktop\nhrs\nmbox\nmmultbench\nmmultbench.c\npyinstaller\nscreenlog.0\nshutdown\ntaddm_import.log\nv2\nvm\nworkspace\n'
>>> se
''
>>> so.__class__

>>> type(so)

>>> type(se)


But when I do smth like this in code that is ran non-interactively (as 
normal program):


req.write('stderr type %s' % type(se))
req.write('stderr class %s' % str(se.__class__))

then I get empty output. WTF?

How do I get the type or __class__ into some object that I can display?


Why do that: e.g. if documentation is incomplete, e.g. documentation on 
Popen.communicate() says "communicate() returns a tuple (stdoutdata, 
stderrdata)" but doesn't say what is the class of stdoutdata and 
stderrdata (a file object to read? a string?).


Regards,
mk



--
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread Andre Engels
On Fri, Nov 20, 2009 at 4:18 AM, Peng Yu  wrote:
> I'm wondering if there is something similar to list comprehension for
> dict (please see the example code below).
>
>
> d = dict(one=1, two=2)
> print d
>
> def fun(d):#Is there a way similar to list comprehension to change the
> argument d so that d is changed?
>  d=dict(three=3)
>
> fun(d)
> print d
>
> def fun1(d):
>  d['one']=-1
>
> fun1(d)
> print d
>
>
> L = [1, 2]
> print L
>
> def fun2(L):#this doesn't have any effect on the argument L
>  L=[]
>
> fun2(L)
> print L#[1, 2]
>
> def fun3(L):# argument L is changed
>  L[:]=[1, 2, 3]
>
> fun3(L)
> print L#[1, 2, 3]
> --
> http://mail.python.org/mailman/listinfo/python-list
>

def fun(d):
   d.clear()
   d[three] = 3




-- 
André Engels, andreeng...@gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread Terry Reedy

Peng Yu wrote:

I'm wondering if there is something similar to list comprehension for
dict (please see the example code below).


Python 3 has list, set, and dict comprehensions.
Don't know about 2.6/7

--
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread Paul Rudin
Patrick Sabin  writes:

> Peng Yu wrote:
>> I'm wondering if there is something similar to list comprehension for
>> dict (please see the example code below).
>
> Do you mean something like this:
>
 {i:i+1 for i in [1,2,3,4]}
> {1: 2, 2: 3, 3: 4, 4: 5}
>
> This works in python3, but not in python2

Of course in python 2 you can do:

>>> dict((i, i+1) for i in [1,2,3,4])
{1: 2, 2: 3, 3: 4, 4: 5}
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread Patrick Sabin

Peng Yu wrote:

I'm wondering if there is something similar to list comprehension for
dict (please see the example code below).


Do you mean something like this:

>>> {i:i+1 for i in [1,2,3,4]}
{1: 2, 2: 3, 3: 4, 4: 5}

This works in python3, but not in python2

- Patrick
--
http://mail.python.org/mailman/listinfo/python-list


Re: non-copy slices

2009-11-20 Thread Ajit Kumar
On Thu, Nov 19, 2009 at 8:14 PM, Ethan Furman  wrote:
>> No I'm well aware that there is no deep copy of the objects and the lists
>> only keep references to the objects and in essence they have the same
>> objects in there. But this doesn't mean they are the same list.
>> Modifications to slices are not written back to the original list.
>>
>> x = range(5)
>> y = x[1:3]
>> y[0] = 13
>> x[1] == y[0]  --> False
>>
>> Of course if I modify the object in the slice then the original list will
>> see the change, but this is not what I was saying. Second and more
>> importantly it's the performance penalty from allocating a large number of
>> lists produced from the slices and the copy of the references. islice does
>> not have this penalty, it should only instantiate a small object that
>> iterates on the original list.
>>
>> Themis
>
> So "shallow copy" == "new label created for existing object".
>
> So is your desired behavior to write back to the original list if your
> sub-list is modified?  In other words, you are creating a window onto an
> existing list?  If not, what would happen when a sublist element was
> modified (or deleted, or appended, or ...)?

On a related note, GO encourages use of slices.

http://golang.org/doc/effective_go.html#slices
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread Stefan Behnel
Stefan Behnel, 20.11.2009 09:24:
> You can use  d.update(...)
> 
> It accepts both another dict as well as a generator expression that
> produces item tuples, e.g.
> 
> d.update( (i, i**2) for i in range(10) )

This also works, BTW:

>>> d = {}
>>> d.update(value=5)
>>> d
{'value': 5}

Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread Stefan Behnel
Peng Yu, 20.11.2009 04:18:
> I'm wondering if there is something similar to list comprehension for
> dict (please see the example code below).

A list comprehension is an expression that produces a list, e.g.

[ i**2 for i in range(10) ]

Your example below uses a slice assignment.


> def fun(d):#Is there a way similar to list comprehension to change the
> argument d so that d is changed?
>   d=dict(three=3)
> [...]
> def fun3(L):# argument L is changed
>   L[:]=[1, 2, 3]

You can use  d.update(...)

It accepts both another dict as well as a generator expression that
produces item tuples, e.g.

d.update( (i, i**2) for i in range(10) )

Does that help?

Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to list comprehension in dict?

2009-11-20 Thread Michele Simionato
On Nov 20, 4:18 am, Peng Yu  wrote:
> I'm wondering if there is something similar to list comprehension for
> dict

Yes, but only in Python 3:

>>> {(i, x) for i, x in enumerate('abc')}
{(0, 'a'), (1, 'b'), (2, 'c')}
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python/HTML integration: phileas v0.3 released

2009-11-20 Thread Stefan Behnel
papa hippo, 19.11.2009 19:53:
> The prime goal of 'phileas' is to enable html code to be seamlessly
> included in python code in a natural looking syntax, without resorting
> to templatng language.

I assume you know XIST, ElementTree's ElementMaker, and all those other
ways of generating XML/HTML from Python code in a natural looking way?

Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: DOM related question and problem

2009-11-20 Thread Stefan Behnel
elca, 18.11.2009 19:04:
> these day im making python script related with DOM.
> 
> problem is these day many website structure is very complicate .
> [...]
> what is best method to check  can extract such like following info quickly?

This should help:

http://blog.ianbicking.org/2008/12/10/lxml-an-underappreciated-web-scraping-library/

Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: Urwid 0.9.9 - Console UI Library

2009-11-20 Thread Tim Roberts
"Michel Claveau - MVP" wrote:
>
>Hi! 
>
>You forget to write "urwid" do not run under Windows.

He also forgot to write "urwid" do not run under CP/M or OS/360.  Why did
you feel compelled to post this three times?  If it supported Windows, it
would say so.  The fact that it doesn't say so means it isn't supported.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing a Carriage Return in Unicode

2009-11-20 Thread Scott David Daniels

MRAB wrote:

u'\u240D' isn't a carriage return (that's u'\r') but a symbol (a visible
"CR" graphic) for carriage return. Windows programs normally expect
lines to end with '\r\n'; just use u'\n' in programs and open the text
files in text mode ('r' or 'w').



This is the one thing from standards that I believe Microsoft got right
where others did not.  The ASCII (American Standard for Information
Interchange) standard end of line is _both_ carriage return (\r) _and_
line feed (\n) -- I believe in that order.

The Unix operating system, in its enthusiasm to make _everything_
simpler (against Einstein's advice, "Everything should be made as simple
as possible, but not simpler.") decided that end-of-line should be a
simple line feed and not carriage return line feed.  Before they made
that decision, there was debate about the order of cr-lf or lf-cr, or
inventing a new EOL character ('\037' == '\x1F' was the candidate).

If you've actually typed on a physical typewriter, you know that moving
the carriage back is a distinct operation from rolling the platen
forward; both operations are accomplished when you push the carriage
back using the bar, but you know they are distinct.  Hell, MIT even had
"line starve" character that moved the cursor up (or rolled the platen
back).


Lots of people talk about "dos-mode files" and "windows files" as if
Microsoft got it wrong; it did not -- Unix made up a convenient fiction
and people went along with it. (And, yes, if Unix had been there first,
their convention was, in fact, better).

So, sorry for venting, but I have bee wanting to say this in public
for years.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Inserting Unicode text with MySQLdb in Python 2.4-2.5?

2009-11-20 Thread John Nagle

Keith Hughitt wrote:

Hi all,

I ran into a problem recently when trying to add support for earlier
versions of Python (2.4 and 2.5) to some database related code which
uses MySQLdb, and was wondering if anyone has any suggestions.

With later versions of Python (2.6), inserting Unicode is very simple,
e.g.:

# -*- coding: utf-8 -*-
...
cursor.execute('''INSERT INTO `table` VALUES (0,
'Ångström'),...''')

When the same code is run on earlier versions, however, the results is
either garbled text (e.g. "Ã or "?" instead of "Å" in Python 2.5), or
an exception being thrown (Python 2.4):

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in
position 60: ordinal not in range(128)

So far I've tried a number of different things, including:

1. Using Unicode strings (e.g. u"\u212B")

2. Manually specifying the encoding using sys.setdefaultencoding
('utf-8')

3. Manually enabling Unicode support in MySQLdb
(use_unicode=False, charset = "utf8")


   No, that's backwards.  Try:

db = MySQLdb.connect(host="localhost",
use_unicode = True, charset = "utf8",
user=username, passwd=password, db=database)

"use_unicode" means that you want MySQLdb to accept and return
Unicode strings.  "charset="utf8" means you want MySQLdb to
negotiate with the server to use UTF8 on the socket connecting
it to the database.  This works fine in Python 2.4 and 2.5.
Returned strings will be in Unicode.

At the database end, you have to make sure that 1) MySQL was
built with Unicode support (it usually is), 2) the database
fields of interest are in Unicode.  I suggest

ALTER DATABASE dbname DEFAULT CHARACTER SET utf8;

before doing any CREATE TABLE operations. Then strings
will be UTF8 in the database.

Read this: http://dev.mysql.com/doc/refman/5.0/en/charset-unicode.html

It all works quite well.

John Nagle
--
http://mail.python.org/mailman/listinfo/python-list


Re: FYI: ConfigParser, ordered options, PEP 372 and OrderedDict + big thank you

2009-11-20 Thread Scott David Daniels

Jonathan Fine wrote:...

A big thanks to Armin Ronacher and Raymond Hettinger for
   PEP 372: Adding an ordered dictionary to collections
...  I prototyped (in about an hour).

I then thought - maybe someone has been down this path before

So all that I want has been done already, and will be waiting for me 
when I move to Python3.


So a big thank you is in order.


And thank you for, having done that, not simply smiling because your
work was lighter.  Instead you described a great work path and handed
an attaboy to a pair of people that richly deserve attaboys.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: python bijection

2009-11-20 Thread Joshua Bronson
On Nov 19, 9:17 pm, Carl Banks  wrote:
> Apart from the GPL

what Ben said :)

> it seems perfectly fine to release, and looks like
> an interesting strategy.  I've wanted one of those once in a while,
> never enough to bother looking for one or writing one myself.

glad to hear it! i'll release it to pypi if such feedback continues.

> But you should absolutely not inherit from dict if you're overriding
> all it's methods.  It's useless and wasteful to do that, perhaps
> dangerous.  You end up using bytes for a small hash table that's never
> used.
>
> Plus Python 3 has a notion of Abstract Base Classes: it will allow
> customization of isinstance to advertise that your class implements
> MutableMapping, which is the right way to do it.

Actually that's what I was originally thinking of doing but didn't go
through with it in my first pass out of concern that users might want
isinstance(bijection(), dict) to be True. Now that you bring it up, I
agree that it's the correct way to do it, and have reimplemented
bijection as a MutableMapping (ABCs are actually in Python 2.6). Take
a peek at the new and improved 
http://bitbucket.org/jab/toys/src/tip/bijection.py
if you get a chance and let me know how it looks!

Anyone have any other feedback? For instance, is offering the __call__
syntax for the inverse mapping wonderful or terrible, or maybe both?

Thanks,
Josh
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python bijection

2009-11-20 Thread Ben Finney
Carl Banks  writes:

> On Nov 19, 3:24 pm, Joshua Bronson  wrote:
> > I couldn't find a library providing a bijective map data structure
> > (allowing for constant-time lookups by value) in the few minutes I
> > looked, so I took a few more minutes to code one
> > up:http://bitbucket.org/jab/toys/src/tip/bijection.py
> >
> > Is this at all worth releasing? Comments and suggestions welcome.
>
> Apart from the GPL, it seems perfectly fine to release, and looks like
> an interesting strategy. I've wanted one of those once in a while,
> never enough to bother looking for one or writing one myself.

I would think GPL is an excellent choice for such a library then, if the
author's intention is to encourage more software to be free software so
that it can incorporate a unique library like this.

-- 
 \  “The fact that I have no remedy for all the sorrows of the |
  `\ world is no reason for my accepting yours. It simply supports |
_o__)  the strong probability that yours is a fake.” —Henry L. Mencken |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python bijection

2009-11-20 Thread Carl Banks
On Nov 19, 3:24 pm, Joshua Bronson  wrote:
> I couldn't find a library providing a bijective map data structure
> (allowing for constant-time lookups by value) in the few minutes I
> looked, so I took a few more minutes to code one 
> up:http://bitbucket.org/jab/toys/src/tip/bijection.py
>
> Is this at all worth releasing? Comments and suggestions welcome.


Apart from the GPL, it seems perfectly fine to release, and looks like
an interesting strategy.  I've wanted one of those once in a while,
never enough to bother looking for one or writing one myself.

But you should absolutely not inherit from dict if you're overriding
all it's methods.  It's useless and wasteful to do that, perhaps
dangerous.  You end up using bytes for a small hash table that's never
used.

Plus Python 3 has a notion of Abstract Base Classes: it will allow
customization of isinstance to advertise that your class implements
MutableMapping, which is the right way to do it.


Carl Banks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Whom Must We Worship

2009-11-20 Thread furlan
On Sun, 15 Nov 2009 00:17:43 -0800, Mary wrote:

> Whom Must We Worship



> The Decision is yours!

Thank you, I got that.

I choose Python. Thanks for sharing.

ciao,
f

-- 
aa #2301
"...The word that separates that which is dead from that which is 
livingIn the beginning was the word and that word was...CHOICE"
-- Tom Robbins (SLWW)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python bijection

2009-11-20 Thread Joshua Bronson
On Nov 19, 7:05 pm, Steven D'Aprano  wrote:
> If I want a mapping a <-> b, I generally just create a dict {a:b, b:a}.
> What is the advantages or disadvantages of your code over the simplicity
> of the dict approach?

Well for one, you don't have to manually update the mapping from b ->
a if ever the mapping from a -> b changes. With your method you have
to write something like "d[a] = c; d[c] = a; del d[b]" instead of just
"d[a] = c", "del d[d.pop(a)]" instead of just "del d[a]", etc.

More significantly, your approach doesn't actually model a bijection
since there's no distinction between keys (the domain) and values (the
range). In other words, you lose information about which way is the
forward mapping and which is the inverse mapping. Worse, d.keys() and
d.values() would each give you the combination of your keys and
values, neither of which would be right, and d.items() would also
return twice as many elements as you expect with no way to distinguish
which side of the mapping a given pair comes from.
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >