Re: Error importing __init__ declared variable from another package

2012-02-28 Thread Jason Veldicott
Accidentally hit post by mistake before msg completed.

Any comments appreciated.  It's a very simple scenario, but not sure what
the mistake is.

Thanks

Jason


On Tue, Feb 28, 2012 at 6:55 PM, Jason Veldicott
wrote:

> Hi,
>
> I have a simple configuration of modules as beneath, but an import error
> is reported:
>
> /engine
>(__init__ is empty here)
>engine.py
> /sim
>__init__.py
>
>
> The module engine.py imports a variable instantiated in sim.__init__ as
> follows:
>
>from sim import var_name
>var_name.func()
>
> The following error messaged is received on the func() call above (Eclipse
> PyDev):
>
> "undefined variable from import: func"
>
> Any idea why this is causing an error?
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Error importing __init__ declared variable from another package

2012-02-28 Thread Peter Otten
Jason Veldicott wrote:

> Hi,
> 
> I have a simple configuration of modules as beneath, but an import error
> is reported:
> 
> /engine
>(__init__ is empty here)
>engine.py
> /sim
>__init__.py
> 
> 
> The module engine.py imports a variable instantiated in sim.__init__ as
> follows:
> 
>from sim import var_name
>var_name.func()
> 
> The following error messaged is received on the func() call above (Eclipse
> PyDev):
> 
> "undefined variable from import: func"

Are you rephrasing or is this really the error message? If so run your 
program again on the command-line. Then please cut and paste the error 
message together with the traceback.

> Any idea why this is causing an error?

What version of Python are you using?
What does sim/__init__.py contain?

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


check if directory is writable in a portable way

2012-02-28 Thread Andrea Crotti

How should I check if I can create files in a directory?

I tried to simply check if the directory is writeable with this function:

def is_writable(name):
"""Return true if the file is writable from the current user
"""
return os.access(name, os.W_OK)

but that doesn't work at all on Windows, because for example if I create 
a new directory
and do os.access(dirpath, os.W_OK) it returns false, even if I can 
create files inside without problems.


So maybe the only solution that works is something like
try:
open(path.join('temp', 'w'))
except OsError:
return False
else:
os.remove(path.join('temp'))
return True

would it make sense?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python urllib2 problem: Name or service not known

2012-02-28 Thread Alex Borghgraef
On Feb 28, 1:36 am, Steven D'Aprano  wrote:
> On Mon, 27 Feb 2012 12:48:27 -0800, Alex Borghgraef wrote:
> > Hi all,
>
> > Some time ago I've written some python code to read video data off an IP
> > camera connected via a router to a laptop. Now I try to run this code on
> > a different laptop and router combination, but now I can't access the
> > camera.
>
> [...]
>
> Check your web proxy and firewall.

Hmm. Damn. I had disabled the proxy in .bashrc, but apparently the one
in /etc/bash.bashrc was still enabled. Thanks.

I'll still have to find out a way to get this thing working with proxy
enabled if I ever want to connect it to our overall network.

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


Re: Python urllib2 problem: Name or service not known

2012-02-28 Thread Alex Borghgraef
On Feb 28, 10:50 am, Alex Borghgraef 
wrote:

> I'll still have to find out a way to get this thing working with proxy
> enabled if I ever want to connect it to our overall network.

Ok, doing os.environ['http_proxy']='' before importing urllib2 seems
to do the trick for that.

--
Alex


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


Re: check if directory is writable in a portable way

2012-02-28 Thread Tim Golden

On 28/02/2012 10:07, Andrea Crotti wrote:

How should I check if I can create files in a directory?

I tried to simply check if the directory is writeable with this function:

def is_writable(name):
"""Return true if the file is writable from the current user
"""
return os.access(name, os.W_OK)

but that doesn't work at all on Windows, because for example if I create
a new directory
and do os.access(dirpath, os.W_OK) it returns false, even if I can
create files inside without problems.

So maybe the only solution that works is something like
try:
open(path.join('temp', 'w'))
except OsError:
return False
else:
os.remove(path.join('temp'))
return True

would it make sense?



This is remarkably complicated to do on Windows by
checking Security APIs etc. If the try:except dance
works for you, I recommend that you use it. (In the
past, people who have asked this question have not
wanted to use try-except because they didn't want the
overhead of creating even a zero-length file)

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


Re: namespace question

2012-02-28 Thread Ben Finney
Steven D'Aprano  writes:

> On Sun, 26 Feb 2012 19:47:49 +1100, Ben Finney wrote:
>
> >> An integer variable is a variable holding an integer. A string variable
> >> is a variable holding a string. A list variable is a variable holding a
> >> list.
> > 
> > And Python has none of those. Its references don't “hold” anything.
>
> Ah, but they do. Following the name binding:
>
> x = 1
>
> the name "x" now holds a reference to an int, or if you want to cut out 
> one layer of indirection, the name "x" holds an int. That is to say, the 
> value associated with the name "x" is an int.

Names don't hold anything. Not in natural language, and not in Python.

Which is exactly *why* the term “name” is a good one, since Python's
bindings don't hold anything either. It's a reference, not a container.

> I don't believe this is a troublesome concept.

Then you have a different concept of “name” from anything I'd expect
anyone to understand.

> > I appreciate that you think “variable” is a useful term in Python,
> > but this kind of mangling of the concept convinces me that it's not
> > worth it.
>
> I'm not sure that there is any mangling here. Or at least, the concept
> is only mangled if you believe that Pascal- or C-like variables (named
> memory locations) are the one true definition of "variable". I do not
> believe this.

The fact that you keep having to come back to container analogies, when
that's exactly what Python doesn't have and what differentiates it, is
why I think the term “variable” isn't helping the discussion. Not for
us, and not for newcomers to the language.

> Words vary in their meanings

Of course they do. but we don't have to let any word mean any arbitrary
thing. I reject attempts to Humpty Dumpty our way through discussions
with newcomers about Python concepts.

-- 
 \ “Simplicity and elegance are unpopular because they require |
  `\   hard work and discipline to achieve and education to be |
_o__)appreciated.” —Edsger W. Dijkstra |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: check if directory is writable in a portable way

2012-02-28 Thread Andrea Crotti

On 02/28/2012 11:34 AM, Tim Chase wrote:

On 02/28/12 04:07, Andrea Crotti wrote:

How should I check if I can create files in a directory?

So maybe the only solution that works is something like
try:
  open(path.join('temp', 'w'))
except OsError:
  return False
else:
  os.remove(path.join('temp'))
  return True


It depends on the system & location.  It's possible to set up 
directories with permissions that allow you to create files but not 
delete them, in which case you'd either (1) create the file and 
possibly fail on subsequent tests because the file already exists; or 
(2) litter the directory with tmpnam()-like results that you were 
unable to delete.


It's ugly, I've encountered it, and haven't found a good universal 
solution myself.


-tkc



That's really ugly right, didn't think about this possibility.
Well it's not a really critical part of my code, so I guess it's fine 
with the try-except dance..


But isn't there (or should there be) a windows-related library that 
abstracts this horrible things?


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


Re: check if directory is writable in a portable way

2012-02-28 Thread Tim Chase

On 02/28/12 04:07, Andrea Crotti wrote:

How should I check if I can create files in a directory?

So maybe the only solution that works is something like
try:
  open(path.join('temp', 'w'))
except OsError:
  return False
else:
  os.remove(path.join('temp'))
  return True


It depends on the system & location.  It's possible to set up 
directories with permissions that allow you to create files but 
not delete them, in which case you'd either (1) create the file 
and possibly fail on subsequent tests because the file already 
exists; or (2) litter the directory with tmpnam()-like results 
that you were unable to delete.


It's ugly, I've encountered it, and haven't found a good 
universal solution myself.


-tkc




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


Re: check if directory is writable in a portable way

2012-02-28 Thread Tim Chase

On 02/28/12 06:01, Andrea Crotti wrote:

How should I check if I can create files in a directory?


But isn't there (or should there be) a windows-related library that
abstracts this horrible things?


Yes, there should be. There isn't as far as I know (though that 
doesn't mean much given my limited experiences in the recesses of 
Win32 APIs).


Additionally, even if you did a LBYL instead, you'd open yourself 
to a race-condition where the permissions could theoretically 
change between the time you check and the time you actually try 
to write there.  Granted, that's a slim chance and usually would 
be a "Doctor, my foot hurts when I do $THIS"/"Well don't do that" 
situation where the solution is "don't change the permissions 
while running this program".


-tkc



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


pickling to an output file.

2012-02-28 Thread Smiley 4321
I have created a bytestream (readfile.pkl) from pickle.dump() already in
different folder (say /tmp) succesfully. Now I wish to open the file, read
it and finally write the output to a file to "output.txt".

To read the file bytestream file (readfile.pkl) I did perform as --

---
import pickle

def print_report():
with open('/tmp/readfile.pkl', 'rb') as f:
my_shared = pickle.load(f)
print my_shared
print ""

--

My above code should perform all 3 of below -

(a) Open the pickle file as above
(b) read the pickled object
(c) write the output to a file say "output.txt"

Can I have know if my above code is reading properly and how to write to an
output file 'output.txt'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about sub-packages

2012-02-28 Thread Frank Millman

"Frank Millman"  wrote in message 
news:[email protected]...
> Hi all
>
> This is a follow-up to my recent question about circular imports, but on a 
> different subject, hence the new thread.
>
[...]
>
> If this makes sense, my next thought was, where is the best place to put 
> this API. Then I thought, why not put it in the __init__.py of the 
> sub-package? Then all that the users of the package have to do is import 
> the package, and then place calls on it directly.
>
> I did a quick test and it seems to work. Is this a good idea, or are there 
> any downsides?
>

Answering my own question again ...

The one-liner API concept *may* be a good idea - still waiting for some 
feedback on that.

But putting them into __init__.py is not a good idea, as I run into some 
subtle 'circular import' problems again. I don't fully understand the 
conditions under which it fails, but that is unimportant, as my objective is 
to avoid circular imports altogether.

I have created a module called 'api.py' and put them in there, and that 
seems to work (for now...).

Frank



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


Re: check if directory is writable in a portable way

2012-02-28 Thread Tim Golden

On 28/02/2012 12:01, Andrea Crotti wrote:

On 02/28/2012 11:34 AM, Tim Chase wrote:

On 02/28/12 04:07, Andrea Crotti wrote:

How should I check if I can create files in a directory?

So maybe the only solution that works is something like
try:
open(path.join('temp', 'w'))
except OsError:
return False
else:
os.remove(path.join('temp'))
return True


It depends on the system & location. It's possible to set up
directories with permissions that allow you to create files but not
delete them, in which case you'd either (1) create the file and
possibly fail on subsequent tests because the file already exists; or
(2) litter the directory with tmpnam()-like results that you were
unable to delete.

It's ugly, I've encountered it, and haven't found a good universal
solution myself.

-tkc



That's really ugly right, didn't think about this possibility.
Well it's not a really critical part of my code, so I guess it's fine
with the try-except dance..

But isn't there (or should there be) a windows-related library that
abstracts this horrible things?


Well I maintain winsys [1] which does help out in admin-y tasks
in general on Windows, but I'm afraid I've never had the need
to scratch this particular itch, and the API solution is messier
than you might think, and more fiddly to get right. If I ever
get enough moments together I might do it, but patches are
always looked upon kindly :)

TJG

[1] https://github.com/tjguk/winsys
--
http://mail.python.org/mailman/listinfo/python-list


Re: namespace question

2012-02-28 Thread Steven D'Aprano
On Tue, 28 Feb 2012 22:36:56 +1100, Ben Finney wrote:

> Steven D'Aprano  writes:
> 
>> On Sun, 26 Feb 2012 19:47:49 +1100, Ben Finney wrote:
>>
>> >> An integer variable is a variable holding an integer. A string
>> >> variable is a variable holding a string. A list variable is a
>> >> variable holding a list.
>> > 
>> > And Python has none of those. Its references don't “hold” anything.
>>
>> Ah, but they do. Following the name binding:
>>
>> x = 1
>>
>> the name "x" now holds a reference to an int, or if you want to cut out
>> one layer of indirection, the name "x" holds an int. That is to say,
>> the value associated with the name "x" is an int.
> 
> Names don't hold anything. Not in natural language, and not in Python.

I don't agree, I think the analogy of "holding on to", or "holding", 
works well for names in this context. A *binding* (the term you prefer) 
refers to the process of *tying* two objects together with rope, string, 
or twine such that they *hold fast*. If names don't hold values, neither 
can they be bound.

I would be surprised if many programmers, whether experienced or not, or 
even non-programmers, failed to grasp the concept of a name "holding" a 
value. (And I point out that to *grasp a concept* is also to hold on to 
it. We hold our loved ones dear even when they are on the other side of 
the world, we hold these truths to be self-evident, and we don't hold 
with that sort of behaviour -- surely we are capable of holding onto the 
idea that names can hold values?)

But rather than spend any more time trying to convince you to hold a 
different opinion, I will accept your criticism and rephrase my earlier 
statement:

A string variable is a symbolic identifier given to a value and capable 
of being varied, which is given to a value which is a string.

Python has these -- it has names, which are symbolic identifiers given to 
values, and those name:value bindings are capable of varying. It has 
strings. And you can bind names to strings. Ergo, it has string variables.

I acknowledge that when I say "string variable", I mean a variable whose 
value is a string *at this moment*, I don't mean a variable which is 
prohibited by the compiler from taking a non-string value.

To the extent that careless use of the term variable may confuse the 
naive reader who assumes that Python is just like Pascal (or any other 
statically typed language), it is sometimes useful to emphasis the 
differences by talking about "name bindings". But sometimes it is useful 
to emphasis the similarities, in which case "name binding" is obfuscatory 
and "variable" is perfectly reasonable.



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


xlrd 0.7.3 released!

2012-02-28 Thread Chris Withers

Hi All,

I'm pleased to announce the release of xlrd 0.7.3.

This release just brings in some documentation updates that were missed 
for 0.7.2.


cheers,

Chris

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


suppressing argparse arguments in the help

2012-02-28 Thread Andrea Crotti
I have a script that might be used interactively but also has some 
arguments that

should not be used by "normal" users.
So I just want to suppress them from the help.
I've read somewhere that the help=SUPPRESS should do what I want:

parser.add_argument('-n', '--test_only',
action='store_true',
help=SUPPRESS)

but that's what I get from "myapp -h", which is not exactly what I was 
looking for..



  -f, --first_level ==SUPPRESS== (default: False)
  --never_redevelop ==SUPPRESS== (default: False)


Any other solutions?
--
http://mail.python.org/mailman/listinfo/python-list


Pickle performing class instantiation ??

2012-02-28 Thread Smiley 4321
I am looking for pickle performing class instantiation, something as
prototype like -

- Have a class
- Instantiate the class with 3 parameters
- pickle the class instance
- generate a bytestream (.pkl) using pickle.dump
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pickle performing class instantiation ??

2012-02-28 Thread Smiley 4321
Am I doing the right thing for -

- Have a class
- Instantiate the class with 3 parameters
- pickle the class instance
- generate a bytestream (.pkl) using pickle.dump, simply guessing -

as -

---
#!/usr/bin/python

import pickle

class Pickle:
def __init__(self, Parameter1, Parameter2, Parameter3):
self.PM1 = Parameter1
self.PM2 = Parameter2
self.PM3 = Parameter3

with open('/tmp/readfile.pkl', 'wb') as f:
pickle.dump((self.PM1, self.PM2, self.PM3), f)

with open('/tmp/readfile.pkl', 'rb') as f:
self.PM1, self.PM2, self.PM3 = pickle.load(f)

print self.PM1
print self.PM2
print self.PM3


but I get an error message as -

-
$ ./class-test1.py
Traceback (most recent call last):
  File "./class-test1.py", line 12, in 
pickle.dump((self.PM1, self.PM2, self.PM3), f)
NameError: name 'self' is not defined
--

On Tue, Feb 28, 2012 at 8:44 PM, Smiley 4321  wrote:

> I am looking for pickle performing class instantiation, something as
> prototype like -
>
> - Have a class
> - Instantiate the class with 3 parameters
> - pickle the class instance
> - generate a bytestream (.pkl) using pickle.dump
>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: suppressing argparse arguments in the help

2012-02-28 Thread Peter Otten
Andrea Crotti wrote:

> I have a script that might be used interactively but also has some
> arguments that
> should not be used by "normal" users.
> So I just want to suppress them from the help.
> I've read somewhere that the help=SUPPRESS should do what I want:
> 
>  parser.add_argument('-n', '--test_only',
>  action='store_true',
>  help=SUPPRESS)
> 
> but that's what I get from "myapp -h", which is not exactly what I was
> looking for..
> 
> 
>-f, --first_level ==SUPPRESS== (default: False)
>--never_redevelop ==SUPPRESS== (default: False)
> 
> 
> Any other solutions?

That shouldn't happen. Did you reload() somewhere?
argparse tests object identity not equality with SUPPRESS, so you have to 
ensure that SUPPRESS stems from the same instance of the argparse module as 
your ArgumentParser.

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


RE: Pickle performing class instantiation ??

2012-02-28 Thread Prasad, Ramit
>class Pickle:
>    def __init__(self, Parameter1, Parameter2, Parameter3):
>        self.PM1 = Parameter1
>        self.PM2 = Parameter2
>        self.PM3 = Parameter3

>with open('/tmp/readfile.pkl', 'wb') as f:
>    pickle.dump((self.PM1, self.PM2, self.PM3), f)
    
>with open('/tmp/readfile.pkl', 'rb') as f:
>    self.PM1, self.PM2, self.PM3 = pickle.load(f)

>$ ./class-test1.py
>Traceback (most recent call last):
>  File "./class-test1.py", line 12, in 
>    pickle.dump((self.PM1, self.PM2, self.PM3), f)
>NameError: name 'self' is not defined


You need to create an instance of Pickle first and then manipulate that 
instance.
instance = Pickle( 'something', ['blah'],  5 )
print instance.PM1

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pickle performing class instantiation ??

2012-02-28 Thread Peter Otten
Smiley 4321 wrote:

> Am I doing the right thing for -
> 
> - Have a class
> - Instantiate the class with 3 parameters
> - pickle the class instance
> - generate a bytestream (.pkl) using pickle.dump, simply guessing -
> 
> as -
> 
> ---
> #!/usr/bin/python
> 
> import pickle
> 
> class Pickle:
> def __init__(self, Parameter1, Parameter2, Parameter3):
> self.PM1 = Parameter1
> self.PM2 = Parameter2
> self.PM3 = Parameter3

# You need something to pickle first.
# You could call it "self", but that is confusing
# as this name is by convention used inside methods as
# the current instance. So:
p = Pickle(1, 2, 3)

> with open('/tmp/readfile.pkl', 'wb') as f:
  # Don't touch the object's attributes, 
  # pickle the whole instance instead:
  pickle.dump(p, f)

> with open('/tmp/readfile.pkl', 'rb') as f:
  # Load the instance back from file
  p = pickle.load(f)

# print it 
print p.PM1
print p.PM2
print p.PM3


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


Re: Pickle performing class instantiation ??

2012-02-28 Thread Andrew Berg
On 2/28/2012 9:54 AM, Smiley 4321 wrote:
> NameError: name 'self' is not defined
self is meaningless outside a class definition. You should refactor your
dump, load and print code as methods inside the class definition, create
an instance and call the methods on that instance.

-- 
CPython 3.2.2 | Windows NT 6.1.7601.17640
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: suppressing argparse arguments in the help

2012-02-28 Thread Andrea Crotti

On 02/28/2012 04:02 PM, Peter Otten wrote:

Andrea Crotti wrote:


I have a script that might be used interactively but also has some
arguments that
should not be used by "normal" users.
So I just want to suppress them from the help.
I've read somewhere that the help=SUPPRESS should do what I want:

  parser.add_argument('-n', '--test_only',
  action='store_true',
  help=SUPPRESS)

but that's what I get from "myapp -h", which is not exactly what I was
looking for..


-f, --first_level ==SUPPRESS== (default: False)
--never_redevelop ==SUPPRESS== (default: False)


Any other solutions?

That shouldn't happen. Did you reload() somewhere?
argparse tests object identity not equality with SUPPRESS, so you have to
ensure that SUPPRESS stems from the same instance of the argparse module as
your ArgumentParser.



Ah great yes it wasn't actually the same..
but why not just use
if text != SUPPRESS
instead of:
if text is not SUPPRESS

probably the second is more safe, but they are it's still checking 
against a constant that

is very unlikely to clash with anything..
--
http://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing, what am I doing wrong?

2012-02-28 Thread Eric Frederich
If I do a time.sleep(0.001) right at the beginning of the run() method,
then it completes fine.
I was able to run it through a couple hundred times without problem.
If I sleep for less time than that or not at all, it may or may not
complete.

On Mon, Feb 27, 2012 at 9:38 PM, MRAB  wrote:

> On 27/02/2012 16:57, Eric Frederich wrote:
>
>> Still freezing sometimes, like 1 out of 10 times that I run it.
>> Here is updated code and a couple of outputs.
>>
>>  [snip]
> I don't know what the problem is. All I can suggest is a slightly
> modified version.
>
> If a worker that says it's terminating without first saying that it's
> got nothing, then I can only assume that the worker had some uncaught
> exception.
>
>
>
> #!/usr/bin/env python
>
> import sys
> import Queue
> import multiprocessing
> import time
>
> def FOO(a, b, c):
>print 'foo', a, b, c
>return (a + b) * c
>
> class MyWorker(multiprocessing.**Process):
>def __init__(self, name, inbox, outbox):
>super(MyWorker, self).__init__()
>self.name = name
>self.inbox = inbox
>self.outbox = outbox
>print >> sys.stderr, 'Created %s' % self.name; sys.stderr.flush()
>def run(self):
>print >> sys.stderr, 'Running %s' % self.name; sys.stderr.flush()
>try:
>
>while True:
>try:
>args = self.inbox.get_nowait()
>print >> sys.stderr, '%s got something to do' %
> self.name; sys.stderr.flush()
>except Queue.Empty:
>print >> sys.stderr, '%s got nothing' % self.name;
> sys.stderr.flush()
>break
>self.outbox.put(FOO(*args))
>finally:
>print >> sys.stderr, '%s is terminating' % self.name;
> sys.stderr.flush()
>
>
> if __name__ == '__main__':
># This file is being run as the main script. This part won't be
># run if the file is imported.
>
>print >> sys.stderr, 'Creating todo queue'; sys.stderr.flush()
>todo = multiprocessing.Queue()
>
>for i in xrange(100):
>todo.put((i, i + 1, i + 2))
>
>print >> sys.stderr, 'Creating results queue'; sys.stderr.flush()
>result_queue = multiprocessing.Queue()
>
>print >> sys.stderr, 'Creating Workers'; sys.stderr.flush()
>w1 = MyWorker('Worker 1', todo, result_queue)
>w2 = MyWorker('Worker 2', todo, result_queue)
>
>print >> sys.stderr, 'Starting Worker 1'; sys.stderr.flush()
>w1.start()
>print >> sys.stderr, 'Starting Worker 2'; sys.stderr.flush()
>w2.start()
>
>for i in xrange(100):
>print result_queue.get()
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Need to write python source with python

2012-02-28 Thread crstop
Hi All,

I'm new to Python but have experience with a few other programming 
languages(Java, Perl, JavaScript). 

I'm using Python 2.7.2 and I'm trying to create and write to a file (.py) a 
python class and functions from python. I will also need to later read and edit 
the file. I realize I could just write strings using the available string and 
file writing methods but suspect there is a better way than that. 

I have read about pickle, ast, and Django; searched this group and the web but 
haven't found a solution that seems to fit. Any suggestion? 
-- 
http://mail.python.org/mailman/listinfo/python-list


GUI for pickle read

2012-02-28 Thread Smiley 4321
Can I have some thoughts about - building a GUI to display the results of
the pickle read?

A prototype code should be fine on Linux.

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


Re: Need to write python source with python

2012-02-28 Thread Peter Otten
[email protected] wrote:

> I'm new to Python but have experience with a few other programming
> languages(Java, Perl, JavaScript).
> 
> I'm using Python 2.7.2 and I'm trying to create and write to a file (.py)
> a python class and functions from python. I will also need to later read
> and edit the file. I realize I could just write strings using the
> available string and file writing methods but suspect there is a better
> way than that.
> 
> I have read about pickle, ast, and Django; searched this group and the web
> but haven't found a solution that seems to fit. Any suggestion?

Due to Python's dynamic nature it is rarely necessary to generate Python 
code. What are you actually trying to achieve?

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


Re: Need to write python source with python

2012-02-28 Thread 88888 Dihedral
在 2012年2月29日星期三UTC+8上午1时56分43秒,Peter Otten写道:
> [email protected] wrote:
> 
> > I'm new to Python but have experience with a few other programming
> > languages(Java, Perl, JavaScript).
> > 
> > I'm using Python 2.7.2 and I'm trying to create and write to a file (.py)
> > a python class and functions from python. I will also need to later read
> > and edit the file. I realize I could just write strings using the
> > available string and file writing methods but suspect there is a better
> > way than that.
> > 
> > I have read about pickle, ast, and Django; searched this group and the web
> > but haven't found a solution that seems to fit. Any suggestion?
> 
> Due to Python's dynamic nature it is rarely necessary to generate Python 
> code. What are you actually trying to achieve?

Check myHDL, and BOA and pythoncard that can  translate user messages
to pyhton code as delphie.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multiprocessing, what am I doing wrong?

2012-02-28 Thread MRAB

On 28/02/2012 17:16, Eric Frederich wrote:

If I do a time.sleep(0.001) right at the beginning of the run() method,
then it completes fine.
I was able to run it through a couple hundred times without problem.
If I sleep for less time than that or not at all, it may or may not
complete.


[snip]

To me that suggests that the OS works in units of 1 millisecond, so a
sleep of less than 0.001 seconds is rounded to 0 milliseconds.
--
http://mail.python.org/mailman/listinfo/python-list


Re: suppressing argparse arguments in the help

2012-02-28 Thread Peter Otten
Andrea Crotti wrote:

> On 02/28/2012 04:02 PM, Peter Otten wrote:
>> Andrea Crotti wrote:
>>
>>> I have a script that might be used interactively but also has some
>>> arguments that
>>> should not be used by "normal" users.
>>> So I just want to suppress them from the help.
>>> I've read somewhere that the help=SUPPRESS should do what I want:
>>>
>>>   parser.add_argument('-n', '--test_only',
>>>   action='store_true',
>>>   help=SUPPRESS)
>>>
>>> but that's what I get from "myapp -h", which is not exactly what I was
>>> looking for..
>>>
>>>
>>> -f, --first_level ==SUPPRESS== (default: False)
>>> --never_redevelop ==SUPPRESS== (default: False)
>>>
>>>
>>> Any other solutions?
>> That shouldn't happen. Did you reload() somewhere?
>> argparse tests object identity not equality with SUPPRESS, so you have to
>> ensure that SUPPRESS stems from the same instance of the argparse module
>> as your ArgumentParser.
>>
> 
> Ah great yes it wasn't actually the same..
> but why not just use
> if text != SUPPRESS
> instead of:
> if text is not SUPPRESS

Steven Bethard would have to answer that. 
If it were my code I would have used the equality test, but also the correct 
symbolic constant...
 
> probably the second is more safe, but they are it's still checking
> against a constant that
> is very unlikely to clash with anything..


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


Re: GUI for pickle read

2012-02-28 Thread Jerry Hill
On Tue, Feb 28, 2012 at 12:51 PM, Smiley 4321  wrote:
> Can I have some thoughts about - building a GUI to display the results of
> the pickle read?
>
> A prototype code should be fine on Linux.

It doesn't seem like it would be all that useful, though I may just be
lacking in imagination.  What would you do with such a thing?

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


Re: Need to write python source with python

2012-02-28 Thread crstop
On Tuesday, February 28, 2012 9:56:43 AM UTC-8, Peter Otten wrote:
> [email protected] wrote:
> 
> > I'm new to Python but have experience with a few other programming
> > languages(Java, Perl, JavaScript).
> > 
> > I'm using Python 2.7.2 and I'm trying to create and write to a file (.py)
> > a python class and functions from python. I will also need to later read
> > and edit the file. I realize I could just write strings using the
> > available string and file writing methods but suspect there is a better
> > way than that.
> > 
> > I have read about pickle, ast, and Django; searched this group and the web
> > but haven't found a solution that seems to fit. Any suggestion?
> 
> Due to Python's dynamic nature it is rarely necessary to generate Python 
> code. What are you actually trying to achieve?

I'm trying to generate the script file that will launch a PythonCard resource 
file.

very basic example from the documentation.

#!/usr/bin/python
"""
__version__ = "$Revision: 1.10 $"
__date__ = "$Date: 2004/04/24 22:13:31 $"
"""

from PythonCard import model

class Minimal(model.Background):
pass

if __name__ == '__main__':
app = model.Application(Minimal)
app.MainLoop() 
-- 
http://mail.python.org/mailman/listinfo/python-list


Listing children processes

2012-02-28 Thread Mihai Badoiu
I'm trying to compute the total CPU load of an external process and it's
children.  (so I cannot use resource.getrusage)  For the load of the
process I can just grab it from /proc/X/stat.  How do I get the CPU load of
the children processes?  Is there an easy way to get a list of the children
processes?

thanks,

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


Re: GUI for pickle read

2012-02-28 Thread Aaron France



On Tue, Feb 28, 2012 at 12:51 PM, Smiley 4321  wrote:

Can I have some thoughts about - building a GUI to display the results of
the pickle read?

A prototype code should be fine on Linux.

What on earth is this post asking?

Do you want code? Opinions?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Question about circular imports

2012-02-28 Thread Ethan Furman

OKB (not okblacke) wrote:
	Anyway, testing this just reinforced my distaste for circular 
imports.  Just trying to think about how it ought to work with a 
importing c but then c and d importing each other makes my brain hurt.  
Refactoring the files so that common code is in a separate library 
imported by both is easier to understand, and has the nice side bonus 
that it works.




+1 QOTW
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python math is off by .000000000000045

2012-02-28 Thread Ethan Furman

Michael Torrie wrote:

He's simply showing you the hex (binary) representation of the
floating-point number's binary representation.  As you can clearly see
in the case of 1.1, there is no finite sequence that can store that.
You end up with repeating numbers.  


Thanks for the explanation.


This should help you understand why you get errors
doing simple things like x/y*y doesn't quite get you back to x.


I already understood that.  I just didn't understand what point he was 
trying to make since he gave no explanation.


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


Re: Need to write python source with python

2012-02-28 Thread Peter Otten
[email protected] wrote:

> On Tuesday, February 28, 2012 9:56:43 AM UTC-8, Peter Otten wrote:
>> [email protected] wrote:
>> 
>> > I'm new to Python but have experience with a few other programming
>> > languages(Java, Perl, JavaScript).
>> > 
>> > I'm using Python 2.7.2 and I'm trying to create and write to a file
>> > (.py) a python class and functions from python. I will also need to
>> > later read and edit the file. I realize I could just write strings
>> > using the available string and file writing methods but suspect there
>> > is a better way than that.
>> > 
>> > I have read about pickle, ast, and Django; searched this group and the
>> > web but haven't found a solution that seems to fit. Any suggestion?
>> 
>> Due to Python's dynamic nature it is rarely necessary to generate Python
>> code. What are you actually trying to achieve?
> 
> I'm trying to generate the script file that will launch a PythonCard
> resource file.
> 
> very basic example from the documentation.
> 
> #!/usr/bin/python
> """
> __version__ = "$Revision: 1.10 $"
> __date__ = "$Date: 2004/04/24 22:13:31 $"
> """
> 
> from PythonCard import model
> 
> class Minimal(model.Background):
> pass
> 
> if __name__ == '__main__':
> app = model.Application(Minimal)
> app.MainLoop()

If it doesn't get too complex you could start with Python's built-in string 
formatting:

import sys

template = '''\
#!/usr/bin/python
from PythonCard import model

class {Class}(model.Background):
pass

if __name__ == '__main__':
app = model.Application({Class})
app.MainLoop()
'''

resourcename, filename = sys.argv[1:]

with open(resourcename, "U") as f:
data = eval(f.read())

with open(filename, "w") as f:
f.write(template.format(Class=data["application"]["name"]))

If you need logic inside the template, here's on overview: 

http://wiki.python.org/moin/Templating

So there are rather too many options than too few.


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


Re: wcout issue

2012-02-28 Thread Dave Angel

On 02/27/2012 10:26 PM, Ben Held wrote:

Hello,

After upgrading from Python 2.6 to 3.2 in our application we noticed that after 
calling Py_Initialize, our output from std::wcout has extra spaces in it:

wcout<<  L"Just a test\n";

now prints out:

J u s t  a  t e s t

Any clue why?


Ben Held




Have you tried capturing this output, and looking at it with a hex 
viewer?  Are the characters really space, or could they be nulls?  If 
the latter, perhaps it's outputting ucs2 for Unicode.  (I have no 
further ideas, just trying to provoke some experimentation)




--

DaveA

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


Re: Need to write python source with python

2012-02-28 Thread crstop
On Tuesday, February 28, 2012 11:25:33 AM UTC-8, Peter Otten wrote:
> [email protected] wrote:
> 
> > On Tuesday, February 28, 2012 9:56:43 AM UTC-8, Peter Otten wrote:
> >> [email protected] wrote:
> >> 
> >> > I'm new to Python but have experience with a few other programming
> >> > languages(Java, Perl, JavaScript).
> >> > 
> >> > I'm using Python 2.7.2 and I'm trying to create and write to a file
> >> > (.py) a python class and functions from python. I will also need to
> >> > later read and edit the file. I realize I could just write strings
> >> > using the available string and file writing methods but suspect there
> >> > is a better way than that.
> >> > 
> >> > I have read about pickle, ast, and Django; searched this group and the
> >> > web but haven't found a solution that seems to fit. Any suggestion?
> >> 
> >> Due to Python's dynamic nature it is rarely necessary to generate Python
> >> code. What are you actually trying to achieve?
> > 
> > I'm trying to generate the script file that will launch a PythonCard
> > resource file.
> > 
> > very basic example from the documentation.
> > 
> > #!/usr/bin/python
> > """
> > __version__ = "$Revision: 1.10 $"
> > __date__ = "$Date: 2004/04/24 22:13:31 $"
> > """
> > 
> > from PythonCard import model
> > 
> > class Minimal(model.Background):
> > pass
> > 
> > if __name__ == '__main__':
> > app = model.Application(Minimal)
> > app.MainLoop()
> 
> If it doesn't get too complex you could start with Python's built-in string 
> formatting:
> 
> import sys
> 
> template = '''\
> #!/usr/bin/python
> from PythonCard import model
> 
> class {Class}(model.Background):
> pass
> 
> if __name__ == '__main__':
> app = model.Application({Class})
> app.MainLoop()
> '''
> 
> resourcename, filename = sys.argv[1:]
> 
> with open(resourcename, "U") as f:
> data = eval(f.read())
> 
> with open(filename, "w") as f:
> f.write(template.format(Class=data["application"]["name"]))
> 
> If you need logic inside the template, here's on overview: 
> 
> http://wiki.python.org/moin/Templating
> 
> So there are rather too many options than too few.

It shouldn't get very complicated so I look through those options.

Thanks to all posters

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


Re: Listing children processes

2012-02-28 Thread Chris Rebert
On Tue, Feb 28, 2012 at 10:33 AM, Mihai Badoiu  wrote:
> I'm trying to compute the total CPU load of an external process and it's
> children.  (so I cannot use resource.getrusage)  For the load of the process
> I can just grab it from /proc/X/stat.  How do I get the CPU load of the
> children processes?  Is there an easy way to get a list of the children
> processes?

http://code.google.com/p/psutil/

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


Re: Listing children processes

2012-02-28 Thread Mihai Badoiu
Looked at that before.  psutil doesn't do children.

--mihai

On Tue, Feb 28, 2012 at 4:35 PM, Chris Rebert  wrote:

> On Tue, Feb 28, 2012 at 10:33 AM, Mihai Badoiu  wrote:
> > I'm trying to compute the total CPU load of an external process and it's
> > children.  (so I cannot use resource.getrusage)  For the load of the
> process
> > I can just grab it from /proc/X/stat.  How do I get the CPU load of the
> > children processes?  Is there an easy way to get a list of the children
> > processes?
>
> http://code.google.com/p/psutil/
>
> Cheers,
> Chris
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Listing children processes

2012-02-28 Thread Arnaud Delobelle
On 28 February 2012 21:39, Mihai Badoiu  wrote:
> On Tue, Feb 28, 2012 at 4:35 PM, Chris Rebert  wrote:
>>
>> On Tue, Feb 28, 2012 at 10:33 AM, Mihai Badoiu  wrote:
>> > I'm trying to compute the total CPU load of an external process and it's
>> > children.  (so I cannot use resource.getrusage)  For the load of the
>> > process
>> > I can just grab it from /proc/X/stat.  How do I get the CPU load of the
>> > children processes?  Is there an easy way to get a list of the children
>> > processes?
>>
>> http://code.google.com/p/psutil/
>>
>> Cheers,
>> Chris

> Looked at that before.  psutil doesn't do children.
>
> --mihai

Please don't top-post!  Also, psutil.Process.get_children() looks to
me like it "does" children.

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


alternative to with statement?

2012-02-28 Thread Craig Yoshioka
I see that there was previously a PEP to allow the with statement to skip the 
enclosing block... this was shot down, and I'm trying to think of the most 
elegant alternative.
The best I've found is to abuse the for notation:

for _ in cachingcontext(x):
# create cached resources here
# return cached resources

I would have really liked:

with cachingcontext(x):
# create cached resources here
# return cached resources

I'd also like to avoid the following because it is unnecessary boilerplate:

with cachingcontext(x) as skip:
if not skip:
 # create cached resources here
# return cached resources



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


RE: alternative to with statement?

2012-02-28 Thread Prasad, Ramit
Craig Yoshioka wrote:
>I see that there was previously a PEP to allow the with statement to skip the 
>enclosing block... this was shot down, and I'm trying to think of the most 
>elegant alternative. [..]

>I would have really liked:
>with cachingcontext(x):
># create cached resources here
># return cached resources

Is this a common pattern? I thought the point of the context manager 
was to remove create and close the resources (like with file opening).
Seems slightly odd to use just for creation...but maybe only because
I have never used it like that.


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: suppressing argparse arguments in the help

2012-02-28 Thread Calvin Spealman
On Tue, Feb 28, 2012 at 1:07 PM, Peter Otten <[email protected]> wrote:
> Andrea Crotti wrote:
>
>> On 02/28/2012 04:02 PM, Peter Otten wrote:
>>> Andrea Crotti wrote:
>>>
 I have a script that might be used interactively but also has some
 arguments that
 should not be used by "normal" users.
 So I just want to suppress them from the help.
 I've read somewhere that the help=SUPPRESS should do what I want:

       parser.add_argument('-n', '--test_only',
                           action='store_true',
                           help=SUPPRESS)

 but that's what I get from "myapp -h", which is not exactly what I was
 looking for..


     -f, --first_level     ==SUPPRESS== (default: False)
     --never_redevelop     ==SUPPRESS== (default: False)


 Any other solutions?
>>> That shouldn't happen. Did you reload() somewhere?
>>> argparse tests object identity not equality with SUPPRESS, so you have to
>>> ensure that SUPPRESS stems from the same instance of the argparse module
>>> as your ArgumentParser.
>>>
>>
>> Ah great yes it wasn't actually the same..
>> but why not just use
>> if text != SUPPRESS
>> instead of:
>> if text is not SUPPRESS

Because identity tests are good in these situations where you need to pass a
"special" value and anything else could be valid, technically. This is known as
a "sentinel" and is a common pattern.

> Steven Bethard would have to answer that.
> If it were my code I would have used the equality test, but also the correct
> symbolic constant...
>
>> probably the second is more safe, but they are it's still checking
>> against a constant that
>> is very unlikely to clash with anything..
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list



-- 
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://techblog.ironfroggy.com/
Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: alternative to with statement?

2012-02-28 Thread Chris Kaynor
On Tue, Feb 28, 2012 at 1:04 PM, Craig Yoshioka  wrote:
>
> I see that there was previously a PEP to allow the with statement to skip the 
> enclosing block... this was shot down, and I'm trying to think of the most 
> elegant alternative.
> The best I've found is to abuse the for notation:
>
> for _ in cachingcontext(x):
>    # create cached resources here
> # return cached resources
>
> I would have really liked:
>
> with cachingcontext(x):
>    # create cached resources here
> # return cached resources
>
> I'd also like to avoid the following because it is unnecessary boilerplate:
>
> with cachingcontext(x) as skip:
>    if not skip:
>         # create cached resources here
> # return cached resources
>

An alternative way to do this would be to make it a decorator.
Something along the lines of (written in my browser):

def cachedfunc(func):
cachedvalue = None
def newfunc(*args, **kwargs):
nonlocal cachedvalue
if cachedvalue is None:
cachedvalue = func(*args, **kwargs)
return cachedvalue
return newfunc

@cachedfunc
def test():
import time
time.sleep(1)
return 10

test() # should take 1 second
test() # should be instant

While that formula has limitations, it shows the basis of the idea.
I'll leave it to the reader to expand it to allow calls with different
arguments producing different results.

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


Re: alternative to with statement?

2012-02-28 Thread Terry Reedy

On 2/28/2012 5:12 PM, Prasad, Ramit wrote:

Craig Yoshioka wrote:

I see that there was previously a PEP to allow the with statement to skip the 
enclosing block... this was shot down, and I'm trying to think of the most 
elegant alternative. [..]



I would have really liked:
with cachingcontext(x):
# create cached resources here
# return cached resources


Is this a common pattern? I thought the point of the context manager
was to remove create and close the resources (like with file opening).


Exactly. It is Python's version of RAII
https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization


Seems slightly odd to use just for creation...

I do not see the point either.

--
Terry Jan Reedy

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


Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-28 Thread Rick Johnson
On Feb 24, 8:54 am, Steven D'Aprano  wrote:

> for...else is a very useful construct, but the name is misleading. It
> took me a long time to stop thinking that the else clause executes when
> the for loop was empty.

Agreed. This is a major stumbling block for neophytes.

> In Python 4000, I think for loops should be spelled:
>
> for name in iterable:
>     # for block
> then:
>     # only if not exited with break
> else:
>     # only if iterable is empty
>
> and likewise for while loops.

I like this syntax better than the current syntax, however, it is
STILL far too confusing!

> for name in iterable:
>     # for block

this part is okay

> then:
>     # only if not exited with break

I only know how the "then" clause works if you include that comment
each and every time!

> else:
>     # only if iterable is empty

Again. I need more info before this code becomes intuitive. Too much
guessing is required. Not to mention that the try/except/else suite
treats "else" differently.

try:
   do_this()
except EXCEPTION:
   recover()
else NO_EXCEPTION:
   okay_do_this_also().

for x in iterable:
   do_this()
except EXCEPTION:
   recover()
else NO_EXCEPTION:
   do_this_also()

while LOOPING:
do_this()
except EXCEPTION:
break or recover()
else NO_EXCEPTION:
do_this_also()

In this manner "else" will behave consistently between exception
handling and looping.

But this whole idea of using an else clause is ridiculous anyway
because all you've done is to "break up" the code visually. Not to
mention; breaking the cognitive flow of a reader!

try:
do_this()
okay_do_this_also()
what_the_heck.do_this_too()
except EXCEPTION:
recover()
finally:
   always_do_this()

Loop syntax can drop the "else" and adopt "then/finally" -- if you
think we even need a finally!?!?

for x in iterable:
   do_this()
except EXCEPTION:
   recover()
then:
okay_do_this_also()
what_the_heck.do_this_too()
finally:
   always_do_this()

while LOOPING:
do_this()
except EXCEPTION:
   recover()
then:
okay_do_this_also()
what_the_heck.do_this_too()
finally:
   always_do_this()

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


Re: GUI for pickle read

2012-02-28 Thread Rick Johnson
On Feb 28, 12:13 pm, Jerry Hill  wrote:
> On Tue, Feb 28, 2012 at 12:51 PM, Smiley 4321  wrote:
> > Can I have some thoughts about - building a GUI to display the results of
> > the pickle read?

Sure. But first can you show us some code that reads a pickled file
and prints the contents?

Hint: Why does the pickle module have methods called "load", "loads",
"dump", and "dumps"?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: alternative to with statement?

2012-02-28 Thread Craig Yoshioka
It is a bit non-normal.  but I think this is a good use case as I want to 
create a very simple-to-use system for non-python experts to safely wrap their 
CLI programs in a caching architecture... that's why I lament the inability to 
not use the more streamlined 'with' syntax–  abusing the for loop might just be 
confusing.  The with statement is also a good fit because the caching strategy 
does have to atomically acquire, create and release the appropriate locks.  
With this statement the cached CLI wrappers can be called from simultaneously 
from different scripts and still coordinate their activity, by waiting for each 
other to finish, and reusing the cached results, etc.




 
On Feb 28, 2012, at 1:04 PM, Craig Yoshioka wrote:

> I see that there was previously a PEP to allow the with statement to skip the 
> enclosing block... this was shot down, and I'm trying to think of the most 
> elegant alternative.
> The best I've found is to abuse the for notation:
> 
> for _ in cachingcontext(x):
># create cached resources here
> # return cached resources
> 
> I would have really liked:
> 
> with cachingcontext(x):
># create cached resources here
> # return cached resources
> 
> I'd also like to avoid the following because it is unnecessary boilerplate:
> 
> with cachingcontext(x) as skip:
>if not skip:
> # create cached resources here
> # return cached resources
> 
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-28 Thread Chris Angelico
On Wed, Feb 29, 2012 at 9:56 AM, Rick Johnson
 wrote:
> On Feb 24, 8:54 am, Steven D'Aprano  [email protected]> wrote:
>
>> In Python 4000, I think for loops should be spelled:
>>
>> for name in iterable:
>>     # for block
>> then:
>>     # only if not exited with break
>> else:
>>     # only if iterable is empty
>>
>> and likewise for while loops.
>
> I like this syntax better than the current syntax, however, it is
> STILL far too confusing!

Absolutely, it's FAR too confusing. Every syntactic structure should
have the addition of a "foo:" suite, which will run when the
programmer expects it to and no other time. This would solve a LOT of
problems.

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


Re: check if directory is writable in a portable way

2012-02-28 Thread Mark Hammond

On 28/02/2012 9:07 PM, Andrea Crotti wrote:

How should I check if I can create files in a directory?


By trying to create them there :)  Presumably you want to know that so 
you can write something "real" - so just write that something real.


The problem gets quite hard when you consider things like elevation - 
your *user* may have rights to write to a directory but only when 
elevated - think writing into Program Files.  Further, this check can 
only ever be transient - what if you have the rights by virtue of a 
group membership, but tomorrow you are no longer in that group?  Or by 
virtue of being the "owner" of the directory but later losing the ownership?


The only reasonable way to check is to write to it, and you may as well 
skip attempting to write a temp file - just write what you care about 
and handle failure in the most graceful way you can.  This is what 
almost every app does - consider apps with a "save as" dialog - they 
never check the directory is writable, they just attempt the actual 
write and handle the failure.


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


Re: Listing children processes

2012-02-28 Thread Mark Lawrence

On 28/02/2012 21:47, Arnaud Delobelle wrote:

On 28 February 2012 21:39, Mihai Badoiu  wrote:

On Tue, Feb 28, 2012 at 4:35 PM, Chris Rebert  wrote:


On Tue, Feb 28, 2012 at 10:33 AM, Mihai Badoiu  wrote:

I'm trying to compute the total CPU load of an external process and it's
children.  (so I cannot use resource.getrusage)  For the load of the
process
I can just grab it from /proc/X/stat.  How do I get the CPU load of the
children processes?  Is there an easy way to get a list of the children
processes?


http://code.google.com/p/psutil/

Cheers,
Chris



Looked at that before.  psutil doesn't do children.

--mihai


Please don't top-post!  Also, psutil.Process.get_children() looks to
me like it "does" children.



You are incorrect.  I've been told of by the BDFL for stating that 
people should not top post on any Python mailing list/news group.


--
Cheers.

Mark Lawrence.

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


Re: Listing children processes

2012-02-28 Thread Dave Angel

On 02/28/2012 06:41 PM, Mark Lawrence wrote:

On 28/02/2012 21:47, Arnaud Delobelle wrote:

Please don't top-post!  Also, psutil.Process.get_children() looks to
me like it "does" children.



You are incorrect.  I've been told of by the BDFL for stating that 
people should not top post on any Python mailing list/news group.




What does the BDFL have to do with the usefulness of psutil?

I'm not likely to download it, but here's a ref from the docs:

http://code.google.com/p/psutil/wiki/Documentation

get_open_files()
Return files opened by process as a list of namedtuples including file 
absolute path name and file descriptor. Example:

>>> import psutil, os
>>> f = open('file.ext', 'w')
>>> p = psutil.Process(os.getpid())
>>> p.get_open_files()
[openfile(path='/home/giampaolo/svn/psutil/file.ext', fd=3)]




--

DaveA

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


Re: Listing children processes

2012-02-28 Thread Steven D'Aprano
On Tue, 28 Feb 2012 23:41:16 +, Mark Lawrence wrote:

> I've been told of by the BDFL for stating that
> people should not top post on any Python mailing list/news group.

He's the BDFL of Python, not of mailing list etiquette.


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


Re: Listing children processes

2012-02-28 Thread Ben Finney
Mark Lawrence  writes:

> On 28/02/2012 21:47, Arnaud Delobelle wrote:

> > Please don't top-post! Also, psutil.Process.get_children() looks to
> > me like it "does" children.
>
> You are incorrect.

Incorrect? The only factual statement Arnaud made was about ‘psutil’.

> I've been told of by the BDFL for stating that people should not top
> post on any Python mailing list/news group.

Fortunately, the BDFL is not a position of authority outside the Python
language. We as a community have the long-standing consensus that
top-posting is to be avoided.

-- 
 \ “Don't be misled by the enormous flow of money into bad defacto |
  `\standards for unsophisticated buyers using poor adaptations of |
_o__) incomplete ideas.” —Alan Kay |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A quirk/gotcha of for i, x in enumerate(seq) when seq is empty

2012-02-28 Thread Steven D'Aprano
On Wed, 29 Feb 2012 10:24:18 +1100, Chris Angelico wrote:

> Every syntactic structure should
> have the addition of a "foo:" suite, which will run when the programmer
> expects it to and no other time. This would solve a LOT of problems.

Indeed, when I design my killer language, the identifiers "foo" 
and "bar" will be reserved words, never used, and not even 
mentioned in the reference manual. Any program using one will 
simply dump core without comment. Multitudes will rejoice. 
-- Tim Peters, 29 Apr 1998



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


RE: alternative to with statement?

2012-02-28 Thread Prasad, Ramit
Craig Yoshioka wrote:
>It is a bit non-normal.  but I think this is a good use case as I want to 
>create a very simple-to-use system for non-python experts to safely wrap their 
>CLI programs in a caching architecture... that's why I lament the inability to 
>not use the more streamlined 'with' syntax-  abusing the for loop might just 
>be confusing.  The with statement is also a good fit because the caching 
>strategy does have to atomically acquire, create and release the appropriate 
>locks.  With this statement the cached CLI wrappers can be called from 
>simultaneously from different scripts and still coordinate their activity, by 
>waiting for each other to finish, and reusing the cached results, etc.

> with cachingcontext(x):
># create cached resources here
> # return cached resources

On a separate note, how would you even return the cached resources without 
using the following ?
with cachingcontext(x) as blah:

The only thing cachingcontext seems to do is catch errors and since 
cachingcontext would be used by the "non-python experts",
 I have trouble understanding how this would help them any more than: 
try:
# create cached resources here
except SomeError:
# handle error


If you return the cached resources outside the caching context than how could 
you possibly release it 
via the context manager? It seems like you want some kind of resource pooling 
but if that is the 
case then just write your own pool.
See 
http://stackoverflow.com/questions/1514120/python-implementation-of-the-object-pool-design-pattern
 .

Sorry if that does not help, but I do not comprehend what you are trying to do. 
It seems inherently
conflicted to say you want to use "with" because it lets you atomically open, 
use, then close resources
but you do NOT want to atomically open, use, then close resources...


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Listing children processes

2012-02-28 Thread Mark Lawrence

On 29/02/2012 00:16, Steven D'Aprano wrote:

On Tue, 28 Feb 2012 23:41:16 +, Mark Lawrence wrote:


I've been told of by the BDFL for stating that
people should not top post on any Python mailing list/news group.


He's the BDFL of Python, not of mailing list etiquette.




Incorrect, I was told off for this

http://code.activestate.com/lists/python-ideas/14065/

Please don't bother apologising as I don't want to read another of your 
versions of War and Peace.


--
Cheers.

Mark Lawrence.

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


python scripts solution for euler projects

2012-02-28 Thread scripts examples

   Got a web site setup for solving euler problems in python, perl,
ruby and javascript.

   Feel free to give me any feedback, thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python scripts solution for euler projects

2012-02-28 Thread Rodrick Brown
On Tue, Feb 28, 2012 at 10:59 PM, scripts examples <
[email protected]> wrote:

>
>   Got a web site setup for solving euler problems in python, perl,
> ruby and javascript.
>
>   Feel free to give me any feedback, thanks.
>



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


Re: Error importing __init__ declared variable from another package

2012-02-28 Thread Jason Veldicott
>
> > Hi,
> >
> > I have a simple configuration of modules as beneath, but an import error
> > is reported:
> >
> > /engine
> >(__init__ is empty here)
> >engine.py
> > /sim
> >__init__.py
> >
> >
> > The module engine.py imports a variable instantiated in sim.__init__ as
> > follows:
> >
> >from sim import var_name
> >var_name.func()
> >
> > The following error messaged is received on the func() call above
> (Eclipse
> > PyDev):
> >
> > "undefined variable from import: func"
> Are you rephrasing or is this really the error message? If so run your
> program again on the command-line. Then please cut and paste the error
> message together with the traceback.
> > Any idea why this is causing an error?
> What version of Python are you using?
> What does sim/__init__.py contain?



Thanks Peter.

I'm using Python 2.6, but it works at the command line.  The error only
appears in Eclipse as a red cross in the margin.  The exact error msg, as
appears in a floating text caption on mouse over, is as I mentioned
(capitalised).

Perhaps it is some issue in PyDev, maybe related to the version of Python
I'm using.

I'm in the process of trying to solve another related import problem, and
wished to resolve this one in the hope that it might shed light on the
other. But as it works beside the error icon appearing, I might just ignore
it and spare the trouble of precise identification of cause.

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


Is it necessary to call Tk() when writing a GUI app with Tkinter?

2012-02-28 Thread John Salerno
The book I'm reading about using Tkinter only does this when creating the 
top-level window:

app = Application()
app.mainloop()

and of course the Application class has subclassed the tkinter.Frame class.

However, in the Python documentation, I see this:

root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

Is it necessary to explicitly call Tk(), then pass that result as an argument 
for the Application call? Is it also necessary to call destroy() on the root 
frame?

I tried the above and I got the following error:

Traceback (most recent call last):
  File "C:\Users\John\Desktop\gui.py", line 12, in 
root.destroy()
  File "C:\Python32\lib\tkinter\__init__.py", line 1714, in destroy
self.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command:  application has been 
destroyed

So apparently closing the window with the X button (on Windows) implicitly 
calls the destroy() method of the root frame. If that's the case, why does the 
documentation explicitly call it?

Furthermore, I pasted the exact example from the documentation into IDLE and 
ran it, and I also go the same error, so the example in the documentation 
doesn't even work.

So is it sufficient simply to create an Application instance, use mainloop, and 
then handle the closing of the window elsewhere in the program (such as a 
widget calling the destroy method on-click, or just letting the X button do it)?

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


Re: Listing children processes

2012-02-28 Thread Ben Finney
Mark Lawrence  writes:

> On 29/02/2012 00:16, Steven D'Aprano wrote:
> > On Tue, 28 Feb 2012 23:41:16 +, Mark Lawrence wrote:
> >> I've been told of by the BDFL for stating that people should not
> >> top post on any Python mailing list/news group.
> >
> > He's the BDFL of Python, not of mailing list etiquette.
>
> Incorrect

What Steven says is correct: the BDFL of Python has no special authority
over mailing list etiquette.

> I was told off for this

No-one's saying you weren't told off. We're saying that there's no point
invoking the BDFL when it comes to mailing list etiquette, as he has no
special authority on that.

> Please don't bother apologising

I doubt anyone feels compelled to apologise to you for this.

-- 
 \   “In the long run nothing can withstand reason and experience, |
  `\and the contradiction which religion offers to both is all too |
_o__)palpable.” —Sigmund Freud |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


[no subject]

2012-02-28 Thread Brendon Joyce

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


Pickle handling dictionary

2012-02-28 Thread Smiley 4321
I was successful in testing pickle with multiple objects both READ & WRITE.

I did WRITE as -
---
#!/usr/bin/python

import pickle

class apple_Class(object):
def __init__(self, **kwds):
self.__dict__.update(kwds)
myInst = apple_Class()
myInst.FirstString = "Apple"
myInst.SecondString = "Orange"

with open('/tmp/readfile.pkl', 'wb') as f:
pickle.dump((myInst.FirstString, myInst.SecondString), f)


I did READ as -

---
#!/usr/bin/python

import pickle

class apple_Class(object):
def __init__(self, **kwds):
self.__dict__.update(kwds)

myInst = apple_Class()
with open('/tmp/readfile.pkl', 'rb') as f:
myInst.FirstString, myInst.SecondString = pickle.load(f)

print myInst.FirstString
print myInst.SecondString
---

Both worked successfully.

Now, I have to write a file having below three things -

- Having dictionary of many instances
- string as a Key
- Instances as Value.

Finally, I have to do the same thing as above to WRITE in separate file and
READ in separate file.

So, the issues is simply how to handle many instances in a dictionary with
Key and Value.
-- 
http://mail.python.org/mailman/listinfo/python-list