Re: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3?

2013-08-11 Thread Alan Gauld

On 09/08/13 16:50, SM wrote:

Sorry I only just picked this up.


(ex: self.tab_fw = QtGui.QWidget(), self.tab_ann = QtGui.QWidget(),
etc), its own textEdit window and its own progress bar widget.
All the tabs are defined within this single class - they are not
instances of the class, as the tabs are distinct from each other. All of
that is working really well.


OK, I'll assume the structure looks like

MainGUIWindow
- Tab1
  - TextEdit1
  - ProgressBar1
  - any other widgets on tab1
- Tab2
  - TextEdit2
  - ProgressBar2
  - any other widgets on tab2   
- Tab3
  - TextEdit3
  - ProgressBar3
  - any other widgets on tab3   

And that both the TextEdit and progress bar widgets are part of
the Qt framework/library? So there are 3 distinct progress bar 
instances, one per tab?



an added feature, I am now asked to have a "progress bar" widget
which appears when an operation is going on, to tell the user that
he/she has to wait.


Is this an extra (4th?) progress bar or is this the ones already 
described as being in the tabs?


Also, when you say "appears" do you mean its invisible until the 
operation starts? Or is it visible but inactive (maybe greyed out?)



[SM]: Yes. I shouldn't say duplicated, but each tab has a different
progress bar of its own. In the example, I was quoting the code for just
on tab. The others will be implemented similarly.


OK So I'm again going to assume:

A total of 1 progress bar per tab. The "added feature" is what these 
bars are addressing?



So I am running two threads - first one is the progressbar
widget which has a rectangular slab spinning sideways to
indicate that the operation is going on.


I usually expect a progress bar to indicate the percentage complete but 
this sounds like its just a spinning eggshell activity indicator. Is 
that correct? You have no progress value indication?



The second thread is the one which is actually doing the operation.

> I have a flag that the second thread sets after the job is done

OK, again I'll assume we are dealing with just an activity indicator not 
a value display.



and stops spinning and gets out of the loop when the flag is set.  I
have that working as well.


OK, Good the picture is building.


[SM]: I did start out with a timer. But due to my lack of experience
with usage of timer in Python, the code I wrote was running sequentially


That sounds like you tried to use a Python native timer. Normally in a 
GUI the toolkit (Qt in this case) provides a timer event that triggers
via the normal event mechanism. You set the timer, the event arrives and 
you do something then reset the timer for the next time round.


I found this page that describes the Qt approach (which includes
multi-shot timers) and links to more detailed examples etc.

http://qt-project.org/doc/qt-4.7/timers.html#id-fd46b213-376e-4636-a7d2-8ae899de1e11

The code is in C++ but should translate easily to python and PyQt.

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3?

2013-08-11 Thread Alan Gauld

On 11/08/13 17:05, Alan Gauld wrote:


this sounds like its just a spinning eggshell


erm, make that egg-timer... auto spell correction strikes again!

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Arduino Web Page

2013-08-11 Thread Engineering
I have written the following code in Python for my Arduino

 

#!c:\Python27\Python.exe

import cgi, cgitb;

import sys, serial

cgitb.enable()

ser = serial.Serial('COM27', 9600)

myvar = ser.readline()

print "Content-type:text/html\n\n"

print """







Real Time Temperature



  

window.onload = startInterval;

function startInterval()

{

setInterval("startTime();",1000);

}

 

function startTime(myvar)

{

document.getElementById('mine').innerHTML ="Temperature"
+parseInt(myvar);

 

}

  





Real Time Temperature:





"""

I have Apache Web Server 

But on running this  my localhost , I am getting the following output
"TemperatureNaN" . Why is NaN being showed. Why cant I get the number as
read by se.readline()?

Is there a way to separate the Python code and the Javascript on different
files. If so can you please give a example.?

I am not really interested to do with bottle or other web framework as I
will be controlling my arduino with python from the web and also do video
image processing with openCV. Open CV have a support for Python and not for
Javascript.

 

Thanks

Subhendu Sinha Chaudhuri

 

CLEANTECH SOLUTION

www.cleantechsolution.in

 

SAVE PAPER , SAVE EARTH

 

 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Beginner question

2013-08-11 Thread eschneider92
I've been learning python from the website 'inventwithpython.com', and I'm on a 
chapter that covers the following code: 



import random 
import time 
def displayIntro(): 
print('You are in a land full of dragons. In front of you,') 
print('you see two caves. In one cave, the dragon is friendly') 
print('and will share his treasure with you. The other dragon') 
print('is greedy and hungry, and will eat you on sight.') 
print() 
def chooseCave(): 
cave = '' 
while cave != '1' and cave != '2': 
print('Which cave will you go into? (1 or 2)') 
cave = input() 
return cave 
def checkCave(chosenCave): 
print('You approach the cave...') 
time.sleep(2) 
print('It is dark and spooky...') 
time.sleep(2) 
print('A large dragon jumps out in front of you! He opens his jaws and...') 
print() 
time.sleep(2) 
friendlyCave = random.randint(1, 2) 
if chosenCave == str(friendlyCave): 
print('Gives you his treasure!') 
else: 
print('Gobbles you down in one bite!') 
playAgain = 'yes' 
while playAgain == 'yes' or playAgain == 'y': 
displayIntro() 
caveNumber = chooseCave() 
checkCave(caveNumber) 
print('Do you want to play again? (yes or no)') 
playAgain = input() 

I'm confused about what the line 'checkCave(caveNumber)' does and how it works. 
I would appreciate any help with this 

Thank you, 

Eric 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python and Web

2013-08-11 Thread Engineering
Dear All

 

After a lot of struggling for weeks , I can read the serial port readings in
my web page. Below is the code

 

#!c:\Python27\Python.exe

import cgi, cgitb;

import sys, serial ,time

cgitb.enable()

ser = serial.Serial('COM27', 9600)

numOfLines = 0

print "Content-type:text/html\n\n"

print """







Real Time Temperature



Real Time Temperature:

"""

while True:

response = ser.readline()

print """



var int=self.setInterval(function(){refer()},1000);

function refer()

{

document.getElementById('mine').innerHTML ="""+response+""";

}











"""

 

ser.close()

sys.exit(0)

 

BUT is this good programming ? I personally do not feel so. 

SECONDLY , during the whole execution process , the cursor is showing busy
and I am prevented to click anything else. Why is this so? How to avoid it.


 

CLEANTECH SOLUTION

www.cleantechsolution.in

 

SAVE PAPER , SAVE EARTH

 

 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Programming for the absolute beginner 3e, Ch3 Challenge 4

2013-08-11 Thread John Feleppa
Thanks for getting back to me...

I am using Python 3.3.2.

The challenge is as follows:

# Chapter 3 Challenge 4
#
# Write the psudocode for a program where the player and the computer
# trade places in the number guessing game.  That is, the player picks a
# random number between 1 and 100 that the computer has to guess.
# Before you start, think about how you guess.  If all goes well, try
# coding the game.

It builds on the previous challenge, where the user guesses the number - I
coded that OK, as below.

import random

print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")

# set the initial values
the_number = random.randint(1, 100)
guess = int(input("Take a guess: "))
tries = 1

# guessing loop

while guess != the_number and tries < 10:
if guess > the_number:
print("Lower...")
else:
print("Higher...")

guess = int(input("Take a guess: "))
tries += 1

if guess == the_number:
print("You guessed it!  The number was", the_number)
print("And it only took you", tries, "tries!\n")
else:
print("Huh - you took ", tries, "tries and still never got it.")
print("It was ", the_number, " - but why tell you, you'll forget it,
won't you.")

input("\n\nPress the enter key to exit.")
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner question

2013-08-11 Thread Alan Gauld

On 10/08/13 04:30, eschneide...@comcast.net wrote:

I've been learning python from the website 'inventwithpython.com', and
I'm on a chapter that covers the following code:

import random
import time



def displayIntro():
  print('You are in a land full of dragons. In front of you,')
  print('you see two caves. In one cave, the dragon is friendly')
  print('and will share his treasure with you. The other dragon')
  print('is greedy and hungry, and will eat you on sight.')
  print()




def chooseCave():
 cave = ''
 while cave != '1' and cave != '2':
 print('Which cave will you go into? (1 or 2)')
 cave = input()
  return cave




def checkCave(chosenCave):
 print('You approach the cave...')
 time.sleep(2)
 print('It is dark and spooky...')
 time.sleep(2)
 print('A large dragon jumps out in front of you! He opens his jaws
and...')
 print()
 time.sleep(2)
 friendlyCave = random.randint(1, 2)
 if chosenCave == str(friendlyCave):
 print('Gives you his treasure!')
 else:
 print('Gobbles you down in one bite!')




playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':
 displayIntro()
 caveNumber = chooseCave()
 checkCave(caveNumber)
 print('Do you want to play again? (yes or no)')
 playAgain = input()




I'm confused about what the line 'checkCave(caveNumber)' does and how it
works. I would appreciate any help with this


It calls the checkCave function defined above. It passes in the cave 
'number' as an argument. The function basically decides whether the 
monster inside is friendly or not and prints an appropriate message.

It then returns control to the main while loop.

Does that help?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dbus.Array to string

2013-08-11 Thread Steven D'Aprano

On 12/08/13 08:53, Amit Saha wrote:

Hello all,

The other day, I had to convert a dbus.Array [1] object to a string. I
found a way to do it, but I am not sure if that is the best way.



Does the Array object not have a "toString" method? Or similar?

What do you get when you call str() or repr() on the Array object?



Here is a dbus.Array object:

dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103),
dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100),
dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68),
dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'),
variant_level=1)



Basically it is an array of bytes, with a couple of extra arguments. Do you 
care about the signature and variant_level arguments?




And this is how I converted it to it's string representation:


Careful. In usual Python terminology, the string representation of an object, 
or just repr, is the string that represents how the object is created, more or 
less. So the string representation would be something like:

"Array([Byte(66), Byte(105), ..., Byte(53)], signature=Signature('y'), 
variant_level=1)"

which is nothing like what you want.



import dbus



ssid = "dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103), 
dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100), dbus.Byte(54), 
dbus.Byte(55), dbus.Byte(57), dbus.Byte(68), dbus.Byte(56), dbus.Byte(53)], 
signature=dbus.Signature('y'), variant_level=1)

"


Here you create a string, which looks like "dbus.Array(...)".



ssid = ''.join([chr(character) for character in ssid])


Since ssid is already a string, iterating over it gives you characters; calling 
chr() on a character raises an exception.



ssid

'BigPond679D85'


Not with the code you provide.


If you drop the quotation marks around the dbus.Array, so you get an Array 
object instead of a string, then your code may work. But look at your list 
comprehension:

[chr(character) for character in ssid]

That's confusing. If iterating over ssid gives characters, as the code claims, 
then there is no need to call chr(), and in fact you wouldn't need the list 
comp at all, you could just say:

''.join(ssid)


But it isn't an array of characters, it is an array of bytes. So:

''.join([chr(byte) for byte in ssid])

is less misleading and confusing.

Another alternative:

''.join(map(chr, ssid))


is more terse, but may not be as readable to those who aren't familiar with 
map. Both forms should be more or less equally as efficient.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dbus.Array to string

2013-08-11 Thread Steven D'Aprano

On 12/08/13 11:40, eryksun wrote:

On Sun, Aug 11, 2013 at 9:14 PM, Amit Saha  wrote:

import dbus
ssid=dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103),

dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100),
dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68),
dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'),
variant_level=1)


temp_ssid = ''.join([chr(byte) for byte in ssid])
temp_ssid

'BigPond679D85'


Typically strings should be unicode. If the byte sequence is Latin-1
(including ASCII), you can map unichr() and join the characters with
u''.join(); that's equivalent to chr() and ''.join() in 3.x. More
generally, decode() the byte string. A simply way that works in 2.6+
is to create a bytearray:

 >>> bytearray(ssid).decode('latin-1')
 u'BigPond679D85'



Nice!

But, what makes you think that the encoding will be latin-1? If I had to guess 
an encoding, I'd guess UTF-8.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor