Re: Books?

2012-08-22 Thread Michael Poeltl
I would recommend "Dive into Python3"

just goole-search
"dive into python3" filetype:pdf

and you got it!

regards
Michael

* Anonymous Group  [2012-08-22 03:40]:
> What books do you recomend for learning python? Preferably free and/or
> online.
> -- 
> http://mail.python.org/mailman/listinfo/python-list

-- 
Michael Poeltl
Computational Materials Physics  voice: +43-1-4277-51409
Univ. Wien, Sensengasse 8/12 fax:   +43-1-4277-9514 (or 9513) 
A-1090 Wien, AUSTRIA   cmp.mpi.univie.ac.at 
---
ubuntu-11.10 | vim-7.3 | python-3.2.2 | mutt-1.5.21 | elinks-0.12
---
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: asking

2012-08-22 Thread mingqiang hu
I mean any of "a","b","c" in string "adfbdfc"  makes the statement true,can
I not use a function?  suppose I got lots of substring let's say
s1="a",s2="b",s3="c" ...,not wrap them as a tuple or a list , just make the
statement as simple as possible to check if any of the value is the
substring of S="fasfasdfgbefve".

On Wed, Aug 22, 2012 at 1:17 PM, Dave Angel  wrote:

> On 08/22/2012 12:17 AM, Ian Foote wrote:
> > Oops, hopefully this with indent correctly:
> >
> > def all_in(string, substrings):
> > for substring in substrings:
> > if substring not in string:
> > return False
> > return True
>
> The POP's question was ambiguous (did he want to match any of the
> substrings, or all of the substrings), but his example code:
>
>
> ("a" in "adfbdfc")  or ( "b" in "adfbdfc") or ("c" in "adfbdfc" )
>
> implements the opposite sense of what you have.  So perhaps he'd want:
>
>
> def any_in(string, substrings):
> for substring in substrings:
> if substring in string:
>   return True:
> return False
>
>
> --
>
> DaveA
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: something about split()???

2012-08-22 Thread Mark Lawrence

On 22/08/2012 06:46, Terry Reedy wrote:

On 8/21/2012 11:43 PM, mingqiang hu wrote:

why filter is bad when use lambda ?


Inefficient, not 'bad'. Because the equivalent comprehension or
generator expression does not require a function call.



A case of premature optimisation? :)

--
Cheers.

Mark Lawrence.

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


Re: How to set the socket type and the protocol of a socket using create_connection?

2012-08-22 Thread Guillaume Comte
Le mercredi 22 août 2012 04:10:43 UTC+2, Dennis Lee Bieber a écrit :
> On Tue, 21 Aug 2012 10:00:28 -0700 (PDT), Guillaume Comte
> 
>  declaimed the following in
> 
> gmane.comp.python.general:
> 
> 
> 
>   A later follow-up
> 
> > Unfortunatly, my_socket.bind((src_addr, 1)) doesn't work. I get the error 
> > message: "socket.error: [Errno 49] Can't assign requested address"...
> 
> > 
> 
> 
> 
>   Since .bind() is used to set up a /listener/, the network stack
> 
> (LINK layer) probably has to be tied to the IP address; that is, a valid
> 
> "source" address needs to be supplied to .bind.
> 
Do you mean that an alias is not a valid source address? Because ping.c can do 
it...

I've also tried changing the port to  or 8080 but the same error happens.
> 
> 
> 
> 
> > But when I try to set a source address, I still get the same error 
> > message...
> 
> > 
> 
> > Does anyone know how I could build the IP header (I don't even know if it 
> > can be the solution but it's worth a try...)?
> 
> 
> 
>   Based upon http://linux.die.net/man/7/raw "Raw sockets allow new
> 
> IPv4 protocols to be implemented in user space. A raw socket receives or
> 
> sends the raw datagram not including link level headers. " implies that
> 
> you need to build the entire IP packet for your ICMP message... That
> 
> means you do NOT use socket methods to set fields -- you'll probably
> 
> have to use something like the struct module to lay out the entire IP
> 
> packet /including/ header contents, and then pass that as-is to the
> 
> socket.
> 
> 
> 
> -- 
> 
>   Wulfraed Dennis Lee Bieber AF6VN
> 
> wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/

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


Re: Top-posting &c. (was Re: [ANNC] pybotwar-0.8)

2012-08-22 Thread Mark Lawrence

On 22/08/2012 07:25, Bob Martin wrote:

in 679182 20120821 181439 Dennis Lee Bieber  wrote:

On Tue, 21 Aug 2012 08:07:33 +0200, Alex Strickland 
declaimed the following in gmane.comp.python.general:


On 2012/08/17 12:42 AM, Madison May wrote:


As a lurker, I agree completely with Chris's sentiments.


I too, but I'd prefer something top-posted than have to skip through 38
pages of quoted e-mail to get to a (generally) 1 liner at the bottom.


Doesn't help me though... Agent shows quoted material as blue, fresh
text as black.

I tend to not see a one-liner at the top (since it is next to the
attribution line) and if the rest of the page is all blue text I hit
page down... and down, down, down... looking for black text... Then end
up going "Wha', where's the new stuff?" and having to scroll back up to
find a one-liner cuddling up with the attribution line.


Yep, and the only solution is for everyone to top-post.



The only solution is for people to use common sense.  At least one snag 
is that Voltaire said common sense is not so common.


--
Cheers.

Mark Lawrence.

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


Re: help me debug my "word capitalizer" script

2012-08-22 Thread Andreas Perstinger

On 22.08.2012 08:21, Santosh Kumar wrote:

with open(givenfile) as file:
 # List to store the capitalised lines.
 lines = []
 for line in file:
 # Split words by spaces.
 words = line.split(' ')


The last element in your "words" list will still have a newline 
character appended to it.

You could probably use line.split().
See also the docs:
http://docs.python.org/py3k/library/stdtypes.html#str.split


 for i, word in enumerate(words):
 if len(word.strip(punctuation)) > 3:
 # Capitalise and replace words longer than 3 (without
punctuation)
 words[i] = word.capitalize()
 # Join the capitalised words with spaces.
 lines.append(' '.join(words))


This rebuilds the line including a newline character at the end.


 # Join the capitalised lines by the line separator
 capitalised = linesep.join(lines)


Because you haven't removed the newline character from each line, 
joining them with "linesep" introduces a second newline character after 
each line.


Bye, Andreas
--
http://mail.python.org/mailman/listinfo/python-list


Re: Books?

2012-08-22 Thread Mark Lawrence

On 22/08/2012 02:36, Anonymous Group wrote:

What books do you recomend for learning python? Preferably free and/or
online.



Search for the Alan Gauld tutorial.  I've never used it myself, but OTOH 
I've never heard anybody complain about it!!!


As someone else has already mentioned it, I'd highly recommend Dive Into 
Python


--
Cheers.

Mark Lawrence.

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


Re: help me debug my "word capitalizer" script

2012-08-22 Thread Chris Angelico
On Wed, Aug 22, 2012 at 4:21 PM, Santosh Kumar  wrote:
> Purpose of the script:
> To capitalize the first letter of any word in a given file, leaving
> words which have 3 or less letters.
>
> Bugs:
> I know it has many bugs or/and it can be improved by cutting down the
> code, but my current focus is to fix this bug:
>   1. When I pass it any file, it does it stuff but inserts a blank
> line everytime it processes a new line. (Please notice that I don't
> want the output in an another file, I want it on screen).

Firstly, THANK YOU for making your question so easy to work with!

I'll start with a quick answer to your immediate question, then add
some other comments about the code.

If you print(repr(words[-1])) somewhere in there, you'll notice that
the lines you're iterating over actually have a \n at the end. That's
where your duplication is coming from; you're adding a line separator,
but one's already being added for you.

Since you're working in text, you can just let \n be your line
separator; input and output should both translate as required.
Probably the easiest fix is to simply ignore os.linesep and keep the
newlines on the last words of the lines; all you care about is the
beginning of the word. However, this might raise another odd issue, in
that a two-letter word at the end of a line would be capitalized. This
can be fixed by adding '\n' to the set of characters that you strip.

Alternatively, trim off the newlines and then join then back in again.
Not difficult and I'm sure you'll see where that needs to be done :)

So! Unsolicited comments follow.

> Here is the script I am using:
>
> from os import linesep
> from string import punctuation
> from sys import argv

I would be inclined instead to simply "import os" "import string"
"import sys" and then use the qualified names. It's not something
you're doing a lot of - each of these is used in precisely one place
in the script - so the adorned names aren't going to get much in the
way.

> script, givenfile = argv
>
> with open(givenfile) as file:

Presumably you don't need a full-on arg parser. I'd actually just
inline this as "open(argv[1])" rather than unpack into separate
variables; that way, if the user provides more args, the extras will
be ignored (instead of throwing an exception).

Something you may want to look into at some point is list
comprehensions. Whenever you have a loop that iterates over a list and
builds another list, consider a more functional notation. For
instance, your inner loop could become:

words = [(word.capitalize if len(word.strip(punctuation)) > 3 else
word) for word in line.split(' ')]

The parentheses are optional, but may help you see how the interpreter
parses that.

Code looks pretty good though, and again, a big thank you for making
your question so clear :) It's a pleasure to help you.

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


Re: Reimporting modules, and sandboxing?

2012-08-22 Thread Mark Lawrence

On 21/08/2012 22:51, Dan Stromberg wrote:

I know I've seen this discussed before, and I came away from observing the
discussion thinking "Python doesn't do that very well...", but we have some
people here who really would like to do this, and I need to better
understand the pros and cons now.

Is there a good way of reimporting an _independent_ set of CPython modules?

I'm aware of these issues:
http://stackoverflow.com/questions/1254370/reimport-a-module-in-python-while-interactive
http://docs.python.org/library/functions.html#reload

Are there 
more?

Does sandboxing Python code help in some way?  If yes, then what kind of
sandbox?  Note that this isn't sandboxing to prevent execution of malicious
code, it's sandboxing to preserve reimportability, which may be an easier
constraint to satisfy.  The modules are friendly to each other, they aren't
trying to break into each other, we just have to be careful that they don't
step on each other's ability to reimport accidentally.

Although I love Pypy, I don't think we'll be able to use Pypy sandboxes
this time around.

Also, if we need to reload 3 modules at once, can reload() reimport them
atomically, as a set?  Or would it need to do them one at a time?

In short, we're talking about having some code that allows automatically
re-importing changed modules.  These modules will be pretty independent of
each other in the Python world, but might interact with each other in the
REST world even though they're written in CPython.

Thanks!





As there are no other replies, and my apologies if I'm teaching granny 
to suck eggs, but there are a load of threads on python-dev discussing 
sandboxing.  Whether there are any tie ins with importing is for me not 
to know and for you to find out :) I'll also wonder out loud if the 
reworking of the import mechanism for 3.3 has any impact at all on your 
question.


--
Cheers.

Mark Lawrence.

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


Re: help me debug my "word capitalizer" script

2012-08-22 Thread Hans Mulder
On 22/08/12 08:21:47, Santosh Kumar wrote:
> Here is the script I am using:
> 
> from os import linesep
> from string import punctuation
> from sys import argv
> 
> script, givenfile = argv
> 
> with open(givenfile) as file:
> # List to store the capitalised lines.
> lines = []
> for line in file:
> # Split words by spaces.
> words = line.split(' ')
> for i, word in enumerate(words):
> if len(word.strip(punctuation)) > 3:
> # Capitalise and replace words longer than 3 (without
> punctuation)
> words[i] = word.capitalize()
> # Join the capitalised words with spaces.
> lines.append(' '.join(words))
> # Join the capitalised lines by the line separator
> capitalised = linesep.join(lines)
> # Optionally, write the capitalised words back to the file.
> 
> print(capitalised)
> 
> 
> Purpose of the script:
> To capitalize the first letter of any word in a given file, leaving
> words which have 3 or less letters.
> 
> Bugs:
> I know it has many bugs or/and it can be improved by cutting down the
> code, but my current focus is to fix this bug:
>   1. When I pass it any file, it does it stuff but inserts a blank
> line every time it processes a new line. (Please notice that I don't
> want the output in an another file, I want it on screen).

The lines you read from your input file end in a line separator.
When you print them, the 'print' command adds another line separator.
This results in two line separators in a row, in other words, a blank
line.

The best way to solve this is usually to remove the line separator
right after you've read in the line.  You could do that by inserting
after line 10:

line = line.rstrip()

That will remove all whitespace characters (spaces, tabs, carriage
returns, newlines) from the end of the line.

Alternatively, if you want to remove only the line separator,
you could do:

if line.endswith(linesep):
line = line[:-len(linesep)]

The 'if' command is only necessary for the last line, which may or
may not end in a linesep.  All earlier lines are guaranteed to end
with a linesep.


Hope this helps,

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


Re: asking

2012-08-22 Thread Dave Angel
On 08/22/2012 03:17 AM, mingqiang hu wrote:
> I mean any of "a","b","c" in string "adfbdfc"  makes the statement true,can
> I not use a function?  suppose I got lots of substring let's say
> s1="a",s2="b",s3="c" ...,not wrap them as a tuple or a list , just make the
> statement as simple as possible to check if any of the value is the
> substring of S="fasfasdfgbefve".
> 
You top-posted.  And you're not bothering to really read the responses
you're getting.  Your problem statement is still ambiguous, and you're
really asking for someone to write a custom language for you.  So I'm
done here.




-- 

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


Re: How to set the socket type and the protocol of a socket using create_connection?

2012-08-22 Thread Guillaume Comte
I've managed to build the IP header. I've put the source and destination 
addresses in this header but it doesn't change the real source address...

I'm trying to read the ping source code but I'm lost...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to set the socket type and the protocol of a socket using create_connection?

2012-08-22 Thread Hans Mulder
On 22/08/12 09:29:37, Guillaume Comte wrote:
> Le mercredi 22 août 2012 04:10:43 UTC+2, Dennis Lee Bieber a écrit :
>> On Tue, 21 Aug 2012 10:00:28 -0700 (PDT), Guillaume Comte
>>  declaimed the following in
>> gmane.comp.python.general:

>>  A later follow-up

>>> Unfortunately, my_socket.bind((src_addr, 1)) doesn't work. I get the
>>> error message: "socket.error: [Errno 49] Can't assign requested address"...

>>  Since .bind() is used to set up a /listener/, the network stack

Not necessarily.  That is, a listener is normally  bound to a
specific port, since otherwise the client doesn't know what port
to connect to (unless you provide that factoid on another port).

But nothing prevents a client from binding its socket to a
specific port number.  These days, that doesn't buy you much,
but in the old days, some services would only talk to clients
using privileged port numbers (<1024).

>> (LINK layer) probably has to be tied to the IP address; that is, a valid
>> "source" address needs to be supplied to .bind.

> Do you mean that an alias is not a valid source address? Because ping.c can 
> do it...

> I've also tried changing the port to  or 8080 but the same error happens.

On my laptop, 0 appears to be the only port number that bind accepts
for a raw socket.  Other numbers I tried all raise "socket.error:
[Errno 49] Can't assign requested address".

But this might depend on your OS.  What OS are you using?

Hope this helps,

-- HansM

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


Re: How to set the socket type and the protocol of a socket using create_connection?

2012-08-22 Thread Guillaume Comte
Le mercredi 22 août 2012 11:03:11 UTC+2, Hans Mulder a écrit :

> 
> On my laptop, 0 appears to be the only port number that bind accepts
> 
> for a raw socket.  Other numbers I tried all raise "socket.error:
> 
> [Errno 49] Can't assign requested address".
> 
> 
> 
> But this might depend on your OS.  What OS are you using?
> 

I'm using FreeBSD 7.3
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: asking

2012-08-22 Thread alex23
On 08/22/2012 03:17 AM, mingqiang hu wrote:
> I mean any of "a","b","c" in string "adfbdfc"  makes the statement true,can
> I not use a function?

any(map(string.__contains__, substrings))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Books?

2012-08-22 Thread Jamie Paul Griffin
[ Mark Lawrence wrote on Wed 22.Aug'12 at  8:43:58 +0100 ]

> On 22/08/2012 02:36, Anonymous Group wrote:
> > What books do you recomend for learning python? Preferably free and/or
> > online.
> >
> 
> Search for the Alan Gauld tutorial.  I've never used it myself, but OTOH 
> I've never heard anybody complain about it!!!
> 
> As someone else has already mentioned it, I'd highly recommend Dive Into 
> Python

I bought "Beginning Python - Using Python 2.6 and 3.1" by James Payne, 
published by Wrox www.wrox.com. I've found it quite good so far, it's pretty 
comprehensive.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help me debug my "word capitalizer" script

2012-08-22 Thread Kamil Kuduk
> Purpose of the script:
> To capitalize the first letter of any word in a given file, leaving
> words which have 3 or less letters.

First or all? If first and this is the only purpose of the script you
can easily use sed:
less file.txt | sed -e "s/\b\([a-z]\{4,\}\)/\u\1/g"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help me debug my "word capitalizer" script

2012-08-22 Thread Chris Angelico
On Wed, Aug 22, 2012 at 8:28 PM, Kamil Kuduk  wrote:
>> Purpose of the script:
>> To capitalize the first letter of any word in a given file, leaving
>> words which have 3 or less letters.
>
> First or all? If first and this is the only purpose of the script you
> can easily use sed:
> less file.txt | sed -e "s/\b\([a-z]\{4,\}\)/\u\1/g"

Why less? Why not just redirect input?

Though, this isn't really on topic for Python.

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


Re: help me debug my "word capitalizer" script

2012-08-22 Thread Kamil Kuduk
On Wed, Aug 22, 2012 at 12:41 PM, Chris Angelico  wrote:
> Why less? Why not just redirect input?
Yeah, my bad, I somehow used to do it, for grep too, and I know that
this is slower

> Though, this isn't really on topic for Python.

I would still go with regexp, something like:

with open('myfile.txt', 'rw) as file:
 print "".join(re.sub(r'\w{3,}', lambda(match):
match.group(0).title(), line) for line in file)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: color coding for numbers

2012-08-22 Thread Ulrich Eckhardt

Am 21.08.2012 19:07, schrieb DJC:

On 21/08/12 12:55, Ulrich Eckhardt wrote:

Am 21.08.2012 10:38, schrieb namenobodywa...@gmail.com:

what is the best way


Define "best" before asking such questions. ;)





Sorry, that one must have been unclear. The point was that when asking 
for a _best_ solution to a problem, the criteria for evaluating a 
solution must be known. If you don't define them and they are not 
implicit, there is no possible answer to the question.


Uli

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


Re: asking

2012-08-22 Thread Tim Chase
On 08/22/12 04:42, alex23 wrote:
> On 08/22/2012 03:17 AM, mingqiang hu wrote:
>> I mean any of "a","b","c" in string "adfbdfc"  makes the statement true,can
>> I not use a function?
> 
> any(map(string.__contains__, substrings))

As map()/reduce() vs. list-comprehension discussions are going on in
another thread, I find it more readable to write

  any(letter in "abfbdfc" for letter in "abc")

or

  all(letter in "abfbdfc" for letter in "abc")

depending on what the OP wants (a matter clarity which the OP seems
to be a little vague on).

Alternatively, if they're actual sub-strings rather than just
letters, it can be written as

  substrings = [
"abf",
"bfb",
"fbd",
]
  target = "abfdbfc"
  any(substring in target for substring in substrings)
  all(substring in target for substring in substrings)

which is about as close to English as it gets.

If the target is exceptionally long, I'd be tempted to go with
Terry's suggestion of a regular expression which should be able to
do the checks in one search pass of the target haystack.

-tkc






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


psphere: how to make thread safe

2012-08-22 Thread sajuptpm
Hi,

psphere: Python interface for the VMware vSphere Web Services SDK

I already developed an app using https://bitbucket.org/jkinred/psphere. But 
getting lot of errors since psphere is not thread safe (I think). So i wrote 
couple of scripts to test it (See attached files) and found that caching 
mechanism used by psphere is not thread safe. Could someone please give me some 
suggestion to make it thread safe.


===Test Code 

import psphere
from psphere.client import Client
from psphere.managedobjects import HostSystem, VirtualMachine, ComputeResource
client = Client("192.168.0.114", "root", "vmware1") ##vCenter
print "\nSucessfully connected to vCenter.\n"

from threading import Thread

def myfunc(i):
host1 = HostSystem.get(client, name="192.168.0.134")
host2 = HostSystem.get(client, name="192.168.0.113")
print "i--",i
while True:
#host1.update(properties=["config", "vm"])
#host2.update(properties=["config", "vm"])
c = type(host1.config.network)
v = type(host2.config.network)
for vm in host1.vm:
k = vm.config.template
for vm in host2.vm:
p = vm.config.template


for i in range(10): 
t = Thread(target=myfunc, args=(i,))
t.start()


"""
OUTPUT 
===
Sucessfully connected to vCenter.

i-- 1
i-- 3
i-- 5
i-- 0
 i-- 4
i-- 2i-- 7
i-- 
9
i-- 6
i-- 8
Exception in thread Thread-4:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
  File "/usr/lib/python2.7/threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
  File "vcenter_test1.py", line 19, in myfunc
k = vm.config.template
  File "/home/saju/cvt/trunk/src/cvt/web/cvt/psphere/__init__.py", line 79, in 
__get__
value = self.fget(inst)
  File "/home/saju/cvt/trunk/src/cvt/web/cvt/psphere/managedobjects.py", line 
1236, in config
return self._get_dataobject("config", False)
  File "/home/saju/cvt/trunk/src/cvt/web/cvt/psphere/__init__.py", line 116, in 
_get_dataobject
return self._cache[name][0]
KeyError: 'config'

Exception in thread Thread-6:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
  File "/usr/lib/python2.7/threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
  File "vcenter_test1.py", line 17, in myfunc
v = type(host2.config.network)
AttributeError: VirtualMachineConfigInfo instance has no attribute 'network'



"""

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


Re: help me debug my "word capitalizer" script

2012-08-22 Thread Santosh Kumar
OK! The bug one fixed. Thanks to Andreas Perstinger.

Let's move to Bug #2:
 2. How do I escape the words that are already in uppercase? For example:

The input file has this:
NASA

The script changes this to:
Nasa

Is it possible to make this script look at a word, see if its first
character is capitalized, if capitalized then skip that word. If not
do the processing? I don't wan to use regex? Do I need it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: asking

2012-08-22 Thread Steven D'Aprano
On Wed, 22 Aug 2012 02:42:16 -0700, alex23 wrote:

> On 08/22/2012 03:17 AM, mingqiang hu wrote:
>> I mean any of "a","b","c" in string "adfbdfc"  makes the statement
>> true,can I not use a function?
> 
> any(map(string.__contains__, substrings))

Nice.

However, be aware that in Python 2, map() is eager and produces a full 
list ahead of time. In Python 3, map() is lazy and only produces results 
as needed. Since any returns as soon as it sees a true value, Python 3 
can be significantly faster here under some circumstances. For example, 
consider:

string = "a"*1000
substrings = "abcdefghijklmnopqrstuvwxyz"

map(string.__contains__, substrings) in Python 2 produces a list of 26 
values:

[True, False, False, False, False, ..., False]

up front, before passing that list to any() which stops as soon as it 
sees the first value. Generating those 25 False values was a waste of 
time.

In Python 3, map() produces a lazy iterator object that doesn't calculate 
the values until required. So map(string.__contains__, substrings) 
doesn't do anything straight away. It waits for any() to request a value, 
then returns True. any() then immediately returns, and the other 25 False 
values never get calculated.

In Python 2, you can use itertools.imap for a lazy version. In Python 3, 
you can use list(map( ... )) for an eager version.

Here is a version which is lazy in both Python 2 and 3. I expect it to be 
more or less equally as efficient, and I find it easier to read:

any(sub in string for sub in substrings)

There's also an all() function that works similarly to any(), except that 
it stops as soon as it sees a false value.


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


Re: xlrd 0.8.0 released!

2012-08-22 Thread Hubert Holin
La Défense, le 22/08/2012

Hi

Congratulations for the work well done, and thanks for the help xlrd brings 
to my work.

I would like to keep up with the development but would like to know which 
is the repo to follow. The Python-Excel website points to 
https://github.com/python-excel/xlrd, but that one does not have a 0.8.0 
tag (or at least did not have one when I looked a few days ago, and 
I unfortunately can't check now, from work).

Merci

Hubert Holin

Le mercredi 1 août 2012 17:01:56 UTC+2, Chris Withers a écrit :
>
> Hi All, 
>
> I'm pleased to announce the release of xlrd 0.8.0: 
>
> http://pypi.python.org/pypi/xlrd/0.8.0 
>
> This release finally lands the support for both .xls and .xlsx files. 
> Many thanks to John Machin for all his work on making this happen. 
> Opening of .xlsx files is seamless, just use xlrd as you did before and 
> it all should "just work". 
>
> xlrd 0.8.0 is also the first release that that targets Python 2.6 and 
> 2.7, but no Python 3 just yet. Python 2.5 and below may work but are not 
> supported. If you need to use Python 2.5 or earlier, please stick to 
> xlrd 0.7.x. 
>
> Speaking of xlrd 0.7.x, that's now in "requested maintenance only" mode 
> ;-) That means, if possible, use 0.8.x. If you have a really good reason 
> for sticking with 0.7.x, and you find a bug that you can't work around, 
> then please make this clear on the python...@googlegroups.comand 
> we'll see what we can do. 
>
> If you find any problems, please ask about them on the list, or submit 
> an issue on GitHub: 
>
> https://github.com/python-excel/xlrd/issues 
>
> Full details of all things Python and Excel related can be found here: 
>
> http://www.python-excel.org/ 
>
> cheers, 
>
> Chris 
>
> -- 
> Simplistix - Content Management, Batch Processing & Python Consulting 
>  - http://www.simplistix.co.uk 
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Books?

2012-08-22 Thread Steven D'Aprano
On Tue, 21 Aug 2012 18:36:50 -0700, Anonymous Group wrote:

> What books do you recomend for learning python? Preferably free and/or
> online.

Completely by coincidence, I have just discovered, and I mean *literally* 
just a few minutes ago, this book:

http://www.springer.com/mathematics/computational+science+%26+engineering/book/978-3-642-30292-3

http://codingcat.com/knjige/python/A%20Primer%20on%20Scientific%20Programming%20with%20Python.pdf


I wish it had existed when I was a beginner! I haven't read the whole 
thing, but dammit it looks like exactly the sort of book I would have 
adored as a newbie. (Your mileage may vary.)



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


Re: help me debug my "word capitalizer" script

2012-08-22 Thread Joel Goldstick
On Wed, Aug 22, 2012 at 9:00 AM, Santosh Kumar  wrote:
> OK! The bug one fixed. Thanks to Andreas Perstinger.
>
> Let's move to Bug #2:
>  2. How do I escape the words that are already in uppercase? For example:
>
> The input file has this:
> NASA
>
> The script changes this to:
> Nasa
>
> Is it possible to make this script look at a word, see if its first
> character is capitalized, if capitalized then skip that word. If not
> do the processing? I don't wan to use regex? Do I need it?
> --
> http://mail.python.org/mailman/listinfo/python-list

Go into your python shell and type help(str) or lookup the
documentation on the python site about strings
http://docs.python.org/library/string.html

There are methods to tell if a word is all caps, or if the first
letter is capitalized

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


Re: Objects in Python

2012-08-22 Thread Joel Goldstick
On Wed, Aug 22, 2012 at 10:13 AM, shaun  wrote:
> I'm having an issue its my first time using python and i set up a class one 
> of the methods is supposed to return a string but instead returns:
>
>  389E0>>
>
> Im very new to python and the object orientated feature doesnt seem to be as 
> well put together as Java. Can anyone help with this problem?
> --
> http://mail.python.org/mailman/listinfo/python-list

It looks like you didn't add parens to the end of your call.  Show us
your code. with the traceback

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


Re: xlrd 0.8.0 released!

2012-08-22 Thread Chris Withers

On 22/08/2012 15:03, Hubert Holin wrote:

I would like to keep up with the development but would like to know
which is the repo to follow. The Python-Excel website points to
https://github.com/python-excel/xlrd, but that one does not have a 0.8.0
tag (or at least did not have one when I looked a few days ago, and I
unfortunately can't check now, from work).


Oops, I was very bad... not only did I forget to push the tag (it's 
annoying that you have to do a "git push --tags" to get tasg pushed) but 
I actually forgot to commit the final change that stamped on the 0.8.0 
version number :-S


Fixed and pushed now, thanks for noticing :-)

Chris

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


Re: Objects in Python

2012-08-22 Thread Jussi Piitulainen
shaun writes:

> I'm having an issue its my first time using python and i set up a
> class one of the methods is supposed to return a string but instead
> returns:
> 
>  389E0>>
> 
> Im very new to python and the object orientated feature doesnt seem
> to be as well put together as Java. Can anyone help with this
> problem?

I bet you are trying to call the method, returnString, without the
parentheses that enclose the parameters (and without any @property
stuff in the class).

>>> Para('dox').myWev
>
>>> Para('dox').myWev()
'dox'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread Peter Otten
shaun wrote:

> I'm having an issue its my first time using python and i set up a class
> one of the methods is supposed to return a string but instead returns:
> 
>  389E0>>
> 
> Im very new to python and the object orientated feature doesnt seem to be
> as well put together as Java. 

It's definitely too early for you to draw conclusions ;)

> Can anyone help with this problem?

You have successfully created a bound method, now you need to invoke it:

>>> class Param(object):
... def returnString(self):
... return "hello"
... 
>>> p = Param()
>>> p.returnString
>
>>> p.returnString()
'hello'

Unlike some other langages Python does not implicitly invoke functions or 
methods. That makes it easy to pass them around like any other object.




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


Re: help me debug my "word capitalizer" script

2012-08-22 Thread Rebelo

> Let's move to Bug #2:
> 
>  2. How do I escape the words that are already in uppercase? For example:
> 
> 
> 
> The input file has this:
> 
> NASA
> 
> 
> 
> The script changes this to:
> 
> Nasa
> 
> 
> 
> Is it possible to make this script look at a word, see if its first
> 
> character is capitalized, if capitalized then skip that word. If not
> 
> do the processing? I don't wan to use regex? Do I need it?

change: 
 words[i] = word.capitalize() 

into:
if word != word.upper() :
 words[i] = word.capitalize() 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread lipska the kat

On 22/08/12 15:13, shaun wrote:

[snip]


Im very new to python and the object orientated feature doesnt seem to be as 
well put together as Java. Can anyone help with this problem?


From one Java head to another I suggest you park what you know about 
Java and approach Python with a clear mind.


Python is not Java and Java is not Python, that much has become clear.
Python has actually been around longer than Java and contains many 
features you will be familiar with, serialization and introspection to 
name but two. The whole 'everything is an object' thing is a bit strange 
at first but actually it just means that everything you write is wrapped 
up in a component that exposes various standard methods and attributes, 
you treat functions as Objects and modules as Objects and even your 
classes will automagically sprout new attributes and properties, at 
least that's what I've discovered so far.


There is no real enforced concept of information hiding, no binding of 
type to variable in fact no concept of typing at all as far as I can 
see. No interfaces and no subtype polymorphism (Python has 'Duck Type' 
polymorphism and I haven't really explored all the ramifications of this 
yet). It does however have multiple inheritance.


In trying to get a handle on the language it has helped me to think of 
Python as a friendly interface onto the C programming language, it may 
or may not help you


There are some very experienced pythonistas here and I'm sure you will 
get the help you need. There is a tutor mailing list and a great first 
starter is Dive into Python (google it)


I can't say that Python will replace Java for me, I've been using Java 
since version 1, but it's got a good standard library and good support 
here and on the mailing list ... and it supports Unicode :-)


I like it, give it a chance and you will probably like it too.

lipska

--
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun
--
http://mail.python.org/mailman/listinfo/python-list


Re: Books?

2012-08-22 Thread Virgil Stokes

On 22-Aug-2012 16:04, Steven D'Aprano wrote:

On Tue, 21 Aug 2012 18:36:50 -0700, Anonymous Group wrote:


What books do you recomend for learning python? Preferably free and/or
online.

Completely by coincidence, I have just discovered, and I mean *literally*
just a few minutes ago, this book:

http://www.springer.com/mathematics/computational+science+%26+engineering/book/978-3-642-30292-3

http://codingcat.com/knjige/python/A%20Primer%20on%20Scientific%20Programming%20with%20Python.pdf


I wish it had existed when I was a beginner! I haven't read the whole
thing, but dammit it looks like exactly the sort of book I would have
adored as a newbie. (Your mileage may vary.)



I second this --- this is a very good book IMHO. I have the first edition (2009) 
and have found it very useful.


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


Re: Objects in Python

2012-08-22 Thread John Gordon
In <18409992-1e28-4721-8e64-60c69668d...@googlegroups.com> shaun 
 writes:

> I'm having an issue its my first time using python and i set up a class one 
> of the methods is supposed to return a string but instead returns:

>  389E0>>

It looks like you're referencing the method object itself, instead of
calling it method.  In other words, you've left off the parentheses.

I.e. you're doing something like this:

print my_object.foo

Instead of this:

print my_object.foo()

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: Objects in Python

2012-08-22 Thread shaun
Here is some code: 
//This is the object I want to create:
#!/usr/bin/python
import cx_Oracle
import sys
import time
import datetime


class batchParam:

def __init__(self,array):
self.array=array


def breakuparray(self):
for row in self.array:
mer = row[0].ljust(25, ' ')
merc = row[1].ljust(13, ' ')
mertype = row[2]
merloc = row[3]
mercount = row[4]
mersec = row[5]
acq = row[6]



def returnBatch(self):
self.breakuparray()
return "\x01001\x0251.%s%s%s%s%s%s%s%s\x03"  % (mer, merc, 
mertype, merloc, mercount, mersec, acq);


//Here is the script I want to run the 
object in:


#!/usr/bin/python
import cx_Oracle
import sys
import time
import datetime
sys.path.append("C:\\Documents and Settings\\swiseman\\Desktop")
from batchParam import batchParam

term = sys.argv[1]
batch = sys.argv[2]

con = cx_Oracle.connect('databaseInfo')


cur = con.cursor()
cur.execute("SELECT * FROM SOME_TABLE))

results = cur.fetchall()

batchParam(results)
Batch=batchParam.returnBatch

print Batch

cur.close()

//

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


Re: help me debug my "word capitalizer" script

2012-08-22 Thread MRAB

On 22/08/2012 09:20, Hans Mulder wrote:
[snip]


Alternatively, if you want to remove only the line separator,
you could do:

 if line.endswith(linesep):
 line = line[:-len(linesep)]

The 'if' command is only necessary for the last line, which may or
may not end in a linesep.  All earlier lines are guaranteed to end
with a linesep.


Even better is:

line = line.rstrip(linesep)

The line separator is '\n'.

Strictly speaking, the line separator varies according to platform
(Windows, *nix, etc), but it's translated to '\n' on reading from a
file which has been opened in text mode (the default).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread Chris Angelico
On Thu, Aug 23, 2012 at 1:25 AM, shaun  wrote:
> def breakuparray(self):
> for row in self.array:
> mer = row[0].ljust(25, ' ')
> merc = row[1].ljust(13, ' ')
> mertype = row[2]
> merloc = row[3]
> mercount = row[4]
> mersec = row[5]
> acq = row[6]

The "for ... in ..." construct is a loop. I'm not sure what you're
trying to accomplish here, but you're taking the last entry in
self.array and unpacking that as a nested array. Perhaps not what you
had in mind.

For what you're doing there, though, a class is overkill. Remember,
Python isn't Java; the most natural way to do everything isn't
necessarily to write a class that unpacks things and packs them up
again in a different way.

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


Re: Objects in Python

2012-08-22 Thread Dave Angel
On 08/22/2012 11:25 AM, shaun wrote:
> Here is some code: 
> //This is the object I want to create:
> #!/usr/bin/python
> import cx_Oracle
> import sys
> import time
> import datetime
>
>
> class batchParam:
>
>   def __init__(self,array):
>   self.array=array
>
>
>   def breakuparray(self):
>   for row in self.array:
>   mer = row[0].ljust(25, ' ')
>   merc = row[1].ljust(13, ' ')
>   mertype = row[2]
>   merloc = row[3]
>   mercount = row[4]
>   mersec = row[5]
>   acq = row[6]
>   
>   
>   
>   def returnBatch(self):
>   self.breakuparray()
>   return "\x01001\x0251.%s%s%s%s%s%s%s%s\x03"  % (mer, merc, 
> mertype, merloc, mercount, mersec, acq);
>
>
> //Here is the script I want to run the 
> object in:
>
>
> #!/usr/bin/python
> import cx_Oracle
> import sys
> import time
> import datetime
> sys.path.append("C:\\Documents and Settings\\swiseman\\Desktop")
> from batchParam import batchParam
>
> term = sys.argv[1]
> batch = sys.argv[2]
>
> con = cx_Oracle.connect('databaseInfo')
>
>
> cur = con.cursor()
> cur.execute("SELECT * FROM SOME_TABLE))
>
> results = cur.fetchall()
>
> batchParam(results)

This creates an instance of batchParam, but doesn't save it anywhere. 
So it's discarded immediately.

> Batch=batchParam.returnBatch
This tries to returns a reference to a static method of the class. 
Without an object, you won't get access to normal instance methods;
there's no 'self'.  And without parentheses, you won't even try to call
the method, right or wrong.

Probably you wanted something like:

obj = batchParam(results)   #now obj is an instance
mystring = obj.returnBatch()#calls the method, and saves the
returned string
print mystring

>
> print Batch
>
> cur.close()
>
>

Other comments:   Don't make the mistake of forcing every class into its
own source file.  Unlike java, python has no such restrictions.  It also
has ordinary functions, not part of any class.   So if several classes
are related, go ahead and put them in a common file.  Or keep them
separate, Python doesn't mind.

There are capitalization conventions:  class names start with a capital
letter, and source code filenames do not.  So the class you've got in 
batchParam could be called BatchParam.

Neither of these matter much, but they make it easier for someone else
to see what you were trying to do.

It would also be helpful if you posted the complete error message (with
traceback), so we could more easily guess where in the code the problem
occurs.  It can be useful to add a comment in the actual source you
post, since you have line numbers in your editor, and we don't in our
emails.  But don't try to get cute with colors, as this is a text
forum.  (that last comment may not apply to you, since you already used
a plain-text format for your message)

Python does have classmethod and staticmethod, but that's not usually
what you want, and not here.


-- 

DaveA

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


Re: Objects in Python

2012-08-22 Thread MRAB

On 22/08/2012 15:59, lipska the kat wrote:

On 22/08/12 15:13, shaun wrote:

[snip]


Im very new to python and the object orientated feature doesnt seem to be as 
well put together as Java. Can anyone help with this problem?


  From one Java head to another I suggest you park what you know about
Java and approach Python with a clear mind.

Python is not Java and Java is not Python, that much has become clear.
Python has actually been around longer than Java and contains many
features you will be familiar with, serialization and introspection to
name but two. The whole 'everything is an object' thing is a bit strange
at first but actually it just means that everything you write is wrapped
up in a component that exposes various standard methods and attributes,
you treat functions as Objects and modules as Objects and even your
classes will automagically sprout new attributes and properties, at
least that's what I've discovered so far.

There is no real enforced concept of information hiding, no binding of
type to variable in fact no concept of typing at all as far as I can
see.


strong typing != static typing

Python is strongly typed, but not statically typed.

> No interfaces and no subtype polymorphism (Python has 'Duck Type'

polymorphism and I haven't really explored all the ramifications of this
yet). It does however have multiple inheritance.


[snip]
Python doesn't have interfaces as in Java because it isn't statically
typed.

The idea behind Duck Typing is that the actual type doesn't matter; if
it supports the required method(s) and returns the expected type, then
that's good enough!

http://en.wikipedia.org/wiki/Duck_typing
--
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread Mark Lawrence

On 22/08/2012 16:47, Chris Angelico wrote:


For what you're doing there, though, a class is overkill. Remember,
Python isn't Java; the most natural way to do everything isn't
necessarily to write a class that unpacks things and packs them up
again in a different way.

ChrisA



This shows just how poor the Python documentation is.  I can't find the 
"overcoming brainwashing" section anywhere!!!


--
Cheers.

Mark Lawrence.

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


Re: Objects in Python

2012-08-22 Thread lipska the kat

On 22/08/12 16:58, MRAB wrote:

On 22/08/2012 15:59, lipska the kat wrote:

On 22/08/12 15:13, shaun wrote:

[snip]


Im very new to python and the object orientated feature doesnt seem
to be as well put together as Java. Can anyone help with this problem?


From one Java head to another I suggest you park what you know about
Java and approach Python with a clear mind.

[snip]


strong typing != static typing

Python is strongly typed, but not statically typed.

 > No interfaces and no subtype polymorphism (Python has 'Duck Type'

polymorphism and I haven't really explored all the ramifications of this
yet). It does however have multiple inheritance.



[snip]

The residents can be pretty defensive as well :-)

Once again, no criticism intended.

lipska

--
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun
--
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread Mark Lawrence

On 22/08/2012 17:10, lipska the kat wrote:

On 22/08/12 16:58, MRAB wrote:

On 22/08/2012 15:59, lipska the kat wrote:

On 22/08/12 15:13, shaun wrote:

[snip]


Im very new to python and the object orientated feature doesnt seem
to be as well put together as Java. Can anyone help with this problem?


From one Java head to another I suggest you park what you know about
Java and approach Python with a clear mind.

[snip]


strong typing != static typing

Python is strongly typed, but not statically typed.

 > No interfaces and no subtype polymorphism (Python has 'Duck Type'

polymorphism and I haven't really explored all the ramifications of this
yet). It does however have multiple inheritance.



[snip]

The residents can be pretty defensive as well :-)

Once again, no criticism intended.

lipska



I'm lost.  I see nothing defensive at all.  I see a statement of fact.

--
Cheers.

Mark Lawrence.

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


writelines puzzle

2012-08-22 Thread William R. Wing (Bill Wing)
In the middle of a longer program that reads and plots data from a log file, I 
have added the following five lines (rtt_data is fully qualified file name):

wd = open(rtt_data, 'w')
stat = wd.write(str(i))
stat = wd.writelines(str(x_dates[:i]))
stat = wd.writelines(str(y_rtt[:i]))
wd.close()

The value of i is unknown before I have read through the input log file, but is 
typically in the neighborhood of 2500.  x_dates is a list of time stamps from 
the date2num method, that is values of the form 734716.72445602, day number 
plus decimal fraction of a day.  y_rtt is a list of three- or four-digit 
floating point numbers.  The x_dates and y_rtt lists are complete and plot 
correctly using matplotlib.  Reading and parsing the input log file and 
extracting the data I need is time consuming, so I decided to save the data for 
further analysis without the overhead of reading and parsing it every time.

Much to my surprise, when I looked at the output file, it only contained 160 
characters.  Catting produces:

StraylightPro:Logs wrw$ cat RTT_monitor.dat
2354[ 734716.72185185  734716.72233796  734716.72445602 ...,  734737.4440162
  734737.45097222  734737.45766204][ 240.28.5   73.3 ...,   28.4   27.4   
26.4]

Clearly I'm missing something fundamental about using the writelines method, 
and I'm sure it will be a DUH moment for me, but I'd sure appreciate someone 
telling me how to get that data all written out.  I certainly don't insist on 
writelines, but I would like the file to be human-readable.

Python 2.7.3
OS-X 10.8

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


Filter versus comprehension (was Re: something about split()???)

2012-08-22 Thread Terry Reedy

On 8/22/2012 3:30 AM, Mark Lawrence wrote:

On 22/08/2012 06:46, Terry Reedy wrote:

On 8/21/2012 11:43 PM, mingqiang hu wrote:

why filter is bad when use lambda ?


Inefficient, not 'bad'. Because the equivalent comprehension or
generator expression does not require a function call.


for each item in the iterable.


A case of premature optimisation? :)


No, as regards my post. I simply made a factual statement without 
advocating a particular action.


filter(lambda x: , iterable)
(x for x in iterable if )

both create iterators that produce the items in iterable such that 
bool() is true. The following, with output rounded, shows 
something of the effect of the extra function call.


>>> timeit.timeit("list(i for i in ranger if False)", "ranger=range(0)")
0.91
>>> timeit.timeit("list(i for i in ranger if False)", "ranger=range(20)")
1.28
>>> timeit.timeit("list(filter(lambda i: False, ranger))", 
"ranger=range(0)")

0.83
>>> timeit.timeit("list(filter(lambda i: False, ranger))", 
"ranger=range(20)")

2.60

Simply keeping true items is faster with filter -- at least on my 
particular machine with 3.3.0b2.


>>> timeit.timeit("list(filter(None, ranger))", "ranger=range(20)")
1.03

Filter is also faster if the expression is a function call.

>>> timeit.timeit("list(filter(f, ranger))", "ranger=range(20); 
f=lambda i: False")

2.5033614114454394
>>> timeit.timeit("list(i for i in ranger if f(i))", "ranger=range(20); 
f=lambda i: False")

3.2394095327040304

---
Perhaps or even yes as regards the so-called rule 'always use 
comprehension'. If one prefers filter as more readable, if one only 
wants to keep true items, if the expression is a function call, if 
evaluating the expression takes much more time than the extra function 
call so the latter does not matter, if the number of items is few enough 
that the extra time does not matter, then the rule is not needed or even 
wrong.


So I think PyLint should be changed to stop its filter fud.

--
Terry Jan Reedy

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


Re: writelines puzzle

2012-08-22 Thread Chris Kaynor
Reading your post, I do not see for sure what your actual issue is, so
I am taking my best guess: that the file does not contain as much data
as would be expected.

On Wed, Aug 22, 2012 at 8:38 AM, William R. Wing (Bill Wing)
 wrote:
> In the middle of a longer program that reads and plots data from a log file, 
> I have added the following five lines (rtt_data is fully qualified file name):
>
> wd = open(rtt_data, 'w')

Here, you are opening the data for write ("w"), which will replace the
contents of the file each time the file is opened. I am guessing you
want append ("a").

> stat = wd.write(str(i))
> stat = wd.writelines(str(x_dates[:i]))
> stat = wd.writelines(str(y_rtt[:i]))
> wd.close()

Also, rather than opening the file, writing to it, then closing it
manually, you would be better off using the with statement (presuming
Python 2.5+), like so:

with open(rtt_data, 'w') as wd:
wd.write(str(i))
wd.writelines(str(x_dates[:i]))
wd.writelines(str(y_rtt[:i]))

In this case, you can be absolutely certain that the file will be
closed at the end, even if one of the commands in the middle fails.
The way you had it written, if one of the write/writeline, or any of
the formatting, fails, the file would be left open for an
indeterminate amount of time (the wd variable may be kept around as
part of the exception, preventing it from being garbage collected and
thus closed). Not a big deal, but more important if you are doing more
work with the file open.

>
> The value of i is unknown before I have read through the input log file, but 
> is typically in the neighborhood of 2500.  x_dates is a list of time stamps 
> from the date2num method, that is values of the form 734716.72445602, day 
> number plus decimal fraction of a day.  y_rtt is a list of three- or 
> four-digit floating point numbers.  The x_dates and y_rtt lists are complete 
> and plot correctly using matplotlib.  Reading and parsing the input log file 
> and extracting the data I need is time consuming, so I decided to save the 
> data for further analysis without the overhead of reading and parsing it 
> every time.
>
> Much to my surprise, when I looked at the output file, it only contained 160 
> characters.  Catting produces:
>
> StraylightPro:Logs wrw$ cat RTT_monitor.dat
> 2354[ 734716.72185185  734716.72233796  734716.72445602 ...,  734737.4440162
>   734737.45097222  734737.45766204][ 240.28.5   73.3 ...,   28.4   27.4   
> 26.4]
>
> Clearly I'm missing something fundamental about using the writelines method, 
> and I'm sure it will be a DUH moment for me, but I'd sure appreciate someone 
> telling me how to get that data all written out.  I certainly don't insist on 
> writelines, but I would like the file to be human-readable.
>
> Python 2.7.3
> OS-X 10.8
>
> Thanks,
> Bill
> --
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: writelines puzzle

2012-08-22 Thread Joel Goldstick
On Wed, Aug 22, 2012 at 11:38 AM, William R. Wing (Bill Wing)
 wrote:
> In the middle of a longer program that reads and plots data from a log file, 
> I have added the following five lines (rtt_data is fully qualified file name):
>
> wd = open(rtt_data, 'w')
> stat = wd.write(str(i))
> stat = wd.writelines(str(x_dates[:i]))
> stat = wd.writelines(str(y_rtt[:i]))
> wd.close()
>
> The value of i is unknown before I have read through the input log file, but 
> is typically in the neighborhood of 2500.  x_dates is a list of time stamps 
> from the date2num method, that is values of the form 734716.72445602, day 
> number plus decimal fraction of a day.  y_rtt is a list of three- or 
> four-digit floating point numbers.  The x_dates and y_rtt lists are complete 
> and plot correctly using matplotlib.  Reading and parsing the input log file 
> and extracting the data I need is time consuming, so I decided to save the 
> data for further analysis without the overhead of reading and parsing it 
> every time.
>
> Much to my surprise, when I looked at the output file, it only contained 160 
> characters.  Catting produces:
>
> StraylightPro:Logs wrw$ cat RTT_monitor.dat
> 2354[ 734716.72185185  734716.72233796  734716.72445602 ...,  734737.4440162
>   734737.45097222  734737.45766204][ 240.28.5   73.3 ...,   28.4   27.4   
> 26.4]
>
> Clearly I'm missing something fundamental about using the writelines method, 
> and I'm sure it will be a DUH moment for me, but I'd sure appreciate someone 
> telling me how to get that data all written out.  I certainly don't insist on 
> writelines, but I would like the file to be human-readable.
>
> Python 2.7.3
> OS-X 10.8
>
> Thanks,
> Bill
> --
> http://mail.python.org/mailman/listinfo/python-list

writelines writes a list of strings to a file.
you are using this:
> stat = wd.writelines(str(x_dates[:i]))
which  is the same as my second line below
If you use map it will perform the first argument over the list.
See if that works for you

>>> l = [1,2,3]
>>> str(l)
'[1, 2, 3]'
>>> s = map(str, l)
>>> s
['1', '2', '3']



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


Re: Objects in Python

2012-08-22 Thread Terry Reedy

On 8/22/2012 10:59 AM, lipska the kat wrote:


There is no real enforced concept of information hiding, no binding of
type to variable in fact no concept of typing at all as far as I can
see.


Given that type(valid_name) always returns a type(class), that is a 
slightly strange statement. What is true is that there is no concept of 
static type for names. In Python (and similar languages) type or class 
is a property of objects, not names. This goes alone with names being 
bound to objects rather than linear memory blocks. (Memory block is a 
machine implementation of the abstraction 'information object'.) And 
Python objects are more stfongly typed than in some other languages. 
Names are only dynamically and indirectly typed when they are bound to 
an object. Except for the few keyword names like None, True, etc, names 
can be rebound to another object of another type.


--
Terry Jan Reedy

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


Re: Objects in Python

2012-08-22 Thread lipska the kat

On 22/08/12 17:30, Mark Lawrence wrote:

On 22/08/2012 17:10, lipska the kat wrote:

On 22/08/12 16:58, MRAB wrote:

On 22/08/2012 15:59, lipska the kat wrote:

On 22/08/12 15:13, shaun wrote:

[snip]


Im very new to python and the object orientated feature doesnt seem
to be as well put together as Java. Can anyone help with this problem?




The residents can be pretty defensive as well :-)

Once again, no criticism intended.

lipska



I'm lost. I see nothing defensive at all. I see a statement of fact.



You seem to be perpetually lost Mark ...


lipska

--
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun
--
http://mail.python.org/mailman/listinfo/python-list


Re: writelines puzzle

2012-08-22 Thread Jerry Hill
On Wed, Aug 22, 2012 at 11:38 AM, William R. Wing (Bill Wing)
 wrote:
> Much to my surprise, when I looked at the output file, it only contained 160 
> characters.  Catting produces:
>
> StraylightPro:Logs wrw$ cat RTT_monitor.dat
> 2354[ 734716.72185185  734716.72233796  734716.72445602 ...,  734737.4440162
>   734737.45097222  734737.45766204][ 240.28.5   73.3 ...,   28.4   27.4   
> 26.4]

If that's the full output, then my guess is that x_dates and y_rtt are
not actual python lists.  I bet they are, in fact, numpy arrays and
that the string representation of those arrays (what you're getting
from str(x_dates), etc) include the '...' in the middle instead of the
full contents.

Am I close?

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


Re: Objects in Python

2012-08-22 Thread Ian Kelly
In addition to the excellent feedback that Dave gave you:

On Wed, Aug 22, 2012 at 9:25 AM, shaun  wrote:
> def breakuparray(self):
> for row in self.array:
> mer = row[0].ljust(25, ' ')
> merc = row[1].ljust(13, ' ')
> mertype = row[2]
> merloc = row[3]
> mercount = row[4]
> mersec = row[5]
> acq = row[6]

This loops over each row in self.array and stores the contents in a
set of variables called mer, merc, etc.  Note that as written these
variables are *local* to the breakuparray method, not attributes of
the class.  Also note that on each iteration, you reassign to the same
variables again, wiping out all the work you did on the previous
iteration.  In the end, only the last row is stored in the local
variables, and then even that is wiped out when the method returns.

> def returnBatch(self):
> self.breakuparray()
> return "\x01001\x0251.%s%s%s%s%s%s%s%s\x03"  % (mer, merc, 
> mertype, merloc, mercount, mersec, acq);

This appears to be trying to use the same local variables from the
breakuparray method, but it can't.  Those are out of scope here.  If
you want to share these data between the breakuparray method and the
returnBatch method, you should either have breakuparray return them,
or you should store them as attributes on the object instance:
self.mer, self.merc, self.mertype, etc. (In Python, object attribute
storage is always explicit, never implicit as in Java and the rest of
the C++ family).
-- 
http://mail.python.org/mailman/listinfo/python-list


How to properly implement worker processes

2012-08-22 Thread Dennis Jacobfeuerborn
Hi,
I'm trying to implement a system for periodically checking URLs and I've run 
into problems with some of the implementation details. The URLs are supposed to 
be checked continuously until the config for an URL is explicitly removed.

The plan is to spawn a worker process for each URL that sends the status of the 
last check to its parent which keeps track of the state of all URLs. When a URL 
is no longer supposed to be checked the parent process should shutdown/kill the 
respective worker process.

What I've been going for so far is that the parent process creates a global 
queue that is passed to all children upon creation which they use to send 
status messages to the parent. Then for each process a dedicated queue is 
created that the parent uses to issue commands to the child.

The issue is that since the child processes spent some time in sleep() when a 
command from the parent comes they cannot respond immediately which is rather 
undesirable. What I would rather like to do is have the parent simply kill the 
child instead which is instantaneous and more reliable.

My problem is that according to the multiprocessing docs if I kill the child 
while it uses the queue to send a status to the parent then the queue becomes 
corrupted and since that queue is shared that means the whole thing pretty much 
stops working.

How can I get around this problem and receive status updates from all children 
efficiently without a shared queue and with the ability to simply kill the 
child process when it's no longer needed?

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


Re: help me debug my "word capitalizer" script

2012-08-22 Thread Terry Reedy

On 8/22/2012 10:46 AM, Rebelo wrote:


Is it possible to make this script look at a word, see if its first
character is capitalized, if capitalized then skip that word.


Unicode has two 'capital' concepts: 'uppercase' and 'titlecase'. They 
are the same for latin chars but not for all alphabets.



I don't wan to use regex? Do I need it?


No. It may or may not be easier.


change:
  words[i] = word.capitalize()

into:
if word != word.upper() :
  words[i] = word.capitalize()


Still buggy
>>> s = 'CamelCase'
>>> if s != s.upper(): s = s.capitalize()

>>> s
'Camelcase'

use "if not word[0].isupper:..."
or "if word[0].islower

This will still .capitalize() 'weirdWord' to 'Weirdword'.
If you do not want that, only change the first letter.

>>> s = 'weirdWord'
>>> if s[0].islower(): s = s[0].upper()+s[1:]

>>> s
'WeirdWord'

--
Terry Jan Reedy

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


Re: writelines puzzle

2012-08-22 Thread Peter Otten
William R. Wing (Bill Wing) wrote:

> In the middle of a longer program that reads and plots data from a log
> file, I have added the following five lines (rtt_data is fully qualified
> file name):
> 
> wd = open(rtt_data, 'w')
> stat = wd.write(str(i))
> stat = wd.writelines(str(x_dates[:i]))
> stat = wd.writelines(str(y_rtt[:i]))
> wd.close()
> 
> The value of i is unknown before I have read through the input log file,
> but is typically in the neighborhood of 2500.  x_dates is a list of time
> stamps from the date2num method, that is values of the form
> 734716.72445602, day number plus decimal fraction of a day.  y_rtt is a
> list of three- or four-digit floating point numbers.  The x_dates and
> y_rtt lists are complete and plot correctly using matplotlib.  Reading and
> parsing the input log file and extracting the data I need is time
> consuming, so I decided to save the data for further analysis without the
> overhead of reading and parsing it every time.
> 
> Much to my surprise, when I looked at the output file, it only contained
> 160 characters.  Catting produces:
> 
> StraylightPro:Logs wrw$ cat RTT_monitor.dat
> 2354[ 734716.72185185  734716.72233796  734716.72445602 ..., 
> 734737.4440162
>   734737.45097222  734737.45766204][ 240.28.5   73.3 ...,   28.4  
>   27.4   26.4]
> 
> Clearly I'm missing something fundamental about using the writelines
> method, and I'm sure it will be a DUH moment for me, but I'd sure
> appreciate someone telling me how to get that data all written out.  I
> certainly don't insist on writelines, but I would like the file to be
> human-readable.

When you apply str() to a numpy array big arrays are helpfully truncated, 
probably because the functionality is meant to be used in the interactive 
interpreter rather than to write to a file.
The default value is 1000 entries. One way to get the desired output is to 
increase the threshold:

>>> numpy.set_printoptions(threshold=4)
>>> print numpy.arange(10)
[0 1 2 ..., 7 8 9]
>>> numpy.set_printoptions(threshold=10)
>>> print numpy.arange(10)
[0 1 2 3 4 5 6 7 8 9]

Also, in

file.writelines(some_str)

writelines iterates over the characters of the some_string, so you should 
instead write the above as

file.write(some_str)

Your code will become 

assert numpy.get_printoptions()["threshold"] >= i
wd.write(str(x_dates[:i]))

If you intended to write one array entry at a time with writelines() here's 
how to do that:

wd.write("[")
wd.writelines("%s " % x for x in x_dates[:i])
wd.write("]\n")

numpy.savetxt() may also suit your needs.

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


Re: How to properly implement worker processes

2012-08-22 Thread Ian Kelly
On Wed, Aug 22, 2012 at 11:29 AM, Dennis Jacobfeuerborn
 wrote:
> Hi,
> I'm trying to implement a system for periodically checking URLs and I've run 
> into problems with some of the implementation details. The URLs are supposed 
> to be checked continuously until the config for an URL is explicitly removed.
>
> The plan is to spawn a worker process for each URL that sends the status of 
> the last check to its parent which keeps track of the state of all URLs. When 
> a URL is no longer supposed to be checked the parent process should 
> shutdown/kill the respective worker process.
>
> What I've been going for so far is that the parent process creates a global 
> queue that is passed to all children upon creation which they use to send 
> status messages to the parent. Then for each process a dedicated queue is 
> created that the parent uses to issue commands to the child.
>
> The issue is that since the child processes spent some time in sleep() when a 
> command from the parent comes they cannot respond immediately which is rather 
> undesirable. What I would rather like to do is have the parent simply kill 
> the child instead which is instantaneous and more reliable.
>
> My problem is that according to the multiprocessing docs if I kill the child 
> while it uses the queue to send a status to the parent then the queue becomes 
> corrupted and since that queue is shared that means the whole thing pretty 
> much stops working.
>
> How can I get around this problem and receive status updates from all 
> children efficiently without a shared queue and with the ability to simply 
> kill the child process when it's no longer needed?

The usual approach to killing worker processes safely is to send them
an "exit" command, which they should respond to by terminating
cleanly.  Instead of using sleep(), have the workers do a blocking
get() on the queue with a timeout.  This way they'll receive the
"exit" message immediately as desired, but they'll still wake up at
the desired intervals in order to do their work.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread lipska the kat

On 22/08/12 18:01, Terry Reedy wrote:

On 8/22/2012 10:59 AM, lipska the kat wrote:


There is no real enforced concept of information hiding, no binding of
type to variable in fact no concept of typing at all as far as I can
see.


Given that type(valid_name) always returns a type(class), that is a
slightly strange statement.


[snip]

Well I'm a beginner so I'm allowed to make strange statements.
However I don't think it's that strange and here's why.

If, in a language, I find I am able to say

a = 1

then later, in the same scope I can say

a = "foo"

then later again in the same scope I can say

a = ([1,2,3], "xyz", True)

then, and I may be missing something here, to me, that doesn't say 
'strongly typed' that says 'no typing constraints whatsoever'


If you can show me a 'type' that cannot be assigned to

a

in the same scope then I would be most interested to know, I haven't 
found one yet.


We need to separate out the 'view' from the 'implementation' here.
Most developers I know, if looking at the code and without the possibly 
dubious benefit of knowing that in Python 'everything is an object' 
would not call this 'strong typing'


Once again, this is not a criticism, it's an observation

It is OK to to make (possibly erroneous) observations isn't it?

Thanks for taking the time to reply.

lipska

--
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun
--
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread Mark Lawrence

On 22/08/2012 18:06, lipska the kat wrote:

On 22/08/12 17:30, Mark Lawrence wrote:

On 22/08/2012 17:10, lipska the kat wrote:

On 22/08/12 16:58, MRAB wrote:

On 22/08/2012 15:59, lipska the kat wrote:

On 22/08/12 15:13, shaun wrote:

[snip]


Im very new to python and the object orientated feature doesnt seem
to be as well put together as Java. Can anyone help with this
problem?




The residents can be pretty defensive as well :-)

Once again, no criticism intended.

lipska



I'm lost. I see nothing defensive at all. I see a statement of fact.



You seem to be perpetually lost Mark ...


lipska



Maybe but I seem to understand Python rather better than some people :)

Once again, no criticism intended.



--
Cheers.

Mark Lawrence.

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


Re: Objects in Python

2012-08-22 Thread Ian Kelly
On Wed, Aug 22, 2012 at 11:46 AM, lipska the kat
 wrote:
> If, in a language, I find I am able to say
>
> a = 1
>
> then later, in the same scope I can say
>
> a = "foo"
>
> then later again in the same scope I can say
>
> a = ([1,2,3], "xyz", True)
>
> then, and I may be missing something here, to me, that doesn't say 'strongly
> typed' that says 'no typing constraints whatsoever'

You're conflating "strong typing" with "static typing".  Strong typing
does not refer to restrictions on what type of data can be stored
where, but to restrictions on how operations on that data can be
intermixed.

The classic example of weak typing is concatenation of strings and
numbers, e.g. ("abc" + 123).  Weakly typed languages like JavaScript
will implicitly coerce the number to a string and perform the
concatenation.  Strongly typed languages like Python will raise a
TypeError instead.

Note that statically typed languages can be weakly typed as well.  For
instance, C is commonly considered to be weakly typed because the
casting rules of that language allow you to treat any piece of data as
being of any type, even though the variables themselves are all
statically typed.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyCrypto builds neither with MSVC nor MinGW

2012-08-22 Thread bikewave
I also had the unresolved externals problem (not the mdir.h problem, though)
and my solution was different.
a) reinstall correct python2.6.4, using an Intel-flavor msi vice
AMD64-flavor
b) source the c:\program files(x86\microsoft visual studio
9.0\vc\bin\vcvars32.bat
and shazzm the pycrypto build + install worked fine.
My CPU is Intel not AMD so I apparently had a bogus python install.



--
View this message in context: 
http://python.6.n6.nabble.com/PyCrypto-builds-neither-with-MSVC-nor-MinGW-tp4366880p4986058.html
Sent from the Python - python-list mailing list archive at Nabble.com.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread Mark Lawrence

On 22/08/2012 18:46, lipska the kat wrote:

On 22/08/12 18:01, Terry Reedy wrote:

On 8/22/2012 10:59 AM, lipska the kat wrote:


There is no real enforced concept of information hiding, no binding of
type to variable in fact no concept of typing at all as far as I can
see.


Given that type(valid_name) always returns a type(class), that is a
slightly strange statement.


[snip]

Well I'm a beginner so I'm allowed to make strange statements.
However I don't think it's that strange and here's why.

If, in a language, I find I am able to say

a = 1

then later, in the same scope I can say

a = "foo"

then later again in the same scope I can say

a = ([1,2,3], "xyz", True)

then, and I may be missing something here, to me, that doesn't say
'strongly typed' that says 'no typing constraints whatsoever'

If you can show me a 'type' that cannot be assigned to

a

in the same scope then I would be most interested to know, I haven't
found one yet.


You've said nothing above except that any object you like can be bound 
to a Python name.  The name 'a' is never used.  What happens when you 
actually do something with the object that you've bound to 'a'?




We need to separate out the 'view' from the 'implementation' here.
Most developers I know, if looking at the code and without the possibly
dubious benefit of knowing that in Python 'everything is an object'
would not call this 'strong typing'


I really despair that after ten years of using Python people still seem 
to be incapable of distinguishing strong, static, weak and dynamic 
typing.  Not that it's a specific Python problem of course, just that I 
always get to read about it here.




Once again, this is not a criticism, it's an observation

It is OK to to make (possibly erroneous) observations isn't it?


Not if undoes concepts that computer scientists have patiently been 
trying to explain for years.




Thanks for taking the time to reply.

lipska




--
Cheers.

Mark Lawrence.

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


Re: PyCrypto builds neither with MSVC nor MinGW

2012-08-22 Thread Dave Angel
On 08/22/2012 02:21 PM, bikewave wrote:
> I also had the unresolved externals problem (not the mdir.h problem, though)
> and my solution was different.
> a) reinstall correct python2.6.4, using an Intel-flavor msi vice
> AMD64-flavor
> b) source the c:\program files(x86\microsoft visual studio
> 9.0\vc\bin\vcvars32.bat
> and shazzm the pycrypto build + install worked fine.
> My CPU is Intel not AMD so I apparently had a bogus python install.
>

For most open-software distributions:

AMD64 is the 64 bit build for both AMD and Intel.   Probably referred to
as AMD64 because AMD had a compatible 64 bit processor while Intel was
still fooling around with Merced for its 64 bit stuff.

X86 is the 32 bit build for both AMD and Intel.

-- 

DaveA

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


Re: How do I display unicode value stored in a string variable using ord()

2012-08-22 Thread Hans Mulder
On 19/08/12 19:48:06, Paul Rubin wrote:
> Terry Reedy  writes:
>>  py> s = chr(0x + 1)
>>  py> a, b = s
> That looks like a 3.2- narrow build. Such which treat unicode strings
> as sequences of code units rather than sequences of codepoints. Not an
> implementation bug, but compromise design that goes back about a
> decade to when unicode was added to Python.

Actually, this compromise design was new in 3.0.

In 2.x, unicode strings were sequences of code points.
Narrow builds rejected any code points > 0x:

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s = unichr(0x + 1)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: unichr() arg not in range(0x1) (narrow Python build)


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


Re: writelines puzzle

2012-08-22 Thread William R. Wing (Bill Wing)
On Aug 22, 2012, at 12:48 PM, Chris Kaynor  wrote:

> Reading your post, I do not see for sure what your actual issue is, so
> I am taking my best guess: that the file does not contain as much data
> as would be expected.
> 

Sorry, I should have been more explicit.  The value of "i" in this instance is 
2354, so the file should (I thought) have contained the value of "i" followed 
by 2 x 2354 values of the data.

> On Wed, Aug 22, 2012 at 8:38 AM, William R. Wing (Bill Wing)
>  wrote:
>> In the middle of a longer program that reads and plots data from a log file, 
>> I have added the following five lines (rtt_data is fully qualified file 
>> name):
>> 
>> wd = open(rtt_data, 'w')
> 
> Here, you are opening the data for write ("w"), which will replace the
> contents of the file each time the file is opened. I am guessing you
> want append ("a").
> 

No, I really do want 'w'.  In the final package, this will be invoked once at 
the end of the month, before the log file is compressed and rolled.

[big snip]

>> 
>> Much to my surprise, when I looked at the output file, it only contained 160 
>> characters [instead of ~2700 multi-character data values].  Catting produces:
>> 
>> StraylightPro:Logs wrw$ cat RTT_monitor.dat
>> 2354[ 734716.72185185  734716.72233796  734716.72445602 ...,  734737.4440162
>>  734737.45097222  734737.45766204][ 240.28.5   73.3 ...,   28.4   27.4   
>> 26.4]
>> 

Please look at what cat found in the file.  First there is the value of "i" as 
expected.  This is then followed by the first 3 values of the time stamp 
x-data, then a comma, an ellipsis, another comma, and the last three data 
values.  That patten (3 values, an ellipsis, and 3 final values) is repeated 
again for the y-data array/list.  It is almost as though "writelines" had 
written an editorial summary of the data rather than the data.

>> Clearly I'm missing something fundamental about using the writelines method, 
>> and I'm sure it will be a DUH moment for me, but I'd sure appreciate someone 
>> telling me how to get that data all written out.  I certainly don't insist 
>> on writelines, but I would like the file to be human-readable.
>> 
>> Python 2.7.3
>> OS-X 10.8
>> 
>> Thanks,
>> Bill
>> --
>> http://mail.python.org/mailman/listinfo/python-list
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


Re: Objects in Python

2012-08-22 Thread lipska the kat

On 22/08/12 19:15, Ian Kelly wrote:

On Wed, Aug 22, 2012 at 11:46 AM, lipska the kat
  wrote:

If, in a language, I find I am able to say

a = 1


[snip]



You're conflating "strong typing" with "static typing".  Strong typing
does not refer to restrictions on what type of data can be stored
where, but to restrictions on how operations on that data can be
intermixed.


Yes of course I am, thank you for pointing that out.
I don't know why I have this overwhelming need to see variables 
explicitly defined ... years of 'same old same old' I suppose.


I do seem to remember reading something about Python moving towards 
static typing and there are tools out there to help avoid run time 
disasters ... anyway, this has latched now so thanks for taking the time 
to reply.


lipska

--
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun
--
http://mail.python.org/mailman/listinfo/python-list


Re: Re: Objects in Python

2012-08-22 Thread Evan Driscoll
On 08/22/2012 12:46 PM, lipska the kat wrote:
> If you can show me a 'type' that cannot be assigned to
> 
> a
> 
> in the same scope then I would be most interested to know, I haven't
> found one yet.

As other people have said, you've just pointed out the difference
between static typing and dynamic typing -- and how the former types
variables (or "names") and the latter types objects. I just have a few
things to add to your post and others.


First, from some point of view, you're correct. From a type theory point
of view "dynamically typed" is an oxymoron, because *by definition*
(from this point of view) types are a property of variables (and
expressions). For example, Ben Pierce says (in "Types and Programming
Languages"):

  The word "static" is sometimes added explicitly...to distinguish
  the sorts of compile-time analyses we are considering here from the
  dynamic or latent typing found in languages such as Scheme, where
  run-time type tags are used to distinguish different kinds of
  structures in the heap. Terms like "dynamically typed" are arguably
  misnomers and should probably be replaced by "dynamically checked,"
  but the usage is standard.

(And the usage is standard because it's just really useful to be able to
say "dynamically-typed" instead of "uni-typed with runtime checks of
things that act like types". (I don't think Pierce's "dynamically
checked" is specific enough. :-))


Second, this concept isn't *so* unfamiliar to you. If I give you the
following Java code:

  void foo(Object o) { ... }

and ask what type 'o' is, there are kind of two answers. The first is
that 'o' is an 'Object'. But you can't make an Object -- that's an
abstract class. (IIRC. If it's not then just bear with me; you get the
idea. :-)) So from a strictly static type-theory point of view, 'foo' is
unusable because it takes a type which you can never create. But of
course that's not the case, because in actual Java 'o' has some dynamic
type which is a subclass of 'Object'.

Though I'm sure this statement will be *really* popular with this list
, if it puts your mind at ease a little, you can imagine that
there are no primitive types and Python names all have type 'Object',
but that you can refer to the functions in an object's dynamic type
without explicitly downcasting. (The analogy isn't perfect.)


On 08/22/2012 01:15 PM, Ian Kelly wrote:
> The classic example of weak typing is concatenation of strings and
> numbers, e.g. ("abc" + 123).  Weakly typed languages like JavaScript
> will implicitly coerce the number to a string and perform the
> concatenation.  Strongly typed languages like Python will raise a
> TypeError instead.

I would also say don't get *too* caught up in categorizing everything
into "strong" and "weak"; that's a spectrum, and where things fall is a
lot more interesting than just "here or there". Really it's even more
complex than just a linear spectrum -- Language A can be stronger than
Language B in one respect but weaker in another.

In particular, it's possible to have rather stronger typing than Python
(especially with respect to Booleans, but in some other respects as well).

Evan



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread lipska the kat

On 22/08/12 19:07, Mark Lawrence wrote:

On 22/08/2012 18:06, lipska the kat wrote:

On 22/08/12 17:30, Mark Lawrence wrote:

On 22/08/2012 17:10, lipska the kat wrote:

On 22/08/12 16:58, MRAB wrote:

On 22/08/2012 15:59, lipska the kat wrote:

On 22/08/12 15:13, shaun wrote:



[snip]



Maybe but I seem to understand Python rather better than some people :)


Well I should certainly hope so after '10 years of using Python'
I freely admit I don't fully understand Python yet but I will and 
hopefully it won't take 10 years ;-)


Never give up, never surrender !!!


Once again, no criticism intended.


None taken

lipska

--
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun
--
http://mail.python.org/mailman/listinfo/python-list


Re: writelines puzzle

2012-08-22 Thread Dave Angel
On 08/22/2012 02:00 PM, William R. Wing (Bill Wing) wrote:
> On Aug 22, 2012, at 12:48 PM, Chris Kaynor  wrote:
>
>> Reading your post, I do not see for sure what your actual issue is, so
>> I am taking my best guess: that the file does not contain as much data
>> as would be expected.
>>
> Sorry, I should have been more explicit.  The value of "i" in this instance 
> is 2354, so the file should (I thought) have contained the value of "i" 
> followed by 2 x 2354 values of the data.
>
>> On Wed, Aug 22, 2012 at 8:38 AM, William R. Wing (Bill Wing)
>>  wrote:
>>> In the middle of a longer program that reads and plots data from a log 
>>> file, I have added the following five lines (rtt_data is fully qualified 
>>> file name):
>>>
>>> wd = open(rtt_data, 'w')
>> Here, you are opening the data for write ("w"), which will replace the
>> contents of the file each time the file is opened. I am guessing you
>> want append ("a").
>>
> No, I really do want 'w'.  In the final package, this will be invoked once at 
> the end of the month, before the log file is compressed and rolled.
>
> [big snip]
>
>>> Much to my surprise, when I looked at the output file, it only contained 
>>> 160 characters [instead of ~2700 multi-character data values].  Catting 
>>> produces:
>>>
>>> StraylightPro:Logs wrw$ cat RTT_monitor.dat
>>> 2354[ 734716.72185185  734716.72233796  734716.72445602 ...,  734737.4440162
>>>  734737.45097222  734737.45766204][ 240.28.5   73.3 ...,   28.4   27.4  
>>>  26.4]
>>>
> Please look at what cat found in the file.  First there is the value of "i" 
> as expected.  This is then followed by the first 3 values of the time stamp 
> x-data, then a comma, an ellipsis, another comma, and the last three data 
> values.  That patten (3 values, an ellipsis, and 3 final values) is repeated 
> again for the y-data array/list.  It is almost as though "writelines" had 
> written an editorial summary of the data rather than the data.

This problem has nothing to do with writelines().  You pass writelines()
a simple string, it writes it to the file. writelines() normally EXPECTS
a list, but you convert it with the str() function.  So it iterates
through the characters, rather than through the elements of a list.

Clearly, the value x_data  is not a list either, as you claimed in your
original message.   Calling str() on a list would have produced a single
string representing a list, and assuming the elements are floats, it'd
look pretty much what you had except for the ellipses.  Is that really
what you wanted?  If so, i'd fix the code that produced a non-list for
that value (and similarly for y_rtt).

On the other hand, perhaps you expected the values to be one or two per
line, judging from your use of the writelines() function.

I suspect you have a numpy array or similar object, not a list.  So
confirm what you've actually got, and what you want/expect the output to
look like, and somebody will (or already has) help out.



-- 

DaveA

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


Re: writelines puzzle

2012-08-22 Thread William R. Wing (Bill Wing)
On Aug 22, 2012, at 1:28 PM, Jerry Hill  wrote:

> On Wed, Aug 22, 2012 at 11:38 AM, William R. Wing (Bill Wing)
>  wrote:
>> Much to my surprise, when I looked at the output file, it only contained 160 
>> characters.  Catting produces:
>> 
>> StraylightPro:Logs wrw$ cat RTT_monitor.dat
>> 2354[ 734716.72185185  734716.72233796  734716.72445602 ...,  734737.4440162
>>  734737.45097222  734737.45766204][ 240.28.5   73.3 ...,   28.4   27.4   
>> 26.4]
> 
> If that's the full output, then my guess is that x_dates and y_rtt are
> not actual python lists.  I bet they are, in fact, numpy arrays and
> that the string representation of those arrays (what you're getting
> from str(x_dates), etc) include the '...' in the middle instead of the
> full contents.
> 
> Am I close?
> 
> -- 
> Jerry

Yes - bingo.  They are numpy arrays.  And that was the hint I needed to go look 
in the numpy docs.  Found numpy.set_printoptions(threshold='nan'), which has 
indeed allowed me to get all the data into the file.

I now see Peter Otten made the same suggestion.

Thanks to you both.

Bill

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


Re: How to properly implement worker processes

2012-08-22 Thread Dennis Jacobfeuerborn
On Wednesday, August 22, 2012 7:46:34 PM UTC+2, Ian wrote:
> On Wed, Aug 22, 2012 at 11:29 AM, Dennis Jacobfeuerborn
> 
>  wrote:
> 
> > Hi,
> 
> > I'm trying to implement a system for periodically checking URLs and I've 
> > run into problems with some of the implementation details. The URLs are 
> > supposed to be checked continuously until the config for an URL is 
> > explicitly removed.
> 
> >
> 
> > The plan is to spawn a worker process for each URL that sends the status of 
> > the last check to its parent which keeps track of the state of all URLs. 
> > When a URL is no longer supposed to be checked the parent process should 
> > shutdown/kill the respective worker process.
> 
> >
> 
> > What I've been going for so far is that the parent process creates a global 
> > queue that is passed to all children upon creation which they use to send 
> > status messages to the parent. Then for each process a dedicated queue is 
> > created that the parent uses to issue commands to the child.
> 
> >
> 
> > The issue is that since the child processes spent some time in sleep() when 
> > a command from the parent comes they cannot respond immediately which is 
> > rather undesirable. What I would rather like to do is have the parent 
> > simply kill the child instead which is instantaneous and more reliable.
> 
> >
> 
> > My problem is that according to the multiprocessing docs if I kill the 
> > child while it uses the queue to send a status to the parent then the queue 
> > becomes corrupted and since that queue is shared that means the whole thing 
> > pretty much stops working.
> 
> >
> 
> > How can I get around this problem and receive status updates from all 
> > children efficiently without a shared queue and with the ability to simply 
> > kill the child process when it's no longer needed?
> 
> 
> 
> The usual approach to killing worker processes safely is to send them
> 
> an "exit" command, which they should respond to by terminating
> 
> cleanly.  Instead of using sleep(), have the workers do a blocking
> 
> get() on the queue with a timeout.  This way they'll receive the
> 
> "exit" message immediately as desired, but they'll still wake up at
> 
> the desired intervals in order to do their work.

I was thinking about something like that but the issue is that this really only 
works when you don't do any actual blocking work. I may be able to get around 
the sleep() but then I have to fetch the URL or do some other work that might 
block for a while so the get() trick doesn't work.
Also the child process might not be able to deal with such an exit command at 
all for one reason or another so the only safe way to get rid of it is for the 
parent to kill it.

The better option would be to not use a shared queue for communication and 
instead use only dedicated pipes/queues for each child process but the doesn't 
seem to be a way to wait for a message from multiple queues/pipes. If that were 
the case then I could simply kill the child and get rid of the respective 
pipes/queues without affecting the other processes or communication channels.

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


Re: Objects in Python

2012-08-22 Thread lipska the kat

On 22/08/12 20:03, Evan Driscoll wrote:

On 08/22/2012 12:46 PM, lipska the kat wrote:

If you can show me a 'type' that cannot be assigned to

a

in the same scope then I would be most interested to know, I haven't
found one yet.



[snip]



Second, this concept isn't *so* unfamiliar to you. If I give you the
following Java code:

   void foo(Object o) { ... }

and ask what type 'o' is, there are kind of two answers. The first is
that 'o' is an 'Object'. But you can't make an Object -- that's an
abstract class. (IIRC. If it's not then just bear with me; you get the
idea. :-)) So from a strictly static type-theory point of view, 'foo' is
unusable because it takes a type which you can never create. But of
course that's not the case, because in actual Java 'o' has some dynamic
type which is a subclass of 'Object'.


Well I think this is where I'm struggling a bit.

looking at this method declaration I can see that the method takes an 
argument of type Object (and just FYI class Object is not abstract and 
you can do Object o = new Object()) and does not return a value.
I know that for the lifetime of this JVM, whatever o turns out to be it 
will always be an Object. I can't assign a primitive to o as ints chars 
floats etc are certainly not Objects. There are certain invariants that 
give me a warm and comfortable feeling inside.


compare this to a function declaration in Python

def foo(self):

... I can learn nothing about this function by looking at the first line

If I see

def foo(self, someVar=1):

I can determine that foo takes an argument that, at some time or other 
is or has been a number of some description but I can't rely on it and I 
have no idea what is returned and that's where I think I'm having trouble.


But I will get over it and I will learn the language and I may look back 
on these exchanges and say ... I was wrong, this is so much better that 
what I was used to, or maybe I won't. I'll let you know


Thank you for your calm and reasoned response.

Disclaimer:
None of my comments above should be construed as criticisms
they are just observations.

lipska

--
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun
--
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread MRAB

On 22/08/2012 20:45, lipska the kat wrote:

On 22/08/12 20:03, Evan Driscoll wrote:

On 08/22/2012 12:46 PM, lipska the kat wrote:

If you can show me a 'type' that cannot be assigned to

a

in the same scope then I would be most interested to know, I haven't
found one yet.



[snip]



Second, this concept isn't *so* unfamiliar to you. If I give you the
following Java code:

   void foo(Object o) { ... }

and ask what type 'o' is, there are kind of two answers. The first is
that 'o' is an 'Object'. But you can't make an Object -- that's an
abstract class. (IIRC. If it's not then just bear with me; you get the
idea. :-)) So from a strictly static type-theory point of view, 'foo' is
unusable because it takes a type which you can never create. But of
course that's not the case, because in actual Java 'o' has some dynamic
type which is a subclass of 'Object'.


Well I think this is where I'm struggling a bit.

looking at this method declaration I can see that the method takes an
argument of type Object (and just FYI class Object is not abstract and
you can do Object o = new Object()) and does not return a value.
I know that for the lifetime of this JVM, whatever o turns out to be it
will always be an Object. I can't assign a primitive to o as ints chars
floats etc are certainly not Objects. There are certain invariants that
give me a warm and comfortable feeling inside.

compare this to a function declaration in Python

def foo(self):


[snip]
That's not actually a declaration but a definition. :-)

The function's body is bound to the name at runtime, so:

def double_it(x):
return x * 2

is not far from:

double_it = lambda x: x * 2

The only declarations are "global" and "nonlocal" (and the latter
exists only in recent versions of Python).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread Mark Lawrence

On 22/08/2012 21:31, MRAB wrote:

On 22/08/2012 20:45, lipska the kat wrote:


compare this to a function declaration in Python

def foo(self):


[snip]
That's not actually a declaration but a definition. :-)

The function's body is bound to the name at runtime, so:

 def double_it(x):
 return x * 2

is not far from:

 double_it = lambda x: x * 2

The only declarations are "global" and "nonlocal" (and the latter
exists only in recent versions of Python).


Looking at the self I'm assuming that's a method and not a function.

--
Cheers.

Mark Lawrence.

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


Re: How to properly implement worker processes

2012-08-22 Thread Ian Kelly
On Wed, Aug 22, 2012 at 1:40 PM, Dennis Jacobfeuerborn
 wrote:
> I was thinking about something like that but the issue is that this really 
> only works when you don't do any actual blocking work. I may be able to get 
> around the sleep() but then I have to fetch the URL or do some other work 
> that might block for a while so the get() trick doesn't work.

At a lower level, it is possible to poll on both the pipe and the
socket simultaneously.  At this point though you might want to start
looking at an asynchronous or event-driven framework like twisted or
gevent.

> Also the child process might not be able to deal with such an exit command at 
> all for one reason or another so the only safe way to get rid of it is for 
> the parent to kill it.

I think you mean that it is the most "reliable" way.  In general, the
only "safe" way to cause a process to exit is the cooperative
approach, because it may otherwise leave external resources such as
file data in an unexpected state that could cause problems later.

> The better option would be to not use a shared queue for communication and 
> instead use only dedicated pipes/queues for each child process but the 
> doesn't seem to be a way to wait for a message from multiple queues/pipes. If 
> that were the case then I could simply kill the child and get rid of the 
> respective pipes/queues without affecting the other processes or 
> communication channels.

Assuming that you're using a Unix system:

from select import select

while True:
ready, _, _ = select(pipes, [], [], timeout)
if not ready:
# process timeout
else:
for pipe in ready:
message = pipe.get()
# process message
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re: Objects in Python

2012-08-22 Thread Evan Driscoll
On 08/22/2012 02:45 PM, lipska the kat wrote:
> On 22/08/12 20:03, Evan Driscoll wrote:
>> Second, this concept isn't *so* unfamiliar to you. If I give you the
>> following Java code:
>>
>>void foo(Object o) { ... }
>>
> looking at this method declaration I can see that the method takes an
> argument of type Object (and just FYI class Object is not abstract and
> you can do Object o = new Object()) and does not return a value.
> I know that for the lifetime of this JVM, whatever o turns out to be it
> will always be an Object. I can't assign a primitive to o as ints chars
> floats etc are certainly not Objects. There are certain invariants that
> give me a warm and comfortable feeling inside.

I'm not saying it's nothing, but "can't assign a primitive" isn't much
of an invariant in the broad scheme of things when you can pass items as
diverse as lists, GUI buttons, files, etc. I would say it's more like if
you see 'int x' then *that* imposes a pretty big invariant, but passing
'Object' imposes almost nothing.

This is especially true considering the fact that you actually *can* say
'foo(4)' and Java will go and autobox the 4 into Integer(4) for you.

(BTW, this analogy suggests a way that's actually fairly useful for how
to look at Python coming from the Java perspective: Python just lacks
primitive types and things like integers are always boxed. Thus *all*
Python variables are essentially references.)

Evan



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread Ben Finney
lipska the kat  writes:

> If, in a language, I find I am able to say
>
> a = 1
>
> then later, in the same scope I can say
>
> a = "foo"
>
> then later again in the same scope I can say
>
> a = ([1,2,3], "xyz", True)
>
> then, and I may be missing something here, to me, that doesn't say
> strongly typed' that says 'no typing constraints whatsoever'

You haven't discovered anything about types; what you have discovered is
that Python name bindings are not variables.

In fact, Python doesn't have variables – not as C or Java programmers
would understand the term. What it has instead are references to objects
(with names as one kind of reference).

The documentation unfortunately calls these references “variables” in
various places, which IMO compounds the confusion in newcomers
experienced with other languages. It's best, I think, to reject the idea
that Python has variables, and think in terms of references instead.

Any reference (with some very narrow specific exclusions, like ‘None’)
can be re-bound to any other object without regard to the previous
binding.

> We need to separate out the 'view' from the 'implementation' here.
> Most developers I know, if looking at the code and without the
> possibly dubious benefit of knowing that in Python 'everything is an
> object' would not call this 'strong typing'

Those people are confused, then. Python is strongly typed: objects
always know their type, the type is always exact, and the type of an
object can't be changed.

This is always true regardless of whether the object is referred to
zero, one, or many times.

Python is dynamically typed: References (and hence names) don't have
types.

> It is OK to to make (possibly erroneous) observations isn't it?

One of our long-time regulars (Aahz, whom I haven't seen for a long
time, sadly) quipped that the best way to get correct information on
Usenet is not to ask a question, but to post incorrect information.
That's not a license for such behaviour, but an observation on its
effectiveness.

I'd say that so long as you phrase your assertions to indicate the level
of confidence and possibility of error, that's okay.

-- 
 \  “Generally speaking, the errors in religion are dangerous; |
  `\those in philosophy only ridiculous.” —David Hume, _A Treatise |
_o__)   of Human Nature_, 1739 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread Ian Kelly
On Wed, Aug 22, 2012 at 5:58 PM, Ben Finney  wrote:
> Those people are confused, then. Python is strongly typed: objects
> always know their type, the type is always exact, and the type of an
> object can't be changed.

Except when it can.

>>> class A: pass
...
>>> class B: pass
...
>>> a = A()
>>> type(a)

>>> isinstance(a, B)
False
>>> a.__class__ = B
>>> type(a)

>>> isinstance(a, A)
False
>>> isinstance(a, B)
True

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


Re: Objects in Python

2012-08-22 Thread Walter Hurry
On Wed, 22 Aug 2012 18:46:43 +0100, lipska the kat wrote:

> Well I'm a beginner

Then maybe you should read more and write less.

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


Re: Objects in Python

2012-08-22 Thread Chris Angelico
On Thu, Aug 23, 2012 at 5:03 AM, lipska the kat
 wrote:
> On 22/08/12 19:15, Ian Kelly wrote:
>>
>> You're conflating "strong typing" with "static typing".  Strong typing
>> does not refer to restrictions on what type of data can be stored
>> where, but to restrictions on how operations on that data can be
>> intermixed.
>
> Yes of course I am, thank you for pointing that out.
> I don't know why I have this overwhelming need to see variables explicitly
> defined ... years of 'same old same old' I suppose.

Python's object model is not fundamentally incompatible with a system
of declared variables. I, too, like declaring my variables explicitly.
There are quite a few advantages to it:

1) Scoping is extremely clear. (Leaving aside anomalous behaviour like
Javascript's "var" declarations applying to a whole function.) You see
a declaration, you know that variable exists there and further inside
only. In a C-style language, that means it disappears at the
corresponding close brace.

2) Related to the above, you can infinitely nest scopes. There's
nothing wrong with having six variables called 'q'; you always use the
innermost one. Yes, this can hurt readability, especially taken to
this sort of stupid extreme, but that's a question for a style guide
and not a language design.

3) Variable-name typos can be caught at parse time. In Python, if you
misspell a variable, you've just made a new local variable.

It has its downsides, too, of course; mainly that it's (in some
people's opinion) syntactic salt. Why should I have to tell the
interpreter/compiler what it can figure out by itself? It's
duplicating information, and that's inefficient. It's like forcing all
your integer constants to be given in both decimal and hex, to catch
errors.

I'm of the opinion that the benefits are worth the effort. Others
disagree. It's hugely a matter of taste, and I'm just glad that there
are enough languages around that we can all have what we want :)

If you like the Python object model but want to add variable
declarations, grab me off list and I'll tell you about my personal
favorite language...

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


Re: How to properly implement worker processes

2012-08-22 Thread Dennis Jacobfeuerborn
On Wednesday, August 22, 2012 11:15:10 PM UTC+2, Ian wrote:
> On Wed, Aug 22, 2012 at 1:40 PM, Dennis Jacobfeuerborn
> 
>  wrote:
> 
> > I was thinking about something like that but the issue is that this really 
> > only works when you don't do any actual blocking work. I may be able to get 
> > around the sleep() but then I have to fetch the URL or do some other work 
> > that might block for a while so the get() trick doesn't work.
> 
> 
> 
> At a lower level, it is possible to poll on both the pipe and the
> 
> socket simultaneously.  At this point though you might want to start
> 
> looking at an asynchronous or event-driven framework like twisted or
> 
> gevent.
> 

I was looking at twisted and while the Agent class would allow me to make async 
request it doesn't seem to support setting a timeout or aborting the running 
request. That's really the important part since the http request is really the 
only thing that might block for a while. If I can make the request 
asynchronously and abort it when I receive a QUIT command from the parent then 
this would pretty much solve the issue.

> 
> > Also the child process might not be able to deal with such an exit command 
> > at all for one reason or another so the only safe way to get rid of it is 
> > for the parent to kill it.
> 
> 
> 
> I think you mean that it is the most "reliable" way.  In general, the
> 
> only "safe" way to cause a process to exit is the cooperative
> 
> approach, because it may otherwise leave external resources such as
> 
> file data in an unexpected state that could cause problems later.
> 

True but the child is doing nothing but making http requests and reporting the 
result to the parent so killing the process shouldn't be too much of a deal in 
this case. A segfault in an Apache worker process is very similar in that it's 
an uncontrolled termination of the process and that works out fine.

> 
> > The better option would be to not use a shared queue for communication and 
> > instead use only dedicated pipes/queues for each child process but the 
> > doesn't seem to be a way to wait for a message from multiple queues/pipes. 
> > If that were the case then I could simply kill the child and get rid of the 
> > respective pipes/queues without affecting the other processes or 
> > communication channels.
> 
> 
> 
> Assuming that you're using a Unix system:
> 
> 
> 
> from select import select
> 
> 
> 
> while True:
> 
> ready, _, _ = select(pipes, [], [], timeout)
> 
> if not ready:
> 
> # process timeout
> 
> else:
> 
> for pipe in ready:
> 
> message = pipe.get()
> 
> # process message

That looks like a workable solution. When I decide to kill a worker process I 
can remove the pipe from the pipes list and discard it since it's not shared.

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


Methods versus functions [was Re: Objects in Python]

2012-08-22 Thread Steven D'Aprano
On Wed, 22 Aug 2012 21:46:29 +0100, Mark Lawrence wrote:

>> On 22/08/2012 20:45, lipska the kat wrote:
>>>
>>> compare this to a function declaration in Python
>>>
>>> def foo(self):
[...]
> Looking at the self I'm assuming that's a method and not a function.


Actually, it is a function. It doesn't get turned into a method until you 
try to use it.

The way methods work in Python is this:

Function objects are created by the "def" statement, regardless of 
whether that is inside a class or not. So:

class X(object):
def spam(self, value):
pass

creates a single function object which takes one argument, self, and 
sticks in in the class namespace X.__dict__['spam'] = spam

When you look up the name "spam" on an X instance:

x = X()
x.spam(42)

Python does it's usual attribute lookup stuff, finds the name "spam" in 
the class namespace, and extracts the function object. So far that's just 
ordinary attribute access. This is where it gets interesting...

Function objects are *descriptors*, which means that they have a __get__ 
method:

py> (lambda x: None).__get__


Python sees that __get__ method and calls it with some appropriate 
arguments (the class object and the instance object, I believe) and the 
__get__ method constructs a method on the fly, handles the automatic bind-
instance-to-self magic, and returns the method object. And then finally 
Python calls the method object with whatever arguments you provided.

As they say, "You can solve any problem in computer science with 
sufficient layers of indirection". This descriptor protocol, as they call 
it, is the mechanism behind methods, class methods, static methods, 
properties, and probably other things as well.

You can do all sorts of funky things with descriptors -- google for 
"descriptor protocol", "data descriptor", "non-data descriptor" etc. if 
you are interested. Here's a trivial, but useful, one:

http://code.activestate.com/recipes/577030-dualmethod-descriptor/


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


Re: Objects in Python

2012-08-22 Thread Steven D'Aprano
On Thu, 23 Aug 2012 12:02:16 +1000, Chris Angelico wrote:

> 2) Related to the above, you can infinitely nest scopes. There's nothing
> wrong with having six variables called 'q'; you always use the innermost
> one. Yes, this can hurt readability

Well, there you go. There *is* something wrong with having six variables 
called 'q'.

Namespaces and scopes are work-arounds for the fact that, while there are 
an infinite number of possible names, most of the good ones are already 
taken.

Namespaces and scopes mean that I can use a name "q" even though I've 
already used it for something else, but there is still cost to re-using 
names (even if that cost is sometimes trivial).


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


Re: Objects in Python

2012-08-22 Thread Steven D'Aprano
On Wed, 22 Aug 2012 18:46:43 +0100, lipska the kat wrote:

> We need to separate out the 'view' from the 'implementation' here. Most
> developers I know, if looking at the code and without the possibly
> dubious benefit of knowing that in Python 'everything is an object'
> would not call this 'strong typing'

Most developers are wrong :)

A very useful resource to read is "What To Know Before Debating Type 
Systems", which was put on the internet, lost, then found and put back up 
here:

http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/

I don't *quite* go so far as to agree that "strong typing" and "weak 
typing" are meaningless, but they certainly don't mean quite as much as 
people sometimes think.

It also includes this brilliant quote:

"I find it amusing when novice programmers believe their main job is 
preventing programs from crashing. ... More experienced programmers 
realize that correct code is great, code that crashes could use 
improvement, but incorrect code that doesn’t crash is a horrible 
nightmare."

In Python's case, we say:

1) objects are strongly typed -- every object has a type, which is 
usually immutable and sometimes known at compile time;

2) names (what some people insist on calling variables) are not typed at 
all (with the exception of a handful of keywords like None);

3) most Python built-ins are strongly typed, and users are discouraged 
from writing excessively weakly-typed operations (you could write an int 
subclass that can be added to a string or a list, just as you can add it 
to a float, but you shouldn't);

4) there's a culture of preferring "duck typing", which is kind of an ad 
hoc form of interfaces, over strict type testing, so users are also 
discouraged from writing excessively strongly-typed operations (if you 
just need something you can iterate over, why insist that it must be a 
list and nothing but a list?).


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


Re: Re: Objects in Python

2012-08-22 Thread Evan Driscoll
On 8/22/2012 18:58, Ben Finney wrote:
> You haven't discovered anything about types; what you have discovered is
> that Python name bindings are not variables.
> 
> In fact, Python doesn't have variables – not as C or Java programmers
> would understand the term. What it has instead are references to objects
> (with names as one kind of reference).

OK, I've seen this said a few times, and I have to ask: what do you mean
by this? I consider myself pretty decent at Python and other languages,
and I really don't get it.

Especially the comparison to Java variables. Java programmers are quite
used to variables which are references to objects: everything that's not
a primitive type is a reference, and it's kinda hard to determine
whether primitives are references or actual primitives.

And many other languages have reference behavior and still call their
bindings "variables", e.g. Scheme.

Evan




signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Objects in Python

2012-08-22 Thread Chris Angelico
On Thu, Aug 23, 2012 at 2:11 PM, Steven D'Aprano
 wrote:
> On Thu, 23 Aug 2012 12:02:16 +1000, Chris Angelico wrote:
>
>> 2) Related to the above, you can infinitely nest scopes. There's nothing
>> wrong with having six variables called 'q'; you always use the innermost
>> one. Yes, this can hurt readability
>
> Well, there you go. There *is* something wrong with having six variables
> called 'q'.

But it's the same thing that's wrong with having one variable called
'q', when that variable would be better called 'invoice' because it's
iterating over your invoices, or 'item' when it's iterating over the
lines of one invoice. Python doesn't stop you from doing that; it's
not the language's job to enforce useful variable names.

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


Re: Re: Objects in Python

2012-08-22 Thread Chris Angelico
On Thu, Aug 23, 2012 at 2:49 PM, Evan Driscoll  wrote:
> On 8/22/2012 18:58, Ben Finney wrote:
>> You haven't discovered anything about types; what you have discovered is
>> that Python name bindings are not variables.
>>
>> In fact, Python doesn't have variables – not as C or Java programmers
>> would understand the term. What it has instead are references to objects
>> (with names as one kind of reference).
>
> OK, I've seen this said a few times, and I have to ask: what do you mean
> by this? I consider myself pretty decent at Python and other languages,
> and I really don't get it.

Simple example that'll work in many languages:

x = 1;

In C, this means: Assign the integer 1 to the variable x (possibly
with implicit type casting, eg to floating point).

In Java or C++, this means: Assign the integer 1 to the variable x.
Ditto, but possibly 'x' is actually a class member etc rather than a
simple variable.

In Python, this means: Make the name x now refer to the object 1.
Whatever x had before is de-referenced by one (in a simple refcounting
situation, that may result in the object being destroyed), and the
object referred to by the literal 1 is now pointed to by x.

Names are just one kind of reference because complex objects can have
unnamed references. For instance:

foo = [1, 2, 3]
foo[1] = "Hello, world!"

The second element of foo just got rebound in exactly the same way x
did, but it doesn't have a name of its own.

Does that sort things out, or just make things more confusing?

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