Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 11th, 2004, 04:36 PM
Yegg`'s Avatar
Yegg` Yegg` is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Dec 2004
Location: Meriden, Connecticut
Posts: 1,731 Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Weeks 6 Days 4 h 43 sec
Reputation Power: 68
Send a message via AIM to Yegg`
Look Over my Code

Here are a few things that I need to have looked over. I'm pretty sure that they're close to working only they may have 1 or 2 things wrong with them.
Original Code:
Code:
Public Function StrToHex(ByVal String1 As String) As String
On Error GoTo EndStr
  Dim strTemp As String, strReturn As String, i As Long
    For i = 1 To Len(String1)
      strTemp = Hex(Asc(Mid(String1, i, 1)))
      If Len(strTemp) = 1 Then strTemp = "0" & strTemp
      strReturn = strReturn & " " & strTemp
    Next i
      StrToHex = strReturn
EndStr:
 Exit Function
End Function

My code:
Code:
    def StrToHex():
        while i == 1:len(string1)
        strTemp = hex(ord(string1[i:1]))
        if len(strTemp) == 1:
            strTemp = "0"+strTemp
        strReturn = strReturn+" "+strTemp

Original Code:
Code:
Public Function KillNull(ByVal Text As String) As String
    Dim i As Integer
    i = InStr(1, Text, Chr(0))
    If i = 0 Then
        KillNull = Text
        Exit Function
    End If
    KillNull = Left(Text, i - 1)
End Function

My code:
Code:
    def KillNull(): # Pending approval 
        KillNull = KillNull()
        i = chr(0) in text
        if i == 0:
            killnull = text
            pass
        killnull = text[i:1]

Original code:
Code:
Public Sub NullTruncString(ByRef Text As String)
    Dim i As Integer
    
    i = InStr(Text, Chr(0))
    If i = 0 Then Exit Sub
    
    Text = Left(Text, i - 1)
End Sub

My code:
Code:
    def NullTruncString(): # Pending approval 
        i = chr(0) in text
        if i == 0:
            pass
        text = text[i:1]

Reply With Quote
  #2  
Old December 12th, 2004, 02:59 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,275 Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level)Schol-R-LEA User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 7 h 35 m 38 sec
Reputation Power: 616
One of the advantages of an interpreted language, especially one which has an interactive listener prompt, is that it is much easier to test an individual function or group of functions in isolation; you don't need to set up a whole test harness for it, you just load a copy of the function into the workspace and then write a few tests cases to see if it behave the way you expect it to.

That is to say, you'd have had faster results, with less sarcasm, if you'd just run the damn functions and saw that they do not, in fact, work.

just to take the first one, well, right of the bat I could see that the were some syntax errors, so it was clear you hadn't try testing them yourself. Also, the translation of the VB code was incorrect; you dropped the function argument and the return value, you wrote the while loop incorrectly (BTW, it would be easier to use for i in range(len(string1)):, it think) and made a hash of the indentation.

Anyway, I fired up DrPython (which I definitely recommend, though I hear Boa Constructor is even better), pasted your code into the editor window, fixed errors I saw, and worked up a simple test. Fixing the obvious problems sbrought out a less obvious one, that the range string1[i:1] should be string1[i:i + 1]; otherwise, it becomes an invalid range once i is greater than zero.
Code:
def StrToHex(string1):
	strReturn = ""
	for i in range(len(string1)):
		strTemp = hex(ord(string1[i:i + 1]))
		if len(strTemp) == 1:
			strTemp = "0" + strTemp
			strReturn = strReturn +" "+strTemp
	return strReturn
			
print "'Hello' becomes " + StrToHex("Hello")

(The last line is the test case, which will run automatically when the script is run in the interpreter).

Fixing that got the function to the point where the interpreter wouldn't spit it out; however, the test showed that the function was returning a blank string. As a quick sanity check, I dropped into the interactive window, and ran
Code:
print(hex(ord('h')))
This returned the string '0x68', which is correct, but clearly also not what the function in question was geared towards.

At this point I decided to leave off, as it was clear - as I had said to you already - that Python's idioms, conventions and libraries were too different from VB's to make a direct translation practical. The two hex() functions have the same purpose, but they are not interchangible. If you want to create a function closer to the one in VB, you might want to see how the Python entries for this 'hex dump chrestomathy' did it. HTH.

Reply With Quote
  #3  
Old December 12th, 2004, 02:50 PM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 9
Looks like I didn't get round to posting my reply before Firefox crashed..

*sigh*

Let me try again quickly...

Code:
1 def KillNull(): # Pending approval 
2         KillNull = KillNull()
3         i = chr(0) in text
4         if i == 0:
5             killnull = text
6             pass
7         killnull = text[i:1]


Line 1: no parameters, so where is the "text" coming from?
Line 2: Calling itself? That will just loop then crash!
Line 3: i is commonly a counter variable, so that's a bit confusing, but it will work
Line 4: The result of "x in y" is True or False, so test i == False isntead for clarity.
Line 5: right
Line 6: This does nothing. Pass is just a placeholder
Line 7: This ignores all text before the null, is that right?
Line 8: No return command. Now data can't get into the function, or out, so it's a touch pointless.

-----

Same points here for the last bit...

Code:
1 def NullTruncString(): # Pending approval 
2         i = chr(0) in text
3         if i == 0:
4             pass
5         text = text[i:1]


Line 1: Also no parameters
Line 2: Same about the True False thing
Line 3: This test does nothing. Pass doesn't exit the function, it just does ... nothing.
Line 5: Doesn't this do the same as KillNull?
Line 6: Also no return command...

Depending on what the code actually needs to do, you could probably replace it all with one of the following:

Either...

Remove a null at the start of a string:

Code:
def killNullAtOneEnd(text):
    if text.startswith(chr(0)):
        return text[1:]
    else:
        return text

(or use .endswith() and text[:-1] for the end of the string)

Or...

Dump both the functions completely and remove any null in the string:

Code:
text = text.replace(chr(0), "")


Or ...

Use string.split to get the text either side of the null:

Code:
beforeNull, afterNull = text.split(chr(0))



Last edited by sfb : December 12th, 2004 at 02:54 PM.

Reply With Quote
  #4  
Old December 12th, 2004, 03:30 PM
Yegg`'s Avatar
Yegg` Yegg` is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Dec 2004
Location: Meriden, Connecticut
Posts: 1,731 Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Weeks 6 Days 4 h 43 sec
Reputation Power: 68
Send a message via AIM to Yegg`
Ok, thanks for the help the both of you, I understand this much better now.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Look Over my Code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT