Re: how to structure a directory with many scripts and shared code

2010-02-16 Thread Benedict Verheyen
Steven D'Aprano wrote:
> On Mon, 15 Feb 2010 16:29:05 +0100, Benedict Verheyen wrote:
> 
>> However, when i make a subdirectory, for example database and put a
>> script in there, how would i import logutils or mailtutils from within
>> the database subdirectory? This fails:
>> from tools.logutils import logger
> 
> Sounds like you need to ensure that the top level directory needs to be 
> added to your PYTHONPATH.
> 
> Do you need instructions to do this?
> 

Hi Steve,


thanks, i think i know how to add the top level directory to the PYTHONPATH.

I'm however looking further into the suggestion of ssteinerX to use a
setup.py file to add the tools utility scripts to the pythonpath.
In the near future, another person will be helping me with programming
and thus, i want to make sure the structure is usable by other people
than myself.

Furthermore, i'm thinking if it wouldn't be cleaner to make such a setup.py
file per script, at least the bigger, more important scripts.
Now my structure is like this:

python_scripts
|
|-->trunk
..|-> all scripts
..|-> tools

The python_scripts directory and subdirs are versioned via SVN.
I think these steps would help:

1. Make a package of tools.
2. Put the bigger scripts in a seperate directory also using a setup.py file.
3. If possible, put the other scripts that somehow belong together in a seperate
directory. Names need to be more clear but i'm just illustrating a point.

python_scripts
|
|-->trunk
..|-> my big script 1
|-> setup.py
..|-> my big script 2
|-> setup.py
..|-> database
|-> database script 1
|-> database script 2
..|-> tools
|-> setup.py

Does that look like a clear structure?

Thanks,
Benedict


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


Re: listing existing windows services with python

2010-02-16 Thread alex23
"Alf P. Steinbach"  wrote:
> it's great that you provide the kind
> of help that you did, pointing out a probably very good module that it seems
> gives the required functionality, and giving an URL.

Yes, because that's _actually helping people_ and not just
contributing the usual self-serving pontificating nonsense that just
_flows_ from your goddamn mouth like a river of effluent
psuedotruths.

If you really wanted to help you would've cited _actual security
flaws_ in your first response  to me, instead of hand waving us away
to once again do the leg work of verification. Based on your postings
here, you regularly don't have the time or inclination to back up some
of the bizarre claims you make. It would actually improve the signal
in this channel a lot if you just chose not to make those posts at all.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Union of class variables

2010-02-16 Thread alex23
On Feb 16, 6:16 pm, Joan Miller  wrote:
> Is possible to get a third class with the class variables of another
> two classes?
>
> 
> class A:
>     foo = 1
>
> class B:
>     bar = 2
> 

Through multiple inheritance?

  >>> class C(A, B):
  ...   pass
  ...
  >>> C.foo
  1
  >>> C.bar
  2
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: listing existing windows services with python

2010-02-16 Thread Alf P. Steinbach

* alex23:

"Alf P. Steinbach"  wrote:

it's great that you provide the kind
of help that you did, pointing out a probably very good module that it seems
gives the required functionality, and giving an URL.


Yes, because that's _actually helping people_ and not just
contributing the usual self-serving pontificating nonsense that just
_flows_ from your goddamn mouth like a river of effluent
psuedotruths.


That's an off-topic personal attack, and it's completely untrue.

You're wasting bandwidth.



If you really wanted to help you would've cited _actual security
flaws_ in your first response  to me, instead of hand waving us away
to once again do the leg work of verification.


Posting two ranting and raving articles complaining about 1 (one) line or so in 
an earlier article, just because you personally wouldn't want to know that, 
that's a total waste of the reader's time.


Disregarding the negative emotional adjectives you add everywhere, it's a good 
heuristic to post what you would want to know.


I would want to know that a service is started, that it's designed for net based 
administration by Microsoft, and that so it might constitute a security risk.


And so I added that information, each point of which is relevant. If you don't 
want to know about the "probably" little thing you can just skim over it. If 
you're unable to do that then something's wrong with you.




Based on your postings
here, you regularly don't have the time or inclination to back up some
of the bizarre claims you make. It would actually improve the signal
in this channel a lot if you just chose not to make those posts at all.


That an off-topic personal attack, and it's completely untrue.

Another poster (not me) has described you in an article here as a sockpuppet 
(false identity being in reality another poster here) and a troll; I believe at 
least the troll bit.


It would reduce the noise level a bit if you stopped contributing noise like 
above.


Cheers,

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


Re: Printing with raw_input

2010-02-16 Thread Peter Otten
Shashwat Anand wrote:

> raw_input uses sys.stderr I guess ?

I had a look at the C code, but it's a bit confusing.

If I'm reading it correctly the prompt is written to the "real" stderr if 
and only if sys.stdin and sys.stdout are attached to a terminal.

$ python -c"raw_input('prompt\n')" 2>tmp.txt
foo
$ cat tmp.txt
prompt
$ python -c"raw_input('prompt\n')" 2>tmp.txt | cat
foo
prompt
$ cat tmp.txt

I wonder if that is intentional.

Peter


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


Re: Union of class variables [Solved]

2010-02-16 Thread Joan Miller
On 16 feb, 08:40, alex23  wrote:
> On Feb 16, 6:16 pm, Joan Miller  wrote:
>
> > Is possible to get a third class with the class variables of another
> > two classes?
>
> > 
> > class A:
> >     foo = 1
>
> > class B:
> >     bar = 2
> > 
>
> Through multiple inheritance?
>
>   >>> class C(A, B):
>   ...   pass
>   ...
>   >>> C.foo
>   1
>   >>> C.bar
>   2

Yes! It solves the problem :) Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Union of class variables

2010-02-16 Thread Ben Finney
Joan Miller  writes:

> Is possible to get a third class with the class variables of another
> two classes?

Multiple inheritance is allowed in Python:

class Baz(Foo, Bar):
pass

However, it leads to complications that you likely don't want, e.g.
http://www.artima.com/weblogs/viewpost.jsp?thread=246341>.

Is it *really* the case that the new class is best expressed by an IS-A
relationship to *both* the others? (“Every Baz IS-A Foo; every Baz IS-A
Bar”)

You should consider composition, instead. Decide which other class
represents the more general case of the new class, and give it
additional capabilities through a HAS-A relationship. (“Every Baz IS-A
Foo; every Baz HAS-A Bar”)

class Baz(Foo):

def __init__(self):
self.bar = Bar()

That way, you keep the clarity of single inheritance and additional
capabilities are accessed through an attribute.

-- 
 \“Always code as if the guy who ends up maintaining your code |
  `\ will be a violent psychopath who knows where you live.” —John |
_o__) F. Woods |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: listing existing windows services with python

2010-02-16 Thread Ben Finney
alex23  writes:

> Yes, because that's _actually helping people_ and not just
> contributing the usual self-serving pontificating nonsense that just
> _flows_ from your goddamn mouth like a river of effluent psuedotruths.

I can't see what improvement you hope to achieve by sending something
like this. If, instead, you just want to vent cathartically, this isn't
the place to do it.

Please, don't send the above kind of vitriol to this public forum.
Better yet, compose it in your editor, bask in what you've written, then
delete it unsent.

-- 
 \“Kill myself? Killing myself is the last thing I'd ever do.” |
  `\—Homer, _The Simpsons_ |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


finding contents from string

2010-02-16 Thread danin
Hi all,
I am looking for particular solution. I am having one string
say:
string- "http://maps.google.co.in/maps/ms?
hl=en&ie=UTF8&msa=0&msid=106178526636832397420.00047fb46fa8d02481f09&ll=20.730428,86.456909&spn=2.178194,3.526611&z=8
"
  and from this string  i need to extract value
-20.730428,86.456909. These value are dynamic so i cant use filter. So
can anyone please tell me about how to do this.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding contents from string

2010-02-16 Thread Arnaud Delobelle
danin  writes:

> Hi all,
> I am looking for particular solution. I am having one string
> say:
> string- "http://maps.google.co.in/maps/ms?
> hl=en&ie=UTF8&msa=0&msid=106178526636832397420.00047fb46fa8d02481f09&ll=20.730428,86.456909&spn=2.178194,3.526611&z=8
> "
>   and from this string  i need to extract value
> -20.730428,86.456909. These value are dynamic so i cant use filter. So
> can anyone please tell me about how to do this.

In Python 2.5:

>>> import urlparse
>>> url='http://www.example.com/foo?bar=42&spam=eggs'
>>> parsed_url = urlparse.urlparse(url)
>>> parsed_url.query
'bar=42&spam=eggs'
>>> import cgi
>>> parsed_query = cgi.parse_qs(parsed_url.query)
>>> parsed_query
{'bar': ['42'], 'spam': ['eggs']}

In Python >= 2.6, parse_qs lives in the urlparse module as well.

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


Re: finding contents from string

2010-02-16 Thread Peter Otten
danin wrote:

> Hi all,
> I am looking for particular solution. I am having one string
> say:
> string- "http://maps.google.co.in/maps/ms?
> 
hl=en&ie=UTF8&msa=0&msid=106178526636832397420.00047fb46fa8d02481f09&ll=20.730428,86.456909&spn=2.178194,3.526611&z=8
> "
>   and from this string  i need to extract value
> -20.730428,86.456909. 

Where does the "-" come from?

> These value are dynamic so i cant use filter. 

I don't understand.

> So can anyone please tell me about how to do this.

>>> from urlparse import urlparse, parse_qs
>>> parse_qs(urlparse(string).query)["ll"][0]
'20.730428,86.456909'

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


Re: Few Small Questions Regarding CGI

2010-02-16 Thread Lawrence D'Oliveiro
In message <631b0785-38db-4c12-
a82a-7b11e2235...@o16g2000prh.googlegroups.com>, joy99 wrote:

> Is there any other material or URL for step by step learning of CGI.

There’s the official spec here 
.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding contents from string

2010-02-16 Thread Lars Behrens
danin wrote:

> can anyone please tell me about how to do this.

Now come on, you have to give a *bit* more information. What have you done 
so far? What did you plan? What are the rules for finding the string?

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


Updating Packages in 2.5 (win/numpy) and Related Matters

2010-02-16 Thread W. eWatson
I normally use IDLE on Win, but recently needed to go to command prompt 
to see all error messages. When I did, I was greeted by a host of 
deprecation and Numpy messages before things got running. The program 
otherwise functioned OK, after I found the problem I was after. Are 
these messages a warning to get to the next update of numpy?


I would guess that updating to a higher update does not mean I need to 
remove the old one, correct?


In general for libraries like numpy or scipy, I use win32 updates, but I 
see win32-p3 updates too on download pages. Since I may be distributing 
this program to p3 machines, will I need to provide the win32-p3 updates 
to those users?


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


Re: listing existing windows services with python

2010-02-16 Thread News123
Thanks for your answers,




The security wouldn't be a concern, as I would run while being only
accessible by trusted hosts.

I don't use the script often, so if it would start a WMI service during
runtime and stop it afterwards it would be fine.

I just wonder whether installing another 3rd party package for just
listing all services s worth the effort.


In my current case I'll go probably for parsing the output of
"sc.exe query" or I'll traverse the registry.

currently I just want to get a list of the names of the running services.


bye


N



Alf P. Steinbach wrote:
> * alex23:
>> News123  wrote:
>>> What is the best way with python to get a list of all windows services.
>>>
>>> As a start I would be glad to receive only the service names.
>>>
>>> However it would be nicer if I could get all the properties of a service
>>> as well.
>>
>> I highly recommend Tim Golden's fantastic WMI module[1].
> 
> It's probably Very Good, but one Microsoft-thing one should be aware of:
> using WMI functionality generally starts up a background WMI service...
> 
> Similarly, starting the standard clipboard viewer (discontinued in
> Windows Vista) starts up a silly copy-clipboard-contents-over-network
> service.
> 
> On a low-range machine the WMI service can be a resource concern.
> 
> I don't know whether the WMI service is a security concern (it probably
> is, considering IIS and anything Microsoft+Network and that WMI is meant
> to administrate machines over network, it's an enterprise thing not a
> personal computer user thing), but the clipboard service is, as I
> recall, a concern.
> 
> 
>> >>> import wmi
>> >>> c = wmi.WMI()
>> >>> services = c.Win32_Service()
>> >>> s = services[0]
>> >>> s
>> <_wmi_object: \\LIB-D5NYF1S\root
>> \cimv2:Win32_Service.Name="Alerter">
>> >>> s.properties
>> {u'DisplayName': None, u'ServiceSpecificExitCode': None, u'State':
>> None, u'Syste
>> mName': None, u'ErrorControl': None, u'Status': None,
>> u'ProcessId': None, u'Desc
>> ription': None, u'Started': None, u'AcceptStop': None,
>> u'CheckPoint': None, u'Pa
>> thName': None, u'WaitHint': None, u'Name': None, u'InstallDate':
>> None, u'Caption
>> ': None, u'StartMode': None, u'DesktopInteract': None,
>> u'ServiceType': None, u'T
>> agId': None, u'StartName': None, u'AcceptPause': None,
>> u'CreationClassName': Non
>> e, u'SystemCreationClassName': None, u'ExitCode': None}
>> >>> s.Name
>> u'Alerter'
>> >>> s.Started
>> False
>> >>> s.ServiceType
>> u'Share Process'
>>
>> 1: http://timgolden.me.uk/python/wmi/index.html
> 
> Cheers,
> 
> - Alf
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: finding contents from string

2010-02-16 Thread Praveen Boppana
It looks like you want to extract the query parameters from standard url's. 
There is a standard Python module to accomplish this:

http://www.python.org/doc/2.5.2/lib/module-urlparse.html




From: danin 
To: python-list@python.org
Sent: Tue, 16 February, 2010 4:13:09 PM
Subject: finding contents from string

Hi all,
I am looking for particular solution. I am having one string
say:
string- "http://maps.google.co.in/maps/ms?
hl=en&ie=UTF8&msa=0&msid=106178526636832397420.00047fb46fa8d02481f09&ll=20.730428,86.456909&spn=2.178194,3.526611&z=8
"
  and from this string  i need to extract value
-20.730428,86.456909. These value are dynamic so i cant use filter. So
can anyone please tell me about how to do this.
-- 
http://mail.python.org/mailman/listinfo/python-list



  Your Mail works best with the New Yahoo Optimized IE8. Get it NOW! 
http://downloads.yahoo.com/in/internetexplorer/-- 
http://mail.python.org/mailman/listinfo/python-list


Re: listing existing windows services with python

2010-02-16 Thread Tim Golden

On 16/02/2010 12:18, News123 wrote:

I don't use the script often, so if it would start a WMI service during
runtime and stop it afterwards it would be fine.


FWIW -- your other considerations notwithstanding -- I'm not aware
of WMI having this effect. Generally you can assume that the WMI
services are running (or not, if someone's shut them off) but
I've never heard of them being started up by virtue of a call
to the WMI subsystem and then stopped afterwards.

That said, I'd be interested if someone did have a pointer for this;
worth putting a caveat in the docs if that were the case.

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


Re: Is there a simple way to find the list index to the max value?

2010-02-16 Thread Arnaud Delobelle
"W. eWatson"  writes:

> See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.

Here are a few ways.

>>> a = [1,4,9,3]
>>> max_index = max(xrange(len(a)), key=a.__getitem__)
>>> max_index
2
>>> # Or:
... max_index = max((n, i) for i, n in enumerate(a))[1]
>>> max_index
2
>>> # Or:
... from itertools import *
>>> max_index = max(izip(a, count()))[1]
>>> max_index
2

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


Re: Is there a simple way to find the list index to the max value?

2010-02-16 Thread steve

On 02/16/2010 05:49 PM, W. eWatson wrote:

See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.


The most obvious would be a.index(max(a)). Is that what you wanted ?

cheers,
- steve
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a simple way to find the list index to the max value?

2010-02-16 Thread Arnaud Delobelle
Arnaud Delobelle  writes:

> "W. eWatson"  writes:
>
>> See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
>
> Here are a few ways.

[...]

My copy past went wrond and I forgot the first one:

>>> a = [1,4,9,3]
>>> max_index = a.index(max(a))
>>> max_index
2

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


Re: listing existing windows services with python

2010-02-16 Thread Alf P. Steinbach

* Tim Golden:

On 16/02/2010 12:18, News123 wrote:

I don't use the script often, so if it would start a WMI service during
runtime and stop it afterwards it would be fine.


FWIW -- your other considerations notwithstanding -- I'm not aware
of WMI having this effect. Generally you can assume that the WMI
services are running (or not, if someone's shut them off) but
I've never heard of them being started up by virtue of a call
to the WMI subsystem and then stopped afterwards.

That said, I'd be interested if someone did have a pointer for this;
worth putting a caveat in the docs if that were the case.

TJG


I just googled the filename from memory, found

  http://www.neuber.com/taskmanager/process/wmiprvse.exe.html>

Don't know if I've disabled it because invoking wmic didn't produce it.

Uh, wait, since it hosts the provider service(s), perhaps...

Yes, 'wmic service list brief' which actually retrieves some information (I 
guess it can be anything, I remembered this from listing process command lines) 
started an [wmprvse.exe] process.


It terminated after the query/usage, but as I recall in some cases it doesn't.

I don't know how that works with programmatic access, but it's worth checking 
out.


Cheers,

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


Re: listing existing windows services with python

2010-02-16 Thread Tim Golden

On 16/02/2010 12:48, Alf P. Steinbach wrote:

I just googled the filename from memory, found

http://www.neuber.com/taskmanager/process/wmiprvse.exe.html>

Don't know if I've disabled it because invoking wmic didn't produce it.

Uh, wait, since it hosts the provider service(s), perhaps...

Yes, 'wmic service list brief' which actually retrieves some information (I
guess it can be anything, I remembered this from listing process command lines)
started an [wmprvse.exe] process.

It terminated after the query/usage, but as I recall in some cases it doesn't.

I don't know how that works with programmatic access, but it's worth checking 
out.


Thanks. As I understand it, you're not talking about a *service* starting up
or shutting down; rather a *process* starting up etc., presumably controlled
by the underlying service?

I'll do some further research to see what's going on there.

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


Re: listing existing windows services with python

2010-02-16 Thread Alf P. Steinbach

* Tim Golden:

On 16/02/2010 12:48, Alf P. Steinbach wrote:

I just googled the filename from memory, found

http://www.neuber.com/taskmanager/process/wmiprvse.exe.html>

Don't know if I've disabled it because invoking wmic didn't produce it.

Uh, wait, since it hosts the provider service(s), perhaps...

Yes, 'wmic service list brief' which actually retrieves some 
information (I
guess it can be anything, I remembered this from listing process 
command lines)

started an [wmprvse.exe] process.

It terminated after the query/usage, but as I recall in some cases it 
doesn't.


I don't know how that works with programmatic access, but it's worth 
checking out.


Thanks. As I understand it, you're not talking about a *service* 
starting up
or shutting down; rather a *process* starting up etc., presumably 
controlled

by the underlying service?


It doesn't seem to provide ordinary Windows "service"s, but it's a bit unclear 
since e.g. the URL above says




Beginning with Windows XP, WMI resides in a shared service host with several 
other services. To avoid stopping all the services when a provider fails, 
providers are loaded into a separate host process named Wmiprvse.exe. Multiple 
instances of Wmiprvse.exe can run at the same time under different accounts: 
LocalSystem, NetworkService or LocalService. The WMI core WinMgmt.exe is loaded 
into the shared Local Service host named Svchost.exe.

Note: wmiprvsw.exe is the Sasser worm!

Note: The wmiprvse.exe file is located in the folder C:\WINDOWS\System32\Wbem. 
In other cases, wmiprvse.exe is a virus, spyware, trojan or worm! Check this 
with Security Task Manager.




Checking for ordinary Windows services:


C:\test> (wmic service list >nul) & tasklist /svc /fi "imagename eq 
wmiprvse.exe"

Image Name   PID Services
= == =
wmiprvse.exe1076 N/A

C:\test> _





I'll do some further research to see what's going on there.


Cheers,

- Alf (is this off-topic for the group?)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using class attributes

2010-02-16 Thread Leo Breebaart
Chris Rebert  writes:

> On Mon, Feb 15, 2010 at 10:29 AM, Leo Breebaart  wrote:
>
> > I have a base class Foo with a number of derived classes FooA,
> > FooB, FooC, etc. Each of these derived classes needs to read
> > (upon initialisation) text from an associated template file
> > FooA.tmpl, FooB.tmpl, FooC.tmpl, etc.
> > [...]
> > But, since this information is the same for every instance of
> > each derived class, I was wondering if there was a way to achieve
> > the same thing outside of the __init__ function, and just have
> > these assignments be done as a class attribute (i.e. so that I
> > can refer to FooA.template_body, etc.)
> 
> Metaclasses to the rescue!:
> 
> class WithTemplateAttrs(type):
> def __new__(cls, name, bases, dct):
> klass =3D type.__new__(cls, name, bases, dct)
> klass.template_filename =3D "%s.tmpl" % name
> klass.template_body =3D read_body_from(klass.template_filename)
> return klass
> 
> class Foo(object):
> __metaclass__ =3D WithTemplateAttrs
> #rest of class body here
> 
> Now just have FooA, FooB, etc. subclass Foo as before. They'll
> automatically get the attributes generated.

Thanks for the feedback! I am thrilled that an actual real-life
issue I'm having may be resolvable by metaclasses (which so far
I've only admired from afar but never considered relevant to my
day-to-day work), but unfortunately I'm still struggling to get
this to work.

If I add your code, what happens is that the Foo class will try
to read "Foo.tmpl", which does not exist -- it is only the
derived classes FooA etc, that need to execute this code, not Foo
itself.

And actually that makes sense -- I think my problem was not too
clearly thought out to begin with. Of course it's not possible to
associate a single template_body with the Foo class, it will be a
different template for each derived class. So at best I need to
associate your metaclass with each derived class, but at that
point I might as well just read the template in the __init__()
method with __class__.__name__, and use lazy evaluation / caching
to avoid doing the actual file-reading work more than once.

I think.

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


Re: listing existing windows services with python

2010-02-16 Thread Martin P. Hellwig

On 02/16/10 13:51, Alf P. Steinbach wrote:


- Alf (is this off-topic for the group?)
Strictly speaking yes, but I do find it interesting and there is nothing 
wrong with ignoring posts you don't like to read.


--
mph

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


Implicit __init__ execution in multiple inheritance

2010-02-16 Thread rludwinowski
class A:
def __init__(self):
print("A__init__")

class B:
def __init__(self):
print("B__init__")

class C(A, B):
pass

C()

>> A__init__

Why __init__ class B will not be automatic executed?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using class attributes

2010-02-16 Thread Arnaud Delobelle
Leo Breebaart  writes:

> Chris Rebert  writes:
>
>> On Mon, Feb 15, 2010 at 10:29 AM, Leo Breebaart  wrote:
>>
>> > I have a base class Foo with a number of derived classes FooA,
>> > FooB, FooC, etc. Each of these derived classes needs to read
>> > (upon initialisation) text from an associated template file
>> > FooA.tmpl, FooB.tmpl, FooC.tmpl, etc.
>> > [...]
>> > But, since this information is the same for every instance of
>> > each derived class, I was wondering if there was a way to achieve
>> > the same thing outside of the __init__ function, and just have
>> > these assignments be done as a class attribute (i.e. so that I
>> > can refer to FooA.template_body, etc.)
>> 
>> Metaclasses to the rescue!:
>> 
>> class WithTemplateAttrs(type):
>> def __new__(cls, name, bases, dct):
>> klass =3D type.__new__(cls, name, bases, dct)
>> klass.template_filename =3D "%s.tmpl" % name
>> klass.template_body =3D read_body_from(klass.template_filename)
>> return klass
>> 
>> class Foo(object):
>> __metaclass__ =3D WithTemplateAttrs
>> #rest of class body here
>> 
>> Now just have FooA, FooB, etc. subclass Foo as before. They'll
>> automatically get the attributes generated.
>
> Thanks for the feedback! I am thrilled that an actual real-life
> issue I'm having may be resolvable by metaclasses (which so far
> I've only admired from afar but never considered relevant to my
> day-to-day work), but unfortunately I'm still struggling to get
> this to work.
>
> If I add your code, what happens is that the Foo class will try
> to read "Foo.tmpl", which does not exist -- it is only the
> derived classes FooA etc, that need to execute this code, not Foo
> itself.
>
> And actually that makes sense -- I think my problem was not too
> clearly thought out to begin with. Of course it's not possible to
> associate a single template_body with the Foo class, it will be a
> different template for each derived class. So at best I need to
> associate your metaclass with each derived class, but at that
> point I might as well just read the template in the __init__()
> method with __class__.__name__, and use lazy evaluation / caching
> to avoid doing the actual file-reading work more than once.
>
> I think.

Descriptors to the rescue :)

def read_body_from(filename):
print "** loading content **"
return "" % filename

# This is a kind of class property
class TemplateFilename(object):
def __get__(self, obj, cls):
return "%s.tmpl" % cls.__name__

# And this is a kind of cached class property
class TemplateBody(object):
def __get__(self, obj, cls):
try:
return cls._body
except AttributeError:
cls._body = read_body_from(cls.template_filename)
return cls._body

class Foo(object):
template_filename = TemplateFilename()
template_body = TemplateBody()

class FooA(Foo):
   pass

class FooB(Foo):
   pass

# In action:
>>> FooA.template_filename
'FooA.tmpl'
>>> FooB.template_filename
'FooB.tmpl'
>>> FooA.template_body
** loading content **
""
>>> FooA.template_body
""
>>> foob = FooB()
>>> foob.template_filename
'FooB.tmpl'
>>> foob.template_body
** loading content **
""
>>> foob.template_body
""

HTH

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


Re: Implicit __init__ execution in multiple inheritance

2010-02-16 Thread Arnaud Delobelle
rludwinowski  writes:

> class A:
> def __init__(self):
> print("A__init__")
>
> class B:
> def __init__(self):
> print("B__init__")
>
> class C(A, B):
> pass
>
> C()
>
>>> A__init__
>
> Why __init__ class B will not be automatic executed?

Because it's documented behaviour?  By default, at initialisation, an
instance of C will go up the method resolution order and only execture
the first __init__() method found.  If you want to change this, you have
to do it explicitely within the __init__ method(s) of the parent
class(es).  E.g. try this (assuming Python 3 syntax):

class A:
def __init__(self):
super().__init__()
print("A__init__")

class B:
def __init__(self):
super().__init__()
print("B__init__")

class C(A, B):
pass

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


Re: how to structure a directory with many scripts and shared code

2010-02-16 Thread sstein...@gmail.com

On Feb 16, 2010, at 3:28 AM, Benedict Verheyen wrote:

> Steven D'Aprano wrote:
>> On Mon, 15 Feb 2010 16:29:05 +0100, Benedict Verheyen wrote:
>> 
>>> However, when i make a subdirectory, for example database and put a
>>> script in there, how would i import logutils or mailtutils from within
>>> the database subdirectory? This fails:
>>>from tools.logutils import logger
>> 
>> Sounds like you need to ensure that the top level directory needs to be 
>> added to your PYTHONPATH.
>> 
>> Do you need instructions to do this?
>> 
> 
> Hi Steve,
> 
> 
> thanks, i think i know how to add the top level directory to the PYTHONPATH.
> 
> I'm however looking further into the suggestion of ssteinerX to use a
> setup.py file to add the tools utility scripts to the pythonpath.
> In the near future, another person will be helping me with programming
> and thus, i want to make sure the structure is usable by other people
> than myself.
> 
> Furthermore, i'm thinking if it wouldn't be cleaner to make such a setup.py
> file per script, at least the bigger, more important scripts.
> Now my structure is like this:
> 
> python_scripts
> |
> |-->trunk
> ..|-> all scripts
> ..|-> tools
> 
> The python_scripts directory and subdirs are versioned via SVN.
> I think these steps would help:
> 
> 1. Make a package of tools.
> 2. Put the bigger scripts in a seperate directory also using a setup.py file.
> 3. If possible, put the other scripts that somehow belong together in a 
> seperate
> directory. Names need to be more clear but i'm just illustrating a point.
> 
> python_scripts
> |
> |-->trunk
> ..|-> my big script 1
> |-> setup.py
> ..|-> my big script 2
> |-> setup.py
> ..|-> database
> |-> database script 1
> |-> database script 2
> ..|-> tools
> |-> setup.py
> 
> Does that look like a clear structure?

No.

Make one setup.py at the top, put related scripts (like database) into separate 
sub-modules, so they can all be imported off a common 'trunk' as you have it 
above i.e. as trunk.database.xxx.

Then use entry points for any command line scripts.

Slightly harder, to setup initially but much cleaner to use and maintain and, 
it all installs with one call to setup.py develop.

S

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


Re: Using class attributes

2010-02-16 Thread Jean-Michel Pichavant

Arnaud Delobelle wrote:

Leo Breebaart  writes:

  

Chris Rebert  writes:



On Mon, Feb 15, 2010 at 10:29 AM, Leo Breebaart  wrote:

  

I have a base class Foo with a number of derived classes FooA,
FooB, FooC, etc. Each of these derived classes needs to read
(upon initialisation) text from an associated template file
FooA.tmpl, FooB.tmpl, FooC.tmpl, etc.
[...]
But, since this information is the same for every instance of
each derived class, I was wondering if there was a way to achieve
the same thing outside of the __init__ function, and just have
these assignments be done as a class attribute (i.e. so that I
can refer to FooA.template_body, etc.)


Metaclasses to the rescue!:

class WithTemplateAttrs(type):
def __new__(cls, name, bases, dct):
klass =3D type.__new__(cls, name, bases, dct)
klass.template_filename =3D "%s.tmpl" % name
klass.template_body =3D read_body_from(klass.template_filename)
return klass

class Foo(object):
__metaclass__ =3D WithTemplateAttrs
#rest of class body here

Now just have FooA, FooB, etc. subclass Foo as before. They'll
automatically get the attributes generated.
  

Thanks for the feedback! I am thrilled that an actual real-life
issue I'm having may be resolvable by metaclasses (which so far
I've only admired from afar but never considered relevant to my
day-to-day work), but unfortunately I'm still struggling to get
this to work.

If I add your code, what happens is that the Foo class will try
to read "Foo.tmpl", which does not exist -- it is only the
derived classes FooA etc, that need to execute this code, not Foo
itself.

And actually that makes sense -- I think my problem was not too
clearly thought out to begin with. Of course it's not possible to
associate a single template_body with the Foo class, it will be a
different template for each derived class. So at best I need to
associate your metaclass with each derived class, but at that
point I might as well just read the template in the __init__()
method with __class__.__name__, and use lazy evaluation / caching
to avoid doing the actual file-reading work more than once.

I think.



Descriptors to the rescue :)

def read_body_from(filename):
print "** loading content **"
return "" % filename

# This is a kind of class property
class TemplateFilename(object):
def __get__(self, obj, cls):
return "%s.tmpl" % cls.__name__

# And this is a kind of cached class property
class TemplateBody(object):
def __get__(self, obj, cls):
try:
return cls._body
except AttributeError:
cls._body = read_body_from(cls.template_filename)
return cls._body

class Foo(object):
template_filename = TemplateFilename()
template_body = TemplateBody()

class FooA(Foo):
   pass

class FooB(Foo):
   pass

# In action:
  

FooA.template_filename


'FooA.tmpl'
  

FooB.template_filename


'FooB.tmpl'
  

FooA.template_body


** loading content **
""
  

FooA.template_body


""
  

foob = FooB()
foob.template_filename


'FooB.tmpl'
  

foob.template_body


** loading content **
""
  

foob.template_body


""

HTH

  
While all these proposals are giving interesting technical anwsers to 
the OP problem, I think that the OP original code is still the best (_imo_).


 class Foo(object):
 def __init__(self):
 self.template_filename = "%s.tmpl" % self.__class__.__name__
 self.template_body = read_body_from(self.template_filename)


That is true  the same attributes are defined for every instance, but, 
honestly, who cares ? (unless you target the best class design 2010 
price :-) )

This solution beat the others in terms of simplicity and readability.

It's still usefull to train your mind with decorators and metaclasses 
though, they can save your life eventually.


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


Re: plugin / intra process communication system

2010-02-16 Thread Paul Kölle

Am 15.02.2010 23:12, schrieb Florian Ludwig:

On Sun, 2010-02-14 at 18:47 +0100, Diez B. Roggisch wrote:

[...]

And then of course, this is not really needed. In Python, behavior
counts, not type-information. So you can get away without any explicit
declared interface. You might chose to not do that, for aestetic
reasons, or better documentation. But you aren't forced.


Actually some plugin-systems in python do force you and they check if
your "implementation" comply with the "interface".

Which is a good thing IMO ;)


Here is one solution I came up with.

[...]
Can you post working code? It's not clear what "pbus" is and how it works.

cheers
 Paul



What do you think? Any obvious pitfalls (besides reinventing something)?

Please keep in mind that syntax/api is not "done" or anything its just
an concept presentation.

Thanks,
Florian






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


Re: Implicit __init__ execution in multiple inheritance

2010-02-16 Thread Bruno Desthuilliers

Arnaud Delobelle a écrit :

rludwinowski  writes:


class A:
def __init__(self):
print("A__init__")

class B:
def __init__(self):
print("B__init__")

class C(A, B):
pass

C()


A__init__

Why __init__ class B will not be automatic executed?


Because it's documented behaviour?  By default, at initialisation, an
instance of C will go up the method resolution order and only execture
the first __init__() method found.


Note to the OP : Python's "methods" are attributes, so the normal 
attribute lookup rules apply - which is why the lookup stops at the 
first (in _mro_ order) '__init__' method found.


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


Re: Using class attributes

2010-02-16 Thread Arnaud Delobelle
Jean-Michel Pichavant  writes:
[...]
> While all these proposals are giving interesting technical anwsers to
> the OP problem, I think that the OP original code is still the best
> (_imo_).
>
>  class Foo(object):
>  def __init__(self):
>  self.template_filename = "%s.tmpl" % self.__class__.__name__
>  self.template_body = read_body_from(self.template_filename)
>
>
> That is true  the same attributes are defined for every instance, but,
> honestly, who cares ? (unless you target the best class design 2010
> price :-) )

You might care if you have many instances but few classes, and the size
of template_body is significant.  Although with this design it is
possible to cache template bodies within the 'read_body_from' function
if necessary.

Also I was under the impression that the OP wanted these attribute to be
accessible from the class object as well.

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


Re: Updating Packages in 2.5 (win/numpy) and Related Matters

2010-02-16 Thread Robert Kern

On 2010-02-16 06:16 AM, W. eWatson wrote:

I normally use IDLE on Win, but recently needed to go to command prompt
to see all error messages. When I did, I was greeted by a host of
deprecation and Numpy messages before things got running. The program
otherwise functioned OK, after I found the problem I was after. Are
these messages a warning to get to the next update of numpy?

I would guess that updating to a higher update does not mean I need to
remove the old one, correct?

In general for libraries like numpy or scipy, I use win32 updates, but I
see win32-p3 updates too on download pages. Since I may be distributing
this program to p3 machines, will I need to provide the win32-p3 updates
to those users?


You will definitely want to ask these questions on the numpy-discussion mailing 
list. They are numpy-specific. Please copy-and-paste the messages that you get.


  http://www.scipy.org/Mailing_Lists

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Is there a simple way to find the list index to the max value?

2010-02-16 Thread W. eWatson

On 2/16/2010 4:41 AM, Arnaud Delobelle wrote:

Arnaud Delobelle  writes:


"W. eWatson"  writes:


See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.


Here are a few ways.


[...]

My copy past went wrond and I forgot the first one:


a = [1,4,9,3]
max_index = a.index(max(a))
max_index

2


Ah, the good one for last! Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: listing existing windows services with python

2010-02-16 Thread sstein...@gmail.com
On Feb 16, 2010, at 4:04 AM, Ben Finney wrote:

> Please, don't send the above kind of vitriol to this public forum.
> Better yet, compose it in your editor, bask in what you've written, then 
> delete it unsent.

+1

If you kids want to have some sort of pissing-in-your-sockpuppet type of 
contest to see who can out-whatever each other, do it off-list and don't waste 
everyone else's time with.

What ever happened to "don't feed the trolls" ??  If you really believe 
someone's trolling, don't answer, block them, and move on.  

Providing a lengthy, well-reasoned reply only shows that they've pulled you 
into the troll pit for a fight with them, regardless of whether you're "right."

S

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


Re: Updating Packages in 2.5 (win/numpy) and Related Matters

2010-02-16 Thread W. eWatson

On 2/16/2010 7:30 AM, Robert Kern wrote:

On 2010-02-16 06:16 AM, W. eWatson wrote:

I normally use IDLE on Win, but recently needed to go to command prompt
to see all error messages. When I did, I was greeted by a host of
deprecation and Numpy messages before things got running. The program
otherwise functioned OK, after I found the problem I was after. Are
these messages a warning to get to the next update of numpy?

I would guess that updating to a higher update does not mean I need to
remove the old one, correct?

In general for libraries like numpy or scipy, I use win32 updates, but I
see win32-p3 updates too on download pages. Since I may be distributing
this program to p3 machines, will I need to provide the win32-p3 updates
to those users?


You will definitely want to ask these questions on the numpy-discussion
mailing list. They are numpy-specific. Please copy-and-paste the
messages that you get.

http://www.scipy.org/Mailing_Lists

Good idea, for the first part of this. I would think "In genera for ..." 
would be answerable here, but I'll give them both a shot.


I'll post what I find here, as a follow up.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python Optimization

2010-02-16 Thread Paul Boddie
On 14 Feb, 19:41, Steve Howell  wrote:
>
> I ditto the profiling recommendation.
>
> http://docs.python.org/library/profile.html

(To the original inquirer...) Try this, too:

http://wiki.python.org/moin/PythonSpeed/Profiling

If you have the tools, it's a lot easier than scanning through tables
of times.

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


Re: listing existing windows services with python

2010-02-16 Thread Steve Holden
Alf P. Steinbach wrote:
[...]
>> I'll do some further research to see what's going on there.
> 
> Cheers,
> 
> - Alf (is this off-topic for the group?)

It's gone a lot further off than this without anyone complaining.  I
think your experiences to date should convince you that you can rely on
being told when you drift too far off-topic :)

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: how to structure a directory with many scripts and shared code

2010-02-16 Thread Benedict Verheyen
sstein...@gmail.com wrote:
> On Feb 16, 2010, at 3:28 AM, Benedict Verheyen wrote:


>> python_scripts
>> |
>> |-->trunk
>> ..|-> my big script 1
>> |-> setup.py
>> ..|-> my big script 2
>> |-> setup.py
>> ..|-> database
>> |-> database script 1
>> |-> database script 2
>> ..|-> tools
>> |-> setup.py
>>
>> Does that look like a clear structure?
> 
> No.
> 
> Make one setup.py at the top, put related scripts (like database) into 
> separate sub-modules,
> so they can all be imported off a common 'trunk' as you have it above i.e. as 
> trunk.database.xxx.
> 
> Then use entry points for any command line scripts.
> 
> Slightly harder, to setup initially but much cleaner to use and maintain and, 
> it all installs with one call to setup.py develop.
> 
> S

Hhhm, i can see how it makes the maintenance cleaner.
However, in the case of a single setup.py, i will end up installing scripts on 
servers
that will never use them, only some.
As with making egg files for the bigger scripts, you will only install what's 
needed.
Then again, the script are small so in that respect it doesn't really matter.

How would i best distribute it? Via the single setup.py or building an egg from 
the
setup.py or plain copying the files to the server?
The code is company specific so nobody else will benefit from it.

Thanks,
Benedict


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


Re: Updating Packages in 2.5 (win/numpy) and Related Matters

2010-02-16 Thread Robert Kern

On 2010-02-16 09:37 AM, W. eWatson wrote:

On 2/16/2010 7:30 AM, Robert Kern wrote:

On 2010-02-16 06:16 AM, W. eWatson wrote:

I normally use IDLE on Win, but recently needed to go to command prompt
to see all error messages. When I did, I was greeted by a host of
deprecation and Numpy messages before things got running. The program
otherwise functioned OK, after I found the problem I was after. Are
these messages a warning to get to the next update of numpy?

I would guess that updating to a higher update does not mean I need to
remove the old one, correct?

In general for libraries like numpy or scipy, I use win32 updates, but I
see win32-p3 updates too on download pages. Since I may be distributing
this program to p3 machines, will I need to provide the win32-p3 updates
to those users?


You will definitely want to ask these questions on the numpy-discussion
mailing list. They are numpy-specific. Please copy-and-paste the
messages that you get.

http://www.scipy.org/Mailing_Lists


Good idea, for the first part of this. I would think "In genera for ..."
would be answerable here, but I'll give them both a shot.


Both parts are very numpy/scipy-specific, trust me. The reason that numpy has 
binary releases for Pentium 3 machines is because we have SSE code of various 
levels due to our accelerated BLAS libraries. This is very rare outside of numpy 
and scipy.


And please provide the information that I asked for over in numpy-discussion.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: how to structure a directory with many scripts and shared code

2010-02-16 Thread Jean-Michel Pichavant

Benedict Verheyen wrote:

sstein...@gmail.com wrote:
  

On Feb 16, 2010, at 3:28 AM, Benedict Verheyen wrote:




  

python_scripts
|
|-->trunk
..|-> my big script 1
|-> setup.py
..|-> my big script 2
|-> setup.py
..|-> database
|-> database script 1
|-> database script 2
..|-> tools
|-> setup.py

Does that look like a clear structure?
  

No.

Make one setup.py at the top, put related scripts (like database) into separate 
sub-modules,
so they can all be imported off a common 'trunk' as you have it above i.e. as 
trunk.database.xxx.

Then use entry points for any command line scripts.

Slightly harder, to setup initially but much cleaner to use and maintain and, 
it all installs with one call to setup.py develop.

S



Hhhm, i can see how it makes the maintenance cleaner.
However, in the case of a single setup.py, i will end up installing scripts on 
servers
that will never use them, only some.
  
You should care about this only if your scripts exceeed a faire amount 
of Ko.

Are you sure you've written Mo of python code ?

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


Re: how to structure a directory with many scripts and shared code

2010-02-16 Thread Benedict Verheyen
Jean-Michel Pichavant wrote:
> Benedict Verheyen wrote:


>> Hhhm, i can see how it makes the maintenance cleaner.
>> However, in the case of a single setup.py, i will end up installing
>> scripts on servers
>> that will never use them, only some.
>>   
> You should care about this only if your scripts exceeed a faire amount
> of Ko.
> Are you sure you've written Mo of python code ?
> 
> JM

Nope and i indicated that before:

"Then again, the script are small so in that respect it doesn't really matter."

Regards,
Benedict

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


Re: Which mock library do you prefer?

2010-02-16 Thread Lacrima
On Feb 16, 2:17 am, Ben Finney  wrote:
> Lacrima  writes:
> > Minimock has wider usage and community, but I have some troubles using
> > it. Maybe I am wrong, but with minimock you always have to keep track
> > the order of imports in your test modules. Well, may be I just don't
> > understand fully how minimock works.
>
> I'm not sure why you think you need to keep track of the order of
> imports.
>
> Simply set up the mocks as you want them, in your fixtures; then, when
> tearing down your fixtures, use ‘minimock.restore()’ to restore the
> affected namespaces to their initial state.
>
> --
>  \       “… one of the main causes of the fall of the Roman Empire was |
>   `\        that, lacking zero, they had no way to indicate successful |
> _o__)                  termination of their C programs.” —Robert Firth |
> Ben Finney

Hi Ben!

See these two topics:
http://groups.google.com/group/minimock-dev/browse_thread/thread/bcbb3b7cc60eb96f
http://groups.google.com/group/minimock-dev/browse_thread/thread/c41cd996735ea1a6

There are special cases, which you have to be aware of, if you use
minimock.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using class attributes

2010-02-16 Thread Terry Reedy

On 2/16/2010 8:56 AM, Leo Breebaart wrote:

Chris Rebert  writes:


On Mon, Feb 15, 2010 at 10:29 AM, Leo Breebaart  wrote:


I have a base class Foo with a number of derived classes FooA,
FooB, FooC, etc. Each of these derived classes needs to read
(upon initialisation) text from an associated template file
FooA.tmpl, FooB.tmpl, FooC.tmpl, etc.
[...]
But, since this information is the same for every instance of
each derived class, I was wondering if there was a way to achieve
the same thing outside of the __init__ function, and just have
these assignments be done as a class attribute (i.e. so that I
can refer to FooA.template_body, etc.)


Metaclasses to the rescue!:

class WithTemplateAttrs(type):
 def __new__(cls, name, bases, dct):
 klass =3D type.__new__(cls, name, bases, dct)
 klass.template_filename =3D "%s.tmpl" % name
 klass.template_body =3D read_body_from(klass.template_filename)
 return klass

class Foo(object):
 __metaclass__ =3D WithTemplateAttrs
 #rest of class body here

Now just have FooA, FooB, etc. subclass Foo as before. They'll
automatically get the attributes generated.


Thanks for the feedback! I am thrilled that an actual real-life
issue I'm having may be resolvable by metaclasses (which so far
I've only admired from afar but never considered relevant to my
day-to-day work),


I thought this a really nice example of metaclass use too.

> but unfortunately I'm still struggling to get

this to work.

If I add your code, what happens is that the Foo class will try
to read "Foo.tmpl", which does not exist -- it is only the
derived classes FooA etc, that need to execute this code, not Foo
itself.


My simpleminded solution to the above would be to create a dummy 
Foo.tmpl file, so it does exist. Or, in __new__ above, conditionalize 
the fetch: if name != 'Foo': ...


Terry Jan Reedy

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


Re: Which mock library do you prefer?

2010-02-16 Thread Lacrima
On Feb 15, 9:56 pm, Phlip  wrote:
> Lacrima wrote:
> > Thanks for your reply! Isn't what you are talking about integration
> > tests? And unit tests should be fully isolated? So even for method
> > 'some_method()' of class A I should mock instance of class A (i.e. to
> > mock 'self') to test 'some_method()'.
>
> "Unit test" is a high-end QA concept. Developers can get the best
> return on "developer tests". They don't bother with aerospace-quality
> isolation between units.
>
> If a TDD test needs to pull in a bunch of modules to pass, that's
> generally a good thing, because they all get indirect testing. If they
> catch a bug, their local tests might not catch it, but the higher
> level tests still have a chance.
>
> (And if your product still needs unit tests, TDD will make them very
> easy for a formal QA team to add.)
>
> However, expensive setup is a design smell. That means if a test case
> requires too many lines of code for its Assemble phase (before its
> Activate and Assert phases), then maybe those lines of code support
> objects that are too coupled, and they need a better design.
>
> Throwing mocks at these objects, instead of decoupling them, will
> "perfume" the design smell, instead of curing it.
>
> > > "construction encapsulation")
> > And could you give an example.
>
>   def test_frob(self):
>       frob = Frob()
>       frob.knob = Mock()
>       frob.knob.value = Mock(return_value = 42)
>       assert 42 == frob.method_using_knob()
>
> We need the mock because we can't control how Frob's constructor built
> its knob. So instead, give Frob the option to construct with a Knob:
>
>   def test_frob(self):
>       knob = Knob(42)
>       frob = Frob(knob)
>       assert frob.method_using_knob()
>
> Note that in production the Knob constructor never takes a Knob. Maybe
> we should upgrade the production code too (!), or maybe Knob's
> constructor should only create a knob if it didn't get passed one.
> Either technique is acceptable, because the resulting code decouples
> Frobs and Knobs just a little bit more.
>
> > For me it's really hard to develop test first. Often I don't know what
> > tests to write to replace hardcoded return values by objects that
> > perform actual work.
>
> You have read too many books on TDD. C-:
>
> Alternate between writing lines of test and lines of code. Run the
> tests after the fewest possible edits, and always correctly predict if
> the tests will pass, or will fail, and with what diagnostic. (And
> configure your editor to run the stankin tests, no matter how hard it
> fights you!) The high-end tricks will get easier after you get the
> basic cycle down.
>
> --
>   Phlip
>  http://c2.com/cgi/wiki?ZeekLand

Hi Phlip!

Thanks for your exhaustive answer.
Actually, I'll investigate your example with 'frob'. From just reading
the example it's not clear for me what I will benefit, using this
approach.
And I have already refused to write totally isolated tests, because it
looks like a great waste of time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Which mock library do you prefer?

2010-02-16 Thread Phlip
On Feb 16, 9:08 am, Lacrima  wrote:

> Thanks for your exhaustive answer.
> Actually, I'll investigate your example with 'frob'. From just reading
> the example it's not clear for me what I will benefit, using this
> approach.

I can't get a good hit for "construction encapsulation" in Google.
(Although I got some good bad ones!)

This paper _almost_ gets the idea:
http://www.netobjectives.com/download/Code%20Qualities%20and%20Practices.pdf

> And I have already refused to write totally isolated tests, because it
> looks like a great waste of time.

Do you run your tests after the fewest possible edits? Such as 1-3
lines of code?

I'm not sure why the TDD books don't hammer that point down...

--
  Phlip
  http://c2.com/cgi/wiki?ZeekLand
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: plugin / intra process communication system

2010-02-16 Thread Diez B. Roggisch

Am 15.02.10 23:12, schrieb Florian Ludwig:

On Sun, 2010-02-14 at 18:47 +0100, Diez B. Roggisch wrote:

Here there problem with the trac (and other plugin systems I've

seen)

approach:

You need to define something like:
|
| class IAuthPlugin(Interface): [...]
|
in your blog software.


Why? Any reason you can't define it in a separate package the
blog-software depends on, as well as your wiki?


That's actually my point - most plugin systems I've seen, like the one
trac uses, are not encouraging you to do so. Having a module that just
defines an Interface is kind of weird - and in the real world no one is
doing it.


Just because nobody doesn't do it doesn't mean it's not desirable. IMHO 
the ones using interfaces usually immediatly implement them for some 
cases - so you actually get the interface for you authentication 
framework, and also implement an OpenID plugin for it. And this forms 
one package. Then you can add new packages that implement other things.


repoze.who and what are examples for such design.





And then of course, this is not really needed. In Python, behavior
counts, not type-information. So you can get away without any explicit
declared interface. You might chose to not do that, for aestetic
reasons, or better documentation. But you aren't forced.


Actually some plugin-systems in python do force you and they check if
your "implementation" comply with the "interface".


I didn't say nobody does it, I just said it isn't needed to get a 
working plugin system. If you don't want to have a separate package 
*and* don't want to pull in any unused code, this is pretty much your 
only option.



Here is one solution I came up with. Based on the earlier example:

 Lets say you program a wiki and like to allow different kind of
 authentications. So you create two plugins (for example one for
 OpenID and one for Shibboleth).

  Some time later you feel like reinventing the wheel again and
  you program a blog. Again you like to allow different ways of
  authentication and you already wrote plugins for exactly the
   same for your wiki, right?


auth_openid.py - providing the authentication service
http://dpaste.com/hold/159619/

wiki.py - providing the wiki
http://dpaste.com/hold/159634/

Now putting both together:


import pbus

import wiki
import auth_openid
# or: import auth_shibboleth

pbus.get("wiki").run()


No interface definitions.


What do you think? Any obvious pitfalls (besides reinventing something)?


I don't know what pbus is supposed to do. Nor how it's laid out on a 
python package level.


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


Hubris connects Ruby to Haskell, will there be such a connection between Python and Haskell?

2010-02-16 Thread Casey Hawthorne
Hubris connects Ruby to Haskell, will there be such a connection
between Python and Haskell?
--
Regards,
Casey
-- 
http://mail.python.org/mailman/listinfo/python-list


Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-16 Thread Casey Hawthorne
Interesting talk on Python vs. Ruby and how he would like Python to
have just a bit more syntactic flexibility.

http://blog.extracheese.org/2010/02/python-vs-ruby-a-battle-to-the-death.html
--
Regards,
Casey
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing for email addresses

2010-02-16 Thread galileo228
Hey all, thanks as always for the quick responses.

I actually found a very simple way to do what I needed to do. In
short, I needed to take an email which had a large number of addresses
in the 'to' field, and place just the identifiers (everything to the
left of @domain.com), in a python list.

I simply highlighted all the addresses and placed them in a text file
called emails.txt. Then I had the following code which placed each
line in the file into the list 'names':

[code]
fileHandle = open('/Users/Matt/Documents/python/results.txt','r')
names = fileHandle.readlines()
[/code]

Now, the 'names' list has values looking like this: ['aa...@domain.com
\n', 'bb...@domain.com\n', etc]. So I ran the following code:

[code]
for x in names:
st_list.append(x.replace('@domain.com\n',''))
[/code]

And that did the trick! 'Names' now has ['aaa12', 'bbb34', etc].

Obviously this only worked because all of the domain names were the
same. If they were not then based on your comments and my own
research, I would've had to use regex and the split(), which looked
massively complicated to learn.

Thanks all.

Matt

On Feb 15, 8:01 pm, Ben Finney  wrote:
> galileo228  writes:
> > I'm trying to write python code that will open a textfile and find the
> > email addresses inside it. I then want the code to take just the
> > characters to the left of the "@" symbol, and place them in a list.
>
> Email addresses can have more than one �...@’ character. In fact, the
> quoting rules allow the local-part to contain *any ASCII character* and
> remain valid.
>
> > Any suggestions would be much appeciated!
>
> For a brief but thorough treatment of parsing email addresses, see RFC
> 3696, “Application Techniques for Checking and Transformation of Names”
> http://www.ietf.org/rfc/rfc3696.txt>, specifically section 3.
>
> --
>  \          “What I have to do is see, at any rate, that I do not lend |
>   `\      myself to the wrong which I condemn.” —Henry Thoreau, _Civil |
> _o__)                                                    Disobedience_ |
> Ben Finney

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


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-16 Thread Andrej Mitrovic
On Feb 16, 7:38 pm, Casey Hawthorne 
wrote:
> Interesting talk on Python vs. Ruby and how he would like Python to
> have just a bit more syntactic flexibility.
>
> http://blog.extracheese.org/2010/02/python-vs-ruby-a-battle-to-the-de...
> --
> Regards,
> Casey

Gary's friend Geoffrey Grosenbach says in his blog post (which Gary
linked to): "Python has no comparable equivalent to Ruby’s do end
block. Python lambdas are limited to one line and can’t contain
statements (for, if, def, etc.). Which leaves me wondering, what’s the
point?"

I'm sorry, lambda's do support if's and for's. Also, lambda's are
expressions, not statements, but you can pass them around, keep them
in a dictionary if you want to. And if you need more than one line of
statements, for crying out loud use a def? And who needs those "do-
end" blocks anyway, trying to turn Python into Pascal?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a simple way to find the list index to the max value?

2010-02-16 Thread Aahz
In article <4b7a91b1.6030...@lonetwin.net>, steve   wrote:
>On 02/16/2010 05:49 PM, W. eWatson wrote:
>>
>> See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
>
>The most obvious would be a.index(max(a)). Is that what you wanted ?

The disadvantage of that is that it's O(2N) instead of O(N).
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"At Resolver we've found it useful to short-circuit any doubt and just
refer to comments in code as 'lies'. :-)"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: plugin / intra process communication system

2010-02-16 Thread Florian Ludwig
On Tue, 2010-02-16 at 16:14 +0100, Paul Kölle wrote:
> Am 15.02.2010 23:12, schrieb Florian Ludwig:
> > On Sun, 2010-02-14 at 18:47 +0100, Diez B. Roggisch wrote:
> >> [...]
> >> And then of course, this is not really needed. In Python, behavior
> >> counts, not type-information. So you can get away without any
> >> explicit
> >> declared interface. You might chose to not do that, for aestetic
> >> reasons, or better documentation. But you aren't forced.
> >
> > Actually some plugin-systems in python do force you and they check 
> > if your "implementation" comply with the "interface".

> Which is a good thing IMO ;)

Type checking might be good but not pythonic - imo. The problem with
having interfaces needed to be declared (and therefore the possibility
to check) is as I outlined in my first mail:
 Where to put those interface descriptions if you want both sides
pluggable? You probably end up depending on other modules/projects just
to import the interfaces.

> > Here is one solution I came up with.
> [...]
> Can you post working code? It's not clear what "pbus" is and how it
> works.

Oh, sorry. The idea is similar to "d-bus" - hence the name. Its "the
communcation system". The code is pretty simple and quick. Please
remember its just to show of the concept not to use it in any production
software or anything.

Instead of using classes as connecting-piece I thought using strings
(maybe path "/plugin/wiki" or "org.example.wiki") might solve my
problem. It might be possible to add optional interface validation
though.


pbus.py - just a simple concept implementation of a framework
http://dpaste.com/hold/159980/

auth_openid.py - providing the authentication service
http://dpaste.com/hold/159619/

wiki.py - providing the wiki
http://dpaste.com/hold/159634/

Now putting both together:

> import pbus
>
> import wiki
> import auth_openid
> # or: import auth_shibboleth
>
> pbus.get("wiki").run()



-- 
Florian Ludwig 


signature.asc
Description: This is a digitally signed message part
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a simple way to find the list index to the max value?

2010-02-16 Thread Tim Golden

On 16/02/2010 19:44, Aahz wrote:

In article<4b7a91b1.6030...@lonetwin.net>, steve  wrote:

On 02/16/2010 05:49 PM, W. eWatson wrote:


See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.


The most obvious would be a.index(max(a)). Is that what you wanted ?


The disadvantage of that is that it's O(2N) instead of O(N).


 Well you could do this:

a.sort ()
return -1

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


The future of "frozen" types as the number of CPU cores increases

2010-02-16 Thread John Nagle

   In the beginning, Python had some types which were "frozen", and some which
weren't.  Now, there's a trend towards having both "frozen" and "unfrozen"
versions of built-in types.  We now have "frozen" and "unfrozen" sets,
dictionaries, and byte arrays.  It's becoming clear that that the concept of 
"frozen" is separate from the type involved.


   Maybe "frozen" should be generalized, so that all "unfrozen" objects also
have "frozen" versions.

   I'd suggest

p = freeze(q)

which would generate a "frozen" copy of q.  (For lists, this would
return a tuple.  For unfrozen sets, a frozen set.  Etc.)

p = deepfreeze(q)

would generate "frozen" deep copy of q, for which each object it references
is also a "frozen" copy.

For objects,

class c(frozenobject) :
def __init__(self, someparam) :
self.p = someparam
...

would result in a class which returns frozen objects from the constructor.

   In concurrent programs, frozen objects can be shared without locking.
This simplifies locking considerably.  This may provide a way to get out
from the Global Interpreter Lock.  One possible implementation would be
to have unfrozen objects managed by reference counting and locking as
in CPython.  Frozen objects would live in a different memory space and be
garbage collected by a concurrent garbage collector.

   If we add "synchronized" objects with built-in locking, like Java,
threading becomes much cleaner.

class d(synchronized) :
...

A typical "synchronized" object would be a queue.  Anything with
state shared across threads has to be synchronized.  Only one
thread at a time can be active within a synchronized object,
due to a built-in implicit lock.

Semantics of synchronized objects:

"Synchronized" objects cannot be frozen.  Trying to freeze
them just returns the object.

If a thread blocks on an explicit "wait", the synchronized
object is temporarily unlocked during the "wait".  This
allows threads to wait for, say, a queue to get another
entry from another thread.

If only synchronized objects and frozen objects can be shared across
threads, the GIL becomes unnecessary.  Big performance win on multicore
CPUs.

   "Synchronized" was a pain in Java because Java doesn't have "frozen"
objects.  Too much effort went into synchronizing little stuff.  But
in a language with frozen objects, the little stuff would be frozen,
and wouldn't need its own locks.

   Threaded programs that already use queue objects to communicate with
each other are almost ready for this kind of architecture now.  If
the queue class made a frozen copy of any unfrozen, unsynchronized
copy placed on a queue, conversion to this approach could be almost
automatic.

   What would probably happen in practice is that potential race conditions in
existing programs would raise "unshareable object" exceptions,
indicating that something needed to be synchronized.  That's a good thing.
This approach makes threading much easier for the typical programmer.
Instead of race conditions and random errors, you get error messages.

   And we get rid of the GIL.

   I look at this as Python's answer to multicore CPUs and "Go".

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


Re: Parsing for email addresses

2010-02-16 Thread Tim Chase

galileo228 wrote:

[code]
fileHandle = open('/Users/Matt/Documents/python/results.txt','r')
names = fileHandle.readlines()
[/code]

Now, the 'names' list has values looking like this: ['aa...@domain.com
\n', 'bb...@domain.com\n', etc]. So I ran the following code:

[code]
for x in names:
st_list.append(x.replace('@domain.com\n',''))
[/code]

And that did the trick! 'Names' now has ['aaa12', 'bbb34', etc].

Obviously this only worked because all of the domain names were the
same. If they were not then based on your comments and my own
research, I would've had to use regex and the split(), which looked
massively complicated to learn.


The complexities stemmed from several factors that, with more 
details, could have made the solutions less daunting:


  (a) you mentioned "finding" the email addresses -- this makes 
it sound like there's other junk in the file that has to be 
sifted through to find "things that look like an email address". 
If the sole content of the file is lines containing only email 
addresses, then "find the email address" is a bit like [1]


  (b) you omitted the detail that the domains are all the same. 
 Even if they're not the same, (a) reduces the problem to a much 
easier task:


  s = set()
  for line in file('results.txt'):
s.add(line.rsplit('@', 1)[0].lower())
  print s

If it was previously a CSV or tab-delimited file, Python offers 
batteries-included processing to make it easy:


  import csv
  f = file('results.txt', 'rb')
  r = csv.DictReader(f)  # CSV
  # r = csv.DictReader(f, delimiter='\t') # tab delim
  s = set()
  for row in r:
s.add(row['Email'].lower())
  f.close()

or even

  f = file(...)
  r = csv.DictReader(...)
  s = set(row['Email'].lower() for row in r)
  f.close()

Hope this gives you more ideas to work with.

-tkc

[1]
http://jacksmix.files.wordpress.com/2007/05/findx.jpg



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


Re: listing existing windows services with python

2010-02-16 Thread Alf P. Steinbach

* Steve Holden:

Alf P. Steinbach wrote:
[...]

I'll do some further research to see what's going on there.

Cheers,

- Alf (is this off-topic for the group?)


It's gone a lot further off than this without anyone complaining.  I
think your experiences to date should convince you that you can rely on
being told when you drift too far off-topic :)


Some old folks' music, Eternity's Breath & Stratus; the guy playing is 65:

  http://www.youtube.com/watch?v=nMH6WH9gyCc>

Hip hoppers may skip the 80 seconds complicated intro.

It is relevant in a way, although very indirectly. For although the comment did 
not pop up here, many Satch videos (this is not Satch) have comments like "How 
on Earth can he remember all those notes?"


And he doesn't.

It's not memorization: one can't create at this level by memorizing notes or 
rules.

It's an understanding of the tool one uses and what one wants to create and the 
basic themes, and in this video it's also the synergy effect of four people 
doing that, creating together something they can't quite believe they're doing, 
so you have four madly grinning world class musicians  --  and audience... :-)



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


Re: shelve.open generates (22, 'Invalid argument') Os X 10.5 with Python 2.5

2010-02-16 Thread Ned Deily
In article 
<7a9d26a8-0a9f-4bf3-bf50-0ac5e337f...@r24g2000yqd.googlegroups.com>,
 seth  wrote:
> We have a code that creates a simple Python shelve database. We are
> able to serialize objects and store them in the dbm file. This seem to
> work the same on Windows XP Python 2.5, Ubuntu 9.1 with Python 2.6,
> but on Os X 10.5 with Python 2.5 the database filename is changed by
> the operating system by attaching the .db extension to it. Does an one
> know why is that?

It's a documented "feature": it depends on the underlying database 
package used by shelve.

http://docs.python.org/library/shelve.html

"As a side-effect, an extension may be added to the filename ..."

-- 
 Ned Deily,
 n...@acm.org

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


A fix for pylab and TkAgg plotting in SAGE (Mac OS X)

2010-02-16 Thread Lou Pecora
A few weeks ago I posted a problem with Mac OS X with matplotlib in the 
SAGE distribution in which attempting to import pylab gave an error 
message which stated that there was an import error and the module 
_tkagg could not be found. The problem appears to be with matplotlib 
(I'm not clear about this).  After much searching and some emailing I 
have found a solution to this problem and it is easy to do.  

Full credit goes to Eric Sonnendrucker who sent it to me.

Here it is:

1) Download the Tcl/Tk sources (go to http://www.tcl.tk/, click on Get 
Tcl/Tk Now, click on Sources Download Page,, download the latest 
version).

2) In the Terminal cd to the unix folder in the tcl folder.  Compile the 
unix version (of Tcl and Tk) as follows 
./configure --enable-framework --disable-xft
make
make install

3) Install SAGE from source.  Download the SAGE source code (go to 
http://www.sagemath.org/, click on Download, click on Download complete 
source code).  In the terminal cd to the SAGE folder and *before* you 
run the sage install you need to set

SAGE_MATPLOTLIB_GUI=True

using export SAGE_MATPLOTLIB_GUI=True with bash. The installer then 
picks it up automatically.

Complete the SAGE installation by typing make and hit return. That's it. 
This will take a while (up to a few hours depending on the computer).

4) You might also need to modify you matplotlibrc by setting 
backend='TkAgg'  after the installation


Test this by doing an import pylab in sage.  You should not get an error.

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


Hubris connects Ruby to Haskell, will there be such a connection between Python and Haskell?

2010-02-16 Thread geremy condra
Ok, I'll admit- my hand hovered over the 'initiate flamewar' button
for a moment before
I figured out what you were actually asking.

And I know I'm working on it, which probably means 8 or 9 others are as well.

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


Wrestling with the Py2exe Install, Win7, Py2.5

2010-02-16 Thread W. eWatson
I've finally decided to see if I could make an executable out of a py 
file. Win7. Py2.5. I brought down the install file and proceeded with 
the install. I got two warning messages. Forgot the first. The second 
said,"Could not set the key value." I again used OK. I think that was 
the only choice. It then issued a message in a larger dialog. It was 
about setting a key, and pointed me to a log. It mentions a Removepy2exe -u


Although it finished, I have no idea where the program is. It does not 
show up on the Start menu All Programs List nore my desktop. What's up?


I've had these messages (key) occur on other Python installs as I 
transition to Win7. So far no problem.


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


GIL state during import

2010-02-16 Thread Doron Tal
Is the GIL released during import statement execution when accessing the
file?
If not, is it a bug?
If it is not a bug, or it is here to stay for any other reason, I think it
should be mentioned in the documentation.

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


Shipping Executables

2010-02-16 Thread rodmc
Hi,

I have been merrily programming away in Python now for a few years and
have a couple of applications I would like to possibly publish at some
point - with the exception of certain libraries they are more or less
100% Python. However I have read elsewhere online that Python due to
it's architecture is not so good for this, especially as it is easier
for people to hack into the code. Also where software requires some
security aspects I guess it would also not be much use, is this
correct?

Anyway I would appreciate any views or tips that people have?

Kind regards,

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


Re: shelve.open generates (22, 'Invalid argument') Os X 10.5 with Python 2.5

2010-02-16 Thread seth
On Feb 14, 1:21 pm, a...@pythoncraft.com (Aahz) wrote:

> Nope -- any reason you can't change the filename?
> --

Os X 10.5 did not recognized the dbm extension.

But, I have been able to fix the problem (hope it helps somebody):
At http://docs.python.org/library/shelve.html it says:

"shelve.open(filename[, flag='c'[, protocol=None[,
writeback=False]]])¶

Open a persistent dictionary. The filename specified is the base
filename for the underlying database. As a side-effect, an extension
may be added to the filename and more than one file may be created. By
default, the underlying database file is opened for reading and
writing. "

Then, I went ahead and try to find out which type of db file was being
created:

print whichdb.whichdb(dbmFile)#prints bsddb185
kpfmac:xLPR kpf$ python
Python 2.5.1 (r251:54863, Feb  9 2009, 18:49:36)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import bsddb185
>>> bsddb185.open('realizations.db')


I found that the dbm extension is generated by bsddb and bsddb3 so I
went ahead and installed it: sudo easy_install bsddb3.
Finally, in our code, I just went ahead and instead of using
shelve.open, I am calling shelve.BsdDbShelf(bsddb3.btopen(filename,
'c'))

#realizations=shelve.open( filename, 'c', 2, writeback =
False )
#hk 021010: need to use BsdDbShelf to ensure the usage of
bsddb3 on OsX 10.5
#shelve.open creates a bsddb185 file on OsX 10.5
#but shelve.open on OsX reads with bsddb3, who knows why...
#as a result the shelve.open throws
# (22, 'Invalid argument')
dbshelf=shelve.BsdDbShelf(bsddb3.btopen(filename, 'c'))
realizations=dbshelf

And that fixed on Os X 10.5. It also works fine on Windows, by
ensuring the proper import:
try:
import bsddb3
except:
import bsddb as bsddb3


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


Re: Is there a simple way to find the list index to the max value?

2010-02-16 Thread Arnaud Delobelle
a...@pythoncraft.com (Aahz) writes:

> In article <4b7a91b1.6030...@lonetwin.net>, steve   wrote:
>>On 02/16/2010 05:49 PM, W. eWatson wrote:
>>>
>>> See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
>>
>>The most obvious would be a.index(max(a)). Is that what you wanted ?
>
> The disadvantage of that is that it's O(2N) instead of O(N).

:-)

Joke aside, even though you traverse the list twice, it may still be
quicker than other solutions because both max and list.index are C
functions and no intermediate object is constructed.

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


Re: Shipping Executables

2010-02-16 Thread geremy condra
On Tue, Feb 16, 2010 at 4:41 PM, rodmc  wrote:
> Hi,
>
> I have been merrily programming away in Python now for a few years and
> have a couple of applications I would like to possibly publish at some
> point - with the exception of certain libraries they are more or less
> 100% Python. However I have read elsewhere online that Python due to
> it's architecture is not so good for this, especially as it is easier
> for people to hack into the code.

If you mean that it is difficult to stop people from modifying the
source code, then you're correct. If you mean that python
programs are more likely to have security problems than, say,
C, I think its safe to assume that you're not correct about that.

> Also where software requires some
> security aspects I guess it would also not be much use, is this
> correct?

I've never had any reason to feel more concerned about the
code that I write in python, and I use it for crypto research.

> Anyway I would appreciate any views or tips that people have?

I'd worry about developing a product worth stealing before I
worried about people stealing it ;)

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


Re: Which mock library do you prefer?

2010-02-16 Thread Phlip
On Feb 16, 12:30 pm, Ben Finney  wrote:
> Lacrima  writes:

> > And I have already refused to write totally isolated tests, because it
> > looks like a great waste of time.
>
> It only looks like that until you chase your tail in a long, fruitless
> debugging session because (you later realise) the behaviour of one test
> is being affected by another. Test isolation is essential to ensure that
> your tests are doing what you think they're doing.

That is runtime test isolation. It's not the same thing as "unit test
isolation". Just take care in your tearDown() to scrub your
environment.

Google "Mock abuse" from here...

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


Re: The future of "frozen" types as the number of CPU cores increases

2010-02-16 Thread Terry Reedy

On 2/16/2010 3:15 PM, John Nagle wrote:

In the beginning, Python had some types which were "frozen",

> and some which weren't.

In the beginning, there was only one 'frozen' general purpose collection 
type, the tuple. And Guido resisted the suggestion that lists and tuple 
were mutable and frozen versions of each other.


However, things have changed, and lists and tuple *are* effectively 
mutable and hashable versions of each other, and we have the three other 
pairs you mentioned. The idea of freeze() may have been floated (and 
punctured) during Py3 discussions, but I think it a fairly good one.


Terry Jan Reedy

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


Re: GIL state during import

2010-02-16 Thread Terry Reedy

On 2/16/2010 4:37 PM, Doron Tal wrote:

Is the GIL released during import statement execution when accessing the
file?
If not, is it a bug?
If it is not a bug, or it is here to stay for any other reason, I think
it should be mentioned in the documentation.


The CPython GIL is a CPython implementation artifact and should not be 
mentioned in the language docs (and is not, as far as I know). Were you 
thinking of something else? And why this specific feature of its 
behavior, whatever it is.


tjr



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


ANN: obfuscate 0.2.2b

2010-02-16 Thread Steven D'Aprano
I am pleased to announce the beta release of obfuscate 0.2.2b.

http://pypi.python.org/pypi/obfuscate/

The beta release does not contain any new functionality from the previous 
version, but includes bug fixes and many new tests.



obfuscate is a pure-Python module providing classical encryption
algorithms suitable for obfuscating and unobfuscating text.

obfuscate includes the following ciphers:
 - Caesar, rot13, rot5, rot18, rot47
 - atbash
 - Playfair, Playfair6 and Playfair16
 - Railfence (encryption only)
 - Keyword
 - Affine
 - Vigenere
 - frob (xor)

and others.

DISCLAIMER: obfuscate is not cryptographically strong, and should not be
used where high security is required. (The ciphers provided in obfuscate
may have been state of the art centuries ago, but should not be used where
strong encryption is required.

obfuscate is released under the MIT licence.

Requires Python 2.5 or 2.6.

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


Re: listing existing windows services with python

2010-02-16 Thread David Bolen
alex23  writes:

> News123  wrote:
>> What is the best way with python to get a list of all windows services.
>>
>> As a start I would be glad to receive only the service names.
>>
>> However it would be nicer if I could get all the properties of a service
>> as well.
>
> I highly recommend Tim Golden's fantastic WMI module[1].

Another alternative is the win32service module from the pywin32
package (which IMO you'll almost certainly want available when doing
any significant Windows-specific operations) which wraps the native
win32 libraries for enumerating, querying and controlling services.

A simple loop could use EnumServicesStatus to iterate through the
services, OpenService with the SERVICE_QUERY_CONFIG flag to get a
handle to each service, and then QueryServiceConfig to retrieve
configuration information.

Since pywin32 is a relatively thin wrapper over the win32 libraries,
pure MSDN documentation can be used for help with the calls, augmented
by any Python-related information contained in the pywin32
documentation.

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


How to efficiently extract information from structured text file

2010-02-16 Thread Imaginationworks
Hi,

I am trying to read object information from a text file (approx.
30,000 lines) with the following format, each line corresponds to a
line in the text file.  Currently, the whole file was read into a
string list using readlines(), then use for loop to search the "= {"
and "};" to determine the Object, SubObject,and SubSubObject. My
questions are

1) Is there any efficient method that I can search the whole string
list to find the location of the tokens(such as '= {' or '};'

2) Is there any efficient ways to extract the object information you
may suggest?

Thanks,

- Jeremy



= Structured text file =
Object1 = {

...

SubObject1 = {


SubSubObject1 = {
...
};
};

SubObject2 = {


SubSubObject21 = {
...
};
};

SubObjectN = {


SubSubObjectN = {
...
};
};
};
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing for email addresses

2010-02-16 Thread galileo228
Tim -

Thanks for this.  I actually did intend to have to sift through other
junk in the file, but then figured I could just cut and paste emails
directly from the 'to' field, thus making life easier.

Also, in this particular instance, the domain names were the same, and
thus I was able to figure out my solution, but I do need to know how
to handle the same situation when the domain names are different, so
your response was most helpful.

Apologies for leaving out some details.

Matt

On Feb 16, 3:15 pm, Tim Chase  wrote:
> galileo228 wrote:
> > [code]
> > fileHandle = open('/Users/Matt/Documents/python/results.txt','r')
> > names = fileHandle.readlines()
> > [/code]
>
> > Now, the 'names' list has values looking like this: ['aa...@domain.com
> > \n', 'bb...@domain.com\n', etc]. So I ran the following code:
>
> > [code]
> > for x in names:
> >     st_list.append(x.replace('@domain.com\n',''))
> > [/code]
>
> > And that did the trick! 'Names' now has ['aaa12', 'bbb34', etc].
>
> > Obviously this only worked because all of the domain names were the
> > same. If they were not then based on your comments and my own
> > research, I would've had to use regex and the split(), which looked
> > massively complicated to learn.
>
> The complexities stemmed from several factors that, with more
> details, could have made the solutions less daunting:
>
>    (a) you mentioned "finding" the email addresses -- this makes
> it sound like there's other junk in the file that has to be
> sifted through to find "things that look like an email address".
> If the sole content of the file is lines containing only email
> addresses, then "find the email address" is a bit like [1]
>
>    (b) you omitted the detail that the domains are all the same.
>   Even if they're not the same, (a) reduces the problem to a much
> easier task:
>
>    s = set()
>    for line in file('results.txt'):
>      s.add(line.rsplit('@', 1)[0].lower())
>    print s
>
> If it was previously a CSV or tab-delimited file, Python offers
> batteries-included processing to make it easy:
>
>    import csv
>    f = file('results.txt', 'rb')
>    r = csv.DictReader(f)  # CSV
>    # r = csv.DictReader(f, delimiter='\t') # tab delim
>    s = set()
>    for row in r:
>      s.add(row['Email'].lower())
>    f.close()
>
> or even
>
>    f = file(...)
>    r = csv.DictReader(...)
>    s = set(row['Email'].lower() for row in r)
>    f.close()
>
> Hope this gives you more ideas to work with.
>
> -tkc
>
> [1]http://jacksmix.files.wordpress.com/2007/05/findx.jpg

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


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-16 Thread Jonathan Gardner
On Feb 16, 11:41 am, Andrej Mitrovic 
wrote:
> On Feb 16, 7:38 pm, Casey Hawthorne 
> wrote:
>
> > Interesting talk on Python vs. Ruby and how he would like Python to
> > have just a bit more syntactic flexibility.
>
> >http://blog.extracheese.org/2010/02/python-vs-ruby-a-battle-to-the-de...
> > --
> > Regards,
> > Casey
>
> Gary's friend Geoffrey Grosenbach says in his blog post (which Gary
> linked to): "Python has no comparable equivalent to Ruby’s do end
> block. Python lambdas are limited to one line and can’t contain
> statements (for, if, def, etc.). Which leaves me wondering, what’s the
> point?"
>
> I'm sorry, lambda's do support if's and for's. Also, lambda's are
> expressions, not statements, but you can pass them around, keep them
> in a dictionary if you want to. And if you need more than one line of
> statements, for crying out loud use a def? And who needs those "do-
> end" blocks anyway, trying to turn Python into Pascal?

I used to think anonymous functions (AKA blocks, etc...) would be a
nice feature for Python.

Then I looked at a stack trace from a different programming language
with lots of anonymous functions. (I believe it was perl.)

I became enlightened.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Shipping Executables

2010-02-16 Thread Philip Semanchuk


On Feb 16, 2010, at 4:41 PM, rodmc wrote:


Hi,

I have been merrily programming away in Python now for a few years and
have a couple of applications I would like to possibly publish at some
point - with the exception of certain libraries they are more or less
100% Python. However I have read elsewhere online that Python due to
it's architecture is not so good for this, especially as it is easier
for people to hack into the code. Also where software requires some
security aspects I guess it would also not be much use, is this
correct?



Hi Rod,
The user's ability to hack into the code is usually considered one of  
the strengths of Python & open source software in general. Since most  
Python software that's distributed  is open source, you're doing  
something different than most. It'd help if you explain how you want  
your software to differ from a typical open source distribution. Do  
you not want people to change the code? Are you worried about your  
code & ideas being stolen?




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


Wrap and intercept function calls

2010-02-16 Thread Dan Yamins
Hi:

I'm wondering what the best way to wrap and modify function calls is.
Essentially what I want to achieve is to have a function like this:

def Wrap(frame,event,arg):
 if event == 'call':
result = PerformCheck(GetArgumentsFromFrame(frame))
if Condition(result):
return result
else:
return [normal function call]

called whenever a "call" event is about to occur.

When I say "return result" I mean:  return that data object instead of what
the function would have returned, and prevent execution of the function.


Is there any way to do this using sys.settrace?  Or perhaps something from
the bdb or pbd module?

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


Re: How to efficiently extract information from structured text file

2010-02-16 Thread Rhodri James
On Tue, 16 Feb 2010 23:48:17 -, Imaginationworks   
wrote:



Hi,

I am trying to read object information from a text file (approx.
30,000 lines) with the following format, each line corresponds to a
line in the text file.  Currently, the whole file was read into a
string list using readlines(), then use for loop to search the "= {"
and "};" to determine the Object, SubObject,and SubSubObject. My
questions are

1) Is there any efficient method that I can search the whole string
list to find the location of the tokens(such as '= {' or '};'


The usual idiom is to process a line at a time, which avoids the memory  
overhead of reading the entire file in, creating the list, and so on.   
Assuming your input file is laid out as neatly as you said, that's  
straightforward to do:


for line in myfile:
if "= {" in line:
start_a_new_object(line)
elif "};" in line:
end_current_object(line)
else:
add_stuff_to_current_object(line)

You probably want more robust tests than I used there, but that depends on  
how well-defined your input file is.  If it can be edited by hand, you'll  
need to be more defensive!



2) Is there any efficient ways to extract the object information you
may suggest?


That depends on what you mean by "extract the object information".  If you  
mean "get the object name", just split the line at the "=" and strip off  
the whitespace you don't want.  If you mean "track how objects are  
connected to one another, have each object keep a list of its immediate  
sub-objects (which will have lists of their immediate sub-objects, and so  
on); it's fairly easy to keep track of which objects are current using a  
list as a stack.  If you mean something else, sorry but my crystal ball is  
cloudy tonight.


--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Download unnamed web image?

2010-02-16 Thread galileo228
All,

My python program signs onto the student facebook at my school and,
given email addresses, returns the associated full name.  If I were to
do this through a regular browser, there is also a picture of the
individual, and I am trying to get my program to download the picture
as well. The problem: the html code of the page does not point to a
particular file, but rather refers to (what seems like) a query.

So, if one went to the facebook and searched for me using my school
net id (msb83), the image of my profile on the results page is:



Using BeautifulSoup, mechanize, and urllib, I've constructed the
following:

br.open("http://www.school.edu/students/facebook/";)
br.select_form(nr = 1)

br.form['fulltextsearch'] = 'msb83' # this searches the facebook for
me
br.submit()
results = br.response().read()
soup = BeautifulSoup(results)
foo2 = soup.find('td', attrs={'width':'95'})
foo3 = foo2.find('a')
foo4 = foo3.find('img', attrs={'src':'deliverImage.cfm?netid=msb83'})
# this just drills down to the  line and   until this point the
program does not return an error

save_as = os.path.join('./', msb83 + '.jpg')
urllib.urlretrieve(foo4, save_as)

I get the following error msg after running this code:

AttributeError: 'NoneType' object has no attribute 'strip'

I can download the picture through my browser by right-clicking,
selecting save as, and then the image gets saved as
'deliverImage.cfm.jpeg.'

Are there any suggestions as to how I might be able to download the
image using python?

Please let me know if more information is needed -- happy to supply
it.

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


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-16 Thread Aahz
In article <8ca440b2-6094-4b35-80c5-81d000517...@v20g2000prb.googlegroups.com>,
Jonathan Gardner   wrote:
>
>I used to think anonymous functions (AKA blocks, etc...) would be a
>nice feature for Python.
>
>Then I looked at a stack trace from a different programming language
>with lots of anonymous functions. (I believe it was perl.)
>
>I became enlightened.

+1 QOTW
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"At Resolver we've found it useful to short-circuit any doubt and just
refer to comments in code as 'lies'. :-)"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to efficiently extract information from structured text file

2010-02-16 Thread Gary Herron

Imaginationworks wrote:

Hi,

I am trying to read object information from a text file (approx.
30,000 lines) with the following format, each line corresponds to a
line in the text file.  Currently, the whole file was read into a
string list using readlines(), then use for loop to search the "= {"
and "};" to determine the Object, SubObject,and SubSubObject. My
questions are

1) Is there any efficient method that I can search the whole string
list to find the location of the tokens(such as '= {' or '};'
  


Yes.   Read the *whole* file into a single string using file.read() 
method, and then search through the string using string methods (for 
simple things) or use re, the regular expression module, (for more 
complex searches). 

Note:  There is a point where a file becomes large enough that reading 
the whole file into memory at once (either as a single string or as a 
list of strings) is foolish.However, 30,000 lines doesn't push that 
boundary.

2) Is there any efficient ways to extract the object information you
may suggest?
  


Again, the re module has nice ways to find a pattern, and return parse 
out pieces of it.   Building a good regular expression takes time, 
experience, and a bit of black magic...To do so for this case, we 
might need more knowledge of your format.  Also regular expressions have 
their limits.  For instance, if the sub objects can nest to any level, 
then in fact, regular expressions alone can't solve the whole problem, 
and you'll need a more robust parser.




Thanks,

- Jeremy



= Structured text file =
Object1 = {

...

SubObject1 = {


SubSubObject1 = {
...
};
};

SubObject2 = {


SubSubObject21 = {
...
};
};

SubObjectN = {


SubSubObjectN = {
...
};
};
};
  


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


Re: Download unnamed web image?

2010-02-16 Thread John Bokma
galileo228  writes:

> Using BeautifulSoup, mechanize, and urllib, I've constructed the
> following:
>
> br.open("http://www.school.edu/students/facebook/";)
> br.select_form(nr = 1)
>
> br.form['fulltextsearch'] = 'msb83' # this searches the facebook for
> me
> br.submit()
> results = br.response().read()
> soup = BeautifulSoup(results)
> foo2 = soup.find('td', attrs={'width':'95'})
> foo3 = foo2.find('a')
> foo4 = foo3.find('img', attrs={'src':'deliverImage.cfm?netid=msb83'})
> # this just drills down to the  line and   until this point the
> program does not return an error
>
> save_as = os.path.join('./', msb83 + '.jpg')
> urllib.urlretrieve(foo4, save_as)
>
> I get the following error msg after running this code:
>
> AttributeError: 'NoneType' object has no attribute 'strip'

Wild guess, since you didn't provide line numbers, etc.

foo4 is None

(I also would like to suggest to use more meaningful names)

-- 
John Bokma   j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Hubris connects Ruby to Haskell, will there be such a connection between Python and Haskell?

2010-02-16 Thread Carl Banks
On Feb 16, 10:32 am, Casey Hawthorne 
wrote:
> Hubris connects Ruby to Haskell, will there be such a connection
> between Python and Haskell?

I would have expected Hubris to link Ada and Common Lisp together.


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


Re: Shipping Executables

2010-02-16 Thread Steven D'Aprano
On Tue, 16 Feb 2010 13:41:21 -0800, rodmc wrote:

> Hi,
> 
> I have been merrily programming away in Python now for a few years and
> have a couple of applications I would like to possibly publish at some
> point - with the exception of certain libraries they are more or less
> 100% Python. However I have read elsewhere online that Python due to
> it's architecture is not so good for this, especially as it is easier
> for people to hack into the code.

Looks like you are looking to apply the philosophy "No user serviceable 
parts inside".



> Also where software requires some
> security aspects I guess it would also not be much use, is this correct?

Absolutely 100% wrong. It is an fundamental principle of security that 
you must not assume that the enemy is ignorant of your procedures. 
"Security by obscurity" is not security at all.

See, for example:

http://en.wikipedia.org/wiki/Kerckhoffs'_Principle

If you are trusting that your software will be secure because people 
cannot read the source code, you have already failed. Hackers break into 
computer systems without the source code as a matter of course: allowing 
the source to be available generally makes so little difference as to be 
no difference. Worse, keeping the source code secret *as a security 
measure* lulls people into a false sense of security, letting them use 
weak security confident that since nobody knows how weak it is, it will 
be strong. That's not how it works.

If you have other reasons for wanting to keep the source code secret, 
that's one thing. But doing it because it is more secure is foolish: 
software simply isn't more secure when supplied as a binary instead of 
source code.


> Anyway I would appreciate any views or tips that people have?

Don't worry about it. If your application is secure, it will be secure 
even if everybody knows how it works. If it's not secure, then the bad 
guys will learn how it works even without the source code.



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


Re: Download unnamed web image?

2010-02-16 Thread galileo228
On Feb 16, 8:48 pm, John Bokma  wrote:
> galileo228  writes:
> > Using BeautifulSoup, mechanize, and urllib, I've constructed the
> > following:
>
> > br.open("http://www.school.edu/students/facebook/";)
> > br.select_form(nr = 1)
>
> > br.form['fulltextsearch'] = 'msb83' # this searches the facebook for
> > me
> > br.submit()
> > results = br.response().read()
> > soup = BeautifulSoup(results)
> > foo2 = soup.find('td', attrs={'width':'95'})
> > foo3 = foo2.find('a')
> > foo4 = foo3.find('img', attrs={'src':'deliverImage.cfm?netid=msb83'})
> > # this just drills down to the  line and   until this point the
> > program does not return an error
>
> > save_as = os.path.join('./', msb83 + '.jpg')
> > urllib.urlretrieve(foo4, save_as)>


> > I get the following error msg after running this code:
>
> > AttributeError: 'NoneType' object has no attribute 'strip'
>
> Wild guess, since you didn't provide line numbers, etc.
>
> foo4 is None
>
> (I also would like to suggest to use more meaningful names)
>
> --
> John Bokma                                                               j3b


I thought it was too, and I just doublechecked.  It's actually

foo3 = foo2.find('a')

that is causing the NoneType error.

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


Re: Download unnamed web image?

2010-02-16 Thread galileo228
On Feb 16, 9:40 pm, galileo228  wrote:
> On Feb 16, 8:48 pm, John Bokma  wrote:
>
>
>
> > galileo228  writes:
> > > Using BeautifulSoup, mechanize, and urllib, I've constructed the
> > > following:
>
> > > br.open("http://www.school.edu/students/facebook/";)
> > > br.select_form(nr = 1)
>
> > > br.form['fulltextsearch'] = 'msb83' # this searches the facebook for
> > > me
> > > br.submit()
> > > results = br.response().read()
> > > soup = BeautifulSoup(results)
> > > foo2 = soup.find('td', attrs={'width':'95'})
> > > foo3 = foo2.find('a')
> > > foo4 = foo3.find('img', attrs={'src':'deliverImage.cfm?netid=msb83'})
> > > # this just drills down to the  line and   until this point the
> > > program does not return an error
>
> > > save_as = os.path.join('./', msb83 + '.jpg')
> > > urllib.urlretrieve(foo4, save_as)>
> > > I get the following error msg after running this code:
>
> > > AttributeError: 'NoneType' object has no attribute 'strip'
>
> > Wild guess, since you didn't provide line numbers, etc.
>
> > foo4 is None
>
> > (I also would like to suggest to use more meaningful names)
>
> > --
> > John Bokma                                                               j3b
>
> I thought it was too, and I just doublechecked.  It's actually
>
> foo3 = foo2.find('a')
>
> that is causing the NoneType error.
>
> Thoughts?

I've now fixed the foo3 issue, and I now know that the problem is with
the urllib.urlretrieve line (see above). This is the error msg I get
in IDLE:

Traceback (most recent call last):
  File "/Users/Matt/Documents/python/dtest.py", line 59, in 
urllib.urlretrieve(foo4, save_as)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/urllib.py", line 94, in urlretrieve
return _urlopener.retrieve(url, filename, reporthook, data)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/urllib.py", line 226, in retrieve
url = unwrap(toBytes(url))
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/urllib.py", line 1033, in unwrap
url = url.strip()
TypeError: 'NoneType' object is not callable

Is this msg being generated because I'm trying to retrieve a url
that's not really a file?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interesting talk on Python vs. Ruby and how he would like Python to have just a bit more syntactic flexibility.

2010-02-16 Thread David Cournapeau
On Wed, Feb 17, 2010 at 4:41 AM, Andrej Mitrovic
 wrote:
>
> Gary's friend Geoffrey Grosenbach says in his blog post (which Gary
> linked to): "Python has no comparable equivalent to Ruby’s do end
> block. Python lambdas are limited to one line and can’t contain
> statements (for, if, def, etc.). Which leaves me wondering, what’s the
> point?"
>
> I'm sorry, lambda's do support if's and for's. Also, lambda's are
> expressions, not statements, but you can pass them around, keep them
> in a dictionary if you want to. And if you need more than one line of
> statements, for crying out loud use a def?

I think that's a bit of a strawman: the point made by the OP is that
it enables writing simple DSL easier, and the ruby's community seems
to value this. They are not advocating using anonymous functions where
"normal" functions would do.

cheers,

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


Re: Download unnamed web image?

2010-02-16 Thread Matthew Barnett

galileo228 wrote:

On Feb 16, 9:40 pm, galileo228  wrote:

On Feb 16, 8:48 pm, John Bokma  wrote:




galileo228  writes:

Using BeautifulSoup, mechanize, and urllib, I've constructed the
following:
br.open("http://www.school.edu/students/facebook/";)
br.select_form(nr = 1)
br.form['fulltextsearch'] = 'msb83' # this searches the facebook for
me
br.submit()
results = br.response().read()
soup = BeautifulSoup(results)
foo2 = soup.find('td', attrs={'width':'95'})
foo3 = foo2.find('a')
foo4 = foo3.find('img', attrs={'src':'deliverImage.cfm?netid=msb83'})
# this just drills down to the  line and   until this point the
program does not return an error
save_as = os.path.join('./', msb83 + '.jpg')
urllib.urlretrieve(foo4, save_as)>
I get the following error msg after running this code:
AttributeError: 'NoneType' object has no attribute 'strip'

Wild guess, since you didn't provide line numbers, etc.
foo4 is None
(I also would like to suggest to use more meaningful names)
--
John Bokma   j3b

I thought it was too, and I just doublechecked.  It's actually

foo3 = foo2.find('a')

that is causing the NoneType error.

Thoughts?


I've now fixed the foo3 issue, and I now know that the problem is with
the urllib.urlretrieve line (see above). This is the error msg I get
in IDLE:

Traceback (most recent call last):
  File "/Users/Matt/Documents/python/dtest.py", line 59, in 
urllib.urlretrieve(foo4, save_as)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/urllib.py", line 94, in urlretrieve
return _urlopener.retrieve(url, filename, reporthook, data)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/urllib.py", line 226, in retrieve
url = unwrap(toBytes(url))
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/urllib.py", line 1033, in unwrap
url = url.strip()
TypeError: 'NoneType' object is not callable

Is this msg being generated because I'm trying to retrieve a url
that's not really a file?


It's because the URL you're passing in, namely foo4, is None. This is
presumably because foo3.find() returns None if it can't find the entry.

You checked the value of foo3, but did you check the value of foo4?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Download unnamed web image?

2010-02-16 Thread John Bokma
galileo228  writes:

> On Feb 16, 9:40 pm, galileo228  wrote:

[...]

> I've now fixed the foo3 issue, and I now know that the problem is with
> the urllib.urlretrieve line (see above). This is the error msg I get
> in IDLE:
>
> Traceback (most recent call last):
>   File "/Users/Matt/Documents/python/dtest.py", line 59, in 
> urllib.urlretrieve(foo4, save_as)
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/urllib.py", line 94, in urlretrieve
> return _urlopener.retrieve(url, filename, reporthook, data)
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/urllib.py", line 226, in retrieve
> url = unwrap(toBytes(url))
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/urllib.py", line 1033, in unwrap
> url = url.strip()
> TypeError: 'NoneType' object is not callable
>
> Is this msg being generated because I'm trying to retrieve a url
> that's not really a file?

--8<---cut here---start->8---
>>> import urllib;
>>> urllib.urlretrieve(None)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.5/urllib.py", line 89, in urlretrieve
return _urlopener.retrieve(url, filename, reporthook, data)
  File "/usr/lib/python2.5/urllib.py", line 210, in retrieve
url = unwrap(toBytes(url))
  File "/usr/lib/python2.5/urllib.py", line 1009, in unwrap
url = url.strip()
AttributeError: 'NoneType' object has no attribute 'strip'
--8<---cut here---end--->8---

To me it looks like you're still calling urlretrieve with None as a
first value.

-- 
John Bokma   j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
-- 
http://mail.python.org/mailman/listinfo/python-list


Is there a simple way to find the list index to the max value?

2010-02-16 Thread W. eWatson

See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Which mock library do you prefer?

2010-02-16 Thread Ben Finney
Lacrima  writes:

> And I have already refused to write totally isolated tests, because it
> looks like a great waste of time.

It only looks like that until you chase your tail in a long, fruitless
debugging session because (you later realise) the behaviour of one test
is being affected by another. Test isolation is essential to ensure that
your tests are doing what you think they're doing.

-- 
 \   “A ‘No’ uttered from deepest conviction is better and greater |
  `\   than a ‘Yes’ merely uttered to please, or what is worse, to |
_o__)  avoid trouble.” —Mohandas K. Gandhi |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a simple way to find the list index to the max value?

2010-02-16 Thread TomF

On 2010-02-16 11:44:45 -0800, a...@pythoncraft.com (Aahz) said:

In article <4b7a91b1.6030...@lonetwin.net>, steve   wrote:

On 02/16/2010 05:49 PM, W. eWatson wrote:


See Subject. a = [1,4,9,3]. Find max, 9, then index to it, 2.


The most obvious would be a.index(max(a)). Is that what you wanted ?


The disadvantage of that is that it's O(2N) instead of O(N).


I don't think you understand order notation.   There's no such thing as O(2N).

To answer the original question, how about:
max(enumerate(l), key=lambda x: x[1])[0]
As to whether this is faster than index(max()), you'd have to time it.

-Tom

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


Re: The future of "frozen" types as the number of CPU cores increases

2010-02-16 Thread John Nagle

Terry Reedy wrote:

On 2/16/2010 3:15 PM, John Nagle wrote:

In the beginning, Python had some types which were "frozen",

 > and some which weren't.

In the beginning, there was only one 'frozen' general purpose collection 
type, the tuple. And Guido resisted the suggestion that lists and tuple 
were mutable and frozen versions of each other.


However, things have changed, and lists and tuple *are* effectively 
mutable and hashable versions of each other, and we have the three other 
pairs you mentioned. The idea of freeze() may have been floated (and 
punctured) during Py3 discussions, but I think it a fairly good one.


   Yes, we're now at the point where all the built-in mutable types
have "frozen" versions.  But we don't have that for objects.  It's
generally considered a good thing in language design to offer,
for user defined types, most of the functionality of built-in ones.

   It's the concurrency aspect of this that interests me, though.
A language with immutable objects can potentially handle concurrency
more safely than one where everything is potentially mutable.
The language knows what can't change, which simplifies locking.

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


Re: The future of "frozen" types as the number of CPU cores increases

2010-02-16 Thread Paul Rubin
John Nagle  writes:
>> However, things have changed, and lists and tuple *are* effectively
>> mutable and hashable versions of each other...
>It's the concurrency aspect of this that interests me, though.
> A language with immutable objects can potentially handle concurrency
> more safely than one where everything is potentially mutable.
> The language knows what can't change, which simplifies locking.

I wonder how well that applies to tuples containing mutable objects,
e.g.  t = ([1,2], [3,4])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Shipping Executables

2010-02-16 Thread Banibrata Dutta
On Wed, Feb 17, 2010 at 7:41 AM, Steven D'Aprano <
ste...@remove.this.cybersource.com.au> wrote:

> > security aspects I guess it would also not be much use, is this correct?
>
> Absolutely 100% wrong. It is an fundamental principle of security that
> you must not assume that the enemy is ignorant of your procedures.
> "Security by obscurity" is not security at all.
>
> See, for example:
>
> http://en.wikipedia.org/wiki/Kerckhoffs'_Principle
>
> I believe, the use of work 'security' wasn't the best choice to describe
the need, if I understand the original poster's intentions. The intentions
of original poster were "intellectual property protection", where-in they
have indeed created something worth stealing, and would like to put it under
lock-n-key. For that purpose, I do not think Python is the right choice.

BTW for people who are non-believers in something being worth stealing
needing protection, need to read about the Skype client.

-- 
regards,
Banibrata
http://www.linkedin.com/in/bdutta
http://twitter.com/edgeliving
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The future of "frozen" types as the number of CPU cores increases

2010-02-16 Thread Alf P. Steinbach

* Paul Rubin:

John Nagle  writes:

However, things have changed, and lists and tuple *are* effectively
mutable and hashable versions of each other...

   It's the concurrency aspect of this that interests me, though.
A language with immutable objects can potentially handle concurrency
more safely than one where everything is potentially mutable.
The language knows what can't change, which simplifies locking.


I wonder how well that applies to tuples containing mutable objects,
e.g.  t = ([1,2], [3,4])


One would need a collection type that's immutable "all the way".

Current frozen types don't have that.

However, there's also the question of the immutability of what names refer to. 
It seems to me that this means that the compiler can't really assume very much 
without very deep analysis. And so, if it boils down to the programmer applying 
assumptions about mutability/constantness, then types may not be The Answer.



Cheers,

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


Re: Shipping Executables

2010-02-16 Thread geremy condra
On Wed, Feb 17, 2010 at 1:10 AM, Banibrata Dutta
 wrote:
>
>
> On Wed, Feb 17, 2010 at 7:41 AM, Steven D'Aprano
>  wrote:
>>
>> > security aspects I guess it would also not be much use, is this correct?
>> Absolutely 100% wrong. It is an fundamental principle of security that
>> you must not assume that the enemy is ignorant of your procedures.
>> "Security by obscurity" is not security at all.
>>
>> See, for example:
>>
>> http://en.wikipedia.org/wiki/Kerckhoffs'_Principle
>>
> I believe, the use of work 'security' wasn't the best choice to describe the
> need, if I understand the original poster's intentions. The intentions of
> original poster were "intellectual property protection",

Which has little to do with the language in question.

> where-in they have
> indeed created something worth stealing, and would like to put it under
> lock-n-key. For that purpose, I do not think Python is the right choice.

Why?

> BTW for people who are non-believers in something being worth stealing
> needing protection, need to read about the Skype client.

Most of the people I know who were interested in REing skype were
a lot more interested in either interoperating with the protocol or ensuring
that skype wasn't deliberately including malware or a backdoor. In any
even I don't see this having anything to do with Python.

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


Re: Shipping Executables

2010-02-16 Thread Steve Holden
geremy condra wrote:
[...]
> I'd worry about developing a product worth stealing before I
> worried about people stealing it ;)
> 
> Geremy Condra

+1 FAQ entry!

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

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


Re: The future of "frozen" types as the number of CPU cores increases

2010-02-16 Thread Steven D'Aprano
On Tue, 16 Feb 2010 21:09:27 -0800, John Nagle wrote:

> Yes, we're now at the point where all the built-in mutable types
> have "frozen" versions.  But we don't have that for objects.  It's
> generally considered a good thing in language design to offer, for user
> defined types, most of the functionality of built-in ones.


It's not hard to build immutable user-defined types. Whether they're 
immutable enough is another story :)



>>> class FrozenMeta(type):
... def __new__(meta, classname, bases, classDict):
... def __setattr__(*args):
... raise TypeError("can't change immutable class")
... classDict['__setattr__'] = __setattr__
... classDict['__delattr__'] = __setattr__
... return type.__new__(meta, classname, bases, classDict)
...
>>>
>>> class Thingy(object):
... __metaclass__ = FrozenMeta
... def __init__(self, x):
... self.__dict__['x'] = x
...
>>>
>>>
>>> t = Thingy(45)
>>> t.x
45
>>> t.x = 42
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 4, in __setattr__
TypeError: can't change immutable class


It's a bit ad hoc, but it seems to work for me. Unfortunately there's no 
way to change __dict__ to a "write once, read many" dict.





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


Re: Shipping Executables

2010-02-16 Thread Steven D'Aprano
On Wed, 17 Feb 2010 02:00:59 -0500, geremy condra quoted Banibrata Dutta
:

>> BTW for people who are non-believers in something being worth stealing
>> needing protection, need to read about the Skype client.

Pardon me for breaking threading, but the original post has not come 
through to my provider, only the reply from Geremy.

Many things are worth stealing and therefore need protection.

In any case, reverse engineering software is not theft. And even if it 
were, keeping the source code secret is no barrier to a competent, 
determined attacker or investigator. Skype is a good example: despite the 
lack of source code and the secret protocol, analysts were able to 
discover that TOM-Skype sends personally identifiable information, 
encryption keys and private messages back to central servers.

In my personal opinion, releasing closed source software is prima facie 
evidence that the software is or does something bad: leaking personal 
information, infringing somebody else's copyright or patent, or just 
being badly written. I'm not saying that every piece of closed source 
software is like that, but when you hide the source, the burden of proof 
is on you to prove that you're not hiding something unpleasant.



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


  1   2   >