[Tutor] UDP client

2017-02-25 Thread Phil

Thank you for reading this.

As an exercise, and for no other purpose, I'm trying to convert some C++ 
code that I put together 17 years ago. I'm very rusty and hours of 
Internet searches have made me more confused that I was to start with.


The following works under Python2 but not under Python3.

import socket

host = "127.0.0.1"
#host = "localhost"

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

s.connect((host, 1210))

data = "GET_LIST"

s.sendall(data)

#s.sendto(data, (host, 1210))

s.shutdown(1)

while 1:
buf = s.recv(2048)
if not len(buf):
break
#print "Received: %s" % buf

According to the Python3 wiki sendto() should work under Python3 but I 
get this error:


Traceback (most recent call last):
  File "/home/phil/Python/predict_client1.py", line 12, in 
s.sendall(data)
TypeError: a bytes-like object is required, not 'str'

The same error is received if I use sendall(data).

So, is this reasonable UDP client code and what have I overlooked to get 
this to work under Python3?


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


Re: [Tutor] Matplotlib in Tkinter

2017-02-25 Thread Albert-Jan Roskam
(Sorry for top-posting)

Hi, I recently ran into the same problem when I wanted to embed an interactive 
Matplotlib choropleth in my Tkinter app. I solved it by creating a separate 
class MyPlot for plot + toolbar (it inherited from Tkinter.Frame). MyPlot 
internally uses the pack geometry manager as given in the example from the mpl 
website. Then, you use MyPlot as 'one unit', and you use the grid method on its 
instances.

Best wishes,
Albert-Jan

From: Tutor  on behalf of 
Pooja Bhalode 
Sent: Sunday, February 19, 2017 7:36:32 PM
To: tutor@python.org
Subject: [Tutor] Matplotlib in Tkinter

Hi,

I am trying to create a graph in Tkinter window. And following is a snipet
of the code.

Code:
  simroot = Toplevel(root)
simroot.title("Simulation of experiments")
Label(simroot, text = "Displaying concentration profiles with respect to
time:").pack(side = TOP)

Label(simroot, text = "Select the experiments to be displayed: ").pack(side
= TOP)
optionexp = ['Experiment 1', 'Experiment 2', 'Experiment 3', 'Experiment
4', 'Experiment 5']
expsim = StringVar()
dropexp = OptionMenu(simroot, expsim, *optionexp)
dropexp.pack(side = TOP)
dropexp.config(width = 15)

# Row 1 would print the name of the experiment selected and the species
displayed would be in row3.
Label(simroot, text = "Select concentration species:").pack(side = TOP)
concsim = StringVar()
optionlistsim = ['A', 'B', 'C', 'D', 'E']
dropsim = OptionMenu(simroot, concsim, *optionlistsim)
dropsim.pack(side = TOP)
dropsim.config(width = 8)
Label(simroot, text = "").pack(side = LEFT)

def Submitplot():
print "Create plot"

f = Figure(figsize = (5,5), dpi = 80)
a = f.add_subplot(111)
a.plot([1,2,3,4,5],[10,11,12,14,15])

canvas = FigureCanvasTkAgg(f, simroot)
canvas.show()
canvas.get_tk_widget().pack(side = BOTTOM)

toolbar = NavigationToolbar2TkAgg(canvas, simroot)
toolbar.update()
canvas._tkcanvas.pack(side = BOTTOM)
Button(simroot, text = "Submit and Plot", command = Submitplot).pack(side =
BOTTOM)

Here, the output comes as:
[image: Inline image 1]
The problem is I am not able to use grid layout instead of pack (used
here). I want to place the text to the left, top side in the window.

Also, when I hover the mouse over the figure and the x and y values are
shown, it flickers really fast and the white space in the Tkinter window
turns black when it flickers. Can someone please tell me what the issue
could be? Also, the toolbar should have been at the bottom but it shows up
at the top.

I am using pack in this Toplevel of the main root whereas in the rest of
the code, I am using grid layout. I switched to pack for this
snipet because I could not figure out how grid would work for this part.

Can someone please let me know how to correct these issues?
Thankyou so much.

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


Re: [Tutor] Invalid Syntax

2017-02-25 Thread Peter Otten
Alan Gauld via Tutor wrote:

> On 25/02/17 10:35, ehsan faraz wrote:
>> ... where “tip” is an invalid syntax.

Alan went one step ahead and addressed the logical errors, but the 
SyntaxError is caused by missing closing parentheses:

>> price = int(input("Enter the menu price for your meal:")
>> tip = int(input("Enter your tip percentage:")


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


Re: [Tutor] Fwd: Re: SciPy Optimize-like calling function as string

2017-02-25 Thread Alan Gauld via Tutor
On 25/02/17 17:24, Alan Gauld via Tutor wrote:

>> If you don't know that you need to optimize then your
>> time is usually better spent elsewhere. Dictionaries
>> are pretty fast in Python and I'd suggest you try
>> that first before shaving milliseconds in places
>> that may not be where you need to make savings.
> 
> Honestly, that method makes no sense to me whatsoever. 

Sorry, what I meant is: try the simpler dictionary
based technique first. Dictionaries are what Python
uses internally for almost everything so they
are very fast. Only if you know you need to improve
on their performance should you try using other
techniques (and that's an extremely rare scenario).

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Invalid Syntax

2017-02-25 Thread Steven D'Aprano
On Sat, Feb 25, 2017 at 02:35:21AM -0800, ehsan faraz wrote:

> Hello, I am working on the following assignment where “tip” is an 
> invalid syntax. Can someone please tell me how to fix this? Here is 
> what it should look like:

Hello, and welcome!

Why don't you show us the actual error message you get? That will point 
to the line with the problem, or at worst the next line. Otherwise we 
have to GUESS where the problem is.

My guess is that the syntax error is this line:

> tip = int(input("Enter your tip percentage:")

Count the opening brackets ( and the closing brackets ). They don't 
match:

  ( ( )


By the way, you had an earlier line of code:


> tip = tip

That line is useless. Take it out.



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


Re: [Tutor] Invalid Syntax

2017-02-25 Thread Alan Gauld via Tutor
On 25/02/17 10:35, ehsan faraz wrote:
> ... where “tip” is an invalid syntax. 

You need to assign a value to tip before trying
to read it, otherwise tip is undefined.

Similarly you should define price before trying
to use it.

But those would give you a NameError not a
syntax error... Can you send us the full error
text please (always do this when posting, it
helps a lot)


> sales_tax = 0.085 * price
> total_due = sales_tax * price + tip

You access price and tip before defining them.

And shouldn't that * be a +?

> tip = tip

This line does nothing

> print("Welcome to Restaurant Helper.")

> price = int(input("Enter the menu price for your meal:")
> tip = int(input("Enter your tip percentage:")

You need these 2 lines before the calculations above.

> print("The price of the meal is:" , price)
> print("The sales tax due is:" , sales_tax)
> print("The tip amount is:" , tip)
> print("the total due is:" , total_due) 


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] counting number of loops

2017-02-25 Thread Alan Gauld via Tutor
On 25/02/17 17:12, Rafael Knuth wrote:
> I want to compare two strings and count the number of identical letters:
> 
> stringB = "ABCD"
> stringA = "AABBCCDDEE"
> for b in stringB:
> if b in stringA:
> r = 0
> r += 1
> print (r)
> 
> How do I count the output (r) instead of printing it out?
> (result should be 4). Thanks!

You are very close.
You are doing too much inside the for loop.
1) initialise the counter 'r' before the loop starts
 - you only want it set to zero at the begiunning not
   everytime you go through the loop.

2) move the print(r) line out of the loop at the end
 - you only really need to know the final answer, not
   the answer after each loop iteration..

BTW If you have covered sets then there is an easier
solution by converting both strings to sets and
taking the intersection. But you may not have
done sets yet...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] Fwd: Re: SciPy Optimize-like calling function as string

2017-02-25 Thread Alan Gauld via Tutor
Forwarding to list.

Please always use reply-All or Reply-list when responding to the list.

On Feb 18, 2017, at 6:21 PM, Alan Gauld via Tutor > wrote:
>
> On 18/02/17 21:52, Joseph Slater wrote:
>> I'm trying to use the scipy.optimize code ...
>> I've seen this done with dictionaries on some pages,
>> but it seems that this is intended to be faster
>
> There is a saying in programming that premature
> optimization is the root of all evil.

This is true, but I'm conceding that I don't need the solution so much
as I'm trying to polish my Python. In fact, there is no problem I'm
solving today. This is mostly a training exercise. I'd like to be as
"pythonic" as possible in the long run.

>
> If you don't know that you need to optimize then your
> time is usually better spent elsewhere. Dictionaries
> are pretty fast in Python and I'd suggest you try
> that first before shaving milliseconds in places
> that may not be where you need to make savings.

Honestly, that method makes no sense to me whatsoever. 

>
> Identify an actual problem, then identify the cause
> and optimise that. Don't try to second guess the future.
> Most performance problems are down to bad algorithms
> or bad data structures not bad dispatch techniques.

(I will go Google "dispatch techniques"). 

I'm trying to overcome 20 years of "Matlab" programming to modernize.
I've fallen way out of touch and taken this up as a hobby that may also
be professionally helpful from time to time. 

Sorry for the delay. I really do appreciate the responses. It's my hobby
versus my day job now. My students get all the fun of actually
programing these days!


Professor & Chair

(+1) 937-775-5040
https://people.wright.edu/joseph.slater

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


[Tutor] counting number of loops

2017-02-25 Thread Rafael Knuth
I want to compare two strings and count the number of identical letters:

stringB = "ABCD"
stringA = "AABBCCDDEE"
for b in stringB:
if b in stringA:
r = 0
r += 1
print (r)

How do I count the output (r) instead of printing it out?
(result should be 4). Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Invalid Syntax

2017-02-25 Thread ehsan faraz
Hello, I am working on the following assignment where “tip” is an invalid 
syntax. Can someone please tell me how to fix this? Here is what it should look 
like:

Welcome to Restaurant Helper. 
Enter the menu price for your meal: 100 
Enter your tip percentage: 15 T
he price of the meal is: 100.00 
The sales tax due is: 8.50 
The tip amount is: 15.00 
The total due is: 123.50

The program asks the diner for the menu price of the meal and what percentage 
of that price the diner wishes to give as a tip. 
The program then computes the sales tax amount, the tip amount, and the total 
that will be paid to the restaurant.
The program should assume that the local sales tax is 8.5 percent. Also, the 
program should compute the amount of the tip by applying the tip percentage 
only to the menu price. 


sales_tax = 0.085 * price

total_due = sales_tax * price + tip

tip = tip

print("Welcome to Restaurant Helper.")
price = int(input("Enter the menu price for your meal:")
tip = int(input("Enter your tip percentage:")
print("The price of the meal is:" , price)
print("The sales tax due is:" , sales_tax)
print("The tip amount is:" , tip)
print("the total due is:" , total_due) 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor