Re: [Tutor] loading modules only when needed and PEP 008

2008-03-19 Thread Eric Walstad
Hey Timmie,
Tim Michelsen wrote:
>> When I do this sort of thing I like
>> to move my imports into my functions/methods. 
> Would this then be compliant with the style recommendations?
Hm, I'm not sure.  I guess that if you consider that somewhere near the
top of the style guide it says something about 'foolish consistency'
than I suppose it does .  I think it's ok to deviate from the pep
when it makes sense to do so.  Define 'makes sense'.  I'm assuming that
importing the matplotlib at the top of your file is making your app
unusably slow; to me that is a great reason to break from the style guide.


>> And as we are talking about style, note that your else: pass isn't
>> really necessary but it does make it explicitly clear that you are 
>> choosing not to do anything if the plot_data isn't 'yes'. 
> Ok, so I already got some style here, at least...
I wouldn't include the 'else' bits, but that's my style.


>> Are those
>>  tabs you're using?  Four spaces are preferred, according to the
>> style guide.
> I always use 4 spaces. This sometimes leads to confusions when working 
> with the same editor on text files that rely tabs but this is another 
> issue...
Sorry.  In my mail reader the indentation looked like tabs.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading parts of a input string into different variables based on units.

2008-03-19 Thread Marc Tompkins
On Wed, Mar 19, 2008 at 3:56 PM, Tim Michelsen <[EMAIL PROTECTED]>
wrote:

> m = 0
> cm = 0
> mm = 0
> ## first list item
> if first.endswith("m",-1):
> m = first.strip("m")
> elif first.endswith("cm",-2):
> cm = first.strip("cm")
> elif first.endswith("mm",-2):
> mm = first.strip("mm")
> else:
> print 'Wrong unit!'
> ## second list item
> if second.endswith("m",-1):
> m = second.strip("m")
> elif second.endswith("cm",-2):
> cm = second.strip("cm")
> elif second.endswith("mm",-2):
> mm = second.strip("mm")
> else:
> print 'Wrong unit!'
> ## third list item
> if second.endswith("m",-1):
> m = second.strip("m")
> elif second.endswith("cm",-2):
> cm = second.strip("cm")
> elif second.endswith("mm",-2):
> mm = second.strip("mm")
> else:
> print 'Wrong unit!'
>

Since they all end in "m", if you do that test first it catches all three
cases.  It's almost as if you put the "else" before the "if".
Regexes are a wonderful thing to learn - do that by all means - but the
simplest thing would be to reverse the order of your tests: check against
"mm" first, then "cm", THEN "m".

To clarify - regexes are probably the best solution for your current
program, but as a general rule it helps to look at tests like this from a
different angle.

-- 
www.fsrtechnologies.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python to C++

2008-03-19 Thread Michael Langford
Weave is probably what you really want to do, although it doesn't do
what you said: http://wiki.python.org/moin/weave
Pyrex actually does what you said: http://wiki.python.org/moin/Pyrex

On Wed, Mar 19, 2008 at 7:52 PM, Dinesh B Vadhia
<[EMAIL PROTECTED]> wrote:
>
>
> Say because of performance, you might want to re-write/convert Python code
> to C++.  What is the best way (or best practice) to do this wrt the tools
> available?
>
> Dinesh
>
> ___
>  Tutor maillist  -  Tutor@python.org
>  http://mail.python.org/mailman/listinfo/tutor
>
>



-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converter

2008-03-19 Thread bob gailer
[EMAIL PROTECTED] wrote:
> print "Converter"
> number = 2.5
> n = int(raw_input("Insert Feet Amount"))
> while n > 0:
> print n, "Feet is"
> print n*12, "inches"
> print n*30, "centimeters"
> print
>
> Currently this is what I am working with.
> 2 problems I am facing.
> 1. When I run the script and type in the amount of feet it calculates it OVER 
> AND OVER.
>   

I'd like to introduce you to the concept known as "desk checking". 
Pretend you are the computer executing this program. Mentally (or even 
better on paper) execute each statement in the order that the computer 
would. Write down the values of variables as they change. Especially pay 
attention to the value of n. Also ask of what use is the variable numnber?
> [snip]
>
> 2. Currently this script only works for the amount of feet, is their a way to 
> have it choose feet, inches, to convert into whatever, without having to 
> right the script over and over, and all in different programs. 

I don't understand this part of the question.
> Maybe using if raw_input = feet then do this equation. I am coding in pure 
> Python on my mac, so please nothing in C as  have been some answers I have 
> received.

-- 
Bob Gailer
919-636-4239 Chapel Hill, NC

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Converter

2008-03-19 Thread wackedd
print "Converter"
number = 2.5
n = int(raw_input("Insert Feet Amount"))
while n > 0:
print n, "Feet is"
print n*12, "inches"
print n*30, "centimeters"
print

Currently this is what I am working with.
2 problems I am facing.
1. When I run the script and type in the amount of feet it calculates it OVER 
AND OVER.

ex: 1 Feet is
12 inches
30 centimeters
1 Feet is
12 inches
30 centimeters
1 Feet is

2. Currently this script only works for the amount of feet, is their a way to 
have it choose feet, inches, to convert into whatever, without having to right 
the script over and over, and all in different programs. Maybe using if 
raw_input = feet then do this equation. I am coding in pure Python on my mac, 
so please nothing in C as  have been some answers I have received.
Thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading parts of a input string into different variables based on units.

2008-03-19 Thread Kent Johnson
Alan Gauld wrote:

> For something like this I'd use regular expressions.
> If your strings vary in length then I'd use a separate regular
> expression per unit then use that to findall matching
> substrings for each unit.

> Of course you will have to work out the right regex but that
> shouldn't be too difficult if you are sure you have a whitespace
> separator and a number followed by the unit string.

One regex can split apart a numeric part and a non-numeric unit:

In [22]: import re
In [23]: splitter = re.compile(r'(\d+)(\S+)')
In [24]: splitter.findall('2m 4cm 3mm')
Out[24]: [('2', 'm'), ('4', 'cm'), ('3', 'mm')]
In [25]: splitter.findall('1pound 30pence')
Out[25]: [('1', 'pound'), ('30', 'pence')]

If you want to allow decimals change the regex to r'([\d.]+)(\S+)'

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reading parts of a input string into different variables based on units.

2008-03-19 Thread Tim Michelsen
Hello,
thank you for the fast reply!

> A regex would avoid that since it would detect an m
> as being diffrent to a cm or mm.
> 
> Of course you will have to work out the right regex but that
> shouldn't be too difficult if you are sure you have a whitespace
> separator and a number followed by the unit string.
I will see whether I can get the right regex together. Ease for educated 
computer scientist but for me coming from a Non-Programmer background 
the rege still bears some fear... and a learning curve.

> Now you have a list of values for each unit, you just need
> to convert the string values to numeric and sum them or
> whatever else you need to do.
I will try to step forward and the send my results back.

Thanks and regards,
Timmie

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] loading modules only when needed and PEP 008

2008-03-19 Thread Tim Michelsen
> When I do this sort of thing I like
> to move my imports into my functions/methods. 
Would this then be compliant with the style recommendations?

 > The 'main' code then
> conditionally calls the function/method.  All the code in the
> function safely assumes that the import has been done but code in the
> calling function assumes the import hasn't been done.
Yes, this should a the solution.


> And as we are talking about style, note that your else: pass isn't
> really necessary but it does make it explicitly clear that you are 
> choosing not to do anything if the plot_data isn't 'yes'. 
Ok, so I already got some style here, at least...

> Are those
>  tabs you're using?  Four spaces are preferred, according to the
> style guide.
I always use 4 spaces. This sometimes leads to confusions when working 
with the same editor on text files that rely tabs but this is another 
issue...


Thanks very much for your fast response!

Kind regards,
Timmie

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python to C++

2008-03-19 Thread Dinesh B Vadhia
Say because of performance, you might want to re-write/convert Python code to 
C++.  What is the best way (or best practice) to do this wrt the tools 
available?

Dinesh
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using pyusb

2008-03-19 Thread Michael Langford
Btw, the win32 version of libusb is not maintained anymore, and bogus
in my experience. I didn't really get to use libusb much on linux, but
it seemed to get a descriptor at least. IIRC, they were upset with
their interface and in the middle of vastly changing it.

  --Michael

On Wed, Mar 19, 2008 at 7:26 PM, Michael Langford
<[EMAIL PROTECTED]> wrote:
> I've never heard of the type of cable you're using. Can you send a
>  link to one to the list?
>
>  You're missing most of the USB ideas more than the python ideas.
>
>  USB is very very complicated. The USB spec (of which you care about
>  chapter 9) is nothing like how most people actually use USB. Most
>  people use a HID device (even for things that have nothing to do with
>  human interfaces) and communicate that way.
>
>  If you're going to go with a raw control then:
>  First off: Are you sure you're getting  handle to the device? What is
>  the fault you are seeing?
>
>  Secondly: How do you know what endpoints your device has? Are you sure
>  you have the right addresses for them? You should be able to see this
>  with a USB snooping utility.
>
>  Control is used to use channel 0 on the USB. It only allows 8 bytes of
>  data and a time and is usually used to switch between modes on a
>  device in my experience. You may have to do a write here to put the
>  device in a mode to do something . This is incredibly device specific.
>
>  Secondly, why are you using two different endpoints for read and
>  write? Usually you can use 82 and 02 both. Is this something in the
>  cable documentation that tells you to do this?
>
>  As you have bulk endpoints, you should be using bulk read/write. There
>  are 4 types of USB endpoints: Control, Isochronous, Interrupt and
>  Bulk.
>
>  Control Transfers are only sent over endpoint 0.
>  Isochronous Transfers are sent periodically and are used for "real
>  time" devices such as web cams (in practice, very very few people ever
>  use this mode)
>  Interrupts Transfers are next, they have a high priority after the
>  first two types and are limited to 256 bytes (IIRC)
>  Bulk Transfers are JUST like interrupt transfers, except, they are
>  lower priority.
>
>  As you apparently are using a bulk endpoint, you need to use bulk
>  transfers with it. The priorities are only with regards to the USB
>  system. Just as long as your device is using all the same priority,
>  don't worry about which one you use.
>
>  Your OS should have some tool to allow you to view the USB descriptors
>  of the device. This will at least tell you where the endpoints are
>  that you maybe could be communicating over.
>
>  Please check your control panel or the command line utils (or /proc
>  device) that tells you this info, as well as where you're stuck before
>  we can help you more.
>
>  --Michael
>
>  PS: I would have loved to have know about these modules a couple
>  months ago. I could have avoided some C kernel modules perhaps.
>
>
>
>
>  On Wed, Mar 19, 2008 at 4:14 PM, Mike Holloway <[EMAIL PROTECTED]> wrote:
>  > Hi all,
>  >
>  >  I'm very new to this list, so hello all!
>  >  I'm just starting out using python, my background is lamp.
>  >  t
>  >  I have a Prolific Technologies bridged usb cable that I wish to talk to
>  >  using pyusb and libusb, I've tried following examples and compiling my
>  >  own code but I'm really struggling getting it to work.
>  >
>  >  I'm trying to send a sequence of letters to the cable, for them to
>  >  reappear on the other side. I've been trying to use bulkWrite and
>  >  bulkRead methods but I'm not sure I'm using them right. There's also
>  >  controlMethod, but I'm not sure what that is used for.
>  >
>  >  Can anyone help get me started, I'm concerned mostly with the
>  >  communication, I reckon I could actually get somewhere if I can just
>  >  nail the first bit, here's my code so far:
>  >
>  >  * Cheers in advance, Mike.
>  >
>  >
>  >  import usb
>  >  import sys
>  >  import os
>  >  import time
>  >  from array import array
>  >
>  >  class DeviceDescriptor:
>  > def __init__(self, vendor_id, product_id, interface_id) :
>  > self.vendor_id = vendor_id
>  > self.product_id = product_id
>  > self.interface_id = interface_id
>  >
>  > def get_device(self) :
>  > buses = usb.busses()
>  > for bus in buses :
>  > for device in bus.devices :
>  > if device.idVendor == self.vendor_id :
>  > if device.idProduct == self.product_id :
>  > return device
>  > return None
>  >
>  >  class XFPS():
>  > VENDOR_ID  = 0x067B #: Vendor Id
>  > PRODUCT_ID   = 0x   #: Product Id for the bridged usb cable
>  > INTERFACE_ID = 0#: The interface we use to talk to the device
>  > BULK_IN_EP   = 0x83 #: Endpoint for Bulk reads
>  > BULK_OUT_EP  = 0x02 #: Endpoint for Bulk writes
>  > PACKET_LENGTH = 0x40#: 64 bytes
>  >

Re: [Tutor] Using pyusb

2008-03-19 Thread Michael Langford
I've never heard of the type of cable you're using. Can you send a
link to one to the list?

You're missing most of the USB ideas more than the python ideas.

USB is very very complicated. The USB spec (of which you care about
chapter 9) is nothing like how most people actually use USB. Most
people use a HID device (even for things that have nothing to do with
human interfaces) and communicate that way.

If you're going to go with a raw control then:
First off: Are you sure you're getting  handle to the device? What is
the fault you are seeing?

Secondly: How do you know what endpoints your device has? Are you sure
you have the right addresses for them? You should be able to see this
with a USB snooping utility.

Control is used to use channel 0 on the USB. It only allows 8 bytes of
data and a time and is usually used to switch between modes on a
device in my experience. You may have to do a write here to put the
device in a mode to do something . This is incredibly device specific.

Secondly, why are you using two different endpoints for read and
write? Usually you can use 82 and 02 both. Is this something in the
cable documentation that tells you to do this?

As you have bulk endpoints, you should be using bulk read/write. There
are 4 types of USB endpoints: Control, Isochronous, Interrupt and
Bulk.

Control Transfers are only sent over endpoint 0.
Isochronous Transfers are sent periodically and are used for "real
time" devices such as web cams (in practice, very very few people ever
use this mode)
Interrupts Transfers are next, they have a high priority after the
first two types and are limited to 256 bytes (IIRC)
Bulk Transfers are JUST like interrupt transfers, except, they are
lower priority.

As you apparently are using a bulk endpoint, you need to use bulk
transfers with it. The priorities are only with regards to the USB
system. Just as long as your device is using all the same priority,
don't worry about which one you use.

Your OS should have some tool to allow you to view the USB descriptors
of the device. This will at least tell you where the endpoints are
that you maybe could be communicating over.

Please check your control panel or the command line utils (or /proc
device) that tells you this info, as well as where you're stuck before
we can help you more.

 --Michael

PS: I would have loved to have know about these modules a couple
months ago. I could have avoided some C kernel modules perhaps.


On Wed, Mar 19, 2008 at 4:14 PM, Mike Holloway <[EMAIL PROTECTED]> wrote:
> Hi all,
>
>  I'm very new to this list, so hello all!
>  I'm just starting out using python, my background is lamp.
>  t
>  I have a Prolific Technologies bridged usb cable that I wish to talk to
>  using pyusb and libusb, I've tried following examples and compiling my
>  own code but I'm really struggling getting it to work.
>
>  I'm trying to send a sequence of letters to the cable, for them to
>  reappear on the other side. I've been trying to use bulkWrite and
>  bulkRead methods but I'm not sure I'm using them right. There's also
>  controlMethod, but I'm not sure what that is used for.
>
>  Can anyone help get me started, I'm concerned mostly with the
>  communication, I reckon I could actually get somewhere if I can just
>  nail the first bit, here's my code so far:
>
>  * Cheers in advance, Mike.
>
>
>  import usb
>  import sys
>  import os
>  import time
>  from array import array
>
>  class DeviceDescriptor:
> def __init__(self, vendor_id, product_id, interface_id) :
> self.vendor_id = vendor_id
> self.product_id = product_id
> self.interface_id = interface_id
>
> def get_device(self) :
> buses = usb.busses()
> for bus in buses :
> for device in bus.devices :
> if device.idVendor == self.vendor_id :
> if device.idProduct == self.product_id :
> return device
> return None
>
>  class XFPS():
> VENDOR_ID  = 0x067B #: Vendor Id
> PRODUCT_ID   = 0x   #: Product Id for the bridged usb cable
> INTERFACE_ID = 0#: The interface we use to talk to the device
> BULK_IN_EP   = 0x83 #: Endpoint for Bulk reads
> BULK_OUT_EP  = 0x02 #: Endpoint for Bulk writes
> PACKET_LENGTH = 0x40#: 64 bytes
>
> device_descriptor = DeviceDescriptor(VENDOR_ID, \
>  PRODUCT_ID, INTERFACE_ID)
>
> def __init__(self,) :
> # The actual device (PyUSB object)
> self.device = self.device_descriptor.get_device()
> # Handle that is used to communicate with device. Setup in L{open}
> self.handle = None
>
> def open(self) :
> self.device = self.device_descriptor.get_device()
> if not self.device:
> print >> sys.stderr, "Cable isn't plugged in"
> try:
> self.handle = self.device.open()
> self.handle.claimInterface(self.device_descri

Re: [Tutor] reading parts of a input string into different variables based on units.

2008-03-19 Thread Alan Gauld
"Tim Michelsen" <[EMAIL PROTECTED]> wrote

> I would like to read several parts of a string into different 
> variables
> based on the (physical) units of the quantities.
>
> Here's my testing code:
> ###
> mystring = '2m 4cm 3mm' # can also be '1pound 30pence', ...
> mylist = mystring.split(" ")

For something like this I'd use regular expressions.
If your strings vary in length then I'd use a separate regular
expression per unit then use that to findall matching
substrings for each unit.

> if first.endswith("m",-1):
> m = first.strip("m")
> elif first.endswith("cm",-2):
> cm = first.strip("cm")
> elif first.endswith("mm",-2):
> mm = first.strip("mm")

I'd also look at using a dictionary to store the results
so that you can use the unit as a key.

> Well, I cannot get the meters assigned to the m variable, the
> centimeters to the cm variable and the milimeters to the mm 
> variable.
> All units end with "m" and therefore my code confuses the strings I 
> am
> looking for. I would always reassign the m variable.

A regex would avoid that since it would detect an m
as being diffrent to a cm or mm.

Of course you will have to work out the right regex but that
shouldn't be too difficult if you are sure you have a whitespace
separator and a number followed by the unit string.

The code then becomes (in pseudo code)

cm_re = # whatever
mm_re = # whatever
m_re = # whatever

units['cm'] = cm_re.findall(theString)
units['mm'] = mm_re.findall(theString)
units['m'] = m_re.findall(theString)

Now you have a list of values for each unit, you just need
to convert the string values to numeric and sum them or
whatever else you need to do.

HTH,

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] loading modules only when needed and PEP 008

2008-03-19 Thread Eric Walstad
Hi Tim,
Tim Michelsen wrote:
> Hello fellow Pythonistas,
> I have a question concerning import statements.
...
> it takes a lot 
> of time for a TKinter-GUI to start up. And this start-up time is even 
> longer when matplotlib is imported

> a optimized version would be:
> import sys
> 
> plot_data = 'yes' # option: yes/no
> 
> if plot_data == 'yes':
>   import matplotlib
> else:
>   pass
...
> How would you handle such a case?
> What is recommended in such a case?
> Does anyone have experience with this?
Beware that code later in your module that calls matplotlib.foo() may 
fail if plot_data is not 'yes'.  When I do this sort of thing I like to 
move my imports into my functions/methods.  The 'main' code then 
conditionally calls the function/method.  All the code in the function 
safely assumes that the import has been done but code in the calling 
function assumes the import hasn't been done.

-
if plot_data:
 show_plot_data(mydata)

-
def show_plot_data(data):
 "load matplotlib and show the user the data"
 import matplotlib
 ...do stuff with matplotlib...
-

And as we are talking about style, note that your
else:
 pass
isn't really necessary but it does make it explicitly clear that you are 
choosing not to do anything if the plot_data isn't 'yes'.  Are those 
tabs you're using?  Four spaces are preferred, according to the style guide.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] reading parts of a input string into different variables based on units.

2008-03-19 Thread Tim Michelsen
Hello,
I would like to read several parts of a string into different variables 
based on the (physical) units of the quantities.

Here's my testing code:
###
mystring = '2m 4cm 3mm' # can also be '1pound 30pence', ...
mylist = mystring.split(" ")
print mylist

first = mylist[0]
second = mylist[1]
third = mylist[2]

print "first", first
print "second", second
print "third", third

m = 0
cm = 0
mm = 0
## first list item
if first.endswith("m",-1):
 m = first.strip("m")
elif first.endswith("cm",-2):
 cm = first.strip("cm")
elif first.endswith("mm",-2):
 mm = first.strip("mm")
else:
 print 'Wrong unit!'
## second list item
if second.endswith("m",-1):
 m = second.strip("m")
elif second.endswith("cm",-2):
 cm = second.strip("cm")
elif second.endswith("mm",-2):
 mm = second.strip("mm")
else:
 print 'Wrong unit!'
## third list item
if second.endswith("m",-1):
 m = second.strip("m")
elif second.endswith("cm",-2):
 cm = second.strip("cm")
elif second.endswith("mm",-2):
 mm = second.strip("mm")
else:
 print 'Wrong unit!'

print m, cm, mm
###

Well, I cannot get the meters assigned to the m variable, the 
centimeters to the cm variable and the milimeters to the mm variable. 
All units end with "m" and therefore my code confuses the strings I am 
looking for. I would always reassign the m variable.

I would be very grateful of someone could give me a point or hint how I 
read the quantites (read: numbers) into my variables m, cm, mm.

Thanks and kind regards,
Timmie

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] loading modules only when needed and PEP 008

2008-03-19 Thread Tim Michelsen
Hello fellow Pythonistas,
I have a question concerning import statements.

My code uses matplotlib to plot the results of my calculations.

Since I want to make this plotting functionality a optional feature I 
would like to import matplotlib only when needed because it takes a lot 
of time for a TKinter-GUI to start up. And this start-up time is even 
longer when matplotlib is imported.

I am still in doubt because PEP 008 [1] states that all imports should 
come right at the beginning of the code.

My current code goes something like:

import sys

import matplotlib

## 
## do some calcs
## ...
matplotlib.plot(mydata)

a optimized version would be:
import sys

plot_data = 'yes' # option: yes/no

if plot_data == 'yes':
import matplotlib
else:
pass

## 
## do some calcs
## ...
if plot_data == 'yes':
matplotlib.plot(mydata)
else:
pass

How would you handle such a case?
What is recommended in such a case?
Does anyone have experience with this?

Thanks for your suggestions in advance!

Kind regards,
Tim



[1] PEP 8 -- Style Guide for Python Code - 
http://www.python.org/dev/peps/pep-0008/

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Should config file parser be in module's __init__.py

2008-03-19 Thread Kent Johnson
Shrutarshi Basu wrote:
> Ok, I'm starting to understand how things work. Just one last
> question: suppose my package has a config.py (which contains a config
> dict) which another module in the package imports by "import config".
> If the user of my package has a config.py in the directory from where
> they run their program (which uses my package), then which config.py
> will be used? The user's or the packages? The python tutorial section
> on modules leads me to believe it will be the users, since the current
> directory is searched first, but I'm not sure because it is my package
> which does the import, not the users program.

It will be the user's config.py.

Look at sys.path to see the order in which directories are searched for 
imports.

Ken
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Should config file parser be in module's __init__.py

2008-03-19 Thread Shrutarshi Basu
Ok, I'm starting to understand how things work. Just one last
question: suppose my package has a config.py (which contains a config
dict) which another module in the package imports by "import config".
If the user of my package has a config.py in the directory from where
they run their program (which uses my package), then which config.py
will be used? The user's or the packages? The python tutorial section
on modules leads me to believe it will be the users, since the current
directory is searched first, but I'm not sure because it is my package
which does the import, not the users program.
Thanks again,
Basu
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using pyusb

2008-03-19 Thread Alan Gauld

"Mike Holloway" <[EMAIL PROTECTED]> wrote

> I have a Prolific Technologies bridged usb cable that I wish to talk 
> to
> using pyusb and libusb,

Sorry I can't help but thanks for pointing these out.
I've been intending to write a USB module for over
a year, now I don't need to! :-)

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using pyusb

2008-03-19 Thread Mike Holloway
Hi all,

I'm very new to this list, so hello all!
I'm just starting out using python, my background is lamp.

I have a Prolific Technologies bridged usb cable that I wish to talk to 
using pyusb and libusb, I've tried following examples and compiling my 
own code but I'm really struggling getting it to work.

I'm trying to send a sequence of letters to the cable, for them to 
reappear on the other side. I've been trying to use bulkWrite and 
bulkRead methods but I'm not sure I'm using them right. There's also 
controlMethod, but I'm not sure what that is used for.

Can anyone help get me started, I'm concerned mostly with the 
communication, I reckon I could actually get somewhere if I can just 
nail the first bit, here's my code so far:

* Cheers in advance, Mike.


import usb
import sys
import os
import time
from array import array

class DeviceDescriptor:
def __init__(self, vendor_id, product_id, interface_id) :
self.vendor_id = vendor_id
self.product_id = product_id
self.interface_id = interface_id

def get_device(self) :
buses = usb.busses()
for bus in buses :
for device in bus.devices :
if device.idVendor == self.vendor_id :
if device.idProduct == self.product_id :
return device
return None

class XFPS():
VENDOR_ID  = 0x067B #: Vendor Id
PRODUCT_ID   = 0x   #: Product Id for the bridged usb cable
INTERFACE_ID = 0#: The interface we use to talk to the device
BULK_IN_EP   = 0x83 #: Endpoint for Bulk reads
BULK_OUT_EP  = 0x02 #: Endpoint for Bulk writes
PACKET_LENGTH = 0x40#: 64 bytes

device_descriptor = DeviceDescriptor(VENDOR_ID, \
 PRODUCT_ID, INTERFACE_ID)

def __init__(self,) :
# The actual device (PyUSB object)
self.device = self.device_descriptor.get_device()
# Handle that is used to communicate with device. Setup in L{open}
self.handle = None
   
def open(self) :
self.device = self.device_descriptor.get_device()
if not self.device:
print >> sys.stderr, "Cable isn't plugged in"
try:
self.handle = self.device.open()
self.handle.claimInterface(self.device_descriptor.interface_id)
except usb.USBError, err:
print >> sys.stderr, err

def close(self):   
""" Release device interface """
try:
self.handle.reset()
self.handle.releaseInterface()
except Exception, err:
print >> sys.stderr, err
self.handle, self.device = None, None
   
def my_bulk_write(self):
A = chr(0x75) # u
B = chr(0x69) # i
X = chr(0x6F) # o
Y = chr(0x70) # p
LB = chr(0x6C) # l
RB = chr(0x6B) # k
LT = chr(0x68) # h
RT = chr(0x6A) # j

S = chr(0x32)
s = chr(0x73)

self.close()
self.open()
   
msg = [A,B,A,B,A,B,A,B]
#help(self.handle.bulkWrite)
help(self.handle.interruptWrite)
sent_bytes = self.handle.interruptWrite(XFPS.BULK_OUT_EP,msg,1000)
print sent_bytes
if sent_bytes:
read_bytes = self.handle.interruptRead(0x81,sent_bytes);
print read_bytes

xfps = XFPS()
xfps.open()
xfps.my_bulk_write()

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calling super classs __init__?

2008-03-19 Thread Allen Fowler

> Nowadays the best practice for invoking a method from all superclasses
> (yes, multiple inheritance) is this:
> 
> class SubClass(BaseClass):
> def __init__(self, t, *args, **kw):
> super(SubClass, self).__init__(*args, **kw)
> # do something with t
> 
> That way you let Python decide which superclasses your SubClass has,
> instead of hard-coding it in several places.
>

Excellent.  Thank you.  This seems far more logical.

Is there a proper way to handle the case when SubClass() is called using 
positional arguments, and you do not desire "t" to be at the beginning?

Thanks again,
:)





  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converter

2008-03-19 Thread Alan Gauld

"Jack Lucas" <[EMAIL PROTECTED]> wrote

>I am trying to make a converter for Python on my Mac.
> I know I can find one online, but this is more satisfying.

Have you looked at the Cocoa tutorial on using Python on MacOS?
It is just such a convertor(for currency). Using that as a template it
should be easy to modify it to use different units.

http://pyobjc.sourceforge.net/documentation/pyobjc-core/tutorial/index.html

> Also if someone would not mind explaining WHY I do certain 
> functions,
> as I am new to Python.

Its best if you give us examples. If you don't know why then ask here.

You may find the Cocoa tutorial too deep if you are that new and
a simple text based solution might be more appropriate. But the
actual coding in the Cocoa app is very little and it does look a
lot better! :-).

Alan G. 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converter

2008-03-19 Thread Timmie
> start, as I want to be able to input any Unit to Convert (will keep it simple
for now, feet, inches etc. and add
Maybe you want to get inspired by a Gnome deskbar handler:
http://www.kryogenix.org/days/2006/09/06/converter-deskbar

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Should config file parser be in module's __init__.py

2008-03-19 Thread Kent Johnson
Shrutarshi Basu wrote:
> Thanks for that, i think that creating a configuration submodule and
> importing it into the modules that need it will be the most elegant
> solution for me. I was wondering whether the following solution would
> work / be good practice?
> 1. have the parser system in __init__.py

You could do that but I would put it in a config module.

> 2. have the parser store the result in a string, say confstr

If you only have one configuration parameter this is OK. If you have 
multiple parameters you should put them in a class or dict, otherwise 
the individual modules still have to parse the string.

> 3. have modules in the package access it with __init__.confstr

No, if it is in __init__.py you access it at package scope, it would be 
mypackage.confstr.

Kent

PS Please use Reply All to reply to the list.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Converter

2008-03-19 Thread Jack Lucas
I am trying to make a converter for Python on my Mac. I know I can find one 
online, but this is more satisfying.
I am not sure exactly where to start. I want to be able to input a "Unit to 
Convert" a "Unit to Convert to" and an "Amount of Unit to Convert"
For example Unit to Convert: centimeters,  Unit to Convert to: inches, Amount 
of Unit to Convert: 2.5. Thus you would get 2.5 Centimeters is equal to 1 inch. 
Or something like that to display. I have no idea where to start, as I want to 
be able to input any Unit to Convert (will keep it simple for now, feet, inches 
etc. and add more later.) and have it display the unit I want to convert to, 
and not all the other junk. Can anyone help me? Atleast get one setup, as I am 
sure I can then copy and paste, and make minor changes for the rest. Also if 
someone would not mind explaining WHY I do certain functions, as I am new to 
Python.
Thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to search a directory from a Tkinter program

2008-03-19 Thread Alan Gauld

"brindly sujith" <[EMAIL PROTECTED]> wrote

> i am developing an application in tkinter
>
> i want to know whether we have any option to search a directory from 
> tkinter
> program

I'm not sure what you mean.
You can use the standard Python modules to search a directory,
or indeed an entire directory tree using walk(). I demonstrate this
in the OS topic of my tutor.

OTOH if you mean is there a standard directory dialog so that
the user can browse a directory then yes, look in the standard
dialogs package as described in the documentation and tutorials.
More on those here:

http://www.pythonware.com/library/tkinter/introduction/x1164-data-entry.htm

And exploring the module with dir() etc will throw up more info.

So either way it is pretty straightforward.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] c++::return

2008-03-19 Thread Luke Paireepinart
elis aeris wrote:
> I actually said "ending" to avoid problems with different terminology 
> between different languages - but i guess a return is still a return 
> in python.
Return does not exit a program.  A return statement denotes a point in a 
function when the function will stop executing and the specified value 
will be returned to the calling function.
In C++, a return in your main function will cause the program to stop 
executing, because there was no calling function to give control back to.
>
> i did this search
>
> http://www.google.ca/search?hl=en&q=python+tutorial+return&btnG=Google+Search&meta=
>  
> 
>
> and it didn't return any particularly obvious answer.
There probably won't be a whole tutorial on a 'return' statement - it's 
a fundamental part of functions and you should've picked up on it in any 
tutorial discussing how to define functions.
>
> The funny thing is, i wrote that program last june and managed to not 
> know/use a single return :)
This is not a good thing.  You shouldn't have a function that is this 
long.  The entire point of functions is small snippets of reusable code 
- having one massive function do everything negates the whole benefit of 
functions.  You may just as well have not put it in a function at all.
Where is this code, by the way?  I didn't receive a copy of it.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to search a directory from a Tkinter program

2008-03-19 Thread brindly sujith
hi

i am developing an application in tkinter

i want to know whether we have any option to search a directory from tkinter
program

please answer me

thank you
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor