Re: Directory Web Site

2013-09-23 Thread Chris Angelico
On Mon, Sep 23, 2013 at 11:14 AM,   wrote:
> Was hoping to get some tips or advice on scripting a program that would sort 
> through my many links on my directory website and print out to me the ones 
> that are broken or no longer functioning so that I could fix or remove them 
> from the site. Any help, ideas, advice will be greatly appreciated.
> --
> https://mail.python.org/mailman/listinfo/python-list

The easiest solution is probably a non-Python tool like wget. Search
the web for "link checker" or "href verifier" or words to that effect,
see what you find. This is an extremely common task, and I don't think
you need to write any code for it.

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


Re: Why do I have to use "global" so much when using Turtle?

2013-09-23 Thread John Ladasky
No, Rusi, I have never seen Pynguin before -- and it looks very cool!
-- 
https://mail.python.org/mailman/listinfo/python-list


How To Build Scrapy Environment

2013-09-23 Thread YetToCome
I have already install Twisted, zope.interface, w3lib, libxml2, etc,but it 
still can not be built,here is the error message:

Traceback (most recent call last):
  File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
  File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
  File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 167, in 
execute()
  File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 142, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
  File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 88, in _run_print
_help
func(*a, **kw)
  File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 149, in _run_comm
and
cmd.run(args, opts)
  File "C:\Python27\lib\site-packages\scrapy\commands\crawl.py", line 47, in run
crawler = self.crawler_process.create_crawler()
  File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 142, in create_cr
awler
self.crawlers[name] = Crawler(self.settings)
  File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 23, in __init__
self.spiders = spman_cls.from_crawler(self)
  File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 35, in from
_crawler
sm = cls.from_settings(crawler.settings)
  File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 31, in from
_settings
return cls(settings.getlist('SPIDER_MODULES'))
  File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 22, in __in
it__
for module in walk_modules(name):
  File "C:\Python27\lib\site-packages\scrapy\utils\misc.py", line 66, in walk_mo
dules
submod = __import__(fullpath, {}, {}, [''])
  File "blog_crawl\spiders\dmoz_spider.py", line 1, in 
class DmozSpider(BaseSpider):
NameError: name 'BaseSpider' is not defined

someone tells me why, pls
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How To Build Scrapy Environment

2013-09-23 Thread Peter Otten
YetToCome wrote:

> I have already install Twisted, zope.interface, w3lib, libxml2, etc,but it
> still can not be built,here is the error message:
> 
> Traceback (most recent call last):
>   File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
> "__main__", fname, loader, pkg_name)
>   File "C:\Python27\lib\runpy.py", line 72, in _run_code
> exec code in run_globals
>   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 167, in
>   
> execute()
>   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 142, in
>   execute
> _run_print_help(parser, _run_command, cmd, args, opts)
>   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 88, in
>   _run_print
> _help
> func(*a, **kw)
>   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 149, in
>   _run_comm
> and
> cmd.run(args, opts)
>   File "C:\Python27\lib\site-packages\scrapy\commands\crawl.py", line 47,
>   in run
> crawler = self.crawler_process.create_crawler()
>   File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 142, in
>   create_cr
> awler
> self.crawlers[name] = Crawler(self.settings)
>   File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 23, in
>   __init__
> self.spiders = spman_cls.from_crawler(self)
>   File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 35,
>   in from
> _crawler
> sm = cls.from_settings(crawler.settings)
>   File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 31,
>   in from
> _settings
> return cls(settings.getlist('SPIDER_MODULES'))
>   File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 22,
>   in __in
> it__
> for module in walk_modules(name):
>   File "C:\Python27\lib\site-packages\scrapy\utils\misc.py", line 66, in
>   walk_mo
> dules
> submod = __import__(fullpath, {}, {}, [''])
>   File "blog_crawl\spiders\dmoz_spider.py", line 1, in 
> class DmozSpider(BaseSpider):
> NameError: name 'BaseSpider' is not defined
> 
> someone tells me why, pls

Assuming you are working your way through the tutorial at

http://doc.scrapy.org/en/latest/intro/tutorial.html#our-first-spider

you probably forgot to import the BaseSpider class with

from scrapy.spider import BaseSpider

as shown in the code snippet in the "Our first Spider" section of the 
tutorial.


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


Re: reload and work flow suggestions

2013-09-23 Thread Peter Cacioppi
On Saturday, September 21, 2013 2:43:13 PM UTC-7, Peter Cacioppi wrote:
> This is an idea brought over from another post.
> 
> 
> 
> When I write Python code I generally have 2 or 3 windows open simultaneously.
> 
> 
> 
> 1) An editor for the actual code.
> 
> 2) The interactive interpreter.
> 
> 3) An editor for the unit tests. (Sometimes skipped for quick one-off scripts)
> 
> 
> 
> My work flow tends to involve using 2 to debug the issues that come up with 1 
> and 3. I'll write some new code in 1, play around with it in 2, then solidify 
> the tests in 3. Or a test in 3 fails and I dig around with it using 2.
> 
> 
> 
> My problem is that I tend to use reload() quite a bit. I want to call 
> functions and construct objects that are inside the guts of 1 and pass them 
> arguments that are stored as variables in 2. If I restart my session for 2 I 
> lose these variables (iPython does mitigate the pain here somewhat). Hence, I 
> reload() modules into 2 when they are changed.
> 
> 
> 
> I use ipdb a lot in 2. I usually don't feel comfortable with virgin code or a 
> debug fix that hasn't been stepped through with the debugger.
> 
> 
> 
> Is there something wrong with this work flow? I understand most python 
> experts avoid reload(). So what are they doing that I'm not? I love the 
> ability of Python to quickly let you dive deep into your code and set up a 
> difficult case with 2, it's hard to imagine giving this up, and it's hard to 
> imagine using it without reload(). 
> 
> 
> 
> Thanks for any tips.

Thanks for the tips guys.

One thing re: editors and interactive environments. I'm not a huge emacs fan 
(ducking) and I really like iPython.

In order to avoid calling reload, I've changed my workflow to the following. 

1) The "real" code
2) The unit tests
3) A scratch script

Then, I launch iPython, which can intellisense launch 3 easily. Then I make 
whatever changes I need to 1-3 to make a baby step forward, close iPython, and 
repeat. 

This seems to preserve the easy-to-dive-into, play-around-with-it fun that is 
Python without forcing me to call reload(). I suspect I'm now a bit more close 
to what the "experts" are doing.

Thanks again, I love kibbutzing about work-flow, toys, etc.



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


Re: How To Build Scrapy Environment

2013-09-23 Thread YetToCome
在 2013年9月23日星期一UTC+8下午4时12分21秒,YetToCome写道:
> I have already install Twisted, zope.interface, w3lib, libxml2, etc,but it 
> still can not be built,here is the error message:
> 
> 
> 
> Traceback (most recent call last):
> 
>   File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
> 
> "__main__", fname, loader, pkg_name)
> 
>   File "C:\Python27\lib\runpy.py", line 72, in _run_code
> 
> exec code in run_globals
> 
>   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 167, in 
> 
> 
> execute()
> 
>   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 142, in execute
> 
> _run_print_help(parser, _run_command, cmd, args, opts)
> 
>   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 88, in 
> _run_print
> 
> _help
> 
> func(*a, **kw)
> 
>   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 149, in 
> _run_comm
> 
> and
> 
> cmd.run(args, opts)
> 
>   File "C:\Python27\lib\site-packages\scrapy\commands\crawl.py", line 47, in 
> run
> 
> crawler = self.crawler_process.create_crawler()
> 
>   File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 142, in 
> create_cr
> 
> awler
> 
> self.crawlers[name] = Crawler(self.settings)
> 
>   File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 23, in __init__
> 
> self.spiders = spman_cls.from_crawler(self)
> 
>   File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 35, in 
> from
> 
> _crawler
> 
> sm = cls.from_settings(crawler.settings)
> 
>   File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 31, in 
> from
> 
> _settings
> 
> return cls(settings.getlist('SPIDER_MODULES'))
> 
>   File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 22, in 
> __in
> 
> it__
> 
> for module in walk_modules(name):
> 
>   File "C:\Python27\lib\site-packages\scrapy\utils\misc.py", line 66, in 
> walk_mo
> 
> dules
> 
> submod = __import__(fullpath, {}, {}, [''])
> 
>   File "blog_crawl\spiders\dmoz_spider.py", line 1, in 
> 
> class DmozSpider(BaseSpider):
> 
> NameError: name 'BaseSpider' is not defined
> 
> 
> 
> someone tells me why, pls



在 2013年9月23日星期一UTC+8下午4时12分21秒,YetToCome写道:
> I have already install Twisted, zope.interface, w3lib, libxml2, etc,but it 
> still can not be built,here is the error message:
> 
> 
> 
> Traceback (most recent call last):
> 
>   File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
> 
> "__main__", fname, loader, pkg_name)
> 
>   File "C:\Python27\lib\runpy.py", line 72, in _run_code
> 
> exec code in run_globals
> 
>   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 167, in 
> 
> 
> execute()
> 
>   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 142, in execute
> 
> _run_print_help(parser, _run_command, cmd, args, opts)
> 
>   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 88, in 
> _run_print
> 
> _help
> 
> func(*a, **kw)
> 
>   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 149, in 
> _run_comm
> 
> and
> 
> cmd.run(args, opts)
> 
>   File "C:\Python27\lib\site-packages\scrapy\commands\crawl.py", line 47, in 
> run
> 
> crawler = self.crawler_process.create_crawler()
> 
>   File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 142, in 
> create_cr
> 
> awler
> 
> self.crawlers[name] = Crawler(self.settings)
> 
>   File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 23, in __init__
> 
> self.spiders = spman_cls.from_crawler(self)
> 
>   File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 35, in 
> from
> 
> _crawler
> 
> sm = cls.from_settings(crawler.settings)
> 
>   File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 31, in 
> from
> 
> _settings
> 
> return cls(settings.getlist('SPIDER_MODULES'))
> 
>   File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 22, in 
> __in
> 
> it__
> 
> for module in walk_modules(name):
> 
>   File "C:\Python27\lib\site-packages\scrapy\utils\misc.py", line 66, in 
> walk_mo
> 
> dules
> 
> submod = __import__(fullpath, {}, {}, [''])
> 
>   File "blog_crawl\spiders\dmoz_spider.py", line 1, in 
> 
> class DmozSpider(BaseSpider):
> 
> NameError: name 'BaseSpider' is not defined
> 
> 
> 
> someone tells me why, pls

it had an another error: No module named queuelib, but i have installed all the 
libs mentioned in that passage...

2013-09-23 16:44:17+0800 [scrapy] INFO: Scrapy 0.18.2 started (bot: tutorial)
2013-09-23 16:44:17+0800 [scrapy] DEBUG: Optional features available: ssl, http1
1, libxml2
2013-09-23 16:44:17+0800 [scrapy] DEBUG: Overridden settings: {'NEWSPIDER_MODULE
': 'tutorial.spiders', 'SPIDER_MODULES': ['tutorial.spiders'], 'BOT_NAME': 'tuto
rial'}
2013-09-23 16:44:17+0800 [scrapy] DEBUG: Enabled extensions: LogStats, TelnetCon
sole, CloseSpider, WebService, CoreStats, SpiderState
Traceback (most recent call last):
  File "C:\Python27\lib\runpy.py", line 16

Re: How To Build Scrapy Environment

2013-09-23 Thread YetToCome
在 2013年9月23日星期一UTC+8下午4时37分22秒,Peter Otten写道:
> YetToCome wrote:
> 
> 
> 
> > I have already install Twisted, zope.interface, w3lib, libxml2, etc,but it
> 
> > still can not be built,here is the error message:
> 
> > 
> 
> > Traceback (most recent call last):
> 
> >   File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
> 
> > "__main__", fname, loader, pkg_name)
> 
> >   File "C:\Python27\lib\runpy.py", line 72, in _run_code
> 
> > exec code in run_globals
> 
> >   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 167, in
> 
> >   
> 
> > execute()
> 
> >   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 142, in
> 
> >   execute
> 
> > _run_print_help(parser, _run_command, cmd, args, opts)
> 
> >   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 88, in
> 
> >   _run_print
> 
> > _help
> 
> > func(*a, **kw)
> 
> >   File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 149, in
> 
> >   _run_comm
> 
> > and
> 
> > cmd.run(args, opts)
> 
> >   File "C:\Python27\lib\site-packages\scrapy\commands\crawl.py", line 47,
> 
> >   in run
> 
> > crawler = self.crawler_process.create_crawler()
> 
> >   File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 142, in
> 
> >   create_cr
> 
> > awler
> 
> > self.crawlers[name] = Crawler(self.settings)
> 
> >   File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 23, in
> 
> >   __init__
> 
> > self.spiders = spman_cls.from_crawler(self)
> 
> >   File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 35,
> 
> >   in from
> 
> > _crawler
> 
> > sm = cls.from_settings(crawler.settings)
> 
> >   File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 31,
> 
> >   in from
> 
> > _settings
> 
> > return cls(settings.getlist('SPIDER_MODULES'))
> 
> >   File "C:\Python27\lib\site-packages\scrapy\spidermanager.py", line 22,
> 
> >   in __in
> 
> > it__
> 
> > for module in walk_modules(name):
> 
> >   File "C:\Python27\lib\site-packages\scrapy\utils\misc.py", line 66, in
> 
> >   walk_mo
> 
> > dules
> 
> > submod = __import__(fullpath, {}, {}, [''])
> 
> >   File "blog_crawl\spiders\dmoz_spider.py", line 1, in 
> 
> > class DmozSpider(BaseSpider):
> 
> > NameError: name 'BaseSpider' is not defined
> 
> > 
> 
> > someone tells me why, pls
> 
> 
> 
> Assuming you are working your way through the tutorial at
> 
> 
> 
> http://doc.scrapy.org/en/latest/intro/tutorial.html#our-first-spider
> 
> 
> 
> you probably forgot to import the BaseSpider class with
> 
> 
> 
> from scrapy.spider import BaseSpider
> 
> 
> 
> as shown in the code snippet in the "Our first Spider" section of the 
> 
> tutorial.

it had an another error: No module named queuelib, but i have installed all the 
libs mentioned in that passage...

2013-09-23 16:44:17+0800 [scrapy] INFO: Scrapy 0.18.2 started (bot: tutorial)
2013-09-23 16:44:17+0800 [scrapy] DEBUG: Optional features available: ssl, http1
1, libxml2
2013-09-23 16:44:17+0800 [scrapy] DEBUG: Overridden settings: {'NEWSPIDER_MODULE
': 'tutorial.spiders', 'SPIDER_MODULES': ['tutorial.spiders'], 'BOT_NAME': 'tuto
rial'}
2013-09-23 16:44:17+0800 [scrapy] DEBUG: Enabled extensions: LogStats, TelnetCon
sole, CloseSpider, WebService, CoreStats, SpiderState
Traceback (most recent call last):
  File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
  File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
  File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 167, in 
execute()
  File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 142, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
  File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 88, in _run_print
_help
func(*a, **kw)
  File "C:\Python27\lib\site-packages\scrapy\cmdline.py", line 149, in _run_comm
and
cmd.run(args, opts)
  File "C:\Python27\lib\site-packages\scrapy\commands\crawl.py", line 50, in run

self.crawler_process.start()
  File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 93, in start
if self.start_crawling():
  File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 168, in start_cra
wling
return self.start_crawler() is not None
  File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 151, in start_cra
wler
crawler.configure()
  File "C:\Python27\lib\site-packages\scrapy\crawler.py", line 45, in configure
self.engine = ExecutionEngine(self, self._spider_closed)
  File "C:\Python27\lib\site-packages\scrapy\core\engine.py", line 61, in __init
__
self.scheduler_cls = load_object(self.settings['SCHEDULER'])
  File "C:\Python27\lib\site-packages\scrapy\utils\misc.py", line 40, in load_ob
ject
raise ImportError, "Error loading object '%s': %s" % (path, e)
ImportError: Error loading object 'scrapy.core.scheduler.Scheduler': No module n
amed queuelib
-- 
https://mail.python.or

Re: How To Build Scrapy Environment

2013-09-23 Thread Peter Otten
YetToCome wrote:

[snip]

No need to quote the whole turd -- just confirm that it worked...

> it had an another error: No module named queuelib, but i have installed
> all the libs mentioned in that passage...

> ImportError: Error loading object 'scrapy.core.scheduler.Scheduler': No
> module n amed queuelib

Googling "queuelib site:scrapy.org" leads to

http://doc.scrapy.org/en/latest/news.html

containing

"""
0.18.0 (released 2013-08-09)¶
[...]
Moved persistent (on disk) queues to a separate project (queuelib) which 
scrapy now depends on
"""

which in turn leads to

https://github.com/scrapy/queuelib

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


Re: reload and work flow suggestions

2013-09-23 Thread rusi
On Monday, September 23, 2013 2:01:00 PM UTC+5:30, Peter Cacioppi wrote:
> One thing re: editors and interactive environments. I'm not a huge emacs fan 
> (ducking) and I really like iPython.

Heh! Yeah we are an endangered species
G enerally
N ot
U sed
E ditor for
M iddle
A ged
C omputer
S cientists

> 
> 
> In order to avoid calling reload, I've changed my workflow to the following. 
> 
> 
> 1) The "real" code
> 2) The unit tests
> 3) A scratch script
> 
> 
> 
> Then, I launch iPython, which can intellisense launch 3 easily. Then I make 
> whatever changes I need to 1-3 to make a baby step forward, close iPython, 
> and repeat. 

Hardly looks very ergonomic to me
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How To Build Scrapy Environment

2013-09-23 Thread YetToCome
在 2013年9月23日星期一UTC+8下午5时25分25秒,Peter Otten写道:
> YetToCome wrote:
> 
> 
> 
> [snip]
> 
> 
> 
> No need to quote the whole turd -- just confirm that it worked...
> 
> 
> 
> > it had an another error: No module named queuelib, but i have installed
> 
> > all the libs mentioned in that passage...
> 
> 
> 
> > ImportError: Error loading object 'scrapy.core.scheduler.Scheduler': No
> 
> > module n amed queuelib
> 
> 
> 
> Googling "queuelib site:scrapy.org" leads to
> 
> 
> 
> http://doc.scrapy.org/en/latest/news.html
> 
> 
> 
> containing
> 
> 
> 
> """
> 
> 0.18.0 (released 2013-08-09)¶
> 
> [...]
> 
> Moved persistent (on disk) queues to a separate project (queuelib) which 
> 
> scrapy now depends on
> 
> """
> 
> 
> 
> which in turn leads to
> 
> 
> 
> https://github.com/scrapy/queuelib

it works, and i also add some mudules, thank you very much!!!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: reload and work flow suggestions

2013-09-23 Thread Oscar Benjamin
On 23 September 2013 10:35, rusi  wrote:
>> Then, I launch iPython, which can intellisense launch 3 easily. Then I make
>> whatever changes I need to 1-3 to make a baby step forward, close iPython,
>> and repeat.
>
> Hardly looks very ergonomic to me

I'm not quite sure what's meant by intellisense here but using ipython
with a scratch script works very well for me.

Here's how a typical session works for writing a sqrt_floor() function
that works for all positive integers:

In [1]: import math

In [2]: math.sqrt(16)
Out[2]: 4.0

In [3]: math.sqrt(15)
Out[3]: 3.872983346207417

In [4]: int(math.sqrt(15))
Out[4]: 3

In [5]: x = 10 ** 20 + 1

In [6]: int(math.sqrt(x ** 2)) == x
Out[6]: False

In [7]: edit tmp.py

[At this point vim opens, I do some working on paper and write the
following in vim:

import math

def sqrt_floor(y):
x = int(y)
while x ** 2 <= y < (x+1) ** 2:
x = (x + y // x) // 2
return x
]

done. Executing edited code...

In [8]: sqrt_floor(100)
Out[8]: 100

In [9]: sqrt_floor(101)
Out[9]: 101

[Okay whoops! that's not what I wanted... Need to invert the condition
in the while loop!]

In [10]: edit tmp.py
 done. Executing edited code...

In [11]: edit tmp.py
 done. Executing edited code...

In [12]: sqrt_floor(101)
Out[12]: 10

In [13]: sqrt_floor(100)
Out[13]: 10

In [14]: x = 10 ** 20 + 1

In [15]: sqrt_floor(x ** 2) == x
Out[15]: True

[Okay that's better]

In [16]: edit tmp.py

[Now I'll test against a load of integers so the file looks like

import math

def sqrt_floor(y):
x = int(y)
while not (x ** 2 <= y < (x+1) ** 2):
x = (x + y // x) // 2
return x


for y in range(10 ** 6):
x = sqrt_floor(y)
assert x ** 2 <= y < (x+1) ** 2
]
done. Executing edited code...

[All tests have passed but it took a little while so let's time this function]

In [17]: timeit sqrt(10 ** 10)
---
NameError Traceback (most recent call last)

NameError: global name 'sqrt' is not defined

In [18]: from math import sqrt

In [19]: timeit sqrt(10 ** 10)
100 loops, best of 3: 393 ns per loop

In [20]: timeit sqrt_floor(10 ** 10)
1 loops, best of 3: 52.4 us per loop

In [21]: edit tmp.py

[Since sqrt is 100x faster we can use it internally to get an initial guess.
Now the file looks like:

import math

def sqrt_floor(y):
x = int(math.sqrt(y))
while not (x ** 2 <= y < (x+1) ** 2):
x = (x + y // x) // 2
return x


for y in range(10 ** 6):
x = sqrt_floor(y)
assert x ** 2 <= y < (x+1) ** 2
]
done. Executing edited code...

In [22]: timeit sqrt_floor(10 ** 10)
10 loops, best of 3: 4.58 us per loop

[Great. That's 10x faster than before and probably as good as it gets
for speed but this optimisation has problems...]

In [23]: sqrt_floor(10 ** 1000)
---
OverflowError Traceback (most recent call last)
 in ()
> 1 sqrt_floor(10 ** 1000)

Q:\tmp.py in sqrt_floor(y)
  2
  3 def sqrt_floor(y):
> 4 x = int(math.sqrt(y))
  5 while not (x ** 2 <= y < (x+1) ** 2):
  6 x = (x + y // x) // 2

OverflowError: long int too large to convert to float

In [24]: edit tmp.py

[Now the file looks like

import math

def sqrt_floor(y):
try:
x = int(math.sqrt(y))
except OverflowError:
x = y
while not (x ** 2 <= y < (x+1) ** 2):
x = (x + y // x) // 2
return x


for y in range(10 ** 6):
x = sqrt_floor(y)
assert x ** 2 <= y < (x+1) ** 2
]

 done. Executing edited code...
[ This reruns the tests in the file; no AssertionError so all good ]

In [25]: sqrt_floor(10 ** 1000)
Out[25]: 
1L

In [26]: timeit sqrt(10 ** 10)
100 loops, best of 3: 381 ns per loop

And now we're basically done. Recheck the proof, think about how to
handle negative numbers, write a docstring and move on.


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


[ANN] pyspread 0.2.5

2013-09-23 Thread Martin Manns
==
pyspread 0.2.5
==


Pyspread 0.2.5 is released.

This is mainly a bug fix release with some improvements in chart
creation and CSV import.

About pyspread
==

Pyspread is a non-traditional spreadsheet application that is based on
and written in the programming language Python. 

The goal of pyspread is to be the most pythonic spreadsheet application.

Pyspread is free software. It is released under the GPL v3.

Project website: http://manns.github.com/pyspread/


What is new in 0.2.5


 + Brazilian Portuguese localization added
 + Pyspread now displays dates with matplotlib 1.3.0
 + CSV import now allows different file encodings
 + All unit tests now succeed in Windows
 + Chart grid lines are now separately selectable for each axis
 + Chart annotation texts are now treated as code instead of strings
 + Tooltip text for pie chart colors is extended
 + Chart padding fixed
 + Default font size for renderer is now set explicitly if 0 (OS X fix)
 + String representation method of S removed


Known issues


 + Selection mode is disabled in Windows.
 + Paste As... does not work if dates are present in the data structure.
 + Sometimes, pressing redo when there is nothing left to redo has
   undesired effects such as redoing an operation again.

Enjoy

Martin

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


Re: building an online judge to evaluate Python programs

2013-09-23 Thread Fábio Santos
On 20 Sep 2013 21:14, "Jabba Laci"  wrote:
>
> > That last seems to me to be the biggie.  Several times in the past few
> > years, people in this mailing list have tried to build a safe sandbox.
> > And each one was a big failure, for a hacker of sufficient interest.
> > Some of them were spectacular failures.
> >
> > If you have to be safe from your user, Python may be the wrong language
> > to give them.
>
> Well, the course is about Python and I want to test Python scripts...
>
> I've heard about "chroot jail" but I never used it. Wikipedia says:
>
> "A chroot on Unix operating systems is an operation that changes the
> apparent root directory for the current running process and its
> children. A program that is run in such a modified environment cannot
> name (and therefore normally not access) files outside the designated
> directory tree. The term "chroot" may refer to the chroot(2) system
> call or the chroot(8) wrapper program. The modified environment is
> called a "chroot jail"."
>
> I guess it could be used for sandboxing.
>
> Laszlo

It may be a good start to whitelist the modules and builtins they are
allowed to use.

The ast module could be used to scan the source tree for import statements
and run the imported modules through the whitelist.

There should also be many ways to run a script with stripped-down builtins.

Then you can control execution time and memory usage using an external tool.

I'm quite sure this isn't all you need, but it can be a good place to start.
-- 
https://mail.python.org/mailman/listinfo/python-list


parse list recurisively

2013-09-23 Thread andypu
Hello,

i use a load of lists and often i dont know how deep it is, how can i parse 
that lists elegantly (without a bunch of for loops)
-- 
https://mail.python.org/mailman/listinfo/python-list


Help with python functions?

2013-09-23 Thread kjakupak
1.a. Write a function temp(T, from_unit, to_unit) where from_unit and to_unit 
are temperature units, either 'F' (or 'f') for fahrenheit, or 'C' (or 'c') for 
celsius, or 'K' (or 'k') for kelvin; and T is a temperature number for the unit 
from_unit. The function should return the temperature number in the unit to 
to_unit; no unit is returned. [input(...) and print(...) are not allowed]

1.b. Write a function comp(T1, u1, T2, u2) where u1, u2 are temperature units 
as in 1.a., and T1, T2 are temperature numbers for these units. The function 
should return -1 if T1 in u1 is a lower temperature than T2 in u2; it should 
return 0 is the two express equal temperatures; and it should return 1 
otherwise. 

2. Write a function P(p_0, t, i) where t is a time in years *integer), is is a 
yearly interest (as a traditional percentage), and p_0 is the initial principal 
(at time 0). The function should return the principal at time t. Principals and 
interests are float numbers.

Can anyone help me with any of these please? Much appreciated. I honestly don't 
even know how to start them
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse list recurisively

2013-09-23 Thread Chris Angelico
On Mon, Sep 23, 2013 at 10:53 PM,   wrote:
> Hello,
>
> i use a load of lists and often i dont know how deep it is, how can i parse 
> that lists elegantly (without a bunch of for loops)

You can write a function that calls itself - that's what "recursive"
usually means in programming. Start with the tutorial on functions, or
just search the web for 'python recursive function'. Once you have a
bit of code down, you'll be able to ask a more specific question.

Have fun!

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


Re: Python Interview Questions

2013-09-23 Thread Roy Smith
Somebody, whose identity has been lost in three-deep quoting, said:
> > > I am now appearing for Job Interviews these days and I am
> > > wondering if anybody of you appeared for a Python
> > > Interview. Can you please share the questions you were
> > > asked. That will be great help to me.

We have a standard list of about 2 dozen screening questions we use that 
cover a broad but shallow swath of CS, Unix, and Python.  I'm not going 
to share the exact questions, but here's some of the Python topics we 
cover:

The ramifications of string immutability.

How default function arguments work, especially how they interact with 
mutable objects.

How booleans and various ways of testing for equality work.

A question about subclassing a built-in type.

List comprehensions vs. generator expressions.

We don't expect everybody to get every question, but it gives us a quick 
first cut to evaluate applicants before we decide to bring them in for 
an interview or not.

We also want to see that you understand some basic computer science.  If 
nothing else, you need to understand, what O(n) means, and be able to 
give some examples of Python code which exhibit various orders of 
complexity.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: building an online judge to evaluate Python programs

2013-09-23 Thread Ned Batchelder

On 9/23/13 8:33 AM, Fábio Santos wrote:



On 20 Sep 2013 21:14, "Jabba Laci" > wrote:

>
> > That last seems to me to be the biggie.  Several times in the past few
> > years, people in this mailing list have tried to build a safe sandbox.
> > And each one was a big failure, for a hacker of sufficient interest.
> > Some of them were spectacular failures.
> >
> > If you have to be safe from your user, Python may be the wrong 
language

> > to give them.
>
> Well, the course is about Python and I want to test Python scripts...
>
> I've heard about "chroot jail" but I never used it. Wikipedia says:
>
> "A chroot on Unix operating systems is an operation that changes the
> apparent root directory for the current running process and its
> children. A program that is run in such a modified environment cannot
> name (and therefore normally not access) files outside the designated
> directory tree. The term "chroot" may refer to the chroot(2) system
> call or the chroot(8) wrapper program. The modified environment is
> called a "chroot jail"."
>
> I guess it could be used for sandboxing.
>
> Laszlo

It may be a good start to whitelist the modules and builtins they are 
allowed to use.


The ast module could be used to scan the source tree for import 
statements and run the imported modules through the whitelist.


There should also be many ways to run a script with stripped-down 
builtins.


Then you can control execution time and memory usage using an external 
tool.


I'm quite sure this isn't all you need, but it can be a good place to 
start.





Python really is too dynamic for any static analysis like this to help 
in the long run.  It will stop people from doing the obvious things, but 
there will always be a way to circumvent it.  Take a look here:  Eval 
really is dangerous: 
http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html


If you want to run untrusted Python code and prevent malice (or 
stupidity) from harming you, you need OS-level protection.


--Ned.

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


Re: Help with python functions?

2013-09-23 Thread Roy Smith
In article ,
 [email protected] wrote:

> 1.a. Write a function temp(T, from_unit, to_unit) where from_unit and to_unit 
> are temperature units, either 'F' (or 'f') for fahrenheit, or 'C' (or 'c') 
> for celsius, or 'K' (or 'k') for kelvin; and T is a temperature number for 
> the unit from_unit. The function should return the temperature number in the 
> unit to to_unit; no unit is returned. [input(...) and print(...) are not 
> allowed]

We don't do people's homework for them.

But, I'll give you a hint.  Forget about doing this in python.  Write 
down the steps you would take to do this problem with pencil and paper.  
That really is how you begin any programming problem; understanding the 
algorithm you need to execute.  The rest is, as Mozart said, just 
scribbling.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-23 Thread Neil Cerutti
On 2013-09-22, Luca Cerone  wrote:
> I understand your point, but now I am not writing unit tests to
> check the correctness of the code. I am only writing a tutorial
> and assuming that the code is correct. What I have to be sure
> is that the code in the tutorial can be executed correctly, and
> some commands print verbose output which can change.
>
> It is not enough to write >>> ignore = function(a,b,c) won't
> work because the function still prints messages on screen and
> this causes the failure of the test...

It won't be very good documenation any more but nothing stops you
from examining the result in the next doctest and making yourself
happy about it.

  >>> x = input("indeterminate:")
  >>> result = "'{}'".format(x))
  >>> result.startswith("'") and result.endswith("'")
  True

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


Re: Help with python functions?

2013-09-23 Thread Steven D'Aprano
On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote:

> Can anyone help me with any of these please? Much appreciated. I
> honestly don't even know how to start them

Start by writing a function that does nothing:

def nothing():
pass


Now change it so that it takes three arguments:

T, a temperature
from_unit, the temperature scale to convert from 
to_unit, the temperature scale to convert to


def nothing(T, from_unit, to_unit):
pass


Now change the name of the function to match the name you were told to 
use:


def temp(T, from_unit, to_unit):
pass


Now you're a third of the way done! All you need do now is write the 
logic of the function, and make sure it works.

What's the logic of the function? There are four cases:

(1) If from_unit is "C", and to_unit is "F", you need to convert T from 
Celsius to Fahrenheit. I'm sure you can find the formula for that if you 
google it.

(2) otherwise, if from_unit is "F", and to_unit is "C", you need to 
convert T from Fahrenheit to Celsius. Again, the formula to use is 
readily available in about a million reference books and web sites.

(3) otherwise, if from_unit and to_unit are the same (both "C", or both 
"F"). In this case, there is no conversion needed and you can just return 
T unchanged.

(4) otherwise, one or both of from_unit or to_unit must be something 
other than "C" or "F", e.g. "Z" or "Hello". In that case, you have to 
deal with the error. Have you learned about exceptions yet?

It may be acceptable to ignore case #4 -- you'll have to ask your teacher.

Then try it out and make sure it works! For example:

107.6°F == 42°C
-15°C = 5°F


Now you're done! On to the next function... 




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


Re: parse list recurisively

2013-09-23 Thread andypu
thanks i was not able to figure it out some days before but now i think i can 
do it myself: a nice function that searches a string in a list.

def parsesb(lis, string):
print lis 
for num, nam in enumerate (lis):
print num, nam
if type(nam) == list: parsesb(lis[num],string)
if type(nam) == str: # do something
if nam == string: print 'hit!!!'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python, pythontex and plots

2013-09-23 Thread chitturk
True, I did not explain what I was trying to do.

pythontex is a package that allows the inclusion of python code within a
LaTeX document - (sort of like python.sty, but IMO, better) - I use
it along with noweb to create documents that contain documentation,
code and output of the code - and pythontex allows me to access variables
within the python code embedded in the LaTeX - except for the case
I mentioned ... Within the python code (inside the LaTeX document) I had
a "savefig(outputfile)" and I was trying to reference the outputfile
using \py{outputfile}

It may be that someone on the comp.text.tex group may have an answer
(there may be users of pythontex on that newsgroup)

On Sunday, September 22, 2013 11:54:20 PM UTC-5, rusi wrote:
> Take a look at babel
> 
> http://www.cs.unm.edu/~eschulte/data/CISE-13-3-SciProg.pdf
> 
> http://orgmode.org/worg/org-contrib/babel/intro.html
> 
> 
> 
> Its my impression that babel supports everything and more that pylatex does
> 
> 
> 
> ...the catch is that its under emacs...!!

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


Re: parse list recurisively

2013-09-23 Thread Oscar Benjamin
On 23 September 2013 13:53,   wrote:
> Hello,
>
> i use a load of lists and often i dont know how deep it is, how can i parse 
> that lists elegantly (without a bunch of for loops)

I don't really understand what you mean. Can you show some code that
illustrates what you're doing?

http://sscce.org/


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


Re: Making it a MultiThread!

2013-09-23 Thread stas poritskiy
Thanks for getting back to me, so i assume it is OK to have a very very long 
file? The sample code i posted here is basically the barebones of the main app. 
so, combining the GUI-file(gui.py)  with main code is acceptable? Separating 
them into modules was initially the attempt to keep things in order.

On Saturday, September 21, 2013 8:00:29 PM UTC-5, Piet van Oostrum wrote:
> stas poritskiy  writes:
> 
> 
> 
> >> I am working on integration of multiple GUI (Tkinter) elements, such
> 
> >> a progress bar, label update, etc, and throughout my research i
> 
> >> discovered that i need to have Threading modules used to distribute
> 
> >> the calls for GUI update and processing of my main App.
> 
> 
> 
> In general your GUI should be in your main process/thread. For example
> 
> your program doesn't run on Mac OS X because you cannot run the GUI in
> 
> another process or thread.
> 
> 
> 
> Now if your GUI actions are starting some actions that last more than a
> 
> few tenths of a second, you should run these actions in another thread
> 
> or process (usually a thread will work), otherwise the GUI becomes
> 
> unresponsive. If you want to exchange some information between the GUI
> 
> and that thread/process you can use a Queue object, or just give the
> 
> information from the GUI at the start of the thread.
> 
> 
> 
> >> My Application is broken into multiple Classes(modules), and i wanted
> 
> >> to hear your thought on the best practices to implement the Threading
> 
> >> model.
> 
> 
> 
> The structure of you modules isn't good. There are circular imports, and
> 
> that is a sign the the design needs to be simplified:
> 
> 
> 
> multiProcessLauncher imports gui.
> 
> gui imports multiProcessLauncher.
> 
> 
> 
> The latter import isn't necessary. 
> 
> 
> 
> Instead of
> 
> 
> 
> import multiProcessLauncher
> 
> processor = multiProcessLauncher
> 
> name = processor.multiprocessing.current_process().name
> 
> 
> 
> you can just use:
> 
> 
> 
> import multiprocessing
> 
> name = multiprocessing.current_process().name
> 
> 
> 
> My advice would be to just do the GUI in the main module, and if the
> 
> Action needs more time, the create a new Thread or Process in the Action
> 
> class.
> 
> 
> 
> -- 
> 
> Piet van Oostrum 
> 
> WWW: http://pietvanoostrum.com/
> 
> PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parse list recurisively

2013-09-23 Thread andypu
i have a list and i want to search for a certain string and replace it.

i think i got it now...

sbxe1 = list([['type','ter','lala'],'name'])
def parsesb(lis, string, replacement):
for num, nam in enumerate (lis):
if type(nam) == list: 
parsesb(lis[num],string,replacement)
if type(nam) == str: # do something
if nam == string: lis[num] = replacement
parsesb(sbxe1 ,'name', 'bogyman')
print sbxe1 
-- 
https://mail.python.org/mailman/listinfo/python-list


python IDE and function definition

2013-09-23 Thread Chris Friesen


Hi all,

I'm looking for a python IDE (for Linux) that can look at code like this:

class ConductorManager(manager.Manager):
def compute_recover(self, context, instance):
self.compute_api.stop(context, instance, do_cast=False)

where I could highlight the "stop" and ask it to go to the definition. 
(Where the definition is in a different file.)


I'm running into issues where my current IDE (I'm playing with Komodo) 
can't seem to locate the definition, I suspect because it's too ambiguous.


The fact that python is dynamically typed seems to mean that there could 
potentially be multiple answers, any class with a stop() method with the 
right signature could presumably be plausible, right?  So rather than 
give up, I'd like to have my IDE suggest all possible answers.


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


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-23 Thread Luca Cerone
> It won't be very good documenation any more but nothing stops you
> 
> from examining the result in the next doctest and making yourself
> 
> happy about it.
> 
> 
> 
>   >>> x = input("indeterminate:")
> 
>   >>> result = "'{}'".format(x))
> 
>   >>> result.startswith("'") and result.endswith("'")
> 
>   True
> 

Hi Neil, thanks for the hint, but this won't work.

The problem is that the function displays some output informing you of what 
steps are being performed (some of which are displayed by a 3rd party function 
that I don't control).

This output "interferes" with the output that should be checked by doctest.

For example, you can check that the following doctest would fail:

.. doctest:: example_fake

   >>> def myfun(x,verbose):
   ...print "random output"
   ...return x
   >>> myfun(10)
   10

When you run make doctest the test fails with this message:

File "tutorial.rst", line 11, in example_fake
Failed example:
myfun(10)
Expected:
10
Got:
random output
10

In this case (imagine that "random output" is really random, therefore I can 
not easily filter it, if not ignoring several lines. This would be quite easy 
if ellipsis and line continuation wouldn't have the same sequence of 
characters, but unfortunately this is not the case.

The method you proposed still is not applicable, because I have no way to use 
startswith() and endswith()...

The following code could do what I want if I could ignore the output...

   >>> def myfun(x,verbose):
   ...print "random output"
   ...return x
   >>> result = myfun(10) #should ignore the output here!
   >>> print result
   10

fails with this message:

File "tutorial.rst", line 11, in example_fake
Failed example:
result = myfun(10)
Expected nothing
Got:
random output

(line 11 contains: >>> result = myfun(10))

A SKIP directive is not feasible either:

.. doctest:: example_fake

   >>> def myfun(x):
   ...print "random output"
   ...return x
   >>> result = myfun(10) # doctest: +SKIP 
   >>> result
   10

fails with this error message:
File "tutorial.rst", line 12, in example_fake
Failed example:
result
Exception raised:
Traceback (most recent call last):
  File "/usr/lib/python2.7/doctest.py", line 1289, in __run
compileflags, 1) in test.globs
  File "", line 1, in 
result
NameError: name 'result' is not defined

As you can see is not that I want something too weird, is just that sometimes 
you can't control what the function display and ignoring the output is a 
reasonable way to implement a doctest.

Hope these examples helped to understand better what my problem is.

Thanks all of you guys for the hints, suggestions and best practices :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-23 Thread Luca Cerone
I don't know why but it seems that google groups stripped the indentation from 
the code. I just wanted to ensure you that in the examples that I have run
the definition of myfunc contained correctly indented code!

On Monday, 23 September 2013 15:45:43 UTC+1, Luca Cerone  wrote:
> .. doctest:: example_fake
> 
> 
> 
>>>> def myfun(x,verbose):
> 
>...print "random output"
> 
>...return x
> 
>>>> myfun(10)
> 
>10
> 
> 
> 
> When you run make doctest the test fails with this message:
> 
> 
> 
> File "tutorial.rst", line 11, in example_fake
> 
> Failed example:
> 
> myfun(10)
> 
> Expected:
> 
> 10
> 
> Got:
> 
> random output
> 
> 10
> 
> 
> 
> In this case (imagine that "random output" is really random, therefore I can 
> not easily filter it, if not ignoring several lines. This would be quite easy 
> if ellipsis and line continuation wouldn't have the same sequence of 
> characters, but unfortunately this is not the case.
> 
> 
> 
> The method you proposed still is not applicable, because I have no way to use 
> startswith() and endswith()...
> 
> 
> 
> The following code could do what I want if I could ignore the output...
> 
> 
> 
>>>> def myfun(x,verbose):
> 
>...print "random output"
> 
>...return x
> 
>>>> result = myfun(10) #should ignore the output here!
> 
>>>> print result
> 
>10
> 
> 
> 
> fails with this message:
> 
> 
> 
> File "tutorial.rst", line 11, in example_fake
> 
> Failed example:
> 
> result = myfun(10)
> 
> Expected nothing
> 
> Got:
> 
> random output
> 
> 
> 
> (line 11 contains: >>> result = myfun(10))
> 
> 
> 
> A SKIP directive is not feasible either:
> 
> 
> 
> .. doctest:: example_fake
> 
> 
> 
>>>> def myfun(x):
> 
>...print "random output"
> 
>...return x
> 
>>>> result = myfun(10) # doctest: +SKIP 
> 
>>>> result
> 
>10
> 
> 
> 
> fails with this error message:
> 
> File "tutorial.rst", line 12, in example_fake
> 
> Failed example:
> 
> result
> 
> Exception raised:
> 
> Traceback (most recent call last):
> 
>   File "/usr/lib/python2.7/doctest.py", line 1289, in __run
> 
> compileflags, 1) in test.globs
> 
>   File "", line 1, in 
> 
> result
> 
> NameError: name 'result' is not defined
> 
> 
> 
> As you can see is not that I want something too weird, is just that sometimes 
> you can't control what the function display and ignoring the output is a 
> reasonable way to implement a doctest.
> 
> 
> 
> Hope these examples helped to understand better what my problem is.
> 
> 
> 
> Thanks all of you guys for the hints, suggestions and best practices :)

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


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-23 Thread Skip Montanaro
> I don't know why but it seems that google groups stripped the indentation 
> from the code.

Because it's Google Groups.  :-)

800-pound gorillas tend to do pretty much whatever they want.

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


Re: python IDE and function definition

2013-09-23 Thread Fabio Zadrozny
On Mon, Sep 23, 2013 at 12:06 PM, Chris Friesen wrote:

>
> Hi all,
>
> I'm looking for a python IDE (for Linux) that can look at code like this:
>
> class ConductorManager(manager.**Manager):
> def compute_recover(self, context, instance):
> self.compute_api.stop(context, instance, do_cast=False)
>
> where I could highlight the "stop" and ask it to go to the definition.
> (Where the definition is in a different file.)
>
> I'm running into issues where my current IDE (I'm playing with Komodo)
> can't seem to locate the definition, I suspect because it's too ambiguous.
>
> The fact that python is dynamically typed seems to mean that there could
> potentially be multiple answers, any class with a stop() method with the
> right signature could presumably be plausible, right?  So rather than give
> up, I'd like to have my IDE suggest all possible answers.



PyDev (http://pydev.org/) is able to do that (i.e.: if the find definition
doesn't find it directly, it shows a list with possible matches for you to
choose the most appropriate one: http://pydev.org/manual_adv_gotodef.html)
-- additionally, you can also search for methods/classes/attributes
directly: http://pydev.org/manual_adv_open_decl_quick.html

Cheers,

Fabio



>
>
> Chris
> --
> https://mail.python.org/**mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


What's the best way to extract 2 values from a CSV file from each row systematically?

2013-09-23 Thread quarantinemiles
Hey guys,

I'm a little new to Python, and am still learning!

I'm test building a web scraper that extracts prices from a website, based on 
two values I want to extract from a CSV file. The CSV has at least 1000 rows, 
an example:

0,0,KGD,0,DME,0,,0,0

The values I want to extract are KGD and DME (columns 3 and 5).

Each row in the CSV file contains values in columns 3 and 5 that I'd like to 
extract. What's the best way to extract these data, so I can insert them as 
inputs in two different fields in a form? A list, dictionary, or MySQL? I try 
not to do anything with MySQL as I'm not familiar with it at all.

I'm thinking of dictionary because at least I can make it work as a key/value 
pair (for columns 3 and 5), but a dictionary is unordered. I'd like to 
automatically go through each row in the CSV file from beginning to end to 
extract the two values in columns 3 and 5 and insert them into fields in a form.

I'd really appreciate any suggestions or help, thanks in advance!



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


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-23 Thread Neil Cerutti
On 2013-09-23, Neil Cerutti  wrote:
> Perhaps try the "advanced API" and define your oen
> OutputChecker to add the feature that you need.
>
> Figuring out how to best invoke doctest with your modified
> OutputChecker will take some digging in the source, probably
> looking at doctest.testmod. I don't see an example in the docs.

The docstring for doctest.DocTestRunner contains the example code
I was looking for.

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


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-23 Thread Neil Cerutti
On 2013-09-23, Luca Cerone  wrote:
>> It won't be very good documenation any more but nothing stops you
>> 
>> from examining the result in the next doctest and making yourself
>> 
>> happy about it.
>> 
>> 
>> 
>>   >>> x = input("indeterminate:")
>> 
>>   >>> result = "'{}'".format(x))
>> 
>>   >>> result.startswith("'") and result.endswith("'")
>> 
>>   True
>> 
>
> Hi Neil, thanks for the hint, but this won't work.
>
> The problem is that the function displays some output informing
> you of what steps are being performed (some of which are
> displayed by a 3rd party function that I don't control).
>
> This output "interferes" with the output that should be checked by doctest.
>
> For example, you can check that the following doctest would fail:
>
> .. doctest:: example_fake
>
>>>> def myfun(x,verbose):
>...print "random output"
>...return x
>>>> myfun(10)
>10
>
> When you run make doctest the test fails with this message:
>
> File "tutorial.rst", line 11, in example_fake
> Failed example:
> myfun(10)
> Expected:
> 10
> Got:
> random output
> 10
>
> In this case (imagine that "random output" is really random,
> therefore I can not easily filter it, if not ignoring several
> lines. This would be quite easy if ellipsis and line
> continuation wouldn't have the same sequence of characters, but
> unfortunately this is not the case.

Perhaps try the "advanced API" and define your oen OutputChecker
to add the feature that you need.

Figuring out how to best invoke doctest with your modified
OutputChecker will take some digging in the source, probably
looking at doctest.testmod. I don't see an example in the docs.

> Hope these examples helped to understand better what my problem
> is.

Yes, I think it's well-defined now.

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


help with SUDS

2013-09-23 Thread Ismar Sehic
hello, i'm trying to make a wrapper for making xml requests to hotelbeds.com 
site.
this is what i got:


import logging
from suds.xsd.doctor import ImportDoctor, Import
from suds.plugin import Plugin
logging.basicConfig(level = logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
from suds.client import Client
url = 'http://xxx.xxx.xxx.xx/appservices/ws/FrontendService?wsdl'

imp = Import('http://schemas.xmlsoap.org/soap/encoding/')
doctor = ImportDoctor(imp)
client = Client(url, doctor = doctor)

print client

rw = client.service.getHotelValuedAvail()
print rw)
print client 

which gives me following result : 
Suds ( https://fedorahosted.org/suds/ )  version: 0.4 GA  build: R699-20100913

Service ( FrontendServiceService ) tns="http://www.hotelbeds.com/wsdl/2005/06";
   Prefixes (1)
 ns0 = "http://schemas.xmlsoap.org/soap/encoding/";
  Ports (1):
  (FrontendService)
 Methods (34):
   cancelProtectionAdd(xs:anyType cancelProtectionAdd, )
   cancelProtectionRemove(xs:anyType cancelProtectionRemove, )
getCancelProtectionAvail(xs:anyType getCancelProtectionAvail, )
getCarCountryList(xs:anyType getCarCountryList, )
getCarInfoSet(xs:anyType getCarInfoSet, )
getCarValuedAvail(xs:anyType getCarValuedAvail, )
getCountryList(xs:anyType getCountryList, )
getDestinationGroupList(xs:anyType getDestinationGroupList, )
getHotelBoardGroupList(xs:anyType getHotelBoardGroupList, )
getHotelBoardList(xs:anyType getHotelBoardList, )
getHotelCategoryGroupList(xs:anyType getHotelCategoryGroupList, )
getHotelCategoryList(xs:anyType getHotelCategoryList, )
getHotelCountryList(xs:anyType getHotelCountryList, )
getHotelDetail(xs:anyType getHotelDetail, )
getHotelList(xs:anyType getHotelList, )
getHotelRoomTypeGroupList(xs:anyType getHotelRoomTypeGroupList, )
getHotelValuedAvail(xs:anyType getHotelValuedAvail, )
getIncomingOfficeDetail(xs:anyType getIncomingOfficeDetail, )
getIncomingOfficeList(xs:anyType getIncomingOfficeList, )
getPurchaseDetail(xs:anyType getPurchaseDetail, )
getPurchaseList(xs:anyType getPurchaseList, )
getTicketAvail(xs:anyType getTicketAvail, )
getTicketClassificationList(xs:anyType getTicketClassificationList, )
getTicketCountryList(xs:anyType getTicketCountryList, )
getTicketDetail(xs:anyType getTicketDetail, )
getTicketValuation(xs:anyType getTicketValuation, )
getTransferCountryList(xs:anyType getTransferCountryList, )
getTransferValuedAvail(xs:anyType getTransferValuedAvail, )
getZoneGroupList(xs:anyType getZoneGroupList, )
purchaseCancel(xs:anyType purchaseCancel, )
purchaseConfirm(xs:anyType purchaseConfirm, )
purchaseFlush(xs:anyType purchaseFlush, )
serviceAdd(xs:anyType serviceAdd, )
serviceRemove(xs:anyType serviceRemove, )
 Types (48):
ns0:Array
ns0:ENTITIES
ns0:ENTITY
ns0:ID
ns0:IDREF
ns0:IDREFS
ns0:NCName
ns0:NMTOKEN
ns0:NMTOKENS
ns0:NOTATION
ns0:Name
ns0:QName
ns0:Struct
ns0:anyURI
ns0:arrayCoordinate
ns0:base64
ns0:base64Binary
ns0:boolean
ns0:byte
ns0:date
ns0:dateTime
ns0:decimal
ns0:double
ns0:duration
ns0:float
ns0:gDay
ns0:gMonth
ns0:gMonthDay
ns0:gYear
ns0:gYearMonth
ns0:hexBinary
ns0:int
ns0:integer
ns0:language
ns0:long
ns0:negativeInteger
ns0:nonNegativeInteger
ns0:nonPositiveInteger
ns0:normalizedString
ns0:positiveInteger
ns0:short
ns0:string
ns0:time
ns0:token
ns0:unsignedByte
ns0:unsignedInt
ns0:unsignedLong
ns0:unsignedShort



DEBUG:suds.client:sending to (http://212.170.239.71:9091/appservices 
/ws/FrontendService)
message:

http://axis.frontend.hydra.hotelbeds.com"; 
xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"; 
xmlns:xsi="http://www.w3.org
/2001/XMLSchema-instance" 
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/";>
   
   
  
   

DEBUG:suds.client:headers = {'SOAPAction': u'""', 'Content-Type': 'text/xml; 
charset=utf-8'}
Traceback (most recent call last):
  File "soaptest.py", line 23, in 
rw = client.service.getHotelValuedAvail()
  File 
"/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/client.py", 
line 542, in __call__
return client.invoke(args, kwargs)
  File 
"/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/client.py", 
line 602, in invoke
result = self.send(soapenv)
  File 
"/usr/local/lib/python2.6/dist-packages/suds-0.4-py2.6.egg/suds/client.py", 
line 637, in send

Re: python IDE and function definition

2013-09-23 Thread Fabio Zadrozny
On Mon, Sep 23, 2013 at 2:29 PM, Chris Friesen  wrote:

> On 09/23/2013 09:32 AM, Fabio Zadrozny wrote:
>
>> On Mon, Sep 23, 2013 at 12:06 PM, Chris Friesen > > wrote:
>>
>>
>> Hi all,
>>
>> I'm looking for a python IDE (for Linux) that can look at code like
>> this:
>>
>> class ConductorManager(manager.__**Manager):
>>
>>  def compute_recover(self, context, instance):
>>  self.compute_api.stop(context, instance, do_cast=False)
>>
>> where I could highlight the "stop" and ask it to go to the
>> definition. (Where the definition is in a different file.)
>>
>> I'm running into issues where my current IDE (I'm playing with
>> Komodo) can't seem to locate the definition, I suspect because it's
>> too ambiguous.
>>
>> The fact that python is dynamically typed seems to mean that there
>> could potentially be multiple answers, any class with a stop()
>> method with the right signature could presumably be plausible,
>> right?  So rather than give up, I'd like to have my IDE suggest all
>> possible answers.
>>
>>
>>
>> PyDev (http://pydev.org/) is able to do that (i.e.: if the find
>> definition doesn't find it directly, it shows a list with possible
>> matches for you to choose the most appropriate one:
>> http://pydev.org/manual_adv_**gotodef.html)
>> -- additionally, you can also
>> search for methods/classes/attributes directly:
>> http://pydev.org/manual_adv_**open_decl_quick.html
>>
>
> I've installed eclipse/pydev and tried it out.  The problem that I'm
> seeing is that it will show me *all* stop() methods that it knows about,
> regardless of function signature.
>
> So in the above case, my function call looks like:
> self.compute_api.stop(context, instance, do_cast=False)
>
> but pydev will offer matches that look like:
> def stop(self):
>
> This runs into problems with commonly-named functions.  I tried searching
> for the start() method in:
>
> self.compute_api.start(**context, instance)
>
> and it complained that there were too many possible results.
>
> Basically, I'm looking for something smart enough to throw out methods
> with the same name but that don't match the signature.
>
> Chris
>
>
Seems like a nice request... it'd be nice if you can report it as a feature
request at the pydev tracker (at https://sw-brainwy.rhcloud.com/ -- note
that you have to sign up to create an issue).

Cheers,

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


Re: What's the best way to extract 2 values from a CSV file from each row systematically?

2013-09-23 Thread Tim Chase
On 2013-09-23 10:10, [email protected] wrote:
> based on two values I want to extract from a CSV file. The
> CSV has at least 1000 rows, an example:
> 
> 0,0,KGD,0,DME,0,,0,0
[snip]
> I'd like to automatically go through each row in the CSV file from
> beginning to end to extract the two values in columns 3 and 5 and
> insert them into fields in a form.

The csv module has several tools that make this easy to do.  If there
are column-headers, you can do

  import csv
  with file("myfile.csv", "rb") as f:
for row in csv.DictReader(f)
  insert_fields_into_form(
row["KGD"],
row["DME"],
)

which I like for clarity, ease of updating, and robustness (if for
some reason, the columns get moved around, or columns get
added/removed, as long as the headers remain the same, you can get
the data).

If it doesn't have headers, then you'd have to manually pick out the
columns, either by tuple-unpacking:

  with file("myfile.csv", "rb") as f:
for _, _, kgd, _, dme in csv.reader(f)
  insert_fields_into_form(kgd, dme)

or by directly indexing:

  KGD_COL = 3
  DME_COL = 5
  with file("myfile.csv", "rb") as f:
for row in csv.reader(f)
  insert_fields_into_form(
row[KGD_COL],
row[DME_COL],
)

both of which are more fragile than DictReader when it comes to
columns being added/removed.  I leave the implementation of
insert_fields_into_form() up to you :-)

-tkc





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


Re: python IDE and function definition

2013-09-23 Thread Chris Friesen

On 09/23/2013 09:32 AM, Fabio Zadrozny wrote:

On Mon, Sep 23, 2013 at 12:06 PM, Chris Friesen mailto:[email protected]>> wrote:


Hi all,

I'm looking for a python IDE (for Linux) that can look at code like
this:

class ConductorManager(manager.__Manager):
 def compute_recover(self, context, instance):
 self.compute_api.stop(context, instance, do_cast=False)

where I could highlight the "stop" and ask it to go to the
definition. (Where the definition is in a different file.)

I'm running into issues where my current IDE (I'm playing with
Komodo) can't seem to locate the definition, I suspect because it's
too ambiguous.

The fact that python is dynamically typed seems to mean that there
could potentially be multiple answers, any class with a stop()
method with the right signature could presumably be plausible,
right?  So rather than give up, I'd like to have my IDE suggest all
possible answers.



PyDev (http://pydev.org/) is able to do that (i.e.: if the find
definition doesn't find it directly, it shows a list with possible
matches for you to choose the most appropriate one:
http://pydev.org/manual_adv_gotodef.html) -- additionally, you can also
search for methods/classes/attributes directly:
http://pydev.org/manual_adv_open_decl_quick.html


I've installed eclipse/pydev and tried it out.  The problem that I'm 
seeing is that it will show me *all* stop() methods that it knows about, 
regardless of function signature.


So in the above case, my function call looks like:
self.compute_api.stop(context, instance, do_cast=False)

but pydev will offer matches that look like:
def stop(self):

This runs into problems with commonly-named functions.  I tried 
searching for the start() method in:


self.compute_api.start(context, instance)

and it complained that there were too many possible results.

Basically, I'm looking for something smart enough to throw out methods 
with the same name but that don't match the signature.


Chris

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


Re: What's the best way to extract 2 values from a CSV file from each row systematically?

2013-09-23 Thread Joel Goldstick
On Mon, Sep 23, 2013 at 1:10 PM,  wrote:

> Hey guys,
>
> I'm a little new to Python, and am still learning!
>
> I'm test building a web scraper that extracts prices from a website, based
> on two values I want to extract from a CSV file. The CSV has at least 1000
> rows, an example:
>
> 0,0,KGD,0,DME,0,,0,0
>
> The values I want to extract are KGD and DME (columns 3 and 5).
>
> Each row in the CSV file contains values in columns 3 and 5 that I'd like
> to extract. What's the best way to extract these data, so I can insert them
> as inputs in two different fields in a form? A list, dictionary, or MySQL?
> I try not to do anything with MySQL as I'm not familiar with it at all.
>
> I'm thinking of dictionary because at least I can make it work as a
> key/value pair (for columns 3 and 5), but a dictionary is unordered. I'd
> like to automatically go through each row in the CSV file from beginning to
> end to extract the two values in columns 3 and 5 and insert them into
> fields in a form.
>
> I'd really appreciate any suggestions or help, thanks in advance!
>
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

You should check out the csv module here:
http://docs.python.org/2/library/csv.html#module-csv
It will read your csv file into a list (the file rows) of lists (the
columns).  You can easily loop over the data to extract the columns you
want using list indexing

-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: building an online judge to evaluate Python programs

2013-09-23 Thread Modulok
>
>
> If you want to run untrusted Python code and prevent malice (or stupidity)
> from harming you, you need OS-level protection.
>
>
Agreed. Just for fun here's a simple example of what could be an honest
mistake
that consumes all physical memory and swap. A well behaved kernel will kill
the
process eventually when it can no longer allocate memory, but not before
bringing the machine to its knees:

class Foo(object):
def __init__(self):
self.x = 1
def __iter__(self):
return self
def next(self):
self.x += 1
# Oops. In a well behaved iterator this should eventually
# raise 'StopIteration'. I knew I forgot something.


a = Foo()
b = list(a)


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


Re: Functional Programming and python

2013-09-23 Thread Vito De Tullio
rusi wrote:

> [Not everything said there is correct; eg python supports currying better
> [than haskell which is surprising considering that Haskell's surname is
> [Curry!]

AFAIK python does not support currying at all (if not via some decorators or 
something like that).

Instead every function in haskell implicitly support currying... so... how 
does "no support" is better than "full support"?


-- 
By ZeD

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


Re: Sphinx Doctest: test the code without comparing the output.

2013-09-23 Thread Luca Cerone
> 
> The docstring for doctest.DocTestRunner contains the example code
> 
> I was looking for.
> 
> 
 Thanks, I will give it a try!
> 
> -- 
> 
> Neil Cerutti

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


Re: What's the best way to extract 2 values from a CSV file from each row systematically?

2013-09-23 Thread Neil Cerutti
On 2013-09-23, [email protected]  wrote:
> Hey guys,
>
> I'm a little new to Python, and am still learning!
>
> I'm test building a web scraper that extracts prices from a
> website, based on two values I want to extract from a CSV file.
> The CSV has at least 1000 rows, an example:
>
> 0,0,KGD,0,DME,0,,0,0
>
> The values I want to extract are KGD and DME (columns 3 and 5).

Use the csv module.

http://docs.python.org/2/library/csv.html

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


how can I retrieve a particular tweet, having its tweet id?

2013-09-23 Thread Andres Soto
 how can I retrieve a particular tweet, having its tweet id, and the username, 
the date and the language?
Regards
Andrés Soto
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how can I retrieve a particular tweet, having its tweet id?

2013-09-23 Thread Terry Reedy

On 9/23/2013 2:10 PM, Andres Soto wrote:

  how can I retrieve a particular tweet, having its tweet id, and the
username, the date and the language?


What, if anything, have you done to try to solve this yourself?
Like searching the web? ('Python twitter' for instance)

--
Terry Jan Reedy

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


Re: reload and work flow suggestions

2013-09-23 Thread Peter Cacioppi
On Saturday, September 21, 2013 2:43:13 PM UTC-7, Peter Cacioppi wrote:
> This is an idea brought over from another post.
> 
> 
> 
> When I write Python code I generally have 2 or 3 windows open simultaneously.
> 
> 
> 
> 1) An editor for the actual code.
> 
> 2) The interactive interpreter.
> 
> 3) An editor for the unit tests. (Sometimes skipped for quick one-off scripts)
> 
> 
> 
> My work flow tends to involve using 2 to debug the issues that come up with 1 
> and 3. I'll write some new code in 1, play around with it in 2, then solidify 
> the tests in 3. Or a test in 3 fails and I dig around with it using 2.
> 
> 
> 
> My problem is that I tend to use reload() quite a bit. I want to call 
> functions and construct objects that are inside the guts of 1 and pass them 
> arguments that are stored as variables in 2. If I restart my session for 2 I 
> lose these variables (iPython does mitigate the pain here somewhat). Hence, I 
> reload() modules into 2 when they are changed.
> 
> 
> 
> I use ipdb a lot in 2. I usually don't feel comfortable with virgin code or a 
> debug fix that hasn't been stepped through with the debugger.
> 
> 
> 
> Is there something wrong with this work flow? I understand most python 
> experts avoid reload(). So what are they doing that I'm not? I love the 
> ability of Python to quickly let you dive deep into your code and set up a 
> difficult case with 2, it's hard to imagine giving this up, and it's hard to 
> imagine using it without reload(). 
> 
> 
> 
> Thanks for any tips.

So when I say I exploit the intellisense of iPython, it's simply this.

I have a "working on X" script that has some handy variables and runs whatever 
is currently of interest in the functional code or the units tests. It even 
launches into the ipdb debugger, if needed. 

The "working on X" is kept in the scripts directory of iPython. The older 
"working on Y" scripts are renamed to "not working on Y", so that there is only 
one script in that particular directory that starts with "wo". 

When iPython launches, I just type run wo [tab] and it completes the file name. 
So the whole "proxy for reload" process is actually pretty quick. For me it's 
faster than what I used to do with reload(), plus the "working on" script keeps 
a nice record of where I was in a debugging/experiment process. iPython makes 
it easy to create history dumps which can then be selectively copied over to 
the "working on" if I forget what was useful.

At any rate, it sounds like emacs plays very well with python and iPython, not 
disparaging the utility of emacs kung fu.

But if you want to use a different editor (I use idle for editing), and also 
use iPython for interactivity (which is very slick), this workflow works nice 
for me and might be helpful to you. 

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


Re: Help with python functions?

2013-09-23 Thread kjakupak
On Monday, September 23, 2013 9:56:45 AM UTC-4, Steven D'Aprano wrote:
> On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote:
> 
> Now you're done! On to the next function... 
> 
> 
> 
> -- 
> 
> Steven

def temp(T, from_unit, to_unit):
conversion_table = {('c', 'k'):lambda x: x + 273.15,
('c', 'f'):lambda x: (x * (9.0/5)) + 32,
('k', 'c'):lambda x: x - 273.15,
('k', 'f'):lambda x: (x * (9.0/5)) - 459.67,
('f', 'c'):lambda x: (x - 32) * (5.0/9),
('f', 'k'):lambda x: (x + 459.67) * (5.0/9)}
f = conversion_table[(from_unit.lower(), to_unit.lower())]
return f(T)

Would this be correct?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help with python functions?

2013-09-23 Thread Terry Reedy

On 9/23/2013 6:32 PM, [email protected] wrote:

On Monday, September 23, 2013 9:56:45 AM UTC-4, Steven D'Aprano wrote:

On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote:

Now you're done! On to the next function...



--

Steven


def temp(T, from_unit, to_unit):
 conversion_table = {('c', 'k'):lambda x: x + 273.15,
 ('c', 'f'):lambda x: (x * (9.0/5)) + 32,
 ('k', 'c'):lambda x: x - 273.15,
 ('k', 'f'):lambda x: (x * (9.0/5)) - 459.67,
 ('f', 'c'):lambda x: (x - 32) * (5.0/9),
 ('f', 'k'):lambda x: (x + 459.67) * (5.0/9)}
 f = conversion_table[(from_unit.lower(), to_unit.lower())]
 return f(T)


What happens if you run some tests? If you use unittest, you can use the 
assertAlmostEqualMethod, or just write something similar yourself. Be 
careful with values near 0..


At minimum, how many tests do you need, 6 or 9?


--
Terry Jan Reedy

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


Re: Help with python functions?

2013-09-23 Thread kjakupak
On Monday, September 23, 2013 9:56:45 AM UTC-4, Steven D'Aprano wrote: 
> On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote: 
> 
> Now you're done! On to the next function... 
> 
> 
> 
> -- 
> 
> Steven 

def temp(T, from_unit, to_unit): 
conversion_table = {('c', 'k'):lambda x: x + 273.15, 
('c', 'f'):lambda x: (x * (9.0/5)) + 32, 
('k', 'c'):lambda x: x - 273.15, 
('k', 'f'):lambda x: (x * (9.0/5)) - 459.67, 
('f', 'c'):lambda x: (x - 32) * (5.0/9), 
('f', 'k'):lambda x: (x + 459.67) * (5.0/9)} 
f = conversion_table[(from_unit.lower(), to_unit.lower())] 
return f(T) 

Would this be correct? 
Also, the temperature number had to be of type float so I feel like I did this 
wrong...

As for the next one, so far I've gotten:
def comp(T1, u1, T2, u2):
if u1 > u2:
return -1
elif u2 > u1:
return 1
else:
return 0


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


Re: python IDE and function definition

2013-09-23 Thread Neil Hodgson

Chris Friesen:


where I could highlight the "stop" and ask it to go to the definition.
(Where the definition is in a different file.)

I'm running into issues where my current IDE (I'm playing with Komodo)
can't seem to locate the definition, I suspect because it's too ambiguous.


Some IDEs allow you to help them understand the context by adding 
type information. Here's some documentation for Wing IDE that uses an 
isinstance assertion:

http://www.wingware.com/doc/edit/helping-wing-analyze-code

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


Re: Help with python functions?

2013-09-23 Thread Dave Angel
On 23/9/2013 18:55, [email protected] wrote:

> On Monday, September 23, 2013 9:56:45 AM UTC-4, Steven D'Aprano wrote: 
>> On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote: 
>> 
>> Now you're done! On to the next function... 
>> 
>> 
>> 
>> -- 
>> 
>> Steven 
>
> def temp(T, from_unit, to_unit): 
> conversion_table = {('c', 'k'):lambda x: x + 273.15, 
> ('c', 'f'):lambda x: (x * (9.0/5)) + 32, 
> ('k', 'c'):lambda x: x - 273.15, 
> ('k', 'f'):lambda x: (x * (9.0/5)) - 459.67, 
> ('f', 'c'):lambda x: (x - 32) * (5.0/9), 
> ('f', 'k'):lambda x: (x + 459.67) * (5.0/9)} 
> f = conversion_table[(from_unit.lower(), to_unit.lower())] 
> return f(T) 
>
> Would this be correct? 
> Also, the temperature number had to be of type float so I feel like I did 
> this wrong...
>
> As for the next one, so far I've gotten:
> def comp(T1, u1, T2, u2):
> if u1 > u2:
> return -1
> elif u2 > u1:
> return 1
> else:
> return 0


I didn't see any spec that said Python 3.x.  in version 2.x, this would
be incorrect.

-- 
DaveA


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


Re: Help with python functions?

2013-09-23 Thread kjakupak
On Monday, September 23, 2013 8:07:44 PM UTC-4, Dave Angel wrote:
> 
> I didn't see any spec that said Python 3.x.  in version 2.x, this would
> 
> be incorrect.
> 
> 
> 
> -- 
> 
> DaveA

It's for Python 3.2
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help with python functions?

2013-09-23 Thread Denis McMahon
On Mon, 23 Sep 2013 15:55:53 -0700, kjakupak wrote:

> As for the next one, so far I've gotten:
> def comp(T1, u1, T2, u2):
> if u1 > u2:
> return -1
> elif u2 > u1:
> return 1
> else:
> return 0

If the first function you wrote allows you to convert temps in different 
scales to a common scale, then in the second function, you can call the 
first function to convert both temps to a common scale, and compare them.

Adding "same scale" conversions in the first function might help. In a 
same scale conversion, the input and output units are the same, and the 
output value is the input value.

Then to compare T1 in u1 and T2 in u2, convert them both to a common 
scale (which might be u1 or u2 or some other scale) using your temp 
function, and then compare the resulting values.

-- 
Denis McMahon, [email protected]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What minimum should a person know before saying "I know Python"

2013-09-23 Thread CM
On Friday, September 20, 2013 5:58:00 AM UTC-4, Aseem Bansal wrote:
> I started Python 4 months ago. Largely self-study with use of Python 
> documentation, stackoverflow and google. I was thinking what is the minimum 
> that I must know before I can say that I know Python?

Seems to me a fuzzy boundary between "Not knowing" and "knowing".  I prefer 
thinking in terms of a spectrum, from 0-10 or pick your scale.

0 - Person who has never heard of Python or has but that's the extent of it.
1 - Beginning installer / Hello Worlder! / clumsy dabbler / what is self?
2 - Underway in earnest, not yet making anything all that much
3 - Making stuff, but clunky
4 - Making stuff pretty well, but looking up 2/3rds of it on SE or equivalent.
5 - Making stuff pretty well, but looking up 1/3rds of it on SE or equivalent.
6 - Making stuff pretty well, occasionally consulting the Python.org docs
7 - Tim Chase's list level
8 - The guy who hired the guy at 7 (assuming he is even further on)
9 - Gurus of this list
10 - Uber-gurus 
10^6 - Guido

I feel like I'm about 5 maybe, with some embarrassing chinks in the armor?  
Draw the "know line" boundary wherever you want, but I'd think you'd probably 
want to be above 4.  I know I'd feel more comfortable saying I know Python if I 
were at 7 (and thanks, Tim Chase; I saved that list a while back in my files to 
consult someday, maybe).  That said, I've written 20k+ loc of (mostly?) working 
code in Python and have done some contracting work at my humble 5, so there's 
that.



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


Re: Help with python functions?

2013-09-23 Thread kjakupak
On Monday, September 23, 2013 10:12:05 PM UTC-4, Denis McMahon wrote:
> 
> 
> If the first function you wrote allows you to convert temps in different 
> 
> scales to a common scale, then in the second function, you can call the 
> 
> first function to convert both temps to a common scale, and compare them.
> 
> 
> 
> Adding "same scale" conversions in the first function might help. In a 
> 
> same scale conversion, the input and output units are the same, and the 
> 
> output value is the input value.
> 
> 
> 
> Then to compare T1 in u1 and T2 in u2, convert them both to a common 
> 
> scale (which might be u1 or u2 or some other scale) using your temp 
> 
> function, and then compare the resulting values.
> 
> 
> 
> -- 
> 
> Denis McMahon, [email protected]

Not sure if we've gotten that far in class, considering I don't know how to go 
about doing that.

For the third function, I'm actually kind of stumped:
def P(p_0, t, i):
Amount = P(1 + (i/100))
return P(1 + (t * i/12))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: building an online judge to evaluate Python programs

2013-09-23 Thread Larry Hudson

On 09/23/2013 06:20 AM, Ned Batchelder wrote:




If you want to run untrusted Python code and prevent malice (or stupidity) from 
harming you, you
need OS-level protection.

--Ned.



That reminds me of the quote from Albert Einstein, (paraphrased):

"There are only two things that are infinite, the universe and human stupidity.  And I'm not 
sure about the universe."


 -=- Larry -=-

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


Re: Help with python functions?

2013-09-23 Thread Steven D'Aprano
On Mon, 23 Sep 2013 15:32:37 -0700, kjakupak wrote:

> On Monday, September 23, 2013 9:56:45 AM UTC-4, Steven D'Aprano wrote:
>> On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote:
>> 
>> Now you're done! On to the next function...
>> 
>> 
>> 
>> --
>> 
>> Steven
> 
> def temp(T, from_unit, to_unit):
> conversion_table = {('c', 'k'):lambda x: x + 273.15,
> ('c', 'f'):lambda x: (x * (9.0/5)) + 32, 
> ('k', 'c'):lambda x: x - 273.15,
> ('k', 'f'):lambda x: (x * (9.0/5)) - 459.67,
> ('f', 'c'):lambda x: (x - 32) * (5.0/9),
> ('f', 'k'):lambda x: (x + 459.67) * (5.0/9)}
> f = conversion_table[(from_unit.lower(), to_unit.lower())] 
> return f(T)

Well, I'm impressed! From "I honestly don't even know how to start them" 
to a dispatch table containing first class functions made with lambda in 
under 9 hours. Well done!

I expected you to start with a big block of if...elif statements, but a 
dispatch table is a much nicer solution.


> Would this be correct?

You tell us :-) Does it work? Are you confident that the conversion 
equations are correct? If you try converting various temperatures, do you 
get the right results?



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


Re: Help with python functions?

2013-09-23 Thread Steven D'Aprano
On Mon, 23 Sep 2013 15:55:53 -0700, kjakupak wrote:


> As for the next one, so far I've gotten: def comp(T1, u1, T2, u2):
> if u1 > u2:
> return -1
> elif u2 > u1:
> return 1
> else:
> return 0


That is only comparing the units, not the temperatures. Since the units 
are characters, you're essentially saying that any temperature in Kelvin 
is always greater than any temperature in Celsius, since "K" > "C". 
Worse, you're saying that any two temperatures with the same unit are 
automatically equal, so that -50°F == 5°F.

Instead, you need to convert both temperatures into a common unit, say, 
Kelvin, then compare the two temperatures:

* convert T1 from u1 to Kelvin;
* convert T2 from u2 to Kelvin;
* compare T1 and T2.

You don't have to use Kelvin. You could use any temperature scale, so 
long as it is the same for both temperatures.


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


Re: Help with python functions?

2013-09-23 Thread Steven D'Aprano
On Mon, 23 Sep 2013 15:32:37 -0700, kjakupak wrote:

> def temp(T, from_unit, to_unit):
> conversion_table = {('c', 'k'):lambda x: x + 273.15,
> ('c', 'f'):lambda x: (x * (9.0/5)) + 32, ('k',
> 'c'):lambda x: x - 273.15,
> ('k', 'f'):lambda x: (x * (9.0/5)) - 459.67,
> ('f', 'c'):lambda x: (x - 32) * (5.0/9), ('f',
> 'k'):lambda x: (x + 459.67) * (5.0/9)}
> f = conversion_table[(from_unit.lower(), to_unit.lower())] return
> f(T)
> 
> Would this be correct?

Oh, I forgot... what happens if both units are the same?

Hint: if both units are the same, no conversion is necessary.




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


Re: Help with python functions?

2013-09-23 Thread Dave Angel
On 23/9/2013 21:23, [email protected] wrote:

> On Monday, September 23, 2013 8:07:44 PM UTC-4, Dave Angel wrote:
>> 
>> I didn't see any spec that said Python 3.x.  in version 2.x, this would
>> 
>> be incorrect.
>> 
>> 
>> 
>> -- 
>> 
>> DaveA
>
> It's for Python 3.2

Then I'd have a comment saying so, right at the top.  Or use that for
the shebang line.

-- 
DaveA

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


Re: help with SUDS

2013-09-23 Thread dieter
Ismar Sehic  writes:

> hello, i'm trying to make a wrapper for making xml requests to hotelbeds.com 
> site.
> ...
>   File "/usr/lib/python2.6/urllib2.py", line 1172, in http_open
> return self.do_open(httplib.HTTPConnection, req)
>   File "/usr/lib/python2.6/urllib2.py", line 1147, in do_open
> raise URLError(err)
> urllib2.URLError: 

This indicates a problem of the service you are contacting:
"suds" tries to open a connection to the service url defined
in the WSDL, but it does not get a response within a reasonable
time frame.

The problem might be temporary (a temporary overload of the
corresponding service) - or they may be some error in the
WSDL - or some firewall may interfere with your request - or ...

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