unpack

ha bo hasna1980_14 at hotmail.com
Fri Apr 11 16:07:09 CEST 2008


thank you i did find solution i did have just change: 

     unpackedData = struct.unpack(unpackFormat, data)  to

     unpackedData = struct.unpack(unpackFormat, data.decode('string_escape'))




From: python-list-request at python.org
Subject: Python-list Digest, Vol 55, Issue 179
To: python-list at python.org
Date: Fri, 11 Apr 2008 15:50:04 +0200

Send Python-list mailing list submissions to
	python-list at python.org
 
To subscribe or unsubscribe via the World Wide Web, visit
	http://mail.python.org/mailman/listinfo/python-list
or, via email, send a message with subject or body 'help' to
	python-list-request at python.org
 
You can reach the person managing the list at
	python-list-owner at python.org
 
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Python-list digest..."
--Pièce jointe du message transmise--
From: ivan.illarionov at gmail.com
To: python-list at python.org
Date: Fri, 11 Apr 2008 05:58:46 -0700
Subject: Re: Rounding a number to nearest even

On Apr 11, 2:14 pm, bdsatish <bdsat... at gmail.com> wrote:
> The built-in function round( ) will always "round up", that is 1.5 is
> rounded to 2.0 and 2.5 is rounded to 3.0.
>
> If I want to round to the nearest even, that is
>
> my_round(1.5) = 2        # As expected
> my_round(2.5) = 2        # Not 3, which is an odd num
>
> I'm interested in rounding numbers of the form "x.5" depending upon
> whether x is odd or even. Any idea about how to implement it ?
 
def even_round(x):
    if x % 1 == .5 and not (int(x) % 2):
        return float(int(x))
    else:
        return round(x)
 
nums = [ 3.2, 3.6, 3.5, 2.5, -.5, -1.5, -1.3, -1.8, -2.5 ]
for num in nums:
    print num, '->', even_round(num)
 
3.2 -> 3.0
3.6 -> 4.0
3.5 -> 4.0
2.5 -> 2.0
-0.5 -> 0.0
-1.5 -> -2.0
-1.3 -> -1.0
-1.8 -> -2.0
-2.5 -> -2.0
 
 
--Pièce jointe du message transmise--
From: steve at holdenweb.com
To: python-list at python.org
Date: Fri, 11 Apr 2008 09:07:42 -0400
Subject: Re: (unknown)

ha bo wrote:
> hi i use this programme in my application django:
> import struct
> MASK_CCITT  = 0x1021     # CRC-CCITT mask (ISO 3309, used in X25, HDLC)
> MASK_CRC16  = 0xA001     # CRC16 mask (used in ARC files)
> 
> def updcrc(crc, data, mask=MASK_CRC16):
> 
>      data_length = len(data)
>      unpackFormat = '%db' % data_length
>      unpackedData = struct.unpack(unpackFormat, data)
> 
>      for char in data:
>             c = ord(char)
>             c = c << 8
>    
>             for j in xrange(8):
>                 if (crc ^ c) & 0x8000:
>                     crc = (crc << 1) ^ mask
>                 else:
>                     crc = crc << 1
>                 c = c << 1
> 
>      return crc & 0xffff
> 
> 
> and i call this function in other function in my view:
> 
> 
> def encodekey(var, expires=None, user='', trusted=False):
>  
>         import random, base64
>         import updcrc
>         key = "%02X" % len(var) + var
>         key += "%02X" % len(user) + user
>         if expires is not None:
>             key += expires.strftime('%Y%m%d%H%M')
>         else:
>             year = random.choice(range(2000,2100))
>             month = 23
>             day = random.choice(range(1,32))
>             hour = random.choice(range(1,25))
>             minute = random.choice(range(1,60))
>             key += "%04d%02d%02d%02d%02d" % (year, month, day, hour, minute)
> 
>         if trusted:
>             checksum = updcrc(42, key)
>         else:   
>             checksum = updcrc(0, key)
>         key += "%04X" % checksum
> 
>         return base64.b64encode(key)
> but it give me this error:
> 
> 
>     unpack requires a string argument of length 31
> 
> 
> someone can help me
> 
Next time you report an error, please include the whole traceback, not 
just the exception message. That information is included for a reason.
 
You might also want to print out the value of data in your updcrc 
function, since that is where the problem is occurring.
 
Finally I might point out there is little point to the struct.unpack 
since you don't use its result, but I am guessing this is because your 
algorithm is currently in transition.
 
regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/
 
 
--Pièce jointe du message transmise--
From: victorsubervi at gmail.com
CC: python-list at python.org
To: gagsl-py2 at yahoo.com.ar
Date: Fri, 11 Apr 2008 08:36:02 -0500
Subject: Re: String Literal to Blob

Nope. Do not see it. My ugly stupid way works. I guess I will just proceed with that and write my howto accordingly.
Victor


On Thu, Apr 10, 2008 at 9:01 PM, Gabriel Genellina <gagsl-py2 at yahoo.com.ar> wrote:

En Thu, 10 Apr 2008 14:04:43 -0300, Victor Subervi
<victorsubervi at gmail.com> escribió:





> Well, what I did was this:
>
>             content = col_fields[0][14].tostring()
>             pic = "tmp" + str(i) + ".jpg"
>             img = open(pic, "w")

>             img.write(content)
>             print '<img src="%s"><br /><br />' % pic
>             img.close()
> where I am incrementing i. Ugly. Stupid. But if it is the only way to do

> it
> in python, and I do not want to invest the time doing it in php, which I
> think would be prettier in this instance, then I guess it will do. Your
> thoughts appreciated.

You REALLY should read some material on how HTTP works. I'll try to sketch

a few important points. First, suppose you have an HTML page (album.html)
with two images in it:

<html><body>
<p>This is me: <img src="/images/myself.jpg">
and this is my cat <img src="/images/garfield.jpg">

</body></html>

Suppose the URL for that page is http://some.server.com/gabriel/album.html
and you type that in your favorite browser. This is what happens:

1) The sees the initial "http:" and says "I'll use HTTP". Then sees
"some.server.com" and opens a connection to that server on port 80. Then

sees "/gabriel.album.html" and builds an HTTP GET request for it.
2) The server receives the GET request, looks for the "album.html"
document, determines the right Content-Type, and returns it specifying

"Content-Type: text/html"
3) The browser receives the HTML text and tries to display it. When it
encounters the first <img> tag it looks at the src attribute; it doesn't
know that image; so a *NEW* HTTP request is required. This time it says

"GET /images/myself.jpg"
4) The server receives the GET request, looks for a file with that name,
determines that it's a jpeg image, and returns its contents along with a
"Content-Type: image/jpeg".

5) The browser receives the image and is able to display it.
6) The same thing happens with the second <img> tag, there is a third HTTP
GET request for it.

Note that:
- The images themselves *aren't* in the HTML page, they are somewhere

else. HTML is text and contains ONLY the URI for the image.
- THREE DIFFERENT requests are done to show that page. Each one returns A
SINGLE OBJECT of A SINGLE TYPE.

The above was using static HTML with static images. If you use CGI to

generate dynamic content, it's the same thing. From the browser point of
view, there is no difference: it still will generate three different
requests for the three pieces (one html document with two images).

Your CGI script (or scripts) will receive three different requests then:
when requested for HTML, return HTML; when requested for an image, return
an image. They are DIFFERENT things, DIFFERENT requests, happening at

DIFFERENT times, so don't mix them.

I think that combining Steve's responses and mine you now have enough
information to be able to solve your problems. Perhaps if you re-read the
whole thread from start you'll have now a better understanding of what's

happening.

--
Gabriel Genellina




--
http://mail.python.org/mailman/listinfo/python-list


--Pièce jointe du message transmise--
From: huayang.xia at gmail.com
To: python-list at python.org
Date: Fri, 11 Apr 2008 06:37:23 -0700
Subject: Re: Convert PyIDispatch object to struct IDispatch*

On Apr 11, 12:15 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Thu, 10 Apr 2008 18:45:04 -0300, Huayang Xia <huayang.... at gmail.com>
> escribió:
>
> > I am trying to use ctypes to call dll functions. One of the functions
> > requires argument "struct IDispatch* ". I do have a PyIDispatch object
> > in python. How can I convert this "PyIDispatch object" to "struct
> > IDispatch* "?
>
> I think a PyIDispatch object is an IDispatch* itself.
> But you'll get better answers from the python-win32 list:http://mail.python.org/mailman/listinfo/python-win32
>
> --
> Gabriel Genellina
 
Thanks for the info.
 
To call a dll function, it needs a C style IDispatch*. PyIDispatch is
a python wrapped one. I found a reference from:
 
http://svn.python.org/projects/ctypes/trunk/comtypes/comtypes/test/test_win32com_interop.py
 
which shows how to convert C style to python style. Unfortunately i
need the reversed version.
 
I will post the question to python-win32.
 
--Pièce jointe du message transmise--
From: enleverlesX.XmcX at XmclaveauX.com
To: python-list at python.org
Date: Fri, 11 Apr 2008 15:38:32 +0200
Subject: Python plus

After IronPython, Python + iron : 
 http://www.golfermania.com/SnakeEyes/PYTHON-PLUS-IRON.jpg
 
;o)  
 
Michel Claveau
 
 
--Pièce jointe du message transmise--
From: mail at timgolden.me.uk
CC: python-list at python.org
Date: Fri, 11 Apr 2008 14:47:20 +0100
Subject: Re: Convert PyIDispatch object to struct IDispatch*

Huayang Xia wrote:
> On Apr 11, 12:15 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> wrote:
>> En Thu, 10 Apr 2008 18:45:04 -0300, Huayang Xia <huayang.... at gmail.com>
>> escribió:
>>
>>> I am trying to use ctypes to call dll functions. One of the functions
>>> requires argument "struct IDispatch* ". I do have a PyIDispatch object
>>> in python. How can I convert this "PyIDispatch object" to "struct
>>> IDispatch* "?
>> I think a PyIDispatch object is an IDispatch* itself.
>> But you'll get better answers from the python-win32 list:http://mail.python.org/mailman/listinfo/python-win32
>>
>> --
>> Gabriel Genellina
> 
> Thanks for the info.
> 
> To call a dll function, it needs a C style IDispatch*. PyIDispatch is
> a python wrapped one. I found a reference from:
> 
> http://svn.python.org/projects/ctypes/trunk/comtypes/comtypes/test/test_win32com_interop.py
> 
> which shows how to convert C style to python style. Unfortunately i
> need the reversed version.
> 
> I will post the question to python-win32.
 
I've had a quick look at the PyIDispatch source and I can't see any obvious
way in which the underlying IDispatch is exposed. May have missed something,
but it's possible that there's not way out.
 
TJG
 
--Pièce jointe du message transmise--
From: hdante at gmail.com
To: python-list at python.org
Date: Fri, 11 Apr 2008 06:49:23 -0700
Subject: Re: Rounding a number to nearest even

On Apr 11, 9:45 am, bdsatish <bdsat... at gmail.com> wrote:
> On Apr 11, 5:33 pm, bdsatish <bdsat... at gmail.com> wrote:
>
>
>
> > HI Gerard,
>
> > I think you've taken it to the best possible implementation. Thanks !
> > On Apr 11, 5:14 pm, Gerard Flanagan <grflana... at gmail.com> wrote:
>
> > > In fact you can avoid the call to the builtin round:
>
> > > ------------------------------------------------
>
> > > assert myround(3.2) == 3
> > > assert myround(3.6) == 4
> > > assert myround(3.5) == 4
> > > assert myround(2.5) == 2
> > > assert myround(-0.5) == 0.0
> > > assert myround(-1.5) == -2.0
> > > assert myround(-1.3) == -1.0
> > > assert myround(-1.8) == -2
> > > assert myround(-2.5) ==  -2.0
> > > ------------------------------------------------
>
> OK, I was  too early to praise Gerard. The following version:
>
> def myround(x):
>      n = int(x)
>      if abs(x - n) >= 0.5 and n % 2:
>           return n + 1 - 2 * int(n<0)
>      else:
>           return n
>
> of Gerard doesn't work for 0.6 (or 0.7, etc.) It gives the answer 0
> but I would expect 1.0 ( because 0.6 doesn't end in 0.5 at all... so
> usual rules of round( ) apply)
 
 Interestingly, you could solve this by using python 3. :-)
 round(x[, n])
    Return the floating point value x rounded to n digits after the
decimal point. If n is omitted, it defaults to zero. Values are
rounded to the closest multiple of 10 to the power minus n; if two
multiples are equally close, rounding is done toward the even choice
(so, for example, both round(0.5) and round(-0.5) are 0, and
round(1.5) is 2). Delegates to x.__round__(n).
 
 My turn: ;-)
 
def yaround(x):
    i = int(x)
    f = x - i
    if f != 0.5 and f != -0.5: return round(x)
    return 2.0*round(x/2.0)
 
a = (-10.0, -9.0, -8.0, -4.6, -4.5, -4.4, -4.0, -3.6, -3.5,
     -3.4, -0.6, -0.5, -0.4, 0.0, 0.4, 0.5, 0.6, 0.9, 1.0,
     1.4, 1.5, 1.6, 2.0, 2.4, 2.5, 2.6, 10.0, 100.0)
 
b = (-10.0, -9.0, -8.0, -5.0, -4.0, -4.0, -4.0, -4.0, -4.0,
     -3.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
     1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0, 10.0, 100.0)
 
for i in range(len(a)):
    assert yaround(a[i]) == b[i]
 
 
 
 

_________________________________________________________________
Lancez des recherches en toute sécurité depuis n'importe quelle page Web. Téléchargez GRATUITEMENT Windows Live Toolbar aujourd'hui !
http://toolbar.live.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080411/4d23f561/attachment.html>


More information about the Python-list mailing list