Re: [Tutor] Converting MAC address . Need Help

2006-02-27 Thread Travis Spencer
On 2/27/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
> This is not what Sudarshana asked for - '\\x00' and '\x00' are not the
> same string

Thanks for clearning that up for me, Kent.

--

Regards,

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


Re: [Tutor] Converting MAC address . Need Help

2006-02-27 Thread Travis Spencer
On 2/27/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Sudarshana KS wrote:
> > The problem i am facing is the mac address i have is in the form
> > 00:11:22:33:44:55 need to convert to '\x00\x11\x22\x33\x44\x55'

Perhaps I'm mistaking but it seems that you need to prepend the `\x'
escape sequence to the mac address you already have and replace the
colons with the same sequence.  If it is any harder than that, I'm
missing something.  If I got what you mean, Sudarshana, you can
achieve your goal with this one-liner:

>>> macAddress = '00:11:22:33:44:55'
>>> reduce(lambda a, b: a + r"\x" + b, macAddress.split(':'), "")
'\\x00\\x11\\x22\\x33\\x44\\x55'

Perhaps you may prefer this:

>>> "\\x" + macAddress.replace(":", "\\x")
'\\x00\\x11\\x22\\x33\\x44\\x55'

> The list elements are still strings, you need to convert them to
> integers.

Why is this needed, Kent?  Why not just leave them as strings?

> The strings are in hexadecimal so use the optional base
> parameter to int():
>   >>> int('11', 16)
> 17
>
> Finally you have to convert this number to a byte of a string using chr():
>   >>> chr(17)
> '\x11'

Seems like a lot of extra work to me.

--

Regards,

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


Re: [Tutor] Parsing os.popen(command) output

2005-09-14 Thread Travis Spencer
On 9/11/05, Bill Burns <[EMAIL PROTECTED]> wrote:
> Like I said, it does work but can it be improved upon?

The variable names could stand some improvement.  p, r, s, tmp, i,
etc. aren't very explanatory.  If you choose better names, your
coworkers, not to mention your future self, will be very thankful.

-- 

Regards,

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


Re: [Tutor] convert binary to ascii

2005-09-02 Thread Travis Spencer
On 9/2/05, Danny Yoo <[EMAIL PROTECTED]> wrote:

Hey Danny,

> If we have strings of ones and zeros, we can turn those into numbers by
> using the int() function.  For example:
> 
> ##
> >>> int("101", 2)
> 5
> >>> int("110", 2)
> 6
> >>> int("111", 2)
> 7

Oh, that's nice.  I guess my solution was the total C hacker's way;
not very pythonic :-)  Thanks for the info.

-- 

Regards,

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


Re: [Tutor] convert binary to ascii

2005-09-02 Thread Travis Spencer
On 9/2/05, Servando Garcia <[EMAIL PROTECTED]> wrote:
> Hello

Hey Servando,

> Here is my question : sam in binary is  01110011  0111  01101101.
>if given 01110011  0111  01101101  how would I go back to ascii.
>   I guess am asking is there a module that will do this conversion for
> me. 

I don't know about module, but it isn't hard to do it yourself.  Here
is one way:

#!/bin/env python

import sys

for line in sys.stdin:
result = ""

for binaryStr in line.split():
j = len(binaryStr) - 1
i = charCodeOfBinaryStr = 0

while j > 0:
charCodeOfBinaryStr += int(binaryStr[j]) * 2 ** i
j -= 1
i += 1

result += chr(charCodeOfBinaryStr)

print result

Here is the output of your binary string:

$ python btoa.py <<< "01110011  0111  01101101"
sam

Its a really common algorithm.  Google for "change of base algorithm"
and you'll probably find lots of explinations.

HTH.

-- 

Regards,

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


Re: [Tutor] Directory permission and ACLs

2005-09-02 Thread Travis Spencer
On 9/2/05, Pat Martin <[EMAIL PROTECTED]> wrote:
> I am new to python and wanted to write a program that looks at directory
> permissions and ext3 ACLs and also change them if needed. What modules
> would I be looking at for those functions?

os and os.path:

* http://docs.python.org/lib/module-os.html
* http://docs.python.org/lib/module-os.path.html

stat and shutil might be useful too.

HTH.

-- 

Regards,

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


Re: [Tutor] Writing to XML file with minidom

2005-08-31 Thread Travis Spencer
On 8/31/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
> Hi Travis,

Hey Danny,

> Putting pure binary bytes in an XML file has a flaw: the issue is that the
> binary bytes themselves might contain characters that could be interpreted
> as XML!  Even if we wrap the content in CDATA, there's nothing that really
> stops the bytes from containing the characters "]]>" to prematurely close
> off the CDATA tag.

Oh, sure.  I didn't think that through, and if I had, I wouldn't have
know how to work around it.

> To get around this, we can use a technique called "ascii-armor" to wrap
> protection around the troublesome binary text.
> 
> http://en.wikipedia.org/wiki/ASCII_armor

Brilliant.  I won't forget the term "ascii-armor" if I ever find
myself in Johan's shoes and I've forgotten the details.

> (Hey, look, an Elf!  *grin*)

HA!

> Hope this helps!

Tremendously.  Thanks, Danny!

-- 

Regards,

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


Re: [Tutor] Writing to XML file with minidom

2005-08-31 Thread Travis Spencer
On 8/30/05, Johan Geldenhuys <[EMAIL PROTECTED]> wrote:
> One snag that I found is that the des encryption that I used for the data 
> that is 
> written back, it is not parsed correctly when the file is read again with the 
> new 
> data in it. There is non-printable characters or non-ascii chars in that 
> gives errors
> from expat when the contents is parsed.
> I had to use a different encryption algorithm. I am going to do some tests on 
> it 
> now.

Put the cyphertext in a CDATA section, so the parser knows to ignore
its contents:







-- 

Regards,

Travis Spencer 

P.S. Please don't send HTML e-mails.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor