Re: External Hashing [was Re: matching strings in a large set of strings]

2010-05-01 Thread Jack
http://www.swizwatch.com/
All Cartier replica watches sold at Hotwristwatch.com are brand-new and high
quality. Each Cartier Replica Watch produced is examined carefully by our
quality test department and each watch is inspected again before being sent
to our customer. It is our desire that you do experience the joy of shopping
when buying one of our Cartier Replica Watches at our site. Some Cartier
Watches may need to be special ordered, please call for availability on
Cartier watches. Best service you will receive from us.

"Helmut Jarausch"  
??:840jkofai...@mid.dfncis.de...
>I think one could apply an external hashing technique which would require 
>only
> very few disk accesses per lookup.
> Unfortunately, I'm now aware of an implementation in Python.
> Does anybody know about a Python implementation of external hashing?
>
> Thanks,
> Helmut.
>
> -- 
> Helmut Jarausch
>
> Lehrstuhl fuer Numerische Mathematik
> RWTH - Aachen University
> D 52056 Aachen, Germany 


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


Online survey jobs & data entry jobs

2010-05-01 Thread online jobs
http://teluguscope.com/job.html

http://teluguscope.com/links/onlinedataentryjobs.html

http://teluguscope.com/links/Copy%20Past%20jobs.html

http://teluguscope.com/links/Work%20from%20Home.html

http://teluguscope.com/links/Formfilling%20jobs.html

http://teluguscope.com/links/Survey%20Jobs.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ignoring XML Namespaces with cElementTree

2010-05-01 Thread Carl Banks
On Apr 27, 6:42 pm, dmtr  wrote:
> Is there any way to configure cElementTree to ignore the XML root
> namespace?  Default cElementTree (Python 2.6.4) appears to add the XML
> root namespace URI to _every_ single tag.  I know that I can strip
> URIs manually, from every tag, but it is a rather idiotic thing to do
> (performance wise).

Perhaps upgrade to lxml.  Not sure if gives you control over namespace
expansion but if it doesn't it should at least be faster.

For this and some other reasons, I find ElementTree not quite as handy
when processing files from another source as when I'm saving and
retrieving my own data.


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


Re: Ignoring XML Namespaces with cElementTree

2010-05-01 Thread Carl Banks
On Apr 29, 10:12 pm, Stefan Behnel  wrote:
> dmtr, 30.04.2010 04:57:
>
>
>
> > I'm referring to xmlns/URI prefixes. Here's a code example:
> >   from xml.etree.cElementTree import iterparse
> >   from cStringIO import StringIO
> >   xml = """http://www.very_long_url.com";> > root>"""
> >   for event, elem in iterparse(StringIO(xml)): print event, elem
>
> > The output is:
> >   endhttp://www.very_long_url.com}child' at 0xb7ddfa58>
> >   endhttp://www.very_long_url.com}root' at 0xb7ddfa40>
>
> > I don't want these "{http://www.very_long_url.com}"; in front of my
> > tags.
>
> > They create performance disaster on large files
>
> I seriously doubt that they do.

I don't know what kind of XML files you deal with, but for me a large
XML file is gigabyte-sized (obviously I don't use Element Tree for
those).

Even for files tens-of-megabyte files string ops to expand tags with
namespaces is going to be a pretty decent penalty--remember
ElementTree does nothing lazily.


> > (first cElementTree
> > adds them, then I have to remove them in python).
>
> I think that's your main mistake: don't remove them. Instead, use the fully
> qualified names when comparing.

Unless you have multiple namespaces or are working with defined schema
or something, it's useless boilerplate.

It'd be a nice feature if ElementTree could let users optionally
ignore a namespace, unfortunately it doesn't have it.


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


Re: CGI python 3 write RAW BINARY

2010-05-01 Thread Dodo

Le 30/04/2010 17:52, Antoine Pitrou a écrit :

Le Thu, 29 Apr 2010 23:37:32 +0200, Dodo a écrit :

I don't get a thing.
Now with the fix :
All browsers shows a different thing, but not the image!
http://ddclermont.homeip.net/misc/python/

If I save it to computer :
* Windows image viewer won't read it
* Irfanview can read it without problems


Did you set the content-type and content-length in the HTTP headers?
Can you post your code?



I didn't know about content-lenght
Here's the new code (I used a fixed image patch to make sure this is not 
the source of the problem)



#!/usr/bin/python3
import cgi, sys, cgitb
cgitb.enable()

f = open("/home/dodo/54.jpg", "rb")
data = f.read()
l = len(data)
f.close()

print("Content-type:image/jpg\nContent-length:%d\n\n" % l)

sys.stdout.flush()
sys.stdout.buffer.write( data )



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


Re: Python daemonisation with python-daemon

2010-05-01 Thread Ben Finney
Thomas Courbon  writes:

> I would like to turn my server script into a Linux/Unix daemon
> (launched at boot time by init, dunno if that matter) using the nice
> python-daemon package by Ben Finley et al

I resemble that name :-)

> This package comes with a class DaemonRunner that seems to fit almost
> exactly my need but I still have some interrogation.

Yes, that class is unlikely to grow much beyond a simple example of how
to use ‘DaemonContext’. You should feel free to take ‘DaemonRunner’
under the granted license and adapt it to be closer to what you want.

> The DaemonRunner class expects my class to have stdin_path,
> stdout_path, stderr_path attributes and after reading the code it
> seems they have to be valid paths. Is that ok for a Daemon to redirect
> those stream to /dev/null for example ? I would prefer to alter the
> DaemonRunner class to accept None as value since DaemonContext, the
> underlying class, seems to accept None for those parameters.

This is a godd idea, allowing a more minimal use of ‘DaemonRunner’. I'll
take this as a feature request for a future revision of the class.

> Also, the DaemonRunner use os.kill(pid, signal.SIGTERM) to stop the
> daemon. I wonder if with my signal handling I'll be able to terminate
> correctly the daemon (joining children, flushing buffers, closing
> connections...). If that's relevant, the connection I use is a
> Listener/Client connection from the standard multiprocessing module.

I'll be interested to know too :-)

> I'm quite neophyte in Unix daemon programming so please forgive me if
> my question are obvious.

Thank you for your feedback, and I hope the ‘python-daemon’ library
continues to meet your needs.

-- 
 \  “In the long run, the utility of all non-Free software |
  `\  approaches zero. All non-Free software is a dead end.” —Mark |
_o__)Pilgrim, 2006 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


PyObject_SetAttrString - doesn't set instance attribute

2010-05-01 Thread Jason
I'm having a bit of trouble with C/Python bindings. Particularly,
trying to set an instance variable from C when the object is
initialised using PyObject_SetAttrString, but nothing seems to happen.
The C initialisation code is:

static void
nautilus_python_object_instance_init (NautilusPythonObject *object)
{

fprintf(stderr, "nautilus_python_object_instance_init called\n");

NautilusPythonObjectClass *class;
debug_enter();

class = (NautilusPythonObjectClass*)(((GTypeInstance*)object)-
>g_class);

object->instance = PyObject_CallObject(class->type, NULL);

PyObject* test_int = PyInt_FromLong(42);

if (object->instance == NULL)
{
PyErr_Print();
}
else
{
fprintf(stderr, "Setting magic parameter\n");
fprintf(stderr, "From C: ");
PyObject_Print(object->instance, stderr, 0);
fprintf(stderr, "\n");
int retval = PyObject_SetAttrString(object->instance,
"super_happy_magic", test_int);
fprintf(stderr, "Result: %i\n", retval);
}

Py_DECREF(test_int);
}

...and the Python module contains:


class MenuProviderTest(nautilus.MenuProvider):

def __init__(self):
print "From Python: %s" % self

try:
print getattr(self, "super_happy_magic")
except AttributeError:
print "Didn't work!"

When the MenuProviderTest is created, the output is:

nautilus_python_object_instance_init called
Setting magic parameter
>From C: 
Result: 0
>From Python: 
Didn't work!

(I've tried getattr and self.super_happy_magic, with the same effect.)

Where am I going wrong?

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


Re: Performance of list.index - how to speed up a silly algorithm?

2010-05-01 Thread Laszlo Nagy


Maybe this should be implemented in C. But I believe that the 
algorithm itself must be wrong (regardless of the language). I really 
think that I'm doing something wrong. Looks like my algorithm's 
processing time is not linear to the number of rows. Not even 
log(n)*n. There should be a more effective way to do this. But how?


I had the idea of sorting the rows by a given dimension. Then it 
would be obvious to speed up the indexing part - for that dimension. 
PROBABLY sorting all rows would be faster than calling list.index() 
for each row. But... it does not seem very efficient either. What if 
I have 1 million rows and 10 dimensions? Do I sort 1 million rows on 
the disk 10 times? Some of you might have ran into the same problem 
before, and can tell me which is the most efficient way to do this.


The .index method does a linear search, checking on average 1/2 of the 
items in the list. That's why it's so slow.


In order to avoid that you could build a dict of each value in
dimension_values[col_idx] and its index in a single pass so that it
becomes a quick lookup.
Changed to dicts and hashed lookups. Now the processing time is O(n), 
and went up to 8000 rows/sec.


Probably I'll never want to process more than 1M rows. That will take 
about 125 seconds. Fair enough.


Thank you very much!

  Laszlo

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


Re: matching strings in a large set of strings

2010-05-01 Thread News123
Dennis Lee Bieber wrote:
> On Thu, 29 Apr 2010 11:38:28 +0200, "Karin Lagesen"
>  declaimed the following in comp.lang.python:
> 
>> Hello.
>>
>> I have approx 83 million strings, all 14 characters long. I need to be
>> able to take another string and find out whether this one is present
>> within the 83 million strings.
>>

>>
>   So don't load them into memory... First use a file-based (not memory
>
> 
>   That lets you do a binary search on the file. Much faster than a
> linear search (linear search will average out to 41.5M read operations;
> binary should be around 1 reads)

Don't you meant 27 reads instead of 41.5 M reads?

>>> from math import log
>>> log(83e6)/log(2)
26.306608000671101
>>>


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


Re: Use Regular Expressions to extract URL's

2010-05-01 Thread Walter Overby
A John Gruber post from November seems relevant.  I have not tried his
regex in any language.

http://daringfireball.net/2009/11/liberal_regex_for_matching_urls

Regards,

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


Re: http://pypi.python.org/pypi

2010-05-01 Thread Martin v. Loewis
> I have only access to the webpage form too upload my one file.
> pkginfo is ok, just want to at a single .py file instead of a complete
> site-package tar directory, because it is not a site-package, its more
> like a single exe file.

Uploading individual .py files is not supported. If it's not a Python
package, it shouldn't be in the Python Package Index.

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ignoring XML Namespaces with cElementTree

2010-05-01 Thread Stefan Behnel

Carl Banks, 01.05.2010 12:33:

On Apr 29, 10:12 pm, Stefan Behnel wrote:

dmtr, 30.04.2010 04:57:

I don't want these "{http://www.very_long_url.com}"; in front of my
tags.  They create performance disaster on large files


I seriously doubt that they do.


I don't know what kind of XML files you deal with, but for me a large
XML file is gigabyte-sized (obviously I don't use Element Tree for
those).


Why not? I used cElementTree for files of that size (1-1.5GB unpacked) a 
couple of times, and it was never a problem.




Even for files tens-of-megabyte files string ops to expand tags with
namespaces is going to be a pretty decent penalty--remember
ElementTree does nothing lazily.


So? Did you run a profiler on it to know that there is a penalty due to the 
string concatenation? cElementTree's parser (expat) and its tree builder 
are blazingly fast, especially the iterparse() implementation.


http://codespeak.net/lxml/performance.html#parsing-and-serialising
http://codespeak.net/lxml/performance.html#a-longer-example
http://effbot.org/zone/celementtree.htm#benchmarks



(first cElementTree adds them, then I have to remove them in python).


I think that's your main mistake: don't remove them. Instead, use the fully
qualified names when comparing.


Unless you have multiple namespaces or are working with defined schema
or something, it's useless boilerplate.

It'd be a nice feature if ElementTree could let users optionally
ignore a namespace, unfortunately it doesn't have it.


I agree that that would make for a nice parser option, e.g. when dealing 
with HTML and XHTML in the same code.


Stefan

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


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Tim Chase

On 05/01/2010 12:08 AM, Patrick Maupin wrote:

+=, -=, /=, *=, etc.  conceptually (and, if lhs object supports in-
place operator methods, actually) *modify* the lhs object.

Your proposed .= syntax conceptually *replaces* the lhs object
(actually, rebinds the lhs symbol to the new object).


The += family of operators really do rebind the symbol, not 
modify the object.


  >>> from decimal import Decimal
  >>> d = Decimal(42)
  >>> e = Decimal(18)
  >>> orig = d
  >>> d += e
  >>> d
  Decimal("60")
  >>> e
  Decimal("18")
  >>> orig
  Decimal("42")
  >>> d is orig
  False

If your suggestion that += *modifies* the object, then orig would 
now unintuitively contain 60 and "d is orig" would return True.


This doesn't preclude you from implementing a self-mutating += 
style __add__ method and returning "self", but it's usually a bad 
idea unless it's dire for performance (and even then, think it 
over a couple times).


-tkc



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


Re: matching strings in a large set of strings

2010-05-01 Thread Stefan Behnel

Duncan Booth, 30.04.2010 10:20:

So more than 3GB just for the strings (and that's for Python 2.x on
Python 3.x you'll need nearly 5GB).

Running on a 64 bit version of Python should be fine, but for a 32 bit
system a naive approach just isn't going to work.

Option 1: use a trie. That should reduce storage, maybe it will reduce
it enough, maybe not. It depends on the data.


Depending on the implementation and the data, a trie can also use a lot 
/more/ space than the set of strings that it contains. The 83 million 14 
character strings can well include a relatively large subset of the 
possible permutations (imagine purely numeric, hex-numeric or lower-case 
alpha-numeric strings, for example), so the trie will likely need to branch 
very often with very low intermediate run length. If you use pointers for 
trie branches, that's at least 8 bytes per branch on a 64bit system, versus 
1 byte per character in a byte string list. Depending on the ratio of 
branches to characters, one or the other may win. So a "naive approach" 
likely won't work for tries either.


Stefan

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


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Stefan Behnel

Tim Chase, 01.05.2010 14:13:

On 05/01/2010 12:08 AM, Patrick Maupin wrote:

+=, -=, /=, *=, etc. conceptually (and, if lhs object supports in-
place operator methods, actually) *modify* the lhs object.

Your proposed .= syntax conceptually *replaces* the lhs object
(actually, rebinds the lhs symbol to the new object).


The += family of operators really do rebind the symbol, not modify the
object.

 >>> from decimal import Decimal
 >>> d = Decimal(42)
 >>> e = Decimal(18)
 >>> orig = d
 >>> d += e
 >>> d
Decimal("60")
 >>> e
Decimal("18")
 >>> orig
Decimal("42")
 >>> d is orig
False

If your suggestion that += *modifies* the object, then orig would now
unintuitively contain 60 and "d is orig" would return True.

This doesn't preclude you from implementing a self-mutating += style
__add__ method and returning "self", but it's usually a bad idea unless
it's dire for performance (and even then, think it over a couple times).


It's not like this was unprecedented in Python, though:

  Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
  [GCC 4.4.3] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> l = [1,2,3]
  >>> a = l
  >>> l += [4,5,6]
  >>> l
  [1, 2, 3, 4, 5, 6]
  >>> a
  [1, 2, 3, 4, 5, 6]

And I'm pretty sure this wasn't just done for performance reasons. Mutable 
data types behave that way.


Stefan

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


Re: Fast Efficient way to transfer an object to another list

2010-05-01 Thread Tim Chase

On 04/30/2010 10:35 PM, Steven D'Aprano wrote:

If you know there is one, and only one, item with that stock code:

def transfer_stock(stock_code, old_list, new_list):
 """ Transfer a stock from one list to another """
 i = old_list.index(stock_code)  # search
 new_list.append(old_list[i])  # copy
 del old_list[i]  # delete
 return new_list


This could be written as

  def move(code, source, dest):
dest.append(source.pop(source.index(code)))
return dest

depending on how one thinks.  I tend to prefer

  lst.pop(idx)

over

  tmp = lst[idx]
  del lst[idx]

only using the latter if "idx" is a range/slice.

Though since the function mutates the arguments, I'd be tempted 
to return None like list.sort() does for the same rationale.


If more than one item is in the source, it will move/remove the 
first leaving the remainder; if no matching item is in the source 
it will appropriately raise a ValueError.


-tkc




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


Help needed in downloading Brown corpus

2010-05-01 Thread Adil Kaleem
Hi all
I'm a masters student in NLP. I needed to download the Brown corpus.
I'm unable to download from nltk.org with the python CLI. My network
connection is behind a proxy server so it's creating a problem. Since
I don't know a bit of python, so unable to figure a way out. Can
someone help me in this regard either how to solve my downloading
problem or how to get Brown Corpus from some other link.
Thanks in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help needed in downloading Brown corpus

2010-05-01 Thread Shashank Singh
try this:

run this in your terminal before you hit nltk.download().
Don't forget to set username, password and proxy info for your own
system.(in PROXY_INFO)

import urllib2

PROXY_INFO = {
  'user' : username,
  'pass' : password,
  'host' : proxy_server,
  'port' : proxy_port
}

proxy_support = urllib2.ProxyHandler({"http" : \
"http://%(user)s:%(pass)s...@%(host)s:%(port)d" % PROXY_INFO})
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)

urllib2.install_opener(opener)

HTH

On Sat, May 1, 2010 at 6:58 PM, Adil Kaleem  wrote:

> Hi all
> I'm a masters student in NLP. I needed to download the Brown corpus.
> I'm unable to download from nltk.org with the python CLI. My network
> connection is behind a proxy server so it's creating a problem. Since
> I don't know a bit of python, so unable to figure a way out. Can
> someone help me in this regard either how to solve my downloading
> problem or how to get Brown Corpus from some other link.
> Thanks in advance.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Windows7 run python script / sub process with elevated privileges

2010-05-01 Thread News123
Hi,

I have a small python script, which has been started as normal non
privileged user.

At a later point in time it would like to start another python script
with elevated privileges.

How can I write my code such, that I will get the privilege elevation
prompt and I can start a sub process / python function with elevated
privileges.

thanks for any hints.

Under Linux Ubuntu I would use sudo/gksu

Under Windows 7 I'm a little lost.

A Python only solution is of course prefered, but any .bat .c .C# .cmd
wrapper would be fine.


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


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Alf P. Steinbach

On 01.05.2010 14:13, * Tim Chase:

On 05/01/2010 12:08 AM, Patrick Maupin wrote:

+=, -=, /=, *=, etc. conceptually (and, if lhs object supports in-
place operator methods, actually) *modify* the lhs object.

Your proposed .= syntax conceptually *replaces* the lhs object
(actually, rebinds the lhs symbol to the new object).


The += family of operators really do rebind the symbol, not modify the
object.

 >>> from decimal import Decimal
 >>> d = Decimal(42)
 >>> e = Decimal(18)
 >>> orig = d
 >>> d += e
 >>> d
Decimal("60")
 >>> e
Decimal("18")
 >>> orig
Decimal("42")
 >>> d is orig
False

If your suggestion that += *modifies* the object, then orig would now
unintuitively contain 60 and "d is orig" would return True.


In some cases += modifies the object. For CPython this is an optimization for 
the 'str' type, reducing to O(n) time the common newbie O(n^2) loops. The 
criterion for doing it is that there is exactly 1 reference (as is the case 
after a first append, subsequent appends can just modify).




This doesn't preclude you from implementing a self-mutating += style
__add__ method and returning "self", but it's usually a bad idea unless
it's dire for performance (and even then, think it over a couple times).


Agreed, at the Python level one doesn't in general have the necessary 
information to do it safely. Nothwithstanding the current CPython and Jython 
documentation error of sys.getrefcount (or whatever the name was) that indicates 
that it's available in any implementation.



Cheers,

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


Re: Fast Efficient way to transfer an object to another list

2010-05-01 Thread MRAB

Tim Chase wrote:

On 04/30/2010 10:35 PM, Steven D'Aprano wrote:

If you know there is one, and only one, item with that stock code:

def transfer_stock(stock_code, old_list, new_list):
 """ Transfer a stock from one list to another """
 i = old_list.index(stock_code)  # search
 new_list.append(old_list[i])  # copy
 del old_list[i]  # delete
 return new_list


This could be written as

  def move(code, source, dest):
dest.append(source.pop(source.index(code)))
return dest

depending on how one thinks.  I tend to prefer

  lst.pop(idx)

over

  tmp = lst[idx]
  del lst[idx]

only using the latter if "idx" is a range/slice.

Though since the function mutates the arguments, I'd be tempted to 
return None like list.sort() does for the same rationale.


If more than one item is in the source, it will move/remove the first 
leaving the remainder; if no matching item is in the source it will 
appropriately raise a ValueError.



It would be more efficient if instead of deleting or popping the item
you moved the last one into its place:

if idx == len(source) - 1:
item = source.pop()
else:
item = source[idx]
source[idx] = source.pop()

assuming that the order of the items in the list doesn't matter.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Fast Efficient way to transfer an object to another list

2010-05-01 Thread Francesco Bochicchio
On 1 Mag, 05:35, Steven D'Aprano  wrote:

>
> def transfer_stock(stock_code, old_list, new_list):
>     """ Transfer a stock from one list to another """
>     while True:  # loop forever
>         try:
>             i = old_list.index(stock_code)
>         except ValueError:
>             # not found, so we're done
>             break
>         new_list.append(old_list[i])
>         del old_list[i]
>     return new_list
>
> --
> Steven

I think this could be slower than doing like the OP, since  'index'
rescan the whole list every time
while doing an explicit loop you only scan the list once.

Anyway i think that list.extract( old_list, predicate ) -> new_list
would be a nice addition to the standard library
(possibly a C faster version of what one could implement in
python) ... and since the library is not under moratorium
maybe we will have it ...  the semantic could be like th OP asked:

--- code begins

class ListE(list):
def extract(self, predicate):
res = []
for idx, el in enumerate(self):
if predicate(el):
res.append( self.pop(idx) )
return res

class Stock(object):
def __init__(self, code):
self.code = code
def __repr__(self): return "Stock: code=%d" % self.code

l = ListE( Stock(n) for n in range(19) )

subl = l.extract( lambda x: x.code in (1,4, 9) )

print " l = ", l
print  "subl = ", subl

--- code ends
--- results

 l =  [Stock: code=0, Stock: code=2, Stock: code=3, Stock: code=5,
Stock: code=6, Stock: code=7, Stock: code=8, Stock: code=10, Stock:
code=11, Stock: code=12, Stock: code=13, Stock: code=14, Stock:
code=15, Stock: code=16, Stock: code=17, Stock: code=18]
subl =  [Stock: code=1, Stock: code=4, Stock: code=9]




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


Re: Windows7 run python script / sub process with elevated privileges

2010-05-01 Thread News123
I Found a first solution, though not very satisfying:
News123 wrote:
> Hi,
> 
> I have a small python script, which has been started as normal non
> privileged user.
> 
> At a later point in time it would like to start another python script
> with elevated privileges.
> 
> How can I write my code such, that I will get the privilege elevation
> prompt and I can start a sub process / python function with elevated
> privileges.
> 
> thanks for any hints.
> 
> Under Linux Ubuntu I would use sudo/gksu
> 
> Under Windows 7 I'm a little lost.
> 
> A Python only solution is of course prefered, but any .bat .c .C# .cmd
> wrapper would be fine.
> 

I could call following command:

PowerShell -Command (New-Object -com
'Shell.Application').ShellExecute('Cmd.exe', '/c
c:\abs_path_to_my_app\pyscript.pyw', '
', 'runas')


So I might do something like:

# ### script starts
import subprocess

cmd_wrap = "PowerShell -Command (New-Object -com "\
  "'Shell.Application').ShellExecute('Cmd.exe', "\
  "'/c %s', '', 'runas')"

args = r"c:\abspathtommyapp\myapp.pyw"

cmd = cmd_wrap % args

# create KW args to hide console
kwargs = { }
su = subprocess.STARTUPINFO()
su.dwFlags |= subprocess.STARTF_USESHOWWINDOW
su.wShowWindow = subprocess.SW_HIDE
kwargs['startupinfo'] = su
p = subprocess.Popen( cmd.split() , **kwargs )
# script ends here


However my issue is, that I have one annoying console window popping up
when PowerShell starts cmd.exe


Is there anything better?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fast Efficient way to transfer an object to another list

2010-05-01 Thread Daniel Stutzbach
On Fri, Apr 30, 2010 at 9:16 PM, Jimbo  wrote:

> Hello I have a relatively simple thing to do; move an object from one
> to list into another. But I think my solution maybe inefficient &
> slow.
>

Removing an item from a list is O(n) on average, so it's going to be a bit
slow any way you slice it (unless you only remove from the end of the list
which is O(1)).

Can you tell us more about why you're using a list?  If the order of the
items doesn't matter, you may be able to use a set().

If the order matters, how is the list ordered?  If we know how the list is
ordered, we may be able to help you come up with a clever way to remove an
item cheaply.
--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC 
-- 
http://mail.python.org/mailman/listinfo/python-list


recommended way to insert data into a one to many relationship using python

2010-05-01 Thread Wolfgang Meiners
Hi,

one to many relationships are fairly common, i think. So there should be
a recommended way to insert data into such a relation using python.


Given the following programm, what is the recommended way to insert the
list of NewEmployees to the database?


# !python
# -*- coding: utf-8 -*-

import sqlite3

con = sqlite3.connect(":memory:")
cur = con.cursor()

cur.execute("""create table employees(
eid integer primary key autoincrement,
name text not null,
rid integer references rooms(rid))""")

cur.execute("""create table rooms(
rid integer primary key autoincrement,
number integer,
fid integer references floors(fid))""")

cur.execute("""create table floors(
fid integer primary key autoincrement,
floor text not null)""")

cur.execute("""insert into floors(floor) values ('first floor')""")
cur.execute("""insert into floors(floor) values ('second floor')""")

cur.execute("""insert into rooms(number,fid) values (21, 1)""")
cur.execute("""insert into rooms(number,fid) values (22, 2)""")

cur.execute("""insert into employees(name,rid) values ('Joe', 1)""")
cur.execute("""insert into employees(name,rid) values ('Nancy', 2)""")

cur.execute("""create view emplist as select name, number, floor
from employees natural inner join rooms natural inner join
floors""")

print cur.execute("""select * from emplist order by name""").fetchall()

NewEmployees  =[]
NewEmployees.append({'name': 'George', 'room': 89, 'floor': 'third floor'})
NewEmployees.append({'name': 'Ellen', 'room': 21, 'floor': 'first floor'})

print NewEmployees
con.close()
===

Thank you for any hint
Wolfgang
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help needed in downloading Brown corpus

2010-05-01 Thread Peter Pearson
On Sat, 1 May 2010 06:28:33 -0700 (PDT), Adil Kaleem wrote:
> Hi all
> I'm a masters student in NLP. I needed to download the Brown corpus.
> I'm unable to download from nltk.org with the python CLI. My network
> connection is behind a proxy server so it's creating a problem. Since
> I don't know a bit of python, so unable to figure a way out. Can
> someone help me in this regard either how to solve my downloading
> problem or how to get Brown Corpus from some other link.
> Thanks in advance.

As I understand it (from Wikipedia), the Brown Corpus is a 
collection of samples of modern American English text, and
nltk.org provides a Python toolkit for exploring said Corpus.

I'm trying to figure out whether you're trying to get the
Corpus or the toolkit.  Neither interpretation looks
plausible: if you're after the Corpus itself and you don't
know a bit of Python, why are you struggling with Python?
On the other hand, if you're after the toolkit, what's the
problem? People download Python modules all the time.  And
besides, why would you be after the toolkit if you don't
know a bit of Python?

If your goal is to learn enough Python to use the toolkit,
you'll find this newsgroup full of smart and helpful people.

But you'll have to be a little more explicit about what
you're trying to do and more specific about what obstacles
you encounter.  As you might see by looking at other threads
on this newsgroup, the queries that get the most prompt and
useful responses usually include blocks of quoted text of
the form "I type this [cut-and-pasted block] and get
this response [another cut-and-pasted block]."

-- 
To email me, substitute nowhere->spamcop, invalid->net.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Lie Ryan
On 05/01/10 11:16, Steven D'Aprano wrote:
> On Fri, 30 Apr 2010 12:34:34 -0400, D'Arcy J.M. Cain wrote:
> 
> In practice though, I think that's a difference that makes no difference. 
> It walks like an operator, it swims like an operator, and it quacks like 
> an operator.
> 

Nope it's not. A full-time operator in python have a reflected version
(e.g. __radd__), which dot does not have. And Python's object system
makes it that the argument to __getattr__ is always a string even though
there might be a valid variable that corresponds to it:

a = MyClass()
b = MyClass()
print a . b

I've often wanted to figure out a way to (ab)use python's dot operator
for function composition (i.e. f.g(x) ==> f(g(x)) ). There's no way to
do it, not without being way too hackish. OTOH, doing so is quite
trivial with regular operators. In short, unless there's __rgetattr__
and unless you can refer to the right-hand operand as an object[1], dot
doesn't quack like an operator.

[1] well, technically string is an object, but you get what I mean
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Patrick Maupin
On May 1, 7:13 am, Tim Chase  wrote:
> On 05/01/2010 12:08 AM, Patrick Maupin wrote:
>
> > +=, -=, /=, *=, etc.  conceptually (and, if lhs object supports in-
> > place operator methods, actually) *modify* the lhs object.
>
> > Your proposed .= syntax conceptually *replaces* the lhs object
> > (actually, rebinds the lhs symbol to the new object).
>
> The += family of operators really do rebind the symbol, not
> modify the object.
>
>    >>> from decimal import Decimal
>    >>> d = Decimal(42)
>    >>> e = Decimal(18)
>    >>> orig = d
>    >>> d += e
>    >>> d
>    Decimal("60")
>    >>> e
>    Decimal("18")
>    >>> orig
>    Decimal("42")
>    >>> d is orig
>    False
>
> If your suggestion that += *modifies* the object, then orig would
> now unintuitively contain 60 and "d is orig" would return True.

Well, I wrote "conceptually" (which I believe is true; it's certainly
true for me) and "sometimes actually" (which I know is true):

>>> x = [1,2,3,4,5]
>>> y = x
>>> x += [6]
>>> y
[1, 2, 3, 4, 5, 6]

>>>
>>> x = set()
>>> y = x
>>> x |= set([1])
>>> y
set([1])

SO, if you find those results "unintuitive", perhaps you should
upgrade your understanding of python.  Personally, I don't find any of
the results I gave, or the results you gave, surprising, so I'm not
saying my "conceptually and sometimes actually modifies the result" is
right for *you* but it works great for me. :-)

> This doesn't preclude you from implementing a self-mutating +=
> style __add__ method and returning "self", but it's usually a bad
> idea unless it's dire for performance (and even then, think it
> over a couple times).

Well, you should submit a bug report to fix the operation of lists and
sets for a starter.

But first, you might want to read PEP 203 -- augmented assignments.  I
particularly like the section which says:

"The idea behind augmented assignment in Python is that it isn't just
an easier way to write the common practice of storing the result of a
binary operation in its left-hand operand, but also a way for the left-
hand operand in question to know that it should operate `on itself',
rather than creating a modified copy of itself."

There are a lot of sections which have a similar flavor.  If (which I
doubt), the "augmented dot" is accepted, it won't necessarily have the
same meaning.  x = x.foo could replace x with any other kind of
object, and I view it as a replacement, while I view x += foo as a
modification.

Regards,
Pat
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: recommended way to insert data into a one to many relationship using python

2010-05-01 Thread Peter Otten
Wolfgang Meiners wrote:

> Hi,
> 
> one to many relationships are fairly common, i think. So there should be
> a recommended way to insert data into such a relation using python.
> 
> 
> Given the following programm, what is the recommended way to insert the
> list of NewEmployees to the database?
> 
> 
> # !python
> # -*- coding: utf-8 -*-
> 
> import sqlite3
> 
> con = sqlite3.connect(":memory:")
> cur = con.cursor()
> 
> cur.execute("""create table employees(
> eid integer primary key autoincrement,
> name text not null,
> rid integer references rooms(rid))""")
> 
> cur.execute("""create table rooms(
> rid integer primary key autoincrement,
> number integer,
> fid integer references floors(fid))""")
> 
> cur.execute("""create table floors(
> fid integer primary key autoincrement,
> floor text not null)""")
> 
> cur.execute("""insert into floors(floor) values ('first floor')""")
> cur.execute("""insert into floors(floor) values ('second floor')""")
> 
> cur.execute("""insert into rooms(number,fid) values (21, 1)""")
> cur.execute("""insert into rooms(number,fid) values (22, 2)""")
> 
> cur.execute("""insert into employees(name,rid) values ('Joe', 1)""")
> cur.execute("""insert into employees(name,rid) values ('Nancy', 2)""")
> 
> cur.execute("""create view emplist as select name, number, floor
> from employees natural inner join rooms natural inner join
> floors""")
> 
> print cur.execute("""select * from emplist order by name""").fetchall()
> 
> NewEmployees  =[]
> NewEmployees.append({'name': 'George', 'room': 89, 'floor': 'third
> floor'}) NewEmployees.append({'name': 'Ellen', 'room': 21, 'floor': 'first
> floor'})
> 
> print NewEmployees
> con.close()
> 
===
> 
> Thank you for any hint
> Wolfgang

There are probably some opportunities for generalization lurking in the 
following mess, but it would take me more time than I'm willing to invest.

cur.execute("create table new_employees (name, room, floor, fid);")
cur.executemany("""insert into new_employees (name, room, floor) 
   values (:name, :room, :floor)""", NewEmployees)

c2 = con.cursor()

missing = c2.execute("""
select distinct n.floor from new_employees n
left outer join floors f on n.floor = f.floor
where f.floor is null
""")
cur.executemany("insert into floors (floor) values (?)", missing)
cur.execute("""
update new_employees
set fid = (select fid from floors where floors.floor = 
new_employees.floor)""")


missing = c2.execute("""
select distinct n.fid, n.room from new_employees n
left outer join rooms r on n.fid = r.fid and n.room = r.number
where r.fid is null""")
cur.executemany("insert into rooms (fid, number) values (?, ?)", missing)

new = c2.execute("""
select n.name, r.rid from new_employees n, rooms r
where n.room = r.number and n.fid == r.fid
""")
cur.executemany("insert into employees (name, rid) values (?, ?)", new)

If your data is small enough you may try to do the heavy lifting in Python 
instead of SQL; if not, maybe you'd better ask in a SQL forum.

Peter

Afterthought: Can SQLAlchemy do these kind of things cleanly?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread D'Arcy J.M. Cain
On Sun, 02 May 2010 05:08:53 +1000
Lie Ryan  wrote:
> On 05/01/10 11:16, Steven D'Aprano wrote:
> > On Fri, 30 Apr 2010 12:34:34 -0400, D'Arcy J.M. Cain wrote:
> > 
> > In practice though, I think that's a difference that makes no difference. 
> > It walks like an operator, it swims like an operator, and it quacks like 
> > an operator.
> > 
> 
> Nope it's not. A full-time operator in python have a reflected version

Hi.  Please watch the attributions.  I didn't write that.  I was the
one who said that '.' was not an operator.

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


jpeg package

2010-05-01 Thread Paul Johnston
Hi,

I've used the jpeg library on PyPI in the past and it's been great:
http://pypi.python.org/pypi/jpeg/0.1.4

However, the library home page is now unaccessible. I can't even find
the library on archive.org. Any idea how I can get it?
http://www.emilas.com/jpeg/

Thanks,

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


Re: matching strings in a large set of strings

2010-05-01 Thread News123
Dennis Lee Bieber wrote:
> On Sat, 01 May 2010 13:48:02 +0200, News123  declaimed
> the following in gmane.comp.python.general:
> 
>> Dennis Lee Bieber wrote:
>>> That lets you do a binary search on the file. Much faster than a
>>> linear search (linear search will average out to 41.5M read operations;
>>> binary should be around 1 reads)
>> Don't you meant 27 reads instead of 41.5 M reads?
>>
>   Don't you mean my 1 reads? The 41.5M is the average for the
> linear search.
>  
Indeed, this is what I meant. or phrased differently:
"about 27 reads with a binary search instead of 41.5M reads average with
a linear search."




> from math import log
> log(83e6)/log(2)
>> 26.306608000671101
>   Probably -- it was late at night and I was working things out in my
> head...

I know about late nights.  I just wondered whether I overlooked something.

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


Re: [capi-sig] SWIG + expy

2010-05-01 Thread Jack Jansen

On  27-Apr-2010, at 08:30 , Yingjie Lan wrote:

> Hi,
> 
> Is it possible to use SWIG to parse C/C++, and provide an interface for me to 
> generate some code? I thought it might be good to have SWIG help generate 
> expy (see http://expy.sourceforge.net) files, then generate the python 
> extension via expy.


I would be very interested in a universal intermediate format for all the 
interface generators. I'm still using a version of Guido's old bgen, now 
grudgingly extended to handle C++ and do bidirectional bridging between Python 
and C++, and while I love and cherish the code generator the C++ parser is, 
uhm... challenging.
Parsing C++ with per-line regular expressions is no fun:-)

I looked at gccxml at some point, as well as at some of the competing Python 
interface generators, but it went nowhere. gccxml output is far too detailed, 
and the output is too much of a simple parse tree to be of any use. The 
intermediate formats of the other interface generators I looked at were all too 
inaccessible.

Maybe we can come up with something decent in this group?

If there is enough interest: I can start by describing bgen's intermediate 
format, and if other people do the same for theirs we may be able to get to 
common ground...
--
Jack Jansen, , http://www.cwi.nl/~jack
If I can't dance I don't want to be part of your revolution -- Emma Goldman



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


Re: jpeg package

2010-05-01 Thread Vincent Davis
This gets you the cached page

link

I also fond some of the source pages in googles cache

  *Vincent Davis
720-301-3003 *
vinc...@vincentdavis.net
 my blog  |
LinkedIn

On Sat, May 1, 2010 at 2:16 PM, Paul Johnston  wrote:

> Hi,
>
> I've used the jpeg library on PyPI in the past and it's been great:
> http://pypi.python.org/pypi/jpeg/0.1.4
>
> However, the library home page is now unaccessible. I can't even find
> the library on archive.org. Any idea how I can get it?
> http://www.emilas.com/jpeg/
>
> Thanks,
>
> Paul
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need direction on mass find/replacement in HTML files

2010-05-01 Thread Novocastrian_Nomad
One single line regex solution would be:

re.sub(r'http\://www.mysite.org/\?page=([^"]+)',r'pages/\1.htm',html)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Steven D'Aprano
On Sun, 02 May 2010 05:08:53 +1000, Lie Ryan wrote:

> On 05/01/10 11:16, Steven D'Aprano wrote:
>> On Fri, 30 Apr 2010 12:34:34 -0400, D'Arcy J.M. Cain wrote:
>> 
>> In practice though, I think that's a difference that makes no
>> difference. It walks like an operator, it swims like an operator, and
>> it quacks like an operator.
>> 
>> 
> Nope it's not. A full-time operator in python have a reflected version
> (e.g. __radd__), which dot does not have. 

What are the reflected versions of __eq__ and __ne__ (binary == and != 
operators)?

And __neg__, __pos__ and __inv__ (for the unary - + and ~ operators)?

And the three-argument form of __pow__ for power(1, 2, x)?



> And Python's object system
> makes it that the argument to __getattr__ is always a string even though
> there might be a valid variable that corresponds to it:

That is nothing to do with the object system, it is related to the 
semantics of Python syntax. a.b doesn't mean "apply the binary dot 
operator to arguments a and b". It is syntactic sugar for "look for an 
attribute named 'b' on object a". As such, the operands that __getattr__ 
receives are the object a and the *name* b (implemented as a string).

Also, the implementation of attribute lookup is quite complex, with all 
sorts of special cases and optimizations.


> a = MyClass()
> b = MyClass()
> print a . b
> 
> I've often wanted to figure out a way to (ab)use python's dot operator
> for function composition (i.e. f.g(x) ==> f(g(x)) ). There's no way to
> do it, not without being way too hackish.

That's a good example of where the difference does make a difference.



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


Re: Fast Efficient way to transfer an object to another list

2010-05-01 Thread Steven D'Aprano
On Sat, 01 May 2010 08:11:45 -0700, Francesco Bochicchio wrote:

> On 1 Mag, 05:35, Steven D'Aprano 
> wrote:
> 
> 
>> def transfer_stock(stock_code, old_list, new_list):
>>     """ Transfer a stock from one list to another """ while True:  #
>>     loop forever
>>         try:
>>             i = old_list.index(stock_code)
>>         except ValueError:
>>             # not found, so we're done
>>             break
>>         new_list.append(old_list[i])
>>         del old_list[i]
>>     return new_list
>>
>> --
>> Steven
> 
> I think this could be slower than doing like the OP, since  'index'
> rescan the whole list every time
> while doing an explicit loop you only scan the list once.

If the list is sufficiently big enough, yes, you are correct. But since 
list.index is fast C code, and a while loop is slow Python code, it would 
need to be fairly big, and probably much bigger than you think!

The simplest way to speed the above code up is not to start from the 
beginning each time. That requires two very small changes. And since 
deletions from the front of the list are slow, MRAB's suggestion is also 
a good idea. This requires another very small change. Putting them 
together:

# Untested.
def transfer_stock(stock_code, old_list, new_list):
    """ Transfer a stock from one list to another """
i = 0
while True:  # loop forever
        try:
            i = old_list.index(stock_code, i)
        except ValueError:
            # not found, so we're done
            break
        new_list.append(old_list[i])
        old_list[i] = old_list[-1]
del old_list[-1]
    return new_list

This should be very much faster, for hardly any extra complexity.



> Anyway i think that list.extract( old_list, predicate ) -> new_list
> would be a nice addition to the standard library (possibly a C faster
> version of what one could implement in python) ... and since the library
> is not under moratorium maybe we will have it ...  

But since it is a method on a built-in (list), and not a library 
function, it does fall under the moratorium.



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


Re: Help needed in downloading Brown corpus

2010-05-01 Thread Steven D'Aprano
On Sat, 01 May 2010 06:28:33 -0700, Adil Kaleem wrote:

> Hi all
> I'm a masters student in NLP. I needed to download the Brown corpus. I'm
> unable to download from nltk.org with the python CLI. My network
> connection is behind a proxy server so it's creating a problem. Since I
> don't know a bit of python, so unable to figure a way out. 

This has nothing to do with Python, it is a network issue. Talk to your 
network administrator and get him to fix the proxy, or give you a way to 
bypass it. Or if you have an internet connection at home, download it 
from there.


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


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Steven D'Aprano
On Sat, 01 May 2010 07:13:42 -0500, Tim Chase wrote:

> On 05/01/2010 12:08 AM, Patrick Maupin wrote:
>> +=, -=, /=, *=, etc.  conceptually (and, if lhs object supports in-
>> place operator methods, actually) *modify* the lhs object.
>>
>> Your proposed .= syntax conceptually *replaces* the lhs object
>> (actually, rebinds the lhs symbol to the new object).
> 
> The += family of operators really do rebind the symbol, not modify the
> object.

They potentially do both, depending on the object, even for built-ins.


>>>> from decimal import Decimal
[...]

I'm not sure why you took the trouble to import Decimal for this example, 
when you could have shown the same thing with built-ins int or float. All 
three types are immutable.

A counter example with a mutable type:

>>> a = []
>>> b = a
>>> a += [2]
>>> a
[2]
>>> b
[2]

thus demonstrating that __iadd__ modifies in place as well as rebinds for 
at least one mutable type.

> This doesn't preclude you from implementing a self-mutating += style
> __add__ method and returning "self", but it's usually a bad idea 

Obviously the Python dev team don't agree with that :)

Just to prove that += for lists is not an accident:

>>> a = set()
>>> b = a
>>> a |= set([1])
>>> a
set([1])
>>> b
set([1])



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


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Chris Rebert
On Sat, May 1, 2010 at 6:32 PM, Steven D'Aprano
 wrote:
> On Sat, 01 May 2010 07:13:42 -0500, Tim Chase wrote:
>> This doesn't preclude you from implementing a self-mutating += style
>> __add__ method and returning "self", but it's usually a bad idea
>
> Obviously the Python dev team don't agree with that :)
>
> Just to prove that += for lists is not an accident:
>
 a = set()
 b = a
 a |= set([1])
 a
> set([1])
 b
> set([1])

In both cases, __iOP__ operator methods are being used, not vanilla
__OP__ methods, so neither of your examples are relevant to Mr.
Chase's point.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Steven D'Aprano
On Sat, 01 May 2010 19:03:04 -0700, Chris Rebert wrote:

> On Sat, May 1, 2010 at 6:32 PM, Steven D'Aprano
>  wrote:
>> On Sat, 01 May 2010 07:13:42 -0500, Tim Chase wrote:
>>> This doesn't preclude you from implementing a self-mutating += style
>>> __add__ method and returning "self", but it's usually a bad idea
>>
>> Obviously the Python dev team don't agree with that :)
>>
>> Just to prove that += for lists is not an accident:
[...]
> In both cases, __iOP__ operator methods are being used, not vanilla
> __OP__ methods, so neither of your examples are relevant to Mr. Chase's
> point.


I'm sorry, I read Tim's reference to __add__ (instead of __iadd__) as a 
typo. Having __add__ mutate self would be a strange thing to do.



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


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Patrick Maupin
On May 1, 9:03 pm, Chris Rebert  wrote:
> In both cases, __iOP__ operator methods are being used, not vanilla
> __OP__ methods, so neither of your examples are relevant to Mr.
> Chase's point.

Well, Tim's main assertion was: "The += family of operators really do
rebind the symbol, not modify the object."

So, using __iadd__ to disprove this blanket assertion is certainly
relevant.

Regards,
Pat


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


Re: Ignoring XML Namespaces with cElementTree

2010-05-01 Thread dmtr
> Unless you have multiple namespaces or are working with defined schema
> or something, it's useless boilerplate.
>
> It'd be a nice feature if ElementTree could let users optionally
> ignore a namespace, unfortunately it doesn't have it.


Yep. Exactly my point. Here's a link to the patch addressing this:
http://bugs.python.org/issue8583
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Aahz
In article <4bdcd631$0$27782$c3e8...@news.astraweb.com>,
Steven D'Aprano   wrote:
>On Sat, 01 May 2010 07:13:42 -0500, Tim Chase wrote:
>> 
>> The += family of operators really do rebind the symbol, not modify the
>> object.
>
>They potentially do both, depending on the object, even for built-ins.

No, they always rebind; sometimes they modify the object and sometimes
they rebind the original target to the same object.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [capi-sig] SWIG + expy

2010-05-01 Thread Stefan Behnel

Jack Jansen, 01.05.2010 23:40:

I would be very interested in a universal intermediate format for all
the interface generators. I'm still using a version of Guido's old bgen,
now grudgingly extended to handle C++ and do bidirectional bridging
between Python and C++, and while I love and cherish the code generator
the C++ parser is, uhm... challenging. Parsing C++ with per-line regular
expressions is no fun:-)

I looked at gccxml at some point, as well as at some of the competing
Python interface generators, but it went nowhere. gccxml output is far
too detailed, and the output is too much of a simple parse tree to be of
any use. The intermediate formats of the other interface generators I
looked at were all too inaccessible.

Maybe we can come up with something decent in this group?


This sounds like a good topic for the CAPI-sig group, rather than for 
c.l.py. I suggest keeping subsequent discussion there.


Stefan

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


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Alf P. Steinbach

On 02.05.2010 06:06, * Aahz:

In article<4bdcd631$0$27782$c3e8...@news.astraweb.com>,
Steven D'Aprano  wrote:

On Sat, 01 May 2010 07:13:42 -0500, Tim Chase wrote:


The += family of operators really do rebind the symbol, not modify the
object.


They potentially do both, depending on the object, even for built-ins.


No, they always rebind; sometimes they modify the object


If they always rebind and sometimes modify object then they "potentially do 
both", and so the "No" at the start of the sentence contradicts this later part.




and sometimes
they rebind the original target to the same object.


At the Python level that seems to be an undetectable null-operation. Granted one 
could see something going on in a machine code or byte code debugger. But making 
that distinction (doing nothing versus self-assignment) at the Python level 
seems, to me, to be meaningless.



Cheers,

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


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Chris Rebert
On Sat, May 1, 2010 at 10:05 PM, Alf P. Steinbach  wrote:
> On 02.05.2010 06:06, * Aahz:
>> In article<4bdcd631$0$27782$c3e8...@news.astraweb.com>,
>> Steven D'Aprano  wrote:
>>>
>>> On Sat, 01 May 2010 07:13:42 -0500, Tim Chase wrote:

 The += family of operators really do rebind the symbol, not modify the
 object.
>>>
>>> They potentially do both, depending on the object, even for built-ins.
>>
>> No, they always rebind; sometimes they modify the object
>
> If they always rebind and sometimes modify object then they "potentially do
> both", and so the "No" at the start of the sentence contradicts this later
> part.
>
>
>> and sometimes
>> they rebind the original target to the same object.
>
> At the Python level that seems to be an undetectable null-operation. Granted
> one could see something going on in a machine code or byte code debugger.
> But making that distinction (doing nothing versus self-assignment) at the
> Python level seems, to me, to be meaningless.

There are some circumstances where the subtle distinction matters.
Consider x.y += z where x is an instance of a class that overrides
__setattr__ and __getattribute__ and x.y results in a mutable object.
Not doing the assignment can result in a noticeable difference in
behavior since __setattr__ won't get called. Yes, this is a slightly
obscure case, but it does come up.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help needed in downloading Brown corpus

2010-05-01 Thread Adil Kaleem
On May 1, 10:54 pm, Peter Pearson  wrote:

>
> As I understand it (from Wikipedia), the Brown Corpus is a
> collection of samples of modern American English text, and
> nltk.org provides a Python toolkit for exploring said Corpus.
>
> I'm trying to figure out whether you're trying to get the
> Corpus or the toolkit.  Neither interpretation looks
> plausible: if you're after the Corpus itself and you don't
> know a bit of Python, why are you struggling with Python?
> On the other hand, if you're after the toolkit, what's the
> problem? People download Python modules all the time.  And
> besides, why would you be after the toolkit if you don't
> know a bit of Python?
>
> If your goal is to learn enough Python to use the toolkit,
> you'll find this newsgroup full of smart and helpful people.
>
> But you'll have to be a little more explicit about what
> you're trying to do and more specific about what obstacles
> you encounter.  As you might see by looking at other threads
> on this newsgroup, the queries that get the most prompt and
> useful responses usually include blocks of quoted text of
> the form "I type this [cut-and-pasted block] and get
> this response [another cut-and-pasted block]."
>
> --
> To email me, substitute nowhere->spamcop, invalid->net.



Hi Peter,
Thanks for replying. Appreciate your pain in writing. I'm sorry I
wasn't proper in my question. I downloaded the toolkit and from it I
wanted to download the corpora. But since my system is behind the
proxy (no direct connection), I couldn't download the corpora as was
the process told for downloading using Python IDLE. The exact steps/
codes to write were given on the website for downloading but was only
for a direct connection. I didn't knew the process of configuring IDLE
for a proxy network for the download. And lastly since I'm unaware of
Python programming, I faced the problem. This was my total essence of
the problem and in this regard, I sought to take help from the very
best of this language knowers i.e, you all.
But nonetheless, I got my problem solved. One amongst you, Shashank
Singh, in this group helped me in solving the problem. I thank him
again for taking all the pain in making the system work for me. Thanks
Shashank, I owe you man.

Thank you Peter for giving your precious time in reading and replying
to my problem.
-- 
http://mail.python.org/mailman/listinfo/python-list


sometype.__new__ and C subclasses

2010-05-01 Thread James Porter
I've been trying to write a Python C extension module that uses NumPy 
and has a subtype of numpy.ndarray written in C. However, I've run into 
a snag: calling numpy.ndarray.__new__(mysubtype, ...) triggers an 
exception in the bowels of Python (this is necessary for a handful of 
NumPy features). I'm posting to this list to try to figure out why this 
exception exists in the first place, and what (if anything) I can do to 
work around it.


The exception in question happens in Objects/typeobject.c in 
tp_new_wrapper. Here's the comment for the block:


/* Check that the use doesn't do something silly and unsafe like
   object.__new__(dict).  To do this, we check that the
   most derived base that's not a heap type is this type. */

The code has the end effect that basetype.__new__(subtype, ...) fails 
whenever subtype is a statically-defined type (i.e. a normal C extension 
type object). Why is this necessary in general? I can see why it might 
be bad for a limited number of core Python types, but it seems 
unnecessarily limiting for Python C extensions.


On a more practical note, is there a way (short of rewriting the subtype 
in Python) to work around this? It seems that I could call the type 
metaclass to create a heap type in C, but I'm not sure of all the 
implications of that.


Thanks in advance,
Jim

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


Re: Help needed in downloading Brown corpus

2010-05-01 Thread Adil Kaleem
On May 2, 6:30 am, Steven D'Aprano  wrote:
> On Sat, 01 May 2010 06:28:33 -0700, Adil Kaleem wrote:
> > Hi all
> > I'm a masters student in NLP. I needed to download the Brown corpus. I'm
> > unable to download from nltk.org with the python CLI. My network
> > connection is behind a proxy server so it's creating a problem. Since I
> > don't know a bit of python, so unable to figure a way out.
>
> This has nothing to do with Python, it is a network issue. Talk to your
> network administrator and get him to fix the proxy, or give you a way to
> bypass it. Or if you have an internet connection at home, download it
> from there.
>
> --
> Steven


Hi Steven,
It all had to do with configuring the network from Python IDLE,
aliter: It all had to do with Python. I could have done it, the
bypasssing. But I couldn't have learned anything if I hadn't fought
with the problem, of course it was Shashank Singh from this group who
helped me through. Bypassing is always not good, atleast for me and
therefore I came to seek the help of you all.
Thanks Steven in reading and replying to my problem.

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


Re: Python dot-equals (syntax proposal)

2010-05-01 Thread Lie Ryan
On 05/02/10 10:58, Steven D'Aprano wrote:
>> > And Python's object system
>> > makes it that the argument to __getattr__ is always a string even though
>> > there might be a valid variable that corresponds to it:
> That is nothing to do with the object system, it is related to the 
> semantics of Python syntax. a.b doesn't mean "apply the binary dot 
> operator to arguments a and b". It is syntactic sugar for "look for an 
> attribute named 'b' on object a". As such, the operands that __getattr__ 
> receives are the object a and the *name* b (implemented as a string).

You just described *exactly* the reason why dot is not, and cannot be an
operator.
-- 
http://mail.python.org/mailman/listinfo/python-list