Re: testing code

2018-07-08 Thread Sharan Basappa
On Sunday, 8 July 2018 12:42:07 UTC+5:30, Christian Gollwitzer  wrote:
> Am 08.07.18 um 06:21 schrieb Sharan Basappa:
> > sorry. there was a copy paste error when i posted. I pasted test_2.py for 
> > both the files:
> > 
> > here are the files again. The issue remains.
> 
> > output:
> > %run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py"
> > 30
> > 
> 
> Jim spotted it... '%run' is in IPython, right? "import" statements are 
> executed only once for a given module (that's the purpose) - you have 
> probably modified your imported file, and not restarted the interpreter. 
> The second time you then import it, nothing happens.
> 
> If you restart your interpreter, you should see both lines printed.
> 
>   Christian

yes, exactly. this is the issue although execution is in Canopy and not in 
iPython
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: testing code

2018-07-08 Thread Christian Gollwitzer

Am 08.07.18 um 06:21 schrieb Sharan Basappa:

sorry. there was a copy paste error when i posted. I pasted test_2.py for both 
the files:

here are the files again. The issue remains.



output:
%run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py"
30



Jim spotted it... '%run' is in IPython, right? "import" statements are 
executed only once for a given module (that's the purpose) - you have 
probably modified your imported file, and not restarted the interpreter. 
The second time you then import it, nothing happens.


If you restart your interpreter, you should see both lines printed.

Christian

--
https://mail.python.org/mailman/listinfo/python-list


Re: testing code

2018-07-08 Thread Sharan Basappa
On Sunday, 8 July 2018 11:52:39 UTC+5:30, Jim Lee  wrote:
> On 07/07/18 21:21, Sharan Basappa wrote:
> >
> > sorry. there was a copy paste error when i posted. I pasted test_2.py for 
> > both the files:
> >
> > here are the files again. The issue remains.
> > [...]
> >
> > output:
> > %run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py"
> > 30
> 
> [11:24 PM jlee@kerndev ~] $cat test_2.py
> x = 10
> y = 20
> 
> c = x-y
> 
> print c
> 
> def func1():
>      return x+y
> 
> [11:24 PM jlee@kerndev ~] $cat test_2_test.py
> import test_2
> 
> x = test_2.func1()
> print x
> [11:24 PM jlee@kerndev ~] $python test_2_test.py
> -10
> 30
> [11:24 PM jlee@kerndev ~] $
> 
> 
> As you can see, the  code from the import is indeed executed by the 
> python interpreter.
> 
> I'm not familiar with the "%run" prefix in your command - some sort of 
> windows-ism?
> 
> -Jim

i think I figured out the issue. I am running this in Canopy. When I use Canopy 
Gui to run this, it appears that it loads the imported file only once. So, when 
I re-start canopy then everything goes fine the first time. When I use command 
prompt then it works all well.

PS: run is nothing but the front end command that Canopy uses to run Python in 
the background.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: testing code

2018-07-08 Thread Jim Lee



On 07/07/18 21:21, Sharan Basappa wrote:


sorry. there was a copy paste error when i posted. I pasted test_2.py for both 
the files:

here are the files again. The issue remains.
[...]

output:
%run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py"
30


[11:24 PM jlee@kerndev ~] $cat test_2.py
x = 10
y = 20

c = x-y

print c

def func1():
    return x+y

[11:24 PM jlee@kerndev ~] $cat test_2_test.py
import test_2

x = test_2.func1()
print x
[11:24 PM jlee@kerndev ~] $python test_2_test.py
-10
30
[11:24 PM jlee@kerndev ~] $


As you can see, the  code from the import is indeed executed by the 
python interpreter.


I'm not familiar with the "%run" prefix in your command - some sort of 
windows-ism?


-Jim


--
https://mail.python.org/mailman/listinfo/python-list


Re: testing code

2018-07-07 Thread Sharan Basappa
On Saturday, 7 July 2018 18:22:23 UTC+5:30, Chris Angelico  wrote:
> On Sat, Jul 7, 2018 at 10:02 PM, Sharan Basappa
>  wrote:
> > On Friday, 6 July 2018 09:22:31 UTC+5:30, Chris Angelico  wrote:
> >> On Fri, Jul 6, 2018 at 12:56 PM, Sharan Basappa
> >>  wrote:
> >> > Please let me know if the following understanding of mine is correct.
> >> > I need to put the program code in a separate file and organize every 
> >> > executable code in some form of function. If any code exists outside of 
> >> > function then it is not executable by importing.
> >> >
> >>
> >> Kinda. It's actually the other way around: if any code exists outside
> >> of functions, it will be executed immediately when you import. So
> >> you're correct in that it would be hard (maybe impossible) to
> >> unit-test that; and yes, the normal way to do it is to put all your
> >> important code into functions.
> >>
> >> ChrisA
> >
> > Chris,
> >
> > Things do work as per expected with one exception.
> > You mentioned that as soon as a file is imported, it executes immediately.
> >
> > Please see the example below:
> >
> > file: test_2.py
> >
> > x = 10
> > y = 20
> >
> > c = x-y
> >
> > print c
> >
> > def func1():
> > return x+y
> >
> > test_2_test.py
> >
> > x = 10
> > y = 20
> >
> > c = x-y
> >
> > print c
> >
> > def func1():
> > return x+y
> >
> > this is the output:
> > %run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py"
> > 30
> >
> > As you can see, print c in test_2 file was not executed upon import as 
> > there is no corresponding output
> 
> I don't think you import your other module anywhere.
> 
> ChrisA

sorry. there was a copy paste error when i posted. I pasted test_2.py for both 
the files:

here are the files again. The issue remains.

test_2.py:
x = 10
y = 20

c = x-y

print c

def func1():
return x+y 

test_2_test.py:
import test_2

x = test_2.func1()
print x

output:
%run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py"
30
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: testing code

2018-07-07 Thread Chris Angelico
On Sat, Jul 7, 2018 at 10:02 PM, Sharan Basappa
 wrote:
> On Friday, 6 July 2018 09:22:31 UTC+5:30, Chris Angelico  wrote:
>> On Fri, Jul 6, 2018 at 12:56 PM, Sharan Basappa
>>  wrote:
>> > Please let me know if the following understanding of mine is correct.
>> > I need to put the program code in a separate file and organize every 
>> > executable code in some form of function. If any code exists outside of 
>> > function then it is not executable by importing.
>> >
>>
>> Kinda. It's actually the other way around: if any code exists outside
>> of functions, it will be executed immediately when you import. So
>> you're correct in that it would be hard (maybe impossible) to
>> unit-test that; and yes, the normal way to do it is to put all your
>> important code into functions.
>>
>> ChrisA
>
> Chris,
>
> Things do work as per expected with one exception.
> You mentioned that as soon as a file is imported, it executes immediately.
>
> Please see the example below:
>
> file: test_2.py
>
> x = 10
> y = 20
>
> c = x-y
>
> print c
>
> def func1():
> return x+y
>
> test_2_test.py
>
> x = 10
> y = 20
>
> c = x-y
>
> print c
>
> def func1():
> return x+y
>
> this is the output:
> %run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py"
> 30
>
> As you can see, print c in test_2 file was not executed upon import as there 
> is no corresponding output

I don't think you import your other module anywhere.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: testing code

2018-07-07 Thread Sharan Basappa
On Friday, 6 July 2018 09:22:31 UTC+5:30, Chris Angelico  wrote:
> On Fri, Jul 6, 2018 at 12:56 PM, Sharan Basappa
>  wrote:
> > Please let me know if the following understanding of mine is correct.
> > I need to put the program code in a separate file and organize every 
> > executable code in some form of function. If any code exists outside of 
> > function then it is not executable by importing.
> >
> 
> Kinda. It's actually the other way around: if any code exists outside
> of functions, it will be executed immediately when you import. So
> you're correct in that it would be hard (maybe impossible) to
> unit-test that; and yes, the normal way to do it is to put all your
> important code into functions.
> 
> ChrisA

Chris,

Things do work as per expected with one exception.
You mentioned that as soon as a file is imported, it executes immediately.

Please see the example below:

file: test_2.py

x = 10
y = 20

c = x-y

print c

def func1():
return x+y  

test_2_test.py

x = 10
y = 20

c = x-y

print c

def func1():
return x+y  

this is the output:
%run "D:/Projects/Initiatives/machine learning/programs/test_2_test.py"
30

As you can see, print c in test_2 file was not executed upon import as there is 
no corresponding output
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: testing code

2018-07-06 Thread Sharan Basappa
On Friday, 6 July 2018 09:22:31 UTC+5:30, Chris Angelico  wrote:
> On Fri, Jul 6, 2018 at 12:56 PM, Sharan Basappa
>  wrote:
> > Please let me know if the following understanding of mine is correct.
> > I need to put the program code in a separate file and organize every 
> > executable code in some form of function. If any code exists outside of 
> > function then it is not executable by importing.
> >
> 
> Kinda. It's actually the other way around: if any code exists outside
> of functions, it will be executed immediately when you import. So
> you're correct in that it would be hard (maybe impossible) to
> unit-test that; and yes, the normal way to do it is to put all your
> important code into functions.
> 
> ChrisA

thanks a lot
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: testing code

2018-07-06 Thread Sharan Basappa
On Friday, 6 July 2018 09:32:08 UTC+5:30, Cameron Simpson  wrote:
> On 05Jul2018 19:56, Sharan Basappa  wrote:
> >I have implemented my first program in python that uses ML to do some 
> >classification task. The whole code is in a single file currently.
> >It contains executable code as well as functions.
> 
> I presume when you write "executable code" you mean some kind of "main 
> program" 
> that just runs when you run the .py file?
> 
> >At the end of the program, I have series of calls that is used to test my 
> >code.
> >Now, I would like to re-structure this to separate test code from the 
> >program.
> >As I have not done this in Python, I am a bit lost.
> >
> >Please let me know if the following understanding of mine is correct.
> >I need to put the program code in a separate file and organize every 
> >executable code in some form of function. If any code exists outside of 
> >function then it is not executable by importing.
> 
> This is not quite right. Because Python is a dynamic language, importing a 
> file 
> actually runs it. That is how the functions etc get defined.
> 
> So what you need to do is arrange that your "series of calls that is used to 
> test my code" live in their own function, and that that function only gets 
> run 
> if the file is directly run.
> 
> Fortunately, this is a very common, almost universal, requirement and there 
> is 
> a standard idom for arranging it.
> 
> Support you have your code in the file "foo.py" (because I need a concrete 
> filename for the example). It might look like this at present:
> 
>   def func1(...):
> 
>   def func2(...):
> 
>   x = func1(...)
>   y = func2(...)
>   print(x + y)
> 
> i.e. some function definitions and then you testing code.
> 
> Now, you can write another file "foo_tests.py" which starts like this:
> 
>   import foo
>   ... run some tests of foo.func1, foo.func2 etc ...
> 
> The issue is that as written, foo.py will run your test calls during the 
> import.  Restructure foo.py like this:
> 
>   def main():
>   x = func1(...)
>   y = func2(...)
>   print(x + y)
> 
>   def func1(...):
> 
>   def func2(...):
> 
>   if __name__ == '__main__':
>   main()
> 
> This is very standard. When you run a python file directly the built in  
> __name__ variable contains the string "__main__". This tells you that you're 
> running it as a "main program" i.e. directly.
> 
> If you import the file instead, as from your planned testing file, the 
> __name__ 
> variable will contain the string "foo" because that is the name of the module.
> 
> So that "main" function and the "if" at the bottom is standard Python 
> boilerplate code for what you're trying to do.
> 
> >Import this in my test program (file/module) and then initiate  calls 
> >present 
> >in the program.
> >If there is some simple example, it would help a lot.
> 
> Now you can do this part.
> 
> Cheers,
> Cameron Simpson 

Cameron.

thanks. this is much more easier than I thought.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: testing code

2018-07-05 Thread Cameron Simpson

On 05Jul2018 19:56, Sharan Basappa  wrote:
I have implemented my first program in python that uses ML to do some 
classification task. The whole code is in a single file currently.

It contains executable code as well as functions.


I presume when you write "executable code" you mean some kind of "main program" 
that just runs when you run the .py file?



At the end of the program, I have series of calls that is used to test my code.
Now, I would like to re-structure this to separate test code from the program.
As I have not done this in Python, I am a bit lost.

Please let me know if the following understanding of mine is correct.
I need to put the program code in a separate file and organize every executable 
code in some form of function. If any code exists outside of function then it 
is not executable by importing.


This is not quite right. Because Python is a dynamic language, importing a file 
actually runs it. That is how the functions etc get defined.


So what you need to do is arrange that your "series of calls that is used to 
test my code" live in their own function, and that that function only gets run 
if the file is directly run.


Fortunately, this is a very common, almost universal, requirement and there is 
a standard idom for arranging it.


Support you have your code in the file "foo.py" (because I need a concrete 
filename for the example). It might look like this at present:


 def func1(...):

 def func2(...):

 x = func1(...)
 y = func2(...)
 print(x + y)

i.e. some function definitions and then you testing code.

Now, you can write another file "foo_tests.py" which starts like this:

 import foo
 ... run some tests of foo.func1, foo.func2 etc ...

The issue is that as written, foo.py will run your test calls during the 
import.  Restructure foo.py like this:


 def main():
 x = func1(...)
 y = func2(...)
 print(x + y)

 def func1(...):

 def func2(...):

 if __name__ == '__main__':
 main()

This is very standard. When you run a python file directly the built in  
__name__ variable contains the string "__main__". This tells you that you're 
running it as a "main program" i.e. directly.


If you import the file instead, as from your planned testing file, the __name__ 
variable will contain the string "foo" because that is the name of the module.


So that "main" function and the "if" at the bottom is standard Python 
boilerplate code for what you're trying to do.


Import this in my test program (file/module) and then initiate  calls present 
in the program.

If there is some simple example, it would help a lot.


Now you can do this part.

Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: testing code

2018-07-05 Thread Chris Angelico
On Fri, Jul 6, 2018 at 12:56 PM, Sharan Basappa
 wrote:
> Please let me know if the following understanding of mine is correct.
> I need to put the program code in a separate file and organize every 
> executable code in some form of function. If any code exists outside of 
> function then it is not executable by importing.
>

Kinda. It's actually the other way around: if any code exists outside
of functions, it will be executed immediately when you import. So
you're correct in that it would be hard (maybe impossible) to
unit-test that; and yes, the normal way to do it is to put all your
important code into functions.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


testing code

2018-07-05 Thread Sharan Basappa
Hi All,

I am new to Python though not new to programming.

I have implemented my first program in python that uses ML to do some 
classification task. The whole code is in a single file currently.
It contains executable code as well as functions.

At the end of the program, I have series of calls that is used to test my code.
Now, I would like to re-structure this to separate test code from the program.
As I have not done this in Python, I am a bit lost.

Please let me know if the following understanding of mine is correct.
I need to put the program code in a separate file and organize every executable 
code in some form of function. If any code exists outside of function then it 
is not executable by importing.

Import this in my test program (file/module) and then initiate  calls present 
in the program.

If there is some simple example, it would help a lot.
-- 
https://mail.python.org/mailman/listinfo/python-list


[issue33644] Fix signatures of tp_finalize handlers in testing code.

2018-05-26 Thread Serhiy Storchaka

Change by Serhiy Storchaka :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33644] Fix signatures of tp_finalize handlers in testing code.

2018-05-26 Thread Serhiy Storchaka

Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:


New changeset 9ba3be4718bdf82e20280980807e054004a9bade by Serhiy Storchaka 
(Miss Islington (bot)) in branch '3.6':
bpo-33644: Fix signatures of tp_finalize handlers in testing code. (GH-7111) 
(GH-7125)
https://github.com/python/cpython/commit/9ba3be4718bdf82e20280980807e054004a9bade


--

___
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33644>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33644] Fix signatures of tp_finalize handlers in testing code.

2018-05-26 Thread miss-islington

miss-islington <mariatta.wijaya+miss-isling...@gmail.com> added the comment:


New changeset 14d289be60a2ad4cb65990a63ed2e75e9a8cb3ec by Miss Islington (bot) 
in branch '3.7':
bpo-33644: Fix signatures of tp_finalize handlers in testing code. (GH-7111)
https://github.com/python/cpython/commit/14d289be60a2ad4cb65990a63ed2e75e9a8cb3ec


--
nosy: +miss-islington

___
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33644>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33644] Fix signatures of tp_finalize handlers in testing code.

2018-05-26 Thread miss-islington

Change by miss-islington :


--
pull_requests: +6760

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33644] Fix signatures of tp_finalize handlers in testing code.

2018-05-26 Thread miss-islington

Change by miss-islington :


--
pull_requests: +6759

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33644] Fix signatures of tp_finalize handlers in testing code.

2018-05-26 Thread Serhiy Storchaka

Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:


New changeset 19de8b3dd742fb53681478ad4fff57ed7c37a953 by Serhiy Storchaka in 
branch 'master':
bpo-33644: Fix signatures of tp_finalize handlers in testing code. (GH-7111)
https://github.com/python/cpython/commit/19de8b3dd742fb53681478ad4fff57ed7c37a953


--

___
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33644>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33644] Fix signatures of tp_finalize handlers in testing code.

2018-05-25 Thread Serhiy Storchaka

Change by Serhiy Storchaka :


--
keywords: +patch
pull_requests: +6750
stage:  -> patch review

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33644] Fix signatures of tp_finalize handlers in testing code.

2018-05-25 Thread Serhiy Storchaka

New submission from Serhiy Storchaka <storchaka+cpyt...@gmail.com>:

tp_finalize handlers in Modules/_testmultiphase.c and Modules/_testmultiphase.c 
return int. This signature is incompatible with the signature of the 
tp_finalize slot. They should not return any value.

--
components: Tests
messages: 317662
nosy: serhiy.storchaka
priority: normal
severity: normal
status: open
title: Fix signatures of tp_finalize handlers in testing code.
type: behavior
versions: Python 3.6, Python 3.7, Python 3.8

___
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33644>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: testing code in python source package

2009-09-21 Thread Ben Finney
Peng Yu pengyu...@gmail.com writes:

 I'm wondering if the development of python is test driven. If it is,
 where in the Python-2.6.2 source directory is the test code for the
 modules in ./Lib?

A great majority of your many questions in this forum are already
answered in the available documentation online. You seem to leap
immediately to asking here rather than demonstrating that you have first
tried and failed to find answers in existing documentation.

You would save yourself a lot of time, not to mention social capital, if
you would invest the effort to learn how to research your answers at
URL:http://docs.python.org/ and URL:http://www.python.org/dev/.

-- 
 \  “Any intelligent fool can make things bigger and more complex… |
  `\It takes a touch of genius – and a lot of courage – to move in |
_o__)the opposite direction.” —Albert Einstein |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


testing code in python source package

2009-09-20 Thread Peng Yu
Hi,

I'm wondering if the development of python is test driven. If it is,
where in the Python-2.6.2 source directory is the test code for the
modules in ./Lib?

Regards,
Peng
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: testing code in python source package

2009-09-20 Thread alex23
Peng Yu pengyu...@gmail.com wrote:
 I'm wondering if the development of python is test driven. If it is,
 where in the Python-2.6.2 source directory is the test code for the
 modules in ./Lib?

Unsurprisingly, they're located in Lib/test. Is it _really_ that
difficult to find?
-- 
http://mail.python.org/mailman/listinfo/python-list


proxy testing code with multi-thread

2008-02-01 Thread [EMAIL PROTECTED]
hi all
I'm now meeting some problem when using threading to test whether or
not a proxy is good or not
and the total number of proxies is just 60
but each time my programme stopped just at approaching the end,
say,
for each proxy I made a proxy
but it just stopped working at maybe 58 or 59 or some other similar
number
below is the code
any advice will be greatly appreciated!
---
import urllib2
import threading
import time
import traceback
import sys

class mythread(threading.Thread):
def __init__(self, agent, i):
self.agent = agent
self.index = i
threading.Thread.__init__(self)

def run(self):
tryagent(self.agent, self.index)

def tryagent(agent, i):
global mutex, bad, goodlist, total
proxy_handler = urllib2.ProxyHandler({'http':'http://' + agent})
opener = urllib2.build_opener(proxy_handler)
try:
response = opener.open('http://www.indeed.com/q-Director-Of-
Finance-jobs.html')
data_web = response.read()
if data_web.find('All director finance job') == -1:
#mutex.acquire( )
bad = bad + 1
total = total + 1
print 'agent %d %s is bad in try' %(i, agent,)
print 'bad number: %d total:%d' %(bad,total)
#mutex.release( )

else:
#mutex.acquire( )
total = total + 1
print 'agent %d %s is good' %(i, agent)
goodlist.append(agent)
#mutex.release( )
except:
   # mutex.release( )
#mutex.acquire( )
traceback.print_exc()
total = total + 1
bad = bad + 1
print 'agent %d %s is bad in except' %(i, agent,)
print 'bad number: %d total: %d' %(bad,total)
#mutex.release( )


agentlist = open('F:\\Python25\\works\\agent.txt',
'r').read().split('\n')
mutex = threading.Lock()
total = 0
bad = 0
goodlist = []
threads = []
print 'total proxy number:%d' %(len(agentlist))

if len(agentlist) = 100:
for i, agent in enumerate(agentlist):
threads.append(mythread(agent, i))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
else:
i = 1
while i * 100  len(agentlist):
i = i + 1
threads = []
for j in range(100):
threads.append(agentlist[j + 100 * i])
for thread in threads:
thread.start()
for thread in threads:
thread.join()
i = i - 1
threads = []
for k in range(len(agentlist) - 100 * i):
threads.append(agentlist[100 * i + k])
for thread in threads:
thread.start()
for thread in threads:
thread.join()


urllib2.socket.setdefaulttimeout(1)
newproxysock = open('F:\\Python25\\works\\proxy.txt', 'w')
saveout = sys.stdout
sys.stdout = newproxysock
for i, proxy in enumerate(goodlist):
print proxy
sys.stdout = saveout
newproxysock.close()

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: testing -- what to do for testing code with behaviour dependant upon which files exist?

2005-04-04 Thread Brian van den Broek

Brian van den Broek [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]

Hi all,
I'm just starting to employ unit testing (I'm using doctest), and I am
uncertain how to handle writing tests where the behaviour being tested is 
dependant on whether certain file paths point to actual files.
Hi all,
I had a busy weekend after posting, so didn't get a chance to 
follow-up until now. Thanks to all who responded.

Python's my first real language, and I am a mere hobbyist, so the 
suggestions for search terms provided by Terry (and implicitly by 
Andre who suggested a mock strategy) are most welcome! Sometimes the 
hardest part of googling is knowing what terms to search for.

Grig suggested the tempfile module; I had rejected that initially, as 
the code I am testing displays an error message containing the bad 
file path, so I thought I needed to know the path name at time of 
coding the tests. But, I've since learned that doctest can be made to 
consider only leading portions of a line's content. So, this might 
well work for me. Thanks for reminding me of the approach.

Jeremy suggested using a directory name akin to 
C:\onlyanidiotwouldhavethisdirecotrynameonadrive. That is what I had 
settled on before I posted. Somehow it feels unhappy and inelegant. 
But, I'm a bit less uncomfortable with it seeing that others have done 
so, too.

Anyway, thanks again for the suggestions :-)  Best to all,
Brian vdB
--
http://mail.python.org/mailman/listinfo/python-list


Re: testing -- what to do for testing code with behaviour dependant upon which files exist?

2005-04-04 Thread Jeremy Bowers
On Mon, 04 Apr 2005 17:02:20 -0400, Brian van den Broek wrote:
 Jeremy suggested using a directory name akin to 
 C:\onlyanidiotwouldhavethisdirecotrynameonadrive. That is what I had 
 settled on before I posted. Somehow it feels unhappy and inelegant. 
 But, I'm a bit less uncomfortable with it seeing that others have done 
 so, too.

To be clear, I would actually suggest
onlyanidiotwouldhavethisdirecotrynameonadrive... note the lack of C:\,
which would be platform specific, as would any other root specification.
Take advantage of the fact that every system I know of makes relative
directories easy, and also note you can work out what directory the
current file is in with a combination of __file__ and os.getcwd() (and
that while that won't work if someone changes the working directory,
that's bad form and IIRC breaks some other things in Python as well, so
don't do that). 

Making it a relative directory may make it look just as bad, but it is in
some sense somewhat less inelegant; at that point, if someone is creating
that directory in the test directory of your app, they're just fooling
with you, and you don't really have to worry about people who maliciously
make your unit tests fail under most circumstances... :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: testing -- what to do for testing code with behaviour dependant upon which files exist?

2005-04-04 Thread Brian van den Broek
Jeremy Bowers said unto the world upon 2005-04-04 17:26:
On Mon, 04 Apr 2005 17:02:20 -0400, Brian van den Broek wrote:
Jeremy suggested using a directory name akin to 
C:\onlyanidiotwouldhavethisdirecotrynameonadrive. That is what I had 
settled on before I posted. Somehow it feels unhappy and inelegant. 
But, I'm a bit less uncomfortable with it seeing that others have done 
so, too.

To be clear, I would actually suggest
onlyanidiotwouldhavethisdirecotrynameonadrive... note the lack of C:\,
Quite so. My apologies for mis-characterizing your suggestion. And 
thanks for the additional information that mis-characterization 
provoked :-)

Best,
Brian vdB
which would be platform specific, as would any other root specification.
Take advantage of the fact that every system I know of makes relative
directories easy, and also note you can work out what directory the
current file is in with a combination of __file__ and os.getcwd() (and
that while that won't work if someone changes the working directory,
that's bad form and IIRC breaks some other things in Python as well, so
don't do that). 

Making it a relative directory may make it look just as bad, but it is in
some sense somewhat less inelegant; at that point, if someone is creating
that directory in the test directory of your app, they're just fooling
with you, and you don't really have to worry about people who maliciously
make your unit tests fail under most circumstances... :-)

--
http://mail.python.org/mailman/listinfo/python-list


testing -- what to do for testing code with behaviour dependant upon which files exist?

2005-04-02 Thread Brian van den Broek
Hi all,
I'm just starting to employ unit testing (I'm using doctest), and I am
uncertain how to handle writing tests where the behaviour being tested 
is dependant on whether certain file paths point to actual files.

I have a class which takes, in its __init__, a list of file paths to
process. The class has a method to validate that the paths passed in
are appropriate ones for the class. One portion of the validation code
ensures that the passed in file paths actually exist.
The relevant part of the validation method code looks like:
# self.universe_files is a list of file paths
non_existent_files = [ x for x in self.universe_files if
   not os.path.isfile(x) ]
if non_existent_files:
raise Files_dont_existError, non_existent_files
where Files_dont_existError is a custom exception with an informative
error message nicely printing a listing of all the file paths which
didn't point to extant files.
I can test the custom error class just fine, but I don't see how to
test the validation method itself.
My problem is that I want to test how it behaves both when it is sent 
only existing file paths, and when it is sent at least 1 non-existent 
one. But it seems like that in order to test that, I have to know in 
advance details of what files exist on the system running the tests. 
And, it won't do to pick file paths with the right properties with 
respect to my computer, as I am writing this code to share with 
someone else, and so I'd have to know details of his file system, too. 
(If it matters, I am using Python 2.4.1 on windows, and I am sending 
the code to someone running Python 2.2 on Linux.)

So, how does one handle such cases with tests?
Thanks for any suggestions. Best,
Brian vdB
--
http://mail.python.org/mailman/listinfo/python-list


Re: testing -- what to do for testing code with behaviour dependant upon which files exist?

2005-04-02 Thread Andr Malo
* Brian van den Broek wrote:

 The relevant part of the validation method code looks like:
 
  # self.universe_files is a list of file paths
  non_existent_files = [ x for x in self.universe_files if
 not os.path.isfile(x) ]
  if non_existent_files:
  raise Files_dont_existError, non_existent_files
 
 I can test the custom error class just fine, but I don't see how to
 test the validation method itself.

The logic is simple -- you don't want to test os.path.isfile, so mock it.
Just encapsulate the os.path.isfile call in an own method, which can be
overridden by your test.

nd
-- 
# Andr Malo, http://pub.perlig.de/ #
--
http://mail.python.org/mailman/listinfo/python-list


Re: testing -- what to do for testing code with behaviour dependant upon which files exist?

2005-04-02 Thread Grig Gheorghiu
Can't you use the tempfile module to generate unique names for
non-existent files and directories? Take a look at
http://www.python.org/doc/lib/module-tempfile.html -- it works on all
supported platforms.

Grig

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: testing -- what to do for testing code with behaviour dependant upon which files exist?

2005-04-02 Thread Jeremy Bowers
On Sat, 02 Apr 2005 15:30:13 -0500, Brian van den Broek wrote:
 So, how does one handle such cases with tests?

When I had a similar situation, I created a directory for testing that was
in a known state, and tested on that. If you can test based on a relative
directory, that should work OK.

Non-existant paths shouldn't be too hard to come up with; hardcoding a
constant relative dir of
THISDIRECTORYCANTPOSSIBLYEXISTANDIFITDOESYOURENUTS ought to do OK.



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: testing -- what to do for testing code with behaviour dependant upon which files exist?

2005-04-02 Thread Terry Reedy

Brian van den Broek [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 Hi all,

 I'm just starting to employ unit testing (I'm using doctest), and I am
 uncertain how to handle writing tests where the behaviour being tested is 
 dependant on whether certain file paths point to actual files.

I would think about writing a dummy 'file' function whose return or raising 
depends on the filename passed in.  Then your test would be independent of 
the actual environment.  For something more elaborate, I would google 
Google's Python newgroup archive for 'dummy filesystem' for past threads on 
the topic.

TJR



-- 
http://mail.python.org/mailman/listinfo/python-list