[Tutor] Need a little help in formatting print statement

2014-12-07 Thread Anubhav Yadav
I am trying to solve the CS1 Python
http://cse.msu.edu/~cse231/PracticeOfComputingUsingPython/ Exercises.

The first exercise calculates the energy released from an earthquake based
on it's ritcher scale.

I wrote the code. Here http://pastebin.com/nXjxCZWJ is the code.

I am supposed to get this http://i.imgur.com/gi5syL7.png output:

If I use the %f formatter, I get this output:

Ritcher ScaleEnergyTNT

1.01995262.3149690.0005

5.01995262314968.882812476.8791

9.12818382931264449024.00673609687.2047

9.23981071705534952960.00951498973.5982

9.511220184543019653120.002681688466.3049

and If I use the %e or %g format specifier I get this:

Ritcher ScaleEnergyTNT

1.01.99526e+060.0005

5.01.99526e+12476.8791

9.12.81838e+18673609687.2047

9.23.98107e+18951498973.5982

9.51.12202e+192681688466.3049

In the sample output given by the exercise, the first two values do not use
the scientific notation. So either they are not using a loop to print the
table, or there is a format specifier that prints the floating value in
scientific notation only if some condition is checked.

I know there is a String function called format with which I can achieve
great string formatting but I want to stick with the old C ways of
formatting for now.

Any suggestions would be greatly appreciated.

-- 
Regards,
Anubhav Yadav
KPIT Technologies,
Pune.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need a little help in formatting print statement

2014-12-07 Thread Anubhav Yadav
 If your code is short enough, say, less than 50 lines, please include it
 directly in your email. Some of us are reading and replying to posts at
 a time where it is inconvenient to access the web, or at least parts of
 the web.

 Yes sure, next time I will keep that in mind.


 Try using the %r formatter:

 py for x in (1995262.314969, 1995262314968.882812,
 ...   2818382931264449024.0, 11220184543019653120.0):
 ... print( %r % x )  # same as print(repr(x))
 ...
 1995262.314969
 1995262314968.8828
 2.818382931264449e+18
 1.1220184543019653e+19


I had read about this formatter before, thanks a lot. Cheers!!


-- 
Regards,
Anubhav Yadav
KPIT Technologies,
Pune.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Does anyone here has the problems of CS231 saved?

2015-02-15 Thread Anubhav Yadav
I used to solve the programming problems of the CS231 course of the
Michigan State University, they had many python programming problems listed
on a website [1].

Now suddenly the website seems to be down and I was wondering if anyone has
a local copy of this problems with them and is willing to share with me?

There were about 10-12 good problems each on topics on the following
concepts:
1) First Steps
2) Control statements
3) Working with strings
4) Functions
5) List and tuples
6) Dictionaries and sets
7) Classes and Class Designs

And the problems were nicely mixed with concepts from gui programming, game
programming, they used modules like turtle graphics etc. There even used to
concepts from natural language processing or information retrieval.

So if someone does have a copy of the all the examples, please share it
with me. There used to be a pdf for each problem statement clearly
explaining what needs to be done with the examples. So I wonder if someone
has saved all the pdf's it would be really helpful. There is also a cached
version of the website [2]. But I am not able to access the problem links
in the cached contents.

Worst case if no one has a copy to all the examples, can someone suggest me
a different website with python programming assignments for the same
purpose?

Thank you.

[1] http://www.cse.msu.edu/~cse231/PracticeOfComputingUsingPython/
[2]
http://webcache.googleusercontent.com/search?q=cache:http://www.cse.msu.edu/~cse231/PracticeOfComputingUsingPython/
-- 
Regards,
Anubhav Yadav
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Does anyone here has the problems of CS231 saved?

2015-02-15 Thread Anubhav Yadav

 Looks like the Internet Archive has a copy from 2013-Jun-23:

 
 https://web.archive.org/web/*/http://www.cse.msu.edu/~cse231/PracticeOfComputingUsingPython/
 



Thanks a lot. That helps a lot. Although the problems are very less in
comparison to the ones that were latest, I think I can do with it.
Thanks again.



-- 
Regards,
Anubhav Yadav
KPIT Technologies,
Pune.
___
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-12 Thread Anubhav Yadav
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...


I did implemented a small server which could only serve one client at a
time. In fact
after realizing that it is able to serve a single client at a moment, I
decided to go with multithreaded
approach. I just didn't thought of including that single threaded server
code here.


 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.


I agree, but since I am trying to learn, I think I should limit my clients
to 100 rather than 1000 or 5000
per machine? Getting a server of that capacity is difficult for me.


 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.


I can relate, I wrote this mail at 2 am in the night and immediately hit
the bed. :)


 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.


I have a time.sleep(1) before sending each requests, from each client.



-- 
Regards,
Anubhav Yadav
KPIT Technologies,
Pune.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Implementing a Media Streaming Server in Python

2015-05-18 Thread Anubhav Yadav
I was looking to implement a media streaming server in python, which can
stream movies to mobile based clients efficiently to multiple clients (over
50 clients )

Should I just use plain sockets to create connection or should I go with a
framework like twisted/flask?

I want to know what would be the best way to design the video/media
streaming server?

Thank you.

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


[Tutor] Need some help with setuptools for my project.

2015-04-06 Thread Anubhav Yadav
Hi,

I am new to python and still trying to get my concepts clear with respect
to standard practices in python. I wrote a script which fetches the latest
cricket scores from the internet and sends desktop notifications using
pynotify. The project, initially supported both python2 as well as python3,
but I realized it is very difficult to keep up with the errors. So I
migrated the project to python2.

Now I wanted to make my script robust such that it could be installed
easily by the users. I broke up my script in logical modules and added them
all under root directory with the name of the project.

Here is how the source tree looks now:

|-- LICENSE
|-- README.md
|-- requirements.txt
|-- scorer
|   |-- app.py
|   |-- fetch_scores.py
|   |-- __init__.py
|   |-- __main__.py
|   |-- notification.py
|   |-- system.py
|   `-- ui.py
`-- setup.py

Where scorer is the name of the project. Here are the contents of the
requirements.txt file:

scorer
requests==2.6.0
beautifulsoup4==4.3.2

And here is my setup.py

from setuptools import setup

def readme():
with open('README.md') as f:
return f.read()

setup(
name = 'scorer',
version = 0.1,
description = 'A simple script to show desktop notifications for
cricket scores',
long_description = readme(),
url = 'https://github.com/neo1691/scorer.py',
author = 'Anubhav Yadav',
author_email = 'anubhav1...@gmail.com',
license = 'GPLv2',
packages = ['scorer'],
install_requires=[
  'requests',
  'beautifulsoup4',
  ],
zip_safe = False
)

When I run ``python setup.py build``, It gives me the following output:

running build
running build_py
creating build
creating build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/scorer
copying scorer/ui.py - build/lib.linux-x86_64-2.7/scorer
copying scorer/__init__.py - build/lib.linux-x86_64-2.7/scorer
copying scorer/system.py - build/lib.linux-x86_64-2.7/scorer
copying scorer/fetch_scores.py - build/lib.linux-x86_64-2.7/scorer
copying scorer/__main__.py - build/lib.linux-x86_64-2.7/scorer
copying scorer/app.py - build/lib.linux-x86_64-2.7/scorer
copying scorer/notification.py - build/lib.linux-x86_64-2.7/scorer

It creates a build directory with the above files, and I have no idea what
to do with them. How to use it? Or how to test my app without installing
it.

If I then run ``sudo python setup.py install``, it gives me a lot of output
but one of the lines is:
Installed /usr/lib/python2.7/site-packages/scorer-0.1-py2.7.egg

A few more directories are created in my project directory, build, dist,
scorer.egg-info etc. Yet I still have no idea how to run my app.

I can run my app from the root of the project directory using ``python -m
scorer`` since I have called my main() function using the __main__.py file.

Can someone help me figure out how can I package my project according to
setuptools format? Also can someone tell me how to un-install the project
from systemwide after running ``sudo python setup.py install``?

Any help would be greatly appreciated. I apologize if my question does not
belongs to this mailing list. My project is hosted on github[1].

Thank you.

[1] https://github.com/neo1691/scorer.py

-- 
Regards,
Anubhav Yadav
KPIT Technologies,
Pune.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need some help with setuptools for my project.

2015-04-07 Thread Anubhav Yadav
You might consider packaging your project as a script so that it can be run
 by the user from the command line. See:
 https://docs.python.org/2/distutils/setupscript.html#installing-scripts

 Provided that you add something like #!/usr/bin/python to the top of
 scorer.py, 'python setup.py install’ will make it executable and move it to
 /usr/local/bin (on mac anyway) so that it can be run from the command line
 without the need to be in a specific directory or use the python command.
 You can even drop the .py from the file name so the user would just type in
 ‘scorer’ to start the script.


Hi, as you can see my project doesn't have any single script. It is now
made of many modules, and the app.py imports all the modules and executes
them in the main() function. I also have an __main__.py where I call the
main() function. I can run my project from the root of the directory using
``python -m scorer``. But if I use setuptools and install python using
``sudo python setup.py install``, setuptools says that my script is
installed but I don't know how to run it?



 If you’d like to make your script available to the wider community, you
 can put it on PyPI. See:
 https://docs.python.org/2/distutils/packageindex.html


I would love to upload my script to PyPi, but first I need to figure out if
my project is getting installed properly using setuptools or not.


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


Re: [Tutor] Need some help with setuptools for my project.

2015-04-07 Thread Anubhav Yadav
I apologise, my method was for distutils, not setuptools. I get the two
 muddled. The mechanism for creating a console script like you describe with
 setuptools is described here[1]. The post you linked also has a section on
 it under the heading ‘Executable scripts’. It allows for the multiple file
 approach you have.

 This is it, here's what I did:

I edited my setup.py and added the following:
  entry_points={
'console_scripts':[
'scorer = scorer.app.main'
]
},

As the main function is in scorer/app.py.

Now after running sudo python setup.py install, I can see that there is a
scorer binary installed in my system, but when I run that, I get the
following error.

Traceback (most recent call last):
  File /usr/bin/scorer, line 9, in module
load_entry_point('scorer==0.1', 'console_scripts', 'scorer')()
  File /usr/lib/python2.7/site-packages/pkg_resources/__init__.py, line
546, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
  File /usr/lib/python2.7/site-packages/pkg_resources/__init__.py, line
2666, in load_entry_point
return ep.load()
  File /usr/lib/python2.7/site-packages/pkg_resources/__init__.py, line
2339, in load
return self.resolve()
  File /usr/lib/python2.7/site-packages/pkg_resources/__init__.py, line
2345, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
ImportError: No module named main

I am this close I guess!

As an aside, I would also suggest that you look at PEP8, the Python style
 guide[3]. Your code should really conform to it as much as possible.


And thanks a lot for giving me the PEP8 link, it will be helpful.


 [1]
 https://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation
 [2] https://www.python.org/dev/peps/pep-0008/


 —

 Dylan Evans




-- 
Regards,
Anubhav Yadav
KPIT Technologies,
Pune.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need some help with setuptools for my project.

2015-04-07 Thread Anubhav Yadav

 On your GitHub repository, you have a single file, scorer.py. I think that
 this is a better approach in this instance than the multiple file approach
 you have now taken (see below).


I am sorry I linked the master repository. See the structureCode branch[1].


  setuptools says that my script is installed but I don't know how to run
 it?

 setuptools will have installed your module to site-packages so that other
 developers can import it to use in writing their own code. However, your
 code doesn’t provide anything for developers e.g. new functions, classes
 etc. so this isn’t really want you want. Rather, your code should be run by
 the end user, the cricket fan. From the first link I gave:

 “Python modules...are usually not run by themselves but imported by
 scripts. Scripts are files containing Python source code, intended to be
 started from the command line

 The latter sounds much more like your situation, so I recommend you do it
 that way. Scripts like this should be single files, hence my recommendation
 to use one file.

 Hope that helps,


This certainly helps, if you see the new link that I have given this time,
you will notice that I have an app.py in my project dir, and it has the
main() function. I have called the main function in __main__.py. This[2]
blog post tells me that you can run your app using ``python -m scorer``
from the root directory of the project, and indeed I am able to run my
scorer app like this.

Now my question is  how can I use setup tools so that users can just run
scorer from the command line and the my script(which was installed before
by setuptools) will start?

[1] https://github.com/neo1691/scorer.py/tree/structureCode
[2] http://blog.habnab.it/blog/2013/07/21/python-packages-and-you/

-- 
Regards,
Anubhav Yadav
KPIT Technologies,
Pune.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need some help with setuptools for my project.

2015-04-08 Thread Anubhav Yadav
Note the : before main, not ..


That's it, that fixed the problem. Now I have a scorer binary. Thanks. A
couple of more questions!

1) If I have install_requires in setup.py, then do I need requirements.txt?
2) Can I run ``python setup.py install`` or ``python setup.py develop``
with root privileges? Just to test the app first?



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


[Tutor] Help understanding list comprehensions

2015-06-16 Thread Anubhav Yadav
I have a doubt.

I had a list like this

[['Varun', 19.0], ['Kakunami', 19.0], ['Harsh', 20.0], ['Beria', 20.0],
['Vikas', 21.0]]

I am using a for loop to del the rows with the lowest value like this:

for row in marks:
if row[1] == lowest:
marks.remove(row)

But after running this loop, I get the following:

[['Kakunami', 19.0], ['Harsh', 20.0], ['Beria', 20.0], ['Vikas', 21.0]]

If I print row for every iteration one row is missed in the iteration. Here
is the output.

['Varun', 19.0]
['Harsh', 20.0]
['Beria', 20.0]
['Vikas', 21.0]

If I use list comprehensions, I get the right output. I just want to
understand what is happening. Can anyone help me out here?

This if from an hackerrank assignment.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help understanding list comprehensions

2015-06-16 Thread Anubhav Yadav
I am sorry I didn't posted my code. I had solved this question long back on
hackerrank, and I couldn't figure out why this behaviour was seen in this
problem, so I had posted this question on their discussion forum. Couldn't
get a good answer so I decided to post here. Luckily I found the revision
history of my submission there where I could find the earlier code which
was giving this error.

Here is the code.

marks = [['Varun', 19.0], ['Kakunami', 19.0], ['Harsh', 20.0], ['Beria',
20.0], ['Vikas', 21.0]]
marks = sorted(marks, key=lambda score:score[1])
lowest = marks[0][1]

for row in marks:
if row[1] == lowest:
marks.remove(row)

second_lowest = marks[0][1]

sh = []
for row in marks:
if second_lowest == row[1]:
sh.append(row)

for row in sorted(sh):
print row[0]
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help understanding list comprehensions

2015-06-18 Thread Anubhav Yadav
So I need to create a copy of the list before trying to iterate through the
list.
Thanks a lot everyone for their answers.

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


Re: [Tutor] Help understanding list comprehensions

2015-06-17 Thread Anubhav Yadav
Either the subject is misleading or you misunderstand something. Im am
sorry to tell you the great truth, but there was no list comprehension  in
your code at all, just a list. Comprehension is what Alan wrote for you,
that is the next step in studying Python, when you already understand lists
and loops well.


I understand that my title was a little misleading, and I apologise for the
same. Here is code that worked:

marks = []
for i in range(int(input())):
name = raw_input()
score = float(raw_input())
marks.append([name, score])
marks = sorted(marks, key=lambda score:score[1])
lowest = marks[0][1]
marks = [ x for x in marks if x[1] != lowest]
second_lowest = marks[0][1]
lowest = sorted([x for x in marks if x[1]==second_lowest])
for row in lowest:
print row[0]

And it worked because I was using list comprehensions for removing the
element from the list. It didn't worked in the for loop before. I wanted to
ask why it didn't worked in for loop but worked in list comprehension.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Need help with two similar test cases that I have written. One works and the other fails

2016-02-06 Thread Anubhav Yadav
Hello Everyone,

I am trying to write a simple program that generated random numbers based
on some rules.
when my program is in the yellow state, it should generate the numbers
from 100.0-100.5
and 102.5-103.0. When my program is in the red state, it should generate
numbers in the range
less than 99.9 and greater than 103.1.

I have written two very simple functions, and two unittest.TestCase test
cases to test the two functions. I somehow feel that both my test cases
doesn't make any sense, yet one of it passes perfectly and other one does
not pass.

Here is my code:

import random
import unittest

red_temperature_min = 99.9
red_temperature_max = 103.0
normal_temperature_min = 101.0
normal_temperature_max = 102.0
yellow_temperature_min = 100.0
yellow_temperature_max = 103.0

def red_temperature_simulation():
"""
Method to simulate the red temperature condition
"""
choice = random.randint(0,1)
if choice:
return round(random.uniform(red_temperature_min - 5.0,
red_temperature_min))
else:
return round(random.uniform(red_temperature_max,
red_temperature_max + 5.0))

def yellow_temperature_simulation():
"""
Method to simulate the yellow temperature condition
"""
choice = random.randint(0,1)
if choice:
return round(random.uniform(yellow_temperature_min,
normal_temperature_min - 0.5), 2)
else:
return round(random.uniform(normal_temperature_max + 0.5,
yellow_temperature_max), 2)


class TestCase(unittest.TestCase):
def test_red_temperature_simulation(self):
"""
Method to test the red_temperature_simulation method
"""
for i in range(10):
self.assertLess(red_temperature_simulation(), 100.0) and \
self.assertGreater(red_temperature_simulation(), 103.0)

def test_yellow_temperature_simulation(self):
"""
Method to test the yellow_temperature_simulation method
"""
for i in range(100):
(self.assertGreaterEqual(yellow_temperature_simulation(),
 100.0)) and \
(self.assertLessEqual(yellow_temperature_simulation(),
  100.5)) and \
(self.assertGreaterEqual(yellow_temperature_simulation(),
 102.5)) and \
(self.assertLessEqual(yellow_temperature_simulation(), 103.0))



I try to test if a number 99.7 is less than 100.0 and at the same time I
also check if it's greater than 102.5. The sentence itself doesn't make
sense, but somehow the test case for yellow passes, but the test case for
red fails. I have also tried using and instead of or!

I want to test my code, is there a better way of doing it?

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


Re: [Tutor] Need help with two similar test cases that I have written. One works and the other fails

2016-02-07 Thread Anubhav Yadav
> Hi Anubhav,
>
> Ah!  The assert functions are meant to be used as statements, not as
> composable expressions.  If you're familiar with the idea of side effects,
> then you need to understand that you should be calling the assert functions
> just for their side effects, not for their return value.
>
> If you want to test two conditions, do them as  separate statements.  By
> trying to compose them with 'and', the code actually means something
> different due to boolean logic short circuiting.
>
> If you have questions, please feel free to ask.  Good luck!
>

Hi,

Thanks a lot for replying back. I should do something like this right?
truth_value = yellow_temperature_simulation() <= 100.5 and
yellow_temperature_simulation() >= 100.0
self.assertTrue(truth_value)

I just want to confirm if I get the concept right?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help with two similar test cases that I have written. One works and the other fails

2016-02-07 Thread Anubhav Yadav
> > when my program is in the yellow state, it should generate the numbers
> > from 100.0-100.5
> > and 102.5-103.0
>
> The "and" is a bit misleading as you want to allow values that are in the
> first *or* the second intrval. When you spell that in Python it becomes
>
> temperature = yellow_temperature_simulation()
> self.assertTrue(
> (100.0 <= temperature < 100.5)
> or (102.5 <= temperature < 103.0))
>
>
I agree, That's the mistake I made. I fixed it as follows:

def test_red_temperature_simulation(self):
"""
Method to test the red_temperature_simulation method
"""
for i in range(100):
test_value = red_temperature_simulation()
self.assertTrue((test_value < 100.0) or (test_value > 103.0),
msg="Test failed for {}".format(test_value))

And now everything works the way it should.

Thanks for all your help :)

-- 
Regards,
Anubhav Yadav
KPIT Technologies,
Pune.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help with two similar test cases that I have written. One works and the other fails

2016-02-07 Thread Anubhav Yadav
> > Be careful.  Now you have a test that always succeeds.
>
> No, I'm wrong.  Sorry about that: I misread the numerical range.
>

This is the first time I am ever writing unit tests, and I am glad I am
doing it.

Thanks a lot for all your help!.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Need advice on testing python code.

2017-08-27 Thread Anubhav Yadav
Hello.

I am a python developer and I write a lot of python code everyday. I try to do 
as much unit testing as possible. But I want to be better at it, I want to 
write more test cases, specially that rely on database insertions and reads and 
file io. Here are my use-cases for testing. 
How to test if things are going into the database properly or not? 
(mysql/mongo). I want to be able to create a test database environment as 
simple as possible. Create and delete the test environment before each 
functional test case is run. 
Sometimes I write code that read some data from some rabbitmq queue and do 
certain things. How can I write end to end functional test that creates a test 
rabbitmq environment (exchanges and queues) -> wait for sometime -> see if the 
intended work has been done -> delete the test environment. 
I want to be able to make sure that any new commit on my self hosted gitlab 
server should first run all functional test cases first before accepting the 
merge. 
Since we use lot of docker here to deploy modules to productions, I want to 
write functional test cases that test the whole system as a whole and see if 
things are happening the way they are supposed to happen or not. This means 
firing up lot of docker containers, lot of test databases with some data, and 
run all the test cases from an end user point of view. 
Can you suggest me the right python testing frameworks that I should be using? 
Right now I am using unittest to write test cases and manual if/else statements 
to run the functional test cases. 
I try to create rabbitmq queues and bind them to rabbitmq exchanges using the 
pika module. I then run the module using python -m moduleName and then sleep 
for sometime. Then I kill the processs (subprocess) and then I see if the 
intended consequences have happened or not. It's a pain in the ass to be doing 
so many things for test cases. I clearly need to learn how to do things better. 
Any suggestion/book/article/course/video will help me immensely in becoming a 
developer who writes better code with lot of test cases. 

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


Re: [Tutor] Need advice on testing python code.

2017-08-28 Thread Anubhav Yadav
Hi George, 

> And pytest has more than 200 plugins to make it easier to test things. As I 
> remember it has mongo plugin as we well
> 

Thank you for your comments. 

I have been using `py.test` for only two reasons.

1. To run my tests places in the `/tests` directory. 
2. To run them whenever I save any file int he project using `py.test -f` 
(after installing pytest-xdist plugin). 

I will have a look at py.test again and look at examples. This is a great start 
for me. 

I usually work with writing APIs using Flask, which I test using `fixtures` and 
`mock`. I also write a lot of code which interacts with a lot of external 
modules like
`rabbitmq`, `databases` etc. I want to be better at write maybe integration 
tests and functional tests. 

If you have any more advice regarding those please let me know. Any recommended 
reading would help immensely. 

- Anubhav. 




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


Re: [Tutor] Need advice on testing python code.

2017-08-28 Thread Anubhav Yadav

> Here is the compatibility list of plugins:
> http://plugincompat.herokuapp.com/  
> 
> here is the rabbitmq plugin:
> https://pypi.python.org/pypi/pytest-rabbitmq 
> 
> 
> and here is one for databases:
> https://pypi.python.org/pypi/pytest-sqlalchemy/0.1 
> 
> 
> docker
> https://pypi.python.org/pypi/pytest-docker/0.6.0 
> 
> 
> 
> And if you do not have a CI system yet, then the buildbot CI can be good for 
> you
> https://buildbot.net/ 
> which is written in Python (and of course you manage it in Python)
> (used by Python, Mozilla, Chrome etc)
> 

This is awesome George. 

Much appreciated. I think `pytest` will be my favourite testing tool in the 
python ecosystem going forward. 

Cheers. 

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