HTTPConncetion - HEAD request

2011-06-16 Thread gervaz
Hi all, can someone tell me why the read() function in the following
py3 code returns b''?

>>> h = http.client.HTTPConnection("www.twitter.com")
>>> h.connect()
>>> h.request("HEAD", "/", "HTTP 1.0")
>>> r = h.getresponse()
>>> r.read()
b''

Thanks,

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


Re: HTTPConncetion - HEAD request

2011-06-17 Thread gervaz
On 17 Giu, 01:00, Ian Kelly  wrote:
> On Thu, Jun 16, 2011 at 4:43 PM, gervaz  wrote:
> > Hi all, can someone tell me why the read() function in the following
> > py3 code returns b''?
>
> >>>> h = http.client.HTTPConnection("www.twitter.com")
> >>>> h.connect()
> >>>> h.request("HEAD", "/", "HTTP 1.0")
> >>>> r = h.getresponse()
> >>>> r.read()
> > b''
>
> You mean why does it return an empty byte sequence?  Because the HEAD
> method only requests the response headers, not the body, so the body
> is empty.  If you want to see the response body, use GET.
>
> Cheers,
> Ian

The fact is that I have a list of urls and I wanted to retrieve the
minimum necessary information in order to understand if the link is a
valid html page or e.g. a picture or something else. As far as I
understood here http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
the HEAD command is the one that let you do this. But it seems it
doesn't work.

Any help?

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


Re: HTTPConncetion - HEAD request

2011-06-17 Thread gervaz
On 17 Giu, 12:14, Adam Tauno Williams  wrote:
> On Thu, 2011-06-16 at 15:43 -0700, gervaz wrote:
> > Hi all, can someone tell me why the read() function in the following
> > py3 code returns b''
> > >>> h = http.client.HTTPConnection("www.twitter.com")
> > >>> h.connect()
> > >>> h.request("HEAD", "/", "HTTP 1.0")
> > >>> r = h.getresponse()
> > >>> r.read()
> > b''
>
> Because there is no body in a HEAD request.  What is useful are the
> Content-Type, Content-Length, and etag headers.
>
> Is r.getcode() == 200?  That indicates a successful response; you
> *always* much check the response code before interpreting the response.
>
> Also I'm pretty sure that "HTTP 1.0" is wrong.

Ok, thanks for the replies, just another question in order to have a
similar behaviour using a different approach...
I decided to implement this solution:

class HeadRequest(urllib.request.Request):
def get_method(self):
return "HEAD"

Now I download the url using:

r = HeadRequest(url, None, self.headers)
c = urllib.request.urlopen(r)

but I don't know how to retrieve the request status (e.g. 200) as in
the previous examples with a different implementation...

Any suggestion?

Thanks,

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


Python scoping

2011-06-20 Thread gervaz
Hi all, can you explain me why this simple function works well (i.e. I
can call the print function using txt) in py

>>> def test(value):
... if value%5: txt = "hello"
... else: txt = "test"
... print(txt)

while in other languages like C the txt identifier would be undefined?
Is there a way to force the scoping?


Thanks,

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


Re: Python scoping

2011-06-21 Thread gervaz
On 21 Giu, 06:06, Ben Finney  wrote:
> Chris Angelico  writes:
> > On Tue, Jun 21, 2011 at 12:38 PM, Ben Finney  
> > wrote:
> > > The *binding* is scoped.
>
> > And the binding follows the exact same rules as anything else would.
> > It has scope and visibility. In terms of the OP, the binding IS like a
> > variable.
>
> Yes. So let's stop behaving as though the *name* behaves like a
> variable. It isn't, and doesn't.
>
> --
>  \          “Computer perspective on Moore's Law: Human effort becomes |
>   `\           twice as expensive roughly every two years.” —anonymous |
> _o__)                                                                  |
> Ben Finney

Ok, thanks for the clarification, I'll take extra care e.g. when
dealing with exceptions.

Ciao,

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


Get the name of a function

2011-08-05 Thread gervaz
Hi all, is there a way to retrive the function name like with
self.__class__.__name__?

Using self.__dict__.__name__ I've got

>>> def test():
... print(self.__dict__.__name__)
...
>>> test


But I really just want the function name, so 'test'

Any help?

Thanks,

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


Use cookies from a script in a browser

2011-03-18 Thread gervaz
Hi all,
I use a scraper to retrieve data from a web page.
In order to do that I need to enable cookies.
The data that I'm looking for is contained in a bunch of web pages.
Is there a way to show this web pages in a browser using the cookies
used in the script (otherwise it doesn't work).

Thanks,

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


Re: Use cookies from a script in a browser

2011-03-18 Thread gervaz
On 18 Mar, 22:52, Miki Tebeka  wrote:
> You can use mechanize, which holds a cookie jar and can user the browser 
> cookies as well.

I use:
opener =
urllib.request.build_opener(urllib.request.HTTPCookieProcessor())
urllib.request.install_opener(opener)
I start scraping from http://page.com/home.html that need the cookies
enabled, then, once I found what I was looking for, I've got
http://page.com/pageX.html and http://page.com/pageY.html. Those page
cannot be viewed if I just copy & paste the url in the browser,
because they need the cookies.

What can I do?

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


Proxy authentication required

2011-03-21 Thread gervaz
Hi all,
I've got to download some web pages but I'm behind a proxy. So far
this is what I've used without any successful result receiving the
error: "urllib.error.HTTPError: HTTP Error 407: Proxy Authentication
Required ( The ISA Server requires auth
orization to fulfill the request. Access to the Web Proxy filter is
denied.  )":

hc = urllib.request.HTTPCookieProcessor()
hp = urllib.request.ProxyHandler({"http": "10.242.38.251:80",
"username": "domain\username", "password": "password"})
opener = urllib.request.build_opener(hc, hp)
urllib.request.urlopen("http://www.google.it/";)

Any suggestion?

Thanks,

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


Re: Proxy authentication required

2011-03-22 Thread gervaz
On 22 Mar, 00:02, Chris Rebert  wrote:
> On Mon, Mar 21, 2011 at 2:38 AM, gervaz  wrote:
> > Hi all,
> > I've got to download some web pages but I'm behind a proxy. So far
> > this is what I've used without any successful result receiving the
> > error: "urllib.error.HTTPError: HTTP Error 407: Proxy Authentication
> > Required ( The ISA Server requires auth
> > orization to fulfill the request. Access to the Web Proxy filter is
> > denied.  )":
>
> > hc = urllib.request.HTTPCookieProcessor()
> > hp = urllib.request.ProxyHandler({"http": "10.242.38.251:80",
> > "username": "domain\username", "password": "password"})
>
> Remember that backslash is used for string escapes in Python; so that
> should be "domain\\username" in order to get 1 literal backslash. I
> suspect this is the cause of your proxy authentication problem.
>
> > opener = urllib.request.build_opener(hc, hp)
>
> Are you sure that's the right order for the handlers? (I don't know myself.)
>
> > urllib.request.urlopen("http://www.google.it/";)
>
> Cheers,
> Chris
> --
> Windows, Y U use backslash for stuff!?http://blog.rebertia.com

Hi Cris,
I had already tested the solution with the '\\' but the result is the
same. As per the arguments order, build_opener thakes *handlers as
argument so no problem.

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


Re: Proxy authentication required

2011-03-22 Thread gervaz
On 22 Mar, 09:34, gervaz  wrote:
> On 22 Mar, 00:02, Chris Rebert  wrote:
>
>
>
>
>
> > On Mon, Mar 21, 2011 at 2:38 AM, gervaz  wrote:
> > > Hi all,
> > > I've got to download some web pages but I'm behind a proxy. So far
> > > this is what I've used without any successful result receiving the
> > > error: "urllib.error.HTTPError: HTTP Error 407: Proxy Authentication
> > > Required ( The ISA Server requires auth
> > > orization to fulfill the request. Access to the Web Proxy filter is
> > > denied.  )":
>
> > > hc = urllib.request.HTTPCookieProcessor()
> > > hp = urllib.request.ProxyHandler({"http": "10.242.38.251:80",
> > > "username": "domain\username", "password": "password"})
>
> > Remember that backslash is used for string escapes in Python; so that
> > should be "domain\\username" in order to get 1 literal backslash. I
> > suspect this is the cause of your proxy authentication problem.
>
> > > opener = urllib.request.build_opener(hc, hp)
>
> > Are you sure that's the right order for the handlers? (I don't know myself.)
>
> > > urllib.request.urlopen("http://www.google.it/";)
>
> > Cheers,
> > Chris
> > --
> > Windows, Y U use backslash for stuff!?http://blog.rebertia.com
>
> Hi Cris,
> I had already tested the solution with the '\\' but the result is the
> same. As per the arguments order, build_opener thakes *handlers as
> argument so no problem.
>
> Mattia- Nascondi testo citato
>
> - Mostra testo citato -

Further investigating the issue the proxy needs NTLM authentication,
for that reason no solution so far works. I'm using py3.

Thanks,

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


Regexp

2009-01-19 Thread gervaz
Hi all, I need to find all the address in a html source page, I'm
using:
'href="(?Phttp://mysite.com/[^"]+)">()?(?P[^]+)()?'
but the [^]+ pattern retrieve all the strings not containing <
or / or a etc, although I just not want the word "". How can I
specify: 'do not search the string "blabla"?'

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


Re: Regexp

2009-01-19 Thread gervaz
On Jan 19, 4:01 pm, Ant  wrote:
> A 0-width positive lookahead is probably what you want here:
>
> >>> s = """
>
> ... hdhd http://mysite.com/blah.html";>Test String OK a>
> ...
> ... """>>> p = r'href="(http://mysite.com/[^"]+)">(.*)(?=)'
> >>> m = re.search(p, s)
> >>> m.group(1)
>
> 'http://mysite.com/blah.html'>>> m.group(2)
>
> 'Test String OK'
>
> The (?=...) bit is the lookahead, and won't consume any of the string
> you are searching. I've binned the named groups for clarity.
>
> The beautiful soup answers are a better bet though - they've already
> done the hard work, and after all, you are trying to roll your own
> partial HTML parser here, which will struggle with badly formed html...

Ok, thank you all, I'll take a look at beautiful soup, albeit the
lookahead solution fits better for the little I have to do.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Correct URL encoding

2009-03-15 Thread gervaz
On Mar 16, 12:38 am, Graham Breed  wrote:
> mattia wrote:
> > I'm using urlopen in order to download some web pages. I've always to
> > replace some characters that are in the url, so I've come up with:
> > url.replace("|", "%7C").replace("/", "%2F").replace(" ", "+").replace
> > (":", "%3A")
> > There isn't a better way of doing this?
>
> Yeah, shame there's no function -- called "urlencode" say --
> that does it all for you.
>
>                        Graham

Sorry, but using Python 2.6 and urlencode I've got this error:
TypeError: not a valid non-string sequence or mapping object
What I was looking for (found in Python3) is:
from urllib.parse import quote
urlopen(quote(url)).read()
but seems there is nothing similar in py2.6
--
http://mail.python.org/mailman/listinfo/python-list


py3.x urllib.request - HTTPCookieProcessor and header adding

2009-12-16 Thread gervaz
Hi all, I need to fetch some html pages and it is required to have
cookies enabled. So, I'm using

opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor
())
urllib.request.install_opener(opener)

as a global instance. Is there a way to always use a default header
like:

{"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:
1.9.1.5) Gecko/20091102 Firefox/3.5.5"}

instead of always creating urllib.request.Request(my_url, None,
headers) and call different urls with the same
cookies?

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


putchar(8)

2009-10-16 Thread gervaz
Hi all, is there a pythonic way to have the
-- 
http://mail.python.org/mailman/listinfo/python-list


putchar(8)

2009-10-16 Thread gervaz
Hi all, is there in python the equivalent of the C function int putchar
(int c)? I need to print putchar(8).

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


Signal handler & cygwin

2010-12-25 Thread gervaz
Hi all, given the followin code snippet:

import signal
import time
import sys
import os

print("{0}\n".format(os.getpid()))

ContinueProcessing = True

def stop(signal, frame):
print("\nSignal received!\n")
time.sleep(1)
global ContinueProcessing
ContinueProcessing = False

signal.signal(signal.SIGINT, stop)

while ContinueProcessing:
try:
sys.stdout.write(".")
sys.stdout.flush()
time.sleep(0.1)
except:
continue

if I kill the process using kill -s SIGINT PID in linux everything
works fine and the signal is correctly handled, while in win, using
cywin (/bin/kill.exe -f -s SIGINT WINPID), the program just exits
without trapping the interrupt.

Any similar experience?
-- 
http://mail.python.org/mailman/listinfo/python-list


Interrput a thread

2010-12-29 Thread gervaz
Hi all, I need to stop a threaded (using CTR+C or kill) application if
it runs too much or if I decide to resume the work later.
I come up with the following test implementation but I wanted some
suggestion from you on how I can implement what I need in a better or
more pythonic way. Here the code:

import os
import signal
import time
from threading import Thread, current_thread
from queue import LifoQueue, Empty

COMMAND = {"STOP": 0, "NORMAL": 1}
THREAD_NUM = 5

lq = LifoQueue()

print("{0}\n".format(os.getpid()))

class InterceptInterrupt(Exception):
pass

class Handler:
def __init__(self, queue):
self._queue = queue
def __del__(self):
print("Bye bye!")
def getHandler(self, signum, frame):
print("Interrupt raised!")
for _ in range(THREAD_NUM):
self._queue.put((COMMAND["STOP"], None))
raise InterceptInterrupt

h = Handler(lq)

signal.signal(signal.SIGINT, h.getHandler)

for i in range(25):
lq.put((COMMAND["NORMAL"], i))

def do_work(queue):
while True:
time.sleep(5)
try:
cmd, value = queue.get(block=False)
if cmd == COMMAND["STOP"]:
print("{0}: STOP command
received!".format(current_thread().name))
break
elif cmd == COMMAND["NORMAL"]:
print(value)
except Empty:
break

threads = [Thread(target=do_work, args=(lq,)) for _ in
range(THREAD_NUM)]

for t in threads:
t.start()

while not lq.empty():
try:
time.sleep(1)
except (IOError, InterceptInterrupt):
break

for t in threads:
t.join()

if lq.empty():
print("The queue is empty.")
else:
print("The queue is NOT empty. Some additional work has to be
done.")

Thank you,
Mattia
-- 
http://mail.python.org/mailman/listinfo/python-list


String building using join

2010-12-31 Thread gervaz
Hi all, I would like to ask you how I can use the more efficient join
operation in a code like this:

>>> class Test:
... def __init__(self, v1, v2):
... self.v1 = v1
... self.v2 = v2
...
>>> def prg(l):
... txt = ""
... for x in l:
... if x.v1 is not None:
... txt += x.v1 + "\n"
... if x.v2 is not None:
... txt += x.v2 + "\n"
... return txt
...
>>> t1 = Test("hello", None)
>>> t2 = Test(None, "ciao")
>>> t3 = Test("salut", "hallo")
>>> t = [t1, t2, t3]
>>>
>>> prg(t)
'hello\nciao\nsalut\nhallo\n'

The idea would be create a new list with the values not None and then
use the join function... but I don't know if it is really worth it.
Any hint?

>>> def prg2(l):
... e = []
... for x in l:
... if x.v1 is not None:
... e.append(x.v1)
... if x.v2 is not None:
... e.append(x.v2)
... return "\n".join(e)
...
>>> prg2(t)
'hello\nciao\nsalut\nhallo'

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


Re: String building using join

2011-01-02 Thread gervaz
On 31 Dic 2010, 16:43, Emile van Sebille  wrote:
> On 12/31/2010 7:22 AM gervaz said...
>
>
>
>
>
> > Hi all, I would like to ask you how I can use the more efficient join
> > operation in a code like this:
>
> >>>> class Test:
> > ...     def __init__(self, v1, v2):
> > ...         self.v1 = v1
> > ...         self.v2 = v2
> > ...
> >>>> def prg(l):
> > ...     txt = ""
> > ...     for x in l:
> > ...         if x.v1 is not None:
> > ...             txt += x.v1 + "\n"
> > ...         if x.v2 is not None:
> > ...             txt += x.v2 + "\n"
> > ...     return txt
> > ...
> >>>> t1 = Test("hello", None)
> >>>> t2 = Test(None, "ciao")
> >>>> t3 = Test("salut", "hallo")
> >>>> t = [t1, t2, t3]
>
> >>>> prg(t)
> > 'hello\nciao\nsalut\nhallo\n'
>
> > The idea would be create a new list with the values not None and then
> > use the join function... but I don't know if it is really worth it.
> > Any hint?
>
> >>>> def prg2(l):
>
>              return "\n".join([x for x in l if x])
>
> Emile
>
>
>
> > ...     e = []
> > ...     for x in l:
> > ...         if x.v1 is not None:
> > ...             e.append(x.v1)
> > ...         if x.v2 is not None:
> > ...             e.append(x.v2)
> > ...     return "\n".join(e)
> > ...
> >>>> prg2(t)
> > 'hello\nciao\nsalut\nhallo'
>
> > Thanks, Mattia- Nascondi testo citato
>
> - Mostra testo citato -- Nascondi testo citato
>
> - Mostra testo citato -

Sorry, but it does not work

>>> def prg3(l):
... return "\n".join([x for x in l if x])
...
>>> prg3(t)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in prg3
TypeError: sequence item 0: expected str instance, Test found
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Interrput a thread

2011-01-02 Thread gervaz
On 31 Dic 2010, 23:25, Alice Bevan–McGregor 
wrote:
> On 2010-12-31 10:28:26 -0800, John Nagle said:
>
> > Even worse, sending control-C to a multi-thread program
> > is unreliable in CPython.  See "http://blip.tv/file/2232410";
> > for why.  It's painful.
>
> AFIK, that has been resolved in Python 3.2 with the introduction of an
> intelligent thread scheduler as part of the GIL release/acquire process.
>
>         - Alice.

Ok, but then suppose I have multiple long running threads that I want
to delete/suspend because they are tooking too much time, which
solution do you propose?

Thanks,

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


Re: String building using join

2011-01-02 Thread gervaz
On 2 Gen, 19:14, Emile van Sebille  wrote:
> On 1/2/2011 9:43 AM gervaz said...
>
>
>
>
>
> > On 31 Dic 2010, 16:43, Emile van Sebille  wrote:
> >> On 12/31/2010 7:22 AM gervaz said...
>
> >>> Hi all, I would like to ask you how I can use the more efficient join
> >>> operation in a code like this:
>
> >>>>>> class Test:
> >>> ...     def __init__(self, v1, v2):
> >>> ...         self.v1 = v1
> >>> ...         self.v2 = v2
> >>> ...
> >>>>>> def prg(l):
> >>> ...     txt = ""
> >>> ...     for x in l:
> >>> ...         if x.v1 is not None:
> >>> ...             txt += x.v1 + "\n"
> >>> ...         if x.v2 is not None:
> >>> ...             txt += x.v2 + "\n"
> >>> ...     return txt
> >>> ...
> >>>>>> t1 = Test("hello", None)
> >>>>>> t2 = Test(None, "ciao")
> >>>>>> t3 = Test("salut", "hallo")
> >>>>>> t = [t1, t2, t3]
>
> >>>>>> prg(t)
> >>> 'hello\nciao\nsalut\nhallo\n'
>
> >>> The idea would be create a new list with the values not None and then
> >>> use the join function... but I don't know if it is really worth it.
> >>> Any hint?
>
> >>>>>> def prg2(l):
>
> >>               return "\n".join([x for x in l if x])
>
> >> Emile
>
> >>> ...     e = []
> >>> ...     for x in l:
> >>> ...         if x.v1 is not None:
> >>> ...             e.append(x.v1)
> >>> ...         if x.v2 is not None:
> >>> ...             e.append(x.v2)
> >>> ...     return "\n".join(e)
> >>> ...
> >>>>>> prg2(t)
> >>> 'hello\nciao\nsalut\nhallo'
>
> >>> Thanks, Mattia- Nascondi testo citato
>
> >> - Mostra testo citato -- Nascondi testo citato
>
> >> - Mostra testo citato -
>
> > Sorry, but it does not work
>
> Oh -- you want a working solution, not a hint?  OK.
>
> class Test:
>       def __init__(self, v1, v2):
>           self.v1 = v1
>           self.v2 = v2
>
> t1 = Test("hello", None)
> t2 = Test(None, "ciao")
> t3 = Test("salut", "hallo")
> t = [t1, t2, t3]
>
> "\n".join([y for x in t for y in [x.v1,x.v2] if y])
>
> Emile
>
>
>
>
>
> >>>> def prg3(l):
> > ...     return "\n".join([x for x in l if x])
> > ...
> >>>> prg3(t)
> > Traceback (most recent call last):
> >    File "", line 1, in
> >    File "", line 2, in prg3
> > TypeError: sequence item 0: expected str instance, Test found- Nascondi 
> > testo citato
>
> - Mostra testo citato -- Nascondi testo citato
>
> - Mostra testo citato -

Thanks Emile, despite that now the solution runs in quadratic time I
guess. I could also provide a __str__(self) representation, but in my
real code I don't have access to the class. Also using str() on an
empty object (i.e. None), the representation is 'None'.

Ciao,

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


Re: String building using join

2011-01-02 Thread gervaz
On 2 Gen, 22:37, gervaz  wrote:
> On 2 Gen, 19:14, Emile van Sebille  wrote:
>
>
>
>
>
> > On 1/2/2011 9:43 AM gervaz said...
>
> > > On 31 Dic 2010, 16:43, Emile van Sebille  wrote:
> > >> On 12/31/2010 7:22 AM gervaz said...
>
> > >>> Hi all, I would like to ask you how I can use the more efficient join
> > >>> operation in a code like this:
>
> > >>>>>> class Test:
> > >>> ...     def __init__(self, v1, v2):
> > >>> ...         self.v1 = v1
> > >>> ...         self.v2 = v2
> > >>> ...
> > >>>>>> def prg(l):
> > >>> ...     txt = ""
> > >>> ...     for x in l:
> > >>> ...         if x.v1 is not None:
> > >>> ...             txt += x.v1 + "\n"
> > >>> ...         if x.v2 is not None:
> > >>> ...             txt += x.v2 + "\n"
> > >>> ...     return txt
> > >>> ...
> > >>>>>> t1 = Test("hello", None)
> > >>>>>> t2 = Test(None, "ciao")
> > >>>>>> t3 = Test("salut", "hallo")
> > >>>>>> t = [t1, t2, t3]
>
> > >>>>>> prg(t)
> > >>> 'hello\nciao\nsalut\nhallo\n'
>
> > >>> The idea would be create a new list with the values not None and then
> > >>> use the join function... but I don't know if it is really worth it.
> > >>> Any hint?
>
> > >>>>>> def prg2(l):
>
> > >>               return "\n".join([x for x in l if x])
>
> > >> Emile
>
> > >>> ...     e = []
> > >>> ...     for x in l:
> > >>> ...         if x.v1 is not None:
> > >>> ...             e.append(x.v1)
> > >>> ...         if x.v2 is not None:
> > >>> ...             e.append(x.v2)
> > >>> ...     return "\n".join(e)
> > >>> ...
> > >>>>>> prg2(t)
> > >>> 'hello\nciao\nsalut\nhallo'
>
> > >>> Thanks, Mattia- Nascondi testo citato
>
> > >> - Mostra testo citato -- Nascondi testo citato
>
> > >> - Mostra testo citato -
>
> > > Sorry, but it does not work
>
> > Oh -- you want a working solution, not a hint?  OK.
>
> > class Test:
> >       def __init__(self, v1, v2):
> >           self.v1 = v1
> >           self.v2 = v2
>
> > t1 = Test("hello", None)
> > t2 = Test(None, "ciao")
> > t3 = Test("salut", "hallo")
> > t = [t1, t2, t3]
>
> > "\n".join([y for x in t for y in [x.v1,x.v2] if y])
>
> > Emile
>
> > >>>> def prg3(l):
> > > ...     return "\n".join([x for x in l if x])
> > > ...
> > >>>> prg3(t)
> > > Traceback (most recent call last):
> > >    File "", line 1, in
> > >    File "", line 2, in prg3
> > > TypeError: sequence item 0: expected str instance, Test found- Nascondi 
> > > testo citato
>
> > - Mostra testo citato -- Nascondi testo citato
>
> > - Mostra testo citato -
>
> Thanks Emile, despite that now the solution runs in quadratic time I
> guess. I could also provide a __str__(self) representation, but in my
> real code I don't have access to the class. Also using str() on an
> empty object (i.e. None), the representation is 'None'.
>
> Ciao,
>
> Mattia- Nascondi testo citato
>
> - Mostra testo citato -

And this one is another working solution...

>>> def prg4(l):
... s = []
... for x in l:
... s.extend(set([x.v1, x.v2]) - set([None]))
... return "\n".join(s)
...
>>> prg4(t)
'hello\nciao\nhallo\nsalut'

My original question was just related to the fact that I read that the
string concatenation in expensive and it sould be used the join()
function but now probably it is better to stick with the first
implementation, the simplest one.

Ciao,

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


Re: Interrput a thread

2011-01-03 Thread gervaz
On 3 Gen, 17:47, de...@web.de (Diez B. Roggisch) wrote:
> gervaz  writes:
> > On 31 Dic 2010, 23:25, Alice Bevan–McGregor 
> > wrote:
> >> On 2010-12-31 10:28:26 -0800, John Nagle said:
>
> >> > Even worse, sending control-C to a multi-thread program
> >> > is unreliable in CPython.  See "http://blip.tv/file/2232410";
> >> > for why.  It's painful.
>
> >> AFIK, that has been resolved in Python 3.2 with the introduction of an
> >> intelligent thread scheduler as part of the GIL release/acquire process.
>
> >>         - Alice.
>
> > Ok, but then suppose I have multiple long running threads that I want
> > to delete/suspend because they are tooking too much time, which
> > solution do you propose?
>
> If possible, use multiple processes instead.
>
> Diez- Nascondi testo citato
>
> - Mostra testo citato -

Multiple processes, ok, but then regarding processes' interruption
there will be the same problems pointed out by using threads?

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


Re: Interrput a thread

2011-01-03 Thread gervaz
On 3 Gen, 22:17, Adam Skutt  wrote:
> On Jan 3, 4:06 pm, Jean-Paul Calderone 
> wrote:
>
>
>
> > > Multiple processes, ok, but then regarding processes' interruption
> > > there will be the same problems pointed out by using threads?
>
> > No.  Processes can be terminated easily on all major platforms.  See
> > `os.kill`.
>
> Yes, but that's not the whole story, now is it?  It's certainly much
> more reliable and easier to kill a process.  It's not any easier to do
> it and retain defined behavior, depending on exactly what you're
> doing.  For example, if you kill it while it's in the middle of
> updating shared memory, you can potentially incur undefined behavior
> on the part of any process that can also access shared memory.
>
> In short, taking a program that uses threads and shared state and
> simply replacing the threads with processes will likely not gain you a
> thing.  It entirely depends on what those threads are doing and how
> they do it.
>
> Adam

As per the py3.1 documentation, os.kill is only available in the Unix
os. Regarding the case pointed out by Adam I think the best way to
deal with it is to create a critical section so that the shared memory
will be updated in an atomic fashion. Btw it would be useful to take a
look at some actual code/documentation in order to understand how
others dealt with the problem...

Ciao,

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


Re: Interrput a thread

2011-01-04 Thread gervaz
On 4 Gen, 07:13, Jean-Paul Calderone 
wrote:
> On Jan 3, 6:17 pm, Adam Skutt  wrote:
>
> > On Jan 3, 5:24 pm, Jean-Paul Calderone 
> > wrote:
>
> > > Of course.  The whole point here is not about threads vs processes.
> > > It's about shared memory concurrency vs non-shared memory
> > > concurrency.  You can implement both with threads and both with
> > > processes, but threads are geared towards shared memory and processes
> > > are geared towards non-shared memory.  So what most people mean by
> > > "use processes" is "don't use shared memory".
>
> > This is entirely my presumption, but I think if the OP were keenly
> > aware of the differences between thread and processes, it's pretty
> > likely he wouldn't have asked his question in the first place.
>
> Fair enough. :)
>
> > Also, I've written lots and lots of "use processes" code on multiple
> > platforms, and much of it has used some sort of shared memory
> > construct.  It's actually pretty common, especially in code bases with
> > a lot of history.  Not all the world is Apache.
>
> Hee hee, Apache. :)
>
>
>
> > Adam- Nascondi testo citato
>
> - Mostra testo citato -

BTW thanks for the suggestions. As in my original code snippet, the
shared object that the threads are using is the Queue, that itself
implement locking mechanism so I don't have to worry about concurrent
access. Regarding my question, it was just to have a hint on how a
thread termination can be handled as, per example, you have the
consumer-producer pattern.

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