[Tutor] Bookpool sale on Addison Wesley

2007-08-08 Thread Kent Johnson
Bookpool is having a sale on all books from Addison-Wesley and Prentice 
Hall. In my opinion these are two of the best publishers for top-notch 
computer titles.

A few Python books on sale:
Core Python Programming $27.25
http://www.bookpool.com/sm/0132269937

Rapid Web Applications with TurboGears $24.50
http://www.bookpool.com/sm/0132433885

Some recommended non-Python books:
Design Patterns: Elements of Reusable Object-Oriented Software $32.95
http://www.bookpool.com/sm/0201633612

Refactoring: Improving the Design of Existing Code
http://www.bookpool.com/sm/0201485672

Agile Software Development Principles, Patterns, and Practices
http://www.bookpool.com/sm/0135974445

Extreme Programming Explained: Embrace Change, 2nd Edition
http://www.bookpool.com/sm/0321278658
and all the other books in this series

These are just a few personal favorites, there are many more excellent 
books on sale.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Shelve problem

2007-08-08 Thread Terry Carroll
On Wed, 8 Aug 2007, TheSarge wrote:

> I have five data files, that are used to build a database.
> 
> 1.txt
> 2.txt
> 3.txt
> 4.text
> 5.txt
> 
> I want to build a database using a persistent dictionary (shelve).
> 
> The specifications are that the key for each dictionary keyword pair, is the
> lefthand side 
> value of the # sign, and the corresponding value for the data is the phrase
> on the 
> righthand side.
> 
> Can someone help me manipulate this please? I can't not get a grasp on
> shelves and on what I need to do.

Basically, once you create a shelf object (which you do by calling 
shelve.open on a new or existing filename), you treat it like a 
dictionary.  When you close it, the values in the dictionary are saved.

For example, in one program, you would have:


shelf = shelve.open("testfile.shelf")

And later, lines like:

shelf["a"] = "Alligators all around" # just like a dictionary.
shelf["b"] = "Bursting balloons"
 . . .
shelf["z"] = "Zippety Zound"

and eventually:

shelf.close()


Then, in another program (or later in the same program) you could re-open 
the shelf-file and use it as a dictionary

shlf = shelv.open("testfile.shelf")

for key in shlf:
   print key, shlf[key]

and you should see, in arbitrary order, things like:

b Bursting balloons
r Riding reindeer
e Entertaining Elephants
a Aligators all around


Is this a homework problem, or a real-life application?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Shelve problem

2007-08-08 Thread TheSarge

I have five data files, that are used to build a database.

1.txt
2.txt
3.txt
4.text
5.txt

I want to build a database using a persistent dictionary (shelve).

The specifications are that the key for each dictionary keyword pair, is the
lefthand side 
value of the # sign, and the corresponding value for the data is the phrase
on the 
righthand side.

Can someone help me manipulate this please? I can't not get a grasp on
shelves and on what I need to do.

Thanks,

TheSarge

-- 
View this message in context: 
http://www.nabble.com/Shelve-problem-tf4239852.html#a12064251
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] httplib exceptions

2007-08-08 Thread Kent Johnson
Alan Gauld wrote:
>> There is a python Bug:
>> http://sourceforge.net/tracker/index.php?func=detail&aid=1486335&group_id=5470&atid=105470
>> 
>> that states this is caused by a missing EOF, and is "not a big deal"
> 
> Any chance that you are running into OS differences? eg Checking a 
> Unix file on
> a Windows box or vice versa? Windows expects an EOF at the end of a 
> file,
> Unix doesn't. Not sure what happens when you add in the complexity of
> sucking the file across the net though...

The problem is in httplib in the portion of the code that handles HTTP 
1.1 chunked transfers, so I don't think differences in file handling 
come in to play.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] httplib exceptions

2007-08-08 Thread Alan Gauld

"Bob Larsen" <[EMAIL PROTECTED]> wrote

It won't help with your problem but...

>try:
>page = urllib2.urlopen(url)
>soup= page.read()
>reex = re.compile(regex)
>test = re.findall(reex,soup)

You could change this to

  test = reex.findall(soup)

Its normal when compiling a regex to use the methods
of the compiled expression rather than to pass the
compiled regex to a module function.

>except ValueError,e:
>return 0
>if test:
>return 1
>else:
>return 0

And the if/else could be replaced with

return bool(test)

And on the real problem:

> There is a python Bug:
> http://sourceforge.net/tracker/index.php?func=detail&aid=1486335&group_id=5470&atid=105470
> 
> that states this is caused by a missing EOF, and is "not a big deal"

Any chance that you are running into OS differences? eg Checking a 
Unix file on
a Windows box or vice versa? Windows expects an EOF at the end of a 
file,
Unix doesn't. Not sure what happens when you add in the complexity of
sucking the file across the net though...

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] httplib exceptions

2007-08-08 Thread Kent Johnson
Bob Larsen wrote:
> I have written a script which checks all of my servers at predetermined 
> intervals. 
> 
> code:
> 
> try:
> page = urllib2.urlopen(url)
> soup= page.read()
> reex = re.compile(regex)
> test = re.findall(reex,soup)
> except ValueError,e:
> return 0
> if test:
> return 1
> else:
> return 0
> 
> Without the try clause the following exception is thrown:
> 
> Traceback (most recent call last):
>   File "~/webcheck2.py", line 116, in ?
> if sitetest(url,search):
>   File "~/webcheck2.py", line 71, in sitetest
> soup= page.read()
>   File "/usr/lib/python2.4/socket.py", line 285, in read
> data = self._sock.recv(recv_size)
>   File "/usr/lib/python2.4/httplib.py", line 460, in read
> return self._read_chunked(amt)
>   File "/usr/lib/python2.4/httplib.py", line 499, in _read_chunked
> chunk_left = int(line, 16)
> ValueError: invalid literal for int():
> 
> This script has been running well for months.  Within the past week it 
> has started misbehaving.
> 
> There is a python Bug:
> http://sourceforge.net/tracker/index.php?func=detail&aid=1486335&group_id=5470&atid=105470
>  
> 
> that states this is caused by a missing EOF, and is "not a big deal"
> 
> At this point I am confused.
> 
> What exactly is causing this problem that didn't exist until a week ago?
> 
> How can I work Around this?
> 
> Any Insight is greatly appreciated

Not much to add but sympathy...I occasionally see this error also when 
reading web pages from public servers.

My read on the bug report is
- they think it is an error in the data coming from the server
- they don't know the right way to fix it

So, has anything changed on the servers you are pinging? an upgrade to a 
new version or change to a different server?

I guess you could try the patch, I haven't been that adventurous.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] httplib exceptions

2007-08-08 Thread Bob Larsen
I have written a script which checks all of my servers at predetermined 
intervals. 

code:

try:
page = urllib2.urlopen(url)
soup= page.read()
reex = re.compile(regex)
test = re.findall(reex,soup)
except ValueError,e:
return 0
if test:
return 1
else:
return 0

Without the try clause the following exception is thrown:

Traceback (most recent call last):
  File "~/webcheck2.py", line 116, in ?
if sitetest(url,search):
  File "~/webcheck2.py", line 71, in sitetest
soup= page.read()
  File "/usr/lib/python2.4/socket.py", line 285, in read
data = self._sock.recv(recv_size)
  File "/usr/lib/python2.4/httplib.py", line 460, in read
return self._read_chunked(amt)
  File "/usr/lib/python2.4/httplib.py", line 499, in _read_chunked
chunk_left = int(line, 16)
ValueError: invalid literal for int():

This script has been running well for months.  Within the past week it 
has started misbehaving.

There is a python Bug:
http://sourceforge.net/tracker/index.php?func=detail&aid=1486335&group_id=5470&atid=105470
 

that states this is caused by a missing EOF, and is "not a big deal"

At this point I am confused.

What exactly is causing this problem that didn't exist until a week ago?

How can I work Around this?

Any Insight is greatly appreciated

Bob Larsen


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dial-up from Python

2007-08-08 Thread Luke Paireepinart
Diego Lindoso wrote:
>
>
> -- 
> Diego Lindoso.
> Fone: 3466-2387
> Cel :   9634-5993
Beeep! Beep beep boop boop beep
cs shhk shhk csh
[connection timeout]
-Luke

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comparing two numpy arrays

2007-08-08 Thread Alan Gauld

"Kent Johnson" <[EMAIL PROTECTED]> wrote

> No, a set is a sequence, you can convert it to a list directly:
> b = list(a)

But this is better than an LC obviously! :-)

Alan G

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] dial-up from Python

2007-08-08 Thread Diego Lindoso
-- 
Diego Lindoso.
Fone: 3466-2387
Cel :   9634-5993
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comparing two numpy arrays

2007-08-08 Thread Alan Gauld
"Andy Cheesman" <[EMAIL PROTECTED]> wrote

> only way of interconversion a brute force method?
> 
> i.e a = set([1, 2, 3])
> b = []
> for thing in a:
> b.append(thing)

Which looks a lot like a list comprehension:

b = [member for member in Set([1,2,3])]

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Removing tags with BeautifulSoup

2007-08-08 Thread Sebastien

Hi,

I'm in the process of cleaning some html files with BeautifulSoup and
I want to remove all traces of the tables. Here is the bit of the code
that deals with tables:

def remove(soup, tagname):
for tag in soup.findAll(tagname):
contents = tag.contents
parent = tag.parent
tag.extract()
for tag in contents:
parent.append(tag)

remove(soup, "table")
remove(soup, "tr")
remove(soup, "td")

It works fine but leaves an empty table structure at the end of the
soup. Like:

  

  



  



  ...

And the extract method of BeautifulSoup seems the extract only what is
in the tags.

So I'm just looking for a quick and dirty way to remove this table
structure at the end of the documents. I'm thinking with re but there
must be a way to do it with BeautifulSoup, maybe I'm missing
something.

An other thing that makes me wonder, this code:

for script in soup("script"):
soup.script.extract()

Works fine and remove script tags, but:

for table in soup("table"):
soup.table.extract()

Raises AttributeError: 'NoneType' object has no attribute 'extract'

Oh, and BTW, when I extract script tags this way, all the tag is gone,
like I want it, it doesn't only removes the content of the tag.

Thanks in advance 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] from netcat to socket

2007-08-08 Thread János Juhász
Dear All,

I made a small script to emulate a jetdirect device and capture the data 
sent 
from SAP to three separate barcode printers. 
I need it to make backup for the SAP printing, as I can place the captured 

files onto a local server and place a small batch file beside them,
that can be used to print the files without SAP.
My first version worked with nc.exe (netcat) like so :

from threading import Thread 
import os
import time

printers = (('10.36.24.40', 'front'),
('10.36.24.41', 'gable'),
('10.36.24.42', 'top'))


class CaptureThread(Thread):
def __init__(self, address, type):
Thread.__init__(self)
self.setDaemon(True)
self.address = address
self.type = type
self.port = 9100

def run(self):
command = 'nc -l -s %s -p %d > %s.prn' %(self.address, self.port, 
self.type)
print command
os.system(command)
print '%s is printed' % self.type
time.sleep(2) #  wait for two seconds


def capture():
print_threads = []
for ip, name in printers:
print_thread = CaptureThread(ip, name)
print_thread.start()
print_threads.append(print_thread)
# now wait for them to finish
for print_thread in print_threads:
print_thread.join()

if __name__ == '__main__':
while 1:
print '-'*30
capture()
#do stuff with the saved files



I tried to change it to be socket based like so:

from threading import Thread 
import os
import time
import socket

## Settings
threads = {'front':{'capt':('127.0.0.1', 9100), 'dest':('127.0.0.1', 
9100), 'thread':None},
   'gable':{'capt':('127.0.0.1', 9101), 'dest':('127.0.0.1', 
9101), 'thread':None},
   'top':  {'capt':('127.0.0.1', 9102), 'dest':('127.0.0.1', 
9102), 'thread':None},
   }

class PrinterThread(Thread):
def __init__(self, address, port):
Thread.__init__(self)
self.setDaemon(True)
self.address = address
self.port = port
self.content = ''
self.soc = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
self.soc.bind((address, port))
self.soc.listen(1) 

def run(self):
try:
conn, addr = self.soc.accept()
while 1:
data = conn.recv(1024)
self.content += data
if not data:
conn.close()
break
except:
print 'exception (connection closed)'





So the question is, how translate 

command = 'nc -l -s %s -p %d > %s.prn' %(self.address, self.port, 
self.type)
os.system(command)

to be socket based.

I also would ask your opinions about the structure of 'threads'.


Regards,
Janos
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to sort a dictionary by values

2007-08-08 Thread shawn bright
sorry all, i did mean greatest to least, thanks for all the help here
shawn

On 8/8/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> Tiger12506 wrote:
> >> Just curious: Is there a reason to use __getitem__() over itemgetter
> (used
> >> in the example in my reply)?
> >
> > __getitem__ is a method builtin to a dict object. itemgetter 1) has to
> be
> > imported 2) is more generically used, therefore probably using a more
> > generic/slower algorithm
>
> itemgetter() written in C and should be pretty fast. The actual sort
> algorithm is the same in either case, in fact the list of keys to be
> sorted is the same in either case.
>
> On my computer my version seems to be a little faster on random data but
> as I noted in a separate email, the results are different so the
> algorithm should be chosen based on the desired results.
>
> In [27]: import random
> In [28]: d = dict( (i, random.random()) for i in range(1000) )
> In [29]: import timeit
> In [34]: timeit.Timer('sorted(d.keys(), key=d.__getitem__,
> reverse=True)', 'from __main__ import d').timeit(1)
> Out[34]: 7.3717570304870605
> In [38]: timeit.Timer('sorted(d.items(), key=operator.itemgetter(1),
> reverse=True)', 'from __main__ import d; import operator').timeit(1)
> Out[38]: 8.2723259925842285
>
> Kent
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comparing two numpy arrays

2007-08-08 Thread Kent Johnson
Andy Cheesman wrote:
> I've googled a bit for manipulation of
> sets into other data structure(lists, arrays) and not seen much. Is the
> only way of interconversion a brute force method?
> 
> i.e   a = set([1, 2, 3])
>   b = []
>   for thing in a:
>   b.append(thing)

No, a set is a sequence, you can convert it to a list directly:
b = list(a)

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to sort a dictionary by values

2007-08-08 Thread Kent Johnson
Tiger12506 wrote:
>> Just curious: Is there a reason to use __getitem__() over itemgetter (used 
>> in the example in my reply)?
> 
> __getitem__ is a method builtin to a dict object. itemgetter 1) has to be 
> imported 2) is more generically used, therefore probably using a more 
> generic/slower algorithm

itemgetter() written in C and should be pretty fast. The actual sort 
algorithm is the same in either case, in fact the list of keys to be 
sorted is the same in either case.

On my computer my version seems to be a little faster on random data but 
as I noted in a separate email, the results are different so the 
algorithm should be chosen based on the desired results.

In [27]: import random
In [28]: d = dict( (i, random.random()) for i in range(1000) )
In [29]: import timeit
In [34]: timeit.Timer('sorted(d.keys(), key=d.__getitem__, 
reverse=True)', 'from __main__ import d').timeit(1)
Out[34]: 7.3717570304870605
In [38]: timeit.Timer('sorted(d.items(), key=operator.itemgetter(1), 
reverse=True)', 'from __main__ import d; import operator').timeit(1)
Out[38]: 8.2723259925842285

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to sort a dictionary by values

2007-08-08 Thread Kent Johnson
wormwood_3 wrote:
>>> You can use d.__getitem__ as the key function for a sort of the keys. 
>>> __getitem__() is the special method that is called for indexing a 
>>> dictionary (or a list).
> 
> Just curious: Is there a reason to use __getitem__() over itemgetter
> (used in the example in my reply)?

The results are different. My code gives a list of keys, sorted by 
value, which was how I interpreted the request. Your code gives a list 
of key, value pairs, sorted by value.

It's easy enough to change your list to just keys with a list 
comprehension. Then you are essentially doing a sort using 
decorate-sort-undecorate (except the decorate step builds the tuples in 
the wrong order). You could think of the sort using key=d.__getitem__ as 
the modern equivalent of your DSU sort.

OTOH if the OP really *wants* a sorted list of key, value pairs, your 
version is fine.

>>> In [24]: d = {'a':21.3, 'b':32.8, 'c': 12.92}
>>> In [26]: sorted(d.keys(), key=d.__getitem__, reverse=True)
> 
> I think Shawn would want to leave off "reverse=True". The default is
> least to greatest, which is what he wanted, I think.

Possibly. He did say "least to greatest" but his example output is 
greatest to least.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comparing two numpy arrays

2007-08-08 Thread Andy Cheesman
Thats a great solution, thanks! I've googled a bit for manipulation of
sets into other data structure(lists, arrays) and not seen much. Is the
only way of interconversion a brute force method?

i.e a = set([1, 2, 3])
b = []
for thing in a:
b.append(thing)

Andy


Bob Gailer wrote:
> Eric Brunson wrote:
>> Bob Gailer wrote:
>>> Andy Cheesman wrote:
>>>  
 Hi people,

 If I've two numpy arrays, is there a non-looping way of finding common
 values. (the example below has identical shapes for the arrays but this
 may not be the case in my scenario)

 e.g
 a = array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 b = array([ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

 answer = array([ 5,6,7,8,9])
 
>>> Set union?
>>>
>>>   
>> Did you mean Set intersection?
> Yes. Sigh.
>>
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Large Scale Python websites

2007-08-08 Thread OkaMthembo
On 8/7/07, Mike Hansen <[EMAIL PROTECTED]> wrote:
>
> > >> Is anyone aware of any large scale web apps developed in Python?
> > >> Please let
> > >> me know of any that you know of...
>
> I think that reddit.com switched from LISP to Python a while back.
>
> Mike
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>


Interesting to hear these examples, especially YouTube. Apparently, Yelp.comand
Slide.com also use the beautiful language.

Thanks to everyone,


-- 
Sithembewena Lloyd Dube

"The Stupidry Foundry"
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Checking for custom error codes

2007-08-08 Thread Alan Gauld

"wormwood_3" <[EMAIL PROTECTED]> wrote

>def lookup(self):
> ...
>for potdomain in self.potdomains:
>try:
>who.whois(potdomain)
>self.availdomains.append(potdomain)
>except 'NoSuchDomain':
>pass

> This may, however, be something else wrong with my code, or the 
> rwhois module, not the try, except check.

You couldcheck that the except is working by replacing the pass
statement with a print statement. If the print statement shows
up you know the except worked so you can put the pass back in.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor