Numeric type conversions

2008-06-17 Thread John Dann
I'm new to Python and can't readily find the appropriate function for
the following situation:

I'm reading in a byte stream from a serial port (which I've got
working OK with pyserial) and which contains numeric data in a packed
binary format. Much of the data content occurs as integers encoded as
2 consecutive bytes, ie a 2-byte integer.

I'm guess that there should be a function available whereby I can say
something like:

My2ByteInt = ConvertToInt(ByteStream[12:13])

to take a random example of the 2 bytes occurring at positions 12 and
13 in the byte stream.

Can anyone point me in the right direction towards a suitable function
please?

NB I don't know without further checking exactly how the bytes are
encoded, but I'm just transcribing some code across from a Windows
VB.Net program and these same bytes could be read straight into a
standard 2-byte signed short int, so I can be confident that it's a
fairly standard Windows-compatible encoding. 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Numeric type conversions

2008-06-17 Thread John Dann
On Tue, 17 Jun 2008 08:58:11 -0700 (PDT), MRAB
<[EMAIL PROTECTED]> wrote:

>[snip]
>Please note that in slicing the start position is included and the end
>position is excluded, so that should be ByteStream[12:14].

Yes, I just tripped over that, in fact, hence the error in my original
post. I suppose there must be some logic in including the start
position but excluding the end position, though it does escape me for
now. I can understand making a range inclusive or exclusive but not a
mixture of the two. Suppose it's just something you have to get used
to with Python and, no doubt, much commented on in the past.

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


Simple Python class questions

2008-06-19 Thread John Dann
A Python newbie, but some basic understanding of how classes, objects
etc work in eg VB.Net. However, I'm struggling a little to translate
this knowledge into the Python context.

I'm trying to teach myself this aspect of Python by working up a trial
project, part of which calls for pulling in data from a serial data
connection at regular intervals. It looked sensible to place all the
comms procedures/functions in their own class and module and make
calls to those functions from an object instantiated in a main
controlling module. But I'm struggling to get this working - not sure
whether it's a fundamental misunderstanding of the use of classes in
Python, syntax errors pure and simple or, most likely, a combination
of both!

Maybe I could provide some outline code as an illustration:

Let's say I define the class in a module called comms.py. The class
isn't really going to inherit from any other class (except presumably
in the most primitive base-class sense, which is presumably automatic
and implicit in using the class keyword). Let's call the class
serial_link. So in comms.py I have:

class serial_link:
def __init__(self):
Try
Import serial # the pyserial library
Except ImportException
#Error handling

def openPort(self):
Try
#Code to try opening serial port
Return "Success"
Except SerialException
Return "Failure"

Then in my separate main calling module I might have:

Import comms
serlink=comms.seral_link #Create instance of serial_link class
print serlink.openPort


The last line I'm hoping would print Success or Failure. But I just
seem to get some internal reference to the openPort function enclosed
in <>.

So why doesn't it work please? I may be making multiple errors here
but as far as I can see the syntax seems right. For this particular
example, I don't need to pass any arguments from the
'seriallink.openPort' function so haven't included any parentheses (or
are they mandatory even if empty?) I could go on with the explanations
etc, but it may be simplest to see if anyone can spot a howler
straight out.

TIA for anyone willing to help
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Python class questions

2008-06-19 Thread John Dann
Many thanks for the speedy replies.

On Thu, 19 Jun 2008 14:14:02 +0200, Cédric Lucantis <[EMAIL PROTECTED]>
wrote:

>Le Thursday 19 June 2008 13:54:03 John Dann, vous avez écrit :
>> Let's say I define the class in a module called comms.py. The class
>> isn't really going to inherit from any other class (except presumably
>> in the most primitive base-class sense, which is presumably automatic
>> and implicit in using the class keyword).
>
>No it's not :) It is recommended to always use new-style classes, and thus to 
>give the object base explicitely :
>
>class serial_link (object) :
>   ...

Can I just confirm: between the parentheses should be the literal
'object' - ie (object) - you're not just using 'object' as a
placeholder where there should be a more specific class name or
object?

--

On Thu, 19 Jun 2008 14:21:46 +0200, Ulrich Eckhardt
<[EMAIL PROTECTED]> wrote:

>Stop, this can't work. Other than VB, Python actually is case sensitive, so
>you must write 'try' and not 'Try' and also 'import' and not 'Import'.
>Further, many (all?) statements that cause an indention are usually
>terminated with a colon, so like with 'class ..:' and 'def ..:' you also
>must use 'try:' and not just 'try'. Fix all these and try again, I guess
>this will already help a lot.

Sorry - the original code was syntactically correct - I just re-keyed
it rather quickly for the original newsgroup post here, rather than
copy/paste a larger chunk. I'll try to be more careful with any future
posts.

>One more thing: you are abusing exceptions. Typically, in such a short
>program you only have one try-except pair in the main entry function and
>all other code only throws the exceptions. In particular the __init__
>function of a class should always signal errors using exceptions. However,
>this is not a strict yes/no question but rather a stylistic one.

Thanks - I need to think more clearly about the best way of doing
this.

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


Re: Simple Python class questions

2008-06-20 Thread John Dann
Many thanks for the further comments:

On Thu, 19 Jun 2008 21:24:31 -0400, Terry Reedy <[EMAIL PROTECTED]>
wrote:

>>  def __init__(self):
>>  Try
>>  Import serial # the pyserial library

>The import should be at module level.  You only want to do it once, not 
>for every link.  And if the import fails, you should find out right 
>away.  

Yes I was wondering about that, but I wasn't clear about when 'body'
code (ie not contained within a def block) in the module might run
under Python. So it seemed to be safer to place the import statement
inside the 'constructor' to get the earliest warning of non-visibility
of pyserial. But you seem to be implying that the body code will run
when the class is instantiated - have I understood that right? It
surely doesn't run when the module containing the class is imported
into the main module - does it?? It would certainly make life easier
to place the import in the body of the module.

I think that some of the other points hinge on the this one, so let me
get my understanding straight on that first!

>
>I guess you learned by now why cut/paste/edit-down is superior to 
>re-typing ;-)

Well I know what you mean, but actually in this instance my Python
environment is a non-networked laptop , so no easy way to cut and
paste to a networked PC! (Actually the laptop does have an Ethernet
chip in but bizarrely the driver somehow manages to kill my ADSL
connection at the exchange or ISP, which takes hours to reset so I
take care not to use this option. But learning Linux/Python is a
useful role for this otherwise defunct PC)

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


Passing arguments to subclasses

2008-06-23 Thread John Dann
May I ask a simple newbie question, which I presume is true, but for
which I can't readily find confirmation:

Let's say I have a parent class with an __init__ method explicitly
defined:

class ParentClass(object):
def __init__(self, keyword1, keyword2):
etc

and I subclass this:

class ChildClass(ParentClass):
# No __init__ method explicitly defined

Now I presume that I can instantiate a child object as:

child = ChildClass(arg1, arg2)

and arg1, arg2 will be passed through to the 'constructor' of the
antecedent ParentClass (there being no overrriding __init__ method
defined for ChildClass) and mapping to keyword1, keyword2 etc.

Have I understood this correctly?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Passing arguments to subclasses

2008-06-23 Thread John Dann
Thanks for the responses - they're much appreciated. And I understand
the slight impatience with questions that could possibly be answered
without recourse to a forum - I'm usually in the opposite position of
fielding many newbie questions in a forum in a completely different
field!

But don't be too harsh on this category of questions for a couple of
reasons. First, newbies don't necessarily yet have the same easy
familiarity with the Python interpreter that's implied. - personally
I'd be a little concerned as to whether any significant block of
test/example code that I'd entered into the interpreter was actually
addressing the exact question that I had in mind. 

And second, the answer might have been of the 'yes, but' kind. In
other words, it might perhaps have been true in a certain sort of
simple example, but one that failed to provide the complete picture.
So sometimes it's reassuring to be able to get an authoritative
answer.

Thanks again for taking the time to answer.
--
http://mail.python.org/mailman/listinfo/python-list


Komodo Edit newbie Q

2008-06-26 Thread John Dann
I'm learning Python using the Komodo Edit freeware code editor. One
thing I'm finding a little confusing is that the code completion lists
(what I would call Intellisense coming from a .Net background) are
often very incomplete, especially with imported classes like wx. It's
like KE can't look far enough into these classes to offer a
comprehensive list of method etc options.

I presume that I haven't possibly omitted some KE setup step that
would give more detail to the code completion?

If not then maybe this is just a limitation of the KE freeware
(because it's free?). Does anyone know if say Komodo IDE offers more
in the way of code completion (I know that it offers more things in
the way of debugging etc, but it's code completion specifically that
I'm asking about here). Or is one of the other Python IDEs maybe more
capable when it comes to code completion?
--
http://mail.python.org/mailman/listinfo/python-list


Scope and program structure problems

2008-07-01 Thread John Dann
Trying to learn Python here, but getting tangled up with variable
scope across functions, modules etc and associated problems. Can
anyone advise please?

Learning project is a GUI-based (wxPython) Python program that needs
to access external data across a serial port. 

The first thing that I need the program to do when it starts up is to
check that it can see the serial port, the first step of which is to
check that it can import the pySerial module, and display an
appropriate message in the statusbar of the main frame. (I know there
may be other ways of doing this but this is what I'm trying to do
currently and I'd like to understand how to make this approach work
robustly even if there are other options.)

So it seems that if I want to write to the frame's statusbar, I can't
do anything re serial import until the frame is initialising. And I
can't put any serial import code after the app.Mainloop() code line
because it won't run until the frame closes. 

In other words, I seem to be stuck with importing the serial library
either in the __init__ function or - if implemented slightly
differently - in a separate function of the wxPython Frame class, ie a
code structure of:

---
# Can't put serial import code here because there's no GUI yet to send
messages to.

Class Frame(wx.Frame)
def __init__()
etc
#code to check and use serial port has to go below??
def CheckSerial() # eg triggered by appropriate menu click event
Try:
import serial
etc
ser = Serial.etc  # set ser object as handle to serial port
def UseSerial():
# Code to perform serial IO

app=App(wx.App)
app.MainLoop()

# Can't put serial import code here because it won't execute until
frame closes?
---

But then I hit another problem. If I set the object ser to point to
the serial port in the CheckSerial function, then it's just got local
scope within the function and can't be seen from the UseSerial
function.

So I've got 2 questions. The main one is whether there's any way of
explicitly giving the ser object global scope in the code line:

ser = Serial.etc  # set ser object as handle to serial port

And, second, does my overall description above reveal any serious
misunderstanding about how best to handle this sort of problem in
Python?

Apologies for the long post and hope that my description is clear
enough to make sense.

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


Re: Scope and program structure problems

2008-07-01 Thread John Dann
Many thanks for the repsonse - much appreciated. 

And sorry - yes I was probably compounding two separate issues here -
the GUI one and the variable scope one. Maybe the wxPython list would
be the best place to ask more about the GUI side of things.

Then actually I can simplify my remaining question quite a lot - I
know you've partly answered this already but let me just rephrase
this:

It must be commonplace to first use a variable or object within one
function in a module and then want to use it again  - perhaps still
with the same value as in the first function - in a second function.

So what's the recommended Python way round this? If I was using .Net
then I'd be declaring the variables/objects explicitly and could set
their scope according to how/where they were declared. But if the
Python way is not to declare vars before use then this must create
some problems for cross-function use.

So it is best to declare such vars at the module level (ie outside of
a function) and set eg to Null/None or to assign them with a keyword
such as global or static (assuming that concept applies in Python) at
first use inside a function or what?

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


How to create a timer/scheduler in Python?

2008-07-12 Thread John Dann
I need what I'd call (in .Net) a timer, ie I need to run a function eg
every 2 seconds - it doesn't need to be millisec accurate but it would
be nice if it wasn't eg every 4 seconds or something.

Rather surprisingly, Core Python (Chun) doesn't seem to index 'timer'
or 'scheduler', which leaves me wondering whether this is an aspect of
Python that isn't perhaps widely used?

Looking around on the net I can see references to a thread timer, but
I'm not really looking to start any new threads (I just want part of
the GUI to update every 2 secs) and don't want to get into that sort
of complication while still just learning Python.

Is there really no simple timer/scheduler function available in
Python?
--
http://mail.python.org/mailman/listinfo/python-list