python and web pages

2009-11-18 Thread Daniel Dalton
Hi,

Here is my situation:
I'm using the command line, as in, I'm not starting gnome or kde (I'm on
linux.)
I have a string of text attached to a variable,. So I need to use one of
the browsers on linux, that run under the command line, eg. lynx,
elinks, links, links2 and do the following.
1. Open a certain web page and find the first text box on the page, and
put this text into the form.
2. Press the submit button, and wait for the result page to load.
3. Click on the 15th link down the page.

So, how do I do this, can anyone point me to some docs or modules that
may help out here?

Thank you,

-- 
Cheers,
Dan

http://members.iinet.net.au/~ddalton/

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


Re: ANN: Urwid 0.9.9 - Console UI Library

2009-11-18 Thread Michel Claveau - MVP
Hi! 

You forget to write "urwid" do not run under Windows.

@-salutations
-- 
Michel Claveau
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: Urwid 0.9.9 - Console UI Library

2009-11-18 Thread Michel Claveau - MVP
Hi! 

You forget to write "urwid" do not run under Windows.

@-salutations
-- 
Michel Claveau
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: Urwid 0.9.9 - Console UI Library

2009-11-18 Thread Michel Claveau - MVP
Hi! 

You forget to write "urwid" do not run under Windows.

@-salutations
-- 
Michel Claveau
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IOError: [Errno 28] No space left on device

2009-11-18 Thread Terry Reedy

hong zhang wrote:



"cont_x --" doesn't work. So the above can't be the

actual code.

You never want to post the actual code you're
running.  That would make it too easy for people to help.



It is typo.


To avoid typos, copy and paste, as has been suggested many times.

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


Re: is there any FIX message handle modules in Python?

2009-11-18 Thread alex23
On Nov 19, 3:21 pm, "Stephen.Wu" <54wut...@gmail.com> wrote:
> FIX message is the "Financial information Exchange" protocol
> messages...
> any 3rd libs we have?

You mean like this one that was the first result when I googled
'python "financial information exchange"'?

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


is there any FIX message handle modules in Python?

2009-11-18 Thread Stephen.Wu
FIX message is the "Financial information Exchange" protocol
messages...
any 3rd libs we have?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: DeprecationWarning on md5

2009-11-18 Thread Zeynel
Thanks. I tried the suppress it but no success. I need to read the
documentation more carefully. But since this is not error, I will
ignore them for now.


On Nov 18, 9:12 pm, Chris Rebert  wrote:
> On Wed, Nov 18, 2009 at 5:23 PM, Zeynel  wrote:
> > Hello,
>
> > I am a newbie both in Scrapy and Python. When I create a project with
> > Scrapy I get these errors:
>
> > C:\Python26\lib\site-packages\twisted\python\filepath.py:12:
> > DeprecationWarning: the sha module is deprecated; use the hashlib
> > module instead import sha
> > C:\Python26\lib\site-packages\twisted\spread\pb.py:30:
> > DeprecationWarning: the md5 module is deprecated; use hashlib instead
> > import md5
> > C:\Python26\lib\site-packages\twisted\mail\smtp.py:10:
> > DeprecationWarning: the MimeWriter module is deprecated; use the email
> > package instead
>
> > I found several references to this "bug" but I could not understand
> > how to fix it. Can anyone help?
>
> This is a "bug" in the version of Twisted you're using. What Python is
> saying is that Twisted is using some modules from the Python stdlib
> that are deprecated and will be removed in some future version of
> Python, thus Twisted will /eventually/ need to be changed to use the
> newer replacement library.
> However, this is only a warning, not an error; the code will run just
> fine until you update to the eventual Python version that removed said
> deprecated libraries (by which time a new Twisted version will
> probably be available with the necessary fixes made).
>
> So, basically you can safely ignore the warnings, unless you want to
> custom-patch your Twisted installation, which I wouldn't particularly
> recommend.
> It's also entirely possible your Twisted is outdated and a new version
> fixed to avoid using the deprecated modules is already available.
>
> If you want to suppress the output of the warnings, see the docs for
> the `warnings` module:http://docs.python.org/library/warnings.html
>
> Cheers,
> Chris
> --http://blog.rebertia.com

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


Re: Beautifulsoup code that is not running

2009-11-18 Thread Zeynel
Yes, it shows as empty string. But I learned about Scrapy and now I am
studying the tutorial. I like it better than BeautifulSoup. For
beginners it is better, I think.

On Nov 18, 11:50 am, Peter Pearson  wrote:
> On Tue, 17 Nov 2009 14:38:55 -0800 (PST), Zeynel  wrote:
>
> [snip]
>
>  from BeautifulSoup import BeautifulSoup
>
>  soup = BeautifulSoup (file("test.html").read())
>  title = soup.find('title')
>  titleString = title.string
>  open('extract.text', 'w').write(titleString)
>
> > This runs without an error, but nothing is written into the
> > extract.text file. test.html has  tags in it.
>
> Hmm.  Works for me, but of course I don't have your test.html.
> Why don't you try examining "title" and "titleString"?  Perhaps
>  has resulted in titleString being the empty string.
>
> --
> To email me, substitute nowhere->spamcop, invalid->net.

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


mechanize login problem with website

2009-11-18 Thread elca

Hello

I'm making auto-login script by use mechanize python.

Before I was used mechanize with no problem, but http://www.gmarket.co.kr in
this site I couldn't make it .

whenever i try to login always login page was returned even with correct
gmarket id , pass, i can't login and I saw some suspicious message

"top.location.reload();"

I think this related with my problem, but don't know exactly how to handle .

i was upload my script in here
http://paste.pocoo.org/show/151607/

if anyone can help me much appreciate thanks in advance

-- 
View this message in context: 
http://old.nabble.com/mechanize-login-problem-with-website-tp26420202p26420202.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Qt Python radiobutton: activate event

2009-11-18 Thread Threader Slash
Hi Guys,

I am trying to get the choice made by the user on Python Qt with
radiobutton.

QtCore.QObject.connect(self.radioButton1,
QtCore.SIGNAL("toggled()"),self.radio_activateInput)
QtCore.QObject.connect(self.radioButton2,
QtCore.SIGNAL("toggled()"),self.radio_activateInput)

and that

QtCore.QObject.connect(self.performGroupBox,
QtCore.SIGNAL("toggled()"),self.radio_activateInput)

But it didn't make anything when I click on the option.

Yes, I have enabled it:

self.radioButton1.setCheckable(True)
self.radioButton1.setChecked(True)
self.radioButton2.setCheckable(True)

Any suggestion?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the naming convention for accessor of a 'private' variable?

2009-11-18 Thread Chris Rebert
On Wed, Nov 18, 2009 at 7:04 PM, Peng Yu  wrote:
> On Wed, Nov 18, 2009 at 8:47 PM, Chris Rebert  wrote:
>> On Wed, Nov 18, 2009 at 6:27 PM, Peng Yu  wrote:
>>> http://www.python.org/dev/peps/pep-0008/
>>>
>>> The above webpage states the following naming convention. Such a
>>> variable can be an internal variable in a class. I'm wondering what is
>>> the naming convention for the method that access such variable.
>>>
>>>    - _single_leading_underscore: weak "internal use" indicator.  E.g. "from 
>>> M
>>>      import *" does not import objects whose name starts with an underscore.
>>
>> If there's a method to access the variable, then it's not all that
>> private, is it?
>> Accessor methods are not Pythonic. Just make the attribute public by
>> not prefixing it with an underscore.
>>
>> See also "Python is Not Java":
>> http://dirtsimple.org/2004/12/python-is-not-java.html
>
> I don't quite understand the following paragraph from the above
> webpage. Would you please give me an example to help me understand it?
>
> "Here's what you do. You write a function that contains a function.
> The inner function is a template for the functions that you're writing
> over and over again, but with variables in it for all the things that
> vary from one case of the function to the next. The outer function
> takes parameters that have the same names as those variables, and
> returns the inner function. Then, every place where you'd otherwise be
> writing yet another function, simply call the outer function, and
> assign the return value to the name you want the "duplicated" function
> to appear. Now, if you need to change how the pattern works, you only
> have to change it in one place: the template."

#untested off-the-cuff example:

from time import time

def add_timing(func): # takes a function as an argument
"""Wraps functions so they output their execution time when called"""
def wrapper(*args, **kwargs): #defines a new function
start = time()
# thanks to closures, we can access func here
result = func(*args, **kwargs) # have the new function call
the given function
end = time()
duration = end-start
print "Call to", func.__name__, "took", duration, "seconds"
return result
return wrapper # return the new function so that it can replace
the function it calls

def long_calculation(x, y, z):
# some code

long_calculation = add_timing(long_calculation)

print long_calculation(1, 7, 42)

#Example output:
#Call to long_calculation took 135.5 seconds
#99

Now imaging applying add_timing to multiple functions and note the
amount of code you'd save and how the unrelated concerns of timing and
mathematical calculation (or whatever the functions you apply
add_timing() to do) would then be cleanly separated.
The same technique can be applied to logging and other repetitive code
to factor it out.
It's sort of like aspect-oriented programming, but not as kludgey.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: convert a string to a regex?

2009-11-18 Thread Peng Yu
On Wed, Nov 18, 2009 at 9:12 PM, Tim Chase
 wrote:
>> There are many special characters listed on
>> http://docs.python.org/library/re.html
>>
>> I'm wondering if there is a convenient function that can readily
>> convert a string with the special characters to its corresponding
>> regex. For example,
>>
>> "some.thing" => "some\.thing"
>
> Did you try bothering to *read* the page you linked to?
>
> There's a function for escaping strings right there...
>
> Literacy...a worthwhile skill to obtain.

Sorry, I didn't see it. If there are examples besides explanations for
each command, it will help people see the usage of each command more
easily.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: convert a string to a regex?

2009-11-18 Thread Tim Chase

There are many special characters listed on
http://docs.python.org/library/re.html

I'm wondering if there is a convenient function that can readily
convert a string with the special characters to its corresponding
regex. For example,

"some.thing" => "some\.thing"


Did you try bothering to *read* the page you linked to?

There's a function for escaping strings right there...

Literacy...a worthwhile skill to obtain.

-tkc


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


Re: convert a string to a regex?

2009-11-18 Thread Chris Rebert
On Wed, Nov 18, 2009 at 6:51 PM, Peng Yu  wrote:
> There are many special characters listed on
> http://docs.python.org/library/re.html
>
> I'm wondering if there is a convenient function that can readily
> convert a string with the special characters to its corresponding
> regex. For example,
>
> "some.thing" => "some\.thing"

If you'd taken 45 seconds to scan the documentation:

http://docs.python.org/library/re.html#re.escape

re.escape(string)
Return string with all non-alphanumerics backslashed; this is
useful if you want to match an arbitrary literal string that may have
regular expression metacharacters in it.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the naming convention for accessor of a 'private' variable?

2009-11-18 Thread Ben Finney
Peng Yu  writes:

> The above webpage states the following naming convention. Such a
> variable can be an internal variable in a class. I'm wondering what is
> the naming convention for the method that access such variable.

A property http://docs.python.org/library/functions.html#property>
named with a regular public attribute name::

class Foo(object):

def __init__(self, spam):
self._spam = spam

@property
def spam(self):
return self._spam

-- 
 \  “The only tyrant I accept in this world is the still voice |
  `\  within.” —Mahatma Gandhi |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the naming convention for accessor of a 'private' variable?

2009-11-18 Thread Peng Yu
On Wed, Nov 18, 2009 at 8:47 PM, Chris Rebert  wrote:
> On Wed, Nov 18, 2009 at 6:27 PM, Peng Yu  wrote:
>> http://www.python.org/dev/peps/pep-0008/
>>
>> The above webpage states the following naming convention. Such a
>> variable can be an internal variable in a class. I'm wondering what is
>> the naming convention for the method that access such variable.
>>
>>    - _single_leading_underscore: weak "internal use" indicator.  E.g. "from M
>>      import *" does not import objects whose name starts with an underscore.
>
> If there's a method to access the variable, then it's not all that
> private, is it?
> Accessor methods are not Pythonic. Just make the attribute public by
> not prefixing it with an underscore.
>
> See also "Python is Not Java":
> http://dirtsimple.org/2004/12/python-is-not-java.html

I don't quite understand the following paragraph from the above
webpage. Would you please give me an example to help me understand it?

"Here's what you do. You write a function that contains a function.
The inner function is a template for the functions that you're writing
over and over again, but with variables in it for all the things that
vary from one case of the function to the next. The outer function
takes parameters that have the same names as those variables, and
returns the inner function. Then, every place where you'd otherwise be
writing yet another function, simply call the outer function, and
assign the return value to the name you want the "duplicated" function
to appear. Now, if you need to change how the pattern works, you only
have to change it in one place: the template."
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Invitation to connect on LinkedIn

2009-11-18 Thread Chris Rebert
On Wed, Nov 18, 2009 at 6:16 PM, Jim Qiu  wrote:
>
> LinkedIn
>
> Jim Qiu requested to add you as a connection on LinkedIn:
>
> Jaime,
>
> I'd like to add you to my professional network on LinkedIn.
>
> - Jim
>
>
> Accept View invitation from Jim Qiu
>
>
> WHY MIGHT CONNECTING WITH JIM QIU BE A GOOD IDEA?
>
> Jim Qiu's connections could be useful to you
> After accepting Jim Qiu's invitation, check Jim Qiu's connections to see who 
> else you may know and who you might want an introduction to. Building these 
> connections can create opportunities in the future.
>
>
>
> © 2009, LinkedIn Corporation
>
> --
> http://mail.python.org/mailman/listinfo/python-list

Could one of the list admins please reset the password and change the
email on this guy's LinkedIn account?
How the heck someone sets their account email to a mailinglist, I'll
never figure out.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the naming convention for accessor of a 'private' variable?

2009-11-18 Thread Peng Yu
On Wed, Nov 18, 2009 at 8:50 PM, Benjamin Kaplan
 wrote:
> On Wed, Nov 18, 2009 at 9:27 PM, Peng Yu  wrote:
>> http://www.python.org/dev/peps/pep-0008/
>>
>> The above webpage states the following naming convention. Such a
>> variable can be an internal variable in a class. I'm wondering what is
>> the naming convention for the method that access such variable.
>>
>>    - _single_leading_underscore: weak "internal use" indicator.  E.g. "from M
>>      import *" does not import objects whose name starts with an underscore.
>> --
>
> You never have any convention for a method that accesses the private
> variable. In a language that has scope declarations, a method that
> uses private variables is still declared public or private- there's no
> special keyword for it.

I knew this. That's why I put quote around 'private' in the email subject.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: non-copy slices

2009-11-18 Thread Rami Chowdhury
On Wednesday 18 November 2009 17:47:09 tbour...@doc.ic.ac.uk wrote:
> Hi,
> 
> sth == something :) sorry for the abbreviation. I'm talking about the
> shallow copy, still it's a copy. 

I'm not sure you're understanding the point others have been making. A 
list item is merely another reference to an existing object -- it 
doesn't copy the object in any way.

>  Unnecessary in my case and the worst
>  part in my scenario is the creation (allocation)>  and deletion of a
>  very large number of lists of moderate size (a few hundred objects)
>  generated due to slices, while I only need to have a restricted view
>  on the original list. 
>  The islice class partially solves the problem
>  as I mentioned in the previous emails.
> 
> Cheers,
> Themis
> 
> On Wed, Nov 18, 2009 at 3:44 PM, Ethan Furman  
wrote:
> > tbour...@doc.ic.ac.uk wrote:
> > > Hi,
> > >
> > > I was looking for a facility similar to slices in python library
> > > that would avoid the implicit creation of a new list and copy of
> > > elements that is the default behaviour. Instead I'd rather have a
> > > lazy iteratable object on the original sequence. Well, in the end
> > > I wrote it myself but I was wondering if I missed sth in the
> > > library. If I didn't is there a particular reason there isn't sth
> > > like that? I find it hard to believe that all slice needs have
> > > strictly copy semantics.
> > >
> > > Cheers,
> > > Themis
> >
> > Two questions:  1) What is "sth"?  and 2), What copy?
> >
> > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
> > (Intel)]
> > In [1]: class dummy(object):
> >...: pass
> >...:
> >
> > In [2]: a = dummy()
> > In [3]: b = dummy()
> > In [4]: c = dummy()
> > In [5]: d = dummy()
> > In [6]: e = dummy()
> > In [7]: list1 = [a, b, c, d, e]
> > In [8]: list1
> > Out[8]:
> > [<__main__.dummy object at 0x0130C510>,
> >  <__main__.dummy object at 0x013F1A50>,
> >  <__main__.dummy object at 0x00A854F0>,
> >  <__main__.dummy object at 0x00A7EF50>,
> >  <__main__.dummy object at 0x00A7E650>]
> >
> > In [9]: list2 = list1[1:3]
> > In [10]: list2
> > Out[10]:
> > [<__main__.dummy object at 0x013F1A50>,
> >  <__main__.dummy object at 0x00A854F0>]
> >
> > In [11]: list2[0] is list1[1]
> > Out[11]: *True*
> > In [12]: list2[1] is list1[2]
> > Out[12]: *True*
> >
> > No copying of items going on here.  What do you get?
> >
> > ~Ethan~
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> 



Rami Chowdhury
"As an online discussion grows longer, the probability of a comparison 
involving Nazis or Hitler approaches one." -- Godwin's Law
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
-- 
http://mail.python.org/mailman/listinfo/python-list


convert a string to a regex?

2009-11-18 Thread Peng Yu
There are many special characters listed on
http://docs.python.org/library/re.html

I'm wondering if there is a convenient function that can readily
convert a string with the special characters to its corresponding
regex. For example,

"some.thing" => "some\.thing"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the naming convention for accessor of a 'private' variable?

2009-11-18 Thread Benjamin Kaplan
On Wed, Nov 18, 2009 at 9:27 PM, Peng Yu  wrote:
> http://www.python.org/dev/peps/pep-0008/
>
> The above webpage states the following naming convention. Such a
> variable can be an internal variable in a class. I'm wondering what is
> the naming convention for the method that access such variable.
>
>    - _single_leading_underscore: weak "internal use" indicator.  E.g. "from M
>      import *" does not import objects whose name starts with an underscore.
> --

You never have any convention for a method that accesses the private
variable. In a language that has scope declarations, a method that
uses private variables is still declared public or private- there's no
special keyword for it.

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


Invitation to connect on LinkedIn

2009-11-18 Thread Jim Qiu
LinkedIn


Jim Qiu requested to add you as a connection on LinkedIn:
--

Jaime,

I'd like to add you to my professional network on LinkedIn.

- Jim

Accept invitation from Jim Qiu
http://www.linkedin.com/e/I2LlXdLlWUhFABKmxVOlgGLlWUhFAfhMPPF/blk/I431163609_3/6lColZJrmZznQNdhjRQnOpBtn9QfmhBt71BoSd1p65Lr6lOfPdvej0ScPoNcjcQiiZNnSFSd55EjiYTcP4NejgPdPwLrCBxbOYWrSlI/EML_comm_afe/

View invitation from Jim Qiu
http://www.linkedin.com/e/I2LlXdLlWUhFABKmxVOlgGLlWUhFAfhMPPF/blk/I431163609_3/0PnPAMdzcScj4Pd4ALqnpPbOYWrSlI/svi/

--

Why might connecting with Jim Qiu be a good idea?

Jim Qiu's connections could be useful to you:
After accepting Jim Qiu's invitation, check Jim Qiu's connections to see who 
else you may know and who you might want an introduction to. Building these 
connections can create opportunities in the future.

 
--
(c) 2009, LinkedIn Corporation

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


Re: What is the naming convention for accessor of a 'private' variable?

2009-11-18 Thread Chris Rebert
On Wed, Nov 18, 2009 at 6:27 PM, Peng Yu  wrote:
> http://www.python.org/dev/peps/pep-0008/
>
> The above webpage states the following naming convention. Such a
> variable can be an internal variable in a class. I'm wondering what is
> the naming convention for the method that access such variable.
>
>    - _single_leading_underscore: weak "internal use" indicator.  E.g. "from M
>      import *" does not import objects whose name starts with an underscore.

If there's a method to access the variable, then it's not all that
private, is it?
Accessor methods are not Pythonic. Just make the attribute public by
not prefixing it with an underscore.

See also "Python is Not Java":
http://dirtsimple.org/2004/12/python-is-not-java.html

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: non-copy slices

2009-11-18 Thread Daniel Stutzbach
On Wed, Nov 18, 2009 at 2:22 PM, Themis Bourdenas <
t.bourdena...@imperial.ac.uk> wrote:

> It's nothing in the library that completely imitates the slice without the
> copies, right?


You might be interested in my blist extension type (link below).
Syntactically, using a blist is just like using a list, but it has different
performance characteristics.

In particular for your needs, taking a slice is cheap.  The blist implements
copy-on-write behind the scenes, so taking a slice takes O(log n) time,
where n is the size of the initial blist.

http://pypi.python.org/pypi/blist/

Here is a simple example, which creates a blist with over 500 million
elements and takes a slice of over 500 million elements.  In under 22
microseconds. :-)

from blist import blist
small_list = blist([0])
BIG_list = small_list * 2**29
BIG_slice = BIG_list[4:-5]

Cashew:~$ python2.5 -m timeit -s 'from blist import blist'
'small_list=blist([0])' ''BIG_list=small_list*2**29'
'BIG_slice=BIG_list[4:-5]'
1 loops, best of 3: 21.5 usec per loop

--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: combinatorics via __future__ generators

2009-11-18 Thread Chris Rebert
On Wed, Nov 18, 2009 at 4:58 PM, Phlip  wrote:
> Python:
>
> I have a quaint combinatorics problem. Before I solve it, or find a
> solution among "generators", I thought y'all might like to show off
> any solutions.
>
> Given an array like this...
>
>  [0, 4, 3]
>
> Produce an array like this:
>
>  [
>    [0, 0, 0],
>    [0, 1, 0],
>    [0, 2, 0],
>    [0, 3, 0],
>    [0, 1, 1],
>    [0, 2, 1],
>    [0, 3, 1],
>    [0, 1, 2],
>    [0, 2, 2],
>    [0, 3, 2],
> ]
>
> The first array is the counts of options in 4 slots, and the second is
> all combinations of indexes of each option, such that every option
> associates once with every other option. The leading 0 simply
> represents a slot with no options; the algorithm must preserve those.
>
> This should be child's play for the generator package, right?

from itertools import imap, product

def special_range(n):
return (xrange(n) if n else [0])

def something(arr):
return product(*imap(special_range, arr))

print list(something([0,4,3]))

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


hex

2009-11-18 Thread hong zhang
List,

I want to input hex number instead of int number. in type="int" in following,

parser.add_option("-F", "--forcemcs", dest="force_mcs", type="int", default=0, 
help="index of 11n mcs table. Default: 0.")

How can I do it?
Thanks.

--henry


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


Re: Language mavens: Is there a programming with "if then else ENDIF" syntax?

2009-11-18 Thread Steve Howell
On Nov 18, 5:42 pm, Steven D'Aprano
 wrote:
> On Wed, 18 Nov 2009 17:14:27 -0800, Steve Howell wrote:
> > In my rewritten code, here is the smell:
>
> >     dispatches = {
> >             'dict': _dict,
> >             'list': _list,
> >             'attr': _attr,
> >             'key': _key,
> >             'as': _as,
> >             'call': _call,
> >             'recurse': _recurse,
> >             }
> >     if kind in dispatches:
> >         return dispatches[kind](ast)
> >     else:
> >         raise Exception('unknown kind!')
>
> > There is nothing wrong with the code taken out of context, but one of
> > the first things that lexical duplication should alert you to is the
> > fact that you are creating code that is brittle to extension.
>
> Really? It is very simple to extend it by adding another key:item to the
> dict.
>

Easy for me to extend, of course, since I wrote the code.  It's the
maintainers I am worried about.  I can make their lives easier, of
course, by changing the text of the exception to say something like
this:

   "Silly maintenance programmer, you now have to update the dispatch
table!"

I have no problem with that.


> > In my
> > example, the reference to _dict is 36 lines of code away from its
> > implementation, which forces indirection on the reader.
>
> It gets worse -- the reference to the in operator is in a completely
> different module to the implementation of the in operator, which may be
> in a completely different language! (C in this case.)
>
> I'm being sarcastic, of course, but I do have a serious point. I'm not
> impressed by complaints that the definition of the function is oh-so-very-
> far-away from where you use it. Well duh, that's one of the reasons we
> have functions, so they can be used anywhere, not just where they're
> defined.
>

The slippery slope here is that every block of code that you ever
write should be extracted out into a method.  Now I am all in favor of
tiny methods, but the whole point of if/elif/elif/elif/elif/end code
is that you are actually calling out to the maintenance programmer
that this code is only applicable in certain conditions.  That's why
"if" statements are called "conditionals"!  You are actually telling
the maintenance programmer that is only sensible to use these
statements under these conditions.  It is actually extremely explicit.

Now I'm being sarcastic too, but I also have a serious point.  Nine
times out of ten if/elif/elif/end calls out the smell of spaghetti
code.  But it can also occasionally be a false negative.  It can be a
case of avoiding premature abstraction and indirection.


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


What is the naming convention for accessor of a 'private' variable?

2009-11-18 Thread Peng Yu
http://www.python.org/dev/peps/pep-0008/

The above webpage states the following naming convention. Such a
variable can be an internal variable in a class. I'm wondering what is
the naming convention for the method that access such variable.

- _single_leading_underscore: weak "internal use" indicator.  E.g. "from M
  import *" does not import objects whose name starts with an underscore.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: DeprecationWarning on md5

2009-11-18 Thread Chris Rebert
On Wed, Nov 18, 2009 at 5:23 PM, Zeynel  wrote:
> Hello,
>
> I am a newbie both in Scrapy and Python. When I create a project with
> Scrapy I get these errors:
>
> C:\Python26\lib\site-packages\twisted\python\filepath.py:12:
> DeprecationWarning: the sha module is deprecated; use the hashlib
> module instead import sha
> C:\Python26\lib\site-packages\twisted\spread\pb.py:30:
> DeprecationWarning: the md5 module is deprecated; use hashlib instead
> import md5
> C:\Python26\lib\site-packages\twisted\mail\smtp.py:10:
> DeprecationWarning: the MimeWriter module is deprecated; use the email
> package instead
>
> I found several references to this "bug" but I could not understand
> how to fix it. Can anyone help?

This is a "bug" in the version of Twisted you're using. What Python is
saying is that Twisted is using some modules from the Python stdlib
that are deprecated and will be removed in some future version of
Python, thus Twisted will /eventually/ need to be changed to use the
newer replacement library.
However, this is only a warning, not an error; the code will run just
fine until you update to the eventual Python version that removed said
deprecated libraries (by which time a new Twisted version will
probably be available with the necessary fixes made).

So, basically you can safely ignore the warnings, unless you want to
custom-patch your Twisted installation, which I wouldn't particularly
recommend.
It's also entirely possible your Twisted is outdated and a new version
fixed to avoid using the deprecated modules is already available.

If you want to suppress the output of the warnings, see the docs for
the `warnings` module:
http://docs.python.org/library/warnings.html

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New syntax for blocks

2009-11-18 Thread r
On Nov 18, 5:27 pm, Steven D'Aprano
 wrote:
> On Wed, 18 Nov 2009 18:28:11 +1300, greg wrote:
> > r wrote:
> >> I think the syntax was chosen because the alternatives are even worse
> >> AND since assignment is SO common in programming, would you *really*
> >> rather type two chars instead of one?
>
> > Smalltalk solved the problem by using a left-arrow character for
> > assignment. But they had an unfair advantage in being able to use a
> > non-standard character set on their custom-built machines.
>
> > We should be able to do a lot better now using Unicode. We could even
> > heal the <> vs != rift by using a real not-equal symbol!
>
> The problem isn't with the available characters, but with *typing* them.
>
> It is hard to enter arbitrary Unicode characters by the keyboard, which
> frankly boggles my mind. I don't know what the state of the art on Mac is
> these days, but in 1984s Macs had a standard keyboard layout that let you
> enter most available characters via the keyboard, using sensible
> mnemonics. E.g. on a US keyboard layout, you could get ≠ by holding down
> the Option key and typing =.
>
> For me, I had to:
>
> Click Start menu > Utilities > More Applications > KCharSelect.
> Click through thirty-four(!) tables scanning by eye for the symbol I
> wanted.
> Click the ≠ character.
> Click To Clipboard.
> Go back to my editor window and paste.
>
> --
> Steven

♂ <-- Heres a free lesson... Stephen, hold down  and press
 twice, then release . ;-)

PS: But please lets not start using Unicode chars in programming, you
guy's already know how much i *hate* Unicode.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: non-copy slices

2009-11-18 Thread tbourden
Hi,

sth == something :) sorry for the abbreviation. I'm talking about the
shallow copy, still it's a copy. Unnecessary in my case and the worst part
in my scenario is the creation (allocation) and deletion of a very large
number of lists of moderate size (a few hundred objects) generated due to
slices, while I only need to have a restricted view on the original list.
The islice class partially solves the problem as I mentioned in the previous
emails.

Cheers,
Themis


On Wed, Nov 18, 2009 at 3:44 PM, Ethan Furman  wrote:

> tbour...@doc.ic.ac.uk wrote:
> > Hi,
> >
> > I was looking for a facility similar to slices in python library that
> > would avoid the implicit creation of a new list and copy of elements
> > that is the default behaviour. Instead I'd rather have a lazy iteratable
> > object on the original sequence. Well, in the end I wrote it myself but
> > I was wondering if I missed sth in the library. If I didn't is there a
> > particular reason there isn't sth like that? I find it hard to believe
> > that all slice needs have strictly copy semantics.
> >
> > Cheers,
> > Themis
>
> Two questions:  1) What is "sth"?  and 2), What copy?
>
> Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
> (Intel)]
> In [1]: class dummy(object):
>...: pass
>...:
>
> In [2]: a = dummy()
> In [3]: b = dummy()
> In [4]: c = dummy()
> In [5]: d = dummy()
> In [6]: e = dummy()
> In [7]: list1 = [a, b, c, d, e]
> In [8]: list1
> Out[8]:
> [<__main__.dummy object at 0x0130C510>,
>  <__main__.dummy object at 0x013F1A50>,
>  <__main__.dummy object at 0x00A854F0>,
>  <__main__.dummy object at 0x00A7EF50>,
>  <__main__.dummy object at 0x00A7E650>]
>
> In [9]: list2 = list1[1:3]
> In [10]: list2
> Out[10]:
> [<__main__.dummy object at 0x013F1A50>,
>  <__main__.dummy object at 0x00A854F0>]
>
> In [11]: list2[0] is list1[1]
> Out[11]: *True*
> In [12]: list2[1] is list1[2]
> Out[12]: *True*
>
> No copying of items going on here.  What do you get?
>
> ~Ethan~
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Language mavens: Is there a programming with "if then else ENDIF" syntax?

2009-11-18 Thread Steven D'Aprano
On Wed, 18 Nov 2009 17:14:27 -0800, Steve Howell wrote:

> In my rewritten code, here is the smell:
> 
> dispatches = {
> 'dict': _dict,
> 'list': _list,
> 'attr': _attr,
> 'key': _key,
> 'as': _as,
> 'call': _call,
> 'recurse': _recurse,
> }
> if kind in dispatches:
> return dispatches[kind](ast)
> else:
> raise Exception('unknown kind!')
> 
> 
> There is nothing wrong with the code taken out of context, but one of
> the first things that lexical duplication should alert you to is the
> fact that you are creating code that is brittle to extension.

Really? It is very simple to extend it by adding another key:item to the 
dict.


> In my
> example, the reference to _dict is 36 lines of code away from its
> implementation, which forces indirection on the reader.

It gets worse -- the reference to the in operator is in a completely 
different module to the implementation of the in operator, which may be 
in a completely different language! (C in this case.)

I'm being sarcastic, of course, but I do have a serious point. I'm not 
impressed by complaints that the definition of the function is oh-so-very-
far-away from where you use it. Well duh, that's one of the reasons we 
have functions, so they can be used anywhere, not just where they're 
defined.

If a function *only* makes sense immediately where you use it, then the 
level of coupling is too great and you should either reduce the coupling 
or bite the bullet and live with all the hassles of inline code. (Code 
duplication, difficulty in testing, etc.)

Of course it would be convenient if, having noticed the reference to 
(say) function _recurse, you could see its definition without needing to 
do more than just glance up a line or two. But you should rarely need to 
care about the implementation -- that's another point of functions. You 
should be able to treat _recurse as a black box, just as you use the 
built-in function len as a black box without needing to see its 
implementation every time you use it.



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


Re: Arcane question regarding white space, editors, and code collapsing

2009-11-18 Thread Ben Finney
Steven D'Aprano  writes:

> On Thu, 19 Nov 2009 10:56:35 +1100, Ben Finney wrote:
>
> > It's quite improper (though syntactically null, in Python) to have
> > trailing whitespace on lines. That includes blank lines.
>
> Blank lines are far from improper in Python, they're recommended by
> PEP 8.

I phrased that poorly. I meant “That [admonishment against trailing
whitespace] includes trailing whitespace on otherwise-empty lines”.

> > One major reason is that trailing whitespace causes spurious
> > invisible differences between otherwise-identical lines when doing
> > an automatic comparison, which is done quite a lot in collaboration
> > and version control.
>
> Then you need better comparison software that doesn't give so many
> false matches due to insignificant differences.

I have such tools, but I shouldn't require others to also have and use
them. It would be irresponsible of me to spew output that causes
breakage in the absence of such tools.

My position on this is: be strict in what you emit (i.e. no trailing
whitespace), and construct your output to function well with
lowest-common-denominator standard tools (i.e. don't expect
sophistication above the standard ‘diff -u’ etc.).

-- 
 \  “It's up to the masses to distribute [music] however they want |
  `\… The laws don't matter at that point. People sharing music in |
_o__)their bedrooms is the new radio.” —Neil Young, 2008-05-06 |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Language mavens: Is there a programming with "if then else ENDIF" syntax?

2009-11-18 Thread Steve Howell
On Nov 18, 5:13 pm, Steven D'Aprano
 wrote:
> On Wed, 18 Nov 2009 15:58:24 -0800, Steve Howell wrote:
> > On Nov 18, 2:22 pm, Steven D'Aprano
> >  wrote:
> >> On Wed, 18 Nov 2009 02:06:49 -0800, Steve Howell wrote:
> >> > P.S. The underscores before the method names might look a little
> >> > funny for inner methods, but it's the nature of the code..._dict and
> >> > _list would lead to confusion with builtins, if not actual conflict.
> [...]
>
> But I'm not sure what naming convention you used. It seems fairly
> arbitrary to me, but whatever it is, it clashes with built-ins, which is
> a good sign that you need a different set of names. Since naming the
> functions with leading underscores also clashes with the convention that
> such functions are private, that's a further sign that you should use a
> different naming convention. At the point that you are apologizing for
> the convention you use, maybe you should rethink it.
>
> But of course it's your code, and you're free to use whatever convention
> you like.
>

The code in question, with the _list and _dict, is used as a part of
mini-compiler-like-thingy that generates code from an expression
syntax.  If you've ever worked with code to implement compilers,
interpreters, VMs, etc., you probably know the naming challenges
involved.  The whole reason I mentioned the wierd names in a "P.S."
was to say that I realized it was a little wierd, and it was kind of
besides the point.  But I have enjoyed your responses.  They are
slightly pedantic, of course, but in a good way!  Makes me think about
the code more...

If you want more context on the code itself (apart from if/elif
discussion and other digressions), I describe it in more detail here:

http://showellonprogramming.blogspot.com/2009/11/more-on-python-deep-copy-schema.html
-- 
http://mail.python.org/mailman/listinfo/python-list


DeprecationWarning on md5

2009-11-18 Thread Zeynel
Hello,

I am a newbie both in Scrapy and Python. When I create a project with
Scrapy I get these errors:

C:\Python26\lib\site-packages\twisted\python\filepath.py:12:
DeprecationWarning: the sha module is deprecated; use the hashlib
module instead import sha
C:\Python26\lib\site-packages\twisted\spread\pb.py:30:
DeprecationWarning: the md5 module is deprecated; use hashlib instead
import md5
C:\Python26\lib\site-packages\twisted\mail\smtp.py:10:
DeprecationWarning: the MimeWriter module is deprecated; use the email
package instead

I found several references to this "bug" but I could not understand
how to fix it. Can anyone help?

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


Re: Language mavens: Is there a programming with "if then else ENDIF" syntax?

2009-11-18 Thread Steve Howell
On Nov 18, 3:02 pm, Steven D'Aprano
 wrote:
> Lexical duplication is one of the weakest code smells around, because it
> is so prone to false negatives. You often can't avoid referring to the
> same lexical element multiple times:
>
> def sinc(x):
>     if x != 0:
>         return sin(x)/x
>     return 1
>

The existence of one code sample where lexical duplication is a false
negative should not get you into the habit of disregarding it all
together.

In my rewritten code, here is the smell:

dispatches = {
'dict': _dict,
'list': _list,
'attr': _attr,
'key': _key,
'as': _as,
'call': _call,
'recurse': _recurse,
}
if kind in dispatches:
return dispatches[kind](ast)
else:
raise Exception('unknown kind!')


There is nothing wrong with the code taken out of context, but one of
the first things that lexical duplication should alert you to is the
fact that you are creating code that is brittle to extension.  In my
example, the reference to _dict is 36 lines of code away from its
implementation, which forces indirection on the reader.  So maybe the
methods in between def _dict and _dict are too long.  I can pretty
quickly rule that out, as the methods average about four lines each.

So maybe the dispatch method is calling out the need for a class, as
Simon suggested in another post.  I also wonder if an inline decorator
is in order.

One thing I promise not to do is revert the code to if/elif/elif.  :)

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


Re: Language mavens: Is there a programming with "if then else ENDIF" syntax?

2009-11-18 Thread Steven D'Aprano
On Wed, 18 Nov 2009 15:58:24 -0800, Steve Howell wrote:

> On Nov 18, 2:22 pm, Steven D'Aprano
>  wrote:
>> On Wed, 18 Nov 2009 02:06:49 -0800, Steve Howell wrote:
>> > P.S. The underscores before the method names might look a little
>> > funny for inner methods, but it's the nature of the code..._dict and
>> > _list would lead to confusion with builtins, if not actual conflict.
>>
>> Then name them something sensible that tells what they do!
>>
>> The convention (which you break at your peril) is that functions are
>> verbs, and classes are nouns. Even "handle_dict" is better than _dict
>> -- the latter looks like you're calling a private mapping type.
>>
>>
> Do you verbify functions with no side effects?  Do you prefer
> calculate_cosine(angle) to cosine(angle)?


Neither, I prefer cos(angle).

Mathematical functions are a special case. They have been around since 
long before computers, and there is a whole different tradition for them. 
(The tradition is that most functions are named after Euler, or the first 
person to discover them after Euler.)

But you're right, the convention of using verbs for functions isn't as 
strong as the convention of using nouns for classes and types. Functions 
that return some property of a noun are often named after the property:

len(x) rather than get_len(x)
sqrt(x) rather than calculate_sqrt(x)

and similar.

Likewise, conversion functions are often named after the return result:

int(x) returns an int
str(x) returns a str

[pedant]
these are actually types, not functions, in Python
[/pedant]


But I'm not sure what naming convention you used. It seems fairly 
arbitrary to me, but whatever it is, it clashes with built-ins, which is 
a good sign that you need a different set of names. Since naming the 
functions with leading underscores also clashes with the convention that 
such functions are private, that's a further sign that you should use a 
different naming convention. At the point that you are apologizing for 
the convention you use, maybe you should rethink it.

But of course it's your code, and you're free to use whatever convention 
you like.



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


Re: combinatorics via __future__ generators

2009-11-18 Thread Phlip
On Nov 18, 4:58 pm, Phlip  wrote:
> Python:
>
> I have a quaint combinatorics problem. Before I solve it, or find a
> solution among "generators", I thought y'all might like to show off
> any solutions.
>
> Given an array like this...
>
>   [0, 4, 3]
>
> Produce an array like this:
>
>   [
>     [0, 0, 0],
>     [0, 1, 0],
>     [0, 2, 0],
>     [0, 3, 0],
  [0, 0, 1],
> [0, 1, 1],
>     [0, 2, 1],
>     [0, 3, 1],
  [0, 0, 2],
> [0, 1, 2],
>     [0, 2, 2],
>     [0, 3, 2],
> ]

I forgot those combinations!

>
> The first array is the counts of options in 4 slots, and the second is
> all combinations of indexes of each option, such that every option
> associates once with every other option. The leading 0 simply
> represents a slot with no options; the algorithm must preserve those.
>
> This should be child's play for the generator package, right?
>
> --
>   Phlip
>  http://zeekland.zeroplayer.com/

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


Re: Arcane question regarding white space, editors, and code collapsing

2009-11-18 Thread Steven D'Aprano
On Thu, 19 Nov 2009 10:56:35 +1100, Ben Finney wrote:

> Wells  writes:
> 
>> Is it... pythonic, then, to have these lines of tabs/spaces to support
>> code collapsing? Is it proper, improper, or irrelevant?
> 
> It's quite improper (though syntactically null, in Python) to have
> trailing whitespace on lines. That includes blank lines.

Blank lines are far from improper in Python, they're recommended by PEP 8.


> One major reason is that trailing whitespace causes spurious invisible
> differences between otherwise-identical lines when doing an automatic
> comparison, which is done quite a lot in collaboration and version
> control.

Then you need better comparison software that doesn't give so many false 
matches due to insignificant differences.


> Fix your text editor (which may entail switching to a better text
> editor) to respect blank lines and the conventions of the language.

Yes, if the editor's handling of code collapsing is broken, it needs to 
be fixed, or replaced.


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


Re: Writing a Carriage Return in Unicode

2009-11-18 Thread MRAB

Doug wrote:

Hi!

I am trying to write a UTF-8 file of UNICODE strings with a carriage
return at the end of each line (code below).

filOpen = codecs.open("c:\\temp\\unicode.txt",'w','utf-8')

str1 = u'This is a test.'
str2 = u'This is the second line.'
str3 = u'This is the third line.'

strCR = u"\u240D"

filOpen.write(str1 + strCR)
filOpen.write(str2 + strCR)
filOpen.write(str3 + strCR)

filOpen.close()

The output looks like
This is a test.�This is the second line.�This is the third
line.� when opened in Wordpad as a UNICODE file.

Thanks for your help!!


u'\u240D' isn't a carriage return (that's u'\r') but a symbol (a visible
"CR" graphic) for carriage return. Windows programs normally expect
lines to end with '\r\n'; just use u'\n' in programs and open the text
files in text mode ('r' or 'w').

Some Windows programs won't recognise UTF-8 text as UTF-8 in files
unless they start with a BOM; this will be handled automatically in
Python if you specify the encoding as 'utf-8-sig'.
-- 
http://mail.python.org/mailman/listinfo/python-list


combinatorics via __future__ generators

2009-11-18 Thread Phlip
Python:

I have a quaint combinatorics problem. Before I solve it, or find a
solution among "generators", I thought y'all might like to show off
any solutions.

Given an array like this...

  [0, 4, 3]

Produce an array like this:

  [
[0, 0, 0],
[0, 1, 0],
[0, 2, 0],
[0, 3, 0],
[0, 1, 1],
[0, 2, 1],
[0, 3, 1],
[0, 1, 2],
[0, 2, 2],
[0, 3, 2],
]

The first array is the counts of options in 4 slots, and the second is
all combinations of indexes of each option, such that every option
associates once with every other option. The leading 0 simply
represents a slot with no options; the algorithm must preserve those.

This should be child's play for the generator package, right?

--
  Phlip
  http://zeekland.zeroplayer.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: DOM related question and problem

2009-11-18 Thread elca



Chris Rebert-6 wrote:
> 
> On Wed, Nov 18, 2009 at 10:04 AM, elca  wrote:
>> Hello,
>> these day im making python script related with DOM.
>>
>> problem is these day many website structure is very complicate .
>>
>> what is best method to check DOM structure and path..
>>
>> i mean...following is some example.
>>
>> what is best method to check  can extract such like following info
>> quickly?
>>
>> before i was spent much time to extract such info .
>>
>> and yes im also new to python and DOM.
>>
>>    IE.Document.Frames(1).Document.forms('comment').value = 'hello'
>>
>> if i use DOM inspector, can i extract such info quickly ? if so would you
>> show me some sample?
>>
>> here is some site . i want to extract some dom info.
>>
>> today i was spent all day long to extract what is dom info. but failed
>>
>> http://www.segye.com/Articles/News/Politics/Article.asp?aid=20091118001261&ctg1=06&ctg2=00&subctg1=06&subctg2=00&cid=010101060
>>
>> at the end of this page,can find some comment input box.
>>
>> i want to know what kind of dom element should have to use, such like
>>
>>    IE.Document.Frames(1).Document.forms('comment').value = 'hello'
>>
>> anyhelp much appreciate thanks
> 
> This sounds suspiciously like a spambot. Why do you want to submit
> comments in an automated fashion exactly?
> 
> Cheers,
> Chris
> --
> http://blog.rebertia.com
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 
Hello
this is not spambot actually.
it related with my blog scraper..
anyone can help me or advice much appreciate
-- 
View this message in context: 
http://old.nabble.com/DOM-related-question-and-problem-tp26412730p26418556.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: Language mavens: Is there a programming with "if then else ENDIF" syntax?

2009-11-18 Thread Steve Howell
On Nov 18, 3:02 pm, Steven D'Aprano
 wrote:
>
> That depends on the code. In particular, it depends on how coupled the
> code is. Ideally, you should have loosely coupled code, not highly
> coupled. If the code is loosely coupled, then there's no problem with
> understanding it in isolation. If the code is highly coupled, then it is
> hard to refactor it into a separate function, but that's a side-effect of
> the original problem, namely the high degree of coupling.
>

Different blocks of an if/elif/elif/elif/elif/end are never directly
coupled to each other, since you know that only one of the blocks is
gonna execute.  The blocks inherently do not effect each other.

What you really achieve by extracting the code from an inside an elif
block into a method is decoupling the elif block from the code ABOVE
the if.  This in and of itself is a good thing, of course, because you
get a nice clean namespace in the extracted method, and you make the
coupling between the code ABOVE the if and the extracted method
explicit by specifying which parameters get passed.  And in the case
of my original code, all seven methods that were extracted only
depended on a single parameter, the variable I was calling "ast," so
it was easy to dispatch them all using the same mechanism.  (In my
case I didn't achieve much in terms of cleaning the local namespace,
since the only variable defined above the if/elif/elif/elif/end was
"kind," but I did make my code less brittle to future changes.)

I am not going to defend "if/elif/elif/elif/elif/end" too vigorously
here.  There are obvious advantages to extracting methods, even
methods that only ever get called from one parent.  I do not miss
switch statements one bit in Python.  I am little more ambivalent
about anonymous methods.  It pains me just a tiny bit whenever I have
write code like this:

dispatches = {
'dict': handle_dict,
'list': handle_list,
'attr': handle_attr,
'key':  handle_key,
'as':   handle_as,
'call': handle_call,
}
if kind in dispatches:
return dispatches[kind](ast)
else:
raise Exception('unknown kind!')

I have used the idiom that Simon suggested in an earlier post, shown
below, but it is still brittle to name changes in a way that anonymous
methods are not, because you can't break an inline anonymous method
with a name change (the benefits on anonymity!!):

try:
method = getattr(self, 'do_' + kind)
except AttributeError:
raise Exception('unknown kind!')

self.ast = ast

return method()

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


Writing a Carriage Return in Unicode

2009-11-18 Thread Doug

Hi!

I am trying to write a UTF-8 file of UNICODE strings with a carriage
return at the end of each line (code below).

filOpen = codecs.open("c:\\temp\\unicode.txt",'w','utf-8')

str1 = u'This is a test.'
str2 = u'This is the second line.'
str3 = u'This is the third line.'

strCR = u"\u240D"

filOpen.write(str1 + strCR)
filOpen.write(str2 + strCR)
filOpen.write(str3 + strCR)

filOpen.close()

The output looks like
This is a test.␍This is the second line.␍This is the third
line.␍ when opened in Wordpad as a UNICODE file.

Thanks for your help!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Language mavens: Is there a programming with "if then else ENDIF" syntax?

2009-11-18 Thread Steve Howell
On Nov 18, 2:22 pm, Steven D'Aprano
 wrote:
> On Wed, 18 Nov 2009 02:06:49 -0800, Steve Howell wrote:
> > P.S. The underscores before the method names might look a little funny
> > for inner methods, but it's the nature of the code..._dict and _list
> > would lead to confusion with builtins, if not actual conflict.
>
> Then name them something sensible that tells what they do!
>
> The convention (which you break at your peril) is that functions are
> verbs, and classes are nouns. Even "handle_dict" is better than _dict --
> the latter looks like you're calling a private mapping type.
>

Do you verbify functions with no side effects?  Do you prefer
calculate_cosine(angle) to cosine(angle)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Arcane question regarding white space, editors, and code collapsing

2009-11-18 Thread Ben Finney
Wells  writes:

> Is it... pythonic, then, to have these lines of tabs/spaces to support
> code collapsing? Is it proper, improper, or irrelevant?

It's quite improper (though syntactically null, in Python) to have
trailing whitespace on lines. That includes blank lines.

One major reason is that trailing whitespace causes spurious invisible
differences between otherwise-identical lines when doing an automatic
comparison, which is done quite a lot in collaboration and version
control.

Fix your text editor (which may entail switching to a better text
editor) to respect blank lines and the conventions of the language.

-- 
 \  “Hey Homer! You're late for English!” “Pff! English, who needs |
  `\  that? I'm never going to England!” —Barney & Homer, _The |
_o__)Simpsons_ |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Arcane question regarding white space, editors, and code collapsing

2009-11-18 Thread Wells
I work in TextMate a lot, which I generally love, but it's code
collapsing confounds me. Essentially you have to indent blank lines to
the proper level for the current block. Then it will collapse that
section as one section. If you have simply a new line, it will see it
as a break, and not collapse, though the python interpreter doesn't
care- it only cares about lines of actual code.

Is it... pythonic, then, to have these lines of tabs/spaces to support
code collapsing? Is it proper, improper, or irrelevant?

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


Re: New syntax for blocks

2009-11-18 Thread Steven D'Aprano
On Wed, 18 Nov 2009 18:28:11 +1300, greg wrote:

> r wrote:
>> I think the syntax was chosen because the alternatives are even worse
>> AND since assignment is SO common in programming, would you *really*
>> rather type two chars instead of one?
> 
> Smalltalk solved the problem by using a left-arrow character for
> assignment. But they had an unfair advantage in being able to use a
> non-standard character set on their custom-built machines.
> 
> We should be able to do a lot better now using Unicode. We could even
> heal the <> vs != rift by using a real not-equal symbol!

The problem isn't with the available characters, but with *typing* them.

It is hard to enter arbitrary Unicode characters by the keyboard, which 
frankly boggles my mind. I don't know what the state of the art on Mac is 
these days, but in 1984s Macs had a standard keyboard layout that let you 
enter most available characters via the keyboard, using sensible 
mnemonics. E.g. on a US keyboard layout, you could get ≠ by holding down 
the Option key and typing =.

For me, I had to:

Click Start menu > Utilities > More Applications > KCharSelect.
Click through thirty-four(!) tables scanning by eye for the symbol I 
wanted.
Click the ≠ character.
Click To Clipboard.
Go back to my editor window and paste.



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


Re: Language mavens: Is there a programming with "if then else ENDIF" syntax?

2009-11-18 Thread Steven D'Aprano
On Wed, 18 Nov 2009 01:53:50 -0800, Steve Howell wrote:

> On Nov 18, 1:32 am, Chris Rebert  wrote:
>> On Wed, Nov 18, 2009 at 1:15 AM, Steve Howell 
>> wrote:
>> > On the topic of "switch" statements and
>> > even-more-concise-then-we-have- already if/elif/else/end constructs,
>> > I have to say that Python does occasionally force you to write code
>> > like the code below.  Maybe "force" is too strong a word, but Python
>> > lends itself to if/elif blocks like below, which get the job done
>> > just fine, but which are not syntactically pretty, due to the
>> > "(el){0,1}if kind ==" duplication. There are often cases where
>> > if/elif statements are just a smell that you do not know how to do
>> > dictionary lookups, but if you converted the below code to use
>> > dictionary lookups, you would complicate the code almost as much as
>> > you abstracted the code, if not more, unless I am just being very
>> > naive.
>>
>> I'm gonna have to disagree and say using the dictionary dispatch
>> technique would clean it up a good bit. Yes, it would entail creating
>> several functions, but those functions could then be documented (vs.
>> the currently opaque code blocks); and due to their separation and
>> smaller length, they would be easier to understand and test than the
>> given code. Additionally, the sheer length of the given code segment
>> probably constitutes a code smell in and of itself for the function
>> containing that code.
>>
>>
> If you introduce seven tiny little methods, aren't you increasing the
> length of the module by seven lines and introducing more complexity with
> the dispatch mechanism? (Actually, it's 14 lines more if you put a line
> of whitespace between your methods, and then you are also blurring an
> important cue that each of the seven code blocks all perform within the
> same context.)


Dear me, the 1960s called, they want their controversy over structured 
programming back *wink*

Yes, you increase the length of the module by having function headers. 
You also increase the length of the module by using meaningful names 
instead of calling everything "a", "b", "c" etc. We're not playing code 
golf, there's no reward for making unreadable code.

The dispatch mechanism is no more complicated than a series of if...elif 
statements. In fact, you can replace an arbitrary number of "if x == y" 
statements with a *single* lookup:

if x == 1:
return 27
elif x == 2:
return 14
elif x == 5:
return 7
else:
return -1

becomes the single line:

return {1: 27, 2: 14, 5: 7}.get(x, -1)

(although for more complicated examples, you would put the dict into its 
own line, or even lines).

Dispatching isn't a panacea. You can't (easily) replace a series of 
if...elif statements where the conditions being tested aren't equality 
tests, e.g.:

if 0 <= x <= 1:
return 27
elif 1 < x < 1.5 or x == 2:
return 14
elif ...

is hard to turn into a dispatcher. But it's still a very useful technique 
when it applies.



> I do agree with your point that separate methods lead to easier unit
> testing.
> 
> I'm a little more skeptical about the documentation/understanding
> argument, since code is often best understood within the context of
> surrounding code.

That depends on the code. In particular, it depends on how coupled the 
code is. Ideally, you should have loosely coupled code, not highly 
coupled. If the code is loosely coupled, then there's no problem with 
understanding it in isolation. If the code is highly coupled, then it is 
hard to refactor it into a separate function, but that's a side-effect of 
the original problem, namely the high degree of coupling.

As a general rule, if you need to know the context of the surrounding 
code to understand a function, you have too much coupling.



> I am also a bit skeptical of any coding technique
> that leads to lexical duplication like {'attr': process_attr, 'key':
> process_key, 'call': process_call}(ast); that is just replacing one
> smell with another.

But how is that different from this?

if condition(x, 'attr'):
return process_attr(x)(ast)
elif condition(x, 'key'):
return process_key(x)(ast)
elif ...


Lexical duplication is one of the weakest code smells around, because it 
is so prone to false negatives. You often can't avoid referring to the 
same lexical element multiple times:


def sinc(x):
if x != 0:
return sin(x)/x
return 1

That's four references to x in a four line function. Is this a problem? 
No, of course not.



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


Re: Using "setup.py" for an application, not a library module.

2009-11-18 Thread Alan Franzoni
On 11/18/09 9:53 PM, John Nagle wrote:
> Most of the documentation for "setup.py" assumes you're packaging a
> library module. (Ref: "http://docs.python.org/distutils/setupscript.html";)
> How do you properly package an application?  What happens
> on "setup.py install"?  Where does the application get installed?  Where
> does
> the main program go?

Usually, just package the lib and from your "main program" (e.g. the
script that goes in /usr/bin, for instance), do just something like that:

#!/usr/bin/python
from mylib import main
import sys

sys.exit(main())


-- 
Alan Franzoni
contact me at pub...@[mysurname].eu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python gui builders

2009-11-18 Thread Kevin Walzer

On 11/18/09 4:15 PM, Simon Hibbs wrote:

On 17 Nov, 23:25, Kevin Walzer  wrote:

On 11/17/09 4:25 PM, Tim Daneliuk wrote:


+1 Tkinter for the simple stuff


You can actually use Tkinter to do quite sophisticated GUI's that rival
anything found in Qt or wx...


Neither Tkinteror Wx have anything that come close to QGraphicsView,
the Model-View-Delegate framework, the Phonon multimedia framework
integration, QtSQL, QtXML, QtSVG, and the many other first grade
components in Qt. You can substitute components from other frameworks,
e.g. for database access, but then you lose the integration QtSQL has
with the model-view-delegate features in other parts of the Qt world.


A few points of response:

-I was focusing primarily on the UI bits, not the other parts of Qt. 
People tend to think that Tkinter only has labels, listboxes, buttons 
and menus: recent advances in Tk have greatly expanded and modernized 
the available widgets. There's also a rich econsystem of widget packages 
within Tk that can be wrapped in Tkinter.


-Qt's support for things like SQL and XML make sense in a Pythonic 
context mainly if you're focusing on their integration with other parts 
of Qt. Python has many of these things just fine on its own, as you 
doubtless know.




Qt is much more than just a GUI framework, it's more of a rival to
something like the MacOS development libraries, or the .NET framework
in terms of the library facilities it offers. I think that's why Nokia
bought into it. Qt provides them with a platform with the potential to
rival the iPhone dev environment.


Of course, and I certainly wasn't claiming otherwise.



Of course to take full advantage of it you need to invest in learning
it all, which is non-trivial. It comes at a cost in time and potential
lock-in. But the point is you can't just say Qt is just like Wx.



wxWidgets (the C++ library) has support for a lot of things other than 
UI bits, as well. wxPython itself is mainly a GUI library because the 
additional features of wxWidgets in C++ are redundant in Python.


--Kevin

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: using struct module on a file

2009-11-18 Thread Simon Forman
On Wed, Nov 18, 2009 at 11:42 AM, Ulrich Eckhardt  wrote:
> Hia!
>
> I need to read a file containing packed "binary" data. For that, I find the
> struct module pretty convenient. What I always need to do is reading a chunk
> of data from the file (either using calcsize() or a struct.Struct instance)
> and then parsing it with unpack(). For that, I repeatedly write utility
> functions that help me do just that, but I can't imagine that there is no
> better way for that.
>
> Questions:
> 0. Am I approaching this from the wrong direction? I'm not a programming
> noob, but rather new to Python still.
> 1. The struct module has pack_into() or unpack_from(), could I somehow
> combine that with a file?
> 2. Is there some easier way to read files? I know about array and xdrlib,
> but neither really fit my desires.
> 3. Failing all that, would you consider this a useful addition to the struct
> module, i.e. should I write a feature request?
>
> Thanks!
>
> Uli

You might look at "Construct":

http://construct.wikispaces.com/

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


Re: A Good Mailer

2009-11-18 Thread Nick Stinemates
On Wed, Nov 18, 2009 at 03:27:11PM -0400, Victor Subervi wrote:
> Hi;
> I need a good mailer that will enable me to mail email from web forms.

smtplib

> Suggestions?

silly example..

#!/usr/bin/env python
import smtplib

session = smtplib.SMTP("localhost")
username = "abc"
password = "def"
session.login(username, password) # only if it requires auth

subject = "Hello, "

header = "Subject: %s \r\nContent-type: text/html;
charset=utf-8\r\n\r\n"

message = "world!"

email_from = "vic...@is.awesome"
email_to = ["em...@myhost.com"]

session.sendmail(email_from, email_to, header+messages)

HTH,
Nick Stinemates


> TIA,
> Victor

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

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


Re: Language mavens: Is there a programming with "if then else ENDIF" syntax?

2009-11-18 Thread Steven D'Aprano
On Wed, 18 Nov 2009 02:06:49 -0800, Steve Howell wrote:

> P.S. The underscores before the method names might look a little funny
> for inner methods, but it's the nature of the code..._dict and _list
> would lead to confusion with builtins, if not actual conflict.


Then name them something sensible that tells what they do!

The convention (which you break at your peril) is that functions are 
verbs, and classes are nouns. Even "handle_dict" is better than _dict -- 
the latter looks like you're calling a private mapping type.


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


Re: Using "setup.py" for an application, not a library module.

2009-11-18 Thread Philip Semanchuk


On Nov 18, 2009, at 3:53 PM, John Nagle wrote:


   Most of the documentation for "setup.py" assumes you're packaging a
library module. (Ref: "http://docs.python.org/distutils/setupscript.html 
")

How do you properly package an application?  What happens
on "setup.py install"?  Where does the application get installed?   
Where does

the main program go?

 If I package and build my app, it packages properly, and
unpacks and builds into

Messager1.0/build/lib

which is appropriate for a library, but not an application.
Here's the setup file.


distutils.core.setup(
name='Messager',
description="Baudot Teletype RSS and SMS program",
version='1.0',
author="John Nagle",
author_email="na...@animats.com",
packages=['messager'],
requires=['pyserial', 'feedparser']
   )


Hi John,
I'm not sure what part you find unpalatable other than "lib" being in  
the pathname. I recently wrote a setup.py for an app called Analysis.  
Under OS X, Linux and Windows it installs into the site-packages  
directory so I can run it like so:


python C:\Python25\Lib\site-packages\analysis\main.py

or:

python /usr/local/lib/python2.6/dist-packages/analysis/main.py


Now, packaging an application according to the expectations of the  
platform to provide a double-clickable icon will require something  
like py2exe or py2app. distutils doesn't provide a facility for that.


DOes that give you an idea about what you were asking?

bye
Philip




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


Re: python gui builders

2009-11-18 Thread Stef Mientki

Simon Hibbs wrote:

On 18 Nov, 07:51, sturlamolden  wrote:

  

GPL



PyQT is GPL for now, but Qt itself is available under the LGPL as is
PySide. Eventualy PySide, which tracks the PyQT API, will supplant it
and the issue will be moot. For now it can be a problem, but PyQT
developer licenses are very afordable at only a few hundred dollars.
If a commercial project can't aford that, it's got problems.

Only you can know enough to make an informed decision. Wx does look
more usable than last time I used it for a project and is a fine
option too, for me though QT is the gold standard against all others
are measured, and generaly found wanting.

Simon Hibbs
  

Wouldn't it be nice
if each fan of some form of GUI-package,
would post it's code (and resulting images) for generating one or two 
standard GUI-forms ?


Then everyone can judge the differences,
and see what's simple and not  so simple !!

And of course I'm willing to contribute the wxPython (wrapped in some 
convenience procedures) for it.


cheers,
Stef

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


Solved: Inserting Unicode text with MySQLdb in Python 2.4-2.5?

2009-11-18 Thread Threader Slash
-- Forwarded message --
From: Keith Hughitt 
To: python-list@python.org
Date: Wed, 18 Nov 2009 06:09:11 -0800 (PST)
Subject: Inserting Unicode text with MySQLdb in Python 2.4-2.5?
Hi all,

I ran into a problem recently when trying to add support for earlier
versions of Python (2.4 and 2.5) to some database related code which
uses MySQLdb, and was wondering if anyone has any suggestions.

With later versions of Python (2.6), inserting Unicode is very simple,
e.g.:

   # -*- coding: utf-8 -*-
   ...
   cursor.execute('''INSERT INTO `table` VALUES (0,
'Ångström'),...''')

When the same code is run on earlier versions, however, the results is
either garbled text (e.g. "Ã or "?" instead of "Å" in Python 2.5), or
an exception being thrown (Python 2.4):

   UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in
position 60: ordinal not in range(128)

So far I've tried a number of different things, including:

   1. Using Unicode strings (e.g. u"\u212B")

   2. Manually specifying the encoding using sys.setdefaultencoding
('utf-8')

   3. Manually enabling Unicode support in MySQLdb
(use_unicode=False, charset = "utf8")

...but no combination of any of the above resulted in proper database
content.

To be certain that the issue was related to Python/MySQLdb and not
MySQL itself, I manually inserted the text and it worked just fine.
Furthermore, when working in a Python console, both print "Å" and
print u"\u212B" display the correct output.

Any ideas? The versions of the MySQLdb adapter tested were 1.2.1
(Python 2.4), and 1.2.2-10 (Python 2.5).

Thanks!
Keith


Hello Keithm,

Here is your answer...

I run on Python2.5, this syntax works fine for me -- a piece of my code:

getMaxID_Query = """SELECT MAX(customerID)
FROM customer;"""

self.cursorMySQL.execute(getMaxID_Query)

Try to use: """ instead of ''' . It will solve your problem.

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


Re: question about tree in python

2009-11-18 Thread Lie Ryan

nospam wrote:
How should I write a tree using diconary. I have used a dictonary to 
make a tree.


dictionary tree?

root = {
'node_a': {
'node_a_a': 'blah',
'node_a_b': 'foo',
'node_a_c': 'bar',
},
'node_b': {
'node_b_a': 'soo',
'node_b_b': 'flee',
'node_b_c': {
'node_b_c_a': 'bee',
'node_b_c_b': 'fee',
'node_b_c_c': 'dee',
},
},
}

you can throw in some default dicts as well as necessary...

Am I misunderstanding something?
--
http://mail.python.org/mailman/listinfo/python-list


Re: WindowsError is not available on linux?

2009-11-18 Thread Dave Angel



Benjamin Kaplan wrote:

On Wed, Nov 18, 2009 at 2:53 PM, Aahz  wrote:
  

In article ,
Peng Yu   wrote:


It's not clear to me whether WindowsError is available on linux or
not, after I read the document.
  

Here's what I told a co-worker to do yesterday:

if os.name ='nt':
   DiskError =OSError, WindowsError)
else:
   DiskError =indowsError

try:
   disk_operation()
except DiskError:
   logit()
--



Shouldn't that be the other way?
if os.name ='nt':
DiskError =SError, WindowsError
else :
DiskError =SError


  
Doesn't matter.  It's not needed anyway, since WindowsError is derived 
from OSError.   So just use OSError in the except clause.


DaveA

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


Re: Language mavens: Is there a programming with "if then else ENDIF" syntax?

2009-11-18 Thread Steven D'Aprano
On Wed, 18 Nov 2009 09:33:38 +0200, Dotan Cohen wrote:

>> Is there any particular reason why this might be a *bad* language-
>> design idea?
> 
> It is about as far from OO as one could get. Whether or not that is
> "bad" depends on the use case.


That's crazy talk. IF...ENDIF is *syntax*, not a programming model. How 
is this hypothetical Python-like syntax not object oriented?

class Parrot:
def speak(self):
if self.name is None:
name = "Polly"
else:
name = self.name
endif
return "%s wants a cracker!" % name


Syntax controls *how* you instruct the compiler, the programming model 
controls *what* you instruct the compiler to do.



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


Re: python gui builders

2009-11-18 Thread David Bolen
Simon Hibbs  writes:

> I've had this problem for a few years. I've tried PythonCard,
> WxWidgets with WxDesigner, BoaConstructor, etc. None of them come
> anywhere close to PyQT/QTDesigner.

For me, the killer feature missing from of all of the wx-based
designers is that they require sizer based designs at all stages, not
even permitting a fixed layout up front as a first draft.  Either that
or any case I've found permitting a fixed layout, then didn't permit
turning that easily into a sizer-based layout.

>From an overall design perspective, that was the feature I found most
intriguing in QTDesigner.  I could randomly drop stuff around the
window while doing an initial layout, which is especially helpful when
you aren't quite sure yet how you want the layout to look.  Then you
can select groups of objects and apply the containers to provide for
flexible layout.

I absolutely prefer sizer-based layouts for a final implementation,
but early in the design stages find it more helpful, and freeing, not
to be as tied to the containers.

With that said, for various reasons I still prefer wxPython to Qt, and
at the moment, find wxFormBuilder the best fit for my own designs
(even before the direct Python support, just using XRC).

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


Re: python gui builders

2009-11-18 Thread Simon Hibbs
On 17 Nov, 23:25, Kevin Walzer  wrote:
> On 11/17/09 4:25 PM, Tim Daneliuk wrote:
>
> > +1 Tkinter for the simple stuff
>
> You can actually use Tkinter to do quite sophisticated GUI's that rival
> anything found in Qt or wx...

Neither Tkinteror Wx have anything that come close to QGraphicsView,
the Model-View-Delegate framework, the Phonon multimedia framework
integration, QtSQL, QtXML, QtSVG, and the many other first grade
components in Qt. You can substitute components from other frameworks,
e.g. for database access, but then you lose the integration QtSQL has
with the model-view-delegate features in other parts of the Qt world.

Qt is much more than just a GUI framework, it's more of a rival to
something like the MacOS development libraries, or the .NET framework
in terms of the library facilities it offers. I think that's why Nokia
bought into it. Qt provides them with a platform with the potential to
rival the iPhone dev environment.

Of course to take full advantage of it you need to invest in learning
it all, which is non-trivial. It comes at a cost in time and potential
lock-in. But the point is you can't just say Qt is just like Wx.

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


Re: non-copy slices

2009-11-18 Thread Ethan Furman

tbour...@doc.ic.ac.uk wrote:

Hi,

I was looking for a facility similar to slices in python library that 
would avoid the implicit creation of a new list and copy of elements 
that is the default behaviour. Instead I'd rather have a lazy iteratable 
object on the original sequence. Well, in the end I wrote it myself but 
I was wondering if I missed sth in the library. If I didn't is there a 
particular reason there isn't sth like that? I find it hard to believe 
that all slice needs have strictly copy semantics.


Cheers,
Themis


Two questions:  1) What is "sth"?  and 2), What copy?

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)]
In [1]: class dummy(object):
   ...: pass
   ...:

In [2]: a = dummy()
In [3]: b = dummy()
In [4]: c = dummy()
In [5]: d = dummy()
In [6]: e = dummy()
In [7]: list1 = [a, b, c, d, e]
In [8]: list1
Out[8]:
[<__main__.dummy object at 0x0130C510>,
 <__main__.dummy object at 0x013F1A50>,
 <__main__.dummy object at 0x00A854F0>,
 <__main__.dummy object at 0x00A7EF50>,
 <__main__.dummy object at 0x00A7E650>]

In [9]: list2 = list1[1:3]
In [10]: list2
Out[10]:
[<__main__.dummy object at 0x013F1A50>,
 <__main__.dummy object at 0x00A854F0>]

In [11]: list2[0] is list1[1]
Out[11]: *True*
In [12]: list2[1] is list1[2]
Out[12]: *True*

No copying of items going on here.  What do you get?

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


Re: WindowsError is not available on linux?

2009-11-18 Thread Christian Heimes
Dave Angel wrote:
> Worse, even if the exception cannot be thrown on a non-Windows 
> environment, leaving it undefined makes it very awkward to write 
> portable code.  An except clause that can never happen in a particular 
> environment is pretty innocent.  Or somebody can use a base class for 
> his except clause, to catch this error and other related ones.  But it 
> blows up if you explicitly use this exception.  I think that needs 
> documentation, at a minimum.

WindowsError is a subclass of OSError that contains additional Windows
specific error information. Just catch OSError and you are on the safe
side.

Christian

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


Re: IOError: [Errno 28] No space left on device

2009-11-18 Thread hong zhang


--- On Wed, 11/18/09, Grant Edwards  wrote:

> From: Grant Edwards 
> Subject: Re: IOError: [Errno 28] No space left on device
> To: python-list@python.org
> Date: Wednesday, November 18, 2009, 2:00 PM
> On 2009-11-18, Diez B. Roggisch
> 
> wrote:
> > hong zhang wrote:
> >>> 
> >>> > but following is good.
> >>> >
> >>> > cont_tx = 1
> >>> > for i in
> >>>
> glob.glob('/sys/kernel/debug/ieee80211/phy*/iwlagn/data/continuous_tx'):
> >>> >with open(i, 'w') as f:
> >>> >print >>f,
> >>> cont_tx
> >>> 
> >>> Well, if that works, then what's your
> problem?
> >> 
> >> But error comes from following, above is good.
> That is point here.
> >> 
> >> def do_cont_tx( is_start):
> >> global cont_tx_started, stdscr
> >> if is_start == START and not cont_tx_started:
> >> cont_tx = 1
> >> for i in
> >>
> glob.glob('/sys/kernel/debug/ieee80211/phy*/iwlagn/data/continuous_tx'):
> >> with open(i, 'w') as f: print >>f, cont_tx
> --
> >
> > "cont_x --" doesn't work. So the above can't be the
> actual code.
> 
> You never want to post the actual code you're
> running.  That
> would make it too easy for people to help.
> 
> -- 

It is typo.

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


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


Re: python gui builders

2009-11-18 Thread Simon Hibbs
On 18 Nov, 07:51, sturlamolden  wrote:

>
> GPL

PyQT is GPL for now, but Qt itself is available under the LGPL as is
PySide. Eventualy PySide, which tracks the PyQT API, will supplant it
and the issue will be moot. For now it can be a problem, but PyQT
developer licenses are very afordable at only a few hundred dollars.
If a commercial project can't aford that, it's got problems.

Only you can know enough to make an informed decision. Wx does look
more usable than last time I used it for a project and is a fine
option too, for me though QT is the gold standard against all others
are measured, and generaly found wanting.

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


Re: WindowsError is not available on linux?

2009-11-18 Thread exarkun

On 07:53 pm, a...@pythoncraft.com wrote:

In article ,
Peng Yu   wrote:


It's not clear to me whether WindowsError is available on linux or
not, after I read the document.


Here's what I told a co-worker to do yesterday:

if os.name == 'nt':
   DiskError = (OSError, WindowsError)
else:
   DiskError = WindowsError

try:
   disk_operation()
except DiskError:
   logit()


This isn't necessary.  WindowsError subclasses OSError.

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


Using "setup.py" for an application, not a library module.

2009-11-18 Thread John Nagle

Most of the documentation for "setup.py" assumes you're packaging a
library module. (Ref: "http://docs.python.org/distutils/setupscript.html";)
How do you properly package an application?  What happens
on "setup.py install"?  Where does the application get installed?  Where does
the main program go?

  If I package and build my app, it packages properly, and
unpacks and builds into

Messager1.0/build/lib

which is appropriate for a library, but not an application.
Here's the setup file.


distutils.core.setup(
name='Messager',
description="Baudot Teletype RSS and SMS program",
version='1.0',
author="John Nagle",
author_email="na...@animats.com",
packages=['messager'],
requires=['pyserial', 'feedparser']
)


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


Re: WindowsError is not available on linux?

2009-11-18 Thread Benjamin Kaplan
On Wed, Nov 18, 2009 at 2:53 PM, Aahz  wrote:
> In article ,
> Peng Yu   wrote:
>>
>>It's not clear to me whether WindowsError is available on linux or
>>not, after I read the document.
>
> Here's what I told a co-worker to do yesterday:
>
> if os.name == 'nt':
>    DiskError = (OSError, WindowsError)
> else:
>    DiskError = WindowsError
>
> try:
>    disk_operation()
> except DiskError:
>    logit()
> --

Shouldn't that be the other way?
if os.name == 'nt':
DiskError = OSError, WindowsError
else :
DiskError = OSError


> Aahz (a...@pythoncraft.com)           <*>         http://www.pythoncraft.com/
>
> "Debugging is twice as hard as writing the code in the first place.
> Therefore, if you write the code as cleverly as possible, you are, by
> definition, not smart enough to debug it."  --Brian W. Kernighan
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: non-copy slices

2009-11-18 Thread Themis Bourdenas
Sth else that I noticed as I started using islice. The name is somewhat
misleading. Having the slice part in the name I would expect it to imitate
the functionality of normal slices. Random access, sub-slicing etc. However,
it is only iteratable. Any particular reasons for that? My guess is that
it's inside the itertools so it's meant only for iteration and not random
access. However, as I told before the name implies the same functionality
while the only thing they share is iteration. It's nothing in the library
that completely imitates the slice without the copies, right?

Cheers,
Themis


On Wed, Nov 18, 2009 at 6:49 PM, Terry Reedy  wrote:

> tbour...@doc.ic.ac.uk wrote:
> > Hi,
> >
> > I was looking for a facility similar to slices in python library that
> > would avoid the implicit creation of a new list and copy of elements
> > that is the default behaviour. Instead I'd rather have a lazy iteratable
> > object on the original sequence. Well, in the end I wrote it myself but
> > I was wondering if I missed sth in the library  If I didn't is there a
> > particular reason there isn't sth like that? I find it hard to believe
> > that all slice needs have strictly copy semantics.
>
> It is a strict *shallow* copy. There is no copying of contents.
> That aside, you are right, hence itertools.islice as already mentioned.
> In the design of 3.0, I believe the idea was raised of making slices
> iterables in 3.0, just as was done for map, filter, and range. However,
> it would have been highly disruptive, and not save much space. Map and
> range create an unbounded number of new objects, rather than just a
> sequence of references to existing objects (or bytes or words for bytes
> and str slices). There is also the problem of virtual slices preventing
> garbage collection of the source sequence when it is not otherwise needed.
>
> Terry Jan Reedy
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Language mavens: Is there a programming with "if then else ENDIF"syntax?

2009-11-18 Thread Dotan Cohen
> The OP explicitly said no block delimiters. Your example uses {..}, and
> doesn't have endif.
>

Just out of habit. I think that PHP, like C, lets you avoid the block
deliminators so long as the block all fits on one line.


-- 
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Language mavens: Is there a programming with "if then else ENDIF" syntax?

2009-11-18 Thread Simon Forman
On Wed, Nov 18, 2009 at 4:15 AM, Steve Howell  wrote:
> On the topic of "switch" statements and even-more-concise-then-we-have-
> already if/elif/else/end constructs, I have to say that Python does
> occasionally force you to write code like the code below.  Maybe
> "force" is too strong a word, but Python lends itself to if/elif
> blocks like below, which get the job done just fine, but which are not
> syntactically pretty, due to the "(el){0,1}if kind ==" duplication.
> There are often cases where if/elif statements are just a smell that
> you do not know how to do dictionary lookups, but if you converted the
> below code to use dictionary lookups, you would complicate the code
> almost as much as you abstracted the code, if not more, unless I am
> just being very naive.  Anonymous methods would help to a certain
> degree.  I am not saying I want either anonymous methods or switch
> statements, but the lack of either in a language leads to very
> procedural looking code when you use number-of-lines-of-code as a
> (possibly dubious) metric.
>
> Maybe this excerpt can be golfed down to something simpler, I would
> love to see it!
>
>        if kind == 'dict':
>            return dictionary_schema(ast)
>        elif kind == 'list':
>            method = dictionary_schema(ast)
>            return lambda lst: map(method, lst)
>        elif kind == 'attr':
>            return ((lambda obj: getattr(obj, ast.field)), ast.field)
>        elif kind == 'key':
>            return (lambda obj: obj.get(ast.field), ast.field)
>        elif kind == 'as':
>            method, old_name = schema(ast.parent)
>            return (method, ast.synonym)
>        elif kind == 'call':
>            method, old_name = schema(ast.parent)
>            def call(obj):
>                return method(obj)()
>            return (call, old_name)
>        elif kind == 'recurse':
>            expr = ast.expr
>            kind = expr.kind
>            method, field_name = schema(ast.parent)
>            if kind in ['attr', 'key']:
>                new_method, new_field_name = schema(expr)
>                field_name = new_field_name
>            elif kind in ['dict', 'list']:
>                new_method = schema(expr)
>            else:
>                raise Exception('unknown kind!')
>            def recurse(obj):
>                obj = method(obj)
>                return new_method(obj)
>            return (recurse, field_name)
>        else:
>            raise Exception('unknown kind!')


This code is perhaps not a great example of your point.  Every
"(el){0,1}if" clause (other than the ones in the 'recurse' branch) end
in return statements, so the "el's" are pointless.

FWIW I might write it like so:

class ASTthing:

def processAST(self, ast, kind):
try:
method = getattr(self, 'do_' + kind)
except AttributeError:
raise Exception('unknown kind!')

self.ast = ast

return method()

def do_dict(self):
return dictionary_schema(self.ast)

def do_list(self):
method = dictionary_schema(self.ast)
return lambda lst: map(method, lst)

def do_attr(self):
field_name = self.ast.field
return lambda obj: getattr(obj, field_name), field_name

def do_key(self):
field_name = self.ast.field
return lambda obj: obj.get(field_name), field_name

def do_as(self):
method, old_name = schema(self.ast.parent)
return method, self.ast.synonym

def do_call(self):
method, old_name = schema(self.ast.parent)
return lambda obj: method(obj)(), old_name

def do_recurse(self):
expr = self.ast.expr
kind = expr.kind

method, field_name = schema(self.ast.parent)

if kind in 'attrkey':
new_method, field_name = schema(expr)

elif kind in 'dictlist':
new_method = schema(expr)

else:
raise Exception('unknown kind!')

def recurse(obj):
obj = method(obj)
return new_method(obj)

return recurse, field_name


IMO, that's more pythonic and less complicated.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IOError: [Errno 28] No space left on device

2009-11-18 Thread Grant Edwards
On 2009-11-18, Diez B. Roggisch  wrote:
> hong zhang wrote:
>>> 
>>> > but following is good.
>>> >
>>> > cont_tx = 1
>>> > for i in
>>> glob.glob('/sys/kernel/debug/ieee80211/phy*/iwlagn/data/continuous_tx'):
>>> >with open(i, 'w') as f:
>>> >print >>f,
>>> cont_tx
>>> 
>>> Well, if that works, then what's your problem?
>> 
>> But error comes from following, above is good. That is point here.
>> 
>> def do_cont_tx( is_start):
>> global cont_tx_started, stdscr
>> if is_start == START and not cont_tx_started:
>> cont_tx = 1
>> for i in
>> glob.glob('/sys/kernel/debug/ieee80211/phy*/iwlagn/data/continuous_tx'):
>> with open(i, 'w') as f: print >>f, cont_tx --
>
> "cont_x --" doesn't work. So the above can't be the actual code.

You never want to post the actual code you're running.  That
would make it too easy for people to help.

-- 
Grant Edwards   grante Yow! My mind is making
  at   ashtrays in Dayton ...
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: WindowsError is not available on linux?

2009-11-18 Thread Aahz
In article ,
Peng Yu   wrote:
>
>It's not clear to me whether WindowsError is available on linux or
>not, after I read the document. 

Here's what I told a co-worker to do yesterday:

if os.name == 'nt':
DiskError = (OSError, WindowsError)
else:
DiskError = WindowsError

try:
disk_operation()
except DiskError:
logit()
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it."  --Brian W. Kernighan
-- 
http://mail.python.org/mailman/listinfo/python-list


A Good Mailer

2009-11-18 Thread Victor Subervi
Hi;
I need a good mailer that will enable me to mail email from web forms.
Suggestions?
TIA,
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python gui builders

2009-11-18 Thread Dave Cook
On 2009-11-18, sturlamolden  wrote:

> GPL

If it's an issue for your project, I suggest wxPython.  It's
cross-platform, fairly complete, and extensible.  But the API is
clunky compared to Qt.

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


Re: Minimally intrusive XML editing using Python

2009-11-18 Thread Nobody
On Wed, 18 Nov 2009 13:55:52 +0100, Thomas Lotze wrote:

> I wonder what Python XML library is best for writing a program that makes
> small modifications to an XML file in a minimally intrusive way. By that I
> mean that information the program doesn't recognize is kept, as are
> comments and whitespace, the order of attributes and even whitespace
> around attributes. In short, I want to be able to change an XML file while
> producing minimal textual diffs.
> 
> Most libraries don't allow controlling the order of and the whitespace
> around attributes, so what's generally left to do is store snippets of
> original text along with the model objects and re-use that for writing the
> edited XML if the model wasn't modified by the program. Does a library
> exist that helps with this? Does any XML library at all allow structured
> access to the text representation of a tag with its attributes?

Expat parsers have a CurrentByteIndex field, while SAX parsers have
locators. You can use this to identify the portions of the input which
need to be processed, and just copy everything else. One downside is that
these only report either the beginning (Expat) or end (SAX) of the tag;
you'll have to deduce the other side yourself.

OTOH, "diff" is probably the wrong tool for the job.

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


Re: Minimally intrusive XML editing using Python

2009-11-18 Thread Dave Angel

Thomas Lotze wrote:

Chris Rebert wrote:

  

Have you considered using an XML-specific diff tool such as:



I'm afraid I'll have to fall back to using such a thing if I don't find a
solution to what I actually want to do.

I do realize that XML isn't primarily about its textual representation, so
I guess I shouldn't be surprised if what I'm looking for doesn't exist.
Still, it would be nice if it did...

  
What's your real problem, or use case?  Are you just concerned with 
diffing, or are others likely to read the xml, and want it formatted the 
way it already is?  And how general do you need this tool to be?  For 
example, if the only thing you're doing is modifying existing attributes 
or existing tags, the "minimal change" would be pretty unambiguous.  But 
if you're adding tags, or adding content on what was an empty element, 
then the requirement gets fuzzy  And finding an existing library for 
something "fuzzy" is unlikely.


Sample input, change list, and desired output would be very  useful.

DaveA

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


Re: IOError: [Errno 28] No space left on device

2009-11-18 Thread hong zhang


--- On Wed, 11/18/09, Diez B. Roggisch  wrote:

> From: Diez B. Roggisch 
> Subject: Re: IOError: [Errno 28] No space left on device
> To: python-list@python.org
> Date: Wednesday, November 18, 2009, 12:11 PM
> hong zhang wrote:
> 
> > 
> > 
> > --- On Wed, 11/18/09, Grant Edwards 
> wrote:
> > 
> >> From: Grant Edwards 
> >> Subject: Re: IOError: [Errno 28] No space left on
> device
> >> To: python-list@python.org
> >> Date: Wednesday, November 18, 2009, 9:22 AM
> >> On 2009-11-18, hong zhang 
> >> wrote:
> >> 
> >> >> Apparently the harddisk where you stored
> the file
> >> is full?
> >> 
> >> It's not a real file, and takes up no space.
> >> 
> >> > I have plenty space see:
> >> > $ df -l
> >> > Filesystem
> >> 1K-blocks      Used
> >> Available Use% Mounted on
> >> > /dev/sda1
> >> 74027808   4910016
> >> 65357380   7% /
> >> 
> >> That doesn't matter.  /sys doesn't contain real
> >> files.  It's an
> >> API to access kernel internals.
> >> 
> >> > but following is good.
> >> >
> >> > cont_tx = 1
> >> > for i in
> >>
> glob.glob('/sys/kernel/debug/ieee80211/phy*/iwlagn/data/continuous_tx'):
> >> >with open(i, 'w') as f:
> >> >print >>f,
> >> cont_tx
> >> 
> >> Well, if that works, then what's your problem?
> > 
> > But error comes from following, above is good. That is
> point here.
> > 
> > def do_cont_tx( is_start):
> > global cont_tx_started, stdscr
> > if is_start == START and not cont_tx_started:
> > cont_tx = 1
> > for i in
> >
> glob.glob('/sys/kernel/debug/ieee80211/phy*/iwlagn/data/continuous_tx'):
> > with open(i, 'w') as f: print >>f, cont_tx --
> 
> "cont_x --" doesn't work. So the above can't be the actual
> code.

Sorry, -- is typo.


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


Re: IOError: [Errno 28] No space left on device

2009-11-18 Thread hong zhang


--- On Wed, 11/18/09, Grant Edwards  wrote:

> From: Grant Edwards 
> Subject: Re: IOError: [Errno 28] No space left on device
> To: python-list@python.org
> Date: Wednesday, November 18, 2009, 9:22 AM
> On 2009-11-18, hong zhang 
> wrote:
> 
> >> Apparently the harddisk where you stored the file
> is full?
> 
> It's not a real file, and takes up no space.
> 
> > I have plenty space see:
> > $ df -l
> > Filesystem       
>    1K-blocks      Used
> Available Use% Mounted on
> > /dev/sda1         
>    74027808   4910016 
> 65357380   7% /
> 
> That doesn't matter.  /sys doesn't contain real
> files.  It's an
> API to access kernel internals.
> 
> > but following is good.
> >
> > cont_tx = 1
> > for i in
> glob.glob('/sys/kernel/debug/ieee80211/phy*/iwlagn/data/continuous_tx'):
> >      with open(i, 'w') as f:
> >          print >>f,
> cont_tx
> 
> Well, if that works, then what's your problem?

But error comes from following, above is good. That is point here.

def do_cont_tx( is_start):
global cont_tx_started, stdscr
if is_start == START and not cont_tx_started:
cont_tx = 1
for i in 
glob.glob('/sys/kernel/debug/ieee80211/phy*/iwlagn/data/continuous_tx'):
with open(i, 'w') as f:
print >>f, cont_tx 


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


Re: non-copy slices

2009-11-18 Thread Terry Reedy

tbour...@doc.ic.ac.uk wrote:

Hi,

I was looking for a facility similar to slices in python library that 
would avoid the implicit creation of a new list and copy of elements 
that is the default behaviour. Instead I'd rather have a lazy iteratable 
object on the original sequence. Well, in the end I wrote it myself but 
I was wondering if I missed sth in the library  If I didn't is there a
particular reason there isn't sth like that? I find it hard to believe 
that all slice needs have strictly copy semantics.


It is a strict *shallow* copy. There is no copying of contents.
That aside, you are right, hence itertools.islice as already mentioned. 
In the design of 3.0, I believe the idea was raised of making slices 
iterables in 3.0, just as was done for map, filter, and range. However, 
it would have been highly disruptive, and not save much space. Map and 
range create an unbounded number of new objects, rather than just a 
sequence of references to existing objects (or bytes or words for bytes 
and str slices). There is also the problem of virtual slices preventing 
garbage collection of the source sequence when it is not otherwise needed.


Terry Jan Reedy

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


Re: getting properly one subprocess output

2009-11-18 Thread Nobody
On Wed, 18 Nov 2009 12:25:14 +0100, Jean-Michel Pichavant wrote:

> I'm currently inspecting my Linux process list, trying to parse it in 
> order to get one particular process (and kill it).
> I ran into an annoying issue:
> The stdout display is somehow truncated (maybe a terminal length issue, 
> I don't know), breaking my parsing.

> As you can see, to complete process command line is truncated.
> Any clue on how to get the full version ?

If you only need it to work on Linux, you can just enumerate 
/proc/[1-9]*/exe or /proc/[1-9]*/cmdline.

Or you can add -ww to "ps" to avoid truncating the output.

Note that the /proc/*/exe report the actual executable. The command line
reported by "ps" (from /proc/*/cmdline) can be modified by the program, so
it doesn't necessarily reflect the program being run.


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


Re: Calling Python functions from Excel

2009-11-18 Thread Cannonbiker
On 18 lis, 03:09, "Mark Tolonen"  wrote:
> "Chris Withers"  wrote in message
>
> news:4b02d1e3.6080...@simplistix.co.uk...
>
> > Mark Tolonen wrote:
>
>  Please I need Calling Python functions from Excel and receive result
>  back in Excel. Can me somebody advise simplest solution please? I am
>  more VBA programmer than Python.
>
> >>> Tryhttp://code.google.com/p/pyinex/
>
> >> The book Python: Programming on Win32 has a whole chapter on COM, and a
> >> section on COM servers.
>
> > ...and it's generally accepted that COM sucks rocks through straws, so
> > explore alternatives when they're available ;-)
>
> > Chris
>
> True, but as usual Python makes it pretty darn easy (requires PyWin32):
>
> - ex.py ---
> class Example(object):
>     _public_methods_ = ['Add','Mul']
>     _reg_progid_ = 'MyPython.Example'
>     _reg_clsid_ = '{insert_GUID_here}'
>
>     def Add(self,a,b):
>         return a+b
>
>     def Mul(self,a,b):
>         return a*b
>
> if __name__ == '__main__':
>     import win32com.server.register
>     win32com.server.register.UseCommandLine(Example)
> -
>
> -- Excel Macro --
> Sub Testit()
>     Set ex = CreateObject("MyPython.Example")
>     Range("A1") = ex.Add(1, 2)
>     Range("A2") = ex.Mul(3, 4)
> End Sub
> 
>
> Just run the script to register the server.  "ex.py --unregister" will
> remove it.
>
> -Mark

Thanks very much. It works perfectly!!! :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Command line arguments??

2009-11-18 Thread Nobody
On Tue, 17 Nov 2009 23:57:55 +, Rhodri James wrote:

>>> Quote the filenames or escape the spaces:
>>>
>>> C:\Python26\Python.exe C:\echo.py "C:\New Folder\text.txt"
>>>
>>> We've been living with this pain ever since windowed GUIs encouraged  
>>> users
>>> to put spaces in their file names (Apple, I'm looking at you!).
>>> Fundamentally, if people want the pretty they have to live with the
>>> consequences.
>>
>> We've been living with much worse ever since Unix allowed users to put
>> not only spaces but even newlines in their filenames.
> 
> You'll notice I said "encouraged", not "allowed".

You'll notice I said "allowed", not "encouraged".

Code which can't handle spaces in filenames is broken from the outset; it
doesn't suddenly break the first time that someone passes a filename
containing spaces.

I have a suspicion that Win95 put spaces in two of the most important
directory names specifically to force developers to deal with this.

Nowadays, any Windows program which cannot handle spaces in pathnames is
usually a port of a Unix program (and a shoddy one at that).

OTOH, I'm surprised at how much Windows software still cannot handle
filenames outside of the system codepage (i.e. it's using the
byte-oriented API rather than the Unicode API).

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


Re: Minimally intrusive XML editing using Python

2009-11-18 Thread Anthra Norell

Thomas Lotze wrote:

Chris Rebert wrote:

  

Have you considered using an XML-specific diff tool such as:



I'm afraid I'll have to fall back to using such a thing if I don't find a
solution to what I actually want to do.

I do realize that XML isn't primarily about its textual representation, so
I guess I shouldn't be surprised if what I'm looking for doesn't exist.
Still, it would be nice if it did...

  

Thomas,
  I just might have what you are looking for. But I want to be sure I 
understand your problem. So, please show a small sample and explain how 
you want it modified.

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


Re: IOError: [Errno 28] No space left on device

2009-11-18 Thread Diez B. Roggisch
hong zhang wrote:

> 
> 
> --- On Wed, 11/18/09, Grant Edwards  wrote:
> 
>> From: Grant Edwards 
>> Subject: Re: IOError: [Errno 28] No space left on device
>> To: python-list@python.org
>> Date: Wednesday, November 18, 2009, 9:22 AM
>> On 2009-11-18, hong zhang 
>> wrote:
>> 
>> >> Apparently the harddisk where you stored the file
>> is full?
>> 
>> It's not a real file, and takes up no space.
>> 
>> > I have plenty space see:
>> > $ df -l
>> > Filesystem
>> 1K-blocks      Used
>> Available Use% Mounted on
>> > /dev/sda1
>> 74027808   4910016
>> 65357380   7% /
>> 
>> That doesn't matter.  /sys doesn't contain real
>> files.  It's an
>> API to access kernel internals.
>> 
>> > but following is good.
>> >
>> > cont_tx = 1
>> > for i in
>> glob.glob('/sys/kernel/debug/ieee80211/phy*/iwlagn/data/continuous_tx'):
>> >with open(i, 'w') as f:
>> >print >>f,
>> cont_tx
>> 
>> Well, if that works, then what's your problem?
> 
> But error comes from following, above is good. That is point here.
> 
> def do_cont_tx( is_start):
> global cont_tx_started, stdscr
> if is_start == START and not cont_tx_started:
> cont_tx = 1
> for i in
> glob.glob('/sys/kernel/debug/ieee80211/phy*/iwlagn/data/continuous_tx'):
> with open(i, 'w') as f: print >>f, cont_tx --

"cont_x --" doesn't work. So the above can't be the actual code.

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


Re: break LABEL vs. exceptions + PROPOSAL

2009-11-18 Thread Terry Reedy

Chris Rebert wrote:

On Wed, Nov 18, 2009 at 4:05 AM, Lo'oris  wrote:

I've found this email, back from 10 years ago:
http://mail.python.org/pipermail/python-list/1999-September/009983.html

I guess it went unnoticed, because that proposal looks really
intresting.


I think it went unnoticed because it is not very good, once looked at.


• break labels have been refused into python
• we can do it anyway using exceptions


So the proposal is not needed


• this is a proposal for something better, resembling "the exception
way" and much more powerful and python-like than break labels


It amounts to duplicating raise x...exception x as break xcontinue x 
in the name of aesthetics and supposed efficiency. There would be no new 
functionality nor any abbreviation of code. The semantics of 
break/continue as specific loop subcommands would be changed to 'use 
anyplace'. The OP gives as a reason the possibility of a typo creating a 
raise x ... except y mis-match. But a break x ... continue y mismatch is 
equally likely.


Anyway, I think the example given would be better written with immediate 
assignment followed by simple break, rather than the proposed delayed 
assignment.


The exception example as given would have to be rewritten to work in 3.x.


You're gonna have to wait 18-24 months:
http://www.python.org/dev/peps/pep-3003/

Also, the python-ideas list might be a better forum for discussing
this than the general-interest list:
http://mail.python.org/mailman/listinfo/python-ideas


This is a fine place to discuss it.

Terry Jan Reedy


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


Re: DOM related question and problem

2009-11-18 Thread Chris Rebert
On Wed, Nov 18, 2009 at 10:04 AM, elca  wrote:
> Hello,
> these day im making python script related with DOM.
>
> problem is these day many website structure is very complicate .
>
> what is best method to check DOM structure and path..
>
> i mean...following is some example.
>
> what is best method to check  can extract such like following info quickly?
>
> before i was spent much time to extract such info .
>
> and yes im also new to python and DOM.
>
>    IE.Document.Frames(1).Document.forms('comment').value = 'hello'
>
> if i use DOM inspector, can i extract such info quickly ? if so would you
> show me some sample?
>
> here is some site . i want to extract some dom info.
>
> today i was spent all day long to extract what is dom info. but failed
>
> http://www.segye.com/Articles/News/Politics/Article.asp?aid=20091118001261&ctg1=06&ctg2=00&subctg1=06&subctg2=00&cid=010101060
>
> at the end of this page,can find some comment input box.
>
> i want to know what kind of dom element should have to use, such like
>
>    IE.Document.Frames(1).Document.forms('comment').value = 'hello'
>
> anyhelp much appreciate thanks

This sounds suspiciously like a spambot. Why do you want to submit
comments in an automated fashion exactly?

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: Urwid 0.9.9 - Console UI Library

2009-11-18 Thread Chris Jones
I noticed that when run on a 256-color capable xterm, upon exiting the
demo programs the colors in the bash shell are modified - e.g the bash
prompt, the output of colored ls commands. 

For instance, due to my fiddling with dircolors, a file with executable
flags on is normally displayed in light green and it now appears in
bright green, presumably #00ff00.

CJ



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


DOM related question and problem

2009-11-18 Thread elca

Hello,
these day im making python script related with DOM.

problem is these day many website structure is very complicate .

what is best method to check DOM structure and path..

i mean...following is some example.

what is best method to check  can extract such like following info quickly?

before i was spent much time to extract such info .

and yes im also new to python and DOM.

IE.Document.Frames(1).Document.forms('comment').value = 'hello'

if i use DOM inspector, can i extract such info quickly ? if so would you
show me some sample?

here is some site . i want to extract some dom info. 

today i was spent all day long to extract what is dom info. but failed

http://www.segye.com/Articles/News/Politics/Article.asp?aid=20091118001261&ctg1=06&ctg2=00&subctg1=06&subctg2=00&cid=010101060

at the end of this page,can find some comment input box.

i want to know what kind of dom element should have to use, such like 

IE.Document.Frames(1).Document.forms('comment').value = 'hello'

anyhelp much appreciate thanks


-- 
View this message in context: 
http://old.nabble.com/DOM-related-question-and-problem-tp26412730p26412730.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: IOError: [Errno 28] No space left on device

2009-11-18 Thread hong zhang


--- On Wed, 11/18/09, Grant Edwards  wrote:

> From: Grant Edwards 
> Subject: Re: IOError: [Errno 28] No space left on device
> To: python-list@python.org
> Date: Wednesday, November 18, 2009, 9:22 AM
> On 2009-11-18, hong zhang 
> wrote:
> 
> >> Apparently the harddisk where you stored the file
> is full?
> 
> It's not a real file, and takes up no space.
> 
> > I have plenty space see:
> > $ df -l
> > Filesystem       
>    1K-blocks      Used
> Available Use% Mounted on
> > /dev/sda1         
>    74027808   4910016 
> 65357380   7% /
> 
> That doesn't matter.  /sys doesn't contain real
> files.  It's an
> API to access kernel internals.
> 
> > but following is good.
> >
> > cont_tx = 1
> > for i in
> glob.glob('/sys/kernel/debug/ieee80211/phy*/iwlagn/data/continuous_tx'):
> >      with open(i, 'w') as f:
> >          print >>f,
> cont_tx
> 
> Well, if that works, then what's your problem?

But error comes from following, above is good. That is point here.

def do_cont_tx( is_start):
global cont_tx_started, stdscr
if is_start == START and not cont_tx_started:
cont_tx = 1
for i in 
glob.glob('/sys/kernel/debug/ieee80211/phy*/iwlagn/data/continuous_tx'):
with open(i, 'w') as f:
print >>f, cont_tx -- 

>                
>            
>    visi.com         
>   PREVIOUS LIFE as a COMPLETE
>                
>                
>                
>    STRANGER?
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 


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


Re: _winreg error on open key (64bit) - proper usage of _winreg.DisableReflectionKey

2009-11-18 Thread Brian Curtin
On Tue, Nov 17, 2009 at 19:59, Mark Hammond wrote:

> On 18/11/2009 6:29 AM, Randall Walls wrote:
>
>> I don't believe so, but it seems like I'm in a catch 22, where I need to
>> _winreg.OpenKey the key first before I can pass it to
>> _winreg.DisableReflectionKey, but it doesn't exist, so I can't open it.
>>
>> I did find out that I can open the key using:
>> hKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
>> r"SOFTWARE\ODBC\ODBC.INI\
>> DRSQL2000_mu0100\\", 0, _winreg.KEY_READ | _winreg.KEY_WOW64_64KEY)
>>
>> The 'trick' was adding _winreg.KEY_WOW64_64KEY, which apparently tells
>> the system to look in the 64bit key area, and not under the Wow6432Node.
>> That brings up problem #2, though... I can't seem to CREATE a key in the
>> above path, and _winreg.CreateKey doesn't accept _winreg.KEY_WOW64_64KEY
>> (in fact it doesn't accept any options other than key, sub_key).
>> _winreg.CreateKey does work, it just puts the key in
>> SOFTWARE\Wow6432Node\ODBC\ODBC.INI. So I'm in a quandry... I'd like to
>> use one or the other, and not have to account for both.
>>
>
> It looks like _winreg needs to be enhanced to make the RegCreateKeyEx API
> function available.  It can be called via the win32api module of pywin32, or
> could also be called via ctypes.
>
> HTH,
>
> Mark
>

Created http://bugs.python.org/issue7347 and added a patch which I think
will solve the problem moving forward.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using struct module on a file

2009-11-18 Thread Jon Clements
On Nov 18, 4:42 pm, Ulrich Eckhardt  wrote:
> Hia!
>
> I need to read a file containing packed "binary" data. For that, I find the
> struct module pretty convenient. What I always need to do is reading a chunk
> of data from the file (either using calcsize() or a struct.Struct instance)
> and then parsing it with unpack(). For that, I repeatedly write utility
> functions that help me do just that, but I can't imagine that there is no
> better way for that.
>
> Questions:
> 0. Am I approaching this from the wrong direction? I'm not a programming
> noob, but rather new to Python still.
> 1. The struct module has pack_into() or unpack_from(), could I somehow
> combine that with a file?
> 2. Is there some easier way to read files? I know about array and xdrlib,
> but neither really fit my desires.
> 3. Failing all that, would you consider this a useful addition to the struct
> module, i.e. should I write a feature request?
>
> Thanks!
>
> Uli

First time I've seen zero based indexing for paragraph markers :)

unpack_from() will work on anything that supports the buffer
interface.

To work with files you can use something like:

my4chars = struct.Struct('4c')
def struct_read(s, f):
  return s.unpack_from(f.read(s.size))

Which isn't hideously painful.

Jon.



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


Re: ANN: a mini-language for encapsulating deep-copy operations on Python data structures

2009-11-18 Thread Steve Howell
On Nov 18, 4:34 am, "M.-A. Lemburg"  wrote:
> Steve Howell wrote:
> > [...]
> > Here is an example of the DDL (and I hate the terminology "DDL," just
> > cannot think of anything better):
>
> >             {
> >                 'show_table_of_contents',
> >                 'author' {
> >                     .person 'name',
> >                     .person 'location' as city,
> >                     .favorite_books()[
> >                         .title,
> >                         .cost() as expense
> >                         ] as books}
> >             }
>
> > There are more details here:
>
> >http://showellonprogramming.blogspot.com/2009/11/more-on-python-deep-...

Thanks for your feedback, Marc-Andre!  Comments inline...

>
> Personally, I find the explicit approach more intuitive, since
> you immediately see what you are going to get:
>
> show_table_of_contents = context['show_table_of_contents']
> author = context['author']
> d = {
>     'show_table_of_contents': show_table_of_contents,
>     'author': {
>         'name': author.person['name'],
>         'city': author.person['location'],
>         'books': [{
>             'title': item.title,
>             'expense': item.cost(),
>             }
>             for item in author.favorite_books()],
>         },
>     }
>

I understand what you mean about the explicit approach.  When you go
for brevity, you sacrifice some explicitness. One of the things I'm
actually looking to do next is to be able to generate code like what
you've written above.  You could make the generated code even more
procedural in terms of how it walks the structure--for example,
replace the list comprehension with a for loop.  This could be useful
for inserting tracing code, for example.

> I particularly find this part non-intuitive:
>
> >                     .favorite_books()[
> >                         .title,
> >                         .cost() as expense
> >                         ] as books}
>

Yep, I think what I really want is this:

.favorite_books()[{
.title,
.cost() as expense
}] as books}

I am not sure the curlies make the transformation completely
intuitive, but they are more suggestive that you are generating a list
of dictionaries.

> and would leave in the square brackets for __getitem__
> lookups on these:
>
> >                     .person 'name',
> >                     .person 'location' as city,
>

Yep, I agree.  The square brackets were not a deliberate omission.
They were just slightly more tricky to implement because of '[' having
double syntactical meaning.

> For additional inspiration, you might want to look at XSLT
> which provides similar transformations on XML data structures.
>
> There are also a number of other transformation languages:
>
> http://en.wikipedia.org/wiki/Model_Transformation_Languagehttp://en.wikipedia.org/wiki/Data_transformation
>

Thanks for the links!

> Regarding the term "DDL": that's normally used for "data definition
> language" and doesn't really have all that much to do with
> transforming data. You normally define data structures using
> DDL - without actually putting data into those structures.
>
> Why not "PyDTL" Python data transformation language ?!
>

Yep, I like that suggestion.  The language does indeed describe a
transformation.  In some ways I was wanting to think of it more
broadly, in terms that the transformation also acts as a schema for
any input objects.  For example, you could use '{.title, .publisher,
{.name} }' as a schema to validate that an object behaves like a
book.  I suppose that's still a transformation in an abstract sense,
where the output is just True, False, or maybe even the slice of the
transformation that can/cannot be executed.

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


Re: Beautifulsoup code that is not running

2009-11-18 Thread Peter Pearson
On Tue, 17 Nov 2009 14:38:55 -0800 (PST), Zeynel  wrote:
[snip]
 from BeautifulSoup import BeautifulSoup
>
 soup = BeautifulSoup (file("test.html").read())
 title = soup.find('title')
 titleString = title.string
 open('extract.text', 'w').write(titleString)
>
> This runs without an error, but nothing is written into the
> extract.text file. test.html has  tags in it.

Hmm.  Works for me, but of course I don't have your test.html.
Why don't you try examining "title" and "titleString"?  Perhaps
 has resulted in titleString being the empty string.

-- 
To email me, substitute nowhere->spamcop, invalid->net.
-- 
http://mail.python.org/mailman/listinfo/python-list


question about tree in python

2009-11-18 Thread nospam
How should I write a tree using diconary. I have used a dictonary to 
make a tree.


tree={best:collections.defaultdict(lambda:default)} in a id3 tree.

Is there a way to multple values and when then only return on type of 
values.


I tried this apporach but it did not work.

class Tree:
def __init__(self):
pass



def ini(self,default_tree,d_attr):
self.tree={d_attr:collections.defaultdict(lambda:default_tree)}
self.tree_A={d_attr:collections.defaultdict(lambda:default_tree)}
self.tree_I={d_attr:collections.defaultdict(lambda:default_tree)}
self.tree_p={d_attr:collections.defaultdict(lambda:default_tree)}
self.tree_n={d_attr:collections.defaultdict(lambda:default_tree)}
return self.tree

def __call__(self,best,val,subtree):
self.tree[best][val]=subtree

def 
input_tree(self,best,val,subtree,postive,negative,attribute_value,info_gain):

tree=self.tree
print best
print val
tree[best][val]=subtree
print self.tree
self.tree_A[best][val]= attribute_value
self.tree_I[best][val]= info_gain
self.tree_p[best][val]= postive
self.tree_n=negative
tree=self.tree
return tree
--
http://mail.python.org/mailman/listinfo/python-list


using struct module on a file

2009-11-18 Thread Ulrich Eckhardt
Hia!

I need to read a file containing packed "binary" data. For that, I find the 
struct module pretty convenient. What I always need to do is reading a chunk 
of data from the file (either using calcsize() or a struct.Struct instance) 
and then parsing it with unpack(). For that, I repeatedly write utility 
functions that help me do just that, but I can't imagine that there is no 
better way for that.

Questions:
0. Am I approaching this from the wrong direction? I'm not a programming 
noob, but rather new to Python still.
1. The struct module has pack_into() or unpack_from(), could I somehow 
combine that with a file?
2. Is there some easier way to read files? I know about array and xdrlib, 
but neither really fit my desires.
3. Failing all that, would you consider this a useful addition to the struct 
module, i.e. should I write a feature request?

Thanks!

Uli

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


Re: getting properly one subprocess output

2009-11-18 Thread Jon Clements
On Nov 18, 4:14 pm, Jon Clements  wrote:
> On Nov 18, 11:25 am, Jean-Michel Pichavant 
> wrote:
>
>
>
> > Hi python fellows,
>
> > I'm currently inspecting my Linux process list, trying to parse it in
> > order to get one particular process (and kill it).
> > I ran into an annoying issue:
> > The stdout display is somehow truncated (maybe a terminal length issue,
> > I don't know), breaking my parsing.
>
> > import subprocess
> > commandLine = ['ps', '-eo "%p %U %P %y %t %C %c %a"']
> > process = subprocess.Popen(commandLine, stdout=subprocess.PIPE,
> > stderr=subprocess.PIPE)
> > processList, stderrdata = process.communicate()
>
> > Here is a sample of what I get in processList.split('\n'):
>
> >  ' "25487 1122      4344 ?         7-17:48:32  2.5 firefox-bin    
> > /usr/lib/iceweasel/firefox-"',
> >  ' "25492 1122      4892 pts/6          00:08 57.2 ipython        
> > /usr/bin/python /usr/bin/ip"',
>
> > As you can see, to complete process command line is truncated.
> > Any clue on how to get the full version ?
>
> > JM
>
> > (python 2.5)
>
> What about "ps -eo pid,tty,cmd" ?
>
> Sample:
> 12680 ?        geany /usr/share/gramps/ReportBase/
> _CommandLineReport.py
> 12682 ?        gnome-pty-helper
> 12683 pts/0    /bin/bash
> 13038 ?        gnome-terminal
> 13039 ?        gnome-pty-helper
> 13040 pts/1    bash
> 13755 pts/1    ps -eo pid,tty,cmd
>
> ...etc...
>
> hth,
>
> Jon.

Another thought: if you're only wanting to find and kill a process,
what about pkill? Saves you having to filter the list in Python and
then issue a kill command.

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


Re: getting properly one subprocess output

2009-11-18 Thread Jon Clements
On Nov 18, 11:25 am, Jean-Michel Pichavant 
wrote:
> Hi python fellows,
>
> I'm currently inspecting my Linux process list, trying to parse it in
> order to get one particular process (and kill it).
> I ran into an annoying issue:
> The stdout display is somehow truncated (maybe a terminal length issue,
> I don't know), breaking my parsing.
>
> import subprocess
> commandLine = ['ps', '-eo "%p %U %P %y %t %C %c %a"']
> process = subprocess.Popen(commandLine, stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
> processList, stderrdata = process.communicate()
>
> Here is a sample of what I get in processList.split('\n'):
>
>  ' "25487 1122      4344 ?         7-17:48:32  2.5 firefox-bin    
> /usr/lib/iceweasel/firefox-"',
>  ' "25492 1122      4892 pts/6          00:08 57.2 ipython        
> /usr/bin/python /usr/bin/ip"',
>
> As you can see, to complete process command line is truncated.
> Any clue on how to get the full version ?
>
> JM
>
> (python 2.5)

What about "ps -eo pid,tty,cmd" ?

Sample:
12680 ?geany /usr/share/gramps/ReportBase/
_CommandLineReport.py
12682 ?gnome-pty-helper
12683 pts/0/bin/bash
13038 ?gnome-terminal
13039 ?gnome-pty-helper
13040 pts/1bash
13755 pts/1ps -eo pid,tty,cmd

...etc...


hth,

Jon.



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


  1   2   >