• Quick note - the problem with Youtube videos not embedding on the forum appears to have been fixed, thanks to ZiprHead. If you do still see problems let me know.

Anyone know Python?

Beerina

Sarcastic Conqueror of Notions
Joined
Mar 3, 2004
Messages
34,330
When I read some binary data with pySerial, I get an str that contains elements that, when printed, look like this:

'\xc0'


That itself is a str. How do I turn it back into a number so I can do math on it?

o int( '\xc0' ) chokes

o The binascii library is useless as it can't handle this in any of its format conversions.


As these are bytes, I could conceivably do 256-sized lookup table, but that's obviously stupid to do.



I am not super familiar with Python, yet, having only started a few days ago.

The result should be amenable to simple math, like adding or subtracting 3.
 
The ord() method returns the Unicode code for the character in a string of length 1. I think that should do the trick.

~~ Paul
 
Byte array objects, pickler, jeebus...



ord('\xc0') ==> 192


Thanks, Paul!
 
Last edited:
It all pales into insignificance against the backdrop of a whitespace language.
Code:
for n in range(2, 10):
   for x in range(2, n):
      if n % x == 0:
        print n, 'equals', x, '*', n/x
print tracing info
        break
   else:
      print n, 'is a prime number'
Screwed and screwed again.

~~ Paul
 
Ahh, another question, If I may?


Wut?

Code:
>>> class abc:
	def __init__(self):
		print "abc.__init__ called!"

>>> x = abc.abc()

Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    x = abc.abc()
AttributeError: class abc has no attribute 'abc'
>>> x = abc.__class()

Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    x = abc.__class__()
AttributeError: class abc has no attribute '__class__'
>>>


Ok, figured it out. The reason is Dive Into Python, the online tutorial, saves their class in a module named the same thing, but with lower case for module name.

This works:

Code:
>>> x = abc()
abc.__init__ called!
>>> x
<__main__.abc instance at 0x011C2D78>
>>>


Apparently the Dive Into Python example was thus doing the equivalent of x = ABC.abc(), where the first abc is the module name.
 
Last edited:
Damn, I thought the OP meant I could quote lines from the "Holy Grail" and such....


Wrong Python....:(
 
Code:
>>> for n in range(1,100):
	print "Spam"


Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
Spam
>>>
 
Last edited:
Ok, next question:

I am trying to use makepy.py to access a COM-interfaced library.

Several web sites say to do this to see what's available:

e.g. here:

Example using Microsoft Office 97.

Either:

Run 'win32com\client\makepy.py' (eg, run it from the command window, or double-click on it) and a list will be presented. Select the Type Library 'Microsoft Word 8.0 Object Library'

But I get this:

>>> import win32com.client
>>> win32com\client\makepy.py <---- Red rectangle starts after the y in .py
SyntaxError: unexpected character after line continuation character

>>>



makepy.py exists in the relevant win32com\client directory "C:\Python26\Lib\site-packages\win32com\client"



Any ideas? I get the same result typing the line in manually, in case copy-paste from the web site captured some bizarre character codes.


Note I am not looking for a general tutorial on COM and Python (that's what that page is) just wondering about what I could be doing wrong.
 
Last edited:
Well, you have to run it from the command prompt:

C:\Python26\Lib\site-packages\win32com\client\makepy.py
 
This simply isn't valid python:

>>> win32com\client\makepy.py

If you're trying to run that, you need to do it from a command prompt, not a Python interactive prompt.
 
It all pales into insignificance against the backdrop of a whitespace language.
Code:
for n in range(2, 10):
   for x in range(2, n):
      if n % x == 0:
        print n, 'equals', x, '*', n/x
print tracing info
        break
   else:
      print n, 'is a prime number'
Screwed and screwed again.

~~ Paul

Hey wow. There might be sane compsci types here. Down with significant whitespace. Down with that puny lambda.
 
I don't know the first thing about programming, but I'd like to put a plug in for my best friend, Mark Pilgrim, who wrote a book (several, actually) called Dive Into Python. He's quite a good writer. It's available in print or online. Google it, and him.
 

Back
Top Bottom