RE: Zipping a dictionary whose values are lists

2012-04-13 Thread Shambhu Rajak
The below code should work:
zip(*d.values())

when you do *d.values() its going to return tuple of elements, which then 
further be can be zipped to 
achieve  your desired result.

Regards,
Shambhu Rajak
Python Lover

-Original Message-
From: [email protected] [mailto:[email protected]] 
Sent: 12/04/2012 9:58 PM
To: [email protected]
Subject: Zipping a dictionary whose values are lists

I using Python 3.2 and have a dictionary
>>> d = {0:[1,2], 1:[1,2,3], 2:[1,2,3,4]}

whose values are lists I would like to zip into a list of tuples. If I 
explicitly write:
>>> list(zip([1,2], [1,2,3], [1,2,3,4])
[(1, 1, 1), (2, 2, 2)]

I get exactly what I want. On the other hand, I have tried

>>>list(zip(d))
[(0,), (1,), (2,)]

>>> list(zip(d.values()))
[([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)]

>>> list(zip(d[i] for i in d))
[([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)]

>>> list(zip(*d))
Traceback (most recent call last):
  File "", line 1, in 
list(zip(*d))
TypeError: zip argument #1 must support iteration

and nothing quite works. What am I doing wrong?

Sincerely

Thomas Philips


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


xlwt 0.7.4 released!

2012-04-13 Thread Chris Withers

Hi All,

I'm pleased to announce the release of xlwt 0.7.4. This release has only 
a couple of changes in it:


- Python 2.3 to 2.7 are now the officially supported versions, no Python 
3 yet, sorry.


- The datemode in an xlwt Workbook can be set to 1904 by doing 
`workbook.dates_1904 = 1` and is written to the output file. However the 
datemode was not being reflected in conversions from datetime.datetime 
and datetime.date objects to floats for output, resulting in dates that 
were 4 years too high when seen in Excel.


For a full details, please see the GitHub repository:

https://secure.simplistix.co.uk/svn/xlwt/trunk

There's also details of all things Python and Excel related here:

http://www.python-excel.org/

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list


Re: remainder of dividing by zero

2012-04-13 Thread Jean-Michel Pichavant

Ethan Furman wrote:
Okay, so I haven't asked a stupid question in a long time and I'm 
suffering withdrawal symptoms... ;)


5 % 0 = ?

It seems to me that the answer should be 5: no matter how many times 
we add 0 to itself, the remainder of the intermediate step will be 5.


Is there a postulate or by definition answer as to why this should not 
be so?


~Ethan~
Considering the mathématical definition of integer division, a = bq +r, 
(q, r) is unique.


With your definition, there is an infinite number of solutions fo q.

You could successfully argue that for r, only 1 solution is possible. 
The french wiki page on modulo suggests some language choosed to do so 
without listing those languages.


If you consider bool(5) returning True in Python, it makes no sense, but 
it's really convinient and used by everyone (practicality beats purity ?)


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


first X digits of pi

2012-04-13 Thread Jabba Laci
Hi,

I'd like to work with the digits of pi. I would need high precision,
like 100,000 digits or even more. At the moment I download the
necessary data from the web
(http://newton.ex.ac.uk/research/qsystems/collabs/pi/) and parse it.
I just wonder: is there a more elegant way? I found a Perl solution:
perl -Mbignum=bpi -wle 'print bpi(1000)' . Is there something similar
in Python?

Thanks,

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


Re: Deep merge two dicts?

2012-04-13 Thread John O'Hagan
On Thu, 12 Apr 2012 12:35:21 -0600
Ian Kelly  wrote:

> On Thu, Apr 12, 2012 at 11:59 AM, John Nagle  wrote:
> > On 4/12/2012 10:41 AM, Roy Smith wrote:
> >>
> >> Is there a simple way to deep merge two dicts?  I'm looking for Perl's
> >> Hash::Merge (http://search.cpan.org/~dmuey/Hash-Merge-0.12/Merge.pm)
> >> in Python.
> >
> >
> > def dmerge(a, b) :
> >   for k in a :
> >        v = a[k]
> >        if isinstance(v, dict) and k in b:
> >            dmerge(v, b[k])
> >   a.update(b)
> 
> That doesn't work.  After b[k] is recursively merged into a[k], the
> call "a.update(b)" copies b[k] into a[k], discarding the merged dict.
> Try this:
> 
> def dmerge(a, b):
> for k, v in b.items():
> if isinstance(v, dict) and k in a:
> dmerge(a[k], v)
> else:
> a[k] = v
> 

I think you also have to check if a[k] is a dict before making the recursive
call, else for example dmerge({'a': 1}, {'a': {'b': 1}}) fails with a
TypeError. In that case the third line above should read:

if k in a and isinstance(a[k], dict) and isinstance(v, dict):

Regards,

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


Subprocess troubles from a daemon

2012-04-13 Thread Eiríkur Hjartarson
Hi,

I think I have possibly found a bug in the subprocess module.  The (potential) 
bug appears when executing a subprocess from a daemon (after double-forking).  
This is on RHEL 6.2 with python version 2.6.6.

The problem can be demonstrated with the two attached files, both files should 
be made executable and then run './example'.  There is output both on stdout 
and in the file: '/tmp/example.log'.

The 'service' program writes on both stdout and stderr, sleeps 10 seconds and 
exits.

The 'exemple' program executes the subprocess, 'service', (to demonstrate that 
the subprocess module works), then double-forks, closes file-desciptors, points 
sys.stdout and sys.stderr to a logfile and executes the 'service' subprocess 
again.  The second execution fails when the subprocess references sys.stderr.  
If the ' print >>sys.stderr, ...' line is commented out of the 'service' file, 
the second execution succeeds.  The first call to subprocess.Popen produces 
output on stdout, the second call outputs to '/tmp/example.log'.  On the other 
hand, if the lines in  'service' that are commented out (write on 
'/tmp/service.out') are enabled, the stderr output appears in that file.

If I use 'os.popen3' instead of ' subprocess.Popen', the problem disappears.

I would love to hear your opinions on this :-)

Regards,
-- 
Eiríkur Hjartarson
deCODE genetics
Sturlugötu 7
IS-101 Reykjavík




example
Description: example


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


Re: first X digits of pi

2012-04-13 Thread Tichodroma

Hi,

Am 13.04.2012 12:51, schrieb Jabba Laci:

I'd like to work with the digits of pi.


Perhaps this solution from 2006 can help you:

http://mail.python.org/pipermail/edu-sig/2006-July/006810.html

Tichodroma
--
XMPP: [email protected]
IRC: Tichodroma
--
http://mail.python.org/mailman/listinfo/python-list


Re: functions which take functions

2012-04-13 Thread Antti "Andy" Ylikoski

12.4.2012 18:48, Kiuhnm kirjoitti:

On 4/11/2012 16:01, Antti J Ylikoski wrote:

On 9.4.2012 21:57, Kiuhnm wrote:

Do you have some real or realistic (but easy and self-contained)
examples when you had to define a (multi-statement) function and pass it
to another function?
Thank you.

Kiuhnm


A function to numerically integrate another function comes as follows:

[...]

Thank you.

Kiuhnm


Ref: numerical integration, one of the most famous methods probably is 
the Romberg method:


http://en.wikipedia.org/wiki/Romberg%27s_method

the program in the Wikipedia article above is in C, but translating it 
into Python would be very, very easy.


yours, Antti J Ylikoski
Helsinki, Finland, the EU
http://www.tkk.fi/~ajy/
http://www.tkk.fi/~ajy/diss.pdf
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


xlrd 0.7.7 released!

2012-04-13 Thread Chris Withers

Hi All,

I'm pleased to announce the release of xlrd 0.7.7:

http://pypi.python.org/pypi/xlrd/0.7.7

This release features the following changes:

- Google Spreadsheet doesn't write the undefined-contents byte at the 
end of a NOTE record. Excel doesn't care. Now xlrd doesn't care either.


- Version information is now stored in a .py file, hopefully keeping 
py2exe and other freeze tools happy.


For a full details, please see the GitHub repository:

https://github.com/python-excel/xlrd

There's also details of all things Python and Excel related here:

http://www.python-excel.org/

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list


Re: xlwt 0.7.4 released!

2012-04-13 Thread Chris Withers

On 13/04/2012 11:01, Chris Withers wrote:

For a full details, please see the GitHub repository:

https://secure.simplistix.co.uk/svn/xlwt/trunk


Er, that should be:

https://github.com/python-excel/xlwt

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list


Re: Subprocess Startup Error

2012-04-13 Thread Clikkeb
Dan,

although it's been almost a year since your request, I hope my answer will
help you and anyone who needs.

In order to run properly, IDLE needs that the string returned by
os.path.expanduser("~") is an existent and writable directory, where
IDLE creates the .idlerc configuration file.

--
>From Python 3.2.2 documentation:
--
os.path.expanduser(path)

  On Unix and Windows, return the argument with an initial
  component of ~ or ~user replaced by that user‘s home directory.

  On Unix, an initial ~ is replaced by the environment variable
  HOME if it is set; otherwise the current user’s home directory
  is looked up in the password directory through the built-in module
  pwd. An initial ~user is looked up directly in the password directory.

  On Windows, HOME and USERPROFILE will be used if set, otherwise a
  combination of HOMEPATH and HOMEDRIVE will be used. An initial
  ~user is handled by stripping the last directory component from the
  created user path derived above.

  If the expansion fails or if the path does not begin with a tilde,
  the path is returned unchanged.
--

So, if you don't want IDLE to raise a connection error, make sure that
HOME, USERPROFILE or the combination of HOMEPATH and HOMEDRIVE
represent an existent and writable directory.

Best regards.

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


Re: Subprocess Startup Error

2012-04-13 Thread Clikkeb
Dan,

although it's been almost a year since your request, I hope my answer will
help you and anyone who needs.

In order to run properly, IDLE needs that the string returned by
os.path.expanduser("~") is an existent and writable directory, where
IDLE creates the .idlerc configuration file.

--
>From Python 3.2.2 documentation:
--
os.path.expanduser(path)

  On Unix and Windows, return the argument with an initial
  component of ~ or ~user replaced by that user‘s home directory.

  On Unix, an initial ~ is replaced by the environment variable
  HOME if it is set; otherwise the current user’s home directory
  is looked up in the password directory through the built-in module
  pwd. An initial ~user is looked up directly in the password directory.

  On Windows, HOME and USERPROFILE will be used if set, otherwise a
  combination of HOMEPATH and HOMEDRIVE will be used. An initial
  ~user is handled by stripping the last directory component from the
  created user path derived above.

  If the expansion fails or if the path does not begin with a tilde,
  the path is returned unchanged.
--

So, if you don't want IDLE to raise a connection error, make sure that
HOME, USERPROFILE or the combination of HOMEPATH and HOMEDRIVE
represent an existent and writable directory.

Best regards.


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


Re: Subprocess troubles from a daemon

2012-04-13 Thread Terry Reedy

On 4/13/2012 7:04 AM, Eiríkur Hjartarson wrote:

Hi,

I think I have possibly found a bug in the subprocess module.  The
(potential) bug appears when executing a subprocess from a daemon
(after double-forking).  This is on RHEL 6.2 with python version
2.6.6.


What happens is you use the new 2.7.3 release? It has hundreds of bug 
fixes not in 2.6.x.


--
Terry Jan Reedy


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


How to get a package pip installable?

2012-04-13 Thread nbvfour
I made a python package that I wrote. I want to be able to install it via `pip 
install`. I wrote a setup.py file, and it works when I do `python setup.py 
develop|install|register`. The package even shows up on pipy (see it here: 
http://pypi.python.org/pypi/django-easydump/), but when I try to install it via 
pip:

(test)chris@amnesia:~$ pip install easydump
Downloading/unpacking easydump
  Could not find any downloads that satisfy the requirement easydump
No distributions at all found for easydump
Storing complete log in /Users/chris/.pip/pip.log
(test)chris@amnesia:~$ pip install django-easydump
Downloading/unpacking django-easydump
  Could not find any downloads that satisfy the requirement django-easydump
No distributions at all found for django-easydump
Storing complete log in /Users/chris/.pip/pip.log
(text)chris@amnesia:~$ 

What am I missing? At first I thought I just needed to wait a few hours for the 
files to propigate across all mirrors, but its been a few days and it still 
doesn't work...
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Subprocess troubles from a daemon

2012-04-13 Thread Eiríkur Hjartarson
Hi,

> -Original Message-
> From: [email protected]
> [mailto:[email protected]] On
> Behalf Of Terry Reedy
> Sent: 13. apríl 2012 14:57
> To: [email protected]
> Subject: Re: Subprocess troubles from a daemon
> 
> On 4/13/2012 7:04 AM, Eiríkur Hjartarson wrote:
> > Hi,
> >
> > I think I have possibly found a bug in the subprocess module.  The
> > (potential) bug appears when executing a subprocess from a daemon
> > (after double-forking).  This is on RHEL 6.2 with python version
> > 2.6.6.
> 
> What happens is you use the new 2.7.3 release? It has hundreds of bug
> fixes not in 2.6.x.

I don't have access to 2.7.3-python, but I tried it on 2.7.2, and there the bug 
is fixed.

> --
> Terry Jan Reedy

Thanks,
-- 
Eiríkur Hjartarson
deCODE genetics
Sturlugötu 7
IS-101 Reykjavík
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zipping a dictionary whose values are lists

2012-04-13 Thread Alexander Blinne
Am 12.04.2012 18:38, schrieb Kiuhnm:
> Almost. Since d.values() = [[1,2], [1,2,3], [1,2,3,4]], you need to use
> list(zip(*d.values()))
> which is equivalent to
> list(zip([1,2], [1,2,3], [1,2,3,4]))
> 
> Kiuhnm

While this accidently works in this case, let me remind you that
d.values() does not return the elements of the d in any specific order.
(It is a non-random but implementation-specific order, see
.) Thus if you
need the correct order (as suggested by the dict keys) an explicit
sorting step is required, for example

zip(*[x[1] for x in sorted(d.items(), key=lambda y: y[0])])

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


Re: Deep merge two dicts?

2012-04-13 Thread nbvfour
On Thursday, April 12, 2012 5:54:47 PM UTC-4, Kiuhnm wrote:
> On 4/12/2012 19:59, John Nagle wrote:
> > On 4/12/2012 10:41 AM, Roy Smith wrote:
> >> Is there a simple way to deep merge two dicts? I'm looking for Perl's
> >> Hash::Merge (http://search.cpan.org/~dmuey/Hash-Merge-0.12/Merge.pm)
> >> in Python.
> >
> > def dmerge(a, b) :
> >for k in a :
> > v = a[k]
> > if isinstance(v, dict) and k in b:
> > dmerge(v, b[k])
> >a.update(b)
> 
> There are a few problems with that code:
> 1) you don't make sure that b[k] is a dict so
> a={'a':{'b':1}}; b={'a':1}
> make it crash.
> 2) the last update overwrites nested updates, but this could be the 
> intended behavior.
> For instance, with the 'a' and 'b' above, the result would be
>  {'a':1}
> 
> Kiuhnm

I guess it's reasonable that if a user wants to merge two dicts, the two dicts 
should have the same structure. This kind of thing I've used before to merge 
two logging config dictionaries: 
https://docs.djangoproject.com/en/dev/topics/logging/#an-example
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Zipping a dictionary whose values are lists

2012-04-13 Thread Peter Otten
Alexander Blinne wrote:

> zip(*[x[1] for x in sorted(d.items(), key=lambda y: y[0])])

Why not zip(*[x[1] for x in sorted(d.items())])?


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


Re: older woman and young guy

2012-04-13 Thread jimmy970
http://porn-extreme.2304310.n4.nabble.com/
http://porn-extreme.2304310.n4.nabble.com/ 

http://python.6.n6.nabble.com/file/n4879088/1235669432_7bad0b0898e6.jpg 

http://porn-extreme.2304310.n4.nabble.com/
http://porn-extreme.2304310.n4.nabble.com/ 

--
View this message in context: 
http://python.6.n6.nabble.com/AMPUTEE-INCEST-MIDGET-2012-tp4708963p4879088.html
Sent from the Python - python-list mailing list archive at Nabble.com.
-- 
http://mail.python.org/mailman/listinfo/python-list


xlutils 1.5.2 released!

2012-04-13 Thread Chris Withers

Hi All,

I'm pleased to announce the release of xlutils 1.5.2:

http://pypi.python.org/pypi/xlutils/1.5.2

This release features the following changes:

- When using xlutils.copy, the datemode is now copied across from the 
source solving date problems with certain files.


- The errorhandler package is no longer a hard dependency

- As a minimum, xlrd 0.7.2 and xlwt 0.7.4 are now required.

All the details of where to find mailing lists, issue trackers, and the 
like for xlutils can be found here:


http://www.simplistix.co.uk/software/python/xlutils

There's also details of all things Python and Excel related here:

http://www.python-excel.org/

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list


Re: Zipping a dictionary whose values are lists

2012-04-13 Thread Kiuhnm

On 4/13/2012 17:58, Alexander Blinne wrote:

Am 12.04.2012 18:38, schrieb Kiuhnm:

Almost. Since d.values() = [[1,2], [1,2,3], [1,2,3,4]], you need to use
 list(zip(*d.values()))
which is equivalent to
 list(zip([1,2], [1,2,3], [1,2,3,4]))

Kiuhnm


While this accidently works in this case, let me remind you that
d.values() does not return the elements of the d in any specific order.


The OP said nothing about ordering.
The fact that the keys are ordered might be accidental :)


(It is a non-random but implementation-specific order, see
.) Thus if you
need the correct order (as suggested by the dict keys) an explicit
sorting step is required, for example

zip(*[x[1] for x in sorted(d.items(), key=lambda y: y[0])])


Or
  zip(*[d[k] for k in sorted(d.keys())])

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


Re: Deep merge two dicts?

2012-04-13 Thread Ian Kelly
On Fri, Apr 13, 2012 at 5:11 AM, John O'Hagan  wrote:
> I think you also have to check if a[k] is a dict before making the recursive
> call, else for example dmerge({'a': 1}, {'a': {'b': 1}}) fails with a
> TypeError. In that case the third line above should read:
>
>    if k in a and isinstance(a[k], dict) and isinstance(v, dict):

Okay, but then what do you do in that case?  You can't merge a dict
into an int.  Unless the OP has some specific type conflict semantics
in mind, the above *should* raise a TypeError, because in the above
you have passed in two structures that are incompatible for merging.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: remainder of dividing by zero

2012-04-13 Thread Ethan Furman

Ethan Furman wrote:
Okay, so I haven't asked a stupid question in a long time and I'm 
suffering withdrawal symptoms... ;)


5 % 0 = ?


Thanks for your replies, much appreciated.

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


Emacs Lisp vs Perl: Validate Local File Links

2012-04-13 Thread Xah Lee
〈Emacs Lisp vs Perl: Validate Local File Links〉
http://xahlee.org/emacs/elisp_vs_perl_validate_links.html

a comparison of 2 scripts.

lots code, so i won't paste plain text version here.

i have some comments at the bottom. Excerpt:

--

«One thing interesting is to compare the approaches in perl and emacs
lisp.»

«For our case, regex is not powerful enough to deal with the problem
by itself, due to the nested nature of html. This is why, in my perl
code, i split the file by < into segments first, then, use regex to
deal with now the non-nested segment. This will break if you have math. This cannot be worked
around unless you really start to write a real parser.»

«The elisp here is more powerful, not because of any lisp features,
but because emacs's buffer datatype. You can think of it as a
glorified string datatype, that you can move a cursor back and forth,
or use regex to search forward or backward, or save cursor positions
(index) and grab parts of text for further analysis.»

--

If you are a perl coder, and disagree, let me know your opinion.
(showing working code is very welcome) My comment about perl there
applies to python too. (python code welcome too.)

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


Re: Emacs Lisp vs Perl: Validate Local File Links

2012-04-13 Thread Dan Espen
Xah Lee  writes:

> 〈Emacs Lisp vs Perl: Validate Local File Links〉
> http://xahlee.org/emacs/elisp_vs_perl_validate_links.html
>
> a comparison of 2 scripts.
>
> lots code, so i won't paste plain text version here.
>
> i have some comments at the bottom. Excerpt:
>
> --
>
> «One thing interesting is to compare the approaches in perl and emacs
> lisp.»
>
> «For our case, regex is not powerful enough to deal with the problem
> by itself, due to the nested nature of html. This is why, in my perl
> code, i split the file by < into segments first, then, use regex to
> deal with now the non-nested segment. This will break if you have  title="x < href=z" href="math.html">math. This cannot be worked
> around unless you really start to write a real parser.»
>
> «The elisp here is more powerful, not because of any lisp features,
> but because emacs's buffer datatype. You can think of it as a
> glorified string datatype, that you can move a cursor back and forth,
> or use regex to search forward or backward, or save cursor positions
> (index) and grab parts of text for further analysis.»
>
> --
>
> If you are a perl coder, and disagree, let me know your opinion.
> (showing working code is very welcome) My comment about perl there
> applies to python too. (python code welcome too.)

Interesting.

Perl, Python, and Lisp have real HTML parsers available.
I've used the ones for Perl and Python.

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


Re: first X digits of pi

2012-04-13 Thread Jabba Laci
Hi,

Thanks for the answers. Gibbons' algorithm (from 2006) is a nice way
to generate the digits one after the other. However, it can get slow.
The mpmath approach is very fast, I think I will use that one. In a
script you can get the value of pi as a string with

str(mp.pi)

Best,

Laszlo

On Fri, Apr 13, 2012 at 15:56, Emanuel Woiski  wrote:
> Easy. Use mpmath (alone or under sympy):
>
 from sympy.mpmath import mp
 mp.dps = 20
 +mp.pi
>
> ... and there you are: 20 digits of pi :)
>
> regards
> woiski
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Emacs Lisp vs Perl: Validate Local File Links

2012-04-13 Thread David Lam
Xah Lee

#1 mailing list troll   =D

On Fri, Apr 13, 2012 at 10:35 AM, Xah Lee  wrote:

> 〈Emacs Lisp vs Perl: Validate Local File Links〉
> http://xahlee.org/emacs/elisp_vs_perl_validate_links.html
>
> a comparison of 2 scripts.
>
> lots code, so i won't paste plain text version here.
>
> i have some comments at the bottom. Excerpt:
>
> --
>
> «One thing interesting is to compare the approaches in perl and emacs
> lisp.»
>
> «For our case, regex is not powerful enough to deal with the problem
> by itself, due to the nested nature of html. This is why, in my perl
> code, i split the file by < into segments first, then, use regex to
> deal with now the non-nested segment. This will break if you have  title="x < href=z" href="math.html">math. This cannot be worked
> around unless you really start to write a real parser.»
>
> «The elisp here is more powerful, not because of any lisp features,
> but because emacs's buffer datatype. You can think of it as a
> glorified string datatype, that you can move a cursor back and forth,
> or use regex to search forward or backward, or save cursor positions
> (index) and grab parts of text for further analysis.»
>
> --
>
> If you are a perl coder, and disagree, let me know your opinion.
> (showing working code is very welcome) My comment about perl there
> applies to python too. (python code welcome too.)
>
>  Xah
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Python one-liner?

2012-04-13 Thread Evan Driscoll
I have a function 'f' and a list 'l'. I want a dictionary where the keys
are evaluations of 'f(thing from l)' and the values are lists of stuff
from 'l' that matches. So for instance, if 'f = lambda x: x%3' and
'l=range(9)', then I want { 0: [0,3,6], 1:[1,4,7], 2:[2,5,8]}.

I can do that with an explicit loop, or with this suggestion (untested)

d = {}
def appender(e):
d.get(f(e), []).append(e)
map(appender, l)

but I was wondering if there's some prettier way to do it with some
fancy dictionary comprehension or itertools expression or something. At
this point I'm asking mostly out of curiosity and less for some code to
actually use, so either Python 2 or 3 is acceptable (but I'm writing in
3 at the moment).

Evan




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


Re: Python one-liner?

2012-04-13 Thread Evan Driscoll
On 4/13/2012 22:33, Evan Driscoll wrote:
> d = {}
> def appender(e):
> d.get(f(e), []).append(e)
> map(appender, l)

Just in case it isn't clear, the above has at least two problems and
won't even come close to working. :-)


Though I might as well ask another question... if I have a dict with
values which are lists, what's a good way to say "append x to the list
at key k, creating a list if it's not there"? dict.setdefault seems
potentially promising but the docs are crappy and don't say how it works.

Evan




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


Re: Python one-liner?

2012-04-13 Thread Evan Driscoll
On 4/13/2012 22:42, Evan Driscoll wrote:
> Though I might as well ask another question... if I have a dict with
> values which are lists, what's a good way to say "append x to the list
> at key k, creating a list if it's not there"? dict.setdefault seems
> potentially promising but the docs are crappy and don't say how it works.
Ha ha, sorry I can't read right now apparently. dict.setdefault does
exactly what I wanted.

(The name just prompted another interpretation in my mind which doesn't
work and I got tunnel vision. I'll stop spamming now, and we return to
your regularly scheduled broadcast.)

Evan





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


Re: Python one-liner?

2012-04-13 Thread Chris Angelico
On Sat, Apr 14, 2012 at 1:44 PM, Evan Driscoll  wrote:
> Ha ha, sorry I can't read right now apparently. dict.setdefault does
> exactly what I wanted.
>
> (The name just prompted another interpretation in my mind which doesn't
> work and I got tunnel vision. I'll stop spamming now, and we return to
> your regularly scheduled broadcast.)

Yeah, it's an oddly named method. But I can't think of any name for it
that isn't odd.

I was just writing up a reply when your third message came in, but
there's one small aspect that's still worth mentioning, so I'll quote
it.



Yes, that would be the right method to use. I'd not bother with the
function and map() though, and simply iterate:

d = {}
for val in l:
 d.setdefault(f(val), []).append(val)

Enjoy!

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


Re: Deep merge two dicts?

2012-04-13 Thread John O'Hagan
On Fri, 13 Apr 2012 10:50:15 -0600
Ian Kelly  wrote:

> On Fri, Apr 13, 2012 at 5:11 AM, John O'Hagan  wrote:
> > I think you also have to check if a[k] is a dict before making the recursive
> > call, else for example dmerge({'a': 1}, {'a': {'b': 1}}) fails with a
> > TypeError. In that case the third line above should read:
> >
> >    if k in a and isinstance(a[k], dict) and isinstance(v, dict):
> 
> Okay, but then what do you do in that case?  You can't merge a dict
> into an int.  Unless the OP has some specific type conflict semantics
> in mind, the above *should* raise a TypeError, because in the above
> you have passed in two structures that are incompatible for merging.

I had assumed it should work like dict.update, but deeply. Following the link
provided by the OP, I see your point: the merge function described there has
various precedence/conflict-handling options, of which my assumption is only
one. Another is to enforce the same nesting structure for both arguments, as
yours does.

As an aside, I'm not entirely clear on the distinction between merge and
update; for example, should a merge return a new object? 

Regards,

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