[Tutor] request for comments regarding my function

2009-10-24 Thread Isaac
Hello all,
I wrote a function to organize a list of sorted objects ( django blog
entries ); each object has a Python datetime attribute ( called
pub_date ). I am posting a message to the tutor mailing list to get
feedback regarding better ways to accomplish the same task. I have a
feeling I could use recursion here, instead of looping through the
entire _query list 3 times. Here is my code:

def create_archive_list(_query):
"""
given a sorted queryset, return a list in the format:

archive_list = [{'2009': [{'December': ['entry1', 'entry2']},
  {'January': ['entry3', 'entry4']}
  ]
 },

{'2008': [{'January': ['entry5', 'entry6']},
  ]
 }
]
"""

archive_list = []
tmp_year_list = []

# make a list of Python dictionaries; the keys are the year, as in
'2009', and the value
# is an empty list.
for item in _query:
if item.pub_date.year not in tmp_year_list:
tmp_year_list.append(item.pub_date.year)
tmp_dict = {}
tmp_dict[item.pub_date.year] = []
archive_list.append(tmp_dict)
else:
pass

# for every list in the archive_list dictionaries, append a
dictionary with the
# blog entry's month name as a key, and the value being an empty list
for entry in _query:
for item in archive_list:
_year = entry.pub_date.year
if _year in item:
_tmp_month = entry.pub_date.strftime("%B")
# make a dictionary with the month name as a key and
an empty list as a value
tmp_dict = {}
tmp_dict[_tmp_month] = []

if tmp_dict not in item[_year]:
item[_year].append(tmp_dict)

# append the blog entry object to the list if the pub_date month
and year match
# the dictionary keys in the archive_list dictionary/list tree.
for entry in _query:
# dict in list
for item in archive_list:
# year of entry
_year = entry.pub_date.year
if _year in item:
_year_list = item[_year]
for _dict_month in _year_list:
_tmp_month = entry.pub_date.strftime("%B")
if _tmp_month in _dict_month:
_dict_month[_tmp_month].append(entry)

return archive_list
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Moving a Shelf Between Linux and Mac OS X

2009-10-24 Thread Patrick Kenny
Greetings,

I recently started learning Python and I have written a script that uses
a shelf on Mac OS X using Python 2.6.  I recently attempted to move the
directory over to my Linux system, which also has Python 2.6 installed,
and run the script there.

The script works fine, but the shelf does not load.  Instead, it appears
that a new shelf is created.  On the Mac, the shelf file is saved as
class-shelve.db, but after running the script on Linux, a new file,
class-shelve (without a .db suffix) is created.

I tried simply deleting class-shelve and renaming class-shelve.db as
class-shelve, but when I run the script I get this error:

Traceback (most recent call last):
  File "card.py", line 232, in 
DisplayInventory(scope)
  File "card.py", line 65, in DisplayInventory
db = shelve.open('class-shelve')
  File "/usr/lib64/python2.6/shelve.py", line 234, in open
return DbfilenameShelf(filename, flag, protocol, writeback)
  File "/usr/lib64/python2.6/shelve.py", line 218, in __init__
Shelf.__init__(self, anydbm.open(filename, flag), protocol,
writeback)
  File "/usr/lib64/python2.6/anydbm.py", line 82, in open
mod = __import__(result)
ImportError: No module named bsddb185

Are shelves portable between different OSs?  I would like to make the
data created on the shelf on the Mac also accessible on Linux; is there
an easy way to do this?


Cheers,
Patrick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pack as HEX question

2009-10-24 Thread bob gailer

Dave Angel wrote:



for byte0, byte1 in itertools.izip(packed_data, 
itertools.cycle(packed_XorKey)):

   decrypt.append(chr(ord(byte0) ^ ord(byte1)))



And of course that leads to:

decrypt = [chr(ord(byte0) ^ ord(byte1)) for byte0, byte1 in 
itertools.izip(packed_data, itertools.cycle(packed_XorKey))]


--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pack as HEX question

2009-10-24 Thread ALAN GAULD
EncryptString="313B372C2E2C63362E2128"
>
>
>This is different to what I expected, it is a string of ascii bytes 
>representing hex values.
>Is that the real format of your data? If so we need to split it into pairs and 
>use 
>int() to convert it. Or is your real data a string of bytes that happen to 
>have 
>those 11 hex values?
>
>XorKey="41424344"
>key = struct.unpack("d", XorKey)
>
>You have here 8 bytes(chars) not 4 bytes. 
This is what I was talking about in my first 
message you need to be clear about exactly 
what form your data takes. Is it a string 
representing hex values or is it a sequence 
of bytes? For example the 4 hex bytes 
41,42,43,44 are represented by the 
string "ABCD" which is very different
to "41424344" which is a string of 8 bytes 
and we can see those values with:

for c in XorKey:
print hex(ord(c))

num_ints = len(EncryptString)/11I said divide the data by the length of an int. 
An int is usually 4 bytes 
(you can find that by using the calcsize() function in the struct module)
so you want to divide EncryptString by 4. But only if the data is not 
in this format. If it is in this string format you need to split the string 
into 2 
character pairs and feed them to int()

data = struct.unpack("%dd"% num_ints,EncryptString)
>
>> The above code generates an error the my string must be a string length of 
>> 16Always post full error messages do not summarize them. 
Its not clear exactl;y which string the error is complaining 
about nor why.
I suspect you have a value of 2 for num_ints so your unpack is looking 
for 8 bytes but you are giving it 22. So where the 16 is coming from 
I'm not sure.

> When you say I need to determine the number of ints in my data 
>> i.e. EncryptString that value would be 11 right?Your string is 22 characters 
>> (i.e. bytes) long. It represents 11 bytes in hex which is

3 ints plus 3 bytes left over.
You need to really stop and check what kind of data you are dealing with.
In this case its strings, but in your real world case it may be real bytes in 
which case your test code is invalid. Understanding the data is critical in 
this kind of scenario.


Alan G.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using IDLE v2.6

2009-10-24 Thread Robert Berman
Ken,

Two recommendations.

1)  Komodo   Edit 5. Free edition available from Activestate.
www.activestate,com

2)  Wing IDE 101 Free Edition from www.wingware.com
my favorite of the freely available IDE's.

Good luck,

Robert


On Sat, 2009-10-24 at 13:57 -0400, Ken G. wrote:
> I just reinstalled Python v2.6.2 due to a computer crash.  I reinstalled
> IDLE v2.6 but I was wondering if there is something better than IDLE. 
> 
> I am using Ubuntu 9.04 so they do have some limited choices available.
> 
> Thanks for your input.
> 
> Ken
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pack as HEX question

2009-10-24 Thread Tom Green
Sweet nice tip I love this list.  Thank you.

Mike

On Sat, Oct 24, 2009 at 7:40 PM, Dave Angel  wrote:

> Tom Green wrote:
>
>> Thanks for your reply Dave.  I am capturing the data off the network
>> (wireshark) and saving it in WinHex for testing.  I am actually building a
>> socket to decrypt the data, so prior to implementing my logic in the
>> socket,
>> I figured I would write the algorithm in a quick script.  Here is what I
>> have so far and it works.
>>
>> import struct,binascii
>> decrypt=[]
>> data_count=0
>> key_pos=0
>> #packed data consists of HEX values
>> packed_data = binascii.unhexlify('313B372C2E2C63362E2128')
>> #XorKey data consists of HEX values
>> packed_XorKey=binascii.unhexlify('41424344')
>> while data_count < len(packed_data):
>>if key_pos >len(packed_XorKey)-1:
>>key_pos=0
>>
>>
>> decrypt.append(chr(ord(packed_data[data_count])^ord(packed_XorKey[key_pos])))
>>key_pos+=1
>>data_count+=1
>> print "".join(decrypt)
>>
>> This decrypts to Python rock
>>
>> This logic seems to work, but I am certain there is a better way.
>>
>> Mike
>>
>>
>> On Sat, Oct 24, 2009 at 4:43 PM, Dave Angel  wrote:
>>
>>
>>
>>> Tom Green wrote:
>>>
>>>
>>>
 Alan,

 Thanks for your response and hopefully I can clear things up.  I
 apologize
 for not being more clear.

 I obtain the HEX encoded data from Winhex i.e. copy Hex values.  The HEX
 encode data is very large and I simply paste it into my Python script
 along
 with the XOR key.  The data is a string of bytes represented in HEX, as
 I
 showed.

 Here is the problem I ran into.

 Take the 4 byte XOR key.  If I convert them to int with Base 16 it takes
 the
 4 and converts it to 0x34 when I in turn I actually need 0x41.

 Thanks for your feedback.

 Mike

 On Sat, Oct 24, 2009 at 10:24 AM, Alan Gauld >>>

> wrote:
>
>
 



>  Encrypted string in hex
>
>
>
>
>> "313B372C2E2C63362E2128"
>>
>> Four byte key
>> XOR key "41424344"
>>
>>
>>
>>
>>
> 
>
>
>
 If by Winhex, you mean the hex editor, then you're undoubtedly going
 about


>>> it the long way.  There's no need to convert the file to printable hex,
>>> you
>>> should be able to work on it directly.  Just open the file, read it into
>>> a
>>> string (in python2.x), then loop through it, one byte at a time.  Store
>>> your
>>> key in a binary string as well.  Now just loop through the two in pairs
>>> (use
>>> zip, and cycle) doing a chr of xor of ords.
>>>
>>>
>>> And when you respond, give us your python version, and show us the code
>>> you've got (almost) working.
>>>
>>> DaveA
>>>
>>>
>>>
>>
>>
>>
> (You top-posted, so your message is out of order)
>
> Replace your while -- loop with the following simpler version.
>
>
> for byte0, byte1 in itertools.izip(packed_data,
> itertools.cycle(packed_XorKey)):
>   decrypt.append(chr(ord(byte0) ^ ord(byte1)))
>
> (You'll need an import itertools, at beginning, of course.)
>
> itertools.cycle() repeats the pattern of the keys.  itertools.izip()
> combines two iterables into one.  Then you just loop through that one in the
> usual way, where byte0 comes from the packed_data, and byte1 comes from the
> XorKey.
>
> DaveA
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pack as HEX question

2009-10-24 Thread Dave Angel

Tom Green wrote:

Thanks for your reply Dave.  I am capturing the data off the network
(wireshark) and saving it in WinHex for testing.  I am actually building a
socket to decrypt the data, so prior to implementing my logic in the socket,
I figured I would write the algorithm in a quick script.  Here is what I
have so far and it works.

import struct,binascii
decrypt=[]
data_count=0
key_pos=0
#packed data consists of HEX values
packed_data = binascii.unhexlify('313B372C2E2C63362E2128')
#XorKey data consists of HEX values
packed_XorKey=binascii.unhexlify('41424344')
while data_count < len(packed_data):
if key_pos >len(packed_XorKey)-1:
key_pos=0

decrypt.append(chr(ord(packed_data[data_count])^ord(packed_XorKey[key_pos])))
key_pos+=1
data_count+=1
print "".join(decrypt)

This decrypts to Python rock

This logic seems to work, but I am certain there is a better way.

Mike


On Sat, Oct 24, 2009 at 4:43 PM, Dave Angel  wrote:

  

Tom Green wrote:



Alan,

Thanks for your response and hopefully I can clear things up.  I apologize
for not being more clear.

I obtain the HEX encoded data from Winhex i.e. copy Hex values.  The HEX
encode data is very large and I simply paste it into my Python script
along
with the XOR key.  The data is a string of bytes represented in HEX, as I
showed.

Here is the problem I ran into.

Take the 4 byte XOR key.  If I convert them to int with Base 16 it takes
the
4 and converts it to 0x34 when I in turn I actually need 0x41.

Thanks for your feedback.

Mike

On Sat, Oct 24, 2009 at 10:24 AM, Alan Gauld   

wrote:




  

 Encrypted string in hex




"313B372C2E2C63362E2128"

Four byte key
XOR key "41424344"



  





If by Winhex, you mean the hex editor, then you're undoubtedly going about
  

it the long way.  There's no need to convert the file to printable hex, you
should be able to work on it directly.  Just open the file, read it into a
string (in python2.x), then loop through it, one byte at a time.  Store your
key in a binary string as well.  Now just loop through the two in pairs (use
zip, and cycle) doing a chr of xor of ords.


And when you respond, give us your python version, and show us the code
you've got (almost) working.

DaveA




  

(You top-posted, so your message is out of order)

Replace your while -- loop with the following simpler version.


for byte0, byte1 in itertools.izip(packed_data, 
itertools.cycle(packed_XorKey)):

   decrypt.append(chr(ord(byte0) ^ ord(byte1)))

(You'll need an import itertools, at beginning, of course.)

itertools.cycle() repeats the pattern of the keys.  itertools.izip() 
combines two iterables into one.  Then you just loop through that one in 
the usual way, where byte0 comes from the packed_data, and byte1 comes 
from the XorKey.


DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using IDLE v2.6

2009-10-24 Thread Kurt Bendl

"Ken G."  wrote

IDLE v2.6 but I was wondering if there is something better than IDLE.


I've been happy with WingIDE on Linux, fwiw. I'm still
a TextMate fanboy (if you use a Mac).

-kb

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pack as HEX question

2009-10-24 Thread Tom Green
Thanks for your reply Dave.  I am capturing the data off the network
(wireshark) and saving it in WinHex for testing.  I am actually building a
socket to decrypt the data, so prior to implementing my logic in the socket,
I figured I would write the algorithm in a quick script.  Here is what I
have so far and it works.

import struct,binascii
decrypt=[]
data_count=0
key_pos=0
#packed data consists of HEX values
packed_data = binascii.unhexlify('313B372C2E2C63362E2128')
#XorKey data consists of HEX values
packed_XorKey=binascii.unhexlify('41424344')
while data_count < len(packed_data):
if key_pos >len(packed_XorKey)-1:
key_pos=0

decrypt.append(chr(ord(packed_data[data_count])^ord(packed_XorKey[key_pos])))
key_pos+=1
data_count+=1
print "".join(decrypt)

This decrypts to Python rock

This logic seems to work, but I am certain there is a better way.

Mike


On Sat, Oct 24, 2009 at 4:43 PM, Dave Angel  wrote:

> Tom Green wrote:
>
>> Alan,
>>
>> Thanks for your response and hopefully I can clear things up.  I apologize
>> for not being more clear.
>>
>> I obtain the HEX encoded data from Winhex i.e. copy Hex values.  The HEX
>> encode data is very large and I simply paste it into my Python script
>> along
>> with the XOR key.  The data is a string of bytes represented in HEX, as I
>> showed.
>>
>> Here is the problem I ran into.
>>
>> Take the 4 byte XOR key.  If I convert them to int with Base 16 it takes
>> the
>> 4 and converts it to 0x34 when I in turn I actually need 0x41.
>>
>> Thanks for your feedback.
>>
>> Mike
>>
>> On Sat, Oct 24, 2009 at 10:24 AM, Alan Gauld > >wrote:
>>
>> 
>>
>>>
>>>  Encrypted string in hex
>>>
>>>
 "313B372C2E2C63362E2128"

 Four byte key
 XOR key "41424344"



>>> 
>>>
>> If by Winhex, you mean the hex editor, then you're undoubtedly going about
> it the long way.  There's no need to convert the file to printable hex, you
> should be able to work on it directly.  Just open the file, read it into a
> string (in python2.x), then loop through it, one byte at a time.  Store your
> key in a binary string as well.  Now just loop through the two in pairs (use
> zip, and cycle) doing a chr of xor of ords.
>
>
> And when you respond, give us your python version, and show us the code
> you've got (almost) working.
>
> DaveA
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pack as HEX question

2009-10-24 Thread Dave Angel

Tom Green wrote:

Alan,

Thanks for your response and hopefully I can clear things up.  I apologize
for not being more clear.

I obtain the HEX encoded data from Winhex i.e. copy Hex values.  The HEX
encode data is very large and I simply paste it into my Python script along
with the XOR key.  The data is a string of bytes represented in HEX, as I
showed.

Here is the problem I ran into.

Take the 4 byte XOR key.  If I convert them to int with Base 16 it takes the
4 and converts it to 0x34 when I in turn I actually need 0x41.

Thanks for your feedback.

Mike

On Sat, Oct 24, 2009 at 10:24 AM, Alan Gauld wrote:




 Encrypted string in hex


"313B372C2E2C63362E2128"

Four byte key
XOR key "41424344"

  


If by Winhex, you mean the hex editor, then you're undoubtedly going 
about it the long way.  There's no need to convert the file to printable 
hex, you should be able to work on it directly.  Just open the file, 
read it into a string (in python2.x), then loop through it, one byte at 
a time.  Store your key in a binary string as well.  Now just loop 
through the two in pairs (use zip, and cycle) doing a chr of xor of ords.



And when you respond, give us your python version, and show us the code 
you've got (almost) working.


DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ifdef in python

2009-10-24 Thread Albert-Jan Roskam
Isn't it better to use
if __debug__:

I thought such an if statement always evaluated to False when the python 
program was run in OPTIMIZED (-o) mode?

Cheers!!
Albert-Jan

~~
Before you criticize someone, walk a mile in their shoes, that way 
when you do criticize them, you're a mile away and you have their shoes! 
~~


--- On Wed, 10/21/09, Wayne  wrote:

> From: Wayne 
> Subject: Re: [Tutor] ifdef in python
> To: "Lie Ryan" 
> Cc: tutor@python.org
> Date: Wednesday, October 21, 2009, 11:37 PM
> 
> 
> On Wed, Oct 21, 2009 at 3:29 PM,
> Lie Ryan 
> wrote:
> 
> 
> Lizhi Yang wrote:
> 
> 
> Is there anything similar to ifdef statement in C or C++ in
> python?
> 
> 
> 
> 
> Not really, but python's regular if can be used for a
> similar purpose:
> 
> 
> 
> DEBUG = True
> 
> 
> 
> if DEBUG:
> 
>     import logging
> 
>     def foo():
> 
>         # debug version
> 
> else:
> 
>     def foo():
> 
>         # normal version
> 
> 
> 
> def main():
> 
>     foo()
> 
> 
> 
> if __name__ == '__main__':
> 
>     main()
> 
> 
> 
> or something like that.
> Really a try/except block would actually
> work
> def spam1():    pass
> try:   spam2except
> NameError:
> 
>    def spam2():      
> pass
> Because if DEBUG isn't declared you'll
> throw a NameError
> 
> -Inline Attachment Follows-
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 


  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pack as HEX question

2009-10-24 Thread Tom Green
This is what I have so far,

import struct

EncryptString="313B372C2E2C63362E2128"
XorKey="41424344"
key = struct.unpack("d", XorKey)
num_ints = len(EncryptString)/11
data = struct.unpack("%dd"% num_ints,EncryptString)

The above code generates an error the my string must be a string length of
16

When you say I need to determine the number of ints in my data i.e.
EncryptString that value would be 11 right?

I appreciate your help.

Mike.

On Sat, Oct 24, 2009 at 2:41 PM, ALAN GAULD wrote:

> > Take the 4 byte XOR key.  If I convert them to int with Base 16
> >  it takes the 4 and converts it to 0x34 when I in turn I actually need
> 0x41.
>
> OK, thats because int is for converting strings. You need to use
> struct.unpack
> to convert the 4 bytes into an int.
>
> You also need to figure out how many ints you have in your data and
> construct a format string with that many ints to struct unpack the data.
>
> Them apply the xor to each int using the key
>
> something like
>
> key = struct.unpack("d", keydata)
> num_ints = len(raw_data)/len(int)
> data = struct.unpack("%dd" % num_ints, raw_data)
>
> result = [n^key for n in data]
>
> (untested pseudo code!)
>
> HTH,
>
> Alan G.
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using IDLE v2.6

2009-10-24 Thread Alan Gauld
"Ken G."  wrote 

IDLE v2.6 but I was wondering if there is something better than IDLE. 


There is a wealth of options and a Google search on Python IDEs 
will throw many up.


Some common recommendations:

PyDev on Eclipse (good if you already use Eclipse for say Java or C++)
Eric
SPE
Scipe/Notepad++  (but these may be windows only?)
Komodo  (from activestate)

Of these I was most impressed with PyDev and SPE (which includes 
a GUI builder)


And there are several others.

Personally on Linux I, and several others, prefer to use the OS as 
our IDE and stick with a 3 window approach:

1) running vim/emacs as editor
2) a python interpreter running in an xterm for experimenting/testing
3) a blank xterm for running the saved script

There is also a python mode for emacs if you are a heavy user 
of emacs.


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pack as HEX question

2009-10-24 Thread ALAN GAULD
> Take the 4 byte XOR key.  If I convert them to int with Base 16
>  it takes the 4 and converts it to 0x34 when I in turn I actually need 0x41.  

OK, thats because int is for converting strings. You need to use struct.unpack
to convert the 4 bytes into an int.

You also need to figure out how many ints you have in your data and 
construct a format string with that many ints to struct unpack the data.

Them apply the xor to each int using the key

something like

key = struct.unpack("d", keydata)
num_ints = len(raw_data)/len(int)
data = struct.unpack("%dd" % num_ints, raw_data)

result = [n^key for n in data]

(untested pseudo code!)

HTH,

Alan G.___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using IDLE v2.6

2009-10-24 Thread Ken G.

I just reinstalled Python v2.6.2 due to a computer crash.  I reinstalled
IDLE v2.6 but I was wondering if there is something better than IDLE. 


I am using Ubuntu 9.04 so they do have some limited choices available.

Thanks for your input.

Ken
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pack as HEX question

2009-10-24 Thread Tom Green
Alan,

Thanks for your response and hopefully I can clear things up.  I apologize
for not being more clear.

I obtain the HEX encoded data from Winhex i.e. copy Hex values.  The HEX
encode data is very large and I simply paste it into my Python script along
with the XOR key.  The data is a string of bytes represented in HEX, as I
showed.

Here is the problem I ran into.

Take the 4 byte XOR key.  If I convert them to int with Base 16 it takes the
4 and converts it to 0x34 when I in turn I actually need 0x41.

Thanks for your feedback.

Mike

On Sat, Oct 24, 2009 at 10:24 AM, Alan Gauld wrote:

>
> "Tom Green"  wrote
>
>
>  wondering if there was someway in Python to indicate that the string is a
>> string of HEX values similar to Perl's pack.
>>
>
> You need to be very careful in your terminology here.
>
> Is it "a string of hex values" ie a string representation of a hex
> value or is it a string of bytes which can be represented in hex
> as you showed? The two and their processing are entirely different!
>
>
>  Encrypted string in hex
>> "313B372C2E2C63362E2128"
>>
>> Four byte key
>> XOR key "41424344"
>>
>
>
> Assuming the data really is binary data represented by the hex
> value you display above then you can use the struct module to
> unpack the values into integers directly.
>
> If it is a string of characters representing hex values then you can use
> the int() function to convert them by providing a base argument of 16..
>
>
>  I have done this numerous times, but I wanted to throw it out there as I
>> know there must be a easier way.  I am using Python 2.5.2
>>
>
> There is almost certainly an easier way but which way we
> that is cannot tell for sure from the information given.
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pack as HEX question

2009-10-24 Thread Alan Gauld


"Tom Green"  wrote


wondering if there was someway in Python to indicate that the string is a
string of HEX values similar to Perl's pack.


You need to be very careful in your terminology here.

Is it "a string of hex values" ie a string representation of a hex
value or is it a string of bytes which can be represented in hex
as you showed? The two and their processing are entirely different!


Encrypted string in hex
"313B372C2E2C63362E2128"

Four byte key
XOR key "41424344"



Assuming the data really is binary data represented by the hex
value you display above then you can use the struct module to
unpack the values into integers directly.

If it is a string of characters representing hex values then you can use
the int() function to convert them by providing a base argument of 16..


I have done this numerous times, but I wanted to throw it out there as I
know there must be a easier way.  I am using Python 2.5.2


There is almost certainly an easier way but which way we
that is cannot tell for sure from the information given.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Pack as HEX question

2009-10-24 Thread Tom Green
Hello everyone,

First, I wanted to thank everyone in advance for your help and any feedback
is appreciated.  Here is what I am trying to accomplish:

I have this encrypted data that was sent across the network.  The decryption
is a simple XOR with a 16 byte key.  I started writing a basic Python script
that would decrypt the traffic by XORing each byte with the XOR key.  I was
wondering if there was someway in Python to indicate that the string is a
string of HEX values similar to Perl's pack.

For example:

Encrypted string in hex
"313B372C2E2C63362E2128"

Four byte key
XOR key "41424344"

Is there any way to tell Python that the encrypted string and XOR key is
HEX? This way I can XOR each byte without having to either place a \x
between every two characters.  I realize I need to convert everything to an
integer first.

I have done this numerous times, but I wanted to throw it out there as I
know there must be a easier way.  I am using Python 2.5.2

Thank you,
Mike
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor