• Welcome to Valhalla Legends Archive.
 

Two Problems

Started by Yegg, January 10, 2005, 06:01 PM

Previous topic - Next topic

Yegg

I've been trying to convert 2 different VB6 functions into Python, here are the two functions, they are used for chat clients.
Public Sub ParseData(strData)
    'Parse a data string which has been received from Battle.net.
    'This should be called whenever a Winsock_DataArrival event
    'occurs, where strData is the received data string.
   
    Dim strStrings() As String, i
   
    'Sometimes Battle.net puts several strings into one line,
    'separated by CrLf's. Here we split and parse them.
   
    SplitStr strData, strStrings, vbCrLf
    For i = 1 To UBound(strStrings)
        Parse2 strStrings(i)
    Next i
End Sub


Private Sub Parse2(ByVal strData As String)
On Error Resume Next
    'This subroutine does the actual parsing. It gets called
    'by the ParseData subroutine. There is no need to call it
    'from outside.
   
    Dim strData1, strData2, i
    Dim strArgs() As String
   
    'Sometimes, Battle.net puts a LineFeed character at the
    'beginning of strings. We need to trim it.
    If Left(strData, 1) = Chr(10) Then
        strData = Right(strData, Len(strData) - 1)
    End If
   
    'Check whether this is normal text, or an event
    If Val(Left(strData, 4)) < 1001 Or Val(Left(strData, 4)) > 3000 Then
        Event_Unknown strData
        Exit Sub
    End If
   
    'If this is an event, then we separate the arguments, and quoted text
    i = InStr(1, strData, Chr(34), vbBinaryCompare)
    If i <> 0 Then
        strData1 = Left(strData, i - 2)
        strData2 = Mid(strData, i + 1, Len(strData) - i - 1)
    Else
        strData1 = strData
        strData2 = ""
    End If
    SplitStr strData1, strArgs(), " "
   
    'Call Appropriate Event based on ID
    Select Case Val(strArgs(1))
        Case 1001: Event_User strArgs(3), strArgs(4), strArgs(5)
        Case 1002: Event_Join strArgs(3), strArgs(4), strArgs(5)
        Case 1003: Event_Leave strArgs(3), strArgs(4)
        Case 1004: Event_RecvWhisper strArgs(3), strArgs(4), strData2
        Case 1005: Event_Talk strArgs(3), strArgs(4), strData2
        Case 1006: Event_Broadcast strData2
        Case 1007: Event_Channel strData2
        Case 1009: Event_Flags strArgs(3), strArgs(4)
        Case 1010: Event_SendWhisper strArgs(3), strArgs(4), strData2
        Case 1013: Event_ChannelFull strData2
        Case 1014: Event_ChannelNotExist strData2
        Case 1015: Event_ChannelRestricted strData2
        Case 1016: Event_Info strData2
        Case 1018: Event_Info strData2
        Case 1019: Event_Error strData2
        Case 1023: Event_Emote strArgs(3), strArgs(4), strData2
        Case 2010: Event_Name strArgs(3)
        Case 3000: Event_Info strData2
    End Select
End Sub


I took this code from VaporBot. My 2 problems are; in ParseData, where is strStrings coming from and what creates it? In Parse2 where is strArgs() coming from and what creates it?

Mr. Neo

Both of those arrays, strStrings and strArgs, are being created in each method.


Dim strStrings() As String, i

Dim strArgs() As String

Yegg

But when it comes to something like Python 2.3, where do they equal?

Mr. Neo


Banana fanna fo fanna


# this two liner is equivalent to ParseData
def parse_data(d):
   map(handle_event, d.split("\r\n"))
# here's a one-liner:
parse_data = lambda d: map(handle_event, d.split("\r\n"))

def event_user(args, string_arg):
   pass


### this stuff is equivalent to parse2

event_handlers = {
   1001: event_user,
   # ...
   }

def handle_event(d):
   try:
       if d.find('"') > -1:
           string_arg = d[d.find('"')+1:d.rfind('"')]
       else:
           string_arg = ""
       args = d.split(" ")
       event_handlers[int(args[0])](args, string_arg)
   except:
       event_unknown(d)

Yegg

Thanks a lot for the code Banana fanna fo fanna.

Yegg

I'm getting an error on the following line of code,
map(handle_event, d.split("\r\n"))
I'll try and fix this, but if you can, tell me what might be wrong. Maybe I have to import something as well?