Tuesday, March 2, 2010

comp.lang.python - 25 new messages in 11 topics - digest

comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en

comp.lang.python@googlegroups.com

Today's topics:

* Queue peek? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/ba3cb62c81d4cb7a?hl=en
* freebsd and multiprocessing - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/712e00ab1354e885?hl=en
* Draft PEP on RSON configuration file format - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/09ce33197b330e90?hl=en
* Is this secure? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/ffff2b290db4e811?hl=en
* Adding to a module's __dict__? - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/40837c4567d64745?hl=en
* os.fdopen() issue in Python 3.1? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/49a2ca4c1f9e7a59?hl=en
* Call Signtool using python - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/2a6d7daff19d7360?hl=en
* Docstrings considered too complicated - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/dea5c94f3d058e26?hl=en
* case do problem - 5 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/d73f6f6a59d3bbfd?hl=en
* Broken references in postings - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/658a7033105e20f3?hl=en
* conditional import into global namespace - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/8718bce88cea1cf5?hl=en

==============================================================================
TOPIC: Queue peek?
http://groups.google.com/group/comp.lang.python/t/ba3cb62c81d4cb7a?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Mar 2 2010 8:29 am
From: Veloz


Hi all
I'm looking for a queue that I can use with multiprocessing, which has
a peek method.

I've seen some discussion about queue.peek but don't see anything in
the docs about it.

Does python have a queue class with peek semantics?

Michael


== 2 of 2 ==
Date: Tues, Mar 2 2010 10:18 am
From: Raymond Hettinger


On Mar 2, 8:29 am, Veloz <michaelve...@gmail.com> wrote:
> Hi all
> I'm looking for a queue that I can use with multiprocessing, which has
> a peek method.
>
> I've seen some discussion about queue.peek but don't see anything in
> the docs about it.
>
> Does python have a queue class with peek semantics?

Am curious about your use case? Why peek at something
that could be gone by the time you want to use it.

val = q.peek()
if something_i_want(val):
v2 = q.get() # this could be different than val

Wouldn't it be better to just get() the value and return if you don't
need it?

val = q.peek()
if not something_i_want(val):
q.put(val)


Raymond


==============================================================================
TOPIC: freebsd and multiprocessing
http://groups.google.com/group/comp.lang.python/t/712e00ab1354e885?hl=en
==============================================================================

== 1 of 3 ==
Date: Tues, Mar 2 2010 8:31 am
From: Tim Arnold


Hi,
I'm intending to use multiprocessing on a freebsd machine (6.3
release, quad core, 8cpus, amd64). I see in the doc that on this
platform I can't use synchronize:

ImportError: This platform lacks a functioning sem_open
implementation, therefore, the required synchronization primitives
needed will not function, see issue 3770.

As far as I can tell, I have no need to synchronize the processes--I
have several processes run separately and I need to know when they're
all finished; there's no communication between them and each owns its
own log file for output.

Is anyone using multiprocessing on FreeBSD and run into any other
gotchas?
thanks,
--Tim Arnold


== 2 of 3 ==
Date: Tues, Mar 2 2010 8:52 am
From: Philip Semanchuk

On Mar 2, 2010, at 11:31 AM, Tim Arnold wrote:

> Hi,
> I'm intending to use multiprocessing on a freebsd machine (6.3
> release, quad core, 8cpus, amd64). I see in the doc that on this
> platform I can't use synchronize:
>
> ImportError: This platform lacks a functioning sem_open
> implementation, therefore, the required synchronization primitives
> needed will not function, see issue 3770.
>
> As far as I can tell, I have no need to synchronize the processes--I
> have several processes run separately and I need to know when they're
> all finished; there's no communication between them and each owns its
> own log file for output.
>
> Is anyone using multiprocessing on FreeBSD and run into any other
> gotchas?

Hi Tim,
I don't use multiprocessing but I've written two low-level IPC
packages, one for SysV IPC and the other for POSIX IPC.

I think that multiprocessing prefers POSIX IPC (which is where
sem_open() comes from). I don't know what it uses if that's not
available, but SysV IPC seems a likely alternative. I must emphasize,
however, that that's a guess on my part.

FreeBSD didn't have POSIX IPC support until 7.0, and that was sort of
broken until 7.2. As it happens, I was testing my POSIX IPC code
against 7.2 last night and it works just fine.

SysV IPC works under FreeBSD 6 (and perhaps earlier versions; 6 is the
oldest I've tested). ISTR that by default each message queue is
limited to 2048 bytes in total size. 'sysctl kern.ipc' can probably
tell you that and may even let you change it. Other than that I can't
think of any SysV limitations that might bite you.

HTH
Philip

== 3 of 3 ==
Date: Tues, Mar 2 2010 9:59 am
From: Tim Arnold


On Mar 2, 11:52 am, Philip Semanchuk <phi...@semanchuk.com> wrote:
> On Mar 2, 2010, at 11:31 AM, Tim Arnold wrote:
>
>
>
>
>
> > Hi,
> > I'm intending to use multiprocessing on a freebsd machine (6.3
> > release, quad core, 8cpus, amd64). I see in the doc that on this
> > platform I can't use synchronize:
>
> > ImportError: This platform lacks a functioning sem_open
> > implementation, therefore, the required synchronization primitives
> > needed will not function, see issue 3770.
>
> > As far as I can tell, I have no need to synchronize the processes--I
> > have several processes run separately and I need to know when they're
> > all finished; there's no communication between them and each owns its
> > own log file for output.
>
> > Is anyone using multiprocessing on FreeBSD and run into any other
> > gotchas?
>
> Hi Tim,
> I don't use multiprocessing but I've written two low-level IPC  
> packages, one for SysV IPC and the other for POSIX IPC.
>
> I think that multiprocessing prefers POSIX IPC (which is where  
> sem_open() comes from). I don't know what it uses if that's not  
> available, but SysV IPC seems a likely alternative. I must emphasize,  
> however, that that's a guess on my part.
>
> FreeBSD didn't have POSIX IPC support until 7.0, and that was sort of  
> broken until 7.2. As it happens, I was testing my POSIX IPC code  
> against 7.2 last night and it works just fine.
>
> SysV IPC works under FreeBSD 6 (and perhaps earlier versions; 6 is the  
> oldest I've tested). ISTR that by default each message queue is  
> limited to 2048 bytes in total size. 'sysctl kern.ipc' can probably  
> tell you that and may even let you change it. Other than that I can't  
> think of any SysV limitations that might bite you.
>
> HTH
> Philip

Hi Philip,
Thanks for that information. I wish I could upgrade the machine to
7.2! alas, out of my power. I get the following results from sysctl:
% sysctl kern.ipc | grep msg
kern.ipc.msgseg: 2048
kern.ipc.msgssz: 8
kern.ipc.msgtql: 40
kern.ipc.msgmnb: 2048
kern.ipc.msgmni: 40
kern.ipc.msgmax: 16384

I'll write some test programs using multiprocessing and see how they
go before committing to rewrite my current code. I've also been
looking at 'parallel python' although it may have the same issues.
http://www.parallelpython.com/

thanks again,
--Tim

==============================================================================
TOPIC: Draft PEP on RSON configuration file format
http://groups.google.com/group/comp.lang.python/t/09ce33197b330e90?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Mar 2 2010 8:34 am
From: Robert Kern


On 2010-03-01 22:55 PM, Terry Reedy wrote:
> On 3/1/2010 7:56 PM, Patrick Maupin wrote:
>> On Mar 1, 5:57 pm, Erik Max Francis<m...@alcyone.com> wrote:
>>> Patrick Maupin wrote:
>>> This not only seriously stretching the meaning of the term "superset"
>>> (as Python is most definitely not even remotely a superset of JSON), but
>>
>> Well, you are entitled to that opinion, but seriously, if I take valid
>> JSON, replace unquoted true with True, unquoted false with False,
>> replace unquoted null with None, and take the quoted strings and
>> replace occurrences of \uXXXX with the appropriate unicode, then I do,
>> in fact, have valid Python. But don't take my word for it -- try it
>> out!
>
> To me this is so strained that I do not see why why you are arguing the
> point. So what? The resulting Python 'program' will be equivalent, I
> believe, to 'pass'. Ie, construct objects and then discard them with no
> computation or output.

Not if you eval() rather than exec(). It's reasonably well-accepted that JSON is
very close to being a subset of Python's expression syntax with just a few
modifications.

--
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

== 2 of 2 ==
Date: Tues, Mar 2 2010 9:59 am
From: Terry Reedy


On 3/2/2010 11:34 AM, Robert Kern wrote:
> On 2010-03-01 22:55 PM, Terry Reedy wrote:
>> On 3/1/2010 7:56 PM, Patrick Maupin wrote:
>>> On Mar 1, 5:57 pm, Erik Max Francis<m...@alcyone.com> wrote:
>>>> Patrick Maupin wrote:
>>>> This not only seriously stretching the meaning of the term "superset"
>>>> (as Python is most definitely not even remotely a superset of JSON),
>>>> but
>>>
>>> Well, you are entitled to that opinion, but seriously, if I take valid
>>> JSON, replace unquoted true with True, unquoted false with False,
>>> replace unquoted null with None, and take the quoted strings and
>>> replace occurrences of \uXXXX with the appropriate unicode, then I do,
>>> in fact, have valid Python. But don't take my word for it -- try it
>>> out!
>>
>> To me this is so strained that I do not see why why you are arguing the
>> point. So what? The resulting Python 'program' will be equivalent, I
>> believe, to 'pass'. Ie, construct objects and then discard them with no
>> computation or output.
>
> Not if you eval() rather than exec().

>>> eval(1)

creates and objects and discards it, with a net result of 'pass'.
What do you think I am missing.

It's reasonably well-accepted that
> JSON is very close to being a subset of Python's expression syntax with
> just a few modifications.

It is specifically JavaScript Object Notation, which is very similar to
a subset of Python's object notation (number and string literals and
list and dict displays (but not set displays), and three named
constants). Without operators, it barely qualifies, to me, even as
'expression syntax'.

To me, comparing object notation with programming language is not
helpful to the OP's purpose. His main claim is that JSON can be usefully
extended but that YAML is too much, so that perhaps he, with help, can
find a 'sweet spot' in between.

Terry Jan Reedy

==============================================================================
TOPIC: Is this secure?
http://groups.google.com/group/comp.lang.python/t/ffff2b290db4e811?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Mar 2 2010 8:37 am
From: Robert Kern


On 2010-02-28 01:28 AM, Aahz wrote:
> In article<mailman.247.1267115557.4577.python-list@python.org>,
> Robert Kern<robert.kern@gmail.com> wrote:
>>
>> If you are storing the password instead of making your user remember
>> it, most platforms have some kind of keychain secure password
>> storage. I recommend reading up on the APIs available on your targeted
>> platforms.
>
> Are you sure? I haven't done a lot of research, but my impression was
> that Windows didn't have anything built in.

You're right, not built-in, but Windows does provide enough crypto services for
a cross-platform Python implementation to be built:

http://pypi.python.org/pypi/keyring

--
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


==============================================================================
TOPIC: Adding to a module's __dict__?
http://groups.google.com/group/comp.lang.python/t/40837c4567d64745?hl=en
==============================================================================

== 1 of 3 ==
Date: Tues, Mar 2 2010 8:49 am
From: Carl Banks


On Mar 2, 5:21 am, Roy Smith <r...@panix.com> wrote:
> In article <mailman.96.1267508316.23598.python-l...@python.org>,
>  Chris Rebert <c...@rebertia.com> wrote:
>
>
>
> > On Mon, Mar 1, 2010 at 8:27 PM, Roy Smith <r...@panix.com> wrote:
> > > >From inside a module, I want to add a key-value pair to the module's
> > > __dict__.  I know I can just do:
>
> > > FOO = 'bar'
>
> > > at the module top-level, but I've got 'FOO' as a string and what I
> > > really need to do is
>
> > > __dict__['Foo'] = 'bar'
>
> > > When I do that, I get "NameError: name '__dict__' is not defined".  Is
> > > it possible to do what I'm trying to do?
>
> > Yes; just modify the dict returned by the globals() built-in function
> > instead.
>
> Ah, cool.  Thanks.
>
> > It's usually not wise to do this and is better to use a
> > separate dict instead, but I'll assume you know what you're doing and
> > have good reasons to disregard the standard advice due to your
> > use-case.
>
> Why is it unwise?


He didn't say it was unwise. He said it's usually not wise.


Carl Banks


== 2 of 3 ==
Date: Tues, Mar 2 2010 9:26 am
From: MRAB


John Posner wrote:
> On 3/2/2010 10:19 AM, Roy Smith wrote:
>>
>> Somewhat sadly, in my case, I can't even machine process the header
>> file. I don't, strictly speaking, have a header file. What I have is
>> a PDF which documents what's in the header file, and I'm manually re-
>> typing the data out of that. Sigh.
>
> Here's an idea, perhaps too obvious, to minimize your keystrokes:
>
> 1. Create a text file with the essential data:
>
> XYZ_FOO 0 The foo property
> XYZ_BAR 1 The bar property
> XYZ_BAZ 2 reserved for future use
>
> 2. Use a Python script to convert this into the desired code:
>
> declare('XYZ_FOO', 0, "The foo property")
> declare('XYZ_BAR', 1, "The bar property")
> declare('XYZ_BAZ', 2, "reserved for future use")
>
> Note:
>
> >>> s
> 'XYZ_FOO 0 The foo property'
> >>> s.split(None, 2)
> ['XYZ_FOO', '0', 'The foo property']
>
You might be able to reduce your typing by copy-and-pasting the relevant
text from the PDF into an editor and then editing it.


== 3 of 3 ==
Date: Tues, Mar 2 2010 9:38 am
From: Terry Reedy


On 3/2/2010 11:18 AM, John Posner wrote:
> On 3/2/2010 10:19 AM, Roy Smith wrote:
>>
>> Somewhat sadly, in my case, I can't even machine process the header
>> file. I don't, strictly speaking, have a header file. What I have is
>> a PDF which documents what's in the header file, and I'm manually re-
>> typing the data out of that. Sigh.

There are Python modules to read/write pdf.

> Here's an idea, perhaps too obvious, to minimize your keystrokes:
>
> 1. Create a text file with the essential data:
>
> XYZ_FOO 0 The foo property
> XYZ_BAR 1 The bar property
> XYZ_BAZ 2 reserved for future use
>
> 2. Use a Python script to convert this into the desired code:
>
> declare('XYZ_FOO', 0, "The foo property")
> declare('XYZ_BAR', 1, "The bar property")
> declare('XYZ_BAZ', 2, "reserved for future use")
>
> Note:
>
> >>> s
> 'XYZ_FOO 0 The foo property'
> >>> s.split(None, 2)
> ['XYZ_FOO', '0', 'The foo property']

Given that set of triples is constant, I would think about having the
Python script do the computation just once, instead of with every
inport. In other words, the script should *call* the declare function
and then write out the resulting set of dicts either to a .py or pickle
file.

tjr

==============================================================================
TOPIC: os.fdopen() issue in Python 3.1?
http://groups.google.com/group/comp.lang.python/t/49a2ca4c1f9e7a59?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Mar 2 2010 9:32 am
From: MRAB


Albert Hopkins wrote:
> I have a snippet of code that looks like this:
>
> pid, fd = os.forkpty()
> if pid == 0:
> subprocess.call(args)
> else:
> input = os.fdopen(fd).read()
> ...
>
>
> This seems to work find for CPython 2.5 and 2.6 on my Linux system.
> However, with CPython 3.1 I get:
>
> input = os.fdopen(fd).read()
> IOError: [Errno 5] Input/output error
>
> Is there something wrong in Python 3.1? Is this the correct way to do
> this (run a process in a pseudo-tty and read it's output) or is there
> another way I should/could be doing this?
>
The documentation also mentions the 'pty' module. Have you tried that
instead?

==============================================================================
TOPIC: Call Signtool using python
http://groups.google.com/group/comp.lang.python/t/2a6d7daff19d7360?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Mar 2 2010 9:33 am
From: enda man


On Mar 2, 2:46 pm, "Matt Mitchell" <mmitch...@transparent.com> wrote:
> I think you need to use the /p switch to pass signtool.exe a password
> when using the /f switch.
> Check outhttp://msdn.microsoft.com/en-us/library/8s9b9yaz%28VS.80%29.aspxfor
> more info.
>
> -----------------------------------
> The information contained in this electronic message and any attached document(s) is intended only for the personal and confidential use of the designated recipients named above. This message may be confidential. If the reader of this message is not the intended recipient, you are hereby notified that you have received this document in error, and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify sender immediately by telephone (603) 262-6300 or by electronic mail immediately. Thank you.
>
> -----Original Message-----
> From: python-list-bounces+mmitchell=transparent....@python.org
>
> [mailto:python-list-bounces+mmitchell=transparent....@python.org] On
> Behalf Of enda man
> Sent: Tuesday, March 02, 2010 6:34 AM
> To: python-l...@python.org
> Subject: Call Signtool using python
>
> Hi,
>
> I want to call the Windows signtool to sign a binary from a python
> script.
>
> Here is my script:
> //
> os.chdir('./Install/activex/cab')
> subprocess.call(["signtool", "sign", "/v", "/f", "webph.pfx", "/t",
> "http://timestamp.verisign.com/scripts/timstamp.dll", "WebPh.exe" ])
> //
>
> But I am getting this error:
> ////
> SignTool Error: The specified PFX password is not correct.
>
> Number of files successfully Signed: 0
> Number of warnings: 0
> Number of errors: 1
> Finished building plugin installer
> scons: done building targets.
> ////
>
> This python script is called as part of a scons build, which is also
> python code.
>
> Anyone seen this before or can pass on any ideas.
> Tks,
> EM
>
> --http://mail.python.org/mailman/listinfo/python-list
>
>

The same command works when run from the command line but when I run
it from python using subprocess.call(...) it fails as described in my
first post. I do not need to use the /p switch as the password is
embedded in the pfx file.
That is why I think it is something I am or am not doing in python.

EM

== 2 of 2 ==
Date: Tues, Mar 2 2010 9:59 am
From: Chris Rebert


On Tue, Mar 2, 2010 at 3:34 AM, enda man <emannion@gmail.com> wrote:
> Hi,
>
> I want to call the Windows signtool to sign a binary from a python
> script.
>
> Here is my script:
> //
> os.chdir('./Install/activex/cab')
> subprocess.call(["signtool", "sign", "/v", "/f", "webph.pfx", "/t",
> "http://timestamp.verisign.com/scripts/timstamp.dll", "WebPh.exe" ])
> //
>
> But I am getting this error:
> ////
> SignTool Error: The specified PFX password is not correct.
>
> Number of files successfully Signed: 0
> Number of warnings: 0
> Number of errors: 1
> Finished building plugin installer
> scons: done building targets.
> ////
>
>
> This python script is called as part of a scons build, which is also
> python code.
>
> Anyone seen this before or can pass on any ideas.

Nothing looks obviously wrong (though I'm unfamiliar with signtool).
Have you tried specifying an absolute path to webph.pfx?

Cheers,
Chris
--
http://blog.rebertia.com

==============================================================================
TOPIC: Docstrings considered too complicated
http://groups.google.com/group/comp.lang.python/t/dea5c94f3d058e26?hl=en
==============================================================================

== 1 of 3 ==
Date: Tues, Mar 2 2010 9:35 am
From: Andreas Waldenburger


On Mon, 01 Mar 2010 21:09:39 +0000 Mark Lawrence
<breamoreboy@yahoo.co.uk> wrote:

> Andreas Waldenburger wrote:
> > [snip]
> > We did not buy code. If it were written in C or such, we would never
> > get to see it.
> >
> > It's not our concern.
> >
> > /W
> >
>
> From your original post.
>
> <quote>
> a company that works with my company writes a lot of of their code in
> Python (lucky jerks). I've seen their code and it basically looks like
> this:
> </quote>
>
> So what is the relationship between your company and this other
> company?
They write a piece of software that we run as a component in a software
ecosystem that we (and others) have developed.


> When it gets down to pounds, shillings and pence (gosh, I'm
> old!:) it sure as hell could make a tremendous difference in the long
> term, given that usually maintainance costs are astronomical when
> compared to initial development costs.
>
I totally agree, but as long as their software works, why should we
care how their code looks? It's totally their responsibility. Why
should we nanny every partner we have?

Don't get me wrong; our whole system is more fragile than I find
comfortable. But I guess getting 10ish different parties around the
globe to work in complete unison is quite a feat, and I'm surprised it
even works as it is. But it does, and I'm glad we don't have to
micromanage other people's code.


/W


--
INVALID? DE!

== 2 of 3 ==
Date: Tues, Mar 2 2010 9:40 am
From: Andreas Waldenburger


On Tue, 02 Mar 2010 09:48:47 +1100 Ben Finney
<ben+python@benfinney.id.au> wrote:

> > It's not our concern.
>
> Then I don't see what that problem is.

There is none. I was griping about how stupid they are. That is a
personal problem I have with their *code* (not software), and I thought
I'd just share my superiority complex with everyone.

I had hoped that everyone just read it, went like "Oh geez.", smiled it
off with a hint of lesson learned and got back to whatever it was they
were doing. Alas, I was wrong ... and I'm sorry.

/W

--
INVALID? DE!

== 3 of 3 ==
Date: Tues, Mar 2 2010 10:05 am
From: Jean-Michel Pichavant


Andreas Waldenburger wrote:
> On Tue, 02 Mar 2010 09:48:47 +1100 Ben Finney
> <ben+python@benfinney.id.au> wrote:
>
>
>>> It's not our concern.
>>>
>> Then I don't see what that problem is.
>>
>
> There is none. I was griping about how stupid they are. That is a
> personal problem I have with their *code* (not software), and I thought
> I'd just share my superiority complex with everyone.
>
> I had hoped that everyone just read it, went like "Oh geez.", smiled it
> off with a hint of lesson learned and got back to whatever it was they
> were doing. Alas, I was wrong ... and I'm sorry.
>
> /W
>
>
There's something wrong saying that stupid people write working code
that totally satisfies your needs. Don't you agree ? ;-)

JM

==============================================================================
TOPIC: case do problem
http://groups.google.com/group/comp.lang.python/t/d73f6f6a59d3bbfd?hl=en
==============================================================================

== 1 of 5 ==
Date: Tues, Mar 2 2010 9:46 am
From: Tracubik


hi, i've to convert from Pascal this code:

iterations=0;
count=0;
REPEAT;
iterations = iterations+1;
...
IF (genericCondition) THEN count=count+1;
...
CASE count OF:
1: m = 1
2: m = 10
3: m = 100
UNTIL count = 4 OR iterations = 20

i do something like this:

iterations = 0
count = 0

m_Switch = (1,10,100)

while True:
iterations +=1
...
if (genericCondition):
count +=1
...
try:
m = m_Switch[count-1]
except: pass
if count = 4 or iterations = 20

the problem is that when count = 4 m_Switch[4-1] have no value, so i use
the try..except.

Is there a better solution to solve this problem? and, generally
speaking, the try..except block slow down the execution of the program or
not?

Thank you in advance
Nico


== 2 of 5 ==
Date: Tues, Mar 2 2010 9:50 am
From: Tracubik


additional information:

when count=4 i haven't to change the m value, so i have to do nothing or
something like m = m

Nico


== 3 of 5 ==
Date: Tues, Mar 2 2010 10:00 am
From: "Alf P. Steinbach"


* Tracubik:
> hi, i've to convert from Pascal this code:
>
> iterations=0;
> count=0;
> REPEAT;
> iterations = iterations+1;
> ...
> IF (genericCondition) THEN count=count+1;
> ...
> CASE count OF:
> 1: m = 1
> 2: m = 10
> 3: m = 100

Uhm, is this syntactically valid Pascal? As I recall, every Pascal construct was
delimited in some way. Once I had the complete Pascal syntax in my head, but
alas, not anymore...


> UNTIL count = 4 OR iterations = 20
>
> i do something like this:
>
> iterations = 0
> count = 0
>
> m_Switch = (1,10,100)
>
> while True:
> iterations +=1
> ...
> if (genericCondition):
> count +=1
> ...
> try:
> m = m_Switch[count-1]
> except: pass
> if count = 4 or iterations = 20
>
> the problem is that when count = 4 m_Switch[4-1] have no value, so i use
> the try..except.

iterations = 0
count = 0
while not( count == 4 or iterations == 20 ):
iterations += 1
# ...
if generic_condition:
count += 1
# ...
m = (1, 10, 100, 100)[count]


> Is there a better solution to solve this problem?

Define "better". Do you mean faster, more clear, shorter, using less memory, what?

Above I've assumed that you want to get rid of the try block, since that's what
you're asking about:


> and, generally
> speaking, the try..except block slow down the execution of the program or
> not?

Probably, but don't think about it. Python programming is at a level where that
kind of efficiency doesn't count. Or, ideally it shouldn't count.


Cheers & hth.,

- Alf


== 4 of 5 ==
Date: Tues, Mar 2 2010 10:01 am
From: "Alf P. Steinbach"


* Alf P. Steinbach:
> * Tracubik:
>> hi, i've to convert from Pascal this code:
>>
>> iterations=0;
>> count=0;
>> REPEAT;
>> iterations = iterations+1;
>> ...
>> IF (genericCondition) THEN count=count+1;
>> ...
>> CASE count OF:
>> 1: m = 1
>> 2: m = 10
>> 3: m = 100
>
> Uhm, is this syntactically valid Pascal? As I recall, every Pascal
> construct was delimited in some way. Once I had the complete Pascal
> syntax in my head, but alas, not anymore...
>
>
>> UNTIL count = 4 OR iterations = 20
>>
>> i do something like this:
>>
>> iterations = 0
>> count = 0
>>
>> m_Switch = (1,10,100)
>>
>> while True:
>> iterations +=1
>> ...
>> if (genericCondition):
>> count +=1
>> ...
>> try:
>> m = m_Switch[count-1]
>> except: pass
>> if count = 4 or iterations = 20
>>
>> the problem is that when count = 4 m_Switch[4-1] have no value, so i
>> use the try..except.
>
> iterations = 0
> count = 0
> while not( count == 4 or iterations == 20 ):
> iterations += 1
> # ...
> if generic_condition:
> count += 1
> # ...
> m = (1, 10, 100, 100)[count]

Add one extra 100 there.


>> Is there a better solution to solve this problem?
>
> Define "better". Do you mean faster, more clear, shorter, using less
> memory, what?
>
> Above I've assumed that you want to get rid of the try block, since
> that's what you're asking about:
>
>
>> and, generally speaking, the try..except block slow down the execution
>> of the program or not?
>
> Probably, but don't think about it. Python programming is at a level
> where that kind of efficiency doesn't count. Or, ideally it shouldn't
> count.
>
>
> Cheers & hth.,
>
> - Alf


== 5 of 5 ==
Date: Tues, Mar 2 2010 10:04 am
From: "Alf P. Steinbach"


* Alf P. Steinbach:
> * Alf P. Steinbach:
>> * Tracubik:
>>> hi, i've to convert from Pascal this code:
>>>
>>> iterations=0;
>>> count=0;
>>> REPEAT;
>>> iterations = iterations+1;
>>> ...
>>> IF (genericCondition) THEN count=count+1;
>>> ...
>>> CASE count OF:
>>> 1: m = 1
>>> 2: m = 10
>>> 3: m = 100
>>
>> Uhm, is this syntactically valid Pascal? As I recall, every Pascal
>> construct was delimited in some way. Once I had the complete Pascal
>> syntax in my head, but alas, not anymore...
>>
>>
>>> UNTIL count = 4 OR iterations = 20
>>>
>>> i do something like this:
>>>
>>> iterations = 0
>>> count = 0
>>>
>>> m_Switch = (1,10,100)
>>>
>>> while True:
>>> iterations +=1
>>> ...
>>> if (genericCondition):
>>> count +=1
>>> ...
>>> try:
>>> m = m_Switch[count-1]
>>> except: pass
>>> if count = 4 or iterations = 20
>>>
>>> the problem is that when count = 4 m_Switch[4-1] have no value, so i
>>> use the try..except.
>>
>> iterations = 0
>> count = 0
>> while not( count == 4 or iterations == 20 ):
>> iterations += 1
>> # ...
>> if generic_condition:
>> count += 1
>> # ...
>> m = (1, 10, 100, 100)[count]
>
> Add one extra 100 there.

Oh dear, it's one of those days.

if 1 <= count <= 3:
m = (1, 10, 100)[count - 1]


>>> Is there a better solution to solve this problem?
>>
>> Define "better". Do you mean faster, more clear, shorter, using less
>> memory, what?
>>
>> Above I've assumed that you want to get rid of the try block, since
>> that's what you're asking about:
>>
>>
>>> and, generally speaking, the try..except block slow down the
>>> execution of the program or not?
>>
>> Probably, but don't think about it. Python programming is at a level
>> where that kind of efficiency doesn't count. Or, ideally it shouldn't
>> count.
>>
>>
>> Cheers & hth.,
>>
>> - Alf

==============================================================================
TOPIC: Broken references in postings
http://groups.google.com/group/comp.lang.python/t/658a7033105e20f3?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Mar 2 2010 9:46 am
From: Grant Edwards


I've noticed recently that a lot of the "refernces" and
"in-reply-to" headers in c.l.p are broken, resulting in the
inability to move from a child to a parent in a tree.

For example in a recent reply (subejct: os.fdopen() issue in
Python 3.1?), the references and in-reply-to headers both
contained:

1267539898.477222.7.camel@necropolis

However, the article replied to has a message-ID header of

mailman.110.1267539885.23598.python-list@python.org

I don't see 1267539898.477222.7.camel@necropolis anywhere in
the headers of the referrant.

Is something broken in the mail<->news gateway?

Or is it just individual news/mail clients that are broken?

--
Grant Edwards grant.b.edwards Yow! I own seven-eighths of
at all the artists in downtown
gmail.com Burbank!

==============================================================================
TOPIC: conditional import into global namespace
http://groups.google.com/group/comp.lang.python/t/8718bce88cea1cf5?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Mar 2 2010 9:46 am
From: mk


Hello everyone,

I have a class that is dependent on subprocess functionality. I would
like to make it self-contained in the sense that it would import
subprocess if it's not imported yet.

What is the best way to proceed with this?

I see a few possibilities:

1. do a class level import, like:

class TimeSync(object):

import subprocess


2. do an import in init, which is worse bc it's ran every time an
instance is created:

def __init__(self, shiftsec, ntpserver):
import subprocess


Both of those methods have disadvantage in this context, though: they
create 'subprocess' namespace in a class or instance, respectively.

Is there anyway to make it a global import?

Regards,
mk

== 2 of 2 ==
Date: Tues, Mar 2 2010 10:21 am
From: MRAB


mk wrote:
> Hello everyone,
>
> I have a class that is dependent on subprocess functionality. I would
> like to make it self-contained in the sense that it would import
> subprocess if it's not imported yet.
>
> What is the best way to proceed with this?
>
> I see a few possibilities:
>
> 1. do a class level import, like:
>
> class TimeSync(object):
>
> import subprocess
>
>
> 2. do an import in init, which is worse bc it's ran every time an
> instance is created:
>
> def __init__(self, shiftsec, ntpserver):
> import subprocess
>
>
> Both of those methods have disadvantage in this context, though: they
> create 'subprocess' namespace in a class or instance, respectively.
>
> Is there anyway to make it a global import?
>
The simplest solution is just import it at the top of the module.


==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en

To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate