How to read from serial port?

2016-04-26 Thread David Aldrich
Hi

I have written a very simple program to read and print data from the serial 
port using pyserial:

#!/usr/bin/python3
import serial

ser=serial.Serial('COM1',115200)
while True:
out = ser.read()
print('Receiving...'+out)

When I run it and send data for it to read I get:

C:\SVNProj\Raggio\trunk\hostconsole\gui\prototypes\serial_test>py serial_read.py
Traceback (most recent call last):
  File "serial_read.py", line 9, in 
print('Receiving...'+out)
TypeError: Can't convert 'bytes' object to str implicitly

I am using Python 3.5.  How would I fix this error please?

Best regards

David

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


RE: Problem working with subprocess.check_call

2015-10-29 Thread David Aldrich
> Try this

> subprocess.check_call(["bash", "-O", "extglob", "-c", cmd])

That worked. Thanks very much!

David
-- 
https://mail.python.org/mailman/listinfo/python-list


Problem working with subprocess.check_call

2015-10-29 Thread David Aldrich
Hi

I am working on Linux with Python 3.4.

I want to do a bash diff on two text files and show just the first 20 lines of 
diff's output.  So I tried:

>>> cmd = 'head -20 <(diff ' + file1 + ' ' + file2 + ')'
>>> subprocess.check_call(cmd, shell=True)

The command contained in cmd works ok from the bash prompt but not from Python 
code.  In Python I get:

/bin/sh: -c: line 0: syntax error near unexpected token `('

I think the problem is that check_call is not using the bash shell.  So I also 
tried:

>>> subprocess.check_call("bash", "-O", "extglob", "-c", cmd)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python3.4/subprocess.py", line 556, in check_call
retcode = call(*popenargs, **kwargs)
  File "/usr/local/lib/python3.4/subprocess.py", line 537, in call
with Popen(*popenargs, **kwargs) as p:
  File "/usr/local/lib/python3.4/subprocess.py", line 767, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

Can anyone help me with this please?

Best regards

David

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


RE: Problem working with subprocess.check_call

2015-10-29 Thread David Aldrich
Thanks for all your answers.

David
-- 
https://mail.python.org/mailman/listinfo/python-list


Automating Sphinx generated documentation

2015-09-17 Thread David Aldrich
Hi

I have setup Sphinx for my Python project. We keep all our code and 
documentation in Subversion. So, following changes to the Python code, I need 
to regenerate and commit the Sphinx generated documentation.

I just wondered how people manage this.  I'm thinking of using Jenkins (a 
continuous integration tool) to check for changes, regenerate the docs and 
check them in.

Any suggestions please?

Best regards

David

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


RE: kivy editable multicolumn list

2015-09-15 Thread David Aldrich
> Not sure if this is the place to ask about kivy ...

Try the kivy users list here:

https://groups.google.com/forum/#!forum/kivy-users

Best regards

David
-- 
https://mail.python.org/mailman/listinfo/python-list


A basic dictionary question

2015-06-11 Thread David Aldrich
Hi

I am fairly new to Python.  I am writing some code that uses a dictionary to 
store definitions of hardware registers.  Here is a small part of it:

import sys

register = {
'address'  : 0x3001c,
'fields' : {
'FieldA' : {
'range' : (31,20),
},
'FieldB' : {
'range' : (19,16),
},
},
'width' : 32
};

def main():
fields = register['fields']
for field, range_dir in fields: == This line fails
range_dir = field['range']
x,y = range_dir['range']
print(x, y)

if __name__ == '__main__':
main()

I want the code to print the range of bits of each field defined in the 
dictionary.

The output is:

Traceback (most recent call last):
  File testdir.py, line 32, in module
main()
  File testdir.py, line 26, in main
for field, range_dir in fields:
ValueError: too many values to unpack (expected 2)

Please will someone explain what I am doing wrong?

Also I would like to ask how I could print the ranges in the order they are 
defined.  Should I use a different dictionary class or could I add a field to 
the dictionary/list to achieve this?

Best regards

David

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


What sort of data structure to use?

2015-06-03 Thread David Aldrich
Hi

I have written a Python utility that performs a certain activity on some 
predefined sets of files.  Here is the outline of what I have written:

# File Set A
pathA = 'pathA'
fileListA = ['fileA1.txt', 'fileA2.txt']

# File Set B
pathB = 'pathB'
fileListB = ['fileB1.txt', 'fileB2.txt', 'fileB3.txt']

myFunc1(pathA, fileListA)
myFunc2(pathA, fileListA)

myFunc1(pathB, fileListB)
myFunc2(pathB, fileListB)

I want to add more file sets, so I really want to add the sets to a list and 
iterate over the list, calling myFunc1  myFunc2 for each item.

My question is: what sort of data structure could I use to organise this, given 
that I want to associate a set of files with each path and that, for each set, 
there is an arbitrary number of files?

Best regards

David

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


RE: What sort of data structure to use?

2015-06-03 Thread David Aldrich
Thanks very much for all the answers given to my question. They help me to 
think about the problem pythonically.

Best regards

David

 -Original Message-
 From: Python-list [mailto:python-list-
 bounces+david.aldrich=emea.nec@python.org] On Behalf Of Peter
 Otten
 Sent: 03 June 2015 11:59
 To: python-list@python.org
 Subject: Re: What sort of data structure to use?
 
 David Aldrich wrote:
 
  Hi
 
  I have written a Python utility that performs a certain activity on
  some predefined sets of files.  Here is the outline of what I have written:
 
  # File Set A
  pathA = 'pathA'
  fileListA = ['fileA1.txt', 'fileA2.txt']
 
  # File Set B
  pathB = 'pathB'
  fileListB = ['fileB1.txt', 'fileB2.txt', 'fileB3.txt']
 
  myFunc1(pathA, fileListA)
  myFunc2(pathA, fileListA)
 
  myFunc1(pathB, fileListB)
  myFunc2(pathB, fileListB)
 
  I want to add more file sets, so I really want to add the sets to a
  list and iterate over the list, calling myFunc1  myFunc2 for each item.
 
  My question is: what sort of data structure could I use to organise
  this, given that I want to associate a set of files with each path and
  that, for each set, there is an arbitrary number of files?
 
 I'd start simple and put (path, files) pairs into a list:
 
 path_files_pairs = [
 (pathA, [fileA1.txt, fileA2.txt, ...]),
 (pathB, [fileB1.txt, ...]),
 ]
 
 for path, files in path_files_pairs:
 func1(path, files)
 func2(path, files)
 
 You can always add complications later:
 
 import glob
 import os
 
 class VirtualFileset:
 def __init__(self, folder, pattern):
 self.folder = folder
 self.pattern = pattern
 def __iter__(self):
 yield self.folder
 yield glob.glob(os.path.join(self.folder, self.pattern))
 
 path_files_pairs = [
 (pathA, [fileA1.txt, fileA2.txt, ...]),
 (pathB, [fileB1.txt, ...]),
 VirtualFileset(pathC, *.py), # all python files in directory pathC
 ]
 
 for path, files in path_files_pairs:
 func1(path, files)
 func2(path, files)
 
 
 --
 https://mail.python.org/mailman/listinfo/python-list
 
 
  Click
 https://www.mailcontrol.com/sr/51ZWmSF1P47GX2PQPOmvUmaGI8Tu3yGr
 Vrr5Tv1xM3UP2MNyoKSTyt0rIsjE4onM5MUvmWbo6fT3KeH4!zzvzA==  to
 report this email as spam.
-- 
https://mail.python.org/mailman/listinfo/python-list


Problem running Python 2.7 on Ubuntu 10.04

2015-04-20 Thread David Aldrich
Hi

I wonder if someone could help me with this problem please?

On an Ubuntu 10.04 platform, I want to run the latest version of Meld, which is 
a Python program.

Ubuntu 10.04 runs Python 2.6 as standard.  Meld requires Python 2.7.  So I have 
installed Python 2.7 under /usr/local/bin and Python 2.7 runs ok there.

But when I try to run Meld I see:

- /usr/local/bin/python2.7 ~/meld/bin/meld
Cannot import: GTK+
No module named gi

So I need to install the gtk package and do so in such a way that it is visible 
to /usr/local/bin/python2.7.

How would I do that please?

Best regards

David



Best regards

David

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


RE: Python shell: Arrow keys not working in PuTTY

2015-02-24 Thread David Aldrich
  BUT do *not* run `make install` as that will overwrite your system
  Python and Bad Things will happen. Instead, run `make altinstall`.

Thanks for all the warnings. We did use `make altinstall`, so all is ok.

Recompiling, with readline installed, fixed the arrow keys.
-- 
https://mail.python.org/mailman/listinfo/python-list


Python shell: Arrow keys not working in PuTTY

2015-02-23 Thread David Aldrich
Hi

I want to use the Python 3.4 interpreter interactively, via a PuTTY ssh 
session.  Python is running on Centos 5.

Currently, the arrow keys do not work:

$ /usr/local/bin/python3.4
Python 3.4.2 (default, Feb 11 2015, 15:06:33)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-55)] on linux
Type help, copyright, credits or license for more information.
 ^[[A

This stackoverflow thread:

http://stackoverflow.com/questions/893053/python-shell-arrow-keys-do-not-work-on-remote-machine

suggests that the problem can be fixed by installing the readline package:

sudo apt-get install libreadline-dev

followed by a rebuild of Python

or

pip install readline

Please can anyone comment on the easiest way to fix this?  Is a rebuild of 
Python necessary?

Best regards

David

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


RE: Python shell: Arrow keys not working in PuTTY

2015-02-23 Thread David Aldrich
Thanks for your replies, I will give readline a try.

 PS: and you mention being on CentOS but running apt-get.  I believe CentOS
 and other Red-Hat based distros use yum instead of apt-get

Yes, I think I need to use:

yum install readline-devel

Best regards

David
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: A question about a list and subprocess.check_call()

2015-02-17 Thread David Aldrich

It's also possible to do it the other around using shlex.split. I prefer that 
version because 
I can easily copy/paste the command from code to the shell, it's also more 
readable IMO:

 cmd = python3 -O -c import sys; print(sys.argv[1:]) foo bar spam egg 
 
 print(cmd)
 subprocess.check_call(shlex.split(cmd))

Thanks again for the replies to my question.  I especially like the solution of 
using shlex.split().
 

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


RE: A question about a list and subprocess.check_call()

2015-02-16 Thread David Aldrich
Hi Peter

Thanks very much for your reply. I have added one more question below.

 The straightforward approach is to pass a list or tuple:
 
 def build(build_options=()):
 subprocess_check_call((make,) + build_options)
 
 build((flagA=true, flagB=true))

This looks fine - I am trying it.

I would like to display on the console the entire make command, so I have done 
this:

def build(build_options=()):
make_command = 'make '.join(map(build_options))
print('Build command: ' + make_command)
subprocess.check_call((make,)+build_options)

but I get error:

make_command = 'make '.join(map(build_options))
TypeError: map() must have at least two arguments.

What would be the correct way to concatenate and display the elements in the 
tuple please?

Best regards

David

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


A question about a list and subprocess.check_call()

2015-02-16 Thread David Aldrich
Hi

I wonder if someone could help me with this problem please.  I am writing a 
Python script that builds and tests a C++ program on Linux.  The build options 
depend on the test, so I have encapsulated the 'make' call in a Python function:

def build(build_options=''):
if len(build_options):
subprocess.check_call(['make',build_options])
else:
subprocess.check_call('make')

This works fine if I call:

build()
or
build('flagA=true')

The latter gives:

make flagA=true

which is correct.

However, I now want to call make with two flags:

make flagA=true flagB=true

I tried calling:

build('flagA=true flagB=true')

which did indeed result in:

make flagA=true flagB=true

but 'make' ignored the second option. So I think that the list that was passed 
to subprocess.check_call() was incorrect.

In summary, I want to pass a list to build(), which by default should be empty, 
and pass that list on to subprocess.check_call() with 'make' as the first 
element of the list.

Any ideas please?

Best regards

David

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


Beginner question - class definition error

2015-01-28 Thread David Aldrich
Hi

I am just getting started with Python 3.3.3 and Kivy 1.8.

I am using the Kivy  development environment on Windows (open a command prompt 
and call kivy.bat).

With this minimal code:

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.uix.label import Label

class MyApp(App):
  def build(self):
return Label(text='Hello World')

  if __name__ == '__main__':
MyApp().run()

I get this error when I run it:

C:\python MinimalApplication.py
[INFO  ] Kivy v1.8.0
[INFO  ] [Logger  ] Record log in snip
[INFO  ] [Factory ] 157 symbols loaded
[DEBUG  ] [Cache   ] register kv.lang with limit=None, 
timeout=Nones
[DEBUG  ] [Cache   ] register kv.image with limit=None, 
timeout=60s
[DEBUG  ] [Cache   ] register kv.atlas with limit=None, 
timeout=Nones
[INFO  ] [Image   ] Providers: img_tex, img_dds, img_pygame, 
img_gif (img_pil ignored)
[DEBUG  ] [Cache   ] register kv.texture with limit=1000, 
timeout=60s
[DEBUG  ] [Cache   ] register kv.shader with limit=1000, 
timeout=3600s
[INFO  ] [Text] Provider: pygame
 Traceback (most recent call last):
   File MinimalApplication.py, line 7, in module
 class MyApp(App):
   File MinimalApplication.py, line 12, in MyApp
 MyApp().run()
 NameError: name 'MyApp' is not defined

How can I fix this please?

Best regards

David

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


RE: Beginner question - class definition error

2015-01-28 Thread David Aldrich
 Unindent the 'if' statement. Currently, it's indented inside the class
 definition, so MyApp isn't defined yet.

Thanks very much. That fixed it.

Best regards

David
-- 
https://mail.python.org/mailman/listinfo/python-list


A question about setup.py

2014-12-04 Thread David Aldrich
Hi

I'm trying to install the path.py package under Python 2.7 on Windows.

I installed it using:

easy_install path.py

That worked but it didn't install path.py which is needed by my PTVS IDE for 
code completion (Intellisense).

I then tried downloading path.py-7.0.zip. I unzipped it and ran:

python setup.py install

within the extracted folder. But path.py still appears not to have been copied 
to:

C:\Python27

What am I doing wrong?

Best regards

David

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


Where is path.py?

2014-11-27 Thread David Aldrich
Hi

I am running Python 2.7 on Windows 8.1. Today I installed path.py using 
easy_install:

easy_install path.py

The example code that I've seen imports path.py as follows:

from path import path

I am fairly new to Python and have a few questions about this:

1) Why is 'from path' required here?

2) I am using Microsoft's PTVS IDE to develop my code. It is not displaying 
code completion for path.py (it works for the standard libraries). How would I 
tell PTVS about path.py? 

Perhaps I just need to know where path.py is as I can't find it in my 
filesystem.

Best regards

David

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


RE: Where is path.py?

2014-11-27 Thread David Aldrich
 The syntax:

from module import name

 will import the name name from the module module, so:

 from path import path

 will import the name 'path' (a class) from the module 'path'.

Thanks.  But I don't quite understand.  If I use sys:

import sys

args = sys.argv[1:]

I don't need to use 'from'.  What is different with using path?

 Perhaps I just need to know where path.py is as I can't find it in my
 filesystem.

Try:

import path
print path.__file__

c:\Python27\lib\site-packages\path.py-7.0-py2.7.egg\path.pyc

Where would I typically put that path in an IDE?

Thanks for your help.

David
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Where is path.py?

2014-11-27 Thread David Aldrich
 The only confusing bit here is that instead of sys and argv, you have
 path and path, the same name twice. But it's the same thing happening.

Thanks very much for all replies I received for my question. It's clearer now.

By the way, for Windows users, I do recommend Microsoft's PTVS (Python Tools 
for Visual Studio). It's an excellent IDE and free if you use the Express 
Edition of Visual Studio.

David
-- 
https://mail.python.org/mailman/listinfo/python-list


Missing python27.dll on Win 7 64-bit

2011-06-17 Thread David Aldrich
Hi

I am building a 32-bit C++ application using Visual C++ Express 2008 on 64-bit 
Windows 7.  The application links to Python, so I installed 32-bit Python 2.7.2 
by running python-2.7.2.msi.

When I run my app, I get error:

... python27.dll is missing from your computer ...

and, indeed, it is in neither C:\Windows\System32 nor C:\Windows\SysWOW64.

Please will someone suggest what I am doing wrong?

Best regards

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


Missing python27.dll on Win 7 64-bit

2011-06-16 Thread David Aldrich
Hi

I am building a 32-bit C++ application using Visual C++ Express 2008 on 64-bit 
Windows 7.  The application links to Python, so I installed 32-bit Python 2.7.2 
by running python-2.7.2.msi.

When I run my app, I get error:

... python27.dll is missing from your computer ...

and, indeed, it is in neither C:\Windows\System32 nor C:\Windows\SysWOW64.

Please will someone suggest what I am doing wrong?

Best regards

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