Re: [Tutor] Guess the number

2015-05-11 Thread Kewal Patel
i don't know if this is efficient but i think it works just fine


import random

# input a number from user

input_number = input("Enter a number")

#function defination

def guess_number(start,stop):
global input_number
try:
g_number = random.randrange(start,stop)
if g_number == input_number :
print "The Input Number is : ",g_number
else:
print "guessed number is :",g_number
reply = raw_input("Enter higher or lower")
if reply == "lower":
guess_number(start,g_number)
else:
guess_number(g_number,stop)
except ValueError,(ex):
print "you have entered wrong answer."

guess_number(1,100)

On Thu, May 7, 2015 at 9:39 AM, Ikaia Leleiwi  wrote:

> I am having trouble writing a program that guesses a number inputted by the
> user.  A number between 1-100 is inputted into the program and the computer
> produces a random integer that is the "guess" as to what the inputted
> number might be.  If the guess is lower then the inputted number then the
> user inputs the word 'higher' to indicate the computer needs to guess a
> higher number.  If the guess is higher than the inputted number then the
> user inputs the word 'lower' to indicate the computer needs to guess a
> lower number.
>
> My goal is to have this process repeat, continually narrowing down the
> range of possible numbers that the computer can guess, until it guesses the
> correct number.
>
> The code I have thus far is as follows:
>
> #Computer Guesses Number Game
> #The player chooses a number between 1 and 100
> #The computer guesses a number
> #The player inputs either higher or lower depending whether
> #the computer guesses higher than the chosen number or lower
> #When the computer guesses correctly it is congratulated
>
> import random
>
> number = int(input("Pick an integer between 1-100 "))
>
> guess = random.randint(1,100)
>
> while guess != number:
>
> if guess < number:
> print("The computer guesses: ",guess)
> input("higher or lower ")
> guess = random.randint(guess,100)
>
> elif guess > number:
> print("The computer guesses: ",guess)
> input("higher or lower ")
> guess = random.randint(1,guess)
>
> else:
> print("Congradulations Computer!! You guessed that the number was
> ",number)
>
> ---
>
> I can't figure out how to narrow down the random integer range as the
> computer guesses closer and closer to the actual value of the number chosen
> in the beginning.
>
> Any help would be greatly appreciated
>
> Thanks,
> Kai
> ___
> 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


[Tutor] TCP server/client application failing. Need some help with the basics!

2015-05-11 Thread Anubhav Yadav
I am very new to python. I have been a very mediocre programmer, but now I
have decided that I want to level up as a programmer.

I wanted to write a simple TCP client/server (where in the server acts as a
simple TCP Listener). I searched on the forums and I saw that people
advised using frameworks like twisted and sync.io for client server
applications to take advantage of asynchronous model. But since I couldn't
visualize the problems that people faced when they implemented servers as
multithreaded model, so I decided to start by implementing a multithreaded
server, and then improve as I go on facing issues.

So I started with using socket library for both client and server:

Here is my listener.py:

import socket
import threading
from time import sleep

class Serve(threading.Thread):
def __init__(self, client_socket, client_address):
threading.Thread.__init__(self)
self.socket = client_socket
self.address, self.port = client_address
def run(self):
try:
while True:
data = self.socket.recv(300)
if data:
print data
sleep(2)
else:
break
except Exception as e:
print e
finally:
self.socket.close()


if __name__ == '__main__':
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', ))
sock.listen(1)

servers = []

while True:
connection, client_address = sock.accept()
print "New Connection from {}".format(client_address)
server = Serve(connection, client_address)
servers.append(server)
server.start()

for server in servers:
server.join()

Here is my sender.py. It is supposed to simulate clients connecting to the
server.

import argparse
import socket
import threading
from time import sleep


parser = argparse.ArgumentParser(description="A simple TCP sender")
parser.add_argument('-H', '--host', help='host to connect to',
default='localhost')
parser.add_argument('-p', '--port', help='port of the host', type=int,
default=)
parser.add_argument('-w', '--workers', help='number of threads to
create', default=1000, type=int)
args = parser.parse_args()
count = args.workers

def send(id):
lock = threading.Lock()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (args.host, args.port)
try:
sock.connect(server_address)
while True:
sock.sendall('@ABCDEF1234567890FABC#')
sleep(1)
except Exception as e:
print "thread no {} killed".format(id)
print e
lock.acquire()
count-=1
lock.release()
sleep(1)
print "Total threads are {}".format(count)
finally:
sock.close()

threads = []
if __name__ == '__main__':
for i in range(args.workers):
t = threading.Thread(target=send, args=(i,))
threads.append(t)
t.start()

for thread in threads:
thread.join()

When I run the client and server together with 1000 clients, many threads
are killed on a machine with low memory, and only 210-220 clients are
connected with the server. The killed clients gave the following error:

`[Errno 104] Connection reset by peer`

Right now I am not on the low memory machine, but I remember the error also
had "broken pipe" in it.

So I decided to run the same code with 1000 clients on my laptop with 8 GB
memory. This time almost all clients where connected but one or two clients
got killed with the same above error (I could not see broken error in the
messages this time".

Then I ran the same code with 500 clients, and this is where the code broke
with the following errors.

Exception in thread Thread-4999:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
  File "/usr/lib/python2.7/threading.py", line 763, in run
  File "sender.py", line 17, in send
  File "/usr/lib/python2.7/socket.py", line 187, in __init__
error: [Errno 24] Too many open files

Total threads are 4998

These errors came a lot, but the count said that only two threads failed. I
think my logic to calculate the count is wrong.

Then I decided to use SocketServer for the server. Here is the new Server:

import SocketServer
from threading import Thread
from time import sleep

class service(SocketServer.BaseRequestHandler):
def handle(self):
print "Client connected with ", self.client_address
while True:
try:
data = self.request.recv(300)
if data:

Re: [Tutor] TCP server/client application failing. Need some help with the basics!

2015-05-11 Thread Alan Gauld

On 11/05/15 21:07, Anubhav Yadav wrote:


I wanted to write a simple TCP client/server (where in the server acts as a
simple TCP Listener). I searched on the forums and I saw that people
advised using frameworks like twisted and sync.io for client server
applications to take advantage of asynchronous model.


Yes, for large scale work I'd second that recommendation.,
But when learning its often better to start small.


multithreaded model, so I decided to start by implementing a multithreaded
server, and then improve as I go on facing issues.


But starting with a multi threaded server is not what I'd
call starting small. Its quite advanced. I'd start getting
non multi threaded servers running reliably first. Then
I'd try adding some threading. As in maybe a dozen
connections sharing some data source..

Only then would I even consider the extra issues of
scalability to thousands of connections...


Is it even right to create many thousands clients using threads on the same
machine?


It may be theoretically possible but when I was designing
large client server/web based systems I used a rule of thumb
that said not more than 100 connections per core. So for 5000 
connections I'd need 50 cores. Allowing one core for the OS to

run per box that leaves 7 cores per 8-core server, so I need
8 servers to handle 5000 connections. For resilience I'd
add another one (so called N+1 architecture).

Now those servers were all carrying significant processing
requests not simple ping-like requests, so on that basis I'd
guess that you don't need 50 cores for your example. But
it does seem like a lot to ask of a single box. And if you
intend to extend that to a network model think about the
load on your network card too. My servers at work all
had a couple of (gigabit) network cards connected.


Would love to understand the details of threading and sockets! Please do
leave your comments and criticize me. Sorry for this long post.


Too much code for me to read at this time of night, but I
would try slimming down your testing, at least initially.
Make sure it works on lower loads then ramp up slowly
until it breaks.

The other thing I'd consider is the timing of your requests.
I didn't check but have you a decent gap between requests
or are all 5000 arriving nearly simultaneously? It may
be a network configuration issue - the socket/port queue
simply getting choked to the point where packets time out.

--
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] reading lines from a list of files

2015-05-11 Thread Alex Kleider

Is there a better (more 'Pythonic') way to do the following?

for f_name in f_names:
with open(f_name, 'r') as f:
for line in f:

As always, thank you, tutors, for all you are doing.

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


Re: [Tutor] reading lines from a list of files

2015-05-11 Thread Peter Otten
Alex Kleider wrote:

> Is there a better (more 'Pythonic') way to do the following?
> 
>  for f_name in f_names:
>  with open(f_name, 'r') as f:
>  for line in f:

There's the fileinput module



but personally I prefer the way you show above.

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