[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

access to base class __init__

sammy

3/6/2008 12:10:00 AM

I got myself in jam trying to be too fancy with threading.Thread
Docs say / remind to call the base __init__
but I can't fighure out how.

---------------------------
def main()
......
ls.listen(5)
key = ' '
# while key != EXITCHARCTER:
while stop_serving == False:
cs, raddr = ls.accept()
print "Main_Thread: ",cs, raddr
nt = client_socket_handler( cs, raddr )
print threading.enumerate()
key = getkey()

# ls.close()
time.sleep(4)
print "Server Exiting."

class client_socket_handler(threading.Thread):
def __init__(self, cs, remote_address):
???????????????????????
self.threading.Thread.__init__(self,self.socket_handler,None,None)
self.socket = cs
self.rhost_addr = remote_address
print "client_socket_handler.__init__(): ", self.socket,
self.rhost_addr
# t1 = threading.Thread( None,self.socket_handler, None, (5,78) )
# t1.start()
self.start()
print "client_socket_handler.__init__(): ", self.socket,
self.rhost_addr
print "client_socket_handler.__init__(): enumerate()",
threading.enumerate()

def socket_handler( self, invar, indict ):
threadname = self.getName()
print "\"%s started\"" % threadname
print "client_socket_handler.socket_handler() invar: ", invar
instr = self.socket.recv( 500 )
# print instr
req_lines = string.split( instr, "\r" )
for line in req_lines:
line.strip( "\n")
print req_lines
print len( instr )

----------------------------------

self.threading.Thread.__init__()
self.Thread.__init__()
??
7 Answers

Aaron Brady

3/6/2008 3:39:00 AM

0

On Mar 5, 6:09 pm, sambo q <sa...@voidstar.com> wrote:
> I got myself in jam trying to be too fancy with  threading.Thread
> Docs say / remind to call the base __init__
> but I can't fighure out how.
>
> ---------------------------
> def main()
> .....
>     ls.listen(5)
>     key = ' '
> #    while key != EXITCHARCTER:
>     while stop_serving == False:
>         cs, raddr = ls.accept()
>         print "Main_Thread: ",cs, raddr
>         nt = client_socket_handler( cs, raddr )
>         print threading.enumerate()
>         key = getkey()
>
> #    ls.close()
>     time.sleep(4)
>     print "Server Exiting."
>
> class client_socket_handler(threading.Thread):
>     def __init__(self, cs, remote_address):
> ???????????????????????
>         self.threading.Thread.__init__(self,self.socket_handler,None,None)
>         self.socket = cs
>         self.rhost_addr = remote_address
>         print "client_socket_handler.__init__(): ", self.socket,
> self.rhost_addr
> #        t1 = threading.Thread( None,self.socket_handler, None, (5,78) )
> #        t1.start()
>         self.start()
>         print "client_socket_handler.__init__(): ", self.socket,
> self.rhost_addr
>         print "client_socket_handler.__init__(): enumerate()",
> threading.enumerate()
>
>     def socket_handler( self, invar, indict ):
>         threadname = self.getName()
>         print "\"%s started\"" % threadname
>         print "client_socket_handler.socket_handler() invar: ", invar
>         instr = self.socket.recv( 500 )
> #        print instr
>         req_lines = string.split( instr, "\r" )
>         for line in req_lines:
>             line.strip( "\n")
>         print req_lines
>         print len( instr )
>
> ----------------------------------
>
> self.threading.Thread.__init__()
> self.Thread.__init__()
> ??

recall a= A() --> a.b() --> A.b( a ). What is A? threading.Thread.

In other words, threading.Thread.__init__( *stuff ).

Aaron Brady

3/6/2008 3:43:00 AM

0

On Mar 5, 6:09 pm, sambo q <sa...@voidstar.com> wrote:
> I got myself in jam trying to be too fancy with  threading.Thread
> Docs say / remind to call the base __init__
> but I can't fighure out how.
>
> ---------------------------
> def main()
> .....
>     ls.listen(5)
>     key = ' '
> #    while key != EXITCHARCTER:
>     while stop_serving == False:
>         cs, raddr = ls.accept()
>         print "Main_Thread: ",cs, raddr
>         nt = client_socket_handler( cs, raddr )
>         print threading.enumerate()
>         key = getkey()
>
> #    ls.close()
>     time.sleep(4)
>     print "Server Exiting."
>
> class client_socket_handler(threading.Thread):
>     def __init__(self, cs, remote_address):
> ???????????????????????
>         self.threading.Thread.__init__(self,self.socket_handler,None,None)
>         self.socket = cs
>         self.rhost_addr = remote_address
>         print "client_socket_handler.__init__(): ", self.socket,
> self.rhost_addr
> #        t1 = threading.Thread( None,self.socket_handler, None, (5,78) )
> #        t1.start()
>         self.start()
>         print "client_socket_handler.__init__(): ", self.socket,
> self.rhost_addr
>         print "client_socket_handler.__init__(): enumerate()",
> threading.enumerate()
>
>     def socket_handler( self, invar, indict ):
>         threadname = self.getName()
>         print "\"%s started\"" % threadname
>         print "client_socket_handler.socket_handler() invar: ", invar
>         instr = self.socket.recv( 500 )
> #        print instr
>         req_lines = string.split( instr, "\r" )
>         for line in req_lines:
>             line.strip( "\n")
>         print req_lines
>         print len( instr )
>
> ----------------------------------
>
> self.threading.Thread.__init__()
> self.Thread.__init__()
> ??

recall a= A() --> a.b() --> A.b( a ). What is A? threading.Thread.

In other words, threading.Thread.__init__( *stuff ).

Dennis Lee Bieber

3/6/2008 5:06:00 AM

0

On Thu, 06 Mar 2008 00:09:44 GMT, sambo q <sambo@voidstar.com> declaimed
the following in comp.lang.python:

> I got myself in jam trying to be too fancy with threading.Thread
> Docs say / remind to call the base __init__
> but I can't fighure out how.
>
You have a number of problems in this attempt...

> class client_socket_handler(threading.Thread):
> def __init__(self, cs, remote_address):
> ???????????????????????
> self.threading.Thread.__init__(self,self.socket_handler,None,None)

This is asking for a threading attribute that is part of the
instance... you just need the direct module reference. Drop the leading
self.

I'm not sure why you need to subclass from threading.Thread if all
the thread control is being done internal to this class. Subclassing
means being able to do things like:

mySH = client_socket_handler(some, args)
....
mySh.start()

.... ie; instances of your class behave just like an extended instance of
the Thread class; but the user of this class never sees that it IS a
thread. I believe the other facet is that when subclassing from Thread,
one overloads the run() method AS the target instead of passing some
other function as a target argument and letting the parent run() method
call it.


So why not just make the thread an attribute of the class instead.


> self.socket = cs
> self.rhost_addr = remote_address
> print "client_socket_handler.__init__(): ", self.socket,
> self.rhost_addr
> # t1 = threading.Thread( None,self.socket_handler, None, (5,78) )
> # t1.start()

Which is about what you were doing here, but should have used
self.t1 =...
self.t1.start()

> self.start()
> print "client_socket_handler.__init__(): ", self.socket,
> self.rhost_addr
> print "client_socket_handler.__init__(): enumerate()",
> threading.enumerate()
>
> def socket_handler( self, invar, indict ):

You aren't passing any arguments to this method.

> threadname = self.getName()
> print "\"%s started\"" % threadname
> print "client_socket_handler.socket_handler() invar: ", invar
> instr = self.socket.recv( 500 )
> # print instr
> req_lines = string.split( instr, "\r" )
> for line in req_lines:
> line.strip( "\n")

What do you expect the behavior to be if a new-line is embedded and
not adjacent to a carriage return?

> print req_lines
> print len( instr )
>
> ----------------------------------
>
> self.threading.Thread.__init__()
> self.Thread.__init__()
> ??
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

sammy

3/7/2008 3:35:00 AM

0

Dennis Lee Bieber wrote:

>
> I'm not sure why you need to subclass from threading.Thread if all
> the thread control is being done internal to this class. Subclassing
> means being able to do things like:
>
Well it was the first thing that occured to me how to have
private data for each thread ( IP, PORT , CURDIR if I decide to serve
files. ) although I did come across some private_data CLASS in the docs.


> mySH = client_socket_handler(some, args)
> ...
> mySh.start()

hope I can call self.start() from __init__() or self.socket_handler()

>
> ... ie; instances of your class behave just like an extended instance of
> the Thread class; but the user of this class never sees that it IS a
> thread. I believe the other facet is that when subclassing from Thread,
> one overloads the run() method AS the target instead of passing some
> other function as a target argument and letting the parent run() method
> call it.
>
>
> So why not just make the thread an attribute of the class instead.
>
>
Maybe that would be clearer. Starting to wonder if I could return thread
name this way, then again, I think part reason for this aproach was the
fact that once started it could finish/crash all on it's lonesome and
it's instance could wait for garbage collection.

>> self.socket = cs
>> self.rhost_addr = remote_address
>> print "client_socket_handler.__init__(): ", self.socket,
>> self.rhost_addr
>> # t1 = threading.Thread( None,self.socket_handler, None, (5,78) )
>> # t1.start()
>
> Which is about what you were doing here, but should have used
> self.t1 =...
> self.t1.start()
>
Well this shouldn't realy be here just stuff I pasted over.
Which brings me to a question how to start my thread internaly.
Once I managed to call __init__() I should be able to call
self.start() nicht var? or is it vahr?

>> self.start()
>> print "client_socket_handler.__init__(): ", self.socket,
>> self.rhost_addr
>> print "client_socket_handler.__init__(): enumerate()",
>> threading.enumerate()
>>
>> def socket_handler( self, invar, indict ):
>
> You aren't passing any arguments to this method.
>
>> threadname = self.getName()
>> print "\"%s started\"" % threadname
>> print "client_socket_handler.socket_handler() invar: ", invar
>> instr = self.socket.recv( 500 )
>> # print instr
>> req_lines = string.split( instr, "\r" )
>> for line in req_lines:
>> line.strip( "\n")
>
> What do you expect the behavior to be if a new-line is embedded and
> not adjacent to a carriage return?
>
oops:
#handle macs
self.socket.send( "500 Inferior systems not permited.\r\n" )
self.socket.close()

Heh. Besides I don't think NL is permisible as line separator.
the command is at the beginnign and I don't expect to support
any multitude of tags.

Dennis Lee Bieber

3/7/2008 4:55:00 AM

0

On Thu, 06 Mar 2008 22:35:18 -0500, Sam <sambo@voidstar.com> declaimed
the following in comp.lang.python:

> >
> > So why not just make the thread an attribute of the class instead.
> >
> >
> Maybe that would be clearer. Starting to wonder if I could return thread
> name this way, then again, I think part reason for this aproach was the
> fact that once started it could finish/crash all on it's lonesome and
> it's instance could wait for garbage collection.
>
> >> self.socket = cs
> >> self.rhost_addr = remote_address
> >> print "client_socket_handler.__init__(): ", self.socket,
> >> self.rhost_addr
> >> # t1 = threading.Thread( None,self.socket_handler, None, (5,78) )
> >> # t1.start()
> >
> > Which is about what you were doing here, but should have used
> > self.t1 =...
> > self.t1.start()
> >
> Well this shouldn't realy be here just stuff I pasted over.
> Which brings me to a question how to start my thread internaly.
> Once I managed to call __init__() I should be able to call
> self.start() nicht var? or is it vahr?
>

If the end-user isn't supposed to even know there is a thread
running...

class whatever(object):
def __init__(self, *args): #fit whatever args you need
self._myThread = threading.Thread(target=self._something,
args=(what, ever))
self.otherStuff = ...
self._myThread.start()
def _something(w, e):
#the function that should run as the thread worker

.... the actual thread becomes just an attribute...

> Heh. Besides I don't think NL is permisible as line separator.
> the command is at the beginnign and I don't expect to support
> any multitude of tags.

I was thinking something that might have been embedded in the data
itself, once you get to that point.

Consider the results of:

>>> sample="""line 1\r\nline 2\r\nembedded\nline\r\nsomething\r\n"""
>>> sample.split()
['line', '1', 'line', '2', 'embedded', 'line', 'something']
>>> sample.split("\r\n")
['line 1', 'line 2', 'embedded\nline', 'something', '']
>>> sample.split("\r")
['line 1', '\nline 2', '\nembedded\nline', '\nsomething', '\n']
>>> sample.splitlines()
['line 1', 'line 2', 'embedded', 'line', 'something']
>>> sample="""line 1\rline 2\rembedded\nline\rsomething\r"""
>>> sample.splitlines()
['line 1', 'line 2', 'embedded', 'line', 'something']
>>>

Note that use "\r\n" as the split argument keeps embedded \n, but
doesn't need a subsequent strip to remove the \n left when using just
"\r" to split. [It does, however, give an empty element following the
concluding "\r\n" but so would "\r" followed by strip]

splitlines() handles \r\n, \n, and \r as line endings -- and no stray
empty ending! (however, \n\r /does/ create an empty element)



--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

sammy

3/12/2008 3:50:00 PM

0

Dennis Lee Bieber wrote:

> On Thu, 06 Mar 2008 22:35:18 -0500, Sam <sambo@voidstar.com> declaimed
> the following in comp.lang.python:

> ['line 1', 'line 2', 'embedded', 'line', 'something']
>>>> sample="""line 1\rline 2\rembedded\nline\rsomething\r"""
>>>> sample.splitlines()
> ['line 1', 'line 2', 'embedded', 'line', 'something']
>>>>
>
You are right partly right. That code only parsed the HTML
request header which I believe had one specific line termination
as prescribed by RFC ( but I will have too look it up later
since I don't remember whichfor sure. )

Once again got confused on them buggers, "\n" is LINUX
( the least logical).

> splitlines() handles \r\n, \n, and \r as line endings -- and no stray
> empty ending! (however, \n\r /does/ create an empty element)
>


!!~! SPLITLINES() BEAUTY, Thanks for reminding me. I mush have a faulty
memory cell or entire row.

Rick Rothstein \(MVP - VB\)

3/25/2010 1:13:00 AM

0

I assumed the multiple lines shown for each ID were in the same cell
separated by Line Feed characters (rather than each line being on its own
row).

--
Rick (MVP - Excel)



"ryguy7272" <ryguy7272@discussions.microsoft.com> wrote in message
news:0F2E474E-F104-4CCB-B453-FA06DD8EE59B@microsoft.com...
> Rick, your macro didn't work for me, so I suspect my data-setup is wrong.
> Fasty, maybe this is what you want:
>
> Sub copyit()
> Dim i As Long
> Dim LastRow As Long
>
> With Application
>
> .ScreenUpdating = False
> .Calculation = xlCalculationManual
> End With
>
> With ActiveSheet
>
> LastRow = .Cells(.Rows.count, "A").End(xlUp).Row
> For i = LastRow To 2 Step -1
>
> If .Cells(i, "A").Value = .Cells(i - 1, "A").Value Then
>
> .Cells(i, "C").Resize(, 100).Copy .Cells(i - 1, "D")
> .Rows(i).Delete
> End If
> Next i
>
> .Columns(2).Delete
> End With
>
> With Application
>
> .Calculation = xlCalculationAutomatic
> .ScreenUpdating = True
> End With
> End Sub
>
> --
> Ryan---
> If this information was helpful, please indicate this by clicking ''Yes''.
>
>
> "Rick Rothstein" wrote:
>
>> Give this macro a try...
>>
>> Sub SplitDataLines()
>> Dim X As Long, LastRow As Long, Data() As String
>> Const FirstRow As Long = 1, DataCol As String = "B"
>> LastRow = Cells(Rows.Count, DataCol).End(xlUp).Row
>> For X = LastRow To FirstRow Step -1
>> With Cells(X, DataCol)
>> Data = Split(.Value, vbLf)
>> If UBound(Data) Then
>> .Offset(1).Resize(UBound(Data)).EntireRow.Insert
>> .Resize(UBound(Data) + 1).Value =
>> WorksheetFunction.Transpose(Data)
>> .Offset(, -1).Resize(UBound(Data) + 1).Value =
>> .Offset(, -1).Value
>> End If
>> End With
>> Next
>> End Sub
>>
>> --
>> Rick (MVP - Excel)
>>
>>
>>
>> "fasty100" <fasty100@discussions.microsoft.com> wrote in message
>> news:ECAEEA83-86D6-41BA-B21C-7EC11AFEF86A@microsoft.com...
>> > Hi,
>> > I have the following problem :
>> > A=ID B
>> > 1 4 data
>> > more data
>> > even more data
>> > 2 5 data
>> > more data
>> > 3 6 data
>> > 4 7 data
>> > more data
>> > The first numbers are just the row numbering from Excel, the second
>> > number
>> > is an ID number I use , in colum B you will find the data about this ID
>> > number, but I want for data, more data and even more data that it is
>> > put
>> > in
>> > another row with the ID number in colum A, so i can create a tabel in
>> > acces
>> > with this in it, now it does not work because you have more objects in
>> > 1
>> > cel
>> > So this schould be the result :
>> > A=ID B
>> > 1 4 data
>> > 2 4 more data
>> > 3 4 even more data
>> > 4 5 data
>> > 5 5 more data
>> > 6 6 data
>> > 7 7 data
>> > 8 7 more data
>> >
>> > How can I do this, so I can create a table in Acces
>> > Thanks
>> >
>> >
>> >
>> .
>>