Re: Myth Busters: % "this old style of formatting will eventually be removed from the language"

2013-05-22 Thread nn
On May 22, 6:35 am, Skip Montanaro  wrote:
> >> Is this tutorial outdated or this still an issue?
>
> >> [1]
> >>http://docs.python.org/2/tutorial/inputoutput.html#old-string-formatting
>
> > That tutorial is out of date.  %-formatting isn't being removed.
>
> OTOH, PEP 3101 also mentions deprecation, at the very end: "... both
> systems can co-exist until it comes time to deprecate the older
> system."
>
> I have been operating under the assumption since the days of that PEP
> that %-style formatting would eventually disappear, dreading the day
> when I'd have to learn the str.format language.  I apologize for
> (inadvertently) spreading FUD.
>
> It does seem like the documentation should be updated in a few places.
>  If the decision has been made to not remove the older system, it
> might be worthwhile to mention that somewhere.  Clearly the tutorial
> and PEP 3101 should be updated.
>
> Skip

I was of the impression that deprecating % was still planned for Py4k,
whenever that is. I personally believe that eliminating % formatting
would reduce bugs and generally make code clearer, but I realize that
breaking backward compatibility has a high price.

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


Re: PEP 378: Format Specifier for Thousands Separator

2013-05-22 Thread nn
On May 22, 2:30 pm, Ned Batchelder  wrote:
> On 5/22/2013 10:58 AM, Steven D'Aprano wrote:
>
> > On Wed, 22 May 2013 05:45:12 -0500, Skip Montanaro wrote:
>
> >> I didn't mean to create a tempest in a teapot.  I was away from
> >> comp.lang.python, python-bugs, and python-dev for a few years.  In
> >> particular, I didn't ever see the aforementioned thread from Feb 2012.
> >>   Had I known of that thread I would have worded the sentence which
> >> shall not be repeated differently.
>
> >> My apologies...
> > No problem, it's not about you specifically, it's just that some of us
> > fans of % formatting can be a tad sensitive about it, especially since
> > the idea that it has been deprecated (or soon will be deprecated, or one
> > day will be deprecated, and therefore code using it is bad) is relatively
> > widespread on the Internet.
>
> Seems like maybe this should become a question in the Python FAQ.
>
> --Ned.
>
>
>
>
>
>
>
>
>
> > Glad to have you back here!

Maybe a cformat(formatstring, variables)  function should be created
in the string module so people who prefer that can use it. I don't
mind the C formatting syntax but I don't like the fact that the %
operator does something totally different when the first variable is
an integer and the fact that it misbehaves if the second variable is a
tuple.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 378: Format Specifier for Thousands Separator

2013-05-23 Thread nn
On May 22, 6:31 pm, Carlos Nepomuceno 
wrote:
> 
>
> > Date: Wed, 22 May 2013 13:26:23 -0700
> > Subject: Re: PEP 378: Format Specifier for Thousands Separator
> > From: prueba...@latinmail.com
> > To: python-l...@python.org
> [...]
>
> > Maybe a cformat(formatstring, variables) function should be created
> > in the string module so people who prefer that can use it. I don't
> > mind the C formatting syntax but I don't like the fact that the %
> > operator does something totally different when the first variable is
> > an integer and the fact that it misbehaves if the second variable is a
> > tuple.
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> I still don't understand why % benefits from literals optimization 
> ("'%d'%12345") while '{:d}'.format(12345) doesn't.
>
> What "totally different" you talking about? Please give me an example.

>>> def eggs(spam, ham): return spam % ham

>>> def milk(beef, steak): return beef.format(steak)

>>> a='%s'
>>> c=9
>>> d=4
>>> e=[1,2]
>>> f=(3,5)
>>> d='{}'

>>> eggs(a,4)
'4'
>>> eggs(c,4)
1
>>> eggs(a,e)
'[1, 2]'
>>> eggs(a,f)
Traceback (most recent call last):
  File "", line 1, in 
eggs(a,f)
  File "", line 1, in eggs
def eggs(spam, ham): return spam % ham
TypeError: not all arguments converted during string formatting
>>> '%s'%(5%3)
'2'

>>> milk(d,4)
'4'
>>> milk(c,4)
Traceback (most recent call last):
  File "", line 1, in 
milk(c,4)
  File "", line 1, in milk
def milk(beef, steak): return beef.format(steak)
AttributeError: 'int' object has no attribute 'format'
>>> milk(d,e)
'[1, 2]'
>>> milk(d,f)
'(3, 5)'
>>> '{}'.format(5%3)
'2'

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


Re: PEP 378: Format Specifier for Thousands Separator

2013-05-24 Thread nn
On May 23, 2:42 pm, Dave Angel  wrote:
> On 05/23/2013 11:26 AM, Carlos Nepomuceno wrote:
>
> > 
> >> Date: Thu, 23 May 2013 06:44:05 -0700
> >> Subject: Re: PEP 378: Format Specifier for Thousands Separator
> >> From: prueba...@latinmail.com
> >> To: python-l...@python.org
> > [...]
>
> You left out the part where a and f are initialized:
>
>  >>> a='%s'
>  >>> f=(3,5)
>
> > eggs(a,f)
> >> Traceback (most recent call last):
> >> File "", line 1, in 
> >> eggs(a,f)
> >> File "", line 1, in eggs
> >> def eggs(spam, ham): return spam % ham
> >> TypeError: not all arguments converted during string formatting
> > '%s'%(5%3)
> >> '2'
>
> > So % doesn't handle tuples! Why's that? Is it intentional (by design)?
>
> It's a conflict in the design.  A tuple is used to supply multiple
> arguments to the % operator.  So if you want to have a tuple as the
> first argument, you need to enclose it in another tuple.
>
> try the following:
>
> print a % (f,)
>
> The trouble is, it doesn't generalize very readily, so it's difficult to
> use in a function like eggs()
>
> --
> DaveA

It's all there, it's just that quoting ate it. Let's try this again:

>>> def eggs(spam, ham): return spam % ham
>>> def milk(beef, steak): return beef.format(steak)
>>> a='%s'
>>> c=9
>>> d=4
>>> e=[1,2]
>>> f=(3,5)
>>> d='{}'
>>> eggs(a,4)
'4'
>>> eggs(c,4)
1
>>> eggs(a,e)
'[1, 2]'
>>> eggs(a,f)

Traceback (most recent call last):
  File "", line 1, in 
eggs(a,f)
  File "", line 1, in eggs
def eggs(spam, ham): return spam % ham
TypeError: not all arguments converted during string formatting
>>> '%s'%(5%3)
'2'

>>> milk(d,4)
'4'
>>> milk(c,4)

Traceback (most recent call last):
  File "", line 1, in 
milk(c,4)
  File "", line 1, in milk
def milk(beef, steak): return beef.format(steak)
AttributeError: 'int' object has no attribute 'format'
>>> milk(d,e)
'[1, 2]'
>>> milk(d,f)
'(3, 5)'
>>> '{}'.format(5%3)
'2'

The three issues represented:
1. Functions in which both values of the operator are parameters might
not return a string but an integer instead. It is not always
immediately obvious when reading such functions if modulus or
formatting is intended.
2. Function doesn't handle tuple properly unless carefully written
3. Too much % noise because % is used for 3 things: the special
placeholder character inside format strings, the format operator and
the modulus operator.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fatal Python error

2013-05-31 Thread nn
On May 29, 10:05 am, Joshua Landau  wrote:
> On 29 May 2013 14:02, Dave Angel  wrote:
>
> > On 05/29/2013 08:45 AM, Oscar Benjamin wrote:
> > Joshua:  Avoid doing anything complex inside an exception handler.
>
> Unfortunately, Ranger (the file manager in question) wraps a lot of stuff
> in one big exception handler. Hence there isn't much choice. The original
> wasn't actually in an infinite recursion, too, but just a recursion over a
> large directory.
>
> Is there a reason that Python 3 can't be made to work like Python 2 and
> PyPy, and -if not- should it? The catchable fail would be much nicer than
> just bombing the program.
>
> In the meantime the algorithm should just be reworked, but it seems like a
> larger step than should be needed.
>
> If nothing else, the exception frame is huge.  I probably would have
>
> > spotted it except for the indentation problem triggered by html.  The top
> > level code following your function didn't have any loops, so it wasn't a
> > problem.
>
> > Can anyone help Joshua put his gmail into text mode?
>
> I've found a new option. As a test, here's a simplified version without the
> property:
>
> def loop():
>     try:
>         (lambda: None)()
>     except:
>         pass
>
>     loop()
>
> try:
>     loop()
> except RuntimeError:
>     pass
>
> which is pretty much Oscar Benjamin's, but less stupid.

If nobody else has, I would recommend you submit a bug at
bugs.python.org.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: print or write on a text file ?

2012-10-01 Thread nn
On Sep 28, 2:42 pm, Franck Ditter  wrote:
> Hi !
> Here is Python 3.3
> Is it better in any way to use print(x,x,x,file='out')
> or out.write(x) ? Any reason to prefer any of them ?
> There should be a printlines, like readlines ?
> Thanks,
>
>     franck

There is

out.writelines(lst)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python math is off by .000000000000045

2012-02-22 Thread nn
On Feb 22, 1:13 pm, Alec Taylor  wrote:
> Simple mathematical problem, + and - only:
>
> >>> 1800.00-1041.00-555.74+530.74-794.95
>
> -60.9500045
>
> That's wrong.
>
> Proofhttp://www.wolframalpha.com/input/?i=1800.00-1041.00-555.74%2B530.74-...
> -60.95 aka (-(1219/20))
>
> Is there a reason Python math is only approximated? - Or is this a bug?
>
> Thanks for all info,
>
> Alec Taylor

I get the right answer if I use the right datatype:

>>> import decimal

>>> D=decimal.Decimal

>>> D('1800.00')-D('1041.00')-D('555.74')+D('530.74')-D('794.95')

Decimal('-60.95')

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


Re: Python-URL! - weekly Python news and links (Mar 31)

2012-04-02 Thread nn
On Mar 31, 11:38 am, Cameron Laird  wrote:
> I pine for the fjords.
>
> And it's time to bring "Python-URL!" to a close.  "Python-URL!", which
> Jean-Claude Wippler and I appear to have launched in 1998, has reached
> the end of its utility.  We still have many loyal and enthusiastic
> readers--one subscription request arrived within the last day, in
> fact--and certainly much writing turns up every week that *deserves*
> the spotlight "Python-URL!" has shone in the past.
>
> However, the Python world has changed a great deal over the last
> fourteen years.  There are many, MANY other ways for those with an
> interest in Python to nourish themselves, and Python itself has grown
> and "normalized" so much that it no longer fits particularly well in
> the "Python-URL!" format.  Enjoy "Mouse vs. Python" 
> http://www.blog.pythonlibrary.org/>, the Python areas of DZone,
> Reddit, developerWorks, stackoverflow, and so on.
>
> For your reference, I append below the most-recent-but-not-
> particularly-
> current version of "Python-URL!"'s coda of related readings.
>
> That is all.
>
> 
> Everything Python-related you want is probably one or two clicks away
> in
> these pages:
>
>     Python.org's Python Language Website is the traditional
>     center of Pythonia
>        http://www.python.org
>     Notice especially the master FAQ
>        http://www.python.org/doc/FAQ.html
>
>     Just beginning with Python?  This page is a great place to start:
>        http://wiki.python.org/moin/BeginnersGuide/Programmers
>
>     Planet Python:  you want to visit there:
>        http://planet.python.org
>     But don't confuse it with Planet SciPy:
>        http://planet.scipy.org
>     And don't confuse *that* with SciPyTip, a high-quality daily (!)
> tip
>     for the numerically-inclined:
>        http://twitter.com/SciPyTip
>
>     Python Insider is the official blog of the Python core development
>     team:
>        http://pyfound.blogspot.com/2011/03/python-dev-launches-python-insider
> -blog.html
>
>     The Python Software Foundation (PSF) has replaced the Python
>     Consortium as an independent nexus of activity.  It has official
>     responsibility for Python's development and maintenance.
>        http://www.python.org/psf/
>     Among the ways you can support PSF is with a donation.
>        http://www.python.org/psf/donations/
>     Keep up with the PSF at "Python Software Foundation News":
>        http://pyfound.blogspot.com
>
>     The Python Papers aims to publish "the efforts of Python
> enthusiasts":
>        http://pythonpapers.org/
>
>     Doug Hellman's "Module of the week" is essential reading:
>        http://www.doughellmann.com/PyMOTW/
>
>     comp.lang.python.announce announces new Python software.  Be
>     sure to scan this newsgroup weekly.
>        http://groups.google.com/group/comp.lang.python.announce/topics
>
>     Python411 indexes "podcasts ... to help people learn Python ..."
>     Updates appear more-than-weekly:
>        http://www.awaretek.com/python/index.html
>
>     The Python Package Index catalogues packages.
>        http://www.python.org/pypi/
>
>     Much of Python's real work takes place on Special-Interest Group
>     mailing lists
>        http://www.python.org/sigs/
>
>     Python Success Stories--from air-traffic control to on-line
>     match-making--can inspire you or decision-makers to whom you're
>     subject with a vision of what the language makes practical.
>        http://www.pythonology.com/success
>
>     The Summary of Python Tracker Issues is an automatically generated
>     report summarizing new bugs, closed ones, and patch submissions.
>        http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.c
> omp.python.devel&sort=date
>
>     nullege is an interesting search Web application, with the
> intelligence
>     to distinguish between Python code and comments.  It provides what
>     appear to be relevant results, and demands neither Java nor CSS be
>     enabled:
>        http://www.nullege.com
>
>     Although unmaintained since 2002, the Cetus collection of Python
>     hyperlinks retains a few gems.
>        http://www.cetus-links.org/oo_python.html
>
>     The Cookbook is a collaborative effort to capture useful and
>     interesting recipes:
>        http://code.activestate.com/recipes/langs/python/
>
>     Many Python conferences around the world are in preparation.
>     Watch this space for links to them.
>
>     Among several Python-oriented RSS/RDF feeds available, see:
>        http://www.python.org/channews.rdf
>     For more, see:
>        http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all
>     The old Python "To-Do List" now lives principally in a
>     SourceForge reincarnation.
>        http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse
>        http://www.python.org/dev/peps/pep-0042/
>
>     del.icio.us presen

Re: Is there a better way to do this snippet?

2012-04-03 Thread nn
On Apr 3, 11:02 am, Alain Ketterlin 
wrote:
> python  writes:
> > tag23gr is a list of lists each with two items.
> > g23tag is an empty dictionary when I run the for loop below.
> > When is is complete each key is a graphic name who's values are a list
> > of tags.
>
> > for item in tag23gr:
> > ...        value, key = tuple(item)
> > ...        if(g23tag.get(key)):
> > ...                g23tag[key].append(value)
> > ...        else:
> > ...                g23tag[key] = [value]
>
> for item in tag23gr:
>     g23tag.setdefault(item[0],[]).append(item[1])
>
> -- Alain.

Or alternatively:

from collections import defaultdict
g23tag = defaultdict(list)
for item in tag23gr:
g23tag[item[0]].append(item[1])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a better way to do this snippet?

2012-04-03 Thread nn
On Apr 3, 12:26 pm, Alain Ketterlin 
wrote:
> nn  writes:
> >> > for item in tag23gr:
> >> > ...        value, key = tuple(item)
> >> > ...        if(g23tag.get(key)):
> >> > ...                g23tag[key].append(value)
> >> > ...        else:
> >> > ...                g23tag[key] = [value]
>
> >> for item in tag23gr:
> >>     g23tag.setdefault(item[0],[]).append(item[1])
> > Or alternatively:
>
> > from collections import defaultdict
> > g23tag = defaultdict(list)
> > for item in tag23gr:
> > g23tag[item[0]].append(item[1])
>
> Very handy in that case, but in general I dislike the idea of silently
> inserting a default value when the access is a read, e.g., in
> x=g23tag[wrung]. Explicit is better than implicit, as they say. YMMV.
>
> -- Alain.

Valid point. Preferred choice depends on the access patterns to the
dict (e.g. one write and multiple reads, multiple writes and one loop
over items, etc.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to count the total number of strings (in the list) used in Python?

2012-04-17 Thread nn
On Apr 16, 3:00 pm, Chinesekidz  wrote:
> Hello!
>
> I would like to know how to write the program to count the total
> number of strings (in the list) used in Python..
>
> for example:
>
> list:['1','2','3','4']
>
> for l in range(4):
>      num=input("list:"+list[l]+"(between 1 and 4):")
>      if num.isdigit:
>         tnum=tnum+int(num)
>
> print("Total number of list used:",???)
>
> I want the output to be:
>
> list 1 (between 1 and 4): 2
> list 2 (between 1 and 4): 5
> list 3 (between 1 and 4): 6
> list 4 (between 1 and 4):5
> list 1 (between 1 and 4):4
>
> Total number of list used: 5
>
> in the case above, it is to show the how many times of strings from
> the list has been used...
>
> I would like to know how to write the code for this...
>
> Thanks in advance

I didn't understand the question. If you don't get any further answers
on the list it is because nobody else understood the question either.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Working with Cursors

2012-04-17 Thread nn
On Apr 17, 2:11 pm, timlash  wrote:
> Searched the web and this forum without satisfaction.  Using Python 2.7 and 
> pyODBC on Windows XP I can get the code below to run and generate two cursors 
> from two different databases without problems.  Ideally, I'd then like to 
> join these result cursors thusly:
>
> SELECT a.state, sum(b.Sales) FROM cust_curs a INNER JOIN fin_curs b ON 
> a.Cust_id = b.Cust_id GROUP BY a.state
>
> Is there a way to join cursors using SQL statements in python or pyODBC?  
> Would I need to store these cursors in a common DB (SQLite3?) to accomplish 
> this?  Is there a pure python data handling approach that would generate this 
> summary from these two cursors?
>
> Thanks for your consideration.
>
> Working code:
>
> import pyodbc
>
> #
> # DB2 Financial Data Cursor
> #
> cnxn = pyodbc.connect('DSN=DB2_Fin;UID=;PWD=')
> fin_curs = cnxn.cursor()
>
> fin_curs.execute("""SELECT Cust_id, sum(Sales) as Sales
>                         FROM Finance.Sales_Tbl
>                         GROUP BY Cust_id""")
>
> #
> # Oracle Customer Data Cursor
> #
> cnxn = pyodbc.connect('DSN=Ora_Cust;UID=;PWD=')
> cust_curs = cnxn.cursor()
>
> cust_curs.execute("""SELECT Distinct Cust_id, gender, address, state
>                         FROM Customers.Cust_Data""")

If any of the two cursors fits in memory you could store it in a dict
and then look the extra data up as you traverse the second one. E.g.

>sales = {}
>for fin_curs_row in fin_curs:
>fin_cust_id,sales = fin_curs_row
>sales[fin_cust_id] = sales
>for cust_curs_row in cust_curs:
>cust_cust_id = cust_curs_row[0]
>print cust_curs_row, sales[cust_cust_id]

If you can't make it fit in memory, one way would be to order both
cursors by customer_id and pair them up as they come along:

>fin_curs_row = fin_curs.next()
>cust_curs_row = cust_curs.next()
>While True:
>fin_cust_id,sales = fin_curs_row
>cust_cust_id = cust_curs_row[0]
>try:
>if fin_cust_id == cust_cust_id:
>print cust_curs_row, sales
>fin_curs_row = fin_curs.next()
>elif fin_cust_id > cust_cust_id:
>cust_curs_row = cust_curs.next()
>else:
>fin_curs_row = fin_curs.next()
>except StopIteration:
>break


In the end if speed is not an issue, just loading everthing in SQLite
and doing the join there makes it so simple that anybody should be
able to maintain the code, so that is also a good choice.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Working with Cursors

2012-04-17 Thread nn
On Apr 17, 2:11 pm, timlash  wrote:
> Searched the web and this forum without satisfaction.  Using Python 2.7 and 
> pyODBC on Windows XP I can get the code below to run and generate two cursors 
> from two different databases without problems.  Ideally, I'd then like to 
> join these result cursors thusly:
>
> SELECT a.state, sum(b.Sales) FROM cust_curs a INNER JOIN fin_curs b ON 
> a.Cust_id = b.Cust_id GROUP BY a.state
>
> Is there a way to join cursors using SQL statements in python or pyODBC?  
> Would I need to store these cursors in a common DB (SQLite3?) to accomplish 
> this?  Is there a pure python data handling approach that would generate this 
> summary from these two cursors?
>
> Thanks for your consideration.
>
> Working code:
>
> import pyodbc
>
> #
> # DB2 Financial Data Cursor
> #
> cnxn = pyodbc.connect('DSN=DB2_Fin;UID=;PWD=')
> fin_curs = cnxn.cursor()
>
> fin_curs.execute("""SELECT Cust_id, sum(Sales) as Sales
>                         FROM Finance.Sales_Tbl
>                         GROUP BY Cust_id""")
>
> #
> # Oracle Customer Data Cursor
> #
> cnxn = pyodbc.connect('DSN=Ora_Cust;UID=;PWD=')
> cust_curs = cnxn.cursor()
>
> cust_curs.execute("""SELECT Distinct Cust_id, gender, address, state
>                         FROM Customers.Cust_Data""")

If any of the two cursors fits in memory you could store it in a dict
and then look the extra data up as you traverse the second one. E.g.

>sales = {}
>for fin_curs_row in fin_curs:
>fin_cust_id,sales = fin_curs_row
>sales[fin_cust_id] = sales
>for cust_curs_row in cust_curs:
>cust_cust_id = cust_curs_row[0]
>print cust_curs_row, sales[cust_cust_id]

If you can't make it fit in memory, one way would be to order both
cursors by customer_id and pair them up as they come along:

>fin_curs_row = fin_curs.next()
>cust_curs_row = cust_curs.next()
>While True:
>fin_cust_id,sales = fin_curs_row
>cust_cust_id = cust_curs_row[0]
>try:
>if fin_cust_id == cust_cust_id:
>print cust_curs_row, sales
>fin_curs_row = fin_curs.next()
>elif fin_cust_id > cust_cust_id:
>cust_curs_row = cust_curs.next()
>else:
>fin_curs_row = fin_curs.next()
>except StopIteration:
>break


In the end if speed is not an issue, just loading everthing in SQLite
and doing the join there makes it so simple that anybody should be
able to maintain the code, so that is also a good choice.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the most efficient way to find similarities and differences between the contents of two lists?

2011-06-13 Thread nn
On Jun 13, 11:06 am, Zachary Dziura  wrote:
> Hi all.
>
> I'm writing a Python script that will be used to compare two database
> tables. Currently, those two tables are dumped into .csv files,
> whereby my code goes through both files and makes comparisons. Thus
> far, I only have functionality coded to make comparisons on the
> headers to check for similarities and differences. Here is the code
> for that functionality:
>
> similar_headers = 0
> different_headers = 0
> source_headers = sorted(source_mapping.headers)
> target_headers = sorted(target_mapping.headers)
>
> # Check if the headers between the two mappings are the same
> if set(source_headers) == set(target_headers):
>     similar_headers = len(source_headers)
> else:
>     # We're going to do two run-throughs of the tables, to find the
>     # different and similar header names. Start with the source
>     # headers...
>     for source_header in source_headers:
>         if source_header in target_headers:
>             similar_headers += 1
>         else:
>             different_headers += 1
>     # Now check target headers for any differences
>     for target_header in target_headers:
>         if target_header in source_headers:
>             pass
>         else:
>             different_headers += 1
>
> As you can probably tell, I make two iterations: one for the
> 'source_headers' list, and another for the 'target_headers' list.
> During the first iteration, if a specific header (mapped to a variable
> 'source_header') exists in both lists, then the 'similar_headers'
> variable is incremented by one. Similarly, if it doesn't exist in both
> lists, 'different_headers' is incremented by one. For the second
> iteration, it only checks for different headers.
>
> My code works as expected and there are no bugs, however I get the
> feeling that I'm not doing this comparison in the most efficient way
> possible. Is there another way that I can make this same comparison
> while making my code more Pythonic and efficient? I would prefer not
> to have to install an external module from elsewhere, though if I have
> to then I will.
>
> Thanks in advance for any and all answers!

how about:

# Check if the headers between the two mappings are the same
source_headers_set = set(source_headers)
target_headers_set = set(target_headers)

similar_headers = len(source_headers_set & target_headers_set)
different_headers = len(source_headers_set ^ target_headers_set)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Implicit initialization is EVIL!

2011-07-05 Thread nn
On Jul 4, 11:35 am, Robin Becker  wrote:
> On 03/07/2011 23:21, Chris Angelico wrote:
> .
>
> > var(0x14205359) x   # Don't forget to provide an address where the
> > object will be located
> > x=42
>
> 
> did you forget to specify the memory bank and computer (and presumably planet
> etc etc)
> -molly-coddled-ly yrs-
> Robin Becker

Ah, I see we have a mainframe programmer among us ... :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: testing if a list contains a sublist

2011-08-16 Thread nn
On Aug 16, 8:23 am, Alain Ketterlin 
wrote:
> Roy Smith  writes:
> >> what is the best way to check if a given list (lets call it l1) is
> >> totally contained in a second list (l2)?
>
> [...]
>
> > import re
>
> > def sublist(l1, l2):
> >     s1 = ''.join(map(str, l1))
> >     s2 = ''.join(map(str, l2))
> >     return re.search(s1, s2)
>
> This is complete nonsense (try it on [12] and [1,2]).
>
> The original problem is "string searching" (where strings here are
> sequences of numbers instead of characters). See
>
> http://en.wikipedia.org/wiki/String_searching_algorithm
>
> for any algorithm (Rabin-Karp seems appropriate to me).
>
> -- Alain.

That can be easily fixed:

>>> def sublist(lst1, lst2):
s1 = ','.join(map(str, lst1))
s2 = ','.join(map(str, lst2))
return False if s2.find(s1)==-1 else True

>>> sublist([1,2],[1,2,3,4,5])
True
>>> sublist([1,2,2],[1,2,3,4,5])
False
>>> sublist([1,2,3],[1,3,5,7])
False
>>> sublist([12],[1,2])
False
>>>

I don't know about best, but it works for the examples given.

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


Re: Fun with 'str' and 'bytes'

2011-03-04 Thread nn
On Mar 4, 7:32 am, "Frank Millman"  wrote:
> Hi all
>
> I want to create a cookie containing a session id. In python 2.6 I had the
> following -
>
> from __future__ import unicode_literals
> session_id = b64encode(urandom(20))
> response_headers.append(
>     (b'Set-Cookie', b'sid="{0}"'.format(session_id)))
>
> After upgrading to 3.2, the above lines generate this traceback -
>     AttributeError: 'bytes' object has no attribute 'format'
>
> The best workaround I can come up with is the following -
>
> session_id = b64encode(urandom(20))
> response_headers.append(
>     (b'Set-Cookie', b'sid="' + session_id + b'"'))
>
> It works, but it is not pretty. Is there a more elegant solution?
>
> Thanks
>
> Frank Millman

As far as I know, that is pretty much it. Also see:

http://bugs.python.org/issue3982
http://mail.python.org/pipermail/python-dev/2010-July/102252.html
http://lucumr.pocoo.org/2010/5/25/wsgi-on-python-3/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the best book to learn Python from Perl and C++ background

2011-03-04 Thread nn
On Mar 4, 12:49 pm, Ignoramus20691  wrote:
> I bought a "Hello World!" book for my 9 year old son. The book teached
> "programming for kids" and it does it in Python.
>
> I do not know any Python, but I am very comfortable with C++ and perl.
> I wrote a little over 100k lines of perl.
>
> I want to learn Python quickly to help him with his studies/fun.
>
> I would prefer a "no shit" book that quickly teaches a language to
> programmers.
>
> Any suggestions?
>
> i

The included ~12 page tutorial was all I needed; the rest was just
looking up stuff in the standard documentation and messing around at
the interactive prompt:

http://docs.python.org/tutorial/index.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: better way to do this in python

2011-04-04 Thread nn
On Apr 3, 8:06 am, Mag Gam  wrote:
> Thanks for the responses.
>
> Basically, I have a large file with this format,
>
> Date INFO username command srcipaddress filename
>
> I would like to do statistics on:
> total number of usernames and who they are
> username and commands
> username and filenames
> unique source ip addresses
> unique filenames
>
> Then I would like to bucket findings with days (date).
>
> Overall, I would like to build a log file analyzer.
>
>
>
>
>
>
>
> On Sat, Apr 2, 2011 at 10:59 PM, Dan Stromberg  wrote:
>
> > On Sat, Apr 2, 2011 at 5:24 PM, Chris Angelico  wrote:
>
> >> On Sun, Apr 3, 2011 at 9:58 AM, Mag Gam  wrote:
> >> > I suppose I can do something like this.
> >> > (pseudocode)
>
> >> > d={}
> >> > try:
> >> >  d[key]+=1
> >> > except KeyError:
> >> >  d[key]=1
>
> >> > I was wondering if there is a pythonic way of doing this? I plan on
> >> > doing this many times for various files. Would the python collections
> >> > class be sufficient?
>
> >> I think you want collections.Counter. From the docs: "Counter objects
> >> have a dictionary interface except that they return a zero count for
> >> missing items instead of raising a KeyError".
>
> >> ChrisA
>
> > I realize you (Mag) asked for a Python solution, but since you mention
> > awk... you can also do this with "sort < input | uniq -c" - one line of
> > "code".  GNU sort doesn't use as nice an algorithm as a hashing-based
> > solution (like you'd probably use with Python), but for a sort, GNU sort's
> > quite good.
>
> > --
> >http://mail.python.org/mailman/listinfo/python-list

Take a look at:
http://code.activestate.com/recipes/577535-aggregates-using-groupby-defaultdict-and-counter/

for some ideas of how to group and count things.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: replace regex in file using a dictionary

2011-04-05 Thread nn
On Apr 5, 3:59 am, Martin De Kauwe  wrote:
> Hi,
>
> So i want to replace multiple lines in a text file and I have reasoned
> the best way to do this is with a dictionary. I have simplified my
> example and broadly I get what I want however I am now printing my
> replacement string and part of the original expression. I am guessing
> that I will need to use a more complicated expression than I am
> currently am to achieve what I want?
>
> my aim is to match the expression before the "=" and replace the
> string after the equals...
>
> My failed example...
>
> def replace_keys(text, replacements_dict):
>     for key, var in replacements_dict.iteritems():
>         replacement = "%s = %s" % (key, var)
>         text = text.replace(key, replacement)
>     return text
>
> # example of two lines of the file
> str = \
> """structcn = 150.0
> metfrac0 = 0.85
> """
>
> # replacements
> replacement_dict = {"structcn": "999.0", "metfrac0": "0.85"}
> new_str = replace_keys(str, replacement_dict)
>
> print str
> print
> print new_str
>
> which produces...
>
> structcn = 150.0
> metfrac0 = 0.85
>
> structcn = 999.0 = 150.0
> metfrac0 = 0.85 = 0.85
>
> thanks.

If the structure is very regular you can use something like this:

def replace_keys(text, replacements_dict):
lines=text.splitlines()
for i, row in enumerate(lines):
key, sep, val = row.split()
lines[i]=" ".join(
(key, sep, replacement_dict.get(key, val)))
return '\n'.join(lines)+'\n'
-- 
http://mail.python.org/mailman/listinfo/python-list


Finding empty columns. Is there a faster way?

2011-04-21 Thread nn
time head -100 myfile  >/dev/null

real0m4.57s
user0m3.81s
sys 0m0.74s

time ./repnullsalt.py '|' myfile
0 1 Null columns:
11, 20, 21, 22, 23, 24, 25, 26, 27, 30, 31, 33, 45, 50, 68

real1m28.94s
user1m28.11s
sys 0m0.72s



import sys
def main():
with open(sys.argv[2],'rb') as inf:
limit = sys.argv[3] if len(sys.argv)>3 else 1
dlm = sys.argv[1].encode('latin1')
nulls = [x==b'' for x in next(inf)[:-1].split(dlm)]
enum = enumerate
split = bytes.split
out = sys.stdout
prn = print
for j, r in enum(inf):
if j%100==0:
prn(j//100,end=' ')
out.flush()
if j//100>=limit:
break
for i, cur in enum(split(r[:-1],dlm)):
nulls[i] |= cur==b''
print('Null columns:')
print(', '.join(str(i+1) for i,val in enumerate(nulls) if val))

if not (len(sys.argv)>2):
sys.exit("Usage: "+sys.argv[0]+
 "   ")

main()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding empty columns. Is there a faster way?

2011-04-22 Thread nn
On Apr 21, 4:32 pm, Jon Clements  wrote:
> On Apr 21, 5:40 pm, nn  wrote:
>
>
>
>
>
>
>
>
>
> > time head -100 myfile  >/dev/null
>
> > real    0m4.57s
> > user    0m3.81s
> > sys     0m0.74s
>
> > time ./repnullsalt.py '|' myfile
> > 0 1 Null columns:
> > 11, 20, 21, 22, 23, 24, 25, 26, 27, 30, 31, 33, 45, 50, 68
>
> > real    1m28.94s
> > user    1m28.11s
> > sys     0m0.72s
>
> > import sys
> > def main():
> >     with open(sys.argv[2],'rb') as inf:
> >         limit = sys.argv[3] if len(sys.argv)>3 else 1
> >         dlm = sys.argv[1].encode('latin1')
> >         nulls = [x==b'' for x in next(inf)[:-1].split(dlm)]
> >         enum = enumerate
> >         split = bytes.split
> >         out = sys.stdout
> >         prn = print
> >         for j, r in enum(inf):
> >             if j%100==0:
> >                 prn(j//100,end=' ')
> >                 out.flush()
> >                 if j//100>=limit:
> >                     break
> >             for i, cur in enum(split(r[:-1],dlm)):
> >                 nulls[i] |= cur==b''
> >     print('Null columns:')
> >     print(', '.join(str(i+1) for i,val in enumerate(nulls) if val))
>
> > if not (len(sys.argv)>2):
> >     sys.exit("Usage: "+sys.argv[0]+
> >          "   ")
>
> > main()
>
> What's with the aliasing enumerate and print??? And on heavy disk IO I
> can hardly see that name lookups are going to be any problem at all?
> And why the time stats with /dev/null ???
>
> I'd probably go for something like:
>
> import csv
>
> with open('somefile') as fin:
>     nulls = set()
>     for row in csv.reader(fin, delimiter='|'):
>         nulls.update(idx for idx,val in enumerate(row, start=1) if not
> val)
>     print 'nulls =', sorted(nulls)
>
> hth
> Jon

Thanks, Jon
aliasing is a common method to avoid extra lookups. The time stats for
head is giving the pure I/O time. So of the 88 seconds the python
program takes 5 seconds are due to I/O, so there is quite a bit of
overhead.

I ended up with this, not super fast so I probably won't be running it
against all 350 million rows of my file but faster than before:

time head -100 myfile |./repnulls.py
nulls = [11, 20, 21, 22, 23, 24, 25, 26, 27, 30, 31, 33, 45, 50, 68]

real0m49.95s
user0m53.13s
sys 0m2.21s


import sys
def main():
fin = sys.stdin.buffer
dlm = sys.argv[1].encode('latin1') if len(sys.argv)>1 else b'|'
nulls = set()
nulls.update(i for row in fin for i, val in
enumerate(row[:-1].split(dlm), start=1) if not val)
print('nulls =', sorted(nulls))
main()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Today's fun and educational Python recipe

2011-05-05 Thread nn
On May 4, 2:17 pm, Raymond Hettinger  wrote:
> Here's a 22-line beauty for a classic and amazing 
> algorithm:http://bit.ly/bloom_filter
>
> The wiki article on the algorithm is brief and 
> well-written:http://en.wikipedia.org/wiki/Bloom_filter
>
> It turns out that people in the 1970's were pretty smart :-)
>
> Raymond
>
> ---
> follow my other python tips and recipes on twitter:  @raymondh

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


Re: string formatting

2011-05-06 Thread nn
On May 6, 8:10 am, Web Dreamer  wrote:
> Chris Rebert a écrit ce vendredi 6 mai 2011 11:23 dans
>  :
>
>
>
> > I'm not them, but:
> > "Note: The formatting operations described here [involving %] are
> > obsolete and may go away in future versions of Python. Use the new
> > String Formatting [i.e. format()] in new code."
> >http://docs.python.org/dev/library/stdtypes.html#old-string-formatting-
> operations
>
> > Technically, not formally deprecated, but such a characterization
> > isn't too far off the mark either.
>
> Thanks Chris for the link.
>
> Indeed, They mention:
>
> "The formatting operations described here are obsolete and may go away in
> future versions of Python. Use the new String Formatting in new code."
>
> So the proper word is "obsolete" and in my mind I remembered "deprecated"
> since they say it could be removed from future versions of python.
>
> So I should have said "obsolete".
>
> What I would like to know is the difference between "deprecated" and
> "obsolete"...
>
> --
> Web Dreamer

In this context I think obsolete means: Will be removed in some
undetermined version in the future; 3 versions or more from now.
There is also pending deprecation: Will be (usually) removed in the
version after the next. And
deprecated: Will be removed in the next version.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: generate properties code in class dynamically

2011-05-12 Thread nn
On May 12, 9:11 am, JamesEM  wrote:
> Hello,
> I have a python class that contains a dictionary.
> I would like to use python properties to access the elements of the
> dictionary.
> This could be achieved as follows:
>
> class MyClass(object):
>
>     def __init__(self):
>         self.d = {}
>         d['field1'] = 1.0
>         d['field2'] = 'A'
>         d['field3'] = [10.0,20.0,30.0]
>
>     @property
>     def field1(self):
>         return self.d['field1']
>
>     @field1.setter
>     def field1(self, value):
>         self.d['field1'] = value
>
>     @field1.deleter
>     def field1(self):
>         del self.d['field1']
>
>     @property
>     def field2(self):
>         return self.d['field2']
>
>     @field1.setter
>     def field2(self, value):
>         self.d['field2'] = value
>
>     @field1.deleter
>     def field2(self):
>         del self.d['field2']
>
>     @property
>     def field3(self):
>         return self.d['field3']
>
>     @field3.setter
>     def field3(self, value):
>         self.d['field3'] = value
>
>     @field3.deleter
>     def field3(self):
>         del self.d['field3']
>
> However, I am effectively writing the same properties code three
> times.
> I would prefer to generate the properties code dynamically from the
> keys of the dictionaries.
> What I am looking for is something like:
>
> class MyClass(object):
>
>     def __init__(self):
>         self.d = {}
>         d['field1'] = 1.0
>         d['field2'] = 'A'
>         d['field3'] = [10.0,20.0,30.0]
>         for f in d:
>            create_property(f)
>
> where create_property(f) dynamically creates the property code for
> field f in MyClass.
>
> Is this possible?
> If so, how could I do it?
>
> Thanks,
> James

Some searching in the cookbook should help. I found this:

http://code.activestate.com/recipes/577590-dictionary-whos-keys-act-like-attributes-as-well/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: convert time

2011-09-12 Thread nn
On Sep 11, 1:00 am, Steven D'Aprano  wrote:
> 守株待兔 wrote:
> > how can i convert "Dec 11" into  2011-12?
>
> if my_str == "Dec 11":
>     return 1999  # 2011 - 12
>
> Does that help?
>
> But seriously... 2011-12 is not a proper date, so the simplest way is
> probably something like this:
>
> def convert(date_str):
>     month, short_year = date_str.split()
>     if len(short_year) == 4:
>         year = short_year
>     else:
>         year = '20' + short_year
>     months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split()
>     month = months.index(month) + 1
>     return year + '-' + str(month)
>
> Otherwise see the time and datetime modules:
>
> http://docs.python.org/library/time.htmlhttp://docs.python.org/library/datetime.html
>
> --
> Steven

Just a small comment that you can get "months" by doing:

from calendar import month_abbr
months = list(month_abbr)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multiplication error in python

2011-09-27 Thread nn
On Sep 27, 1:21 pm, sakthi  wrote:
> In the following code,>>> l=[1,2,3,4,5]
> >>> i=0
> >>> for a in l:
>
> ...     p=2*a
> ...     t=p+i
> ...     i=t
> ...>>> t
>
> 45
>
> Python gives an answer as 45. But i am getting 30 when i execute
> manually. Is there any different multiplication pattern in python?
> Thank yu.

You must be doing something different than these steps:

Python 3.2.2 (default, Sep  4 2011, 09:51:08) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> l=[1,2,3,4,5]
>>> i=0
>>> for a in l:
p=2*a
t=p+i
i=t


>>> t
30
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fairly OT: Why "flufl"?

2013-02-04 Thread nn
On Feb 4, 10:10 am, Chris Angelico  wrote:
> This isn't particularly related to the post I'm quoting, it's more a
> point of curiosity.
>
> On Mon, Feb 4, 2013 at 10:53 AM, João Bernardo  wrote:
>
> Re: [Python-ideas] constant/enum type in stdlib
>
> > I have my own implementation with a basic api somewhat borrowed from
> > flufl.enum (plus a lot of other stuff)...
>
> What is the origin of the term FLUFL? It's referenced in PEP 401 about
> the retirement of the BDFL and the appointment of Barry Warsaw as
> Guido's successor. Is that where the expression FLUFL originated, or
> is "Friendly Language Uncle For Life" a backformation?
>
> This might be more of a personal question for Barry, in the same way
> that asking me why I'm "Rosuav" wouldn't be a list question, but I'm
> wondering if there's something more Python to it.
>
> Just a point of random curiosity!
>
> ChrisA

My guess is that it originated with PEP 401, and that FLUFL ("Friendly
Language Uncle For Life") were created as humorous take on the equally
silly title of BDFL ("Benevolent Dictator For Life").
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Do you feel bad because of the Python docs?

2013-02-26 Thread nn
On Feb 26, 11:19 am, notbob  wrote:
> On 2013-02-26, Steven D'Aprano  wrote:
>
> > "The Python documentation is bad, and you should feel bad".
>
> Ahh!  A point at which I can interject.
>
> As a rank green python noob, I definitely hava an opinion on python
> documentation and it's not entirely flattering.  OTOH, it's hard to
> toss any other single linux based documentation up as a sterling
> example.  IOW, I've seen worse.  How am I learning about python?
>
> Several sources.  The "Non-Programmer's Tutorial" docs from wikibooks
> was a false start.  It goes for about 2 pages before I realized
> they've snuck in some python syntax without explaining it.  So, I jump
> over to "The Python Tutorial", which immediately leaves me submerged,
> as it's waaay over my clueless head.  I flounder around and
> desperately grab onto "Basic Python" over at About.com.  Finally, I'm
> rescued!
>
> Whoda thunk it?  I usta despise About.com.  But, they've matured
> greatly since their early days.  I'm not a programmer.  In fact I
> really dislike programming.  But, as a long time linux user, I really
> need to learn a useful higher language.  And here is this website that
> takes me by the hand and speaks to me like what I am.  Dumb as a post
> and disinterested.  But, they are patient.  They explain basic
> programming concepts before launching into specifics.  When they do
> get specific, they use simple examples that make sense.  The don't
> toss in syntax they haven't fully explained.  Great site and the one
> I'm now using to progress.  I'm sure the other sites I've named will
> become helpful, eventually, but now I can move forward with
> confidence.
>
> Are python doc sites perfect?  No.  I've yet to come upon anything
> that clarifies why's and wherefores and the differences between the CMI
> IDLE and the GUI IDLE.  And boy, are they different!  OTOH, as I said,
> I've seen worse Linux docs.  BitchX or zsh?  What docs!?  Even the man
> pages took me a long time to figure out.  Bluefish?  Krita?
> Puh-leeze!  emacs?  It's a wonder I can use it at all.  ;)
>
> Despite all that, I'd say python documentation is better than a poke
> in the eye with a sharp stick.  I'm sure the official pages will make
> more sense to me when I understand more.  As it is, they jes toss out
> "lc-letter" like I know what they're talking about.  They explain it a
> little bit, but I still hadda wiki it to get the full story.
>
> As a person with some technical writing experience, I know how
> difficult it can be.  I had to be careful about who I was writing for,
> engineers or laymen.  It's the same with programming docs.  Writing
> tutorials about python as if I jes came from 5 yrs as a C programmer
> is not in the least bit helpful to a beginner like myself.  Sometimes,
> one jes hasta hunt for the right flavor.
>
> nb

For me on the other hand. The Python tutorial has been the most useful
tutorial I have ever used. I had experience with Basic and Pascal.
Most other tutorials go too slow for me and I loose interest. The
python one just kept going and after reading the dozen pages in a
couple of hours I had enough of an idea about the language to start
doing the things I was interested in.

The only thing that confused me at first was finding functions like
"open" or "input" and string methods. I lost a bit of time searching
in the language reference until I found out that they are in the
Library reference: I didn't think of "open" as a library. But now I
have no problem; all I need is found under Library reference in
section 2 and 4 and starting with section 6. I also use the global
module index when I already have an idea what I am looking for.

What it could have is better searching capability and a way to see
more examples. Examples would clutter the documentation so maybe they
should be collapsible, but you can never have enough examples. As
Einstein said:
“Example isn't another way to teach, it is the only way to teach”
Outside of the tutorial there are not a lot of succinct examples of
more advanced uses of the different keywords and builtins. Thankfully
for the libraries there is Python Module of the Week for people that
know about it.

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


Re: xlrd 0.9.2 released!

2013-04-10 Thread nn
On Apr 9, 3:38 pm, Chris Withers  wrote:
> Hi All,
>
> I'm pleased to announce the release of xlrd 0.9.2:
>
> http://pypi.python.org/pypi/xlrd/0.9.2
>
> This release includes the following changes:
>
> - Fix some packaging issues that meant docs and examples were missing
> from the tarball.
>
> - Fixed a small but serious regression that caused problems opening
> .xlsx files.
>
> If you find any problems, please ask about them on the
> python-ex...@googlegroups.com list, or submit an issue on GitHub:
>
> https://github.com/python-excel/xlrd/issues
>
> Full details of all things Python and Excel related can be found here:
>
> http://www.python-excel.org/
>
> cheers,
>
> Chris
>
> --
> Simplistix - Content Management, Batch Processing & Python Consulting
>              -http://www.simplistix.co.uk

Thanks from me (and I am sure many others).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Weird behaviour?

2013-04-22 Thread nn
On Apr 21, 9:19 pm, Steven D'Aprano  wrote:
> On Mon, 22 Apr 2013 10:56:11 +1000, Chris Angelico wrote:
> > You're running this under Windows. The convention on Windows is for
> > end-of-line to be signalled with \r\n, but the convention inside Python
> > is to use just \n. With the normal use of buffered and parsed input,
> > this is all handled for you; with unbuffered input, that translation
> > also seems to be disabled, so your string actually contains '120\r', as
> > will be revealed by its repr().
>
> If that's actually the case, then I would call that a bug in raw_input.
>
> Actually, raw_input doesn't seem to cope well with embedded newlines even
> without the -u option. On Linux, I can embed a control character by
> typing Ctrl-V followed by Ctrl-. E.g. Ctrl-V Ctrl-M to embed a
> carriage return, Ctrl-V Ctrl-J to embed a newline. So watch:
>
> [steve@ando ~]$ python2.7 -c "x = raw_input('Hello? '); print repr(x)"
> Hello? 120^M^Jabc
> '120\r'
>
> Everything after the newline is lost.
>
> --
> Steven

Maybe it is related to this bug?

http://bugs.python.org/issue11272


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


Re: Question about file objects...

2009-12-02 Thread nn
On Dec 2, 9:14 am, J  wrote:
> Something that came up in class...
>
> when you are pulling data from a file using f.next(), the file is read
> one line at a time.
>
> What was explained to us is that Python iterates the file based on a
> carriage return as the delimiter.
> But what if you have a file that has one line of text, but that one
> line has 16,000 items that are comma delimited?
>
> Is there a way to read the file, one item at a time, delimited by
> commas WITHOUT having to read all 16,000 items from that one line,
> then split them out into a list or dictionary??
>
> Cheers
> Jeff
>
> --
>
> Ogden Nash  - "The trouble with a kitten is that when it grows up,
> it's always a cat." 
> -http://www.brainyquote.com/quotes/authors/o/ogden_nash.html

File iteration is a convenience since it is the most common case. If
everything is on one line, you will have to handle record separators
manually by using the .read() method on the file
object and searching for the comma. If everything fits in memory the
straightforward way would be to read the whole file with .read() and
use .split(",") on the returned string. That should give you a nice
list of everything.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about file objects...

2009-12-03 Thread nn
On Dec 2, 6:56 pm, Terry Reedy  wrote:
> J wrote:
> > On Wed, Dec 2, 2009 at 09:27, nn  wrote:
> >>> Is there a way to read the file, one item at a time, delimited by
> >>> commas WITHOUT having to read all 16,000 items from that one line,
> >>> then split them out into a list or dictionary??
>
> >> File iteration is a convenience since it is the most common case. If
> >> everything is on one line, you will have to handle record separators
> >> manually by using the .read() method on the file
> >> object and searching for the comma. If everything fits in memory the
> >> straightforward way would be to read the whole file with .read() and
> >> use .split(",") on the returned string. That should give you a nice
> >> list of everything.
>
> > Agreed. The confusion came because the guy teaching said that
> > iterating the file is delimited by a carriage return character...
>
> If he said exactly that, he is not exactly correct. File iteration looks
> for line ending character(s), which depends on the system or universal
> newline setting.
>
> > which to me sounds like it's an arbitrary thing that can be changed...
>
> > I was already thinking that I'd have to read it in small chunks and
> > search for the delimiter i want...  and reading the whole file into a
> > string and then splitting that would would be nice, until the file is
> > so large that it starts taking up significant amounts of memory.
>
> > Anyway, thanks both of you for the explanations... I appreciate the help!
>
> I would not be surprised if a generic file chunk generator were posted
> somewhere. It would be a good entry for the Python Cookbook, if not
> there already.
>
> tjr

There should be but writing one isn't too difficult:

def chunker(file_obj):
parts=['']
while True:
fdata=file_obj.read(8192)
if not fdata: break
parts=(parts[-1]+fdata).split(',')
for col in parts[:-1]:
yield col
yield parts[-1]

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


Re: Which is more pythonic?

2009-12-04 Thread nn
On Dec 3, 10:41 am, Filip Gruszczyński  wrote:
> I have just written a very small snippet of code and started thinking,
> which version would be more pythonic. Basically, I am adding a list of
> string to combo box in qt. So, the most obvious way is:
>
> for choice in self.__choices:
>         choicesBox.addItem(choice)
>
> But I could also do:
>
> map(self.__choices, choicesBox.addItem)
>
> or
>
> [choicesBox.addItem(choice) for choice in self.__choices]
>
> I guess map version would be fastest and explicit for is the slowest
> version. However, the first, most obvious way seems most clear to me
> and I don't have to care about speed with adding elements to combo
> box. Still, it's two lines instead of one, so maybe it's not the best.
> So, which one is?
>
> --
> Filip Gruszczyński

First option is the most pythonic IMO. If it HAS to be one line I
would still prefer:

for choice in self.__choices: choicesBox.addItem(choice)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Py 3: Terminal script can't find relative path

2010-01-19 Thread nn
On Jan 19, 8:03 am, Gnarlodious  wrote:
> On Jan 18, 4:21 pm, John Bokma  wrote:
>
> > Gnarlodious  writes:
> > > I am running a script in a browser that finds the file in subfolder
> > > Data:
>
> > > Content=Plist('Data/Content.plist')
>
> > > However, running the same script in Terminal errors:
>
> > > IOError: [Errno 2] No such file or directory: 'Data/Content.plist'
>
> > What does:
>
> > ls -l Data/Content.plist
>
> > in the terminal give?
>
> I can replace with absolute paths and it works as expected. Could this
> be a Python 3 bug? Where as a CGI script it finds the relative path
> but not in Terminal?
>
> -- Gnarlie

Stop and think for a second what you are saying: It works with
absolute paths, it works as CGI script with relative paths, it doesn't
work in the terminal. What is different? Do you know for sure what
folder you are starting at when using the relative path? Most likely
the terminal starts in a different place than the CGI script.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Arrrrgh! Another module broken

2010-01-19 Thread nn
On Jan 18, 11:37 am, Grant Edwards  wrote:
> On 2010-01-18, Jive Dadson  wrote:
>
> > I just found another module that broke when I went to 2.6.  Gnuplot.
> > Apparently one of its routines has a parameter named "with."  That used
> > to be okay, and now it's not.
>
> I remember seeing depreicated warnings about that _years_ ago,
> and I would have sworn it's been fixed for at least a couple
> years.
>
> --
> Grant

FWIW, "with" deprecation warnings exist since September 19, 2006 when
Python 2.5 was released.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes for AIX

2010-01-26 Thread nn
On Jan 25, 6:36 pm, "Waddle, Jim"  wrote:
> Chris,
> Thanks for responding to my email.
> I apologize for the remark about python only being developed for windows. I 
> got the impression when I was looking at the ActivePython web site and saw 
> that the version of python that they had available was not supported on very 
> many unix systems. I should not make general statement based on only one web 
> site. After reading your email I decided to see for myself what the issue was 
> about compiling python on AIX 5.3.
>
> This is the error I saw the first time I tried to use ctypes.
>
> Python 2.4.3 (#1, Jul 17 2006, 20:00:23) [C] on aix5
> Type "help", "copyright", "credits" or "license" for more information.>>> 
> import ctypes
>
> Traceback (most recent call last):
>   File "", line 1, in ?
> ImportError: No module named ctypes
>
> This version of python was downloaded and installed from ActivePython and 
> when I checked their webpage it states that ctypes is not available on AIX.
> I then figured I would get a new copy of python and install it on AIX. I 
> downloaded python.2.5.5c2 fromhttp://www.python.org. I did the configure and 
> make which posted many errors in the ctypes function which I guess is the 
> reason that is does not get include in the final make.
>
> an example of the build error I get when doing the make is:
> xlc_r -q64 -DNDEBUG -O -I. 
> -I/s/users/cz030a/xferjunk/python/Python-2.5.5c2/./Include 
> -Ibuild/temp.aix-5.3-2.5/libffi/inclu
> de -Ibuild/temp.aix-5.3-2.5/libffi 
> -I/s/users/cz030a/xferjunk/python/Python-2.5.5c2/Modules/_ctypes/libffi/src 
> -I/s/users/c
> z030a/xferjunk/ots/python2.5/include -I. -IInclude -I./Include 
> -I/s/users/cz030a/xferjunk/python/Python-2.5.5c2/Include -I/
> s/users/cz030a/xferjunk/python/Python-2.5.5c2 -c 
> /s/users/cz030a/xferjunk/python/Python-2.5.5c2/Modules/_ctypes/_ctypes.c -
> o 
> build/temp.aix-5.3-2.5/s/users/cz030a/xferjunk/python/Python-2.5.5c2/Modules/_ctypes/_ctypes.o
> "/s/users/cz030a/xferjunk/python/Python-2.5.5c2/Modules/_ctypes/_ctypes.c", 
> line 2820.31: 1506-068 (W) Operation between ty
> pes "void*" and "int(*)(void)" is not allowed.
> "/s/users/cz030a/xferjunk/python/Python-2.5.5c2/Modules/_ctypes/_ctypes.c", 
> line 3363.28: 1506-280 (W) Function argument as
> signment between types "int(*)(void)" and "void*" is not allowed.
> "/s/users/cz030a/xferjunk/python/Python-2.5.5c2/Modules/_ctypes/_ctypes.c", 
> line 4768.67: 1506-280 (W) Function argument as
> signment between types "void*" and "void*(*)(void*,const void*,unsigned 
> long)" is not allowed.
> "/s/users/cz030a/xferjunk/python/Python-2.5.5c2/Modules/_ctypes/_ctypes.c", 
> line 4769.66: 1506-280 (W) Function argument as
> signment between types "void*" and "void*(*)(void*,int,unsigned long)" is not 
> allowed.
>
> I do not have sufficient knowledge to know how to fix this. I would think 
> that this error somehow is related to compiling on aix. If you have any 
> suggestions on how to correct this problem , I would appreciate it
>
> Jim Waddle
> KIT-D
> 425-785-5194
>
> -Original Message-
> From: ch...@rebertia.com [mailto:ch...@rebertia.com] On Behalf Of Chris Rebert
> Sent: Sunday, January 24, 2010 7:31 AM
> To: Waddle, Jim
>
> Cc: python-l...@python.org
> Subject: Re: ctypes for AIX
>
> On Sun, Jan 24, 2010 at 5:54 AM, Waddle, Jim  wrote:
> > I need to use ctypes with python running on AIX.
>
> According to the ctypes readme, ctypes is based on libffi, which
> according to its website, supports AIX for PowerPC64.
> So, perhaps you could state what the actual error or problem you're
> encountering is?
> It is theoretically possible the ctypes-bundled libffi is either
> outdated or had the AIX-specific bits removed; I don't know, I'm not a
> CPython dev.
>
> > It appears that python is being developed mostly for windows.
>
> No, not really; your statement is especially ironic considering one of
> Python's primary areas of use is for web applications as part of a
> LAMP stack.
>
> > Is there a policy concerning getting functions like ctypes working on AIX.
>
> No idea. Someone will probably chime in though.
>
> Cheers,
> Chris
> --http://blog.rebertia.com
>
>

Look at this website:

http://pware.hvcc.edu/news.html

"8/1/2009 Python with ctypes!"

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


Re: Need help with a program

2010-01-28 Thread nn
On Jan 28, 10:50 am, evilweasel  wrote:
> I will make my question a little more clearer. I have close to 60,000
> lines of the data similar to the one I posted. There are various
> numbers next to the sequence (this is basically the number of times
> the sequence has been found in a particular sample). So, I would need
> to ignore the ones containing '0' and write all other sequences
> (excluding the number, since it is trivial) in a new text file, in the
> following format:
>
> >seq59902
>
> TTTATTATATAGT
>
> >seq59903
>
> TTTATTTCTTGGCGTTGT
>
> >seq59904
>
> TTTGGTTGCCCTGCGTGG
>
> >seq59905
>
> TTTGTTTATGGG
>
> The number next to 'seq' is the line number of the sequence. When I
> run the above program, what I expect is an output file that is similar
> to the above output but with the ones containing '0' ignored. But, I
> am getting all the sequences printed in the file.
>
> Kindly excuse the 'newbieness' of the program. :) I am hoping to
> improve in the next few months. Thanks to all those who replied. I
> really appreciate it. :)

People have already given you some pointers to your problem. In the
end you will have to "tweak the details" because only you have access
to the data not us.

Just as example here is another way to do what you are doing:

with open('dnain.dat') as infile, open('dnaout.dat','w') as outfile:
   partgen=(line.split() for line in infile)
   dnagen=(str(i+1)+'\n'+part[0]+'\n'
   for i,part in enumerate(partgen)
   if len(part)>1 and part[1]!='0')
   outfile.writelines(dnagen)

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


Re: Need help with a program

2010-01-28 Thread nn


Arnaud Delobelle wrote:
> nn  writes:
>
> > On Jan 28, 10:50 am, evilweasel  wrote:
> >> I will make my question a little more clearer. I have close to 60,000
> >> lines of the data similar to the one I posted. There are various
> >> numbers next to the sequence (this is basically the number of times
> >> the sequence has been found in a particular sample). So, I would need
> >> to ignore the ones containing '0' and write all other sequences
> >> (excluding the number, since it is trivial) in a new text file, in the
> >> following format:
> >>
> >> >seq59902
> >>
> >> TTTATTATATAGT
> >>
> >> >seq59903
> >>
> >> TTTATTTCTTGGCGTTGT
> >>
> >> >seq59904
> >>
> >> TTTGGTTGCCCTGCGTGG
> >>
> >> >seq59905
> >>
> >> TTTGTTTATGGG
> >>
> >> The number next to 'seq' is the line number of the sequence. When I
> >> run the above program, what I expect is an output file that is similar
> >> to the above output but with the ones containing '0' ignored. But, I
> >> am getting all the sequences printed in the file.
> >>
> >> Kindly excuse the 'newbieness' of the program. :) I am hoping to
> >> improve in the next few months. Thanks to all those who replied. I
> >> really appreciate it. :)
> >
> > People have already given you some pointers to your problem. In the
> > end you will have to "tweak the details" because only you have access
> > to the data not us.
> >
> > Just as example here is another way to do what you are doing:
> >
> > with open('dnain.dat') as infile, open('dnaout.dat','w') as outfile:
> >partgen=(line.split() for line in infile)
> >dnagen=(str(i+1)+'\n'+part[0]+'\n'
> >for i,part in enumerate(partgen)
> >if len(part)>1 and part[1]!='0')
> >outfile.writelines(dnagen)
>
> I think that generator expressions are overrated :) What's wrong with:
>
> with open('dnain.dat') as infile, open('dnaout.dat','w') as outfile:
> for i, line in enumerate(infile):
> parts = line.split()
> if len(parts) > 1 and parts[1] != '0':
> outfile.write(">seq%s\n%s\n" % (i+1, parts[0]))
>
> (untested)
>
> --
> Arnaud

Nothing really,
After posting I was thinking I should have posted a more
straightforward version like the one you wrote. Now there is! It
probably is more efficient too. I just have a tendency to think in
terms of pipes: "pipe this junk in here, then in here, get output".
Probably damage from too much Unix scripting.Since I can't resist the
urge to post crazy code here goes the bonus round (don't do this at
work):

open('dnaout.dat','w').writelines(
   'seq%s\n%s\n'%(i+1,part[0])
   for i,part in enumerate(line.split() for line in open('dnain.dat'))
   if len(part)>1 and part[1]!='0')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help with a program

2010-01-29 Thread nn


Johann Spies wrote:
> On Thu, Jan 28, 2010 at 07:07:04AM -0800, evilweasel wrote:
> > Hi folks,
> >
> > I am a newbie to python, and I would be grateful if someone could
> > point out the mistake in my program. Basically, I have a huge text
> > file similar to the format below:
> >
> > AGACTCGAGTGCGCGGA   0
> > AGATAAGCTAATTAAGCTACTGG 0
> > AGATAAGCTAATTAAGCTACTGGGTT   1
> > AGCTCACAATAT 1
> > AGGTCGCCTGACGGCTGC  0
>
> I know this is a python list but if you really want to get the job
> done quickly this is one method without writing python code:
>
> $ cat /tmp/y
> AGACTCGAGTGCGCGGA   0
> AGATAAGCTAATTAAGCTACTGG 0
> AGATAAGCTAATTAAGCTACTGGGTT   1
> AGCTCACAATAT 1
> AGGTCGCCTGACGGCTGC  0
> $ grep -v 0 /tmp/y > tmp/z
> $ cat /tmp/z
> AGATAAGCTAATTAAGCTACTGGGTT   1
> AGCTCACAATAT 1
>
> Regards
> Johann
> --
> Johann Spies  Telefoon: 021-808 4599
> Informasietegnologie, Universiteit van Stellenbosch
>
>  "My son, if sinners entice thee, consent thou not."
> Proverbs 1:10

I would rather use awk for this:

awk 'NF==2 && $2!~/^0$/ {printf("seq%s\n%s\n",NR,$1)}' dnain.dat

but I think that is getting a bit off topic...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using String for new List name

2009-09-29 Thread nn
On Sep 28, 7:37 pm, Scott  wrote:
> On Sep 28, 2:00 pm, Dave Angel  wrote:
>
>
>
> > Scott wrote:
> > > Thank you fine folks for getting back with your answers!
>
> > > So down the road I do dictname[line42].append("new stuff"). (or [var]
> > > if I'm looping through the dict)
>
> > Nope, you still haven't gotten it.  Of course, I really don't know where
> > you're going wrong, since you didn't use the same symbols as any of the
> > responses you had gotten.
>
> > I suspect that you meant dictname[] to be the dictionary that Duncan
> > called values[].  On that assumption, in order to append, you'd want
> > something like:
>
> > values["line42"].append("new stuff")
> >      or
> > values[var].append("new stuff") if you happen to have a variable called
> > var with a value of "line42".
>
> > You will need to get a firm grasp on the distinctions between symbol
> > names, literals, and values.  And although Python lets you blur these in
> > some pretty bizarre ways, you haven't a chance of understanding those
> > unless you learn how to play by the rules first.  I'd suggest your first
> > goal should be to come up with better naming conventions.  And when
> > asking questions here, try for more meaningful data than "Line42" to
> > make your point.
>
> > Suppose a text file called "customers.txt" has on each line a name and
> > some data.  We want to initialize an (empty)  list for each of those
> > customers, and refer to it by the customer's name.  At first glance we
> > might seem to want to initialize a variable for each customer, but our
> > program doesn't know any of the names ahead of time, so it's much better
> > to have some form of collection. We choose a dictionary.
>
> > transactions = {}
> > with open("customers.txt") as infile:
> >     for line in infile:
> >         fields = line.split()
> >         customername = fields[0]            #customer is first thing on
> > the line
> >         transactions[customername] = []       #this is where we'll put
> > the transactions at some later point, for this customer
>
> > Now, if our program happens to have a special case for a single
> > customer, we might have in our program something like:
>
> >     transactions["mayor"].append("boots")
>
> > But more likely, we'll be in a loop, working through another file:
>
> > .
> >         for line in otherfile:
> >                fields = line.split()
> >                customername = fields[0]
> >                transaction = fields[1]
>
> > transactions[customername].append(transaction)                #append
> > one transaction
>
> > or interacting:
> >       name = raw_input("Customer name")
> >       trans = raw_input("transaction for that customer")
> >       transactions[name].append(trans)
>
> Dave,
>
> I'm amazed at everyone's willingness to share and teach! I will sure
> do the same once I have the experience.
>
> I think that one of the problems here is that I tried to make my
> initial question as bone simple as possible. When I first tried to
> explain what I was doing I was getting up to 2 pages and I thought "I
> bet these folks don't need to read my program. They probably just need
> to know the one bit I'm looking for." So I deleted it all and reduced
> it to the 10 line example that I posted.
>
> It was then suggested that I eschew using regular expressions when not
> required because I used Y = re.split(" ", X) in my example. In my
> program it is actually aclLs = re.split("\s|:|/", aclS) which I think
> requires a regex. I just didn't want anyone to waste their time
> parsing the regex when it was not really germane to my actual
> question.
>
> The same applies to the suggestion for using meaningful variables. In
> the above aclLs represents (to me) "access control list List-Split"
> and aclS represents "access control list String." Again, I thought X
> and Y, like foo and bar or spam and eggs would do for a simple
> example.
>
> Of course I then went and forgot the quotes around "line42" and really
> looked confused. I was so excited to have an answer that I typed the
> reply without thinking it through. Not good.
>
> Don't worry though, I take no offense. I understand and welcome the
> advice. I don't have anyone to work with and this post is my first
> interaction with any person who knows programming and Python. I am but
> a network engineer (Cisco, Lan/Wan, firewalls, security, monitoring
> (this is the connection), etc.) who has never programmed. I will work
> on clearer communications in future posts.
>
> I'm happy for a chance to share what I am actually trying to
> accomplish here.
>
> I have a firewall with a static list of access-control-list (ACL)
> rules (about 500 rules). I also have a directory with one week of
> syslog output from the firewall. About 100 text files that are each
> about 10 to 30 MB in size.
>
> My quest, if you will, is to create a list of syslog entries, each
> representing a successful network connection, with each syslog entry
> listed under the access

Re: Enormous Input and Output Test

2009-10-05 Thread nn
On Oct 4, 8:41 am, Duncan Booth  wrote:
> Jon Clements  wrote:
> > On Oct 4, 12:08 pm, n00m  wrote:
> >> Duncan Booth,
>
> >> alas... still TLE:
>
> >> 2800839
> >> 2009-10-04 13:03:59
> >> Q
> >> Enormous Input and Output Test
> >> time limit exceeded
> >> -
> >> 88M
> >> PYTH
>
> > Just to throw into the mix...
>
> > What about buffering? Does anyone know what the effective stdin buffer
> > is for Python? I mean, it really can't be the multiplying that's a
> > bottleneck. Not sure if it's possible, but can a new stdin be created
> > (possibly using os.fdopen) with a hefty buffer size?
>
> > I'm probably way off, but something to share.
>
> I did try a version where I just read the data in, split it up, and then
> wrote it out again. On my test file that took about 2 seconds compared with
> the 8 seconds it took the full code I posted, so while there may be scope
> for faster I/O (e.g. using mmap), any real speedup would have to be in the
> convert to int, multiply, convert back to str pipeline.

This takes 5 second on my machine using a file with 1,000,000 random
entries:

inf=open('tstf')
outf=open('tstof','w')
for i in xrange(int(inf.next(),10)):
n1,s,n2=inf.next().partition(' ')
print >>outf,int(n1,10)*int(n2,10)
outf.close()
inf.close()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why is this slower?

2009-10-06 Thread nn
On Oct 5, 12:46 pm, Joseph Reagle  wrote:
> I would think the commented code would be faster (fewer loops), but it is
> not (because of function calls).
>
>     #Average user_time = 5.9975 over 4 iterations
>     inSRC = set([bio.name for bio in bios.values()])
>     inEB = set([bio.name for bio in bios.values() if bio.eb_title])
>     inWP = set([bio.name for bio in bios.values() if bio.wp_title])
>     inBoth = inEB & inWP
>     missingEB = inSRC - inEB
>     missingWP = inSRC - inWP
>     missingBoth = missingEB & missingWP
>     avg_size_ratio = find_average(
>         [bio.wp_ratio for bio in bios.values() if bio.wp_wc and bio.eb_wc])
>     mdn_size_ratio = find_median(
>         [bio.wp_ratio for bio in bios.values() if bio.wp_wc and bio.eb_wc])
>     SRCfem = set([bio.name for bio in bios.values() if bio.gender
> == 'female'])
>     EBfem = set([bio.name for bio in bios.values() if bio.eb_gender
> == 'female'])
>     WPfem = set([bio.name for bio in bios.values() if bio.wp_gender
> == 'female'])
>     SRCmale = set([bio.name for bio in bios.values() if bio.gender
> == 'male'])
>     EBmale = set([bio.name for bio in bios.values() if bio.eb_gender
> == 'male'])
>     WPmale = set([bio.name for bio in bios.values() if bio.wp_gender
> == 'male'])
>     SRCun = set([bio.name for bio in bios.values() if bio.gender
> == 'unknown'])
>     EBun = set([bio.name for bio in bios.values() if bio.eb_gender
> == 'unknown'])
>     WPun = set([bio.name for bio in bios.values() if bio.wp_gender
> == 'unknown'])
>
>     ##Average user_time = 6.0025 over 4 iterations
>     #def set_amend(obj, bio):
>         #if obj == None:
>             #obj = set([])
>         #obj.add(bio.name)
>         #return obj
>
>     #inSRC = set([])
>     #inSRC = set([])
>     #inEB = set([])
>     #inWP = set([])
>     #SRCfem = set([])
>     #EBfem = set([])
>     #WPfem = set([])
>     #SRCmale = set([])
>     #EBmale = set([])
>     #WPmale = set([])
>     #SRCun = set([])
>     #EBun = set([])
>     #WPun = set([])
>
>     #for bio in bios.values():
>         ### use a function that takes set name (creates one) and conditional
>         #inSRC = set_amend(inSRC, bio)
>         #if bio.eb_title: inEB = set_amend(inEB, bio)
>         #if bio.wp_title: inWP = set_amend(inWP, bio)
>         #if bio.gender == 'female': SRCfem = set_amend(SRCfem, bio)
>         #if bio.eb_gender == 'female': EBfem = set_amend(EBfem, bio)
>         #if bio.wp_gender == 'female': WPfem = set_amend(WPfem,bio)
>         #if bio.gender == 'male': SRCmale = set_amend(SRCmale, bio)
>         #if bio.eb_gender == 'male': EBmale = set_amend(EBmale, bio)
>         #if bio.wp_gender == 'male': WPmale = set_amend(WPmale, bio)
>         #if bio.gender == 'unknown': SRCun = set_amend(SRCun, bio)
>         #if bio.eb_gender == 'unknown': EBun = set_amend(EBun, bio)
>         #if bio.wp_gender == 'unknown': WPun = set_amend(WPun, bio)
>     #inBoth = inEB & inWP
>     #missingEB = inSRC - inEB
>     #missingWP = inSRC - inWP
>     #missingBoth = missingEB & missingWP
>     #avg_size_ratio = find_average(
>         #[bio.wp_ratio for bio in bios.values() if bio.wp_wc and bio.eb_wc])
>     #mdn_size_ratio = find_median(
>         #[bio.wp_ratio for bio in bios.values() if bio.wp_wc and bio.eb_wc])

Not only are you doing many function calls but you are assigning 12
objects each time. Why not do this?

for bio in bios.values():
 inSRC.add(bio)
 if bio.eb_title: inEB.add(bio)
 if bio.wp_title: inWP.add(bio)
 if bio.gender == 'female': SRCfem.add(bio)
 if bio.eb_gender == 'female': EBfem.add(bio)
 if bio.wp_gender == 'female': WPfem.add(bio)
 if bio.gender == 'male': SRCmale.add(bio)
 if bio.eb_gender == 'male': EBmale.add(bio)
 if bio.wp_gender == 'male': WPmale.add(bio)
 if bio.gender == 'unknown': SRCun.add(bio)
 if bio.eb_gender == 'unknown': EBun.add(bio)
 if bio.wp_gender == 'unknown': WPun.add(bio)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A stupid newbie question about output...

2009-10-20 Thread nn
On Oct 20, 2:23 pm, J  wrote:
> Can someone explain why this code results in two different outputs?
>
> > for os in comp.CIM_OperatingSystem ():
> >  print os.Name.split("|")[0] + " Service Pack", os.ServicePackMajorVersion
> >  osVer = os.Name.split("|")[0] + " Service Pack", os.ServicePackMajorVersion
> >  print osVer
>
> the first print statement gives me this:
>   Microsoft Windows XP Professional Service Pack 3
>
> and the second print statement gives me this:
> (u'Microsoft Windows XP Professional Service Pack', 3)
>
> I know this is a grossly oversimplified example.
>
> The ultimate goal is to get certain things from the system via WMI and
> then use that data in other scripts to generate test case scripts for
> a testing tool.
>
> So given this example, what I ultimately want is to create a class
> full of this stuff, that I can call from another program like this:
>
> > import SystemInformation
> > comp = SystemInformation()
> > # Get OS info
> > OSVersion = comp.osVer
>
> OR just print the result
>
> > print comp.osVer
>
> Can this be cleared up by using some sort of formatting in print (like
> sprintf) or am I going to have to manipulate the WMI results before
> sending them out??
>
> Thanks
>
> Jeff
>
> --
>
> Ted Turner  - "Sports is like a war without the killing." 
> -http://www.brainyquote.com/quotes/authors/t/ted_turner.html

This is because of some magic the print statement does with variables
separated by commas adding a space. So you can either do:

print (os.Name.split("|")[0] + " Service Pack",
os.ServicePackMajorVersion)

or

osVer = "%s %s" % (os.Name.split("|")[0] + " Service Pack",
os.ServicePackMajorVersion)

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


Re: Language mavens: Is there a programming with "if then else ENDIF" syntax?

2009-11-17 Thread nn
On Nov 16, 11:54 am, Steve Ferg 
wrote:
> This is a question for the language mavens that I know hang out here.
> It is not Python related, except that recent comparisons of Python to
> Google's new Go language brought it to mind.
>
> NOTE that this is *not* a suggestion to change Python.  I like Python
> just the way it is.  I'm just curious about language design.
>
> For a long time I've wondered why languages still use blocks
> (delimited by do/end, begin/end, { } , etc.) in ifThenElse statements.
>
> I've often thought that a language with this kind of block-free syntax
> would be nice and intuitive:
>
>     if  then
>         do stuff
>     elif  then
>         do stuff
>     else
>         do stuff
>     endif
>
> Note that you do not need block delimiters.
>
> Obviously, you could make a more Pythonesque syntax by using a colon
> rather then "then" for the condition terminator.  You could make it
> more PL/I-like by using "do", etc.
>
> You can write shell scripts using if ... fi, but other than that I
> don't recall a language with this kind of syntax.
>
> Does anybody know a language with this kind of syntax for
> ifThenElseEndif?
>
> Is there any particular reason why this might be a *bad* language-
> design idea?

I personally like the "END X" syntax (not that I would want it for
Python mind you). It makes it easier to read programs backwards.
Foxpro used that syntax form extensively:

http://msdn.microsoft.com/en-us/library/b660264t%28VS.80%29.aspx

DO CASE ... ENDCASE
DO WHILE ... ENDDO
FOR EACH ... ENDFOR
FOR ... ENDFOR
IF ... ENDIF
PRINTJOB ... ENDPRINTJOB
SCAN ... ENDSCAN
TEXT ... ENDTEXT
WITH ... ENDWITH
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list when the contents is a list

2010-02-18 Thread nn


Wes James wrote:
> I have been trying to create a list form a string.  The string will be
> a list (this is the contents will look like a list).  i.e. "[]" or
> "['a','b']"
>
> The "[]" is simple since I can just check if value == "[]" then return []
>
> But with "['a','b']" I have tried and get:
>
> a="['a','b']"
>
> b=a[1:-1].split(',')
>
> returns
>
> [ " 'a' "," 'b' " ]
>
> when I want it to return ['a','b'].
>
> How can I do this?
>
> thx,
>
> -wes


I am surprised nobody gave you the simple answer yet that may even
work for your situation:

b=a[2:-2].split("','")
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A more pythonish code

2010-02-25 Thread nn


prasad_chand wrote:
> Hi,
>
> I use python to do simple math problems as a hobby.
>
> I have made a program that finds the number of divisors(factors) of a
> given number. I am hoping to improve my language skills, specifically
> I would like to re-write the function "prime_factors" more gracefully.
> While the program works right, I am hoping that I could get some input
> on how to write better python code. I have attached the code below.
>
>
> def prime_factors(n):
> """
> Reduce a number to its prime factors. e.g. 48 is 2^4,3^1 (add (4+1)
> (1+1) = 10)
>
> Updates a global dictionary(my_dict) with prime numbers and number
> of occurances. In the case of 48 {2:4,3:1}
>
> """
> tmp_n = n
>
> while True:
>
> if tmp_n == 1:
> break
>
> tmp_check = tmp_n
>
> for x in range(2,int(ceil(sqrt(tmp_n)) + 1)):
> if tmp_n % x == 0:
> add_to_dict(x)
> tmp_n /= x
> break
>
> if tmp_check == tmp_n: #number is prime...so just to add to
> dict
> add_to_dict(tmp_n)
> break
>
>
> def add_one(x):
> return x+1
>
>
> def mul(x,y):
> return x * y
>
> def add_to_dict(p_num):
> if my_dict.has_key(p_num):
> my_dict[p_num] += 1
> else:
> my_dict[p_num] = 1
>
>
> my_dict = {}
>
>
> prime_factors(135)
> l = map(add_one,my_dict.values())
> print reduce(mul, l, 1)
>
>
> Thanks for your time.

I did a quick refactoring for Python 3.1 (should mostly work in older
versions too):

from math import ceil, sqrt
from functools import reduce
from collections import defaultdict
from operator import mul

def prime_factors(n):
"""
Reduce a number to its prime factors. e.g. 48 is 2^4,3^1 (add
(4+1)
(1+1) = 10)
"""
factors = defaultdict(int)
while n != 1:
for x in range(2,int(ceil(sqrt(n)) + 1)):
if n % x == 0:
factors[x] += 1
n /= x
break
else:
#number is prime...so just to add to dict
factors[int(n)] += 1
break
return factors

factors = prime_factors(135)
exponents = [x+1 for x in factors.values()]
print(reduce(mul, exponents, 1))

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


Re: Can I specify regex group to return float or int instead of string?

2010-02-25 Thread nn
On Feb 25, 12:20 pm, Steven D'Aprano  wrote:
> On Thu, 25 Feb 2010 09:00:07 -0800, Jeremy wrote:
> > On Feb 25, 9:41 am, Steven D'Aprano  > cybersource.com.au> wrote:
> >> On Thu, 25 Feb 2010 07:48:44 -0800, Jeremy wrote:
> >> > I have a regular expression that searches for some numbers and puts
> >> > them into a dictionary, i.e.
>
> >> > '(?P\d+)\s+(?P\d+\.\d+)'
>
> >> > Is it possible to have the results of the matches returned as int or
> >> > float objects instead of strings?
>
> >> No. Just convert the match with int() or float() before storing it in
> >> the dictionary. That is, instead of:
>
> >> d[key] = match
>
> >> use
>
> >> d[key] = float(match)
>
> >> or similar.
>
> > I was afraid that was the only option.  Oh well, thought I'd ask anyway.
>
> Why "afraid"? What's the problem with calling int or float on the match?
>
> --
> Steven

He must not be Dutch

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


Re: loop over list and process into groups

2010-03-04 Thread nn


lbolla wrote:
> On Mar 4, 3:57 pm, Sneaky Wombat  wrote:
> > [ {'vlan_or_intf': 'VLAN2021'},
> >  {'vlan_or_intf': 'Interface'},
> >  {'vlan_or_intf': 'Po1'},
> >  {'vlan_or_intf': 'Po306'},
> >  {'vlan_or_intf': 'VLAN2022'},
> >  {'vlan_or_intf': 'Interface'},
> >  {'vlan_or_intf': 'Gi7/33'},
> >  {'vlan_or_intf': 'Po1'},
> >  {'vlan_or_intf': 'Po306'},
> >  {'vlan_or_intf': 'VLAN2051'},
> >  {'vlan_or_intf': 'Interface'},
> >  {'vlan_or_intf': 'Gi9/6'},
> >  {'vlan_or_intf': 'VLAN2052'},
> >  {'vlan_or_intf': 'Interface'},
> >  {'vlan_or_intf': 'Gi9/6'},]
> >
> > I want it to be converted to:
> >
> > [{'2021':['Po1','Po306']},{'2022':['Gi7/33','Po1','Po306']},etc etc]
> >
> > I was going to write a def to loop through and look for certain pre-
> > compiled regexs, and then put them in a new dictionary and append to a
> > list, but I'm having trouble thinking of a good way to capture each
> > dictionary.  Each dictionary will have a key that is the vlan and the
> > value will be a list of interfaces that participate in that vlan.
> > Each list will be variable, many containing only one interface and
> > some containing many interfaces.
> >
> > I thought about using itertools, but i only use that for fixed data.
> > I don't know of a good way to loop over variably sized data.  I was
> > wondering if anyone had any ideas about a good way to convert this
> > list or dictionary into the right format that I need.  The solution I
> > come up with will most likely be ugly and error prone, so I thought
> > i'd ask this python list while I work.  Hopefully I learn a better way
> > to solve this problem.
> >
> > Thanks!
> >
> > I also have the data in a list,
> >
> > [ 'VLAN4065',
> >  'Interface',
> >  'Gi9/6',
> >  'Po2',
> >  'Po3',
> >  'Po306',
> >  'VLAN4068',
> >  'Interface',
> >  'Gi9/6',
> >  'VLAN4069',
> >  'Interface',
> >  'Gi9/6',]
>
>
>
> ===
>
> from itertools import groupby
>
> data = \
> [ {'vlan_or_intf': 'VLAN2021'},
>  {'vlan_or_intf': 'Interface'},
>  {'vlan_or_intf': 'Po1'},
>  {'vlan_or_intf': 'Po306'},
>  {'vlan_or_intf': 'VLAN2022'},
>  {'vlan_or_intf': 'Interface'},
>  {'vlan_or_intf': 'Gi7/33'},
>  {'vlan_or_intf': 'Po1'},
>  {'vlan_or_intf': 'Po306'},
>  {'vlan_or_intf': 'VLAN2051'},
>  {'vlan_or_intf': 'Interface'},
>  {'vlan_or_intf': 'Gi9/6'},
>  {'vlan_or_intf': 'VLAN2052'},
>  {'vlan_or_intf': 'Interface'},
>  {'vlan_or_intf': 'Gi9/6'},]
>
> def clean_up(lst):
>   return [d.values()[0] for d in data if d.values()[0] != 'Interface']
>
> out = {}
> for k, g in groupby(clean_up(data) , key=lambda s:
> s.startswith('VLAN')):
>   if k:
>   key = list(g)[0].replace('VLAN','')
>   else:
>   out[key] = list(g)
>
> print out
> ===
>
> hth,
> L.

Good use of groupby. Here is what I ended up coming up:

from itertools import groupby
laninfo=[ 'VLAN4065',
 'Interface',
 'Gi9/6',
 'Po2',
 'Po3',
 'Po306',
 'VLAN4068',
 'Interface',
 'Gi9/6',
 'VLAN4069',
 'Interface',
 'Gi9/6',]

def splitgrp(s, f=[False]):
   f[0]^=s.startswith('VLAN')
   return f[0]
lanlst=(list(g) for k,g in groupby(laninfo,key=splitgrp))
out={item[0][4:]:item[2:] for item in lanlst}
print(out)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Evaluate my first python script, please

2010-03-04 Thread nn
On Mar 4, 2:30 pm, MRAB  wrote:
> Pete Emerson wrote:
> > I've written my first python program, and would love suggestions for
> > improvement.
>
> > I'm a perl programmer and used a perl version of this program to guide
> > me. So in that sense, the python is "perlesque"
>
> > This script parses /etc/hosts for hostnames, and based on terms given
> > on the command line (argv), either prints the list of hostnames that
> > match all the criteria, or uses ssh to connect to the host if the
> > number of matches is unique.
>
> > I am looking for advice along the lines of "an easier way to do this"
> > or "a more python way" (I'm sure that's asking for trouble!) or
> > "people commonly do this instead" or "here's a slick trick" or "oh,
> > interesting, here's my version to do the same thing".
>
> > I am aware that there are performance improvements and error checking
> > that could be made, such as making sure the file exists and is
> > readable and precompiling the regular expressions and not calculating
> > how many sys.argv arguments there are more than once. I'm not hyper
> > concerned with performance or idiot proofing for this particular
> > script.
>
> > Thanks in advance.
>
> > 
> > #!/usr/bin/python
>
> > import sys, fileinput, re, os
>
> > filename = '/etc/hosts'
>
> > hosts = []
>
> > for line in open(filename, 'r'):
> >    match = re.search('\d+\.\d+\.\d+\.\d+\s+(\S+)', line)
>
> Use 'raw' strings for regular expressions.
>
> 'Normal' Python string literals use backslashes in escape sequences (eg,
> '\n'), but so do regular expressions, and regular expressions are passed
> to the 're' module in strings, so that can lead to a profusion of
> backslashes!
>
> You could either escape the backslashes:
>
>         match = re.search('\\d+\\.\\d+\\.\\d+\\.\\d+\s+(\\S+)', line)
>
> or use a raw string:
>
>         match = re.search(r'\d+\.\d+\.\d+\.\d+\s+(\S+)', line)
>
> A raw string literal is just like a normal string literal, except that
> backslashes aren't special.
>
> >    if match is None or re.search('^(?:float|localhost)\.', line):
> > continue
>
> It would be more 'Pythonic' to say "not match".
>
>         if not match or re.search(r'^(?:float|localhost)\.', line):
>
> >    hostname = match.group(1)
> >    count = 0
> >    for arg in sys.argv[1:]:
> >            for section in hostname.split('.'):
> >                    if section == arg:
> >                            count = count + 1
>
> Shorter is:
>
>                                 count += 1
>
> >                            break
> >    if count == len(sys.argv) - 1:
> >            hosts.append(hostname)
>
> > if len(hosts) == 1:
> >    os.system("ssh -A " + hosts[0])
> > else:
> >    print '\n'.join(hosts)
>
> You're splitting the hostname repeatedly. You could split it just once,
> outside the outer loop, and maybe make it into a set. You could then
> have:
>
>          host_parts = set(hostname.split('.'))
>         count = 0
>         for arg in sys.argv[1:]:
>                 if arg in host_parts:
>                         count += 1
>
> Incidentally, the re module contains a cache, so the regular expressions
> won't be recompiled every time (unless you have a lot of them and the re
> module flushes the cache).

I think that you really just need a set intersection for the count
part and this should work:

host_parts = set(hostname.split('.'))
count = len(host_parts.intersection(sys.argv[1:]))

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


Re: loop over list and process into groups

2010-03-05 Thread nn


mk wrote:
> Sneaky Wombat wrote:
> > [ 'VLAN4065',
> >  'Interface',
> >  'Gi9/6',
> >  'Po2',
> >  'Po3',
> >  'Po306',
> >  'VLAN4068',
> >  'Interface',
> >  'Gi9/6',
> >  'VLAN4069',
> >  'Interface',
> >  'Gi9/6',]
>
> Hey, I just invented a cute ;-) two-liner using list comprehensions:
>
> # alist = list above
>
> tmp, dk = [], {}
> [(x.startswith('VLAN') and (dk.setdefault(x,[]) or tmp.append(x))) or
> (not x.startswith('VLAN') and dk[tmp[-1]].append(x))for x in alist
> if x != 'Interface']
>
> No need to use a nuke like itertools to kill a fly. ;-)
>
> Regards,
> mk

Oh my! You could have at least used some "if else" to make it a little
bit easier on the eyes :-)

[(dk.setdefault(x,[]) or tmp.append(x))
   if x.startswith('VLAN')
   else dk[tmp[-1]].append(x)
   for x in alist
   if x != 'Interface']
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: related lists mean value (golfed)

2010-03-09 Thread nn


Peter Otten wrote:
> Michael Rudolf wrote:
>
> > Am 09.03.2010 13:02, schrieb Peter Otten:
> > [sum(a for a,b in zip(x,y) if b==c)/y.count(c)for c in y]
> >> [1.5, 1.5, 8.0, 4.0, 4.0, 4.0]
> >> Peter
> >
> > ... pwned.
> > Should be the fastest and shortest way to do it.
>
> It may be short, but it is not particularly efficient. A dict-based approach
> is probably the fastest. If y is guaranteed to be sorted itertools.groupby()
> may also be worth a try.
>
> $ cat tmp_average_compare.py
> from __future__ import division
> from collections import defaultdict
> try:
> from itertools import izip as zip
> except ImportError:
> pass
>
> x = [1 ,2, 8, 5, 0, 7]
> y = ['a', 'a', 'b', 'c', 'c', 'c' ]
>
> def f(x=x, y=y):
> p = defaultdict(int)
> q = defaultdict(int)
> for a, b in zip(x, y):
> p[b] += a
> q[b] += 1
> return [p[b]/q[b] for b in y]
>
> def g(x=x, y=y):
> return [sum(a for a,b in zip(x,y)if b==c)/y.count(c)for c in y]
>
> if __name__ == "__main__":
> print(f())
> print(g())
> assert f() == g()
> $ python3 -m timeit -s 'from tmp_average_compare import f, g' 'f()'
> 10 loops, best of 3: 11.4 usec per loop
> $ python3 -m timeit -s 'from tmp_average_compare import f, g' 'g()'
> 1 loops, best of 3: 22.8 usec per loop
>
> Peter


I converged to the same solution but had an extra reduction step in
case there were a lot of repeats in the input. I think it is a good
compromise between efficiency, readability and succinctness.

x = [1 ,2, 8, 5, 0, 7]
y = ['a', 'a', 'b', 'c', 'c', 'c' ]
from collections import defaultdict
totdct = defaultdict(int)
cntdct = defaultdict(int)
for name, num in zip(y,x):
   totdct[name] += num
   cntdct[name] += 1
avgdct = {name : totdct[name]/cnts for name, cnts in cntdct.items()}
w = [avgdct[name] for name in y]

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


Re: Searching for most pythonic/least stupid way to do something simple

2010-03-16 Thread nn


david jensen wrote:
> ... and of course i screwed up my outcomes... that should read
> outcomes=[[4,3,8,3,5],[3,4,8,3,5],[2,5,8,3,5],[1,6,8,3,5],[0,7,8,3,5]]

For starters:

def getOutcomes(myList=[2,5,8,3,5]):
   low_id = int(myList[0]>myList[1])
   amountToShare = 2*myList[low_id]
   remainder = myList[not low_id]-myList[low_id]
   tail=myList[2:]
   outcomes = [[amountToShare*perc, remainder+amountToShare*(1-perc)]
+ tail
 for perc in (1.0, 0.75, 0.5, 0.25, 0.0)]
   return outcomes
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Searching for most pythonic/least stupid way to do something simple

2010-03-17 Thread nn


Michael Torrie wrote:
> david jensen wrote:
> > of course, changing nn's to:
> > def getOutcomes(myList=[2,5,8,3,5]):
> >low_id = int(myList[0]>myList[1])
> >amountToShare = 2*myList[low_id]
> >remainder = myList[not low_id]-myList[low_id]
> >tail=list(myList[2:])
> >outcomes = [[amountToShare*perc, remainder+amountToShare*(1-perc)]+
> > tail for perc in (1.0, 0.75, 0.5, 0.25, 0.0)] if not low_id else
> > [[remainder+amountToShare*perc, amountToShare*(1-perc)]+ tail for perc
> > in (1.0, 0.75, 0.5, 0.25, 0.0)]
> >return outcomes
> >
> >
> > works, just hides the ugliness in a more compact form
>
> If Gerard's code works, I would consider it far superior to your code
> here.  Pythonic does not necessarily mean short and ugly, nor does it
> mean that you have to always use list comprehensions.   Having a
> readable algorithm that's easy to follow in the future is a far better
> way than trying to use python's cool features to compact the code to as
> small and unreadable section as possible.
>
> I used to use list comprehension all the time, but I've found that often
> an explicit for loop is a much better solution in terms of
> maintainability.  Especially when you start seeing nested comprehensions
> such as you have here.

To be fair, that list comprehension was unnecessary complicated. The
following version does the same thing and still looks pretty
reasonable IMHO:

def getOutcomes(myList=[2,5,8,3,5]):
   low_id = int(myList[0]>myList[1])
   high_id = not low_id
   smaller = myList[low_id]
   bigger = myList[high_id]
   amountToShare = 2*smaller
   remainder = bigger-smaller
   remain0 = low_id*remainder
   remain1 = high_id*remainder
   tail = list(myList[2:])
   percents = (1.0, 0.75, 0.5, 0.25, 0.0)
   outcomes = [[remain0+amountToShare*perc, remain1+amountToShare*(1-
perc)]
   +tail for perc in percents]
   return outcomes
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: short-circuiting any/all ?

2010-03-22 Thread nn


kj wrote:
> I have a list of items L, and a test function is_invalid that checks
> the validity of each item.  To check that there are no invalid
> items in L, I could check the value of any(map(is_invalid, L)).
> But this approach is suboptimal in the sense that, no matter what
> L is, is_invalid will be executed for all elements of L, even though
> the value returned by any() is fully determined by the first True
> in its argument.  In other words, all calls to is_invalid after
> the first one to return True are superfluous.  Is there a
> short-circuiting counterpart to any(map(is_invalid, L)) that avoids
> these superfluous calls?
>
> OK, there's this one, of course:
>
> def _any_invalid(L):
> for i in L:
> if is_invalid(i):
> return True
> return False
>
> But is there anything built-in?  (I imagine that a lazy version of
> map *may* do the trick, *if* any() will let it be lazy.)
>
> TIA!
>
> ~K

If you are in Python 3 "any(map(is_invalid, L))" should short circuit.
If you are in Python 2 use "from itertools import imap;
any(imap(is_invalid, L))"
-- 
http://mail.python.org/mailman/listinfo/python-list


Unicode blues in Python3

2010-03-23 Thread nn
I know that unicode is the way to go in Python 3.1, but it is getting
in my way right now in my Unix scripts. How do I write a chr(253) to a
file?

#nntst2.py
import sys,codecs
mychar=chr(253)
print(sys.stdout.encoding)
print(mychar)

 > ./nntst2.py
ISO8859-1
ý

 > ./nntst2.py >nnout2
Traceback (most recent call last):
  File "./nntst2.py", line 5, in 
print(mychar)
UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in
position 0: ordinal not in range(128)

> cat nnout2
ascii

..Oh great!

ok lets try this:
#nntst3.py
import sys,codecs
mychar=chr(253)
print(sys.stdout.encoding)
print(mychar.encode('latin1'))

> ./nntst3.py
ISO8859-1
b'\xfd'

> ./nntst3.py >nnout3

> cat nnout3
ascii
b'\xfd'

..Eh... not what I want really.

#nntst4.py
import sys,codecs
mychar=chr(253)
print(sys.stdout.encoding)
sys.stdout=codecs.getwriter("latin1")(sys.stdout)
print(mychar)

 > ./nntst4.py
ISO8859-1
Traceback (most recent call last):
  File "./nntst4.py", line 6, in 
print(mychar)
  File "Python-3.1.2/Lib/codecs.py", line 356, in write
self.stream.write(data)
TypeError: must be str, not bytes

..OK, this is not working either.

Is there any way to write a value 253 to standard output?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode blues in Python3

2010-03-23 Thread nn


Rami Chowdhury wrote:
> On Tuesday 23 March 2010 10:33:33 nn wrote:
> > I know that unicode is the way to go in Python 3.1, but it is getting
> > in my way right now in my Unix scripts. How do I write a chr(253) to a
> > file?
> >
> > #nntst2.py
> > import sys,codecs
> > mychar=chr(253)
> > print(sys.stdout.encoding)
> > print(mychar)
>
> The following code works for me:
>
> $ cat nnout5.py
> #!/usr/bin/python3.1
>
> import sys
> mychar = chr(253)
> sys.stdout.write(mychar)
> $ echo $(cat nnout)
> ý
>
> Can I ask why you're using print() in the first place, rather than writing
> directly to a file? Python 3.x, AFAIK, distinguishes between text and binary 
> > files and will let you specify the encoding you want for strings you write.
>
> Hope that helps,
> Rami
> >
> >  > ./nntst2.py
> >
> > ISO8859-1
> > ý
> >
> >  > ./nntst2.py >nnout2
> >
> > Traceback (most recent call last):
> >   File "./nntst2.py", line 5, in 
> > print(mychar)
> > UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in
> > position 0: ordinal not in range(128)
> >
> > > cat nnout2
> >
> > ascii
> >
> > ..Oh great!
> >
> > ok lets try this:
> > #nntst3.py
> > import sys,codecs
> > mychar=chr(253)
> > print(sys.stdout.encoding)
> > print(mychar.encode('latin1'))
> >
> > > ./nntst3.py
> >
> > ISO8859-1
> > b'\xfd'
> >
> > > ./nntst3.py >nnout3
> > >
> > > cat nnout3
> >
> > ascii
> > b'\xfd'
> >
> > ..Eh... not what I want really.
> >
> > #nntst4.py
> > import sys,codecs
> > mychar=chr(253)
> > print(sys.stdout.encoding)
> > sys.stdout=codecs.getwriter("latin1")(sys.stdout)
> > print(mychar)
> >
> >  > ./nntst4.py
> >
> > ISO8859-1
> > Traceback (most recent call last):
> >   File "./nntst4.py", line 6, in 
> > print(mychar)
> >   File "Python-3.1.2/Lib/codecs.py", line 356, in write
> > self.stream.write(data)
> > TypeError: must be str, not bytes
> >
> > ..OK, this is not working either.
> >
> > Is there any way to write a value 253 to standard output?
>

#nntst5.py
import sys
mychar=chr(253)
sys.stdout.write(mychar)

> ./nntst5.py >nnout5
Traceback (most recent call last):
  File "./nntst5.py", line 4, in 
sys.stdout.write(mychar)
UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in
position 0: ordinal not in range(128)

equivalent to print.

I use print so I can do tests and debug runs to the screen or pipe it
to some other tool and then configure the production bash script to
write the final output to a file of my choosing.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode blues in Python3

2010-03-23 Thread nn


Gary Herron wrote:
> nn wrote:
> > I know that unicode is the way to go in Python 3.1, but it is getting
> > in my way right now in my Unix scripts. How do I write a chr(253) to a
> > file?
> >
>
> Python3 make a distinction between bytes and string(i.e., unicode)
> types, and you are still thinking in the Python2 mode that does *NOT*
> make such a distinction.  What you appear to want is to write a
> particular byte to a file -- so use the bytes type and a file open in
> binary mode:
>
>  >>> b=bytes([253])
>  >>> f = open("abc", 'wb')
>  >>> f.write(b)
> 1
>  >>> f.close()
>
> On unix (at least), the "od" program can verify the contents is correct:
>  > od abc -d
> 000   253
> 001
>
>
> Hope that helps.
>
> Gary Herron
>
>
>
> > #nntst2.py
> > import sys,codecs
> > mychar=chr(253)
> > print(sys.stdout.encoding)
> > print(mychar)
> >
> >  > ./nntst2.py
> > ISO8859-1
> > ý
> >
> >  > ./nntst2.py >nnout2
> > Traceback (most recent call last):
> >   File "./nntst2.py", line 5, in 
> > print(mychar)
> > UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in
> > position 0: ordinal not in range(128)
> >
> >
> >> cat nnout2
> >>
> > ascii
> >
> > ..Oh great!
> >
> > ok lets try this:
> > #nntst3.py
> > import sys,codecs
> > mychar=chr(253)
> > print(sys.stdout.encoding)
> > print(mychar.encode('latin1'))
> >
> >
> >> ./nntst3.py
> >>
> > ISO8859-1
> > b'\xfd'
> >
> >
> >> ./nntst3.py >nnout3
> >>
> >
> >
> >> cat nnout3
> >>
> > ascii
> > b'\xfd'
> >
> > ..Eh... not what I want really.
> >
> > #nntst4.py
> > import sys,codecs
> > mychar=chr(253)
> > print(sys.stdout.encoding)
> > sys.stdout=codecs.getwriter("latin1")(sys.stdout)
> > print(mychar)
> >
> >  > ./nntst4.py
> > ISO8859-1
> > Traceback (most recent call last):
> >   File "./nntst4.py", line 6, in 
> > print(mychar)
> >   File "Python-3.1.2/Lib/codecs.py", line 356, in write
> > self.stream.write(data)
> > TypeError: must be str, not bytes
> >
> > ..OK, this is not working either.
> >
> > Is there any way to write a value 253 to standard output?
> >

Actually what I want is to write a particular byte to standard output,
and I want this to work regardless of where that output gets sent to.
I am aware that I could do
open('nnout','w',encoding='latin1').write(mychar) but I am porting a
python2 program and don't want to rewrite everything that uses that
script.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode blues in Python3

2010-03-23 Thread nn


Stefan Behnel wrote:
> nn, 23.03.2010 19:46:
> > Actually what I want is to write a particular byte to standard output,
> > and I want this to work regardless of where that output gets sent to.
> > I am aware that I could do
> > open('nnout','w',encoding='latin1').write(mychar) but I am porting a
> > python2 program and don't want to rewrite everything that uses that
> > script.
>
> Are you writing text or binary data to stdout?
>
> Stefan

latin1 charset text.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode blues in Python3

2010-03-24 Thread nn


Martin v. Loewis wrote:
> nn wrote:
> >
> > Stefan Behnel wrote:
> >> nn, 23.03.2010 19:46:
> >>> Actually what I want is to write a particular byte to standard output,
> >>> and I want this to work regardless of where that output gets sent to.
> >>> I am aware that I could do
> >>> open('nnout','w',encoding='latin1').write(mychar) but I am porting a
> >>> python2 program and don't want to rewrite everything that uses that
> >>> script.
> >> Are you writing text or binary data to stdout?
> >>
> >> Stefan
> >
> > latin1 charset text.
>
> Are you sure about that? If you carefully reconsider, could you come to
> the conclusion that you are not writing text at all, but binary data?
>
> If it really was text that you write, why do you need to use
> U+00FD (LATIN SMALL LETTER Y WITH ACUTE). To my knowledge, that
> character is really infrequently used in practice. So that you try to
> write it strongly suggests that it is not actually text what you are
> writing.
>
> Also, your formulation suggests the same:
>
> "Is there any way to write a value 253 to standard output?"
>
> If you would really be writing text, you'd ask
>
>
> "Is there any way to write '�' to standard output?"
>
> Regards,
> Martin

To be more informative I am both writing text and binary data
together. That is I am embedding text from another source into stream
that uses non-ascii characters as "control" characters. In Python2 I
was processing it mostly as text containing a few "funny" characters.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode blues in Python3

2010-03-24 Thread nn


Steven D'Aprano wrote:
> On Tue, 23 Mar 2010 11:46:33 -0700, nn wrote:
>
> > Actually what I want is to write a particular byte to standard output,
> > and I want this to work regardless of where that output gets sent to.
>
> What do you mean "work"?
>
> Do you mean "display a particular glyph" or something else?
>
> In bash:
>
> $ echo -e "\0101"  # octal 101 = decimal 65
> A
> $ echo -e "\0375"  # decimal 253
> �
>
> but if I change the terminal encoding, I get this:
>
> $ echo -e "\0375"
> ý
>
> Or this:
>
> $ echo -e "\0375"
> ²
>
> depending on which encoding I use.
>
> I think your question is malformed. You need to work out what behaviour
> you actually want, before you can ask for help on how to get it.
>
>
>
> --
> Steven

Yes sorry it is a bit ambiguous. I don't really care what glyph is,
the program reading my output reads 8 bit values expects the binary
value 0xFD as control character and lets everything else through as is.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode blues in Python3

2010-03-24 Thread nn


Antoine Pitrou wrote:
> Le Tue, 23 Mar 2010 10:33:33 -0700, nn a écrit :
>
> > I know that unicode is the way to go in Python 3.1, but it is getting in
> > my way right now in my Unix scripts. How do I write a chr(253) to a
> > file?
> >
> > #nntst2.py
> > import sys,codecs
> > mychar=chr(253)
> > print(sys.stdout.encoding)
> > print(mychar)
>
> print() writes to the text (unicode) layer of sys.stdout.
> If you want to access the binary (bytes) layer, you must use
> sys.stdout.buffer. So:
>
>   sys.stdout.buffer.write(chr(253).encode('latin1'))
>
> or:
>
>   sys.stdout.buffer.write(bytes([253]))
>
> See http://docs.python.org/py3k/library/io.html#io.TextIOBase.buffer

Just what I needed! Now I full control of the output.

Thanks Antoine. The new io stack is still a bit of a mystery to me.

Thanks everybody else, and sorry for confusing the issue. Latin1 just
happens to be very convenient to manipulate bytes and is what I
thought of initially to handle my mix of textual and non-textual data.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bunch 2.0 - a dict with a default

2010-11-19 Thread nn
On Nov 18, 8:45 pm, Phlip  wrote:
> Pythonistas:
>
> If everyone likes this post, then the code is a "snippet" for
> community edification. Otherwise, it's a question: How to do this kind
> of thing better?
>
> I want a dict() variant that passes these test cases:
>
>         map = Map()
>         assert not(map.has_key('_default'))
>
>         map = Map(yo='dude')
>         assert map['yo'] == 'dude'
>         assert map.yo == 'dude'
>         assert None == map['whatever']
>         assert not(map.has_key('_default'))
>
>         map = Map(yo='dude', _default='q')
>         assert 'q' == map['whatever']
>         assert not(map.has_key('_default'))
>
> That's like Bunch, but with a default value. (That makes code with
> excess if statements less likely.)
>
> So here's the implementation:
>
> def Map(*args, **kwargs):
>     value = kwargs.get('_default', None)
>     if kwargs.has_key('_default'):  del kwargs['_default']
>
>     class _DefMap(dict):
>         def __init__(self, *a, **kw):
>             dict.__init__(self, *a, **kw)
>             self.__dict__ = self
>
>         def __getitem__(self, key):
>             if not self.has_key(key):  self[key] = value
>             return dict.__getitem__(self, key)
>
>     return _DefMap(*args, **kwargs)
>
> --
>   Phlip
>  http://bit.ly/ZeekLand

Looks like some combination of defaultdict and namedtuple.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: use of __new__ to permit "dynamic" completion within (any?) IDE ?

2010-12-08 Thread nn
On Dec 7, 10:52 am, gst  wrote:
> Hi,
>
> I met a situation where I was passing an object created in/with an
> upper level module class to a lower level module class' instance in
> one of its __init__ argument and saving a ref of the upper object in
> that lower level class' new instance.
>
> But in my IDE I want the completion to also work from within the lower
> level module when it's refering to the object passed from the upper
> level:
>
> Well, I'm sure I'm not very clear in my terms (and probably a bit long
> in the sentence) so here it is in code:
>
> files:
> module1.py
> subpackage/module2.py
>
> file module1.py:
>
> from subpackage.module2 import class2
>
> class class1(object):
>
>     def __new__(cls, _self=None, *args, **kwargs):
>         if _self:  ## we've been passed an instance already
> initialyzed
>                      ## so directly return it instead of creating a
> new object.
>             return _self
>         return object.__new__(cls)
>
>     def __init__(self, _self=None, *args, **kwargs):
>         if _self:  ## we've been passed an instance already
> initialyzed
>                      ## so directly returns
>             ## assert(self is _self) ?
>             return
>         self.object2 = class2(object1=self, "blip", "blop")
>         # others init
>
> file module2.py:
>
> class class2(object):
>
>     def __init__(self, object1, *args, **kwargs):
>
>         from ..module1 import class1
>
>         self.object1 = class1(_self=object1)    ## instead of:
> self.object1 = object1
>
>     ## others functions and/or init..
>     ## where  now I've completion working on self.object1 :
>     ## if I add(or remove) fields/methods in module1 (and save) then
>     ## I have them available(or disappeared) in the completion when
> executed from this submodule.
>     ## This ofcourse permits to save me of remembering all of the
> class1 attributes/methods when I'm working with self.object1 from
> within one of class2 methods.
>
> What do you think of this ?
>
> I guess there can be others ways of doing this..  ?
>
> Thanks,
>
> Regards,
>
> Greg.
>
> note: I'm using Eclipse 3.5.2 with pydev so (I don't really know how
> others IDE can handle this case) .

You might want to test with Wing IDE http://wingware.com/
using the 10 day trial version to see if that works just for kicks.
I've heard it has the best autocomplete of all IDEs out there.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: completely implicit interpolation based on a frame object

2010-12-09 Thread nn
On Dec 9, 2:29 am, Edward Peschko  wrote:
> >> Any ideas would be great on this, including pitfalls that people see
> >> in implementing it.
>
> >http://docs.python.org/library/string.html#template-strings
>
> > regards
> >  Steve
>
> Steve,
>
> Thanks for the tip, I did look at templates and decided that they
> weren't quite completely what I was looking for, although they could
> be part of the solution.
>
> 1. they require ${ } around the variables in question that you want to
> interpolate. When run through the trace hook, normal code doesn't do
> that.
> 2. they don't provide (AFAICT) a complete interpolation solution.
> Suppose I want to define custom interpolations in my tracer, like say,
> expanding out lists and dicts, or fleshing out objects using their
> stringification method. Does template do this?
>
> Formats may be a bit closer here, but I'm not sure if they are
> workable (or usable) with 2.5... which is where I need to work.
>
> Ed

One of the solutions from here might work for you:

http://wiki.python.org/moin/Templating
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Deprecation warnings (2.7 -> 3 )

2010-12-10 Thread nn
On Dec 9, 10:15 pm, rusi  wrote:
> In trying to get from 2.x to 3 Terry suggested I use 2.7 with
> deprecation warnings
>
> Heres the (first) set
>
> DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__
> in 3.x
> DeprecationWarning: callable() not supported in 3.x; use isinstance(x,
> collections.Callable)
>
> Is there one place/site I can go to for understanding all these and
> trying to clear them?

I don't know any place that explains those. But the second is a pretty
straightforward: replace "callable(x)" by "isinstance(x,
collections.Callable)"

The first one is more tricky. You have to find the class with a __eq__
method in it and copy and paste the __hash__ method from its
superclass into itself.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Deprecation warnings (2.7 -> 3 )

2010-12-10 Thread nn
On Dec 10, 11:17 am, nn  wrote:
> On Dec 9, 10:15 pm, rusi  wrote:
>
> > In trying to get from 2.x to 3 Terry suggested I use 2.7 with
> > deprecation warnings
>
> > Heres the (first) set
>
> > DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__
> > in 3.x
> > DeprecationWarning: callable() not supported in 3.x; use isinstance(x,
> > collections.Callable)
>
> > Is there one place/site I can go to for understanding all these and
> > trying to clear them?
>
> I don't know any place that explains those. But the second is a pretty
> straightforward: replace "callable(x)" by "isinstance(x,
> collections.Callable)"
>
> The first one is more tricky. You have to find the class with a __eq__
> method in it and copy and paste the __hash__ method from its
> superclass into itself.

And BTW callable was deprecated in 3.0 and then added back in for 3.2.
So if you decide to not support 3.0 and 3.1 you would be fine just
leaving it as is.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: decouple copy of a list

2010-12-10 Thread nn
On Dec 10, 8:48 am, Dirk Nachbar  wrote:
> I want to take a copy of a list a
>
> b=a
>
> and then do things with b which don't affect a.
>
> How can I do this?
>
> Dirk

Not knowing the particulars,
you may have to use:

import copy
b=copy.deepcopy(a)
-- 
http://mail.python.org/mailman/listinfo/python-list


move to end, in Python 3.2 Really?

2011-01-17 Thread nn
I somehow missed this before. I like most of the additions from
Raymond Hettinger. But the api on this baffles me a bit:

>>> d = OrderedDict.fromkeys('abcde')

>>> d.move_to_end('b', last=False)

>>> ''.join(d.keys)
'bacde'

I understand that "end" could potentially mean either end, but would
"move_to_end" and "move_to_beginning" not have been clearer?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: move to end, in Python 3.2 Really?

2011-01-18 Thread nn
On Jan 18, 12:20 am, Raymond Hettinger  wrote:
> On Jan 17, 6:51 pm, nn  wrote:
>
> > ...But the api on this baffles me a bit:
>
> > >>> d = OrderedDict.fromkeys('abcde')
> > >>> d.move_to_end('b', last=False)
> > >>> ''.join(d.keys)
>
> > 'bacde'
>
> > I understand that "end" could potentially mean either end, but would
> > "move_to_end" and "move_to_beginning" not have been clearer?
>
> The default (and normal usage) is to move an item to the last
> position.
> So, od.move_to_end(k) becomes a fast equivalent to v=d.pop(k)
> followed by d[k]=v.
>
> The less common usage of moving to the beginning is done with
> last=False.  This parallels the existing API for od.popitem():
>
> >>> od = OrderedDict.fromkeys('abcdefghi')
> >>> od.move_to_end('c')               # default case:  move to last
> >>> od.popitem()                      # default case:  pop from last
> ('c', None)
> >>> od.move_to_end('d', last=False)   # other case:    move to first
> >>> od.popitem(last=False)            # other case:    pop from first
>
> ('d', None)
>
> The existing list.pop() API is similar (though it takes an index
> value instead of a boolean):
>
> >>> mylist.pop()                      # default case:  pop from last
> >>> mylist.pop(0)                     # other case:    pop from first
>
> Those were the design considerations.  Sorry you didn't like the
> result.
>
> Raymond

Ah that is where it came from! I didn't remember popitem used that API
too. If you use them together it has a nice symmetry. I guess it is
just that "end" is more confusing than "pop" in that context.

Considering the precedence of popitem I withdraw my objection. I still
think it looks a bit odd but it is not unreasonable either. Sometimes
ugly consistency trumps beautiful inconsistency; c'est la vie...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: itertools.groupby usage to get structured data

2011-02-07 Thread nn
On Feb 5, 7:12 am, Peter Otten <__pete...@web.de> wrote:
> Slafs wrote:
> > Hi there!
>
> > I'm having trouble to wrap my brain around this kind of problem:
>
> > What I have :
> >   1) list of dicts
> >   2) list of keys that i would like to be my grouping arguments of
> > elements from 1)
> >   3) list of keys that i would like do "aggregation" on the elements
> > of 1) with some function e.g. sum
>
> > For instance i got:
> > 1) [ { 'g1' : 1, 'g2' : 8, 's_v1' : 5.0, 's_v2' : 3.5 },
> >       { 'g1' : 1, 'g2' : 9, 's_v1' : 2.0, 's_v2' : 3.0 },
> >       {'g1' : 2, 'g2' : 8, 's_v1' : 6.0, 's_v2' : 8.0}, ... ]
> > 2) ['g1', 'g2']
> > 3) ['s_v1', 's_v2']
>
> > To be precise 1) is a result of a values_list method from a QuerySet
> > in Django; 2) is the arguments for that method; 3) those are the
> > annotation keys. so 1) is a result of:
> >    qs.values_list('g1', 'g2').annotate(s_v1=Sum('v1'), s_v2=Sum('v2'))
>
> > What i want to have is:
> > a "big" nested dictionary with 'g1' values as 1st level keys and a
> > dictionary of aggregates and "subgroups" in it.
>
> > In my example it would be something like this:
> > {
> >   1 : {
> >           's_v1' : 7.0,
> >           's_v2' : 6.5,
> >           'g2' :{
> >                    8 : {
> >                           's_v1' : 5.0,
> >                           's_v2' : 3.5 },
> >                    9 :  {
> >                           's_v1' : 2.0,
> >                           's_v2' : 3.0 }
> >                 }
> >        },
> >   2 : {
> >            's_v1' : 6.0,
> >            's_v2' : 8.0,
> >            'g2' : {
> >                     8 : {
> >                           's_v1' : 6.0,
> >                           's_v2' : 8.0}
> >            }
> >        },
> > ...
> > }
>
> > # notice the summed values of s_v1 and s_v2 when g1 == 1
>
> > I was looking for a solution that would let me do that kind of
> > grouping with variable lists of 2) and 3) i.e. having also 'g3' as
> > grouping element so the 'g2' dicts could also have their own
> > "subgroup" and be even more nested then.
> > I was trying something with itertools.groupby and updating nested
> > dicts, but as i was writing the code it started to feel too verbose to
> > me :/
>
> > Do You have any hints maybe? because i'm kind of stucked :/
>
> > Regards
>
> > Sławek
>
> Not super-efficient, but simple:
>
> $ cat python sumover.py
> cat: python: No such file or directory
> data = [ { 'g1' : 1, 'g2' : 8, 's_v1' : 5.0, 's_v2' : 3.5 },
>          { 'g1' : 1, 'g2' : 9, 's_v1' : 2.0, 's_v2' : 3.0 },
>          {'g1' : 2, 'g2' : 8, 's_v1' : 6.0, 's_v2' : 8.0}]
> sum_over = ["s_v1", "s_v2"]
> group_by = ["g1", "g2"]
>
> wanted = {
>   1 : {
>           's_v1' : 7.0,
>           's_v2' : 6.5,
>           'g2' :{
>                    8 : {
>                           's_v1' : 5.0,
>                           's_v2' : 3.5 },
>                    9 :  {
>                           's_v1' : 2.0,
>                           's_v2' : 3.0 }
>                 }
>        },
>   2 : {
>            's_v1' : 6.0,
>            's_v2' : 8.0,
>            'g2' : {
>                     8 : {
>                           's_v1' : 6.0,
>                           's_v2' : 8.0}
>            }
>        },
>
> }
>
> def calc(data, group_by, sum_over):
>     tree = {}
>     group_by = group_by + [None]
>     for item in data:
>         d = tree
>         for g in group_by:
>             for so in sum_over:
>                 d[so] = d.get(so, 0.0) + item[so]
>             if g:
>                 d = d.setdefault(g, {}).setdefault(item[g], {})
>     return tree
>
> got = calc(data, group_by, sum_over)[group_by[0]]
> assert got == wanted
> $ python sumover.py
> $
>
> Untested.

Very clever. I didn't understand how it worked until I rewrote it like
this:

def calc(data, group_by, sum_over):
tree = {}
group_by = [None] + group_by
for item in data:
d = tree
for g in group_by:
if g:
d = d.setdefault(g, {}).setdefault(item[g], {})
for so in sum_over:
d[so] = d.get(so, 0.0) + item[so]
return tree


Processing "None" in the last round of the loop was throwing me off.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Executing functions

2011-02-11 Thread nn
On Feb 11, 9:15 am, DataSmash  wrote:
> Can someone help me understand why Example #1 & Example #2 will run
> the functions,
> while Example #3 DOES NOT?
> Thanks for your time!
> R.D.
>
> def One():
>     print "running fuction 1"
> def Two():
>     print "running fuction 2"
> def Three():
>     print "running fuction 3"
>
> # Example #1
> fList = ["Two()","Three()"]
> for func in fList:
>     exec func
>
> # Example #2
> Two()
> Three()
>
> # Example #2
> fList = ["Two()","Three()"]
> for func in fList:
>     func

Example 1 is executing the code inside strings
Example 2 is evaluating two functions and throwing the result away
Example 3 is evaluating literals and throwing the result away

I don't know why you would expect the third example to run the
functions.
Maybe running this version will enlighten you:

fList = ["random","stuff"]
for func in fList:
func

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


Re: Why these don't work??

2010-04-08 Thread nn


M. Hamed wrote:
> I'm trying the following statements that I found here and there on
> Google, but none of them works on my Python 2.5, are they too old? or
> newer?
>
> "abc".reverse()
> import numpy

reverse does not work on strings but does work on lists:
>>> x=list("abc")
>>> x.reverse()
>>> x
['c', 'b', 'a']

or maybe this:
>>> ''.join(reversed("abc"))
'cba'

as far as numpy you need to install it first:

http://pypi.python.org/pypi/numpy/


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


Re: Missing DLL in win98

2010-06-04 Thread nn
On Jun 4, 9:53 am, Spyder42  wrote:
> On Fri, 04 Jun 2010 15:32:15 +0200, Christian Heimes
>
>  wrote:
> >> So your response is either, you don't know if there is a fix, or 'No
> >> way in h377.' You couldn't figure out by my post that I already knew
> >> that?
>
> >Let me paraphrase my answer:
>
> >You can't run Python 2.6 on Windows 98 because we have dropped support
> >for any Windows older than Windows 2000 SP4. It's documented at
> >http://docs.python.org/whatsnew/2.6.html#port-specific-changes-windows,
> >too.
>
> >Christian
>
> Yes, I get that. So your answer is... You have no idea as to weather
> there is a workaround, or a fix, or patch or anything that would help.
> right? I don't consider buying an OS to be a valid workaround.
>
>         Thanks
>         Spyder

Correct. If you use Windows 98, you are stuck at the 2.5 series of
Python. As far as I know (Christian might correct me) there is no fix
or patch to allow otherwise.

The usual volunteers that build the Windows version of Python don't
care about the extra work of maintaining Windows 98 support anymore
and nobody else has stepped forward to do it or to pay somebody to do
so.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The inverse of .join

2010-06-17 Thread nn

Neil Cerutti wrote:
> What's the best way to do the inverse operation of the .join
> function?
>
> --
> Neil Cerutti

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


Re: new to python - trouble calling a function from another function

2010-08-06 Thread nn
On Aug 5, 2:01 pm, Daniel Urban  wrote:
> > I'm building an elevator simulator for a class assignment. I recently ran
> > into a roadblock and don't know how to fix it. For some reason, in my
> > checkQueue function below, the call to self.goUp() is never executed. It is
> > on the last line of code I pasted in. I can put print statements before and
> > after the call and I have a print statement in goUp() itself.  Only the
> > print statements before and after the call are executed. The one inside
> > goUp() is never executed because goUp() never seems to be executed. How can
> > that be? I don't get any errors when the script executes. Surely this isn't
> > some limitation I'm encountering?
>
> I think the self.goUp() call is executed, the goUp function gets
> called, returns a generator object (because goUp is a generator
> function), then you don't use that generator object for anything.
>
> Daniel

Brandon, this example might help you understand the problem:

>>> def g():
print('p1')
yield 2
print('p3')


>>> g()

>>> b=g()
>>> next(b)
p1
2
>>> next(b)
p3
Traceback (most recent call last):
  File "", line 1, in 
next(b)
StopIteration
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regular expression issue

2010-08-09 Thread nn
On Aug 9, 9:18 am, genxtech  wrote:
> On Aug 8, 7:34 pm, Tim Chase  wrote:
>
>
>
> > On 08/08/10 17:20, genxtech wrote:
>
> > > if re.search(search_string, in_string) != None:
>
> > While the other responses have addressed some of the big issues,
> > it's also good to use
>
> >    if thing_to_test is None:
>
> > or
>
> >    if thing_to_test is not None:
>
> > instead of "== None" or "!= None".
>
> > -tkc
>
> I would like to thank all of you for your responses.  I understand
> what the regular expression means, and am aware of the double negative
> nature of the test.  I guess what I am really getting at is why the
> last test returns a value of None, and even when using the syntax
> suggested in this quoted solution, the code for the last test is doing
> the opposite of the previous 2 tests that also returned a value of
> None.  I hope this makes sense and clarifies what I am trying to ask.
> Thanks


First: You understand the regular expression and the double negative
but not both of them together, otherwise you would not be asking here.
The suggestion of refactoring the code is that down the road you or
somebody else doing maintenance will have to read it again. Good books
avoid confusing grammar, likewise, good programs avoid confusing
logic.

Second: the root of your problem is the mistaken believe that P=>Q
implies (not P)=>(not Q); This is not so. Let me give an example: if
you say "if it rains" then "the ground is wet" that does not imply "if
it doesn't rain" then "the ground is not wet". You could be watering
the plants for instance. Saying "if the word finishes with a consonant
and an y" then "ay, ey, iy, oy and uy are not at the end of the word"
does not imply that "if the word does not finish with a consonant and
an y" then "ay, ey, iy, oy or uy were found at the end of the word".
The word could end in x for instance.

I hope I didn't make it more confusing, otherwise other people will
probably chime in to make it clear to you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: passing variables as object attributes

2010-08-16 Thread nn
On Aug 16, 10:08 am, Vikas Mahajan  wrote:
> On 16 August 2010 19:23, Nitin Pawar  wrote:> you 
> would need to define a class first with its attiributes and then you may
> > want to initiate the variables by calling the class initilializer
>
> Actually I have to dynamically add attributes to a object. I am
> writing python script for  FreeCAD software. I am using loop to create
> multiple cylinders and I am writing following code-:
> cyname = "Cylinder"
> FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname)
> FreeCAD.ActiveDocument.cyname.Radius= .5
> FreeCAD.ActiveDocument.cyname.Height= 10
>
> And I am getting this error-:
> AttributeError: 'App.Document' object has no attribute 'cyname'
>
> But when I use Cylinder in place of cyname, I am not getting any error.
>
> Please help.
>
> Thanks.

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


Re: passing variables as object attributes

2010-08-16 Thread nn
On Aug 16, 10:08 am, Vikas Mahajan  wrote:
> On 16 August 2010 19:23, Nitin Pawar  wrote:> you 
> would need to define a class first with its attiributes and then you may
> > want to initiate the variables by calling the class initilializer
>
> Actually I have to dynamically add attributes to a object. I am
> writing python script for  FreeCAD software. I am using loop to create
> multiple cylinders and I am writing following code-:
> cyname = "Cylinder"
> FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname)
> FreeCAD.ActiveDocument.cyname.Radius= .5
> FreeCAD.ActiveDocument.cyname.Height= 10
>
> And I am getting this error-:
> AttributeError: 'App.Document' object has no attribute 'cyname'
>
> But when I use Cylinder in place of cyname, I am not getting any error.
>
> Please help.
>
> Thanks.

This might work:
cyname = "Cylinder"
FreeCAD.ActiveDocument.addObject("Part::Cylinder",cyname)
getattr(FreeCAD.ActiveDocument,cyname).Radius= .5
getattr(FreeCAD.ActiveDocument,cyname).Height= 10

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


Re: generator expression works in shell, NameError in script

2009-06-18 Thread nn
On Jun 18, 8:38 am, guthrie  wrote:
> On Jun 17, 6:38 pm, Steven Samuel Cole 
> wrote:
>
> > Still don't really understand why my initial code didn't work, though...
>
> Your code certainly looks reasonable, and looks to me like it "should"
> work. The comment of partial namespace is interesting, but
> unconvincing (to me) - but I am not a Python expert! It would
> certainly seem that within that code block it is in the local
> namespace, not removed from that scope to be in another.
>
> Seems that it should either be a bug, or else is a design error in the
> language!
>
> Just as in the above noted "WTF" - non-intuitive language constructs
> that surprise users are poor design.

This is certainly an odd one. This code works fine under 2.6 but fails
in Python 3.1.

>>> class x:
... lst=[2]
... gen=[lst.index(e) for e in lst]
...
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in x
  File "", line 3, in 
NameError: global name 'lst' is not defined
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this pylint error message valid or silly?

2009-06-19 Thread nn
On Jun 18, 8:56 pm, Matthew Wilson  wrote:
> Here's the code that I'm feeding to pylint:
>
>     $ cat f.py
>     from datetime import datetime
>
>     def f(c="today"):
>
>         if c == "today":
>                     c = datetime.today()
>
>         return c.date()
>
> And here's what pylint says:
>
>     $ pylint -e f.py
>     No config file found, using default configuration
>     * Module f
>     E: 10:f: Instance of 'str' has no 'date' member (but some types could
>     not be inferred)
>
> Is this a valid error message?  Is the code above bad?  If so, what is
> the right way?
>
> I changed from using a string as the default to None, and then pylint
> didn't mind:
>
>     $ cat f.py
>     from datetime import datetime
>
>     def f(c=None):
>
>         if c is None:
>                     c = datetime.today()
>
>         return c.date()
>
>     $ pylint -e f.py
>     No config file found, using default configuration
>
> I don't see any difference between using a string vs None.  Both are
> immutable.  I find the string much more informative, since I can write
> out what I want.
>
> Looking for comments.
>
> Matt

>>> def midnight_next_day(initial_time=None):

 if initial_time == "use today's  date":
initial_time = datetime.now()

 return initial_time.date() + timedelta(days=1)

>>> midnight_next_day()

Traceback (most recent call last):
  File "", line 1, in 
midnight_next_day()
  File "", line 6, in midnight_next_day
return initial_time.date() + timedelta(days=1)
AttributeError: 'NoneType' object has no attribute 'date'
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this pylint error message valid or silly?

2009-06-19 Thread nn
On Jun 18, 8:56 pm, Matthew Wilson  wrote:
> Here's the code that I'm feeding to pylint:
>
>     $ cat f.py
>     from datetime import datetime
>
>     def f(c="today"):
>
>         if c == "today":
>                     c = datetime.today()
>
>         return c.date()
>
> And here's what pylint says:
>
>     $ pylint -e f.py
>     No config file found, using default configuration
>     * Module f
>     E: 10:f: Instance of 'str' has no 'date' member (but some types could
>     not be inferred)
>
> Is this a valid error message?  Is the code above bad?  If so, what is
> the right way?
>
> I changed from using a string as the default to None, and then pylint
> didn't mind:
>
>     $ cat f.py
>     from datetime import datetime
>
>     def f(c=None):
>
>         if c is None:
>                     c = datetime.today()
>
>         return c.date()
>
>     $ pylint -e f.py
>     No config file found, using default configuration
>
> I don't see any difference between using a string vs None.  Both are
> immutable.  I find the string much more informative, since I can write
> out what I want.
>
> Looking for comments.
>
> Matt

>>> def midnight_next_day(initial_time="use today's date"):

 if initial_time == "use today's  date":
initial_time = datetime.now()

 return initial_time.date() + timedelta(days=1)

>>> midnight_next_day()

Traceback (most recent call last):
  File "", line 1, in 
midnight_next_day()
  File "", line 6, in midnight_next_day
return initial_time.date() + timedelta(days=1)
AttributeError: 'str' object has no attribute 'date'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this pylint error message valid or silly?

2009-06-19 Thread nn
On Jun 18, 8:56 pm, Matthew Wilson  wrote:
> Here's the code that I'm feeding to pylint:
>
>     $ cat f.py
>     from datetime import datetime
>
>     def f(c="today"):
>
>         if c == "today":
>                     c = datetime.today()
>
>         return c.date()
>
> And here's what pylint says:
>
>     $ pylint -e f.py
>     No config file found, using default configuration
>     * Module f
>     E: 10:f: Instance of 'str' has no 'date' member (but some types could
>     not be inferred)
>
> Is this a valid error message?  Is the code above bad?  If so, what is
> the right way?
>
> I changed from using a string as the default to None, and then pylint
> didn't mind:
>
>     $ cat f.py
>     from datetime import datetime
>
>     def f(c=None):
>
>         if c is None:
>                     c = datetime.today()
>
>         return c.date()
>
>     $ pylint -e f.py
>     No config file found, using default configuration
>
> I don't see any difference between using a string vs None.  Both are
> immutable.  I find the string much more informative, since I can write
> out what I want.
>
> Looking for comments.
>
> Matt

This is a limitation of static type checking and Pylint is a (limited)
static type checker for Python. A language with static type checking
would not have even allowed you to compile this. A static type
checkers sees this:

def midnight_next_day(initial_time [string or in python whatever gets
passed(T)]):

 if initial_time [string (or T)]== [(some constant) string]:
initial_time [datetime] = datetime.now()
 {implied else:
 initial_time [string or (T)] }

 return initial_time[could be either datetime or string (oops
string does not have date()) pylint doesn’t check for T].date() +
timedelta(days=1)

or this:

def midnight_next_day(initial_time [None object or (T)]):

 if initial_time [None object or (T)]== [None object]:
initial_time [type datetime] = datetime.now()
 {implied else:
 initial_time [T] }

 return initial_time[datetime (pylint doesn’t check for T)].date()
+ timedelta(days=1)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string character count

2009-06-30 Thread nn
On Jun 30, 1:56 pm, MRAB  wrote:
> noydb wrote:
> > If I have a string for a file name such that I want to find the number
> > of characters to the left of the dot, how can that be done?
>
> > I did it this way:
> > x = "text12345.txt"
> > dot = x.find('.')
> > print dot
>
> > Was curious to see what method others would use - helps me learn.  I
> > guess I was most curious to see if it could be done in one line.
>
>  >>> print "text12345.txt".find('.')
> 9
>
> > And, how would a char count be done with no dot -- like if the string
> > were "textstringwithoutdot" or "no dot in text string"?
>
> If there's no dot then find() returns -1.
>
> If you wanted to know the number of characters before the dot, if
> present, or in total otherwise, then you could use split():
>
>  >>> len("text12345.txt".split(".", 1)[0])
> 9
>  >>> len("textstringwithoutdot".split(".", 1)[0])
> 20

Also:

len("text12345.txt".partition('.')[0])
9
-- 
http://mail.python.org/mailman/listinfo/python-list


library search path when compiling python

2009-07-07 Thread nn
I am trying to compile python with ssl support but the libraries are
not in /usr/lib but in /opt/freeware/lib. How do I add that folder to
the default library search path?

It looks like configure --libdir=DIR might do the job but I don't want
to replace the default lib search path, just add an additional folder
to it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: library search path when compiling python

2009-07-08 Thread nn
On Jul 7, 4:06 pm, Christian Heimes  wrote:
> nn wrote:
> > I am trying to compile python with ssl support but the libraries are
> > not in /usr/lib but in /opt/freeware/lib. How do I add that folder to
> > the default library search path?
>
> > It looks like configure --libdir=DIR might do the job but I don't want
> > to replace the default lib search path, just add an additional folder
> > to it.
>
> You didn't mention your OS. On Linux you can set the environment
> variable LD_RUN_PATH prior to compiling Python. The env var sets the
> rpath for the linker. See man ld(1) for details.
>
> Christian

Sorry I didn't realize that was OS specific. I am on AIX 5.3.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tough-to-explain Python

2009-07-08 Thread nn
On Jul 7, 5:18 pm, kj  wrote:
> In  Chris Rebert 
>  writes:
>
> >You might find the following helpful (partially):
> >http://effbot.org/zone/call-by-object.htm
>
> Extremely helpful.  Thanks!  (I learned more from it than someone
> who will teach the stuff would care to admit...)
>
> And it confirmed Paul Graham's often-made assertion that all of
> programming language design is still catching up to Lisp...
>
> kj

One possibility is to avoid teaching mutable datatypes at the
beginning (i.e. no lists,dicts and sets), then you would have
something more lispish. You would have to use tuples instead of lists
for everything. That avoids the gotchas of mutable types at the cost
of efficiency.

Python is not a purist language but rather walks a fine line between
high flexible abstractions and simple fast implementation.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: strange python scripting error

2009-07-24 Thread nn
On Jul 23, 7:03 pm, Dave Angel  wrote:
> Mark Tarver wrote:
> > I have a very strange error.  I have two test python files test.py and
> > python.py which contain the following code
>
> > #!/usr/bin/python
> > print "Content-type: text/html"
> > print
> > print ""
> > print "Hello, Linux.com!"
> > print ""
>
> > One file (test.py) works; you call it up and it shows a web page with
>
> > Hello, Linux.com
>
> > The other fails with a server configuration error.  Both are running
> > under Linux, same server, same permissions.  Running a character scan
> > shows that both files contain the same printable characters and are
> > therefore typographically identical.   They are absolutely the same.
>
> > The only hint at a difference I can see is that my ftp program says
> > the files are of unequal lengths.  test.py is 129 bytes long.
> > python.py 134 bytes long.
>
> > A zipped folder containing both files is at
>
> >www.lambdassociates.org/weird.zip
>
> > Any ideas welcome.
>
> > Mark
>
> Easiest explanation is that python.py has Windows-style newlines.  In
> other words, each line ends with 0d0a, rather than the Unix convention
> of 0a.
>
> If your server is Unix-based, it can't handle that first line, since it
> has an illegal character (0d) following the
>
> #!/usr/bin/python
>
> line.  Convert it to Unix line-endings.
>
> DaveA

Use dos2unix for conversion of the longer file and try again:

http://linux.about.com/od/commands/l/blcmdl1_dos2uni.htm
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overlap in python

2009-08-05 Thread nn
On Aug 5, 7:13 am, Marcus Wanner  wrote:
> On 8/4/2009 6:09 PM, MRAB wrote:
>
> >  >>> parts = [(5, 9, "a"), (7, 10, "b"), (3, 6, "c"), (15, 20, "d"),
> > (18, 23, "e")]
> >  >>> parts.sort()
> >  >>> parts
> > [(3, 6, 'c'), (5, 9, 'a'), (7, 10, 'b'), (15, 20, 'd'), (18, 23, 'e')]
> >  >>> # Merge overlapping intervals.
> >  >>> pos = 1
> >  >>> while pos < len(parts):
> >         # Merge the pair in parts[pos - 1 : pos + 1] if they overlap.
> >         p, q = parts[pos - 1 : pos + 1]
> >         if p[1] >= q[0]:
> >                 parts[pos - 1 : pos + 1] = [(p[0], max(p[1], q[1]), p[2]
> > + "." + q[2])]
> >         else:
> >                 # They don't overlap, so try the next pair.
> >                 pos += 1
>
> >  >>> parts
> > [(3, 10, 'c.a.b'), (15, 23, 'd.e')]
>
> That's the best solution I've seen so far. It even has input/output
> formatted as close as is reasonably possible to the format specified.
>
> As we would say in googlecode, +1.
>
> Marcus

How does it compare to this one?

http://groups.google.com/group/comp.lang.python/browse_frm/thread/1a1d2ed9d05d11d0/56684b795fc527cc#56684b795fc527cc
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: syntax checker in python

2009-08-10 Thread nn
On Aug 7, 4:39 pm, horos11  wrote:
> ps - I just realized that it isn't enough to do:
>
> python -c 'import /path/to/script'
>
> since that actually executes any statement inside of the script
> (wheras all I want to do is check syntax)
>
> So - let me reprhase that - exactly how can you do a syntax check in
> python? Something like perl's -c:
>
>      perl -c script_name.p
>
> Ed

You might want to check PyFlakes; it doesn't execute scripts unlike
the others.

http://www.divmod.org/trac/wiki/DivmodPyflakes
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Extracting patterns after matching a regex

2009-09-08 Thread nn
On Sep 8, 9:55 am, "Mart."  wrote:
> On Sep 8, 2:16 pm, "Andreas Tawn"  wrote:
>
>
>
> > > Hi,
>
> > > I need to extract a string after a matching a regular expression. For
> > > example I have the string...
>
> > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov"
>
> > > and once I match "FTPHOST" I would like to extract
> > > "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to the
> > > problem, I had been trying to match the string using something like
> > > this:
>
> > > m = re.findall(r"FTPHOST", s)
>
> > > But I couldn't then work out how to return the "e4ftl01u.ecs.nasa.gov"
> > > part. Perhaps I need to find the string and then split it? I had some
> > > help with a similar problem, but now I don't seem to be able to
> > > transfer that to this problem!
>
> > > Thanks in advance for the help,
>
> > > Martin
>
> > No need for regex.
>
> > s = "FTPHOST: e4ftl01u.ecs.nasa.gov"
> > If "FTPHOST" in s:
> >     return s[9:]
>
> > Cheers,
>
> > Drea
>
> Sorry perhaps I didn't make it clear enough, so apologies. I only
> presented the example  s = "FTPHOST: e4ftl01u.ecs.nasa.gov" as I
> thought this easily encompassed the problem. The solution presented
> works fine for this i.e. re.search(r'FTPHOST: (.*)',s).group(1). But
> when I used this on the actual file I am trying to parse I realised it
> is slightly more complicated as this also pulls out other information,
> for example it prints
>
> e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n',
> 'Ftp Pull Download Links: \r\n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/
> 0301872638CySfQB\r\n', 'Down load ZIP file of packaged order:\r\n',
>
> etc. So I need to find a way to stop it before the \r
>
> slicing the string wouldn't work in this scenario as I can envisage a
> situation where the string lenght increases and I would prefer not to
> keep having to change the string.
>
> Many thanks

It is not clear from your post what the input is really like. But just
guessing this might work:

>>> print s
'MEDIATYPE: FtpPull\r\n', 'MEDIAFORMAT: FILEFORMAT\r\n','FTPHOST:
e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r
\n','Ftp Pull Download Links: \r\n'

>>> re.search(r'FTPHOST: (.*?)\\r',s).group(1)
'e4ftl01u.ecs.nasa.gov'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Extracting patterns after matching a regex

2009-09-08 Thread nn
On Sep 8, 10:27 am, pdpi  wrote:
> On Sep 8, 3:21 pm, nn  wrote:
>
>
>
> > On Sep 8, 9:55 am, "Mart."  wrote:
>
> > > On Sep 8, 2:16 pm, "Andreas Tawn"  wrote:
>
> > > > > Hi,
>
> > > > > I need to extract a string after a matching a regular expression. For
> > > > > example I have the string...
>
> > > > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov"
>
> > > > > and once I match "FTPHOST" I would like to extract
> > > > > "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to the
> > > > > problem, I had been trying to match the string using something like
> > > > > this:
>
> > > > > m = re.findall(r"FTPHOST", s)
>
> > > > > But I couldn't then work out how to return the "e4ftl01u.ecs.nasa.gov"
> > > > > part. Perhaps I need to find the string and then split it? I had some
> > > > > help with a similar problem, but now I don't seem to be able to
> > > > > transfer that to this problem!
>
> > > > > Thanks in advance for the help,
>
> > > > > Martin
>
> > > > No need for regex.
>
> > > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov"
> > > > If "FTPHOST" in s:
> > > >     return s[9:]
>
> > > > Cheers,
>
> > > > Drea
>
> > > Sorry perhaps I didn't make it clear enough, so apologies. I only
> > > presented the example  s = "FTPHOST: e4ftl01u.ecs.nasa.gov" as I
> > > thought this easily encompassed the problem. The solution presented
> > > works fine for this i.e. re.search(r'FTPHOST: (.*)',s).group(1). But
> > > when I used this on the actual file I am trying to parse I realised it
> > > is slightly more complicated as this also pulls out other information,
> > > for example it prints
>
> > > e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n',
> > > 'Ftp Pull Download Links: \r\n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/
> > > 0301872638CySfQB\r\n', 'Down load ZIP file of packaged order:\r\n',
>
> > > etc. So I need to find a way to stop it before the \r
>
> > > slicing the string wouldn't work in this scenario as I can envisage a
> > > situation where the string lenght increases and I would prefer not to
> > > keep having to change the string.
>
> > > Many thanks
>
> > It is not clear from your post what the input is really like. But just
> > guessing this might work:
>
> > >>> print s
>
> > 'MEDIATYPE: FtpPull\r\n', 'MEDIAFORMAT: FILEFORMAT\r\n','FTPHOST:
> > e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r
> > \n','Ftp Pull Download Links: \r\n'
>
> > >>> re.search(r'FTPHOST: (.*?)\\r',s).group(1)
>
> > 'e4ftl01u.ecs.nasa.gov'
>
> Except, I'm assuming, the OP's getting the data from a (windows-
> formatted) file, so \r\n shouldn't be escaped in the regex:
>
> >>> re.search(r'FTPHOST: (.*?)\r',s).group(1)
>
>

I am just playing the guessing game like everybody else here. Since
the OP didn't use re.DOTALL and was getting more than one line for .*
I assumed that the \n was quite literally '\' and 'n'.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Extracting patterns after matching a regex

2009-09-08 Thread nn
On Sep 8, 10:25 am, "Mart."  wrote:
> On Sep 8, 3:21 pm, nn  wrote:
>
>
>
> > On Sep 8, 9:55 am, "Mart."  wrote:
>
> > > On Sep 8, 2:16 pm, "Andreas Tawn"  wrote:
>
> > > > > Hi,
>
> > > > > I need to extract a string after a matching a regular expression. For
> > > > > example I have the string...
>
> > > > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov"
>
> > > > > and once I match "FTPHOST" I would like to extract
> > > > > "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to the
> > > > > problem, I had been trying to match the string using something like
> > > > > this:
>
> > > > > m = re.findall(r"FTPHOST", s)
>
> > > > > But I couldn't then work out how to return the "e4ftl01u.ecs.nasa.gov"
> > > > > part. Perhaps I need to find the string and then split it? I had some
> > > > > help with a similar problem, but now I don't seem to be able to
> > > > > transfer that to this problem!
>
> > > > > Thanks in advance for the help,
>
> > > > > Martin
>
> > > > No need for regex.
>
> > > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov"
> > > > If "FTPHOST" in s:
> > > >     return s[9:]
>
> > > > Cheers,
>
> > > > Drea
>
> > > Sorry perhaps I didn't make it clear enough, so apologies. I only
> > > presented the example  s = "FTPHOST: e4ftl01u.ecs.nasa.gov" as I
> > > thought this easily encompassed the problem. The solution presented
> > > works fine for this i.e. re.search(r'FTPHOST: (.*)',s).group(1). But
> > > when I used this on the actual file I am trying to parse I realised it
> > > is slightly more complicated as this also pulls out other information,
> > > for example it prints
>
> > > e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n',
> > > 'Ftp Pull Download Links: \r\n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/
> > > 0301872638CySfQB\r\n', 'Down load ZIP file of packaged order:\r\n',
>
> > > etc. So I need to find a way to stop it before the \r
>
> > > slicing the string wouldn't work in this scenario as I can envisage a
> > > situation where the string lenght increases and I would prefer not to
> > > keep having to change the string.
>
> > > Many thanks
>
> > It is not clear from your post what the input is really like. But just
> > guessing this might work:
>
> > >>> print s
>
> > 'MEDIATYPE: FtpPull\r\n', 'MEDIAFORMAT: FILEFORMAT\r\n','FTPHOST:
> > e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r
> > \n','Ftp Pull Download Links: \r\n'
>
> > >>> re.search(r'FTPHOST: (.*?)\\r',s).group(1)
>
> > 'e4ftl01u.ecs.nasa.gov'
>
> Hi,
>
> That does work. So the \ escapes the \r, does this tell it to stop
> when it reaches the "\r"?
>
> Thanks

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


Re: Extracting patterns after matching a regex

2009-09-08 Thread nn
On Sep 8, 11:19 am, Dave Angel  wrote:
> Mart. wrote:
> > 
> > I have been doing this to turn the email into a string
>
> > email =ys.argv[1]
> > f =open(email, 'r')
> > s =str(f.readlines())
>
> > so FTPHOST isn't the first element, it is just part of a larger
> > string. When I turn the email into a string it looks like...
>
> > 'FINISHED: 09/07/2009 08:42:31\r\n', '\r\n', 'MEDIATYPE: FtpPull\r\n',
> > 'MEDIAFORMAT: FILEFORMAT\r\n', 'FTPHOST: e4ftl01u.ecs.nasa.gov\r\n',
> > 'FTPDIR: /PullDir/0301872638CySfQB\r\n', 'Ftp Pull Download Links: \r
> > \n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB\r\n', 'Down
> > load ZIP file of packaged order:\r\n',
> > 
>
> The mistake I see is trying to turn a list into a string, just so you
> can try to parse it back again.  Just write a loop that iterates through
> the list that readlines() returns.
>
> DaveA

No kidding.

Instead of this:
s = str(f.readlines())

ftphost = re.search(r'FTPHOST: (.*?)\\r',s).group(1)
ftpdir  = re.search(r'FTPDIR: (.*?)\\r',s).group(1)
url = 'ftp://' + ftphost + ftpdir


I would have possibly done something like this (not tested):
lines = f.readlines()
header={}
for row in lines:
key,sep,value = row.partition(':')[2].rstrip()
header[key.lower()]=value
url = 'ftp://' + header['ftphost'] + header['ftpdir']
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Extracting patterns after matching a regex

2009-09-08 Thread nn
On Sep 8, 12:16 pm, nn  wrote:
> On Sep 8, 11:19 am, Dave Angel  wrote:
>
>
>
> > Mart. wrote:
> > > 
> > > I have been doing this to turn the email into a string
>
> > > email =ys.argv[1]
> > > f =open(email, 'r')
> > > s =str(f.readlines())
>
> > > so FTPHOST isn't the first element, it is just part of a larger
> > > string. When I turn the email into a string it looks like...
>
> > > 'FINISHED: 09/07/2009 08:42:31\r\n', '\r\n', 'MEDIATYPE: FtpPull\r\n',
> > > 'MEDIAFORMAT: FILEFORMAT\r\n', 'FTPHOST: e4ftl01u.ecs.nasa.gov\r\n',
> > > 'FTPDIR: /PullDir/0301872638CySfQB\r\n', 'Ftp Pull Download Links: \r
> > > \n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB\r\n', 'Down
> > > load ZIP file of packaged order:\r\n',
> > > 
>
> > The mistake I see is trying to turn a list into a string, just so you
> > can try to parse it back again.  Just write a loop that iterates through
> > the list that readlines() returns.
>
> > DaveA
>
> No kidding.
>
> Instead of this:
> s = str(f.readlines())
>
> ftphost = re.search(r'FTPHOST: (.*?)\\r',s).group(1)
> ftpdir  = re.search(r'FTPDIR: (.*?)\\r',s).group(1)
> url = 'ftp://' + ftphost + ftpdir
>
> I would have possibly done something like this (not tested):
> lines = f.readlines()
> header={}
> for row in lines:
>     key,sep,value = row.partition(':')[2].rstrip()
>     header[key.lower()]=value
> url = 'ftp://' + header['ftphost'] + header['ftpdir']

Well I said not tested that would be of course:
lines = f.readlines()
header={}
for row in lines:
key,sep,value = row.partition(':')
header[key.lower()]=value.rstrip()
url = 'ftp://' + header['ftphost'] + header['ftpdir']

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


Re: custom data warehouse in python vs. out-of-the-box ETL tool

2009-09-23 Thread nn
On Sep 22, 4:00 pm, snfctech  wrote:
> Does anyone have experience building a data warehouse in python?  Any
> thoughts on custom vs using an out-of-the-box product like Talend or
> Informatica?
>
> I have an integrated system Dashboard project that I was going to
> build using cross-vendor joins on existing DBs, but I keep hearing
> that a data warehouse is the way to go.  e.g. I want to create orders
> and order_items with relations to members (MS Access DB), products
> (flat file) and employees (MySQL).
>
> Thanks in advance for any tips.

I use both Python and a Data-warehouse tool (Datastage) from IBM that
is similar to Informatica. The main difference with Python is
throughput. The tool has good sort and join routines of multithreaded
C code that handles data bigger than what fits in RAM. It also has
good native drivers for the DB2 database. For data conversions and
other transformations every row gets processed on a different CPU. You
can really put a 16 core machine to good use with this thing.

In your case you probably won't have enough data to justify the cost
of buying a tool. They are quite expensive.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Poll: Do you use csv.Sniffer?

2009-09-25 Thread nn
On Sep 24, 10:26 pm, s...@pobox.com wrote:
> If you are a csv module user, I have a question for you:  Do you use the
> csv.Sniffer class?
>
>     o Yes, frequently
>     o Yes, on occasion
>     o I tried it a few times but don't use it now
>     o No, I don't need it
>     o No, never heard of it
>     o No (some other reason)
>
> If you don't use it, how do you figure out the structure of your csv files?
>
>     o I just know
>     o I stumble around in the dark trying different parameters until the csv
>       reader starts to spit out useful data
>
> If csv.Sniff was to be removed from a future version of Python, how
> difficult would it be to adapt?
>
>     o No problem
>     o No problem as long as you make it available via PyPI
>     o It would be a problem
>
> If it would be a problem for you (which would not easily be solved by adding
> it to PyPI), feel free to describe why it would be a challenge for you.  In
> fact, please feel free to add any other comments you like to your response.
>
> Private replies please.
>
> Thanks,
>
> --
> Skip Montanaro - s...@pobox.com -http://www.smontanaro.net/
>     Getting old sucks, but it beats dying young

Do you use the csv.Sniffer class? No. Don't use it right now.

how do you figure out the structure of your csv files? "head -2
filename" + visual inspection + guesswork

If csv.Sniff was to be removed from a future version of Python, how
difficult would it be to adapt? No problem.

It would still be nice if the code was available somewhere on the net.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Minimum and Maximum of a list containing floating point numbers

2010-09-07 Thread nn
On Sep 6, 10:31 pm, Steven D'Aprano  wrote:
> On Tue, 07 Sep 2010 11:00:45 +1000, Ben Finney wrote:
> > If you're going to use the list of float objects, you can convert them
> > all with a list comprehension.
> [...]
> >     >>> numbers_as_float = [float(x) for x in numbers_as_str]
>
> That's awfully verbose. A map is simpler:
>
> numbers_as_float = map(float, numbers_as_str)
>
> --
> Steven

In Python 3.x it has one disadvantage:

>>> numbers_as_float = map(float, numbers_as_str)
>>> max(numbers_as_float)
10.24
>>> min(numbers_as_float)
Traceback (most recent call last):
  File "", line 1, in 
min(numbers_as_float)
ValueError: min() arg is an empty sequence
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ugh! Python 3.1.x and MySQL

2010-09-10 Thread nn
On Sep 10, 12:27 pm, fuglyducky  wrote:
> Most of the python books coming out now are Py3K. I just started
> programming and have a need to access a MySQL database. I would like
> to use Python to do this. Unfortunately, I cannot find anyone that has
> created anything that allows my to do this.
>
> I've tried installing an ODBC driver and using sqlalchemy, oursql, and
> a few other things with no luck.
>
> So...just wondering if anyone is aware of any libraries/modules that I
> can use to connect to a MySQL DB using Python 3.1.x?
>
> Ideally, I'd like to be able to this from both x86 and x64 systems (if
> that makes any difference).
>
> Thanks for any input you may have!!!

Google found this:

http://sourceforge.net/projects/mysql-python/forums/forum/70460/topic/3831691
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: business date and calendar libraries?

2010-09-15 Thread nn
On Sep 13, 3:02 pm, David Robinow  wrote:
> On Mon, Sep 13, 2010 at 1:06 PM, Chris Withers  wrote:
> > I'm wondering what libraries people would use to answer the following
> > questions relating to business days:
>
> > - on a less-naive level; same question but taking into account public
> > holidays
>
>  This depends on which government is oppressing you.
>
> > - on a horrific level; same question, but taking into account business days
> > of a particular market (NYSE, LSE, etc)
>
>  This is just an instance of the public holiday case. You need to
> define the holidays.
>  If you read lisp you might want to look at the emacs calendar module
> for some hints on how they describe holidays, such as, for a US-biased
> example,  Martin Luther King day is the third Monday in January,
> Memorial Day is the last Monday in May, Good Friday is not a public
> holiday but some markets are closed.

A good way to do this IMHO is to keep a list of "holidays" for each
year in your program and check if a day is in the list to do your
calculations. The list could be automatically generated by an
algorithm that figures out all the weird holiday dates or -as is done
in many companies- some honcho adds them according to his preference
to a database.
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >