Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Javier Collado
Hello,

2011/8/31 T. Goodchild :
> But one of the things that bugs me is the requirement that all class
> methods have 'self' as their first parameter.  On a gut level, to me
> this seems to be at odds with Python’s dedication to simplicity.

I think the answer to this question is part of the zen of python:
<>

http://www.python.org/dev/peps/pep-0020/

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


Re: parse html rendered by js

2011-02-12 Thread Javier Collado
Hello,

2011/2/11 yanghq :
>    but for some pages rendered by js, like:

You could use selenium or windmill to help you reproduce the contents
of the web page in a browser so you can get the data from the DOM tree
once the page has been rendered instead of by parsing the js.

Best regards,
Javier
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.path.isfile and wildcard for directory name

2010-12-30 Thread Javier Collado
Hello,

2010/12/30  :
> How can i do the same thing (wildcard in a directory name) in python please ?

You can get the contents of a directory with os.listdir and filter
with fnmatch.fnmatch more or less as in the example from the
documentation:
-
import fnmatch
import os

for file in os.listdir('.'):
if fnmatch.fnmatch(file, '*.txt'):
print file
-

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


Re: Parsing markup.

2010-11-25 Thread Javier Collado
Hello,

2010/11/26 Joe Goldthwaite :
> I’m attempting to parse some basic tagged markup.
>
>  Elementree and lxml seem to want a full formatted
> page, not a small segment like this one.

BeautifulSoup (http://www.crummy.com/software/BeautifulSoup/) could
help in the parsing:

>>> from BeautifulSoup import BeautifulSoup as Soup
>>> s = Soup(text)
>>> print s.prettify()

 This is a paragraph with
 
  bold
 
 and
 
  italic
 
 elements in it


 It can be made up of multiple lines separated by pagagraph tags.

>>> s.findAll('b')
[bold]
>>> s.findAll('i')
[italic]
>>> s.findAll('p')
[This is a paragraph with bold and italic elements in it,
 It can be made up of multiple lines separated by pagagraph tags.]

Best regards,
Javier
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: re.sub unexpected behaviour

2010-07-06 Thread Javier Collado
Thanks for your answers. They helped me to realize that I was
mistakenly using match.string (the whole string) when I should be
using math.group(0) (the whole match).

Best regards,
Javier
-- 
http://mail.python.org/mailman/listinfo/python-list


re.sub unexpected behaviour

2010-07-06 Thread Javier Collado
Hello,

Let's imagine that we have a simple function that generates a
replacement for a regular expression:

def process(match):
return match.string

If we use that simple function with re.sub using a simple pattern and
a string we get the expected output:
re.sub('123', process, '123')
'123'

However, if the string passed to re.sub contains a trailing new line
character, then we get an extra new line character unexpectedly:
re.sub(r'123', process, '123\n')
'123\n\n'

If we try to get the same result using a replacement string, instead
of a function, the strange behaviour cannot be reproduced:
re.sub(r'123', '123', '123')
'123'

re.sub('123', '123', '123\n')
'123\n'

Is there any explanation for this? If I'm skipping something when
using a replacement function with re.sub, please let me know.

Best regards,
Javier
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fast regex

2010-05-06 Thread Javier Collado
Hello,

2010/5/6 james_027 :
> I was working with regex on a very large text, really large but I have
> time constrained. Does python has any other regex library or string
> manipulation library that works really fast?

re2 (http://code.google.com/p/re2/) is suppossed to be faster than the
standard library in python. Unfortunately, it's implemented in C++ and
there isn't an official python wrapper for it. However, you can find a
wrapper that can be useful for you here:
http://github.com/facebook/pyre2

Best regards,
Javier
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Are there in Python some static web site generating tools like webgen, nanoc or webby in Ruby ?

2010-03-09 Thread Javier Collado
> I'm only aware of Hyde (http://ringce.com/hyde)

There are also jekyll and cyrax:
http://github.com/mojombo/jekyll/
http://pypi.python.org/pypi/cyrax/0.1.5

I haven't tried any of them, but it looks like cyrax is in active
development and its design was inspired in both jekyll and hyde.

Best regards,
Javier
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python and http POST

2010-02-12 Thread Javier Collado
Hello,

I haven't used httplib2, but you can certainly use any other
alternative to send HTTP requests:
- urllib/urllib2
- mechanize

With regard to how do you find the form you're looking for, you may:
- create the HTTP request on your own with urllib2. To find out what
variables do you need to post, you can use tamperdata Firefox addon as
suggested (I haven't used that one) or httpfox (I have and it works
great).
- use mechanize to locate the form for you, fill the data in and click
on the submit button.

Additionally, you may wan to scrape some data that may be useful for
your requests. For that BeautifulSoup is good solution (with some
Firebug help to visually locate what you're looking for).

Best regards,
Javier

P.S. Some examples here:
http://www.packtpub.com/article/web-scraping-with-python
http://www.packtpub.com/article/web-scraping-with-python-part-2

2010/2/11 galileo228 :
> Hey All,
>
> Been teaching myself Python for a few weeks, and am trying to write a
> program that will go to a url, enter a string in one of the search
> fields, submit the search, and return the contents of the search
> result.
>
> I'm using httplib2.
>
> My two particular questions:
>
> 1) When I set my 'body' var, (i.e. 'body = {'query':'search_term'}),
> how do I know what the particular key should be? In other words, how
> do I tell python which form on the web page I'm visiting I'd like to
> fill in? Do I simply go to the webpage itself and look at the html
> source? But if that's the case, which tag tells me the name of the
> key?
>
> 2) Even once python fills in the form properly, how can I tell it to
> 'submit' the search?
>
> Thanks all!
>
> Matt
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help parsing a page with python

2010-01-27 Thread Javier Collado
Hello,

You can find some advice here:
http://www.packtpub.com/article/web-scraping-with-python-part-2

Best regards,
Javier

2010/1/27 mierdatutis mi :
> Hello again,
>
> What test case for Windmill? Can you say me the link, please?
>
> Many thanks
>
> 2010/1/27 Javier Collado 
>>
>> Hello,
>>
>> A test case for Windmill might also be used to extract the information
>> that you're looking for.
>>
>> Best regards,
>>    Javier
>>
>> 2010/1/27 mierdatutis mi :
>> > Those videos are generated by javascript.
>> > There is some parser with python for javascript???
>> >
>> > Thanks a lot!
>> >
>> >
>> > 2010/1/27 Simon Brunning 
>> >>
>> >> 2010/1/27 mierdatutis mi :
>> >> > Hi,
>> >> >
>> >> > I would like to parse a webpage to can get the url of the video
>> >> > download. I
>> >> > use pyhton and firebug but I cant get the url link.
>> >> >
>> >> > Example:
>> >> >
>> >> > The url where I have to get the video link is:
>> >> >
>> >> >
>> >> > http://www.rtve.es/mediateca/videos/20100125/saber-comer---salsa-verde-judiones-25-01-10/676590.shtml";
>> >> >
>> >> > The video is
>> >> > http://www.rtve.es/resources/TE_SSAC011/flv/8/2/1264426362028.flv
>> >> > Could you help me please?
>> >>
>> >> That URL doesn't appear to be in the HTML - it must be being brought
>> >> in by the JavaScript somehow.
>> >>
>> >> --
>> >> Cheers,
>> >> Simon B.
>> >> --
>> >> http://mail.python.org/mailman/listinfo/python-list
>> >
>> >
>> > --
>> > http://mail.python.org/mailman/listinfo/python-list
>> >
>> >
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help parsing a page with python

2010-01-27 Thread Javier Collado
Hello,

A test case for Windmill might also be used to extract the information
that you're looking for.

Best regards,
Javier

2010/1/27 mierdatutis mi :
> Those videos are generated by javascript.
> There is some parser with python for javascript???
>
> Thanks a lot!
>
>
> 2010/1/27 Simon Brunning 
>>
>> 2010/1/27 mierdatutis mi :
>> > Hi,
>> >
>> > I would like to parse a webpage to can get the url of the video
>> > download. I
>> > use pyhton and firebug but I cant get the url link.
>> >
>> > Example:
>> >
>> > The url where I have to get the video link is:
>> >
>> > http://www.rtve.es/mediateca/videos/20100125/saber-comer---salsa-verde-judiones-25-01-10/676590.shtml";
>> >
>> > The video is
>> > http://www.rtve.es/resources/TE_SSAC011/flv/8/2/1264426362028.flv
>> > Could you help me please?
>>
>> That URL doesn't appear to be in the HTML - it must be being brought
>> in by the JavaScript somehow.
>>
>> --
>> Cheers,
>> Simon B.
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: scraping with urllib2

2010-01-27 Thread Javier Collado
Hello,

To accept cookies, use the HTTPCookieProcessor as explained here:
http://www.nomadjourney.com/2009/03/automatic-site-login-using-python-urllib2/

Best regards,
Javier

2010/1/27 Andre Engels :
> On Wed, Jan 27, 2010 at 6:26 AM, Patrick  wrote:
>> I'm trying to scrape the attached link for the price listed $99.99:
>> http://bananarepublic.gap.com/browse/product.do?cid=41559&vid=1&pid=692392
>>
>> I can see the price if I view the source(I even turned off java and
>> javascript), but when I use urllib2, the price doesn't show up.
>>
>> Is there another library other than urllib2 that would work?
>
> To see that page you need to accept cookies from the site and send them back.
>
>
>
> --
> André Engels, andreeng...@gmail.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python or Ant

2010-01-26 Thread Javier Collado
Hello,

One tool that I really like is doit:
http://python-doit.sourceforge.net/

If you need to execute jobs remotely, you may like to take a look at STAF:
http://staf.sourceforge.net/index.php

Best regards,
Javier

2010/1/26 Chris Rebert :
> On Tue, Jan 26, 2010 at 10:58 AM,   wrote:
>> My company is looking at creating a tool to allow us to define and manage a
>> process for each job we run (a typical job may be look on a customers ftp
>> site for a file, download it, decrypt it and load it into our database). We
>> would like something which would allow us to glue together various existing
>> processes we currently use into a single unit with multiple steps. Along the
>> way, this new routine would need to log its progress and be able to report
>> and even handle errors. A coworker has suggested we look at Ant ("Another
>> Neat Tool") and, though it looks promising, I have reservations. If I recall
>> correctly, it was intended as a replacement for "Make" and I worry that we
>> may be trying to force Ant to be something it is not. Also, most of our code
>> base is in Python and I'd really like to stay that way, of possible.
>>
>> Are there any systems out there that will allow me to run multiple programs
>> as one process? We could write our own, of course, and the Twisted package
>> looks like it would be fun to use. Or, is Ant a viable solution to our
>> problem?
>>
>> Your constructive comments would be appreciated
>
> There are several make-replacements written in Python. They could be an 
> option.
>
> Here's a list of some of them (courtesy some googling):
> - Paver (http://www.blueskyonmars.com/projects/paver/)
> - SCons (http://www.scons.org/)
> - Vellum (https://launchpad.net/vellum)
> - Aap (http://www.a-a-p.org/)
>
> (List is in no particular order and likely incomplete; I have not
> tried any of these myself.)
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sikuli: the coolest Python project I have yet seen...

2010-01-25 Thread Javier Collado
Hello,

I think the site is under maintenance. I tried a couple of hours ago
and it worked fine.

As an alternative, I found that this link also worked:
http://www.sikuli.org/

Unfortunately, it seems it's not working right now.

Best regards,
Javier

2010/1/25 Virgil Stokes :
> On 25-Jan-2010 04:18, Ron wrote:
>>
>> Sikuli is the coolest Python project I have ever seen in my ten year
>> hobbyist career. An MIT oepn source project, Sikuli uses Python to
>> automate GUI tasks (in any GUI or GUI baed app that runs the JVM) by
>> simply drag and dropping GUI elements into Python scripts as function
>> arguments. Download at http://sikuli.csail.mit.edu/ I also did this
>>
>
> This link is broken!
>
> --V
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess troubles

2010-01-21 Thread Javier Collado
Hello,

If you set shell=False, then I think that arg2 should be separated
into two different parts.

Also, arg3 could be set just to pattern (no need to add extra spaces
or using str function).

Best regards,
Javier

2010/1/21 Tomas Pelka :
> Hey all,
>
> have a problem with following piece of code:
>
> --
> import subprocess
>
> paattern = "python"
> cmd = "/usr/bin/locate"
> arg1 = " -i"
> arg2 = " -d /var/www/books/mlocate.db"
> arg3 = str(" " + pattern)
>
> p1 = subprocess.Popen([cmd, arg1, arg2, arg3], shell=False,
> stdout=subprocess.PIPE, stderr=subprocess.PIPE)
> (stdoutdata, stderrdata) = p1.communicate()
>
> print p1.returncode
> print "%s -- %s" % (stdoutdata, stderrdata)
> --
>
> But return code is always 1 and command do not return any result/error
> (stdoutdata, stderrdata are None). If I run this command (/usr/bin/locate -i
> -d /var/www/books/mlocate.db python) from standard shell everything goes
> fine.
>
> Could you please give me an advice what I'm doing wrong?
>
> Thanks
> Cheers
>
> --
> Tom
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Symbols as parameters?

2010-01-21 Thread Javier Collado
Hello,

I'd say that isn't totally incorrect to use strings instead of
symbols. Please note that in other programming languages symbols,
atoms and the like are in fact immutable strings, which is what python
provides by default.

Best regards,
Javier

2010/1/21 Alf P. Steinbach :
> * Martin Drautzburg:
>>
>> Hello all,
>>
>> When passing parameters to a function, you sometimes need a paramter
>> which can only assume certain values, e.g.
>>
>>        def move (direction):
>>                ...
>> If direction can only be "up", "down", "left" or "right", you can solve
>> this by passing strings, but this is not quite to the point:
>>
>>        - you could pass invalid strings easily
>>        - you need to quote thigs, which is a nuisance
>>        - the parameter IS REALLY NOT A STRING, but a direction
>>
>> Alternatively you could export such symbols, so when you "import *" you
>> have them available in the caller's namespace. But that forces you
>> to "import *" which pollutes your namespace.
>>
>> What I am really looking for is a way
>>
>>        - to be able to call move(up)
>>        - having the "up" symbol only in the context of the function call
>>
>> So it should look something like this
>>
>> ... magic, magic ...
>> move(up)
>> ... unmagic, unmagic ...
>> print up
>>
>> This should complain that "up" is not defined during the "print" call,
>> but not when move() is called. And of course there should be as little
>> magic as possible.
>>
>> Any way to achieve this?
>
 def move( direction ):
> ...   print( "move " + str( direction ) )
> ...
 move( "up" )
> move up

 class using_directions:
> ...     up = 42
> ...     move( up )
> ...
> move 42
 up
> Traceback (most recent call last):
>  File "", line 1, in 
> NameError: name 'up' is not defined
 _
>
>
> Of course it's an abuse of the language. :-)
>
> So I wouldn't recommend it, but it's perhaps worth having seen it.
>
>
> Cheers & hth.,
>
> - Alf
>
> PS: I hope this was not a homework question.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Ignore leading '>>>' and ellipsis?

2010-01-14 Thread Javier Collado
Hello,

I think that's exactly what the cpaste magic function does. Type
'cpaste?' in your IPython session for more information.

Best regards,
Javier

2010/1/14 Reckoner :
>
> Hi,
>
> I am studying some examples in a tutorial where there are a lot of
> leading >>> characters and ellipsis in the text. This makes it hard to
> cut and paste into the IPython interpreter since it doesn't like these
> strings.
>
> Is there another interpreter I could use that will appropriately
> ignore and interpret these leading terms?
>
> For example, I cannot paste the following directly into the
> interpreter:
>
 d = dict(x.__array_interface__)
 d['shape'] = (3, 2, 5)
 d['strides'] = (20, 20, 4)
>
 class Arr:
> ...     __array_interface__ = d
> ...     base = x
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: When to use mechanize and Windmill library during WebScraping ?

2009-12-12 Thread Javier Collado
Hello,

If a script that uses mechanize fails to find an html node that has
been identified with Firebug, this is probably because that node has
been autogenerated (provided that the expression to get the node is
correct).

As an alternative to verify this, you can try to download the html
page and open it in your favourite editor. If some of the nodes that
you can see in your browser are missing or empty, then one of the
JavaScript scripts in the page should have created/populated it.

If you're in doubt, you can try to use mechanize and, if you have
problems such as the described above, then you can move to windmill or
some other tool that executes JavaScript code before trying to get the
desired data.

Best regards,
Javier

2009/12/11 Raji Seetharaman :
> Hi
>
> For 'Webscraping with Python' mechanize or urllib2 and windmill or selenium
> libraries are used  to download the webpages.
>
> http://www.packtpub.com/article/web-scraping-with-python
>
> The above link makes use of mechanize library to download the web pages.
>
> The below link uses windmill library to download the web pages.
>
> http://www.packtpub.com/article/web-scraping-with-python-part-2
>
> I dont know when to use mechanize or windmill library
>
> It has been said that Windmill library is used when the HTML file is auto
> generated by the JavaScript code.
>
> Also i dont know how to identify whether the HTML file is auto generated by
> the JavaScript code or not ?
>
> Suggest me
>
> Thanks
>
> Raji. S
> http://sraji.wordpress.com/
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to specify Python version in script?

2009-11-11 Thread Javier Collado
Hello,

If you are working on linux, you can change the shebang line from:
#!/usr/bin/python

to:
#!/usr/bin/python2.6

Best regards,
Javier

P.S. If you just want to avoid python 3 while running the latest
python 2.x version, this should also work:
#!/usr/bin/python2

2009/11/11 Benjamin Kaplan :
> On Wed, Nov 11, 2009 at 12:16 PM, kj  wrote:
>>
>>
>>
>>
>> I have a script that must be run with Python 2.6.x.  If one tries
>> to run it with, say, 2.5.x, *eventually* it runs into problems and
>> crashes.  (The failure is quicker if one attempts to run it with
>> Python 3.x.)
>>
>> Is there some way to specify at the very beginning of the script
>> the acceptable range of Python versions?
>>
>
> min_version = (2,6)
> import sys
> if sys.version_info < min_version :
>   print >> stderr, "must be run with at least Python 2.6"
>   sys.exit(1)
>
>
>> TIA!
>>
>> kynn
>>
>> P.S. I know that I can hardcode the path to a specific intpreter
>> in the #! line, but I'm trying to keep the code a bit more general
>> than that.
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: substituting list comprehensions for map()

2009-11-02 Thread Javier Collado
Hello,

I'll do the following:
[op1+op2 for op1,op2 in zip(operandlist1, operandlist2)]

Best regards,
Javier

2009/11/2 Jon P. :
> I'd like to do:
>
> resultlist = operandlist1 + operandlist2
>
> where for example
>
> operandlist1=[1,2,3,4,5]
> operandlist2=[5,4,3,2,1]
>
> and resultlist will become [6,6,6,6,6].  Using map(), I
> can do:
>
> map(lambda op1,op2: op1 + op2, operandlist1, operandlist2)
>
> Is there any reasonable way to do this via a list comprehension ?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: the usage of 'yield' keyword

2009-10-14 Thread Javier Collado
Hello,

I think that the best information available on the subject is the following:
http://www.dabeaz.com/generators/
http://www.dabeaz.com/coroutines/

Best regards,
Javier

2009/10/14 Peng Yu :
> http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt
>
> The explanation of yield is not clear to me, as I don't know what a
> generator is. I see the following example using 'yield'. Could
> somebody explain how 'yield' works in this example? Thank you!
>
> def brange(limit):
>  i = 0
>  while i < limit:
>      yield i
>      i += 1
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where to find pexpect

2009-10-13 Thread Javier Collado
Hello,

Google is your friend:
http://sourceforge.net/projects/pexpect/
http://sourceforge.net/projects/pexpect/files/pexpect/Release%202.3/pexpect-2.3.tar.gz/download

Best regards,
Javier

2009/10/13 Antoon Pardon :
> I have been looking for pexpect. The links I find like
> http://pexpect.sourceforge.net all end up at
> http://www.noah.org/wiki/Pexpect which produces a 404 not
> found problem.
>
> Does someone know the current location?
>
> --
> Antoon Pardon
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Distributing Python-programs to Ubuntu users

2009-09-25 Thread Javier Collado
Hello,

I recommend you to check this:
https://wiki.ubuntu.com/PackagingGuide/Complete

The best way to release the software to Ubuntu users is by means of a
PPA (https://help.launchpad.net/Packaging/PPA) so that people can
track your application updates automatically. Before the PPA is
created you need to have a launchpad account and a sign the Ubuntu
Code of Conduct. However isn't that hard and you just have to do all
this setup for the first time.

A tool that might be used to automate package creation for an
application is Quickly (https://wiki.ubuntu.com/Quickly). However, if
the project development has already started, probably it won't be
useful for you.

Best regards,
Javier

2009/9/25 Olof Bjarnason :
> Hi!
>
> I write small games in Python/PyGame. I want to find a way to make a
> downloadable package/installer/script to put on my webpage, especially
> for Ubuntu users.
>
> I've skimmed a couple of tutorials on how to generate .deb-files, but,
> wow, it's a whole new skill set to do that!
>
> Does anyone have any hint on a more economic way of creating
> single-file distribution packages for Python+PyGame projects? Maybe
> some GUI-tool that automates the .deb file creation process, but
> targetting Python specifically and not C++.
>
> /Olof
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lambda functions

2009-08-31 Thread Javier Collado
Hello,

This page has some advice about how to avoid some of the lambda
functions limitations:
http://p-nand-q.com/python/stupid_lambda_tricks.html

In particular, it suggests to use map function instead of for loops.

Best regards,
Javier

2009/8/31 Pierre :
> Hello,
>
> I would like to know if it is possible to define a loop in a lambda
> function
>
> How to manage the indents ? Example :
> s_minus_1 = lambda s : for index in range(0, len(s)) : s[index] = s
> [index]-1
>
>
> Thanks !
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OptionParser How to: prog [options] [arguments]

2009-08-14 Thread Javier Collado
Hello,

I think that this isn't possible with optparse library.

However, it's possible with argparse (http://code.google.com/p/argparse/):
 http://argparse.googlecode.com/svn/trunk/doc/other-methods.html#sub-commands

It's not a standard library, but it's worth to take a look at it.

Best regards,
Javier

2009/8/14 Steven Woody :
> Hi,
> I am using OptionParser, but I've not managed figure out a way to support
> what I wanted command line format "prog  [options] [arguments]".
> E.g., "svn ls -r123 http://hello.world".   Can I do this using OptionParser?
> Thanks.
> --
> Life is the only flaw in an otherwise perfect nonexistence
>    -- Schopenhauer
>
> narke
> public key at http://subkeys.pgp.net:11371 (narkewo...@gmail.com)
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Non-blocking read with popen subprocess

2009-07-31 Thread Javier Collado
Hello,

According to my experience and from what I've read in other threads,
subprocess isn't easy to use for interactive tasks. I don't really
know, but maybe it wasn't even designed for that at all.

On the other hand, pexpect seems to work fine for interactive use and
even provides a method for nonblocking reads, so I think that you
should consider to take a look at it:
http://pexpect.sourceforge.net/pexpect.html#spawn-read_nonblocking

Best regards,
Javier

2009/7/31 Dhanesh :
> Hi ,
>
> I am trying to use subprocess popen on a windows command line
> executable with spits messages on STDOUT as well as STDIN. Code
> snippet is as below :-
> ##
> sOut=""
>    sErr=""
>    javaLoaderPath = os.path.join("c:\\","Program Files","Research In
> Motion","BlackBerry JDE 4.7.0","bin","Javaloader.exe")
>    cmd = [javaLoaderPath,'-u','load','helloworld.jad']
>    popen = subprocess.Popen
> (cmd,bufsize=256,shell=False,stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
>    while True:
>        sErr = sErr + popen.stderr.read(64)
>        sOut = sOut + popen.stdout.read(64)--> Blocks
> here
>        if None != popen.poll():
>            break;
> ##
>
> I observed that python scripts blocks at stdout read on the other hand
> when I do not create a child stdout PIPE  say  " stdout=None" , things
> seems to be working fine.
>
> how can I we have a non blocking read ?
> And why does stdout block even where data is available to be read
> ( which seem to be apparent when stdout=None, and logs appear on
> parents STDOUT) ?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


string.Template issue

2009-07-30 Thread Javier Collado
Hello,

In the string.Template documentation
(http://docs.python.org/library/string.html) it's explained that if a
custom regular expression for pattern substitution is needed, it's
possible to override idpattern class attribute (whose default value is
[_a-z][_a-z0-9]*).

However, if the custom pattern that is needed is just uppercase
letters something like [A-Z]+ won't work because of the following line
in the _TemplateMetaclass class __init__ method:
cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE)

I would say that this is an error (IGNORECASE just shouldn't be there)
and that the line above should be:
cls.pattern = _re.compile(pattern, _re.VERBOSE)
and the default value for idpattern:
[_a-zA-Z][_a-zA-Z0-9]*

Do you agree on this? Is there any reason for the IGNORECASE option to
be passed to re.compile?

Best regards,
Javier
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Config files with different types

2009-07-03 Thread Javier Collado
Hello,

Have you considered using something that is already developed?

You could take a look at this presentation for an overview of what's available:
http://us.pycon.org/2009/conference/schedule/event/5/

Anyway, let me explain that, since I "discovered" it, my favourite
format for configuration files is yaml (http://yaml.org/,
http://pyyaml.org/). It's easy to read, easy to write, available in
different programming languagues, etc. In addition to this, type
conversion is already in place so I think it covers your requirement.
For example:

IIn [1]: import yaml

In [2]: yaml.load("""name: person name
   ...: age: 25
   ...: is_programmer: true""")
Out[2]: {'age': 25, 'is_programmer': True, 'name': 'person name'}

Best regards,
Javier

2009/7/2 Zach Hobesh :
> Hi all,
>
> I've written a function that reads a specifically formatted text file
> and spits out a dictionary.  Here's an example:
>
> config.txt:
>
> Destination = C:/Destination
> Overwrite = True
>
>
> Here's my function that takes 1 argument (text file)
>
> the_file = open(textfile,'r')
> linelist = the_file.read().split('\n')
> the_file.close()
> configs = {}
> for line in linelist:
>       try:
>              key,value = line.split('=')
>              key.strip()
>              value.strip()
>              key.lower()
>              value.lower()
>              configs[key] = value
>
>       except ValueError:
>              break
>
> so I call this on my config file, and then I can refer back to any
> config in my script like this:
>
> shutil.move(your_file,configs['destination'])
>
> which I like because it's very clear and readable.
>
> So this works great for simple text config files.  Here's how I want
> to improve it:
>
> I want to be able to look at the value and determine what type it
> SHOULD be.  Right now, configs['overwrite'] = 'true' (a string) when
> it might be more useful as a boolean.  Is there a quick way to do
> this?  I'd also like to able to read '1' as an in, '1.0' as a float,
> etc...
>
> I remember once I saw a script that took a string and tried int(),
> float() wrapped in a try except, but I was wondering if there was a
> more direct way.
>
> Thanks in advance,
>
> Zach
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: packaging apps

2009-06-30 Thread Javier Collado
Hello,

Regarding packaging for debian (.deb), the best reference I've found is:
https://wiki.ubuntu.com/PackagingGuide/Python

However, all that mess probably won't be needed anymore once this is finished:
https://blueprints.edge.launchpad.net/ubuntu/+spec/desktop-karmic-automagic-python-build-system

Best regards,
   Javier

2009/6/30 Ronn Ross :
> I have a simple application that has a glade file and a .py file. How would
> I package that into an installer for Windows, Mac, and a deb file? Can
> anyone point me in the right direction?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Making code run in both source tree and installation path

2009-06-29 Thread Javier Collado
Hello,

I would like to be able to run the main script in a python project
from both the source tree and the path in which it's installed on
Ubuntu. The script, among other things, imports a package which in
turns makes use of some data files that contains some metadata that is
needed in xml format.

The source tree has an structure such as this one:
setup.py
debian/ (packaging files)
src/ (source code)
src/lib (package files)
src/data (data files)
src/bin (main script)

However, when the project is installed using setup.py install, the
directory structure is approximately this way:
/usr/local/bin (main script)
/usr/local/share/ (data files)
/usr/local/lib/python2.x/dist-packages/ (library files)

And when installing the code through a package, the structure is the
same one, but removing "local".

Hence, the data files aren't always in the same relative directories
depending on we're executing code from the source tree or from the
installation. To make it possible to run the code from both places,
I've seen different approaches:
- distutils trick in setup.py to modify the installed script (i.e.
changing a global variable value) so that it has a reference to the
data files location.
- Heuristic in the package code to detect when it's being executed
from the source tree and when it has been the installed
- Just using an environment variable that the user must set according
to his needs

I guess that there are other options, for example, maybe using
buildout. What would you say it's the best/more elegant option to
solve this problem?

Best regards,
   Javier
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calling subprocess with arguments

2009-06-19 Thread Javier Collado
Hello,

The problem might be that, aside from creating the Popen object, to
get the command run you need to call 'communicate' (other options, not
used with the Popen object directly, are 'call' or 'waitpid' as
explained in the documentation). Did you do that?

Best regards,
Javier

2009/6/19 Tyler Laing :
> So no one has an answer for why passing flags and the values the flags need
> through subprocess does not work? I would like an answer. I've examined all
> the examples I could find online, which were all toy examples, and not
> helpful to my problem.
>
> On Thu, Jun 18, 2009 at 7:40 PM, Tyler Laing  wrote:
>>
>> I've been trying any variation I can think of to do this properly, but
>> here's my problem:
>>
>> I want to execute this command string: vlc -I rc
>>
>> This allows vlc to be controlled via  a remote interface instead of the
>> normal gui interface.
>>
>> Now, say, I try this from subprocess:
>>
>> >>>p=subprocess.Popen('vlc -I rc test.avi'.split(' '), shell=False,
>> >>> stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>
>> But I don't get the remote interface. I get the normal gui interface. So
>> how do I do it? I've tried passing ['vlc', '-I', 'rc'], I've tried ['-I',
>> 'rc'] with executable set to 'vlc'. I've had shell=True, I've had
>> shell=False. I've tried all these combinations.
>>
>> What am I doing wrong?
>>
>> --
>> Visit my blog at http://oddco.ca/zeroth/zblog
>
>
>
> --
> Visit my blog at http://oddco.ca/zeroth/zblog
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about None

2009-06-12 Thread Javier Collado
Hello,

You're right, types.NoneType is not available in python 3.0, I wasn't
aware of that change. Thanks for pointing it out.

Best regards,
Javier

2009/6/12 Jeff McNeil :
> On Jun 12, 10:05 am, Paul LaFollette 
> wrote:
>> Kind people,
>>
>> Using Python 3.0 on a Gatesware machine (XP).
>> I am building a class in which I want to constrain the types that can
>> be stored in various instance variables.  For instance, I want to be
>> certain that self.loc contains an int.  This is straightforward (as
>> long as I maintain the discipline of changing loc through a method
>> rather than just twiddling it directly.
>>
>>   def setLoc(lo):
>>     assert isinstance(lo, int), "loc must be an int"
>>     self.loc = lo
>>
>> does the trick nicely.
>>
>> However, I also want to constrain self.next to be either an instance
>> of class Node, or None.  I would think that the following should work
>> but it doesn't.
>>
>>   def setNext(nxt):
>>     assert isinstance(nxt, (Node, NoneType)), "next must be a Node"
>>     self.next = nxt
>>
>> since type(Node) responds with  but the assertion
>> above gives "name 'NoneType' is not defined" suggesting that NoneType
>> is some sort of quasi-class.
>>
>>   def setNext(nxt):
>>     assert nxt==None or isinstance(nxt, Node), "next must be a Node"
>>     self.next = nxt
>>
>> works ok, but it's uglier than it ought to be.
>>
>> So, I have three questions.
>>
>> 1) Why doesn't isinstance(nxt, (Node, NoneType)) work?
>> 2) is their a less ugly alternative that what I am using?
>> 3) (this is purely philosophical but I am curious)  Would it not be
>> more intuitive if
>> isinstance(None, ) returned true?
>>
>> Thank you for your kind attention.
>> Paul
>
> 1. The problem is described clearly by that Exception. The 'NoneType'
> name isn't bound to any objects at that current scope.  I know that
> with 2.6, you can import 'types.NoneType' but I don't believe the 3.0
> types module includes NoneType.  Someone else will have to clarify
> that as I don't have 3.0 installed.
>
> 2. A less ugly alternative? You could use the accepted method of
> testing against None:
>
> if var is None:
>    do_stuff()
>
> The use of the 'is' operator checks whether objects are exactly the
> same (id(var) == id(None)) as opposed to 'isinstance' or '==.'
>
> You might also try defining descriptors in order to make your type
> checks slightly more transparent. That might be confusing to other
> users of your class, though. Not many people expect a TypeError from
> an attribute assignment.
>
> 3. None is an instance of NoneType. Why should isinstance return True
> when it's tested against other types?
>
> HTH,
>
> Jeff
> mcjeff.blogspot.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about None

2009-06-12 Thread Javier Collado
Hello,

This should work for you:

In [1]: import types

In [2]: isinstance(None, types.NoneType)
Out[2]: True


Best regards,
Javier

2009/6/12 Paul LaFollette :
> Kind people,
>
> Using Python 3.0 on a Gatesware machine (XP).
> I am building a class in which I want to constrain the types that can
> be stored in various instance variables.  For instance, I want to be
> certain that self.loc contains an int.  This is straightforward (as
> long as I maintain the discipline of changing loc through a method
> rather than just twiddling it directly.
>
>  def setLoc(lo):
>    assert isinstance(lo, int), "loc must be an int"
>    self.loc = lo
>
> does the trick nicely.
>
> However, I also want to constrain self.next to be either an instance
> of class Node, or None.  I would think that the following should work
> but it doesn't.
>
>  def setNext(nxt):
>    assert isinstance(nxt, (Node, NoneType)), "next must be a Node"
>    self.next = nxt
>
> since type(Node) responds with  but the assertion
> above gives "name 'NoneType' is not defined" suggesting that NoneType
> is some sort of quasi-class.
>
>  def setNext(nxt):
>    assert nxt==None or isinstance(nxt, Node), "next must be a Node"
>    self.next = nxt
>
> works ok, but it's uglier than it ought to be.
>
> So, I have three questions.
>
> 1) Why doesn't isinstance(nxt, (Node, NoneType)) work?
> 2) is their a less ugly alternative that what I am using?
> 3) (this is purely philosophical but I am curious)  Would it not be
> more intuitive if
> isinstance(None, ) returned true?
>
> Thank you for your kind attention.
> Paul
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getop or optparse with option with spaces?

2009-06-10 Thread Javier Collado
Hello,

It's strange behaviour. Have you tried argparse
(http://code.google.com/p/argparse/)? I've been using it for long time
without any problem like that?

Best regards,
Javier

2009/6/10 David Shapiro :
> Hello,
>
> I have been trying to find an example of how to deal with options that have 
> spaces in them.  I am using jython, which is the same I think as python 
> 2.2.3.   I feebly tried to use optparse and argparse with no success (got 
> gettext, locale, and optparse).   The code is as follows:
>
>    try:
>        (opts, args) = getopt.getopt(sys.argv[1:], "hs:p:n:j:d:l:u:c:t:w:q:v",
> ["help", "server=", "port=", 
> "dsName=","jndiName=","driverName=","driverURL=","user=","passWD=","targetServer=","whereProp=","testTableName=","version"])
>    except getopt.GetoptError:
>        usage()
>
>    for opt in opts:
>        (key, value) = opt
>        if (key in ("-v", "--version")):
>                print "Version: " + version
>                sys.exit(1)
>        if (key in ("-h", "--help")):
>                usage()
>        if (key in ("-s", "--server")):
>                server = value
>        if (key in ("-p", "--port")):
>                port = value
>        if (key in ("-n", "--dsName")):
>                dsName = value
>        if (key in ("-j", "--jndiName")):
>                jndiName = value
>        if (key in ("-d", "--driverName")):
>                driverName = value
>        if (key in ("-l", "--driverURL")):
>                driverURL = value
>        if (key in ("-u", "--user")):
>                user = value
>        if (key in ("-c", "--passWD")):
>                passWD = value
>        if (key in ("-t", "--targetServer")):
>                targetServer = value
>        if (key in ("-q", "--testTableName")):
>                testTableName = value
>        if (key in ("-w", "--whereProp")):
>                whereProp = value
>
>
> print "server: " + server
> print "port: " + port
> print "dsName: " + dsName
> print "jndiName: " + jndiName
> print "driverName: " + driverName
> print "driverURL: " + driverURL
> print "user: " + user
> print "passWD: " + passWD
> print "testtable: " + testTableName
> print "targetServer: " + targetServer
> print "whereProp: " + whereProp
>
> The one that gives me trouble is with the -q option, because it can look 
> like: -q "SQL 1 TABLE".  It returns back just SQL.  How do I get it to return 
> the whole thing that is in double quotes?  Another problem is that whereProp 
> value is just not seen. Is there a limit to the size for argv?
>
> If I use optparse instead of getopt, I see that SQL 1 TABLE goes into args 
> instead of values by the way.  A temporary workaround is to use " 
> ".join(args) and assign that to testTableName, but I worry about what will 
> happen if testTableName is blank or has something with no spaces.  Also, it 
> just seem weird I have to do a work around like that.  I could have swore 
> using double quotes should have fixed this issue, but they do not seem to 
> work.
>
> David
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Start the interactive shell within an application

2009-06-09 Thread Javier Collado
Take a look either at code.interact or at
IPython.ipapi.launch_new_instance. Basically, the only thing that you
have to provide is a dictionary object that contains the namespace
that you would like to have in your shell once it's launched.

Best regards,
Javier

2009/6/9 eGlyph :
> On Jun 9, 11:49 am, Jean-Michel Pichavant 
> wrote:
>> I'm sometimes tired of adding prints to scan the current namespace so
>> I'd like to pause the execution and give the user the shell prompt.
>> This is obviously for debugging purpose.
>
> This is definitely doable, have look at rhythmbox or gedit - they
> provide an interactive console.
> Also, have a look at IPython, they have a recipe and an example of
> embedding IPython into a user's application.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PYTHONPATH and multiple python versions

2009-06-05 Thread Javier Collado
Hello,

I think that virtualenv could also do the job.

Best regards,
Javier

2009/6/5 Red Forks :
> maybe a shell script to switch PYTHONPATH, like:
> start-python-2.5
> start-python-2.4 ...
> On Fri, Jun 5, 2009 at 4:56 PM, David Cournapeau  wrote:
>>
>> Hi,
>>
>> As I don't have admin privileges on my main dev machine, I install a
>> good deal of python modules somewhere in my $HOME, using PYTHONPATH to
>> point my python intepreter to the right location. I think PEP370
>> (per-user site-packages) does exactly what I need, but it works only
>> for python 2.6 and above. Am I out of luck for versions below ?
>>
>> David
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Global variables from a class

2009-05-29 Thread Javier Collado
You're right. I agree on that it's important to use proper words.
Thanks for the correction.

Best regards,
Javier

2009/5/29 Steven D'Aprano :
> On Fri, 29 May 2009 12:04:53 +0200, Javier Collado wrote:
>
>> Hello,
>>
>> First thing is a class variable (one for every instance) and second one
>> an instance variable (one per instance).
>
> One of these things don't belong:
>
> A string variable is a variable holding a string.
> A float variable is a variable holding a float.
> An int variable is a variable holding an int.
> A list variable is a variable holding a list.
> A "class variable" is an attribute of a class object, holding an
>  object of arbitrary type, which is shared by all instances
>  of the class.
>
> Please don't use the term "class variable" to mean class attribute.
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Global variables from a class

2009-05-29 Thread Javier Collado
Hello,

First thing is a class variable (one for every instance) and second
one an instance variable (one per instance).

For further information, please take a look at:
http://diveintopython.org/object_oriented_framework/class_attributes.html

Best regards,
   Javier

2009/5/29 Kless :
> I usually use a class to access to global variables. So, which would
> be the correct way to set them --since the following classes--:
>
> 
> class Foo:
>   var = 'lala'
>
> class Bar:
>   def __init__(self):
>      self.var = 'lele'
> 
>
> Or is it the same?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a GUI for informing a user of missing dependencies?

2009-05-12 Thread Javier Collado
Hello,

I'm not an expert, but if you use setuptools for your application,
then a 'install_requires' argument would do the job. For more
information, please take a look at:
http://peak.telecommunity.com/DevCenter/setuptools

Best regards,
Javier

2009/5/12 Jason :
> I'm writing a small GUI (Python 2.5/6) using wxPython and pySerial.
> Since these are both 3rd party modules, if a user doesn't have them
> installed and tries to run the GUI from a file browser, the app will
> fail silently. I'm hoping to avoid that.
>
> Sure, I'll have a readme file, but I was wondering if there is an even
> more direct way. I was going to roll my own (eg. very simple Tkinter
> fallback error dialog if wxPython import fails, wxPython error dialog
> if anything else is missing); but I was wondering if such a system
> already exists out there.
>
> — Jason
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list